Skip to content

Instantly share code, notes, and snippets.

@celadevra
Last active November 19, 2021 10:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save celadevra/7ae45920e2494fbc38ef to your computer and use it in GitHub Desktop.
Save celadevra/7ae45920e2494fbc38ef to your computer and use it in GitHub Desktop.
Sane switching of input methods when using evil-mode in Emacs on OSX
;; 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 "com.apple.keylayout.Dvorak" "Default ascii-only input method")
(defvar prev-im (substring (shell-command-to-string "~/bin/im-select") 0 -1)
"IM that I use when starting Emacs and exiting insert mode")
(defun im-use-dvorak ()
"Switch to Dvorak input method on a Mac. im-select is a tool
provided at http://git.io/ndA8Mw"
(interactive)
(cond ((eq system-type 'darwin)
(call-process-shell-command (concat "~/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 "~/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 "~/bin/im-select " prev-im))
(call-process-shell-command (concat "~/bin/im-select " default-im))))))
(add-hook 'evil-normal-state-entry-hook 'im-use-dvorak)
(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-dvorak)
@heinwol
Copy link

heinwol commented Nov 19, 2021

Easy to adapt to a linux system (e.g. with xkb-switch). Works perfectly!

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