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