* lisp/mail/footnote.el (footnote-align-to-fn-text): New config var
[emacs.git] / lisp / mail / footnote.el
blobadfe03d3061c5970c5ecdc35e3e2108ad321e487
1 ;;; footnote.el --- footnote support for message mode
3 ;; Copyright (C) 1997, 2000-2017 Free Software Foundation, Inc.
5 ;; Author: Steven L Baur <steve@xemacs.org>
6 ;; Boruch Baum <boruch_baum@gmx.com>
7 ;; Keywords: mail, news
8 ;; Version: 0.19
10 ;; This file is part of GNU Emacs.
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.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This file provides footnote[1] support for message-mode in emacsen.
28 ;; footnote-mode is implemented as a minor mode.
30 ;; [1] Footnotes look something like this. Along with some decorative
31 ;; stuff.
33 ;;;; TODO:
34 ;; + Reasonable Undo support.
35 ;; - could use an `apply' entry in the buffer-undo-list to be warned when
36 ;; a footnote we inserted is removed via undo.
37 ;; - should try to handle the more general problem of deleting/removing
38 ;; footnotes via standard editing commands rather than via footnote
39 ;; commands.
40 ;; + more language styles.
41 ;; + The key sequence 'C-c ! a C-y C-c ! b' should auto-fill the
42 ;; footnote in adaptive fill mode. This does not seem to be a bug in
43 ;; `adaptive-fill' because it behaves that way on all point movements
44 ;; + Handle footmode mode elegantly in all modes, even if that means refuses to
45 ;; accept the burden. For example, in a programming language mode, footnotes
46 ;; should be commented.
47 ;; + Manually autofilling the a first footnote should not cause it to
48 ;; wrap into the footnote section tag
49 ;; + Current solution adds a second newline after the section tag, so it is
50 ;; clearly a separate paragraph. There may be stylistic objections to this.
51 ;; + Footnotes with multiple paragraphs should not have their first
52 ;; line out-dented.
53 ;; + Upon leaving footnote area, perform an auto-fill on an entire
54 ;; footnote (including multiple paragraphs), or on entire footnote area.
55 ;; + fill-paragraph takes arg REGION, but seemingly only when called
56 ;; interactively.
57 ;; + At some point, it became necessary to change `footnote-section-tag-regexp'
58 ;; to remove its trailing space. (Adaptive fill side-effect?)
59 ;; + useful for lazy testing
60 ;; (setq footnote-narrow-to-footnotes-when-editing t)
61 ;; (setq footnote-section-tag "Footnotes: ")
62 ;; (setq footnote-section-tag-regexp "Footnotes\\(\\[.\\]\\)?:")
64 ;;; Code:
66 (eval-when-compile (require 'cl-lib))
67 (defvar filladapt-token-table)
69 (defgroup footnote nil
70 "Support for footnotes in mail and news messages."
71 :version "21.1"
72 :group 'message)
74 (defcustom footnote-mode-line-string " FN"
75 "String to display in modes section of the mode-line."
76 :type 'string
77 :group 'footnote)
79 (defcustom footnote-mode-hook nil
80 "Hook functions run when footnote-mode is activated."
81 :type 'hook
82 :group 'footnote)
84 (defcustom footnote-narrow-to-footnotes-when-editing nil
85 "If non-nil, narrow to footnote text body while editing a footnote."
86 :type 'boolean
87 :group 'footnote)
89 (defcustom footnote-prompt-before-deletion t
90 "If non-nil, prompt before deleting a footnote.
91 There is currently no way to undo deletions."
92 :type 'boolean
93 :group 'footnote)
95 (defcustom footnote-spaced-footnotes t
96 "If non-nil, insert an empty line between footnotes.
97 Customizing this variable has no effect on buffers already
98 displaying footnotes."
99 :type 'boolean
100 :group 'footnote)
102 (defcustom footnote-use-message-mode t ; Nowhere used.
103 "If non-nil, assume Footnoting will be done in `message-mode'."
104 :type 'boolean
105 :group 'footnote)
107 (defcustom footnote-body-tag-spacing 2
108 "Number of spaces separating a footnote body tag and its text.
109 Customizing this variable has no effect on buffers already
110 displaying footnotes."
111 :type 'integer
112 :group 'footnote)
114 (defcustom footnote-prefix [(control ?c) ?!]
115 "Prefix key to use for Footnote command in Footnote minor mode.
116 The value of this variable is checked as part of loading Footnote mode.
117 After that, changing the prefix key requires manipulating keymaps."
118 :type 'key-sequence
119 :group 'footnote)
121 ;;; Interface variables that probably shouldn't be changed
123 (defcustom footnote-section-tag "Footnotes: "
124 "Tag inserted at beginning of footnote section.
125 If you set this to the empty string, no tag is inserted and the
126 value of `footnote-section-tag-regexp' is ignored. Customizing
127 this variable has no effect on buffers already displaying
128 footnotes."
129 :type 'string
130 :group 'footnote)
132 (defcustom footnote-section-tag-regexp
133 ;; Even if `footnote-section-tag' has a trailing space, let's not require it
134 ;; here, since it might be trimmed by various commands.
135 "Footnotes\\(\\[.\\]\\)?:"
136 "Regexp which indicates the start of a footnote section.
137 This variable is disregarded when `footnote-section-tag' is the
138 empty string. Customizing this variable has no effect on buffers
139 already displaying footnotes."
140 :version "27.1"
141 :type 'regexp
142 :group 'footnote)
144 ;; The following three should be consumed by footnote styles.
145 (defcustom footnote-start-tag "["
146 "String used to denote start of numbered footnote.
147 Should not be set to the empty string. Customizing this variable
148 has no effect on buffers already displaying footnotes."
149 :type 'string
150 :group 'footnote)
152 (defcustom footnote-end-tag "]"
153 "String used to denote end of numbered footnote.
154 Should not be set to the empty string. Customizing this variable
155 has no effect on buffers already displaying footnotes."
156 :type 'string
157 :group 'footnote)
159 (defcustom footnote-signature-separator
160 (if (boundp 'message-signature-separator)
161 message-signature-separator
162 "^-- $")
163 "Regexp used by Footnote mode to recognize signatures."
164 :type 'regexp
165 :group 'footnote)
167 (defcustom footnote-align-to-fn-text t
168 "If non-nil, align footnote text lines.
169 If nil, footnote text lines are to be aligned flush left with left side
170 of the footnote number. If non-nil footnote text lines are to be aligned
171 with the first character of footnote text."
172 :type 'boolean)
174 ;;; Private variables
176 (defvar footnote-style-number nil
177 "Footnote style represented as an index into footnote-style-alist.")
178 (make-variable-buffer-local 'footnote-style-number)
180 (defvar footnote-text-marker-alist nil
181 "List of markers pointing to text of footnotes in message buffer.")
182 (make-variable-buffer-local 'footnote-text-marker-alist)
184 (defvar footnote-pointer-marker-alist nil
185 "List of markers pointing to footnote pointers in message buffer.")
186 (make-variable-buffer-local 'footnote-pointer-marker-alist)
188 (defvar footnote-mouse-highlight 'highlight
189 "Text property name to enable mouse over highlight.")
191 (defvar footnote-mode)
193 ;;; Default styles
194 ;;; NUMERIC
195 (defconst footnote-numeric-regexp "[0-9]+"
196 "Regexp for digits.")
198 (defun Footnote-numeric (n)
199 "Numeric footnote style.
200 Use Arabic numerals for footnoting."
201 (int-to-string n))
203 ;;; ENGLISH UPPER
204 (defconst footnote-english-upper "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
205 "Upper case English alphabet.")
207 (defconst footnote-english-upper-regexp "[A-Z]+"
208 "Regexp for upper case English alphabet.")
210 (defun Footnote-english-upper (n)
211 "Upper case English footnoting.
212 Wrapping around the alphabet implies successive repetitions of letters."
213 (let* ((ltr (mod (1- n) (length footnote-english-upper)))
214 (rep (/ (1- n) (length footnote-english-upper)))
215 (chr (char-to-string (aref footnote-english-upper ltr)))
217 (while (>= rep 0)
218 (setq rc (concat rc chr))
219 (setq rep (1- rep)))
220 rc))
222 ;;; ENGLISH LOWER
223 (defconst footnote-english-lower "abcdefghijklmnopqrstuvwxyz"
224 "Lower case English alphabet.")
226 (defconst footnote-english-lower-regexp "[a-z]+"
227 "Regexp of lower case English alphabet.")
229 (defun Footnote-english-lower (n)
230 "Lower case English footnoting.
231 Wrapping around the alphabet implies successive repetitions of letters."
232 (let* ((ltr (mod (1- n) (length footnote-english-lower)))
233 (rep (/ (1- n) (length footnote-english-lower)))
234 (chr (char-to-string (aref footnote-english-lower ltr)))
236 (while (>= rep 0)
237 (setq rc (concat rc chr))
238 (setq rep (1- rep)))
239 rc))
241 ;;; ROMAN LOWER
242 (defconst footnote-roman-lower-list
243 '((1 . "i") (5 . "v") (10 . "x")
244 (50 . "l") (100 . "c") (500 . "d") (1000 . "m"))
245 "List of roman numerals with their values.")
247 (defconst footnote-roman-lower-regexp "[ivxlcdm]+"
248 "Regexp of roman numerals.")
250 (defun Footnote-roman-lower (n)
251 "Generic Roman number footnoting."
252 (Footnote-roman-common n footnote-roman-lower-list))
254 ;;; ROMAN UPPER
255 (defconst footnote-roman-upper-list
256 '((1 . "I") (5 . "V") (10 . "X")
257 (50 . "L") (100 . "C") (500 . "D") (1000 . "M"))
258 "List of roman numerals with their values.")
260 (defconst footnote-roman-upper-regexp "[IVXLCDM]+"
261 "Regexp of roman numerals. Not complete")
263 (defun Footnote-roman-upper (n)
264 "Generic Roman number footnoting."
265 (Footnote-roman-common n footnote-roman-upper-list))
267 (defun Footnote-roman-common (n footnote-roman-list)
268 "Lower case Roman footnoting."
269 (let* ((our-list footnote-roman-list)
270 (rom-lngth (length our-list))
271 (rom-high 0)
272 (rom-low 0)
273 (rom-div -1)
274 (count-high 0)
275 (count-low 0))
276 ;; find surrounding numbers
277 (while (and (<= count-high (1- rom-lngth))
278 (>= n (car (nth count-high our-list))))
279 ;; (message "Checking %d" (car (nth count-high our-list)))
280 (setq count-high (1+ count-high)))
281 (setq rom-high count-high)
282 (setq rom-low (1- count-high))
283 ;; find the appropriate divisor (if it exists)
284 (while (and (= rom-div -1)
285 (< count-low rom-high))
286 (when (or (> n (- (car (nth rom-high our-list))
287 (/ (car (nth count-low our-list))
288 2)))
289 (= n (- (car (nth rom-high our-list))
290 (car (nth count-low our-list)))))
291 (setq rom-div count-low))
292 ;; (message "Checking %d and %d in div loop" rom-high count-low)
293 (setq count-low (1+ count-low)))
294 ;;(message "We now have high: %d, low: %d, div: %d, n: %d"
295 ;; rom-high rom-low (if rom-div rom-div -1) n)
296 (let ((rom-low-pair (nth rom-low our-list))
297 (rom-high-pair (nth rom-high our-list))
298 (rom-div-pair (if (not (= rom-div -1)) (nth rom-div our-list) nil)))
299 ;; (message "pairs are: rom-low: %S, rom-high: %S, rom-div: %S"
300 ;; rom-low-pair rom-high-pair rom-div-pair)
301 (cond
302 ((< n 0) (error "Footnote-roman-common called with n < 0"))
303 ((= n 0) "")
304 ((= n (car rom-low-pair)) (cdr rom-low-pair))
305 ((= n (car rom-high-pair)) (cdr rom-high-pair))
306 ((= (car rom-low-pair) (car rom-high-pair))
307 (concat (cdr rom-low-pair)
308 (Footnote-roman-common
309 (- n (car rom-low-pair))
310 footnote-roman-list)))
311 ((>= rom-div 0) (concat (cdr rom-div-pair) (cdr rom-high-pair)
312 (Footnote-roman-common
313 (- n (- (car rom-high-pair)
314 (car rom-div-pair)))
315 footnote-roman-list)))
316 (t (concat (cdr rom-low-pair)
317 (Footnote-roman-common
318 (- n (car rom-low-pair))
319 footnote-roman-list)))))))
321 ;; Latin-1
323 (defconst footnote-latin-string "¹²³ºª§¶"
324 "String of Latin-1 footnoting characters.")
326 ;; Note not [...]+, because this style cycles.
327 (defconst footnote-latin-regexp (concat "[" footnote-latin-string "]")
328 "Regexp for Latin-1 footnoting characters.")
330 (defun Footnote-latin (n)
331 "Latin-1 footnote style.
332 Use a range of Latin-1 non-ASCII characters for footnoting."
333 (string (aref footnote-latin-string
334 (mod (1- n) (length footnote-latin-string)))))
336 ;; Unicode
338 (defconst footnote-unicode-string "⁰¹²³⁴⁵⁶⁷⁸⁹"
339 "String of Unicode footnoting characters.")
341 (defconst footnote-unicode-regexp (concat "[" footnote-unicode-string "]+")
342 "Regexp for Unicode footnoting characters.")
344 (defun Footnote-unicode (n)
345 "Unicode footnote style.
346 Use Unicode characters for footnoting."
347 (let (modulus result done)
348 (while (not done)
349 (setq modulus (mod n 10)
350 n (truncate n 10))
351 (and (zerop n) (setq done t))
352 (push (aref footnote-unicode-string modulus) result))
353 (apply #'string result)))
355 ;; Hebrew
357 (defconst footnote-hebrew-numeric-regex "[אבגדהוזחטיכלמנסעפצקרשת']+")
358 ; (defconst footnote-hebrew-numeric-regex "\\([אבגדהוזחט]'\\)?\\(ת\\)?\\(ת\\)?\\([קרשת]\\)?\\([טיכלמנסעפצ]\\)?\\([אבגדהוזחט]\\)?")
360 (defconst footnote-hebrew-numeric
362 ("א" "ב" "ג" "ד" "ה" "ו" "ז" "ח" "ט")
363 ("י" "כ" "ל" "מ" "נ" "ס" "ע" "פ" "צ")
364 ("ק" "ר" "ש" "ת" "תק" "תר"" תש" "תת" "תתק")))
366 (defun Footnote-hebrew-numeric (n)
367 "Supports 9999 footnotes, then rolls over."
368 (let* ((n (+ (mod n 10000) (/ n 10000)))
369 (thousands (/ n 1000))
370 (hundreds (/ (mod n 1000) 100))
371 (tens (/ (mod n 100) 10))
372 (units (mod n 10))
373 (special (if (not (= tens 1)) nil
374 (or (when (= units 5) "טו")
375 (when (= units 6) "טז")))))
376 (concat
377 (when (/= 0 thousands)
378 (concat (nth (1- thousands) (nth 0 footnote-hebrew-numeric)) "'"))
379 (when (/= 0 hundreds)
380 (nth (1- hundreds) (nth 2 footnote-hebrew-numeric)))
381 (if special special
382 (concat
383 (when (/= 0 tens) (nth (1- tens) (nth 1 footnote-hebrew-numeric)))
384 (when (/= 0 units) (nth (1- units) (nth 0 footnote-hebrew-numeric))))))))
386 (defconst footnote-hebrew-symbolic-regex "[אבגדהוזחטיכלמנסעפצקרשת]")
388 (defconst footnote-hebrew-symbolic
390 "א" "ב" "ג" "ד" "ה" "ו" "ז" "ח" "ט" "י" "כ" "ל" "מ" "נ" "ס" "ע" "פ" "צ" "ק" "ר" "ש" "ת"))
392 (defun Footnote-hebrew-symbolic (n)
393 "Only 22 elements, per the style of eg. 'פירוש שפתי חכמים על רש״י'.
394 Proceeds from `י' to `כ', from `צ' to `ק'. After `ת', rolls over to `א'."
395 (nth (mod (1- n) 22) footnote-hebrew-symbolic))
397 ;;; list of all footnote styles
398 (defvar footnote-style-alist
399 `((numeric Footnote-numeric ,footnote-numeric-regexp)
400 (english-lower Footnote-english-lower ,footnote-english-lower-regexp)
401 (english-upper Footnote-english-upper ,footnote-english-upper-regexp)
402 (roman-lower Footnote-roman-lower ,footnote-roman-lower-regexp)
403 (roman-upper Footnote-roman-upper ,footnote-roman-upper-regexp)
404 (latin Footnote-latin ,footnote-latin-regexp)
405 (unicode Footnote-unicode ,footnote-unicode-regexp)
406 (hebrew-numeric Footnote-hebrew-numeric ,footnote-hebrew-numeric-regex)
407 (hebrew-symbolic Footnote-hebrew-symbolic ,footnote-hebrew-symbolic-regex))
408 "Styles of footnote tags available.
409 By default, Arabic numbers, English letters, Roman Numerals,
410 Latin and Unicode superscript characters, and Hebrew numerals
411 are available.")
413 (defcustom footnote-style 'numeric
414 "Default style used for footnoting.
415 numeric == 1, 2, 3, ...
416 english-lower == a, b, c, ...
417 english-upper == A, B, C, ...
418 roman-lower == i, ii, iii, iv, v, ...
419 roman-upper == I, II, III, IV, V, ...
420 latin == ¹ ² ³ º ª § ¶
421 unicode == ¹, ², ³, ...
422 hebrew-numeric == א, ב, ..., יא, ..., תקא...
423 hebrew-symbolic == א, ב, ..., י, כ, ..., צ, ק, ..., ת, א
424 See also variables `footnote-start-tag' and `footnote-end-tag'.
426 Note: some characters in the unicode style may not show up
427 properly if the default font does not contain those characters.
429 Customizing this variable has no effect on buffers already
430 displaying footnotes. To change the style of footnotes in such a
431 buffer use the command `Footnote-set-style'."
432 :type (cons 'choice (mapcar (lambda (x) (list 'const (car x)))
433 footnote-style-alist))
434 :group 'footnote)
436 ;;; Style utilities & functions
437 (defun Footnote-style-p (style)
438 "Return non-nil if style is a valid style known to `footnote-mode'."
439 (assq style footnote-style-alist))
441 (defun Footnote-index-to-string (index)
442 "Convert a binary index into a string to display as a footnote.
443 Conversion is done based upon the current selected style."
444 (let ((alist (if (Footnote-style-p footnote-style)
445 (assq footnote-style footnote-style-alist)
446 (nth 0 footnote-style-alist))))
447 (funcall (nth 1 alist) index)))
449 (defun Footnote-current-regexp ()
450 "Return the regexp of the index of the current style."
451 (concat (nth 2 (or (assq footnote-style footnote-style-alist)
452 (nth 0 footnote-style-alist)))
453 "*"))
455 (defun Footnote-refresh-footnotes (&optional index-regexp)
456 "Redraw all footnotes.
457 You must call this or arrange to have this called after changing footnote
458 styles."
459 (unless index-regexp
460 (setq index-regexp (Footnote-current-regexp)))
461 (save-excursion
462 ;; Take care of the pointers first
463 (let ((i 0) locn alist)
464 (while (setq alist (nth i footnote-pointer-marker-alist))
465 (setq locn (cdr alist))
466 (while locn
467 (goto-char (car locn))
468 ;; Try to handle the case where `footnote-start-tag' and
469 ;; `footnote-end-tag' are the same string.
470 (when (looking-back (concat
471 (regexp-quote footnote-start-tag)
472 "\\(" index-regexp "+\\)"
473 (regexp-quote footnote-end-tag))
474 (line-beginning-position))
475 (replace-match
476 (propertize
477 (concat
478 footnote-start-tag
479 (Footnote-index-to-string (1+ i))
480 footnote-end-tag)
481 'footnote-number (1+ i) footnote-mouse-highlight t)
482 nil "\\1"))
483 (setq locn (cdr locn)))
484 (setq i (1+ i))))
486 ;; Now take care of the text section
487 (let ((i 0) alist)
488 (while (setq alist (nth i footnote-text-marker-alist))
489 (goto-char (cdr alist))
490 (when (looking-at (concat
491 (regexp-quote footnote-start-tag)
492 "\\(" index-regexp "+\\)"
493 (regexp-quote footnote-end-tag)))
494 (replace-match
495 (propertize
496 (concat
497 footnote-start-tag
498 (Footnote-index-to-string (1+ i))
499 footnote-end-tag)
500 'footnote-number (1+ i))
501 nil "\\1"))
502 (setq i (1+ i))))))
504 (defun Footnote-assoc-index (key alist)
505 "Give index of key in alist."
506 (let ((i 0) (max (length alist)) rc)
507 (while (and (null rc)
508 (< i max))
509 (when (eq key (car (nth i alist)))
510 (setq rc i))
511 (setq i (1+ i)))
512 rc))
514 (defun Footnote-cycle-style ()
515 "Select next defined footnote style."
516 (interactive)
517 (let ((old (Footnote-assoc-index footnote-style footnote-style-alist))
518 (max (length footnote-style-alist))
519 idx)
520 (setq idx (1+ old))
521 (when (>= idx max)
522 (setq idx 0))
523 (setq footnote-style (car (nth idx footnote-style-alist)))
524 (Footnote-refresh-footnotes (nth 2 (nth old footnote-style-alist)))))
526 (defun Footnote-set-style (&optional style)
527 "Select a specific style."
528 (interactive
529 (list (intern (completing-read
530 "Footnote Style: "
531 obarray #'Footnote-style-p 'require-match))))
532 (let ((old (Footnote-assoc-index footnote-style footnote-style-alist)))
533 (setq footnote-style style)
534 (Footnote-refresh-footnotes (nth 2 (nth old footnote-style-alist)))))
536 ;; Internal functions
537 (defun Footnote-insert-numbered-footnote (arg &optional mousable)
538 "Insert numbered footnote at (point)."
539 (let ((string (concat footnote-start-tag
540 (Footnote-index-to-string arg)
541 footnote-end-tag)))
542 (insert-before-markers
543 (if mousable
544 (propertize
545 string 'footnote-number arg footnote-mouse-highlight t)
546 (propertize string 'footnote-number arg)))))
548 (defun Footnote-renumber (from to pointer-alist text-alist)
549 "Renumber a single footnote."
550 (let* ((posn-list (cdr pointer-alist)))
551 (setcar pointer-alist to)
552 (setcar text-alist to)
553 (while posn-list
554 (goto-char (car posn-list))
555 (when (looking-back (concat (regexp-quote footnote-start-tag)
556 (Footnote-current-regexp)
557 (regexp-quote footnote-end-tag))
558 (line-beginning-position))
559 (replace-match
560 (propertize
561 (concat footnote-start-tag
562 (Footnote-index-to-string to)
563 footnote-end-tag)
564 'footnote-number to footnote-mouse-highlight t)))
565 (setq posn-list (cdr posn-list)))
566 (goto-char (cdr text-alist))
567 (when (looking-at (concat (regexp-quote footnote-start-tag)
568 (Footnote-current-regexp)
569 (regexp-quote footnote-end-tag)))
570 (replace-match
571 (propertize
572 (concat footnote-start-tag
573 (Footnote-index-to-string to)
574 footnote-end-tag)
575 'footnote-number to)))))
577 ;; Not needed? <-- 2017-12 Boruch: Not my comment! BUT, when I
578 ;; starting hacking the code, this function
579 ;; `Footnote-narrow-to-footnotes' was never narrowing, and the result
580 ;; wasn't breaking anything.
581 (defun Footnote-narrow-to-footnotes ()
582 "Restrict text in buffer to show only text of footnotes."
583 (interactive) ; testing
584 (narrow-to-region (Footnote--get-area-point-min)
585 (Footnote--get-area-point-max)))
587 (defun Footnote-goto-char-point-max ()
588 "Move to end of buffer or prior to start of .signature."
589 (goto-char (point-max))
590 (or (re-search-backward footnote-signature-separator nil t)
591 (point)))
593 (defun Footnote-insert-text-marker (arg locn)
594 "Insert a marker pointing to footnote ARG, at buffer location LOCN."
595 (let ((marker (make-marker)))
596 (unless (assq arg footnote-text-marker-alist)
597 (set-marker marker locn)
598 (setq footnote-text-marker-alist
599 (cons (cons arg marker) footnote-text-marker-alist))
600 (setq footnote-text-marker-alist
601 (Footnote-sort footnote-text-marker-alist)))))
603 (defun Footnote-insert-pointer-marker (arg locn)
604 "Insert a marker pointing to footnote ARG, at buffer location LOCN."
605 (let ((marker (make-marker))
606 alist)
607 (set-marker marker locn)
608 (if (setq alist (assq arg footnote-pointer-marker-alist))
609 (setf alist
610 (cons marker (cdr alist)))
611 (setq footnote-pointer-marker-alist
612 (cons (cons arg (list marker)) footnote-pointer-marker-alist))
613 (setq footnote-pointer-marker-alist
614 (Footnote-sort footnote-pointer-marker-alist)))))
616 (defun Footnote-insert-footnote (arg)
617 "Insert a footnote numbered ARG, at (point)."
618 (push-mark)
619 (Footnote-insert-pointer-marker arg (point))
620 (Footnote-insert-numbered-footnote arg t)
621 (Footnote-goto-char-point-max)
622 (if (cond
623 ((not (string-equal footnote-section-tag ""))
624 (re-search-backward (concat "^" footnote-section-tag-regexp) nil t))
625 (footnote-text-marker-alist
626 (goto-char (cdar footnote-text-marker-alist))))
627 (save-restriction
628 (when footnote-narrow-to-footnotes-when-editing
629 (Footnote-narrow-to-footnotes))
630 (Footnote-goto-footnote (1- arg)) ; evil, FIXME (less evil now)
631 ;; (message "Inserting footnote %d" arg)
632 (unless
633 (or (eq arg 1)
634 (when (re-search-forward
635 (if footnote-spaced-footnotes
636 "\n\n"
637 (concat "\n"
638 (regexp-quote footnote-start-tag)
639 (Footnote-current-regexp)
640 (regexp-quote footnote-end-tag)))
641 nil t)
642 (unless (beginning-of-line) t))
643 (Footnote-goto-char-point-max)
644 (cond
645 ((not (string-equal footnote-section-tag ""))
646 (re-search-backward
647 (concat "^" footnote-section-tag-regexp) nil t))
648 (footnote-text-marker-alist
649 (goto-char (cdar footnote-text-marker-alist)))))))
650 (unless (looking-at "^$")
651 (insert "\n"))
652 (when (eobp)
653 (insert "\n"))
654 (unless (string-equal footnote-section-tag "")
655 (insert footnote-section-tag "\n")))
656 (let ((old-point (point)))
657 (Footnote-insert-numbered-footnote arg nil)
658 (Footnote-insert-text-marker arg old-point)))
660 (defun Footnote-sort (list)
661 (sort list (lambda (e1 e2)
662 (< (car e1) (car e2)))))
664 (defun Footnote-text-under-cursor ()
665 "Return the number of the current footnote if in footnote text.
666 Return nil if the cursor is not positioned over the text of
667 a footnote."
668 (when (and footnote-text-marker-alist
669 (<= (Footnote--get-area-point-min)
670 (point)
671 (Footnote--get-area-point-max)))
672 (let ((i 1) alist-txt result)
673 (while (and (setq alist-txt (nth i footnote-text-marker-alist))
674 (null result))
675 (when (< (point) (cdr alist-txt))
676 (setq result (car (nth (1- i) footnote-text-marker-alist))))
677 (setq i (1+ i)))
678 (when (and (null result) (null alist-txt))
679 (setq result (car (nth (1- i) footnote-text-marker-alist))))
680 result)))
682 (defun Footnote-under-cursor ()
683 "Return the number of the footnote underneath the cursor.
684 Return nil if the cursor is not over a footnote."
685 (or (get-text-property (point) 'footnote-number)
686 (Footnote-text-under-cursor)))
688 (defun Footnote--calc-fn-alignment-column ()
689 "Calculate the left alignment for footnote text."
690 ;; FIXME: Maybe it would be better to go to the footnote's beginning and
691 ;; see at which column it starts.
692 (+ footnote-body-tag-spacing
693 (string-width
694 (concat footnote-start-tag footnote-end-tag
695 (Footnote-index-to-string
696 (caar (last footnote-text-marker-alist)))))))
698 (defun Footnote--fill-prefix-string ()
699 "Return the fill prefix to be used by footnote mode."
700 ;; TODO: Prefix to this value other prefix strings, such as those
701 ;; designating a comment line, a message response, or a boxquote.
702 (make-string (Footnote--calc-fn-alignment-column) ?\s))
704 (defun Footnote--point-in-body-p ()
705 "Return non-nil if point is in the buffer text area,
706 i.e. before the beginning of the footnote area."
707 (< (point) (Footnote--get-area-point-min)))
709 (defun Footnote--get-area-point-min (&optional before-tag)
710 "Return start of the first footnote.
711 If there is no footnote area, returns `point-max'.
712 With optional arg BEFORE-TAG, return position of the `footnote-section-tag'
713 instead, if applicable."
714 (cond
715 ;; FIXME: Shouldn't we use `Footnote--get-area-point-max' instead?
716 ((not footnote-text-marker-alist) (point-max))
717 ((not before-tag) (cdr (car footnote-text-marker-alist)))
718 ((string-equal footnote-section-tag "")
719 (cdr (car footnote-text-marker-alist)))
721 (save-excursion
722 (goto-char (cdr (car footnote-text-marker-alist)))
723 (if (re-search-backward (concat "^" footnote-section-tag-regexp) nil t)
724 (match-beginning 0)
725 (message "Footnote section tag not found!")
726 ;; This `else' should never happen, and indicates an error,
727 ;; ie. footnotes already exist and a footnote-section-tag is defined,
728 ;; but the section tag hasn't been found. We choose to assume that the
729 ;; user deleted it intentionally and wants us to behave in this buffer
730 ;; as if the section tag was set "", so we do that, now.
731 ;;(setq footnote-section-tag "")
733 ;; HOWEVER: The rest of footnote mode does not currently honor or
734 ;; account for this.
736 ;; To illustrate the difference in behavior, create a few footnotes,
737 ;; delete the section tag, and create another footnote. Then undo,
738 ;; comment the above line (that sets the tag to ""), re-evaluate this
739 ;; function, and repeat.
741 ;; TODO: integrate sanity checks at reasonable operational points.
742 (cdr (car footnote-text-marker-alist)))))))
744 (defun Footnote--get-area-point-max ()
745 "Return the end of footnote area.
746 This is either `point-max' or the start of a `.signature' string, as
747 defined by variable `footnote-signature-separator'. If there is no
748 footnote area, returns `point-max'."
749 (save-excursion (Footnote-goto-char-point-max)))
751 (defun Footnote--adaptive-fill-function (orig-fun)
753 (and
754 footnote-mode
755 footnote-align-to-fn-text
756 (Footnote-text-under-cursor)
757 ;; (not (Footnote--point-in-body-p))
758 ;; (< (point) (Footnote--signature-area-start-point))
759 (Footnote--fill-prefix-string))
760 ;; If not within a footnote's text, fallback to the default.
761 (funcall orig-fun)))
763 ;;; User functions
765 (defun Footnote-make-hole ()
766 (save-excursion
767 (let ((i 0)
768 (notes (length footnote-pointer-marker-alist))
769 alist-ptr alist-txt rc)
770 (while (< i notes)
771 (setq alist-ptr (nth i footnote-pointer-marker-alist))
772 (setq alist-txt (nth i footnote-text-marker-alist))
773 (when (< (point) (- (cadr alist-ptr) 3))
774 (unless rc
775 (setq rc (car alist-ptr)))
776 (save-excursion
777 (message "Renumbering from %s to %s"
778 (Footnote-index-to-string (car alist-ptr))
779 (Footnote-index-to-string
780 (1+ (car alist-ptr))))
781 (Footnote-renumber (car alist-ptr)
782 (1+ (car alist-ptr))
783 alist-ptr
784 alist-txt)))
785 (setq i (1+ i)))
786 rc)))
788 (defun Footnote-add-footnote (&optional arg)
789 "Add a numbered footnote.
790 The number the footnote receives is dependent upon the relative location
791 of any other previously existing footnotes.
792 If the variable `footnote-narrow-to-footnotes-when-editing' is set,
793 the buffer is narrowed to the footnote body. The restriction is removed
794 by using `Footnote-back-to-message'."
795 (interactive "*P")
796 (let ((num
797 (if footnote-text-marker-alist
798 (if (< (point) (cl-cadar (last footnote-pointer-marker-alist)))
799 (Footnote-make-hole)
800 (1+ (caar (last footnote-text-marker-alist))))
801 1)))
802 (message "Adding footnote %d" num)
803 (Footnote-insert-footnote num)
804 (insert-before-markers (make-string footnote-body-tag-spacing ? ))
805 (let ((opoint (point)))
806 (save-excursion
807 (insert-before-markers
808 (if footnote-spaced-footnotes
809 "\n\n"
810 "\n"))
811 (when footnote-narrow-to-footnotes-when-editing
812 (Footnote-narrow-to-footnotes)))
813 ;; Emacs/XEmacs bug? save-excursion doesn't restore point when using
814 ;; insert-before-markers.
815 (goto-char opoint))))
817 (defun Footnote-delete-footnote (&optional arg)
818 "Delete a numbered footnote.
819 With no parameter, delete the footnote under (point). With ARG specified,
820 delete the footnote with that number."
821 (interactive "*P")
822 (unless arg
823 (setq arg (Footnote-under-cursor)))
824 (when (and arg
825 (or (not footnote-prompt-before-deletion)
826 (y-or-n-p (format "Really delete footnote %d?" arg))))
827 (let (alist-ptr alist-txt locn)
828 (setq alist-ptr (assq arg footnote-pointer-marker-alist))
829 (setq alist-txt (assq arg footnote-text-marker-alist))
830 (unless (and alist-ptr alist-txt)
831 (error "Can't delete footnote %d" arg))
832 (setq locn (cdr alist-ptr))
833 (while (car locn)
834 (save-excursion
835 (goto-char (car locn))
836 (when (looking-back (concat (regexp-quote footnote-start-tag)
837 (Footnote-current-regexp)
838 (regexp-quote footnote-end-tag))
839 (line-beginning-position))
840 (delete-region (match-beginning 0) (match-end 0))))
841 (setq locn (cdr locn)))
842 (save-excursion
843 (goto-char (cdr alist-txt))
844 (delete-region
845 (point)
846 (if footnote-spaced-footnotes
847 (search-forward "\n\n" nil t)
848 (save-restriction ; <= 2017-12 Boruch: WHY?? I see no narrowing / widening here.
849 (end-of-line)
850 (next-single-char-property-change
851 (point) 'footnote-number nil (Footnote-goto-char-point-max))))))
852 (setq footnote-pointer-marker-alist
853 (delq alist-ptr footnote-pointer-marker-alist))
854 (setq footnote-text-marker-alist
855 (delq alist-txt footnote-text-marker-alist))
856 (Footnote-renumber-footnotes)
857 (when (and (null footnote-text-marker-alist)
858 (null footnote-pointer-marker-alist))
859 (save-excursion
860 (if (not (string-equal footnote-section-tag ""))
861 (let* ((end (Footnote-goto-char-point-max))
862 (start (1- (re-search-backward
863 (concat "^" footnote-section-tag-regexp)
864 nil t))))
865 (forward-line -1)
866 (when (looking-at "\n")
867 (kill-line))
868 (delete-region start (if (< end (point-max))
870 (point-max))))
871 (Footnote-goto-char-point-max)
872 (when (looking-back "\n\n" (- (point) 2))
873 (kill-line -1))))))))
875 (defun Footnote-renumber-footnotes (&optional arg)
876 "Renumber footnotes, starting from 1."
877 (interactive "*P")
878 (save-excursion
879 (let ((i 0)
880 (notes (length footnote-pointer-marker-alist))
881 alist-ptr alist-txt)
882 (while (< i notes)
883 (setq alist-ptr (nth i footnote-pointer-marker-alist))
884 (setq alist-txt (nth i footnote-text-marker-alist))
885 (unless (= (1+ i) (car alist-ptr))
886 (Footnote-renumber (car alist-ptr) (1+ i) alist-ptr alist-txt))
887 (setq i (1+ i))))))
889 (defun Footnote-goto-footnote (&optional arg)
890 "Jump to the text of a footnote.
891 With no parameter, jump to the text of the footnote under (point). With ARG
892 specified, jump to the text of that footnote."
893 (interactive "P")
894 (unless arg
895 (setq arg (Footnote-under-cursor)))
896 (let ((footnote (assq arg footnote-text-marker-alist)))
897 (cond
898 (footnote
899 (goto-char (cdr footnote)))
900 ((eq arg 0)
901 (goto-char (point-max))
902 (cond
903 ((not (string-equal footnote-section-tag ""))
904 (re-search-backward (concat "^" footnote-section-tag-regexp))
905 (forward-line 1))
906 (footnote-text-marker-alist
907 (goto-char (cdar footnote-text-marker-alist)))))
909 (error "I don't see a footnote here")))))
911 (defun Footnote-back-to-message (&optional arg)
912 "Move cursor back to footnote referent.
913 If the cursor is not over the text of a footnote, point is not changed.
914 If the buffer was narrowed due to `footnote-narrow-to-footnotes-when-editing'
915 being set it is automatically widened."
916 (interactive "P")
917 (let ((note (Footnote-text-under-cursor)))
918 (when note
919 (when footnote-narrow-to-footnotes-when-editing
920 (widen))
921 (goto-char (cadr (assq note footnote-pointer-marker-alist))))))
923 (defvar footnote-mode-map
924 (let ((map (make-sparse-keymap)))
925 (define-key map "a" 'Footnote-add-footnote)
926 (define-key map "b" 'Footnote-back-to-message)
927 (define-key map "c" 'Footnote-cycle-style)
928 (define-key map "d" 'Footnote-delete-footnote)
929 (define-key map "g" 'Footnote-goto-footnote)
930 (define-key map "r" 'Footnote-renumber-footnotes)
931 (define-key map "s" 'Footnote-set-style)
932 map))
934 (defvar footnote-minor-mode-map
935 (let ((map (make-sparse-keymap)))
936 (define-key map footnote-prefix footnote-mode-map)
937 map)
938 "Keymap used for binding footnote minor mode.")
940 ;;;###autoload
941 (define-minor-mode footnote-mode
942 "Toggle Footnote mode.
943 With a prefix argument ARG, enable Footnote mode if ARG is
944 positive, and disable it otherwise. If called from Lisp, enable
945 the mode if ARG is omitted or nil.
947 Footnote mode is a buffer-local minor mode. If enabled, it
948 provides footnote support for `message-mode'. To get started,
949 play around with the following keys:
950 \\{footnote-minor-mode-map}"
951 :lighter footnote-mode-line-string
952 :keymap footnote-minor-mode-map
953 ;; (filladapt-mode t)
954 (unless adaptive-fill-function
955 ;; nil and `ignore' have the same semantics for adaptive-fill-function,
956 ;; but only `ignore' behaves correctly with add/remove-function.
957 (setq adaptive-fill-function #'ignore))
958 (remove-function (local 'adaptive-fill-function)
959 #'Footnote--adaptive-fill-function)
960 (when footnote-mode
961 ;; (Footnote-setup-keybindings)
962 (make-local-variable 'footnote-style)
963 (make-local-variable 'footnote-body-tag-spacing)
964 (make-local-variable 'footnote-spaced-footnotes)
965 (make-local-variable 'footnote-section-tag)
966 (make-local-variable 'footnote-section-tag-regexp)
967 (make-local-variable 'footnote-start-tag)
968 (make-local-variable 'footnote-end-tag)
969 (make-local-variable 'adaptive-fill-function)
970 (add-function :around (local 'adaptive-fill-function)
971 #'Footnote--adaptive-fill-function)
973 ;; filladapt is an XEmacs package which AFAIK has never been ported
974 ;; to Emacs.
975 (when (boundp 'filladapt-token-table)
976 ;; add tokens to filladapt to match footnotes
977 ;; 1] xxxxxxxxxxx x x x or [1] x x x x x x x
978 ;; xxx x xx xxx xxxx x x x xxxxxxxxxx
979 (let ((bullet-regexp (concat (regexp-quote footnote-start-tag)
980 "?[0-9a-zA-Z]+"
981 (regexp-quote footnote-end-tag)
982 "[ \t]")))
983 (unless (assoc bullet-regexp filladapt-token-table)
984 (setq filladapt-token-table
985 (append filladapt-token-table
986 (list (list bullet-regexp 'bullet)))))))))
988 (provide 'footnote)
990 ;;; footnote.el ends here