1 ;;; org-footnote.el --- Footnote support in Org and elsewhere
3 ;; Copyright (C) 2009 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-in-commented-line
"org" ())
42 (declare-function org-in-regexp
"org" (re &optional nlines visually
))
43 (declare-function org-mark-ring-push
"org" (&optional pos buffer
))
44 (declare-function outline-next-heading
"outline")
45 (declare-function org-trim
"org" (s))
46 (declare-function org-show-context
"org" (&optional key
))
47 (declare-function org-back-to-heading
"org" (&optional invisible-ok
))
48 (declare-function org-end-of-subtree
"org" (&optional invisible-ok to-heading
))
49 (declare-function org-in-verbatim-emphasis
"org" ())
50 (defvar org-odd-levels-only
) ;; defined in org.el
52 (defconst org-footnote-re
53 (concat "[^][\n]" ; to make sure it is not at the beginning of a line
58 (org-re "\\(fn:\\([-_[:word:]]+?\\)?\\)\\(?::\\([^\]]*?\\)\\)?")
61 "Regular expression for matching footnotes.")
63 (defconst org-footnote-definition-re
64 (org-re "^\\(\\[\\([0-9]+\\|fn:[-_[:word:]]+\\)\\]\\)")
65 "Regular expression matching the definition of a footnote.")
67 (defcustom org-footnote-section
"Footnotes"
68 "Outline heading containing footnote definitions before export.
69 This can be nil, to place footnotes locally at the end of the current
70 outline node. If can also be the name of a special outline heading
71 under which footnotes should be put.
72 This variable defines the place where Org puts the definition
73 automatically, i.e. when creating the footnote, and when sorting the notes.
74 However, by hand you may place definitions *anywhere*.
75 If this is a string, during export, all subtrees starting with this
76 heading will be removed after extracting footnote definitions."
79 (string :tag
"Collect fotnotes under heading")
80 (const :tag
"Define footnotes locally" nil
)))
82 (defcustom org-footnote-tag-for-non-org-mode-files
"Footnotes:"
83 "Tag marking the beginning of footnote section.
84 The Org-mode footnote engine can be used in arbitrary text files as well
85 as in Org-mode. Outside Org-mode, new footnotes are always placed at
86 the end of the file. When you normalize the notes, any line containing
87 only this tag will be removed, a new one will be inserted at the end
88 of the file, followed by the collected and normalized footnotes."
92 (defcustom org-footnote-define-inline nil
93 "Non-nil means, define footnotes inline, at reference location.
94 When nil, footnotes will be defined in a special section near
95 the end of the document. When t, the [fn:label:definition] notation
96 will be used to define the footnote at the reference position."
100 (defcustom org-footnote-auto-label t
101 "Non-nil means, define automatically new labels for footnotes.
104 nil prompt the user for each label
105 t create unique labels of the form [fn:1], [fn:2], ...
106 confirm like t, but let the user edit the created value. In particular,
107 the label can be removed from the minibuffer, to create
108 an anonymous footnote.
109 plain Automatically create plain number labels like [1]"
112 (const :tag
"Frompt for label" nil
)
113 (const :tag
"Create automatic [fn:N]" t
)
114 (const :tag
"Offer automatic [fn:N] for editing" confirm
)
115 (const :tag
"Create automatic [N]" plain
)))
117 (defcustom org-footnote-auto-adjust nil
118 "Non-nil means, automatically adjust footnotes after insert/delete.
119 When this is t, after each insertion or deletion of a footnote,
120 simple fn:N footnotes will be renumbered, and all footnotes will be sorted.
121 If you want to have just sorting or just renumbering, set this variable
122 to `sort' or `renumber'.
124 The main values of this variable can be set with in-buffer options:
127 #+STARTUP: nofnadjust"
130 (const :tag
"Renumber" renumber
)
131 (const :tag
"Sort" sort
)
132 (const :tag
"Renumber and Sort" t
)))
134 (defcustom org-footnote-fill-after-inline-note-extraction nil
135 "Non-nil means, fill paragraphs after extracting footnotes.
136 When extracting inline footnotes, the lengths of lines can change a lot.
137 When this option is set, paragraphs from which an inline footnote has been
138 extracted will be filled again."
142 (defun org-footnote-at-reference-p ()
143 "Is the cursor at a footnote reference?
144 If yes, return the beginning position, the label, and the definition, if local."
145 (when (org-in-regexp org-footnote-re
15)
146 (list (match-beginning 0)
148 (if (equal (match-string 2) "fn:") nil
(match-string 2)))
151 (defun org-footnote-at-definition-p ()
152 "Is the cursor at a footnote definition.
153 This matches only pure definitions like [1] or [fn:name] at the beginning
154 of a line. It does not a references like [fn:name:definition], where the
155 footnote text is included and defined locally.
156 The return value will be nil if not at a footnote definition, and a list
157 with start and label of the footnote if there is a definition at point."
160 (let ((lim (save-excursion (re-search-backward "^\\*+ \\|^[ \t]*$" nil t
))))
161 (when (re-search-backward org-footnote-definition-re lim t
)
162 (list (match-beginning 0) (match-string 2))))))
164 (defun org-footnote-goto-definition (label)
165 "Find the definition of the footnote with label LABEL."
166 (interactive "sLabel: ")
168 (setq label
(org-footnote-normalize-label label
))
169 (let ((re (format "^\\[%s\\]\\|.\\[%s:" label label
))
172 (setq pos
(or (re-search-forward re nil t
)
173 (and (goto-char (point-min))
174 (re-search-forward re nil t
))
175 (and (progn (widen) t
)
176 (goto-char (point-min))
177 (re-search-forward re nil t
)))))
179 (error "Cannot find definition of footnote %s" label
)
181 (org-show-context 'link-search
)
182 (message "Edit definition and go back with `C-c &' or, if unique, with `C-c C-c'."))))
184 (defun org-footnote-goto-next-reference (label)
185 "Find the definition of the footnote with label LABEL."
186 (interactive "sLabel: ")
188 (setq label
(org-footnote-normalize-label label
))
189 (let ((re (format ".\\[%s[]:]" label
))
192 (setq pos
(or (re-search-forward re nil t
)
193 (and (goto-char (point-min))
194 (re-search-forward re nil t
))
195 (and (progn (widen) t
)
197 (re-search-forward re nil t
))
198 (and (goto-char (point-min))
199 (re-search-forward re nil t
)))))
203 (org-show-context 'link-search
))
204 (error "Cannot find reference of footnote %s" label
))))
206 (defun org-footnote-normalize-label (label)
207 (if (numberp label
) (setq label
(number-to-string label
)))
208 (if (not (string-match "^[0-9]+$\\|^$\\|^fn:" label
))
209 (setq label
(concat "fn:" label
)))
212 (defun org-footnote-all-labels ()
213 "Return list with all defined foot labels used in the buffer."
218 (goto-char (point-min))
219 (while (re-search-forward org-footnote-definition-re nil t
)
220 (setq l
(org-match-string-no-properties 2))
221 (and l
(add-to-list 'rtn l
)))
222 (goto-char (point-min))
223 (while (re-search-forward org-footnote-re nil t
)
224 (setq l
(or (org-match-string-no-properties 1)
225 (org-match-string-no-properties 2)))
226 (and l
(not (equal l
"fn:")) (add-to-list 'rtn l
)))))
229 (defun org-footnote-unique-label (&optional current
)
230 "Return a new unique footnote label.
231 The returns the firsts fn:N labels that is currently not used."
232 (unless current
(setq current
(org-footnote-all-labels)))
233 (let ((fmt (if (eq org-footnote-auto-label
'plain
) "%d" "fn:%d"))
235 (while (member (format fmt cnt
) current
)
239 (defvar org-footnote-label-history nil
240 "History of footnote labels entered in current buffer.")
241 (make-variable-buffer-local 'org-footnote-label-history
)
243 (defun org-footnote-new ()
244 "Insert a new footnote.
245 This command prompts for a label. If this is a label referencing an
246 existing label, only insert the label. If the footnote label is empty
247 or new, let the user edit the definition of the footnote."
249 (let* ((labels (org-footnote-all-labels))
250 (propose (org-footnote-unique-label labels
))
252 (if (member org-footnote-auto-label
'(t plain
))
255 "Label (leave empty for anonymous): "
256 (mapcar 'list labels
) nil nil
257 (if (eq org-footnote-auto-label
'confirm
) propose nil
)
258 'org-footnote-label-history
))))
259 (setq label
(org-footnote-normalize-label label
))
264 ((member label labels
)
265 (insert "[" label
"]")
266 (message "New reference to existing note"))
267 (org-footnote-define-inline
268 (insert "[" label
": ]")
270 (org-footnote-auto-adjust-maybe))
272 (insert "[" label
"]")
273 (org-footnote-create-definition label
)
274 (org-footnote-auto-adjust-maybe)))))
276 (defun org-footnote-create-definition (label)
277 "Start the definition of a footnote with label LABEL."
278 (interactive "sLabel: ")
279 (setq label
(org-footnote-normalize-label label
))
283 (if (not org-footnote-section
)
284 ;; No section, put footnote into the current outline node
286 ;; Try to find or make the special node
287 (setq re
(concat "^\\*+[ \t]+" org-footnote-section
"[ \t]*$"))
288 (unless (or (re-search-forward re nil t
)
289 (and (progn (widen) t
)
290 (re-search-forward re nil t
)))
291 (goto-char (point-max))
292 (insert "\n\n* " org-footnote-section
"\n")))
293 ;; Now go to the end of this entry and insert there.
294 (org-footnote-goto-local-insertion-point)
295 (org-show-context 'link-search
))
297 (setq re
(concat "^" org-footnote-tag-for-non-org-mode-files
"[ \t]*$"))
298 (unless (re-search-forward re nil t
)
299 (goto-char (point-max))
300 (skip-chars-backward " \t\r\n")
302 (delete-region (point) (point-max))
303 (insert org-footnote-tag-for-non-org-mode-files
"\n"))
304 (goto-char (point-max))
305 (skip-chars-backward " \t\r\n")))
307 (insert "[" label
"] ")
308 (message "Edit definition and go back with `C-c &' or, if unique, with `C-c C-c'.")))
311 (defun org-footnote-action (&optional special
)
312 "Do the right thing for footnotes.
313 When at a footnote reference, jump to the definition. When at a definition,
314 jump to the refernces. When neither at definition or reference,
315 create a new footnote, interactively.
316 With prefix arg SPECIAL, offer additional commands in a menu."
321 (message "Footnotes: [s]ort | [r]enumber fn:N | [S]=r+s |->[n]umeric | [d]elete")
322 (setq c
(read-char-exclusive))
325 (org-footnote-normalize 'sort
))
327 (org-footnote-renumber-fn:N
))
329 (org-footnote-renumber-fn:N
)
330 (org-footnote-normalize 'sort
))
332 (org-footnote-normalize))
334 (org-footnote-delete))
335 (t (error "No such footnote command %c" c
))))
336 ((setq tmp
(org-footnote-at-reference-p))
338 (org-footnote-goto-definition (nth 1 tmp
))
339 (goto-char (match-beginning 4))))
340 ((setq tmp
(org-footnote-at-definition-p))
341 (org-footnote-goto-next-reference (nth 1 tmp
)))
342 (t (org-footnote-new)))))
345 (defun org-footnote-normalize (&optional sort-only for-preprocessor
)
346 "Collect the footnotes in various formats and normalize them.
347 This finds the different sorts of footnotes allowed in Org, and
348 normalizes them to the usual [N] format that is understood by the
350 When SORT-ONLY is set, only sort the footnote definitions into the
351 referenced sequence."
352 ;; This is based on Paul's function, but rewritten.
354 (and (boundp 'org-inlinetask-min-level
)
355 org-inlinetask-min-level
356 (1- org-inlinetask-min-level
)))
357 (nstars (and limit-level
358 (if org-odd-levels-only
359 (and limit-level
(1- (* limit-level
2)))
362 (concat "\\*" (if nstars
(format "\\{1,%d\\} " nstars
) "+ ")))
364 ref def idef ref-table beg beg1 marker a before ins-point
)
366 ;; Now find footnote references, and extract the definitions
367 (goto-char (point-min))
368 (while (re-search-forward org-footnote-re nil t
)
369 (unless (or (org-in-commented-line) (org-in-verbatim-emphasis))
371 (setq def
(match-string 4)
373 ref
(or (match-string 1) (match-string 2))
374 before
(char-to-string (char-after (match-beginning 0))))
375 (if (equal ref
"fn:") (setq ref nil
))
376 (if (and ref
(setq a
(assoc ref ref-table
)))
378 (setq marker
(nth 1 a
))
379 (unless (nth 2 a
) (setf (caddr a
) def
)))
380 (setq marker
(number-to-string (incf count
))))
383 (setq def
(org-trim def
))
385 (goto-char (point-min))
386 (if (not (re-search-forward (concat "^\\[" (regexp-quote ref
)
389 (setq beg
(match-beginning 0))
390 (setq beg1
(match-end 0))
392 (org-re "^[ \t]*$\\|^\\*+ \\|^\\[\\([0-9]+\\|fn:[-_[:word:]]+\\)\\]")
394 (setq def
(buffer-substring beg1
(or (match-beginning 0)
397 (skip-chars-backward " \t\n\t")
398 (delete-region (1+ (point)) (match-beginning 0))))))
400 (replace-match (concat before
"[" marker
"]"))
402 org-footnote-fill-after-inline-note-extraction
404 (if (not a
) (push (list ref marker def
(if idef t nil
))
407 ;; First find and remove the footnote section
408 (goto-char (point-min))
411 (if (and org-footnote-section
413 (concat "^\\*[ \t]+" (regexp-quote org-footnote-section
)
416 (if (or for-preprocessor
(not org-footnote-section
))
418 (org-back-to-heading t
)
420 (setq ins-point
(point))
421 (delete-region (point) (org-end-of-subtree t
)))
422 (goto-char (point-max))
423 (unless for-preprocessor
424 (when org-footnote-section
425 (or (bolp) (insert "\n"))
426 (insert "* " org-footnote-section
"\n")
427 (setq ins-point
(point))))))
429 (if (re-search-forward
431 (regexp-quote org-footnote-tag-for-non-org-mode-files
)
435 (goto-char (point-max))
436 (skip-chars-backward " \t\n\r")
437 (delete-region (point) (point-max))
438 (insert "\n\n" org-footnote-tag-for-non-org-mode-files
"\n")
439 (setq ins-point
(point))))
441 ;; Insert the footnotes again
442 (goto-char (or ins-point
(point-max)))
443 (setq ref-table
(reverse ref-table
))
445 ;; remove anonymous and inline footnotes from the list
448 (lambda (x) (and (car x
)
449 (not (equal (car x
) "fn:"))
453 ;; Make sure each footnote has a description, or an error message.
459 (format "FOOTNOTE DEFINITION NOT FOUND: %s" (car x
)))
460 (setcar (cddr x
) (org-trim (nth 2 x
))))
464 (if (or (not (org-mode-p)) ; not an Org file
465 org-footnote-section
; we do not use a footnote section
466 (not sort-only
) ; this is normalization
467 for-preprocessor
) ; the is the preprocessor
468 ;; Insert the footnotes together in one place
473 (format "[%s] %s" (nth (if sort-only
0 1) x
)
474 (org-trim (nth 2 x
))))
476 (if ref-table
(insert "\n" def
"\n\n")))
477 ;; Insert each footnote near the first reference
478 ;; Happens only in Org files with no special footnote section,
479 ;; and only when doing sorting
480 (mapc 'org-insert-footnote-reference-near-definition
483 (defun org-insert-footnote-reference-near-definition (entry)
484 "Find first reference of footnote ENTRY and insert the definition there.
485 ENTRY is (fn-label num-mark definition)."
487 (goto-char (point-min))
488 (when (re-search-forward (format ".\\[%s[]:]" (regexp-quote (car entry
)))
490 (org-footnote-goto-local-insertion-point)
491 (insert (format "\n\n[%s] %s" (car entry
) (nth 2 entry
))))))
493 (defun org-footnote-goto-local-insertion-point ()
494 "Find insertion point for footnote, just before next outline heading."
495 (org-with-limited-levels (outline-next-heading))
496 (or (bolp) (newline))
497 (beginning-of-line 0)
498 (while (and (not (bobp)) (= (char-after) ?
#))
499 (beginning-of-line 0))
500 (if (looking-at "[ \t]*#\\+TBLFM:") (beginning-of-line 2))
502 (skip-chars-backward "\n\r\t "))
504 (defun org-footnote-delete (&optional label
)
505 "Delete the footnote at point.
506 This will remove the definition (even multiple definitions if they exist)
507 and all references of a footnote label."
509 (let (x label l beg def-re
(nref 0) (ndef 0))
511 (when (setq x
(org-footnote-at-reference-p))
512 (setq label
(nth 1 x
))
513 (when (or (not label
) (equal "fn:" label
))
514 (delete-region (1+ (match-beginning 0)) (match-end 0))
515 (message "Anonymous footnote removed")
517 (when (and (not label
) (setq x
(org-footnote-at-definition-p)))
518 (setq label
(nth 1 x
)))
519 (unless label
(error "Don't know which footnote to remove")))
522 (goto-char (point-min))
523 (while (re-search-forward org-footnote-re nil t
)
524 (setq l
(or (match-string 1) (match-string 2)))
525 (when (equal l label
)
526 (delete-region (1+ (match-beginning 0)) (match-end 0))
528 (goto-char (point-min))
529 (setq def-re
(concat "^\\[" (regexp-quote label
) "\\]"))
530 (while (re-search-forward def-re nil t
)
531 (setq beg
(match-beginning 0))
532 (if (re-search-forward "^\\[\\|^[ \t]*$\\|^\\*+ " nil t
)
533 (goto-char (match-beginning 0))
534 (goto-char (point-max)))
535 (delete-region beg
(point))
537 (org-footnote-auto-adjust-maybe)
538 (message "%d definition(s) of and %d reference(s) of footnote %s removed"
541 (defun org-footnote-renumber-fn:N
()
542 "Renumber the simple footnotes like fn:17 into a sequence in the document."
548 (goto-char (point-min))
549 (while (re-search-forward "\\[fn:\\([0-9]+\\)[]:]" nil t
)
550 (setq i
(string-to-number (match-string 1)))
551 (when (and (string-match "\\S-" (buffer-substring
552 (point-at-bol) (match-beginning 0)))
554 (push (cons i
(number-to-string (incf n
))) map
)))
555 (goto-char (point-min))
556 (while (re-search-forward "\\(\\[fn:\\)\\([0-9]+\\)\\([]:]\\)" nil t
)
557 (replace-match (concat "\\1" (cdr (assq (string-to-number (match-string 2)) map
)) "\\3")))))))
559 (defun org-footnote-auto-adjust-maybe ()
560 "Renumber and/or sort footnotes according to user settings."
561 (when (memq org-footnote-auto-adjust
'(t renumber
))
562 (org-footnote-renumber-fn:N
))
563 (when (memq org-footnote-auto-adjust
'(t sort
))
564 (let ((label (nth 1 (org-footnote-at-definition-p))))
565 (org-footnote-normalize 'sort
)
567 (goto-char (point-min))
568 (and (re-search-forward (concat "^\\[" (regexp-quote label
) "\\]")
571 (just-one-space)))))))
573 (provide 'org-footnote
)
575 ;; arch-tag: 1b5954df-fb5d-4da5-8709-78d944dbfc37
577 ;;; org-footnote.el ends here