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