Mercurial > emacs
comparison lisp/mail/footnote.el @ 26784:4d1698b6a3bb
Current XEmacs version.
author | Dave Love <fx@gnu.org> |
---|---|
date | Thu, 09 Dec 1999 23:31:50 +0000 |
parents | |
children | 9fa9fd18d083 |
comparison
equal
deleted
inserted
replaced
26783:626ae4bb4994 | 26784:4d1698b6a3bb |
---|---|
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 | |
9 ;; This file is part of XEmacs. | |
10 | |
11 ;; XEmacs is free software; you can redistribute it and/or modify it | |
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 | |
16 ;; XEmacs is distributed in the hope that it will be useful, but | |
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 | |
22 ;; along with XEmacs; see the file COPYING. If not, write to the Free | |
23 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
24 ;; 02111-1307, USA. | |
25 | |
26 ;;; Synched up with: Not in FSF | |
27 | |
28 ;;; Commentary: | |
29 | |
30 ;; This file provides footnote[1] support for message-mode in emacsen. | |
31 ;; footnote-mode is implemented as a minor mode. | |
32 | |
33 ;; [1] Footnotes look something like this. Along with some decorative | |
34 ;; stuff. | |
35 | |
36 ;; TODO: | |
37 ;; Reasonable Undo support. | |
38 ;; more language styles. | |
39 | |
40 ;;; Change Log: | |
41 | |
42 ;; May-31-1998: In `Footnote-make-hole', `concat' was called with integer | |
43 ;; argument; now use `Footnote-index-to-string' and `format' | |
44 ;; Apr-04-1997: Added option to narrow buffer when editing the text of | |
45 ;; a footnote. | |
46 ;; Insertion and renumbering now works. | |
47 ;; Deletion and renumbering now works. | |
48 ;; Footnote styles implemented. | |
49 ;; Apr-05-1997: Added dumb version of footnote-set-style. | |
50 ;; Merged minor corrections from Hrvoje Niksic, Sudish Joseph, | |
51 ;; and David Moore. | |
52 ;; Remove absolute dependency on message-mode. | |
53 ;; Replicate letters when footnote numbers hit the end of | |
54 ;; the alphabet. | |
55 ;; Apr-06-1997: Emacs portability patches from Lars Magne Ingebrigtsen. | |
56 ;; Apr-18-1997: Stricter matching of footnote tag. (Idea from Colin Rafferty) | |
57 ;; May-16-1997: Allow customization of spacing of footnote body tag. (Idea | |
58 ;; from Samuel Tardieu). | |
59 | |
60 ;;; Code: | |
61 | |
62 (defgroup footnote nil | |
63 "Support for footnotes in mail and news messages." | |
64 :group 'message) | |
65 | |
66 ;;;###autoload | |
67 (defcustom footnote-mode-line-string " FN" | |
68 "*String to display in modes section of the mode-line." | |
69 :group 'footnote) | |
70 | |
71 (defcustom footnote-mode-hook nil | |
72 "*Hook functions run when footnote-mode is activated." | |
73 :type 'hook | |
74 :group 'footnote) | |
75 | |
76 (defcustom footnote-narrow-to-footnotes-when-editing nil | |
77 "*If set, narrow to footnote text body while editing a footnote." | |
78 :type 'boolean | |
79 :group 'footnote) | |
80 | |
81 (defcustom footnote-prompt-before-deletion t | |
82 "*If set, prompt before deleting a footnote. | |
83 There is currently no way to undo deletions." | |
84 :type 'boolean | |
85 :group 'footnote) | |
86 | |
87 (defcustom footnote-spaced-footnotes t | |
88 "If set true it will put a blank line between each footnote. | |
89 If nil, no blank line will be inserted." | |
90 :type 'boolean | |
91 :group 'footnote) | |
92 | |
93 (defcustom footnote-style 'numeric | |
94 "*Style used for footnoting. | |
95 numeric == 1, 2, 3, ... | |
96 english-lower == a, b, c, ... | |
97 english-upper == A, B, C, ... | |
98 roman-lower == i, ii, iii, iv, v, ... | |
99 roman-upper == I, II, III, IV, V, ... | |
100 Some versions of XEmacs and Emacs/mule may support further styles | |
101 like 'hebrew, 'greek-lower, and 'greek-upper." | |
102 :type 'symbol | |
103 :group 'footnote) | |
104 | |
105 (defcustom footnote-use-message-mode t | |
106 "*If non-nil assume Footnoting will be done in message-mode." | |
107 :type 'boolean | |
108 :group 'footnote) | |
109 | |
110 (defcustom footnote-body-tag-spacing 2 | |
111 "*Number of blanks separating a footnote body tag and its text." | |
112 :type 'integer | |
113 :group 'footnote) | |
114 | |
115 ;;;###autoload | |
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 | |
459 (acons arg marker footnote-text-marker-alist)) | |
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 | |
472 (acons arg (list marker) footnote-pointer-marker-alist)) | |
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 ;;;###autoload | |
568 (defun Footnote-add-footnote (&optional arg) | |
569 "Add a numbered footnote. | |
570 The number the footnote receives is dependent upon the relative location | |
571 of any other previously existing footnotes. | |
572 If the variable `footnote-narrow-to-footnotes-when-editing' is set, | |
573 the buffer is narrowed to the footnote body. The restriction is removed | |
574 by using `Footnote-back-to-message'." | |
575 (interactive "*P") | |
576 (let (num) | |
577 (if footnote-text-marker-alist | |
578 (if (< (point) (cadar (last footnote-pointer-marker-alist))) | |
579 (setq num (Footnote-make-hole)) | |
580 (setq num (1+ (caar (last footnote-text-marker-alist))))) | |
581 (setq num 1)) | |
582 (message "Adding footnote %d" num) | |
583 (Footnote-insert-footnote num) | |
584 (insert-before-markers (make-string footnote-body-tag-spacing ? )) | |
585 (let ((opoint (point))) | |
586 (save-excursion | |
587 (insert-before-markers | |
588 (if footnote-spaced-footnotes | |
589 "\n\n" | |
590 "\n")) | |
591 (when footnote-narrow-to-footnotes-when-editing | |
592 (Footnote-narrow-to-footnotes))) | |
593 ;; Emacs/XEmacs bug? save-excursion doesn't restore point when using | |
594 ;; insert-before-markers. | |
595 (goto-char opoint)))) | |
596 | |
597 (defun Footnote-delete-footnote (&optional arg) | |
598 "Delete a numbered footnote. | |
599 With no parameter, delete the footnote under (point). With arg specified, | |
600 delete the footnote with that number." | |
601 (interactive "*P") | |
602 (unless arg | |
603 (setq arg (Footnote-under-cursor))) | |
604 (when (and arg | |
605 (or (not footnote-prompt-before-deletion) | |
606 (y-or-n-p (format "Really delete footnote %d?" arg)))) | |
607 (let (alist-ptr alist-txt locn) | |
608 (setq alist-ptr (assq arg footnote-pointer-marker-alist)) | |
609 (setq alist-txt (assq arg footnote-text-marker-alist)) | |
610 (unless (and alist-ptr alist-txt) | |
611 (error "Can't delete footnote %d" arg)) | |
612 (setq locn (cdr alist-ptr)) | |
613 (while (car locn) | |
614 (save-excursion | |
615 (goto-char (car locn)) | |
616 (let* ((end (point)) | |
617 (start (search-backward footnote-start-tag nil t))) | |
618 (kill-region start end))) | |
619 (setq locn (cdr locn))) | |
620 (save-excursion | |
621 (goto-char (cdr alist-txt)) | |
622 (kill-region (point) (search-forward "\n\n" nil t))) | |
623 (setq footnote-pointer-marker-alist | |
624 (delq alist-ptr footnote-pointer-marker-alist)) | |
625 (setq footnote-text-marker-alist | |
626 (delq alist-txt footnote-text-marker-alist)) | |
627 (Footnote-renumber-footnotes) | |
628 (when (and (null footnote-text-marker-alist) | |
629 (null footnote-pointer-marker-alist)) | |
630 (save-excursion | |
631 (let* ((end (Footnote-goto-char-point-max)) | |
632 (start (1- (re-search-backward | |
633 (concat "^" footnote-section-tag-regexp) | |
634 nil t)))) | |
635 (forward-line -1) | |
636 (when (looking-at "\n") | |
637 (kill-line)) | |
638 (kill-region start (if (< end (point-max)) | |
639 end | |
640 (point-max))))))))) | |
641 | |
642 (defun Footnote-renumber-footnotes (&optional arg) | |
643 "Renumber footnotes, starting from 1." | |
644 (interactive "*P") | |
645 (save-excursion | |
646 (let ((i 0) | |
647 (notes (length footnote-pointer-marker-alist)) | |
648 alist-ptr alist-txt) | |
649 (while (< i notes) | |
650 (setq alist-ptr (nth i footnote-pointer-marker-alist)) | |
651 (setq alist-txt (nth i footnote-text-marker-alist)) | |
652 (unless (eq (1+ i) (car alist-ptr)) | |
653 (Footnote-renumber (car alist-ptr) (1+ i) alist-ptr alist-txt)) | |
654 (setq i (1+ i)))))) | |
655 | |
656 (defun Footnote-goto-footnote (&optional arg) | |
657 "Jump to the text of a footnote. | |
658 With no parameter, jump to the text of the footnote under (point). With arg | |
659 specified, jump to the text of that footnote." | |
660 (interactive "P") | |
661 (setq zmacs-region-stays t) | |
662 (let (footnote) | |
663 (if arg | |
664 (setq footnote (assq arg footnote-text-marker-alist)) | |
665 (when (setq arg (Footnote-under-cursor)) | |
666 (setq footnote (assq arg footnote-text-marker-alist)))) | |
667 (if footnote | |
668 (goto-char (cdr footnote)) | |
669 (if (= arg 0) | |
670 (progn | |
671 (goto-char (point-max)) | |
672 (re-search-backward (concat "^" footnote-section-tag-regexp)) | |
673 (forward-line 1)) | |
674 (error "I don't see a footnote here."))))) | |
675 | |
676 (defun Footnote-back-to-message (&optional arg) | |
677 "Move cursor back to footnote referent. | |
678 If the cursor is not over the text of a footnote, point is not changed. | |
679 If the buffer was narrowed due to `footnote-narrow-to-footnotes-when-editing' | |
680 being set it is automatically widened." | |
681 (interactive "P") | |
682 (setq zmacs-region-stays t) | |
683 (let ((note (Footnote-text-under-cursor))) | |
684 (when note | |
685 (when footnote-narrow-to-footnotes-when-editing | |
686 (widen)) | |
687 (goto-char (cadr (assq note footnote-pointer-marker-alist)))))) | |
688 | |
689 ;;;###autoload | |
690 (defvar footnote-mode-map nil | |
691 "Keymap used for footnote minor mode.") | |
692 | |
693 ;; Set up our keys | |
694 ;;;###autoload | |
695 (unless footnote-mode-map | |
696 (setq footnote-mode-map (make-sparse-keymap)) | |
697 (define-key footnote-mode-map "a" 'Footnote-add-footnote) | |
698 (define-key footnote-mode-map "b" 'Footnote-back-to-message) | |
699 (define-key footnote-mode-map "c" 'Footnote-cycle-style) | |
700 (define-key footnote-mode-map "d" 'Footnote-delete-footnote) | |
701 (define-key footnote-mode-map "g" 'Footnote-goto-footnote) | |
702 (define-key footnote-mode-map "r" 'Footnote-renumber-footnotes) | |
703 (define-key footnote-mode-map "s" 'Footnote-set-style)) | |
704 | |
705 ;;;###autoload | |
706 (defvar footnote-minor-mode-map nil | |
707 "Keymap used for binding footnote minor mode.") | |
708 | |
709 ;;;###autoload | |
710 (unless footnote-minor-mode-map | |
711 (define-key global-map footnote-prefix footnote-mode-map)) | |
712 | |
713 ;;;###autoload | |
714 (defun footnote-mode (&optional arg) | |
715 "Toggle footnote minor mode. | |
716 \\<message-mode-map> | |
717 key binding | |
718 --- ------- | |
719 | |
720 \\[Footnote-renumber-footnotes] Footnote-renumber-footnotes | |
721 \\[Footnote-goto-footnote] Footnote-goto-footnote | |
722 \\[Footnote-delete-footnote] Footnote-delete-footnote | |
723 \\[Footnote-cycle-style] Footnote-cycle-style | |
724 \\[Footnote-back-to-message] Footnote-back-to-message | |
725 \\[Footnote-add-footnote] Footnote-add-footnote | |
726 " | |
727 (interactive "*P") | |
728 ;; (filladapt-mode t) | |
729 (setq zmacs-region-stays t) | |
730 (setq footnote-mode | |
731 (if (null arg) (not footnote-mode) | |
732 (> (prefix-numeric-value arg) 0))) | |
733 (when footnote-mode | |
734 ;; (Footnote-setup-keybindings) | |
735 (make-local-variable 'footnote-style) | |
736 (if (fboundp 'force-mode-line-update) | |
737 (force-mode-line-update) | |
738 (set-buffer-modified-p (buffer-modified-p))) | |
739 | |
740 (when (boundp 'filladapt-token-table) | |
741 ;; add tokens to filladapt to match footnotes | |
742 ;; 1] xxxxxxxxxxx x x x or [1] x x x x x x x | |
743 ;; xxx x xx xxx xxxx x x x xxxxxxxxxx | |
744 (let ((bullet-regexp (concat (regexp-quote footnote-start-tag) | |
745 "?[0-9a-zA-Z]+" | |
746 (regexp-quote footnote-end-tag) | |
747 "[ \t]"))) | |
748 (unless (assoc bullet-regexp filladapt-token-table) | |
749 (setq filladapt-token-table | |
750 (append filladapt-token-table | |
751 (list (list bullet-regexp 'bullet))))))) | |
752 | |
753 (run-hooks 'footnote-mode-hook))) | |
754 | |
755 ;; install on minor-mode-alist | |
756 ;;;###autoload | |
757 (when (fboundp 'add-minor-mode) | |
758 ;; XEmacs | |
759 (add-minor-mode 'footnote-mode | |
760 footnote-mode-line-string | |
761 footnote-minor-mode-map)) | |
762 | |
763 ;; Emacs -- don't autoload | |
764 (unless (assq 'footnote-mode minor-mode-alist) | |
765 (setq minor-mode-alist | |
766 (cons '(footnote-mode footnote-mode-line-string) | |
767 minor-mode-alist))) | |
768 | |
769 (provide 'footnote) | |
770 | |
771 ;;; footnote.el ends here |