views:

91

answers:

3

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.

  1. 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.
  2. 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.
  3. 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!

A: 

You may add the key bindings is a hook I guess:

(add-hook 'some-mode-hook
 (lambda ()
 (define-key some-mode-map (kbd "C-c w") 'something)
 ...
 )
)

The anonymous function may be more complex of course and you can do whatever checks you'd like to do. Of course if you need to change the bindings interactively you can simply use some interactive function...

Bozhidar Batsov
I guess that's an option. But I'd rather solve it in some other way to make it easier for the user to specify the mode specific settings.
rejeep
A: 

Make the some-mode-map variable buffer local, and when some-mode is enabled, it'll check to see which of the sets of key bindings to install. Because the some-mode-map is buffer local, the key bindings should be local to that buffer (and not affect the other buffers).

Trey Jackson
I actually tried that, but it didn't work. Not sure why though. I'll try again... I guess I should use (make-variable-buffer-local 'some-mode-map) to make it buffer local?
rejeep
Making the mode map buffer local does not work for me. It seems to be working, because the bindings are correct. If I inspect the mode map variable, the correct bindings are there. But if I do *C-h c* on one of those bindings, it is not bound to what it should be, but to the Emacs default binding.
rejeep
A: 

This has some good information that might help.

thermans