Elisp:有条件地改变键盘绑定 [英] Elisp: Conditionally change keybinding
问题描述
我正在尝试编写一个自定义选项卡完成实现,它会根据所在的位置来尝试一堆不同的完成。但是,如果没有满足完成的条件,我希望选项卡能够执行当前模式本来打算做的事情。
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.
任何想法如何做?
谢谢!/ Erik
推荐答案
您可以使用键绑定
(或其更具体的变体全局键绑定
, min或模式键绑定
和 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)
(message "my-complete")
(let ((my-complete-mode nil))
(call-interactively (key-binding (kbd "TAB")))))
这篇关于Elisp:有条件地改变键盘绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!