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