org-footnote.el 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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.34
  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-commented-line "org" ())
  36. (declare-function org-in-regexp "org" (re &optional nlines visually))
  37. (declare-function org-mark-ring-push "org" (&optional pos buffer))
  38. (declare-function outline-next-heading "outline")
  39. (declare-function org-trim "org" (s))
  40. (declare-function org-show-context "org" (&optional key))
  41. (declare-function org-back-to-heading "org" (&optional invisible-ok))
  42. (declare-function org-end-of-subtree "org" (&optional invisible-ok to-heading))
  43. (declare-function org-in-verbatim-emphasis "org" ())
  44. (declare-function org-inside-latex-macro-p "org" ())
  45. (defvar org-odd-levels-only) ;; defined in org.el
  46. (defconst org-footnote-re
  47. (concat "[^][\n]" ; to make sure it is not at the beginning of a line
  48. "\\["
  49. "\\(?:"
  50. "\\([0-9]+\\)"
  51. "\\|"
  52. (org-re "\\(fn:\\([-_[:word:]]+?\\)?\\)\\(?::\\([^\]]*?\\)\\)?")
  53. "\\)"
  54. "\\]")
  55. "Regular expression for matching footnotes.")
  56. (defconst org-footnote-definition-re
  57. (org-re "^\\(\\[\\([0-9]+\\|fn:[-_[:word:]]+\\)\\]\\)")
  58. "Regular expression matching the definition of a footnote.")
  59. (defcustom org-footnote-section "Footnotes"
  60. "Outline heading containing footnote definitions before export.
  61. This can be nil, to place footnotes locally at the end of the current
  62. outline node. If can also be the name of a special outline heading
  63. under which footnotes should be put.
  64. This variable defines the place where Org puts the definition
  65. automatically, i.e. when creating the footnote, and when sorting the notes.
  66. However, by hand you may place definitions *anywhere*.
  67. If this is a string, during export, all subtrees starting with this
  68. heading will be removed after extracting footnote definitions."
  69. :group 'org-footnotes
  70. :type '(choice
  71. (string :tag "Collect footnotes under heading")
  72. (const :tag "Define footnotes locally" nil)))
  73. (defcustom org-footnote-tag-for-non-org-mode-files "Footnotes:"
  74. "Tag marking the beginning of footnote section.
  75. The Org-mode footnote engine can be used in arbitrary text files as well
  76. as in Org-mode. Outside Org-mode, new footnotes are always placed at
  77. the end of the file. When you normalize the notes, any line containing
  78. only this tag will be removed, a new one will be inserted at the end
  79. of the file, followed by the collected and normalized footnotes."
  80. :group 'org-footnotes
  81. :type 'string)
  82. (defcustom org-footnote-define-inline nil
  83. "Non-nil means, define footnotes inline, at reference location.
  84. When nil, footnotes will be defined in a special section near
  85. the end of the document. When t, the [fn:label:definition] notation
  86. will be used to define the footnote at the reference position."
  87. :group 'org-footnote
  88. :type 'boolean)
  89. (defcustom org-footnote-auto-label t
  90. "Non-nil means, define automatically new labels for footnotes.
  91. Possible values are:
  92. nil prompt the user for each label
  93. t create unique labels of the form [fn:1], [fn:2], ...
  94. confirm like t, but let the user edit the created value. In particular,
  95. the label can be removed from the minibuffer, to create
  96. an anonymous footnote.
  97. plain Automatically create plain number labels like [1]"
  98. :group 'org-footnote
  99. :type '(choice
  100. (const :tag "Prompt for label" nil)
  101. (const :tag "Create automatic [fn:N]" t)
  102. (const :tag "Offer automatic [fn:N] for editing" confirm)
  103. (const :tag "Create automatic [N]" plain)))
  104. (defcustom org-footnote-auto-adjust nil
  105. "Non-nil means, automatically adjust footnotes after insert/delete.
  106. When this is t, after each insertion or deletion of a footnote,
  107. simple fn:N footnotes will be renumbered, and all footnotes will be sorted.
  108. If you want to have just sorting or just renumbering, set this variable
  109. to `sort' or `renumber'.
  110. The main values of this variable can be set with in-buffer options:
  111. #+STARTUP: fnadjust
  112. #+STARTUP: nofnadjust"
  113. :group 'org-footnote
  114. :type '(choice
  115. (const :tag "Renumber" renumber)
  116. (const :tag "Sort" sort)
  117. (const :tag "Renumber and Sort" t)))
  118. (defcustom org-footnote-fill-after-inline-note-extraction nil
  119. "Non-nil means, fill paragraphs after extracting footnotes.
  120. When extracting inline footnotes, the lengths of lines can change a lot.
  121. When this option is set, paragraphs from which an inline footnote has been
  122. extracted will be filled again."
  123. :group 'org-footnote
  124. :type 'boolean)
  125. (defun org-footnote-at-reference-p ()
  126. "Is the cursor at a footnote reference?
  127. If yes, return the beginning position, the label, and the definition, if local."
  128. (when (org-in-regexp org-footnote-re 15)
  129. (list (match-beginning 0)
  130. (or (match-string 1)
  131. (if (equal (match-string 2) "fn:") nil (match-string 2)))
  132. (match-string 4))))
  133. (defun org-footnote-at-definition-p ()
  134. "Is the cursor at a footnote definition.
  135. This matches only pure definitions like [1] or [fn:name] at the beginning
  136. of a line. It does not a references like [fn:name:definition], where the
  137. footnote text is included and defined locally.
  138. The return value will be nil if not at a footnote definition, and a list
  139. with start and label of the footnote if there is a definition at point."
  140. (save-excursion
  141. (end-of-line 1)
  142. (let ((lim (save-excursion (re-search-backward "^\\*+ \\|^[ \t]*$" nil t))))
  143. (when (re-search-backward org-footnote-definition-re lim t)
  144. (list (match-beginning 0) (match-string 2))))))
  145. (defun org-footnote-goto-definition (label)
  146. "Find the definition of the footnote with label LABEL."
  147. (interactive "sLabel: ")
  148. (org-mark-ring-push)
  149. (setq label (org-footnote-normalize-label label))
  150. (let ((re (format "^\\[%s\\]\\|.\\[%s:" label label))
  151. pos)
  152. (save-excursion
  153. (setq pos (or (re-search-forward re nil t)
  154. (and (goto-char (point-min))
  155. (re-search-forward re nil t))
  156. (and (progn (widen) t)
  157. (goto-char (point-min))
  158. (re-search-forward re nil t)))))
  159. (if (not pos)
  160. (error "Cannot find definition of footnote %s" label)
  161. (goto-char pos)
  162. (org-show-context 'link-search)
  163. (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: ")
  167. (org-mark-ring-push)
  168. (setq label (org-footnote-normalize-label label))
  169. (let ((re (format ".\\[%s[]:]" label))
  170. (p0 (point)) pos)
  171. (save-excursion
  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)
  176. (goto-char p0)
  177. (re-search-forward re nil t))
  178. (and (goto-char (point-min))
  179. (re-search-forward re nil t)))))
  180. (if pos
  181. (progn
  182. (goto-char pos)
  183. (org-show-context 'link-search))
  184. (error "Cannot find reference of footnote %s" label))))
  185. (defun org-footnote-normalize-label (label)
  186. (if (numberp label) (setq label (number-to-string label)))
  187. (if (not (string-match "^[0-9]+$\\|^$\\|^fn:" label))
  188. (setq label (concat "fn:" label)))
  189. label)
  190. (defun org-footnote-all-labels ()
  191. "Return list with all defined foot labels used in the buffer."
  192. (let (rtn l)
  193. (save-excursion
  194. (save-restriction
  195. (widen)
  196. (goto-char (point-min))
  197. (while (re-search-forward org-footnote-definition-re nil t)
  198. (setq l (org-match-string-no-properties 2))
  199. (and l (add-to-list 'rtn l)))
  200. (goto-char (point-min))
  201. (while (re-search-forward org-footnote-re nil t)
  202. (setq l (or (org-match-string-no-properties 1)
  203. (org-match-string-no-properties 2)))
  204. (and l (not (equal l "fn:")) (add-to-list 'rtn l)))))
  205. rtn))
  206. (defun org-footnote-unique-label (&optional current)
  207. "Return a new unique footnote label.
  208. The returns the firsts fn:N labels that is currently not used."
  209. (unless current (setq current (org-footnote-all-labels)))
  210. (let ((fmt (if (eq org-footnote-auto-label 'plain) "%d" "fn:%d"))
  211. (cnt 1))
  212. (while (member (format fmt cnt) current)
  213. (incf cnt))
  214. (format fmt cnt)))
  215. (defvar org-footnote-label-history nil
  216. "History of footnote labels entered in current buffer.")
  217. (make-variable-buffer-local 'org-footnote-label-history)
  218. (defun org-footnote-new ()
  219. "Insert a new footnote.
  220. This command prompts for a label. If this is a label referencing an
  221. existing label, only insert the label. If the footnote label is empty
  222. or new, let the user edit the definition of the footnote."
  223. (interactive)
  224. (let* ((labels (org-footnote-all-labels))
  225. (propose (org-footnote-unique-label labels))
  226. (label
  227. (if (member org-footnote-auto-label '(t plain))
  228. propose
  229. (completing-read
  230. "Label (leave empty for anonymous): "
  231. (mapcar 'list labels) nil nil
  232. (if (eq org-footnote-auto-label 'confirm) propose nil)
  233. 'org-footnote-label-history))))
  234. (setq label (org-footnote-normalize-label label))
  235. (cond
  236. ((equal label "")
  237. (insert "[fn:: ]")
  238. (backward-char 1))
  239. ((member label labels)
  240. (insert "[" label "]")
  241. (message "New reference to existing note"))
  242. (org-footnote-define-inline
  243. (insert "[" label ": ]")
  244. (backward-char 1)
  245. (org-footnote-auto-adjust-maybe))
  246. (t
  247. (insert "[" label "]")
  248. (org-footnote-create-definition label)
  249. (org-footnote-auto-adjust-maybe)))))
  250. (defun org-footnote-create-definition (label)
  251. "Start the definition of a footnote with label LABEL."
  252. (interactive "sLabel: ")
  253. (setq label (org-footnote-normalize-label label))
  254. (let (re)
  255. (cond
  256. ((org-mode-p)
  257. (if (not org-footnote-section)
  258. ;; No section, put footnote into the current outline node
  259. nil
  260. ;; Try to find or make the special node
  261. (setq re (concat "^\\*+[ \t]+" org-footnote-section "[ \t]*$"))
  262. (unless (or (re-search-forward re nil t)
  263. (and (progn (widen) t)
  264. (re-search-forward re nil t)))
  265. (goto-char (point-max))
  266. (insert "\n\n* " org-footnote-section "\n")))
  267. ;; Now go to the end of this entry and insert there.
  268. (org-footnote-goto-local-insertion-point)
  269. (org-show-context 'link-search))
  270. (t
  271. (setq re (concat "^" org-footnote-tag-for-non-org-mode-files "[ \t]*$"))
  272. (unless (re-search-forward re nil t)
  273. (goto-char (point-max))
  274. (skip-chars-backward " \t\r\n")
  275. (insert "\n\n")
  276. (delete-region (point) (point-max))
  277. (insert org-footnote-tag-for-non-org-mode-files "\n"))
  278. (goto-char (point-max))
  279. (skip-chars-backward " \t\r\n")))
  280. (insert "\n\n")
  281. (insert "[" label "] ")
  282. (message "Edit definition and go back with `C-c &' or, if unique, with `C-c C-c'.")))
  283. ;;;###autoload
  284. (defun org-footnote-action (&optional special)
  285. "Do the right thing for footnotes.
  286. When at a footnote reference, jump to the definition. When at a definition,
  287. jump to the references. When neither at definition or reference,
  288. create a new footnote, interactively.
  289. With prefix arg SPECIAL, offer additional commands in a menu."
  290. (interactive "P")
  291. (let (tmp c)
  292. (cond
  293. (special
  294. (message "Footnotes: [s]ort | [r]enumber fn:N | [S]=r+s |->[n]umeric | [d]elete")
  295. (setq c (read-char-exclusive))
  296. (cond
  297. ((equal c ?s)
  298. (org-footnote-normalize 'sort))
  299. ((equal c ?r)
  300. (org-footnote-renumber-fn:N))
  301. ((equal c ?S)
  302. (org-footnote-renumber-fn:N)
  303. (org-footnote-normalize 'sort))
  304. ((equal c ?n)
  305. (org-footnote-normalize))
  306. ((equal c ?d)
  307. (org-footnote-delete))
  308. (t (error "No such footnote command %c" c))))
  309. ((setq tmp (org-footnote-at-reference-p))
  310. (if (nth 1 tmp)
  311. (org-footnote-goto-definition (nth 1 tmp))
  312. (goto-char (match-beginning 4))))
  313. ((setq tmp (org-footnote-at-definition-p))
  314. (org-footnote-goto-next-reference (nth 1 tmp)))
  315. (t (org-footnote-new)))))
  316. ;;;###autoload
  317. (defun org-footnote-normalize (&optional sort-only for-preprocessor)
  318. "Collect the footnotes in various formats and normalize them.
  319. This finds the different sorts of footnotes allowed in Org, and
  320. normalizes them to the usual [N] format that is understood by the
  321. Org-mode exporters.
  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* ((limit-level
  326. (and (boundp 'org-inlinetask-min-level)
  327. org-inlinetask-min-level
  328. (1- org-inlinetask-min-level)))
  329. (nstars (and limit-level
  330. (if org-odd-levels-only
  331. (and limit-level (1- (* limit-level 2)))
  332. limit-level)))
  333. (outline-regexp
  334. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))
  335. (count 0)
  336. ref def idef ref-table beg beg1 marker a before ins-point)
  337. (save-excursion
  338. ;; Now find footnote references, and extract the definitions
  339. (goto-char (point-min))
  340. (while (re-search-forward org-footnote-re nil t)
  341. (unless (or (org-in-commented-line) (org-in-verbatim-emphasis)
  342. (org-inside-latex-macro-p))
  343. (org-if-unprotected
  344. (setq def (match-string 4)
  345. idef def
  346. ref (or (match-string 1) (match-string 2))
  347. before (char-to-string (char-after (match-beginning 0))))
  348. (if (equal ref "fn:") (setq ref nil))
  349. (if (and ref (setq a (assoc ref ref-table)))
  350. (progn
  351. (setq marker (nth 1 a))
  352. (unless (nth 2 a) (setf (caddr a) def)))
  353. (setq marker (number-to-string (incf count))))
  354. (save-match-data
  355. (if def
  356. (setq def (org-trim def))
  357. (save-excursion
  358. (goto-char (point-min))
  359. (if (not (re-search-forward (concat "^\\[" (regexp-quote ref)
  360. "\\]") nil t))
  361. (setq def nil)
  362. (setq beg (match-beginning 0))
  363. (setq beg1 (match-end 0))
  364. (re-search-forward
  365. (org-re "^[ \t]*$\\|^\\*+ \\|^\\[\\([0-9]+\\|fn:[-_[:word:]]+\\)\\]")
  366. nil 'move)
  367. (setq def (buffer-substring beg1 (or (match-beginning 0)
  368. (point-max))))
  369. (goto-char beg)
  370. (skip-chars-backward " \t\n\t")
  371. (delete-region (1+ (point)) (match-beginning 0))))))
  372. (unless sort-only
  373. (replace-match (concat before "[" marker "]") t t)
  374. (and idef
  375. org-footnote-fill-after-inline-note-extraction
  376. (fill-paragraph)))
  377. (if (not a) (push (list ref marker def (if idef t nil))
  378. ref-table)))))
  379. ;; First find and remove the footnote section
  380. (goto-char (point-min))
  381. (cond
  382. ((org-mode-p)
  383. (if (and org-footnote-section
  384. (re-search-forward
  385. (concat "^\\*[ \t]+" (regexp-quote org-footnote-section)
  386. "[ \t]*$")
  387. nil t))
  388. (if (or for-preprocessor (not org-footnote-section))
  389. (replace-match "")
  390. (org-back-to-heading t)
  391. (forward-line 1)
  392. (setq ins-point (point))
  393. (delete-region (point) (org-end-of-subtree t)))
  394. (goto-char (point-max))
  395. (unless for-preprocessor
  396. (when org-footnote-section
  397. (or (bolp) (insert "\n"))
  398. (insert "* " org-footnote-section "\n")
  399. (setq ins-point (point))))))
  400. (t
  401. (if (re-search-forward
  402. (concat "^"
  403. (regexp-quote org-footnote-tag-for-non-org-mode-files)
  404. "[ \t]*$")
  405. nil t)
  406. (replace-match ""))
  407. (goto-char (point-max))
  408. (skip-chars-backward " \t\n\r")
  409. (delete-region (point) (point-max))
  410. (insert "\n\n" org-footnote-tag-for-non-org-mode-files "\n")
  411. (setq ins-point (point))))
  412. ;; Insert the footnotes again
  413. (goto-char (or ins-point (point-max)))
  414. (setq ref-table (reverse ref-table))
  415. (when sort-only
  416. ;; remove anonymous and inline footnotes from the list
  417. (setq ref-table
  418. (delq nil (mapcar
  419. (lambda (x) (and (car x)
  420. (not (equal (car x) "fn:"))
  421. (not (nth 3 x))
  422. x))
  423. ref-table))))
  424. ;; Make sure each footnote has a description, or an error message.
  425. (setq ref-table
  426. (mapcar
  427. (lambda (x)
  428. (if (not (nth 2 x))
  429. (setcar (cddr x)
  430. (format "FOOTNOTE DEFINITION NOT FOUND: %s" (car x)))
  431. (setcar (cddr x) (org-trim (nth 2 x))))
  432. x)
  433. ref-table))
  434. (if (or (not (org-mode-p)) ; not an Org file
  435. org-footnote-section ; we do not use a footnote section
  436. (not sort-only) ; this is normalization
  437. for-preprocessor) ; the is the preprocessor
  438. ;; Insert the footnotes together in one place
  439. (progn
  440. (setq def
  441. (mapconcat
  442. (lambda (x)
  443. (format "[%s] %s" (nth (if sort-only 0 1) x)
  444. (org-trim (nth 2 x))))
  445. ref-table "\n\n"))
  446. (if ref-table (insert "\n" def "\n\n")))
  447. ;; Insert each footnote near the first reference
  448. ;; Happens only in Org files with no special footnote section,
  449. ;; and only when doing sorting
  450. (mapc 'org-insert-footnote-reference-near-definition
  451. ref-table)))))
  452. (defun org-insert-footnote-reference-near-definition (entry)
  453. "Find first reference of footnote ENTRY and insert the definition there.
  454. ENTRY is (fn-label num-mark definition)."
  455. (when (car entry)
  456. (goto-char (point-min))
  457. (when (re-search-forward (format ".\\[%s[]:]" (regexp-quote (car entry)))
  458. nil t)
  459. (org-footnote-goto-local-insertion-point)
  460. (insert (format "\n\n[%s] %s" (car entry) (nth 2 entry))))))
  461. (defun org-footnote-goto-local-insertion-point ()
  462. "Find insertion point for footnote, just before next outline heading."
  463. (org-with-limited-levels (outline-next-heading))
  464. (or (bolp) (newline))
  465. (beginning-of-line 0)
  466. (while (and (not (bobp)) (= (char-after) ?#))
  467. (beginning-of-line 0))
  468. (if (looking-at "[ \t]*#\\+TBLFM:") (beginning-of-line 2))
  469. (end-of-line 1)
  470. (skip-chars-backward "\n\r\t "))
  471. (defun org-footnote-delete (&optional label)
  472. "Delete the footnote at point.
  473. This will remove the definition (even multiple definitions if they exist)
  474. and all references of a footnote label."
  475. (catch 'done
  476. (let (x label l beg def-re (nref 0) (ndef 0))
  477. (unless label
  478. (when (setq x (org-footnote-at-reference-p))
  479. (setq label (nth 1 x))
  480. (when (or (not label) (equal "fn:" label))
  481. (delete-region (1+ (match-beginning 0)) (match-end 0))
  482. (message "Anonymous footnote removed")
  483. (throw 'done t)))
  484. (when (and (not label) (setq x (org-footnote-at-definition-p)))
  485. (setq label (nth 1 x)))
  486. (unless label (error "Don't know which footnote to remove")))
  487. (save-excursion
  488. (save-restriction
  489. (goto-char (point-min))
  490. (while (re-search-forward org-footnote-re nil t)
  491. (setq l (or (match-string 1) (match-string 2)))
  492. (when (equal l label)
  493. (delete-region (1+ (match-beginning 0)) (match-end 0))
  494. (incf nref)))
  495. (goto-char (point-min))
  496. (setq def-re (concat "^\\[" (regexp-quote label) "\\]"))
  497. (while (re-search-forward def-re nil t)
  498. (setq beg (match-beginning 0))
  499. (if (re-search-forward "^\\[\\|^[ \t]*$\\|^\\*+ " nil t)
  500. (goto-char (match-beginning 0))
  501. (goto-char (point-max)))
  502. (delete-region beg (point))
  503. (incf ndef))))
  504. (org-footnote-auto-adjust-maybe)
  505. (message "%d definition(s) of and %d reference(s) of footnote %s removed"
  506. ndef nref label))))
  507. (defun org-footnote-renumber-fn:N ()
  508. "Renumber the simple footnotes like fn:17 into a sequence in the document."
  509. (interactive)
  510. (let (map i (n 0))
  511. (save-excursion
  512. (save-restriction
  513. (widen)
  514. (goto-char (point-min))
  515. (while (re-search-forward "\\[fn:\\([0-9]+\\)[]:]" nil t)
  516. (setq i (string-to-number (match-string 1)))
  517. (when (and (string-match "\\S-" (buffer-substring
  518. (point-at-bol) (match-beginning 0)))
  519. (not (assq i map)))
  520. (push (cons i (number-to-string (incf n))) map)))
  521. (goto-char (point-min))
  522. (while (re-search-forward "\\(\\[fn:\\)\\([0-9]+\\)\\([]:]\\)" nil t)
  523. (replace-match (concat "\\1" (cdr (assq (string-to-number (match-string 2)) map)) "\\3")))))))
  524. (defun org-footnote-auto-adjust-maybe ()
  525. "Renumber and/or sort footnotes according to user settings."
  526. (when (memq org-footnote-auto-adjust '(t renumber))
  527. (org-footnote-renumber-fn:N))
  528. (when (memq org-footnote-auto-adjust '(t sort))
  529. (let ((label (nth 1 (org-footnote-at-definition-p))))
  530. (org-footnote-normalize 'sort)
  531. (when label
  532. (goto-char (point-min))
  533. (and (re-search-forward (concat "^\\[" (regexp-quote label) "\\]")
  534. nil t)
  535. (progn (insert " ")
  536. (just-one-space)))))))
  537. (provide 'org-footnote)
  538. ;; arch-tag: 1b5954df-fb5d-4da5-8709-78d944dbfc37
  539. ;;; org-footnote.el ends here