org-footnote.el 21 KB

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