I'm using the Dvorak keyboard layout and have swapped C-x
and C-t
, because I find it easier to type on Dvorak.
(define-key key-translation-map [?\C-x] [?\C-t])
(define-key key-translation-map [?\C-t] [?\C-x])
I'm also using god-mode
, though, so I'd like x
to be swapped with t
when in god-local-mode
, such that typing x
results in C-t
and typing t
results in C-x
. I have a very crude solution using keyboard-translate
:
(defun my-god-swap-on ()
(interactive)
(keyboard-translate ?x ?t)
(keyboard-translate ?t ?x))
(defun my-god-swap-off ()
(interactive)
(aset keyboard-translate-table ?t nil)
(aset keyboard-translate-table ?x nil))
(add-hook 'god-mode-enabled-hook 'my-god-swap-on)
(add-hook 'god-mode-disabled-hook 'my-god-swap-off)
Although this works it is extremely inelegant as it modifies global state. If I switched to a buffer where god-mode is not enabled, the translation would still be active. I could add yet another hook for buffer switching to make sure that the translation is disabled when god-local-mode
is inactive in the current buffer, but this seems like a very ugly mitigation technique.
Is it possible to restrict the effects to a keyboard translation to only a single minor mode?
If this is not possible, is there another more elegant way I could swap keys in god-mode
? I see that god-mode
rewires all keys bound to self-insert-command
to god-mode-self-insert
, but this uses this-command-keys-vector
to determine with what key it was called and I cannot seem to shadow the definition of this-command-keys-vector
with a let binding to swap x
and t
in a wrapper before passing control to god-mode-self-insert
.