diff options
| author | Wilfred Hughes <me@wilfred.me.uk> | 2018-07-28 14:30:46 +0100 |
|---|---|---|
| committer | Wilfred Hughes <me@wilfred.me.uk> | 2018-07-28 14:30:46 +0100 |
| commit | a7260fb844924481abf01ac5796354ced084c1f8 (patch) | |
| tree | 9f3a2525ee30935529491fa4673b2ac8cea22130 | |
| parent | 0eac5f08ea471010902e84501b2b5d0c62762195 (diff) | |
Show keybindings for command aliases too
Fixes #143
| -rw-r--r-- | CHANGELOG.md | 4 | ||||
| -rw-r--r-- | helpful.el | 28 | ||||
| -rw-r--r-- | test/unit-test.el | 29 |
3 files changed, 59 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index f06934a..a82d54a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -# v0.13 +# v0.13 (unreleased) Buffer-local variables are now highlighted, and it's possible to see all the different values of a buffer-local variable. @@ -8,6 +8,8 @@ Variable values are now shown before docstrings. Fixed an issue where special forms were incorrectly described as functions. +Helpful now shows keybindings for aliases of the current command too. + # v0.12 Added a 'pretty view' for string values, keymap values, and hooks. @@ -1315,11 +1315,37 @@ same bindings as `global-map'." matching-keymaps)) +(defun helpful--merge-alists (l1 l2) + "Given two alists mapping symbols to lists, return a single +alist with the lists concatenated." + (let* ((l1-keys (-map #'-first-item l1)) + (l2-keys (-map #'-first-item l2)) + (l2-extra-keys (-difference l2-keys l1-keys)) + (l2-extra-values + (--map (assoc it l2) l2-extra-keys)) + (l1-with-values + (-map (-lambda ((key . values)) + (cons key (append values + (cdr (assoc key l2))))) + l1))) + (append l1-with-values l2-extra-values))) + +(defun helpful--keymaps-containing-aliases (command-sym) + "Return a list of pairs mapping keymap symbols to the +keybindings for COMMAND-SYM in each keymap. + +Includes keybindings for aliases, unlike +`helpful--keymaps-containing'." + (let* ((aliases (helpful--aliases 'helpful--dummy-command t)) + (syms (cons command-sym aliases)) + (syms-keymaps (-map #'helpful--keymaps-containing syms))) + (-reduce #'helpful--merge-alists syms-keymaps))) + (defun helpful--format-keys (command-sym) "Describe all the keys that call COMMAND-SYM." (let (mode-lines global-lines) - (--each (helpful--keymaps-containing command-sym) + (--each (helpful--keymaps-containing-aliases command-sym) (-let [(map . keys) it] (dolist (key keys) (push diff --git a/test/unit-test.el b/test/unit-test.el index 85f275b..74ff231 100644 --- a/test/unit-test.el +++ b/test/unit-test.el @@ -553,6 +553,35 @@ associated a lambda with a keybinding." '(("minor-mode-map-alist (ido-mode)" "<open>" "C-x C-f")))) (ido-mode 0)) +(defalias 'helpful--dummy-command-alias #'helpful--dummy-command) + +(ert-deftest helpful--keymaps-containing-aliases () + "Ensure that we find keymaps that we've bound command aliases +in." + ;; Create keybindings that are very unlikely to clobber actually + ;; defined keybindings in the current emacs instance. + (global-set-key (kbd "C-c M-S-c") #'helpful--dummy-command) + (global-set-key (kbd "C-c M-S-d") #'helpful--dummy-command-alias) + + (unwind-protect + (let* ((keymaps (helpful--keymaps-containing-aliases #'helpful--dummy-command)) + (global-keybindings (cdr (assoc "global-map" keymaps)))) + (should + (equal global-keybindings (list "C-c M-S-c" "C-c M-S-d")))) + + ;; Undo keybindings. + (global-set-key (kbd "C-c M-S-c") nil) + (global-set-key (kbd "C-c M-S-d") nil))) + +(ert-deftest helpful--merge-alists () + (should + (equal + (helpful--merge-alists '((a . (1 2 3)) (b . (4))) + '((a . (10)) (c . (11)))) + '((a . (1 2 3 10)) + (b . (4)) + (c . (11)))))) + (ert-deftest helpful--source () (-let* (((buf pos opened) (helpful--definition #'helpful--source t)) (source (helpful--source #'helpful--source t buf pos))) |
