开发者

Mode specific key bindings

I have a minor mode that also comes with a glo开发者_如何转开发bal 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!


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...


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜