commit a2fe48573173341fdeed9991b7c62702bc1fee0e
parent 0b165188463cee994c99f4f1eff7bfc7cd902fb6
Author: Wilfred Hughes <me@wilfred.me.uk>
Date: Fri, 6 Jul 2018 14:07:08 +0100
Handle anonymous functions in keymaps
In future, we could explore showing the source code rather than just
saying it's a function. For now, just fix the crash.
Fixes #133.
Diffstat:
3 files changed, 23 insertions(+), 7 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
@@ -13,6 +13,8 @@ with a link to the relevant command.
Added a 'view callees' button which shows functions called inside the
bodies of source code, with links to view them inside helpful.
+Fixed a crash on keymaps where keys are bound to anonymous functions.
+
# v0.11
Further work on syntax highlighting performance for large code
diff --git a/helpful.el b/helpful.el
@@ -752,9 +752,11 @@ vector suitable for `key-description', and COMMAND is a smbol."
;; Prefix keys use a keymap in the function slot of a symbol.
(keymapp (symbol-function keymap)))
(helpful--keymap-keys (symbol-function keymap)))
- ;; Other symbols mean we've reached a leaf, so this is a command
- ;; we can call.
- ((symbolp keymap)
+ ;; Other symbols or compiled functions mean we've reached a leaf,
+ ;; so this is a command we can call.
+ ((or
+ (symbolp keymap)
+ (functionp keymap))
`(([] ,keymap)))
((stringp (car keymap))
(helpful--keymap-keys (cdr keymap)))
@@ -831,10 +833,12 @@ vector suitable for `key-description', and COMMAND is a smbol."
keys-and-commands))
(formatted-commands
(--map
- (helpful--button
- (symbol-name it)
- 'helpful-describe-button
- 'symbol it)
+ (if (symbolp it)
+ (helpful--button
+ (symbol-name it)
+ 'helpful-describe-button
+ 'symbol it)
+ "#<anonymous-function>")
commands))
;; Build lines for display.
(lines
diff --git a/test/unit-test.el b/test/unit-test.el
@@ -496,6 +496,16 @@ associated a lambda with a keybinding."
([3 94 61 62] smerge-diff-base-lower)
([3 94 61 60] smerge-diff-base-upper))))))
+(ert-deftest helpful--keymap-keys--anonymous-fns ()
+ (let* ((keymap (make-keymap)))
+ (define-key keymap (kbd "a")
+ (lambda () (message)))
+ (define-key keymap (kbd "a")
+ (byte-compile-sexp (lambda () (message))))
+
+ ;; Don't crash on anonymous functions in a keymap.
+ (helpful--keymap-keys keymap)))
+
(defun helpful--dummy-command ()
(interactive))