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

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

问题描述

我有一个小模式。如果该模式处于活动状态,并且用户命中DEL,则I
想要执行某些操作,但只有在某些条件成立时才会执行。如果
条件成立,并且执行该操作,那么我再也不要再执行
了。但是如果条件失败,我不想做任何
,并且执行默认的DEL操作。



不知道我该如何解决这个问题。但我想我可以通过两种方法来实现:



1)
我可以将DEL键重新绑定到小模式然后
检查条件是否保持不。但是我怎么知道那个
的默认命令是什么?



2)
我可以添加一个pre命令钩这样。执行命令,然后
中断链。但是如何打破链条?

 (add-hook'pre-command-hook 
(lambda
(when(equal last-input-event'backspace)
;;执行某些操作,然后停止(不要执行backspace绑定的
;;命令)
) ))

你以什么方式解决它?谢谢!

解决方案

这样做的方法是临时禁用您的次模式,然后查找密钥绑定。 >

假装你绑定了'do-thingy DEL 。那么这将会做到这一点(假设你想要触发的条件是(等于last-input-event'backspace)

 (defun do-thingy()
做某事,除非最后一个事件被退回。
(interactive)
(if等于最后输入事件'backspace)
(let *((my-minor-mode nil))
(original-func(key-binding(kbdDEL)))
如果
;;我的小型模式被禁用,则原始功能将是任何DEL
(调用交互式的原始功能))
(消息这是我的小模式行为! )))

注意:此行为假定您设置了您的键绑定 标准方式是次模式将的。具体来说,您应该将键盘映射添加到变量 minor-mode-map-alist 通过添加元素(my-minor-mode。我的小调模式-键映射)。以上 let 语句的工作原理如何,它会暂时禁用您的模式查找所需的绑定。



如果您使用 define-minor-mode 来定义您的次模式,键盘映射将自动设置为正确的方式。


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

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!")))

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.

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

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

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