--- /dev/null
+;;; pmail.el --- main code of "PMAIL" 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
+;; 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: pmail and pmail-summary buffers remain
+;; synchronized and key bindings basically operate the same way in both
+;; buffers, summary by topic or by regular expression, pmail-reply-prefix
+;; variable, and a bury pmail buffer (wipe) command.
+;;
+
+(eval-when-compile
+ (require 'font-lock)
+ (require 'mailabbrev)
+ (require 'mule-util)) ; for detect-coding-with-priority
+
+(require 'pmaildesc)
+(require 'pmailhdr)
+(require 'pmailkwd)
+(require 'mail-parse)
+
+(defvar deleted-head)
+(defvar font-lock-fontified)
+(defvar mail-abbrev-syntax-table)
+(defvar mail-abbrevs)
+(defvar messages-head)
+(defvar pmail-use-spam-filter)
+(defvar rsf-beep)
+(defvar rsf-sleep-after-message)
+(defvar total-messages)
+(defvar tool-bar-map)
+
+; These variables now declared in paths.el.
+;(defvar pmail-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 pmail-file-name
+; (expand-file-name "~/PMAIL")
+; "")
+
+;; Temporary support for mbox.
+(defcustom pmail-file-name "~/PMAIL"
+ "*Name of user's primary mail file."
+ :type 'string
+ :group 'rmail
+ :version "21.1")
+
+(defgroup pmail nil
+ "Mail reader for Emacs."
+ :group 'mail)
+
+(defgroup pmail-retrieve nil
+ "Pmail retrieval options."
+ :prefix "pmail-"
+ :group 'pmail)
+
+(defgroup pmail-files nil
+ "Pmail files."
+ :prefix "pmail-"
+ :group 'pmail)
+
+(defgroup pmail-headers nil
+ "Pmail header options."
+ :prefix "pmail-"
+ :group 'pmail)
+
+(defgroup pmail-reply nil
+ "Pmail reply options."
+ :prefix "pmail-"
+ :group 'pmail)
+
+(defgroup pmail-summary nil
+ "Pmail summary options."
+ :prefix "pmail-"
+ :prefix "pmail-summary-"
+ :group 'pmail)
+
+(defgroup pmail-output nil
+ "Output message to a file."
+ :prefix "pmail-output-"
+ :prefix "pmail-"
+ :group 'pmail)
+
+(defgroup pmail-edit nil
+ "Pmail editing."
+ :prefix "pmail-edit-"
+ :group 'pmail)
+
+(defgroup pmail-obsolete nil
+ "Pmail obsolete customization variables."
+ :group 'pmail)
+
+(defcustom pmail-movemail-program nil
+ "If non-nil, the file name of the `movemail' program."
+ :group 'pmail-retrieve
+ :type '(choice (const nil) string))
+
+(defcustom pmail-pop-password nil
+ "*Password to use when reading mail from POP server.
+Please use `pmail-remote-password' instead."
+ :type '(choice (string :tag "Password")
+ (const :tag "Not Required" nil))
+ :group 'pmail-obsolete)
+
+(defcustom pmail-pop-password-required nil
+ "*Non-nil if a password is required when reading mail from a POP server.
+Please use pmail-remote-password-required instead."
+ :type 'boolean
+ :group 'pmail-obsolete)
+
+(defcustom pmail-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 '(pmail-pop-password)
+ :set #'(lambda (symbol value)
+ (set-default symbol
+ (if (and (not value)
+ (boundp 'pmail-pop-password)
+ pmail-pop-password)
+ pmail-pop-password
+ value))
+ (setq pmail-pop-password nil))
+ :group 'pmail-retrieve
+ :version "22.1")
+
+(defcustom pmail-remote-password-required nil
+ "*Non-nil if a password is required when reading mail from a remote server."
+ :type 'boolean
+ :set-after '(pmail-pop-password-required)
+ :set #'(lambda (symbol value)
+ (set-default symbol
+ (if (and (not value)
+ (boundp 'pmail-pop-password-required)
+ pmail-pop-password-required)
+ pmail-pop-password-required
+ value))
+ (setq pmail-pop-password-required nil))
+ :group 'pmail-retrieve
+ :version "22.1")
+
+(defcustom pmail-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 'pmail-retrieve
+ :version "20.3")
+
+(defvar pmail-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 pmail-encoded-remote-password nil)
+
+(defcustom pmail-preserve-inbox nil
+ "*Non-nil means leave incoming mail in the user's inbox--don't delete it."
+ :type 'boolean
+ :group 'pmail-retrieve)
+
+(defcustom pmail-movemail-search-path nil
+ "*List of directories to search for movemail (in addition to `exec-path')."
+ :group 'pmail-retrieve
+ :type '(repeat (directory)))
+
+(declare-function mail-position-on-field "sendmail" (field &optional soft))
+(declare-function mail-text-start "sendmail" ())
+(declare-function pmail-update-summary "pmailsum" (&rest ignore))
+
+(defun pmail-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 pmail-autodetect ()
+ "Determine the file name of the `movemail' program and return its flavor.
+If `pmail-movemail-program' is non-nil, use it.
+Otherwise, look for `movemail' in the directories in
+`pmail-movemail-search-path', those in `exec-path', and `exec-directory'."
+ (if pmail-movemail-program
+ (pmail-probe pmail-movemail-program)
+ (catch 'scan
+ (dolist (dir (append pmail-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
+ ;; pmail-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 (pmail-probe progname)))
+ (when x
+ (setq pmail-movemail-program progname)
+ (throw 'scan x))))))))))
+
+(defvar pmail-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 pmail-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 pmail-movemail-variant-in-use)
+ ;; Autodetect
+ (setq pmail-movemail-variant-in-use (pmail-autodetect)))
+ (not (null (member pmail-movemail-variant-in-use variants))))
+
+;; Call for effect, to set pmail-movemail-program (if not set by the
+;; user), and pmail-movemail-variant-in-use. Used by various functions.
+;; I'm not sure if M-x pmail is the only entry point to this package.
+;; If so, this can be moved there.
+(pmail-movemail-variant-p)
+
+;;;###autoload
+(defcustom pmail-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 `pmail-default-dont-reply-to-names'."
+ :type '(choice regexp (const :tag "Your Name" nil))
+ :group 'pmail-reply)
+
+;;;###autoload
+(defvar pmail-default-dont-reply-to-names "\\`info-" "\
+A regular expression specifying part of the default value of the
+variable `pmail-dont-reply-to-names', for when the user does not set
+`pmail-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 pmail-ignored-headers
+ (concat "^via:\\|^mail-from:\\|^origin:\\|^references:\\|^sender:"
+ "\\|^status:\\|^received:\\|^content-transfer-encoding:"
+ "\\|^x400-\\(received\\|mts-identifier\\|content-type\\|originator\\|recipients\\):"
+ "\\|^list-\\(help\\|post\\|subscribe\\|id\\|unsubscribe\\|archive\\):"
+ "\\|^resent-\\(face\\|x-.*\\|organization\\|openpgp\\|date\\|message-id\\):"
+ "\\|^thread-\\(topic\\|index\\)"
+ "\\|^summary-line:\\|^precedence:\\|^message-id:"
+ "\\|^path:\\|^face:\\|^delivered-to:\\|^lines:"
+ "\\|^return-path:\\|^errors-to:\\|^return-receipt-to:"
+ "\\|^content-\\(length\\|type\\|class\\|disposition\\):"
+ "\\|^nntp-posting-host:\\|^path:\\|^x-char.*:\\|^x-face:\\|^face:"
+ "\\|^importance:\\|^envelope-to:\\|^delivery-date\\|^openpgp:"
+ "\\|^mbox-line:\\|^cancel-lock:\\|^in-reply-to:\\|^comment:"
+ "\\|^x-.*:\\|^domainkey-signature:"
+ "\\|^original-recipient:\\|^from ")
+ "*Regexp to match header fields that Pmail should normally hide.
+\(See also `pmail-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 Pmail.
+To make a change in this variable take effect
+for a message that you have already viewed,
+go to that message and type \\[pmail-toggle-header] twice."
+ :type 'regexp
+ :group 'pmail-headers)
+
+(defcustom pmail-nonignored-headers "^x-spam-status:"
+ "*Regexp to match X header fields that Pmail should show.
+This regexp overrides `pmail-ignored-headers'; if both this regexp
+and that one match a certain header field, Pmail shows the field.
+If this is nil, ignore all header fields in `pmail-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 Pmail.
+To make a change in this variable take effect
+for a message that you have already viewed,
+go to that message and type \\[pmail-toggle-header] twice."
+ :type '(choice (const nil) (regexp))
+ :group 'pmail-headers)
+
+;;;###autoload
+(defcustom pmail-displayed-headers nil
+ "*Regexp to match Header fields that Pmail should display.
+If nil, display all header fields except those matched by
+`pmail-ignored-headers'."
+ :type '(choice regexp (const :tag "All"))
+ :group 'pmail-headers)
+
+;;;###autoload
+(defcustom pmail-retry-ignored-headers "^x-authentication-warning:" "\
+*Headers that should be stripped when retrying a failed message."
+ :type '(choice regexp (const nil :tag "None"))
+ :group 'pmail-headers)
+
+;;;###autoload
+(defcustom pmail-highlighted-headers "^From:\\|^Subject:" "\
+*Regexp to match Header fields that Pmail should normally highlight.
+A value of nil means don't highlight.
+See also `pmail-highlight-face'."
+ :type 'regexp
+ :group 'pmail-headers)
+
+(defface pmail-highlight
+ '((t (:inherit highlight)))
+ "Face to use for highlighting the most important header fields."
+ :group 'pmail-headers
+ :version "22.1")
+
+(defface pmail-header-name
+ '((t (:inherit font-lock-function-name-face)))
+ "Face to use for highlighting the header names."
+ :group 'pmail-headers
+ :version "23.1")
+
+;;;###autoload
+(defcustom pmail-highlight-face 'pmail-highlight "\
+*Face used by Pmail for highlighting sender and subject.
+See `pmail-font-lock-keywords'."
+ :type '(choice (const :tag "Default" nil)
+ face)
+ :group 'pmail-headers)
+
+;;;###autoload
+(defcustom pmail-delete-after-output nil "\
+*Non-nil means automatically delete a message that is copied to a file."
+ :type 'boolean
+ :group 'pmail-files)
+
+;;;###autoload
+(defcustom pmail-primary-inbox-list nil "\
+*List of files which are inboxes for user's primary mail file `~/PMAIL'.
+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 'pmail-retrieve
+ :group 'pmail-files)
+
+;;;###autoload
+(defcustom pmail-inbox-alist nil
+ "*Alist of mail files and backup directory names.
+Each element has the form (MAIL-FILE INBOX ...). When running
+pmail on MAIL-FILE, mails in all the INBOX files listed will be
+moved to the MAIL-FILE. Be sure to fully qualify your MAIL-FILE.
+
+Example setting if procmail delivers all your spam to
+~/Mail/SPAM.in and you read it from the file ~/Mail/SPAM:
+
+\(setq pmail-inbox-alist '((\"~/Mail/SPAM\" \"~/Mail/SPAM.in\")))"
+ :type '(alist :key-type file :value-type (repeat file))
+ :group 'pmail-retrieve
+ :group 'pmail-files
+ :version "22.1")
+
+;;;###autoload
+(defcustom pmail-mail-new-frame nil
+ "*Non-nil means Pmail 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 PMAIL buffer displayed."
+ :type 'boolean
+ :group 'pmail-reply)
+
+;;;###autoload
+(defcustom pmail-secondary-file-directory "~/"
+ "*Directory for additional secondary Pmail files."
+ :type 'directory
+ :group 'pmail-files)
+;;;###autoload
+(defcustom pmail-secondary-file-regexp "\\.xmail$"
+ "*Regexp for which files are secondary Pmail files."
+ :type 'regexp
+ :group 'pmail-files)
+
+;;;###autoload
+(defcustom pmail-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 'pmail-files)
+
+;;;###autoload
+(defvar pmail-mode-hook nil
+ "List of functions to call when Pmail is invoked.")
+
+;;;###autoload
+(defvar pmail-get-new-mail-hook nil
+ "List of functions to call when Pmail has retrieved new mail.")
+
+;;;###autoload
+(defcustom pmail-show-message-hook nil
+ "List of functions to call when Pmail displays a message."
+ :type 'hook
+ :options '(goto-address)
+ :group 'pmail)
+
+;;;###autoload
+(defvar pmail-quit-hook nil
+ "List of functions to call when quitting out of Pmail.")
+
+;;;###autoload
+(defvar pmail-delete-message-hook nil
+ "List of functions to call when Pmail deletes a message.
+When the hooks are called, the message has been marked deleted but is
+still the current message in the Pmail 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 pmail-mmdf-delim1 "^\001\001\001\001\n"
+ "Regexp marking the start of an mmdf message.")
+(defvar pmail-mmdf-delim2 "^\001\001\001\001\n"
+ "Regexp marking the end of an mmdf message.")
+
+(defcustom pmail-message-filter nil
+ "If non-nil, a filter function for new messages in PMAIL.
+Called with region narrowed to the message, including headers,
+before obeying `pmail-ignored-headers'."
+ :group 'pmail-headers
+ :type '(choice (const nil) function))
+
+(defcustom pmail-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 Version 6 (also known as mbox
+or Unix inbox 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 'pmail
+ :version "21.1"
+ :type '(repeat (sexp :tag "Directive")))
+
+(defvar pmail-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 pmail-reply-regexp "\\`\\(Re\\(([0-9]+)\\|\\[[0-9]+\\]\\|\\^[0-9]+\\)?: *\\)*"
+ "Regexp to delete from Subject line before inserting `pmail-reply-prefix'.")
+
+(defcustom pmail-display-summary nil
+ "*If non-nil, Pmail always displays the summary buffer."
+ :group 'pmail-summary
+ :type 'boolean)
+\f
+(defvar pmail-inbox-list nil)
+(put 'pmail-inbox-list 'permanent-local t)
+
+(defvar pmail-keywords nil)
+(put 'pmail-keywords 'permanent-local t)
+
+(defvar pmail-buffer nil
+ "The PMAIL buffer related to the current buffer.
+In an PMAIL buffer, this holds the PMAIL buffer itself.
+In a summary buffer, this holds the PMAIL buffer it is a summary for.")
+(put 'pmail-buffer 'permanent-local t)
+
+;; Message counters and markers. Deleted flags.
+
+(defvar pmail-current-message nil)
+(put 'pmail-current-message 'permanent-local t)
+
+(defvar pmail-total-messages nil)
+(put 'pmail-total-messages 'permanent-local t)
+
+(defvar pmail-overlay-list nil)
+(put 'pmail-overlay-list 'permanent-local t)
+
+;; These are used by autoloaded pmail-summary.
+
+(defvar pmail-summary-buffer nil)
+(put 'pmail-summary-buffer 'permanent-local t)
+
+(defvar pmail-view-buffer nil
+ "Buffer which holds PMAIL message for MIME displaying.")
+(put 'pmail-view-buffer 'permanent-local t)
+\f
+;; `Sticky' default variables.
+
+;; Last individual label specified to a or k.
+(defvar pmail-last-label nil)
+(put 'pmail-last-label 'permanent-local t)
+
+;; Last set of values specified to C-M-n, C-M-p, C-M-s or C-M-l.
+(defvar pmail-last-multi-labels nil)
+
+(defvar pmail-last-regexp nil)
+(put 'pmail-last-regexp 'permanent-local t)
+
+(defcustom pmail-default-file "~/xmail"
+ "*Default file name for \\[pmail-output]."
+ :type 'file
+ :group 'pmail-files)
+
+(defcustom pmail-default-pmail-file "~/XMAIL"
+ "*Default file name for \\[pmail-output-to-pmail-file]."
+ :type 'file
+ :group 'pmail-files)
+
+(defcustom pmail-default-body-file "~/mailout"
+ "*Default file name for \\[pmail-output-body-to-file]."
+ :type 'file
+ :group 'pmail-files
+ :version "20.3")
+
+;; Mule and MIME related variables.
+
+;;;###autoload
+(defvar pmail-file-coding-system nil
+ "Coding system used in PMAIL file.
+
+This is set to nil by default.")
+
+;;;###autoload
+(defcustom pmail-enable-mime nil
+ "*If non-nil, PMAIL uses MIME feature.
+If the value is t, PMAIL automatically shows MIME decoded message.
+If the value is neither t nor nil, PMAIL 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 `pmail-mime-feature' is not available
+in your session."
+ :type '(choice (const :tag "on" t)
+ (const :tag "off" nil)
+ (other :tag "when asked" ask))
+ :group 'pmail)
+
+(defvar pmail-enable-mime-composing nil
+ "*If non-nil, PMAIL uses `pmail-insert-mime-forwarded-message-function' to forward.")
+
+;;;###autoload
+(defvar pmail-show-mime-function nil
+ "Function to show MIME decoded message of PMAIL file.
+This function is called when `pmail-enable-mime' is non-nil.
+It is called with no argument.")
+
+;;;###autoload
+(defvar pmail-insert-mime-forwarded-message-function nil
+ "Function to insert a message in MIME format so it can be forwarded.
+This function is called if `pmail-enable-mime' or
+`pmail-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 pmail-insert-mime-resent-message-function nil
+ "Function to insert a message in MIME format so it can be resent.
+This function is called if `pmail-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 pmail-search-mime-message-function nil
+ "Function to check if a regexp matches a MIME message.
+This function is called if `pmail-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 pmail-search-mime-header-function nil
+ "Function to check if a regexp matches a header of MIME message.
+This function is called if `pmail-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 pmail-mime-feature 'pmail-mime
+ "Feature to require to load MIME support in Pmail.
+When starting Pmail, if `pmail-enable-mime' is non-nil,
+this feature is required with `require'.
+
+The default value is `pmail-mime'. This feature is provided by
+the pmail-mime package available at <http://www.m17n.org/pmail-mime/>.")
+
+;;;###autoload
+(defvar pmail-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 `pmail-enable-mime' is non-nil, this variables is
+ignored, and all the decoding work is done by a feature specified by
+the variable `pmail-mime-feature'.")
+
+;;;###autoload
+(defvar pmail-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.")
+
+\f
+;;; Regexp matching the delimiter of messages in UNIX mail format
+;;; (UNIX From lines), with an initial ^. Used in pmail-decode-from-line,
+;;; which knows the exact ordering of the \\(...\\) subexpressions.
+(defvar pmail-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 pmail-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 '("^\\(Sender\\|Resent-From\\):"
+ . font-lock-function-name-face)
+ '("^Reply-To:.*$" . font-lock-function-name-face)
+ '("^\\(From:\\)\\(.*\\(\n[ \t]+.*\\)*\\)"
+ (1 font-lock-function-name-face)
+ (2 pmail-highlight-face))
+ '("^\\(Subject:\\)\\(.*\\(\n[ \t]+.*\\)*\\)"
+ (1 font-lock-comment-face)
+ (2 pmail-highlight-face))
+ '("^X-Spam-Status:" . font-lock-keyword-face)
+ '("^\\(To\\|Apparently-To\\|Cc\\|Newsgroups\\):"
+ . font-lock-keyword-face)
+ ;; 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]+.*\\)*$"
+ . 'pmail-header-name))))
+ "Additional expressions to highlight in Pmail mode.")
+
+;; Perform BODY in the summary buffer
+;; in such a way that its cursor is properly updated in its own window.
+(defmacro pmail-select-summary (&rest body)
+ `(let ((total pmail-total-messages))
+ (if (pmail-summary-displayed)
+ (let ((window (selected-window)))
+ (save-excursion
+ (unwind-protect
+ (progn
+ (pop-to-buffer pmail-summary-buffer)
+ ;; pmail-total-messages is a buffer-local var
+ ;; in the pmail buffer.
+ ;; This way we make it available for the body
+ ;; even tho the pmail buffer is not current.
+ (let ((pmail-total-messages total))
+ ,@body))
+ (select-window window))))
+ (save-excursion
+ (set-buffer pmail-summary-buffer)
+ (let ((pmail-total-messages total))
+ ,@body)))
+ (pmail-maybe-display-summary)))
+\f
+;;;; *** Pmail Mode ***
+
+;; This variable is dynamically bound. The defvar is here to placate
+;; the byte compiler.
+
+(defvar pmail-enable-multibyte nil)
+
+;; Avoid errors.
+(defvar pmail-use-spam-filter nil)
+
+(defun pmail-require-mime-maybe ()
+ "Require `pmail-mime-feature' if that is non-nil.
+Signal an error and set `pmail-mime-feature' to nil if the feature
+isn't provided."
+ (when pmail-enable-mime
+ (condition-case err
+ (require pmail-mime-feature)
+ (error
+ (display-warning
+ :warning
+ (format "Although MIME support is requested
+by setting `pmail-enable-mime' to non-nil, the required feature
+`%s' (the value of `pmail-mime-feature')
+is not available in the current session.
+So, the MIME support is turned off for the moment."
+ pmail-mime-feature))
+ (setq pmail-enable-mime nil)))))
+
+;;;###autoload
+(defun pmail (&optional file-name-arg)
+ "Read and edit incoming mail.
+Moves messages into file named by `pmail-file-name' (a babyl format file)
+ and edits that file in PMAIL Mode.
+Type \\[describe-mode] once editing that file, for a list of PMAIL commands.
+
+May be called with file name as argument; then performs pmail 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 `pmail-display-summary' is non-nil, make a summary for this PMAIL file."
+ (interactive (if current-prefix-arg
+ (list (read-file-name "Run pmail on PMAIL file: "))))
+ (pmail-require-mime-maybe)
+ (let* ((file-name (expand-file-name (or file-name-arg pmail-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))
+ ;; This binding is necessary because we must decide if we
+ ;; need code conversion while the buffer is unibyte
+ ;; (i.e. enable-multibyte-characters is nil).
+ (pmail-enable-multibyte
+ (if existed
+ (with-current-buffer existed enable-multibyte-characters)
+ (default-value 'enable-multibyte-characters)))
+ run-mail-hook msg-shown)
+ (when (and existed (eq major-mode 'pmail-edit-mode))
+ (error "Exit Pmail Edit mode before getting new mail"))
+ (if (and existed (not (verify-visited-file-modtime existed)))
+ (progn
+ (find-file file-name)
+ (when (and (verify-visited-file-modtime existed)
+ (eq major-mode 'pmail-mode))
+ (setq major-mode 'fundamental-mode)))
+ (switch-to-buffer
+ (let ((enable-local-variables nil))
+ (find-file-noselect file-name)))
+ ;; As we have read a file as raw-text, the buffer is set to
+ ;; unibyte. We must make it multibyte if necessary.
+ (when (and pmail-enable-multibyte
+ (not enable-multibyte-characters))
+ (set-buffer-multibyte t)))
+ ;; Make sure we're in pmail-mode, even if the buffer did exist and
+ ;; the file was not changed.
+ (unless (eq major-mode 'pmail-mode)
+ ;; If file looks like a Babyl file, save it to a temp file,
+ ;; convert it, and replace the current content with the
+ ;; converted content. Don't save -- let the user do it.
+ (goto-char (point-min))
+ (when (looking-at "BABYL OPTIONS:")
+ (let ((old-file (make-temp-file "pmail"))
+ (new-file (make-temp-file "pmail")))
+ (unwind-protect
+ (progn
+ (write-region (point-min) (point-max) old-file)
+ (unpmail old-file new-file)
+ (message "Replacing BABYL format with mbox format...")
+ (let ((inhibit-read-only t))
+ (erase-buffer)
+ (insert-file-contents-literally new-file))
+ (message "Replacing BABYL format with mbox format...done"))
+ (delete-file old-file)
+ (delete-file new-file))))
+ (goto-char (point-max))
+ (pmail-mode-2)
+ ;; Convert all or parts of file to a format Pmail understands
+ (pmail-convert-file)
+ ;; We use `run-mail-hook' to remember whether we should run
+ ;; `pmail-mode-hook' at the end.
+ (setq run-mail-hook t)
+ ;; Initialize the Pmail state.
+ (pmail-initialize-messages))
+ ;; Now we're back in business. The happens even if we had a
+ ;; perfectly fine file.
+ (unwind-protect
+ (unless (and (not file-name-arg) (pmail-get-new-mail))
+ (pmail-show-message (pmail-first-unseen-message)))
+ (when pmail-display-summary
+ (pmail-summary))
+ (pmail-construct-io-menu)
+ ;; Run any callbacks if the buffer was not in pmail-mode
+ (when run-mail-hook
+ (run-hooks 'pmail-mode-hook)))))
+
+(defun pmail-convert-file ()
+ "Convert unconverted messages.
+A message is unconverted if it doesn't have the BABYL header
+specified in `pmail-header-attribute-header'; it is converted
+using `pmail-convert-mbox-format'."
+ (let ((convert
+ (save-restriction
+ (widen)
+ (let ((case-fold-search nil)
+ (start (point-max))
+ end)
+ (catch 'convert
+ (goto-char start)
+ (while (re-search-backward
+ pmail-unix-mail-delimiter nil t)
+ (setq end start)
+ (setq start (point))
+ (save-excursion
+ (save-restriction
+ (narrow-to-region start end)
+ (goto-char start)
+ (let ((attribute (pmail-header-get-header
+ pmail-header-attribute-header)))
+ (unless attribute
+ (throw 'convert t)))))))))))
+ (if convert
+ (let ((inhibit-read-only t))
+ (pmail-convert-mbox-format)))))
+
+(defun pmail-initialize-messages ()
+ "Initialize message state based on messages in the buffer."
+ (setq pmail-total-messages 0
+ pmail-current-message 1)
+ (pmail-desc-clear-descriptors)
+ (widen)
+ (pmail-header-show-headers)
+ (setq pmail-total-messages (pmail-process-new-messages)))
+
+(defvar pmail-mode-map
+ (let ((map (make-sparse-keymap)))
+ (suppress-keymap map)
+ (define-key map "a" 'pmail-add-label)
+ (define-key map "b" 'pmail-bury)
+ (define-key map "c" 'pmail-continue)
+ (define-key map "d" 'pmail-delete-forward)
+ (define-key map "\C-d" 'pmail-delete-backward)
+ (define-key map "e" 'pmail-edit-current-message)
+ (define-key map "f" 'pmail-forward)
+ (define-key map "g" 'pmail-get-new-mail)
+ (define-key map "h" 'pmail-summary)
+ (define-key map "i" 'pmail-input)
+ (define-key map "j" 'pmail-show-message)
+ (define-key map "k" 'pmail-kill-label)
+ (define-key map "l" 'pmail-summary-by-labels)
+ (define-key map "\e\C-h" 'pmail-summary)
+ (define-key map "\e\C-l" 'pmail-summary-by-labels)
+ (define-key map "\e\C-r" 'pmail-summary-by-recipients)
+ (define-key map "\e\C-s" 'pmail-summary-by-regexp)
+ (define-key map "\e\C-t" 'pmail-summary-by-topic)
+ (define-key map "m" 'pmail-mail)
+ (define-key map "\em" 'pmail-retry-failure)
+ (define-key map "n" 'pmail-next-undeleted-message)
+ (define-key map "\en" 'pmail-next-message)
+ (define-key map "\e\C-n" 'pmail-next-labeled-message)
+ (define-key map "o" 'pmail-output)
+ (define-key map "\C-o" 'pmail-output)
+ (define-key map "p" 'pmail-previous-undeleted-message)
+ (define-key map "\ep" 'pmail-previous-message)
+ (define-key map "\e\C-p" 'pmail-previous-labeled-message)
+ (define-key map "q" 'pmail-quit)
+ (define-key map "r" 'pmail-reply)
+ ;; I find I can't live without the default M-r command -- rms.
+ ;; (define-key map "\er" 'pmail-search-backwards)
+ (define-key map "s" 'pmail-expunge-and-save)
+ (define-key map "\es" 'pmail-search)
+ (define-key map "t" 'pmail-toggle-header)
+ (define-key map "u" 'pmail-undelete-previous-message)
+ (define-key map "w" 'pmail-output-body-to-file)
+ (define-key map "x" 'pmail-expunge)
+ (define-key map "." 'pmail-beginning-of-message)
+ (define-key map "/" 'pmail-end-of-message)
+ (define-key map "<" 'pmail-first-message)
+ (define-key map ">" 'pmail-last-message)
+ (define-key map " " 'scroll-up)
+ (define-key map "\177" 'scroll-down)
+ (define-key map "?" 'describe-mode)
+ (define-key map "\C-c\C-s\C-d" 'pmail-sort-by-date)
+ (define-key map "\C-c\C-s\C-s" 'pmail-sort-by-subject)
+ (define-key map "\C-c\C-s\C-a" 'pmail-sort-by-author)
+ (define-key map "\C-c\C-s\C-r" 'pmail-sort-by-recipient)
+ (define-key map "\C-c\C-s\C-c" 'pmail-sort-by-correspondent)
+ (define-key map "\C-c\C-s\C-l" 'pmail-sort-by-lines)
+ (define-key map "\C-c\C-s\C-k" 'pmail-sort-by-labels)
+ (define-key map "\C-c\C-n" 'pmail-next-same-subject)
+ (define-key map "\C-c\C-p" 'pmail-previous-same-subject)
+ (define-key map [menu-bar] (make-sparse-keymap))
+ (define-key map [menu-bar classify]
+ (cons "Classify" (make-sparse-keymap "Classify")))
+ (define-key map [menu-bar classify input-menu]
+ nil)
+ (define-key map [menu-bar classify output-menu]
+ nil)
+ (define-key map [menu-bar classify output-body]
+ '("Output body to file..." . pmail-output-body-to-file))
+ (define-key map [menu-bar classify output-inbox]
+ '("Output (inbox)..." . pmail-output))
+ (define-key map [menu-bar classify output]
+ '("Output (Pmail)..." . pmail-output))
+ (define-key map [menu-bar classify kill-label]
+ '("Kill Label..." . pmail-kill-label))
+ (define-key map [menu-bar classify add-label]
+ '("Add Label..." . pmail-add-label))
+ (define-key map [menu-bar summary]
+ (cons "Summary" (make-sparse-keymap "Summary")))
+ (define-key map [menu-bar summary senders]
+ '("By Senders..." . pmail-summary-by-senders))
+ (define-key map [menu-bar summary labels]
+ '("By Labels..." . pmail-summary-by-labels))
+ (define-key map [menu-bar summary recipients]
+ '("By Recipients..." . pmail-summary-by-recipients))
+ (define-key map [menu-bar summary topic]
+ '("By Topic..." . pmail-summary-by-topic))
+ (define-key map [menu-bar summary regexp]
+ '("By Regexp..." . pmail-summary-by-regexp))
+ (define-key map [menu-bar summary all]
+ '("All" . pmail-summary))
+ (define-key map [menu-bar mail]
+ (cons "Mail" (make-sparse-keymap "Mail")))
+ (define-key map [menu-bar mail pmail-get-new-mail]
+ '("Get New Mail" . pmail-get-new-mail))
+ (define-key map [menu-bar mail lambda]
+ '("----"))
+ (define-key map [menu-bar mail continue]
+ '("Continue" . pmail-continue))
+ (define-key map [menu-bar mail resend]
+ '("Re-send..." . pmail-resend))
+ (define-key map [menu-bar mail forward]
+ '("Forward" . pmail-forward))
+ (define-key map [menu-bar mail retry]
+ '("Retry" . pmail-retry-failure))
+ (define-key map [menu-bar mail reply]
+ '("Reply" . pmail-reply))
+ (define-key map [menu-bar mail mail]
+ '("Mail" . pmail-mail))
+ (define-key map [menu-bar delete]
+ (cons "Delete" (make-sparse-keymap "Delete")))
+ (define-key map [menu-bar delete expunge/save]
+ '("Expunge/Save" . pmail-expunge-and-save))
+ (define-key map [menu-bar delete expunge]
+ '("Expunge" . pmail-expunge))
+ (define-key map [menu-bar delete undelete]
+ '("Undelete" . pmail-undelete-previous-message))
+ (define-key map [menu-bar delete delete]
+ '("Delete" . pmail-delete-forward))
+ (define-key map [menu-bar move]
+ (cons "Move" (make-sparse-keymap "Move")))
+ (define-key map [menu-bar move search-back]
+ '("Search Back..." . pmail-search-backwards))
+ (define-key map [menu-bar move search]
+ '("Search..." . pmail-search))
+ (define-key map [menu-bar move previous]
+ '("Previous Nondeleted" . pmail-previous-undeleted-message))
+ (define-key map [menu-bar move next]
+ '("Next Nondeleted" . pmail-next-undeleted-message))
+ (define-key map [menu-bar move last]
+ '("Last" . pmail-last-message))
+ (define-key map [menu-bar move first]
+ '("First" . pmail-first-message))
+ (define-key map [menu-bar move previous]
+ '("Previous" . pmail-previous-message))
+ (define-key map [menu-bar move next]
+ '("Next" . pmail-next-message))
+ map)
+ "Keymap for `pmail-mode'.")
+
+;; Pmail toolbar
+(defvar pmail-tool-bar-map
+ (if (display-graphic-p)
+ (let ((map (make-sparse-keymap)))
+ (tool-bar-local-item-from-menu 'pmail-get-new-mail "mail/inbox"
+ map pmail-mode-map)
+ (tool-bar-local-item-from-menu 'pmail-next-undeleted-message "right-arrow"
+ map pmail-mode-map)
+ (tool-bar-local-item-from-menu 'pmail-previous-undeleted-message "left-arrow"
+ map pmail-mode-map)
+ (tool-bar-local-item-from-menu 'pmail-search "search"
+ map pmail-mode-map)
+ (tool-bar-local-item-from-menu 'pmail-input "open"
+ map pmail-mode-map)
+ (tool-bar-local-item-from-menu 'pmail-mail "mail/compose"
+ map pmail-mode-map)
+ (tool-bar-local-item-from-menu 'pmail-reply "mail/reply-all"
+ map pmail-mode-map)
+ (tool-bar-local-item-from-menu 'pmail-forward "mail/forward"
+ map pmail-mode-map)
+ (tool-bar-local-item-from-menu 'pmail-delete-forward "close"
+ map pmail-mode-map)
+ (tool-bar-local-item-from-menu 'pmail-output "mail/move"
+ map pmail-mode-map)
+ (tool-bar-local-item-from-menu 'pmail-output-body-to-file "mail/save"
+ map pmail-mode-map)
+ (tool-bar-local-item-from-menu 'pmail-expunge "delete"
+ map pmail-mode-map)
+ map)))
+
+
+\f
+;; Pmail mode is suitable only for specially formatted data.
+(put 'pmail-mode 'mode-class 'special)
+
+(defun pmail-mode-kill-summary ()
+ (if pmail-summary-buffer (kill-buffer pmail-summary-buffer)))
+
+;;;###autoload
+(defun pmail-mode ()
+ "Pmail Mode is used by \\<pmail-mode-map>\\[pmail] for editing Pmail files.
+All normal editing commands are turned off.
+Instead, these commands are available:
+
+\\[pmail-beginning-of-message] Move point to front of this message.
+\\[pmail-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.
+\\[pmail-next-undeleted-message] Move to Next non-deleted message.
+\\[pmail-previous-undeleted-message] Move to Previous non-deleted message.
+\\[pmail-next-message] Move to Next message whether deleted or not.
+\\[pmail-previous-message] Move to Previous message whether deleted or not.
+\\[pmail-first-message] Move to the first message in Pmail file.
+\\[pmail-last-message] Move to the last message in Pmail file.
+\\[pmail-show-message] Jump to message specified by numeric position in file.
+\\[pmail-search] Search for string and show message it is found in.
+\\[pmail-delete-forward] Delete this message, move to next nondeleted.
+\\[pmail-delete-backward] Delete this message, move to previous nondeleted.
+\\[pmail-undelete-previous-message] Undelete message. Tries current message, then earlier messages
+ till a deleted message is found.
+\\[pmail-edit-current-message] Edit the current message. \\[pmail-cease-edit] to return to Pmail.
+\\[pmail-expunge] Expunge deleted messages.
+\\[pmail-expunge-and-save] Expunge and save the file.
+\\[pmail-quit] Quit Pmail: expunge, save, then switch to another buffer.
+\\[save-buffer] Save without expunging.
+\\[pmail-get-new-mail] Move new mail from system spool directory into this file.
+\\[pmail-mail] Mail a message (same as \\[mail-other-window]).
+\\[pmail-continue] Continue composing outgoing message started before.
+\\[pmail-reply] Reply to this message. Like \\[pmail-mail] but initializes some fields.
+\\[pmail-retry-failure] Send this message again. Used on a mailer failure message.
+\\[pmail-forward] Forward this message to another user.
+\\[pmail-output-to-pmail-file] Output this message to an Pmail file (append it).
+\\[pmail-output] Output this message to a Unix-format mail file (append it).
+\\[pmail-output-body-to-file] Save message body to a file. Default filename comes from Subject line.
+\\[pmail-input] Input Pmail file. Run Pmail on that file.
+\\[pmail-add-label] Add label to message. It will be displayed in the mode line.
+\\[pmail-kill-label] Kill label. Remove a label from current message.
+\\[pmail-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 \\[pmail-add-label].
+\\[pmail-previous-labeled-message] Move to Previous message with specified label
+\\[pmail-summary] Show headers buffer, with a one line summary of each message.
+\\[pmail-summary-by-labels] Summarize only messages with particular label(s).
+\\[pmail-summary-by-recipients] Summarize only messages with particular recipient(s).
+\\[pmail-summary-by-regexp] Summarize only messages with particular regexp(s).
+\\[pmail-summary-by-topic] Summarize only messages with subject line regexp(s).
+\\[pmail-toggle-header] Toggle display of complete header."
+ (interactive)
+ (let ((finding-pmail-file (not (eq major-mode 'pmail-mode))))
+ (pmail-mode-2)
+ (when (and finding-pmail-file
+ (null coding-system-for-read)
+ default-enable-multibyte-characters)
+ (let ((pmail-enable-multibyte t))
+ (pmail-require-mime-maybe)
+ (goto-char (point-max))
+ (set-buffer-multibyte t)))
+ (pmail-show-message pmail-total-messages)
+ (when finding-pmail-file
+ (when pmail-display-summary
+ (pmail-summary))
+ (pmail-construct-io-menu))
+ (run-mode-hooks 'pmail-mode-hook)))
+
+(defun pmail-mode-2 ()
+ (kill-all-local-variables)
+ (pmail-mode-1)
+ (pmail-perm-variables)
+ (pmail-variables))
+
+(defun pmail-mode-1 ()
+ (setq major-mode 'pmail-mode)
+ (setq mode-name "PMAIL")
+ (setq buffer-read-only t)
+ ;; No need to auto save PMAIL 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 an Pmail mode buffer.
+ ;; pmail-output-to-pmail-file enables auto save when you do that.
+ (setq buffer-auto-save-file-name nil)
+ (setq mode-line-modified "--")
+ (use-local-map pmail-mode-map)
+ (set-syntax-table text-mode-syntax-table)
+ (setq local-abbrev-table text-mode-abbrev-table))
+
+;; Set up the permanent locals associated with an Pmail file.
+(defun pmail-perm-variables ()
+ (make-local-variable 'pmail-last-label)
+ (make-local-variable 'pmail-last-regexp)
+ (make-local-variable 'pmail-buffer)
+ (setq pmail-buffer (current-buffer))
+ (make-local-variable 'pmail-view-buffer)
+ (setq pmail-view-buffer pmail-buffer)
+ (make-local-variable 'pmail-summary-buffer)
+ (make-local-variable 'pmail-current-message)
+ (make-local-variable 'pmail-total-messages)
+ (make-local-variable 'pmail-overlay-list)
+ (setq pmail-overlay-list nil)
+ (make-local-variable 'pmail-desc-vector)
+ (make-local-variable 'pmail-inbox-list)
+ (setq pmail-inbox-list (pmail-get-file-inbox-list))
+ ;; Provide default set of inboxes for primary mail file ~/PMAIL.
+ (and (null pmail-inbox-list)
+ (or (equal buffer-file-name (expand-file-name pmail-file-name))
+ (equal buffer-file-truename
+ (abbreviate-file-name (file-truename pmail-file-name))))
+ (setq pmail-inbox-list
+ (or pmail-primary-inbox-list
+ (list (or (getenv "MAIL")
+ (concat rmail-spool-directory
+ (user-login-name)))))))
+ (make-local-variable 'pmail-keywords)
+ (set (make-local-variable 'tool-bar-map) pmail-tool-bar-map)
+ ;; this gets generated as needed
+ (setq pmail-keywords nil))
+
+;; Set up the non-permanent locals associated with Pmail mode.
+(defun pmail-variables ()
+ ;; 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 'pmail-revert)
+ (make-local-variable 'font-lock-defaults)
+ (setq font-lock-defaults
+ '(pmail-font-lock-keywords
+ t t nil nil
+ (font-lock-maximum-size . nil)
+ (font-lock-fontify-buffer-function . pmail-fontify-buffer-function)
+ (font-lock-unfontify-buffer-function . pmail-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 'pmail-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 pmail-mode buffer.
+(defun pmail-revert (arg noconfirm)
+ (with-current-buffer pmail-buffer
+ (let* ((revert-buffer-function (default-value 'revert-buffer-function))
+ (pmail-enable-multibyte enable-multibyte-characters))
+ ;; 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.
+ (with-current-buffer pmail-buffer
+ (pmail-mode-2)
+ (pmail-convert-file)
+ ;; We have read the file as raw-text, so the buffer is set to
+ ;; unibyte. Make it multibyte if necessary.
+ (when (and pmail-enable-multibyte
+ (not enable-multibyte-characters))
+ (set-buffer-multibyte t))
+ (pmail-initialize-messages)
+ (pmail-show-message pmail-total-messages)
+ (run-hooks 'pmail-mode-hook))))))
+
+(defun pmail-get-file-inbox-list ()
+ "Return a list of inbox files for this buffer."
+ (let* ((filename (expand-file-name (buffer-file-name)))
+ (inboxes (cdr (or (assoc filename pmail-inbox-alist)
+ (assoc (abbreviate-file-name filename)
+ pmail-inbox-alist))))
+ (list nil))
+ (dolist (i inboxes)
+ (when (file-name-absolute-p i)
+ (push (expand-file-name i) list)))
+ (nreverse list)))
+
+;;; mbox: ready
+(defun pmail-expunge-and-save ()
+ "Expunge and save PMAIL file."
+ (interactive)
+ (pmail-expunge)
+ (save-buffer)
+ (pmail-display-summary-maybe))
+
+;;; mbox: ready
+(defun pmail-display-summary-maybe ()
+ "If a summary buffer exists then make sure it is updated and displayed."
+ (if (pmail-summary-exists)
+ (let ((current-message pmail-current-message))
+ (pmail-select-summary
+ (pmail-summary-goto-msg current-message)
+ (pmail-summary-pmail-update)
+ (set-buffer-modified-p nil)))))
+
+;;; mbox: ready
+(defun pmail-quit ()
+ "Quit out of PMAIL.
+Hook `pmail-quit-hook' is run after expunging."
+ (interactive)
+ (pmail-expunge-and-save)
+ (when (boundp 'pmail-quit-hook)
+ (run-hooks 'pmail-quit-hook))
+ ;; Don't switch to the summary buffer even if it was recently visible.
+ (when pmail-summary-buffer
+ (replace-buffer-in-windows pmail-summary-buffer)
+ (bury-buffer pmail-summary-buffer))
+ (if pmail-enable-mime
+ (let ((obuf pmail-buffer)
+ (ovbuf pmail-view-buffer))
+ (set-buffer pmail-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))))
+
+;;; mbox: ready
+(defun pmail-bury ()
+ "Bury current Pmail buffer and its summary buffer."
+ (interactive)
+ ;; This let var was called pmail-buffer, but that interfered
+ ;; with the buffer-local var used in summary buffers.
+ (let ((buffer-to-bury (current-buffer)))
+ (if (pmail-summary-exists)
+ (let (window)
+ (while (setq window (get-buffer-window pmail-summary-buffer))
+ (quit-window nil window))
+ (bury-buffer pmail-summary-buffer)))
+ (quit-window)))
+
+;;;??? Fails to add descriptor for new message.
+;;; mbox: ready
+(defun pmail-duplicate-message ()
+ "Create a duplicated copy of the current message.
+The duplicate copy goes into the Pmail file just after the
+original copy."
+ (interactive)
+ (widen)
+ (let ((buffer-read-only nil)
+ (number pmail-current-message)
+ (string (buffer-substring (pmail-desc-get-start pmail-current-message)
+ (pmail-desc-get-end pmail-current-message))))
+ (goto-char (pmail-desc-get-end pmail-current-message))
+ (insert string)
+ (pmail-show-message number)
+ (message "Message duplicated")))
+
+;;;###autoload
+(defun pmail-input (filename)
+ "Run Pmail on file FILENAME."
+ (interactive "FRun pmail on PMAIL file: ")
+ (pmail 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 pmail-find-all-files (start)
+ "Return list of file in dir START that match `pmail-secondary-file-regexp'."
+ (if (file-accessible-directory-p start)
+ ;; Don't sort here.
+ (let* ((case-fold-search t)
+ (files (directory-files start t pmail-secondary-file-regexp)))
+ ;; Sort here instead of in directory-files
+ ;; because this list is usually much shorter.
+ (sort files 'string<))))
+
+(defun pmail-list-to-menu (menu-name l action &optional full-name)
+ (let ((menu (make-sparse-keymap menu-name)))
+ (mapc
+ (function (lambda (item)
+ (let (command)
+ (if (consp item)
+ (progn
+ (setq command
+ (pmail-list-to-menu (car item) (cdr item)
+ action
+ (if full-name
+ (concat full-name "/"
+ (car item))
+ (car item))))
+ (setq name (car item)))
+ (progn
+ (setq name item)
+ (setq command
+ (list 'lambda () '(interactive)
+ (list action
+ (expand-file-name
+ (if full-name
+ (concat full-name "/" item)
+ item)
+ pmail-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 'pmail-disable-menu 'menu-enable ''nil)
+
+(defun pmail-construct-io-menu ()
+ (let ((files (pmail-find-all-files pmail-secondary-file-directory)))
+ (if files
+ (progn
+ (define-key pmail-mode-map [menu-bar classify input-menu]
+ (cons "Input Pmail File"
+ (pmail-list-to-menu "Input Pmail File"
+ files
+ 'pmail-input)))
+ (define-key pmail-mode-map [menu-bar classify output-menu]
+ (cons "Output Pmail File"
+ (pmail-list-to-menu "Output Pmail File"
+ files
+ 'pmail-output))))
+
+ (define-key pmail-mode-map [menu-bar classify input-menu]
+ '("Input Pmail File" . pmail-disable-menu))
+ (define-key pmail-mode-map [menu-bar classify output-menu]
+ '("Output Pmail File" . pmail-disable-menu)))))
+
+\f
+;;;; *** Pmail input ***
+
+(declare-function pmail-spam-filter "pmail-spam-filter" (msg))
+(declare-function pmail-summary-goto-msg "pmailsum" (&optional n nowarn skip-pmail))
+(declare-function pmail-summary-mark-undeleted "pmailsum" (n))
+(declare-function pmail-summary-mark-deleted "pmailsum" (&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" ())
+
+(defun pmail-get-inbox-files ()
+ "Return all files from `pmail-inbox-list' without name conflicts.
+A conflict happens when two inbox file names have the same name
+according to `file-name-nondirectory'."
+ (let (files last-names)
+ (catch 'conflict
+ (dolist (file pmail-inbox-list)
+ (if (member (file-name-nondirectory file) last-names)
+ (throw 'conflict t)
+ (push file files))
+ (push (file-name-nondirectory file) last-names)))
+ (nreverse files)))
+
+(defun pmail-delete-inbox-files (files)
+ "Delete all files given in FILES.
+If delete fails, truncate them to zero length."
+ (dolist (file files)
+ (condition-case nil
+ ;; First, try deleting.
+ (condition-case nil
+ (delete-file file)
+ ;; If we can't delete it, truncate it.
+ (file-error (write-region (point) (point) file)))
+ (file-error nil))))
+
+(defun pmail-get-new-mail (&optional file-name)
+ "Move any new mail from this mail file's inbox files.
+The inbox files for the primary mail file are determined using
+various means when setting up the buffer. The list of inbox
+files are stored in `pmail-inbox-list'.
+
+The most important variable that determines the value of this
+list is `pmail-inbox-alist' which lists the inbox files for any
+mail files you might be using.
+
+If the above yields no inbox files, and if this is the primary
+mail file as determined by `pmail-file-name', the inbox lists
+otherwise defaults to `pmail-primary-inbox-list' if set, or the
+environment variable MAIL if set, or the user's mail file in
+`rmail-spool-directory'.
+
+This is why, by default, no mail file has inbox files, except for
+the primary mail file ~/PMAIL, which gets its new mail from the
+mail spool.
+
+You can also specify the file to get new mail from interactively.
+A prefix argument will read a file name and use that file as the
+inbox. Noninteractively, you can pass the inbox file name as an
+argument.
+
+If the variable `pmail-preserve-inbox' is non-nil, new mail will
+always be left in inbox files rather than deleted.
+
+This function runs `pmail-get-new-mail-hook' before saving the
+updated file. It returns t if it got any new messages."
+ (interactive
+ (list (when current-prefix-arg
+ (read-file-name "Get new mail from file: "))))
+ (run-hooks 'pmail-before-get-new-mail-hook)
+ ;; If the disk file has been changed from under us, revert to it
+ ;; before we get new mail.
+ (unless (verify-visited-file-modtime (current-buffer))
+ (find-file (buffer-file-name)))
+ (with-current-buffer pmail-buffer
+ (widen)
+ ;; Get rid of all undo records for this buffer.
+ (unless (eq buffer-undo-list t)
+ (setq buffer-undo-list nil))
+ (let ((pmail-enable-multibyte (default-value 'enable-multibyte-characters))
+ ;; 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)))
+ current-message found)
+ (condition-case nil
+ (let ((buffer-read-only nil)
+ (buffer-undo-list t)
+ (delete-files nil)
+ (new-messages 0)
+ (rsf-number-of-spam 0))
+ (save-excursion
+ (save-restriction
+ (goto-char (point-max))
+ (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
+ (pmail-insert-inbox-text (list file-name) nil)
+ (setq delete-files (pmail-insert-inbox-text
+ (pmail-get-inbox-files) t)))
+ ;; Process newly found messages and save them into the
+ ;; PMAIL file.
+ (unless (equal (point-min) (point-max))
+ (setq new-messages (pmail-convert-mbox-format))
+ (unless (zerop new-messages)
+ (pmail-process-new-messages)
+ (setq pmail-current-message (1+ pmail-total-messages)
+ pmail-total-messages (pmail-desc-get-count)))
+ (save-buffer))
+ ;; Delete the old files, now that the PMAIL file is
+ ;; saved.
+ (when delete-files
+ (pmail-delete-inbox-files delete-files))))
+
+ (if (zerop new-messages)
+ (when (or file-name pmail-inbox-list)
+ (message "(No new mail has arrived)"))
+
+ ;; Process the new messages for spam using the integrated
+ ;; spam filter. The spam filter can mark messages for
+ ;; deletion and can output a message.
+ ;; XXX pmail-spam-filter hasn't been tested at all with
+ ;; the mbox branch. --enberg
+ (setq current-message (pmail-first-unseen-message))
+ (when pmail-use-spam-filter
+ (while (<= current-message pmail-total-messages)
+ (pmail-spam-filter current-message)
+ (setq current-message (1+ current-message))))
+ ;; Make the first unseen message the current message and
+ ;; update the summary buffer, if one exists.
+ (setq current-message (pmail-first-unseen-message))
+ (if (pmail-summary-exists)
+ (with-current-buffer pmail-summary-buffer
+ (pmail-update-summary)
+ (pmail-summary-goto-msg current-message))
+ (pmail-show-message current-message))
+ ;; Run the after get new mail hook.
+ (run-hooks 'pmail-after-get-new-mail-hook)
+ (message "%d new message%s read"
+ new-messages (if (= 1 new-messages) "" "s"))
+ (setq found t))
+ found)
+ ;; Don't leave the buffer screwed up if we get a disk-full error.
+ (file-error (or found (pmail-show-message)))))))
+
+(defun pmail-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 pmail-remote-password-required
+ (setq got-password (not (pmail-have-password)))
+ (setq supplied-password (pmail-get-remote-password
+ (string-equal proto "imap")))))
+
+ (if (pmail-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"))
+ supplied-password
+ got-password))))
+
+ ((string-match "^po:\\([^:]+\\)\\(:\\(.*\\)\\)?" file)
+ (let (got-password supplied-password
+ (proto "pop")
+ (user (match-string 1 file))
+ (host (match-string 3 file)))
+
+ (when pmail-remote-password-required
+ (setq got-password (not (pmail-have-password)))
+ (setq supplied-password (pmail-get-remote-password nil)))
+
+ (list file "pop" supplied-password got-password)))
+
+ (t
+ (list file nil nil nil))))
+
+(defun pmail-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 "PMAIL file %s is locked"
+ (file-name-nondirectory buffer-file-name)))
+ (let (file tofile delete-files movemail popmail 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 (pmail-parse-url file)))
+ (setq file (nth 0 url-data))
+ (setq popmail (nth 1 url-data))
+ (setq password (nth 2 url-data))
+ (setq got-password (nth 3 url-data)))
+
+ (if popmail
+ (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))
+ ;; cannot have "po:" in file name
+ (substring file 3)
+ file)))
+ ;; Use the directory of this pmail file
+ ;; because it's a nuisance to use the homedir
+ ;; if that is on a full disk and this pmail
+ ;; 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.
+ (if (not popmail)
+ (progn
+ ;; 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 (popmail
+ (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 popmail)
+ (not (file-exists-p file))))
+ nil)
+ (t
+ (with-temp-buffer
+ (let ((errors (current-buffer)))
+ (buffer-disable-undo errors)
+ (let ((args
+ (append
+ (list pmail-movemail-program nil errors nil)
+ (if pmail-preserve-inbox
+ (list "-p")
+ nil)
+ (if (pmail-movemail-variant-p 'mailutils)
+ (append (list "--emacs") pmail-movemail-flags)
+ pmail-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 pmail-remote-password-error
+ nil t))
+ (pmail-set-remote-password nil))
+
+ ;; If using Mailutils, remove initial error code
+ ;; abbreviation
+ (when (pmail-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)))
+ (goto-char (point-max))
+ (or (= (preceding-char) ?\n)
+ (zerop size)
+ (insert ?\n))
+ (if (not (and pmail-preserve-inbox (string= file tofile)))
+ (setq delete-files (cons tofile delete-files)))))
+ (message "")
+ (setq files (cdr files)))
+ delete-files))
+\f
+;;;; *** Pmail message decoding ***
+
+;; these two are unused, and possibly harmul.
+
+;; (defun pmail-decode-region (from to coding)
+;; "Decode the region specified by FROM and TO by CODING.
+;; If CODING is nil or an invalid coding system, decode by `undecided'."
+;; (unless (and coding (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 'dos))
+;; ;; Don't reveal the fact we used -dos decoding, as users generally
+;; ;; will not expect the PMAIL buffer to use DOS EOL format.
+;; (setq buffer-file-coding-system
+;; (setq last-coding-system-used
+;; (coding-system-change-eol-conversion
+;; coding 'unix))))
+
+;; (defun pmail-decode-by-content-type (from to)
+;; "Decode message between FROM and TO according to Content-Type."
+;; (when (and (not pmail-enable-mime) pmail-enable-multibyte)
+;; (let ((coding-system-used nil)
+;; (case-fold-search t))
+;; (save-restriction
+;; (narrow-to-region from to)
+;; (when (and (not pmail-enable-mime) pmail-enable-multibyte)
+;; (let ((coding
+;; (when (save-excursion
+;; (goto-char (pmail-header-get-limit))
+;; (re-search-backward
+;; pmail-mime-charset-pattern
+;; (point-min) t))
+;; (intern (downcase (match-string 1))))))
+;; (setq coding-system-used (pmail-decode-region
+;; (point-min) (point-max)
+;; coding)))))
+;; (setq last-coding-system-used coding-system-used))))
+\f
+;;;; *** Pmail Message Formatting and Header Manipulation ***
+
+(defun pmail-clear-headers (&optional ignored-headers)
+ "Delete all header fields that Pmail should not show.
+If the optional argument IGNORED-HEADERS is non-nil,
+delete all header fields whose names match that regexp.
+Otherwise, if `pmail-displayed-headers' is non-nil,
+delete all header fields *except* those whose names match that regexp.
+Otherwise, delete all header fields whose names match `pmail-ignored-headers'
+unless they also match `pmail-nonignored-headers'."
+ (when (search-forward "\n\n" nil t)
+ (forward-char -1)
+ (let ((case-fold-search t)
+ (buffer-read-only nil))
+ (if (and pmail-displayed-headers (null ignored-headers))
+ (save-restriction
+ (narrow-to-region (point-min) (point))
+ (let (lim next)
+ (goto-char (point-min))
+ (while (and (not (eobp))
+ (save-excursion
+ (if (re-search-forward "\n[^ \t]" nil t)
+ (setq lim (match-beginning 0)
+ next (1+ lim))
+ (setq lim nil next (point-max)))))
+ (if (save-excursion
+ (re-search-forward pmail-displayed-headers lim t))
+ (goto-char next)
+ (delete-region (point) next))))
+ (goto-char (point-min)))
+ (or ignored-headers (setq ignored-headers pmail-ignored-headers))
+ (save-restriction
+ (narrow-to-region (point-min) (point))
+ (goto-char (point-min))
+ (while (and ignored-headers
+ (re-search-forward ignored-headers nil t))
+ (beginning-of-line)
+ (if (and pmail-nonignored-headers
+ (looking-at pmail-nonignored-headers))
+ (forward-line 1)
+ (delete-region (point)
+ (save-excursion
+ (if (re-search-forward "\n[^ \t]" nil t)
+ (1- (point))
+ (point-max)))))))))))
+
+(defun pmail-msg-is-pruned (&optional msg)
+ "Determine if the headers for the current message are being
+ displayed. If MSG is non-nil it will be used as the message number
+ instead of the current message."
+ (pmail-desc-get-header-display-state (or msg pmail-current-message)))
+
+(defun pmail-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")
+ (pmail-header-toggle-visibility arg))
+
+;; Lifted from repos-count-screen-lines.
+(defun pmail-count-screen-lines (start end)
+ "Return number of screen lines between START and END."
+ (save-excursion
+ (save-restriction
+ (narrow-to-region start end)
+ (goto-char (point-min))
+ (vertical-motion (- (point-max) (point-min))))))
+\f
+;;;; *** Pmail Attributes and Keywords ***
+
+;; Make a string describing the current message's attributes by
+;; keywords and set it up as the name of a minor mode so it will
+;; appear in the mode line.
+(defun pmail-display-labels ()
+ (let (keyword-list result)
+ ;; Update the keyword list for the current message.
+ (if (> pmail-current-message 0)
+ (setq keyword-list (pmail-desc-get-keywords pmail-current-message)))
+ ;; Generate the result string.
+ (setq result (mapconcat 'identity keyword-list " "))
+ ;; Update the mode line to display the keywords, the current
+ ;; message index and the total number of messages.
+ (setq mode-line-process
+ (format " %d/%d%s"
+ pmail-current-message pmail-total-messages
+ (if keyword-list (concat " " result) "")))
+ ;; If pmail-enable-mime is non-nil, we may have to update
+ ;; `mode-line-process' of pmail-view-buffer too.
+ (if (and pmail-enable-mime
+ (not (eq (current-buffer) pmail-view-buffer))
+ (buffer-live-p pmail-view-buffer))
+ (let ((mlp mode-line-process))
+ (with-current-buffer pmail-view-buffer
+ (setq mode-line-process mlp))))))
+
+(defun pmail-set-attribute (attr state &optional msgnum)
+ "Turn a attribute ATTR of a message on or off according to STATE.
+ATTR is a string, MSGNUM is the optional message number. By
+default, the current message is changed."
+ (save-excursion
+ (save-restriction
+ (let ((attr-index (pmail-desc-get-attr-index attr)))
+ (set-buffer pmail-buffer)
+ (or msgnum (setq msgnum pmail-current-message))
+ (pmail-desc-set-attribute attr-index state msgnum)
+ ;; Deal with the summary buffer.
+ (when pmail-summary-buffer
+ (pmail-summary-update msgnum))))))
+
+(defun pmail-message-labels-p (n labels)
+ "Return t if message number N has keywords matching LABELS.
+LABELS is a regular expression."
+ (catch 'found
+ (dolist (keyword (pmail-desc-get-keywords n))
+ (when (string-match labels keyword)
+ (throw 'found t)))))
+
+\f
+;;;; *** Pmail Message Selection And Support ***
+
+(defun pmail-msgbeg (n)
+ (pmail-desc-get-start n))
+(make-obsolete 'pmail-msgbeg 'pmail-desc-get-start "22.0")
+
+(defun pmail-msgend (n)
+ (pmail-desc-get-end n))
+(make-obsolete 'pmail-msgend 'pmail-desc-get-end "22.0")
+
+(defun pmail-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 Pmail.
+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 (pmail-desc-get-start pmail-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 (pmail-desc-get-start pmail-current-message)
+ (pmail-desc-get-end pmail-current-message)))))
+
+(defun pmail-process-new-messages (&optional nomsg)
+ "Process the new messages in the buffer.
+The buffer has been narrowed to expose only the new messages.
+For each new message append an entry to the message vector and,
+if necessary, add a header that will capture the salient BABYL
+information. Return the number of new messages. If NOMSG is
+non-nil then do not show any progress messages."
+ (let ((inhibit-read-only t)
+ (case-fold-search nil)
+ (new-message-counter 0)
+ (start (point-max))
+ end date keywords message-descriptor-list)
+ (or nomsg (message "Processing new messages..."))
+ ;; Process each message in turn starting from the back and
+ ;; proceeding to the front of the region. This is especially a
+ ;; good approach since the buffer will likely have new headers
+ ;; added.
+ (save-excursion
+ (goto-char start)
+ (while (re-search-backward pmail-unix-mail-delimiter nil t)
+ ;; Cache the message date to facilitate generating a message
+ ;; summary later. The format is '(DAY-OF-WEEK DAY-NUMBER MON
+ ;; YEAR TIME)
+ (setq date
+ (list (buffer-substring (match-beginning 2) (match-end 2))
+ (buffer-substring (match-beginning 4) (match-end 4))
+ (buffer-substring (match-beginning 3) (match-end 3))
+ (buffer-substring (match-beginning 7) (match-end 7))
+ (buffer-substring (match-beginning 5) (match-end 5))))
+ ;;Set start and end to bracket this message.
+ (setq end start)
+ (setq start (point))
+ (save-excursion
+ (save-restriction
+ (narrow-to-region start end)
+ (goto-char start)
+ ;; Bump the new message counter.
+ (setq new-message-counter (1+ new-message-counter))
+
+ ;; Set up keywords, if any. The keywords are provided via a
+ ;; comma separated list and returned as a list of strings.
+ (setq keywords (pmail-header-get-keywords))
+ (when keywords
+ ;; Keywords do exist. Register them with the keyword
+ ;; management library.
+ (pmail-register-keywords keywords))
+ ;; Insure that we have From and Date headers.
+ ;;(pmail-decode-from-line)
+ ;; Perform User defined filtering.
+ (save-excursion
+ (if pmail-message-filter (funcall pmail-message-filter)))
+ ;; Accumulate the message attributes along with the message
+ ;; markers and the message date list.
+ (setq message-descriptor-list
+ (vconcat (list (list (point-min-marker)
+ (pmail-header-get-header
+ pmail-header-attribute-header)
+ keywords
+ date
+ (count-lines start end)
+ (cadr (mail-extract-address-components; does not like nil
+ (or (pmail-header-get-header "from") "")))
+ (or (pmail-header-get-header "subject")
+ "none")))
+ message-descriptor-list)))))
+ ;; Add the new message data lists to the Pmail message descriptor
+ ;; vector.
+ (pmail-desc-add-descriptors message-descriptor-list)
+ ;; Unless requested otherwise, show the number of new messages.
+ ;; Return the number of new messages.
+ (or nomsg (message "Processing new messages...done (%d)"
+ new-message-counter))
+ new-message-counter)))
+
+(defun pmail-convert-mbox-format ()
+ (let ((case-fold-search nil)
+ (message-count 0)
+ (start (point-max))
+ end)
+ (save-excursion
+ (goto-char start)
+ (while (re-search-backward pmail-unix-mail-delimiter nil t)
+ (setq end start)
+ (setq start (point))
+ (save-excursion
+ (save-restriction
+ (narrow-to-region start end)
+ (goto-char (point-min))
+ ;; Bump the new message counter.
+ (setq message-count (1+ message-count))
+ ;; Detect messages that have been added with DOS line endings
+ ;; and convert the line endings for such messages.
+ (when (save-excursion (end-of-line) (= (preceding-char) ?\r))
+ (let ((buffer-read-only nil)
+ (buffer-undo t)
+ (end-marker (copy-marker end)))
+ (message
+ "Processing new messages...(converting line endings)")
+ (save-excursion
+ (goto-char (point-max))
+ (while (search-backward "\r\n" (point-min) t)
+ (delete-char 1)))
+ (setq end (marker-position end-marker))
+ (set-marker end-marker nil)))
+ ;; Make sure we have an Pmail BABYL attribute header field.
+ ;; All we can assume is that the Pmail BABYL header field is
+ ;; in the header section. It's placement can be modified by
+ ;; another mailer.
+ (let ((attributes (pmail-header-get-header
+ pmail-header-attribute-header)))
+ (unless attributes
+ ;; No suitable header exists. Append the default BABYL
+ ;; data header for a new message.
+ (pmail-header-add-header pmail-header-attribute-header
+ pmail-desc-default-attrs))))))
+ message-count)))
+
+(defun pmail-beginning-of-message ()
+ "Show current message starting from the beginning."
+ (interactive)
+ (let ((pmail-show-message-hook
+ (list (function (lambda ()
+ (goto-char (point-min)))))))
+ (pmail-show-message pmail-current-message)))
+
+(defun pmail-end-of-message ()
+ "Show bottom of current message."
+ (interactive)
+ (let ((pmail-show-message-hook
+ (list (function (lambda ()
+ (goto-char (point-max))
+ (recenter (1- (window-height))))))))
+ (pmail-show-message pmail-current-message)))
+
+(defun pmail-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 (pmail-header-get-header "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))
+ (and pmail-user-mail-address-regexp
+ (not (string-match pmail-user-mail-address-regexp
+ 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 pmail-show-message (&optional n no-summary)
+ "Show message number N (prefix argument), counting from start of file.
+If NO-SUMMARY is non-nil, then do not update the summary buffer."
+ (interactive "p")
+ (unless (eq major-mode 'pmail-mode)
+ (switch-to-buffer pmail-buffer))
+ (if (zerop pmail-total-messages)
+ (progn
+ (message "No messages to show. Add something better soon.")
+ (force-mode-line-update))
+ (let (blurb)
+ ;; Set n to the first sane message based on the sign of n:
+ ;; positive but greater than the total number of messages -> n;
+ ;; negative -> 1.
+ (if (not n)
+ (setq n pmail-current-message)
+ (cond ((<= n 0)
+ (setq n 1
+ pmail-current-message 1
+ blurb "No previous message"))
+ ((> n pmail-total-messages)
+ (setq n pmail-total-messages
+ pmail-current-message pmail-total-messages
+ blurb "No following message"))
+ (t
+ (setq pmail-current-message n))))
+ (let ((beg (pmail-desc-get-start n))
+ (end (pmail-desc-get-end n)))
+ (pmail-header-show-headers)
+ (widen)
+ (narrow-to-region beg end)
+ (goto-char (point-min))
+ ;; Clear the "unseen" attribute when we show a message, unless
+ ;; it is already cleared.
+ (when (pmail-desc-attr-p pmail-desc-unseen-index n)
+ (pmail-desc-set-attribute pmail-desc-unseen-index nil n))
+ (pmail-display-labels)
+ ;; Deal with MIME
+ (if (eq pmail-enable-mime t)
+ (funcall pmail-show-mime-function)
+ (setq pmail-view-buffer pmail-buffer))
+ (when mail-mailing-lists
+ (pmail-unknown-mail-followup-to))
+ (pmail-header-hide-headers)
+ (when transient-mark-mode (deactivate-mark))
+ ;; Make sure that point in the Pmail window is at the beginning
+ ;; of the buffer.
+ (goto-char (point-min))
+ (set-window-point (get-buffer-window pmail-buffer) (point))
+ ;; Run any User code.
+ (run-hooks 'pmail-show-message-hook)
+ ;; 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.
+ (when (and (pmail-summary-exists) (not no-summary))
+ (let ((curr-msg pmail-current-message))
+ ;; Set the summary current message, disabling the Pmail
+ ;; buffer update.
+ (with-current-buffer pmail-summary-buffer
+ (pmail-summary-goto-msg curr-msg nil t))))
+ (with-current-buffer pmail-buffer
+ (pmail-auto-file))
+ ;; Post back any status messages.
+ (when blurb
+ (message blurb))))))
+
+(defun pmail-redecode-body (coding)
+ "Decode the body of the current message using coding system CODING.
+This is useful with mail messages that have malformed or missing
+charset= headers.
+
+This function assumes that the current message is already decoded
+and displayed in the PMAIL buffer, but the coding system used to
+decode it was incorrect. It then encodes the message back to its
+original form, and decodes it again, using the coding system CODING.
+
+Note that if Emacs erroneously auto-detected one of the iso-2022
+encodings in the message, this function might fail because the escape
+sequences that switch between character sets and also single-shift and
+locking-shift codes are impossible to recover. This function is meant
+to be used to fix messages encoded with 8-bit encodings, such as
+iso-8859, koi8-r, etc."
+ (interactive "zCoding system for re-decoding this message: ")
+ (unless pmail-enable-mime
+ (with-current-buffer pmail-buffer
+ (save-excursion
+ (let ((start (pmail-desc-get-start pmail-current-message))
+ (end (pmail-desc-get-end pmail-current-message))
+ header)
+ (narrow-to-region start end)
+ (setq header (pmail-header-get-header "X-Coding-System"))
+ (if header
+ (let ((old-coding (intern header))
+ (buffer-read-only nil))
+ (check-coding-system old-coding)
+ ;; Make sure the new coding system uses the same EOL
+ ;; conversion, to prevent ^M characters from popping
+ ;; up all over the place.
+ (setq coding
+ (coding-system-change-eol-conversion
+ coding
+ (coding-system-eol-type old-coding)))
+ ;; Do the actual recoding.
+ (encode-coding-region start end old-coding)
+ (decode-coding-region start end coding)
+ ;; Rewrite the x-coding-system header according to
+ ;; what we did.
+ (setq last-coding-system-used coding)
+ (pmail-header-add-header
+ "X-Coding-System"
+ (symbol-name last-coding-system-used))
+ (pmail-show-message pmail-current-message))
+ (error "No X-Coding-System header found")))))))
+
+;; FIXME: Double-check this
+(defun pmail-auto-file ()
+ "Automatically move a message into a sub-folder based on criteria.
+Called when a new message is displayed."
+ (if (or (member "filed" (pmail-desc-get-keywords pmail-current-message))
+ (not (string= (buffer-file-name)
+ (expand-file-name pmail-file-name))))
+ ;; Do nothing if it's already been filed.
+ 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")))
+ (directives pmail-automatic-folder-directives)
+ (directive-loop nil)
+ (folder nil))
+ (while directives
+ (setq folder (car (car directives))
+ directive-loop (cdr (car directives)))
+ (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)
+ (pmail-delete-forward)
+ (if (string= "/dev/null" folder)
+ (pmail-delete-message)
+ (pmail-output folder 1 t)
+ (setq directives nil))))
+ (setq directives (cdr directives))))))
+
+(defun pmail-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")
+ (with-current-buffer pmail-buffer
+ (pmail-show-message (+ pmail-current-message n))))
+
+(defun pmail-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")
+ (pmail-next-message (- n)))
+
+(defun pmail-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")
+ (let ((lastwin pmail-current-message)
+ (original pmail-current-message)
+ (current pmail-current-message))
+ ;; Move forwards, remember the last undeleted message seen.
+ (while (and (> n 0) (< current pmail-total-messages))
+ (setq current (1+ current))
+ (unless (pmail-desc-deleted-p current)
+ (setq lastwin current
+ n (1- n))))
+ ;; Same thing for moving backwards
+ (while (and (< n 0) (> current 1))
+ (setq current (1- current))
+ (unless (pmail-desc-deleted-p current)
+ (setq lastwin current
+ n (1+ n))))
+ ;; Show the message (even if no movement took place so that the
+ ;; delete attribute is marked) and determine the result value.
+ (pmail-show-message lastwin)
+ (if (/= lastwin original)
+ t
+ (if (< n 0)
+ (message "No previous nondeleted message"))
+ (if (> n 0)
+ (message "No following nondeleted message"))
+ nil)))
+
+(defun pmail-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")
+ (pmail-next-undeleted-message (- n)))
+
+(defun pmail-first-message ()
+ "Show first message in file."
+ (interactive)
+ (pmail-show-message 1))
+
+(defun pmail-last-message ()
+ "Show last message in file."
+ (interactive)
+ (pmail-show-message pmail-total-messages))
+
+(defun pmail-narrow-to-header (msg)
+ "Narrow the buffer to the headers of message number MSG."
+ (save-excursion
+ (let ((start (pmail-desc-get-start msg))
+ (end (pmail-desc-get-end msg)))
+ (widen)
+ (goto-char start)
+ (unless (search-forward "\n\n" end t)
+ (error "Invalid message format"))
+ (narrow-to-region start (point)))))
+
+(defun pmail-message-recipients-p (msg recipients &optional primary-only)
+ (save-restriction
+ (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") ""))))))
+
+(defun pmail-message-regexp-p (msg regexp)
+ "Return t, if for message number MSG, regexp REGEXP matches in the header."
+ (save-excursion
+ (save-restriction
+ (pmail-narrow-to-header msg)
+ (re-search-forward regexp nil t))))
+
+(defun pmail-search-message (msg regexp)
+ "Return non-nil, if for message number MSG, regexp REGEXP matches."
+ (goto-char (pmail-desc-get-start msg))
+ (if pmail-enable-mime
+ (funcall pmail-search-mime-message-function msg regexp)
+ (re-search-forward regexp (pmail-desc-get-end msg) t)))
+
+(defvar pmail-search-last-regexp nil)
+(defun pmail-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 " "") "Pmail search (regexp"))
+ regexp)
+ (setq prompt
+ (concat prompt
+ (if pmail-search-last-regexp
+ (concat ", default "
+ pmail-search-last-regexp "): ")
+ "): ")))
+ (setq regexp (read-string prompt))
+ (cond ((not (equal regexp ""))
+ (setq pmail-search-last-regexp regexp))
+ ((not pmail-search-last-regexp)
+ (error "No previous Pmail search string")))
+ (list pmail-search-last-regexp
+ (prefix-numeric-value current-prefix-arg))))
+ (or n (setq n 1))
+ (message "%sPmail search for %s..."
+ (if (< n 0) "Reverse " "")
+ regexp)
+ (set-buffer pmail-buffer)
+ (let ((omin (point-min))
+ (omax (point-max))
+ (opoint (point))
+ (reversep (< n 0))
+ (msg pmail-current-message)
+ win)
+ (unwind-protect
+ (progn
+ (widen)
+ (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 win) (> msg 1))
+ (setq msg (1- msg)
+ win (pmail-search-message msg regexp)))
+ (while (and (null win) (< msg pmail-total-messages))
+ (setq msg (1+ msg)
+ win (pmail-search-message msg regexp))))
+ (setq n (+ n (if reversep 1 -1)))))
+ (if win
+ (progn
+ (pmail-show-message 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 "%sPmail search for %s...done"
+ (if reversep "Reverse " "")
+ regexp))
+ (goto-char opoint)
+ (narrow-to-region omin omax)
+ (ding)
+ (message "Search failed: %s" regexp)))))
+
+(defun pmail-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 " "") "Pmail search (regexp"))
+ regexp)
+ (setq prompt
+ (concat prompt
+ (if pmail-search-last-regexp
+ (concat ", default "
+ pmail-search-last-regexp "): ")
+ "): ")))
+ (setq regexp (read-string prompt))
+ (cond ((not (equal regexp ""))
+ (setq pmail-search-last-regexp regexp))
+ ((not pmail-search-last-regexp)
+ (error "No previous Pmail search string")))
+ (list pmail-search-last-regexp
+ (prefix-numeric-value current-prefix-arg))))
+ (pmail-search regexp (- (or n 1))))
+
+;; Show the first message which has the `unseen' attribute.
+(defun pmail-first-unseen-message ()
+ "Return the first message which has not been seen. If all messages
+have been seen, then return the last message."
+ (let ((current 1)
+ found)
+ (while (and (not found) (<= current pmail-total-messages))
+ (if (pmail-desc-attr-p pmail-desc-unseen-index current)
+ (setq found current))
+ (setq current (1+ current)))
+ (or found pmail-total-messages)))
+
+(defun pmail-current-subject ()
+ "Return the current subject.
+The subject is stripped of leading and trailing whitespace, and
+of typical reply prefixes such as Re:."
+ (let ((subject (or (mail-fetch-field "Subject") "")))
+ (if (string-match "\\`[ \t]+" subject)
+ (setq subject (substring subject (match-end 0))))
+ (if (string-match pmail-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 pmail-current-subject-regexp ()
+ "Return a regular expression matching the current subject.
+The regular expression matches the subject header line of
+messages about the same subject. The subject itself is stripped
+of leading and trailing whitespace, of typical reply prefixes
+such as Re: and whitespace within the subject is replaced by a
+regular expression matching whitespace in general in order to
+take into account that subject header lines may include newlines
+and more whitespace. The returned regular expressions contains
+`pmail-reply-regexp' and ends with a newline."
+ (let ((subject (pmail-current-subject)))
+ ;; If Subject is long, mailers will break it into several lines at
+ ;; arbitrary places, so replace whitespace with a regexp that will
+ ;; match any sequence of spaces, TABs, and newlines.
+ (setq subject (regexp-quote subject))
+ (setq subject
+ (replace-regexp-in-string "[ \t\n]+" "[ \t\n]+" subject t t))
+ ;; Some mailers insert extra spaces after "Subject:", so allow any
+ ;; amount of them.
+ (concat "^Subject:[ \t]+"
+ (if (string= "\\`" (substring pmail-reply-regexp 0 2))
+ (substring pmail-reply-regexp 2)
+ pmail-reply-regexp)
+ subject "[ \t]*\n")))
+
+(defun pmail-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 ((search-regexp (pmail-current-subject-regexp))
+ (forward (> n 0))
+ (i pmail-current-message)
+ (case-fold-search t)
+ found)
+ (save-excursion
+ (save-restriction
+ (widen)
+ (if forward
+ (while (and (/= n 0) (< i pmail-total-messages))
+ (let (done)
+ (while (and (not done)
+ (< i pmail-total-messages))
+ (setq i (+ i 1))
+ (pmail-narrow-to-header i)
+ (goto-char (point-min))
+ (setq done (re-search-forward search-regexp (point-max) t)))
+ (if done (setq found i)))
+ (setq n (1- n)))
+ (while (and (/= n 0) (> i 1))
+ (let (done)
+ (while (and (not done) (> i 1))
+ (setq i (- i 1))
+ (pmail-narrow-to-header i)
+ (goto-char (point-min))
+ (setq done (re-search-forward search-regexp (point-max) t)))
+ (if done (setq found i)))
+ (setq n (1+ n))))))
+ (if found
+ (pmail-show-message found)
+ (error "No %s message with same subject"
+ (if forward "following" "previous")))))
+
+(defun pmail-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")
+ (pmail-next-same-subject (- n)))
+\f
+;;;; *** Pmail Message Deletion Commands ***
+
+(defun pmail-delete-message ()
+ "Delete this message and stay on it."
+ (interactive)
+ (pmail-desc-set-attribute pmail-desc-deleted-index t pmail-current-message)
+ (run-hooks 'pmail-delete-message-hook)
+ (pmail-show-message pmail-current-message))
+
+(defun pmail-undelete-previous-message ()
+ "Back up to deleted message, select it, and undelete it."
+ (interactive)
+ (set-buffer pmail-buffer)
+ (let ((msg pmail-current-message))
+ (while (and (> msg 0)
+ (not (pmail-desc-attr-p pmail-desc-deleted-index msg)))
+ (setq msg (1- msg)))
+ (if (= msg 0)
+ (error "No previous deleted message")
+ (pmail-desc-set-attribute pmail-desc-deleted-index nil msg)
+ (pmail-show-message msg)
+ (if (pmail-summary-exists)
+ (save-excursion
+ (set-buffer pmail-summary-buffer)
+ (pmail-summary-mark-undeleted msg)))
+ (pmail-maybe-display-summary))))
+
+;;; mbox: ready
+(defun pmail-delete-forward (&optional backward)
+ "Delete this message and move to next nondeleted one.
+Deleted messages stay in the file until the \\[pmail-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")
+ (pmail-desc-set-attribute pmail-desc-deleted-index t pmail-current-message)
+ (run-hooks 'pmail-delete-message-hook)
+ (let ((del-msg pmail-current-message))
+ (if (pmail-summary-exists)
+ (pmail-select-summary
+ (pmail-summary-mark-deleted del-msg)))
+ (prog1 (pmail-next-undeleted-message (if backward -1 1))
+ (pmail-maybe-display-summary))))
+
+;;; mbox: ready
+(defun pmail-delete-backward ()
+ "Delete this message and move to previous nondeleted one.
+Deleted messages stay in the file until the \\[pmail-expunge] command is given."
+ (interactive)
+ (pmail-delete-forward t))
+
+(defun pmail-expunge-confirmed ()
+ "Return t if deleted message should be expunged. If necessary, ask the user.
+See also user-option `pmail-confirm-expunge'."
+ (set-buffer pmail-buffer)
+ (let ((some-deleted))
+ (dotimes (i pmail-total-messages)
+ (if (pmail-desc-deleted-p (1+ i))
+ (setq some-deleted t)))
+ (or (not some-deleted)
+ (null pmail-confirm-expunge)
+ (funcall pmail-confirm-expunge
+ "Erase deleted messages from Pmail file? "))))
+
+(defun pmail-only-expunge (&optional dont-show)
+ "Actually erase all deleted messages in the file."
+ (interactive)
+ (message "Expunging deleted messages...")
+ ;; Discard all undo records for this buffer.
+ (or (eq buffer-undo-list t) (setq buffer-undo-list nil))
+ ;; Remove the messages from the buffer and from the Pmail message
+ ;; descriptor vector.
+ (pmail-desc-prune-deleted-messages 'pmail-expunge-callback)
+ ;; Update the Pmail message counter, deal with the summary buffer,
+ ;; show the current message and update the User status.
+ (setq pmail-total-messages (pmail-desc-get-count))
+ (pmail-show-message pmail-current-message t)
+ (let* ((omax (- (buffer-size) (point-max)))
+ (omin (- (buffer-size) (point-min)))
+ (opoint (if (and (> pmail-current-message 0)
+ (pmail-message-deleted-p pmail-current-message))
+ 0
+ (if pmail-enable-mime
+ (with-current-buffer pmail-view-buffer
+ (- (point)(point-min)))
+ (- (point) (point-min))))))
+ (when pmail-summary-buffer
+ (with-current-buffer pmail-summary-buffer
+ (pmail-update-summary)))
+ (message "Expunging deleted messages...done")
+ (if (not dont-show)
+ (pmail-show-message
+ (if (zerop pmail-current-message) 1 nil)))
+ (if pmail-enable-mime
+ (goto-char (+ (point-min) opoint))
+ (goto-char (+ (point) opoint)))))
+
+;;; mbox: ready
+(defun pmail-expunge-callback (n)
+ "Called after message N has been pruned to update the current Pmail
+ message counter."
+ (if (< n pmail-current-message)
+ (setq pmail-current-message (1- pmail-current-message))))
+
+;;; mbox: ready
+(defun pmail-expunge ()
+ "Erase deleted messages from Pmail file and summary buffer."
+ (interactive)
+ (when (pmail-expunge-confirmed)
+ (pmail-only-expunge)))
+\f
+;;;; *** Pmail Mailing Commands ***
+
+;;; mbox: In progress. I'm still not happy with the initial citation
+;;; stuff. -pmr
+(defun pmail-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 pmail-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 pmail-mail ()
+ "Send mail in another window.
+While composing the message, use \\[mail-yank-original] to yank the
+original message into it."
+ (interactive)
+ (pmail-start-mail nil nil nil nil nil pmail-view-buffer))
+
+(defun pmail-continue ()
+ "Continue composing outgoing message previously being composed."
+ (interactive)
+ (pmail-start-mail t))
+
+(defun pmail-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")
+ (if (= pmail-total-messages 0)
+ (error "No messages in this file"))
+ (save-excursion
+ (save-restriction
+ (let* ((msgnum pmail-current-message)
+ (from (pmail-header-get-header "from"))
+ (reply-to (or (pmail-header-get-header "reply-to" nil t) from))
+ (cc (unless just-sender
+ (pmail-header-get-header "cc" nil t)))
+ (subject (pmail-header-get-header "subject"))
+ (date (pmail-header-get-header "date"))
+ (to (or (pmail-header-get-header "to" nil t) ""))
+ (message-id (pmail-header-get-header "message-id"))
+ (references (pmail-header-get-header "references" nil nil t))
+ (resent-to (pmail-header-get-header "resent-reply-to" nil t))
+ (resent-cc (unless just-sender
+ (pmail-header-get-header "resent-cc" nil t)))
+ (resent-reply-to (or (pmail-header-get-header "resent-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 pmail-reply-prefix
+ (if (let ((case-fold-search t))
+ (string-match pmail-reply-regexp subject))
+ (substring subject (match-end 0))
+ subject))))
+ ;; Now setup the mail reply buffer.
+ (pmail-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 (pmail-dont-reply-to reply-to)))
+ (if (string= reply-to-list "") reply-to reply-to-list))
+ subject
+ (pmail-make-in-reply-to-field from date message-id)
+ (if just-sender
+ nil
+ ;; mail-strip-quoted-names is NOT necessary for
+ ;; pmail-dont-reply-to to do its job.
+ (let* ((cc-list (pmail-dont-reply-to
+ (mail-strip-quoted-names
+ (if (null cc) to (concat to ", " cc))))))
+ (if (string= cc-list "") nil cc-list)))
+ pmail-view-buffer
+ (list (list 'pmail-reply-callback pmail-buffer "answered" t msgnum))
+ nil
+ (list (cons "References" (concat (mapconcat 'identity references " ")
+ " " message-id))))))))
+
+(defun pmail-reply-callback (buffer attr state n)
+ "Mail reply callback function.
+Sets ATTR (a string) if STATE is
+non-nil, otherwise clears it. N is the message number.
+BUFFER, possibly narrowed, contains an mbox mail message."
+ (save-excursion
+ (set-buffer buffer)
+ (pmail-set-attribute attr state n)))
+
+(defun pmail-mark-message (msgnum-list attr-index)
+ "Set attribute ATTRIBUTE-INDEX in the message of the car of MSGNUM-LIST.
+This is used in the send-actions for
+message buffers. MSGNUM-LIST is a list of the form (MSGNUM)."
+ (save-excursion
+ (let ((n (car msgnum-list)))
+ (set-buffer pmail-buffer)
+ (pmail-narrow-to-message n)
+ (pmail-desc-set-attribute attr-index t n))))
+
+(defun pmail-narrow-to-message (n)
+ "Narrow the current (pmail) buffer to bracket message N."
+ (widen)
+ (narrow-to-region (pmail-desc-get-start n) (pmail-desc-get-end n)))
+
+(defun pmail-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))
+ (pmail-make-in-reply-to-field from date message-id)))))
+\f
+;;; mbox: ready
+(defun pmail-forward (resend)
+ "Forward the current message to another user.
+With prefix argument, \"resend\" the message instead of forwarding it;
+see the documentation of `pmail-resend'."
+ (interactive "P")
+ (if (= pmail-total-messages 0)
+ (error "No messages in this file"))
+ (if resend
+ (call-interactively 'pmail-resend)
+ (let ((forward-buffer pmail-buffer)
+ (msgnum pmail-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 (pmail-start-mail
+ nil nil subject nil nil nil
+ (list (list 'pmail-mark-message
+ forward-buffer
+ (with-current-buffer pmail-buffer
+ (pmail-desc-get-start msgnum))
+ "forwarded"))
+ ;; If only one window, use it for the mail buffer.
+ ;; Otherwise, use another window for the mail buffer
+ ;; so that the Pmail buffer remains visible
+ ;; and sending the mail will get back to it.
+ (and (not pmail-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 pmail-enable-mime pmail-enable-mime-composing)
+ (funcall pmail-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))))))
+\f
+(defun pmail-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: ")
+ (if (= pmail-total-messages 0)
+ (error "No messages in this file"))
+ (require 'sendmail)
+ (require 'mailalias)
+ (unless (or (eq pmail-view-buffer (current-buffer))
+ (eq pmail-buffer (current-buffer)))
+ (error "Not an Pmail 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 pmail-buffer))
+ (unwind-protect
+ (with-current-buffer tembuf
+ ;;>> Copy message into temp buffer
+ (if pmail-enable-mime
+ (funcall pmail-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 pmail-buffer
+ (pmail-set-attribute "resent" t pmail-current-message))))
+\f
+(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 pmail-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 `pmail-retry-ignored-headers' is a regular expression
+specifying headers which should not be copied into the new message."
+ (interactive)
+ (if (= pmail-total-messages 0)
+ (error "No messages in this file"))
+ (require 'mail-utils)
+ (let ((pmail-this-buffer (current-buffer))
+ (msgnum pmail-current-message)
+ bounce-start bounce-end bounce-indent resending
+ (content-type
+ (save-excursion
+ (save-restriction
+ (pmail-header-get-header "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.
+ ;;
+ ;; NOTE: the use of pmail-msgref-vector is a red flag. I'm not
+ ;; sure (yet) what the right thing to do here is but I strongly
+ ;; suspect it needs something along the lines of:
+ ;; ...(pmail-desc-set-attribute pmail-desc-resent-index nil n)...
+ ;; The test to run to see the breakage and figure out what needs
+ ;; to be done is to cause a "resend" to happen and verify that it
+ ;; is either broken or works properly. For now the unbound
+ ;; variable is being left intact. -pmr 8/12/2008
+ (let (mail-signature mail-setup-hook)
+ (if (pmail-start-mail nil nil nil nil nil pmail-this-buffer
+ (list (list 'pmail-mark-message
+ pmail-this-buffer
+ (aref pmail-msgref-vector msgnum)
+ "retried")))
+ ;; 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)
+ pmail-displayed-headers
+ pmail-ignored-headers)
+ (erase-buffer)
+ (insert-buffer-substring pmail-this-buffer
+ bounce-start bounce-end)
+ (goto-char (point-min))
+ (if bounce-indent
+ (indent-rigidly (point-min) (point-max) bounce-indent))
+ (pmail-clear-headers pmail-retry-ignored-headers)
+ (pmail-clear-headers "^sender:\\|^return-path:\\|^received:")
+ (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))))))
+\f
+(defun pmail-summary-exists ()
+ "Non-nil if in an PMAIL buffer and an associated summary buffer exists.
+In fact, the non-nil value returned is the summary buffer itself."
+ (and pmail-summary-buffer (buffer-name pmail-summary-buffer)
+ pmail-summary-buffer))
+
+(defun pmail-summary-displayed ()
+ "t if in PMAIL buffer and an associated summary buffer is displayed."
+ (and pmail-summary-buffer (get-buffer-window pmail-summary-buffer)))
+
+(defcustom pmail-redisplay-summary nil
+ "*Non-nil means Pmail should show the summary when it changes.
+This has an effect only if a summary buffer exists."
+ :type 'boolean
+ :group 'pmail-summary)
+
+(defcustom pmail-summary-window-size nil
+ "*Non-nil means specify the height for an Pmail summary window."
+ :type '(choice (const :tag "Disabled" nil) integer)
+ :group 'pmail-summary)
+
+;; Put the summary buffer back on the screen, if user wants that.
+(defun pmail-maybe-display-summary ()
+ (let ((selected (selected-window))
+ window)
+ ;; If requested, make sure the summary is displayed.
+ (and pmail-summary-buffer (buffer-name pmail-summary-buffer)
+ pmail-redisplay-summary
+ (if (get-buffer-window pmail-summary-buffer 0)
+ ;; It's already in some frame; show that one.
+ (let ((frame (window-frame
+ (get-buffer-window pmail-summary-buffer 0))))
+ (make-frame-visible frame)
+ (raise-frame frame))
+ (display-buffer pmail-summary-buffer)))
+ ;; If requested, set the height of the summary window.
+ (and pmail-summary-buffer (buffer-name pmail-summary-buffer)
+ pmail-summary-window-size
+ (setq window (get-buffer-window pmail-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 (- pmail-summary-window-size (window-height))))
+ (select-window selected)))))
+\f
+;;;; *** Pmail Local Fontification ***
+
+(defun pmail-fontify-buffer-function ()
+ ;; This function's symbol is bound to font-lock-fontify-buffer-function.
+ (add-hook 'pmail-show-message-hook 'pmail-fontify-message nil t)
+ ;; If we're already showing a message, fontify it now.
+ (if pmail-current-message (pmail-fontify-message))
+ ;; Prevent Font Lock mode from kicking in.
+ (setq font-lock-fontified t))
+
+(defun pmail-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 'pmail-show-message-hook 'pmail-fontify-message t)
+ (remove-text-properties (point-min) (point-max) '(pmail-fontified nil))
+ (font-lock-default-unfontify-buffer)
+ (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))
+
+(defun pmail-fontify-message ()
+ "Fontify the current message if it is not already fontified."
+ (when (text-property-any (point-min) (point-max) 'pmail-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) '(pmail-fontified t))
+ (font-lock-fontify-region (point-min) (point-max))
+ (and (not modified) (buffer-modified-p)
+ (set-buffer-modified-p nil)))))))
+\f
+;;; Speedbar support for PMAIL files.
+(eval-when-compile (require 'speedbar))
+
+(defvar pmail-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 pmail-speedbar-last-user nil
+ "The last user to be displayed in the speedbar.")
+
+(defvar pmail-speedbar-key-map nil
+ "Keymap used when in pmail display mode.")
+
+(defun pmail-install-speedbar-variables ()
+ "Install those variables used by speedbar to enhance pmail."
+ (if pmail-speedbar-key-map
+ nil
+ (setq pmail-speedbar-key-map (speedbar-make-specialized-keymap))
+
+ (define-key pmail-speedbar-key-map "e" 'speedbar-edit-line)
+ (define-key pmail-speedbar-key-map "r" 'speedbar-edit-line)
+ (define-key pmail-speedbar-key-map "\C-m" 'speedbar-edit-line)
+ (define-key pmail-speedbar-key-map "M"
+ 'pmail-speedbar-move-message-to-folder-on-line)))
+
+(defvar pmail-speedbar-menu-items
+ '(["Read Folder" speedbar-edit-line t]
+ ["Move message to folder" pmail-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)
+ (pmail-install-speedbar-variables)
+ (add-hook 'speedbar-load-hook 'pmail-install-speedbar-variables))
+
+(defun pmail-speedbar-buttons (buffer)
+ "Create buttons for BUFFER containing pmail 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 PMAIL 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 pmail-speedbar-last-user))
+ nil
+ (setq pmail-speedbar-last-user from)
+ (erase-buffer)
+ (insert "Reply To:\n")
+ (if (stringp from)
+ (speedbar-insert-button from 'speedbar-directory-face 'highlight
+ 'pmail-speedbar-button 'pmail-reply))
+ (insert "Folders:\n")
+ (let* ((case-fold-search nil)
+ (df (directory-files (save-excursion (set-buffer buffer)
+ default-directory)
+ nil pmail-speedbar-match-folder-regexp)))
+ (while df
+ (speedbar-insert-button "<M>" 'speedbar-button-face 'highlight
+ 'pmail-speedbar-move-message (car df))
+ (speedbar-insert-button (car df) 'speedbar-file-face 'highlight
+ 'pmail-speedbar-find-file nil t)
+ (setq df (cdr df)))))))
+
+(defun pmail-speedbar-button (text token indent)
+ "Execute an pmail command specified by TEXT.
+The command used is TOKEN. INDENT is not used."
+ (speedbar-with-attached-buffer
+ (funcall token t)))
+
+(defun pmail-speedbar-find-file (text token indent)
+ "Load in the pmail file TEXT.
+TOKEN and INDENT are not used."
+ (speedbar-with-attached-buffer
+ (message "Loading in PMAIL file %s..." text)
+ (find-file text)))
+
+(defun pmail-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 pmail-speedbar-move-message (text token indent)
+ "From button TEXT, copy current message to the pmail file specified by TOKEN.
+TEXT and INDENT are not used."
+ (speedbar-with-attached-buffer
+ (message "Moving message to %s" token)
+ (pmail-output-to-pmail-file 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 pmail-set-remote-password (password)
+ "Set PASSWORD to be used for retrieving mail from a POP or IMAP server."
+ (interactive "sPassword: ")
+ (if password
+ (setq pmail-encoded-remote-password
+ (pmail-encode-string password (emacs-pid)))
+ (setq pmail-remote-password nil)
+ (setq pmail-encoded-remote-password nil)))
+
+(defun pmail-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 pmail-encoded-remote-password)
+ (if (not pmail-remote-password)
+ (setq pmail-remote-password
+ (read-passwd (if imap
+ "IMAP password: "
+ "POP password: "))))
+ (pmail-set-remote-password pmail-remote-password)
+ (setq pmail-remote-password nil))
+ (pmail-encode-string pmail-encoded-remote-password (emacs-pid)))
+
+(defun pmail-have-password ()
+ (or pmail-remote-password pmail-encoded-remote-password))
+
+(defun pmail-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 pmail-restore-desktop-buffer (desktop-buffer-file-name
+ desktop-buffer-name
+ desktop-buffer-misc)
+ "Restore an pmail buffer specified in a desktop file."
+ (condition-case error
+ (progn
+ (pmail-input desktop-buffer-file-name)
+ (if (eq major-mode 'pmail-mode)
+ (current-buffer)
+ pmail-buffer))
+ (file-locked
+ (kill-buffer (current-buffer))
+ nil)))
+
+(add-to-list 'desktop-buffer-mode-handlers
+ '(pmail-mode . pmail-restore-desktop-buffer))
+
+(provide 'pmail)
+
+;; arch-tag: cff0a950-57fe-4f73-a86e-91ff75afd06c
+;;; pmail.el ends here
--- /dev/null
+;;; pmaildesc.el --- Low level message descriptor library for Pmail.
+
+;; Copyright (C) 2002, 2006 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 2, or (at your option)
+;; any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs; see the file COPYING. If not, write to the
+;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
+
+;;; Commentary:
+
+;;; This package provides low level functions for tracking messages in Pmail.
+
+;;; Code:
+
+(require 'pmailhdr)
+
+(defvar pmail-desc-attributes nil
+ "A private variable providing temporary access to message attributes.")
+
+(defvar pmail-desc-delete-callback nil
+ "A function pointer called after a message has been deleted.
+It expects one argument --- the message number.")
+
+(defvar pmail-desc-vector nil
+ "A vector of message descriptors.
+A message descriptor contains data formatted as follows:
+
+ (START ATTRIBUTES KEYWORDS DATE LINE-COUNT SENDER SUBJECT)
+
+where
+
+ START is a marker at the beginning of the header
+
+ ATTRIBUTES is a string where each character encodes an
+ attribute. A hyphen (-) indicates that the attribute is not
+ set:
+
+ ANSWERED The message has been replied to (A).
+ DELETED The message has been marked for deletion (D).
+ EDITED The message has been edited (E).
+ FILED The message has been filed (F).
+ RESENT The message has been resent (R).
+ STORED The message has been saved to a file (S).
+ UNSEEN The message has not been read (-).
+
+ KEYWORDS is a list of User defined label strings.
+
+ DATE is a list of strings describing the message date:
+
+ DAY-OF-WEEK Mon, Sun, etc.
+ DAY-NUMBER 9, 13, 15, etc.
+ MONTH Feb, Jun, etc.
+ YEAR 2001, 2002, etc.
+ TIME 12:03:25, etc.
+
+ LINE-COUNT is the number of lines in the message.
+
+ SENDER is the name of the User sending the message.
+
+ SUBJECT is the subject header, cached to support fast summary line generation.
+")
+(put 'pmail-desc-vector 'permanent-local t)
+
+;;;; Constants supporting message vector processing.
+
+(defconst pmail-desc-default-attrs "------U"
+ "The default attributes for a new message.")
+
+;;; Message component indexes.
+
+(defconst pmail-desc-beg-index 0
+ "The message descriptor element index for the start of the message text.")
+
+(defconst pmail-desc-attrs-index 1
+ "The message descriptor element index for the attributes string.")
+
+(defconst pmail-desc-keywords-index 2
+ "The message descriptor element index for the User defined labels.")
+
+(defconst pmail-desc-date-index 3
+ "The message descriptor element index for the message date information.")
+
+(defconst pmail-desc-line-count-index 4
+ "The message descriptor element index for the message line count.")
+
+(defconst pmail-desc-sender-index 5
+ "The message descriptor element index for the message line count.")
+
+(defconst pmail-desc-subject-index 6
+ "The message descriptor element index for the message line count.")
+
+;;; Attribute indexes
+
+(defconst pmail-desc-answered-index 0
+ "The index for the `answered' attribute.")
+
+(defconst pmail-desc-deleted-index 1
+ "The index for the `deleted' attribute.")
+
+(defconst pmail-desc-edited-index 2
+ "The index for the `edited' attribute.")
+
+(defconst pmail-desc-filed-index 3
+ "The index for the `filed' attribute.")
+
+(defconst pmail-desc-resent-index 4
+ "The index for the `resent' attribute.")
+
+(defconst pmail-desc-stored-index 5
+ "The index for the `stored' attribute.")
+
+(defconst pmail-desc-unseen-index 6
+ "The index for the `unseen' attribute.")
+
+(defconst pmail-desc-attr-code-index 0
+ "The index for the attibute code.")
+
+(defconst pmail-desc-attr-keyword-index 1
+ "The index for the attribute keyword.")
+
+(defconst pmail-desc-attr-summary-offset-index 2
+ "The index for the attribute offset in a summary buffer.")
+
+(defconst pmail-desc-attr-alist
+ (list (cons pmail-desc-answered-index (list ?A "answered" 1))
+ (cons pmail-desc-deleted-index (list ?D "deleted" 0))
+ (cons pmail-desc-edited-index (list ?E "edited" 3))
+ (cons pmail-desc-filed-index (list ?F "filed" 2))
+ (cons pmail-desc-resent-index (list ?R "resent" nil))
+ (cons pmail-desc-stored-index (list ?S "stored" 4))
+ (cons pmail-desc-unseen-index (list ? "unseen" 0)))
+ "An alist mapping an attribute to a keycode, keyword and summary offset.")
+
+(defconst pmail-desc-attr-index-map
+ (list (cons "answered" pmail-desc-answered-index)
+ (cons "deleted" pmail-desc-deleted-index)
+ (cons "edited" pmail-desc-edited-index)
+ (cons "filed" pmail-desc-filed-index)
+ (cons "resent" pmail-desc-resent-index)
+ (cons "stored" pmail-desc-stored-index)
+ (cons "unseen" pmail-desc-unseen-index)))
+
+;;; Date indexes
+
+(defconst pmail-desc-date-day-of-week-index 0
+ "The DAY-OF-WEEK index into the list of date information.")
+
+(defconst pmail-desc-date-day-number-index 1
+ "The DAY-NUMBER index into the list of date information.")
+
+(defconst pmail-desc-date-month-index 2
+ "The MONTH index into the list of date information.")
+
+(defconst pmail-desc-date-year-index 3
+ "The YEAR index into the list of date information.")
+
+(defconst pmail-desc-date-time-index 4
+ "The TIME index into the list of date information.")
+
+(defsubst pmail-desc-get-descriptor (n)
+ "Return a descriptor for message N.
+N is 1 based, i.e. the first message number is 1."
+ (aref pmail-desc-vector (1- n)))
+
+(defsubst pmail-desc-get-start (n)
+ "Return the position of the start of message N."
+ (marker-position
+ (nth pmail-desc-beg-index (pmail-desc-get-descriptor n))))
+
+(defun pmail-desc-get-end (n)
+ "Return the position of the end of message N."
+ (if (= n (length pmail-desc-vector))
+ (save-restriction
+ (widen)
+ (point-max))
+ (pmail-desc-get-start (1+ n))))
+
+(defun pmail-desc-add-descriptors (descriptor-list)
+ "Append DESCRIPTOR-LIST to the Pmail message descriptor vector."
+ (setq pmail-desc-vector
+ (vconcat pmail-desc-vector descriptor-list)))
+
+(defun pmail-desc-add-keyword (keyword n)
+ "Add KEYWORD to the list of keywords for message N.
+The current buffer must be narrowed to message N. Both
+`pmail-desc-vector' and the message headers are updated."
+ (save-excursion
+ (save-restriction
+ (let ((keywords (pmail-desc-get-keywords n))
+ (display-state (pmail-desc-get-header-display-state n)))
+ (unless (member keyword keywords)
+ (setq keywords (cons keyword keywords))
+ (setcar (nthcdr pmail-desc-keywords-index (pmail-desc-get-descriptor n))
+ keywords)
+ (pmail-header-show-headers)
+ (pmail-header-add-header pmail-header-keyword-header
+ (mapconcat 'identity keywords ","))
+ (pmail-header-toggle-visibility display-state))))))
+
+(defun pmail-desc-remove-keyword (keyword n)
+ "Remove KEYWORD from the list of keywords for message N.
+The current buffer must be narrowed to message N. Both
+`pmail-desc-vector' and the message headers are updated."
+ (save-excursion
+ (save-restriction
+ (let ((keywords (pmail-desc-get-keywords n))
+ (display-state (pmail-desc-get-header-display-state n)))
+ (when (member keyword keywords)
+ (setq keywords (delete keyword keywords))
+ (setcar (nthcdr pmail-desc-keywords-index (pmail-desc-get-descriptor n))
+ keywords)
+ (pmail-header-show-headers)
+ (pmail-header-add-header pmail-header-keyword-header
+ (mapconcat 'identity keywords ","))
+ (pmail-header-toggle-visibility display-state))))))
+
+(defun pmail-desc-attr-p (attr-index n)
+ "Return the state of the the attribute denoted by ATTR-INDEX in
+ message N."
+ (let ((attrs (nth pmail-desc-attrs-index
+ (pmail-desc-get-descriptor n))))
+ (not (equal "-" (substring attrs attr-index (1+ attr-index))))))
+
+(defun pmail-desc-clear-descriptors ()
+ "Clear the Pmail message vector of all messages."
+ (setq pmail-desc-vector nil))
+
+(defun pmail-desc-deleted-p (n)
+ "Return non-nil if message N is marked for deletion."
+ (pmail-desc-attr-p pmail-desc-deleted-index n))
+(defalias 'pmail-message-deleted-p 'pmail-desc-deleted-p)
+
+(defun pmail-desc-delete-maybe (n)
+ "Determine if message N is marked for deletion. If so then delete it.
+Return t if the message is deleted, nil if not."
+ (if (pmail-desc-deleted-p n)
+ (progn
+ (pmail-desc-delete n)
+ t)))
+
+(defun pmail-desc-delete (n)
+ "Remove message N from the Pmail buffer and from the descriptor vector."
+ (save-excursion
+ (save-restriction
+ ;; Enable the buffer to be written, ignore intangibility and do
+ ;; not record these changes in the undo list.
+ (let ((inhibit-read-only t)
+ (inhibit-point-motion-hooks t)
+ (buffer-undo-list t)
+ start end)
+ (widen)
+
+ ;; Remove the message from the buffer and neutralize the
+ ;; marker pointing to the start of the message.
+ (delete-region (pmail-desc-get-start n) (pmail-desc-get-end n))
+ (move-marker (nth pmail-desc-beg-index (pmail-desc-get-descriptor n)) nil)
+
+ ;; Remove the message descriptor from the Pmail message vector
+ ;; and execute the callback indicating the message has been
+ ;; deleted.
+ (aset pmail-desc-vector (1- n) t)
+ (funcall pmail-desc-delete-callback n)))))
+
+(defun pmail-desc-get-attr-code (attr-index n)
+ "Return the attribute code for ATTR-INDEX in message N.
+If the attribute is not set, return nil."
+ (if (pmail-desc-attr-p attr-index n)
+ (string (nth pmail-desc-attr-code-index
+ (cdr (assoc attr-index pmail-desc-attr-alist))))))
+
+(defun pmail-desc-get-attr-index (attr)
+ "Return the attribute index associated with attribute ATTR, a string."
+ (cdr (assoc attr pmail-desc-attr-index-map)))
+
+(defun pmail-desc-get-attributes (n)
+ "Return the attribute vector for message N."
+ (nth pmail-desc-attrs-index (pmail-desc-get-descriptor n)))
+
+(defsubst pmail-desc-get-count ()
+ "Return the number of messages described in the Pmail descriptor vector."
+ (length pmail-desc-vector))
+
+(defun pmail-desc-get-date (n)
+ "Return the date list generated when the messages were read in."
+ (nth pmail-desc-date-index (pmail-desc-get-descriptor n)))
+
+(defun pmail-desc-get-day-number (n)
+ "Return the day number (1..31) from the date associated with message N."
+ (nth pmail-desc-date-day-number-index
+ (nth pmail-desc-date-index (pmail-desc-get-descriptor n))))
+
+(defun pmail-desc-get-day-of-week (n)
+ "Return the day of week (Sun .. Sat) from the date associated with message N."
+ (nth pmail-desc-date-day-of-week-index
+ (nth pmail-desc-date-index (pmail-desc-get-descriptor n))))
+
+(defun pmail-desc-get-header-display-state (n)
+ "Return t if ignorable headers are being displayed, nil otherwise."
+ (save-excursion
+ (save-restriction
+ (pmail-narrow-to-header n)
+ (null (overlays-in (point-min) (point-max))))))
+
+(defun pmail-desc-get-keyword (attr-index)
+ "Return the keyword string associated with ATTR-INDEX."
+ (nth pmail-desc-attr-keyword-index
+ (cdr (assoc attr-index pmail-desc-attr-alist))))
+
+(defun pmail-desc-get-keyword-list (n)
+ "Return the list of user-defined labels for message N."
+ (nth pmail-desc-keywords-index (pmail-desc-get-descriptor n)))
+
+(defun pmail-desc-get-keyword-maybe (attribute)
+ "Return the keyword associated with ATTRIBUTE if it is set, nil otherwise.
+ATTRIBUTE is a cons cell associating an attribute index with a keyword string."
+ (let ((index (car attribute)))
+ (if (not (equal "-" (substring pmail-desc-attributes index (1+ index))))
+ (nth pmail-desc-attr-keyword-index (cdr attribute)))))
+
+(defun pmail-desc-get-keywords (n)
+ "Return a list of keywords for message N.
+This includes the attributes."
+ (setq pmail-desc-attributes (pmail-desc-get-attributes n))
+ (append (delq nil (mapcar
+ 'pmail-desc-get-keyword-maybe
+ pmail-desc-attr-alist))
+ (pmail-desc-get-keyword-list n)))
+
+(defun pmail-desc-get-line-count (n)
+ "Return the message body line count."
+ (nth pmail-desc-line-count-index (pmail-desc-get-descriptor n)))
+
+(defun pmail-desc-get-month (n)
+ "Return the month (Jan .. Dec) from the date associated with message N."
+ (nth pmail-desc-date-month-index
+ (nth pmail-desc-date-index (pmail-desc-get-descriptor n))))
+
+(defun pmail-desc-get-sender (n)
+ "Return the User registered as the mail sender."
+ (nth pmail-desc-sender-index (pmail-desc-get-descriptor n)))
+
+(defun pmail-desc-get-subject (n)
+ "Return the cached subject header."
+ (nth pmail-desc-subject-index (pmail-desc-get-descriptor n)))
+
+(defun pmail-desc-get-summary-offset (attr-index)
+ "Return the summary buffer offset associated with ATTR-INDEX.
+This is the relative position where the attribute code letter is
+displayed in the Pmail summary buffer."
+ (nth pmail-desc-attr-summary-offset-index
+ (cdr (assoc attr-index pmail-desc-attr-alist))))
+
+(defun pmail-desc-get-time (n)
+ "Return the time (hh:mm:ss) from the date associated with message N."
+ (nth pmail-desc-date-time-index
+ (nth pmail-desc-date-index (pmail-desc-get-descriptor n))))
+
+(defun pmail-desc-get-year (n)
+ "Return the year (1969 ... 2###) from the date associated with message N."
+ (nth pmail-desc-date-year-index
+ (nth pmail-desc-date-index (pmail-desc-get-descriptor n))))
+
+;; This is a strange thing to use.
+;; Why not write a simple loop instead?
+(defun pmail-desc-make-index-list ()
+ "Return a list of integers from 1 to the total number of messages."
+ (let ((result (make-vector (length pmail-desc-vector) nil))
+ (index 0))
+ (while (< index (length result))
+ (aset result index (1+ index))
+ (setq index (1+ index)))
+ (append result nil)))
+
+(defun pmail-desc-prune-deleted-messages (callback)
+ "Remove all messages marked for marked for deletion.
+Return the number of messages removed. Invoke CALLBACK immediately
+after a message has been deleted.."
+
+ ;; Set the callback.
+ (setq pmail-desc-delete-callback callback)
+
+ ;; Remove all messages marked for deletion from the Pmail buffer and
+ ;; their descriptors from the Pmail message vector.
+ (let ((result (length (delq t (mapcar 'pmail-desc-delete-maybe
+ (pmail-desc-make-index-list))))))
+ (setq pmail-desc-vector
+ (vconcat (delq t (append pmail-desc-vector nil))))
+ result))
+
+(defun pmail-desc-set-attribute (attr-index state n)
+ "Set the attribute denoted by ATTR-INDEX in message N according to STATE.
+If STATE is non-nil the attribute will be set to the single character code
+associated with ATTR-INDEX in pmail-desc-attr-alist, otherwise the attribute is
+set to the hyphen character (-)."
+ (let ((attributes (nth pmail-desc-attrs-index (pmail-desc-get-descriptor n)))
+ code)
+ (setq code (if state
+ (car (cdr (assoc attr-index pmail-desc-attr-alist)))
+ ?-))
+ (aset attributes attr-index code)
+ (pmail-header-add-header pmail-header-attribute-header attributes)))
+
+(defun pmail-desc-set-start (n pos)
+ "Set the start position for message N to POS."
+ (set-marker (nth pmail-desc-beg-index (pmail-desc-get-descriptor n)) pos))
+
+(defun pmail-desc-showing-message-p (n)
+ "Return t if the current buffer is displaying message N, nil otherwise."
+ (let ((beg (pmail-desc-get-start n))
+ (end (pmail-desc-get-end n))
+ (curpos (point)))
+ (and (>= curpos beg) (< curpos end))))
+
+(provide 'pmaildesc)
+
+;;; pmaildesc.el ends here
--- /dev/null
+;;; pmailedit.el --- "PMAIL edit mode" Edit the current message
+
+;; Copyright (C) 1985, 1994, 2001, 2002, 2003, 2004, 2005, 2006,
+;; 2007, 2008 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 'pmail)
+ (require 'pmaildesc)
+ (require 'pmailsum))
+
+(defcustom pmail-edit-mode-hook nil
+ "List of functions to call when editing an PMAIL message."
+ :type 'hook
+ :version "21.1"
+ :group 'pmail-edit)
+
+(defvar pmail-old-text)
+
+(defvar pmail-edit-map
+ (let ((map (make-sparse-keymap)))
+ (set-keymap-parent map text-mode-map)
+ (define-key map "\C-c\C-c" 'pmail-cease-edit)
+ (define-key map "\C-c\C-]" 'pmail-abort-edit)
+ map)
+ "Keymap for `pmail-edit-mode'.")
+
+;; Pmail Edit mode is suitable only for specially formatted data.
+(put 'pmail-edit-mode 'mode-class 'special)
+
+(declare-function pmail-summary-disable "" ())
+(declare-function pmail-summary-enable "pmailsum" ())
+
+(defun pmail-edit-mode ()
+ "Major mode for editing the contents of an PMAIL message.
+The editing commands are the same as in Text mode, together with two commands
+to return to regular PMAIL:
+ * \\[pmail-abort-edit] cancels the changes
+ you have made and returns to PMAIL
+ * \\[pmail-cease-edit] makes them permanent.
+This functions runs the normal hook `pmail-edit-mode-hook'.
+\\{pmail-edit-map}"
+ (delay-mode-hooks (text-mode))
+ (use-local-map pmail-edit-map)
+ (setq major-mode 'pmail-edit-mode)
+ (setq mode-name "PMAIL Edit")
+ (if (boundp 'mode-line-modified)
+ (setq mode-line-modified (default-value 'mode-line-modified))
+ (setq mode-line-format (default-value 'mode-line-format)))
+ (if (pmail-summary-exists)
+ (save-excursion
+ (set-buffer pmail-summary-buffer)
+ (pmail-summary-disable)))
+ (run-mode-hooks 'pmail-edit-mode-hook))
+
+(defvar pmail-old-pruned nil)
+(put 'pmail-old-pruned 'permanent-local t)
+
+(defvar pmail-edit-saved-coding-system nil)
+(put 'pmail-edit-saved-coding-system 'permanent-local t)
+
+;;;###autoload
+(defun pmail-edit-current-message ()
+ "Edit the contents of this message."
+ (interactive)
+ (if (= pmail-total-messages 0)
+ (error "No messages in this file"))
+ (make-local-variable 'pmail-old-pruned)
+ (setq pmail-old-pruned (pmail-msg-is-pruned))
+ (make-local-variable 'pmail-edit-saved-coding-system)
+ (setq pmail-edit-saved-coding-system save-buffer-coding-system)
+ (pmail-header-show-headers)
+ (pmail-edit-mode)
+ ;; As the local value of save-buffer-coding-system is deleted by
+ ;; pmail-edit-mode, we restore the original value.
+ (make-local-variable 'save-buffer-coding-system)
+ (setq save-buffer-coding-system pmail-edit-saved-coding-system)
+ (make-local-variable 'pmail-old-text)
+ (setq pmail-old-text (buffer-substring (point-min) (point-max)))
+ (setq buffer-read-only nil)
+ (force-mode-line-update)
+ (if (and (eq (key-binding "\C-c\C-c") 'pmail-cease-edit)
+ (eq (key-binding "\C-c\C-]") 'pmail-abort-edit))
+ (message "Editing: Type C-c C-c to return to Pmail, C-c C-] to abort")
+ (message "%s" (substitute-command-keys
+ "Editing: Type \\[pmail-cease-edit] to return to Pmail, \\[pmail-abort-edit] to abort"))))
+
+(defun pmail-cease-edit ()
+ "Finish editing message; switch back to Pmail proper."
+ (interactive)
+ (when (pmail-summary-exists)
+ (with-current-buffer pmail-summary-buffer
+ (pmail-summary-enable)))
+ ;; Make sure buffer ends with a newline.
+ (save-excursion
+ (goto-char (point-max))
+ (when (/= (preceding-char) ?\n)
+ (insert "\n"))
+ ;; Adjust the marker that points to the end of this message, unless
+ ;; we're at the last message.
+ (when (< pmail-current-message (length pmail-desc-vector))
+ (pmail-desc-set-start (1+ pmail-current-message) (point))))
+ (let ((old pmail-old-text))
+ (force-mode-line-update)
+ (kill-all-local-variables)
+ (pmail-mode-1)
+ (if (boundp 'tool-bar-map)
+ (set (make-local-variable 'tool-bar-map) pmail-tool-bar-map))
+ (pmail-variables)
+ ;; As the local value of save-buffer-coding-system is changed by
+ ;; pmail-variables, we restore the original value.
+ (setq save-buffer-coding-system pmail-edit-saved-coding-system)
+ (unless (and (= (length old) (- (point-max) (point-min)))
+ (string= old (buffer-substring (point-min) (point-max))))
+ (setq old nil)
+ (pmail-set-attribute "edited" t))
+ (save-excursion
+ (pmail-show-message)
+ ;; `pmail-show-message' always hides the headers, so we show them
+ ;; here if they were visible before starting the edit.
+ (when pmail-old-pruned
+ (pmail-header-show-headers))))
+ (run-hooks 'pmail-mode-hook)
+ (setq buffer-read-only t))
+
+(defun pmail-abort-edit ()
+ "Abort edit of current message; restore original contents."
+ (interactive)
+ (delete-region (point-min) (point-max))
+ (insert pmail-old-text)
+ (pmail-cease-edit))
+
+(provide 'pmailedit)
+
+;; arch-tag: 93c22709-a14a-46c1-ab91-52c3f5a0ec12
+;;; pmailedit.el ends here
--- /dev/null
+;;; pmail-header.el --- Header handling code of "PMAIL" mail reader for Emacs
+
+;; Copyright (C) 2002, 2006 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 2, or (at your option)
+;; any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs; see the file COPYING. If not, write to the
+;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
+
+;;; Commentary:
+
+;;; Code:
+
+(eval-when-compile
+ (require 'mail-utils))
+
+(defconst pmail-header-attribute-header "X-BABYL-V6-ATTRIBUTES"
+ "The header that stores the Pmail attribute data.")
+
+(defconst pmail-header-keyword-header "X-BABYL-V6-KEYWORDS"
+ "The header that stores the Pmail keyword data.")
+
+(defvar pmail-header-overlay-list nil
+ "List of cached overlays used to make headers hidden or visible.")
+
+(defvar pmail-header-display-state nil
+ "Records the current header display state.
+nil means headers are displayed, t indicates headers are not displayed.")
+
+(defun pmail-header-get-limit ()
+ "Return the end of the headers.
+The current buffer must show one message. If you want to narrow
+to the headers of a mail by number, use `pmail-narrow-to-header'
+instead."
+ (save-excursion
+ (goto-char (point-min))
+ (if (search-forward "\n\n" nil t)
+ (1- (point))
+ (error "Invalid message format"))))
+
+(defun pmail-header-add-header (header value)
+ "Add HEADER to the list of headers and associate VALUE with it.
+The current buffer, possibly narrowed, contains a single message.
+If VALUE is nil or the empty string, the header is removed
+instead."
+ (save-excursion
+ (let* ((inhibit-read-only t)
+ (case-fold-search t)
+ (inhibit-point-motion-hooks t)
+ (buffer-undo-list t)
+ (limit (pmail-header-get-limit))
+ start end)
+ ;; Search for the given header. If found, then set it's value.
+ ;; If not then add the header to the end of the header section.
+ (goto-char (point-min))
+ (if (re-search-forward (format "^%s: " header) limit t)
+ (let ((start (match-beginning 0)))
+ (re-search-forward "\n[^ \t]")
+ (goto-char limit)
+ (delete-region start (1+ (match-beginning 0))))
+ (goto-char limit))
+ (when (> (length value) 0)
+ (insert header ": " value "\n")))))
+
+(defun pmail-header-contains-keyword-p (keyword)
+ "Return t if KEYWORD exists in the current buffer, nil otherwise."
+ (let ((limit (pmail-header-get-limit)))
+ (goto-char (point-min))
+ (if (re-search-forward (format "^%s: " pmail-header-keyword-header) limit t)
+ ;; Some keywords exist. Now search for the specific keyword.
+ (let ((start (point))
+ (end (progn (end-of-line) (point))))
+ (if (re-search-forward (concat "\\(" keyword ",\\|" keyword "$\\)"))
+ t)))))
+
+(defun pmail-header-get-header (&rest args)
+ "Return the text value for a header or nil if no such header exists.
+The arguments ARGS are passed to `mail-fetch-field'. The first
+argument is the header to get.
+
+The current buffer, possibly narrowed, contains a single message.
+Note that it is not necessary to call `pmail-header-show-headers'
+because `inhibit-point-motion-hooks' is locally bound to t."
+ (save-excursion
+ (save-restriction
+ (let* ((inhibit-point-motion-hooks t)
+ (limit (pmail-header-get-limit)))
+ (narrow-to-region (point-min) limit)
+ (apply 'mail-fetch-field args)))))
+
+(defun pmail-header-get-keywords ()
+ "Return the keywords in the current message.
+The current buffer, possibly narrowed, contains a single message."
+ ;; Search for a keyword header and return the comma separated
+ ;; strings as a list.
+ (let ((limit (pmail-header-get-limit)) result)
+ (goto-char (point-min))
+ (if (re-search-forward
+ (format "^%s: " pmail-header-keyword-header) limit t)
+ (save-excursion
+ (save-restriction
+ (narrow-to-region (point) (line-end-position))
+ (goto-char (point-min))
+ (mail-parse-comma-list))))))
+
+(defun pmail-header-hide-headers ()
+ "Hide ignored headers. All others will be visible.
+The current buffer, possibly narrowed, contains a single message."
+ (save-excursion
+ (pmail-header-show-headers)
+ (let ((overlay-list pmail-header-overlay-list)
+ (limit (pmail-header-get-limit))
+ (inhibit-point-motion-hooks t)
+ (case-fold-search t)
+ visibility-p)
+ ;; Record the display state as having headers hidden.
+ (setq pmail-header-display-state t)
+ (if pmail-displayed-headers
+ ;; Set the visibility predicate function to ignore headers
+ ;; marked for display.
+ (setq visibility-p 'pmail-header-show-displayed-p)
+ ;; Set the visibility predicate function to hide ignored
+ ;; headers.
+ (setq visibility-p 'pmail-header-hide-ignored-p))
+ ;; Walk through all the headers marking the non-displayed
+ ;; headers as invisible.
+ (goto-char (point-min))
+ (while (re-search-forward "^[^ \t:]+[ :]" limit t)
+ ;; Determine if the current header needs to be hidden.
+ (forward-line 0)
+ (if (not (funcall visibility-p))
+ ;; It does not. Move point away from this header.
+ (progn
+ (forward-line 1)
+ (while (looking-at "[ \t]+")
+ (forward-line 1)))
+ ;; It does. Make this header hidden by setting an overlay
+ ;; with both the invisible and intangible properties set.
+ (let ((start (point)))
+ ;; Move to end and pick upp any continuation lines on folded
+ ;; headers.
+ (forward-line 1)
+ (while (looking-at "[ \t]+")
+ (forward-line 1))
+ (if (car overlay-list)
+ ;; Use one of the cleared, cached overlays.
+ (let ((overlay (car overlay-list)))
+ (move-overlay overlay start (point))
+ (setq overlay-list (cdr overlay-list)))
+ ;; No overlay exists for this header. Create one and
+ ;; add it to the cache.
+ (let ((overlay (make-overlay start (point))))
+ (overlay-put overlay 'invisible t)
+ (overlay-put overlay 'intangible t)
+ (push overlay pmail-header-overlay-list)))))))))
+
+(defun pmail-header-show-headers ()
+ "Show all headers.
+The current buffer, possibly narrowed, contains a single message."
+ ;; Remove all the overlays used to control hiding headers.
+ (mapcar 'delete-overlay pmail-header-overlay-list)
+ (setq pmail-header-display-state nil))
+
+(defun pmail-header-toggle-visibility (&optional arg)
+ "Toggle the visibility of the ignored headers if ARG is nil.
+Hide the ignored headers if ARG is greater than 0, otherwise show the
+ignored headers. The current buffer, possibly narrowed, contains a
+single message."
+ (cond ((eq arg nil)
+ (if pmail-header-display-state
+ (pmail-header-show-headers)
+ (pmail-header-hide-headers)))
+ ((or (eq arg t) (> arg 0))
+ (pmail-header-hide-headers))
+ (t (pmail-header-show-headers))))
+
+(defun pmail-header-hide-ignored-p ()
+ "Test that the header is one of the headers marked to be ignored."
+ (looking-at pmail-ignored-headers))
+
+(defun pmail-header-show-displayed-p ()
+ "Test that the header is not one of the headers marked for display."
+ (not (looking-at pmail-displayed-headers)))
+
+(provide 'pmailhdr)
+
+;;; pmailhdr.el ends here
--- /dev/null
+;;; pmailkwd.el --- part of the "PMAIL" mail reader for Emacs
+
+;; Copyright (C) 1985, 1988, 1994, 2001, 2002, 2003, 2004,
+;; 2005, 2006, 2007, 2008 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 2, or (at your option)
+;; any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs; see the file COPYING. If not, write to the
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
+
+;;; Commentary:
+
+;; This library manages keywords (labels). Labels are stored in the
+;; variable `pmail-keywords'.
+
+;;; Code:
+
+(defvar pmail-buffer)
+(defvar pmail-current-message)
+(defvar pmail-last-label)
+(defvar pmail-last-multi-labels)
+(defvar pmail-summary-vector)
+(defvar pmail-total-messages)
+
+;; Global to all PMAIL 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.
+
+(provide 'pmailkwd)
+
+(eval-when-compile
+ (require 'mail-utils))
+
+;; Named list of symbols representing valid message attributes in PMAIL.
+
+(defconst pmail-attributes
+ '(deleted answered filed forwarded unseen edited resent)
+ "Keywords with defined semantics used to label messages.
+These have a well-defined meaning to the PMAIL system.")
+
+(defconst pmail-deleted-label 'deleted)
+
+;; Named list of symbols representing valid message keywords in PMAIL.
+
+(defvar pmail-keywords nil
+ "Keywords used to label messages.
+These are all user-defined, unlike `pmail-attributes'.")
+\f
+;;;; Low-level functions.
+
+(defun pmail-attribute-p (s)
+ "Non-nil if S is a known attribute.
+See `pmail-attributes'."
+ (let ((symbol (pmail-make-label s)))
+ (memq symbol pmail-attributes)))
+
+(defun pmail-keyword-p (s)
+ "Non-nil if S is a known keyword for this Pmail file.
+See `pmail-keywords'."
+ (let ((symbol (pmail-make-label s)))
+ (memq symbol pmail-keywords)))
+
+(defun pmail-make-label (s &optional forcep)
+ (cond ((symbolp s) s)
+ (forcep (intern (downcase s)))
+ (t (intern-soft (downcase s)))))
+
+(defun pmail-quote-label-name (label)
+ (regexp-quote (symbol-name (pmail-make-label label t))))
+
+;;;###autoload
+(defun pmail-register-keywords (words)
+ "Add the strings in WORDS to `pmail-keywords'."
+ (dolist (word words)
+ (pmail-register-keyword word)))
+
+(defun pmail-register-keyword (word)
+ "Append the string WORD to `pmail-keywords',
+unless it already is a keyword or an attribute."
+ (let ((keyword (pmail-make-label word t)))
+ (unless (or (pmail-attribute-p keyword)
+ (pmail-keyword-p keyword))
+ (setq pmail-keywords (cons keyword pmail-keywords)))))
+\f
+;;;; Adding and removing message keywords.
+
+;;;###autoload
+(defun pmail-add-label (string)
+ "Add LABEL to labels associated with current PMAIL message."
+ (interactive (list (pmail-read-label "Add label")))
+ (pmail-set-label (pmail-make-label string) t)
+ (pmail-display-labels))
+
+;;;###autoload
+(defun pmail-kill-label (string)
+ "Remove LABEL from labels associated with current PMAIL message."
+ (interactive (list (pmail-read-label "Remove label" t)))
+ (pmail-set-label (pmail-make-label string) nil))
+
+;;;###autoload
+(defun pmail-read-label (prompt &optional existing)
+ "Ask for a label using PROMPT.
+If EXISTING is non-nil, ask for one of the labels of the current
+message."
+ (when (= pmail-total-messages 0)
+ (error "No messages in this file"))
+ (with-current-buffer pmail-buffer
+ (let ((result (if existing
+ (let* ((keywords (pmail-desc-get-keywords
+ pmail-current-message))
+ (last (symbol-name pmail-last-label))
+ (default (if (member last keywords)
+ last
+ (car keywords))))
+ (unless keywords
+ (error "No labels for the current message"))
+ (completing-read
+ (concat prompt " (default " default "): ")
+ keywords nil t nil nil default))
+ (let ((default (symbol-name pmail-last-label)))
+ (completing-read
+ (concat prompt (if pmail-last-label
+ (concat " (default " default "): ")
+ ": "))
+ (mapcar 'list pmail-keywords)
+ nil nil nil nil default)))))
+ (setq pmail-last-label (pmail-make-label result t))
+ ;; return the string, not the symbol
+ result)))
+
+(declare-function pmail-maybe-set-message-counters "pmail" ())
+(declare-function pmail-display-labels "pmail" ())
+(declare-function pmail-msgbeg "pmail" (n))
+(declare-function pmail-set-message-deleted-p "pmail" (n state))
+(declare-function pmail-message-labels-p "pmail" (msg labels))
+(declare-function pmail-show-message "pmail" (&optional n no-summary))
+(declare-function mail-comma-list-regexp "mail-utils" (labels))
+(declare-function mail-parse-comma-list "mail-utils.el" ())
+
+(defun pmail-set-label (l state &optional n)
+ "Add or remove label L in message N.
+The label L is added when STATE is non-nil, otherwise it is
+removed. If N is nil then use the current Pmail message. The
+current buffer, possibly narrowed, displays a message."
+ (if (= pmail-total-messages 0)
+ (error "No messages in this file"))
+ (with-current-buffer pmail-buffer
+ (if (not n) (setq n pmail-current-message))
+ (save-restriction
+ (widen)
+ (narrow-to-region (pmail-desc-get-start n) (pmail-desc-get-end n))
+ ;; FIXME: we should move all string-using functions to symbols!
+ (let ((str (symbol-name l)))
+ (if (pmail-attribute-p l)
+ (pmail-set-attribute str state n)
+ ;; Make sure the keyword is registered.
+ (pmail-register-keyword l)
+ (if state
+ (pmail-desc-add-keyword str n)
+ (pmail-desc-remove-keyword str n))))))
+ (pmail-display-labels)
+ ;; Deal with the summary buffer.
+ (when pmail-summary-buffer
+ (pmail-summary-update n)))
+\f
+;; Motion on messages with keywords.
+
+;;;###autoload
+(defun pmail-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: ")
+ (pmail-next-labeled-message (- n) labels))
+
+;;;###autoload
+(defun pmail-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: ")
+ (when (string= labels "")
+ (setq labels pmail-last-multi-labels))
+ (unless labels
+ (error "No labels to find have been specified previously"))
+ (with-current-buffer pmail-buffer
+ (setq pmail-last-multi-labels labels)
+ (let ((lastwin pmail-current-message)
+ (current pmail-current-message)
+ (regexp (concat ", ?\\("
+ (mail-comma-list-regexp labels)
+ "\\),")))
+ (save-restriction
+ (widen)
+ (while (and (> n 0) (< current pmail-total-messages))
+ (setq current (1+ current))
+ (when (pmail-message-labels-p current regexp)
+ (setq lastwin current n (1- n))))
+ (while (and (< n 0) (> current 1))
+ (setq current (1- current))
+ (when (pmail-message-labels-p current regexp)
+ (setq lastwin current n (1+ n)))))
+ (pmail-show-message lastwin)
+ (when (< n 0)
+ (message "No previous message with labels %s" labels))
+ (when (> n 0)
+ (message "No following message with labels %s" labels)))))
+
+;; arch-tag: b26b3392-99ca-4e1d-933a-dab59b04e9a8
+;;; pmailkwd.el ends here
--- /dev/null
+;;; pmailmm.el --- MIME decoding and display stuff for PMAIL
+
+;; Copyright (C) 2006, 2007, 2008 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 2, or (at your option)
+;; any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs; see the file COPYING. If not, write to the
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
+
+;;; 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 'pmail-mime "pmailmm"
+;; "Show MIME message." t)
+;; (add-hook 'pmail-mode-hook
+;; (lambda ()
+;; (define-key pmail-mode-map (kbd "v")
+;; 'pmail-mime)))
+
+;;; Code:
+
+;;; Variables
+
+(defcustom pmail-mime-media-type-handlers-alist
+ '(("multipart/.*" pmail-mime-multipart-handler)
+ ("text/.*" pmail-mime-text-handler)
+ ("text/\\(x-\\)?patch" pmail-mime-bulk-handler)
+ ("application/pgp-signature" pmail-mime-application/pgp-signature-handler)
+ ("\\(image\\|audio\\|video\\|application\\)/.*" pmail-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 pmail-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 pmail-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 'pmail-mime-total-number-of-bulk-attachments 'permanent-local t)
+
+;;; Buttons
+
+(defun pmail-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 'pmail-mime-save
+ 'action 'pmail-mime-save)
+
+;;; Handlers
+
+(defun pmail-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-pmail-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)
+ (pmail-mime-show t)
+ (set-buffer-multibyte t)))
+
+(defun pmail-mime-bulk-handler (content-type
+ content-disposition
+ content-transfer-encoding)
+ "Handle the current buffer as an attachment to download."
+ (setq pmail-mime-total-number-of-bulk-attachments
+ (1+ pmail-mime-total-number-of-bulk-attachments))
+ ;; Find the default directory for this media type
+ (let* ((directory (catch 'directory
+ (dolist (entry pmail-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 'pmail-mime-save
+ 'filename filename
+ 'directory directory
+ 'data data)))
+
+(defun test-pmail-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)
+ (pmail-mime-show)))
+
+(defun pmail-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 `pmail-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 next)
+ (unless boundary
+ (error "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 pmail-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 `pmail-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
+ (error "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)
+ (pmail-mime-show))))
+ (setq beg next)
+ (goto-char beg))))
+
+(defun test-pmail-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)
+ (pmail-mime-show t)))
+
+;;; Main code
+
+(defun pmail-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 pmail-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.
+ (pmail-mime-bulk-handler content-type
+ content-disposition
+ content-transfer-encoding)))
+
+(defun pmail-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. We're not using `pmail-header-get-header'
+ ;; because we must be able to handle the case of no headers
+ ;; existing in a part. In this case end is at point-min.
+ (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")
+ (pmail-header-hide-headers)
+ (narrow-to-region end (point-max)))
+ (show-headers
+ (pmail-header-hide-headers))
+ (t
+ (delete-region (point-min) end)))
+ (pmail-mime-handle content-type content-disposition
+ content-transfer-encoding))))
+
+(defun pmail-mime ()
+ "Copy buffer contents to a temporary buffer and handle MIME.
+This calls `pmail-mime-show' to do the real job."
+ (interactive)
+ (let ((data (with-current-buffer pmail-buffer
+ (save-restriction
+ (widen)
+ (buffer-substring
+ (pmail-desc-get-start pmail-current-message)
+ (pmail-desc-get-end pmail-current-message)))))
+ (buf (get-buffer-create "*PMAIL*")))
+ (set-buffer buf)
+ (let ((inhibit-read-only t))
+ (erase-buffer)
+ (insert data)
+ (pmail-mime-show t))
+ (view-buffer buf)))
+
+(provide 'pmailmm)
+
+;; pmailmm.el ends here
--- /dev/null
+;;; pmailmsc.el --- miscellaneous support functions for the PMAIL mail reader
+
+;; Copyright (C) 1985, 2002, 2003, 2004, 2005,
+;; 2006, 2007, 2008 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 'pmail))
+
+(defvar pmail-current-message)
+(defvar pmail-inbox-list)
+
+(declare-function pmail-parse-file-inboxes "pmail" ())
+(declare-function pmail-show-message "pmail" (&optional n no-summary))
+
+;;;###autoload
+(defun set-pmail-inbox-list (file-name)
+ "Set the inbox list of the current PMAIL 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 'pmail-mode)
+ (error "set-pmail-inbox-list works only for an Pmail file"))
+ (let ((inbox-list
+ (with-temp-buffer
+ (insert file-name)
+ (goto-char (point-min))
+ (nreverse (mail-parse-comma-list)))))
+ (when (or (not pmail-inbox-list)
+ (y-or-n-p (concat "Replace "
+ (mapconcat 'identity
+ pmail-inbox-list
+ ", ")
+ "? ")))
+ (message "Setting the inbox list for %s for this session"
+ (file-name-nondirectory (buffer-file-name)))
+ (setq pmail-inbox-list inbox-list)))
+ (pmail-show-message pmail-current-message))
+
+;; arch-tag: 74ed1d50-2c25-4cbd-b5ae-d29ed8aba6e4
+;;; pmailmsc.el ends here
--- /dev/null
+;;; pmailout.el --- "PMAIL" mail reader for Emacs: output message to a file.
+
+;; Copyright (C) 1985, 1987, 1993, 1994, 2001, 2002, 2003, 2004,
+;; 2005, 2006, 2007, 2008 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:
+
+(provide 'pmailout)
+
+(eval-when-compile
+ (require 'pmail)
+ (require 'pmaildesc))
+
+;;;###autoload
+(defcustom pmail-output-file-alist nil
+ "*Alist matching regexps to suggested output Pmail 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 'pmail-output)
+
+;;;###autoload
+(defcustom pmail-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 'pmail-output)
+
+(defun pmail-output-read-file-name ()
+ "Read the file name to use for `pmail-output'.
+Set `pmail-default-file' to this name as well as returning it."
+ (let* ((default-file
+ (with-current-buffer pmail-buffer
+ (expand-file-name
+ (or (catch 'answer
+ (dolist (i pmail-output-file-alist)
+ (goto-char (point-min))
+ (when (re-search-forward (car i) nil t)
+ (throw 'answer (eval (cdr i))))))
+ pmail-default-file))))
+ (read-file
+ (expand-file-name
+ (read-file-name
+ (concat "Output message to Pmail (mbox) file: (default "
+ (file-name-nondirectory default-file) "): ")
+ (file-name-directory default-file)
+ (abbreviate-file-name default-file))
+ (file-name-directory default-file))))
+ (setq pmail-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))))))
+
+(declare-function pmail-update-summary "pmailsum" (&rest ignore))
+
+;;; There are functions elsewhere in Emacs that use this function;
+;;; look at them before you change the calling method.
+;;;###autoload
+(defun pmail-output-to-pmail-file (file-name &optional count stay)
+ "Append the current message to an Pmail (mbox) file named FILE-NAME.
+If the file does not exist, ask if it should be created.
+If file is being visited, the message is appended to the Emacs
+buffer visiting that file.
+If the file exists and is not an Pmail file, the message is
+appended in inbox format, the same way `pmail-output' does it.
+
+The default file name comes from `pmail-default-pmail-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.
+
+If the optional argument STAY is non-nil, then leave the last filed
+message up instead of moving forward to the next non-deleted message."
+ (interactive (list (pmail-output-read-file-name)
+ (prefix-numeric-value current-prefix-arg)))
+ ;; Use the 'pmail-output function to perform the output.
+ (pmail-output file-name count nil nil)
+ ;; Deal with the next message
+ (if pmail-delete-after-output
+ (unless (if (and (= count 0) stay)
+ (pmail-delete-message)
+ (pmail-delete-forward))
+ (setq count 0))
+ (when (> count 0)
+ (unless (when (not stay)
+ (pmail-next-undeleted-message 1))
+ (setq count 0)))))
+
+(defun pmail-delete-unwanted-fields ()
+ "Delete from the buffer header fields we don't want output."
+ (when pmail-fields-not-to-output
+ (save-excursion
+ (let ((limit (pmail-header-get-limit))
+ (inhibit-point-motion-hooks t)
+ start)
+ (goto-char (point-min))
+ (while (re-search-forward pmail-fields-not-to-output limit t)
+ (forward-line 0)
+ (setq start (point))
+ (while (progn (forward-line 1) (looking-at "[ \t]+"))
+ (goto-char (line-end-position)))
+ (delete-region start (point)))))))
+
+;;; There are functions elsewhere in Emacs that use this function;
+;;; look at them before you change the calling method.
+;;;###autoload
+(defun pmail-output (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.
+
+If the pruned message header is shown on the current message, then
+messages will be appended with pruned headers; otherwise, messages
+will be appended with their original headers.
+
+The default file name comes from `pmail-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 (pmail-output-read-file-name)
+ (prefix-numeric-value current-prefix-arg)))
+ (or count (setq count 1))
+ (setq file-name
+ (expand-file-name file-name
+ (and pmail-default-file
+ (file-name-directory pmail-default-file))))
+ (if (and (file-readable-p file-name) (mail-file-babyl-p file-name))
+ (error "BABYL output not supported.")
+ (with-current-buffer pmail-buffer
+ (let ((orig-count count)
+ (pmailbuf (current-buffer))
+ (destbuf (find-buffer-visiting file-name))
+ (case-fold-search t))
+ (while (> count 0)
+ (with-temp-buffer
+ (insert-buffer-substring pmailbuf)
+ ;; ensure we can write without barfing on exotic characters
+ (setq buffer-file-coding-system
+ (or pmail-file-coding-system 'raw-text))
+ ;; prune junk headers
+ (pmail-delete-unwanted-fields)
+ (if (not destbuf)
+ ;; The destination file is not being visited, just write
+ ;; out the processed message.
+ (write-region (point-min) (point-max) file-name
+ t (when noattribute 'nomsg))
+ ;; The destination file is being visited. Update it.
+ (let ((msg-string (buffer-string)))
+ (with-current-buffer destbuf
+ ;; Determine if the destination file is an Pmail file.
+ (let ((buffer-read-only nil)
+ (dest-current-message
+ (and (boundp 'pmail-current-message)
+ pmail-current-message)))
+ (if dest-current-message
+ ;; The buffer is an Pmail buffer. Append the
+ ;; message.
+ (progn
+ (widen)
+ (narrow-to-region (point-max) (point-max))
+ (insert msg-string)
+ (insert "\n")
+ (pmail-process-new-messages)
+ (pmail-show-message dest-current-message))
+ ;; The destination file is not an Pmail file, just
+ ;; insert at the end.
+ (goto-char (point-max))
+ (insert msg-string)))))))
+ (unless noattribute
+ (when (equal major-mode 'pmail-mode)
+ (pmail-set-attribute "filed" t)
+ (pmail-header-hide-headers)))
+ (setq count (1- count))
+ (unless from-gnus
+ (let ((next-message-p
+ (if pmail-delete-after-output
+ (pmail-delete-forward)
+ (when (> count 0)
+ (pmail-next-undeleted-message 1))))
+ (num-appended (- orig-count count)))
+ (when (and (> count 0) (not next-message-p))
+ (error (format "Only %d message%s appended" num-appended
+ (if (= num-appended 1) "" "s")))
+ (setq count 0)))))))))
+
+;;;###autoload
+(defun pmail-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")
+ pmail-default-body-file)))
+ (list (setq pmail-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 pmail-default-body-file
+ (file-name-directory pmail-default-body-file))))
+ (save-excursion
+ (goto-char (point-min))
+ (search-forward "\n\n")
+ (and (file-exists-p file-name)
+ (not (y-or-n-p (message "File %s exists; overwrite? " file-name)))
+ (error "Operation aborted"))
+ (write-region (point) (point-max) file-name)
+ (when (equal major-mode 'pmail-mode)
+ (pmail-desc-set-attribute pmail-desc-stored-index
+ t pmail-current-message)))
+ (when pmail-delete-after-output
+ (pmail-delete-forward)))
+
+;; arch-tag: 447117c6-1a9a-4b88-aa43-3101b043e3a4
+;;; pmailout.el ends here
--- /dev/null
+;;; pmailsort.el --- Pmail: sort messages
+
+;; Copyright (C) 1990, 1993, 1994, 2001, 2002, 2003, 2004,
+;; 2005, 2006, 2007, 2008 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 'pmail))
+
+(autoload 'timezone-make-date-sortable "timezone")
+
+;; Sorting messages in Pmail buffer
+
+;;;###autoload
+(defun pmail-sort-by-date (reverse)
+ "Sort messages of current Pmail file by date.
+If prefix argument REVERSE is non-nil, sort them in reverse order."
+ (interactive "P")
+ (pmail-sort-messages reverse
+ (function
+ (lambda (msg)
+ (pmail-make-date-sortable
+ (pmail-fetch-field msg "Date"))))))
+
+;;;###autoload
+(defun pmail-sort-by-subject (reverse)
+ "Sort messages of current Pmail file by subject.
+If prefix argument REVERSE is non-nil, sort them in reverse order."
+ (interactive "P")
+ (pmail-sort-messages reverse
+ (function
+ (lambda (msg)
+ (let ((key (or (pmail-fetch-field msg "Subject") ""))
+ (case-fold-search t))
+ ;; Remove `Re:'
+ (if (string-match "^\\(re:[ \t]*\\)*" key)
+ (substring key (match-end 0))
+ key))))))
+
+;;;###autoload
+(defun pmail-sort-by-author (reverse)
+ "Sort messages of current Pmail file by author.
+If prefix argument REVERSE is non-nil, sort them in reverse order."
+ (interactive "P")
+ (pmail-sort-messages reverse
+ (function
+ (lambda (msg)
+ (downcase ;Canonical name
+ (mail-strip-quoted-names
+ (or (pmail-fetch-field msg "From")
+ (pmail-fetch-field msg "Sender") "")))))))
+
+;;;###autoload
+(defun pmail-sort-by-recipient (reverse)
+ "Sort messages of current Pmail file by recipient.
+If prefix argument REVERSE is non-nil, sort them in reverse order."
+ (interactive "P")
+ (pmail-sort-messages reverse
+ (function
+ (lambda (msg)
+ (downcase ;Canonical name
+ (mail-strip-quoted-names
+ (or (pmail-fetch-field msg "To")
+ (pmail-fetch-field msg "Apparently-To") "")
+ ))))))
+
+;;;###autoload
+(defun pmail-sort-by-correspondent (reverse)
+ "Sort messages of current Pmail file by other correspondent.
+If prefix argument REVERSE is non-nil, sort them in reverse order."
+ (interactive "P")
+ (pmail-sort-messages reverse
+ (function
+ (lambda (msg)
+ (pmail-select-correspondent
+ msg
+ '("From" "Sender" "To" "Apparently-To"))))))
+
+(defun pmail-select-correspondent (msg fields)
+ (let ((ans ""))
+ (while (and fields (string= ans ""))
+ (setq ans
+ (pmail-dont-reply-to
+ (mail-strip-quoted-names
+ (or (pmail-fetch-field msg (car fields)) ""))))
+ (setq fields (cdr fields)))
+ ans))
+
+;;;###autoload
+(defun pmail-sort-by-lines (reverse)
+ "Sort messages of current Pmail file by number of lines.
+If prefix argument REVERSE is non-nil, sort them in reverse order."
+ (interactive "P")
+ (pmail-sort-messages reverse
+ (function
+ (lambda (msg)
+ (count-lines (pmail-desc-get-start msg)
+ (pmail-desc-get-end msg))))))
+
+;;;###autoload
+(defun pmail-sort-by-labels (reverse labels)
+ "Sort messages of current Pmail 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)))
+ (pmail-sort-messages reverse
+ (function
+ (lambda (msg)
+ (let ((n 0))
+ (while (and (< n (length labelvec))
+ (not (pmail-message-labels-p
+ msg (aref labelvec n))))
+ (setq n (1+ n)))
+ n))))))
+\f
+;; Basic functions
+
+(declare-function pmail-update-summary "pmailsum" (&rest ignore))
+
+(defun pmail-sort-messages (reverse keyfun)
+ "Sort messages of current Pmail 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."
+ (save-current-buffer
+ ;; If we are in a summary buffer, operate on the Pmail buffer.
+ (if (eq major-mode 'pmail-summary-mode)
+ (set-buffer pmail-buffer))
+ (let ((buffer-read-only nil)
+ (point-offset (- (point) (point-min)))
+ (predicate nil) ;< or string-lessp
+ (sort-lists nil))
+ (message "Finding sort keys...")
+ (pmail-header-show-headers)
+ (widen)
+ (let ((msgnum 1))
+ (while (>= pmail-total-messages msgnum)
+ (setq sort-lists
+ (cons (list (funcall keyfun msgnum) ;Make sorting key
+ (eq pmail-current-message msgnum) ;True if current
+ (pmail-desc-get-marker-start msgnum)
+ (pmail-desc-get-marker-end 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
+ (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 (pmail-desc-get-start 1))
+ ;; To force update of all markers.
+ (insert-before-markers ?Z)
+ (backward-char 1)
+ ;; Now reorder messages.
+ (while sort-lists
+ (setq msginfo (car sort-lists))
+ ;; Swap two messages.
+ (insert-buffer-substring
+ (current-buffer) (nth 2 msginfo) (nth 3 msginfo))
+ (delete-region (nth 2 msginfo) (nth 3 msginfo))
+ ;; Is current message?
+ (if (nth 1 msginfo)
+ (setq current-message msgnum))
+ (setq sort-lists (cdr sort-lists))
+ (if (zerop (% msgnum 10))
+ (message "Reordering messages...%d" msgnum))
+ (setq msgnum (1+ msgnum)))
+ ;; Delete the garbage inserted before.
+ (delete-char 1)
+ (setq quit-flag nil)
+ (buffer-enable-undo)
+ (goto-char (point-min))
+ (pmail-initialize-messages)
+ (pmail-show-message current-message)
+ (if (pmail-summary-exists)
+ (pmail-select-summary
+ (pmail-update-summary)))))))
+
+;; mbox: ready
+(defun pmail-fetch-field (msg field)
+ "Return the value of the header FIELD of MSG.
+Arguments are MSG and FIELD."
+ (save-restriction
+ (widen)
+ (narrow-to-region
+ (pmail-desc-get-start msg)
+ (pmail-desc-get-end msg))
+ (pmail-header-get-header field)))
+
+(defun pmail-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 'pmailsort)
+
+;; arch-tag: 0d90896b-0c35-46ac-b240-38be5ada2360
+;;; pmailsort.el ends here
--- /dev/null
+;;; pmailsum.el --- make summary buffers for the mail reader
+
+;; Copyright (C) 1985, 1993, 1994, 1995, 1996, 2000, 2001, 2002, 2003,
+;; 2004, 2005, 2006, 2007, 2008 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:
+
+;; All commands run from the summary buffer update the buffer local
+;; variable `pmail-current-message'. As part of the post command
+;; processing point is moved to the beginning of the line describing
+;; the current message.
+
+;;; History:
+
+;; Extended by Bob Weiner of Motorola
+;; Provided all commands from pmail-mode in pmail-summary-mode and made key
+;; bindings in both modes wholly compatible.
+
+;; Overhauled by Paul Reilly to support mbox format.
+
+;;; Code:
+
+(defvar msgnum)
+
+;; For pmail-select-summary
+(require 'pmail)
+
+;;;###autoload
+(defcustom pmail-summary-scroll-between-messages t
+ "*Non-nil means Pmail summary scroll commands move between messages."
+ :type 'boolean
+ :group 'pmail-summary)
+
+;;;###autoload
+(defcustom pmail-summary-line-count-flag t
+ "*Non-nil if Pmail summary should show the number of lines in each message."
+ :type 'boolean
+ :group 'pmail-summary)
+
+(defvar pmail-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 Pmail Summary mode.")
+
+(defvar pmail-summary-redo nil
+ "Private storage for Pmail summary history.")
+
+(defvar pmail-summary-overlay nil
+ "Private storage for an Pmail summary overlay cache")
+(put 'pmail-summary-overlay 'permanent-local t)
+
+(defvar pmail-summary-mode-map
+ (let ((map (make-keymap)))
+ (suppress-keymap map)
+ (define-key map [mouse-2] 'pmail-summary-mouse-goto-message)
+ (define-key map "a" 'pmail-summary-add-label)
+ (define-key map "b" 'pmail-summary-bury)
+ (define-key map "B" 'pmail-summary-browse-body)
+ (define-key map "c" 'pmail-summary-continue)
+ (define-key map "d" 'pmail-summary-delete-forward)
+ (define-key map "\C-d" 'pmail-summary-delete-backward)
+ (define-key map "e" 'pmail-summary-edit-current-message)
+ (define-key map "f" 'pmail-summary-forward)
+ (define-key map "g" 'pmail-summary-get-new-mail)
+ (define-key map "h" 'pmail-summary)
+ (define-key map "i" 'pmail-summary-input)
+ (define-key map "j" 'pmail-summary-goto-msg)
+ (define-key map "\C-m" 'pmail-summary-goto-msg)
+ (define-key map "k" 'pmail-summary-kill-label)
+ (define-key map "l" 'pmail-summary-by-labels)
+ (define-key map "\e\C-h" 'pmail-summary)
+ (define-key map "\e\C-l" 'pmail-summary-by-labels)
+ (define-key map "\e\C-r" 'pmail-summary-by-recipients)
+ (define-key map "\e\C-f" 'pmail-summary-by-senders)
+ (define-key map "\e\C-s" 'pmail-summary-by-regexp)
+ (define-key map "\e\C-t" 'pmail-summary-by-topic)
+ (define-key map "m" 'pmail-summary-mail)
+ (define-key map "\M-m" 'pmail-summary-retry-failure)
+ (define-key map "n" 'pmail-summary-next-msg)
+ (define-key map "\en" 'pmail-summary-next-all)
+ (define-key map "\e\C-n" 'pmail-summary-next-labeled-message)
+ (define-key map "o" 'pmail-summary-output)
+ (define-key map "\C-o" 'pmail-summary-output)
+ (define-key map "p" 'pmail-summary-previous-msg)
+ (define-key map "\ep" 'pmail-summary-previous-all)
+ (define-key map "\e\C-p" 'pmail-summary-previous-labeled-message)
+ (define-key map "q" 'pmail-summary-quit)
+ (define-key map "Q" 'pmail-summary-wipe)
+ (define-key map "r" 'pmail-summary-reply)
+ (define-key map "s" 'pmail-summary-expunge-and-save)
+ (define-key map "\es" 'pmail-summary-search)
+ (define-key map "t" 'pmail-summary-toggle-header)
+ (define-key map "u" 'pmail-summary-undelete)
+ (define-key map "\M-u" 'pmail-summary-undelete-many)
+ (define-key map "x" 'pmail-summary-expunge)
+ (define-key map "w" 'pmail-summary-output-body)
+ (define-key map "." 'pmail-summary-beginning-of-message)
+ (define-key map "/" 'pmail-summary-end-of-message)
+ (define-key map "<" 'pmail-summary-first-message)
+ (define-key map ">" 'pmail-summary-last-message)
+ (define-key map " " 'pmail-summary-scroll-msg-up)
+ (define-key map "\177" 'pmail-summary-scroll-msg-down)
+ (define-key map "?" 'describe-mode)
+ (define-key map "\C-c\C-n" 'pmail-summary-next-same-subject)
+ (define-key map "\C-c\C-p" 'pmail-summary-previous-same-subject)
+ (define-key map "\C-c\C-s\C-d" 'pmail-summary-sort-by-date)
+ (define-key map "\C-c\C-s\C-s" 'pmail-summary-sort-by-subject)
+ (define-key map "\C-c\C-s\C-a" 'pmail-summary-sort-by-author)
+ (define-key map "\C-c\C-s\C-r" 'pmail-summary-sort-by-recipient)
+ (define-key map "\C-c\C-s\C-c" 'pmail-summary-sort-by-correspondent)
+ (define-key map "\C-c\C-s\C-l" 'pmail-summary-sort-by-lines)
+ (define-key map "\C-c\C-s\C-k" 'pmail-summary-sort-by-labels)
+ (define-key map [menu-bar] (make-sparse-keymap))
+ (define-key map [menu-bar classify]
+ (cons "Classify" (make-sparse-keymap "Classify")))
+ (define-key map [menu-bar classify output-menu]
+ '("Output (Pmail Menu)..." . pmail-summary-output-menu))
+ (define-key map [menu-bar classify input-menu]
+ '("Input Pmail File (menu)..." . pmail-input-menu))
+ (define-key map [menu-bar classify input-menu] '(nil))
+ (define-key map [menu-bar classify output-menu] '(nil))
+ (define-key map [menu-bar classify output-body]
+ '("Output (body)..." . pmail-summary-output-body))
+ (define-key map [menu-bar classify output-inbox]
+ '("Output (inbox)..." . pmail-summary-output))
+ (define-key map [menu-bar classify output]
+ '("Output (Pmail)..." . pmail-summary-output))
+ (define-key map [menu-bar classify kill-label]
+ '("Kill Label..." . pmail-summary-kill-label))
+ (define-key map [menu-bar classify add-label]
+ '("Add Label..." . pmail-summary-add-label))
+ (define-key map [menu-bar summary]
+ (cons "Summary" (make-sparse-keymap "Summary")))
+ (define-key map [menu-bar summary senders]
+ '("By Senders..." . pmail-summary-by-senders))
+ (define-key map [menu-bar summary labels]
+ '("By Labels..." . pmail-summary-by-labels))
+ (define-key map [menu-bar summary recipients]
+ '("By Recipients..." . pmail-summary-by-recipients))
+ (define-key map [menu-bar summary topic]
+ '("By Topic..." . pmail-summary-by-topic))
+ (define-key map [menu-bar summary regexp]
+ '("By Regexp..." . pmail-summary-by-regexp))
+ (define-key map [menu-bar summary all]
+ '("All" . pmail-summary))
+ (define-key map [menu-bar mail]
+ (cons "Mail" (make-sparse-keymap "Mail")))
+ (define-key map [menu-bar mail pmail-summary-get-new-mail]
+ '("Get New Mail" . pmail-summary-get-new-mail))
+ (define-key map [menu-bar mail lambda]
+ '("----"))
+ (define-key map [menu-bar mail continue]
+ '("Continue" . pmail-summary-continue))
+ (define-key map [menu-bar mail resend]
+ '("Re-send..." . pmail-summary-resend))
+ (define-key map [menu-bar mail forward]
+ '("Forward" . pmail-summary-forward))
+ (define-key map [menu-bar mail retry]
+ '("Retry" . pmail-summary-retry-failure))
+ (define-key map [menu-bar mail reply]
+ '("Reply" . pmail-summary-reply))
+ (define-key map [menu-bar mail mail]
+ '("Mail" . pmail-summary-mail))
+ (define-key map [menu-bar delete]
+ (cons "Delete" (make-sparse-keymap "Delete")))
+ (define-key map [menu-bar delete expunge/save]
+ '("Expunge/Save" . pmail-summary-expunge-and-save))
+ (define-key map [menu-bar delete expunge]
+ '("Expunge" . pmail-summary-expunge))
+ (define-key map [menu-bar delete undelete]
+ '("Undelete" . pmail-summary-undelete))
+ (define-key map [menu-bar delete delete]
+ '("Delete" . pmail-summary-delete-forward))
+ (define-key map [menu-bar move]
+ (cons "Move" (make-sparse-keymap "Move")))
+ (define-key map [menu-bar move search-back]
+ '("Search Back..." . pmail-summary-search-backward))
+ (define-key map [menu-bar move search]
+ '("Search..." . pmail-summary-search))
+ (define-key map [menu-bar move previous]
+ '("Previous Nondeleted" . pmail-summary-previous-msg))
+ (define-key map [menu-bar move next]
+ '("Next Nondeleted" . pmail-summary-next-msg))
+ (define-key map [menu-bar move last]
+ '("Last" . pmail-summary-last-message))
+ (define-key map [menu-bar move first]
+ '("First" . pmail-summary-first-message))
+ (define-key map [menu-bar move previous]
+ '("Previous" . pmail-summary-previous-all))
+ (define-key map [menu-bar move next]
+ '("Next" . pmail-summary-next-all))
+ map)
+ "Keymap for `pmail-summary-mode'.")
+
+;; 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 pmail-update-summary (&rest ignore)
+ (apply (car pmail-summary-redo) (cdr pmail-summary-redo)))
+
+;;;###autoload
+(defun pmail-summary ()
+ "Display a summary of all messages, one line per message."
+ (interactive)
+ (pmail-new-summary "All" '(pmail-summary) nil))
+
+;;;###autoload
+(defun pmail-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 pmail-last-multi-labels
+ (error "No label specified"))))
+ (setq pmail-last-multi-labels labels)
+ (pmail-new-summary (concat "labels " labels)
+ (list 'pmail-summary-by-labels labels)
+ 'pmail-message-labels-p
+ (mail-comma-list-regexp labels)))
+
+;;;###autoload
+(defun pmail-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")
+ (pmail-new-summary
+ (concat "recipients " recipients)
+ (list 'pmail-summary-by-recipients recipients primary-only)
+ 'pmail-message-recipients-p
+ (mail-comma-list-regexp recipients) primary-only))
+
+;;;###autoload
+(defun pmail-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 PMAIL-summary."
+ (interactive "sRegexp to summarize by: ")
+ (if (string= regexp "")
+ (setq regexp (or pmail-last-regexp
+ (error "No regexp specified"))))
+ (setq pmail-last-regexp regexp)
+ (pmail-new-summary (concat "regexp " regexp)
+ (list 'pmail-summary-by-regexp regexp)
+ 'pmail-message-regexp-p
+ regexp))
+
+;;;###autoload
+(defun pmail-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 (with-current-buffer pmail-buffer
+ (pmail-current-subject)))
+ (prompt (concat "Topics to summarize by (regexp"
+ (if subject ", default current subject" "")
+ "): ")))
+ (list (read-string prompt nil nil subject) current-prefix-arg)))
+ (pmail-new-summary
+ (concat "about " subject)
+ (list 'pmail-summary-by-topic subject whole-message)
+ 'pmail-message-subject-p
+ (mail-comma-list-regexp subject) whole-message))
+
+(defun pmail-message-subject-p (msg subject &optional whole-message)
+ "Return non-nil if SUBJECT is found in MSG.
+If WHOLE-MESSAGE is nil only the subject header will be searched,
+otherwise the whole message will be searched for text matching
+SUBJECT. Return nil to indicate that SUBJECT is not found,
+non-nil otherwise."
+ (save-restriction
+ (narrow-to-region
+ (pmail-desc-get-start msg)
+ (pmail-desc-get-end msg))
+ (goto-char (point-min))
+ (if whole-message
+ (re-search-forward subject nil t))
+ (string-match subject
+ (let ((subj (pmail-header-get-header "subject")))
+ (if subj
+ (funcall pmail-summary-line-decoder subj)
+ "")))))
+
+;;;###autoload
+(defun pmail-summary-by-senders (senders)
+ "Display a summary of all messages with the given SENDERS.
+SENDERS is a string of names separated by commas."
+ (interactive
+ (let* ((sender (when pmail-current-message
+ (pmail-desc-get-sender pmail-current-message)))
+ (sender-re (with-current-buffer pmail-buffer
+ (regexp-quote sender)))
+ (prompt (concat "Senders to summarize by (regexp"
+ (if sender ", default current sender" "")
+ "): ")))
+ (list (read-string prompt nil nil sender))))
+ (pmail-new-summary
+ (concat "senders " senders)
+ (list 'pmail-summary-by-senders senders)
+ 'pmail-message-senders-p
+ (mail-comma-list-regexp senders)))
+
+(defun pmail-message-senders-p (msg sender)
+ "Return non-nil if SENDER is found in MSG.
+The From header is tested."
+ (save-restriction
+ (narrow-to-region
+ (pmail-desc-get-start msg)
+ (pmail-desc-get-end msg))
+ (goto-char (point-min))
+ (string-match sender (or (mail-fetch-field "From") ""))))
+\f
+;;;; General making of a summary buffer.
+
+(defvar pmail-summary-symbol-number 0)
+
+(defun pmail-new-summary (description redo-form function &rest args)
+ "Create a summary of selected messages.
+DESCRIPTION makes part of the mode line of the summary buffer.
+For each message, FUNCTION 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...")
+ (let ((summary-msgs ())
+ (new-summary-line-count 0)
+ (msgnum 1)
+ current-message sumbuf was-in-summary)
+ (save-excursion
+ ;; Go to the Pmail buffer.
+ (if (eq major-mode 'pmail-summary-mode)
+ (setq was-in-summary t))
+ (set-buffer pmail-buffer)
+ ;; Find its summary buffer, or make one.
+ (setq current-message pmail-current-message
+ sumbuf
+ (if (and pmail-summary-buffer
+ (buffer-name pmail-summary-buffer))
+ pmail-summary-buffer
+ (generate-new-buffer (concat (buffer-name) "-summary"))))
+ ;; Collect the message summaries based on the filtering
+ ;; argument (FUNCTION).
+ (while (>= pmail-total-messages msgnum)
+ (if (or (null function)
+ (apply function (cons msgnum args)))
+ (setq summary-msgs
+ (cons (cons msgnum (pmail-summary-get-summary-line msgnum))
+ summary-msgs)))
+ (setq msgnum (1+ msgnum)))
+ (setq summary-msgs (nreverse summary-msgs))
+ ;; Place the collected summaries into the summary buffer.
+ (setq pmail-summary-buffer nil)
+ (save-excursion
+ (let ((rbuf (current-buffer))
+ (vbuf pmail-view-buffer)
+ (total pmail-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)
+ (pmail-summary-mode)
+ (make-local-variable 'minor-mode-alist)
+ (setq minor-mode-alist (list (list t (concat ": " description))))
+ (setq pmail-buffer rbuf
+ pmail-view-buffer vbuf
+ pmail-summary-redo redo-form
+ pmail-total-messages total
+ pmail-current-message current-message)))
+ (setq pmail-summary-buffer sumbuf))
+ ;; Now display the summary buffer and go to the right place in it.
+ (or was-in-summary
+ (progn
+ (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) pmail-summary-window-size)
+ (select-window (next-window (frame-first-window)))
+ (pop-to-buffer sumbuf)
+ ;; If pop-to-buffer did not use that window, delete that
+ ;; window. (This can happen if it uses another frame.)
+ (if (not (eq sumbuf (window-buffer (frame-first-window))))
+ (delete-other-windows)))
+ (pop-to-buffer sumbuf))
+ (set-buffer pmail-buffer)
+ ;; This is how pmail makes the summary buffer reappear.
+ ;; We do this here to make the window the proper size.
+ (pmail-select-summary nil)
+ (set-buffer pmail-summary-buffer)))
+ (pmail-summary-goto-msg current-message nil t)
+ (pmail-summary-construct-io-menu)
+ (message "Computing summary lines...done")))
+\f
+;;;; Low levels of generating a summary.
+
+;;;###autoload
+(defcustom pmail-summary-line-decoder (function identity)
+ "*Function to decode summary-line.
+
+By default, `identity' is set."
+ :type 'function
+ :group 'pmail-summary)
+
+;;;###autoload
+(defcustom pmail-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)))))
+ "\\>\\)")
+ "*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. It is
+initialized based on your `user-login-name' and
+`user-mail-address'.
+
+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 'pmail-retrieve
+ :version "21.1")
+
+\f
+;;;; Simple motion in a summary buffer.
+
+(defun pmail-summary-next-all (&optional number)
+ "Move to an nearby message.
+If NUMBER is positive then move forward NUMBER messages. If NUMBER is
+negative then move backwards NUMBER messages. If NUMBER is nil then
+move forward one message."
+ (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 pmail-buffer))
+
+(defun pmail-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 pmail-buffer))
+
+(defun pmail-summary-next-msg (&optional number)
+ "Display next non-deleted msg from pmail file.
+With optional prefix argument NUMBER, moves forward this number of
+non-deleted messages, or backward if NUMBER is negative."
+ (interactive "p")
+ (let (msg)
+ (with-current-buffer pmail-buffer
+ (pmail-next-undeleted-message number)
+ (setq msg pmail-current-message))
+ (pmail-summary-goto-msg msg)))
+
+(defun pmail-summary-previous-msg (&optional number)
+ "Display previous non-deleted msg from pmail file.
+With optional prefix argument NUMBER, moves backward this number of
+non-deleted messages."
+ (interactive "p")
+ (pmail-summary-next-msg (- (if number number 1))))
+
+(defun pmail-summary-next-labeled-message (n labels)
+ "Show next message with LABEL. 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 pmail-buffer)
+ (pmail-next-labeled-message n labels)
+ (setq msg pmail-current-message))
+ (setq pmail-current-message msg)))
+
+(defun pmail-summary-previous-labeled-message (n labels)
+ "Show previous message with LABEL. 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 pmail-buffer)
+ (pmail-previous-labeled-message n labels)
+ (setq msg pmail-current-message))
+ (setq pmail-current-message msg)))
+
+(defun pmail-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")
+ (with-current-buffer pmail-buffer
+ (pmail-next-same-subject n)))
+
+(defun pmail-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")
+ (pmail-summary-next-same-subject (- n)))
+
+\f
+;; Delete and undelete summary commands.
+
+(defun pmail-summary-delete-forward (&optional count)
+ "Delete this message and move to next nondeleted one.
+Deleted messages stay in the file until the \\[pmail-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)
+ (pmail-summary-goto-msg)
+ (with-current-buffer pmail-buffer
+ (pmail-delete-message)
+ (setq del-msg pmail-current-message))
+ (pmail-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)))
+ (setq count
+ (if (> count 0) (1- count) (1+ count))))
+ ;; Update the summary buffer current message counter and show the
+ ;; message in the Pmail buffer.
+ (pmail-summary-goto-msg (pmail-summary-get-message-at-point))))
+
+(defun pmail-summary-delete-backward (&optional count)
+ "Delete this message and move to previous nondeleted one.
+Deleted messages stay in the file until the \\[pmail-expunge] command is given.
+A prefix argument serves as a repeat count;
+a negative argument means to delete and move forward."
+ (interactive "p")
+ (pmail-summary-delete-forward (- count)))
+
+(defun pmail-summary-mark-deleted (&optional n undel)
+ ;; Since third arg is t, this only alters the summary, not the Pmail buf.
+ (and n (pmail-summary-goto-msg n t))
+ (or (eobp)
+ (not (overlay-get pmail-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 pmail-summary-mark-undeleted (n)
+ (pmail-summary-mark-deleted n t))
+
+(defun pmail-summary-deleted-p (&optional n)
+ (unless n (setq n pmail-current-message))
+ (with-current-buffer pmail-buffer
+ (pmail-desc-deleted-p n)))
+
+(defun pmail-summary-undelete (&optional arg)
+ "Undelete current message.
+Optional prefix ARG means undelete ARG previous messages."
+ (interactive "p")
+ (if (/= arg 1)
+ (pmail-summary-undelete-many arg)
+ (let ((buffer-read-only nil)
+ (opoint (point)))
+ (goto-char (line-end-position))
+ (if (not (re-search-backward "\\(^ *[0-9]*\\)\\(D\\)" nil t))
+ (goto-char opoint)
+ (replace-match "\\1 ")
+ (pmail-summary-goto-msg)
+ (if pmail-enable-mime
+ (set-buffer pmail-buffer)
+ (pop-to-buffer pmail-buffer))
+ (when (pmail-message-deleted-p pmail-current-message)
+ (pmail-undelete-previous-message))
+ (when pmail-enable-mime
+ (pop-to-buffer pmail-view-buffer))
+ (pop-to-buffer pmail-summary-buffer)))))
+
+(defun pmail-summary-undelete-many (&optional n)
+ "Undelete all deleted msgs, optional prefix arg N means undelete N prev msgs."
+ (interactive "P")
+ (with-current-buffer pmail-buffer
+ (let* ((init-msg (if n pmail-current-message pmail-total-messages))
+ (pmail-current-message init-msg)
+ (n (or n pmail-total-messages))
+ (msgs-undeled 0))
+ (while (and (> pmail-current-message 0) (< msgs-undeled n))
+ (when (pmail-message-deleted-p pmail-current-message)
+ (pmail-set-attribute "deleted" nil)
+ (setq msgs-undeled (1+ msgs-undeled)))
+ (setq pmail-current-message (1- pmail-current-message)))
+ (with-current-buffer pmail-summary-buffer
+ (setq pmail-current-message init-msg msgs-undeled 0)
+ (while (and (> pmail-current-message 0) (< msgs-undeled n))
+ (when (pmail-summary-deleted-p pmail-current-message)
+ (pmail-summary-mark-undeleted pmail-current-message)
+ (setq msgs-undeled (1+ msgs-undeled)))
+ (setq pmail-current-message (1- pmail-current-message)))))))
+\f
+;; Pmail Summary mode is suitable only for specially formatted data.
+(put 'pmail-summary-mode 'mode-class 'special)
+
+(defun pmail-summary-mode ()
+ "Pmail Summary Mode is invoked from Pmail Mode by using \\<pmail-mode-map>\\[pmail-summary].
+As commands are issued in the summary buffer, they are applied to the
+corresponding mail messages in the pmail buffer.
+
+All normal editing commands are turned off.
+Instead, nearly all the Pmail mode commands are available,
+though many of them move only among the messages in the summary.
+
+These additional commands exist:
+
+\\[pmail-summary-undelete-many] Undelete all or prefix arg deleted messages.
+\\[pmail-summary-wipe] Delete the summary and go to the Pmail buffer.
+
+Commands for filtering the summary:
+
+\\[pmail-summary-by-labels] Filter by label.
+\\[pmail-summary-by-topic] Filter by Subject.
+ Filter by the entire message (header and body) if given a
+ prefix argument.
+\\[pmail-summary-by-senders] Filter by From field.
+\\[pmail-summary-by-recipients] Filter by To, From, and Cc fields.
+ Filter by To and From only if given a prefix argument.
+
+The commands listed above take comma-separated lists of regular
+expressions.
+
+\\[pmail-summary-by-regexp] Filter by any header line.
+\\[pmail-summary] Restore the default summary.
+
+Commands for sorting the summary:
+
+\\[pmail-summary-sort-by-date] Sort by date.
+\\[pmail-summary-sort-by-subject] Sort by subject.
+\\[pmail-summary-sort-by-author] Sort by author.
+\\[pmail-summary-sort-by-recipient] Sort by recipient.
+\\[pmail-summary-sort-by-correspondent] Sort by correspondent.
+\\[pmail-summary-sort-by-lines] Sort by lines.
+\\[pmail-summary-sort-by-labels] Sort by labels."
+ (interactive)
+ (kill-all-local-variables)
+ (setq major-mode 'pmail-summary-mode)
+ (setq mode-name "PMAIL Summary")
+ (setq truncate-lines t)
+ (setq buffer-read-only t)
+ (set-syntax-table text-mode-syntax-table)
+ (make-local-variable 'pmail-buffer)
+ (make-local-variable 'pmail-view-buffer)
+ (make-local-variable 'pmail-total-messages)
+ (make-local-variable 'pmail-current-message)
+ (setq pmail-current-message nil)
+ (make-local-variable 'pmail-summary-redo)
+ (setq pmail-summary-redo nil)
+ (make-local-variable 'revert-buffer-function)
+ (make-local-variable 'font-lock-defaults)
+ (setq font-lock-defaults '(pmail-summary-font-lock-keywords t))
+ (pmail-summary-enable)
+ (run-mode-hooks 'pmail-summary-mode-hook))
+
+;; Summary features need to be disabled during edit mode.
+(defun pmail-summary-disable ()
+ (use-local-map text-mode-map)
+ (remove-hook 'post-command-hook 'pmail-summary-pmail-update t)
+ (setq revert-buffer-function nil))
+
+(defun pmail-summary-enable ()
+ (use-local-map pmail-summary-mode-map)
+ (add-hook 'post-command-hook 'pmail-summary-pmail-update nil t)
+ (setq revert-buffer-function 'pmail-update-summary))
+
+(defvar pmail-summary-put-back-unseen nil
+ "Used for communicating between calls to `pmail-summary-pmail-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.")
+
+(defun pmail-summary-pmail-update ()
+ "Update the Pmail summary buffer.
+Put the cursor on the beginning of the line containing the
+current message and highlight the buffer. Show in Pmail the
+message described by the summary line that point is on, but only
+if the Pmail buffer is already visible. This is on
+`post-command-hook' in summary buffers."
+ (let (buffer-read-only)
+ (save-excursion
+ ;; If at end of buffer, pretend we are on the last text line.
+ (when (eobp)
+ (forward-line -1))
+ ;; Determine the message number corresponding to line point is on.
+ (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 `unseen'.
+ (when (not isearch-mode)
+ (setq pmail-summary-put-back-unseen nil))
+ (or (eq pmail-current-message msg-num)
+ (let ((window (get-buffer-window pmail-view-buffer t))
+ (owin (selected-window)))
+ (if isearch-mode
+ (save-excursion
+ (set-buffer pmail-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 pmail-summary-put-back-unseen
+ (pmail-set-attribute "unseen" t
+ pmail-current-message))
+ ;; Arrange to do that later, for the new current message,
+ ;; if it still has `unseen'.
+ (setq pmail-summary-put-back-unseen
+ (member "unseen" (pmail-desc-get-keywords msg-num))))
+ (setq pmail-summary-put-back-unseen nil))
+ ;; Go to the desired message.
+ (setq pmail-current-message msg-num)
+ ;; Update the summary to show the message has been seen.
+ (when (= (following-char) ?-)
+ (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)
+ (pmail-show-message msg-num t))
+ (select-window owin))
+ (when (buffer-name pmail-buffer)
+ (save-excursion
+ (set-buffer pmail-buffer)
+ (pmail-show-message msg-num t))))))
+ (pmail-summary-update-highlight nil)))))
+\f
+(defun pmail-summary-mouse-goto-message (event)
+ "Select the message whose summary line you click on."
+ (interactive "@e")
+ (goto-char (posn-point (event-end event)))
+ (setq pmail-current-message (pmail-summary-get-message-at-point))
+ (pmail-summary-pmail-update))
+
+(defun pmail-summary-get-message-at-point ()
+ "Return the message number corresponding to the line containing point.
+If the summary buffer contains no messages, nil is returned."
+ (save-excursion
+ ;; Position point at the beginning of a line.
+ (if (eobp)
+ (forward-line -1)
+ (forward-line 0))
+ ;; Parse the message number.
+ (string-to-number
+ (buffer-substring (point) (min (point-max) (+ 6 (point)))))))
+
+(defun pmail-summary-goto-msg (&optional n nowarn skip-pmail)
+ "Go to message N in the summary buffer and the Pmail buffer.
+If N is nil, use the message corresponding to point in the summary
+buffer and move to that message in the Pmail buffer.
+
+If NOWARN, don't say anything if N is out of range.
+If SKIP-PMAIL, don't do anything to the Pmail buffer."
+ (interactive "P")
+ (if (consp n) (setq n (prefix-numeric-value n)))
+ ;; Do the end of buffer adjustment.
+ (if (eobp) (forward-line -1))
+ (beginning-of-line)
+ ;; Set N to the current message unless it was already set by the
+ ;; caller.
+ (unless n (setq n (pmail-summary-get-message-at-point)))
+ (let* ((obuf (current-buffer))
+ (buf pmail-buffer)
+ (cur (point))
+ message-not-found
+ (curmsg (string-to-number
+ (buffer-substring (point)
+ (min (point-max) (+ 6 (point))))))
+ (total (with-current-buffer buf
+ pmail-total-messages)))
+ ;; Do a validity check on N. If it is valid then set the current
+ ;; summary message to N. `pmail-summary-pmail-update' will then
+ ;; actually move point to the selected message.
+ (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))
+ (pmail-summary-goto-msg nil nowarn skip-pmail)))
+ (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 " "))))
+ (pmail-summary-update-highlight message-not-found)
+ (beginning-of-line)
+ ;; Determine if the Pmail buffer needs to be processed.
+ (if skip-pmail
+ nil
+ ;; It does.
+ (let ((selwin (selected-window)))
+ (unwind-protect
+ (progn (pop-to-buffer buf)
+ (pmail-show-message n))
+ (select-window selwin)
+ ;; The actions above can alter the current buffer. Preserve it.
+ (set-buffer obuf))))))
+
+;; Update the highlighted line in an pmail summary buffer.
+;; That should be current. We highlight the line point is on.
+;; If NOT-FOUND is non-nil, we turn off highlighting.
+(defun pmail-summary-update-highlight (not-found)
+ ;; Make sure we have an overlay to use.
+ (or pmail-summary-overlay
+ (progn
+ (make-local-variable 'pmail-summary-overlay)
+ (setq pmail-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 pmail-summary-overlay 'face nil)
+ (move-overlay pmail-summary-overlay
+ (save-excursion (beginning-of-line)
+ (skip-chars-forward " ")
+ (point))
+ (save-excursion (end-of-line) (point)))
+ (overlay-put pmail-summary-overlay 'face 'highlight)))
+\f
+(defun pmail-summary-scroll-msg-up (&optional dist)
+ "Scroll the Pmail window forward.
+If the Pmail window is displaying the end of a message,
+advance to the next message."
+ (interactive "P")
+ (if (eq dist '-)
+ (pmail-summary-scroll-msg-down nil)
+ (let ((pmail-buffer-window (get-buffer-window pmail-view-buffer)))
+ (if pmail-buffer-window
+ (if (let ((pmail-summary-window (selected-window)))
+ (select-window pmail-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 pmail-summary-window)))
+ (if (not pmail-summary-scroll-between-messages)
+ (error "End of buffer")
+ (pmail-summary-next-msg (or dist 1)))
+ (let ((other-window-scroll-buffer pmail-view-buffer))
+ (scroll-other-window dist)))
+ ;; If it isn't visible at all, show the beginning.
+ (pmail-summary-beginning-of-message)))))
+
+(defun pmail-summary-scroll-msg-down (&optional dist)
+ "Scroll the Pmail window backward.
+If the Pmail window is now displaying the beginning of a message,
+move to the previous message."
+ (interactive "P")
+ (if (eq dist '-)
+ (pmail-summary-scroll-msg-up nil)
+ (let ((pmail-buffer-window (get-buffer-window pmail-view-buffer)))
+ (if pmail-buffer-window
+ (if (let ((pmail-summary-window (selected-window)))
+ (select-window pmail-buffer-window)
+ (prog1
+ ;; Is BOB visible in the buffer?
+ (save-excursion
+ (move-to-window-line 0)
+ (beginning-of-line)
+ (bobp))
+ (select-window pmail-summary-window)))
+ (if (not pmail-summary-scroll-between-messages)
+ (error "Beginning of buffer")
+ (pmail-summary-previous-msg (or dist 1)))
+ (let ((other-window-scroll-buffer pmail-view-buffer))
+ (scroll-other-window-down dist)))
+ ;; If it isn't visible at all, show the beginning.
+ (pmail-summary-beginning-of-message)))))
+
+(defun pmail-summary-beginning-of-message ()
+ "Show current message from the beginning."
+ (interactive)
+ (pmail-summary-show-message 'BEG))
+
+(defun pmail-summary-end-of-message ()
+ "Show bottom of current message."
+ (interactive)
+ (pmail-summary-show-message 'END))
+
+(defun pmail-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 pmail-view-buffer))
+ (split-window (selected-window) pmail-summary-window-size)
+ (select-window (frame-first-window))
+ (pop-to-buffer pmail-view-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 pmail-view-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 pmail-summary-buffer))
+
+(defun pmail-summary-bury ()
+ "Bury the Pmail buffer and the Pmail summary buffer."
+ (interactive)
+ (let ((buffer-to-bury (current-buffer)))
+ (let (window)
+ (while (setq window (get-buffer-window pmail-buffer))
+ (set-window-buffer window (other-buffer pmail-buffer)))
+ (bury-buffer pmail-buffer))
+ (switch-to-buffer (other-buffer buffer-to-bury))
+ (bury-buffer buffer-to-bury)))
+
+(defun pmail-summary-quit ()
+ "Quit out of Pmail and Pmail summary."
+ (interactive)
+ (pmail-summary-wipe)
+ (pmail-quit))
+
+(defun pmail-summary-wipe ()
+ "Kill and wipe away Pmail summary, remaining within Pmail."
+ (interactive)
+ (save-excursion (set-buffer pmail-buffer) (setq pmail-summary-buffer nil))
+ (let ((local-pmail-buffer pmail-view-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 pmail buffer, or switch to it in this window.
+ (pop-to-buffer local-pmail-buffer)))
+
+(defun pmail-summary-expunge ()
+ "Actually erase all deleted messages and recompute summary headers."
+ (interactive)
+ (set-buffer pmail-buffer)
+ (pmail-expunge)
+ (set-buffer pmail-summary-buffer))
+
+(defun pmail-summary-expunge-and-save ()
+ "Expunge and save PMAIL file."
+ (interactive)
+ (set-buffer pmail-buffer)
+ (pmail-expunge)
+ (save-buffer)
+ (set-buffer pmail-summary-buffer)
+ (set-buffer-modified-p nil))
+
+(defun pmail-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 (current-message new-mail)
+ (with-current-buffer pmail-buffer
+ (setq new-mail (pmail-get-new-mail file-name)
+ current-message pmail-current-message))
+ ;; If new mail was found, display of the correct message was
+ ;; done elsewhere.
+ (unless new-mail
+ (pmail-summary-goto-msg current-message nil t))))
+
+(defun pmail-summary-input (filename)
+ "Run Pmail on file FILENAME."
+ (interactive "FRun pmail on PMAIL file: ")
+ ;; We switch windows here, then display the other Pmail file there.
+ (pop-to-buffer pmail-buffer)
+ (pmail filename))
+
+(defun pmail-summary-first-message ()
+ "Show first message in Pmail file from summary buffer."
+ (interactive)
+ (with-no-warnings
+ (beginning-of-buffer)))
+
+(defun pmail-summary-last-message ()
+ "Show last message in Pmail file from summary buffer."
+ (interactive)
+ (with-no-warnings
+ (end-of-buffer))
+ (forward-line -1))
+
+(declare-function pmail-abort-edit "pmailedit" ())
+(declare-function pmail-cease-edit "pmailedit"())
+(declare-function pmail-set-label "pmailkwd" (l state &optional n))
+(declare-function pmail-output-read-file-name "pmailout" ())
+(declare-function pmail-output-read-pmail-file-name "pmailout" ())
+(declare-function mail-send-and-exit "sendmail" (&optional arg))
+
+(defvar pmail-summary-edit-map
+ (let ((map (nconc (make-sparse-keymap) text-mode-map)))
+ (define-key map "\C-c\C-c" 'pmail-cease-edit)
+ (define-key map "\C-c\C-]" 'pmail-abort-edit)
+ map)
+ "Mode map to use when editing the pmail summary.")
+
+(defun pmail-summary-edit-current-message ()
+ "Edit the contents of this message."
+ (interactive)
+ (pop-to-buffer pmail-buffer)
+ (pmail-edit-current-message)
+ (use-local-map pmail-summary-edit-map))
+
+(defun pmail-summary-cease-edit ()
+ "Finish editing message, then go back to Pmail summary buffer."
+ (interactive)
+ (pmail-cease-edit)
+ (pop-to-buffer pmail-summary-buffer))
+
+(defun pmail-summary-abort-edit ()
+ "Abort edit of current message; restore original contents.
+Go back to summary buffer."
+ (interactive)
+ (pmail-abort-edit)
+ (pop-to-buffer pmail-summary-buffer))
+
+(defun pmail-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 " "") "Pmail search (regexp"))
+ regexp)
+ (setq prompt
+ (concat prompt
+ (if pmail-search-last-regexp
+ (concat ", default "
+ pmail-search-last-regexp "): ")
+ "): ")))
+ (setq regexp (read-string prompt))
+ (cond ((not (equal regexp ""))
+ (setq pmail-search-last-regexp regexp))
+ ((not pmail-search-last-regexp)
+ (error "No previous Pmail search string")))
+ (list pmail-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 pmail-buffer)
+ (pmail-search regexp (- n)))
+ (set-buffer buffer))))
+
+(defun pmail-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 " "") "Pmail search (regexp"))
+ regexp)
+ (setq prompt
+ (concat prompt
+ (if pmail-search-last-regexp
+ (concat ", default "
+ pmail-search-last-regexp "): ")
+ "): ")))
+ (setq regexp (read-string prompt))
+ (cond ((not (equal regexp ""))
+ (setq pmail-search-last-regexp regexp))
+ ((not pmail-search-last-regexp)
+ (error "No previous Pmail search string")))
+ (list pmail-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 pmail-buffer)
+ (pmail-search regexp n))
+ (set-buffer buffer))))
+
+(defun pmail-summary-toggle-header ()
+ "Show original message header if pruned header currently shown, or vice versa."
+ (interactive)
+ (with-current-buffer pmail-buffer
+ (pmail-toggle-header)))
+
+(defun pmail-summary-add-label (label)
+ "Add LABEL to labels associated with current Pmail message.
+Completion is performed over known labels when reading."
+ (interactive (list (with-current-buffer pmail-buffer
+ (pmail-read-label "Add label"))))
+ (with-current-buffer pmail-buffer
+ (pmail-add-label label)))
+
+(defun pmail-summary-kill-label (label)
+ "Remove LABEL from labels associated with current Pmail message.
+Completion is performed over known labels when reading."
+ (interactive (list (with-current-buffer pmail-buffer
+ (pmail-read-label "Kill label" t))))
+ (with-current-buffer pmail-buffer
+ (pmail-kill-label label)))
+\f
+;;;; *** Pmail Summary Mailing Commands ***
+
+(defun pmail-summary-override-mail-send-and-exit ()
+ "Replace bindings to 'mail-send-and-exit with 'pmail-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 'pmail-summary-send-and-exit)))
+
+(defun pmail-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 pmail-buffer)))
+ (if window
+ (select-window window)
+ (set-buffer pmail-buffer)))
+ (pmail-start-mail nil nil nil nil nil (current-buffer))
+ (pmail-summary-override-mail-send-and-exit))
+
+(defun pmail-summary-continue ()
+ "Continue composing outgoing message previously being composed."
+ (interactive)
+ (let ((window (get-buffer-window pmail-buffer)))
+ (if window
+ (select-window window)
+ (set-buffer pmail-buffer)))
+ (pmail-start-mail t))
+
+(defun pmail-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 pmail-view-buffer)))
+ (if window
+ (select-window window)
+ (set-buffer pmail-view-buffer)))
+ (pmail-reply just-sender)
+ (pmail-summary-override-mail-send-and-exit))
+
+(defun pmail-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 pmail-buffer)))
+ (if window
+ (select-window window)
+ (set-buffer pmail-buffer)))
+ (pmail-retry-failure)
+ (pmail-summary-override-mail-send-and-exit))
+
+(defun pmail-summary-send-and-exit ()
+ "Send mail reply and return to summary buffer."
+ (interactive)
+ (mail-send-and-exit t))
+
+(defun pmail-summary-forward (resend)
+ "Forward the current message to another user.
+With prefix argument, \"resend\" the message instead of forwarding it;
+see the documentation of `pmail-resend'."
+ (interactive "P")
+ (save-excursion
+ (let ((window (get-buffer-window pmail-buffer)))
+ (if window
+ (select-window window)
+ (set-buffer pmail-buffer)))
+ (pmail-forward resend)
+ (pmail-summary-override-mail-send-and-exit)))
+
+(defun pmail-summary-resend ()
+ "Resend current message using `pmail-resend'."
+ (interactive)
+ (save-excursion
+ (let ((window (get-buffer-window pmail-buffer)))
+ (if window
+ (select-window window)
+ (set-buffer pmail-buffer)))
+ (call-interactively 'pmail-resend)))
+\f
+;;;; Summary output commands.
+
+(defun pmail-summary-output-to-pmail-file (&optional file-name n)
+ "Append the current message to an Pmail file named FILE-NAME.
+If the file does not exist, ask if it should be created.
+If file is being visited, the message is appended to the Emacs
+buffer visiting that file.
+
+A prefix argument N says to output N consecutive messages
+starting with the current one. Deleted messages are skipped and don't count."
+ (interactive
+ (progn (require 'pmailout)
+ (list (pmail-output-read-file-name)
+ (prefix-numeric-value current-prefix-arg))))
+ (let ((i 0) prev-msg)
+ (while
+ (and (< i n)
+ (progn (pmail-summary-goto-msg)
+ (not (eq prev-msg
+ (setq prev-msg
+ (with-current-buffer pmail-buffer
+ pmail-current-message))))))
+ (setq i (1+ i))
+ (with-current-buffer pmail-buffer
+ (let ((pmail-delete-after-output nil))
+ (pmail-output-to-pmail-file file-name 1)))
+ (if pmail-delete-after-output
+ (pmail-summary-delete-forward nil)
+ (if (< i n)
+ (pmail-summary-next-msg 1))))))
+
+(defun pmail-summary-output (&optional file-name n)
+ "Append this message to Unix mail file named FILE-NAME.
+
+A prefix argument N says to output N consecutive messages
+starting with the current one. Deleted messages are skipped and don't count."
+ (interactive
+ (progn (require 'pmailout)
+ (list (pmail-output-read-file-name)
+ (prefix-numeric-value current-prefix-arg))))
+ (let ((i 0) prev-msg)
+ (while
+ (and (< i n)
+ (progn (pmail-summary-goto-msg)
+ (not (eq prev-msg
+ (setq prev-msg
+ (with-current-buffer pmail-buffer
+ pmail-current-message))))))
+ (setq i (1+ i))
+ (with-current-buffer pmail-buffer
+ (let ((pmail-delete-after-output nil))
+ (pmail-output file-name 1)))
+ (if pmail-delete-after-output
+ (pmail-summary-delete-forward nil)
+ (if (< i n)
+ (pmail-summary-next-msg 1))))))
+
+(defun pmail-summary-output-menu ()
+ "Output current message to another Pmail file, chosen with a menu.
+Also set the default for subsequent \\[pmail-output-to-pmail-file] commands.
+The variables `pmail-secondary-file-directory' and
+`pmail-secondary-file-regexp' control which files are offered in the menu."
+ (interactive)
+ (save-excursion
+ (set-buffer pmail-buffer)
+ (let ((pmail-delete-after-output nil))
+ (call-interactively 'pmail-output-menu)))
+ (if pmail-delete-after-output
+ (pmail-summary-delete-forward nil)))
+
+(defun pmail-summary-construct-io-menu ()
+ (let ((files (pmail-find-all-files pmail-secondary-file-directory)))
+ (if files
+ (progn
+ (define-key pmail-summary-mode-map [menu-bar classify input-menu]
+ (cons "Input Pmail File"
+ (pmail-list-to-menu "Input Pmail File"
+ files
+ 'pmail-summary-input)))
+ (define-key pmail-summary-mode-map [menu-bar classify output-menu]
+ (cons "Output Pmail File"
+ (pmail-list-to-menu "Output Pmail File"
+ files
+ 'pmail-summary-output-to-pmail-file))))
+ (define-key pmail-summary-mode-map [menu-bar classify input-menu]
+ '("Input Pmail File" . pmail-disable-menu))
+ (define-key pmail-summary-mode-map [menu-bar classify output-menu]
+ '("Output Pmail File" . pmail-disable-menu)))))
+
+(defun pmail-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 pmail-buffer)
+ (let ((pmail-delete-after-output nil))
+ (if file-name
+ (pmail-output-body-to-file file-name)
+ (call-interactively 'pmail-output-body-to-file))))
+ (if pmail-delete-after-output
+ (pmail-summary-delete-forward nil)))
+\f
+;; Sorting messages in Pmail Summary buffer.
+
+(defun pmail-summary-sort-by-date (reverse)
+ "Sort messages of current Pmail summary by date.
+If prefix argument REVERSE is non-nil, sort them in reverse order."
+ (interactive "P")
+ (pmail-sort-from-summary (function pmail-sort-by-date) reverse))
+
+(defun pmail-summary-sort-by-subject (reverse)
+ "Sort messages of current Pmail summary by subject.
+If prefix argument REVERSE is non-nil, sort them in reverse order."
+ (interactive "P")
+ (pmail-sort-from-summary (function pmail-sort-by-subject) reverse))
+
+(defun pmail-summary-sort-by-author (reverse)
+ "Sort messages of current Pmail summary by author.
+If prefix argument REVERSE is non-nil, sort them in reverse order."
+ (interactive "P")
+ (pmail-sort-from-summary (function pmail-sort-by-author) reverse))
+
+(defun pmail-summary-sort-by-recipient (reverse)
+ "Sort messages of current Pmail summary by recipient.
+If prefix argument REVERSE is non-nil, sort them in reverse order."
+ (interactive "P")
+ (pmail-sort-from-summary (function pmail-sort-by-recipient) reverse))
+
+(defun pmail-summary-sort-by-correspondent (reverse)
+ "Sort messages of current Pmail summary by other correspondent.
+If prefix argument REVERSE is non-nil, sort them in reverse order."
+ (interactive "P")
+ (pmail-sort-from-summary (function pmail-sort-by-correspondent) reverse))
+
+(defun pmail-summary-sort-by-lines (reverse)
+ "Sort messages of current Pmail summary by lines of the message.
+If prefix argument REVERSE is non-nil, sort them in reverse order."
+ (interactive "P")
+ (pmail-sort-from-summary (function pmail-sort-by-lines) reverse))
+
+(defun pmail-summary-sort-by-labels (reverse labels)
+ "Sort messages of current Pmail 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: ")
+ (pmail-sort-from-summary
+ (function (lambda (reverse)
+ (pmail-sort-by-labels reverse labels)))
+ reverse))
+
+(defun pmail-sort-from-summary (sortfun reverse)
+ "Sort Pmail messages from Summary buffer and update it after sorting."
+ (require 'pmailsort)
+ (let ((selwin (selected-window)))
+ (unwind-protect
+ (progn (pop-to-buffer pmail-buffer)
+ (funcall sortfun reverse))
+ (select-window selwin))))
+
+(defun pmail-summary-get-sender (n)
+ "Return the sender for message N.
+If sender matches `pmail-user-mail-address-regexp' or
+`user-mail-address', return the to-address instead."
+ (let ((sender (pmail-desc-get-sender n)))
+ (if (or (null sender)
+ (and pmail-user-mail-address-regexp
+ (string-match pmail-user-mail-address-regexp sender)))
+ ;; Either no sender known, or it's this user.
+ (save-restriction
+ (narrow-to-region (pmail-desc-get-start n)
+ (pmail-desc-get-end n))
+ (concat "to: " (mail-strip-quoted-names
+ (pmail-header-get-header "to"))))
+ sender)))
+
+(defun pmail-summary-get-line-count (n)
+ "Return a string containing the number of lines in message N.
+If `pmail-summary-line-count-flag' is nil, return the empty string."
+ (if pmail-summary-line-count-flag
+ (let ((lines (pmail-desc-get-line-count n)))
+ (format (cond ((<= lines 9) " [%d]")
+ ((<= lines 99) " [%d]")
+ ((<= lines 999) " [%3d]")
+ (t "[%d]"))
+ lines))
+ ""))
+
+(defun pmail-summary-get-summary-attributes (n)
+ "Return the attribute character codes for message N.
+`-' means an unseen message, `D' means marked for deletion."
+ (format "%s%s%s%s%s"
+ (cond ((pmail-desc-attr-p pmail-desc-unseen-index n) "-")
+ ((pmail-desc-attr-p pmail-desc-deleted-index n) "D")
+ (t " "))
+ (or (pmail-desc-get-attr-code pmail-desc-answered-index n) " ")
+ (or (pmail-desc-get-attr-code pmail-desc-filed-index n) " ")
+ (or (pmail-desc-get-attr-code pmail-desc-edited-index n) " ")
+ (or (pmail-desc-get-attr-code pmail-desc-stored-index n) " ")))
+
+(defun pmail-summary-get-summary-line (n)
+ "Return a summary line for message N."
+ (let (keywords str subj)
+ (dolist (keyword (pmail-desc-get-keywords n))
+ (when (and (pmail-keyword-p keyword)
+ (not (pmail-attribute-p keyword)))
+ (setq keywords (cons keyword keywords))))
+ (setq keywords (nreverse keywords)
+ str (if keywords
+ (concat "{ " (mapconcat 'identity keywords " ") " } ")
+ "")
+ subj (replace-regexp-in-string "\\s-+" " "
+ (pmail-desc-get-subject n)))
+ (funcall pmail-summary-line-decoder
+ (format "%5s%s%6s %25.25s%s %s\n"
+ n
+ (pmail-summary-get-summary-attributes n)
+ (concat (pmail-desc-get-day-number n) "-"
+ (pmail-desc-get-month n))
+ (pmail-summary-get-sender n)
+ (pmail-summary-get-line-count n)
+ (concat str subj)))))
+
+(defun pmail-summary-update (n)
+ "Rewrite the summary line for message N."
+ (with-current-buffer pmail-buffer
+ ;; we need to do this in the pmail-buffer lest the keywords are
+ ;; not recognized
+ (let ((summary (pmail-summary-get-summary-line n)))
+ (with-current-buffer pmail-summary-buffer
+ (save-excursion
+ (let ((buffer-read-only nil))
+ (pmail-summary-goto-msg n)
+ ;; summary line includes newline at the end
+ (delete-region (point) (1+ (line-end-position)))
+ (insert summary)))))))
+
+(provide 'pmailsum)
+
+;; arch-tag: 556079ee-75c1-47f5-9884-2e0a0bc6c5a1
+;;; pmailsum.el ends here
--- /dev/null
+;; -*- no-byte-compile: t -*-
+;; In load-path, after this directory should come
+;; certain of its subdirectories. Here we specify them.
+(normal-top-level-add-to-load-path '("mbox-trunk-annotations" "mbox-changes" ))
+;; Local Variables:
+;; version-control: never
+;; no-byte-compile: t
+;; End: