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