Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to alternate two input methods in Emacs?

Tags:

emacs

I know that there is "C-\" to switch input methods, but that allows only one additional input method - for example, by default I have english qwerty layout, and I can alternate between it and dvorak.

But is there some way to do alternation between two other languages with the same ease? For example, if I want to alternate english-dvorak and my native language layout?

like image 320
Rogach Avatar asked Aug 20 '12 04:08

Rogach


2 Answers

To switch between 2 or more alternative input methods quickly, I've added the following code to my init.el:

;; Input method and key binding configuration.
(setq alternative-input-methods
      '(("russian-computer" . [?\C-\\])
        ("chinese-py-punct" . [?\C-|])
        ("german-postfix"   . [?\C-\M-|])))

(setq default-input-method
      (caar alternative-input-methods))

(defun toggle-alternative-input-method (method &optional arg interactive)
  (if arg
      (toggle-input-method arg interactive)
    (let ((previous-input-method current-input-method))
      (when current-input-method
        (deactivate-input-method))
      (unless (and previous-input-method
                   (string= previous-input-method method))
        (activate-input-method method)))))

(defun reload-alternative-input-methods ()
  (dolist (config alternative-input-methods)
    (let ((method (car config)))
      (global-set-key (cdr config)
                      `(lambda (&optional arg interactive)
                         ,(concat "Behaves similar to `toggle-input-method', but uses \""
                                  method "\" instead of `default-input-method'")
                         (interactive "P\np")
                         (toggle-alternative-input-method ,method arg interactive))))))

(reload-alternative-input-methods)

So, to switch to Russian, Chinese or German IME I use C-\, C-| and C-M-| accordingly. And to switch back to English I use the same key as for the current IME (i.e. if I have Chinese IME active, I'll switch back using C-|).

To configure use alternative-input-methods variable. It's a list of conses of an input method name and a key binding.

Note! If you activate an IME either by calling M-x toggle-input-method or by C-u C-\, pressing C-\ would switch to an input method according to alternative-input-methods variable (in the default configuration Emacs remembers new IME and uses it for C-\).

like image 144
Alexey Avatar answered Nov 16 '22 01:11

Alexey


If I type C-u C-\ to select one input method, and once again to select another, I find that every subsequent invocation uses the previous input method as the default input, so switching to the other input method boils down to C-u C-\ RET.

like image 3
legoscia Avatar answered Nov 16 '22 03:11

legoscia