org-footnote.el 21 KB

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