org-footnote.el 22 KB

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