1 ;;; footnote.el --- footnote support for message mode -*- lexical-binding:t -*-
3 ;; Copyright (C) 1997, 2000-2018 Free Software Foundation, Inc.
5 ;; Author: Steven L Baur <steve@xemacs.org> (1997-2011)
6 ;; Boruch Baum <boruch_baum@gmx.com> (2017-)
7 ;; Keywords: mail, news
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/>.
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
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
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
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
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\\(\\[.\\]\\)?:")
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."
74 (defcustom footnote-mode-line-string
" FN"
75 "String to display in modes section of the mode-line."
79 (defcustom footnote-mode-hook nil
80 "Hook functions run when footnote-mode is activated."
84 (defcustom footnote-narrow-to-footnotes-when-editing nil
85 "If non-nil, narrow to footnote text body while editing a 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."
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."
102 (defcustom footnote-use-message-mode t
; Nowhere used.
103 "If non-nil, assume Footnoting will be done in `message-mode'."
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."
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."
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
133 (defcustom footnote-section-tag-regexp
134 ;; Even if `footnote-section-tag' has a trailing space, let's not require it
135 ;; here, since it might be trimmed by various commands.
136 "Footnotes\\(\\[.\\]\\)?:"
137 "Regexp which indicates the start of a footnote section.
138 This variable is disregarded when `footnote-section-tag' is the
139 empty string. Customizing this variable has no effect on buffers
140 already displaying footnotes."
145 ;; The following three should be consumed by footnote styles.
146 (defcustom footnote-start-tag
"["
147 "String used to denote start of numbered footnote.
148 Should not be set to the empty string. Customizing this variable
149 has no effect on buffers already displaying footnotes."
153 (defcustom footnote-end-tag
"]"
154 "String used to denote end of numbered footnote.
155 Should not be set to the empty string. Customizing this variable
156 has no effect on buffers already displaying footnotes."
160 (defcustom footnote-signature-separator
161 (if (boundp 'message-signature-separator
)
162 message-signature-separator
164 "Regexp used by Footnote mode to recognize signatures."
168 (defcustom footnote-align-to-fn-text t
169 "How to left-align footnote text.
170 If nil, footnote text is to be aligned flush left with left side
171 of the footnote number. If non-nil, footnote text is to be aligned
172 left with the first character of footnote text."
175 ;;; Private variables
177 (defvar footnote-style-number nil
178 "Footnote style represented as an index into footnote-style-alist.")
179 (make-variable-buffer-local 'footnote-style-number
)
181 (defvar footnote-text-marker-alist nil
182 "List of markers pointing to text of footnotes in message buffer.")
183 (make-variable-buffer-local 'footnote-text-marker-alist
)
185 (defvar footnote-pointer-marker-alist nil
186 "List of markers pointing to footnote pointers in message buffer.")
187 (make-variable-buffer-local 'footnote-pointer-marker-alist
)
189 (defvar footnote-mouse-highlight
'highlight
190 "Text property name to enable mouse over highlight.")
192 (defvar footnote-mode
)
196 (defconst footnote-numeric-regexp
"[0-9]+"
197 "Regexp for digits.")
199 (defun footnote--numeric (n)
200 "Numeric footnote style.
201 Use Arabic numerals for footnoting."
205 (defconst footnote-english-upper
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
206 "Upper case English alphabet.")
208 (defconst footnote-english-upper-regexp
"[A-Z]+"
209 "Regexp for upper case English alphabet.")
211 (defun footnote--english-upper (n)
212 "Upper case English footnoting.
213 Wrapping around the alphabet implies successive repetitions of letters."
214 (let* ((ltr (mod (1- n
) (length footnote-english-upper
)))
215 (rep (/ (1- n
) (length footnote-english-upper
)))
216 (chr (char-to-string (aref footnote-english-upper ltr
)))
219 (setq rc
(concat rc chr
))
224 (defconst footnote-english-lower
"abcdefghijklmnopqrstuvwxyz"
225 "Lower case English alphabet.")
227 (defconst footnote-english-lower-regexp
"[a-z]+"
228 "Regexp of lower case English alphabet.")
230 (defun footnote--english-lower (n)
231 "Lower case English footnoting.
232 Wrapping around the alphabet implies successive repetitions of letters."
233 (let* ((ltr (mod (1- n
) (length footnote-english-lower
)))
234 (rep (/ (1- n
) (length footnote-english-lower
)))
235 (chr (char-to-string (aref footnote-english-lower ltr
)))
238 (setq rc
(concat rc chr
))
243 (defconst footnote-roman-lower-list
244 '((1 .
"i") (5 .
"v") (10 .
"x")
245 (50 .
"l") (100 .
"c") (500 .
"d") (1000 .
"m"))
246 "List of roman numerals with their values.")
248 (defconst footnote-roman-lower-regexp
249 (concat "[" (mapconcat #'cdr footnote-roman-lower-list
"") "]+")
250 "Regexp of roman numerals.")
252 (defun footnote--roman-lower (n)
253 "Generic Roman number footnoting."
254 (footnote--roman-common n footnote-roman-lower-list
))
257 (defconst footnote-roman-upper-list
258 (mapcar (lambda (x) (cons (car x
) (upcase (cdr x
))))
259 footnote-roman-lower-list
)
260 "List of roman numerals with their values.")
262 (defconst footnote-roman-upper-regexp
(upcase footnote-roman-lower-regexp
)
263 "Regexp of roman numerals. Not complete")
265 (defun footnote--roman-upper (n)
266 "Generic Roman number footnoting."
267 (footnote--roman-common n footnote-roman-upper-list
))
269 (defun footnote--roman-common (n footnote-roman-list
)
270 "Lower case Roman footnoting."
271 (let* ((our-list footnote-roman-list
)
272 (rom-lngth (length our-list
))
278 ;; find surrounding numbers
279 (while (and (<= count-high
(1- rom-lngth
))
280 (>= n
(car (nth count-high our-list
))))
281 ;; (message "Checking %d" (car (nth count-high our-list)))
282 (setq count-high
(1+ count-high
)))
283 (setq rom-high count-high
)
284 (setq rom-low
(1- count-high
))
285 ;; find the appropriate divisor (if it exists)
286 (while (and (= rom-div -
1)
287 (< count-low rom-high
))
288 (when (or (> n
(- (car (nth rom-high our-list
))
289 (/ (car (nth count-low our-list
))
291 (= n
(- (car (nth rom-high our-list
))
292 (car (nth count-low our-list
)))))
293 (setq rom-div count-low
))
294 ;; (message "Checking %d and %d in div loop" rom-high count-low)
295 (setq count-low
(1+ count-low
)))
296 ;;(message "We now have high: %d, low: %d, div: %d, n: %d"
297 ;; rom-high rom-low (if rom-div rom-div -1) n)
298 (let ((rom-low-pair (nth rom-low our-list
))
299 (rom-high-pair (nth rom-high our-list
))
300 (rom-div-pair (if (not (= rom-div -
1)) (nth rom-div our-list
) nil
)))
301 ;; (message "pairs are: rom-low: %S, rom-high: %S, rom-div: %S"
302 ;; rom-low-pair rom-high-pair rom-div-pair)
304 ((< n
0) (error "footnote--roman-common called with n < 0"))
306 ((= n
(car rom-low-pair
)) (cdr rom-low-pair
))
307 ((= n
(car rom-high-pair
)) (cdr rom-high-pair
))
308 ((= (car rom-low-pair
) (car rom-high-pair
))
309 (concat (cdr rom-low-pair
)
310 (footnote--roman-common
311 (- n
(car rom-low-pair
))
312 footnote-roman-list
)))
313 ((>= rom-div
0) (concat (cdr rom-div-pair
) (cdr rom-high-pair
)
314 (footnote--roman-common
315 (- n
(- (car rom-high-pair
)
317 footnote-roman-list
)))
318 (t (concat (cdr rom-low-pair
)
319 (footnote--roman-common
320 (- n
(car rom-low-pair
))
321 footnote-roman-list
)))))))
325 (defconst footnote-latin-string
"¹²³ºª§¶"
326 "String of Latin-1 footnoting characters.")
328 ;; Note not [...]+, because this style cycles.
329 (defconst footnote-latin-regexp
(concat "[" footnote-latin-string
"]")
330 "Regexp for Latin-1 footnoting characters.")
332 (defun footnote--latin (n)
333 "Latin-1 footnote style.
334 Use a range of Latin-1 non-ASCII characters for footnoting."
335 (string (aref footnote-latin-string
336 (mod (1- n
) (length footnote-latin-string
)))))
340 (defconst footnote-unicode-string
"⁰¹²³⁴⁵⁶⁷⁸⁹"
341 "String of Unicode footnoting characters.")
343 (defconst footnote-unicode-regexp
(concat "[" footnote-unicode-string
"]+")
344 "Regexp for Unicode footnoting characters.")
346 (defun footnote--unicode (n)
347 "Unicode footnote style.
348 Use Unicode characters for footnoting."
349 (let (modulus result done
)
351 (setq modulus
(mod n
10)
353 (and (zerop n
) (setq done t
))
354 (push (aref footnote-unicode-string modulus
) result
))
355 (apply #'string result
)))
359 (defconst footnote-hebrew-numeric
361 ("א" "ב" "ג" "ד" "ה" "ו" "ז" "ח" "ט")
362 ("י" "כ" "ל" "מ" "נ" "ס" "ע" "פ" "צ")
363 ("ק" "ר" "ש" "ת" "תק" "תר" "תש" "תת" "תתק")))
365 (defconst footnote-hebrew-numeric-regex
366 (concat "[" (apply #'concat
(apply #'append footnote-hebrew-numeric
)) "']+"))
367 ;; (defconst footnote-hebrew-numeric-regex "\\([אבגדהוזחט]'\\)?\\(ת\\)?\\(ת\\)?\\([קרשת]\\)?\\([טיכלמנסעפצ]\\)?\\([אבגדהוזחט]\\)?")
369 (defun footnote--hebrew-numeric (n)
370 "Supports 9999 footnotes, then rolls over."
371 (let* ((n (+ (mod n
10000) (/ n
10000)))
372 (thousands (/ n
1000))
373 (hundreds (/ (mod n
1000) 100))
374 (tens (/ (mod n
100) 10))
377 ((not (= tens
1)) nil
)
379 ((= units
6) "טז"))))
381 (when (/= 0 thousands
)
382 (concat (nth (1- thousands
) (nth 0 footnote-hebrew-numeric
)) "'"))
383 (when (/= 0 hundreds
)
384 (nth (1- hundreds
) (nth 2 footnote-hebrew-numeric
)))
387 (when (/= 0 tens
) (nth (1- tens
) (nth 1 footnote-hebrew-numeric
)))
388 (when (/= 0 units
) (nth (1- units
) (nth 0 footnote-hebrew-numeric
))))))))
390 (defconst footnote-hebrew-symbolic
392 "א" "ב" "ג" "ד" "ה" "ו" "ז" "ח" "ט" "י" "כ" "ל" "מ" "נ" "ס" "ע" "פ" "צ" "ק" "ר" "ש" "ת"))
394 (defconst footnote-hebrew-symbolic-regex
395 (concat "[" (apply #'concat footnote-hebrew-symbolic
) "]"))
397 (defun footnote--hebrew-symbolic (n)
398 "Only 22 elements, per the style of eg. 'פירוש שפתי חכמים על רש״י'.
399 Proceeds from `י' to `כ', from `צ' to `ק'. After `ת', rolls over to `א'."
400 (nth (mod (1- n
) 22) footnote-hebrew-symbolic
))
402 ;;; list of all footnote styles
403 (defvar footnote-style-alist
404 `((numeric footnote--numeric
,footnote-numeric-regexp
)
405 (english-lower footnote--english-lower
,footnote-english-lower-regexp
)
406 (english-upper footnote--english-upper
,footnote-english-upper-regexp
)
407 (roman-lower footnote--roman-lower
,footnote-roman-lower-regexp
)
408 (roman-upper footnote--roman-upper
,footnote-roman-upper-regexp
)
409 (latin footnote--latin
,footnote-latin-regexp
)
410 (unicode footnote--unicode
,footnote-unicode-regexp
)
411 (hebrew-numeric footnote--hebrew-numeric
,footnote-hebrew-numeric-regex
)
412 (hebrew-symbolic footnote--hebrew-symbolic
,footnote-hebrew-symbolic-regex
))
413 "Styles of footnote tags available.
414 By default, Arabic numbers, English letters, Roman Numerals,
415 Latin and Unicode superscript characters, and Hebrew numerals
417 Each element of the list should be of the form (NAME FUNCTION REGEXP)
418 where NAME is a symbol, FUNCTION takes a footnote number and
419 returns the corresponding representation in that style as a string,
420 and REGEXP should be a regexp that matches any output of FUNCTION.")
422 (defcustom footnote-style
'numeric
423 "Default style used for footnoting.
424 numeric == 1, 2, 3, ...
425 english-lower == a, b, c, ...
426 english-upper == A, B, C, ...
427 roman-lower == i, ii, iii, iv, v, ...
428 roman-upper == I, II, III, IV, V, ...
429 latin == ¹ ² ³ º ª § ¶
430 unicode == ¹, ², ³, ...
431 hebrew-numeric == א, ב, ..., יא, ..., תקא...
432 hebrew-symbolic == א, ב, ..., י, כ, ..., צ, ק, ..., ת, א
433 See also variables `footnote-start-tag' and `footnote-end-tag'.
435 Note: some characters in the unicode style may not show up
436 properly if the default font does not contain those characters.
438 Customizing this variable has no effect on buffers already
439 displaying footnotes. To change the style of footnotes in such a
440 buffer use the command `footnote-set-style'."
441 :type
(cons 'choice
(mapcar (lambda (x) (list 'const
(car x
)))
442 footnote-style-alist
))
445 ;;; Style utilities & functions
446 (defun footnote--style-p (style)
447 "Return non-nil if style is a valid style known to `footnote-mode'."
448 (assq style footnote-style-alist
))
450 (defun footnote--index-to-string (index)
451 "Convert a binary index into a string to display as a footnote.
452 Conversion is done based upon the current selected style."
453 (let ((alist (if (footnote--style-p footnote-style
)
454 (assq footnote-style footnote-style-alist
)
455 (nth 0 footnote-style-alist
))))
456 (funcall (nth 1 alist
) index
)))
458 (defun footnote--current-regexp ()
459 "Return the regexp of the index of the current style."
460 (concat (nth 2 (or (assq footnote-style footnote-style-alist
)
461 (nth 0 footnote-style-alist
)))
464 (defun footnote--refresh-footnotes (&optional index-regexp
)
465 "Redraw all footnotes.
466 You must call this or arrange to have this called after changing footnote
469 (setq index-regexp
(footnote--current-regexp)))
471 ;; Take care of the pointers first
472 (let ((i 0) locn alist
)
473 (while (setq alist
(nth i footnote-pointer-marker-alist
))
474 (setq locn
(cdr alist
))
476 (goto-char (car locn
))
477 ;; Try to handle the case where `footnote-start-tag' and
478 ;; `footnote-end-tag' are the same string.
479 (when (looking-back (concat
480 (regexp-quote footnote-start-tag
)
481 "\\(" index-regexp
"+\\)"
482 (regexp-quote footnote-end-tag
))
483 (line-beginning-position))
488 (footnote--index-to-string (1+ i
))
490 'footnote-number
(1+ i
) footnote-mouse-highlight t
)
492 (setq locn
(cdr locn
)))
495 ;; Now take care of the text section
497 (while (setq alist
(nth i footnote-text-marker-alist
))
498 (goto-char (cdr alist
))
499 (when (looking-at (concat
500 (regexp-quote footnote-start-tag
)
501 "\\(" index-regexp
"+\\)"
502 (regexp-quote footnote-end-tag
)))
507 (footnote--index-to-string (1+ i
))
509 'footnote-number
(1+ i
))
513 (defun footnote--assoc-index (key alist
)
514 "Give index of key in alist."
515 (let ((i 0) (max (length alist
)) rc
)
516 (while (and (null rc
)
518 (when (eq key
(car (nth i alist
)))
523 (defun footnote-cycle-style ()
524 "Select next defined footnote style."
526 (let ((old (footnote--assoc-index footnote-style footnote-style-alist
))
527 (max (length footnote-style-alist
))
532 (setq footnote-style
(car (nth idx footnote-style-alist
)))
533 (footnote--refresh-footnotes (nth 2 (nth old footnote-style-alist
)))))
535 (defun footnote-set-style (&optional style
)
536 "Select a specific style."
538 (list (intern (completing-read
540 obarray
#'footnote--style-p
'require-match
))))
541 (let ((old (footnote--assoc-index footnote-style footnote-style-alist
)))
542 (setq footnote-style style
)
543 (footnote--refresh-footnotes (nth 2 (nth old footnote-style-alist
)))))
545 ;; Internal functions
546 (defun footnote--insert-numbered-footnote (arg &optional mousable
)
547 "Insert numbered footnote at (point)."
548 (let ((string (concat footnote-start-tag
549 (footnote--index-to-string arg
)
551 (insert-before-markers
554 string
'footnote-number arg footnote-mouse-highlight t
)
555 (propertize string
'footnote-number arg
)))))
557 (defun footnote--renumber (_from to pointer-alist text-alist
)
558 "Renumber a single footnote."
559 (let* ((posn-list (cdr pointer-alist
)))
560 (setcar pointer-alist to
)
561 (setcar text-alist to
)
563 (goto-char (car posn-list
))
564 (when (looking-back (concat (regexp-quote footnote-start-tag
)
565 (footnote--current-regexp)
566 (regexp-quote footnote-end-tag
))
567 (line-beginning-position))
570 (concat footnote-start-tag
571 (footnote--index-to-string to
)
573 'footnote-number to footnote-mouse-highlight t
)))
574 (setq posn-list
(cdr posn-list
)))
575 (goto-char (cdr text-alist
))
576 (when (looking-at (concat (regexp-quote footnote-start-tag
)
577 (footnote--current-regexp)
578 (regexp-quote footnote-end-tag
)))
581 (concat footnote-start-tag
582 (footnote--index-to-string to
)
584 'footnote-number to
)))))
586 (defun footnote--narrow-to-footnotes ()
587 "Restrict text in buffer to show only text of footnotes."
588 (interactive) ; testing
589 (narrow-to-region (footnote--get-area-point-min)
590 (footnote--get-area-point-max)))
592 (defun footnote--goto-char-point-max ()
593 "Move to end of buffer or prior to start of .signature."
594 (goto-char (point-max))
595 (or (re-search-backward footnote-signature-separator nil t
)
598 (defun footnote--insert-text-marker (arg locn
)
599 "Insert a marker pointing to footnote ARG, at buffer location LOCN."
600 (let ((marker (make-marker)))
601 (unless (assq arg footnote-text-marker-alist
)
602 (set-marker marker locn
)
603 (setq footnote-text-marker-alist
604 (cons (cons arg marker
) footnote-text-marker-alist
))
605 (setq footnote-text-marker-alist
606 (footnote--sort footnote-text-marker-alist
)))))
608 (defun footnote--insert-pointer-marker (arg locn
)
609 "Insert a marker pointing to footnote ARG, at buffer location LOCN."
610 (let ((marker (make-marker))
612 (set-marker marker locn
)
613 (if (setq alist
(assq arg footnote-pointer-marker-alist
))
615 (cons marker
(cdr alist
)))
616 (setq footnote-pointer-marker-alist
617 (cons (cons arg
(list marker
)) footnote-pointer-marker-alist
))
618 (setq footnote-pointer-marker-alist
619 (footnote--sort footnote-pointer-marker-alist
)))))
621 (defun footnote--insert-footnote (arg)
622 "Insert a footnote numbered ARG, at (point)."
624 (footnote--insert-pointer-marker arg
(point))
625 (footnote--insert-numbered-footnote arg t
)
626 (footnote--goto-char-point-max)
628 ((not (string-equal footnote-section-tag
""))
629 (re-search-backward (concat "^" footnote-section-tag-regexp
) nil t
))
630 (footnote-text-marker-alist
631 (goto-char (cdar footnote-text-marker-alist
))))
633 (when footnote-narrow-to-footnotes-when-editing
634 (footnote--narrow-to-footnotes))
635 (footnote-goto-footnote (1- arg
)) ; evil, FIXME (less evil now)
636 ;; (message "Inserting footnote %d" arg)
639 (when (re-search-forward
640 (if footnote-spaced-footnotes
643 (regexp-quote footnote-start-tag
)
644 (footnote--current-regexp)
645 (regexp-quote footnote-end-tag
)))
647 (unless (beginning-of-line) t
))
648 (footnote--goto-char-point-max)
650 ((not (string-equal footnote-section-tag
""))
652 (concat "^" footnote-section-tag-regexp
) nil t
))
653 (footnote-text-marker-alist
654 (goto-char (cdar footnote-text-marker-alist
)))))))
655 (unless (looking-at "^$")
659 (unless (string-equal footnote-section-tag
"")
660 (insert footnote-section-tag
"\n")))
661 (let ((old-point (point)))
662 (footnote--insert-numbered-footnote arg nil
)
663 (footnote--insert-text-marker arg old-point
)))
665 (defun footnote--sort (list)
666 (sort list
(lambda (e1 e2
)
667 (< (car e1
) (car e2
)))))
669 (defun footnote--text-under-cursor ()
670 "Return the number of the current footnote if in footnote text.
671 Return nil if the cursor is not positioned over the text of
673 (when (and footnote-text-marker-alist
674 (<= (footnote--get-area-point-min)
676 (footnote--get-area-point-max)))
677 (let ((i 1) alist-txt result
)
678 (while (and (setq alist-txt
(nth i footnote-text-marker-alist
))
680 (when (< (point) (cdr alist-txt
))
681 (setq result
(car (nth (1- i
) footnote-text-marker-alist
))))
683 (when (and (null result
) (null alist-txt
))
684 (setq result
(car (nth (1- i
) footnote-text-marker-alist
))))
687 (defun footnote--under-cursor ()
688 "Return the number of the footnote underneath the cursor.
689 Return nil if the cursor is not over a footnote."
690 (or (get-text-property (point) 'footnote-number
)
691 (footnote--text-under-cursor)))
693 (defun footnote--calc-fn-alignment-column ()
694 "Calculate the left alignment for footnote text."
695 ;; FIXME: Maybe it would be better to go to the footnote's beginning and
696 ;; see at which column it starts.
697 (+ footnote-body-tag-spacing
699 (concat footnote-start-tag footnote-end-tag
700 (footnote--index-to-string
701 (caar (last footnote-text-marker-alist
)))))))
703 (defun footnote--fill-prefix-string ()
704 "Return the fill prefix to be used by footnote mode."
705 ;; TODO: Prefix to this value other prefix strings, such as those
706 ;; designating a comment line, a message response, or a boxquote.
707 (make-string (footnote--calc-fn-alignment-column) ?\s
))
709 (defun footnote--point-in-body-p ()
710 "Return non-nil if point is in the buffer text area,
711 i.e. before the beginning of the footnote area."
712 (< (point) (footnote--get-area-point-min)))
714 (defun footnote--get-area-point-min (&optional before-tag
)
715 "Return start of the first footnote.
716 If there is no footnote area, returns `point-max'.
717 With optional arg BEFORE-TAG, return position of the `footnote-section-tag'
718 instead, if applicable."
720 ;; FIXME: Shouldn't we use `footnote--get-area-point-max' instead?
721 ((not footnote-text-marker-alist
) (point-max))
722 ((not before-tag
) (cdr (car footnote-text-marker-alist
)))
723 ((string-equal footnote-section-tag
"")
724 (cdr (car footnote-text-marker-alist
)))
727 (goto-char (cdr (car footnote-text-marker-alist
)))
728 (if (re-search-backward (concat "^" footnote-section-tag-regexp
) nil t
)
730 (message "Footnote section tag not found!")
731 ;; This `else' should never happen, and indicates an error,
732 ;; ie. footnotes already exist and a footnote-section-tag is defined,
733 ;; but the section tag hasn't been found. We choose to assume that the
734 ;; user deleted it intentionally and wants us to behave in this buffer
735 ;; as if the section tag was set "", so we do that, now.
736 ;;(setq footnote-section-tag "")
738 ;; HOWEVER: The rest of footnote mode does not currently honor or
741 ;; To illustrate the difference in behavior, create a few footnotes,
742 ;; delete the section tag, and create another footnote. Then undo,
743 ;; comment the above line (that sets the tag to ""), re-evaluate this
744 ;; function, and repeat.
746 ;; TODO: integrate sanity checks at reasonable operational points.
747 (cdr (car footnote-text-marker-alist
)))))))
749 (defun footnote--get-area-point-max ()
750 "Return the end of footnote area.
751 This is either `point-max' or the start of a `.signature' string, as
752 defined by variable `footnote-signature-separator'. If there is no
753 footnote area, returns `point-max'."
754 (save-excursion (footnote--goto-char-point-max)))
756 (defun footnote--adaptive-fill-function (orig-fun)
760 footnote-align-to-fn-text
761 (footnote--text-under-cursor)
762 ;; (not (footnote--point-in-body-p))
763 ;; (< (point) (footnote--signature-area-start-point))
764 (footnote--fill-prefix-string))
765 ;; If not within a footnote's text, fallback to the default.
770 (defun footnote--make-hole ()
773 (notes (length footnote-pointer-marker-alist
))
774 alist-ptr alist-txt rc
)
776 (setq alist-ptr
(nth i footnote-pointer-marker-alist
))
777 (setq alist-txt
(nth i footnote-text-marker-alist
))
778 (when (< (point) (- (cadr alist-ptr
) 3))
780 (setq rc
(car alist-ptr
)))
782 (message "Renumbering from %s to %s"
783 (footnote--index-to-string (car alist-ptr
))
784 (footnote--index-to-string
785 (1+ (car alist-ptr
))))
786 (footnote--renumber (car alist-ptr
)
793 (defun footnote-add-footnote ()
794 "Add a numbered footnote.
795 The number the footnote receives is dependent upon the relative location
796 of any other previously existing footnotes.
797 If the variable `footnote-narrow-to-footnotes-when-editing' is set,
798 the buffer is narrowed to the footnote body. The restriction is removed
799 by using `footnote-back-to-message'."
802 (if footnote-text-marker-alist
803 (if (< (point) (cl-cadar (last footnote-pointer-marker-alist
)))
804 (footnote--make-hole)
805 (1+ (caar (last footnote-text-marker-alist
))))
807 (message "Adding footnote %d" num
)
808 (footnote--insert-footnote num
)
809 (insert-before-markers (make-string footnote-body-tag-spacing ?
))
810 (let ((opoint (point)))
812 (insert-before-markers
813 (if footnote-spaced-footnotes
816 (when footnote-narrow-to-footnotes-when-editing
817 (footnote--narrow-to-footnotes)))
818 ;; Emacs/XEmacs bug? save-excursion doesn't restore point when using
819 ;; insert-before-markers.
820 (goto-char opoint
))))
822 (defun footnote-delete-footnote (&optional arg
)
823 "Delete a numbered footnote.
824 With no parameter, delete the footnote under (point). With ARG specified,
825 delete the footnote with that number."
828 (setq arg
(footnote--under-cursor)))
830 (or (not footnote-prompt-before-deletion
)
831 (y-or-n-p (format "Really delete footnote %d?" arg
))))
832 (let (alist-ptr alist-txt locn
)
833 (setq alist-ptr
(assq arg footnote-pointer-marker-alist
))
834 (setq alist-txt
(assq arg footnote-text-marker-alist
))
835 (unless (and alist-ptr alist-txt
)
836 (error "Can't delete footnote %d" arg
))
837 (setq locn
(cdr alist-ptr
))
840 (goto-char (car locn
))
841 (when (looking-back (concat (regexp-quote footnote-start-tag
)
842 (footnote--current-regexp)
843 (regexp-quote footnote-end-tag
))
844 (line-beginning-position))
845 (delete-region (match-beginning 0) (match-end 0))))
846 (setq locn
(cdr locn
)))
848 (goto-char (cdr alist-txt
))
851 (if footnote-spaced-footnotes
852 (search-forward "\n\n" nil t
)
853 (save-restriction ; <= 2017-12 Boruch: WHY?? I see no narrowing / widening here.
855 (next-single-char-property-change
856 (point) 'footnote-number nil
(footnote--goto-char-point-max))))))
857 (setq footnote-pointer-marker-alist
858 (delq alist-ptr footnote-pointer-marker-alist
))
859 (setq footnote-text-marker-alist
860 (delq alist-txt footnote-text-marker-alist
))
861 (footnote-renumber-footnotes)
862 (when (and (null footnote-text-marker-alist
)
863 (null footnote-pointer-marker-alist
))
865 (if (not (string-equal footnote-section-tag
""))
866 (let* ((end (footnote--goto-char-point-max))
867 (start (1- (re-search-backward
868 (concat "^" footnote-section-tag-regexp
)
871 (when (looking-at "\n")
873 (delete-region start
(if (< end
(point-max))
876 (footnote--goto-char-point-max)
877 (when (looking-back "\n\n" (- (point) 2))
878 (kill-line -
1))))))))
880 (defun footnote-renumber-footnotes ()
881 "Renumber footnotes, starting from 1."
885 (notes (length footnote-pointer-marker-alist
))
888 (setq alist-ptr
(nth i footnote-pointer-marker-alist
))
889 (setq alist-txt
(nth i footnote-text-marker-alist
))
890 (unless (= (1+ i
) (car alist-ptr
))
891 (footnote--renumber (car alist-ptr
) (1+ i
) alist-ptr alist-txt
))
894 (defun footnote-goto-footnote (&optional arg
)
895 "Jump to the text of a footnote.
896 With no parameter, jump to the text of the footnote under (point). With ARG
897 specified, jump to the text of that footnote."
900 (setq arg
(footnote--under-cursor)))
901 (let ((footnote (assq arg footnote-text-marker-alist
)))
904 (goto-char (cdr footnote
)))
906 (goto-char (point-max))
908 ((not (string-equal footnote-section-tag
""))
909 (re-search-backward (concat "^" footnote-section-tag-regexp
))
911 (footnote-text-marker-alist
912 (goto-char (cdar footnote-text-marker-alist
)))))
914 (error "I don't see a footnote here")))))
916 (defun footnote-back-to-message ()
917 "Move cursor back to footnote referent.
918 If the cursor is not over the text of a footnote, point is not changed.
919 If the buffer was narrowed due to `footnote-narrow-to-footnotes-when-editing'
920 being set it is automatically widened."
922 (let ((note (footnote--text-under-cursor)))
924 (when footnote-narrow-to-footnotes-when-editing
926 (goto-char (cadr (assq note footnote-pointer-marker-alist
))))))
928 (defvar footnote-mode-map
929 (let ((map (make-sparse-keymap)))
930 (define-key map
"a" 'footnote-add-footnote
)
931 (define-key map
"b" 'footnote-back-to-message
)
932 (define-key map
"c" 'footnote-cycle-style
)
933 (define-key map
"d" 'footnote-delete-footnote
)
934 (define-key map
"g" 'footnote-goto-footnote
)
935 (define-key map
"r" 'footnote-renumber-footnotes
)
936 (define-key map
"s" 'footnote-set-style
)
939 (defvar footnote-minor-mode-map
940 (let ((map (make-sparse-keymap)))
941 (define-key map footnote-prefix footnote-mode-map
)
943 "Keymap used for binding footnote minor mode.")
946 (define-minor-mode footnote-mode
947 "Toggle Footnote mode.
948 With a prefix argument ARG, enable Footnote mode if ARG is
949 positive, and disable it otherwise. If called from Lisp, enable
950 the mode if ARG is omitted or nil.
952 Footnote mode is a buffer-local minor mode. If enabled, it
953 provides footnote support for `message-mode'. To get started,
954 play around with the following keys:
955 \\{footnote-minor-mode-map}"
956 :lighter footnote-mode-line-string
957 :keymap footnote-minor-mode-map
958 ;; (filladapt-mode t)
959 (unless adaptive-fill-function
960 ;; nil and `ignore' have the same semantics for adaptive-fill-function,
961 ;; but only `ignore' behaves correctly with add/remove-function.
962 (setq adaptive-fill-function
#'ignore
))
963 (remove-function (local 'adaptive-fill-function
)
964 #'footnote--adaptive-fill-function
)
966 ;; (footnote-setup-keybindings)
967 (make-local-variable 'footnote-style
)
968 (make-local-variable 'footnote-body-tag-spacing
)
969 (make-local-variable 'footnote-spaced-footnotes
)
970 (make-local-variable 'footnote-section-tag
)
971 (make-local-variable 'footnote-section-tag-regexp
)
972 (make-local-variable 'footnote-start-tag
)
973 (make-local-variable 'footnote-end-tag
)
974 (make-local-variable 'adaptive-fill-function
)
975 (add-function :around
(local 'adaptive-fill-function
)
976 #'footnote--adaptive-fill-function
)
978 ;; filladapt is an XEmacs package which AFAIK has never been ported
980 (when (boundp 'filladapt-token-table
)
981 ;; add tokens to filladapt to match footnotes
982 ;; 1] xxxxxxxxxxx x x x or [1] x x x x x x x
983 ;; xxx x xx xxx xxxx x x x xxxxxxxxxx
984 (let ((bullet-regexp (concat (regexp-quote footnote-start-tag
)
986 (regexp-quote footnote-end-tag
)
988 (unless (assoc bullet-regexp filladapt-token-table
)
989 (setq filladapt-token-table
990 (append filladapt-token-table
991 (list (list bullet-regexp
'bullet
)))))))))
995 ;;; footnote.el ends here