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