Hi, I just encountered some unexpected behavior when defining key bindings for maps which have parent maps: When I define a key for some state for the parent map as well as the child map, the child map overrides the bindings for the parent map. Here is what I mean: (defun alok-foo () (interactive) (message "foo")) (defun alok-bar () (interactive) (message "bar")) (defvar alok-mode-map (make-sparse-keymap)) (defvar alok-child-mode-map (let ((map (make-sparse-keymap))) (set-keymap-parent map alok-mode-map) (define-key map (kbd "M-<return>") 'alok-bar) map)) (define-derived-mode alok-mode prog-mode "ALOK" "Major ALOK mode") (define-derived-mode alok-child-mode alok-mode "calok" "ALOK derived mode") (evil-set-initial-state 'alok-mode 'motion) (evil-set-initial-state 'alok-child-mode 'motion) (evil-define-key 'motion alok-mode-map (kbd "f") 'alok-foo) (evil-define-key 'motion alok-child-mode-map (kbd "f") 'alok-bar) When I enter into alok-mode and press `f`, I would ecpect to see "foo" in the message area, but instead I get "bar". I tried to find the reason for this myself, but I got stuck quickly. Is there a way to work around this? Thanks in advance! Albert -- Albert Krewinkel GPG: 8eed e3e2 e8c5 6f18 81fe e836 388d c0b2 1f63 1124
Am 08.09.2014 um 21:56 schrieb Albert Krewinkel:
Hi,
I just encountered some unexpected behavior when defining key bindings for maps which have parent maps: When I define a key for some state for the parent map as well as the child map, the child map overrides the bindings for the parent map.
The reason is that the auxiliary keymaps defined by `evil-define-key` are implemented using special key bindings in the keymaps. And these maps are accessed using `lookup-key`. Therefore, if a parent keymap has an auxiliary keymap, then searching for the auxiliary keymap in a child keymaps (using something like `(lookup-key child-map auxiliary-keymap-special-key)`) yields the binding in the parent keymap. That said, it is certainly possible to fix the issue, but this needs a careful handling of parent keymaps in the appropriate places. You could try the following work around: Define at least one auxiliary binding in the child keymap *before* defining one in the parent keymap. This ensures that the child keymap has its own auxiliary binding. (In other words: just swap the last two lines in your example). Frank
Frank Fischer <[email protected]> writes:
Am 08.09.2014 um 21:56 schrieb Albert Krewinkel:
Hi,
I just encountered some unexpected behavior when defining key bindings for maps which have parent maps: When I define a key for some state for the parent map as well as the child map, the child map overrides the bindings for the parent map.
You could try the following work around:
Define at least one auxiliary binding in the child keymap *before* defining one in the parent keymap. This ensures that the child keymap has its own auxiliary binding. (In other words: just swap the last two lines in your example).
That workaround is great, its works perfectly. Thanks for the help and the nice explanation (and for your work on evil)! Cheers, Albert -- Albert Krewinkel GPG: 8eed e3e2 e8c5 6f18 81fe e836 388d c0b2 1f63 1124
participants (2)
-
Albert Krewinkel -
Frank Fischer