branch: master commit ef384e781e6107850c7fadc78cb0675d7fe72e69 Author: Justin Burkett <jus...@burkett.cc> Commit: Justin Burkett <jus...@burkett.cc>
Fix and improve define-key based replacements Check for equality of definition as well as the key sequence. Unless it's a prefix binding, in which case only check the key sequence. Make sure we apply kbd when looking up pseudo bindings. Move the logic into which-key--get-pseudo-binding. --- which-key.el | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/which-key.el b/which-key.el index d28f4a2..cb6da90 100644 --- a/which-key.el +++ b/which-key.el @@ -916,7 +916,7 @@ meant to be used as :before advice for `define-key'." (when (and (consp def) (stringp (car def)) (symbolp (cdr def))) - (define-key keymap (which-key--pseudo-key key) (car def))))) + (define-key keymap (which-key--pseudo-key key) `(which-key ,def))))) (when which-key-enable-extended-define-key (advice-add #'define-key :before #'which-key--process-define-key-args)) @@ -1358,14 +1358,28 @@ local bindings coming first. Within these categories order using (throw 'res res))))))) (nreverse res))) +(defun which-key--get-pseudo-binding (key-binding) + (let* ((pseudo-binding + (key-binding (which-key--pseudo-key (kbd (car key-binding)) t))) + (pseudo-binding (when pseudo-binding (cadr pseudo-binding))) + (pseudo-desc (when pseudo-binding (car pseudo-binding))) + (pseudo-def (when pseudo-binding (cdr pseudo-binding))) + (real-def (key-binding (kbd (car key-binding)))) + ;; treat keymaps as if they're nil bindings. This creates the + ;; possibility that we rename the wrong binding but this seems + ;; unlikely. + (real-def (unless (keymapp real-def) real-def))) + (when (and pseudo-binding + (eq pseudo-def real-def)) + (cons (car key-binding) pseudo-desc)))) + (defun which-key--maybe-replace (key-binding) "Use `which-key--replacement-alist' to maybe replace KEY-BINDING. KEY-BINDING is a cons cell of the form \(KEY . BINDING\) each of which are strings. KEY is of the form produced by `key-binding'." - (let ((menu-item-repl - (key-binding (which-key--pseudo-key (car key-binding) t)))) - (if menu-item-repl - (cons (car key-binding) menu-item-repl) + (let* ((pseudo-binding (which-key--get-pseudo-binding key-binding))) + (if pseudo-binding + pseudo-binding (let* ((mode-res (which-key--get-replacements key-binding t)) (all-repls (or mode-res (which-key--get-replacements key-binding))))