1 ;;; org-footnote.el --- Footnote support in Org and elsewhere
3 ;; Copyright (C) 2009, 2010 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 (declare-function org-inside-latex-macro-p
"org" ())
51 (declare-function org-id-uuid
"org" ())
52 (declare-function org-fill-paragraph
"org" (justify))
53 (defvar org-odd-levels-only
) ;; defined in org.el
54 (defvar message-signature-separator
) ;; defined in message.el
56 (defconst org-footnote-re
57 ;; Only [1]-like footnotes are closed in this regexp, as footnotes
58 ;; from other types might contain square brackets (i.e. links) in
61 ;; `org-re' is used for regexp compatibility with XEmacs.
62 (org-re (concat "\\[\\(?:"
63 ;; Match inline footnotes.
64 "fn:\\([-_[:word:]]+\\)?:\\|"
65 ;; Match other footnotes.
66 "\\(?:\\([0-9]+\\)\\]\\)\\|"
67 "\\(fn:[-_[:word:]]+\\)"
69 "Regular expression for matching footnotes.")
71 (defconst org-footnote-definition-re
72 (org-re "^\\(\\[\\([0-9]+\\|fn:[-_[:word:]]+\\)\\]\\)")
73 "Regular expression matching the definition of a footnote.")
75 (defgroup org-footnote nil
76 "Footnotes in Org-mode."
80 (defcustom org-footnote-section
"Footnotes"
81 "Outline heading containing footnote definitions before export.
82 This can be nil, to place footnotes locally at the end of the current
83 outline node. If can also be the name of a special outline heading
84 under which footnotes should be put.
85 This variable defines the place where Org puts the definition
86 automatically, i.e. when creating the footnote, and when sorting the notes.
87 However, by hand you may place definitions *anywhere*.
88 If this is a string, during export, all subtrees starting with this
89 heading will be removed after extracting footnote definitions."
92 (string :tag
"Collect footnotes under heading")
93 (const :tag
"Define footnotes locally" nil
)))
95 (defcustom org-footnote-tag-for-non-org-mode-files
"Footnotes:"
96 "Tag marking the beginning of footnote section.
97 The Org-mode footnote engine can be used in arbitrary text files as well
98 as in Org-mode. Outside Org-mode, new footnotes are always placed at
99 the end of the file. When you normalize the notes, any line containing
100 only this tag will be removed, a new one will be inserted at the end
101 of the file, followed by the collected and normalized footnotes."
105 (defcustom org-footnote-define-inline nil
106 "Non-nil means define footnotes inline, at reference location.
107 When nil, footnotes will be defined in a special section near
108 the end of the document. When t, the [fn:label:definition] notation
109 will be used to define the footnote at the reference position."
113 (defcustom org-footnote-auto-label t
114 "Non-nil means define automatically new labels for footnotes.
117 nil prompt the user for each label
118 t create unique labels of the form [fn:1], [fn:2], ...
119 confirm like t, but let the user edit the created value. In particular,
120 the label can be removed from the minibuffer, to create
121 an anonymous footnote.
122 random Automatically generate a unique, random label.
123 plain Automatically create plain number labels like [1]"
126 (const :tag
"Prompt for label" nil
)
127 (const :tag
"Create automatic [fn:N]" t
)
128 (const :tag
"Offer automatic [fn:N] for editing" confirm
)
129 (const :tag
"Create a random label" random
)
130 (const :tag
"Create automatic [N]" plain
)))
132 (defcustom org-footnote-auto-adjust nil
133 "Non-nil means automatically adjust footnotes after insert/delete.
134 When this is t, after each insertion or deletion of a footnote,
135 simple fn:N footnotes will be renumbered, and all footnotes will be sorted.
136 If you want to have just sorting or just renumbering, set this variable
137 to `sort' or `renumber'.
139 The main values of this variable can be set with in-buffer options:
142 #+STARTUP: nofnadjust"
145 (const :tag
"Renumber" renumber
)
146 (const :tag
"Sort" sort
)
147 (const :tag
"Renumber and Sort" t
)))
149 (defcustom org-footnote-fill-after-inline-note-extraction nil
150 "Non-nil means fill paragraphs after extracting footnotes.
151 When extracting inline footnotes, the lengths of lines can change a lot.
152 When this option is set, paragraphs from which an inline footnote has been
153 extracted will be filled again."
157 (defun org-footnote-at-reference-p ()
158 "Is the cursor at a footnote reference?
160 If so, return an list containing its label, beginning and ending
161 positions, and the definition, if local."
162 (when (and (not (or (org-in-commented-line)
163 (org-in-verbatim-emphasis)))
164 (or (org-in-regexp org-footnote-re
)
165 (save-excursion (re-search-backward org-footnote-re nil t
)))
166 ;; A footnote reference cannot start at bol.
167 (/= (match-beginning 0) (point-at-bol)))
168 (let* ((beg (match-beginning 0))
169 (label (or (match-string 2) (match-string 3)
170 ;; Anonymous footnotes don't have labels
171 (and (match-string 1) (concat "fn:" (match-string 1)))))
172 ;; Inline footnotes don't end at (match-end 0) as
173 ;; `org-footnote-re' stops just after the second colon.
174 ;; Find the real ending with `scan-sexps', so Org doesn't
175 ;; get fooled by unrelated closing square brackets.
176 (end (ignore-errors (scan-sexps beg
1))))
177 ;; Point is really at a reference if it's located before true
178 ;; ending of the footnote and isn't within a LaTeX macro. About
179 ;; that case, some special attention should be paid. Indeed,
180 ;; when two footnotes are side by side, once the first one is
181 ;; changed into LaTeX, the second one might then be considered
182 ;; as an optional argument of the command. To prevent that, we
183 ;; have a look at the `org-protected' property of that LaTeX
185 (when (and end
(< (point) end
)
186 (or (not (org-inside-latex-macro-p))
187 (and (get-text-property (1- beg
) 'org-protected
)
188 (not (get-text-property beg
'org-protected
)))))
190 ;; Definition: ensure this is an inline footnote first.
191 (and (or (not label
) (match-string 1))
192 (org-trim (buffer-substring (match-end 0) (1- end
)))))))))
194 (defun org-footnote-at-definition-p ()
195 "Is the cursor at a footnote definition?
197 This matches only pure definitions like [1] or [fn:name] at the beginning
198 of a line. It does not match references like [fn:name:definition], where the
199 footnote text is included and defined locally.
201 The return value will be nil if not at a footnote definition, and a list with
202 label, start, end and definition of the footnote otherwise."
205 (let ((lim (save-excursion (re-search-backward "^\\*+ \\|^[ \t]*$" nil t
))))
206 (when (re-search-backward org-footnote-definition-re lim t
)
208 (list (match-string 2)
211 (or (and (re-search-forward
212 (org-re "^[ \t]*$\\|^\\*+ \\|^\\[\\([0-9]+\\|fn:[-_[:word:]]+\\)\\]")
214 (progn (skip-chars-forward " \t\n") (point-at-bol)))
216 (org-trim (buffer-substring (match-end 0) (point))))))))
218 (defun org-footnote-get-next-reference (&optional label backward limit
)
219 "Return complete reference of the next footnote.
221 If LABEL is provided, get the next reference of that footnote. If
222 BACKWARD is non-nil, find previous reference instead. LIMIT is
223 the buffer position bounding the search.
225 Return value is a list like those provided by `org-footnote-at-reference-p'.
226 If no footnote is found, return nil."
228 (let* ((label-fmt (if label
(format "\\[%s[]:]" label
) org-footnote-re
)))
231 (unless (funcall (if backward
#'re-search-backward
#'re-search-forward
)
234 (unless backward
(backward-char))
235 (let ((ref (org-footnote-at-reference-p)))
236 (when ref
(throw 'exit ref
))))))))
238 (defun org-footnote-next-reference-or-definition (limit)
239 "Move point to next footnote reference or definition.
241 LIMIT is the buffer position bounding the search.
243 Return value is a list like those provided by
244 `org-footnote-at-reference-p' or `org-footnote-at-definition-p'.
245 If no footnote is found, return nil."
249 (unless (re-search-forward org-footnote-re limit t
)
251 ;; Beware: with [1]-like footnotes point will be just after
252 ;; the closing square bracket.
255 ((setq ref
(org-footnote-at-reference-p))
257 ;; Definition: also grab the last square bracket, only
258 ;; matched in `org-footnote-re' for [1]-like footnotes.
259 ((= (point-at-bol) (match-beginning 0))
260 (let ((end (match-end 0)))
262 (list nil
(match-beginning 0)
263 (if (eq (char-before end
) 93) end
(1+ end
)))))))))))
265 (defun org-footnote-get-definition (label)
266 "Return label, boundaries and definition of the footnote LABEL."
267 (let* ((label (regexp-quote (org-footnote-normalize-label label
)))
268 (re (format "^\\[%s\\]\\|.\\[%s:" label label
))
271 (when (or (re-search-forward re nil t
)
272 (and (goto-char (point-min))
273 (re-search-forward re nil t
))
274 (and (progn (widen) t
)
275 (goto-char (point-min))
276 (re-search-forward re nil t
)))
277 (let ((refp (org-footnote-at-reference-p)))
279 ((and (nth 3 refp
) refp
))
280 ((org-footnote-at-definition-p))))))))
282 (defun org-footnote-goto-definition (label)
283 "Move point to the definition of the footnote LABEL."
284 (interactive "sLabel: ")
286 (let ((def (org-footnote-get-definition label
)))
288 (error "Cannot find definition of footnote %s" label
)
289 (goto-char (nth 1 def
))
290 (looking-at (format "\\[%s\\]\\|\\[%s:" label label
))
291 (goto-char (match-end 0))
292 (org-show-context 'link-search
)
293 (message "Edit definition and go back with `C-c &' or, if unique, with `C-c C-c'."))))
295 (defun org-footnote-goto-previous-reference (label)
296 "Find the first closest (to point) reference of footnote with label LABEL."
297 (interactive "sLabel: ")
299 (let* ((label (org-footnote-normalize-label label
)) ref
)
301 (setq ref
(or (org-footnote-get-next-reference label t
)
302 (org-footnote-get-next-reference label
)
306 (org-footnote-get-next-reference label t
)
307 (org-footnote-get-next-reference label
))))))
309 (error "Cannot find reference of footnote %s" label
)
310 (goto-char (nth 1 ref
))
311 (org-show-context 'link-search
))))
313 (defun org-footnote-normalize-label (label)
314 "Return LABEL as an appropriate string."
316 ((numberp label
) (number-to-string label
))
317 ((equal "" label
) nil
)
318 ((not (string-match "^[0-9]+$\\|^fn:" label
))
319 (concat "fn:" label
))
322 (defun org-footnote-all-labels (&optional with-defs
)
323 "Return list with all defined foot labels used in the buffer.
325 If WITH-DEFS is non-nil, also associate the definition to each
326 label. The function will then return an alist whose key is label
327 and value definition."
331 ;; Depending on WITH-DEFS, store label or (label . def) of
332 ;; footnote reference/definition given as argument in RTN.
334 (let ((lbl (car el
)))
335 (push (if with-defs
(cons lbl
(nth 3 el
)) lbl
) rtn
))))))
339 ;; Find all labels found in definitions.
340 (goto-char (point-min))
342 (while (re-search-forward org-footnote-definition-re nil t
)
343 (when (setq def
(org-footnote-at-definition-p))
344 (funcall push-to-rtn def
))))
345 ;; Find all labels found in references.
346 (goto-char (point-min))
348 (while (setq ref
(org-footnote-get-next-reference))
349 (goto-char (nth 2 ref
))
350 (and (car ref
) ; ignore anonymous footnotes
351 (not (funcall (if with-defs
#'assoc
#'member
) (car ref
) rtn
))
352 (funcall push-to-rtn ref
))))))
355 (defun org-footnote-unique-label (&optional current
)
356 "Return a new unique footnote label.
357 The returns the firsts fn:N labels that is currently not used."
358 (unless current
(setq current
(org-footnote-all-labels)))
359 (let ((fmt (if (eq org-footnote-auto-label
'plain
) "%d" "fn:%d"))
361 (while (member (format fmt cnt
) current
)
365 (defvar org-footnote-label-history nil
366 "History of footnote labels entered in current buffer.")
367 (make-variable-buffer-local 'org-footnote-label-history
)
369 (defun org-footnote-new ()
370 "Insert a new footnote.
371 This command prompts for a label. If this is a label referencing an
372 existing label, only insert the label. If the footnote label is empty
373 or new, let the user edit the definition of the footnote."
375 (let* ((labels (and (not (equal org-footnote-auto-label
'random
))
376 (org-footnote-all-labels)))
377 (propose (org-footnote-unique-label labels
))
379 (org-footnote-normalize-label
381 ((member org-footnote-auto-label
'(t plain
))
383 ((equal org-footnote-auto-label
'random
)
385 (substring (org-id-uuid) 0 8))
388 "Label (leave empty for anonymous): "
389 (mapcar 'list labels
) nil nil
390 (if (eq org-footnote-auto-label
'confirm
) propose nil
)
391 'org-footnote-label-history
))))))
396 ((member label labels
)
397 (insert "[" label
"]")
398 (message "New reference to existing note"))
399 (org-footnote-define-inline
400 (insert "[" label
": ]")
402 (org-footnote-auto-adjust-maybe))
404 (insert "[" label
"]")
405 (org-footnote-create-definition label
)
406 (org-footnote-auto-adjust-maybe)))))
408 (defun org-footnote-create-definition (label)
409 "Start the definition of a footnote with label LABEL."
410 (interactive "sLabel: ")
411 (let ((label (org-footnote-normalize-label label
)))
414 ;; No section, put footnote into the current outline node Try to
415 ;; find or make the special node
416 (when org-footnote-section
417 (goto-char (point-min))
418 (let ((re (concat "^\\*+[ \t]+" org-footnote-section
"[ \t]*$")))
419 (unless (or (re-search-forward re nil t
)
420 (and (progn (widen) t
)
421 (re-search-forward re nil t
)))
422 (goto-char (point-max))
423 (insert "\n\n* " org-footnote-section
"\n"))))
424 ;; Now go to the end of this entry and insert there.
425 (org-footnote-goto-local-insertion-point)
426 (org-show-context 'link-search
))
428 (let ((re (concat "^" org-footnote-tag-for-non-org-mode-files
"[ \t]*$")))
429 (unless (re-search-forward re nil t
)
430 (let ((max (if (and (derived-mode-p 'message-mode
)
431 (re-search-forward message-signature-separator nil t
))
432 (progn (beginning-of-line) (point))
433 (goto-char (point-max)))))
434 (skip-chars-backward " \t\r\n")
435 (delete-region (point) max
)
437 (insert org-footnote-tag-for-non-org-mode-files
"\n"))))
438 ;; Skip existing footnotes
439 (while (re-search-forward "^[[:space:]]*\\[[^]]+\\] " nil t
)
441 (insert "\n[" label
"] \n")
442 (goto-char (1- (point)))
443 (message "Edit definition and go back with `C-c &' or, if unique, with `C-c C-c'.")))
446 (defun org-footnote-action (&optional special
)
447 "Do the right thing for footnotes.
449 When at a footnote reference, jump to the definition.
451 When at a definition, jump to the references if they exist, offer
452 to create them otherwise.
454 When neither at definition or reference, create a new footnote,
457 With prefix arg SPECIAL, offer additional commands in a menu."
462 (message "Footnotes: [s]ort | [r]enumber fn:N | [S]=r+s |->[n]umeric | [d]elete")
463 (setq c
(read-char-exclusive))
465 ((eq c ?s
) (org-footnote-normalize 'sort
))
466 ((eq c ?r
) (org-footnote-renumber-fn:N
))
468 (org-footnote-renumber-fn:N
)
469 (org-footnote-normalize 'sort
))
470 ((eq c ?n
) (org-footnote-normalize))
471 ((eq c ?d
) (org-footnote-delete))
472 (t (error "No such footnote command %c" c
))))
473 ((setq tmp
(org-footnote-at-reference-p))
475 ;; Anonymous footnote: move point at the beginning of its
478 (goto-char (nth 1 tmp
))
480 ;; A definition exists: move to it.
481 ((ignore-errors (org-footnote-goto-definition (car tmp
))))
482 ;; No definition exists: offer to create it.
483 ((yes-or-no-p (format "No definition for %s. Create one? " (car tmp
)))
484 (org-footnote-create-definition (car tmp
)))))
485 ((setq tmp
(org-footnote-at-definition-p))
486 (org-footnote-goto-previous-reference (car tmp
)))
487 (t (org-footnote-new)))))
489 (defvar org-footnote-insert-pos-for-preprocessor
'point-max
490 "See `org-footnote-normalize'.")
493 (defun org-footnote-normalize (&optional sort-only pre-process-p
)
494 "Collect the footnotes in various formats and normalize them.
496 This finds the different sorts of footnotes allowed in Org, and
497 normalizes them to the usual [N] format that is understood by the
500 When SORT-ONLY is set, only sort the footnote definitions into the
503 When PRE-PROCESS-P is non-nil, the default action, is to insert
504 normalized footnotes towards the end of the pre-processing buffer.
505 Some exporters like docbook, odt, etc. expect that footnote
506 definitions be available before any references to them. Such
507 exporters can let bind `org-footnote-insert-pos-for-preprocessor' to
508 symbol 'point-min to achieve the desired behaviour.
510 Additional note on `org-footnote-insert-pos-for-preprocessor':
511 1. This variable has not effect when FOR-PREPROCESSOR is nil.
512 2. This variable (potentially) obviates the need for extra scan
513 of pre-processor buffer as witnessed in
514 `org-export-docbook-get-footnotes'."
515 ;; This is based on Paul's function, but rewritten.
517 ;; Re-create `org-with-limited-levels', but not limited to Org
520 (and (boundp 'org-inlinetask-min-level
)
521 org-inlinetask-min-level
522 (1- org-inlinetask-min-level
)))
523 (nstars (and limit-level
524 (if org-odd-levels-only
525 (and limit-level
(1- (* limit-level
2)))
528 (concat "\\*" (if nstars
(format "\\{1,%d\\} " nstars
) "+ ")))
529 ;; Determine the highest marker used so far.
530 (ref-table (when pre-process-p org-export-footnotes-seen
))
531 (count (if (and pre-process-p ref-table
)
532 (apply 'max
(mapcar (lambda (e) (nth 1 e
)) ref-table
))
536 ;; 1. Find every footnote reference, extract the definition, and
537 ;; collect that data in REF-TABLE. If SORT-ONLY is nil, also
538 ;; normalize references.
539 (goto-char (point-min))
540 (while (setq ref
(org-footnote-get-next-reference))
541 (let* ((lbl (car ref
))
542 ;; When footnote isn't anonymous, check if it's label
543 ;; (REF) is already stored in REF-TABLE. In that case,
544 ;; extract number used to identify it (MARKER). If
545 ;; footnote is unknown, increment the global counter
546 ;; (COUNT) to create an unused identifier.
547 (a (and lbl
(assoc lbl ref-table
)))
548 (marker (or (nth 1 a
) (incf count
)))
549 ;; Is the reference inline or pointing to an inline
551 (inlinep (or (stringp (nth 3 ref
)) (nth 3 a
))))
552 ;; Replace footnote reference with [MARKER]. Maybe fill
553 ;; paragraph once done. If SORT-ONLY is non-nil, only move
554 ;; to the end of reference found to avoid matching it twice.
555 ;; If PRE-PROCESS-P isn't nil, also add `org-footnote'
556 ;; property to it, so it can be easily recognized by
559 (goto-char (nth 2 ref
))
560 (delete-region (nth 1 ref
) (nth 2 ref
))
561 (goto-char (nth 1 ref
))
562 (let ((new-ref (format "[%d]" marker
)))
563 (when pre-process-p
(org-add-props new-ref
'(org-footnote t
)))
566 org-footnote-fill-after-inline-note-extraction
567 (org-fill-paragraph)))
568 ;; Add label (REF), identifier (MARKER) and definition (DEF)
569 ;; to REF-TABLE if data was unknown.
571 (let ((def (or (nth 3 ref
) ; inline
573 (cdr (assoc lbl org-export-footnotes-data
)))
574 (nth 3 (org-footnote-get-definition lbl
)))))
575 (push (list lbl marker def inlinep
) ref-table
)))
576 ;; Remove definition of non-inlined footnotes.
577 (unless inlinep
(org-footnote-delete-definitions lbl
))))
578 ;; 2. Find and remove the footnote section, if any. If we are
579 ;; exporting, insert it again at end of buffer. In a non
580 ;; org-mode file, insert instead
581 ;; `org-footnote-tag-for-non-org-mode-files'.
582 (goto-char (point-min))
585 (if (and org-footnote-section
587 (concat "^\\*[ \t]+" (regexp-quote org-footnote-section
)
592 (org-back-to-heading t
)
594 (setq ins-point
(point))
595 (delete-region (point) (org-end-of-subtree t
)))
596 (goto-char (point-max))
597 (unless pre-process-p
598 (when org-footnote-section
599 (or (bolp) (insert "\n"))
600 (insert "* " org-footnote-section
"\n")
601 (setq ins-point
(point))))))
603 (if (re-search-forward
605 (regexp-quote org-footnote-tag-for-non-org-mode-files
)
609 (goto-char (point-max))
610 (skip-chars-backward " \t\n\r")
611 (delete-region (point) (point-max))
612 (insert "\n\n" org-footnote-tag-for-non-org-mode-files
"\n")
613 (setq ins-point
(point))))
614 ;; 3. Clean-up REF-TABLE.
620 ;; When only sorting, ignore inline footnotes.
621 ((and sort-only
(nth 3 x
)) nil
)
622 ;; No definition available: provide one.
624 (append (butlast x
2)
625 (list (format "DEFINITION NOT FOUND: %s" (car x
))
629 (setq ref-table
(nreverse ref-table
))
630 ;; 4. Insert the footnotes again in the buffer, at the
634 (eq org-footnote-insert-pos-for-preprocessor
'point-min
)
639 ((not ref-table
)) ; no footnote: exit
640 ;; Cases when footnotes should be inserted together in one place.
641 ((or (not (org-mode-p))
645 (mapconcat (lambda (x) (format "[%s] %s"
646 (nth (if sort-only
0 1) x
) (nth 2 x
)))
649 ;; When exporting, add newly insert markers along with their
650 ;; associated definition to `org-export-footnotes-seen'.
652 (setq org-export-footnotes-seen ref-table
)))
653 ;; Else, insert each definition at the end of the section
654 ;; containing their first reference. Happens only in Org
655 ;; files with no special footnote section, and only when
657 (t (mapc 'org-insert-footnote-reference-near-definition
660 (defun org-insert-footnote-reference-near-definition (entry)
661 "Find first reference of footnote ENTRY and insert the definition there.
662 ENTRY is (fn-label num-mark definition)."
664 (goto-char (point-min))
665 (let ((ref (org-footnote-get-next-reference (car entry
))))
667 (goto-char (nth 2 ref
))
668 (org-footnote-goto-local-insertion-point)
669 (insert (format "\n[%s] %s\n" (car entry
) (nth 2 entry
)))))))
671 (defun org-footnote-goto-local-insertion-point ()
672 "Find insertion point for footnote, just before next outline heading."
673 (org-with-limited-levels (outline-next-heading))
674 (or (bolp) (newline))
675 (beginning-of-line 0)
676 (while (and (not (bobp)) (= (char-after) ?
#))
677 (beginning-of-line 0))
678 (if (looking-at "[ \t]*#\\+TBLFM:") (beginning-of-line 2))
680 (skip-chars-backward "\n\r\t ")
683 (defun org-footnote-delete-references (label)
684 "Delete every reference to footnote LABEL.
685 Return the number of footnotes removed."
687 (goto-char (point-min))
689 (while (setq ref
(org-footnote-get-next-reference label
))
690 (goto-char (nth 1 ref
))
691 (delete-region (nth 1 ref
) (nth 2 ref
))
695 (defun org-footnote-delete-definitions (label)
696 "Delete every definition of the footnote LABEL.
697 Return the number of footnotes removed."
699 (goto-char (point-min))
700 (let ((def-re (concat "^\\[" (regexp-quote label
) "\\]"))
702 (while (re-search-forward def-re nil t
)
703 (let ((full-def (org-footnote-at-definition-p)))
704 (delete-region (nth 1 full-def
) (nth 2 full-def
)))
708 (defun org-footnote-delete (&optional label
)
709 "Delete the footnote at point.
710 This will remove the definition (even multiple definitions if they exist)
711 and all references of a footnote label.
713 If LABEL is non-nil, delete that footnote instead."
715 (let* ((nref 0) (ndef 0) x
716 ;; 1. Determine LABEL of footnote at point.
718 ;; LABEL is provided as argument.
720 ;; Footnote reference at point. If the footnote is
721 ;; anonymous, delete it and exit instead.
722 ((setq x
(org-footnote-at-reference-p))
725 (delete-region (nth 1 x
) (nth 2 x
))
726 (message "Anonymous footnote removed")
728 ;; Footnote definition at point.
729 ((setq x
(org-footnote-at-definition-p))
731 (t (error "Don't know which footnote to remove")))))
732 ;; 2. Now that LABEL is non-nil, find every reference and every
733 ;; definition, and delete them.
734 (setq nref
(org-footnote-delete-references label
)
735 ndef
(org-footnote-delete-definitions label
))
736 ;; 3. Verify consistency of footnotes and notify user.
737 (org-footnote-auto-adjust-maybe)
738 (message "%d definition(s) of and %d reference(s) of footnote %s removed"
741 (defun org-footnote-renumber-fn:N
()
742 "Renumber the simple footnotes like fn:17 into a sequence in the document."
748 (goto-char (point-min))
749 (while (re-search-forward "\\[fn:\\([0-9]+\\)[]:]" nil t
)
750 (setq i
(string-to-number (match-string 1)))
751 (when (and (string-match "\\S-" (buffer-substring
752 (point-at-bol) (match-beginning 0)))
754 (push (cons i
(number-to-string (incf n
))) map
)))
755 (goto-char (point-min))
756 (while (re-search-forward "\\(\\[fn:\\)\\([0-9]+\\)\\([]:]\\)" nil t
)
757 (replace-match (concat "\\1" (cdr (assq (string-to-number (match-string 2)) map
)) "\\3")))))))
759 (defun org-footnote-auto-adjust-maybe ()
760 "Renumber and/or sort footnotes according to user settings."
761 (when (memq org-footnote-auto-adjust
'(t renumber
))
762 (org-footnote-renumber-fn:N
))
763 (when (memq org-footnote-auto-adjust
'(t sort
))
764 (let ((label (car (org-footnote-at-definition-p))))
765 (org-footnote-normalize 'sort
)
767 (goto-char (point-min))
768 (and (re-search-forward (concat "^\\[" (regexp-quote label
) "\\]")
771 (just-one-space)))))))
773 (provide 'org-footnote
)
775 ;; arch-tag: 1b5954df-fb5d-4da5-8709-78d944dbfc37
777 ;;; org-footnote.el ends here