找出一个按键对应的所有绑定?

关于快捷键的优先级问题 继续讨论:

https://www.gnu.org/software/emacs/manual/html_node/elisp/Searching-Keymaps.html

或许是个不错的编程挑战。

2 个赞

想到一个简单的应用——「按键分发」,想象你有一个键绑定到了两个命令,比如说:

(global-set-key (kbd "C-c C-c") #'emacs-version)
(local-set-key (kbd "C-c C-c") #'about-emacs)

由于 Local Map 优先于 Global Map,你想执行 emacs-version 该怎么办?

(defun chunyang-dispatch-key (key)
  (interactive "kDispatch Key: ")
  (require 'seq)
  (let* ((commands (seq-filter #'commandp
                               (mapcar (lambda (map) (lookup-key map key))
                                       (current-active-maps))))
         (collection (mapcar #'symbol-name commands))
         (command (intern (completing-read
                           (concat (key-description key) " ")
                           collection nil t nil nil (cadr collection)))))
    (command-execute command)))

然后 M-x chunyang-dispatch-key C-c C-c 就会列出所有的命令,然后可以选择一个执行。

4 个赞

如果能按C-c C-c时,就直接列出所有绑定的命令就好了

which-key?