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