1 ;;; org-footnote.el --- Footnote support in Org and elsewhere
3 ;; Copyright (C) 2009-2011 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;; This file contains the code dealing with footnotes in Org-mode.
29 ;; The code can also be used in arbitrary text modes to provide
30 ;; footnotes. Compared to Steven L Baur's footnote.el it provides
31 ;; better support for resuming editing. It is less configurable than
32 ;; Steve's code, though.
41 (declare-function org-combine-plists "org" (&rest plists))
42 (declare-function org-in-commented-line "org" ())
43 (declare-function org-in-indented-comment-line "org" ())
44 (declare-function org-in-regexp "org" (re &optional nlines visually))
45 (declare-function org-in-block-p "org" (names))
46 (declare-function org-mark-ring-push "org" (&optional pos buffer))
47 (declare-function outline-next-heading "outline")
48 (declare-function org-trim "org" (s))
49 (declare-function org-show-context "org" (&optional key))
50 (declare-function org-back-to-heading "org" (&optional invisible-ok))
51 (declare-function org-end-of-subtree "org" (&optional invisible-ok to-heading))
52 (declare-function org-in-verbatim-emphasis "org" ())
53 (declare-function org-inside-latex-macro-p "org" ())
54 (declare-function org-id-uuid "org" ())
55 (declare-function org-fill-paragraph "org" (&optional justify))
56 (declare-function org-export-preprocess-string "org-exp"
57 (string &rest parameters))
59 (defvar org-outline-regexp-bol) ; defined in org.el
60 (defvar org-odd-levels-only) ;; defined in org.el
61 (defvar org-bracket-link-regexp) ; defined in org.el
62 (defvar message-signature-separator) ;; defined in message.el
64 (defconst org-footnote-re
65 ;; Only [1]-like footnotes are closed in this regexp, as footnotes
66 ;; from other types might contain square brackets (i.e. links) in
69 ;; `org-re' is used for regexp compatibility with XEmacs.
70 (org-re (concat "\\[\\(?:"
71 ;; Match inline footnotes.
72 "fn:\\([-_[:word:]]+\\)?:\\|"
73 ;; Match other footnotes.
74 "\\(?:\\([0-9]+\\)\\]\\)\\|"
75 "\\(fn:[-_[:word:]]+\\)"
77 "Regular expression for matching footnotes.")
79 (defconst org-footnote-definition-re
80 (org-re "^\\(\\[\\([0-9]+\\|fn:[-_[:word:]]+\\)\\]\\)")
81 "Regular expression matching the definition of a footnote.")
83 (defvar org-footnote-forbidden-blocks '("example" "verse" "src" "ascii" "beamer"
84 "docbook" "html" "latex" "odt")
85 "Names of blocks where footnotes are not allowed.")
87 (defgroup org-footnote nil
88 "Footnotes in Org-mode."
92 (defcustom org-footnote-section "Footnotes"
93 "Outline heading containing footnote definitions before export.
94 This can be nil, to place footnotes locally at the end of the current
95 outline node. If can also be the name of a special outline heading
96 under which footnotes should be put.
97 This variable defines the place where Org puts the definition
98 automatically, i.e. when creating the footnote, and when sorting the notes.
99 However, by hand you may place definitions *anywhere*.
100 If this is a string, during export, all subtrees starting with this
101 heading will be removed after extracting footnote definitions."
104 (string :tag "Collect footnotes under heading")
105 (const :tag "Define footnotes locally" nil)))
107 (defcustom org-footnote-tag-for-non-org-mode-files "Footnotes:"
108 "Tag marking the beginning of footnote section.
109 The Org-mode footnote engine can be used in arbitrary text files as well
110 as in Org-mode. Outside Org-mode, new footnotes are always placed at
111 the end of the file. When you normalize the notes, any line containing
112 only this tag will be removed, a new one will be inserted at the end
113 of the file, followed by the collected and normalized footnotes."
117 (defcustom org-footnote-define-inline nil
118 "Non-nil means define footnotes inline, at reference location.
119 When nil, footnotes will be defined in a special section near
120 the end of the document. When t, the [fn:label:definition] notation
121 will be used to define the footnote at the reference position."
125 (defcustom org-footnote-auto-label t
126 "Non-nil means define automatically new labels for footnotes.
129 nil prompt the user for each label
130 t create unique labels of the form [fn:1], [fn:2], ...
131 confirm like t, but let the user edit the created value. In particular,
132 the label can be removed from the minibuffer, to create
133 an anonymous footnote.
134 random Automatically generate a unique, random label.
135 plain Automatically create plain number labels like [1]"
138 (const :tag "Prompt for label" nil)
139 (const :tag "Create automatic [fn:N]" t)
140 (const :tag "Offer automatic [fn:N] for editing" confirm)
141 (const :tag "Create a random label" random)
142 (const :tag "Create automatic [N]" plain)))
144 (defcustom org-footnote-auto-adjust nil
145 "Non-nil means automatically adjust footnotes after insert/delete.
146 When this is t, after each insertion or deletion of a footnote,
147 simple fn:N footnotes will be renumbered, and all footnotes will be sorted.
148 If you want to have just sorting or just renumbering, set this variable
149 to `sort' or `renumber'.
151 The main values of this variable can be set with in-buffer options:
154 #+STARTUP: nofnadjust"
157 (const :tag "Renumber" renumber)
158 (const :tag "Sort" sort)
159 (const :tag "Renumber and Sort" t)))
161 (defcustom org-footnote-fill-after-inline-note-extraction nil
162 "Non-nil means fill paragraphs after extracting footnotes.
163 When extracting inline footnotes, the lengths of lines can change a lot.
164 When this option is set, paragraphs from which an inline footnote has been
165 extracted will be filled again."
169 (defun org-footnote-in-valid-context-p ()
170 "Is point in a context where footnotes are allowed?"
172 (not (or (org-in-commented-line)
173 (org-in-indented-comment-line)
174 (org-in-verbatim-emphasis)
175 ;; Avoid literal example.
178 (looking-at "[ \t]*:[ \t]+"))
179 ;; Avoid cited text and headers in message-mode.
180 (and (derived-mode-p 'message-mode)
183 (looking-at message-cite-prefix-regexp))
184 (message-point-in-header-p)))
185 ;; Avoid forbidden blocks.
186 (org-in-block-p org-footnote-forbidden-blocks)))))
188 (defun org-footnote-at-reference-p ()
189 "Is the cursor at a footnote reference?
191 If so, return a list containing its label, beginning and ending
192 positions, and the definition, when inlined."
193 (when (and (org-footnote-in-valid-context-p)
194 (or (looking-at org-footnote-re)
195 (org-in-regexp org-footnote-re)
196 (save-excursion (re-search-backward org-footnote-re nil t)))
197 ;; Only inline footnotes can start at bol.
198 (or (eq (char-before (match-end 0)) 58)
199 (/= (match-beginning 0) (point-at-bol))))
200 (let* ((beg (match-beginning 0))
201 (label (or (match-string 2) (match-string 3)
202 ;; Anonymous footnotes don't have labels
203 (and (match-string 1) (concat "fn:" (match-string 1)))))
204 ;; Inline footnotes don't end at (match-end 0) as
205 ;; `org-footnote-re' stops just after the second colon.
206 ;; Find the real ending with `scan-sexps', so Org doesn't
207 ;; get fooled by unrelated closing square brackets.
208 (end (ignore-errors (scan-sexps beg 1))))
209 ;; Point is really at a reference if it's located before true
210 ;; ending of the footnote.
211 (when (and end (< (point) end)
212 ;; Verify match isn't a part of a link.
217 (org-in-regexp org-bracket-link-regexp))))
218 (and linkp (< (point) (cdr linkp))))))
219 ;; Verify point doesn't belong to a LaTeX macro.
220 ;; Beware though, when two footnotes are side by
221 ;; side, once the first one is changed into LaTeX,
222 ;; the second one might then be considered as an
223 ;; optional argument of the command. Thus, check
224 ;; the `org-protected' property of that command.
225 (or (not (org-inside-latex-macro-p))
226 (and (get-text-property (1- beg) 'org-protected)
227 (not (get-text-property beg 'org-protected)))))
229 ;; Definition: ensure this is an inline footnote first.
230 (and (or (not label) (match-string 1))
231 (org-trim (buffer-substring (match-end 0) (1- end)))))))))
233 (defun org-footnote-at-definition-p ()
234 "Is the cursor at a footnote definition?
236 This matches only pure definitions like [1] or [fn:name] at the beginning
237 of a line. It does not match references like [fn:name:definition], where the
238 footnote text is included and defined locally.
240 The return value will be nil if not at a footnote definition, and a list with
241 label, start, end and definition of the footnote otherwise."
242 (when (org-footnote-in-valid-context-p)
245 (let ((lim (save-excursion (re-search-backward
246 (concat org-outline-regexp-bol
247 "\\|^[ \t]*$") nil t))))
248 (when (re-search-backward org-footnote-definition-re lim t)
250 (list (match-string 2)
253 ;; In a message, limit search to signature.
254 (let ((bound (and (derived-mode-p 'message-mode)
256 (goto-char (point-max))
258 message-signature-separator nil t)))))
259 (or (and (re-search-forward
261 (concat "^[ \t]*$" "\\|"
262 org-outline-regexp-bol
264 "^\\[\\([0-9]+\\|fn:[-_[:word:]]+\\)\\]"))
266 (progn (skip-chars-forward " \t\n") (point-at-bol)))
268 (org-trim (buffer-substring (match-end 0) (point)))))))))
270 (defun org-footnote-get-next-reference (&optional label backward limit)
271 "Return complete reference of the next footnote.
273 If LABEL is provided, get the next reference of that footnote. If
274 BACKWARD is non-nil, find previous reference instead. LIMIT is
275 the buffer position bounding the search.
277 Return value is a list like those provided by `org-footnote-at-reference-p'.
278 If no footnote is found, return nil."
280 (let* ((label-fmt (if label (format "\\[%s[]:]" label) org-footnote-re)))
283 (unless (funcall (if backward #'re-search-backward #'re-search-forward)
286 (unless backward (backward-char))
287 (let ((ref (org-footnote-at-reference-p)))
288 (when ref (throw 'exit ref))))))))
290 (defun org-footnote-next-reference-or-definition (limit)
291 "Move point to next footnote reference or definition.
293 LIMIT is the buffer position bounding the search.
295 Return value is a list like those provided by
296 `org-footnote-at-reference-p' or `org-footnote-at-definition-p'.
297 If no footnote is found, return nil."
301 (unless (re-search-forward org-footnote-re limit t)
303 ;; Beware: with [1]-like footnotes point will be just after
304 ;; the closing square bracket.
307 ((setq ref (org-footnote-at-reference-p))
309 ;; Definition: also grab the last square bracket, only
310 ;; matched in `org-footnote-re' for [1]-like footnotes.
311 ((save-match-data (org-footnote-at-definition-p))
312 (let ((end (match-end 0)))
314 (list nil (match-beginning 0)
315 (if (eq (char-before end) 93) end (1+ end)))))))))))
317 (defun org-footnote-get-definition (label)
318 "Return label, boundaries and definition of the footnote LABEL."
319 (let* ((label (regexp-quote (org-footnote-normalize-label label)))
320 (re (format "^\\[%s\\]\\|.\\[%s:" label label))
323 (when (or (re-search-forward re nil t)
324 (and (goto-char (point-min))
325 (re-search-forward re nil t))
326 (and (progn (widen) t)
327 (goto-char (point-min))
328 (re-search-forward re nil t)))
329 (let ((refp (org-footnote-at-reference-p)))
331 ((and (nth 3 refp) refp))
332 ((org-footnote-at-definition-p))))))))
334 (defun org-footnote-goto-definition (label)
335 "Move point to the definition of the footnote LABEL."
336 (interactive "sLabel: ")
338 (let ((def (org-footnote-get-definition label)))
340 (error "Cannot find definition of footnote %s" label)
341 (goto-char (nth 1 def))
342 (looking-at (format "\\[%s\\]\\|\\[%s:" label label))
343 (goto-char (match-end 0))
344 (org-show-context 'link-search)
345 (message "Edit definition and go back with `C-c &' or, if unique, with `C-c C-c'."))))
347 (defun org-footnote-goto-previous-reference (label)
348 "Find the first closest (to point) reference of footnote with label LABEL."
349 (interactive "sLabel: ")
351 (let* ((label (org-footnote-normalize-label label)) ref)
353 (setq ref (or (org-footnote-get-next-reference label t)
354 (org-footnote-get-next-reference label)
358 (org-footnote-get-next-reference label t)
359 (org-footnote-get-next-reference label))))))
361 (error "Cannot find reference of footnote %s" label)
362 (goto-char (nth 1 ref))
363 (org-show-context 'link-search))))
365 (defun org-footnote-normalize-label (label)
366 "Return LABEL as an appropriate string."
368 ((numberp label) (number-to-string label))
369 ((equal "" label) nil)
370 ((not (string-match "^[0-9]+$\\|^fn:" label))
371 (concat "fn:" label))
374 (defun org-footnote-all-labels (&optional with-defs)
375 "Return list with all defined foot labels used in the buffer.
377 If WITH-DEFS is non-nil, also associate the definition to each
378 label. The function will then return an alist whose key is label
379 and value definition."
383 ;; Depending on WITH-DEFS, store label or (label . def) of
384 ;; footnote reference/definition given as argument in RTN.
386 (let ((lbl (car el)))
387 (push (if with-defs (cons lbl (nth 3 el)) lbl) rtn))))))
391 ;; Find all labels found in definitions.
392 (goto-char (point-min))
394 (while (re-search-forward org-footnote-definition-re nil t)
395 (when (setq def (org-footnote-at-definition-p))
396 (funcall push-to-rtn def))))
397 ;; Find all labels found in references.
398 (goto-char (point-min))
400 (while (setq ref (org-footnote-get-next-reference))
401 (goto-char (nth 2 ref))
402 (and (car ref) ; ignore anonymous footnotes
403 (not (funcall (if with-defs #'assoc #'member) (car ref) rtn))
404 (funcall push-to-rtn ref))))))
407 (defun org-footnote-unique-label (&optional current)
408 "Return a new unique footnote label.
409 The returns the firsts fn:N labels that is currently not used."
410 (unless current (setq current (org-footnote-all-labels)))
411 (let ((fmt (if (eq org-footnote-auto-label 'plain) "%d" "fn:%d"))
413 (while (member (format fmt cnt) current)
417 (defvar org-footnote-label-history nil
418 "History of footnote labels entered in current buffer.")
419 (make-variable-buffer-local 'org-footnote-label-history)
421 (defun org-footnote-new ()
422 "Insert a new footnote.
423 This command prompts for a label. If this is a label referencing an
424 existing label, only insert the label. If the footnote label is empty
425 or new, let the user edit the definition of the footnote."
427 (unless (and (not (bolp)) (org-footnote-in-valid-context-p))
428 (error "Cannot insert a footnote here"))
429 (let* ((labels (and (not (equal org-footnote-auto-label 'random))
430 (org-footnote-all-labels)))
431 (propose (org-footnote-unique-label labels))
433 (org-footnote-normalize-label
435 ((member org-footnote-auto-label '(t plain))
437 ((equal org-footnote-auto-label 'random)
439 (substring (org-id-uuid) 0 8))
442 "Label (leave empty for anonymous): "
443 (mapcar 'list labels) nil nil
444 (if (eq org-footnote-auto-label 'confirm) propose nil)
445 'org-footnote-label-history))))))
450 ((member label labels)
451 (insert "[" label "]")
452 (message "New reference to existing note"))
453 (org-footnote-define-inline
454 (insert "[" label ": ]")
456 (org-footnote-auto-adjust-maybe))
458 (insert "[" label "]")
459 (org-footnote-create-definition label)
460 (org-footnote-auto-adjust-maybe)))))
462 (defun org-footnote-create-definition (label)
463 "Start the definition of a footnote with label LABEL."
464 (interactive "sLabel: ")
465 (let ((label (org-footnote-normalize-label label)))
468 ;; No section, put footnote into the current outline node Try to
469 ;; find or make the special node
470 (when org-footnote-section
471 (goto-char (point-min))
472 (let ((re (concat "^\\*+[ \t]+" org-footnote-section "[ \t]*$")))
473 (unless (or (re-search-forward re nil t)
474 (and (progn (widen) t)
475 (re-search-forward re nil t)))
476 (goto-char (point-max))
477 (insert "\n\n* " org-footnote-section "\n"))))
478 ;; Now go to the end of this entry and insert there.
479 (org-footnote-goto-local-insertion-point)
480 (org-show-context 'link-search))
482 ;; In a non-Org file. Search for footnote tag, or create it if
483 ;; necessary (at the end of buffer, or before a signature if in
484 ;; Message mode). Set point after any definition already there.
485 (let ((tag (concat "^" org-footnote-tag-for-non-org-mode-files "[ \t]*$"))
487 (if (and (derived-mode-p 'message-mode)
489 message-signature-separator nil t))
490 (copy-marker (point-at-bol) t)
491 (copy-marker (point-max) t)))))
493 (unless (re-search-backward tag nil t)
494 (skip-chars-backward " \t\r\n")
495 (delete-region (point) max)
496 (insert "\n\n" org-footnote-tag-for-non-org-mode-files "\n"))
497 ;; Skip existing footnotes.
498 (while (re-search-forward org-footnote-definition-re max t))
499 (let ((def (org-footnote-at-definition-p)))
500 (when def (goto-char (nth 2 def))))
501 (set-marker max nil))))
502 ;; Insert footnote label, position point and notify user.
503 (unless (bolp) (insert "\n"))
504 (insert "\n[" label "] \n")
506 (message "Edit definition and go back with `C-c &' or, if unique, with `C-c C-c'.")))
509 (defun org-footnote-action (&optional special)
510 "Do the right thing for footnotes.
512 When at a footnote reference, jump to the definition.
514 When at a definition, jump to the references if they exist, offer
515 to create them otherwise.
517 When neither at definition or reference, create a new footnote,
520 With prefix arg SPECIAL, offer additional commands in a menu."
525 (message "Footnotes: [s]ort | [r]enumber fn:N | [S]=r+s |->[n]umeric | [d]elete")
526 (setq c (read-char-exclusive))
528 ((eq c ?s) (org-footnote-normalize 'sort))
529 ((eq c ?r) (org-footnote-renumber-fn:N))
531 (org-footnote-renumber-fn:N)
532 (org-footnote-normalize 'sort))
533 ((eq c ?n) (org-footnote-normalize))
534 ((eq c ?d) (org-footnote-delete))
535 (t (error "No such footnote command %c" c))))
536 ((setq tmp (org-footnote-at-reference-p))
538 ;; Anonymous footnote: move point at the beginning of its
541 (goto-char (nth 1 tmp))
543 ;; A definition exists: move to it.
544 ((ignore-errors (org-footnote-goto-definition (car tmp))))
545 ;; No definition exists: offer to create it.
546 ((yes-or-no-p (format "No definition for %s. Create one? " (car tmp)))
547 (org-footnote-create-definition (car tmp)))))
548 ((setq tmp (org-footnote-at-definition-p))
549 (org-footnote-goto-previous-reference (car tmp)))
550 (t (org-footnote-new)))))
552 (defvar org-footnote-insert-pos-for-preprocessor 'point-max
553 "See `org-footnote-normalize'.")
555 (defvar org-export-footnotes-seen nil) ; silence byte-compiler
556 (defvar org-export-footnotes-data nil) ; silence byte-compiler
559 (defun org-footnote-normalize (&optional sort-only export-props)
560 "Collect the footnotes in various formats and normalize them.
562 This finds the different sorts of footnotes allowed in Org, and
563 normalizes them to the usual [N] format that is understood by the
566 When SORT-ONLY is set, only sort the footnote definitions into the
569 If Org is amidst an export process, EXPORT-PROPS will hold the
570 export properties of the buffer.
572 When EXPORT-PROPS is non-nil, the default action is to insert
573 normalized footnotes towards the end of the pre-processing buffer.
574 Some exporters like docbook, odt, etc. expect that footnote
575 definitions be available before any references to them. Such
576 exporters can let bind `org-footnote-insert-pos-for-preprocessor' to
577 symbol 'point-min to achieve the desired behaviour.
579 Additional note on `org-footnote-insert-pos-for-preprocessor':
580 1. This variable has not effect when FOR-PREPROCESSOR is nil.
581 2. This variable (potentially) obviates the need for extra scan
582 of pre-processor buffer as witnessed in
583 `org-export-docbook-get-footnotes'."
584 ;; This is based on Paul's function, but rewritten.
586 ;; Re-create `org-with-limited-levels', but not limited to Org
589 (and (boundp 'org-inlinetask-min-level)
590 org-inlinetask-min-level
591 (1- org-inlinetask-min-level)))
592 (nstars (and limit-level
593 (if org-odd-levels-only
594 (and limit-level (1- (* limit-level 2)))
597 (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))
598 ;; Determine the highest marker used so far.
599 (ref-table (when export-props org-export-footnotes-seen))
600 (count (if (and export-props ref-table)
601 (apply 'max (mapcar (lambda (e) (nth 1 e)) ref-table))
605 ;; 1. Find every footnote reference, extract the definition, and
606 ;; collect that data in REF-TABLE. If SORT-ONLY is nil, also
607 ;; normalize references.
608 (goto-char (point-min))
609 (while (setq ref (org-footnote-get-next-reference))
610 (let* ((lbl (car ref))
611 ;; When footnote isn't anonymous, check if it's label
612 ;; (REF) is already stored in REF-TABLE. In that case,
613 ;; extract number used to identify it (MARKER). If
614 ;; footnote is unknown, increment the global counter
615 ;; (COUNT) to create an unused identifier.
616 (a (and lbl (assoc lbl ref-table)))
617 (marker (or (nth 1 a) (incf count)))
618 ;; Is the reference inline or pointing to an inline
620 (inlinep (or (stringp (nth 3 ref)) (nth 3 a))))
621 ;; Replace footnote reference with [MARKER]. Maybe fill
622 ;; paragraph once done. If SORT-ONLY is non-nil, only move
623 ;; to the end of reference found to avoid matching it twice.
624 ;; If EXPORT-PROPS isn't nil, also add `org-footnote'
625 ;; property to it, so it can be easily recognized by
628 (goto-char (nth 2 ref))
629 (delete-region (nth 1 ref) (nth 2 ref))
630 (goto-char (nth 1 ref))
631 (let ((new-ref (format "[%d]" marker)))
632 (when export-props (org-add-props new-ref '(org-footnote t)))
635 org-footnote-fill-after-inline-note-extraction
636 (org-fill-paragraph)))
637 ;; Add label (REF), identifier (MARKER) and definition (DEF)
638 ;; to REF-TABLE if data was unknown.
640 (let ((def (or (nth 3 ref) ; inline
642 (cdr (assoc lbl org-export-footnotes-data)))
643 (nth 3 (org-footnote-get-definition lbl)))))
644 (push (list lbl marker
645 ;; When exporting, each definition goes
646 ;; through `org-export-preprocess-string' so
647 ;; it is ready to insert in the
648 ;; backend-specific buffer.
653 '(:todo-keywords t :tags t :priority t))))
654 (org-export-preprocess-string def parameters))
656 inlinep) ref-table)))
657 ;; Remove definition of non-inlined footnotes.
658 (unless inlinep (org-footnote-delete-definitions lbl))))
659 ;; 2. Find and remove the footnote section, if any. Also
660 ;; determine where footnotes shall be inserted (INS-POINT).
661 (goto-char (point-min))
664 (if (and org-footnote-section
666 (concat "^\\*[ \t]+" (regexp-quote org-footnote-section)
670 (setq ins-point (match-beginning 0))
671 (delete-region (match-beginning 0) (org-end-of-subtree t)))
672 (setq ins-point (point-max))))
674 (when (re-search-forward
676 (regexp-quote org-footnote-tag-for-non-org-mode-files)
680 ;; In message-mode, ensure footnotes are inserted before the
683 (or (and (derived-mode-p 'message-mode)
685 (goto-char (point-max))
687 message-signature-separator nil t)
691 (skip-chars-backward " \t\n\r")
693 (delete-region (point) pt-max))
694 (setq ins-point (point))))
695 ;; 3. Clean-up REF-TABLE.
701 ;; When only sorting, ignore inline footnotes.
702 ((and sort-only (nth 3 x)) nil)
703 ;; No definition available: provide one.
705 (append (butlast x 2)
706 (list (format "DEFINITION NOT FOUND: %s" (car x))
710 (setq ref-table (nreverse ref-table))
711 ;; 4. Insert the footnotes again in the buffer, at the
715 (eq org-footnote-insert-pos-for-preprocessor 'point-min)
720 ;; No footnote: exit.
722 ;; Cases when footnotes should be inserted in one place.
723 ((or (not (org-mode-p))
726 ;; Insert again the section title.
729 (insert "\n\n" org-footnote-tag-for-non-org-mode-files "\n"))
730 ((and org-footnote-section (not export-props))
731 (or (bolp) (insert "\n"))
732 (insert "* " org-footnote-section "\n")))
733 ;; Insert the footnotes.
735 (mapconcat (lambda (x) (format "[%s] %s"
736 (nth (if sort-only 0 1) x) (nth 2 x)))
739 ;; When exporting, add newly inserted markers along with their
740 ;; associated definition to `org-export-footnotes-seen'.
742 (setq org-export-footnotes-seen ref-table)))
743 ;; Else, insert each definition at the end of the section
744 ;; containing their first reference. Happens only in Org files
745 ;; with no special footnote section, and only when doing
747 (t (mapc 'org-insert-footnote-reference-near-definition
750 (defun org-insert-footnote-reference-near-definition (entry)
751 "Find first reference of footnote ENTRY and insert the definition there.
752 ENTRY is (fn-label num-mark definition)."
754 (goto-char (point-min))
755 (let ((ref (org-footnote-get-next-reference (car entry))))
757 (goto-char (nth 2 ref))
758 (org-footnote-goto-local-insertion-point)
759 (insert (format "\n[%s] %s\n" (car entry) (nth 2 entry)))))))
761 (defun org-footnote-goto-local-insertion-point ()
762 "Find insertion point for footnote, just before next outline heading."
763 (org-with-limited-levels (outline-next-heading))
764 (or (bolp) (newline))
765 (beginning-of-line 0)
766 (while (and (not (bobp)) (= (char-after) ?#))
767 (beginning-of-line 0))
768 (if (looking-at "[ \t]*#\\+TBLFM:") (beginning-of-line 2))
770 (skip-chars-backward "\n\r\t ")
773 (defun org-footnote-delete-references (label)
774 "Delete every reference to footnote LABEL.
775 Return the number of footnotes removed."
777 (goto-char (point-min))
779 (while (setq ref (org-footnote-get-next-reference label))
780 (goto-char (nth 1 ref))
781 (delete-region (nth 1 ref) (nth 2 ref))
785 (defun org-footnote-delete-definitions (label)
786 "Delete every definition of the footnote LABEL.
787 Return the number of footnotes removed."
789 (goto-char (point-min))
790 (let ((def-re (concat "^\\[" (regexp-quote label) "\\]"))
792 (while (re-search-forward def-re nil t)
793 (let ((full-def (org-footnote-at-definition-p)))
794 (delete-region (nth 1 full-def) (nth 2 full-def)))
798 (defun org-footnote-delete (&optional label)
799 "Delete the footnote at point.
800 This will remove the definition (even multiple definitions if they exist)
801 and all references of a footnote label.
803 If LABEL is non-nil, delete that footnote instead."
805 (let* ((nref 0) (ndef 0) x
806 ;; 1. Determine LABEL of footnote at point.
808 ;; LABEL is provided as argument.
810 ;; Footnote reference at point. If the footnote is
811 ;; anonymous, delete it and exit instead.
812 ((setq x (org-footnote-at-reference-p))
815 (delete-region (nth 1 x) (nth 2 x))
816 (message "Anonymous footnote removed")
818 ;; Footnote definition at point.
819 ((setq x (org-footnote-at-definition-p))
821 (t (error "Don't know which footnote to remove")))))
822 ;; 2. Now that LABEL is non-nil, find every reference and every
823 ;; definition, and delete them.
824 (setq nref (org-footnote-delete-references label)
825 ndef (org-footnote-delete-definitions label))
826 ;; 3. Verify consistency of footnotes and notify user.
827 (org-footnote-auto-adjust-maybe)
828 (message "%d definition(s) of and %d reference(s) of footnote %s removed"
831 (defun org-footnote-renumber-fn:N ()
832 "Renumber the simple footnotes like fn:17 into a sequence in the document."
838 (goto-char (point-min))
839 (while (re-search-forward "\\[fn:\\([0-9]+\\)[]:]" nil t)
840 (setq i (string-to-number (match-string 1)))
841 (when (and (string-match "\\S-" (buffer-substring
842 (point-at-bol) (match-beginning 0)))
844 (push (cons i (number-to-string (incf n))) map)))
845 (goto-char (point-min))
846 (while (re-search-forward "\\(\\[fn:\\)\\([0-9]+\\)\\([]:]\\)" nil t)
847 (replace-match (concat "\\1" (cdr (assq (string-to-number (match-string 2)) map)) "\\3")))))))
849 (defun org-footnote-auto-adjust-maybe ()
850 "Renumber and/or sort footnotes according to user settings."
851 (when (memq org-footnote-auto-adjust '(t renumber))
852 (org-footnote-renumber-fn:N))
853 (when (memq org-footnote-auto-adjust '(t sort))
854 (let ((label (car (org-footnote-at-definition-p))))
855 (org-footnote-normalize 'sort)
857 (goto-char (point-min))
858 (and (re-search-forward (concat "^\\[" (regexp-quote label) "\\]")
861 (just-one-space)))))))
863 (provide 'org-footnote)
867 ;;; org-footnote.el ends here