From f4301765a9b4a44ce732e3118036acb48c51f94a Mon Sep 17 00:00:00 2001 From: Wilfred Hughes Date: Sun, 31 Dec 2017 01:09:18 +0000 Subject: Add imenu support Fixes #24. This is much inspired by the sample code given by @xenodium in issue #36. Thanks! :) --- CHANGELOG.md | 2 ++ helpful.el | 23 ++++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4537037..86d0ca1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # v0.6 +Added imenu support! You can now navigate between headings with imenu. + Helpful now correctly handles \= escapes in docstrings. Added disassemble buttons to byte-code functions in symbol properties. diff --git a/helpful.el b/helpful.el index c3f46a9..4eed109 100644 --- a/helpful.el +++ b/helpful.el @@ -1389,9 +1389,30 @@ See also `helpful-callable' and `helpful-variable'." (helpful-symbol symbol) (user-error "There is no symbol at point."))) +(defun helpful--imenu-index () + "Return a list of headings in the current buffer, suitable for +imenu." + (let (headings) + (goto-char (point-min)) + (while (not (eobp)) + (when (eq (get-text-property (point) 'face) + 'helpful-heading) + (push + (cons + (buffer-substring-no-properties + (line-beginning-position) (line-end-position)) + (line-beginning-position)) + headings)) + (forward-line)) + (nreverse headings))) + (define-derived-mode helpful-mode special-mode "Helpful" "Major mode for *Helpful* buffers." - (add-hook 'xref-backend-functions #'elisp--xref-backend nil t)) + (add-hook 'xref-backend-functions #'elisp--xref-backend nil t) + + (setq imenu-create-index-function #'helpful--imenu-index) + ;; Prevent imenu converting "Source Code" to "Source.Code". + (setq-local imenu-space-replacement " ")) (defun helpful-visit-reference () "Go to the reference at point." -- cgit v1.0