1 ;;; supercite.el --- minor mode for citing mail and news replies
3 ;; Copyright (C) 1993, 1997, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: 1993 Barry A. Warsaw <bwarsaw@python.org>
7 ;; Maintainer: Glenn Morris <rgm@gnu.org>
8 ;; Created: February 1993
9 ;; Keywords: mail, news
11 ;; This file is part of GNU Emacs.
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, or (at your option)
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.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
29 ;; supercite|Barry A. Warsaw|supercite-help@python.org
30 ;; |Mail and news reply citation package
31 ;; |1993/09/22 18:58:46|3.1|
39 (require 'sendmail
) ;; For mail-header-end.
41 ;; start user configuration variables
42 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
44 (defgroup supercite nil
50 (defgroup supercite-frames nil
51 "Supercite (regi) frames."
55 (defgroup supercite-attr nil
56 "Supercite attributions."
60 (defgroup supercite-cite nil
65 (defgroup supercite-hooks nil
66 "Hooking into supercite."
70 (defcustom sc-auto-fill-region-p t
71 "*If non-nil, automatically fill each paragraph after it has been cited."
75 (defcustom sc-blank-lines-after-headers
1
76 "*Number of blank lines to leave after mail headers have been nuked.
77 Set to nil, to use whatever blank lines happen to occur naturally."
78 :type
'(choice (const :tag
"leave" nil
)
82 (defcustom sc-citation-leader
" "
83 "*String comprising first part of a citation."
85 :group
'supercite-cite
)
87 (defcustom sc-citation-delimiter
">"
88 "*String comprising third part of a citation.
89 This string is used in both nested and non-nested citations."
91 :group
'supercite-cite
)
93 (defcustom sc-citation-separator
" "
94 "*String comprising fourth and last part of a citation."
96 :group
'supercite-cite
)
98 (defcustom sc-citation-leader-regexp
"[ \t]*"
99 "*Regexp describing citation leader for a cited line.
100 This should NOT have a leading `^' character."
102 :group
'supercite-cite
)
104 ;; Nemacs and Mule users note: please see the texinfo manual for
105 ;; suggestions on setting these variables.
106 (defcustom sc-citation-root-regexp
"[-._[:alnum:]]*"
107 "*Regexp describing variable root part of a citation for a cited line.
108 This should NOT have a leading `^' character. See also
109 `sc-citation-nonnested-root-regexp'."
111 :group
'supercite-cite
)
113 (defcustom sc-citation-nonnested-root-regexp
"[-._[:alnum:]]+"
114 "*Regexp describing the variable root part of a nested citation.
115 This should NOT have a leading `^' character. This variable is
116 related to `sc-citation-root-regexp' but whereas that variable
117 describes both nested and non-nested citation roots, this variable
118 describes only nested citation roots."
120 :group
'supercite-cite
)
122 (defcustom sc-citation-delimiter-regexp
"[>]+"
123 "*Regexp describing citation delimiter for a cited line.
124 This should NOT have a leading `^' character."
126 :group
'supercite-cite
)
128 (defcustom sc-citation-separator-regexp
"[ \t]*"
129 "*Regexp describing citation separator for a cited line.
130 This should NOT have a leading `^' character."
132 :group
'supercite-cite
)
134 (defcustom sc-cite-blank-lines-p nil
135 "*If non-nil, put a citation on blank lines."
137 :group
'supercite-cite
)
139 (defcustom sc-cite-frame-alist
'()
140 "*Alist for frame selection during citing.
141 Each element of this list has the following form:
142 (INFOKEY ((REGEXP . FRAME)
146 Where INFOKEY is a key for `sc-mail-field', REGEXP is a regular
147 expression to match against the INFOKEY's value. FRAME is
148 a citation frame, or a symbol that represents the name of
149 a variable whose value is a citation frame."
150 :type
'(repeat (list symbol
(repeat (cons regexp
151 (choice (repeat (repeat sexp
))
153 :group
'supercite-frames
)
154 (put 'sc-cite-frame-alist
'risky-local-variable t
)
156 (defcustom sc-uncite-frame-alist
'()
157 "*Alist for frame selection during unciting.
158 See the variable `sc-cite-frame-alist' for details."
159 :type
'(repeat (list symbol
(repeat (cons regexp
160 (choice (repeat (repeat sexp
))
162 :group
'supercite-frames
)
163 (put 'sc-uncite-frame-alist
'risky-local-variable t
)
165 (defcustom sc-recite-frame-alist
'()
166 "*Alist for frame selection during reciting.
167 See the variable `sc-cite-frame-alist' for details."
168 :type
'(repeat (list symbol
(repeat (cons regexp
169 (choice (repeat (repeat sexp
))
171 :group
'supercite-frames
)
172 (put 'sc-recite-frame-alist
'risky-local-variable t
)
174 (defcustom sc-default-cite-frame
175 '(;; initialize fill state and temporary variables when entering
176 ;; frame. this makes things run much faster
178 (sc-fill-if-different)
179 (setq sc-tmp-nested-regexp
(sc-cite-regexp "")
180 sc-tmp-nonnested-regexp
(sc-cite-regexp)
185 (sc-cite-regexp sc-citation-nonnested-root-regexp
))
187 ;; blank lines mean paragraph separators, so fill the last cited
188 ;; paragraph, unless sc-cite-blank-lines-p is non-nil, in which
189 ;; case we treat blank lines just like any other line.
190 ("^[ \t]*$" (if sc-cite-blank-lines-p
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
"")
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
212 ;; be sure when we're done that we fill the last cited paragraph.
213 (end (sc-fill-if-different ""))
215 "*Default REGI frame for citing a region."
216 :type
'(repeat (repeat sexp
))
217 :group
'supercite-frames
)
218 (put 'sc-default-cite-frame
'risky-local-variable t
)
220 (defcustom sc-default-uncite-frame
221 '(;; do nothing on a blank line
223 ;; if the line is cited, uncite it
224 ((sc-cite-regexp) (sc-uncite-line))
226 "*Default REGI frame for unciting a region."
227 :type
'(repeat (repeat sexp
))
228 :group
'supercite-frames
)
229 (put 'sc-default-uncite-frame
'risky-local-variable t
)
231 (defcustom sc-default-recite-frame
232 '(;; initialize fill state when entering frame
233 (begin (sc-fill-if-different))
234 ;; do nothing on a blank line
236 ;; if we're looking at a cited line, recite it
237 ((sc-cite-regexp) (sc-recite-line (sc-cite-regexp)))
238 ;; otherwise, the line is uncited, so just cite it
240 ;; be sure when we're done that we fill the last cited paragraph.
241 (end (sc-fill-if-different ""))
243 "*Default REGI frame for reciting a region."
244 :type
'(repeat (repeat sexp
))
245 :group
'supercite-frames
)
246 (put 'sc-default-recite-frame
'risky-local-variable t
)
248 (defcustom sc-cite-region-limit t
249 "*This variable controls automatic citation of yanked text.
252 non-nil -- cite the entire region, regardless of its size
253 nil -- do not cite the region at all
254 <integer> -- a number indicating the threshold for citation. When
255 the number of lines in the region is greater than this
256 value, a warning message will be printed and the region
257 will not be cited. Lines in region are counted with
260 The gathering of attribution information is not affected by the value
261 of this variable. The number of lines in the region is calculated
262 *after* all mail headers are removed. This variable is only consulted
263 during the initial citing via `sc-cite-original'."
264 :type
'(choice (const :tag
"do not cite" nil
)
265 (integer :tag
"citation threshold")
266 (other :tag
"always cite" t
))
267 :group
'supercite-cite
)
269 (defcustom sc-confirm-always-p t
270 "*If non-nil, always confirm attribution string before citing text body."
272 :group
'supercite-attr
)
274 (defcustom sc-default-attribution
"Anon"
275 "*String used when author's attribution cannot be determined."
277 :group
'supercite-attr
)
278 (defcustom sc-default-author-name
"Anonymous"
279 "*String used when author's name cannot be determined."
281 :group
'supercite-attr
)
282 (defcustom sc-downcase-p nil
283 "*Non-nil means downcase the attribution and citation strings."
285 :group
'supercite-attr
286 :group
'supercite-cite
)
287 (defcustom sc-electric-circular-p t
288 "*If non-nil, treat electric references as circular."
290 :group
'supercite-attr
)
292 (defcustom sc-electric-mode-hook nil
293 "*Hook for `sc-electric-mode' electric references mode."
295 :group
'supercite-hooks
)
296 (defcustom sc-electric-references-p nil
297 "*Use electric references if non-nil."
301 (defcustom sc-fixup-whitespace-p nil
302 "*If non-nil, delete all leading white space before citing."
306 (defcustom sc-load-hook nil
307 "*Hook which gets run once after Supercite loads."
309 :group
'supercite-hooks
)
310 (defcustom sc-pre-hook nil
311 "*Hook which gets run before each invocation of `sc-cite-original'."
313 :group
'supercite-hooks
)
314 (defcustom sc-post-hook nil
315 "*Hook which gets run after each invocation of `sc-cite-original'."
317 :group
'supercite-hooks
)
319 (defcustom sc-mail-warn-if-non-rfc822-p t
320 "*Warn if mail headers don't conform to RFC822."
322 :group
'supercite-attr
)
323 (defcustom sc-mumble
""
324 "*Value returned by `sc-mail-field' if field isn't in mail headers."
326 :group
'supercite-attr
)
328 (defcustom sc-name-filter-alist
329 '(("^\\(Mr\\|Mrs\\|Ms\\|Dr\\)[.]?$" .
0)
330 ("^\\(Jr\\|Sr\\)[.]?$" . last
)
333 "*Name list components which are filtered out as noise.
334 This variable contains an association list where each element is of
335 the form: (REGEXP . POSITION).
337 REGEXP is a regular expression which matches the name list component.
338 Match is performed using `string-match'. POSITION is the position in
339 the name list which can match the regular expression, starting at zero
340 for the first element. Use `last' to match the last element in the
341 list and `any' to match all elements."
342 :type
'(repeat (cons regexp
(choice (const last
) (const any
)
343 (integer :tag
"position"))))
344 :group
'supercite-attr
)
346 (defcustom sc-nested-citation-p nil
347 "*Controls whether to use nested or non-nested citation style.
348 Non-nil uses nested citations, nil uses non-nested citations."
352 (defcustom sc-nuke-mail-headers
'all
353 "*Controls mail header nuking.
354 Used in conjunction with `sc-nuke-mail-header-list'. Valid values are:
356 `all' -- nuke all mail headers
357 `none' -- don't nuke any mail headers
358 `specified' -- nuke headers specified in `sc-nuke-mail-header-list'
359 `keep' -- keep headers specified in `sc-nuke-mail-header-list'"
360 :type
'(choice (const all
) (const none
)
361 (const specified
) (const keep
))
364 (defcustom sc-nuke-mail-header-list nil
365 "*List of mail header regexps to remove or keep in body of reply.
366 This list contains regular expressions describing the mail headers to
367 keep or nuke, depending on the value of `sc-nuke-mail-headers'."
368 :type
'(repeat regexp
)
371 (defcustom sc-preferred-attribution-list
372 '("sc-lastchoice" "x-attribution" "firstname" "initials" "lastname")
373 "*Specifies what to use as the attribution string.
374 Supercite creates a list of possible attributions when it scans the
375 mail headers from the original message. Each attribution choice is
376 associated with a key in an attribution alist. Supercite tries to
377 pick a \"preferred\" attribution by matching the attribution alist
378 keys against the elements in `sc-preferred-attribution-list' in order.
379 The first non-empty string value found is used as the preferred
382 Note that Supercite now honors the X-Attribution: mail field. If
383 present in the original message, the value of this field should always
384 be used to select the most preferred attribution since it reflects how
385 the original author would like to be distinguished. It should be
386 considered bad taste to put any attribution preference key before
387 \"x-attribution\" in this list, except perhaps for \"sc-lastchoice\"
390 Supercite remembers the last attribution used when reciting an already
391 cited paragraph. This attribution will always be saved with the
392 \"sc-lastchoice\" key, which can be used in this list. Note that the
393 last choice is always reset after every call of `sc-cite-original'.
395 Barring error conditions, the following preferences are always present
396 in the attribution alist:
398 \"emailname\" -- email terminus name
399 \"initials\" -- initials of author
400 \"firstname\" -- first name of author
401 \"lastname\" -- last name of author
402 \"middlename-1\" -- first middle name of author
403 \"middlename-2\" -- second middle name of author
406 Middle name indexes can be any positive integer greater than 0,
407 although it is unlikely that many authors will supply more than one
408 middle name, if that many. The string of all middle names is
409 associated with the key \"middlenames\"."
410 :type
'(repeat string
)
411 :group
'supercite-attr
)
413 (defcustom sc-attrib-selection-list nil
414 "*An alist for selecting preferred attribution based on mail headers.
415 Each element of this list has the following form:
417 (INFOKEY ((REGEXP . ATTRIBUTION)
418 (REGEXP . ATTRIBUTION)
421 Where INFOKEY is a key for `sc-mail-field', REGEXP is a regular
422 expression to match against the INFOKEY's value. ATTRIBUTION can be a
423 string or a list. If it's a string, then it is the attribution that is
424 selected by `sc-select-attribution'. If it is a list, it is `eval'd
425 and the return value must be a string, which is used as the selected
426 attribution. Note that the variable `sc-preferred-attribution-list'
427 must contain an element of the string \"sc-consult\" for this variable
428 to be consulted during attribution selection."
429 :type
'(repeat (list string
431 (choice (sexp :tag
"List to eval")
433 :group
'supercite-attr
)
434 (put 'sc-attrib-selection-list
'risky-local-variable t
)
436 (defcustom sc-attribs-preselect-hook nil
437 "*Hook to run before selecting an attribution."
439 :group
'supercite-attr
440 :group
'supercite-hooks
)
441 (defcustom sc-attribs-postselect-hook nil
442 "*Hook to run after selecting an attribution, but before confirmation."
444 :group
'supercite-attr
445 :group
'supercite-hooks
)
447 (defcustom sc-pre-cite-hook nil
448 "*Hook to run before citing a region of text."
450 :group
'supercite-cite
451 :group
'supercite-hooks
)
452 (defcustom sc-pre-uncite-hook nil
453 "*Hook to run before unciting a region of text."
455 :group
'supercite-cite
456 :group
'supercite-hooks
)
457 (defcustom sc-pre-recite-hook nil
458 "*Hook to run before reciting a region of text."
460 :group
'supercite-cite
461 :group
'supercite-hooks
)
463 (defcustom sc-preferred-header-style
4
464 "*Index into `sc-rewrite-header-list' specifying preferred header style.
465 Index zero accesses the first function in the list."
469 (defcustom sc-reference-tag-string
">>>>> "
470 "*String used at the beginning of built-in reference headers."
474 (defcustom sc-rewrite-header-list
477 (sc-header-inarticle-writes)
478 (sc-header-regarding-adds)
479 (sc-header-attributed-writes)
480 (sc-header-author-writes)
482 (sc-no-blank-line-or-header)
484 "*List of reference header rewrite functions.
485 The variable `sc-preferred-header-style' controls which function in
486 this list is chosen for automatic reference header insertions.
487 Electric reference mode will cycle through this list of functions."
490 (put 'sc-rewrite-header-list
'risky-local-variable t
)
492 (defcustom sc-titlecue-regexp
"\\s +-+\\s +"
493 "*Regular expression describing the separator between names and titles.
494 Set to nil to treat entire field as a name."
495 :type
'(choice (const :tag
"entire field as name" nil
)
497 :group
'supercite-attr
)
499 (defcustom sc-use-only-preference-p nil
500 "*Controls what happens when the preferred attribution cannot be found.
501 If non-nil, then `sc-default-attribution' will be used. If nil, then
502 some secondary scheme will be employed to find a suitable attribution
505 :group
'supercite-attr
)
507 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
508 ;; end user configuration variables
510 (define-obsolete-variable-alias 'sc-version
'emacs-version
"23.1")
512 (defvar sc-mail-info nil
513 "Alist of mail header information gleaned from reply buffer.")
514 (defvar sc-attributions nil
515 "Alist of attributions for use when citing.")
517 (defvar sc-tmp-nested-regexp nil
518 "Temporary regexp describing nested citations.")
519 (defvar sc-tmp-nonnested-regexp nil
520 "Temporary regexp describing non-nested citations.")
521 (defvar sc-tmp-dumb-regexp nil
522 "Temp regexp describing non-nested citation cited with a nesting citer.")
524 (make-variable-buffer-local 'sc-mail-info
)
525 (make-variable-buffer-local 'sc-attributions
)
528 ;; ======================================================================
531 (defvar sc-mode-map-prefix
"\C-c\C-p"
532 "*Key binding to install Supercite keymap.
533 If this is nil, Supercite keymap is not installed.")
536 (let ((map (make-sparse-keymap)))
537 (define-key map
"a" 'sc-S-preferred-attribution-list
)
538 (define-key map
"b" 'sc-T-mail-nuke-blank-lines
)
539 (define-key map
"c" 'sc-T-confirm-always
)
540 (define-key map
"d" 'sc-T-downcase
)
541 (define-key map
"e" 'sc-T-electric-references
)
542 (define-key map
"f" 'sc-T-auto-fill-region
)
543 (define-key map
"h" 'sc-T-describe
)
544 (define-key map
"l" 'sc-S-cite-region-limit
)
545 (define-key map
"n" 'sc-S-mail-nuke-mail-headers
)
546 (define-key map
"N" 'sc-S-mail-header-nuke-list
)
547 (define-key map
"o" 'sc-T-electric-circular
)
548 (define-key map
"p" 'sc-S-preferred-header-style
)
549 (define-key map
"s" 'sc-T-nested-citation
)
550 (define-key map
"u" 'sc-T-use-only-preferences
)
551 (define-key map
"w" 'sc-T-fixup-whitespace
)
552 (define-key map
"?" 'sc-T-describe
)
554 "Keymap for sub-keymap of setting and toggling functions.")
557 (let ((map (make-sparse-keymap)))
558 (define-key map
"c" 'sc-cite-region
)
559 (define-key map
"f" 'sc-mail-field-query
)
560 (define-key map
"g" 'sc-mail-process-headers
)
561 (define-key map
"h" 'sc-describe
)
562 (define-key map
"i" 'sc-insert-citation
)
563 (define-key map
"o" 'sc-open-line
)
564 (define-key map
"r" 'sc-recite-region
)
565 (define-key map
"\C-p" 'sc-raw-mode-toggle
)
566 (define-key map
"u" 'sc-uncite-region
)
567 (define-key map
"v" 'sc-version
)
568 (define-key map
"w" 'sc-insert-reference
)
569 (define-key map
"\C-t" sc-T-keymap
)
570 (define-key map
"\C-b" 'sc-submit-bug-report
)
571 (define-key map
"?" 'sc-describe
)
573 "Keymap for Supercite quasi-mode.")
575 (defvar sc-electric-mode-map
576 (let ((map (make-sparse-keymap)))
577 (define-key map
"p" 'sc-eref-prev
)
578 (define-key map
"n" 'sc-eref-next
)
579 (define-key map
"s" 'sc-eref-setn
)
580 (define-key map
"j" 'sc-eref-jump
)
581 (define-key map
"x" 'sc-eref-abort
)
582 (define-key map
"q" 'sc-eref-abort
)
583 (define-key map
"\r" 'sc-eref-exit
)
584 (define-key map
"\n" 'sc-eref-exit
)
585 (define-key map
"g" 'sc-eref-goto
)
586 (define-key map
"?" 'describe-mode
)
587 (define-key map
"\C-h" 'describe-mode
)
588 (define-key map
[f1] 'describe-mode)
589 (define-key map [help] 'describe-mode)
591 "Keymap for `sc-electric-mode' electric references mode.")
594 (defvar sc-minibuffer-local-completion-map
595 (let ((map (copy-keymap minibuffer-local-completion-map)))
596 (define-key map "\C-t" 'sc-toggle-fn)
597 (define-key map " " 'self-insert-command)
599 "Keymap for minibuffer confirmation of attribution strings.")
601 (defvar sc-minibuffer-local-map
602 (let ((map (copy-keymap minibuffer-local-map)))
603 (define-key map "\C-t" 'sc-toggle-fn)
605 "Keymap for minibuffer confirmation of attribution strings.")
608 ;; ======================================================================
611 (defun sc-ask (alist)
612 "Ask a question in the minibuffer requiring a single character answer.
613 This function is kind of an extension of `y-or-n-p' where a single
614 letter is used to answer a question. Question is formed from ALIST
615 which has members of the form: (WORD . LETTER). WORD is the long
616 word form, while LETTER is the letter for selecting that answer. The
617 selected letter is returned, or nil if the question was not answered.
618 Note that WORD is a string and LETTER is a character. All LETTERs in
619 the list should be unique."
620 (let* ((prompt (concat
621 (mapconcat (function (lambda (elt) (car elt))) alist ", ")
625 (lambda (elt) (char-to-string (cdr elt)))) alist "/")
629 (if (fboundp 'allocate-event)
633 (if (let ((cursor-in-echo-area t)
636 (setq event (read-event))
637 (prog1 quit-flag (setq quit-flag nil)))
639 (message "%s%s" p (single-key-description event))
640 (if (fboundp 'deallocate-event)
641 (deallocate-event event))
645 (if (featurep 'xemacs)
646 (let* ((key (and (key-press-event-p event) (event-key event)))
647 (char (and key (event-to-character event))))
651 (if char (setq char (downcase char)))
653 ((setq elt (rassq char alist))
654 (message "%s%s" p (car elt))
656 ((if (fboundp 'button-release-event-p)
657 (button-release-event-p event)) ; ignore them
660 (message "%s%s" p (single-key-description event))
661 (if (featurep 'xemacs)
666 (setq p (concat "Try again. " prompt)))))))
667 (if (fboundp 'deallocate-event)
668 (deallocate-event event))
671 (defun sc-scan-info-alist (alist)
672 "Find a match in the info alist that matches a regexp in ALIST."
676 (let* ((elem (car alist))
678 (infoval (sc-mail-field infokey))
679 (mlist (car (cdr elem))))
681 (let* ((ml-elem (car mlist))
682 (regexp (car ml-elem))
683 (thing (cdr ml-elem)))
684 (if (string-match regexp infoval)
685 ;; we found a match, time to return
689 ;; else we didn't find a match
690 (setq mlist (cdr mlist))
691 ))) ;end of mlist loop
692 (setq alist (cdr alist))
693 )) ;end of alist loop
697 ;; ======================================================================
698 ;; extract mail field information from headers in reply buffer
700 ;; holder variables for bc happiness
701 (defvar sc-mail-headers-start nil
702 "Start of header fields.")
703 (defvar sc-mail-headers-end nil
704 "End of header fields.")
705 (defvar sc-mail-field-history nil
706 "For minibuffer completion on mail field queries.")
707 (defvar sc-mail-field-modification-history nil
708 "For minibuffer completion on mail field modifications.")
709 (defvar sc-mail-glom-frame
710 '((begin (setq sc-mail-headers-start (point)))
711 ("^x-attribution:[ \t]+.*$" (sc-mail-fetch-field t) nil t)
712 ("^\\S +:.*$" (sc-mail-fetch-field) nil t)
713 ("^$" (list 'abort '(step . 0)))
714 ("^[ \t]+" (sc-mail-append-field))
715 (sc-mail-warn-if-non-rfc822-p (sc-mail-error-in-mail-field))
716 (end (setq sc-mail-headers-end (point))))
717 "Regi frame for glomming mail header information.")
718 (put 'sc-mail-glom-frame 'risky-local-variable t)
720 (defvar curline) ; dynamic bondage
723 (defun sc-mail-fetch-field (&optional attribs-p)
724 "Insert a key and value into `sc-mail-info' alist.
725 If optional ATTRIBS-P is non-nil, the key/value pair is placed in
726 `sc-attributions' too."
727 (if (string-match "^\\(\\S *\\)\\s *:\\s +\\(.*\\)$" curline)
728 (let* ((key (downcase (match-string-no-properties 1 curline)))
729 (val (match-string-no-properties 2 curline))
730 (keyval (cons key val)))
731 (push keyval sc-mail-info)
733 (push keyval sc-attributions))))
736 (defun sc-mail-append-field ()
737 "Append a continuation line onto the last fetched mail field's info."
738 (let ((keyval (car sc-mail-info)))
739 (if (and keyval (string-match "^\\s *\\(.*\\)$" curline))
740 (setcdr keyval (concat (cdr keyval) " "
741 (match-string-no-properties 1 curline)))))
744 (defun sc-mail-error-in-mail-field ()
745 "Issue warning that mail headers don't conform to RFC 822."
746 (let* ((len (min (length curline) 10))
747 (ellipsis (if (< len (length curline)) "..." ""))
748 (msg "Mail header \"%s%s\" doesn't conform to RFC 822. skipping..."))
749 (message msg (substring curline 0 len) ellipsis))
754 ;; mail header nuking
755 (defvar sc-mail-last-header-nuked-p nil
756 "True if the last header was nuked.")
758 (defun sc-mail-nuke-line ()
759 "Nuke the current mail header line."
760 (delete-region (line-beginning-position) (line-beginning-position 2))
763 (defun sc-mail-nuke-header-line ()
764 "Delete current-line and set up for possible continuation."
765 (setq sc-mail-last-header-nuked-p t)
768 (defun sc-mail-nuke-continuation-line ()
769 "Delete a continuation line if the last header line was deleted."
770 (if sc-mail-last-header-nuked-p
771 (sc-mail-nuke-line)))
773 (defun sc-mail-cleanup-blank-lines ()
774 "Leave some blank lines after original mail headers are nuked.
775 The number of lines left is specified by `sc-blank-lines-after-headers'."
776 (if sc-blank-lines-after-headers
779 (skip-chars-backward " \t\n")
783 (if (looking-at "[ \t]*$")
784 (delete-region (line-beginning-position)
785 (line-beginning-position 2)))
786 (insert-char ?\n sc-blank-lines-after-headers)))
789 (defun sc-mail-build-nuke-frame ()
790 "Build the regiframe for nuking mail headers."
791 (let (every-func entry-func nonentry-func)
793 ((eq sc-nuke-mail-headers 'all)
794 (setq every-func '(progn (forward-line -1) (sc-mail-nuke-line))))
795 ((eq sc-nuke-mail-headers 'specified)
796 (setq entry-func '(sc-mail-nuke-header-line)
797 nonentry-func '(setq sc-mail-last-header-nuked-p nil)))
798 ((eq sc-nuke-mail-headers 'keep)
799 (setq entry-func '(setq sc-mail-last-header-nuked-p nil)
800 nonentry-func '(sc-mail-nuke-header-line)))
801 ;; we never get far enough to interpret a frame if s-n-m-h == 'none
802 ((eq sc-nuke-mail-headers 'none))
803 (t (error "Invalid value for sc-nuke-mail-headers: %s"
804 sc-nuke-mail-headers))
808 (regi-mapcar sc-nuke-mail-header-list entry-func nil t))
809 (and nonentry-func (list (list "^\\S +:.*$" nonentry-func)))
810 (and (not every-func)
811 '(("^[ \t]+" (sc-mail-nuke-continuation-line))))
812 '((begin (setq sc-mail-last-header-zapped-p nil)))
813 '((end (sc-mail-cleanup-blank-lines)))
814 (and every-func (list (list 'every every-func)))
817 ;; mail processing and zapping. this is the top level entry defun to
818 ;; all header processing.
819 (defun sc-mail-process-headers (start end)
820 "Process original mail message's mail headers.
821 After processing, mail headers may be nuked. Header information is
822 stored in `sc-mail-info', and any old information is lost unless an
825 (let ((info (copy-alist sc-mail-info))
826 (attribs (copy-alist sc-attributions)))
827 (setq sc-mail-info nil
829 (regi-interpret sc-mail-glom-frame start end)
830 (if (null sc-mail-info)
832 (message "No mail headers found! Restoring old information.")
833 (setq sc-mail-info info
834 sc-attributions attribs))
835 (regi-interpret (sc-mail-build-nuke-frame)
836 sc-mail-headers-start sc-mail-headers-end)
840 ;; let the user change mail field information
841 (defun sc-mail-field (field)
842 "Return the mail header field value associated with FIELD.
843 If there was no mail header with FIELD as its key, return the value of
844 `sc-mumble'. FIELD is case insensitive."
845 (or (cdr (assoc-string field sc-mail-info 'case-fold)) sc-mumble))
847 (defun sc-mail-field-query (arg)
848 "View the value of a mail field.
849 With `\\[universal-argument]', prompts for action on mail field.
850 Action can be one of: View, Modify, Add, or Delete."
852 (let* ((alist '(("view" . ?v) ("modify" . ?m) ("add" . ?a) ("delete" . ?d)))
853 (action (if (not arg) ?v (sc-ask alist)))
857 (setq key (completing-read
858 (concat (car (rassq action alist))
859 " information key: ")
861 (if (eq action ?a) nil 'noexit)
862 nil 'sc-mail-field-history))
865 (message "%s: %s" key (cdr (assoc key sc-mail-info))))
867 (setq sc-mail-info (delq (assoc key sc-mail-info) sc-mail-info)))
869 (let ((keyval (assoc key sc-mail-info)))
870 ;; first put initial value onto list if not already there
871 (if (not (member (cdr keyval)
872 sc-mail-field-modification-history))
873 (setq sc-mail-field-modification-history
874 (cons (cdr keyval) sc-mail-field-modification-history)))
875 (setcdr keyval (read-string
876 (concat key ": ") (cdr keyval)
877 'sc-mail-field-modification-history))))
879 (push (cons key (read-string (concat key ": "))) sc-mail-info))
883 ;; ======================================================================
886 (defvar sc-attribution-confirmation-history nil
887 "History for confirmation of attribution strings.")
888 (defvar sc-citation-confirmation-history nil
889 "History for confirmation of attribution prefixes.")
891 (defun sc-attribs-%@-addresses (from &optional delim)
892 "Extract the author's email terminus from email address FROM.
893 Match addresses of the style ``name%[stuff].'' when called with DELIM
894 of \"%\" and addresses of the style ``[stuff]name@[stuff]'' when
895 called with DELIM \"@\". If DELIM is nil or not provided, matches
896 addresses of the style ``name''."
897 (and (string-match (concat "[-[:alnum:]_.]+" delim) from 0)
900 (- (match-end 0) (if (null delim) 0 1)))))
902 (defun sc-attribs-!-addresses (from)
903 "Extract the author's email terminus from email address FROM.
904 Match addresses of the style ``[stuff]![stuff]...!name[stuff].''"
905 (let ((eos (length from))
906 (mstart (string-match "![-[:alnum:]_.]+\\([^-![:alnum:]_.]\\|$\\)"
908 (mend (match-end 0)))
910 (substring from (1+ mstart) (- mend (if (= mend eos) 0 1)))
913 (defun sc-attribs-<>-addresses (from)
914 "Extract the author's email terminus from email address FROM.
915 Match addresses of the style ``<name[stuff]>.''"
916 (and (string-match "<\\(.*\\)>" from)
917 (match-string 1 from)))
919 (defun sc-get-address (from author)
920 "Get the full email address path from FROM.
921 AUTHOR is the author's name (which is removed from the address)."
922 (let ((eos (length from)))
923 (if (string-match (concat "\\`\"?" (regexp-quote author)
925 (let ((address (substring from (match-end 0) eos)))
926 (if (and (= (aref address 0) ?<)
927 (= (aref address (1- (length address))) ?>))
928 (substring address 1 (1- (length address)))
930 (if (string-match "[-[:alnum:]!@%._]+" from 0)
931 (match-string 0 from)
935 (defun sc-attribs-emailname (from)
936 "Get the email terminus name from FROM."
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)))
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
950 (let ((sos (+ start extend))
951 (eos (- end extend)))
952 (substring string sos
953 (or (string-match sc-titlecue-regexp string sos) eos)
956 (defun sc-attribs-extract-namestring (from)
957 "Extract the name string from FROM.
958 This should be the author's full name minus an optional title."
959 ;; FIXME: we probably should use mail-extract-address-components.
962 ;; If there is a <...> in the name,
963 ;; treat everything before that as the full name.
964 ;; Even if it contains parens, use the whole thing.
965 ;; On the other hand, we do look for quotes in the usual way.
966 (and (string-match " *<.*>" from 0)
968 (sc-name-substring from 0 (match-beginning 0) 0)))
969 (if (string-match "\".*\"" before-angles 0)
971 before-angles (match-beginning 0) (match-end 0) 1)
974 from (string-match "(.*)" from 0) (match-end 0) 1)
976 from (string-match "\".*\"" from 0) (match-end 0) 1)
978 from (string-match "\\([-.[:alnum:]_]+\\s +\\)+<" from 0)
980 (sc-attribs-emailname from))))
981 ;; strip off any leading or trailing whitespace
984 (eos (1- (length namestring))))
985 (while (and (<= bos eos)
986 (memq (aref namestring bos) '(32 ?\t)))
988 (while (and (> eos bos)
989 (memq (aref namestring eos) '(32 ?\t)))
991 (substring namestring bos (1+ eos))))))
993 (defun sc-attribs-chop-namestring (namestring)
994 "Convert NAMESTRING to a list of names.
995 example: (sc-attribs-chop-namestring \"John Xavier Doe\")
996 => (\"John\" \"Xavier\" \"Doe\")"
997 (if (string-match "\\([ \t]*\\)\\([^ \t._]+\\)\\([ \t]*\\)" namestring)
998 (cons (match-string 2 namestring)
999 (sc-attribs-chop-namestring (substring namestring (match-end 3)))
1002 (defun sc-attribs-strip-initials (namelist)
1003 "Extract the author's initials from the NAMELIST."
1007 (if (< 0 (length name))
1008 (substring name 0 1))))
1011 (defun sc-guess-attribution (&optional string)
1012 "Guess attribution string on current line.
1013 If attribution cannot be guessed, nil is returned. Optional STRING if
1014 supplied, is used instead of the line point is on in the current buffer."
1016 (string (or string (buffer-substring (line-beginning-position)
1017 (line-end-position))))
1020 (= start (or (string-match sc-citation-leader-regexp string start) -1))
1021 (setq start (match-end 0))
1022 (= start (or (string-match sc-citation-root-regexp string start) 1))
1023 (setq attribution (match-string 0 string)
1024 start (match-end 0))
1025 (= start (or (string-match sc-citation-delimiter-regexp string start) -1))
1026 (setq start (match-end 0))
1027 (= start (or (string-match sc-citation-separator-regexp string start) -1))
1030 (defun sc-attribs-filter-namelist (namelist)
1031 "Filter out noise in NAMELIST according to `sc-name-filter-alist'."
1032 (let ((elements (length namelist))
1034 keepers filtered-list)
1038 (setq position (1+ position))
1043 (let ((regexp (car filter))
1045 (if (and (string-match regexp name)
1046 (or (and (numberp pos)
1049 (= position (1- elements)))
1053 sc-name-filter-alist)
1055 (setq keepers (cons position keepers)))
1061 (setq filtered-list (cons (nth position namelist) filtered-list))
1066 (defun sc-attribs-chop-address (from)
1067 "Extract attribution information from FROM.
1068 This populates the `sc-attributions' with the list of possible attributions."
1069 (if (and (stringp from)
1070 (< 0 (length from)))
1071 (let* ((sc-mumble "")
1072 (namestring (sc-attribs-extract-namestring from))
1073 (namelist (sc-attribs-filter-namelist
1074 (sc-attribs-chop-namestring namestring)))
1075 (revnames (reverse (cdr namelist)))
1076 (firstname (car namelist))
1077 (midnames (reverse (cdr revnames)))
1078 (lastname (car revnames))
1079 (initials (sc-attribs-strip-initials namelist))
1080 (emailname (sc-attribs-emailname from))
1084 ;; put basic information
1086 ;; put middle names and build sc-author entry
1087 middlenames (mapconcat
1090 (let ((key-attribs (format "middlename-%d" n))
1091 (key-mail (format "sc-middlename-%d" n)))
1092 (push (cons key-attribs midname) sc-attributions)
1093 (push (cons key-mail midname) sc-mail-info)
1098 author (concat firstname " " middlenames (and midnames " ") lastname)
1100 sc-attributions (append
1102 (cons "firstname" firstname)
1103 (cons "lastname" lastname)
1104 (cons "emailname" emailname)
1105 (cons "initials" initials))
1107 sc-mail-info (append
1109 (cons "sc-firstname" firstname)
1110 (cons "sc-middlenames" middlenames)
1111 (cons "sc-lastname" lastname)
1112 (cons "sc-emailname" emailname)
1113 (cons "sc-initials" initials)
1114 (cons "sc-author" author)
1115 (cons "sc-from-address" (sc-get-address
1116 (sc-mail-field "from")
1118 (cons "sc-reply-address" (sc-get-address
1119 (sc-mail-field "reply-to")
1121 (cons "sc-sender-address" (sc-get-address
1122 (sc-mail-field "sender")
1127 ;; from string is empty
1128 (push (cons "sc-author" sc-default-author-name) sc-mail-info)))
1130 (defvar sc-attrib-or-cite nil
1131 "Used to toggle between attribution input or citation input.")
1133 (defun sc-toggle-fn ()
1134 "Toggle between attribution selection and citation selection.
1135 Only used during confirmation."
1137 (setq sc-attrib-or-cite (not sc-attrib-or-cite))
1138 (throw 'sc-reconfirm t))
1140 (defun sc-select-attribution ()
1141 "Select an attribution from `sc-attributions'.
1143 Variables involved in selection process include:
1144 `sc-preferred-attribution-list'
1145 `sc-use-only-preference-p'
1146 `sc-confirm-always-p'
1147 `sc-default-attribution'
1148 `sc-attrib-selection-list'.
1150 Runs the hook `sc-attribs-preselect-hook' before selecting an
1151 attribution and the hook `sc-attribs-postselect-hook' after making the
1152 selection but before querying is performed. During
1153 `sc-attribs-postselect-hook' the variable `citation' is bound to the
1154 auto-selected citation string and the variable `attribution' is bound
1155 to the auto-selected attribution string."
1156 (run-hooks 'sc-attribs-preselect-hook)
1157 (let ((query-p sc-confirm-always-p)
1158 attribution citation
1159 (attriblist sc-preferred-attribution-list))
1161 ;; first cruise through sc-preferred-attribution-list looking for
1162 ;; a match in either sc-attributions or sc-mail-info. if the
1163 ;; element is "sc-consult", then we have to do the alist
1164 ;; consultation phase
1166 (let* ((preferred (car attriblist)))
1168 ((string= preferred "sc-consult")
1169 ;; we've been told to consult the attribution vs. mail
1170 ;; header key alist. we do this until we find a match in
1171 ;; the sc-attrib-selection-list. if we do not find a match,
1172 ;; we continue scanning attriblist
1173 (let ((attrib (sc-scan-info-alist sc-attrib-selection-list)))
1176 (setq attriblist (cdr attriblist)))
1178 (setq attribution attrib
1181 (setq attribution (eval attrib))
1182 (if (stringp attribution)
1183 (setq attriblist nil)
1184 (setq attribution nil
1185 attriblist (cdr attriblist))))
1186 (t (error "%s did not evaluate to a string or list!"
1187 "sc-attrib-selection-list"))
1189 ((setq attribution (cdr (assoc preferred sc-attributions)))
1190 (setq attriblist nil))
1192 (setq attriblist (cdr attriblist)))
1195 ;; if preference was not found, we may use a secondary method to
1196 ;; find a valid attribution
1197 (if (and (not attribution)
1198 (not sc-use-only-preference-p))
1199 ;; secondary method tries to find a preference in this order
1205 ;; 6. first non-empty attribution in alist
1207 (or (cdr (assoc "sc-lastchoice" sc-attributions))
1208 (cdr (assoc "x-attribution" sc-attributions))
1209 (cdr (assoc "firstname" sc-attributions))
1210 (cdr (assoc "lastname" sc-attributions))
1211 (cdr (assoc "initials" sc-attributions))
1212 (cdr (car sc-attributions)))))
1214 ;; still couldn't find an attribution. we're now limited to using
1215 ;; the default attribution, but we'll force a query when this happens
1216 (if (not attribution)
1217 (setq attribution sc-default-attribution
1220 ;; create the attribution prefix
1221 (setq citation (sc-make-citation attribution))
1223 ;; run the post selection hook before querying the user
1224 (run-hooks 'sc-attribs-postselect-hook)
1226 ;; query for confirmation
1228 (let* ((query-alist (mapcar (function (lambda (entry)
1229 (list (cdr entry))))
1231 (minibuffer-local-completion-map
1232 sc-minibuffer-local-completion-map)
1233 (minibuffer-local-map sc-minibuffer-local-map)
1234 (initial attribution)
1235 (completer-disable t) ; in case completer.el is used
1237 (setq sc-attrib-or-cite nil) ; nil==attribution, t==citation
1239 (catch 'sc-reconfirm
1242 (if sc-attrib-or-cite
1244 "Enter citation prefix: "
1246 'sc-citation-confirmation-history)
1248 "Complete attribution name: "
1251 'sc-attribution-confirmation-history)
1254 (if sc-attrib-or-cite
1255 ;; since the citation was chosen, we have to guess at
1257 (setq citation choice
1258 attribution (or (sc-guess-attribution citation)
1261 (setq citation (sc-make-citation choice)
1262 attribution choice))
1265 ;; its possible that the user wants to downcase the citation and
1268 (setq citation (downcase citation)
1269 attribution (downcase attribution)))
1271 ;; set up mail info alist
1272 (let* ((ckey "sc-citation")
1273 (akey "sc-attribution")
1274 (ckeyval (assoc ckey sc-mail-info))
1275 (akeyval (assoc akey sc-mail-info)))
1277 (setcdr ckeyval citation)
1278 (push (cons ckey citation) sc-mail-info))
1280 (setcdr akeyval attribution)
1281 (push (cons akey attribution) sc-mail-info)))
1283 ;; set the sc-lastchoice attribution
1284 (let* ((lkey "sc-lastchoice")
1285 (lastchoice (assoc lkey sc-attributions)))
1287 (setcdr lastchoice attribution)
1288 (push (cons lkey attribution) sc-attributions)))
1292 ;; ======================================================================
1293 ;; filladapt hooks for supercite 3.1. you shouldn't need anything
1294 ;; extra to make gin-mode understand supercited lines. Even this
1295 ;; stuff might not be entirely necessary...
1297 (defun sc-cite-regexp (&optional root-regexp)
1298 "Return a regexp describing a Supercited line.
1299 The regexp is the concatenation of `sc-citation-leader-regexp',
1300 `sc-citation-root-regexp', `sc-citation-delimiter-regexp', and
1301 `sc-citation-separator-regexp'. If optional ROOT-REGEXP is supplied,
1302 use it instead of `sc-citation-root-regexp'."
1303 (concat sc-citation-leader-regexp
1304 (or root-regexp sc-citation-root-regexp)
1305 sc-citation-delimiter-regexp
1306 sc-citation-separator-regexp))
1308 (defun sc-make-citation (attribution)
1309 "Make a non-nested citation from ATTRIBUTION."
1310 (concat sc-citation-leader
1312 sc-citation-delimiter
1313 sc-citation-separator))
1315 (defvar filladapt-prefix-table)
1317 (defun sc-setup-filladapt ()
1318 "Setup `filladapt-prefix-table' to handle Supercited paragraphs."
1319 (let* ((fa-sc-elt 'filladapt-supercite-included-text)
1320 (elt (rassq fa-sc-elt filladapt-prefix-table)))
1321 (if elt (setcar elt (sc-cite-regexp))
1322 (message "Filladapt doesn't seem to know about Supercite.")
1326 ;; ======================================================================
1327 ;; citing and unciting regions of text
1329 (defvar sc-fill-begin 1
1330 "Buffer position to begin filling.")
1331 (defvar sc-fill-line-prefix ""
1332 "Fill prefix of previous line")
1335 (defun sc-fill-if-different (&optional prefix)
1336 "Fill the region bounded by `sc-fill-begin' and point.
1337 Only fill if optional PREFIX is different than `sc-fill-line-prefix'.
1338 If `sc-auto-fill-region-p' is nil, do not fill region. If PREFIX is
1339 not supplied, initialize fill variables. This is useful for a regi
1340 `begin' frame-entry."
1342 (setq sc-fill-line-prefix ""
1343 sc-fill-begin (line-beginning-position))
1344 (if (and sc-auto-fill-region-p
1345 (not (string= prefix sc-fill-line-prefix)))
1346 (let ((fill-prefix sc-fill-line-prefix))
1347 (if (not (string= fill-prefix ""))
1348 (fill-region sc-fill-begin (line-beginning-position)))
1349 (setq sc-fill-line-prefix prefix
1350 sc-fill-begin (line-beginning-position))))
1354 (defun sc-cite-coerce-cited-line ()
1355 "Coerce a Supercited line to look like our style."
1356 (let* ((attribution (sc-guess-attribution))
1357 (regexp (sc-cite-regexp attribution))
1358 (prefix (sc-make-citation attribution)))
1359 (if (and attribution
1360 (looking-at regexp))
1365 (goto-char (match-end 0))
1366 (if (bolp) (forward-char -1))
1369 (sc-fill-if-different prefix)))
1372 (defun sc-cite-coerce-dumb-citer ()
1373 "Coerce a non-nested citation that's been cited with a dumb nesting citer."
1374 (delete-region (match-beginning 1) (match-end 1))
1376 (sc-cite-coerce-cited-line))
1378 (defun sc-guess-nesting (&optional string)
1379 "Guess the citation nesting on the current line.
1380 If nesting cannot be guessed, nil is returned. Optional STRING if
1381 supplied, is used instead of the line point is on in the current
1384 (string (or string (buffer-substring (line-beginning-position)
1385 (line-end-position))))
1388 (= start (or (string-match sc-citation-leader-regexp string start) -1))
1389 (setq start (match-end 0))
1390 (= start (or (string-match sc-citation-delimiter-regexp string start) -1))
1391 (setq nesting (match-string 0 string)
1392 start (match-end 0))
1393 (= start (or (string-match sc-citation-separator-regexp string start) -1))
1396 (defun sc-add-citation-level ()
1397 "Add a citation level for nested citation style w/ coercion."
1398 (let* ((nesting (sc-guess-nesting))
1399 (citation (make-string (1+ (length nesting))
1400 (string-to-char sc-citation-delimiter)))
1401 (prefix (concat sc-citation-leader citation sc-citation-separator)))
1402 (if (looking-at (sc-cite-regexp ""))
1403 (delete-region (match-beginning 0) (match-end 0)))
1405 (sc-fill-if-different prefix)))
1407 (defun sc-cite-line (&optional citation)
1408 "Cite a single line of uncited text.
1409 Optional CITATION overrides any citation automatically selected."
1410 (if sc-fixup-whitespace-p
1412 (let ((prefix (or citation
1413 (cdr (assoc "sc-citation" sc-mail-info))
1414 sc-default-attribution)))
1416 (sc-fill-if-different prefix))
1419 (defun sc-uncite-line ()
1420 "Remove citation from current line."
1421 (let ((cited (looking-at (sc-cite-regexp))))
1423 (delete-region (match-beginning 0) (match-end 0))))
1426 (defun sc-recite-line (regexp)
1427 "Remove citation matching REGEXP from current line and recite line."
1428 (let ((cited (looking-at (concat "^" regexp)))
1429 (prefix (cdr (assoc "sc-citation" sc-mail-info))))
1431 (delete-region (match-beginning 0) (match-end 0)))
1432 (insert (or prefix sc-default-attribution))
1433 (sc-fill-if-different prefix))
1436 ;; interactive functions
1437 (defun sc-cite-region (start end &optional confirm-p interactive)
1438 "Cite a region delineated by START and END.
1439 If optional CONFIRM-P is non-nil, the attribution is confirmed before
1440 its use in the citation string. This function first runs
1443 When called interactively, the optional arg INTERACTIVE is non-nil,
1444 and that means call `sc-select-attribution' too."
1445 (interactive "r\nP\np")
1447 (let ((frame (sc-scan-info-alist sc-cite-frame-alist))
1448 (sc-confirm-always-p (if confirm-p t sc-confirm-always-p)))
1449 (if (and frame (symbolp frame))
1450 (setq frame (symbol-value frame)))
1451 (or frame (setq frame sc-default-cite-frame))
1452 (run-hooks 'sc-pre-cite-hook)
1454 (sc-select-attribution))
1455 (regi-interpret frame start end)))
1457 (defun sc-uncite-region (start end)
1458 "Uncite a region delineated by START and END.
1459 First runs `sc-pre-uncite-hook'."
1462 (let ((frame (sc-scan-info-alist sc-uncite-frame-alist)))
1463 (if (and frame (symbolp frame))
1464 (setq frame (symbol-value frame)))
1465 (or frame (setq frame sc-default-uncite-frame))
1466 (run-hooks 'sc-pre-uncite-hook)
1467 (regi-interpret frame start end)))
1469 (defun sc-recite-region (start end)
1470 "Recite a region delineated by START and END.
1471 First runs `sc-pre-recite-hook'."
1473 (let ((sc-confirm-always-p t))
1474 (sc-select-attribution))
1476 (let ((frame (sc-scan-info-alist sc-recite-frame-alist)))
1477 (if (and frame (symbolp frame))
1478 (setq frame (symbol-value frame)))
1479 (or frame (setq frame sc-default-recite-frame))
1480 (run-hooks 'sc-pre-recite-hook)
1481 (regi-interpret frame start end)))
1484 ;; ======================================================================
1487 (defun sc-hdr (prefix field &optional sep return-nil-p)
1488 "Returns a concatenation of PREFIX and FIELD.
1489 If FIELD is not a string or is the empty string, the empty string will
1490 be returned. Optional third argument SEP is concatenated on the end if
1491 it is a string. Returns empty string, unless optional RETURN-NIL-P is
1493 (if (and (stringp field)
1494 (not (string= field "")))
1495 (concat prefix field (or sep ""))
1496 (and (not return-nil-p) "")))
1498 (defun sc-whofrom ()
1499 "Return the value of (sc-mail-field \"from\") or nil."
1500 (let ((sc-mumble nil))
1501 (sc-mail-field "from")))
1503 (defun sc-no-header ()
1504 "Does nothing. Use this instead of nil to get a blank header."
1507 (defun sc-no-blank-line-or-header()
1508 "Similar to `sc-no-header' except it removes the preceding blank line."
1511 (progn (forward-line -1)
1512 (or (= (point) (mail-header-end))
1513 (and (eq major-mode 'mh-letter-mode)
1515 (mh-in-header-p))))))
1516 (progn (forward-line)
1517 (let ((kill-lines-magic t))
1520 (defun sc-header-on-said ()
1521 "\"On <date>, <from> said:\" unless:
1522 1. the \"from\" field cannot be found, in which case nothing is inserted;
1523 2. the \"date\" field is missing in which case only the from part is printed."
1524 (let ((sc-mumble "")
1525 (whofrom (sc-whofrom)))
1527 (insert sc-reference-tag-string
1528 (sc-hdr "On " (sc-mail-field "date") ", ")
1529 whofrom " said:\n"))))
1531 (defun sc-header-inarticle-writes ()
1532 "\"In article <message-id>, <from> writes:\"
1533 Treats \"message-id\" and \"from\" fields similar to `sc-header-on-said'."
1534 (let ((sc-mumble "")
1535 (whofrom (sc-mail-field "from")))
1537 (insert sc-reference-tag-string
1538 (sc-hdr "In article " (sc-mail-field "message-id") ", ")
1539 whofrom " writes:\n"))))
1541 (defun sc-header-regarding-adds ()
1542 "\"Regarding <subject>; <from> adds:\"
1543 Treats \"subject\" and \"from\" fields similar to `sc-header-on-said'."
1544 (let ((sc-mumble "")
1545 (whofrom (sc-whofrom)))
1547 (insert sc-reference-tag-string
1548 (sc-hdr "Regarding " (sc-mail-field "subject") "; ")
1549 whofrom " adds:\n"))))
1551 (defun sc-header-attributed-writes ()
1552 "\"<sc-attribution>\" == <sc-author> <address> writes:
1553 Treats these fields in a similar manner to `sc-header-on-said'."
1554 (let ((sc-mumble "")
1555 (whofrom (sc-whofrom)))
1557 (insert sc-reference-tag-string
1558 (sc-hdr "\"" (sc-mail-field "sc-attribution") "\" == ")
1559 (sc-hdr "" (sc-mail-field "sc-author") " ")
1560 (or (sc-hdr "<" (sc-mail-field "sc-from-address") ">" t)
1561 (sc-hdr "<" (sc-mail-field "sc-reply-address") ">" t)
1565 (defun sc-header-author-writes ()
1566 "<sc-author> writes:"
1567 (let ((sc-mumble "")
1568 (whofrom (sc-whofrom)))
1570 (insert sc-reference-tag-string
1571 (sc-hdr "" (sc-mail-field "sc-author"))
1574 (defun sc-header-verbose ()
1575 "Very verbose, some say gross."
1576 (let ((sc-mumble "")
1577 (whofrom (sc-whofrom))
1578 (tag sc-reference-tag-string))
1580 (insert (sc-hdr (concat tag "On ") (sc-mail-field "date") ",\n")
1581 (or (sc-hdr tag (sc-mail-field "sc-author") "\n" t)
1582 (concat tag whofrom "\n"))
1583 (sc-hdr (concat tag "from the organization of ")
1584 (sc-mail-field "organization") "\n")
1585 (let ((rtag (concat tag "who can be reached at: ")))
1586 (or (sc-hdr rtag (sc-mail-field "sc-from-address") "\n" t)
1587 (sc-hdr rtag (sc-mail-field "sc-reply-address") "\n" t)
1590 (concat tag "(whose comments are cited below with \"")
1591 (sc-mail-field "sc-citation") "\"),\n")
1592 (sc-hdr (concat tag "had this to say in article ")
1593 (sc-mail-field "message-id") "\n")
1594 (sc-hdr (concat tag "in newsgroups ")
1595 (sc-mail-field "newsgroups") "\n")
1596 (sc-hdr (concat tag "concerning the subject of ")
1597 (sc-mail-field "subject") "\n")
1598 (sc-hdr (concat tag "(see ")
1599 (sc-mail-field "references")
1600 " for more details)\n")
1604 ;; ======================================================================
1607 (defconst sc-electric-bufname " *sc-erefs* "
1608 "Supercite electric reference mode's buffer name.")
1609 (defvar sc-eref-style 0
1610 "Current electric reference style.")
1612 (defun sc-valid-index-p (index)
1613 "Returns INDEX if it is a valid index into `sc-rewrite-header-list'.
1614 Otherwise returns nil."
1615 ;; a number, and greater than or equal to zero
1616 ;; less than or equal to the last index
1617 (and (natnump index)
1618 (< index (length sc-rewrite-header-list))
1621 (defun sc-eref-insert-selected (&optional nomsg)
1622 "Insert the selected reference header in the current buffer.
1623 Optional NOMSG, if non-nil, inhibits printing messages, unless an
1625 (let ((ref (nth sc-eref-style sc-rewrite-header-list)))
1629 (let ((lines (count-lines (point-min) (point-max))))
1630 (or nomsg (message "Ref header %d [%d line%s]: %s"
1632 (if (= lines 1) "" "s")
1636 "Symbol's function definition is void: %s (Header %d)"
1637 (car (cdr err)) sc-eref-style)
1642 (defun sc-electric-mode (&optional arg)
1644 Mode for viewing Supercite reference headers. Commands are:
1645 \n\\{sc-electric-mode-map}
1647 `sc-electric-mode' is not intended to be run interactively, but rather
1648 accessed through Supercite's electric reference feature. See
1649 `sc-insert-reference' for more details. Optional ARG is the initial
1650 header style to use, unless not supplied or invalid, in which case
1651 `sc-preferred-header-style' is used."
1653 (let ((info sc-mail-info))
1656 (or (sc-valid-index-p arg)
1657 (sc-valid-index-p sc-preferred-header-style)
1660 (get-buffer-create sc-electric-bufname)
1661 ;; set up buffer and enter command loop
1663 (save-window-excursion
1664 (pop-to-buffer sc-electric-bufname)
1665 (kill-all-local-variables)
1666 (let ((sc-mail-info info)
1667 (buffer-read-only t)
1668 (mode-name "SC Electric Refs")
1669 (major-mode 'sc-electric-mode))
1670 (use-local-map sc-electric-mode-map)
1671 (sc-eref-show sc-eref-style)
1672 (run-mode-hooks 'sc-electric-mode-hook)
1677 (sc-eref-insert-selected))
1678 (kill-buffer sc-electric-bufname)
1681 ;; functions for electric reference mode
1682 (defun sc-eref-show (index)
1683 "Show reference INDEX in `sc-rewrite-header-list'."
1684 (let ((msg "No %ing reference headers in list.")
1685 (last (length sc-rewrite-header-list)))
1688 ((sc-valid-index-p index) index)
1690 (if sc-electric-circular-p
1692 (progn (error msg "preced") 0)))
1694 (if sc-electric-circular-p
1696 (progn (error msg "follow") (1- last))))
1699 (set-buffer sc-electric-bufname)
1700 (let ((buffer-read-only nil))
1702 (goto-char (point-min))
1703 (sc-eref-insert-selected)
1704 ;; now shrink the window to just contain the electric reference
1706 (let ((hdrlines (count-lines (point-min) (point-max)))
1707 (winlines (1- (window-height))))
1708 (if (/= hdrlines winlines)
1709 (if (> hdrlines winlines)
1710 ;; we have to enlarge the window
1711 (enlarge-window (- hdrlines winlines))
1712 ;; we have to shrink the window
1713 (shrink-window (- winlines (max hdrlines window-min-height)))
1717 (defun sc-eref-next ()
1718 "Display next reference in other buffer."
1720 (sc-eref-show (1+ sc-eref-style)))
1722 (defun sc-eref-prev ()
1723 "Display previous reference in other buffer."
1725 (sc-eref-show (1- sc-eref-style)))
1727 (defun sc-eref-setn ()
1728 "Set reference header selected as preferred."
1730 (setq sc-preferred-header-style sc-eref-style)
1731 (message "Preferred reference style set to header %d." sc-eref-style))
1733 (defun sc-eref-goto (refnum)
1734 "Show reference style indexed by REFNUM.
1735 If REFNUM is an invalid index, don't go to that reference and return
1737 (interactive "NGoto Reference: ")
1738 (if (sc-valid-index-p refnum)
1739 (sc-eref-show refnum)
1740 (error "Invalid reference: %d. (Range: [%d .. %d])"
1741 refnum 0 (1- (length sc-rewrite-header-list)))
1744 (defun sc-eref-jump ()
1745 "Set reference header to preferred header."
1747 (sc-eref-show sc-preferred-header-style))
1749 (defun sc-eref-abort ()
1750 "Exit from electric reference mode without inserting reference."
1752 (setq sc-eref-style nil)
1753 (exit-recursive-edit))
1755 (defun sc-eref-exit ()
1756 "Exit from electric reference mode and insert selected reference."
1758 (exit-recursive-edit))
1760 (defun sc-insert-reference (arg)
1761 "Insert, at point, a reference header in the body of the reply.
1762 Numeric ARG indicates which header style from `sc-rewrite-header-list'
1763 to use when rewriting the header. No supplied ARG indicates use of
1764 `sc-preferred-header-style'.
1766 With just `\\[universal-argument]', electric reference insert mode is
1767 entered, regardless of the value of `sc-electric-references-p'. See
1768 `sc-electric-mode' for more information."
1772 (let ((preference (or (sc-valid-index-p arg)
1773 (sc-valid-index-p sc-preferred-header-style)
1774 sc-preferred-header-style
1776 (if sc-electric-references-p
1777 (sc-electric-mode preference)
1778 (sc-eref-insert-selected t)
1782 ;; ======================================================================
1783 ;; variable toggling
1785 (defun sc-raw-mode-toggle ()
1786 "Toggle, in one fell swoop, two important SC variables:
1787 `sc-fixup-whitespace-p' and `sc-auto-fill-region-p'"
1789 (setq sc-fixup-whitespace-p (not sc-fixup-whitespace-p)
1790 sc-auto-fill-region-p (not sc-auto-fill-region-p))
1791 (force-mode-line-update))
1793 (defun sc-toggle-var (variable)
1794 "Boolean toggle VARIABLE's value.
1795 VARIABLE must be a bound symbol. nil values change to t, non-nil
1796 values are changed to nil."
1797 (message "%s changed from %s to %s"
1798 variable (symbol-value variable)
1799 (set variable (not (symbol-value variable)))))
1801 (defun sc-set-variable (var)
1802 "Set the Supercite VARIABLE.
1803 This function mimics `set-variable', except that the variable to set
1804 is determined non-interactively. The value is queried for in the
1805 minibuffer exactly the same way that `set-variable' does it.
1807 You can see the current value of the variable when the minibuffer is
1808 querying you by typing `C-h'. Note that the format is changed
1809 slightly from that used by `set-variable' -- the current value is
1810 printed just after the variable's name instead of at the bottom of the
1812 (let* ((minibuffer-help-form '(funcall myhelp))
1815 (with-output-to-temp-buffer "*Help*"
1818 (let ((print-length 20))
1819 (princ "\t(Current value: ")
1820 (prin1 (symbol-value var))
1822 (princ "\n\nDocumentation:\n")
1823 (princ (substring (documentation-property
1825 'variable-documentation)
1827 (with-current-buffer standard-output
1830 (set var (eval-minibuffer (format "Set %s to value: " var)))))
1832 (defmacro sc-toggle-symbol (rootname)
1833 `(defun ,(intern (concat "sc-T-" rootname)) ()
1835 (sc-toggle-var ',(intern (concat "sc-" rootname "-p")))))
1837 (defmacro sc-setvar-symbol (rootname)
1838 `(defun ,(intern (concat "sc-S-" rootname)) ()
1840 (sc-set-variable ',(intern (concat "sc-" rootname)))))
1842 (sc-toggle-symbol "confirm-always")
1843 (sc-toggle-symbol "downcase")
1844 (sc-toggle-symbol "electric-references")
1845 (sc-toggle-symbol "auto-fill-region")
1846 (sc-toggle-symbol "mail-nuke-blank-lines")
1847 (sc-toggle-symbol "nested-citation")
1848 (sc-toggle-symbol "electric-circular")
1849 (sc-toggle-symbol "use-only-preferences")
1850 (sc-toggle-symbol "fixup-whitespace")
1852 (sc-setvar-symbol "preferred-attribution-list")
1853 (sc-setvar-symbol "preferred-header-style")
1854 (sc-setvar-symbol "mail-nuke-mail-headers")
1855 (sc-setvar-symbol "mail-header-nuke-list")
1856 (sc-setvar-symbol "cite-region-limit")
1858 (defun sc-T-describe ()
1861 Supercite provides a number of key bindings which simplify the process
1862 of setting or toggling certain variables controlling its operation.
1864 Note on function names in this list: all functions of the form
1865 `sc-S-<name>' actually call `sc-set-variable' on the corresponding
1866 `sc-<name>' variable. All functions of the form `sc-T-<name>' call
1867 `sc-toggle-var' on the corresponding `sc-<name>-p' variable.
1871 (describe-function 'sc-T-describe))
1874 ;; ======================================================================
1875 ;; published interface to mail and news readers
1877 (define-minor-mode sc-minor-mode
1878 "Supercite minor mode."
1880 :lighter (" SC" (sc-auto-fill-region-p
1881 (":f" (sc-fixup-whitespace-p "w"))
1882 (sc-fixup-whitespace-p ":w")))
1883 :keymap `((,sc-mode-map-prefix . ,sc-mode-map)))
1886 (defun sc-cite-original ()
1887 "Workhorse citing function which performs the initial citation.
1888 This is callable from the various mail and news readers' reply
1889 function according to the agreed upon standard. See the associated
1890 info node `(SC)Top' for more details.
1891 `sc-cite-original' does not do any yanking of the
1892 original message but it does require a few things:
1894 1) The reply buffer is the current buffer.
1896 2) The original message has been yanked and inserted into the
1899 3) Verbose mail headers from the original message have been
1900 inserted into the reply buffer directly before the text of the
1903 4) Point is at the beginning of the verbose headers.
1905 5) Mark is at the end of the body of text to be cited.
1907 For Emacs 19's, the region need not be active (and typically isn't
1908 when this function is called. Also, the hook `sc-pre-hook' is run
1909 before, and `sc-post-hook' is run after the guts of this function."
1910 (run-hooks 'sc-pre-hook)
1916 ;; grab point and mark since the region is probably not active when
1917 ;; this function gets automatically called. we want point to be a
1918 ;; mark so any deleting before point works properly
1919 (let* ((zmacs-regions nil) ; for XEemacs
1920 (mark-active t) ; for Emacs
1921 (point (point-marker))
1922 (mark (copy-marker (mark-marker))))
1924 ;; make sure point comes before mark, not all functions are
1931 ;; first process mail headers, and populate sc-mail-info
1932 (sc-mail-process-headers point mark)
1934 ;; now get possible attributions
1935 (sc-attribs-chop-address (or (sc-mail-field "from")
1936 (sc-mail-field "reply")
1937 (sc-mail-field "reply-to")
1938 (sc-mail-field "sender")))
1939 ;; select the attribution
1940 (sc-select-attribution)
1942 ;; cite the region, but first check the value of sc-cite-region-limit
1943 (let ((linecnt (count-lines point mark)))
1944 (and sc-cite-region-limit
1945 (if (or (not (numberp sc-cite-region-limit))
1946 (<= linecnt sc-cite-region-limit))
1948 ;; cite the region and insert the header rewrite
1949 (sc-cite-region point mark)
1951 (let ((sc-eref-style (or sc-preferred-header-style 0)))
1952 (if sc-electric-references-p
1953 (sc-electric-mode sc-eref-style)
1954 (sc-eref-insert-selected t))))
1957 "Region not cited. %d lines exceeds sc-cite-region-limit: %d"
1958 linecnt sc-cite-region-limit))))
1960 ;; finally, free the point-marker
1961 (set-marker point nil)
1962 (set-marker mark nil)
1964 (run-hooks 'sc-post-hook))
1967 ;; ======================================================================
1968 ;; bug reporting and miscellaneous commands
1970 (defun sc-open-line (arg)
1971 "Like `open-line', but insert the citation prefix at the front of the line.
1972 With numeric ARG, inserts that many new lines."
1975 (let ((start (point))
1976 (prefix (or (progn (beginning-of-line)
1977 (if (looking-at (sc-cite-regexp))
1989 (defun sc-insert-citation (arg)
1990 "Insert citation string at beginning of current line if not already cited.
1991 With `\\[universal-argument]' insert citation even if line is already
1996 (if (or (not (looking-at (sc-cite-regexp)))
1997 (looking-at "^[ \t]*$")
1999 (insert (sc-mail-field "sc-citation"))
2000 (error "Line is already cited"))))
2002 (defun sc-version (message)
2003 "Echo the current version of Supercite in the minibuffer.
2004 If MESSAGE is non-nil (interactively, with no prefix argument),
2005 inserts the version string in the current buffer instead."
2006 (interactive (not current-prefix-arg))
2007 (let ((verstr (format "Using Supercite.el %s" emacs-version)))
2010 (insert "`sc-version' says: " verstr))))
2012 (defun sc-describe ()
2014 Supercite is a package which provides a flexible mechanism for citing
2015 email and news replies. Please see the associated texinfo file for
2016 more information. Info node `(SC)Top'."
2018 (describe-function 'sc-describe))
2020 (define-obsolete-function-alias 'sc-submit-bug-report 'report-emacs-bug "23.1")
2024 (provide 'supercite)
2025 (run-hooks 'sc-load-hook)
2027 ;; arch-tag: a5d5bfa6-3bd5-4414-8c65-0afc83e45cd3
2028 ;;; supercite.el ends here