my-toggle-input-method
;; {{ make IME compatible with evil-mode
(defun my-toggle-input-method ()
"When input method is on, goto `evil-insert-state'."
(interactive)
;; load IME when needed, less memory footprint
(when my-toggle-ime-init-function
(funcall my-toggle-ime-init-function))
;; some guys don't use evil-mode at all
(cond
((and (boundp 'evil-mode) evil-mode)
;; evil-mode
(cond
((eq evil-state 'insert)
(toggle-input-method))
(t
(evil-insert-state)
(unless current-input-method
(toggle-input-method))))
(cond
(current-input-method
;; evil-escape and pyim may conflict
;; @see https://github.com/redguardtoo/emacs.d/issues/629
(evil-escape-mode -1)
(message "IME on!"))
(t
(evil-escape-mode 1)
(message "IME off!"))))
(t
;; NOT evil-mode
(toggle-input-method))))
使用 (and (boundp 'evil-mode) evil-mode) 确认在 evil-mode 中, 再根据不同的 evil 状态采取不同的 toggle 行为。在 toggle 之后,根据输入法状态开关 evil-escape-mode 并提示输入法的开关。