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-back-to-heading
"org" (&optional invisible-ok
))
46 (declare-function org-end-of-subtree
"org" (&optional invisible-ok to-heading
))
48 (defconst org-footnote-re
49 (concat "." ; to make sure it is not at the beginning of a line
54 (org-re "\\(fn:\\([-_[:word:]]+?\\)?\\)\\(?::\\([^\]]*?\\)\\)?")
57 "Regular expression for matching footnotes.")
59 (defconst org-footnote-definition-re
60 (org-re "^\\(\\[\\([0-9]+\\|fn:[-_[:word:]]+\\)\\]\\)")
61 "Regular expression matching the definition of a footnote.")
63 (defcustom org-footnote-section
"Footnotes"
64 "Outline heading containing footnote definitions before export.
65 This can be nil, to place footnotes locally at the end of the current
66 outline node. If can also be the name of a special outline heading
67 under which footnotes should be put.
68 This variable defines the place where Org puts the definition
69 automatically. However, by hand you may place definitions *anywhere*.
70 If this is a string, during export, all subtrees starting with this
71 heading will be removed after extracting footnote definitions."
74 (string :tag
"Special outline node name")
75 (const :tag
"Define footnotes in the current outline node" nil
)))
77 (defcustom org-footnote-tag-for-non-org-mode-files
"Footnotes:"
78 "Tag marking the beginning of footnote section.
79 The Org-mode footnote engine can be used in arbitrary text files as well
80 as in Org-mode. Outside Org-mode, new footnotes are always placed at
81 the end of the file. When you normalize the notes, any line containing
82 only this tag will be removed, a new one will be inserted at the end
83 of the file, followed by the collected and normalized footnotes."
87 (defcustom org-footnote-define-inline nil
88 "Non-nil means, define footnotes inline, at reference location.
89 When nil, footnotes will be defined in a special section near
90 the end of the document. When t, the [fn:label:definition] notation
91 will be used to define the footnote at the reference position."
95 (defcustom org-footnote-auto-label t
96 "Non-nil means, define automatically new labels for footnotes.
99 nil prompt the user for each label
100 t create unique labels of the form [fn:1], [fn:2], ...
101 confirm like t, but let the user edit the created value. In particular,
102 the label can be removed from the minibuffer, to create
103 an anonymous footnote.
104 plain Automatically create plain number labels like [1]"
107 (const :tag
"Frompt for label" nil
)
108 (const :tag
"Create automatic [fn:N]" t
)
109 (const :tag
"Offer automatic [fn:N] for editing" confirm
)
110 (const :tag
"Create automatic [N]" plain
)))
112 (defun org-footnote-at-reference-p ()
113 "Is the cursor at a footnote reference?
114 If yes, return the beginning position, the label, and the definition, if local."
115 (when (org-in-regexp org-footnote-re
15)
116 (list (match-beginning 0)
118 (if (equal (match-string 2) "fn:") nil
(match-string 2)))
121 (defun org-footnote-at-definition-p ()
122 "Is the cursor at a footnote definition.
123 This matches only pure definitions like [1] or [fn:name] at the beginning
124 of a line. It does not a references like [fn:name:definition], where the
125 footnote text is included and defined locally.
126 The return value will be nil if not at a foornote definition, and a list
127 with start and label of the footnote if there is a definition at point."
130 (let ((lim (save-excursion (re-search-backward "^\\*+ \\|^[ \t]*$" nil t
))))
131 (when (re-search-backward org-footnote-definition-re lim t
)
132 (list (match-beginning 0) (match-string 2))))))
134 (defun org-footnote-goto-definition (label)
135 "Find the definition of the footnote with label LABEL."
136 (interactive "sLabel: ")
138 (setq label
(org-footnote-normalize-label label
))
139 (let ((re (format "^\\[%s\\]\\|.\\[%s:" label label
))
142 (setq pos
(or (re-search-forward re nil t
)
143 (and (goto-char (point-min))
144 (re-search-forward re nil t
))
145 (and (progn (widen) t
)
146 (goto-char (point-min))
147 (re-search-forward re nil t
)))))
149 (error "Cannot find definition of footnote %s" label
)
151 (message "Edit definition and go back with `C-c &' or, if unique, with `C-c C-c'."))))
153 (defun org-footnote-goto-next-reference (label)
154 "Find the definition of the footnote with label LABEL."
155 (interactive "sLabel: ")
157 (setq label
(org-footnote-normalize-label label
))
158 (let ((re (format ".\\[%s[]:]" label
))
161 (setq pos
(or (re-search-forward re nil t
)
162 (and (goto-char (point-min))
163 (re-search-forward re nil t
))
164 (and (progn (widen) t
)
166 (re-search-forward re nil t
))
167 (and (goto-char (point-min))
168 (re-search-forward re nil t
)))))
169 (if pos
(goto-char pos
)
170 (error "Cannot find reference of footnote %s" label
))))
172 (defun org-footnote-normalize-label (label)
173 (if (numberp label
) (setq label
(number-to-string label
)))
174 (if (not (string-match "^[0-9]+$\\|^$\\|^fn:" label
))
175 (setq label
(concat "fn:" label
)))
178 (defun org-footnote-all-labels ()
179 "Return list with all defined foot labels used in the buffer."
184 (goto-char (point-min))
185 (while (re-search-forward org-footnote-definition-re nil t
)
186 (setq l
(org-match-string-no-properties 2))
187 (and l
(add-to-list 'rtn l
)))
188 (goto-char (point-min))
189 (while (re-search-forward org-footnote-re nil t
)
190 (setq l
(or (org-match-string-no-properties 1)
191 (org-match-string-no-properties 2)))
192 (and l
(not (equal l
"fn:")) (add-to-list 'rtn l
)))))
195 (defun org-footnote-unique-label (&optional current
)
196 "Return a new unique footnote label.
197 The returns the firsts fn:N labels that is currently not used."
198 (unless current
(setq current
(org-footnote-all-labels)))
199 (let ((fmt (if (eq org-footnote-auto-label
'plain
) "%d" "fn:%d"))
201 (while (member (format fmt cnt
) current
)
205 (defvar org-footnote-label-history nil
206 "History of footnote labels entered in current buffer.")
207 (make-variable-buffer-local 'org-footnote-label-history
)
209 (defun org-footnote-new ()
210 "Insert a new footnote.
211 This command prompts for a label. If this is a label referencing an
212 existing label, only insert the label. If the footnote label is empty
213 or new, let the user edit the definition of the footnote."
215 (let* ((labels (org-footnote-all-labels))
216 (propose (org-footnote-unique-label labels
))
218 (if (member org-footnote-auto-label
'(t plain
))
221 "Label (leave empty for anonymous): "
222 (mapcar 'list labels
) nil nil
223 (if (eq org-footnote-auto-label
'confirm
) propose nil
)
224 'org-footnote-label-history
))))
225 (setq label
(org-footnote-normalize-label label
))
230 ((member label labels
)
231 (insert "[" label
"]")
232 (message "New reference to existing note"))
233 (org-footnote-define-inline
234 (insert "[" label
": ]")
237 (insert "[" label
"]")
238 (org-footnote-create-definition label
)))))
240 (defun org-footnote-create-definition (label)
241 "Start the definition of a footnote with label LABEL."
242 (interactive "sLabel: ")
243 (setq label
(org-footnote-normalize-label label
))
247 (if (not org-footnote-section
)
248 ;; No section, put foornote into the curren outline node
250 ;; Try to find or make the special node
251 (setq re
(concat "^\\*+[ \t]+" org-footnote-section
"[ \t]*$"))
252 (unless (or (re-search-forward re nil t
)
253 (and (progn (widen) t
)
254 (re-search-forward re nil t
)))
255 (goto-char (point-max))
256 (insert "\n\n* " org-footnote-section
)))
257 ;; Now go to the end of this entry and insert there.
258 (outline-next-heading)
260 (skip-chars-backward " \t\n\r")
261 (delete-region (point) p
))
263 (setq re
(concat "^" org-footnote-tag-for-non-org-mode-files
"[ \t]*$"))
264 (unless (re-search-forward re nil t
)
265 (goto-char (point-max))
266 (skip-chars-backward " \t\r\n")
268 (delete-region (point) (point-max))
269 (insert org-footnote-tag-for-non-org-mode-files
"\n"))
270 (goto-char (point-max))
271 (skip-chars-backward " \t\r\n")
272 (delete-region (point) (point-max))))
275 (insert "[" label
"] ")
276 (message "Edit definition and go back with `C-c &' or, if unique, with `C-c C-c'.")))
279 (defun org-footnote-action (&optional special
)
280 "Do the right thing for footnotes.
281 When at a foornote reference, jump to the definition. When at a definition,
282 jump to the refernces. When neither at definition or reference,
283 create a new footnote, interactively.
284 With prefix arg SPECIAL, offer additional commands in a menu."
289 (message "Footnotes: [s]ort | convert to [n]umeric | [d]elete")
290 (setq c
(read-char-exclusive))
293 (org-footnote-normalize 'sort
))
295 (org-footnote-normalize))
297 (org-footnote-delete))
298 (t (error "No such footnote command %c" c
))))
299 ((setq tmp
(org-footnote-at-reference-p))
301 (org-footnote-goto-definition (nth 1 tmp
))
302 (goto-char (match-beginning 4))))
303 ((setq tmp
(org-footnote-at-definition-p))
304 (org-footnote-goto-next-reference (nth 1 tmp
)))
305 (t (org-footnote-new)))))
308 (defun org-footnote-normalize (&optional sort-only for-preprocessor
)
309 "Collect the footnotes in various formats and normalize them.
310 This find the different sorts of footnotes allowed in Org, and
311 normalizes them to the usual [N] format that is understood by the
313 When SORT-ONLY is set, only sort the footnote definitions into the
314 referenced sequence."
315 ;; This is based on Paul's function, but rewritten.
316 (let ((count 0) ref def ref-table liste beg beg1 ref def marker a before
319 ;; Now find footnote references,
320 (goto-char (point-min))
321 (while (re-search-forward org-footnote-re nil t
)
323 (setq def
(match-string 4)
324 ref
(or (match-string 1) (match-string 2))
325 before
(char-to-string (char-after (match-beginning 0))))
326 (if (equal ref
"fn:") (setq ref nil
))
327 (if (and ref
(setq a
(assoc ref ref-table
)))
328 (setq marker
(nth 1 a
))
329 (setq marker
(number-to-string (incf count
))))
332 (setq def
(org-trim def
))
334 (if (not (re-search-forward (concat "^\\[" (regexp-quote ref
)
337 (format "FOOTNOTE DEFINITION NOT FOUND: %s" ref
))
338 (setq beg
(match-beginning 0))
339 (setq beg1
(match-end 0))
341 (org-re "^[ \t]*$\\|^\\[\\([0-9]+\\|fn:[-_[:word:]]+\\)\\]")
343 (setq def
(buffer-substring beg1
(match-beginning 0)))
344 (delete-region beg
(match-beginning 0))))))
345 (unless sort-only
(replace-match (concat before
"[" marker
"]")))
346 (if (not a
) (push (list ref marker def
) ref-table
))))
348 ;; First find and remove the footnote section
349 (goto-char (point-min))
352 (if (and org-footnote-section
354 (concat "^\\*[ \t]+" (regexp-quote org-footnote-section
)
359 (org-back-to-heading t
)
361 (setq ins-point
(point))
362 (delete-region (point) (org-end-of-subtree t
)))
363 (goto-char (point-max))
364 (unless for-preprocessor
365 (when org-footnote-section
366 (insert "* " org-footnote-section
"\n")
367 (setq ins-point
(point))))))
369 (if (re-search-forward
371 (regexp-quote org-footnote-tag-for-non-org-mode-files
)
375 (goto-char (point-max))
376 (skip-chars-backward " \t\n\r")
377 (delete-region (point) (point-max))
378 (insert "\n\n" org-footnote-tag-for-non-org-mode-files
"\n")
379 (setq ins-point
(point))))
381 ;; Insert the footnotes again
382 (goto-char (or ins-point
(point-max)))
383 (setq ref-table
(reverse ref-table
))
387 (lambda (x) (and (car x
)
388 (not (equal (car x
) "fn:"))
394 (format "[%s] %s" (nth (if sort-only
0 1) x
)
395 (org-trim (nth 2 x
))))
397 (if ref-table
(insert "\n" def
"\n\n")))))
399 (defun org-footnote-delete (&optional label
)
400 "Delete the footnote at point.
401 This will remove the definition (even multiple definitions if they exist)
402 and all references of a footnote label."
404 (let (x label l beg def-re
(nref 0) (ndef 0))
406 (when (setq x
(org-footnote-at-reference-p))
407 (setq label
(nth 1 x
))
408 (when (or (not label
) (equal "fn:" label
))
409 (delete-region (1+ (match-beginning 0)) (match-end 0))
410 (message "Anonymous footnote removed")
412 (when (and (not label
) (setq x
(org-footnote-at-definition-p)))
413 (setq label
(nth 1 x
)))
414 (unless label
(error "Don't know which footnote to remove")))
417 (goto-char (point-min))
418 (while (re-search-forward org-footnote-re nil t
)
419 (setq l
(or (match-string 1) (match-string 2)))
420 (when (equal l label
)
421 (delete-region (1+ (match-beginning 0)) (match-end 0))
423 (goto-char (point-min))
424 (setq def-re
(concat "^\\[" (regexp-quote label
) "\\]"))
425 (while (re-search-forward def-re nil t
)
426 (setq beg
(match-beginning 0))
427 (if (re-search-forward "^\\[\\|^[ \t]*$\\|^\\*+ " nil t
)
428 (goto-char (match-beginning 0))
429 (goto-char (point-max)))
430 (delete-region beg
(point))
432 (message "%d definition(s) of and %d reference(s) of footnote %s removed"
435 (provide 'org-footnote
)
437 ;; arch-tag: 1b5954df-fb5d-4da5-8709-78d944dbfc37
438 ;;; org-footnote.el ends here