1 ;;; org-footnote.el --- Footnote support in Org and elsewhere
3 ;; Copyright (C) 2009-2014 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;; This file contains the code dealing with footnotes in Org-mode.
28 ;; The code can also be used in arbitrary text modes to provide
29 ;; footnotes. Compared to Steven L Baur's footnote.el it provides
30 ;; better support for resuming editing. It is less configurable than
31 ;; Steve's code, though.
40 (declare-function message-point-in-header-p
"message" ())
41 (declare-function org-back-over-empty-lines
"org" ())
42 (declare-function org-back-to-heading
"org" (&optional invisible-ok
))
43 (declare-function org-combine-plists
"org" (&rest plists
))
44 (declare-function org-element-context
"org-element" (&optional element
))
45 (declare-function org-element-property
"org-element" (property element
))
46 (declare-function org-element-type
"org-element" (element))
47 (declare-function org-end-of-subtree
"org" (&optional invisible-ok to-heading
))
48 (declare-function org-fill-paragraph
"org" (&optional justify
))
49 (declare-function org-icompleting-read
"org" (&rest args
))
50 (declare-function org-id-uuid
"org-id" ())
51 (declare-function org-in-block-p
"org" (names))
52 (declare-function org-at-comment-p
"org" ())
53 (declare-function org-in-regexp
"org" (re &optional nlines visually
))
54 (declare-function org-in-verbatim-emphasis
"org" ())
55 (declare-function org-inside-LaTeX-fragment-p
"org" ())
56 (declare-function org-inside-latex-macro-p
"org" ())
57 (declare-function org-mark-ring-push
"org" (&optional pos buffer
))
58 (declare-function org-show-context
"org" (&optional key
))
59 (declare-function org-trim
"org" (s))
60 (declare-function org-skip-whitespace
"org" ())
61 (declare-function outline-next-heading
"outline")
62 (declare-function org-skip-whitespace
"org" ())
64 (defvar org-outline-regexp-bol
) ; defined in org.el
65 (defvar org-odd-levels-only
) ; defined in org.el
66 (defvar org-bracket-link-regexp
) ; defined in org.el
67 (defvar message-cite-prefix-regexp
) ; defined in message.el
68 (defvar message-signature-separator
) ; defined in message.el
70 (defconst org-footnote-re
71 ;; Only [1]-like footnotes are closed in this regexp, as footnotes
72 ;; from other types might contain square brackets (i.e. links) in
75 ;; `org-re' is used for regexp compatibility with XEmacs.
77 ;; Match inline footnotes.
78 (org-re "fn:\\([-_[:word:]]+\\)?:\\|")
79 ;; Match other footnotes.
80 "\\(?:\\([0-9]+\\)\\]\\)\\|"
81 (org-re "\\(fn:[-_[:word:]]+\\)")
83 "Regular expression for matching footnotes.")
85 (defconst org-footnote-definition-re
86 (org-re "^\\[\\([0-9]+\\|fn:[-_[:word:]]+\\)\\]")
87 "Regular expression matching the definition of a footnote.")
89 (defconst org-footnote-forbidden-blocks
90 '("ascii" "beamer" "comment" "example" "html" "latex" "odt" "src")
91 "Names of blocks where footnotes are not allowed.")
93 (defgroup org-footnote nil
94 "Footnotes in Org-mode."
98 (defcustom org-footnote-section
"Footnotes"
99 "Outline heading containing footnote definitions.
101 This can be nil, to place footnotes locally at the end of the
102 current outline node. If can also be the name of a special
103 outline heading under which footnotes should be put.
105 This variable defines the place where Org puts the definition
106 automatically, i.e. when creating the footnote, and when sorting
107 the notes. However, by hand you may place definitions
110 If this is a string, during export, all subtrees starting with
111 this heading will be ignored.
113 If you don't use the customize interface to change this variable,
114 you will need to run the following command after the change:
116 \\[universal-argument] \\[org-element-cache-reset]"
118 :initialize
'custom-initialize-default
119 :set
(lambda (var val
)
121 (when (fboundp 'org-element-cache-reset
)
122 (org-element-cache-reset 'all
)))
124 (string :tag
"Collect footnotes under heading")
125 (const :tag
"Define footnotes locally" nil
)))
127 (defcustom org-footnote-tag-for-non-org-mode-files
"Footnotes:"
128 "Tag marking the beginning of footnote section.
129 The Org footnote engine can be used in arbitrary text files as well
130 as in Org-mode. Outside Org mode, new footnotes are always placed at
131 the end of the file. When you normalize the notes, any line containing
132 only this tag will be removed, a new one will be inserted at the end
133 of the file, followed by the collected and normalized footnotes.
135 If you don't want any tag in such buffers, set this variable to nil."
138 (string :tag
"Collect footnotes under tag")
139 (const :tag
"Don't use a tag" nil
)))
141 (defcustom org-footnote-define-inline nil
142 "Non-nil means define footnotes inline, at reference location.
143 When nil, footnotes will be defined in a special section near
144 the end of the document. When t, the [fn:label:definition] notation
145 will be used to define the footnote at the reference position."
149 (defcustom org-footnote-auto-label t
150 "Non-nil means define automatically new labels for footnotes.
153 nil Prompt the user for each label.
154 t Create unique labels of the form [fn:1], [fn:2], etc.
155 confirm Like t, but let the user edit the created value.
156 The label can be removed from the minibuffer to create
157 an anonymous footnote.
158 random Automatically generate a unique, random label.
159 plain Automatically create plain number labels like [1]."
162 (const :tag
"Prompt for label" nil
)
163 (const :tag
"Create automatic [fn:N]" t
)
164 (const :tag
"Offer automatic [fn:N] for editing" confirm
)
165 (const :tag
"Create a random label" random
)
166 (const :tag
"Create automatic [N]" plain
)))
168 (defcustom org-footnote-auto-adjust nil
169 "Non-nil means automatically adjust footnotes after insert/delete.
170 When this is t, after each insertion or deletion of a footnote,
171 simple fn:N footnotes will be renumbered, and all footnotes will be sorted.
172 If you want to have just sorting or just renumbering, set this variable
173 to `sort' or `renumber'.
175 The main values of this variable can be set with in-buffer options:
178 #+STARTUP: nofnadjust"
181 (const :tag
"No adjustment" nil
)
182 (const :tag
"Renumber" renumber
)
183 (const :tag
"Sort" sort
)
184 (const :tag
"Renumber and Sort" t
)))
186 (defcustom org-footnote-fill-after-inline-note-extraction nil
187 "Non-nil means fill paragraphs after extracting footnotes.
188 When extracting inline footnotes, the lengths of lines can change a lot.
189 When this option is set, paragraphs from which an inline footnote has been
190 extracted will be filled again."
194 (defun org-footnote-in-valid-context-p ()
195 "Is point in a context where footnotes are allowed?"
197 (not (or (org-at-comment-p)
198 (org-inside-LaTeX-fragment-p)
199 ;; Avoid literal example.
200 (org-in-verbatim-emphasis)
203 (looking-at "[ \t]*:[ \t]+"))
204 ;; Avoid cited text and headers in message-mode.
205 (and (derived-mode-p 'message-mode
)
208 (looking-at message-cite-prefix-regexp
))
209 (message-point-in-header-p)))
210 ;; Avoid forbidden blocks.
211 (org-in-block-p org-footnote-forbidden-blocks
)))))
213 (defun org-footnote-at-reference-p ()
214 "Is the cursor at a footnote reference?
216 If so, return a list containing its label, beginning and ending
217 positions, and the definition, when inlined."
218 (when (and (org-footnote-in-valid-context-p)
219 (or (looking-at org-footnote-re
)
220 (org-in-regexp org-footnote-re
)
221 (save-excursion (re-search-backward org-footnote-re nil t
)))
222 (/= (match-beginning 0) (point-at-bol)))
223 (let* ((beg (match-beginning 0))
224 (label (or (org-match-string-no-properties 2)
225 (org-match-string-no-properties 3)
226 ;; Anonymous footnotes don't have labels
227 (and (match-string 1)
228 (concat "fn:" (org-match-string-no-properties 1)))))
229 ;; Inline footnotes don't end at (match-end 0) as
230 ;; `org-footnote-re' stops just after the second colon.
231 ;; Find the real ending with `scan-sexps', so Org doesn't
232 ;; get fooled by unrelated closing square brackets.
233 (end (ignore-errors (scan-sexps beg
1))))
234 ;; Point is really at a reference if it's located before true
235 ;; ending of the footnote.
236 (when (and end
(< (point) end
)
237 ;; Verify match isn't a part of a link.
242 (org-in-regexp org-bracket-link-regexp
))))
243 (and linkp
(< (point) (cdr linkp
))))))
244 ;; Verify point doesn't belong to a LaTeX macro.
245 (not (org-inside-latex-macro-p)))
247 ;; Definition: ensure this is an inline footnote first.
248 (and (or (not label
) (match-string 1))
249 (org-trim (buffer-substring-no-properties
250 (match-end 0) (1- end
)))))))))
252 (defun org-footnote-at-definition-p ()
253 "Is point within a footnote definition?
255 This matches only pure definitions like [1] or [fn:name] at the
256 beginning of a line. It does not match references like
257 \[fn:name:definition], where the footnote text is included and
260 The return value will be nil if not at a footnote definition, and
261 a list with label, start, end and definition of the footnote
263 (when (save-excursion (beginning-of-line) (org-footnote-in-valid-context-p))
266 ;; Footnotes definitions are separated by new headlines, another
267 ;; footnote definition or 2 blank lines.
268 (let ((lim (save-excursion
270 (concat org-outline-regexp-bol
271 "\\|^\\([ \t]*\n\\)\\{2,\\}") nil t
))))
272 (when (re-search-backward org-footnote-definition-re lim t
)
273 (let ((label (org-match-string-no-properties 1))
274 (beg (match-beginning 0))
275 (beg-def (match-end 0))
276 ;; In message-mode, do not search after signature.
277 (end (let ((bound (and (derived-mode-p 'message-mode
)
279 (goto-char (point-max))
281 message-signature-separator nil t
)))))
285 (concat org-outline-regexp-bol
"\\|"
286 org-footnote-definition-re
"\\|"
287 "^\\([ \t]*\n\\)\\{2,\\}") bound
'move
))
291 (org-trim (buffer-substring-no-properties beg-def end
)))))))))
293 (defun org-footnote-get-next-reference (&optional label backward limit
)
294 "Return complete reference of the next footnote.
296 If LABEL is provided, get the next reference of that footnote. If
297 BACKWARD is non-nil, find previous reference instead. LIMIT is
298 the buffer position bounding the search.
300 Return value is a list like those provided by `org-footnote-at-reference-p'.
301 If no footnote is found, return nil."
303 (let* ((label-fmt (if label
(format "\\[%s[]:]" label
) org-footnote-re
)))
306 (unless (funcall (if backward
#'re-search-backward
#'re-search-forward
)
309 (unless backward
(backward-char))
310 (let ((ref (org-footnote-at-reference-p)))
311 (when ref
(throw 'exit ref
))))))))
313 (defun org-footnote-next-reference-or-definition (limit)
314 "Move point to next footnote reference or definition.
316 LIMIT is the buffer position bounding the search.
318 Return value is a list like those provided by
319 `org-footnote-at-reference-p' or `org-footnote-at-definition-p'.
320 If no footnote is found, return nil."
321 (let* (ref (origin (point)))
324 (unless (re-search-forward org-footnote-re limit t
)
327 ;; Beware: with [1]-like footnotes point will be just after
328 ;; the closing square bracket.
331 ((setq ref
(org-footnote-at-reference-p))
333 ;; Definition: also grab the last square bracket, only
334 ;; matched in `org-footnote-re' for [1]-like footnotes.
335 ((save-match-data (org-footnote-at-definition-p))
336 (let ((end (match-end 0)))
338 (list nil
(match-beginning 0)
339 (if (eq (char-before end
) 93) end
(1+ end
)))))))))))
341 (defun org-footnote-get-definition (label)
342 "Return label, boundaries and definition of the footnote LABEL."
343 (let* ((label (regexp-quote (org-footnote-normalize-label label
)))
344 (re (format "^\\[%s\\]\\|.\\[%s:" label label
))
348 (when (or (re-search-forward re nil t
)
349 (and (goto-char (point-min))
350 (re-search-forward re nil t
))
351 (and (progn (widen) t
)
352 (goto-char (point-min))
353 (re-search-forward re nil t
)))
354 (let ((refp (org-footnote-at-reference-p)))
356 ((and (nth 3 refp
) refp
))
357 ((org-footnote-at-definition-p)))))))))
359 (defun org-footnote-goto-definition (label)
360 "Move point to the definition of the footnote LABEL.
361 Return a non-nil value when a definition has been found."
362 (interactive "sLabel: ")
364 (let ((def (org-footnote-get-definition label
)))
366 (error "Cannot find definition of footnote %s" label
)
367 (goto-char (nth 1 def
))
368 (looking-at (format "\\[%s\\]\\|\\[%s:" label label
))
369 (goto-char (match-end 0))
370 (org-show-context 'link-search
)
371 (when (derived-mode-p 'org-mode
)
372 (message "Edit definition and go back with `C-c &' or, if unique, with `C-c C-c'."))
375 (defun org-footnote-goto-previous-reference (label)
376 "Find the first closest (to point) reference of footnote with label LABEL."
377 (interactive "sLabel: ")
379 (let* ((label (org-footnote-normalize-label label
)) ref
)
381 (setq ref
(or (org-footnote-get-next-reference label t
)
382 (org-footnote-get-next-reference label
)
386 (org-footnote-get-next-reference label t
)
387 (org-footnote-get-next-reference label
))))))
389 (error "Cannot find reference of footnote %s" label
)
390 (goto-char (nth 1 ref
))
391 (org-show-context 'link-search
))))
393 (defun org-footnote-normalize-label (label)
394 "Return LABEL as an appropriate string."
396 ((numberp label
) (number-to-string label
))
397 ((equal "" label
) nil
)
398 ((not (string-match "^[0-9]+$\\|^fn:" label
))
399 (concat "fn:" label
))
402 (defun org-footnote-all-labels (&optional with-defs
)
403 "Return list with all defined foot labels used in the buffer.
405 If WITH-DEFS is non-nil, also associate the definition to each
406 label. The function will then return an alist whose key is label
407 and value definition."
411 ;; Depending on WITH-DEFS, store label or (label . def) of
412 ;; footnote reference/definition given as argument in RTN.
414 (let ((lbl (car el
)))
415 (push (if with-defs
(cons lbl
(nth 3 el
)) lbl
) rtn
))))))
419 ;; Find all labels found in definitions.
420 (goto-char (point-min))
422 (while (re-search-forward org-footnote-definition-re nil t
)
423 (when (setq def
(org-footnote-at-definition-p))
424 (funcall push-to-rtn def
))))
425 ;; Find all labels found in references.
426 (goto-char (point-min))
428 (while (setq ref
(org-footnote-get-next-reference))
429 (goto-char (nth 2 ref
))
430 (and (car ref
) ; ignore anonymous footnotes
431 (not (funcall (if with-defs
#'assoc
#'member
) (car ref
) rtn
))
432 (funcall push-to-rtn ref
))))))
435 (defun org-footnote-unique-label (&optional current
)
436 "Return a new unique footnote label.
438 The function returns the first \"fn:N\" or \"N\" label that is
441 Optional argument CURRENT is the list of labels active in the
443 (unless current
(setq current
(org-footnote-all-labels)))
444 (let ((fmt (if (eq org-footnote-auto-label
'plain
) "%d" "fn:%d"))
446 (while (member (format fmt cnt
) current
)
450 (defun org-footnote-new ()
451 "Insert a new footnote.
452 This command prompts for a label. If this is a label referencing an
453 existing label, only insert the label. If the footnote label is empty
454 or new, let the user edit the definition of the footnote."
456 (unless (org-footnote-in-valid-context-p)
457 (error "Cannot insert a footnote here"))
458 (let* ((lbls (and (not (equal org-footnote-auto-label
'random
))
459 (org-footnote-all-labels)))
460 (propose (and (not (equal org-footnote-auto-label
'random
))
461 (org-footnote-unique-label lbls
)))
463 (org-footnote-normalize-label
465 ((member org-footnote-auto-label
'(t plain
))
467 ((equal org-footnote-auto-label
'random
)
468 (format "fn:%x" (random #x100000000
)))
470 (org-icompleting-read
471 "Label (leave empty for anonymous): "
472 (mapcar 'list lbls
) nil nil
473 (if (eq org-footnote-auto-label
'confirm
) propose nil
)))))))
475 ((bolp) (error "Cannot create a footnote reference at left margin"))
480 (insert "[" label
"]")
481 (message "New reference to existing note"))
482 (org-footnote-define-inline
483 (insert "[" label
": ]")
485 (org-footnote-auto-adjust-maybe))
487 (insert "[" label
"]")
488 (org-footnote-create-definition label
)
489 (org-footnote-auto-adjust-maybe)))))
491 (defvar org-blank-before-new-entry
) ; silence byte-compiler
492 (defun org-footnote-create-definition (label)
493 "Start the definition of a footnote with label LABEL."
494 (interactive "sLabel: ")
495 (let ((label (org-footnote-normalize-label label
))
496 electric-indent-mode
) ;; Prevent wrong indentation
499 ((derived-mode-p 'org-mode
)
500 ;; If `org-footnote-section' is defined, find it, or create it
501 ;; at the end of the buffer.
502 (when org-footnote-section
503 (goto-char (point-min))
504 (let ((re (concat "^\\*+[ \t]+" org-footnote-section
"[ \t]*$")))
505 (unless (or (re-search-forward re nil t
)
506 (and (progn (widen) t
)
507 (re-search-forward re nil t
)))
508 (goto-char (point-max))
509 (skip-chars-backward " \t\r\n")
510 (unless (bolp) (newline))
511 ;; Insert new section. Separate it from the previous one
512 ;; with a blank line, unless `org-blank-before-new-entry'
513 ;; explicitly says no.
514 (when (and (cdr (assq 'heading org-blank-before-new-entry
))
515 (zerop (save-excursion (org-back-over-empty-lines))))
517 (insert "* " org-footnote-section
"\n"))))
518 ;; Move to the end of this entry (which may be
519 ;; `org-footnote-section' or the current one).
520 (org-footnote-goto-local-insertion-point)
521 (org-show-context 'link-search
))
523 ;; In a non-Org file. Search for footnote tag, or create it if
524 ;; specified (at the end of buffer, or before signature if in
525 ;; Message mode). Set point after any definition already there.
526 (let ((tag (and org-footnote-tag-for-non-org-mode-files
527 (concat "^" (regexp-quote
528 org-footnote-tag-for-non-org-mode-files
)
530 (max (if (and (derived-mode-p 'message-mode
)
531 (goto-char (point-max))
533 message-signature-separator nil t
))
535 ;; Ensure one blank line separates last
536 ;; footnote from signature.
540 (point-max-marker))))
541 (set-marker-insertion-type max t
)
543 ;; Check if the footnote tag is defined but missing. In this
544 ;; case, insert it, before any footnote or one blank line
545 ;; after any previous text.
546 (when (and tag
(not (re-search-backward tag nil t
)))
547 (skip-chars-backward " \t\r\n")
548 (while (re-search-backward org-footnote-definition-re nil t
))
549 (unless (bolp) (newline 2))
550 (insert org-footnote-tag-for-non-org-mode-files
"\n\n"))
551 ;; Remove superfluous white space and clear marker.
553 (skip-chars-backward " \t\r\n")
554 (delete-region (point) max
)
555 (unless (bolp) (newline))
556 (set-marker max nil
))))
557 ;; Insert footnote label.
558 (when (zerop (org-back-over-empty-lines)) (newline))
559 (insert "[" label
"] \n")
561 ;; Only notify user about next possible action when in an Org
562 ;; buffer, as the bindings may have different meanings otherwise.
563 (when (derived-mode-p 'org-mode
)
565 "Edit definition and go back with `C-c &' or, if unique, with `C-c C-c'."))))
568 (defun org-footnote-action (&optional special
)
569 "Do the right thing for footnotes.
571 When at a footnote reference, jump to the definition.
573 When at a definition, jump to the references if they exist, offer
574 to create them otherwise.
576 When neither at definition or reference, create a new footnote,
577 interactively if possible.
579 With prefix arg SPECIAL, or when no footnote can be created,
580 offer additional commands in a menu."
582 (let* ((context (and (not special
) (org-element-context)))
583 (type (org-element-type context
)))
585 ((eq type
'footnote-reference
)
586 (let ((label (org-element-property :label context
)))
588 ;; Anonymous footnote: move point at the beginning of its
591 (goto-char (org-element-property :contents-begin context
)))
592 ;; A definition exists: move to it.
593 ((ignore-errors (org-footnote-goto-definition label
)))
594 ;; No definition exists: offer to create it.
595 ((yes-or-no-p (format "No definition for %s. Create one? " label
))
596 (org-footnote-create-definition label
)))))
597 ((eq type
'footnote-definition
)
598 (org-footnote-goto-previous-reference
599 (org-element-property :label context
)))
601 (zerop (current-column))
602 (not (org-footnote-in-valid-context-p)))
603 (message "Footnotes: [s]ort | [r]enumber fn:N | [S]=r+s |->[n]umeric | [d]elete")
604 (let ((c (read-char-exclusive)))
606 ((eq c ?s
) (org-footnote-normalize 'sort
))
607 ((eq c ?r
) (org-footnote-renumber-fn:N
))
609 (org-footnote-renumber-fn:N
)
610 (org-footnote-normalize 'sort
))
611 ((eq c ?n
) (org-footnote-normalize))
612 ((eq c ?d
) (org-footnote-delete))
613 (t (error "No such footnote command %c" c
)))))
614 (t (org-footnote-new)))))
617 (defun org-footnote-normalize (&optional sort-only
)
618 "Collect the footnotes in various formats and normalize them.
620 This finds the different sorts of footnotes allowed in Org, and
621 normalizes them to the usual [N] format.
623 When SORT-ONLY is set, only sort the footnote definitions into the
624 referenced sequence."
625 ;; This is based on Paul's function, but rewritten.
627 ;; Re-create `org-with-limited-levels', but not limited to Org
630 (and (boundp 'org-inlinetask-min-level
)
631 org-inlinetask-min-level
632 (1- org-inlinetask-min-level
)))
633 (nstars (and limit-level
634 (if org-odd-levels-only
(1- (* limit-level
2))
637 (concat "\\*" (if nstars
(format "\\{1,%d\\} " nstars
) "+ ")))
639 ins-point ref ref-table
)
641 ;; 1. Find every footnote reference, extract the definition, and
642 ;; collect that data in REF-TABLE. If SORT-ONLY is nil, also
643 ;; normalize references.
644 (goto-char (point-min))
645 (while (setq ref
(org-footnote-get-next-reference))
646 (let* ((lbl (car ref
))
648 ;; When footnote isn't anonymous, check if it's label
649 ;; (REF) is already stored in REF-TABLE. In that case,
650 ;; extract number used to identify it (MARKER). If
651 ;; footnote is unknown, increment the global counter
652 ;; (COUNT) to create an unused identifier.
653 (a (and lbl
(assoc lbl ref-table
)))
654 (marker (or (nth 1 a
) (incf count
)))
655 ;; Is the reference inline or pointing to an inline
657 (inlinep (or (stringp (nth 3 ref
)) (nth 3 a
))))
658 ;; Replace footnote reference with [MARKER]. Maybe fill
659 ;; paragraph once done. If SORT-ONLY is non-nil, only move
660 ;; to the end of reference found to avoid matching it twice.
661 (if sort-only
(goto-char (nth 2 ref
))
662 (delete-region (nth 1 ref
) (nth 2 ref
))
663 (goto-char (nth 1 ref
))
664 (insert (format "[%d]" marker
))
666 org-footnote-fill-after-inline-note-extraction
667 (org-fill-paragraph)))
668 ;; Add label (REF), identifier (MARKER), definition (DEF)
669 ;; type (INLINEP) and position (POS) to REF-TABLE if data
672 (let ((def (or (nth 3 ref
) ; Inline definition.
673 (nth 3 (org-footnote-get-definition lbl
)))))
674 (push (list lbl marker def
675 ;; Reference beginning position is a marker
676 ;; to preserve it during further buffer
678 inlinep
(copy-marker pos
)) ref-table
)))))
679 ;; 2. Find and remove the footnote section, if any. Also
680 ;; determine where footnotes shall be inserted (INS-POINT).
682 ((and org-footnote-section
(derived-mode-p 'org-mode
))
683 (goto-char (point-min))
684 (if (re-search-forward
685 (concat "^\\*[ \t]+" (regexp-quote org-footnote-section
)
687 (delete-region (match-beginning 0) (org-end-of-subtree t t
)))
688 ;; A new footnote section is inserted by default at the end of
690 (goto-char (point-max))
691 (skip-chars-backward " \r\t\n")
693 (unless (bolp) (newline)))
694 ;; No footnote section set: Footnotes will be added at the end
695 ;; of the section containing their first reference.
696 ((derived-mode-p 'org-mode
))
698 ;; Remove any left-over tag in the buffer, if one is set up.
699 (when org-footnote-tag-for-non-org-mode-files
700 (let ((tag (concat "^" (regexp-quote
701 org-footnote-tag-for-non-org-mode-files
)
703 (goto-char (point-min))
704 (while (re-search-forward tag nil t
)
706 (delete-region (point) (progn (forward-line) (point))))))
707 ;; In Message mode, ensure footnotes are inserted before the
709 (if (and (derived-mode-p 'message-mode
)
710 (goto-char (point-max))
711 (re-search-backward message-signature-separator nil t
))
713 (goto-char (point-max)))))
714 (setq ins-point
(point-marker))
715 ;; 3. Clean-up REF-TABLE.
721 ;; When only sorting, ignore inline footnotes.
722 ;; Also clear position marker.
723 ((and sort-only
(nth 3 x
))
724 (set-marker (nth 4 x
) nil
) nil
)
725 ;; No definition available: provide one.
728 (list (car x
) (nth 1 x
)
729 (format "DEFINITION NOT FOUND: %s" (car x
)))
733 (setq ref-table
(nreverse ref-table
))
734 ;; 4. Remove left-over definitions in the buffer.
736 (unless (nth 3 x
) (org-footnote-delete-definitions (car x
))))
738 ;; 5. Insert the footnotes again in the buffer, at the
740 (goto-char ins-point
)
742 ;; No footnote: exit.
744 ;; Cases when footnotes should be inserted in one place.
745 ((or (not (derived-mode-p 'org-mode
)) org-footnote-section
)
746 ;; Insert again the section title, if any. Ensure that title,
747 ;; or the subsequent footnotes, will be separated by a blank
748 ;; lines from the rest of the document. In an Org buffer,
749 ;; separate section with a blank line, unless explicitly
750 ;; stated in `org-blank-before-new-entry'.
751 (if (not (derived-mode-p 'org-mode
))
752 (progn (skip-chars-backward " \t\n\r")
753 (delete-region (point) ins-point
)
754 (unless (bolp) (newline))
755 (when org-footnote-tag-for-non-org-mode-files
756 (insert "\n" org-footnote-tag-for-non-org-mode-files
"\n")))
757 (when (and (cdr (assq 'heading org-blank-before-new-entry
))
758 (zerop (save-excursion (org-back-over-empty-lines))))
760 (insert "* " org-footnote-section
"\n"))
761 (set-marker ins-point nil
)
762 ;; Insert the footnotes, separated by a blank line.
767 (set-marker (nth 4 x
) nil
)
768 (format "\n[%s] %s" (nth (if sort-only
0 1) x
) (nth 2 x
)))
770 (unless (eobp) (insert "\n\n")))
771 ;; Each footnote definition has to be inserted at the end of
772 ;; the section where its first reference belongs.
776 (let ((pos (nth 4 x
)))
779 (set-marker pos nil
))
780 (org-footnote-goto-local-insertion-point)
781 (insert (format "\n[%s] %s\n"
782 (if sort-only
(car x
) (nth 1 x
))
786 (defun org-footnote-goto-local-insertion-point ()
787 "Find insertion point for footnote, just before next outline heading."
788 (org-with-limited-levels (outline-next-heading))
789 (or (bolp) (newline))
790 (beginning-of-line 0)
791 (while (and (not (bobp)) (= (char-after) ?
#))
792 (beginning-of-line 0))
793 (if (let ((case-fold-search t
)) (looking-at "[ \t]*#\\+tblfm:")) (beginning-of-line 2))
795 (skip-chars-backward "\n\r\t ")
798 (defun org-footnote-delete-references (label)
799 "Delete every reference to footnote LABEL.
800 Return the number of footnotes removed."
802 (goto-char (point-min))
804 (while (setq ref
(org-footnote-get-next-reference label
))
805 (goto-char (nth 1 ref
))
806 (delete-region (nth 1 ref
) (nth 2 ref
))
810 (defun org-footnote-delete-definitions (label)
811 "Delete every definition of the footnote LABEL.
812 Return the number of footnotes removed."
814 (goto-char (point-min))
815 (let ((def-re (concat "^\\[" (regexp-quote label
) "\\]"))
817 (while (re-search-forward def-re nil t
)
818 (let ((full-def (org-footnote-at-definition-p)))
820 ;; Remove the footnote, and all blank lines before it.
821 (goto-char (nth 1 full-def
))
822 (skip-chars-backward " \r\t\n")
823 (unless (bolp) (forward-line))
824 (delete-region (point) (nth 2 full-def
))
828 (defun org-footnote-delete (&optional label
)
829 "Delete the footnote at point.
830 This will remove the definition (even multiple definitions if they exist)
831 and all references of a footnote label.
833 If LABEL is non-nil, delete that footnote instead."
835 (let* ((nref 0) (ndef 0) x
836 ;; 1. Determine LABEL of footnote at point.
838 ;; LABEL is provided as argument.
840 ;; Footnote reference at point. If the footnote is
841 ;; anonymous, delete it and exit instead.
842 ((setq x
(org-footnote-at-reference-p))
845 (delete-region (nth 1 x
) (nth 2 x
))
846 (message "Anonymous footnote removed")
848 ;; Footnote definition at point.
849 ((setq x
(org-footnote-at-definition-p))
851 (t (error "Don't know which footnote to remove")))))
852 ;; 2. Now that LABEL is non-nil, find every reference and every
853 ;; definition, and delete them.
854 (setq nref
(org-footnote-delete-references label
)
855 ndef
(org-footnote-delete-definitions label
))
856 ;; 3. Verify consistency of footnotes and notify user.
857 (org-footnote-auto-adjust-maybe)
858 (message "%d definition(s) of and %d reference(s) of footnote %s removed"
861 (defun org-footnote-renumber-fn:N
()
862 "Renumber the simple footnotes like fn:17 into a sequence in the document."
865 (org-with-wide-buffer
866 (goto-char (point-min))
867 (while (re-search-forward "\\[fn:\\([0-9]+\\)[]:]" nil t
)
869 (goto-char (match-beginning 0))
870 ;; Ensure match is a footnote reference or definition.
871 (when (save-match-data (if (bolp)
872 (org-footnote-at-definition-p)
873 (org-footnote-at-reference-p)))
874 (let ((new-val (or (cdr (assoc (match-string 1) map
))
875 (number-to-string (incf n
)))))
876 (unless (assoc (match-string 1) map
)
877 (push (cons (match-string 1) new-val
) map
))
878 (replace-match new-val nil nil nil
1))))))))
880 (defun org-footnote-auto-adjust-maybe ()
881 "Renumber and/or sort footnotes according to user settings."
882 (when (memq org-footnote-auto-adjust
'(t renumber
))
883 (org-footnote-renumber-fn:N
))
884 (when (memq org-footnote-auto-adjust
'(t sort
))
885 (let ((label (car (org-footnote-at-definition-p))))
886 (org-footnote-normalize 'sort
)
888 (goto-char (point-min))
889 (and (re-search-forward (concat "^\\[" (regexp-quote label
) "\\]")
892 (just-one-space)))))))
894 (provide 'org-footnote
)
897 ;; generated-autoload-file: "org-loaddefs.el"
900 ;;; org-footnote.el ends here