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