org-footnote.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. ;;; org-footnote.el --- Footnote support in Org and elsewhere
  2. ;;
  3. ;; Copyright (C) 2009 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://orgmode.org
  8. ;; Version: 6.16trans
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  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.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;; This file contains the code dealing with footnotes in Org-mode.
  26. ;; The code can also be used in arbitrary text modes to provide
  27. ;; footnotes. Compared to Steven L Baur's footnote.el it provides
  28. ;; better support for resuming editing. It is less configurable than
  29. ;; Steve's code, though.
  30. ;;; Code:
  31. (eval-when-compile
  32. (require 'cl))
  33. (require 'org-macs)
  34. (require 'org-compat)
  35. (declare-function org-in-regexp "org" (re &optional nlines visually))
  36. (declare-function org-mark-ring-push "org" (&optional pos buffer))
  37. (declare-function outline-next-heading "outline")
  38. (declare-function org-trim "org" (s))
  39. (declare-function org-back-to-heading "org" (&optional invisible-ok))
  40. (declare-function org-end-of-subtree "org" (&optional invisible-ok to-heading))
  41. (defconst org-footnote-re
  42. (concat "." ; to make sure it is not at the beginning of a line
  43. "\\["
  44. "\\(?:"
  45. "\\([0-9]+\\)"
  46. "\\|"
  47. (org-re "\\(fn:\\([-_[:word:]]+?\\)?\\)\\(?::\\([^\]]*?\\)\\)?")
  48. "\\)"
  49. "\\]")
  50. "Regular expression for matching footnotes.")
  51. (defconst org-footnote-definition-re
  52. (org-re "^\\(\\[\\([0-9]+\\|fn:[-_[:word:]]+\\)\\]\\)")
  53. "Regular expression matching the definition of a footnote.")
  54. (defcustom org-footnote-section "Footnotes"
  55. "Outline heading containing footnote definitions before export.
  56. This can be nil, to place footnotes locally at the end of the current
  57. outline node. If can also be the name of a special outline heading
  58. under which footnotes should be put.
  59. This variable defines the place where Org puts the definition
  60. automatically. However, by hand you may place definitions *anywhere*.
  61. If this is a string, during export, all subtrees starting with this
  62. heading will be removed after extracting footnote definitions."
  63. :group 'org-footnotes
  64. :type '(choice
  65. (string :tag "Special outline node name")
  66. (const :tag "Define footnotes in the current outline node" nil)))
  67. (defcustom org-footnote-tag-for-non-org-mode-files "Footnotes:"
  68. "Tag marking the beginning of footnote section.
  69. The Org-mode footnote engine can be used in arbitrary text files as well
  70. as in Org-mode. Outside Org-mode, new footnotes are always placed at
  71. the end of the file. When you normalize the notes, any line containing
  72. only this tag will be removed, a new one will be inserted at the end
  73. of the file, followed by the collected and normalized footnotes."
  74. :group 'org-footnotes
  75. :type 'string)
  76. (defcustom org-footnote-define-inline nil
  77. "Non-nil means, define footnotes inline, at reference location.
  78. When nil, footnotes will be defined in a special section near
  79. the end of the document. When t, the [fn:label:definition] notation
  80. will be used to define the footnote at the reference position."
  81. :group 'org-footnote
  82. :type 'boolean)
  83. (defcustom org-footnote-auto-label t
  84. "Non-nil means, define automatically new labels for footnotes.
  85. Possible values are:
  86. nil prompt the user for each label
  87. t create unique labels of the form [fn:1], [fn:2], ...
  88. confirm like t, but let the user edit the created value. In particular,
  89. the label can be removed from the minibuffer, to create
  90. an anonymous footnote.
  91. plain Automatically create plain number labels like [1]"
  92. :group 'org-footnote
  93. :type '(choice
  94. (const :tag "Frompt for label" nil)
  95. (const :tag "Create automatic [fn:N]" t)
  96. (const :tag "Offer automatic [fn:N] for editing" confirm)
  97. (const :tag "Create automatic [N]" plain)))
  98. (defun org-footnote-at-reference-p ()
  99. "Is the cursor at a footnote reference?
  100. If yes, return the beginning position, the label, and the definition, if local."
  101. (when (org-in-regexp org-footnote-re 15)
  102. (list (match-beginning 0)
  103. (or (match-string 1)
  104. (if (equal (match-string 2) "fn:") nil (match-string 2)))
  105. (match-string 4))))
  106. (defun org-footnote-at-definition-p ()
  107. "Is the cursor at a footnote definition.
  108. This matches only pure definitions like [1] or [fn:name] at the beginning
  109. of a line. It does not a references like [fn:name:definition], where the
  110. footnote text is included and defined locally.
  111. The return value will be nil if not at a foornote definition, and a list
  112. with start and label of the footnote if there is a definition at point."
  113. (save-excursion
  114. (end-of-line 1)
  115. (let ((lim (save-excursion (re-search-backward "^\\*+ \\|^[ \t]*$" nil t))))
  116. (when (re-search-backward org-footnote-definition-re lim t)
  117. (list (match-beginning 0) (match-string 2))))))
  118. (defun org-footnote-goto-definition (label)
  119. "Find the definition of the footnote with label LABEL."
  120. (interactive "sLabel: ")
  121. (org-mark-ring-push)
  122. (setq label (org-footnote-normalize-label label))
  123. (let ((re (format "^\\[%s\\]\\|.\\[%s:" label label))
  124. pos)
  125. (save-excursion
  126. (setq pos (or (re-search-forward re nil t)
  127. (and (goto-char (point-min))
  128. (re-search-forward re nil t))
  129. (and (progn (widen) t)
  130. (goto-char (point-min))
  131. (re-search-forward re nil t)))))
  132. (if (not pos)
  133. (error "Cannot find definition of footnote %s" label)
  134. (goto-char pos)
  135. (message "Edit definition and go back with `C-c &' or, if unique, with `C-c C-c'."))))
  136. (defun org-footnote-goto-next-reference (label)
  137. "Find the definition of the footnote with label LABEL."
  138. (interactive "sLabel: ")
  139. (org-mark-ring-push)
  140. (setq label (org-footnote-normalize-label label))
  141. (let ((re (format ".\\[%s[]:]" label))
  142. (p0 (point)) pos)
  143. (save-excursion
  144. (setq pos (or (re-search-forward re nil t)
  145. (and (goto-char (point-min))
  146. (re-search-forward re nil t))
  147. (and (progn (widen) t)
  148. (goto-char p0)
  149. (re-search-forward re nil t))
  150. (and (goto-char (point-min))
  151. (re-search-forward re nil t)))))
  152. (if pos (goto-char pos)
  153. (error "Cannot find reference of footnote %s" label))))
  154. (defun org-footnote-normalize-label (label)
  155. (if (numberp label) (setq label (number-to-string label)))
  156. (if (not (string-match "^[0-9]+$\\|^$\\|^fn:" label))
  157. (setq label (concat "fn:" label)))
  158. label)
  159. (defun org-footnote-all-labels ()
  160. "Return list with all defined foot labels used in the buffer."
  161. (let (rtn l)
  162. (save-excursion
  163. (save-restriction
  164. (widen)
  165. (goto-char (point-min))
  166. (while (re-search-forward org-footnote-definition-re nil t)
  167. (setq l (org-match-string-no-properties 2))
  168. (and l (add-to-list 'rtn l)))
  169. (goto-char (point-min))
  170. (while (re-search-forward org-footnote-re nil t)
  171. (setq l (or (org-match-string-no-properties 1)
  172. (org-match-string-no-properties 2)))
  173. (and l (not (equal l "fn:")) (add-to-list 'rtn l)))))
  174. rtn))
  175. (defun org-footnote-unique-label (&optional current)
  176. "Return a new unique footnote label.
  177. The returns the firsts fn:N labels that is currently not used."
  178. (unless current (setq current (org-footnote-all-labels)))
  179. (let ((fmt (if (eq org-footnote-auto-label 'plain) "%d" "fn:%d"))
  180. (cnt 1))
  181. (while (member (format fmt cnt) current)
  182. (incf cnt))
  183. (format fmt cnt)))
  184. (defvar org-footnote-label-history nil
  185. "History of footnote labels entered in current buffer.")
  186. (make-variable-buffer-local 'org-footnote-label-history)
  187. (defun org-footnote-new ()
  188. "Insert a new footnote.
  189. This command prompts for a label. If this is a label referencing an
  190. existing label, only insert the label. If the footnote label is empty
  191. or new, let the user edit the definition of the footnote."
  192. (interactive)
  193. (let* ((labels (org-footnote-all-labels))
  194. (propose (org-footnote-unique-label labels))
  195. (label
  196. (if (member org-footnote-auto-label '(t plain))
  197. propose
  198. (completing-read
  199. "Label (leave empty for anonymous): "
  200. (mapcar 'list labels) nil nil
  201. (if (eq org-footnote-auto-label 'confirm) propose nil)
  202. 'org-footnote-label-history))))
  203. (setq label (org-footnote-normalize-label label))
  204. (cond
  205. ((equal label "")
  206. (insert "[fn:: ]")
  207. (backward-char 1))
  208. ((member label labels)
  209. (insert "[" label "]")
  210. (message "New reference to existing note"))
  211. (org-footnote-define-inline
  212. (insert "[" label ": ]")
  213. (backward-char 1))
  214. (t
  215. (insert "[" label "]")
  216. (org-footnote-create-definition label)))))
  217. (defun org-footnote-create-definition (label)
  218. "Start the definition of a footnote with label LABEL."
  219. (interactive "sLabel: ")
  220. (setq label (org-footnote-normalize-label label))
  221. (let (re p)
  222. (cond
  223. ((org-mode-p)
  224. (if (not org-footnote-section)
  225. ;; No section, put foornote into the curren outline node
  226. nil
  227. ;; Try to find or make the special node
  228. (setq re (concat "^\\*+[ \t]+" org-footnote-section "[ \t]*$"))
  229. (unless (or (re-search-forward re nil t)
  230. (and (progn (widen) t)
  231. (re-search-forward re nil t)))
  232. (goto-char (point-max))
  233. (insert "\n\n* " org-footnote-section)))
  234. ;; Now go to the end of this entry and insert there.
  235. (outline-next-heading)
  236. (setq p (point))
  237. (skip-chars-backward " \t\n\r")
  238. (delete-region (point) p))
  239. (t
  240. (setq re (concat "^" org-footnote-tag-for-non-org-mode-files "[ \t]*$"))
  241. (unless (re-search-forward re nil t)
  242. (goto-char (point-max))
  243. (skip-chars-backward " \t\r\n")
  244. (insert "\n\n")
  245. (delete-region (point) (point-max))
  246. (insert org-footnote-tag-for-non-org-mode-files "\n"))
  247. (goto-char (point-max))
  248. (skip-chars-backward " \t\r\n")
  249. (delete-region (point) (point-max))))
  250. (insert "\n\n\n")
  251. (backward-char 1)
  252. (insert "[" label "] ")
  253. (message "Edit definition and go back with `C-c &' or, if unique, with `C-c C-c'.")))
  254. ;;;###autoload
  255. (defun org-footnote-action (&optional special)
  256. "Do the right thing for footnotes.
  257. When at a foornote reference, jump to the definition. When at a definition,
  258. jump to the refernces. When neither at definition or reference,
  259. create a new footnote, interactively.
  260. With prefix arg SPECIAL, offer additional commands in a menu."
  261. (interactive "P")
  262. (let (tmp c)
  263. (cond
  264. (special
  265. (message "Footnotes: [s]ort | convert to [n]umeric | [d]elete")
  266. (setq c (read-char-exclusive))
  267. (cond
  268. ((equal c ?s)
  269. (org-footnote-normalize 'sort))
  270. ((equal c ?n)
  271. (org-footnote-normalize))
  272. ((equal c ?d)
  273. (org-footnote-delete))
  274. (t (error "No such footnote command %c" c))))
  275. ((setq tmp (org-footnote-at-reference-p))
  276. (if (nth 1 tmp)
  277. (org-footnote-goto-definition (nth 1 tmp))
  278. (goto-char (match-beginning 4))))
  279. ((setq tmp (org-footnote-at-definition-p))
  280. (org-footnote-goto-next-reference (nth 1 tmp)))
  281. (t (org-footnote-new)))))
  282. ;;;###autoload
  283. (defun org-footnote-normalize (&optional sort-only for-preprocessor)
  284. "Collect the footnotes in various formats and normalize them.
  285. This find the different sorts of footnotes allowed in Org, and
  286. normalizes them to the usual [N] format that is understood by the
  287. Org-mode exporters.
  288. When SORT-ONLY is set, only sort the footnote definitions into the
  289. referenced sequence."
  290. ;; This is based on Paul's function, but rewritten.
  291. (let ((count 0) ref def ref-table liste beg beg1 ref def marker a before
  292. ins-point)
  293. (save-excursion
  294. ;; Now find footnote references,
  295. (goto-char (point-min))
  296. (while (re-search-forward org-footnote-re nil t)
  297. (org-if-unprotected
  298. (setq def (match-string 4)
  299. ref (or (match-string 1) (match-string 2))
  300. before (char-to-string (char-after (match-beginning 0))))
  301. (if (equal ref "fn:") (setq ref nil))
  302. (if (and ref (setq a (assoc ref ref-table)))
  303. (setq marker (nth 1 a))
  304. (setq marker (number-to-string (incf count))))
  305. (save-match-data
  306. (if def
  307. (setq def (org-trim def))
  308. (save-excursion
  309. (if (not (re-search-forward (concat "^\\[" (regexp-quote ref)
  310. "\\]") nil t))
  311. (setq def
  312. (format "FOOTNOTE DEFINITION NOT FOUND: %s" ref))
  313. (setq beg (match-beginning 0))
  314. (setq beg1 (match-end 0))
  315. (re-search-forward
  316. (org-re "^[ \t]*$\\|^\\[\\([0-9]+\\|fn:[-_[:word:]]+\\)\\]")
  317. nil 'move)
  318. (setq def (buffer-substring beg1 (match-beginning 0)))
  319. (delete-region beg (match-beginning 0))))))
  320. (unless sort-only (replace-match (concat before "[" marker "]")))
  321. (if (not a) (push (list ref marker def) ref-table))))
  322. ;; First find and remove the footnote section
  323. (goto-char (point-min))
  324. (cond
  325. ((org-mode-p)
  326. (if (and org-footnote-section
  327. (re-search-forward
  328. (concat "^\\*[ \t]+" (regexp-quote org-footnote-section)
  329. "[ \t]*$")
  330. nil t))
  331. (if for-preprocessor
  332. (replace-match "")
  333. (org-back-to-heading t)
  334. (forward-line 1)
  335. (setq ins-point (point))
  336. (delete-region (point) (org-end-of-subtree t)))
  337. (goto-char (point-max))
  338. (unless for-preprocessor
  339. (when org-footnote-section
  340. (insert "* " org-footnote-section "\n")
  341. (setq ins-point (point))))))
  342. (t
  343. (if (re-search-forward
  344. (concat "^"
  345. (regexp-quote org-footnote-tag-for-non-org-mode-files)
  346. "[ \t]*$")
  347. nil t)
  348. (replace-match ""))
  349. (goto-char (point-max))
  350. (skip-chars-backward " \t\n\r")
  351. (delete-region (point) (point-max))
  352. (insert "\n\n" org-footnote-tag-for-non-org-mode-files "\n")
  353. (setq ins-point (point))))
  354. ;; Insert the footnotes again
  355. (goto-char (or ins-point (point-max)))
  356. (setq ref-table (reverse ref-table))
  357. (when sort-only
  358. (setq ref-table
  359. (delq nil (mapcar
  360. (lambda (x) (and (car x)
  361. (not (equal (car x) "fn:"))
  362. x))
  363. ref-table))))
  364. (setq def
  365. (mapconcat
  366. (lambda (x)
  367. (format "[%s] %s" (nth (if sort-only 0 1) x)
  368. (org-trim (nth 2 x))))
  369. ref-table "\n\n"))
  370. (if ref-table (insert "\n" def "\n\n")))))
  371. (defun org-footnote-delete (&optional label)
  372. "Delete the footnote at point.
  373. This will remove the definition (even multiple definitions if they exist)
  374. and all references of a footnote label."
  375. (catch 'done
  376. (let (x label l beg def-re (nref 0) (ndef 0))
  377. (unless label
  378. (when (setq x (org-footnote-at-reference-p))
  379. (setq label (nth 1 x))
  380. (when (or (not label) (equal "fn:" label))
  381. (delete-region (1+ (match-beginning 0)) (match-end 0))
  382. (message "Anonymous footnote removed")
  383. (throw 'done t)))
  384. (when (and (not label) (setq x (org-footnote-at-definition-p)))
  385. (setq label (nth 1 x)))
  386. (unless label (error "Don't know which footnote to remove")))
  387. (save-excursion
  388. (save-restriction
  389. (goto-char (point-min))
  390. (while (re-search-forward org-footnote-re nil t)
  391. (setq l (or (match-string 1) (match-string 2)))
  392. (when (equal l label)
  393. (delete-region (1+ (match-beginning 0)) (match-end 0))
  394. (incf nref)))
  395. (goto-char (point-min))
  396. (setq def-re (concat "^\\[" (regexp-quote label) "\\]"))
  397. (while (re-search-forward def-re nil t)
  398. (setq beg (match-beginning 0))
  399. (if (re-search-forward "^\\[\\|^[ \t]*$\\|^\\*+ " nil t)
  400. (goto-char (match-beginning 0))
  401. (goto-char (point-max)))
  402. (delete-region beg (point))
  403. (incf ndef))))
  404. (message "%d definition(s) of and %d reference(s) of footnote %s removed"
  405. ndef nref label))))
  406. (provide 'org-footnote)
  407. ;; arch-tag: 1b5954df-fb5d-4da5-8709-78d944dbfc37
  408. ;;; org-footnote.el ends here