footnote.el 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. ;;; footnote.el --- footnote support for message mode -*- coding: utf-8;-*-
  2. ;; Copyright (C) 1997, 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Steven L Baur <steve@xemacs.org>
  4. ;; Keywords: mail, news
  5. ;; Version: 0.19
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This file provides footnote[1] support for message-mode in emacsen.
  19. ;; footnote-mode is implemented as a minor mode.
  20. ;; [1] Footnotes look something like this. Along with some decorative
  21. ;; stuff.
  22. ;; TODO:
  23. ;; Reasonable Undo support.
  24. ;; more language styles.
  25. ;;; Code:
  26. (eval-when-compile
  27. (require 'cl)
  28. (defvar filladapt-token-table))
  29. (defgroup footnote nil
  30. "Support for footnotes in mail and news messages."
  31. :version "21.1"
  32. :group 'message)
  33. (defcustom footnote-mode-line-string " FN"
  34. "String to display in modes section of the mode-line."
  35. :group 'footnote)
  36. (defcustom footnote-mode-hook nil
  37. "Hook functions run when footnote-mode is activated."
  38. :type 'hook
  39. :group 'footnote)
  40. (defcustom footnote-narrow-to-footnotes-when-editing nil
  41. "If non-nil, narrow to footnote text body while editing a footnote."
  42. :type 'boolean
  43. :group 'footnote)
  44. (defcustom footnote-prompt-before-deletion t
  45. "If non-nil, prompt before deleting a footnote.
  46. There is currently no way to undo deletions."
  47. :type 'boolean
  48. :group 'footnote)
  49. (defcustom footnote-spaced-footnotes t
  50. "If non-nil, insert an empty line between footnotes.
  51. Customizing this variable has no effect on buffers already
  52. displaying footnotes."
  53. :type 'boolean
  54. :group 'footnote)
  55. (defcustom footnote-use-message-mode t ; Nowhere used.
  56. "If non-nil, assume Footnoting will be done in `message-mode'."
  57. :type 'boolean
  58. :group 'footnote)
  59. (defcustom footnote-body-tag-spacing 2
  60. "Number of spaces separating a footnote body tag and its text.
  61. Customizing this variable has no effect on buffers already
  62. displaying footnotes."
  63. :type 'integer
  64. :group 'footnote)
  65. (defcustom footnote-prefix [(control ?c) ?!]
  66. "Prefix key to use for Footnote command in Footnote minor mode.
  67. The value of this variable is checked as part of loading Footnote mode.
  68. After that, changing the prefix key requires manipulating keymaps."
  69. ;; FIXME: the type should be a key-sequence, but it seems Custom
  70. ;; doesn't support that yet.
  71. ;; :type 'string
  72. )
  73. ;;; Interface variables that probably shouldn't be changed
  74. (defcustom footnote-section-tag "Footnotes: "
  75. "Tag inserted at beginning of footnote section.
  76. If you set this to the empty string, no tag is inserted and the
  77. value of `footnote-section-tag-regexp' is ignored. Customizing
  78. this variable has no effect on buffers already displaying
  79. footnotes."
  80. :type 'string
  81. :group 'footnote)
  82. (defcustom footnote-section-tag-regexp "Footnotes\\(\\[.\\]\\)?: "
  83. "Regexp which indicates the start of a footnote section.
  84. This variable is disregarded when `footnote-section-tag' is the
  85. empty string. Customizing this variable has no effect on buffers
  86. already displaying footnotes."
  87. :type 'regexp
  88. :group 'footnote)
  89. ;; The following three should be consumed by footnote styles.
  90. (defcustom footnote-start-tag "["
  91. "String used to denote start of numbered footnote.
  92. Should not be set to the empty string. Customizing this variable
  93. has no effect on buffers already displaying footnotes."
  94. :type 'string
  95. :group 'footnote)
  96. (defcustom footnote-end-tag "]"
  97. "String used to denote end of numbered footnote.
  98. Should not be set to the empty string. Customizing this variable
  99. has no effect on buffers already displaying footnotes."
  100. :type 'string
  101. :group 'footnote)
  102. (defvar footnote-signature-separator (if (boundp 'message-signature-separator)
  103. message-signature-separator
  104. "^-- $")
  105. "*String used to recognize .signatures.")
  106. ;;; Private variables
  107. (defvar footnote-style-number nil
  108. "Footnote style represented as an index into footnote-style-alist.")
  109. (make-variable-buffer-local 'footnote-style-number)
  110. (defvar footnote-text-marker-alist nil
  111. "List of markers pointing to text of footnotes in message buffer.")
  112. (make-variable-buffer-local 'footnote-text-marker-alist)
  113. (defvar footnote-pointer-marker-alist nil
  114. "List of markers pointing to footnote pointers in message buffer.")
  115. (make-variable-buffer-local 'footnote-pointer-marker-alist)
  116. (defvar footnote-mouse-highlight 'highlight
  117. "Text property name to enable mouse over highlight.")
  118. ;;; Default styles
  119. ;;; NUMERIC
  120. (defconst footnote-numeric-regexp "[0-9]+"
  121. "Regexp for digits.")
  122. (defun Footnote-numeric (n)
  123. "Numeric footnote style.
  124. Use Arabic numerals for footnoting."
  125. (int-to-string n))
  126. ;;; ENGLISH UPPER
  127. (defconst footnote-english-upper "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  128. "Upper case English alphabet.")
  129. (defconst footnote-english-upper-regexp "[A-Z]+"
  130. "Regexp for upper case English alphabet.")
  131. (defun Footnote-english-upper (n)
  132. "Upper case English footnoting.
  133. Wrapping around the alphabet implies successive repetitions of letters."
  134. (let* ((ltr (mod (1- n) (length footnote-english-upper)))
  135. (rep (/ (1- n) (length footnote-english-upper)))
  136. (chr (char-to-string (aref footnote-english-upper ltr)))
  137. rc)
  138. (while (>= rep 0)
  139. (setq rc (concat rc chr))
  140. (setq rep (1- rep)))
  141. rc))
  142. ;;; ENGLISH LOWER
  143. (defconst footnote-english-lower "abcdefghijklmnopqrstuvwxyz"
  144. "Lower case English alphabet.")
  145. (defconst footnote-english-lower-regexp "[a-z]+"
  146. "Regexp of lower case English alphabet.")
  147. (defun Footnote-english-lower (n)
  148. "Lower case English footnoting.
  149. Wrapping around the alphabet implies successive repetitions of letters."
  150. (let* ((ltr (mod (1- n) (length footnote-english-lower)))
  151. (rep (/ (1- n) (length footnote-english-lower)))
  152. (chr (char-to-string (aref footnote-english-lower ltr)))
  153. rc)
  154. (while (>= rep 0)
  155. (setq rc (concat rc chr))
  156. (setq rep (1- rep)))
  157. rc))
  158. ;;; ROMAN LOWER
  159. (defconst footnote-roman-lower-list
  160. '((1 . "i") (5 . "v") (10 . "x")
  161. (50 . "l") (100 . "c") (500 . "d") (1000 . "m"))
  162. "List of roman numerals with their values.")
  163. (defconst footnote-roman-lower-regexp "[ivxlcdm]+"
  164. "Regexp of roman numerals.")
  165. (defun Footnote-roman-lower (n)
  166. "Generic Roman number footnoting."
  167. (Footnote-roman-common n footnote-roman-lower-list))
  168. ;;; ROMAN UPPER
  169. (defconst footnote-roman-upper-list
  170. '((1 . "I") (5 . "V") (10 . "X")
  171. (50 . "L") (100 . "C") (500 . "D") (1000 . "M"))
  172. "List of roman numerals with their values.")
  173. (defconst footnote-roman-upper-regexp "[IVXLCDM]+"
  174. "Regexp of roman numerals. Not complete")
  175. (defun Footnote-roman-upper (n)
  176. "Generic Roman number footnoting."
  177. (Footnote-roman-common n footnote-roman-upper-list))
  178. (defun Footnote-roman-common (n footnote-roman-list)
  179. "Lower case Roman footnoting."
  180. (let* ((our-list footnote-roman-list)
  181. (rom-lngth (length our-list))
  182. (rom-high 0)
  183. (rom-low 0)
  184. (rom-div -1)
  185. (count-high 0)
  186. (count-low 0))
  187. ;; find surrounding numbers
  188. (while (and (<= count-high (1- rom-lngth))
  189. (>= n (car (nth count-high our-list))))
  190. ;; (message "Checking %d" (car (nth count-high our-list)))
  191. (setq count-high (1+ count-high)))
  192. (setq rom-high count-high)
  193. (setq rom-low (1- count-high))
  194. ;; find the appropriate divisor (if it exists)
  195. (while (and (= rom-div -1)
  196. (< count-low rom-high))
  197. (when (or (> n (- (car (nth rom-high our-list))
  198. (/ (car (nth count-low our-list))
  199. 2)))
  200. (= n (- (car (nth rom-high our-list))
  201. (car (nth count-low our-list)))))
  202. (setq rom-div count-low))
  203. ;; (message "Checking %d and %d in div loop" rom-high count-low)
  204. (setq count-low (1+ count-low)))
  205. ;;(message "We now have high: %d, low: %d, div: %d, n: %d"
  206. ;; rom-high rom-low (if rom-div rom-div -1) n)
  207. (let ((rom-low-pair (nth rom-low our-list))
  208. (rom-high-pair (nth rom-high our-list))
  209. (rom-div-pair (if (not (= rom-div -1)) (nth rom-div our-list) nil)))
  210. ;; (message "pairs are: rom-low: %S, rom-high: %S, rom-div: %S"
  211. ;; rom-low-pair rom-high-pair rom-div-pair)
  212. (cond
  213. ((< n 0) (error "Footnote-roman-common called with n < 0"))
  214. ((= n 0) "")
  215. ((= n (car rom-low-pair)) (cdr rom-low-pair))
  216. ((= n (car rom-high-pair)) (cdr rom-high-pair))
  217. ((= (car rom-low-pair) (car rom-high-pair))
  218. (concat (cdr rom-low-pair)
  219. (Footnote-roman-common
  220. (- n (car rom-low-pair))
  221. footnote-roman-list)))
  222. ((>= rom-div 0) (concat (cdr rom-div-pair) (cdr rom-high-pair)
  223. (Footnote-roman-common
  224. (- n (- (car rom-high-pair)
  225. (car rom-div-pair)))
  226. footnote-roman-list)))
  227. (t (concat (cdr rom-low-pair)
  228. (Footnote-roman-common
  229. (- n (car rom-low-pair))
  230. footnote-roman-list)))))))
  231. ;; Latin-1
  232. (defconst footnote-latin-string "¹²³ºª§¶"
  233. "String of Latin-1 footnoting characters.")
  234. ;; Note not [...]+, because this style cycles.
  235. (defconst footnote-latin-regexp (concat "[" footnote-latin-string "]")
  236. "Regexp for Latin-1 footnoting characters.")
  237. (defun Footnote-latin (n)
  238. "Latin-1 footnote style.
  239. Use a range of Latin-1 non-ASCII characters for footnoting."
  240. (string (aref footnote-latin-string
  241. (mod (1- n) (length footnote-latin-string)))))
  242. ;; Unicode
  243. (defconst footnote-unicode-string "⁰¹²³⁴⁵⁶⁷⁸⁹"
  244. "String of Unicode footnoting characters.")
  245. (defconst footnote-unicode-regexp (concat "[" footnote-unicode-string "]+")
  246. "Regexp for Unicode footnoting characters.")
  247. (defun Footnote-unicode (n)
  248. "Unicode footnote style.
  249. Use Unicode characters for footnoting."
  250. (let (modulus result done)
  251. (while (not done)
  252. (setq modulus (mod n 10)
  253. n (truncate n 10))
  254. (and (zerop n) (setq done t))
  255. (push (aref footnote-unicode-string modulus) result))
  256. (apply #'string result)))
  257. ;;; list of all footnote styles
  258. (defvar footnote-style-alist
  259. `((numeric Footnote-numeric ,footnote-numeric-regexp)
  260. (english-lower Footnote-english-lower ,footnote-english-lower-regexp)
  261. (english-upper Footnote-english-upper ,footnote-english-upper-regexp)
  262. (roman-lower Footnote-roman-lower ,footnote-roman-lower-regexp)
  263. (roman-upper Footnote-roman-upper ,footnote-roman-upper-regexp)
  264. (latin Footnote-latin ,footnote-latin-regexp)
  265. (unicode Footnote-unicode ,footnote-unicode-regexp))
  266. "Styles of footnote tags available.
  267. By default only boring Arabic numbers, English letters and Roman Numerals
  268. are available.
  269. See footnote-han.el, footnote-greek.el and footnote-hebrew.el for more
  270. exciting styles.")
  271. (defcustom footnote-style 'numeric
  272. "Default style used for footnoting.
  273. numeric == 1, 2, 3, ...
  274. english-lower == a, b, c, ...
  275. english-upper == A, B, C, ...
  276. roman-lower == i, ii, iii, iv, v, ...
  277. roman-upper == I, II, III, IV, V, ...
  278. latin == ¹ ² ³ º ª § ¶
  279. unicode == ¹, ², ³, ...
  280. See also variables `footnote-start-tag' and `footnote-end-tag'.
  281. Note: some characters in the unicode style may not show up
  282. properly if the default font does not contain those characters.
  283. Customizing this variable has no effect on buffers already
  284. displaying footnotes. To change the style of footnotes in such a
  285. buffer use the command `Footnote-set-style'."
  286. :type (cons 'choice (mapcar (lambda (x) (list 'const (car x)))
  287. footnote-style-alist))
  288. :group 'footnote)
  289. ;;; Style utilities & functions
  290. (defun Footnote-style-p (style)
  291. "Return non-nil if style is a valid style known to `footnote-mode'."
  292. (assq style footnote-style-alist))
  293. (defun Footnote-index-to-string (index)
  294. "Convert a binary index into a string to display as a footnote.
  295. Conversion is done based upon the current selected style."
  296. (let ((alist (if (Footnote-style-p footnote-style)
  297. (assq footnote-style footnote-style-alist)
  298. (nth 0 footnote-style-alist))))
  299. (funcall (nth 1 alist) index)))
  300. (defun Footnote-current-regexp ()
  301. "Return the regexp of the index of the current style."
  302. (concat (nth 2 (or (assq footnote-style footnote-style-alist)
  303. (nth 0 footnote-style-alist)))
  304. "*"))
  305. (defun Footnote-refresh-footnotes (&optional index-regexp)
  306. "Redraw all footnotes.
  307. You must call this or arrange to have this called after changing footnote
  308. styles."
  309. (unless index-regexp
  310. (setq index-regexp (Footnote-current-regexp)))
  311. (save-excursion
  312. ;; Take care of the pointers first
  313. (let ((i 0) locn alist)
  314. (while (setq alist (nth i footnote-pointer-marker-alist))
  315. (setq locn (cdr alist))
  316. (while locn
  317. (goto-char (car locn))
  318. ;; Try to handle the case where `footnote-start-tag' and
  319. ;; `footnote-end-tag' are the same string.
  320. (when (looking-back (concat
  321. (regexp-quote footnote-start-tag)
  322. "\\(" index-regexp "+\\)"
  323. (regexp-quote footnote-end-tag))
  324. (line-beginning-position))
  325. (replace-match
  326. (propertize
  327. (concat
  328. footnote-start-tag
  329. (Footnote-index-to-string (1+ i))
  330. footnote-end-tag)
  331. 'footnote-number (1+ i) footnote-mouse-highlight t)
  332. nil "\\1"))
  333. (setq locn (cdr locn)))
  334. (setq i (1+ i))))
  335. ;; Now take care of the text section
  336. (let ((i 0) alist)
  337. (while (setq alist (nth i footnote-text-marker-alist))
  338. (goto-char (cdr alist))
  339. (when (looking-at (concat
  340. (regexp-quote footnote-start-tag)
  341. "\\(" index-regexp "+\\)"
  342. (regexp-quote footnote-end-tag)))
  343. (replace-match
  344. (propertize
  345. (concat
  346. footnote-start-tag
  347. (Footnote-index-to-string (1+ i))
  348. footnote-end-tag)
  349. 'footnote-number (1+ i))
  350. nil "\\1"))
  351. (setq i (1+ i))))))
  352. (defun Footnote-assoc-index (key alist)
  353. "Give index of key in alist."
  354. (let ((i 0) (max (length alist)) rc)
  355. (while (and (null rc)
  356. (< i max))
  357. (when (eq key (car (nth i alist)))
  358. (setq rc i))
  359. (setq i (1+ i)))
  360. rc))
  361. (defun Footnote-cycle-style ()
  362. "Select next defined footnote style."
  363. (interactive)
  364. (let ((old (Footnote-assoc-index footnote-style footnote-style-alist))
  365. (max (length footnote-style-alist))
  366. idx)
  367. (setq idx (1+ old))
  368. (when (>= idx max)
  369. (setq idx 0))
  370. (setq footnote-style (car (nth idx footnote-style-alist)))
  371. (Footnote-refresh-footnotes (nth 2 (nth old footnote-style-alist)))))
  372. (defun Footnote-set-style (&optional style)
  373. "Select a specific style."
  374. (interactive
  375. (list (intern (completing-read
  376. "Footnote Style: "
  377. obarray #'Footnote-style-p 'require-match))))
  378. (let ((old (Footnote-assoc-index footnote-style footnote-style-alist)))
  379. (setq footnote-style style)
  380. (Footnote-refresh-footnotes (nth 2 (nth old footnote-style-alist)))))
  381. ;; Internal functions
  382. (defun Footnote-insert-numbered-footnote (arg &optional mousable)
  383. "Insert numbered footnote at (point)."
  384. (let ((string (concat footnote-start-tag
  385. (Footnote-index-to-string arg)
  386. footnote-end-tag)))
  387. (insert-before-markers
  388. (if mousable
  389. (propertize
  390. string 'footnote-number arg footnote-mouse-highlight t)
  391. (propertize string 'footnote-number arg)))))
  392. (defun Footnote-renumber (from to pointer-alist text-alist)
  393. "Renumber a single footnote."
  394. (let* ((posn-list (cdr pointer-alist)))
  395. (setcar pointer-alist to)
  396. (setcar text-alist to)
  397. (while posn-list
  398. (goto-char (car posn-list))
  399. (when (looking-back (concat (regexp-quote footnote-start-tag)
  400. (Footnote-current-regexp)
  401. (regexp-quote footnote-end-tag))
  402. (line-beginning-position))
  403. (replace-match
  404. (propertize
  405. (concat footnote-start-tag
  406. (Footnote-index-to-string to)
  407. footnote-end-tag)
  408. 'footnote-number to footnote-mouse-highlight t)))
  409. (setq posn-list (cdr posn-list)))
  410. (goto-char (cdr text-alist))
  411. (when (looking-at (concat (regexp-quote footnote-start-tag)
  412. (Footnote-current-regexp)
  413. (regexp-quote footnote-end-tag)))
  414. (replace-match
  415. (propertize
  416. (concat footnote-start-tag
  417. (Footnote-index-to-string to)
  418. footnote-end-tag)
  419. 'footnote-number to)))))
  420. ;; Not needed?
  421. (defun Footnote-narrow-to-footnotes ()
  422. "Restrict text in buffer to show only text of footnotes."
  423. (interactive) ; testing
  424. (goto-char (point-max))
  425. (when (re-search-backward footnote-signature-separator nil t)
  426. (let ((end (point)))
  427. (cond
  428. ((and (not (string-equal footnote-section-tag ""))
  429. (re-search-backward
  430. (concat "^" footnote-section-tag-regexp) nil t))
  431. (narrow-to-region (point) end))
  432. (footnote-text-marker-alist
  433. (narrow-to-region (cdar footnote-text-marker-alist) end))))))
  434. (defun Footnote-goto-char-point-max ()
  435. "Move to end of buffer or prior to start of .signature."
  436. (goto-char (point-max))
  437. (or (re-search-backward footnote-signature-separator nil t)
  438. (point)))
  439. (defun Footnote-insert-text-marker (arg locn)
  440. "Insert a marker pointing to footnote ARG, at buffer location LOCN."
  441. (let ((marker (make-marker)))
  442. (unless (assq arg footnote-text-marker-alist)
  443. (set-marker marker locn)
  444. (setq footnote-text-marker-alist
  445. (cons (cons arg marker) footnote-text-marker-alist))
  446. (setq footnote-text-marker-alist
  447. (Footnote-sort footnote-text-marker-alist)))))
  448. (defun Footnote-insert-pointer-marker (arg locn)
  449. "Insert a marker pointing to footnote ARG, at buffer location LOCN."
  450. (let ((marker (make-marker))
  451. alist)
  452. (set-marker marker locn)
  453. (if (setq alist (assq arg footnote-pointer-marker-alist))
  454. (setf alist
  455. (cons marker (cdr alist)))
  456. (setq footnote-pointer-marker-alist
  457. (cons (cons arg (list marker)) footnote-pointer-marker-alist))
  458. (setq footnote-pointer-marker-alist
  459. (Footnote-sort footnote-pointer-marker-alist)))))
  460. (defun Footnote-insert-footnote (arg)
  461. "Insert a footnote numbered ARG, at (point)."
  462. (push-mark)
  463. (Footnote-insert-pointer-marker arg (point))
  464. (Footnote-insert-numbered-footnote arg t)
  465. (Footnote-goto-char-point-max)
  466. (if (cond
  467. ((not (string-equal footnote-section-tag ""))
  468. (re-search-backward (concat "^" footnote-section-tag-regexp) nil t))
  469. (footnote-text-marker-alist
  470. (goto-char (cdar footnote-text-marker-alist))))
  471. (save-restriction
  472. (when footnote-narrow-to-footnotes-when-editing
  473. (Footnote-narrow-to-footnotes))
  474. (Footnote-goto-footnote (1- arg)) ; evil, FIXME (less evil now)
  475. ;; (message "Inserting footnote %d" arg)
  476. (unless
  477. (or (eq arg 1)
  478. (when (re-search-forward
  479. (if footnote-spaced-footnotes
  480. "\n\n"
  481. (concat "\n"
  482. (regexp-quote footnote-start-tag)
  483. (Footnote-current-regexp)
  484. (regexp-quote footnote-end-tag)))
  485. nil t)
  486. (unless (beginning-of-line) t))
  487. (Footnote-goto-char-point-max)
  488. (cond
  489. ((not (string-equal footnote-section-tag ""))
  490. (re-search-backward
  491. (concat "^" footnote-section-tag-regexp) nil t))
  492. (footnote-text-marker-alist
  493. (goto-char (cdar footnote-text-marker-alist)))))))
  494. (unless (looking-at "^$")
  495. (insert "\n"))
  496. (when (eobp)
  497. (insert "\n"))
  498. (unless (string-equal footnote-section-tag "")
  499. (insert footnote-section-tag "\n")))
  500. (let ((old-point (point)))
  501. (Footnote-insert-numbered-footnote arg nil)
  502. (Footnote-insert-text-marker arg old-point)))
  503. (defun Footnote-sort (list)
  504. (sort list (lambda (e1 e2)
  505. (< (car e1) (car e2)))))
  506. (defun Footnote-text-under-cursor ()
  507. "Return the number of footnote if in footnote text.
  508. Return nil if the cursor is not positioned over the text of
  509. a footnote."
  510. (when (and (let ((old-point (point)))
  511. (save-excursion
  512. (save-restriction
  513. (Footnote-narrow-to-footnotes)
  514. (and (>= old-point (point-min))
  515. (<= old-point (point-max))))))
  516. footnote-text-marker-alist
  517. (>= (point) (cdar footnote-text-marker-alist)))
  518. (let ((i 1)
  519. alist-txt rc)
  520. (while (and (setq alist-txt (nth i footnote-text-marker-alist))
  521. (null rc))
  522. (when (< (point) (cdr alist-txt))
  523. (setq rc (car (nth (1- i) footnote-text-marker-alist))))
  524. (setq i (1+ i)))
  525. (when (and (null rc)
  526. (null alist-txt))
  527. (setq rc (car (nth (1- i) footnote-text-marker-alist))))
  528. rc)))
  529. (defun Footnote-under-cursor ()
  530. "Return the number of the footnote underneath the cursor.
  531. Return nil if the cursor is not over a footnote."
  532. (or (get-text-property (point) 'footnote-number)
  533. (Footnote-text-under-cursor)))
  534. ;;; User functions
  535. (defun Footnote-make-hole ()
  536. (save-excursion
  537. (let ((i 0)
  538. (notes (length footnote-pointer-marker-alist))
  539. alist-ptr alist-txt rc)
  540. (while (< i notes)
  541. (setq alist-ptr (nth i footnote-pointer-marker-alist))
  542. (setq alist-txt (nth i footnote-text-marker-alist))
  543. (when (< (point) (- (cadr alist-ptr) 3))
  544. (unless rc
  545. (setq rc (car alist-ptr)))
  546. (save-excursion
  547. (message "Renumbering from %s to %s"
  548. (Footnote-index-to-string (car alist-ptr))
  549. (Footnote-index-to-string
  550. (1+ (car alist-ptr))))
  551. (Footnote-renumber (car alist-ptr)
  552. (1+ (car alist-ptr))
  553. alist-ptr
  554. alist-txt)))
  555. (setq i (1+ i)))
  556. rc)))
  557. (defun Footnote-add-footnote (&optional arg)
  558. "Add a numbered footnote.
  559. The number the footnote receives is dependent upon the relative location
  560. of any other previously existing footnotes.
  561. If the variable `footnote-narrow-to-footnotes-when-editing' is set,
  562. the buffer is narrowed to the footnote body. The restriction is removed
  563. by using `Footnote-back-to-message'."
  564. (interactive "*P")
  565. (let (num)
  566. (if footnote-text-marker-alist
  567. (if (< (point) (cadar (last footnote-pointer-marker-alist)))
  568. (setq num (Footnote-make-hole))
  569. (setq num (1+ (caar (last footnote-text-marker-alist)))))
  570. (setq num 1))
  571. (message "Adding footnote %d" num)
  572. (Footnote-insert-footnote num)
  573. (insert-before-markers (make-string footnote-body-tag-spacing ? ))
  574. (let ((opoint (point)))
  575. (save-excursion
  576. (insert-before-markers
  577. (if footnote-spaced-footnotes
  578. "\n\n"
  579. "\n"))
  580. (when footnote-narrow-to-footnotes-when-editing
  581. (Footnote-narrow-to-footnotes)))
  582. ;; Emacs/XEmacs bug? save-excursion doesn't restore point when using
  583. ;; insert-before-markers.
  584. (goto-char opoint))))
  585. (defun Footnote-delete-footnote (&optional arg)
  586. "Delete a numbered footnote.
  587. With no parameter, delete the footnote under (point). With ARG specified,
  588. delete the footnote with that number."
  589. (interactive "*P")
  590. (unless arg
  591. (setq arg (Footnote-under-cursor)))
  592. (when (and arg
  593. (or (not footnote-prompt-before-deletion)
  594. (y-or-n-p (format "Really delete footnote %d?" arg))))
  595. (let (alist-ptr alist-txt locn)
  596. (setq alist-ptr (assq arg footnote-pointer-marker-alist))
  597. (setq alist-txt (assq arg footnote-text-marker-alist))
  598. (unless (and alist-ptr alist-txt)
  599. (error "Can't delete footnote %d" arg))
  600. (setq locn (cdr alist-ptr))
  601. (while (car locn)
  602. (save-excursion
  603. (goto-char (car locn))
  604. (when (looking-back (concat (regexp-quote footnote-start-tag)
  605. (Footnote-current-regexp)
  606. (regexp-quote footnote-end-tag))
  607. (line-beginning-position))
  608. (delete-region (match-beginning 0) (match-end 0))))
  609. (setq locn (cdr locn)))
  610. (save-excursion
  611. (goto-char (cdr alist-txt))
  612. (delete-region
  613. (point)
  614. (if footnote-spaced-footnotes
  615. (search-forward "\n\n" nil t)
  616. (save-restriction
  617. (end-of-line)
  618. (next-single-char-property-change
  619. (point) 'footnote-number nil (Footnote-goto-char-point-max))))))
  620. (setq footnote-pointer-marker-alist
  621. (delq alist-ptr footnote-pointer-marker-alist))
  622. (setq footnote-text-marker-alist
  623. (delq alist-txt footnote-text-marker-alist))
  624. (Footnote-renumber-footnotes)
  625. (when (and (null footnote-text-marker-alist)
  626. (null footnote-pointer-marker-alist))
  627. (save-excursion
  628. (if (not (string-equal footnote-section-tag ""))
  629. (let* ((end (Footnote-goto-char-point-max))
  630. (start (1- (re-search-backward
  631. (concat "^" footnote-section-tag-regexp)
  632. nil t))))
  633. (forward-line -1)
  634. (when (looking-at "\n")
  635. (kill-line))
  636. (delete-region start (if (< end (point-max))
  637. end
  638. (point-max))))
  639. (Footnote-goto-char-point-max)
  640. (when (looking-back "\n\n")
  641. (kill-line -1))))))))
  642. (defun Footnote-renumber-footnotes (&optional arg)
  643. "Renumber footnotes, starting from 1."
  644. (interactive "*P")
  645. (save-excursion
  646. (let ((i 0)
  647. (notes (length footnote-pointer-marker-alist))
  648. alist-ptr alist-txt)
  649. (while (< i notes)
  650. (setq alist-ptr (nth i footnote-pointer-marker-alist))
  651. (setq alist-txt (nth i footnote-text-marker-alist))
  652. (unless (= (1+ i) (car alist-ptr))
  653. (Footnote-renumber (car alist-ptr) (1+ i) alist-ptr alist-txt))
  654. (setq i (1+ i))))))
  655. (defun Footnote-goto-footnote (&optional arg)
  656. "Jump to the text of a footnote.
  657. With no parameter, jump to the text of the footnote under (point). With ARG
  658. specified, jump to the text of that footnote."
  659. (interactive "P")
  660. (unless arg
  661. (setq arg (Footnote-under-cursor)))
  662. (let ((footnote (assq arg footnote-text-marker-alist)))
  663. (cond
  664. (footnote
  665. (goto-char (cdr footnote)))
  666. ((eq arg 0)
  667. (goto-char (point-max))
  668. (cond
  669. ((not (string-equal footnote-section-tag ""))
  670. (re-search-backward (concat "^" footnote-section-tag-regexp))
  671. (forward-line 1))
  672. (footnote-text-marker-alist
  673. (goto-char (cdar footnote-text-marker-alist)))))
  674. (t
  675. (error "I don't see a footnote here")))))
  676. (defun Footnote-back-to-message (&optional arg)
  677. "Move cursor back to footnote referent.
  678. If the cursor is not over the text of a footnote, point is not changed.
  679. If the buffer was narrowed due to `footnote-narrow-to-footnotes-when-editing'
  680. being set it is automatically widened."
  681. (interactive "P")
  682. (let ((note (Footnote-text-under-cursor)))
  683. (when note
  684. (when footnote-narrow-to-footnotes-when-editing
  685. (widen))
  686. (goto-char (cadr (assq note footnote-pointer-marker-alist))))))
  687. (defvar footnote-mode-map
  688. (let ((map (make-sparse-keymap)))
  689. (define-key map "a" 'Footnote-add-footnote)
  690. (define-key map "b" 'Footnote-back-to-message)
  691. (define-key map "c" 'Footnote-cycle-style)
  692. (define-key map "d" 'Footnote-delete-footnote)
  693. (define-key map "g" 'Footnote-goto-footnote)
  694. (define-key map "r" 'Footnote-renumber-footnotes)
  695. (define-key map "s" 'Footnote-set-style)
  696. map))
  697. (defvar footnote-minor-mode-map
  698. (let ((map (make-sparse-keymap)))
  699. (define-key map footnote-prefix footnote-mode-map)
  700. map)
  701. "Keymap used for binding footnote minor mode.")
  702. ;;;###autoload
  703. (define-minor-mode footnote-mode
  704. "Toggle Footnote mode.
  705. With a prefix argument ARG, enable Footnote mode if ARG is
  706. positive, and disable it otherwise. If called from Lisp, enable
  707. the mode if ARG is omitted or nil.
  708. Footnode mode is a buffer-local minor mode. If enabled, it
  709. provides footnote support for `message-mode'. To get started,
  710. play around with the following keys:
  711. \\{footnote-minor-mode-map}"
  712. :lighter footnote-mode-line-string
  713. :keymap footnote-minor-mode-map
  714. ;; (filladapt-mode t)
  715. (when footnote-mode
  716. ;; (Footnote-setup-keybindings)
  717. (make-local-variable 'footnote-style)
  718. (make-local-variable 'footnote-body-tag-spacing)
  719. (make-local-variable 'footnote-spaced-footnotes)
  720. (make-local-variable 'footnote-section-tag)
  721. (make-local-variable 'footnote-section-tag-regexp)
  722. (make-local-variable 'footnote-start-tag)
  723. (make-local-variable 'footnote-end-tag)
  724. (when (boundp 'filladapt-token-table)
  725. ;; add tokens to filladapt to match footnotes
  726. ;; 1] xxxxxxxxxxx x x x or [1] x x x x x x x
  727. ;; xxx x xx xxx xxxx x x x xxxxxxxxxx
  728. (let ((bullet-regexp (concat (regexp-quote footnote-start-tag)
  729. "?[0-9a-zA-Z]+"
  730. (regexp-quote footnote-end-tag)
  731. "[ \t]")))
  732. (unless (assoc bullet-regexp filladapt-token-table)
  733. (setq filladapt-token-table
  734. (append filladapt-token-table
  735. (list (list bullet-regexp 'bullet)))))))))
  736. (provide 'footnote)
  737. ;;; footnote.el ends here