I would like to have a function
call-keymap which takes one argument
km (a keymap) and such that the result of
(call-keymap km) is the same as binding
km to a key and then pressing that key.
It should look something like this
(defun call-keymap (km) (while (keymapp km) (setq km (lookup-key km (read-key-sequence "")))) (call-interactively km))
except that this doesn't work (specifically the last line is wrong, since a non-keymap entry of a keymap needn't be a function).
Intuitively, it would seem that the main command loop in emacs should be something like
(while t (call-keymap (current-global-map))
(or rather, it should do something with
current-active-maps instead of
I was surprised to not be able to find a built-in function with the desired functionality of
call-keymap, since this seems to me to be such a basic function.
Could someone please point me to such a built-in, or indicate how to fix the above definition?
Stefan very rightly asked that I explain what my use-case is for such a function.
evil-mode and have
SPC as a prefix-key for various useful commands when in
insert-state, I would like some other binding (say,
M-SPC) to produce the same behaviour as
SPC in normal mode (I think emacs-DOOM has such a feature). My idea was then to bind
M-SPC to a function which fetches the current
normal-state binding of
SPC (which is a keymap) and calls it.
Presumably, there are other good solutions to this, so that my original question may be moot, though I would be still be interested to know the answer. For example, here is one quite-hacky-but-maybe-not-entirely-unsatisfactory solution to my problem:
(defun set-alt-space () (evil-local-set-key 'insert (kbd "M-SPC") (key-binding (kbd "SPC")))) (add-hook 'evil-normal-state-exit-hook 'set-alt-space)