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