Emacs determining keyboard layout

前端 未结 1 1837
庸人自扰
庸人自扰 2021-02-14 07:54

Is there a way for Emacs to detect the current keyboard layout?

I often write texts in English and German, switching the (Win OS) keyboard layout. However, some function

1条回答
  •  悲哀的现实
    2021-02-14 08:35

    Consider using M-x set-input-method and M-x toggle-input-method. Toggle is bound to C-\, set is bound to C-x RET C-\. I recommend this binding, if you have hyper key:

    (global-set-key [?\H-\\] 'set-input-method).

    If you asking not how to type in different language, but how to make several commands work when you using different languages in your OS, try just to bind them. It worked nice on russian symbols. One black-black night i even wrote

    (setq russian-symbols '(
                             (?й . ?q)
                             (?ц . ?w)
                             (?у . ?e)
                             (?к . ?r)
                             (?е . ?t)
                             (?н . ?y)
                             (?г . ?u)
                             (?ш . ?i)
                             (?щ . ?o)
                             (?з . ?p)
                             (?х . ?\[)
                             (?ъ . ?\])
                             (?ф . ?a)
                             (?ы . ?s)
                             (?в . ?d)
                             (?а . ?f)
                             (?п . ?g)
                             (?р . ?h)
                             (?о . ?j)
                             (?л . ?k)
                             (?д . ?l)
                             (?ж . ?\;)
                             (?э . ?')
                             (?я . ?z)
                             (?ч . ?x)
                             (?с . ?c)
                             (?м . ?v)
                             (?и . ?b)
                             (?т . ?n)
                             (?ь . ?m)
                             (?б . ?,)
                             (?ю . ?.)
    
                             (?Й . ?Q)
                             (?Ц . ?W)
                             (?У . ?E)
                             (?К . ?R)
                             (?Е . ?T)
                             (?Н . ?Y)
                             (?Г . ?U)
                             (?Ш . ?I)
                             (?Щ . ?O)
                             (?З . ?P)
                             (?Х . ?{)
                             (?Ъ . ?})
                             (?Ф . ?A)
                             (?Ы . ?S)
                             (?В . ?D)
                             (?А . ?F)
                             (?П . ?G)
                             (?Р . ?H)
                             (?О . ?J)
                             (?Л . ?K)
                             (?Д . ?L)
                             (?Ж . ?:)
                             (?Э . ?\")
                             (?Я . ?Z)
                             (?Ч . ?X)
                             (?С . ?C)
                             (?М . ?V)
                             (?И . ?B)
                             (?Т . ?N)
                             (?Ь . ?M)
                             (?Б . ?<)
                             (?Ю . ?>)
    
                             (?Ё . ?~)
                             (?ё . ?`)
                             ))
    
    (setq russian-symbols-full (append russian-symbols
                                 '((?. . ?/)
                                 (?, . ??)
                                 (?\" . ?@)
                                 (?№ . ?#)
                                 (?\; . ?$)
                                 (?: . ?^)
                                 (?\? . ?&))))
    (defun cm-ru-to-en-string(string)
      (apply 'concat (mapcar (lambda (arg) (setq arg (format "%c" (or (cdr (assoc arg russian-symbols-full)) arg)))) string)))
    
    (defun cm-en-to-ru-string(string)
      (apply 'concat (mapcar (lambda (arg) (setq arg (format "%c" (or (car (rassoc arg russian-symbols-full)) arg)))) string)))
    
    (defun cm-ru-to-en-region()
      (interactive)
      (let ((text (buffer-substring-no-properties (mark) (point))))
        (delete-region (mark) (point))
        (insert (cm-ru-to-en-string text))))
    
    (defun cm-en-to-tu-region()
      (interactive)
      (let ((text (buffer-substring-no-properties (mark) (point))))
        (delete-region (mark) (point))
        (insert (cm-en-to-ru-string text))))
    
    ;; DO NOT USE vvv SINCE YOU WILL NOT BE ABLE TO INPUT THROUGH C-\
    
    ;; (let ((symbols russian-symbols))
    ;;   (while symbols
    ;;     (global-set-key (vector (car (car symbols))) (vector (cdr (car symbols))))
    ;;     (setq symbols (cdr symbols))))
    
    ;; DO NOT USE ^^^ SINCE YOU WILL NOT BE ABLE TO INPUT THROUGH C-\
    
    ;; (- ?\C-ы ?ы) ;;russian C-
    ;; (- ?\C-s ?s) ;;english C-
    ;; (- ?\M-ы ?ы) ;;russian M-
    ;; (- ?\M-s ?s) ;;english M-
    
    
    (setq russian-symbols-map1
      (append 
        (mapcar (lambda (arg) (setq arg (cons (+ (- ?\C-ы ?ы) (car arg)) (+ (- ?\C-s ?s) (cdr arg))))) russian-symbols)
        (mapcar (lambda (arg) (setq arg (cons (+ (- ?\M-ы ?ы) (car arg)) (+ (- ?\M-s ?s) (cdr arg))))) russian-symbols)
        (mapcar (lambda (arg) (setq arg (cons (+ (- ?\C-\M-ы ?ы) (car arg)) (+ (- ?\C-\M-s ?s) (cdr arg))))) russian-symbols)
        (mapcar (lambda (arg) (setq arg (cons (+ (- ?\H-ы ?ы) (car arg)) (+ (- ?\H-s ?s) (cdr arg))))) russian-symbols)
        (mapcar (lambda (arg) (setq arg (cons (+ (- ?\H-\C-ы ?ы) (car arg)) (+ (- ?\H-\C-s ?s) (cdr arg))))) russian-symbols)
        (mapcar (lambda (arg) (setq arg (cons (+ (- ?\H-\M-ы ?ы) (car arg)) (+ (- ?\H-\M-s ?s) (cdr arg))))) russian-symbols)
        (mapcar (lambda (arg) (setq arg (cons (+ (- ?\H-\C-\M-ы ?ы) (car arg)) (+ (- ?\H-\C-\M-s ?s) (cdr arg))))) russian-symbols)))
    
    (setq russian-symbols-map2
      (append
        russian-symbols-map1
        russian-symbols)) ; We must not start with russian letters, but if it is element in a sequence - in should be fine.
    
    (setq symbols2 russian-symbols-map1)    ; One-key sequence command.
    (let ((symbols2 russian-symbols-map1))
        (while symbols2
          (if
            (and (symbolp (lookup-key global-map
                            (vector
                              (cdr (car symbols2))
                              )))
              (lookup-key global-map
                (vector
                  (cdr (car symbols2))
                  )))
            (global-set-key
              (vector
                (car (car symbols2))
                )
              (lookup-key global-map
                (vector
                  (cdr (car symbols2))
                  ))))
          (setq symbols2 (cdr symbols2))))
    
    
    
    (let ((symbols1 russian-symbols-map2) (symbols2 russian-symbols-map1)) ; Two keys sequence
      (while symbols1
        (while symbols2
          (if
            (and (symbolp (lookup-key global-map
                            (vector
                              (cdr (car symbols2))
                              (cdr (car symbols1))
                              )))
              (lookup-key global-map
                (vector
                  (cdr (car symbols2))
                  (cdr (car symbols1))
                  )))
            (global-set-key
              (vector
                (car (car symbols2))
                (car (car symbols1))
                )
              (lookup-key global-map
                (vector
                  (cdr (car symbols2))
                  (cdr (car symbols1))
                  ))))
          (setq symbols2 (cdr symbols2)))
        (setq symbols2 russian-symbols-map1)
        (setq symbols1 (cdr symbols1))))
    
    (provide 'shamanizm) ;russian emacs-users should lol reading this
    

    It works. I've binded all global hotkeys up to 2 level with russian symbols.

    I am not using it now, it eat startup time, and it ruins readability of my *Help* with crazy things like It is bound to C-x b, C-x и, C-ч b, C-ч и. Use it wisely.

    0 讨论(0)
提交回复
热议问题