[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Buffer-local key-translation-map
From: |
Spencer Baugh |
Subject: |
Buffer-local key-translation-map |
Date: |
Thu, 14 Dec 2023 18:30:28 -0500 |
User-agent: |
Gnus/5.13 (Gnus v5.13) |
Is it OK to have key-translation-map be a buffer-local variable?
If there's a key translation in function-key-map which I wish to change
on a per-buffer basis, is it correct to delete that key translation from
function-key-map and add it back in a buffer-local key-translation-map?
More context:
In graphical frames, Emacs maps <escape> to ESC with function-key-map.
evil-mode users would usually prefer for this to not happen; they never
want to hit <escape> and have it be potentially interpreted as the start
of a key sequence. (For example, if an evil-mode user types C-x
<escape>, they would prefer that the key sequence be aborted, not that
it be interpreted as potentially the start of C-x M-: or other such
bindings)
The most obvious way to do this is simply:
(define-key function-key-map (kbd "<escape>") nil)
However, this affects all buffers identically. A user might want to
have some buffers in evil-mode (really evil-local-mode) and some not;
or, more likely, a user might want to make use of "Emacs state", which
allows disabling all evil-mode keybindings in a per-buffer way. For
such buffers, <escape> should still map to ESC.
So then, the next obvious thought is to run:
(setq-local function-key-map
(define-keymap :parent function-key-map
"<escape>" nil))
in any buffer which wants to use evil-mode keybindings, and therefore
disable mapping <escape> to ESC.
However, function-key-map and local-function-key-map cannot be
buffer-local variables.
But luckily key-translation-map can! So then we could just move the
translation of <escape> to ESC to be in key-translation-map, where it
can be overridden buffer-locally. Which looks like:
(define-key function-key-map (kbd "<escape>") nil)
(define-key key-translation-map (kbd "<escape>") (kbd "ESC"))
;; in evil-mode buffers
(setq-local key-translation-map
(define-keymap :parent key-translation-map
"<escape>" nil))
This seems to work fine. But is this supported? Is there a better way
to do this?
- Buffer-local key-translation-map,
Spencer Baugh <=