Skip to content
This repository has been archived by the owner on Jun 25, 2024. It is now read-only.

Commit

Permalink
Fix handling of key ranges ("a .. d")
Browse files Browse the repository at this point in the history
When the last key in the key sequence is a range, extract the whole range
instead of just the final key.

Fixes #161
  • Loading branch information
justbur committed Feb 9, 2017
1 parent ea6f1dc commit 0d56e43
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
10 changes: 9 additions & 1 deletion which-key-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
'("SPC t 2" . "[ ] test mode")))))

(ert-deftest which-key-test--maybe-replace-multiple ()
"Test `which-key-allow-multiple-replacements'. See #156"
"Test `which-key-allow-multiple-replacements'. See #156."
(let ((which-key-replacement-alist
'(((nil . "helm") . (nil . "HLM"))
((nil . "projectile") . (nil . "PRJTL"))))
Expand All @@ -114,5 +114,13 @@
(which-key--maybe-replace '("C-c C-c" . "helm-projectile-x"))
'("C-c C-c" . "HLM-PRJTL-x")))))

(ert-deftest which-key-test--key-extraction ()
"Test `which-key--extract-key'. See #161."
(should (equal (which-key--extract-key "SPC a") "a"))
(should (equal (which-key--extract-key "C-x a") "a"))
(should (equal (which-key--extract-key "<left> b a") "a"))
(should (equal (which-key--extract-key "<left> a .. c") "a .. c"))
(should (equal (which-key--extract-key "M-a a .. c") "a .. c")))

(provide 'which-key-tests)
;;; which-key-tests.el ends here
12 changes: 10 additions & 2 deletions which-key.el
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,14 @@ ORIGINAL-DESCRIPTION is the description given by
str))))))
desc))

(defun which-key--extract-key (key-str)
"Pull the last key (or key range) out of KEY-STR."
(save-match-data
(let ((key-range-regexp "\\`.*\\([^ \t]+ \\.\\. [^ \t]+\\)\\'"))
(if (string-match key-range-regexp key-str)
(match-string 1 key-str)
(car (last (split-string key-str " ")))))))

(defun which-key--format-and-replace (unformatted)
"Take a list of (key . desc) cons cells in UNFORMATTED, add
faces and perform replacements according to the three replacement
Expand All @@ -1451,15 +1459,15 @@ alists. Returns a list (key separator description)."
(let* ((key (car key-binding))
(orig-desc (cdr key-binding))
(group (which-key--group-p orig-desc))
(keys (which-key--current-key-string key))
(keys (concat (which-key--current-key-string) " " key))
(local (eq (which-key--safe-lookup-key local-map (kbd keys))
(intern orig-desc)))
(hl-face (which-key--highlight-face orig-desc))
(key-binding (which-key--maybe-replace (cons keys orig-desc))))
(when (consp key-binding)
(push
(list (which-key--propertize-key
(car (last (split-string (car key-binding) " "))))
(which-key--extract-key (car key-binding)))
sep-w-face
(which-key--propertize-description
(cdr key-binding) group local hl-face orig-desc))
Expand Down

0 comments on commit 0d56e43

Please sign in to comment.