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