上下文:我想创建一个次要模式,在该模式下,f
快速按下两次将导致此时按的(
操作无效。这并不总是意味着仅插入(
。例如,在启用了paredit模式或autopair模式的缓冲区中,按(
通常会插入()
。在paredit模式的缓冲区中,有时会导致包装所选文本:例如,如果我选择a b
并按(
,则应导致将所选内容替换为(a b)
。
为了检测是否f
被按下两次,我只需要采用http://www.emacswiki.org/emacs/electric-dot-and-dash.el中短代码中的逻辑
因此,唯一缺少的部分是一个Lisp代码片段,该片段告诉Emacs“(
现在按下触发器!”
我想到的第一件事是代码段应该
(
但是如果自动配对程序包(自动配对或paredit或其他类似程序包)绑定(
到具有查找用于调用该命令的键的逻辑的命令,或者该程序包仅依赖于自我插入后,该操作就会失败-hook或post-command-hook而不是binding (
。
更新
我查阅了Key Chord文档,结果发现我想用这个问题的答案做一个更简单的解决方案:
(require 'key-chord)
(key-chord-mode 1)
(defvar my-easy-open-paren-mode-map
(let ((map (make-sparse-keymap)))
(key-chord-define map ",." (kbd "("))
map))
(define-minor-mode my-easy-open-paren-mode
"In this mode, pressing . and , together is another way of pressing the open paren.")
(defvar my-easy-semicolon-mode-map
(let ((map (make-sparse-keymap)))
(key-chord-define map ";;" (kbd "C-e ;"))
map))
(define-minor-mode my-easy-semicolon-mode
"In this mode, pressing semicolon twice fast is another way of pressing C-e and semicolon.")
(add-hook 'prog-mode-hook 'my-easy-open-paren-mode)
(add-hook 'c-mode-common-hook 'my-easy-semicolon-mode)
但是,在其他情况下,触发按键可能仍然有用。
您可能会喜欢Key Chord库,该库将功能绑定到两次按键。(请注意,我不建议您使用f
英语书写,而是使用YMMV。)
post-self-insert-hook
如果绑定为,则仍会运行self-insert-command
。post-command-hook
可以在任何情况下运行,但是如果您担心它看到错误的功能和/或输入事件,则可以操作这些...
查找绑定后,您的函数可以设置this-command
为您将要使用的函数call-interactively
,以及last-command-event
所需的键。例如:
(defun my-fake-paren ()
(interactive)
(let ((command (key-binding "(")))
(setq last-command-event ?\()
(setq this-command command)
(call-interactively command)))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句