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