org-footnote.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. (defun org-footnote-at-reference-p ()
  84. "Is the cursor at a footnote reference?
  85. If yes, return the beginning position, the label, and the definition, if local."
  86. (when (org-in-regexp org-footnote-re 15)
  87. (list (match-beginning 0)
  88. (or (match-string 1)
  89. (if (equal (match-string 2) "fn:") nil (match-string 2)))
  90. (match-string 4))))
  91. (defun org-footnote-at-definition-p ()
  92. "Is the cursor at a footnote definition.
  93. This matches only pure definitions like [1] or [fn:name] at the beginning
  94. of a line. It does not a references like [fn:name:definition], where the
  95. footnote text is included and defined locally.
  96. The return value will be nil if not at a foornote definition, and a list
  97. with start and label of the footnote if there is a definition at point."
  98. (save-excursion
  99. (end-of-line 1)
  100. (let ((lim (save-excursion (re-search-backward "^\\*+ \\|^[ \t]*$" nil t))))
  101. (when (re-search-backward org-footnote-definition-re lim t)
  102. (list (match-beginning 0) (match-string 2))))))
  103. (defun org-footnote-goto-definition (label)
  104. "Find the definition of the footnote with label LABEL."
  105. (interactive "sLabel: ")
  106. (org-mark-ring-push)
  107. (setq label (org-footnote-normalize-label label))
  108. (let ((re (format "^\\[%s\\]\\|.\\[%s:" label label))
  109. pos)
  110. (save-excursion
  111. (setq pos (or (re-search-forward re nil t)
  112. (and (goto-char (point-min))
  113. (re-search-forward re nil t))
  114. (and (progn (widen) t)
  115. (goto-char (point-min))
  116. (re-search-forward re nil t)))))
  117. (if (not pos)
  118. (error "Cannot find definition of footnote %s" label)
  119. (goto-char pos)
  120. (message "Edit definition and go back with `C-c &' or, if unique, with `C-c C-c'."))))
  121. (defun org-footnote-goto-next-reference (label)
  122. "Find the definition of the footnote with label LABEL."
  123. (interactive "sLabel: ")
  124. (org-mark-ring-push)
  125. (setq label (org-footnote-normalize-label label))
  126. (let ((re (format ".\\[%s[]:]" label))
  127. (p0 (point)) pos)
  128. (save-excursion
  129. (setq pos (or (re-search-forward re nil t)
  130. (and (goto-char (point-min))
  131. (re-search-forward re nil t))
  132. (and (progn (widen) t)
  133. (goto-char p0)
  134. (re-search-forward re nil t))
  135. (and (goto-char (point-min))
  136. (re-search-forward re nil t)))))
  137. (if pos (goto-char pos)
  138. (error "Cannot find reference of footnote %s" label))))
  139. (defun org-footnote-normalize-label (label)
  140. (if (numberp label) (setq label (number-to-string label)))
  141. (if (not (string-match "^[0-9]+$\\|^$\\|^fn:" label))
  142. (setq label (concat "fn:" label)))
  143. label)
  144. (defun org-footnote-all-labels ()
  145. "Return list with all defined foot labels used in the buffer."
  146. (let (rtn l)
  147. (save-excursion
  148. (save-restriction
  149. (widen)
  150. (goto-char (point-min))
  151. (while (re-search-forward org-footnote-definition-re nil t)
  152. (setq l (org-match-string-no-properties 2))
  153. (and l (add-to-list 'rtn l)))
  154. (goto-char (point-min))
  155. (while (re-search-forward org-footnote-re nil t)
  156. (setq l (or (org-match-string-no-properties 1)
  157. (org-match-string-no-properties 2)))
  158. (and l (not (equal l "fn:")) (add-to-list 'rtn l)))))
  159. rtn))
  160. (defun org-footnote-new ()
  161. "Insert a new footnote.
  162. This command prompts for a label. If this is a label referencing an
  163. existing label, only insert the label. If the footnote label is empty
  164. or new, let the user edit the definition of the footnote."
  165. (interactive)
  166. (let* ((labels (org-footnote-all-labels))
  167. (label (completing-read
  168. "Label (leave empty for anonymous): "
  169. (mapcar 'list labels))))
  170. (setq label (org-footnote-normalize-label label))
  171. (cond
  172. ((equal label "")
  173. (insert "[fn:: ]")
  174. (backward-char 1))
  175. ((member label labels)
  176. (insert "[" label "]")
  177. (message "New reference to existing note"))
  178. (org-footnote-define-inline
  179. (insert "[" label ": ]")
  180. (backward-char 1))
  181. (t
  182. (insert "[" label "]")
  183. (org-footnote-create-definition label)))))
  184. (defun org-footnote-create-definition (label)
  185. "Start the definition of a footnote with label LABEL."
  186. (interactive "sLabel: ")
  187. (setq label (org-footnote-normalize-label label))
  188. (let (re p)
  189. (cond
  190. ((org-mode-p)
  191. (if (not org-footnote-section)
  192. ;; No section, put foornote into the curren outline node
  193. nil
  194. ;; Try to find or make the special node
  195. (setq re (concat "^\\*+[ \t]+" org-footnote-section "[ \t]*$"))
  196. (unless (or (re-search-forward re nil t)
  197. (and (progn (widen) t)
  198. (re-search-forward re nil t)))
  199. (goto-char (point-max))
  200. (insert "\n\n* " org-footnote-section)))
  201. ;; Now go to the end of this entry and insert there.
  202. (outline-next-heading)
  203. (setq p (point))
  204. (skip-chars-backward " \t\n\r")
  205. (delete-region (point) p))
  206. (t
  207. (setq re (concat "^" org-footnote-tag-for-non-org-mode-files "[ \t]*$"))
  208. (unless (re-search-forward re nil t)
  209. (goto-char (point-max))
  210. (skip-chars-backward " \t\r\n")
  211. (insert "\n\n")
  212. (delete-region (point) (point-max))
  213. (insert org-footnote-tag-for-non-org-mode-files "\n"))
  214. (goto-char (point-max))
  215. (skip-chars-backward " \t\r\n")
  216. (delete-region (point) (point-max))))
  217. (insert "\n\n\n")
  218. (backward-char 1)
  219. (insert "[" label "] ")
  220. (message "Edit definition and go back with `C-c &' or, if unique, with `C-c C-c'.")))
  221. ;;;###autoload
  222. (defun org-footnote-action (&optional special)
  223. "Do the right thing for footnotes.
  224. When at a foornote reference, jump to the definition. When at a definition,
  225. jump to the refernces. When neither at definition or reference,
  226. create a new footnote, interactively.
  227. With prefix arg SPECIAL, offer additional commands in a menu."
  228. (interactive "P")
  229. (let (tmp c)
  230. (cond
  231. (special
  232. (message "Footnotes: [s]ort | convert to [n]umeric | [d]elete")
  233. (setq c (read-char-exclusive))
  234. (cond
  235. ((equal c ?s)
  236. (org-footnote-normalize 'sort))
  237. ((equal c ?n)
  238. (org-footnote-normalize))
  239. ((equal c ?d)
  240. (org-footnote-delete))
  241. (t (error "No such footnote command %c" c))))
  242. ((setq tmp (org-footnote-at-reference-p))
  243. (if (nth 1 tmp)
  244. (org-footnote-goto-definition (nth 1 tmp))
  245. (goto-char (match-beginning 4))))
  246. ((setq tmp (org-footnote-at-definition-p))
  247. (org-footnote-goto-next-reference (nth 1 tmp)))
  248. (t (org-footnote-new)))))
  249. ;;;###autoload
  250. (defun org-footnote-normalize (&optional sort-only for-preprocessor)
  251. "Collect the footnotes in various formats and normalize them.
  252. This find the different sorts of footnotes allowed in Org, and
  253. normalizes them to the usual [N] format that is understood by the
  254. Org-mode exporters.
  255. When SORT-ONLY is set, only sort the footnote definitions into the
  256. referenced sequence."
  257. ;; This is based on Paul's function, but rewritten.
  258. (let ((count 0) ref def ref-table liste beg beg1 ref def marker a before
  259. ins-point)
  260. (save-excursion
  261. ;; Now find footnote references,
  262. (goto-char (point-min))
  263. (while (re-search-forward org-footnote-re nil t)
  264. (org-if-unprotected
  265. (setq def (match-string 4)
  266. ref (or (match-string 1) (match-string 2))
  267. before (char-to-string (char-after (match-beginning 0))))
  268. (if (equal ref "fn:") (setq ref nil))
  269. (if (and ref (setq a (assoc ref ref-table)))
  270. (setq marker (nth 1 a))
  271. (setq marker (number-to-string (incf count))))
  272. (save-match-data
  273. (if def
  274. (setq def (org-trim def))
  275. (save-excursion
  276. (if (not (re-search-forward (concat "^\\[" (regexp-quote ref)
  277. "\\]") nil t))
  278. (setq def
  279. (format "FOOTNOTE DEFINITION NOT FOUND: %s" ref))
  280. (setq beg (match-beginning 0))
  281. (setq beg1 (match-end 0))
  282. (re-search-forward
  283. (org-re "^[ \t]*$\\|^\\[\\([0-9]+\\|fn:[-_[:word:]]+\\)\\]")
  284. nil 'move)
  285. (setq def (buffer-substring beg1 (match-beginning 0)))
  286. (delete-region beg (match-beginning 0))))))
  287. (unless sort-only (replace-match (concat before "[" marker "]")))
  288. (if (not a) (push (list ref marker def) ref-table))))
  289. ;; First find and remove the footnote section
  290. (goto-char (point-min))
  291. (cond
  292. ((org-mode-p)
  293. (if (and org-footnote-section
  294. (re-search-forward
  295. (concat "^\\*[ \t]+" (regexp-quote org-footnote-section)
  296. "[ \t]*$")
  297. nil t))
  298. (if for-preprocessor
  299. (replace-match "")
  300. (org-back-to-heading t)
  301. (forward-line 1)
  302. (setq ins-point (point))
  303. (delete-region (point) (org-end-of-subtree t)))
  304. (goto-char (point-max))
  305. (unless for-preprocessor
  306. (when org-footnote-section
  307. (insert "* " org-footnote-section "\n")
  308. (setq ins-point (point))))))
  309. (t
  310. (if (re-search-forward
  311. (concat "^"
  312. (regexp-quote org-footnote-tag-for-non-org-mode-files)
  313. "[ \t]*$")
  314. nil t)
  315. (replace-match ""))
  316. (goto-char (point-max))
  317. (skip-chars-backward " \t\n\r")
  318. (delete-region (point) (point-max))
  319. (insert "\n\n" org-footnote-tag-for-non-org-mode-files "\n")
  320. (setq ins-point (point))))
  321. ;; Insert the footnotes again
  322. (goto-char (or ins-point (point-max)))
  323. (setq ref-table (reverse ref-table))
  324. (when sort-only
  325. (setq ref-table
  326. (delq nil (mapcar
  327. (lambda (x) (and (car x)
  328. (not (equal (car x) "fn:"))
  329. x))
  330. ref-table))))
  331. (setq def
  332. (mapconcat
  333. (lambda (x)
  334. (format "[%s] %s" (nth (if sort-only 0 1) x)
  335. (org-trim (nth 2 x))))
  336. ref-table "\n\n"))
  337. (if ref-table (insert "\n" def "\n\n")))))
  338. (defun org-footnote-delete (&optional label)
  339. "Delete the footnote at point.
  340. This will remove the definition (even multiple definitions if they exist)
  341. and all references of a footnote label."
  342. (catch 'done
  343. (let (x label l beg def-re (nref 0) (ndef 0))
  344. (unless label
  345. (when (setq x (org-footnote-at-reference-p))
  346. (setq label (nth 1 x))
  347. (when (or (not label) (equal "fn:" label))
  348. (delete-region (1+ (match-beginning 0)) (match-end 0))
  349. (message "Anonymous footnote removed")
  350. (throw 'done t)))
  351. (when (and (not label) (setq x (org-footnote-at-definition-p)))
  352. (setq label (nth 1 x)))
  353. (unless label (error "Don't know which footnote to remove")))
  354. (save-excursion
  355. (save-restriction
  356. (goto-char (point-min))
  357. (while (re-search-forward org-footnote-re nil t)
  358. (setq l (or (match-string 1) (match-string 2)))
  359. (when (equal l label)
  360. (delete-region (1+ (match-beginning 0)) (match-end 0))
  361. (incf nref)))
  362. (goto-char (point-min))
  363. (setq def-re (concat "^\\[" (regexp-quote label) "\\]"))
  364. (while (re-search-forward def-re nil t)
  365. (setq beg (match-beginning 0))
  366. (if (re-search-forward "^\\[\\|^[ \t]*$\\|^\\*+ " nil t)
  367. (goto-char (match-beginning 0))
  368. (goto-char (point-max)))
  369. (delete-region beg (point))
  370. (incf ndef))))
  371. (message "%d definition(s) of and %d reference(s) of footnote %s removed"
  372. ndef nref label))))
  373. (provide 'org-footnote)
  374. ;; arch-tag: 1b5954df-fb5d-4da5-8709-78d944dbfc37
  375. ;;; org-footnote.el ends here