Replace `string-to-int' by `string-to-number'.
[bpt/emacs.git] / lisp / mail / supercite.el
1 ;;; supercite.el --- minor mode for citing mail and news replies
2
3 ;; Copyright (C) 1993, 1997, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5 ;; Author: 1993 Barry A. Warsaw <bwarsaw@python.org>
6 ;; Maintainer: FSF
7 ;; Created: February 1993
8 ;; Last Modified: 1993/09/22 18:58:46
9 ;; Keywords: mail, news
10
11 ;; supercite.el revision: 3.54
12
13 ;; This file is part of GNU Emacs.
14
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; any later version.
19
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
29
30 ;; LCD Archive Entry
31 ;; supercite|Barry A. Warsaw|supercite-help@python.org
32 ;; |Mail and news reply citation package
33 ;; |1993/09/22 18:58:46|3.1|
34
35 ;;; Commentary:
36
37 ;;; Code:
38
39 \f
40 (require 'regi)
41 (require 'sendmail) ;; For mail-header-end.
42
43 ;; start user configuration variables
44 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
45
46 (defgroup supercite nil
47 "Supercite package"
48 :prefix "sc-"
49 :group 'mail
50 :group 'news)
51
52 (defgroup supercite-frames nil
53 "Supercite (regi) frames"
54 :prefix "sc-"
55 :group 'supercite)
56
57 (defgroup supercite-attr nil
58 "Supercite attributions"
59 :prefix "sc-"
60 :group 'supercite)
61
62 (defgroup supercite-cite nil
63 "Supercite citings"
64 :prefix "sc-"
65 :group 'supercite)
66
67 (defgroup supercite-hooks nil
68 "Hooking into supercite"
69 :prefix "sc-"
70 :group 'supercite)
71
72 (defcustom sc-auto-fill-region-p t
73 "*If non-nil, automatically fill each paragraph after it has been cited."
74 :type 'boolean
75 :group 'supercite)
76
77 (defcustom sc-blank-lines-after-headers 1
78 "*Number of blank lines to leave after mail headers have been nuked.
79 Set to nil, to use whatever blank lines happen to occur naturally."
80 :type '(choice (const :tag "leave" nil)
81 integer)
82 :group 'supercite)
83
84 (defcustom sc-citation-leader " "
85 "*String comprising first part of a citation."
86 :type 'string
87 :group 'supercite-cite)
88
89 (defcustom sc-citation-delimiter ">"
90 "*String comprising third part of a citation.
91 This string is used in both nested and non-nested citations."
92 :type 'string
93 :group 'supercite-cite)
94
95 (defcustom sc-citation-separator " "
96 "*String comprising fourth and last part of a citation."
97 :type 'string
98 :group 'supercite-cite)
99
100 (defcustom sc-citation-leader-regexp "[ \t]*"
101 "*Regexp describing citation leader for a cited line.
102 This should NOT have a leading `^' character."
103 :type 'regexp
104 :group 'supercite-cite)
105
106 ;; Nemacs and Mule users note: please see the texinfo manual for
107 ;; suggestions on setting these variables.
108 (defcustom sc-citation-root-regexp "[-._[:alnum:]]*"
109 "*Regexp describing variable root part of a citation for a cited line.
110 This should NOT have a leading `^' character. See also
111 `sc-citation-nonnested-root-regexp'."
112 :type 'regexp
113 :group 'supercite-cite)
114
115 (defcustom sc-citation-nonnested-root-regexp "[-._[:alnum:]]+"
116 "*Regexp describing the variable root part of a nested citation.
117 This should NOT have a leading `^' character. This variable is
118 related to `sc-citation-root-regexp' but whereas that variable
119 describes both nested and non-nested citation roots, this variable
120 describes only nested citation roots."
121 :type 'regexp
122 :group 'supercite-cite)
123
124 (defcustom sc-citation-delimiter-regexp "[>]+"
125 "*Regexp describing citation delimiter for a cited line.
126 This should NOT have a leading `^' character."
127 :type 'regexp
128 :group 'supercite-cite)
129
130 (defcustom sc-citation-separator-regexp "[ \t]*"
131 "*Regexp describing citation separator for a cited line.
132 This should NOT have a leading `^' character."
133 :type 'regexp
134 :group 'supercite-cite)
135
136 (defcustom sc-cite-blank-lines-p nil
137 "*If non-nil, put a citation on blank lines."
138 :type 'boolean
139 :group 'supercite-cite)
140
141 (defcustom sc-cite-frame-alist '()
142 "*Alist for frame selection during citing.
143 Each element of this list has the following form:
144 (INFOKEY ((REGEXP . FRAME)
145 (REGEXP . FRAME)
146 (...)))
147
148 Where INFOKEY is a key for `sc-mail-field', REGEXP is a regular
149 expression to match against the INFOKEY's value. FRAME is
150 a citation frame, or a symbol that represents the name of
151 a variable whose value is a citation frame."
152 :type '(repeat (list symbol (repeat (cons regexp
153 (choice (repeat (repeat sexp))
154 symbol)))))
155 :group 'supercite-frames)
156 (put 'sc-cite-frame-alist 'risky-local-variable t)
157
158 (defcustom sc-uncite-frame-alist '()
159 "*Alist for frame selection during unciting.
160 See the variable `sc-cite-frame-alist' for details."
161 :type '(repeat (list symbol (repeat (cons regexp
162 (choice (repeat (repeat sexp))
163 symbol)))))
164 :group 'supercite-frames)
165 (put 'sc-uncite-frame-alist 'risky-local-variable t)
166
167 (defcustom sc-recite-frame-alist '()
168 "*Alist for frame selection during reciting.
169 See the variable `sc-cite-frame-alist' for details."
170 :type '(repeat (list symbol (repeat (cons regexp
171 (choice (repeat (repeat sexp))
172 symbol)))))
173 :group 'supercite-frames)
174 (put 'sc-recite-frame-alist 'risky-local-variable t)
175
176 (defcustom sc-default-cite-frame
177 '(;; initialize fill state and temporary variables when entering
178 ;; frame. this makes things run much faster
179 (begin (progn
180 (sc-fill-if-different)
181 (setq sc-tmp-nested-regexp (sc-cite-regexp "")
182 sc-tmp-nonnested-regexp (sc-cite-regexp)
183 sc-tmp-dumb-regexp
184 (concat "\\("
185 (sc-cite-regexp "")
186 "\\)"
187 (sc-cite-regexp sc-citation-nonnested-root-regexp))
188 )))
189 ;; blank lines mean paragraph separators, so fill the last cited
190 ;; paragraph, unless sc-cite-blank-lines-p is non-nil, in which
191 ;; case we treat blank lines just like any other line.
192 ("^[ \t]*$" (if sc-cite-blank-lines-p
193 (sc-cite-line)
194 (sc-fill-if-different "")))
195 ;; do nothing if looking at a reference tag. make sure that the
196 ;; tag string isn't the empty string since this will match every
197 ;; line. it cannot be nil.
198 (sc-reference-tag-string (if (string= sc-reference-tag-string "")
199 (list 'continue)
200 nil))
201 ;; this regexp catches nested citations in which the author cited
202 ;; a non-nested citation with a dumb citer.
203 (sc-tmp-dumb-regexp (sc-cite-coerce-dumb-citer))
204 ;; if we are looking at a nested citation then add a citation level
205 (sc-tmp-nested-regexp (sc-add-citation-level))
206 ;; if we're looking at a non-nested citation, coerce it to our style
207 (sc-tmp-nonnested-regexp (sc-cite-coerce-cited-line))
208 ;; we must be looking at an uncited line. if we are in nested
209 ;; citations, just add a citation level
210 (sc-nested-citation-p (sc-add-citation-level))
211 ;; we're looking at an uncited line and we are in non-nested
212 ;; citations, so cite it with a non-nested citation
213 (t (sc-cite-line))
214 ;; be sure when we're done that we fill the last cited paragraph.
215 (end (sc-fill-if-different ""))
216 )
217 "*Default REGI frame for citing a region."
218 :type '(repeat (repeat sexp))
219 :group 'supercite-frames)
220 (put 'sc-default-cite-frame 'risky-local-variable t)
221
222 (defcustom sc-default-uncite-frame
223 '(;; do nothing on a blank line
224 ("^[ \t]*$" nil)
225 ;; if the line is cited, uncite it
226 ((sc-cite-regexp) (sc-uncite-line))
227 )
228 "*Default REGI frame for unciting a region."
229 :type '(repeat (repeat sexp))
230 :group 'supercite-frames)
231 (put 'sc-default-uncite-frame 'risky-local-variable t)
232
233 (defcustom sc-default-recite-frame
234 '(;; initialize fill state when entering frame
235 (begin (sc-fill-if-different))
236 ;; do nothing on a blank line
237 ("^[ \t]*$" nil)
238 ;; if we're looking at a cited line, recite it
239 ((sc-cite-regexp) (sc-recite-line (sc-cite-regexp)))
240 ;; otherwise, the line is uncited, so just cite it
241 (t (sc-cite-line))
242 ;; be sure when we're done that we fill the last cited paragraph.
243 (end (sc-fill-if-different ""))
244 )
245 "*Default REGI frame for reciting a region."
246 :type '(repeat (repeat sexp))
247 :group 'supercite-frames)
248 (put 'sc-default-recite-frame 'risky-local-variable t)
249
250 (defcustom sc-cite-region-limit t
251 "*This variable controls automatic citation of yanked text.
252 Valid values are:
253
254 non-nil -- cite the entire region, regardless of its size
255 nil -- do not cite the region at all
256 <integer> -- a number indicating the threshold for citation. When
257 the number of lines in the region is greater than this
258 value, a warning message will be printed and the region
259 will not be cited. Lines in region are counted with
260 `count-lines'.
261
262 The gathering of attribution information is not affected by the value
263 of this variable. The number of lines in the region is calculated
264 *after* all mail headers are removed. This variable is only consulted
265 during the initial citing via `sc-cite-original'."
266 :type '(choice (const :tag "do not cite" nil)
267 (integer :tag "citation threshold")
268 (other :tag "always cite" t))
269 :group 'supercite-cite)
270
271 (defcustom sc-confirm-always-p t
272 "*If non-nil, always confirm attribution string before citing text body."
273 :type 'boolean
274 :group 'supercite-attr)
275
276 (defcustom sc-default-attribution "Anon"
277 "*String used when author's attribution cannot be determined."
278 :type 'string
279 :group 'supercite-attr)
280 (defcustom sc-default-author-name "Anonymous"
281 "*String used when author's name cannot be determined."
282 :type 'string
283 :group 'supercite-attr)
284 (defcustom sc-downcase-p nil
285 "*Non-nil means downcase the attribution and citation strings."
286 :type 'boolean
287 :group 'supercite-attr
288 :group 'supercite-cite)
289 (defcustom sc-electric-circular-p t
290 "*If non-nil, treat electric references as circular."
291 :type 'boolean
292 :group 'supercite-attr)
293
294 (defcustom sc-electric-mode-hook nil
295 "*Hook for `sc-electric-mode' electric references mode."
296 :type 'hook
297 :group 'supercite-hooks)
298 (defcustom sc-electric-references-p nil
299 "*Use electric references if non-nil."
300 :type 'boolean
301 :group 'supercite)
302
303 (defcustom sc-fixup-whitespace-p nil
304 "*If non-nil, delete all leading white space before citing."
305 :type 'boolean
306 :group 'supercite)
307
308 (defcustom sc-load-hook nil
309 "*Hook which gets run once after Supercite loads."
310 :type 'hook
311 :group 'supercite-hooks)
312 (defcustom sc-pre-hook nil
313 "*Hook which gets run before each invocation of `sc-cite-original'."
314 :type 'hook
315 :group 'supercite-hooks)
316 (defcustom sc-post-hook nil
317 "*Hook which gets run after each invocation of `sc-cite-original'."
318 :type 'hook
319 :group 'supercite-hooks)
320
321 (defcustom sc-mail-warn-if-non-rfc822-p t
322 "*Warn if mail headers don't conform to RFC822."
323 :type 'boolean
324 :group 'supercite-attr)
325 (defcustom sc-mumble ""
326 "*Value returned by `sc-mail-field' if field isn't in mail headers."
327 :type 'string
328 :group 'supercite-attr)
329
330 (defcustom sc-name-filter-alist
331 '(("^\\(Mr\\|Mrs\\|Ms\\|Dr\\)[.]?$" . 0)
332 ("^\\(Jr\\|Sr\\)[.]?$" . last)
333 ("^ASTS$" . 0)
334 ("^[I]+$" . last))
335 "*Name list components which are filtered out as noise.
336 This variable contains an association list where each element is of
337 the form: (REGEXP . POSITION).
338
339 REGEXP is a regular expression which matches the name list component.
340 Match is performed using `string-match'. POSITION is the position in
341 the name list which can match the regular expression, starting at zero
342 for the first element. Use `last' to match the last element in the
343 list and `any' to match all elements."
344 :type '(repeat (cons regexp (choice (const last) (const any)
345 (integer :tag "position"))))
346 :group 'supercite-attr)
347
348 (defcustom sc-nested-citation-p nil
349 "*Controls whether to use nested or non-nested citation style.
350 Non-nil uses nested citations, nil uses non-nested citations."
351 :type 'boolean
352 :group 'supercite)
353
354 (defcustom sc-nuke-mail-headers 'all
355 "*Controls mail header nuking.
356 Used in conjunction with `sc-nuke-mail-header-list'. Valid values are:
357
358 `all' -- nuke all mail headers
359 `none' -- don't nuke any mail headers
360 `specified' -- nuke headers specified in `sc-nuke-mail-header-list'
361 `keep' -- keep headers specified in `sc-nuke-mail-header-list'"
362 :type '(choice (const all) (const none)
363 (const specified) (const keep))
364 :group 'supercite)
365
366 (defcustom sc-nuke-mail-header-list nil
367 "*List of mail header regexps to remove or keep in body of reply.
368 This list contains regular expressions describing the mail headers to
369 keep or nuke, depending on the value of `sc-nuke-mail-headers'."
370 :type '(repeat regexp)
371 :group 'supercite)
372
373 (defcustom sc-preferred-attribution-list
374 '("sc-lastchoice" "x-attribution" "firstname" "initials" "lastname")
375 "*Specifies what to use as the attribution string.
376 Supercite creates a list of possible attributions when it scans the
377 mail headers from the original message. Each attribution choice is
378 associated with a key in an attribution alist. Supercite tries to
379 pick a \"preferred\" attribution by matching the attribution alist
380 keys against the elements in `sc-preferred-attribution-list' in order.
381 The first non-empty string value found is used as the preferred
382 attribution.
383
384 Note that Supercite now honors the X-Attribution: mail field. If
385 present in the original message, the value of this field should always
386 be used to select the most preferred attribution since it reflects how
387 the original author would like to be distinguished. It should be
388 considered bad taste to put any attribution preference key before
389 \"x-attribution\" in this list, except perhaps for \"sc-lastchoice\"
390 \(see below).
391
392 Supercite remembers the last attribution used when reciting an already
393 cited paragraph. This attribution will always be saved with the
394 \"sc-lastchoice\" key, which can be used in this list. Note that the
395 last choice is always reset after every call of `sc-cite-original'.
396
397 Barring error conditions, the following preferences are always present
398 in the attribution alist:
399
400 \"emailname\" -- email terminus name
401 \"initials\" -- initials of author
402 \"firstname\" -- first name of author
403 \"lastname\" -- last name of author
404 \"middlename-1\" -- first middle name of author
405 \"middlename-2\" -- second middle name of author
406 ...
407
408 Middle name indexes can be any positive integer greater than 0,
409 although it is unlikely that many authors will supply more than one
410 middle name, if that many. The string of all middle names is
411 associated with the key \"middlenames\"."
412 :type '(repeat string)
413 :group 'supercite-attr)
414
415 (defcustom sc-attrib-selection-list nil
416 "*An alist for selecting preferred attribution based on mail headers.
417 Each element of this list has the following form:
418
419 (INFOKEY ((REGEXP . ATTRIBUTION)
420 (REGEXP . ATTRIBUTION)
421 (...)))
422
423 Where INFOKEY is a key for `sc-mail-field', REGEXP is a regular
424 expression to match against the INFOKEY's value. ATTRIBUTION can be a
425 string or a list. If its a string, then it is the attribution that is
426 selected by `sc-select-attribution'. If it is a list, it is `eval'd
427 and the return value must be a string, which is used as the selected
428 attribution. Note that the variable `sc-preferred-attribution-list'
429 must contain an element of the string \"sc-consult\" for this variable
430 to be consulted during attribution selection."
431 :type '(repeat (list string
432 (repeat (cons regexp
433 (choice (sexp :tag "List to eval")
434 string)))))
435 :group 'supercite-attr)
436 (put 'sc-attrib-selection-list 'risky-local-variable t)
437
438 (defcustom sc-attribs-preselect-hook nil
439 "*Hook to run before selecting an attribution."
440 :type 'hook
441 :group 'supercite-attr
442 :group 'supercite-hooks)
443 (defcustom sc-attribs-postselect-hook nil
444 "*Hook to run after selecting an attribution, but before confirmation."
445 :type 'hook
446 :group 'supercite-attr
447 :group 'supercite-hooks)
448
449 (defcustom sc-pre-cite-hook nil
450 "*Hook to run before citing a region of text."
451 :type 'hook
452 :group 'supercite-cite
453 :group 'supercite-hooks)
454 (defcustom sc-pre-uncite-hook nil
455 "*Hook to run before unciting a region of text."
456 :type 'hook
457 :group 'supercite-cite
458 :group 'supercite-hooks)
459 (defcustom sc-pre-recite-hook nil
460 "*Hook to run before reciting a region of text."
461 :type 'hook
462 :group 'supercite-cite
463 :group 'supercite-hooks)
464
465 (defcustom sc-preferred-header-style 4
466 "*Index into `sc-rewrite-header-list' specifying preferred header style.
467 Index zero accesses the first function in the list."
468 :type 'integer
469 :group 'supercite)
470
471 (defcustom sc-reference-tag-string ">>>>> "
472 "*String used at the beginning of built-in reference headers."
473 :type 'string
474 :group 'supercite)
475
476 (defcustom sc-rewrite-header-list
477 '((sc-no-header)
478 (sc-header-on-said)
479 (sc-header-inarticle-writes)
480 (sc-header-regarding-adds)
481 (sc-header-attributed-writes)
482 (sc-header-author-writes)
483 (sc-header-verbose)
484 (sc-no-blank-line-or-header)
485 )
486 "*List of reference header rewrite functions.
487 The variable `sc-preferred-header-style' controls which function in
488 this list is chosen for automatic reference header insertions.
489 Electric reference mode will cycle through this list of functions."
490 :type '(repeat sexp)
491 :group 'supercite)
492 (put 'sc-rewrite-header-list 'risky-local-variable t)
493
494 (defcustom sc-titlecue-regexp "\\s +-+\\s +"
495 "*Regular expression describing the separator between names and titles.
496 Set to nil to treat entire field as a name."
497 :type '(choice (const :tag "entire field as name" nil)
498 regexp)
499 :group 'supercite-attr)
500
501 (defcustom sc-use-only-preference-p nil
502 "*Controls what happens when the preferred attribution cannot be found.
503 If non-nil, then `sc-default-attribution' will be used. If nil, then
504 some secondary scheme will be employed to find a suitable attribution
505 string."
506 :type 'boolean
507 :group 'supercite-attr)
508
509 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
510 ;; end user configuration variables
511 \f
512 (defconst sc-version "3.1"
513 "Supercite version number.")
514 (defconst sc-help-address "bug-supercite@gnu.org"
515 "Address accepting submissions of bug reports.")
516
517 (defvar sc-mail-info nil
518 "Alist of mail header information gleaned from reply buffer.")
519 (defvar sc-attributions nil
520 "Alist of attributions for use when citing.")
521
522 (defvar sc-tmp-nested-regexp nil
523 "Temporary regexp describing nested citations.")
524 (defvar sc-tmp-nonnested-regexp nil
525 "Temporary regexp describing non-nested citations.")
526 (defvar sc-tmp-dumb-regexp nil
527 "Temp regexp describing non-nested citation cited with a nesting citer.")
528
529 (make-variable-buffer-local 'sc-mail-info)
530 (make-variable-buffer-local 'sc-attributions)
531
532 \f
533 ;; ======================================================================
534 ;; supercite keymaps
535
536 (defvar sc-mode-map-prefix "\C-c\C-p"
537 "*Key binding to install Supercite keymap.
538 If this is nil, Supercite keymap is not installed.")
539
540 (defvar sc-T-keymap
541 (let ((map (make-sparse-keymap)))
542 (define-key map "a" 'sc-S-preferred-attribution-list)
543 (define-key map "b" 'sc-T-mail-nuke-blank-lines)
544 (define-key map "c" 'sc-T-confirm-always)
545 (define-key map "d" 'sc-T-downcase)
546 (define-key map "e" 'sc-T-electric-references)
547 (define-key map "f" 'sc-T-auto-fill-region)
548 (define-key map "h" 'sc-T-describe)
549 (define-key map "l" 'sc-S-cite-region-limit)
550 (define-key map "n" 'sc-S-mail-nuke-mail-headers)
551 (define-key map "N" 'sc-S-mail-header-nuke-list)
552 (define-key map "o" 'sc-T-electric-circular)
553 (define-key map "p" 'sc-S-preferred-header-style)
554 (define-key map "s" 'sc-T-nested-citation)
555 (define-key map "u" 'sc-T-use-only-preferences)
556 (define-key map "w" 'sc-T-fixup-whitespace)
557 (define-key map "?" 'sc-T-describe)
558 map)
559 "Keymap for sub-keymap of setting and toggling functions.")
560
561 (defvar sc-mode-map
562 (let ((map (make-sparse-keymap)))
563 (define-key map "c" 'sc-cite-region)
564 (define-key map "f" 'sc-mail-field-query)
565 (define-key map "g" 'sc-mail-process-headers)
566 (define-key map "h" 'sc-describe)
567 (define-key map "i" 'sc-insert-citation)
568 (define-key map "o" 'sc-open-line)
569 (define-key map "r" 'sc-recite-region)
570 (define-key map "\C-p" 'sc-raw-mode-toggle)
571 (define-key map "u" 'sc-uncite-region)
572 (define-key map "v" 'sc-version)
573 (define-key map "w" 'sc-insert-reference)
574 (define-key map "\C-t" sc-T-keymap)
575 (define-key map "\C-b" 'sc-submit-bug-report)
576 (define-key map "?" 'sc-describe)
577 map)
578 "Keymap for Supercite quasi-mode.")
579
580 (defvar sc-electric-mode-map
581 (let ((map (make-sparse-keymap)))
582 (define-key map "p" 'sc-eref-prev)
583 (define-key map "n" 'sc-eref-next)
584 (define-key map "s" 'sc-eref-setn)
585 (define-key map "j" 'sc-eref-jump)
586 (define-key map "x" 'sc-eref-abort)
587 (define-key map "q" 'sc-eref-abort)
588 (define-key map "\r" 'sc-eref-exit)
589 (define-key map "\n" 'sc-eref-exit)
590 (define-key map "g" 'sc-eref-goto)
591 (define-key map "?" 'describe-mode)
592 (define-key map "\C-h" 'describe-mode)
593 (define-key map [f1] 'describe-mode)
594 (define-key map [help] 'describe-mode)
595 map)
596 "Keymap for `sc-electric-mode' electric references mode.")
597
598
599 (defvar sc-minibuffer-local-completion-map
600 (let ((map (copy-keymap minibuffer-local-completion-map)))
601 (define-key map "\C-t" 'sc-toggle-fn)
602 (define-key map " " 'self-insert-command)
603 map)
604 "Keymap for minibuffer confirmation of attribution strings.")
605
606 (defvar sc-minibuffer-local-map
607 (let ((map (copy-keymap minibuffer-local-map)))
608 (define-key map "\C-t" 'sc-toggle-fn)
609 map)
610 "Keymap for minibuffer confirmation of attribution strings.")
611
612 \f
613 ;; ======================================================================
614 ;; utility functions
615
616 (defun sc-ask (alist)
617 "Ask a question in the minibuffer requiring a single character answer.
618 This function is kind of an extension of `y-or-n-p' where a single
619 letter is used to answer a question. Question is formed from ALIST
620 which has members of the form: (WORD . LETTER). WORD is the long
621 word form, while LETTER is the letter for selecting that answer. The
622 selected letter is returned, or nil if the question was not answered.
623 Note that WORD is a string and LETTER is a character. All LETTERs in
624 the list should be unique."
625 (let* ((prompt (concat
626 (mapconcat (function (lambda (elt) (car elt))) alist ", ")
627 "? ("
628 (mapconcat
629 (function
630 (lambda (elt) (char-to-string (cdr elt)))) alist "/")
631 ") "))
632 (p prompt)
633 (event
634 (if (fboundp 'allocate-event)
635 (allocate-event)
636 nil)))
637 (while (stringp p)
638 (if (let ((cursor-in-echo-area t)
639 (inhibit-quit t))
640 (message "%s" p)
641 (setq event (read-event))
642 (prog1 quit-flag (setq quit-flag nil)))
643 (progn
644 (message "%s%s" p (single-key-description event))
645 (and (fboundp 'deallocate-event)
646 (deallocate-event event))
647 (setq quit-flag nil)
648 (signal 'quit '())))
649 (let ((char
650 (if (featurep 'xemacs)
651 (let* ((key (and (key-press-event-p event) (event-key event)))
652 (char (and key (event-to-character event))))
653 char)
654 event))
655 elt)
656 (if char (setq char (downcase char)))
657 (cond
658 ((setq elt (rassq char alist))
659 (message "%s%s" p (car elt))
660 (setq p (cdr elt)))
661 ((and (fboundp 'button-release-event-p)
662 (button-release-event-p event)) ; ignore them
663 nil)
664 (t
665 (message "%s%s" p (single-key-description event))
666 (if (featurep 'xemacs)
667 (ding nil 'y-or-n-p)
668 (ding))
669 (discard-input)
670 (if (eq p prompt)
671 (setq p (concat "Try again. " prompt)))))))
672 (and (fboundp 'deallocate-event)
673 (deallocate-event event))
674 p))
675
676 (defun sc-scan-info-alist (alist)
677 "Find a match in the info alist that matches a regexp in ALIST."
678 (let ((sc-mumble "")
679 rtnvalue)
680 (while alist
681 (let* ((elem (car alist))
682 (infokey (car elem))
683 (infoval (sc-mail-field infokey))
684 (mlist (car (cdr elem))))
685 (while mlist
686 (let* ((ml-elem (car mlist))
687 (regexp (car ml-elem))
688 (thing (cdr ml-elem)))
689 (if (string-match regexp infoval)
690 ;; we found a match, time to return
691 (setq rtnvalue thing
692 mlist nil
693 alist nil)
694 ;; else we didn't find a match
695 (setq mlist (cdr mlist))
696 ))) ;end of mlist loop
697 (setq alist (cdr alist))
698 )) ;end of alist loop
699 rtnvalue))
700
701 \f
702 ;; ======================================================================
703 ;; extract mail field information from headers in reply buffer
704
705 ;; holder variables for bc happiness
706 (defvar sc-mail-headers-start nil
707 "Start of header fields.")
708 (defvar sc-mail-headers-end nil
709 "End of header fields.")
710 (defvar sc-mail-field-history nil
711 "For minibuffer completion on mail field queries.")
712 (defvar sc-mail-field-modification-history nil
713 "For minibuffer completion on mail field modifications.")
714 (defvar sc-mail-glom-frame
715 '((begin (setq sc-mail-headers-start (point)))
716 ("^x-attribution:[ \t]+.*$" (sc-mail-fetch-field t) nil t)
717 ("^\\S +:.*$" (sc-mail-fetch-field) nil t)
718 ("^$" (list 'abort '(step . 0)))
719 ("^[ \t]+" (sc-mail-append-field))
720 (sc-mail-warn-if-non-rfc822-p (sc-mail-error-in-mail-field))
721 (end (setq sc-mail-headers-end (point))))
722 "Regi frame for glomming mail header information.")
723
724 (defvar curline) ; dynamic bondage
725
726 ;; regi functions
727 (defun sc-mail-fetch-field (&optional attribs-p)
728 "Insert a key and value into `sc-mail-info' alist.
729 If optional ATTRIBS-P is non-nil, the key/value pair is placed in
730 `sc-attributions' too."
731 (if (string-match "^\\(\\S *\\)\\s *:\\s +\\(.*\\)$" curline)
732 (let* ((key (downcase (match-string-no-properties 1 curline)))
733 (val (match-string-no-properties 2 curline))
734 (keyval (cons key val)))
735 (push keyval sc-mail-info)
736 (if attribs-p
737 (push keyval sc-attributions))))
738 nil)
739
740 (defun sc-mail-append-field ()
741 "Append a continuation line onto the last fetched mail field's info."
742 (let ((keyval (car sc-mail-info)))
743 (if (and keyval (string-match "^\\s *\\(.*\\)$" curline))
744 (setcdr keyval (concat (cdr keyval) " "
745 (match-string-no-properties 1 curline)))))
746 nil)
747
748 (defun sc-mail-error-in-mail-field ()
749 "Issue warning that mail headers don't conform to RFC 822."
750 (let* ((len (min (length curline) 10))
751 (ellipsis (if (< len (length curline)) "..." ""))
752 (msg "Mail header \"%s%s\" doesn't conform to RFC 822. skipping..."))
753 (message msg (substring curline 0 len) ellipsis))
754 (beep)
755 (sit-for 2)
756 nil)
757
758 ;; mail header nuking
759 (defvar sc-mail-last-header-nuked-p nil
760 "True if the last header was nuked.")
761
762 (defun sc-mail-nuke-line ()
763 "Nuke the current mail header line."
764 (delete-region (line-beginning-position) (line-beginning-position 2))
765 '((step . -1)))
766
767 (defun sc-mail-nuke-header-line ()
768 "Delete current-line and set up for possible continuation."
769 (setq sc-mail-last-header-nuked-p t)
770 (sc-mail-nuke-line))
771
772 (defun sc-mail-nuke-continuation-line ()
773 "Delete a continuation line if the last header line was deleted."
774 (if sc-mail-last-header-nuked-p
775 (sc-mail-nuke-line)))
776
777 (defun sc-mail-cleanup-blank-lines ()
778 "Leave some blank lines after original mail headers are nuked.
779 The number of lines left is specified by `sc-blank-lines-after-headers'."
780 (if sc-blank-lines-after-headers
781 (save-restriction
782 (widen)
783 (skip-chars-backward " \t\n")
784 (forward-line 1)
785 (delete-blank-lines)
786 (beginning-of-line)
787 (if (looking-at "[ \t]*$")
788 (delete-region (line-beginning-position)
789 (line-beginning-position 2)))
790 (insert-char ?\n sc-blank-lines-after-headers)))
791 nil)
792
793 (defun sc-mail-build-nuke-frame ()
794 "Build the regiframe for nuking mail headers."
795 (let (every-func entry-func nonentry-func)
796 (cond
797 ((eq sc-nuke-mail-headers 'all)
798 (setq every-func '(progn (forward-line -1) (sc-mail-nuke-line))))
799 ((eq sc-nuke-mail-headers 'specified)
800 (setq entry-func '(sc-mail-nuke-header-line)
801 nonentry-func '(setq sc-mail-last-header-nuked-p nil)))
802 ((eq sc-nuke-mail-headers 'keep)
803 (setq entry-func '(setq sc-mail-last-header-nuked-p nil)
804 nonentry-func '(sc-mail-nuke-header-line)))
805 ;; we never get far enough to interpret a frame if s-n-m-h == 'none
806 ((eq sc-nuke-mail-headers 'none))
807 (t (error "Invalid value for sc-nuke-mail-headers: %s"
808 sc-nuke-mail-headers))
809 ) ; end-cond
810 (append
811 (and entry-func
812 (regi-mapcar sc-nuke-mail-header-list entry-func nil t))
813 (and nonentry-func (list (list "^\\S +:.*$" nonentry-func)))
814 (and (not every-func)
815 '(("^[ \t]+" (sc-mail-nuke-continuation-line))))
816 '((begin (setq sc-mail-last-header-zapped-p nil)))
817 '((end (sc-mail-cleanup-blank-lines)))
818 (and every-func (list (list 'every every-func)))
819 )))
820
821 ;; mail processing and zapping. this is the top level entry defun to
822 ;; all header processing.
823 (defun sc-mail-process-headers (start end)
824 "Process original mail message's mail headers.
825 After processing, mail headers may be nuked. Header information is
826 stored in `sc-mail-info', and any old information is lost unless an
827 error occurs."
828 (interactive "r")
829 (let ((info (copy-alist sc-mail-info))
830 (attribs (copy-alist sc-attributions)))
831 (setq sc-mail-info nil
832 sc-attributions nil)
833 (regi-interpret sc-mail-glom-frame start end)
834 (if (null sc-mail-info)
835 (progn
836 (message "No mail headers found! Restoring old information.")
837 (setq sc-mail-info info
838 sc-attributions attribs))
839 (regi-interpret (sc-mail-build-nuke-frame)
840 sc-mail-headers-start sc-mail-headers-end)
841 )))
842
843 \f
844 ;; let the user change mail field information
845 (defun sc-mail-field (field)
846 "Return the mail header field value associated with FIELD.
847 If there was no mail header with FIELD as its key, return the value of
848 `sc-mumble'. FIELD is case insensitive."
849 (or (cdr (assoc-string field sc-mail-info 'case-fold)) sc-mumble))
850
851 (defun sc-mail-field-query (arg)
852 "View the value of a mail field.
853 With `\\[universal-argument]', prompts for action on mail field.
854 Action can be one of: View, Modify, Add, or Delete."
855 (interactive "P")
856 (let* ((alist '(("view" . ?v) ("modify" . ?m) ("add" . ?a) ("delete" . ?d)))
857 (action (if (not arg) ?v (sc-ask alist)))
858 key)
859 (if (not action)
860 ()
861 (setq key (completing-read
862 (concat (car (rassq action alist))
863 " information key: ")
864 sc-mail-info nil
865 (if (eq action ?a) nil 'noexit)
866 nil 'sc-mail-field-history))
867 (cond
868 ((eq action ?v)
869 (message "%s: %s" key (cdr (assoc key sc-mail-info))))
870 ((eq action ?d)
871 (setq sc-mail-info (delq (assoc key sc-mail-info) sc-mail-info)))
872 ((eq action ?m)
873 (let ((keyval (assoc key sc-mail-info)))
874 ;; first put initial value onto list if not already there
875 (if (not (member (cdr keyval)
876 sc-mail-field-modification-history))
877 (setq sc-mail-field-modification-history
878 (cons (cdr keyval) sc-mail-field-modification-history)))
879 (setcdr keyval (read-string
880 (concat key ": ") (cdr keyval)
881 'sc-mail-field-modification-history))))
882 ((eq action ?a)
883 (push (cons key (read-string (concat key ": "))) sc-mail-info))
884 ))))
885
886 \f
887 ;; ======================================================================
888 ;; attributions
889
890 (defvar sc-attribution-confirmation-history nil
891 "History for confirmation of attribution strings.")
892 (defvar sc-citation-confirmation-history nil
893 "History for confirmation of attribution prefixes.")
894
895 (defun sc-attribs-%@-addresses (from &optional delim)
896 "Extract the author's email terminus from email address FROM.
897 Match addresses of the style ``name%[stuff].'' when called with DELIM
898 of \"%\" and addresses of the style ``[stuff]name@[stuff]'' when
899 called with DELIM \"@\". If DELIM is nil or not provided, matches
900 addresses of the style ``name''."
901 (and (string-match (concat "[-[:alnum:]_.]+" delim) from 0)
902 (substring from
903 (match-beginning 0)
904 (- (match-end 0) (if (null delim) 0 1)))))
905
906 (defun sc-attribs-!-addresses (from)
907 "Extract the author's email terminus from email address FROM.
908 Match addresses of the style ``[stuff]![stuff]...!name[stuff].''"
909 (let ((eos (length from))
910 (mstart (string-match "![-[:alnum:]_.]+\\([^-![:alnum:]_.]\\|$\\)"
911 from 0))
912 (mend (match-end 0)))
913 (and mstart
914 (substring from (1+ mstart) (- mend (if (= mend eos) 0 1)))
915 )))
916
917 (defun sc-attribs-<>-addresses (from)
918 "Extract the author's email terminus from email address FROM.
919 Match addresses of the style ``<name[stuff]>.''"
920 (and (string-match "<\\(.*\\)>" from)
921 (match-string 1 from)))
922
923 (defun sc-get-address (from author)
924 "Get the full email address path from FROM.
925 AUTHOR is the author's name (which is removed from the address)."
926 (let ((eos (length from)))
927 (if (string-match (concat "\\`\"?" (regexp-quote author)
928 "\"?\\s +") from 0)
929 (let ((address (substring from (match-end 0) eos)))
930 (if (and (= (aref address 0) ?<)
931 (= (aref address (1- (length address))) ?>))
932 (substring address 1 (1- (length address)))
933 address))
934 (if (string-match "[-[:alnum:]!@%._]+" from 0)
935 (match-string 0 from)
936 "")
937 )))
938
939 (defun sc-attribs-emailname (from)
940 "Get the email terminus name from FROM."
941 (or
942 (sc-attribs-%@-addresses from "%")
943 (sc-attribs-%@-addresses from "@")
944 (sc-attribs-!-addresses from)
945 (sc-attribs-<>-addresses from)
946 (sc-attribs-%@-addresses from)
947 (substring from 0 10)))
948
949 (defun sc-name-substring (string start end extend)
950 "Extract the specified substring of STRING from START to END.
951 EXTEND is the number of characters on each side to extend the
952 substring."
953 (and start
954 (let ((sos (+ start extend))
955 (eos (- end extend)))
956 (substring string sos
957 (or (string-match sc-titlecue-regexp string sos) eos)
958 ))))
959
960 (defun sc-attribs-extract-namestring (from)
961 "Extract the name string from FROM.
962 This should be the author's full name minus an optional title."
963 ;; FIXME: we probably should use mail-extract-address-components.
964 (let ((namestring
965 (or
966 ;; If there is a <...> in the name,
967 ;; treat everything before that as the full name.
968 ;; Even if it contains parens, use the whole thing.
969 ;; On the other hand, we do look for quotes in the usual way.
970 (and (string-match " *<.*>" from 0)
971 (let ((before-angles
972 (sc-name-substring from 0 (match-beginning 0) 0)))
973 (if (string-match "\".*\"" before-angles 0)
974 (sc-name-substring
975 before-angles (match-beginning 0) (match-end 0) 1)
976 before-angles)))
977 (sc-name-substring
978 from (string-match "(.*)" from 0) (match-end 0) 1)
979 (sc-name-substring
980 from (string-match "\".*\"" from 0) (match-end 0) 1)
981 (sc-name-substring
982 from (string-match "\\([-.[:alnum:]_]+\\s +\\)+<" from 0)
983 (match-end 1) 0)
984 (sc-attribs-emailname from))))
985 ;; strip off any leading or trailing whitespace
986 (if namestring
987 (let ((bos 0)
988 (eos (1- (length namestring))))
989 (while (and (<= bos eos)
990 (memq (aref namestring bos) '(32 ?\t)))
991 (setq bos (1+ bos)))
992 (while (and (> eos bos)
993 (memq (aref namestring eos) '(32 ?\t)))
994 (setq eos (1- eos)))
995 (substring namestring bos (1+ eos))))))
996
997 (defun sc-attribs-chop-namestring (namestring)
998 "Convert NAMESTRING to a list of names.
999 example: (sc-attribs-chop-namestring \"John Xavier Doe\")
1000 => (\"John\" \"Xavier\" \"Doe\")"
1001 (if (string-match "\\([ \t]*\\)\\([^ \t._]+\\)\\([ \t]*\\)" namestring)
1002 (cons (match-string 2 namestring)
1003 (sc-attribs-chop-namestring (substring namestring (match-end 3)))
1004 )))
1005
1006 (defun sc-attribs-strip-initials (namelist)
1007 "Extract the author's initials from the NAMELIST."
1008 (mapconcat
1009 (function
1010 (lambda (name)
1011 (if (< 0 (length name))
1012 (substring name 0 1))))
1013 namelist ""))
1014
1015 (defun sc-guess-attribution (&optional string)
1016 "Guess attribution string on current line.
1017 If attribution cannot be guessed, nil is returned. Optional STRING if
1018 supplied, is used instead of the line point is on in the current buffer."
1019 (let ((start 0)
1020 (string (or string (buffer-substring (line-beginning-position)
1021 (line-end-position))))
1022 attribution)
1023 (and
1024 (= start (or (string-match sc-citation-leader-regexp string start) -1))
1025 (setq start (match-end 0))
1026 (= start (or (string-match sc-citation-root-regexp string start) 1))
1027 (setq attribution (match-string 0 string)
1028 start (match-end 0))
1029 (= start (or (string-match sc-citation-delimiter-regexp string start) -1))
1030 (setq start (match-end 0))
1031 (= start (or (string-match sc-citation-separator-regexp string start) -1))
1032 attribution)))
1033
1034 (defun sc-attribs-filter-namelist (namelist)
1035 "Filter out noise in NAMELIST according to `sc-name-filter-alist'."
1036 (let ((elements (length namelist))
1037 (position -1)
1038 keepers filtered-list)
1039 (mapcar
1040 (function
1041 (lambda (name)
1042 (setq position (1+ position))
1043 (let ((keep-p t))
1044 (mapcar
1045 (function
1046 (lambda (filter)
1047 (let ((regexp (car filter))
1048 (pos (cdr filter)))
1049 (if (and (string-match regexp name)
1050 (or (and (numberp pos)
1051 (= pos position))
1052 (and (eq pos 'last)
1053 (= position (1- elements)))
1054 (eq pos 'any)))
1055 (setq keep-p nil))
1056 )))
1057 sc-name-filter-alist)
1058 (if keep-p
1059 (setq keepers (cons position keepers)))
1060 )))
1061 namelist)
1062 (mapcar
1063 (function
1064 (lambda (position)
1065 (setq filtered-list (cons (nth position namelist) filtered-list))
1066 ))
1067 keepers)
1068 filtered-list))
1069
1070 (defun sc-attribs-chop-address (from)
1071 "Extract attribution information from FROM.
1072 This populates the `sc-attributions' with the list of possible attributions."
1073 (if (and (stringp from)
1074 (< 0 (length from)))
1075 (let* ((sc-mumble "")
1076 (namestring (sc-attribs-extract-namestring from))
1077 (namelist (sc-attribs-filter-namelist
1078 (sc-attribs-chop-namestring namestring)))
1079 (revnames (reverse (cdr namelist)))
1080 (firstname (car namelist))
1081 (midnames (reverse (cdr revnames)))
1082 (lastname (car revnames))
1083 (initials (sc-attribs-strip-initials namelist))
1084 (emailname (sc-attribs-emailname from))
1085 (n 1)
1086 author middlenames)
1087
1088 ;; put basic information
1089 (setq
1090 ;; put middle names and build sc-author entry
1091 middlenames (mapconcat
1092 (function
1093 (lambda (midname)
1094 (let ((key-attribs (format "middlename-%d" n))
1095 (key-mail (format "sc-middlename-%d" n)))
1096 (push (cons key-attribs midname) sc-attributions)
1097 (push (cons key-mail midname) sc-mail-info)
1098 (setq n (1+ n))
1099 midname)))
1100 midnames " ")
1101
1102 author (concat firstname " " middlenames (and midnames " ") lastname)
1103
1104 sc-attributions (append
1105 (list
1106 (cons "firstname" firstname)
1107 (cons "lastname" lastname)
1108 (cons "emailname" emailname)
1109 (cons "initials" initials))
1110 sc-attributions)
1111 sc-mail-info (append
1112 (list
1113 (cons "sc-firstname" firstname)
1114 (cons "sc-middlenames" middlenames)
1115 (cons "sc-lastname" lastname)
1116 (cons "sc-emailname" emailname)
1117 (cons "sc-initials" initials)
1118 (cons "sc-author" author)
1119 (cons "sc-from-address" (sc-get-address
1120 (sc-mail-field "from")
1121 namestring))
1122 (cons "sc-reply-address" (sc-get-address
1123 (sc-mail-field "reply-to")
1124 namestring))
1125 (cons "sc-sender-address" (sc-get-address
1126 (sc-mail-field "sender")
1127 namestring))
1128 )
1129 sc-mail-info)
1130 ))
1131 ;; from string is empty
1132 (push (cons "sc-author" sc-default-author-name) sc-mail-info)))
1133
1134 (defvar sc-attrib-or-cite nil
1135 "Used to toggle between attribution input or citation input.")
1136
1137 (defun sc-toggle-fn ()
1138 "Toggle between attribution selection and citation selection.
1139 Only used during confirmation."
1140 (interactive)
1141 (setq sc-attrib-or-cite (not sc-attrib-or-cite))
1142 (throw 'sc-reconfirm t))
1143
1144 (defun sc-select-attribution ()
1145 "Select an attribution from `sc-attributions'.
1146
1147 Variables involved in selection process include:
1148 `sc-preferred-attribution-list'
1149 `sc-use-only-preference-p'
1150 `sc-confirm-always-p'
1151 `sc-default-attribution'
1152 `sc-attrib-selection-list'.
1153
1154 Runs the hook `sc-attribs-preselect-hook' before selecting an
1155 attribution and the hook `sc-attribs-postselect-hook' after making the
1156 selection but before querying is performed. During
1157 `sc-attribs-postselect-hook' the variable `citation' is bound to the
1158 auto-selected citation string and the variable `attribution' is bound
1159 to the auto-selected attribution string."
1160 (run-hooks 'sc-attribs-preselect-hook)
1161 (let ((query-p sc-confirm-always-p)
1162 attribution citation
1163 (attriblist sc-preferred-attribution-list))
1164
1165 ;; first cruise through sc-preferred-attribution-list looking for
1166 ;; a match in either sc-attributions or sc-mail-info. if the
1167 ;; element is "sc-consult", then we have to do the alist
1168 ;; consultation phase
1169 (while attriblist
1170 (let* ((preferred (car attriblist)))
1171 (cond
1172 ((string= preferred "sc-consult")
1173 ;; we've been told to consult the attribution vs. mail
1174 ;; header key alist. we do this until we find a match in
1175 ;; the sc-attrib-selection-list. if we do not find a match,
1176 ;; we continue scanning attriblist
1177 (let ((attrib (sc-scan-info-alist sc-attrib-selection-list)))
1178 (cond
1179 ((not attrib)
1180 (setq attriblist (cdr attriblist)))
1181 ((stringp attrib)
1182 (setq attribution attrib
1183 attriblist nil))
1184 ((listp attrib)
1185 (setq attribution (eval attrib)
1186 attriblist nil))
1187 (t (error "%s did not evaluate to a string or list!"
1188 "sc-attrib-selection-list"))
1189 )))
1190 ((setq attribution (cdr (assoc preferred sc-attributions)))
1191 (setq attriblist nil))
1192 (t
1193 (setq attriblist (cdr attriblist)))
1194 )))
1195
1196 ;; if preference was not found, we may use a secondary method to
1197 ;; find a valid attribution
1198 (if (and (not attribution)
1199 (not sc-use-only-preference-p))
1200 ;; secondary method tries to find a preference in this order
1201 ;; 1. sc-lastchoice
1202 ;; 2. x-attribution
1203 ;; 3. firstname
1204 ;; 4. lastname
1205 ;; 5. initials
1206 ;; 6. first non-empty attribution in alist
1207 (setq attribution
1208 (or (cdr (assoc "sc-lastchoice" sc-attributions))
1209 (cdr (assoc "x-attribution" sc-attributions))
1210 (cdr (assoc "firstname" sc-attributions))
1211 (cdr (assoc "lastname" sc-attributions))
1212 (cdr (assoc "initials" sc-attributions))
1213 (cdr (car sc-attributions)))))
1214
1215 ;; still couldn't find an attribution. we're now limited to using
1216 ;; the default attribution, but we'll force a query when this happens
1217 (if (not attribution)
1218 (setq attribution sc-default-attribution
1219 query-p t))
1220
1221 ;; create the attribution prefix
1222 (setq citation (sc-make-citation attribution))
1223
1224 ;; run the post selection hook before querying the user
1225 (run-hooks 'sc-attribs-postselect-hook)
1226
1227 ;; query for confirmation
1228 (if query-p
1229 (let* ((query-alist (mapcar (function (lambda (entry)
1230 (list (cdr entry))))
1231 sc-attributions))
1232 (minibuffer-local-completion-map
1233 sc-minibuffer-local-completion-map)
1234 (minibuffer-local-map sc-minibuffer-local-map)
1235 (initial attribution)
1236 (completer-disable t) ; in case completer.el is used
1237 choice)
1238 (setq sc-attrib-or-cite nil) ; nil==attribution, t==citation
1239 (while
1240 (catch 'sc-reconfirm
1241 (progn
1242 (setq choice
1243 (if sc-attrib-or-cite
1244 (read-string
1245 "Enter citation prefix: "
1246 citation
1247 'sc-citation-confirmation-history)
1248 (completing-read
1249 "Complete attribution name: "
1250 query-alist nil nil
1251 (cons initial 0)
1252 'sc-attribution-confirmation-history)
1253 ))
1254 nil)))
1255 (if sc-attrib-or-cite
1256 ;; since the citation was chosen, we have to guess at
1257 ;; the attribution
1258 (setq citation choice
1259 attribution (or (sc-guess-attribution citation)
1260 citation))
1261
1262 (setq citation (sc-make-citation choice)
1263 attribution choice))
1264 ))
1265
1266 ;; its possible that the user wants to downcase the citation and
1267 ;; attribution
1268 (if sc-downcase-p
1269 (setq citation (downcase citation)
1270 attribution (downcase attribution)))
1271
1272 ;; set up mail info alist
1273 (let* ((ckey "sc-citation")
1274 (akey "sc-attribution")
1275 (ckeyval (assoc ckey sc-mail-info))
1276 (akeyval (assoc akey sc-mail-info)))
1277 (if ckeyval
1278 (setcdr ckeyval citation)
1279 (push (cons ckey citation) sc-mail-info))
1280 (if akeyval
1281 (setcdr akeyval attribution)
1282 (push (cons akey attribution) sc-mail-info)))
1283
1284 ;; set the sc-lastchoice attribution
1285 (let* ((lkey "sc-lastchoice")
1286 (lastchoice (assoc lkey sc-attributions)))
1287 (if lastchoice
1288 (setcdr lastchoice attribution)
1289 (push (cons lkey attribution) sc-attributions)))
1290 ))
1291
1292 \f
1293 ;; ======================================================================
1294 ;; filladapt hooks for supercite 3.1. you shouldn't need anything
1295 ;; extra to make gin-mode understand supercited lines. Even this
1296 ;; stuff might not be entirely necessary...
1297
1298 (defun sc-cite-regexp (&optional root-regexp)
1299 "Return a regexp describing a Supercited line.
1300 The regexp is the concatenation of `sc-citation-leader-regexp',
1301 `sc-citation-root-regexp', `sc-citation-delimiter-regexp', and
1302 `sc-citation-separator-regexp'. If optional ROOT-REGEXP is supplied,
1303 use it instead of `sc-citation-root-regexp'."
1304 (concat sc-citation-leader-regexp
1305 (or root-regexp sc-citation-root-regexp)
1306 sc-citation-delimiter-regexp
1307 sc-citation-separator-regexp))
1308
1309 (defun sc-make-citation (attribution)
1310 "Make a non-nested citation from ATTRIBUTION."
1311 (concat sc-citation-leader
1312 attribution
1313 sc-citation-delimiter
1314 sc-citation-separator))
1315
1316 (defun sc-setup-filladapt ()
1317 "Setup `filladapt-prefix-table' to handle Supercited paragraphs."
1318 (let* ((fa-sc-elt 'filladapt-supercite-included-text)
1319 (elt (rassq fa-sc-elt filladapt-prefix-table)))
1320 (if elt (setcar elt (sc-cite-regexp))
1321 (message "Filladapt doesn't seem to know about Supercite.")
1322 (beep))))
1323
1324 \f
1325 ;; ======================================================================
1326 ;; citing and unciting regions of text
1327
1328 (defvar sc-fill-begin 1
1329 "Buffer position to begin filling.")
1330 (defvar sc-fill-line-prefix ""
1331 "Fill prefix of previous line")
1332
1333 ;; filling
1334 (defun sc-fill-if-different (&optional prefix)
1335 "Fill the region bounded by `sc-fill-begin' and point.
1336 Only fill if optional PREFIX is different than `sc-fill-line-prefix'.
1337 If `sc-auto-fill-region-p' is nil, do not fill region. If PREFIX is
1338 not supplied, initialize fill variables. This is useful for a regi
1339 `begin' frame-entry."
1340 (if (not prefix)
1341 (setq sc-fill-line-prefix ""
1342 sc-fill-begin (line-beginning-position))
1343 (if (and sc-auto-fill-region-p
1344 (not (string= prefix sc-fill-line-prefix)))
1345 (let ((fill-prefix sc-fill-line-prefix))
1346 (if (not (string= fill-prefix ""))
1347 (fill-region sc-fill-begin (line-beginning-position)))
1348 (setq sc-fill-line-prefix prefix
1349 sc-fill-begin (line-beginning-position))))
1350 )
1351 nil)
1352
1353 (defun sc-cite-coerce-cited-line ()
1354 "Coerce a Supercited line to look like our style."
1355 (let* ((attribution (sc-guess-attribution))
1356 (regexp (sc-cite-regexp attribution))
1357 (prefix (sc-make-citation attribution)))
1358 (if (and attribution
1359 (looking-at regexp))
1360 (progn
1361 (delete-region
1362 (match-beginning 0)
1363 (save-excursion
1364 (goto-char (match-end 0))
1365 (if (bolp) (forward-char -1))
1366 (point)))
1367 (insert prefix)
1368 (sc-fill-if-different prefix)))
1369 nil))
1370
1371 (defun sc-cite-coerce-dumb-citer ()
1372 "Coerce a non-nested citation that's been cited with a dumb nesting citer."
1373 (delete-region (match-beginning 1) (match-end 1))
1374 (beginning-of-line)
1375 (sc-cite-coerce-cited-line))
1376
1377 (defun sc-guess-nesting (&optional string)
1378 "Guess the citation nesting on the current line.
1379 If nesting cannot be guessed, nil is returned. Optional STRING if
1380 supplied, is used instead of the line point is on in the current
1381 buffer."
1382 (let ((start 0)
1383 (string (or string (buffer-substring (line-beginning-position)
1384 (line-end-position))))
1385 nesting)
1386 (and
1387 (= start (or (string-match sc-citation-leader-regexp string start) -1))
1388 (setq start (match-end 0))
1389 (= start (or (string-match sc-citation-delimiter-regexp string start) -1))
1390 (setq nesting (match-string 0 string)
1391 start (match-end 0))
1392 (= start (or (string-match sc-citation-separator-regexp string start) -1))
1393 nesting)))
1394
1395 (defun sc-add-citation-level ()
1396 "Add a citation level for nested citation style w/ coercion."
1397 (let* ((nesting (sc-guess-nesting))
1398 (citation (make-string (1+ (length nesting))
1399 (string-to-char sc-citation-delimiter)))
1400 (prefix (concat sc-citation-leader citation sc-citation-separator)))
1401 (if (looking-at (sc-cite-regexp ""))
1402 (delete-region (match-beginning 0) (match-end 0)))
1403 (insert prefix)
1404 (sc-fill-if-different prefix)))
1405
1406 (defun sc-cite-line (&optional citation)
1407 "Cite a single line of uncited text.
1408 Optional CITATION overrides any citation automatically selected."
1409 (if sc-fixup-whitespace-p
1410 (fixup-whitespace))
1411 (let ((prefix (or citation
1412 (cdr (assoc "sc-citation" sc-mail-info))
1413 sc-default-attribution)))
1414 (insert prefix)
1415 (sc-fill-if-different prefix))
1416 nil)
1417
1418 (defun sc-uncite-line ()
1419 "Remove citation from current line."
1420 (let ((cited (looking-at (sc-cite-regexp))))
1421 (if cited
1422 (delete-region (match-beginning 0) (match-end 0))))
1423 nil)
1424
1425 (defun sc-recite-line (regexp)
1426 "Remove citation matching REGEXP from current line and recite line."
1427 (let ((cited (looking-at (concat "^" regexp)))
1428 (prefix (cdr (assoc "sc-citation" sc-mail-info))))
1429 (if cited
1430 (delete-region (match-beginning 0) (match-end 0)))
1431 (insert (or prefix sc-default-attribution))
1432 (sc-fill-if-different prefix))
1433 nil)
1434
1435 ;; interactive functions
1436 (defun sc-cite-region (start end &optional confirm-p interactive)
1437 "Cite a region delineated by START and END.
1438 If optional CONFIRM-P is non-nil, the attribution is confirmed before
1439 its use in the citation string. This function first runs
1440 `sc-pre-cite-hook'.
1441
1442 When called interactively, the optional arg INTERACTIVE is non-nil,
1443 and that means call `sc-select-attribution' too."
1444 (interactive "r\nP\np")
1445 (undo-boundary)
1446 (let ((frame (sc-scan-info-alist sc-cite-frame-alist))
1447 (sc-confirm-always-p (if confirm-p t sc-confirm-always-p)))
1448 (if (and frame (symbolp frame))
1449 (setq frame (symbol-value frame)))
1450 (or frame (setq frame sc-default-cite-frame))
1451 (run-hooks 'sc-pre-cite-hook)
1452 (if interactive
1453 (sc-select-attribution))
1454 (regi-interpret frame start end)))
1455
1456 (defun sc-uncite-region (start end)
1457 "Uncite a region delineated by START and END.
1458 First runs `sc-pre-uncite-hook'."
1459 (interactive "r")
1460 (undo-boundary)
1461 (let ((frame (sc-scan-info-alist sc-uncite-frame-alist)))
1462 (if (and frame (symbolp frame))
1463 (setq frame (symbol-value frame)))
1464 (or frame (setq frame sc-default-uncite-frame))
1465 (run-hooks 'sc-pre-uncite-hook)
1466 (regi-interpret frame start end)))
1467
1468 (defun sc-recite-region (start end)
1469 "Recite a region delineated by START and END.
1470 First runs `sc-pre-recite-hook'."
1471 (interactive "r")
1472 (let ((sc-confirm-always-p t))
1473 (sc-select-attribution))
1474 (undo-boundary)
1475 (let ((frame (sc-scan-info-alist sc-recite-frame-alist)))
1476 (if (and frame (symbolp frame))
1477 (setq frame (symbol-value frame)))
1478 (or frame (setq frame sc-default-recite-frame))
1479 (run-hooks 'sc-pre-recite-hook)
1480 (regi-interpret frame start end)))
1481
1482 \f
1483 ;; ======================================================================
1484 ;; building headers
1485
1486 (defun sc-hdr (prefix field &optional sep return-nil-p)
1487 "Returns a concatenation of PREFIX and FIELD.
1488 If FIELD is not a string or is the empty string, the empty string will
1489 be returned. Optional third argument SEP is concatenated on the end if
1490 it is a string. Returns empty string, unless optional RETURN-NIL-P is
1491 non-nil."
1492 (if (and (stringp field)
1493 (not (string= field "")))
1494 (concat prefix field (or sep ""))
1495 (and (not return-nil-p) "")))
1496
1497 (defun sc-whofrom ()
1498 "Return the value of (sc-mail-field \"from\") or nil."
1499 (let ((sc-mumble nil))
1500 (sc-mail-field "from")))
1501
1502 (defun sc-no-header ()
1503 "Does nothing. Use this instead of nil to get a blank header."
1504 ())
1505
1506 (defun sc-no-blank-line-or-header()
1507 "Similar to `sc-no-header' except it removes the preceding blank line."
1508 (if (not (bobp))
1509 (if (and (eolp)
1510 (progn (forward-line -1)
1511 (or (= (point) (mail-header-end))
1512 (and (eq major-mode 'mh-letter-mode)
1513 (mh-in-header-p)))))
1514 (progn (forward-line)
1515 (let ((kill-lines-magic t))
1516 (kill-line))))))
1517
1518 (defun sc-header-on-said ()
1519 "\"On <date>, <from> said:\" unless:
1520 1. the \"from\" field cannot be found, in which case nothing is inserted;
1521 2. the \"date\" field is missing in which case only the from part is printed."
1522 (let ((sc-mumble "")
1523 (whofrom (sc-whofrom)))
1524 (if whofrom
1525 (insert sc-reference-tag-string
1526 (sc-hdr "On " (sc-mail-field "date") ", ")
1527 whofrom " said:\n"))))
1528
1529 (defun sc-header-inarticle-writes ()
1530 "\"In article <message-id>, <from> writes:\"
1531 Treats \"message-id\" and \"from\" fields similar to `sc-header-on-said'."
1532 (let ((sc-mumble "")
1533 (whofrom (sc-mail-field "from")))
1534 (if whofrom
1535 (insert sc-reference-tag-string
1536 (sc-hdr "In article " (sc-mail-field "message-id") ", ")
1537 whofrom " writes:\n"))))
1538
1539 (defun sc-header-regarding-adds ()
1540 "\"Regarding <subject>; <from> adds:\"
1541 Treats \"subject\" and \"from\" fields similar to `sc-header-on-said'."
1542 (let ((sc-mumble "")
1543 (whofrom (sc-whofrom)))
1544 (if whofrom
1545 (insert sc-reference-tag-string
1546 (sc-hdr "Regarding " (sc-mail-field "subject") "; ")
1547 whofrom " adds:\n"))))
1548
1549 (defun sc-header-attributed-writes ()
1550 "\"<sc-attribution>\" == <sc-author> <address> writes:
1551 Treats these fields in a similar manner to `sc-header-on-said'."
1552 (let ((sc-mumble "")
1553 (whofrom (sc-whofrom)))
1554 (if whofrom
1555 (insert sc-reference-tag-string
1556 (sc-hdr "\"" (sc-mail-field "sc-attribution") "\" == ")
1557 (sc-hdr "" (sc-mail-field "sc-author") " ")
1558 (or (sc-hdr "<" (sc-mail-field "sc-from-address") ">" t)
1559 (sc-hdr "<" (sc-mail-field "sc-reply-address") ">" t)
1560 "")
1561 " writes:\n"))))
1562
1563 (defun sc-header-author-writes ()
1564 "<sc-author> writes:"
1565 (let ((sc-mumble "")
1566 (whofrom (sc-whofrom)))
1567 (if whofrom
1568 (insert sc-reference-tag-string
1569 (sc-hdr "" (sc-mail-field "sc-author"))
1570 " writes:\n"))))
1571
1572 (defun sc-header-verbose ()
1573 "Very verbose, some say gross."
1574 (let ((sc-mumble "")
1575 (whofrom (sc-whofrom))
1576 (tag sc-reference-tag-string))
1577 (if whofrom
1578 (insert (sc-hdr (concat tag "On ") (sc-mail-field "date") ",\n")
1579 (or (sc-hdr tag (sc-mail-field "sc-author") "\n" t)
1580 (concat tag whofrom "\n"))
1581 (sc-hdr (concat tag "from the organization of ")
1582 (sc-mail-field "organization") "\n")
1583 (let ((rtag (concat tag "who can be reached at: ")))
1584 (or (sc-hdr rtag (sc-mail-field "sc-from-address") "\n" t)
1585 (sc-hdr rtag (sc-mail-field "sc-reply-address") "\n" t)
1586 ""))
1587 (sc-hdr
1588 (concat tag "(whose comments are cited below with \"")
1589 (sc-mail-field "sc-citation") "\"),\n")
1590 (sc-hdr (concat tag "had this to say in article ")
1591 (sc-mail-field "message-id") "\n")
1592 (sc-hdr (concat tag "in newsgroups ")
1593 (sc-mail-field "newsgroups") "\n")
1594 (sc-hdr (concat tag "concerning the subject of ")
1595 (sc-mail-field "subject") "\n")
1596 (sc-hdr (concat tag "(see ")
1597 (sc-mail-field "references")
1598 " for more details)\n")
1599 ))))
1600
1601 \f
1602 ;; ======================================================================
1603 ;; header rewrites
1604
1605 (defconst sc-electric-bufname " *sc-erefs* "
1606 "Supercite electric reference mode's buffer name.")
1607 (defvar sc-eref-style 0
1608 "Current electric reference style.")
1609
1610 (defun sc-valid-index-p (index)
1611 "Returns INDEX if it is a valid index into `sc-rewrite-header-list'.
1612 Otherwise returns nil."
1613 ;; a number, and greater than or equal to zero
1614 ;; less than or equal to the last index
1615 (and (natnump index)
1616 (< index (length sc-rewrite-header-list))
1617 index))
1618
1619 (defun sc-eref-insert-selected (&optional nomsg)
1620 "Insert the selected reference header in the current buffer.
1621 Optional NOMSG, if non-nil, inhibits printing messages, unless an
1622 error occurs."
1623 (let ((ref (nth sc-eref-style sc-rewrite-header-list)))
1624 (condition-case err
1625 (progn
1626 (eval ref)
1627 (let ((lines (count-lines (point-min) (point-max))))
1628 (or nomsg (message "Ref header %d [%d line%s]: %s"
1629 sc-eref-style lines
1630 (if (= lines 1) "" "s")
1631 ref))))
1632 (void-function
1633 (progn (message
1634 "Symbol's function definition is void: %s (Header %d)"
1635 (car (cdr err)) sc-eref-style)
1636 (beep)
1637 ))
1638 )))
1639
1640 (defun sc-electric-mode (&optional arg)
1641 "
1642 Mode for viewing Supercite reference headers. Commands are:
1643 \n\\{sc-electric-mode-map}
1644
1645 `sc-electric-mode' is not intended to be run interactively, but rather
1646 accessed through Supercite's electric reference feature. See
1647 `sc-insert-reference' for more details. Optional ARG is the initial
1648 header style to use, unless not supplied or invalid, in which case
1649 `sc-preferred-header-style' is used."
1650
1651 (let ((info sc-mail-info))
1652
1653 (setq sc-eref-style
1654 (or (sc-valid-index-p arg)
1655 (sc-valid-index-p sc-preferred-header-style)
1656 0))
1657
1658 (get-buffer-create sc-electric-bufname)
1659 ;; set up buffer and enter command loop
1660 (save-excursion
1661 (save-window-excursion
1662 (pop-to-buffer sc-electric-bufname)
1663 (kill-all-local-variables)
1664 (let ((sc-mail-info info)
1665 (buffer-read-only t)
1666 (mode-name "SC Electric Refs")
1667 (major-mode 'sc-electric-mode))
1668 (use-local-map sc-electric-mode-map)
1669 (sc-eref-show sc-eref-style)
1670 (run-hooks 'sc-electric-mode-hook)
1671 (recursive-edit)
1672 )))
1673
1674 (and sc-eref-style
1675 (sc-eref-insert-selected))
1676 (kill-buffer sc-electric-bufname)
1677 ))
1678
1679 ;; functions for electric reference mode
1680 (defun sc-eref-show (index)
1681 "Show reference INDEX in `sc-rewrite-header-list'."
1682 (let ((msg "No %ing reference headers in list.")
1683 (last (length sc-rewrite-header-list)))
1684 (setq sc-eref-style
1685 (cond
1686 ((sc-valid-index-p index) index)
1687 ((< index 0)
1688 (if sc-electric-circular-p
1689 (1- last)
1690 (progn (error msg "preced") 0)))
1691 ((>= index last)
1692 (if sc-electric-circular-p
1693 0
1694 (progn (error msg "follow") (1- last))))
1695 ))
1696 (save-excursion
1697 (set-buffer sc-electric-bufname)
1698 (let ((buffer-read-only nil))
1699 (erase-buffer)
1700 (goto-char (point-min))
1701 (sc-eref-insert-selected)
1702 ;; now shrink the window to just contain the electric reference
1703 ;; header.
1704 (let ((hdrlines (count-lines (point-min) (point-max)))
1705 (winlines (1- (window-height))))
1706 (if (/= hdrlines winlines)
1707 (if (> hdrlines winlines)
1708 ;; we have to enlarge the window
1709 (enlarge-window (- hdrlines winlines))
1710 ;; we have to shrink the window
1711 (shrink-window (- winlines (max hdrlines window-min-height)))
1712 )))
1713 ))))
1714
1715 (defun sc-eref-next ()
1716 "Display next reference in other buffer."
1717 (interactive)
1718 (sc-eref-show (1+ sc-eref-style)))
1719
1720 (defun sc-eref-prev ()
1721 "Display previous reference in other buffer."
1722 (interactive)
1723 (sc-eref-show (1- sc-eref-style)))
1724
1725 (defun sc-eref-setn ()
1726 "Set reference header selected as preferred."
1727 (interactive)
1728 (setq sc-preferred-header-style sc-eref-style)
1729 (message "Preferred reference style set to header %d." sc-eref-style))
1730
1731 (defun sc-eref-goto (refnum)
1732 "Show reference style indexed by REFNUM.
1733 If REFNUM is an invalid index, don't go to that reference and return
1734 nil."
1735 (interactive "NGoto Reference: ")
1736 (if (sc-valid-index-p refnum)
1737 (sc-eref-show refnum)
1738 (error "Invalid reference: %d. (Range: [%d .. %d])"
1739 refnum 0 (1- (length sc-rewrite-header-list)))
1740 ))
1741
1742 (defun sc-eref-jump ()
1743 "Set reference header to preferred header."
1744 (interactive)
1745 (sc-eref-show sc-preferred-header-style))
1746
1747 (defun sc-eref-abort ()
1748 "Exit from electric reference mode without inserting reference."
1749 (interactive)
1750 (setq sc-eref-style nil)
1751 (exit-recursive-edit))
1752
1753 (defun sc-eref-exit ()
1754 "Exit from electric reference mode and insert selected reference."
1755 (interactive)
1756 (exit-recursive-edit))
1757
1758 (defun sc-insert-reference (arg)
1759 "Insert, at point, a reference header in the body of the reply.
1760 Numeric ARG indicates which header style from `sc-rewrite-header-list'
1761 to use when rewriting the header. No supplied ARG indicates use of
1762 `sc-preferred-header-style'.
1763
1764 With just `\\[universal-argument]', electric reference insert mode is
1765 entered, regardless of the value of `sc-electric-references-p'. See
1766 `sc-electric-mode' for more information."
1767 (interactive "P")
1768 (if (consp arg)
1769 (sc-electric-mode)
1770 (let ((preference (or (sc-valid-index-p arg)
1771 (sc-valid-index-p sc-preferred-header-style)
1772 sc-preferred-header-style
1773 0)))
1774 (if sc-electric-references-p
1775 (sc-electric-mode preference)
1776 (sc-eref-insert-selected t)
1777 ))))
1778
1779 \f
1780 ;; ======================================================================
1781 ;; variable toggling
1782
1783 (defun sc-raw-mode-toggle ()
1784 "Toggle, in one fell swoop, two important SC variables:
1785 `sc-fixup-whitespace-p' and `sc-auto-fill-region-p'"
1786 (interactive)
1787 (setq sc-fixup-whitespace-p (not sc-fixup-whitespace-p)
1788 sc-auto-fill-region-p (not sc-auto-fill-region-p))
1789 (force-mode-line-update))
1790
1791 (defun sc-toggle-var (variable)
1792 "Boolean toggle VARIABLE's value.
1793 VARIABLE must be a bound symbol. nil values change to t, non-nil
1794 values are changed to nil."
1795 (message "%s changed from %s to %s"
1796 variable (symbol-value variable)
1797 (set variable (not (symbol-value variable)))))
1798
1799 (defun sc-set-variable (var)
1800 "Set the Supercite VARIABLE.
1801 This function mimics `set-variable', except that the variable to set
1802 is determined non-interactively. The value is queried for in the
1803 minibuffer exactly the same way that `set-variable' does it.
1804
1805 You can see the current value of the variable when the minibuffer is
1806 querying you by typing `C-h'. Note that the format is changed
1807 slightly from that used by `set-variable' -- the current value is
1808 printed just after the variable's name instead of at the bottom of the
1809 help window."
1810 (let* ((minibuffer-help-form '(funcall myhelp))
1811 (myhelp
1812 (lambda ()
1813 (with-output-to-temp-buffer "*Help*"
1814 (prin1 var)
1815 (if (boundp var)
1816 (let ((print-length 20))
1817 (princ "\t(Current value: ")
1818 (prin1 (symbol-value var))
1819 (princ ")")))
1820 (princ "\n\nDocumentation:\n")
1821 (princ (substring (documentation-property
1822 var
1823 'variable-documentation)
1824 1))
1825 (with-current-buffer standard-output
1826 (help-mode))
1827 nil))))
1828 (set var (eval-minibuffer (format "Set %s to value: " var)))))
1829
1830 (defmacro sc-toggle-symbol (rootname)
1831 `(defun ,(intern (concat "sc-T-" rootname)) ()
1832 (interactive)
1833 (sc-toggle-var ',(intern (concat "sc-" rootname "-p")))))
1834
1835 (defmacro sc-setvar-symbol (rootname)
1836 `(defun ,(intern (concat "sc-S-" rootname)) ()
1837 (interactive)
1838 (sc-set-variable ',(intern (concat "sc-" rootname)))))
1839
1840 (sc-toggle-symbol "confirm-always")
1841 (sc-toggle-symbol "downcase")
1842 (sc-toggle-symbol "electric-references")
1843 (sc-toggle-symbol "auto-fill-region")
1844 (sc-toggle-symbol "mail-nuke-blank-lines")
1845 (sc-toggle-symbol "nested-citation")
1846 (sc-toggle-symbol "electric-circular")
1847 (sc-toggle-symbol "use-only-preferences")
1848 (sc-toggle-symbol "fixup-whitespace")
1849
1850 (sc-setvar-symbol "preferred-attribution-list")
1851 (sc-setvar-symbol "preferred-header-style")
1852 (sc-setvar-symbol "mail-nuke-mail-headers")
1853 (sc-setvar-symbol "mail-header-nuke-list")
1854 (sc-setvar-symbol "cite-region-limit")
1855
1856 (defun sc-T-describe ()
1857 "
1858
1859 Supercite provides a number of key bindings which simplify the process
1860 of setting or toggling certain variables controlling its operation.
1861
1862 Note on function names in this list: all functions of the form
1863 `sc-S-<name>' actually call `sc-set-variable' on the corresponding
1864 `sc-<name>' variable. All functions of the form `sc-T-<name>' call
1865 `sc-toggle-var' on the corresponding `sc-<name>-p' variable.
1866
1867 \\{sc-T-keymap}"
1868 (interactive)
1869 (describe-function 'sc-T-describe))
1870
1871 \f
1872 ;; ======================================================================
1873 ;; published interface to mail and news readers
1874
1875 (define-minor-mode sc-minor-mode
1876 "Supercite minor mode."
1877 :group 'supercite
1878 :lighter (" SC" (sc-auto-fill-region-p
1879 (":f" (sc-fixup-whitespace-p "w"))
1880 (sc-fixup-whitespace-p ":w")))
1881 :keymap `((,sc-mode-map-prefix . ,sc-mode-map)))
1882
1883 ;;;###autoload
1884 (defun sc-cite-original ()
1885 "Workhorse citing function which performs the initial citation.
1886 This is callable from the various mail and news readers' reply
1887 function according to the agreed upon standard. See the associated
1888 info node `(SC)Top' for more details.
1889 `sc-cite-original' does not do any yanking of the
1890 original message but it does require a few things:
1891
1892 1) The reply buffer is the current buffer.
1893
1894 2) The original message has been yanked and inserted into the
1895 reply buffer.
1896
1897 3) Verbose mail headers from the original message have been
1898 inserted into the reply buffer directly before the text of the
1899 original message.
1900
1901 4) Point is at the beginning of the verbose headers.
1902
1903 5) Mark is at the end of the body of text to be cited.
1904
1905 For Emacs 19's, the region need not be active (and typically isn't
1906 when this function is called. Also, the hook `sc-pre-hook' is run
1907 before, and `sc-post-hook' is run after the guts of this function."
1908 (run-hooks 'sc-pre-hook)
1909
1910 (sc-minor-mode 1)
1911
1912 (undo-boundary)
1913
1914 ;; grab point and mark since the region is probably not active when
1915 ;; this function gets automatically called. we want point to be a
1916 ;; mark so any deleting before point works properly
1917 (let* ((zmacs-regions nil) ; for XEemacs
1918 (mark-active t) ; for Emacs
1919 (point (point-marker))
1920 (mark (copy-marker (mark-marker))))
1921
1922 ;; make sure point comes before mark, not all functions are
1923 ;; interactive "r"
1924 (if (< mark point)
1925 (let ((tmp point))
1926 (setq point mark
1927 mark tmp)))
1928
1929 ;; first process mail headers, and populate sc-mail-info
1930 (sc-mail-process-headers point mark)
1931
1932 ;; now get possible attributions
1933 (sc-attribs-chop-address (or (sc-mail-field "from")
1934 (sc-mail-field "reply")
1935 (sc-mail-field "reply-to")
1936 (sc-mail-field "sender")))
1937 ;; select the attribution
1938 (sc-select-attribution)
1939
1940 ;; cite the region, but first check the value of sc-cite-region-limit
1941 (let ((linecnt (count-lines point mark)))
1942 (and sc-cite-region-limit
1943 (if (or (not (numberp sc-cite-region-limit))
1944 (<= linecnt sc-cite-region-limit))
1945 (progn
1946 ;; cite the region and insert the header rewrite
1947 (sc-cite-region point mark)
1948 (goto-char point)
1949 (let ((sc-eref-style (or sc-preferred-header-style 0)))
1950 (if sc-electric-references-p
1951 (sc-electric-mode sc-eref-style)
1952 (sc-eref-insert-selected t))))
1953 (beep)
1954 (message
1955 "Region not cited. %d lines exceeds sc-cite-region-limit: %d"
1956 linecnt sc-cite-region-limit))))
1957
1958 ;; finally, free the point-marker
1959 (set-marker point nil)
1960 (set-marker mark nil)
1961 )
1962 (run-hooks 'sc-post-hook))
1963
1964 \f
1965 ;; ======================================================================
1966 ;; bug reporting and miscellaneous commands
1967
1968 (defun sc-open-line (arg)
1969 "Like `open-line', but insert the citation prefix at the front of the line.
1970 With numeric ARG, inserts that many new lines."
1971 (interactive "p")
1972 (save-excursion
1973 (let ((start (point))
1974 (prefix (or (progn (beginning-of-line)
1975 (if (looking-at (sc-cite-regexp))
1976 (match-string 0)))
1977 "")))
1978 (goto-char start)
1979 (open-line arg)
1980 (forward-line 1)
1981 (while (< 0 arg)
1982 (insert prefix)
1983 (forward-line 1)
1984 (setq arg (1- arg))
1985 ))))
1986
1987 (defun sc-insert-citation (arg)
1988 "Insert citation string at beginning of current line if not already cited.
1989 With `\\[universal-argument]' insert citation even if line is already
1990 cited."
1991 (interactive "P")
1992 (save-excursion
1993 (beginning-of-line)
1994 (if (or (not (looking-at (sc-cite-regexp)))
1995 (looking-at "^[ \t]*$")
1996 (consp arg))
1997 (insert (sc-mail-field "sc-citation"))
1998 (error "Line is already cited"))))
1999
2000 (defun sc-version (message)
2001 "Echo the current version of Supercite in the minibuffer.
2002 If MESSAGE is non-nil (interactively, with no prefix argument),
2003 inserts the version string in the current buffer instead."
2004 (interactive (not current-prefix-arg))
2005 (let ((verstr (format "Using Supercite.el %s" sc-version)))
2006 (if message
2007 (message verstr)
2008 (insert "`sc-version' says: " verstr))))
2009
2010 (defun sc-describe ()
2011 "
2012 Supercite is a package which provides a flexible mechanism for citing
2013 email and news replies. Please see the associated texinfo file for
2014 more information. Info node `(SC)Top'."
2015 (interactive)
2016 (describe-function 'sc-describe))
2017
2018 (defun sc-submit-bug-report ()
2019 "Submit a bug report on Supercite via mail."
2020 (interactive)
2021 (require 'reporter)
2022 (and
2023 (y-or-n-p "Do you want to submit a report on Supercite? ")
2024 (reporter-submit-bug-report
2025 sc-help-address
2026 (concat "Supercite version " sc-version)
2027 (list
2028 'sc-attrib-selection-list
2029 'sc-auto-fill-region-p
2030 'sc-blank-lines-after-headers
2031 'sc-citation-leader
2032 'sc-citation-delimiter
2033 'sc-citation-separator
2034 'sc-citation-leader-regexp
2035 'sc-citation-root-regexp
2036 'sc-citation-nonnested-root-regexp
2037 'sc-citation-delimiter-regexp
2038 'sc-citation-separator-regexp
2039 'sc-cite-region-limit
2040 'sc-confirm-always-p
2041 'sc-default-attribution
2042 'sc-default-author-name
2043 'sc-downcase-p
2044 'sc-electric-circular-p
2045 'sc-electric-references-p
2046 'sc-fixup-whitespace-p
2047 'sc-mail-warn-if-non-rfc822-p
2048 'sc-mumble
2049 'sc-name-filter-alist
2050 'sc-nested-citation-p
2051 'sc-nuke-mail-headers
2052 'sc-nuke-mail-header-list
2053 'sc-preferred-attribution-list
2054 'sc-preferred-header-style
2055 'sc-reference-tag-string
2056 'sc-rewrite-header-list
2057 'sc-titlecue-regexp
2058 'sc-use-only-preference-p
2059 ))))
2060
2061 \f
2062 ;; useful stuff
2063 (provide 'supercite)
2064 (run-hooks 'sc-load-hook)
2065
2066 ;; arch-tag: a5d5bfa6-3bd5-4414-8c65-0afc83e45cd3
2067 ;;; supercite.el ends here