Sunday, March 19, 2006

E-mail citations -- emacs does outlook

In some circles what you do with the text of an e-mail you are responding to is a big deal. There is one school of thought that is concerned with quoting too much text back to the user, not putting quoted text in the right order, and about half a dozen other rules.

And then there's another school of thought that says, copy the entire e-mail to the bottom of the message, and just start typing at the top.

While I'm usually in the camp that says to quote back messages properly, the majority of the world is in the latter. Why? One word, Outlook. It's the style used by Microsoft Outlook, so it's the style used by most of the world (and probalby lots of other e-mails too that are too weak to provide quoting functionality).

I use emacs to read my mail, and it comes with a very fancy Citation Engine. Which does remarkable things, yet, doesn't behave like Outlook. As a result, my e-mails stick out like a sore thumb.

Tonight I finally gave in and decided I would support Outlook style mail replies. Below is the code I used to support this. If you use emacs and want to blend in better with Outlookers, this code may be useful.

(defun outlook-style-citation ()
  "Quote back messages in an outlook style. I can't begin to describe
how ugly this is. But this is the style folks use."
  (let* ((start (point))
         (end (mark t)))
    (setq message-yank-prefix "")
    (setq message-yank-cited-prefix "")
    (setq message-reply-headers
            (narrow-to-region start end)
            (vector 0 ; 0
                     (or (message-fetch-field "subject") "none") ; 1
                     (message-fetch-field "from") ; 2
                     (message-fetch-field "date") ; 3
                     (message-fetch-field "message-id" t) ; 4
                     (message-fetch-field "references") ; 5
                     0 0 "" ; 6, 7 , 8
                     (message-fetch-field "to") ; 9
                     (message-fetch-field "cc") ; 10
    (goto-char start)
 (unless (bolp)
   (insert "\n"))
    (insert "\n\n")                     
    (insert "-----Original Message-----\n")
    (insert (format "From: %s\n"  (aref message-reply-headers 2)))
    (insert (format "To: %s\n"  (aref message-reply-headers 9)))
    (if (aref message-reply-headers 10)
        (insert (format "Cc: %s\n"  (aref message-reply-headers 10))))
    (insert (format "Date: %s\n"  (aref message-reply-headers 3)))
    (insert "\n")
    (goto-char end)))

(defadvice message-yank-original (after goto-top)
  "Make sure we go to the top of our mail message after 
a yank."
  (re-search-backward "^--text follows this line--")
  (insert "\n\n--\n")
  (insert-file-contents message-signature-file)
  (re-search-backward "^--text follows this line--")
(ad-activate 'message-yank-original)

(defun message-cite-use-outlook ()
  "Make sure we are setup to use oultook citation style."
  (ad-activate 'message-yank-original)
  (setq message-cite-function 'outlook-style-citation))

It's worth noting that the code marked with defadvice is actually advice to an existing function. This is more commonly known today as Aspect Oriented Programming. This has been a standard feature of emacs lisp for years.

To switch to Outlook citation mode, I just run the command message-cite-use-outlook and I'm good to go.

No comments:

Post a Comment