]> code.delx.au - gnu-emacs/blobdiff - lisp/mail/mh-comp.el
(report-emacs-bug-text-prompt): New variable.
[gnu-emacs] / lisp / mail / mh-comp.el
index 0ed7774dc09cb9e22653c7e0738a02e2dbc75390..bc6556cc31b8d1b5a820307cfa9a6800e745d34a 100644 (file)
@@ -1,33 +1,60 @@
-;;; mh-comp --- mh-e functions for composing messages
-;; Time-stamp: <94/03/08 10:05:20 gildea>
+;;; mh-comp.el --- mh-e functions for composing messages
+;; Time-stamp: <2001-12-20 18:55:07 pavel>
 
-;; Copyright 1993 Free Software Foundation, Inc.
+;; Copyright (C) 1993,1995,1997,2000  Free Software Foundation, Inc.
 
-;; This file is part of mh-e.
+;; Maintainer: Bill Wohler <wohler@newt.com>
+;; Keywords: mail
+;; Bug-reports: include `M-x mh-version' output in any correspondence
 
-;; mh-e is free software; you can redistribute it and/or modify
+;; This file is 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.
 
-;; mh-e is distributed in the hope that it will be useful,
+;; 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 mh-e; see the file COPYING.  If not, write to
-;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
 
 ;;; Commentary:
 
 ;; Internal support for mh-e package.
 
+;;; Change Log:
+
+;; $Id: mh-comp.el,v 1.22 2001/07/15 19:53:53 pj Exp $
+
 ;;; 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-dist "R"
   "String whose first character is used to notate redistributed messages.")
 
-(defvar mh-send-prog "send"
-  "Name of the MH send program.
-Some sites need to change this because of a name conflict.")
-
 (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.
@@ -48,7 +71,7 @@ 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.")
+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.
@@ -57,9 +80,9 @@ 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.
+with `mh-ins-buf-prefix' prefixed to each line.
 
-See also the variable mh-yank-from-start-of-msg, which controls how
+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
@@ -68,67 +91,112 @@ much of the message passed to the hook.")
 
 ;;; Personal preferences:
 
-(defvar mh-delete-yanked-msg-window nil
+(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.")
+windows displaying the message."
+  :type 'boolean
+  :group 'mh-compose)
 
-(defvar mh-yank-from-start-of-msg t
+(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.")
-
-(defvar mh-reply-default-reply-to nil
+ignored."
+  :type '(choice (const :tag "Below point" nil)
+                (const :tag "Without header" body)
+                (other :tag "Entire message" t))
+  :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\", or \"cc\".")
-
-(defvar mh-signature-file-name "~/.signature"
+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].")
+Inserted into message by \\<mh-letter-mode-map>\\[mh-insert-signature]."
+  :type 'file
+  :group 'mh-compose)
 
-(defvar mh-forward-subject-format "%s: %s"
+(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.")
+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
+Default is \"components\".  If not an absolute file 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 an absolute file name, the file
 is searched for first in the user's MH directory, then in the
 system MH lib directory.")
 
+(defvar mh-repl-group-formfile "replgroupcomps"
+  "Name of file to be used as a skeleton for replying to the sender
+and all recipients of a messages.  Only used if mh-nmh-p is non-nil.
+Default is \"replgroupcomps\".  If not an absolute file name, the file
+is searched for first in the user's MH directory, then in the system
+MH lib directory.")
+
 ;;; Hooks:
 
-(defvar mh-letter-mode-hook nil
-  "Invoked in `mh-letter-mode' on a new letter.")
+(defcustom mh-letter-mode-hook nil
+  "Invoked in `mh-letter-mode' on a new letter."
+  :type 'hook
+  :group 'mh-compose)
 
-(defvar mh-compose-letter-function nil
-  "Invoked in `mh-compose-and-send-mail' on a draft letter.
-It is passed three arguments: TO recipients, SUBJECT, and CC recipients.")
+(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 '(choice (const nil) function)
+  :group 'mh-compose)
 
-(defvar mh-before-send-letter-hook nil
-  "Invoked at the beginning of the \\<mh-letter-mode-map>\\[mh-send-letter] command.")
+(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 mail system
-         "\\|^------- Unsent Draft$"   ;from MH itself
+  (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
+         "\\|^  --- 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:\\|^Delivery-Date:\\|^Return-Path:"
+  "^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:"))
+(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)
@@ -148,14 +216,33 @@ Used by the \\<mh-folder-mode-map>`\\[mh-edit-again]' and `\\[mh-extract-rejecte
 (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."
+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 (&optional to subject other-headers &rest ignored)
+  "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 (or to "") "" (or subject ""))))
+
+
 (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)))
@@ -170,14 +257,14 @@ See also documentation for `\\[mh-send]' function."
                 (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))
-    (set-buffer-modified-p nil)
+    (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 displayed message.  The variable mh-new-draft-cleaned-headers
+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)))
@@ -192,9 +279,11 @@ See also documentation for `\\[mh-send]' function."
          (t
           (message "Does not appear to be a rejected letter.")))
     (goto-char (point-min))
-    (set-buffer-modified-p nil)
-    (mh-compose-and-send-mail draft "" from-folder msg (mh-get-field "To:")
-                             (mh-get-field "From:") (mh-get-field "Cc:")
+    (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)))
 
 
@@ -206,7 +295,7 @@ See also documentation for `\\[mh-send]' function."
                     (mh-read-address "Cc: ")
                     (if current-prefix-arg
                         (mh-read-seq-default "Forward" t)
-                        (mh-get-msg-num t))))
+                      (mh-get-msg-num t))))
   (or msg-or-seq
       (setq msg-or-seq (mh-get-msg-num t)))
   (let* ((folder mh-current-folder)
@@ -215,32 +304,42 @@ See also documentation for `\\[mh-send]' function."
         (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)
+                      (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)
-                        (set-buffer-modified-p nil)))
+                        (save-buffer)))
                      (t
-                      (mh-read-draft "" draft-name nil)))))
-    (goto-char (point-min))
-    (re-search-forward "^------- Forwarded Message")
-    (forward-line -1)
-    (narrow-to-region (point) (point-max))
-    (let ((orig-from (save-excursion (mh-get-field "From:")))
-         (orig-subject (save-excursion (mh-get-field "Subject:"))))
-      (widen)
+                      (mh-read-draft "" draft-name nil))))
+        (fwd-msg-file (mh-msg-filename (if (numberp msg-or-seq)
+                                           msg-or-seq
+                                         (car (mh-seq-to-msgs msg-or-seq)))
+                                       folder)))
+    (let (orig-from
+         orig-subject)
+      (save-excursion
+       (set-buffer (get-buffer-create mh-temp-buffer))
+       (erase-buffer)
+       (insert-file-contents fwd-msg-file)
+       (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)))
-      (save-excursion (mh-insert-fields "Subject:" forw-subject))
-      (delete-other-windows)
-      (if (numberp msg-or-seq)
-         (mh-add-msgs-to-seq msg-or-seq 'forwarded t)
+       (mh-insert-fields "Subject:" forw-subject)
+       (goto-char (point-min))
+       (if (re-search-forward "^------- Forwarded Message" nil t)
+           (forward-line -1)
+         (re-search-forward "^--------")
+         (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)))))
+       (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.
@@ -260,7 +359,9 @@ See also documentation for `\\[mh-send]' function."
 (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."
+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))
@@ -269,7 +370,7 @@ to the MH mail system."
 (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."
+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)))
@@ -293,14 +394,14 @@ setting of the variable mh-redist-full-contents.  See its documentation."
       (if mh-redist-full-contents
          (call-process "/bin/sh" nil 0 nil "-c"
                        (format "mhdist=1 mhaltmsg=%s %s -push %s"
-                               (buffer-file-name)
+                               buffer-file-name
                                (expand-file-name mh-send-prog mh-progs)
-                               (buffer-file-name)))
+                               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))))
+                             buffer-file-name)))
       (mh-annotate-msg msg folder mh-note-dist
                       "-component" "Resent:"
                       "-text" (format "\"%s %s\"" to cc))
@@ -308,64 +409,75 @@ setting of the variable mh-redist-full-contents.  See its documentation."
       (message "Redistributing...done"))))
 
 
-(defun mh-reply (msg &optional includep)
-  "Reply to a MESSAGE (default: displayed message).
+(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.
+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.
-See also documentation for `\\[mh-send]' function."
+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
+    (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)))
+          (folder mh-current-folder)
+          (show-buffer mh-show-buffer)
+          (config (current-window-configuration))
+          (group-reply (or (equal reply-to "cc") (equal reply-to "all")))
+          (form-file (cond ((and mh-nmh-p group-reply
+                                 (stringp mh-repl-group-formfile))
+                            mh-repl-group-formfile)
+                           ((stringp mh-repl-formfile) mh-repl-formfile)
+                           (t nil))))
       (message "Composing a reply...")
       (mh-exec-cmd "repl" "-build" "-noquery" "-nodraftfolder"
-            mh-current-folder msg
+            (if form-file
+                (list "-form" form-file))
+            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")))
+                  (group-reply (if mh-nmh-p
+                                   '("-group" "-nocc" "me")
+                                 '("-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)
-       (set-buffer-modified-p nil)
+       (save-buffer)
        
-       (let ((to (mh-get-field "To:"))
-             (subject (mh-get-field "Subject:"))
-             (cc (mh-get-field "Cc:")))
+       (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 msg folder)))
-         (mh-add-msgs-to-seq msg 'answered t)
+               (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 msg to subject cc
+         (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.
-Do not call this function from outside mh-e; use \\[mh-smail] instead."
+draft and passed three arguments: TO, SUBJECT, and CC."
   (interactive (list
                (mh-read-address "To: ")
                (mh-read-address "Cc: ")
@@ -389,9 +501,9 @@ See also documentation for `\\[mh-send]' function."
 
 
 (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."
+  ;; 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...")
@@ -407,6 +519,12 @@ CONFIG is the window configuration before sending mail."
                       (setq components
                             (expand-file-name mh-comp-formfile mh-lib)))
                      components)
+                    ((file-exists-p
+                      (setq components
+                            (expand-file-name mh-comp-formfile
+                                              ;; What is this mh-etc ??  -sm
+                                              (and (boundp 'mh-etc) mh-etc))))
+                     components)
                     (t
                      (error (format "Can't find components file \"%s\""
                                     components)))))
@@ -422,7 +540,7 @@ CONFIG is the window configuration before sending mail."
 (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
+  ;; 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.
@@ -455,8 +573,11 @@ CONFIG is the window configuration before sending mail."
                  (delete-file draft-name))))))
   (cond ((and initial-contents
              (or (zerop (buffer-size))
-                 (not (y-or-n-p
-                       (format "A draft exists.  Use for %s? " use)))))
+                 (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))))
@@ -507,23 +628,50 @@ CONFIG is the window configuration before sending mail."
   ;; 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.
-  (let ((case-fold-search t))
-    (goto-char (point-min))
-    (mh-goto-header-end 0)
-    (if (re-search-backward (format "^%s" field) nil t)
-       (progn
-         (forward-line 1)
-         (while (looking-at "^[ \t]")
-           (forward-line 1))
-         (backward-char 1)             ;to end of previous line
-         t)
-       nil)))
-
+  (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)
+  (if (re-search-forward "^-*$" nil nil)
       (forward-line arg)))
 
 
@@ -536,27 +684,25 @@ CONFIG is the window configuration before sending mail."
 
 ;;; Mode for composing and sending a draft message.
 
-(defvar mh-sent-from-folder nil
-  "Folder of msg associated with this letter.")
+(defvar mh-sent-from-folder nil)       ;Folder of msg assoc with this letter.
 
-(defvar mh-sent-from-msg nil
-  "Number of msg associated with this letter.")
+(defvar mh-sent-from-msg nil)          ;Number of msg assoc with this letter.
 
-(defvar mh-send-args nil
-  "Extra arguments to pass to \"send\" command.")
+(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-char nil)          ;Character to use to annotate mh-sent-from-msg.
 
-(defvar mh-annotate-field nil
-  "Field name for message annotation.")
+(defvar mh-annotate-field nil)         ;Field name for message annotation.
 
 (put 'mh-letter-mode 'mode-class 'special)
 
 ;;;###autoload
-(defun mh-letter-mode ()
+(define-derived-mode mh-letter-mode text-mode "MH-Letter"
   "Mode for composing letters in mh-e.\\<mh-letter-mode-map>
-When you have finished composing, type \\[mh-send-letter] to send the letter.
+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}
 
@@ -572,15 +718,15 @@ Variables controlling this mode (defaults in parentheses):
     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)
+This command runs the normal hooks `text-mode-hook' and `mh-letter-mode-hook'."
   (or mh-user-path (mh-find-path))
-  (kill-all-local-variables)
   (make-local-variable 'paragraph-start)
   (setq paragraph-start (concat "^[ \t]*[-_][-_][-_]+$\\|" paragraph-start))
   (make-local-variable 'paragraph-separate)
@@ -594,29 +740,19 @@ invoked with no args, if those values are non-nil."
   (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))
+  (when auto-fill-function
+    (make-local-variable 'auto-fill-function)
+    (setq auto-fill-function 'mh-auto-fill-for-letter)))
 
 
 (defun mh-auto-fill-for-letter ()
   ;; Auto-fill in letters treats the header specially by inserting a tab
   ;; before continuation line.
-  (do-auto-fill)
   (if (mh-in-header-p)
-      (save-excursion
-       (beginning-of-line nil)
-       (insert-char ?\t 1))))
+      (let ((fill-prefix "\t"))
+       (do-auto-fill))
+    (do-auto-fill)))
 
 
 (defun mh-in-header-p ()
@@ -631,11 +767,14 @@ invoked with no args, if those values are non-nil."
 (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  mh-to-field-choices.
+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 (assoc (logior last-input-char ?`) mh-to-field-choices)))
+  (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)
@@ -662,10 +801,10 @@ Prompt for the field name with a completion list of the current folders."
   (or folder
       (setq folder (mh-prompt-for-folder
                    "Fcc"
-                   (or (and mh-msg-folder-hook
+                   (or (and mh-default-folder-for-message-function
                             (save-excursion
                               (goto-char (point-min))
-                              (funcall mh-msg-folder-hook)))
+                              (funcall mh-default-folder-for-message-function)))
                        "")
                    t)))
   (let ((last-input-char ?\C-f))
@@ -678,16 +817,16 @@ Prompt for the field name with a completion list of the current folders."
 
 
 (defun mh-insert-signature ()
-  "Insert the file named by mh-signature-file-name at the current point."
+  "Insert the file named by `mh-signature-file-name' at point."
   (interactive)
   (insert-file-contents mh-signature-file-name)
-  (set-buffer-modified-p (buffer-modified-p))) ; force mode line update
+  (force-mode-line-update))
 
 
 (defun mh-check-whom ()
-  "Verify recipients of the current letter."
+  "Verify recipients of the current letter, showing expansion of any aliases."
   (interactive)
-  (let ((file-name (buffer-file-name)))
+  (let ((file-name buffer-file-name))
     (save-buffer)
     (message "Checking recipients...")
     (mh-in-show-buffer ("*Recipients*")
@@ -726,9 +865,9 @@ Prompt for the field name with a completion list of the current folders."
   (setq mh-previous-window-config config)
   (setq mode-line-buffer-identification (list "{%b}"))
   (if (and (boundp 'mh-compose-letter-function)
-          (symbol-value 'mh-compose-letter-function))
+          mh-compose-letter-function)
       ;; run-hooks will not pass arguments.
-      (let ((value (symbol-value 'mh-compose-letter-function)))
+      (let ((value mh-compose-letter-function))
        (if (and (listp value) (not (eq (car value) 'lambda)))
            (while value
              (funcall (car value) to subject cc)
@@ -739,15 +878,24 @@ Prompt for the field name with a completion list of the current folders."
 (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."
+Run `mh-before-send-letter-hook' before actually doing anything."
   (interactive "P")
   (run-hooks 'mh-before-send-letter-hook)
-  (set-buffer-modified-p t)            ; Make sure buffer is written
   (save-buffer)
   (message "Sending...")
   (let ((draft-buffer (current-buffer))
-       (file-name (buffer-file-name))
-       (config mh-previous-window-config))
+       (file-name buffer-file-name)
+       (config mh-previous-window-config)
+       (coding-system-for-write
+        (if (and (local-variable-p 'buffer-file-coding-system)
+                 ;; We're not sure why, but buffer-file-coding-system
+                 ;; tends to get set to undecided-unix.
+                 (not (memq buffer-file-coding-system
+                            '(undecided undecided-unix undecided-dos))))
+            buffer-file-coding-system
+          (or sendmail-coding-system
+              default-buffer-file-coding-system
+              'iso-latin-1))))
     (cond (arg
           (pop-to-buffer "MH mail delivery")
           (erase-buffer)
@@ -765,8 +913,8 @@ Run mh-before-send-letter-hook before doing anything."
                         mh-annotate-char
                         "-component" mh-annotate-field
                         "-text" (format "\"%s %s\""
-                                        (mh-get-field "To:")
-                                        (mh-get-field "Cc:"))))
+                                        (mh-get-header-field "To:")
+                                        (mh-get-header-field "Cc:"))))
 
     (cond ((or (not arg)
               (y-or-n-p "Kill draft buffer? "))
@@ -778,27 +926,27 @@ Run mh-before-send-letter-hook before doing anything."
       (message "Sending...backgrounded"))))
 
 
-(defun mh-insert-letter (msg folder verbatum)
-  "Insert a MESSAGE from any FOLDER into the current letter.
-Removes the message's headers using mh-invisible-headers.
-Prefixes each non-blank line with mh-ins-buf-prefix (default \"> \").
-If prefix argument VERBATUM provided, do not indent and do not delete
-headers.  Leaves the mark before the letter and point after it."
+(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 (read-input (format "Message number%s: "
+   (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)
-                                "")))
-        (mh-prompt-for-folder "Message from" mh-sent-from-folder nil)
+                              "")))
         current-prefix-arg))
   (save-restriction
     (narrow-to-region (point) (point))
     (let ((start (point-min)))
-      (if (equal msg "") (setq msg (int-to-string mh-sent-from-msg)))
+      (if (equal message "") (setq message (int-to-string mh-sent-from-msg)))
       (mh-exec-lib-cmd-output "mhl" "-nobell" "-noclear"
-                             (expand-file-name msg
+                             (expand-file-name message
                                                (mh-expand-file-name folder)))
-      (cond ((not verbatum)
+      (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))))))
@@ -822,8 +970,8 @@ yanked message will be deleted."
            (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
+                                    mark-active ;Emacs 19
+                                  (mark)) ;Emacs 18
                                 (buffer-substring (region-beginning)
                                                   (region-end)))
                                ((eq 'body mh-yank-from-start-of-msg)
@@ -838,30 +986,29 @@ yanked message will be deleted."
                                (t
                                 (buffer-substring (point) (point-max))))))
          (set-buffer to-buffer)
-         (narrow-to-region to-point to-point)
-         (push-mark)
-         (insert mh-ins-str)
-         (mh-insert-prefix-string mh-ins-buf-prefix)
-         (insert "\n")
-         (widen)))
-      (error "There is no current message")))
+         (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
+  ;; Run mail-citation-hook to insert a prefix string before each line
   ;; in the buffer.  Generality for supercite users.
-  (save-excursion
-    (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))
-          (while (< (point) (mark))
-            (insert mh-ins-string)
-            (forward-line 1))))))
+  (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))
+        (while (< (point) (mark))
+          (insert mh-ins-string)
+          (forward-line 1)))))
 
 
 (defun mh-fully-kill-draft ()
@@ -870,8 +1017,8 @@ 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)))
+       (if (file-exists-p buffer-file-name)
+           (delete-file buffer-file-name))
        (set-buffer-modified-p nil)
        (kill-buffer (buffer-name))
        (message "")
@@ -879,23 +1026,30 @@ Use \\[kill-buffer] if you don't want to delete the draft message file."
            (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-i" 'mh-insert-letter)
 (define-key mh-letter-mode-map "\C-c\C-y" 'mh-yank-cur-msg)
-(define-key mh-letter-mode-map "\C-c\C-s" 'mh-insert-signature)
 (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)
@@ -904,25 +1058,41 @@ Use \\[kill-buffer] if you don't want to delete the draft message file."
 (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 message part from a file." t)
+  "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 an anonymous ftp external body part." t)
+  "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 reference to a compressed tar file." t)
+  "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." t)
+  "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"
-  "Filter the current draft through the mhn program for MIME formatting.
-Using directives already inserted in the draft, fills in
+  "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 MIME directives to insert
+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),
@@ -931,4 +1101,7 @@ various types of components in a message, see
 \\[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." t)
+  "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-comp.el ends here