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