Anonymous avatar Anonymous committed 33b7519

Created

Comments (0)

Files changed (11)

+1998-01-12  SL Baur  <steve@altair.xemacs.org>
+
+	* Makefile: Update to newer package interface.
+
+1997-12-10  Markus Linnala  <maage@cs.tut.fi>
+
+	* mh-e.el (mh-e-load-hook): New hook.
+
+	* mh-comp.el (mh-user-agent-compose) New function to act as
+	`mail-user-agent' entry point to mh-e.
+
+1997-01-05  SL Baur  <steve@altair.xemacs.org>
+
+	* Makefile: Update to newer package interface
+
+1997-12-21  SL Baur  <steve@altair.xemacs.org>
+
+	* Makefile: Created.
+
+# Makefile for MH-E lisp code
+
+# This file is part of XEmacs.
+
+# XEmacs is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the
+# Free Software Foundation; either version 2, or (at your option) any
+# later version.
+
+# XEmacs is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+# for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with XEmacs; see the file COPYING.  If not, write to
+# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+# Boston, MA 02111-1307, USA.
+
+VERSION = 1.03
+PACKAGE = mh-e
+PKG_TYPE = regular
+REQUIRES = mail-lib xemacs-base
+CATEGORY = comm
+
+ELCS = mh-comp.elc mh-e.elc mh-funcs.elc mh-mime.elc mh-pick.elc mh-seq.elc \
+	mh-utils.elc
+
+INFO_FILES = mh-e.info*
+
+include ../../XEmacs.rules
+
+all:: $(ELCS) auto-autoloads.elc custom-load.elc mh-e.info
+
+srckit: srckit-std
+
+binkit: binkit-sourceinfo
+;;; mh-comp --- mh-e functions for composing messages
+;; Time-stamp: <95/08/19 17:48:59 gildea>
+
+;; Copyright (C) 1993, 1995 Free Software Foundation, Inc.
+
+;; This file is part of mh-e, part of GNU Emacs.
+
+;; GNU Emacs is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 2, or (at your option)
+;; any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs; see the file COPYING.  If not, write to
+;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+
+;;; Commentary:
+
+;; Internal support for mh-e package.
+
+;;; Change Log:
+
+;; $Id$
+
+;;; Code:
+
+(provide 'mh-comp)
+(require 'mh-utils)
+
+;;; Site customization (see also mh-utils.el):
+
+(defgroup mh-compose nil
+  "Mh-e functions for composing messages"
+  :prefix "mh-"
+  :group 'mh)
+
+
+(defvar mh-send-prog "send"
+  "Name of the MH send program.
+Some sites need to change this because of a name conflict.")
+
+(defvar mh-redist-full-contents nil
+  "Non-nil if the `dist' command needs whole letter for redistribution.
+This is the case only when `send' is compiled with the BERK option.
+If MH will not allow you to redist a previously redist'd msg, set to nil.")
+
+
+(defvar mh-note-repl "-"
+  "String whose first character is used to notate replied to messages.")
+
+(defvar mh-note-forw "F"
+  "String whose first character is used to notate forwarded messages.")
+
+(defvar mh-note-dist "R"
+  "String whose first character is used to notate redistributed messages.")
+
+(defvar mh-yank-hooks nil
+  "Obsolete hook for modifying a citation just inserted in the mail buffer.
+Each hook function can find the citation between point and mark.
+And each hook function should leave point and mark around the citation
+text as modified.
+
+This is a normal hook, misnamed for historical reasons.
+It is semi-obsolete and is only used if mail-citation-hook is nil.")
+
+(defvar mail-citation-hook nil
+  "*Hook for modifying a citation just inserted in the mail buffer.
+Each hook function can find the citation between point and mark.
+And each hook function should leave point and mark around the citation
+text as modified.
+
+If this hook is entirely empty (nil), the text of the message is inserted
+with mh-ins-buf-prefix prefixed to each line.
+
+See also the variable mh-yank-from-start-of-msg, which controls how
+much of the message passed to the hook.")
+
+;;; Copied from sendmail.el for Hyperbole
+(defvar mail-header-separator "--------"
+  "*Line used by MH to separate headers from text in messages being composed.")
+
+;;; Personal preferences:
+
+(defcustom mh-delete-yanked-msg-window nil
+  "*Controls window display when a message is yanked by \\<mh-letter-mode-map>\\[mh-yank-cur-msg].
+If non-nil, yanking the current message into a draft letter deletes any
+windows displaying the message."
+  :type 'boolean
+  :group 'mh-compose)
+
+(defcustom mh-yank-from-start-of-msg t
+  "*Controls which part of a message is yanked by \\<mh-letter-mode-map>\\[mh-yank-cur-msg].
+If non-nil, include the entire message.  If the symbol `body', then yank the
+message minus the header.  If nil, yank only the portion of the message
+following the point.  If the show buffer has a region, this variable is
+ignored."
+  :type '(choice (const t) (const nil) (const body))
+  :group 'mh-compose)
+
+(defcustom mh-ins-buf-prefix "> "
+  "*String to put before each non-blank line of a yanked or inserted message.
+\\<mh-letter-mode-map>Used when the message is inserted into an outgoing letter
+by \\[mh-insert-letter] or \\[mh-yank-cur-msg]."
+  :type 'string
+  :group 'mh-compose)
+
+(defcustom mh-reply-default-reply-to nil
+  "*Sets the person or persons to whom a reply will be sent.
+If nil, prompt for recipient.  If non-nil, then \\<mh-folder-mode-map>`\\[mh-reply]' will use this
+value and it should be one of \"from\", \"to\", \"cc\", or \"all\".
+The values \"cc\" and \"all\" do the same thing."
+  :type '(choice (const :tag "Prompt" nil)
+		 (const "from") (const "to")
+		 (const "cc") (const "all"))
+  :group 'mh-compose)
+
+(defcustom mh-signature-file-name "~/.signature"
+  "*Name of file containing the user's signature.
+Inserted into message by \\<mh-letter-mode-map>\\[mh-insert-signature]."
+  :type 'file
+  :group 'mh-compose)
+
+(defcustom mh-forward-subject-format "%s: %s"
+  "*Format to generate the Subject: line contents for a forwarded message.
+The two string arguments to the format are the sender of the original
+message and the original subject line."
+  :type 'string
+  :group 'mh-compose)
+
+(defvar mh-comp-formfile "components"
+  "Name of file to be used as a skeleton for composing messages.
+Default is \"components\".  If not a complete path name, the file
+is searched for first in the user's MH directory, then in the
+system MH lib directory.")
+
+(defvar mh-repl-formfile "replcomps"
+  "Name of file to be used as a skeleton for replying to messages.
+Default is \"replcomps\".  If not a complete path name, the file
+is searched for first in the user's MH directory, then in the
+system MH lib directory.")
+
+;;; Hooks:
+
+(defcustom mh-letter-mode-hook nil
+  "Invoked in `mh-letter-mode' on a new letter."
+  :type 'hook
+  :group 'mh-compose)
+
+(defcustom mh-compose-letter-function nil
+  "Invoked when setting up a letter draft.
+It is passed three arguments: TO recipients, SUBJECT, and CC recipients."
+  :type 'function
+  :group 'mh-compose)
+
+(defcustom mh-before-send-letter-hook nil
+  "Invoked at the beginning of the \\<mh-letter-mode-map>\\[mh-send-letter] command."
+  :type 'hook
+  :group 'mh-compose)
+
+
+(defvar mh-rejected-letter-start
+  (concat "^   ----- Unsent message follows -----$"	;from sendmail V5
+	  "\\|^   ----- Original message follows -----$" ;from sendmail V8
+	  "\\|^------- Unsent Draft$"			;from MH itself
+	  "\\|^----------  Original Message  ----------$" ;from zmailer
+	  "\\|^  --- The unsent message follows ---$"	;from AIX mail system
+	  "\\|^    Your message follows:$"		;from MMDF-II
+	  "\\|^Content-Description: Returned Content$"	;1993 KJ sendmail
+	  )
+  "Regexp specifying the beginning of the wrapper around a returned letter.
+This wrapper is generated by the mail system when rejecting a letter.")
+
+(defvar mh-new-draft-cleaned-headers
+  "^Date:\\|^Received:\\|^Message-Id:\\|^From:\\|^Sender:\\|^Errors-To:\\|^Delivery-Date:\\|^Return-Path:"
+  "Regexp of header lines to remove before offering a message as a new draft.
+Used by the \\<mh-folder-mode-map>`\\[mh-edit-again]' and `\\[mh-extract-rejected-mail]' commands.")
+
+(defvar mh-to-field-choices '(("t" . "To:") ("s" . "Subject:") ("c" . "Cc:")
+			      ("b" . "Bcc:") ("f" . "Fcc:") ("r" . "From:")
+			      ("d" . "Dcc:"))
+  "Alist of (final-character . field-name) choices for mh-to-field.")
+
+(defvar mh-letter-mode-map (copy-keymap text-mode-map)
+  "Keymap for composing mail.")
+
+(defvar mh-letter-mode-syntax-table nil
+  "Syntax table used by mh-e while in MH-Letter mode.")
+
+(if mh-letter-mode-syntax-table
+    ()
+    (setq mh-letter-mode-syntax-table
+	  (make-syntax-table text-mode-syntax-table))
+    (modify-syntax-entry ?% "." mh-letter-mode-syntax-table))
+
+
+;;;###autoload
+(defun mh-smail ()
+  "Compose and send mail with the MH mail system.
+This function is an entry point to mh-e, the Emacs front end
+to the MH mail system.
+
+See documentation of `\\[mh-send]' for more details on composing mail."
+  (interactive)
+  (mh-find-path)
+  (call-interactively 'mh-send))
+
+
+(defvar mh-error-if-no-draft nil)	;raise error over using old draft
+
+
+;;;###autoload
+(defun mh-smail-batch ()
+  "Set up a mail composition draft with the MH mail system.
+This function is an entry point to mh-e, the Emacs front end
+to the MH mail system.  This function does not prompt the user
+for any header fields, and thus is suitable for use by programs
+that want to create a mail buffer.
+Users should use `\\[mh-smail]' to compose mail."
+  (mh-find-path)
+  (let ((mh-error-if-no-draft t))
+    (mh-send "" "" "")))
+
+;;;###autoload
+(defun mh-user-agent-compose (&optional to subject other-headers continue
+                                             switch-function yank-action
+                                             send-actions)
+  "Set up mail composition draft with the MH mail system.
+This is `mail-user-agent' entry point to mh-e.
+
+The optional arguments TO and SUBJECT specify recipients and the
+initial Subject field, respectively.
+
+OTHER-HEADERS is an alist specifying additional
+header fields.  Elements look like (HEADER . VALUE) where both
+HEADER and VALUE are strings.
+
+CONTINUE, SWITCH-FUNCTION, YANK-ACTION and SEND-ACTIONS are ignored."
+  (mh-find-path)
+  (let ((mh-error-if-no-draft t))
+    (mh-send to "" subject)
+    (while other-headers
+      (mh-insert-fields (concat (car (car other-headers)) ":") 
+                       (cdr (car other-headers)))
+      (setq other-headers (cdr other-headers)))))
+
+(defun mh-edit-again (msg)
+  "Clean-up a draft or a message previously sent and make it resendable.
+Default is the current message.
+The variable mh-new-draft-cleaned-headers specifies the headers to remove.
+See also documentation for `\\[mh-send]' function."
+  (interactive (list (mh-get-msg-num t)))
+  (let* ((from-folder mh-current-folder)
+	 (config (current-window-configuration))
+	 (draft
+	  (cond ((and mh-draft-folder (equal from-folder mh-draft-folder))
+		 (pop-to-buffer (find-file-noselect (mh-msg-filename msg)) t)
+		 (rename-buffer (format "draft-%d" msg))
+		 (buffer-name))
+		(t
+		 (mh-read-draft "clean-up" (mh-msg-filename msg) nil)))))
+    (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil)
+    (goto-char (point-min))
+    (save-buffer)
+    (mh-compose-and-send-mail draft "" from-folder nil nil nil nil nil nil
+			      config)))
+
+
+(defun mh-extract-rejected-mail (msg)
+  "Extract a letter returned by the mail system and make it resendable.
+Default is the current message.  The variable mh-new-draft-cleaned-headers
+gives the headers to clean out of the original message.
+See also documentation for `\\[mh-send]' function."
+  (interactive (list (mh-get-msg-num t)))
+  (let ((from-folder mh-current-folder)
+	(config (current-window-configuration))
+	(draft (mh-read-draft "extraction" (mh-msg-filename msg) nil)))
+    (goto-char (point-min))
+    (cond ((re-search-forward mh-rejected-letter-start nil t)
+	   (skip-chars-forward " \t\n")
+	   (delete-region (point-min) (point))
+	   (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil))
+	  (t
+	   (message "Does not appear to be a rejected letter.")))
+    (goto-char (point-min))
+    (save-buffer)
+    (mh-compose-and-send-mail draft "" from-folder msg
+			      (mh-get-header-field "To:")
+			      (mh-get-header-field "From:")
+			      (mh-get-header-field "Cc:")
+			      nil nil config)))
+
+
+(defun mh-forward (to cc &optional msg-or-seq)
+  "Forward a message or message sequence.  Defaults to displayed message.
+If optional prefix argument provided, then prompt for the message sequence.
+See also documentation for `\\[mh-send]' function."
+  (interactive (list (mh-read-address "To: ")
+		     (mh-read-address "Cc: ")
+		     (if current-prefix-arg
+			 (mh-read-seq-default "Forward" t)
+		       (mh-get-msg-num t))))
+  (or msg-or-seq
+      (setq msg-or-seq (mh-get-msg-num t)))
+  (let* ((folder mh-current-folder)
+	 (config (current-window-configuration))
+	 ;; forw always leaves file in "draft" since it doesn't have -draft
+	 (draft-name (expand-file-name "draft" mh-user-path))
+	 (draft (cond ((or (not (file-exists-p draft-name))
+			   (y-or-n-p "The file 'draft' exists.  Discard it? "))
+		       (mh-exec-cmd "forw" "-build"
+				    mh-current-folder msg-or-seq)
+		       (prog1
+			   (mh-read-draft "" draft-name t)
+			 (mh-insert-fields "To:" to "Cc:" cc)
+			 (save-buffer)))
+		      (t
+		       (mh-read-draft "" draft-name nil)))))
+    (let (orig-from
+	  orig-subject)
+      (goto-char (point-min))
+      (re-search-forward "^------- Forwarded Message")
+      (forward-line 1)
+      (skip-chars-forward " \t\n")
+      (save-restriction
+	(narrow-to-region (point) (point-max))
+	(setq orig-from (mh-get-header-field "From:"))
+	(setq orig-subject (mh-get-header-field "Subject:")))
+      (let ((forw-subject
+	     (mh-forwarded-letter-subject orig-from orig-subject)))
+	(mh-insert-fields "Subject:" forw-subject)
+	(goto-char (point-min))
+	(re-search-forward "^------- Forwarded Message")
+	(forward-line -1)
+	(delete-other-windows)
+	(if (numberp msg-or-seq)
+	    (mh-add-msgs-to-seq msg-or-seq 'forwarded t)
+	  (mh-add-msgs-to-seq (mh-seq-to-msgs msg-or-seq) 'forwarded t))
+	(mh-compose-and-send-mail draft "" folder msg-or-seq
+				  to forw-subject cc
+				  mh-note-forw "Forwarded:"
+				  config)))))
+
+(defun mh-forwarded-letter-subject (from subject)
+  ;; Return a Subject suitable for a forwarded message.
+  ;; Original message has headers FROM and SUBJECT.
+  (let ((addr-start (string-match "<" from))
+	(comment (string-match "(" from)))
+    (cond ((and addr-start (> addr-start 0))
+	   ;; Full Name <luser@host>
+	   (setq from (substring from 0 (1- addr-start))))
+	  (comment
+	   ;; luser@host (Full Name)
+	   (setq from (substring from (1+ comment) (1- (length from)))))))
+  (format mh-forward-subject-format from subject))
+
+
+;;;###autoload
+(defun mh-smail-other-window ()
+  "Compose and send mail in other window with the MH mail system.
+This function is an entry point to mh-e, the Emacs front end
+to the MH mail system.
+
+See documentation of `\\[mh-send]' for more details on composing mail."
+  (interactive)
+  (mh-find-path)
+  (call-interactively 'mh-send-other-window))
+
+
+(defun mh-redistribute (to cc &optional msg)
+  "Redistribute a letter.
+Depending on how your copy of MH was compiled, you may need to change the
+setting of the variable mh-redist-full-contents.  See its documentation."
+  (interactive (list (mh-read-address "Redist-To: ")
+		     (mh-read-address "Redist-Cc: ")
+		     (mh-get-msg-num t)))
+  (or msg
+      (setq msg (mh-get-msg-num t)))
+  (save-window-excursion
+    (let ((folder mh-current-folder)
+	  (draft (mh-read-draft "redistribution"
+				(if mh-redist-full-contents
+				    (mh-msg-filename msg)
+				  nil)
+				nil)))
+      (mh-goto-header-end 0)
+      (insert "Resent-To: " to "\n")
+      (if (not (equal cc "")) (insert "Resent-cc: " cc "\n"))
+      (mh-clean-msg-header (point-min)
+			   "^Message-Id:\\|^Received:\\|^Return-Path:\\|^Sender:\\|^Date:\\|^From:"
+			   nil)
+      (save-buffer)
+      (message "Redistributing...")
+      (if mh-redist-full-contents
+	  (call-process "/bin/sh" nil 0 nil "-c"
+			(format "mhdist=1 mhaltmsg=%s %s -push %s"
+				buffer-file-name
+				(expand-file-name mh-send-prog mh-progs)
+				buffer-file-name))
+	(call-process "/bin/sh" nil 0 nil "-c"
+		      (format "mhdist=1 mhaltmsg=%s mhannotate=1 %s -push %s"
+			      (mh-msg-filename msg folder)
+			      (expand-file-name mh-send-prog mh-progs)
+			      buffer-file-name)))
+      (mh-annotate-msg msg folder mh-note-dist
+		       "-component" "Resent:"
+		       "-text" (format "\"%s %s\"" to cc))
+      (kill-buffer draft)
+      (message "Redistributing...done"))))
+
+
+(defun mh-reply (message &optional includep)
+  "Reply to MESSAGE (default: current message).
+If optional prefix argument INCLUDEP provided, then include the message
+in the reply using filter mhl.reply in your MH directory.
+Prompts for type of addresses to reply to:
+   from    sender only,
+   to      sender and primary recipients,
+   cc/all  sender and all recipients.
+If the file named by `mh-repl-formfile' exists, it is used as a skeleton
+for the reply.  See also documentation for `\\[mh-send]' function."
+  (interactive (list (mh-get-msg-num t) current-prefix-arg))
+  (let ((minibuffer-help-form
+	 "from => Sender only\nto => Sender and primary recipients\ncc or all => Sender and all recipients"))
+    (let ((reply-to (or mh-reply-default-reply-to
+			(completing-read "Reply to whom: "
+					 '(("from") ("to") ("cc") ("all"))
+					 nil
+					 t)))
+	  (folder mh-current-folder)
+	  (show-buffer mh-show-buffer)
+	  (config (current-window-configuration)))
+      (message "Composing a reply...")
+      (mh-exec-cmd "repl" "-build" "-noquery" "-nodraftfolder"
+	     (if (stringp mh-repl-formfile) ;must be string, but we're paranoid
+		 (list "-form" mh-repl-formfile))
+	     mh-current-folder message
+	     (cond ((or (equal reply-to "from") (equal reply-to ""))
+		    '("-nocc" "all"))
+		   ((equal reply-to "to")
+		    '("-cc" "to"))
+		   ((or (equal reply-to "cc") (equal reply-to "all"))
+		    '("-cc" "all" "-nocc" "me")))
+	     (if includep
+		 '("-filter" "mhl.reply")))
+      (let ((draft (mh-read-draft "reply"
+				  (expand-file-name "reply" mh-user-path)
+				  t)))
+	(delete-other-windows)
+	(save-buffer)
+	
+	(let ((to (mh-get-header-field "To:"))
+	      (subject (mh-get-header-field "Subject:"))
+	      (cc (mh-get-header-field "Cc:")))
+	  (goto-char (point-min))
+	  (mh-goto-header-end 1)
+	  (or includep
+	      (mh-in-show-buffer (show-buffer)
+		(mh-display-msg message folder)))
+	  (mh-add-msgs-to-seq message 'answered t)
+	  (message "Composing a reply...done")
+	  (mh-compose-and-send-mail draft "" folder message to subject cc
+				    mh-note-repl "Replied:" config))))))
+
+
+(defun mh-send (to cc subject)
+  "Compose and send a letter.
+The file named by `mh-comp-formfile' will be used as the form.
+Do not call this function from outside mh-e; use \\[mh-smail] instead.
+
+The letter is composed in mh-letter-mode; see its documentation for more
+details.  If `mh-compose-letter-function' is defined, it is called on the
+draft and passed three arguments: to, subject, and cc."
+  (interactive (list
+		(mh-read-address "To: ")
+		(mh-read-address "Cc: ")
+		(read-string "Subject: ")))
+  (let ((config (current-window-configuration)))
+    (delete-other-windows)
+    (mh-send-sub to cc subject config)))
+
+
+(defun mh-send-other-window (to cc subject)
+  "Compose and send a letter in another window.
+Do not call this function from outside mh-e;
+use \\[mh-smail-other-window] instead.
+See also documentation for `\\[mh-send]' function."
+  (interactive (list
+		(mh-read-address "To: ")
+		(mh-read-address "Cc: ")
+		(read-string "Subject: ")))
+  (let ((pop-up-windows t))
+    (mh-send-sub to cc subject (current-window-configuration))))
+
+
+(defun mh-send-sub (to cc subject config)
+  ;; Do the real work of composing and sending a letter.
+  ;; Expects the TO, CC, and SUBJECT fields as arguments.
+  ;; CONFIG is the window configuration before sending mail.
+  (let ((folder mh-current-folder)
+	(msg-num (mh-get-msg-num nil)))
+    (message "Composing a message...")
+    (let ((draft (mh-read-draft
+		  "message"
+		  (let (components)
+		    (cond
+		     ((file-exists-p
+		       (setq components
+			     (expand-file-name mh-comp-formfile mh-user-path)))
+		      components)
+		     ((file-exists-p
+		       (setq components
+			     (expand-file-name mh-comp-formfile mh-lib)))
+		      components)
+		     (t
+		      (error (format "Can't find components file \"%s\""
+				     components)))))
+		  nil)))
+      (mh-insert-fields "To:" to "Subject:" subject "Cc:" cc)
+      (goto-char (point-max))
+      (message "Composing a message...done")
+      (mh-compose-and-send-mail draft "" folder msg-num
+				to subject cc
+				nil nil config))))
+
+
+(defun mh-read-draft (use initial-contents delete-contents-file)
+  ;; Read draft file into a draft buffer and make that buffer the current one.
+  ;; USE is a message used for prompting about the intended use of the message.
+  ;; INITIAL-CONTENTS is filename that is read into an empty buffer, or NIL
+  ;; if buffer should not be modified.  Delete the initial-contents file if
+  ;; DELETE-CONTENTS-FILE flag is set.
+  ;; Returns the draft folder's name.
+  ;; If the draft folder facility is enabled in ~/.mh_profile, a new buffer is
+  ;; used each time and saved in the draft folder.  The draft file can then be
+  ;; reused.
+  (cond (mh-draft-folder
+	 (let ((orig-default-dir default-directory)
+	       (draft-file-name (mh-new-draft-name)))
+	   (pop-to-buffer (generate-new-buffer
+			   (format "draft-%s"
+				   (file-name-nondirectory draft-file-name))))
+	   (condition-case ()
+	       (insert-file-contents draft-file-name t)
+	     (file-error))
+	   (setq default-directory orig-default-dir)))
+	(t
+	 (let ((draft-name (expand-file-name "draft" mh-user-path)))
+	   (pop-to-buffer "draft")	; Create if necessary
+	   (if (buffer-modified-p)
+	       (if (y-or-n-p "Draft has been modified; kill anyway? ")
+		   (set-buffer-modified-p nil)
+		   (error "Draft preserved")))
+	   (setq buffer-file-name draft-name)
+	   (clear-visited-file-modtime)
+	   (unlock-buffer)
+	   (cond ((and (file-exists-p draft-name)
+		       (not (equal draft-name initial-contents)))
+		  (insert-file-contents draft-name)
+		  (delete-file draft-name))))))
+  (cond ((and initial-contents
+	      (or (zerop (buffer-size))
+		  (if (y-or-n-p
+			(format "A draft exists.  Use for %s? " use))
+		      (if mh-error-if-no-draft
+			  (error "A prior draft exists."))
+		    t)))
+	 (erase-buffer)
+	 (insert-file-contents initial-contents)
+	 (if delete-contents-file (delete-file initial-contents))))
+  (auto-save-mode 1)
+  (if mh-draft-folder
+      (save-buffer))			; Do not reuse draft name
+  (buffer-name))
+
+
+(defun mh-new-draft-name ()
+  ;; Returns the pathname of folder for draft messages.
+  (save-excursion
+    (mh-exec-cmd-quiet t "mhpath" mh-draft-folder "new")
+    (buffer-substring (point-min) (1- (point-max)))))
+
+
+(defun mh-annotate-msg (msg buffer note &rest args)
+  ;; Mark the MESSAGE in BUFFER listing with the character NOTE and annotate
+  ;; the saved message with ARGS.
+  (apply 'mh-exec-cmd "anno" buffer msg args)
+  (save-excursion
+    (cond ((get-buffer buffer)		; Buffer may be deleted
+	   (set-buffer buffer)
+	   (if (symbolp msg)
+	       (mh-notate-seq msg note (1+ mh-cmd-note))
+	       (mh-notate msg note (1+ mh-cmd-note)))))))
+
+
+(defun mh-insert-fields (&rest name-values)
+  ;; Insert the NAME-VALUE pairs in the current buffer.
+  ;; If field NAME exists, append VALUE to it.
+  ;; Do not insert any pairs whose value is the empty string.
+  (let ((case-fold-search t))
+    (while name-values
+      (let ((field-name (car name-values))
+	    (value (car (cdr name-values))))
+	(cond ((equal value "")
+	       nil)
+	      ((mh-position-on-field field-name)
+	       (insert " " value))
+	      (t
+	       (insert field-name " " value "\n")))
+	(setq name-values (cdr (cdr name-values)))))))
+
+
+(defun mh-position-on-field (field &optional ignore)
+  ;; Move to the end of the FIELD in the header.
+  ;; Move to end of entire header if FIELD not found.
+  ;; Returns non-nil iff FIELD was found.
+  ;; The optional second arg is for pre-version 4 compatibility.
+  (if (mh-goto-header-field field)
+      (progn
+	(mh-header-field-end)
+	t)))
+
+
+(defun mh-get-header-field (field)
+  ;; Find and return the body of FIELD in the mail header.
+  ;; Returns the empty string if the field is not in the header of the
+  ;; current buffer.
+  (if (mh-goto-header-field field)
+      (progn
+	(skip-chars-forward " \t")	;strip leading white space in body
+	(let ((start (point)))
+	  (mh-header-field-end)
+	  (buffer-substring start (point))))
+    ""))
+
+(fset 'mh-get-field 'mh-get-header-field) ;mh-e 4 compatibility
+
+(defun mh-goto-header-field (field)
+  ;; Move to FIELD in the message header.
+  ;; Move to the end of the FIELD name, which should end in a colon.
+  ;; Returns T if found, NIL if not.
+  (goto-char (point-min))
+  (let ((case-fold-search t)
+	(headers-end (save-excursion
+		      (mh-goto-header-end 0)
+		      (point))))
+    (re-search-forward (format "^%s" field) headers-end t)))
+
+(defun mh-header-field-end ()
+  ;; Move to the end of the current header field.
+  ;; Handles RFC 822 continuation lines.
+  (forward-line 1)
+  (while (looking-at "^[ \t]")
+    (forward-line 1))
+  (backward-char 1))		;to end of previous line
+  
+
+(defun mh-goto-header-end (arg)
+  ;; Find the end of the message header in the current buffer and position
+  ;; the cursor at the ARG'th newline after the header.
+  (if (re-search-forward "^-*$" nil nil)
+      (forward-line arg)))
+
+
+(defun mh-read-address (prompt)
+  ;; Read a To: or Cc: address, prompting in the minibuffer with PROMPT.
+  ;; May someday do completion on aliases.
+  (read-string prompt))
+
+
+
+;;; Mode for composing and sending a draft message.
+
+(defvar mh-sent-from-folder nil)	;Folder of msg assoc with this letter.
+
+(defvar mh-sent-from-msg nil)		;Number of msg assoc with this letter.
+
+(defvar mh-send-args nil)		;Extra args to pass to "send" command.
+
+(defvar mh-annotate-char nil)		;Character to use to annotate mh-sent-from-msg.
+
+(defvar mh-annotate-field nil)		;Field name for message annotation.
+
+(put 'mh-letter-mode 'mode-class 'special)
+
+;;;###autoload
+(defun mh-letter-mode ()
+  "Mode for composing letters in mh-e.\\<mh-letter-mode-map>
+When you have finished composing, type \\[mh-send-letter] to send the message
+using the MH mail handling system.
+See the documentation for \\[mh-edit-mhn] for information on composing MIME
+messages.
+
+\\{mh-letter-mode-map}
+
+Variables controlling this mode (defaults in parentheses):
+
+ mh-delete-yanked-msg-window (nil)
+    If non-nil, \\[mh-yank-cur-msg] will delete any windows displaying
+    the yanked message.
+
+ mh-yank-from-start-of-msg (t)
+    If non-nil, \\[mh-yank-cur-msg] will include the entire message.
+    If `body', just yank the body (no header).
+    If nil, only the portion of the message following the point will be yanked.
+    If there is a region, this variable is ignored.
+
+ mh-ins-buf-prefix (\"> \")
+    String to insert before each non-blank line of a message as it is
+    inserted in a draft letter.
+
+ mh-signature-file-name (\"~/.signature\")
+    File to be inserted into message by \\[mh-insert-signature].
+
+Upon invoking mh-letter-mode, text-mode-hook and mh-letter-mode-hook are
+invoked with no args, if those values are non-nil."
+
+  (interactive)
+  (or mh-user-path (mh-find-path))
+  (make-local-variable 'paragraph-start)
+  (setq paragraph-start (concat "^[ \t]*[-_][-_][-_]+$\\|" paragraph-start))
+  (make-local-variable 'paragraph-separate)
+  (setq paragraph-separate
+	(concat "^[ \t]*[-_][-_][-_]+$\\|" paragraph-separate))
+  (make-local-variable 'mh-send-args)
+  (make-local-variable 'mh-annotate-char)
+  (make-local-variable 'mh-annotate-field)
+  (make-local-variable 'mh-previous-window-config)
+  (make-local-variable 'mh-sent-from-folder)
+  (make-local-variable 'mh-sent-from-msg)
+  (make-local-variable 'mail-header-separator)
+  (setq mail-header-separator "--------") ;for Hyperbole
+  (use-local-map mh-letter-mode-map)
+  (setq major-mode 'mh-letter-mode)
+  (mh-set-mode-name "MH-Letter")
+  (set-syntax-table mh-letter-mode-syntax-table)
+  (run-hooks 'text-mode-hook)
+  ;; if text-mode-hook turned on auto-fill, tune it for messages
+  (cond ((and (boundp 'auto-fill-hook) auto-fill-hook) ;emacs 18
+	 (make-local-variable 'auto-fill-hook)
+	 (setq auto-fill-hook 'mh-auto-fill-for-letter)))
+  (cond ((and (boundp 'auto-fill-function) auto-fill-function) ;emacs 19
+	 (make-local-variable 'auto-fill-function)
+	 (setq auto-fill-function 'mh-auto-fill-for-letter)))
+  (run-hooks 'mh-letter-mode-hook))
+
+
+(defun mh-auto-fill-for-letter ()
+  ;; Auto-fill in letters treats the header specially by inserting a tab
+  ;; before continuation line.
+  (if (mh-in-header-p)
+      (let ((fill-prefix "\t"))
+	(do-auto-fill))
+    (do-auto-fill)))
+
+
+(defun mh-in-header-p ()
+  ;; Return non-nil if the point is in the header of a draft message.
+  (save-excursion
+    (let ((cur-point (point)))
+      (goto-char (point-min))
+      (re-search-forward "^-*$" nil t)
+      (< cur-point (point)))))
+
+
+(defun mh-to-field ()
+  "Move point to the end of a specified header field.
+The field is indicated by the previous keystroke (the last keystroke
+of the command) according to the list in the variable mh-to-field-choices.
+Create the field if it does not exist.  Set the mark to point before moving."
+  (interactive)
+  (expand-abbrev)
+  (let ((target (cdr (or (assoc (char-to-string (logior last-input-char ?`))
+				mh-to-field-choices)
+			 ;; also look for a char for version 4 compat
+			 (assoc (logior last-input-char ?`) mh-to-field-choices))))
+	(case-fold-search t))
+    (push-mark)
+    (cond ((mh-position-on-field target)
+	   (let ((eol (point)))
+	     (skip-chars-backward " \t")
+	     (delete-region (point) eol))
+	   (if (and (not (eq (logior last-input-char ?`) ?s))
+		    (save-excursion
+		      (backward-char 1)
+		      (not (looking-at "[:,]"))))
+	       (insert ", ")
+	       (insert " ")))
+	  (t
+	   (if (mh-position-on-field "To:")
+	       (forward-line 1))
+	   (insert (format "%s \n" target))
+	   (backward-char 1)))))
+
+
+(defun mh-to-fcc (&optional folder)
+  "Insert an Fcc: FOLDER field in the current message.
+Prompt for the field name with a completion list of the current folders."
+  (interactive)
+  (or folder
+      (setq folder (mh-prompt-for-folder
+		    "Fcc"
+		    (or (and mh-default-folder-for-message-function
+			     (save-excursion
+			       (goto-char (point-min))
+			       (funcall mh-default-folder-for-message-function)))
+			"")
+		    t)))
+  (let ((last-input-char ?\C-f))
+    (expand-abbrev)
+    (save-excursion
+      (mh-to-field)
+      (insert (if (mh-folder-name-p folder)
+		  (substring folder 1)
+		folder)))))
+
+
+(defun mh-insert-signature ()
+  "Insert the file named by mh-signature-file-name at the current point."
+  (interactive)
+  (insert-file-contents mh-signature-file-name)
+  (set-buffer-modified-p (buffer-modified-p))) ; force mode line update
+
+
+(defun mh-check-whom ()
+  "Verify recipients of the current letter, showing expansion of any aliases."
+  (interactive)
+  (let ((file-name buffer-file-name))
+    (save-buffer)
+    (message "Checking recipients...")
+    (mh-in-show-buffer ("*Recipients*")
+      (bury-buffer (current-buffer))
+      (erase-buffer)
+      (mh-exec-cmd-output "whom" t file-name))
+    (message "Checking recipients...done")))
+
+
+
+;;; Routines to compose and send a letter.
+
+(defun mh-compose-and-send-mail (draft send-args
+				       sent-from-folder sent-from-msg
+				       to subject cc
+				       annotate-char annotate-field
+				       config)
+  ;; Edit and compose a draft message in buffer DRAFT and send or save it.
+  ;; SENT-FROM-FOLDER is buffer containing scan listing of current folder, or
+  ;; nil if none exists.
+  ;; SENT-FROM-MSG is the message number or sequence name or nil.
+  ;; SEND-ARGS is an optional argument passed to the send command.
+  ;; The TO, SUBJECT, and CC fields are passed to the
+  ;; mh-compose-letter-function.
+  ;; If ANNOTATE-CHAR is non-null, it is used to notate the scan listing of the
+  ;; message.  In that case, the ANNOTATE-FIELD is used to build a string
+  ;; for mh-annotate-msg.
+  ;; CONFIG is the window configuration to restore after sending the letter.
+  (pop-to-buffer draft)
+  (mh-letter-mode)
+  (setq mh-sent-from-folder sent-from-folder)
+  (setq mh-sent-from-msg sent-from-msg)
+  (setq mh-send-args send-args)
+  (setq mh-annotate-char annotate-char)
+  (setq mh-annotate-field annotate-field)
+  (setq mh-previous-window-config config)
+  (setq mode-line-buffer-identification (list "{%b}"))
+  (if (and (boundp 'mh-compose-letter-function)
+	   mh-compose-letter-function)
+      ;; run-hooks will not pass arguments.
+      (let ((value mh-compose-letter-function))
+	(if (and (listp value) (not (eq (car value) 'lambda)))
+	    (while value
+	      (funcall (car value) to subject cc)
+	      (setq value (cdr value)))
+	    (funcall mh-compose-letter-function to subject cc)))))
+
+
+(defun mh-send-letter (&optional arg)
+  "Send the draft letter in the current buffer.
+If optional prefix argument is provided, monitor delivery.
+Run mh-before-send-letter-hook before doing anything."
+  (interactive "P")
+  (run-hooks 'mh-before-send-letter-hook)
+  (save-buffer)
+  (message "Sending...")
+  (let ((draft-buffer (current-buffer))
+	(file-name buffer-file-name)
+	(config mh-previous-window-config))
+    (cond (arg
+	   (pop-to-buffer "MH mail delivery")
+	   (erase-buffer)
+	   (mh-exec-cmd-output mh-send-prog t "-watch" "-nopush"
+			       "-nodraftfolder" mh-send-args file-name)
+	   (goto-char (point-max))	; show the interesting part
+	   (recenter -1)
+	   (set-buffer draft-buffer))	; for annotation below
+	  (t
+	   (mh-exec-cmd-daemon mh-send-prog "-nodraftfolder" "-noverbose"
+			       mh-send-args file-name)))
+    (if mh-annotate-char
+	(mh-annotate-msg mh-sent-from-msg
+			 mh-sent-from-folder
+			 mh-annotate-char
+			 "-component" mh-annotate-field
+			 "-text" (format "\"%s %s\""
+					 (mh-get-header-field "To:")
+					 (mh-get-header-field "Cc:"))))
+
+    (cond ((or (not arg)
+	       (y-or-n-p "Kill draft buffer? "))
+	   (kill-buffer draft-buffer)
+	   (if config
+	       (set-window-configuration config))))
+    (if arg
+	(message "Sending...done")
+      (message "Sending...backgrounded"))))
+
+
+(defun mh-insert-letter (folder message verbatim)
+  "Insert a message into the current letter.
+Removes the message's headers using mh-invisible-headers.  Prefixes
+each non-blank line with mh-ins-buf-prefix.  Prompts for FOLDER and
+MESSAGE.  If prefix argument VERBATIM provided, do not indent and do
+not delete headers.  Leaves the mark before the letter and point after it."
+  (interactive
+   (list (mh-prompt-for-folder "Message from" mh-sent-from-folder nil)
+	 (read-input (format "Message number%s: "
+			     (if mh-sent-from-msg
+				 (format " [%d]" mh-sent-from-msg)
+			       "")))
+	 current-prefix-arg))
+  (save-restriction
+    (narrow-to-region (point) (point))
+    (let ((start (point-min)))
+      (if (equal message "") (setq message (int-to-string mh-sent-from-msg)))
+      (mh-exec-lib-cmd-output "mhl" "-nobell" "-noclear"
+			      (expand-file-name message
+						(mh-expand-file-name folder)))
+      (cond ((not verbatim)
+	     (mh-clean-msg-header start mh-invisible-headers mh-visible-headers)
+	     (set-mark start)		; since mh-clean-msg-header moves it
+	     (mh-insert-prefix-string mh-ins-buf-prefix))))))
+
+
+(defun mh-yank-cur-msg ()
+  "Insert the current message into the draft buffer.
+Prefix each non-blank line in the message with the string in
+`mh-ins-buf-prefix'.  If a region is set in the message's buffer, then
+only the region will be inserted.  Otherwise, the entire message will
+be inserted if `mh-yank-from-start-of-msg' is non-nil.  If this variable
+is nil, the portion of the message following the point will be yanked.
+If `mh-delete-yanked-msg-window' is non-nil, any window displaying the
+yanked message will be deleted."
+  (interactive)
+  (if (and mh-sent-from-folder mh-sent-from-msg)
+      (let ((to-point (point))
+	    (to-buffer (current-buffer)))
+	(set-buffer mh-sent-from-folder)
+	(if mh-delete-yanked-msg-window
+	    (delete-windows-on mh-show-buffer))
+	(set-buffer mh-show-buffer)	; Find displayed message
+	(let ((mh-ins-str (cond ((if (boundp 'mark-active)
+				     mark-active ;Emacs 19
+				   (mark)) ;Emacs 18
+				 (buffer-substring (region-beginning)
+						   (region-end)))
+				((eq 'body mh-yank-from-start-of-msg)
+				 (buffer-substring
+				  (save-excursion
+				    (goto-char (point-min))
+				    (mh-goto-header-end 1)
+				    (point))
+				  (point-max)))
+				(mh-yank-from-start-of-msg
+				 (buffer-substring (point-min) (point-max)))
+				(t
+				 (buffer-substring (point) (point-max))))))
+	  (set-buffer to-buffer)
+	  (save-restriction
+	    (narrow-to-region to-point to-point)
+	    (push-mark)
+	    (insert mh-ins-str)
+	    (mh-insert-prefix-string mh-ins-buf-prefix)
+	    (insert "\n"))))
+    (error "There is no current message")))
+
+
+(defun mh-insert-prefix-string (mh-ins-string)
+  ;; Run mail-citation-hook to insert a prefix string before each line
+  ;; in the buffer.  Generality for supercite users.
+  (set-mark (point-max))
+  (goto-char (point-min))
+  (cond (mail-citation-hook
+	 (run-hooks 'mail-citation-hook))
+	(mh-yank-hooks			;old hook name
+	 (run-hooks 'mh-yank-hooks))
+	(t
+	 (or (bolp) (forward-line 1))
+	 (let ((zmacs-regions nil))	;so "(mark)" works in XEmacs
+	   (while (< (point) (mark))
+	     (insert mh-ins-string)
+	     (forward-line 1))))))
+
+
+(defun mh-fully-kill-draft ()
+  "Kill the draft message file and the draft message buffer.
+Use \\[kill-buffer] if you don't want to delete the draft message file."
+  (interactive)
+  (if (y-or-n-p "Kill draft message? ")
+      (let ((config mh-previous-window-config))
+	(if (file-exists-p buffer-file-name)
+	    (delete-file buffer-file-name))
+	(set-buffer-modified-p nil)
+	(kill-buffer (buffer-name))
+	(message "")
+	(if config
+	    (set-window-configuration config)))
+    (error "Message not killed")))
+
+
+;;; Build the letter-mode keymap:
+
+(define-key mh-letter-mode-map "\C-c\C-f\C-b" 'mh-to-field)
+(define-key mh-letter-mode-map "\C-c\C-f\C-c" 'mh-to-field)
+(define-key mh-letter-mode-map "\C-c\C-f\C-d" 'mh-to-field)
+(define-key mh-letter-mode-map "\C-c\C-f\C-f" 'mh-to-fcc)
+(define-key mh-letter-mode-map "\C-c\C-f\C-r" 'mh-to-field)
+(define-key mh-letter-mode-map "\C-c\C-f\C-s" 'mh-to-field)
+(define-key mh-letter-mode-map "\C-c\C-f\C-t" 'mh-to-field)
+(define-key mh-letter-mode-map "\C-c\C-fb" 'mh-to-field)
+(define-key mh-letter-mode-map "\C-c\C-fc" 'mh-to-field)
+(define-key mh-letter-mode-map "\C-c\C-fd" 'mh-to-field)
+(define-key mh-letter-mode-map "\C-c\C-ff" 'mh-to-fcc)
+(define-key mh-letter-mode-map "\C-c\C-fr" 'mh-to-field)
+(define-key mh-letter-mode-map "\C-c\C-fs" 'mh-to-field)
+(define-key mh-letter-mode-map "\C-c\C-ft" 'mh-to-field)
+(define-key mh-letter-mode-map "\C-c\C-i" 'mh-insert-letter)
+(define-key mh-letter-mode-map "\C-c\C-q" 'mh-fully-kill-draft)
+(define-key mh-letter-mode-map "\C-c\C-\\" 'mh-fully-kill-draft) ;if no C-q
+(define-key mh-letter-mode-map "\C-c\C-s" 'mh-insert-signature)
+(define-key mh-letter-mode-map "\C-c\C-^" 'mh-insert-signature) ;if no C-s
+(define-key mh-letter-mode-map "\C-c\C-w" 'mh-check-whom)
+(define-key mh-letter-mode-map "\C-c\C-y" 'mh-yank-cur-msg)
+(define-key mh-letter-mode-map "\C-c\C-c" 'mh-send-letter)
+(define-key mh-letter-mode-map "\C-c\C-m\C-f" 'mh-mhn-compose-forw)
+(define-key mh-letter-mode-map "\C-c\C-m\C-e" 'mh-mhn-compose-anon-ftp)
+(define-key mh-letter-mode-map "\C-c\C-m\C-t" 'mh-mhn-compose-external-compressed-tar)
+(define-key mh-letter-mode-map "\C-c\C-m\C-i" 'mh-mhn-compose-insertion)
+(define-key mh-letter-mode-map "\C-c\C-e" 'mh-edit-mhn)
+(define-key mh-letter-mode-map "\C-c\C-m\C-u" 'mh-revert-mhn-edit)
+
+;; "C-c /" prefix is used in mh-letter-mode by pgp.el
+
+;;; autoloads from mh-mime
+
+(autoload 'mh-mhn-compose-insertion "mh-mime"
+  "Add a directive to insert a MIME message part from a file.
+This is the typical way to insert non-text parts in a message.
+See also \\[mh-edit-mhn]." t)
+
+(autoload 'mh-mhn-compose-anon-ftp "mh-mime"
+  "Add a directive for a MIME anonymous ftp external body part.
+This directive tells MH to include a reference to a
+message/external-body part retrievable by anonymous FTP.
+See also \\[mh-edit-mhn]." t)
+
+(autoload 'mh-mhn-compose-external-compressed-tar "mh-mime"
+  "Add a directive to include a MIME reference to a compressed tar file.
+The file should be available via anonymous ftp.  This directive
+tells MH to include a reference to a message/external-body part.
+See also \\[mh-edit-mhn]." t)
+
+(autoload 'mh-mhn-compose-forw "mh-mime"
+  "Add a forw directive to this message, to forward a message with MIME.
+This directive tells MH to include another message in this one.
+See also \\[mh-edit-mhn]." t)
+
+(autoload 'mh-edit-mhn "mh-mime"
+  "Format the current draft for MIME, expanding any mhn directives.
+Process the current draft with the mhn program, which,
+using directives already inserted in the draft, fills in
+all the MIME components and header fields.
+This step should be done last just before sending the message.
+The mhn program is part of MH version 6.8 or later.
+The `\\[mh-revert-mhn-edit]' command undoes this command.
+For assistance with creating mhn directives to insert
+various types of components in a message, see
+\\[mh-mhn-compose-insertion] (generic insertion from a file),
+\\[mh-mhn-compose-anon-ftp] (external reference to file via anonymous ftp),
+\\[mh-mhn-compose-external-compressed-tar] \
+\(reference to compressed tar file via anonymous ftp), and
+\\[mh-mhn-compose-forw] (forward message)." t)
+
+(autoload 'mh-revert-mhn-edit "mh-mime"
+  "Undoes the effect of \\[mh-edit-mhn] by reverting to the backup file.
+Optional non-nil argument means don't ask for confirmation." t)
+;;; mh-e.el --- GNU Emacs interface to the MH mail system
+
+;;; Copyright (C) 1985,86,87,88,90,92,93,94,95 Free Software Foundation, Inc.
+
+(eval '(run-hooks 'mh-e-load-hook))
+
+(defconst mh-e-time-stamp "Time-stamp: <95/10/30 19:14:06 gildea>")
+(defconst mh-e-version "5.0.2"
+  "Version numbers of this version of mh-e.")
+
+;; Maintainer: Stephen Gildea <gildea@lcs.mit.edu>
+;; Version: 5.0.2
+;; Keywords: mail
+;; Bug-reports: include `M-x mh-version' output in any correspondence
+
+;; This file is part of mh-e, part of GNU Emacs.
+
+;; GNU Emacs is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 2, or (at your option)
+;; any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs; see the file COPYING.  If not, write to
+;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+
+;;; Commentary:
+
+;;; HOW TO USE:
+;;; M-x mh-rmail to read mail.  Type C-h m there for a list of commands.
+;;; C-u M-x mh-rmail to visit any folder.
+;;; M-x mh-smail to send mail.  From within the mail reader, "m" works, too.
+
+;;; MH (Message Handler) is a powerful mail reader.  The MH newsgroup
+;;; is comp.mail.mh; the mailing list is mh-users@ics.uci.edu (send to
+;;; mh-users-request to be added).  See the monthly Frequently Asked
+;;; Questions posting there for information on getting MH and mh-e.
+
+;;; mh-e is an Emacs interface to the MH mail system.
+;;; The mailing list mh-e@x.org is for discussion of mh-e and
+;;; announcements of new versions.  Send a "subscribe" message to
+;;; mh-e-request@x.org to be added.  Do not report bugs here; mail
+;;; them directly to the author (see top of mh-e.el source).
+;;; Include the output of M-x mh-version in any bug report.
+
+;;; mh-e works with GNU Emacs 18 or 19, and MH 6.
+
+;;; NB.  MH must have been compiled with the MHE compiler flag or several
+;;; features necessary for mh-e will be missing from MH commands, specifically
+;;; the -build switch to repl and forw.
+
+;;; Your .emacs might benefit from these bindings:
+;;; (global-set-key "\C-cr" 'mh-rmail)
+;;; (global-set-key "\C-xm" 'mh-smail)
+;;; (global-set-key "\C-x4m" 'mh-smail-other-window)
+
+;;; Change Log:
+
+;;; Original version for Gosling emacs by Brian Reid, Stanford, 1982.
+;;; Modified by James Larus, BBN, July 1984 and UCB, 1984 & 1985.
+;;; Rewritten for GNU Emacs, James Larus 1985.  larus@ginger.berkeley.edu
+;;; Modified by Stephen Gildea 1988.  gildea@lcs.mit.edu
+(defconst mh-e-RCS-id "$Id$")
+
+;;; Code:
+
+(provide 'mh-e)
+(require 'mh-utils)
+
+
+;;; Hooks:
+
+(defgroup mh nil
+  "Emacs interface to the MH mail system"
+  :group 'mail)
+
+(defgroup mh-hook nil
+  "Hooks to mh-e mode"
+  :prefix "mh-"
+  :group 'mh)
+
+
+(defcustom mh-folder-mode-hook nil
+  "Invoked in MH-Folder mode on a new folder."
+  :type 'hook
+  :group 'mh-hook)
+
+(defcustom mh-inc-folder-hook nil
+  "Invoked by \\<mh-folder-mode-map>`\\[mh-inc-folder]' after incorporating mail into a folder."
+  :type 'hook
+  :group 'mh-hook)
+
+(defcustom mh-show-hook nil
+  "Invoked after \\<mh-folder-mode-map>`\\[mh-show]' shows a message."
+  :type 'hook
+  :group 'mh-hook)
+
+(defcustom mh-show-mode-hook nil
+  "Invoked in MH-Show mode on each message."
+  :type 'hook
+  :group 'mh-hook)
+
+(defcustom mh-delete-msg-hook nil
+  "Invoked after marking each message for deletion."
+  :type 'hook
+  :group 'mh-hook)
+
+(defcustom mh-refile-msg-hook nil
+  "Invoked after marking each message for refiling."
+  :type 'hook
+  :group 'mh-hook)
+
+(defcustom mh-before-quit-hook nil
+  "Invoked by \\<mh-folder-mode-map>`\\[mh-quit]' before quitting mh-e.  See also  mh-quit-hook."
+  :type 'hook
+  :group 'mh-hook)
+
+(defcustom mh-quit-hook nil
+  "Invoked after \\<mh-folder-mode-map>`\\[mh-quit]' quits mh-e.  See also  mh-before-quit-hook."
+  :type 'hook
+  :group 'mh-hook)
+
+
+
+;;; Personal preferences:
+
+(defcustom mh-lpr-command-format "lpr -J '%s'"
+  "*Format for Unix command that prints a message.
+The string should be a Unix command line, with the string '%s' where
+the job's name (folder and message number) should appear.  The formatted
+message text is piped to this command when you type \\<mh-folder-mode-map>`\\[mh-print-msg]'."
+  :type 'string
+  :group 'mh)
+
+(defcustom mh-scan-prog "scan"
+  "*Program to run to generate one-line-per-message listing of a folder.
+Normally \"scan\" or a file name linked to scan.  This file is searched
+for relative to the mh-progs directory unless it is an absolute pathname.
+Automatically becomes buffer-local when set in any fashion."
+  :type 'string
+  :group 'mh)
+(make-variable-buffer-local 'mh-scan-prog)
+
+(defcustom mh-inc-prog "inc"
+  "*Program to run to incorporate new mail into a folder.
+Normally \"inc\".  This file is searched for relative to
+the mh-progs directory unless it is an absolute pathname."
+  :type 'string
+  :group 'mh)
+
+(defcustom mh-folder-coding-system 'ctext
+  "*Coding-system to decode folder."
+  :type 'coding-system
+  :group 'mh)
+(make-variable-buffer-local 'mh-folder-coding-system)
+
+(defcustom mh-print-background nil
+  "*Print messages in the background if non-nil.
+WARNING: do not delete the messages until printing is finished;
+otherwise, your output may be truncated."
+  :type 'boolean
+  :group 'mh)
+
+(defcustom mh-recenter-summary-p nil
+  "*Recenter summary window when the show window is toggled off if non-nil."
+  :type 'boolean
+  :group 'mh)
+
+(defcustom mh-do-not-confirm nil
+  "*Non-nil means do not prompt for confirmation before some mh-e commands.
+Affects non-recoverable commands such as mh-kill-folder and mh-undo-folder."
+  :type 'boolean
+  :group 'mh)
+
+(defcustom mh-store-default-directory nil
+  "*Last directory used by \\[mh-store-msg]; default for next store.
+A directory name string, or nil to use current directory."
+  :type '(choice (const :tag "Current" nil)
+		 directory)
+  :group 'mh)
+
+;;; Parameterize mh-e to work with different scan formats.  The defaults work
+;;; with the standard MH scan listings, in which the first 4 characters on
+;;; the line are the message number, followed by two places for notations.
+
+(defvar mh-good-msg-regexp  "^....[^D^]"
+  "Regexp specifiying the scan lines that are 'good' messages.")
+
+(defvar mh-deleted-msg-regexp "^....D"
+  "Regexp matching scan lines of deleted messages.")
+
+(defvar mh-refiled-msg-regexp  "^....\\^"
+  "Regexp matching scan lines of refiled messages.")
+
+(defvar mh-valid-scan-line "^ *[0-9]"
+  "Regexp matching scan lines for messages (not error messages).")
+
+(defvar mh-cur-scan-msg-regexp "^....\\+"
+  "Regexp matching scan line for the cur message.")
+
+(defvar mh-note-deleted "D"
+  "String whose first character is used to notate deleted messages.")
+
+(defvar mh-note-refiled "^"
+  "String whose first character is used to notate refiled messages.")
+
+(defvar mh-note-cur "+"
+  "String whose first character is used to notate the current message.")
+
+(defvar mh-partial-folder-mode-line-annotation "select"
+  "Annotation when displaying part of a folder.
+The string is displayed after the folder's name.  NIL for no annotation.")
+
+
+;;; Internal variables:
+
+(defvar mh-last-destination nil)	;Destination of last refile or write command.
+
+(defvar mh-folder-mode-map (make-keymap)
+  "Keymap for MH folders.")
+
+(defvar mh-delete-list nil)		;List of msg numbers to delete.
+
+(defvar mh-refile-list nil)		;List of folder names in mh-seq-list.
+
+(defvar mh-next-direction 'forward)	;Direction to move to next message.
+
+(defvar mh-narrowed-to-seq nil)		;Sequence display is narrowed to or nil if not narrowed.
+
+(defvar mh-first-msg-num nil)		;Number of first msg in buffer.
+
+(defvar mh-last-msg-num nil)		;Number of last msg in buffer.
+
+(defvar mh-mode-line-annotation nil)	;Indiction this is not the full folder.
+
+;;; Macros and generic functions:
+
+(defun mh-mapc (func list)
+  (while list
+    (funcall func (car list))
+    (setq list (cdr list))))
+
+
+
+;;; Entry points:
+
+;;;###autoload
+(defun mh-rmail (&optional arg)
+  "Inc(orporate) new mail with MH, or, with arg, scan an MH mail folder.
+This function is an entry point to mh-e, the Emacs front end
+to the MH mail system."
+  (interactive "P")
+  (mh-find-path)
+  (if arg
+      (call-interactively 'mh-visit-folder)
+      (mh-inc-folder)))
+
+
+;;; mh-smail and mh-smail-other-window have been moved to the new file
+;;; mh-comp.el, but Emacs 18 still looks for them here, so provide a
+;;; definition here, too, for a while.
+
+(defun mh-smail ()
+  "Compose and send mail with the MH mail system.
+This function is an entry point to mh-e, the Emacs front end
+to the MH mail system."
+  (interactive)
+  (mh-find-path)
+  (require 'mh-comp)
+  (call-interactively 'mh-send))
+
+
+(defun mh-smail-other-window ()
+  "Compose and send mail in other window with the MH mail system.
+This function is an entry point to mh-e, the Emacs front end
+to the MH mail system."
+  (interactive)
+  (mh-find-path)
+  (require 'mh-comp)
+  (call-interactively 'mh-send-other-window))
+
+
+
+;;; User executable mh-e commands:
+
+
+(defun mh-delete-msg (msg-or-seq)
+  "Mark the specified MESSAGE(s) for subsequent deletion and move to the next.
+Default is the displayed message.  If optional prefix argument is
+given then prompt for the message sequence."
+  (interactive (list (if current-prefix-arg
+			 (mh-read-seq-default "Delete" t)
+			 (mh-get-msg-num t))))
+  (mh-delete-msg-no-motion msg-or-seq)
+  (mh-next-msg))
+
+
+(defun mh-delete-msg-no-motion (msg-or-seq)
+  "Mark the specified MESSAGE(s) for subsequent deletion.
+Default is the displayed message.  If optional prefix argument is
+provided, then prompt for the message sequence."
+  (interactive (list (if current-prefix-arg
+			 (mh-read-seq-default "Delete" t)
+			 (mh-get-msg-num t))))
+  (if (numberp msg-or-seq)
+      (mh-delete-a-msg msg-or-seq)
+      (mh-map-to-seq-msgs 'mh-delete-a-msg msg-or-seq)))
+
+
+(defun mh-execute-commands ()
+  "Process outstanding delete and refile requests."
+  (interactive)
+  (if mh-narrowed-to-seq (mh-widen))
+  (mh-process-commands mh-current-folder)
+  (mh-set-scan-mode)
+  (mh-goto-cur-msg)			; after mh-set-scan-mode for efficiency
+  (mh-make-folder-mode-line)
+  t)					; return t for [local-]write-file-hooks
+
+
+(defun mh-first-msg ()
+  "Move to the first message."
+  (interactive)
+  (goto-char (point-min))
+  (while (and (not (eobp)) (not (looking-at mh-valid-scan-line)))
+    (forward-line 1)))
+
+
+(defun mh-header-display ()
+  "Show the current message with all its headers.
+Displays headers that might have been suppressed by setting the
+variables `mh-clean-message-header' or `mhl-formfile', or by the fallback
+behavior of scrolling uninteresting headers off the top of the window.
+Type \"\\[mh-show]\" to show the message normally again."
+  (interactive)
+  (and (not mh-showing-with-headers)
+       (or mhl-formfile mh-clean-message-header)
+       (mh-invalidate-show-buffer))
+  (let ((mhl-formfile nil)
+	(mh-clean-message-header nil))
+    (mh-show-msg nil)
+    (mh-in-show-buffer (mh-show-buffer)
+      (goto-char (point-min))
+      (mh-recenter 0))
+    (setq mh-showing-with-headers t)))
+
+
+(defun mh-inc-folder (&optional maildrop-name)
+  "Inc(orporate)s new mail into the Inbox folder.
+Optional prefix argument specifies an alternate maildrop from the default.
+If the prefix argument is given, incorporates mail into the current
+folder, otherwise uses the folder named by `mh-inbox'.
+Runs `mh-inc-folder-hook' after incorporating new mail.
+Do not call this function from outside mh-e; use \\[mh-rmail] instead."
+  (interactive (list (if current-prefix-arg
+			 (expand-file-name
+			  (read-file-name "inc mail from file: "
+					  mh-user-path)))))
+  (let ((config (current-window-configuration)))
+    (if (not maildrop-name)
+	(cond ((not (get-buffer mh-inbox))
+	       (mh-make-folder mh-inbox)
+	       (setq mh-previous-window-config config))
+	      ((not (eq (current-buffer) (get-buffer mh-inbox)))
+	       (switch-to-buffer mh-inbox)
+	       (setq mh-previous-window-config config)))))
+  (mh-get-new-mail maildrop-name)
+  (run-hooks 'mh-inc-folder-hook))
+
+
+(defun mh-last-msg ()
+  "Move to the last message."
+  (interactive)
+  (goto-char (point-max))
+  (while (and (not (bobp)) (looking-at "^$"))
+    (forward-line -1)))
+
+
+(defun mh-next-undeleted-msg (&optional arg)
+  "Move to the NTH next undeleted message in window."
+  (interactive "p")
+  (setq mh-next-direction 'forward)
+  (forward-line 1)
+  (cond ((re-search-forward mh-good-msg-regexp nil 0 arg)
+	 (beginning-of-line)
+	 (mh-maybe-show))
+	(t
+	 (forward-line -1)
+	 (if (get-buffer mh-show-buffer)
+	     (delete-windows-on mh-show-buffer)))))
+
+
+(defun mh-refile-msg (msg-or-seq folder)
+  "Refile MESSAGE(s) (default: displayed message) into FOLDER.
+If optional prefix argument provided, then prompt for message sequence."
+  (interactive
+   (list (if current-prefix-arg
+	     (mh-read-seq-default "Refile" t)
+	   (mh-get-msg-num t))
+	 (intern
+	  (mh-prompt-for-folder
+	   "Destination"
+	   (or (and mh-default-folder-for-message-function
+		    (let ((refile-file (mh-msg-filename (mh-get-msg-num t))))
+		      (save-excursion
+			(set-buffer (get-buffer-create mh-temp-buffer))
+			(erase-buffer)
+			(insert-file-contents refile-file)
+			(let ((buffer-file-name refile-file))
+			  (funcall mh-default-folder-for-message-function)))))
+	       (and (eq 'refile (car mh-last-destination))
+		    (symbol-name (cdr mh-last-destination)))
+	       "")
+	   t))))
+  (setq mh-last-destination (cons 'refile folder))
+  (if (numberp msg-or-seq)
+      (mh-refile-a-msg msg-or-seq folder)
+      (mh-map-to-seq-msgs 'mh-refile-a-msg msg-or-seq folder))
+  (mh-next-msg))
+
+
+(defun mh-refile-or-write-again (message)
+  "Re-execute the last refile or write command on the given MESSAGE.
+Default is the displayed message.  Use the same folder or file as the
+previous refile or write command."
+  (interactive (list (mh-get-msg-num t)))
+  (if (null mh-last-destination)
+      (error "No previous refile or write"))
+  (cond ((eq (car mh-last-destination) 'refile)
+	 (mh-refile-a-msg message (cdr mh-last-destination))
+	 (message "Destination folder: %s" (cdr mh-last-destination)))
+	(t
+	 (apply 'mh-write-msg-to-file message (cdr mh-last-destination))
+	 (message "Destination: %s" (cdr mh-last-destination))))
+  (mh-next-msg))
+
+
+(defun mh-quit ()
+  "Quit the current mh-e folder.
+Start by running mh-before-quit-hook.  Restore the previous window
+configuration, if one exists.  Finish by running mh-quit-hook."
+  (interactive)
+  (run-hooks 'mh-before-quit-hook) 
+  (mh-update-sequences)
+  (mh-invalidate-show-buffer)
+  (bury-buffer (current-buffer))
+  (if (get-buffer mh-show-buffer)
+      (bury-buffer mh-show-buffer))
+  (if mh-previous-window-config
+      (set-window-configuration mh-previous-window-config))
+  (run-hooks 'mh-quit-hook))
+
+(defun mh-page-msg (&optional arg)
+  "Page the displayed message forwards.
+Scrolls ARG lines or a full screen if no argument is supplied."
+  (interactive "P")
+  (scroll-other-window arg))
+
+
+(defun mh-previous-page (&optional arg)
+  "Page the displayed message backwards.
+Scrolls ARG lines or a full screen if no argument is supplied."
+  (interactive "P")
+  (mh-in-show-buffer (mh-show-buffer)
+    (scroll-down arg)))
+
+
+(defun mh-previous-undeleted-msg (&optional arg)
+  "Move to the NTH previous undeleted message in window."
+  (interactive "p")
+  (setq mh-next-direction 'backward)
+  (beginning-of-line)
+  (cond ((re-search-backward mh-good-msg-regexp nil 0 arg)
+	 (mh-maybe-show))
+	(t
+	 (if (get-buffer mh-show-buffer)
+	     (delete-windows-on mh-show-buffer)))))
+
+
+(defun mh-rescan-folder (&optional range)
+  "Rescan a folder after optionally processing the outstanding commands.
+If optional prefix argument is provided, prompt for the range of
+messages to display.  Otherwise show the entire folder."
+  (interactive (list (if current-prefix-arg
+			 (mh-read-msg-range "Range to scan [all]? ")
+		       nil)))
+  (setq mh-next-direction 'forward)
+  (mh-scan-folder mh-current-folder (or range "all")))
+
+
+(defun mh-write-msg-to-file (msg file no-headers)
+  "Append MESSAGE to the end of a FILE.
+If NO-HEADERS (prefix argument) is provided, write only the message body.
+Otherwise send the entire message including the headers."
+  (interactive
+   (list (mh-get-msg-num t)
+	 (let ((default-dir (if (eq 'write (car mh-last-destination))
+				(file-name-directory (car (cdr mh-last-destination)))
+			      default-directory)))
+	   (read-file-name (format "Save message%s in file: "
+				   (if current-prefix-arg " body" ""))
+			   default-dir
+			   (if (eq 'write (car mh-last-destination))
+			       (car (cdr mh-last-destination))
+			     (expand-file-name "mail.out" default-dir))))
+	 current-prefix-arg))
+  (let ((msg-file-to-output (mh-msg-filename msg))
+	(output-file (mh-expand-file-name file)))
+    (setq mh-last-destination (list 'write file (if no-headers 'no-headers)))
+    (save-excursion
+      (set-buffer (get-buffer-create mh-temp-buffer))
+      (erase-buffer)
+      (insert-file-contents msg-file-to-output)
+      (goto-char (point-min))
+      (if no-headers (search-forward "\n\n"))
+      (append-to-file (point) (point-max) output-file))))
+
+
+(defun mh-toggle-showing ()
+  "Toggle the scanning mode/showing mode of displaying messages."
+  (interactive)
+  (if mh-showing
+      (mh-set-scan-mode)
+      (mh-show)))
+
+
+(defun mh-undo (msg-or-seq)
+  "Undo the pending deletion or refile of the specified MESSAGE(s).
+Default is the displayed message.  If optional prefix argument is
+provided, then prompt for the message sequence."
+  (interactive (list (if current-prefix-arg
+			 (mh-read-seq-default "Undo" t)
+			 (mh-get-msg-num t))))
+  (cond ((numberp msg-or-seq)
+	 (let ((original-position (point)))
+	   (beginning-of-line)
+	   (while (not (or (looking-at mh-deleted-msg-regexp)
+			   (looking-at mh-refiled-msg-regexp)
+			   (and (eq mh-next-direction 'forward) (bobp))
+			   (and (eq mh-next-direction 'backward)
+				(save-excursion (forward-line) (eobp)))))
+	     (forward-line (if (eq mh-next-direction 'forward) -1 1)))
+	   (if (or (looking-at mh-deleted-msg-regexp)
+		   (looking-at mh-refiled-msg-regexp))
+	       (progn
+		 (mh-undo-msg (mh-get-msg-num t))
+		 (mh-maybe-show))
+	       (goto-char original-position)
+	       (error "Nothing to undo"))))
+	(t
+	 (mh-map-to-seq-msgs 'mh-undo-msg msg-or-seq)))
+  ;; update the mh-refile-list so mh-outstanding-commands-p will work
+  (mh-mapc (function
+	    (lambda (elt)
+	      (if (not (mh-seq-to-msgs elt))
+		  (setq mh-refile-list (delq elt mh-refile-list)))))
+	   mh-refile-list)
+  (if (not (mh-outstanding-commands-p))
+      (mh-set-folder-modified-p nil)))
+
+
+;;;###autoload
+(defun mh-version ()
+  "Display version information about mh-e and the MH mail handling system."
+  (interactive)
+  (mh-find-progs)
+  (set-buffer (get-buffer-create mh-temp-buffer))
+  (erase-buffer)
+  (insert "  mh-e info:\n\nversion: " mh-e-version "\n" mh-e-time-stamp
+	  "\nEmacs: " emacs-version " on " (symbol-name system-type) " ")
+  (condition-case ()
+      (call-process "uname" nil t nil "-a")
+    (file-error))
+  (insert "\n\n  MH info:\n\n" (expand-file-name "inc" mh-progs) ":\n")
+  (let ((help-start (point)))
+    (condition-case err-data
+	(mh-exec-cmd-output "inc" nil "-help")
+      (file-error (insert (mapconcat 'concat (cdr err-data) ": "))))
+    (goto-char help-start)
+    (search-forward "version: " nil t)
+    (beginning-of-line)
+    (delete-region help-start (point))
+    (goto-char (point-min)))
+  (display-buffer mh-temp-buffer))
+
+
+(defun mh-visit-folder (folder &optional range)
+  "Visit FOLDER and display RANGE of messages.
+Do not call this function from outside mh-e; see \\[mh-rmail] instead."
+  (interactive (list (mh-prompt-for-folder "Visit" mh-inbox t)
+		     (mh-read-msg-range "Range [all]? ")))
+  (let ((config (current-window-configuration)))
+    (mh-scan-folder folder (or range "all"))
+    (setq mh-previous-window-config config))
+  nil)
+
+
+(defun mh-compat-quit ()
+  "The \"b\" key is obsolescent; will assume you want \"\\[mh-quit]\" ..."
+  ;; Was going to make it run mh-burst-digest, but got complaint that
+  ;; 'b' should mean 'back', as it does in info, less, and rn.
+  ;; This is a temporary compatibility function.
+  (interactive)
+  (message "%s" (documentation this-command))
+  (sit-for 1)
+  (call-interactively 'mh-quit))
+
+
+(defun mh-update-sequences ()
+  "Update MH's Unseen sequence and current folder and message.
+Flush mh-e's state out to MH.  The message at the cursor becomes current."
+  (interactive)
+  ;; mh-update-sequences is the opposite of mh-read-folder-sequences,
+  ;; which updates mh-e's state from MH.
+  (let ((folder-set (mh-update-unseen))
+	(new-cur (mh-get-msg-num nil)))
+    (if new-cur
+	(let ((seq-entry (mh-find-seq 'cur)))
+	  (mh-remove-cur-notation)
+	  (setcdr seq-entry (list new-cur)) ;delete-seq-locally, add-msgs-to-seq
+	  (mh-define-sequence 'cur (list new-cur))
+	  (beginning-of-line)
+	  (if (looking-at mh-good-msg-regexp)
+	      (mh-notate nil mh-note-cur mh-cmd-note)))
+      (or folder-set
+	  (save-excursion
+	    (mh-exec-cmd-quiet t "folder" mh-current-folder "-fast"))))))
+
+
+
+
+;;; Support routines.
+
+(defun mh-delete-a-msg (msg)
+  ;; Delete the MESSAGE.
+  (save-excursion
+    (mh-goto-msg msg nil t)
+    (if (looking-at mh-refiled-msg-regexp)
+	(error "Message %d is refiled.  Undo refile before deleting." msg))
+    (if (looking-at mh-deleted-msg-regexp)
+	nil
+	(mh-set-folder-modified-p t)
+	(setq mh-delete-list (cons msg mh-delete-list))
+	(mh-add-msgs-to-seq msg 'deleted t)
+	(mh-notate msg mh-note-deleted mh-cmd-note)
+	(run-hooks 'mh-delete-msg-hook))))
+
+(defun mh-refile-a-msg (msg destination)
+  ;; Refile MESSAGE in FOLDER.  FOLDER is a symbol, not a string.
+  (save-excursion
+    (mh-goto-msg msg nil t)
+    (cond ((looking-at mh-deleted-msg-regexp)
+	   (error "Message %d is deleted.  Undo delete before moving." msg))
+	  ((looking-at mh-refiled-msg-regexp)
+	   (if (y-or-n-p
+		(format "Message %d already refiled.  Copy to %s as well? "
+			msg destination))
+	       (mh-exec-cmd "refile" (mh-get-msg-num t) "-link"
+			    "-src" mh-current-folder
+			    (symbol-name destination))
+	       (message "Message not copied.")))
+	  (t
+	   (mh-set-folder-modified-p t)
+	   (if (not (memq destination mh-refile-list))
+	       (setq mh-refile-list (cons destination mh-refile-list)))
+	   (if (not (memq msg (mh-seq-to-msgs destination)))
+	       (mh-add-msgs-to-seq msg destination t))
+	   (mh-notate msg mh-note-refiled mh-cmd-note)
+	   (run-hooks 'mh-refile-msg-hook)))))
+
+
+(defun mh-next-msg ()
+  ;; Move backward or forward to the next undeleted message in the buffer.
+  (if (eq mh-next-direction 'forward)
+      (mh-next-undeleted-msg 1)
+      (mh-previous-undeleted-msg 1)))
+
+
+(defun mh-set-scan-mode ()
+  ;; Display the scan listing buffer, but do not show a message.
+  (if (get-buffer mh-show-buffer)
+      (delete-windows-on mh-show-buffer))
+  (setq mh-showing nil)
+  (set-buffer-modified-p (buffer-modified-p)) ;force mode line update
+  (if mh-recenter-summary-p
+      (mh-recenter nil)))
+
+
+(defun mh-undo-msg (msg)
+  ;; Undo the deletion or refile of one MESSAGE.
+  (cond ((memq msg mh-delete-list)
+	 (setq mh-delete-list (delq msg mh-delete-list))
+	 (mh-delete-msg-from-seq msg 'deleted t))
+	(t
+	 (mh-mapc (function (lambda (dest)
+			      (mh-delete-msg-from-seq msg dest t)))
+		  mh-refile-list)))
+  (mh-notate msg ?  mh-cmd-note))
+
+
+
+
+;;; The folder data abstraction.
+
+(defun mh-make-folder (name)
+  ;; Create and initialize a new mail folder called NAME and make it the
+  ;; current folder.
+  (switch-to-buffer name)
+  (setq buffer-read-only nil)
+  (erase-buffer)
+  (setq buffer-read-only t)
+  (mh-folder-mode)
+  (mh-set-folder-modified-p nil)
+  (setq buffer-file-name mh-folder-filename)
+  (mh-make-folder-mode-line))
+
+
+;;; Ensure new buffers won't get this mode if default-major-mode is nil.
+(put 'mh-folder-mode 'mode-class 'special)
+
+(defun mh-folder-mode ()
+  "Major mh-e mode for \"editing\" an MH folder scan listing.\\<mh-folder-mode-map>
+You can show the message the cursor is pointing to, and step through the
+messages.  Messages can be marked for deletion or refiling into another
+folder; these commands are executed all at once with a separate command.
+
+A prefix argument (\\[universal-argument]) to delete, refile, list, or undo
+applies the action to a message sequence.
+
+Here is a list of the standard keys for mh-e commands, grouped by function.
+This list is purposefully not customized; mh-e has a long history, and many
+alternate key bindings as a result.  This list is to encourage users to use
+standard keys so the other keys can perhaps someday be put to new uses.
+
+t	toggle show or scan-only mode
+RET	show message, or back to top if already showing
+
+SPC	page message forward
+DEL	page message back
+
+n	next message
+p	previous message
+g	go to message by number
+
+d	mark for deletion
+o, ^	mark for output (refile) to another folder
+?	show folder of pending refile
+u	undo delete or refile marking
+
+x	execute marked deletes and refiles
+i	incorporate new mail
+
+m	mail a new message
+r	reply to a message
+f	forward a message
+
+q	quit mh-e
+
+M-f	visit new folder
+M-r	rescan this folder
+
+Here are all the commands with their current binding, listed in key order:
+\\{mh-folder-mode-map}
+
+Variables controlling mh-e operation are (defaults in parentheses):
+
+ mh-recursive-folders (nil)
+    Non-nil means commands which operate on folders do so recursively.
+
+ mh-bury-show-buffer (t)
+    Non-nil means that the buffer used to display message is buried.
+    It will never be offered as the default other buffer.
+
+ mh-clean-message-header (nil)
+    Non-nil means remove header lines matching the regular expression
+    specified in mh-invisible-headers from messages.
+
+ mh-visible-headers (nil)
+    If non-nil, it contains a regexp specifying the headers that are shown in
+    a message if mh-clean-message-header is non-nil.  Setting this variable
+    overrides mh-invisible-headers.
+
+ mh-do-not-confirm (nil)
+    Non-nil means do not prompt for confirmation before executing some
+    non-recoverable commands such as mh-kill-folder and mh-undo-folder.
+
+ mhl-formfile (nil)
+    Name of format file to be used by mhl to show messages.
+    A value of T means use the default format file.
+    Nil means don't use mhl to format messages.
+
+ mh-lpr-command-format (\"lpr -p -J '%s'\")
+    Format for command used to print a message on a system printer.
+
+ mh-scan-prog (\"scan\")
+    Program to run to generate one-line-per-message listing of a folder.
+    Normally \"scan\" or a file name linked to scan.  This file is searched
+    for relative to the mh-progs directory unless it is an absolute pathname.
+    Automatically becomes buffer-local when set in any fashion.
+
+ mh-print-background (nil)
+    Print messages in the background if non-nil.
+    WARNING: do not delete the messages until printing is finished;
+    otherwise, your output may be truncated.
+
+ mh-recenter-summary-p (nil)
+    If non-nil, then the scan listing is recentered when the window displaying
+    a messages is toggled off.
+
+ mh-summary-height (4)
+    Number of lines in the summary window including the mode line.
+
+The value of mh-folder-mode-hook is called when a new folder is set up."
+
+  (kill-all-local-variables)
+  (use-local-map mh-folder-mode-map)
+  (setq major-mode 'mh-folder-mode)
+  (mh-set-mode-name "MH-Folder")
+  (mh-make-local-vars
+   'mh-current-folder (buffer-name)	; Name of folder, a string
+   'mh-show-buffer (format "show-%s" (buffer-name)) ; Buffer that displays msgs
+   'mh-folder-filename			; e.g. "/usr/foobar/Mail/inbox/"
+   (file-name-as-directory (mh-expand-file-name (buffer-name)))
+   'mh-showing nil			; Show message also?
+   'mh-delete-list nil			; List of msgs nums to delete
+   'mh-refile-list nil			; List of folder names in mh-seq-list
+   'mh-seq-list nil			; Alist of (seq . msgs) nums
+   'mh-seen-list nil			; List of displayed messages
+   'mh-next-direction 'forward		; Direction to move to next message
+   'mh-narrowed-to-seq nil		; Sequence display is narrowed to
+   'mh-first-msg-num nil		; Number of first msg in buffer
+   'mh-last-msg-num nil			; Number of last msg in buffer
+   'mh-msg-count nil			; Number of msgs in buffer
+   'mh-mode-line-annotation nil		; Indiction this is not the full folder
+   'mh-previous-window-config nil)	; Previous window configuration
+  (setq truncate-lines t)
+  (auto-save-mode -1)
+  (setq buffer-offer-save t)
+  ;; XEmacs change
+  (if (featurep 'scrollbar)
+      (set-specifier scrollbar-height (cons (current-buffer) 0)))
+  (if (boundp 'local-write-file-hooks)
+      (setq local-write-file-hooks '(mh-execute-commands)) ;Emacs 19
+    (make-local-variable 'write-file-hooks)
+    (setq write-file-hooks '(mh-execute-commands))) ;Emacs 18
+  (make-local-variable 'revert-buffer-function)
+  (setq revert-buffer-function 'mh-undo-folder)
+  (or (assq 'mh-showing minor-mode-alist)
+      (setq minor-mode-alist
+	    (cons '(mh-showing " Show") minor-mode-alist)))
+  (run-hooks 'mh-folder-mode-hook))
+
+
+(defun mh-make-local-vars (&rest pairs)
+  ;; Take VARIABLE-VALUE pairs and make local variables initialized to the
+  ;; value.
+  (while pairs
+    (make-variable-buffer-local (car pairs))
+    (set (car pairs) (car (cdr pairs)))
+    (setq pairs (cdr (cdr pairs)))))
+
+
+(defun mh-scan-folder (folder range)
+  ;; Scan the FOLDER over the RANGE.  Return in the folder's buffer.
+  (cond ((null (get-buffer folder))
+	 (mh-make-folder folder))
+	(t
+	 (mh-process-or-undo-commands folder)
+	 (switch-to-buffer folder)))
+  (mh-regenerate-headers range)
+  (cond ((zerop (buffer-size))
+	 (if (equal range "all")
+	     (message "Folder %s is empty" folder)
+	   (message "No messages in %s, range %s" folder range))
+	 (sit-for 5)))
+  (mh-goto-cur-msg))
+
+
+(defun mh-regenerate-headers (range &optional update)
+  ;; scan folder over range RANGE.
+  ;; If UPDATE, append the scan lines, otherwise replace.
+  (let ((folder mh-current-folder)
+	scan-start)
+    (message "Scanning %s..." folder)
+    (with-mh-folder-updating (nil)
+      (if update
+	  (goto-char (point-max))
+	(erase-buffer))
+      (setq scan-start (point))
+      (let ((coding-system-for-read mh-folder-coding-system))
+	(mh-exec-cmd-output mh-scan-prog nil
+			    "-noclear" "-noheader"
+			    "-width" (window-width)
+			    folder range))
+      (goto-char scan-start)
+      (cond ((looking-at "scan: no messages in")
+	     (keep-lines mh-valid-scan-line)) ; Flush random scan lines
+	    ((looking-at "scan: "))	; Keep error messages
+	    (t
+	     (keep-lines mh-valid-scan-line))) ; Flush random scan lines
+      (setq mh-seq-list (mh-read-folder-sequences folder nil))
+      (mh-notate-user-sequences)
+      (or update
+	  (setq mh-mode-line-annotation
+		(if (equal range "all")
+		    nil
+		  mh-partial-folder-mode-line-annotation)))
+      (mh-make-folder-mode-line))
+    (message "Scanning %s...done" folder)))
+
+
+(defun mh-get-new-mail (maildrop-name)
+  ;; Read new mail from a maildrop into the current buffer.
+  ;; Return in the current buffer.
+  (let ((point-before-inc (point))
+	(folder mh-current-folder)
+	(new-mail-p nil))
+    (with-mh-folder-updating (t)
+      (message (if maildrop-name
+		   (format "inc %s -file %s..." folder maildrop-name)
+		   (format "inc %s..." folder)))
+      (setq mh-next-direction 'forward)
+      (goto-char (point-max))
+      (let ((start-of-inc (point))
+	    (coding-system-for-read mh-folder-coding-system))
+	(if maildrop-name
+	    ;; I think MH 5 used "-ms-file" instead of "-file",
+	    ;; which would make inc'ing from maildrops fail.
+	    (mh-exec-cmd-output mh-inc-prog nil folder
+				"-file" (expand-file-name maildrop-name)
+				"-width" (window-width)
+				"-truncate")
+	    (mh-exec-cmd-output mh-inc-prog nil
+				"-width" (window-width)))
+	(message
+	 (if maildrop-name
+	     (format "inc %s -file %s...done" folder maildrop-name)
+	     (format "inc %s...done" folder)))
+	(goto-char start-of-inc)
+	(cond ((save-excursion
+		 (re-search-forward "^inc: no mail" nil t))
+	       (message "No new mail%s%s" (if maildrop-name " in " "")
+			(if maildrop-name maildrop-name "")))
+	      ((re-search-forward "^inc:" nil t) ; Error messages
+	       (error "inc error"))
+	      (t
+	       (mh-remove-cur-notation)
+	       (setq new-mail-p t)))
+	(keep-lines mh-valid-scan-line) ; Flush random scan lines
+	(setq mh-seq-list (mh-read-folder-sequences folder t))
+	(mh-notate-user-sequences)
+	(if new-mail-p
+	    (progn
+	      (mh-make-folder-mode-line)
+	      (mh-goto-cur-msg))
+	    (goto-char point-before-inc))))))
+
+
+(defun mh-make-folder-mode-line (&optional ignored)
+  ;; Set the fields of the mode line for a folder buffer.
+  ;; The optional argument is now obsolete.  It used to be used to pass
+  ;; in what is now stored in the buffer-local variable
+  ;; mh-mode-line-annotation.
+  (save-excursion
+    (mh-first-msg)
+    (setq mh-first-msg-num (mh-get-msg-num nil))
+    (mh-last-msg)
+    (setq mh-last-msg-num (mh-get-msg-num nil))
+    (setq mh-msg-count (count-lines (point-min) (point-max)))
+    (setq mode-line-buffer-identification
+	  (list (format "{%%b%s} %d msg%s"
+			(if mh-mode-line-annotation
+			    (format "/%s" mh-mode-line-annotation)
+			  "")
+			mh-msg-count
+			(if (zerop mh-msg-count)
+			    "s"
+			  (if (> mh-msg-count 1)
+			      (format "s (%d-%d)" mh-first-msg-num
+				      mh-last-msg-num)
+			    (format " (%d)" mh-first-msg-num))))))))
+
+
+(defun mh-unmark-all-headers (remove-all-flags)
+  ;; Remove all '+' flags from the headers, and if called with a non-nil
+  ;; argument, remove all 'D', '^' and '%' flags too.
+  ;; Optimized for speed (i.e., no regular expressions).
+  (save-excursion
+    (let ((case-fold-search nil)
+	  (last-line (1- (point-max)))
+	  char)
+      (mh-first-msg)
+      (while (<= (point) last-line)
+	(forward-char mh-cmd-note)
+	(setq char (following-char))
+	(if (or (and remove-all-flags
+		     (or (eql char (aref mh-note-deleted 0))
+			 (eql char (aref mh-note-refiled 0))))
+		(eql char (aref mh-note-cur 0)))
+	    (progn
+	      (delete-char 1)
+	      (insert " ")))
+	(if remove-all-flags
+	    (progn
+	      (forward-char 1)
+	      (if (eql (following-char) (aref mh-note-seq 0))
+		  (progn
+		    (delete-char 1)
+		    (insert " ")))))
+	(forward-line)))))
+
+
+(defun mh-remove-cur-notation ()
+  ;; Remove old cur notation (cf mh-goto-cur-msg code).
+  (let ((cur-msg (car (mh-seq-to-msgs 'cur))))
+    (save-excursion
+      (and cur-msg
+	   (mh-goto-msg cur-msg t t)
+	   (looking-at mh-cur-scan-msg-regexp)
+	   (mh-notate nil ?  mh-cmd-note)))))
+
+(defun mh-goto-cur-msg ()
+  ;; Position the cursor at the current message.
+  (let ((cur-msg (car (mh-seq-to-msgs 'cur))))
+    (cond ((and cur-msg
+		(mh-goto-msg cur-msg t t))
+	   (mh-notate nil mh-note-cur mh-cmd-note)
+	   (mh-recenter 0)
+	   (mh-maybe-show cur-msg))
+	  (t
+	   (mh-last-msg)
+	   (message "No current message")))))
+
+
+(defun mh-process-or-undo-commands (folder)
+  ;; If FOLDER has outstanding commands, then either process or discard them.
+  ;; Called by functions like mh-sort-folder, so also invalidate show buffer.
+  (set-buffer folder)
+  (if (mh-outstanding-commands-p)
+      (if (or mh-do-not-confirm
+	      (y-or-n-p
+		"Process outstanding deletes and refiles (or lose them)? "))
+	  (mh-process-commands folder)
+	  (mh-undo-folder)))
+  (mh-update-unseen)
+  (mh-invalidate-show-buffer))
+
+
+(defun mh-process-commands (folder)
+  ;; Process outstanding commands for the folder FOLDER.
+  (message "Processing deletes and refiles for %s..." folder)
+  (set-buffer folder)
+  (with-mh-folder-updating (nil)
+    ;; Update the unseen sequence if it exists
+    (mh-update-unseen)
+
+    ;; Then refile messages
+    (mh-mapc
+     (function
+      (lambda (dest)
+	(let ((msgs (mh-seq-to-msgs dest)))
+	  (cond (msgs
+		 (apply 'mh-exec-cmd "refile"
+			"-src" folder (symbol-name dest)
+			(mh-coalesce-msg-list msgs))
+		 (mh-delete-scan-msgs msgs))))))
+     mh-refile-list)
+    (setq mh-refile-list nil)
+
+    ;; Now delete messages
+    (cond (mh-delete-list
+	   (apply 'mh-exec-cmd "rmm" folder
+		  (mh-coalesce-msg-list mh-delete-list))
+	   (mh-delete-scan-msgs mh-delete-list)
+	   (setq mh-delete-list nil)))
+
+    ;; Don't need to remove sequences since delete and refile do so.
+
+    ;; Mark cur message
+    (if (> (buffer-size) 0)
+	(mh-define-sequence 'cur (list (or (mh-get-msg-num nil) "last"))))
+
+    (and (buffer-file-name (get-buffer mh-show-buffer))
+	 (not (file-exists-p (buffer-file-name (get-buffer mh-show-buffer))))
+	 ;; If "inc" were to put a new msg in this file,
+	 ;; we would not notice, so mark it invalid now.
+	 (mh-invalidate-show-buffer))
+
+    (setq mh-seq-list (mh-read-folder-sequences mh-current-folder nil))
+    (mh-unmark-all-headers t)
+    (mh-notate-user-sequences)
+    (message "Processing deletes and refiles for %s...done" folder)))
+
+
+(defun mh-update-unseen ()
+  ;; Flush updates to the Unseen sequence out to MH.
+  ;; Return non-NIL iff set the MH folder.
+  (if mh-seen-list
+      (let* ((unseen-seq (mh-find-seq mh-unseen-seq))
+	     (unseen-msgs (mh-seq-msgs unseen-seq)))
+	(if unseen-msgs
+	    (progn
+	      (mh-undefine-sequence mh-unseen-seq mh-seen-list)
+	      (while mh-seen-list
+		(setq unseen-msgs (delq (car mh-seen-list) unseen-msgs))
+		(setq mh-seen-list (cdr mh-seen-list)))
+	      (setcdr unseen-seq unseen-msgs)
+	      t)			;since we set the folder
+	  (setq mh-seen-list nil)))))
+
+
+(defun mh-delete-scan-msgs (msgs)
+  ;; Delete the scan listing lines for each of the msgs in the LIST.
+  (save-excursion
+    (while msgs
+      (if (mh-goto-msg (car msgs) t t)
+	  (mh-delete-line 1))
+      (setq msgs (cdr msgs)))))
+
+
+(defun mh-outstanding-commands-p ()
+  ;; Returns non-nil if there are outstanding deletes or refiles.
+  (or mh-delete-list mh-refile-list))
+
+
+(defun mh-coalesce-msg-list (messages)
+  ;; Give a list of MESSAGES, return a list of message number ranges.
+  ;; Sort of the opposite of mh-read-msg-list, which expands ranges.
+  ;; Message lists passed to MH programs go through this so
+  ;; command line arguments won't exceed system limits.
+  (let ((msgs (sort (copy-sequence messages) 'mh-greaterp))
+	(range-high nil)
+	(prev -1)
+	(ranges nil))
+    (while prev
+      (if range-high
+	  (if (or (not (numberp prev))
+		  (not (eql (car msgs) (1- prev))))
+	      (progn			;non-sequential, flush old range
+		(if (eql prev range-high)
+		    (setq ranges (cons range-high ranges))
+		  (setq ranges (cons (format "%s-%s" prev range-high) ranges)))
+		(setq range-high nil))))
+      (or range-high
+	  (setq range-high (car msgs))) ;start new or first range
+      (setq prev (car msgs))
+      (setq msgs (cdr msgs)))
+    ranges))
+
+(defun mh-greaterp (msg1 msg2)
+  ;; Sort two message indicators.  Strings are "smaller" than numbers.
+  ;; Legal values are things like "cur", "last", 1, and 1820.
+  (if (numberp msg1)
+	 (if (numberp msg2)
+	     (> msg1 msg2)
+	   t)
+    (if (numberp msg2)
+	nil
+      (string-lessp msg2 msg1))))
+
+
+
+;;; Basic sequence handling
+
+(defun mh-delete-seq-locally (seq)
+  ;; Remove mh-e's record of SEQUENCE.
+  (let ((entry (mh-find-seq seq)))
+    (setq mh-seq-list (delq entry mh-seq-list))))
+
+(defun mh-read-folder-sequences (folder save-refiles)
+  ;; Read and return the predefined sequences for a FOLDER.
+  ;; If SAVE-REFILES is non-nil, then keep the sequences
+  ;; that note messages to be refiled.
+  (let ((seqs ()))
+    (cond (save-refiles
+	    (mh-mapc (function (lambda (seq) ; Save the refiling sequences
+				 (if (mh-folder-name-p (mh-seq-name seq))
+				     (setq seqs (cons seq seqs)))))
+		     mh-seq-list)))
+    (save-excursion
+      (if (eq 0 (mh-exec-cmd-quiet nil "mark" folder "-list"))
+	  (progn
+	    ;; look for name in line of form "cur: 4" or "myseq (private): 23"
+	    (while (re-search-forward "^[^: ]+" nil t)
+	      (setq seqs (cons (mh-make-seq (intern (buffer-substring
+						     (match-beginning 0)
+						     (match-end 0)))
+					    (mh-read-msg-list))
+			       seqs)))
+	    (delete-region (point-min) (point))))) ; avoid race with mh-process-daemon
+    seqs))
+
+(defun mh-read-msg-list ()
+  ;; Return a list of message numbers from the current point to the end of
+  ;; the line.  Expands ranges into set of individual numbers.
+  (let ((msgs ())
+	(end-of-line (save-excursion (end-of-line) (point)))
+	num)
+    (while (re-search-forward "[0-9]+" end-of-line t)
+      (setq num (string-to-int (buffer-substring (match-beginning 0)
+						 (match-end 0))))
+      (cond ((looking-at "-")		; Message range
+	     (forward-char 1)
+	     (re-search-forward "[0-9]+" end-of-line t)
+	     (let ((num2 (string-to-int (buffer-substring (match-beginning 0)
+							  (match-end 0)))))
+	       (if (< num2 num)
+		   (error "Bad message range: %d-%d" num num2))
+	       (while (<= num num2)
+		 (setq msgs (cons num msgs))
+		 (setq num (1+ num)))))
+	    ((not (zerop num))		;"pick" outputs "0" to mean no match
+	     (setq msgs (cons num msgs)))))
+    msgs))
+
+(defun mh-notate-user-sequences ()
+  ;; Mark the scan listing of all messages in user-defined sequences.
+  (let ((seqs mh-seq-list)
+	name)
+    (while seqs
+      (setq name (mh-seq-name (car seqs)))
+      (if (not (mh-internal-seq name))
+	  (mh-notate-seq name mh-note-seq (1+ mh-cmd-note)))
+      (setq seqs (cdr seqs)))))
+
+
+(defun mh-internal-seq (name)
+  ;; Return non-NIL if NAME is the name of an internal mh-e sequence.
+  (or (memq name '(answered cur deleted forwarded printed))
+      (eq name mh-unseen-seq)
+      (eq name mh-previous-seq)
+      (mh-folder-name-p name)))
+
+
+(defun mh-delete-msg-from-seq (message sequence &optional internal-flag)
+  "Delete MESSAGE from SEQUENCE.  MESSAGE defaults to displayed message.
+From Lisp, optional third arg INTERNAL-FLAG non-nil means do not
+inform MH of the change."
+  (interactive (list (mh-get-msg-num t)
+		     (mh-read-seq-default "Delete from" t)
+		     nil))
+  (let ((entry (mh-find-seq sequence)))
+    (cond (entry