Skip to content

Instantly share code, notes, and snippets.

@BeanSecurity
Created August 19, 2019 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BeanSecurity/87d99501be74cd4f090231d2b8a757df to your computer and use it in GitHub Desktop.
Save BeanSecurity/87d99501be74cd4f090231d2b8a757df to your computer and use it in GitHub Desktop.
Smart auto switch layouts for insert and normal modes (Spacemacs+xkb)
(setq shell-switch-lang "xte 'keydown Shift_L' 'keydown Shift_R' 'keyup Shift_L' 'keyup Shift_R'") ;; shell command that switch layouts emulating setxkbmap toggle keybind (left Shift+right Shift for me)
;; 'xte' from 'xautomation' package
(setq get-current-lang (lambda () (substring (shell-command-to-string "getxkblayout") 0 2))) ;; get C script 'getxkblayout' and compile from my gist
(setq-default prev-lang (funcall get-current-lang)) ;; save previous layout
(add-hook 'evil-insert-state-entry-hook ;; change layout whiforle insert mode starting if its not similar to previous
(lambda ()
(if (not (equal prev-lang
(funcall get-current-lang)))
(shell-command shell-switch-lang))))
(add-hook 'evil-insert-state-exit-hook ;; change layout to default(eng) exiting insert mode
(lambda ()
(setq prev-lang (funcall get-current-lang))
(if (not (equal prev-lang "us"))
(shell-command shell-switch-lang))))
;; same for replace mode
(add-hook 'evil-replace-state-entry-hook
(lambda ()
(if (not (equal prev-lang
(funcall get-current-lang)))
(shell-command shell-switch-lang))))
(add-hook 'evil-replace-state-exit-hook
(lambda ()
(setq prev-lang (funcall get-current-lang))
(if (not (equal prev-lang "us"))
(shell-command shell-switch-lang))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment