org-footnote.el 21 KB

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