org-footnote.el 21 KB

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