diff options
| author | Wilfred Hughes <me@wilfred.me.uk> | 2018-01-01 11:37:48 +0000 |
|---|---|---|
| committer | Wilfred Hughes <me@wilfred.me.uk> | 2018-01-01 11:37:48 +0000 |
| commit | 1be4595e2c2fee4c32ee083240a2200ab70ef71b (patch) | |
| tree | da3bcccf128a9884e81306515378f16499fc5c63 | |
| parent | 216ffe9b1cf81f26119f15b9ee04f2fec53592fd (diff) | |
Handle keymap references in docstrings
Fixes #83.
| -rw-r--r-- | CHANGELOG.md | 1 | ||||
| -rw-r--r-- | helpful.el | 85 | ||||
| -rw-r--r-- | test/unit-test.el | 10 |
3 files changed, 59 insertions, 37 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c80085..2cfbba5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ Smarter handling of keybindings: that inherit them. * Don't show menu bar items, as they clutter the display (please file a bug if you miss this). +* Correctly handle `\<foo-map>` in docstrings. # v0.5 @@ -606,43 +606,54 @@ Emacs uses \\= to escape \\[ references, so replace that unescaping too." ;; Based on `substitute-command-keys', but converts command ;; references to buttons. - (with-temp-buffer - (insert docstring) - (goto-char (point-min)) - (while (not (eobp)) - (cond - ((looking-at - ;; Text of the form \=X - (rx "\\=")) - ;; Remove the escaping, then step over the escaped char. - ;; Step over the escaped character. - (delete-region (point) (+ (point) 2)) - (forward-char 1)) - ((looking-at - ;; Text of the form \\[foo] - (rx "\\[" (group (+ (not (in "]")))) "]")) - (let* ((symbol-with-parens (match-string 0)) - (symbol-name (match-string 1))) - ;; Remove the original string. - (delete-region (point) - (+ (point) (length symbol-with-parens))) - ;; Add a button. - (let* ((symbol (intern symbol-name)) - (key (where-is-internal symbol nil t)) - (key-description - (if key - (key-description key) - (format "M-x %s" symbol-name)))) - (insert - (helpful--button - key-description - 'helpful-describe-exactly-button - 'symbol symbol - 'callable-p t))))) - ;; Don't modify other characters. - (t - (forward-char 1)))) - (buffer-string))) + (let ((keymap nil)) + (with-temp-buffer + (insert docstring) + (goto-char (point-min)) + (while (not (eobp)) + (cond + ((looking-at + ;; Text of the form \=X + (rx "\\=")) + ;; Remove the escaping, then step over the escaped char. + ;; Step over the escaped character. + (delete-region (point) (+ (point) 2)) + (forward-char 1)) + ((looking-at + ;; Text of the form \\<foo-keymap> + (rx "\\<" (group (+ (not (in ">")))) ">")) + (let* ((symbol-with-parens (match-string 0)) + (symbol-name (match-string 1))) + ;; Remove the original string. + (delete-region (point) + (+ (point) (length symbol-with-parens))) + ;; Set the new keymap. + (setq keymap (symbol-value (intern symbol-name))))) + ((looking-at + ;; Text of the form \\[foo-command] + (rx "\\[" (group (+ (not (in "]")))) "]")) + (let* ((symbol-with-parens (match-string 0)) + (symbol-name (match-string 1))) + ;; Remove the original string. + (delete-region (point) + (+ (point) (length symbol-with-parens))) + ;; Add a button. + (let* ((symbol (intern symbol-name)) + (key (where-is-internal symbol keymap t)) + (key-description + (if key + (key-description key) + (format "M-x %s" symbol-name)))) + (insert + (helpful--button + key-description + 'helpful-describe-exactly-button + 'symbol symbol + 'callable-p t))))) + ;; Don't modify other characters. + (t + (forward-char 1)))) + (buffer-string)))) ;; TODO: fix upstream Emacs bug that means `-map' is not highlighted ;; in the docstring for `--map'. diff --git a/test/unit-test.el b/test/unit-test.el index 51029da..db75500 100644 --- a/test/unit-test.el +++ b/test/unit-test.el @@ -34,6 +34,16 @@ (should (not (s-contains-p "\\=" formatted-docstring))))) +(ert-deftest helpful--docstring-keymap () + "Handle keymap references in docstrings." + (let* ((formatted-docstring + (helpful--format-docstring + "\\<minibuffer-local-map>\\[next-history-element]"))) + ;; This test will fail in a local Emacs instance that has modified + ;; minibuffer keybindings. + (should + (string-equal formatted-docstring "M-n")))) + (ert-deftest helpful--docstring-advice () "Get the docstring on advised functions." (should |
