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