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