(latex-find-indent): ... and handle conjunction correctly this time.
[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))
913 (pmail-show-message (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
987;; Decode Babyl formatted part at the head of current buffer by
988;; pmail-file-coding-system, or if it is nil, do auto conversion.
989
990(defun pmail-decode-babyl-format ()
991 (let ((modifiedp (buffer-modified-p))
992 (buffer-read-only nil)
993 (coding-system pmail-file-coding-system)
994 from to)
995 (goto-char (point-min))
996 (search-forward "\n\^_" nil t) ; Skip BABYL header.
997 (setq from (point))
998 (goto-char (point-max))
999 (search-backward "\n\^_" from 'mv)
1000 (setq to (point))
1001 (unless (and coding-system
1002 (coding-system-p coding-system))
1003 (setq coding-system
1004 ;; If pmail-file-coding-system is nil, Emacs 21 writes
1005 ;; PMAIL files in emacs-mule, Emacs 22 in utf-8, but
1006 ;; earlier versions did that with the current buffer's
1007 ;; encoding. So we want to favor detection of emacs-mule
1008 ;; (whose normal priority is quite low) and utf-8, but
1009 ;; still allow detection of other encodings if they won't
1010 ;; fit. The call to with-coding-priority below achieves
1011 ;; that.
1012 (with-coding-priority '(emacs-mule utf-8)
1013 (detect-coding-region from to 'highest))))
1014 (unless (eq (coding-system-type coding-system) 'undecided)
1015 (set-buffer-modified-p t) ; avoid locking when decoding
1016 (let ((buffer-undo-list t))
1017 (decode-coding-region from to coding-system))
1018 (setq coding-system last-coding-system-used))
1019 (set-buffer-modified-p modifiedp)
1020 (setq buffer-file-coding-system nil)
1021 (setq save-buffer-coding-system
1022 (or coding-system 'undecided))))
1023
1024(defvar pmail-mode-map nil)
1025(if pmail-mode-map
1026 nil
1027 (setq pmail-mode-map (make-keymap))
1028 (suppress-keymap pmail-mode-map)
1029 (define-key pmail-mode-map "a" 'pmail-add-label)
1030 (define-key pmail-mode-map "b" 'pmail-bury)
1031 (define-key pmail-mode-map "c" 'pmail-continue)
1032 (define-key pmail-mode-map "d" 'pmail-delete-forward)
1033 (define-key pmail-mode-map "\C-d" 'pmail-delete-backward)
1034 (define-key pmail-mode-map "e" 'pmail-edit-current-message)
1035 (define-key pmail-mode-map "f" 'pmail-forward)
1036 (define-key pmail-mode-map "g" 'pmail-get-new-mail)
1037 (define-key pmail-mode-map "h" 'pmail-summary)
1038 (define-key pmail-mode-map "i" 'pmail-input)
1039 (define-key pmail-mode-map "j" 'pmail-show-message)
1040 (define-key pmail-mode-map "k" 'pmail-kill-label)
1041 (define-key pmail-mode-map "l" 'pmail-summary-by-labels)
1042 (define-key pmail-mode-map "\e\C-h" 'pmail-summary)
1043 (define-key pmail-mode-map "\e\C-l" 'pmail-summary-by-labels)
1044 (define-key pmail-mode-map "\e\C-r" 'pmail-summary-by-recipients)
1045 (define-key pmail-mode-map "\e\C-s" 'pmail-summary-by-regexp)
1046 (define-key pmail-mode-map "\e\C-t" 'pmail-summary-by-topic)
1047 (define-key pmail-mode-map "m" 'pmail-mail)
1048 (define-key pmail-mode-map "\em" 'pmail-retry-failure)
1049 (define-key pmail-mode-map "n" 'pmail-next-undeleted-message)
1050 (define-key pmail-mode-map "\en" 'pmail-next-message)
1051 (define-key pmail-mode-map "\e\C-n" 'pmail-next-labeled-message)
1052 (define-key pmail-mode-map "o" 'pmail-output-to-pmail-file)
1053 (define-key pmail-mode-map "\C-o" 'pmail-output)
1054 (define-key pmail-mode-map "p" 'pmail-previous-undeleted-message)
1055 (define-key pmail-mode-map "\ep" 'pmail-previous-message)
1056 (define-key pmail-mode-map "\e\C-p" 'pmail-previous-labeled-message)
1057 (define-key pmail-mode-map "q" 'pmail-quit)
1058 (define-key pmail-mode-map "r" 'pmail-reply)
1059;; I find I can't live without the default M-r command -- rms.
1060;; (define-key pmail-mode-map "\er" 'pmail-search-backwards)
1061 (define-key pmail-mode-map "s" 'pmail-expunge-and-save)
1062 (define-key pmail-mode-map "\es" 'pmail-search)
1063 (define-key pmail-mode-map "t" 'pmail-toggle-header)
1064 (define-key pmail-mode-map "u" 'pmail-undelete-previous-message)
1065 (define-key pmail-mode-map "w" 'pmail-output-body-to-file)
1066 (define-key pmail-mode-map "x" 'pmail-expunge)
1067 (define-key pmail-mode-map "." 'pmail-beginning-of-message)
1068 (define-key pmail-mode-map "/" 'pmail-end-of-message)
1069 (define-key pmail-mode-map "<" 'pmail-first-message)
1070 (define-key pmail-mode-map ">" 'pmail-last-message)
1071 (define-key pmail-mode-map " " 'scroll-up)
1072 (define-key pmail-mode-map "\177" 'scroll-down)
1073 (define-key pmail-mode-map "?" 'describe-mode)
1074 (define-key pmail-mode-map "\C-c\C-s\C-d" 'pmail-sort-by-date)
1075 (define-key pmail-mode-map "\C-c\C-s\C-s" 'pmail-sort-by-subject)
1076 (define-key pmail-mode-map "\C-c\C-s\C-a" 'pmail-sort-by-author)
1077 (define-key pmail-mode-map "\C-c\C-s\C-r" 'pmail-sort-by-recipient)
1078 (define-key pmail-mode-map "\C-c\C-s\C-c" 'pmail-sort-by-correspondent)
1079 (define-key pmail-mode-map "\C-c\C-s\C-l" 'pmail-sort-by-lines)
1080 (define-key pmail-mode-map "\C-c\C-s\C-k" 'pmail-sort-by-labels)
1081 (define-key pmail-mode-map "\C-c\C-n" 'pmail-next-same-subject)
1082 (define-key pmail-mode-map "\C-c\C-p" 'pmail-previous-same-subject)
1083 )
1084\f
1085(define-key pmail-mode-map [menu-bar] (make-sparse-keymap))
1086
1087(define-key pmail-mode-map [menu-bar classify]
1088 (cons "Classify" (make-sparse-keymap "Classify")))
1089
1090(define-key pmail-mode-map [menu-bar classify input-menu]
1091 nil)
1092
1093(define-key pmail-mode-map [menu-bar classify output-menu]
1094 nil)
1095
1096(define-key pmail-mode-map [menu-bar classify output-body]
1097 '("Output body to file..." . pmail-output-body-to-file))
1098
1099(define-key pmail-mode-map [menu-bar classify output-inbox]
1100 '("Output (inbox)..." . pmail-output))
1101
1102(define-key pmail-mode-map [menu-bar classify output]
1103 '("Output (Pmail)..." . pmail-output-to-pmail-file))
1104
1105(define-key pmail-mode-map [menu-bar classify kill-label]
1106 '("Kill Label..." . pmail-kill-label))
1107
1108(define-key pmail-mode-map [menu-bar classify add-label]
1109 '("Add Label..." . pmail-add-label))
1110
1111(define-key pmail-mode-map [menu-bar summary]
1112 (cons "Summary" (make-sparse-keymap "Summary")))
1113
1114(define-key pmail-mode-map [menu-bar summary senders]
1115 '("By Senders..." . pmail-summary-by-senders))
1116
1117(define-key pmail-mode-map [menu-bar summary labels]
1118 '("By Labels..." . pmail-summary-by-labels))
1119
1120(define-key pmail-mode-map [menu-bar summary recipients]
1121 '("By Recipients..." . pmail-summary-by-recipients))
1122
1123(define-key pmail-mode-map [menu-bar summary topic]
1124 '("By Topic..." . pmail-summary-by-topic))
1125
1126(define-key pmail-mode-map [menu-bar summary regexp]
1127 '("By Regexp..." . pmail-summary-by-regexp))
1128
1129(define-key pmail-mode-map [menu-bar summary all]
1130 '("All" . pmail-summary))
1131
1132(define-key pmail-mode-map [menu-bar mail]
1133 (cons "Mail" (make-sparse-keymap "Mail")))
1134
1135(define-key pmail-mode-map [menu-bar mail pmail-get-new-mail]
1136 '("Get New Mail" . pmail-get-new-mail))
1137
1138(define-key pmail-mode-map [menu-bar mail lambda]
1139 '("----"))
1140
1141(define-key pmail-mode-map [menu-bar mail continue]
1142 '("Continue" . pmail-continue))
1143
1144(define-key pmail-mode-map [menu-bar mail resend]
1145 '("Re-send..." . pmail-resend))
1146
1147(define-key pmail-mode-map [menu-bar mail forward]
1148 '("Forward" . pmail-forward))
1149
1150(define-key pmail-mode-map [menu-bar mail retry]
1151 '("Retry" . pmail-retry-failure))
1152
1153(define-key pmail-mode-map [menu-bar mail reply]
1154 '("Reply" . pmail-reply))
1155
1156(define-key pmail-mode-map [menu-bar mail mail]
1157 '("Mail" . pmail-mail))
1158
1159(define-key pmail-mode-map [menu-bar delete]
1160 (cons "Delete" (make-sparse-keymap "Delete")))
1161
1162(define-key pmail-mode-map [menu-bar delete expunge/save]
1163 '("Expunge/Save" . pmail-expunge-and-save))
1164
1165(define-key pmail-mode-map [menu-bar delete expunge]
1166 '("Expunge" . pmail-expunge))
1167
1168(define-key pmail-mode-map [menu-bar delete undelete]
1169 '("Undelete" . pmail-undelete-previous-message))
1170
1171(define-key pmail-mode-map [menu-bar delete delete]
1172 '("Delete" . pmail-delete-forward))
1173
1174(define-key pmail-mode-map [menu-bar move]
1175 (cons "Move" (make-sparse-keymap "Move")))
1176
1177(define-key pmail-mode-map [menu-bar move search-back]
1178 '("Search Back..." . pmail-search-backwards))
1179
1180(define-key pmail-mode-map [menu-bar move search]
1181 '("Search..." . pmail-search))
1182
1183(define-key pmail-mode-map [menu-bar move previous]
1184 '("Previous Nondeleted" . pmail-previous-undeleted-message))
1185
1186(define-key pmail-mode-map [menu-bar move next]
1187 '("Next Nondeleted" . pmail-next-undeleted-message))
1188
1189(define-key pmail-mode-map [menu-bar move last]
1190 '("Last" . pmail-last-message))
1191
1192(define-key pmail-mode-map [menu-bar move first]
1193 '("First" . pmail-first-message))
1194
1195(define-key pmail-mode-map [menu-bar move previous]
1196 '("Previous" . pmail-previous-message))
1197
1198(define-key pmail-mode-map [menu-bar move next]
1199 '("Next" . pmail-next-message))
e131541f
PR
1200
1201;; Pmail toolbar
1202(defvar pmail-tool-bar-map
1203 (if (display-graphic-p)
1204 (let ((map (make-sparse-keymap)))
1205 (tool-bar-local-item-from-menu 'pmail-get-new-mail "mail/inbox"
1206 map pmail-mode-map)
1207 (tool-bar-local-item-from-menu 'pmail-next-undeleted-message "right-arrow"
1208 map pmail-mode-map)
1209 (tool-bar-local-item-from-menu 'pmail-previous-undeleted-message "left-arrow"
1210 map pmail-mode-map)
1211 (tool-bar-local-item-from-menu 'pmail-search "search"
1212 map pmail-mode-map)
1213 (tool-bar-local-item-from-menu 'pmail-input "open"
1214 map pmail-mode-map)
1215 (tool-bar-local-item-from-menu 'pmail-mail "mail/compose"
1216 map pmail-mode-map)
1217 (tool-bar-local-item-from-menu 'pmail-reply "mail/reply-all"
1218 map pmail-mode-map)
1219 (tool-bar-local-item-from-menu 'pmail-forward "mail/forward"
1220 map pmail-mode-map)
1221 (tool-bar-local-item-from-menu 'pmail-delete-forward "close"
1222 map pmail-mode-map)
1223 (tool-bar-local-item-from-menu 'pmail-output "mail/move"
1224 map pmail-mode-map)
1225 (tool-bar-local-item-from-menu 'pmail-output-body-to-file "mail/save"
1226 map pmail-mode-map)
1227 (tool-bar-local-item-from-menu 'pmail-expunge "delete"
1228 map pmail-mode-map)
1229 map)))
1230
1231
1232\f
1233;; Pmail mode is suitable only for specially formatted data.
1234(put 'pmail-mode 'mode-class 'special)
1235
1236(defun pmail-mode-kill-summary ()
1237 (if pmail-summary-buffer (kill-buffer pmail-summary-buffer)))
1238
1239;;;###autoload
1240(defun pmail-mode ()
1241 "Pmail Mode is used by \\<pmail-mode-map>\\[pmail] for editing Pmail files.
1242All normal editing commands are turned off.
1243Instead, these commands are available:
1244
1245\\[pmail-beginning-of-message] Move point to front of this message.
1246\\[pmail-end-of-message] Move point to bottom of this message.
1247\\[scroll-up] Scroll to next screen of this message.
1248\\[scroll-down] Scroll to previous screen of this message.
1249\\[pmail-next-undeleted-message] Move to Next non-deleted message.
1250\\[pmail-previous-undeleted-message] Move to Previous non-deleted message.
1251\\[pmail-next-message] Move to Next message whether deleted or not.
1252\\[pmail-previous-message] Move to Previous message whether deleted or not.
1253\\[pmail-first-message] Move to the first message in Pmail file.
1254\\[pmail-last-message] Move to the last message in Pmail file.
1255\\[pmail-show-message] Jump to message specified by numeric position in file.
1256\\[pmail-search] Search for string and show message it is found in.
1257\\[pmail-delete-forward] Delete this message, move to next nondeleted.
1258\\[pmail-delete-backward] Delete this message, move to previous nondeleted.
1259\\[pmail-undelete-previous-message] Undelete message. Tries current message, then earlier messages
1260 till a deleted message is found.
1261\\[pmail-edit-current-message] Edit the current message. \\[pmail-cease-edit] to return to Pmail.
1262\\[pmail-expunge] Expunge deleted messages.
1263\\[pmail-expunge-and-save] Expunge and save the file.
1264\\[pmail-quit] Quit Pmail: expunge, save, then switch to another buffer.
1265\\[save-buffer] Save without expunging.
1266\\[pmail-get-new-mail] Move new mail from system spool directory into this file.
1267\\[pmail-mail] Mail a message (same as \\[mail-other-window]).
1268\\[pmail-continue] Continue composing outgoing message started before.
1269\\[pmail-reply] Reply to this message. Like \\[pmail-mail] but initializes some fields.
1270\\[pmail-retry-failure] Send this message again. Used on a mailer failure message.
1271\\[pmail-forward] Forward this message to another user.
1272\\[pmail-output-to-pmail-file] Output this message to an Pmail file (append it).
1273\\[pmail-output] Output this message to a Unix-format mail file (append it).
1274\\[pmail-output-body-to-file] Save message body to a file. Default filename comes from Subject line.
1275\\[pmail-input] Input Pmail file. Run Pmail on that file.
1276\\[pmail-add-label] Add label to message. It will be displayed in the mode line.
1277\\[pmail-kill-label] Kill label. Remove a label from current message.
1278\\[pmail-next-labeled-message] Move to Next message with specified label
1279 (label defaults to last one specified).
1280 Standard labels: filed, unseen, answered, forwarded, deleted.
1281 Any other label is present only if you add it with \\[pmail-add-label].
1282\\[pmail-previous-labeled-message] Move to Previous message with specified label
1283\\[pmail-summary] Show headers buffer, with a one line summary of each message.
1284\\[pmail-summary-by-labels] Summarize only messages with particular label(s).
1285\\[pmail-summary-by-recipients] Summarize only messages with particular recipient(s).
1286\\[pmail-summary-by-regexp] Summarize only messages with particular regexp(s).
1287\\[pmail-summary-by-topic] Summarize only messages with subject line regexp(s).
1288\\[pmail-toggle-header] Toggle display of complete header."
1289 (interactive)
1290 (let ((finding-pmail-file (not (eq major-mode 'pmail-mode))))
1291 (pmail-mode-2)
1292 (when (and finding-pmail-file
61dcccff
PR
1293 (null coding-system-for-read)
1294 default-enable-multibyte-characters)
e131541f 1295 (let ((pmail-enable-multibyte t))
61dcccff
PR
1296 (pmail-require-mime-maybe)
1297 (pmail-convert-file-maybe)
1298 (goto-char (point-max))
1299 (set-buffer-multibyte t)))
1300 (pmail-set-message-counters)
e131541f
PR
1301 (pmail-show-message pmail-total-messages)
1302 (when finding-pmail-file
1303 (when pmail-display-summary
1304 (pmail-summary))
1305 (pmail-construct-io-menu))
1306 (run-mode-hooks 'pmail-mode-hook)))
1307
1308(defun pmail-mode-2 ()
1309 (kill-all-local-variables)
1310 (pmail-mode-1)
1311 (pmail-perm-variables)
1312 (pmail-variables))
1313
1314(defun pmail-mode-1 ()
1315 (setq major-mode 'pmail-mode)
1316 (setq mode-name "PMAIL")
1317 (setq buffer-read-only t)
1318 ;; No need to auto save PMAIL files in normal circumstances
1319 ;; because they contain no info except attribute changes
1320 ;; and deletion of messages.
1321 ;; The one exception is when messages are copied into an Pmail mode buffer.
1322 ;; pmail-output-to-pmail-file enables auto save when you do that.
1323 (setq buffer-auto-save-file-name nil)
1324 (setq mode-line-modified "--")
1325 (use-local-map pmail-mode-map)
1326 (set-syntax-table text-mode-syntax-table)
1327 (setq local-abbrev-table text-mode-abbrev-table))
1328
61dcccff
PR
1329(defun pmail-generate-viewer-buffer ()
1330 "Return a newly created buffer suitable for viewing messages."
1331 (let ((suffix (file-name-nondirectory (or buffer-file-name (buffer-name)))))
1332 (generate-new-buffer (format " *message-viewer %s*" suffix))))
1333
e131541f
PR
1334;; Set up the permanent locals associated with an Pmail file.
1335(defun pmail-perm-variables ()
1336 (make-local-variable 'pmail-last-label)
1337 (make-local-variable 'pmail-last-regexp)
61dcccff 1338 (make-local-variable 'pmail-deleted-vector)
e131541f
PR
1339 (make-local-variable 'pmail-buffer)
1340 (setq pmail-buffer (current-buffer))
1341 (make-local-variable 'pmail-view-buffer)
61dcccff 1342 (setq pmail-view-buffer (pmail-generate-viewer-buffer))
e131541f 1343 (make-local-variable 'pmail-summary-buffer)
61dcccff 1344 (make-local-variable 'pmail-summary-vector)
e131541f
PR
1345 (make-local-variable 'pmail-current-message)
1346 (make-local-variable 'pmail-total-messages)
1347 (make-local-variable 'pmail-overlay-list)
1348 (setq pmail-overlay-list nil)
61dcccff
PR
1349 (make-local-variable 'pmail-message-vector)
1350 (make-local-variable 'pmail-msgref-vector)
e131541f 1351 (make-local-variable 'pmail-inbox-list)
61dcccff 1352 (setq pmail-inbox-list (pmail-parse-file-inboxes))
e131541f
PR
1353 ;; Provide default set of inboxes for primary mail file ~/PMAIL.
1354 (and (null pmail-inbox-list)
1355 (or (equal buffer-file-name (expand-file-name pmail-file-name))
1356 (equal buffer-file-truename
1357 (abbreviate-file-name (file-truename pmail-file-name))))
1358 (setq pmail-inbox-list
1359 (or pmail-primary-inbox-list
1360 (list (or (getenv "MAIL")
1361 (concat rmail-spool-directory
1362 (user-login-name)))))))
1363 (make-local-variable 'pmail-keywords)
1364 (set (make-local-variable 'tool-bar-map) pmail-tool-bar-map)
61dcccff 1365 (make-local-variable 'pmail-buffers-swapped-p)
e131541f
PR
1366 ;; this gets generated as needed
1367 (setq pmail-keywords nil))
1368
1369;; Set up the non-permanent locals associated with Pmail mode.
1370(defun pmail-variables ()
61dcccff
PR
1371 (make-local-variable 'save-buffer-coding-system)
1372 ;; If we don't already have a value for save-buffer-coding-system,
1373 ;; get it from buffer-file-coding-system, and clear that
1374 ;; because it should be determined in pmail-show-message.
1375 (unless save-buffer-coding-system
1376 (setq save-buffer-coding-system (or buffer-file-coding-system 'undecided))
1377 (setq buffer-file-coding-system nil))
e131541f
PR
1378 ;; Don't let a local variables list in a message cause confusion.
1379 (make-local-variable 'local-enable-local-variables)
1380 (setq local-enable-local-variables nil)
1381 (make-local-variable 'revert-buffer-function)
1382 (setq revert-buffer-function 'pmail-revert)
1383 (make-local-variable 'font-lock-defaults)
1384 (setq font-lock-defaults
1385 '(pmail-font-lock-keywords
1386 t t nil nil
1387 (font-lock-maximum-size . nil)
1388 (font-lock-fontify-buffer-function . pmail-fontify-buffer-function)
1389 (font-lock-unfontify-buffer-function . pmail-unfontify-buffer-function)
1390 (font-lock-inhibit-thing-lock . (lazy-lock-mode fast-lock-mode))))
1391 (make-local-variable 'require-final-newline)
1392 (setq require-final-newline nil)
1393 (make-local-variable 'version-control)
1394 (setq version-control 'never)
1395 (make-local-variable 'kill-buffer-hook)
1396 (add-hook 'kill-buffer-hook 'pmail-mode-kill-summary)
1397 (make-local-variable 'file-precious-flag)
1398 (setq file-precious-flag t)
1399 (make-local-variable 'desktop-save-buffer)
1400 (setq desktop-save-buffer t))
1401
1402;; Handle M-x revert-buffer done in an pmail-mode buffer.
1403(defun pmail-revert (arg noconfirm)
61dcccff
PR
1404 (set-buffer pmail-buffer)
1405 (let* ((revert-buffer-function (default-value 'revert-buffer-function))
1406 (pmail-enable-multibyte enable-multibyte-characters)
1407 ;; See similar code in `pmail'.
1408 (coding-system-for-read (and pmail-enable-multibyte 'raw-text)))
1409 ;; Call our caller again, but this time it does the default thing.
1410 (when (revert-buffer arg noconfirm)
1411 ;; If the user said "yes", and we changed something,
1412 ;; reparse the messages.
1413 (set-buffer pmail-buffer)
1414 (pmail-mode-2)
1415 ;; Convert all or part to Babyl file if possible.
1416 (pmail-convert-file-maybe)
1417 ;; We have read the file as raw-text, so the buffer is set to
1418 ;; unibyte. Make it multibyte if necessary.
1419 (if (and pmail-enable-multibyte
1420 (not enable-multibyte-characters))
1421 (set-buffer-multibyte t))
1422 (goto-char (point-max))
1423 (pmail-set-message-counters)
1424 (pmail-show-message pmail-total-messages)
1425 (run-hooks 'pmail-mode-hook))))
1426
1427;; Return a list of files from this buffer's Mail: option.
1428;; Does not assume that messages have been parsed.
1429;; Just returns nil if buffer does not look like Babyl format.
1430(defun pmail-parse-file-inboxes ()
1431 (save-excursion
1432 (save-restriction
1433 (widen)
1434 (goto-char 1)
1435 (cond ((looking-at "BABYL OPTIONS:")
1436 (search-forward "\n\^_" nil 'move)
1437 (narrow-to-region 1 (point))
1438 (goto-char 1)
1439 (when (search-forward "\nMail:" nil t)
1440 (narrow-to-region (point) (progn (end-of-line) (point)))
1441 (goto-char (point-min))
1442 (mail-parse-comma-list)))))))
1443
e131541f
PR
1444(defun pmail-expunge-and-save ()
1445 "Expunge and save PMAIL file."
1446 (interactive)
1447 (pmail-expunge)
61dcccff 1448 (set-buffer pmail-buffer)
e131541f 1449 (save-buffer)
e131541f 1450 (if (pmail-summary-exists)
61dcccff 1451 (pmail-select-summary (set-buffer-modified-p nil))))
e131541f 1452
e131541f
PR
1453(defun pmail-quit ()
1454 "Quit out of PMAIL.
1455Hook `pmail-quit-hook' is run after expunging."
1456 (interactive)
61dcccff
PR
1457 ;; Determine if the buffers need to be swapped.
1458 (pmail-swap-buffers-maybe)
e131541f
PR
1459 (pmail-expunge-and-save)
1460 (when (boundp 'pmail-quit-hook)
1461 (run-hooks 'pmail-quit-hook))
1462 ;; Don't switch to the summary buffer even if it was recently visible.
1463 (when pmail-summary-buffer
1464 (replace-buffer-in-windows pmail-summary-buffer)
1465 (bury-buffer pmail-summary-buffer))
1466 (if pmail-enable-mime
1467 (let ((obuf pmail-buffer)
1468 (ovbuf pmail-view-buffer))
1469 (set-buffer pmail-view-buffer)
1470 (quit-window)
1471 (replace-buffer-in-windows ovbuf)
1472 (replace-buffer-in-windows obuf)
1473 (bury-buffer obuf))
1474 (let ((obuf (current-buffer)))
1475 (quit-window)
1476 (replace-buffer-in-windows obuf))))
1477
e131541f
PR
1478(defun pmail-bury ()
1479 "Bury current Pmail buffer and its summary buffer."
1480 (interactive)
1481 ;; This let var was called pmail-buffer, but that interfered
1482 ;; with the buffer-local var used in summary buffers.
1483 (let ((buffer-to-bury (current-buffer)))
1484 (if (pmail-summary-exists)
1485 (let (window)
1486 (while (setq window (get-buffer-window pmail-summary-buffer))
1487 (quit-window nil window))
1488 (bury-buffer pmail-summary-buffer)))
1489 (quit-window)))
1490
e131541f
PR
1491(defun pmail-duplicate-message ()
1492 "Create a duplicated copy of the current message.
1493The duplicate copy goes into the Pmail file just after the
1494original copy."
1495 (interactive)
1496 (widen)
1497 (let ((buffer-read-only nil)
1498 (number pmail-current-message)
61dcccff
PR
1499 (string (buffer-substring (pmail-msgbeg pmail-current-message)
1500 (pmail-msgend pmail-current-message))))
1501 (goto-char (pmail-msgend pmail-current-message))
e131541f 1502 (insert string)
61dcccff 1503 (pmail-forget-messages)
e131541f
PR
1504 (pmail-show-message number)
1505 (message "Message duplicated")))
1506
1507;;;###autoload
1508(defun pmail-input (filename)
1509 "Run Pmail on file FILENAME."
1510 (interactive "FRun pmail on PMAIL file: ")
1511 (pmail filename))
1512
61dcccff 1513
e131541f
PR
1514;; This used to scan subdirectories recursively, but someone pointed out
1515;; that if the user wants that, person can put all the files in one dir.
61dcccff
PR
1516;; And the recursive scan was slow. So I took it out.
1517;; rms, Sep 1996.
e131541f
PR
1518(defun pmail-find-all-files (start)
1519 "Return list of file in dir START that match `pmail-secondary-file-regexp'."
1520 (if (file-accessible-directory-p start)
1521 ;; Don't sort here.
1522 (let* ((case-fold-search t)
1523 (files (directory-files start t pmail-secondary-file-regexp)))
1524 ;; Sort here instead of in directory-files
1525 ;; because this list is usually much shorter.
1526 (sort files 'string<))))
1527
1528(defun pmail-list-to-menu (menu-name l action &optional full-name)
1529 (let ((menu (make-sparse-keymap menu-name)))
1530 (mapc
1531 (function (lambda (item)
1532 (let (command)
1533 (if (consp item)
1534 (progn
1535 (setq command
1536 (pmail-list-to-menu (car item) (cdr item)
1537 action
1538 (if full-name
1539 (concat full-name "/"
1540 (car item))
1541 (car item))))
1542 (setq name (car item)))
1543 (progn
1544 (setq name item)
1545 (setq command
1546 (list 'lambda () '(interactive)
1547 (list action
1548 (expand-file-name
1549 (if full-name
1550 (concat full-name "/" item)
1551 item)
1552 pmail-secondary-file-directory))))))
1553 (define-key menu (vector (intern name))
1554 (cons name command)))))
1555 (reverse l))
1556 menu))
1557
1558;; This command is always "disabled" when it appears in a menu.
1559(put 'pmail-disable-menu 'menu-enable ''nil)
1560
1561(defun pmail-construct-io-menu ()
1562 (let ((files (pmail-find-all-files pmail-secondary-file-directory)))
1563 (if files
1564 (progn
1565 (define-key pmail-mode-map [menu-bar classify input-menu]
1566 (cons "Input Pmail File"
1567 (pmail-list-to-menu "Input Pmail File"
1568 files
1569 'pmail-input)))
1570 (define-key pmail-mode-map [menu-bar classify output-menu]
1571 (cons "Output Pmail File"
1572 (pmail-list-to-menu "Output Pmail File"
1573 files
61dcccff 1574 'pmail-output-to-pmail-file))))
e131541f
PR
1575
1576 (define-key pmail-mode-map [menu-bar classify input-menu]
1577 '("Input Pmail File" . pmail-disable-menu))
1578 (define-key pmail-mode-map [menu-bar classify output-menu]
1579 '("Output Pmail File" . pmail-disable-menu)))))
1580
1581\f
1582;;;; *** Pmail input ***
1583
61dcccff
PR
1584(declare-function pmail-spam-filter "pmail-spam-filter" (msg))
1585(declare-function pmail-summary-goto-msg "pmailsum" (&optional n nowarn skip-pmail))
e131541f
PR
1586(declare-function pmail-summary-mark-undeleted "pmailsum" (n))
1587(declare-function pmail-summary-mark-deleted "pmailsum" (&optional n undel))
1588(declare-function rfc822-addresses "rfc822" (header-text))
1589(declare-function mail-abbrev-make-syntax-table "mailabbrev.el" ())
1590(declare-function mail-sendmail-delimit-header "sendmail" ())
1591(declare-function mail-header-end "sendmail" ())
1592
61dcccff
PR
1593;; RLK feature not added in this version:
1594;; argument specifies inbox file or files in various ways.
3e5013c3 1595
e131541f 1596(defun pmail-get-new-mail (&optional file-name)
61dcccff
PR
1597 "Move any new mail from this PMAIL file's inbox files.
1598The inbox files can be specified with the file's Mail: option. The
1599variable `pmail-primary-inbox-list' specifies the inboxes for your
1600primary PMAIL file if it has no Mail: option. By default, this is
1601your /usr/spool/mail/$USER.
1602
1603You can also specify the file to get new mail from. In this case, the
1604file of new mail is not changed or deleted. Noninteractively, you can
1605pass the inbox file name as an argument. Interactively, a prefix
1606argument causes us to read a file name and use that file as the inbox.
e131541f
PR
1607
1608If the variable `pmail-preserve-inbox' is non-nil, new mail will
1609always be left in inbox files rather than deleted.
1610
61dcccff
PR
1611This function runs `pmail-get-new-mail-hook' before saving the updated file.
1612It returns t if it got any new messages."
e131541f 1613 (interactive
61dcccff
PR
1614 (list (if current-prefix-arg
1615 (read-file-name "Get new mail from file: "))))
e131541f 1616 (run-hooks 'pmail-before-get-new-mail-hook)
61dcccff
PR
1617 ;; If the disk file has been changed from under us,
1618 ;; revert to it before we get new mail.
1619 (or (verify-visited-file-modtime (current-buffer))
1620 (find-file (buffer-file-name)))
1621 (set-buffer pmail-buffer)
1622 (pmail-maybe-set-message-counters)
1623 (widen)
1624 ;; Get rid of all undo records for this buffer.
1625 (or (eq buffer-undo-list t)
e131541f 1626 (setq buffer-undo-list nil))
61dcccff
PR
1627 (let ((all-files (if file-name (list file-name)
1628 pmail-inbox-list))
1629 (pmail-enable-multibyte (default-value 'enable-multibyte-characters))
1630 found)
1631 (unwind-protect
1632 (progn
1633 (while all-files
1634 (let ((opoint (point))
1635 (new-messages 0)
1636 (rsf-number-of-spam 0)
1637 (delete-files ())
1638 ;; If buffer has not changed yet, and has not been saved yet,
1639 ;; don't replace the old backup file now.
1640 (make-backup-files (and make-backup-files (buffer-modified-p)))
1641 (buffer-read-only nil)
1642 ;; Don't make undo records for what we do in getting mail.
1643 (buffer-undo-list t)
1644 success
1645 ;; Files to insert this time around.
1646 files
1647 ;; Last names of those files.
1648 file-last-names)
1649 ;; Pull files off all-files onto files
1650 ;; as long as there is no name conflict.
1651 ;; A conflict happens when two inbox file names
1652 ;; have the same last component.
1653 (while (and all-files
1654 (not (member (file-name-nondirectory (car all-files))
1655 file-last-names)))
1656 (setq files (cons (car all-files) files)
1657 file-last-names
1658 (cons (file-name-nondirectory (car all-files)) files))
1659 (setq all-files (cdr all-files)))
1660 ;; Put them back in their original order.
1661 (setq files (nreverse files))
1662
1663 (goto-char (point-max))
1664 (skip-chars-backward " \t\n") ; just in case of brain damage
1665 (delete-region (point) (point-max)) ; caused by require-final-newline
1666 (save-excursion
1667 (save-restriction
1668 (narrow-to-region (point) (point))
1669 ;; Read in the contents of the inbox files,
1670 ;; renaming them as necessary,
1671 ;; and adding to the list of files to delete eventually.
1672 (if file-name
1673 (pmail-insert-inbox-text files nil)
1674 (setq delete-files (pmail-insert-inbox-text files t)))
1675 ;; Scan the new text and convert each message to mbox format.
1676 (goto-char (point-min))
1677 (unwind-protect
1678 (save-excursion
1679 (setq new-messages (pmail-add-babyl-headers)
1680 success t))
1681 ;; Try to delete the garbage just inserted.
1682 (or success (delete-region (point-min) (point-max)))
1683 ;; If we could not convert the file's inboxes,
1684 ;; rename the files we tried to read
1685 ;; so we won't over and over again.
1686 (if (and (not file-name) (not success))
1687 (let ((delfiles delete-files)
1688 (count 0))
1689 (while delfiles
1690 (while (file-exists-p (format "PMAILOSE.%d" count))
1691 (setq count (1+ count)))
1692 (rename-file (car delfiles)
1693 (format "PMAILOSE.%d" count))
1694 (setq delfiles (cdr delfiles))))))
1695 (or (zerop new-messages)
1696 (let (success)
1697 (goto-char (point-min))
1698 (pmail-count-new-messages)
1699 (run-hooks 'pmail-get-new-mail-hook)
1700 (save-buffer)))
1701 ;; Delete the old files, now that babyl file is saved.
1702 (while delete-files
1703 (condition-case ()
1704 ;; First, try deleting.
1705 (condition-case ()
1706 (delete-file (car delete-files))
1707 (file-error
1708 ;; If we can't delete it, truncate it.
1709 (write-region (point) (point) (car delete-files))))
1710 (file-error nil))
1711 (setq delete-files (cdr delete-files)))))
1712 (if (= new-messages 0)
1713 (progn (goto-char opoint)
1714 (if (or file-name pmail-inbox-list)
1715 (message "(No new mail has arrived)")))
1716 ;; check new messages to see if any of them is spam:
1717 (if (and (featurep 'pmail-spam-filter)
1718 pmail-use-spam-filter)
1719 (let*
1720 ((old-messages (- pmail-total-messages new-messages))
1721 (rsf-scanned-message-number (1+ old-messages))
1722 ;; save deletion flags of old messages: vector starts
1723 ;; at zero (is one longer that no of messages),
1724 ;; therefore take 1+ old-messages
1725 (save-deleted
1726 (substring pmail-deleted-vector 0 (1+
1727 old-messages))))
1728 ;; set all messages to undeleted
1729 (setq pmail-deleted-vector
1730 (make-string (1+ pmail-total-messages) ?\ ))
1731 (while (<= rsf-scanned-message-number
1732 pmail-total-messages)
1733 (progn
1734 (if (not (pmail-spam-filter rsf-scanned-message-number))
1735 (progn (setq rsf-number-of-spam (1+ rsf-number-of-spam)))
1736 )
1737 (setq rsf-scanned-message-number (1+ rsf-scanned-message-number))
1738 ))
1739 (if (> rsf-number-of-spam 0)
1740 (progn
1741 (when (pmail-expunge-confirmed)
1742 (pmail-only-expunge t))
1743 ))
1744 (setq pmail-deleted-vector
1745 (concat
1746 save-deleted
1747 (make-string (- pmail-total-messages old-messages)
1748 ?\ )))
1749 ))
1750 (if (pmail-summary-exists)
1751 (pmail-select-summary
1752 (pmail-update-summary)))
1753 (message "%d new message%s read%s"
1754 new-messages (if (= 1 new-messages) "" "s")
1755 ;; print out a message on number of spam messages found:
1756 (if (and (featurep 'pmail-spam-filter)
1757 pmail-use-spam-filter
1758 (> rsf-number-of-spam 0))
1759 (cond ((= 1 new-messages)
1760 ", and appears to be spam")
1761 ((= rsf-number-of-spam new-messages)
1762 ", and all appear to be spam")
1763 ((> rsf-number-of-spam 1)
1764 (format ", and %d appear to be spam"
1765 rsf-number-of-spam))
1766 (t
1767 ", and 1 appears to be spam"))
1768 ""))
1769 (when (and (featurep 'pmail-spam-filter)
1770 pmail-use-spam-filter
1771 (> rsf-number-of-spam 0))
1772 (if rsf-beep (beep t))
1773 (sleep-for rsf-sleep-after-message))
1774
1775 ;; Move to the first new message
1776 ;; unless we have other unseen messages before it.
1777 (pmail-show-message (pmail-first-unseen-message))
1778 (run-hooks 'pmail-after-get-new-mail-hook)
1779 (setq found t))))
1780 found)
1781 ;; Don't leave the buffer screwed up if we get a disk-full error.
1782 (or found (pmail-show-message)))))
e131541f
PR
1783
1784(defun pmail-parse-url (file)
1785 "Parse the supplied URL. Return (list MAILBOX-NAME REMOTE PASSWORD GOT-PASSWORD)
1786WHERE MAILBOX-NAME is the name of the mailbox suitable as argument to the
1787actual version of `movemail', REMOTE is non-nil if MAILBOX-NAME refers to
1788a remote mailbox, PASSWORD is the password if it should be
1789supplied as a separate argument to `movemail' or nil otherwise, GOT-PASSWORD
1790is non-nil if the user has supplied the password interactively.
1791"
1792 (cond
1793 ((string-match "^\\([^:]+\\)://\\(\\([^:@]+\\)\\(:\\([^@]+\\)\\)?@\\)?.*" file)
1794 (let (got-password supplied-password
1795 (proto (match-string 1 file))
1796 (user (match-string 3 file))
1797 (pass (match-string 5 file))
1798 (host (substring file (or (match-end 2)
1799 (+ 3 (match-end 1))))))
1800
1801 (if (not pass)
1802 (when pmail-remote-password-required
1803 (setq got-password (not (pmail-have-password)))
1804 (setq supplied-password (pmail-get-remote-password
61dcccff
PR
1805 (string-equal proto "imap"))))
1806 ;; The password is embedded. Strip it out since movemail
1807 ;; does not really like it, in spite of the movemail spec.
1808 (setq file (concat proto "://" user "@" host)))
1809
e131541f
PR
1810 (if (pmail-movemail-variant-p 'emacs)
1811 (if (string-equal proto "pop")
1812 (list (concat "po:" user ":" host)
1813 t
1814 (or pass supplied-password)
1815 got-password)
1816 (error "Emacs movemail does not support %s protocol" proto))
61dcccff 1817 (list file
e131541f 1818 (or (string-equal proto "pop") (string-equal proto "imap"))
acdc2006 1819 (or supplied-password pass)
e131541f
PR
1820 got-password))))
1821
1822 ((string-match "^po:\\([^:]+\\)\\(:\\(.*\\)\\)?" file)
1823 (let (got-password supplied-password
1824 (proto "pop")
1825 (user (match-string 1 file))
1826 (host (match-string 3 file)))
1827
1828 (when pmail-remote-password-required
1829 (setq got-password (not (pmail-have-password)))
1830 (setq supplied-password (pmail-get-remote-password nil)))
1831
1832 (list file "pop" supplied-password got-password)))
1833
1834 (t
1835 (list file nil nil nil))))
1836
1837(defun pmail-insert-inbox-text (files renamep)
1838 ;; Detect a locked file now, so that we avoid moving mail
1839 ;; out of the real inbox file. (That could scare people.)
1840 (or (memq (file-locked-p buffer-file-name) '(nil t))
1841 (error "PMAIL file %s is locked"
1842 (file-name-nondirectory buffer-file-name)))
1843 (let (file tofile delete-files movemail popmail got-password password)
1844 (while files
1845 ;; Handle remote mailbox names specially; don't expand as filenames
1846 ;; in case the userid contains a directory separator.
1847 (setq file (car files))
1848 (let ((url-data (pmail-parse-url file)))
1849 (setq file (nth 0 url-data))
1850 (setq popmail (nth 1 url-data))
1851 (setq password (nth 2 url-data))
1852 (setq got-password (nth 3 url-data)))
1853
1854 (if popmail
1855 (setq renamep t)
1856 (setq file (file-truename
1857 (substitute-in-file-name (expand-file-name file)))))
1858 (setq tofile (expand-file-name
1859 ;; Generate name to move to from inbox name,
1860 ;; in case of multiple inboxes that need moving.
1861 (concat ".newmail-"
1862 (file-name-nondirectory
acdc2006
PR
1863 (if (memq system-type '(windows-nt cygwin ms-dos))
1864 ;; cannot have colons in file name
1865 (replace-regexp-in-string ":" "-" file)
e131541f
PR
1866 file)))
1867 ;; Use the directory of this pmail file
1868 ;; because it's a nuisance to use the homedir
1869 ;; if that is on a full disk and this pmail
1870 ;; file isn't.
1871 (file-name-directory
1872 (expand-file-name buffer-file-name))))
1873 ;; Always use movemail to rename the file,
1874 ;; since there can be mailboxes in various directories.
61dcccff
PR
1875 (when (not popmail)
1876 ;; On some systems, /usr/spool/mail/foo is a directory
1877 ;; and the actual inbox is /usr/spool/mail/foo/foo.
1878 (if (file-directory-p file)
1879 (setq file (expand-file-name (user-login-name)
1880 file))))
e131541f
PR
1881 (cond (popmail
1882 (message "Getting mail from the remote server ..."))
1883 ((and (file-exists-p tofile)
1884 (/= 0 (nth 7 (file-attributes tofile))))
1885 (message "Getting mail from %s..." tofile))
1886 ((and (file-exists-p file)
1887 (/= 0 (nth 7 (file-attributes file))))
1888 (message "Getting mail from %s..." file)))
1889 ;; Set TOFILE if have not already done so, and
1890 ;; rename or copy the file FILE to TOFILE if and as appropriate.
1891 (cond ((not renamep)
1892 (setq tofile file))
1893 ((or (file-exists-p tofile) (and (not popmail)
1894 (not (file-exists-p file))))
1895 nil)
1896 (t
1897 (with-temp-buffer
1898 (let ((errors (current-buffer)))
1899 (buffer-disable-undo errors)
1900 (let ((args
1901 (append
acdc2006 1902 (list (or pmail-movemail-program "movemail") nil errors nil)
e131541f
PR
1903 (if pmail-preserve-inbox
1904 (list "-p")
1905 nil)
1906 (if (pmail-movemail-variant-p 'mailutils)
1907 (append (list "--emacs") pmail-movemail-flags)
1908 pmail-movemail-flags)
1909 (list file tofile)
1910 (if password (list password) nil))))
1911 (apply 'call-process args))
1912 (if (not (buffer-modified-p errors))
1913 ;; No output => movemail won
1914 nil
1915 (set-buffer errors)
1916 (subst-char-in-region (point-min) (point-max)
1917 ?\n ?\ )
1918 (goto-char (point-max))
1919 (skip-chars-backward " \t")
1920 (delete-region (point) (point-max))
1921 (goto-char (point-min))
1922 (if (looking-at "movemail: ")
1923 (delete-region (point-min) (match-end 0)))
1924 (beep t)
1925 ;; If we just read the password, most likely it is
1926 ;; wrong. Otherwise, see if there is a specific
1927 ;; reason to think that the problem is a wrong passwd.
1928 (if (or got-password
1929 (re-search-forward pmail-remote-password-error
1930 nil t))
1931 (pmail-set-remote-password nil))
1932
1933 ;; If using Mailutils, remove initial error code
1934 ;; abbreviation
1935 (when (pmail-movemail-variant-p 'mailutils)
1936 (goto-char (point-min))
1937 (when (looking-at "[A-Z][A-Z0-9_]*:")
1938 (delete-region (point-min) (match-end 0))))
1939
1940 (message "movemail: %s"
1941 (buffer-substring (point-min)
1942 (point-max)))
1943
1944 (sit-for 3)
1945 nil)))))
1946
1947 ;; At this point, TOFILE contains the name to read:
1948 ;; Either the alternate name (if we renamed)
1949 ;; or the actual inbox (if not renaming).
1950 (if (file-exists-p tofile)
1951 (let ((coding-system-for-read 'no-conversion)
1952 size)
1953 (goto-char (point-max))
1954 (setq size (nth 1 (insert-file-contents tofile)))
61dcccff
PR
1955 ;; Determine if a pair of newline message separators need
1956 ;; to be added to the new collection of messages. This is
1957 ;; the case for all new message collections added to a
1958 ;; non-empty mail file.
1959 (unless (zerop size)
1960 (save-restriction
1961 (let ((start (point-min)))
1962 (widen)
1963 (unless (eq start (point-min))
1964 (goto-char start)
1965 (insert "\n\n")
1966 (setq size (+ 2 size))))))
e131541f
PR
1967 (goto-char (point-max))
1968 (or (= (preceding-char) ?\n)
1969 (zerop size)
1970 (insert ?\n))
1971 (if (not (and pmail-preserve-inbox (string= file tofile)))
1972 (setq delete-files (cons tofile delete-files)))))
1973 (message "")
1974 (setq files (cdr files)))
1975 delete-files))
61dcccff
PR
1976
1977;; Decode the region specified by FROM and TO by CODING.
1978;; If CODING is nil or an invalid coding system, decode by `undecided'.
1979(defun pmail-decode-region (from to coding)
1980 (if (or (not coding) (not (coding-system-p coding)))
1981 (setq coding 'undecided))
1982 ;; Use -dos decoding, to remove ^M characters left from base64 or
1983 ;; rogue qp-encoded text.
1984 (decode-coding-region from to
1985 (coding-system-change-eol-conversion coding 1))
1986 ;; Don't reveal the fact we used -dos decoding, as users generally
1987 ;; will not expect the PMAIL buffer to use DOS EOL format.
1988 (setq buffer-file-coding-system
1989 (setq last-coding-system-used
1990 (coding-system-change-eol-conversion coding 0))))
1991
1992(defun pmail-add-babyl-headers ()
1993 "Validate the RFC2822 format for the new messages. Point, at
1994entry should be looking at the first new message. An error will
1995be thrown if the new messages are not RCC2822 compliant. Lastly,
1996unless one already exists, add an Rmail attribute header to the
1997new messages in the region "
1998 (let ((count 0)
1999 (start (point))
2000 limit)
2001 ;; Detect an empty inbox file.
2002 (unless (= start (point-max))
2003 ;; Scan the new messages to establish a count and to insure that
2004 ;; an attribute header is present.
2005 (while (looking-at "From ")
2006 ;; Determine if a new attribute header needs to be added to
2007 ;; the message.
2008 (if (search-forward "\n\n" nil t)
2009 (progn
2010 (setq count (1+ count))
2011 (forward-char -1)
2012 (narrow-to-region start (point))
2013 (unless (mail-fetch-field pmail-attribute-header)
2014 (insert pmail-attribute-header ": ------U\n"))
2015 (widen))
2016 (error "Invalid mbox format detected in inbox file"))
2017 ;; Move to the next message.
2018 (if (search-forward "\n\nFrom " nil 'move)
2019 (forward-char -5))
2020 (setq start (point))))
2021 count))
2022
2023;; the pmail-break-forwarded-messages feature is not implemented
2024(defun pmail-convert-to-babyl-format ()
2025 (let ((count 0) start
2026 (case-fold-search nil)
2027 (buffer-undo-list t)
2028 (invalid-input-resync
2029 (function (lambda ()
2030 (message "Invalid Babyl format in inbox!")
2031 (sit-for 3)
2032 ;; Try to get back in sync with a real message.
2033 (if (re-search-forward
2034 (concat pmail-mmdf-delim1 "\\|^From") nil t)
2035 (beginning-of-line)
2036 (goto-char (point-max)))))))
2037 (goto-char (point-min))
2038 (save-restriction
2039 (while (not (eobp))
2040 (setq start (point))
2041 (cond ((looking-at "BABYL OPTIONS:") ;Babyl header
2042 (if (search-forward "\n\^_" nil t)
2043 ;; If we find the proper terminator, delete through there.
2044 (delete-region (point-min) (point))
2045 (funcall invalid-input-resync)
2046 (delete-region (point-min) (point))))
2047 ;; Babyl format message
2048 ((looking-at "\^L")
2049 (or (search-forward "\n\^_" nil t)
2050 (funcall invalid-input-resync))
2051 (setq count (1+ count))
2052 ;; Make sure there is no extra white space after the ^_
2053 ;; at the end of the message.
2054 ;; Narrowing will make sure that whatever follows the junk
2055 ;; will be treated properly.
2056 (delete-region (point)
2057 (save-excursion
2058 (skip-chars-forward " \t\n")
2059 (point)))
2060 ;; The following let* form was wrapped in a `save-excursion'
2061 ;; which in one case caused infinite looping, see:
2062 ;; http://lists.gnu.org/archive/html/emacs-devel/2008-01/msg00968.html
2063 ;; Removing that form leaves `point' at the end of the
2064 ;; region decoded by `pmail-decode-region' which should
2065 ;; be correct.
2066 (let* ((header-end
2067 (progn
2068 (save-excursion
2069 (goto-char start)
2070 (forward-line 1)
2071 (if (looking-at "0")
2072 (forward-line 1)
2073 (forward-line 2))
2074 (save-restriction
2075 (narrow-to-region (point) (point-max))
2076 (rfc822-goto-eoh)
2077 (point)))))
2078 (case-fold-search t)
2079 (quoted-printable-header-field-end
2080 (save-excursion
2081 (goto-char start)
2082 (re-search-forward
2083 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
2084 header-end t)))
2085 (base64-header-field-end
2086 (save-excursion
2087 (goto-char start)
2088 ;; Don't try to decode non-text data.
2089 (and (re-search-forward
2090 "^content-type:\\(\n?[\t ]\\)\\(text\\|message\\)/"
2091 header-end t)
2092 (goto-char start)
2093 (re-search-forward
2094 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
2095 header-end t)))))
2096 (if quoted-printable-header-field-end
2097 (save-excursion
2098 (unless
2099 (mail-unquote-printable-region header-end (point) nil t t)
2100 (message "Malformed MIME quoted-printable message"))
2101 ;; Change "quoted-printable" to "8bit",
2102 ;; to reflect the decoding we just did.
2103 (goto-char quoted-printable-header-field-end)
2104 (delete-region (point) (search-backward ":"))
2105 (insert ": 8bit")))
2106 (if base64-header-field-end
2107 (save-excursion
2108 (when
2109 (condition-case nil
2110 (progn
2111 (base64-decode-region (1+ header-end)
2112 (- (point) 2))
2113 t)
2114 (error nil))
2115 ;; Change "base64" to "8bit", to reflect the
2116 ;; decoding we just did.
2117 (goto-char base64-header-field-end)
2118 (delete-region (point) (search-backward ":"))
2119 (insert ": 8bit"))))
2120 (setq last-coding-system-used nil)
2121 (or pmail-enable-mime
2122 (not pmail-enable-multibyte)
2123 (let ((mime-charset
2124 (if (and pmail-decode-mime-charset
2125 (save-excursion
2126 (goto-char start)
2127 (search-forward "\n\n" nil t)
2128 (let ((case-fold-search t))
2129 (re-search-backward
2130 pmail-mime-charset-pattern
2131 start t))))
2132 (intern (downcase (match-string 1))))))
2133 (pmail-decode-region start (point) mime-charset))))
2134 ;; Add an X-Coding-System: header if we don't have one.
2135 (save-excursion
2136 (goto-char start)
2137 (forward-line 1)
2138 (if (looking-at "0")
2139 (forward-line 1)
2140 (forward-line 2))
2141 (or (save-restriction
2142 (narrow-to-region (point) (point-max))
2143 (rfc822-goto-eoh)
2144 (goto-char (point-min))
2145 (re-search-forward "^X-Coding-System:" nil t))
2146 (insert "X-Coding-System: "
2147 (symbol-name last-coding-system-used)
2148 "\n")))
2149 (narrow-to-region (point) (point-max))
2150 (and (= 0 (% count 10))
2151 (message "Converting to Babyl format...%d" count)))
2152 ;;*** MMDF format
2153 ((let ((case-fold-search t))
2154 (looking-at pmail-mmdf-delim1))
2155 (let ((case-fold-search t))
2156 (replace-match "\^L\n0, unseen,,\n*** EOOH ***\n")
2157 (re-search-forward pmail-mmdf-delim2 nil t)
2158 (replace-match "\^_"))
2159 (save-excursion
2160 (save-restriction
2161 (narrow-to-region start (1- (point)))
2162 (goto-char (point-min))
2163 (while (search-forward "\n\^_" nil t) ; single char "\^_"
2164 (replace-match "\n^_")))) ; 2 chars: "^" and "_"
2165 (setq last-coding-system-used nil)
2166 (or pmail-enable-mime
2167 (not pmail-enable-multibyte)
2168 (decode-coding-region start (point) 'undecided))
2169 (save-excursion
2170 (goto-char start)
2171 (forward-line 3)
2172 (insert "X-Coding-System: "
2173 (symbol-name last-coding-system-used)
2174 "\n"))
2175 (narrow-to-region (point) (point-max))
2176 (setq count (1+ count))
2177 (and (= 0 (% count 10))
2178 (message "Converting to Babyl format...%d" count)))
2179 ;;*** Mail format
2180 ((looking-at "^From ")
2181 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
2182 (pmail-nuke-pinhead-header)
2183 ;; If this message has a Content-Length field,
2184 ;; skip to the end of the contents.
2185 (let* ((header-end (save-excursion
2186 (and (re-search-forward "\n\n" nil t)
2187 (1- (point)))))
2188 (case-fold-search t)
2189 (quoted-printable-header-field-end
2190 (save-excursion
2191 (re-search-forward
2192 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
2193 header-end t)))
2194 (base64-header-field-end
2195 (and
2196 ;; Don't decode non-text data.
2197 (save-excursion
2198 (re-search-forward
2199 "^content-type:\\(\n?[\t ]\\)\\(text\\|message\\)/"
2200 header-end t))
2201 (save-excursion
2202 (re-search-forward
2203 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
2204 header-end t))))
2205 (size
2206 ;; Get the numeric value from the Content-Length field.
2207 (save-excursion
2208 ;; Back up to end of prev line,
2209 ;; in case the Content-Length field comes first.
2210 (forward-char -1)
2211 (and (search-forward "\ncontent-length: "
2212 header-end t)
2213 (let ((beg (point))
2214 (eol (progn (end-of-line) (point))))
2215 (string-to-number (buffer-substring beg eol)))))))
2216 (and size
2217 (if (and (natnump size)
2218 (<= (+ header-end size) (point-max))
2219 ;; Make sure this would put us at a position
2220 ;; that we could continue from.
2221 (save-excursion
2222 (goto-char (+ header-end size))
2223 (skip-chars-forward "\n")
2224 (or (eobp)
2225 (and (looking-at "BABYL OPTIONS:")
2226 (search-forward "\n\^_" nil t))
2227 (and (looking-at "\^L")
2228 (search-forward "\n\^_" nil t))
2229 (let ((case-fold-search t))
2230 (looking-at pmail-mmdf-delim1))
2231 (looking-at "From "))))
2232 (goto-char (+ header-end size))
2233 (message "Ignoring invalid Content-Length field")
2234 (sit-for 1 0 t)))
2235 (if (let ((case-fold-search nil))
2236 (re-search-forward
2237 (concat "^[\^_]?\\("
2238 pmail-unix-mail-delimiter
2239 "\\|"
2240 pmail-mmdf-delim1 "\\|"
2241 "^BABYL OPTIONS:\\|"
2242 "\^L\n[01],\\)") nil t))
2243 (goto-char (match-beginning 1))
2244 (goto-char (point-max)))
2245 (setq count (1+ count))
2246 (if quoted-printable-header-field-end
2247 (save-excursion
2248 (unless
2249 (mail-unquote-printable-region header-end (point) nil t t)
2250 (message "Malformed MIME quoted-printable message"))
2251 ;; Change "quoted-printable" to "8bit",
2252 ;; to reflect the decoding we just did.
2253 (goto-char quoted-printable-header-field-end)
2254 (delete-region (point) (search-backward ":"))
2255 (insert ": 8bit")))
2256 (if base64-header-field-end
2257 (save-excursion
2258 (when
2259 (condition-case nil
2260 (progn
2261 (base64-decode-region
2262 (1+ header-end)
2263 (save-excursion
2264 ;; Prevent base64-decode-region
2265 ;; from removing newline characters.
2266 (skip-chars-backward "\n\t ")
2267 (point)))
2268 t)
2269 (error nil))
2270 ;; Change "base64" to "8bit", to reflect the
2271 ;; decoding we just did.
2272 (goto-char base64-header-field-end)
2273 (delete-region (point) (search-backward ":"))
2274 (insert ": 8bit")))))
2275
2276 (save-excursion
2277 (save-restriction
2278 (narrow-to-region start (point))
2279 (goto-char (point-min))
2280 (while (search-forward "\n\^_" nil t) ; single char
2281 (replace-match "\n^_")))) ; 2 chars: "^" and "_"
2282 ;; This is for malformed messages that don't end in newline.
2283 ;; There shouldn't be any, but some users say occasionally
2284 ;; there are some.
2285 (or (bolp) (newline))
2286 (insert ?\^_)
2287 (setq last-coding-system-used nil)
2288 (or pmail-enable-mime
2289 (not pmail-enable-multibyte)
2290 (let ((mime-charset
2291 (if (and pmail-decode-mime-charset
2292 (save-excursion
2293 (goto-char start)
2294 (search-forward "\n\n" nil t)
2295 (let ((case-fold-search t))
2296 (re-search-backward
2297 pmail-mime-charset-pattern
2298 start t))))
2299 (intern (downcase (match-string 1))))))
2300 (pmail-decode-region start (point) mime-charset)))
2301 (save-excursion
2302 (goto-char start)
2303 (forward-line 3)
2304 (insert "X-Coding-System: "
2305 (symbol-name last-coding-system-used)
2306 "\n"))
2307 (narrow-to-region (point) (point-max))
2308 (and (= 0 (% count 10))
2309 (message "Converting to Babyl format...%d" count)))
2310 ;;
2311 ;; This kludge is because some versions of sendmail.el
2312 ;; insert an extra newline at the beginning that shouldn't
2313 ;; be there. sendmail.el has been fixed, but old versions
2314 ;; may still be in use. -- rms, 7 May 1993.
2315 ((eolp) (delete-char 1))
2316 (t (error "Cannot convert to babyl format")))))
2317 (setq buffer-undo-list nil)
2318 count))
2319
2320;; Delete the "From ..." line, creating various other headers with
2321;; information from it if they don't already exist. Now puts the
2322;; original line into a mail-from: header line for debugging and for
2323;; use by the pmail-output function.
2324(defun pmail-nuke-pinhead-header ()
2325 (save-excursion
2326 (save-restriction
2327 (let ((start (point))
2328 (end (progn
2329 (condition-case ()
2330 (search-forward "\n\n")
2331 (error
2332 (goto-char (point-max))
2333 (insert "\n\n")))
2334 (point)))
2335 has-from has-date)
2336 (narrow-to-region start end)
2337 (let ((case-fold-search t))
2338 (goto-char start)
2339 (setq has-from (search-forward "\nFrom:" nil t))
2340 (goto-char start)
2341 (setq has-date (and (search-forward "\nDate:" nil t) (point)))
2342 (goto-char start))
2343 (let ((case-fold-search nil))
2344 (if (re-search-forward (concat "^" pmail-unix-mail-delimiter) nil t)
2345 (replace-match
2346 (concat
2347 "Mail-from: \\&"
2348 ;; Keep and reformat the date if we don't
2349 ;; have a Date: field.
2350 (if has-date
2351 ""
2352 (concat
2353 "Date: \\2, \\4 \\3 \\9 \\5 "
2354
2355 ;; The timezone could be matched by group 7 or group 10.
2356 ;; If neither of them matched, assume EST, since only
2357 ;; Easterners would be so sloppy.
2358 ;; It's a shame the substitution can't use "\\10".
2359 (cond
2360 ((/= (match-beginning 7) (match-end 7)) "\\7")
2361 ((/= (match-beginning 10) (match-end 10))
2362 (buffer-substring (match-beginning 10)
2363 (match-end 10)))
2364 (t "EST"))
2365 "\n"))
2366 ;; Keep and reformat the sender if we don't
2367 ;; have a From: field.
2368 (if has-from
2369 ""
2370 "From: \\1\n"))
2371 t)))))))
e131541f
PR
2372\f
2373;;;; *** Pmail Message Formatting and Header Manipulation ***
2374
61dcccff
PR
2375(defun pmail-copy-headers (beg end &optional ignored-headers)
2376 "Copy displayed header fields to the message viewer buffer.
2377BEG and END marks the start and end positions of the message in
2378the mail buffer. If the optional argument IGNORED-HEADERS is
2379non-nil, ignore all header fields whose names match that regexp.
2380Otherwise, if `rmail-displayed-headers' is non-nil, copy only
2381those header fields whose names match that regexp. Otherwise,
2382copy all header fields whose names do not match
2383`rmail-ignored-headers' (unless they also match
2384`rmail-nonignored-headers')."
2385 (let ((result "")
2386 (header-start-regexp "\n[^ \t]")
2387 lim)
2388 (with-current-buffer pmail-buffer
2389 (when (search-forward "\n\n" nil t)
2390 (forward-char -1)
e131541f 2391 (save-restriction
61dcccff
PR
2392 ;; Put point right after the From header line.
2393 (narrow-to-region beg (point))
e131541f 2394 (goto-char (point-min))
61dcccff
PR
2395 (unless (re-search-forward header-start-regexp nil t)
2396 (error "Invalid mbox format; no header follows the From message separator."))
2397 (forward-char -1)
2398 (cond
2399 ;; Handle the case where all headers should be copied.
2400 ((eq pmail-header-style 'full)
2401 (setq result (buffer-substring beg (point-max))))
2402 ;; Handle the case where the headers matching the diplayed
2403 ;; headers regexp should be copied.
2404 ((and pmail-displayed-headers (null ignored-headers))
2405 (while (not (eobp))
2406 (save-excursion
2407 (setq lim (if (re-search-forward header-start-regexp nil t)
2408 (1+ (match-beginning 0))
2409 (point-max))))
2410 (when (looking-at pmail-displayed-headers)
2411 (setq result (concat result (buffer-substring (point) lim))))
2412 (goto-char lim)))
2413 ;; Handle the ignored headers.
2414 ((or ignored-headers (setq ignored-headers pmail-ignored-headers))
2415 (while (and ignored-headers (not (eobp)))
2416 (save-excursion
2417 (setq lim (if (re-search-forward header-start-regexp nil t)
2418 (1+ (match-beginning 0))
2419 (point-max))))
2420 (if (and (looking-at ignored-headers)
2421 (not (looking-at pmail-nonignored-headers)))
2422 (goto-char lim)
2423 (setq result (concat result (buffer-substring (point) lim)))
2424 (goto-char lim))))
2425 (t (error "No headers selected for display!"))))))
2426 result))
2427
2428(defun pmail-copy-body (beg end)
2429 "Return the message body to be displayed in the view buffer.
2430BEG and END marks the start and end positions of the message in
2431the mail buffer."
2432 (with-current-buffer pmail-buffer
2433 (if (search-forward "\n\n" nil t)
2434 (buffer-substring (point) end)
2435 (error "Invalid message format: no header/body separator"))))
e131541f
PR
2436
2437(defun pmail-toggle-header (&optional arg)
2438 "Show original message header if pruned header currently shown, or vice versa.
2439With argument ARG, show the message header pruned if ARG is greater than zero;
2440otherwise, show it in full."
2441 (interactive "P")
61dcccff
PR
2442 (setq pmail-header-style
2443 (cond
2444 ((and (numberp arg) (> arg 0)) 'normal)
2445 ((eq pmail-header-style 'full) 'normal)
2446 (t 'full)))
2447 (pmail-show-message))
e131541f
PR
2448
2449;; Lifted from repos-count-screen-lines.
61dcccff 2450;; Return number of screen lines between START and END.
e131541f 2451(defun pmail-count-screen-lines (start end)
e131541f
PR
2452 (save-excursion
2453 (save-restriction
2454 (narrow-to-region start end)
2455 (goto-char (point-min))
2456 (vertical-motion (- (point-max) (point-min))))))
2457\f
2458;;;; *** Pmail Attributes and Keywords ***
2459
61dcccff
PR
2460(defun pmail-get-header (name &optional msg)
2461 "Return the value of message header NAME, nil if no such header
2462exists. MSG, if set identifies the message number to use. The
2463current mail message will be used otherwise."
2464 (save-excursion
2465 (save-restriction
2466 (with-current-buffer pmail-buffer
2467 (widen)
2468 (let* ((n (or msg pmail-current-message))
2469 (beg (pmail-msgbeg n))
2470 end)
2471 (goto-char beg)
2472 (setq end (search-forward "\n\n" nil t))
2473 (if end
2474 (progn
2475 (narrow-to-region beg end)
2476 (mail-fetch-field name))
2477 (error "Invalid mbox format encountered.")))))))
2478
2479(defun pmail-get-attr-names (&optional msg)
2480 "Return the message attributes in a comma separated string.
2481MSG, if set identifies the message number to use. The current
2482mail message will be used otherwise."
2483 (let ((value (pmail-get-header pmail-attribute-field-name msg))
2484 result temp)
2485 (dotimes (index (length value))
2486 (setq temp (and (not (= ?- (aref value index)))
2487 (nth 1 (aref pmail-attr-array index)))
2488 result
2489 (cond
2490 ((and temp result) (format "%s, %s" result temp))
2491 (temp temp)
2492 (t result))))
2493 result))
2494
2495(defun pmail-get-keywords (&optional msg)
2496 "Return the message keywords in a comma separated string.
2497MSG, if set identifies the message number to use. The current
2498mail message will be used otherwise."
2499 (pmail-get-header pmail-keyword-header msg))
2500
e131541f 2501(defun pmail-display-labels ()
61dcccff
PR
2502 "Update the mode line with the (set) attributes and keywords
2503for the current message."
2504 (let (blurb attr-names keywords)
2505 ;; Combine the message attributes and keywords into a comma
2506 ;; separated list.
2507 (setq attr-names (pmail-get-attr-names pmail-current-message)
2508 keywords (pmail-get-keywords pmail-current-message))
2509 (setq blurb
2510 (cond
2511 ((and attr-names keywords) (concat attr-names ", " keywords))
2512 (attr-names attr-names)
2513 (keywords keywords)
2514 (t "")))
e131541f
PR
2515 (setq mode-line-process
2516 (format " %d/%d%s"
61dcccff 2517 pmail-current-message pmail-total-messages blurb))
e131541f
PR
2518 ;; If pmail-enable-mime is non-nil, we may have to update
2519 ;; `mode-line-process' of pmail-view-buffer too.
2520 (if (and pmail-enable-mime
2521 (not (eq (current-buffer) pmail-view-buffer))
2522 (buffer-live-p pmail-view-buffer))
2523 (let ((mlp mode-line-process))
2524 (with-current-buffer pmail-view-buffer
2525 (setq mode-line-process mlp))))))
2526
61dcccff
PR
2527(defun pmail-get-attr-value (attr state)
2528 "Return the character value for ATTR.
2529ATTR is a (numberic) index, an offset into the mbox attribute
2530header value. STATE is one of nil, t, or a character value."
2531 (cond
2532 ((numberp state) state)
2533 ((not state) ?-)
2534 (t (nth 0 (aref pmail-attr-array attr)))))
2535
e131541f 2536(defun pmail-set-attribute (attr state &optional msgnum)
61dcccff
PR
2537 "Turn an attribute of a message on or off according to STATE.
2538STATE is either nil or the character (numeric) value associated
2539with the state (nil represents off and non-nil represents on).
2540ATTR is the index of the attribute. MSGNUM is message number to
2541change; nil means current message."
2542 (set-buffer pmail-buffer)
2543 (let ((value (pmail-get-attr-value attr state))
2544 (omax (point-max-marker))
2545 (omin (point-min-marker))
2546 (buffer-read-only nil)
2547 limit)
2548 (or msgnum (setq msgnum pmail-current-message))
2549 (if (> msgnum 0)
2550 (unwind-protect
2551 (save-excursion
2552 ;; Determine if the current state is the desired state.
2553 (widen)
2554 (goto-char (pmail-msgbeg msgnum))
2555 (save-excursion
2556 (setq limit (search-forward "\n\n" nil t)))
2557 (when (search-forward (concat pmail-attribute-header ": ") limit t)
2558 (forward-char attr)
2559 (when (/= value (char-after))
2560 (delete-char 1)
2561 (insert value)))
2562 (if (= attr pmail-deleted-attr-index)
2563 (pmail-set-message-deleted-p msgnum state)))
2564 ;; Note: we don't use save-restriction because that does not work right
2565 ;; if changes are made outside the saved restriction
2566 ;; before that restriction is restored.
2567 (narrow-to-region omin omax)
2568 (set-marker omin nil)
2569 (set-marker omax nil)
2570 (if (= msgnum pmail-current-message)
2571 (pmail-display-labels))))))
2572
2573(defun pmail-message-attr-p (msg attrs)
2574 "Return t if the attributes header for message MSG contains a
2575match for the regexp ATTRS."
e131541f
PR
2576 (save-excursion
2577 (save-restriction
61dcccff
PR
2578 (let ((start (pmail-msgbeg msg))
2579 limit)
2580 (widen)
2581 (goto-char start)
2582 (setq limit (search-forward "\n\n" (pmail-msgend msg) t))
2583 (goto-char start)
2584 (and limit
2585 (search-forward (concat pmail-attribute-header ": ") limit t)
2586 (looking-at attrs))))))
e131541f
PR
2587\f
2588;;;; *** Pmail Message Selection And Support ***
2589
e131541f 2590(defun pmail-msgend (n)
61dcccff
PR
2591 (marker-position (aref pmail-message-vector (1+ n))))
2592
2593(defun pmail-msgbeg (n)
2594 (marker-position (aref pmail-message-vector n)))
e131541f
PR
2595
2596(defun pmail-widen-to-current-msgbeg (function)
2597 "Call FUNCTION with point at start of internal data of current message.
2598Assumes that bounds were previously narrowed to display the message in Pmail.
2599The bounds are widened enough to move point where desired, then narrowed
2600again afterward.
2601
2602FUNCTION may not change the visible text of the message, but it may
2603change the invisible header text."
2604 (save-excursion
2605 (unwind-protect
2606 (progn
61dcccff 2607 (narrow-to-region (pmail-msgbeg pmail-current-message)
e131541f
PR
2608 (point-max))
2609 (goto-char (point-min))
2610 (funcall function))
2611 ;; Note: we don't use save-restriction because that does not work right
2612 ;; if changes are made outside the saved restriction
2613 ;; before that restriction is restored.
61dcccff
PR
2614 (narrow-to-region (pmail-msgbeg pmail-current-message)
2615 (pmail-msgend pmail-current-message)))))
2616
2617(defun pmail-forget-messages ()
2618 (unwind-protect
2619 (if (vectorp pmail-message-vector)
2620 (let* ((i 0)
2621 (v pmail-message-vector)
2622 (n (length v)))
2623 (while (< i n)
2624 (move-marker (aref v i) nil)
2625 (setq i (1+ i)))))
2626 (setq pmail-message-vector nil)
2627 (setq pmail-msgref-vector nil)
2628 (setq pmail-deleted-vector nil)))
2629
2630(defun pmail-maybe-set-message-counters ()
2631 (if (not (and pmail-deleted-vector
2632 pmail-message-vector
2633 pmail-current-message
2634 pmail-total-messages))
2635 (pmail-set-message-counters)))
2636
2637(defun pmail-count-new-messages (&optional nomsg)
2638 "Count the number of new messages in the region.
2639Output a helpful message unless NOMSG is non-nil."
2640 (let* ((case-fold-search nil)
2641 (total-messages 0)
2642 (messages-head nil)
2643 (deleted-head nil))
2644 (or nomsg (message "Counting new messages..."))
2645 (goto-char (point-max))
2646 ;; Put at the end of messages-head
2647 ;; the entry for message N+1, which marks
2648 ;; the end of message N. (N = number of messages).
2649 (setq messages-head (list (point-marker)))
2650 (pmail-set-message-counters-counter (point-min))
2651 (setq pmail-current-message (1+ pmail-total-messages))
2652 (setq pmail-total-messages
2653 (+ pmail-total-messages total-messages))
2654 (setq pmail-message-vector
2655 (vconcat pmail-message-vector (cdr messages-head)))
2656 (aset pmail-message-vector
2657 pmail-current-message (car messages-head))
2658 (setq pmail-deleted-vector
2659 (concat pmail-deleted-vector deleted-head))
2660 (setq pmail-summary-vector
2661 (vconcat pmail-summary-vector (make-vector total-messages nil)))
2662 (setq pmail-msgref-vector
2663 (vconcat pmail-msgref-vector (make-vector total-messages nil)))
2664 ;; Fill in the new elements of pmail-msgref-vector.
2665 (let ((i (1+ (- pmail-total-messages total-messages))))
2666 (while (<= i pmail-total-messages)
2667 (aset pmail-msgref-vector i (list i))
2668 (setq i (1+ i))))
2669 (goto-char (point-min))
2670 (or nomsg (message "Counting new messages...done (%d)" total-messages))))
2671
2672(defun pmail-set-message-counters ()
2673 (pmail-forget-messages)
2674 (save-excursion
2675 (save-restriction
2676 (widen)
2677 (let* ((point-save (point))
2678 (total-messages 0)
2679 (messages-after-point)
2680 (case-fold-search nil)
2681 (messages-head nil)
2682 (deleted-head nil))
2683 ;; Determine how many messages follow point.
2684 (message "Counting messages...")
2685 (goto-char (point-max))
2686 ;; Put at the end of messages-head
2687 ;; the entry for message N+1, which marks
2688 ;; the end of message N. (N = number of messages).
2689 (setq messages-head (list (point-marker)))
2690 (pmail-set-message-counters-counter (min (point) point-save))
2691 (setq messages-after-point total-messages)
2692
2693 ;; Determine how many precede point.
2694 (pmail-set-message-counters-counter)
2695 (setq pmail-total-messages total-messages)
2696 (setq pmail-current-message
2697 (min total-messages
2698 (max 1 (- total-messages messages-after-point))))
2699 (setq pmail-message-vector
2700 (apply 'vector (cons (point-min-marker) messages-head))
2701 pmail-deleted-vector (concat "0" deleted-head)
2702 pmail-summary-vector (make-vector pmail-total-messages nil)
2703 pmail-msgref-vector (make-vector (1+ pmail-total-messages) nil))
2704 (let ((i 0))
2705 (while (<= i pmail-total-messages)
2706 (aset pmail-msgref-vector i (list i))
2707 (setq i (1+ i))))
2708 (message "Counting messages...done")))))
2709
2710
2711(defsubst pmail-collect-deleted (message-end)
2712 "Collect the message deletion flags for each message.
2713MESSAGE-END is the buffer position corresponding to the end of
2714the message. Point is at the beginning of the message."
2715 ;; NOTE: This piece of code will be executed on a per-message basis.
2716 ;; In the face of thousands of messages, it has to be as fast as
2717 ;; possible, hence some brute force constant use is employed in
2718 ;; addition to inlining.
2719 (save-excursion
2720 (setq deleted-head
2721 (cons (if (and (search-forward "X-BABYL-V6-ATTRIBUTES: " message-end t)
2722 (looking-at "?D"))
2723 ?D
2724 ?\ ) deleted-head))))
2725
2726(defun pmail-set-message-counters-counter (&optional stop)
2727 ;; Collect the start position for each message into 'messages-head.
2728 (let ((start (point)))
2729 (while (search-backward "\n\nFrom " stop t)
2730 (forward-char 2)
2731 (pmail-collect-deleted start)
2732 ;; Show progress after every 20 messages or so.
2733 (setq messages-head (cons (point-marker) messages-head)
2734 total-messages (1+ total-messages)
2735 start (point))
2736 (if (zerop (% total-messages 20))
2737 (message "Counting messages...%d" total-messages)))
2738 ;; Handle the first message, maybe.
2739 (if stop
2740 (goto-char stop)
2741 (goto-char (point-min)))
2742 (unless (not (looking-at "From "))
2743 (pmail-collect-deleted start)
2744 (setq messages-head (cons (point-marker) messages-head)
2745 total-messages (1+ total-messages)))))
e131541f
PR
2746
2747(defun pmail-beginning-of-message ()
2748 "Show current message starting from the beginning."
2749 (interactive)
2750 (let ((pmail-show-message-hook
2751 (list (function (lambda ()
2752 (goto-char (point-min)))))))
2753 (pmail-show-message pmail-current-message)))
2754
2755(defun pmail-end-of-message ()
2756 "Show bottom of current message."
2757 (interactive)
2758 (let ((pmail-show-message-hook
2759 (list (function (lambda ()
2760 (goto-char (point-max))
2761 (recenter (1- (window-height))))))))
2762 (pmail-show-message pmail-current-message)))
2763
2764(defun pmail-unknown-mail-followup-to ()
2765 "Handle a \"Mail-Followup-To\" header field with an unknown mailing list.
2766Ask the user whether to add that list name to `mail-mailing-lists'."
61dcccff
PR
2767 (save-restriction
2768 (let ((mail-followup-to (mail-fetch-field "mail-followup-to" nil t)))
2769 (when mail-followup-to
2770 (let ((addresses
2771 (split-string
2772 (mail-strip-quoted-names mail-followup-to)
2773 ",[[:space:]]+" t)))
2774 (dolist (addr addresses)
2775 (when (and (not (member addr mail-mailing-lists))
2776 (not
2777 ;; taken from pmailsum.el
2778 (string-match
2779 (or pmail-user-mail-address-regexp
2780 (concat "^\\("
2781 (regexp-quote (user-login-name))
2782 "\\($\\|@\\)\\|"
2783 (regexp-quote
2784 (or user-mail-address
2785 (concat (user-login-name) "@"
2786 (or mail-host-address
2787 (system-name)))))
2788 "\\>\\)"))
2789 addr))
2790 (y-or-n-p
2791 (format "Add `%s' to `mail-mailing-lists'? "
2792 addr)))
2793 (customize-save-variable 'mail-mailing-lists
2794 (cons addr mail-mailing-lists)))))))))
2795
2796(defun pmail-swap-buffers-maybe ()
2797 "Determine if the Pmail buffer is showing a message.
2798If so restore the actual mbox message collection."
2799 (unless (not pmail-buffers-swapped-p)
2800 (with-current-buffer pmail-buffer
2801 (buffer-swap-text pmail-view-buffer)
2802 (setq pmail-buffers-swapped-p nil))))
e131541f
PR
2803
2804(defun pmail-show-message (&optional n no-summary)
2805 "Show message number N (prefix argument), counting from start of file.
61dcccff 2806If summary buffer is currently displayed, update current message there also."
e131541f 2807 (interactive "p")
61dcccff
PR
2808 (or (eq major-mode 'pmail-mode)
2809 (switch-to-buffer pmail-buffer))
2810 (pmail-swap-buffers-maybe)
2811 (pmail-maybe-set-message-counters)
2812 (widen)
2813 (let (blurb)
2814 (if (zerop pmail-total-messages)
2815 (save-excursion
2816 (with-current-buffer pmail-view-buffer
2817 (erase-buffer)
2818 (setq blurb "No mail.")))
e131541f
PR
2819 (if (not n)
2820 (setq n pmail-current-message)
2821 (cond ((<= n 0)
2822 (setq n 1
2823 pmail-current-message 1
2824 blurb "No previous message"))
2825 ((> n pmail-total-messages)
2826 (setq n pmail-total-messages
2827 pmail-current-message pmail-total-messages
2828 blurb "No following message"))
2829 (t
2830 (setq pmail-current-message n))))
61dcccff
PR
2831 (let ((buf pmail-buffer)
2832 (beg (pmail-msgbeg n))
2833 (end (pmail-msgend n))
2834 headers body)
2835 (goto-char beg)
2836 (setq headers (pmail-copy-headers beg end)
2837 body (pmail-copy-body beg end))
2838 (pmail-set-attribute pmail-unseen-attr-index nil)
2839 (with-current-buffer pmail-view-buffer
2840 (erase-buffer)
2841 (insert headers "\n")
2842 (pmail-highlight-headers)
2843 (insert body)
2844 (goto-char (point-min)))))
2845 (when mail-mailing-lists
2846 (pmail-unknown-mail-followup-to))
2847 (if transient-mark-mode (deactivate-mark))
2848 (pmail-display-labels)
2849 (buffer-swap-text pmail-view-buffer)
2850 (setq pmail-buffers-swapped-p t)
2851 (run-hooks 'pmail-show-message-hook)
2852 ;; If there is a summary buffer, try to move to this message
2853 ;; in that buffer. But don't complain if this message
2854 ;; is not mentioned in the summary.
2855 ;; Don't do this at all if we were called on behalf
2856 ;; of cursor motion in the summary buffer.
2857 (and (pmail-summary-exists) (not no-summary)
2858 (let ((curr-msg pmail-current-message))
2859 (pmail-select-summary
2860 (pmail-summary-goto-msg curr-msg t t))))
e131541f 2861 (with-current-buffer pmail-buffer
61dcccff
PR
2862 (pmail-auto-file))
2863 (if blurb
2864 (message blurb))))
2865
2866;; Find all occurrences of certain fields, and highlight them.
2867(defun pmail-highlight-headers ()
2868 ;; Do this only if the system supports faces.
2869 (if (and (fboundp 'internal-find-face)
2870 pmail-highlighted-headers)
e131541f 2871 (save-excursion
61dcccff
PR
2872 (search-forward "\n\n" nil 'move)
2873 (save-restriction
2874 (narrow-to-region (point-min) (point))
2875 (let ((case-fold-search t)
2876 (inhibit-read-only t)
2877 ;; Highlight with boldface if that is available.
2878 ;; Otherwise use the `highlight' face.
2879 (face (or 'pmail-highlight
2880 (if (face-differs-from-default-p 'bold)
2881 'bold 'highlight)))
2882 ;; List of overlays to reuse.
2883 (overlays pmail-overlay-list))
2884 (goto-char (point-min))
2885 (while (re-search-forward pmail-highlighted-headers nil t)
2886 (skip-chars-forward " \t")
2887 (let ((beg (point))
2888 overlay)
2889 (while (progn (forward-line 1)
2890 (looking-at "[ \t]")))
2891 ;; Back up over newline, then trailing spaces or tabs
2892 (forward-char -1)
2893 (while (member (preceding-char) '(? ?\t))
2894 (forward-char -1))
2895 (if overlays
2896 ;; Reuse an overlay we already have.
2897 (progn
2898 (setq overlay (car overlays)
2899 overlays (cdr overlays))
2900 (overlay-put overlay 'face face)
2901 (move-overlay overlay beg (point)))
2902 ;; Make a new overlay and add it to
2903 ;; pmail-overlay-list.
2904 (setq overlay (make-overlay beg (point)))
2905 (overlay-put overlay 'face face)
2906 (setq pmail-overlay-list
2907 (cons overlay pmail-overlay-list))))))))))
2908
e131541f
PR
2909(defun pmail-auto-file ()
2910 "Automatically move a message into a sub-folder based on criteria.
2911Called when a new message is displayed."
61dcccff
PR
2912 (if (or (zerop pmail-total-messages)
2913 (pmail-message-attr-p pmail-current-message "...F...")
e131541f
PR
2914 (not (string= (buffer-file-name)
2915 (expand-file-name pmail-file-name))))
61dcccff
PR
2916 ;; Do nothing if the message has already been filed or if there
2917 ;; are no messages.
e131541f
PR
2918 nil
2919 ;; Find out some basics (common fields)
2920 (let ((from (mail-fetch-field "from"))
2921 (subj (mail-fetch-field "subject"))
2922 (to (concat (mail-fetch-field "to") "," (mail-fetch-field "cc")))
61dcccff 2923 (d pmail-automatic-folder-directives)
e131541f
PR
2924 (directive-loop nil)
2925 (folder nil))
61dcccff
PR
2926 (while d
2927 (setq folder (car (car d))
2928 directive-loop (cdr (car d)))
e131541f
PR
2929 (while (and (car directive-loop)
2930 (let ((f (cond
2931 ((string= (car directive-loop) "from") from)
2932 ((string= (car directive-loop) "to") to)
2933 ((string= (car directive-loop) "subject") subj)
2934 (t (mail-fetch-field (car directive-loop))))))
2935 (and f (string-match (car (cdr directive-loop)) f))))
2936 (setq directive-loop (cdr (cdr directive-loop))))
2937 ;; If there are no directives left, then it was a complete match.
2938 (if (null directive-loop)
2939 (if (null folder)
2940 (pmail-delete-forward)
2941 (if (string= "/dev/null" folder)
2942 (pmail-delete-message)
61dcccff
PR
2943 (pmail-output-to-pmail-file folder 1 t)
2944 (setq d nil))))
2945 (setq d (cdr d))))))
e131541f
PR
2946
2947(defun pmail-next-message (n)
2948 "Show following message whether deleted or not.
61dcccff 2949With prefix arg N, moves forward N messages, or backward if N is negative."
e131541f 2950 (interactive "p")
61dcccff
PR
2951 (set-buffer pmail-buffer)
2952 (pmail-maybe-set-message-counters)
2953 (pmail-show-message (+ pmail-current-message n)))
e131541f
PR
2954
2955(defun pmail-previous-message (n)
2956 "Show previous message whether deleted or not.
61dcccff 2957With prefix arg N, moves backward N messages, or forward if N is negative."
e131541f
PR
2958 (interactive "p")
2959 (pmail-next-message (- n)))
2960
2961(defun pmail-next-undeleted-message (n)
2962 "Show following non-deleted message.
61dcccff
PR
2963With prefix arg N, moves forward N non-deleted messages,
2964or backward if N is negative.
e131541f
PR
2965
2966Returns t if a new message is being shown, nil otherwise."
2967 (interactive "p")
61dcccff
PR
2968 (set-buffer pmail-buffer)
2969 (pmail-maybe-set-message-counters)
e131541f 2970 (let ((lastwin pmail-current-message)
e131541f 2971 (current pmail-current-message))
e131541f
PR
2972 (while (and (> n 0) (< current pmail-total-messages))
2973 (setq current (1+ current))
61dcccff
PR
2974 (if (not (pmail-message-deleted-p current))
2975 (setq lastwin current n (1- n))))
e131541f
PR
2976 (while (and (< n 0) (> current 1))
2977 (setq current (1- current))
61dcccff
PR
2978 (if (not (pmail-message-deleted-p current))
2979 (setq lastwin current n (1+ n))))
2980 (if (/= lastwin pmail-current-message)
2981 (progn (pmail-show-message lastwin)
2982 t)
e131541f
PR
2983 (if (< n 0)
2984 (message "No previous nondeleted message"))
2985 (if (> n 0)
2986 (message "No following nondeleted message"))
2987 nil)))
2988
2989(defun pmail-previous-undeleted-message (n)
2990 "Show previous non-deleted message.
2991With prefix argument N, moves backward N non-deleted messages,
2992or forward if N is negative."
2993 (interactive "p")
2994 (pmail-next-undeleted-message (- n)))
2995
2996(defun pmail-first-message ()
2997 "Show first message in file."
2998 (interactive)
61dcccff 2999 (pmail-maybe-set-message-counters)
e131541f
PR
3000 (pmail-show-message 1))
3001
3002(defun pmail-last-message ()
3003 "Show last message in file."
3004 (interactive)
61dcccff 3005 (pmail-maybe-set-message-counters)
e131541f
PR
3006 (pmail-show-message pmail-total-messages))
3007
61dcccff
PR
3008(defun pmail-what-message ()
3009 (let ((where (point))
3010 (low 1)
3011 (high pmail-total-messages)
3012 (mid (/ pmail-total-messages 2)))
3013 (while (> (- high low) 1)
3014 (if (>= where (pmail-msgbeg mid))
3015 (setq low mid)
3016 (setq high mid))
3017 (setq mid (+ low (/ (- high low) 2))))
3018 (if (>= where (pmail-msgbeg high)) high low)))
e131541f
PR
3019
3020(defun pmail-message-recipients-p (msg recipients &optional primary-only)
3021 (save-restriction
61dcccff
PR
3022 (goto-char (pmail-msgbeg msg))
3023 (search-forward "\n*** EOOH ***\n")
3024 (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
e131541f
PR
3025 (or (string-match recipients (or (mail-fetch-field "To") ""))
3026 (string-match recipients (or (mail-fetch-field "From") ""))
3027 (if (not primary-only)
3028 (string-match recipients (or (mail-fetch-field "Cc") ""))))))
3029
61dcccff
PR
3030(defun pmail-message-regexp-p (n regexp)
3031 "Return t, if for message number N, regexp REGEXP matches in the header."
3032 (let ((beg (pmail-msgbeg n))
3033 (end (pmail-msgend n)))
3034 (goto-char beg)
3035 (forward-line 1)
3036 (save-excursion
3037 (save-restriction
3038 (if (prog1 (= (following-char) ?0)
3039 (forward-line 2)
3040 ;; If there's a Summary-line in the (otherwise empty)
3041 ;; header, we didn't yet get past the EOOH line.
3042 (when (looking-at "^\\*\\*\\* EOOH \\*\\*\\*\n")
3043 (forward-line 1))
3044 (setq beg (point))
3045 (narrow-to-region (point) end))
3046 (progn
3047 (rfc822-goto-eoh)
3048 (setq end (point)))
3049 (setq beg (point))
3050 (search-forward "\n*** EOOH ***\n" end t)
3051 (setq end (1+ (match-beginning 0)))))
3052 (goto-char beg)
3053 (if pmail-enable-mime
3054 (funcall pmail-search-mime-header-function n regexp end)
3055 (re-search-forward regexp end t)))))
e131541f
PR
3056
3057(defun pmail-search-message (msg regexp)
3058 "Return non-nil, if for message number MSG, regexp REGEXP matches."
61dcccff 3059 (goto-char (pmail-msgbeg msg))
e131541f
PR
3060 (if pmail-enable-mime
3061 (funcall pmail-search-mime-message-function msg regexp)
61dcccff 3062 (re-search-forward regexp (pmail-msgend msg) t)))
e131541f
PR
3063
3064(defvar pmail-search-last-regexp nil)
3065(defun pmail-search (regexp &optional n)
3066 "Show message containing next match for REGEXP (but not the current msg).
3067Prefix argument gives repeat count; negative argument means search
3068backwards (through earlier messages).
3069Interactively, empty argument means use same regexp used last time."
3070 (interactive
3071 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
3072 (prompt
3073 (concat (if reversep "Reverse " "") "Pmail search (regexp"))
3074 regexp)
3075 (setq prompt
3076 (concat prompt
3077 (if pmail-search-last-regexp
3078 (concat ", default "
3079 pmail-search-last-regexp "): ")
3080 "): ")))
3081 (setq regexp (read-string prompt))
3082 (cond ((not (equal regexp ""))
3083 (setq pmail-search-last-regexp regexp))
3084 ((not pmail-search-last-regexp)
3085 (error "No previous Pmail search string")))
3086 (list pmail-search-last-regexp
3087 (prefix-numeric-value current-prefix-arg))))
3088 (or n (setq n 1))
3089 (message "%sPmail search for %s..."
3090 (if (< n 0) "Reverse " "")
3091 regexp)
3092 (set-buffer pmail-buffer)
61dcccff 3093 (pmail-maybe-set-message-counters)
e131541f
PR
3094 (let ((omin (point-min))
3095 (omax (point-max))
3096 (opoint (point))
61dcccff 3097 win
e131541f 3098 (reversep (< n 0))
61dcccff 3099 (msg pmail-current-message))
e131541f
PR
3100 (unwind-protect
3101 (progn
3102 (widen)
3103 (while (/= n 0)
3104 ;; Check messages one by one, advancing message number up or down
3105 ;; but searching forward through each message.
3106 (if reversep
3107 (while (and (null win) (> msg 1))
3108 (setq msg (1- msg)
3109 win (pmail-search-message msg regexp)))
3110 (while (and (null win) (< msg pmail-total-messages))
3111 (setq msg (1+ msg)
3112 win (pmail-search-message msg regexp))))
3113 (setq n (+ n (if reversep 1 -1)))))
3114 (if win
3115 (progn
3116 (pmail-show-message msg)
3117 ;; Search forward (if this is a normal search) or backward
3118 ;; (if this is a reverse search) through this message to
3119 ;; position point. This search may fail because REGEXP
3120 ;; was found in the hidden portion of this message. In
3121 ;; that case, move point to the beginning of visible
3122 ;; portion.
3123 (if reversep
3124 (progn
3125 (goto-char (point-max))
3126 (re-search-backward regexp nil 'move))
3127 (goto-char (point-min))
3128 (re-search-forward regexp nil t))
3129 (message "%sPmail search for %s...done"
3130 (if reversep "Reverse " "")
3131 regexp))
3132 (goto-char opoint)
3133 (narrow-to-region omin omax)
3134 (ding)
3135 (message "Search failed: %s" regexp)))))
3136
3137(defun pmail-search-backwards (regexp &optional n)
3138 "Show message containing previous match for REGEXP.
3139Prefix argument gives repeat count; negative argument means search
3140forward (through later messages).
3141Interactively, empty argument means use same regexp used last time."
3142 (interactive
3143 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
3144 (prompt
3145 (concat (if reversep "Reverse " "") "Pmail search (regexp"))
3146 regexp)
3147 (setq prompt
3148 (concat prompt
3149 (if pmail-search-last-regexp
3150 (concat ", default "
3151 pmail-search-last-regexp "): ")
3152 "): ")))
3153 (setq regexp (read-string prompt))
3154 (cond ((not (equal regexp ""))
3155 (setq pmail-search-last-regexp regexp))
3156 ((not pmail-search-last-regexp)
3157 (error "No previous Pmail search string")))
3158 (list pmail-search-last-regexp
3159 (prefix-numeric-value current-prefix-arg))))
3160 (pmail-search regexp (- (or n 1))))
3161
61dcccff 3162
e131541f 3163(defun pmail-first-unseen-message ()
61dcccff
PR
3164 "Return the message index for the first message which has the
3165`unseen' attribute."
3166 (pmail-maybe-set-message-counters)
e131541f
PR
3167 (let ((current 1)
3168 found)
61dcccff
PR
3169 (save-restriction
3170 (widen)
3171 (while (and (not found) (<= current pmail-total-messages))
3172 (if (pmail-message-attr-p current "......U")
3173 (setq found current))
3174 (setq current (1+ current))))
3175 found))
e131541f
PR
3176
3177(defun pmail-current-subject ()
3178 "Return the current subject.
3179The subject is stripped of leading and trailing whitespace, and
3180of typical reply prefixes such as Re:."
3181 (let ((subject (or (mail-fetch-field "Subject") "")))
3182 (if (string-match "\\`[ \t]+" subject)
3183 (setq subject (substring subject (match-end 0))))
3184 (if (string-match pmail-reply-regexp subject)
3185 (setq subject (substring subject (match-end 0))))
3186 (if (string-match "[ \t]+\\'" subject)
3187 (setq subject (substring subject 0 (match-beginning 0))))
3188 subject))
3189
3190(defun pmail-current-subject-regexp ()
3191 "Return a regular expression matching the current subject.
3192The regular expression matches the subject header line of
3193messages about the same subject. The subject itself is stripped
3194of leading and trailing whitespace, of typical reply prefixes
3195such as Re: and whitespace within the subject is replaced by a
3196regular expression matching whitespace in general in order to
3197take into account that subject header lines may include newlines
3198and more whitespace. The returned regular expressions contains
3199`pmail-reply-regexp' and ends with a newline."
3200 (let ((subject (pmail-current-subject)))
3201 ;; If Subject is long, mailers will break it into several lines at
3202 ;; arbitrary places, so replace whitespace with a regexp that will
3203 ;; match any sequence of spaces, TABs, and newlines.
3204 (setq subject (regexp-quote subject))
3205 (setq subject
3206 (replace-regexp-in-string "[ \t\n]+" "[ \t\n]+" subject t t))
3207 ;; Some mailers insert extra spaces after "Subject:", so allow any
3208 ;; amount of them.
3209 (concat "^Subject:[ \t]+"
3210 (if (string= "\\`" (substring pmail-reply-regexp 0 2))
3211 (substring pmail-reply-regexp 2)
3212 pmail-reply-regexp)
3213 subject "[ \t]*\n")))
3214
3215(defun pmail-next-same-subject (n)
3216 "Go to the next mail message having the same subject header.
3217With prefix argument N, do this N times.
3218If N is negative, go backwards instead."
3219 (interactive "p")
3220 (let ((search-regexp (pmail-current-subject-regexp))
3221 (forward (> n 0))
3222 (i pmail-current-message)
3223 (case-fold-search t)
3224 found)
3225 (save-excursion
3226 (save-restriction
3227 (widen)
61dcccff
PR
3228 (while (and (/= n 0)
3229 (if forward
3230 (< i pmail-total-messages)
3231 (> i 1)))
3232 (let (done)
3233 (while (and (not done)
3234 (if forward
3235 (< i pmail-total-messages)
3236 (> i 1)))
3237 (setq i (if forward (1+ i) (1- i)))
3238 (goto-char (pmail-msgbeg i))
3239 (search-forward "\n*** EOOH ***\n")
3240 (let ((beg (point)) end)
3241 (search-forward "\n\n")
3242 (setq end (point))
3243 (goto-char beg)
3244 (setq done (re-search-forward search-regexp end t))))
3245 (if done (setq found i)))
3246 (setq n (if forward (1- n) (1+ n))))))
e131541f
PR
3247 (if found
3248 (pmail-show-message found)
3249 (error "No %s message with same subject"
3250 (if forward "following" "previous")))))
3251
3252(defun pmail-previous-same-subject (n)
3253 "Go to the previous mail message having the same subject header.
3254With prefix argument N, do this N times.
3255If N is negative, go forwards instead."
3256 (interactive "p")
3257 (pmail-next-same-subject (- n)))
3258\f
3259;;;; *** Pmail Message Deletion Commands ***
3260
61dcccff
PR
3261(defun pmail-message-deleted-p (n)
3262 (= (aref pmail-deleted-vector n) ?D))
3263
3264(defun pmail-set-message-deleted-p (n state)
3265 (aset pmail-deleted-vector n (if state ?D ?\ )))
3266
e131541f
PR
3267(defun pmail-delete-message ()
3268 "Delete this message and stay on it."
3269 (interactive)
61dcccff
PR
3270 (pmail-set-attribute pmail-deleted-attr-index t)
3271 (run-hooks 'pmail-delete-message-hook))
e131541f
PR
3272
3273(defun pmail-undelete-previous-message ()
3274 "Back up to deleted message, select it, and undelete it."
3275 (interactive)
3276 (set-buffer pmail-buffer)
3277 (let ((msg pmail-current-message))
3278 (while (and (> msg 0)
61dcccff 3279 (not (pmail-message-deleted-p msg)))
e131541f
PR
3280 (setq msg (1- msg)))
3281 (if (= msg 0)
3282 (error "No previous deleted message")
61dcccff
PR
3283 (if (/= msg pmail-current-message)
3284 (pmail-show-message msg))
3285 (pmail-set-attribute pmail-deleted-attr-index nil)
e131541f
PR
3286 (if (pmail-summary-exists)
3287 (save-excursion
3288 (set-buffer pmail-summary-buffer)
3289 (pmail-summary-mark-undeleted msg)))
3290 (pmail-maybe-display-summary))))
3291
e131541f
PR
3292(defun pmail-delete-forward (&optional backward)
3293 "Delete this message and move to next nondeleted one.
3294Deleted messages stay in the file until the \\[pmail-expunge] command is given.
3295With prefix argument, delete and move backward.
3296
3297Returns t if a new message is displayed after the delete, or nil otherwise."
3298 (interactive "P")
61dcccff 3299 (pmail-set-attribute pmail-deleted-attr-index t)
e131541f
PR
3300 (run-hooks 'pmail-delete-message-hook)
3301 (let ((del-msg pmail-current-message))
3302 (if (pmail-summary-exists)
3303 (pmail-select-summary
3304 (pmail-summary-mark-deleted del-msg)))
3305 (prog1 (pmail-next-undeleted-message (if backward -1 1))
3306 (pmail-maybe-display-summary))))
3307
e131541f
PR
3308(defun pmail-delete-backward ()
3309 "Delete this message and move to previous nondeleted one.
3310Deleted messages stay in the file until the \\[pmail-expunge] command is given."
3311 (interactive)
3312 (pmail-delete-forward t))
3313
61dcccff
PR
3314;; Compute the message number a given message would have after expunging.
3315;; The present number of the message is OLDNUM.
3316;; DELETEDVEC should be pmail-deleted-vector.
3317;; The value is nil for a message that would be deleted.
3318(defun pmail-msg-number-after-expunge (deletedvec oldnum)
3319 (if (or (null oldnum) (= (aref deletedvec oldnum) ?D))
3320 nil
3321 (let ((i 0)
3322 (newnum 0))
3323 (while (< i oldnum)
3324 (if (/= (aref deletedvec i) ?D)
3325 (setq newnum (1+ newnum)))
3326 (setq i (1+ i)))
3327 newnum)))
3328
e131541f 3329(defun pmail-expunge-confirmed ()
61dcccff 3330 "Return t if deleted message should be expunged. If necessary, ask the user.
e131541f
PR
3331See also user-option `pmail-confirm-expunge'."
3332 (set-buffer pmail-buffer)
61dcccff
PR
3333 (or (not (stringp pmail-deleted-vector))
3334 (not (string-match "D" pmail-deleted-vector))
3335 (null pmail-confirm-expunge)
3336 (funcall pmail-confirm-expunge
3337 "Erase deleted messages from Pmail file? ")))
e131541f
PR
3338
3339(defun pmail-only-expunge (&optional dont-show)
3340 "Actually erase all deleted messages in the file."
3341 (interactive)
61dcccff 3342 (set-buffer pmail-buffer)
e131541f
PR
3343 (message "Expunging deleted messages...")
3344 ;; Discard all undo records for this buffer.
61dcccff
PR
3345 (or (eq buffer-undo-list t)
3346 (setq buffer-undo-list nil))
3347 (pmail-maybe-set-message-counters)
e131541f
PR
3348 (let* ((omax (- (buffer-size) (point-max)))
3349 (omin (- (buffer-size) (point-min)))
3350 (opoint (if (and (> pmail-current-message 0)
3351 (pmail-message-deleted-p pmail-current-message))
3352 0
3353 (if pmail-enable-mime
3354 (with-current-buffer pmail-view-buffer
3355 (- (point)(point-min)))
61dcccff
PR
3356 (- (point) (point-min)))))
3357 (messages-head (cons (aref pmail-message-vector 0) nil))
3358 (messages-tail messages-head)
3359 ;; Don't make any undo records for the expunging.
3360 (buffer-undo-list t)
3361 (win))
3362 (unwind-protect
3363 (save-excursion
3364 (widen)
3365 (goto-char (point-min))
3366 (let ((counter 0)
3367 (number 1)
3368 (total pmail-total-messages)
3369 (new-message-number pmail-current-message)
3370 (new-summary nil)
3371 (new-msgref (list (list 0)))
3372 (pmailbuf (current-buffer))
3373 (buffer-read-only nil)
3374 (messages pmail-message-vector)
3375 (deleted pmail-deleted-vector)
3376 (summary pmail-summary-vector))
3377 (setq pmail-total-messages nil
3378 pmail-current-message nil
3379 pmail-message-vector nil
3380 pmail-deleted-vector nil
3381 pmail-summary-vector nil)
3382
3383 (while (<= number total)
3384 (if (= (aref deleted number) ?D)
3385 (progn
3386 (delete-region
3387 (marker-position (aref messages number))
3388 (marker-position (aref messages (1+ number))))
3389 (move-marker (aref messages number) nil)
3390 (if (> new-message-number counter)
3391 (setq new-message-number (1- new-message-number))))
3392 (setq counter (1+ counter))
3393 (setq messages-tail
3394 (setcdr messages-tail
3395 (cons (aref messages number) nil)))
3396 (setq new-summary
3397 (cons (if (= counter number) (aref summary (1- number)))
3398 new-summary))
3399 (setq new-msgref
3400 (cons (aref pmail-msgref-vector number)
3401 new-msgref))
3402 (setcar (car new-msgref) counter))
3403 (if (zerop (% (setq number (1+ number)) 20))
3404 (message "Expunging deleted messages...%d" number)))
3405 (setq messages-tail
3406 (setcdr messages-tail
3407 (cons (aref messages number) nil)))
3408 (setq pmail-current-message new-message-number
3409 pmail-total-messages counter
3410 pmail-message-vector (apply 'vector messages-head)
3411 pmail-deleted-vector (make-string (1+ counter) ?\ )
3412 pmail-summary-vector (vconcat (nreverse new-summary))
3413 pmail-msgref-vector (apply 'vector (nreverse new-msgref))
3414 win t)))
3415 (message "Expunging deleted messages...done")
3416 (if (not win)
3417 (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
3418 (if (not dont-show)
3419 (pmail-show-message
3420 (if (zerop pmail-current-message) 1 nil)))
3421 (pmail-swap-buffers-maybe)
3422 (if pmail-enable-mime
3423 (goto-char (+ (point-min) opoint))
3424 (goto-char (+ (point) opoint))))))
3425
e131541f
PR
3426(defun pmail-expunge ()
3427 "Erase deleted messages from Pmail file and summary buffer."
3428 (interactive)
3429 (when (pmail-expunge-confirmed)
61dcccff
PR
3430 (pmail-only-expunge)
3431 (if (pmail-summary-exists)
3432 (pmail-select-summary (pmail-update-summary)))))
e131541f
PR
3433\f
3434;;;; *** Pmail Mailing Commands ***
3435
e131541f
PR
3436(defun pmail-start-mail (&optional noerase to subject in-reply-to cc
3437 replybuffer sendactions same-window others)
3438 (let (yank-action)
3439 (if replybuffer
3440 (setq yank-action (list 'insert-buffer replybuffer)))
3441 (setq others (cons (cons "cc" cc) others))
3442 (setq others (cons (cons "in-reply-to" in-reply-to) others))
3443 (if same-window
3444 (compose-mail to subject others
3445 noerase nil
3446 yank-action sendactions)
3447 (if pmail-mail-new-frame
3448 (prog1
3449 (compose-mail to subject others
3450 noerase 'switch-to-buffer-other-frame
3451 yank-action sendactions)
3452 ;; This is not a standard frame parameter;
3453 ;; nothing except sendmail.el looks at it.
3454 (modify-frame-parameters (selected-frame)
3455 '((mail-dedicated-frame . t))))
3456 (compose-mail to subject others
3457 noerase 'switch-to-buffer-other-window
3458 yank-action sendactions)))))
3459
3460(defun pmail-mail ()
3461 "Send mail in another window.
3462While composing the message, use \\[mail-yank-original] to yank the
3463original message into it."
3464 (interactive)
3465 (pmail-start-mail nil nil nil nil nil pmail-view-buffer))
3466
3467(defun pmail-continue ()
3468 "Continue composing outgoing message previously being composed."
3469 (interactive)
3470 (pmail-start-mail t))
3471
3472(defun pmail-reply (just-sender)
3473 "Reply to the current message.
3474Normally include CC: to all other recipients of original message;
3475prefix argument means ignore them. While composing the reply,
3476use \\[mail-yank-original] to yank the original message into it."
3477 (interactive "P")
61dcccff
PR
3478 (let (from reply-to cc subject date to message-id references
3479 resent-to resent-cc resent-reply-to
3480 (msgnum pmail-current-message))
3481 (save-excursion
3482 (save-restriction
3483 (if pmail-enable-mime
3484 (narrow-to-region
3485 (goto-char (point-min))
3486 (if (search-forward "\n\n" nil 'move)
3487 (1+ (match-beginning 0))
3488 (point)))
3489 (widen)
3490 (goto-char (pmail-msgbeg pmail-current-message))
3491 (forward-line 1)
3492 (if (= (following-char) ?0)
3493 (narrow-to-region
3494 (progn (forward-line 2)
3495 (point))
3496 (progn (search-forward "\n\n" (pmail-msgend pmail-current-message)
3497 'move)
3498 (point)))
3499 (narrow-to-region (point)
3500 (progn (search-forward "\n*** EOOH ***\n")
3501 (beginning-of-line) (point)))))
3502 (setq from (mail-fetch-field "from")
3503 reply-to (or (mail-fetch-field "mail-reply-to" nil t)
3504 (mail-fetch-field "reply-to" nil t)
3505 from)
3506 subject (mail-fetch-field "subject")
3507 date (mail-fetch-field "date")
3508 message-id (mail-fetch-field "message-id")
3509 references (mail-fetch-field "references" nil nil t)
3510 resent-reply-to (mail-fetch-field "resent-reply-to" nil t)
3511 resent-cc (and (not just-sender)
3512 (mail-fetch-field "resent-cc" nil t))
3513 resent-to (or (mail-fetch-field "resent-to" nil t) "")
3514;;; resent-subject (mail-fetch-field "resent-subject")
3515;;; resent-date (mail-fetch-field "resent-date")
3516;;; resent-message-id (mail-fetch-field "resent-message-id")
3517 )
3518 (unless just-sender
3519 (if (mail-fetch-field "mail-followup-to" nil t)
3520 ;; If this header field is present, use it instead of the To and CC fields.
3521 (setq to (mail-fetch-field "mail-followup-to" nil t))
3522 (setq cc (or (mail-fetch-field "cc" nil t) "")
3523 to (or (mail-fetch-field "to" nil t) ""))))
3524
3525 ))
3526
3527 ;; Merge the resent-to and resent-cc into the to and cc.
3528 (if (and resent-to (not (equal resent-to "")))
3529 (if (not (equal to ""))
3530 (setq to (concat to ", " resent-to))
3531 (setq to resent-to)))
3532 (if (and resent-cc (not (equal resent-cc "")))
3533 (if (not (equal cc ""))
3534 (setq cc (concat cc ", " resent-cc))
3535 (setq cc resent-cc)))
3536 ;; Add `Re: ' to subject if not there already.
3537 (and (stringp subject)
3538 (setq subject
3539 (concat pmail-reply-prefix
3540 (if (let ((case-fold-search t))
3541 (string-match pmail-reply-regexp subject))
3542 (substring subject (match-end 0))
3543 subject))))
3544 (pmail-start-mail
3545 nil
3546 ;; Using mail-strip-quoted-names is undesirable with newer mailers
3547 ;; since they can handle the names unstripped.
3548 ;; I don't know whether there are other mailers that still
3549 ;; need the names to be stripped.
e131541f 3550;;; (mail-strip-quoted-names reply-to)
61dcccff
PR
3551 ;; Remove unwanted names from reply-to, since Mail-Followup-To
3552 ;; header causes all the names in it to wind up in reply-to, not
3553 ;; in cc. But if what's left is an empty list, use the original.
3554 (let* ((reply-to-list (pmail-dont-reply-to reply-to)))
3555 (if (string= reply-to-list "") reply-to reply-to-list))
3556 subject
3557 (pmail-make-in-reply-to-field from date message-id)
3558 (if just-sender
3559 nil
3560 ;; mail-strip-quoted-names is NOT necessary for pmail-dont-reply-to
3561 ;; to do its job.
3562 (let* ((cc-list (pmail-dont-reply-to
3563 (mail-strip-quoted-names
3564 (if (null cc) to (concat to ", " cc))))))
3565 (if (string= cc-list "") nil cc-list)))
3566 pmail-view-buffer
3567 (list (list 'pmail-mark-message
3568 pmail-buffer
3569 (with-current-buffer pmail-buffer
3570 (aref pmail-msgref-vector msgnum))
3571 "answered"))
3572 nil
3573 (list (cons "References" (concat (mapconcat 'identity references " ")
3574 " " message-id))))))
3575
3576(defun pmail-mark-message (buffer msgnum-list attribute)
3577 "Give BUFFER's message number in MSGNUM-LIST the attribute ATTRIBUTE.
3578This is use in the send-actions for message buffers.
3579MSGNUM-LIST is a list of the form (MSGNUM)
3580which is an element of pmail-msgref-vector."
e131541f
PR
3581 (save-excursion
3582 (set-buffer buffer)
61dcccff
PR
3583 (if (car msgnum-list)
3584 (pmail-set-attribute attribute t (car msgnum-list)))))
e131541f
PR
3585
3586(defun pmail-make-in-reply-to-field (from date message-id)
3587 (cond ((not from)
3588 (if message-id
3589 message-id
3590 nil))
3591 (mail-use-rfc822
3592 (require 'rfc822)
3593 (let ((tem (car (rfc822-addresses from))))
3594 (if message-id
3595 (if (or (not tem)
3596 (string-match
3597 (regexp-quote (if (string-match "@[^@]*\\'" tem)
3598 (substring tem 0
3599 (match-beginning 0))
3600 tem))
3601 message-id))
3602 ;; missing From, or Message-ID is sufficiently informative
3603 message-id
3604 (concat message-id " (" tem ")"))
3605 ;; Copy TEM, discarding text properties.
3606 (setq tem (copy-sequence tem))
3607 (set-text-properties 0 (length tem) nil tem)
3608 (setq tem (copy-sequence tem))
3609 ;; Use prin1 to fake RFC822 quoting
3610 (let ((field (prin1-to-string tem)))
3611 (if date
3612 (concat field "'s message of " date)
3613 field)))))
3614 ((let* ((foo "[^][\000-\037()<>@,;:\\\" ]+")
3615 (bar "[^][\000-\037()<>@,;:\\\"]+"))
3616 ;; These strings both match all non-ASCII characters.
3617 (or (string-match (concat "\\`[ \t]*\\(" bar
3618 "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
3619 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
3620 from)
3621 (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
3622 bar "\\))[ \t]*\\'")
3623 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
3624 from)))
3625 (let ((start (match-beginning 1))
3626 (end (match-end 1)))
3627 ;; Trim whitespace which above regexp match allows
3628 (while (and (< start end)
3629 (memq (aref from start) '(?\t ?\ )))
3630 (setq start (1+ start)))
3631 (while (and (< start end)
3632 (memq (aref from (1- end)) '(?\t ?\ )))
3633 (setq end (1- end)))
3634 (let ((field (substring from start end)))
3635 (if date (setq field (concat "message from " field " on " date)))
3636 (if message-id
3637 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
3638 (concat message-id " (" field ")")
3639 field))))
3640 (t
3641 ;; If we can't kludge it simply, do it correctly
3642 (let ((mail-use-rfc822 t))
3643 (pmail-make-in-reply-to-field from date message-id)))))
3644\f
e131541f
PR
3645(defun pmail-forward (resend)
3646 "Forward the current message to another user.
3647With prefix argument, \"resend\" the message instead of forwarding it;
3648see the documentation of `pmail-resend'."
3649 (interactive "P")
e131541f
PR
3650 (if resend
3651 (call-interactively 'pmail-resend)
3652 (let ((forward-buffer pmail-buffer)
3653 (msgnum pmail-current-message)
3654 (subject (concat "["
3655 (let ((from (or (mail-fetch-field "From")
3656 (mail-fetch-field ">From"))))
3657 (if from
3658 (concat (mail-strip-quoted-names from) ": ")
3659 ""))
3660 (or (mail-fetch-field "Subject") "")
3661 "]")))
3662 (if (pmail-start-mail
3663 nil nil subject nil nil nil
3664 (list (list 'pmail-mark-message
3665 forward-buffer
3666 (with-current-buffer pmail-buffer
61dcccff 3667 (aref pmail-msgref-vector msgnum))
e131541f
PR
3668 "forwarded"))
3669 ;; If only one window, use it for the mail buffer.
3670 ;; Otherwise, use another window for the mail buffer
3671 ;; so that the Pmail buffer remains visible
3672 ;; and sending the mail will get back to it.
3673 (and (not pmail-mail-new-frame) (one-window-p t)))
3674 ;; The mail buffer is now current.
3675 (save-excursion
3676 ;; Insert after header separator--before signature if any.
3677 (goto-char (mail-text-start))
3678 (if (or pmail-enable-mime pmail-enable-mime-composing)
3679 (funcall pmail-insert-mime-forwarded-message-function
3680 forward-buffer)
3681 (insert "------- Start of forwarded message -------\n")
3682 ;; Quote lines with `- ' if they start with `-'.
3683 (let ((beg (point)) end)
3684 (setq end (point-marker))
3685 (set-marker-insertion-type end t)
3686 (insert-buffer-substring forward-buffer)
3687 (goto-char beg)
3688 (while (re-search-forward "^-" end t)
3689 (beginning-of-line)
3690 (insert "- ")
3691 (forward-line 1))
3692 (goto-char end)
3693 (skip-chars-backward "\n")
3694 (if (< (point) end)
3695 (forward-char 1))
3696 (delete-region (point) end)
3697 (set-marker end nil))
3698 (insert "------- End of forwarded message -------\n"))
3699 (push-mark))))))
3700\f
3701(defun pmail-resend (address &optional from comment mail-alias-file)
3702 "Resend current message to ADDRESSES.
3703ADDRESSES should be a single address, a string consisting of several
3704addresses separated by commas, or a list of addresses.
3705
3706Optional FROM is the address to resend the message from, and
3707defaults from the value of `user-mail-address'.
3708Optional COMMENT is a string to insert as a comment in the resent message.
3709Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
3710typically for purposes of moderating a list."
3711 (interactive "sResend to: ")
e131541f
PR
3712 (require 'sendmail)
3713 (require 'mailalias)
3714 (unless (or (eq pmail-view-buffer (current-buffer))
3715 (eq pmail-buffer (current-buffer)))
3716 (error "Not an Pmail buffer"))
3717 (if (not from) (setq from user-mail-address))
3718 (let ((tembuf (generate-new-buffer " sendmail temp"))
3719 (case-fold-search nil)
3720 (mail-personal-alias-file
3721 (or mail-alias-file mail-personal-alias-file))
3722 (mailbuf pmail-buffer))
3723 (unwind-protect
3724 (with-current-buffer tembuf
3725 ;;>> Copy message into temp buffer
3726 (if pmail-enable-mime
3727 (funcall pmail-insert-mime-resent-message-function mailbuf)
3728 (insert-buffer-substring mailbuf))
3729 (goto-char (point-min))
3730 ;; Delete any Sender field, since that's not specifiable.
3731 ; Only delete Sender fields in the actual header.
3732 (re-search-forward "^$" nil 'move)
3733 ; Using "while" here rather than "if" because some buggy mail
3734 ; software may have inserted multiple Sender fields.
3735 (while (re-search-backward "^Sender:" nil t)
3736 (let (beg)
3737 (setq beg (point))
3738 (forward-line 1)
3739 (while (looking-at "[ \t]")
3740 (forward-line 1))
3741 (delete-region beg (point))))
3742 ; Go back to the beginning of the buffer so the Resent- fields
3743 ; are inserted there.
3744 (goto-char (point-min))
3745 ;;>> Insert resent-from:
3746 (insert "Resent-From: " from "\n")
3747 (insert "Resent-Date: " (mail-rfc822-date) "\n")
3748 ;;>> Insert resent-to: and bcc if need be.
3749 (let ((before (point)))
3750 (if mail-self-blind
3751 (insert "Resent-Bcc: " (user-login-name) "\n"))
3752 (insert "Resent-To: " (if (stringp address)
3753 address
3754 (mapconcat 'identity address ",\n\t"))
3755 "\n")
3756 ;; Expand abbrevs in the recipients.
3757 (save-excursion
3758 (if (featurep 'mailabbrev)
3759 (let ((end (point-marker))
3760 (local-abbrev-table mail-abbrevs)
3761 (old-syntax-table (syntax-table)))
3762 (if (and (not (vectorp mail-abbrevs))
3763 (file-exists-p mail-personal-alias-file))
3764 (build-mail-abbrevs))
3765 (unless mail-abbrev-syntax-table
3766 (mail-abbrev-make-syntax-table))
3767 (set-syntax-table mail-abbrev-syntax-table)
3768 (goto-char before)
3769 (while (and (< (point) end)
3770 (progn (forward-word 1)
3771 (<= (point) end)))
3772 (expand-abbrev))
3773 (set-syntax-table old-syntax-table))
3774 (expand-mail-aliases before (point)))))
3775 ;;>> Set up comment, if any.
3776 (if (and (sequencep comment) (not (zerop (length comment))))
3777 (let ((before (point))
3778 after)
3779 (insert comment)
3780 (or (eolp) (insert "\n"))
3781 (setq after (point))
3782 (goto-char before)
3783 (while (< (point) after)
3784 (insert "Resent-Comment: ")
3785 (forward-line 1))))
3786 ;; Don't expand aliases in the destination fields
3787 ;; of the original message.
3788 (let (mail-aliases)
3789 (funcall send-mail-function)))
3790 (kill-buffer tembuf))
3791 (with-current-buffer pmail-buffer
61dcccff 3792 (pmail-set-attribute pmail-resent-attr-index t pmail-current-message))))
e131541f
PR
3793\f
3794(defvar mail-unsent-separator
3795 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
3796 "^ *---+ +Returned message +---+ *$\\|"
3797 "^ *---+ *Returned mail follows *---+ *$\\|"
3798 "^Start of returned message$\\|"
3799 "^---+ Below this line is a copy of the message.$\\|"
3800 "^ *---+ +Original message +---+ *$\\|"
3801 "^ *--+ +begin message +--+ *$\\|"
3802 "^ *---+ +Original message follows +---+ *$\\|"
3803 "^ *---+ +Your message follows +---+ *$\\|"
3804 "^|? *---+ +Message text follows: +---+ *|?$\\|"
3805 "^ *---+ +This is a copy of \\w+ message, including all the headers.*---+ *$")
3806 "A regexp that matches the separator before the text of a failed message.")
3807
3808(defvar mail-mime-unsent-header "^Content-Type: message/rfc822 *$"
3809 "A regexp that matches the header of a MIME body part with a failed message.")
3810
3811(defun pmail-retry-failure ()
3812 "Edit a mail message which is based on the contents of the current message.
3813For a message rejected by the mail system, extract the interesting headers and
3814the body of the original message.
3815If the failed message is a MIME multipart message, it is searched for a
3816body part with a header which matches the variable `mail-mime-unsent-header'.
3817Otherwise, the variable `mail-unsent-separator' should match the string that
3818delimits the returned original message.
3819The variable `pmail-retry-ignored-headers' is a regular expression
3820specifying headers which should not be copied into the new message."
3821 (interactive)
e131541f
PR
3822 (require 'mail-utils)
3823 (let ((pmail-this-buffer (current-buffer))
3824 (msgnum pmail-current-message)
3825 bounce-start bounce-end bounce-indent resending
61dcccff
PR
3826 ;; Fetch any content-type header in current message
3827 ;; Must search thru the whole unpruned header.
e131541f
PR
3828 (content-type
3829 (save-excursion
3830 (save-restriction
61dcccff 3831 (mail-fetch-field "Content-Type") ))))
e131541f
PR
3832 (save-excursion
3833 (goto-char (point-min))
3834 (let ((case-fold-search t))
3835 (if (and content-type
3836 (string-match
3837 ";[\n\t ]*boundary=\"?\\([-0-9a-z'()+_,./:=? ]+\\)\"?"
3838 content-type))
3839 ;; Handle a MIME multipart bounce message.
3840 (let ((codestring
3841 (concat "\n--"
3842 (substring content-type (match-beginning 1)
3843 (match-end 1)))))
3844 (unless (re-search-forward mail-mime-unsent-header nil t)
3845 (error "Cannot find beginning of header in failed message"))
3846 (unless (search-forward "\n\n" nil t)
3847 (error "Cannot find start of Mime data in failed message"))
3848 (setq bounce-start (point))
3849 (if (search-forward codestring nil t)
3850 (setq bounce-end (match-beginning 0))
3851 (setq bounce-end (point-max))))
3852 ;; Non-MIME bounce.
3853 (or (re-search-forward mail-unsent-separator nil t)
3854 (error "Cannot parse this as a failure message"))
3855 (skip-chars-forward "\n")
3856 ;; Support a style of failure message in which the original
3857 ;; message is indented, and included within lines saying
3858 ;; `Start of returned message' and `End of returned message'.
3859 (if (looking-at " +Received:")
3860 (progn
3861 (setq bounce-start (point))
3862 (skip-chars-forward " ")
3863 (setq bounce-indent (- (current-column)))
3864 (goto-char (point-max))
3865 (re-search-backward "^End of returned message$" nil t)
3866 (setq bounce-end (point)))
3867 ;; One message contained a few random lines before
3868 ;; the old message header. The first line of the
3869 ;; message started with two hyphens. A blank line
3870 ;; followed these random lines. The same line
3871 ;; beginning with two hyphens was possibly marking
3872 ;; the end of the message.
3873 (if (looking-at "^--")
3874 (let ((boundary (buffer-substring-no-properties
3875 (point)
3876 (progn (end-of-line) (point)))))
3877 (search-forward "\n\n")
3878 (skip-chars-forward "\n")
3879 (setq bounce-start (point))
3880 (goto-char (point-max))
3881 (search-backward (concat "\n\n" boundary) bounce-start t)
3882 (setq bounce-end (point)))
3883 (setq bounce-start (point)
3884 bounce-end (point-max)))
3885 (unless (search-forward "\n\n" nil t)
3886 (error "Cannot find end of header in failed message"))))))
3887 ;; We have found the message that bounced, within the current message.
3888 ;; Now start sending new message; default header fields from original.
3889 ;; Turn off the usual actions for initializing the message body
3890 ;; because we want to get only the text from the failure message.
e131541f
PR
3891 (let (mail-signature mail-setup-hook)
3892 (if (pmail-start-mail nil nil nil nil nil pmail-this-buffer
3893 (list (list 'pmail-mark-message
3894 pmail-this-buffer
61dcccff 3895 (aref pmail-msgref-vector msgnum)
e131541f
PR
3896 "retried")))
3897 ;; Insert original text as initial text of new draft message.
3898 ;; Bind inhibit-read-only since the header delimiter
3899 ;; of the previous message was probably read-only.
3900 (let ((inhibit-read-only t)
3901 pmail-displayed-headers
3902 pmail-ignored-headers)
3903 (erase-buffer)
3904 (insert-buffer-substring pmail-this-buffer
3905 bounce-start bounce-end)
3906 (goto-char (point-min))
3907 (if bounce-indent
3908 (indent-rigidly (point-min) (point-max) bounce-indent))
3909 (pmail-clear-headers pmail-retry-ignored-headers)
3910 (pmail-clear-headers "^sender:\\|^return-path:\\|^received:")
3911 (mail-sendmail-delimit-header)
3912 (save-restriction
3913 (narrow-to-region (point-min) (mail-header-end))
3914 (setq resending (mail-fetch-field "resent-to"))
3915 (if mail-self-blind
3916 (if resending
3917 (insert "Resent-Bcc: " (user-login-name) "\n")
3918 (insert "BCC: " (user-login-name) "\n"))))
3919 (goto-char (point-min))
3920 (mail-position-on-field (if resending "Resent-To" "To") t))))))
3921\f
3922(defun pmail-summary-exists ()
3923 "Non-nil if in an PMAIL buffer and an associated summary buffer exists.
3924In fact, the non-nil value returned is the summary buffer itself."
3925 (and pmail-summary-buffer (buffer-name pmail-summary-buffer)
3926 pmail-summary-buffer))
3927
3928(defun pmail-summary-displayed ()
3929 "t if in PMAIL buffer and an associated summary buffer is displayed."
3930 (and pmail-summary-buffer (get-buffer-window pmail-summary-buffer)))
3931
3932(defcustom pmail-redisplay-summary nil
3933 "*Non-nil means Pmail should show the summary when it changes.
3934This has an effect only if a summary buffer exists."
3935 :type 'boolean
3936 :group 'pmail-summary)
3937
3938(defcustom pmail-summary-window-size nil
3939 "*Non-nil means specify the height for an Pmail summary window."
3940 :type '(choice (const :tag "Disabled" nil) integer)
3941 :group 'pmail-summary)
3942
3943;; Put the summary buffer back on the screen, if user wants that.
3944(defun pmail-maybe-display-summary ()
3945 (let ((selected (selected-window))
3946 window)
3947 ;; If requested, make sure the summary is displayed.
3948 (and pmail-summary-buffer (buffer-name pmail-summary-buffer)
3949 pmail-redisplay-summary
3950 (if (get-buffer-window pmail-summary-buffer 0)
3951 ;; It's already in some frame; show that one.
3952 (let ((frame (window-frame
3953 (get-buffer-window pmail-summary-buffer 0))))
3954 (make-frame-visible frame)
3955 (raise-frame frame))
3956 (display-buffer pmail-summary-buffer)))
3957 ;; If requested, set the height of the summary window.
3958 (and pmail-summary-buffer (buffer-name pmail-summary-buffer)
3959 pmail-summary-window-size
3960 (setq window (get-buffer-window pmail-summary-buffer))
3961 ;; Don't try to change the size if just one window in frame.
3962 (not (eq window (frame-root-window (window-frame window))))
3963 (unwind-protect
3964 (progn
3965 (select-window window)
3966 (enlarge-window (- pmail-summary-window-size (window-height))))
3967 (select-window selected)))))
3968\f
3969;;;; *** Pmail Local Fontification ***
3970
3971(defun pmail-fontify-buffer-function ()
3972 ;; This function's symbol is bound to font-lock-fontify-buffer-function.
3973 (add-hook 'pmail-show-message-hook 'pmail-fontify-message nil t)
3974 ;; If we're already showing a message, fontify it now.
3975 (if pmail-current-message (pmail-fontify-message))
3976 ;; Prevent Font Lock mode from kicking in.
3977 (setq font-lock-fontified t))
3978
3979(defun pmail-unfontify-buffer-function ()
3980 ;; This function's symbol is bound to font-lock-fontify-unbuffer-function.
3981 (let ((modified (buffer-modified-p))
3982 (buffer-undo-list t) (inhibit-read-only t)
3983 before-change-functions after-change-functions
3984 buffer-file-name buffer-file-truename)
3985 (save-restriction
3986 (widen)
3987 (remove-hook 'pmail-show-message-hook 'pmail-fontify-message t)
3988 (remove-text-properties (point-min) (point-max) '(pmail-fontified nil))
3989 (font-lock-default-unfontify-buffer)
3990 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))
3991
3992(defun pmail-fontify-message ()
61dcccff
PR
3993 ;; Fontify the current message if it is not already fontified.
3994 (if (text-property-any (point-min) (point-max) 'pmail-fontified nil)
3995 (let ((modified (buffer-modified-p))
3996 (buffer-undo-list t) (inhibit-read-only t)
3997 before-change-functions after-change-functions
3998 buffer-file-name buffer-file-truename)
3999 (save-excursion
4000 (save-match-data
4001 (add-text-properties (point-min) (point-max) '(pmail-fontified t))
4002 (font-lock-fontify-region (point-min) (point-max))
4003 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))))
e131541f
PR
4004\f
4005;;; Speedbar support for PMAIL files.
4006(eval-when-compile (require 'speedbar))
4007
4008(defvar pmail-speedbar-match-folder-regexp "^[A-Z0-9]+\\(\\.[A-Z0-9]+\\)?$"
4009 "*This regex is used to match folder names to be displayed in speedbar.
4010Enabling this will permit speedbar to display your folders for easy
4011browsing, and moving of messages.")
4012
4013(defvar pmail-speedbar-last-user nil
4014 "The last user to be displayed in the speedbar.")
4015
4016(defvar pmail-speedbar-key-map nil
4017 "Keymap used when in pmail display mode.")
4018
4019(defun pmail-install-speedbar-variables ()
4020 "Install those variables used by speedbar to enhance pmail."
4021 (if pmail-speedbar-key-map
4022 nil
4023 (setq pmail-speedbar-key-map (speedbar-make-specialized-keymap))
4024
4025 (define-key pmail-speedbar-key-map "e" 'speedbar-edit-line)
4026 (define-key pmail-speedbar-key-map "r" 'speedbar-edit-line)
4027 (define-key pmail-speedbar-key-map "\C-m" 'speedbar-edit-line)
4028 (define-key pmail-speedbar-key-map "M"
4029 'pmail-speedbar-move-message-to-folder-on-line)))
4030
4031(defvar pmail-speedbar-menu-items
4032 '(["Read Folder" speedbar-edit-line t]
4033 ["Move message to folder" pmail-speedbar-move-message-to-folder-on-line
4034 (save-excursion (beginning-of-line)
4035 (looking-at "<M> "))])
4036 "Additional menu-items to add to speedbar frame.")
4037
4038;; Make sure our special speedbar major mode is loaded
4039(if (featurep 'speedbar)
4040 (pmail-install-speedbar-variables)
4041 (add-hook 'speedbar-load-hook 'pmail-install-speedbar-variables))
4042
4043(defun pmail-speedbar-buttons (buffer)
4044 "Create buttons for BUFFER containing pmail messages.
4045Click on the address under Reply to: to reply to this person.
4046Under Folders: Click a name to read it, or on the <M> to move the
4047current message into that PMAIL folder."
4048 (let ((from nil))
4049 (save-excursion
4050 (set-buffer buffer)
4051 (goto-char (point-min))
4052 (if (not (re-search-forward "^Reply-To: " nil t))
4053 (if (not (re-search-forward "^From:? " nil t))
4054 (setq from t)))
4055 (if from
4056 nil
4057 (setq from (buffer-substring (point) (save-excursion
4058 (end-of-line)
4059 (point))))))
4060 (goto-char (point-min))
4061 (if (and (looking-at "Reply to:")
4062 (equal from pmail-speedbar-last-user))
4063 nil
4064 (setq pmail-speedbar-last-user from)
4065 (erase-buffer)
4066 (insert "Reply To:\n")
4067 (if (stringp from)
4068 (speedbar-insert-button from 'speedbar-directory-face 'highlight
4069 'pmail-speedbar-button 'pmail-reply))
4070 (insert "Folders:\n")
4071 (let* ((case-fold-search nil)
4072 (df (directory-files (save-excursion (set-buffer buffer)
4073 default-directory)
4074 nil pmail-speedbar-match-folder-regexp)))
4075 (while df
4076 (speedbar-insert-button "<M>" 'speedbar-button-face 'highlight
4077 'pmail-speedbar-move-message (car df))
4078 (speedbar-insert-button (car df) 'speedbar-file-face 'highlight
4079 'pmail-speedbar-find-file nil t)
4080 (setq df (cdr df)))))))
4081
4082(defun pmail-speedbar-button (text token indent)
4083 "Execute an pmail command specified by TEXT.
4084The command used is TOKEN. INDENT is not used."
4085 (speedbar-with-attached-buffer
4086 (funcall token t)))
4087
4088(defun pmail-speedbar-find-file (text token indent)
4089 "Load in the pmail file TEXT.
4090TOKEN and INDENT are not used."
4091 (speedbar-with-attached-buffer
4092 (message "Loading in PMAIL file %s..." text)
4093 (find-file text)))
4094
4095(defun pmail-speedbar-move-message-to-folder-on-line ()
4096 "If the current line is a folder, move current message to it."
4097 (interactive)
4098 (save-excursion
4099 (beginning-of-line)
4100 (if (re-search-forward "<M> " (save-excursion (end-of-line) (point)) t)
4101 (progn
4102 (forward-char -2)
4103 (speedbar-do-function-pointer)))))
4104
4105(defun pmail-speedbar-move-message (text token indent)
4106 "From button TEXT, copy current message to the pmail file specified by TOKEN.
4107TEXT and INDENT are not used."
4108 (speedbar-with-attached-buffer
4109 (message "Moving message to %s" token)
4110 (pmail-output-to-pmail-file token)))
4111
4112; Functions for setting, getting and encoding the POP password.
4113; The password is encoded to prevent it from being easily accessible
4114; to "prying eyes." Obviously, this encoding isn't "real security,"
4115; nor is it meant to be.
4116
4117;;;###autoload
4118(defun pmail-set-remote-password (password)
4119 "Set PASSWORD to be used for retrieving mail from a POP or IMAP server."
4120 (interactive "sPassword: ")
4121 (if password
4122 (setq pmail-encoded-remote-password
4123 (pmail-encode-string password (emacs-pid)))
4124 (setq pmail-remote-password nil)
4125 (setq pmail-encoded-remote-password nil)))
4126
4127(defun pmail-get-remote-password (imap)
4128 "Get the password for retrieving mail from a POP or IMAP server. If none
4129has been set, then prompt the user for one."
4130 (when (not pmail-encoded-remote-password)
4131 (if (not pmail-remote-password)
4132 (setq pmail-remote-password
4133 (read-passwd (if imap
4134 "IMAP password: "
4135 "POP password: "))))
4136 (pmail-set-remote-password pmail-remote-password)
4137 (setq pmail-remote-password nil))
4138 (pmail-encode-string pmail-encoded-remote-password (emacs-pid)))
4139
4140(defun pmail-have-password ()
4141 (or pmail-remote-password pmail-encoded-remote-password))
4142
4143(defun pmail-encode-string (string mask)
4144 "Encode STRING with integer MASK, by taking the exclusive OR of the
4145lowest byte in the mask with the first character of string, the
4146second-lowest-byte with the second character of the string, etc.,
4147restarting at the lowest byte of the mask whenever it runs out.
4148Returns the encoded string. Calling the function again with an
4149encoded string (and the same mask) will decode the string."
4150 (setq mask (abs mask)) ; doesn't work if negative
4151 (let* ((string-vector (string-to-vector string)) (i 0)
4152 (len (length string-vector)) (curmask mask) charmask)
4153 (while (< i len)
4154 (if (= curmask 0)
4155 (setq curmask mask))
4156 (setq charmask (% curmask 256))
4157 (setq curmask (lsh curmask -8))
4158 (aset string-vector i (logxor charmask (aref string-vector i)))
4159 (setq i (1+ i)))
4160 (concat string-vector)))
4161
4162;;;; Desktop support
4163
4164(defun pmail-restore-desktop-buffer (desktop-buffer-file-name
4165 desktop-buffer-name
4166 desktop-buffer-misc)
4167 "Restore an pmail buffer specified in a desktop file."
4168 (condition-case error
4169 (progn
4170 (pmail-input desktop-buffer-file-name)
4171 (if (eq major-mode 'pmail-mode)
4172 (current-buffer)
4173 pmail-buffer))
4174 (file-locked
4175 (kill-buffer (current-buffer))
4176 nil)))
4177
4178(add-to-list 'desktop-buffer-mode-handlers
4179 '(pmail-mode . pmail-restore-desktop-buffer))
4180
61dcccff 4181
e131541f
PR
4182(provide 'pmail)
4183
51a5d095
GM
4184;; Local Variables:
4185;; change-log-default-name: "ChangeLog.pmail"
4186;; End:
4187
0faeefbb 4188;; arch-tag: 65d257d3-c281-4a65-9c38-e61af95af2f0
e131541f 4189;;; pmail.el ends here