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-regexp
"org" (re &optional nlines visually
))
42 (declare-function org-mark-ring-push
"org" (&optional pos buffer
))
43 (declare-function outline-next-heading
"outline")
44 (declare-function org-trim
"org" (s))
45 (declare-function org-show-context
"org" (&optional key
))
46 (declare-function org-back-to-heading
"org" (&optional invisible-ok
))
47 (declare-function org-end-of-subtree
"org" (&optional invisible-ok to-heading
))
49 (defconst org-footnote-re
50 (concat "[^][\n]" ; to make sure it is not at the beginning of a line
55 (org-re "\\(fn:\\([-_[:word:]]+?\\)?\\)\\(?::\\([^\]]*?\\)\\)?")
58 "Regular expression for matching footnotes.")
60 (defconst org-footnote-definition-re
61 (org-re "^\\(\\[\\([0-9]+\\|fn:[-_[:word:]]+\\)\\]\\)")
62 "Regular expression matching the definition of a footnote.")
64 (defcustom org-footnote-section
"Footnotes"
65 "Outline heading containing footnote definitions before export.
66 This can be nil, to place footnotes locally at the end of the current
67 outline node. If can also be the name of a special outline heading
68 under which footnotes should be put.
69 This variable defines the place where Org puts the definition
70 automatically, i.e. when creating the footnote, and when sorting the notes.
71 However, by hand you may place definitions *anywhere*.
72 If this is a string, during export, all subtrees starting with this
73 heading will be removed after extracting footnote definitions."
76 (string :tag
"Collect fotnotes under heading")
77 (const :tag
"Define footnotes locally" nil
)))
79 (defcustom org-footnote-tag-for-non-org-mode-files
"Footnotes:"
80 "Tag marking the beginning of footnote section.
81 The Org-mode footnote engine can be used in arbitrary text files as well
82 as in Org-mode. Outside Org-mode, new footnotes are always placed at
83 the end of the file. When you normalize the notes, any line containing
84 only this tag will be removed, a new one will be inserted at the end
85 of the file, followed by the collected and normalized footnotes."
89 (defcustom org-footnote-define-inline nil
90 "Non-nil means, define footnotes inline, at reference location.
91 When nil, footnotes will be defined in a special section near
92 the end of the document. When t, the [fn:label:definition] notation
93 will be used to define the footnote at the reference position."
97 (defcustom org-footnote-auto-label t
98 "Non-nil means, define automatically new labels for footnotes.
101 nil prompt the user for each label
102 t create unique labels of the form [fn:1], [fn:2], ...
103 confirm like t, but let the user edit the created value. In particular,
104 the label can be removed from the minibuffer, to create
105 an anonymous footnote.
106 plain Automatically create plain number labels like [1]"
109 (const :tag
"Frompt for label" nil
)
110 (const :tag
"Create automatic [fn:N]" t
)
111 (const :tag
"Offer automatic [fn:N] for editing" confirm
)
112 (const :tag
"Create automatic [N]" plain
)))
114 (defcustom org-footnote-fill-after-inline-note-extraction nil
115 "Non-nil means, fill paragraphs after extracting footnotes.
116 When extracting inline footnotes, the lengths of lines can change a lot.
117 When this option is set, paragraphs from which an inline footnote has been
118 extracted will be filled again."
122 (defun org-footnote-at-reference-p ()
123 "Is the cursor at a footnote reference?
124 If yes, return the beginning position, the label, and the definition, if local."
125 (when (org-in-regexp org-footnote-re
15)
126 (list (match-beginning 0)
128 (if (equal (match-string 2) "fn:") nil
(match-string 2)))
131 (defun org-footnote-at-definition-p ()
132 "Is the cursor at a footnote definition.
133 This matches only pure definitions like [1] or [fn:name] at the beginning
134 of a line. It does not a references like [fn:name:definition], where the
135 footnote text is included and defined locally.
136 The return value will be nil if not at a footnote definition, and a list
137 with start and label of the footnote if there is a definition at point."
140 (let ((lim (save-excursion (re-search-backward "^\\*+ \\|^[ \t]*$" nil t
))))
141 (when (re-search-backward org-footnote-definition-re lim t
)
142 (list (match-beginning 0) (match-string 2))))))
144 (defun org-footnote-goto-definition (label)
145 "Find the definition of the footnote with label LABEL."
146 (interactive "sLabel: ")
148 (setq label
(org-footnote-normalize-label label
))
149 (let ((re (format "^\\[%s\\]\\|.\\[%s:" label label
))
152 (setq pos
(or (re-search-forward re nil t
)
153 (and (goto-char (point-min))
154 (re-search-forward re nil t
))
155 (and (progn (widen) t
)
156 (goto-char (point-min))
157 (re-search-forward re nil t
)))))
159 (error "Cannot find definition of footnote %s" label
)
161 (org-show-context 'link-search
)
162 (message "Edit definition and go back with `C-c &' or, if unique, with `C-c C-c'."))))
164 (defun org-footnote-goto-next-reference (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[]:]" 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
)
177 (re-search-forward re nil t
))
178 (and (goto-char (point-min))
179 (re-search-forward re nil t
)))))
183 (org-show-context 'link-search
))
184 (error "Cannot find reference of footnote %s" label
))))
186 (defun org-footnote-normalize-label (label)
187 (if (numberp label
) (setq label
(number-to-string label
)))
188 (if (not (string-match "^[0-9]+$\\|^$\\|^fn:" label
))
189 (setq label
(concat "fn:" label
)))
192 (defun org-footnote-all-labels ()
193 "Return list with all defined foot labels used in the buffer."
198 (goto-char (point-min))
199 (while (re-search-forward org-footnote-definition-re nil t
)
200 (setq l
(org-match-string-no-properties 2))
201 (and l
(add-to-list 'rtn l
)))
202 (goto-char (point-min))
203 (while (re-search-forward org-footnote-re nil t
)
204 (setq l
(or (org-match-string-no-properties 1)
205 (org-match-string-no-properties 2)))
206 (and l
(not (equal l
"fn:")) (add-to-list 'rtn l
)))))
209 (defun org-footnote-unique-label (&optional current
)
210 "Return a new unique footnote label.
211 The returns the firsts fn:N labels that is currently not used."
212 (unless current
(setq current
(org-footnote-all-labels)))
213 (let ((fmt (if (eq org-footnote-auto-label
'plain
) "%d" "fn:%d"))
215 (while (member (format fmt cnt
) current
)
219 (defvar org-footnote-label-history nil
220 "History of footnote labels entered in current buffer.")
221 (make-variable-buffer-local 'org-footnote-label-history
)
223 (defun org-footnote-new ()
224 "Insert a new footnote.
225 This command prompts for a label. If this is a label referencing an
226 existing label, only insert the label. If the footnote label is empty
227 or new, let the user edit the definition of the footnote."
229 (let* ((labels (org-footnote-all-labels))
230 (propose (org-footnote-unique-label labels
))
232 (if (member org-footnote-auto-label
'(t plain
))
235 "Label (leave empty for anonymous): "
236 (mapcar 'list labels
) nil nil
237 (if (eq org-footnote-auto-label
'confirm
) propose nil
)
238 'org-footnote-label-history
))))
239 (setq label
(org-footnote-normalize-label label
))
244 ((member label labels
)
245 (insert "[" label
"]")
246 (message "New reference to existing note"))
247 (org-footnote-define-inline
248 (insert "[" label
": ]")
251 (insert "[" label
"]")
252 (org-footnote-create-definition label
)))))
254 (defun org-footnote-create-definition (label)
255 "Start the definition of a footnote with label LABEL."
256 (interactive "sLabel: ")
257 (setq label
(org-footnote-normalize-label label
))
261 (if (not org-footnote-section
)
262 ;; No section, put footnote into the current outline node
264 ;; Try to find or make the special node
265 (setq re
(concat "^\\*+[ \t]+" org-footnote-section
"[ \t]*$"))
266 (unless (or (re-search-forward re nil t
)
267 (and (progn (widen) t
)
268 (re-search-forward re nil t
)))
269 (goto-char (point-max))
270 (insert "\n\n* " org-footnote-section
"\n")))
271 ;; Now go to the end of this entry and insert there.
272 (org-footnote-goto-local-insertion-point))
274 (setq re
(concat "^" org-footnote-tag-for-non-org-mode-files
"[ \t]*$"))
275 (unless (re-search-forward re nil t
)
276 (goto-char (point-max))
277 (skip-chars-backward " \t\r\n")
279 (delete-region (point) (point-max))
280 (insert org-footnote-tag-for-non-org-mode-files
"\n"))
281 (goto-char (point-max))
282 (skip-chars-backward " \t\r\n")))
284 (insert "[" label
"] ")
285 (message "Edit definition and go back with `C-c &' or, if unique, with `C-c C-c'.")))
288 (defun org-footnote-action (&optional special
)
289 "Do the right thing for footnotes.
290 When at a footnote reference, jump to the definition. When at a definition,
291 jump to the refernces. When neither at definition or reference,
292 create a new footnote, interactively.
293 With prefix arg SPECIAL, offer additional commands in a menu."
298 (message "Footnotes: [s]ort | convert to [n]umeric | [d]elete")
299 (setq c
(read-char-exclusive))
302 (org-footnote-normalize 'sort
))
304 (org-footnote-normalize))
306 (org-footnote-delete))
307 (t (error "No such footnote command %c" c
))))
308 ((setq tmp
(org-footnote-at-reference-p))
310 (org-footnote-goto-definition (nth 1 tmp
))
311 (goto-char (match-beginning 4))))
312 ((setq tmp
(org-footnote-at-definition-p))
313 (org-footnote-goto-next-reference (nth 1 tmp
)))
314 (t (org-footnote-new)))))
317 (defun org-footnote-normalize (&optional sort-only for-preprocessor
)
318 "Collect the footnotes in various formats and normalize them.
319 This find the different sorts of footnotes allowed in Org, and
320 normalizes them to the usual [N] format that is understood by the
322 When SORT-ONLY is set, only sort the footnote definitions into the
323 referenced sequence."
324 ;; This is based on Paul's function, but rewritten.
325 (let ((count 0) ref def idef ref-table beg beg1 marker a before
328 ;; Now find footnote references, and extract the definitions
329 (goto-char (point-min))
330 (while (re-search-forward org-footnote-re nil t
)
332 (setq def
(match-string 4)
334 ref
(or (match-string 1) (match-string 2))
335 before
(char-to-string (char-after (match-beginning 0))))
336 (if (equal ref
"fn:") (setq ref nil
))
337 (if (and ref
(setq a
(assoc ref ref-table
)))
339 (setq marker
(nth 1 a
))
340 (unless (nth 2 a
) (setf (caddr a
) def
)))
341 (setq marker
(number-to-string (incf count
))))
344 (setq def
(org-trim def
))
346 (goto-char (point-min))
347 (if (not (re-search-forward (concat "^\\[" (regexp-quote ref
)
350 (setq beg
(match-beginning 0))
351 (setq beg1
(match-end 0))
353 (org-re "^[ \t]*$\\|^\\*+ \\|^\\[\\([0-9]+\\|fn:[-_[:word:]]+\\)\\]")
355 (setq def
(buffer-substring beg1
(or (match-beginning 0)
358 (skip-chars-backward " \t\n\t")
359 (delete-region (1+ (point)) (match-beginning 0))))))
361 (replace-match (concat before
"[" marker
"]"))
363 org-footnote-fill-after-inline-note-extraction
365 (if (not a
) (push (list ref marker def
) ref-table
))))
367 ;; First find and remove the footnote section
368 (goto-char (point-min))
371 (if (and org-footnote-section
373 (concat "^\\*[ \t]+" (regexp-quote org-footnote-section
)
376 (if (or for-preprocessor
(not org-footnote-section
))
378 (org-back-to-heading t
)
380 (setq ins-point
(point))
381 (delete-region (point) (org-end-of-subtree t
)))
382 (goto-char (point-max))
383 (unless for-preprocessor
384 (when org-footnote-section
385 (or (bolp) (insert "\n"))
386 (insert "* " org-footnote-section
"\n")
387 (setq ins-point
(point))))))
389 (if (re-search-forward
391 (regexp-quote org-footnote-tag-for-non-org-mode-files
)
395 (goto-char (point-max))
396 (skip-chars-backward " \t\n\r")
397 (delete-region (point) (point-max))
398 (insert "\n\n" org-footnote-tag-for-non-org-mode-files
"\n")
399 (setq ins-point
(point))))
401 ;; Insert the footnotes again
402 (goto-char (or ins-point
(point-max)))
403 (setq ref-table
(reverse ref-table
))
405 ;; remove anonymous fotnotes from the list
408 (lambda (x) (and (car x
)
409 (not (equal (car x
) "fn:"))
412 ;; Make sure each footnote has a description, or an error message.
418 (format "FOOTNOTE DEFINITION NOT FOUND: %s" (car x
)))
419 (setcar (cddr x
) (org-trim (nth 2 x
))))
423 (if (or (not (org-mode-p)) ; not an Org file
424 org-footnote-section
; we do not use a footnote section
425 (not sort-only
) ; this is normalization
426 for-preprocessor
) ; the is the preprocessor
427 ;; Insert the footnotes together in one place
432 (format "[%s] %s" (nth (if sort-only
0 1) x
)
433 (org-trim (nth 2 x
))))
435 (if ref-table
(insert "\n" def
"\n\n")))
436 ;; Insert each footnote near the first reference
437 ;; Happens only in Org files with no special footnote section,
438 ;; and only when doing sorting
439 (mapc 'org-insert-footnote-reference-near-definition
442 (defun org-insert-footnote-reference-near-definition (entry)
443 "Find first reference of footnote ENTRY and insert the definition there.
444 ENTRY is (fn-label num-mark definition)."
446 (goto-char (point-min))
447 (when (re-search-forward (format ".\\[%s[]:]" (regexp-quote (car entry
)))
449 (org-footnote-goto-local-insertion-point)
450 (insert (format "\n\n[%s] %s" (car entry
) (nth 2 entry
))))))
452 (defun org-footnote-goto-local-insertion-point ()
453 "Find insertion point for footnote, just before next outline heading."
454 (outline-next-heading)
455 (or (bolp) (newline))
456 (beginning-of-line 0)
457 (while (and (not (bobp)) (= (char-after) ?
#))
458 (beginning-of-line 0))
459 (if (looking-at "#\\+TBLFM:") (beginning-of-line 2))
461 (skip-chars-backward "\n\r\t "))
463 (defun org-footnote-delete (&optional label
)
464 "Delete the footnote at point.
465 This will remove the definition (even multiple definitions if they exist)
466 and all references of a footnote label."
468 (let (x label l beg def-re
(nref 0) (ndef 0))
470 (when (setq x
(org-footnote-at-reference-p))
471 (setq label
(nth 1 x
))
472 (when (or (not label
) (equal "fn:" label
))
473 (delete-region (1+ (match-beginning 0)) (match-end 0))
474 (message "Anonymous footnote removed")
476 (when (and (not label
) (setq x
(org-footnote-at-definition-p)))
477 (setq label
(nth 1 x
)))
478 (unless label
(error "Don't know which footnote to remove")))
481 (goto-char (point-min))
482 (while (re-search-forward org-footnote-re nil t
)
483 (setq l
(or (match-string 1) (match-string 2)))
484 (when (equal l label
)
485 (delete-region (1+ (match-beginning 0)) (match-end 0))
487 (goto-char (point-min))
488 (setq def-re
(concat "^\\[" (regexp-quote label
) "\\]"))
489 (while (re-search-forward def-re nil t
)
490 (setq beg
(match-beginning 0))
491 (if (re-search-forward "^\\[\\|^[ \t]*$\\|^\\*+ " nil t
)
492 (goto-char (match-beginning 0))
493 (goto-char (point-max)))
494 (delete-region beg
(point))
496 (message "%d definition(s) of and %d reference(s) of footnote %s removed"
499 (provide 'org-footnote
)
501 ;; arch-tag: 1b5954df-fb5d-4da5-8709-78d944dbfc37
503 ;;; org-footnote.el ends here