Convert consecutive FSF copyright years to ranges.
[bpt/emacs.git] / lisp / mail / supercite.el
CommitLineData
30b7c745
RS
1;;; supercite.el --- minor mode for citing mail and news replies
2
73b0cd50 3;; Copyright (C) 1993, 1997, 2001-2011 Free Software Foundation, Inc.
dcb38be8 4
5231aacb 5;; Author: 1993 Barry A. Warsaw <bwarsaw@python.org>
aff88519 6;; Maintainer: Glenn Morris <rgm@gnu.org>
30b7c745 7;; Created: February 1993
b7f66977 8;; Keywords: mail, news
30b7c745 9
30b7c745
RS
10;; This file is part of GNU Emacs.
11
b1fc2b50 12;; GNU Emacs is free software: you can redistribute it and/or modify
30b7c745 13;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
30b7c745
RS
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
b1fc2b50 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
30b7c745
RS
24
25;; LCD Archive Entry
6923dac3 26;; supercite|Barry A. Warsaw|supercite-help@python.org
30b7c745
RS
27;; |Mail and news reply citation package
28;; |1993/09/22 18:58:46|3.1|
29
e8af40ee
PJ
30;;; Commentary:
31
32;;; Code:
30b7c745
RS
33
34\f
35(require 'regi)
36
37;; start user configuration variables
38;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
39
e38eebc2 40(defgroup supercite nil
434a76a2 41 "Supercite package."
e38eebc2
RS
42 :prefix "sc-"
43 :group 'mail
44 :group 'news)
45
46(defgroup supercite-frames nil
434a76a2 47 "Supercite (regi) frames."
e38eebc2
RS
48 :prefix "sc-"
49 :group 'supercite)
50
51(defgroup supercite-attr nil
434a76a2 52 "Supercite attributions."
e38eebc2
RS
53 :prefix "sc-"
54 :group 'supercite)
55
56(defgroup supercite-cite nil
434a76a2 57 "Supercite citings."
e38eebc2
RS
58 :prefix "sc-"
59 :group 'supercite)
60
61(defgroup supercite-hooks nil
434a76a2 62 "Hooking into supercite."
e38eebc2
RS
63 :prefix "sc-"
64 :group 'supercite)
65
66(defcustom sc-auto-fill-region-p t
613966a5 67 "If non-nil, automatically fill each paragraph after it has been cited."
e38eebc2
RS
68 :type 'boolean
69 :group 'supercite)
70
71(defcustom sc-blank-lines-after-headers 1
613966a5 72 "Number of blank lines to leave after mail headers have been nuked.
e38eebc2
RS
73Set 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 " "
613966a5 79 "String comprising first part of a citation."
e38eebc2
RS
80 :type 'string
81 :group 'supercite-cite)
30b7c745 82
e38eebc2 83(defcustom sc-citation-delimiter ">"
613966a5 84 "String comprising third part of a citation.
e38eebc2
RS
85This string is used in both nested and non-nested citations."
86 :type 'string
87 :group 'supercite-cite)
88
89(defcustom sc-citation-separator " "
613966a5 90 "String comprising fourth and last part of a citation."
e38eebc2
RS
91 :type 'string
92 :group 'supercite-cite)
30b7c745 93
e38eebc2 94(defcustom sc-citation-leader-regexp "[ \t]*"
613966a5 95 "Regexp describing citation leader for a cited line.
e38eebc2
RS
96This should NOT have a leading `^' character."
97 :type 'regexp
98 :group 'supercite-cite)
30b7c745
RS
99
100;; Nemacs and Mule users note: please see the texinfo manual for
101;; suggestions on setting these variables.
82db7fdf 102(defcustom sc-citation-root-regexp "[-._[:alnum:]]*"
613966a5 103 "Regexp describing variable root part of a citation for a cited line.
30b7c745 104This should NOT have a leading `^' character. See also
e38eebc2
RS
105`sc-citation-nonnested-root-regexp'."
106 :type 'regexp
107 :group 'supercite-cite)
108
82db7fdf 109(defcustom sc-citation-nonnested-root-regexp "[-._[:alnum:]]+"
613966a5 110 "Regexp describing the variable root part of a nested citation.
30b7c745 111This should NOT have a leading `^' character. This variable is
80f16257 112related to `sc-citation-root-regexp' but whereas that variable
30b7c745 113describes both nested and non-nested citation roots, this variable
e38eebc2
RS
114describes only nested citation roots."
115 :type 'regexp
116 :group 'supercite-cite)
117
118(defcustom sc-citation-delimiter-regexp "[>]+"
613966a5 119 "Regexp describing citation delimiter for a cited line.
e38eebc2
RS
120This should NOT have a leading `^' character."
121 :type 'regexp
122 :group 'supercite-cite)
123
124(defcustom sc-citation-separator-regexp "[ \t]*"
613966a5 125 "Regexp describing citation separator for a cited line.
e38eebc2
RS
126This should NOT have a leading `^' character."
127 :type 'regexp
128 :group 'supercite-cite)
30b7c745 129
e38eebc2 130(defcustom sc-cite-blank-lines-p nil
613966a5 131 "If non-nil, put a citation on blank lines."
e38eebc2
RS
132 :type 'boolean
133 :group 'supercite-cite)
30b7c745 134
e38eebc2 135(defcustom sc-cite-frame-alist '()
613966a5 136 "Alist for frame selection during citing.
30b7c745 137Each element of this list has the following form:
30b7c745
RS
138 (INFOKEY ((REGEXP . FRAME)
139 (REGEXP . FRAME)
140 (...)))
141
142Where INFOKEY is a key for `sc-mail-field', REGEXP is a regular
37d8fcc2
JL
143expression to match against the INFOKEY's value. FRAME is
144a citation frame, or a symbol that represents the name of
145a variable whose value is a citation frame."
e38eebc2
RS
146 :type '(repeat (list symbol (repeat (cons regexp
147 (choice (repeat (repeat sexp))
148 symbol)))))
149 :group 'supercite-frames)
22458c33 150(put 'sc-cite-frame-alist 'risky-local-variable t)
e38eebc2
RS
151
152(defcustom sc-uncite-frame-alist '()
613966a5 153 "Alist for frame selection during unciting.
e38eebc2
RS
154See 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)
22458c33 159(put 'sc-uncite-frame-alist 'risky-local-variable t)
e38eebc2
RS
160
161(defcustom sc-recite-frame-alist '()
613966a5 162 "Alist for frame selection during reciting.
e38eebc2
RS
163See 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)
22458c33 168(put 'sc-recite-frame-alist 'risky-local-variable t)
30b7c745 169
e38eebc2 170(defcustom sc-default-cite-frame
30b7c745
RS
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 "\\)"
a3d3fe6e
GM
181 (sc-cite-regexp
182 sc-citation-nonnested-root-regexp)))))
30b7c745
RS
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.
a3d3fe6e 209 (end (sc-fill-if-different "")))
613966a5 210 "Default REGI frame for citing a region."
e38eebc2
RS
211 :type '(repeat (repeat sexp))
212 :group 'supercite-frames)
22458c33 213(put 'sc-default-cite-frame 'risky-local-variable t)
30b7c745 214
e38eebc2 215(defcustom sc-default-uncite-frame
30b7c745
RS
216 '(;; do nothing on a blank line
217 ("^[ \t]*$" nil)
218 ;; if the line is cited, uncite it
a3d3fe6e 219 ((sc-cite-regexp) (sc-uncite-line)))
613966a5 220 "Default REGI frame for unciting a region."
e38eebc2
RS
221 :type '(repeat (repeat sexp))
222 :group 'supercite-frames)
22458c33 223(put 'sc-default-uncite-frame 'risky-local-variable t)
30b7c745 224
e38eebc2 225(defcustom sc-default-recite-frame
30b7c745
RS
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.
a3d3fe6e 235 (end (sc-fill-if-different "")))
613966a5 236 "Default REGI frame for reciting a region."
e38eebc2
RS
237 :type '(repeat (repeat sexp))
238 :group 'supercite-frames)
22458c33 239(put 'sc-default-recite-frame 'risky-local-variable t)
30b7c745 240
e38eebc2 241(defcustom sc-cite-region-limit t
613966a5 242 "This variable controls automatic citation of yanked text.
0486e05a 243Valid values are:
30b7c745
RS
244
245non-nil -- cite the entire region, regardless of its size
246nil -- 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
a1506d29 251 `count-lines'.
30b7c745
RS
252
253The gathering of attribution information is not affected by the value
254of this variable. The number of lines in the region is calculated
255*after* all mail headers are removed. This variable is only consulted
e38eebc2 256during the initial citing via `sc-cite-original'."
1af20361
AS
257 :type '(choice (const :tag "do not cite" nil)
258 (integer :tag "citation threshold")
259 (other :tag "always cite" t))
e38eebc2
RS
260 :group 'supercite-cite)
261
262(defcustom sc-confirm-always-p t
613966a5 263 "If non-nil, always confirm attribution string before citing text body."
e38eebc2
RS
264 :type 'boolean
265 :group 'supercite-attr)
266
267(defcustom sc-default-attribution "Anon"
613966a5 268 "String used when author's attribution cannot be determined."
e38eebc2
RS
269 :type 'string
270 :group 'supercite-attr)
271(defcustom sc-default-author-name "Anonymous"
613966a5 272 "String used when author's name cannot be determined."
e38eebc2
RS
273 :type 'string
274 :group 'supercite-attr)
275(defcustom sc-downcase-p nil
613966a5 276 "Non-nil means downcase the attribution and citation strings."
e38eebc2
RS
277 :type 'boolean
278 :group 'supercite-attr
279 :group 'supercite-cite)
280(defcustom sc-electric-circular-p t
613966a5 281 "If non-nil, treat electric references as circular."
e38eebc2
RS
282 :type 'boolean
283 :group 'supercite-attr)
284
285(defcustom sc-electric-mode-hook nil
613966a5 286 "Hook for `sc-electric-mode' electric references mode."
e38eebc2
RS
287 :type 'hook
288 :group 'supercite-hooks)
289(defcustom sc-electric-references-p nil
613966a5 290 "Use electric references if non-nil."
e38eebc2
RS
291 :type 'boolean
292 :group 'supercite)
293
294(defcustom sc-fixup-whitespace-p nil
613966a5 295 "If non-nil, delete all leading white space before citing."
e38eebc2
RS
296 :type 'boolean
297 :group 'supercite)
298
299(defcustom sc-load-hook nil
613966a5 300 "Hook which gets run once after Supercite loads."
e38eebc2
RS
301 :type 'hook
302 :group 'supercite-hooks)
303(defcustom sc-pre-hook nil
613966a5 304 "Hook which gets run before each invocation of `sc-cite-original'."
e38eebc2
RS
305 :type 'hook
306 :group 'supercite-hooks)
307(defcustom sc-post-hook nil
613966a5 308 "Hook which gets run after each invocation of `sc-cite-original'."
e38eebc2
RS
309 :type 'hook
310 :group 'supercite-hooks)
311
312(defcustom sc-mail-warn-if-non-rfc822-p t
613966a5 313 "Warn if mail headers don't conform to RFC822."
e38eebc2
RS
314 :type 'boolean
315 :group 'supercite-attr)
316(defcustom sc-mumble ""
613966a5 317 "Value returned by `sc-mail-field' if field isn't in mail headers."
e38eebc2
RS
318 :type 'string
319 :group 'supercite-attr)
320
321(defcustom sc-name-filter-alist
30b7c745
RS
322 '(("^\\(Mr\\|Mrs\\|Ms\\|Dr\\)[.]?$" . 0)
323 ("^\\(Jr\\|Sr\\)[.]?$" . last)
324 ("^ASTS$" . 0)
325 ("^[I]+$" . last))
613966a5 326 "Name list components which are filtered out as noise.
30b7c745
RS
327This variable contains an association list where each element is of
328the form: (REGEXP . POSITION).
329
330REGEXP is a regular expression which matches the name list component.
331Match is performed using `string-match'. POSITION is the position in
332the name list which can match the regular expression, starting at zero
333for the first element. Use `last' to match the last element in the
e38eebc2
RS
334list and `any' to match all elements."
335 :type '(repeat (cons regexp (choice (const last) (const any)
336 (integer :tag "position"))))
337 :group 'supercite-attr)
30b7c745 338
e38eebc2 339(defcustom sc-nested-citation-p nil
613966a5 340 "Controls whether to use nested or non-nested citation style.
e38eebc2
RS
341Non-nil uses nested citations, nil uses non-nested citations."
342 :type 'boolean
343 :group 'supercite)
30b7c745 344
e38eebc2 345(defcustom sc-nuke-mail-headers 'all
613966a5 346 "Controls mail header nuking.
0486e05a 347Used in conjunction with `sc-nuke-mail-header-list'. Valid values are:
30b7c745
RS
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'
e38eebc2
RS
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)
30b7c745 356
e38eebc2 357(defcustom sc-nuke-mail-header-list nil
613966a5 358 "List of mail header regexps to remove or keep in body of reply.
30b7c745 359This list contains regular expressions describing the mail headers to
e38eebc2
RS
360keep or nuke, depending on the value of `sc-nuke-mail-headers'."
361 :type '(repeat regexp)
362 :group 'supercite)
30b7c745 363
e38eebc2 364(defcustom sc-preferred-attribution-list
30b7c745 365 '("sc-lastchoice" "x-attribution" "firstname" "initials" "lastname")
613966a5 366 "Specifies what to use as the attribution string.
30b7c745
RS
367Supercite creates a list of possible attributions when it scans the
368mail headers from the original message. Each attribution choice is
369associated with a key in an attribution alist. Supercite tries to
370pick a \"preferred\" attribution by matching the attribution alist
371keys against the elements in `sc-preferred-attribution-list' in order.
372The first non-empty string value found is used as the preferred
373attribution.
374
375Note that Supercite now honors the X-Attribution: mail field. If
376present in the original message, the value of this field should always
377be used to select the most preferred attribution since it reflects how
378the original author would like to be distinguished. It should be
379considered bad taste to put any attribution preference key before
380\"x-attribution\" in this list, except perhaps for \"sc-lastchoice\"
a4e104bf 381\(see below).
30b7c745
RS
382
383Supercite remembers the last attribution used when reciting an already
384cited paragraph. This attribution will always be saved with the
385\"sc-lastchoice\" key, which can be used in this list. Note that the
386last choice is always reset after every call of `sc-cite-original'.
387
388Barring error conditions, the following preferences are always present
389in 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
399Middle name indexes can be any positive integer greater than 0,
400although it is unlikely that many authors will supply more than one
401middle name, if that many. The string of all middle names is
e38eebc2
RS
402associated with the key \"middlenames\"."
403 :type '(repeat string)
404 :group 'supercite-attr)
30b7c745 405
e38eebc2 406(defcustom sc-attrib-selection-list nil
613966a5 407 "An alist for selecting preferred attribution based on mail headers.
30b7c745
RS
408Each element of this list has the following form:
409
410 (INFOKEY ((REGEXP . ATTRIBUTION)
411 (REGEXP . ATTRIBUTION)
412 (...)))
413
414Where INFOKEY is a key for `sc-mail-field', REGEXP is a regular
d7df8561
JB
415expression to match against the INFOKEY's value. ATTRIBUTION can be a
416string or a list. If it's a string, then it is the attribution that is
30b7c745
RS
417selected by `sc-select-attribution'. If it is a list, it is `eval'd
418and the return value must be a string, which is used as the selected
419attribution. Note that the variable `sc-preferred-attribution-list'
420must contain an element of the string \"sc-consult\" for this variable
e38eebc2 421to be consulted during attribution selection."
98237e7c
DL
422 :type '(repeat (list string
423 (repeat (cons regexp
424 (choice (sexp :tag "List to eval")
425 string)))))
e38eebc2 426 :group 'supercite-attr)
22458c33 427(put 'sc-attrib-selection-list 'risky-local-variable t)
e38eebc2
RS
428
429(defcustom sc-attribs-preselect-hook nil
613966a5 430 "Hook to run before selecting an attribution."
e38eebc2
RS
431 :type 'hook
432 :group 'supercite-attr
433 :group 'supercite-hooks)
434(defcustom sc-attribs-postselect-hook nil
613966a5 435 "Hook to run after selecting an attribution, but before confirmation."
e38eebc2
RS
436 :type 'hook
437 :group 'supercite-attr
438 :group 'supercite-hooks)
439
440(defcustom sc-pre-cite-hook nil
613966a5 441 "Hook to run before citing a region of text."
e38eebc2
RS
442 :type 'hook
443 :group 'supercite-cite
444 :group 'supercite-hooks)
445(defcustom sc-pre-uncite-hook nil
613966a5 446 "Hook to run before unciting a region of text."
e38eebc2
RS
447 :type 'hook
448 :group 'supercite-cite
449 :group 'supercite-hooks)
450(defcustom sc-pre-recite-hook nil
613966a5 451 "Hook to run before reciting a region of text."
e38eebc2
RS
452 :type 'hook
453 :group 'supercite-cite
454 :group 'supercite-hooks)
455
456(defcustom sc-preferred-header-style 4
613966a5 457 "Index into `sc-rewrite-header-list' specifying preferred header style.
e38eebc2
RS
458Index zero accesses the first function in the list."
459 :type 'integer
460 :group 'supercite)
30b7c745 461
e38eebc2 462(defcustom sc-reference-tag-string ">>>>> "
613966a5 463 "String used at the beginning of built-in reference headers."
e38eebc2
RS
464 :type 'string
465 :group 'supercite)
30b7c745 466
e38eebc2 467(defcustom sc-rewrite-header-list
30b7c745
RS
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)
a3d3fe6e 475 (sc-no-blank-line-or-header))
613966a5 476 "List of reference header rewrite functions.
30b7c745
RS
477The variable `sc-preferred-header-style' controls which function in
478this list is chosen for automatic reference header insertions.
e38eebc2
RS
479Electric reference mode will cycle through this list of functions."
480 :type '(repeat sexp)
481 :group 'supercite)
22458c33 482(put 'sc-rewrite-header-list 'risky-local-variable t)
30b7c745 483
e38eebc2 484(defcustom sc-titlecue-regexp "\\s +-+\\s +"
613966a5 485 "Regular expression describing the separator between names and titles.
e38eebc2
RS
486Set 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)
30b7c745 490
e38eebc2 491(defcustom sc-use-only-preference-p nil
613966a5 492 "Controls what happens when the preferred attribution cannot be found.
30b7c745
RS
493If non-nil, then `sc-default-attribution' will be used. If nil, then
494some secondary scheme will be employed to find a suitable attribution
e38eebc2
RS
495string."
496 :type 'boolean
497 :group 'supercite-attr)
30b7c745 498
613966a5
GM
499(defcustom sc-mode-map-prefix "\C-c\C-p"
500 "Key binding to install Supercite keymap."
501 :type 'string
502 :group 'supercite)
503
30b7c745
RS
504;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
505;; end user configuration variables
506\f
a445af6b 507(define-obsolete-variable-alias 'sc-version 'emacs-version "23.1")
30b7c745
RS
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
30b7c745 514(defvar sc-tmp-nested-regexp nil
d49b7f88 515 "Temporary regexp describing nested citations.")
30b7c745
RS
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
30b7c745
RS
521(make-variable-buffer-local 'sc-mail-info)
522(make-variable-buffer-local 'sc-attributions)
30b7c745
RS
523
524\f
525;; ======================================================================
526;; supercite keymaps
527
d49b7f88
SM
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)
30b7c745 547 "Keymap for sub-keymap of setting and toggling functions.")
d49b7f88
SM
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)
30b7c745 566 "Keymap for Supercite quasi-mode.")
d49b7f88
SM
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)
30b7c745 584 "Keymap for `sc-electric-mode' electric references mode.")
d49b7f88
SM
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)
30b7c745 592 "Keymap for minibuffer confirmation of attribution strings.")
d49b7f88
SM
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)
30b7c745 598 "Keymap for minibuffer confirmation of attribution strings.")
30b7c745
RS
599
600\f
601;; ======================================================================
602;; utility functions
603
30b7c745
RS
604(defun sc-ask (alist)
605 "Ask a question in the minibuffer requiring a single character answer.
606This function is kind of an extension of `y-or-n-p' where a single
607letter is used to answer a question. Question is formed from ALIST
608which has members of the form: (WORD . LETTER). WORD is the long
609word form, while LETTER is the letter for selecting that answer. The
610selected letter is returned, or nil if the question was not answered.
611Note that WORD is a string and LETTER is a character. All LETTERs in
612the list should be unique."
613 (let* ((prompt (concat
a3d3fe6e 614 (mapconcat (lambda (elt) (car elt)) alist ", ")
30b7c745
RS
615 "? ("
616 (mapconcat
a3d3fe6e 617 (lambda (elt) (char-to-string (cdr elt))) alist "/")
30b7c745
RS
618 ") "))
619 (p prompt)
620 (event
d49b7f88 621 (if (fboundp 'allocate-event)
30b7c745
RS
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)
d49b7f88 628 (setq event (read-event))
30b7c745
RS
629 (prog1 quit-flag (setq quit-flag nil)))
630 (progn
631 (message "%s%s" p (single-key-description event))
1eb6c5b8
RS
632 (if (fboundp 'deallocate-event)
633 (deallocate-event event))
30b7c745
RS
634 (setq quit-flag nil)
635 (signal 'quit '())))
636 (let ((char
d49b7f88 637 (if (featurep 'xemacs)
30b7c745
RS
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)))
1eb6c5b8
RS
648 ((if (fboundp 'button-release-event-p)
649 (button-release-event-p event)) ; ignore them
30b7c745
RS
650 nil)
651 (t
652 (message "%s%s" p (single-key-description event))
d49b7f88 653 (if (featurep 'xemacs)
30b7c745
RS
654 (ding nil 'y-or-n-p)
655 (ding))
656 (discard-input)
657 (if (eq p prompt)
658 (setq p (concat "Try again. " prompt)))))))
1eb6c5b8
RS
659 (if (fboundp 'deallocate-event)
660 (deallocate-event event))
30b7c745
RS
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))
a3d3fe6e 671 (mlist (cadr elem)))
30b7c745
RS
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
98237e7c 678 (setq rtnvalue thing
30b7c745
RS
679 mlist nil
680 alist nil)
681 ;; else we didn't find a match
a3d3fe6e
GM
682 (setq mlist (cdr mlist))))) ;end of mlist loop
683 (setq alist (cdr alist)))) ;end of alist loop
30b7c745
RS
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)))
a8d4467b 701 ("^From " (sc-mail-check-from) nil nil)
30b7c745
RS
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.")
c4f7f786 709(put 'sc-mail-glom-frame 'risky-local-variable t)
30b7c745 710
d49b7f88 711(defvar curline) ; dynamic bondage
82db7fdf 712
30b7c745 713;; regi functions
a8d4467b
GM
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.
720Such a line should only occur at the very start of the headers."
721 (and sc-mail-warn-if-non-rfc822-p
b22f4f66 722 (/= (point) sc-mail-headers-start)
a8d4467b
GM
723 (sc-mail-error-in-mail-field)))
724
30b7c745
RS
725(defun sc-mail-fetch-field (&optional attribs-p)
726 "Insert a key and value into `sc-mail-info' alist.
727If 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)
d49b7f88
SM
730 (let* ((key (downcase (match-string-no-properties 1 curline)))
731 (val (match-string-no-properties 2 curline))
30b7c745 732 (keyval (cons key val)))
d49b7f88 733 (push keyval sc-mail-info)
30b7c745 734 (if attribs-p
d49b7f88 735 (push keyval sc-attributions))))
30b7c745
RS
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))
4a3393e4 742 (setcdr keyval (concat (cdr keyval) " "
d49b7f88 743 (match-string-no-properties 1 curline)))))
30b7c745
RS
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."
d49b7f88 762 (delete-region (line-beginning-position) (line-beginning-position 2))
30b7c745
RS
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.
777The 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]*$")
d49b7f88
SM
786 (delete-region (line-beginning-position)
787 (line-beginning-position 2)))
30b7c745
RS
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))
9d034245 805 (t (error "Invalid value for sc-nuke-mail-headers: %s"
a3d3fe6e 806 sc-nuke-mail-headers))) ; end-cond
30b7c745
RS
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)))
a3d3fe6e 815 (and every-func (list (list 'every every-func))))))
30b7c745
RS
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.
821After processing, mail headers may be nuked. Header information is
822stored in `sc-mail-info', and any old information is lost unless an
823error 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)
a3d3fe6e 836 sc-mail-headers-start sc-mail-headers-end))))
30b7c745
RS
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.
842If there was no mail header with FIELD as its key, return the value of
843`sc-mumble'. FIELD is case insensitive."
caf3c78f 844 (or (cdr (assoc-string field sc-mail-info 'case-fold)) sc-mumble))
30b7c745
RS
845
846(defun sc-mail-field-query (arg)
847 "View the value of a mail field.
848With `\\[universal-argument]', prompts for action on mail field.
849Action 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 ()
d49b7f88 856 (setq key (completing-read
30b7c745
RS
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
d49b7f88
SM
870 (if (not (member (cdr keyval)
871 sc-mail-field-modification-history))
30b7c745
RS
872 (setq sc-mail-field-modification-history
873 (cons (cdr keyval) sc-mail-field-modification-history)))
d49b7f88 874 (setcdr keyval (read-string
30b7c745
RS
875 (concat key ": ") (cdr keyval)
876 'sc-mail-field-modification-history))))
877 ((eq action ?a)
a3d3fe6e 878 (push (cons key (read-string (concat key ": "))) sc-mail-info))))))
30b7c745
RS
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.
891Match addresses of the style ``name%[stuff].'' when called with DELIM
892of \"%\" and addresses of the style ``[stuff]name@[stuff]'' when
893called with DELIM \"@\". If DELIM is nil or not provided, matches
894addresses of the style ``name''."
d49b7f88 895 (and (string-match (concat "[-[:alnum:]_.]+" delim) from 0)
30b7c745
RS
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.
902Match addresses of the style ``[stuff]![stuff]...!name[stuff].''"
903 (let ((eos (length from))
d49b7f88 904 (mstart (string-match "![-[:alnum:]_.]+\\([^-![:alnum:]_.]\\|$\\)"
30b7c745
RS
905 from 0))
906 (mend (match-end 0)))
907 (and mstart
a3d3fe6e 908 (substring from (1+ mstart) (- mend (if (= mend eos) 0 1))))))
30b7c745
RS
909
910(defun sc-attribs-<>-addresses (from)
911 "Extract the author's email terminus from email address FROM.
912Match addresses of the style ``<name[stuff]>.''"
913 (and (string-match "<\\(.*\\)>" from)
d49b7f88 914 (match-string 1 from)))
30b7c745
RS
915
916(defun sc-get-address (from author)
917 "Get the full email address path from FROM.
918AUTHOR is the author's name (which is removed from the address)."
919 (let ((eos (length from)))
caf3c78f
SM
920 (if (string-match (concat "\\`\"?" (regexp-quote author)
921 "\"?\\s +") from 0)
30b7c745
RS
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))
82db7fdf 927 (if (string-match "[-[:alnum:]!@%._]+" from 0)
d49b7f88 928 (match-string 0 from)
a3d3fe6e 929 ""))))
30b7c745
RS
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.
943EXTEND is the number of characters on each side to extend the
944substring."
945 (and start
946 (let ((sos (+ start extend))
947 (eos (- end extend)))
948 (substring string sos
a3d3fe6e 949 (or (string-match sc-titlecue-regexp string sos) eos)))))
30b7c745
RS
950
951(defun sc-attribs-extract-namestring (from)
952 "Extract the name string from FROM.
953This should be the author's full name minus an optional title."
d49b7f88 954 ;; FIXME: we probably should use mail-extract-address-components.
30b7c745
RS
955 (let ((namestring
956 (or
dcb38be8
RS
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.
f42dc896 960 ;; On the other hand, we do look for quotes in the usual way.
dcb38be8 961 (and (string-match " *<.*>" from 0)
f42dc896
KH
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)))
30b7c745
RS
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
82db7fdf 973 from (string-match "\\([-.[:alnum:]_]+\\s +\\)+<" from 0)
30b7c745
RS
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.
d49b7f88 990example: (sc-attribs-chop-namestring \"John Xavier Doe\")
30b7c745
RS
991 => (\"John\" \"Xavier\" \"Doe\")"
992 (if (string-match "\\([ \t]*\\)\\([^ \t._]+\\)\\([ \t]*\\)" namestring)
d49b7f88 993 (cons (match-string 2 namestring)
a3d3fe6e 994 (sc-attribs-chop-namestring (substring namestring (match-end 3))))))
30b7c745
RS
995
996(defun sc-attribs-strip-initials (namelist)
997 "Extract the author's initials from the NAMELIST."
998 (mapconcat
a3d3fe6e
GM
999 (lambda (name)
1000 (if (< 0 (length name))
1001 (substring name 0 1)))
30b7c745
RS
1002 namelist ""))
1003
1004(defun sc-guess-attribution (&optional string)
1005 "Guess attribution string on current line.
1006If attribution cannot be guessed, nil is returned. Optional STRING if
1007supplied, is used instead of the line point is on in the current buffer."
1008 (let ((start 0)
d49b7f88
SM
1009 (string (or string (buffer-substring (line-beginning-position)
1010 (line-end-position))))
30b7c745
RS
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))
d49b7f88 1016 (setq attribution (match-string 0 string)
30b7c745
RS
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)
804edf03 1028 (mapc
a3d3fe6e
GM
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)))))
30b7c745 1047 namelist)
804edf03 1048 (mapc
a3d3fe6e
GM
1049 (lambda (position)
1050 (setq filtered-list (cons (nth position namelist) filtered-list)))
30b7c745
RS
1051 keepers)
1052 filtered-list))
1053
1054(defun sc-attribs-chop-address (from)
1055 "Extract attribution information from FROM.
1056This populates the `sc-attributions' with the list of possible attributions."
1057 (if (and (stringp from)
1058 (< 0 (length from)))
a1506d29 1059 (let* ((sc-mumble "")
30b7c745
RS
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)
a1506d29 1071
30b7c745
RS
1072 ;; put basic information
1073 (setq
1074 ;; put middle names and build sc-author entry
1075 middlenames (mapconcat
a3d3fe6e
GM
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))
30b7c745
RS
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")
a3d3fe6e
GM
1110 namestring)))
1111 sc-mail-info)))
30b7c745 1112 ;; from string is empty
d49b7f88 1113 (push (cons "sc-author" sc-default-author-name) sc-mail-info)))
30b7c745
RS
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.
1120Only 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
1128Variables 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
1135Runs the hook `sc-attribs-preselect-hook' before selecting an
1136attribution and the hook `sc-attribs-postselect-hook' after making the
1137selection but before querying is performed. During
1138`sc-attribs-postselect-hook' the variable `citation' is bound to the
1139auto-selected citation string and the variable `attribution' is bound
1140to 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)
9c6760b6
GM
1166 (setq attribution (eval attrib))
1167 (if (stringp attribution)
1168 (setq attriblist nil)
1169 (setq attribution nil
1170 attriblist (cdr attriblist))))
30b7c745 1171 (t (error "%s did not evaluate to a string or list!"
a3d3fe6e 1172 "sc-attrib-selection-list")))))
30b7c745
RS
1173 ((setq attribution (cdr (assoc preferred sc-attributions)))
1174 (setq attriblist nil))
1175 (t
a3d3fe6e 1176 (setq attriblist (cdr attriblist))))))
30b7c745
RS
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
a3d3fe6e 1211 (let* ((query-alist (mapcar (lambda (entry) (list (cdr entry)))
30b7c745
RS
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
f67acc8d
RS
1222 (progn
1223 (setq choice
1224 (if sc-attrib-or-cite
d49b7f88 1225 (read-string
f67acc8d
RS
1226 "Enter citation prefix: "
1227 citation
1228 'sc-citation-confirmation-history)
d49b7f88 1229 (completing-read
f67acc8d
RS
1230 "Complete attribution name: "
1231 query-alist nil nil
1232 (cons initial 0)
a3d3fe6e 1233 'sc-attribution-confirmation-history)))
f67acc8d 1234 nil)))
30b7c745
RS
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))
a1506d29 1241
30b7c745 1242 (setq citation (sc-make-citation choice)
a3d3fe6e 1243 attribution choice))))
30b7c745
RS
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)
d49b7f88 1258 (push (cons ckey citation) sc-mail-info))
30b7c745
RS
1259 (if akeyval
1260 (setcdr akeyval attribution)
d49b7f88 1261 (push (cons akey attribution) sc-mail-info)))
30b7c745
RS
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)
a3d3fe6e 1268 (push (cons lkey attribution) sc-attributions)))))
30b7c745
RS
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.
1278The 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,
1281use 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
4703062c
RS
1294(defvar filladapt-prefix-table)
1295
30b7c745
RS
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.
1316Only fill if optional PREFIX is different than `sc-fill-line-prefix'.
1317If `sc-auto-fill-region-p' is nil, do not fill region. If PREFIX is
1318not supplied, initialize fill variables. This is useful for a regi
1319`begin' frame-entry."
1320 (if (not prefix)
1321 (setq sc-fill-line-prefix ""
d49b7f88 1322 sc-fill-begin (line-beginning-position))
30b7c745
RS
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 ""))
d49b7f88 1327 (fill-region sc-fill-begin (line-beginning-position)))
30b7c745 1328 (setq sc-fill-line-prefix prefix
a3d3fe6e 1329 sc-fill-begin (line-beginning-position)))))
30b7c745
RS
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.
1358If nesting cannot be guessed, nil is returned. Optional STRING if
1359supplied, is used instead of the line point is on in the current
1360buffer."
1361 (let ((start 0)
d49b7f88
SM
1362 (string (or string (buffer-substring (line-beginning-position)
1363 (line-end-position))))
30b7c745
RS
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))
d49b7f88 1369 (setq nesting (match-string 0 string)
30b7c745
RS
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 ()
80f16257 1375 "Add a citation level for nested citation style w/ coercion."
30b7c745
RS
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.
1387Optional 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
3fc1b264 1415(defun sc-cite-region (start end &optional confirm-p interactive)
30b7c745
RS
1416 "Cite a region delineated by START and END.
1417If optional CONFIRM-P is non-nil, the attribution is confirmed before
1418its use in the citation string. This function first runs
3fc1b264
RS
1419`sc-pre-cite-hook'.
1420
1421When called interactively, the optional arg INTERACTIVE is non-nil,
1422and that means call `sc-select-attribution' too."
1423 (interactive "r\nP\np")
30b7c745 1424 (undo-boundary)
37d8fcc2 1425 (let ((frame (sc-scan-info-alist sc-cite-frame-alist))
30b7c745 1426 (sc-confirm-always-p (if confirm-p t sc-confirm-always-p)))
37d8fcc2
JL
1427 (if (and frame (symbolp frame))
1428 (setq frame (symbol-value frame)))
1429 (or frame (setq frame sc-default-cite-frame))
30b7c745 1430 (run-hooks 'sc-pre-cite-hook)
3fc1b264 1431 (if interactive
30b7c745
RS
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.
1437First runs `sc-pre-uncite-hook'."
1438 (interactive "r")
1439 (undo-boundary)
37d8fcc2
JL
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))
30b7c745
RS
1444 (run-hooks 'sc-pre-uncite-hook)
1445 (regi-interpret frame start end)))
a1506d29 1446
30b7c745
RS
1447(defun sc-recite-region (start end)
1448 "Recite a region delineated by START and END.
1449First runs `sc-pre-recite-hook'."
1450 (interactive "r")
1451 (let ((sc-confirm-always-p t))
1452 (sc-select-attribution))
1453 (undo-boundary)
37d8fcc2
JL
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))
30b7c745
RS
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.
1467If FIELD is not a string or is the empty string, the empty string will
1468be returned. Optional third argument SEP is concatenated on the end if
1469it is a string. Returns empty string, unless optional RETURN-NIL-P is
1470non-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
88b8d18e
GM
1485(declare-function mh-in-header-p "mh-utils" ())
1486
1487(defun sc-no-blank-line-or-header ()
80f16257 1488 "Similar to `sc-no-header' except it removes the preceding blank line."
88b8d18e
GM
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))))
30b7c745
RS
1501
1502(defun sc-header-on-said ()
1503 "\"On <date>, <from> said:\" unless:
15041. the \"from\" field cannot be found, in which case nothing is inserted;
15052. 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:\"
1515Treats \"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:\"
1525Treats \"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:
1535Treats 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")
a3d3fe6e 1582 " for more details)\n")))))
30b7c745
RS
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'.
1595Otherwise 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.
1604Optional NOMSG, if non-nil, inhibits printing messages, unless an
1605error 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))))
a1506d29 1611 (or nomsg (message "Ref header %d [%d line%s]: %s"
30b7c745
RS
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)"
a3d3fe6e
GM
1618 (cadr err) sc-eref-style)
1619 (beep))))))
30b7c745 1620
365f8d85
SM
1621(defun sc-electric-mode (&optional style)
1622 "Mode for viewing Supercite reference headers. Commands are:
30b7c745
RS
1623\n\\{sc-electric-mode-map}
1624
1625`sc-electric-mode' is not intended to be run interactively, but rather
1626accessed through Supercite's electric reference feature. See
365f8d85 1627`sc-insert-reference' for more details. Optional STYLE is the initial
30b7c745
RS
1628header 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
365f8d85 1634 (or (sc-valid-index-p style)
30b7c745
RS
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)
ca11f4a8 1650 (run-mode-hooks 'sc-electric-mode-hook)
a3d3fe6e 1651 (recursive-edit))))
30b7c745
RS
1652
1653 (and sc-eref-style
1654 (sc-eref-insert-selected))
a3d3fe6e 1655 (kill-buffer sc-electric-bufname)))
30b7c745
RS
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
a3d3fe6e 1672 (progn (error msg "follow") (1- last))))))
937e6a56
SM
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))))))))))
30b7c745
RS
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.
1708If REFNUM is an invalid index, don't go to that reference and return
1709nil."
1710 (interactive "NGoto Reference: ")
1711 (if (sc-valid-index-p refnum)
1712 (sc-eref-show refnum)
1713 (error "Invalid reference: %d. (Range: [%d .. %d])"
a3d3fe6e 1714 refnum 0 (1- (length sc-rewrite-header-list)))))
30b7c745
RS
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.
1734Numeric ARG indicates which header style from `sc-rewrite-header-list'
1735to use when rewriting the header. No supplied ARG indicates use of
1736`sc-preferred-header-style'.
1737
1738With just `\\[universal-argument]', electric reference insert mode is
1739entered, 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)
a3d3fe6e 1750 (sc-eref-insert-selected t)))))
30b7c745
RS
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))
8d96f9de 1762 (force-mode-line-update))
30b7c745
RS
1763
1764(defun sc-toggle-var (variable)
1765 "Boolean toggle VARIABLE's value.
f0529b5b 1766VARIABLE must be a bound symbol. nil values change to t, non-nil
30b7c745
RS
1767values are changed to nil."
1768 (message "%s changed from %s to %s"
1769 variable (symbol-value variable)
d49b7f88 1770 (set variable (not (symbol-value variable)))))
30b7c745
RS
1771
1772(defun sc-set-variable (var)
1773 "Set the Supercite VARIABLE.
1774This function mimics `set-variable', except that the variable to set
1775is determined non-interactively. The value is queried for in the
1776minibuffer exactly the same way that `set-variable' does it.
1777
1778You can see the current value of the variable when the minibuffer is
1779querying you by typing `C-h'. Note that the format is changed
1780slightly from that used by `set-variable' -- the current value is
1781printed just after the variable's name instead of at the bottom of the
1782help window."
d49b7f88 1783 (let* ((minibuffer-help-form '(funcall myhelp))
30b7c745 1784 (myhelp
d49b7f88
SM
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)))))
30b7c745
RS
1802
1803(defmacro sc-toggle-symbol (rootname)
d49b7f88
SM
1804 `(defun ,(intern (concat "sc-T-" rootname)) ()
1805 (interactive)
1806 (sc-toggle-var ',(intern (concat "sc-" rootname "-p")))))
30b7c745
RS
1807
1808(defmacro sc-setvar-symbol (rootname)
d49b7f88
SM
1809 `(defun ,(intern (concat "sc-S-" rootname)) ()
1810 (interactive)
1811 (sc-set-variable ',(intern (concat "sc-" rootname)))))
30b7c745
RS
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
1832Supercite provides a number of key bindings which simplify the process
1833of setting or toggling certain variables controlling its operation.
1834
1835Note 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
30b7c745
RS
1844\f
1845;; ======================================================================
1846;; published interface to mail and news readers
1847
d49b7f88
SM
1848(define-minor-mode sc-minor-mode
1849 "Supercite minor mode."
feb02f95
LK
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)))
d49b7f88 1855
80056f3d 1856;;;###autoload
30b7c745
RS
1857(defun sc-cite-original ()
1858 "Workhorse citing function which performs the initial citation.
1859This is callable from the various mail and news readers' reply
d49b7f88
SM
1860function according to the agreed upon standard. See the associated
1861info node `(SC)Top' for more details.
1862`sc-cite-original' does not do any yanking of the
30b7c745
RS
1863original 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
34b5f6df
GM
1878The region need not be active (and typically isn't when this
1879function is called). Also, the hook `sc-pre-hook' is run before,
1880and `sc-post-hook' is run after the guts of this function."
30b7c745
RS
1881 (run-hooks 'sc-pre-hook)
1882
d49b7f88 1883 (sc-minor-mode 1)
30b7c745
RS
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
d49b7f88 1890 (let* ((zmacs-regions nil) ; for XEemacs
3f66fcdb 1891 (mark-active t) ; for Emacs
30b7c745
RS
1892 (point (point-marker))
1893 (mark (copy-marker (mark-marker))))
a1506d29 1894
30b7c745
RS
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)
a3d3fe6e 1933 (set-marker mark nil))
d49b7f88 1934 (run-hooks 'sc-post-hook))
30b7c745
RS
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.
1942With 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))
d49b7f88 1948 (match-string 0)))
30b7c745
RS
1949 "")))
1950 (goto-char start)
1951 (open-line arg)
1952 (forward-line 1)
1953 (while (< 0 arg)
1954 (insert prefix)
1955 (forward-line 1)
34b5f6df 1956 (setq arg (1- arg))))))
30b7c745
RS
1957
1958(defun sc-insert-citation (arg)
1959 "Insert citation string at beginning of current line if not already cited.
1960With `\\[universal-argument]' insert citation even if line is already
1961cited."
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"))
e8af40ee 1969 (error "Line is already cited"))))
30b7c745 1970
7ca46ac7 1971;; The argument logic here is crazy.
3fc1b264 1972(defun sc-version (message)
7ca46ac7 1973 "Return the current Supercite version.
3fc1b264 1974If MESSAGE is non-nil (interactively, with no prefix argument),
7ca46ac7
GM
1975echoes the version in the minibuffer. Otherwise, inserts the
1976version at point."
241625c3 1977 (interactive (list (not current-prefix-arg)))
1ad042ed 1978 (let ((verstr (format "Using Supercite.el %s" emacs-version)))
3fc1b264
RS
1979 (if message
1980 (message verstr)
1981 (insert "`sc-version' says: " verstr))))
30b7c745 1982
34b5f6df
GM
1983(make-obsolete 'sc-version 'emacs-version "23.1")
1984
30b7c745 1985(defun sc-describe ()
34b5f6df 1986 "Read the Supercite info node."
30b7c745 1987 (interactive)
34b5f6df
GM
1988 (info "(SC)top"))
1989
aadb6864 1990(make-obsolete 'sc-describe "read the SC manual using `info'." "23.1")
30b7c745 1991
a445af6b 1992(define-obsolete-function-alias 'sc-submit-bug-report 'report-emacs-bug "23.1")
30b7c745
RS
1993
1994\f
1995;; useful stuff
1996(provide 'supercite)
1997(run-hooks 'sc-load-hook)
1998
1999;;; supercite.el ends here