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