changeset 101360:b10df8502c4a

Renamed all pmail* files to rmail*.
author Bastien Guerry <bzg@altern.org>
date Thu, 22 Jan 2009 17:09:23 +0000
parents ecec9b595874
children 5421268f1d18
files lisp/mail/rmail.el lisp/mail/rmailedit.el lisp/mail/rmailkwd.el lisp/mail/rmailmm.el lisp/mail/rmailmsc.el lisp/mail/rmailout.el lisp/mail/rmailsort.el lisp/mail/rmailsum.el
diffstat 8 files changed, 7367 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lisp/mail/rmail.el	Thu Jan 22 17:09:23 2009 +0000
@@ -0,0 +1,3893 @@
+;;; rmail.el --- main code of "RMAIL" mail reader for Emacs
+
+;; Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1996, 1997, 1998,
+;;   2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
+;;   Free Software Foundation, Inc.
+
+;; Maintainer: FSF
+;; Keywords: mail
+
+;; 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 3 of the License, 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.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;;; Code:
+
+;; Souped up by shane@mit-ajax based on ideas of rlk@athena.mit.edu
+;;   New features include attribute and keyword support, message
+;;   selection by dispatch table, summary by attributes and keywords,
+;;   expunging by dispatch table, sticky options for file commands.
+
+;; Extended by Bob Weiner of Motorola
+;;   New features include: rmail and rmail-summary buffers remain
+;;   synchronized and key bindings basically operate the same way in both
+;;   buffers, summary by topic or by regular expression, rmail-reply-prefix
+;;   variable, and a bury rmail buffer (wipe) command.
+;;
+
+(require 'mail-utils)
+(eval-when-compile (require 'mule-util)) ; for detect-coding-with-priority
+
+(defconst rmail-attribute-header "X-RMAIL-ATTRIBUTES"
+  "The header that stores the Rmail attribute data.")
+
+(defconst rmail-keyword-header "X-RMAIL-KEYWORDS"
+  "The header that stores the Rmail keyword data.")
+
+;;; Attribute indexes
+
+(defconst rmail-answered-attr-index 0
+  "The index for the `answered' attribute.")
+
+(defconst rmail-deleted-attr-index 1
+  "The index for the `deleted' attribute.")
+
+(defconst rmail-edited-attr-index 2
+  "The index for the `edited' attribute.")
+
+(defconst rmail-filed-attr-index 3
+  "The index for the `filed' attribute.")
+
+(defconst rmail-retried-attr-index 4
+  "The index for the `retried' attribute.")
+
+(defconst rmail-forwarded-attr-index 5
+  "The index for the `forwarded' attribute.")
+
+(defconst rmail-unseen-attr-index 6
+  "The index for the `unseen' attribute.")
+
+(defconst rmail-resent-attr-index 6
+  "The index for the `resent' attribute.")
+
+(defconst rmail-attr-array
+  '[(?A "answered")
+    (?D "deleted")
+    (?E "edited")
+    (?F "filed")
+    (?R "retried")
+    (?S "forwarded")
+    (?U "unseen")
+    (?r "resent")]
+  "An array that provides a mapping between an attribute index,
+its character representation and its display representation.")
+
+(defvar deleted-head)
+(defvar font-lock-fontified)
+(defvar mail-abbrev-syntax-table)
+(defvar mail-abbrevs)
+(defvar messages-head)
+(defvar rmail-use-spam-filter)
+(defvar rsf-beep)
+(defvar rsf-sleep-after-message)
+(defvar total-messages)
+(defvar tool-bar-map)
+
+(defvar rmail-header-style 'normal
+  "The current header display style choice, one of
+'normal (selected headers) or 'full (all headers).")
+
+; These variables now declared in paths.el.
+;(defvar rmail-spool-directory "/usr/spool/mail/"
+;  "This is the name of the directory used by the system mailer for\n\
+;delivering new mail.  Its name should end with a slash.")
+;(defvar rmail-file-name
+;  (expand-file-name "~/RMAIL")
+;  "")
+
+;; Temporary support for mbox.
+(defcustom rmail-file-name "~/RMAIL"
+  "*Name of user's primary mail file."
+  :type 'string
+  :group 'rmail
+  :version "21.1")
+
+(defgroup rmail nil
+  "Mail reader for Emacs."
+  :group 'mail)
+
+(defgroup rmail-retrieve nil
+  "Rmail retrieval options."
+  :prefix "rmail-"
+  :group 'rmail)
+
+(defgroup rmail-files nil
+  "Rmail files."
+  :prefix "rmail-"
+  :group 'rmail)
+
+(defgroup rmail-headers nil
+  "Rmail header options."
+  :prefix "rmail-"
+  :group 'rmail)
+
+(defgroup rmail-reply nil
+  "Rmail reply options."
+  :prefix "rmail-"
+  :group 'rmail)
+
+(defgroup rmail-summary nil
+  "Rmail summary options."
+  :prefix "rmail-"
+  :prefix "rmail-summary-"
+  :group 'rmail)
+
+(defgroup rmail-output nil
+  "Output message to a file."
+  :prefix "rmail-output-"
+  :prefix "rmail-"
+  :group 'rmail)
+
+(defgroup rmail-edit nil
+  "Rmail editing."
+  :prefix "rmail-edit-"
+  :group 'rmail)
+
+(defgroup rmail-obsolete nil
+  "Rmail obsolete customization variables."
+  :group 'rmail)
+
+(defcustom rmail-movemail-program nil
+  "If non-nil, the file name of the `movemail' program."
+  :group 'rmail-retrieve
+  :type '(choice (const nil) string))
+
+(defcustom rmail-pop-password nil
+  "*Password to use when reading mail from POP server.
+Please use `rmail-remote-password' instead."
+  :type '(choice (string :tag "Password")
+		 (const :tag "Not Required" nil))
+  :group 'rmail-obsolete)
+
+(defcustom rmail-pop-password-required nil
+  "*Non-nil if a password is required when reading mail from a POP server.
+Please use rmail-remote-password-required instead."
+  :type 'boolean
+  :group 'rmail-obsolete)
+
+(defcustom rmail-remote-password nil
+  "*Password to use when reading mail from a remote server.
+This setting is ignored for mailboxes whose URL already contains a password."
+  :type '(choice (string :tag "Password")
+		 (const :tag "Not Required" nil))
+  :set-after '(rmail-pop-password)
+  :set #'(lambda (symbol value)
+	   (set-default symbol
+			(if (and (not value)
+                                 (boundp 'rmail-pop-password)
+				 rmail-pop-password)
+			    rmail-pop-password
+			  value))
+	   (setq rmail-pop-password nil))
+  :group 'rmail-retrieve
+  :version "22.1")
+
+(defcustom rmail-remote-password-required nil
+  "*Non-nil if a password is required when reading mail from a remote server."
+  :type 'boolean
+  :set-after '(rmail-pop-password-required)
+  :set #'(lambda (symbol value)
+	   (set-default symbol
+			(if (and (not value)
+                                 (boundp 'rmail-pop-password-required)
+				 rmail-pop-password-required)
+			    rmail-pop-password-required
+			  value))
+	   (setq rmail-pop-password-required nil))
+  :group 'rmail-retrieve
+  :version "22.1")
+
+(defcustom rmail-movemail-flags nil
+  "*List of flags to pass to movemail.
+Most commonly used to specify `-g' to enable GSS-API authentication
+or `-k' to enable Kerberos authentication."
+  :type '(repeat string)
+  :group 'rmail-retrieve
+  :version "20.3")
+
+(defvar rmail-remote-password-error "invalid usercode or password\\|
+unknown user name or bad password\\|Authentication failed\\|MU_ERR_AUTH_FAILURE"
+  "Regular expression matching incorrect-password POP or IMAP server error
+messages.
+If you get an incorrect-password error that this expression does not match,
+please report it with \\[report-emacs-bug].")
+
+(defvar rmail-encoded-remote-password nil)
+
+(defcustom rmail-preserve-inbox nil
+  "*Non-nil means leave incoming mail in the user's inbox--don't delete it."
+  :type 'boolean
+  :group 'rmail-retrieve)
+
+(defcustom rmail-movemail-search-path nil
+    "*List of directories to search for movemail (in addition to `exec-path')."
+    :group 'rmail-retrieve
+    :type '(repeat (directory)))
+
+(declare-function mail-position-on-field "sendmail" (field &optional soft))
+(declare-function mail-text-start "sendmail" ())
+(declare-function rmail-dont-reply-to "mail-utils" (destinations))
+(declare-function rmail-update-summary "rmailsum" (&rest ignore))
+
+(defun rmail-probe (prog)
+  "Determine what flavor of movemail PROG is.
+We do this by executing it with `--version' and analyzing its output."
+  (with-temp-buffer
+    (let ((tbuf (current-buffer)))
+      (buffer-disable-undo tbuf)
+      (call-process prog nil tbuf nil "--version")
+      (if (not (buffer-modified-p tbuf))
+	  ;; Should not happen...
+	  nil
+	(goto-char (point-min))
+	(cond
+	 ((looking-at ".*movemail: invalid option")
+	  'emacs)    ;; Possibly...
+	 ((looking-at "movemail (GNU Mailutils .*)")
+	  'mailutils)
+	 (t
+	  ;; FIXME:
+	  'emacs))))))
+
+(defun rmail-autodetect ()
+  "Determine the file name of the `movemail' program and return its flavor.
+If `rmail-movemail-program' is non-nil, use it.
+Otherwise, look for `movemail' in the directories in
+`rmail-movemail-search-path', those in `exec-path', and `exec-directory'."
+  (if rmail-movemail-program
+      (rmail-probe rmail-movemail-program)
+    (catch 'scan
+      (dolist (dir (append rmail-movemail-search-path exec-path
+			   (list exec-directory)))
+	(when (and dir (file-accessible-directory-p dir))
+	  ;; Previously, this didn't have to work on Windows, because
+	  ;; rmail-insert-inbox-text before r1.439 fell back to using
+	  ;; (expand-file-name "movemail" exec-directory) and just
+	  ;; assuming it would work.
+	  ;; http://lists.gnu.org/archive/html/bug-gnu-emacs/2008-02/msg00087.html
+	  (let ((progname (expand-file-name
+			   (concat "movemail"
+				   (if (memq system-type '(ms-dos windows-nt))
+				       ".exe")) dir)))
+	    (when (and (not (file-directory-p progname))
+		       (file-executable-p progname))
+	      (let ((x (rmail-probe progname)))
+		(when x
+		  (setq rmail-movemail-program progname)
+		  (throw 'scan x))))))))))
+
+(defvar rmail-movemail-variant-in-use nil
+  "The movemail variant currently in use. Known variants are:
+
+  `emacs'     Means any implementation, compatible with the native Emacs one.
+              This is the default;
+  `mailutils' Means GNU mailutils implementation, capable of handling full
+mail URLs as the source mailbox.")
+
+;;;###autoload
+(defun rmail-movemail-variant-p (&rest variants)
+  "Return t if the current movemail variant is any of VARIANTS.
+Currently known variants are 'emacs and 'mailutils."
+  (when (not rmail-movemail-variant-in-use)
+    ;; Autodetect
+    (setq rmail-movemail-variant-in-use (rmail-autodetect)))
+  (not (null (member rmail-movemail-variant-in-use variants))))
+
+;; Call for effect, to set rmail-movemail-program (if not set by the
+;; user), and rmail-movemail-variant-in-use. Used by various functions.
+;; I'm not sure if M-x rmail is the only entry point to this package.
+;; If so, this can be moved there.
+(rmail-movemail-variant-p)
+
+;;;###autoload
+(defcustom rmail-dont-reply-to-names nil "\
+*A regexp specifying addresses to prune from a reply message.
+A value of nil means exclude your own email address as an address
+plus whatever is specified by `rmail-default-dont-reply-to-names'."
+  :type '(choice regexp (const :tag "Your Name" nil))
+  :group 'rmail-reply)
+
+;;;###autoload
+(defvar rmail-default-dont-reply-to-names "\\`info-" "\
+A regular expression specifying part of the default value of the
+variable `rmail-dont-reply-to-names', for when the user does not set
+`rmail-dont-reply-to-names' explicitly.  (The other part of the default
+value is the user's email address and name.)
+It is useful to set this variable in the site customization file.")
+
+;;;###autoload
+(defcustom rmail-ignored-headers
+  (concat "^via:\\|^mail-from:\\|^origin:\\|^references:\\|^sender:"
+	  "\\|^status:\\|^received:\\|^x400-originator:\\|^x400-recipients:"
+	  "\\|^x400-received:\\|^x400-mts-identifier:\\|^x400-content-type:"
+	  "\\|^\\(resent-\\|\\)message-id:\\|^summary-line:\\|^resent-date:"
+	  "\\|^nntp-posting-host:\\|^path:\\|^x-char.*:\\|^x-face:\\|^face:"
+	  "\\|^x-mailer:\\|^delivered-to:\\|^lines:"
+	  "\\|^content-transfer-encoding:\\|^x-coding-system:"
+	  "\\|^return-path:\\|^errors-to:\\|^return-receipt-to:"
+	  "\\|^precedence:\\|^mime-version:"
+	  "\\|^list-owner:\\|^list-help:\\|^list-post:\\|^list-subscribe:"
+	  "\\|^list-id:\\|^list-unsubscribe:\\|^list-archive:"
+	  "\\|^content-length:\\|^nntp-posting-date:\\|^user-agent"
+	  "\\|^importance:\\|^envelope-to:\\|^delivery-date\\|^openpgp:"
+	  "\\|^mbox-line:\\|^cancel-lock:"
+	  "\\|^DomainKey-Signature:\\|^dkim-signature:"
+	  "\\|^resent-face:\\|^resent-x.*:\\|^resent-organization:\\|^resent-openpgp:"
+	  "\\|^x-.*:")
+  "*Regexp to match header fields that Rmail should normally hide.
+\(See also `rmail-nonignored-headers', which overrides this regexp.)
+This variable is used for reformatting the message header,
+which normally happens once for each message,
+when you view the message for the first time in Rmail.
+To make a change in this variable take effect
+for a message that you have already viewed,
+go to that message and type \\[rmail-toggle-header] twice."
+  :type 'regexp
+  :group 'rmail-headers)
+
+(defcustom rmail-nonignored-headers "^x-spam-status:"
+  "*Regexp to match X header fields that Rmail should show.
+This regexp overrides `rmail-ignored-headers'; if both this regexp
+and that one match a certain header field, Rmail shows the field.
+If this is nil, ignore all header fields in `rmail-ignored-headers'.
+
+This variable is used for reformatting the message header,
+which normally happens once for each message,
+when you view the message for the first time in Rmail.
+To make a change in this variable take effect
+for a message that you have already viewed,
+go to that message and type \\[rmail-toggle-header] twice."
+  :type '(choice (const nil) (regexp))
+  :group 'rmail-headers)
+
+;;;###autoload
+(defcustom rmail-displayed-headers nil
+  "*Regexp to match Header fields that Rmail should display.
+If nil, display all header fields except those matched by
+`rmail-ignored-headers'."
+  :type '(choice regexp (const :tag "All"))
+  :group 'rmail-headers)
+
+;;;###autoload
+(defcustom rmail-retry-ignored-headers "^x-authentication-warning:" "\
+*Headers that should be stripped when retrying a failed message."
+  :type '(choice regexp (const nil :tag "None"))
+  :group 'rmail-headers)
+
+;;;###autoload
+(defcustom rmail-highlighted-headers "^From:\\|^Subject:" "\
+*Regexp to match Header fields that Rmail should normally highlight.
+A value of nil means don't highlight."
+  :type 'regexp
+  :group 'rmail-headers)
+
+(defface rmail-highlight
+  '((t (:inherit highlight)))
+  "Face to use for highlighting the most important header fields."
+  :group 'rmail-headers
+  :version "22.1")
+
+(defface rmail-header-name
+  '((t (:inherit font-lock-function-name-face)))
+  "Face to use for highlighting the header names."
+  :group 'rmail-headers
+  :version "23.1")
+
+;;;###autoload
+(defcustom rmail-delete-after-output nil "\
+*Non-nil means automatically delete a message that is copied to a file."
+  :type 'boolean
+  :group 'rmail-files)
+
+;;;###autoload
+(defcustom rmail-primary-inbox-list nil "\
+*List of files which are inboxes for user's primary mail file `~/RMAIL'.
+nil means the default, which is (\"/usr/spool/mail/$USER\")
+\(the name varies depending on the operating system,
+and the value of the environment variable MAIL overrides it)."
+  ;; Don't use backquote here, because we don't want to need it
+  ;; at load time.
+  :type (list 'choice '(const :tag "Default" nil)
+	      (list 'repeat ':value (list (or (getenv "MAIL")
+					      (concat "/var/spool/mail/"
+						      (getenv "USER"))))
+		    'file))
+  :group 'rmail-retrieve
+  :group 'rmail-files)
+
+;;;###autoload
+(defcustom rmail-mail-new-frame nil
+  "*Non-nil means Rmail makes a new frame for composing outgoing mail.
+This is handy if you want to preserve the window configuration of
+the frame where you have the RMAIL buffer displayed."
+  :type 'boolean
+  :group 'rmail-reply)
+
+;;;###autoload
+(defcustom rmail-secondary-file-directory "~/"
+  "*Directory for additional secondary Rmail files."
+  :type 'directory
+  :group 'rmail-files)
+;;;###autoload
+(defcustom rmail-secondary-file-regexp "\\.xmail$"
+  "*Regexp for which files are secondary Rmail files."
+  :type 'regexp
+  :group 'rmail-files)
+
+;;;###autoload
+(defcustom rmail-confirm-expunge 'y-or-n-p
+  "*Whether and how to ask for confirmation before expunging deleted messages."
+  :type '(choice (const :tag "No confirmation" nil)
+		 (const :tag "Confirm with y-or-n-p" y-or-n-p)
+		 (const :tag "Confirm with yes-or-no-p" yes-or-no-p))
+  :version "21.1"
+  :group 'rmail-files)
+
+;;;###autoload
+(defvar rmail-mode-hook nil
+  "List of functions to call when Rmail is invoked.")
+
+;;;###autoload
+(defvar rmail-get-new-mail-hook nil
+  "List of functions to call when Rmail has retrieved new mail.")
+
+;;;###autoload
+(defcustom rmail-show-message-hook nil
+  "List of functions to call when Rmail displays a message."
+  :type 'hook
+  :options '(goto-address)
+  :group 'rmail)
+
+;;;###autoload
+(defvar rmail-quit-hook nil
+  "List of functions to call when quitting out of Rmail.")
+
+;;;###autoload
+(defvar rmail-delete-message-hook nil
+  "List of functions to call when Rmail deletes a message.
+When the hooks are called, the message has been marked deleted but is
+still the current message in the Rmail buffer.")
+
+;; These may be altered by site-init.el to match the format of mmdf files
+;;  delimiting used on a given host (delim1 and delim2 from the config
+;;  files).
+
+(defvar rmail-mmdf-delim1 "^\001\001\001\001\n"
+  "Regexp marking the start of an mmdf message.")
+(defvar rmail-mmdf-delim2 "^\001\001\001\001\n"
+  "Regexp marking the end of an mmdf message.")
+
+(defcustom rmail-message-filter nil
+  "If non-nil, a filter function for new messages in RMAIL.
+Called with region narrowed to the message, including headers,
+before obeying `rmail-ignored-headers'."
+  :group 'rmail-headers
+  :type '(choice (const nil) function))
+
+(defcustom rmail-automatic-folder-directives nil
+  "List of directives specifying where to put a message.
+Each element of the list is of the form:
+
+  (FOLDERNAME FIELD REGEXP [ FIELD REGEXP ] ... )
+
+Where FOLDERNAME is the name of a BABYL format folder to put the
+message.  If any of the field regexp's are nil, then it is ignored.
+
+If FOLDERNAME is \"/dev/null\", it is deleted.
+If FOLDERNAME is nil then it is deleted, and skipped.
+
+FIELD is the plain text name of a field in the message, such as
+\"subject\" or \"from\".  A FIELD of \"to\" will automatically include
+all text from the \"cc\" field as well.
+
+REGEXP is an expression to match in the preceeding specified FIELD.
+FIELD/REGEXP pairs continue in the list.
+
+examples:
+  (\"/dev/null\" \"from\" \"@spam.com\") ; delete all mail from spam.com
+  (\"RMS\" \"from\" \"rms@\") ; save all mail from RMS."
+  :group 'rmail
+  :version "21.1"
+  :type '(repeat (sexp :tag "Directive")))
+
+(defvar rmail-reply-prefix "Re: "
+  "String to prepend to Subject line when replying to a message.")
+
+;; Some mailers use "Re(2):" or "Re^2:" or "Re: Re:" or "Re[2]:".
+;; This pattern should catch all the common variants.
+;; rms: I deleted the change to delete tags in square brackets
+;; because they mess up RT tags.
+(defvar rmail-reply-regexp "\\`\\(Re\\(([0-9]+)\\|\\[[0-9]+\\]\\|\\^[0-9]+\\)?: *\\)*"
+  "Regexp to delete from Subject line before inserting `rmail-reply-prefix'.")
+
+(defcustom rmail-display-summary nil
+  "*If non-nil, Rmail always displays the summary buffer."
+  :group 'rmail-summary
+  :type 'boolean)
+
+(defvar rmail-inbox-list nil)
+(put 'rmail-inbox-list 'permanent-local t)
+
+(defvar rmail-buffer nil
+  "The RMAIL buffer related to the current buffer.
+In an RMAIL buffer, this holds the RMAIL buffer itself.
+In a summary buffer, this holds the RMAIL buffer it is a summary for.")
+(put 'rmail-buffer 'permanent-local t)
+
+;; Message counters and markers.  Deleted flags.
+
+(defvar rmail-current-message nil)
+(put 'rmail-current-message 'permanent-local t)
+
+(defvar rmail-total-messages nil)
+(put 'rmail-total-messages 'permanent-local t)
+
+(defvar rmail-message-vector nil)
+(put 'rmail-message-vector 'permanent-local t)
+
+(defvar rmail-deleted-vector nil)
+(put 'rmail-deleted-vector 'permanent-local t)
+
+(defvar rmail-msgref-vector nil
+  "In an Rmail buffer, a vector whose Nth element is a list (N).
+When expunging renumbers messages, these lists are modified
+by substituting the new message number into the existing list.")
+(put 'rmail-msgref-vector 'permanent-local t)
+
+(defvar rmail-overlay-list nil)
+(put 'rmail-overlay-list 'permanent-local t)
+
+;; These are used by autoloaded rmail-summary.
+
+(defvar rmail-summary-buffer nil)
+(put 'rmail-summary-buffer 'permanent-local t)
+(defvar rmail-summary-vector nil)
+(put 'rmail-summary-vector 'permanent-local t)
+
+;; Rmail buffer swapping variables.
+
+(defvar rmail-buffer-swapped nil
+  "If non-nil, `rmail-buffer' is swapped with `rmail-view-buffer'.")
+(make-variable-buffer-local 'rmail-buffer-swapped)
+
+(defvar rmail-view-buffer nil
+  "Buffer which holds RMAIL message for MIME displaying.")
+(put 'rmail-view-buffer 'permanent-local t)
+
+;; `Sticky' default variables.
+
+;; Last individual label specified to a or k.
+(defvar rmail-last-label nil)
+
+;; Last set of values specified to C-M-n, C-M-p, C-M-s or C-M-l.
+(defvar rmail-last-multi-labels nil)
+
+(defvar rmail-last-regexp nil)
+(put 'rmail-last-regexp 'permanent-local t)
+
+(defcustom rmail-default-file "~/xmail"
+  "*Default file name for \\[rmail-output]."
+  :type 'file
+  :group 'rmail-files)
+(defcustom rmail-default-body-file "~/mailout"
+  "*Default file name for \\[rmail-output-body-to-file]."
+  :type 'file
+  :group 'rmail-files
+  :version "20.3")
+
+;; Mule and MIME related variables.
+
+;;;###autoload
+(defvar rmail-file-coding-system nil
+  "Coding system used in RMAIL file.
+
+This is set to nil by default.")
+
+;;;###autoload
+(defcustom rmail-enable-mime nil
+  "*If non-nil, RMAIL uses MIME feature.
+If the value is t, RMAIL automatically shows MIME decoded message.
+If the value is neither t nor nil, RMAIL does not show MIME decoded message
+until a user explicitly requires it.
+
+Even if the value is non-nil, you can't use MIME feature
+if the feature specified by `rmail-mime-feature' is not available
+in your session."
+  :type '(choice (const :tag "on" t)
+		 (const :tag "off" nil)
+		 (other :tag "when asked" ask))
+  :group 'rmail)
+
+(defvar rmail-enable-mime-composing nil
+  "*If non-nil, RMAIL uses `rmail-insert-mime-forwarded-message-function' to forward.")
+
+;;;###autoload
+(defvar rmail-show-mime-function nil
+  "Function to show MIME decoded message of RMAIL file.
+This function is called when `rmail-enable-mime' is non-nil.
+It is called with no argument.")
+
+;;;###autoload
+(defvar rmail-insert-mime-forwarded-message-function nil
+  "Function to insert a message in MIME format so it can be forwarded.
+This function is called if `rmail-enable-mime' or
+`rmail-enable-mime-composing' is non-nil.
+It is called with one argument FORWARD-BUFFER, which is a
+buffer containing the message to forward.  The current buffer
+is the outgoing mail buffer.")
+
+;;;###autoload
+(defvar rmail-insert-mime-resent-message-function nil
+  "Function to insert a message in MIME format so it can be resent.
+This function is called if `rmail-enable-mime' is non-nil.
+It is called with one argument FORWARD-BUFFER, which is a
+buffer containing the message to forward.  The current buffer
+is the outgoing mail buffer.")
+
+;;;###autoload
+(defvar rmail-search-mime-message-function nil
+  "Function to check if a regexp matches a MIME message.
+This function is called if `rmail-enable-mime' is non-nil.
+It is called with two arguments MSG and REGEXP, where
+MSG is the message number, REGEXP is the regular expression.")
+
+;;;###autoload
+(defvar rmail-search-mime-header-function nil
+  "Function to check if a regexp matches a header of MIME message.
+This function is called if `rmail-enable-mime' is non-nil.
+It is called with three arguments MSG, REGEXP, and LIMIT, where
+MSG is the message number,
+REGEXP is the regular expression,
+LIMIT is the position specifying the end of header.")
+
+;;;###autoload
+(defvar rmail-mime-feature 'rmail-mime
+  "Feature to require to load MIME support in Rmail.
+When starting Rmail, if `rmail-enable-mime' is non-nil,
+this feature is required with `require'.
+
+The default value is `rmail-mime'.  This feature is provided by
+the rmail-mime package available at <http://www.m17n.org/rmail-mime/>.")
+
+;;;###autoload
+(defvar rmail-decode-mime-charset t
+  "*Non-nil means a message is decoded by MIME's charset specification.
+If this variable is nil, or the message has not MIME specification,
+the message is decoded as normal way.
+
+If the variable `rmail-enable-mime' is non-nil, this variables is
+ignored, and all the decoding work is done by a feature specified by
+the variable `rmail-mime-feature'.")
+
+;;;###autoload
+(defvar rmail-mime-charset-pattern
+  (concat "^content-type:[ \t]*text/plain;"
+	  "\\(?:[ \t\n]*\\(?:format\\|delsp\\)=\"?[-a-z0-9]+\"?;\\)*"
+	  "[ \t\n]*charset=\"?\\([^ \t\n\";]+\\)\"?")
+  "Regexp to match MIME-charset specification in a header of message.
+The first parenthesized expression should match the MIME-charset name.")
+
+
+;;; Regexp matching the delimiter of messages in UNIX mail format
+;;; (UNIX From lines), minus the initial ^.  Note that if you change
+;;; this expression, you must change the code in rmail-nuke-pinhead-header
+;;; that knows the exact ordering of the \\( \\) subexpressions.
+(defvar rmail-unix-mail-delimiter
+  (let ((time-zone-regexp
+	 (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
+		 "\\|[-+]?[0-9][0-9][0-9][0-9]"
+		 "\\|"
+		 "\\) *")))
+    (concat
+     "From "
+
+     ;; Many things can happen to an RFC 822 mailbox before it is put into
+     ;; a `From' line.  The leading phrase can be stripped, e.g.
+     ;; `Joe <@w.x:joe@y.z>' -> `<@w.x:joe@y.z>'.  The <> can be stripped, e.g.
+     ;; `<@x.y:joe@y.z>' -> `@x.y:joe@y.z'.  Everything starting with a CRLF
+     ;; can be removed, e.g.
+     ;;		From: joe@y.z (Joe	K
+     ;;			User)
+     ;; can yield `From joe@y.z (Joe 	K Fri Mar 22 08:11:15 1996', and
+     ;;		From: Joe User
+     ;;			<joe@y.z>
+     ;; can yield `From Joe User Fri Mar 22 08:11:15 1996'.
+     ;; The mailbox can be removed or be replaced by white space, e.g.
+     ;;		From: "Joe User"{space}{tab}
+     ;;			<joe@y.z>
+     ;; can yield `From {space}{tab} Fri Mar 22 08:11:15 1996',
+     ;; where {space} and {tab} represent the Ascii space and tab characters.
+     ;; We want to match the results of any of these manglings.
+     ;; The following regexp rejects names whose first characters are
+     ;; obviously bogus, but after that anything goes.
+     "\\([^\0-\b\n-\r\^?].*\\)? "
+
+     ;; The time the message was sent.
+     "\\([^\0-\r \^?]+\\) +"				; day of the week
+     "\\([^\0-\r \^?]+\\) +"				; month
+     "\\([0-3]?[0-9]\\) +"				; day of month
+     "\\([0-2][0-9]:[0-5][0-9]\\(:[0-6][0-9]\\)?\\) *"	; time of day
+
+     ;; Perhaps a time zone, specified by an abbreviation, or by a
+     ;; numeric offset.
+     time-zone-regexp
+
+     ;; The year.
+     " \\([0-9][0-9]+\\) *"
+
+     ;; On some systems the time zone can appear after the year, too.
+     time-zone-regexp
+
+     ;; Old uucp cruft.
+     "\\(remote from .*\\)?"
+
+     "\n"))
+  nil)
+
+(defvar rmail-font-lock-keywords
+  ;; These are all matched case-insensitively.
+  (eval-when-compile
+    (let* ((cite-chars "[>|}]")
+	   (cite-prefix "a-z")
+	   (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
+      (list '("^\\(From\\|Sender\\|Resent-From\\):"
+	      . 'rmail-header-name)
+	    '("^Reply-To:.*$" . 'rmail-header-name)
+	    '("^Subject:" . 'rmail-header-name)
+	    '("^X-Spam-Status:" . 'rmail-header-name)
+	    '("^\\(To\\|Apparently-To\\|Cc\\|Newsgroups\\):"
+	      . 'rmail-header-name)
+	    ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
+	    `(,cite-chars
+	      (,(concat "\\=[ \t]*"
+			"\\(\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
+			"\\(" cite-chars "[ \t]*\\)\\)+\\)"
+			"\\(.*\\)")
+	       (beginning-of-line) (end-of-line)
+	       (1 font-lock-comment-delimiter-face nil t)
+	       (5 font-lock-comment-face nil t)))
+	    '("^\\(X-[a-z0-9-]+\\|In-reply-to\\|Date\\):.*\\(\n[ \t]+.*\\)*$"
+	      . 'rmail-header-name))))
+  "Additional expressions to highlight in Rmail mode.")
+
+;; Perform BODY in the summary buffer
+;; in such a way that its cursor is properly updated in its own window.
+(defmacro rmail-select-summary (&rest body)
+  `(let ((total rmail-total-messages))
+     (if (rmail-summary-displayed)
+	 (let ((window (selected-window)))
+	   (save-excursion
+	     (unwind-protect
+		 (progn
+		   (pop-to-buffer rmail-summary-buffer)
+		   ;; rmail-total-messages is a buffer-local var
+		   ;; in the rmail buffer.
+		   ;; This way we make it available for the body
+		   ;; even tho the rmail buffer is not current.
+		   (let ((rmail-total-messages total))
+		     ,@body))
+	       (select-window window))))
+       (save-excursion
+	 (set-buffer rmail-summary-buffer)
+	 (let ((rmail-total-messages total))
+	   ,@body)))
+     (rmail-maybe-display-summary)))
+
+;;;; *** Rmail Mode ***
+
+;; This variable is dynamically bound.  The defvar is here to placate
+;; the byte compiler.
+
+(defvar rmail-enable-multibyte nil)
+
+
+(defun rmail-require-mime-maybe ()
+  "Require `rmail-mime-feature' if that is non-nil.
+Signal an error and set `rmail-mime-feature' to nil if the feature
+isn't provided."
+  (when rmail-enable-mime
+    (condition-case err
+	(require rmail-mime-feature)
+      (error
+       (display-warning
+	'rmail
+	(format "Although MIME support is requested
+by setting `rmail-enable-mime' to non-nil, the required feature
+`%s' (the value of `rmail-mime-feature')
+is not available in the current session.
+So, the MIME support is turned off for the moment."
+		rmail-mime-feature)
+	:warning)
+       (setq rmail-enable-mime nil)))))
+
+
+;;;###autoload
+(defun rmail (&optional file-name-arg)
+  "Read and edit incoming mail.
+Moves messages into file named by `rmail-file-name' (a babyl format file)
+ and edits that file in RMAIL Mode.
+Type \\[describe-mode] once editing that file, for a list of RMAIL commands.
+
+May be called with file name as argument; then performs rmail editing on
+that file, but does not copy any new mail into the file.
+Interactively, if you supply a prefix argument, then you
+have a chance to specify a file name with the minibuffer.
+
+If `rmail-display-summary' is non-nil, make a summary for this RMAIL file."
+  (interactive (if current-prefix-arg
+		   (list (read-file-name "Run rmail on RMAIL file: "))))
+  (rmail-require-mime-maybe)
+  (let* ((file-name (expand-file-name (or file-name-arg rmail-file-name)))
+	 ;; Use find-buffer-visiting, not get-file-buffer, for those users
+	 ;; who have find-file-visit-truename set to t.
+	 (existed (find-buffer-visiting file-name))
+	 run-mail-hook mail-buf msg-shown)
+    ;; Determine if an existing mail file has been changed behind the
+    ;; scene...
+    (if (and existed (not (verify-visited-file-modtime existed)))
+	;; The mail file has been changed.  Revisit it and reset the
+	;; message state variables when in rmail mode.
+	(progn
+	  (find-file file-name)
+	  (when (and (verify-visited-file-modtime existed)
+		     (eq major-mode 'rmail-mode))
+	    (rmail-set-message-counters)))
+      ;; The mail file is either unchanged or not visited.  Visit it.
+      (switch-to-buffer
+       (let ((enable-local-variables nil))
+	 (find-file-noselect file-name))))
+    ;; Insure that the collection and view buffers are in sync and
+    ;; insure that a message is not being edited.
+    (if (eq major-mode 'rmail-mode)
+	(rmail-swap-buffers-maybe))
+    (if (eq major-mode 'rmail-edit-mode)
+	(error "Exit Rmail Edit mode before getting new mail"))
+    (or (and existed (> (buffer-size) 0))
+	(setq run-mail-hook t))
+    ;; Insure that the Rmail file is in mbox format, the buffer is in
+    ;; Rmail mode and has been scanned to find all the messages
+    ;; (setting the global message variables in the process).
+    (rmail-convert-file-maybe)
+    (unless (eq major-mode 'rmail-mode)
+      (rmail-mode-2))
+    (goto-char (point-max))
+    (rmail-maybe-set-message-counters)
+    (setq mail-buf rmail-buffer)
+    ;; Show the first unread message and process summary mode.
+    (unwind-protect
+	;; Only get new mail when there is not a file name argument.
+	(unless file-name-arg
+	  (rmail-get-new-mail))
+      (progn
+	(set-buffer mail-buf)
+	(rmail-show-message-maybe (rmail-first-unseen-message))
+	(if rmail-display-summary (rmail-summary))
+	(rmail-construct-io-menu)
+	(if run-mail-hook
+	    (run-hooks 'rmail-mode-hook))))))
+
+(defun rmail-convert-file-maybe ()
+  "Determine if the file needs to be converted to mbox format."
+  (widen)
+  (goto-char (point-min))
+  ;; Detect previous Babyl format files.
+  (cond ((looking-at "BABYL OPTIONS:")
+	 ;; The file is Babyl version 5.  Use unrmail to convert
+	 ;; it.
+	 (rmail-convert-babyl-to-mbox))
+	((looking-at "Version: 5\n")
+	 ;; Losing babyl file made by old version of Rmail.  Fix the
+	 ;; babyl file header and use unrmail to convert to mbox
+	 ;; format.
+	 (let ((buffer-read-only nil))
+	   (insert "BABYL OPTIONS: -*- rmail -*-\n")
+	   (rmail-convert-babyl-to-mbox)))
+	((equal (point-min) (point-max))
+	 (message "Empty Rmail file."))
+	((looking-at "From "))
+	(t (error "Invalid mbox file"))))
+
+(defun rmail-error-bad-format (&optional msgnum)
+  "Report that the buffer is not in the mbox file format.
+MSGNUM, if present, indicates the malformed message."
+  (if msgnum
+      (error "Message %d is not a valid RFC2822 message" msgnum)
+    (error "Message is not a valid RFC2822 message")))
+
+(defun rmail-convert-babyl-to-mbox ()
+  "Convert the mail file from Babyl version 5 to mbox.
+This function also reinitializes local variables used by Rmail."
+  (let ((old-file (make-temp-file "rmail"))
+	(new-file (make-temp-file "rmail")))
+    (unwind-protect
+	(progn
+	  (kill-all-local-variables)
+	  (write-region (point-min) (point-max) old-file)
+	  (unrmail old-file new-file)
+	  (message "Replacing BABYL format with mbox format...")
+	  (let ((inhibit-read-only t))
+	    (erase-buffer)
+	    (insert-file-contents-literally new-file)
+	    (rmail-mode-1)
+	    (rmail-perm-variables)
+	    (rmail-variables)
+	    (goto-char (point-max))
+	    (rmail-set-message-counters))
+	  (message "Replacing BABYL format with mbox format...done"))
+      (delete-file old-file)
+      (delete-file new-file))))
+
+(defun rmail-get-coding-system ()
+  "Return a suitable coding system to use for the current mail message.
+The buffer is expected to be narrowed to just the header of the message."
+  (let ((content-type-header (mail-fetch-field "content-type"))
+	separator)
+    (save-excursion
+      (setq separator (search-forward "\n\n")))
+    (if (and content-type-header
+	     (string-match rmail-mime-charset-pattern content-type-header))
+	(substring content-type-header (match-beginning 1) (match-end 1))
+      'undecided)))
+
+;;; Set up Rmail mode keymaps
+
+(defvar rmail-mode-map nil)
+(if rmail-mode-map
+    nil
+  (setq rmail-mode-map (make-keymap))
+  (suppress-keymap rmail-mode-map)
+  (define-key rmail-mode-map "a"      'rmail-add-label)
+  (define-key rmail-mode-map "b"      'rmail-bury)
+  (define-key rmail-mode-map "c"      'rmail-continue)
+  (define-key rmail-mode-map "d"      'rmail-delete-forward)
+  (define-key rmail-mode-map "\C-d"   'rmail-delete-backward)
+  (define-key rmail-mode-map "e"      'rmail-edit-current-message)
+  (define-key rmail-mode-map "f"      'rmail-forward)
+  (define-key rmail-mode-map "g"      'rmail-get-new-mail)
+  (define-key rmail-mode-map "h"      'rmail-summary)
+  (define-key rmail-mode-map "i"      'rmail-input)
+  (define-key rmail-mode-map "j"      'rmail-show-message-maybe)
+  (define-key rmail-mode-map "k"      'rmail-kill-label)
+  (define-key rmail-mode-map "l"      'rmail-summary-by-labels)
+  (define-key rmail-mode-map "\e\C-h" 'rmail-summary)
+  (define-key rmail-mode-map "\e\C-l" 'rmail-summary-by-labels)
+  (define-key rmail-mode-map "\e\C-r" 'rmail-summary-by-recipients)
+  (define-key rmail-mode-map "\e\C-s" 'rmail-summary-by-regexp)
+  (define-key rmail-mode-map "\e\C-t" 'rmail-summary-by-topic)
+  (define-key rmail-mode-map "m"      'rmail-mail)
+  (define-key rmail-mode-map "\em"    'rmail-retry-failure)
+  (define-key rmail-mode-map "n"      'rmail-next-undeleted-message)
+  (define-key rmail-mode-map "\en"    'rmail-next-message)
+  (define-key rmail-mode-map "\e\C-n" 'rmail-next-labeled-message)
+  (define-key rmail-mode-map "o"      'rmail-output)
+  (define-key rmail-mode-map "\C-o"   'rmail-output-as-seen)
+  (define-key rmail-mode-map "p"      'rmail-previous-undeleted-message)
+  (define-key rmail-mode-map "\ep"    'rmail-previous-message)
+  (define-key rmail-mode-map "\e\C-p" 'rmail-previous-labeled-message)
+  (define-key rmail-mode-map "q"      'rmail-quit)
+  (define-key rmail-mode-map "r"      'rmail-reply)
+;; I find I can't live without the default M-r command -- rms.
+;;  (define-key rmail-mode-map "\er"  'rmail-search-backwards)
+  (define-key rmail-mode-map "s"      'rmail-expunge-and-save)
+  (define-key rmail-mode-map "\es"    'rmail-search)
+  (define-key rmail-mode-map "t"      'rmail-toggle-header)
+  (define-key rmail-mode-map "u"      'rmail-undelete-previous-message)
+  (define-key rmail-mode-map "w"      'rmail-output-body-to-file)
+  (define-key rmail-mode-map "\C-c\C-w"    'rmail-widen)
+  (define-key rmail-mode-map "x"      'rmail-expunge)
+  (define-key rmail-mode-map "."      'rmail-beginning-of-message)
+  (define-key rmail-mode-map "/"      'rmail-end-of-message)
+  (define-key rmail-mode-map "<"      'rmail-first-message)
+  (define-key rmail-mode-map ">"      'rmail-last-message)
+  (define-key rmail-mode-map " "      'scroll-up)
+  (define-key rmail-mode-map "\177"   'scroll-down)
+  (define-key rmail-mode-map "?"      'describe-mode)
+  (define-key rmail-mode-map "\C-c\C-s\C-d" 'rmail-sort-by-date)
+  (define-key rmail-mode-map "\C-c\C-s\C-s" 'rmail-sort-by-subject)
+  (define-key rmail-mode-map "\C-c\C-s\C-a" 'rmail-sort-by-author)
+  (define-key rmail-mode-map "\C-c\C-s\C-r" 'rmail-sort-by-recipient)
+  (define-key rmail-mode-map "\C-c\C-s\C-c" 'rmail-sort-by-correspondent)
+  (define-key rmail-mode-map "\C-c\C-s\C-l" 'rmail-sort-by-lines)
+  (define-key rmail-mode-map "\C-c\C-s\C-k" 'rmail-sort-by-labels)
+  (define-key rmail-mode-map "\C-c\C-n" 'rmail-next-same-subject)
+  (define-key rmail-mode-map "\C-c\C-p" 'rmail-previous-same-subject)
+  )
+
+(define-key rmail-mode-map [menu-bar] (make-sparse-keymap))
+
+(define-key rmail-mode-map [menu-bar classify]
+  (cons "Classify" (make-sparse-keymap "Classify")))
+
+(define-key rmail-mode-map [menu-bar classify input-menu]
+  nil)
+
+(define-key rmail-mode-map [menu-bar classify output-menu]
+  nil)
+
+(define-key rmail-mode-map [menu-bar classify output-body]
+  '("Output body to file..." . rmail-output-body-to-file))
+
+(define-key rmail-mode-map [menu-bar classify output-inbox]
+  '("Output..." . rmail-output))
+
+(define-key rmail-mode-map [menu-bar classify output]
+  '("Output as seen..." . rmail-output-as-seen))
+
+(define-key rmail-mode-map [menu-bar classify kill-label]
+  '("Kill Label..." . rmail-kill-label))
+
+(define-key rmail-mode-map [menu-bar classify add-label]
+  '("Add Label..." . rmail-add-label))
+
+(define-key rmail-mode-map [menu-bar summary]
+  (cons "Summary" (make-sparse-keymap "Summary")))
+
+(define-key rmail-mode-map [menu-bar summary senders]
+  '("By Senders..." . rmail-summary-by-senders))
+
+(define-key rmail-mode-map [menu-bar summary labels]
+  '("By Labels..." . rmail-summary-by-labels))
+
+(define-key rmail-mode-map [menu-bar summary recipients]
+  '("By Recipients..." . rmail-summary-by-recipients))
+
+(define-key rmail-mode-map [menu-bar summary topic]
+  '("By Topic..." . rmail-summary-by-topic))
+
+(define-key rmail-mode-map [menu-bar summary regexp]
+  '("By Regexp..." . rmail-summary-by-regexp))
+
+(define-key rmail-mode-map [menu-bar summary all]
+  '("All" . rmail-summary))
+
+(define-key rmail-mode-map [menu-bar mail]
+  (cons "Mail" (make-sparse-keymap "Mail")))
+
+(define-key rmail-mode-map [menu-bar mail rmail-get-new-mail]
+  '("Get New Mail" . rmail-get-new-mail))
+
+(define-key rmail-mode-map [menu-bar mail lambda]
+  '("----"))
+
+(define-key rmail-mode-map [menu-bar mail continue]
+  '("Continue" . rmail-continue))
+
+(define-key rmail-mode-map [menu-bar mail resend]
+  '("Re-send..." . rmail-resend))
+
+(define-key rmail-mode-map [menu-bar mail forward]
+  '("Forward" . rmail-forward))
+
+(define-key rmail-mode-map [menu-bar mail retry]
+  '("Retry" . rmail-retry-failure))
+
+(define-key rmail-mode-map [menu-bar mail reply]
+  '("Reply" . rmail-reply))
+
+(define-key rmail-mode-map [menu-bar mail mail]
+  '("Mail" . rmail-mail))
+
+(define-key rmail-mode-map [menu-bar delete]
+  (cons "Delete" (make-sparse-keymap "Delete")))
+
+(define-key rmail-mode-map [menu-bar delete expunge/save]
+  '("Expunge/Save" . rmail-expunge-and-save))
+
+(define-key rmail-mode-map [menu-bar delete expunge]
+  '("Expunge" . rmail-expunge))
+
+(define-key rmail-mode-map [menu-bar delete undelete]
+  '("Undelete" . rmail-undelete-previous-message))
+
+(define-key rmail-mode-map [menu-bar delete delete]
+  '("Delete" . rmail-delete-forward))
+
+(define-key rmail-mode-map [menu-bar move]
+  (cons "Move" (make-sparse-keymap "Move")))
+
+(define-key rmail-mode-map [menu-bar move search-back]
+  '("Search Back..." . rmail-search-backwards))
+
+(define-key rmail-mode-map [menu-bar move search]
+  '("Search..." . rmail-search))
+
+(define-key rmail-mode-map [menu-bar move previous]
+  '("Previous Nondeleted" . rmail-previous-undeleted-message))
+
+(define-key rmail-mode-map [menu-bar move next]
+  '("Next Nondeleted" . rmail-next-undeleted-message))
+
+(define-key rmail-mode-map [menu-bar move last]
+  '("Last" . rmail-last-message))
+
+(define-key rmail-mode-map [menu-bar move first]
+  '("First" . rmail-first-message))
+
+(define-key rmail-mode-map [menu-bar move previous]
+  '("Previous" . rmail-previous-message))
+
+(define-key rmail-mode-map [menu-bar move next]
+  '("Next" . rmail-next-message))
+
+;; Rmail toolbar
+(defvar rmail-tool-bar-map
+  (let ((map (make-sparse-keymap)))
+    (tool-bar-local-item-from-menu 'rmail-get-new-mail "mail/inbox"
+				   map rmail-mode-map)
+    (tool-bar-local-item-from-menu 'rmail-next-undeleted-message "right-arrow"
+				   map rmail-mode-map)
+    (tool-bar-local-item-from-menu 'rmail-previous-undeleted-message "left-arrow"
+				   map rmail-mode-map)
+    (tool-bar-local-item-from-menu 'rmail-search "search"
+				   map rmail-mode-map)
+    (tool-bar-local-item-from-menu 'rmail-input "open"
+				   map rmail-mode-map)
+    (tool-bar-local-item-from-menu 'rmail-mail "mail/compose"
+				   map rmail-mode-map)
+    (tool-bar-local-item-from-menu 'rmail-reply "mail/reply-all"
+				   map rmail-mode-map)
+    (tool-bar-local-item-from-menu 'rmail-forward "mail/forward"
+				   map rmail-mode-map)
+    (tool-bar-local-item-from-menu 'rmail-delete-forward "close"
+				   map rmail-mode-map)
+    (tool-bar-local-item-from-menu 'rmail-output "mail/move"
+				   map rmail-mode-map)
+    (tool-bar-local-item-from-menu 'rmail-output-body-to-file "mail/save"
+				   map rmail-mode-map)
+    (tool-bar-local-item-from-menu 'rmail-expunge "delete"
+				   map rmail-mode-map)
+    map))
+
+
+
+;; Rmail mode is suitable only for specially formatted data.
+(put 'rmail-mode 'mode-class 'special)
+
+(defun rmail-mode-kill-summary ()
+  (if rmail-summary-buffer (kill-buffer rmail-summary-buffer)))
+
+;;;###autoload
+(defun rmail-mode ()
+  "Rmail Mode is used by \\<rmail-mode-map>\\[rmail] for editing Rmail files.
+All normal editing commands are turned off.
+Instead, these commands are available:
+
+\\[rmail-beginning-of-message]	Move point to front of this message.
+\\[rmail-end-of-message]	Move point to bottom of this message.
+\\[scroll-up]	Scroll to next screen of this message.
+\\[scroll-down]	Scroll to previous screen of this message.
+\\[rmail-next-undeleted-message]	Move to Next non-deleted message.
+\\[rmail-previous-undeleted-message]	Move to Previous non-deleted message.
+\\[rmail-next-message]	Move to Next message whether deleted or not.
+\\[rmail-previous-message]	Move to Previous message whether deleted or not.
+\\[rmail-first-message]	Move to the first message in Rmail file.
+\\[rmail-last-message]	Move to the last message in Rmail file.
+\\[rmail-show-message-maybe]	Jump to message specified by numeric position in file.
+\\[rmail-search]	Search for string and show message it is found in.
+\\[rmail-delete-forward]	Delete this message, move to next nondeleted.
+\\[rmail-delete-backward]	Delete this message, move to previous nondeleted.
+\\[rmail-undelete-previous-message]	Undelete message.  Tries current message, then earlier messages
+	till a deleted message is found.
+\\[rmail-edit-current-message]	Edit the current message.  \\[rmail-cease-edit] to return to Rmail.
+\\[rmail-expunge]	Expunge deleted messages.
+\\[rmail-expunge-and-save]	Expunge and save the file.
+\\[rmail-quit]       Quit Rmail: expunge, save, then switch to another buffer.
+\\[save-buffer] Save without expunging.
+\\[rmail-get-new-mail]	Move new mail from system spool directory into this file.
+\\[rmail-mail]	Mail a message (same as \\[mail-other-window]).
+\\[rmail-continue]	Continue composing outgoing message started before.
+\\[rmail-reply]	Reply to this message.  Like \\[rmail-mail] but initializes some fields.
+\\[rmail-retry-failure]	Send this message again.  Used on a mailer failure message.
+\\[rmail-forward]	Forward this message to another user.
+\\[rmail-output]	Output (append) this message to another mail file.
+\\[rmail-output-as-seen]	Output (append) this message to file as it's displayed.
+\\[rmail-output-body-to-file]	Save message body to a file.  Default filename comes from Subject line.
+\\[rmail-input]	Input Rmail file.  Run Rmail on that file.
+\\[rmail-add-label]	Add label to message.  It will be displayed in the mode line.
+\\[rmail-kill-label]	Kill label.  Remove a label from current message.
+\\[rmail-next-labeled-message]   Move to Next message with specified label
+          (label defaults to last one specified).
+          Standard labels: filed, unseen, answered, forwarded, deleted.
+          Any other label is present only if you add it with \\[rmail-add-label].
+\\[rmail-previous-labeled-message]   Move to Previous message with specified label
+\\[rmail-summary]	Show headers buffer, with a one line summary of each message.
+\\[rmail-summary-by-labels]	Summarize only messages with particular label(s).
+\\[rmail-summary-by-recipients]   Summarize only messages with particular recipient(s).
+\\[rmail-summary-by-regexp]   Summarize only messages with particular regexp(s).
+\\[rmail-summary-by-topic]   Summarize only messages with subject line regexp(s).
+\\[rmail-toggle-header]	Toggle display of complete header."
+  (interactive)
+  (let ((finding-rmail-file (not (eq major-mode 'rmail-mode))))
+    (rmail-mode-2)
+    (when (and finding-rmail-file
+	       (null coding-system-for-read)
+	       default-enable-multibyte-characters)
+      (let ((rmail-enable-multibyte t))
+	(rmail-require-mime-maybe)
+	(rmail-convert-file-maybe)
+	(goto-char (point-max))
+	(set-buffer-multibyte t)))
+    (rmail-set-message-counters)
+    (rmail-show-message-maybe rmail-total-messages)
+    (when finding-rmail-file
+      (when rmail-display-summary
+	(rmail-summary))
+      (rmail-construct-io-menu))
+    (run-mode-hooks 'rmail-mode-hook)))
+
+(defun rmail-mode-2 ()
+  (kill-all-local-variables)
+  (rmail-mode-1)
+  (rmail-perm-variables)
+  (rmail-variables))
+
+(defun rmail-mode-1 ()
+  (setq major-mode 'rmail-mode)
+  (setq mode-name "RMAIL")
+  (setq buffer-read-only t)
+  ;; No need to auto save RMAIL files in normal circumstances
+  ;; because they contain no info except attribute changes
+  ;; and deletion of messages.
+  ;; The one exception is when messages are copied into another mbox buffer.
+  ;; rmail-output enables auto save when you do that.
+  (setq buffer-auto-save-file-name nil)
+  (use-local-map rmail-mode-map)
+  (set-syntax-table text-mode-syntax-table)
+  (setq local-abbrev-table text-mode-abbrev-table)
+  ;; Functions to support buffer swapping:
+  (add-hook 'write-region-annotate-functions
+	    'rmail-write-region-annotate nil t)
+  (add-hook 'kill-buffer-hook 'rmail-mode-kill-buffer-hook nil t)
+  (add-hook 'change-major-mode-hook 'rmail-change-major-mode-hook nil t))
+
+(defun rmail-generate-viewer-buffer ()
+  "Return a reusable buffer suitable for viewing messages.
+Create the buffer if necessary."
+  (let* ((suffix (file-name-nondirectory (or buffer-file-name (buffer-name))))
+	 (name (format " *message-viewer %s*" suffix))
+	 (buf (get-buffer name)))
+    (unless buf
+      (generate-new-buffer name))))
+
+(defun rmail-change-major-mode-hook ()
+  ;; Bring the actual Rmail messages back into the main buffer.
+  (when (rmail-buffers-swapped-p)
+    (setq rmail-buffer-swapped nil)
+    (let ((modp (buffer-modified-p)))
+      (buffer-swap-text rmail-view-buffer)
+      (set-buffer-modified-p modp))))
+
+(defun rmail-buffers-swapped-p ()
+  "Return non-nil if the message collection is in `rmail-view-buffer'."
+  ;; This is analogous to tar-data-swapped-p in tar-mode.el.
+  (and (buffer-live-p rmail-view-buffer)
+       rmail-buffer-swapped))
+
+(defun rmail-swap-buffers-maybe ()
+  "Determine if the Rmail buffer is showing a message.
+If so restore the actual mbox message collection."
+  (with-current-buffer rmail-buffer
+    (when (rmail-buffers-swapped-p)
+      (let ((modp (buffer-modified-p)))
+	(buffer-swap-text rmail-view-buffer)
+	(set-buffer-modified-p modp))
+      (setq rmail-buffer-swapped nil))))
+
+(defun rmail-mode-kill-buffer-hook ()
+  (if (buffer-live-p rmail-view-buffer) (kill-buffer rmail-view-buffer)))
+
+;; Set up the permanent locals associated with an Rmail file.
+(defun rmail-perm-variables ()
+  (make-local-variable 'rmail-last-regexp)
+  (make-local-variable 'rmail-deleted-vector)
+  (make-local-variable 'rmail-buffer)
+  (setq rmail-buffer (current-buffer))
+  (set-buffer-multibyte nil)
+  (make-local-variable 'rmail-view-buffer)
+  (save-excursion
+    (setq rmail-view-buffer (rmail-generate-viewer-buffer))
+    (set-buffer rmail-view-buffer)
+    (setq buffer-undo-list t)
+    (set-buffer-multibyte t))
+  (make-local-variable 'rmail-summary-buffer)
+  (make-local-variable 'rmail-summary-vector)
+  (make-local-variable 'rmail-current-message)
+  (make-local-variable 'rmail-total-messages)
+  (setq rmail-total-messages 0)
+  (make-local-variable 'rmail-overlay-list)
+  (setq rmail-overlay-list nil)
+  (make-local-variable 'rmail-message-vector)
+  (make-local-variable 'rmail-msgref-vector)
+  (make-local-variable 'rmail-inbox-list)
+  ;; Provide default set of inboxes for primary mail file ~/RMAIL.
+  (and (null rmail-inbox-list)
+       (or (equal buffer-file-name (expand-file-name rmail-file-name))
+	   (equal buffer-file-truename
+		  (abbreviate-file-name (file-truename rmail-file-name))))
+       (setq rmail-inbox-list
+	     (or rmail-primary-inbox-list
+		 (list (or (getenv "MAIL")
+			   (concat rmail-spool-directory
+				   (user-login-name)))))))
+  (set (make-local-variable 'tool-bar-map) rmail-tool-bar-map))
+
+;; Set up the non-permanent locals associated with Rmail mode.
+(defun rmail-variables ()
+  ;; Turn off undo.  We turn it back on in rmail-edit.
+  (setq buffer-undo-list t)
+  ;; Don't let a local variables list in a message cause confusion.
+  (make-local-variable 'local-enable-local-variables)
+  (setq local-enable-local-variables nil)
+  (make-local-variable 'revert-buffer-function)
+  (setq revert-buffer-function 'rmail-revert)
+  (make-local-variable 'font-lock-defaults)
+  (setq font-lock-defaults
+	'(rmail-font-lock-keywords
+	  t t nil nil
+	  (font-lock-maximum-size . nil)
+	  (font-lock-fontify-buffer-function . rmail-fontify-buffer-function)
+	  (font-lock-unfontify-buffer-function . rmail-unfontify-buffer-function)
+	  (font-lock-inhibit-thing-lock . (lazy-lock-mode fast-lock-mode))))
+  (make-local-variable 'require-final-newline)
+  (setq require-final-newline nil)
+  (make-local-variable 'version-control)
+  (setq version-control 'never)
+  (make-local-variable 'kill-buffer-hook)
+  (add-hook 'kill-buffer-hook 'rmail-mode-kill-summary)
+  (make-local-variable 'file-precious-flag)
+  (setq file-precious-flag t)
+  (make-local-variable 'desktop-save-buffer)
+  (setq desktop-save-buffer t))
+
+;; Handle M-x revert-buffer done in an rmail-mode buffer.
+(defun rmail-revert (arg noconfirm)
+  (set-buffer rmail-buffer)
+  (let* ((revert-buffer-function (default-value 'revert-buffer-function))
+	 (rmail-enable-multibyte enable-multibyte-characters)
+	 ;; See similar code in `rmail'.
+	 (coding-system-for-read (and rmail-enable-multibyte 'raw-text)))
+    ;; Call our caller again, but this time it does the default thing.
+    (when (revert-buffer arg noconfirm)
+      ;; If the user said "yes", and we changed something,
+      ;; reparse the messages.
+      (set-buffer rmail-buffer)
+      (rmail-mode-2)
+      ;; Convert all or part to Babyl file if possible.
+      (rmail-convert-file-maybe)
+      ;; We have read the file as raw-text, so the buffer is set to
+      ;; unibyte.  Make it multibyte if necessary.
+      (if (and rmail-enable-multibyte
+	       (not enable-multibyte-characters))
+	  (set-buffer-multibyte t))
+      (goto-char (point-max))
+      (rmail-set-message-counters)
+      (rmail-show-message-maybe rmail-total-messages)
+      (run-hooks 'rmail-mode-hook))))
+
+(defun rmail-expunge-and-save ()
+  "Expunge and save RMAIL file."
+  (interactive)
+  (set-buffer rmail-buffer)
+  (rmail-expunge t)
+  (rmail-swap-buffers-maybe)
+  (save-buffer)
+  (if (rmail-summary-exists)
+      (rmail-select-summary (set-buffer-modified-p nil))
+    (rmail-show-message)))
+
+(defun rmail-quit ()
+  "Quit out of RMAIL.
+Hook `rmail-quit-hook' is run after expunging."
+  (interactive)
+  (set-buffer rmail-buffer)
+  (rmail-expunge t)
+  (rmail-swap-buffers-maybe)
+  (save-buffer)
+  (when (boundp 'rmail-quit-hook)
+    (run-hooks 'rmail-quit-hook))
+  ;; Don't switch to the summary buffer even if it was recently visible.
+  (when rmail-summary-buffer
+    (replace-buffer-in-windows rmail-summary-buffer)
+    (bury-buffer rmail-summary-buffer))
+  (if rmail-enable-mime
+      (let ((obuf rmail-buffer)
+	    (ovbuf rmail-view-buffer))
+	(set-buffer rmail-view-buffer)
+	(quit-window)
+	(replace-buffer-in-windows ovbuf)
+	(replace-buffer-in-windows obuf)
+	(bury-buffer obuf))
+    (let ((obuf (current-buffer)))
+      (quit-window)
+      (replace-buffer-in-windows obuf))))
+
+(defun rmail-bury ()
+  "Bury current Rmail buffer and its summary buffer."
+  (interactive)
+  ;; This let var was called rmail-buffer, but that interfered
+  ;; with the buffer-local var used in summary buffers.
+  (let ((buffer-to-bury (current-buffer)))
+    (if (rmail-summary-exists)
+	(let (window)
+	  (while (setq window (get-buffer-window rmail-summary-buffer))
+	    (quit-window nil window))
+	  (bury-buffer rmail-summary-buffer)))
+    (quit-window)))
+
+(defun rmail-duplicate-message ()
+  "Create a duplicated copy of the current message.
+The duplicate copy goes into the Rmail file just after the
+original copy."
+  (interactive)
+  (widen)
+  (let ((buffer-read-only nil)
+	(number rmail-current-message)
+	(string (buffer-substring (rmail-msgbeg rmail-current-message)
+				  (rmail-msgend rmail-current-message))))
+    (goto-char (rmail-msgend rmail-current-message))
+    (insert string)
+    (rmail-forget-messages)
+    (rmail-show-message-maybe number)
+    (message "Message duplicated")))
+
+;;;###autoload
+(defun rmail-input (filename)
+  "Run Rmail on file FILENAME."
+  (interactive "FRun rmail on RMAIL file: ")
+  (rmail filename))
+
+;; This used to scan subdirectories recursively, but someone pointed out
+;; that if the user wants that, person can put all the files in one dir.
+;; And the recursive scan was slow.  So I took it out.
+;; rms, Sep 1996.
+(defun rmail-find-all-files (start)
+  "Return list of file in dir START that match `rmail-secondary-file-regexp'."
+  (if (file-accessible-directory-p start)
+      ;; Don't sort here.
+      (let* ((case-fold-search t)
+	     (files (directory-files start t rmail-secondary-file-regexp)))
+	;; Sort here instead of in directory-files
+	;; because this list is usually much shorter.
+	(sort files 'string<))))
+
+(defun rmail-list-to-menu (menu-name l action &optional full-name)
+  (let ((menu (make-sparse-keymap menu-name)))
+    (mapc
+     (lambda (item)
+       (let (command)
+	 (if (consp item)
+	     (setq command
+		   (rmail-list-to-menu
+		    (car item) (cdr item) action
+		    (if full-name
+			(concat full-name "/"
+				(car item))
+		      (car item)))
+		   name (car item))
+	   (setq name item)
+	   (setq command
+		 (list 'lambda () '(interactive)
+		       (list action
+			     (expand-file-name
+			      (if full-name
+				  (concat full-name "/" item)
+				item)
+			      rmail-secondary-file-directory)))))
+	 (define-key menu (vector (intern name))
+	   (cons name command))))
+     (reverse l))
+    menu))
+
+;; This command is always "disabled" when it appears in a menu.
+(put 'rmail-disable-menu 'menu-enable ''nil)
+
+(defun rmail-construct-io-menu ()
+  (let ((files (rmail-find-all-files rmail-secondary-file-directory)))
+    (if files
+	(progn
+	  (define-key rmail-mode-map [menu-bar classify input-menu]
+	    (cons "Input Rmail File"
+		  (rmail-list-to-menu "Input Rmail File"
+				      files
+				      'rmail-input)))
+	  (define-key rmail-mode-map [menu-bar classify output-menu]
+	    (cons "Output Rmail File"
+		  (rmail-list-to-menu "Output Rmail File"
+				      files
+				      'rmail-output))))
+
+      (define-key rmail-mode-map [menu-bar classify input-menu]
+	'("Input Rmail File" . rmail-disable-menu))
+      (define-key rmail-mode-map [menu-bar classify output-menu]
+	'("Output Rmail File" . rmail-disable-menu)))))
+
+
+;;;; *** Rmail input ***
+
+(declare-function rmail-spam-filter "rmail-spam-filter" (msg))
+(declare-function rmail-summary-goto-msg "rmailsum" (&optional n nowarn skip-rmail))
+(declare-function rmail-summary-mark-undeleted "rmailsum" (n))
+(declare-function rmail-summary-mark-deleted "rmailsum" (&optional n undel))
+(declare-function rfc822-addresses "rfc822" (header-text))
+(declare-function mail-abbrev-make-syntax-table "mailabbrev.el" ())
+(declare-function mail-sendmail-delimit-header "sendmail" ())
+(declare-function mail-header-end "sendmail" ())
+
+;; RLK feature not added in this version:
+;; argument specifies inbox file or files in various ways.
+
+(defun rmail-get-new-mail (&optional file-name)
+  "Move any new mail from this RMAIL file's inbox files.
+The inbox files can be specified with the file's Mail: option.  The
+variable `rmail-primary-inbox-list' specifies the inboxes for your
+primary RMAIL file if it has no Mail: option.  By default, this is
+your /usr/spool/mail/$USER.
+
+You can also specify the file to get new mail from.  In this case, the
+file of new mail is not changed or deleted.  Noninteractively, you can
+pass the inbox file name as an argument.  Interactively, a prefix
+argument causes us to read a file name and use that file as the inbox.
+
+If the variable `rmail-preserve-inbox' is non-nil, new mail will
+always be left in inbox files rather than deleted.
+
+This function runs `rmail-get-new-mail-hook' before saving the updated file.
+It returns t if it got any new messages."
+  (interactive
+   (list (if current-prefix-arg
+	     (read-file-name "Get new mail from file: "))))
+  (run-hooks 'rmail-before-get-new-mail-hook)
+  ;; If the disk file has been changed from under us,
+  ;; revert to it before we get new mail.
+  (or (verify-visited-file-modtime (current-buffer))
+      (find-file (buffer-file-name)))
+  (set-buffer rmail-buffer)
+  (rmail-swap-buffers-maybe)
+  (rmail-maybe-set-message-counters)
+  (widen)
+  ;; Get rid of all undo records for this buffer.
+  (or (eq buffer-undo-list t)
+      (setq buffer-undo-list nil))
+  (let ((all-files (if file-name (list file-name) rmail-inbox-list))
+	(rmail-enable-multibyte (default-value 'enable-multibyte-characters))
+	found)
+    (unwind-protect
+	(when all-files
+	  (let ((opoint (point))
+		;; If buffer has not changed yet, and has not been
+		;; saved yet, don't replace the old backup file now.
+		(make-backup-files (and make-backup-files (buffer-modified-p)))
+		(buffer-read-only nil)
+		;; Don't make undo records while getting mail.
+		(buffer-undo-list t)
+		delete-files success files file-last-names)
+	    ;; Pull files off all-files onto files as long as there is
+	    ;; no name conflict.  A conflict happens when two inbox
+	    ;; file names have the same last component.
+	    (while (and all-files
+			(not (member (file-name-nondirectory (car all-files))
+				     file-last-names)))
+	      (setq files (cons (car all-files) files)
+		    file-last-names
+		    (cons (file-name-nondirectory (car all-files)) files))
+	      (setq all-files (cdr all-files)))
+	    ;; Put them back in their original order.
+	    (setq files (nreverse files))
+	    (goto-char (point-max))
+	    (skip-chars-backward " \t\n") ; just in case of brain damage
+	    (delete-region (point) (point-max)) ; caused by require-final-newline
+	    (setq found (rmail-get-new-mail-1 file-name files delete-files))))
+      found)
+    ;; Don't leave the buffer screwed up if we get a disk-full error.
+    (or found (rmail-show-message-maybe))))
+
+(defun rmail-get-new-mail-1 (file-name files delete-files)
+  "Return t if new messages are detected without error, nil otherwise."
+  (save-excursion
+    (save-restriction
+      (let ((new-messages 0)
+	    (spam-filter-p (and (featurep 'rmail-spam-filter)
+				rmail-use-spam-filter))
+	    (blurb "")
+	    result success suffix)
+	(narrow-to-region (point) (point))
+	;; Read in the contents of the inbox files, renaming them as
+	;; necessary, and adding to the list of files to delete
+	;; eventually.
+	(if file-name
+	    (rmail-insert-inbox-text files nil)
+	  (setq delete-files (rmail-insert-inbox-text files t)))
+	;; Scan the new text and convert each message to
+	;; Rmail/mbox format.
+	(goto-char (point-min))
+	(skip-chars-forward " \n")
+	(narrow-to-region (point) (point-max))
+	(unwind-protect
+	    (setq new-messages (rmail-add-mbox-headers)
+		  success t)
+	  ;; Try to delete the garbage just inserted.
+	  (or success (delete-region (point-min) (point-max)))
+	  ;; If we could not convert the file's inboxes, rename the
+	  ;; files we tried to read so we won't over and over again.
+	  (if (and (not file-name) (not success))
+	      (let ((delfiles delete-files)
+		    (count 0))
+		(while delfiles
+		  (while (file-exists-p (format "RMAILOSE.%d" count))
+		    (setq count (1+ count)))
+		  (rename-file (car delfiles) (format "RMAILOSE.%d" count))
+		  (setq delfiles (cdr delfiles))))))
+	;; Determine if there are messages.
+	(unless (zerop new-messages)
+	  ;; There are.  Process them.
+	  (goto-char (point-min))
+	  (rmail-count-new-messages)
+	  (run-hooks 'rmail-get-new-mail-hook)
+	  (save-buffer))
+	;; Delete the old files, now that the Rmail file is saved.
+	(while delete-files
+	  (condition-case ()
+	      ;; First, try deleting.
+	      (condition-case ()
+		  (delete-file (car delete-files))
+		(file-error
+		 ;; If we can't delete it, truncate it.
+		 (write-region (point) (point) (car delete-files))))
+	    (file-error nil))
+	  (setq delete-files (cdr delete-files)))
+	(if (zerop new-messages)
+	    (when (or file-name rmail-inbox-list)
+	      (message "(No new mail has arrived)"))
+	  ;; Generate the spam message.
+	  (setq blurb (if spam-filter-p
+			  (rmail-get-new-mail-filter-spam new-messages)
+			"")))
+	(if (rmail-summary-exists)
+	    (rmail-select-summary (rmail-update-summary)))
+	(setq suffix (if (= 1 new-messages) "" "s"))
+	(message "%d new message%s read%s" new-messages suffix blurb)
+	(when spam-filter-p
+	  (if rsf-beep (beep t))
+	  (sleep-for rsf-sleep-after-message))
+
+	;; Establish the return value and move to the first new
+	;; message unless we have other unseen messages before it.
+	(setq result (> new-messages 0))
+	(when result
+	  (rmail-show-message-maybe (rmail-first-unseen-message)))
+	(run-hooks 'rmail-after-get-new-mail-hook)
+	result))))
+
+(defun rmail-get-new-mail-filter-spam (new-message-count)
+  "Process new messages for spam."
+  (let* ((old-messages (- rmail-total-messages new-message-count))
+	 (rsf-number-of-spam 0)
+	 (rsf-scanned-message-number (1+ old-messages))
+	 ;; save deletion flags of old messages: vector starts at zero
+	 ;; (is one longer that no of messages), therefore take 1+
+	 ;; old-messages
+	 (save-deleted (substring rmail-deleted-vector 0 (1+ old-messages)))
+	 blurb)
+    ;; set all messages to undeleted
+    (setq rmail-deleted-vector (make-string (1+ rmail-total-messages) ?\ ))
+    (while (<= rsf-scanned-message-number rmail-total-messages)
+      (progn
+	(if (not (rmail-spam-filter rsf-scanned-message-number))
+	    (progn (setq rsf-number-of-spam (1+ rsf-number-of-spam))))
+	(setq rsf-scanned-message-number (1+ rsf-scanned-message-number))))
+    (if (> rsf-number-of-spam 0)
+	(progn
+	  (when (rmail-expunge-confirmed)
+	    (rmail-only-expunge t))))
+    (setq rmail-deleted-vector
+	  (concat save-deleted
+		  (make-string (- rmail-total-messages old-messages) ?\ )))
+    ;; Generate a return value message based on the number of spam
+    ;; messages found.
+    (cond
+     ((zerop rsf-number-of-spam) "")
+     ((= 1 new-message-count) ", and appears to be spam")
+     ((= rsf-number-of-spam new-message-count) ", and all appear to be spam")
+     ((> rsf-number-of-spam 1)
+      (format ", and %d appear to be spam" rsf-number-of-spam))
+     (t ", and 1 appears to be spam"))))
+
+(defun rmail-parse-url (file)
+  "Parse the supplied URL. Return (list MAILBOX-NAME REMOTE PASSWORD GOT-PASSWORD)
+WHERE MAILBOX-NAME is the name of the mailbox suitable as argument to the
+actual version of `movemail', REMOTE is non-nil if MAILBOX-NAME refers to
+a remote mailbox, PASSWORD is the password if it should be
+supplied as a separate argument to `movemail' or nil otherwise, GOT-PASSWORD
+is non-nil if the user has supplied the password interactively.
+"
+  (cond
+   ((string-match "^\\([^:]+\\)://\\(\\([^:@]+\\)\\(:\\([^@]+\\)\\)?@\\)?.*" file)
+      (let (got-password supplied-password
+	    (proto (match-string 1 file))
+	    (user  (match-string 3 file))
+	    (pass  (match-string 5 file))
+	    (host  (substring file (or (match-end 2)
+				       (+ 3 (match-end 1))))))
+
+	(if (not pass)
+	    (when rmail-remote-password-required
+	      (setq got-password (not (rmail-have-password)))
+	      (setq supplied-password (rmail-get-remote-password
+				       (string-equal proto "imap"))))
+	  ;; The password is embedded.  Strip it out since movemail
+	  ;; does not really like it, in spite of the movemail spec.
+	  (setq file (concat proto "://" user "@" host)))
+
+	(if (rmail-movemail-variant-p 'emacs)
+	    (if (string-equal proto "pop")
+		(list (concat "po:" user ":" host)
+		      t
+		      (or pass supplied-password)
+		      got-password)
+	      (error "Emacs movemail does not support %s protocol" proto))
+	  (list file
+		(or (string-equal proto "pop") (string-equal proto "imap"))
+		(or supplied-password pass)
+		got-password))))
+
+   ((string-match "^po:\\([^:]+\\)\\(:\\(.*\\)\\)?" file)
+    (let (got-password supplied-password
+          (proto "pop")
+	  (user  (match-string 1 file))
+	  (host  (match-string 3 file)))
+
+      (when rmail-remote-password-required
+	(setq got-password (not (rmail-have-password)))
+	(setq supplied-password (rmail-get-remote-password nil)))
+
+      (list file "pop" supplied-password got-password)))
+
+   (t
+    (list file nil nil nil))))
+
+(defun rmail-insert-inbox-text (files renamep)
+  ;; Detect a locked file now, so that we avoid moving mail
+  ;; out of the real inbox file.  (That could scare people.)
+  (or (memq (file-locked-p buffer-file-name) '(nil t))
+      (error "RMAIL file %s is locked"
+	     (file-name-nondirectory buffer-file-name)))
+  (let (file tofile delete-files movemail pormail got-password password)
+    (while files
+      ;; Handle remote mailbox names specially; don't expand as filenames
+      ;; in case the userid contains a directory separator.
+      (setq file (car files))
+      (let ((url-data (rmail-parse-url file)))
+	(setq file (nth 0 url-data))
+	(setq pormail (nth 1 url-data))
+	(setq password (nth 2 url-data))
+	(setq got-password (nth 3 url-data)))
+
+      (if pormail
+	  (setq renamep t)
+	(setq file (file-truename
+		    (substitute-in-file-name (expand-file-name file)))))
+      (setq tofile (expand-file-name
+		    ;; Generate name to move to from inbox name,
+		    ;; in case of multiple inboxes that need moving.
+		    (concat ".newmail-"
+			    (file-name-nondirectory
+			     (if (memq system-type '(windows-nt cygwin ms-dos))
+				 ;; cannot have colons in file name
+				 (replace-regexp-in-string ":" "-" file)
+			       file)))
+		    ;; Use the directory of this rmail file
+		    ;; because it's a nuisance to use the homedir
+		    ;; if that is on a full disk and this rmail
+		    ;; file isn't.
+		    (file-name-directory
+		     (expand-file-name buffer-file-name))))
+      ;; Always use movemail to rename the file,
+      ;; since there can be mailboxes in various directories.
+      (when (not pormail)
+	;; On some systems, /usr/spool/mail/foo is a directory
+	;; and the actual inbox is /usr/spool/mail/foo/foo.
+	(if (file-directory-p file)
+	    (setq file (expand-file-name (user-login-name)
+					 file))))
+      (cond (pormail
+	     (message "Getting mail from the remote server ..."))
+	    ((and (file-exists-p tofile)
+		  (/= 0 (nth 7 (file-attributes tofile))))
+	     (message "Getting mail from %s..." tofile))
+	    ((and (file-exists-p file)
+		  (/= 0 (nth 7 (file-attributes file))))
+	     (message "Getting mail from %s..." file)))
+      ;; Set TOFILE if have not already done so, and
+      ;; rename or copy the file FILE to TOFILE if and as appropriate.
+      (cond ((not renamep)
+	     (setq tofile file))
+	    ((or (file-exists-p tofile) (and (not pormail)
+					     (not (file-exists-p file))))
+	     nil)
+	    (t
+	     (with-temp-buffer
+	       (let ((errors (current-buffer)))
+		 (buffer-disable-undo errors)
+		 (let ((args
+			(append
+			 (list (or rmail-movemail-program "movemail") nil errors nil)
+			 (if rmail-preserve-inbox
+			     (list "-p")
+			   nil)
+			 (if (rmail-movemail-variant-p 'mailutils)
+			     (append (list "--emacs") rmail-movemail-flags)
+			   rmail-movemail-flags)
+			 (list file tofile)
+			 (if password (list password) nil))))
+		   (apply 'call-process args))
+		 (if (not (buffer-modified-p errors))
+		     ;; No output => movemail won
+		     nil
+		   (set-buffer errors)
+		   (subst-char-in-region (point-min) (point-max)
+					 ?\n ?\  )
+		   (goto-char (point-max))
+		   (skip-chars-backward " \t")
+		   (delete-region (point) (point-max))
+		   (goto-char (point-min))
+		   (if (looking-at "movemail: ")
+		       (delete-region (point-min) (match-end 0)))
+		   (beep t)
+		   ;; If we just read the password, most likely it is
+		   ;; wrong.  Otherwise, see if there is a specific
+		   ;; reason to think that the problem is a wrong passwd.
+		   (if (or got-password
+			   (re-search-forward rmail-remote-password-error
+					      nil t))
+		       (rmail-set-remote-password nil))
+
+		   ;; If using Mailutils, remove initial error code
+		   ;; abbreviation
+		   (when (rmail-movemail-variant-p 'mailutils)
+		     (goto-char (point-min))
+		     (when (looking-at "[A-Z][A-Z0-9_]*:")
+		       (delete-region (point-min) (match-end 0))))
+
+		   (message "movemail: %s"
+			    (buffer-substring (point-min)
+					      (point-max)))
+
+		   (sit-for 3)
+		   nil)))))
+
+      ;; At this point, TOFILE contains the name to read:
+      ;; Either the alternate name (if we renamed)
+      ;; or the actual inbox (if not renaming).
+      (if (file-exists-p tofile)
+	  (let ((coding-system-for-read 'no-conversion)
+		size)
+	    (goto-char (point-max))
+	    (setq size (nth 1 (insert-file-contents tofile)))
+	    ;; Determine if a pair of newline message separators need
+	    ;; to be added to the new collection of messages.  This is
+	    ;; the case for all new message collections added to a
+	    ;; non-empty mail file.
+	    (unless (zerop size)
+	      (save-restriction
+		(let ((start (point-min)))
+		  (widen)
+		  (unless (eq start (point-min))
+		    (goto-char start)
+		    (insert "\n\n")
+		    (setq size (+ 2 size))))))
+	    (goto-char (point-max))
+	    (or (= (preceding-char) ?\n)
+		(zerop size)
+		(insert ?\n))
+	    (if (not (and rmail-preserve-inbox (string= file tofile)))
+		(setq delete-files (cons tofile delete-files)))))
+      (message "")
+      (setq files (cdr files)))
+    delete-files))
+
+;; Decode the region specified by FROM and TO by CODING.
+;; If CODING is nil or an invalid coding system, decode by `undecided'.
+(defun rmail-decode-region (from to coding &optional destination)
+  (if (or (not coding) (not (coding-system-p coding)))
+      (setq coding 'undecided))
+  ;; Use -dos decoding, to remove ^M characters left from base64 or
+  ;; rogue qp-encoded text.
+  (decode-coding-region
+   from to (coding-system-change-eol-conversion coding 1) destination)
+  ;; Don't reveal the fact we used -dos decoding, as users generally
+  ;; will not expect the RMAIL buffer to use DOS EOL format.
+  (setq buffer-file-coding-system
+	(setq last-coding-system-used
+	      (coding-system-change-eol-conversion coding 0))))
+
+(defun rmail-add-mbox-headers ()
+  "Validate the RFC2822 format for the new messages.
+Point should be at the first new message.
+An error is signalled if the new messages are not RFC2822
+compliant.
+Unless an Rmail attribute header already exists, add it to the
+new messages.  Return the number of new messages."
+  (save-excursion
+    (save-restriction
+      (let ((count 0)
+	    (start (point))
+	    (value "------U-")
+	    limit)
+	;; Detect an empty inbox file.
+	(unless (= start (point-max))
+	  ;; Scan the new messages to establish a count and to insure that
+	  ;; an attribute header is present.
+	  (while (looking-at "From ")
+	    ;; Determine if a new attribute header needs to be added to
+	    ;; the message.
+	    (if (search-forward "\n\n" nil t)
+		(progn
+		  (setq count (1+ count))
+		  (narrow-to-region start (point))
+		  (unless (mail-fetch-field rmail-attribute-header)
+		    (backward-char 1)
+		    (insert rmail-attribute-header ": " value "\n"))
+		  (widen))
+	      (rmail-error-bad-format))
+	    ;; Move to the next message.
+	    (if (search-forward "\n\nFrom " nil 'move)
+		(forward-char -5))
+	    (setq start (point))))
+	count))))
+
+(defun rmail-get-header (name &optional msgnum)
+  "Return the value of message header NAME, nil if it has none.
+MSGNUM specifies the message number to get it from.
+If MSGNUM is nil, use the current message."
+  (with-current-buffer rmail-buffer
+    (or msgnum (setq msgnum rmail-current-message))
+    (when (> msgnum 0)
+      (let (msgbeg end)
+	(setq msgbeg (rmail-msgbeg msgnum))
+	;; All access to the buffer's local variables is now finished...
+	(save-excursion
+	  ;; ... so it is ok to go to a different buffer.
+	  (if (rmail-buffers-swapped-p) (set-buffer rmail-view-buffer))
+	  (save-restriction
+	    (widen)
+	    (save-excursion
+	      (goto-char msgbeg)
+	      (setq end (search-forward "\n\n" nil t))
+	      (if end
+		  (progn
+		    (narrow-to-region msgbeg end)
+		    (mail-fetch-field name))
+		(rmail-error-bad-format msgnum)))))))))
+
+(defun rmail-set-header (name &optional msgnum value)
+  "Store VALUE in message header NAME, nil if it has none.
+MSGNUM specifies the message number to operate on.
+If MSGNUM is nil, use the current message."
+  (with-current-buffer rmail-buffer
+    (or msgnum (setq msgnum rmail-current-message))
+    (when (> msgnum 0)
+      (let (msgbeg end)
+	(setq msgbeg (rmail-msgbeg msgnum))
+	;; All access to the buffer's local variables is now finished...
+	(save-excursion
+	  ;; ... so it is ok to go to a different buffer.
+	  (if (rmail-buffers-swapped-p) (set-buffer rmail-view-buffer))
+	  (save-restriction
+	    (widen)
+	    (save-excursion
+	      (goto-char msgbeg)
+	      (setq end (search-forward "\n\n" nil t))
+	      (if end (setq end (1- end)))
+	      (if end
+		  (progn
+		    (narrow-to-region msgbeg end)
+		    (goto-char msgbeg)
+		    (if (re-search-forward (concat "^"
+						   (regexp-quote name)
+						   ":")
+					   nil t)
+			(progn
+			  (delete-region (point) (line-end-position))
+			  (insert " " value))
+		      (goto-char end)
+		      (insert name ": " value "\n")))
+		(rmail-error-bad-format msgnum)))))))))
+
+;;;; *** Rmail Attributes and Keywords ***
+
+(defun rmail-get-attr-names (&optional msg)
+  "Return the message attributes in a comma separated string.
+MSG specifies the message number to get it from.
+If MSG is nil, use the current message."
+  (let ((value (rmail-get-header rmail-attribute-header msg))
+	result temp)
+    (dotimes (index (length value))
+      (setq temp (and (not (= ?- (aref value index)))
+		      (nth 1 (aref rmail-attr-array index)))
+	    result
+	    (cond
+	     ((and temp result) (format "%s, %s" result temp))
+	     (temp temp)
+	     (t result))))
+    result))
+
+(defun rmail-get-keywords (&optional msg)
+  "Return the message keywords in a comma separated string.
+MSG, if non-nil, identifies the message number to use.
+If nil, that means the current message."
+  (rmail-get-header rmail-keyword-header msg))
+
+(defun rmail-get-labels (&optional msg)
+  "Return a string with the labels (attributes and keywords) of msg MSG.
+It is put in comma-separated form.
+MSG, if non-nil, identifies the message number to use.
+If nil, that means the current message."
+  (let (blurb attr-names keywords)
+    ;; Combine the message attributes and keywords 
+    ;; into a comma-separated list.
+    (setq attr-names (rmail-get-attr-names rmail-current-message)
+	  keywords (rmail-get-keywords rmail-current-message))
+    (if (string= keywords "")
+	(setq keywords nil))
+    (cond
+     ((and attr-names keywords) (concat " " attr-names ", " keywords))
+     (attr-names (concat " " attr-names))
+     (keywords (concat " " keywords))
+     (t ""))))
+
+(defun rmail-display-labels ()
+  "Update the current messages's attributes and keywords in mode line."
+  (let ((blurb (rmail-get-labels)))
+    (setq mode-line-process
+	  (format " %d/%d%s"
+		  rmail-current-message rmail-total-messages blurb))
+    ;; If rmail-enable-mime is non-nil, we may have to update
+    ;; `mode-line-process' of rmail-view-buffer too.
+    (if (and rmail-enable-mime
+	     (not (eq (current-buffer) rmail-view-buffer))
+	     (buffer-live-p rmail-view-buffer))
+	(let ((mlp mode-line-process))
+	  (with-current-buffer rmail-view-buffer
+	    (setq mode-line-process mlp))))))
+
+(defun rmail-get-attr-value (attr state)
+  "Return the character value for ATTR.
+ATTR is a (numeric) index, an offset into the mbox attribute
+header value. STATE is one of nil, t, or a character value."
+  (cond
+   ((numberp state) state)
+   ((not state) ?-)
+   (t (nth 0 (aref rmail-attr-array attr)))))
+
+(defun rmail-set-attribute (attr state &optional msgnum)
+  "Turn an attribute of a message on or off according to STATE.
+STATE is either nil or the character (numeric) value associated
+with the state (nil represents off and non-nil represents on).
+ATTR is the index of the attribute.  MSGNUM is message number to
+change; nil means current message."
+  (with-current-buffer rmail-buffer
+    (let ((value (rmail-get-attr-value attr state))
+	  (inhibit-read-only t)
+	  limit
+	  altered
+	  msgbeg)
+      (or msgnum (setq msgnum rmail-current-message))
+      (when (> msgnum 0)
+	;; The "deleted" attribute is also stored in a special vector
+	;; so update that too.
+	(if (= attr rmail-deleted-attr-index)
+	    (rmail-set-message-deleted-p msgnum state))
+	(setq msgbeg (rmail-msgbeg msgnum))
+
+	;; All access to the buffer's local variables is now finished...
+	(unwind-protect
+	    (save-excursion
+	      ;; ... so it is ok to go to a different buffer.
+	      (if (rmail-buffers-swapped-p) (set-buffer rmail-view-buffer))
+	      (save-restriction
+		(widen)
+		(save-excursion
+		  ;; Determine if the current state is the desired state.
+		  (goto-char msgbeg)
+		  (save-excursion
+		    (setq limit (search-forward "\n\n" nil t)))
+		  (if (search-forward (concat rmail-attribute-header ": ") limit t)
+		      ;; If this message already records attributes,
+		      ;; just change the value for this one.
+		      (let ((missing (- (+ (point) attr) (line-end-position))))
+			;; Position point at this  attribute,
+			;; adding attributes if necessary.
+			(if (> missing 0)
+			    (progn
+			      (end-of-line)
+			      (insert-char ?- missing)
+			      (backward-char 1))
+			  (forward-char attr))
+			;; Change this attribute.
+			(when (/= value (char-after))
+			  (setq altered t)
+			  (delete-char 1)
+			  (insert value)))
+		    ;; Otherwise add a header line to record the attributes
+		    ;; and set all but this one to no.
+		    (let ((header-value "--------"))
+		      (aset header-value attr value)
+		      (goto-char (if limit (- limit 1) (point-max)))
+		      (setq altered (/= value ?-))
+		      (insert rmail-attribute-header ": " header-value "\n"))))))
+	  (if (= msgnum rmail-current-message)
+	      (rmail-display-labels))))
+      ;; If we made a significant change in an attribute,
+      ;; mark rmail-buffer modified, so it will be (1) saved 
+      ;; and (2) displayed in the mode line.
+      (if altered
+	  (set-buffer-modified-p t)))))
+
+(defun rmail-message-attr-p (msg attrs)
+  "Return t if the attributes header for message MSG matches regexp ATTRS.
+This function assumes the Rmail buffer is unswapped."
+  (save-excursion
+    (save-restriction
+      (let ((start (rmail-msgbeg msg))
+	    limit)
+	(widen)
+	(goto-char start)
+	(setq limit (search-forward "\n\n" (rmail-msgend msg) t))
+	(goto-char start)
+	(and limit
+	     (search-forward (concat rmail-attribute-header ": ") limit t)
+	     (looking-at attrs))))))
+
+(defun rmail-message-unseen-p (msgnum)
+  "Test the unseen attribute for message MSGNUM.
+Return non-nil if the unseen attribute is set, nil otherwise."
+  (rmail-message-attr-p msgnum "......U"))
+
+;; Return t if the attributes/keywords line of msg number MSG
+;; contains a match for the regexp LABELS.
+(defun rmail-message-labels-p (msg labels)
+  (string-match labels (rmail-get-labels msg)))
+
+;;;; *** Rmail Message Selection And Support ***
+
+(defun rmail-msgend (n)
+  (marker-position (aref rmail-message-vector (1+ n))))
+
+(defun rmail-msgbeg (n)
+  (marker-position (aref rmail-message-vector n)))
+
+(defun rmail-apply-in-message (msgnum function &rest args)
+  "Call FUNCTION on ARGS while narrowed to message MSGNUM.
+Point is at the start of the message.
+This returns what the call to FUNCTION returns.
+If MSGNUM is nil, use the current message."
+  (with-current-buffer rmail-buffer
+    (or msgnum (setq msgnum rmail-current-message))
+    (when (> msgnum 0)
+      (let (msgbeg msgend)
+	(setq msgbeg (rmail-msgbeg msgnum))
+	(setq msgend (rmail-msgend msgnum))
+	;; All access to the rmail-buffer's local variables is now finished...
+	(save-excursion
+	  ;; ... so it is ok to go to a different buffer.
+	  (if (rmail-buffers-swapped-p) (set-buffer rmail-view-buffer))
+	  (save-restriction
+	    (widen)
+	    (save-excursion
+	      (goto-char msgbeg)
+	      (save-restriction
+		(narrow-to-region msgbeg msgend)
+		(apply function args)))))))))
+
+(defun rmail-widen-to-current-msgbeg (function)
+  "Call FUNCTION with point at start of internal data of current message.
+Assumes that bounds were previously narrowed to display the message in Rmail.
+The bounds are widened enough to move point where desired, then narrowed
+again afterward.
+
+FUNCTION may not change the visible text of the message, but it may
+change the invisible header text."
+  (save-excursion
+    (unwind-protect
+	(progn
+	  (narrow-to-region (rmail-msgbeg rmail-current-message)
+			    (point-max))
+	  (goto-char (point-min))
+	  (funcall function))
+	;; Note: we don't use save-restriction because that does not work right
+	;; if changes are made outside the saved restriction
+	;; before that restriction is restored.
+      (narrow-to-region (rmail-msgbeg rmail-current-message)
+			(rmail-msgend rmail-current-message)))))
+
+;; Manage the message vectors and counters.
+
+(defun rmail-forget-messages ()
+  (unwind-protect
+      (if (vectorp rmail-message-vector)
+	  (let* ((i 0)
+		 (v rmail-message-vector)
+		 (n (length v)))
+	    (while (< i n)
+	      (move-marker (aref v i)  nil)
+	      (setq i (1+ i)))))
+    (setq rmail-message-vector nil)
+    (setq rmail-msgref-vector nil)
+    (setq rmail-deleted-vector nil)))
+
+(defun rmail-maybe-set-message-counters ()
+  (if (not (and rmail-deleted-vector
+		rmail-message-vector
+		rmail-current-message
+		rmail-total-messages))
+      (rmail-set-message-counters)))
+
+(defun rmail-count-new-messages (&optional nomsg)
+  "Count the number of new messages.
+The buffer should be narrowed to include only the new messages.
+Output a helpful message unless NOMSG is non-nil."
+  (let* ((case-fold-search nil)
+	 (total-messages 0)
+	 (messages-head nil)
+	 (deleted-head nil))
+    (or nomsg (message "Counting new messages..."))
+    (goto-char (point-max))
+    ;; Put at the end of messages-head
+    ;; the entry for message N+1, which marks
+    ;; the end of message N.  (N = number of messages).
+    (setq messages-head (list (point-marker)))
+    (rmail-set-message-counters-counter (point-min))
+    (setq rmail-current-message (1+ rmail-total-messages))
+    (setq rmail-total-messages
+	  (+ rmail-total-messages total-messages))
+    (setq rmail-message-vector
+	  (vconcat rmail-message-vector (cdr messages-head)))
+    (aset rmail-message-vector
+	  rmail-current-message (car messages-head))
+    (setq rmail-deleted-vector
+	  (concat rmail-deleted-vector deleted-head))
+    (setq rmail-summary-vector
+	  (vconcat rmail-summary-vector (make-vector total-messages nil)))
+    (setq rmail-msgref-vector
+	  (vconcat rmail-msgref-vector (make-vector total-messages nil)))
+    ;; Fill in the new elements of rmail-msgref-vector.
+    (let ((i (1+ (- rmail-total-messages total-messages))))
+      (while (<= i rmail-total-messages)
+	(aset rmail-msgref-vector i (list i))
+	(setq i (1+ i))))
+    (goto-char (point-min))
+    (or nomsg (message "Counting new messages...done (%d)" total-messages))))
+
+(defun rmail-set-message-counters ()
+  (rmail-forget-messages)
+  (save-excursion
+    (save-restriction
+      (widen)
+      (let* ((point-save (point))
+	     (total-messages 0)
+	     (messages-after-point)
+	     (case-fold-search nil)
+	     (messages-head nil)
+	     (deleted-head nil))
+	;; Determine how many messages follow point.
+	(message "Counting messages...")
+	(goto-char (point-max))
+	;; Put at the end of messages-head
+	;; the entry for message N+1, which marks
+	;; the end of message N.  (N = number of messages).
+	(setq messages-head (list (point-marker)))
+	(rmail-set-message-counters-counter (min (point) point-save))
+	(setq messages-after-point total-messages)
+
+	;; Determine how many precede point.
+	(rmail-set-message-counters-counter)
+	(setq rmail-total-messages total-messages)
+	(setq rmail-current-message
+	      (min total-messages
+		   (max 1 (- total-messages messages-after-point))))
+	(setq rmail-message-vector
+	      (apply 'vector (cons (point-min-marker) messages-head))
+	      rmail-deleted-vector (concat "0" deleted-head)
+	      rmail-summary-vector (make-vector rmail-total-messages nil)
+	      rmail-msgref-vector (make-vector (1+ rmail-total-messages) nil))
+	(let ((i 0))
+	  (while (<= i rmail-total-messages)
+	    (aset rmail-msgref-vector i (list i))
+	    (setq i (1+ i))))
+	(let ((i 0))
+	  (while (<= i rmail-total-messages)
+	    (rmail-set-message-deleted-p i (rmail-message-attr-p i ".D"))
+	    (setq i (1+ i))))
+	(message "Counting messages...done")))))
+
+
+(defsubst rmail-collect-deleted (message-end)
+  "Collect the message deletion flags for each message.
+MESSAGE-END is the buffer position corresponding to the end of
+the message.  Point is at the beginning of the message."
+  ;; NOTE: This piece of code will be executed on a per-message basis.
+  ;; In the face of thousands of messages, it has to be as fast as
+  ;; possible, hence some brute force constant use is employed in
+  ;; addition to inlining.
+  (save-excursion
+    (setq deleted-head
+	  (cons (if (and (search-forward (concat rmail-attribute-header ": ") message-end t)
+			 (looking-at "?D"))
+		    ?D
+		  ?\ ) deleted-head))))
+
+(defun rmail-set-message-counters-counter (&optional stop)
+  ;; Collect the start position for each message into 'messages-head.
+  (let ((start (point)))
+    (while (search-backward "\n\nFrom " stop t)
+      (forward-char 2)
+      (rmail-collect-deleted start)
+      (setq messages-head (cons (point-marker) messages-head)
+	    total-messages (1+ total-messages)
+	    start (point))
+      ;; Show progress after every 20 messages or so.
+      (if (zerop (% total-messages 20))
+	  (message "Counting messages...%d" total-messages)))
+    ;; Handle the first message, maybe.
+    (if stop
+	(goto-char stop)
+      (goto-char (point-min)))
+    (unless (not (looking-at "From "))
+      (rmail-collect-deleted start)
+      (setq messages-head (cons (point-marker) messages-head)
+	    total-messages (1+ total-messages)))))
+
+;; Display a message.
+
+;;;; *** Rmail Message Formatting and Header Manipulation ***
+
+(defun rmail-toggle-header (&optional arg)
+  "Show original message header if pruned header currently shown, or vice versa.
+With argument ARG, show the message header pruned if ARG is greater than zero;
+otherwise, show it in full."
+  (interactive "P")
+  (setq rmail-header-style
+	(cond
+	 ((and (numberp arg) (> arg 0)) 'normal)
+	 ((eq rmail-header-style 'full) 'normal)
+	 (t 'full)))
+  (rmail-show-message-maybe))
+
+(defun rmail-beginning-of-message ()
+  "Show current message starting from the beginning."
+  (interactive)
+  (let ((rmail-show-message-hook
+	 (list (function (lambda ()
+			   (goto-char (point-min)))))))
+    (rmail-show-message-maybe rmail-current-message)))
+
+(defun rmail-end-of-message ()
+  "Show bottom of current message."
+  (interactive)
+  (let ((rmail-show-message-hook
+	 (list (function (lambda ()
+			   (goto-char (point-max))
+			   (recenter (1- (window-height))))))))
+    (rmail-show-message-maybe rmail-current-message)))
+
+(defun rmail-unknown-mail-followup-to ()
+  "Handle a \"Mail-Followup-To\" header field with an unknown mailing list.
+Ask the user whether to add that list name to `mail-mailing-lists'."
+   (save-restriction
+     (let ((mail-followup-to (mail-fetch-field "mail-followup-to" nil t)))
+       (when mail-followup-to
+	 (let ((addresses
+		(split-string
+		 (mail-strip-quoted-names mail-followup-to)
+		 ",[[:space:]]+" t)))
+	   (dolist (addr addresses)
+	     (when (and (not (member addr mail-mailing-lists))
+			(not
+			 ;; taken from rmailsum.el
+			 (string-match
+			  (or rmail-user-mail-address-regexp
+			      (concat "^\\("
+				      (regexp-quote (user-login-name))
+				      "\\($\\|@\\)\\|"
+				      (regexp-quote
+				       (or user-mail-address
+					   (concat (user-login-name) "@"
+						   (or mail-host-address
+						       (system-name)))))
+				      "\\>\\)"))
+			  addr))
+			(y-or-n-p
+			 (format "Add `%s' to `mail-mailing-lists'? "
+				 addr)))
+	       (customize-save-variable 'mail-mailing-lists
+					(cons addr mail-mailing-lists)))))))))
+
+(defun rmail-widen ()
+  "Display the entire mailbox file."
+  (interactive)
+  (rmail-swap-buffers-maybe)
+  (widen))
+
+(defun rmail-show-message-maybe (&optional n no-summary)
+  "Show message number N (prefix argument), counting from start of file.
+If summary buffer is currently displayed, update current message there also."
+  (interactive "p")
+  (or (eq major-mode 'rmail-mode)
+      (switch-to-buffer rmail-buffer))
+  (rmail-swap-buffers-maybe)
+  (rmail-maybe-set-message-counters)
+  (widen)
+  (let ((msgnum (or n rmail-current-message))
+	blurb)
+    (if (zerop rmail-total-messages)
+	(save-excursion
+	  (with-current-buffer rmail-view-buffer
+	    (erase-buffer)
+	    (setq blurb "No mail.")))
+      (setq blurb (rmail-show-message msgnum))
+      (when mail-mailing-lists
+	(rmail-unknown-mail-followup-to))
+      (if transient-mark-mode (deactivate-mark))
+      ;; If there is a summary buffer, try to move to this message
+      ;; in that buffer.  But don't complain if this message
+      ;; is not mentioned in the summary.
+      ;; Don't do this at all if we were called on behalf
+      ;; of cursor motion in the summary buffer.
+      (and (rmail-summary-exists) (not no-summary)
+	   (let ((curr-msg rmail-current-message))
+	     (rmail-select-summary
+	      (rmail-summary-goto-msg curr-msg t t))))
+      (with-current-buffer rmail-buffer
+	(rmail-auto-file)))
+    (if blurb
+	(message blurb))))
+
+(defun rmail-is-text-p ()
+  "Return t if the region contains a text message, nil otherwise."
+  (save-excursion
+    (let ((text-regexp "\\(text\\|message\\)/")
+	  (content-type-header (mail-fetch-field "content-type")))
+      ;; The message is text if either there is no content type header
+      ;; (a default of "text/plain; charset=US-ASCII" is assumed) or
+      ;; the base content type is either text or message.
+      (or (not content-type-header)
+	  (string-match text-regexp content-type-header)))))
+
+(defun rmail-show-message (&optional msg)
+  "Show message MSG using a special view buffer.
+Return text to display in the minibuffer if MSG is out of
+range (displaying a reasonable choice as well), nil otherwise.
+The current mail message becomes the message displayed."
+  (let ((mbox-buf rmail-buffer)
+	(view-buf rmail-view-buffer)
+	blurb beg end body-start coding-system character-coding is-text-message)
+    (if (not msg)
+	(setq msg rmail-current-message))
+    (cond ((<= msg 0)
+	   (setq msg 1
+		 rmail-current-message 1
+		 blurb "No previous message"))
+	  ((> msg rmail-total-messages)
+	   (setq msg rmail-total-messages
+		 rmail-current-message rmail-total-messages
+		 blurb "No following message"))
+	  (t (setq rmail-current-message msg)))
+    (with-current-buffer rmail-buffer
+      ;; Mark the message as seen, bracket the message in the mail
+      ;; buffer and determine the coding system the transfer encoding.
+      (rmail-set-attribute rmail-unseen-attr-index nil)
+      (rmail-swap-buffers-maybe)
+      (setq beg (rmail-msgbeg msg)
+	    end (rmail-msgend msg))
+      (narrow-to-region beg end)
+      (goto-char beg)
+      (setq body-start (search-forward "\n\n" nil t))
+      (narrow-to-region beg (point))
+      (goto-char beg)
+      (setq character-coding (mail-fetch-field "content-transfer-encoding")
+	    is-text-message (rmail-is-text-p)
+	    coding-system (rmail-get-coding-system))
+      (if character-coding
+	  (setq character-coding (downcase character-coding)))
+      (narrow-to-region beg end)
+      ;; Decode the message body into an empty view buffer using a
+      ;; unibyte temporary buffer where the character decoding takes
+      ;; place.
+      (with-current-buffer rmail-view-buffer
+	(erase-buffer))
+      (if (null character-coding)
+	  ;; Do it directly since that is fast.
+	  (rmail-decode-region body-start end coding-system view-buf)
+	;; Can this be done directly, skipping the temp buffer?
+	(with-temp-buffer
+	  (set-buffer-multibyte nil)
+	  (insert-buffer-substring mbox-buf body-start end)
+	  (cond
+	   ((string= character-coding "quoted-printable")
+	    (mail-unquote-printable-region (point-min) (point-max)))
+	   ((and (string= character-coding "base64") is-text-message)
+	    (base64-decode-region (point-min) (point-max)))
+	   ((eq character-coding 'uuencode)
+	    (error "Not supported yet"))
+	   (t))
+	  (rmail-decode-region (point-min) (point-max)
+			       coding-system view-buf)))
+      ;; Copy the headers to the front of the message view buffer.
+      (with-current-buffer rmail-view-buffer
+	(goto-char (point-min)))
+      (rmail-copy-headers beg end)
+      ;; Add the separator (blank line) between headers and body;
+      ;; highlight the message, activate any URL like text and add
+      ;; special highlighting for and quoted material.
+      (with-current-buffer rmail-view-buffer
+	(insert "\n")
+	(goto-char (point-min))
+	(rmail-highlight-headers)
+	;(rmail-activate-urls)
+	;(rmail-process-quoted-material)
+	)
+      ;; Update the mode-line with message status information and swap
+      ;; the view buffer/mail buffer contents.
+      (rmail-display-labels)
+      (let ((modp (buffer-modified-p)))
+	(buffer-swap-text rmail-view-buffer)
+	(set-buffer-modified-p modp))
+      (setq rmail-buffer-swapped t)
+      (run-hooks 'rmail-show-message-hook))
+    blurb))
+
+(defun rmail-copy-headers (beg end &optional ignored-headers)
+  "Copy displayed header fields to the message viewer buffer.
+BEG and END marks the start and end positions of the message in
+the mbox buffer.  If the optional argument IGNORED-HEADERS is
+non-nil, ignore all header fields whose names match that regexp.
+Otherwise, if `rmail-displayed-headers' is non-nil, copy only
+those header fields whose names match that regexp.  Otherwise,
+copy all header fields whose names do not match
+`rmail-ignored-headers' (unless they also match
+`rmail-nonignored-headers')."
+  (let ((header-start-regexp "\n[^ \t]")
+	lim)
+    (with-current-buffer rmail-buffer
+      (when (search-forward "\n\n" nil t)
+	(forward-char -1)
+	(save-restriction
+	  ;; Put point right after the From header line.
+	  (narrow-to-region beg (point))
+	  (goto-char (point-min))
+	  (unless (re-search-forward header-start-regexp nil t)
+	    (rmail-error-bad-format))
+	  (forward-char -1)
+	  (cond
+	   ;; Handle the case where all headers should be copied.
+	   ((eq rmail-header-style 'full)
+	    (prepend-to-buffer rmail-view-buffer beg (point-max)))
+	   ;; Handle the case where the headers matching the diplayed
+	   ;; headers regexp should be copied.
+	   ((and rmail-displayed-headers (null ignored-headers))
+	    (while (not (eobp))
+	      (save-excursion
+		(setq lim (if (re-search-forward header-start-regexp nil t)
+			      (1+ (match-beginning 0))
+			    (point-max))))
+	      (when (looking-at rmail-displayed-headers)
+		(append-to-buffer rmail-view-buffer (point) lim))
+	      (goto-char lim)))
+	   ;; Handle the ignored headers.
+	   ((or ignored-headers (setq ignored-headers rmail-ignored-headers))
+	    (while (and ignored-headers (not (eobp)))
+	      (save-excursion
+		(setq lim (if (re-search-forward header-start-regexp nil t)
+			      (1+ (match-beginning 0))
+			    (point-max))))
+	      (if (and (looking-at ignored-headers)
+		       (not (looking-at rmail-nonignored-headers)))
+		  (goto-char lim)
+		(append-to-buffer rmail-view-buffer (point) lim)
+		(goto-char lim))))
+	   (t (error "No headers selected for display!"))))))))
+
+;; Find all occurrences of certain fields, and highlight them.
+(defun rmail-highlight-headers ()
+  ;; Do this only if the system supports faces.
+  (if (and (fboundp 'internal-find-face)
+	   rmail-highlighted-headers)
+      (save-excursion
+	(search-forward "\n\n" nil 'move)
+	(save-restriction
+	  (narrow-to-region (point-min) (point))
+	  (let ((case-fold-search t)
+		(inhibit-read-only t)
+		;; Highlight with boldface if that is available.
+		;; Otherwise use the `highlight' face.
+		(face (or 'rmail-highlight
+			  (if (face-differs-from-default-p 'bold)
+			      'bold 'highlight)))
+		;; List of overlays to reuse.
+		(overlays rmail-overlay-list))
+	    (goto-char (point-min))
+	    (while (re-search-forward rmail-highlighted-headers nil t)
+	      (skip-chars-forward " \t")
+	      (let ((beg (point))
+		    overlay)
+		(while (progn (forward-line 1)
+			      (looking-at "[ \t]")))
+		;; Back up over newline, then trailing spaces or tabs
+		(forward-char -1)
+		(while (member (preceding-char) '(?  ?\t))
+		  (forward-char -1))
+		(if overlays
+		    ;; Reuse an overlay we already have.
+		    (progn
+		      (setq overlay (car overlays)
+			    overlays (cdr overlays))
+		      (overlay-put overlay 'face face)
+		      (move-overlay overlay beg (point)))
+		  ;; Make a new overlay and add it to
+		  ;; rmail-overlay-list.
+		  (setq overlay (make-overlay beg (point)))
+		  (overlay-put overlay 'face face)
+		  (setq rmail-overlay-list
+			(cons overlay rmail-overlay-list))))))))))
+
+(defun rmail-auto-file ()
+  "Automatically move a message into a sub-folder based on criteria.
+Called when a new message is displayed."
+  (if (or (zerop rmail-total-messages)
+	  (rmail-message-attr-p rmail-current-message "...F")
+	  (not (string= (buffer-file-name)
+			(expand-file-name rmail-file-name))))
+      ;; Do nothing if the message has already been filed or if there
+      ;; are no messages.
+      nil
+    ;; Find out some basics (common fields)
+    (let ((from (mail-fetch-field "from"))
+	  (subj (mail-fetch-field "subject"))
+	  (to   (concat (mail-fetch-field "to") "," (mail-fetch-field "cc")))
+	  (d rmail-automatic-folder-directives)
+	  (directive-loop nil)
+	  (folder nil))
+      (while d
+	(setq folder (car (car d))
+	      directive-loop (cdr (car d)))
+	(while (and (car directive-loop)
+		    (let ((f (cond
+			      ((string= (car directive-loop) "from") from)
+			      ((string= (car directive-loop) "to") to)
+			      ((string= (car directive-loop) "subject") subj)
+			      (t (mail-fetch-field (car directive-loop))))))
+		      (and f (string-match (car (cdr directive-loop)) f))))
+	  (setq directive-loop (cdr (cdr directive-loop))))
+	;; If there are no directives left, then it was a complete match.
+	(if (null directive-loop)
+	    (if (null folder)
+		(rmail-delete-forward)
+	      (if (string= "/dev/null" folder)
+		  (rmail-delete-message)
+		(rmail-output folder 1 t)
+		(setq d nil))))
+	(setq d (cdr d))))))
+
+;; Simple message motion commands.
+
+(defun rmail-next-message (n)
+  "Show following message whether deleted or not.
+With prefix arg N, moves forward N messages, or backward if N is negative."
+  (interactive "p")
+  (set-buffer rmail-buffer)
+  (rmail-maybe-set-message-counters)
+  (rmail-show-message-maybe (+ rmail-current-message n)))
+
+(defun rmail-previous-message (n)
+  "Show previous message whether deleted or not.
+With prefix arg N, moves backward N messages, or forward if N is negative."
+  (interactive "p")
+  (rmail-next-message (- n)))
+
+(defun rmail-next-undeleted-message (n)
+  "Show following non-deleted message.
+With prefix arg N, moves forward N non-deleted messages,
+or backward if N is negative.
+
+Returns t if a new message is being shown, nil otherwise."
+  (interactive "p")
+  (set-buffer rmail-buffer)
+  (rmail-maybe-set-message-counters)
+  (let ((lastwin rmail-current-message)
+	(current rmail-current-message))
+    (while (and (> n 0) (< current rmail-total-messages))
+      (setq current (1+ current))
+      (if (not (rmail-message-deleted-p current))
+	  (setq lastwin current n (1- n))))
+    (while (and (< n 0) (> current 1))
+      (setq current (1- current))
+      (if (not (rmail-message-deleted-p current))
+	  (setq lastwin current n (1+ n))))
+    (if (/= lastwin rmail-current-message)
+ 	(progn (rmail-show-message-maybe lastwin)
+ 	       t)
+      (if (< n 0)
+	  (message "No previous nondeleted message"))
+      (if (> n 0)
+	  (message "No following nondeleted message"))
+      nil)))
+
+(defun rmail-previous-undeleted-message (n)
+  "Show previous non-deleted message.
+With prefix argument N, moves backward N non-deleted messages,
+or forward if N is negative."
+  (interactive "p")
+  (rmail-next-undeleted-message (- n)))
+
+(defun rmail-first-message ()
+  "Show first message in file."
+  (interactive)
+  (rmail-maybe-set-message-counters)
+  (rmail-show-message-maybe (< 1 rmail-total-messages)))
+
+(defun rmail-last-message ()
+  "Show last message in file."
+  (interactive)
+  (rmail-maybe-set-message-counters)
+  (rmail-show-message-maybe rmail-total-messages))
+
+(defun rmail-what-message ()
+  "For debugging Rmail: find the message number that point is in."
+  (let ((where (point))
+	(low 1)
+	(high rmail-total-messages)
+	(mid (/ rmail-total-messages 2)))
+    (while (> (- high low) 1)
+      (if (>= where (rmail-msgbeg mid))
+	  (setq low mid)
+	(setq high mid))
+      (setq mid (+ low (/ (- high low) 2))))
+    (if (>= where (rmail-msgbeg high)) high low)))
+
+;; Searching in Rmail file.
+
+(defun rmail-search-message (msg regexp)
+  "Return non-nil, if for message number MSG, regexp REGEXP matches."
+  ;; This is adequate because its only caller, rmail-search,
+  ;; unswaps the buffers.
+  (goto-char (rmail-msgbeg msg))
+  (if rmail-enable-mime
+      (funcall rmail-search-mime-message-function msg regexp)
+    (re-search-forward regexp (rmail-msgend msg) t)))
+
+(defvar rmail-search-last-regexp nil)
+(defun rmail-search (regexp &optional n)
+  "Show message containing next match for REGEXP (but not the current msg).
+Prefix argument gives repeat count; negative argument means search
+backwards (through earlier messages).
+Interactively, empty argument means use same regexp used last time."
+  (interactive
+    (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
+	   (prompt
+	    (concat (if reversep "Reverse " "") "Rmail search (regexp"))
+	   regexp)
+      (setq prompt
+	    (concat prompt
+		    (if rmail-search-last-regexp
+			(concat ", default "
+				rmail-search-last-regexp "): ")
+		      "): ")))
+      (setq regexp (read-string prompt))
+      (cond ((not (equal regexp ""))
+	     (setq rmail-search-last-regexp regexp))
+	    ((not rmail-search-last-regexp)
+	     (error "No previous Rmail search string")))
+      (list rmail-search-last-regexp
+	    (prefix-numeric-value current-prefix-arg))))
+  (or n (setq n 1))
+  (message "%sRmail search for %s..."
+	   (if (< n 0) "Reverse " "")
+	   regexp)
+  (set-buffer rmail-buffer)
+  (let ((orig-message rmail-current-message)
+	(msg rmail-current-message)
+	(reversep (< n 0))
+	(opoint (if (rmail-buffers-swapped-p) (point)))
+	found)
+    (rmail-swap-buffers-maybe)
+    (rmail-maybe-set-message-counters)
+    (widen)
+    (unwind-protect
+	(while (/= n 0)
+	  ;; Check messages one by one, advancing message number up or
+	  ;; down but searching forward through each message.
+	  (if reversep
+	      (while (and (null found) (> msg 1))
+		(setq msg (1- msg)
+		      found (rmail-search-message msg regexp)))
+	    (while (and (null found) (< msg rmail-total-messages))
+	      (setq msg (1+ msg)
+		    found (rmail-search-message msg regexp))))
+	  (setq n (+ n (if reversep 1 -1))))
+      (if found
+	  (progn
+	    (rmail-show-message-maybe msg)
+	    ;; Search forward (if this is a normal search) or backward
+	    ;; (if this is a reverse search) through this message to
+	    ;; position point.  This search may fail because REGEXP
+	    ;; was found in the hidden portion of this message.  In
+	    ;; that case, move point to the beginning of visible
+	    ;; portion.
+	    (if reversep
+		(progn
+		  (goto-char (point-max))
+		  (re-search-backward regexp nil 'move))
+	      (goto-char (point-min))
+	      (re-search-forward regexp nil t))
+	    (message "%sRmail search for %s...done"
+		     (if reversep "Reverse " "")
+		     regexp))
+	(rmail-show-message-maybe orig-message)
+	(if opoint (goto-char opoint))
+	(ding)
+	(message "Search failed: %s" regexp)))))
+
+(defun rmail-search-backwards (regexp &optional n)
+  "Show message containing previous match for REGEXP.
+Prefix argument gives repeat count; negative argument means search
+forward (through later messages).
+Interactively, empty argument means use same regexp used last time."
+  (interactive
+    (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
+	   (prompt
+	    (concat (if reversep "Reverse " "") "Rmail search (regexp"))
+	   regexp)
+      (setq prompt
+	    (concat prompt
+		    (if rmail-search-last-regexp
+			(concat ", default "
+				rmail-search-last-regexp "): ")
+		      "): ")))
+      (setq regexp (read-string prompt))
+      (cond ((not (equal regexp ""))
+	     (setq rmail-search-last-regexp regexp))
+	    ((not rmail-search-last-regexp)
+	     (error "No previous Rmail search string")))
+      (list rmail-search-last-regexp
+	    (prefix-numeric-value current-prefix-arg))))
+  (rmail-search regexp (- (or n 1))))
+
+;; Scan for attributes, and compare subjects.
+
+(defun rmail-first-unseen-message ()
+  "Return message number of first message which has `unseen' attribute."
+  (rmail-maybe-set-message-counters)
+  (let ((current 1)
+	found)
+    (save-restriction
+      (widen)
+      (while (and (not found) (<= current rmail-total-messages))
+	(if (rmail-message-attr-p current "......U")
+	    (setq found current))
+	(setq current (1+ current))))
+    found))
+
+(defun rmail-simplified-subject (&optional msgnum)
+  "Return the simplified subject of message MSGNUM (or current message).
+Simplifying the subject means stripping leading and trailing whitespace,
+and typical reply prefixes such as Re:."
+  (let ((subject (or (rmail-get-header "Subject" msgnum) "")))
+    (if (string-match "\\`[ \t]+" subject)
+	(setq subject (substring subject (match-end 0))))
+    (if (string-match rmail-reply-regexp subject)
+	(setq subject (substring subject (match-end 0))))
+    (if (string-match "[ \t]+\\'" subject)
+	(setq subject (substring subject 0 (match-beginning 0))))
+    subject))
+
+(defun rmail-simplified-subject-regexp ()
+  "Return a regular expression matching the current simplified subject.
+The idea is to match it against simplified subjects of other messages."
+  (let ((subject (rmail-simplified-subject)))
+    (setq subject (regexp-quote subject))
+    ;; Hide commas so it will work ok if parsed as a comma-separated list
+    ;; of regexps.
+    (setq subject
+	  (replace-regexp-in-string "," "\054" subject t t))
+    (concat "\\`" subject "\\'")))
+
+(defun rmail-next-same-subject (n)
+  "Go to the next mail message having the same subject header.
+With prefix argument N, do this N times.
+If N is negative, go backwards instead."
+  (interactive "p")
+  (let ((subject (rmail-simplified-subject))
+	(forward (> n 0))
+	(i rmail-current-message)
+	found)
+    (while (and (/= n 0)
+		(if forward
+		    (< i rmail-total-messages)
+		  (> i 1)))
+      (let (done)
+	(while (and (not done)
+		    (if forward
+			(< i rmail-total-messages)
+		      (> i 1)))
+	  (setq i (if forward (1+ i) (1- i)))
+	  (setq done (string-equal subject (rmail-simplified-subject i))))
+	(if done (setq found i)))
+      (setq n (if forward (1- n) (1+ n))))
+    (if found
+	(rmail-show-message-maybe found)
+      (error "No %s message with same subject"
+	     (if forward "following" "previous")))))
+
+(defun rmail-previous-same-subject (n)
+  "Go to the previous mail message having the same subject header.
+With prefix argument N, do this N times.
+If N is negative, go forwards instead."
+  (interactive "p")
+  (rmail-next-same-subject (- n)))
+
+;;;; *** Rmail Message Deletion Commands ***
+
+(defun rmail-message-deleted-p (n)
+  (= (aref rmail-deleted-vector n) ?D))
+
+(defun rmail-set-message-deleted-p (n state)
+  (aset rmail-deleted-vector n (if state ?D ?\ )))
+
+(defun rmail-delete-message ()
+  "Delete this message and stay on it."
+  (interactive)
+  (rmail-set-attribute rmail-deleted-attr-index t)
+  (run-hooks 'rmail-delete-message-hook))
+
+(defun rmail-undelete-previous-message ()
+  "Back up to deleted message, select it, and undelete it."
+  (interactive)
+  (set-buffer rmail-buffer)
+  (let ((msg rmail-current-message))
+    (while (and (> msg 0)
+		(not (rmail-message-deleted-p msg)))
+      (setq msg (1- msg)))
+    (if (= msg 0)
+	(error "No previous deleted message")
+      (if (/= msg rmail-current-message)
+	  (rmail-show-message-maybe msg))
+      (rmail-set-attribute rmail-deleted-attr-index nil)
+      (if (rmail-summary-exists)
+	  (save-excursion
+	    (set-buffer rmail-summary-buffer)
+	    (rmail-summary-mark-undeleted msg)))
+      (rmail-maybe-display-summary))))
+
+(defun rmail-delete-forward (&optional backward)
+  "Delete this message and move to next nondeleted one.
+Deleted messages stay in the file until the \\[rmail-expunge] command is given.
+With prefix argument, delete and move backward.
+
+Returns t if a new message is displayed after the delete, or nil otherwise."
+  (interactive "P")
+  (rmail-set-attribute rmail-deleted-attr-index t)
+  (run-hooks 'rmail-delete-message-hook)
+  (let ((del-msg rmail-current-message))
+    (if (rmail-summary-exists)
+	(rmail-select-summary
+	 (rmail-summary-mark-deleted del-msg)))
+    (prog1 (rmail-next-undeleted-message (if backward -1 1))
+      (rmail-maybe-display-summary))))
+
+(defun rmail-delete-backward ()
+  "Delete this message and move to previous nondeleted one.
+Deleted messages stay in the file until the \\[rmail-expunge] command is given."
+  (interactive)
+  (rmail-delete-forward t))
+
+;; Expunging.
+
+;; Compute the message number a given message would have after expunging.
+;; The present number of the message is OLDNUM.
+;; DELETEDVEC should be rmail-deleted-vector.
+;; The value is nil for a message that would be deleted.
+(defun rmail-msg-number-after-expunge (deletedvec oldnum)
+  (if (or (null oldnum) (= (aref deletedvec oldnum) ?D))
+      nil
+    (let ((i 0)
+	  (newnum 0))
+      (while (< i oldnum)
+	(if (/= (aref deletedvec i) ?D)
+	    (setq newnum (1+ newnum)))
+	(setq i (1+ i)))
+      newnum)))
+
+(defun rmail-expunge-confirmed ()
+  "Return t if deleted message should be expunged. If necessary, ask the user.
+See also user-option `rmail-confirm-expunge'."
+  (set-buffer rmail-buffer)
+  (or (not (stringp rmail-deleted-vector))
+      (not (string-match "D" rmail-deleted-vector))
+      (null rmail-confirm-expunge)
+      (funcall rmail-confirm-expunge
+	       "Erase deleted messages from Rmail file? ")))
+
+(defun rmail-only-expunge (&optional dont-show)
+  "Actually erase all deleted messages in the file."
+  (interactive)
+  (rmail-swap-buffers-maybe)
+  (set-buffer rmail-buffer)
+  (message "Expunging deleted messages...")
+  ;; Discard all undo records for this buffer.
+  (or (eq buffer-undo-list t)
+      (setq buffer-undo-list nil))
+  (rmail-maybe-set-message-counters)
+  (let* ((omax (- (buffer-size) (point-max)))
+	 (omin (- (buffer-size) (point-min)))
+	 (opoint (if (and (> rmail-current-message 0)
+			  (rmail-message-deleted-p rmail-current-message))
+		     0
+		   (if rmail-enable-mime
+		       (with-current-buffer rmail-view-buffer
+			 (- (point)(point-min)))
+		     (- (point) (point-min)))))
+	 (messages-head (cons (aref rmail-message-vector 0) nil))
+	 (messages-tail messages-head)
+	 ;; Don't make any undo records for the expunging.
+	 (buffer-undo-list t)
+	 (win))
+    (unwind-protect
+	(save-excursion
+	  (widen)
+	  (goto-char (point-min))
+	  (let ((counter 0)
+		(number 1)
+		new-summary
+		(new-msgref (list (list 0)))
+		(buffer-read-only nil)
+		(total rmail-total-messages)
+		(new-message-number rmail-current-message)
+		(messages rmail-message-vector)
+		(deleted rmail-deleted-vector)
+		(summary rmail-summary-vector))
+	    (setq rmail-total-messages nil
+		  rmail-current-message nil
+		  rmail-message-vector nil
+		  rmail-deleted-vector nil
+		  rmail-summary-vector nil)
+
+	    (while (<= number total)
+	      (if (= (aref deleted number) ?D)
+		  (progn
+		    (delete-region (aref messages number)
+				   (aref messages (1+ number)))
+		    (move-marker (aref messages number) nil)
+		    (if (> new-message-number counter)
+			(setq new-message-number (1- new-message-number))))
+		(setq counter (1+ counter))
+		(setq messages-tail
+		      (setcdr messages-tail
+			      (cons (aref messages number) nil)))
+		(setq new-summary
+		      (cons (if (= counter number) (aref summary (1- number)))
+			    new-summary))
+		(setq new-msgref
+		      (cons (aref rmail-msgref-vector number)
+			    new-msgref))
+		(setcar (car new-msgref) counter))
+	      (if (zerop (% (setq number (1+ number)) 20))
+		  (message "Expunging deleted messages...%d" number)))
+	    (setq messages-tail
+		  (setcdr messages-tail
+			  (cons (aref messages number) nil)))
+	    (setq rmail-current-message new-message-number
+		  rmail-total-messages counter
+		  rmail-message-vector (apply 'vector messages-head)
+		  rmail-deleted-vector (make-string (1+ counter) ?\ )
+		  rmail-summary-vector (vconcat (nreverse new-summary))
+		  rmail-msgref-vector (apply 'vector (nreverse new-msgref))
+		  win t)))
+      (message "Expunging deleted messages...done")
+      (if (not win)
+	  (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
+      (if (not dont-show)
+	  (rmail-show-message-maybe (min rmail-current-message rmail-total-messages)))
+      (if rmail-enable-mime
+	  (goto-char (+ (point-min) opoint))
+	(goto-char (+ (point) opoint))))))
+
+(defun rmail-expunge (&optional dont-show)
+  "Erase deleted messages from Rmail file and summary buffer."
+  (interactive)
+  (when (rmail-expunge-confirmed)
+    (let ((old-total rmail-total-messages)
+	  (opoint (with-current-buffer rmail-buffer
+		    (when (rmail-buffers-swapped-p)
+		      (point)))))
+      (rmail-only-expunge dont-show)
+      (if (rmail-summary-exists)
+	  (rmail-select-summary (rmail-update-summary))
+	(rmail-show-message rmail-current-message)
+	(if (and (eq old-total rmail-total-messages) opoint)
+	    (goto-char opoint))))))
+
+;;;; *** Rmail Mailing Commands ***
+
+(defun rmail-start-mail (&optional noerase to subject in-reply-to cc
+				   replybuffer sendactions same-window others)
+  (let (yank-action)
+    (if replybuffer
+	(setq yank-action (list 'insert-buffer replybuffer)))
+    (setq others (cons (cons "cc" cc) others))
+    (setq others (cons (cons "in-reply-to" in-reply-to) others))
+    (if same-window
+	(compose-mail to subject others
+		      noerase nil
+		      yank-action sendactions)
+      (if rmail-mail-new-frame
+	  (prog1
+	      (compose-mail to subject others
+			    noerase 'switch-to-buffer-other-frame
+			    yank-action sendactions)
+	    ;; This is not a standard frame parameter;
+	    ;; nothing except sendmail.el looks at it.
+	    (modify-frame-parameters (selected-frame)
+				     '((mail-dedicated-frame . t))))
+	(compose-mail to subject others
+		      noerase 'switch-to-buffer-other-window
+		      yank-action sendactions)))))
+
+(defun rmail-mail ()
+  "Send mail in another window.
+While composing the message, use \\[mail-yank-original] to yank the
+original message into it."
+  (interactive)
+  (rmail-start-mail nil nil nil nil nil rmail-view-buffer))
+
+(defun rmail-continue ()
+  "Continue composing outgoing message previously being composed."
+  (interactive)
+  (rmail-start-mail t))
+
+(defun rmail-reply (just-sender)
+  "Reply to the current message.
+Normally include CC: to all other recipients of original message;
+prefix argument means ignore them.  While composing the reply,
+use \\[mail-yank-original] to yank the original message into it."
+  (interactive "P")
+  (let (from reply-to cc subject date to message-id references
+	     resent-to resent-cc resent-reply-to
+	     (msgnum rmail-current-message))
+    (save-excursion
+      (save-restriction
+	(widen)
+	(if (rmail-buffers-swapped-p)
+	    (narrow-to-region
+	     (goto-char (point-min))
+	     (search-forward "\n\n" nil 'move))
+	  (goto-char (rmail-msgbeg rmail-current-message))
+	  (forward-line 1)
+	  (narrow-to-region
+	   (point)
+	   (search-forward "\n\n"
+			   (rmail-msgend rmail-current-message)
+			   'move)))
+	(setq from (mail-fetch-field "from")
+	      reply-to (or (mail-fetch-field "mail-reply-to" nil t)
+			   (mail-fetch-field "reply-to" nil t)
+			   from)
+	      subject (mail-fetch-field "subject")
+	      date (mail-fetch-field "date")
+	      message-id (mail-fetch-field "message-id")
+	      references (mail-fetch-field "references" nil nil t)
+	      resent-reply-to (mail-fetch-field "resent-reply-to" nil t)
+	      resent-cc (and (not just-sender)
+			     (mail-fetch-field "resent-cc" nil t))
+	      resent-to (or (mail-fetch-field "resent-to" nil t) "")
+;;;	      resent-subject (mail-fetch-field "resent-subject")
+;;;	      resent-date (mail-fetch-field "resent-date")
+;;;	      resent-message-id (mail-fetch-field "resent-message-id")
+	      )
+	(unless just-sender
+	  (if (mail-fetch-field "mail-followup-to" nil t)
+	      ;; If this header field is present, use it instead of
+	      ;; the To and CC fields.
+	      (setq to (mail-fetch-field "mail-followup-to" nil t))
+	    (setq cc (or (mail-fetch-field "cc" nil t) "")
+		  to (or (mail-fetch-field "to" nil t) ""))))))
+
+    ;; Merge the resent-to and resent-cc into the to and cc.
+    (if (and resent-to (not (equal resent-to "")))
+	(if (not (equal to ""))
+	    (setq to (concat to ", " resent-to))
+	  (setq to resent-to)))
+    (if (and resent-cc (not (equal resent-cc "")))
+	(if (not (equal cc ""))
+	    (setq cc (concat cc ", " resent-cc))
+	  (setq cc resent-cc)))
+    ;; Add `Re: ' to subject if not there already.
+    (and (stringp subject)
+	 (setq subject
+	       (concat rmail-reply-prefix
+		       (if (let ((case-fold-search t))
+			     (string-match rmail-reply-regexp subject))
+			   (substring subject (match-end 0))
+			 subject))))
+    (rmail-start-mail
+     nil
+     ;; Using mail-strip-quoted-names is undesirable with newer mailers
+     ;; since they can handle the names unstripped.
+     ;; I don't know whether there are other mailers that still
+     ;; need the names to be stripped.
+;;;     (mail-strip-quoted-names reply-to)
+     ;; Remove unwanted names from reply-to, since Mail-Followup-To
+     ;; header causes all the names in it to wind up in reply-to, not
+     ;; in cc.  But if what's left is an empty list, use the original.
+     (let* ((reply-to-list (rmail-dont-reply-to reply-to)))
+       (if (string= reply-to-list "") reply-to reply-to-list))
+     subject
+     (rmail-make-in-reply-to-field from date message-id)
+     (if just-sender
+	 nil
+       ;; mail-strip-quoted-names is NOT necessary for rmail-dont-reply-to
+       ;; to do its job.
+       (let* ((cc-list (rmail-dont-reply-to
+			(mail-strip-quoted-names
+			 (if (null cc) to (concat to ", " cc))))))
+	 (if (string= cc-list "") nil cc-list)))
+     rmail-view-buffer
+     (list (list 'rmail-mark-message
+		 rmail-buffer
+		 (with-current-buffer rmail-buffer
+		   (aref rmail-msgref-vector msgnum))
+		 rmail-answered-attr-index))
+     nil
+     (list (cons "References" (concat (mapconcat 'identity references " ")
+				      " " message-id))))))
+
+(defun rmail-mark-message (buffer msgnum-list attribute)
+  "Give BUFFER's message number in MSGNUM-LIST the attribute ATTRIBUTE.
+This is use in the send-actions for message buffers.
+MSGNUM-LIST is a list of the form (MSGNUM)
+which is an element of rmail-msgref-vector."
+  (save-excursion
+    (set-buffer buffer)
+    (if (car msgnum-list)
+	(rmail-set-attribute attribute t (car msgnum-list)))))
+
+(defun rmail-make-in-reply-to-field (from date message-id)
+  (cond ((not from)
+         (if message-id
+             message-id
+             nil))
+        (mail-use-rfc822
+         (require 'rfc822)
+         (let ((tem (car (rfc822-addresses from))))
+           (if message-id
+               (if (or (not tem)
+		       (string-match
+			(regexp-quote (if (string-match "@[^@]*\\'" tem)
+					  (substring tem 0
+						     (match-beginning 0))
+					tem))
+			message-id))
+                   ;; missing From, or Message-ID is sufficiently informative
+                   message-id
+                   (concat message-id " (" tem ")"))
+	     ;; Copy TEM, discarding text properties.
+	     (setq tem (copy-sequence tem))
+	     (set-text-properties 0 (length tem) nil tem)
+	     (setq tem (copy-sequence tem))
+	     ;; Use prin1 to fake RFC822 quoting
+	     (let ((field (prin1-to-string tem)))
+	       (if date
+		   (concat field "'s message of " date)
+		   field)))))
+        ((let* ((foo "[^][\000-\037()<>@,;:\\\" ]+")
+                (bar "[^][\000-\037()<>@,;:\\\"]+"))
+	   ;; These strings both match all non-ASCII characters.
+           (or (string-match (concat "\\`[ \t]*\\(" bar
+                                     "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
+                             ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
+                             from)
+               (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
+                                     bar "\\))[ \t]*\\'")
+                             ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
+                             from)))
+         (let ((start (match-beginning 1))
+               (end (match-end 1)))
+           ;; Trim whitespace which above regexp match allows
+           (while (and (< start end)
+                       (memq (aref from start) '(?\t ?\ )))
+             (setq start (1+ start)))
+           (while (and (< start end)
+                       (memq (aref from (1- end)) '(?\t ?\ )))
+             (setq end (1- end)))
+           (let ((field (substring from start end)))
+             (if date (setq field (concat "message from " field " on " date)))
+             (if message-id
+                 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
+                 (concat message-id " (" field ")")
+                 field))))
+        (t
+         ;; If we can't kludge it simply, do it correctly
+         (let ((mail-use-rfc822 t))
+           (rmail-make-in-reply-to-field from date message-id)))))
+
+(defun rmail-forward (resend)
+  "Forward the current message to another user.
+With prefix argument, \"resend\" the message instead of forwarding it;
+see the documentation of `rmail-resend'."
+  (interactive "P")
+  (if resend
+      (call-interactively 'rmail-resend)
+    (let ((forward-buffer rmail-buffer)
+	  (msgnum rmail-current-message)
+	  (subject (concat "["
+			   (let ((from (or (mail-fetch-field "From")
+					   (mail-fetch-field ">From"))))
+			     (if from
+				 (concat (mail-strip-quoted-names from) ": ")
+			       ""))
+			   (or (mail-fetch-field "Subject") "")
+			   "]")))
+      (if (rmail-start-mail
+	   nil nil subject nil nil nil
+	   (list (list 'rmail-mark-message
+		       forward-buffer
+		       (with-current-buffer rmail-buffer
+			 (aref rmail-msgref-vector msgnum))
+		       rmail-forwarded-attr-index))
+	   ;; If only one window, use it for the mail buffer.
+	   ;; Otherwise, use another window for the mail buffer
+	   ;; so that the Rmail buffer remains visible
+	   ;; and sending the mail will get back to it.
+	   (and (not rmail-mail-new-frame) (one-window-p t)))
+	  ;; The mail buffer is now current.
+	  (save-excursion
+	    ;; Insert after header separator--before signature if any.
+	    (goto-char (mail-text-start))
+	    (if (or rmail-enable-mime rmail-enable-mime-composing)
+		(funcall rmail-insert-mime-forwarded-message-function
+			 forward-buffer)
+	      (insert "------- Start of forwarded message -------\n")
+	      ;; Quote lines with `- ' if they start with `-'.
+	      (let ((beg (point)) end)
+		(setq end (point-marker))
+		(set-marker-insertion-type end t)
+		(insert-buffer-substring forward-buffer)
+		(goto-char beg)
+		(while (re-search-forward "^-" end t)
+		  (beginning-of-line)
+		  (insert "- ")
+		  (forward-line 1))
+		(goto-char end)
+		(skip-chars-backward "\n")
+		(if (< (point) end)
+		    (forward-char 1))
+		(delete-region (point) end)
+		(set-marker end nil))
+	      (insert "------- End of forwarded message -------\n"))
+	    (push-mark))))))
+
+(defun rmail-resend (address &optional from comment mail-alias-file)
+  "Resend current message to ADDRESSES.
+ADDRESSES should be a single address, a string consisting of several
+addresses separated by commas, or a list of addresses.
+
+Optional FROM is the address to resend the message from, and
+defaults from the value of `user-mail-address'.
+Optional COMMENT is a string to insert as a comment in the resent message.
+Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
+typically for purposes of moderating a list."
+  (interactive "sResend to: ")
+  (require 'sendmail)
+  (require 'mailalias)
+  (unless (or (eq rmail-view-buffer (current-buffer))
+	      (eq rmail-buffer (current-buffer)))
+    (error "Not an Rmail buffer"))
+  (if (not from) (setq from user-mail-address))
+  (let ((tembuf (generate-new-buffer " sendmail temp"))
+	(case-fold-search nil)
+	(mail-personal-alias-file
+	 (or mail-alias-file mail-personal-alias-file))
+	(mailbuf rmail-buffer))
+    (unwind-protect
+	(with-current-buffer tembuf
+	  ;;>> Copy message into temp buffer
+	  (if rmail-enable-mime
+	      (funcall rmail-insert-mime-resent-message-function mailbuf)
+	    (insert-buffer-substring mailbuf))
+	  (goto-char (point-min))
+	  ;; Delete any Sender field, since that's not specifiable.
+	  ; Only delete Sender fields in the actual header.
+	  (re-search-forward "^$" nil 'move)
+	  ; Using "while" here rather than "if" because some buggy mail
+	  ; software may have inserted multiple Sender fields.
+	  (while (re-search-backward "^Sender:" nil t)
+	    (let (beg)
+	      (setq beg (point))
+	      (forward-line 1)
+	      (while (looking-at "[ \t]")
+		(forward-line 1))
+	      (delete-region beg (point))))
+	  ; Go back to the beginning of the buffer so the Resent- fields
+	  ; are inserted there.
+	  (goto-char (point-min))
+	  ;;>> Insert resent-from:
+	  (insert "Resent-From: " from "\n")
+	  (insert "Resent-Date: " (mail-rfc822-date) "\n")
+	  ;;>> Insert resent-to: and bcc if need be.
+	  (let ((before (point)))
+	    (if mail-self-blind
+		(insert "Resent-Bcc: " (user-login-name) "\n"))
+	    (insert "Resent-To: " (if (stringp address)
+			       address
+			     (mapconcat 'identity address ",\n\t"))
+		    "\n")
+	    ;; Expand abbrevs in the recipients.
+	    (save-excursion
+	      (if (featurep 'mailabbrev)
+		  (let ((end (point-marker))
+			(local-abbrev-table mail-abbrevs)
+			(old-syntax-table (syntax-table)))
+		    (if (and (not (vectorp mail-abbrevs))
+			     (file-exists-p mail-personal-alias-file))
+			(build-mail-abbrevs))
+		    (unless mail-abbrev-syntax-table
+		      (mail-abbrev-make-syntax-table))
+		    (set-syntax-table mail-abbrev-syntax-table)
+		    (goto-char before)
+		    (while (and (< (point) end)
+				(progn (forward-word 1)
+				       (<= (point) end)))
+		      (expand-abbrev))
+		    (set-syntax-table old-syntax-table))
+		(expand-mail-aliases before (point)))))
+	  ;;>> Set up comment, if any.
+	  (if (and (sequencep comment) (not (zerop (length comment))))
+	      (let ((before (point))
+		    after)
+		(insert comment)
+		(or (eolp) (insert "\n"))
+		(setq after (point))
+		(goto-char before)
+		(while (< (point) after)
+		  (insert "Resent-Comment: ")
+		  (forward-line 1))))
+	  ;; Don't expand aliases in the destination fields
+	  ;; of the original message.
+	  (let (mail-aliases)
+	    (funcall send-mail-function)))
+      (kill-buffer tembuf))
+    (with-current-buffer rmail-buffer
+      (rmail-set-attribute rmail-resent-attr-index t rmail-current-message))))
+
+(defvar mail-unsent-separator
+  (concat "^ *---+ +Unsent message follows +---+ *$\\|"
+	  "^ *---+ +Returned message +---+ *$\\|"
+	  "^ *---+ *Returned mail follows *---+ *$\\|"
+	  "^Start of returned message$\\|"
+	  "^---+ Below this line is a copy of the message.$\\|"
+	  "^ *---+ +Original message +---+ *$\\|"
+	  "^ *--+ +begin message +--+ *$\\|"
+	  "^ *---+ +Original message follows +---+ *$\\|"
+	  "^ *---+ +Your message follows +---+ *$\\|"
+	  "^|? *---+ +Message text follows: +---+ *|?$\\|"
+	  "^ *---+ +This is a copy of \\w+ message, including all the headers.*---+ *$")
+  "A regexp that matches the separator before the text of a failed message.")
+
+(defvar mail-mime-unsent-header "^Content-Type: message/rfc822 *$"
+ "A regexp that matches the header of a MIME body part with a failed message.")
+
+(defun rmail-retry-failure ()
+  "Edit a mail message which is based on the contents of the current message.
+For a message rejected by the mail system, extract the interesting headers and
+the body of the original message.
+If the failed message is a MIME multipart message, it is searched for a
+body part with a header which matches the variable `mail-mime-unsent-header'.
+Otherwise, the variable `mail-unsent-separator' should match the string that
+delimits the returned original message.
+The variable `rmail-retry-ignored-headers' is a regular expression
+specifying headers which should not be copied into the new message."
+  (interactive)
+  (require 'mail-utils)
+  (let ((rmail-this-buffer (current-buffer))
+	(msgnum rmail-current-message)
+	bounce-start bounce-end bounce-indent resending
+	;; Fetch any content-type header in current message
+	;; Must search thru the whole unpruned header.
+	(content-type
+	 (save-excursion
+	   (save-restriction
+	     (mail-fetch-field "Content-Type") ))))
+    (save-excursion
+      (goto-char (point-min))
+      (let ((case-fold-search t))
+	(if (and content-type
+		 (string-match
+		  ";[\n\t ]*boundary=\"?\\([-0-9a-z'()+_,./:=? ]+\\)\"?"
+		  content-type))
+	    ;; Handle a MIME multipart bounce message.
+	    (let ((codestring
+		   (concat "\n--"
+			   (substring content-type (match-beginning 1)
+				      (match-end 1)))))
+	      (unless (re-search-forward mail-mime-unsent-header nil t)
+		(error "Cannot find beginning of header in failed message"))
+	      (unless (search-forward "\n\n" nil t)
+		(error "Cannot find start of Mime data in failed message"))
+	      (setq bounce-start (point))
+	      (if (search-forward codestring nil t)
+		  (setq bounce-end (match-beginning 0))
+		(setq bounce-end (point-max))))
+	  ;; Non-MIME bounce.
+	  (or (re-search-forward mail-unsent-separator nil t)
+	      (error "Cannot parse this as a failure message"))
+	  (skip-chars-forward "\n")
+	  ;; Support a style of failure message in which the original
+	  ;; message is indented, and included within lines saying
+	  ;; `Start of returned message' and `End of returned message'.
+	  (if (looking-at " +Received:")
+	      (progn
+		(setq bounce-start (point))
+		(skip-chars-forward " ")
+		(setq bounce-indent (- (current-column)))
+		(goto-char (point-max))
+		(re-search-backward "^End of returned message$" nil t)
+		(setq bounce-end (point)))
+	    ;; One message contained a few random lines before
+	    ;; the old message header.  The first line of the
+	    ;; message started with two hyphens.  A blank line
+	    ;; followed these random lines.  The same line
+	    ;; beginning with two hyphens was possibly marking
+	    ;; the end of the message.
+	    (if (looking-at "^--")
+		(let ((boundary (buffer-substring-no-properties
+				 (point)
+				 (progn (end-of-line) (point)))))
+		  (search-forward "\n\n")
+		  (skip-chars-forward "\n")
+		  (setq bounce-start (point))
+		  (goto-char (point-max))
+		  (search-backward (concat "\n\n" boundary) bounce-start t)
+		  (setq bounce-end (point)))
+	      (setq bounce-start (point)
+		    bounce-end (point-max)))
+	    (unless (search-forward "\n\n" nil t)
+	      (error "Cannot find end of header in failed message"))))))
+    ;; We have found the message that bounced, within the current message.
+    ;; Now start sending new message; default header fields from original.
+    ;; Turn off the usual actions for initializing the message body
+    ;; because we want to get only the text from the failure message.
+    (let (mail-signature mail-setup-hook)
+      (if (rmail-start-mail nil nil nil nil nil rmail-this-buffer
+			    (list (list 'rmail-mark-message
+					rmail-this-buffer
+					(aref rmail-msgref-vector msgnum)
+					rmail-retried-attr-index)))
+	  ;; Insert original text as initial text of new draft message.
+	  ;; Bind inhibit-read-only since the header delimiter
+	  ;; of the previous message was probably read-only.
+	  (let ((inhibit-read-only t)
+		rmail-displayed-headers
+		rmail-ignored-headers)
+	    (erase-buffer)
+	    (insert-buffer-substring rmail-this-buffer
+				     bounce-start bounce-end)
+	    (goto-char (point-min))
+	    (if bounce-indent
+		(indent-rigidly (point-min) (point-max) bounce-indent))
+	    (mail-sendmail-delimit-header)
+	    (save-restriction
+	      (narrow-to-region (point-min) (mail-header-end))
+	      (setq resending (mail-fetch-field "resent-to"))
+	      (if mail-self-blind
+		  (if resending
+		      (insert "Resent-Bcc: " (user-login-name) "\n")
+		    (insert "BCC: " (user-login-name) "\n"))))
+	    (goto-char (point-min))
+	    (mail-position-on-field (if resending "Resent-To" "To") t))))))
+
+(defun rmail-summary-exists ()
+  "Non-nil if in an RMAIL buffer and an associated summary buffer exists.
+In fact, the non-nil value returned is the summary buffer itself."
+  (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
+       rmail-summary-buffer))
+
+(defun rmail-summary-displayed ()
+  "t if in RMAIL buffer and an associated summary buffer is displayed."
+  (and rmail-summary-buffer (get-buffer-window rmail-summary-buffer)))
+
+(defcustom rmail-redisplay-summary nil
+  "*Non-nil means Rmail should show the summary when it changes.
+This has an effect only if a summary buffer exists."
+  :type 'boolean
+  :group 'rmail-summary)
+
+(defcustom rmail-summary-window-size nil
+  "*Non-nil means specify the height for an Rmail summary window."
+  :type '(choice (const :tag "Disabled" nil) integer)
+  :group 'rmail-summary)
+
+;; Put the summary buffer back on the screen, if user wants that.
+(defun rmail-maybe-display-summary ()
+  (let ((selected (selected-window))
+	window)
+    ;; If requested, make sure the summary is displayed.
+    (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
+	 rmail-redisplay-summary
+	 (if (get-buffer-window rmail-summary-buffer 0)
+	     ;; It's already in some frame; show that one.
+	     (let ((frame (window-frame
+			   (get-buffer-window rmail-summary-buffer 0))))
+	       (make-frame-visible frame)
+	       (raise-frame frame))
+	   (display-buffer rmail-summary-buffer)))
+    ;; If requested, set the height of the summary window.
+    (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
+	 rmail-summary-window-size
+	 (setq window (get-buffer-window rmail-summary-buffer))
+	 ;; Don't try to change the size if just one window in frame.
+	 (not (eq window (frame-root-window (window-frame window))))
+	 (unwind-protect
+	     (progn
+	       (select-window window)
+	       (enlarge-window (- rmail-summary-window-size (window-height))))
+	   (select-window selected)))))
+
+;;;; *** Rmail Local Fontification ***
+
+(defun rmail-fontify-buffer-function ()
+  ;; This function's symbol is bound to font-lock-fontify-buffer-function.
+  (add-hook 'rmail-show-message-hook 'rmail-fontify-message nil t)
+  ;; If we're already showing a message, fontify it now.
+  (if rmail-current-message (rmail-fontify-message))
+  ;; Prevent Font Lock mode from kicking in.
+  (setq font-lock-fontified t))
+
+(defun rmail-unfontify-buffer-function ()
+  ;; This function's symbol is bound to font-lock-fontify-unbuffer-function.
+  (let ((modified (buffer-modified-p))
+	(buffer-undo-list t) (inhibit-read-only t)
+	before-change-functions after-change-functions
+	buffer-file-name buffer-file-truename)
+    (save-restriction
+      (widen)
+      (remove-hook 'rmail-show-message-hook 'rmail-fontify-message t)
+      (remove-text-properties (point-min) (point-max) '(rmail-fontified nil))
+      (font-lock-default-unfontify-buffer)
+      (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))
+
+(defun rmail-fontify-message ()
+  ;; Fontify the current message if it is not already fontified.
+  (if (text-property-any (point-min) (point-max) 'rmail-fontified nil)
+      (let ((modified (buffer-modified-p))
+	    (buffer-undo-list t) (inhibit-read-only t)
+	    before-change-functions after-change-functions
+	    buffer-file-name buffer-file-truename)
+	(save-excursion
+	  (save-match-data
+	    (add-text-properties (point-min) (point-max) '(rmail-fontified t))
+	    (font-lock-fontify-region (point-min) (point-max))
+	    (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))))
+
+;;; Speedbar support for RMAIL files.
+(eval-when-compile (require 'speedbar))
+
+(defvar rmail-speedbar-match-folder-regexp "^[A-Z0-9]+\\(\\.[A-Z0-9]+\\)?$"
+  "*This regex is used to match folder names to be displayed in speedbar.
+Enabling this will permit speedbar to display your folders for easy
+browsing, and moving of messages.")
+
+(defvar rmail-speedbar-last-user nil
+  "The last user to be displayed in the speedbar.")
+
+(defvar rmail-speedbar-key-map nil
+  "Keymap used when in rmail display mode.")
+
+(defun rmail-install-speedbar-variables ()
+  "Install those variables used by speedbar to enhance rmail."
+  (if rmail-speedbar-key-map
+      nil
+    (setq rmail-speedbar-key-map (speedbar-make-specialized-keymap))
+
+    (define-key rmail-speedbar-key-map "e" 'speedbar-edit-line)
+    (define-key rmail-speedbar-key-map "r" 'speedbar-edit-line)
+    (define-key rmail-speedbar-key-map "\C-m" 'speedbar-edit-line)
+    (define-key rmail-speedbar-key-map "M"
+      'rmail-speedbar-move-message-to-folder-on-line)))
+
+(defvar rmail-speedbar-menu-items
+  '(["Read Folder" speedbar-edit-line t]
+    ["Move message to folder" rmail-speedbar-move-message-to-folder-on-line
+     (save-excursion (beginning-of-line)
+		     (looking-at "<M> "))])
+  "Additional menu-items to add to speedbar frame.")
+
+;; Make sure our special speedbar major mode is loaded
+(if (featurep 'speedbar)
+    (rmail-install-speedbar-variables)
+  (add-hook 'speedbar-load-hook 'rmail-install-speedbar-variables))
+
+(defun rmail-speedbar-buttons (buffer)
+  "Create buttons for BUFFER containing rmail messages.
+Click on the address under Reply to: to reply to this person.
+Under Folders: Click a name to read it, or on the <M> to move the
+current message into that RMAIL folder."
+  (let ((from nil))
+    (save-excursion
+      (set-buffer buffer)
+      (goto-char (point-min))
+      (if (not (re-search-forward "^Reply-To: " nil t))
+	  (if (not (re-search-forward "^From:? " nil t))
+	      (setq from t)))
+      (if from
+	  nil
+	(setq from (buffer-substring (point) (save-excursion
+					       (end-of-line)
+					       (point))))))
+    (goto-char (point-min))
+    (if (and (looking-at "Reply to:")
+	     (equal from rmail-speedbar-last-user))
+	nil
+      (setq rmail-speedbar-last-user from)
+      (erase-buffer)
+      (insert "Reply To:\n")
+      (if (stringp from)
+	  (speedbar-insert-button from 'speedbar-directory-face 'highlight
+				  'rmail-speedbar-button 'rmail-reply))
+      (insert "Folders:\n")
+      (let* ((case-fold-search nil)
+	     (df (directory-files (save-excursion (set-buffer buffer)
+						  default-directory)
+				  nil rmail-speedbar-match-folder-regexp)))
+	(while df
+	  (speedbar-insert-button "<M>" 'speedbar-button-face 'highlight
+				  'rmail-speedbar-move-message (car df))
+	  (speedbar-insert-button (car df) 'speedbar-file-face 'highlight
+				  'rmail-speedbar-find-file nil t)
+	  (setq df (cdr df)))))))
+
+(defun rmail-speedbar-button (text token indent)
+  "Execute an rmail command specified by TEXT.
+The command used is TOKEN.  INDENT is not used."
+  (speedbar-with-attached-buffer
+   (funcall token t)))
+
+(defun rmail-speedbar-find-file (text token indent)
+  "Load in the rmail file TEXT.
+TOKEN and INDENT are not used."
+  (speedbar-with-attached-buffer
+   (message "Loading in RMAIL file %s..." text)
+   (find-file text)))
+
+(defun rmail-speedbar-move-message-to-folder-on-line ()
+  "If the current line is a folder, move current message to it."
+  (interactive)
+  (save-excursion
+    (beginning-of-line)
+    (if (re-search-forward "<M> " (save-excursion (end-of-line) (point)) t)
+	(progn
+	  (forward-char -2)
+	  (speedbar-do-function-pointer)))))
+
+(defun rmail-speedbar-move-message (text token indent)
+  "From button TEXT, copy current message to the rmail file specified by TOKEN.
+TEXT and INDENT are not used."
+  (speedbar-with-attached-buffer
+   (message "Moving message to %s" token)
+   (rmail-output token)))
+
+; Functions for setting, getting and encoding the POP password.
+; The password is encoded to prevent it from being easily accessible
+; to "prying eyes."  Obviously, this encoding isn't "real security,"
+; nor is it meant to be.
+
+;;;###autoload
+(defun rmail-set-remote-password (password)
+  "Set PASSWORD to be used for retrieving mail from a POP or IMAP server."
+  (interactive "sPassword: ")
+  (if password
+      (setq rmail-encoded-remote-password
+	    (rmail-encode-string password (emacs-pid)))
+    (setq rmail-remote-password nil)
+    (setq rmail-encoded-remote-password nil)))
+
+(defun rmail-get-remote-password (imap)
+  "Get the password for retrieving mail from a POP or IMAP server.  If none
+has been set, then prompt the user for one."
+  (when (not rmail-encoded-remote-password)
+    (if (not rmail-remote-password)
+	(setq rmail-remote-password
+	      (read-passwd (if imap
+			       "IMAP password: "
+			     "POP password: "))))
+    (rmail-set-remote-password rmail-remote-password)
+    (setq rmail-remote-password nil))
+  (rmail-encode-string rmail-encoded-remote-password (emacs-pid)))
+
+(defun rmail-have-password ()
+  (or rmail-remote-password rmail-encoded-remote-password))
+
+(defun rmail-encode-string (string mask)
+ "Encode STRING with integer MASK, by taking the exclusive OR of the
+lowest byte in the mask with the first character of string, the
+second-lowest-byte with the second character of the string, etc.,
+restarting at the lowest byte of the mask whenever it runs out.
+Returns the encoded string.  Calling the function again with an
+encoded string (and the same mask) will decode the string."
+ (setq mask (abs mask))			; doesn't work if negative
+ (let* ((string-vector (string-to-vector string)) (i 0)
+	(len (length string-vector)) (curmask mask) charmask)
+   (while (< i len)
+     (if (= curmask 0)
+	 (setq curmask mask))
+     (setq charmask (% curmask 256))
+     (setq curmask (lsh curmask -8))
+     (aset string-vector i (logxor charmask (aref string-vector i)))
+     (setq i (1+ i)))
+   (concat string-vector)))
+
+;;;;  Desktop support
+
+(defun rmail-restore-desktop-buffer (desktop-buffer-file-name
+                                     desktop-buffer-name
+                                     desktop-buffer-misc)
+  "Restore an rmail buffer specified in a desktop file."
+  (condition-case error
+      (progn
+        (rmail-input desktop-buffer-file-name)
+        (if (eq major-mode 'rmail-mode)
+            (current-buffer)
+          rmail-buffer))
+    (file-locked
+      (kill-buffer (current-buffer))
+      nil)))
+
+(add-to-list 'desktop-buffer-mode-handlers
+	     '(rmail-mode . rmail-restore-desktop-buffer))
+
+;; Used in `write-region-annotate-functions' to write rmail files.
+(defun rmail-write-region-annotate (start end)
+  (when (rmail-buffers-swapped-p)
+    (set-buffer rmail-view-buffer)
+    (widen)
+    nil))
+
+(provide 'rmail)
+
+;; Local Variables:
+;; change-log-default-name: "ChangeLog.rmail"
+;; End:
+
+;; arch-tag: 65d257d3-c281-4a65-9c38-e61af95af2f0
+;;; rmail.el ends here
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lisp/mail/rmailedit.el	Thu Jan 22 17:09:23 2009 +0000
@@ -0,0 +1,217 @@
+;;; rmailedit.el --- "RMAIL edit mode"  Edit the current message
+
+;; Copyright (C) 1985, 1994, 2001, 2002, 2003, 2004, 2005, 2006,
+;;   2007, 2008, 2009 Free Software Foundation, Inc.
+
+;; Maintainer: FSF
+;; Keywords: mail
+
+;; 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 3 of the License, 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.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;;; Code:
+
+(eval-when-compile
+  (require 'rmail)
+  (require 'rmailsum))
+
+(defcustom rmail-edit-mode-hook nil
+  "List of functions to call when editing an RMAIL message."
+  :type 'hook
+  :version "21.1"
+  :group 'rmail-edit)
+
+(defvar rmail-old-text)
+
+(defvar rmail-edit-map nil)
+(if rmail-edit-map
+    nil
+  ;; Make a keymap that inherits text-mode-map.
+  (setq rmail-edit-map (make-sparse-keymap))
+  (set-keymap-parent rmail-edit-map text-mode-map)
+  (define-key rmail-edit-map "\C-c\C-c" 'rmail-cease-edit)
+  (define-key rmail-edit-map "\C-c\C-]" 'rmail-abort-edit))
+
+;; Rmail Edit mode is suitable only for specially formatted data.
+(put 'rmail-edit-mode 'mode-class 'special)
+
+(declare-function rmail-summary-disable "" ())
+(declare-function rmail-summary-enable "rmailsum" ())
+
+(defun rmail-edit-mode ()
+  "Major mode for editing the contents of an RMAIL message.
+The editing commands are the same as in Text mode, together with two commands
+to return to regular RMAIL:
+  *  \\[rmail-abort-edit] cancels the changes
+     you have made and returns to RMAIL
+  *  \\[rmail-cease-edit] makes them permanent.
+This functions runs the normal hook `rmail-edit-mode-hook'.
+\\{rmail-edit-map}"
+  (if (rmail-summary-exists)
+      (save-excursion
+	(set-buffer rmail-summary-buffer)
+	(rmail-summary-disable)))
+  (let (rmail-buffer-swapped)
+    ;; Prevent change-major-mode-hook from unswapping the buffers.
+    (delay-mode-hooks (text-mode))
+    (use-local-map rmail-edit-map)
+    (setq major-mode 'rmail-edit-mode)
+    (setq mode-name "RMAIL Edit")
+    (if (boundp 'mode-line-modified)
+	(setq mode-line-modified (default-value 'mode-line-modified))
+      (setq mode-line-format (default-value 'mode-line-format)))
+    (run-mode-hooks 'rmail-edit-mode-hook)))
+
+(defvar rmail-old-pruned nil)
+(put 'rmail-old-pruned 'permanent-local t)
+
+;;;###autoload
+(defun rmail-edit-current-message ()
+  "Edit the contents of this message."
+  (interactive)
+  (if (= rmail-total-messages 0)
+      (error "No messages in this buffer"))
+  (make-local-variable 'rmail-old-pruned)
+  (setq rmail-old-pruned (eq rmail-header-style 'normal))
+  (rmail-edit-mode)
+  (make-local-variable 'rmail-old-text)
+  (save-restriction
+    (widen)
+    (setq rmail-old-text (buffer-substring (point-min) (point-max))))
+  (setq buffer-read-only nil)
+  (setq buffer-undo-list nil)
+  (force-mode-line-update)
+  (if (and (eq (key-binding "\C-c\C-c") 'rmail-cease-edit)
+	   (eq (key-binding "\C-c\C-]") 'rmail-abort-edit))
+      (message "Editing: Type C-c C-c to return to Rmail, C-c C-] to abort")
+    (message "%s" (substitute-command-keys
+		   "Editing: Type \\[rmail-cease-edit] to return to Rmail, \\[rmail-abort-edit] to abort"))))
+
+(defun rmail-cease-edit ()
+  "Finish editing message; switch back to Rmail proper."
+  (interactive)
+  (if (rmail-summary-exists)
+      (save-excursion
+	(set-buffer rmail-summary-buffer)
+	(rmail-summary-enable)))
+  (widen)
+  ;; Disguise any "From " lines so they don't start a new message.
+  (save-excursion
+    (goto-char (point-min))
+    (while (search-forward "\nFrom " nil t)
+      (beginning-of-line)
+      (insert ">")))
+  ;; Make sure buffer ends with a blank line
+  ;; so as not to run this message together with the following one.
+  (save-excursion
+    (goto-char (point-max))
+    (if (/= (preceding-char) ?\n)
+	(insert "\n"))
+    (unless (looking-back "\n\n")
+      (insert "\n")))
+  (let ((old rmail-old-text)
+	character-coding is-text-message coding-system
+	headers-end)
+    ;; Go back to Rmail mode, but carefully.
+    (force-mode-line-update)
+    (let (rmail-buffer-swapped)
+      (kill-all-local-variables)
+      (rmail-mode-1)
+      (if (boundp 'tool-bar-map)
+	  (set (make-local-variable 'tool-bar-map) rmail-tool-bar-map))
+      (setq buffer-undo-list t)
+      (rmail-variables))
+    ;; If text has really changed, mark message as edited.
+    (unless (and (= (length old) (- (point-max) (point-min)))
+		 (string= old (buffer-substring (point-min) (point-max))))
+      (setq old nil)
+      (goto-char (point-min))
+      (search-forward "\n\n")
+      (setq headers-end (point))
+
+      (rmail-swap-buffers-maybe)
+
+      (setq character-coding (mail-fetch-field "content-transfer-encoding")
+	    is-text-message (rmail-is-text-p)
+	    coding-system (rmail-get-coding-system))
+      (if character-coding
+	  (setq character-coding (downcase character-coding)))
+
+      (narrow-to-region (rmail-msgbeg rmail-current-message)
+			(rmail-msgend rmail-current-message))
+      (goto-char (point-min))
+      (search-forward "\n\n")
+      (let ((inhibit-read-only t)
+	    (headers-end-1 (point)))
+	(insert-buffer-substring rmail-view-buffer headers-end)
+	(delete-region (point) (point-max))
+
+	;; Re-encode the message body in whatever
+	;; way it was decoded.
+	(cond
+	 ((string= character-coding "quoted-printable")
+	  (mail-quote-printable-region headers-end-1 (point-max)))
+	 ((and (string= character-coding "base64") is-text-message)
+	  (base64-encode-region headers-end-1 (point-max)))
+	 ((eq character-coding 'uuencode)
+	  (error "Not supported yet."))
+	 (t
+	  (if (or (not coding-system) (not (coding-system-p coding-system)))
+	      (setq coding-system 'undecided))
+	  (encode-coding-region headers-end-1 (point-max) coding-system)))
+	))
+
+    (rmail-set-attribute rmail-edited-attr-index t)
+	
+    ;;??? BROKEN perhaps.
+    ;; I think that the Summary-Line header may not be kept there any more.
+;;;       (if (boundp 'rmail-summary-vector)
+;;; 	  (progn
+;;; 	    (aset rmail-summary-vector (1- rmail-current-message) nil)
+;;; 	    (save-excursion
+;;; 	      (rmail-widen-to-current-msgbeg
+;;; 		(function (lambda ()
+;;; 			    (forward-line 2)
+;;; 			    (if (looking-at "Summary-line: ")
+;;; 				(let ((buffer-read-only nil))
+;;; 				  (delete-region (point)
+;;; 						 (progn (forward-line 1)
+;;; 							(point)))))))))))
+    )
+
+  (save-excursion
+    (rmail-show-message)
+    (rmail-toggle-header (if rmail-old-pruned 1 0)))
+  (run-hooks 'rmail-mode-hook))
+
+(defun rmail-abort-edit ()
+  "Abort edit of current message; restore original contents."
+  (interactive)
+  (widen)
+  (delete-region (point-min) (point-max))
+  (insert rmail-old-text)
+  (rmail-cease-edit)
+  (rmail-highlight-headers))
+
+(provide 'rmailedit)
+
+;; Local Variables:
+;; change-log-default-name: "ChangeLog.rmail"
+;; End:
+
+;; arch-tag: 9524f335-12cc-4e95-9e9b-3208dc30550b
+;;; rmailedit.el ends here
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lisp/mail/rmailkwd.el	Thu Jan 22 17:09:23 2009 +0000
@@ -0,0 +1,169 @@
+;;; rmailkwd.el --- part of the "RMAIL" mail reader for Emacs
+
+;; Copyright (C) 1985, 1988, 1994, 2001, 2002, 2003, 2004, 2005, 2006,
+;;   2007, 2008, 2009  Free Software Foundation, Inc.
+
+;; Maintainer: FSF
+;; Keywords: mail
+
+;; 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 3 of the License, 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.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;;; Code:
+
+(require 'rmail)
+
+;; Global to all RMAIL buffers.  It exists primarily for the sake of
+;; completion.  It is better to use strings with the label functions
+;; and let them worry about making the label.
+
+(defvar rmail-label-obarray (make-vector 47 0))
+
+(mapc (function (lambda (s) (intern s rmail-label-obarray)))
+      '("deleted" "answered" "filed" "forwarded" "unseen" "edited"
+	"resent"))
+
+(defun rmail-make-label (s)
+  (intern (downcase s) rmail-label-obarray))
+
+;;;###autoload
+(defun rmail-add-label (string)
+  "Add LABEL to labels associated with current RMAIL message.
+Performs completion over known labels when reading."
+  (interactive (list (rmail-read-label "Add label")))
+  (rmail-set-label string t))
+
+;;;###autoload
+(defun rmail-kill-label (string)
+  "Remove LABEL from labels associated with current RMAIL message.
+Performs completion over known labels when reading."
+  (interactive (list (rmail-read-label "Remove label")))
+  (rmail-set-label string nil))
+
+;;;###autoload
+(defun rmail-read-label (prompt)
+  (let ((result
+	 (completing-read (concat prompt
+				  (if rmail-last-label
+				      (concat " (default "
+					      (symbol-name rmail-last-label)
+					      "): ")
+				    ": "))
+			  rmail-label-obarray
+			  nil
+			  nil)))
+    (if (string= result "")
+	rmail-last-label
+      (setq rmail-last-label (rmail-make-label result)))))
+
+(defun rmail-set-label (label state &optional msg)
+  "Set LABEL as present or absent according to STATE in message MSG."
+  (with-current-buffer rmail-buffer
+    (rmail-maybe-set-message-counters)
+    (if (not msg) (setq msg rmail-current-message))
+    ;; Force recalculation of summary for this message.
+    (aset rmail-summary-vector (1- msg) nil)
+    (let (attr-index)
+      ;; Is this label an attribute?
+      (dotimes (i (length rmail-attr-array))
+	(if (string= (cadr (aref rmail-attr-array i)) label)
+	    (setq attr-index i)))
+      (if attr-index
+	  ;; If so, set it as an attribute.
+	  (rmail-set-attribute attr-index state msg)
+	;; Is this keyword already present in msg's keyword list?
+	(let* ((header (rmail-get-header rmail-keyword-header msg))
+	       (regexp (concat ", " (regexp-quote (symbol-name label)) ","))
+	       (present (string-match regexp (concat ", " header ","))))
+	  ;; If current state is not correct,
+	  (unless (eq present state)
+	    ;; either add it or delete it.
+	    (rmail-set-header
+	     rmail-keyword-header msg
+	     (if state
+		 ;; Add this keyword at the end.
+		 (if (and header (not (string= header "")))
+		     (concat header ", " (symbol-name label))
+		   (symbol-name label))
+	       ;; Delete this keyword.
+	       (let ((before (substring header 0
+					(max 0 (- (match-beginning 0) 2))))
+		     (after (substring header
+				       (min (length header)
+					    (- (match-end 0) 1)))))
+		 (cond ((string= before "")
+			after)
+		       ((string= after "")
+			before)
+		       (t (concat before ", " after)))))))))
+      (if (= msg rmail-current-message)
+	  (rmail-display-labels)))))
+
+;; Motion on messages with keywords.
+
+;;;###autoload
+(defun rmail-previous-labeled-message (n labels)
+  "Show previous message with one of the labels LABELS.
+LABELS should be a comma-separated list of label names.
+If LABELS is empty, the last set of labels specified is used.
+With prefix argument N moves backward N messages with these labels."
+  (interactive "p\nsMove to previous msg with labels: ")
+  (rmail-next-labeled-message (- n) labels))
+
+(declare-function mail-comma-list-regexp "mail-utils" (labels))
+
+;;;###autoload
+(defun rmail-next-labeled-message (n labels)
+  "Show next message with one of the labels LABELS.
+LABELS should be a comma-separated list of label names.
+If LABELS is empty, the last set of labels specified is used.
+With prefix argument N moves forward N messages with these labels."
+  (interactive "p\nsMove to next msg with labels: ")
+  (if (string= labels "")
+      (setq labels rmail-last-multi-labels))
+  (or labels
+      (error "No labels to find have been specified previously"))
+  (set-buffer rmail-buffer)
+  (setq rmail-last-multi-labels labels)
+  (rmail-maybe-set-message-counters)
+  (let ((lastwin rmail-current-message)
+	(current rmail-current-message)
+	(regexp (concat ", ?\\("
+			(mail-comma-list-regexp labels)
+			"\\),")))
+    (while (and (> n 0) (< current rmail-total-messages))
+      (setq current (1+ current))
+      (if (string-match regexp (rmail-get-labels current))
+	  (setq lastwin current n (1- n))))
+    (while (and (< n 0) (> current 1))
+      (setq current (1- current))
+      (if (string-match regexp (rmail-get-labels current))
+	  (setq lastwin current n (1+ n))))
+    (if (< n 0)
+	(error "No previous message with labels %s" labels)
+      (if (> n 0)
+	  (error "No following message with labels %s" labels)
+	(rmail-show-message lastwin)))))
+
+(provide 'rmailkwd)
+
+;; Local Variables:
+;; change-log-default-name: "ChangeLog.rmail"
+;; End:
+
+;; arch-tag: 1149979c-8e47-4333-9629-cf3dc887a6a7
+;;; rmailkwd.el ends here
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lisp/mail/rmailmm.el	Thu Jan 22 17:09:23 2009 +0000
@@ -0,0 +1,410 @@
+;;; rmailmm.el --- MIME decoding and display stuff for RMAIL
+
+;; Copyright (C) 2006, 2007, 2008, 2009  Free Software Foundation, Inc.
+
+;; Maintainer: FSF
+;; Keywords: mail
+
+;; 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 3 of the License, 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.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; Essentially based on the design of Alexander Pohoyda's MIME
+;; extensions (mime-display.el and mime.el).  To use, copy a complete
+;; message into a new buffer and call (mime-show t).
+
+;; To use:
+
+;; (autoload 'rmail-mime "rmailmm"
+;;   "Show MIME message." t)
+;; (add-hook 'rmail-mode-hook
+;;           (lambda ()
+;;             (define-key rmail-mode-map (kbd "v")
+;;               'rmail-mime)))
+
+;;; Code:
+
+(require 'rmail)
+(require 'mail-parse)
+
+;;; Variables
+
+(defcustom rmail-mime-media-type-handlers-alist
+  '(("multipart/.*" rmail-mime-multipart-handler)
+    ("text/.*" rmail-mime-text-handler)
+    ("text/\\(x-\\)?patch" rmail-mime-bulk-handler)
+    ("application/pgp-signature" rmail-mime-application/pgp-signature-handler)
+    ("\\(image\\|audio\\|video\\|application\\)/.*" rmail-mime-bulk-handler))
+  "Alist of media type handlers, also known as agents.
+Every handler is a list of type (string symbol) where STRING is a
+regular expression to match the media type with and SYMBOL is a
+function to run.  Handlers should return a non-nil value if the
+job is done."
+  :type 'list
+  :group 'mime)
+
+(defcustom rmail-mime-attachment-dirs-alist
+  '(("text/.*" "~/Documents")
+    ("image/.*" "~/Pictures")
+    (".*" "~/Desktop" "~" "/tmp"))
+  "Default directories to save attachments into.
+Each media type may have it's own list of directories in order of
+preference.  The first existing directory in the list will be
+used."
+  :type 'list
+  :group 'mime)
+
+(defvar rmail-mime-total-number-of-bulk-attachments 0
+  "A total number of attached bulk bodyparts in the message.  If more than 3,
+offer a way to save all attachments at once.")
+(put 'rmail-mime-total-number-of-bulk-attachments 'permanent-local t)
+
+;;; Buttons
+
+(defun rmail-mime-save (button)
+  "Save the attachment using info in the BUTTON."
+  (let* ((filename (button-get button 'filename))
+	 (directory (button-get button 'directory))
+	 (data (button-get button 'data)))
+    (while (file-exists-p (expand-file-name filename directory))
+      (let* ((f (file-name-sans-extension filename))
+	     (i 1))
+	(when (string-match "-\\([0-9]+\\)$" f)
+	  (setq i (1+ (string-to-number (match-string 1 f)))
+		f (substring f 0 (match-beginning 0))))
+	(setq filename (concat f "-" (number-to-string i) "."
+			       (file-name-extension filename)))))
+    (setq filename (expand-file-name
+		    (read-file-name (format "Save as (default: %s): " filename)
+				    directory
+				    (expand-file-name filename directory))
+		    directory))
+    (when (file-regular-p filename)
+      (error (message "File `%s' already exists" filename)))
+    (with-temp-file filename
+      (set-buffer-file-coding-system 'no-conversion)
+      (insert data))))
+
+(define-button-type 'rmail-mime-save
+  'action 'rmail-mime-save)
+
+;;; Handlers
+
+(defun rmail-mime-text-handler (content-type
+				content-disposition
+				content-transfer-encoding)
+  "Handle the current buffer as a plain text MIME part."
+  (let* ((charset (cdr (assq 'charset (cdr content-type))))
+	 (coding-system (when charset
+			  (intern (downcase charset)))))
+    (when (coding-system-p coding-system)
+      (decode-coding-region (point-min) (point-max) coding-system))))
+
+(defun test-rmail-mime-handler ()
+  "Test of a mail using no MIME parts at all."
+  (let ((mail "To: alex@gnu.org
+Content-Type: text/plain; charset=koi8-r
+Content-Transfer-Encoding: 8bit
+MIME-Version: 1.0
+
+\372\304\322\301\327\323\324\327\325\312\324\305\41"))
+    (switch-to-buffer (get-buffer-create "*test*"))
+    (erase-buffer)
+    (set-buffer-multibyte nil)
+    (insert mail)
+    (rmail-mime-show t)
+    (set-buffer-multibyte t)))
+
+(defun rmail-mime-bulk-handler (content-type
+				content-disposition
+				content-transfer-encoding)
+  "Handle the current buffer as an attachment to download."
+  (setq rmail-mime-total-number-of-bulk-attachments
+	(1+ rmail-mime-total-number-of-bulk-attachments))
+  ;; Find the default directory for this media type
+  (let* ((directory (catch 'directory
+		      (dolist (entry rmail-mime-attachment-dirs-alist)
+			(when (string-match (car entry) (car content-type))
+			  (dolist (dir (cdr entry))
+			    (when (file-directory-p dir)
+			      (throw 'directory dir)))))))
+	 (filename (or (cdr (assq 'name (cdr content-type)))
+		       (cdr (assq 'filename (cdr content-disposition)))
+		       "noname"))
+	 (label (format "\nAttached %s file: " (car content-type)))
+	 (data (buffer-string)))
+    (delete-region (point-min) (point-max))
+    (insert label)
+    (insert-button filename
+		   :type 'rmail-mime-save
+		   'filename filename
+		   'directory directory
+		   'data data)))
+
+(defun test-rmail-mime-bulk-handler ()
+  "Test of a mail used as an example in RFC 2183."
+  (let ((mail "Content-Type: image/jpeg
+Content-Disposition: attachment; filename=genome.jpeg;
+  modification-date=\"Wed, 12 Feb 1997 16:29:51 -0500\";
+Content-Description: a complete map of the human genome
+Content-Transfer-Encoding: base64
+
+iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAMAAABg3Am1AAAABGdBTUEAALGPC/xhBQAAAAZQ
+TFRF////AAAAVcLTfgAAAPZJREFUeNq9ldsOwzAIQ+3//+l1WlvA5ZLsoUiTto4TB+ISoAjy
++ITfRBfcAmgRFFeAm+J6uhdKdFhFWUgDkFsK0oUp/9G2//Kj7Jx+5tSKOdBscgUYiKHRS/me
+WATQdRUvAK0Bnmshmtn79PpaLBbbOZkjKvRnjRZoRswOkG1wFchKew2g9wXVJVZL/m4+B+vv
+9AxQQR2Q33SgAYJzzVACdAWjAfRYzYFO9n6SLnydtQHSMxYDMAKqZ/8FS/lTK+zuq3CtK64L
+UDwbgUEAUmk2Zyg101d6PhCDySgAvTvDgKiuOrc4dLxUb7UMnhGIexyI+d6U+ABuNAP4Simx
+lgAAAABJRU5ErkJggg==
+"))
+    (switch-to-buffer (get-buffer-create "*test*"))
+    (erase-buffer)
+    (insert mail)
+    (rmail-mime-show)))
+
+(defun rmail-mime-multipart-handler (content-type
+				     content-disposition
+				     content-transfer-encoding)
+  "Handle the current buffer as a multipart MIME body.
+The current buffer should be narrowed to the body.  CONTENT-TYPE,
+CONTENT-DISPOSITION, and CONTENT-TRANSFER-ENCODING are the values
+of the respective parsed headers.  See `rmail-mime-handle' for their
+format."
+  ;; Some MUAs start boundaries with "--", while it should start
+  ;; with "CRLF--", as defined by RFC 2046:
+  ;;    The boundary delimiter MUST occur at the beginning of a line,
+  ;;    i.e., following a CRLF, and the initial CRLF is considered to
+  ;;    be attached to the boundary delimiter line rather than part
+  ;;    of the preceding part.
+  ;; We currently don't handle that.
+  (let ((boundary (cdr (assq 'boundary content-type)))
+	beg end next)
+    (unless boundary
+      (rmail-mm-get-boundary-error-message
+       "No boundary defined" content-type content-disposition
+       content-transfer-encoding))
+    (setq boundary (concat "\n--" boundary))
+    ;; Hide the body before the first bodypart
+    (goto-char (point-min))
+    (when (and (search-forward boundary nil t)
+	       (looking-at "[ \t]*\n"))
+      (delete-region (point-min) (match-end 0)))
+    ;; Reset the counter
+    (setq rmail-mime-total-number-of-bulk-attachments 0)
+    ;; Loop over all body parts, where beg points at the beginning of
+    ;; the part and end points at the end of the part.  next points at
+    ;; the beginning of the next part.
+    (setq beg (point-min))
+    (while (search-forward boundary nil t)
+      (setq end (match-beginning 0))
+      ;; If this is the last boundary according to RFC 2046, hide the
+      ;; epilogue, else hide the boundary only.  Use a marker for
+      ;; `next' because `rmail-mime-show' may change the buffer.
+      (cond ((looking-at "--[ \t]*\n")
+	     (setq next (point-max-marker)))
+	    ((looking-at "[ \t]*\n")
+	     (setq next (copy-marker (match-end 0))))
+	    (t
+	     (rmail-mm-get-boundary-error-message
+	      "Malformed boundary" content-type content-disposition
+	      content-transfer-encoding)))
+      (delete-region end next)
+      ;; Handle the part.
+      (save-match-data
+	(save-excursion
+	  (save-restriction
+	    (narrow-to-region beg end)
+	    (rmail-mime-show))))
+      (setq beg next)
+      (goto-char beg))))
+
+(defun test-rmail-mime-multipart-handler ()
+  "Test of a mail used as an example in RFC 2046."
+  (let ((mail "From: Nathaniel Borenstein <nsb@bellcore.com>
+To: Ned Freed <ned@innosoft.com>
+Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)
+Subject: Sample message
+MIME-Version: 1.0
+Content-type: multipart/mixed; boundary=\"simple boundary\"
+
+This is the preamble.  It is to be ignored, though it
+is a handy place for composition agents to include an
+explanatory note to non-MIME conformant readers.
+
+--simple boundary
+
+This is implicitly typed plain US-ASCII text.
+It does NOT end with a linebreak.
+--simple boundary
+Content-type: text/plain; charset=us-ascii
+
+This is explicitly typed plain US-ASCII text.
+It DOES end with a linebreak.
+
+--simple boundary--
+
+This is the epilogue.  It is also to be ignored."))
+    (switch-to-buffer (get-buffer-create "*test*"))
+    (erase-buffer)
+    (insert mail)
+    (rmail-mime-show t)))
+
+;;; Main code
+
+(defun rmail-mime-handle (content-type
+			  content-disposition
+			  content-transfer-encoding)
+  "Handle the current buffer as a MIME part.
+The current buffer should be narrowed to the respective body, and
+point should be at the beginning of the body.
+
+CONTENT-TYPE, CONTENT-DISPOSITION, and CONTENT-TRANSFER-ENCODING
+are the values of the respective parsed headers.  The parsed
+headers for CONTENT-TYPE and CONTENT-DISPOSITION have the form
+
+  \(VALUE . ALIST)
+
+In other words:
+
+  \(VALUE (ATTRIBUTE . VALUE) (ATTRIBUTE . VALUE) ...)
+
+VALUE is a string and ATTRIBUTE is a symbol.
+
+Consider the following header, for example:
+
+Content-Type: multipart/mixed;
+	boundary=\"----=_NextPart_000_0104_01C617E4.BDEC4C40\"
+
+The parsed header value:
+
+\(\"multipart/mixed\"
+  \(\"boundary\" . \"----=_NextPart_000_0104_01C617E4.BDEC4C40\"))"
+  ;; Handle the content transfer encodings we know.  Unknown transfer
+  ;; encodings will be passed on to the various handlers.
+  (cond ((string= content-transfer-encoding "base64")
+	 (when (ignore-errors
+		 (base64-decode-region (point) (point-max)))
+	   (setq content-transfer-encoding nil)))
+	((string= content-transfer-encoding "quoted-printable")
+	 (quoted-printable-decode-region (point) (point-max))
+	 (setq content-transfer-encoding nil))
+	((string= content-transfer-encoding "8bit")
+	 ;; FIXME: Is this the correct way?
+	 (set-buffer-multibyte nil)))
+  ;; Inline stuff requires work.  Attachments are handled by the bulk
+  ;; handler.
+  (if (string= "inline" (car content-disposition))
+      (let ((stop nil))
+	(dolist (entry rmail-mime-media-type-handlers-alist)
+	  (when (and (string-match (car entry) (car content-type)) (not stop))
+	    (progn
+	      (setq stop (funcall (cadr entry) content-type
+				  content-disposition
+				  content-transfer-encoding))))))
+    ;; Everything else is an attachment.
+    (rmail-mime-bulk-handler content-type
+		       content-disposition
+		       content-transfer-encoding)))
+
+(defun rmail-mime-show (&optional show-headers)
+  "Handle the current buffer as a MIME message.
+If SHOW-HEADERS is non-nil, then the headers of the current part
+will shown as usual for a MIME message.  The headers are also
+shown for the content type message/rfc822.  This function will be
+called recursively if multiple parts are available.
+
+The current buffer must contain a single message.  It will be
+modified."
+  (let ((end (point-min))
+	content-type
+	content-transfer-encoding
+	content-disposition)
+    ;; `point-min' returns the beginning and `end' points at the end
+    ;; of the headers.
+    (goto-char (point-min))
+    ;; If we're showing a part without headers, then it will start
+    ;; with a newline.
+    (if (eq (char-after) ?\n)
+	(setq end (1+ (point)))
+      (when (search-forward "\n\n" nil t)
+	(setq end (match-end 0))
+	(save-restriction
+	  (narrow-to-region (point-min) end)
+	  ;; FIXME: Default disposition of the multipart entities should
+	  ;; be inherited.
+	  (setq content-type
+		(mail-fetch-field "Content-Type")
+		content-transfer-encoding
+		(mail-fetch-field "Content-Transfer-Encoding")
+		content-disposition
+		(mail-fetch-field "Content-Disposition")))))
+    (if content-type
+	(setq content-type (mail-header-parse-content-type
+			    content-type))
+      ;; FIXME: Default "message/rfc822" in a "multipart/digest"
+      ;; according to RFC 2046.
+      (setq content-type '("text/plain")))
+    (setq content-disposition
+	  (if content-disposition
+	      (mail-header-parse-content-disposition content-disposition)
+	    ;; If none specified, we are free to choose what we deem
+	    ;; suitable according to RFC 2183.  We like inline.
+	    '("inline")))
+    ;; Unrecognized disposition types are to be treated like
+    ;; attachment according to RFC 2183.
+    (unless (member (car content-disposition) '("inline" "attachment"))
+      (setq content-disposition '("attachment")))
+    ;; Hide headers and handle the part.
+    (save-restriction
+      (cond ((string= (car content-type) "message/rfc822")
+	     (narrow-to-region end (point-max)))
+	    ((not show-headers)
+	     (delete-region (point-min) end)))
+      (rmail-mime-handle content-type content-disposition
+			 content-transfer-encoding))))
+
+(defun rmail-mime ()
+  "Copy buffer contents to a temporary buffer and handle MIME.
+This calls `rmail-mime-show' to do the real job."
+  (interactive)
+  (rmail-swap-buffers-maybe)
+  (let ((data (with-current-buffer rmail-buffer
+		(save-restriction
+		  (widen)
+		  (buffer-substring
+		   (rmail-msgbeg rmail-current-message)
+		   (rmail-msgend rmail-current-message)))))
+	(buf (get-buffer-create "*RMAIL*")))
+    (set-buffer buf)
+    (let ((inhibit-read-only t))
+      (erase-buffer)
+      (insert data)
+      (rmail-mime-show t))
+    (view-buffer buf)))
+
+(defun rmail-mm-get-boundary-error-message (message type disposition encoding)
+  "Return MESSAGE with more information on the main mime components."
+  (error "%s; type: %s; disposition: %s; encoding: %s"
+	 message type disposition encoding))
+
+(provide 'rmailmm)
+
+;; Local Variables:
+;; change-log-default-name: "ChangeLog.rmail"
+;; End:
+
+;; arch-tag: 3f2c5e5d-1aef-4512-bc20-fd737c9d5dd9
+;;; rmailmm.el ends here
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lisp/mail/rmailmsc.el	Thu Jan 22 17:09:23 2009 +0000
@@ -0,0 +1,66 @@
+;;; rmailmsc.el --- miscellaneous support functions for the RMAIL mail reader
+
+;; Copyright (C) 1985, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+;;   2009  Free Software Foundation, Inc.
+
+;; Maintainer: FSF
+;; Keywords: mail
+
+;; 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 3 of the License, 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.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;;; Code:
+
+(eval-when-compile
+  (require 'rmail))
+
+(defvar rmail-current-message)
+(defvar rmail-inbox-list)
+
+(declare-function mail-parse-comma-list "mail-utils" ())
+(declare-function rmail-show-message "rmail" (&optional msg))
+
+;;;###autoload
+(defun set-rmail-inbox-list (file-name)
+  "Set the inbox list of the current RMAIL file to FILE-NAME.
+You can specify one file name, or several names separated by commas.
+If FILE-NAME is empty, remove any existing inbox list."
+  (interactive "sSet mailbox list to (comma-separated list of filenames): ")
+  (unless (eq major-mode 'rmail-mode)
+    (error "set-rmail-inbox-list works only for an Rmail file"))
+  (let ((inbox-list
+	 (with-temp-buffer
+	   (insert file-name)
+	   (goto-char (point-min))
+	   (nreverse (mail-parse-comma-list)))))
+    (when (or (not rmail-inbox-list)
+	      (y-or-n-p (concat "Replace "
+				(mapconcat 'identity
+					   rmail-inbox-list
+					   ", ")
+				"? ")))
+      (message "Setting the inbox list for %s for this session"
+	       (file-name-nondirectory (buffer-file-name)))
+      (setq rmail-inbox-list inbox-list)))
+  (rmail-show-message rmail-current-message))
+
+;; Local Variables:
+;; change-log-default-name: "ChangeLog.rmail"
+;; End:
+
+;; arch-tag: 94614a62-2a0a-4e25-bac9-06f461ed4c60
+;;; rmailmsc.el ends here
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lisp/mail/rmailout.el	Thu Jan 22 17:09:23 2009 +0000
@@ -0,0 +1,602 @@
+;;; rmailout.el --- "RMAIL" mail reader for Emacs: output message to a file
+
+;; Copyright (C) 1985, 1987, 1993, 1994, 2001, 2002, 2003, 2004, 2005,
+;;   2006, 2007, 2008, 2009  Free Software Foundation, Inc.
+
+;; Maintainer: FSF
+;; Keywords: mail
+
+;; 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 3 of the License, 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.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;;; Code:
+
+(require 'rmail)
+(provide 'rmailout)
+
+;;;###autoload
+(defcustom rmail-output-decode-coding nil
+  "*If non-nil, do coding system decoding when outputting message as Babyl."
+  :type '(choice (const :tag "on" t)
+		 (const :tag "off" nil))
+  :group 'rmail)
+
+;;;###autoload
+(defcustom rmail-output-file-alist nil
+  "*Alist matching regexps to suggested output Rmail files.
+This is a list of elements of the form (REGEXP . NAME-EXP).
+The suggestion is taken if REGEXP matches anywhere in the message buffer.
+NAME-EXP may be a string constant giving the file name to use,
+or more generally it may be any kind of expression that returns
+a file name as a string."
+  :type '(repeat (cons regexp
+		       (choice :value ""
+			       (string :tag "File Name")
+			       sexp)))
+  :group 'rmail-output)
+
+(defun rmail-output-read-file-name ()
+  "Read the file name to use for `rmail-output'.
+Set `rmail-default-file' to this name as well as returning it."
+  (let ((default-file
+	  (let (answer tail)
+	    (setq tail rmail-output-file-alist)
+	    ;; Suggest a file based on a pattern match.
+	    (while (and tail (not answer))
+	      (save-excursion
+		(goto-char (point-min))
+		(if (re-search-forward (car (car tail)) nil t)
+		    (setq answer (eval (cdr (car tail)))))
+		(setq tail (cdr tail))))
+	    ;; If no suggestion, use same file as last time.
+	    (or answer rmail-default-file))))
+    (let ((read-file
+	   (expand-file-name
+	    (read-file-name
+	     (concat "Output message to mail file (default "
+		     (file-name-nondirectory default-file)
+		     "): ")
+	     (file-name-directory default-file)
+	     (abbreviate-file-name default-file))
+	    (file-name-directory default-file))))
+      (setq rmail-default-file
+	    (if (file-directory-p read-file)
+		(expand-file-name (file-name-nondirectory default-file)
+				  read-file)
+	      (expand-file-name
+	       (or read-file (file-name-nondirectory default-file))
+	       (file-name-directory default-file)))))))
+
+;;;###autoload
+(defcustom rmail-fields-not-to-output nil
+  "*Regexp describing fields to exclude when outputting a message to a file."
+  :type '(choice (const :tag "None" nil)
+		 regexp)
+  :group 'rmail-output)
+
+;; Delete from the buffer header fields we don't want output.
+;; Buffer should be pre-narrowed to the header.
+;; PRESERVE is a regexp for fields NEVER to delete.
+(defun rmail-delete-unwanted-fields (preserve)
+  (if rmail-fields-not-to-output
+      (save-excursion
+	(goto-char (point-min))
+	(while (re-search-forward rmail-fields-not-to-output nil t)
+	  (beginning-of-line)
+	  (unless (looking-at preserve)
+	    (delete-region (point)
+			   (progn (forward-line 1) (point))))))))
+
+(defun rmail-output-as-babyl (file-name nomsg)
+  "Convert the current buffer's text to Babyl and output to FILE-NAME.
+It alters the current buffer's text, so it should be a temp buffer."
+  (let ((coding-system-for-write
+	 'emacs-mule-unix))
+    (save-restriction
+      (goto-char (point-min))
+      (search-forward "\n\n" nil 'move)
+      (narrow-to-region (point-min) (point))
+      (if rmail-fields-not-to-output
+	  (rmail-delete-unwanted-fields nil)))
+
+    ;; Convert to Babyl format.
+    (rmail-convert-to-babyl-format)
+    ;; Write it into the file, or its buffer.
+    (let ((buf (find-buffer-visiting file-name))
+	  (tembuf (current-buffer)))
+      (if (null buf)
+	  (write-region (point-min) (point-max) file-name t nomsg)
+	(if (eq buf (current-buffer))
+	    (error "Can't output message to same file it's already in"))
+	;; File has been visited, in buffer BUF.
+	(set-buffer buf)
+	(let ((inhibit-read-only t)
+	      (msg (with-no-warnings
+		     (and (boundp 'rmail-current-message)
+			  rmail-current-message))))
+	  ;; If MSG is non-nil, buffer is in RMAIL mode.
+	  (if msg
+	      (rmail-output-to-r-mail-buffer tembuf msg)
+	    ;; Output file not in rmail mode => just insert at the end.
+	    (narrow-to-region (point-min) (1+ (buffer-size)))
+	    (goto-char (point-max))
+	    (insert-buffer-substring tembuf)))))))
+
+;; When Rmail is really installed, if we delete or rename the old Rmail
+;; we should do likewise with this function.
+
+(defun rmail-output-to-r-mail-buffer (tembuf msg)
+  "Copy msg in TEMBUF from BEG to END into this old R-mail BABYL buffer.
+Do what is necessary to make babyl R-mail know about the new message.
+Then display message number MSG."
+  (with-no-warnings
+    ;; Turn on Auto Save mode, if it's off in this
+    ;; buffer but enabled by default.
+    (and (not buffer-auto-save-file-name)
+	 auto-save-default
+	 (auto-save-mode t))
+    (rmail-maybe-set-message-counters)
+    (widen)
+    (narrow-to-region (point-max) (point-max))
+    (insert-buffer-substring tembuf)
+    (goto-char (point-min))
+    (widen)
+    (search-backward "\n\^_")
+    (narrow-to-region (point) (point-max))
+    (rmail-count-new-messages t)
+    (if (rmail-summary-exists)
+	(rmail-select-summary
+	 (rmail-update-summary)))
+    (rmail-show-message msg)))
+
+(defun rmail-convert-to-babyl-format ()
+  (let ((count 0) (start (point-min))
+	(case-fold-search nil)
+	(buffer-undo-list t))
+    (goto-char (point-min))
+    (save-restriction
+      (unless (looking-at "^From ")
+	(error "Invalid mbox message"))
+      (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
+      (rmail-nuke-pinhead-header)
+      ;; Decode base64 or quoted printable contents, Rmail style.
+      (let* ((header-end (save-excursion
+			   (and (re-search-forward "\n\n" nil t)
+				(1- (point)))))
+	     (case-fold-search t)
+	     (quoted-printable-header-field-end
+	      (save-excursion
+		(re-search-forward
+		 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
+		 header-end t)))
+	     (base64-header-field-end
+	      (and
+	       ;; Don't decode non-text data.
+	       (save-excursion
+		 (re-search-forward
+		  "^content-type:\\(\n?[\t ]\\)\\(text\\|message\\)/"
+		  header-end t))
+	       (save-excursion
+		 (re-search-forward
+		  "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
+		  header-end t)))))
+
+	(goto-char (point-max))
+	(if quoted-printable-header-field-end
+	    (save-excursion
+	      (unless (mail-unquote-printable-region
+		       header-end (point) nil t t)
+		(message "Malformed MIME quoted-printable message"))
+	      ;; Change "quoted-printable" to "8bit",
+	      ;; to reflect the decoding we just did.
+	      (goto-char quoted-printable-header-field-end)
+	      (delete-region (point) (search-backward ":"))
+	      (insert ": 8bit")))
+	(if base64-header-field-end
+	    (save-excursion
+	      (when (condition-case nil
+			(progn
+			  (base64-decode-region
+			   (1+ header-end)
+			   (save-excursion
+			     ;; Prevent base64-decode-region
+			     ;; from removing newline characters.
+			     (skip-chars-backward "\n\t ")
+			     (point)))
+			  t)
+		      (error nil))
+		;; Change "base64" to "8bit", to reflect the
+		;; decoding we just did.
+		(goto-char base64-header-field-end)
+		(delete-region (point) (search-backward ":"))
+		(insert ": 8bit")))))
+      ;; Transform anything within the message text
+      ;; that might appear to be the end of a Babyl-format message.
+      (save-excursion
+	(save-restriction
+	  (narrow-to-region start (point))
+	  (goto-char (point-min))
+	  (while (search-forward "\n\^_" nil t) ; single char
+	    (replace-match "\n^_"))))		; 2 chars: "^" and "_"
+      ;; This is for malformed messages that don't end in newline.
+      ;; There shouldn't be any, but some users say occasionally
+      ;; there are some.
+      (or (bolp) (newline))
+      (insert ?\^_)
+      (setq last-coding-system-used nil)
+      ;; Decode coding system, following specs in the message header,
+      ;; and record what coding system was decoded.
+      (if rmail-output-decode-coding
+	  (let ((mime-charset
+		 (if (save-excursion
+		       (goto-char start)
+		       (search-forward "\n\n" nil t)
+		       (let ((case-fold-search t))
+			 (re-search-backward
+			  rmail-mime-charset-pattern
+			  start t)))
+		     (intern (downcase (match-string 1))))))
+	    (rmail-decode-region start (point) mime-charset)))
+      (save-excursion
+	(goto-char start)
+	(forward-line 3)
+	(insert "X-Coding-System: "
+		(symbol-name last-coding-system-used)
+		"\n")))))
+
+;; Delete the "From ..." line, creating various other headers with
+;; information from it if they don't already exist.  Now puts the
+;; original line into a mail-from: header line for debugging and for
+;; use by the rmail-output function.
+(defun rmail-nuke-pinhead-header ()
+  (save-excursion
+    (save-restriction
+      (let ((start (point))
+  	    (end (progn
+		   (condition-case ()
+		       (search-forward "\n\n")
+		     (error
+		      (goto-char (point-max))
+		      (insert "\n\n")))
+		   (point)))
+	    has-from has-date)
+	(narrow-to-region start end)
+	(let ((case-fold-search t))
+	  (goto-char start)
+	  (setq has-from (search-forward "\nFrom:" nil t))
+	  (goto-char start)
+	  (setq has-date (and (search-forward "\nDate:" nil t) (point)))
+	  (goto-char start))
+	(let ((case-fold-search nil))
+	  (if (re-search-forward (concat "^" rmail-unix-mail-delimiter) nil t)
+	      (replace-match
+		(concat
+		  "Mail-from: \\&"
+		  ;; Keep and reformat the date if we don't
+		  ;;  have a Date: field.
+		  (if has-date
+		      ""
+		    (concat
+		     "Date: \\2, \\4 \\3 \\9 \\5 "
+
+		     ;; The timezone could be matched by group 7 or group 10.
+		     ;; If neither of them matched, assume EST, since only
+		     ;; Easterners would be so sloppy.
+		     ;; It's a shame the substitution can't use "\\10".
+		     (cond
+		      ((/= (match-beginning 7) (match-end 7)) "\\7")
+		      ((/= (match-beginning 10) (match-end 10))
+		       (buffer-substring (match-beginning 10)
+					 (match-end 10)))
+		      (t "EST"))
+		     "\n"))
+		  ;; Keep and reformat the sender if we don't
+		  ;; have a From: field.
+		  (if has-from
+		      ""
+		    "From: \\1\n"))
+		t)))))))
+
+(defun rmail-output-as-mbox (file-name nomsg &optional as-seen)
+  "Convert the current buffer's text to mbox Babyl and output to FILE-NAME.
+It alters the current buffer's text, so call with a temp buffer current.
+If FILE-NAME is visited, output into its buffer instead.
+AS-SEEN is non-nil if we are copying the message \"as seen\"."
+  (let ((case-fold-search t)
+	mail-from mime-version content-type)
+
+    ;; Preserve the Mail-From and MIME-Version fields
+    ;; even if they have been pruned.
+    (search-forward "\n\n" nil 'move)
+    (narrow-to-region (point-min) (point))
+
+    (rmail-delete-unwanted-fields
+     (if rmail-enable-mime "Mail-From"
+       "Mail-From\\|MIME-Version\\|Content-type"))
+
+    (widen)
+
+    ;; Make sure message ends with blank line.
+    (goto-char (point-max))
+    (unless (bolp)
+       (insert "\n"))
+    (unless (looking-back "\n\n")
+      (insert "\n"))
+
+    ;; Generate a From line from other header fields
+    ;; if necessary.
+    (goto-char (point-min))
+    (unless (looking-at "From ")
+      (insert "From "
+	      (mail-strip-quoted-names
+	       (save-excursion
+		 (save-restriction
+		   (goto-char (point-min))
+		   (narrow-to-region
+		    (point)
+		    (or (search-forward "\n\n" nil)
+			(point-max)))
+		   (or (mail-fetch-field "from")
+		       (mail-fetch-field "really-from")
+		       (mail-fetch-field "sender")
+		       "unknown"))))
+	      " " (current-time-string) "\n"))
+
+    (let ((buf (find-buffer-visiting file-name))
+	  (tembuf (current-buffer)))
+      (if (null buf)
+	  (let ((coding-system-for-write 'raw-text-unix))
+	    (write-region (point-min) (point-max) file-name t nomsg))
+	(if (eq buf (current-buffer))
+	    (error "Can't output message to same file it's already in"))
+	;; File has been visited, in buffer BUF.
+	(set-buffer buf)
+ 	(let ((inhibit-read-only t)
+	      (msg (and (boundp 'rmail-current-message)
+			rmail-current-message)))
+	  (and msg as-seen
+	       (error "Can't output \"as seen\" to a visited Rmail file"))
+	  (if msg
+	      (rmail-output-to-rmail-buffer tembuf msg)
+	    ;; Output file not in Rmail mode => just insert at the end.
+	    (narrow-to-region (point-min) (1+ (buffer-size)))
+	    (goto-char (point-max))
+	    (insert-buffer-substring tembuf)))))))
+
+;; Called only if rmail-summary-exists, which means rmailsum is loaded.
+(declare-function rmail-update-summary "rmailsum" (&rest ignore))
+
+(defun rmail-output-to-rmail-buffer (tembuf msg)
+  "Copy msg in TEMBUF from BEG to END into this Rmail buffer.
+Do what is necessary to make Rmail know about the new message.
+Then display message number MSG."
+  (save-excursion
+    (rmail-swap-buffers-maybe)
+    ;; Turn on Auto Save mode, if it's off in this
+    ;; buffer but enabled by default.
+    (and (not buffer-auto-save-file-name)
+	 auto-save-default
+	 (auto-save-mode t))
+    (rmail-maybe-set-message-counters)
+    (narrow-to-region (point-max) (point-max))
+    (insert-buffer-substring tembuf)
+    (rmail-count-new-messages t)
+    (if (rmail-summary-exists)
+	(rmail-select-summary
+	 (rmail-update-summary)))
+    (rmail-show-message msg)))
+
+;;; There are functions elsewhere in Emacs that use this function;
+;;; look at them before you change the calling method.
+;;;###autoload
+(defun rmail-output (file-name &optional count noattribute from-gnus)
+  "Append this message to mail file FILE-NAME.
+This works with both mbox format and Babyl format files,
+outputting in the appropriate format for each.
+The default file name comes from `rmail-default-file',
+which is updated to the name you use in this command.
+
+A prefix argument COUNT says to output that many consecutive messages,
+starting with the current one.  Deleted messages are skipped and don't count.
+When called from Lisp code, COUNT may be omitted and defaults to 1.
+
+This command always outputs the complete message header,
+even the header display is currently pruned.
+
+The optional third argument NOATTRIBUTE, if non-nil, says not
+to set the `filed' attribute, and not to display a message.
+
+The optional fourth argument FROM-GNUS is set when called from GNUS."
+  (interactive
+   (list (rmail-output-read-file-name)
+	 (prefix-numeric-value current-prefix-arg)))
+  (or count (setq count 1))
+  (setq file-name
+	(expand-file-name file-name
+			  (and rmail-default-file
+			       (file-name-directory rmail-default-file))))
+
+  ;; Warn about creating new file.
+  (or (find-buffer-visiting file-name)
+      (file-exists-p file-name)
+      (yes-or-no-p
+       (concat "\"" file-name "\" does not exist, create it? "))
+      (error "Output file does not exist"))
+
+  (set-buffer rmail-buffer)
+
+  (let ((orig-count count)
+	(case-fold-search t)
+	(tembuf (get-buffer-create " rmail-output"))
+	(babyl-format
+	 (and (file-readable-p file-name) (mail-file-babyl-p file-name))))
+
+    (unwind-protect
+	(while (> count 0)
+	  (with-current-buffer rmail-buffer
+	    (let (cur beg end)
+	      (setq beg (rmail-msgbeg rmail-current-message)
+		    end (rmail-msgend rmail-current-message))
+	      ;; All access to the buffer's local variables is now finished...
+	      (save-excursion
+		;; ... so it is ok to go to a different buffer.
+		(if (rmail-buffers-swapped-p) (set-buffer rmail-view-buffer))
+		(setq cur (current-buffer))
+		(save-restriction
+		  (widen)
+		  (with-current-buffer tembuf
+		    (insert-buffer-substring cur beg end)
+		    ;; Convert the text to one format or another and output.
+		    (if babyl-format
+			(rmail-output-as-babyl file-name (if noattribute 'nomsg))
+		      (rmail-output-as-mbox file-name 
+					    (if noattribute 'nomsg))))))))
+
+	  ;; Mark message as "filed".
+	  (unless noattribute
+	    (rmail-set-attribute rmail-filed-attr-index t))
+
+	  (setq count (1- count))
+
+	  (or from-gnus
+	      (let ((next-message-p
+		     (if rmail-delete-after-output
+			 (rmail-delete-forward)
+		       (if (> count 0)
+			   (rmail-next-undeleted-message 1))))
+		    (num-appended (- orig-count count)))
+		(if (and (> count 0) (not next-message-p))
+		    (error "Only %d message%s appended" num-appended
+			   (if (= num-appended 1) "" "s"))))))
+      (kill-buffer tembuf))))
+
+(defun rmail-output-as-seen (file-name &optional count noattribute from-gnus)
+  "Append this message to system-inbox-format mail file named FILE-NAME.
+A prefix argument COUNT says to output that many consecutive messages,
+starting with the current one.  Deleted messages are skipped and don't count.
+When called from Lisp code, COUNT may be omitted and defaults to 1.
+
+This outputs the message header as you see it.
+
+The default file name comes from `rmail-default-file',
+which is updated to the name you use in this command.
+
+The optional third argument NOATTRIBUTE, if non-nil, says not
+to set the `filed' attribute, and not to display a message.
+
+The optional fourth argument FROM-GNUS is set when called from GNUS."
+  (interactive
+   (list (rmail-output-read-file-name)
+	 (prefix-numeric-value current-prefix-arg)))
+  (or count (setq count 1))
+  (setq file-name
+	(expand-file-name file-name
+			  (and rmail-default-file
+			       (file-name-directory rmail-default-file))))
+  (set-buffer rmail-buffer)
+
+  ;; Warn about creating new file.
+  (or (find-buffer-visiting file-name)
+      (file-exists-p file-name)
+      (yes-or-no-p
+       (concat "\"" file-name "\" does not exist, create it? "))
+      (error "Output file does not exist"))
+
+    (if (and (file-readable-p file-name) (mail-file-babyl-p file-name))
+	(error "Cannot output `as seen' to a Babyl file"))
+
+  (let ((orig-count count)
+	(case-fold-search t)
+	(tembuf (get-buffer-create " rmail-output")))
+
+    (unwind-protect
+	(while (> count 0)
+	  (let (cur beg end)
+	    ;; If operating from whole-mbox buffer, get message bounds.
+	    (if (not (rmail-buffers-swapped-p))
+		(setq beg (rmail-msgbeg rmail-current-message)
+		      end (rmail-msgend rmail-current-message)))
+	    ;; All access to the buffer's local variables is now finished...
+	    (save-excursion
+	      (setq cur (current-buffer))
+	      (save-restriction
+		(widen)
+		;; If operating from the view buffer, get the bounds.
+		(unless beg
+		  (setq beg (point-min)
+			end (point-max)))
+
+		(with-current-buffer tembuf
+		  (insert-buffer-substring cur beg end)
+		  ;; Convert the text to one format or another and output.
+		  (rmail-output-as-mbox file-name 
+					(if noattribute 'nomsg)
+					t)))))
+
+	  ;; Mark message as "filed".
+	  (unless noattribute
+	    (rmail-set-attribute rmail-filed-attr-index t))
+
+	  (setq count (1- count))
+
+	  (or from-gnus
+	      (let ((next-message-p
+		     (if rmail-delete-after-output
+			 (rmail-delete-forward)
+		       (if (> count 0)
+			   (rmail-next-undeleted-message 1))))
+		    (num-appended (- orig-count count)))
+		(if (and (> count 0) (not next-message-p))
+		    (error "Only %d message%s appended" num-appended
+			   (if (= num-appended 1) "" "s"))))))
+      (kill-buffer tembuf))))
+
+
+;;;###autoload
+(defun rmail-output-body-to-file (file-name)
+  "Write this message body to the file FILE-NAME.
+FILE-NAME defaults, interactively, from the Subject field of the message."
+  (interactive
+   (let ((default-file
+	   (or (mail-fetch-field "Subject")
+	       rmail-default-body-file)))
+     (list (setq rmail-default-body-file
+		 (read-file-name
+		  "Output message body to file: "
+		  (and default-file (file-name-directory default-file))
+		  default-file
+		  nil default-file)))))
+  (setq file-name
+	(expand-file-name file-name
+			  (and rmail-default-body-file
+			       (file-name-directory rmail-default-body-file))))
+  (save-excursion
+    (goto-char (point-min))
+    (search-forward "\n\n")
+    (and (file-exists-p file-name)
+	 (not (y-or-n-p (format "File %s exists; overwrite? " file-name)))
+	 (error "Operation aborted"))
+    (write-region (point) (point-max) file-name))
+  (if rmail-delete-after-output
+      (rmail-delete-forward)))
+
+;; Local Variables:
+;; change-log-default-name: "ChangeLog.rmail"
+;; End:
+
+;; arch-tag: 4059abf0-f249-4be4-8e0d-602d370d01d1
+;;; rmailout.el ends here
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lisp/mail/rmailsort.el	Thu Jan 22 17:09:23 2009 +0000
@@ -0,0 +1,245 @@
+;;; rmailsort.el --- Rmail: sort messages
+
+;; Copyright (C) 1990, 1993, 1994, 2001, 2002, 2003, 2004,
+;;   2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
+
+;; Author: Masanobu UMEDA <umerin@mse.kyutech.ac.jp>
+;; Maintainer: FSF
+;; Keywords: mail
+
+;; 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 3 of the License, 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.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;;; Code:
+
+(eval-when-compile
+  (require 'mail-utils)
+  (require 'sort)
+  (require 'rmail))
+
+(autoload 'timezone-make-date-sortable "timezone")
+
+(declare-function rmail-update-summary "rmailsum" (&rest ignore))
+
+;; Sorting messages in Rmail buffer
+
+;;;###autoload
+(defun rmail-sort-by-date (reverse)
+  "Sort messages of current Rmail file by date.
+If prefix argument REVERSE is non-nil, sort them in reverse order."
+  (interactive "P")
+  (rmail-sort-messages reverse
+		       (function
+			(lambda (msg)
+			  (rmail-make-date-sortable
+			   (rmail-get-header "Date" msg))))))
+
+;;;###autoload
+(defun rmail-sort-by-subject (reverse)
+  "Sort messages of current Rmail file by subject.
+If prefix argument REVERSE is non-nil, sort them in reverse order."
+  (interactive "P")
+  (rmail-sort-messages reverse
+		       (function
+			(lambda (msg)
+			  (let ((key (or (rmail-get-header "Subject" msg) ""))
+				(case-fold-search t))
+			    ;; Remove `Re:'
+			    (if (string-match "^\\(re:[ \t]*\\)*" key)
+				(substring key (match-end 0))
+			      key))))))
+
+;;;###autoload
+(defun rmail-sort-by-author (reverse)
+  "Sort messages of current Rmail file by author.
+If prefix argument REVERSE is non-nil, sort them in reverse order."
+  (interactive "P")
+  (rmail-sort-messages reverse
+		       (function
+			(lambda (msg)
+			  (downcase	;Canonical name
+			   (mail-strip-quoted-names
+			    (or (rmail-get-header "From" msg)
+				(rmail-get-header "Sender" msg) "")))))))
+
+;;;###autoload
+(defun rmail-sort-by-recipient (reverse)
+  "Sort messages of current Rmail file by recipient.
+If prefix argument REVERSE is non-nil, sort them in reverse order."
+  (interactive "P")
+  (rmail-sort-messages reverse
+		       (function
+			(lambda (msg)
+			  (downcase	;Canonical name
+			   (mail-strip-quoted-names
+			    (or (rmail-get-header "To" msg)
+				(rmail-get-header "Apparently-To" msg) "")
+			    ))))))
+
+;;;###autoload
+(defun rmail-sort-by-correspondent (reverse)
+  "Sort messages of current Rmail file by other correspondent.
+If prefix argument REVERSE is non-nil, sort them in reverse order."
+  (interactive "P")
+  (rmail-sort-messages reverse
+		       (function
+			(lambda (msg)
+			  (rmail-select-correspondent
+			   msg
+			   '("From" "Sender" "To" "Apparently-To"))))))
+
+(defun rmail-select-correspondent (msg fields)
+  (let ((ans ""))
+    (while (and fields (string= ans ""))
+      (setq ans
+	    ;; NB despite the name, this lives in mail-utils.el.
+	    (rmail-dont-reply-to
+	     (mail-strip-quoted-names
+	      (or (rmail-get-header (car fields) msg) ""))))
+      (setq fields (cdr fields)))
+    ans))
+
+;;;###autoload
+(defun rmail-sort-by-lines (reverse)
+  "Sort messages of current Rmail file by number of lines.
+If prefix argument REVERSE is non-nil, sort them in reverse order."
+  (interactive "P")
+  (rmail-sort-messages reverse
+		       (function
+			(lambda (msg)
+			  (count-lines (rmail-msgbeg msg)
+				       (rmail-msgend msg))))))
+
+;;;###autoload
+(defun rmail-sort-by-labels (reverse labels)
+  "Sort messages of current Rmail file by labels.
+If prefix argument REVERSE is non-nil, sort them in reverse order.
+KEYWORDS is a comma-separated list of labels."
+  (interactive "P\nsSort by labels: ")
+  (or (string-match "[^ \t]" labels)
+      (error "No labels specified"))
+  (setq labels (concat (substring labels (match-beginning 0)) ","))
+  (let (labelvec)
+    (while (string-match "[ \t]*,[ \t]*" labels)
+      (setq labelvec (cons
+		      (concat ", ?\\("
+			      (substring labels 0 (match-beginning 0))
+			      "\\),")
+		      labelvec))
+      (setq labels (substring labels (match-end 0))))
+    (setq labelvec (apply 'vector (nreverse labelvec)))
+    (rmail-sort-messages reverse
+			 (function
+			  (lambda (msg)
+			    (let ((n 0))
+			      (while (and (< n (length labelvec))
+					  (not (rmail-message-labels-p
+						msg (aref labelvec n))))
+				(setq n (1+ n)))
+			      n))))))
+
+;; Basic functions
+
+(defun rmail-sort-messages (reverse keyfun)
+  "Sort messages of current Rmail file.
+If 1st argument REVERSE is non-nil, sort them in reverse order.
+2nd argument KEYFUN is called with a message number, and should return a key."
+  (with-current-buffer rmail-buffer
+    (let ((return-to-point
+	   (if (rmail-buffers-swapped-p)
+	       (point)))
+	  (predicate nil)			;< or string-lessp
+	  (sort-lists nil))
+      (rmail-swap-buffers-maybe)
+      (message "Finding sort keys...")
+      (widen)
+      (let ((msgnum 1))
+	(while (>= rmail-total-messages msgnum)
+	  (setq sort-lists
+		(cons (list (funcall keyfun msgnum) ;Make sorting key
+			    (eq rmail-current-message msgnum) ;True if current
+			    (aref rmail-message-vector msgnum)
+			    (aref rmail-message-vector (1+ msgnum)))
+		      sort-lists))
+	  (if (zerop (% msgnum 10))
+	      (message "Finding sort keys...%d" msgnum))
+	  (setq msgnum (1+ msgnum))))
+      (or reverse (setq sort-lists (nreverse sort-lists)))
+      ;; Decide predicate: < or string-lessp
+      (if (numberp (car (car sort-lists))) ;Is a key numeric?
+	  (setq predicate (function <))
+	(setq predicate (function string-lessp)))
+      (setq sort-lists
+	    (sort sort-lists
+		  (function
+		   (lambda (a b)
+		     (funcall predicate (car a) (car b))))))
+      (if reverse (setq sort-lists (nreverse sort-lists)))
+      ;; Now we enter critical region.  So, keyboard quit is disabled.
+      (message "Reordering messages...")
+      (let ((inhibit-quit t)		;Inhibit quit
+	    (inhibit-read-only t)
+	    (current-message nil)
+	    (msgnum 1)
+	    (msginfo nil))
+	;; There's little hope that we can easily undo after that.
+	(buffer-disable-undo (current-buffer))
+	(goto-char (rmail-msgbeg 1))
+	;; To force update of all markers,
+	;; keep the new copies separated from the remaining old messages.
+	(insert-before-markers ?Z)
+	(backward-char 1)
+	;; Now reorder messages.
+	(dolist (msginfo sort-lists)
+	  ;; Swap two messages.
+	  (insert-buffer-substring
+	   (current-buffer) (nth 2 msginfo) (nth 3 msginfo))
+	  ;; The last message may not have \n\n after it.
+	  (unless (bobp)
+	    (insert "\n"))
+	  (unless (looking-back "\n\n")
+	    (insert "\n"))
+	  (delete-region (nth 2 msginfo) (nth 3 msginfo))
+	  ;; Is current message?
+	  (if (nth 1 msginfo)
+	      (setq current-message msgnum))
+	  (if (zerop (% msgnum 10))
+	      (message "Reordering messages...%d" msgnum))
+	  (setq msgnum (1+ msgnum)))
+	;; Delete the dummy separator Z inserted before.
+	(delete-char 1)
+	(setq quit-flag nil)
+	(rmail-set-message-counters)
+	(rmail-show-message current-message)
+	(if return-to-point
+	    (goto-char return-to-point))
+	(if (rmail-summary-exists)
+	    (rmail-select-summary (rmail-update-summary)))))))
+
+(defun rmail-make-date-sortable (date)
+  "Make DATE sortable using the function string-lessp."
+  ;; Assume the default time zone is GMT.
+  (timezone-make-date-sortable date "GMT" "GMT"))
+
+(provide 'rmailsort)
+
+;; Local Variables:
+;; change-log-default-name: "ChangeLog.rmail"
+;; End:
+
+;; arch-tag: 665da245-f6a7-4115-ad8c-ba19216988d5
+;;; rmailsort.el ends here
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lisp/mail/rmailsum.el	Thu Jan 22 17:09:23 2009 +0000
@@ -0,0 +1,1765 @@
+;;; rmailsum.el --- make summary buffers for the mail reader
+
+;; Copyright (C) 1985, 1993, 1994, 1995, 1996, 2000, 2001, 2002, 2003,
+;;   2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
+
+;; Maintainer: FSF
+;; Keywords: mail
+
+;; 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 3 of the License, 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.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; Extended by Bob Weiner of Motorola
+;;   Provided all commands from rmail-mode in rmail-summary-mode and made key
+;;   bindings in both modes wholly compatible.
+
+;;; Code:
+
+(defvar msgnum)
+
+;; For rmail-select-summary
+(require 'rmail)
+
+;;;###autoload
+(defcustom rmail-summary-scroll-between-messages t
+  "*Non-nil means Rmail summary scroll commands move between messages."
+  :type 'boolean
+  :group 'rmail-summary)
+
+;;;###autoload
+(defcustom rmail-summary-line-count-flag t
+  "*Non-nil means Rmail summary should show the number of lines in each message."
+  :type 'boolean
+  :group 'rmail-summary)
+
+(defvar rmail-summary-font-lock-keywords
+  '(("^.....D.*" . font-lock-string-face)			; Deleted.
+    ("^.....-.*" . font-lock-type-face)				; Unread.
+    ;; Neither of the below will be highlighted if either of the above are:
+    ("^.....[^D-] \\(......\\)" 1 font-lock-keyword-face)	; Date.
+    ("{ \\([^\n}]+\\) }" 1 font-lock-comment-face))		; Labels.
+  "Additional expressions to highlight in Rmail Summary mode.")
+
+(defvar rmail-summary-redo
+  "(FUNCTION . ARGS) to regenerate this Rmail summary buffer.")
+
+(defvar rmail-summary-overlay nil)
+(put 'rmail-summary-overlay 'permanent-local t)
+
+(defvar rmail-summary-mode-map nil)
+
+;; Entry points for making a summary buffer.
+
+;; Regenerate the contents of the summary
+;; using the same selection criterion as last time.
+;; M-x revert-buffer in a summary buffer calls this function.
+(defun rmail-update-summary (&rest ignore)
+  (apply (car rmail-summary-redo) (cdr rmail-summary-redo)))
+
+;;;###autoload
+(defun rmail-summary ()
+  "Display a summary of all messages, one line per message."
+  (interactive)
+  (rmail-new-summary "All" '(rmail-summary) nil)
+  (unless (get-buffer-window rmail-buffer)
+    (rmail-summary-beginning-of-message)))
+
+;;;###autoload
+(defun rmail-summary-by-labels (labels)
+  "Display a summary of all messages with one or more LABELS.
+LABELS should be a string containing the desired labels, separated by commas."
+  (interactive "sLabels to summarize by: ")
+  (if (string= labels "")
+      (setq labels (or rmail-last-multi-labels
+		       (error "No label specified"))))
+  (setq rmail-last-multi-labels labels)
+  (rmail-new-summary (concat "labels " labels)
+		     (list 'rmail-summary-by-labels labels)
+		     'rmail-message-labels-p
+		     (concat ", \\(" (mail-comma-list-regexp labels) "\\),")))
+
+;;;###autoload
+(defun rmail-summary-by-recipients (recipients &optional primary-only)
+  "Display a summary of all messages with the given RECIPIENTS.
+Normally checks the To, From and Cc fields of headers;
+but if PRIMARY-ONLY is non-nil (prefix arg given),
+ only look in the To and From fields.
+RECIPIENTS is a string of regexps separated by commas."
+  (interactive "sRecipients to summarize by: \nP")
+  (rmail-new-summary
+   (concat "recipients " recipients)
+   (list 'rmail-summary-by-recipients recipients primary-only)
+   'rmail-message-recipients-p
+   (mail-comma-list-regexp recipients) primary-only))
+
+(defun rmail-message-recipients-p (msg recipients &optional primary-only)
+  (rmail-apply-in-message msg 'rmail-message-recipients-p-1
+			  recipients primary-only))
+
+(defun rmail-message-recipients-p-1 (recipients &optional primary-only)
+  (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
+  (or (string-match recipients (or (mail-fetch-field "To") ""))
+      (string-match recipients (or (mail-fetch-field "From") ""))
+      (if (not primary-only)
+	  (string-match recipients (or (mail-fetch-field "Cc") "")))))
+
+;;;###autoload
+(defun rmail-summary-by-regexp (regexp)
+  "Display a summary of all messages according to regexp REGEXP.
+If the regular expression is found in the header of the message
+\(including in the date and other lines, as well as the subject line),
+Emacs will list the header line in the RMAIL-summary."
+  (interactive "sRegexp to summarize by: ")
+  (if (string= regexp "")
+      (setq regexp (or rmail-last-regexp
+			 (error "No regexp specified"))))
+  (setq rmail-last-regexp regexp)
+  (rmail-new-summary (concat "regexp " regexp)
+		     (list 'rmail-summary-by-regexp regexp)
+		     'rmail-message-regexp-p
+                     regexp))
+
+(defun rmail-message-regexp-p (msg regexp)
+  "Return t, if for message number MSG, regexp REGEXP matches in the header."
+  (rmail-apply-in-message msg 'rmail-message-regexp-p-1 msg regexp))
+
+(defun rmail-message-regexp-p-1 (msg regexp)
+  (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
+  (if rmail-enable-mime
+      (funcall rmail-search-mime-header-function msg regexp (point))
+    (re-search-forward regexp nil t)))
+
+;;;###autoload
+(defun rmail-summary-by-topic (subject &optional whole-message)
+  "Display a summary of all messages with the given SUBJECT.
+Normally checks the Subject field of headers;
+but if WHOLE-MESSAGE is non-nil (prefix arg given),
+ look in the whole message.
+SUBJECT is a string of regexps separated by commas."
+  (interactive
+   (let* ((subject (rmail-simplified-subject))
+	  (prompt (concat "Topics to summarize by (regexp"
+			  (if subject ", default current subject" "")
+			  "): ")))
+     (list (read-string prompt nil nil subject) current-prefix-arg)))
+  (rmail-new-summary
+   (concat "about " subject)
+   (list 'rmail-summary-by-topic subject whole-message)
+   'rmail-message-subject-p
+   (mail-comma-list-regexp subject) whole-message))
+
+(defun rmail-message-subject-p (msg subject &optional whole-message)
+  (if whole-message
+      (rmail-apply-in-message msg 're-search-forward subject nil t)
+    (string-match subject (rmail-simplified-subject msg))))
+
+;;;###autoload
+(defun rmail-summary-by-senders (senders)
+  "Display a summary of all messages with the given SENDERS.
+SENDERS is a string of names separated by commas."
+  (interactive "sSenders to summarize by: ")
+  (rmail-new-summary
+   (concat "senders " senders)
+   (list 'rmail-summary-by-senders senders)
+   'rmail-message-senders-p
+   (mail-comma-list-regexp senders)))
+
+(defun rmail-message-senders-p (msg senders)
+  (string-match senders (or (rmail-get-header "From" msg) "")))
+
+;; General making of a summary buffer.
+
+(defvar rmail-summary-symbol-number 0)
+
+(defvar rmail-new-summary-line-count)
+
+(defun rmail-new-summary (desc redo func &rest args)
+  "Create a summary of selected messages.
+DESC makes part of the mode line of the summary buffer. REDO is form ...
+For each message, FUNC is applied to the message number and ARGS...
+and if the result is non-nil, that message is included.
+nil for FUNCTION means all messages."
+  (message "Computing summary lines...")
+  (unless rmail-buffer
+    (error "No RMAIL buffer found"))
+  (let (mesg was-in-summary)
+    (if (eq major-mode 'rmail-summary-mode)
+	(setq was-in-summary t))
+    (with-current-buffer rmail-buffer
+      (setq mesg rmail-current-message
+	    rmail-summary-buffer (rmail-new-summary-1 desc redo func args)))
+    ;; Now display the summary buffer and go to the right place in it.
+    (unless was-in-summary
+      (if (and (one-window-p)
+	       pop-up-windows
+	       (not pop-up-frames))
+	  ;; If there is just one window, put the summary on the top.
+	  (progn
+	    (split-window (selected-window) rmail-summary-window-size)
+	    (select-window (next-window (frame-first-window)))
+	    (pop-to-buffer rmail-summary-buffer)
+	    ;; If pop-to-buffer did not use that window, delete that
+	    ;; window.  (This can happen if it uses another frame.)
+	    (if (not (eq rmail-summary-buffer
+			 (window-buffer (frame-first-window))))
+		(delete-other-windows)))
+	(pop-to-buffer rmail-summary-buffer))
+      (set-buffer rmail-buffer)
+      ;; This is how rmail makes the summary buffer reappear.
+      ;; We do this here to make the window the proper size.
+      (rmail-select-summary nil)
+      (set-buffer rmail-summary-buffer))
+    (rmail-summary-goto-msg mesg t t)
+    (rmail-summary-construct-io-menu)
+    (message "Computing summary lines...done")))
+
+(defun rmail-new-summary-1 (description form function args)
+  "Filter messages to obtain summary lines.
+DESCRIPTION is added to the mode line.
+
+Return the summary buffer by invoking FUNCTION on each message
+passing the message number and ARGS...
+
+REDO is a form ...
+
+The current buffer must be a Rmail buffer either containing a
+collection of mbox formatted messages or displaying a single
+message."
+  (let ((summary-msgs ())
+	(rmail-new-summary-line-count 0)
+	(sumbuf (rmail-get-create-summary-buffer)))
+    ;; Scan the messages, getting their summary strings
+    ;; and putting the list of them in SUMMARY-MSGS.
+    (let ((msgnum 1)
+	  (main-buffer (current-buffer))
+	  (total rmail-total-messages)
+	  (inhibit-read-only t))
+      (save-excursion
+	;; Go where the mbox text is.
+	(if (rmail-buffers-swapped-p)
+	    (set-buffer rmail-view-buffer))
+	(let ((old-min (point-min-marker))
+	      (old-max (point-max-marker)))
+	  (unwind-protect
+	      ;; Can't use save-restriction here; that doesn't work if we
+	      ;; plan to modify text outside the original restriction.
+	      (save-excursion
+		(widen)
+		(goto-char (point-min))
+		(while (>= total msgnum)
+		  ;; Go back to the Rmail buffer so
+		  ;; so FUNCTION and rmail-get-summary can see its local vars.
+		  (with-current-buffer main-buffer
+		    ;; First test whether to include this message.
+		    (if (or (null function)
+			    (apply function msgnum args))
+			(setq summary-msgs
+			      (cons (cons msgnum (rmail-get-summary msgnum))
+				    summary-msgs))))
+		  (setq msgnum (1+ msgnum))
+		  ;; Provide a periodic User progress message.
+		  (if (zerop (% rmail-new-summary-line-count 10))
+		      (message "Computing summary lines...%d"
+			       rmail-new-summary-line-count)))
+		(setq summary-msgs (nreverse summary-msgs)))
+	    (narrow-to-region old-min old-max)))))
+
+    ;; Temporarily, while summary buffer is unfinished,
+    ;; we "don't have" a summary.
+    ;;
+    ;; I have not a clue what this clause is doing.  If you read this
+    ;; chunk of code and have a clue, then please email that clue to
+    ;; pmr@pajato.com
+    (setq rmail-summary-buffer nil)
+    (if rmail-enable-mime
+	(with-current-buffer rmail-buffer
+	  (setq rmail-summary-buffer nil)))
+
+    (save-excursion
+      (let ((rbuf (current-buffer))
+	    (total rmail-total-messages))
+	(set-buffer sumbuf)
+	;; Set up the summary buffer's contents.
+	(let ((buffer-read-only nil))
+	  (erase-buffer)
+	  (while summary-msgs
+	    (princ (cdr (car summary-msgs)) sumbuf)
+	    (setq summary-msgs (cdr summary-msgs)))
+	  (goto-char (point-min)))
+	;; Set up the rest of its state and local variables.
+	(setq buffer-read-only t)
+	(rmail-summary-mode)
+	(make-local-variable 'minor-mode-alist)
+	(setq minor-mode-alist (list (list t (concat ": " description))))
+	(setq rmail-buffer rbuf
+	      rmail-summary-redo form
+	      rmail-total-messages total)))
+    sumbuf))
+
+(defun rmail-get-create-summary-buffer ()
+  "Obtain a summary buffer by re-using an existing summary
+buffer, or by creating a new summary buffer."
+  (if (and rmail-summary-buffer (buffer-name rmail-summary-buffer))
+      rmail-summary-buffer
+    (generate-new-buffer (concat (buffer-name) "-summary"))))
+	    
+
+;; Low levels of generating a summary.
+
+(defun rmail-get-summary (msgnum)
+  "Return the summary line for message MSGNUM.
+The mbox buffer must be current when you call this function
+even if its text is swapped.
+
+If the message has a summary line already, it will be stored in
+the message as a header and simply returned, otherwise the
+summary line is created, saved in the message header, cached and
+returned.
+
+The current buffer contains the unrestricted message collection."
+  (let ((line (aref rmail-summary-vector (1- msgnum))))
+    (unless line
+      ;; Register a summary line for MSGNUM.
+      (setq rmail-new-summary-line-count (1+ rmail-new-summary-line-count)
+	    line (rmail-create-summary-line msgnum))
+      ;; Cache the summary line for use during this Rmail session.
+      (aset rmail-summary-vector (1- msgnum) line))
+    line))
+
+;;;###autoload
+(defcustom rmail-summary-line-decoder (function identity)
+  "*Function to decode a Rmail summary line.
+It receives the summary line for one message as a string
+and should return the decoded string.
+
+By default, it is `identity', which returns the string unaltered."
+  :type 'function
+  :group 'rmail-summary)
+
+(defun rmail-create-summary-line (msgnum)
+  "Return the summary line for message MSGNUM.
+Obtain the message summary from the header if it is available
+otherwise create it and store it in the message header.
+
+The mbox buffer must be current when you call this function
+even if its text is swapped."
+  (let ((beg (rmail-msgbeg msgnum))
+	(end (rmail-msgend msgnum))
+	(deleted (rmail-message-deleted-p msgnum))
+	(unseen (rmail-message-unseen-p msgnum))
+	lines)
+    (save-excursion
+      ;; Switch to the buffer that has the whole mbox text.
+      (if (rmail-buffers-swapped-p)
+	  (set-buffer rmail-view-buffer))
+      ;; Now we can compute the line count.
+      (if rmail-summary-line-count-flag
+	  (setq lines (count-lines beg end)))
+
+      ;; Narrow to the message header.
+      (save-excursion
+	(goto-char beg)
+	(if (search-forward "\n\n" end t)
+	    (save-restriction
+	      (narrow-to-region beg (point))
+	      ;; Generate a status line from the message.
+	      (rmail-create-summary msgnum deleted unseen lines))
+	  (rmail-error-bad-format msgnum))))))
+
+(defun rmail-get-summary-labels ()
+  "Return a coded string wrapped in curly braces denoting the status labels.
+
+The current buffer must already be narrowed to the message headers for
+the message being processed."
+  (let ((status (mail-fetch-field rmail-attribute-header))
+	(index 0)
+	(result "")
+	char)
+    ;; Strip off the read/unread and the deleted attribute which are
+    ;; handled separately.
+    (setq status
+	  (if status
+	      (concat (substring status 0 1) (substring status 2 6))
+	    ""))
+    (while (< index (length status))
+      (unless (string= "-" (setq char (substring status index (1+ index))))
+	(setq result (concat result char)))
+      (setq index (1+ index)))
+    (when (> (length result) 0)
+      (setq result (concat "{" result "}")))
+    result))
+
+(defun rmail-create-summary (msgnum deleted unseen lines)
+  "Return the summary line for message MSGNUM.
+The current buffer should already be narrowed to the header for that message.
+It could be either buffer, so don't access Rmail local variables.
+DELETED is t if this message is marked deleted.
+UNSEEN is t if it is marked unseen.
+LINES is the number of lines in the message (if we should display that)
+ or else nil."
+  (goto-char (point-min))
+  (let ((line (rmail-header-summary))
+	(labels (rmail-get-summary-labels))
+	pos status prefix basic-start basic-end linecount-string)
+
+    (setq linecount-string
+	  (cond
+	   ((not lines)       " ")
+	   ((<= lines      9) (format "   [%d]" lines))
+	   ((<= lines     99) (format "  [%d]" lines))
+	   ((<= lines    999) (format " [%d]" lines))
+	   ((<= lines   9999) (format "  [%dk]" (/ lines 1000)))
+	   ((<= lines  99999) (format " [%dk]" (/ lines 1000)))
+	   (t                 (format "[%dk]" (/ lines 1000)))))
+
+    (setq status (cond
+		  (deleted ?D)
+		  (unseen ?-)
+		  (t ? ))
+	  prefix (format "%5d%c" msgnum status)
+	  basic-start (car line)
+	  basic-end (cadr line))
+    (funcall rmail-summary-line-decoder
+	     (concat prefix basic-start linecount-string " "
+		     labels basic-end))))
+
+;;;###autoload
+(defcustom rmail-user-mail-address-regexp nil
+  "*Regexp matching user mail addresses.
+If non-nil, this variable is used to identify the correspondent
+when receiving new mail.  If it matches the address of the sender,
+the recipient is taken as correspondent of a mail.
+If nil \(default value\), your `user-login-name' and `user-mail-address'
+are used to exclude yourself as correspondent.
+
+Usually you don't have to set this variable, except if you collect mails
+sent by you under different user names.
+Then it should be a regexp matching your mail addresses.
+
+Setting this variable has an effect only before reading a mail."
+  :type '(choice (const :tag "None" nil) regexp)
+  :group 'rmail-retrieve
+  :version "21.1")
+
+(defun rmail-header-summary ()
+  "Return a message summary based on the message headers.
+The value is a list of two strings, the first and second parts of the summary.
+
+The current buffer must already be narrowed to the message headers for
+the message being processed."
+  (goto-char (point-min))
+  (list
+   (concat (save-excursion
+	     (if (not (re-search-forward "^Date:" nil t))
+		 "      "
+	       (cond ((re-search-forward "\\([^0-9:]\\)\\([0-3]?[0-9]\\)\\([- \t_]+\\)\\([adfjmnos][aceopu][bcglnprtvy]\\)"
+		       (save-excursion (end-of-line) (point)) t)
+		      (format "%2d-%3s"
+			      (string-to-number (buffer-substring
+						 (match-beginning 2)
+						 (match-end 2)))
+			      (buffer-substring
+			       (match-beginning 4) (match-end 4))))
+		     ((re-search-forward "\\([^a-z]\\)\\([adfjmnos][acepou][bcglnprtvy]\\)\\([-a-z \t_]*\\)\\([0-9][0-9]?\\)"
+		       (save-excursion (end-of-line) (point)) t)
+		      (format "%2d-%3s"
+			      (string-to-number (buffer-substring
+						 (match-beginning 4)
+						 (match-end 4)))
+			      (buffer-substring
+			       (match-beginning 2) (match-end 2))))
+		     ((re-search-forward "\\(19\\|20\\)\\([0-9][0-9]\\)-\\([01][0-9]\\)-\\([0-3][0-9]\\)"
+		       (save-excursion (end-of-line) (point)) t)
+		      (format "%2s%2s%2s"
+			      (buffer-substring
+			       (match-beginning 2) (match-end 2))
+			      (buffer-substring
+			       (match-beginning 3) (match-end 3))
+			      (buffer-substring
+			       (match-beginning 4) (match-end 4))))
+		     (t "??????"))))
+	   "  "
+	   (save-excursion
+	     (let* ((from (and (re-search-forward "^From:[ \t]*" nil t)
+			       (mail-strip-quoted-names
+				(buffer-substring
+				 (1- (point))
+				 ;; Get all the lines of the From field
+				 ;; so that we get a whole comment if there is one,
+				 ;; so that mail-strip-quoted-names can discard it.
+				 (let ((opoint (point)))
+				   (while (progn (forward-line 1)
+						 (looking-at "[ \t]")))
+				   ;; Back up over newline, then trailing spaces or tabs
+				   (forward-char -1)
+				   (skip-chars-backward " \t")
+				   (point))))))
+		    len mch lo)
+	       (if (or (null from)
+		       (string-match
+			(or rmail-user-mail-address-regexp
+			    (concat "^\\("
+				    (regexp-quote (user-login-name))
+				    "\\($\\|@\\)\\|"
+				    (regexp-quote
+				     ;; Don't lose if run from init file
+				     ;; where user-mail-address is not
+				     ;; set yet.
+				     (or user-mail-address
+					 (concat (user-login-name) "@"
+						 (or mail-host-address
+						     (system-name)))))
+				    "\\>\\)"))
+			from))
+		   ;; No From field, or it's this user.
+		   (save-excursion
+		     (goto-char (point-min))
+		     (if (not (re-search-forward "^To:[ \t]*" nil t))
+			 nil
+		       (setq from
+			     (concat "to: "
+				     (mail-strip-quoted-names
+				      (buffer-substring
+				       (point)
+				       (progn (end-of-line)
+					      (skip-chars-backward " \t")
+					      (point)))))))))
+	       (if (null from)
+		   "                         "
+		 (setq len (length from))
+		 (setq mch (string-match "[@%]" from))
+		 (format "%25s"
+			 (if (or (not mch) (<= len 25))
+			     (substring from (max 0 (- len 25)))
+			   (substring from
+				      (setq lo (cond ((< (- mch 14) 0) 0)
+						     ((< len (+ mch 11))
+						      (- len 25))
+						     (t (- mch 14))))
+				      (min len (+ lo 25)))))))))
+   (concat (if (re-search-forward "^Subject:" nil t)
+	       (progn (skip-chars-forward " \t")
+		      (buffer-substring (point)
+					(progn (end-of-line)
+					       (point))))
+	     (re-search-forward "[\n][\n]+" nil t)
+	     (buffer-substring (point) (progn (end-of-line) (point))))
+	   "\n")))
+
+;; Simple motion in a summary buffer.
+
+(defun rmail-summary-next-all (&optional number)
+  (interactive "p")
+  (forward-line (if number number 1))
+  ;; It doesn't look nice to move forward past the last message line.
+  (and (eobp) (> number 0)
+       (forward-line -1))
+  (display-buffer rmail-buffer))
+
+(defun rmail-summary-previous-all (&optional number)
+  (interactive "p")
+  (forward-line (- (if number number 1)))
+  ;; It doesn't look nice to move forward past the last message line.
+  (and (eobp) (< number 0)
+       (forward-line -1))
+  (display-buffer rmail-buffer))
+
+(defun rmail-summary-next-msg (&optional number)
+  "Display next non-deleted msg from rmail file.
+With optional prefix argument NUMBER, moves forward this number of non-deleted
+messages, or backward if NUMBER is negative."
+  (interactive "p")
+  (forward-line 0)
+  (and (> number 0) (end-of-line))
+  (let ((count (if (< number 0) (- number) number))
+	(search (if (> number 0) 're-search-forward 're-search-backward))
+	(non-del-msg-found nil))
+    (while (and (> count 0) (setq non-del-msg-found
+				  (or (funcall search "^.....[^D]" nil t)
+				      non-del-msg-found)))
+      (setq count (1- count))))
+  (beginning-of-line)
+  (display-buffer rmail-buffer))
+
+(defun rmail-summary-previous-msg (&optional number)
+  "Display previous non-deleted msg from rmail file.
+With optional prefix argument NUMBER, moves backward this number of
+non-deleted messages."
+  (interactive "p")
+  (rmail-summary-next-msg (- (if number number 1))))
+
+(defun rmail-summary-next-labeled-message (n labels)
+  "Show next message with LABELS.  Defaults to last labels used.
+With prefix argument N moves forward N messages with these labels."
+  (interactive "p\nsMove to next msg with labels: ")
+  (let (msg)
+    (save-excursion
+      (set-buffer rmail-buffer)
+      (rmail-next-labeled-message n labels)
+      (setq msg rmail-current-message))
+    (rmail-summary-goto-msg msg)))
+
+(defun rmail-summary-previous-labeled-message (n labels)
+  "Show previous message with LABELS.  Defaults to last labels used.
+With prefix argument N moves backward N messages with these labels."
+  (interactive "p\nsMove to previous msg with labels: ")
+  (let (msg)
+    (save-excursion
+      (set-buffer rmail-buffer)
+      (rmail-previous-labeled-message n labels)
+      (setq msg rmail-current-message))
+    (rmail-summary-goto-msg msg)))
+
+(defun rmail-summary-next-same-subject (n)
+  "Go to the next message in the summary having the same subject.
+With prefix argument N, do this N times.
+If N is negative, go backwards."
+  (interactive "p")
+  (let ((forward (> n 0))
+	subject i found)
+    (with-current-buffer rmail-buffer
+      (setq subject (rmail-simplified-subject)
+	    i rmail-current-message))
+    (save-excursion
+      (while (and (/= n 0)
+		  (if forward
+		      (not (eobp))
+		    (not (bobp))))
+	(let (done)
+	  (while (and (not done)
+		      (if forward
+			  (not (eobp))
+			(not (bobp))))
+	    ;; Advance thru summary.
+	    (forward-line (if forward 1 -1))
+	    ;; Get msg number of this line.
+	    (setq i (string-to-number
+		     (buffer-substring (point)
+				       (min (point-max) (+ 6 (point))))))
+	    (setq done (string-equal subject (rmail-simplified-subject i))))
+	  (if done (setq found i)))
+	(setq n (if forward (1- n) (1+ n)))))
+    (if found
+	(rmail-summary-goto-msg found)
+      (error "No %s message with same subject"
+	     (if forward "following" "previous")))))
+
+(defun rmail-summary-previous-same-subject (n)
+  "Go to the previous message in the summary having the same subject.
+With prefix argument N, do this N times.
+If N is negative, go forwards instead."
+  (interactive "p")
+  (rmail-summary-next-same-subject (- n)))
+
+;; Delete and undelete summary commands.
+
+(defun rmail-summary-delete-forward (&optional count)
+  "Delete this message and move to next nondeleted one.
+Deleted messages stay in the file until the \\[rmail-expunge] command is given.
+A prefix argument serves as a repeat count;
+a negative argument means to delete and move backward."
+  (interactive "p")
+  (unless (numberp count) (setq count 1))
+  (let (end del-msg
+	    (backward (< count 0)))
+    (while (/= count 0)
+      (rmail-summary-goto-msg)
+      (with-current-buffer rmail-buffer
+	(rmail-delete-message)
+	(setq del-msg rmail-current-message))
+      (rmail-summary-mark-deleted del-msg)
+      (while (and (not (if backward (bobp) (eobp)))
+		  (save-excursion (beginning-of-line)
+				  (looking-at " *[0-9]+D")))
+	(forward-line (if backward -1 1)))
+      ;; It looks ugly to move to the empty line at end of buffer.
+      (and (eobp) (not backward)
+	   (forward-line -1))
+      (setq count
+	    (if (> count 0) (1- count) (1+ count))))))
+
+(defun rmail-summary-delete-backward (&optional count)
+  "Delete this message and move to previous nondeleted one.
+Deleted messages stay in the file until the \\[rmail-expunge] command is given.
+A prefix argument serves as a repeat count;
+a negative argument means to delete and move forward."
+  (interactive "p")
+  (rmail-summary-delete-forward (- count)))
+
+(defun rmail-summary-mark-deleted (&optional n undel)
+  ;; Since third arg is t, this only alters the summary, not the Rmail buf.
+  (and n (rmail-summary-goto-msg n t t))
+  (or (eobp)
+      (not (overlay-get rmail-summary-overlay 'face))
+      (let ((buffer-read-only nil))
+	(skip-chars-forward " ")
+	(skip-chars-forward "[0-9]")
+	(if undel
+	    (if (looking-at "D")
+		(progn (delete-char 1) (insert " ")))
+	  (delete-char 1)
+	  (insert "D"))))
+  (beginning-of-line))
+
+(defun rmail-summary-mark-undeleted (n)
+  (rmail-summary-mark-deleted n t))
+
+(defun rmail-summary-deleted-p (&optional n)
+  (save-excursion
+    (and n (rmail-summary-goto-msg n nil t))
+    (skip-chars-forward " ")
+    (skip-chars-forward "[0-9]")
+    (looking-at "D")))
+
+(defun rmail-summary-undelete (&optional arg)
+  "Undelete current message.
+Optional prefix ARG means undelete ARG previous messages."
+  (interactive "p")
+  (if (/= arg 1)
+      (rmail-summary-undelete-many arg)
+    (let ((buffer-read-only nil)
+	  (opoint (point)))
+      (end-of-line)
+      (cond ((re-search-backward "\\(^ *[0-9]*\\)\\(D\\)" nil t)
+	     (replace-match "\\1 ")
+	     (rmail-summary-goto-msg)
+	     (if rmail-enable-mime
+		 (set-buffer rmail-buffer)
+	       (pop-to-buffer rmail-buffer))
+	     (and (rmail-message-deleted-p rmail-current-message)
+		  (rmail-undelete-previous-message))
+	     (if rmail-enable-mime
+		 (pop-to-buffer rmail-buffer))
+	     (pop-to-buffer rmail-summary-buffer))
+	    (t (goto-char opoint))))))
+
+(defun rmail-summary-undelete-many (&optional n)
+  "Undelete all deleted msgs, optional prefix arg N means undelete N prev msgs."
+  (interactive "P")
+  (save-excursion
+    (set-buffer rmail-buffer)
+    (let* ((init-msg (if n rmail-current-message rmail-total-messages))
+	   (rmail-current-message init-msg)
+	   (n (or n rmail-total-messages))
+	   (msgs-undeled 0))
+      (while (and (> rmail-current-message 0)
+		  (< msgs-undeled n))
+	(if (rmail-message-deleted-p rmail-current-message)
+	    (progn (rmail-set-attribute "deleted" nil)
+		   (setq msgs-undeled (1+ msgs-undeled))))
+	(setq rmail-current-message (1- rmail-current-message)))
+      (set-buffer rmail-summary-buffer)
+      (setq rmail-current-message init-msg msgs-undeled 0)
+      (while (and (> rmail-current-message 0)
+		  (< msgs-undeled n))
+	(if (rmail-summary-deleted-p rmail-current-message)
+	    (progn (rmail-summary-mark-undeleted rmail-current-message)
+		   (setq msgs-undeled (1+ msgs-undeled))))
+	(setq rmail-current-message (1- rmail-current-message))))
+    (rmail-summary-goto-msg)))
+
+;; Rmail Summary mode is suitable only for specially formatted data.
+(put 'rmail-summary-mode 'mode-class 'special)
+
+(defun rmail-summary-mode ()
+  "Rmail Summary Mode is invoked from Rmail Mode by using \\<rmail-mode-map>\\[rmail-summary].
+As commands are issued in the summary buffer, they are applied to the
+corresponding mail messages in the rmail buffer.
+
+All normal editing commands are turned off.
+Instead, nearly all the Rmail mode commands are available,
+though many of them move only among the messages in the summary.
+
+These additional commands exist:
+
+\\[rmail-summary-undelete-many]	Undelete all or prefix arg deleted messages.
+\\[rmail-summary-wipe] Delete the summary and go to the Rmail buffer.
+
+Commands for sorting the summary:
+
+\\[rmail-summary-sort-by-date] Sort by date.
+\\[rmail-summary-sort-by-subject] Sort by subject.
+\\[rmail-summary-sort-by-author] Sort by author.
+\\[rmail-summary-sort-by-recipient] Sort by recipient.
+\\[rmail-summary-sort-by-correspondent] Sort by correspondent.
+\\[rmail-summary-sort-by-lines] Sort by lines.
+\\[rmail-summary-sort-by-labels] Sort by labels."
+  (interactive)
+  (kill-all-local-variables)
+  (setq major-mode 'rmail-summary-mode)
+  (setq mode-name "RMAIL Summary")
+  (setq truncate-lines t)
+  (setq buffer-read-only t)
+  (set-syntax-table text-mode-syntax-table)
+  (make-local-variable 'rmail-buffer)
+  (make-local-variable 'rmail-total-messages)
+  (make-local-variable 'rmail-current-message)
+  (setq rmail-current-message nil)
+  (make-local-variable 'rmail-summary-redo)
+  (setq rmail-summary-redo nil)
+  (make-local-variable 'revert-buffer-function)
+  (make-local-variable 'font-lock-defaults)
+  (setq font-lock-defaults '(rmail-summary-font-lock-keywords t))
+  (rmail-summary-enable)
+  (run-mode-hooks 'rmail-summary-mode-hook))
+
+;; Summary features need to be disabled during edit mode.
+(defun rmail-summary-disable ()
+  (use-local-map text-mode-map)
+  (remove-hook 'post-command-hook 'rmail-summary-rmail-update t)
+  (setq revert-buffer-function nil))
+
+(defun rmail-summary-enable ()
+  (use-local-map rmail-summary-mode-map)
+  (add-hook 'post-command-hook 'rmail-summary-rmail-update nil t)
+  (setq revert-buffer-function 'rmail-update-summary))
+
+(defvar rmail-summary-put-back-unseen nil
+  "Used for communicating between calls to `rmail-summary-rmail-update'.
+If it moves to a message within an Incremental Search, and removes
+the `unseen' attribute from that message, it sets this flag
+so that if the next motion between messages is in the same Incremental
+Search, the `unseen' attribute is restored.")
+
+;; Show in Rmail the message described by the summary line that point is on,
+;; but only if the Rmail buffer is already visible.
+;; This is a post-command-hook in summary buffers.
+(defun rmail-summary-rmail-update ()
+  (let (buffer-read-only)
+    (save-excursion
+      ;; If at end of buffer, pretend we are on the last text line.
+      (if (eobp)
+	  (forward-line -1))
+      (beginning-of-line)
+      (skip-chars-forward " ")
+      (let ((msg-num (string-to-number (buffer-substring
+                                        (point)
+                                        (progn (skip-chars-forward "0-9")
+                                               (point))))))
+	;; Always leave `unseen' removed
+	;; if we get out of isearch mode.
+	;; Don't let a subsequent isearch restore that `unseen'.
+	(if (not isearch-mode)
+	    (setq rmail-summary-put-back-unseen nil))
+
+	(or (eq rmail-current-message msg-num)
+	    (let ((window (get-buffer-window rmail-buffer t))
+		  (owin (selected-window)))
+	      (if isearch-mode
+		  (save-excursion
+		    (set-buffer rmail-buffer)
+		    ;; If we first saw the previous message in this search,
+		    ;; and we have gone to a different message while searching,
+		    ;; put back `unseen' on the former one.
+		    (if rmail-summary-put-back-unseen
+			(rmail-set-attribute "unseen" t
+					     rmail-current-message))
+		    ;; Arrange to do that later, for the new current message,
+		    ;; if it still has `unseen'.
+		    (setq rmail-summary-put-back-unseen
+			  (rmail-message-attr-p msg-num rmail-unseen-attr-index)))
+		(setq rmail-summary-put-back-unseen nil))
+
+	      ;; Go to the desired message.
+	      (setq rmail-current-message msg-num)
+
+	      ;; Update the summary to show the message has been seen.
+	      (if (= (following-char) ?-)
+		  (progn
+		    (delete-char 1)
+		    (insert " ")))
+
+	      (if window
+		  ;; Using save-window-excursion would cause the new value
+		  ;; of point to get lost.
+		  (unwind-protect
+		      (progn
+			(select-window window)
+			(rmail-show-message-maybe msg-num t))
+		    (select-window owin))
+		(if (buffer-name rmail-buffer)
+		    (save-excursion
+		      (set-buffer rmail-buffer)
+		      (rmail-show-message-maybe msg-num t))))))
+	(rmail-summary-update-highlight nil)))))
+
+(defun rmail-summary-save-buffer ()
+  "Save the buffer associated with this RMAIL summary."
+  (interactive)
+  (save-window-excursion
+    (save-excursion
+      (switch-to-buffer rmail-buffer)
+      (save-buffer))))
+
+
+(if rmail-summary-mode-map
+    nil
+  (setq rmail-summary-mode-map (make-keymap))
+  (suppress-keymap rmail-summary-mode-map)
+
+  (define-key rmail-summary-mode-map [mouse-2] 'rmail-summary-mouse-goto-message)
+  (define-key rmail-summary-mode-map "a"      'rmail-summary-add-label)
+  (define-key rmail-summary-mode-map "b"      'rmail-summary-bury)
+  (define-key rmail-summary-mode-map "c"      'rmail-summary-continue)
+  (define-key rmail-summary-mode-map "d"      'rmail-summary-delete-forward)
+  (define-key rmail-summary-mode-map "\C-d"   'rmail-summary-delete-backward)
+  (define-key rmail-summary-mode-map "e"      'rmail-summary-edit-current-message)
+  (define-key rmail-summary-mode-map "f"      'rmail-summary-forward)
+  (define-key rmail-summary-mode-map "g"      'rmail-summary-get-new-mail)
+  (define-key rmail-summary-mode-map "h"      'rmail-summary)
+  (define-key rmail-summary-mode-map "i"      'rmail-summary-input)
+  (define-key rmail-summary-mode-map "j"      'rmail-summary-goto-msg)
+  (define-key rmail-summary-mode-map "\C-m"   'rmail-summary-goto-msg)
+  (define-key rmail-summary-mode-map "k"      'rmail-summary-kill-label)
+  (define-key rmail-summary-mode-map "l"      'rmail-summary-by-labels)
+  (define-key rmail-summary-mode-map "\e\C-h" 'rmail-summary)
+  (define-key rmail-summary-mode-map "\e\C-l" 'rmail-summary-by-labels)
+  (define-key rmail-summary-mode-map "\e\C-r" 'rmail-summary-by-recipients)
+  (define-key rmail-summary-mode-map "\e\C-s" 'rmail-summary-by-regexp)
+  (define-key rmail-summary-mode-map "\e\C-t" 'rmail-summary-by-topic)
+  (define-key rmail-summary-mode-map "m"      'rmail-summary-mail)
+  (define-key rmail-summary-mode-map "\M-m"   'rmail-summary-retry-failure)
+  (define-key rmail-summary-mode-map "n"      'rmail-summary-next-msg)
+  (define-key rmail-summary-mode-map "\en"    'rmail-summary-next-all)
+  (define-key rmail-summary-mode-map "\e\C-n" 'rmail-summary-next-labeled-message)
+  (define-key rmail-summary-mode-map "o"      'rmail-summary-output)
+  (define-key rmail-summary-mode-map "\C-o"   'rmail-summary-output)
+  (define-key rmail-summary-mode-map "p"      'rmail-summary-previous-msg)
+  (define-key rmail-summary-mode-map "\ep"    'rmail-summary-previous-all)
+  (define-key rmail-summary-mode-map "\e\C-p" 'rmail-summary-previous-labeled-message)
+  (define-key rmail-summary-mode-map "q"      'rmail-summary-quit)
+  (define-key rmail-summary-mode-map "Q"      'rmail-summary-wipe)
+  (define-key rmail-summary-mode-map "r"      'rmail-summary-reply)
+  (define-key rmail-summary-mode-map "s"      'rmail-summary-expunge-and-save)
+  (define-key rmail-summary-mode-map "\es"    'rmail-summary-search)
+  (define-key rmail-summary-mode-map "t"      'rmail-summary-toggle-header)
+  (define-key rmail-summary-mode-map "u"      'rmail-summary-undelete)
+  (define-key rmail-summary-mode-map "\M-u"   'rmail-summary-undelete-many)
+  (define-key rmail-summary-mode-map "x"      'rmail-summary-expunge)
+  (define-key rmail-summary-mode-map "w"      'rmail-summary-output-body)
+  (define-key rmail-summary-mode-map "."      'rmail-summary-beginning-of-message)
+  (define-key rmail-summary-mode-map "/"      'rmail-summary-end-of-message)
+  (define-key rmail-summary-mode-map "<"      'rmail-summary-first-message)
+  (define-key rmail-summary-mode-map ">"      'rmail-summary-last-message)
+  (define-key rmail-summary-mode-map " "      'rmail-summary-scroll-msg-up)
+  (define-key rmail-summary-mode-map "\177"   'rmail-summary-scroll-msg-down)
+  (define-key rmail-summary-mode-map "?"      'describe-mode)
+  (define-key rmail-summary-mode-map "\C-c\C-n" 'rmail-summary-next-same-subject)
+  (define-key rmail-summary-mode-map "\C-c\C-p" 'rmail-summary-previous-same-subject)
+  (define-key rmail-summary-mode-map "\C-c\C-s\C-d"
+    'rmail-summary-sort-by-date)
+  (define-key rmail-summary-mode-map "\C-c\C-s\C-s"
+    'rmail-summary-sort-by-subject)
+  (define-key rmail-summary-mode-map "\C-c\C-s\C-a"
+    'rmail-summary-sort-by-author)
+  (define-key rmail-summary-mode-map "\C-c\C-s\C-r"
+    'rmail-summary-sort-by-recipient)
+  (define-key rmail-summary-mode-map "\C-c\C-s\C-c"
+    'rmail-summary-sort-by-correspondent)
+  (define-key rmail-summary-mode-map "\C-c\C-s\C-l"
+    'rmail-summary-sort-by-lines)
+  (define-key rmail-summary-mode-map "\C-c\C-s\C-k"
+    'rmail-summary-sort-by-labels)
+  (define-key rmail-summary-mode-map "\C-x\C-s" 'rmail-summary-save-buffer)
+  )
+
+;;; Menu bar bindings.
+
+(define-key rmail-summary-mode-map [menu-bar] (make-sparse-keymap))
+
+(define-key rmail-summary-mode-map [menu-bar classify]
+  (cons "Classify" (make-sparse-keymap "Classify")))
+
+(define-key rmail-summary-mode-map [menu-bar classify output-menu]
+  '("Output (Rmail Menu)..." . rmail-summary-output-menu))
+
+(define-key rmail-summary-mode-map [menu-bar classify input-menu]
+  '("Input Rmail File (menu)..." . rmail-input-menu))
+
+(define-key rmail-summary-mode-map [menu-bar classify input-menu]
+  '(nil))
+
+(define-key rmail-summary-mode-map [menu-bar classify output-menu]
+  '(nil))
+
+(define-key rmail-summary-mode-map [menu-bar classify output-body]
+  '("Output (body)..." . rmail-summary-output-body))
+
+(define-key rmail-summary-mode-map [menu-bar classify output-inbox]
+  '("Output (inbox)..." . rmail-summary-output))
+
+(define-key rmail-summary-mode-map [menu-bar classify output]
+  '("Output (Rmail)..." . rmail-summary-output))
+
+(define-key rmail-summary-mode-map [menu-bar classify kill-label]
+  '("Kill Label..." . rmail-summary-kill-label))
+
+(define-key rmail-summary-mode-map [menu-bar classify add-label]
+  '("Add Label..." . rmail-summary-add-label))
+
+(define-key rmail-summary-mode-map [menu-bar summary]
+  (cons "Summary" (make-sparse-keymap "Summary")))
+
+(define-key rmail-summary-mode-map [menu-bar summary senders]
+  '("By Senders..." . rmail-summary-by-senders))
+
+(define-key rmail-summary-mode-map [menu-bar summary labels]
+  '("By Labels..." . rmail-summary-by-labels))
+
+(define-key rmail-summary-mode-map [menu-bar summary recipients]
+  '("By Recipients..." . rmail-summary-by-recipients))
+
+(define-key rmail-summary-mode-map [menu-bar summary topic]
+  '("By Topic..." . rmail-summary-by-topic))
+
+(define-key rmail-summary-mode-map [menu-bar summary regexp]
+  '("By Regexp..." . rmail-summary-by-regexp))
+
+(define-key rmail-summary-mode-map [menu-bar summary all]
+  '("All" . rmail-summary))
+
+(define-key rmail-summary-mode-map [menu-bar mail]
+  (cons "Mail" (make-sparse-keymap "Mail")))
+
+(define-key rmail-summary-mode-map [menu-bar mail rmail-summary-get-new-mail]
+  '("Get New Mail" . rmail-summary-get-new-mail))
+
+(define-key rmail-summary-mode-map [menu-bar mail lambda]
+  '("----"))
+
+(define-key rmail-summary-mode-map [menu-bar mail continue]
+  '("Continue" . rmail-summary-continue))
+
+(define-key rmail-summary-mode-map [menu-bar mail resend]
+  '("Re-send..." . rmail-summary-resend))
+
+(define-key rmail-summary-mode-map [menu-bar mail forward]
+  '("Forward" . rmail-summary-forward))
+
+(define-key rmail-summary-mode-map [menu-bar mail retry]
+  '("Retry" . rmail-summary-retry-failure))
+
+(define-key rmail-summary-mode-map [menu-bar mail reply]
+  '("Reply" . rmail-summary-reply))
+
+(define-key rmail-summary-mode-map [menu-bar mail mail]
+  '("Mail" . rmail-summary-mail))
+
+(define-key rmail-summary-mode-map [menu-bar delete]
+  (cons "Delete" (make-sparse-keymap "Delete")))
+
+(define-key rmail-summary-mode-map [menu-bar delete expunge/save]
+  '("Expunge/Save" . rmail-summary-expunge-and-save))
+
+(define-key rmail-summary-mode-map [menu-bar delete expunge]
+  '("Expunge" . rmail-summary-expunge))
+
+(define-key rmail-summary-mode-map [menu-bar delete undelete]
+  '("Undelete" . rmail-summary-undelete))
+
+(define-key rmail-summary-mode-map [menu-bar delete delete]
+  '("Delete" . rmail-summary-delete-forward))
+
+(define-key rmail-summary-mode-map [menu-bar move]
+  (cons "Move" (make-sparse-keymap "Move")))
+
+(define-key rmail-summary-mode-map [menu-bar move search-back]
+  '("Search Back..." . rmail-summary-search-backward))
+
+(define-key rmail-summary-mode-map [menu-bar move search]
+  '("Search..." . rmail-summary-search))
+
+(define-key rmail-summary-mode-map [menu-bar move previous]
+  '("Previous Nondeleted" . rmail-summary-previous-msg))
+
+(define-key rmail-summary-mode-map [menu-bar move next]
+  '("Next Nondeleted" . rmail-summary-next-msg))
+
+(define-key rmail-summary-mode-map [menu-bar move last]
+  '("Last" . rmail-summary-last-message))
+
+(define-key rmail-summary-mode-map [menu-bar move first]
+  '("First" . rmail-summary-first-message))
+
+(define-key rmail-summary-mode-map [menu-bar move previous]
+  '("Previous" . rmail-summary-previous-all))
+
+(define-key rmail-summary-mode-map [menu-bar move next]
+  '("Next" . rmail-summary-next-all))
+
+(defun rmail-summary-mouse-goto-message (event)
+  "Select the message whose summary line you click on."
+  (interactive "@e")
+  (goto-char (posn-point (event-end event)))
+  (rmail-summary-goto-msg))
+
+(defun rmail-summary-goto-msg (&optional n nowarn skip-rmail)
+  "Go to message N in the summary buffer and the Rmail buffer.
+If N is nil, use the message corresponding to point in the summary
+and move to that message in the Rmail buffer.
+
+If NOWARN, don't say anything if N is out of range.
+If SKIP-RMAIL, don't do anything to the Rmail buffer."
+  (interactive "P")
+  (if (consp n) (setq n (prefix-numeric-value n)))
+  (if (eobp) (forward-line -1))
+  (beginning-of-line)
+  (let* ((obuf (current-buffer))
+	 (buf rmail-buffer)
+	 (cur (point))
+	 message-not-found
+	 (curmsg (string-to-number
+		  (buffer-substring (point)
+				    (min (point-max) (+ 6 (point))))))
+	 (total (save-excursion (set-buffer buf) rmail-total-messages)))
+    ;; If message number N was specified, find that message's line
+    ;; or set message-not-found.
+    ;; If N wasn't specified or that message can't be found.
+    ;; set N by default.
+    (if (not n)
+	(setq n curmsg)
+      (if (< n 1)
+	  (progn (message "No preceding message")
+		 (setq n 1)))
+      (if (and (> n total)
+	       (> total 0))
+	  (progn (message "No following message")
+		 (goto-char (point-max))
+		 (rmail-summary-goto-msg nil nowarn skip-rmail)))
+      (goto-char (point-min))
+      (if (not (re-search-forward (format "^%5d[^0-9]" n) nil t))
+	  (progn (or nowarn (message "Message %d not found" n))
+		 (setq n curmsg)
+		 (setq message-not-found t)
+		 (goto-char cur))))
+    (beginning-of-line)
+    (skip-chars-forward " ")
+    (skip-chars-forward "0-9")
+    (save-excursion (if (= (following-char) ?-)
+			(let ((buffer-read-only nil))
+			  (delete-char 1)
+			  (insert " "))))
+    (rmail-summary-update-highlight message-not-found)
+    (beginning-of-line)
+    (if skip-rmail
+	nil
+      (let ((selwin (selected-window)))
+	(unwind-protect
+	    (progn (pop-to-buffer buf)
+		   (rmail-show-message-maybe n))
+	  (select-window selwin)
+	  ;; The actions above can alter the current buffer.  Preserve it.
+	  (set-buffer obuf))))))
+
+;; Update the highlighted line in an rmail summary buffer.
+;; That should be current.  We highlight the line point is on.
+;; If NOT-FOUND is non-nil, we turn off highlighting.
+(defun rmail-summary-update-highlight (not-found)
+  ;; Make sure we have an overlay to use.
+  (or rmail-summary-overlay
+      (progn
+	(make-local-variable 'rmail-summary-overlay)
+	(setq rmail-summary-overlay (make-overlay (point) (point)))))
+  ;; If this message is in the summary, use the overlay to highlight it.
+  ;; Otherwise, don't highlight anything.
+  (if not-found
+      (overlay-put rmail-summary-overlay 'face nil)
+    (move-overlay rmail-summary-overlay
+		  (save-excursion (beginning-of-line)
+				  (skip-chars-forward " ")
+				  (point))
+		  (save-excursion (end-of-line) (point)))
+    (overlay-put rmail-summary-overlay 'face 'highlight)))
+
+(defun rmail-summary-scroll-msg-up (&optional dist)
+  "Scroll the Rmail window forward.
+If the Rmail window is displaying the end of a message,
+advance to the next message."
+  (interactive "P")
+  (if (eq dist '-)
+      (rmail-summary-scroll-msg-down nil)
+    (let ((rmail-buffer-window (get-buffer-window rmail-buffer)))
+      (if rmail-buffer-window
+	  (if (let ((rmail-summary-window (selected-window)))
+		(select-window rmail-buffer-window)
+		(prog1
+		    ;; Is EOB visible in the buffer?
+		    (save-excursion
+		      (let ((ht (window-height (selected-window))))
+			(move-to-window-line (- ht 2))
+			(end-of-line)
+			(eobp)))
+		  (select-window rmail-summary-window)))
+	      (if (not rmail-summary-scroll-between-messages)
+		  (error "End of buffer")
+		(rmail-summary-next-msg (or dist 1)))
+	    (let ((other-window-scroll-buffer rmail-buffer))
+	      (scroll-other-window dist)))
+	;; If it isn't visible at all, show the beginning.
+	(rmail-summary-beginning-of-message)))))
+
+(defun rmail-summary-scroll-msg-down (&optional dist)
+  "Scroll the Rmail window backward.
+If the Rmail window is now displaying the beginning of a message,
+move to the previous message."
+  (interactive "P")
+  (if (eq dist '-)
+      (rmail-summary-scroll-msg-up nil)
+    (let ((rmail-buffer-window (get-buffer-window rmail-buffer)))
+      (if rmail-buffer-window
+	  (if (let ((rmail-summary-window (selected-window)))
+		(select-window rmail-buffer-window)
+		(prog1
+		    ;; Is BOB visible in the buffer?
+		    (save-excursion
+		      (move-to-window-line 0)
+		      (beginning-of-line)
+		      (bobp))
+		  (select-window rmail-summary-window)))
+	      (if (not rmail-summary-scroll-between-messages)
+		  (error "Beginning of buffer")
+		(rmail-summary-previous-msg (or dist 1)))
+	    (let ((other-window-scroll-buffer rmail-buffer))
+	      (scroll-other-window-down dist)))
+	;; If it isn't visible at all, show the beginning.
+	(rmail-summary-beginning-of-message)))))
+
+(defun rmail-summary-beginning-of-message ()
+  "Show current message from the beginning."
+  (interactive)
+  (rmail-summary-show-message 'BEG))
+
+(defun rmail-summary-end-of-message ()
+  "Show bottom of current message."
+  (interactive)
+  (rmail-summary-show-message 'END))
+
+(defun rmail-summary-show-message (where)
+  "Show current mail message.
+Position it according to WHERE which can be BEG or END"
+  (if (and (one-window-p) (not pop-up-frames))
+      ;; If there is just one window, put the summary on the top.
+      (let ((buffer rmail-buffer))
+	(split-window (selected-window) rmail-summary-window-size)
+	(select-window (frame-first-window))
+	(pop-to-buffer rmail-buffer)
+	;; If pop-to-buffer did not use that window, delete that
+	;; window.  (This can happen if it uses another frame.)
+	(or (eq buffer (window-buffer (next-window (frame-first-window))))
+	    (delete-other-windows)))
+    (pop-to-buffer rmail-buffer))
+  (cond
+   ((eq where 'BEG)
+	(goto-char (point-min))
+	(search-forward "\n\n"))
+   ((eq where 'END)
+	(goto-char (point-max))
+	(recenter (1- (window-height))))
+   )
+  (pop-to-buffer rmail-summary-buffer))
+
+(defun rmail-summary-bury ()
+  "Bury the Rmail buffer and the Rmail summary buffer."
+  (interactive)
+  (let ((buffer-to-bury (current-buffer)))
+    (let (window)
+      (while (setq window (get-buffer-window rmail-buffer))
+	(set-window-buffer window (other-buffer rmail-buffer)))
+      (bury-buffer rmail-buffer))
+    (switch-to-buffer (other-buffer buffer-to-bury))
+    (bury-buffer buffer-to-bury)))
+
+(defun rmail-summary-quit ()
+  "Quit out of Rmail and Rmail summary."
+  (interactive)
+  (rmail-summary-wipe)
+  (rmail-quit))
+
+(defun rmail-summary-wipe ()
+  "Kill and wipe away Rmail summary, remaining within Rmail."
+  (interactive)
+  (save-excursion (set-buffer rmail-buffer) (setq rmail-summary-buffer nil))
+  (let ((local-rmail-buffer rmail-buffer))
+    (kill-buffer (current-buffer))
+    ;; Delete window if not only one.
+    (if (not (eq (selected-window) (next-window nil 'no-minibuf)))
+	(delete-window))
+    ;; Switch windows to the rmail buffer, or switch to it in this window.
+    (pop-to-buffer local-rmail-buffer)))
+
+(defun rmail-summary-expunge ()
+  "Actually erase all deleted messages and recompute summary headers."
+  (interactive)
+  (save-excursion
+    (set-buffer rmail-buffer)
+    (when (rmail-expunge-confirmed)
+      (rmail-only-expunge)))
+  (rmail-update-summary))
+
+(defun rmail-summary-expunge-and-save ()
+  "Expunge and save RMAIL file."
+  (interactive)
+  (save-excursion
+    (rmail-expunge-and-save))
+  (rmail-update-summary)
+  (set-buffer-modified-p nil))
+
+(defun rmail-summary-get-new-mail (&optional file-name)
+  "Get new mail and recompute summary headers.
+
+Optionally you can specify the file to get new mail from.  In this case,
+the file of new mail is not changed or deleted.  Noninteractively, you can
+pass the inbox file name as an argument.  Interactively, a prefix
+argument says to read a file name and use that file as the inbox."
+  (interactive
+   (list (if current-prefix-arg
+	     (read-file-name "Get new mail from file: "))))
+  (let (msg)
+    (save-excursion
+      (set-buffer rmail-buffer)
+      (rmail-get-new-mail file-name)
+      ;; Get the proper new message number.
+      (setq msg rmail-current-message))
+    ;; Make sure that message is displayed.
+    (or (zerop msg)
+	(rmail-summary-goto-msg msg))))
+
+(defun rmail-summary-input (filename)
+  "Run Rmail on file FILENAME."
+  (interactive "FRun rmail on RMAIL file: ")
+  ;; We switch windows here, then display the other Rmail file there.
+  (pop-to-buffer rmail-buffer)
+  (rmail filename))
+
+(defun rmail-summary-first-message ()
+  "Show first message in Rmail file from summary buffer."
+  (interactive)
+  (with-no-warnings
+    (beginning-of-buffer)))
+
+(defun rmail-summary-last-message ()
+  "Show last message in Rmail file from summary buffer."
+  (interactive)
+  (with-no-warnings
+    (end-of-buffer))
+  (forward-line -1))
+
+(declare-function rmail-abort-edit "rmailedit" ())
+(declare-function rmail-cease-edit "rmailedit"())
+(declare-function rmail-set-label "rmailkwd" (l state &optional n))
+(declare-function rmail-output-read-file-name "rmailout" ())
+(declare-function mail-send-and-exit "sendmail" (&optional arg))
+
+(defvar rmail-summary-edit-map nil)
+(if rmail-summary-edit-map
+    nil
+  (setq rmail-summary-edit-map
+	(nconc (make-sparse-keymap) text-mode-map))
+  (define-key rmail-summary-edit-map "\C-c\C-c" 'rmail-cease-edit)
+  (define-key rmail-summary-edit-map "\C-c\C-]" 'rmail-abort-edit))
+
+(defun rmail-summary-edit-current-message ()
+  "Edit the contents of this message."
+  (interactive)
+  (pop-to-buffer rmail-buffer)
+  (rmail-edit-current-message)
+  (use-local-map rmail-summary-edit-map))
+
+(defun rmail-summary-cease-edit ()
+  "Finish editing message, then go back to Rmail summary buffer."
+  (interactive)
+  (rmail-cease-edit)
+  (pop-to-buffer rmail-summary-buffer))
+
+(defun rmail-summary-abort-edit ()
+  "Abort edit of current message; restore original contents.
+Go back to summary buffer."
+  (interactive)
+  (rmail-abort-edit)
+  (pop-to-buffer rmail-summary-buffer))
+
+(defun rmail-summary-search-backward (regexp &optional n)
+  "Show message containing next match for REGEXP.
+Prefix argument gives repeat count; negative argument means search
+backwards (through earlier messages).
+Interactively, empty argument means use same regexp used last time."
+  (interactive
+    (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
+	   (prompt
+	    (concat (if reversep "Reverse " "") "Rmail search (regexp"))
+	   regexp)
+      (setq prompt
+	    (concat prompt
+		    (if rmail-search-last-regexp
+			(concat ", default "
+				rmail-search-last-regexp "): ")
+		      "): ")))
+      (setq regexp (read-string prompt))
+      (cond ((not (equal regexp ""))
+	     (setq rmail-search-last-regexp regexp))
+	    ((not rmail-search-last-regexp)
+	     (error "No previous Rmail search string")))
+      (list rmail-search-last-regexp
+	    (prefix-numeric-value current-prefix-arg))))
+  ;; Don't use save-excursion because that prevents point from moving
+  ;; properly in the summary buffer.
+  (let ((buffer (current-buffer)))
+    (unwind-protect
+	(progn
+	  (set-buffer rmail-buffer)
+	  (rmail-search regexp (- n)))
+      (set-buffer buffer))))
+
+(defun rmail-summary-search (regexp &optional n)
+  "Show message containing next match for REGEXP.
+Prefix argument gives repeat count; negative argument means search
+backwards (through earlier messages).
+Interactively, empty argument means use same regexp used last time."
+  (interactive
+    (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
+	   (prompt
+	    (concat (if reversep "Reverse " "") "Rmail search (regexp"))
+	   regexp)
+      (setq prompt
+	    (concat prompt
+		    (if rmail-search-last-regexp
+			(concat ", default "
+				rmail-search-last-regexp "): ")
+		      "): ")))
+      (setq regexp (read-string prompt))
+      (cond ((not (equal regexp ""))
+	     (setq rmail-search-last-regexp regexp))
+	    ((not rmail-search-last-regexp)
+	     (error "No previous Rmail search string")))
+      (list rmail-search-last-regexp
+	    (prefix-numeric-value current-prefix-arg))))
+  ;; Don't use save-excursion because that prevents point from moving
+  ;; properly in the summary buffer.
+  (let ((buffer (current-buffer)))
+    (unwind-protect
+	(progn
+	  (set-buffer rmail-buffer)
+	  (rmail-search regexp n))
+      (set-buffer buffer))))
+
+(defun rmail-summary-toggle-header ()
+  "Show original message header if pruned header currently shown, or vice versa."
+  (interactive)
+  (save-window-excursion
+    (set-buffer rmail-buffer)
+    (rmail-toggle-header))
+  ;; Inside save-excursion, some changes to point in the RMAIL buffer are lost.
+  ;; Set point to point-min in the RMAIL buffer, if it is visible.
+  (let ((window (get-buffer-window rmail-buffer)))
+    (if window
+        ;; Using save-window-excursion would lose the new value of point.
+        (let ((owin (selected-window)))
+          (unwind-protect
+              (progn
+                (select-window window)
+                (goto-char (point-min)))
+            (select-window owin))))))
+
+
+(defun rmail-summary-add-label (label)
+  "Add LABEL to labels associated with current Rmail message.
+Completion is performed over known labels when reading."
+  (interactive (list (save-excursion
+		       (set-buffer rmail-buffer)
+		       (rmail-read-label "Add label"))))
+  (save-excursion
+    (set-buffer rmail-buffer)
+    (rmail-add-label label)))
+
+(defun rmail-summary-kill-label (label)
+  "Remove LABEL from labels associated with current Rmail message.
+Completion is performed over known labels when reading."
+  (interactive (list (save-excursion
+		       (set-buffer rmail-buffer)
+		       (rmail-read-label "Kill label"))))
+  (save-excursion
+    (set-buffer rmail-buffer)
+    (rmail-set-label label nil)))
+
+;;;; *** Rmail Summary Mailing Commands ***
+
+(defun rmail-summary-override-mail-send-and-exit ()
+  "Replace bindings to `mail-send-and-exit' with `rmail-summary-send-and-exit'."
+  (use-local-map (copy-keymap (current-local-map)))
+  (dolist (key (where-is-internal 'mail-send-and-exit))
+    (define-key (current-local-map) key 'rmail-summary-send-and-exit)))
+
+(defun rmail-summary-mail ()
+  "Send mail in another window.
+While composing the message, use \\[mail-yank-original] to yank the
+original message into it."
+  (interactive)
+  (let ((window (get-buffer-window rmail-buffer)))
+    (if window
+	(select-window window)
+      (set-buffer rmail-buffer)))
+  (rmail-start-mail nil nil nil nil nil (current-buffer))
+  (rmail-summary-override-mail-send-and-exit))
+
+(defun rmail-summary-continue ()
+  "Continue composing outgoing message previously being composed."
+  (interactive)
+  (let ((window (get-buffer-window rmail-buffer)))
+    (if window
+	(select-window window)
+      (set-buffer rmail-buffer)))
+  (rmail-start-mail t))
+
+(defun rmail-summary-reply (just-sender)
+  "Reply to the current message.
+Normally include CC: to all other recipients of original message;
+prefix argument means ignore them.  While composing the reply,
+use \\[mail-yank-original] to yank the original message into it."
+  (interactive "P")
+  (let ((window (get-buffer-window rmail-buffer)))
+    (if window
+	(select-window window)
+      (set-buffer rmail-buffer)))
+  (rmail-reply just-sender)
+  (rmail-summary-override-mail-send-and-exit))
+
+(defun rmail-summary-retry-failure ()
+  "Edit a mail message which is based on the contents of the current message.
+For a message rejected by the mail system, extract the interesting headers and
+the body of the original message; otherwise copy the current message."
+  (interactive)
+  (let ((window (get-buffer-window rmail-buffer)))
+    (if window
+	(select-window window)
+      (set-buffer rmail-buffer)))
+  (rmail-retry-failure)
+  (rmail-summary-override-mail-send-and-exit))
+
+(defun rmail-summary-send-and-exit ()
+  "Send mail reply and return to summary buffer."
+  (interactive)
+  (mail-send-and-exit t))
+
+(defun rmail-summary-forward (resend)
+  "Forward the current message to another user.
+With prefix argument, \"resend\" the message instead of forwarding it;
+see the documentation of `rmail-resend'."
+  (interactive "P")
+  (save-excursion
+    (let ((window (get-buffer-window rmail-buffer)))
+      (if window
+	  (select-window window)
+	(set-buffer rmail-buffer)))
+    (rmail-forward resend)
+    (rmail-summary-override-mail-send-and-exit)))
+
+(defun rmail-summary-resend ()
+  "Resend current message using `rmail-resend'."
+  (interactive)
+  (save-excursion
+    (let ((window (get-buffer-window rmail-buffer)))
+      (if window
+	  (select-window window)
+	(set-buffer rmail-buffer)))
+    (call-interactively 'rmail-resend)))
+
+;; Summary output commands.
+
+(defun rmail-summary-output (&optional file-name n)
+  "Append this message to mail file FILE-NAME.
+This works with both mbox format and Babyl format files,
+outputting in the appropriate format for each.
+The default file name comes from `rmail-default-file',
+which is updated to the name you use in this command.
+
+A prefix argument N says to output that many consecutive messages
+from those in the summary, starting with the current one.
+Deleted messages are skipped and don't count.
+When called from Lisp code, N may be omitted and defaults to 1.
+
+This command always outputs the complete message header,
+even the header display is currently pruned."
+  (interactive
+   (progn (require 'rmailout)
+	  (list (rmail-output-read-file-name)
+		(prefix-numeric-value current-prefix-arg))))
+  (let ((i 0) prev-msg)
+    (while
+	(and (< i n)
+	     (progn (rmail-summary-goto-msg)
+		    (not (eq prev-msg
+			     (setq prev-msg
+				   (with-current-buffer rmail-buffer
+				     rmail-current-message))))))
+      (setq i (1+ i))
+      (with-current-buffer rmail-buffer
+	(let ((rmail-delete-after-output nil))
+	  (rmail-output file-name 1)))
+      (if rmail-delete-after-output
+	  (rmail-summary-delete-forward nil)
+	(if (< i n)
+	    (rmail-summary-next-msg 1))))))
+
+(defalias 'rmail-summary-output-to-rmail-file 'rmail-summary-output)
+
+(declare-function rmail-output-as-seen "rmailout"
+		  (file-name &optional count noattribute from-gnus))
+
+(defun rmail-summary-output-as-seen (&optional file-name n)
+  "Append this message to system-inbox-format mail file named FILE-NAME.
+A prefix argument N says to output that many consecutive messages,
+from the summary, starting with the current one.
+Deleted messages are skipped and don't count.
+When called from Lisp code, N may be omitted and defaults to 1.
+
+This outputs the message header as you see it (or would see it)
+displayed in Rmail.
+
+The default file name comes from `rmail-default-file',
+which is updated to the name you use in this command."
+  (interactive
+   (progn (require 'rmailout)
+	  (list (rmail-output-read-file-name)
+		(prefix-numeric-value current-prefix-arg))))
+  (require 'rmailout) ; for rmail-output-as-seen in non-interactive case
+  (let ((i 0) prev-msg)
+    (while
+	(and (< i n)
+	     (progn (rmail-summary-goto-msg)
+		    (not (eq prev-msg
+			     (setq prev-msg
+				   (with-current-buffer rmail-buffer
+				     rmail-current-message))))))
+      (setq i (1+ i))
+      (with-current-buffer rmail-buffer
+	(let ((rmail-delete-after-output nil))
+	  (rmail-output-as-seen file-name 1)))
+      (if rmail-delete-after-output
+	  (rmail-summary-delete-forward nil)
+	(if (< i n)
+	    (rmail-summary-next-msg 1))))))
+
+(defun rmail-summary-output-menu ()
+  "Output current message to another Rmail file, chosen with a menu.
+Also set the default for subsequent \\[rmail-output-to-babyl-file] commands.
+The variables `rmail-secondary-file-directory' and
+`rmail-secondary-file-regexp' control which files are offered in the menu."
+  (interactive)
+  (save-excursion
+    (set-buffer rmail-buffer)
+    (let ((rmail-delete-after-output nil))
+      (call-interactively 'rmail-output-menu)))
+  (if rmail-delete-after-output
+      (rmail-summary-delete-forward nil)))
+
+(defun rmail-summary-construct-io-menu ()
+  (let ((files (rmail-find-all-files rmail-secondary-file-directory)))
+    (if files
+	(progn
+	  (define-key rmail-summary-mode-map [menu-bar classify input-menu]
+	    (cons "Input Rmail File"
+		  (rmail-list-to-menu "Input Rmail File"
+				      files
+				      'rmail-summary-input)))
+	  (define-key rmail-summary-mode-map [menu-bar classify output-menu]
+	    (cons "Output Rmail File"
+		  (rmail-list-to-menu "Output Rmail File"
+				      files
+				      'rmail-summary-output))))
+      (define-key rmail-summary-mode-map [menu-bar classify input-menu]
+	'("Input Rmail File" . rmail-disable-menu))
+      (define-key rmail-summary-mode-map [menu-bar classify output-menu]
+	'("Output Rmail File" . rmail-disable-menu)))))
+
+(defun rmail-summary-output-body (&optional file-name)
+  "Write this message body to the file FILE-NAME.
+FILE-NAME defaults, interactively, from the Subject field of the message."
+  (interactive)
+  (save-excursion
+    (set-buffer rmail-buffer)
+    (let ((rmail-delete-after-output nil))
+      (if file-name
+	  (rmail-output-body-to-file file-name)
+	(call-interactively 'rmail-output-body-to-file))))
+  (if rmail-delete-after-output
+      (rmail-summary-delete-forward nil)))
+
+;; Sorting messages in Rmail Summary buffer.
+
+(defun rmail-summary-sort-by-date (reverse)
+  "Sort messages of current Rmail summary by date.
+If prefix argument REVERSE is non-nil, sort them in reverse order."
+  (interactive "P")
+  (rmail-sort-from-summary (function rmail-sort-by-date) reverse))
+
+(defun rmail-summary-sort-by-subject (reverse)
+  "Sort messages of current Rmail summary by subject.
+If prefix argument REVERSE is non-nil, sort them in reverse order."
+  (interactive "P")
+  (rmail-sort-from-summary (function rmail-sort-by-subject) reverse))
+
+(defun rmail-summary-sort-by-author (reverse)
+  "Sort messages of current Rmail summary by author.
+If prefix argument REVERSE is non-nil, sort them in reverse order."
+  (interactive "P")
+  (rmail-sort-from-summary (function rmail-sort-by-author) reverse))
+
+(defun rmail-summary-sort-by-recipient (reverse)
+  "Sort messages of current Rmail summary by recipient.
+If prefix argument REVERSE is non-nil, sort them in reverse order."
+  (interactive "P")
+  (rmail-sort-from-summary (function rmail-sort-by-recipient) reverse))
+
+(defun rmail-summary-sort-by-correspondent (reverse)
+  "Sort messages of current Rmail summary by other correspondent.
+If prefix argument REVERSE is non-nil, sort them in reverse order."
+  (interactive "P")
+  (rmail-sort-from-summary (function rmail-sort-by-correspondent) reverse))
+
+(defun rmail-summary-sort-by-lines (reverse)
+  "Sort messages of current Rmail summary by lines of the message.
+If prefix argument REVERSE is non-nil, sort them in reverse order."
+  (interactive "P")
+  (rmail-sort-from-summary (function rmail-sort-by-lines) reverse))
+
+(defun rmail-summary-sort-by-labels (reverse labels)
+  "Sort messages of current Rmail summary by labels.
+If prefix argument REVERSE is non-nil, sort them in reverse order.
+KEYWORDS is a comma-separated list of labels."
+  (interactive "P\nsSort by labels: ")
+  (rmail-sort-from-summary
+   (function (lambda (reverse)
+	       (rmail-sort-by-labels reverse labels)))
+   reverse))
+
+(defun rmail-sort-from-summary (sortfun reverse)
+  "Sort Rmail messages from Summary buffer and update it after sorting."
+  (require 'rmailsort)
+  (let ((selwin (selected-window)))
+    (unwind-protect
+	(progn (pop-to-buffer rmail-buffer)
+	       (funcall sortfun reverse))
+      (select-window selwin))))
+
+(provide 'rmailsum)
+
+;; Local Variables:
+;; change-log-default-name: "ChangeLog.rmail"
+;; End:
+
+;; arch-tag: 80b0a27a-a50d-4f37-9466-83d32d1e0ca8
+;;; rmailsum.el ends here