Emacs 键绑定回退 [英] Emacs key binding fallback

查看:18
本文介绍了Emacs 键绑定回退的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个次要模式.如果该模式处于活动状态并且用户点击 DEL,我想做一些动作,但前提是某些条件成立.如果条件成立,动作被执行 我什么都不想做在那之后.但如果条件失败,我不想做任何事情并让默认的 DEL 操作执行.

I have a minor mode. If that mode is active and the user hits DEL, I want to do some action, but only if some condition holds. If the condition holds and the action is executed I want to do nothing more after that. But if the condition fails, I don't want to do anything and let the default DEL action execute.

不知道如何解决这个问题.但我想我可以通过两种方式做到这一点:

Not sure how I could solve this. But I guess I could do it in two ways:

1)我可以将 DEL 键重新绑定到次要模式下的函数,然后检查条件是否成立.但是我怎么知道DEL 的默认命令是?

1) I could rebind the DEL key to a function in the minor mode and then check if the conditions holds ot not. But then how do I know what the default command to DEL is?

2)我可以像这样添加一个预命令钩子.执行命令,然后打破链条.但我如何打破链条?

2) I could add a pre command hook like this. Execute the command and then break the chain. But how do I break the chain?

(add-hook 'pre-command-hook
          (lambda()
            (when (equal last-input-event 'backspace)
              ;; Do something and then stop (do not execute the
              ;; command that backspace is bound to)
              )))

你会用什么方式解决它?谢谢!

In what way would you solve it? Thanks!

推荐答案

这样做的方法是暂时禁用您的次要模式,然后查找键绑定.

The way to do this is to temporarily disable your minor mode, then look up the key binding.

假设您已将 'do-thingy 绑定到 DEL.然后这将起到作用(假设您要触发的条件是 (equal last-input-event 'backspace):

Pretend that you've bound 'do-thingy to DEL. Then this would do the trick (assuming the condition you want to trigger off is (equal last-input-event 'backspace):

(defun do-thingy ()
  "Do something, unless last event was backspace."
  (interactive)
  (if (equal last-input-event 'backspace)
      (let* ((my-minor-mode nil)
             (original-func (key-binding (kbd "DEL"))))
        ;; original-func is whatever DEL would be if
        ;; my-minor-mode were disabled
        (call-interactively original-func))
    (message "Here's my minor mode behavior!")))

注意:此行为假设您已设置键绑定 次要模式的标准方式.具体来说,您应该将您的键盘映射添加到变量 minor-mode-map-alist 通过添加元素 (my-minor-mode . my-minor-mode-keymap).这就是上述 let 语句的工作方式,它会在暂时禁用模式的情况下查找您想要的绑定.

Note: This behavior assumes you have set up your key bindings the standard way a minor-mode would. Specifically, you should add your keymap to the variable minor-mode-map-alist by adding an element (my-minor-mode . my-minor-mode-keymap). That's how the above let statement works, it looks up the binding you want with your mode temporarily disabled.

如果您使用 define-minor-mode 定义你的次要模式,键盘映射会自动设置正确的方式".

If you use define-minor-mode to define your minor mode, the keymap gets set up the "right way" automatically.

这篇关于Emacs 键绑定回退的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆