Elisp:有条件地更改键绑定 [英] Elisp: Conditionally change keybinding
问题描述
我正在尝试编写一个自定义选项卡完成实现,它会根据点所在的位置尝试一系列不同的完成.但是,如果不满足任何完成条件,我希望 tab 执行当前模式最初打算执行的操作.
I'm trying to write a custom tab completion implementation which tries a bunch of different completions depending on where the point is. However, if none of the conditions for completions are met I would like tab to do what ever the current mode originally intended it to do.
像这样:
(defun my-custom-tab-completion ()
(interactive)
(cond
(some-condition
(do-something))
(some-other-condition
(do-something-else))
(t
(do-whatever-tab-is-supposed-to-do-in-the-current-mode))) ;; How do I do this?
目前我正在检查特定模式并为该模式做正确的事情,但我真的想要一个解决方案,它只做正确的事情,而不必为该特定模式明确添加条件.
Currently I'm checking for specific modes and doing the right thing for that mode, but I really would like a solution that just does the right thing without me having to explicitly add a condition for that specific mode.
对如何做到这一点有任何想法吗?
Any ideas of how to do this?
谢谢!/埃里克
推荐答案
您可以使用诸如 key-binding
之类的函数(或其更具体的变体 global-key-binding
>、minor-mode-key-binding
和 local-key-binding
) 来探测绑定的活动键映射.
You could use functions such as key-binding
(or its more specific variants global-key-binding
, minor-mode-key-binding
and local-key-binding
) to probe active keymaps for bindings.
例如:
(call-interactively (key-binding (kbd "TAB")))
;; in an emacs-lisp-mode buffer:
;; --> indent-for-tab-command
;;
;; in a c++-mode buffer with yas/minor-mode:
;; --> yas/expand
<小时>
如果您的命令绑定到 TAB,避免无限循环的一种方法可能是将您的绑定置于次要模式,并在查找 TAB 绑定:
One way to avoid infinite loops if your command is bound to TAB could be to put your binding in a minor mode, and temporarily disable its keymap while looking for the TAB binding:
(define-minor-mode my-complete-mode
"Smart completion"
:keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "TAB") 'my-complete)
map))
(defun my-complete ()
(interactive)
(if (my-condition)
(message "my-complete")
(let ((my-complete-mode nil))
(call-interactively (key-binding (kbd "TAB"))))))
这篇关于Elisp:有条件地更改键绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!