Merge branch 'master' into comment-cache
[emacs.git] / lisp / mail / rmail.el
blob010d8e3ad14a31b39a0589d6eaa669ca9c755515
1 ;;; rmail.el --- main code of "RMAIL" mail reader for Emacs -*- lexical-binding:t -*-
3 ;; Copyright (C) 1985-1988, 1993-1998, 2000-2017 Free Software
4 ;; Foundation, Inc.
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: mail
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;;; Code:
28 ;; Souped up by shane@mit-ajax based on ideas of rlk@athena.mit.edu
29 ;; New features include attribute and keyword support, message
30 ;; selection by dispatch table, summary by attributes and keywords,
31 ;; expunging by dispatch table, sticky options for file commands.
33 ;; Extended by Bob Weiner of Motorola
34 ;; New features include: rmail and rmail-summary buffers remain
35 ;; synchronized and key bindings basically operate the same way in both
36 ;; buffers, summary by topic or by regular expression, rmail-reply-prefix
37 ;; variable, and a bury rmail buffer (wipe) command.
40 (require 'mail-utils)
41 (require 'rfc2047)
43 (require 'rmail-loaddefs)
45 (declare-function compilation--message->loc "compile" (cl-x) t)
46 (declare-function epa--find-coding-system-for-mime-charset "epa" (mime-charset))
48 (defconst rmail-attribute-header "X-RMAIL-ATTRIBUTES"
49 "The header that stores the Rmail attribute data.")
51 (defconst rmail-keyword-header "X-RMAIL-KEYWORDS"
52 "The header that stores the Rmail keyword data.")
54 ;;; Attribute indexes
56 (defconst rmail-answered-attr-index 0
57 "The index for the `answered' attribute.")
59 (defconst rmail-deleted-attr-index 1
60 "The index for the `deleted' attribute.")
62 (defconst rmail-edited-attr-index 2
63 "The index for the `edited' attribute.")
65 (defconst rmail-filed-attr-index 3
66 "The index for the `filed' attribute.")
68 (defconst rmail-retried-attr-index 4
69 "The index for the `retried' attribute.")
71 (defconst rmail-forwarded-attr-index 5
72 "The index for the `forwarded' attribute.")
74 (defconst rmail-unseen-attr-index 6
75 "The index for the `unseen' attribute.")
77 (defconst rmail-resent-attr-index 7
78 "The index for the `resent' attribute.")
80 (defconst rmail-attr-array
81 '[(?A "answered")
82 (?D "deleted")
83 (?E "edited")
84 (?F "filed")
85 (?R "retried")
86 (?S "forwarded")
87 (?U "unseen")
88 (?r "resent")]
89 "An array that provides a mapping between an attribute index,
90 its character representation and its display representation.")
92 (defvar deleted-head)
93 (defvar font-lock-fontified)
94 (defvar mail-abbrev-syntax-table)
95 (defvar mail-abbrevs)
96 (defvar messages-head)
97 (defvar total-messages)
98 (defvar tool-bar-map)
99 (defvar mail-encode-mml)
101 (defvar rmail-header-style 'normal
102 "The current header display style choice, one of
103 `normal' (selected headers) or `full' (all headers).")
105 (defvar rmail-mime-decoded nil
106 "Non-nil if message has been processed by `rmail-show-mime-function'.")
107 (put 'rmail-mime-decoded 'permanent-local t) ; for rmail-edit
109 (defsubst rmail-mime-message-p ()
110 "Non-nil if and only if the current message is a MIME."
111 (or (get-text-property (point) 'rmail-mime-entity)
112 (get-text-property (point-min) 'rmail-mime-entity)))
114 (defgroup rmail nil
115 "Mail reader for Emacs."
116 :group 'mail)
118 (defgroup rmail-retrieve nil
119 "Rmail retrieval options."
120 :prefix "rmail-"
121 :group 'rmail)
123 (defgroup rmail-files nil
124 "Rmail files."
125 :prefix "rmail-"
126 :group 'rmail)
128 (defgroup rmail-headers nil
129 "Rmail header options."
130 :prefix "rmail-"
131 :group 'rmail)
133 (defgroup rmail-reply nil
134 "Rmail reply options."
135 :prefix "rmail-"
136 :group 'rmail)
138 (defgroup rmail-summary nil
139 "Rmail summary options."
140 :prefix "rmail-"
141 :prefix "rmail-summary-"
142 :group 'rmail)
144 (defgroup rmail-output nil
145 "Output message to a file."
146 :prefix "rmail-output-"
147 :prefix "rmail-"
148 :group 'rmail)
150 (defgroup rmail-edit nil
151 "Rmail editing."
152 :prefix "rmail-edit-"
153 :group 'rmail)
155 ;;;###autoload
156 (defcustom rmail-file-name (purecopy "~/RMAIL")
157 "Name of user's primary mail file."
158 :type 'string
159 :group 'rmail
160 :version "21.1")
162 ;;;###autoload
163 (put 'rmail-spool-directory 'standard-value
164 '((cond ((file-exists-p "/var/mail") "/var/mail/")
165 ((file-exists-p "/var/spool/mail") "/var/spool/mail/")
166 ((memq system-type '(hpux usg-unix-v)) "/usr/mail/")
167 (t "/usr/spool/mail/"))))
169 ;;;###autoload
170 (defcustom rmail-spool-directory
171 (purecopy
172 (cond ((file-exists-p "/var/mail")
173 ;; SVR4 and recent BSD are said to use this.
174 ;; Rather than trying to know precisely which systems use it,
175 ;; let's assume this dir is never used for anything else.
176 "/var/mail/")
177 ;; Many GNU/Linux systems use this name.
178 ((file-exists-p "/var/spool/mail") "/var/spool/mail/")
179 ((memq system-type '(hpux usg-unix-v)) "/usr/mail/")
180 (t "/usr/spool/mail/")))
181 "Name of directory used by system mailer for delivering new mail.
182 Its name should end with a slash."
183 :initialize 'custom-initialize-delay
184 :type 'directory
185 :group 'rmail)
187 ;;;###autoload(custom-initialize-delay 'rmail-spool-directory nil)
189 (defcustom rmail-movemail-program nil
190 "If non-nil, the file name of the `movemail' program."
191 :group 'rmail-retrieve
192 :type '(choice (const nil) string))
194 (define-obsolete-variable-alias 'rmail-pop-password
195 'rmail-remote-password "22.1")
197 (defcustom rmail-remote-password nil
198 "Password to use when reading mail from a remote server.
199 This setting is ignored for mailboxes whose URL already contains a password."
200 :type '(choice (string :tag "Password")
201 (const :tag "Not Required" nil))
202 :group 'rmail-retrieve
203 :version "22.1")
205 (define-obsolete-variable-alias 'rmail-pop-password-required
206 'rmail-remote-password-required "22.1")
208 (defcustom rmail-remote-password-required nil
209 "Non-nil if a password is required when reading mail from a remote server."
210 :type 'boolean
211 :group 'rmail-retrieve
212 :version "22.1")
214 (defcustom rmail-movemail-flags nil
215 "List of flags to pass to movemail.
216 Most commonly used to specify `-g' to enable GSS-API authentication
217 or `-k' to enable Kerberos authentication."
218 :type '(repeat string)
219 :group 'rmail-retrieve
220 :version "20.3")
222 (defvar rmail-remote-password-error "invalid usercode or password\\|
223 unknown user name or bad password\\|Authentication failed\\|MU_ERR_AUTH_FAILURE"
224 "Regular expression matching incorrect-password POP or IMAP server error
225 messages.
226 If you get an incorrect-password error that this expression does not match,
227 please report it with \\[report-emacs-bug].")
229 (defvar rmail-encoded-remote-password nil)
231 (defcustom rmail-preserve-inbox nil
232 "Non-nil means leave incoming mail in the user's inbox--don't delete it."
233 :type 'boolean
234 :group 'rmail-retrieve)
236 (defcustom rmail-movemail-search-path nil
237 "List of directories to search for movemail (in addition to `exec-path')."
238 :group 'rmail-retrieve
239 :type '(repeat (directory)))
241 (declare-function mail-dont-reply-to "mail-utils" (destinations))
242 (declare-function rmail-update-summary "rmailsum" (&rest ignore))
243 (declare-function rmail-mime-toggle-hidden "rmailmm" ())
244 (declare-function rmail-mime-entity-truncated "rmailmm" (entity))
246 (defun rmail-probe (prog)
247 "Determine what flavor of movemail PROG is.
248 We do this by executing it with `--version' and analyzing its output."
249 (with-temp-buffer
250 (let ((tbuf (current-buffer)))
251 (buffer-disable-undo tbuf)
252 (call-process prog nil tbuf nil "--version")
253 (if (not (buffer-modified-p tbuf))
254 ;; Should not happen...
256 (goto-char (point-min))
257 (cond
258 ((looking-at ".*movemail: invalid option")
259 'emacs) ;; Possibly...
260 ((looking-at "movemail (GNU Mailutils")
261 'mailutils)
263 ;; FIXME:
264 'emacs))))))
266 (defun rmail-autodetect ()
267 "Determine the file name of the `movemail' program and return its flavor.
268 If `rmail-movemail-program' is non-nil, use it.
269 Otherwise, look for `movemail' in the directories in
270 `rmail-movemail-search-path', those in `exec-path', and `exec-directory'."
271 (if rmail-movemail-program
272 (rmail-probe rmail-movemail-program)
273 (catch 'scan
274 (dolist (dir (append rmail-movemail-search-path exec-path
275 (list exec-directory)))
276 (when (and dir (file-accessible-directory-p dir))
277 ;; Previously, this didn't have to work on Windows, because
278 ;; rmail-insert-inbox-text before r1.439 fell back to using
279 ;; (expand-file-name "movemail" exec-directory) and just
280 ;; assuming it would work.
281 ;; http://lists.gnu.org/archive/html/bug-gnu-emacs/2008-02/msg00087.html
282 (let ((progname (expand-file-name
283 (concat "movemail"
284 (if (memq system-type '(ms-dos windows-nt))
285 ".exe")) dir)))
286 (when (and (not (file-directory-p progname))
287 (file-executable-p progname))
288 (let ((x (rmail-probe progname)))
289 (when x
290 (setq rmail-movemail-program progname)
291 (throw 'scan x))))))))))
293 (defvar rmail-movemail-variant-in-use nil
294 "The movemail variant currently in use. Known variants are:
296 `emacs' Means any implementation, compatible with the native Emacs one.
297 This is the default;
298 `mailutils' Means GNU mailutils implementation, capable of handling full
299 mail URLs as the source mailbox.")
301 ;;;###autoload
302 (defun rmail-movemail-variant-p (&rest variants)
303 "Return t if the current movemail variant is any of VARIANTS.
304 Currently known variants are `emacs' and `mailutils'."
305 (when (not rmail-movemail-variant-in-use)
306 ;; Autodetect
307 (setq rmail-movemail-variant-in-use (rmail-autodetect)))
308 (not (null (member rmail-movemail-variant-in-use variants))))
310 ;; Call for effect, to set rmail-movemail-program (if not set by the
311 ;; user), and rmail-movemail-variant-in-use. Used by various functions.
312 ;; I'm not sure if M-x rmail is the only entry point to this package.
313 ;; If so, this can be moved there.
314 (rmail-movemail-variant-p)
316 ;;;###autoload
317 (defcustom rmail-user-mail-address-regexp nil
318 "Regexp matching user mail addresses.
319 If non-nil, this variable is used to identify the correspondent
320 when receiving new mail. If it matches the address of the sender,
321 the recipient is taken as correspondent of a mail.
322 If nil \(default value), your `user-login-name' and `user-mail-address'
323 are used to exclude yourself as correspondent.
325 Usually you don't have to set this variable, except if you collect mails
326 sent by you under different user names.
327 Then it should be a regexp matching your mail addresses.
329 Setting this variable has an effect only before reading a mail."
330 :type '(choice (const :tag "None" nil) regexp)
331 :group 'rmail-retrieve
332 :version "21.1")
334 ;;;###autoload
335 (define-obsolete-variable-alias 'rmail-dont-reply-to-names
336 'mail-dont-reply-to-names "24.1")
338 ;; Prior to 24.1, this used to contain "\\`info-".
339 ;;;###autoload
340 (defvar rmail-default-dont-reply-to-names nil
341 "Regexp specifying part of the default value of `mail-dont-reply-to-names'.
342 This is used when the user does not set `mail-dont-reply-to-names'
343 explicitly.")
344 ;;;###autoload
345 (make-obsolete-variable 'rmail-default-dont-reply-to-names
346 'mail-dont-reply-to-names "24.1")
348 ;;;###autoload
349 (defcustom rmail-ignored-headers
350 (purecopy
351 (concat "^via:\\|^mail-from:\\|^origin:\\|^references:\\|^sender:"
352 "\\|^status:\\|^received:\\|^x400-originator:\\|^x400-recipients:"
353 "\\|^x400-received:\\|^x400-mts-identifier:\\|^x400-content-type:"
354 "\\|^\\(resent-\\|\\)message-id:\\|^summary-line:\\|^resent-date:"
355 "\\|^nntp-posting-host:\\|^path:\\|^x-char.*:\\|^x-face:\\|^face:"
356 "\\|^x-mailer:\\|^delivered-to:\\|^lines:"
357 "\\|^content-transfer-encoding:\\|^x-coding-system:"
358 "\\|^return-path:\\|^errors-to:\\|^return-receipt-to:"
359 "\\|^precedence:\\|^mime-version:"
360 "\\|^list-owner:\\|^list-help:\\|^list-post:\\|^list-subscribe:"
361 "\\|^list-id:\\|^list-unsubscribe:\\|^list-archive:"
362 "\\|^content-length:\\|^nntp-posting-date:\\|^user-agent"
363 "\\|^importance:\\|^envelope-to:\\|^delivery-date\\|^openpgp:"
364 "\\|^mbox-line:\\|^cancel-lock:"
365 "\\|^DomainKey-Signature:\\|^dkim-signature:"
366 "\\|^resent-face:\\|^resent-x.*:\\|^resent-organization:\\|^resent-openpgp:"
367 "\\|^x-.*:"))
368 "Regexp to match header fields that Rmail should normally hide.
369 \(See also `rmail-nonignored-headers', which overrides this regexp.)
370 This variable is used for reformatting the message header,
371 which normally happens once for each message,
372 when you view the message for the first time in Rmail.
373 To make a change in this variable take effect
374 for a message that you have already viewed,
375 go to that message and type \\[rmail-toggle-header] twice."
376 :type 'regexp
377 :group 'rmail-headers)
379 (defcustom rmail-nonignored-headers "^x-spam-status:"
380 "Regexp to match X header fields that Rmail should show.
381 This regexp overrides `rmail-ignored-headers'; if both this regexp
382 and that one match a certain header field, Rmail shows the field.
383 If this is nil, ignore all header fields in `rmail-ignored-headers'.
385 This variable is used for reformatting the message header,
386 which normally happens once for each message,
387 when you view the message for the first time in Rmail.
388 To make a change in this variable take effect
389 for a message that you have already viewed,
390 go to that message and type \\[rmail-toggle-header] twice."
391 :type '(choice (const nil) (regexp))
392 :group 'rmail-headers)
394 ;;;###autoload
395 (defcustom rmail-displayed-headers nil
396 "Regexp to match Header fields that Rmail should display.
397 If nil, display all header fields except those matched by
398 `rmail-ignored-headers'."
399 :type '(choice regexp (const :tag "All" nil))
400 :group 'rmail-headers)
402 ;;;###autoload
403 (defcustom rmail-retry-ignored-headers (purecopy "^x-authentication-warning:\\|^x-detected-operating-system:\\|^x-spam[-a-z]*:\\|content-type:\\|content-transfer-encoding:\\|mime-version:\\|message-id:")
404 "Headers that should be stripped when retrying a failed message."
405 :type '(choice regexp (const nil :tag "None"))
406 :group 'rmail-headers
407 :version "23.2") ; added x-detected-operating-system, x-spam
409 ;;;###autoload
410 (defcustom rmail-highlighted-headers (purecopy "^From:\\|^Subject:")
411 "Regexp to match Header fields that Rmail should normally highlight.
412 A value of nil means don't highlight. Uses the face `rmail-highlight'."
413 :type '(choice regexp (const :tag "None" nil))
414 :group 'rmail-headers)
416 (defface rmail-highlight
417 '((t (:inherit highlight)))
418 "Face to use for highlighting the most important header fields.
419 The variable `rmail-highlighted-headers' specifies which headers."
420 :group 'rmail-headers
421 :version "22.1")
423 ;; This was removed in Emacs 23.1 with no notification, an unnecessary
424 ;; incompatible change.
425 (defcustom rmail-highlight-face 'rmail-highlight
426 "Face used by Rmail for highlighting headers."
427 ;; Note that nil doesn't actually mean use the default face, it
428 ;; means use either bold or highlight. It's not worth fixing this
429 ;; now that this is obsolete.
430 :type '(choice (const :tag "Default" nil)
431 face)
432 :group 'rmail-headers)
433 (make-obsolete-variable 'rmail-highlight-face
434 "customize the face `rmail-highlight' instead."
435 "23.2")
437 (defface rmail-header-name
438 '((t (:inherit font-lock-function-name-face)))
439 "Face to use for highlighting the header names.
440 The variable `rmail-font-lock-keywords' specifies which headers
441 get highlighted."
442 :group 'rmail-headers
443 :version "23.1")
445 (defcustom rmail-delete-after-output nil
446 "Non-nil means automatically delete a message that is copied to a file."
447 :type 'boolean
448 :group 'rmail-files)
450 ;;;###autoload
451 (defcustom rmail-primary-inbox-list nil
452 "List of files that are inboxes for your primary mail file `rmail-file-name'.
453 If this is nil, uses the environment variable MAIL. If that is
454 unset, uses a file named by the function `user-login-name' in the
455 directory `rmail-spool-directory' (whose value depends on the
456 operating system). For example, \"/var/mail/USER\"."
457 ;; Don't use backquote here, because we don't want to need it at load time.
458 ;; (That must be an old comment - it's dumped these days.)
459 :type (list 'choice '(const :tag "Default" nil)
460 (list 'repeat ':value (list (or (getenv "MAIL")
461 (concat rmail-spool-directory
462 (user-login-name))))
463 'file))
464 :group 'rmail-retrieve
465 :group 'rmail-files)
467 (defcustom rmail-mail-new-frame nil
468 "Non-nil means Rmail makes a new frame for composing outgoing mail.
469 This is handy if you want to preserve the window configuration of
470 the frame where you have the RMAIL buffer displayed."
471 :type 'boolean
472 :group 'rmail-reply)
474 ;;;###autoload
475 (defcustom rmail-secondary-file-directory (purecopy "~/")
476 "Directory for additional secondary Rmail files."
477 :type 'directory
478 :group 'rmail-files)
479 ;;;###autoload
480 (defcustom rmail-secondary-file-regexp (purecopy "\\.xmail$")
481 "Regexp for which files are secondary Rmail files."
482 :type 'regexp
483 :group 'rmail-files)
485 (defcustom rmail-confirm-expunge 'y-or-n-p
486 "Whether and how to ask for confirmation before expunging deleted messages.
487 The value, if non-nil is a function to call with a question (string)
488 as argument, to ask the user that question."
489 :type '(choice (const :tag "No confirmation" nil)
490 (const :tag "Confirm with y-or-n-p" y-or-n-p)
491 (const :tag "Confirm with yes-or-no-p" yes-or-no-p))
492 :version "21.1"
493 :group 'rmail-files)
494 (put 'rmail-confirm-expunge 'risky-local-variable t)
496 ;;;###autoload
497 (defvar rmail-mode-hook nil
498 "List of functions to call when Rmail is invoked.")
500 (defvar rmail-get-new-mail-hook nil
501 "List of functions to call when Rmail has retrieved new mail.")
503 ;;;###autoload
504 (defcustom rmail-show-message-hook nil
505 "List of functions to call when Rmail displays a message."
506 :type 'hook
507 :options '(goto-address)
508 :group 'rmail)
510 (defvar rmail-quit-hook nil
511 "List of functions to call when quitting out of Rmail.")
513 (defvar rmail-delete-message-hook nil
514 "List of functions to call when Rmail deletes a message.
515 When the hooks are called, the message has been marked deleted but is
516 still the current message in the Rmail buffer.")
518 ;; These may be altered by site-init.el to match the format of mmdf files
519 ;; delimiting used on a given host (delim1 and delim2 from the config
520 ;; files).
522 (defvar rmail-mmdf-delim1 "^\001\001\001\001\n"
523 "Regexp marking the start of an mmdf message.")
524 (defvar rmail-mmdf-delim2 "^\001\001\001\001\n"
525 "Regexp marking the end of an mmdf message.")
527 ;; FIXME Post-mbox, this is now unused.
528 ;; In Emacs-22, this was called:
529 ;; i) the very first time a message was shown.
530 ;; ii) when toggling the headers to the normal state, every time.
531 ;; It's not clear what it should do now, since there is nothing that
532 ;; records when a message is shown for the first time (unseen is not
533 ;; necessarily the same thing).
534 ;; See http://lists.gnu.org/archive/html/emacs-devel/2009-03/msg00013.html
535 (defcustom rmail-message-filter nil
536 "If non-nil, a filter function for new messages in RMAIL.
537 Called with region narrowed to the message, including headers,
538 before obeying `rmail-ignored-headers'."
539 :group 'rmail-headers
540 :type '(choice (const nil) function))
542 (make-obsolete-variable 'rmail-message-filter
543 "it is not used (try `rmail-show-message-hook')."
544 "23.1")
546 (defcustom rmail-automatic-folder-directives nil
547 "List of directives specifying how to automatically file messages.
548 Whenever Rmail shows a message in the folder that `rmail-file-name'
549 specifies, it calls `rmail-auto-file' to maybe file the message in
550 another folder according to this list. Messages that are already
551 marked as `filed', or are in different folders, are left alone.
553 Each element of the list is of the form:
555 (FOLDERNAME FIELD REGEXP [ FIELD REGEXP ] ... )
557 FOLDERNAME is the name of a folder in which to put the message.
558 If FOLDERNAME is nil then Rmail deletes the message, and moves on to
559 the next. If FOLDERNAME is \"/dev/null\", Rmail deletes the message,
560 but does not move to the next.
562 FIELD is the name of a header field in the message, such as
563 \"subject\" or \"from\". A FIELD of \"to\" includes all text
564 from both the \"to\" and \"cc\" headers.
566 REGEXP is a regular expression to match (case-sensitively) against
567 the preceding specified FIELD.
569 There may be any number of FIELD/REGEXP pairs.
570 All pairs must match for a directive to apply to a message.
571 For a given message, Rmail applies only the first matching directive.
573 Examples:
574 (\"/dev/null\" \"from\" \"@spam.com\") ; delete all mail from spam.com
575 (\"RMS\" \"from\" \"rms@\") ; save all mail from RMS.
577 :group 'rmail
578 :version "21.1"
579 :type '(repeat (sexp :tag "Directive")))
581 (defvar rmail-reply-prefix "Re: "
582 "String to prepend to Subject line when replying to a message.")
584 ;; Some mailers use "Re(2):" or "Re^2:" or "Re: Re:" or "Re[2]:".
585 ;; This pattern should catch all the common variants.
586 ;; rms: I deleted the change to delete tags in square brackets
587 ;; because they mess up RT tags.
588 (defvar rmail-reply-regexp "\\`\\(Re\\(([0-9]+)\\|\\[[0-9]+\\]\\|\\^[0-9]+\\)?: *\\)*"
589 "Regexp to delete from Subject line before inserting `rmail-reply-prefix'.")
591 (defcustom rmail-display-summary nil
592 "If non-nil, Rmail always displays the summary buffer."
593 :group 'rmail-summary
594 :type 'boolean)
596 (defvar rmail-inbox-list nil)
597 (put 'rmail-inbox-list 'permanent-local t)
599 (defvar rmail-buffer nil
600 "The RMAIL buffer related to the current buffer.
601 In an RMAIL buffer, this holds the RMAIL buffer itself.
602 In a summary buffer, this holds the RMAIL buffer it is a summary for.")
603 (put 'rmail-buffer 'permanent-local t)
605 (defvar rmail-was-converted nil
606 "Non-nil in an Rmail buffer that was just converted from Babyl format.")
607 (put 'rmail-was-converted 'permanent-local t)
609 (defvar rmail-seriously-modified nil
610 "Non-nil in an Rmail buffer that has been modified in a major way.")
611 (put 'rmail-seriously-modified 'permanent-local t)
613 ;; Message counters and markers. Deleted flags.
615 (defvar rmail-current-message nil
616 "Integer specifying the message currently being displayed in this folder.
617 Counts messages from 1 to `rmail-total-messages'. A value of 0
618 means there are no messages in the folder.")
619 (put 'rmail-current-message 'permanent-local t)
621 (defvar rmail-total-messages nil
622 "Integer specifying the total number of messages in this folder.
623 Includes deleted messages.")
624 (put 'rmail-total-messages 'permanent-local t)
626 (defvar rmail-message-vector nil
627 "Vector of markers specifying the start and end of each message.
628 Element N and N+1 specify the start and end of message N.")
629 (put 'rmail-message-vector 'permanent-local t)
631 (defvar rmail-deleted-vector nil
632 "A string of length `rmail-total-messages' plus one.
633 Character N is either a space or \"D\", according to whether
634 message N is deleted or not.")
635 (put 'rmail-deleted-vector 'permanent-local t)
637 (defvar rmail-msgref-vector nil
638 "In an Rmail buffer, a vector whose Nth element is a list (N).
639 When expunging renumbers messages, these lists are modified
640 by substituting the new message number into the existing list.")
641 (put 'rmail-msgref-vector 'permanent-local t)
643 (defvar rmail-overlay-list nil)
644 (put 'rmail-overlay-list 'permanent-local t)
646 ;; These are used by autoloaded rmail-summary.
648 (defvar rmail-summary-buffer nil)
649 (put 'rmail-summary-buffer 'permanent-local t)
650 (defvar rmail-summary-vector nil
651 "In an Rmail buffer, vector of (newline-terminated) strings.
652 Element N specifies the summary line for message N+1.")
653 (put 'rmail-summary-vector 'permanent-local t)
655 ;; Rmail buffer swapping variables.
657 (defvar rmail-buffer-swapped nil
658 "If non-nil, `rmail-buffer' is swapped with `rmail-view-buffer'.")
659 (make-variable-buffer-local 'rmail-buffer-swapped)
660 (put 'rmail-buffer-swapped 'permanent-local t)
662 (defvar rmail-view-buffer nil
663 "Buffer which holds RMAIL message for MIME displaying.")
664 (make-variable-buffer-local 'rmail-view-buffer)
665 (put 'rmail-view-buffer 'permanent-local t)
667 ;; `Sticky' default variables.
669 ;; Last individual label specified to a or k.
670 (defvar rmail-last-label nil)
672 ;; Last set of values specified to C-M-n, C-M-p, C-M-s or C-M-l.
673 (defvar rmail-last-multi-labels nil)
675 (defvar rmail-last-regexp nil)
676 (put 'rmail-last-regexp 'permanent-local t)
678 ;; Note that rmail-output-read-file-name modifies this.
679 (defcustom rmail-default-file "~/xmail"
680 "Default file name for \\[rmail-output]."
681 :type 'file
682 :group 'rmail-files)
683 (defcustom rmail-default-body-file "~/mailout"
684 "Default file name for \\[rmail-output-body-to-file]."
685 :type 'file
686 :group 'rmail-files
687 :version "20.3")
689 ;; Mule and MIME related variables.
691 ;;;###autoload
692 (defvar rmail-file-coding-system nil
693 "Coding system used in RMAIL file.
695 This is set to nil by default.")
697 (defcustom rmail-get-coding-function nil
698 "Function of no args to try to determine coding system for a message.
699 If nil, just search for `rmail-mime-charset-pattern'."
700 :type '(choice (const nil) function)
701 :group 'rmail
702 :version "24.4")
704 (defcustom rmail-enable-mime t
705 "If non-nil, RMAIL automatically displays decoded MIME messages.
706 For this to work, the feature specified by `rmail-mime-feature' must
707 be available."
708 :type 'boolean
709 :version "23.3"
710 :group 'rmail)
712 (defcustom rmail-enable-mime-composing t
713 "If non-nil, use `rmail-insert-mime-forwarded-message-function' to forward."
714 :type 'boolean
715 :version "24.1" ; nil -> t
716 :group 'rmail)
718 (defvar rmail-show-mime-function nil
719 "Function of no argument called to show a decoded MIME message.
720 This function is called when `rmail-enable-mime' is non-nil.
721 The package providing MIME support should set this.")
723 ;;;###autoload
724 (defvar rmail-insert-mime-forwarded-message-function nil
725 "Function to insert a message in MIME format so it can be forwarded.
726 This function is called if `rmail-enable-mime' and
727 `rmail-enable-mime-composing' are non-nil.
728 It is called with one argument FORWARD-BUFFER, which is a
729 buffer containing the message to forward. The current buffer
730 is the outgoing mail buffer.")
732 (defvar rmail-insert-mime-resent-message-function nil
733 "Function to insert a message in MIME format so it can be resent.
734 This function is called by `rmail-resend' if `rmail-enable-mime' is non-nil.
735 It is called with one argument FORWARD-BUFFER, which is a
736 buffer containing the message to forward. The current buffer
737 is the outgoing mail buffer.")
739 ;; FIXME one might want to pass a LIMIT, as per
740 ;; rmail-search-mime-header-function.
741 (defvar rmail-search-mime-message-function nil
742 "Function to check if a regexp matches a MIME message.
743 This function is called by `rmail-search-message' if
744 `rmail-enable-mime' is non-nil. It is called (with point at the
745 start of the message) with two arguments MSG and REGEXP, where
746 MSG is the message number, REGEXP is the regular expression.")
748 (defvar rmail-search-mime-header-function nil
749 "Function to check if a regexp matches a header of MIME message.
750 This function is called by `rmail-message-regexp-p-1' if
751 `rmail-enable-mime' is non-nil. It is called (with point at the
752 start of the header) with three arguments MSG, REGEXP, and LIMIT,
753 where MSG is the message number, REGEXP is the regular
754 expression, LIMIT is the position specifying the end of header.")
756 (defvar rmail-mime-feature 'rmailmm
757 "Feature to require for MIME support in Rmail.
758 When starting Rmail, if `rmail-enable-mime' is non-nil, this
759 feature is loaded with `require'. The default value is `rmailmm'.
761 The library should set the variable `rmail-show-mime-function'
762 to an appropriate value, and optionally also set
763 `rmail-search-mime-message-function',
764 `rmail-search-mime-header-function',
765 `rmail-insert-mime-forwarded-message-function', and
766 `rmail-insert-mime-resent-message-function'.")
768 (defvar rmail-mime-charset-pattern
769 (concat "^content-type:[ \t]*text/plain;"
770 "\\(?:[ \t\n]*\\(?:format\\|delsp\\)=\"?[-a-z0-9]+\"?;\\)*"
771 "[ \t\n]*charset=\"?\\([^ \t\n\";]+\\)\"?")
772 "Regexp to match MIME-charset specification in a header of message.
773 The first parenthesized expression should match the MIME-charset name.")
776 (defvar rmail-unix-mail-delimiter
777 (let ((time-zone-regexp
778 (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
779 "\\|[-+]?[0-9][0-9][0-9][0-9]"
780 "\\|"
781 "\\) *")))
782 (concat
783 "From "
785 ;; Many things can happen to an RFC 822 mailbox before it is put into
786 ;; a `From' line. The leading phrase can be stripped, e.g.
787 ;; `Joe <@w.x:joe@y.z>' -> `<@w.x:joe@y.z>'. The <> can be stripped, e.g.
788 ;; `<@x.y:joe@y.z>' -> `@x.y:joe@y.z'. Everything starting with a CRLF
789 ;; can be removed, e.g.
790 ;; From: joe@y.z (Joe K
791 ;; User)
792 ;; can yield `From joe@y.z (Joe K Fri Mar 22 08:11:15 1996', and
793 ;; From: Joe User
794 ;; <joe@y.z>
795 ;; can yield `From Joe User Fri Mar 22 08:11:15 1996'.
796 ;; The mailbox can be removed or be replaced by white space, e.g.
797 ;; From: "Joe User"{space}{tab}
798 ;; <joe@y.z>
799 ;; can yield `From {space}{tab} Fri Mar 22 08:11:15 1996',
800 ;; where {space} and {tab} represent the Ascii space and tab characters.
801 ;; We want to match the results of any of these manglings.
802 ;; The following regexp rejects names whose first characters are
803 ;; obviously bogus, but after that anything goes.
804 "\\([^\0-\b\n-\r\^?].*\\)? "
806 ;; The time the message was sent.
807 "\\([^\0-\r \^?]+\\) +" ; day of the week
808 "\\([^\0-\r \^?]+\\) +" ; month
809 "\\([0-3]?[0-9]\\) +" ; day of month
810 "\\([0-2][0-9]:[0-5][0-9]\\(:[0-6][0-9]\\)?\\) *" ; time of day
812 ;; Perhaps a time zone, specified by an abbreviation, or by a
813 ;; numeric offset.
814 time-zone-regexp
816 ;; The year.
817 " \\([0-9][0-9]+\\) *"
819 ;; On some systems the time zone can appear after the year, too.
820 time-zone-regexp
822 ;; Old uucp cruft.
823 "\\(remote from .*\\)?"
825 "\n"))
826 "Regexp matching the delimiter of messages in UNIX mail format
827 \(UNIX From lines), minus the initial ^. Note that if you change
828 this expression, you must change the code in `rmail-nuke-pinhead-header'
829 that knows the exact ordering of the \\( \\) subexpressions.")
831 ;; FIXME the rmail-header-name headers ought to be customizable.
832 ;; It seems a bit arbitrary, for example, that all of the Date: line
833 ;; gets highlighted.
834 (defvar rmail-font-lock-keywords
835 ;; These are all matched case-insensitively.
836 (eval-when-compile
837 (let* ((cite-chars "[>|}]")
838 (cite-prefix "[:alpha:]")
839 (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
840 (list '("^\\(From\\|Sender\\|Resent-From\\):"
841 . 'rmail-header-name)
842 '("^\\(Mail-\\)?Reply-To:.*$" . 'rmail-header-name)
843 ;; FIXME Mail-Followup-To should probably be here too.
844 '("^Subject:" . 'rmail-header-name)
845 '("^X-Spam-Status:" . 'rmail-header-name)
846 '("^\\(To\\|Apparently-To\\|Cc\\|Newsgroups\\):"
847 . 'rmail-header-name)
848 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
849 `(,cite-chars
850 (,(concat "\\=[ \t]*"
851 "\\(\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
852 "\\(" cite-chars "[ \t]*\\)\\)+\\)"
853 "\\(.*\\)")
854 (beginning-of-line) (end-of-line)
855 (1 font-lock-comment-delimiter-face nil t)
856 (5 font-lock-comment-face nil t)))
857 '("^\\(X-[a-z0-9-]+\\|In-reply-to\\|Date\\):.*\\(\n[ \t]+.*\\)*$"
858 . 'rmail-header-name))))
859 "Additional expressions to highlight in Rmail mode.")
861 ;; Rmail does not expect horizontal splitting. (Bug#2282)
862 (defun rmail-pop-to-buffer (&rest args)
863 "Like `pop-to-buffer', but with `split-width-threshold' set to nil."
864 (let (split-width-threshold)
865 (apply 'pop-to-buffer args)))
867 ;; Perform BODY in the summary buffer
868 ;; in such a way that its cursor is properly updated in its own window.
869 (defmacro rmail-select-summary (&rest body)
870 `(let ((total rmail-total-messages))
871 (if (rmail-summary-displayed)
872 (let ((window (selected-window)))
873 (save-excursion
874 (unwind-protect
875 (progn
876 (rmail-pop-to-buffer rmail-summary-buffer)
877 ;; rmail-total-messages is a buffer-local var
878 ;; in the rmail buffer.
879 ;; This way we make it available for the body
880 ;; even tho the rmail buffer is not current.
881 (let ((rmail-total-messages total))
882 ,@body))
883 (select-window window))))
884 (with-current-buffer rmail-summary-buffer
885 (let ((rmail-total-messages total))
886 ,@body)))
887 (rmail-maybe-display-summary)))
889 ;;;; *** Rmail Mode ***
891 (defun rmail-require-mime-maybe ()
892 "Require `rmail-mime-feature' if that is non-nil.
893 Signal an error and set `rmail-mime-feature' to nil if the feature
894 isn't provided."
895 (when rmail-enable-mime
896 (condition-case nil
897 (require rmail-mime-feature)
898 (error
899 (display-warning
900 'rmail
901 (format-message "Although MIME support is requested
902 through `rmail-enable-mime' being non-nil, the required feature
903 `%s' (the value of `rmail-mime-feature')
904 is not available in the current session.
905 So, MIME support is turned off for the moment."
906 rmail-mime-feature)
907 :warning)
908 (setq rmail-enable-mime nil)))))
911 ;;;###autoload
912 (defun rmail (&optional file-name-arg)
913 "Read and edit incoming mail.
914 Moves messages into file named by `rmail-file-name' and edits that
915 file in RMAIL Mode.
916 Type \\[describe-mode] once editing that file, for a list of RMAIL commands.
918 May be called with file name as argument; then performs rmail editing on
919 that file, but does not copy any new mail into the file.
920 Interactively, if you supply a prefix argument, then you
921 have a chance to specify a file name with the minibuffer.
923 If `rmail-display-summary' is non-nil, make a summary for this RMAIL file."
924 (interactive (if current-prefix-arg
925 (list (read-file-name "Run rmail on RMAIL file: "))))
926 (rmail-require-mime-maybe)
927 (let* ((file-name (expand-file-name (or file-name-arg rmail-file-name)))
928 ;; Use find-buffer-visiting, not get-file-buffer, for those users
929 ;; who have find-file-visit-truename set to t.
930 (existed (find-buffer-visiting file-name))
931 run-mail-hook mail-buf msg-shown)
932 ;; Determine if an existing mail file has been changed behind the
933 ;; scene...
934 (if (and existed (not (verify-visited-file-modtime existed)))
935 ;; The mail file has been changed. Revisit it and reset the
936 ;; message state variables when in rmail mode.
937 (progn
938 (find-file file-name)
939 (when (and (verify-visited-file-modtime existed)
940 (eq major-mode 'rmail-mode))
941 (rmail-swap-buffers-maybe)
942 (rmail-set-message-counters)))
943 ;; The mail file is either unchanged or not visited. Visit it.
944 (switch-to-buffer
945 (let ((enable-local-variables nil)
946 ;; Force no-conversion by default, since that's what
947 ;; pre-mbox Rmail did with BABYL files (via
948 ;; auto-coding-regexp-alist).
949 (coding-system-for-read
950 (or coding-system-for-read 'no-conversion)))
951 (find-file-noselect file-name))))
952 ;; Ensure that the collection and view buffers are in sync and
953 ;; ensure that a message is not being edited.
954 (if (eq major-mode 'rmail-mode)
955 (rmail-swap-buffers-maybe))
956 (if (eq major-mode 'rmail-edit-mode)
957 (error "Exit Rmail Edit mode before getting new mail"))
958 (or (and existed (> (buffer-size) 0))
959 (setq run-mail-hook t))
960 ;; Ensure that the Rmail file is in mbox format, the buffer is in
961 ;; Rmail mode and has been scanned to find all the messages
962 ;; (setting the global message variables in the process).
963 (rmail-convert-file-maybe)
964 (unless (eq major-mode 'rmail-mode)
965 (rmail-mode-2))
966 (goto-char (point-max))
967 (rmail-maybe-set-message-counters)
968 (setq mail-buf rmail-buffer)
969 ;; Show the first unread message and process summary mode.
970 (unwind-protect
971 ;; Only get new mail when there is not a file name argument.
972 (unless file-name-arg
973 (setq msg-shown (rmail-get-new-mail)))
974 (progn
975 (set-buffer mail-buf)
976 (or msg-shown
977 (rmail-show-message (rmail-first-unseen-message)))
978 (if rmail-display-summary (rmail-summary))
979 (rmail-construct-io-menu)
980 (if run-mail-hook
981 (run-hooks 'rmail-mode-hook))))))
983 (defun rmail-convert-file-maybe ()
984 "Determine if the file needs to be converted to mbox format."
985 (widen)
986 (goto-char (point-min))
987 ;; Detect previous Babyl format files.
988 (let ((case-fold-search nil))
989 (cond ((looking-at "BABYL OPTIONS:")
990 ;; The file is Babyl version 5. Use unrmail to convert
991 ;; it.
992 (rmail-convert-babyl-to-mbox))
993 ((looking-at "Version: 5\n")
994 ;; Losing babyl file made by old version of Rmail. Fix the
995 ;; babyl file header and use unrmail to convert to mbox
996 ;; format.
997 (let ((buffer-read-only nil))
998 (insert "BABYL OPTIONS: -*- rmail -*-\n")
999 (rmail-convert-babyl-to-mbox)))
1000 ((equal (point-min) (point-max))
1001 (message "Empty Rmail file."))
1002 ((looking-at "From "))
1003 (t (error "Invalid mbox file")))))
1005 (defun rmail-error-bad-format (&optional msgnum)
1006 "Report that the buffer is not in the mbox file format.
1007 MSGNUM, if present, indicates the malformed message."
1008 (if msgnum
1009 (error "Message %d is not a valid RFC2822 message" msgnum)
1010 (error "Message is not a valid RFC2822 message")))
1012 (defun rmail-convert-babyl-to-mbox ()
1013 "Convert the mail file from Babyl version 5 to mbox.
1014 This function also reinitializes local variables used by Rmail."
1015 (let ((old-file (make-temp-file "rmail"))
1016 (new-file (make-temp-file "rmail")))
1017 (unwind-protect
1018 (progn
1019 (kill-all-local-variables)
1020 (write-region (point-min) (point-max) old-file)
1021 (unrmail old-file new-file)
1022 (message "Replacing BABYL format with mbox format...")
1023 (let ((inhibit-read-only t)
1024 (coding-system-for-read 'raw-text)
1025 (buffer-undo-list t))
1026 (erase-buffer)
1027 (insert-file-contents new-file)
1028 ;; Rmail buffers need to be saved with Unix EOLs, or else
1029 ;; the format will not be recognized.
1030 (set-buffer-file-coding-system 'raw-text-unix)
1031 (rmail-mode-1)
1032 (rmail-perm-variables)
1033 (rmail-variables)
1034 (setq rmail-was-converted t)
1035 (rmail-dont-modify-format)
1036 (goto-char (point-max))
1037 (rmail-set-message-counters))
1038 (message "Replacing BABYL format with mbox format...done"))
1039 (delete-file old-file)
1040 (delete-file new-file))))
1042 (defun rmail-get-coding-system ()
1043 "Return a suitable coding system to use for the current mail message.
1044 The buffer is expected to be narrowed to just the header of the message."
1045 (save-excursion
1046 (goto-char (point-min))
1047 (or (if rmail-get-coding-function
1048 (funcall rmail-get-coding-function))
1049 (if (re-search-forward rmail-mime-charset-pattern nil t)
1050 (coding-system-from-name (match-string 1))
1051 'undecided))))
1053 ;;; Set up Rmail mode keymaps
1055 (defvar rmail-mode-map
1056 (let ((map (make-keymap)))
1057 (suppress-keymap map)
1058 (define-key map "a" 'rmail-add-label)
1059 (define-key map "b" 'rmail-bury)
1060 (define-key map "c" 'rmail-continue)
1061 (define-key map "d" 'rmail-delete-forward)
1062 (define-key map "\C-d" 'rmail-delete-backward)
1063 (define-key map "e" 'rmail-edit-current-message)
1064 ;; If you change this, change the rmail-resend menu-item's :keys.
1065 (define-key map "f" 'rmail-forward)
1066 (define-key map "g" 'rmail-get-new-mail)
1067 (define-key map "h" 'rmail-summary)
1068 (define-key map "i" 'rmail-input)
1069 (define-key map "j" 'rmail-show-message)
1070 (define-key map "k" 'rmail-kill-label)
1071 (define-key map "l" 'rmail-summary-by-labels)
1072 (define-key map "\e\C-h" 'rmail-summary)
1073 (define-key map "\e\C-l" 'rmail-summary-by-labels)
1074 (define-key map "\e\C-r" 'rmail-summary-by-recipients)
1075 (define-key map "\e\C-s" 'rmail-summary-by-regexp)
1076 (define-key map "\e\C-f" 'rmail-summary-by-senders)
1077 (define-key map "\e\C-t" 'rmail-summary-by-topic)
1078 (define-key map "m" 'rmail-mail)
1079 (define-key map "\em" 'rmail-retry-failure)
1080 (define-key map "n" 'rmail-next-undeleted-message)
1081 (define-key map "\en" 'rmail-next-message)
1082 (define-key map "\e\C-n" 'rmail-next-labeled-message)
1083 (define-key map "o" 'rmail-output)
1084 (define-key map "\C-o" 'rmail-output-as-seen)
1085 (define-key map "p" 'rmail-previous-undeleted-message)
1086 (define-key map "\ep" 'rmail-previous-message)
1087 (define-key map "\e\C-p" 'rmail-previous-labeled-message)
1088 (define-key map "q" 'rmail-quit)
1089 (define-key map "r" 'rmail-reply)
1090 ;; I find I can't live without the default M-r command -- rms.
1091 ;; (define-key rmail-mode-map "\er" 'rmail-search-backwards)
1092 (define-key map "s" 'rmail-expunge-and-save)
1093 (define-key map "\es" 'rmail-search)
1094 (define-key map "t" 'rmail-toggle-header)
1095 (define-key map "u" 'rmail-undelete-previous-message)
1096 (define-key map "v" 'rmail-mime)
1097 (define-key map "w" 'rmail-output-body-to-file)
1098 (define-key map "\C-c\C-w" 'rmail-widen)
1099 (define-key map "x" 'rmail-expunge)
1100 (define-key map "." 'rmail-beginning-of-message)
1101 (define-key map "/" 'rmail-end-of-message)
1102 (define-key map "<" 'rmail-first-message)
1103 (define-key map ">" 'rmail-last-message)
1104 (define-key map " " 'scroll-up-command)
1105 (define-key map [?\S-\ ] 'scroll-down-command)
1106 (define-key map "\177" 'scroll-down-command)
1107 (define-key map "?" 'describe-mode)
1108 (define-key map "\C-c\C-s\C-d" 'rmail-sort-by-date)
1109 (define-key map "\C-c\C-s\C-s" 'rmail-sort-by-subject)
1110 (define-key map "\C-c\C-s\C-a" 'rmail-sort-by-author)
1111 (define-key map "\C-c\C-s\C-r" 'rmail-sort-by-recipient)
1112 (define-key map "\C-c\C-s\C-c" 'rmail-sort-by-correspondent)
1113 (define-key map "\C-c\C-s\C-l" 'rmail-sort-by-lines)
1114 (define-key map "\C-c\C-s\C-k" 'rmail-sort-by-labels)
1115 (define-key map "\C-c\C-n" 'rmail-next-same-subject)
1116 (define-key map "\C-c\C-p" 'rmail-previous-same-subject)
1119 (define-key map [menu-bar] (make-sparse-keymap))
1121 (define-key map [menu-bar classify]
1122 (cons "Classify" (make-sparse-keymap "Classify")))
1124 (define-key map [menu-bar classify input-menu]
1125 nil)
1127 (define-key map [menu-bar classify output-menu]
1128 nil)
1130 (define-key map [menu-bar classify output-body]
1131 '("Output body to file..." . rmail-output-body-to-file))
1133 (define-key map [menu-bar classify output-inbox]
1134 '("Output..." . rmail-output))
1136 (define-key map [menu-bar classify output]
1137 '("Output as seen..." . rmail-output-as-seen))
1139 (define-key map [menu-bar classify kill-label]
1140 '("Kill Label..." . rmail-kill-label))
1142 (define-key map [menu-bar classify add-label]
1143 '("Add Label..." . rmail-add-label))
1145 (define-key map [menu-bar summary]
1146 (cons "Summary" (make-sparse-keymap "Summary")))
1148 (define-key map [menu-bar summary senders]
1149 '("By Senders..." . rmail-summary-by-senders))
1151 (define-key map [menu-bar summary labels]
1152 '("By Labels..." . rmail-summary-by-labels))
1154 (define-key map [menu-bar summary recipients]
1155 '("By Recipients..." . rmail-summary-by-recipients))
1157 (define-key map [menu-bar summary topic]
1158 '("By Topic..." . rmail-summary-by-topic))
1160 (define-key map [menu-bar summary regexp]
1161 '("By Regexp..." . rmail-summary-by-regexp))
1163 (define-key map [menu-bar summary all]
1164 '("All" . rmail-summary))
1166 (define-key map [menu-bar mail]
1167 (cons "Mail" (make-sparse-keymap "Mail")))
1169 (define-key map [menu-bar mail rmail-get-new-mail]
1170 '("Get New Mail" . rmail-get-new-mail))
1172 (define-key map [menu-bar mail lambda]
1173 '("----"))
1175 (define-key map [menu-bar mail continue]
1176 '("Continue" . rmail-continue))
1178 (define-key map [menu-bar mail resend]
1179 '(menu-item "Resend..." rmail-resend :keys "C-u f"))
1181 (define-key map [menu-bar mail forward]
1182 '("Forward" . rmail-forward))
1184 (define-key map [menu-bar mail retry]
1185 '("Retry" . rmail-retry-failure))
1187 (define-key map [menu-bar mail reply]
1188 '("Reply" . rmail-reply))
1190 (define-key map [menu-bar mail mail]
1191 '("Mail" . rmail-mail))
1193 (define-key map [menu-bar delete]
1194 (cons "Delete" (make-sparse-keymap "Delete")))
1196 (define-key map [menu-bar delete expunge/save]
1197 '("Expunge/Save" . rmail-expunge-and-save))
1199 (define-key map [menu-bar delete expunge]
1200 '("Expunge" . rmail-expunge))
1202 (define-key map [menu-bar delete undelete]
1203 '("Undelete" . rmail-undelete-previous-message))
1205 (define-key map [menu-bar delete delete]
1206 '("Delete" . rmail-delete-forward))
1208 (define-key map [menu-bar move]
1209 (cons "Move" (make-sparse-keymap "Move")))
1211 (define-key map [menu-bar move search-back]
1212 '("Search Back..." . rmail-search-backwards))
1214 (define-key map [menu-bar move search]
1215 '("Search..." . rmail-search))
1217 (define-key map [menu-bar move previous]
1218 '("Previous Nondeleted" . rmail-previous-undeleted-message))
1220 (define-key map [menu-bar move next]
1221 '("Next Nondeleted" . rmail-next-undeleted-message))
1223 (define-key map [menu-bar move last]
1224 '("Last" . rmail-last-message))
1226 (define-key map [menu-bar move first]
1227 '("First" . rmail-first-message))
1229 (define-key map [menu-bar move previous]
1230 '("Previous" . rmail-previous-message))
1232 (define-key map [menu-bar move next]
1233 '("Next" . rmail-next-message))
1235 map)
1236 "Keymap used in Rmail mode.")
1238 ;; Rmail toolbar
1239 (defvar rmail-tool-bar-map
1240 (let ((map (make-sparse-keymap)))
1241 (tool-bar-local-item-from-menu 'rmail-get-new-mail "mail/inbox"
1242 map rmail-mode-map)
1243 (tool-bar-local-item-from-menu 'rmail-next-undeleted-message "right-arrow"
1244 map rmail-mode-map)
1245 (tool-bar-local-item-from-menu 'rmail-previous-undeleted-message "left-arrow"
1246 map rmail-mode-map)
1247 (tool-bar-local-item-from-menu 'rmail-search "search"
1248 map rmail-mode-map)
1249 (tool-bar-local-item-from-menu 'rmail-input "open"
1250 map rmail-mode-map)
1251 (tool-bar-local-item-from-menu 'rmail-mail "mail/compose"
1252 map rmail-mode-map)
1253 (tool-bar-local-item-from-menu 'rmail-reply "mail/reply-all"
1254 map rmail-mode-map)
1255 (tool-bar-local-item-from-menu 'rmail-forward "mail/forward"
1256 map rmail-mode-map)
1257 (tool-bar-local-item-from-menu 'rmail-delete-forward "close"
1258 map rmail-mode-map)
1259 (tool-bar-local-item-from-menu 'rmail-output "mail/move"
1260 map rmail-mode-map)
1261 (tool-bar-local-item-from-menu 'rmail-output-body-to-file "mail/save"
1262 map rmail-mode-map)
1263 (tool-bar-local-item-from-menu 'rmail-expunge "delete"
1264 map rmail-mode-map)
1265 map))
1269 ;; Rmail mode is suitable only for specially formatted data.
1270 (put 'rmail-mode 'mode-class 'special)
1272 (defun rmail-mode-kill-summary ()
1273 (if rmail-summary-buffer (kill-buffer rmail-summary-buffer)))
1275 (defvar rmail-enable-multibyte) ; dynamically bound
1277 ;;;###autoload
1278 (defun rmail-mode ()
1279 "Rmail Mode is used by \\<rmail-mode-map>\\[rmail] for editing Rmail files.
1280 All normal editing commands are turned off.
1281 Instead, these commands are available:
1283 \\[rmail-beginning-of-message] Move point to front of this message.
1284 \\[rmail-end-of-message] Move point to bottom of this message.
1285 \\[scroll-up] Scroll to next screen of this message.
1286 \\[scroll-down] Scroll to previous screen of this message.
1287 \\[rmail-next-undeleted-message] Move to Next non-deleted message.
1288 \\[rmail-previous-undeleted-message] Move to Previous non-deleted message.
1289 \\[rmail-next-message] Move to Next message whether deleted or not.
1290 \\[rmail-previous-message] Move to Previous message whether deleted or not.
1291 \\[rmail-first-message] Move to the first message in Rmail file.
1292 \\[rmail-last-message] Move to the last message in Rmail file.
1293 \\[rmail-show-message] Jump to message specified by numeric position in file.
1294 \\[rmail-search] Search for string and show message it is found in.
1295 \\[rmail-delete-forward] Delete this message, move to next nondeleted.
1296 \\[rmail-delete-backward] Delete this message, move to previous nondeleted.
1297 \\[rmail-undelete-previous-message] Undelete message. Tries current message, then earlier messages
1298 till a deleted message is found.
1299 \\[rmail-edit-current-message] Edit the current message. \\[rmail-cease-edit] to return to Rmail.
1300 \\[rmail-expunge] Expunge deleted messages.
1301 \\[rmail-expunge-and-save] Expunge and save the file.
1302 \\[rmail-quit] Quit Rmail: expunge, save, then switch to another buffer.
1303 \\[save-buffer] Save without expunging.
1304 \\[rmail-get-new-mail] Move new mail from system spool directory into this file.
1305 \\[rmail-mail] Mail a message (same as \\[mail-other-window]).
1306 \\[rmail-continue] Continue composing outgoing message started before.
1307 \\[rmail-reply] Reply to this message. Like \\[rmail-mail] but initializes some fields.
1308 \\[rmail-retry-failure] Send this message again. Used on a mailer failure message.
1309 \\[rmail-forward] Forward this message to another user.
1310 \\[rmail-output] Output (append) this message to another mail file.
1311 \\[rmail-output-as-seen] Output (append) this message to file as it's displayed.
1312 \\[rmail-output-body-to-file] Save message body to a file. Default filename comes from Subject line.
1313 \\[rmail-input] Input Rmail file. Run Rmail on that file.
1314 \\[rmail-add-label] Add label to message. It will be displayed in the mode line.
1315 \\[rmail-kill-label] Kill label. Remove a label from current message.
1316 \\[rmail-next-labeled-message] Move to Next message with specified label
1317 (label defaults to last one specified).
1318 Standard labels: filed, unseen, answered, forwarded, deleted.
1319 Any other label is present only if you add it with \\[rmail-add-label].
1320 \\[rmail-previous-labeled-message] Move to Previous message with specified label
1321 \\[rmail-summary] Show headers buffer, with a one line summary of each message.
1322 \\[rmail-summary-by-labels] Summarize only messages with particular label(s).
1323 \\[rmail-summary-by-recipients] Summarize only messages with particular recipient(s).
1324 \\[rmail-summary-by-regexp] Summarize only messages with particular regexp(s).
1325 \\[rmail-summary-by-topic] Summarize only messages with subject line regexp(s).
1326 \\[rmail-toggle-header] Toggle display of complete header."
1327 (interactive)
1328 (let ((finding-rmail-file (not (eq major-mode 'rmail-mode))))
1329 (rmail-mode-2)
1330 (when (and finding-rmail-file
1331 (null coding-system-for-read)
1332 (default-value 'enable-multibyte-characters))
1333 (let ((rmail-enable-multibyte t))
1334 (rmail-require-mime-maybe)
1335 (rmail-convert-file-maybe)
1336 (goto-char (point-max))
1337 (set-buffer-multibyte t)))
1338 (rmail-set-message-counters)
1339 (rmail-show-message rmail-total-messages)
1340 (when finding-rmail-file
1341 (when rmail-display-summary
1342 (rmail-summary))
1343 (rmail-construct-io-menu))
1344 (run-mode-hooks 'rmail-mode-hook)))
1346 (defun rmail-mode-2 ()
1347 (kill-all-local-variables)
1348 (rmail-mode-1)
1349 (rmail-perm-variables)
1350 (rmail-variables))
1352 (defun rmail-mode-1 ()
1353 (setq major-mode 'rmail-mode)
1354 (setq mode-name "RMAIL")
1355 (setq buffer-read-only t)
1356 ;; No need to auto save RMAIL files in normal circumstances
1357 ;; because they contain no info except attribute changes
1358 ;; and deletion of messages.
1359 ;; The one exception is when messages are copied into another mbox buffer.
1360 ;; rmail-output enables auto save when you do that.
1361 (setq buffer-auto-save-file-name nil)
1362 (use-local-map rmail-mode-map)
1363 (set-syntax-table text-mode-syntax-table)
1364 (setq local-abbrev-table text-mode-abbrev-table)
1365 ;; Functions to support buffer swapping:
1366 (add-hook 'write-region-annotate-functions
1367 'rmail-write-region-annotate nil t)
1368 (add-hook 'kill-buffer-hook 'rmail-mode-kill-buffer-hook nil t)
1369 (add-hook 'change-major-mode-hook 'rmail-change-major-mode-hook nil t))
1371 (defun rmail-generate-viewer-buffer ()
1372 "Return a reusable buffer suitable for viewing messages.
1373 Create the buffer if necessary."
1374 ;; We want to reuse any existing view buffer, so as not to create an
1375 ;; endless number of them. But we must avoid clashes if we visit
1376 ;; two different rmail files with the same basename (Bug#4593).
1377 (if (and (local-variable-p 'rmail-view-buffer)
1378 (buffer-live-p rmail-view-buffer))
1379 rmail-view-buffer
1380 (let ((newbuf
1381 (generate-new-buffer
1382 (format " *message-viewer %s*"
1383 (file-name-nondirectory
1384 (or buffer-file-name (buffer-name)))))))
1385 (with-current-buffer newbuf
1386 (add-hook 'kill-buffer-hook 'rmail-view-buffer-kill-buffer-hook nil t))
1387 newbuf)))
1389 (defun rmail-swap-buffers ()
1390 "Swap text between current buffer and `rmail-view-buffer'.
1391 This function preserves the current buffer's modified flag, and also
1392 sets the current buffer's `buffer-file-coding-system' to that of
1393 `rmail-view-buffer'."
1394 (let ((modp-this (buffer-modified-p))
1395 (modp-that
1396 (with-current-buffer rmail-view-buffer (buffer-modified-p)))
1397 (coding-this buffer-file-coding-system)
1398 (coding-that
1399 (with-current-buffer rmail-view-buffer
1400 buffer-file-coding-system)))
1401 (buffer-swap-text rmail-view-buffer)
1402 (setq buffer-file-coding-system coding-that)
1403 (with-current-buffer rmail-view-buffer
1404 (setq buffer-file-coding-system coding-this)
1405 (restore-buffer-modified-p modp-that))
1406 (restore-buffer-modified-p modp-this)))
1408 (defun rmail-buffers-swapped-p ()
1409 "Return non-nil if the message collection is in `rmail-view-buffer'."
1410 ;; This is analogous to tar-data-swapped-p in tar-mode.el.
1411 rmail-buffer-swapped)
1413 (defun rmail-change-major-mode-hook ()
1414 ;; Bring the actual Rmail messages back into the main buffer.
1415 (when (rmail-buffers-swapped-p)
1416 (rmail-swap-buffers)
1417 (setq rmail-buffer-swapped nil)))
1419 (defun rmail-swap-buffers-maybe ()
1420 "Determine if the Rmail buffer is showing a message.
1421 If so restore the actual mbox message collection."
1422 (with-current-buffer rmail-buffer
1423 (when (rmail-buffers-swapped-p)
1424 (rmail-swap-buffers)
1425 (setq rmail-buffer-swapped nil))))
1427 (defun rmail-modify-format ()
1428 "Warn if important modifications would change Rmail file's format."
1429 (with-current-buffer rmail-buffer
1430 (and rmail-was-converted
1431 ;; If it's already modified, don't warn again.
1432 (not rmail-seriously-modified)
1433 (not
1434 (yes-or-no-p
1435 (message "After this, %s would be saved in mbox format. Proceed? "
1436 (buffer-name))))
1437 (error "Aborted"))
1438 (setq rmail-seriously-modified t)))
1440 (defun rmail-dont-modify-format ()
1441 (when (and rmail-was-converted (not rmail-seriously-modified))
1442 (set-buffer-modified-p nil)
1443 (message "Marking buffer unmodified to avoid rewriting Babyl file as mbox file")))
1445 (defun rmail-mode-kill-buffer-hook ()
1446 ;; Turn off the hook on the view buffer, so we can kill it, then kill it.
1447 (if (buffer-live-p rmail-view-buffer)
1448 (with-current-buffer rmail-view-buffer
1449 (setq kill-buffer-hook nil)
1450 (kill-buffer rmail-view-buffer))))
1452 (defun rmail-view-buffer-kill-buffer-hook ()
1453 (error "Can't kill Rmail view buffer `%s' by itself"
1454 (buffer-name (current-buffer))))
1456 ;; Set up the permanent locals associated with an Rmail file.
1457 (defun rmail-perm-variables ()
1458 (make-local-variable 'rmail-last-regexp)
1459 (make-local-variable 'rmail-deleted-vector)
1460 (make-local-variable 'rmail-buffer)
1461 (make-local-variable 'rmail-was-converted)
1462 (setq rmail-was-converted nil)
1463 (make-local-variable 'rmail-seriously-modified)
1464 (setq rmail-seriously-modified nil)
1465 (setq rmail-buffer (current-buffer))
1466 (set-buffer-multibyte nil)
1467 (with-current-buffer (setq rmail-view-buffer (rmail-generate-viewer-buffer))
1468 (setq buffer-undo-list t)
1469 ;; Note that this does not erase the buffer. Should it?
1470 ;; It depends on how this is called. If somehow called with the
1471 ;; rmail buffers swapped, it would erase the message collection.
1472 (set (make-local-variable 'rmail-overlay-list) nil)
1473 (set-buffer-multibyte t)
1474 ;; Force C-x C-s write Unix EOLs.
1475 (set-buffer-file-coding-system 'undecided-unix))
1476 (make-local-variable 'rmail-summary-buffer)
1477 (make-local-variable 'rmail-summary-vector)
1478 (make-local-variable 'rmail-current-message)
1479 (make-local-variable 'rmail-total-messages)
1480 (setq rmail-total-messages 0)
1481 (make-local-variable 'rmail-message-vector)
1482 (make-local-variable 'rmail-msgref-vector)
1483 (make-local-variable 'rmail-inbox-list)
1484 ;; Provide default set of inboxes for primary mail file ~/RMAIL.
1485 (and (null rmail-inbox-list)
1486 (or (equal buffer-file-name (expand-file-name rmail-file-name))
1487 (equal buffer-file-truename
1488 (abbreviate-file-name (file-truename rmail-file-name))))
1489 (setq rmail-inbox-list
1490 (or rmail-primary-inbox-list
1491 (list (or (getenv "MAIL")
1492 ;; FIXME expand-file-name?
1493 (concat rmail-spool-directory
1494 (user-login-name)))))))
1495 (set (make-local-variable 'tool-bar-map) rmail-tool-bar-map))
1497 ;; Set up the non-permanent locals associated with Rmail mode.
1498 (defun rmail-variables ()
1499 ;; Turn off undo. We turn it back on in rmail-edit.
1500 (setq buffer-undo-list t)
1501 ;; Don't let a local variables list in a message cause confusion.
1502 (make-local-variable 'local-enable-local-variables)
1503 (setq local-enable-local-variables nil)
1504 ;; Don't turn off auto-saving based on the size of the buffer
1505 ;; because that code does not understand buffer-swapping.
1506 (make-local-variable 'auto-save-include-big-deletions)
1507 (setq auto-save-include-big-deletions t)
1508 (make-local-variable 'revert-buffer-function)
1509 (setq revert-buffer-function 'rmail-revert)
1510 (make-local-variable 'font-lock-defaults)
1511 (setq font-lock-defaults
1512 '(rmail-font-lock-keywords
1513 t t nil nil
1514 (font-lock-maximum-size . nil)
1515 (font-lock-dont-widen . t)
1516 (font-lock-inhibit-thing-lock . (lazy-lock-mode fast-lock-mode))))
1517 (make-local-variable 'require-final-newline)
1518 (setq require-final-newline nil)
1519 (make-local-variable 'version-control)
1520 (setq version-control 'never)
1521 (make-local-variable 'kill-buffer-hook)
1522 (add-hook 'kill-buffer-hook 'rmail-mode-kill-summary)
1523 (make-local-variable 'file-precious-flag)
1524 (setq file-precious-flag t)
1525 (make-local-variable 'desktop-save-buffer)
1526 (setq desktop-save-buffer t)
1527 (make-local-variable 'save-buffer-coding-system)
1528 (setq save-buffer-coding-system 'no-conversion)
1529 (setq next-error-move-function 'rmail-next-error-move))
1531 ;; Handle M-x revert-buffer done in an rmail-mode buffer.
1532 (defun rmail-revert (arg noconfirm)
1533 (set-buffer rmail-buffer)
1534 (let* ((revert-buffer-function (default-value 'revert-buffer-function))
1535 (rmail-enable-multibyte enable-multibyte-characters)
1536 ;; See similar code in `rmail'.
1537 ;; FIXME needs updating?
1538 (coding-system-for-read (and rmail-enable-multibyte 'raw-text))
1539 (before-revert-hook 'rmail-swap-buffers-maybe))
1540 ;; Call our caller again, but this time it does the default thing.
1541 (when (revert-buffer arg noconfirm)
1542 ;; If the user said "yes", and we changed something,
1543 ;; reparse the messages.
1544 (set-buffer rmail-buffer)
1545 (rmail-mode-2)
1546 ;; Convert all or part to Babyl file if possible.
1547 (rmail-convert-file-maybe)
1548 ;; We have read the file as raw-text, so the buffer is set to
1549 ;; unibyte. Make it multibyte if necessary.
1550 (if (and rmail-enable-multibyte
1551 (not enable-multibyte-characters))
1552 (set-buffer-multibyte t))
1553 (goto-char (point-max))
1554 (rmail-set-message-counters)
1555 (rmail-show-message rmail-total-messages)
1556 (run-hooks 'rmail-mode-hook))))
1558 (defun rmail-expunge-and-save ()
1559 "Expunge and save RMAIL file."
1560 (interactive)
1561 (set-buffer rmail-buffer)
1562 (rmail-expunge)
1563 ;; No need to swap buffers: rmail-write-region-annotate takes care of it.
1564 ;; (rmail-swap-buffers-maybe)
1565 (save-buffer)
1566 (if (rmail-summary-exists)
1567 (rmail-select-summary (set-buffer-modified-p nil))))
1569 (defun rmail-quit ()
1570 "Quit out of RMAIL.
1571 Hook `rmail-quit-hook' is run after expunging."
1572 (interactive)
1573 (set-buffer rmail-buffer)
1574 (rmail-expunge t)
1575 (save-buffer)
1576 (when (boundp 'rmail-quit-hook)
1577 (run-hooks 'rmail-quit-hook))
1578 ;; Don't switch to the summary buffer even if it was recently visible.
1579 (when (rmail-summary-exists)
1580 (with-current-buffer rmail-summary-buffer
1581 (set-buffer-modified-p nil))
1582 (replace-buffer-in-windows rmail-summary-buffer)
1583 (bury-buffer rmail-summary-buffer))
1584 (let ((obuf (current-buffer)))
1585 (quit-window)
1586 (replace-buffer-in-windows obuf)))
1588 (defun rmail-bury ()
1589 "Bury current Rmail buffer and its summary buffer."
1590 (interactive)
1591 ;; This let var was called rmail-buffer, but that interfered
1592 ;; with the buffer-local var used in summary buffers.
1593 (if (rmail-summary-exists)
1594 (let (window)
1595 (while (setq window (get-buffer-window rmail-summary-buffer))
1596 (quit-window nil window))
1597 (bury-buffer rmail-summary-buffer)))
1598 (quit-window))
1600 (defun rmail-duplicate-message ()
1601 "Create a duplicated copy of the current message.
1602 The duplicate copy goes into the Rmail file just after the original."
1603 ;; If we are in a summary buffer, switch to the Rmail buffer.
1604 ;; FIXME simpler to swap the contents, not the buffers?
1605 (set-buffer rmail-buffer)
1606 (rmail-modify-format)
1607 (let ((buff (current-buffer))
1608 (n rmail-current-message)
1609 (beg (rmail-msgbeg rmail-current-message))
1610 (end (rmail-msgend rmail-current-message)))
1611 (if (rmail-buffers-swapped-p) (set-buffer rmail-view-buffer))
1612 (widen)
1613 (let ((buffer-read-only nil)
1614 (string (buffer-substring-no-properties beg end)))
1615 (goto-char end)
1616 (insert string))
1617 (set-buffer buff)
1618 (rmail-swap-buffers-maybe)
1619 (goto-char (point-max))
1620 (rmail-set-message-counters)
1621 (set-buffer-modified-p t)
1622 (rmail-show-message-1 n))
1623 (if (rmail-summary-exists)
1624 (rmail-select-summary (rmail-update-summary)))
1625 (message "Message duplicated"))
1627 ;;;###autoload
1628 (defun rmail-input (filename)
1629 "Run Rmail on file FILENAME."
1630 (interactive "FRun rmail on RMAIL file: ")
1631 (rmail filename))
1633 ;; This used to scan subdirectories recursively, but someone pointed out
1634 ;; that if the user wants that, person can put all the files in one dir.
1635 ;; And the recursive scan was slow. So I took it out.
1636 ;; rms, Sep 1996.
1637 (defun rmail-find-all-files (start)
1638 "Return list of file in dir START that match `rmail-secondary-file-regexp'."
1639 (if (file-accessible-directory-p start)
1640 ;; Don't sort here.
1641 (let* ((case-fold-search t)
1642 (files (directory-files start t rmail-secondary-file-regexp)))
1643 ;; Sort here instead of in directory-files
1644 ;; because this list is usually much shorter.
1645 (sort files 'string<))))
1647 (defun rmail-list-to-menu (menu-name l action &optional full-name)
1648 (let ((menu (make-sparse-keymap menu-name))
1649 name)
1650 (mapc
1651 (lambda (item)
1652 (let (command)
1653 (if (consp item)
1654 (setq command
1655 (rmail-list-to-menu
1656 (car item) (cdr item) action
1657 (if full-name
1658 (concat full-name "/"
1659 (car item))
1660 (car item)))
1661 name (car item))
1662 (setq name item)
1663 (setq command
1664 (list 'lambda () '(interactive)
1665 (list action
1666 (expand-file-name
1667 (if full-name
1668 (concat full-name "/" item)
1669 item)
1670 rmail-secondary-file-directory)))))
1671 (define-key menu (vector (intern name))
1672 (cons name command))))
1673 (reverse l))
1674 menu))
1676 ;; This command is always "disabled" when it appears in a menu.
1677 (put 'rmail-disable-menu 'menu-enable ''nil)
1679 (defun rmail-construct-io-menu ()
1680 (let ((files (rmail-find-all-files rmail-secondary-file-directory)))
1681 (if files
1682 (progn
1683 (define-key rmail-mode-map [menu-bar classify input-menu]
1684 (cons "Input Rmail File"
1685 (rmail-list-to-menu "Input Rmail File"
1686 files
1687 'rmail-input)))
1688 (define-key rmail-mode-map [menu-bar classify output-menu]
1689 (cons "Output Rmail File"
1690 (rmail-list-to-menu "Output Rmail File"
1691 files
1692 'rmail-output))))
1694 (define-key rmail-mode-map [menu-bar classify input-menu]
1695 '("Input Rmail File" . rmail-disable-menu))
1696 (define-key rmail-mode-map [menu-bar classify output-menu]
1697 '("Output Rmail File" . rmail-disable-menu)))))
1700 ;;;; *** Rmail input ***
1702 (declare-function rmail-summary-goto-msg "rmailsum" (&optional n nowarn skip-rmail))
1703 (declare-function rmail-summary-mark-undeleted "rmailsum" (n))
1704 (declare-function rmail-summary-mark-deleted "rmailsum" (&optional n undel))
1705 (declare-function rfc822-addresses "rfc822" (header-text))
1706 (declare-function mail-abbrev-make-syntax-table "mailabbrev.el" ())
1708 ;; RLK feature not added in this version:
1709 ;; argument specifies inbox file or files in various ways.
1711 ;; In Babyl, the Mail: header in the preamble overrode rmail-inbox-list.
1712 ;; Mbox does not have this feature.
1713 (defun rmail-get-new-mail (&optional file-name)
1714 "Move any new mail from this Rmail file's inbox files.
1715 The buffer-local variable `rmail-inbox-list' specifies the list
1716 of inbox files. By default, this is nil, except for your primary
1717 Rmail file `rmail-file-name'. In this case, when you first visit
1718 the Rmail file it is initialized using either
1719 `rmail-primary-inbox-list', or the \"MAIL\" environment variable,
1720 or the function `user-login-name' and the directory
1721 `rmail-spool-directory' (whose value depends on the operating system).
1723 The command `set-rmail-inbox-list' sets `rmail-inbox-list' to the
1724 value you specify.
1726 You can also specify the file to get new mail from just for one
1727 instance of this command. In this case, the file of new mail is
1728 not changed or deleted. Noninteractively, you can pass the inbox
1729 file name as an argument. Interactively, a prefix argument
1730 causes us to read a file name and use that file as the inbox.
1732 If the variable `rmail-preserve-inbox' is non-nil, new mail will
1733 always be left in inbox files rather than deleted.
1735 Before doing anything, this runs `rmail-before-get-new-mail-hook'.
1736 Just before returning, it runs `rmail-after-get-new-mail-hook',
1737 whether or not there is new mail.
1739 If there is new mail, it runs `rmail-get-new-mail-hook', saves
1740 the updated file, and shows the first unseen message (which might
1741 not be a new one). It returns non-nil if it got any new messages."
1742 (interactive
1743 (list (if current-prefix-arg
1744 (read-file-name "Get new mail from file: "))))
1745 (run-hooks 'rmail-before-get-new-mail-hook)
1746 ;; If the disk file has been changed from under us,
1747 ;; revert to it before we get new mail.
1748 (or (verify-visited-file-modtime (current-buffer))
1749 (find-file (buffer-file-name)))
1750 (set-buffer rmail-buffer)
1751 (rmail-modify-format)
1752 (rmail-swap-buffers-maybe)
1753 (rmail-maybe-set-message-counters)
1754 (widen)
1755 ;; Get rid of all undo records for this buffer.
1756 (or (eq buffer-undo-list t)
1757 (setq buffer-undo-list nil))
1758 (let ((all-files (if file-name (list file-name) rmail-inbox-list))
1759 (rmail-enable-multibyte (default-value 'enable-multibyte-characters))
1760 found)
1761 (unwind-protect
1762 (progn
1763 ;; This loops if any members of the inbox list have the same
1764 ;; basename (see "name conflict" below).
1765 (while all-files
1766 (let (;; If buffer has not changed yet, and has not been
1767 ;; saved yet, don't replace the old backup file now.
1768 (make-backup-files (and make-backup-files
1769 (buffer-modified-p)))
1770 (buffer-read-only nil)
1771 ;; Don't make undo records while getting mail.
1772 (buffer-undo-list t)
1773 delete-files files file-last-names)
1774 ;; Pull files off all-files onto files as long as there is
1775 ;; no name conflict. A conflict happens when two inbox
1776 ;; file names have the same last component.
1777 ;; The reason this careful handling is necessary seems
1778 ;; to be that rmail-insert-inbox-text uses .newmail-BASENAME.
1779 (while (and all-files
1780 (not (member (file-name-nondirectory (car all-files))
1781 file-last-names)))
1782 (setq files (cons (car all-files) files)
1783 file-last-names
1784 (cons (file-name-nondirectory (car all-files)) files))
1785 (setq all-files (cdr all-files)))
1786 ;; Put them back in their original order.
1787 (setq files (nreverse files))
1788 (goto-char (point-max))
1789 ;; Make sure we end with a blank line unless there are
1790 ;; no messages, as required by mbox format (Bug#9974).
1791 (unless (bobp)
1792 (while (not (looking-back "\n\n" (- (point) 2)))
1793 (insert "\n")))
1794 (setq found (or
1795 (rmail-get-new-mail-1 file-name files delete-files)
1796 found))))
1797 ;; Move to the first new message unless we have other unseen
1798 ;; messages before it.
1799 (if found (rmail-show-message (rmail-first-unseen-message)))
1800 (run-hooks 'rmail-after-get-new-mail-hook)
1801 found)
1802 ;; Don't leave the buffer screwed up if we get a disk-full error.
1803 (rmail-show-message))))
1805 (defvar rmail-use-spam-filter)
1806 (declare-function rmail-get-new-mail-filter-spam "rmail-spam-filter" (nnew))
1808 (defun rmail-get-new-mail-1 (file-name files delete-files)
1809 "Return t if new messages are detected without error, nil otherwise."
1810 (save-excursion
1811 (save-restriction
1812 (let ((new-messages 0)
1813 (spam-filter-p (and (featurep 'rmail-spam-filter)
1814 rmail-use-spam-filter))
1815 (blurb "")
1816 result success suffix)
1817 (narrow-to-region (point) (point))
1818 ;; Read in the contents of the inbox files, renaming them as
1819 ;; necessary, and adding to the list of files to delete
1820 ;; eventually.
1821 (unwind-protect
1822 (progn
1823 ;; Set modified now to lock the file, so that we don't
1824 ;; encounter locking problems later in the middle of
1825 ;; reading the mail.
1826 (set-buffer-modified-p t)
1827 (if file-name
1828 (rmail-insert-inbox-text files nil)
1829 (setq delete-files (rmail-insert-inbox-text files t))))
1830 ;; If there was no new mail, or we aborted before actually
1831 ;; trying to get any, mark buffer unmodified. Otherwise the
1832 ;; buffer is correctly marked modified and the file locked
1833 ;; until we save out the new mail.
1834 (if (= (point-min) (point-max))
1835 (set-buffer-modified-p nil)))
1836 ;; Scan the new text and convert each message to
1837 ;; Rmail/mbox format.
1838 (goto-char (point-min))
1839 (skip-chars-forward " \n")
1840 (narrow-to-region (point) (point-max))
1841 (unwind-protect
1842 (setq new-messages (rmail-add-mbox-headers)
1843 success t)
1844 ;; Try to delete the garbage just inserted.
1845 (or success (delete-region (point-min) (point-max)))
1846 ;; If we could not convert the file's inboxes, rename the
1847 ;; files we tried to read so we won't over and over again.
1848 (if (and (not file-name) (not success))
1849 (let ((delfiles delete-files)
1850 (count 0))
1851 (while delfiles
1852 (while (file-exists-p (format "RMAILOSE.%d" count))
1853 (setq count (1+ count)))
1854 (rename-file (car delfiles) (format "RMAILOSE.%d" count))
1855 (setq delfiles (cdr delfiles))))))
1856 ;; Determine if there are messages.
1857 (unless (zerop new-messages)
1858 ;; There are. Process them.
1859 (goto-char (point-min))
1860 (rmail-count-new-messages)
1861 (run-hooks 'rmail-get-new-mail-hook)
1862 (save-buffer))
1863 ;; Delete the old files, now that the Rmail file is saved.
1864 (while delete-files
1865 (condition-case ()
1866 ;; First, try deleting.
1867 (condition-case ()
1868 (delete-file (car delete-files))
1869 (file-error
1870 ;; If we can't delete it, truncate it.
1871 (write-region (point) (point) (car delete-files))))
1872 (file-error nil))
1873 (setq delete-files (cdr delete-files)))
1874 (if (zerop new-messages)
1875 (when (or file-name rmail-inbox-list)
1876 (message "(No new mail has arrived)"))
1877 (if spam-filter-p
1878 (setq blurb (rmail-get-new-mail-filter-spam new-messages))))
1879 (if (rmail-summary-exists)
1880 (rmail-select-summary (rmail-update-summary)))
1881 (setq suffix (if (= 1 new-messages) "" "s"))
1882 (message "%d new message%s read%s" new-messages suffix blurb)
1883 ;; Establish the return value.
1884 (setq result (> new-messages 0))
1885 result))))
1887 (defun rmail-parse-url (file)
1888 "Parse the supplied URL. Return (list MAILBOX-NAME REMOTE PASSWORD GOT-PASSWORD)
1889 WHERE MAILBOX-NAME is the name of the mailbox suitable as argument to the
1890 actual version of `movemail', REMOTE is non-nil if MAILBOX-NAME refers to
1891 a remote mailbox, PASSWORD is the password if it should be
1892 supplied as a separate argument to `movemail' or nil otherwise, GOT-PASSWORD
1893 is non-nil if the user has supplied the password interactively.
1895 (cond
1896 ((string-match "^\\([^:]+\\)://\\(\\([^:@]+\\)\\(:\\([^@]+\\)\\)?@\\)?.*" file)
1897 (let (got-password supplied-password
1898 (proto (match-string 1 file))
1899 (user (match-string 3 file))
1900 (pass (match-string 5 file))
1901 (host (substring file (or (match-end 2)
1902 (+ 3 (match-end 1))))))
1904 (if (not pass)
1905 (when rmail-remote-password-required
1906 (setq got-password (not (rmail-have-password)))
1907 (setq supplied-password (rmail-get-remote-password
1908 (string-equal proto "imap"))))
1909 ;; The password is embedded. Strip it out since movemail
1910 ;; does not really like it, in spite of the movemail spec.
1911 (setq file (concat proto "://" user "@" host)))
1913 (if (rmail-movemail-variant-p 'emacs)
1914 (if (string-equal proto "pop")
1915 (list (concat "po:" user ":" host)
1917 (or pass supplied-password)
1918 got-password)
1919 (error "Emacs movemail does not support %s protocol" proto))
1920 (list file
1921 (or (string-equal proto "pop") (string-equal proto "imap"))
1922 (or supplied-password pass)
1923 got-password))))
1925 ((string-match "^po:\\([^:]+\\)\\(:\\(.*\\)\\)?" file)
1926 (let (got-password supplied-password
1927 ;; (proto "pop")
1928 ;; (user (match-string 1 file))
1929 ;; (host (match-string 3 file))
1932 (when rmail-remote-password-required
1933 (setq got-password (not (rmail-have-password)))
1934 (setq supplied-password (rmail-get-remote-password nil)))
1936 (list file "pop" supplied-password got-password)))
1939 (list file nil nil nil))))
1941 (defun rmail-unrmail-new-mail (from-file)
1942 "Replace newly read mail in Babyl format with equivalent mbox format.
1944 FROM-FILE is the Babyl file from which the new mail should be read."
1945 (let ((to-file (make-temp-file "rmail"))
1946 size)
1947 (unrmail from-file to-file)
1948 (let ((inhibit-read-only t)
1949 (coding-system-for-read 'raw-text)
1950 (buffer-undo-list t))
1951 (delete-region (point) (point-max))
1952 (setq size (nth 1 (insert-file-contents to-file)))
1953 (delete-file to-file)
1954 size)))
1956 (defun rmail-unrmail-new-mail-maybe (file size)
1957 "If newly read mail from FILE is in Babyl format, convert it to mbox format.
1959 SIZE is the original size of the newly read mail.
1960 Value is the size of the newly read mail after conversion."
1961 ;; Detect previous Babyl format files.
1962 (let ((case-fold-search nil)
1963 (old-file file))
1964 (cond ((looking-at "BABYL OPTIONS:")
1965 ;; The new mail is in Babyl version 5 format. Use unrmail
1966 ;; to convert it.
1967 (setq size (rmail-unrmail-new-mail old-file)))
1968 ((looking-at "Version: 5\n")
1969 ;; New mail is in Babyl format made by old version of
1970 ;; Rmail. Fix the babyl file header and use unrmail to
1971 ;; convert it.
1972 (let ((buffer-read-only nil)
1973 (write-region-annotate-functions nil)
1974 (write-region-post-annotation-function nil)
1975 (old-file (make-temp-file "rmail")))
1976 (insert "BABYL OPTIONS: -*- rmail -*-\n")
1977 (forward-line -1)
1978 (write-region (point) (point-max) old-file)
1979 (setq size (rmail-unrmail-new-mail old-file))
1980 (delete-file old-file))))
1981 size))
1983 (defun rmail-insert-inbox-text (files renamep)
1984 (let (file tofile delete-files popmail got-password password)
1985 (while files
1986 ;; Handle remote mailbox names specially; don't expand as filenames
1987 ;; in case the userid contains a directory separator.
1988 (setq file (car files))
1989 (let ((url-data (rmail-parse-url file)))
1990 (setq file (nth 0 url-data))
1991 (setq popmail (nth 1 url-data))
1992 (setq password (nth 2 url-data))
1993 (setq got-password (nth 3 url-data)))
1995 (if popmail
1996 (setq renamep t)
1997 (setq file (file-truename
1998 (substitute-in-file-name (expand-file-name file)))))
1999 (setq tofile (expand-file-name
2000 ;; Generate name to move to from inbox name,
2001 ;; in case of multiple inboxes that need moving.
2002 (concat ".newmail-"
2003 (file-name-nondirectory
2004 (if (memq system-type '(windows-nt cygwin ms-dos))
2005 ;; cannot have colons in file name
2006 (replace-regexp-in-string ":" "-" file)
2007 file)))
2008 ;; Use the directory of this rmail file
2009 ;; because it's a nuisance to use the homedir
2010 ;; if that is on a full disk and this rmail
2011 ;; file isn't.
2012 (file-name-directory
2013 (expand-file-name buffer-file-name))))
2014 ;; Always use movemail to rename the file,
2015 ;; since there can be mailboxes in various directories.
2016 (when (not popmail)
2017 ;; On some systems, /usr/spool/mail/foo is a directory
2018 ;; and the actual inbox is /usr/spool/mail/foo/foo.
2019 (if (file-directory-p file)
2020 (setq file (expand-file-name (user-login-name)
2021 file))))
2022 (cond (popmail
2023 (message "Getting mail from the remote server ..."))
2024 ((and (file-exists-p tofile)
2025 (/= 0 (nth 7 (file-attributes tofile))))
2026 (message "Getting mail from %s..." tofile))
2027 ((and (file-exists-p file)
2028 (/= 0 (nth 7 (file-attributes file))))
2029 (message "Getting mail from %s..." file)))
2030 ;; Set TOFILE if have not already done so, and
2031 ;; rename or copy the file FILE to TOFILE if and as appropriate.
2032 (cond ((not renamep)
2033 (setq tofile file))
2034 ((or (file-exists-p tofile) (and (not popmail)
2035 (not (file-exists-p file))))
2036 nil)
2038 (with-temp-buffer
2039 (let ((errors (current-buffer)))
2040 (buffer-disable-undo errors)
2041 (let ((args
2042 (append
2043 (list (or rmail-movemail-program "movemail") nil errors nil)
2044 (if rmail-preserve-inbox
2045 (list "-p")
2046 nil)
2047 (if (rmail-movemail-variant-p 'mailutils)
2048 (append (list "--emacs") rmail-movemail-flags)
2049 rmail-movemail-flags)
2050 (list file tofile)
2051 (if password (list password) nil))))
2052 (apply 'call-process args))
2053 (if (not (buffer-modified-p errors))
2054 ;; No output => movemail won
2056 (set-buffer errors)
2057 (subst-char-in-region (point-min) (point-max)
2058 ?\n ?\s)
2059 (goto-char (point-max))
2060 (skip-chars-backward " \t")
2061 (delete-region (point) (point-max))
2062 (goto-char (point-min))
2063 (if (looking-at "movemail: ")
2064 (delete-region (point-min) (match-end 0)))
2065 (beep t)
2066 ;; If we just read the password, most likely it is
2067 ;; wrong. Otherwise, see if there is a specific
2068 ;; reason to think that the problem is a wrong passwd.
2069 (if (or got-password
2070 (re-search-forward rmail-remote-password-error
2071 nil t))
2072 (rmail-set-remote-password nil))
2074 ;; If using Mailutils, remove initial error code
2075 ;; abbreviation
2076 (when (rmail-movemail-variant-p 'mailutils)
2077 (goto-char (point-min))
2078 (when (looking-at "[A-Z][A-Z0-9_]*:")
2079 (delete-region (point-min) (match-end 0))))
2081 (message "movemail: %s"
2082 (buffer-substring (point-min)
2083 (point-max)))
2085 (sit-for 3)
2086 nil)))))
2088 ;; At this point, TOFILE contains the name to read:
2089 ;; Either the alternate name (if we renamed)
2090 ;; or the actual inbox (if not renaming).
2091 (if (file-exists-p tofile)
2092 (let ((coding-system-for-read 'no-conversion)
2093 size)
2094 (goto-char (point-max))
2095 (setq size
2096 ;; If new mail is in Babyl format, convert it to mbox.
2097 (rmail-unrmail-new-mail-maybe
2098 tofile
2099 (nth 1 (insert-file-contents tofile))))
2100 (goto-char (point-max))
2101 ;; Make sure the read-in mbox data properly ends with a
2102 ;; blank line unless it is of size 0.
2103 (unless (zerop size)
2104 (while (not (looking-back "\n\n" (- (point) 2)))
2105 (insert "\n")))
2106 (if (not (and rmail-preserve-inbox (string= file tofile)))
2107 (setq delete-files (cons tofile delete-files)))))
2108 (message "")
2109 (setq files (cdr files)))
2110 delete-files))
2112 ;; Decode the region specified by FROM and TO by CODING.
2113 ;; If CODING is nil or an invalid coding system, decode by `undecided'.
2114 (defun rmail-decode-region (from to coding &optional destination)
2115 (if (or (not coding) (not (coding-system-p coding)))
2116 (setq coding 'undecided))
2117 ;; Use -dos decoding, to remove ^M characters left from base64 or
2118 ;; rogue qp-encoded text.
2119 (decode-coding-region
2120 from to (coding-system-change-eol-conversion coding 1) destination)
2121 ;; Don't reveal the fact we used -dos decoding, as users generally
2122 ;; will not expect the RMAIL buffer to use DOS EOL format.
2123 (cond
2124 ((null destination)
2125 (setq buffer-file-coding-system
2126 (setq last-coding-system-used
2127 (coding-system-change-eol-conversion coding 0))))
2128 ((bufferp destination)
2129 (with-current-buffer destination
2130 (setq buffer-file-coding-system
2131 (setq last-coding-system-used
2132 (coding-system-change-eol-conversion coding 0)))))))
2134 (defun rmail-ensure-blank-line ()
2135 "Ensure a message ends in a blank line.
2136 Call with point at the end of the message."
2137 (unless (bolp)
2138 (insert "\n"))
2139 (unless (looking-back "\n\n" (- (point) 2))
2140 (insert "\n")))
2142 (defun rmail-add-mbox-headers ()
2143 "Validate the RFC2822 format for the new messages.
2144 Point should be at the first new message.
2145 An error is signaled if the new messages are not RFC2822
2146 compliant.
2147 Unless an Rmail attribute header already exists, add it to the
2148 new messages. Return the number of new messages."
2149 (save-excursion
2150 (save-restriction
2151 (let ((count 0)
2152 (start (point))
2153 (value "------U-")
2154 (case-fold-search nil)
2155 (delim (concat "\n\n" rmail-unix-mail-delimiter))
2156 stop)
2157 ;; Detect an empty inbox file.
2158 (unless (= start (point-max))
2159 ;; Scan the new messages to establish a count and to ensure that
2160 ;; an attribute header is present.
2161 (if (looking-at rmail-unix-mail-delimiter)
2162 (while (not stop)
2163 ;; Determine if a new attribute header needs to be
2164 ;; added to the message.
2165 (if (search-forward "\n\n" nil t)
2166 (progn
2167 (setq count (1+ count))
2168 (narrow-to-region start (point))
2169 (unless (mail-fetch-field rmail-attribute-header)
2170 (backward-char 1)
2171 (insert rmail-attribute-header ": " value "\n"))
2172 (widen))
2173 (rmail-error-bad-format))
2174 ;; Move to the next message.
2175 (if (not (re-search-forward delim nil 'move))
2176 (setq stop t)
2177 (goto-char (match-beginning 0))
2178 (forward-char 2))
2179 (setq start (point)))
2180 (rmail-error-bad-format)))
2181 count))))
2183 (defun rmail-get-header-1 (name)
2184 "Subroutine of `rmail-get-header'.
2185 Narrow to header, call `mail-fetch-field' to find header NAME."
2186 (if (search-forward "\n\n" nil t)
2187 (progn
2188 (narrow-to-region (point-min) (point))
2189 (mail-fetch-field name))
2190 (rmail-error-bad-format)))
2192 (defun rmail-get-header (name &optional msgnum)
2193 "Return the value of message header NAME, nil if it has none.
2194 MSGNUM specifies the message number to get it from.
2195 If MSGNUM is nil, use the current message."
2196 (rmail-apply-in-message msgnum 'rmail-get-header-1 name))
2198 (defun rmail-set-header-1 (name value)
2199 "Subroutine of `rmail-set-header'.
2200 Narrow to headers, set header NAME to VALUE, replacing existing if present.
2201 VALUE nil means to remove NAME altogether.
2203 Only changes the first instance of NAME. If VALUE is multi-line,
2204 continuation lines should already be indented. VALUE should not
2205 end in a newline."
2206 (if (search-forward "\n\n" nil t)
2207 (progn
2208 (forward-char -1)
2209 (narrow-to-region (point-min) (point))
2210 ;; cf mail-fetch-field.
2211 (goto-char (point-min))
2212 (if (let ((case-fold-search t))
2213 (re-search-forward (concat "^" (regexp-quote name) "[ \t]*:")
2214 nil 'move))
2215 (let ((start (point))
2216 end)
2217 (while (and (zerop (forward-line 1))
2218 (looking-at "[ \t]")))
2219 ;; Back up over newline.
2220 (forward-char -1)
2221 (setq end (point))
2222 (goto-char start)
2223 (if value
2224 (progn
2225 (delete-region start end)
2226 (insert " " value))
2227 (delete-region (line-beginning-position) (1+ end))))
2228 ;; Not already present: insert at end of headers.
2229 (if value (insert name ": " value "\n"))))
2230 (rmail-error-bad-format)))
2232 (defun rmail-set-header (name &optional msgnum value)
2233 "Set message header NAME to VALUE in message number MSGNUM.
2234 If MSGNUM is nil, use the current message. NAME and VALUE are strings.
2235 VALUE may also be nil, meaning to remove the header."
2236 (rmail-apply-in-message msgnum 'rmail-set-header-1 name value)
2237 (with-current-buffer rmail-buffer
2238 ;; Ensure header changes get saved.
2239 ;; (Note replacing a header with an identical copy modifies.)
2240 (set-buffer-modified-p t)
2241 ;; However: don't save in mbox format over a Babyl file
2242 ;; merely because of this.
2243 (rmail-dont-modify-format)))
2245 ;;;; *** Rmail Attributes and Keywords ***
2247 (defun rmail-get-attr-names (&optional msg)
2248 "Return the message attributes in a comma separated string.
2249 MSG specifies the message number to get it from.
2250 If MSG is nil, use the current message."
2251 (let ((value (rmail-get-header rmail-attribute-header msg))
2252 (nmax (length rmail-attr-array))
2253 result temp)
2254 (when value
2255 (if (> (length value) nmax)
2256 (message "Warning: corrupt attribute header in message")
2257 (dotimes (index (length value))
2258 (setq temp (and (not (= ?- (aref value index)))
2259 (nth 1 (aref rmail-attr-array index)))
2260 result
2261 (cond
2262 ((and temp result) (format "%s, %s" result temp))
2263 (temp temp)
2264 (t result)))))
2265 result)))
2267 (defun rmail-get-keywords (&optional msg)
2268 "Return the message keywords in a comma separated string.
2269 MSG, if non-nil, identifies the message number to use.
2270 If nil, that means the current message."
2271 (rmail-get-header rmail-keyword-header msg))
2273 (defun rmail-get-labels (&optional msg)
2274 "Return a string with the labels (attributes and keywords) of msg MSG.
2275 It is put in comma-separated form.
2276 MSG, if non-nil, identifies the message number to use.
2277 If nil, that means the current message."
2278 (or msg (setq msg rmail-current-message))
2279 (let (attr-names keywords)
2280 ;; Combine the message attributes and keywords
2281 ;; into a comma-separated list.
2282 (setq attr-names (rmail-get-attr-names msg)
2283 keywords (rmail-get-keywords msg))
2284 (if (string= keywords "")
2285 (setq keywords nil))
2286 (cond
2287 ;; FIXME ? old rmail did not have spaces in the comma-separated lists.
2288 ((and attr-names keywords) (concat " " attr-names "; " keywords))
2289 (attr-names (concat " " attr-names))
2290 (keywords (concat " " keywords))
2291 (t ""))))
2293 (defun rmail-display-labels ()
2294 "Update the current messages's attributes and keywords in mode line."
2295 (let ((blurb (rmail-get-labels)))
2296 (setq mode-line-process
2297 (format " %d/%d%s"
2298 rmail-current-message rmail-total-messages blurb))))
2300 (defun rmail-get-attr-value (attr state)
2301 "Return the character value for ATTR.
2302 ATTR is a (numeric) index, an offset into the mbox attribute
2303 header value. STATE is one of nil, t, or a character value."
2304 (cond
2305 ((numberp state) state)
2306 ((not state) ?-)
2307 (t (nth 0 (aref rmail-attr-array attr)))))
2309 (defun rmail-set-attribute-1 (attr state)
2310 "Subroutine of `rmail-set-attribute'.
2311 Set Rmail attribute ATTR to STATE in `rmail-attribute-header',
2312 creating the header if necessary. Returns non-nil if a
2313 significant attribute change was made."
2314 (let ((limit (search-forward "\n\n" nil t))
2315 (value (rmail-get-attr-value attr state))
2316 (inhibit-read-only t)
2317 altered)
2318 (goto-char (point-min))
2319 (if (search-forward (concat rmail-attribute-header ": ") limit t)
2320 ;; If this message already records attributes, just change the
2321 ;; value for this one.
2322 (let ((missing (- (+ (point) attr) (line-end-position))))
2323 ;; Position point at this attribute, adding attributes if necessary.
2324 (if (> missing 0)
2325 (progn
2326 (end-of-line)
2327 (insert-char ?- missing)
2328 (backward-char 1))
2329 (forward-char attr))
2330 ;; Change this attribute.
2331 (when (/= value (char-after))
2332 (setq altered t)
2333 (delete-char 1)
2334 (insert value)))
2335 ;; Otherwise add a header line to record the attributes and set
2336 ;; all but this one to no.
2337 (let ((header-value "--------"))
2338 (aset header-value attr value)
2339 (goto-char (if limit (1- limit) (point-max)))
2340 (setq altered (/= value ?-))
2341 (insert rmail-attribute-header ": " header-value "\n")))
2342 altered))
2344 (defun rmail-set-attribute (attr state &optional msgnum)
2345 "Turn an attribute of a message on or off according to STATE.
2346 STATE is either nil or the character (numeric) value associated
2347 with the state (nil represents off and non-nil represents on).
2348 ATTR is either the index number of the attribute, or a string,
2349 both from `rmail-attr-array'. MSGNUM is message number to
2350 change; nil means current message."
2351 (let ((n 0)
2352 (nmax (length rmail-attr-array)))
2353 (while (and (stringp attr)
2354 (< n nmax))
2355 (if (string-equal attr (cadr (aref rmail-attr-array n)))
2356 (setq attr n))
2357 (setq n (1+ n))))
2358 (if (stringp attr)
2359 (error "Unknown attribute `%s'" attr))
2360 ;; Ask for confirmation before setting any attribute except `unseen'
2361 ;; if it would force a format change.
2362 (unless (= attr rmail-unseen-attr-index)
2363 (rmail-modify-format))
2364 (with-current-buffer rmail-buffer
2365 (or msgnum (setq msgnum rmail-current-message))
2366 (when (> msgnum 0)
2367 ;; The "deleted" attribute is also stored in a special vector so
2368 ;; update that too.
2369 (if (= attr rmail-deleted-attr-index)
2370 (rmail-set-message-deleted-p msgnum state))
2371 (if (prog1
2372 (rmail-apply-in-message msgnum 'rmail-set-attribute-1 attr state)
2373 (if (= msgnum rmail-current-message)
2374 (rmail-display-labels)))
2375 ;; Don't save in mbox format over a Babyl file
2376 ;; merely because of a change in `unseen' attribute.
2377 (if (= attr rmail-unseen-attr-index)
2378 (rmail-dont-modify-format)
2379 ;; Otherwise, if we modified the file text via the view buffer,
2380 ;; mark the main buffer modified too.
2381 (set-buffer-modified-p t))))))
2383 (defun rmail-message-attr-p (msg attrs)
2384 "Return non-nil if message number MSG has attributes matching regexp ATTRS."
2385 (let ((value (rmail-get-header rmail-attribute-header msg)))
2386 (and value (string-match attrs value))))
2388 (defun rmail-message-unseen-p (msgnum)
2389 "Return non-nil if message number MSGNUM has the unseen attribute."
2390 (rmail-message-attr-p msgnum "......U"))
2392 ;; FIXME rmail-get-labels does some formatting (eg leading space, `;'
2393 ;; between attributes and labels), so this might not do what you want.
2394 ;; Eg see rmail-sort-by-labels. rmail-get-labels could have an
2395 ;; optional `noformat' argument.
2396 (defun rmail-message-labels-p (msg labels)
2397 "Return non-nil if message number MSG has labels matching regexp LABELS."
2398 (string-match labels (rmail-get-labels msg)))
2400 ;;;; *** Rmail Message Selection And Support ***
2402 (defun rmail-msgend (n)
2403 "Return the end position for message number N."
2404 (marker-position (aref rmail-message-vector (1+ n))))
2406 (defun rmail-msgbeg (n)
2407 "Return the start position for message number N."
2408 (marker-position (aref rmail-message-vector n)))
2410 (defun rmail-apply-in-message (msgnum function &rest args)
2411 "Call FUNCTION on ARGS while narrowed to message MSGNUM.
2412 Point is at the start of the message.
2413 This returns what the call to FUNCTION returns.
2414 If MSGNUM is nil, use the current message."
2415 (with-current-buffer rmail-buffer
2416 (or msgnum (setq msgnum rmail-current-message))
2417 (when (> msgnum 0)
2418 (let (msgbeg msgend)
2419 (setq msgbeg (rmail-msgbeg msgnum))
2420 (setq msgend (rmail-msgend msgnum))
2421 ;; All access to the rmail-buffer's local variables is now finished...
2422 (save-excursion
2423 ;; ... so it is ok to go to a different buffer.
2424 (if (rmail-buffers-swapped-p) (set-buffer rmail-view-buffer))
2425 (save-excursion
2426 (save-restriction
2427 (widen)
2428 (goto-char msgbeg)
2429 (narrow-to-region msgbeg msgend)
2430 (apply function args))))))))
2432 ;; Unused (save for commented out code in rmailedit.el).
2433 (defun rmail-widen-to-current-msgbeg (function)
2434 "Call FUNCTION with point at start of internal data of current message.
2435 Assumes that bounds were previously narrowed to display the message in Rmail.
2436 The bounds are widened enough to move point where desired, then narrowed
2437 again afterward.
2439 FUNCTION may not change the visible text of the message, but it may
2440 change the invisible header text."
2441 (save-excursion
2442 (unwind-protect
2443 (progn
2444 (narrow-to-region (rmail-msgbeg rmail-current-message)
2445 (point-max))
2446 (goto-char (point-min))
2447 (funcall function))
2448 ;; Note: we don't use save-restriction because that does not work right
2449 ;; if changes are made outside the saved restriction
2450 ;; before that restriction is restored.
2451 (narrow-to-region (rmail-msgbeg rmail-current-message)
2452 (rmail-msgend rmail-current-message)))))
2454 ;; Manage the message vectors and counters.
2456 (defun rmail-forget-messages ()
2457 (unwind-protect
2458 (if (vectorp rmail-message-vector)
2459 (let* ((v rmail-message-vector)
2460 (n (length v)))
2461 (dotimes (i n)
2462 (if (aref v i)
2463 (move-marker (aref v i) nil)))))
2464 (setq rmail-message-vector nil)
2465 (setq rmail-msgref-vector nil)
2466 (setq rmail-deleted-vector nil)))
2468 (defun rmail-maybe-set-message-counters ()
2469 (if (not (and rmail-deleted-vector
2470 rmail-message-vector
2471 rmail-current-message
2472 rmail-total-messages))
2473 (rmail-set-message-counters)))
2475 (defun rmail-count-new-messages (&optional nomsg)
2476 "Count the number of new messages.
2477 The buffer should be narrowed to include only the new messages.
2478 Output a helpful message unless NOMSG is non-nil."
2479 (let* ((case-fold-search nil)
2480 (total-messages 0)
2481 (messages-head nil)
2482 (deleted-head nil))
2483 (or nomsg (message "Counting new messages..."))
2484 (goto-char (point-max))
2485 ;; Put at the end of messages-head
2486 ;; the entry for message N+1, which marks
2487 ;; the end of message N. (N = number of messages).
2488 (setq messages-head (list (point-marker)))
2489 (rmail-set-message-counters-counter (point-min))
2490 (setq rmail-current-message (1+ rmail-total-messages))
2491 (setq rmail-total-messages
2492 (+ rmail-total-messages total-messages))
2493 (setq rmail-message-vector
2494 (vconcat rmail-message-vector (cdr messages-head)))
2495 (aset rmail-message-vector
2496 rmail-current-message (car messages-head))
2497 (setq rmail-deleted-vector
2498 (concat rmail-deleted-vector deleted-head))
2499 (setq rmail-summary-vector
2500 (vconcat rmail-summary-vector (make-vector total-messages nil)))
2501 (setq rmail-msgref-vector
2502 (vconcat rmail-msgref-vector (make-vector total-messages nil)))
2503 ;; Fill in the new elements of rmail-msgref-vector.
2504 (let ((i (1+ (- rmail-total-messages total-messages))))
2505 (while (<= i rmail-total-messages)
2506 (aset rmail-msgref-vector i (list i))
2507 (setq i (1+ i))))
2508 (goto-char (point-min))
2509 (or nomsg (message "Counting new messages...done (%d)" total-messages))))
2511 (defun rmail-set-message-counters ()
2512 (rmail-forget-messages)
2513 (save-excursion
2514 (save-restriction
2515 (widen)
2516 (let* ((point-save (point))
2517 (total-messages 0)
2518 (messages-after-point)
2519 (case-fold-search nil)
2520 (messages-head nil)
2521 (deleted-head nil))
2522 ;; Determine how many messages follow point.
2523 (message "Counting messages...")
2524 (goto-char (point-max))
2525 ;; Put at the end of messages-head
2526 ;; the entry for message N+1, which marks
2527 ;; the end of message N. (N = number of messages).
2528 (setq messages-head (list (point-marker)))
2529 (setq messages-after-point
2530 (or (rmail-set-message-counters-counter (min (point) point-save))
2533 (setq rmail-total-messages total-messages)
2534 (setq rmail-current-message
2535 (min total-messages
2536 (max 1 (- total-messages messages-after-point))))
2538 ;; Make an element 0 in rmail-message-vector and rmail-deleted-vector
2539 ;; which will never be used.
2540 (push nil messages-head)
2541 (push ?0 deleted-head)
2542 (setq rmail-message-vector (apply 'vector messages-head)
2543 rmail-deleted-vector (concat deleted-head))
2545 (setq rmail-summary-vector (make-vector rmail-total-messages nil)
2546 rmail-msgref-vector (make-vector (1+ rmail-total-messages) nil))
2548 (let ((i 0))
2549 (while (<= i rmail-total-messages)
2550 (aset rmail-msgref-vector i (list i))
2551 (setq i (1+ i))))
2552 (let ((i 0))
2553 (while (<= i rmail-total-messages)
2554 (rmail-set-message-deleted-p i (rmail-message-attr-p i ".D"))
2555 (setq i (1+ i))))
2556 (message "Counting messages...done")))))
2559 (defsubst rmail-collect-deleted (message-end)
2560 "Collect the message deletion flags for each message.
2561 MESSAGE-END is the buffer position corresponding to the end of
2562 the message. Point is at the beginning of the message."
2563 ;; NOTE: This piece of code will be executed on a per-message basis.
2564 ;; In the face of thousands of messages, it has to be as fast as
2565 ;; possible, hence some brute force constant use is employed in
2566 ;; addition to inlining.
2567 (save-excursion
2568 (setq deleted-head
2569 (cons (if (and (search-forward (concat rmail-attribute-header ": ") message-end t)
2570 (looking-at "?D"))
2572 ?\s) deleted-head))))
2574 (defun rmail-set-message-counters-counter (&optional spot-to-find)
2575 "Collect the start positions of messages in list `messages-head'.
2576 Return the number of messages after the one containing SPOT-TO-FIND."
2577 (let ((start (point))
2578 messages-after-spot)
2579 (while (search-backward "\n\nFrom " nil t)
2580 (forward-char 2)
2581 (when (looking-at rmail-unix-mail-delimiter)
2582 (if (and (<= (point) spot-to-find)
2583 (null messages-after-spot))
2584 (setq messages-after-spot total-messages))
2585 (rmail-collect-deleted start)
2586 (setq messages-head (cons (point-marker) messages-head)
2587 total-messages (1+ total-messages)
2588 start (point))
2589 ;; Show progress after every 20 messages or so.
2590 (if (zerop (% total-messages 20))
2591 (message "Counting messages...%d" total-messages))))
2592 ;; Handle the first message, maybe.
2593 (goto-char (point-min))
2594 (unless (not (looking-at rmail-unix-mail-delimiter))
2595 (if (and (<= (point) spot-to-find)
2596 (null messages-after-spot))
2597 (setq messages-after-spot total-messages))
2598 (rmail-collect-deleted start)
2599 (setq messages-head (cons (point-marker) messages-head)
2600 total-messages (1+ total-messages)))
2601 messages-after-spot))
2603 ;; Display a message.
2605 ;;;; *** Rmail Message Formatting and Header Manipulation ***
2607 ;; This is used outside of rmail.
2608 (defun rmail-msg-is-pruned ()
2609 "Return nil if the current message is showing full headers."
2610 (with-current-buffer (if (rmail-buffers-swapped-p) rmail-view-buffer
2611 rmail-buffer)
2612 (eq rmail-header-style 'normal)))
2614 (defun rmail-toggle-header (&optional arg)
2615 "Toggle between showing full and normal message headers.
2616 With optional integer ARG, show the normal message header if ARG
2617 is greater than zero; otherwise, show it in full."
2618 (interactive "P")
2619 (let ((rmail-header-style
2620 (if (numberp arg)
2621 (if (> arg 0) 'normal 'full)
2622 (if (rmail-msg-is-pruned) 'full 'normal))))
2623 (rmail-show-message)))
2625 (defun rmail-beginning-of-message ()
2626 "Show current message starting from the beginning."
2627 (interactive)
2628 (let ((rmail-show-message-hook '((lambda () (goto-char (point-min)))))
2629 (rmail-header-style (with-current-buffer (if (rmail-buffers-swapped-p)
2630 rmail-view-buffer
2631 rmail-buffer)
2632 rmail-header-style)))
2633 (rmail-show-message rmail-current-message)))
2635 (defun rmail-end-of-message ()
2636 "Show bottom of current message."
2637 (interactive)
2638 (let ((rmail-show-message-hook '((lambda ()
2639 (goto-char (point-max))
2640 (recenter (1- (window-height))))))
2641 (rmail-header-style (with-current-buffer (if (rmail-buffers-swapped-p)
2642 rmail-view-buffer
2643 rmail-buffer)
2644 rmail-header-style)))
2645 (rmail-show-message rmail-current-message)))
2647 (defun rmail-unknown-mail-followup-to ()
2648 "Handle a \"Mail-Followup-To\" header field with an unknown mailing list.
2649 Ask the user whether to add that list name to `mail-mailing-lists'."
2650 ;; FIXME s-r not needed? Use rmail-get-header?
2651 ;; We have not narrowed to the headers at ths point?
2652 (save-restriction
2653 (let ((mail-followup-to (mail-fetch-field "mail-followup-to" nil t)))
2654 (when mail-followup-to
2655 (let ((addresses
2656 (split-string
2657 (mail-strip-quoted-names mail-followup-to)
2658 ",[[:space:]]+" t)))
2659 (dolist (addr addresses)
2660 (when (and (not (member addr mail-mailing-lists))
2661 (not
2662 ;; taken from rmailsum.el
2663 (string-match
2664 (or rmail-user-mail-address-regexp
2665 (concat "^\\("
2666 (regexp-quote (user-login-name))
2667 "\\($\\|@\\)\\|"
2668 (regexp-quote
2669 (or user-mail-address
2670 (concat (user-login-name) "@"
2671 (or mail-host-address
2672 (system-name)))))
2673 "\\>\\)"))
2674 addr))
2675 (y-or-n-p
2676 (format-message "Add `%s' to `mail-mailing-lists'? "
2677 addr)))
2678 (customize-save-variable 'mail-mailing-lists
2679 (cons addr mail-mailing-lists)))))))))
2681 (defun rmail-widen ()
2682 "Display the entire mailbox file."
2683 (interactive)
2684 (rmail-swap-buffers-maybe)
2685 (widen))
2687 (defun rmail-no-mail-p ()
2688 "Return nil if there is mail, else \"No mail.\"."
2689 (if (zerop rmail-total-messages)
2690 (save-excursion
2691 ;; Eg we deleted all the messages, so remove the old N/M mark.
2692 (with-current-buffer rmail-buffer (setq mode-line-process nil))
2693 (with-current-buffer rmail-view-buffer
2694 (erase-buffer)
2695 "No mail."))))
2697 (defun rmail-show-message (&optional n no-summary)
2698 "Show message number N (prefix argument), counting from start of file.
2699 If summary buffer is currently displayed, update current message there also.
2700 N defaults to the current message."
2701 (interactive "p")
2702 (or (eq major-mode 'rmail-mode)
2703 (switch-to-buffer rmail-buffer))
2704 ;; FIXME: Why do we swap the raw data back in?
2705 (rmail-swap-buffers-maybe)
2706 (rmail-maybe-set-message-counters)
2707 (widen)
2708 (let ((blurb (rmail-show-message-1 n)))
2709 (or (zerop rmail-total-messages)
2710 (progn
2711 (when mail-mailing-lists
2712 (rmail-unknown-mail-followup-to))
2713 (if transient-mark-mode (deactivate-mark))
2714 ;; If there is a summary buffer, try to move to this message
2715 ;; in that buffer. But don't complain if this message is
2716 ;; not mentioned in the summary. Don't do this at all if we
2717 ;; were called on behalf of cursor motion in the summary
2718 ;; buffer.
2719 (and (rmail-summary-exists) (not no-summary)
2720 (let ((curr-msg rmail-current-message))
2721 (rmail-select-summary
2722 (rmail-summary-goto-msg curr-msg t t))))
2723 (with-current-buffer rmail-buffer
2724 (rmail-auto-file))))
2725 (if blurb
2726 (message blurb))))
2728 (defun rmail-is-text-p ()
2729 "Return t if the region contains a text message, nil otherwise."
2730 (save-excursion
2731 (let ((text-regexp "\\(text\\|message\\)/")
2732 (content-type-header (mail-fetch-field "content-type")))
2733 ;; The message is text if either there is no content type header
2734 ;; (a default of "text/plain; charset=US-ASCII" is assumed) or
2735 ;; the base content type is either text or message.
2736 (or (not content-type-header)
2737 (string-match text-regexp content-type-header)))))
2739 (defcustom rmail-show-message-verbose-min 200000
2740 "Message size at which to show progress messages for displaying it."
2741 :type 'integer
2742 :group 'rmail
2743 :version "23.1")
2745 ;; FIXME?
2746 ;; rmail-show-mime-function does not unquote >From lines. Should it?
2747 (defcustom rmail-mbox-format 'mboxrd
2748 "The mbox format that your system uses.
2749 There is no way to determine this, so you should set the appropriate value.
2750 The formats quote lines containing \"From \" differently.
2751 The choices are:
2752 `mboxo' : lines that start with \"From \" quoted as \">From \"
2753 `mboxrd': lines that start with \">*From \" quoted with another \">\"
2754 The `mboxo' format is ambiguous, in that one cannot know whether
2755 a line starting with \">From \" originally had a \">\" or not.
2757 It is not critical to set this to the correct value; it only affects
2758 how Rmail displays lines starting with \">*From \" in non-MIME messages.
2760 See also `unrmail-mbox-format'."
2761 :type '(choice (const mboxrd)
2762 (const mboxro))
2763 :version "24.4"
2764 :group 'rmail-files)
2766 (defun rmail-show-message-1 (&optional msg)
2767 "Show message MSG (default: current message) using `rmail-view-buffer'.
2768 Return text to display in the minibuffer if MSG is out of
2769 range (displaying a reasonable choice as well), nil otherwise.
2770 The current mail message becomes the message displayed."
2771 (let ((mbox-buf rmail-buffer)
2772 (view-buf rmail-view-buffer)
2773 blurb beg end body-start coding-system character-coding
2774 is-text-message header-style
2775 showing-message)
2776 (if (not msg)
2777 (setq msg rmail-current-message))
2778 (unless (setq blurb (rmail-no-mail-p))
2779 (cond ((<= msg 0)
2780 (setq msg 1
2781 rmail-current-message 1
2782 blurb "No previous message"))
2783 ((> msg rmail-total-messages)
2784 (setq msg rmail-total-messages
2785 rmail-current-message rmail-total-messages
2786 blurb "No following message"))
2787 (t (setq rmail-current-message msg)))
2788 (with-current-buffer rmail-buffer
2789 (setq header-style rmail-header-style)
2790 ;; Mark the message as seen, but preserve buffer modified flag.
2791 (let ((modiff (buffer-modified-p)))
2792 (rmail-set-attribute rmail-unseen-attr-index nil)
2793 (unless modiff
2794 (restore-buffer-modified-p modiff)))
2795 ;; bracket the message in the mail
2796 ;; buffer and determine the coding system the transfer encoding.
2797 (rmail-swap-buffers-maybe)
2798 (setq beg (rmail-msgbeg msg)
2799 end (rmail-msgend msg))
2800 (when (> (- end beg) rmail-show-message-verbose-min)
2801 (setq showing-message t)
2802 (message "Showing message %d..." msg))
2803 (narrow-to-region beg end)
2804 (goto-char beg)
2805 (with-current-buffer rmail-view-buffer
2806 ;; We give the view buffer a buffer-local value of
2807 ;; rmail-header-style based on the binding in effect when
2808 ;; this function is called; `rmail-toggle-headers' can
2809 ;; inspect this value to determine how to toggle.
2810 (set (make-local-variable 'rmail-header-style) header-style)
2811 ;; In case viewing the previous message sets the paragraph
2812 ;; direction non-nil, we reset it here to allow independent
2813 ;; dynamic determination of paragraph direction in every
2814 ;; message.
2815 (setq bidi-paragraph-direction nil))
2816 (if (and rmail-enable-mime
2817 rmail-show-mime-function
2818 (re-search-forward "mime-version: 1.0" nil t))
2819 (let ((rmail-buffer mbox-buf)
2820 (rmail-view-buffer view-buf))
2821 (setq showing-message t)
2822 (message "Showing message %d..." msg)
2823 (set (make-local-variable 'rmail-mime-decoded) t)
2824 (funcall rmail-show-mime-function))
2825 (setq body-start (search-forward "\n\n" nil t))
2826 (narrow-to-region beg (point))
2827 (goto-char beg)
2828 (save-excursion
2829 (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t)
2830 (setq coding-system (intern (match-string 1)))
2831 (setq coding-system (rmail-get-coding-system))))
2832 (setq character-coding (mail-fetch-field "content-transfer-encoding")
2833 is-text-message (rmail-is-text-p))
2834 (if character-coding
2835 (setq character-coding (downcase character-coding)))
2836 (narrow-to-region beg end)
2837 ;; Decode the message body into an empty view buffer using a
2838 ;; unibyte temporary buffer where the character decoding takes
2839 ;; place.
2840 (with-current-buffer rmail-view-buffer
2841 (erase-buffer))
2842 (if (null character-coding)
2843 ;; Do it directly since that is fast.
2844 (rmail-decode-region body-start end coding-system view-buf)
2845 ;; Can this be done directly, skipping the temp buffer?
2846 (with-temp-buffer
2847 (set-buffer-multibyte nil)
2848 (insert-buffer-substring mbox-buf body-start end)
2849 (cond
2850 ((string= character-coding "quoted-printable")
2851 ;; See bug#5441.
2852 (or (mail-unquote-printable-region (point-min) (point-max)
2853 nil t 'unibyte)
2854 (message "Malformed MIME quoted-printable message")))
2855 ((and (string= character-coding "base64") is-text-message)
2856 (condition-case err
2857 (base64-decode-region (point-min) (point-max))
2858 (error (message "%s" (cdr err)))))
2859 ((eq character-coding 'uuencode)
2860 (error "uuencoded messages are not supported yet"))
2861 (t))
2862 (rmail-decode-region (point-min) (point-max)
2863 coding-system view-buf)))
2864 (with-current-buffer rmail-view-buffer
2865 ;; Prepare the separator (blank line) before the body.
2866 (goto-char (point-min))
2867 (insert "\n")
2868 ;; Unquote quoted From lines.
2869 (let ((fromline (if (eq 'mboxrd rmail-mbox-format)
2870 "^>+From "
2871 "^>From "))
2872 case-fold-search)
2873 (while (re-search-forward fromline nil t)
2874 (beginning-of-line)
2875 (delete-char 1)
2876 (forward-line)))
2877 (goto-char (point-min)))
2878 ;; Copy the headers to the front of the message view buffer.
2879 (rmail-copy-headers beg end)
2880 ;; Decode any RFC2047 encoded message headers.
2881 (if rmail-enable-mime
2882 (with-current-buffer rmail-view-buffer
2883 (rfc2047-decode-region
2884 (point-min)
2885 (progn
2886 (search-forward "\n\n" nil 'move)
2887 (point))))))
2888 ;; highlight the message, activate any URL like text and add
2889 ;; special highlighting for and quoted material.
2890 (with-current-buffer rmail-view-buffer
2891 (goto-char (point-min))
2892 (rmail-highlight-headers)
2893 ;(rmail-activate-urls)
2894 ;(rmail-process-quoted-material)
2896 ;; Update the mode-line with message status information and swap
2897 ;; the view buffer/mail buffer contents.
2898 (rmail-display-labels)
2899 (rmail-swap-buffers)
2900 (setq rmail-buffer-swapped t)
2901 (run-hooks 'rmail-show-message-hook)
2902 (when showing-message
2903 (setq blurb (format "Showing message %d...done" msg)))))
2904 blurb))
2906 (defun rmail-copy-headers (beg _end &optional ignored-headers)
2907 "Copy displayed header fields to the message viewer buffer.
2908 BEG and END marks the start and end positions of the message in
2909 the mbox buffer. If the optional argument IGNORED-HEADERS is
2910 non-nil, ignore all header fields whose names match that regexp.
2911 Otherwise, if `rmail-displayed-headers' is non-nil, copy only
2912 those header fields whose names match that regexp. Otherwise,
2913 copy all header fields whose names do not match
2914 `rmail-ignored-headers' (unless they also match
2915 `rmail-nonignored-headers'). Moves point in the message viewer
2916 buffer to the end of the headers."
2917 (let ((header-start-regexp "\n[^ \t]")
2918 lim)
2919 (with-current-buffer rmail-buffer
2920 (when (search-forward "\n\n" nil t)
2921 (forward-char -1)
2922 (save-restriction
2923 ;; Put point right after the From header line.
2924 (narrow-to-region beg (point))
2925 (goto-char (point-min))
2926 (unless (re-search-forward header-start-regexp nil t)
2927 (rmail-error-bad-format))
2928 (forward-char -1)
2929 (cond
2930 ;; Handle the case where all headers should be copied.
2931 ((eq rmail-header-style 'full)
2932 (prepend-to-buffer rmail-view-buffer beg (point-max))
2933 ;; rmail-show-message-1 expects this function to leave point
2934 ;; at the end of the headers.
2936 (let ((len (- (point-max) beg)))
2937 (with-current-buffer rmail-view-buffer
2938 (goto-char (1+ len)))))
2940 ;; Handle the case where the headers matching the displayed
2941 ;; headers regexp should be copied.
2942 ((and rmail-displayed-headers (null ignored-headers))
2943 (while (not (eobp))
2944 (save-excursion
2945 (setq lim (if (re-search-forward header-start-regexp nil t)
2946 (1+ (match-beginning 0))
2947 (point-max))))
2948 (when (looking-at rmail-displayed-headers)
2949 (append-to-buffer rmail-view-buffer (point) lim))
2950 (goto-char lim)))
2951 ;; Handle the ignored headers.
2952 ((or ignored-headers (setq ignored-headers rmail-ignored-headers))
2953 (while (and ignored-headers (not (eobp)))
2954 (save-excursion
2955 (setq lim (if (re-search-forward header-start-regexp nil t)
2956 (1+ (match-beginning 0))
2957 (point-max))))
2958 (if (and (looking-at ignored-headers)
2959 (not (and rmail-nonignored-headers
2960 (looking-at rmail-nonignored-headers))))
2961 (goto-char lim)
2962 (append-to-buffer rmail-view-buffer (point) lim)
2963 (goto-char lim))))
2964 (t (error "No headers selected for display!"))))))))
2966 (defun rmail-redecode-body (coding)
2967 "Decode the body of the current message using coding system CODING.
2968 This is useful with mail messages that have malformed or missing
2969 charset= headers.
2971 This function assumes that the current message is already decoded
2972 and displayed in the RMAIL buffer, but the coding system used to
2973 decode it was incorrect. It then decodes the message again,
2974 using the coding system CODING."
2975 (interactive "zCoding system for re-decoding this message: ")
2976 (when (not rmail-enable-mime)
2977 (with-current-buffer rmail-buffer
2978 (rmail-swap-buffers-maybe)
2979 (save-restriction
2980 (widen)
2981 (let ((msgbeg (rmail-msgbeg rmail-current-message))
2982 (msgend (rmail-msgend rmail-current-message))
2983 (buffer-read-only nil)
2984 body-start x-coding-header old-coding)
2985 (narrow-to-region msgbeg msgend)
2986 (goto-char (point-min))
2987 (unless (setq body-start (search-forward "\n\n" (point-max) 1))
2988 (error "No message body"))
2990 (save-restriction
2991 ;; Narrow to headers
2992 (narrow-to-region (point-min) body-start)
2993 (setq x-coding-header (goto-char (point-min)))
2994 (if (not (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t))
2995 (setq old-coding (rmail-get-coding-system))
2996 (setq old-coding (intern (match-string 1)))
2997 (setq x-coding-header (point)))
2998 (check-coding-system old-coding)
2999 ;; Make sure the new coding system uses the same EOL
3000 ;; conversion, to prevent ^M characters from popping up
3001 ;; all over the place.
3002 (let ((eol-type (coding-system-eol-type old-coding)))
3003 (if (numberp eol-type)
3004 (setq coding
3005 (coding-system-change-eol-conversion coding eol-type))))
3006 (when (not (coding-system-equal
3007 (coding-system-base old-coding)
3008 (coding-system-base coding)))
3009 ;; Rewrite the coding-system header.
3010 (goto-char x-coding-header)
3011 (if (> (point) (point-min))
3012 (delete-region (line-beginning-position) (point))
3013 (forward-line)
3014 (insert "\n")
3015 (forward-line -1))
3016 (insert "X-Coding-System: "
3017 (symbol-name coding))))
3018 (rmail-show-message))))))
3020 (defun rmail-highlight-headers ()
3021 "Highlight the headers specified by `rmail-highlighted-headers'.
3022 Uses the face specified by `rmail-highlight-face'."
3023 (if rmail-highlighted-headers
3024 (save-excursion
3025 (search-forward "\n\n" nil 'move)
3026 (save-restriction
3027 (narrow-to-region (point-min) (point))
3028 (let ((case-fold-search t)
3029 (inhibit-read-only t)
3030 ;; When rmail-highlight-face is removed, just
3031 ;; use 'rmail-highlight here.
3032 (face (or rmail-highlight-face
3033 (if (face-differs-from-default-p 'bold)
3034 'bold 'highlight)))
3035 ;; List of overlays to reuse.
3036 (overlays rmail-overlay-list))
3037 (goto-char (point-min))
3038 (while (re-search-forward rmail-highlighted-headers nil t)
3039 (skip-chars-forward " \t")
3040 (let ((beg (point))
3041 overlay)
3042 (while (progn (forward-line 1)
3043 (looking-at "[ \t]")))
3044 ;; Back up over newline, then trailing spaces or tabs
3045 (forward-char -1)
3046 (while (member (preceding-char) '(? ?\t))
3047 (forward-char -1))
3048 (if overlays
3049 ;; Reuse an overlay we already have.
3050 (progn
3051 (setq overlay (car overlays)
3052 overlays (cdr overlays))
3053 (overlay-put overlay 'face face)
3054 (move-overlay overlay beg (point)))
3055 ;; Make a new overlay and add it to
3056 ;; rmail-overlay-list.
3057 (setq overlay (make-overlay beg (point)))
3058 (overlay-put overlay 'face face)
3059 (setq rmail-overlay-list
3060 (cons overlay rmail-overlay-list))))))))))
3062 (defun rmail-auto-file ()
3063 "Automatically move a message into another sfolder based on criteria.
3064 This moves messages according to `rmail-automatic-folder-directives'.
3065 It only does something in the folder that `rmail-file-name' specifies.
3066 The function `rmail-show-message' calls this whenever it shows a message.
3067 This leaves a message alone if it already has the `filed' attribute."
3068 (if (or (zerop rmail-total-messages)
3069 (rmail-message-attr-p rmail-current-message "...F")
3070 (not (string= (buffer-file-name)
3071 (expand-file-name rmail-file-name))))
3072 ;; Do nothing if the message has already been filed or if there
3073 ;; are no messages.
3075 ;; Find out some basics (common fields)
3076 (let ((from (mail-fetch-field "from"))
3077 (subj (mail-fetch-field "subject"))
3078 (to (concat (mail-fetch-field "to") "," (mail-fetch-field "cc")))
3079 (d rmail-automatic-folder-directives)
3080 (directive-loop nil)
3081 (folder nil))
3082 (while d
3083 (setq folder (car (car d))
3084 directive-loop (cdr (car d)))
3085 (while (and (car directive-loop)
3086 (let ((f (cond
3087 ((string= (downcase (car directive-loop)) "from")
3088 from)
3089 ((string= (downcase (car directive-loop)) "to")
3091 ((string= (downcase (car directive-loop))
3092 "subject") subj)
3093 (t (mail-fetch-field (car directive-loop))))))
3094 ;; FIXME - shouldn't this ignore case?
3095 (and f (string-match (car (cdr directive-loop)) f))))
3096 (setq directive-loop (cdr (cdr directive-loop))))
3097 ;; If there are no directives left, then it was a complete match.
3098 (if (null directive-loop)
3099 (if (null folder)
3100 (rmail-delete-forward)
3101 (if (string= "/dev/null" folder)
3102 (rmail-delete-message)
3103 (rmail-output folder 1)
3104 (setq d nil))))
3105 (setq d (cdr d))))))
3107 ;; Simple message motion commands.
3109 (defun rmail-next-message (n)
3110 "Show following message whether deleted or not.
3111 With prefix arg N, moves forward N messages, or backward if N is negative."
3112 (interactive "p")
3113 (set-buffer rmail-buffer)
3114 (rmail-maybe-set-message-counters)
3115 (rmail-show-message (+ rmail-current-message n)))
3117 (defun rmail-previous-message (n)
3118 "Show previous message whether deleted or not.
3119 With prefix arg N, moves backward N messages, or forward if N is negative."
3120 (interactive "p")
3121 (rmail-next-message (- n)))
3123 (defun rmail-next-undeleted-message (n)
3124 "Show following non-deleted message.
3125 With prefix arg N, moves forward N non-deleted messages,
3126 or backward if N is negative.
3128 Returns t if a new message is being shown, nil otherwise."
3129 (interactive "p")
3130 (set-buffer rmail-buffer)
3131 (rmail-maybe-set-message-counters)
3132 (let ((lastwin rmail-current-message)
3133 (current rmail-current-message))
3134 (while (and (> n 0) (< current rmail-total-messages))
3135 (setq current (1+ current))
3136 (if (not (rmail-message-deleted-p current))
3137 (setq lastwin current n (1- n))))
3138 (while (and (< n 0) (> current 1))
3139 (setq current (1- current))
3140 (if (not (rmail-message-deleted-p current))
3141 (setq lastwin current n (1+ n))))
3142 (if (/= lastwin rmail-current-message)
3143 (progn (rmail-show-message lastwin)
3145 (if (< n 0)
3146 (message "No previous nondeleted message"))
3147 (if (> n 0)
3148 (message "No following nondeleted message"))
3149 nil)))
3151 (defun rmail-previous-undeleted-message (n)
3152 "Show previous non-deleted message.
3153 With prefix argument N, moves backward N non-deleted messages,
3154 or forward if N is negative."
3155 (interactive "p")
3156 (rmail-next-undeleted-message (- n)))
3158 (defun rmail-first-message ()
3159 "Show first message in file."
3160 (interactive)
3161 (rmail-maybe-set-message-counters)
3162 (rmail-show-message 1))
3164 (defun rmail-last-message ()
3165 "Show last message in file."
3166 (interactive)
3167 (rmail-maybe-set-message-counters)
3168 (rmail-show-message rmail-total-messages))
3170 (defun rmail-next-error-move (msg-pos _bad-marker)
3171 "Move to an error locus (probably grep hit) in an Rmail buffer.
3172 MSG-POS is a marker pointing at the error message in the grep buffer.
3173 BAD-MARKER is a marker that ought to point at where to move to,
3174 but probably is garbage."
3176 (let* ((message-loc (compilation--message->loc
3177 (get-text-property msg-pos 'compilation-message
3178 (marker-buffer msg-pos))))
3179 (column (car message-loc))
3180 (linenum (cadr message-loc))
3181 line-text
3183 msgnum msgbeg msgend
3184 header-field
3185 line-number-within)
3187 ;; Look at the whole Rmail file.
3188 (rmail-swap-buffers-maybe)
3190 (save-restriction
3191 (widen)
3192 (save-excursion
3193 ;; Find the line that the error message points at.
3194 (goto-char (point-min))
3195 (forward-line (1- linenum))
3196 (setq pos (point))
3198 ;; Find the text at the start of the line,
3199 ;; before the first = sign.
3200 ;; This text has a good chance of being also in the
3201 ;; decoded message.
3202 (save-excursion
3203 (skip-chars-forward "^=\n")
3204 (setq line-text (buffer-substring pos (point))))
3206 ;; Find which message this position is in,
3207 ;; and the limits of that message.
3208 (setq msgnum (rmail-what-message pos))
3209 (setq msgbeg (rmail-msgbeg msgnum))
3210 (setq msgend (rmail-msgend msgnum))
3212 ;; Find which header this locus is in,
3213 ;; or if it's in the message body,
3214 ;; and the line-based position within that.
3215 (goto-char msgbeg)
3216 (let ((header-end msgend))
3217 (if (search-forward "\n\n" nil t)
3218 (setq header-end (point)))
3219 (if (>= pos header-end)
3220 (setq line-number-within
3221 (count-lines header-end pos))
3222 (goto-char pos)
3223 (unless (looking-at "^[^ \t]")
3224 (re-search-backward "^[^ \t]"))
3225 (looking-at "[^:\n]*[:\n]")
3226 (setq header-field (match-string 0)
3227 line-number-within (count-lines (point) pos))))))
3229 ;; Display the right message.
3230 (rmail-show-message msgnum)
3232 ;; Move to the right position within the displayed message.
3233 ;; Or at least try. The decoded message's lines may not
3234 ;; correspond to the lines in the inbox file.
3235 (goto-char (point-min))
3236 (if header-field
3237 (progn
3238 (re-search-forward (concat "^" (regexp-quote header-field)) nil t)
3239 (forward-line line-number-within))
3240 (search-forward "\n\n" nil t)
3241 (if (re-search-forward (concat "^" (regexp-quote line-text)) nil t)
3242 (goto-char (match-beginning 0))))
3243 (if (eobp)
3244 ;; If the decoded message doesn't have enough lines,
3245 ;; go to the beginning rather than the end.
3246 (goto-char (point-min))
3247 ;; Otherwise, go to the right column.
3248 (if column
3249 (forward-char column)))))
3251 (defun rmail-what-message (&optional pos)
3252 "Return message number POS (or point) is in."
3253 (let* ((high rmail-total-messages)
3254 (mid (/ high 2))
3255 (low 1)
3256 (where (or pos
3257 (with-current-buffer (if (rmail-buffers-swapped-p)
3258 rmail-view-buffer
3259 (current-buffer))
3260 (point)))))
3261 (while (> (- high low) 1)
3262 (if (>= where (rmail-msgbeg mid))
3263 (setq low mid)
3264 (setq high mid))
3265 (setq mid (+ low (/ (- high low) 2))))
3266 (if (>= where (rmail-msgbeg high)) high low)))
3268 ;; Searching in Rmail file.
3270 (defun rmail-search-message (msg regexp)
3271 "Return non-nil, if for message number MSG, regexp REGEXP matches."
3272 ;; This is adequate because its only caller, rmail-search,
3273 ;; unswaps the buffers.
3274 (goto-char (rmail-msgbeg msg))
3275 (if (and rmail-enable-mime
3276 rmail-search-mime-message-function)
3277 (funcall rmail-search-mime-message-function msg regexp)
3278 (re-search-forward regexp (rmail-msgend msg) t)))
3280 (defvar rmail-search-last-regexp nil)
3281 (defun rmail-search (regexp &optional n)
3282 "Show message containing next match for REGEXP (but not the current msg).
3283 Prefix argument gives repeat count; negative argument means search
3284 backwards (through earlier messages).
3285 Interactively, empty argument means use same regexp used last time."
3286 (interactive
3287 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
3288 (prompt
3289 (concat (if reversep "Reverse " "") "Rmail search (regexp"))
3290 regexp)
3291 (setq prompt
3292 (concat prompt
3293 (if rmail-search-last-regexp
3294 (concat ", default "
3295 rmail-search-last-regexp "): ")
3296 "): ")))
3297 (setq regexp (read-string prompt))
3298 (cond ((not (equal regexp ""))
3299 (setq rmail-search-last-regexp regexp))
3300 ((not rmail-search-last-regexp)
3301 (error "No previous Rmail search string")))
3302 (list rmail-search-last-regexp
3303 (prefix-numeric-value current-prefix-arg))))
3304 (or n (setq n 1))
3305 (message "%sRmail search for %s..."
3306 (if (< n 0) "Reverse " "")
3307 regexp)
3308 (set-buffer rmail-buffer)
3309 (let ((orig-message rmail-current-message)
3310 (msg rmail-current-message)
3311 (reversep (< n 0))
3312 (opoint (if (rmail-buffers-swapped-p) (point)))
3313 found)
3314 (rmail-swap-buffers-maybe)
3315 (rmail-maybe-set-message-counters)
3316 (widen)
3317 (unwind-protect
3318 (while (/= n 0)
3319 ;; Check messages one by one, advancing message number up or
3320 ;; down but searching forward through each message.
3321 (if reversep
3322 (while (and (null found) (> msg 1))
3323 (setq msg (1- msg)
3324 found (rmail-search-message msg regexp)))
3325 (while (and (null found) (< msg rmail-total-messages))
3326 (setq msg (1+ msg)
3327 found (rmail-search-message msg regexp))))
3328 (setq n (+ n (if reversep 1 -1))))
3329 (if found
3330 (progn
3331 (rmail-show-message msg)
3332 ;; Search forward (if this is a normal search) or backward
3333 ;; (if this is a reverse search) through this message to
3334 ;; position point. This search may fail because REGEXP
3335 ;; was found in the hidden portion of this message. In
3336 ;; that case, move point to the beginning of visible
3337 ;; portion.
3338 (if reversep
3339 (progn
3340 (goto-char (point-max))
3341 (re-search-backward regexp nil 'move))
3342 (goto-char (point-min))
3343 (re-search-forward regexp nil t))
3344 (message "%sRmail search for %s...done"
3345 (if reversep "Reverse " "")
3346 regexp))
3347 (rmail-show-message orig-message)
3348 (if opoint (goto-char opoint))
3349 (ding)
3350 (message "Search failed: %s" regexp)))))
3352 (defun rmail-search-backwards (regexp &optional n)
3353 "Show message containing previous match for REGEXP.
3354 Prefix argument gives repeat count; negative argument means search
3355 forward (through later messages).
3356 Interactively, empty argument means use same regexp used last time."
3357 (interactive
3358 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
3359 (prompt
3360 (concat (if reversep "Reverse " "") "Rmail search (regexp"))
3361 regexp)
3362 (setq prompt
3363 (concat prompt
3364 (if rmail-search-last-regexp
3365 (concat ", default "
3366 rmail-search-last-regexp "): ")
3367 "): ")))
3368 (setq regexp (read-string prompt))
3369 (cond ((not (equal regexp ""))
3370 (setq rmail-search-last-regexp regexp))
3371 ((not rmail-search-last-regexp)
3372 (error "No previous Rmail search string")))
3373 (list rmail-search-last-regexp
3374 (prefix-numeric-value current-prefix-arg))))
3375 (rmail-search regexp (- (or n 1))))
3377 ;; Scan for attributes, and compare subjects.
3379 (defun rmail-first-unseen-message ()
3380 "Return message number of first message which has `unseen' attribute."
3381 (rmail-maybe-set-message-counters)
3382 (let ((current 1)
3383 found)
3384 (save-restriction
3385 (widen)
3386 (while (and (not found) (<= current rmail-total-messages))
3387 (if (rmail-message-attr-p current "......U")
3388 (setq found current))
3389 (setq current (1+ current))))
3390 found))
3392 (defun rmail-simplified-subject (&optional msgnum)
3393 "Return the simplified subject of message MSGNUM (or current message).
3394 Simplifying the subject means stripping leading and trailing whitespace,
3395 and typical reply prefixes such as Re:."
3396 (let ((subject (or (rmail-get-header "Subject" msgnum) "")))
3397 (setq subject (rfc2047-decode-string subject))
3398 (if (string-match "\\`[ \t]+" subject)
3399 (setq subject (substring subject (match-end 0))))
3400 (if (string-match rmail-reply-regexp subject)
3401 (setq subject (substring subject (match-end 0))))
3402 (if (string-match "[ \t]+\\'" subject)
3403 (setq subject (substring subject 0 (match-beginning 0))))
3404 ;; If Subject is long, mailers will break it into several lines at
3405 ;; arbitrary places, so normalize whitespace by replacing every
3406 ;; run of whitespace characters with a single space.
3407 (setq subject (replace-regexp-in-string "[ \t\n]+" " " subject))
3408 subject))
3410 (defun rmail-simplified-subject-regexp ()
3411 "Return a regular expression matching the current simplified subject.
3412 The idea is to match it against simplified subjects of other messages."
3413 (let ((subject (rmail-simplified-subject)))
3414 (setq subject (regexp-quote subject))
3415 ;; Hide commas so it will work ok if parsed as a comma-separated list
3416 ;; of regexps.
3417 (setq subject
3418 (replace-regexp-in-string "," "\054" subject t t))
3419 (concat "\\`" subject "\\'")))
3421 (defun rmail-next-same-subject (n)
3422 "Go to the next mail message having the same subject header.
3423 With prefix argument N, do this N times.
3424 If N is negative, go backwards instead."
3425 (interactive "p")
3426 (let ((subject (rmail-simplified-subject))
3427 (forward (> n 0))
3428 (i rmail-current-message)
3429 found)
3430 (while (and (/= n 0)
3431 (if forward
3432 (< i rmail-total-messages)
3433 (> i 1)))
3434 (let (done)
3435 (while (and (not done)
3436 (if forward
3437 (< i rmail-total-messages)
3438 (> i 1)))
3439 (setq i (if forward (1+ i) (1- i)))
3440 (setq done (string-equal subject (rmail-simplified-subject i))))
3441 (if done (setq found i)))
3442 (setq n (if forward (1- n) (1+ n))))
3443 (if found
3444 (rmail-show-message found)
3445 (error "No %s message with same subject"
3446 (if forward "following" "previous")))))
3448 (defun rmail-previous-same-subject (n)
3449 "Go to the previous mail message having the same subject header.
3450 With prefix argument N, do this N times.
3451 If N is negative, go forwards instead."
3452 (interactive "p")
3453 (rmail-next-same-subject (- n)))
3455 ;;;; *** Rmail Message Deletion Commands ***
3457 (defun rmail-message-deleted-p (n)
3458 "Return non-nil if message number N is deleted (in `rmail-deleted-vector')."
3459 (= (aref rmail-deleted-vector n) ?D))
3461 (defun rmail-set-message-deleted-p (n state)
3462 "Set the deleted state of message number N (in `rmail-deleted-vector').
3463 STATE non-nil means mark as deleted."
3464 (aset rmail-deleted-vector n (if state ?D ?\s)))
3466 (defun rmail-delete-message ()
3467 "Delete this message and stay on it."
3468 (interactive)
3469 (rmail-set-attribute rmail-deleted-attr-index t)
3470 (run-hooks 'rmail-delete-message-hook)
3471 (let ((del-msg rmail-current-message))
3472 (if (rmail-summary-exists)
3473 (rmail-select-summary
3474 (rmail-summary-mark-deleted del-msg)))))
3476 (defun rmail-undelete-previous-message (count)
3477 "Back up to deleted message, select it, and undelete it."
3478 (interactive "p")
3479 (set-buffer rmail-buffer)
3480 (dotimes (_ count)
3481 (let ((msg rmail-current-message))
3482 (while (and (> msg 0)
3483 (not (rmail-message-deleted-p msg)))
3484 (setq msg (1- msg)))
3485 (if (= msg 0)
3486 (error "No previous deleted message")
3487 (if (/= msg rmail-current-message)
3488 (rmail-show-message msg))
3489 (rmail-set-attribute rmail-deleted-attr-index nil)
3490 (if (rmail-summary-exists)
3491 (with-current-buffer rmail-summary-buffer
3492 (rmail-summary-mark-undeleted msg))))))
3493 (rmail-maybe-display-summary))
3495 (defun rmail-delete-forward (&optional count)
3496 "Delete this message and move to next nondeleted one.
3497 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
3498 Optional argument COUNT (interactively, prefix argument) is a repeat count;
3499 negative argument means move backwards instead of forwards.
3501 Returns t if a new message is displayed after the delete, or nil otherwise."
3502 (interactive "p")
3503 (if (not count) (setq count 1))
3504 (let (value backward)
3505 (if (< count 0)
3506 (setq count (- count) backward t))
3507 (dotimes (_ count)
3508 (rmail-set-attribute rmail-deleted-attr-index t)
3509 (run-hooks 'rmail-delete-message-hook)
3510 (let ((del-msg rmail-current-message))
3511 (if (rmail-summary-exists)
3512 (rmail-select-summary
3513 (rmail-summary-mark-deleted del-msg)))
3514 (setq value (rmail-next-undeleted-message (if backward -1 1)))))
3515 (rmail-maybe-display-summary)
3516 value))
3518 (defun rmail-delete-backward (&optional count)
3519 "Delete this message and move to previous nondeleted one.
3520 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
3521 Optional argument COUNT (interactively, prefix argument) is a repeat count;
3522 negative argument means move forwards instead of backwards.
3524 Returns t if a new message is displayed after the delete, or nil otherwise."
3526 (interactive "p")
3527 (if (not count) (setq count 1))
3528 (rmail-delete-forward (- count)))
3530 ;; Expunging.
3532 ;; Compute the message number a given message would have after expunging.
3533 ;; The present number of the message is OLDNUM.
3534 ;; DELETEDVEC should be rmail-deleted-vector.
3535 ;; The value is nil for a message that would be deleted.
3536 (defun rmail-msg-number-after-expunge (deletedvec oldnum)
3537 (if (or (null oldnum) (= (aref deletedvec oldnum) ?D))
3539 (let ((i 0)
3540 (newnum 0))
3541 (while (< i oldnum)
3542 (if (/= (aref deletedvec i) ?D)
3543 (setq newnum (1+ newnum)))
3544 (setq i (1+ i)))
3545 newnum)))
3547 (defun rmail-expunge-confirmed ()
3548 "Return t if expunge is needed and desirable.
3549 If `rmail-confirm-expunge' is non-nil, ask user to confirm."
3550 (set-buffer rmail-buffer)
3551 (and (stringp rmail-deleted-vector)
3552 (string-match "D" rmail-deleted-vector)
3553 (if rmail-confirm-expunge
3554 (funcall rmail-confirm-expunge
3555 "Erase deleted messages from Rmail file? ")
3556 t)))
3558 (defun rmail-only-expunge (&optional dont-show)
3559 "Actually erase all deleted messages in the file."
3560 (interactive)
3561 (rmail-swap-buffers-maybe)
3562 (set-buffer rmail-buffer)
3563 (message "Expunging deleted messages...")
3564 ;; Discard all undo records for this buffer.
3565 (or (eq buffer-undo-list t)
3566 (setq buffer-undo-list nil))
3567 (rmail-maybe-set-message-counters)
3568 (let* ((omax (- (buffer-size) (point-max)))
3569 (omin (- (buffer-size) (point-min)))
3570 (opoint (if (and (> rmail-current-message 0)
3571 (rmail-message-deleted-p rmail-current-message))
3573 (if rmail-enable-mime
3574 (with-current-buffer rmail-view-buffer
3575 (- (point)(point-min)))
3576 (- (point) (point-min)))))
3577 (messages-head (cons (aref rmail-message-vector 0) nil))
3578 (messages-tail messages-head)
3579 ;; Don't make any undo records for the expunging.
3580 (buffer-undo-list t)
3581 (win))
3582 (unwind-protect
3583 (save-excursion
3584 (widen)
3585 (goto-char (point-min))
3586 (let ((counter 0)
3587 (number 1)
3588 new-summary
3589 (new-msgref (list (list 0)))
3590 (buffer-read-only nil)
3591 (total rmail-total-messages)
3592 (new-message-number rmail-current-message)
3593 (messages rmail-message-vector)
3594 (deleted rmail-deleted-vector)
3595 (summary rmail-summary-vector))
3596 (setq rmail-total-messages nil
3597 rmail-current-message nil
3598 rmail-message-vector nil
3599 rmail-deleted-vector nil
3600 rmail-summary-vector nil)
3602 (while (<= number total)
3603 (if (= (aref deleted number) ?D)
3604 (progn
3605 (delete-region (aref messages number)
3606 (aref messages (1+ number)))
3607 (move-marker (aref messages number) nil)
3608 (if (> new-message-number counter)
3609 (setq new-message-number (1- new-message-number))))
3610 (setq counter (1+ counter))
3611 (setq messages-tail
3612 (setcdr messages-tail
3613 (cons (aref messages number) nil)))
3614 (setq new-summary
3615 (cons (if (= counter number) (aref summary (1- number)))
3616 new-summary))
3617 (setq new-msgref
3618 (cons (aref rmail-msgref-vector number)
3619 new-msgref))
3620 (setcar (car new-msgref) counter))
3621 (if (zerop (% (setq number (1+ number)) 20))
3622 (message "Expunging deleted messages...%d" number)))
3623 (setq messages-tail
3624 (setcdr messages-tail
3625 (cons (aref messages number) nil)))
3626 (setq rmail-current-message new-message-number
3627 rmail-total-messages counter
3628 rmail-message-vector (apply 'vector messages-head)
3629 rmail-deleted-vector (make-string (1+ counter) ?\s)
3630 rmail-summary-vector (vconcat (nreverse new-summary))
3631 rmail-msgref-vector (apply 'vector (nreverse new-msgref))
3632 win t)))
3633 (message "Expunging deleted messages...done")
3634 (if (not win)
3635 (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
3636 (if (not dont-show)
3637 (rmail-show-message (min rmail-current-message rmail-total-messages)))
3638 (if rmail-enable-mime
3639 (goto-char (+ (point-min) opoint))
3640 (goto-char (+ (point) opoint))))))
3642 ;; The DONT-SHOW argument is new in 23. Does not seem very important.
3643 (defun rmail-expunge (&optional dont-show)
3644 "Erase deleted messages from Rmail file and summary buffer.
3645 This always shows a message (so as not to leave the Rmail buffer
3646 unswapped), and always updates any summary (so that it remains
3647 consistent with the Rmail buffer). If DONT-SHOW is non-nil, it
3648 does not pop any summary buffer."
3649 (interactive)
3650 (when (rmail-expunge-confirmed)
3651 (rmail-modify-format)
3652 (let ((was-deleted (rmail-message-deleted-p rmail-current-message))
3653 (was-swapped (rmail-buffers-swapped-p)))
3654 (rmail-only-expunge t)
3655 ;; We always update the summary buffer, so that the contents
3656 ;; remain consistent with the rmail buffer.
3657 ;; The only difference is, in the dont-show case, we use a
3658 ;; cut-down version of rmail-select-summary that does not pop
3659 ;; the summary buffer. It's only used by rmail-quit, which is
3660 ;; just going to bury any summary immediately after. If we made
3661 ;; rmail-quit bury the summary first, dont-show could be removed.
3662 ;; But the expunge might not be confirmed...
3663 (if (rmail-summary-exists)
3664 (if dont-show
3665 (let ((total rmail-total-messages))
3666 (with-current-buffer rmail-summary-buffer
3667 (let ((rmail-total-messages total))
3668 (rmail-update-summary))))
3669 (rmail-select-summary (rmail-update-summary))))
3670 ;; We always show a message, because (rmail-only-expunge t)
3671 ;; leaves the rmail buffer unswapped.
3672 ;; If we expunged the current message, a new one is current now,
3673 ;; so show it. If we weren't showing a message, show it.
3674 (if (or was-deleted (not was-swapped))
3675 (rmail-show-message-1 rmail-current-message)
3676 ;; We can just show the same message that was being shown before.
3677 (rmail-display-labels)
3678 (rmail-swap-buffers)
3679 (setq rmail-buffer-swapped t)))))
3681 ;;;; *** Rmail Mailing Commands ***
3683 (defun rmail-yank-current-message (buffer)
3684 "Yank into the current buffer the current message of Rmail buffer BUFFER.
3685 If BUFFER is swapped with its message viewer buffer, yank out of BUFFER.
3686 If BUFFER is not swapped, yank out of its message viewer buffer."
3687 (with-current-buffer buffer
3688 (unless (rmail-buffers-swapped-p)
3689 (setq buffer rmail-view-buffer)))
3690 (insert-buffer-substring buffer)
3691 ;; If they yank the text of BUFFER, the encoding of BUFFER is a
3692 ;; better default for the reply message than the default value of
3693 ;; buffer-file-coding-system.
3694 (and (coding-system-equal (default-value 'buffer-file-coding-system)
3695 buffer-file-coding-system)
3696 (setq buffer-file-coding-system
3697 (coding-system-change-text-conversion
3698 buffer-file-coding-system (coding-system-base
3699 (with-current-buffer buffer
3700 buffer-file-coding-system))))))
3702 (defun rmail-start-mail (&optional noerase to subject in-reply-to cc
3703 replybuffer sendactions same-window
3704 other-headers)
3705 (let ((switch-function
3706 (cond (same-window nil)
3707 (rmail-mail-new-frame 'switch-to-buffer-other-frame)
3708 (t 'switch-to-buffer-other-window)))
3709 yank-action)
3710 (if replybuffer
3711 ;; The function used here must behave like insert-buffer wrt
3712 ;; point and mark (see doc of sc-cite-original).
3713 (setq yank-action
3714 `(rmail-yank-current-message ,replybuffer)))
3715 (push (cons "cc" cc) other-headers)
3716 (push (cons "in-reply-to" in-reply-to) other-headers)
3717 (setq other-headers
3718 (mapcar #'(lambda (elt)
3719 (cons (car elt) (if (stringp (cdr elt))
3720 (rfc2047-decode-string (cdr elt)))))
3721 other-headers))
3722 (if (stringp to) (setq to (rfc2047-decode-string to)))
3723 (if (stringp in-reply-to)
3724 (setq in-reply-to (rfc2047-decode-string in-reply-to)))
3725 (if (stringp cc) (setq cc (rfc2047-decode-string cc)))
3726 (if (stringp subject) (setq subject (rfc2047-decode-string subject)))
3727 (prog1
3728 (compose-mail to subject other-headers noerase
3729 switch-function yank-action sendactions
3730 (if replybuffer `(rmail-mail-return ,replybuffer)))
3731 (if (eq switch-function 'switch-to-buffer-other-frame)
3732 ;; This is not a standard frame parameter; nothing except
3733 ;; sendmail.el looks at it.
3734 (modify-frame-parameters (selected-frame)
3735 '((mail-dedicated-frame . t)))))))
3737 (defun rmail-mail-return (&optional newbuf)
3738 "Try to return to Rmail from the mail window.
3739 If optional argument NEWBUF is specified, it is the Rmail buffer
3740 to switch to."
3741 (cond
3742 ;; If there is only one visible frame with no special handling,
3743 ;; consider deleting the mail window to return to Rmail.
3744 ((or (null (delq (selected-frame) (visible-frame-list)))
3745 (not (or (window-dedicated-p (frame-selected-window))
3746 (and pop-up-frames (one-window-p))
3747 (cdr (assq 'mail-dedicated-frame
3748 (frame-parameters))))))
3749 (let (rmail-flag summary-buffer)
3750 (unless (one-window-p)
3751 (with-current-buffer
3752 (window-buffer (next-window (selected-window) 'not))
3753 (setq rmail-flag (eq major-mode 'rmail-mode))
3754 (setq summary-buffer
3755 (and (boundp 'mail-bury-selects-summary)
3756 mail-bury-selects-summary
3757 (boundp 'rmail-summary-buffer)
3758 rmail-summary-buffer
3759 (buffer-name rmail-summary-buffer)
3760 (not (get-buffer-window rmail-summary-buffer))
3761 rmail-summary-buffer))))
3762 (cond ((null rmail-flag)
3763 ;; If the Rmail buffer is not in the next window, switch
3764 ;; directly to the Rmail buffer specified by NEWBUF.
3765 (if (buffer-live-p newbuf)
3766 (switch-to-buffer newbuf)))
3767 ;; If the Rmail buffer is in the next window, switch to
3768 ;; the summary buffer if `mail-bury-selects-summary' is
3769 ;; non-nil. Otherwise just delete this window.
3770 (summary-buffer
3771 (switch-to-buffer summary-buffer))
3773 (delete-window)))))
3774 ;; If the frame was probably made for this buffer, the user
3775 ;; probably wants to delete it now.
3776 ((display-multi-frame-p)
3777 (delete-frame))
3778 ;; The previous frame is where normally they have the Rmail buffer
3779 ;; displayed.
3780 (t (other-frame -1))))
3782 (defun rmail-mail ()
3783 "Send mail in another window.
3784 While composing the message, use \\[mail-yank-original] to yank the
3785 original message into it."
3786 (interactive)
3787 (rmail-start-mail nil nil nil nil nil rmail-buffer))
3789 ;; FIXME should complain if there is nothing to continue.
3790 (defun rmail-continue ()
3791 "Continue composing outgoing message previously being composed."
3792 (interactive)
3793 (rmail-start-mail t))
3795 (defun rmail-reply (just-sender)
3796 "Reply to the current message.
3797 Normally include CC: to all other recipients of original message;
3798 prefix argument means ignore them. While composing the reply,
3799 use \\[mail-yank-original] to yank the original message into it."
3800 (interactive "P")
3801 (if (zerop rmail-current-message)
3802 (error "There is no message to reply to"))
3803 (let (from reply-to cc subject date to message-id references
3804 ;; resent-to resent-cc resent-reply-to
3805 (msgnum rmail-current-message))
3806 (rmail-apply-in-message
3807 rmail-current-message
3808 (lambda ()
3809 (search-forward "\n\n" nil 'move)
3810 (narrow-to-region (point-min) (point))
3811 (setq from (mail-fetch-field "from")
3812 reply-to (or (mail-fetch-field "mail-reply-to" nil t)
3813 (mail-fetch-field "reply-to" nil t)
3814 from)
3815 subject (mail-fetch-field "subject")
3816 date (mail-fetch-field "date")
3817 message-id (mail-fetch-field "message-id")
3818 references (mail-fetch-field "references" nil nil t)
3819 ;; Bug#512. It's inappropriate to reply to these addresses.
3820 ;;resent-reply-to (mail-fetch-field "resent-reply-to" nil t)
3821 ;;resent-cc (and (not just-sender)
3822 ;; (mail-fetch-field "resent-cc" nil t))
3823 ;;resent-to (or (mail-fetch-field "resent-to" nil t) "")
3824 ;;resent-subject (mail-fetch-field "resent-subject")
3825 ;;resent-date (mail-fetch-field "resent-date")
3826 ;;resent-message-id (mail-fetch-field "resent-message-id")
3828 (unless just-sender
3829 (if (mail-fetch-field "mail-followup-to" nil t)
3830 ;; If this header field is present, use it instead of the
3831 ;; To and CC fields.
3832 (setq to (mail-fetch-field "mail-followup-to" nil t))
3833 (setq cc (or (mail-fetch-field "cc" nil t) "")
3834 to (or (mail-fetch-field "to" nil t) ""))))))
3835 ;; Merge the resent-to and resent-cc into the to and cc.
3836 ;; Bug#512. It's inappropriate to reply to these addresses.
3837 ;;(if (and resent-to (not (equal resent-to "")))
3838 ;; (setq to (if (not (equal to ""))
3839 ;; (concat to ", " resent-to)
3840 ;; resent-to)))
3841 ;;(if (and resent-cc (not (equal resent-cc "")))
3842 ;; (setq cc (if (not (equal cc ""))
3843 ;; (concat cc ", " resent-cc)
3844 ;; resent-cc)))
3845 ;; Add `Re: ' to subject if not there already.
3846 (and (stringp subject)
3847 (setq subject (rfc2047-decode-string subject)
3848 subject
3849 (concat rmail-reply-prefix
3850 (if (let ((case-fold-search t))
3851 (string-match rmail-reply-regexp subject))
3852 (substring subject (match-end 0))
3853 subject))))
3854 (rmail-start-mail
3856 ;; Using mail-strip-quoted-names is undesirable with newer mailers
3857 ;; since they can handle the names unstripped.
3858 ;; I don't know whether there are other mailers that still
3859 ;; need the names to be stripped.
3860 ;;; (mail-strip-quoted-names reply-to)
3861 ;; Remove unwanted names from reply-to, since Mail-Followup-To
3862 ;; header causes all the names in it to wind up in reply-to, not
3863 ;; in cc. But if what's left is an empty list, use the original.
3864 (let* ((reply-to-list (mail-dont-reply-to reply-to)))
3865 (if (string= reply-to-list "") reply-to reply-to-list))
3866 subject
3867 (rmail-make-in-reply-to-field from date message-id)
3868 (if just-sender
3870 ;; `mail-dont-reply-to' doesn't need `mail-strip-quoted-names'.
3871 (let* ((cc-list (mail-dont-reply-to
3872 (mail-strip-quoted-names
3873 (if (null cc) to (concat to ", " cc))))))
3874 (if (string= cc-list "") nil cc-list)))
3875 rmail-buffer
3876 (list (list 'rmail-mark-message
3877 rmail-buffer
3878 (with-current-buffer rmail-buffer
3879 (aref rmail-msgref-vector msgnum))
3880 rmail-answered-attr-index))
3882 (if (or references message-id)
3883 (list (cons "References" (if references
3884 (concat
3885 (mapconcat 'identity references " ")
3886 " " message-id)
3887 message-id)))))))
3889 (defun rmail-mark-message (buffer msgnum-list attribute)
3890 "Give BUFFER's message number in MSGNUM-LIST the attribute ATTRIBUTE.
3891 This is use in the send-actions for message buffers.
3892 MSGNUM-LIST is a list of the form (MSGNUM)
3893 which is an element of rmail-msgref-vector."
3894 (with-current-buffer buffer
3895 (if (car msgnum-list)
3896 (rmail-set-attribute attribute t (car msgnum-list)))))
3898 (defun rmail-make-in-reply-to-field (from date message-id)
3899 (cond ((not from)
3900 (if message-id
3901 message-id
3902 nil))
3903 (mail-use-rfc822
3904 (require 'rfc822)
3905 (let ((tem (car (rfc822-addresses from))))
3906 (if message-id
3907 (if (or (not tem)
3908 (string-match
3909 (regexp-quote (if (string-match "@[^@]*\\'" tem)
3910 (substring tem 0
3911 (match-beginning 0))
3912 tem))
3913 message-id))
3914 ;; missing From, or Message-ID is sufficiently informative
3915 message-id
3916 (concat message-id " (" tem ")"))
3917 ;; Message has no Message-ID field.
3918 ;; Copy TEM, discarding text properties.
3919 (setq tem (copy-sequence tem))
3920 (set-text-properties 0 (length tem) nil tem)
3921 (setq tem (copy-sequence tem))
3922 ;; Use prin1 to fake RFC822 quoting
3923 (let ((field (prin1-to-string tem)))
3924 ;; Wrap it in parens to make it a comment according to RFC822
3925 (if date
3926 (concat "(" field "'s message of " date ")")
3927 (concat "(" field ")"))))))
3928 ((let* ((foo "[^][\000-\037()<>@,;:\\\" ]+")
3929 (bar "[^][\000-\037()<>@,;:\\\"]+"))
3930 ;; These strings both match all non-ASCII characters.
3931 (or (string-match (concat "\\`[ \t]*\\(" bar
3932 "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
3933 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
3934 from)
3935 (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
3936 bar "\\))[ \t]*\\'")
3937 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
3938 from)))
3939 (let ((start (match-beginning 1))
3940 (end (match-end 1)))
3941 ;; Trim whitespace which above regexp match allows
3942 (while (and (< start end)
3943 (memq (aref from start) '(?\t ?\s)))
3944 (setq start (1+ start)))
3945 (while (and (< start end)
3946 (memq (aref from (1- end)) '(?\t ?\s)))
3947 (setq end (1- end)))
3948 (let ((field (substring from start end)))
3949 (if date (setq field (concat "message from " field " on " date)))
3950 (if message-id
3951 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
3952 (concat message-id " (" field ")")
3953 ;; Wrap in parens to make it a comment, for RFC822.
3954 (concat "(" field ")")))))
3956 ;; If we can't kludge it simply, do it correctly
3957 (let ((mail-use-rfc822 t))
3958 (rmail-make-in-reply-to-field from date message-id)))))
3960 (defun rmail-forward (resend)
3961 "Forward the current message to another user.
3962 With prefix argument, \"resend\" the message instead of forwarding it;
3963 see the documentation of `rmail-resend'."
3964 (interactive "P")
3965 (if (zerop rmail-current-message)
3966 (error "No message to forward"))
3967 (if resend
3968 (call-interactively 'rmail-resend)
3969 (let ((forward-buffer rmail-buffer)
3970 (msgnum rmail-current-message)
3971 (subject (concat "["
3972 (let ((from (or (mail-fetch-field "From")
3973 ;; FIXME - huh?
3974 (mail-fetch-field ">From"))))
3975 (if from
3976 (concat (mail-strip-quoted-names from) ": ")
3977 ""))
3978 (or (mail-fetch-field "Subject") "")
3979 "]")))
3980 (if (rmail-start-mail
3981 nil nil subject nil nil rmail-buffer
3982 (list (list 'rmail-mark-message
3983 forward-buffer
3984 (with-current-buffer rmail-buffer
3985 (aref rmail-msgref-vector msgnum))
3986 rmail-forwarded-attr-index))
3987 ;; If only one window, use it for the mail buffer.
3988 ;; Otherwise, use another window for the mail buffer
3989 ;; so that the Rmail buffer remains visible
3990 ;; and sending the mail will get back to it.
3991 (and (not rmail-mail-new-frame) (one-window-p t)))
3992 ;; The mail buffer is now current.
3993 (save-excursion
3994 ;; Insert after header separator--before signature if any.
3995 (rfc822-goto-eoh)
3996 (forward-line 1)
3997 (if (and rmail-enable-mime rmail-enable-mime-composing
3998 rmail-insert-mime-forwarded-message-function)
3999 (prog1
4000 (funcall rmail-insert-mime-forwarded-message-function
4001 forward-buffer)
4002 ;; rmail-insert-mime-forwarded-message-function
4003 ;; works by inserting MML tags into forward-buffer.
4004 ;; The MUA will need to convert it to MIME before
4005 ;; sending. mail-encode-mml tells them to do that.
4006 ;; message.el does that automagically.
4007 (or (eq mail-user-agent 'message-user-agent)
4008 (setq mail-encode-mml t)))
4009 (insert "------- Start of forwarded message -------\n")
4010 ;; Quote lines with `- ' if they start with `-'.
4011 (let ((beg (point)) end)
4012 (setq end (point-marker))
4013 (set-marker-insertion-type end t)
4014 (insert-buffer-substring forward-buffer)
4015 (goto-char beg)
4016 (while (re-search-forward "^-" end t)
4017 (beginning-of-line)
4018 (insert "- ")
4019 (forward-line 1))
4020 (goto-char end)
4021 (skip-chars-backward "\n")
4022 (if (< (point) end)
4023 (forward-char 1))
4024 (delete-region (point) end)
4025 (set-marker end nil))
4026 (insert "------- End of forwarded message -------\n"))
4027 (push-mark))))))
4029 (defun rmail-resend (address &optional from comment mail-alias-file)
4030 "Resend current message to ADDRESSES.
4031 ADDRESSES should be a single address, a string consisting of several
4032 addresses separated by commas, or a list of addresses.
4034 Optional FROM is the address to resend the message from, and
4035 defaults from the value of `user-mail-address'.
4036 Optional COMMENT is a string to insert as a comment in the resent message.
4037 Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
4038 typically for purposes of moderating a list."
4039 (interactive "sResend to: ")
4040 (require 'sendmail)
4041 (require 'mailalias)
4042 (unless (or (eq rmail-view-buffer (current-buffer))
4043 (eq rmail-buffer (current-buffer)))
4044 (error "Not an Rmail buffer"))
4045 (if (not from) (setq from user-mail-address))
4046 (let ((tembuf (generate-new-buffer " sendmail temp"))
4047 (case-fold-search nil)
4048 (mail-personal-alias-file
4049 (or mail-alias-file mail-personal-alias-file))
4050 (mailbuf rmail-buffer))
4051 (unwind-protect
4052 (with-current-buffer tembuf
4053 ;;>> Copy message into temp buffer
4054 (if (and rmail-enable-mime
4055 rmail-insert-mime-resent-message-function)
4056 (funcall rmail-insert-mime-resent-message-function mailbuf)
4057 (insert-buffer-substring mailbuf))
4058 (goto-char (point-min))
4059 ;; Delete any Sender field, since that's not specifiable.
4060 ; Only delete Sender fields in the actual header.
4061 (re-search-forward "^$" nil 'move)
4062 ; Using "while" here rather than "if" because some buggy mail
4063 ; software may have inserted multiple Sender fields.
4064 (while (re-search-backward "^Sender:" nil t)
4065 (let (beg)
4066 (setq beg (point))
4067 (forward-line 1)
4068 (while (looking-at "[ \t]")
4069 (forward-line 1))
4070 (delete-region beg (point))))
4071 ; Go back to the beginning of the buffer so the Resent- fields
4072 ; are inserted there.
4073 (goto-char (point-min))
4074 ;;>> Insert resent-from:
4075 (insert "Resent-From: " from "\n")
4076 (insert "Resent-Date: " (mail-rfc822-date) "\n")
4077 ;;>> Insert resent-to: and bcc if need be.
4078 (let ((before (point)))
4079 (if mail-self-blind
4080 (insert "Resent-Bcc: " (user-login-name) "\n"))
4081 (insert "Resent-To: " (if (stringp address)
4082 address
4083 (mapconcat 'identity address ",\n\t"))
4084 "\n")
4085 ;; Expand abbrevs in the recipients.
4086 (save-excursion
4087 (if (featurep 'mailabbrev)
4088 (let ((end (point-marker))
4089 (local-abbrev-table mail-abbrevs)
4090 (old-syntax-table (syntax-table)))
4091 (if (and (not (vectorp mail-abbrevs))
4092 (file-exists-p mail-personal-alias-file))
4093 (build-mail-abbrevs))
4094 (unless mail-abbrev-syntax-table
4095 (mail-abbrev-make-syntax-table))
4096 (set-syntax-table mail-abbrev-syntax-table)
4097 (goto-char before)
4098 (while (and (< (point) end)
4099 (progn (forward-word-strictly 1)
4100 (<= (point) end)))
4101 (expand-abbrev))
4102 (set-syntax-table old-syntax-table))
4103 (expand-mail-aliases before (point)))))
4104 ;;>> Set up comment, if any.
4105 (if (and (sequencep comment) (not (zerop (length comment))))
4106 (let ((before (point))
4107 after)
4108 (insert comment)
4109 (or (eolp) (insert "\n"))
4110 (setq after (point))
4111 (goto-char before)
4112 (while (< (point) after)
4113 (insert "Resent-Comment: ")
4114 (forward-line 1))))
4115 ;; Don't expand aliases in the destination fields
4116 ;; of the original message.
4117 (let (mail-aliases)
4118 (funcall send-mail-function)))
4119 (kill-buffer tembuf))
4120 (with-current-buffer rmail-buffer
4121 (rmail-set-attribute rmail-resent-attr-index t rmail-current-message))))
4123 (defvar mail-unsent-separator
4124 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
4125 "^ *---+ +Returned message +---+ *$\\|"
4126 "^ *---+ *Returned mail follows *---+ *$\\|"
4127 "^Start of returned message$\\|"
4128 "^---+ Below this line is a copy of the message.$\\|"
4129 "^ *---+ +Original message +---+ *$\\|"
4130 "^ *--+ +begin message +--+ *$\\|"
4131 "^ *---+ +Original message follows +---+ *$\\|"
4132 "^ *---+ +Your message follows +---+ *$\\|"
4133 "^|? *---+ +Message text follows: +---+ *|?$\\|"
4134 "^ *---+ +This is a copy of \\w+ message, including all the headers.*---+ *$")
4135 "A regexp that matches the separator before the text of a failed message.")
4137 (defvar mail-mime-unsent-header "^Content-Type: message/rfc822 *$"
4138 "A regexp that matches the header of a MIME body part with a failed message.")
4140 ;; This is a cut-down version of rmail-clear-headers from Emacs 22.
4141 ;; It doesn't have the same functionality, hence the name change.
4142 (defun rmail-delete-headers (regexp)
4143 "Delete any mail headers matching REGEXP.
4144 The message should be narrowed to just the headers."
4145 (when regexp
4146 (goto-char (point-min))
4147 (while (re-search-forward regexp nil t)
4148 (beginning-of-line)
4149 ;; This code from Emacs 22 doesn't seem right, since r-n-h is
4150 ;; just for display.
4151 ;;; (if (looking-at rmail-nonignored-headers)
4152 ;;; (forward-line 1)
4153 (delete-region (point)
4154 (save-excursion
4155 (if (re-search-forward "\n[^ \t]" nil t)
4156 (1- (point))
4157 (point-max)))))))
4159 (autoload 'mail-position-on-field "sendmail")
4161 (declare-function rmail-mime-toggle-raw "rmailmm" (&optional state))
4163 (defvar rmail-mime-mbox-buffer)
4164 (defvar rmail-mime-view-buffer)
4166 (defun rmail-retry-failure ()
4167 "Edit a mail message which is based on the contents of the current message.
4168 For a message rejected by the mail system, extract the interesting headers and
4169 the body of the original message.
4170 If the failed message is a MIME multipart message, it is searched for a
4171 body part with a header which matches the variable `mail-mime-unsent-header'.
4172 Otherwise, the variable `mail-unsent-separator' should match the string that
4173 delimits the returned original message.
4174 The variable `rmail-retry-ignored-headers' is a regular expression
4175 specifying headers which should not be copied into the new message."
4176 (interactive)
4177 (require 'mail-utils)
4178 ;; FIXME This does not handle rmail-mime-feature != 'rmailmm.
4179 ;; There is no API defined for rmail-mime-feature to provide
4180 ;; rmail-mime-message-p, rmail-mime-toggle-raw equivalents.
4181 ;; But does anyone actually use rmail-mime-feature != 'rmailmm?
4182 (if (and rmail-enable-mime
4183 (eq rmail-mime-feature 'rmailmm)
4184 (featurep rmail-mime-feature))
4185 (with-current-buffer rmail-buffer
4186 (if (rmail-mime-message-p)
4187 (let ((rmail-mime-mbox-buffer rmail-view-buffer)
4188 (rmail-mime-view-buffer rmail-buffer))
4189 (rmail-mime-toggle-raw 'raw)))))
4191 (let ((rmail-this-buffer (current-buffer))
4192 (msgnum rmail-current-message)
4193 bounce-start bounce-end bounce-indent resending
4194 (content-type (rmail-get-header "Content-Type")))
4195 (save-excursion
4196 (goto-char (point-min))
4197 (let ((case-fold-search t))
4198 (if (and content-type
4199 (string-match
4200 ";[\n\t ]*boundary=\"?\\([-0-9a-z'()+_,./:=? ]+\\)\"?"
4201 content-type))
4202 ;; Handle a MIME multipart bounce message.
4203 (let ((codestring
4204 (concat "\n--"
4205 (substring content-type (match-beginning 1)
4206 (match-end 1)))))
4207 (unless (re-search-forward mail-mime-unsent-header nil t)
4208 (error "Cannot find beginning of header in failed message"))
4209 (unless (search-forward "\n\n" nil t)
4210 (error "Cannot find start of Mime data in failed message"))
4211 (setq bounce-start (point))
4212 (if (search-forward codestring nil t)
4213 (setq bounce-end (match-beginning 0))
4214 (setq bounce-end (point-max))))
4215 ;; Non-MIME bounce.
4216 (or (re-search-forward mail-unsent-separator nil t)
4217 (error "Cannot parse this as a failure message"))
4218 (skip-chars-forward "\n")
4219 ;; Support a style of failure message in which the original
4220 ;; message is indented, and included within lines saying
4221 ;; `Start of returned message' and `End of returned message'.
4222 (if (looking-at " +Received:")
4223 (progn
4224 (setq bounce-start (point))
4225 (skip-chars-forward " ")
4226 (setq bounce-indent (- (current-column)))
4227 (goto-char (point-max))
4228 (re-search-backward "^End of returned message$" nil t)
4229 (setq bounce-end (point)))
4230 ;; One message contained a few random lines before
4231 ;; the old message header. The first line of the
4232 ;; message started with two hyphens. A blank line
4233 ;; followed these random lines. The same line
4234 ;; beginning with two hyphens was possibly marking
4235 ;; the end of the message.
4236 (if (looking-at "^--")
4237 (let ((boundary (buffer-substring-no-properties
4238 (point)
4239 (progn (end-of-line) (point)))))
4240 (search-forward "\n\n")
4241 (skip-chars-forward "\n")
4242 (setq bounce-start (point))
4243 (goto-char (point-max))
4244 (search-backward (concat "\n\n" boundary) bounce-start t)
4245 (setq bounce-end (point)))
4246 (setq bounce-start (point)
4247 bounce-end (point-max)))
4248 (unless (search-forward "\n\n" nil t)
4249 (error "Cannot find end of header in failed message"))))))
4250 ;; We have found the message that bounced, within the current message.
4251 ;; Now start sending new message; default header fields from original.
4252 ;; Turn off the usual actions for initializing the message body
4253 ;; because we want to get only the text from the failure message.
4254 (let (mail-signature mail-setup-hook)
4255 (if (rmail-start-mail nil nil nil nil nil rmail-this-buffer
4256 (list (list 'rmail-mark-message
4257 rmail-this-buffer
4258 (aref rmail-msgref-vector msgnum)
4259 rmail-retried-attr-index)))
4260 ;; Insert original text as initial text of new draft message.
4261 ;; Bind inhibit-read-only since the header delimiter
4262 ;; of the previous message was probably read-only.
4263 (let ((inhibit-read-only t)
4264 eoh)
4265 (erase-buffer)
4266 (insert-buffer-substring rmail-this-buffer
4267 bounce-start bounce-end)
4268 (goto-char (point-min))
4269 (if bounce-indent
4270 (indent-rigidly (point-min) (point-max) bounce-indent))
4271 (rfc822-goto-eoh)
4272 (setq eoh (point))
4273 (insert mail-header-separator)
4274 (save-restriction
4275 (narrow-to-region (point-min) eoh)
4276 (rmail-delete-headers rmail-retry-ignored-headers)
4277 (rmail-delete-headers "^\\(sender\\|return-path\\|received\\):")
4278 (setq resending (mail-fetch-field "resent-to"))
4279 (if mail-self-blind
4280 (if resending
4281 (insert "Resent-Bcc: " (user-login-name) "\n")
4282 (insert "BCC: " (user-login-name) "\n"))))
4283 (goto-char (point-min))
4284 (mail-position-on-field (if resending "Resent-To" "To") t))))))
4286 (defun rmail-summary-exists ()
4287 "Non-nil if in an RMAIL buffer and an associated summary buffer exists.
4288 In fact, the non-nil value returned is the summary buffer itself."
4289 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
4290 rmail-summary-buffer))
4292 (defun rmail-summary-displayed ()
4293 "t if in RMAIL buffer and an associated summary buffer is displayed."
4294 (and rmail-summary-buffer (get-buffer-window rmail-summary-buffer)))
4296 (defcustom rmail-redisplay-summary nil
4297 "Non-nil means Rmail should show the summary when it changes.
4298 This has an effect only if a summary buffer exists."
4299 :type 'boolean
4300 :group 'rmail-summary)
4302 (defcustom rmail-summary-window-size nil
4303 "Non-nil means specify the height for an Rmail summary window."
4304 :type '(choice (const :tag "Disabled" nil) integer)
4305 :group 'rmail-summary)
4307 ;; Put the summary buffer back on the screen, if user wants that.
4308 (defun rmail-maybe-display-summary ()
4309 (cond
4310 ((or (not rmail-summary-buffer)
4311 (not (buffer-name rmail-summary-buffer))))
4312 (rmail-redisplay-summary
4313 ;; If `rmail-redisplay-summary' is non-nil, make sure the summary
4314 ;; buffer is displayed.
4315 (display-buffer
4316 rmail-summary-buffer
4317 `(nil
4318 (reusable-frames . 0)
4319 ,(when rmail-summary-window-size
4320 `(window-height . ,rmail-summary-window-size)))))
4321 (rmail-summary-window-size
4322 ;; If `rmail-summary-window-size' is non-nil and the summary buffer
4323 ;; is displayed, make sure it gets resized.
4324 (let ((window (get-buffer-window rmail-summary-buffer 0)))
4325 (when window
4326 (window-resize-no-error
4327 window (- rmail-summary-window-size (window-height window))))))))
4329 ;;;; *** Rmail Local Fontification ***
4331 (defun rmail-fontify-buffer-function ()
4332 ;; This function's symbol is bound to font-lock-fontify-buffer-function.
4333 (add-hook 'rmail-show-message-hook 'rmail-fontify-message nil t)
4334 ;; If we're already showing a message, fontify it now.
4335 (if rmail-current-message (rmail-fontify-message))
4336 ;; Prevent Font Lock mode from kicking in.
4337 (setq font-lock-fontified t))
4339 (defun rmail-unfontify-buffer-function ()
4340 ;; This function's symbol is bound to font-lock-fontify-unbuffer-function.
4341 (with-silent-modifications
4342 (save-restriction
4343 (widen)
4344 (remove-hook 'rmail-show-message-hook 'rmail-fontify-message t)
4345 (remove-text-properties (point-min) (point-max) '(rmail-fontified nil))
4346 (font-lock-default-unfontify-buffer))))
4348 (defun rmail-fontify-message ()
4349 ;; Fontify the current message if it is not already fontified.
4350 (if (text-property-any (point-min) (point-max) 'rmail-fontified nil)
4351 (with-silent-modifications
4352 (save-excursion
4353 (save-match-data
4354 (add-text-properties (point-min) (point-max) '(rmail-fontified t))
4355 (font-lock-fontify-region (point-min) (point-max)))))))
4357 ;;; Speedbar support for RMAIL files.
4358 (defcustom rmail-speedbar-match-folder-regexp "^[A-Z0-9]+\\(\\.[A-Z0-9]+\\)?$"
4359 "Regexp matching Rmail folder names to be displayed in Speedbar.
4360 Enabling this permits Speedbar to display your folders for easy
4361 browsing, and moving of messages."
4362 :type 'regexp
4363 :group 'rmail
4364 :group 'speedbar)
4366 (defvar rmail-speedbar-last-user nil
4367 "The last user to be displayed in the speedbar.")
4369 (defvar rmail-speedbar-key-map nil
4370 "Keymap used when in rmail display mode.")
4372 (declare-function speedbar-make-specialized-keymap "speedbar" ())
4374 (defun rmail-install-speedbar-variables ()
4375 "Install those variables used by speedbar to enhance rmail."
4376 (unless rmail-speedbar-key-map
4377 (setq rmail-speedbar-key-map (speedbar-make-specialized-keymap))
4378 (define-key rmail-speedbar-key-map "e" 'speedbar-edit-line)
4379 (define-key rmail-speedbar-key-map "r" 'speedbar-edit-line)
4380 (define-key rmail-speedbar-key-map "\C-m" 'speedbar-edit-line)
4381 (define-key rmail-speedbar-key-map "M"
4382 'rmail-speedbar-move-message-to-folder-on-line)))
4384 ;; Mouse-3.
4385 (defvar rmail-speedbar-menu-items
4386 '(["Read Folder" speedbar-edit-line t]
4387 ["Move message to folder" rmail-speedbar-move-message-to-folder-on-line
4388 (save-excursion (beginning-of-line)
4389 (looking-at "<M> "))])
4390 "Additional menu-items to add to speedbar frame.")
4392 (declare-function speedbar-insert-button "speedbar"
4393 (text face mouse function &optional token prevline))
4395 ;; Make sure our special speedbar major mode is loaded
4396 (if (featurep 'speedbar)
4397 (rmail-install-speedbar-variables)
4398 (add-hook 'speedbar-load-hook 'rmail-install-speedbar-variables))
4400 (defun rmail-speedbar-buttons (buffer)
4401 "Create buttons for BUFFER containing rmail messages.
4402 Click on the address under Reply to: to reply to this person.
4403 Under Folders: Click a name to read it, or on the <M> to move the
4404 current message into that RMAIL folder."
4405 (let ((from nil))
4406 (with-current-buffer buffer
4407 (goto-char (point-min))
4408 (if (not (re-search-forward "^Reply-To: " nil t))
4409 (if (not (re-search-forward "^From:? " nil t))
4410 (setq from t)))
4411 (if from
4413 (setq from (buffer-substring (point) (line-end-position)))))
4414 (goto-char (point-min))
4415 (if (and (looking-at "Reply to:")
4416 (equal from rmail-speedbar-last-user))
4418 (setq rmail-speedbar-last-user from)
4419 (erase-buffer)
4420 (insert "Reply To:\n")
4421 (if (stringp from)
4422 (speedbar-insert-button from 'speedbar-directory-face 'highlight
4423 'rmail-speedbar-button 'rmail-reply))
4424 (insert "Folders:\n")
4425 (let* ((case-fold-search nil)
4426 (df (directory-files (with-current-buffer buffer
4427 default-directory)
4428 nil rmail-speedbar-match-folder-regexp)))
4429 (dolist (file df)
4430 (when (file-regular-p file)
4431 (speedbar-insert-button "<M>" 'speedbar-button-face 'highlight
4432 'rmail-speedbar-move-message file)
4433 (speedbar-insert-button file 'speedbar-file-face 'highlight
4434 'rmail-speedbar-find-file nil t)))))))
4436 (eval-when-compile (require 'dframe))
4437 ;; Part of the macro expansion of dframe-with-attached-buffer.
4438 ;; At runtime, will be pulled in as a require of speedbar.
4439 (declare-function dframe-select-attached-frame "dframe" (&optional frame))
4440 (declare-function dframe-maybee-jump-to-attached-frame "dframe" ())
4442 (defun rmail-speedbar-button (_text token _indent)
4443 "Execute an rmail command specified by TEXT.
4444 The command used is TOKEN. INDENT is not used."
4445 (dframe-with-attached-buffer
4446 (funcall token t)))
4448 (defun rmail-speedbar-find-file (text _token _indent)
4449 "Load in the rmail file TEXT.
4450 TOKEN and INDENT are not used."
4451 (dframe-with-attached-buffer
4452 (message "Loading in RMAIL file %s..." text)
4453 (rmail text)))
4455 (declare-function speedbar-do-function-pointer "speedbar" ())
4457 (defun rmail-speedbar-move-message-to-folder-on-line ()
4458 "If the current line is a folder, move current message to it."
4459 (interactive)
4460 (save-excursion
4461 (beginning-of-line)
4462 (if (re-search-forward "<M> " (line-end-position) t)
4463 (progn
4464 (forward-char -2)
4465 (speedbar-do-function-pointer)))))
4467 (defun rmail-speedbar-move-message (_text token _indent)
4468 "From button TEXT, copy current message to the rmail file specified by TOKEN.
4469 TEXT and INDENT are not used."
4470 (dframe-with-attached-buffer
4471 (message "Moving message to %s" token)
4472 ;; expand-file-name is needed due to the unhelpful way in which
4473 ;; rmail-output expands non-absolute filenames against rmail-default-file.
4474 ;; What is the point of that, anyway?
4475 (rmail-output (expand-file-name token))))
4477 ;; Functions for setting, getting and encoding the POP password.
4478 ;; The password is encoded to prevent it from being easily accessible
4479 ;; to "prying eyes." Obviously, this encoding isn't "real security,"
4480 ;; nor is it meant to be.
4482 ;;;###autoload
4483 (defun rmail-set-remote-password (password)
4484 "Set PASSWORD to be used for retrieving mail from a POP or IMAP server."
4485 (interactive "sPassword: ")
4486 (if password
4487 (setq rmail-encoded-remote-password
4488 (rmail-encode-string password (emacs-pid)))
4489 (setq rmail-remote-password nil)
4490 (setq rmail-encoded-remote-password nil)))
4492 (defun rmail-get-remote-password (imap)
4493 "Get the password for retrieving mail from a POP or IMAP server. If none
4494 has been set, then prompt the user for one."
4495 (when (not rmail-encoded-remote-password)
4496 (if (not rmail-remote-password)
4497 (setq rmail-remote-password
4498 (read-passwd (if imap
4499 "IMAP password: "
4500 "POP password: "))))
4501 (rmail-set-remote-password rmail-remote-password)
4502 (setq rmail-remote-password nil))
4503 (rmail-encode-string rmail-encoded-remote-password (emacs-pid)))
4505 (defun rmail-have-password ()
4506 (or rmail-remote-password rmail-encoded-remote-password))
4508 (defun rmail-encode-string (string mask)
4509 "Encode STRING with integer MASK, by taking the exclusive OR of the
4510 lowest byte in the mask with the first character of string, the
4511 second-lowest-byte with the second character of the string, etc.,
4512 restarting at the lowest byte of the mask whenever it runs out.
4513 Returns the encoded string. Calling the function again with an
4514 encoded string (and the same mask) will decode the string."
4515 (setq mask (abs mask)) ; doesn't work if negative
4516 (let* ((string-vector (string-to-vector string)) (i 0)
4517 (len (length string-vector)) (curmask mask) charmask)
4518 (while (< i len)
4519 (if (= curmask 0)
4520 (setq curmask mask))
4521 (setq charmask (% curmask 256))
4522 (setq curmask (lsh curmask -8))
4523 (aset string-vector i (logxor charmask (aref string-vector i)))
4524 (setq i (1+ i)))
4525 (concat string-vector)))
4527 (defun rmail-epa-decrypt-1 (mime)
4528 "Decrypt a single GnuPG encrypted text in a message.
4529 The starting string of the encrypted text should have just been regexp-matched.
4530 Argument MIME is non-nil if this is a mime message."
4531 (let* ((armor-start (match-beginning 0))
4532 (armor-prefix (buffer-substring
4533 (line-beginning-position)
4534 armor-start))
4535 (armor-end-regexp)
4536 armor-end after-end
4537 unquote)
4538 (if (string-match "<pre>\\'" armor-prefix)
4539 (setq armor-prefix ""))
4541 (setq armor-end-regexp
4542 (concat "^"
4543 armor-prefix
4544 "-----END PGP MESSAGE-----$"))
4545 (setq armor-end (re-search-forward armor-end-regexp
4546 nil t))
4548 (unless armor-end
4549 (error "Encryption armor beginning has no matching end"))
4550 (goto-char armor-start)
4552 ;; Because epa--find-coding-system-for-mime-charset not autoloaded.
4553 (require 'epa)
4555 ;; Advance over this armor.
4556 (goto-char armor-end)
4557 (setq after-end (- (point-max) armor-end))
4559 (when mime
4560 (save-excursion
4561 (goto-char armor-start)
4562 (re-search-backward "^--" nil t)
4563 (save-restriction
4564 (narrow-to-region (point) armor-start)
4566 ;; Use the charset specified in the armor.
4567 (unless coding-system-for-read
4568 (if (re-search-forward "^[ \t]*Charset[ \t\n]*:[ \t\n]*\\(.*\\)" nil t)
4569 (setq coding-system-for-read
4570 (epa--find-coding-system-for-mime-charset
4571 (intern (downcase (match-string 1)))))))
4573 (goto-char (point-min))
4574 (if (re-search-forward "^[ \t]*Content-transfer-encoding[ \t\n]*:[ \t\n]*quoted-printable[ \t]*$" nil t)
4575 (setq unquote t)))))
4577 (when unquote
4578 (let ((inhibit-read-only t))
4579 (mail-unquote-printable-region armor-start
4580 (- (point-max) after-end))))
4582 ;; Decrypt it, maybe in place, maybe making new buffer.
4583 (epa-decrypt-region
4584 armor-start (- (point-max) after-end)
4585 ;; Call back this function to prepare the output.
4586 (lambda ()
4587 (let ((inhibit-read-only t))
4588 (delete-region armor-start (- (point-max) after-end))
4589 (goto-char armor-start)
4590 (current-buffer))))
4592 (list armor-start (- (point-max) after-end) mime
4593 armor-end-regexp)))
4595 (declare-function rmail-mime-entity-truncated "rmailmm" (entity))
4597 ;; Should this have a key-binding, or be in a menu?
4598 ;; There doesn't really seem to be an appropriate menu.
4599 ;; Eg the edit command is not in a menu either.
4601 (defvar rmail-mime-render-html-function) ; defcustom in rmailmm
4602 (defun rmail-epa-decrypt ()
4603 "Decrypt GnuPG or OpenPGP armors in current message."
4604 (interactive)
4606 ;; Save the current buffer here for cleanliness, in case we
4607 ;; change it in one of the calls to `epa-decrypt-region'.
4609 (save-excursion
4610 (let (decrypts (mime (rmail-mime-message-p))
4611 mime-disabled)
4612 (goto-char (point-min))
4614 ;; Turn off mime processing.
4615 (when (and mime
4616 (not (get-text-property (point-min) 'rmail-mime-hidden)))
4617 (setq mime-disabled t)
4618 (rmail-mime))
4620 ;; Now find all armored messages in the buffer
4621 ;; and decrypt them one by one.
4622 (goto-char (point-min))
4623 (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
4624 (let ((coding-system-for-read coding-system-for-read)
4625 (case-fold-search t))
4627 (push (rmail-epa-decrypt-1 mime) decrypts)))
4629 (when (and decrypts (eq major-mode 'rmail-mode))
4630 (rmail-add-label "decrypt"))
4632 (when (and decrypts (rmail-buffers-swapped-p))
4633 (when (y-or-n-p "Replace the original message? ")
4634 (setq decrypts (nreverse decrypts))
4635 (let ((beg (rmail-msgbeg rmail-current-message))
4636 (end (rmail-msgend rmail-current-message))
4637 (from-buffer (current-buffer)))
4638 (with-current-buffer rmail-view-buffer
4639 (narrow-to-region beg end)
4640 (goto-char (point-min))
4641 (dolist (d decrypts)
4642 ;; Find, in the real Rmail buffer, the same armors
4643 ;; that we found and decrypted in the view buffer.
4644 (if (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
4645 (let (armor-start armor-end armor-end-regexp)
4646 (setq armor-start (match-beginning 0)
4647 armor-end-regexp (nth 3 d)
4648 armor-end (re-search-forward
4649 armor-end-regexp
4650 nil t))
4652 ;; Found as expected -- now replace it with the decrypt.
4653 (when armor-end
4654 (delete-region armor-start armor-end)
4655 (insert-buffer-substring from-buffer (nth 0 d) (nth 1 d)))
4657 ;; Change the mime type (if this is in a mime part)
4658 ;; so this part will display by default
4659 ;; when the message is shown later.
4660 (when (nth 2 d)
4661 (goto-char armor-start)
4662 (when (re-search-backward "^--" nil t)
4663 (save-restriction
4664 (narrow-to-region (point) armor-start)
4665 (when (re-search-forward "^content-type[ \t\n]*:[ \t\n]*" nil t)
4666 (when (looking-at "[^\n \t;]+")
4667 (let ((value (match-string 0)))
4668 (unless (member value '("text/plain" "text/html"))
4669 (replace-match "text/plain"))))))))
4670 )))))))
4672 (when (and (null decrypts)
4673 mime mime-disabled)
4674 ;; Re-enable mime processing.
4675 (rmail-mime)
4676 ;; Find each Show button and show that part.
4677 (while (search-forward " Show " nil t)
4678 (forward-char -2)
4679 (let ((rmail-mime-render-html-function nil)
4680 (entity (get-text-property (point) 'rmail-mime-entity)))
4681 (unless (and (not (stringp entity))
4682 (rmail-mime-entity-truncated entity))
4683 (push-button))))
4684 (goto-char (point-min))
4685 (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
4686 (let ((coding-system-for-read coding-system-for-read)
4687 (case-fold-search t))
4688 (push (rmail-epa-decrypt-1 mime) decrypts)))
4692 (unless decrypts
4693 (error "Nothing to decrypt")))))
4696 ;;;; Desktop support
4698 (defun rmail-restore-desktop-buffer (file-name
4699 _buffer-name
4700 _buffer-misc)
4701 "Restore an rmail buffer specified in a desktop file."
4702 (condition-case nil
4703 (progn
4704 (rmail-input file-name)
4705 (if (eq major-mode 'rmail-mode)
4706 (current-buffer)
4707 rmail-buffer))
4708 (file-locked
4709 (kill-buffer (current-buffer))
4710 nil)))
4712 (add-to-list 'desktop-buffer-mode-handlers
4713 '(rmail-mode . rmail-restore-desktop-buffer))
4715 ;; We use this to record the encoding of the current message before
4716 ;; saving the message collection.
4717 (defvar rmail-message-encoding nil)
4719 ;; Used in `write-region-annotate-functions' to write rmail files.
4720 (defun rmail-write-region-annotate (start _end)
4721 (when (and (null start) rmail-buffer-swapped)
4722 (unless (buffer-live-p rmail-view-buffer)
4723 (error "Buffer `%s' with real text of `%s' has disappeared"
4724 (buffer-name rmail-view-buffer)
4725 (buffer-name (current-buffer))))
4726 (setq rmail-message-encoding buffer-file-coding-system)
4727 (set-buffer rmail-view-buffer)
4728 (widen)
4729 nil))
4731 ;; Used to restore the encoding of the buffer where we show the
4732 ;; current message, after we save the message collection. This is
4733 ;; needed because rmail-write-region-annotate switches buffers behind
4734 ;; save-file's back, with the side effect that last-coding-system-used
4735 ;; is assigned to buffer-file-coding-system of the wrong buffer.
4736 (defun rmail-after-save-hook ()
4737 (if (or (eq rmail-view-buffer (current-buffer))
4738 (eq rmail-buffer (current-buffer)))
4739 (with-current-buffer
4740 (if (rmail-buffers-swapped-p) rmail-buffer rmail-view-buffer)
4741 (setq buffer-file-coding-system rmail-message-encoding))))
4742 (add-hook 'after-save-hook 'rmail-after-save-hook)
4745 (provide 'rmail)
4747 ;;; rmail.el ends here