1 ;;; supercite.el --- minor mode for citing mail and news replies
3 ;; Copyright (C) 1993, 1997, 2003, 2004, 2005 Free Software Foundation, Inc.
5 ;; Author: 1993 Barry A. Warsaw <bwarsaw@python.org>
6 ;; Maintainer: Glenn Morris <gmorris@ast.cam.ac.uk>
7 ;; Created: February 1993
8 ;; Last Modified: 1993/09/22 18:58:46
9 ;; Keywords: mail, news
11 ;; supercite.el revision: 3.54
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
31 ;; supercite|Barry A. Warsaw|supercite-help@python.org
32 ;; |Mail and news reply citation package
33 ;; |1993/09/22 18:58:46|3.1|
41 (require 'sendmail
) ;; For mail-header-end.
43 ;; start user configuration variables
44 ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
46 (defgroup supercite nil
52 (defgroup supercite-frames nil
53 "Supercite (regi) frames"
57 (defgroup supercite-attr nil
58 "Supercite attributions"
62 (defgroup supercite-cite nil
67 (defgroup supercite-hooks nil
68 "Hooking into supercite"
72 (defcustom sc-auto-fill-region-p t
73 "*If non-nil, automatically fill each paragraph after it has been cited."
77 (defcustom sc-blank-lines-after-headers
1
78 "*Number of blank lines to leave after mail headers have been nuked.
79 Set to nil, to use whatever blank lines happen to occur naturally."
80 :type
'(choice (const :tag
"leave" nil
)
84 (defcustom sc-citation-leader
" "
85 "*String comprising first part of a citation."
87 :group
'supercite-cite
)
89 (defcustom sc-citation-delimiter
">"
90 "*String comprising third part of a citation.
91 This string is used in both nested and non-nested citations."
93 :group
'supercite-cite
)
95 (defcustom sc-citation-separator
" "
96 "*String comprising fourth and last part of a citation."
98 :group
'supercite-cite
)
100 (defcustom sc-citation-leader-regexp
"[ \t]*"
101 "*Regexp describing citation leader for a cited line.
102 This should NOT have a leading `^' character."
104 :group
'supercite-cite
)
106 ;; Nemacs and Mule users note: please see the texinfo manual for
107 ;; suggestions on setting these variables.
108 (defcustom sc-citation-root-regexp
"[-._[:alnum:]]*"
109 "*Regexp describing variable root part of a citation for a cited line.
110 This should NOT have a leading `^' character. See also
111 `sc-citation-nonnested-root-regexp'."
113 :group
'supercite-cite
)
115 (defcustom sc-citation-nonnested-root-regexp
"[-._[:alnum:]]+"
116 "*Regexp describing the variable root part of a nested citation.
117 This should NOT have a leading `^' character. This variable is
118 related to `sc-citation-root-regexp' but whereas that variable
119 describes both nested and non-nested citation roots, this variable
120 describes only nested citation roots."
122 :group
'supercite-cite
)
124 (defcustom sc-citation-delimiter-regexp
"[>]+"
125 "*Regexp describing citation delimiter for a cited line.
126 This should NOT have a leading `^' character."
128 :group
'supercite-cite
)
130 (defcustom sc-citation-separator-regexp
"[ \t]*"
131 "*Regexp describing citation separator for a cited line.
132 This should NOT have a leading `^' character."
134 :group
'supercite-cite
)
136 (defcustom sc-cite-blank-lines-p nil
137 "*If non-nil, put a citation on blank lines."
139 :group
'supercite-cite
)
141 (defcustom sc-cite-frame-alist
'()
142 "*Alist for frame selection during citing.
143 Each element of this list has the following form:
144 (INFOKEY ((REGEXP . FRAME)
148 Where INFOKEY is a key for `sc-mail-field', REGEXP is a regular
149 expression to match against the INFOKEY's value. FRAME is
150 a citation frame, or a symbol that represents the name of
151 a variable whose value is a citation frame."
152 :type
'(repeat (list symbol
(repeat (cons regexp
153 (choice (repeat (repeat sexp
))
155 :group
'supercite-frames
)
156 (put 'sc-cite-frame-alist
'risky-local-variable t
)
158 (defcustom sc-uncite-frame-alist
'()
159 "*Alist for frame selection during unciting.
160 See the variable `sc-cite-frame-alist' for details."
161 :type
'(repeat (list symbol
(repeat (cons regexp
162 (choice (repeat (repeat sexp
))
164 :group
'supercite-frames
)
165 (put 'sc-uncite-frame-alist
'risky-local-variable t
)
167 (defcustom sc-recite-frame-alist
'()
168 "*Alist for frame selection during reciting.
169 See the variable `sc-cite-frame-alist' for details."
170 :type
'(repeat (list symbol
(repeat (cons regexp
171 (choice (repeat (repeat sexp
))
173 :group
'supercite-frames
)
174 (put 'sc-recite-frame-alist
'risky-local-variable t
)
176 (defcustom sc-default-cite-frame
177 '(;; initialize fill state and temporary variables when entering
178 ;; frame. this makes things run much faster
180 (sc-fill-if-different)
181 (setq sc-tmp-nested-regexp
(sc-cite-regexp "")
182 sc-tmp-nonnested-regexp
(sc-cite-regexp)
187 (sc-cite-regexp sc-citation-nonnested-root-regexp
))
189 ;; blank lines mean paragraph separators, so fill the last cited
190 ;; paragraph, unless sc-cite-blank-lines-p is non-nil, in which
191 ;; case we treat blank lines just like any other line.
192 ("^[ \t]*$" (if sc-cite-blank-lines-p
194 (sc-fill-if-different "")))
195 ;; do nothing if looking at a reference tag. make sure that the
196 ;; tag string isn't the empty string since this will match every
197 ;; line. it cannot be nil.
198 (sc-reference-tag-string (if (string= sc-reference-tag-string
"")
201 ;; this regexp catches nested citations in which the author cited
202 ;; a non-nested citation with a dumb citer.
203 (sc-tmp-dumb-regexp (sc-cite-coerce-dumb-citer))
204 ;; if we are looking at a nested citation then add a citation level
205 (sc-tmp-nested-regexp (sc-add-citation-level))
206 ;; if we're looking at a non-nested citation, coerce it to our style
207 (sc-tmp-nonnested-regexp (sc-cite-coerce-cited-line))
208 ;; we must be looking at an uncited line. if we are in nested
209 ;; citations, just add a citation level
210 (sc-nested-citation-p (sc-add-citation-level))
211 ;; we're looking at an uncited line and we are in non-nested
212 ;; citations, so cite it with a non-nested citation
214 ;; be sure when we're done that we fill the last cited paragraph.
215 (end (sc-fill-if-different ""))
217 "*Default REGI frame for citing a region."
218 :type
'(repeat (repeat sexp
))
219 :group
'supercite-frames
)
220 (put 'sc-default-cite-frame
'risky-local-variable t
)
222 (defcustom sc-default-uncite-frame
223 '(;; do nothing on a blank line
225 ;; if the line is cited, uncite it
226 ((sc-cite-regexp) (sc-uncite-line))
228 "*Default REGI frame for unciting a region."
229 :type
'(repeat (repeat sexp
))
230 :group
'supercite-frames
)
231 (put 'sc-default-uncite-frame
'risky-local-variable t
)
233 (defcustom sc-default-recite-frame
234 '(;; initialize fill state when entering frame
235 (begin (sc-fill-if-different))
236 ;; do nothing on a blank line
238 ;; if we're looking at a cited line, recite it
239 ((sc-cite-regexp) (sc-recite-line (sc-cite-regexp)))
240 ;; otherwise, the line is uncited, so just cite it
242 ;; be sure when we're done that we fill the last cited paragraph.
243 (end (sc-fill-if-different ""))
245 "*Default REGI frame for reciting a region."
246 :type
'(repeat (repeat sexp
))
247 :group
'supercite-frames
)
248 (put 'sc-default-recite-frame
'risky-local-variable t
)
250 (defcustom sc-cite-region-limit t
251 "*This variable controls automatic citation of yanked text.
254 non-nil -- cite the entire region, regardless of its size
255 nil -- do not cite the region at all
256 <integer> -- a number indicating the threshold for citation. When
257 the number of lines in the region is greater than this
258 value, a warning message will be printed and the region
259 will not be cited. Lines in region are counted with
262 The gathering of attribution information is not affected by the value
263 of this variable. The number of lines in the region is calculated
264 *after* all mail headers are removed. This variable is only consulted
265 during the initial citing via `sc-cite-original'."
266 :type
'(choice (const :tag
"do not cite" nil
)
267 (integer :tag
"citation threshold")
268 (other :tag
"always cite" t
))
269 :group
'supercite-cite
)
271 (defcustom sc-confirm-always-p t
272 "*If non-nil, always confirm attribution string before citing text body."
274 :group
'supercite-attr
)
276 (defcustom sc-default-attribution
"Anon"
277 "*String used when author's attribution cannot be determined."
279 :group
'supercite-attr
)
280 (defcustom sc-default-author-name
"Anonymous"
281 "*String used when author's name cannot be determined."
283 :group
'supercite-attr
)
284 (defcustom sc-downcase-p nil
285 "*Non-nil means downcase the attribution and citation strings."
287 :group
'supercite-attr
288 :group
'supercite-cite
)
289 (defcustom sc-electric-circular-p t
290 "*If non-nil, treat electric references as circular."
292 :group
'supercite-attr
)
294 (defcustom sc-electric-mode-hook nil
295 "*Hook for `sc-electric-mode' electric references mode."
297 :group
'supercite-hooks
)
298 (defcustom sc-electric-references-p nil
299 "*Use electric references if non-nil."
303 (defcustom sc-fixup-whitespace-p nil
304 "*If non-nil, delete all leading white space before citing."
308 (defcustom sc-load-hook nil
309 "*Hook which gets run once after Supercite loads."
311 :group
'supercite-hooks
)
312 (defcustom sc-pre-hook nil
313 "*Hook which gets run before each invocation of `sc-cite-original'."
315 :group
'supercite-hooks
)
316 (defcustom sc-post-hook nil
317 "*Hook which gets run after each invocation of `sc-cite-original'."
319 :group
'supercite-hooks
)
321 (defcustom sc-mail-warn-if-non-rfc822-p t
322 "*Warn if mail headers don't conform to RFC822."
324 :group
'supercite-attr
)
325 (defcustom sc-mumble
""
326 "*Value returned by `sc-mail-field' if field isn't in mail headers."
328 :group
'supercite-attr
)
330 (defcustom sc-name-filter-alist
331 '(("^\\(Mr\\|Mrs\\|Ms\\|Dr\\)[.]?$" .
0)
332 ("^\\(Jr\\|Sr\\)[.]?$" . last
)
335 "*Name list components which are filtered out as noise.
336 This variable contains an association list where each element is of
337 the form: (REGEXP . POSITION).
339 REGEXP is a regular expression which matches the name list component.
340 Match is performed using `string-match'. POSITION is the position in
341 the name list which can match the regular expression, starting at zero
342 for the first element. Use `last' to match the last element in the
343 list and `any' to match all elements."
344 :type
'(repeat (cons regexp
(choice (const last
) (const any
)
345 (integer :tag
"position"))))
346 :group
'supercite-attr
)
348 (defcustom sc-nested-citation-p nil
349 "*Controls whether to use nested or non-nested citation style.
350 Non-nil uses nested citations, nil uses non-nested citations."
354 (defcustom sc-nuke-mail-headers
'all
355 "*Controls mail header nuking.
356 Used in conjunction with `sc-nuke-mail-header-list'. Valid values are:
358 `all' -- nuke all mail headers
359 `none' -- don't nuke any mail headers
360 `specified' -- nuke headers specified in `sc-nuke-mail-header-list'
361 `keep' -- keep headers specified in `sc-nuke-mail-header-list'"
362 :type
'(choice (const all
) (const none
)
363 (const specified
) (const keep
))
366 (defcustom sc-nuke-mail-header-list nil
367 "*List of mail header regexps to remove or keep in body of reply.
368 This list contains regular expressions describing the mail headers to
369 keep or nuke, depending on the value of `sc-nuke-mail-headers'."
370 :type
'(repeat regexp
)
373 (defcustom sc-preferred-attribution-list
374 '("sc-lastchoice" "x-attribution" "firstname" "initials" "lastname")
375 "*Specifies what to use as the attribution string.
376 Supercite creates a list of possible attributions when it scans the
377 mail headers from the original message. Each attribution choice is
378 associated with a key in an attribution alist. Supercite tries to
379 pick a \"preferred\" attribution by matching the attribution alist
380 keys against the elements in `sc-preferred-attribution-list' in order.
381 The first non-empty string value found is used as the preferred
384 Note that Supercite now honors the X-Attribution: mail field. If
385 present in the original message, the value of this field should always
386 be used to select the most preferred attribution since it reflects how
387 the original author would like to be distinguished. It should be
388 considered bad taste to put any attribution preference key before
389 \"x-attribution\" in this list, except perhaps for \"sc-lastchoice\"
392 Supercite remembers the last attribution used when reciting an already
393 cited paragraph. This attribution will always be saved with the
394 \"sc-lastchoice\" key, which can be used in this list. Note that the
395 last choice is always reset after every call of `sc-cite-original'.
397 Barring error conditions, the following preferences are always present
398 in the attribution alist:
400 \"emailname\" -- email terminus name
401 \"initials\" -- initials of author
402 \"firstname\" -- first name of author
403 \"lastname\" -- last name of author
404 \"middlename-1\" -- first middle name of author
405 \"middlename-2\" -- second middle name of author
408 Middle name indexes can be any positive integer greater than 0,
409 although it is unlikely that many authors will supply more than one
410 middle name, if that many. The string of all middle names is
411 associated with the key \"middlenames\"."
412 :type
'(repeat string
)
413 :group
'supercite-attr
)
415 (defcustom sc-attrib-selection-list nil
416 "*An alist for selecting preferred attribution based on mail headers.
417 Each element of this list has the following form:
419 (INFOKEY ((REGEXP . ATTRIBUTION)
420 (REGEXP . ATTRIBUTION)
423 Where INFOKEY is a key for `sc-mail-field', REGEXP is a regular
424 expression to match against the INFOKEY's value. ATTRIBUTION can be a
425 string or a list. If its a string, then it is the attribution that is
426 selected by `sc-select-attribution'. If it is a list, it is `eval'd
427 and the return value must be a string, which is used as the selected
428 attribution. Note that the variable `sc-preferred-attribution-list'
429 must contain an element of the string \"sc-consult\" for this variable
430 to be consulted during attribution selection."
431 :type
'(repeat (list string
433 (choice (sexp :tag
"List to eval")
435 :group
'supercite-attr
)
436 (put 'sc-attrib-selection-list
'risky-local-variable t
)
438 (defcustom sc-attribs-preselect-hook nil
439 "*Hook to run before selecting an attribution."
441 :group
'supercite-attr
442 :group
'supercite-hooks
)
443 (defcustom sc-attribs-postselect-hook nil
444 "*Hook to run after selecting an attribution, but before confirmation."
446 :group
'supercite-attr
447 :group
'supercite-hooks
)
449 (defcustom sc-pre-cite-hook nil
450 "*Hook to run before citing a region of text."
452 :group
'supercite-cite
453 :group
'supercite-hooks
)
454 (defcustom sc-pre-uncite-hook nil
455 "*Hook to run before unciting a region of text."
457 :group
'supercite-cite
458 :group
'supercite-hooks
)
459 (defcustom sc-pre-recite-hook nil
460 "*Hook to run before reciting a region of text."
462 :group
'supercite-cite
463 :group
'supercite-hooks
)
465 (defcustom sc-preferred-header-style
4
466 "*Index into `sc-rewrite-header-list' specifying preferred header style.
467 Index zero accesses the first function in the list."
471 (defcustom sc-reference-tag-string
">>>>> "
472 "*String used at the beginning of built-in reference headers."
476 (defcustom sc-rewrite-header-list
479 (sc-header-inarticle-writes)
480 (sc-header-regarding-adds)
481 (sc-header-attributed-writes)
482 (sc-header-author-writes)
484 (sc-no-blank-line-or-header)
486 "*List of reference header rewrite functions.
487 The variable `sc-preferred-header-style' controls which function in
488 this list is chosen for automatic reference header insertions.
489 Electric reference mode will cycle through this list of functions."
492 (put 'sc-rewrite-header-list
'risky-local-variable t
)
494 (defcustom sc-titlecue-regexp
"\\s +-+\\s +"
495 "*Regular expression describing the separator between names and titles.
496 Set to nil to treat entire field as a name."
497 :type
'(choice (const :tag
"entire field as name" nil
)
499 :group
'supercite-attr
)
501 (defcustom sc-use-only-preference-p nil
502 "*Controls what happens when the preferred attribution cannot be found.
503 If non-nil, then `sc-default-attribution' will be used. If nil, then
504 some secondary scheme will be employed to find a suitable attribution
507 :group
'supercite-attr
)
509 ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
510 ;; end user configuration variables
512 (defconst sc-version
"3.1"
513 "Supercite version number.")
514 (defconst sc-help-address
"bug-supercite@gnu.org"
515 "Address accepting submissions of bug reports.")
517 (defvar sc-mail-info nil
518 "Alist of mail header information gleaned from reply buffer.")
519 (defvar sc-attributions nil
520 "Alist of attributions for use when citing.")
522 (defvar sc-tmp-nested-regexp nil
523 "Temporary regexp describing nested citations.")
524 (defvar sc-tmp-nonnested-regexp nil
525 "Temporary regexp describing non-nested citations.")
526 (defvar sc-tmp-dumb-regexp nil
527 "Temp regexp describing non-nested citation cited with a nesting citer.")
529 (make-variable-buffer-local 'sc-mail-info
)
530 (make-variable-buffer-local 'sc-attributions
)
533 ;; ======================================================================
536 (defvar sc-mode-map-prefix
"\C-c\C-p"
537 "*Key binding to install Supercite keymap.
538 If this is nil, Supercite keymap is not installed.")
541 (let ((map (make-sparse-keymap)))
542 (define-key map
"a" 'sc-S-preferred-attribution-list
)
543 (define-key map
"b" 'sc-T-mail-nuke-blank-lines
)
544 (define-key map
"c" 'sc-T-confirm-always
)
545 (define-key map
"d" 'sc-T-downcase
)
546 (define-key map
"e" 'sc-T-electric-references
)
547 (define-key map
"f" 'sc-T-auto-fill-region
)
548 (define-key map
"h" 'sc-T-describe
)
549 (define-key map
"l" 'sc-S-cite-region-limit
)
550 (define-key map
"n" 'sc-S-mail-nuke-mail-headers
)
551 (define-key map
"N" 'sc-S-mail-header-nuke-list
)
552 (define-key map
"o" 'sc-T-electric-circular
)
553 (define-key map
"p" 'sc-S-preferred-header-style
)
554 (define-key map
"s" 'sc-T-nested-citation
)
555 (define-key map
"u" 'sc-T-use-only-preferences
)
556 (define-key map
"w" 'sc-T-fixup-whitespace
)
557 (define-key map
"?" 'sc-T-describe
)
559 "Keymap for sub-keymap of setting and toggling functions.")
562 (let ((map (make-sparse-keymap)))
563 (define-key map
"c" 'sc-cite-region
)
564 (define-key map
"f" 'sc-mail-field-query
)
565 (define-key map
"g" 'sc-mail-process-headers
)
566 (define-key map
"h" 'sc-describe
)
567 (define-key map
"i" 'sc-insert-citation
)
568 (define-key map
"o" 'sc-open-line
)
569 (define-key map
"r" 'sc-recite-region
)
570 (define-key map
"\C-p" 'sc-raw-mode-toggle
)
571 (define-key map
"u" 'sc-uncite-region
)
572 (define-key map
"v" 'sc-version
)
573 (define-key map
"w" 'sc-insert-reference
)
574 (define-key map
"\C-t" sc-T-keymap
)
575 (define-key map
"\C-b" 'sc-submit-bug-report
)
576 (define-key map
"?" 'sc-describe
)
578 "Keymap for Supercite quasi-mode.")
580 (defvar sc-electric-mode-map
581 (let ((map (make-sparse-keymap)))
582 (define-key map
"p" 'sc-eref-prev
)
583 (define-key map
"n" 'sc-eref-next
)
584 (define-key map
"s" 'sc-eref-setn
)
585 (define-key map
"j" 'sc-eref-jump
)
586 (define-key map
"x" 'sc-eref-abort
)
587 (define-key map
"q" 'sc-eref-abort
)
588 (define-key map
"\r" 'sc-eref-exit
)
589 (define-key map
"\n" 'sc-eref-exit
)
590 (define-key map
"g" 'sc-eref-goto
)
591 (define-key map
"?" 'describe-mode
)
592 (define-key map
"\C-h" 'describe-mode
)
593 (define-key map
[f1] 'describe-mode)
594 (define-key map [help] 'describe-mode)
596 "Keymap for `sc-electric-mode' electric references mode.")
599 (defvar sc-minibuffer-local-completion-map
600 (let ((map (copy-keymap minibuffer-local-completion-map)))
601 (define-key map "\C-t" 'sc-toggle-fn)
602 (define-key map " " 'self-insert-command)
604 "Keymap for minibuffer confirmation of attribution strings.")
606 (defvar sc-minibuffer-local-map
607 (let ((map (copy-keymap minibuffer-local-map)))
608 (define-key map "\C-t" 'sc-toggle-fn)
610 "Keymap for minibuffer confirmation of attribution strings.")
613 ;; ======================================================================
616 (defun sc-ask (alist)
617 "Ask a question in the minibuffer requiring a single character answer.
618 This function is kind of an extension of `y-or-n-p' where a single
619 letter is used to answer a question. Question is formed from ALIST
620 which has members of the form: (WORD . LETTER). WORD is the long
621 word form, while LETTER is the letter for selecting that answer. The
622 selected letter is returned, or nil if the question was not answered.
623 Note that WORD is a string and LETTER is a character. All LETTERs in
624 the list should be unique."
625 (let* ((prompt (concat
626 (mapconcat (function (lambda (elt) (car elt))) alist ", ")
630 (lambda (elt) (char-to-string (cdr elt)))) alist "/")
634 (if (fboundp 'allocate-event)
638 (if (let ((cursor-in-echo-area t)
641 (setq event (read-event))
642 (prog1 quit-flag (setq quit-flag nil)))
644 (message "%s%s" p (single-key-description event))
645 (and (fboundp 'deallocate-event)
646 (deallocate-event event))
650 (if (featurep 'xemacs)
651 (let* ((key (and (key-press-event-p event) (event-key event)))
652 (char (and key (event-to-character event))))
656 (if char (setq char (downcase char)))
658 ((setq elt (rassq char alist))
659 (message "%s%s" p (car elt))
661 ((and (fboundp 'button-release-event-p)
662 (button-release-event-p event)) ; ignore them
665 (message "%s%s" p (single-key-description event))
666 (if (featurep 'xemacs)
671 (setq p (concat "Try again. " prompt)))))))
672 (and (fboundp 'deallocate-event)
673 (deallocate-event event))
676 (defun sc-scan-info-alist (alist)
677 "Find a match in the info alist that matches a regexp in ALIST."
681 (let* ((elem (car alist))
683 (infoval (sc-mail-field infokey))
684 (mlist (car (cdr elem))))
686 (let* ((ml-elem (car mlist))
687 (regexp (car ml-elem))
688 (thing (cdr ml-elem)))
689 (if (string-match regexp infoval)
690 ;; we found a match, time to return
694 ;; else we didn't find a match
695 (setq mlist (cdr mlist))
696 ))) ;end of mlist loop
697 (setq alist (cdr alist))
698 )) ;end of alist loop
702 ;; ======================================================================
703 ;; extract mail field information from headers in reply buffer
705 ;; holder variables for bc happiness
706 (defvar sc-mail-headers-start nil
707 "Start of header fields.")
708 (defvar sc-mail-headers-end nil
709 "End of header fields.")
710 (defvar sc-mail-field-history nil
711 "For minibuffer completion on mail field queries.")
712 (defvar sc-mail-field-modification-history nil
713 "For minibuffer completion on mail field modifications.")
714 (defvar sc-mail-glom-frame
715 '((begin (setq sc-mail-headers-start (point)))
716 ("^x-attribution:[ \t]+.*$" (sc-mail-fetch-field t) nil t)
717 ("^\\S +:.*$" (sc-mail-fetch-field) nil t)
718 ("^$" (list 'abort '(step . 0)))
719 ("^[ \t]+" (sc-mail-append-field))
720 (sc-mail-warn-if-non-rfc822-p (sc-mail-error-in-mail-field))
721 (end (setq sc-mail-headers-end (point))))
722 "Regi frame for glomming mail header information.")
724 (defvar curline) ; dynamic bondage
727 (defun sc-mail-fetch-field (&optional attribs-p)
728 "Insert a key and value into `sc-mail-info' alist.
729 If optional ATTRIBS-P is non-nil, the key/value pair is placed in
730 `sc-attributions' too."
731 (if (string-match "^\\(\\S *\\)\\s *:\\s +\\(.*\\)$" curline)
732 (let* ((key (downcase (match-string-no-properties 1 curline)))
733 (val (match-string-no-properties 2 curline))
734 (keyval (cons key val)))
735 (push keyval sc-mail-info)
737 (push keyval sc-attributions))))
740 (defun sc-mail-append-field ()
741 "Append a continuation line onto the last fetched mail field's info."
742 (let ((keyval (car sc-mail-info)))
743 (if (and keyval (string-match "^\\s *\\(.*\\)$" curline))
744 (setcdr keyval (concat (cdr keyval) " "
745 (match-string-no-properties 1 curline)))))
748 (defun sc-mail-error-in-mail-field ()
749 "Issue warning that mail headers don't conform to RFC 822."
750 (let* ((len (min (length curline) 10))
751 (ellipsis (if (< len (length curline)) "..." ""))
752 (msg "Mail header \"%s%s\" doesn't conform to RFC 822. skipping..."))
753 (message msg (substring curline 0 len) ellipsis))
758 ;; mail header nuking
759 (defvar sc-mail-last-header-nuked-p nil
760 "True if the last header was nuked.")
762 (defun sc-mail-nuke-line ()
763 "Nuke the current mail header line."
764 (delete-region (line-beginning-position) (line-beginning-position 2))
767 (defun sc-mail-nuke-header-line ()
768 "Delete current-line and set up for possible continuation."
769 (setq sc-mail-last-header-nuked-p t)
772 (defun sc-mail-nuke-continuation-line ()
773 "Delete a continuation line if the last header line was deleted."
774 (if sc-mail-last-header-nuked-p
775 (sc-mail-nuke-line)))
777 (defun sc-mail-cleanup-blank-lines ()
778 "Leave some blank lines after original mail headers are nuked.
779 The number of lines left is specified by `sc-blank-lines-after-headers'."
780 (if sc-blank-lines-after-headers
783 (skip-chars-backward " \t\n")
787 (if (looking-at "[ \t]*$")
788 (delete-region (line-beginning-position)
789 (line-beginning-position 2)))
790 (insert-char ?\n sc-blank-lines-after-headers)))
793 (defun sc-mail-build-nuke-frame ()
794 "Build the regiframe for nuking mail headers."
795 (let (every-func entry-func nonentry-func)
797 ((eq sc-nuke-mail-headers 'all)
798 (setq every-func '(progn (forward-line -1) (sc-mail-nuke-line))))
799 ((eq sc-nuke-mail-headers 'specified)
800 (setq entry-func '(sc-mail-nuke-header-line)
801 nonentry-func '(setq sc-mail-last-header-nuked-p nil)))
802 ((eq sc-nuke-mail-headers 'keep)
803 (setq entry-func '(setq sc-mail-last-header-nuked-p nil)
804 nonentry-func '(sc-mail-nuke-header-line)))
805 ;; we never get far enough to interpret a frame if s-n-m-h == 'none
806 ((eq sc-nuke-mail-headers 'none))
807 (t (error "Invalid value for sc-nuke-mail-headers: %s"
808 sc-nuke-mail-headers))
812 (regi-mapcar sc-nuke-mail-header-list entry-func nil t))
813 (and nonentry-func (list (list "^\\S +:.*$" nonentry-func)))
814 (and (not every-func)
815 '(("^[ \t]+" (sc-mail-nuke-continuation-line))))
816 '((begin (setq sc-mail-last-header-zapped-p nil)))
817 '((end (sc-mail-cleanup-blank-lines)))
818 (and every-func (list (list 'every every-func)))
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
829 (let ((info (copy-alist sc-mail-info))
830 (attribs (copy-alist sc-attributions)))
831 (setq sc-mail-info nil
833 (regi-interpret sc-mail-glom-frame start end)
834 (if (null sc-mail-info)
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)
844 ;; let the user change mail field information
845 (defun sc-mail-field (field)
846 "Return the mail header field value associated with FIELD.
847 If there was no mail header with FIELD as its key, return the value of
848 `sc-mumble'. FIELD is case insensitive."
849 (or (cdr (assoc-string field sc-mail-info 'case-fold)) sc-mumble))
851 (defun sc-mail-field-query (arg)
852 "View the value of a mail field.
853 With `\\[universal-argument]', prompts for action on mail field.
854 Action can be one of: View, Modify, Add, or Delete."
856 (let* ((alist '(("view" . ?v) ("modify" . ?m) ("add" . ?a) ("delete" . ?d)))
857 (action (if (not arg) ?v (sc-ask alist)))
861 (setq key (completing-read
862 (concat (car (rassq action alist))
863 " information key: ")
865 (if (eq action ?a) nil 'noexit)
866 nil 'sc-mail-field-history))
869 (message "%s: %s" key (cdr (assoc key sc-mail-info))))
871 (setq sc-mail-info (delq (assoc key sc-mail-info) sc-mail-info)))
873 (let ((keyval (assoc key sc-mail-info)))
874 ;; first put initial value onto list if not already there
875 (if (not (member (cdr keyval)
876 sc-mail-field-modification-history))
877 (setq sc-mail-field-modification-history
878 (cons (cdr keyval) sc-mail-field-modification-history)))
879 (setcdr keyval (read-string
880 (concat key ": ") (cdr keyval)
881 'sc-mail-field-modification-history))))
883 (push (cons key (read-string (concat key ": "))) sc-mail-info))
887 ;; ======================================================================
890 (defvar sc-attribution-confirmation-history nil
891 "History for confirmation of attribution strings.")
892 (defvar sc-citation-confirmation-history nil
893 "History for confirmation of attribution prefixes.")
895 (defun sc-attribs-%@-addresses (from &optional delim)
896 "Extract the author's email terminus from email address FROM.
897 Match addresses of the style ``name%[stuff].'' when called with DELIM
898 of \"%\" and addresses of the style ``[stuff]name@[stuff]'' when
899 called with DELIM \"@\". If DELIM is nil or not provided, matches
900 addresses of the style ``name''."
901 (and (string-match (concat "[-[:alnum:]_.]+" delim) from 0)
904 (- (match-end 0) (if (null delim) 0 1)))))
906 (defun sc-attribs-!-addresses (from)
907 "Extract the author's email terminus from email address FROM.
908 Match addresses of the style ``[stuff]![stuff]...!name[stuff].''"
909 (let ((eos (length from))
910 (mstart (string-match "![-[:alnum:]_.]+\\([^-![:alnum:]_.]\\|$\\)"
912 (mend (match-end 0)))
914 (substring from (1+ mstart) (- mend (if (= mend eos) 0 1)))
917 (defun sc-attribs-<>-addresses (from)
918 "Extract the author's email terminus from email address FROM.
919 Match addresses of the style ``<name[stuff]>.''"
920 (and (string-match "<\\(.*\\)>" from)
921 (match-string 1 from)))
923 (defun sc-get-address (from author)
924 "Get the full email address path from FROM.
925 AUTHOR is the author's name (which is removed from the address)."
926 (let ((eos (length from)))
927 (if (string-match (concat "\\`\"?" (regexp-quote author)
929 (let ((address (substring from (match-end 0) eos)))
930 (if (and (= (aref address 0) ?<)
931 (= (aref address (1- (length address))) ?>))
932 (substring address 1 (1- (length address)))
934 (if (string-match "[-[:alnum:]!@%._]+" from 0)
935 (match-string 0 from)
939 (defun sc-attribs-emailname (from)
940 "Get the email terminus name from FROM."
942 (sc-attribs-%@-addresses from "%")
943 (sc-attribs-%@-addresses from "@")
944 (sc-attribs-!-addresses from)
945 (sc-attribs-<>-addresses from)
946 (sc-attribs-%@-addresses from)
947 (substring from 0 10)))
949 (defun sc-name-substring (string start end extend)
950 "Extract the specified substring of STRING from START to END.
951 EXTEND is the number of characters on each side to extend the
954 (let ((sos (+ start extend))
955 (eos (- end extend)))
956 (substring string sos
957 (or (string-match sc-titlecue-regexp string sos) eos)
960 (defun sc-attribs-extract-namestring (from)
961 "Extract the name string from FROM.
962 This should be the author's full name minus an optional title."
963 ;; FIXME: we probably should use mail-extract-address-components.
966 ;; If there is a <...> in the name,
967 ;; treat everything before that as the full name.
968 ;; Even if it contains parens, use the whole thing.
969 ;; On the other hand, we do look for quotes in the usual way.
970 (and (string-match " *<.*>" from 0)
972 (sc-name-substring from 0 (match-beginning 0) 0)))
973 (if (string-match "\".*\"" before-angles 0)
975 before-angles (match-beginning 0) (match-end 0) 1)
978 from (string-match "(.*)" from 0) (match-end 0) 1)
980 from (string-match "\".*\"" from 0) (match-end 0) 1)
982 from (string-match "\\([-.[:alnum:]_]+\\s +\\)+<" from 0)
984 (sc-attribs-emailname from))))
985 ;; strip off any leading or trailing whitespace
988 (eos (1- (length namestring))))
989 (while (and (<= bos eos)
990 (memq (aref namestring bos) '(32 ?\t)))
992 (while (and (> eos bos)
993 (memq (aref namestring eos) '(32 ?\t)))
995 (substring namestring bos (1+ eos))))))
997 (defun sc-attribs-chop-namestring (namestring)
998 "Convert NAMESTRING to a list of names.
999 example: (sc-attribs-chop-namestring \"John Xavier Doe\")
1000 => (\"John\" \"Xavier\" \"Doe\")"
1001 (if (string-match "\\([ \t]*\\)\\([^ \t._]+\\)\\([ \t]*\\)" namestring)
1002 (cons (match-string 2 namestring)
1003 (sc-attribs-chop-namestring (substring namestring (match-end 3)))
1006 (defun sc-attribs-strip-initials (namelist)
1007 "Extract the author's initials from the NAMELIST."
1011 (if (< 0 (length name))
1012 (substring name 0 1))))
1015 (defun sc-guess-attribution (&optional string)
1016 "Guess attribution string on current line.
1017 If attribution cannot be guessed, nil is returned. Optional STRING if
1018 supplied, is used instead of the line point is on in the current buffer."
1020 (string (or string (buffer-substring (line-beginning-position)
1021 (line-end-position))))
1024 (= start (or (string-match sc-citation-leader-regexp string start) -1))
1025 (setq start (match-end 0))
1026 (= start (or (string-match sc-citation-root-regexp string start) 1))
1027 (setq attribution (match-string 0 string)
1028 start (match-end 0))
1029 (= start (or (string-match sc-citation-delimiter-regexp string start) -1))
1030 (setq start (match-end 0))
1031 (= start (or (string-match sc-citation-separator-regexp string start) -1))
1034 (defun sc-attribs-filter-namelist (namelist)
1035 "Filter out noise in NAMELIST according to `sc-name-filter-alist'."
1036 (let ((elements (length namelist))
1038 keepers filtered-list)
1042 (setq position (1+ position))
1047 (let ((regexp (car filter))
1049 (if (and (string-match regexp name)
1050 (or (and (numberp pos)
1053 (= position (1- elements)))
1057 sc-name-filter-alist)
1059 (setq keepers (cons position keepers)))
1065 (setq filtered-list (cons (nth position namelist) filtered-list))
1070 (defun sc-attribs-chop-address (from)
1071 "Extract attribution information from FROM.
1072 This populates the `sc-attributions' with the list of possible attributions."
1073 (if (and (stringp from)
1074 (< 0 (length from)))
1075 (let* ((sc-mumble "")
1076 (namestring (sc-attribs-extract-namestring from))
1077 (namelist (sc-attribs-filter-namelist
1078 (sc-attribs-chop-namestring namestring)))
1079 (revnames (reverse (cdr namelist)))
1080 (firstname (car namelist))
1081 (midnames (reverse (cdr revnames)))
1082 (lastname (car revnames))
1083 (initials (sc-attribs-strip-initials namelist))
1084 (emailname (sc-attribs-emailname from))
1088 ;; put basic information
1090 ;; put middle names and build sc-author entry
1091 middlenames (mapconcat
1094 (let ((key-attribs (format "middlename-%d" n))
1095 (key-mail (format "sc-middlename-%d" n)))
1096 (push (cons key-attribs midname) sc-attributions)
1097 (push (cons key-mail midname) sc-mail-info)
1102 author (concat firstname " " middlenames (and midnames " ") lastname)
1104 sc-attributions (append
1106 (cons "firstname" firstname)
1107 (cons "lastname" lastname)
1108 (cons "emailname" emailname)
1109 (cons "initials" initials))
1111 sc-mail-info (append
1113 (cons "sc-firstname" firstname)
1114 (cons "sc-middlenames" middlenames)
1115 (cons "sc-lastname" lastname)
1116 (cons "sc-emailname" emailname)
1117 (cons "sc-initials" initials)
1118 (cons "sc-author" author)
1119 (cons "sc-from-address" (sc-get-address
1120 (sc-mail-field "from")
1122 (cons "sc-reply-address" (sc-get-address
1123 (sc-mail-field "reply-to")
1125 (cons "sc-sender-address" (sc-get-address
1126 (sc-mail-field "sender")
1131 ;; from string is empty
1132 (push (cons "sc-author" sc-default-author-name) sc-mail-info)))
1134 (defvar sc-attrib-or-cite nil
1135 "Used to toggle between attribution input or citation input.")
1137 (defun sc-toggle-fn ()
1138 "Toggle between attribution selection and citation selection.
1139 Only used during confirmation."
1141 (setq sc-attrib-or-cite (not sc-attrib-or-cite))
1142 (throw 'sc-reconfirm t))
1144 (defun sc-select-attribution ()
1145 "Select an attribution from `sc-attributions'.
1147 Variables involved in selection process include:
1148 `sc-preferred-attribution-list'
1149 `sc-use-only-preference-p'
1150 `sc-confirm-always-p'
1151 `sc-default-attribution'
1152 `sc-attrib-selection-list'.
1154 Runs the hook `sc-attribs-preselect-hook' before selecting an
1155 attribution and the hook `sc-attribs-postselect-hook' after making the
1156 selection but before querying is performed. During
1157 `sc-attribs-postselect-hook' the variable `citation' is bound to the
1158 auto-selected citation string and the variable `attribution' is bound
1159 to the auto-selected attribution string."
1160 (run-hooks 'sc-attribs-preselect-hook)
1161 (let ((query-p sc-confirm-always-p)
1162 attribution citation
1163 (attriblist sc-preferred-attribution-list))
1165 ;; first cruise through sc-preferred-attribution-list looking for
1166 ;; a match in either sc-attributions or sc-mail-info. if the
1167 ;; element is "sc-consult", then we have to do the alist
1168 ;; consultation phase
1170 (let* ((preferred (car attriblist)))
1172 ((string= preferred "sc-consult")
1173 ;; we've been told to consult the attribution vs. mail
1174 ;; header key alist. we do this until we find a match in
1175 ;; the sc-attrib-selection-list. if we do not find a match,
1176 ;; we continue scanning attriblist
1177 (let ((attrib (sc-scan-info-alist sc-attrib-selection-list)))
1180 (setq attriblist (cdr attriblist)))
1182 (setq attribution attrib
1185 (setq attribution (eval attrib))
1186 (if (stringp attribution)
1187 (setq attriblist nil)
1188 (setq attribution nil
1189 attriblist (cdr attriblist))))
1190 (t (error "%s did not evaluate to a string or list!"
1191 "sc-attrib-selection-list"))
1193 ((setq attribution (cdr (assoc preferred sc-attributions)))
1194 (setq attriblist nil))
1196 (setq attriblist (cdr attriblist)))
1199 ;; if preference was not found, we may use a secondary method to
1200 ;; find a valid attribution
1201 (if (and (not attribution)
1202 (not sc-use-only-preference-p))
1203 ;; secondary method tries to find a preference in this order
1209 ;; 6. first non-empty attribution in alist
1211 (or (cdr (assoc "sc-lastchoice" sc-attributions))
1212 (cdr (assoc "x-attribution" sc-attributions))
1213 (cdr (assoc "firstname" sc-attributions))
1214 (cdr (assoc "lastname" sc-attributions))
1215 (cdr (assoc "initials" sc-attributions))
1216 (cdr (car sc-attributions)))))
1218 ;; still couldn't find an attribution. we're now limited to using
1219 ;; the default attribution, but we'll force a query when this happens
1220 (if (not attribution)
1221 (setq attribution sc-default-attribution
1224 ;; create the attribution prefix
1225 (setq citation (sc-make-citation attribution))
1227 ;; run the post selection hook before querying the user
1228 (run-hooks 'sc-attribs-postselect-hook)
1230 ;; query for confirmation
1232 (let* ((query-alist (mapcar (function (lambda (entry)
1233 (list (cdr entry))))
1235 (minibuffer-local-completion-map
1236 sc-minibuffer-local-completion-map)
1237 (minibuffer-local-map sc-minibuffer-local-map)
1238 (initial attribution)
1239 (completer-disable t) ; in case completer.el is used
1241 (setq sc-attrib-or-cite nil) ; nil==attribution, t==citation
1243 (catch 'sc-reconfirm
1246 (if sc-attrib-or-cite
1248 "Enter citation prefix: "
1250 'sc-citation-confirmation-history)
1252 "Complete attribution name: "
1255 'sc-attribution-confirmation-history)
1258 (if sc-attrib-or-cite
1259 ;; since the citation was chosen, we have to guess at
1261 (setq citation choice
1262 attribution (or (sc-guess-attribution citation)
1265 (setq citation (sc-make-citation choice)
1266 attribution choice))
1269 ;; its possible that the user wants to downcase the citation and
1272 (setq citation (downcase citation)
1273 attribution (downcase attribution)))
1275 ;; set up mail info alist
1276 (let* ((ckey "sc-citation")
1277 (akey "sc-attribution")
1278 (ckeyval (assoc ckey sc-mail-info))
1279 (akeyval (assoc akey sc-mail-info)))
1281 (setcdr ckeyval citation)
1282 (push (cons ckey citation) sc-mail-info))
1284 (setcdr akeyval attribution)
1285 (push (cons akey attribution) sc-mail-info)))
1287 ;; set the sc-lastchoice attribution
1288 (let* ((lkey "sc-lastchoice")
1289 (lastchoice (assoc lkey sc-attributions)))
1291 (setcdr lastchoice attribution)
1292 (push (cons lkey attribution) sc-attributions)))
1296 ;; ======================================================================
1297 ;; filladapt hooks for supercite 3.1. you shouldn't need anything
1298 ;; extra to make gin-mode understand supercited lines. Even this
1299 ;; stuff might not be entirely necessary...
1301 (defun sc-cite-regexp (&optional root-regexp)
1302 "Return a regexp describing a Supercited line.
1303 The regexp is the concatenation of `sc-citation-leader-regexp',
1304 `sc-citation-root-regexp', `sc-citation-delimiter-regexp', and
1305 `sc-citation-separator-regexp'. If optional ROOT-REGEXP is supplied,
1306 use it instead of `sc-citation-root-regexp'."
1307 (concat sc-citation-leader-regexp
1308 (or root-regexp sc-citation-root-regexp)
1309 sc-citation-delimiter-regexp
1310 sc-citation-separator-regexp))
1312 (defun sc-make-citation (attribution)
1313 "Make a non-nested citation from ATTRIBUTION."
1314 (concat sc-citation-leader
1316 sc-citation-delimiter
1317 sc-citation-separator))
1319 (defun sc-setup-filladapt ()
1320 "Setup `filladapt-prefix-table' to handle Supercited paragraphs."
1321 (let* ((fa-sc-elt 'filladapt-supercite-included-text)
1322 (elt (rassq fa-sc-elt filladapt-prefix-table)))
1323 (if elt (setcar elt (sc-cite-regexp))
1324 (message "Filladapt doesn't seem to know about Supercite.")
1328 ;; ======================================================================
1329 ;; citing and unciting regions of text
1331 (defvar sc-fill-begin 1
1332 "Buffer position to begin filling.")
1333 (defvar sc-fill-line-prefix ""
1334 "Fill prefix of previous line")
1337 (defun sc-fill-if-different (&optional prefix)
1338 "Fill the region bounded by `sc-fill-begin' and point.
1339 Only fill if optional PREFIX is different than `sc-fill-line-prefix'.
1340 If `sc-auto-fill-region-p' is nil, do not fill region. If PREFIX is
1341 not supplied, initialize fill variables. This is useful for a regi
1342 `begin' frame-entry."
1344 (setq sc-fill-line-prefix ""
1345 sc-fill-begin (line-beginning-position))
1346 (if (and sc-auto-fill-region-p
1347 (not (string= prefix sc-fill-line-prefix)))
1348 (let ((fill-prefix sc-fill-line-prefix))
1349 (if (not (string= fill-prefix ""))
1350 (fill-region sc-fill-begin (line-beginning-position)))
1351 (setq sc-fill-line-prefix prefix
1352 sc-fill-begin (line-beginning-position))))
1356 (defun sc-cite-coerce-cited-line ()
1357 "Coerce a Supercited line to look like our style."
1358 (let* ((attribution (sc-guess-attribution))
1359 (regexp (sc-cite-regexp attribution))
1360 (prefix (sc-make-citation attribution)))
1361 (if (and attribution
1362 (looking-at regexp))
1367 (goto-char (match-end 0))
1368 (if (bolp) (forward-char -1))
1371 (sc-fill-if-different prefix)))
1374 (defun sc-cite-coerce-dumb-citer ()
1375 "Coerce a non-nested citation that's been cited with a dumb nesting citer."
1376 (delete-region (match-beginning 1) (match-end 1))
1378 (sc-cite-coerce-cited-line))
1380 (defun sc-guess-nesting (&optional string)
1381 "Guess the citation nesting on the current line.
1382 If nesting cannot be guessed, nil is returned. Optional STRING if
1383 supplied, is used instead of the line point is on in the current
1386 (string (or string (buffer-substring (line-beginning-position)
1387 (line-end-position))))
1390 (= start (or (string-match sc-citation-leader-regexp string start) -1))
1391 (setq start (match-end 0))
1392 (= start (or (string-match sc-citation-delimiter-regexp string start) -1))
1393 (setq nesting (match-string 0 string)
1394 start (match-end 0))
1395 (= start (or (string-match sc-citation-separator-regexp string start) -1))
1398 (defun sc-add-citation-level ()
1399 "Add a citation level for nested citation style w/ coercion."
1400 (let* ((nesting (sc-guess-nesting))
1401 (citation (make-string (1+ (length nesting))
1402 (string-to-char sc-citation-delimiter)))
1403 (prefix (concat sc-citation-leader citation sc-citation-separator)))
1404 (if (looking-at (sc-cite-regexp ""))
1405 (delete-region (match-beginning 0) (match-end 0)))
1407 (sc-fill-if-different prefix)))
1409 (defun sc-cite-line (&optional citation)
1410 "Cite a single line of uncited text.
1411 Optional CITATION overrides any citation automatically selected."
1412 (if sc-fixup-whitespace-p
1414 (let ((prefix (or citation
1415 (cdr (assoc "sc-citation" sc-mail-info))
1416 sc-default-attribution)))
1418 (sc-fill-if-different prefix))
1421 (defun sc-uncite-line ()
1422 "Remove citation from current line."
1423 (let ((cited (looking-at (sc-cite-regexp))))
1425 (delete-region (match-beginning 0) (match-end 0))))
1428 (defun sc-recite-line (regexp)
1429 "Remove citation matching REGEXP from current line and recite line."
1430 (let ((cited (looking-at (concat "^" regexp)))
1431 (prefix (cdr (assoc "sc-citation" sc-mail-info))))
1433 (delete-region (match-beginning 0) (match-end 0)))
1434 (insert (or prefix sc-default-attribution))
1435 (sc-fill-if-different prefix))
1438 ;; interactive functions
1439 (defun sc-cite-region (start end &optional confirm-p interactive)
1440 "Cite a region delineated by START and END.
1441 If optional CONFIRM-P is non-nil, the attribution is confirmed before
1442 its use in the citation string. This function first runs
1445 When called interactively, the optional arg INTERACTIVE is non-nil,
1446 and that means call `sc-select-attribution' too."
1447 (interactive "r\nP\np")
1449 (let ((frame (sc-scan-info-alist sc-cite-frame-alist))
1450 (sc-confirm-always-p (if confirm-p t sc-confirm-always-p)))
1451 (if (and frame (symbolp frame))
1452 (setq frame (symbol-value frame)))
1453 (or frame (setq frame sc-default-cite-frame))
1454 (run-hooks 'sc-pre-cite-hook)
1456 (sc-select-attribution))
1457 (regi-interpret frame start end)))
1459 (defun sc-uncite-region (start end)
1460 "Uncite a region delineated by START and END.
1461 First runs `sc-pre-uncite-hook'."
1464 (let ((frame (sc-scan-info-alist sc-uncite-frame-alist)))
1465 (if (and frame (symbolp frame))
1466 (setq frame (symbol-value frame)))
1467 (or frame (setq frame sc-default-uncite-frame))
1468 (run-hooks 'sc-pre-uncite-hook)
1469 (regi-interpret frame start end)))
1471 (defun sc-recite-region (start end)
1472 "Recite a region delineated by START and END.
1473 First runs `sc-pre-recite-hook'."
1475 (let ((sc-confirm-always-p t))
1476 (sc-select-attribution))
1478 (let ((frame (sc-scan-info-alist sc-recite-frame-alist)))
1479 (if (and frame (symbolp frame))
1480 (setq frame (symbol-value frame)))
1481 (or frame (setq frame sc-default-recite-frame))
1482 (run-hooks 'sc-pre-recite-hook)
1483 (regi-interpret frame start end)))
1486 ;; ======================================================================
1489 (defun sc-hdr (prefix field &optional sep return-nil-p)
1490 "Returns a concatenation of PREFIX and FIELD.
1491 If FIELD is not a string or is the empty string, the empty string will
1492 be returned. Optional third argument SEP is concatenated on the end if
1493 it is a string. Returns empty string, unless optional RETURN-NIL-P is
1495 (if (and (stringp field)
1496 (not (string= field "")))
1497 (concat prefix field (or sep ""))
1498 (and (not return-nil-p) "")))
1500 (defun sc-whofrom ()
1501 "Return the value of (sc-mail-field \"from\") or nil."
1502 (let ((sc-mumble nil))
1503 (sc-mail-field "from")))
1505 (defun sc-no-header ()
1506 "Does nothing. Use this instead of nil to get a blank header."
1509 (defun sc-no-blank-line-or-header()
1510 "Similar to `sc-no-header' except it removes the preceding blank line."
1513 (progn (forward-line -1)
1514 (or (= (point) (mail-header-end))
1515 (and (eq major-mode 'mh-letter-mode)
1516 (mh-in-header-p)))))
1517 (progn (forward-line)
1518 (let ((kill-lines-magic t))
1521 (defun sc-header-on-said ()
1522 "\"On <date>, <from> said:\" unless:
1523 1. the \"from\" field cannot be found, in which case nothing is inserted;
1524 2. the \"date\" field is missing in which case only the from part is printed."
1525 (let ((sc-mumble "")
1526 (whofrom (sc-whofrom)))
1528 (insert sc-reference-tag-string
1529 (sc-hdr "On " (sc-mail-field "date") ", ")
1530 whofrom " said:\n"))))
1532 (defun sc-header-inarticle-writes ()
1533 "\"In article <message-id>, <from> writes:\"
1534 Treats \"message-id\" and \"from\" fields similar to `sc-header-on-said'."
1535 (let ((sc-mumble "")
1536 (whofrom (sc-mail-field "from")))
1538 (insert sc-reference-tag-string
1539 (sc-hdr "In article " (sc-mail-field "message-id") ", ")
1540 whofrom " writes:\n"))))
1542 (defun sc-header-regarding-adds ()
1543 "\"Regarding <subject>; <from> adds:\"
1544 Treats \"subject\" and \"from\" fields similar to `sc-header-on-said'."
1545 (let ((sc-mumble "")
1546 (whofrom (sc-whofrom)))
1548 (insert sc-reference-tag-string
1549 (sc-hdr "Regarding " (sc-mail-field "subject") "; ")
1550 whofrom " adds:\n"))))
1552 (defun sc-header-attributed-writes ()
1553 "\"<sc-attribution>\" == <sc-author> <address> writes:
1554 Treats these fields in a similar manner to `sc-header-on-said'."
1555 (let ((sc-mumble "")
1556 (whofrom (sc-whofrom)))
1558 (insert sc-reference-tag-string
1559 (sc-hdr "\"" (sc-mail-field "sc-attribution") "\" == ")
1560 (sc-hdr "" (sc-mail-field "sc-author") " ")
1561 (or (sc-hdr "<" (sc-mail-field "sc-from-address") ">" t)
1562 (sc-hdr "<" (sc-mail-field "sc-reply-address") ">" t)
1566 (defun sc-header-author-writes ()
1567 "<sc-author> writes:"
1568 (let ((sc-mumble "")
1569 (whofrom (sc-whofrom)))
1571 (insert sc-reference-tag-string
1572 (sc-hdr "" (sc-mail-field "sc-author"))
1575 (defun sc-header-verbose ()
1576 "Very verbose, some say gross."
1577 (let ((sc-mumble "")
1578 (whofrom (sc-whofrom))
1579 (tag sc-reference-tag-string))
1581 (insert (sc-hdr (concat tag "On ") (sc-mail-field "date") ",\n")
1582 (or (sc-hdr tag (sc-mail-field "sc-author") "\n" t)
1583 (concat tag whofrom "\n"))
1584 (sc-hdr (concat tag "from the organization of ")
1585 (sc-mail-field "organization") "\n")
1586 (let ((rtag (concat tag "who can be reached at: ")))
1587 (or (sc-hdr rtag (sc-mail-field "sc-from-address") "\n" t)
1588 (sc-hdr rtag (sc-mail-field "sc-reply-address") "\n" t)
1591 (concat tag "(whose comments are cited below with \"")
1592 (sc-mail-field "sc-citation") "\"),\n")
1593 (sc-hdr (concat tag "had this to say in article ")
1594 (sc-mail-field "message-id") "\n")
1595 (sc-hdr (concat tag "in newsgroups ")
1596 (sc-mail-field "newsgroups") "\n")
1597 (sc-hdr (concat tag "concerning the subject of ")
1598 (sc-mail-field "subject") "\n")
1599 (sc-hdr (concat tag "(see ")
1600 (sc-mail-field "references")
1601 " for more details)\n")
1605 ;; ======================================================================
1608 (defconst sc-electric-bufname " *sc-erefs* "
1609 "Supercite electric reference mode's buffer name.")
1610 (defvar sc-eref-style 0
1611 "Current electric reference style.")
1613 (defun sc-valid-index-p (index)
1614 "Returns INDEX if it is a valid index into `sc-rewrite-header-list'.
1615 Otherwise returns nil."
1616 ;; a number, and greater than or equal to zero
1617 ;; less than or equal to the last index
1618 (and (natnump index)
1619 (< index (length sc-rewrite-header-list))
1622 (defun sc-eref-insert-selected (&optional nomsg)
1623 "Insert the selected reference header in the current buffer.
1624 Optional NOMSG, if non-nil, inhibits printing messages, unless an
1626 (let ((ref (nth sc-eref-style sc-rewrite-header-list)))
1630 (let ((lines (count-lines (point-min) (point-max))))
1631 (or nomsg (message "Ref header %d [%d line%s]: %s"
1633 (if (= lines 1) "" "s")
1637 "Symbol's function definition is void: %s (Header %d)"
1638 (car (cdr err)) sc-eref-style)
1643 (defun sc-electric-mode (&optional arg)
1645 Mode for viewing Supercite reference headers. Commands are:
1646 \n\\{sc-electric-mode-map}
1648 `sc-electric-mode' is not intended to be run interactively, but rather
1649 accessed through Supercite's electric reference feature. See
1650 `sc-insert-reference' for more details. Optional ARG is the initial
1651 header style to use, unless not supplied or invalid, in which case
1652 `sc-preferred-header-style' is used."
1654 (let ((info sc-mail-info))
1657 (or (sc-valid-index-p arg)
1658 (sc-valid-index-p sc-preferred-header-style)
1661 (get-buffer-create sc-electric-bufname)
1662 ;; set up buffer and enter command loop
1664 (save-window-excursion
1665 (pop-to-buffer sc-electric-bufname)
1666 (kill-all-local-variables)
1667 (let ((sc-mail-info info)
1668 (buffer-read-only t)
1669 (mode-name "SC Electric Refs")
1670 (major-mode 'sc-electric-mode))
1671 (use-local-map sc-electric-mode-map)
1672 (sc-eref-show sc-eref-style)
1673 (run-mode-hooks 'sc-electric-mode-hook)
1678 (sc-eref-insert-selected))
1679 (kill-buffer sc-electric-bufname)
1682 ;; functions for electric reference mode
1683 (defun sc-eref-show (index)
1684 "Show reference INDEX in `sc-rewrite-header-list'."
1685 (let ((msg "No %ing reference headers in list.")
1686 (last (length sc-rewrite-header-list)))
1689 ((sc-valid-index-p index) index)
1691 (if sc-electric-circular-p
1693 (progn (error msg "preced") 0)))
1695 (if sc-electric-circular-p
1697 (progn (error msg "follow") (1- last))))
1700 (set-buffer sc-electric-bufname)
1701 (let ((buffer-read-only nil))
1703 (goto-char (point-min))
1704 (sc-eref-insert-selected)
1705 ;; now shrink the window to just contain the electric reference
1707 (let ((hdrlines (count-lines (point-min) (point-max)))
1708 (winlines (1- (window-height))))
1709 (if (/= hdrlines winlines)
1710 (if (> hdrlines winlines)
1711 ;; we have to enlarge the window
1712 (enlarge-window (- hdrlines winlines))
1713 ;; we have to shrink the window
1714 (shrink-window (- winlines (max hdrlines window-min-height)))
1718 (defun sc-eref-next ()
1719 "Display next reference in other buffer."
1721 (sc-eref-show (1+ sc-eref-style)))
1723 (defun sc-eref-prev ()
1724 "Display previous reference in other buffer."
1726 (sc-eref-show (1- sc-eref-style)))
1728 (defun sc-eref-setn ()
1729 "Set reference header selected as preferred."
1731 (setq sc-preferred-header-style sc-eref-style)
1732 (message "Preferred reference style set to header %d." sc-eref-style))
1734 (defun sc-eref-goto (refnum)
1735 "Show reference style indexed by REFNUM.
1736 If REFNUM is an invalid index, don't go to that reference and return
1738 (interactive "NGoto Reference: ")
1739 (if (sc-valid-index-p refnum)
1740 (sc-eref-show refnum)
1741 (error "Invalid reference: %d. (Range: [%d .. %d])"
1742 refnum 0 (1- (length sc-rewrite-header-list)))
1745 (defun sc-eref-jump ()
1746 "Set reference header to preferred header."
1748 (sc-eref-show sc-preferred-header-style))
1750 (defun sc-eref-abort ()
1751 "Exit from electric reference mode without inserting reference."
1753 (setq sc-eref-style nil)
1754 (exit-recursive-edit))
1756 (defun sc-eref-exit ()
1757 "Exit from electric reference mode and insert selected reference."
1759 (exit-recursive-edit))
1761 (defun sc-insert-reference (arg)
1762 "Insert, at point, a reference header in the body of the reply.
1763 Numeric ARG indicates which header style from `sc-rewrite-header-list'
1764 to use when rewriting the header. No supplied ARG indicates use of
1765 `sc-preferred-header-style'.
1767 With just `\\[universal-argument]', electric reference insert mode is
1768 entered, regardless of the value of `sc-electric-references-p'. See
1769 `sc-electric-mode' for more information."
1773 (let ((preference (or (sc-valid-index-p arg)
1774 (sc-valid-index-p sc-preferred-header-style)
1775 sc-preferred-header-style
1777 (if sc-electric-references-p
1778 (sc-electric-mode preference)
1779 (sc-eref-insert-selected t)
1783 ;; ======================================================================
1784 ;; variable toggling
1786 (defun sc-raw-mode-toggle ()
1787 "Toggle, in one fell swoop, two important SC variables:
1788 `sc-fixup-whitespace-p' and `sc-auto-fill-region-p'"
1790 (setq sc-fixup-whitespace-p (not sc-fixup-whitespace-p)
1791 sc-auto-fill-region-p (not sc-auto-fill-region-p))
1792 (force-mode-line-update))
1794 (defun sc-toggle-var (variable)
1795 "Boolean toggle VARIABLE's value.
1796 VARIABLE must be a bound symbol. nil values change to t, non-nil
1797 values are changed to nil."
1798 (message "%s changed from %s to %s"
1799 variable (symbol-value variable)
1800 (set variable (not (symbol-value variable)))))
1802 (defun sc-set-variable (var)
1803 "Set the Supercite VARIABLE.
1804 This function mimics `set-variable', except that the variable to set
1805 is determined non-interactively. The value is queried for in the
1806 minibuffer exactly the same way that `set-variable' does it.
1808 You can see the current value of the variable when the minibuffer is
1809 querying you by typing `C-h'. Note that the format is changed
1810 slightly from that used by `set-variable' -- the current value is
1811 printed just after the variable's name instead of at the bottom of the
1813 (let* ((minibuffer-help-form '(funcall myhelp))
1816 (with-output-to-temp-buffer "*Help*"
1819 (let ((print-length 20))
1820 (princ "\t(Current value: ")
1821 (prin1 (symbol-value var))
1823 (princ "\n\nDocumentation:\n")
1824 (princ (substring (documentation-property
1826 'variable-documentation)
1828 (with-current-buffer standard-output
1831 (set var (eval-minibuffer (format "Set %s to value: " var)))))
1833 (defmacro sc-toggle-symbol (rootname)
1834 `(defun ,(intern (concat "sc-T-" rootname)) ()
1836 (sc-toggle-var ',(intern (concat "sc-" rootname "-p")))))
1838 (defmacro sc-setvar-symbol (rootname)
1839 `(defun ,(intern (concat "sc-S-" rootname)) ()
1841 (sc-set-variable ',(intern (concat "sc-" rootname)))))
1843 (sc-toggle-symbol "confirm-always")
1844 (sc-toggle-symbol "downcase")
1845 (sc-toggle-symbol "electric-references")
1846 (sc-toggle-symbol "auto-fill-region")
1847 (sc-toggle-symbol "mail-nuke-blank-lines")
1848 (sc-toggle-symbol "nested-citation")
1849 (sc-toggle-symbol "electric-circular")
1850 (sc-toggle-symbol "use-only-preferences")
1851 (sc-toggle-symbol "fixup-whitespace")
1853 (sc-setvar-symbol "preferred-attribution-list")
1854 (sc-setvar-symbol "preferred-header-style")
1855 (sc-setvar-symbol "mail-nuke-mail-headers")
1856 (sc-setvar-symbol "mail-header-nuke-list")
1857 (sc-setvar-symbol "cite-region-limit")
1859 (defun sc-T-describe ()
1862 Supercite provides a number of key bindings which simplify the process
1863 of setting or toggling certain variables controlling its operation.
1865 Note on function names in this list: all functions of the form
1866 `sc-S-<name>' actually call `sc-set-variable' on the corresponding
1867 `sc-<name>' variable. All functions of the form `sc-T-<name>' call
1868 `sc-toggle-var' on the corresponding `sc-<name>-p' variable.
1872 (describe-function 'sc-T-describe))
1875 ;; ======================================================================
1876 ;; published interface to mail and news readers
1878 (define-minor-mode sc-minor-mode
1879 "Supercite minor mode."
1881 :lighter (" SC" (sc-auto-fill-region-p
1882 (":f" (sc-fixup-whitespace-p "w"))
1883 (sc-fixup-whitespace-p ":w")))
1884 :keymap `((,sc-mode-map-prefix . ,sc-mode-map)))
1887 (defun sc-cite-original ()
1888 "Workhorse citing function which performs the initial citation.
1889 This is callable from the various mail and news readers' reply
1890 function according to the agreed upon standard. See the associated
1891 info node `(SC)Top' for more details.
1892 `sc-cite-original' does not do any yanking of the
1893 original message but it does require a few things:
1895 1) The reply buffer is the current buffer.
1897 2) The original message has been yanked and inserted into the
1900 3) Verbose mail headers from the original message have been
1901 inserted into the reply buffer directly before the text of the
1904 4) Point is at the beginning of the verbose headers.
1906 5) Mark is at the end of the body of text to be cited.
1908 For Emacs 19's, the region need not be active (and typically isn't
1909 when this function is called. Also, the hook `sc-pre-hook' is run
1910 before, and `sc-post-hook' is run after the guts of this function."
1911 (run-hooks 'sc-pre-hook)
1917 ;; grab point and mark since the region is probably not active when
1918 ;; this function gets automatically called. we want point to be a
1919 ;; mark so any deleting before point works properly
1920 (let* ((zmacs-regions nil) ; for XEemacs
1921 (mark-active t) ; for Emacs
1922 (point (point-marker))
1923 (mark (copy-marker (mark-marker))))
1925 ;; make sure point comes before mark, not all functions are
1932 ;; first process mail headers, and populate sc-mail-info
1933 (sc-mail-process-headers point mark)
1935 ;; now get possible attributions
1936 (sc-attribs-chop-address (or (sc-mail-field "from")
1937 (sc-mail-field "reply")
1938 (sc-mail-field "reply-to")
1939 (sc-mail-field "sender")))
1940 ;; select the attribution
1941 (sc-select-attribution)
1943 ;; cite the region, but first check the value of sc-cite-region-limit
1944 (let ((linecnt (count-lines point mark)))
1945 (and sc-cite-region-limit
1946 (if (or (not (numberp sc-cite-region-limit))
1947 (<= linecnt sc-cite-region-limit))
1949 ;; cite the region and insert the header rewrite
1950 (sc-cite-region point mark)
1952 (let ((sc-eref-style (or sc-preferred-header-style 0)))
1953 (if sc-electric-references-p
1954 (sc-electric-mode sc-eref-style)
1955 (sc-eref-insert-selected t))))
1958 "Region not cited. %d lines exceeds sc-cite-region-limit: %d"
1959 linecnt sc-cite-region-limit))))
1961 ;; finally, free the point-marker
1962 (set-marker point nil)
1963 (set-marker mark nil)
1965 (run-hooks 'sc-post-hook))
1968 ;; ======================================================================
1969 ;; bug reporting and miscellaneous commands
1971 (defun sc-open-line (arg)
1972 "Like `open-line', but insert the citation prefix at the front of the line.
1973 With numeric ARG, inserts that many new lines."
1976 (let ((start (point))
1977 (prefix (or (progn (beginning-of-line)
1978 (if (looking-at (sc-cite-regexp))
1990 (defun sc-insert-citation (arg)
1991 "Insert citation string at beginning of current line if not already cited.
1992 With `\\[universal-argument]' insert citation even if line is already
1997 (if (or (not (looking-at (sc-cite-regexp)))
1998 (looking-at "^[ \t]*$")
2000 (insert (sc-mail-field "sc-citation"))
2001 (error "Line is already cited"))))
2003 (defun sc-version (message)
2004 "Echo the current version of Supercite in the minibuffer.
2005 If MESSAGE is non-nil (interactively, with no prefix argument),
2006 inserts the version string in the current buffer instead."
2007 (interactive (not current-prefix-arg))
2008 (let ((verstr (format "Using Supercite.el %s" sc-version)))
2011 (insert "`sc-version' says: " verstr))))
2013 (defun sc-describe ()
2015 Supercite is a package which provides a flexible mechanism for citing
2016 email and news replies. Please see the associated texinfo file for
2017 more information. Info node `(SC)Top'."
2019 (describe-function 'sc-describe))
2021 (defun sc-submit-bug-report ()
2022 "Submit a bug report on Supercite via mail."
2026 (y-or-n-p "Do you want to submit a report on Supercite? ")
2027 (reporter-submit-bug-report
2029 (concat "Supercite version " sc-version)
2031 'sc-attrib-selection-list
2032 'sc-auto-fill-region-p
2033 'sc-blank-lines-after-headers
2035 'sc-citation-delimiter
2036 'sc-citation-separator
2037 'sc-citation-leader-regexp
2038 'sc-citation-root-regexp
2039 'sc-citation-nonnested-root-regexp
2040 'sc-citation-delimiter-regexp
2041 'sc-citation-separator-regexp
2042 'sc-cite-region-limit
2043 'sc-confirm-always-p
2044 'sc-default-attribution
2045 'sc-default-author-name
2047 'sc-electric-circular-p
2048 'sc-electric-references-p
2049 'sc-fixup-whitespace-p
2050 'sc-mail-warn-if-non-rfc822-p
2052 'sc-name-filter-alist
2053 'sc-nested-citation-p
2054 'sc-nuke-mail-headers
2055 'sc-nuke-mail-header-list
2056 'sc-preferred-attribution-list
2057 'sc-preferred-header-style
2058 'sc-reference-tag-string
2059 'sc-rewrite-header-list
2061 'sc-use-only-preference-p
2066 (provide 'supercite)
2067 (run-hooks 'sc-load-hook)
2069 ;; arch-tag: a5d5bfa6-3bd5-4414-8c65-0afc83e45cd3
2070 ;;; supercite.el ends here