5

我知道有“C-\”来切换输入法,但它只允许一种额外的输入法——例如,默认情况下我有英文 qwerty 布局,我可以在它和 dvorak 之间切换。

但是有什么方法可以同样轻松地在其他两种语言之间进行交替吗?例如,如果我想交替使用英语-德沃夏克语和我的母语布局?

4

3 回答 3

7

为了在 2 种或更多替代输入法之间快速切换,我将以下代码添加到我的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)

因此,要切换到俄语、中文或德语 IME,我使用,C-\相应地。为了切换回英文,我使用与当前 IME 相同的键(即,如果我激活了中文 IME,我将使用 切换回)。C-|C-M-|C-|

配置使用alternative-input-methods变量。它是输入法名称和键绑定的组合列表。

笔记!如果您通过调用M-x toggle-input-method或 通过激活 IME C-u C-\,则按C-\将根据变量切换到输入法alternative-input-methods(在默认配置中,Emacs 会记住新的 IME 并将其用于C-\)。

于 2013-04-03T17:19:43.427 回答
3

如果我键入C-u C-\选择一种输入法,然后再次选择另一种,我发现每次后续调用都使用以前的输入法作为默认输入,因此切换到另一种输入法归结为C-u C-\ RET.

于 2012-08-20T10:31:24.113 回答
0
(setq my/input-methods '(nil "some-input-method" "japanese" ))
(defvar my/input-method-switch-counts 0 "a count; more than index")
(make-variable-buffer-local 'my/input-method-switch-counts)
(put 'my/input-method-switch-counts 'permanent-local t)
(defun my/switch-input-method () 
  (interactive)
  (setq my/input-method-switch-counts
        (1+ my/input-method-switch-counts))
  (let ((i (nth (% my/input-method-switch-counts
                   (length my/input-methods)
                   )
                my/input-methods
                )))
    (set-input-method i)  ;;interactive use may set any needed
    )
  ;;(message "IME %s on" current-input-method)
  )

(global-set-key (kbd "C-<SPC>") 'my/switch-input-method)
;;also make sure mode-line-mule-info in mode-line-format to show current input-method in mode-line
于 2021-09-11T00:23:46.140 回答