branch: externals/which-key commit e48e190a75a0c176e1deac218b891e77792d6921 Author: Rudi Grinberg <m...@rgrinberg.com> Commit: GitHub <nore...@github.com>
Fix #257 (#258) * Add test for #257 Signed-off-by: Rudi Grinberg <m...@rgrinberg.com> * Fix #257 Explicitly distinguish between replacing with `nil` and not replacing at all. I'm also simplifying the code by making all the branches more explicit. This is a little longer, but makes all the clauses obvious. Signed-off-by: Rudi Grinberg <m...@rgrinberg.com> --- which-key-tests.el | 7 +++++++ which-key.el | 37 ++++++++++++++++++++++--------------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/which-key-tests.el b/which-key-tests.el index ae015be..f9fac65 100644 --- a/which-key-tests.el +++ b/which-key-tests.el @@ -148,6 +148,13 @@ ("e e e" . "eee") ("f" . "{ - C-f")))))) +(ert-deftest which-key-test--nil-replacement () + (let ((which-key-replacement-alist + '(((nil . "winum-select-window-[1-9]") . t)))) + (should (equal + (which-key--maybe-replace '("C-c C-c" . "winum-select-window-1")) + '())))) + (ert-deftest which-key-test--key-sorting () (let ((keys '(("a" . "z") ("A" . "Z") diff --git a/which-key.el b/which-key.el index 143540f..766c8f1 100644 --- a/which-key.el +++ b/which-key.el @@ -1497,33 +1497,40 @@ local bindings coming first. Within these categories order using (defun which-key--replace-in-repl-list-once (key-binding repls) (cl-dolist (repl repls) (when (which-key--match-replacement key-binding repl) - (cl-return (which-key--replace-in-binding key-binding repl))))) + (cl-return `(replaced . ,(which-key--replace-in-binding key-binding repl)))))) (defun which-key--replace-in-repl-list-many (key-binding repls) - (dolist (repl repls key-binding) - (when (which-key--match-replacement key-binding repl) - (setq key-binding (which-key--replace-in-binding key-binding repl))))) + (let (found) + (dolist (repl repls) + (when (which-key--match-replacement key-binding repl) + (setq found 't) + (setq key-binding (which-key--replace-in-binding key-binding repl)))) + (when found `(replaced . ,key-binding)))) (defun which-key--maybe-replace (key-binding &optional prefix) "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* ((pseudo-binding (which-key--get-pseudo-binding key-binding prefix)) - replaced-key-binding) + (let* ((pseudo-binding (which-key--get-pseudo-binding key-binding prefix))) (if pseudo-binding pseudo-binding (let* ((replacer (if which-key-allow-multiple-replacements #'which-key--replace-in-repl-list-many #'which-key--replace-in-repl-list-once))) - (setq replaced-key-binding - (apply replacer - (list key-binding - (cdr-safe (assq major-mode which-key-replacement-alist))))) - ;; terminate early if we're only looking for one replacement and we found it - (if (and replaced-key-binding (not which-key-allow-multiple-replacements)) - replaced-key-binding - (setq key-binding (or replaced-key-binding key-binding)) - (or (apply replacer (list key-binding which-key-replacement-alist)) key-binding)))))) + (pcase + (apply replacer + (list key-binding + (cdr-safe (assq major-mode which-key-replacement-alist)))) + (`(replaced . ,repl) + (if which-key-allow-multiple-replacements + (pcase (apply replacer (list repl which-key-replacement-alist)) + (`(replaced . ,repl) repl) + ('() repl)) + repl)) + ('() + (pcase (apply replacer (list key-binding which-key-replacement-alist)) + (`(replaced . ,repl) repl) + ('() key-binding)))))))) (defsubst which-key--current-key-list (&optional key-str) (append (listify-key-sequence (which-key--current-prefix))