Skip to content

Instantly share code, notes, and snippets.

@penn201500
Forked from celadevra/switch.el
Last active December 13, 2021 13:04
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save penn201500/fd445603ea05faef4c9f5b2e102613ad to your computer and use it in GitHub Desktop.
Save penn201500/fd445603ea05faef4c9f5b2e102613ad to your computer and use it in GitHub Desktop.
Sane switching of input methods when using evil-mode in Emacs on OSX;change some conf for qwerty keyboard, and default-im keyboard changed to be self-conf.
;; switch to english input method when switching to normal mode
;; and switch back when entering insert/replace modes
;; need external script support, currently mac-only
(defvar default-im "org.unknown.keylayout.layoutformc" "Default ascii-only input method")
(defvar prev-im (substring (shell-command-to-string "/usr/local/bin/im-select") 0 -1)
"IM that I use when starting Emacs and exiting insert mode")
(defun im-use-english ()
"Switch to english input method on a Mac. im-select is a tool
provided at https://github.com/daipeihust/im-select"
(interactive)
(cond ((eq system-type 'darwin)
(call-process-shell-command (concat "/usr/local/bin/im-select " default-im)))))
(defun im-remember ()
"Remember the input method being used in insert mode,
so we can switch to it in other modes."
(interactive)
(cond ((eq system-type 'darwin)
(setq prev-im (substring (shell-command-to-string "/usr/local/bin/im-select") 0 -1)))))
(defun im-use-prev ()
"Use previous input method.
If previous input method is not defined, use default method"
(interactive)
(cond ((eq system-type 'darwin)
(if prev-im
(call-process-shell-command (concat "/usr/local/bin/im-select " prev-im))
(call-process-shell-command (concat "/usr/local/bin/im-select " default-im))))))
(add-hook 'evil-normal-state-entry-hook 'im-use-english)
(add-hook 'evil-insert-state-entry-hook 'im-use-prev)
(add-hook 'evil-insert-state-exit-hook 'im-remember)
(add-hook 'evil-replace-state-entry-hook 'im-use-prev)
(add-hook 'evil-replace-state-exit-hook 'im-remember)
(add-hook 'evil-emacs-state-entry-hook 'im-use-english)
(provide 'self-ime)
;;;self-ime.el ends here
@penn201500
Copy link
Author

default-im "org.unknown.keylayout.layoutformc"
--this is self-conf keyboard, you can use default im of mac: org.apple.keylayout.ABC

@penn201500
Copy link
Author

to enable ime auto-switch in emacs evil-insert-mode and evil-normal-mode:
fcitx-remote-for-osx for sogou-pinyin can not be installed on mac 10.14. Don't know why. This el is a great solution!!

tks to: https://gist.github.com/celadevra/7ae45920e2494fbc38ef

@zhangjie2012
Copy link

worked for me, thanks.

btw, run /usr/local/bin/im-select get current input code, for example, my default english input is com.apple.keylayout.US .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment