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