Skip to content

Commit

Permalink
Add footnotes to imenu
Browse files Browse the repository at this point in the history
Closes GH-235
  • Loading branch information
jrblevin committed Dec 22, 2017
1 parent cc45572 commit 24fa9fb
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
- Added read-only viewing modes `markdown-view-mode` and
`gfm-view-mode` with keymaps similar to `view-mode` and
`help-mode`. ([GH-296][])
- Optionally add footnote definitions to the end of the imenu
index using `markdown-add-footnotes-to-imenu`. ([GH-235][])

* Improvements:

Expand Down Expand Up @@ -99,6 +101,7 @@
[gh-224]: https://github.com/jrblevin/markdown-mode/issues/224
[gh-227]: https://github.com/jrblevin/markdown-mode/issues/227
[gh-229]: https://github.com/jrblevin/markdown-mode/pull/229
[gh-235]: https://github.com/jrblevin/markdown-mode/issues/235
[gh-238]: https://github.com/jrblevin/markdown-mode/issues/238
[gh-246]: https://github.com/jrblevin/markdown-mode/issues/246
[gh-247]: https://github.com/jrblevin/markdown-mode/issues/247
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,9 @@ provides an interface to all of the possible customizations:
flat list may allow for faster keyboard navigation via tab
completion.

* `markdown-add-footnotes-to-imenu` - Add footnote definitions to
the end of the imenu index (default: `t`).

* `comment-auto-fill-only-comments` - variable is made
buffer-local and set to `nil` by default. In programming
language modes, when this variable is non-nil, only comments
Expand Down
46 changes: 45 additions & 1 deletion markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,13 @@ completion."
:safe 'booleanp
:package-version '(markdown-mode . "2.2"))

(defcustom markdown-add-footnotes-to-imenu t
"Add footnotes to end of imenu heading index."
:group 'markdown
:type 'boolean
:safe 'booleanp
:package-version '(markdown-mode . "2.4"))

(defcustom markdown-make-gfm-checkboxes-buttons t
"When non-nil, make GFM checkboxes into buttons."
:group 'markdown
Expand Down Expand Up @@ -4721,6 +4728,33 @@ NIL is returned instead."
(forward-line)
(append result (list (point)))))))

(defun markdown-get-defined-footnotes ()
"Return a list of all defined footnotes.
Result is an alist of pairs (MARKER . LINE), where MARKER is the
footnote marker, a string, and LINE is the line number containing
the footnote definition.

For example, suppose the following footnotes are defined at positions
448 and 475:

\[^1]: First footnote here.
\[^marker]: Second footnote.

Then the returned list is: ((\"^1\" . 478) (\"^marker\" . 475))"
(save-excursion
(goto-char (point-min))
(let (footnotes)
(while (markdown-search-until-condition
(lambda () (and (not (markdown-code-block-at-point-p))
(not (markdown-inline-code-at-point-p))
(not (markdown-in-comment-p))))
markdown-regex-footnote-definition nil t)
(let ((marker (match-string-no-properties 1))
(pos (match-beginning 0)))
(unless (zerop (length marker))
(cl-pushnew (cons marker pos) footnotes :test #'equal))))
(reverse footnotes))))


;;; Element Removal ===========================================================

Expand Down Expand Up @@ -5556,6 +5590,7 @@ See `imenu-create-index-function' and `imenu--index-alist' for details."
(self-heading ".")
hashes pos level heading)
(save-excursion
;; Headings
(goto-char (point-min))
(while (re-search-forward markdown-regex-header (point-max) t)
(unless (markdown-code-block-at-point-p)
Expand Down Expand Up @@ -5595,13 +5630,19 @@ See `imenu-create-index-function' and `imenu--index-alist' for details."
(setcdr sibling-alist alist)
(setq cur-alist alist))
(setq cur-level level))))))
(cdr root))))
;; Footnotes
(let ((fn (markdown-get-defined-footnotes)))
(if (or (zerop (length fn))
(null markdown-add-footnotes-to-imenu))
(cdr root)
(nconc (cdr root) (list (cons "Footnotes" fn))))))))

(defun markdown-imenu-create-flat-index ()
"Create and return a flat imenu index alist for the current buffer.
See `imenu-create-index-function' and `imenu--index-alist' for details."
(let* ((empty-heading "-") index heading pos)
(save-excursion
;; Headings
(goto-char (point-min))
(while (re-search-forward markdown-regex-header (point-max) t)
(when (and (not (markdown-code-block-at-point-p))
Expand All @@ -5614,6 +5655,9 @@ See `imenu-create-index-function' and `imenu--index-alist' for details."
(or (> (length heading) 0)
(setq heading empty-heading))
(setq index (append index (list (cons heading pos))))))
;; Footnotes
(when markdown-add-footnotes-to-imenu
(nconc index (markdown-get-defined-footnotes)))
index)))


Expand Down
32 changes: 32 additions & 0 deletions tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -5690,6 +5690,38 @@ comments = false
(should (member "Header2" headers))
(should-not (member "comments = false" headers)))))

(ert-deftest test-markdown-imenu/include-footnotes ()
"Check that footnotes are added to the imenu.
https://github.com/jrblevin/markdown-mode/issues/235"
(markdown-test-string "# H1

[^fn1]: footnote 1

## H2

[^fn2]: footnote 2
"
(let* ((markdown-add-footnotes-to-imenu t)
(entries (mapcar #'car (markdown-imenu-create-flat-index))))
(should (member "^fn1" entries))
(should (member "^fn2" entries)))))

(ert-deftest test-markdown-imenu/no-footnotes ()
"Check that footnotes are not added to the imenu.
https://github.com/jrblevin/markdown-mode/issues/235"
(markdown-test-string "# H1

[^fn1]: footnote 1

## H2

[^fn2]: footnote 2
"
(let* ((markdown-add-footnotes-to-imenu nil)
(entries (mapcar #'car (markdown-imenu-create-flat-index))))
(should-not (member "^fn1" entries))
(should-not (member "^fn2" entries)))))

(ert-deftest test-markdown-command/function ()
"Test ‘markdown’ with ‘markdown-command’ being a function."
(markdown-test-string "foo"
Expand Down

0 comments on commit 24fa9fb

Please sign in to comment.