org-footnote.el 21 KB

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