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