Mode specific key bindings
Posted
by rejeep
on Stack Overflow
See other posts from Stack Overflow
or by rejeep
Published on 2010-04-18T13:47:46Z
Indexed on
2010/04/18
13:53 UTC
Read the original article
Hit count: 430
Hey,
I have a minor mode that also comes with a global mode. The mode have some key bindings and I want the user to have the posibility to specify what bindings should work for each mode.
(my-minor-mode-bindings-for-mode 'some-mode '(key1 key2 ...))
(my-minor-mode-bindings-for-mode 'some-other-mode '(key3 key4 ...))
So I need some kind of mode/buffer-local key map. Buffer local is a bit problematic since the user can change the major mode.
I have tried some solutions of which neither works any good.
- Bind all possible keys always and when the user types the key, check if the key should be active in that mode. Execute action if true, otherwise fall back.
- Like the previous case only that no keys are bound. Instead I use a pre command hook and check if the key pressed should do anything.
- For each buffer update (whatever that means), run a function that first clears the key map and then updates it with the bindings for that particular mode.
I have tried these approaches and I found problems with all of them. Do you know of any good way to solve this?
Thanks!
© Stack Overflow or respective owner