Lennart Borgman <lennart.borgman at gmail.com>
[emacs.git] / lisp / mail / pmail.el
blob25fc614053dda083c877fdf165abe93b87104bb2
1 ;;; pmail.el --- main code of "PMAIL" mail reader for Emacs
3 ;; Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1996, 1997, 1998,
4 ;; 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 ;; Free Software Foundation, Inc.
7 ;; Maintainer: FSF
8 ;; Keywords: mail
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;;; Code:
29 ;; Souped up by shane@mit-ajax based on ideas of rlk@athena.mit.edu
30 ;; New features include attribute and keyword support, message
31 ;; selection by dispatch table, summary by attributes and keywords,
32 ;; expunging by dispatch table, sticky options for file commands.
34 ;; Extended by Bob Weiner of Motorola
35 ;; New features include: pmail and pmail-summary buffers remain
36 ;; synchronized and key bindings basically operate the same way in both
37 ;; buffers, summary by topic or by regular expression, pmail-reply-prefix
38 ;; variable, and a bury pmail buffer (wipe) command.
41 (require 'mail-utils)
42 (eval-when-compile (require 'mule-util)) ; for detect-coding-with-priority
44 (defconst pmail-attribute-header "X-RMAIL-ATTRIBUTES"
45 "The header that stores the Pmail attribute data.")
47 (defconst pmail-keyword-header "X-RMAIL-KEYWORDS"
48 "The header that stores the Pmail keyword data.")
50 ;;; Attribute indexes
52 (defconst pmail-answered-attr-index 0
53 "The index for the `answered' attribute.")
55 (defconst pmail-deleted-attr-index 1
56 "The index for the `deleted' attribute.")
58 (defconst pmail-edited-attr-index 2
59 "The index for the `edited' attribute.")
61 (defconst pmail-filed-attr-index 3
62 "The index for the `filed' attribute.")
64 (defconst pmail-resent-attr-index 4
65 "The index for the `resent' attribute.")
67 (defconst pmail-stored-attr-index 5
68 "The index for the `stored' attribute.")
70 (defconst pmail-unseen-attr-index 6
71 "The index for the `unseen' attribute.")
73 (defconst pmail-attr-array
74 '[(?A "answered")
75 (?D "deleted")
76 (?E "edited")
77 (?F "filed")
78 (?R "replied")
79 (?S "stored")
80 (?U "unseen")]
81 "An array that provides a mapping between an attribute index,
82 it's character representation and it's display representation.")
84 (defvar deleted-head)
85 (defvar font-lock-fontified)
86 (defvar mail-abbrev-syntax-table)
87 (defvar mail-abbrevs)
88 (defvar messages-head)
89 (defvar pmail-use-spam-filter)
90 (defvar rsf-beep)
91 (defvar rsf-sleep-after-message)
92 (defvar total-messages)
93 (defvar tool-bar-map)
95 (defvar pmail-buffers-swapped-p nil
96 "A flag that is non-nil when the message view buffer and the
97 message collection buffer are swapped, i.e. the Pmail buffer
98 contains a single decoded message.")
100 (defvar pmail-header-style 'normal
101 "The current header display style choice, one of
102 'normal (selected headers) or 'full (all headers).")
104 ; These variables now declared in paths.el.
105 ;(defvar pmail-spool-directory "/usr/spool/mail/"
106 ; "This is the name of the directory used by the system mailer for\n\
107 ;delivering new mail. Its name should end with a slash.")
108 ;(defvar pmail-file-name
109 ; (expand-file-name "~/PMAIL")
110 ; "")
112 ;; Temporary support for mbox.
113 (defcustom pmail-file-name "~/PMAIL"
114 "*Name of user's primary mail file."
115 :type 'string
116 :group 'rmail
117 :version "21.1")
119 (defgroup pmail nil
120 "Mail reader for Emacs."
121 :group 'mail)
123 (defgroup pmail-retrieve nil
124 "Pmail retrieval options."
125 :prefix "pmail-"
126 :group 'pmail)
128 (defgroup pmail-files nil
129 "Pmail files."
130 :prefix "pmail-"
131 :group 'pmail)
133 (defgroup pmail-headers nil
134 "Pmail header options."
135 :prefix "pmail-"
136 :group 'pmail)
138 (defgroup pmail-reply nil
139 "Pmail reply options."
140 :prefix "pmail-"
141 :group 'pmail)
143 (defgroup pmail-summary nil
144 "Pmail summary options."
145 :prefix "pmail-"
146 :prefix "pmail-summary-"
147 :group 'pmail)
149 (defgroup pmail-output nil
150 "Output message to a file."
151 :prefix "pmail-output-"
152 :prefix "pmail-"
153 :group 'pmail)
155 (defgroup pmail-edit nil
156 "Pmail editing."
157 :prefix "pmail-edit-"
158 :group 'pmail)
160 (defgroup pmail-obsolete nil
161 "Pmail obsolete customization variables."
162 :group 'pmail)
164 (defcustom pmail-movemail-program nil
165 "If non-nil, the file name of the `movemail' program."
166 :group 'pmail-retrieve
167 :type '(choice (const nil) string))
169 (defcustom pmail-pop-password nil
170 "*Password to use when reading mail from POP server.
171 Please use `pmail-remote-password' instead."
172 :type '(choice (string :tag "Password")
173 (const :tag "Not Required" nil))
174 :group 'pmail-obsolete)
176 (defcustom pmail-pop-password-required nil
177 "*Non-nil if a password is required when reading mail from a POP server.
178 Please use pmail-remote-password-required instead."
179 :type 'boolean
180 :group 'pmail-obsolete)
182 (defcustom pmail-remote-password nil
183 "*Password to use when reading mail from a remote server.
184 This setting is ignored for mailboxes whose URL already contains a password."
185 :type '(choice (string :tag "Password")
186 (const :tag "Not Required" nil))
187 :set-after '(pmail-pop-password)
188 :set #'(lambda (symbol value)
189 (set-default symbol
190 (if (and (not value)
191 (boundp 'pmail-pop-password)
192 pmail-pop-password)
193 pmail-pop-password
194 value))
195 (setq pmail-pop-password nil))
196 :group 'pmail-retrieve
197 :version "22.1")
199 (defcustom pmail-remote-password-required nil
200 "*Non-nil if a password is required when reading mail from a remote server."
201 :type 'boolean
202 :set-after '(pmail-pop-password-required)
203 :set #'(lambda (symbol value)
204 (set-default symbol
205 (if (and (not value)
206 (boundp 'pmail-pop-password-required)
207 pmail-pop-password-required)
208 pmail-pop-password-required
209 value))
210 (setq pmail-pop-password-required nil))
211 :group 'pmail-retrieve
212 :version "22.1")
214 (defcustom pmail-movemail-flags nil
215 "*List of flags to pass to movemail.
216 Most commonly used to specify `-g' to enable GSS-API authentication
217 or `-k' to enable Kerberos authentication."
218 :type '(repeat string)
219 :group 'pmail-retrieve
220 :version "20.3")
222 (defvar pmail-remote-password-error "invalid usercode or password\\|
223 unknown user name or bad password\\|Authentication failed\\|MU_ERR_AUTH_FAILURE"
224 "Regular expression matching incorrect-password POP or IMAP server error
225 messages.
226 If you get an incorrect-password error that this expression does not match,
227 please report it with \\[report-emacs-bug].")
229 (defvar pmail-encoded-remote-password nil)
231 (defcustom pmail-preserve-inbox nil
232 "*Non-nil means leave incoming mail in the user's inbox--don't delete it."
233 :type 'boolean
234 :group 'pmail-retrieve)
236 (defcustom pmail-movemail-search-path nil
237 "*List of directories to search for movemail (in addition to `exec-path')."
238 :group 'pmail-retrieve
239 :type '(repeat (directory)))
241 (declare-function mail-position-on-field "sendmail" (field &optional soft))
242 (declare-function mail-text-start "sendmail" ())
243 (declare-function pmail-dont-reply-to "mail-utils" (destinations))
244 (declare-function pmail-update-summary "pmailsum" (&rest ignore))
246 (defun pmail-probe (prog)
247 "Determine what flavor of movemail PROG is.
248 We do this by executing it with `--version' and analyzing its output."
249 (with-temp-buffer
250 (let ((tbuf (current-buffer)))
251 (buffer-disable-undo tbuf)
252 (call-process prog nil tbuf nil "--version")
253 (if (not (buffer-modified-p tbuf))
254 ;; Should not happen...
256 (goto-char (point-min))
257 (cond
258 ((looking-at ".*movemail: invalid option")
259 'emacs) ;; Possibly...
260 ((looking-at "movemail (GNU Mailutils .*)")
261 'mailutils)
263 ;; FIXME:
264 'emacs))))))
266 (defun pmail-autodetect ()
267 "Determine the file name of the `movemail' program and return its flavor.
268 If `pmail-movemail-program' is non-nil, use it.
269 Otherwise, look for `movemail' in the directories in
270 `pmail-movemail-search-path', those in `exec-path', and `exec-directory'."
271 (if pmail-movemail-program
272 (pmail-probe pmail-movemail-program)
273 (catch 'scan
274 (dolist (dir (append pmail-movemail-search-path exec-path
275 (list exec-directory)))
276 (when (and dir (file-accessible-directory-p dir))
277 ;; Previously, this didn't have to work on Windows, because
278 ;; pmail-insert-inbox-text before r1.439 fell back to using
279 ;; (expand-file-name "movemail" exec-directory) and just
280 ;; assuming it would work.
281 ;; http://lists.gnu.org/archive/html/bug-gnu-emacs/2008-02/msg00087.html
282 (let ((progname (expand-file-name
283 (concat "movemail"
284 (if (memq system-type '(ms-dos windows-nt))
285 ".exe")) dir)))
286 (when (and (not (file-directory-p progname))
287 (file-executable-p progname))
288 (let ((x (pmail-probe progname)))
289 (when x
290 (setq pmail-movemail-program progname)
291 (throw 'scan x))))))))))
293 (defvar pmail-movemail-variant-in-use nil
294 "The movemail variant currently in use. Known variants are:
296 `emacs' Means any implementation, compatible with the native Emacs one.
297 This is the default;
298 `mailutils' Means GNU mailutils implementation, capable of handling full
299 mail URLs as the source mailbox.")
301 ;;;###autoload
302 (defun pmail-movemail-variant-p (&rest variants)
303 "Return t if the current movemail variant is any of VARIANTS.
304 Currently known variants are 'emacs and 'mailutils."
305 (when (not pmail-movemail-variant-in-use)
306 ;; Autodetect
307 (setq pmail-movemail-variant-in-use (pmail-autodetect)))
308 (not (null (member pmail-movemail-variant-in-use variants))))
310 ;; Call for effect, to set pmail-movemail-program (if not set by the
311 ;; user), and pmail-movemail-variant-in-use. Used by various functions.
312 ;; I'm not sure if M-x pmail is the only entry point to this package.
313 ;; If so, this can be moved there.
314 (pmail-movemail-variant-p)
316 ;;;###autoload
317 (defcustom pmail-dont-reply-to-names nil "\
318 *A regexp specifying addresses to prune from a reply message.
319 A value of nil means exclude your own email address as an address
320 plus whatever is specified by `pmail-default-dont-reply-to-names'."
321 :type '(choice regexp (const :tag "Your Name" nil))
322 :group 'pmail-reply)
324 ;;;###autoload
325 (defvar pmail-default-dont-reply-to-names "\\`info-" "\
326 A regular expression specifying part of the default value of the
327 variable `pmail-dont-reply-to-names', for when the user does not set
328 `pmail-dont-reply-to-names' explicitly. (The other part of the default
329 value is the user's email address and name.)
330 It is useful to set this variable in the site customization file.")
332 ;;;###autoload
333 (defcustom pmail-ignored-headers
334 (concat "^via:\\|^mail-from:\\|^origin:\\|^references:\\|^sender:"
335 "\\|^status:\\|^received:\\|^x400-originator:\\|^x400-recipients:"
336 "\\|^x400-received:\\|^x400-mts-identifier:\\|^x400-content-type:"
337 "\\|^\\(resent-\\|\\)message-id:\\|^summary-line:\\|^resent-date:"
338 "\\|^nntp-posting-host:\\|^path:\\|^x-char.*:\\|^x-face:\\|^face:"
339 "\\|^x-mailer:\\|^delivered-to:\\|^lines:"
340 "\\|^content-transfer-encoding:\\|^x-coding-system:"
341 "\\|^return-path:\\|^errors-to:\\|^return-receipt-to:"
342 "\\|^precedence:\\|^list-help:\\|^list-post:\\|^list-subscribe:"
343 "\\|^list-id:\\|^list-unsubscribe:\\|^list-archive:"
344 "\\|^content-length:\\|^nntp-posting-date:\\|^user-agent"
345 "\\|^importance:\\|^envelope-to:\\|^delivery-date\\|^openpgp:"
346 "\\|^mbox-line:\\|^cancel-lock:\\|^DomainKey-Signature:"
347 "\\|^resent-face:\\|^resent-x.*:\\|^resent-organization:\\|^resent-openpgp:"
348 "\\|^x-.*:")
349 "*Regexp to match header fields that Pmail should normally hide.
350 \(See also `pmail-nonignored-headers', which overrides this regexp.)
351 This variable is used for reformatting the message header,
352 which normally happens once for each message,
353 when you view the message for the first time in Pmail.
354 To make a change in this variable take effect
355 for a message that you have already viewed,
356 go to that message and type \\[pmail-toggle-header] twice."
357 :type 'regexp
358 :group 'pmail-headers)
360 (defcustom pmail-nonignored-headers "^x-spam-status:"
361 "*Regexp to match X header fields that Pmail should show.
362 This regexp overrides `pmail-ignored-headers'; if both this regexp
363 and that one match a certain header field, Pmail shows the field.
364 If this is nil, ignore all header fields in `pmail-ignored-headers'.
366 This variable is used for reformatting the message header,
367 which normally happens once for each message,
368 when you view the message for the first time in Pmail.
369 To make a change in this variable take effect
370 for a message that you have already viewed,
371 go to that message and type \\[pmail-toggle-header] twice."
372 :type '(choice (const nil) (regexp))
373 :group 'pmail-headers)
375 ;;;###autoload
376 (defcustom pmail-displayed-headers nil
377 "*Regexp to match Header fields that Pmail should display.
378 If nil, display all header fields except those matched by
379 `pmail-ignored-headers'."
380 :type '(choice regexp (const :tag "All"))
381 :group 'pmail-headers)
383 ;;;###autoload
384 (defcustom pmail-retry-ignored-headers "^x-authentication-warning:" "\
385 *Headers that should be stripped when retrying a failed message."
386 :type '(choice regexp (const nil :tag "None"))
387 :group 'pmail-headers)
389 ;;;###autoload
390 (defcustom pmail-highlighted-headers "^From:\\|^Subject:" "\
391 *Regexp to match Header fields that Pmail should normally highlight.
392 A value of nil means don't highlight."
393 :type 'regexp
394 :group 'pmail-headers)
396 (defface pmail-highlight
397 '((t (:inherit highlight)))
398 "Face to use for highlighting the most important header fields."
399 :group 'pmail-headers
400 :version "22.1")
402 (defface pmail-header-name
403 '((t (:inherit font-lock-function-name-face)))
404 "Face to use for highlighting the header names."
405 :group 'pmail-headers
406 :version "23.1")
408 ;;;###autoload
409 (defcustom pmail-delete-after-output nil "\
410 *Non-nil means automatically delete a message that is copied to a file."
411 :type 'boolean
412 :group 'pmail-files)
414 ;;;###autoload
415 (defcustom pmail-primary-inbox-list nil "\
416 *List of files which are inboxes for user's primary mail file `~/PMAIL'.
417 nil means the default, which is (\"/usr/spool/mail/$USER\")
418 \(the name varies depending on the operating system,
419 and the value of the environment variable MAIL overrides it)."
420 ;; Don't use backquote here, because we don't want to need it
421 ;; at load time.
422 :type (list 'choice '(const :tag "Default" nil)
423 (list 'repeat ':value (list (or (getenv "MAIL")
424 (concat "/var/spool/mail/"
425 (getenv "USER"))))
426 'file))
427 :group 'pmail-retrieve
428 :group 'pmail-files)
430 ;;;###autoload
431 (defcustom pmail-mail-new-frame nil
432 "*Non-nil means Pmail makes a new frame for composing outgoing mail.
433 This is handy if you want to preserve the window configuration of
434 the frame where you have the PMAIL buffer displayed."
435 :type 'boolean
436 :group 'pmail-reply)
438 ;;;###autoload
439 (defcustom pmail-secondary-file-directory "~/"
440 "*Directory for additional secondary Pmail files."
441 :type 'directory
442 :group 'pmail-files)
443 ;;;###autoload
444 (defcustom pmail-secondary-file-regexp "\\.xmail$"
445 "*Regexp for which files are secondary Pmail files."
446 :type 'regexp
447 :group 'pmail-files)
449 ;;;###autoload
450 (defcustom pmail-confirm-expunge 'y-or-n-p
451 "*Whether and how to ask for confirmation before expunging deleted messages."
452 :type '(choice (const :tag "No confirmation" nil)
453 (const :tag "Confirm with y-or-n-p" y-or-n-p)
454 (const :tag "Confirm with yes-or-no-p" yes-or-no-p))
455 :version "21.1"
456 :group 'pmail-files)
458 ;;;###autoload
459 (defvar pmail-mode-hook nil
460 "List of functions to call when Pmail is invoked.")
462 ;;;###autoload
463 (defvar pmail-get-new-mail-hook nil
464 "List of functions to call when Pmail has retrieved new mail.")
466 ;;;###autoload
467 (defcustom pmail-show-message-hook nil
468 "List of functions to call when Pmail displays a message."
469 :type 'hook
470 :options '(goto-address)
471 :group 'pmail)
473 ;;;###autoload
474 (defvar pmail-quit-hook nil
475 "List of functions to call when quitting out of Pmail.")
477 ;;;###autoload
478 (defvar pmail-delete-message-hook nil
479 "List of functions to call when Pmail deletes a message.
480 When the hooks are called, the message has been marked deleted but is
481 still the current message in the Pmail buffer.")
483 ;; These may be altered by site-init.el to match the format of mmdf files
484 ;; delimiting used on a given host (delim1 and delim2 from the config
485 ;; files).
487 (defvar pmail-mmdf-delim1 "^\001\001\001\001\n"
488 "Regexp marking the start of an mmdf message.")
489 (defvar pmail-mmdf-delim2 "^\001\001\001\001\n"
490 "Regexp marking the end of an mmdf message.")
492 (defcustom pmail-message-filter nil
493 "If non-nil, a filter function for new messages in PMAIL.
494 Called with region narrowed to the message, including headers,
495 before obeying `pmail-ignored-headers'."
496 :group 'pmail-headers
497 :type '(choice (const nil) function))
499 (defcustom pmail-automatic-folder-directives nil
500 "List of directives specifying where to put a message.
501 Each element of the list is of the form:
503 (FOLDERNAME FIELD REGEXP [ FIELD REGEXP ] ... )
505 Where FOLDERNAME is the name of a BABYL format folder to put the
506 message. If any of the field regexp's are nil, then it is ignored.
508 If FOLDERNAME is \"/dev/null\", it is deleted.
509 If FOLDERNAME is nil then it is deleted, and skipped.
511 FIELD is the plain text name of a field in the message, such as
512 \"subject\" or \"from\". A FIELD of \"to\" will automatically include
513 all text from the \"cc\" field as well.
515 REGEXP is an expression to match in the preceeding specified FIELD.
516 FIELD/REGEXP pairs continue in the list.
518 examples:
519 (\"/dev/null\" \"from\" \"@spam.com\") ; delete all mail from spam.com
520 (\"RMS\" \"from\" \"rms@\") ; save all mail from RMS."
521 :group 'pmail
522 :version "21.1"
523 :type '(repeat (sexp :tag "Directive")))
525 (defvar pmail-reply-prefix "Re: "
526 "String to prepend to Subject line when replying to a message.")
528 ;; Some mailers use "Re(2):" or "Re^2:" or "Re: Re:" or "Re[2]:".
529 ;; This pattern should catch all the common variants.
530 ;; rms: I deleted the change to delete tags in square brackets
531 ;; because they mess up RT tags.
532 (defvar pmail-reply-regexp "\\`\\(Re\\(([0-9]+)\\|\\[[0-9]+\\]\\|\\^[0-9]+\\)?: *\\)*"
533 "Regexp to delete from Subject line before inserting `pmail-reply-prefix'.")
535 (defcustom pmail-display-summary nil
536 "*If non-nil, Pmail always displays the summary buffer."
537 :group 'pmail-summary
538 :type 'boolean)
540 (defvar pmail-inbox-list nil)
541 (put 'pmail-inbox-list 'permanent-local t)
543 (defvar pmail-keywords nil)
544 (put 'pmail-keywords 'permanent-local t)
546 (defvar pmail-buffer nil
547 "The PMAIL buffer related to the current buffer.
548 In an PMAIL buffer, this holds the PMAIL buffer itself.
549 In a summary buffer, this holds the PMAIL buffer it is a summary for.")
550 (put 'pmail-buffer 'permanent-local t)
552 ;; Message counters and markers. Deleted flags.
554 (defvar pmail-current-message nil)
555 (put 'pmail-current-message 'permanent-local t)
557 (defvar pmail-total-messages nil)
558 (put 'pmail-total-messages 'permanent-local t)
560 (defvar pmail-message-vector nil)
561 (put 'pmail-message-vector 'permanent-local t)
563 (defvar pmail-deleted-vector nil)
564 (put 'pmail-deleted-vector 'permanent-local t)
566 (defvar pmail-msgref-vector nil
567 "In an Pmail buffer, a vector whose Nth element is a list (N).
568 When expunging renumbers messages, these lists are modified
569 by substituting the new message number into the existing list.")
570 (put 'pmail-msgref-vector 'permanent-local t)
572 (defvar pmail-overlay-list nil)
573 (put 'pmail-overlay-list 'permanent-local t)
575 ;; These are used by autoloaded pmail-summary.
577 (defvar pmail-summary-buffer nil)
578 (put 'pmail-summary-buffer 'permanent-local t)
579 (defvar pmail-summary-vector nil)
580 (put 'pmail-summary-vector 'permanent-local t)
582 (defvar pmail-view-buffer nil
583 "Buffer which holds PMAIL message for MIME displaying.")
584 (put 'pmail-view-buffer 'permanent-local t)
586 ;; `Sticky' default variables.
588 ;; Last individual label specified to a or k.
589 (defvar pmail-last-label nil)
590 (put 'pmail-last-label 'permanent-local t)
592 ;; Last set of values specified to C-M-n, C-M-p, C-M-s or C-M-l.
593 (defvar pmail-last-multi-labels nil)
595 (defvar pmail-last-regexp nil)
596 (put 'pmail-last-regexp 'permanent-local t)
598 (defcustom pmail-default-file "~/xmail"
599 "*Default file name for \\[pmail-output]."
600 :type 'file
601 :group 'pmail-files)
602 (defcustom pmail-default-pmail-file "~/XMAIL"
603 "*Default file name for \\[pmail-output-to-pmail-file]."
604 :type 'file
605 :group 'pmail-files)
606 (defcustom pmail-default-body-file "~/mailout"
607 "*Default file name for \\[pmail-output-body-to-file]."
608 :type 'file
609 :group 'pmail-files
610 :version "20.3")
612 ;; Mule and MIME related variables.
614 ;;;###autoload
615 (defvar pmail-file-coding-system nil
616 "Coding system used in PMAIL file.
618 This is set to nil by default.")
620 ;;;###autoload
621 (defcustom pmail-enable-mime nil
622 "*If non-nil, PMAIL uses MIME feature.
623 If the value is t, PMAIL automatically shows MIME decoded message.
624 If the value is neither t nor nil, PMAIL does not show MIME decoded message
625 until a user explicitly requires it.
627 Even if the value is non-nil, you can't use MIME feature
628 if the feature specified by `pmail-mime-feature' is not available
629 in your session."
630 :type '(choice (const :tag "on" t)
631 (const :tag "off" nil)
632 (other :tag "when asked" ask))
633 :group 'pmail)
635 (defvar pmail-enable-mime-composing nil
636 "*If non-nil, PMAIL uses `pmail-insert-mime-forwarded-message-function' to forward.")
638 ;;;###autoload
639 (defvar pmail-show-mime-function nil
640 "Function to show MIME decoded message of PMAIL file.
641 This function is called when `pmail-enable-mime' is non-nil.
642 It is called with no argument.")
644 ;;;###autoload
645 (defvar pmail-insert-mime-forwarded-message-function nil
646 "Function to insert a message in MIME format so it can be forwarded.
647 This function is called if `pmail-enable-mime' or
648 `pmail-enable-mime-composing' is non-nil.
649 It is called with one argument FORWARD-BUFFER, which is a
650 buffer containing the message to forward. The current buffer
651 is the outgoing mail buffer.")
653 ;;;###autoload
654 (defvar pmail-insert-mime-resent-message-function nil
655 "Function to insert a message in MIME format so it can be resent.
656 This function is called if `pmail-enable-mime' is non-nil.
657 It is called with one argument FORWARD-BUFFER, which is a
658 buffer containing the message to forward. The current buffer
659 is the outgoing mail buffer.")
661 ;;;###autoload
662 (defvar pmail-search-mime-message-function nil
663 "Function to check if a regexp matches a MIME message.
664 This function is called if `pmail-enable-mime' is non-nil.
665 It is called with two arguments MSG and REGEXP, where
666 MSG is the message number, REGEXP is the regular expression.")
668 ;;;###autoload
669 (defvar pmail-search-mime-header-function nil
670 "Function to check if a regexp matches a header of MIME message.
671 This function is called if `pmail-enable-mime' is non-nil.
672 It is called with three arguments MSG, REGEXP, and LIMIT, where
673 MSG is the message number,
674 REGEXP is the regular expression,
675 LIMIT is the position specifying the end of header.")
677 ;;;###autoload
678 (defvar pmail-mime-feature 'pmail-mime
679 "Feature to require to load MIME support in Pmail.
680 When starting Pmail, if `pmail-enable-mime' is non-nil,
681 this feature is required with `require'.
683 The default value is `pmail-mime'. This feature is provided by
684 the pmail-mime package available at <http://www.m17n.org/pmail-mime/>.")
686 ;;;###autoload
687 (defvar pmail-decode-mime-charset t
688 "*Non-nil means a message is decoded by MIME's charset specification.
689 If this variable is nil, or the message has not MIME specification,
690 the message is decoded as normal way.
692 If the variable `pmail-enable-mime' is non-nil, this variables is
693 ignored, and all the decoding work is done by a feature specified by
694 the variable `pmail-mime-feature'.")
696 ;;;###autoload
697 (defvar pmail-mime-charset-pattern
698 (concat "^content-type:[ \t]*text/plain;"
699 "\\(?:[ \t\n]*\\(?:format\\|delsp\\)=\"?[-a-z0-9]+\"?;\\)*"
700 "[ \t\n]*charset=\"?\\([^ \t\n\";]+\\)\"?")
701 "Regexp to match MIME-charset specification in a header of message.
702 The first parenthesized expression should match the MIME-charset name.")
705 ;;; Regexp matching the delimiter of messages in UNIX mail format
706 ;;; (UNIX From lines), minus the initial ^. Note that if you change
707 ;;; this expression, you must change the code in pmail-nuke-pinhead-header
708 ;;; that knows the exact ordering of the \\( \\) subexpressions.
709 (defvar pmail-unix-mail-delimiter
710 (let ((time-zone-regexp
711 (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
712 "\\|[-+]?[0-9][0-9][0-9][0-9]"
713 "\\|"
714 "\\) *")))
715 (concat
716 "From "
718 ;; Many things can happen to an RFC 822 mailbox before it is put into
719 ;; a `From' line. The leading phrase can be stripped, e.g.
720 ;; `Joe <@w.x:joe@y.z>' -> `<@w.x:joe@y.z>'. The <> can be stripped, e.g.
721 ;; `<@x.y:joe@y.z>' -> `@x.y:joe@y.z'. Everything starting with a CRLF
722 ;; can be removed, e.g.
723 ;; From: joe@y.z (Joe K
724 ;; User)
725 ;; can yield `From joe@y.z (Joe K Fri Mar 22 08:11:15 1996', and
726 ;; From: Joe User
727 ;; <joe@y.z>
728 ;; can yield `From Joe User Fri Mar 22 08:11:15 1996'.
729 ;; The mailbox can be removed or be replaced by white space, e.g.
730 ;; From: "Joe User"{space}{tab}
731 ;; <joe@y.z>
732 ;; can yield `From {space}{tab} Fri Mar 22 08:11:15 1996',
733 ;; where {space} and {tab} represent the Ascii space and tab characters.
734 ;; We want to match the results of any of these manglings.
735 ;; The following regexp rejects names whose first characters are
736 ;; obviously bogus, but after that anything goes.
737 "\\([^\0-\b\n-\r\^?].*\\)? "
739 ;; The time the message was sent.
740 "\\([^\0-\r \^?]+\\) +" ; day of the week
741 "\\([^\0-\r \^?]+\\) +" ; month
742 "\\([0-3]?[0-9]\\) +" ; day of month
743 "\\([0-2][0-9]:[0-5][0-9]\\(:[0-6][0-9]\\)?\\) *" ; time of day
745 ;; Perhaps a time zone, specified by an abbreviation, or by a
746 ;; numeric offset.
747 time-zone-regexp
749 ;; The year.
750 " \\([0-9][0-9]+\\) *"
752 ;; On some systems the time zone can appear after the year, too.
753 time-zone-regexp
755 ;; Old uucp cruft.
756 "\\(remote from .*\\)?"
758 "\n"))
759 nil)
761 (defvar pmail-font-lock-keywords
762 ;; These are all matched case-insensitively.
763 (eval-when-compile
764 (let* ((cite-chars "[>|}]")
765 (cite-prefix "a-z")
766 (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
767 (list '("^\\(From\\|Sender\\|Resent-From\\):"
768 . 'pmail-header-name)
769 '("^Reply-To:.*$" . 'pmail-header-name)
770 '("^Subject:" . 'pmail-header-name)
771 '("^X-Spam-Status:" . 'pmail-header-name)
772 '("^\\(To\\|Apparently-To\\|Cc\\|Newsgroups\\):"
773 . 'pmail-header-name)
774 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
775 `(,cite-chars
776 (,(concat "\\=[ \t]*"
777 "\\(\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
778 "\\(" cite-chars "[ \t]*\\)\\)+\\)"
779 "\\(.*\\)")
780 (beginning-of-line) (end-of-line)
781 (1 font-lock-comment-delimiter-face nil t)
782 (5 font-lock-comment-face nil t)))
783 '("^\\(X-[a-z0-9-]+\\|In-reply-to\\|Date\\):.*\\(\n[ \t]+.*\\)*$"
784 . 'pmail-header-name))))
785 "Additional expressions to highlight in Pmail mode.")
787 ;; Perform BODY in the summary buffer
788 ;; in such a way that its cursor is properly updated in its own window.
789 (defmacro pmail-select-summary (&rest body)
790 `(let ((total pmail-total-messages))
791 (if (pmail-summary-displayed)
792 (let ((window (selected-window)))
793 (save-excursion
794 (unwind-protect
795 (progn
796 (pop-to-buffer pmail-summary-buffer)
797 ;; pmail-total-messages is a buffer-local var
798 ;; in the pmail buffer.
799 ;; This way we make it available for the body
800 ;; even tho the pmail buffer is not current.
801 (let ((pmail-total-messages total))
802 ,@body))
803 (select-window window))))
804 (save-excursion
805 (set-buffer pmail-summary-buffer)
806 (let ((pmail-total-messages total))
807 ,@body)))
808 (pmail-maybe-display-summary)))
810 ;;;; *** Pmail Mode ***
812 ;; This variable is dynamically bound. The defvar is here to placate
813 ;; the byte compiler.
815 (defvar pmail-enable-multibyte nil)
818 (defun pmail-require-mime-maybe ()
819 "Require `pmail-mime-feature' if that is non-nil.
820 Signal an error and set `pmail-mime-feature' to nil if the feature
821 isn't provided."
822 (when pmail-enable-mime
823 (condition-case err
824 (require pmail-mime-feature)
825 (error
826 (display-warning
827 :warning
828 (format "Although MIME support is requested
829 by setting `pmail-enable-mime' to non-nil, the required feature
830 `%s' (the value of `pmail-mime-feature')
831 is not available in the current session.
832 So, the MIME support is turned off for the moment."
833 pmail-mime-feature))
834 (setq pmail-enable-mime nil)))))
837 ;;;###autoload
838 (defun pmail (&optional file-name-arg)
839 "Read and edit incoming mail.
840 Moves messages into file named by `pmail-file-name' (a babyl format file)
841 and edits that file in PMAIL Mode.
842 Type \\[describe-mode] once editing that file, for a list of PMAIL commands.
844 May be called with file name as argument; then performs pmail editing on
845 that file, but does not copy any new mail into the file.
846 Interactively, if you supply a prefix argument, then you
847 have a chance to specify a file name with the minibuffer.
849 If `pmail-display-summary' is non-nil, make a summary for this PMAIL file."
850 (interactive (if current-prefix-arg
851 (list (read-file-name "Run pmail on PMAIL file: "))))
852 (pmail-require-mime-maybe)
853 (let* ((file-name (expand-file-name (or file-name-arg pmail-file-name)))
854 ;; Use find-buffer-visiting, not get-file-buffer, for those users
855 ;; who have find-file-visit-truename set to t.
856 (existed (find-buffer-visiting file-name))
857 run-mail-hook mail-buf msg-shown)
858 ;; Determine if an existing mail file has been changed behind the
859 ;; scene...
860 (if (and existed (not (verify-visited-file-modtime existed)))
861 ;; The mail file has been changed. Revisit it and reset the
862 ;; message state variables when in pmail mode.
863 (progn
864 (find-file file-name)
865 (when (and (verify-visited-file-modtime existed)
866 (eq major-mode 'pmail-mode))
867 (pmail-set-message-counters)))
868 ;; The mail file is either unchanged or not visited. Visit it.
869 (switch-to-buffer
870 (let ((enable-local-variables nil))
871 (find-file-noselect file-name))))
872 ;; Insure that the collection and view buffers are in sync and
873 ;; insure that a message is not being edited.
874 (setq pmail-buffers-swapped-p nil)
875 (if (eq major-mode 'pmail-edit-mode)
876 (error "Exit Pmail Edit mode before getting new mail"))
877 ;; Insure that the Rmail file is in mbox format, the buffer is in
878 ;; Pmail mode and has been scanned to find all the messages
879 ;; (setting the global message variables in the process).
880 (pmail-convert-file-maybe)
881 (unless (eq major-mode 'pmail-mode)
882 (pmail-mode-2))
883 (goto-char (point-max))
884 (pmail-maybe-set-message-counters)
885 (setq mail-buf pmail-buffer)
886 ;; Show the first unread message and process summary mode.
887 (unwind-protect
888 ;; Only get new mail when there is not a file name argument.
889 (unless file-name-arg
890 (pmail-get-new-mail))
891 (progn
892 (set-buffer mail-buf)
893 (pmail-show-message-maybe (pmail-first-unseen-message))
894 (if pmail-display-summary (pmail-summary))
895 (pmail-construct-io-menu)
896 (if run-mail-hook
897 (run-hooks 'pmail-mode-hook))))))
899 ;; Given the value of MAILPATH, return a list of inbox file names.
900 ;; This is turned off because it is not clear that the user wants
901 ;; all these inboxes to feed into the primary pmail file.
902 ; (defun pmail-convert-mailpath (string)
903 ; (let (idx list)
904 ; (while (setq idx (string-match "[%:]" string))
905 ; (let ((this (substring string 0 idx)))
906 ; (setq string (substring string (1+ idx)))
907 ; (setq list (cons (if (string-match "%" this)
908 ; (substring this 0 (string-match "%" this))
909 ; this)
910 ; list))))
911 ; list))
913 ; I have checked that adding "-*- pmail -*-" to the BABYL OPTIONS line
914 ; will not cause emacs 18.55 problems.
916 ;; This calls pmail-decode-babyl-format if the file is already Babyl.
918 (defun pmail-convert-file-maybe ()
919 "Determine if the file needs to be converted to mbox format."
920 (widen)
921 (goto-char (point-min))
922 ;; Detect previous Babyl format files.
923 (cond ((looking-at "BABYL OPTIONS:")
924 ;; The file is Babyl version 5. Use unrmail to convert
925 ;; it.
926 (pmail-convert-babyl-to-mbox))
927 ((looking-at "Version: 5\n")
928 ;; Losing babyl file made by old version of Pmail. Fix the
929 ;; babyl file header and use unrmail to convert to mbox
930 ;; format.
931 (let ((buffer-read-only nil))
932 (insert "BABYL OPTIONS: -*- pmail -*-\n")
933 (pmail-convert-babyl-to-mbox)))
934 ((equal (point-min) (point-max))
935 (message "Empty Pmail file."))
936 ((looking-at "From "))
937 (t (pmail-error-bad-format))))
939 (defun pmail-error-bad-format (&optional msgnum)
940 "Report that the buffer contains a message that is not RFC2822
941 compliant.
942 MSGNUM, if present, indicates the malformed message."
943 (if msgnum
944 (error "Message %s is not a valid RFC2822 message." msgnum)
945 (error "Invalid mbox format mail file.")))
947 (defun pmail-convert-babyl-to-mbox ()
948 "Convert the mail file from Babyl version 5 to mbox."
949 (let ((old-file (make-temp-file "pmail"))
950 (new-file (make-temp-file "pmail")))
951 (unwind-protect
952 (progn
953 (write-region (point-min) (point-max) old-file)
954 (unrmail old-file new-file)
955 (message "Replacing BABYL format with mbox format...")
956 (let ((inhibit-read-only t))
957 (erase-buffer)
958 (insert-file-contents-literally new-file)
959 (goto-char (point-max))
960 (pmail-set-message-counters))
961 (message "Replacing BABYL format with mbox format...done"))
962 (delete-file old-file)
963 (delete-file new-file))))
965 (defun pmail-insert-pmail-file-header ()
966 (let ((buffer-read-only nil))
967 ;; -*-pmail-*- is here so that visiting the file normally
968 ;; recognizes it as an Pmail file.
969 (insert "BABYL OPTIONS: -*- pmail -*-
970 Version: 5
971 Labels:
972 Note: This is the header of an pmail file.
973 Note: If you are seeing it in pmail,
974 Note: it means the file has no messages in it.\n\^_")))
976 (defun pmail-get-coding-system ()
977 "Return a suitable coding system to use for the mail message in
978 the region."
979 (let ((content-type-header (mail-fetch-field "content-type"))
980 separator)
981 (save-excursion
982 (setq separator (search-forward "\n\n")))
983 (if (and content-type-header
984 (string-match pmail-mime-charset-pattern content-type-header))
985 (substring content-type-header (match-beginning 1) (match-end 1))
986 'undecided)))
988 ;; Decode Babyl formatted part at the head of current buffer by
989 ;; pmail-file-coding-system, or if it is nil, do auto conversion.
991 (defun pmail-decode-babyl-format ()
992 (let ((modifiedp (buffer-modified-p))
993 (buffer-read-only nil)
994 (coding-system pmail-file-coding-system)
995 from to)
996 (goto-char (point-min))
997 (search-forward "\n\^_" nil t) ; Skip BABYL header.
998 (setq from (point))
999 (goto-char (point-max))
1000 (search-backward "\n\^_" from 'mv)
1001 (setq to (point))
1002 (unless (and coding-system
1003 (coding-system-p coding-system))
1004 (setq coding-system
1005 ;; If pmail-file-coding-system is nil, Emacs 21 writes
1006 ;; PMAIL files in emacs-mule, Emacs 22 in utf-8, but
1007 ;; earlier versions did that with the current buffer's
1008 ;; encoding. So we want to favor detection of emacs-mule
1009 ;; (whose normal priority is quite low) and utf-8, but
1010 ;; still allow detection of other encodings if they won't
1011 ;; fit. The call to with-coding-priority below achieves
1012 ;; that.
1013 (with-coding-priority '(emacs-mule utf-8)
1014 (detect-coding-region from to 'highest))))
1015 (unless (eq (coding-system-type coding-system) 'undecided)
1016 (set-buffer-modified-p t) ; avoid locking when decoding
1017 (let ((buffer-undo-list t))
1018 (decode-coding-region from to coding-system))
1019 (setq coding-system last-coding-system-used))
1020 (set-buffer-modified-p modifiedp)
1021 (setq buffer-file-coding-system nil)
1022 (setq save-buffer-coding-system
1023 (or coding-system 'undecided))))
1025 (defvar pmail-mode-map nil)
1026 (if pmail-mode-map
1028 (setq pmail-mode-map (make-keymap))
1029 (suppress-keymap pmail-mode-map)
1030 (define-key pmail-mode-map "a" 'pmail-add-label)
1031 (define-key pmail-mode-map "b" 'pmail-bury)
1032 (define-key pmail-mode-map "c" 'pmail-continue)
1033 (define-key pmail-mode-map "d" 'pmail-delete-forward)
1034 (define-key pmail-mode-map "\C-d" 'pmail-delete-backward)
1035 (define-key pmail-mode-map "e" 'pmail-edit-current-message)
1036 (define-key pmail-mode-map "f" 'pmail-forward)
1037 (define-key pmail-mode-map "g" 'pmail-get-new-mail)
1038 (define-key pmail-mode-map "h" 'pmail-summary)
1039 (define-key pmail-mode-map "i" 'pmail-input)
1040 (define-key pmail-mode-map "j" 'pmail-show-message-maybe)
1041 (define-key pmail-mode-map "k" 'pmail-kill-label)
1042 (define-key pmail-mode-map "l" 'pmail-summary-by-labels)
1043 (define-key pmail-mode-map "\e\C-h" 'pmail-summary)
1044 (define-key pmail-mode-map "\e\C-l" 'pmail-summary-by-labels)
1045 (define-key pmail-mode-map "\e\C-r" 'pmail-summary-by-recipients)
1046 (define-key pmail-mode-map "\e\C-s" 'pmail-summary-by-regexp)
1047 (define-key pmail-mode-map "\e\C-t" 'pmail-summary-by-topic)
1048 (define-key pmail-mode-map "m" 'pmail-mail)
1049 (define-key pmail-mode-map "\em" 'pmail-retry-failure)
1050 (define-key pmail-mode-map "n" 'pmail-next-undeleted-message)
1051 (define-key pmail-mode-map "\en" 'pmail-next-message)
1052 (define-key pmail-mode-map "\e\C-n" 'pmail-next-labeled-message)
1053 (define-key pmail-mode-map "o" 'pmail-output-to-pmail-file)
1054 (define-key pmail-mode-map "\C-o" 'pmail-output)
1055 (define-key pmail-mode-map "p" 'pmail-previous-undeleted-message)
1056 (define-key pmail-mode-map "\ep" 'pmail-previous-message)
1057 (define-key pmail-mode-map "\e\C-p" 'pmail-previous-labeled-message)
1058 (define-key pmail-mode-map "q" 'pmail-quit)
1059 (define-key pmail-mode-map "r" 'pmail-reply)
1060 ;; I find I can't live without the default M-r command -- rms.
1061 ;; (define-key pmail-mode-map "\er" 'pmail-search-backwards)
1062 (define-key pmail-mode-map "s" 'pmail-expunge-and-save)
1063 (define-key pmail-mode-map "\es" 'pmail-search)
1064 (define-key pmail-mode-map "t" 'pmail-toggle-header)
1065 (define-key pmail-mode-map "u" 'pmail-undelete-previous-message)
1066 (define-key pmail-mode-map "w" 'pmail-output-body-to-file)
1067 (define-key pmail-mode-map "x" 'pmail-expunge)
1068 (define-key pmail-mode-map "." 'pmail-beginning-of-message)
1069 (define-key pmail-mode-map "/" 'pmail-end-of-message)
1070 (define-key pmail-mode-map "<" 'pmail-first-message)
1071 (define-key pmail-mode-map ">" 'pmail-last-message)
1072 (define-key pmail-mode-map " " 'scroll-up)
1073 (define-key pmail-mode-map "\177" 'scroll-down)
1074 (define-key pmail-mode-map "?" 'describe-mode)
1075 (define-key pmail-mode-map "\C-c\C-s\C-d" 'pmail-sort-by-date)
1076 (define-key pmail-mode-map "\C-c\C-s\C-s" 'pmail-sort-by-subject)
1077 (define-key pmail-mode-map "\C-c\C-s\C-a" 'pmail-sort-by-author)
1078 (define-key pmail-mode-map "\C-c\C-s\C-r" 'pmail-sort-by-recipient)
1079 (define-key pmail-mode-map "\C-c\C-s\C-c" 'pmail-sort-by-correspondent)
1080 (define-key pmail-mode-map "\C-c\C-s\C-l" 'pmail-sort-by-lines)
1081 (define-key pmail-mode-map "\C-c\C-s\C-k" 'pmail-sort-by-labels)
1082 (define-key pmail-mode-map "\C-c\C-n" 'pmail-next-same-subject)
1083 (define-key pmail-mode-map "\C-c\C-p" 'pmail-previous-same-subject)
1086 (define-key pmail-mode-map [menu-bar] (make-sparse-keymap))
1088 (define-key pmail-mode-map [menu-bar classify]
1089 (cons "Classify" (make-sparse-keymap "Classify")))
1091 (define-key pmail-mode-map [menu-bar classify input-menu]
1092 nil)
1094 (define-key pmail-mode-map [menu-bar classify output-menu]
1095 nil)
1097 (define-key pmail-mode-map [menu-bar classify output-body]
1098 '("Output body to file..." . pmail-output-body-to-file))
1100 (define-key pmail-mode-map [menu-bar classify output-inbox]
1101 '("Output (inbox)..." . pmail-output))
1103 (define-key pmail-mode-map [menu-bar classify output]
1104 '("Output (Pmail)..." . pmail-output-to-pmail-file))
1106 (define-key pmail-mode-map [menu-bar classify kill-label]
1107 '("Kill Label..." . pmail-kill-label))
1109 (define-key pmail-mode-map [menu-bar classify add-label]
1110 '("Add Label..." . pmail-add-label))
1112 (define-key pmail-mode-map [menu-bar summary]
1113 (cons "Summary" (make-sparse-keymap "Summary")))
1115 (define-key pmail-mode-map [menu-bar summary senders]
1116 '("By Senders..." . pmail-summary-by-senders))
1118 (define-key pmail-mode-map [menu-bar summary labels]
1119 '("By Labels..." . pmail-summary-by-labels))
1121 (define-key pmail-mode-map [menu-bar summary recipients]
1122 '("By Recipients..." . pmail-summary-by-recipients))
1124 (define-key pmail-mode-map [menu-bar summary topic]
1125 '("By Topic..." . pmail-summary-by-topic))
1127 (define-key pmail-mode-map [menu-bar summary regexp]
1128 '("By Regexp..." . pmail-summary-by-regexp))
1130 (define-key pmail-mode-map [menu-bar summary all]
1131 '("All" . pmail-summary))
1133 (define-key pmail-mode-map [menu-bar mail]
1134 (cons "Mail" (make-sparse-keymap "Mail")))
1136 (define-key pmail-mode-map [menu-bar mail pmail-get-new-mail]
1137 '("Get New Mail" . pmail-get-new-mail))
1139 (define-key pmail-mode-map [menu-bar mail lambda]
1140 '("----"))
1142 (define-key pmail-mode-map [menu-bar mail continue]
1143 '("Continue" . pmail-continue))
1145 (define-key pmail-mode-map [menu-bar mail resend]
1146 '("Re-send..." . pmail-resend))
1148 (define-key pmail-mode-map [menu-bar mail forward]
1149 '("Forward" . pmail-forward))
1151 (define-key pmail-mode-map [menu-bar mail retry]
1152 '("Retry" . pmail-retry-failure))
1154 (define-key pmail-mode-map [menu-bar mail reply]
1155 '("Reply" . pmail-reply))
1157 (define-key pmail-mode-map [menu-bar mail mail]
1158 '("Mail" . pmail-mail))
1160 (define-key pmail-mode-map [menu-bar delete]
1161 (cons "Delete" (make-sparse-keymap "Delete")))
1163 (define-key pmail-mode-map [menu-bar delete expunge/save]
1164 '("Expunge/Save" . pmail-expunge-and-save))
1166 (define-key pmail-mode-map [menu-bar delete expunge]
1167 '("Expunge" . pmail-expunge))
1169 (define-key pmail-mode-map [menu-bar delete undelete]
1170 '("Undelete" . pmail-undelete-previous-message))
1172 (define-key pmail-mode-map [menu-bar delete delete]
1173 '("Delete" . pmail-delete-forward))
1175 (define-key pmail-mode-map [menu-bar move]
1176 (cons "Move" (make-sparse-keymap "Move")))
1178 (define-key pmail-mode-map [menu-bar move search-back]
1179 '("Search Back..." . pmail-search-backwards))
1181 (define-key pmail-mode-map [menu-bar move search]
1182 '("Search..." . pmail-search))
1184 (define-key pmail-mode-map [menu-bar move previous]
1185 '("Previous Nondeleted" . pmail-previous-undeleted-message))
1187 (define-key pmail-mode-map [menu-bar move next]
1188 '("Next Nondeleted" . pmail-next-undeleted-message))
1190 (define-key pmail-mode-map [menu-bar move last]
1191 '("Last" . pmail-last-message))
1193 (define-key pmail-mode-map [menu-bar move first]
1194 '("First" . pmail-first-message))
1196 (define-key pmail-mode-map [menu-bar move previous]
1197 '("Previous" . pmail-previous-message))
1199 (define-key pmail-mode-map [menu-bar move next]
1200 '("Next" . pmail-next-message))
1202 ;; Pmail toolbar
1203 (defvar pmail-tool-bar-map
1204 (let ((map (make-sparse-keymap)))
1205 (tool-bar-local-item-from-menu 'pmail-get-new-mail "mail/inbox"
1206 map pmail-mode-map)
1207 (tool-bar-local-item-from-menu 'pmail-next-undeleted-message "right-arrow"
1208 map pmail-mode-map)
1209 (tool-bar-local-item-from-menu 'pmail-previous-undeleted-message "left-arrow"
1210 map pmail-mode-map)
1211 (tool-bar-local-item-from-menu 'pmail-search "search"
1212 map pmail-mode-map)
1213 (tool-bar-local-item-from-menu 'pmail-input "open"
1214 map pmail-mode-map)
1215 (tool-bar-local-item-from-menu 'pmail-mail "mail/compose"
1216 map pmail-mode-map)
1217 (tool-bar-local-item-from-menu 'pmail-reply "mail/reply-all"
1218 map pmail-mode-map)
1219 (tool-bar-local-item-from-menu 'pmail-forward "mail/forward"
1220 map pmail-mode-map)
1221 (tool-bar-local-item-from-menu 'pmail-delete-forward "close"
1222 map pmail-mode-map)
1223 (tool-bar-local-item-from-menu 'pmail-output "mail/move"
1224 map pmail-mode-map)
1225 (tool-bar-local-item-from-menu 'pmail-output-body-to-file "mail/save"
1226 map pmail-mode-map)
1227 (tool-bar-local-item-from-menu 'pmail-expunge "delete"
1228 map pmail-mode-map)
1229 map))
1233 ;; Pmail mode is suitable only for specially formatted data.
1234 (put 'pmail-mode 'mode-class 'special)
1236 (defun pmail-mode-kill-summary ()
1237 (if pmail-summary-buffer (kill-buffer pmail-summary-buffer)))
1239 ;;;###autoload
1240 (defun pmail-mode ()
1241 "Pmail Mode is used by \\<pmail-mode-map>\\[pmail] for editing Pmail files.
1242 All normal editing commands are turned off.
1243 Instead, these commands are available:
1245 \\[pmail-beginning-of-message] Move point to front of this message.
1246 \\[pmail-end-of-message] Move point to bottom of this message.
1247 \\[scroll-up] Scroll to next screen of this message.
1248 \\[scroll-down] Scroll to previous screen of this message.
1249 \\[pmail-next-undeleted-message] Move to Next non-deleted message.
1250 \\[pmail-previous-undeleted-message] Move to Previous non-deleted message.
1251 \\[pmail-next-message] Move to Next message whether deleted or not.
1252 \\[pmail-previous-message] Move to Previous message whether deleted or not.
1253 \\[pmail-first-message] Move to the first message in Pmail file.
1254 \\[pmail-last-message] Move to the last message in Pmail file.
1255 \\[pmail-show-message-maybe] Jump to message specified by numeric position in file.
1256 \\[pmail-search] Search for string and show message it is found in.
1257 \\[pmail-delete-forward] Delete this message, move to next nondeleted.
1258 \\[pmail-delete-backward] Delete this message, move to previous nondeleted.
1259 \\[pmail-undelete-previous-message] Undelete message. Tries current message, then earlier messages
1260 till a deleted message is found.
1261 \\[pmail-edit-current-message] Edit the current message. \\[pmail-cease-edit] to return to Pmail.
1262 \\[pmail-expunge] Expunge deleted messages.
1263 \\[pmail-expunge-and-save] Expunge and save the file.
1264 \\[pmail-quit] Quit Pmail: expunge, save, then switch to another buffer.
1265 \\[save-buffer] Save without expunging.
1266 \\[pmail-get-new-mail] Move new mail from system spool directory into this file.
1267 \\[pmail-mail] Mail a message (same as \\[mail-other-window]).
1268 \\[pmail-continue] Continue composing outgoing message started before.
1269 \\[pmail-reply] Reply to this message. Like \\[pmail-mail] but initializes some fields.
1270 \\[pmail-retry-failure] Send this message again. Used on a mailer failure message.
1271 \\[pmail-forward] Forward this message to another user.
1272 \\[pmail-output-to-pmail-file] Output this message to an Pmail file (append it).
1273 \\[pmail-output] Output this message to a Unix-format mail file (append it).
1274 \\[pmail-output-body-to-file] Save message body to a file. Default filename comes from Subject line.
1275 \\[pmail-input] Input Pmail file. Run Pmail on that file.
1276 \\[pmail-add-label] Add label to message. It will be displayed in the mode line.
1277 \\[pmail-kill-label] Kill label. Remove a label from current message.
1278 \\[pmail-next-labeled-message] Move to Next message with specified label
1279 (label defaults to last one specified).
1280 Standard labels: filed, unseen, answered, forwarded, deleted.
1281 Any other label is present only if you add it with \\[pmail-add-label].
1282 \\[pmail-previous-labeled-message] Move to Previous message with specified label
1283 \\[pmail-summary] Show headers buffer, with a one line summary of each message.
1284 \\[pmail-summary-by-labels] Summarize only messages with particular label(s).
1285 \\[pmail-summary-by-recipients] Summarize only messages with particular recipient(s).
1286 \\[pmail-summary-by-regexp] Summarize only messages with particular regexp(s).
1287 \\[pmail-summary-by-topic] Summarize only messages with subject line regexp(s).
1288 \\[pmail-toggle-header] Toggle display of complete header."
1289 (interactive)
1290 (let ((finding-pmail-file (not (eq major-mode 'pmail-mode))))
1291 (pmail-mode-2)
1292 (when (and finding-pmail-file
1293 (null coding-system-for-read)
1294 default-enable-multibyte-characters)
1295 (let ((pmail-enable-multibyte t))
1296 (pmail-require-mime-maybe)
1297 (pmail-convert-file-maybe)
1298 (goto-char (point-max))
1299 (set-buffer-multibyte t)))
1300 (pmail-set-message-counters)
1301 (pmail-show-message-maybe pmail-total-messages)
1302 (when finding-pmail-file
1303 (when pmail-display-summary
1304 (pmail-summary))
1305 (pmail-construct-io-menu))
1306 (run-mode-hooks 'pmail-mode-hook)))
1308 (defun pmail-mode-2 ()
1309 (kill-all-local-variables)
1310 (pmail-mode-1)
1311 (pmail-perm-variables)
1312 (pmail-variables))
1314 (defun pmail-mode-1 ()
1315 (setq major-mode 'pmail-mode)
1316 (setq mode-name "PMAIL")
1317 (setq buffer-read-only t)
1318 ;; No need to auto save PMAIL files in normal circumstances
1319 ;; because they contain no info except attribute changes
1320 ;; and deletion of messages.
1321 ;; The one exception is when messages are copied into an Pmail mode buffer.
1322 ;; pmail-output-to-pmail-file enables auto save when you do that.
1323 (setq buffer-auto-save-file-name nil)
1324 (setq mode-line-modified "--")
1325 (use-local-map pmail-mode-map)
1326 (set-syntax-table text-mode-syntax-table)
1327 (setq local-abbrev-table text-mode-abbrev-table)
1328 ;; First attempt at adding hook functions to support buffer swapping...
1329 (add-hook 'write-region-annotate-functions 'pmail-write-region-annotate nil t)
1330 (add-hook 'kill-buffer-hook 'pmail-mode-kill-buffer-hook nil t)
1331 (add-hook 'change-major-mode-hook 'pmail-change-major-mode-hook nil t))
1333 (defun pmail-generate-viewer-buffer ()
1334 "Return a reusable buffer suitable for viewing messages.
1335 Create the buffer if necessary."
1336 (let* ((suffix (file-name-nondirectory (or buffer-file-name (buffer-name))))
1337 (name (format " *message-viewer %s*" suffix))
1338 (buf (get-buffer name)))
1339 (unless buf
1340 (generate-new-buffer name))))
1342 ;; Used in write-region-annotate-functions to write Pmail files out
1343 ;; correctly.
1344 (defun pmail-write-region-annotate (start end)
1345 ;; When called from write-file (and auto-save), `start' is nil.
1346 ;; When called from M-x write-region, we assume the user wants to save
1347 ;; (part of) the inbox, not the message display data.
1348 (unless (or start (not pmail-buffers-swapped-p))
1349 ;;(tar-clear-modification-flags)
1350 (set-buffer pmail-view-buffer)
1351 (widen)
1352 nil))
1354 (defun pmail-change-major-mode-hook ()
1355 ;; Bring the actual Pmail messages back into the main buffer.
1356 (when (pmail-buffers-swapped-p)
1357 (current-buffer)
1358 (buffer-swap-text pmail-view-buffer)))
1359 ;; Throw away the summary.
1360 ;;(when (buffer-live-p pmail-view-buffer) (kill-buffer pmail-view-buffer)))
1362 (defun pmail-buffers-swapped-p ()
1363 "Return non-nil if the message collection is in `pmail-view-buffer'."
1364 ;; We need to be careful to keep track of which buffer holds the
1365 ;; message collection, since we swap the collection the view of the
1366 ;; current message back and forth. This model is based on Stefan
1367 ;; Monnier's solution for tar-mode.
1368 (and (buffer-live-p pmail-view-buffer)
1369 (> (buffer-size pmail-view-buffer) (buffer-size))))
1371 (defun pmail-mode-kill-buffer-hook ()
1372 (if (buffer-live-p pmail-view-buffer) (kill-buffer pmail-view-buffer)))
1374 ;; Set up the permanent locals associated with an Pmail file.
1375 (defun pmail-perm-variables ()
1376 (make-local-variable 'pmail-last-label)
1377 (make-local-variable 'pmail-last-regexp)
1378 (make-local-variable 'pmail-deleted-vector)
1379 (make-local-variable 'pmail-buffer)
1380 (setq pmail-buffer (current-buffer))
1381 (set-buffer-multibyte nil)
1382 (make-local-variable 'pmail-view-buffer)
1383 (save-excursion
1384 (setq pmail-view-buffer (pmail-generate-viewer-buffer))
1385 (set-buffer pmail-view-buffer)
1386 (set-buffer-multibyte t))
1387 (make-local-variable 'pmail-summary-buffer)
1388 (make-local-variable 'pmail-summary-vector)
1389 (make-local-variable 'pmail-current-message)
1390 (make-local-variable 'pmail-total-messages)
1391 (setq pmail-total-messages 0)
1392 (make-local-variable 'pmail-overlay-list)
1393 (setq pmail-overlay-list nil)
1394 (make-local-variable 'pmail-message-vector)
1395 (make-local-variable 'pmail-msgref-vector)
1396 (make-local-variable 'pmail-inbox-list)
1397 (setq pmail-inbox-list (pmail-parse-file-inboxes))
1398 ;; Provide default set of inboxes for primary mail file ~/PMAIL.
1399 (and (null pmail-inbox-list)
1400 (or (equal buffer-file-name (expand-file-name pmail-file-name))
1401 (equal buffer-file-truename
1402 (abbreviate-file-name (file-truename pmail-file-name))))
1403 (setq pmail-inbox-list
1404 (or pmail-primary-inbox-list
1405 (list (or (getenv "MAIL")
1406 (concat rmail-spool-directory
1407 (user-login-name)))))))
1408 (make-local-variable 'pmail-keywords)
1409 (set (make-local-variable 'tool-bar-map) pmail-tool-bar-map)
1410 (make-local-variable 'pmail-buffers-swapped-p)
1411 ;; this gets generated as needed
1412 (setq pmail-keywords nil))
1414 ;; Set up the non-permanent locals associated with Pmail mode.
1415 (defun pmail-variables ()
1416 (make-local-variable 'save-buffer-coding-system)
1417 ;; If we don't already have a value for save-buffer-coding-system,
1418 ;; get it from buffer-file-coding-system, and clear that
1419 ;; because it should be determined in pmail-show-message.
1420 (unless save-buffer-coding-system
1421 (setq save-buffer-coding-system (or buffer-file-coding-system 'undecided))
1422 (setq buffer-file-coding-system nil))
1423 ;; Don't let a local variables list in a message cause confusion.
1424 (make-local-variable 'local-enable-local-variables)
1425 (setq local-enable-local-variables nil)
1426 (make-local-variable 'revert-buffer-function)
1427 (setq revert-buffer-function 'pmail-revert)
1428 (make-local-variable 'font-lock-defaults)
1429 (setq font-lock-defaults
1430 '(pmail-font-lock-keywords
1431 t t nil nil
1432 (font-lock-maximum-size . nil)
1433 (font-lock-fontify-buffer-function . pmail-fontify-buffer-function)
1434 (font-lock-unfontify-buffer-function . pmail-unfontify-buffer-function)
1435 (font-lock-inhibit-thing-lock . (lazy-lock-mode fast-lock-mode))))
1436 (make-local-variable 'require-final-newline)
1437 (setq require-final-newline nil)
1438 (make-local-variable 'version-control)
1439 (setq version-control 'never)
1440 (make-local-variable 'kill-buffer-hook)
1441 (add-hook 'kill-buffer-hook 'pmail-mode-kill-summary)
1442 (make-local-variable 'file-precious-flag)
1443 (setq file-precious-flag t)
1444 (make-local-variable 'desktop-save-buffer)
1445 (setq desktop-save-buffer t))
1447 ;; Handle M-x revert-buffer done in an pmail-mode buffer.
1448 (defun pmail-revert (arg noconfirm)
1449 (set-buffer pmail-buffer)
1450 (let* ((revert-buffer-function (default-value 'revert-buffer-function))
1451 (pmail-enable-multibyte enable-multibyte-characters)
1452 ;; See similar code in `pmail'.
1453 (coding-system-for-read (and pmail-enable-multibyte 'raw-text)))
1454 ;; Call our caller again, but this time it does the default thing.
1455 (when (revert-buffer arg noconfirm)
1456 ;; If the user said "yes", and we changed something,
1457 ;; reparse the messages.
1458 (set-buffer pmail-buffer)
1459 (pmail-mode-2)
1460 ;; Convert all or part to Babyl file if possible.
1461 (pmail-convert-file-maybe)
1462 ;; We have read the file as raw-text, so the buffer is set to
1463 ;; unibyte. Make it multibyte if necessary.
1464 (if (and pmail-enable-multibyte
1465 (not enable-multibyte-characters))
1466 (set-buffer-multibyte t))
1467 (goto-char (point-max))
1468 (pmail-set-message-counters)
1469 (pmail-show-message-maybe pmail-total-messages)
1470 (run-hooks 'pmail-mode-hook))))
1472 ;; Return a list of files from this buffer's Mail: option.
1473 ;; Does not assume that messages have been parsed.
1474 ;; Just returns nil if buffer does not look like Babyl format.
1475 (defun pmail-parse-file-inboxes ()
1476 (save-excursion
1477 (save-restriction
1478 (widen)
1479 (goto-char 1)
1480 (cond ((looking-at "BABYL OPTIONS:")
1481 (search-forward "\n\^_" nil 'move)
1482 (narrow-to-region 1 (point))
1483 (goto-char 1)
1484 (when (search-forward "\nMail:" nil t)
1485 (narrow-to-region (point) (progn (end-of-line) (point)))
1486 (goto-char (point-min))
1487 (mail-parse-comma-list)))))))
1489 (defun pmail-expunge-and-save ()
1490 "Expunge and save PMAIL file."
1491 (interactive)
1492 (pmail-expunge)
1493 (set-buffer pmail-buffer)
1494 (save-buffer)
1495 (if (pmail-summary-exists)
1496 (pmail-select-summary (set-buffer-modified-p nil))))
1498 (defun pmail-quit ()
1499 "Quit out of PMAIL.
1500 Hook `pmail-quit-hook' is run after expunging."
1501 (interactive)
1502 ;; Determine if the buffers need to be swapped.
1503 (pmail-swap-buffers-maybe)
1504 (pmail-expunge-and-save)
1505 (when (boundp 'pmail-quit-hook)
1506 (run-hooks 'pmail-quit-hook))
1507 ;; Don't switch to the summary buffer even if it was recently visible.
1508 (when pmail-summary-buffer
1509 (replace-buffer-in-windows pmail-summary-buffer)
1510 (bury-buffer pmail-summary-buffer))
1511 (if pmail-enable-mime
1512 (let ((obuf pmail-buffer)
1513 (ovbuf pmail-view-buffer))
1514 (set-buffer pmail-view-buffer)
1515 (quit-window)
1516 (replace-buffer-in-windows ovbuf)
1517 (replace-buffer-in-windows obuf)
1518 (bury-buffer obuf))
1519 (let ((obuf (current-buffer)))
1520 (quit-window)
1521 (replace-buffer-in-windows obuf))))
1523 (defun pmail-bury ()
1524 "Bury current Pmail buffer and its summary buffer."
1525 (interactive)
1526 ;; This let var was called pmail-buffer, but that interfered
1527 ;; with the buffer-local var used in summary buffers.
1528 (let ((buffer-to-bury (current-buffer)))
1529 (if (pmail-summary-exists)
1530 (let (window)
1531 (while (setq window (get-buffer-window pmail-summary-buffer))
1532 (quit-window nil window))
1533 (bury-buffer pmail-summary-buffer)))
1534 (quit-window)))
1536 (defun pmail-duplicate-message ()
1537 "Create a duplicated copy of the current message.
1538 The duplicate copy goes into the Pmail file just after the
1539 original copy."
1540 (interactive)
1541 (widen)
1542 (let ((buffer-read-only nil)
1543 (number pmail-current-message)
1544 (string (buffer-substring (pmail-msgbeg pmail-current-message)
1545 (pmail-msgend pmail-current-message))))
1546 (goto-char (pmail-msgend pmail-current-message))
1547 (insert string)
1548 (pmail-forget-messages)
1549 (pmail-show-message-maybe number)
1550 (message "Message duplicated")))
1552 ;;;###autoload
1553 (defun pmail-input (filename)
1554 "Run Pmail on file FILENAME."
1555 (interactive "FRun pmail on PMAIL file: ")
1556 (pmail filename))
1559 ;; This used to scan subdirectories recursively, but someone pointed out
1560 ;; that if the user wants that, person can put all the files in one dir.
1561 ;; And the recursive scan was slow. So I took it out.
1562 ;; rms, Sep 1996.
1563 (defun pmail-find-all-files (start)
1564 "Return list of file in dir START that match `pmail-secondary-file-regexp'."
1565 (if (file-accessible-directory-p start)
1566 ;; Don't sort here.
1567 (let* ((case-fold-search t)
1568 (files (directory-files start t pmail-secondary-file-regexp)))
1569 ;; Sort here instead of in directory-files
1570 ;; because this list is usually much shorter.
1571 (sort files 'string<))))
1573 (defun pmail-list-to-menu (menu-name l action &optional full-name)
1574 (let ((menu (make-sparse-keymap menu-name)))
1575 (mapc
1576 (function (lambda (item)
1577 (let (command)
1578 (if (consp item)
1579 (progn
1580 (setq command
1581 (pmail-list-to-menu (car item) (cdr item)
1582 action
1583 (if full-name
1584 (concat full-name "/"
1585 (car item))
1586 (car item))))
1587 (setq name (car item)))
1588 (progn
1589 (setq name item)
1590 (setq command
1591 (list 'lambda () '(interactive)
1592 (list action
1593 (expand-file-name
1594 (if full-name
1595 (concat full-name "/" item)
1596 item)
1597 pmail-secondary-file-directory))))))
1598 (define-key menu (vector (intern name))
1599 (cons name command)))))
1600 (reverse l))
1601 menu))
1603 ;; This command is always "disabled" when it appears in a menu.
1604 (put 'pmail-disable-menu 'menu-enable ''nil)
1606 (defun pmail-construct-io-menu ()
1607 (let ((files (pmail-find-all-files pmail-secondary-file-directory)))
1608 (if files
1609 (progn
1610 (define-key pmail-mode-map [menu-bar classify input-menu]
1611 (cons "Input Pmail File"
1612 (pmail-list-to-menu "Input Pmail File"
1613 files
1614 'pmail-input)))
1615 (define-key pmail-mode-map [menu-bar classify output-menu]
1616 (cons "Output Pmail File"
1617 (pmail-list-to-menu "Output Pmail File"
1618 files
1619 'pmail-output-to-pmail-file))))
1621 (define-key pmail-mode-map [menu-bar classify input-menu]
1622 '("Input Pmail File" . pmail-disable-menu))
1623 (define-key pmail-mode-map [menu-bar classify output-menu]
1624 '("Output Pmail File" . pmail-disable-menu)))))
1627 ;;;; *** Pmail input ***
1629 (declare-function pmail-spam-filter "pmail-spam-filter" (msg))
1630 (declare-function pmail-summary-goto-msg "pmailsum" (&optional n nowarn skip-pmail))
1631 (declare-function pmail-summary-mark-undeleted "pmailsum" (n))
1632 (declare-function pmail-summary-mark-deleted "pmailsum" (&optional n undel))
1633 (declare-function rfc822-addresses "rfc822" (header-text))
1634 (declare-function mail-abbrev-make-syntax-table "mailabbrev.el" ())
1635 (declare-function mail-sendmail-delimit-header "sendmail" ())
1636 (declare-function mail-header-end "sendmail" ())
1638 ;; RLK feature not added in this version:
1639 ;; argument specifies inbox file or files in various ways.
1641 (defun pmail-get-new-mail (&optional file-name)
1642 "Move any new mail from this PMAIL file's inbox files.
1643 The inbox files can be specified with the file's Mail: option. The
1644 variable `pmail-primary-inbox-list' specifies the inboxes for your
1645 primary PMAIL file if it has no Mail: option. By default, this is
1646 your /usr/spool/mail/$USER.
1648 You can also specify the file to get new mail from. In this case, the
1649 file of new mail is not changed or deleted. Noninteractively, you can
1650 pass the inbox file name as an argument. Interactively, a prefix
1651 argument causes us to read a file name and use that file as the inbox.
1653 If the variable `pmail-preserve-inbox' is non-nil, new mail will
1654 always be left in inbox files rather than deleted.
1656 This function runs `pmail-get-new-mail-hook' before saving the updated file.
1657 It returns t if it got any new messages."
1658 (interactive
1659 (list (if current-prefix-arg
1660 (read-file-name "Get new mail from file: "))))
1661 (run-hooks 'pmail-before-get-new-mail-hook)
1662 ;; If the disk file has been changed from under us,
1663 ;; revert to it before we get new mail.
1664 (or (verify-visited-file-modtime (current-buffer))
1665 (find-file (buffer-file-name)))
1666 (set-buffer pmail-buffer)
1667 (pmail-maybe-set-message-counters)
1668 (widen)
1669 ;; Get rid of all undo records for this buffer.
1670 (or (eq buffer-undo-list t)
1671 (setq buffer-undo-list nil))
1672 (pmail-get-new-mail-1 file-name))
1674 (defun pmail-get-new-mail-1 (file-name)
1675 "Continuation of 'pmail-get-new-mail. Sort of a procedural
1676 abstraction kind of thing to manage the code size. Return t if
1677 new messages are found, nil otherwise."
1678 (let ((all-files (if file-name (list file-name)
1679 pmail-inbox-list))
1680 (pmail-enable-multibyte (default-value 'enable-multibyte-characters))
1681 found)
1682 (unwind-protect
1683 (when all-files
1684 (let ((opoint (point))
1685 (delete-files ())
1686 ;; If buffer has not changed yet, and has not been
1687 ;; saved yet, don't replace the old backup file now.
1688 (make-backup-files (and make-backup-files (buffer-modified-p)))
1689 (buffer-read-only nil)
1690 ;; Don't make undo records for what we do in getting
1691 ;; mail.
1692 (buffer-undo-list t)
1693 success files file-last-names)
1694 ;; Pull files off all-files onto files as long as there is
1695 ;; no name conflict. A conflict happens when two inbox
1696 ;; file names have the same last component.
1697 (while (and all-files
1698 (not (member (file-name-nondirectory (car all-files))
1699 file-last-names)))
1700 (setq files (cons (car all-files) files)
1701 file-last-names
1702 (cons (file-name-nondirectory (car all-files)) files))
1703 (setq all-files (cdr all-files)))
1704 ;; Put them back in their original order.
1705 (setq files (nreverse files))
1706 (goto-char (point-max))
1707 (skip-chars-backward " \t\n") ; just in case of brain damage
1708 (delete-region (point) (point-max)) ; caused by require-final-newline
1709 (setq found (pmail-get-new-mail-2 file-name files delete-files))))
1710 found)
1711 ;; Don't leave the buffer screwed up if we get a disk-full error.
1712 (or found (pmail-show-message-maybe))))
1714 (defun pmail-get-new-mail-2 (file-name files delete-files)
1715 "Return t if new messages are detected without error, nil otherwise."
1716 (save-excursion
1717 (save-restriction
1718 (let ((new-messages 0)
1719 (spam-filter-p (and (featurep 'pmail-spam-filter) pmail-use-spam-filter))
1720 blurb result success suffix)
1721 (narrow-to-region (point) (point))
1722 ;; Read in the contents of the inbox files, renaming them as
1723 ;; necessary, and adding to the list of files to delete
1724 ;; eventually.
1725 (if file-name
1726 (pmail-insert-inbox-text files nil)
1727 (setq delete-files (pmail-insert-inbox-text files t)))
1728 ;; Scan the new text and convert each message to
1729 ;; Pmail/mbox format.
1730 (goto-char (point-min))
1731 (unwind-protect
1732 (setq new-messages (pmail-add-mbox-headers)
1733 success t)
1734 ;; Try to delete the garbage just inserted.
1735 (or success (delete-region (point-min) (point-max)))
1736 ;; If we could not convert the file's inboxes, rename the
1737 ;; files we tried to read so we won't over and over again.
1738 (if (and (not file-name) (not success))
1739 (let ((delfiles delete-files)
1740 (count 0))
1741 (while delfiles
1742 (while (file-exists-p (format "PMAILOSE.%d" count))
1743 (setq count (1+ count)))
1744 (rename-file (car delfiles) (format "PMAILOSE.%d" count))
1745 (setq delfiles (cdr delfiles))))))
1746 ;; Determine if there are messages.
1747 (unless (zerop new-messages)
1748 ;; There are. Process them.
1749 (goto-char (point-min))
1750 (pmail-count-new-messages)
1751 (run-hooks 'pmail-get-new-mail-hook)
1752 (save-buffer))
1753 ;; Delete the old files, now that the Pmail file is saved.
1754 (while delete-files
1755 (condition-case ()
1756 ;; First, try deleting.
1757 (condition-case ()
1758 (delete-file (car delete-files))
1759 (file-error
1760 ;; If we can't delete it, truncate it.
1761 (write-region (point) (point) (car delete-files))))
1762 (file-error nil))
1763 (setq delete-files (cdr delete-files)))
1764 (if (zerop new-messages)
1765 (when (or file-name pmail-inbox-list)
1766 (message "(No new mail has arrived)"))
1767 ;; Generate the spam message.
1768 (setq blurb (if spam-filter-p
1769 (pmail-get-new-mail-filter-spam new-messages)
1770 "")))
1771 (if (pmail-summary-exists)
1772 (pmail-select-summary (pmail-update-summary)))
1773 (setq suffix (if (= 1 new-messages) "" "s"))
1774 (message "%d new message%s read%s" new-messages suffix blurb)
1775 (when spam-filter-p
1776 (if rsf-beep (beep t))
1777 (sleep-for rsf-sleep-after-message))
1779 ;; Establish the return value and move to the first new
1780 ;; message unless we have other unseen messages before it.
1781 (setq result (> new-messages 0))
1782 (when result
1783 (pmail-show-message-maybe (pmail-first-unseen-message)))
1784 (run-hooks 'pmail-after-get-new-mail-hook)
1785 result))))
1787 (defun pmail-get-new-mail-filter-spam (new-message-count)
1788 "Process new messages for spam."
1789 (let* ((old-messages (- pmail-total-messages new-message-count))
1790 (rsf-number-of-spam 0)
1791 (rsf-scanned-message-number (1+ old-messages))
1792 ;; save deletion flags of old messages: vector starts at zero
1793 ;; (is one longer that no of messages), therefore take 1+
1794 ;; old-messages
1795 (save-deleted (substring pmail-deleted-vector 0 (1+ old-messages)))
1796 blurb)
1797 ;; set all messages to undeleted
1798 (setq pmail-deleted-vector (make-string (1+ pmail-total-messages) ?\ ))
1799 (while (<= rsf-scanned-message-number pmail-total-messages)
1800 (progn
1801 (if (not (pmail-spam-filter rsf-scanned-message-number))
1802 (progn (setq rsf-number-of-spam (1+ rsf-number-of-spam))))
1803 (setq rsf-scanned-message-number (1+ rsf-scanned-message-number))))
1804 (if (> rsf-number-of-spam 0)
1805 (progn
1806 (when (pmail-expunge-confirmed)
1807 (pmail-only-expunge t))))
1808 (setq pmail-deleted-vector
1809 (concat save-deleted
1810 (make-string (- pmail-total-messages old-messages) ?\ )))
1811 ;; Generate a return value message based on the number of spam
1812 ;; messages found.
1813 (cond
1814 ((zerop rsf-number-of-spam) "")
1815 ((= 1 new-message-count) ", and appears to be spam")
1816 ((= rsf-number-of-spam new-message-count) ", and all appear to be spam")
1817 ((> rsf-number-of-spam 1)
1818 (format ", and %d appear to be spam" rsf-number-of-spam))
1819 (t ", and 1 appears to be spam"))))
1821 (defun pmail-parse-url (file)
1822 "Parse the supplied URL. Return (list MAILBOX-NAME REMOTE PASSWORD GOT-PASSWORD)
1823 WHERE MAILBOX-NAME is the name of the mailbox suitable as argument to the
1824 actual version of `movemail', REMOTE is non-nil if MAILBOX-NAME refers to
1825 a remote mailbox, PASSWORD is the password if it should be
1826 supplied as a separate argument to `movemail' or nil otherwise, GOT-PASSWORD
1827 is non-nil if the user has supplied the password interactively.
1829 (cond
1830 ((string-match "^\\([^:]+\\)://\\(\\([^:@]+\\)\\(:\\([^@]+\\)\\)?@\\)?.*" file)
1831 (let (got-password supplied-password
1832 (proto (match-string 1 file))
1833 (user (match-string 3 file))
1834 (pass (match-string 5 file))
1835 (host (substring file (or (match-end 2)
1836 (+ 3 (match-end 1))))))
1838 (if (not pass)
1839 (when pmail-remote-password-required
1840 (setq got-password (not (pmail-have-password)))
1841 (setq supplied-password (pmail-get-remote-password
1842 (string-equal proto "imap"))))
1843 ;; The password is embedded. Strip it out since movemail
1844 ;; does not really like it, in spite of the movemail spec.
1845 (setq file (concat proto "://" user "@" host)))
1847 (if (pmail-movemail-variant-p 'emacs)
1848 (if (string-equal proto "pop")
1849 (list (concat "po:" user ":" host)
1851 (or pass supplied-password)
1852 got-password)
1853 (error "Emacs movemail does not support %s protocol" proto))
1854 (list file
1855 (or (string-equal proto "pop") (string-equal proto "imap"))
1856 (or supplied-password pass)
1857 got-password))))
1859 ((string-match "^po:\\([^:]+\\)\\(:\\(.*\\)\\)?" file)
1860 (let (got-password supplied-password
1861 (proto "pop")
1862 (user (match-string 1 file))
1863 (host (match-string 3 file)))
1865 (when pmail-remote-password-required
1866 (setq got-password (not (pmail-have-password)))
1867 (setq supplied-password (pmail-get-remote-password nil)))
1869 (list file "pop" supplied-password got-password)))
1872 (list file nil nil nil))))
1874 (defun pmail-insert-inbox-text (files renamep)
1875 ;; Detect a locked file now, so that we avoid moving mail
1876 ;; out of the real inbox file. (That could scare people.)
1877 (or (memq (file-locked-p buffer-file-name) '(nil t))
1878 (error "PMAIL file %s is locked"
1879 (file-name-nondirectory buffer-file-name)))
1880 (let (file tofile delete-files movemail popmail got-password password)
1881 (while files
1882 ;; Handle remote mailbox names specially; don't expand as filenames
1883 ;; in case the userid contains a directory separator.
1884 (setq file (car files))
1885 (let ((url-data (pmail-parse-url file)))
1886 (setq file (nth 0 url-data))
1887 (setq popmail (nth 1 url-data))
1888 (setq password (nth 2 url-data))
1889 (setq got-password (nth 3 url-data)))
1891 (if popmail
1892 (setq renamep t)
1893 (setq file (file-truename
1894 (substitute-in-file-name (expand-file-name file)))))
1895 (setq tofile (expand-file-name
1896 ;; Generate name to move to from inbox name,
1897 ;; in case of multiple inboxes that need moving.
1898 (concat ".newmail-"
1899 (file-name-nondirectory
1900 (if (memq system-type '(windows-nt cygwin ms-dos))
1901 ;; cannot have colons in file name
1902 (replace-regexp-in-string ":" "-" file)
1903 file)))
1904 ;; Use the directory of this pmail file
1905 ;; because it's a nuisance to use the homedir
1906 ;; if that is on a full disk and this pmail
1907 ;; file isn't.
1908 (file-name-directory
1909 (expand-file-name buffer-file-name))))
1910 ;; Always use movemail to rename the file,
1911 ;; since there can be mailboxes in various directories.
1912 (when (not popmail)
1913 ;; On some systems, /usr/spool/mail/foo is a directory
1914 ;; and the actual inbox is /usr/spool/mail/foo/foo.
1915 (if (file-directory-p file)
1916 (setq file (expand-file-name (user-login-name)
1917 file))))
1918 (cond (popmail
1919 (message "Getting mail from the remote server ..."))
1920 ((and (file-exists-p tofile)
1921 (/= 0 (nth 7 (file-attributes tofile))))
1922 (message "Getting mail from %s..." tofile))
1923 ((and (file-exists-p file)
1924 (/= 0 (nth 7 (file-attributes file))))
1925 (message "Getting mail from %s..." file)))
1926 ;; Set TOFILE if have not already done so, and
1927 ;; rename or copy the file FILE to TOFILE if and as appropriate.
1928 (cond ((not renamep)
1929 (setq tofile file))
1930 ((or (file-exists-p tofile) (and (not popmail)
1931 (not (file-exists-p file))))
1932 nil)
1934 (with-temp-buffer
1935 (let ((errors (current-buffer)))
1936 (buffer-disable-undo errors)
1937 (let ((args
1938 (append
1939 (list (or pmail-movemail-program "movemail") nil errors nil)
1940 (if pmail-preserve-inbox
1941 (list "-p")
1942 nil)
1943 (if (pmail-movemail-variant-p 'mailutils)
1944 (append (list "--emacs") pmail-movemail-flags)
1945 pmail-movemail-flags)
1946 (list file tofile)
1947 (if password (list password) nil))))
1948 (apply 'call-process args))
1949 (if (not (buffer-modified-p errors))
1950 ;; No output => movemail won
1952 (set-buffer errors)
1953 (subst-char-in-region (point-min) (point-max)
1954 ?\n ?\ )
1955 (goto-char (point-max))
1956 (skip-chars-backward " \t")
1957 (delete-region (point) (point-max))
1958 (goto-char (point-min))
1959 (if (looking-at "movemail: ")
1960 (delete-region (point-min) (match-end 0)))
1961 (beep t)
1962 ;; If we just read the password, most likely it is
1963 ;; wrong. Otherwise, see if there is a specific
1964 ;; reason to think that the problem is a wrong passwd.
1965 (if (or got-password
1966 (re-search-forward pmail-remote-password-error
1967 nil t))
1968 (pmail-set-remote-password nil))
1970 ;; If using Mailutils, remove initial error code
1971 ;; abbreviation
1972 (when (pmail-movemail-variant-p 'mailutils)
1973 (goto-char (point-min))
1974 (when (looking-at "[A-Z][A-Z0-9_]*:")
1975 (delete-region (point-min) (match-end 0))))
1977 (message "movemail: %s"
1978 (buffer-substring (point-min)
1979 (point-max)))
1981 (sit-for 3)
1982 nil)))))
1984 ;; At this point, TOFILE contains the name to read:
1985 ;; Either the alternate name (if we renamed)
1986 ;; or the actual inbox (if not renaming).
1987 (if (file-exists-p tofile)
1988 (let ((coding-system-for-read 'no-conversion)
1989 size)
1990 (goto-char (point-max))
1991 (setq size (nth 1 (insert-file-contents tofile)))
1992 ;; Determine if a pair of newline message separators need
1993 ;; to be added to the new collection of messages. This is
1994 ;; the case for all new message collections added to a
1995 ;; non-empty mail file.
1996 (unless (zerop size)
1997 (save-restriction
1998 (let ((start (point-min)))
1999 (widen)
2000 (unless (eq start (point-min))
2001 (goto-char start)
2002 (insert "\n\n")
2003 (setq size (+ 2 size))))))
2004 (goto-char (point-max))
2005 (or (= (preceding-char) ?\n)
2006 (zerop size)
2007 (insert ?\n))
2008 (if (not (and pmail-preserve-inbox (string= file tofile)))
2009 (setq delete-files (cons tofile delete-files)))))
2010 (message "")
2011 (setq files (cdr files)))
2012 delete-files))
2014 ;; Decode the region specified by FROM and TO by CODING.
2015 ;; If CODING is nil or an invalid coding system, decode by `undecided'.
2016 (defun pmail-decode-region (from to coding &optional destination)
2017 (if (or (not coding) (not (coding-system-p coding)))
2018 (setq coding 'undecided))
2019 ;; Use -dos decoding, to remove ^M characters left from base64 or
2020 ;; rogue qp-encoded text.
2021 (decode-coding-region
2022 from to (coding-system-change-eol-conversion coding 1) destination)
2023 ;; Don't reveal the fact we used -dos decoding, as users generally
2024 ;; will not expect the PMAIL buffer to use DOS EOL format.
2025 (setq buffer-file-coding-system
2026 (setq last-coding-system-used
2027 (coding-system-change-eol-conversion coding 0))))
2029 (defun pmail-add-header (name value)
2030 "Add a message header named NAME with value VALUE.
2031 The current buffer is narrowed to the headers for some
2032 message (including the blank line separator)."
2033 ;; Position point at the end of the headers but before the blank
2034 ;; line separating the headers from the body.
2035 (goto-char (point-max))
2036 (forward-char -1)
2037 (insert name ": " value "\n"))
2039 (defun pmail-add-mbox-headers ()
2040 "Validate the RFC2822 format for the new messages. Point, at
2041 entry should be looking at the first new message. An error will
2042 be thrown if the new messages are not RCC2822 compliant. Lastly,
2043 unless one already exists, add an Rmail attribute header to the
2044 new messages in the region. Return the number of new messages."
2045 (save-excursion
2046 (let ((count 0)
2047 (start (point))
2048 (value "------U")
2049 limit)
2050 ;; Detect an empty inbox file.
2051 (unless (= start (point-max))
2052 ;; Scan the new messages to establish a count and to insure that
2053 ;; an attribute header is present.
2054 (while (looking-at "From ")
2055 ;; Determine if a new attribute header needs to be added to
2056 ;; the message.
2057 (if (search-forward "\n\n" nil t)
2058 (progn
2059 (setq count (1+ count))
2060 (narrow-to-region start (point))
2061 (unless (mail-fetch-field pmail-attribute-header)
2062 (pmail-add-header pmail-attribute-header value))
2063 (widen))
2064 (pmail-error-bad-format))
2065 ;; Move to the next message.
2066 (if (search-forward "\n\nFrom " nil 'move)
2067 (forward-char -5))
2068 (setq start (point))))
2069 count)))
2071 ;; the pmail-break-forwarded-messages feature is not implemented
2072 (defun pmail-convert-to-babyl-format ()
2073 (let ((count 0) start
2074 (case-fold-search nil)
2075 (buffer-undo-list t)
2076 (invalid-input-resync
2077 (function (lambda ()
2078 (message "Invalid Babyl format in inbox!")
2079 (sit-for 3)
2080 ;; Try to get back in sync with a real message.
2081 (if (re-search-forward
2082 (concat pmail-mmdf-delim1 "\\|^From") nil t)
2083 (beginning-of-line)
2084 (goto-char (point-max)))))))
2085 (goto-char (point-min))
2086 (save-restriction
2087 (while (not (eobp))
2088 (setq start (point))
2089 (cond ((looking-at "BABYL OPTIONS:") ;Babyl header
2090 (if (search-forward "\n\^_" nil t)
2091 ;; If we find the proper terminator, delete through there.
2092 (delete-region (point-min) (point))
2093 (funcall invalid-input-resync)
2094 (delete-region (point-min) (point))))
2095 ;; Babyl format message
2096 ((looking-at "\^L")
2097 (or (search-forward "\n\^_" nil t)
2098 (funcall invalid-input-resync))
2099 (setq count (1+ count))
2100 ;; Make sure there is no extra white space after the ^_
2101 ;; at the end of the message.
2102 ;; Narrowing will make sure that whatever follows the junk
2103 ;; will be treated properly.
2104 (delete-region (point)
2105 (save-excursion
2106 (skip-chars-forward " \t\n")
2107 (point)))
2108 ;; The following let* form was wrapped in a `save-excursion'
2109 ;; which in one case caused infinite looping, see:
2110 ;; http://lists.gnu.org/archive/html/emacs-devel/2008-01/msg00968.html
2111 ;; Removing that form leaves `point' at the end of the
2112 ;; region decoded by `pmail-decode-region' which should
2113 ;; be correct.
2114 (let* ((header-end
2115 (progn
2116 (save-excursion
2117 (goto-char start)
2118 (forward-line 1)
2119 (if (looking-at "0")
2120 (forward-line 1)
2121 (forward-line 2))
2122 (save-restriction
2123 (narrow-to-region (point) (point-max))
2124 (rfc822-goto-eoh)
2125 (point)))))
2126 (case-fold-search t)
2127 (quoted-printable-header-field-end
2128 (save-excursion
2129 (goto-char start)
2130 (re-search-forward
2131 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
2132 header-end t)))
2133 (base64-header-field-end
2134 (save-excursion
2135 (goto-char start)
2136 ;; Don't try to decode non-text data.
2137 (and (re-search-forward
2138 "^content-type:\\(\n?[\t ]\\)\\(text\\|message\\)/"
2139 header-end t)
2140 (goto-char start)
2141 (re-search-forward
2142 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
2143 header-end t)))))
2144 (if quoted-printable-header-field-end
2145 (save-excursion
2146 (unless
2147 (mail-unquote-printable-region header-end (point) nil t t)
2148 (message "Malformed MIME quoted-printable message"))
2149 ;; Change "quoted-printable" to "8bit",
2150 ;; to reflect the decoding we just did.
2151 (goto-char quoted-printable-header-field-end)
2152 (delete-region (point) (search-backward ":"))
2153 (insert ": 8bit")))
2154 (if base64-header-field-end
2155 (save-excursion
2156 (when
2157 (condition-case nil
2158 (progn
2159 (base64-decode-region (1+ header-end)
2160 (- (point) 2))
2162 (error nil))
2163 ;; Change "base64" to "8bit", to reflect the
2164 ;; decoding we just did.
2165 (goto-char base64-header-field-end)
2166 (delete-region (point) (search-backward ":"))
2167 (insert ": 8bit"))))
2168 (setq last-coding-system-used nil)
2169 (or pmail-enable-mime
2170 (not pmail-enable-multibyte)
2171 (let ((mime-charset
2172 (if (and pmail-decode-mime-charset
2173 (save-excursion
2174 (goto-char start)
2175 (search-forward "\n\n" nil t)
2176 (let ((case-fold-search t))
2177 (re-search-backward
2178 pmail-mime-charset-pattern
2179 start t))))
2180 (intern (downcase (match-string 1))))))
2181 (pmail-decode-region start (point) mime-charset))))
2182 ;; Add an X-Coding-System: header if we don't have one.
2183 (save-excursion
2184 (goto-char start)
2185 (forward-line 1)
2186 (if (looking-at "0")
2187 (forward-line 1)
2188 (forward-line 2))
2189 (or (save-restriction
2190 (narrow-to-region (point) (point-max))
2191 (rfc822-goto-eoh)
2192 (goto-char (point-min))
2193 (re-search-forward "^X-Coding-System:" nil t))
2194 (insert "X-Coding-System: "
2195 (symbol-name last-coding-system-used)
2196 "\n")))
2197 (narrow-to-region (point) (point-max))
2198 (and (= 0 (% count 10))
2199 (message "Converting to Babyl format...%d" count)))
2200 ;;*** MMDF format
2201 ((let ((case-fold-search t))
2202 (looking-at pmail-mmdf-delim1))
2203 (let ((case-fold-search t))
2204 (replace-match "\^L\n0, unseen,,\n*** EOOH ***\n")
2205 (re-search-forward pmail-mmdf-delim2 nil t)
2206 (replace-match "\^_"))
2207 (save-excursion
2208 (save-restriction
2209 (narrow-to-region start (1- (point)))
2210 (goto-char (point-min))
2211 (while (search-forward "\n\^_" nil t) ; single char "\^_"
2212 (replace-match "\n^_")))) ; 2 chars: "^" and "_"
2213 (setq last-coding-system-used nil)
2214 (or pmail-enable-mime
2215 (not pmail-enable-multibyte)
2216 (decode-coding-region start (point) 'undecided))
2217 (save-excursion
2218 (goto-char start)
2219 (forward-line 3)
2220 (insert "X-Coding-System: "
2221 (symbol-name last-coding-system-used)
2222 "\n"))
2223 (narrow-to-region (point) (point-max))
2224 (setq count (1+ count))
2225 (and (= 0 (% count 10))
2226 (message "Converting to Babyl format...%d" count)))
2227 ;;*** Mail format
2228 ((looking-at "^From ")
2229 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
2230 (pmail-nuke-pinhead-header)
2231 ;; If this message has a Content-Length field,
2232 ;; skip to the end of the contents.
2233 (let* ((header-end (save-excursion
2234 (and (re-search-forward "\n\n" nil t)
2235 (1- (point)))))
2236 (case-fold-search t)
2237 (quoted-printable-header-field-end
2238 (save-excursion
2239 (re-search-forward
2240 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
2241 header-end t)))
2242 (base64-header-field-end
2243 (and
2244 ;; Don't decode non-text data.
2245 (save-excursion
2246 (re-search-forward
2247 "^content-type:\\(\n?[\t ]\\)\\(text\\|message\\)/"
2248 header-end t))
2249 (save-excursion
2250 (re-search-forward
2251 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
2252 header-end t))))
2253 (size
2254 ;; Get the numeric value from the Content-Length field.
2255 (save-excursion
2256 ;; Back up to end of prev line,
2257 ;; in case the Content-Length field comes first.
2258 (forward-char -1)
2259 (and (search-forward "\ncontent-length: "
2260 header-end t)
2261 (let ((beg (point))
2262 (eol (progn (end-of-line) (point))))
2263 (string-to-number (buffer-substring beg eol)))))))
2264 (and size
2265 (if (and (natnump size)
2266 (<= (+ header-end size) (point-max))
2267 ;; Make sure this would put us at a position
2268 ;; that we could continue from.
2269 (save-excursion
2270 (goto-char (+ header-end size))
2271 (skip-chars-forward "\n")
2272 (or (eobp)
2273 (and (looking-at "BABYL OPTIONS:")
2274 (search-forward "\n\^_" nil t))
2275 (and (looking-at "\^L")
2276 (search-forward "\n\^_" nil t))
2277 (let ((case-fold-search t))
2278 (looking-at pmail-mmdf-delim1))
2279 (looking-at "From "))))
2280 (goto-char (+ header-end size))
2281 (message "Ignoring invalid Content-Length field")
2282 (sit-for 1 0 t)))
2283 (if (let ((case-fold-search nil))
2284 (re-search-forward
2285 (concat "^[\^_]?\\("
2286 pmail-unix-mail-delimiter
2287 "\\|"
2288 pmail-mmdf-delim1 "\\|"
2289 "^BABYL OPTIONS:\\|"
2290 "\^L\n[01],\\)") nil t))
2291 (goto-char (match-beginning 1))
2292 (goto-char (point-max)))
2293 (setq count (1+ count))
2294 (if quoted-printable-header-field-end
2295 (save-excursion
2296 (unless
2297 (mail-unquote-printable-region header-end (point) nil t t)
2298 (message "Malformed MIME quoted-printable message"))
2299 ;; Change "quoted-printable" to "8bit",
2300 ;; to reflect the decoding we just did.
2301 (goto-char quoted-printable-header-field-end)
2302 (delete-region (point) (search-backward ":"))
2303 (insert ": 8bit")))
2304 (if base64-header-field-end
2305 (save-excursion
2306 (when
2307 (condition-case nil
2308 (progn
2309 (base64-decode-region
2310 (1+ header-end)
2311 (save-excursion
2312 ;; Prevent base64-decode-region
2313 ;; from removing newline characters.
2314 (skip-chars-backward "\n\t ")
2315 (point)))
2317 (error nil))
2318 ;; Change "base64" to "8bit", to reflect the
2319 ;; decoding we just did.
2320 (goto-char base64-header-field-end)
2321 (delete-region (point) (search-backward ":"))
2322 (insert ": 8bit")))))
2324 (save-excursion
2325 (save-restriction
2326 (narrow-to-region start (point))
2327 (goto-char (point-min))
2328 (while (search-forward "\n\^_" nil t) ; single char
2329 (replace-match "\n^_")))) ; 2 chars: "^" and "_"
2330 ;; This is for malformed messages that don't end in newline.
2331 ;; There shouldn't be any, but some users say occasionally
2332 ;; there are some.
2333 (or (bolp) (newline))
2334 (insert ?\^_)
2335 (setq last-coding-system-used nil)
2336 (or pmail-enable-mime
2337 (not pmail-enable-multibyte)
2338 (let ((mime-charset
2339 (if (and pmail-decode-mime-charset
2340 (save-excursion
2341 (goto-char start)
2342 (search-forward "\n\n" nil t)
2343 (let ((case-fold-search t))
2344 (re-search-backward
2345 pmail-mime-charset-pattern
2346 start t))))
2347 (intern (downcase (match-string 1))))))
2348 (pmail-decode-region start (point) mime-charset)))
2349 (save-excursion
2350 (goto-char start)
2351 (forward-line 3)
2352 (insert "X-Coding-System: "
2353 (symbol-name last-coding-system-used)
2354 "\n"))
2355 (narrow-to-region (point) (point-max))
2356 (and (= 0 (% count 10))
2357 (message "Converting to Babyl format...%d" count)))
2359 ;; This kludge is because some versions of sendmail.el
2360 ;; insert an extra newline at the beginning that shouldn't
2361 ;; be there. sendmail.el has been fixed, but old versions
2362 ;; may still be in use. -- rms, 7 May 1993.
2363 ((eolp) (delete-char 1))
2364 (t (error "Cannot convert to babyl format")))))
2365 (setq buffer-undo-list nil)
2366 count))
2368 ;; Delete the "From ..." line, creating various other headers with
2369 ;; information from it if they don't already exist. Now puts the
2370 ;; original line into a mail-from: header line for debugging and for
2371 ;; use by the pmail-output function.
2372 (defun pmail-nuke-pinhead-header ()
2373 (save-excursion
2374 (save-restriction
2375 (let ((start (point))
2376 (end (progn
2377 (condition-case ()
2378 (search-forward "\n\n")
2379 (error
2380 (goto-char (point-max))
2381 (insert "\n\n")))
2382 (point)))
2383 has-from has-date)
2384 (narrow-to-region start end)
2385 (let ((case-fold-search t))
2386 (goto-char start)
2387 (setq has-from (search-forward "\nFrom:" nil t))
2388 (goto-char start)
2389 (setq has-date (and (search-forward "\nDate:" nil t) (point)))
2390 (goto-char start))
2391 (let ((case-fold-search nil))
2392 (if (re-search-forward (concat "^" pmail-unix-mail-delimiter) nil t)
2393 (replace-match
2394 (concat
2395 "Mail-from: \\&"
2396 ;; Keep and reformat the date if we don't
2397 ;; have a Date: field.
2398 (if has-date
2400 (concat
2401 "Date: \\2, \\4 \\3 \\9 \\5 "
2403 ;; The timezone could be matched by group 7 or group 10.
2404 ;; If neither of them matched, assume EST, since only
2405 ;; Easterners would be so sloppy.
2406 ;; It's a shame the substitution can't use "\\10".
2407 (cond
2408 ((/= (match-beginning 7) (match-end 7)) "\\7")
2409 ((/= (match-beginning 10) (match-end 10))
2410 (buffer-substring (match-beginning 10)
2411 (match-end 10)))
2412 (t "EST"))
2413 "\n"))
2414 ;; Keep and reformat the sender if we don't
2415 ;; have a From: field.
2416 (if has-from
2418 "From: \\1\n"))
2419 t)))))))
2421 ;;;; *** Pmail Message Formatting and Header Manipulation ***
2423 (defun pmail-copy-headers (beg end &optional ignored-headers)
2424 "Copy displayed header fields to the message viewer buffer.
2425 BEG and END marks the start and end positions of the message in
2426 the mail buffer. If the optional argument IGNORED-HEADERS is
2427 non-nil, ignore all header fields whose names match that regexp.
2428 Otherwise, if `rmail-displayed-headers' is non-nil, copy only
2429 those header fields whose names match that regexp. Otherwise,
2430 copy all header fields whose names do not match
2431 `rmail-ignored-headers' (unless they also match
2432 `rmail-nonignored-headers')."
2433 (let ((header-start-regexp "\n[^ \t]")
2434 lim)
2435 (with-current-buffer pmail-buffer
2436 (when (search-forward "\n\n" nil t)
2437 (forward-char -1)
2438 (save-restriction
2439 ;; Put point right after the From header line.
2440 (narrow-to-region beg (point))
2441 (goto-char (point-min))
2442 (unless (re-search-forward header-start-regexp nil t)
2443 (pmail-error-bad-format))
2444 (forward-char -1)
2445 (cond
2446 ;; Handle the case where all headers should be copied.
2447 ((eq pmail-header-style 'full)
2448 (prepend-to-buffer pmail-view-buffer beg (point-max)))
2449 ;; Handle the case where the headers matching the diplayed
2450 ;; headers regexp should be copied.
2451 ((and pmail-displayed-headers (null ignored-headers))
2452 (while (not (eobp))
2453 (save-excursion
2454 (setq lim (if (re-search-forward header-start-regexp nil t)
2455 (1+ (match-beginning 0))
2456 (point-max))))
2457 (when (looking-at pmail-displayed-headers)
2458 (append-to-buffer pmail-view-buffer (point) lim))
2459 (goto-char lim)))
2460 ;; Handle the ignored headers.
2461 ((or ignored-headers (setq ignored-headers pmail-ignored-headers))
2462 (while (and ignored-headers (not (eobp)))
2463 (save-excursion
2464 (setq lim (if (re-search-forward header-start-regexp nil t)
2465 (1+ (match-beginning 0))
2466 (point-max))))
2467 (if (and (looking-at ignored-headers)
2468 (not (looking-at pmail-nonignored-headers)))
2469 (goto-char lim)
2470 (append-to-buffer pmail-view-buffer (point) lim)
2471 (goto-char lim))))
2472 (t (error "No headers selected for display!"))))))))
2474 (defun pmail-toggle-header (&optional arg)
2475 "Show original message header if pruned header currently shown, or vice versa.
2476 With argument ARG, show the message header pruned if ARG is greater than zero;
2477 otherwise, show it in full."
2478 (interactive "P")
2479 (setq pmail-header-style
2480 (cond
2481 ((and (numberp arg) (> arg 0)) 'normal)
2482 ((eq pmail-header-style 'full) 'normal)
2483 (t 'full)))
2484 (pmail-show-message-maybe))
2486 ;; Lifted from repos-count-screen-lines.
2487 ;; Return number of screen lines between START and END.
2488 (defun pmail-count-screen-lines (start end)
2489 (save-excursion
2490 (save-restriction
2491 (narrow-to-region start end)
2492 (goto-char (point-min))
2493 (vertical-motion (- (point-max) (point-min))))))
2495 ;;;; *** Pmail Attributes and Keywords ***
2497 (defun pmail-get-header (name &optional msg)
2498 "Return the value of message header NAME, nil if no such header
2499 exists. MSG, if set identifies the message number to use. The
2500 current mail message will be used otherwise."
2501 (save-excursion
2502 (save-restriction
2503 (with-current-buffer pmail-buffer
2504 (widen)
2505 (let* ((n (or msg pmail-current-message))
2506 (beg (pmail-msgbeg n))
2507 end)
2508 (goto-char beg)
2509 (setq end (search-forward "\n\n" nil t))
2510 (if end
2511 (progn
2512 (narrow-to-region beg end)
2513 (mail-fetch-field name))
2514 (pmail-error-bad-format msg)))))))
2516 (defun pmail-get-attr-names (&optional msg)
2517 "Return the message attributes in a comma separated string.
2518 MSG, if set identifies the message number to use. The current
2519 mail message will be used otherwise."
2520 (let ((value (pmail-get-header pmail-attribute-header msg))
2521 result temp)
2522 (dotimes (index (length value))
2523 (setq temp (and (not (= ?- (aref value index)))
2524 (nth 1 (aref pmail-attr-array index)))
2525 result
2526 (cond
2527 ((and temp result) (format "%s, %s" result temp))
2528 (temp temp)
2529 (t result))))
2530 result))
2532 (defun pmail-get-keywords (&optional msg)
2533 "Return the message keywords in a comma separated string.
2534 MSG, if set identifies the message number to use. The current
2535 mail message will be used otherwise."
2536 (pmail-get-header pmail-keyword-header msg))
2538 (defun pmail-display-labels ()
2539 "Update the mode line with the (set) attributes and keywords
2540 for the current message."
2541 (let (blurb attr-names keywords)
2542 ;; Combine the message attributes and keywords into a comma
2543 ;; separated list.
2544 (setq attr-names (pmail-get-attr-names pmail-current-message)
2545 keywords (pmail-get-keywords pmail-current-message))
2546 (setq blurb
2547 (cond
2548 ((and attr-names keywords) (concat attr-names ", " keywords))
2549 (attr-names attr-names)
2550 (keywords keywords)
2551 (t "")))
2552 (setq mode-line-process
2553 (format " %d/%d%s"
2554 pmail-current-message pmail-total-messages blurb))
2555 ;; If pmail-enable-mime is non-nil, we may have to update
2556 ;; `mode-line-process' of pmail-view-buffer too.
2557 (if (and pmail-enable-mime
2558 (not (eq (current-buffer) pmail-view-buffer))
2559 (buffer-live-p pmail-view-buffer))
2560 (let ((mlp mode-line-process))
2561 (with-current-buffer pmail-view-buffer
2562 (setq mode-line-process mlp))))))
2564 (defun pmail-get-attr-value (attr state)
2565 "Return the character value for ATTR.
2566 ATTR is a (numeric) index, an offset into the mbox attribute
2567 header value. STATE is one of nil, t, or a character value."
2568 (cond
2569 ((numberp state) state)
2570 ((not state) ?-)
2571 (t (nth 0 (aref pmail-attr-array attr)))))
2573 (defun pmail-set-attribute (attr state &optional msgnum)
2574 "Turn an attribute of a message on or off according to STATE.
2575 STATE is either nil or the character (numeric) value associated
2576 with the state (nil represents off and non-nil represents on).
2577 ATTR is the index of the attribute. MSGNUM is message number to
2578 change; nil means current message."
2579 (set-buffer pmail-buffer)
2580 (let ((value (pmail-get-attr-value attr state))
2581 (omax (point-max-marker))
2582 (omin (point-min-marker))
2583 (buffer-read-only nil)
2584 limit)
2585 (or msgnum (setq msgnum pmail-current-message))
2586 (if (> msgnum 0)
2587 (unwind-protect
2588 (save-excursion
2589 ;; Determine if the current state is the desired state.
2590 (widen)
2591 (goto-char (pmail-msgbeg msgnum))
2592 (save-excursion
2593 (setq limit (search-forward "\n\n" nil t)))
2594 (when (search-forward (concat pmail-attribute-header ": ") limit t)
2595 (forward-char attr)
2596 (when (/= value (char-after))
2597 (delete-char 1)
2598 (insert value)))
2599 (if (= attr pmail-deleted-attr-index)
2600 (pmail-set-message-deleted-p msgnum state)))
2601 ;; Note: we don't use save-restriction because that does not work right
2602 ;; if changes are made outside the saved restriction
2603 ;; before that restriction is restored.
2604 (narrow-to-region omin omax)
2605 (set-marker omin nil)
2606 (set-marker omax nil)
2607 (if (= msgnum pmail-current-message)
2608 (pmail-display-labels))))))
2610 (defun pmail-message-attr-p (msg attrs)
2611 "Return t if the attributes header for message MSG contains a
2612 match for the regexp ATTRS."
2613 (save-excursion
2614 (save-restriction
2615 (let ((start (pmail-msgbeg msg))
2616 limit)
2617 (widen)
2618 (goto-char start)
2619 (setq limit (search-forward "\n\n" (pmail-msgend msg) t))
2620 (goto-char start)
2621 (and limit
2622 (search-forward (concat pmail-attribute-header ": ") limit t)
2623 (looking-at attrs))))))
2625 (defun pmail-message-unseen-p (msgnum)
2626 "Test the unseen attribute for message MSGNUM.
2627 Return non-nil if the unseen attribute is set, nil otherwise."
2628 (pmail-message-attr-p msgnum "......U"))
2631 ;;;; *** Pmail Message Selection And Support ***
2633 ;; (defun pmail-get-collection-buffer ()
2634 ;; "Return the buffer containing the mbox formatted messages."
2635 ;; (if (eq major-mode 'pmail-mode)
2636 ;; (if pmail-buffers-swapped-p
2637 ;; pmail-view-buffer
2638 ;; pmail-buffer)
2639 ;; (error "The current buffer must be in Pmail mode.")))
2641 (defun pmail-use-collection-buffer ()
2642 "Insure that the Pmail buffer contains the message collection.
2643 Return the current message number if the Pmail buffer is in a
2644 swapped state, i.e. it currently contains a single decoded
2645 message rather than an entire message collection, nil otherwise."
2646 (let (result)
2647 (when pmail-buffers-swapped-p
2648 (buffer-swap-text pmail-view-buffer)
2649 (setq pmail-buffers-swapped-p nil
2650 result pmail-current-message))
2651 result))
2653 (defun pmail-use-viewer-buffer (&optional msgnum)
2654 "Insure that the Pmail buffer contains the current message.
2655 If message MSGNUM is non-nil make it the current message and
2656 display it. Return nil."
2657 (let (result)
2658 (cond
2659 ((not pmail-buffers-swapped-p)
2660 (let ((message (or msgnum pmail-current-message)))
2661 (pmail-show-message message)))
2662 ((and msgnum (/= msgnum pmail-current-message))
2663 (pmail-show-message msgnum))
2664 (t))
2665 result))
2667 (defun pmail-msgend (n)
2668 (marker-position (aref pmail-message-vector (1+ n))))
2670 (defun pmail-msgbeg (n)
2671 (marker-position (aref pmail-message-vector n)))
2673 (defun pmail-widen-to-current-msgbeg (function)
2674 "Call FUNCTION with point at start of internal data of current message.
2675 Assumes that bounds were previously narrowed to display the message in Pmail.
2676 The bounds are widened enough to move point where desired, then narrowed
2677 again afterward.
2679 FUNCTION may not change the visible text of the message, but it may
2680 change the invisible header text."
2681 (save-excursion
2682 (unwind-protect
2683 (progn
2684 (narrow-to-region (pmail-msgbeg pmail-current-message)
2685 (point-max))
2686 (goto-char (point-min))
2687 (funcall function))
2688 ;; Note: we don't use save-restriction because that does not work right
2689 ;; if changes are made outside the saved restriction
2690 ;; before that restriction is restored.
2691 (narrow-to-region (pmail-msgbeg pmail-current-message)
2692 (pmail-msgend pmail-current-message)))))
2694 (defun pmail-forget-messages ()
2695 (unwind-protect
2696 (if (vectorp pmail-message-vector)
2697 (let* ((i 0)
2698 (v pmail-message-vector)
2699 (n (length v)))
2700 (while (< i n)
2701 (move-marker (aref v i) nil)
2702 (setq i (1+ i)))))
2703 (setq pmail-message-vector nil)
2704 (setq pmail-msgref-vector nil)
2705 (setq pmail-deleted-vector nil)))
2707 (defun pmail-maybe-set-message-counters ()
2708 (if (not (and pmail-deleted-vector
2709 pmail-message-vector
2710 pmail-current-message
2711 pmail-total-messages))
2712 (pmail-set-message-counters)))
2714 (defun pmail-count-new-messages (&optional nomsg)
2715 "Count the number of new messages in the region.
2716 Output a helpful message unless NOMSG is non-nil."
2717 (let* ((case-fold-search nil)
2718 (total-messages 0)
2719 (messages-head nil)
2720 (deleted-head nil))
2721 (or nomsg (message "Counting new messages..."))
2722 (goto-char (point-max))
2723 ;; Put at the end of messages-head
2724 ;; the entry for message N+1, which marks
2725 ;; the end of message N. (N = number of messages).
2726 (setq messages-head (list (point-marker)))
2727 (pmail-set-message-counters-counter (point-min))
2728 (setq pmail-current-message (1+ pmail-total-messages))
2729 (setq pmail-total-messages
2730 (+ pmail-total-messages total-messages))
2731 (setq pmail-message-vector
2732 (vconcat pmail-message-vector (cdr messages-head)))
2733 (aset pmail-message-vector
2734 pmail-current-message (car messages-head))
2735 (setq pmail-deleted-vector
2736 (concat pmail-deleted-vector deleted-head))
2737 (setq pmail-summary-vector
2738 (vconcat pmail-summary-vector (make-vector total-messages nil)))
2739 (setq pmail-msgref-vector
2740 (vconcat pmail-msgref-vector (make-vector total-messages nil)))
2741 ;; Fill in the new elements of pmail-msgref-vector.
2742 (let ((i (1+ (- pmail-total-messages total-messages))))
2743 (while (<= i pmail-total-messages)
2744 (aset pmail-msgref-vector i (list i))
2745 (setq i (1+ i))))
2746 (goto-char (point-min))
2747 (or nomsg (message "Counting new messages...done (%d)" total-messages))))
2749 (defun pmail-set-message-counters ()
2750 (pmail-forget-messages)
2751 (save-excursion
2752 (save-restriction
2753 (widen)
2754 (let* ((point-save (point))
2755 (total-messages 0)
2756 (messages-after-point)
2757 (case-fold-search nil)
2758 (messages-head nil)
2759 (deleted-head nil))
2760 ;; Determine how many messages follow point.
2761 (message "Counting messages...")
2762 (goto-char (point-max))
2763 ;; Put at the end of messages-head
2764 ;; the entry for message N+1, which marks
2765 ;; the end of message N. (N = number of messages).
2766 (setq messages-head (list (point-marker)))
2767 (pmail-set-message-counters-counter (min (point) point-save))
2768 (setq messages-after-point total-messages)
2770 ;; Determine how many precede point.
2771 (pmail-set-message-counters-counter)
2772 (setq pmail-total-messages total-messages)
2773 (setq pmail-current-message
2774 (min total-messages
2775 (max 1 (- total-messages messages-after-point))))
2776 (setq pmail-message-vector
2777 (apply 'vector (cons (point-min-marker) messages-head))
2778 pmail-deleted-vector (concat "0" deleted-head)
2779 pmail-summary-vector (make-vector pmail-total-messages nil)
2780 pmail-msgref-vector (make-vector (1+ pmail-total-messages) nil))
2781 (let ((i 0))
2782 (while (<= i pmail-total-messages)
2783 (aset pmail-msgref-vector i (list i))
2784 (setq i (1+ i))))
2785 (message "Counting messages...done")))))
2788 (defsubst pmail-collect-deleted (message-end)
2789 "Collect the message deletion flags for each message.
2790 MESSAGE-END is the buffer position corresponding to the end of
2791 the message. Point is at the beginning of the message."
2792 ;; NOTE: This piece of code will be executed on a per-message basis.
2793 ;; In the face of thousands of messages, it has to be as fast as
2794 ;; possible, hence some brute force constant use is employed in
2795 ;; addition to inlining.
2796 (save-excursion
2797 (setq deleted-head
2798 (cons (if (and (search-forward (concat pmail-attribute-header ": ") message-end t)
2799 (looking-at "?D"))
2801 ?\ ) deleted-head))))
2803 (defun pmail-set-message-counters-counter (&optional stop)
2804 ;; Collect the start position for each message into 'messages-head.
2805 (let ((start (point)))
2806 (while (search-backward "\n\nFrom " stop t)
2807 (forward-char 2)
2808 (pmail-collect-deleted start)
2809 ;; Show progress after every 20 messages or so.
2810 (setq messages-head (cons (point-marker) messages-head)
2811 total-messages (1+ total-messages)
2812 start (point))
2813 (if (zerop (% total-messages 20))
2814 (message "Counting messages...%d" total-messages)))
2815 ;; Handle the first message, maybe.
2816 (if stop
2817 (goto-char stop)
2818 (goto-char (point-min)))
2819 (unless (not (looking-at "From "))
2820 (pmail-collect-deleted start)
2821 (setq messages-head (cons (point-marker) messages-head)
2822 total-messages (1+ total-messages)))))
2824 (defun pmail-beginning-of-message ()
2825 "Show current message starting from the beginning."
2826 (interactive)
2827 (let ((pmail-show-message-hook
2828 (list (function (lambda ()
2829 (goto-char (point-min)))))))
2830 (pmail-show-message-maybe pmail-current-message)))
2832 (defun pmail-end-of-message ()
2833 "Show bottom of current message."
2834 (interactive)
2835 (let ((pmail-show-message-hook
2836 (list (function (lambda ()
2837 (goto-char (point-max))
2838 (recenter (1- (window-height))))))))
2839 (pmail-show-message-maybe pmail-current-message)))
2841 (defun pmail-unknown-mail-followup-to ()
2842 "Handle a \"Mail-Followup-To\" header field with an unknown mailing list.
2843 Ask the user whether to add that list name to `mail-mailing-lists'."
2844 (save-restriction
2845 (let ((mail-followup-to (mail-fetch-field "mail-followup-to" nil t)))
2846 (when mail-followup-to
2847 (let ((addresses
2848 (split-string
2849 (mail-strip-quoted-names mail-followup-to)
2850 ",[[:space:]]+" t)))
2851 (dolist (addr addresses)
2852 (when (and (not (member addr mail-mailing-lists))
2853 (not
2854 ;; taken from pmailsum.el
2855 (string-match
2856 (or pmail-user-mail-address-regexp
2857 (concat "^\\("
2858 (regexp-quote (user-login-name))
2859 "\\($\\|@\\)\\|"
2860 (regexp-quote
2861 (or user-mail-address
2862 (concat (user-login-name) "@"
2863 (or mail-host-address
2864 (system-name)))))
2865 "\\>\\)"))
2866 addr))
2867 (y-or-n-p
2868 (format "Add `%s' to `mail-mailing-lists'? "
2869 addr)))
2870 (customize-save-variable 'mail-mailing-lists
2871 (cons addr mail-mailing-lists)))))))))
2873 (defun pmail-swap-buffers-maybe ()
2874 "Determine if the Pmail buffer is showing a message.
2875 If so restore the actual mbox message collection."
2876 (unless (not pmail-buffers-swapped-p)
2877 (with-current-buffer pmail-buffer
2878 (buffer-swap-text pmail-view-buffer)
2879 (setq pmail-buffers-swapped-p nil))))
2881 (defun pmail-show-message-maybe (&optional n no-summary)
2882 "Show message number N (prefix argument), counting from start of file.
2883 If summary buffer is currently displayed, update current message there also."
2884 (interactive "p")
2885 (or (eq major-mode 'pmail-mode)
2886 (switch-to-buffer pmail-buffer))
2887 (pmail-swap-buffers-maybe)
2888 (pmail-maybe-set-message-counters)
2889 (widen)
2890 (let ((msgnum (or n pmail-current-message))
2891 blurb)
2892 (if (zerop pmail-total-messages)
2893 (save-excursion
2894 (with-current-buffer pmail-view-buffer
2895 (erase-buffer)
2896 (setq blurb "No mail.")))
2897 (setq blurb (pmail-show-message msgnum))
2898 (when mail-mailing-lists
2899 (pmail-unknown-mail-followup-to))
2900 (if transient-mark-mode (deactivate-mark))
2901 ;; If there is a summary buffer, try to move to this message
2902 ;; in that buffer. But don't complain if this message
2903 ;; is not mentioned in the summary.
2904 ;; Don't do this at all if we were called on behalf
2905 ;; of cursor motion in the summary buffer.
2906 (and (pmail-summary-exists) (not no-summary)
2907 (let ((curr-msg pmail-current-message))
2908 (pmail-select-summary
2909 (pmail-summary-goto-msg curr-msg t t))))
2910 (with-current-buffer pmail-buffer
2911 (pmail-auto-file)))
2912 (if blurb
2913 (message blurb))))
2915 (defun pmail-is-text-p ()
2916 "Return t if the region contains a text message, nil
2917 otherwise."
2918 (save-excursion
2919 (let ((text-regexp "\\(text\\|message\\)/")
2920 (content-type-header (mail-fetch-field "content-type")))
2921 ;; The message is text if either there is no content type header
2922 ;; (a default of "text/plain; charset=US-ASCII" is assumed) or
2923 ;; the base content type is either text or message.
2924 (or (not content-type-header)
2925 (string-match text-regexp content-type-header)))))
2927 (defun pmail-show-message (&optional msg)
2928 "Show message MSG using a special view buffer.
2929 Return text to display in the minibuffer if MSG is out of
2930 range (displaying a reasonable choice as well), nil otherwise.
2931 The current mail message becomes the message displayed."
2932 (let ((mbox-buf pmail-buffer)
2933 (view-buf pmail-view-buffer)
2934 blurb beg end body-start coding-system character-coding is-text-message)
2935 (if (not msg)
2936 (setq msg pmail-current-message))
2937 (cond ((<= msg 0)
2938 (setq msg 1
2939 pmail-current-message 1
2940 blurb "No previous message"))
2941 ((> msg pmail-total-messages)
2942 (setq msg pmail-total-messages
2943 pmail-current-message pmail-total-messages
2944 blurb "No following message"))
2945 (t (setq pmail-current-message msg)))
2946 (with-current-buffer pmail-buffer
2947 ;; Mark the message as seen, bracket the message in the mail
2948 ;; buffer and determine the coding system the transfer encoding.
2949 (pmail-set-attribute pmail-unseen-attr-index nil)
2950 (setq beg (pmail-msgbeg msg)
2951 end (pmail-msgend msg))
2952 (widen)
2953 (narrow-to-region beg end)
2954 (goto-char beg)
2955 (setq body-start (search-forward "\n\n" nil t))
2956 (narrow-to-region beg (point))
2957 (goto-char beg)
2958 (setq character-coding (mail-fetch-field "content-transfer-encoding")
2959 is-text-message (pmail-is-text-p)
2960 coding-system (pmail-get-coding-system))
2961 (widen)
2962 (narrow-to-region beg end)
2963 ;; Decode the message body into an empty view buffer using a
2964 ;; unibyte temporary buffer where the character decoding takes
2965 ;; place.
2966 (with-current-buffer pmail-view-buffer
2967 (erase-buffer))
2968 (with-temp-buffer
2969 (set-buffer-multibyte nil)
2970 (insert-buffer-substring mbox-buf body-start end)
2971 (cond
2972 ((string= character-coding "quoted-printable")
2973 (mail-unquote-printable-region (point-min) (point-max)))
2974 ((and (string= character-coding "base64") is-text-message)
2975 (base64-decode-region (point-min) (point-max)))
2976 ((eq character-coding 'uuencode)
2977 (error "Not supported yet."))
2978 (t))
2979 (pmail-decode-region (point-min) (point-max) coding-system view-buf))
2980 ;; Copy the headers to the front of the message view buffer.
2981 (with-current-buffer pmail-view-buffer
2982 (goto-char (point-min)))
2983 (pmail-copy-headers beg end)
2984 ;; Add the separator (blank line) between headers and body;
2985 ;; highlight the message, activate any URL like text and add
2986 ;; special highlighting for and quoted material.
2987 (with-current-buffer pmail-view-buffer
2988 (insert "\n")
2989 (goto-char (point-min))
2990 (pmail-highlight-headers)
2991 ;(pmail-activate-urls)
2992 ;(pmail-process-quoted-material)
2994 ;; Update the mode-line with message status information and swap
2995 ;; the view buffer/mail buffer contents.
2996 (pmail-display-labels)
2997 (buffer-swap-text pmail-view-buffer)
2998 (setq pmail-buffers-swapped-p t)
2999 (run-hooks 'pmail-show-message-hook))
3000 blurb))
3002 ;; Find all occurrences of certain fields, and highlight them.
3003 (defun pmail-highlight-headers ()
3004 ;; Do this only if the system supports faces.
3005 (if (and (fboundp 'internal-find-face)
3006 pmail-highlighted-headers)
3007 (save-excursion
3008 (search-forward "\n\n" nil 'move)
3009 (save-restriction
3010 (narrow-to-region (point-min) (point))
3011 (let ((case-fold-search t)
3012 (inhibit-read-only t)
3013 ;; Highlight with boldface if that is available.
3014 ;; Otherwise use the `highlight' face.
3015 (face (or 'pmail-highlight
3016 (if (face-differs-from-default-p 'bold)
3017 'bold 'highlight)))
3018 ;; List of overlays to reuse.
3019 (overlays pmail-overlay-list))
3020 (goto-char (point-min))
3021 (while (re-search-forward pmail-highlighted-headers nil t)
3022 (skip-chars-forward " \t")
3023 (let ((beg (point))
3024 overlay)
3025 (while (progn (forward-line 1)
3026 (looking-at "[ \t]")))
3027 ;; Back up over newline, then trailing spaces or tabs
3028 (forward-char -1)
3029 (while (member (preceding-char) '(? ?\t))
3030 (forward-char -1))
3031 (if overlays
3032 ;; Reuse an overlay we already have.
3033 (progn
3034 (setq overlay (car overlays)
3035 overlays (cdr overlays))
3036 (overlay-put overlay 'face face)
3037 (move-overlay overlay beg (point)))
3038 ;; Make a new overlay and add it to
3039 ;; pmail-overlay-list.
3040 (setq overlay (make-overlay beg (point)))
3041 (overlay-put overlay 'face face)
3042 (setq pmail-overlay-list
3043 (cons overlay pmail-overlay-list))))))))))
3045 (defun pmail-auto-file ()
3046 "Automatically move a message into a sub-folder based on criteria.
3047 Called when a new message is displayed."
3048 (if (or (zerop pmail-total-messages)
3049 (pmail-message-attr-p pmail-current-message "...F...")
3050 (not (string= (buffer-file-name)
3051 (expand-file-name pmail-file-name))))
3052 ;; Do nothing if the message has already been filed or if there
3053 ;; are no messages.
3055 ;; Find out some basics (common fields)
3056 (let ((from (mail-fetch-field "from"))
3057 (subj (mail-fetch-field "subject"))
3058 (to (concat (mail-fetch-field "to") "," (mail-fetch-field "cc")))
3059 (d pmail-automatic-folder-directives)
3060 (directive-loop nil)
3061 (folder nil))
3062 (while d
3063 (setq folder (car (car d))
3064 directive-loop (cdr (car d)))
3065 (while (and (car directive-loop)
3066 (let ((f (cond
3067 ((string= (car directive-loop) "from") from)
3068 ((string= (car directive-loop) "to") to)
3069 ((string= (car directive-loop) "subject") subj)
3070 (t (mail-fetch-field (car directive-loop))))))
3071 (and f (string-match (car (cdr directive-loop)) f))))
3072 (setq directive-loop (cdr (cdr directive-loop))))
3073 ;; If there are no directives left, then it was a complete match.
3074 (if (null directive-loop)
3075 (if (null folder)
3076 (pmail-delete-forward)
3077 (if (string= "/dev/null" folder)
3078 (pmail-delete-message)
3079 (pmail-output-to-pmail-file folder 1 t)
3080 (setq d nil))))
3081 (setq d (cdr d))))))
3083 (defun pmail-next-message (n)
3084 "Show following message whether deleted or not.
3085 With prefix arg N, moves forward N messages, or backward if N is negative."
3086 (interactive "p")
3087 (set-buffer pmail-buffer)
3088 (pmail-maybe-set-message-counters)
3089 (pmail-show-message-maybe (+ pmail-current-message n)))
3091 (defun pmail-previous-message (n)
3092 "Show previous message whether deleted or not.
3093 With prefix arg N, moves backward N messages, or forward if N is negative."
3094 (interactive "p")
3095 (pmail-next-message (- n)))
3097 (defun pmail-next-undeleted-message (n)
3098 "Show following non-deleted message.
3099 With prefix arg N, moves forward N non-deleted messages,
3100 or backward if N is negative.
3102 Returns t if a new message is being shown, nil otherwise."
3103 (interactive "p")
3104 (set-buffer pmail-buffer)
3105 (pmail-maybe-set-message-counters)
3106 (let ((lastwin pmail-current-message)
3107 (current pmail-current-message))
3108 (while (and (> n 0) (< current pmail-total-messages))
3109 (setq current (1+ current))
3110 (if (not (pmail-message-deleted-p current))
3111 (setq lastwin current n (1- n))))
3112 (while (and (< n 0) (> current 1))
3113 (setq current (1- current))
3114 (if (not (pmail-message-deleted-p current))
3115 (setq lastwin current n (1+ n))))
3116 (if (/= lastwin pmail-current-message)
3117 (progn (pmail-show-message-maybe lastwin)
3119 (if (< n 0)
3120 (message "No previous nondeleted message"))
3121 (if (> n 0)
3122 (message "No following nondeleted message"))
3123 nil)))
3125 (defun pmail-previous-undeleted-message (n)
3126 "Show previous non-deleted message.
3127 With prefix argument N, moves backward N non-deleted messages,
3128 or forward if N is negative."
3129 (interactive "p")
3130 (pmail-next-undeleted-message (- n)))
3132 (defun pmail-first-message ()
3133 "Show first message in file."
3134 (interactive)
3135 (pmail-maybe-set-message-counters)
3136 (pmail-show-message-maybe (< 1 pmail-total-messages)))
3138 (defun pmail-last-message ()
3139 "Show last message in file."
3140 (interactive)
3141 (pmail-maybe-set-message-counters)
3142 (pmail-show-message-maybe pmail-total-messages))
3144 (defun pmail-what-message ()
3145 (let ((where (point))
3146 (low 1)
3147 (high pmail-total-messages)
3148 (mid (/ pmail-total-messages 2)))
3149 (while (> (- high low) 1)
3150 (if (>= where (pmail-msgbeg mid))
3151 (setq low mid)
3152 (setq high mid))
3153 (setq mid (+ low (/ (- high low) 2))))
3154 (if (>= where (pmail-msgbeg high)) high low)))
3156 (defun pmail-message-recipients-p (msg recipients &optional primary-only)
3157 (save-restriction
3158 (goto-char (pmail-msgbeg msg))
3159 (search-forward "\n*** EOOH ***\n")
3160 (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
3161 (or (string-match recipients (or (mail-fetch-field "To") ""))
3162 (string-match recipients (or (mail-fetch-field "From") ""))
3163 (if (not primary-only)
3164 (string-match recipients (or (mail-fetch-field "Cc") ""))))))
3166 (defun pmail-message-regexp-p (n regexp)
3167 "Return t, if for message number N, regexp REGEXP matches in the header."
3168 (let ((beg (pmail-msgbeg n))
3169 (end (pmail-msgend n)))
3170 (goto-char beg)
3171 (forward-line 1)
3172 (save-excursion
3173 (save-restriction
3174 (if (prog1 (= (following-char) ?0)
3175 (forward-line 2)
3176 ;; If there's a Summary-line in the (otherwise empty)
3177 ;; header, we didn't yet get past the EOOH line.
3178 (when (looking-at "^\\*\\*\\* EOOH \\*\\*\\*\n")
3179 (forward-line 1))
3180 (setq beg (point))
3181 (narrow-to-region (point) end))
3182 (progn
3183 (rfc822-goto-eoh)
3184 (setq end (point)))
3185 (setq beg (point))
3186 (search-forward "\n*** EOOH ***\n" end t)
3187 (setq end (1+ (match-beginning 0)))))
3188 (goto-char beg)
3189 (if pmail-enable-mime
3190 (funcall pmail-search-mime-header-function n regexp end)
3191 (re-search-forward regexp end t)))))
3193 (defun pmail-search-message (msg regexp)
3194 "Return non-nil, if for message number MSG, regexp REGEXP matches."
3195 (goto-char (pmail-msgbeg msg))
3196 (if pmail-enable-mime
3197 (funcall pmail-search-mime-message-function msg regexp)
3198 (re-search-forward regexp (pmail-msgend msg) t)))
3200 (defvar pmail-search-last-regexp nil)
3201 (defun pmail-search (regexp &optional n)
3202 "Show message containing next match for REGEXP (but not the current msg).
3203 Prefix argument gives repeat count; negative argument means search
3204 backwards (through earlier messages).
3205 Interactively, empty argument means use same regexp used last time."
3206 (interactive
3207 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
3208 (prompt
3209 (concat (if reversep "Reverse " "") "Pmail search (regexp"))
3210 regexp)
3211 (setq prompt
3212 (concat prompt
3213 (if pmail-search-last-regexp
3214 (concat ", default "
3215 pmail-search-last-regexp "): ")
3216 "): ")))
3217 (setq regexp (read-string prompt))
3218 (cond ((not (equal regexp ""))
3219 (setq pmail-search-last-regexp regexp))
3220 ((not pmail-search-last-regexp)
3221 (error "No previous Pmail search string")))
3222 (list pmail-search-last-regexp
3223 (prefix-numeric-value current-prefix-arg))))
3224 (or n (setq n 1))
3225 (message "%sPmail search for %s..."
3226 (if (< n 0) "Reverse " "")
3227 regexp)
3228 (set-buffer pmail-buffer)
3229 (pmail-maybe-set-message-counters)
3230 (let ((omin (point-min))
3231 (omax (point-max))
3232 (opoint (point))
3234 (reversep (< n 0))
3235 (msg pmail-current-message))
3236 (unwind-protect
3237 (progn
3238 (widen)
3239 (while (/= n 0)
3240 ;; Check messages one by one, advancing message number up or down
3241 ;; but searching forward through each message.
3242 (if reversep
3243 (while (and (null win) (> msg 1))
3244 (setq msg (1- msg)
3245 win (pmail-search-message msg regexp)))
3246 (while (and (null win) (< msg pmail-total-messages))
3247 (setq msg (1+ msg)
3248 win (pmail-search-message msg regexp))))
3249 (setq n (+ n (if reversep 1 -1)))))
3250 (if win
3251 (progn
3252 (pmail-show-message-maybe msg)
3253 ;; Search forward (if this is a normal search) or backward
3254 ;; (if this is a reverse search) through this message to
3255 ;; position point. This search may fail because REGEXP
3256 ;; was found in the hidden portion of this message. In
3257 ;; that case, move point to the beginning of visible
3258 ;; portion.
3259 (if reversep
3260 (progn
3261 (goto-char (point-max))
3262 (re-search-backward regexp nil 'move))
3263 (goto-char (point-min))
3264 (re-search-forward regexp nil t))
3265 (message "%sPmail search for %s...done"
3266 (if reversep "Reverse " "")
3267 regexp))
3268 (goto-char opoint)
3269 (narrow-to-region omin omax)
3270 (ding)
3271 (message "Search failed: %s" regexp)))))
3273 (defun pmail-search-backwards (regexp &optional n)
3274 "Show message containing previous match for REGEXP.
3275 Prefix argument gives repeat count; negative argument means search
3276 forward (through later messages).
3277 Interactively, empty argument means use same regexp used last time."
3278 (interactive
3279 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
3280 (prompt
3281 (concat (if reversep "Reverse " "") "Pmail search (regexp"))
3282 regexp)
3283 (setq prompt
3284 (concat prompt
3285 (if pmail-search-last-regexp
3286 (concat ", default "
3287 pmail-search-last-regexp "): ")
3288 "): ")))
3289 (setq regexp (read-string prompt))
3290 (cond ((not (equal regexp ""))
3291 (setq pmail-search-last-regexp regexp))
3292 ((not pmail-search-last-regexp)
3293 (error "No previous Pmail search string")))
3294 (list pmail-search-last-regexp
3295 (prefix-numeric-value current-prefix-arg))))
3296 (pmail-search regexp (- (or n 1))))
3299 (defun pmail-first-unseen-message ()
3300 "Return the message index for the first message which has the
3301 `unseen' attribute."
3302 (pmail-maybe-set-message-counters)
3303 (let ((current 1)
3304 found)
3305 (save-restriction
3306 (widen)
3307 (while (and (not found) (<= current pmail-total-messages))
3308 (if (pmail-message-attr-p current "......U")
3309 (setq found current))
3310 (setq current (1+ current))))
3311 found))
3313 (defun pmail-current-subject ()
3314 "Return the current subject.
3315 The subject is stripped of leading and trailing whitespace, and
3316 of typical reply prefixes such as Re:."
3317 (let ((subject (or (mail-fetch-field "Subject") "")))
3318 (if (string-match "\\`[ \t]+" subject)
3319 (setq subject (substring subject (match-end 0))))
3320 (if (string-match pmail-reply-regexp subject)
3321 (setq subject (substring subject (match-end 0))))
3322 (if (string-match "[ \t]+\\'" subject)
3323 (setq subject (substring subject 0 (match-beginning 0))))
3324 subject))
3326 (defun pmail-current-subject-regexp ()
3327 "Return a regular expression matching the current subject.
3328 The regular expression matches the subject header line of
3329 messages about the same subject. The subject itself is stripped
3330 of leading and trailing whitespace, of typical reply prefixes
3331 such as Re: and whitespace within the subject is replaced by a
3332 regular expression matching whitespace in general in order to
3333 take into account that subject header lines may include newlines
3334 and more whitespace. The returned regular expressions contains
3335 `pmail-reply-regexp' and ends with a newline."
3336 (let ((subject (pmail-current-subject)))
3337 ;; If Subject is long, mailers will break it into several lines at
3338 ;; arbitrary places, so replace whitespace with a regexp that will
3339 ;; match any sequence of spaces, TABs, and newlines.
3340 (setq subject (regexp-quote subject))
3341 (setq subject
3342 (replace-regexp-in-string "[ \t\n]+" "[ \t\n]+" subject t t))
3343 ;; Some mailers insert extra spaces after "Subject:", so allow any
3344 ;; amount of them.
3345 (concat "^Subject:[ \t]+"
3346 (if (string= "\\`" (substring pmail-reply-regexp 0 2))
3347 (substring pmail-reply-regexp 2)
3348 pmail-reply-regexp)
3349 subject "[ \t]*\n")))
3351 (defun pmail-next-same-subject (n)
3352 "Go to the next mail message having the same subject header.
3353 With prefix argument N, do this N times.
3354 If N is negative, go backwards instead."
3355 (interactive "p")
3356 (let ((search-regexp (pmail-current-subject-regexp))
3357 (forward (> n 0))
3358 (i pmail-current-message)
3359 (case-fold-search t)
3360 found)
3361 (save-excursion
3362 (save-restriction
3363 (widen)
3364 (while (and (/= n 0)
3365 (if forward
3366 (< i pmail-total-messages)
3367 (> i 1)))
3368 (let (done)
3369 (while (and (not done)
3370 (if forward
3371 (< i pmail-total-messages)
3372 (> i 1)))
3373 (setq i (if forward (1+ i) (1- i)))
3374 (goto-char (pmail-msgbeg i))
3375 (search-forward "\n*** EOOH ***\n")
3376 (let ((beg (point)) end)
3377 (search-forward "\n\n")
3378 (setq end (point))
3379 (goto-char beg)
3380 (setq done (re-search-forward search-regexp end t))))
3381 (if done (setq found i)))
3382 (setq n (if forward (1- n) (1+ n))))))
3383 (if found
3384 (pmail-show-message-maybe found)
3385 (error "No %s message with same subject"
3386 (if forward "following" "previous")))))
3388 (defun pmail-previous-same-subject (n)
3389 "Go to the previous mail message having the same subject header.
3390 With prefix argument N, do this N times.
3391 If N is negative, go forwards instead."
3392 (interactive "p")
3393 (pmail-next-same-subject (- n)))
3395 ;;;; *** Pmail Message Deletion Commands ***
3397 (defun pmail-message-deleted-p (n)
3398 (= (aref pmail-deleted-vector n) ?D))
3400 (defun pmail-set-message-deleted-p (n state)
3401 (aset pmail-deleted-vector n (if state ?D ?\ )))
3403 (defun pmail-delete-message ()
3404 "Delete this message and stay on it."
3405 (interactive)
3406 (pmail-set-attribute pmail-deleted-attr-index t)
3407 (run-hooks 'pmail-delete-message-hook))
3409 (defun pmail-undelete-previous-message ()
3410 "Back up to deleted message, select it, and undelete it."
3411 (interactive)
3412 (set-buffer pmail-buffer)
3413 (let ((msg pmail-current-message))
3414 (while (and (> msg 0)
3415 (not (pmail-message-deleted-p msg)))
3416 (setq msg (1- msg)))
3417 (if (= msg 0)
3418 (error "No previous deleted message")
3419 (if (/= msg pmail-current-message)
3420 (pmail-show-message-maybe msg))
3421 (pmail-set-attribute pmail-deleted-attr-index nil)
3422 (if (pmail-summary-exists)
3423 (save-excursion
3424 (set-buffer pmail-summary-buffer)
3425 (pmail-summary-mark-undeleted msg)))
3426 (pmail-maybe-display-summary))))
3428 (defun pmail-delete-forward (&optional backward)
3429 "Delete this message and move to next nondeleted one.
3430 Deleted messages stay in the file until the \\[pmail-expunge] command is given.
3431 With prefix argument, delete and move backward.
3433 Returns t if a new message is displayed after the delete, or nil otherwise."
3434 (interactive "P")
3435 (pmail-set-attribute pmail-deleted-attr-index t)
3436 (run-hooks 'pmail-delete-message-hook)
3437 (let ((del-msg pmail-current-message))
3438 (if (pmail-summary-exists)
3439 (pmail-select-summary
3440 (pmail-summary-mark-deleted del-msg)))
3441 (prog1 (pmail-next-undeleted-message (if backward -1 1))
3442 (pmail-maybe-display-summary))))
3444 (defun pmail-delete-backward ()
3445 "Delete this message and move to previous nondeleted one.
3446 Deleted messages stay in the file until the \\[pmail-expunge] command is given."
3447 (interactive)
3448 (pmail-delete-forward t))
3450 ;; Compute the message number a given message would have after expunging.
3451 ;; The present number of the message is OLDNUM.
3452 ;; DELETEDVEC should be pmail-deleted-vector.
3453 ;; The value is nil for a message that would be deleted.
3454 (defun pmail-msg-number-after-expunge (deletedvec oldnum)
3455 (if (or (null oldnum) (= (aref deletedvec oldnum) ?D))
3457 (let ((i 0)
3458 (newnum 0))
3459 (while (< i oldnum)
3460 (if (/= (aref deletedvec i) ?D)
3461 (setq newnum (1+ newnum)))
3462 (setq i (1+ i)))
3463 newnum)))
3465 (defun pmail-expunge-confirmed ()
3466 "Return t if deleted message should be expunged. If necessary, ask the user.
3467 See also user-option `pmail-confirm-expunge'."
3468 (set-buffer pmail-buffer)
3469 (or (not (stringp pmail-deleted-vector))
3470 (not (string-match "D" pmail-deleted-vector))
3471 (null pmail-confirm-expunge)
3472 (funcall pmail-confirm-expunge
3473 "Erase deleted messages from Pmail file? ")))
3475 (defun pmail-only-expunge (&optional dont-show)
3476 "Actually erase all deleted messages in the file."
3477 (interactive)
3478 (set-buffer pmail-buffer)
3479 (message "Expunging deleted messages...")
3480 ;; Discard all undo records for this buffer.
3481 (or (eq buffer-undo-list t)
3482 (setq buffer-undo-list nil))
3483 (pmail-maybe-set-message-counters)
3484 (let* ((omax (- (buffer-size) (point-max)))
3485 (omin (- (buffer-size) (point-min)))
3486 (opoint (if (and (> pmail-current-message 0)
3487 (pmail-message-deleted-p pmail-current-message))
3489 (if pmail-enable-mime
3490 (with-current-buffer pmail-view-buffer
3491 (- (point)(point-min)))
3492 (- (point) (point-min)))))
3493 (messages-head (cons (aref pmail-message-vector 0) nil))
3494 (messages-tail messages-head)
3495 ;; Don't make any undo records for the expunging.
3496 (buffer-undo-list t)
3497 (win))
3498 (unwind-protect
3499 (save-excursion
3500 (widen)
3501 (goto-char (point-min))
3502 (let ((counter 0)
3503 (number 1)
3504 (total pmail-total-messages)
3505 (new-message-number pmail-current-message)
3506 (new-summary nil)
3507 (new-msgref (list (list 0)))
3508 (pmailbuf (current-buffer))
3509 (buffer-read-only nil)
3510 (messages pmail-message-vector)
3511 (deleted pmail-deleted-vector)
3512 (summary pmail-summary-vector))
3513 (setq pmail-total-messages nil
3514 pmail-current-message nil
3515 pmail-message-vector nil
3516 pmail-deleted-vector nil
3517 pmail-summary-vector nil)
3519 (while (<= number total)
3520 (if (= (aref deleted number) ?D)
3521 (progn
3522 (delete-region
3523 (marker-position (aref messages number))
3524 (marker-position (aref messages (1+ number))))
3525 (move-marker (aref messages number) nil)
3526 (if (> new-message-number counter)
3527 (setq new-message-number (1- new-message-number))))
3528 (setq counter (1+ counter))
3529 (setq messages-tail
3530 (setcdr messages-tail
3531 (cons (aref messages number) nil)))
3532 (setq new-summary
3533 (cons (if (= counter number) (aref summary (1- number)))
3534 new-summary))
3535 (setq new-msgref
3536 (cons (aref pmail-msgref-vector number)
3537 new-msgref))
3538 (setcar (car new-msgref) counter))
3539 (if (zerop (% (setq number (1+ number)) 20))
3540 (message "Expunging deleted messages...%d" number)))
3541 (setq messages-tail
3542 (setcdr messages-tail
3543 (cons (aref messages number) nil)))
3544 (setq pmail-current-message new-message-number
3545 pmail-total-messages counter
3546 pmail-message-vector (apply 'vector messages-head)
3547 pmail-deleted-vector (make-string (1+ counter) ?\ )
3548 pmail-summary-vector (vconcat (nreverse new-summary))
3549 pmail-msgref-vector (apply 'vector (nreverse new-msgref))
3550 win t)))
3551 (message "Expunging deleted messages...done")
3552 (if (not win)
3553 (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
3554 (if (not dont-show)
3555 (pmail-show-message-maybe (< pmail-current-message pmail-total-messages)))
3556 (pmail-swap-buffers-maybe)
3557 (if pmail-enable-mime
3558 (goto-char (+ (point-min) opoint))
3559 (goto-char (+ (point) opoint))))))
3561 (defun pmail-expunge ()
3562 "Erase deleted messages from Pmail file and summary buffer."
3563 (interactive)
3564 (when (pmail-expunge-confirmed)
3565 (pmail-only-expunge)
3566 (if (pmail-summary-exists)
3567 (pmail-select-summary (pmail-update-summary)))))
3569 ;;;; *** Pmail Mailing Commands ***
3571 (defun pmail-start-mail (&optional noerase to subject in-reply-to cc
3572 replybuffer sendactions same-window others)
3573 (let (yank-action)
3574 (if replybuffer
3575 (setq yank-action (list 'insert-buffer replybuffer)))
3576 (setq others (cons (cons "cc" cc) others))
3577 (setq others (cons (cons "in-reply-to" in-reply-to) others))
3578 (if same-window
3579 (compose-mail to subject others
3580 noerase nil
3581 yank-action sendactions)
3582 (if pmail-mail-new-frame
3583 (prog1
3584 (compose-mail to subject others
3585 noerase 'switch-to-buffer-other-frame
3586 yank-action sendactions)
3587 ;; This is not a standard frame parameter;
3588 ;; nothing except sendmail.el looks at it.
3589 (modify-frame-parameters (selected-frame)
3590 '((mail-dedicated-frame . t))))
3591 (compose-mail to subject others
3592 noerase 'switch-to-buffer-other-window
3593 yank-action sendactions)))))
3595 (defun pmail-mail ()
3596 "Send mail in another window.
3597 While composing the message, use \\[mail-yank-original] to yank the
3598 original message into it."
3599 (interactive)
3600 (pmail-start-mail nil nil nil nil nil pmail-view-buffer))
3602 (defun pmail-continue ()
3603 "Continue composing outgoing message previously being composed."
3604 (interactive)
3605 (pmail-start-mail t))
3607 (defun pmail-reply (just-sender)
3608 "Reply to the current message.
3609 Normally include CC: to all other recipients of original message;
3610 prefix argument means ignore them. While composing the reply,
3611 use \\[mail-yank-original] to yank the original message into it."
3612 (interactive "P")
3613 (let (from reply-to cc subject date to message-id references
3614 resent-to resent-cc resent-reply-to
3615 (msgnum pmail-current-message))
3616 (save-excursion
3617 (save-restriction
3618 (if pmail-enable-mime
3619 (narrow-to-region
3620 (goto-char (point-min))
3621 (if (search-forward "\n\n" nil 'move)
3622 (1+ (match-beginning 0))
3623 (point)))
3624 (widen)
3625 (goto-char (pmail-msgbeg pmail-current-message))
3626 (forward-line 1)
3627 (if (= (following-char) ?0)
3628 (narrow-to-region
3629 (progn (forward-line 2)
3630 (point))
3631 (progn (search-forward "\n\n" (pmail-msgend pmail-current-message)
3632 'move)
3633 (point)))
3634 (narrow-to-region (point)
3635 (progn (search-forward "\n*** EOOH ***\n")
3636 (beginning-of-line) (point)))))
3637 (setq from (mail-fetch-field "from")
3638 reply-to (or (mail-fetch-field "mail-reply-to" nil t)
3639 (mail-fetch-field "reply-to" nil t)
3640 from)
3641 subject (mail-fetch-field "subject")
3642 date (mail-fetch-field "date")
3643 message-id (mail-fetch-field "message-id")
3644 references (mail-fetch-field "references" nil nil t)
3645 resent-reply-to (mail-fetch-field "resent-reply-to" nil t)
3646 resent-cc (and (not just-sender)
3647 (mail-fetch-field "resent-cc" nil t))
3648 resent-to (or (mail-fetch-field "resent-to" nil t) "")
3649 ;;; resent-subject (mail-fetch-field "resent-subject")
3650 ;;; resent-date (mail-fetch-field "resent-date")
3651 ;;; resent-message-id (mail-fetch-field "resent-message-id")
3653 (unless just-sender
3654 (if (mail-fetch-field "mail-followup-to" nil t)
3655 ;; If this header field is present, use it instead of the To and CC fields.
3656 (setq to (mail-fetch-field "mail-followup-to" nil t))
3657 (setq cc (or (mail-fetch-field "cc" nil t) "")
3658 to (or (mail-fetch-field "to" nil t) ""))))
3662 ;; Merge the resent-to and resent-cc into the to and cc.
3663 (if (and resent-to (not (equal resent-to "")))
3664 (if (not (equal to ""))
3665 (setq to (concat to ", " resent-to))
3666 (setq to resent-to)))
3667 (if (and resent-cc (not (equal resent-cc "")))
3668 (if (not (equal cc ""))
3669 (setq cc (concat cc ", " resent-cc))
3670 (setq cc resent-cc)))
3671 ;; Add `Re: ' to subject if not there already.
3672 (and (stringp subject)
3673 (setq subject
3674 (concat pmail-reply-prefix
3675 (if (let ((case-fold-search t))
3676 (string-match pmail-reply-regexp subject))
3677 (substring subject (match-end 0))
3678 subject))))
3679 (pmail-start-mail
3681 ;; Using mail-strip-quoted-names is undesirable with newer mailers
3682 ;; since they can handle the names unstripped.
3683 ;; I don't know whether there are other mailers that still
3684 ;; need the names to be stripped.
3685 ;;; (mail-strip-quoted-names reply-to)
3686 ;; Remove unwanted names from reply-to, since Mail-Followup-To
3687 ;; header causes all the names in it to wind up in reply-to, not
3688 ;; in cc. But if what's left is an empty list, use the original.
3689 (let* ((reply-to-list (pmail-dont-reply-to reply-to)))
3690 (if (string= reply-to-list "") reply-to reply-to-list))
3691 subject
3692 (pmail-make-in-reply-to-field from date message-id)
3693 (if just-sender
3695 ;; mail-strip-quoted-names is NOT necessary for pmail-dont-reply-to
3696 ;; to do its job.
3697 (let* ((cc-list (pmail-dont-reply-to
3698 (mail-strip-quoted-names
3699 (if (null cc) to (concat to ", " cc))))))
3700 (if (string= cc-list "") nil cc-list)))
3701 pmail-view-buffer
3702 (list (list 'pmail-mark-message
3703 pmail-buffer
3704 (with-current-buffer pmail-buffer
3705 (aref pmail-msgref-vector msgnum))
3706 "answered"))
3708 (list (cons "References" (concat (mapconcat 'identity references " ")
3709 " " message-id))))))
3711 (defun pmail-mark-message (buffer msgnum-list attribute)
3712 "Give BUFFER's message number in MSGNUM-LIST the attribute ATTRIBUTE.
3713 This is use in the send-actions for message buffers.
3714 MSGNUM-LIST is a list of the form (MSGNUM)
3715 which is an element of pmail-msgref-vector."
3716 (save-excursion
3717 (set-buffer buffer)
3718 (if (car msgnum-list)
3719 (pmail-set-attribute attribute t (car msgnum-list)))))
3721 (defun pmail-make-in-reply-to-field (from date message-id)
3722 (cond ((not from)
3723 (if message-id
3724 message-id
3725 nil))
3726 (mail-use-rfc822
3727 (require 'rfc822)
3728 (let ((tem (car (rfc822-addresses from))))
3729 (if message-id
3730 (if (or (not tem)
3731 (string-match
3732 (regexp-quote (if (string-match "@[^@]*\\'" tem)
3733 (substring tem 0
3734 (match-beginning 0))
3735 tem))
3736 message-id))
3737 ;; missing From, or Message-ID is sufficiently informative
3738 message-id
3739 (concat message-id " (" tem ")"))
3740 ;; Copy TEM, discarding text properties.
3741 (setq tem (copy-sequence tem))
3742 (set-text-properties 0 (length tem) nil tem)
3743 (setq tem (copy-sequence tem))
3744 ;; Use prin1 to fake RFC822 quoting
3745 (let ((field (prin1-to-string tem)))
3746 (if date
3747 (concat field "'s message of " date)
3748 field)))))
3749 ((let* ((foo "[^][\000-\037()<>@,;:\\\" ]+")
3750 (bar "[^][\000-\037()<>@,;:\\\"]+"))
3751 ;; These strings both match all non-ASCII characters.
3752 (or (string-match (concat "\\`[ \t]*\\(" bar
3753 "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
3754 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
3755 from)
3756 (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
3757 bar "\\))[ \t]*\\'")
3758 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
3759 from)))
3760 (let ((start (match-beginning 1))
3761 (end (match-end 1)))
3762 ;; Trim whitespace which above regexp match allows
3763 (while (and (< start end)
3764 (memq (aref from start) '(?\t ?\ )))
3765 (setq start (1+ start)))
3766 (while (and (< start end)
3767 (memq (aref from (1- end)) '(?\t ?\ )))
3768 (setq end (1- end)))
3769 (let ((field (substring from start end)))
3770 (if date (setq field (concat "message from " field " on " date)))
3771 (if message-id
3772 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
3773 (concat message-id " (" field ")")
3774 field))))
3776 ;; If we can't kludge it simply, do it correctly
3777 (let ((mail-use-rfc822 t))
3778 (pmail-make-in-reply-to-field from date message-id)))))
3780 (defun pmail-forward (resend)
3781 "Forward the current message to another user.
3782 With prefix argument, \"resend\" the message instead of forwarding it;
3783 see the documentation of `pmail-resend'."
3784 (interactive "P")
3785 (if resend
3786 (call-interactively 'pmail-resend)
3787 (let ((forward-buffer pmail-buffer)
3788 (msgnum pmail-current-message)
3789 (subject (concat "["
3790 (let ((from (or (mail-fetch-field "From")
3791 (mail-fetch-field ">From"))))
3792 (if from
3793 (concat (mail-strip-quoted-names from) ": ")
3794 ""))
3795 (or (mail-fetch-field "Subject") "")
3796 "]")))
3797 (if (pmail-start-mail
3798 nil nil subject nil nil nil
3799 (list (list 'pmail-mark-message
3800 forward-buffer
3801 (with-current-buffer pmail-buffer
3802 (aref pmail-msgref-vector msgnum))
3803 "forwarded"))
3804 ;; If only one window, use it for the mail buffer.
3805 ;; Otherwise, use another window for the mail buffer
3806 ;; so that the Pmail buffer remains visible
3807 ;; and sending the mail will get back to it.
3808 (and (not pmail-mail-new-frame) (one-window-p t)))
3809 ;; The mail buffer is now current.
3810 (save-excursion
3811 ;; Insert after header separator--before signature if any.
3812 (goto-char (mail-text-start))
3813 (if (or pmail-enable-mime pmail-enable-mime-composing)
3814 (funcall pmail-insert-mime-forwarded-message-function
3815 forward-buffer)
3816 (insert "------- Start of forwarded message -------\n")
3817 ;; Quote lines with `- ' if they start with `-'.
3818 (let ((beg (point)) end)
3819 (setq end (point-marker))
3820 (set-marker-insertion-type end t)
3821 (insert-buffer-substring forward-buffer)
3822 (goto-char beg)
3823 (while (re-search-forward "^-" end t)
3824 (beginning-of-line)
3825 (insert "- ")
3826 (forward-line 1))
3827 (goto-char end)
3828 (skip-chars-backward "\n")
3829 (if (< (point) end)
3830 (forward-char 1))
3831 (delete-region (point) end)
3832 (set-marker end nil))
3833 (insert "------- End of forwarded message -------\n"))
3834 (push-mark))))))
3836 (defun pmail-resend (address &optional from comment mail-alias-file)
3837 "Resend current message to ADDRESSES.
3838 ADDRESSES should be a single address, a string consisting of several
3839 addresses separated by commas, or a list of addresses.
3841 Optional FROM is the address to resend the message from, and
3842 defaults from the value of `user-mail-address'.
3843 Optional COMMENT is a string to insert as a comment in the resent message.
3844 Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
3845 typically for purposes of moderating a list."
3846 (interactive "sResend to: ")
3847 (require 'sendmail)
3848 (require 'mailalias)
3849 (unless (or (eq pmail-view-buffer (current-buffer))
3850 (eq pmail-buffer (current-buffer)))
3851 (error "Not an Pmail buffer"))
3852 (if (not from) (setq from user-mail-address))
3853 (let ((tembuf (generate-new-buffer " sendmail temp"))
3854 (case-fold-search nil)
3855 (mail-personal-alias-file
3856 (or mail-alias-file mail-personal-alias-file))
3857 (mailbuf pmail-buffer))
3858 (unwind-protect
3859 (with-current-buffer tembuf
3860 ;;>> Copy message into temp buffer
3861 (if pmail-enable-mime
3862 (funcall pmail-insert-mime-resent-message-function mailbuf)
3863 (insert-buffer-substring mailbuf))
3864 (goto-char (point-min))
3865 ;; Delete any Sender field, since that's not specifiable.
3866 ; Only delete Sender fields in the actual header.
3867 (re-search-forward "^$" nil 'move)
3868 ; Using "while" here rather than "if" because some buggy mail
3869 ; software may have inserted multiple Sender fields.
3870 (while (re-search-backward "^Sender:" nil t)
3871 (let (beg)
3872 (setq beg (point))
3873 (forward-line 1)
3874 (while (looking-at "[ \t]")
3875 (forward-line 1))
3876 (delete-region beg (point))))
3877 ; Go back to the beginning of the buffer so the Resent- fields
3878 ; are inserted there.
3879 (goto-char (point-min))
3880 ;;>> Insert resent-from:
3881 (insert "Resent-From: " from "\n")
3882 (insert "Resent-Date: " (mail-rfc822-date) "\n")
3883 ;;>> Insert resent-to: and bcc if need be.
3884 (let ((before (point)))
3885 (if mail-self-blind
3886 (insert "Resent-Bcc: " (user-login-name) "\n"))
3887 (insert "Resent-To: " (if (stringp address)
3888 address
3889 (mapconcat 'identity address ",\n\t"))
3890 "\n")
3891 ;; Expand abbrevs in the recipients.
3892 (save-excursion
3893 (if (featurep 'mailabbrev)
3894 (let ((end (point-marker))
3895 (local-abbrev-table mail-abbrevs)
3896 (old-syntax-table (syntax-table)))
3897 (if (and (not (vectorp mail-abbrevs))
3898 (file-exists-p mail-personal-alias-file))
3899 (build-mail-abbrevs))
3900 (unless mail-abbrev-syntax-table
3901 (mail-abbrev-make-syntax-table))
3902 (set-syntax-table mail-abbrev-syntax-table)
3903 (goto-char before)
3904 (while (and (< (point) end)
3905 (progn (forward-word 1)
3906 (<= (point) end)))
3907 (expand-abbrev))
3908 (set-syntax-table old-syntax-table))
3909 (expand-mail-aliases before (point)))))
3910 ;;>> Set up comment, if any.
3911 (if (and (sequencep comment) (not (zerop (length comment))))
3912 (let ((before (point))
3913 after)
3914 (insert comment)
3915 (or (eolp) (insert "\n"))
3916 (setq after (point))
3917 (goto-char before)
3918 (while (< (point) after)
3919 (insert "Resent-Comment: ")
3920 (forward-line 1))))
3921 ;; Don't expand aliases in the destination fields
3922 ;; of the original message.
3923 (let (mail-aliases)
3924 (funcall send-mail-function)))
3925 (kill-buffer tembuf))
3926 (with-current-buffer pmail-buffer
3927 (pmail-set-attribute pmail-resent-attr-index t pmail-current-message))))
3929 (defvar mail-unsent-separator
3930 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
3931 "^ *---+ +Returned message +---+ *$\\|"
3932 "^ *---+ *Returned mail follows *---+ *$\\|"
3933 "^Start of returned message$\\|"
3934 "^---+ Below this line is a copy of the message.$\\|"
3935 "^ *---+ +Original message +---+ *$\\|"
3936 "^ *--+ +begin message +--+ *$\\|"
3937 "^ *---+ +Original message follows +---+ *$\\|"
3938 "^ *---+ +Your message follows +---+ *$\\|"
3939 "^|? *---+ +Message text follows: +---+ *|?$\\|"
3940 "^ *---+ +This is a copy of \\w+ message, including all the headers.*---+ *$")
3941 "A regexp that matches the separator before the text of a failed message.")
3943 (defvar mail-mime-unsent-header "^Content-Type: message/rfc822 *$"
3944 "A regexp that matches the header of a MIME body part with a failed message.")
3946 (defun pmail-retry-failure ()
3947 "Edit a mail message which is based on the contents of the current message.
3948 For a message rejected by the mail system, extract the interesting headers and
3949 the body of the original message.
3950 If the failed message is a MIME multipart message, it is searched for a
3951 body part with a header which matches the variable `mail-mime-unsent-header'.
3952 Otherwise, the variable `mail-unsent-separator' should match the string that
3953 delimits the returned original message.
3954 The variable `pmail-retry-ignored-headers' is a regular expression
3955 specifying headers which should not be copied into the new message."
3956 (interactive)
3957 (require 'mail-utils)
3958 (let ((pmail-this-buffer (current-buffer))
3959 (msgnum pmail-current-message)
3960 bounce-start bounce-end bounce-indent resending
3961 ;; Fetch any content-type header in current message
3962 ;; Must search thru the whole unpruned header.
3963 (content-type
3964 (save-excursion
3965 (save-restriction
3966 (mail-fetch-field "Content-Type") ))))
3967 (save-excursion
3968 (goto-char (point-min))
3969 (let ((case-fold-search t))
3970 (if (and content-type
3971 (string-match
3972 ";[\n\t ]*boundary=\"?\\([-0-9a-z'()+_,./:=? ]+\\)\"?"
3973 content-type))
3974 ;; Handle a MIME multipart bounce message.
3975 (let ((codestring
3976 (concat "\n--"
3977 (substring content-type (match-beginning 1)
3978 (match-end 1)))))
3979 (unless (re-search-forward mail-mime-unsent-header nil t)
3980 (error "Cannot find beginning of header in failed message"))
3981 (unless (search-forward "\n\n" nil t)
3982 (error "Cannot find start of Mime data in failed message"))
3983 (setq bounce-start (point))
3984 (if (search-forward codestring nil t)
3985 (setq bounce-end (match-beginning 0))
3986 (setq bounce-end (point-max))))
3987 ;; Non-MIME bounce.
3988 (or (re-search-forward mail-unsent-separator nil t)
3989 (error "Cannot parse this as a failure message"))
3990 (skip-chars-forward "\n")
3991 ;; Support a style of failure message in which the original
3992 ;; message is indented, and included within lines saying
3993 ;; `Start of returned message' and `End of returned message'.
3994 (if (looking-at " +Received:")
3995 (progn
3996 (setq bounce-start (point))
3997 (skip-chars-forward " ")
3998 (setq bounce-indent (- (current-column)))
3999 (goto-char (point-max))
4000 (re-search-backward "^End of returned message$" nil t)
4001 (setq bounce-end (point)))
4002 ;; One message contained a few random lines before
4003 ;; the old message header. The first line of the
4004 ;; message started with two hyphens. A blank line
4005 ;; followed these random lines. The same line
4006 ;; beginning with two hyphens was possibly marking
4007 ;; the end of the message.
4008 (if (looking-at "^--")
4009 (let ((boundary (buffer-substring-no-properties
4010 (point)
4011 (progn (end-of-line) (point)))))
4012 (search-forward "\n\n")
4013 (skip-chars-forward "\n")
4014 (setq bounce-start (point))
4015 (goto-char (point-max))
4016 (search-backward (concat "\n\n" boundary) bounce-start t)
4017 (setq bounce-end (point)))
4018 (setq bounce-start (point)
4019 bounce-end (point-max)))
4020 (unless (search-forward "\n\n" nil t)
4021 (error "Cannot find end of header in failed message"))))))
4022 ;; We have found the message that bounced, within the current message.
4023 ;; Now start sending new message; default header fields from original.
4024 ;; Turn off the usual actions for initializing the message body
4025 ;; because we want to get only the text from the failure message.
4026 (let (mail-signature mail-setup-hook)
4027 (if (pmail-start-mail nil nil nil nil nil pmail-this-buffer
4028 (list (list 'pmail-mark-message
4029 pmail-this-buffer
4030 (aref pmail-msgref-vector msgnum)
4031 "retried")))
4032 ;; Insert original text as initial text of new draft message.
4033 ;; Bind inhibit-read-only since the header delimiter
4034 ;; of the previous message was probably read-only.
4035 (let ((inhibit-read-only t)
4036 pmail-displayed-headers
4037 pmail-ignored-headers)
4038 (erase-buffer)
4039 (insert-buffer-substring pmail-this-buffer
4040 bounce-start bounce-end)
4041 (goto-char (point-min))
4042 (if bounce-indent
4043 (indent-rigidly (point-min) (point-max) bounce-indent))
4044 (pmail-clear-headers pmail-retry-ignored-headers)
4045 (pmail-clear-headers "^sender:\\|^return-path:\\|^received:")
4046 (mail-sendmail-delimit-header)
4047 (save-restriction
4048 (narrow-to-region (point-min) (mail-header-end))
4049 (setq resending (mail-fetch-field "resent-to"))
4050 (if mail-self-blind
4051 (if resending
4052 (insert "Resent-Bcc: " (user-login-name) "\n")
4053 (insert "BCC: " (user-login-name) "\n"))))
4054 (goto-char (point-min))
4055 (mail-position-on-field (if resending "Resent-To" "To") t))))))
4057 (defun pmail-summary-exists ()
4058 "Non-nil if in an PMAIL buffer and an associated summary buffer exists.
4059 In fact, the non-nil value returned is the summary buffer itself."
4060 (and pmail-summary-buffer (buffer-name pmail-summary-buffer)
4061 pmail-summary-buffer))
4063 (defun pmail-summary-displayed ()
4064 "t if in PMAIL buffer and an associated summary buffer is displayed."
4065 (and pmail-summary-buffer (get-buffer-window pmail-summary-buffer)))
4067 (defcustom pmail-redisplay-summary nil
4068 "*Non-nil means Pmail should show the summary when it changes.
4069 This has an effect only if a summary buffer exists."
4070 :type 'boolean
4071 :group 'pmail-summary)
4073 (defcustom pmail-summary-window-size nil
4074 "*Non-nil means specify the height for an Pmail summary window."
4075 :type '(choice (const :tag "Disabled" nil) integer)
4076 :group 'pmail-summary)
4078 ;; Put the summary buffer back on the screen, if user wants that.
4079 (defun pmail-maybe-display-summary ()
4080 (let ((selected (selected-window))
4081 window)
4082 ;; If requested, make sure the summary is displayed.
4083 (and pmail-summary-buffer (buffer-name pmail-summary-buffer)
4084 pmail-redisplay-summary
4085 (if (get-buffer-window pmail-summary-buffer 0)
4086 ;; It's already in some frame; show that one.
4087 (let ((frame (window-frame
4088 (get-buffer-window pmail-summary-buffer 0))))
4089 (make-frame-visible frame)
4090 (raise-frame frame))
4091 (display-buffer pmail-summary-buffer)))
4092 ;; If requested, set the height of the summary window.
4093 (and pmail-summary-buffer (buffer-name pmail-summary-buffer)
4094 pmail-summary-window-size
4095 (setq window (get-buffer-window pmail-summary-buffer))
4096 ;; Don't try to change the size if just one window in frame.
4097 (not (eq window (frame-root-window (window-frame window))))
4098 (unwind-protect
4099 (progn
4100 (select-window window)
4101 (enlarge-window (- pmail-summary-window-size (window-height))))
4102 (select-window selected)))))
4104 ;;;; *** Pmail Local Fontification ***
4106 (defun pmail-fontify-buffer-function ()
4107 ;; This function's symbol is bound to font-lock-fontify-buffer-function.
4108 (add-hook 'pmail-show-message-hook 'pmail-fontify-message nil t)
4109 ;; If we're already showing a message, fontify it now.
4110 (if pmail-current-message (pmail-fontify-message))
4111 ;; Prevent Font Lock mode from kicking in.
4112 (setq font-lock-fontified t))
4114 (defun pmail-unfontify-buffer-function ()
4115 ;; This function's symbol is bound to font-lock-fontify-unbuffer-function.
4116 (let ((modified (buffer-modified-p))
4117 (buffer-undo-list t) (inhibit-read-only t)
4118 before-change-functions after-change-functions
4119 buffer-file-name buffer-file-truename)
4120 (save-restriction
4121 (widen)
4122 (remove-hook 'pmail-show-message-hook 'pmail-fontify-message t)
4123 (remove-text-properties (point-min) (point-max) '(pmail-fontified nil))
4124 (font-lock-default-unfontify-buffer)
4125 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))
4127 (defun pmail-fontify-message ()
4128 ;; Fontify the current message if it is not already fontified.
4129 (if (text-property-any (point-min) (point-max) 'pmail-fontified nil)
4130 (let ((modified (buffer-modified-p))
4131 (buffer-undo-list t) (inhibit-read-only t)
4132 before-change-functions after-change-functions
4133 buffer-file-name buffer-file-truename)
4134 (save-excursion
4135 (save-match-data
4136 (add-text-properties (point-min) (point-max) '(pmail-fontified t))
4137 (font-lock-fontify-region (point-min) (point-max))
4138 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))))
4140 ;;; Speedbar support for PMAIL files.
4141 (eval-when-compile (require 'speedbar))
4143 (defvar pmail-speedbar-match-folder-regexp "^[A-Z0-9]+\\(\\.[A-Z0-9]+\\)?$"
4144 "*This regex is used to match folder names to be displayed in speedbar.
4145 Enabling this will permit speedbar to display your folders for easy
4146 browsing, and moving of messages.")
4148 (defvar pmail-speedbar-last-user nil
4149 "The last user to be displayed in the speedbar.")
4151 (defvar pmail-speedbar-key-map nil
4152 "Keymap used when in pmail display mode.")
4154 (defun pmail-install-speedbar-variables ()
4155 "Install those variables used by speedbar to enhance pmail."
4156 (if pmail-speedbar-key-map
4158 (setq pmail-speedbar-key-map (speedbar-make-specialized-keymap))
4160 (define-key pmail-speedbar-key-map "e" 'speedbar-edit-line)
4161 (define-key pmail-speedbar-key-map "r" 'speedbar-edit-line)
4162 (define-key pmail-speedbar-key-map "\C-m" 'speedbar-edit-line)
4163 (define-key pmail-speedbar-key-map "M"
4164 'pmail-speedbar-move-message-to-folder-on-line)))
4166 (defvar pmail-speedbar-menu-items
4167 '(["Read Folder" speedbar-edit-line t]
4168 ["Move message to folder" pmail-speedbar-move-message-to-folder-on-line
4169 (save-excursion (beginning-of-line)
4170 (looking-at "<M> "))])
4171 "Additional menu-items to add to speedbar frame.")
4173 ;; Make sure our special speedbar major mode is loaded
4174 (if (featurep 'speedbar)
4175 (pmail-install-speedbar-variables)
4176 (add-hook 'speedbar-load-hook 'pmail-install-speedbar-variables))
4178 (defun pmail-speedbar-buttons (buffer)
4179 "Create buttons for BUFFER containing pmail messages.
4180 Click on the address under Reply to: to reply to this person.
4181 Under Folders: Click a name to read it, or on the <M> to move the
4182 current message into that PMAIL folder."
4183 (let ((from nil))
4184 (save-excursion
4185 (set-buffer buffer)
4186 (goto-char (point-min))
4187 (if (not (re-search-forward "^Reply-To: " nil t))
4188 (if (not (re-search-forward "^From:? " nil t))
4189 (setq from t)))
4190 (if from
4192 (setq from (buffer-substring (point) (save-excursion
4193 (end-of-line)
4194 (point))))))
4195 (goto-char (point-min))
4196 (if (and (looking-at "Reply to:")
4197 (equal from pmail-speedbar-last-user))
4199 (setq pmail-speedbar-last-user from)
4200 (erase-buffer)
4201 (insert "Reply To:\n")
4202 (if (stringp from)
4203 (speedbar-insert-button from 'speedbar-directory-face 'highlight
4204 'pmail-speedbar-button 'pmail-reply))
4205 (insert "Folders:\n")
4206 (let* ((case-fold-search nil)
4207 (df (directory-files (save-excursion (set-buffer buffer)
4208 default-directory)
4209 nil pmail-speedbar-match-folder-regexp)))
4210 (while df
4211 (speedbar-insert-button "<M>" 'speedbar-button-face 'highlight
4212 'pmail-speedbar-move-message (car df))
4213 (speedbar-insert-button (car df) 'speedbar-file-face 'highlight
4214 'pmail-speedbar-find-file nil t)
4215 (setq df (cdr df)))))))
4217 (defun pmail-speedbar-button (text token indent)
4218 "Execute an pmail command specified by TEXT.
4219 The command used is TOKEN. INDENT is not used."
4220 (speedbar-with-attached-buffer
4221 (funcall token t)))
4223 (defun pmail-speedbar-find-file (text token indent)
4224 "Load in the pmail file TEXT.
4225 TOKEN and INDENT are not used."
4226 (speedbar-with-attached-buffer
4227 (message "Loading in PMAIL file %s..." text)
4228 (find-file text)))
4230 (defun pmail-speedbar-move-message-to-folder-on-line ()
4231 "If the current line is a folder, move current message to it."
4232 (interactive)
4233 (save-excursion
4234 (beginning-of-line)
4235 (if (re-search-forward "<M> " (save-excursion (end-of-line) (point)) t)
4236 (progn
4237 (forward-char -2)
4238 (speedbar-do-function-pointer)))))
4240 (defun pmail-speedbar-move-message (text token indent)
4241 "From button TEXT, copy current message to the pmail file specified by TOKEN.
4242 TEXT and INDENT are not used."
4243 (speedbar-with-attached-buffer
4244 (message "Moving message to %s" token)
4245 (pmail-output-to-pmail-file token)))
4247 ; Functions for setting, getting and encoding the POP password.
4248 ; The password is encoded to prevent it from being easily accessible
4249 ; to "prying eyes." Obviously, this encoding isn't "real security,"
4250 ; nor is it meant to be.
4252 ;;;###autoload
4253 (defun pmail-set-remote-password (password)
4254 "Set PASSWORD to be used for retrieving mail from a POP or IMAP server."
4255 (interactive "sPassword: ")
4256 (if password
4257 (setq pmail-encoded-remote-password
4258 (pmail-encode-string password (emacs-pid)))
4259 (setq pmail-remote-password nil)
4260 (setq pmail-encoded-remote-password nil)))
4262 (defun pmail-get-remote-password (imap)
4263 "Get the password for retrieving mail from a POP or IMAP server. If none
4264 has been set, then prompt the user for one."
4265 (when (not pmail-encoded-remote-password)
4266 (if (not pmail-remote-password)
4267 (setq pmail-remote-password
4268 (read-passwd (if imap
4269 "IMAP password: "
4270 "POP password: "))))
4271 (pmail-set-remote-password pmail-remote-password)
4272 (setq pmail-remote-password nil))
4273 (pmail-encode-string pmail-encoded-remote-password (emacs-pid)))
4275 (defun pmail-have-password ()
4276 (or pmail-remote-password pmail-encoded-remote-password))
4278 (defun pmail-encode-string (string mask)
4279 "Encode STRING with integer MASK, by taking the exclusive OR of the
4280 lowest byte in the mask with the first character of string, the
4281 second-lowest-byte with the second character of the string, etc.,
4282 restarting at the lowest byte of the mask whenever it runs out.
4283 Returns the encoded string. Calling the function again with an
4284 encoded string (and the same mask) will decode the string."
4285 (setq mask (abs mask)) ; doesn't work if negative
4286 (let* ((string-vector (string-to-vector string)) (i 0)
4287 (len (length string-vector)) (curmask mask) charmask)
4288 (while (< i len)
4289 (if (= curmask 0)
4290 (setq curmask mask))
4291 (setq charmask (% curmask 256))
4292 (setq curmask (lsh curmask -8))
4293 (aset string-vector i (logxor charmask (aref string-vector i)))
4294 (setq i (1+ i)))
4295 (concat string-vector)))
4297 ;;;; Desktop support
4299 (defun pmail-restore-desktop-buffer (desktop-buffer-file-name
4300 desktop-buffer-name
4301 desktop-buffer-misc)
4302 "Restore an pmail buffer specified in a desktop file."
4303 (condition-case error
4304 (progn
4305 (pmail-input desktop-buffer-file-name)
4306 (if (eq major-mode 'pmail-mode)
4307 (current-buffer)
4308 pmail-buffer))
4309 (file-locked
4310 (kill-buffer (current-buffer))
4311 nil)))
4313 (add-to-list 'desktop-buffer-mode-handlers
4314 '(pmail-mode . pmail-restore-desktop-buffer))
4317 (provide 'pmail)
4319 ;; Local Variables:
4320 ;; change-log-default-name: "ChangeLog.pmail"
4321 ;; End:
4323 ;; arch-tag: 65d257d3-c281-4a65-9c38-e61af95af2f0
4324 ;;; pmail.el ends here