comparison lisp/mail/pmail.el @ 97528:184bb2071e3f

mail/: Add new (temporary) libaries for which to test Rmail/mbox such that Rmail/babyl is not affected. This creates a facility/feature called "pmail" (analagous to "rmail") that can be used independently from Rmail for testing purposes. The plan is to replace the "rmail" files eventually and remove "pmail" entirely at that point. In the interim, interested developers can use either Rmail or Pmail or both (which is not recommended for the casual User or the faint of heart).
author Paul Reilly <pmr@pajato.com>
date Mon, 18 Aug 2008 04:51:28 +0000
parents
children ec1125c7ac26
comparison
equal deleted inserted replaced
97527:059ec03cfe4e 97528:184bb2071e3f
1 ;;; pmail.el --- main code of "PMAIL" mail reader for Emacs
2
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.
6
7 ;; Maintainer: FSF
8 ;; Keywords: mail
9
10 ;; This file is part of GNU Emacs.
11
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.
16
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.
21
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/>.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
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.
33
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.
39 ;;
40
41 (eval-when-compile
42 (require 'font-lock)
43 (require 'mailabbrev)
44 (require 'mule-util)) ; for detect-coding-with-priority
45
46 (require 'pmaildesc)
47 (require 'pmailhdr)
48 (require 'pmailkwd)
49 (require 'mail-parse)
50
51 (defvar deleted-head)
52 (defvar font-lock-fontified)
53 (defvar mail-abbrev-syntax-table)
54 (defvar mail-abbrevs)
55 (defvar messages-head)
56 (defvar pmail-use-spam-filter)
57 (defvar rsf-beep)
58 (defvar rsf-sleep-after-message)
59 (defvar total-messages)
60 (defvar tool-bar-map)
61
62 ; These variables now declared in paths.el.
63 ;(defvar pmail-spool-directory "/usr/spool/mail/"
64 ; "This is the name of the directory used by the system mailer for\n\
65 ;delivering new mail. Its name should end with a slash.")
66 ;(defvar pmail-file-name
67 ; (expand-file-name "~/PMAIL")
68 ; "")
69
70 ;; Temporary support for mbox.
71 (defcustom pmail-file-name "~/PMAIL"
72 "*Name of user's primary mail file."
73 :type 'string
74 :group 'rmail
75 :version "21.1")
76
77 (defgroup pmail nil
78 "Mail reader for Emacs."
79 :group 'mail)
80
81 (defgroup pmail-retrieve nil
82 "Pmail retrieval options."
83 :prefix "pmail-"
84 :group 'pmail)
85
86 (defgroup pmail-files nil
87 "Pmail files."
88 :prefix "pmail-"
89 :group 'pmail)
90
91 (defgroup pmail-headers nil
92 "Pmail header options."
93 :prefix "pmail-"
94 :group 'pmail)
95
96 (defgroup pmail-reply nil
97 "Pmail reply options."
98 :prefix "pmail-"
99 :group 'pmail)
100
101 (defgroup pmail-summary nil
102 "Pmail summary options."
103 :prefix "pmail-"
104 :prefix "pmail-summary-"
105 :group 'pmail)
106
107 (defgroup pmail-output nil
108 "Output message to a file."
109 :prefix "pmail-output-"
110 :prefix "pmail-"
111 :group 'pmail)
112
113 (defgroup pmail-edit nil
114 "Pmail editing."
115 :prefix "pmail-edit-"
116 :group 'pmail)
117
118 (defgroup pmail-obsolete nil
119 "Pmail obsolete customization variables."
120 :group 'pmail)
121
122 (defcustom pmail-movemail-program nil
123 "If non-nil, the file name of the `movemail' program."
124 :group 'pmail-retrieve
125 :type '(choice (const nil) string))
126
127 (defcustom pmail-pop-password nil
128 "*Password to use when reading mail from POP server.
129 Please use `pmail-remote-password' instead."
130 :type '(choice (string :tag "Password")
131 (const :tag "Not Required" nil))
132 :group 'pmail-obsolete)
133
134 (defcustom pmail-pop-password-required nil
135 "*Non-nil if a password is required when reading mail from a POP server.
136 Please use pmail-remote-password-required instead."
137 :type 'boolean
138 :group 'pmail-obsolete)
139
140 (defcustom pmail-remote-password nil
141 "*Password to use when reading mail from a remote server.
142 This setting is ignored for mailboxes whose URL already contains a password."
143 :type '(choice (string :tag "Password")
144 (const :tag "Not Required" nil))
145 :set-after '(pmail-pop-password)
146 :set #'(lambda (symbol value)
147 (set-default symbol
148 (if (and (not value)
149 (boundp 'pmail-pop-password)
150 pmail-pop-password)
151 pmail-pop-password
152 value))
153 (setq pmail-pop-password nil))
154 :group 'pmail-retrieve
155 :version "22.1")
156
157 (defcustom pmail-remote-password-required nil
158 "*Non-nil if a password is required when reading mail from a remote server."
159 :type 'boolean
160 :set-after '(pmail-pop-password-required)
161 :set #'(lambda (symbol value)
162 (set-default symbol
163 (if (and (not value)
164 (boundp 'pmail-pop-password-required)
165 pmail-pop-password-required)
166 pmail-pop-password-required
167 value))
168 (setq pmail-pop-password-required nil))
169 :group 'pmail-retrieve
170 :version "22.1")
171
172 (defcustom pmail-movemail-flags nil
173 "*List of flags to pass to movemail.
174 Most commonly used to specify `-g' to enable GSS-API authentication
175 or `-k' to enable Kerberos authentication."
176 :type '(repeat string)
177 :group 'pmail-retrieve
178 :version "20.3")
179
180 (defvar pmail-remote-password-error "invalid usercode or password\\|
181 unknown user name or bad password\\|Authentication failed\\|MU_ERR_AUTH_FAILURE"
182 "Regular expression matching incorrect-password POP or IMAP server error
183 messages.
184 If you get an incorrect-password error that this expression does not match,
185 please report it with \\[report-emacs-bug].")
186
187 (defvar pmail-encoded-remote-password nil)
188
189 (defcustom pmail-preserve-inbox nil
190 "*Non-nil means leave incoming mail in the user's inbox--don't delete it."
191 :type 'boolean
192 :group 'pmail-retrieve)
193
194 (defcustom pmail-movemail-search-path nil
195 "*List of directories to search for movemail (in addition to `exec-path')."
196 :group 'pmail-retrieve
197 :type '(repeat (directory)))
198
199 (declare-function mail-position-on-field "sendmail" (field &optional soft))
200 (declare-function mail-text-start "sendmail" ())
201 (declare-function pmail-update-summary "pmailsum" (&rest ignore))
202
203 (defun pmail-probe (prog)
204 "Determine what flavor of movemail PROG is.
205 We do this by executing it with `--version' and analyzing its output."
206 (with-temp-buffer
207 (let ((tbuf (current-buffer)))
208 (buffer-disable-undo tbuf)
209 (call-process prog nil tbuf nil "--version")
210 (if (not (buffer-modified-p tbuf))
211 ;; Should not happen...
212 nil
213 (goto-char (point-min))
214 (cond
215 ((looking-at ".*movemail: invalid option")
216 'emacs) ;; Possibly...
217 ((looking-at "movemail (GNU Mailutils .*)")
218 'mailutils)
219 (t
220 ;; FIXME:
221 'emacs))))))
222
223 (defun pmail-autodetect ()
224 "Determine the file name of the `movemail' program and return its flavor.
225 If `pmail-movemail-program' is non-nil, use it.
226 Otherwise, look for `movemail' in the directories in
227 `pmail-movemail-search-path', those in `exec-path', and `exec-directory'."
228 (if pmail-movemail-program
229 (pmail-probe pmail-movemail-program)
230 (catch 'scan
231 (dolist (dir (append pmail-movemail-search-path exec-path
232 (list exec-directory)))
233 (when (and dir (file-accessible-directory-p dir))
234 ;; Previously, this didn't have to work on Windows, because
235 ;; pmail-insert-inbox-text before r1.439 fell back to using
236 ;; (expand-file-name "movemail" exec-directory) and just
237 ;; assuming it would work.
238 ;; http://lists.gnu.org/archive/html/bug-gnu-emacs/2008-02/msg00087.html
239 (let ((progname (expand-file-name
240 (concat "movemail"
241 (if (memq system-type '(ms-dos windows-nt))
242 ".exe")) dir)))
243 (when (and (not (file-directory-p progname))
244 (file-executable-p progname))
245 (let ((x (pmail-probe progname)))
246 (when x
247 (setq pmail-movemail-program progname)
248 (throw 'scan x))))))))))
249
250 (defvar pmail-movemail-variant-in-use nil
251 "The movemail variant currently in use. Known variants are:
252
253 `emacs' Means any implementation, compatible with the native Emacs one.
254 This is the default;
255 `mailutils' Means GNU mailutils implementation, capable of handling full
256 mail URLs as the source mailbox.")
257
258 ;;;###autoload
259 (defun pmail-movemail-variant-p (&rest variants)
260 "Return t if the current movemail variant is any of VARIANTS.
261 Currently known variants are 'emacs and 'mailutils."
262 (when (not pmail-movemail-variant-in-use)
263 ;; Autodetect
264 (setq pmail-movemail-variant-in-use (pmail-autodetect)))
265 (not (null (member pmail-movemail-variant-in-use variants))))
266
267 ;; Call for effect, to set pmail-movemail-program (if not set by the
268 ;; user), and pmail-movemail-variant-in-use. Used by various functions.
269 ;; I'm not sure if M-x pmail is the only entry point to this package.
270 ;; If so, this can be moved there.
271 (pmail-movemail-variant-p)
272
273 ;;;###autoload
274 (defcustom pmail-dont-reply-to-names nil "\
275 *A regexp specifying addresses to prune from a reply message.
276 A value of nil means exclude your own email address as an address
277 plus whatever is specified by `pmail-default-dont-reply-to-names'."
278 :type '(choice regexp (const :tag "Your Name" nil))
279 :group 'pmail-reply)
280
281 ;;;###autoload
282 (defvar pmail-default-dont-reply-to-names "\\`info-" "\
283 A regular expression specifying part of the default value of the
284 variable `pmail-dont-reply-to-names', for when the user does not set
285 `pmail-dont-reply-to-names' explicitly. (The other part of the default
286 value is the user's email address and name.)
287 It is useful to set this variable in the site customization file.")
288
289 ;;;###autoload
290 (defcustom pmail-ignored-headers
291 (concat "^via:\\|^mail-from:\\|^origin:\\|^references:\\|^sender:"
292 "\\|^status:\\|^received:\\|^content-transfer-encoding:"
293 "\\|^x400-\\(received\\|mts-identifier\\|content-type\\|originator\\|recipients\\):"
294 "\\|^list-\\(help\\|post\\|subscribe\\|id\\|unsubscribe\\|archive\\):"
295 "\\|^resent-\\(face\\|x-.*\\|organization\\|openpgp\\|date\\|message-id\\):"
296 "\\|^thread-\\(topic\\|index\\)"
297 "\\|^summary-line:\\|^precedence:\\|^message-id:"
298 "\\|^path:\\|^face:\\|^delivered-to:\\|^lines:"
299 "\\|^return-path:\\|^errors-to:\\|^return-receipt-to:"
300 "\\|^content-\\(length\\|type\\|class\\|disposition\\):"
301 "\\|^nntp-posting-host:\\|^path:\\|^x-char.*:\\|^x-face:\\|^face:"
302 "\\|^importance:\\|^envelope-to:\\|^delivery-date\\|^openpgp:"
303 "\\|^mbox-line:\\|^cancel-lock:\\|^in-reply-to:\\|^comment:"
304 "\\|^x-.*:\\|^domainkey-signature:"
305 "\\|^original-recipient:\\|^from ")
306 "*Regexp to match header fields that Pmail should normally hide.
307 \(See also `pmail-nonignored-headers', which overrides this regexp.)
308 This variable is used for reformatting the message header,
309 which normally happens once for each message,
310 when you view the message for the first time in Pmail.
311 To make a change in this variable take effect
312 for a message that you have already viewed,
313 go to that message and type \\[pmail-toggle-header] twice."
314 :type 'regexp
315 :group 'pmail-headers)
316
317 (defcustom pmail-nonignored-headers "^x-spam-status:"
318 "*Regexp to match X header fields that Pmail should show.
319 This regexp overrides `pmail-ignored-headers'; if both this regexp
320 and that one match a certain header field, Pmail shows the field.
321 If this is nil, ignore all header fields in `pmail-ignored-headers'.
322
323 This variable is used for reformatting the message header,
324 which normally happens once for each message,
325 when you view the message for the first time in Pmail.
326 To make a change in this variable take effect
327 for a message that you have already viewed,
328 go to that message and type \\[pmail-toggle-header] twice."
329 :type '(choice (const nil) (regexp))
330 :group 'pmail-headers)
331
332 ;;;###autoload
333 (defcustom pmail-displayed-headers nil
334 "*Regexp to match Header fields that Pmail should display.
335 If nil, display all header fields except those matched by
336 `pmail-ignored-headers'."
337 :type '(choice regexp (const :tag "All"))
338 :group 'pmail-headers)
339
340 ;;;###autoload
341 (defcustom pmail-retry-ignored-headers "^x-authentication-warning:" "\
342 *Headers that should be stripped when retrying a failed message."
343 :type '(choice regexp (const nil :tag "None"))
344 :group 'pmail-headers)
345
346 ;;;###autoload
347 (defcustom pmail-highlighted-headers "^From:\\|^Subject:" "\
348 *Regexp to match Header fields that Pmail should normally highlight.
349 A value of nil means don't highlight.
350 See also `pmail-highlight-face'."
351 :type 'regexp
352 :group 'pmail-headers)
353
354 (defface pmail-highlight
355 '((t (:inherit highlight)))
356 "Face to use for highlighting the most important header fields."
357 :group 'pmail-headers
358 :version "22.1")
359
360 (defface pmail-header-name
361 '((t (:inherit font-lock-function-name-face)))
362 "Face to use for highlighting the header names."
363 :group 'pmail-headers
364 :version "23.1")
365
366 ;;;###autoload
367 (defcustom pmail-highlight-face 'pmail-highlight "\
368 *Face used by Pmail for highlighting sender and subject.
369 See `pmail-font-lock-keywords'."
370 :type '(choice (const :tag "Default" nil)
371 face)
372 :group 'pmail-headers)
373
374 ;;;###autoload
375 (defcustom pmail-delete-after-output nil "\
376 *Non-nil means automatically delete a message that is copied to a file."
377 :type 'boolean
378 :group 'pmail-files)
379
380 ;;;###autoload
381 (defcustom pmail-primary-inbox-list nil "\
382 *List of files which are inboxes for user's primary mail file `~/PMAIL'.
383 nil means the default, which is (\"/usr/spool/mail/$USER\")
384 \(the name varies depending on the operating system,
385 and the value of the environment variable MAIL overrides it)."
386 ;; Don't use backquote here, because we don't want to need it
387 ;; at load time.
388 :type (list 'choice '(const :tag "Default" nil)
389 (list 'repeat ':value (list (or (getenv "MAIL")
390 (concat "/var/spool/mail/"
391 (getenv "USER"))))
392 'file))
393 :group 'pmail-retrieve
394 :group 'pmail-files)
395
396 ;;;###autoload
397 (defcustom pmail-inbox-alist nil
398 "*Alist of mail files and backup directory names.
399 Each element has the form (MAIL-FILE INBOX ...). When running
400 pmail on MAIL-FILE, mails in all the INBOX files listed will be
401 moved to the MAIL-FILE. Be sure to fully qualify your MAIL-FILE.
402
403 Example setting if procmail delivers all your spam to
404 ~/Mail/SPAM.in and you read it from the file ~/Mail/SPAM:
405
406 \(setq pmail-inbox-alist '((\"~/Mail/SPAM\" \"~/Mail/SPAM.in\")))"
407 :type '(alist :key-type file :value-type (repeat file))
408 :group 'pmail-retrieve
409 :group 'pmail-files
410 :version "22.1")
411
412 ;;;###autoload
413 (defcustom pmail-mail-new-frame nil
414 "*Non-nil means Pmail makes a new frame for composing outgoing mail.
415 This is handy if you want to preserve the window configuration of
416 the frame where you have the PMAIL buffer displayed."
417 :type 'boolean
418 :group 'pmail-reply)
419
420 ;;;###autoload
421 (defcustom pmail-secondary-file-directory "~/"
422 "*Directory for additional secondary Pmail files."
423 :type 'directory
424 :group 'pmail-files)
425 ;;;###autoload
426 (defcustom pmail-secondary-file-regexp "\\.xmail$"
427 "*Regexp for which files are secondary Pmail files."
428 :type 'regexp
429 :group 'pmail-files)
430
431 ;;;###autoload
432 (defcustom pmail-confirm-expunge 'y-or-n-p
433 "*Whether and how to ask for confirmation before expunging deleted messages."
434 :type '(choice (const :tag "No confirmation" nil)
435 (const :tag "Confirm with y-or-n-p" y-or-n-p)
436 (const :tag "Confirm with yes-or-no-p" yes-or-no-p))
437 :version "21.1"
438 :group 'pmail-files)
439
440 ;;;###autoload
441 (defvar pmail-mode-hook nil
442 "List of functions to call when Pmail is invoked.")
443
444 ;;;###autoload
445 (defvar pmail-get-new-mail-hook nil
446 "List of functions to call when Pmail has retrieved new mail.")
447
448 ;;;###autoload
449 (defcustom pmail-show-message-hook nil
450 "List of functions to call when Pmail displays a message."
451 :type 'hook
452 :options '(goto-address)
453 :group 'pmail)
454
455 ;;;###autoload
456 (defvar pmail-quit-hook nil
457 "List of functions to call when quitting out of Pmail.")
458
459 ;;;###autoload
460 (defvar pmail-delete-message-hook nil
461 "List of functions to call when Pmail deletes a message.
462 When the hooks are called, the message has been marked deleted but is
463 still the current message in the Pmail buffer.")
464
465 ;; These may be altered by site-init.el to match the format of mmdf files
466 ;; delimiting used on a given host (delim1 and delim2 from the config
467 ;; files).
468
469 (defvar pmail-mmdf-delim1 "^\001\001\001\001\n"
470 "Regexp marking the start of an mmdf message.")
471 (defvar pmail-mmdf-delim2 "^\001\001\001\001\n"
472 "Regexp marking the end of an mmdf message.")
473
474 (defcustom pmail-message-filter nil
475 "If non-nil, a filter function for new messages in PMAIL.
476 Called with region narrowed to the message, including headers,
477 before obeying `pmail-ignored-headers'."
478 :group 'pmail-headers
479 :type '(choice (const nil) function))
480
481 (defcustom pmail-automatic-folder-directives nil
482 "List of directives specifying where to put a message.
483 Each element of the list is of the form:
484
485 (FOLDERNAME FIELD REGEXP [ FIELD REGEXP ] ... )
486
487 Where FOLDERNAME is the name of a BABYL Version 6 (also known as mbox
488 or Unix inbox format) folder to put the message. If any of the field
489 regexp's are nil, then it is ignored.
490
491 If FOLDERNAME is \"/dev/null\", it is deleted.
492 If FOLDERNAME is nil then it is deleted, and skipped.
493
494 FIELD is the plain text name of a field in the message, such as
495 \"subject\" or \"from\". A FIELD of \"to\" will automatically include
496 all text from the \"cc\" field as well.
497
498 REGEXP is an expression to match in the preceeding specified FIELD.
499 FIELD/REGEXP pairs continue in the list.
500
501 examples:
502 (\"/dev/null\" \"from\" \"@spam.com\") ; delete all mail from spam.com
503 (\"RMS\" \"from\" \"rms@\") ; save all mail from RMS."
504 :group 'pmail
505 :version "21.1"
506 :type '(repeat (sexp :tag "Directive")))
507
508 (defvar pmail-reply-prefix "Re: "
509 "String to prepend to Subject line when replying to a message.")
510
511 ;; Some mailers use "Re(2):" or "Re^2:" or "Re: Re:" or "Re[2]:".
512 ;; This pattern should catch all the common variants.
513 ;; rms: I deleted the change to delete tags in square brackets
514 ;; because they mess up RT tags.
515 (defvar pmail-reply-regexp "\\`\\(Re\\(([0-9]+)\\|\\[[0-9]+\\]\\|\\^[0-9]+\\)?: *\\)*"
516 "Regexp to delete from Subject line before inserting `pmail-reply-prefix'.")
517
518 (defcustom pmail-display-summary nil
519 "*If non-nil, Pmail always displays the summary buffer."
520 :group 'pmail-summary
521 :type 'boolean)
522
523 (defvar pmail-inbox-list nil)
524 (put 'pmail-inbox-list 'permanent-local t)
525
526 (defvar pmail-keywords nil)
527 (put 'pmail-keywords 'permanent-local t)
528
529 (defvar pmail-buffer nil
530 "The PMAIL buffer related to the current buffer.
531 In an PMAIL buffer, this holds the PMAIL buffer itself.
532 In a summary buffer, this holds the PMAIL buffer it is a summary for.")
533 (put 'pmail-buffer 'permanent-local t)
534
535 ;; Message counters and markers. Deleted flags.
536
537 (defvar pmail-current-message nil)
538 (put 'pmail-current-message 'permanent-local t)
539
540 (defvar pmail-total-messages nil)
541 (put 'pmail-total-messages 'permanent-local t)
542
543 (defvar pmail-overlay-list nil)
544 (put 'pmail-overlay-list 'permanent-local t)
545
546 ;; These are used by autoloaded pmail-summary.
547
548 (defvar pmail-summary-buffer nil)
549 (put 'pmail-summary-buffer 'permanent-local t)
550
551 (defvar pmail-view-buffer nil
552 "Buffer which holds PMAIL message for MIME displaying.")
553 (put 'pmail-view-buffer 'permanent-local t)
554
555 ;; `Sticky' default variables.
556
557 ;; Last individual label specified to a or k.
558 (defvar pmail-last-label nil)
559 (put 'pmail-last-label 'permanent-local t)
560
561 ;; Last set of values specified to C-M-n, C-M-p, C-M-s or C-M-l.
562 (defvar pmail-last-multi-labels nil)
563
564 (defvar pmail-last-regexp nil)
565 (put 'pmail-last-regexp 'permanent-local t)
566
567 (defcustom pmail-default-file "~/xmail"
568 "*Default file name for \\[pmail-output]."
569 :type 'file
570 :group 'pmail-files)
571
572 (defcustom pmail-default-pmail-file "~/XMAIL"
573 "*Default file name for \\[pmail-output-to-pmail-file]."
574 :type 'file
575 :group 'pmail-files)
576
577 (defcustom pmail-default-body-file "~/mailout"
578 "*Default file name for \\[pmail-output-body-to-file]."
579 :type 'file
580 :group 'pmail-files
581 :version "20.3")
582
583 ;; Mule and MIME related variables.
584
585 ;;;###autoload
586 (defvar pmail-file-coding-system nil
587 "Coding system used in PMAIL file.
588
589 This is set to nil by default.")
590
591 ;;;###autoload
592 (defcustom pmail-enable-mime nil
593 "*If non-nil, PMAIL uses MIME feature.
594 If the value is t, PMAIL automatically shows MIME decoded message.
595 If the value is neither t nor nil, PMAIL does not show MIME decoded message
596 until a user explicitly requires it.
597
598 Even if the value is non-nil, you can't use MIME feature
599 if the feature specified by `pmail-mime-feature' is not available
600 in your session."
601 :type '(choice (const :tag "on" t)
602 (const :tag "off" nil)
603 (other :tag "when asked" ask))
604 :group 'pmail)
605
606 (defvar pmail-enable-mime-composing nil
607 "*If non-nil, PMAIL uses `pmail-insert-mime-forwarded-message-function' to forward.")
608
609 ;;;###autoload
610 (defvar pmail-show-mime-function nil
611 "Function to show MIME decoded message of PMAIL file.
612 This function is called when `pmail-enable-mime' is non-nil.
613 It is called with no argument.")
614
615 ;;;###autoload
616 (defvar pmail-insert-mime-forwarded-message-function nil
617 "Function to insert a message in MIME format so it can be forwarded.
618 This function is called if `pmail-enable-mime' or
619 `pmail-enable-mime-composing' is non-nil.
620 It is called with one argument FORWARD-BUFFER, which is a
621 buffer containing the message to forward. The current buffer
622 is the outgoing mail buffer.")
623
624 ;;;###autoload
625 (defvar pmail-insert-mime-resent-message-function nil
626 "Function to insert a message in MIME format so it can be resent.
627 This function is called if `pmail-enable-mime' is non-nil.
628 It is called with one argument FORWARD-BUFFER, which is a
629 buffer containing the message to forward. The current buffer
630 is the outgoing mail buffer.")
631
632 ;;;###autoload
633 (defvar pmail-search-mime-message-function nil
634 "Function to check if a regexp matches a MIME message.
635 This function is called if `pmail-enable-mime' is non-nil.
636 It is called with two arguments MSG and REGEXP, where
637 MSG is the message number, REGEXP is the regular expression.")
638
639 ;;;###autoload
640 (defvar pmail-search-mime-header-function nil
641 "Function to check if a regexp matches a header of MIME message.
642 This function is called if `pmail-enable-mime' is non-nil.
643 It is called with three arguments MSG, REGEXP, and LIMIT, where
644 MSG is the message number,
645 REGEXP is the regular expression,
646 LIMIT is the position specifying the end of header.")
647
648 ;;;###autoload
649 (defvar pmail-mime-feature 'pmail-mime
650 "Feature to require to load MIME support in Pmail.
651 When starting Pmail, if `pmail-enable-mime' is non-nil,
652 this feature is required with `require'.
653
654 The default value is `pmail-mime'. This feature is provided by
655 the pmail-mime package available at <http://www.m17n.org/pmail-mime/>.")
656
657 ;;;###autoload
658 (defvar pmail-decode-mime-charset t
659 "*Non-nil means a message is decoded by MIME's charset specification.
660 If this variable is nil, or the message has not MIME specification,
661 the message is decoded as normal way.
662
663 If the variable `pmail-enable-mime' is non-nil, this variables is
664 ignored, and all the decoding work is done by a feature specified by
665 the variable `pmail-mime-feature'.")
666
667 ;;;###autoload
668 (defvar pmail-mime-charset-pattern
669 (concat "^content-type:[ \t]*text/plain;"
670 "\\(?:[ \t\n]*\\(?:format\\|delsp\\)=\"?[-a-z0-9]+\"?;\\)*"
671 "[ \t\n]*charset=\"?\\([^ \t\n\";]+\\)\"?")
672 "Regexp to match MIME-charset specification in a header of message.
673 The first parenthesized expression should match the MIME-charset name.")
674
675
676 ;;; Regexp matching the delimiter of messages in UNIX mail format
677 ;;; (UNIX From lines), with an initial ^. Used in pmail-decode-from-line,
678 ;;; which knows the exact ordering of the \\(...\\) subexpressions.
679 (defvar pmail-unix-mail-delimiter
680 (let ((time-zone-regexp
681 (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
682 "\\|[-+]?[0-9][0-9][0-9][0-9]"
683 "\\|"
684 "\\) *")))
685 (concat
686 "^From "
687
688 ;; Many things can happen to an RFC 822 mailbox before it is put into
689 ;; a `From' line. The leading phrase can be stripped, e.g.
690 ;; `Joe <@w.x:joe@y.z>' -> `<@w.x:joe@y.z>'. The <> can be stripped, e.g.
691 ;; `<@x.y:joe@y.z>' -> `@x.y:joe@y.z'. Everything starting with a CRLF
692 ;; can be removed, e.g.
693 ;; From: joe@y.z (Joe K
694 ;; User)
695 ;; can yield `From joe@y.z (Joe K Fri Mar 22 08:11:15 1996', and
696 ;; From: Joe User
697 ;; <joe@y.z>
698 ;; can yield `From Joe User Fri Mar 22 08:11:15 1996'.
699 ;; The mailbox can be removed or be replaced by white space, e.g.
700 ;; From: "Joe User"{space}{tab}
701 ;; <joe@y.z>
702 ;; can yield `From {space}{tab} Fri Mar 22 08:11:15 1996',
703 ;; where {space} and {tab} represent the Ascii space and tab characters.
704 ;; We want to match the results of any of these manglings.
705 ;; The following regexp rejects names whose first characters are
706 ;; obviously bogus, but after that anything goes.
707 "\\([^\0-\b\n-\r\^?].*\\)? "
708
709 ;; The time the message was sent.
710 "\\([^\0-\r \^?]+\\) +" ; day of the week
711 "\\([^\0-\r \^?]+\\) +" ; month
712 "\\([0-3]?[0-9]\\) +" ; day of month
713 "\\([0-2][0-9]:[0-5][0-9]\\(:[0-6][0-9]\\)?\\) *" ; time of day
714
715 ;; Perhaps a time zone, specified by an abbreviation, or by a
716 ;; numeric offset.
717 time-zone-regexp
718
719 ;; The year.
720 " \\([0-9][0-9]+\\) *"
721
722 ;; On some systems the time zone can appear after the year, too.
723 time-zone-regexp
724
725 ;; Old uucp cruft.
726 "\\(remote from .*\\)?"
727
728 "\n"))
729 nil)
730
731 (defvar pmail-font-lock-keywords
732 ;; These are all matched case-insensitively.
733 (eval-when-compile
734 (let* ((cite-chars "[>|}]")
735 (cite-prefix "a-z")
736 (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
737 (list '("^\\(Sender\\|Resent-From\\):"
738 . font-lock-function-name-face)
739 '("^Reply-To:.*$" . font-lock-function-name-face)
740 '("^\\(From:\\)\\(.*\\(\n[ \t]+.*\\)*\\)"
741 (1 font-lock-function-name-face)
742 (2 pmail-highlight-face))
743 '("^\\(Subject:\\)\\(.*\\(\n[ \t]+.*\\)*\\)"
744 (1 font-lock-comment-face)
745 (2 pmail-highlight-face))
746 '("^X-Spam-Status:" . font-lock-keyword-face)
747 '("^\\(To\\|Apparently-To\\|Cc\\|Newsgroups\\):"
748 . font-lock-keyword-face)
749 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
750 `(,cite-chars
751 (,(concat "\\=[ \t]*"
752 "\\(\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
753 "\\(" cite-chars "[ \t]*\\)\\)+\\)"
754 "\\(.*\\)")
755 (beginning-of-line) (end-of-line)
756 (1 font-lock-comment-delimiter-face nil t)
757 (5 font-lock-comment-face nil t)))
758 '("^\\(X-[a-z0-9-]+\\|In-reply-to\\|Date\\):.*\\(\n[ \t]+.*\\)*$"
759 . 'pmail-header-name))))
760 "Additional expressions to highlight in Pmail mode.")
761
762 ;; Perform BODY in the summary buffer
763 ;; in such a way that its cursor is properly updated in its own window.
764 (defmacro pmail-select-summary (&rest body)
765 `(let ((total pmail-total-messages))
766 (if (pmail-summary-displayed)
767 (let ((window (selected-window)))
768 (save-excursion
769 (unwind-protect
770 (progn
771 (pop-to-buffer pmail-summary-buffer)
772 ;; pmail-total-messages is a buffer-local var
773 ;; in the pmail buffer.
774 ;; This way we make it available for the body
775 ;; even tho the pmail buffer is not current.
776 (let ((pmail-total-messages total))
777 ,@body))
778 (select-window window))))
779 (save-excursion
780 (set-buffer pmail-summary-buffer)
781 (let ((pmail-total-messages total))
782 ,@body)))
783 (pmail-maybe-display-summary)))
784
785 ;;;; *** Pmail Mode ***
786
787 ;; This variable is dynamically bound. The defvar is here to placate
788 ;; the byte compiler.
789
790 (defvar pmail-enable-multibyte nil)
791
792 ;; Avoid errors.
793 (defvar pmail-use-spam-filter nil)
794
795 (defun pmail-require-mime-maybe ()
796 "Require `pmail-mime-feature' if that is non-nil.
797 Signal an error and set `pmail-mime-feature' to nil if the feature
798 isn't provided."
799 (when pmail-enable-mime
800 (condition-case err
801 (require pmail-mime-feature)
802 (error
803 (display-warning
804 :warning
805 (format "Although MIME support is requested
806 by setting `pmail-enable-mime' to non-nil, the required feature
807 `%s' (the value of `pmail-mime-feature')
808 is not available in the current session.
809 So, the MIME support is turned off for the moment."
810 pmail-mime-feature))
811 (setq pmail-enable-mime nil)))))
812
813 ;;;###autoload
814 (defun pmail (&optional file-name-arg)
815 "Read and edit incoming mail.
816 Moves messages into file named by `pmail-file-name' (a babyl format file)
817 and edits that file in PMAIL Mode.
818 Type \\[describe-mode] once editing that file, for a list of PMAIL commands.
819
820 May be called with file name as argument; then performs pmail editing on
821 that file, but does not copy any new mail into the file.
822 Interactively, if you supply a prefix argument, then you
823 have a chance to specify a file name with the minibuffer.
824
825 If `pmail-display-summary' is non-nil, make a summary for this PMAIL file."
826 (interactive (if current-prefix-arg
827 (list (read-file-name "Run pmail on PMAIL file: "))))
828 (pmail-require-mime-maybe)
829 (let* ((file-name (expand-file-name (or file-name-arg pmail-file-name)))
830 ;; Use find-buffer-visiting, not get-file-buffer, for those users
831 ;; who have find-file-visit-truename set to t.
832 (existed (find-buffer-visiting file-name))
833 ;; This binding is necessary because we must decide if we
834 ;; need code conversion while the buffer is unibyte
835 ;; (i.e. enable-multibyte-characters is nil).
836 (pmail-enable-multibyte
837 (if existed
838 (with-current-buffer existed enable-multibyte-characters)
839 (default-value 'enable-multibyte-characters)))
840 run-mail-hook msg-shown)
841 (when (and existed (eq major-mode 'pmail-edit-mode))
842 (error "Exit Pmail Edit mode before getting new mail"))
843 (if (and existed (not (verify-visited-file-modtime existed)))
844 (progn
845 (find-file file-name)
846 (when (and (verify-visited-file-modtime existed)
847 (eq major-mode 'pmail-mode))
848 (setq major-mode 'fundamental-mode)))
849 (switch-to-buffer
850 (let ((enable-local-variables nil))
851 (find-file-noselect file-name)))
852 ;; As we have read a file as raw-text, the buffer is set to
853 ;; unibyte. We must make it multibyte if necessary.
854 (when (and pmail-enable-multibyte
855 (not enable-multibyte-characters))
856 (set-buffer-multibyte t)))
857 ;; Make sure we're in pmail-mode, even if the buffer did exist and
858 ;; the file was not changed.
859 (unless (eq major-mode 'pmail-mode)
860 ;; If file looks like a Babyl file, save it to a temp file,
861 ;; convert it, and replace the current content with the
862 ;; converted content. Don't save -- let the user do it.
863 (goto-char (point-min))
864 (when (looking-at "BABYL OPTIONS:")
865 (let ((old-file (make-temp-file "pmail"))
866 (new-file (make-temp-file "pmail")))
867 (unwind-protect
868 (progn
869 (write-region (point-min) (point-max) old-file)
870 (unpmail old-file new-file)
871 (message "Replacing BABYL format with mbox format...")
872 (let ((inhibit-read-only t))
873 (erase-buffer)
874 (insert-file-contents-literally new-file))
875 (message "Replacing BABYL format with mbox format...done"))
876 (delete-file old-file)
877 (delete-file new-file))))
878 (goto-char (point-max))
879 (pmail-mode-2)
880 ;; Convert all or parts of file to a format Pmail understands
881 (pmail-convert-file)
882 ;; We use `run-mail-hook' to remember whether we should run
883 ;; `pmail-mode-hook' at the end.
884 (setq run-mail-hook t)
885 ;; Initialize the Pmail state.
886 (pmail-initialize-messages))
887 ;; Now we're back in business. The happens even if we had a
888 ;; perfectly fine file.
889 (unwind-protect
890 (unless (and (not file-name-arg) (pmail-get-new-mail))
891 (pmail-show-message (pmail-first-unseen-message)))
892 (when pmail-display-summary
893 (pmail-summary))
894 (pmail-construct-io-menu)
895 ;; Run any callbacks if the buffer was not in pmail-mode
896 (when run-mail-hook
897 (run-hooks 'pmail-mode-hook)))))
898
899 (defun pmail-convert-file ()
900 "Convert unconverted messages.
901 A message is unconverted if it doesn't have the BABYL header
902 specified in `pmail-header-attribute-header'; it is converted
903 using `pmail-convert-mbox-format'."
904 (let ((convert
905 (save-restriction
906 (widen)
907 (let ((case-fold-search nil)
908 (start (point-max))
909 end)
910 (catch 'convert
911 (goto-char start)
912 (while (re-search-backward
913 pmail-unix-mail-delimiter nil t)
914 (setq end start)
915 (setq start (point))
916 (save-excursion
917 (save-restriction
918 (narrow-to-region start end)
919 (goto-char start)
920 (let ((attribute (pmail-header-get-header
921 pmail-header-attribute-header)))
922 (unless attribute
923 (throw 'convert t)))))))))))
924 (if convert
925 (let ((inhibit-read-only t))
926 (pmail-convert-mbox-format)))))
927
928 (defun pmail-initialize-messages ()
929 "Initialize message state based on messages in the buffer."
930 (setq pmail-total-messages 0
931 pmail-current-message 1)
932 (pmail-desc-clear-descriptors)
933 (widen)
934 (pmail-header-show-headers)
935 (setq pmail-total-messages (pmail-process-new-messages)))
936
937 (defvar pmail-mode-map
938 (let ((map (make-sparse-keymap)))
939 (suppress-keymap map)
940 (define-key map "a" 'pmail-add-label)
941 (define-key map "b" 'pmail-bury)
942 (define-key map "c" 'pmail-continue)
943 (define-key map "d" 'pmail-delete-forward)
944 (define-key map "\C-d" 'pmail-delete-backward)
945 (define-key map "e" 'pmail-edit-current-message)
946 (define-key map "f" 'pmail-forward)
947 (define-key map "g" 'pmail-get-new-mail)
948 (define-key map "h" 'pmail-summary)
949 (define-key map "i" 'pmail-input)
950 (define-key map "j" 'pmail-show-message)
951 (define-key map "k" 'pmail-kill-label)
952 (define-key map "l" 'pmail-summary-by-labels)
953 (define-key map "\e\C-h" 'pmail-summary)
954 (define-key map "\e\C-l" 'pmail-summary-by-labels)
955 (define-key map "\e\C-r" 'pmail-summary-by-recipients)
956 (define-key map "\e\C-s" 'pmail-summary-by-regexp)
957 (define-key map "\e\C-t" 'pmail-summary-by-topic)
958 (define-key map "m" 'pmail-mail)
959 (define-key map "\em" 'pmail-retry-failure)
960 (define-key map "n" 'pmail-next-undeleted-message)
961 (define-key map "\en" 'pmail-next-message)
962 (define-key map "\e\C-n" 'pmail-next-labeled-message)
963 (define-key map "o" 'pmail-output)
964 (define-key map "\C-o" 'pmail-output)
965 (define-key map "p" 'pmail-previous-undeleted-message)
966 (define-key map "\ep" 'pmail-previous-message)
967 (define-key map "\e\C-p" 'pmail-previous-labeled-message)
968 (define-key map "q" 'pmail-quit)
969 (define-key map "r" 'pmail-reply)
970 ;; I find I can't live without the default M-r command -- rms.
971 ;; (define-key map "\er" 'pmail-search-backwards)
972 (define-key map "s" 'pmail-expunge-and-save)
973 (define-key map "\es" 'pmail-search)
974 (define-key map "t" 'pmail-toggle-header)
975 (define-key map "u" 'pmail-undelete-previous-message)
976 (define-key map "w" 'pmail-output-body-to-file)
977 (define-key map "x" 'pmail-expunge)
978 (define-key map "." 'pmail-beginning-of-message)
979 (define-key map "/" 'pmail-end-of-message)
980 (define-key map "<" 'pmail-first-message)
981 (define-key map ">" 'pmail-last-message)
982 (define-key map " " 'scroll-up)
983 (define-key map "\177" 'scroll-down)
984 (define-key map "?" 'describe-mode)
985 (define-key map "\C-c\C-s\C-d" 'pmail-sort-by-date)
986 (define-key map "\C-c\C-s\C-s" 'pmail-sort-by-subject)
987 (define-key map "\C-c\C-s\C-a" 'pmail-sort-by-author)
988 (define-key map "\C-c\C-s\C-r" 'pmail-sort-by-recipient)
989 (define-key map "\C-c\C-s\C-c" 'pmail-sort-by-correspondent)
990 (define-key map "\C-c\C-s\C-l" 'pmail-sort-by-lines)
991 (define-key map "\C-c\C-s\C-k" 'pmail-sort-by-labels)
992 (define-key map "\C-c\C-n" 'pmail-next-same-subject)
993 (define-key map "\C-c\C-p" 'pmail-previous-same-subject)
994 (define-key map [menu-bar] (make-sparse-keymap))
995 (define-key map [menu-bar classify]
996 (cons "Classify" (make-sparse-keymap "Classify")))
997 (define-key map [menu-bar classify input-menu]
998 nil)
999 (define-key map [menu-bar classify output-menu]
1000 nil)
1001 (define-key map [menu-bar classify output-body]
1002 '("Output body to file..." . pmail-output-body-to-file))
1003 (define-key map [menu-bar classify output-inbox]
1004 '("Output (inbox)..." . pmail-output))
1005 (define-key map [menu-bar classify output]
1006 '("Output (Pmail)..." . pmail-output))
1007 (define-key map [menu-bar classify kill-label]
1008 '("Kill Label..." . pmail-kill-label))
1009 (define-key map [menu-bar classify add-label]
1010 '("Add Label..." . pmail-add-label))
1011 (define-key map [menu-bar summary]
1012 (cons "Summary" (make-sparse-keymap "Summary")))
1013 (define-key map [menu-bar summary senders]
1014 '("By Senders..." . pmail-summary-by-senders))
1015 (define-key map [menu-bar summary labels]
1016 '("By Labels..." . pmail-summary-by-labels))
1017 (define-key map [menu-bar summary recipients]
1018 '("By Recipients..." . pmail-summary-by-recipients))
1019 (define-key map [menu-bar summary topic]
1020 '("By Topic..." . pmail-summary-by-topic))
1021 (define-key map [menu-bar summary regexp]
1022 '("By Regexp..." . pmail-summary-by-regexp))
1023 (define-key map [menu-bar summary all]
1024 '("All" . pmail-summary))
1025 (define-key map [menu-bar mail]
1026 (cons "Mail" (make-sparse-keymap "Mail")))
1027 (define-key map [menu-bar mail pmail-get-new-mail]
1028 '("Get New Mail" . pmail-get-new-mail))
1029 (define-key map [menu-bar mail lambda]
1030 '("----"))
1031 (define-key map [menu-bar mail continue]
1032 '("Continue" . pmail-continue))
1033 (define-key map [menu-bar mail resend]
1034 '("Re-send..." . pmail-resend))
1035 (define-key map [menu-bar mail forward]
1036 '("Forward" . pmail-forward))
1037 (define-key map [menu-bar mail retry]
1038 '("Retry" . pmail-retry-failure))
1039 (define-key map [menu-bar mail reply]
1040 '("Reply" . pmail-reply))
1041 (define-key map [menu-bar mail mail]
1042 '("Mail" . pmail-mail))
1043 (define-key map [menu-bar delete]
1044 (cons "Delete" (make-sparse-keymap "Delete")))
1045 (define-key map [menu-bar delete expunge/save]
1046 '("Expunge/Save" . pmail-expunge-and-save))
1047 (define-key map [menu-bar delete expunge]
1048 '("Expunge" . pmail-expunge))
1049 (define-key map [menu-bar delete undelete]
1050 '("Undelete" . pmail-undelete-previous-message))
1051 (define-key map [menu-bar delete delete]
1052 '("Delete" . pmail-delete-forward))
1053 (define-key map [menu-bar move]
1054 (cons "Move" (make-sparse-keymap "Move")))
1055 (define-key map [menu-bar move search-back]
1056 '("Search Back..." . pmail-search-backwards))
1057 (define-key map [menu-bar move search]
1058 '("Search..." . pmail-search))
1059 (define-key map [menu-bar move previous]
1060 '("Previous Nondeleted" . pmail-previous-undeleted-message))
1061 (define-key map [menu-bar move next]
1062 '("Next Nondeleted" . pmail-next-undeleted-message))
1063 (define-key map [menu-bar move last]
1064 '("Last" . pmail-last-message))
1065 (define-key map [menu-bar move first]
1066 '("First" . pmail-first-message))
1067 (define-key map [menu-bar move previous]
1068 '("Previous" . pmail-previous-message))
1069 (define-key map [menu-bar move next]
1070 '("Next" . pmail-next-message))
1071 map)
1072 "Keymap for `pmail-mode'.")
1073
1074 ;; Pmail toolbar
1075 (defvar pmail-tool-bar-map
1076 (if (display-graphic-p)
1077 (let ((map (make-sparse-keymap)))
1078 (tool-bar-local-item-from-menu 'pmail-get-new-mail "mail/inbox"
1079 map pmail-mode-map)
1080 (tool-bar-local-item-from-menu 'pmail-next-undeleted-message "right-arrow"
1081 map pmail-mode-map)
1082 (tool-bar-local-item-from-menu 'pmail-previous-undeleted-message "left-arrow"
1083 map pmail-mode-map)
1084 (tool-bar-local-item-from-menu 'pmail-search "search"
1085 map pmail-mode-map)
1086 (tool-bar-local-item-from-menu 'pmail-input "open"
1087 map pmail-mode-map)
1088 (tool-bar-local-item-from-menu 'pmail-mail "mail/compose"
1089 map pmail-mode-map)
1090 (tool-bar-local-item-from-menu 'pmail-reply "mail/reply-all"
1091 map pmail-mode-map)
1092 (tool-bar-local-item-from-menu 'pmail-forward "mail/forward"
1093 map pmail-mode-map)
1094 (tool-bar-local-item-from-menu 'pmail-delete-forward "close"
1095 map pmail-mode-map)
1096 (tool-bar-local-item-from-menu 'pmail-output "mail/move"
1097 map pmail-mode-map)
1098 (tool-bar-local-item-from-menu 'pmail-output-body-to-file "mail/save"
1099 map pmail-mode-map)
1100 (tool-bar-local-item-from-menu 'pmail-expunge "delete"
1101 map pmail-mode-map)
1102 map)))
1103
1104
1105
1106 ;; Pmail mode is suitable only for specially formatted data.
1107 (put 'pmail-mode 'mode-class 'special)
1108
1109 (defun pmail-mode-kill-summary ()
1110 (if pmail-summary-buffer (kill-buffer pmail-summary-buffer)))
1111
1112 ;;;###autoload
1113 (defun pmail-mode ()
1114 "Pmail Mode is used by \\<pmail-mode-map>\\[pmail] for editing Pmail files.
1115 All normal editing commands are turned off.
1116 Instead, these commands are available:
1117
1118 \\[pmail-beginning-of-message] Move point to front of this message.
1119 \\[pmail-end-of-message] Move point to bottom of this message.
1120 \\[scroll-up] Scroll to next screen of this message.
1121 \\[scroll-down] Scroll to previous screen of this message.
1122 \\[pmail-next-undeleted-message] Move to Next non-deleted message.
1123 \\[pmail-previous-undeleted-message] Move to Previous non-deleted message.
1124 \\[pmail-next-message] Move to Next message whether deleted or not.
1125 \\[pmail-previous-message] Move to Previous message whether deleted or not.
1126 \\[pmail-first-message] Move to the first message in Pmail file.
1127 \\[pmail-last-message] Move to the last message in Pmail file.
1128 \\[pmail-show-message] Jump to message specified by numeric position in file.
1129 \\[pmail-search] Search for string and show message it is found in.
1130 \\[pmail-delete-forward] Delete this message, move to next nondeleted.
1131 \\[pmail-delete-backward] Delete this message, move to previous nondeleted.
1132 \\[pmail-undelete-previous-message] Undelete message. Tries current message, then earlier messages
1133 till a deleted message is found.
1134 \\[pmail-edit-current-message] Edit the current message. \\[pmail-cease-edit] to return to Pmail.
1135 \\[pmail-expunge] Expunge deleted messages.
1136 \\[pmail-expunge-and-save] Expunge and save the file.
1137 \\[pmail-quit] Quit Pmail: expunge, save, then switch to another buffer.
1138 \\[save-buffer] Save without expunging.
1139 \\[pmail-get-new-mail] Move new mail from system spool directory into this file.
1140 \\[pmail-mail] Mail a message (same as \\[mail-other-window]).
1141 \\[pmail-continue] Continue composing outgoing message started before.
1142 \\[pmail-reply] Reply to this message. Like \\[pmail-mail] but initializes some fields.
1143 \\[pmail-retry-failure] Send this message again. Used on a mailer failure message.
1144 \\[pmail-forward] Forward this message to another user.
1145 \\[pmail-output-to-pmail-file] Output this message to an Pmail file (append it).
1146 \\[pmail-output] Output this message to a Unix-format mail file (append it).
1147 \\[pmail-output-body-to-file] Save message body to a file. Default filename comes from Subject line.
1148 \\[pmail-input] Input Pmail file. Run Pmail on that file.
1149 \\[pmail-add-label] Add label to message. It will be displayed in the mode line.
1150 \\[pmail-kill-label] Kill label. Remove a label from current message.
1151 \\[pmail-next-labeled-message] Move to Next message with specified label
1152 (label defaults to last one specified).
1153 Standard labels: filed, unseen, answered, forwarded, deleted.
1154 Any other label is present only if you add it with \\[pmail-add-label].
1155 \\[pmail-previous-labeled-message] Move to Previous message with specified label
1156 \\[pmail-summary] Show headers buffer, with a one line summary of each message.
1157 \\[pmail-summary-by-labels] Summarize only messages with particular label(s).
1158 \\[pmail-summary-by-recipients] Summarize only messages with particular recipient(s).
1159 \\[pmail-summary-by-regexp] Summarize only messages with particular regexp(s).
1160 \\[pmail-summary-by-topic] Summarize only messages with subject line regexp(s).
1161 \\[pmail-toggle-header] Toggle display of complete header."
1162 (interactive)
1163 (let ((finding-pmail-file (not (eq major-mode 'pmail-mode))))
1164 (pmail-mode-2)
1165 (when (and finding-pmail-file
1166 (null coding-system-for-read)
1167 default-enable-multibyte-characters)
1168 (let ((pmail-enable-multibyte t))
1169 (pmail-require-mime-maybe)
1170 (goto-char (point-max))
1171 (set-buffer-multibyte t)))
1172 (pmail-show-message pmail-total-messages)
1173 (when finding-pmail-file
1174 (when pmail-display-summary
1175 (pmail-summary))
1176 (pmail-construct-io-menu))
1177 (run-mode-hooks 'pmail-mode-hook)))
1178
1179 (defun pmail-mode-2 ()
1180 (kill-all-local-variables)
1181 (pmail-mode-1)
1182 (pmail-perm-variables)
1183 (pmail-variables))
1184
1185 (defun pmail-mode-1 ()
1186 (setq major-mode 'pmail-mode)
1187 (setq mode-name "PMAIL")
1188 (setq buffer-read-only t)
1189 ;; No need to auto save PMAIL files in normal circumstances
1190 ;; because they contain no info except attribute changes
1191 ;; and deletion of messages.
1192 ;; The one exception is when messages are copied into an Pmail mode buffer.
1193 ;; pmail-output-to-pmail-file enables auto save when you do that.
1194 (setq buffer-auto-save-file-name nil)
1195 (setq mode-line-modified "--")
1196 (use-local-map pmail-mode-map)
1197 (set-syntax-table text-mode-syntax-table)
1198 (setq local-abbrev-table text-mode-abbrev-table))
1199
1200 ;; Set up the permanent locals associated with an Pmail file.
1201 (defun pmail-perm-variables ()
1202 (make-local-variable 'pmail-last-label)
1203 (make-local-variable 'pmail-last-regexp)
1204 (make-local-variable 'pmail-buffer)
1205 (setq pmail-buffer (current-buffer))
1206 (make-local-variable 'pmail-view-buffer)
1207 (setq pmail-view-buffer pmail-buffer)
1208 (make-local-variable 'pmail-summary-buffer)
1209 (make-local-variable 'pmail-current-message)
1210 (make-local-variable 'pmail-total-messages)
1211 (make-local-variable 'pmail-overlay-list)
1212 (setq pmail-overlay-list nil)
1213 (make-local-variable 'pmail-desc-vector)
1214 (make-local-variable 'pmail-inbox-list)
1215 (setq pmail-inbox-list (pmail-get-file-inbox-list))
1216 ;; Provide default set of inboxes for primary mail file ~/PMAIL.
1217 (and (null pmail-inbox-list)
1218 (or (equal buffer-file-name (expand-file-name pmail-file-name))
1219 (equal buffer-file-truename
1220 (abbreviate-file-name (file-truename pmail-file-name))))
1221 (setq pmail-inbox-list
1222 (or pmail-primary-inbox-list
1223 (list (or (getenv "MAIL")
1224 (concat rmail-spool-directory
1225 (user-login-name)))))))
1226 (make-local-variable 'pmail-keywords)
1227 (set (make-local-variable 'tool-bar-map) pmail-tool-bar-map)
1228 ;; this gets generated as needed
1229 (setq pmail-keywords nil))
1230
1231 ;; Set up the non-permanent locals associated with Pmail mode.
1232 (defun pmail-variables ()
1233 ;; Don't let a local variables list in a message cause confusion.
1234 (make-local-variable 'local-enable-local-variables)
1235 (setq local-enable-local-variables nil)
1236 (make-local-variable 'revert-buffer-function)
1237 (setq revert-buffer-function 'pmail-revert)
1238 (make-local-variable 'font-lock-defaults)
1239 (setq font-lock-defaults
1240 '(pmail-font-lock-keywords
1241 t t nil nil
1242 (font-lock-maximum-size . nil)
1243 (font-lock-fontify-buffer-function . pmail-fontify-buffer-function)
1244 (font-lock-unfontify-buffer-function . pmail-unfontify-buffer-function)
1245 (font-lock-inhibit-thing-lock . (lazy-lock-mode fast-lock-mode))))
1246 (make-local-variable 'require-final-newline)
1247 (setq require-final-newline nil)
1248 (make-local-variable 'version-control)
1249 (setq version-control 'never)
1250 (make-local-variable 'kill-buffer-hook)
1251 (add-hook 'kill-buffer-hook 'pmail-mode-kill-summary)
1252 (make-local-variable 'file-precious-flag)
1253 (setq file-precious-flag t)
1254 (make-local-variable 'desktop-save-buffer)
1255 (setq desktop-save-buffer t))
1256
1257 ;; Handle M-x revert-buffer done in an pmail-mode buffer.
1258 (defun pmail-revert (arg noconfirm)
1259 (with-current-buffer pmail-buffer
1260 (let* ((revert-buffer-function (default-value 'revert-buffer-function))
1261 (pmail-enable-multibyte enable-multibyte-characters))
1262 ;; Call our caller again, but this time it does the default thing.
1263 (when (revert-buffer arg noconfirm)
1264 ;; If the user said "yes", and we changed something, reparse the
1265 ;; messages.
1266 (with-current-buffer pmail-buffer
1267 (pmail-mode-2)
1268 (pmail-convert-file)
1269 ;; We have read the file as raw-text, so the buffer is set to
1270 ;; unibyte. Make it multibyte if necessary.
1271 (when (and pmail-enable-multibyte
1272 (not enable-multibyte-characters))
1273 (set-buffer-multibyte t))
1274 (pmail-initialize-messages)
1275 (pmail-show-message pmail-total-messages)
1276 (run-hooks 'pmail-mode-hook))))))
1277
1278 (defun pmail-get-file-inbox-list ()
1279 "Return a list of inbox files for this buffer."
1280 (let* ((filename (expand-file-name (buffer-file-name)))
1281 (inboxes (cdr (or (assoc filename pmail-inbox-alist)
1282 (assoc (abbreviate-file-name filename)
1283 pmail-inbox-alist))))
1284 (list nil))
1285 (dolist (i inboxes)
1286 (when (file-name-absolute-p i)
1287 (push (expand-file-name i) list)))
1288 (nreverse list)))
1289
1290 ;;; mbox: ready
1291 (defun pmail-expunge-and-save ()
1292 "Expunge and save PMAIL file."
1293 (interactive)
1294 (pmail-expunge)
1295 (save-buffer)
1296 (pmail-display-summary-maybe))
1297
1298 ;;; mbox: ready
1299 (defun pmail-display-summary-maybe ()
1300 "If a summary buffer exists then make sure it is updated and displayed."
1301 (if (pmail-summary-exists)
1302 (let ((current-message pmail-current-message))
1303 (pmail-select-summary
1304 (pmail-summary-goto-msg current-message)
1305 (pmail-summary-pmail-update)
1306 (set-buffer-modified-p nil)))))
1307
1308 ;;; mbox: ready
1309 (defun pmail-quit ()
1310 "Quit out of PMAIL.
1311 Hook `pmail-quit-hook' is run after expunging."
1312 (interactive)
1313 (pmail-expunge-and-save)
1314 (when (boundp 'pmail-quit-hook)
1315 (run-hooks 'pmail-quit-hook))
1316 ;; Don't switch to the summary buffer even if it was recently visible.
1317 (when pmail-summary-buffer
1318 (replace-buffer-in-windows pmail-summary-buffer)
1319 (bury-buffer pmail-summary-buffer))
1320 (if pmail-enable-mime
1321 (let ((obuf pmail-buffer)
1322 (ovbuf pmail-view-buffer))
1323 (set-buffer pmail-view-buffer)
1324 (quit-window)
1325 (replace-buffer-in-windows ovbuf)
1326 (replace-buffer-in-windows obuf)
1327 (bury-buffer obuf))
1328 (let ((obuf (current-buffer)))
1329 (quit-window)
1330 (replace-buffer-in-windows obuf))))
1331
1332 ;;; mbox: ready
1333 (defun pmail-bury ()
1334 "Bury current Pmail buffer and its summary buffer."
1335 (interactive)
1336 ;; This let var was called pmail-buffer, but that interfered
1337 ;; with the buffer-local var used in summary buffers.
1338 (let ((buffer-to-bury (current-buffer)))
1339 (if (pmail-summary-exists)
1340 (let (window)
1341 (while (setq window (get-buffer-window pmail-summary-buffer))
1342 (quit-window nil window))
1343 (bury-buffer pmail-summary-buffer)))
1344 (quit-window)))
1345
1346 ;;;??? Fails to add descriptor for new message.
1347 ;;; mbox: ready
1348 (defun pmail-duplicate-message ()
1349 "Create a duplicated copy of the current message.
1350 The duplicate copy goes into the Pmail file just after the
1351 original copy."
1352 (interactive)
1353 (widen)
1354 (let ((buffer-read-only nil)
1355 (number pmail-current-message)
1356 (string (buffer-substring (pmail-desc-get-start pmail-current-message)
1357 (pmail-desc-get-end pmail-current-message))))
1358 (goto-char (pmail-desc-get-end pmail-current-message))
1359 (insert string)
1360 (pmail-show-message number)
1361 (message "Message duplicated")))
1362
1363 ;;;###autoload
1364 (defun pmail-input (filename)
1365 "Run Pmail on file FILENAME."
1366 (interactive "FRun pmail on PMAIL file: ")
1367 (pmail filename))
1368
1369 ;; This used to scan subdirectories recursively, but someone pointed out
1370 ;; that if the user wants that, person can put all the files in one dir.
1371 ;; And the recursive scan was slow. So I took it out. rms, Sep 1996.
1372 (defun pmail-find-all-files (start)
1373 "Return list of file in dir START that match `pmail-secondary-file-regexp'."
1374 (if (file-accessible-directory-p start)
1375 ;; Don't sort here.
1376 (let* ((case-fold-search t)
1377 (files (directory-files start t pmail-secondary-file-regexp)))
1378 ;; Sort here instead of in directory-files
1379 ;; because this list is usually much shorter.
1380 (sort files 'string<))))
1381
1382 (defun pmail-list-to-menu (menu-name l action &optional full-name)
1383 (let ((menu (make-sparse-keymap menu-name)))
1384 (mapc
1385 (function (lambda (item)
1386 (let (command)
1387 (if (consp item)
1388 (progn
1389 (setq command
1390 (pmail-list-to-menu (car item) (cdr item)
1391 action
1392 (if full-name
1393 (concat full-name "/"
1394 (car item))
1395 (car item))))
1396 (setq name (car item)))
1397 (progn
1398 (setq name item)
1399 (setq command
1400 (list 'lambda () '(interactive)
1401 (list action
1402 (expand-file-name
1403 (if full-name
1404 (concat full-name "/" item)
1405 item)
1406 pmail-secondary-file-directory))))))
1407 (define-key menu (vector (intern name))
1408 (cons name command)))))
1409 (reverse l))
1410 menu))
1411
1412 ;; This command is always "disabled" when it appears in a menu.
1413 (put 'pmail-disable-menu 'menu-enable ''nil)
1414
1415 (defun pmail-construct-io-menu ()
1416 (let ((files (pmail-find-all-files pmail-secondary-file-directory)))
1417 (if files
1418 (progn
1419 (define-key pmail-mode-map [menu-bar classify input-menu]
1420 (cons "Input Pmail File"
1421 (pmail-list-to-menu "Input Pmail File"
1422 files
1423 'pmail-input)))
1424 (define-key pmail-mode-map [menu-bar classify output-menu]
1425 (cons "Output Pmail File"
1426 (pmail-list-to-menu "Output Pmail File"
1427 files
1428 'pmail-output))))
1429
1430 (define-key pmail-mode-map [menu-bar classify input-menu]
1431 '("Input Pmail File" . pmail-disable-menu))
1432 (define-key pmail-mode-map [menu-bar classify output-menu]
1433 '("Output Pmail File" . pmail-disable-menu)))))
1434
1435
1436 ;;;; *** Pmail input ***
1437
1438 (declare-function pmail-spam-filter "pmail-spam-filter" (msg))
1439 (declare-function pmail-summary-goto-msg "pmailsum" (&optional n nowarn skip-pmail))
1440 (declare-function pmail-summary-mark-undeleted "pmailsum" (n))
1441 (declare-function pmail-summary-mark-deleted "pmailsum" (&optional n undel))
1442 (declare-function rfc822-addresses "rfc822" (header-text))
1443 (declare-function mail-abbrev-make-syntax-table "mailabbrev.el" ())
1444 (declare-function mail-sendmail-delimit-header "sendmail" ())
1445 (declare-function mail-header-end "sendmail" ())
1446
1447 (defun pmail-get-inbox-files ()
1448 "Return all files from `pmail-inbox-list' without name conflicts.
1449 A conflict happens when two inbox file names have the same name
1450 according to `file-name-nondirectory'."
1451 (let (files last-names)
1452 (catch 'conflict
1453 (dolist (file pmail-inbox-list)
1454 (if (member (file-name-nondirectory file) last-names)
1455 (throw 'conflict t)
1456 (push file files))
1457 (push (file-name-nondirectory file) last-names)))
1458 (nreverse files)))
1459
1460 (defun pmail-delete-inbox-files (files)
1461 "Delete all files given in FILES.
1462 If delete fails, truncate them to zero length."
1463 (dolist (file files)
1464 (condition-case nil
1465 ;; First, try deleting.
1466 (condition-case nil
1467 (delete-file file)
1468 ;; If we can't delete it, truncate it.
1469 (file-error (write-region (point) (point) file)))
1470 (file-error nil))))
1471
1472 (defun pmail-get-new-mail (&optional file-name)
1473 "Move any new mail from this mail file's inbox files.
1474 The inbox files for the primary mail file are determined using
1475 various means when setting up the buffer. The list of inbox
1476 files are stored in `pmail-inbox-list'.
1477
1478 The most important variable that determines the value of this
1479 list is `pmail-inbox-alist' which lists the inbox files for any
1480 mail files you might be using.
1481
1482 If the above yields no inbox files, and if this is the primary
1483 mail file as determined by `pmail-file-name', the inbox lists
1484 otherwise defaults to `pmail-primary-inbox-list' if set, or the
1485 environment variable MAIL if set, or the user's mail file in
1486 `rmail-spool-directory'.
1487
1488 This is why, by default, no mail file has inbox files, except for
1489 the primary mail file ~/PMAIL, which gets its new mail from the
1490 mail spool.
1491
1492 You can also specify the file to get new mail from interactively.
1493 A prefix argument will read a file name and use that file as the
1494 inbox. Noninteractively, you can pass the inbox file name as an
1495 argument.
1496
1497 If the variable `pmail-preserve-inbox' is non-nil, new mail will
1498 always be left in inbox files rather than deleted.
1499
1500 This function runs `pmail-get-new-mail-hook' before saving the
1501 updated file. It returns t if it got any new messages."
1502 (interactive
1503 (list (when current-prefix-arg
1504 (read-file-name "Get new mail from file: "))))
1505 (run-hooks 'pmail-before-get-new-mail-hook)
1506 ;; If the disk file has been changed from under us, revert to it
1507 ;; before we get new mail.
1508 (unless (verify-visited-file-modtime (current-buffer))
1509 (find-file (buffer-file-name)))
1510 (with-current-buffer pmail-buffer
1511 (widen)
1512 ;; Get rid of all undo records for this buffer.
1513 (unless (eq buffer-undo-list t)
1514 (setq buffer-undo-list nil))
1515 (let ((pmail-enable-multibyte (default-value 'enable-multibyte-characters))
1516 ;; If buffer has not changed yet, and has not been saved yet,
1517 ;; don't replace the old backup file now.
1518 (make-backup-files (and make-backup-files (buffer-modified-p)))
1519 current-message found)
1520 (condition-case nil
1521 (let ((buffer-read-only nil)
1522 (buffer-undo-list t)
1523 (delete-files nil)
1524 (new-messages 0)
1525 (rsf-number-of-spam 0))
1526 (save-excursion
1527 (save-restriction
1528 (goto-char (point-max))
1529 (narrow-to-region (point) (point))
1530 ;; Read in the contents of the inbox files, renaming
1531 ;; them as necessary, and adding to the list of files to
1532 ;; delete eventually.
1533 (if file-name
1534 (pmail-insert-inbox-text (list file-name) nil)
1535 (setq delete-files (pmail-insert-inbox-text
1536 (pmail-get-inbox-files) t)))
1537 ;; Process newly found messages and save them into the
1538 ;; PMAIL file.
1539 (unless (equal (point-min) (point-max))
1540 (setq new-messages (pmail-convert-mbox-format))
1541 (unless (zerop new-messages)
1542 (pmail-process-new-messages)
1543 (setq pmail-current-message (1+ pmail-total-messages)
1544 pmail-total-messages (pmail-desc-get-count)))
1545 (save-buffer))
1546 ;; Delete the old files, now that the PMAIL file is
1547 ;; saved.
1548 (when delete-files
1549 (pmail-delete-inbox-files delete-files))))
1550
1551 (if (zerop new-messages)
1552 (when (or file-name pmail-inbox-list)
1553 (message "(No new mail has arrived)"))
1554
1555 ;; Process the new messages for spam using the integrated
1556 ;; spam filter. The spam filter can mark messages for
1557 ;; deletion and can output a message.
1558 ;; XXX pmail-spam-filter hasn't been tested at all with
1559 ;; the mbox branch. --enberg
1560 (setq current-message (pmail-first-unseen-message))
1561 (when pmail-use-spam-filter
1562 (while (<= current-message pmail-total-messages)
1563 (pmail-spam-filter current-message)
1564 (setq current-message (1+ current-message))))
1565 ;; Make the first unseen message the current message and
1566 ;; update the summary buffer, if one exists.
1567 (setq current-message (pmail-first-unseen-message))
1568 (if (pmail-summary-exists)
1569 (with-current-buffer pmail-summary-buffer
1570 (pmail-update-summary)
1571 (pmail-summary-goto-msg current-message))
1572 (pmail-show-message current-message))
1573 ;; Run the after get new mail hook.
1574 (run-hooks 'pmail-after-get-new-mail-hook)
1575 (message "%d new message%s read"
1576 new-messages (if (= 1 new-messages) "" "s"))
1577 (setq found t))
1578 found)
1579 ;; Don't leave the buffer screwed up if we get a disk-full error.
1580 (file-error (or found (pmail-show-message)))))))
1581
1582 (defun pmail-parse-url (file)
1583 "Parse the supplied URL. Return (list MAILBOX-NAME REMOTE PASSWORD GOT-PASSWORD)
1584 WHERE MAILBOX-NAME is the name of the mailbox suitable as argument to the
1585 actual version of `movemail', REMOTE is non-nil if MAILBOX-NAME refers to
1586 a remote mailbox, PASSWORD is the password if it should be
1587 supplied as a separate argument to `movemail' or nil otherwise, GOT-PASSWORD
1588 is non-nil if the user has supplied the password interactively.
1589 "
1590 (cond
1591 ((string-match "^\\([^:]+\\)://\\(\\([^:@]+\\)\\(:\\([^@]+\\)\\)?@\\)?.*" file)
1592 (let (got-password supplied-password
1593 (proto (match-string 1 file))
1594 (user (match-string 3 file))
1595 (pass (match-string 5 file))
1596 (host (substring file (or (match-end 2)
1597 (+ 3 (match-end 1))))))
1598
1599 (if (not pass)
1600 (when pmail-remote-password-required
1601 (setq got-password (not (pmail-have-password)))
1602 (setq supplied-password (pmail-get-remote-password
1603 (string-equal proto "imap")))))
1604
1605 (if (pmail-movemail-variant-p 'emacs)
1606 (if (string-equal proto "pop")
1607 (list (concat "po:" user ":" host)
1608 t
1609 (or pass supplied-password)
1610 got-password)
1611 (error "Emacs movemail does not support %s protocol" proto))
1612 (list file
1613 (or (string-equal proto "pop") (string-equal proto "imap"))
1614 supplied-password
1615 got-password))))
1616
1617 ((string-match "^po:\\([^:]+\\)\\(:\\(.*\\)\\)?" file)
1618 (let (got-password supplied-password
1619 (proto "pop")
1620 (user (match-string 1 file))
1621 (host (match-string 3 file)))
1622
1623 (when pmail-remote-password-required
1624 (setq got-password (not (pmail-have-password)))
1625 (setq supplied-password (pmail-get-remote-password nil)))
1626
1627 (list file "pop" supplied-password got-password)))
1628
1629 (t
1630 (list file nil nil nil))))
1631
1632 (defun pmail-insert-inbox-text (files renamep)
1633 ;; Detect a locked file now, so that we avoid moving mail
1634 ;; out of the real inbox file. (That could scare people.)
1635 (or (memq (file-locked-p buffer-file-name) '(nil t))
1636 (error "PMAIL file %s is locked"
1637 (file-name-nondirectory buffer-file-name)))
1638 (let (file tofile delete-files movemail popmail got-password password)
1639 (while files
1640 ;; Handle remote mailbox names specially; don't expand as filenames
1641 ;; in case the userid contains a directory separator.
1642 (setq file (car files))
1643 (let ((url-data (pmail-parse-url file)))
1644 (setq file (nth 0 url-data))
1645 (setq popmail (nth 1 url-data))
1646 (setq password (nth 2 url-data))
1647 (setq got-password (nth 3 url-data)))
1648
1649 (if popmail
1650 (setq renamep t)
1651 (setq file (file-truename
1652 (substitute-in-file-name (expand-file-name file)))))
1653 (setq tofile (expand-file-name
1654 ;; Generate name to move to from inbox name,
1655 ;; in case of multiple inboxes that need moving.
1656 (concat ".newmail-"
1657 (file-name-nondirectory
1658 (if (memq system-type '(windows-nt cygwin))
1659 ;; cannot have "po:" in file name
1660 (substring file 3)
1661 file)))
1662 ;; Use the directory of this pmail file
1663 ;; because it's a nuisance to use the homedir
1664 ;; if that is on a full disk and this pmail
1665 ;; file isn't.
1666 (file-name-directory
1667 (expand-file-name buffer-file-name))))
1668 ;; Always use movemail to rename the file,
1669 ;; since there can be mailboxes in various directories.
1670 (if (not popmail)
1671 (progn
1672 ;; On some systems, /usr/spool/mail/foo is a directory
1673 ;; and the actual inbox is /usr/spool/mail/foo/foo.
1674 (if (file-directory-p file)
1675 (setq file (expand-file-name (user-login-name)
1676 file)))))
1677 (cond (popmail
1678 (message "Getting mail from the remote server ..."))
1679 ((and (file-exists-p tofile)
1680 (/= 0 (nth 7 (file-attributes tofile))))
1681 (message "Getting mail from %s..." tofile))
1682 ((and (file-exists-p file)
1683 (/= 0 (nth 7 (file-attributes file))))
1684 (message "Getting mail from %s..." file)))
1685 ;; Set TOFILE if have not already done so, and
1686 ;; rename or copy the file FILE to TOFILE if and as appropriate.
1687 (cond ((not renamep)
1688 (setq tofile file))
1689 ((or (file-exists-p tofile) (and (not popmail)
1690 (not (file-exists-p file))))
1691 nil)
1692 (t
1693 (with-temp-buffer
1694 (let ((errors (current-buffer)))
1695 (buffer-disable-undo errors)
1696 (let ((args
1697 (append
1698 (list pmail-movemail-program nil errors nil)
1699 (if pmail-preserve-inbox
1700 (list "-p")
1701 nil)
1702 (if (pmail-movemail-variant-p 'mailutils)
1703 (append (list "--emacs") pmail-movemail-flags)
1704 pmail-movemail-flags)
1705 (list file tofile)
1706 (if password (list password) nil))))
1707 (apply 'call-process args))
1708 (if (not (buffer-modified-p errors))
1709 ;; No output => movemail won
1710 nil
1711 (set-buffer errors)
1712 (subst-char-in-region (point-min) (point-max)
1713 ?\n ?\ )
1714 (goto-char (point-max))
1715 (skip-chars-backward " \t")
1716 (delete-region (point) (point-max))
1717 (goto-char (point-min))
1718 (if (looking-at "movemail: ")
1719 (delete-region (point-min) (match-end 0)))
1720 (beep t)
1721 ;; If we just read the password, most likely it is
1722 ;; wrong. Otherwise, see if there is a specific
1723 ;; reason to think that the problem is a wrong passwd.
1724 (if (or got-password
1725 (re-search-forward pmail-remote-password-error
1726 nil t))
1727 (pmail-set-remote-password nil))
1728
1729 ;; If using Mailutils, remove initial error code
1730 ;; abbreviation
1731 (when (pmail-movemail-variant-p 'mailutils)
1732 (goto-char (point-min))
1733 (when (looking-at "[A-Z][A-Z0-9_]*:")
1734 (delete-region (point-min) (match-end 0))))
1735
1736 (message "movemail: %s"
1737 (buffer-substring (point-min)
1738 (point-max)))
1739
1740 (sit-for 3)
1741 nil)))))
1742
1743 ;; At this point, TOFILE contains the name to read:
1744 ;; Either the alternate name (if we renamed)
1745 ;; or the actual inbox (if not renaming).
1746 (if (file-exists-p tofile)
1747 (let ((coding-system-for-read 'no-conversion)
1748 size)
1749 (goto-char (point-max))
1750 (setq size (nth 1 (insert-file-contents tofile)))
1751 (goto-char (point-max))
1752 (or (= (preceding-char) ?\n)
1753 (zerop size)
1754 (insert ?\n))
1755 (if (not (and pmail-preserve-inbox (string= file tofile)))
1756 (setq delete-files (cons tofile delete-files)))))
1757 (message "")
1758 (setq files (cdr files)))
1759 delete-files))
1760
1761 ;;;; *** Pmail message decoding ***
1762
1763 ;; these two are unused, and possibly harmul.
1764
1765 ;; (defun pmail-decode-region (from to coding)
1766 ;; "Decode the region specified by FROM and TO by CODING.
1767 ;; If CODING is nil or an invalid coding system, decode by `undecided'."
1768 ;; (unless (and coding (coding-system-p coding))
1769 ;; (setq coding 'undecided))
1770 ;; ;; Use -dos decoding, to remove ^M characters left from base64 or
1771 ;; ;; rogue qp-encoded text.
1772 ;; (decode-coding-region from to
1773 ;; (coding-system-change-eol-conversion
1774 ;; coding 'dos))
1775 ;; ;; Don't reveal the fact we used -dos decoding, as users generally
1776 ;; ;; will not expect the PMAIL buffer to use DOS EOL format.
1777 ;; (setq buffer-file-coding-system
1778 ;; (setq last-coding-system-used
1779 ;; (coding-system-change-eol-conversion
1780 ;; coding 'unix))))
1781
1782 ;; (defun pmail-decode-by-content-type (from to)
1783 ;; "Decode message between FROM and TO according to Content-Type."
1784 ;; (when (and (not pmail-enable-mime) pmail-enable-multibyte)
1785 ;; (let ((coding-system-used nil)
1786 ;; (case-fold-search t))
1787 ;; (save-restriction
1788 ;; (narrow-to-region from to)
1789 ;; (when (and (not pmail-enable-mime) pmail-enable-multibyte)
1790 ;; (let ((coding
1791 ;; (when (save-excursion
1792 ;; (goto-char (pmail-header-get-limit))
1793 ;; (re-search-backward
1794 ;; pmail-mime-charset-pattern
1795 ;; (point-min) t))
1796 ;; (intern (downcase (match-string 1))))))
1797 ;; (setq coding-system-used (pmail-decode-region
1798 ;; (point-min) (point-max)
1799 ;; coding)))))
1800 ;; (setq last-coding-system-used coding-system-used))))
1801
1802 ;;;; *** Pmail Message Formatting and Header Manipulation ***
1803
1804 (defun pmail-clear-headers (&optional ignored-headers)
1805 "Delete all header fields that Pmail should not show.
1806 If the optional argument IGNORED-HEADERS is non-nil,
1807 delete all header fields whose names match that regexp.
1808 Otherwise, if `pmail-displayed-headers' is non-nil,
1809 delete all header fields *except* those whose names match that regexp.
1810 Otherwise, delete all header fields whose names match `pmail-ignored-headers'
1811 unless they also match `pmail-nonignored-headers'."
1812 (when (search-forward "\n\n" nil t)
1813 (forward-char -1)
1814 (let ((case-fold-search t)
1815 (buffer-read-only nil))
1816 (if (and pmail-displayed-headers (null ignored-headers))
1817 (save-restriction
1818 (narrow-to-region (point-min) (point))
1819 (let (lim next)
1820 (goto-char (point-min))
1821 (while (and (not (eobp))
1822 (save-excursion
1823 (if (re-search-forward "\n[^ \t]" nil t)
1824 (setq lim (match-beginning 0)
1825 next (1+ lim))
1826 (setq lim nil next (point-max)))))
1827 (if (save-excursion
1828 (re-search-forward pmail-displayed-headers lim t))
1829 (goto-char next)
1830 (delete-region (point) next))))
1831 (goto-char (point-min)))
1832 (or ignored-headers (setq ignored-headers pmail-ignored-headers))
1833 (save-restriction
1834 (narrow-to-region (point-min) (point))
1835 (goto-char (point-min))
1836 (while (and ignored-headers
1837 (re-search-forward ignored-headers nil t))
1838 (beginning-of-line)
1839 (if (and pmail-nonignored-headers
1840 (looking-at pmail-nonignored-headers))
1841 (forward-line 1)
1842 (delete-region (point)
1843 (save-excursion
1844 (if (re-search-forward "\n[^ \t]" nil t)
1845 (1- (point))
1846 (point-max)))))))))))
1847
1848 (defun pmail-msg-is-pruned (&optional msg)
1849 "Determine if the headers for the current message are being
1850 displayed. If MSG is non-nil it will be used as the message number
1851 instead of the current message."
1852 (pmail-desc-get-header-display-state (or msg pmail-current-message)))
1853
1854 (defun pmail-toggle-header (&optional arg)
1855 "Show original message header if pruned header currently shown, or vice versa.
1856 With argument ARG, show the message header pruned if ARG is greater than zero;
1857 otherwise, show it in full."
1858 (interactive "P")
1859 (pmail-header-toggle-visibility arg))
1860
1861 ;; Lifted from repos-count-screen-lines.
1862 (defun pmail-count-screen-lines (start end)
1863 "Return number of screen lines between START and END."
1864 (save-excursion
1865 (save-restriction
1866 (narrow-to-region start end)
1867 (goto-char (point-min))
1868 (vertical-motion (- (point-max) (point-min))))))
1869
1870 ;;;; *** Pmail Attributes and Keywords ***
1871
1872 ;; Make a string describing the current message's attributes by
1873 ;; keywords and set it up as the name of a minor mode so it will
1874 ;; appear in the mode line.
1875 (defun pmail-display-labels ()
1876 (let (keyword-list result)
1877 ;; Update the keyword list for the current message.
1878 (if (> pmail-current-message 0)
1879 (setq keyword-list (pmail-desc-get-keywords pmail-current-message)))
1880 ;; Generate the result string.
1881 (setq result (mapconcat 'identity keyword-list " "))
1882 ;; Update the mode line to display the keywords, the current
1883 ;; message index and the total number of messages.
1884 (setq mode-line-process
1885 (format " %d/%d%s"
1886 pmail-current-message pmail-total-messages
1887 (if keyword-list (concat " " result) "")))
1888 ;; If pmail-enable-mime is non-nil, we may have to update
1889 ;; `mode-line-process' of pmail-view-buffer too.
1890 (if (and pmail-enable-mime
1891 (not (eq (current-buffer) pmail-view-buffer))
1892 (buffer-live-p pmail-view-buffer))
1893 (let ((mlp mode-line-process))
1894 (with-current-buffer pmail-view-buffer
1895 (setq mode-line-process mlp))))))
1896
1897 (defun pmail-set-attribute (attr state &optional msgnum)
1898 "Turn a attribute ATTR of a message on or off according to STATE.
1899 ATTR is a string, MSGNUM is the optional message number. By
1900 default, the current message is changed."
1901 (save-excursion
1902 (save-restriction
1903 (let ((attr-index (pmail-desc-get-attr-index attr)))
1904 (set-buffer pmail-buffer)
1905 (or msgnum (setq msgnum pmail-current-message))
1906 (pmail-desc-set-attribute attr-index state msgnum)
1907 ;; Deal with the summary buffer.
1908 (when pmail-summary-buffer
1909 (pmail-summary-update msgnum))))))
1910
1911 (defun pmail-message-labels-p (n labels)
1912 "Return t if message number N has keywords matching LABELS.
1913 LABELS is a regular expression."
1914 (catch 'found
1915 (dolist (keyword (pmail-desc-get-keywords n))
1916 (when (string-match labels keyword)
1917 (throw 'found t)))))
1918
1919
1920 ;;;; *** Pmail Message Selection And Support ***
1921
1922 (defun pmail-msgbeg (n)
1923 (pmail-desc-get-start n))
1924 (make-obsolete 'pmail-msgbeg 'pmail-desc-get-start "22.0")
1925
1926 (defun pmail-msgend (n)
1927 (pmail-desc-get-end n))
1928 (make-obsolete 'pmail-msgend 'pmail-desc-get-end "22.0")
1929
1930 (defun pmail-widen-to-current-msgbeg (function)
1931 "Call FUNCTION with point at start of internal data of current message.
1932 Assumes that bounds were previously narrowed to display the message in Pmail.
1933 The bounds are widened enough to move point where desired, then narrowed
1934 again afterward.
1935
1936 FUNCTION may not change the visible text of the message, but it may
1937 change the invisible header text."
1938 (save-excursion
1939 (unwind-protect
1940 (progn
1941 (narrow-to-region (pmail-desc-get-start pmail-current-message)
1942 (point-max))
1943 (goto-char (point-min))
1944 (funcall function))
1945 ;; Note: we don't use save-restriction because that does not work right
1946 ;; if changes are made outside the saved restriction
1947 ;; before that restriction is restored.
1948 (narrow-to-region (pmail-desc-get-start pmail-current-message)
1949 (pmail-desc-get-end pmail-current-message)))))
1950
1951 (defun pmail-process-new-messages (&optional nomsg)
1952 "Process the new messages in the buffer.
1953 The buffer has been narrowed to expose only the new messages.
1954 For each new message append an entry to the message vector and,
1955 if necessary, add a header that will capture the salient BABYL
1956 information. Return the number of new messages. If NOMSG is
1957 non-nil then do not show any progress messages."
1958 (let ((inhibit-read-only t)
1959 (case-fold-search nil)
1960 (new-message-counter 0)
1961 (start (point-max))
1962 end date keywords message-descriptor-list)
1963 (or nomsg (message "Processing new messages..."))
1964 ;; Process each message in turn starting from the back and
1965 ;; proceeding to the front of the region. This is especially a
1966 ;; good approach since the buffer will likely have new headers
1967 ;; added.
1968 (save-excursion
1969 (goto-char start)
1970 (while (re-search-backward pmail-unix-mail-delimiter nil t)
1971 ;; Cache the message date to facilitate generating a message
1972 ;; summary later. The format is '(DAY-OF-WEEK DAY-NUMBER MON
1973 ;; YEAR TIME)
1974 (setq date
1975 (list (buffer-substring (match-beginning 2) (match-end 2))
1976 (buffer-substring (match-beginning 4) (match-end 4))
1977 (buffer-substring (match-beginning 3) (match-end 3))
1978 (buffer-substring (match-beginning 7) (match-end 7))
1979 (buffer-substring (match-beginning 5) (match-end 5))))
1980 ;;Set start and end to bracket this message.
1981 (setq end start)
1982 (setq start (point))
1983 (save-excursion
1984 (save-restriction
1985 (narrow-to-region start end)
1986 (goto-char start)
1987 ;; Bump the new message counter.
1988 (setq new-message-counter (1+ new-message-counter))
1989
1990 ;; Set up keywords, if any. The keywords are provided via a
1991 ;; comma separated list and returned as a list of strings.
1992 (setq keywords (pmail-header-get-keywords))
1993 (when keywords
1994 ;; Keywords do exist. Register them with the keyword
1995 ;; management library.
1996 (pmail-register-keywords keywords))
1997 ;; Insure that we have From and Date headers.
1998 ;;(pmail-decode-from-line)
1999 ;; Perform User defined filtering.
2000 (save-excursion
2001 (if pmail-message-filter (funcall pmail-message-filter)))
2002 ;; Accumulate the message attributes along with the message
2003 ;; markers and the message date list.
2004 (setq message-descriptor-list
2005 (vconcat (list (list (point-min-marker)
2006 (pmail-header-get-header
2007 pmail-header-attribute-header)
2008 keywords
2009 date
2010 (count-lines start end)
2011 (cadr (mail-extract-address-components; does not like nil
2012 (or (pmail-header-get-header "from") "")))
2013 (or (pmail-header-get-header "subject")
2014 "none")))
2015 message-descriptor-list)))))
2016 ;; Add the new message data lists to the Pmail message descriptor
2017 ;; vector.
2018 (pmail-desc-add-descriptors message-descriptor-list)
2019 ;; Unless requested otherwise, show the number of new messages.
2020 ;; Return the number of new messages.
2021 (or nomsg (message "Processing new messages...done (%d)"
2022 new-message-counter))
2023 new-message-counter)))
2024
2025 (defun pmail-convert-mbox-format ()
2026 (let ((case-fold-search nil)
2027 (message-count 0)
2028 (start (point-max))
2029 end)
2030 (save-excursion
2031 (goto-char start)
2032 (while (re-search-backward pmail-unix-mail-delimiter nil t)
2033 (setq end start)
2034 (setq start (point))
2035 (save-excursion
2036 (save-restriction
2037 (narrow-to-region start end)
2038 (goto-char (point-min))
2039 ;; Bump the new message counter.
2040 (setq message-count (1+ message-count))
2041 ;; Detect messages that have been added with DOS line endings
2042 ;; and convert the line endings for such messages.
2043 (when (save-excursion (end-of-line) (= (preceding-char) ?\r))
2044 (let ((buffer-read-only nil)
2045 (buffer-undo t)
2046 (end-marker (copy-marker end)))
2047 (message
2048 "Processing new messages...(converting line endings)")
2049 (save-excursion
2050 (goto-char (point-max))
2051 (while (search-backward "\r\n" (point-min) t)
2052 (delete-char 1)))
2053 (setq end (marker-position end-marker))
2054 (set-marker end-marker nil)))
2055 ;; Make sure we have an Pmail BABYL attribute header field.
2056 ;; All we can assume is that the Pmail BABYL header field is
2057 ;; in the header section. It's placement can be modified by
2058 ;; another mailer.
2059 (let ((attributes (pmail-header-get-header
2060 pmail-header-attribute-header)))
2061 (unless attributes
2062 ;; No suitable header exists. Append the default BABYL
2063 ;; data header for a new message.
2064 (pmail-header-add-header pmail-header-attribute-header
2065 pmail-desc-default-attrs))))))
2066 message-count)))
2067
2068 (defun pmail-beginning-of-message ()
2069 "Show current message starting from the beginning."
2070 (interactive)
2071 (let ((pmail-show-message-hook
2072 (list (function (lambda ()
2073 (goto-char (point-min)))))))
2074 (pmail-show-message pmail-current-message)))
2075
2076 (defun pmail-end-of-message ()
2077 "Show bottom of current message."
2078 (interactive)
2079 (let ((pmail-show-message-hook
2080 (list (function (lambda ()
2081 (goto-char (point-max))
2082 (recenter (1- (window-height))))))))
2083 (pmail-show-message pmail-current-message)))
2084
2085 (defun pmail-unknown-mail-followup-to ()
2086 "Handle a \"Mail-Followup-To\" header field with an unknown mailing list.
2087 Ask the user whether to add that list name to `mail-mailing-lists'."
2088 (save-restriction
2089 (let ((mail-followup-to (pmail-header-get-header "mail-followup-to" nil t)))
2090 (when mail-followup-to
2091 (let ((addresses
2092 (split-string
2093 (mail-strip-quoted-names mail-followup-to)
2094 ",[[:space:]]+" t)))
2095 (dolist (addr addresses)
2096 (when (and (not (member addr mail-mailing-lists))
2097 (and pmail-user-mail-address-regexp
2098 (not (string-match pmail-user-mail-address-regexp
2099 addr)))
2100 (y-or-n-p
2101 (format "Add `%s' to `mail-mailing-lists'? "
2102 addr)))
2103 (customize-save-variable 'mail-mailing-lists
2104 (cons addr mail-mailing-lists)))))))))
2105
2106 (defun pmail-show-message (&optional n no-summary)
2107 "Show message number N (prefix argument), counting from start of file.
2108 If NO-SUMMARY is non-nil, then do not update the summary buffer."
2109 (interactive "p")
2110 (unless (eq major-mode 'pmail-mode)
2111 (switch-to-buffer pmail-buffer))
2112 (if (zerop pmail-total-messages)
2113 (progn
2114 (message "No messages to show. Add something better soon.")
2115 (force-mode-line-update))
2116 (let (blurb)
2117 ;; Set n to the first sane message based on the sign of n:
2118 ;; positive but greater than the total number of messages -> n;
2119 ;; negative -> 1.
2120 (if (not n)
2121 (setq n pmail-current-message)
2122 (cond ((<= n 0)
2123 (setq n 1
2124 pmail-current-message 1
2125 blurb "No previous message"))
2126 ((> n pmail-total-messages)
2127 (setq n pmail-total-messages
2128 pmail-current-message pmail-total-messages
2129 blurb "No following message"))
2130 (t
2131 (setq pmail-current-message n))))
2132 (let ((beg (pmail-desc-get-start n))
2133 (end (pmail-desc-get-end n)))
2134 (pmail-header-show-headers)
2135 (widen)
2136 (narrow-to-region beg end)
2137 (goto-char (point-min))
2138 ;; Clear the "unseen" attribute when we show a message, unless
2139 ;; it is already cleared.
2140 (when (pmail-desc-attr-p pmail-desc-unseen-index n)
2141 (pmail-desc-set-attribute pmail-desc-unseen-index nil n))
2142 (pmail-display-labels)
2143 ;; Deal with MIME
2144 (if (eq pmail-enable-mime t)
2145 (funcall pmail-show-mime-function)
2146 (setq pmail-view-buffer pmail-buffer))
2147 (when mail-mailing-lists
2148 (pmail-unknown-mail-followup-to))
2149 (pmail-header-hide-headers)
2150 (when transient-mark-mode (deactivate-mark))
2151 ;; Make sure that point in the Pmail window is at the beginning
2152 ;; of the buffer.
2153 (goto-char (point-min))
2154 (set-window-point (get-buffer-window pmail-buffer) (point))
2155 ;; Run any User code.
2156 (run-hooks 'pmail-show-message-hook)
2157 ;; If there is a summary buffer, try to move to this message in
2158 ;; that buffer. But don't complain if this message is not
2159 ;; mentioned in the summary. Don't do this at all if we were
2160 ;; called on behalf of cursor motion in the summary buffer.
2161 (when (and (pmail-summary-exists) (not no-summary))
2162 (let ((curr-msg pmail-current-message))
2163 ;; Set the summary current message, disabling the Pmail
2164 ;; buffer update.
2165 (with-current-buffer pmail-summary-buffer
2166 (pmail-summary-goto-msg curr-msg nil t))))
2167 (with-current-buffer pmail-buffer
2168 (pmail-auto-file))
2169 ;; Post back any status messages.
2170 (when blurb
2171 (message blurb))))))
2172
2173 (defun pmail-redecode-body (coding)
2174 "Decode the body of the current message using coding system CODING.
2175 This is useful with mail messages that have malformed or missing
2176 charset= headers.
2177
2178 This function assumes that the current message is already decoded
2179 and displayed in the PMAIL buffer, but the coding system used to
2180 decode it was incorrect. It then encodes the message back to its
2181 original form, and decodes it again, using the coding system CODING.
2182
2183 Note that if Emacs erroneously auto-detected one of the iso-2022
2184 encodings in the message, this function might fail because the escape
2185 sequences that switch between character sets and also single-shift and
2186 locking-shift codes are impossible to recover. This function is meant
2187 to be used to fix messages encoded with 8-bit encodings, such as
2188 iso-8859, koi8-r, etc."
2189 (interactive "zCoding system for re-decoding this message: ")
2190 (unless pmail-enable-mime
2191 (with-current-buffer pmail-buffer
2192 (save-excursion
2193 (let ((start (pmail-desc-get-start pmail-current-message))
2194 (end (pmail-desc-get-end pmail-current-message))
2195 header)
2196 (narrow-to-region start end)
2197 (setq header (pmail-header-get-header "X-Coding-System"))
2198 (if header
2199 (let ((old-coding (intern header))
2200 (buffer-read-only nil))
2201 (check-coding-system old-coding)
2202 ;; Make sure the new coding system uses the same EOL
2203 ;; conversion, to prevent ^M characters from popping
2204 ;; up all over the place.
2205 (setq coding
2206 (coding-system-change-eol-conversion
2207 coding
2208 (coding-system-eol-type old-coding)))
2209 ;; Do the actual recoding.
2210 (encode-coding-region start end old-coding)
2211 (decode-coding-region start end coding)
2212 ;; Rewrite the x-coding-system header according to
2213 ;; what we did.
2214 (setq last-coding-system-used coding)
2215 (pmail-header-add-header
2216 "X-Coding-System"
2217 (symbol-name last-coding-system-used))
2218 (pmail-show-message pmail-current-message))
2219 (error "No X-Coding-System header found")))))))
2220
2221 ;; FIXME: Double-check this
2222 (defun pmail-auto-file ()
2223 "Automatically move a message into a sub-folder based on criteria.
2224 Called when a new message is displayed."
2225 (if (or (member "filed" (pmail-desc-get-keywords pmail-current-message))
2226 (not (string= (buffer-file-name)
2227 (expand-file-name pmail-file-name))))
2228 ;; Do nothing if it's already been filed.
2229 nil
2230 ;; Find out some basics (common fields)
2231 (let ((from (mail-fetch-field "from"))
2232 (subj (mail-fetch-field "subject"))
2233 (to (concat (mail-fetch-field "to") "," (mail-fetch-field "cc")))
2234 (directives pmail-automatic-folder-directives)
2235 (directive-loop nil)
2236 (folder nil))
2237 (while directives
2238 (setq folder (car (car directives))
2239 directive-loop (cdr (car directives)))
2240 (while (and (car directive-loop)
2241 (let ((f (cond
2242 ((string= (car directive-loop) "from") from)
2243 ((string= (car directive-loop) "to") to)
2244 ((string= (car directive-loop) "subject") subj)
2245 (t (mail-fetch-field (car directive-loop))))))
2246 (and f (string-match (car (cdr directive-loop)) f))))
2247 (setq directive-loop (cdr (cdr directive-loop))))
2248 ;; If there are no directives left, then it was a complete match.
2249 (if (null directive-loop)
2250 (if (null folder)
2251 (pmail-delete-forward)
2252 (if (string= "/dev/null" folder)
2253 (pmail-delete-message)
2254 (pmail-output folder 1 t)
2255 (setq directives nil))))
2256 (setq directives (cdr directives))))))
2257
2258 (defun pmail-next-message (n)
2259 "Show following message whether deleted or not.
2260 With prefix arg N, moves forward N messages, or backward if N is
2261 negative."
2262 (interactive "p")
2263 (with-current-buffer pmail-buffer
2264 (pmail-show-message (+ pmail-current-message n))))
2265
2266 (defun pmail-previous-message (n)
2267 "Show previous message whether deleted or not.
2268 With prefix arg N, moves backward N messages, or forward if N is
2269 negative."
2270 (interactive "p")
2271 (pmail-next-message (- n)))
2272
2273 (defun pmail-next-undeleted-message (n)
2274 "Show following non-deleted message.
2275 With prefix arg N, moves forward N non-deleted messages, or
2276 backward if N is negative.
2277
2278 Returns t if a new message is being shown, nil otherwise."
2279 (interactive "p")
2280 (let ((lastwin pmail-current-message)
2281 (original pmail-current-message)
2282 (current pmail-current-message))
2283 ;; Move forwards, remember the last undeleted message seen.
2284 (while (and (> n 0) (< current pmail-total-messages))
2285 (setq current (1+ current))
2286 (unless (pmail-desc-deleted-p current)
2287 (setq lastwin current
2288 n (1- n))))
2289 ;; Same thing for moving backwards
2290 (while (and (< n 0) (> current 1))
2291 (setq current (1- current))
2292 (unless (pmail-desc-deleted-p current)
2293 (setq lastwin current
2294 n (1+ n))))
2295 ;; Show the message (even if no movement took place so that the
2296 ;; delete attribute is marked) and determine the result value.
2297 (pmail-show-message lastwin)
2298 (if (/= lastwin original)
2299 t
2300 (if (< n 0)
2301 (message "No previous nondeleted message"))
2302 (if (> n 0)
2303 (message "No following nondeleted message"))
2304 nil)))
2305
2306 (defun pmail-previous-undeleted-message (n)
2307 "Show previous non-deleted message.
2308 With prefix argument N, moves backward N non-deleted messages,
2309 or forward if N is negative."
2310 (interactive "p")
2311 (pmail-next-undeleted-message (- n)))
2312
2313 (defun pmail-first-message ()
2314 "Show first message in file."
2315 (interactive)
2316 (pmail-show-message 1))
2317
2318 (defun pmail-last-message ()
2319 "Show last message in file."
2320 (interactive)
2321 (pmail-show-message pmail-total-messages))
2322
2323 (defun pmail-narrow-to-header (msg)
2324 "Narrow the buffer to the headers of message number MSG."
2325 (save-excursion
2326 (let ((start (pmail-desc-get-start msg))
2327 (end (pmail-desc-get-end msg)))
2328 (widen)
2329 (goto-char start)
2330 (unless (search-forward "\n\n" end t)
2331 (error "Invalid message format"))
2332 (narrow-to-region start (point)))))
2333
2334 (defun pmail-message-recipients-p (msg recipients &optional primary-only)
2335 (save-restriction
2336 (or (string-match recipients (or (mail-fetch-field "To") ""))
2337 (string-match recipients (or (mail-fetch-field "From") ""))
2338 (if (not primary-only)
2339 (string-match recipients (or (mail-fetch-field "Cc") ""))))))
2340
2341 (defun pmail-message-regexp-p (msg regexp)
2342 "Return t, if for message number MSG, regexp REGEXP matches in the header."
2343 (save-excursion
2344 (save-restriction
2345 (pmail-narrow-to-header msg)
2346 (re-search-forward regexp nil t))))
2347
2348 (defun pmail-search-message (msg regexp)
2349 "Return non-nil, if for message number MSG, regexp REGEXP matches."
2350 (goto-char (pmail-desc-get-start msg))
2351 (if pmail-enable-mime
2352 (funcall pmail-search-mime-message-function msg regexp)
2353 (re-search-forward regexp (pmail-desc-get-end msg) t)))
2354
2355 (defvar pmail-search-last-regexp nil)
2356 (defun pmail-search (regexp &optional n)
2357 "Show message containing next match for REGEXP (but not the current msg).
2358 Prefix argument gives repeat count; negative argument means search
2359 backwards (through earlier messages).
2360 Interactively, empty argument means use same regexp used last time."
2361 (interactive
2362 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
2363 (prompt
2364 (concat (if reversep "Reverse " "") "Pmail search (regexp"))
2365 regexp)
2366 (setq prompt
2367 (concat prompt
2368 (if pmail-search-last-regexp
2369 (concat ", default "
2370 pmail-search-last-regexp "): ")
2371 "): ")))
2372 (setq regexp (read-string prompt))
2373 (cond ((not (equal regexp ""))
2374 (setq pmail-search-last-regexp regexp))
2375 ((not pmail-search-last-regexp)
2376 (error "No previous Pmail search string")))
2377 (list pmail-search-last-regexp
2378 (prefix-numeric-value current-prefix-arg))))
2379 (or n (setq n 1))
2380 (message "%sPmail search for %s..."
2381 (if (< n 0) "Reverse " "")
2382 regexp)
2383 (set-buffer pmail-buffer)
2384 (let ((omin (point-min))
2385 (omax (point-max))
2386 (opoint (point))
2387 (reversep (< n 0))
2388 (msg pmail-current-message)
2389 win)
2390 (unwind-protect
2391 (progn
2392 (widen)
2393 (while (/= n 0)
2394 ;; Check messages one by one, advancing message number up or down
2395 ;; but searching forward through each message.
2396 (if reversep
2397 (while (and (null win) (> msg 1))
2398 (setq msg (1- msg)
2399 win (pmail-search-message msg regexp)))
2400 (while (and (null win) (< msg pmail-total-messages))
2401 (setq msg (1+ msg)
2402 win (pmail-search-message msg regexp))))
2403 (setq n (+ n (if reversep 1 -1)))))
2404 (if win
2405 (progn
2406 (pmail-show-message msg)
2407 ;; Search forward (if this is a normal search) or backward
2408 ;; (if this is a reverse search) through this message to
2409 ;; position point. This search may fail because REGEXP
2410 ;; was found in the hidden portion of this message. In
2411 ;; that case, move point to the beginning of visible
2412 ;; portion.
2413 (if reversep
2414 (progn
2415 (goto-char (point-max))
2416 (re-search-backward regexp nil 'move))
2417 (goto-char (point-min))
2418 (re-search-forward regexp nil t))
2419 (message "%sPmail search for %s...done"
2420 (if reversep "Reverse " "")
2421 regexp))
2422 (goto-char opoint)
2423 (narrow-to-region omin omax)
2424 (ding)
2425 (message "Search failed: %s" regexp)))))
2426
2427 (defun pmail-search-backwards (regexp &optional n)
2428 "Show message containing previous match for REGEXP.
2429 Prefix argument gives repeat count; negative argument means search
2430 forward (through later messages).
2431 Interactively, empty argument means use same regexp used last time."
2432 (interactive
2433 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
2434 (prompt
2435 (concat (if reversep "Reverse " "") "Pmail search (regexp"))
2436 regexp)
2437 (setq prompt
2438 (concat prompt
2439 (if pmail-search-last-regexp
2440 (concat ", default "
2441 pmail-search-last-regexp "): ")
2442 "): ")))
2443 (setq regexp (read-string prompt))
2444 (cond ((not (equal regexp ""))
2445 (setq pmail-search-last-regexp regexp))
2446 ((not pmail-search-last-regexp)
2447 (error "No previous Pmail search string")))
2448 (list pmail-search-last-regexp
2449 (prefix-numeric-value current-prefix-arg))))
2450 (pmail-search regexp (- (or n 1))))
2451
2452 ;; Show the first message which has the `unseen' attribute.
2453 (defun pmail-first-unseen-message ()
2454 "Return the first message which has not been seen. If all messages
2455 have been seen, then return the last message."
2456 (let ((current 1)
2457 found)
2458 (while (and (not found) (<= current pmail-total-messages))
2459 (if (pmail-desc-attr-p pmail-desc-unseen-index current)
2460 (setq found current))
2461 (setq current (1+ current)))
2462 (or found pmail-total-messages)))
2463
2464 (defun pmail-current-subject ()
2465 "Return the current subject.
2466 The subject is stripped of leading and trailing whitespace, and
2467 of typical reply prefixes such as Re:."
2468 (let ((subject (or (mail-fetch-field "Subject") "")))
2469 (if (string-match "\\`[ \t]+" subject)
2470 (setq subject (substring subject (match-end 0))))
2471 (if (string-match pmail-reply-regexp subject)
2472 (setq subject (substring subject (match-end 0))))
2473 (if (string-match "[ \t]+\\'" subject)
2474 (setq subject (substring subject 0 (match-beginning 0))))
2475 subject))
2476
2477 (defun pmail-current-subject-regexp ()
2478 "Return a regular expression matching the current subject.
2479 The regular expression matches the subject header line of
2480 messages about the same subject. The subject itself is stripped
2481 of leading and trailing whitespace, of typical reply prefixes
2482 such as Re: and whitespace within the subject is replaced by a
2483 regular expression matching whitespace in general in order to
2484 take into account that subject header lines may include newlines
2485 and more whitespace. The returned regular expressions contains
2486 `pmail-reply-regexp' and ends with a newline."
2487 (let ((subject (pmail-current-subject)))
2488 ;; If Subject is long, mailers will break it into several lines at
2489 ;; arbitrary places, so replace whitespace with a regexp that will
2490 ;; match any sequence of spaces, TABs, and newlines.
2491 (setq subject (regexp-quote subject))
2492 (setq subject
2493 (replace-regexp-in-string "[ \t\n]+" "[ \t\n]+" subject t t))
2494 ;; Some mailers insert extra spaces after "Subject:", so allow any
2495 ;; amount of them.
2496 (concat "^Subject:[ \t]+"
2497 (if (string= "\\`" (substring pmail-reply-regexp 0 2))
2498 (substring pmail-reply-regexp 2)
2499 pmail-reply-regexp)
2500 subject "[ \t]*\n")))
2501
2502 (defun pmail-next-same-subject (n)
2503 "Go to the next mail message having the same subject header.
2504 With prefix argument N, do this N times.
2505 If N is negative, go backwards instead."
2506 (interactive "p")
2507 (let ((search-regexp (pmail-current-subject-regexp))
2508 (forward (> n 0))
2509 (i pmail-current-message)
2510 (case-fold-search t)
2511 found)
2512 (save-excursion
2513 (save-restriction
2514 (widen)
2515 (if forward
2516 (while (and (/= n 0) (< i pmail-total-messages))
2517 (let (done)
2518 (while (and (not done)
2519 (< i pmail-total-messages))
2520 (setq i (+ i 1))
2521 (pmail-narrow-to-header i)
2522 (goto-char (point-min))
2523 (setq done (re-search-forward search-regexp (point-max) t)))
2524 (if done (setq found i)))
2525 (setq n (1- n)))
2526 (while (and (/= n 0) (> i 1))
2527 (let (done)
2528 (while (and (not done) (> i 1))
2529 (setq i (- i 1))
2530 (pmail-narrow-to-header i)
2531 (goto-char (point-min))
2532 (setq done (re-search-forward search-regexp (point-max) t)))
2533 (if done (setq found i)))
2534 (setq n (1+ n))))))
2535 (if found
2536 (pmail-show-message found)
2537 (error "No %s message with same subject"
2538 (if forward "following" "previous")))))
2539
2540 (defun pmail-previous-same-subject (n)
2541 "Go to the previous mail message having the same subject header.
2542 With prefix argument N, do this N times.
2543 If N is negative, go forwards instead."
2544 (interactive "p")
2545 (pmail-next-same-subject (- n)))
2546
2547 ;;;; *** Pmail Message Deletion Commands ***
2548
2549 (defun pmail-delete-message ()
2550 "Delete this message and stay on it."
2551 (interactive)
2552 (pmail-desc-set-attribute pmail-desc-deleted-index t pmail-current-message)
2553 (run-hooks 'pmail-delete-message-hook)
2554 (pmail-show-message pmail-current-message))
2555
2556 (defun pmail-undelete-previous-message ()
2557 "Back up to deleted message, select it, and undelete it."
2558 (interactive)
2559 (set-buffer pmail-buffer)
2560 (let ((msg pmail-current-message))
2561 (while (and (> msg 0)
2562 (not (pmail-desc-attr-p pmail-desc-deleted-index msg)))
2563 (setq msg (1- msg)))
2564 (if (= msg 0)
2565 (error "No previous deleted message")
2566 (pmail-desc-set-attribute pmail-desc-deleted-index nil msg)
2567 (pmail-show-message msg)
2568 (if (pmail-summary-exists)
2569 (save-excursion
2570 (set-buffer pmail-summary-buffer)
2571 (pmail-summary-mark-undeleted msg)))
2572 (pmail-maybe-display-summary))))
2573
2574 ;;; mbox: ready
2575 (defun pmail-delete-forward (&optional backward)
2576 "Delete this message and move to next nondeleted one.
2577 Deleted messages stay in the file until the \\[pmail-expunge] command is given.
2578 With prefix argument, delete and move backward.
2579
2580 Returns t if a new message is displayed after the delete, or nil otherwise."
2581 (interactive "P")
2582 (pmail-desc-set-attribute pmail-desc-deleted-index t pmail-current-message)
2583 (run-hooks 'pmail-delete-message-hook)
2584 (let ((del-msg pmail-current-message))
2585 (if (pmail-summary-exists)
2586 (pmail-select-summary
2587 (pmail-summary-mark-deleted del-msg)))
2588 (prog1 (pmail-next-undeleted-message (if backward -1 1))
2589 (pmail-maybe-display-summary))))
2590
2591 ;;; mbox: ready
2592 (defun pmail-delete-backward ()
2593 "Delete this message and move to previous nondeleted one.
2594 Deleted messages stay in the file until the \\[pmail-expunge] command is given."
2595 (interactive)
2596 (pmail-delete-forward t))
2597
2598 (defun pmail-expunge-confirmed ()
2599 "Return t if deleted message should be expunged. If necessary, ask the user.
2600 See also user-option `pmail-confirm-expunge'."
2601 (set-buffer pmail-buffer)
2602 (let ((some-deleted))
2603 (dotimes (i pmail-total-messages)
2604 (if (pmail-desc-deleted-p (1+ i))
2605 (setq some-deleted t)))
2606 (or (not some-deleted)
2607 (null pmail-confirm-expunge)
2608 (funcall pmail-confirm-expunge
2609 "Erase deleted messages from Pmail file? "))))
2610
2611 (defun pmail-only-expunge (&optional dont-show)
2612 "Actually erase all deleted messages in the file."
2613 (interactive)
2614 (message "Expunging deleted messages...")
2615 ;; Discard all undo records for this buffer.
2616 (or (eq buffer-undo-list t) (setq buffer-undo-list nil))
2617 ;; Remove the messages from the buffer and from the Pmail message
2618 ;; descriptor vector.
2619 (pmail-desc-prune-deleted-messages 'pmail-expunge-callback)
2620 ;; Update the Pmail message counter, deal with the summary buffer,
2621 ;; show the current message and update the User status.
2622 (setq pmail-total-messages (pmail-desc-get-count))
2623 (pmail-show-message pmail-current-message t)
2624 (let* ((omax (- (buffer-size) (point-max)))
2625 (omin (- (buffer-size) (point-min)))
2626 (opoint (if (and (> pmail-current-message 0)
2627 (pmail-message-deleted-p pmail-current-message))
2628 0
2629 (if pmail-enable-mime
2630 (with-current-buffer pmail-view-buffer
2631 (- (point)(point-min)))
2632 (- (point) (point-min))))))
2633 (when pmail-summary-buffer
2634 (with-current-buffer pmail-summary-buffer
2635 (pmail-update-summary)))
2636 (message "Expunging deleted messages...done")
2637 (if (not dont-show)
2638 (pmail-show-message
2639 (if (zerop pmail-current-message) 1 nil)))
2640 (if pmail-enable-mime
2641 (goto-char (+ (point-min) opoint))
2642 (goto-char (+ (point) opoint)))))
2643
2644 ;;; mbox: ready
2645 (defun pmail-expunge-callback (n)
2646 "Called after message N has been pruned to update the current Pmail
2647 message counter."
2648 (if (< n pmail-current-message)
2649 (setq pmail-current-message (1- pmail-current-message))))
2650
2651 ;;; mbox: ready
2652 (defun pmail-expunge ()
2653 "Erase deleted messages from Pmail file and summary buffer."
2654 (interactive)
2655 (when (pmail-expunge-confirmed)
2656 (pmail-only-expunge)))
2657
2658 ;;;; *** Pmail Mailing Commands ***
2659
2660 ;;; mbox: In progress. I'm still not happy with the initial citation
2661 ;;; stuff. -pmr
2662 (defun pmail-start-mail (&optional noerase to subject in-reply-to cc
2663 replybuffer sendactions same-window others)
2664 (let (yank-action)
2665 (if replybuffer
2666 (setq yank-action (list 'insert-buffer replybuffer)))
2667 (setq others (cons (cons "cc" cc) others))
2668 (setq others (cons (cons "in-reply-to" in-reply-to) others))
2669 (if same-window
2670 (compose-mail to subject others
2671 noerase nil
2672 yank-action sendactions)
2673 (if pmail-mail-new-frame
2674 (prog1
2675 (compose-mail to subject others
2676 noerase 'switch-to-buffer-other-frame
2677 yank-action sendactions)
2678 ;; This is not a standard frame parameter;
2679 ;; nothing except sendmail.el looks at it.
2680 (modify-frame-parameters (selected-frame)
2681 '((mail-dedicated-frame . t))))
2682 (compose-mail to subject others
2683 noerase 'switch-to-buffer-other-window
2684 yank-action sendactions)))))
2685
2686 (defun pmail-mail ()
2687 "Send mail in another window.
2688 While composing the message, use \\[mail-yank-original] to yank the
2689 original message into it."
2690 (interactive)
2691 (pmail-start-mail nil nil nil nil nil pmail-view-buffer))
2692
2693 (defun pmail-continue ()
2694 "Continue composing outgoing message previously being composed."
2695 (interactive)
2696 (pmail-start-mail t))
2697
2698 (defun pmail-reply (just-sender)
2699 "Reply to the current message.
2700 Normally include CC: to all other recipients of original message;
2701 prefix argument means ignore them. While composing the reply,
2702 use \\[mail-yank-original] to yank the original message into it."
2703 (interactive "P")
2704 (if (= pmail-total-messages 0)
2705 (error "No messages in this file"))
2706 (save-excursion
2707 (save-restriction
2708 (let* ((msgnum pmail-current-message)
2709 (from (pmail-header-get-header "from"))
2710 (reply-to (or (pmail-header-get-header "reply-to" nil t) from))
2711 (cc (unless just-sender
2712 (pmail-header-get-header "cc" nil t)))
2713 (subject (pmail-header-get-header "subject"))
2714 (date (pmail-header-get-header "date"))
2715 (to (or (pmail-header-get-header "to" nil t) ""))
2716 (message-id (pmail-header-get-header "message-id"))
2717 (references (pmail-header-get-header "references" nil nil t))
2718 (resent-to (pmail-header-get-header "resent-reply-to" nil t))
2719 (resent-cc (unless just-sender
2720 (pmail-header-get-header "resent-cc" nil t)))
2721 (resent-reply-to (or (pmail-header-get-header "resent-to" nil t) "")))
2722 ;; Merge the resent-to and resent-cc into the to and cc.
2723 (if (and resent-to (not (equal resent-to "")))
2724 (if (not (equal to ""))
2725 (setq to (concat to ", " resent-to))
2726 (setq to resent-to)))
2727 (if (and resent-cc (not (equal resent-cc "")))
2728 (if (not (equal cc ""))
2729 (setq cc (concat cc ", " resent-cc))
2730 (setq cc resent-cc)))
2731 ;; Add `Re: ' to subject if not there already.
2732 (and (stringp subject)
2733 (setq subject
2734 (concat pmail-reply-prefix
2735 (if (let ((case-fold-search t))
2736 (string-match pmail-reply-regexp subject))
2737 (substring subject (match-end 0))
2738 subject))))
2739 ;; Now setup the mail reply buffer.
2740 (pmail-start-mail
2741 nil
2742 ;; Using mail-strip-quoted-names is undesirable with newer
2743 ;; mailers since they can handle the names unstripped. I
2744 ;; don't know whether there are other mailers that still need
2745 ;; the names to be stripped.
2746 ;;; (mail-strip-quoted-names reply-to)
2747 ;; Remove unwanted names from reply-to, since Mail-Followup-To
2748 ;; header causes all the names in it to wind up in reply-to, not
2749 ;; in cc. But if what's left is an empty list, use the original.
2750 (let* ((reply-to-list (pmail-dont-reply-to reply-to)))
2751 (if (string= reply-to-list "") reply-to reply-to-list))
2752 subject
2753 (pmail-make-in-reply-to-field from date message-id)
2754 (if just-sender
2755 nil
2756 ;; mail-strip-quoted-names is NOT necessary for
2757 ;; pmail-dont-reply-to to do its job.
2758 (let* ((cc-list (pmail-dont-reply-to
2759 (mail-strip-quoted-names
2760 (if (null cc) to (concat to ", " cc))))))
2761 (if (string= cc-list "") nil cc-list)))
2762 pmail-view-buffer
2763 (list (list 'pmail-reply-callback pmail-buffer "answered" t msgnum))
2764 nil
2765 (list (cons "References" (concat (mapconcat 'identity references " ")
2766 " " message-id))))))))
2767
2768 (defun pmail-reply-callback (buffer attr state n)
2769 "Mail reply callback function.
2770 Sets ATTR (a string) if STATE is
2771 non-nil, otherwise clears it. N is the message number.
2772 BUFFER, possibly narrowed, contains an mbox mail message."
2773 (save-excursion
2774 (set-buffer buffer)
2775 (pmail-set-attribute attr state n)))
2776
2777 (defun pmail-mark-message (msgnum-list attr-index)
2778 "Set attribute ATTRIBUTE-INDEX in the message of the car of MSGNUM-LIST.
2779 This is used in the send-actions for
2780 message buffers. MSGNUM-LIST is a list of the form (MSGNUM)."
2781 (save-excursion
2782 (let ((n (car msgnum-list)))
2783 (set-buffer pmail-buffer)
2784 (pmail-narrow-to-message n)
2785 (pmail-desc-set-attribute attr-index t n))))
2786
2787 (defun pmail-narrow-to-message (n)
2788 "Narrow the current (pmail) buffer to bracket message N."
2789 (widen)
2790 (narrow-to-region (pmail-desc-get-start n) (pmail-desc-get-end n)))
2791
2792 (defun pmail-make-in-reply-to-field (from date message-id)
2793 (cond ((not from)
2794 (if message-id
2795 message-id
2796 nil))
2797 (mail-use-rfc822
2798 (require 'rfc822)
2799 (let ((tem (car (rfc822-addresses from))))
2800 (if message-id
2801 (if (or (not tem)
2802 (string-match
2803 (regexp-quote (if (string-match "@[^@]*\\'" tem)
2804 (substring tem 0
2805 (match-beginning 0))
2806 tem))
2807 message-id))
2808 ;; missing From, or Message-ID is sufficiently informative
2809 message-id
2810 (concat message-id " (" tem ")"))
2811 ;; Copy TEM, discarding text properties.
2812 (setq tem (copy-sequence tem))
2813 (set-text-properties 0 (length tem) nil tem)
2814 (setq tem (copy-sequence tem))
2815 ;; Use prin1 to fake RFC822 quoting
2816 (let ((field (prin1-to-string tem)))
2817 (if date
2818 (concat field "'s message of " date)
2819 field)))))
2820 ((let* ((foo "[^][\000-\037()<>@,;:\\\" ]+")
2821 (bar "[^][\000-\037()<>@,;:\\\"]+"))
2822 ;; These strings both match all non-ASCII characters.
2823 (or (string-match (concat "\\`[ \t]*\\(" bar
2824 "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
2825 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
2826 from)
2827 (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
2828 bar "\\))[ \t]*\\'")
2829 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
2830 from)))
2831 (let ((start (match-beginning 1))
2832 (end (match-end 1)))
2833 ;; Trim whitespace which above regexp match allows
2834 (while (and (< start end)
2835 (memq (aref from start) '(?\t ?\ )))
2836 (setq start (1+ start)))
2837 (while (and (< start end)
2838 (memq (aref from (1- end)) '(?\t ?\ )))
2839 (setq end (1- end)))
2840 (let ((field (substring from start end)))
2841 (if date (setq field (concat "message from " field " on " date)))
2842 (if message-id
2843 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
2844 (concat message-id " (" field ")")
2845 field))))
2846 (t
2847 ;; If we can't kludge it simply, do it correctly
2848 (let ((mail-use-rfc822 t))
2849 (pmail-make-in-reply-to-field from date message-id)))))
2850
2851 ;;; mbox: ready
2852 (defun pmail-forward (resend)
2853 "Forward the current message to another user.
2854 With prefix argument, \"resend\" the message instead of forwarding it;
2855 see the documentation of `pmail-resend'."
2856 (interactive "P")
2857 (if (= pmail-total-messages 0)
2858 (error "No messages in this file"))
2859 (if resend
2860 (call-interactively 'pmail-resend)
2861 (let ((forward-buffer pmail-buffer)
2862 (msgnum pmail-current-message)
2863 (subject (concat "["
2864 (let ((from (or (mail-fetch-field "From")
2865 (mail-fetch-field ">From"))))
2866 (if from
2867 (concat (mail-strip-quoted-names from) ": ")
2868 ""))
2869 (or (mail-fetch-field "Subject") "")
2870 "]")))
2871 (if (pmail-start-mail
2872 nil nil subject nil nil nil
2873 (list (list 'pmail-mark-message
2874 forward-buffer
2875 (with-current-buffer pmail-buffer
2876 (pmail-desc-get-start msgnum))
2877 "forwarded"))
2878 ;; If only one window, use it for the mail buffer.
2879 ;; Otherwise, use another window for the mail buffer
2880 ;; so that the Pmail buffer remains visible
2881 ;; and sending the mail will get back to it.
2882 (and (not pmail-mail-new-frame) (one-window-p t)))
2883 ;; The mail buffer is now current.
2884 (save-excursion
2885 ;; Insert after header separator--before signature if any.
2886 (goto-char (mail-text-start))
2887 (if (or pmail-enable-mime pmail-enable-mime-composing)
2888 (funcall pmail-insert-mime-forwarded-message-function
2889 forward-buffer)
2890 (insert "------- Start of forwarded message -------\n")
2891 ;; Quote lines with `- ' if they start with `-'.
2892 (let ((beg (point)) end)
2893 (setq end (point-marker))
2894 (set-marker-insertion-type end t)
2895 (insert-buffer-substring forward-buffer)
2896 (goto-char beg)
2897 (while (re-search-forward "^-" end t)
2898 (beginning-of-line)
2899 (insert "- ")
2900 (forward-line 1))
2901 (goto-char end)
2902 (skip-chars-backward "\n")
2903 (if (< (point) end)
2904 (forward-char 1))
2905 (delete-region (point) end)
2906 (set-marker end nil))
2907 (insert "------- End of forwarded message -------\n"))
2908 (push-mark))))))
2909
2910 (defun pmail-resend (address &optional from comment mail-alias-file)
2911 "Resend current message to ADDRESSES.
2912 ADDRESSES should be a single address, a string consisting of several
2913 addresses separated by commas, or a list of addresses.
2914
2915 Optional FROM is the address to resend the message from, and
2916 defaults from the value of `user-mail-address'.
2917 Optional COMMENT is a string to insert as a comment in the resent message.
2918 Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
2919 typically for purposes of moderating a list."
2920 (interactive "sResend to: ")
2921 (if (= pmail-total-messages 0)
2922 (error "No messages in this file"))
2923 (require 'sendmail)
2924 (require 'mailalias)
2925 (unless (or (eq pmail-view-buffer (current-buffer))
2926 (eq pmail-buffer (current-buffer)))
2927 (error "Not an Pmail buffer"))
2928 (if (not from) (setq from user-mail-address))
2929 (let ((tembuf (generate-new-buffer " sendmail temp"))
2930 (case-fold-search nil)
2931 (mail-personal-alias-file
2932 (or mail-alias-file mail-personal-alias-file))
2933 (mailbuf pmail-buffer))
2934 (unwind-protect
2935 (with-current-buffer tembuf
2936 ;;>> Copy message into temp buffer
2937 (if pmail-enable-mime
2938 (funcall pmail-insert-mime-resent-message-function mailbuf)
2939 (insert-buffer-substring mailbuf))
2940 (goto-char (point-min))
2941 ;; Delete any Sender field, since that's not specifiable.
2942 ; Only delete Sender fields in the actual header.
2943 (re-search-forward "^$" nil 'move)
2944 ; Using "while" here rather than "if" because some buggy mail
2945 ; software may have inserted multiple Sender fields.
2946 (while (re-search-backward "^Sender:" nil t)
2947 (let (beg)
2948 (setq beg (point))
2949 (forward-line 1)
2950 (while (looking-at "[ \t]")
2951 (forward-line 1))
2952 (delete-region beg (point))))
2953 ; Go back to the beginning of the buffer so the Resent- fields
2954 ; are inserted there.
2955 (goto-char (point-min))
2956 ;;>> Insert resent-from:
2957 (insert "Resent-From: " from "\n")
2958 (insert "Resent-Date: " (mail-rfc822-date) "\n")
2959 ;;>> Insert resent-to: and bcc if need be.
2960 (let ((before (point)))
2961 (if mail-self-blind
2962 (insert "Resent-Bcc: " (user-login-name) "\n"))
2963 (insert "Resent-To: " (if (stringp address)
2964 address
2965 (mapconcat 'identity address ",\n\t"))
2966 "\n")
2967 ;; Expand abbrevs in the recipients.
2968 (save-excursion
2969 (if (featurep 'mailabbrev)
2970 (let ((end (point-marker))
2971 (local-abbrev-table mail-abbrevs)
2972 (old-syntax-table (syntax-table)))
2973 (if (and (not (vectorp mail-abbrevs))
2974 (file-exists-p mail-personal-alias-file))
2975 (build-mail-abbrevs))
2976 (unless mail-abbrev-syntax-table
2977 (mail-abbrev-make-syntax-table))
2978 (set-syntax-table mail-abbrev-syntax-table)
2979 (goto-char before)
2980 (while (and (< (point) end)
2981 (progn (forward-word 1)
2982 (<= (point) end)))
2983 (expand-abbrev))
2984 (set-syntax-table old-syntax-table))
2985 (expand-mail-aliases before (point)))))
2986 ;;>> Set up comment, if any.
2987 (if (and (sequencep comment) (not (zerop (length comment))))
2988 (let ((before (point))
2989 after)
2990 (insert comment)
2991 (or (eolp) (insert "\n"))
2992 (setq after (point))
2993 (goto-char before)
2994 (while (< (point) after)
2995 (insert "Resent-Comment: ")
2996 (forward-line 1))))
2997 ;; Don't expand aliases in the destination fields
2998 ;; of the original message.
2999 (let (mail-aliases)
3000 (funcall send-mail-function)))
3001 (kill-buffer tembuf))
3002 (with-current-buffer pmail-buffer
3003 (pmail-set-attribute "resent" t pmail-current-message))))
3004
3005 (defvar mail-unsent-separator
3006 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
3007 "^ *---+ +Returned message +---+ *$\\|"
3008 "^ *---+ *Returned mail follows *---+ *$\\|"
3009 "^Start of returned message$\\|"
3010 "^---+ Below this line is a copy of the message.$\\|"
3011 "^ *---+ +Original message +---+ *$\\|"
3012 "^ *--+ +begin message +--+ *$\\|"
3013 "^ *---+ +Original message follows +---+ *$\\|"
3014 "^ *---+ +Your message follows +---+ *$\\|"
3015 "^|? *---+ +Message text follows: +---+ *|?$\\|"
3016 "^ *---+ +This is a copy of \\w+ message, including all the headers.*---+ *$")
3017 "A regexp that matches the separator before the text of a failed message.")
3018
3019 (defvar mail-mime-unsent-header "^Content-Type: message/rfc822 *$"
3020 "A regexp that matches the header of a MIME body part with a failed message.")
3021
3022 (defun pmail-retry-failure ()
3023 "Edit a mail message which is based on the contents of the current message.
3024 For a message rejected by the mail system, extract the interesting headers and
3025 the body of the original message.
3026 If the failed message is a MIME multipart message, it is searched for a
3027 body part with a header which matches the variable `mail-mime-unsent-header'.
3028 Otherwise, the variable `mail-unsent-separator' should match the string that
3029 delimits the returned original message.
3030 The variable `pmail-retry-ignored-headers' is a regular expression
3031 specifying headers which should not be copied into the new message."
3032 (interactive)
3033 (if (= pmail-total-messages 0)
3034 (error "No messages in this file"))
3035 (require 'mail-utils)
3036 (let ((pmail-this-buffer (current-buffer))
3037 (msgnum pmail-current-message)
3038 bounce-start bounce-end bounce-indent resending
3039 (content-type
3040 (save-excursion
3041 (save-restriction
3042 (pmail-header-get-header "Content-Type")))))
3043 (save-excursion
3044 (goto-char (point-min))
3045 (let ((case-fold-search t))
3046 (if (and content-type
3047 (string-match
3048 ";[\n\t ]*boundary=\"?\\([-0-9a-z'()+_,./:=? ]+\\)\"?"
3049 content-type))
3050 ;; Handle a MIME multipart bounce message.
3051 (let ((codestring
3052 (concat "\n--"
3053 (substring content-type (match-beginning 1)
3054 (match-end 1)))))
3055 (unless (re-search-forward mail-mime-unsent-header nil t)
3056 (error "Cannot find beginning of header in failed message"))
3057 (unless (search-forward "\n\n" nil t)
3058 (error "Cannot find start of Mime data in failed message"))
3059 (setq bounce-start (point))
3060 (if (search-forward codestring nil t)
3061 (setq bounce-end (match-beginning 0))
3062 (setq bounce-end (point-max))))
3063 ;; Non-MIME bounce.
3064 (or (re-search-forward mail-unsent-separator nil t)
3065 (error "Cannot parse this as a failure message"))
3066 (skip-chars-forward "\n")
3067 ;; Support a style of failure message in which the original
3068 ;; message is indented, and included within lines saying
3069 ;; `Start of returned message' and `End of returned message'.
3070 (if (looking-at " +Received:")
3071 (progn
3072 (setq bounce-start (point))
3073 (skip-chars-forward " ")
3074 (setq bounce-indent (- (current-column)))
3075 (goto-char (point-max))
3076 (re-search-backward "^End of returned message$" nil t)
3077 (setq bounce-end (point)))
3078 ;; One message contained a few random lines before
3079 ;; the old message header. The first line of the
3080 ;; message started with two hyphens. A blank line
3081 ;; followed these random lines. The same line
3082 ;; beginning with two hyphens was possibly marking
3083 ;; the end of the message.
3084 (if (looking-at "^--")
3085 (let ((boundary (buffer-substring-no-properties
3086 (point)
3087 (progn (end-of-line) (point)))))
3088 (search-forward "\n\n")
3089 (skip-chars-forward "\n")
3090 (setq bounce-start (point))
3091 (goto-char (point-max))
3092 (search-backward (concat "\n\n" boundary) bounce-start t)
3093 (setq bounce-end (point)))
3094 (setq bounce-start (point)
3095 bounce-end (point-max)))
3096 (unless (search-forward "\n\n" nil t)
3097 (error "Cannot find end of header in failed message"))))))
3098 ;; We have found the message that bounced, within the current message.
3099 ;; Now start sending new message; default header fields from original.
3100 ;; Turn off the usual actions for initializing the message body
3101 ;; because we want to get only the text from the failure message.
3102 ;;
3103 ;; NOTE: the use of pmail-msgref-vector is a red flag. I'm not
3104 ;; sure (yet) what the right thing to do here is but I strongly
3105 ;; suspect it needs something along the lines of:
3106 ;; ...(pmail-desc-set-attribute pmail-desc-resent-index nil n)...
3107 ;; The test to run to see the breakage and figure out what needs
3108 ;; to be done is to cause a "resend" to happen and verify that it
3109 ;; is either broken or works properly. For now the unbound
3110 ;; variable is being left intact. -pmr 8/12/2008
3111 (let (mail-signature mail-setup-hook)
3112 (if (pmail-start-mail nil nil nil nil nil pmail-this-buffer
3113 (list (list 'pmail-mark-message
3114 pmail-this-buffer
3115 (aref pmail-msgref-vector msgnum)
3116 "retried")))
3117 ;; Insert original text as initial text of new draft message.
3118 ;; Bind inhibit-read-only since the header delimiter
3119 ;; of the previous message was probably read-only.
3120 (let ((inhibit-read-only t)
3121 pmail-displayed-headers
3122 pmail-ignored-headers)
3123 (erase-buffer)
3124 (insert-buffer-substring pmail-this-buffer
3125 bounce-start bounce-end)
3126 (goto-char (point-min))
3127 (if bounce-indent
3128 (indent-rigidly (point-min) (point-max) bounce-indent))
3129 (pmail-clear-headers pmail-retry-ignored-headers)
3130 (pmail-clear-headers "^sender:\\|^return-path:\\|^received:")
3131 (mail-sendmail-delimit-header)
3132 (save-restriction
3133 (narrow-to-region (point-min) (mail-header-end))
3134 (setq resending (mail-fetch-field "resent-to"))
3135 (if mail-self-blind
3136 (if resending
3137 (insert "Resent-Bcc: " (user-login-name) "\n")
3138 (insert "BCC: " (user-login-name) "\n"))))
3139 (goto-char (point-min))
3140 (mail-position-on-field (if resending "Resent-To" "To") t))))))
3141
3142 (defun pmail-summary-exists ()
3143 "Non-nil if in an PMAIL buffer and an associated summary buffer exists.
3144 In fact, the non-nil value returned is the summary buffer itself."
3145 (and pmail-summary-buffer (buffer-name pmail-summary-buffer)
3146 pmail-summary-buffer))
3147
3148 (defun pmail-summary-displayed ()
3149 "t if in PMAIL buffer and an associated summary buffer is displayed."
3150 (and pmail-summary-buffer (get-buffer-window pmail-summary-buffer)))
3151
3152 (defcustom pmail-redisplay-summary nil
3153 "*Non-nil means Pmail should show the summary when it changes.
3154 This has an effect only if a summary buffer exists."
3155 :type 'boolean
3156 :group 'pmail-summary)
3157
3158 (defcustom pmail-summary-window-size nil
3159 "*Non-nil means specify the height for an Pmail summary window."
3160 :type '(choice (const :tag "Disabled" nil) integer)
3161 :group 'pmail-summary)
3162
3163 ;; Put the summary buffer back on the screen, if user wants that.
3164 (defun pmail-maybe-display-summary ()
3165 (let ((selected (selected-window))
3166 window)
3167 ;; If requested, make sure the summary is displayed.
3168 (and pmail-summary-buffer (buffer-name pmail-summary-buffer)
3169 pmail-redisplay-summary
3170 (if (get-buffer-window pmail-summary-buffer 0)
3171 ;; It's already in some frame; show that one.
3172 (let ((frame (window-frame
3173 (get-buffer-window pmail-summary-buffer 0))))
3174 (make-frame-visible frame)
3175 (raise-frame frame))
3176 (display-buffer pmail-summary-buffer)))
3177 ;; If requested, set the height of the summary window.
3178 (and pmail-summary-buffer (buffer-name pmail-summary-buffer)
3179 pmail-summary-window-size
3180 (setq window (get-buffer-window pmail-summary-buffer))
3181 ;; Don't try to change the size if just one window in frame.
3182 (not (eq window (frame-root-window (window-frame window))))
3183 (unwind-protect
3184 (progn
3185 (select-window window)
3186 (enlarge-window (- pmail-summary-window-size (window-height))))
3187 (select-window selected)))))
3188
3189 ;;;; *** Pmail Local Fontification ***
3190
3191 (defun pmail-fontify-buffer-function ()
3192 ;; This function's symbol is bound to font-lock-fontify-buffer-function.
3193 (add-hook 'pmail-show-message-hook 'pmail-fontify-message nil t)
3194 ;; If we're already showing a message, fontify it now.
3195 (if pmail-current-message (pmail-fontify-message))
3196 ;; Prevent Font Lock mode from kicking in.
3197 (setq font-lock-fontified t))
3198
3199 (defun pmail-unfontify-buffer-function ()
3200 ;; This function's symbol is bound to font-lock-fontify-unbuffer-function.
3201 (let ((modified (buffer-modified-p))
3202 (buffer-undo-list t) (inhibit-read-only t)
3203 before-change-functions after-change-functions
3204 buffer-file-name buffer-file-truename)
3205 (save-restriction
3206 (widen)
3207 (remove-hook 'pmail-show-message-hook 'pmail-fontify-message t)
3208 (remove-text-properties (point-min) (point-max) '(pmail-fontified nil))
3209 (font-lock-default-unfontify-buffer)
3210 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))
3211
3212 (defun pmail-fontify-message ()
3213 "Fontify the current message if it is not already fontified."
3214 (when (text-property-any (point-min) (point-max) 'pmail-fontified nil)
3215 (let ((modified (buffer-modified-p))
3216 (buffer-undo-list t) (inhibit-read-only t)
3217 before-change-functions after-change-functions
3218 buffer-file-name buffer-file-truename)
3219 (save-excursion
3220 (save-match-data
3221 (add-text-properties (point-min) (point-max) '(pmail-fontified t))
3222 (font-lock-fontify-region (point-min) (point-max))
3223 (and (not modified) (buffer-modified-p)
3224 (set-buffer-modified-p nil)))))))
3225
3226 ;;; Speedbar support for PMAIL files.
3227 (eval-when-compile (require 'speedbar))
3228
3229 (defvar pmail-speedbar-match-folder-regexp "^[A-Z0-9]+\\(\\.[A-Z0-9]+\\)?$"
3230 "*This regex is used to match folder names to be displayed in speedbar.
3231 Enabling this will permit speedbar to display your folders for easy
3232 browsing, and moving of messages.")
3233
3234 (defvar pmail-speedbar-last-user nil
3235 "The last user to be displayed in the speedbar.")
3236
3237 (defvar pmail-speedbar-key-map nil
3238 "Keymap used when in pmail display mode.")
3239
3240 (defun pmail-install-speedbar-variables ()
3241 "Install those variables used by speedbar to enhance pmail."
3242 (if pmail-speedbar-key-map
3243 nil
3244 (setq pmail-speedbar-key-map (speedbar-make-specialized-keymap))
3245
3246 (define-key pmail-speedbar-key-map "e" 'speedbar-edit-line)
3247 (define-key pmail-speedbar-key-map "r" 'speedbar-edit-line)
3248 (define-key pmail-speedbar-key-map "\C-m" 'speedbar-edit-line)
3249 (define-key pmail-speedbar-key-map "M"
3250 'pmail-speedbar-move-message-to-folder-on-line)))
3251
3252 (defvar pmail-speedbar-menu-items
3253 '(["Read Folder" speedbar-edit-line t]
3254 ["Move message to folder" pmail-speedbar-move-message-to-folder-on-line
3255 (save-excursion (beginning-of-line)
3256 (looking-at "<M> "))])
3257 "Additional menu-items to add to speedbar frame.")
3258
3259 ;; Make sure our special speedbar major mode is loaded
3260 (if (featurep 'speedbar)
3261 (pmail-install-speedbar-variables)
3262 (add-hook 'speedbar-load-hook 'pmail-install-speedbar-variables))
3263
3264 (defun pmail-speedbar-buttons (buffer)
3265 "Create buttons for BUFFER containing pmail messages.
3266 Click on the address under Reply to: to reply to this person.
3267 Under Folders: Click a name to read it, or on the <M> to move the
3268 current message into that PMAIL folder."
3269 (let ((from nil))
3270 (save-excursion
3271 (set-buffer buffer)
3272 (goto-char (point-min))
3273 (if (not (re-search-forward "^Reply-To: " nil t))
3274 (if (not (re-search-forward "^From:? " nil t))
3275 (setq from t)))
3276 (if from
3277 nil
3278 (setq from (buffer-substring (point) (save-excursion
3279 (end-of-line)
3280 (point))))))
3281 (goto-char (point-min))
3282 (if (and (looking-at "Reply to:")
3283 (equal from pmail-speedbar-last-user))
3284 nil
3285 (setq pmail-speedbar-last-user from)
3286 (erase-buffer)
3287 (insert "Reply To:\n")
3288 (if (stringp from)
3289 (speedbar-insert-button from 'speedbar-directory-face 'highlight
3290 'pmail-speedbar-button 'pmail-reply))
3291 (insert "Folders:\n")
3292 (let* ((case-fold-search nil)
3293 (df (directory-files (save-excursion (set-buffer buffer)
3294 default-directory)
3295 nil pmail-speedbar-match-folder-regexp)))
3296 (while df
3297 (speedbar-insert-button "<M>" 'speedbar-button-face 'highlight
3298 'pmail-speedbar-move-message (car df))
3299 (speedbar-insert-button (car df) 'speedbar-file-face 'highlight
3300 'pmail-speedbar-find-file nil t)
3301 (setq df (cdr df)))))))
3302
3303 (defun pmail-speedbar-button (text token indent)
3304 "Execute an pmail command specified by TEXT.
3305 The command used is TOKEN. INDENT is not used."
3306 (speedbar-with-attached-buffer
3307 (funcall token t)))
3308
3309 (defun pmail-speedbar-find-file (text token indent)
3310 "Load in the pmail file TEXT.
3311 TOKEN and INDENT are not used."
3312 (speedbar-with-attached-buffer
3313 (message "Loading in PMAIL file %s..." text)
3314 (find-file text)))
3315
3316 (defun pmail-speedbar-move-message-to-folder-on-line ()
3317 "If the current line is a folder, move current message to it."
3318 (interactive)
3319 (save-excursion
3320 (beginning-of-line)
3321 (if (re-search-forward "<M> " (save-excursion (end-of-line) (point)) t)
3322 (progn
3323 (forward-char -2)
3324 (speedbar-do-function-pointer)))))
3325
3326 (defun pmail-speedbar-move-message (text token indent)
3327 "From button TEXT, copy current message to the pmail file specified by TOKEN.
3328 TEXT and INDENT are not used."
3329 (speedbar-with-attached-buffer
3330 (message "Moving message to %s" token)
3331 (pmail-output-to-pmail-file token)))
3332
3333 ; Functions for setting, getting and encoding the POP password.
3334 ; The password is encoded to prevent it from being easily accessible
3335 ; to "prying eyes." Obviously, this encoding isn't "real security,"
3336 ; nor is it meant to be.
3337
3338 ;;;###autoload
3339 (defun pmail-set-remote-password (password)
3340 "Set PASSWORD to be used for retrieving mail from a POP or IMAP server."
3341 (interactive "sPassword: ")
3342 (if password
3343 (setq pmail-encoded-remote-password
3344 (pmail-encode-string password (emacs-pid)))
3345 (setq pmail-remote-password nil)
3346 (setq pmail-encoded-remote-password nil)))
3347
3348 (defun pmail-get-remote-password (imap)
3349 "Get the password for retrieving mail from a POP or IMAP server. If none
3350 has been set, then prompt the user for one."
3351 (when (not pmail-encoded-remote-password)
3352 (if (not pmail-remote-password)
3353 (setq pmail-remote-password
3354 (read-passwd (if imap
3355 "IMAP password: "
3356 "POP password: "))))
3357 (pmail-set-remote-password pmail-remote-password)
3358 (setq pmail-remote-password nil))
3359 (pmail-encode-string pmail-encoded-remote-password (emacs-pid)))
3360
3361 (defun pmail-have-password ()
3362 (or pmail-remote-password pmail-encoded-remote-password))
3363
3364 (defun pmail-encode-string (string mask)
3365 "Encode STRING with integer MASK, by taking the exclusive OR of the
3366 lowest byte in the mask with the first character of string, the
3367 second-lowest-byte with the second character of the string, etc.,
3368 restarting at the lowest byte of the mask whenever it runs out.
3369 Returns the encoded string. Calling the function again with an
3370 encoded string (and the same mask) will decode the string."
3371 (setq mask (abs mask)) ; doesn't work if negative
3372 (let* ((string-vector (string-to-vector string)) (i 0)
3373 (len (length string-vector)) (curmask mask) charmask)
3374 (while (< i len)
3375 (if (= curmask 0)
3376 (setq curmask mask))
3377 (setq charmask (% curmask 256))
3378 (setq curmask (lsh curmask -8))
3379 (aset string-vector i (logxor charmask (aref string-vector i)))
3380 (setq i (1+ i)))
3381 (concat string-vector)))
3382
3383 ;;;; Desktop support
3384
3385 (defun pmail-restore-desktop-buffer (desktop-buffer-file-name
3386 desktop-buffer-name
3387 desktop-buffer-misc)
3388 "Restore an pmail buffer specified in a desktop file."
3389 (condition-case error
3390 (progn
3391 (pmail-input desktop-buffer-file-name)
3392 (if (eq major-mode 'pmail-mode)
3393 (current-buffer)
3394 pmail-buffer))
3395 (file-locked
3396 (kill-buffer (current-buffer))
3397 nil)))
3398
3399 (add-to-list 'desktop-buffer-mode-handlers
3400 '(pmail-mode . pmail-restore-desktop-buffer))
3401
3402 (provide 'pmail)
3403
3404 ;; arch-tag: cff0a950-57fe-4f73-a86e-91ff75afd06c
3405 ;;; pmail.el ends here