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