Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend footnote insertion with text notes #586

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
to automatically define a default link text before prompting the user.
- Option to inhibit the prompt for a tooltip text via
`markdown-disable-tooltip-prompt`.
- Allow insertion of text taged footnotes.

* Improvements:
- Correct indirect buffer's indentation in `markdown-edit-code-block` [GH-375][]
Expand All @@ -26,6 +27,7 @@
- Support to display local image with percent encoding file path
- Add ability to resize inline image display (`markdown-toggle-inline-images`) without Imagemagick installed in the computer (emulating Org Mode)
- Support including braces around the language specification in GFM code blocks
- Custom variable to force footnote recount before insertion

* Bug fixes:
- Fix remaining flyspell overlay in code block or comment issue [GH-311][]
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,14 @@ provides an interface to all of the possible customizations:

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

* `markdown-footnote-default-type` - Wither to insert a number or
text tag by default when creating a footnote with
`markdown-insert-footnote` (default: `'number`).

* `markdown-footnote-always-recalculate-number` - Wither to
recalculate the next footnote number when inserting a new number
tagged footnote (default: `nil`).

* `comment-auto-fill-only-comments` - variable is made
buffer-local and set to `nil` by default. In programming
Expand Down
33 changes: 27 additions & 6 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -2147,6 +2147,18 @@ Depending on your font, some reasonable choices are:
(concat "^ \\{0,3\\}\\[\\(\\^" markdown-footnote-chars "*?\\)\\]:\\(?:[ \t]+\\|$\\)")
"Regular expression matching a footnote definition, capturing the label.")

(defcustom markdown-footnote-default-type 'number
"Either `number' or `text', indicating what type of footnotes to insert by default."
:group 'markdown
:type 'symbol
:package-version '(markdown-mode . "2.5"))

(defcustom markdown-footnote-always-recalculate-number nil
"Non-nil if the footnote number should be recalculated before each insertion."
:group 'markdown
:type 'bool
:safe 'booleanp)


;;; Compatibility =============================================================

Expand Down Expand Up @@ -4415,7 +4427,8 @@ at the beginning of the block."

(defun markdown-footnote-counter-inc ()
"Increment `markdown-footnote-counter' and return the new value."
(when (= markdown-footnote-counter 0) ; hasn't been updated in this buffer yet.
(when (or markdown-footnote-always-recalculate-number
(= markdown-footnote-counter 0)) ; hasn't been updated in this buffer yet.
(save-excursion
(goto-char (point-min))
(while (re-search-forward (concat "^\\[\\^\\(" markdown-footnote-chars "*?\\)\\]:")
Expand All @@ -4425,16 +4438,24 @@ at the beginning of the block."
(setq markdown-footnote-counter fn))))))
(cl-incf markdown-footnote-counter))

(defun markdown-insert-footnote ()
"Insert footnote with a new number and move point to footnote definition."
(defun markdown-insert-footnote (&optional tag)
"Insert footnote and move point to footnote definition.
If TAG is `number', insert a number as the footnote identifier.
If TAG is `text', ask for text to name the footnote."
(interactive)
(let ((fn (markdown-footnote-counter-inc)))
(insert (format "[^%d]" fn))
(let ((fn (cond
((equal (or tag markdown-footnote-default-type) 'number)
(number-to-string (markdown-footnote-counter-inc)))
((equal (or tag markdown-footnote-default-type) 'text)
(read-string "Footnote tag: "))
(t
(error "Expected 'number or 'text. Found %s" (symbol-name tag))))))
(insert (format "[^%s]" fn))
(markdown-footnote-text-find-new-location)
(markdown-ensure-blank-line-before)
(unless (markdown-cur-line-blank-p)
(insert "\n"))
(insert (format "[^%d]: " fn))
(insert (format "[^%s]: " fn))
(markdown-ensure-blank-line-after)))

(defun markdown-footnote-text-find-new-location ()
Expand Down