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