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