diff options
| author | Wilfred Hughes <me@wilfred.me.uk> | 2018-01-01 11:20:11 +0000 |
|---|---|---|
| committer | Wilfred Hughes <me@wilfred.me.uk> | 2018-01-01 11:20:11 +0000 |
| commit | 216ffe9b1cf81f26119f15b9ee04f2fec53592fd (patch) | |
| tree | 1a851c15709500e527239321fd0843c3fa90a4d8 | |
| parent | 06fe2ad9c777a6e8c92776d3d6e6e43a513dc1d2 (diff) | |
Better handling of keybindings
Look at all keymaps in global variables, so we don't miss keymaps like
`minibuffer-local-map`. Ignore inherited keybindings. Ignore menu bar
items (I don't find them useful at least).
Refactor global keybinding logic, as the global-map variable means we
don't need to treat global keybindings as a special case.
| -rw-r--r-- | CHANGELOG.md | 8 | ||||
| -rw-r--r-- | helpful.el | 44 |
2 files changed, 33 insertions, 19 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 86d0ca1..4c80085 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,14 @@ values. Quoted keywords in docstrings are now highlighted. +Smarter handling of keybindings: + +* Consider all keymaps, not just `foo-mode-map`. +* Show keybindings where they are defined, don't show them in keymaps + that inherit them. +* Don't show menu bar items, as they clutter the display (please file + a bug if you miss this). + # v0.5 Allow function tracing to be enabled/disabled from Helpful buffers. @@ -850,35 +850,41 @@ buffer." (let (keymaps) (mapatoms (lambda (sym) - (when (and - (boundp sym) - (keymapp (symbol-value sym)) - (s-ends-with-p "-mode-map" (symbol-name sym))) + (when (and (boundp sym) (keymapp (symbol-value sym))) (push sym keymaps)))) keymaps)) (defun helpful--keymaps-containing (command-sym) + "Return a list of pairs listing keymap symbols that contain COMMAND-SYM, +along with the keybindings in each keymap. + +We ignore keybindings that are menu items, and ignore keybindings +from parent keymaps." (let (matching-keymaps) - ;; Look for this command in all major and minor mode maps. - (dolist (keymap (helpful--all-keymap-syms)) - (let ((keycodes (where-is-internal command-sym - (list (symbol-value keymap))))) + ;; Look for this command in all keymaps. + (dolist (keymap-sym (helpful--all-keymap-syms)) + (let* ((keymap (symbol-value keymap-sym)) + (keycodes + (where-is-internal + command-sym (list keymap) nil t)) + ;; Look up this command in the parent keymap. + (parent-keymap (keymap-parent keymap)) + (parent-keycodes + (when parent-keymap + (where-is-internal + command-sym (list parent-keymap) nil t)))) + (setq keycodes + ;; Ignore keybindings that we've just inherited from the + ;; parent. + (-difference keycodes parent-keycodes)) (when keycodes - (push (cons keymap + (push (cons keymap-sym (-map #'key-description keycodes)) matching-keymaps)))) - ;; Look for this command in the global map. - (let ((keycodes (where-is-internal command-sym - (list (current-global-map))))) - (when keycodes - (push (cons 'global - (-map #'key-description keycodes)) - matching-keymaps))) matching-keymaps)) (defun helpful--format-keys (command-sym) - "Describe all the keys that call COMMAND-SYM. -Ensures global keybindings are shown first." + "Describe all the keys that call COMMAND-SYM." (let (mode-lines global-lines) (--each (helpful--keymaps-containing command-sym) @@ -888,7 +894,7 @@ Ensures global keybindings are shown first." (format "%s %s" (propertize (symbol-name map) 'face 'font-lock-variable-name-face) key) - (if (eq map 'global) global-lines mode-lines))))) + (if (eq map 'global-map) global-lines mode-lines))))) (setq global-lines (-sort #'string< global-lines)) (setq mode-lines (-sort #'string< mode-lines)) (-let [lines (-concat global-lines mode-lines)] |
