[comp.emacs] Super Mail Yank

warsaw@cme.nbs.gov (Barry A. Warsaw) (05/23/89)

Fellow GNUheads,

A few weeks back there was a lot of traffic concerning yanking RMAIL
message buffers into the mail reply buffer.  There were postings of
code and some discussion of preferred methods of citation.  At the
time I grabbed every bit of code that came across the net on this
subject and checked them out.  Many had some interesting
features/ideas but I thought that none of them had gone quite far
enough.  Also, I'd been looking for some little project to sink my
novice elisp programming teeth into, and I had just gotten ahold of
the GNU elisp manual, so I figured this was as good a thing to hack on
as any.  Let me describe briefly what this does then give you the code
to play with.  There's a bit more discussion in the code.  The package
is called `superyank'.

Basically there are two citation (or attribution) methods that
superyank employs. The first is called nested-citations and a series
of replies using nested-citations would look like this:

>> My name is John and I wrote the first message.
> My name is Jane and I replied to John's original message.
My name is Jack and I replied to both John and Jane's message.

Most people should be familiar with this method.  The second method is
called non-nested-citations and the above exchange would look like
this using non-nested-citations:

John> My name is John and I wrote the first message.
Jane> My name is Jane and I replied to John's original message.
My name is Jack and I replied to both John and Jane's message.

Code was posted previously that cites lines like this.  Superyank is
different in that it will automatically scan the "From: " line in the
original message to come up with a list of attribution strings (the
"Jane> " part) that you can choose from or override.  For example, if
the From: line looks like this:

From: jdoe@computer.some.where.com (John Xavier Doe)

Your choices for attribution would be:
1) "John"    -- the first name
2) "Xavier"  -- the middle name
3) "Doe"     -- the last name
4) "JXD"     -- the initials
5) "jdoe"    -- the email user name

Again, you can use any of these attribution strings or override them
with your own choice.  You can specify a preferred attribution string
(i.e. first names or initials, etc) and have superyank automatically
cite all lines or query for confirmation first.  Superyank will also
auto fill all paragraphs if you want, or you can manually fill
paragraphs.  Superyank is fairly intellegent in that it can properly
scan and of the following From: lines:

From: jdoe@computer.some.where.com (John Xavier Doe)
From: some!where!computer!jdoe (John Xavier Doe)
From: jdoe%some!where@computer.com (John Xavier Doe)
From: jdoe@computer.some.where.com (John X. Doe -- Hacker Extraordinaire)
From: jdoe@computer.some.where.com (John Xavier-Doe -- Great Guy)
From: John X. Doe, jdoe@computer.some.where.com
From: "John X. Doe", jdoe@computer.some.where.com

I have tested superyank's scanning capabilities against all the
different styles of From: lines that I've seen.  Of course, I can't
promise it will properly scan all From: lines.  Also, superyank has
been tested with both RMAIL replies and gnus replies.

Anyway, you can get more information from checking the code.  Also,
try "C-h f mail-yank-original" to get a more detailed description.

As always, send questions, comments, etc. to me, the author:

NAME: Barry A. Warsaw                USMAIL: National Institute of Standards
TELE: (301) 975-3460                         and Technology (formerly NBS)
UUCP: {...}!uunet!cme-durer!warsaw           Rm. B-124, Bldg. 220
ARPA: warsaw@cme.nbs.gov                     Gaithersburg, MD 20899

================== cut here ============================================
;; superyank.el
;;
;; Inserts the message being replied to with various user controlled
;; citation styles.
;;

;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY.  No author or distributor
;; accepts responsibility to anyone for the consequences of using it
;; or for whether it serves any particular purpose or works at all.
;; Refer to the GNU General Public License for full details.

;; Everyone is granted permission to copy, modify and redistribute
;; this file, but only under the conditions described in the
;; GNU General Public License.   A copy of this license is
;; supposed to have been given to you along with GNU Emacs so you
;; can know your rights and responsibilities.  It should be in a
;; file named COPYING.  Among other things, the copyright notice
;; and this notice must be preserved on all copies.

;; NAME: Barry A. Warsaw                USMAIL: National Institute of Standards
;; TELE: (301) 975-3460                         and Technology (formerly NBS)
;; UUCP: {...}!uunet!cme-durer!warsaw           Rm. B-124, Bldg. 220
;; ARPA: warsaw@cme.nbs.gov                     Gaithersburg, MD 20899
;;   or: warsaw@cme-durer.arpa
;;
;; Modification history:
;;
;; modified: 22-May-1989 baw (documentation)
;; modified:  8-May-1989 baw (auto filling of regions)
;; modified:  1-May-1989 baw (documentation)
;; modified: 27-Apr-1989 baw (new preference scheme)
;; modified: 24-Apr-1989 baw (remove gnus headers, attrib scheme, cite lines)
;; modified: 19-Apr-1989 baw (cite key, fill p, yank region, naming scheme)
;; modified: 12-Apr-1989 baw (incorp other mail yank features seen on net)
;; created : 16-Feb-1989 baw (mod vanilla fn indent-rigidly mail-yank-original)
;;
;; Though I wrote this package basically from scratch, as an elisp
;; learning exercise, it was inspired by postings of similar packages to
;; the gnu.emacs newsgroup over the past month or so.
;;
;; Here's a brief history of how this package developed:
;;
;; I as well as others on the net were pretty unhappy about the way emacs
;; cited replies with the tab or 4 spaces.  It looked ugly and made it hard
;; to distinguish between original and cited lines.  I hacked on the function
;; yank-original to at least give the user the ability to define the citation
;; character.  I posted this simple hack, and others did as well.  The main
;; difference between mine and others was that a space was put after the
;; citation string on on new citations, but not after previously cited lines:
;;
;; >>John wrote this originally
;; > Jane replied to that
;;
;; Then Martin Neitzel posted some code that he developed, derived in part
;; from code that Ashwin Ram posted previous to that.  In Martin's
;; posting, he introduced a new, and (IMHO) superior, citation style,
;; eliminating nested citations.  Yes, I want to join the Small-But-Growing-
;; Help-Stamp-Out-Nested-Citation-Movement! So.
;;
;; But Martin's code simply askes the user for the citation string (here
;; after called the `attribution' string), and I got to thinking, "it wouldn't
;; be that difficult to automate that part."  So I started hacking this out.
;; It proved to be not as simple as I first thought.  But anyway here it
;; is.  See the wish list below for future plans (if I have time).
;;
;; Type "C-h f mail-yank-original" after this package is loaded to get a
;; description of what it does and the variables that control it.
;;
;; ======================================================================
;;
;; Changes wish list
;;
;; 1) C-x C-s yanks a region from the RMAIL buffer instead of the
;;    whole buffer
;;
;; 2) attribution (citation) key at the top and a more informative, but
;;    concise rewritten header
;;
;; ======================================================================
;;
;; packages that are required for superyank to work
;;
(require 'sendmail)

;;
;; ======================================================================
;;
;; this section defines variables that control the operation of
;; super-mail-yank.  Most of these are described in the comment section
;; as well as the DOCSTRING.
;;

;;
;; ----------------------------------------------------------------------
;;
;; this variable holds the default author's name for citations
;;
(defvar sy-default-attribution "Anon"
  "String that describes attribution to unknown person. This string
should not contain the citation string.")

;;
;; ----------------------------------------------------------------------
;;
;; string used as an end delimiter for both nested and non-nested citations
;;
(defvar sy-citation-string ">"
  "String to use as an end-delimiter for citations.  This string is
used in both nested and non-nested citations.  For best results, use a
single character with no trailing space.  Most commonly used string
is: \">\.")

;;
;; ----------------------------------------------------------------------
;;
;; variable controlling citation type, nested or non-nested
;;
(defvar sy-nested-citation nil
  "Non-nil uses nested citations, nil uses non-nested citations.
Nested citations are of the style:

I wrote this
> He wrote this
>> She replied to something he wrote

Non-nested citations are of the style:

I wrote this
John> He wrote this
Jane> She originally wrote this")


;;
;; ----------------------------------------------------------------------
;;
;; regular expression that matches existing citations
;;
(defvar sy-cite-regexp "[a-zA-Z0-9]*>"
  "Regular expression that describes how an already cited line in an
article begins.  The regexp is only used at the beginning of a line,
so it doesn't need to begin with a '^'.")

;;
;; ----------------------------------------------------------------------
;;
;; regular expression that delimits names from titles in the field that
;; looks like:   (John X. Doe -- Computer Hacker Extraordinaire)
;;
(defvar sy-titlecue-regexp "\\s +-+\\s +"

  "Regular expression that delineates names from titles in the name
field.  Often, people will set up their name field to look like this:

(John Xavier Doe -- Computer Hacker Extraordinaire)

Set to nil to treat entire field as a name.")

;;
;; ----------------------------------------------------------------------
;;
;;
(defvar sy-preferred-attribution 2

  "This is an integer indicating what the user's preference is in
attribution style, based on the following key:

0: email address name is preferred
1: initials are preferred
2: first name is preferred
3: last name is preferred

The value of this variable may also be greater than 3, which would
allow you to prefer the 2nd through nth - 1 name.  If the preferred
attribution is nil or the empty string, then the secondary preferrence
will be the first name.  After that, the entire name alist is search
until a non-empty, non-nil name is found.  If no such name is found,
then the user is either queried or the default attribution string is
used depending on the value of sy-confirm-always.

Examples:

assume the from: line looks like this:

from: doe@computer.some.where.com (John Xavier Doe)

The following preferences would return these strings:

0: \"doe\"
1: \"JXD\"
2: \"John\"
3: \"Doe\"
4: \"Xavier\"

anything else would return \"John\".")

;;
;; ----------------------------------------------------------------------
;;
(defvar sy-confirm-always t
  "If t, always confirm attribution string before inserting into
buffer.")


;;
;; ----------------------------------------------------------------------
;;
;; non-nil means downcase the author's name string
;;
(defvar sy-downcase nil
  "Non-nil means downcase the author's name string.")

;;
;; ----------------------------------------------------------------------
;;
;; controls removal of leading white spaces
;;
(defvar sy-left-justify nil
  "If non-nil, delete all leading white space before citing.")

;;
;; ----------------------------------------------------------------------
;;
;; controls auto filling of region
;;
(defvar sy-auto-fill-region nil
  "If non-nil, automatically fill each paragraph that is cited.  If
nil, do not auto fill each paragraph.")

;;
;; ----------------------------------------------------------------------
;;
;; The new citation style means we can clean out other headers in addition
;; to those previously cleaned out.  Anyway, we create our own headers.
;; Also, we want to clean out any headers that gnus puts in.  Add to this
;; for other mail or news readers you may be using.
;;
(setq mail-yank-ignored-headers "^via:\\|^mail-from:\\|^origin:\\|^status:\\|^remailed\\|^received:\\|^[a-z-]*message-id:\\|^summary-line:\\|^to:\\|^cc:\\|^subject:\\|^[in-]?reply-to:\\|^return-path:\\|^date:\\|^from:\\|^newsgroup[s]?:\\|^organization:\\|^keywords:\\|^distribution:\\|reply-path:")


;;
;; ======================================================================
;;
;; This section contains primitive functions used in the schemes.  They
;; extract name fields from various parts of the "from:" field based on
;; the control variables described above.
;;
;; Some will use recursion to pick out the correct namefield in the namestring
;; or the list of initials.  These functions all scan a string that contains
;; the name, ie:  "John Xavier Doe".  There is no limit on the number of names
;; in the string.  Also note that all white spaces are basically ignored and
;; are stripped from the returned strings, and titles are ignored if 
;; sy-titlecue-regexp is set to non-nil.
;;
;; Others will use methods to try to extract the name from the email
;; address of the originator.  The types of addresses readable are
;; described above.

;;
;; ----------------------------------------------------------------------
;;
;; try to extract the name from an email address of the form
;; name%[stuff]
;;
;; Unlike the get-name functions above, these functions operate on the
;; buffer instead of a supplied name-string.
;;
(defun sy-%-style-address ()
  (beginning-of-line)
  (buffer-substring
   (progn (re-search-forward "%" (point-max) t)
		  (if (not (bolp)) (forward-char -1))
		  (point))
   (progn (re-search-backward "^\\|[^a-zA-Z0-9]")
		  (point))))

;;
;; ----------------------------------------------------------------------
;;
;; try to extract names from addresses with the form:
;; [stuff]name@[stuff]
;;
(defun sy-@-style-address ()
  (beginning-of-line)
  (buffer-substring
   (progn (re-search-forward "@" (point-max) t)
		  (if (not (bolp)) (forward-char -1))
		  (point))
   (progn (re-search-backward "^\\|[^a-zA-Z0-0]")
		  (if (not (bolp)) (forward-char 1))
		  (point))))

;;
;; ----------------------------------------------------------------------
;;
;; try to extract the name from addresses with the form:
;; [stuff]![stuff]...!name[stuff]
;;
(defun sy-!-style-address ()
  (beginning-of-line)
  (buffer-substring
   (progn (while (re-search-forward "!" (point-max) t))
		  (point))
   (progn (re-search-forward "[^a-zA-Z0-9]\\|$")
		  (if (not (eolp)) (forward-char -1))
		  (point))))

;;
;; ----------------------------------------------------------------------
;;
;; using the different email name schemes, try each one until you get a
;; non-nil entry
;;
(defun sy-get-emailname ()
  (let ((en1 (sy-%-style-address))
		(en2 (sy-@-style-address))
		(en3 (sy-!-style-address)))
	(cond
	 ((not (string-equal en1 "")) en1)
	 ((not (string-equal en2 "")) en2)
	 ((not (string-equal en3 "")) en3)
	 (t ""))))

;;
;; ----------------------------------------------------------------------
;;
;; returns the "car" of the namestring, really the first namefield
;;
;; (sy-string-car "John Xavier Doe")
;; => "John"
;;
(defun sy-string-car (namestring)
  (substring namestring
	     (progn (string-match "\\s *" namestring) (match-end 0))
	     (progn (string-match "\\s *\\S +" namestring) (match-end 0))))

;;
;; ----------------------------------------------------------------------
;;
;; returns the "cdr" of the namestring, really the whole string from
;; after the first name field to the end of the string.
;;
;; (sy-string-cdr "John Xavier Doe")
;; => "Xavier Doe"
;;
(defun sy-string-cdr (namestring)
  (substring namestring
	     (progn (string-match "\\s *\\S +\\s *" namestring)
		    (match-end 0))))

;;
;; ----------------------------------------------------------------------
;;
;; convert a namestring to a list of namefields
;;
;; (sy-namestring-to-list "John Xavier Doe")
;; => ("John" "Xavier" "Doe")
;;
(defun sy-namestring-to-list (namestring)
  (if (not (string-match namestring ""))
      (append (list (sy-string-car namestring))
	      (sy-namestring-to-list (sy-string-cdr namestring)))))

;;
;; ----------------------------------------------------------------------
;;
;; strip the initials from each item in the list and return a string
;; that is the concatenation of the initials
;;
(defun sy-strip-initials (raw-nlist)
  (if (not raw-nlist)
      nil
    (concat (substring (car raw-nlist) 0 1)
	    (sy-strip-initials (cdr raw-nlist)))))
		  

;;
;; ----------------------------------------------------------------------
;;
;; using the namestring, build a list which is in the following order
;;
;; (email, initials, firstname, lastname, name1, name2, name3 ... nameN-1)
;;
(defun sy-build-ordered-namelist (namestring)
  (let* ((raw-nlist (sy-namestring-to-list namestring))
	 (initials (sy-strip-initials raw-nlist))
	 (firstname (car raw-nlist))
	 (revnames (reverse (cdr raw-nlist)))
	 (lastname (car revnames))
	 (midnames (reverse (cdr revnames)))
	 (emailnames (sy-get-emailname)))
    (append (list emailnames)
	    (list initials)
	    (list firstname)
	    (list lastname)
	    midnames)))

;;
;; ----------------------------------------------------------------------
;;
;; Query the user for the attribution string.  Supply sy-default-attribution
;; as the default choice.
;;
(defun sy-query-for-attribution ()
  (concat 
   (let* ((prompt (concat "Enter attribution string: (default "
			  sy-default-attribution
			  ") "))
	  (query (read-input prompt))
	  (attribution (if (string-equal query "")
			   sy-default-attribution
			 query)))
     (if sy-downcase
	 (downcase attribution)
       attribution))
   sy-citation-string))
   

;;
;; ----------------------------------------------------------------------
;;
;; parse the current line for the namestring
;;
(defun sy-get-namestring ()
  (save-restriction
    (beginning-of-line)
    (if (re-search-forward "(.*)" (point-max) t)
	(let ((start (progn
		       (beginning-of-line)
		       (re-search-forward "\\((\\s *\\)\\|$" (point-max) t)
		       (point)))
	      (end (progn
		     (re-search-forward
		      (concat "\\(\\s *\\()\\|" sy-titlecue-regexp "\\)\\)\\|$")
		      (point-max) t)
		     (point))))
	  (narrow-to-region start end)
	  (let ((start (progn
			 (beginning-of-line)
			 (point)))
		(end (progn
		       (end-of-line)
		       (re-search-backward
			(concat "\\s *\\()\\|" sy-titlecue-regexp "\\)$")
			(point-min) t)
		       (point))))
	    (buffer-substring start end)))
      (let ((start (progn
		     (beginning-of-line)
		     (re-search-forward "^\"*")
		     (point)))
	    (end (progn
		   (re-search-forward "\\(\\s *[a-zA-Z0-9\\.]+\\)*"
				      (point-max) t)
		   (point))))
	(buffer-substring start end)))))


;;
;; ----------------------------------------------------------------------
;;
;; scan the nlist and return the integer pointing to the first legal
;; non-empty namestring.  Returns the integer pointing to the index
;; in the nlist of the preferred namestring, or nil if no legal
;; non-empty namestring could be found.
;;
(defun sy-return-preference-n (nlist)
  (let ((p sy-preferred-attribution)
	(exception nil))
    ;;
    ;; check to be sure the index is not out-of-bounds
    ;;
    (cond
     ((< p 0) (setq p 2) (setq exception t))
     ((not (nth p nlist)) (setq p 2) (setq exception t)))
    ;;
    ;; check to be sure that the explicit preference is not empty
    ;;
    (if (string-equal (nth p nlist) "")
	(progn (setq p 0)
	       (setq exception t)))
    ;;
    ;; find the first non-empty namestring
    ;;
    (while (and (nth p nlist)
		(string-equal (nth p nlist) ""))
      (setq exception t)
      (setq p (+ p 1)))
    ;;
    ;; return the preference index if non-nil, otherwise nil
    ;;
    (if (or (and exception sy-use-only-preference)
	    (not (nth p nlist)))
	nil
      p)))

;;
;;
;; ----------------------------------------------------------------------
;;
;; rebuild the nlist into an alist for completing-read.  Use as a guide
;; the index of the preferred name field.  Get the actual preferred
;; name field base on other factors (see above).  If no actual preferred
;; name field is found, then query the user for the attribution string.
;;
;; also note that the nlist is guaranteed to be non-empty.  At the very
;; least it will consist of 4 empty strings ("" "" "" "")
;; 
(defun sy-nlist-to-alist (nlist)
  (let ((preference (sy-return-preference-n nlist))
	alist
	(n 0))
    ;;
    ;; check to be sure preference is not nil
    ;;
    (if (not preference)
	(setq alist (list (cons (sy-query-for-attribution) nil)))
      ;;
      ;; preference is non-nil
      ;;
      (setq alist (list (cons (nth preference nlist) nil)))
      (while (nth n nlist)
	(if (= n preference) nil
	  (setq alist (append alist (list (cons (nth n nlist) nil)))))
	(setq n (+ n 1))))
    alist))



;;
;; ----------------------------------------------------------------------
;;
;; confirm if desired after the alist has been built
;;
(defun sy-get-attribution (alist)
  (concat
   ;;
   ;; check to see if nested citations are to be used
   ;;
   (if sy-nested-citation
       ""
     ;;
     ;; check to see if confirmation is needed
     ;; if not, just return the preference (first element in alist)
     ;;
     (if (not sy-confirm-always)
	 (car (car alist))
       ;;
       ;; confirmation is requested so build the prompt, confirm
       ;; and return the chosen string
       ;;
       (let* (ignore
	      (prompt (concat "Complete attribution string: (default "
			      (car (car alist))
			      ") "))
	      ;;
	      ;; set up the local completion keymap
	      ;;
	      (minibuffer-local-must-match-map
	       (let ((map (make-sparse-keymap)))
		 (define-key map "?"    'minibuffer-completion-help)
		 (define-key map " "    'minibuffer-complete-word)
		 (define-key map "\t"   'minibuffer-complete)
		 (define-key map "\00A" 'exit-minibuffer)
		 (define-key map "\00D" 'exit-minibuffer)
		 (define-key map "\007"
		   '(lambda ()
		      (interactive)
		      (beep)
		      (exit-minibuffer)))
		 map))
	      ;;
	      ;; read the completion
	      ;;
	      (attribution (completing-read prompt alist))
	      ;;
	      ;; check attribution string for emptyness
	      ;;
	      (choice (if (or (not attribution)
			      (string-equal attribution ""))
			  (car (car alist))
			attribution)))
	 
	 (if sy-downcase
	     (downcase choice)
	   choice))))
   sy-citation-string))


;;
;; ----------------------------------------------------------------------
;;
;; this function will scan the current rmail buffer, narrowing it to the
;; from: line, then using this, it will try to decipher some names from
;; that line.  It will then build the name alist and try to confirm
;; its choice of attribution strings.  It returns the chosen attribution
;; string.
;;
(defun sy-scan-rmail-for-names (rmailbuffer)
  (save-excursion
    (let ((case-fold-search t)
	  alist
	  attribution)
      (switch-to-buffer rmailbuffer)
      (goto-char (point-min))
      ;;
      ;; be sure there is a from: line
      ;;
      (if (not (re-search-forward "^from:\\s *" (point-max) t))
	  (setq attribution (sy-query-for-attribution))
	;;
	;; if there is a from: line, then scan the narrow the buffer,
	;; grab the namestring, and build the alist, then using this
	;; get the attribution string.
	;;
	(save-restriction
	  (narrow-to-region (point)
			    (progn (end-of-line) (point)))
	  (let* ((namestring (sy-get-namestring))
		 (nlist (sy-build-ordered-namelist namestring)))
	    (setq alist (sy-nlist-to-alist nlist))))
	;;
	;; we've built the alist, now confirm the attribution choice
	;; if appropriate
	;;
	(setq attribution (sy-get-attribution alist)))
      attribution)))


;;
;; ======================================================================
;;
;; the following function insert of citations, writing of headers, filling
;; paragraphs and general higher level operations
;;

;;
;; ----------------------------------------------------------------------
;;
;; insert a nested citation
;;
(defun sy-insert-citation (start end cite-string)
  (save-excursion
    (goto-char end)
    (setq end (point-marker))
    (goto-char start)
    (or (bolp)
	(forward-line 1))
    
    (let ((fill-prefix (concat cite-string " "))
	  (fstart (point))
	  (fend (point)))
      
      (while (< (point) end)
	;;
	;; remove leading tabs if desired
	;;
	(if sy-left-justify
	    (delete-region (point)
			   (progn (skip-chars-forward " \t") (point))))
	;;
	;; check to see if the current line should be cited
	;;
	(if (or (eolp)
		(looking-at sy-cite-regexp))
	    ;;
	    ;; do not cite this line unless nested-citations are to be 
	    ;; used
	    ;;
	    (progn
	      (or (eolp)
		  (if sy-nested-citation
		      (insert cite-string)))
	      
	      ;; set fill start and end points
	      ;;
	      (or (= fstart  fend)
		  (not sy-auto-fill-region)
		  (progn (goto-char fend)
			 (or (not (eolp))
			     (setq fend (+ fend 1)))
			 (fill-region-as-paragraph fstart fend)))
	      (setq fstart (point))
	      (setq fend (point)))
	  
	  ;; else
	  ;;
	  (insert fill-prefix)
	  (end-of-line)
	  (setq fend (point)))
	
	(forward-line 1)))
    (move-marker end nil)))

;;
;; ----------------------------------------------------------------------
;;
;; rewrite the header to be more conversational
;;
(defun sy-rewrite-header (start search yankhere message)
  (save-excursion
    (goto-char start)
    (let ((case-fold-search t)
	  (start (progn (re-search-forward search)
			(point)))
	  (end (progn (end-of-line)
		      (point-marker))))
      (copy-region-as-kill start end)
      (goto-char yankhere)
      (yank)
      (insert-string message "\n")
      (point))))

;;
;; ----------------------------------------------------------------------
;;
;; yank the original article in and attribute
;;
(defun sy-yank-original (arg)
  
  "Insert the message being replied to, if any (in rmail/gnus). Puts
point before the text and mark after. Calls generalized citation
function sy-insert-citation to cite all allowable lines."
  
  (interactive "P")
  (if mail-reply-buffer
      (let* ((sy-confirm-always (if (consp arg)
				    t
				  sy-confirm-always))
	     (attribution (sy-scan-rmail-for-names mail-reply-buffer))
	     (top (point))
	     (start (progn (insert-string "\non ")
			   (point)))
	     (end (progn (delete-windows-on mail-reply-buffer)
			 (insert-buffer mail-reply-buffer)
			 (mark)))
	     (start (sy-rewrite-header start "date:\\s *" start ","))
	     (start (sy-rewrite-header start "from:\\s *" start " said:")))
	
	
	(mail-yank-clear-headers top (mark))
	(sy-insert-citation start end attribution))
    
    (goto-char top)
    (exchange-point-and-mark)))


;;
;; ----------------------------------------------------------------------
;;
;; this is here for compatibility with existing mail/news yankers
;; overloads the default mail-yank-original
;;
(defun mail-yank-original (arg)
  
  "Yank original message buffer into the reply buffer, citing as per
user preferences.  Numeric Argument forces confirmation.

Here is a description of the superyank.el package, what it does and
what variables control its operation.  This was written by Barry
Warsaw (warsaw@cme.nbs.gov, {...}!uunet!cme-durer!warsaw).

'Citation' refers the acknowledgement of the original author of a mail
message.  There are two general forms of citation. In 'nested
citations', indication is made that the cited line was written by
someone *other* that the current message author (or by that author at
an earlier time).  No indication is made as to the identity of the
original author.  Thus, a nested citation after multiple replies would
look like this (this is after my reply to a previous message):

>>John originally wrote this
>>and this as well
> Jane said that John didn't know
> what he was talking about
And that's what I think as well.

In non-nested citations, you won't see multiple \">\" characters at the
top of the line.  Non-nested citations will insert an informative
string at the beginning of a cited line, attributing that line to an
author.  The same message described above might look like this if
non-nested citations were used:

John> John originally wrote this
John> and this as well
Jane> Jane said that John didn't know
Jane> what he was talking about
And that's what I think as well.

Notice that my inclusion of Jane's inclusion of John's original
message did not result in a cited line of the form: Jane>John>.  Thus
no nested citations.  The style of citation is controlled by the
variable `sy-nested-citation'.  Nil uses non-nested citations and
non-nil uses old style, nested citations.

The variable `sy-citation-string' is the string to use as a marker for
a citation, either nested or non-nested.  For best results, this
string should be a single character with no trailing space and is
typically the character \">\".  In non-nested citations this string is
appended to the attribution string (author's name), along with a
trailing space.  In nested citations, a trailing space is only added
to a first level citation.

Another important variable is `sy-cite-regexp' which describes strings
that indicate a previously cited line.  This regular expression is
always used at the beginning of a line so it doesn't need to begin
with a \"^\" character.  Change this variable if you change
`sy-citation-string'.

The following section only applies to non-nested citations.

This package has a fair amount of intellegence related to deciphering
the author's name based on information provided by the original
message buffer.  In normal operation, the program will pick out the
author's first and last names, initials, terminal email address and
any other names it can find.  It will then pick an attribution string
from this list based on a user defined preference and it will ask for
confirmation if the user specifies.  This package gathers its
information from the `From:' line of the original message buffer.  It
recognizes From: lines with the following forms:

From: John Xavier Doe <doe@speedy.computer.com>
From: \"John Xavier Doe\" <doe@speedy.computer.com>
From: doe@speedy.computer.com (John Xavier Doe)
From: computer!speedy!doe (John Xavier Doe)
From: computer!speedy!doe (John Xavier Doe)
From: doe%speedy@computer.com (John Xavier Doe)

In this case, if confirmation is requested, the following strings will
be made available for completion:

\"John\"
\"Xavier\"
\"Doe\"
\"JXD\"
\"doe\"

Note that completion is case sensitive.  If there was a problem
picking out a From: line, or any other problem getting even a single
name, then the user will be queried for an attribution string.  The
default attribution string is set in the variable
`sy-default-attribution'.

Sometimes people set their name fields so that it also includes a
title of the form:

From: doe@speedy.computer.com (John Doe -- Hacker Extraordinaire)

To avoid the inclusion of the string \"-- Hacker Extraordinaire\" in
the name list, the variable `sy-titlecue-regexp' is provided.  Its
default setting will still properly recognize names of the form:

From: xdoe@speedy.computer.com (John Xavier-Doe -- Crazed Hacker)

The variable `sy-preferred-attribution' contains an integer that
indicates which name field the user prefers to use as the attribution
string, based on the following key:

0: email address name is preferred
1: initials are preferred
2: first name is preferred
3: last name is preferred

The value can be greater than 3, in which case, you would be
preferring the 2nd throught nth -1 name.  In any case, if the
preferred name can't be found, then by default, the author's first
name will be used.  If that can't be found than the name list is
search for the first non-nil, non-empty name string.  If still no name
can be found, then the user is either queried, or the
`sy-default-attribution' is used, depending on the value of
`sy-confirm-always'.

The variable `sy-confirm-always', if non-nil, will always confirm the
attribution string with the user before inserting it into the reply
buffer.  Confirmation is with completion, but the completion list
is merely a suggestion; the user can override the list by typing in a
string of their choice.

Finally, `sy-downcase' if non-nil, indicates that you always want to
downcase the attribution string before insertion, and
`sy-left-justify', if non-nil, indicates that you want to delete all
leading white space before citing.


Summary of variables, with their default values:

sy-default-attribution     (default: \"Anon\")
           Attribution to use if no attribution string can be deciphered
           from the original message buffer.

sy-citation-string         (default: \">\")
           String to append to the attribution string for citation, for
           best results, it should be one character with no trailing space.

sy-nested-citation         (default: nil)
           Nil means use non-nested citations, non-nil means use old style
           nested citations.

sy-cite-regexp             (default: \"[a-zA-Z0-9]*>\")
           Regular expression that matches the beginning of a previously
           cited line.  Always used at the beginning of a line so it does
           not need to start with a \"^\" character.

sy-titlecue-regexp         (default: \"\\s +-+\\s +\")
           Regular expression that matches a title delimiter in the name
           field.

sy-preferred-attribution   (default: 2)
           Integer indicating user's preferred attribution field.

sy-confirm-always          (default: t)
           Non-nil says always confirm with completion before inserting
           attribution.

sy-downcase                (default: nil)
           Non-nil says downcase the attribution string before insertion.

sy-auto-fill-region        (default: nil)
           Non-nil says don't auto fill the region.  T says auto fill the
           paragraph.

sy-left-justify            (default: nil)
           Non-nil says delete leading white space before citing."
  
  (interactive "P")
  (sy-yank-original arg))


;;
;; ----------------------------------------------------------------------
;;
;; based on Bruce Israel's "fill-paragraph-properly", and modified from
;; code posted by David C. Lawrence.
;;
(defun sy-fill-paragraph-manually (arg)
  "Fill paragraph containing or following point, automatically finding
the sy-cite-regexp and using it as the prefix.  If the sy-cite-regexp
is not in the first line of the paragraph, it makes a guess at what
the fill-prefix for the paragraph should be by looking at the first
line and taking anything up to the first alphanumeric character.

Prefix arg means justify both sides of paragraph as well.

This function just does fill-paragraph if a) fill-prefix is set, or
b) what it deduces to be the paragraph prefix (based on the first line)
does not precede each line in the region."
  (interactive "P")
  (save-excursion
    (forward-paragraph)
    (or (bolp)
	(newline 1))
    
    (let ((end (point))
	  st
	  (fill-prefix fill-prefix))
      (backward-paragraph)
      (if (looking-at "\n")
	  (forward-char 1))
      (setq st (point))
      (if fill-prefix
	  nil
	(untabify st end) ;; die, scurvy tabs!
	;;
        ;; untabify might have made the paragraph longer character-wise,
        ;; make sure end reflects the correct location of eop.
	;;
        (forward-paragraph)
	(setq end (point))
        (goto-char st)
        (if (looking-at sy-cite-regexp)
            (setq fill-prefix (concat
			       (buffer-substring
				st (progn (re-search-forward sy-cite-regexp)
					  (point)))
			       " "))
	  ;;
          ;; this regexp is is convenient because paragraphs quoted by simple
          ;; indentation must still yield to us  <evil laugh>
	  ;;
          (while (looking-at "[^a-zA-Z0-9]")
	    (forward-char 1))
          (setq fill-prefix (buffer-substring st (point))))
        (next-line 1) (beginning-of-line)
        (while (and (< (point) end)
		    (not (string-equal fill-prefix "")))
	  ;;
          ;; if what we decided was the fill-prefix does not precede all
          ;; of the lines in the paragraph, we probably goofed.  Cancel it.
	  ;;
          (if (looking-at (regexp-quote fill-prefix))
	      ()
	    (setq fill-prefix ""))
          (next-line 1)
	  (beginning-of-line)))
      (fill-region-as-paragraph st end arg))))

(define-key mail-mode-map "\C-cq"    'sy-fill-paragraph-manually)
(define-key mail-mode-map "\C-c\C-q" 'sy-fill-paragraph-manually)

ecb@utrccm (ecb) (05/24/89)

Superyank.el seems to have been corrupted in transit. Could you mail it
to me again, or repost it if others have received mangled copies also?


			Bud Boman
			(203) 727-7128
			ecb@utrccm.smc.utc.com