模式特定的按键绑定

发布于 2024-08-29 11:11:03 字数 556 浏览 11 评论 0原文

我有一个小模式,同时也有一个全局模式。该模式有一些键绑定,我希望用户能够指定哪些绑定适用于每种模式。

(my-minor-mode-bindings-for-mode 'some-mode '(key1 key2 ...))
(my-minor-mode-bindings-for-mode 'some-other-mode '(key3 key4 ...))

所以我需要某种模式/缓冲区本地键映射。本地缓冲区是一个 有点问题,因为用户可以更改主要模式。

我尝试过一些解决方案,但都没有效果。

  1. 始终绑定所有可能的键,当用户键入该键时,检查该键是否应在该模式下处于活动状态。如果为 true 则执行操作,否则回退。
  2. 与前一种情况类似,只是没有绑定任何键。相反,我使用预命令挂钩并检查按下的键是否应该执行任何操作。
  3. 对于每个缓冲区更新(无论这意味着什么),运行一个函数,首先清除键映射,然后使用该特定模式的绑定更新它。

我尝试过这些方法,但发现它们都存在问题。您知道有什么好的方法可以解决这个问题吗?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一口甜 2024-09-05 11:11:03

我猜你可以添加键绑定是一个钩子:

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

匿名函数当然可能更复杂,你可以做任何你想做的检查。当然,如果您需要交互地更改绑定,您可以简单地使用一些交互功能......

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

聽兲甴掵 2024-09-05 11:11:03

some-mode-map 变量缓冲区设为本地,当启用 some-mode 时,它将检查要安装哪组按键绑定。因为 some-mode-map 是缓冲区本地的,所以键绑定应该是该缓冲区本地的(并且不会影响其他缓冲区)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文