branch: externals/which-key commit 6290c9e21710c3ebbcdec795c916994682e07c94 Author: Justin Burkett <jus...@burkett.cc> Commit: Justin Burkett <jus...@burkett.cc>
Improve which-key-add-keymap-based-bindings Add a test --- which-key-tests.el | 7 +++++-- which-key.el | 11 +++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/which-key-tests.el b/which-key-tests.el index 40566e7..877f009 100644 --- a/which-key-tests.el +++ b/which-key-tests.el @@ -36,11 +36,14 @@ (define-key map "\C-b" prefix-map) (which-key-add-keymap-based-replacements map "C-a" '("mycomplete" . complete) - "C-b" "mymap") + "C-b" "mymap" + "C-c" "mymap2") + (define-key map "\C-ca" 'foo) (should (equal (which-key--get-keymap-bindings map) '(("C-a" . "mycomplete") - ("C-b" . "group:mymap")))))) + ("C-b" . "group:mymap") + ("C-c" . "group:mymap2")))))) (ert-deftest which-key-test--prefix-declaration () "Test `which-key-declare-prefixes' and diff --git a/which-key.el b/which-key.el index 2d81d2e..2bfbb39 100644 --- a/which-key.el +++ b/which-key.el @@ -922,8 +922,15 @@ actually bound to write-file before performing the replacement." (cond ((consp replacement) (define-key keymap (kbd key) replacement)) ((stringp replacement) - (define-key keymap (kbd key) (cons replacement - (lookup-key keymap (kbd key))))) + (let ((binding (lookup-key keymap (kbd key)))) + (if (or (null binding) + (numberp binding)) + ;; using a keymap in case someone intends to make this a + ;; prefix. If they want to bind something else, they will just + ;; end up overriding the prefix map + (define-key keymap (kbd key) + (cons replacement (make-sparse-keymap))) + (define-key keymap (kbd key) (cons replacement binding))))) (t (user-error "replacement is neither a cons cell or a string"))) (setq key (pop more)