Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / mail / rmail.el
CommitLineData
55535639 1;;; rmail.el --- main code of "RMAIL" mail reader for Emacs
c88ab9ce 2
e84b4b86 3;; Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1996, 1997, 1998,
59ce725a 4;; 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
5701a0ce 5;; Free Software Foundation, Inc.
3a801d0c 6
e5167999 7;; Maintainer: FSF
d7b4d18f 8;; Keywords: mail
e5167999 9
581d7e0b
RM
10;; This file is part of GNU Emacs.
11
b1fc2b50 12;; GNU Emacs is free software: you can redistribute it and/or modify
581d7e0b 13;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
581d7e0b
RM
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
b1fc2b50 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
581d7e0b 24
55535639
PJ
25;;; Commentary:
26
e5167999 27;;; Code:
581d7e0b
RM
28
29;; Souped up by shane@mit-ajax based on ideas of rlk@athena.mit.edu
30;; New features include attribute and keyword support, message
31;; selection by dispatch table, summary by attributes and keywords,
32;; expunging by dispatch table, sticky options for file commands.
33
bd1f0f84
RS
34;; Extended by Bob Weiner of Motorola
35;; New features include: rmail and rmail-summary buffers remain
36;; synchronized and key bindings basically operate the same way in both
37;; buffers, summary by topic or by regular expression, rmail-reply-prefix
38;; variable, and a bury rmail buffer (wipe) command.
39;;
40
581d7e0b 41(require 'mail-utils)
1bbdda4d 42(eval-when-compile (require 'mule-util)) ; for detect-coding-with-priority
bd1f0f84 43
a8144f5e
JB
44(defvar deleted-head)
45(defvar font-lock-fontified)
46(defvar mail-abbrev-syntax-table)
47(defvar mail-abbrevs)
48(defvar messages-head)
49(defvar rmail-use-spam-filter)
50(defvar rsf-beep)
51(defvar rsf-sleep-after-message)
52(defvar total-messages)
1da6a64d 53(defvar tool-bar-map)
a8144f5e 54
bd1f0f84 55; These variables now declared in paths.el.
581d7e0b
RM
56;(defvar rmail-spool-directory "/usr/spool/mail/"
57; "This is the name of the directory used by the system mailer for\n\
6af28e9f 58;delivering new mail. Its name should end with a slash.")
581d7e0b
RM
59;(defvar rmail-file-name
60; (expand-file-name "~/RMAIL")
61; "")
62
9bb97fe9
RS
63(defgroup rmail nil
64 "Mail reader for Emacs."
65 :group 'mail)
66
67(defgroup rmail-retrieve nil
68 "Rmail retrieval options."
69 :prefix "rmail-"
70 :group 'rmail)
71
72(defgroup rmail-files nil
73 "Rmail files."
74 :prefix "rmail-"
75 :group 'rmail)
76
77(defgroup rmail-headers nil
78 "Rmail header options."
79 :prefix "rmail-"
80 :group 'rmail)
81
82(defgroup rmail-reply nil
83 "Rmail reply options."
84 :prefix "rmail-"
85 :group 'rmail)
86
87(defgroup rmail-summary nil
88 "Rmail summary options."
89 :prefix "rmail-"
90 :prefix "rmail-summary-"
91 :group 'rmail)
92
93(defgroup rmail-output nil
94 "Output message to a file."
95 :prefix "rmail-output-"
96 :prefix "rmail-"
97 :group 'rmail)
98
2bc7a799
GM
99(defgroup rmail-edit nil
100 "Rmail editing."
101 :prefix "rmail-edit-"
102 :group 'rmail)
103
1086788e
EZ
104(defgroup rmail-obsolete nil
105 "Rmail obsolete customization variables."
106 :group 'rmail)
9bb97fe9 107
9cda36c0 108(defcustom rmail-movemail-program nil
8510cb94 109 "If non-nil, the file name of the `movemail' program."
9cda36c0 110 :group 'rmail-retrieve
e1c3516e 111 :type '(choice (const nil) string))
74562053 112
9bb97fe9 113(defcustom rmail-pop-password nil
8510cb94
RS
114 "*Password to use when reading mail from POP server.
115Please use `rmail-remote-password' instead."
9bb97fe9
RS
116 :type '(choice (string :tag "Password")
117 (const :tag "Not Required" nil))
1086788e 118 :group 'rmail-obsolete)
ceeb471c 119
9bb97fe9 120(defcustom rmail-pop-password-required nil
e84b4b86 121 "*Non-nil if a password is required when reading mail from a POP server.
8510cb94 122Please use rmail-remote-password-required instead."
9bb97fe9 123 :type 'boolean
1086788e
EZ
124 :group 'rmail-obsolete)
125
126(defcustom rmail-remote-password nil
8510cb94
RS
127 "*Password to use when reading mail from a remote server.
128This setting is ignored for mailboxes whose URL already contains a password."
1086788e
EZ
129 :type '(choice (string :tag "Password")
130 (const :tag "Not Required" nil))
131 :set-after '(rmail-pop-password)
132 :set #'(lambda (symbol value)
bf247b6e 133 (set-default symbol
1086788e
EZ
134 (if (and (not value)
135 (boundp 'rmail-pop-password)
136 rmail-pop-password)
137 rmail-pop-password
138 value))
139 (setq rmail-pop-password nil))
140 :group 'rmail-retrieve
bf247b6e 141 :version "22.1")
1086788e
EZ
142
143(defcustom rmail-remote-password-required nil
144 "*Non-nil if a password is required when reading mail from a remote server."
145 :type 'boolean
146 :set-after '(rmail-pop-password-required)
147 :set #'(lambda (symbol value)
bf247b6e 148 (set-default symbol
1086788e
EZ
149 (if (and (not value)
150 (boundp 'rmail-pop-password-required)
151 rmail-pop-password-required)
152 rmail-pop-password-required
153 value))
154 (setq rmail-pop-password-required nil))
155 :group 'rmail-retrieve
bf247b6e 156 :version "22.1")
ceeb471c 157
757c2332 158(defcustom rmail-movemail-flags nil
442f5309
KH
159 "*List of flags to pass to movemail.
160Most commonly used to specify `-g' to enable GSS-API authentication
161or `-k' to enable Kerberos authentication."
84840709 162 :type '(repeat string)
cd32a7ba
DN
163 :group 'rmail-retrieve
164 :version "20.3")
757c2332 165
1086788e
EZ
166(defvar rmail-remote-password-error "invalid usercode or password\\|
167unknown user name or bad password\\|Authentication failed\\|MU_ERR_AUTH_FAILURE"
168 "Regular expression matching incorrect-password POP or IMAP server error
169messages.
0974f7bf
RS
170If you get an incorrect-password error that this expression does not match,
171please report it with \\[report-emacs-bug].")
172
1086788e 173(defvar rmail-encoded-remote-password nil)
90d56ed8 174
9bb97fe9 175(defcustom rmail-preserve-inbox nil
8510cb94 176 "*Non-nil means leave incoming mail in the user's inbox--don't delete it."
9bb97fe9
RS
177 :type 'boolean
178 :group 'rmail-retrieve)
4144e5cb 179
1086788e
EZ
180(defcustom rmail-movemail-search-path nil
181 "*List of directories to search for movemail (in addition to `exec-path')."
182 :group 'rmail-retrieve
183 :type '(repeat (directory)))
184
2b54af74
DN
185(declare-function mail-position-on-field "sendmail" (field &optional soft))
186(declare-function mail-text-start "sendmail" ())
187(declare-function rmail-update-summary "rmailsum" (&rest ignore))
188
1086788e 189(defun rmail-probe (prog)
8510cb94
RS
190 "Determine what flavor of movemail PROG is.
191We do this by executing it with `--version' and analyzing its output."
1086788e
EZ
192 (with-temp-buffer
193 (let ((tbuf (current-buffer)))
194 (buffer-disable-undo tbuf)
195 (call-process prog nil tbuf nil "--version")
196 (if (not (buffer-modified-p tbuf))
197 ;; Should not happen...
198 nil
199 (goto-char (point-min))
200 (cond
201 ((looking-at ".*movemail: invalid option")
202 'emacs) ;; Possibly...
203 ((looking-at "movemail (GNU Mailutils .*)")
204 'mailutils)
205 (t
206 ;; FIXME:
207 'emacs))))))
208
209(defun rmail-autodetect ()
0574be18 210 "Determine the file name of the `movemail' program and return its flavor.
8510cb94
RS
211If `rmail-movemail-program' is non-nil, use it.
212Otherwise, look for `movemail' in the directories in
213`rmail-movemail-search-path', those in `exec-path', and `exec-directory'."
1086788e
EZ
214 (if rmail-movemail-program
215 (rmail-probe rmail-movemail-program)
216 (catch 'scan
217 (dolist (dir (append rmail-movemail-search-path exec-path
218 (list exec-directory)))
219 (when (and dir (file-accessible-directory-p dir))
6052846b
GM
220 ;; Previously, this didn't have to work on Windows, because
221 ;; rmail-insert-inbox-text before r1.439 fell back to using
222 ;; (expand-file-name "movemail" exec-directory) and just
223 ;; assuming it would work.
224 ;; http://lists.gnu.org/archive/html/bug-gnu-emacs/2008-02/msg00087.html
225 (let ((progname (expand-file-name
226 (concat "movemail"
227 (if (memq system-type '(ms-dos windows-nt))
228 ".exe")) dir)))
1086788e
EZ
229 (when (and (not (file-directory-p progname))
230 (file-executable-p progname))
231 (let ((x (rmail-probe progname)))
232 (when x
233 (setq rmail-movemail-program progname)
234 (throw 'scan x))))))))))
235
236(defvar rmail-movemail-variant-in-use nil
237 "The movemail variant currently in use. Known variants are:
238
239 `emacs' Means any implementation, compatible with the native Emacs one.
240 This is the default;
241 `mailutils' Means GNU mailutils implementation, capable of handling full
ab3c4f67 242mail URLs as the source mailbox.")
1086788e
EZ
243
244;;;###autoload
245(defun rmail-movemail-variant-p (&rest variants)
246 "Return t if the current movemail variant is any of VARIANTS.
247Currently known variants are 'emacs and 'mailutils."
248 (when (not rmail-movemail-variant-in-use)
249 ;; Autodetect
250 (setq rmail-movemail-variant-in-use (rmail-autodetect)))
251 (not (null (member rmail-movemail-variant-in-use variants))))
252
11bdb42c
GM
253;; Call for effect, to set rmail-movemail-program (if not set by the
254;; user), and rmail-movemail-variant-in-use. Used by various functions.
255;; I'm not sure if M-x rmail is the only entry point to this package.
256;; If so, this can be moved there.
257(rmail-movemail-variant-p)
258
581d7e0b 259;;;###autoload
9bb97fe9 260(defcustom rmail-dont-reply-to-names nil "\
0ffba6bd
PR
261*A regexp specifying addresses to prune from a reply message.
262A value of nil means exclude your own email address as an address
02eddff2 263plus whatever is specified by `rmail-default-dont-reply-to-names'."
9bb97fe9
RS
264 :type '(choice regexp (const :tag "Your Name" nil))
265 :group 'rmail-reply)
581d7e0b
RM
266
267;;;###autoload
608aa380
KH
268(defvar rmail-default-dont-reply-to-names "\\`info-" "\
269A regular expression specifying part of the default value of the
270variable `rmail-dont-reply-to-names', for when the user does not set
581d7e0b 271`rmail-dont-reply-to-names' explicitly. (The other part of the default
0ffba6bd 272value is the user's email address and name.)
388348ae 273It is useful to set this variable in the site customization file.")
581d7e0b
RM
274
275;;;###autoload
0404d031 276(defcustom rmail-ignored-headers
868e7865 277 (concat "^via:\\|^mail-from:\\|^origin:\\|^references:\\|^sender:"
0404d031
GM
278 "\\|^status:\\|^received:\\|^x400-originator:\\|^x400-recipients:"
279 "\\|^x400-received:\\|^x400-mts-identifier:\\|^x400-content-type:"
280 "\\|^\\(resent-\\|\\)message-id:\\|^summary-line:\\|^resent-date:"
588c9a71 281 "\\|^nntp-posting-host:\\|^path:\\|^x-char.*:\\|^x-face:\\|^face:"
9c6f79c2 282 "\\|^x-mailer:\\|^delivered-to:\\|^lines:"
0404d031
GM
283 "\\|^content-transfer-encoding:\\|^x-coding-system:"
284 "\\|^return-path:\\|^errors-to:\\|^return-receipt-to:"
0404d031
GM
285 "\\|^precedence:\\|^list-help:\\|^list-post:\\|^list-subscribe:"
286 "\\|^list-id:\\|^list-unsubscribe:\\|^list-archive:"
5b9b5b89
RS
287 "\\|^content-length:\\|^nntp-posting-date:\\|^user-agent"
288 "\\|^importance:\\|^envelope-to:\\|^delivery-date\\|^openpgp:"
f7878d66 289 "\\|^mbox-line:\\|^cancel-lock:\\|^DomainKey-Signature:"
5b9b5b89
RS
290 "\\|^resent-face:\\|^resent-x.*:\\|^resent-organization:\\|^resent-openpgp:"
291
292 "\\|^x-.*:")
7c411780 293 "*Regexp to match header fields that Rmail should normally hide.
5b9b5b89
RS
294\(See also `rmail-nonignored-headers', which overrides this regexp.)
295This variable is used for reformatting the message header,
296which normally happens once for each message,
297when you view the message for the first time in Rmail.
298To make a change in this variable take effect
299for a message that you have already viewed,
300go to that message and type \\[rmail-toggle-header] twice."
301 :type 'regexp
302 :group 'rmail-headers)
303
304(defcustom rmail-nonignored-headers "^x-spam-status:"
305 "*Regexp to match X header fields that Rmail should show.
306This regexp overrides `rmail-ignored-headers'; if both this regexp
307and that one match a certain header field, Rmail shows the field.
be16d955 308If this is nil, ignore all header fields in `rmail-ignored-headers'.
5b9b5b89 309
7c411780
RS
310This variable is used for reformatting the message header,
311which normally happens once for each message,
312when you view the message for the first time in Rmail.
313To make a change in this variable take effect
314for a message that you have already viewed,
315go to that message and type \\[rmail-toggle-header] twice."
be16d955 316 :type '(choice (const nil) (regexp))
9bb97fe9 317 :group 'rmail-headers)
581d7e0b 318
8095bf23 319;;;###autoload
9bb97fe9 320(defcustom rmail-displayed-headers nil
8095bf23
RS
321 "*Regexp to match Header fields that Rmail should display.
322If nil, display all header fields except those matched by
9bb97fe9
RS
323`rmail-ignored-headers'."
324 :type '(choice regexp (const :tag "All"))
325 :group 'rmail-headers)
8095bf23 326
3db0cdac 327;;;###autoload
b926081f 328(defcustom rmail-retry-ignored-headers "^x-authentication-warning:" "\
9bb97fe9
RS
329*Headers that should be stripped when retrying a failed message."
330 :type '(choice regexp (const nil :tag "None"))
331 :group 'rmail-headers)
3db0cdac 332
072c3cd2 333;;;###autoload
9bb97fe9 334(defcustom rmail-highlighted-headers "^From:\\|^Subject:" "\
3df6812a 335*Regexp to match Header fields that Rmail should normally highlight.
765e147a 336A value of nil means don't highlight."
9bb97fe9
RS
337 :type 'regexp
338 :group 'rmail-headers)
072c3cd2 339
f7878d66 340(defface rmail-highlight
01e57329 341 '((t (:inherit highlight)))
f7878d66
RS
342 "Face to use for highlighting the most important header fields."
343 :group 'rmail-headers
344 :version "22.1")
345
765e147a
BG
346(defface rmail-header-name
347 '((t (:inherit font-lock-function-name-face)))
348 "Face to use for highlighting the header names."
349 :group 'rmail-headers
350 :version "23.1")
3df6812a 351
581d7e0b 352;;;###autoload
9bb97fe9
RS
353(defcustom rmail-delete-after-output nil "\
354*Non-nil means automatically delete a message that is copied to a file."
355 :type 'boolean
356 :group 'rmail-files)
581d7e0b
RM
357
358;;;###autoload
9bb97fe9 359(defcustom rmail-primary-inbox-list nil "\
3af9db89 360*List of files which are inboxes for user's primary mail file `~/RMAIL'.
23a7c495 361nil means the default, which is (\"/usr/spool/mail/$USER\")
3af9db89 362\(the name varies depending on the operating system,
9bb97fe9 363and the value of the environment variable MAIL overrides it)."
2079601b
RS
364 ;; Don't use backquote here, because we don't want to need it
365 ;; at load time.
366 :type (list 'choice '(const :tag "Default" nil)
65e7e846
KH
367 (list 'repeat ':value (list (or (getenv "MAIL")
368 (concat "/var/spool/mail/"
369 (getenv "USER"))))
2079601b 370 'file))
9bb97fe9
RS
371 :group 'rmail-retrieve
372 :group 'rmail-files)
581d7e0b 373
94ed51e8 374;;;###autoload
9bb97fe9 375(defcustom rmail-mail-new-frame nil
8a60950d
EZ
376 "*Non-nil means Rmail makes a new frame for composing outgoing mail.
377This is handy if you want to preserve the window configuration of
378the frame where you have the RMAIL buffer displayed."
9bb97fe9
RS
379 :type 'boolean
380 :group 'rmail-reply)
94ed51e8 381
90254bb0 382;;;###autoload
9bb97fe9
RS
383(defcustom rmail-secondary-file-directory "~/"
384 "*Directory for additional secondary Rmail files."
385 :type 'directory
386 :group 'rmail-files)
90254bb0 387;;;###autoload
9bb97fe9
RS
388(defcustom rmail-secondary-file-regexp "\\.xmail$"
389 "*Regexp for which files are secondary Rmail files."
390 :type 'regexp
391 :group 'rmail-files)
90254bb0 392
9f7c6da9 393;;;###autoload
771e5387 394(defcustom rmail-confirm-expunge 'y-or-n-p
9f7c6da9
GM
395 "*Whether and how to ask for confirmation before expunging deleted messages."
396 :type '(choice (const :tag "No confirmation" nil)
397 (const :tag "Confirm with y-or-n-p" y-or-n-p)
398 (const :tag "Confirm with yes-or-no-p" yes-or-no-p))
399 :version "21.1"
400 :group 'rmail-files)
401
6c714afe
RS
402;;;###autoload
403(defvar rmail-mode-hook nil
404 "List of functions to call when Rmail is invoked.")
405
406;;;###autoload
407(defvar rmail-get-new-mail-hook nil
408 "List of functions to call when Rmail has retrieved new mail.")
409
410;;;###autoload
187962c4
DL
411(defcustom rmail-show-message-hook nil
412 "List of functions to call when Rmail displays a message."
413 :type 'hook
4e391653 414 :options '(goto-address)
187962c4 415 :group 'rmail)
6c714afe 416
6c5def8e
GM
417;;;###autoload
418(defvar rmail-quit-hook nil
419 "List of functions to call when quitting out of Rmail.")
420
6c714afe
RS
421;;;###autoload
422(defvar rmail-delete-message-hook nil
423 "List of functions to call when Rmail deletes a message.
424When the hooks are called, the message has been marked deleted but is
425still the current message in the Rmail buffer.")
426
bd1f0f84
RS
427;; These may be altered by site-init.el to match the format of mmdf files
428;; delimiting used on a given host (delim1 and delim2 from the config
429;; files).
581d7e0b 430
95b597ce 431(defvar rmail-mmdf-delim1 "^\001\001\001\001\n"
3e0b7b44 432 "Regexp marking the start of an mmdf message.")
95b597ce 433(defvar rmail-mmdf-delim2 "^\001\001\001\001\n"
3e0b7b44 434 "Regexp marking the end of an mmdf message.")
581d7e0b 435
9cda36c0 436(defcustom rmail-message-filter nil
7424c13b
RS
437 "If non-nil, a filter function for new messages in RMAIL.
438Called with region narrowed to the message, including headers,
9cda36c0
KH
439before obeying `rmail-ignored-headers'."
440 :group 'rmail-headers
8ae63446 441 :type '(choice (const nil) function))
581d7e0b 442
ab59163d
DL
443(defcustom rmail-automatic-folder-directives nil
444 "List of directives specifying where to put a message.
445Each element of the list is of the form:
446
447 (FOLDERNAME FIELD REGEXP [ FIELD REGEXP ] ... )
448
449Where FOLDERNAME is the name of a BABYL format folder to put the
450message. If any of the field regexp's are nil, then it is ignored.
451
452If FOLDERNAME is \"/dev/null\", it is deleted.
453If FOLDERNAME is nil then it is deleted, and skipped.
454
455FIELD is the plain text name of a field in the message, such as
456\"subject\" or \"from\". A FIELD of \"to\" will automatically include
457all text from the \"cc\" field as well.
458
459REGEXP is an expression to match in the preceeding specified FIELD.
460FIELD/REGEXP pairs continue in the list.
461
462examples:
463 (\"/dev/null\" \"from\" \"@spam.com\") ; delete all mail from spam.com
f0a7da4d
GM
464 (\"RMS\" \"from\" \"rms@\") ; save all mail from RMS."
465 :group 'rmail
466 :version "21.1"
467 :type '(repeat (sexp :tag "Directive")))
67f9d50e 468
bd1f0f84
RS
469(defvar rmail-reply-prefix "Re: "
470 "String to prepend to Subject line when replying to a message.")
471
a21b845b 472;; Some mailers use "Re(2):" or "Re^2:" or "Re: Re:" or "Re[2]:".
052ec900
RS
473;; This pattern should catch all the common variants.
474;; rms: I deleted the change to delete tags in square brackets
475;; because they mess up RT tags.
476(defvar rmail-reply-regexp "\\`\\(Re\\(([0-9]+)\\|\\[[0-9]+\\]\\|\\^[0-9]+\\)?: *\\)*"
7424c13b 477 "Regexp to delete from Subject line before inserting `rmail-reply-prefix'.")
08960da1 478
9cda36c0
KH
479(defcustom rmail-display-summary nil
480 "*If non-nil, Rmail always displays the summary buffer."
481 :group 'rmail-summary
482 :type 'boolean)
483\f
581d7e0b 484(defvar rmail-inbox-list nil)
9cda36c0
KH
485(put 'rmail-inbox-list 'permanent-local t)
486
581d7e0b 487(defvar rmail-keywords nil)
9cda36c0 488(put 'rmail-keywords 'permanent-local t)
581d7e0b 489
6a338071
RS
490(defvar rmail-buffer nil
491 "The RMAIL buffer related to the current buffer.
492In an RMAIL buffer, this holds the RMAIL buffer itself.
493In a summary buffer, this holds the RMAIL buffer it is a summary for.")
494(put 'rmail-buffer 'permanent-local t)
495
581d7e0b
RM
496;; Message counters and markers. Deleted flags.
497
498(defvar rmail-current-message nil)
9cda36c0
KH
499(put 'rmail-current-message 'permanent-local t)
500
581d7e0b 501(defvar rmail-total-messages nil)
9cda36c0
KH
502(put 'rmail-total-messages 'permanent-local t)
503
581d7e0b 504(defvar rmail-message-vector nil)
9cda36c0
KH
505(put 'rmail-message-vector 'permanent-local t)
506
581d7e0b 507(defvar rmail-deleted-vector nil)
9cda36c0
KH
508(put 'rmail-deleted-vector 'permanent-local t)
509
0985b412
RS
510(defvar rmail-msgref-vector nil
511 "In an Rmail buffer, a vector whose Nth element is a list (N).
512When expunging renumbers messages, these lists are modified
513by substituting the new message number into the existing list.")
9cda36c0 514(put 'rmail-msgref-vector 'permanent-local t)
581d7e0b 515
24db549c 516(defvar rmail-overlay-list nil)
9cda36c0 517(put 'rmail-overlay-list 'permanent-local t)
a95caeed 518
581d7e0b
RM
519;; These are used by autoloaded rmail-summary.
520
521(defvar rmail-summary-buffer nil)
6a338071 522(put 'rmail-summary-buffer 'permanent-local t)
581d7e0b 523(defvar rmail-summary-vector nil)
6a338071 524(put 'rmail-summary-vector 'permanent-local t)
581d7e0b 525
9cda36c0
KH
526(defvar rmail-view-buffer nil
527 "Buffer which holds RMAIL message for MIME displaying.")
528(put 'rmail-view-buffer 'permanent-local t)
529\f
581d7e0b
RM
530;; `Sticky' default variables.
531
532;; Last individual label specified to a or k.
533(defvar rmail-last-label nil)
9cda36c0
KH
534(put 'rmail-last-label 'permanent-local t)
535
bd1f0f84 536;; Last set of values specified to C-M-n, C-M-p, C-M-s or C-M-l.
581d7e0b 537(defvar rmail-last-multi-labels nil)
9cda36c0 538
bd1f0f84 539(defvar rmail-last-regexp nil)
9cda36c0
KH
540(put 'rmail-last-regexp 'permanent-local t)
541
9bb97fe9
RS
542(defcustom rmail-default-file "~/xmail"
543 "*Default file name for \\[rmail-output]."
544 :type 'file
545 :group 'rmail-files)
546(defcustom rmail-default-rmail-file "~/XMAIL"
547 "*Default file name for \\[rmail-output-to-rmail-file]."
548 :type 'file
549 :group 'rmail-files)
966fbd00
RS
550(defcustom rmail-default-body-file "~/mailout"
551 "*Default file name for \\[rmail-output-body-to-file]."
552 :type 'file
553 :group 'rmail-files
554 :version "20.3")
46947372 555
9cda36c0
KH
556;; Mule and MIME related variables.
557
558;;;###autoload
559(defvar rmail-file-coding-system nil
560 "Coding system used in RMAIL file.
561
562This is set to nil by default.")
563
564;;;###autoload
565(defcustom rmail-enable-mime nil
566 "*If non-nil, RMAIL uses MIME feature.
567If the value is t, RMAIL automatically shows MIME decoded message.
568If the value is neither t nor nil, RMAIL does not show MIME decoded message
d9bf0f5a
KH
569until a user explicitly requires it.
570
571Even if the value is non-nil, you can't use MIME feature
572if the feature specified by `rmail-mime-feature' is not available
573in your session."
9cda36c0
KH
574 :type '(choice (const :tag "on" t)
575 (const :tag "off" nil)
16c53cdb 576 (other :tag "when asked" ask))
9cda36c0
KH
577 :group 'rmail)
578
0c773047
SZ
579(defvar rmail-enable-mime-composing nil
580 "*If non-nil, RMAIL uses `rmail-insert-mime-forwarded-message-function' to forward.")
581
9cda36c0
KH
582;;;###autoload
583(defvar rmail-show-mime-function nil
6b59a5fc
GM
584 "Function to show MIME decoded message of RMAIL file.
585This function is called when `rmail-enable-mime' is non-nil.
586It is called with no argument.")
587
588;;;###autoload
589(defvar rmail-insert-mime-forwarded-message-function nil
590 "Function to insert a message in MIME format so it can be forwarded.
67f9d50e 591This function is called if `rmail-enable-mime' or
0c773047 592`rmail-enable-mime-composing' is non-nil.
6b59a5fc
GM
593It is called with one argument FORWARD-BUFFER, which is a
594buffer containing the message to forward. The current buffer
595is the outgoing mail buffer.")
596
9652931f
GM
597;;;###autoload
598(defvar rmail-insert-mime-resent-message-function nil
599 "Function to insert a message in MIME format so it can be resent.
600This function is called if `rmail-enable-mime' is non-nil.
601It is called with one argument FORWARD-BUFFER, which is a
602buffer containing the message to forward. The current buffer
603is the outgoing mail buffer.")
604
6b59a5fc
GM
605;;;###autoload
606(defvar rmail-search-mime-message-function nil
607 "Function to check if a regexp matches a MIME message.
608This function is called if `rmail-enable-mime' is non-nil.
609It is called with two arguments MSG and REGEXP, where
610MSG is the message number, REGEXP is the regular expression.")
611
612;;;###autoload
613(defvar rmail-search-mime-header-function nil
614 "Function to check if a regexp matches a header of MIME message.
615This function is called if `rmail-enable-mime' is non-nil.
c59566eb 616It is called with three arguments MSG, REGEXP, and LIMIT, where
6b59a5fc
GM
617MSG is the message number,
618REGEXP is the regular expression,
619LIMIT is the position specifying the end of header.")
9cda36c0
KH
620
621;;;###autoload
622(defvar rmail-mime-feature 'rmail-mime
c1c92f8b
RS
623 "Feature to require to load MIME support in Rmail.
624When starting Rmail, if `rmail-enable-mime' is non-nil,
d9bf0f5a
KH
625this feature is required with `require'.
626
627The default value is `rmail-mime'. This feature is provided by
628the rmail-mime package available at <http://www.m17n.org/rmail-mime/>.")
aa2d5fe4
RS
629
630;;;###autoload
631(defvar rmail-decode-mime-charset t
632 "*Non-nil means a message is decoded by MIME's charset specification.
633If this variable is nil, or the message has not MIME specification,
634the message is decoded as normal way.
635
636If the variable `rmail-enable-mime' is non-nil, this variables is
637ignored, and all the decoding work is done by a feature specified by
638the variable `rmail-mime-feature'.")
639
640;;;###autoload
641(defvar rmail-mime-charset-pattern
0736daff 642 (concat "^content-type:[ \t]*text/plain;"
3b96a16d 643 "\\(?:[ \t\n]*\\(?:format\\|delsp\\)=\"?[-a-z0-9]+\"?;\\)*"
4f07f2e8 644 "[ \t\n]*charset=\"?\\([^ \t\n\";]+\\)\"?")
aa2d5fe4
RS
645 "Regexp to match MIME-charset specification in a header of message.
646The first parenthesized expression should match the MIME-charset name.")
647
9cda36c0 648\f
4746118a
JB
649;;; Regexp matching the delimiter of messages in UNIX mail format
650;;; (UNIX From lines), minus the initial ^. Note that if you change
651;;; this expression, you must change the code in rmail-nuke-pinhead-header
652;;; that knows the exact ordering of the \\( \\) subexpressions.
46947372 653(defvar rmail-unix-mail-delimiter
b7cceaf1 654 (let ((time-zone-regexp
258b0b37 655 (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
b7cceaf1
JB
656 "\\|[-+]?[0-9][0-9][0-9][0-9]"
657 "\\|"
658 "\\) *")))
659 (concat
660 "From "
661
74d6e31c
RS
662 ;; Many things can happen to an RFC 822 mailbox before it is put into
663 ;; a `From' line. The leading phrase can be stripped, e.g.
664 ;; `Joe <@w.x:joe@y.z>' -> `<@w.x:joe@y.z>'. The <> can be stripped, e.g.
665 ;; `<@x.y:joe@y.z>' -> `@x.y:joe@y.z'. Everything starting with a CRLF
666 ;; can be removed, e.g.
667 ;; From: joe@y.z (Joe K
668 ;; User)
669 ;; can yield `From joe@y.z (Joe K Fri Mar 22 08:11:15 1996', and
670 ;; From: Joe User
671 ;; <joe@y.z>
672 ;; can yield `From Joe User Fri Mar 22 08:11:15 1996'.
4c4a44f3
PE
673 ;; The mailbox can be removed or be replaced by white space, e.g.
674 ;; From: "Joe User"{space}{tab}
675 ;; <joe@y.z>
676 ;; can yield `From {space}{tab} Fri Mar 22 08:11:15 1996',
677 ;; where {space} and {tab} represent the Ascii space and tab characters.
74d6e31c
RS
678 ;; We want to match the results of any of these manglings.
679 ;; The following regexp rejects names whose first characters are
680 ;; obviously bogus, but after that anything goes.
4c4a44f3 681 "\\([^\0-\b\n-\r\^?].*\\)? "
b7cceaf1
JB
682
683 ;; The time the message was sent.
74d6e31c
RS
684 "\\([^\0-\r \^?]+\\) +" ; day of the week
685 "\\([^\0-\r \^?]+\\) +" ; month
686 "\\([0-3]?[0-9]\\) +" ; day of month
687 "\\([0-2][0-9]:[0-5][0-9]\\(:[0-6][0-9]\\)?\\) *" ; time of day
b7cceaf1
JB
688
689 ;; Perhaps a time zone, specified by an abbreviation, or by a
690 ;; numeric offset.
691 time-zone-regexp
692
693 ;; The year.
74d6e31c 694 " \\([0-9][0-9]+\\) *"
b7cceaf1
JB
695
696 ;; On some systems the time zone can appear after the year, too.
697 time-zone-regexp
698
881fd7eb
KH
699 ;; Old uucp cruft.
700 "\\(remote from .*\\)?"
b7cceaf1
JB
701
702 "\n"))
703 nil)
704
926f559c 705(defvar rmail-font-lock-keywords
e68b1cf1 706 ;; These are all matched case-insensitively.
926f559c 707 (eval-when-compile
9cda36c0 708 (let* ((cite-chars "[>|}]")
e68b1cf1 709 (cite-prefix "a-z")
9cda36c0 710 (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
e68b1cf1 711 (list '("^\\(From\\|Sender\\|Resent-From\\):"
765e147a
BG
712 . 'rmail-header-name)
713 '("^Reply-To:.*$" . 'rmail-header-name)
714 '("^Subject:" . 'rmail-header-name)
715 '("^X-Spam-Status:" . 'rmail-header-name)
9cda36c0 716 '("^\\(To\\|Apparently-To\\|Cc\\|Newsgroups\\):"
765e147a 717 . 'rmail-header-name)
9cda36c0
KH
718 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
719 `(,cite-chars
720 (,(concat "\\=[ \t]*"
fc2e9bcb
RS
721 "\\(\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
722 "\\(" cite-chars "[ \t]*\\)\\)+\\)"
9cda36c0
KH
723 "\\(.*\\)")
724 (beginning-of-line) (end-of-line)
fc2e9bcb
RS
725 (1 font-lock-comment-delimiter-face nil t)
726 (5 font-lock-comment-face nil t)))
e68b1cf1 727 '("^\\(X-[a-z0-9-]+\\|In-reply-to\\|Date\\):.*\\(\n[ \t]+.*\\)*$"
765e147a 728 . 'rmail-header-name))))
926f559c 729 "Additional expressions to highlight in Rmail mode.")
9cda36c0 730
bd1f0f84
RS
731;; Perform BODY in the summary buffer
732;; in such a way that its cursor is properly updated in its own window.
733(defmacro rmail-select-summary (&rest body)
b787fc05
GM
734 `(let ((total rmail-total-messages))
735 (if (rmail-summary-displayed)
736 (let ((window (selected-window)))
737 (save-excursion
738 (unwind-protect
739 (progn
740 (pop-to-buffer rmail-summary-buffer)
741 ;; rmail-total-messages is a buffer-local var
742 ;; in the rmail buffer.
743 ;; This way we make it available for the body
744 ;; even tho the rmail buffer is not current.
745 (let ((rmail-total-messages total))
746 ,@body))
747 (select-window window))))
748 (save-excursion
749 (set-buffer rmail-summary-buffer)
750 (let ((rmail-total-messages total))
751 ,@body)))
752 (rmail-maybe-display-summary)))
581d7e0b
RM
753\f
754;;;; *** Rmail Mode ***
755
f90c23ca
GM
756;; This variable is dynamically bound. The defvar is here to placate
757;; the byte compiler.
758
7d862e07
KH
759(defvar rmail-enable-multibyte nil)
760
f90c23ca
GM
761
762(defun rmail-require-mime-maybe ()
763 "Require `rmail-mime-feature' if that is non-nil.
764Signal an error and set `rmail-mime-feature' to nil if the feature
765isn't provided."
766 (when rmail-enable-mime
767 (condition-case err
768 (require rmail-mime-feature)
769 (error
d9bf0f5a
KH
770 (display-warning
771 :warning
772 (format "Although MIME support is requested
773by setting `rmail-enable-mime' to non-nil, the required feature
774`%s' (the value of `rmail-mime-feature')
775is not available in the current session.
776So, the MIME support is turned off for the moment."
777 rmail-mime-feature))
f90c23ca
GM
778 (setq rmail-enable-mime nil)))))
779
780
581d7e0b
RM
781;;;###autoload
782(defun rmail (&optional file-name-arg)
783 "Read and edit incoming mail.
c42722e3 784Moves messages into file named by `rmail-file-name' (a babyl format file)
581d7e0b
RM
785 and edits that file in RMAIL Mode.
786Type \\[describe-mode] once editing that file, for a list of RMAIL commands.
787
d2fc297c
RS
788May be called with file name as argument; then performs rmail editing on
789that file, but does not copy any new mail into the file.
790Interactively, if you supply a prefix argument, then you
a2e667ec
RS
791have a chance to specify a file name with the minibuffer.
792
793If `rmail-display-summary' is non-nil, make a summary for this RMAIL file."
581d7e0b 794 (interactive (if current-prefix-arg
b5e10b23 795 (list (read-file-name "Run rmail on RMAIL file: "))))
f90c23ca 796 (rmail-require-mime-maybe)
581d7e0b 797 (let* ((file-name (expand-file-name (or file-name-arg rmail-file-name)))
85eba339
EZ
798 ;; Use find-buffer-visiting, not get-file-buffer, for those users
799 ;; who have find-file-visit-truename set to t.
800 (existed (find-buffer-visiting file-name))
1bbdda4d 801 ;; This binding is necessary because we must decide if we
55e2a603
KH
802 ;; need code conversion while the buffer is unibyte
803 ;; (i.e. enable-multibyte-characters is nil).
1ed79e41 804 (rmail-enable-multibyte
67f9d50e 805 (if existed
1ed79e41
RS
806 (with-current-buffer existed enable-multibyte-characters)
807 (default-value 'enable-multibyte-characters)))
7d862e07
KH
808 ;; Since the file may contain messages of different encodings
809 ;; at the tail (non-BYBYL part), we can't decode them at once
810 ;; on reading. So, at first, we read the file without text
811 ;; code conversion, then decode the messages one by one by
812 ;; rmail-decode-babyl-format or
813 ;; rmail-convert-to-babyl-format.
814 (coding-system-for-read (and rmail-enable-multibyte 'raw-text))
3ba3fa75 815 run-mail-hook msg-shown)
581d7e0b
RM
816 ;; Like find-file, but in the case where a buffer existed
817 ;; and the file was reverted, recompute the message-data.
078395ab
RS
818 ;; We used to bind enable-local-variables to nil here,
819 ;; but that should not be needed now that rmail-mode
820 ;; sets it locally to nil.
821 ;; (Binding a variable locally with let is not safe if it has
822 ;; buffer-local bindings.)
581d7e0b
RM
823 (if (and existed (not (verify-visited-file-modtime existed)))
824 (progn
078395ab 825 (find-file file-name)
bd1f0f84
RS
826 (if (and (verify-visited-file-modtime existed)
827 (eq major-mode 'rmail-mode))
581d7e0b
RM
828 (progn (rmail-forget-messages)
829 (rmail-set-message-counters))))
67f9d50e 830 (switch-to-buffer
4cc4a962
RS
831 (let ((enable-local-variables nil))
832 (find-file-noselect file-name))))
bd1f0f84 833 (if (eq major-mode 'rmail-edit-mode)
3210e730 834 (error "Exit Rmail Edit mode before getting new mail"))
581d7e0b
RM
835 (if (and existed (> (buffer-size) 0))
836 ;; Buffer not new and not empty; ensure in proper mode, but that's all.
837 (or (eq major-mode 'rmail-mode)
6af28e9f
RS
838 (progn (rmail-mode-2)
839 (setq run-mail-hook t)))
840 (setq run-mail-hook t)
581d7e0b 841 (rmail-mode-2)
581d7e0b
RM
842 ;; Convert all or part to Babyl file if possible.
843 (rmail-convert-file)
b5e00b02 844 (goto-char (point-max)))
e995c415
RS
845 ;; As we have read a file by raw-text, the buffer is set to
846 ;; unibyte. We must make it multibyte if necessary.
847 (if (and rmail-enable-multibyte
848 (not enable-multibyte-characters))
849 (set-buffer-multibyte t))
f4b5ab4c
RS
850 ;; If necessary, scan to find all the messages.
851 (rmail-maybe-set-message-counters)
afc070d1 852 (unwind-protect
b5e00b02
RS
853 (unless (and (not file-name-arg)
854 (rmail-get-new-mail))
855 (rmail-show-message (rmail-first-unseen-message)))
afc070d1
KH
856 (progn
857 (if rmail-display-summary (rmail-summary))
858 (rmail-construct-io-menu)
859 (if run-mail-hook
860 (run-hooks 'rmail-mode-hook))))))
581d7e0b
RM
861
862;; Given the value of MAILPATH, return a list of inbox file names.
863;; This is turned off because it is not clear that the user wants
864;; all these inboxes to feed into the primary rmail file.
865; (defun rmail-convert-mailpath (string)
866; (let (idx list)
867; (while (setq idx (string-match "[%:]" string))
868; (let ((this (substring string 0 idx)))
869; (setq string (substring string (1+ idx)))
870; (setq list (cons (if (string-match "%" this)
871; (substring this 0 (string-match "%" this))
872; this)
873; list))))
874; list))
875
876; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
877; will not cause emacs 18.55 problems.
878
578b6415
RS
879;; This calls rmail-decode-babyl-format if the file is already Babyl.
880
581d7e0b
RM
881(defun rmail-convert-file ()
882 (let (convert)
883 (widen)
884 (goto-char (point-min))
885 ;; If file doesn't start like a Babyl file,
886 ;; convert it to one, by adding a header and converting each message.
887 (cond ((looking-at "BABYL OPTIONS:"))
888 ((looking-at "Version: 5\n")
889 ;; Losing babyl file made by old version of Rmail.
890 ;; Just fix the babyl file header; don't make a new one,
891 ;; so we don't lose the Labels: file attribute, etc.
892 (let ((buffer-read-only nil))
893 (insert "BABYL OPTIONS: -*- rmail -*-\n")))
760a3528
BF
894 ((equal (point-min) (point-max))
895 ;; Empty RMAIL file. Just insert the header.
896 (rmail-insert-rmail-file-header))
581d7e0b 897 (t
760a3528 898 ;; Non-empty file in non-RMAIL format. Add header and convert.
581d7e0b
RM
899 (setq convert t)
900 (rmail-insert-rmail-file-header)))
901 ;; If file was not a Babyl file or if there are
902 ;; Unix format messages added at the end,
903 ;; convert file as necessary.
904 (if (or convert
e3e87be8
RS
905 (save-excursion
906 (goto-char (point-max))
0b40717e
RS
907 (search-backward "\n\^_")
908 (forward-char 2)
e3e87be8 909 (looking-at "\n*From ")))
581d7e0b
RM
910 (let ((buffer-read-only nil))
911 (message "Converting to Babyl format...")
e3e87be8
RS
912 ;; If file needs conversion, convert it all,
913 ;; except for the BABYL header.
914 ;; (rmail-convert-to-babyl-format would delete the header.)
e3e87be8
RS
915 (goto-char (point-min))
916 (search-forward "\n\^_" nil t)
073fb956 917 (narrow-to-region (point) (point-max))
581d7e0b 918 (rmail-convert-to-babyl-format)
7d862e07
KH
919 (message "Converting to Babyl format...done"))
920 (if (and (not rmail-enable-mime)
921 rmail-enable-multibyte)
922 ;; We still have to decode BABYL part.
923 (rmail-decode-babyl-format)))))
581d7e0b 924
581d7e0b
RM
925(defun rmail-insert-rmail-file-header ()
926 (let ((buffer-read-only nil))
578b6415
RS
927 ;; -*-rmail-*- is here so that visiting the file normally
928 ;; recognizes it as an Rmail file.
581d7e0b
RM
929 (insert "BABYL OPTIONS: -*- rmail -*-
930Version: 5
931Labels:
932Note: This is the header of an rmail file.
933Note: If you are seeing it in rmail,
934Note: it means the file has no messages in it.\n\^_")))
935
95b56cdf 936;; Decode Babyl formatted part at the head of current buffer by
7d862e07 937;; rmail-file-coding-system, or if it is nil, do auto conversion.
d1e69dec
KH
938
939(defun rmail-decode-babyl-format ()
940 (let ((modifiedp (buffer-modified-p))
941 (buffer-read-only nil)
578b6415 942 (coding-system rmail-file-coding-system)
7d862e07 943 from to)
d1e69dec 944 (goto-char (point-min))
578b6415 945 (search-forward "\n\^_" nil t) ; Skip BABYL header.
7d862e07
KH
946 (setq from (point))
947 (goto-char (point-max))
948 (search-backward "\n\^_" from 'mv)
949 (setq to (point))
578b6415
RS
950 (unless (and coding-system
951 (coding-system-p coding-system))
1bbdda4d 952 (setq coding-system
9d2cd7ef
KH
953 ;; If rmail-file-coding-system is nil, Emacs 21 writes
954 ;; RMAIL files in emacs-mule, Emacs 22 in utf-8, but
955 ;; earlier versions did that with the current buffer's
956 ;; encoding. So we want to favor detection of emacs-mule
957 ;; (whose normal priority is quite low) and utf-8, but
958 ;; still allow detection of other encodings if they won't
959 ;; fit. The call to with-coding-priority below achieves
960 ;; that.
961 (with-coding-priority '(emacs-mule utf-8)
962 (detect-coding-region from to 'highest))))
963 (unless (eq (coding-system-type coding-system) 'undecided)
b68a01e7 964 (set-buffer-modified-p t) ; avoid locking when decoding
19032340
RS
965 (let ((buffer-undo-list t))
966 (decode-coding-region from to coding-system))
b68a01e7 967 (setq coding-system last-coding-system-used))
578b6415
RS
968 (set-buffer-modified-p modifiedp)
969 (setq buffer-file-coding-system nil)
970 (setq save-buffer-coding-system
971 (or coding-system 'undecided))))
d1e69dec 972
9cda36c0 973(defvar rmail-mode-map nil)
581d7e0b
RM
974(if rmail-mode-map
975 nil
976 (setq rmail-mode-map (make-keymap))
977 (suppress-keymap rmail-mode-map)
bd1f0f84
RS
978 (define-key rmail-mode-map "a" 'rmail-add-label)
979 (define-key rmail-mode-map "b" 'rmail-bury)
980 (define-key rmail-mode-map "c" 'rmail-continue)
981 (define-key rmail-mode-map "d" 'rmail-delete-forward)
982 (define-key rmail-mode-map "\C-d" 'rmail-delete-backward)
983 (define-key rmail-mode-map "e" 'rmail-edit-current-message)
984 (define-key rmail-mode-map "f" 'rmail-forward)
985 (define-key rmail-mode-map "g" 'rmail-get-new-mail)
986 (define-key rmail-mode-map "h" 'rmail-summary)
987 (define-key rmail-mode-map "i" 'rmail-input)
988 (define-key rmail-mode-map "j" 'rmail-show-message)
989 (define-key rmail-mode-map "k" 'rmail-kill-label)
990 (define-key rmail-mode-map "l" 'rmail-summary-by-labels)
581d7e0b 991 (define-key rmail-mode-map "\e\C-h" 'rmail-summary)
581d7e0b
RM
992 (define-key rmail-mode-map "\e\C-l" 'rmail-summary-by-labels)
993 (define-key rmail-mode-map "\e\C-r" 'rmail-summary-by-recipients)
994 (define-key rmail-mode-map "\e\C-s" 'rmail-summary-by-regexp)
bd1f0f84
RS
995 (define-key rmail-mode-map "\e\C-t" 'rmail-summary-by-topic)
996 (define-key rmail-mode-map "m" 'rmail-mail)
997 (define-key rmail-mode-map "\em" 'rmail-retry-failure)
998 (define-key rmail-mode-map "n" 'rmail-next-undeleted-message)
999 (define-key rmail-mode-map "\en" 'rmail-next-message)
1000 (define-key rmail-mode-map "\e\C-n" 'rmail-next-labeled-message)
1001 (define-key rmail-mode-map "o" 'rmail-output-to-rmail-file)
1002 (define-key rmail-mode-map "\C-o" 'rmail-output)
1003 (define-key rmail-mode-map "p" 'rmail-previous-undeleted-message)
1004 (define-key rmail-mode-map "\ep" 'rmail-previous-message)
1005 (define-key rmail-mode-map "\e\C-p" 'rmail-previous-labeled-message)
1006 (define-key rmail-mode-map "q" 'rmail-quit)
1007 (define-key rmail-mode-map "r" 'rmail-reply)
ed7ace63 1008;; I find I can't live without the default M-r command -- rms.
bd1f0f84
RS
1009;; (define-key rmail-mode-map "\er" 'rmail-search-backwards)
1010 (define-key rmail-mode-map "s" 'rmail-expunge-and-save)
1011 (define-key rmail-mode-map "\es" 'rmail-search)
1012 (define-key rmail-mode-map "t" 'rmail-toggle-header)
1013 (define-key rmail-mode-map "u" 'rmail-undelete-previous-message)
5eb9cf97 1014 (define-key rmail-mode-map "w" 'rmail-output-body-to-file)
bd1f0f84
RS
1015 (define-key rmail-mode-map "x" 'rmail-expunge)
1016 (define-key rmail-mode-map "." 'rmail-beginning-of-message)
3deac4a1 1017 (define-key rmail-mode-map "/" 'rmail-end-of-message)
bd1f0f84
RS
1018 (define-key rmail-mode-map "<" 'rmail-first-message)
1019 (define-key rmail-mode-map ">" 'rmail-last-message)
1020 (define-key rmail-mode-map " " 'scroll-up)
1021 (define-key rmail-mode-map "\177" 'scroll-down)
1022 (define-key rmail-mode-map "?" 'describe-mode)
3bf526cf
RS
1023 (define-key rmail-mode-map "\C-c\C-s\C-d" 'rmail-sort-by-date)
1024 (define-key rmail-mode-map "\C-c\C-s\C-s" 'rmail-sort-by-subject)
1025 (define-key rmail-mode-map "\C-c\C-s\C-a" 'rmail-sort-by-author)
1026 (define-key rmail-mode-map "\C-c\C-s\C-r" 'rmail-sort-by-recipient)
1027 (define-key rmail-mode-map "\C-c\C-s\C-c" 'rmail-sort-by-correspondent)
1028 (define-key rmail-mode-map "\C-c\C-s\C-l" 'rmail-sort-by-lines)
f851a71c 1029 (define-key rmail-mode-map "\C-c\C-s\C-k" 'rmail-sort-by-labels)
60fb2b34
RS
1030 (define-key rmail-mode-map "\C-c\C-n" 'rmail-next-same-subject)
1031 (define-key rmail-mode-map "\C-c\C-p" 'rmail-previous-same-subject)
bd1f0f84 1032 )
5201ddda
RS
1033\f
1034(define-key rmail-mode-map [menu-bar] (make-sparse-keymap))
1035
1036(define-key rmail-mode-map [menu-bar classify]
1037 (cons "Classify" (make-sparse-keymap "Classify")))
1038
4139208e 1039(define-key rmail-mode-map [menu-bar classify input-menu]
8b7ef584 1040 nil)
4139208e
RS
1041
1042(define-key rmail-mode-map [menu-bar classify output-menu]
8b7ef584 1043 nil)
4139208e 1044
5eb9cf97
RS
1045(define-key rmail-mode-map [menu-bar classify output-body]
1046 '("Output body to file..." . rmail-output-body-to-file))
1047
5201ddda 1048(define-key rmail-mode-map [menu-bar classify output-inbox]
37fdc96f 1049 '("Output (inbox)..." . rmail-output))
5201ddda
RS
1050
1051(define-key rmail-mode-map [menu-bar classify output]
37fdc96f 1052 '("Output (Rmail)..." . rmail-output-to-rmail-file))
5201ddda
RS
1053
1054(define-key rmail-mode-map [menu-bar classify kill-label]
37fdc96f 1055 '("Kill Label..." . rmail-kill-label))
5201ddda
RS
1056
1057(define-key rmail-mode-map [menu-bar classify add-label]
37fdc96f 1058 '("Add Label..." . rmail-add-label))
5201ddda
RS
1059
1060(define-key rmail-mode-map [menu-bar summary]
1061 (cons "Summary" (make-sparse-keymap "Summary")))
1062
c0a5a948
RS
1063(define-key rmail-mode-map [menu-bar summary senders]
1064 '("By Senders..." . rmail-summary-by-senders))
1065
5201ddda 1066(define-key rmail-mode-map [menu-bar summary labels]
37fdc96f 1067 '("By Labels..." . rmail-summary-by-labels))
5201ddda
RS
1068
1069(define-key rmail-mode-map [menu-bar summary recipients]
37fdc96f 1070 '("By Recipients..." . rmail-summary-by-recipients))
5201ddda
RS
1071
1072(define-key rmail-mode-map [menu-bar summary topic]
37fdc96f 1073 '("By Topic..." . rmail-summary-by-topic))
5201ddda
RS
1074
1075(define-key rmail-mode-map [menu-bar summary regexp]
37fdc96f 1076 '("By Regexp..." . rmail-summary-by-regexp))
5201ddda
RS
1077
1078(define-key rmail-mode-map [menu-bar summary all]
1079 '("All" . rmail-summary))
1080
1081(define-key rmail-mode-map [menu-bar mail]
1082 (cons "Mail" (make-sparse-keymap "Mail")))
386c099f 1083
5358bc42 1084(define-key rmail-mode-map [menu-bar mail rmail-get-new-mail]
12dd87fc
RS
1085 '("Get New Mail" . rmail-get-new-mail))
1086
1087(define-key rmail-mode-map [menu-bar mail lambda]
6ae1efb4 1088 '("----"))
12dd87fc 1089
5201ddda
RS
1090(define-key rmail-mode-map [menu-bar mail continue]
1091 '("Continue" . rmail-continue))
1092
754b4eb7 1093(define-key rmail-mode-map [menu-bar mail resend]
37fdc96f 1094 '("Re-send..." . rmail-resend))
754b4eb7 1095
5201ddda
RS
1096(define-key rmail-mode-map [menu-bar mail forward]
1097 '("Forward" . rmail-forward))
1098
1099(define-key rmail-mode-map [menu-bar mail retry]
1100 '("Retry" . rmail-retry-failure))
1101
1102(define-key rmail-mode-map [menu-bar mail reply]
1103 '("Reply" . rmail-reply))
1104
1105(define-key rmail-mode-map [menu-bar mail mail]
1106 '("Mail" . rmail-mail))
1107
1108(define-key rmail-mode-map [menu-bar delete]
1109 (cons "Delete" (make-sparse-keymap "Delete")))
1110
1111(define-key rmail-mode-map [menu-bar delete expunge/save]
1112 '("Expunge/Save" . rmail-expunge-and-save))
581d7e0b 1113
5201ddda
RS
1114(define-key rmail-mode-map [menu-bar delete expunge]
1115 '("Expunge" . rmail-expunge))
1116
1117(define-key rmail-mode-map [menu-bar delete undelete]
1118 '("Undelete" . rmail-undelete-previous-message))
1119
1120(define-key rmail-mode-map [menu-bar delete delete]
1121 '("Delete" . rmail-delete-forward))
1122
1123(define-key rmail-mode-map [menu-bar move]
1124 (cons "Move" (make-sparse-keymap "Move")))
1125
1126(define-key rmail-mode-map [menu-bar move search-back]
342aa439 1127 '("Search Back..." . rmail-search-backwards))
5201ddda
RS
1128
1129(define-key rmail-mode-map [menu-bar move search]
37fdc96f 1130 '("Search..." . rmail-search))
5201ddda
RS
1131
1132(define-key rmail-mode-map [menu-bar move previous]
1133 '("Previous Nondeleted" . rmail-previous-undeleted-message))
1134
1135(define-key rmail-mode-map [menu-bar move next]
1136 '("Next Nondeleted" . rmail-next-undeleted-message))
1137
1138(define-key rmail-mode-map [menu-bar move last]
1139 '("Last" . rmail-last-message))
1140
1141(define-key rmail-mode-map [menu-bar move first]
1142 '("First" . rmail-first-message))
1143
1144(define-key rmail-mode-map [menu-bar move previous]
1145 '("Previous" . rmail-previous-message))
1146
1147(define-key rmail-mode-map [menu-bar move next]
1148 '("Next" . rmail-next-message))
1da6a64d
EZ
1149
1150;; Rmail toolbar
1151(defvar rmail-tool-bar-map
1152 (if (display-graphic-p)
1153 (let ((map (make-sparse-keymap)))
1154 (tool-bar-local-item-from-menu 'rmail-get-new-mail "mail/inbox"
1155 map rmail-mode-map)
1156 (tool-bar-local-item-from-menu 'rmail-next-undeleted-message "right-arrow"
1157 map rmail-mode-map)
1158 (tool-bar-local-item-from-menu 'rmail-previous-undeleted-message "left-arrow"
1159 map rmail-mode-map)
1160 (tool-bar-local-item-from-menu 'rmail-search "search"
1161 map rmail-mode-map)
1162 (tool-bar-local-item-from-menu 'rmail-input "open"
1163 map rmail-mode-map)
1164 (tool-bar-local-item-from-menu 'rmail-mail "mail/compose"
1165 map rmail-mode-map)
1166 (tool-bar-local-item-from-menu 'rmail-reply "mail/reply-all"
1167 map rmail-mode-map)
1168 (tool-bar-local-item-from-menu 'rmail-forward "mail/forward"
1169 map rmail-mode-map)
1170 (tool-bar-local-item-from-menu 'rmail-delete-forward "close"
1171 map rmail-mode-map)
1172 (tool-bar-local-item-from-menu 'rmail-output "mail/move"
1173 map rmail-mode-map)
1174 (tool-bar-local-item-from-menu 'rmail-output-body-to-file "mail/save"
1175 map rmail-mode-map)
1176 (tool-bar-local-item-from-menu 'rmail-expunge "delete"
1177 map rmail-mode-map)
1178 map)))
1179
1180
5201ddda 1181\f
581d7e0b
RM
1182;; Rmail mode is suitable only for specially formatted data.
1183(put 'rmail-mode 'mode-class 'special)
1184
3f320f98
RS
1185(defun rmail-mode-kill-summary ()
1186 (if rmail-summary-buffer (kill-buffer rmail-summary-buffer)))
1187
9712b0bd 1188;;;###autoload
581d7e0b
RM
1189(defun rmail-mode ()
1190 "Rmail Mode is used by \\<rmail-mode-map>\\[rmail] for editing Rmail files.
1191All normal editing commands are turned off.
1192Instead, these commands are available:
1193
3deac4a1
EZ
1194\\[rmail-beginning-of-message] Move point to front of this message.
1195\\[rmail-end-of-message] Move point to bottom of this message.
581d7e0b
RM
1196\\[scroll-up] Scroll to next screen of this message.
1197\\[scroll-down] Scroll to previous screen of this message.
1198\\[rmail-next-undeleted-message] Move to Next non-deleted message.
1199\\[rmail-previous-undeleted-message] Move to Previous non-deleted message.
1200\\[rmail-next-message] Move to Next message whether deleted or not.
1201\\[rmail-previous-message] Move to Previous message whether deleted or not.
1202\\[rmail-first-message] Move to the first message in Rmail file.
1203\\[rmail-last-message] Move to the last message in Rmail file.
1204\\[rmail-show-message] Jump to message specified by numeric position in file.
1205\\[rmail-search] Search for string and show message it is found in.
1206\\[rmail-delete-forward] Delete this message, move to next nondeleted.
1207\\[rmail-delete-backward] Delete this message, move to previous nondeleted.
1208\\[rmail-undelete-previous-message] Undelete message. Tries current message, then earlier messages
1209 till a deleted message is found.
bd1f0f84 1210\\[rmail-edit-current-message] Edit the current message. \\[rmail-cease-edit] to return to Rmail.
581d7e0b
RM
1211\\[rmail-expunge] Expunge deleted messages.
1212\\[rmail-expunge-and-save] Expunge and save the file.
1213\\[rmail-quit] Quit Rmail: expunge, save, then switch to another buffer.
1214\\[save-buffer] Save without expunging.
3af9db89 1215\\[rmail-get-new-mail] Move new mail from system spool directory into this file.
581d7e0b
RM
1216\\[rmail-mail] Mail a message (same as \\[mail-other-window]).
1217\\[rmail-continue] Continue composing outgoing message started before.
bd1f0f84
RS
1218\\[rmail-reply] Reply to this message. Like \\[rmail-mail] but initializes some fields.
1219\\[rmail-retry-failure] Send this message again. Used on a mailer failure message.
581d7e0b
RM
1220\\[rmail-forward] Forward this message to another user.
1221\\[rmail-output-to-rmail-file] Output this message to an Rmail file (append it).
1222\\[rmail-output] Output this message to a Unix-format mail file (append it).
74ae80cd 1223\\[rmail-output-body-to-file] Save message body to a file. Default filename comes from Subject line.
581d7e0b
RM
1224\\[rmail-input] Input Rmail file. Run Rmail on that file.
1225\\[rmail-add-label] Add label to message. It will be displayed in the mode line.
1226\\[rmail-kill-label] Kill label. Remove a label from current message.
1227\\[rmail-next-labeled-message] Move to Next message with specified label
1228 (label defaults to last one specified).
1229 Standard labels: filed, unseen, answered, forwarded, deleted.
bd1f0f84 1230 Any other label is present only if you add it with \\[rmail-add-label].
581d7e0b
RM
1231\\[rmail-previous-labeled-message] Move to Previous message with specified label
1232\\[rmail-summary] Show headers buffer, with a one line summary of each message.
bd1f0f84
RS
1233\\[rmail-summary-by-labels] Summarize only messages with particular label(s).
1234\\[rmail-summary-by-recipients] Summarize only messages with particular recipient(s).
1235\\[rmail-summary-by-regexp] Summarize only messages with particular regexp(s).
1236\\[rmail-summary-by-topic] Summarize only messages with subject line regexp(s).
1237\\[rmail-toggle-header] Toggle display of complete header."
581d7e0b 1238 (interactive)
f90c23ca
GM
1239 (let ((finding-rmail-file (not (eq major-mode 'rmail-mode))))
1240 (rmail-mode-2)
1241 (when (and finding-rmail-file
1242 (null coding-system-for-read)
1243 default-enable-multibyte-characters)
1244 (let ((rmail-enable-multibyte t))
1245 (rmail-require-mime-maybe)
1246 (rmail-convert-file)
1247 (goto-char (point-max))
1248 (set-buffer-multibyte t)))
1249 (rmail-set-message-counters)
1250 (rmail-show-message rmail-total-messages)
1251 (when finding-rmail-file
1252 (when rmail-display-summary
1253 (rmail-summary))
1254 (rmail-construct-io-menu))
6f9402eb 1255 (run-mode-hooks 'rmail-mode-hook)))
581d7e0b
RM
1256
1257(defun rmail-mode-2 ()
1258 (kill-all-local-variables)
1259 (rmail-mode-1)
9cda36c0 1260 (rmail-perm-variables)
6af28e9f 1261 (rmail-variables))
581d7e0b
RM
1262
1263(defun rmail-mode-1 ()
1264 (setq major-mode 'rmail-mode)
1265 (setq mode-name "RMAIL")
1266 (setq buffer-read-only t)
876cd689
RS
1267 ;; No need to auto save RMAIL files in normal circumstances
1268 ;; because they contain no info except attribute changes
1269 ;; and deletion of messages.
1270 ;; The one exception is when messages are copied into an Rmail mode buffer.
1271 ;; rmail-output-to-rmail-file enables auto save when you do that.
581d7e0b 1272 (setq buffer-auto-save-file-name nil)
e823bfa3 1273 (setq mode-line-modified "--")
581d7e0b
RM
1274 (use-local-map rmail-mode-map)
1275 (set-syntax-table text-mode-syntax-table)
1276 (setq local-abbrev-table text-mode-abbrev-table))
1277
9cda36c0
KH
1278;; Set up the permanent locals associated with an Rmail file.
1279(defun rmail-perm-variables ()
581d7e0b 1280 (make-local-variable 'rmail-last-label)
bd1f0f84 1281 (make-local-variable 'rmail-last-regexp)
581d7e0b 1282 (make-local-variable 'rmail-deleted-vector)
d1e69dec
KH
1283 (make-local-variable 'rmail-buffer)
1284 (setq rmail-buffer (current-buffer))
1285 (make-local-variable 'rmail-view-buffer)
1286 (setq rmail-view-buffer rmail-buffer)
581d7e0b
RM
1287 (make-local-variable 'rmail-summary-buffer)
1288 (make-local-variable 'rmail-summary-vector)
1289 (make-local-variable 'rmail-current-message)
1290 (make-local-variable 'rmail-total-messages)
24db549c
RS
1291 (make-local-variable 'rmail-overlay-list)
1292 (setq rmail-overlay-list nil)
581d7e0b 1293 (make-local-variable 'rmail-message-vector)
0985b412 1294 (make-local-variable 'rmail-msgref-vector)
581d7e0b
RM
1295 (make-local-variable 'rmail-inbox-list)
1296 (setq rmail-inbox-list (rmail-parse-file-inboxes))
849056cc
RS
1297 ;; Provide default set of inboxes for primary mail file ~/RMAIL.
1298 (and (null rmail-inbox-list)
6cd37f8f 1299 (or (equal buffer-file-name (expand-file-name rmail-file-name))
a38837b5
RS
1300 (equal buffer-file-truename
1301 (abbreviate-file-name (file-truename rmail-file-name))))
849056cc
RS
1302 (setq rmail-inbox-list
1303 (or rmail-primary-inbox-list
1304 (list (or (getenv "MAIL")
1305 (concat rmail-spool-directory
d0eecffa 1306 (user-login-name)))))))
581d7e0b 1307 (make-local-variable 'rmail-keywords)
1da6a64d 1308 (set (make-local-variable 'tool-bar-map) rmail-tool-bar-map)
581d7e0b 1309 ;; this gets generated as needed
6016579d 1310 (setq rmail-keywords nil))
581d7e0b 1311
9cda36c0
KH
1312;; Set up the non-permanent locals associated with Rmail mode.
1313(defun rmail-variables ()
578b6415
RS
1314 (make-local-variable 'save-buffer-coding-system)
1315 ;; If we don't already have a value for save-buffer-coding-system,
1316 ;; get it from buffer-file-coding-system, and clear that
1317 ;; because it should be determined in rmail-show-message.
1318 (unless save-buffer-coding-system
1319 (setq save-buffer-coding-system (or buffer-file-coding-system 'undecided))
1320 (setq buffer-file-coding-system nil))
9cda36c0 1321 ;; Don't let a local variables list in a message cause confusion.
4cc4a962
RS
1322 (make-local-variable 'local-enable-local-variables)
1323 (setq local-enable-local-variables nil)
9cda36c0
KH
1324 (make-local-variable 'revert-buffer-function)
1325 (setq revert-buffer-function 'rmail-revert)
1326 (make-local-variable 'font-lock-defaults)
1327 (setq font-lock-defaults
578b6415 1328 '(rmail-font-lock-keywords
e68b1cf1 1329 t t nil nil
578b6415
RS
1330 (font-lock-maximum-size . nil)
1331 (font-lock-fontify-buffer-function . rmail-fontify-buffer-function)
1332 (font-lock-unfontify-buffer-function . rmail-unfontify-buffer-function)
1333 (font-lock-inhibit-thing-lock . (lazy-lock-mode fast-lock-mode))))
9cda36c0
KH
1334 (make-local-variable 'require-final-newline)
1335 (setq require-final-newline nil)
1336 (make-local-variable 'version-control)
1337 (setq version-control 'never)
1338 (make-local-variable 'kill-buffer-hook)
1339 (add-hook 'kill-buffer-hook 'rmail-mode-kill-summary)
1340 (make-local-variable 'file-precious-flag)
2b5f7b7f
LH
1341 (setq file-precious-flag t)
1342 (make-local-variable 'desktop-save-buffer)
1343 (setq desktop-save-buffer t))
9cda36c0 1344
581d7e0b
RM
1345;; Handle M-x revert-buffer done in an rmail-mode buffer.
1346(defun rmail-revert (arg noconfirm)
9652931f 1347 (set-buffer rmail-buffer)
1ed79e41
RS
1348 (let* ((revert-buffer-function (default-value 'revert-buffer-function))
1349 (rmail-enable-multibyte enable-multibyte-characters)
1350 ;; See similar code in `rmail'.
1351 (coding-system-for-read (and rmail-enable-multibyte 'raw-text)))
581d7e0b
RM
1352 ;; Call our caller again, but this time it does the default thing.
1353 (if (revert-buffer arg noconfirm)
1354 ;; If the user said "yes", and we changed something,
1355 ;; reparse the messages.
3210e730 1356 (progn
9652931f
GM
1357 (set-buffer rmail-buffer)
1358 (rmail-mode-2)
1ed79e41 1359 ;; Convert all or part to Babyl file if possible.
581d7e0b 1360 (rmail-convert-file)
1ed79e41
RS
1361 ;; We have read the file as raw-text, so the buffer is set to
1362 ;; unibyte. Make it multibyte if necessary.
1363 (if (and rmail-enable-multibyte
1364 (not enable-multibyte-characters))
1365 (set-buffer-multibyte t))
581d7e0b 1366 (goto-char (point-max))
1ed79e41
RS
1367 (rmail-set-message-counters)
1368 (rmail-show-message rmail-total-messages)
1369 (run-hooks 'rmail-mode-hook)))))
581d7e0b
RM
1370
1371;; Return a list of files from this buffer's Mail: option.
1372;; Does not assume that messages have been parsed.
1373;; Just returns nil if buffer does not look like Babyl format.
1374(defun rmail-parse-file-inboxes ()
1375 (save-excursion
1376 (save-restriction
1377 (widen)
1378 (goto-char 1)
1379 (cond ((looking-at "BABYL OPTIONS:")
1380 (search-forward "\n\^_" nil 'move)
1381 (narrow-to-region 1 (point))
1382 (goto-char 1)
1383 (if (search-forward "\nMail:" nil t)
1384 (progn
1385 (narrow-to-region (point) (progn (end-of-line) (point)))
1386 (goto-char (point-min))
1387 (mail-parse-comma-list))))))))
1388
1389(defun rmail-expunge-and-save ()
1390 "Expunge and save RMAIL file."
1391 (interactive)
1392 (rmail-expunge)
6b59a5fc 1393 (set-buffer rmail-buffer)
c7065819
KH
1394 (save-buffer)
1395 (if (rmail-summary-exists)
1396 (rmail-select-summary (set-buffer-modified-p nil))))
581d7e0b
RM
1397
1398(defun rmail-quit ()
dd5ee393
RS
1399 "Quit out of RMAIL.
1400Hook `rmail-quit-hook' is run after expunging."
581d7e0b
RM
1401 (interactive)
1402 (rmail-expunge-and-save)
dd5ee393
RS
1403 (when (boundp 'rmail-quit-hook)
1404 (run-hooks 'rmail-quit-hook))
581d7e0b 1405 ;; Don't switch to the summary buffer even if it was recently visible.
df211784
RS
1406 (when rmail-summary-buffer
1407 (replace-buffer-in-windows rmail-summary-buffer)
1408 (bury-buffer rmail-summary-buffer))
6b59a5fc
GM
1409 (if rmail-enable-mime
1410 (let ((obuf rmail-buffer)
1411 (ovbuf rmail-view-buffer))
1412 (set-buffer rmail-view-buffer)
1413 (quit-window)
1414 (replace-buffer-in-windows ovbuf)
1415 (replace-buffer-in-windows obuf)
1416 (bury-buffer obuf))
1417 (let ((obuf (current-buffer)))
1418 (quit-window)
1419 (replace-buffer-in-windows obuf))))
581d7e0b 1420
a16aef15
RS
1421(defun rmail-bury ()
1422 "Bury current Rmail buffer and its summary buffer."
1423 (interactive)
1424 ;; This let var was called rmail-buffer, but that interfered
1425 ;; with the buffer-local var used in summary buffers.
1426 (let ((buffer-to-bury (current-buffer)))
1427 (if (rmail-summary-exists)
1428 (let (window)
1429 (while (setq window (get-buffer-window rmail-summary-buffer))
df211784 1430 (quit-window nil window))
a16aef15 1431 (bury-buffer rmail-summary-buffer)))
df211784 1432 (quit-window)))
a16aef15
RS
1433
1434(defun rmail-duplicate-message ()
1435 "Create a duplicated copy of the current message.
1436The duplicate copy goes into the Rmail file just after the
1437original copy."
1438 (interactive)
1439 (widen)
1440 (let ((buffer-read-only nil)
1441 (number rmail-current-message)
1442 (string (buffer-substring (rmail-msgbeg rmail-current-message)
1443 (rmail-msgend rmail-current-message))))
1444 (goto-char (rmail-msgend rmail-current-message))
1445 (insert string)
1446 (rmail-forget-messages)
1447 (rmail-show-message number)
1448 (message "Message duplicated")))
1449
581d7e0b
RM
1450;;;###autoload
1451(defun rmail-input (filename)
bd1f0f84 1452 "Run Rmail on file FILENAME."
581d7e0b
RM
1453 (interactive "FRun rmail on RMAIL file: ")
1454 (rmail filename))
1455
563ab60d
RS
1456
1457;; This used to scan subdirectories recursively, but someone pointed out
1458;; that if the user wants that, person can put all the files in one dir.
1459;; And the recursive scan was slow. So I took it out.
1460;; rms, Sep 1996.
8b7ef584 1461(defun rmail-find-all-files (start)
563ab60d 1462 "Return list of file in dir START that match `rmail-secondary-file-regexp'."
8b7ef584 1463 (if (file-accessible-directory-p start)
1529a12f 1464 ;; Don't sort here.
563ab60d
RS
1465 (let* ((case-fold-search t)
1466 (files (directory-files start t rmail-secondary-file-regexp)))
1529a12f
RS
1467 ;; Sort here instead of in directory-files
1468 ;; because this list is usually much shorter.
563ab60d 1469 (sort files 'string<))))
8b7ef584
RS
1470
1471(defun rmail-list-to-menu (menu-name l action &optional full-name)
1472 (let ((menu (make-sparse-keymap menu-name)))
5701a0ce 1473 (mapc
8b7ef584 1474 (function (lambda (item)
bc8661ca
RS
1475 (let (command)
1476 (if (consp item)
1477 (progn
1478 (setq command
67f9d50e
FP
1479 (rmail-list-to-menu (car item) (cdr item)
1480 action
bc8661ca
RS
1481 (if full-name
1482 (concat full-name "/"
1483 (car item))
1484 (car item))))
1485 (setq name (car item)))
8b7ef584 1486 (progn
bc8661ca 1487 (setq name item)
67f9d50e 1488 (setq command
bc8661ca
RS
1489 (list 'lambda () '(interactive)
1490 (list action
67f9d50e 1491 (expand-file-name
bc8661ca
RS
1492 (if full-name
1493 (concat full-name "/" item)
1494 item)
1495 rmail-secondary-file-directory))))))
1496 (define-key menu (vector (intern name))
1497 (cons name command)))))
1529a12f 1498 (reverse l))
8b7ef584 1499 menu))
67f9d50e 1500
1529a12f
RS
1501;; This command is always "disabled" when it appears in a menu.
1502(put 'rmail-disable-menu 'menu-enable ''nil)
1503
8b7ef584
RS
1504(defun rmail-construct-io-menu ()
1505 (let ((files (rmail-find-all-files rmail-secondary-file-directory)))
1529a12f 1506 (if files
8b7ef584
RS
1507 (progn
1508 (define-key rmail-mode-map [menu-bar classify input-menu]
67f9d50e
FP
1509 (cons "Input Rmail File"
1510 (rmail-list-to-menu "Input Rmail File"
1529a12f 1511 files
8b7ef584
RS
1512 'rmail-input)))
1513 (define-key rmail-mode-map [menu-bar classify output-menu]
67f9d50e
FP
1514 (cons "Output Rmail File"
1515 (rmail-list-to-menu "Output Rmail File"
1529a12f
RS
1516 files
1517 'rmail-output-to-rmail-file))))
1518
1519 (define-key rmail-mode-map [menu-bar classify input-menu]
1520 '("Input Rmail File" . rmail-disable-menu))
1521 (define-key rmail-mode-map [menu-bar classify output-menu]
1522 '("Output Rmail File" . rmail-disable-menu)))))
90254bb0 1523
581d7e0b
RM
1524\f
1525;;;; *** Rmail input ***
1526
73e72da4
DN
1527(declare-function rmail-spam-filter "rmail-spam-filter" (msg))
1528(declare-function rmail-summary-goto-msg "rmailsum" (&optional n nowarn skip-rmail))
1529(declare-function rmail-summary-mark-undeleted "rmailsum" (n))
1530(declare-function rmail-summary-mark-deleted "rmailsum" (&optional n undel))
1531(declare-function rfc822-addresses "rfc822" (header-text))
1532(declare-function mail-abbrev-make-syntax-table "mailabbrev.el" ())
1533(declare-function mail-sendmail-delimit-header "sendmail" ())
1534(declare-function mail-header-end "sendmail" ())
1535
581d7e0b
RM
1536;; RLK feature not added in this version:
1537;; argument specifies inbox file or files in various ways.
1538
1539(defun rmail-get-new-mail (&optional file-name)
1540 "Move any new mail from this RMAIL file's inbox files.
1541The inbox files can be specified with the file's Mail: option. The
1542variable `rmail-primary-inbox-list' specifies the inboxes for your
3af9db89
RS
1543primary RMAIL file if it has no Mail: option. By default, this is
1544your /usr/spool/mail/$USER.
581d7e0b
RM
1545
1546You can also specify the file to get new mail from. In this case, the
1547file of new mail is not changed or deleted. Noninteractively, you can
1548pass the inbox file name as an argument. Interactively, a prefix
38e76742
RS
1549argument causes us to read a file name and use that file as the inbox.
1550
4144e5cb
RS
1551If the variable `rmail-preserve-inbox' is non-nil, new mail will
1552always be left in inbox files rather than deleted.
1553
bcddd52e
RS
1554This function runs `rmail-get-new-mail-hook' before saving the updated file.
1555It returns t if it got any new messages."
581d7e0b
RM
1556 (interactive
1557 (list (if current-prefix-arg
1558 (read-file-name "Get new mail from file: "))))
1d08d733 1559 (run-hooks 'rmail-before-get-new-mail-hook)
3f3495d3
RS
1560 ;; If the disk file has been changed from under us,
1561 ;; revert to it before we get new mail.
581d7e0b 1562 (or (verify-visited-file-modtime (current-buffer))
3f3495d3 1563 (find-file (buffer-file-name)))
6b59a5fc 1564 (set-buffer rmail-buffer)
581d7e0b
RM
1565 (rmail-maybe-set-message-counters)
1566 (widen)
1567 ;; Get rid of all undo records for this buffer.
1568 (or (eq buffer-undo-list t)
1569 (setq buffer-undo-list nil))
563ab60d 1570 (let ((all-files (if file-name (list file-name)
3ba3fa75 1571 rmail-inbox-list))
7d862e07 1572 (rmail-enable-multibyte (default-value 'enable-multibyte-characters))
3ba3fa75 1573 found)
563ab60d 1574 (unwind-protect
3ba3fa75 1575 (progn
9a10bd0d
KH
1576 (while all-files
1577 (let ((opoint (point))
1578 (new-messages 0)
608aa380 1579 (rsf-number-of-spam 0)
9a10bd0d
KH
1580 (delete-files ())
1581 ;; If buffer has not changed yet, and has not been saved yet,
1582 ;; don't replace the old backup file now.
1583 (make-backup-files (and make-backup-files (buffer-modified-p)))
1584 (buffer-read-only nil)
1585 ;; Don't make undo records for what we do in getting mail.
1586 (buffer-undo-list t)
1587 success
1588 ;; Files to insert this time around.
1589 files
1590 ;; Last names of those files.
1591 file-last-names)
1592 ;; Pull files off all-files onto files
1593 ;; as long as there is no name conflict.
1594 ;; A conflict happens when two inbox file names
1595 ;; have the same last component.
1596 (while (and all-files
1597 (not (member (file-name-nondirectory (car all-files))
1598 file-last-names)))
1599 (setq files (cons (car all-files) files)
1600 file-last-names
1601 (cons (file-name-nondirectory (car all-files)) files))
1602 (setq all-files (cdr all-files)))
1603 ;; Put them back in their original order.
1604 (setq files (nreverse files))
1605
1606 (goto-char (point-max))
1607 (skip-chars-backward " \t\n") ; just in case of brain damage
1608 (delete-region (point) (point-max)) ; caused by require-final-newline
1609 (save-excursion
1610 (save-restriction
1611 (narrow-to-region (point) (point))
1612 ;; Read in the contents of the inbox files,
1613 ;; renaming them as necessary,
1614 ;; and adding to the list of files to delete eventually.
1615 (if file-name
1616 (rmail-insert-inbox-text files nil)
1617 (setq delete-files (rmail-insert-inbox-text files t)))
1618 ;; Scan the new text and convert each message to babyl format.
1619 (goto-char (point-min))
1620 (unwind-protect
1621 (save-excursion
1622 (setq new-messages (rmail-convert-to-babyl-format)
1623 success t))
c317dd93
RS
1624 ;; Try to delete the garbage just inserted.
1625 (or success (delete-region (point-min) (point-max)))
9a10bd0d
KH
1626 ;; If we could not convert the file's inboxes,
1627 ;; rename the files we tried to read
1628 ;; so we won't over and over again.
1629 (if (and (not file-name) (not success))
1630 (let ((delfiles delete-files)
1631 (count 0))
1632 (while delfiles
1633 (while (file-exists-p (format "RMAILOSE.%d" count))
1634 (setq count (1+ count)))
1635 (rename-file (car delfiles)
1636 (format "RMAILOSE.%d" count))
1637 (setq delfiles (cdr delfiles))))))
1638 (or (zerop new-messages)
1639 (let (success)
1640 (widen)
1641 (search-backward "\n\^_" nil t)
1642 (narrow-to-region (point) (point-max))
1643 (goto-char (1+ (point-min)))
1644 (rmail-count-new-messages)
1645 (run-hooks 'rmail-get-new-mail-hook)
1646 (save-buffer)))
1647 ;; Delete the old files, now that babyl file is saved.
1648 (while delete-files
1649 (condition-case ()
1650 ;; First, try deleting.
1651 (condition-case ()
1652 (delete-file (car delete-files))
1653 (file-error
1654 ;; If we can't delete it, truncate it.
1655 (write-region (point) (point) (car delete-files))))
1656 (file-error nil))
1657 (setq delete-files (cdr delete-files)))))
1658 (if (= new-messages 0)
1659 (progn (goto-char opoint)
1660 (if (or file-name rmail-inbox-list)
1661 (message "(No new mail has arrived)")))
608aa380
KH
1662 ;; check new messages to see if any of them is spam:
1663 (if (and (featurep 'rmail-spam-filter)
1664 rmail-use-spam-filter)
1665 (let*
1666 ((old-messages (- rmail-total-messages new-messages))
1667 (rsf-scanned-message-number (1+ old-messages))
1668 ;; save deletion flags of old messages: vector starts
1669 ;; at zero (is one longer that no of messages),
1670 ;; therefore take 1+ old-messages
1671 (save-deleted
1672 (substring rmail-deleted-vector 0 (1+
1673 old-messages))))
1674 ;; set all messages to undeleted
1675 (setq rmail-deleted-vector
1676 (make-string (1+ rmail-total-messages) ?\ ))
1677 (while (<= rsf-scanned-message-number
1678 rmail-total-messages)
1679 (progn
1680 (if (not (rmail-spam-filter rsf-scanned-message-number))
1681 (progn (setq rsf-number-of-spam (1+ rsf-number-of-spam)))
1682 )
1683 (setq rsf-scanned-message-number (1+ rsf-scanned-message-number))
1684 ))
1685 (if (> rsf-number-of-spam 0)
1686 (progn
1687 (when (rmail-expunge-confirmed)
1688 (rmail-only-expunge t))
1689 ))
1690 (setq rmail-deleted-vector
1691 (concat
1692 save-deleted
1693 (make-string (- rmail-total-messages old-messages)
1694 ?\ )))
1695 ))
1696 (if (rmail-summary-exists)
9a10bd0d
KH
1697 (rmail-select-summary
1698 (rmail-update-summary)))
608aa380
KH
1699 (message "%d new message%s read%s"
1700 new-messages (if (= 1 new-messages) "" "s")
1701 ;; print out a message on number of spam messages found:
1702 (if (and (featurep 'rmail-spam-filter)
1703 rmail-use-spam-filter
1704 (> rsf-number-of-spam 0))
ee1d889f
RS
1705 (cond ((= 1 new-messages)
1706 ", and appears to be spam")
1707 ((= rsf-number-of-spam new-messages)
1708 ", and all appear to be spam")
1709 ((> rsf-number-of-spam 1)
1710 (format ", and %d appear to be spam"
1711 rsf-number-of-spam))
1712 (t
1713 ", and 1 appears to be spam"))
608aa380
KH
1714 ""))
1715 (if (and (featurep 'rmail-spam-filter)
1716 rmail-use-spam-filter
1717 (> rsf-number-of-spam 0))
f431a910
EZ
1718 (progn (if rsf-beep (beep t))
1719 (sleep-for rsf-sleep-after-message)))
608aa380 1720
9a10bd0d
KH
1721 ;; Move to the first new message
1722 ;; unless we have other unseen messages before it.
1723 (rmail-show-message (rmail-first-unseen-message))
1724 (run-hooks 'rmail-after-get-new-mail-hook)
1725 (setq found t))))
1726 found)
563ab60d 1727 ;; Don't leave the buffer screwed up if we get a disk-full error.
3ba3fa75 1728 (or found (rmail-show-message)))))
581d7e0b 1729
1086788e
EZ
1730(defun rmail-parse-url (file)
1731 "Parse the supplied URL. Return (list MAILBOX-NAME REMOTE PASSWORD GOT-PASSWORD)
1732WHERE MAILBOX-NAME is the name of the mailbox suitable as argument to the
1733actual version of `movemail', REMOTE is non-nil if MAILBOX-NAME refers to
1734a remote mailbox, PASSWORD is the password if it should be
1735supplied as a separate argument to `movemail' or nil otherwise, GOT-PASSWORD
1736is non-nil if the user has supplied the password interactively.
1737"
2f2e2373
EZ
1738 (cond
1739 ((string-match "^\\([^:]+\\)://\\(\\([^:@]+\\)\\(:\\([^@]+\\)\\)?@\\)?.*" file)
bf247b6e 1740 (let (got-password supplied-password
1086788e
EZ
1741 (proto (match-string 1 file))
1742 (user (match-string 3 file))
1743 (pass (match-string 5 file))
1744 (host (substring file (or (match-end 2)
1745 (+ 3 (match-end 1))))))
e84b4b86 1746
1086788e
EZ
1747 (if (not pass)
1748 (when rmail-remote-password-required
1749 (setq got-password (not (rmail-have-password)))
1750 (setq supplied-password (rmail-get-remote-password
1751 (string-equal proto "imap")))))
bf247b6e 1752
1086788e
EZ
1753 (if (rmail-movemail-variant-p 'emacs)
1754 (if (string-equal proto "pop")
1755 (list (concat "po:" user ":" host)
1756 t
1757 (or pass supplied-password)
1758 got-password)
1759 (error "Emacs movemail does not support %s protocol" proto))
1760 (list file
1761 (or (string-equal proto "pop") (string-equal proto "imap"))
1762 supplied-password
2f2e2373 1763 got-password))))
e84b4b86 1764
2f2e2373
EZ
1765 ((string-match "^po:\\([^:]+\\)\\(:\\(.*\\)\\)?" file)
1766 (let (got-password supplied-password
1767 (proto "pop")
1768 (user (match-string 1 file))
1769 (host (match-string 3 file)))
e84b4b86 1770
2f2e2373
EZ
1771 (when rmail-remote-password-required
1772 (setq got-password (not (rmail-have-password)))
1773 (setq supplied-password (rmail-get-remote-password nil)))
1774
1775 (list file "pop" supplied-password got-password)))
e84b4b86 1776
2f2e2373
EZ
1777 (t
1778 (list file nil nil nil))))
1086788e 1779
581d7e0b 1780(defun rmail-insert-inbox-text (files renamep)
563ab60d
RS
1781 ;; Detect a locked file now, so that we avoid moving mail
1782 ;; out of the real inbox file. (That could scare people.)
1783 (or (memq (file-locked-p buffer-file-name) '(nil t))
1784 (error "RMAIL file %s is locked"
1785 (file-name-nondirectory buffer-file-name)))
639540ec 1786 (let (file tofile delete-files movemail popmail got-password password)
581d7e0b 1787 (while files
1086788e 1788 ;; Handle remote mailbox names specially; don't expand as filenames
1f0402c3
AI
1789 ;; in case the userid contains a directory separator.
1790 (setq file (car files))
1086788e
EZ
1791 (let ((url-data (rmail-parse-url file)))
1792 (setq file (nth 0 url-data))
1793 (setq popmail (nth 1 url-data))
1794 (setq password (nth 2 url-data))
1795 (setq got-password (nth 3 url-data)))
1796
1f0402c3
AI
1797 (if popmail
1798 (setq renamep t)
1799 (setq file (file-truename
3a85a179 1800 (substitute-in-file-name (expand-file-name file)))))
1f0402c3 1801 (setq tofile (expand-file-name
ec3bba39
RS
1802 ;; Generate name to move to from inbox name,
1803 ;; in case of multiple inboxes that need moving.
1086788e 1804 (concat ".newmail-"
bf247b6e 1805 (file-name-nondirectory
1086788e
EZ
1806 (if (memq system-type '(windows-nt cygwin))
1807 ;; cannot have "po:" in file name
1808 (substring file 3)
1809 file)))
ec3bba39
RS
1810 ;; Use the directory of this rmail file
1811 ;; because it's a nuisance to use the homedir
1812 ;; if that is on a full disk and this rmail
1813 ;; file isn't.
1814 (file-name-directory
1815 (expand-file-name buffer-file-name))))
1816 ;; Always use movemail to rename the file,
1817 ;; since there can be mailboxes in various directories.
2f2e2373 1818 (if (not popmail)
581d7e0b 1819 (progn
581d7e0b
RM
1820 ;; On some systems, /usr/spool/mail/foo is a directory
1821 ;; and the actual inbox is /usr/spool/mail/foo/foo.
1822 (if (file-directory-p file)
d0eecffa 1823 (setq file (expand-file-name (user-login-name)
581d7e0b 1824 file)))))
8095bf23 1825 (cond (popmail
1086788e 1826 (message "Getting mail from the remote server ..."))
8095bf23
RS
1827 ((and (file-exists-p tofile)
1828 (/= 0 (nth 7 (file-attributes tofile))))
1829 (message "Getting mail from %s..." tofile))
1830 ((and (file-exists-p file)
1831 (/= 0 (nth 7 (file-attributes file))))
1832 (message "Getting mail from %s..." file)))
581d7e0b
RM
1833 ;; Set TOFILE if have not already done so, and
1834 ;; rename or copy the file FILE to TOFILE if and as appropriate.
1835 (cond ((not renamep)
1836 (setq tofile file))
bd1f0f84
RS
1837 ((or (file-exists-p tofile) (and (not popmail)
1838 (not (file-exists-p file))))
581d7e0b 1839 nil)
581d7e0b 1840 (t
1086788e
EZ
1841 (with-temp-buffer
1842 (let ((errors (current-buffer)))
1843 (buffer-disable-undo errors)
1844 (let ((args
1845 (append
11bdb42c 1846 (list rmail-movemail-program nil errors nil)
1086788e
EZ
1847 (if rmail-preserve-inbox
1848 (list "-p")
1849 nil)
1850 (if (rmail-movemail-variant-p 'mailutils)
1851 (append (list "--emacs") rmail-movemail-flags)
1852 rmail-movemail-flags)
1853 (list file tofile)
1854 (if password (list password) nil))))
1855 (apply 'call-process args))
1856 (if (not (buffer-modified-p errors))
1857 ;; No output => movemail won
1858 nil
1859 (set-buffer errors)
1860 (subst-char-in-region (point-min) (point-max)
1861 ?\n ?\ )
1862 (goto-char (point-max))
1863 (skip-chars-backward " \t")
1864 (delete-region (point) (point-max))
1865 (goto-char (point-min))
1866 (if (looking-at "movemail: ")
1867 (delete-region (point-min) (match-end 0)))
1868 (beep t)
1869 ;; If we just read the password, most likely it is
1870 ;; wrong. Otherwise, see if there is a specific
1871 ;; reason to think that the problem is a wrong passwd.
1872 (if (or got-password
1873 (re-search-forward rmail-remote-password-error
1874 nil t))
1875 (rmail-set-remote-password nil))
1876
1877 ;; If using Mailutils, remove initial error code
1878 ;; abbreviation
1879 (when (rmail-movemail-variant-p 'mailutils)
1880 (goto-char (point-min))
1881 (when (looking-at "[A-Z][A-Z0-9_]*:")
1882 (delete-region (point-min) (match-end 0))))
bf247b6e 1883
1086788e
EZ
1884 (message "movemail: %s"
1885 (buffer-substring (point-min)
1886 (point-max)))
bf247b6e 1887
1086788e
EZ
1888 (sit-for 3)
1889 nil)))))
bf247b6e 1890
581d7e0b
RM
1891 ;; At this point, TOFILE contains the name to read:
1892 ;; Either the alternate name (if we renamed)
1893 ;; or the actual inbox (if not renaming).
1894 (if (file-exists-p tofile)
d1e69dec
KH
1895 (let ((coding-system-for-read 'no-conversion)
1896 size)
581d7e0b 1897 (goto-char (point-max))
d1e69dec 1898 (setq size (nth 1 (insert-file-contents tofile)))
581d7e0b
RM
1899 (goto-char (point-max))
1900 (or (= (preceding-char) ?\n)
1901 (zerop size)
1902 (insert ?\n))
4144e5cb
RS
1903 (if (not (and rmail-preserve-inbox (string= file tofile)))
1904 (setq delete-files (cons tofile delete-files)))))
581d7e0b
RM
1905 (message "")
1906 (setq files (cdr files)))
1907 delete-files))
1908
aa2d5fe4
RS
1909;; Decode the region specified by FROM and TO by CODING.
1910;; If CODING is nil or an invalid coding system, decode by `undecided'.
1911(defun rmail-decode-region (from to coding)
1912 (if (or (not coding) (not (coding-system-p coding)))
1913 (setq coding 'undecided))
8a60950d
EZ
1914 ;; Use -dos decoding, to remove ^M characters left from base64 or
1915 ;; rogue qp-encoded text.
1916 (decode-coding-region from to
1917 (coding-system-change-eol-conversion coding 1))
1918 ;; Don't reveal the fact we used -dos decoding, as users generally
1919 ;; will not expect the RMAIL buffer to use DOS EOL format.
1920 (setq buffer-file-coding-system
1921 (setq last-coding-system-used
1922 (coding-system-change-eol-conversion coding 0))))
aa2d5fe4 1923
581d7e0b
RM
1924;; the rmail-break-forwarded-messages feature is not implemented
1925(defun rmail-convert-to-babyl-format ()
1926 (let ((count 0) start
b77ab9e8 1927 (case-fold-search nil)
ee1d889f 1928 (buffer-undo-list t)
b77ab9e8
RS
1929 (invalid-input-resync
1930 (function (lambda ()
1931 (message "Invalid Babyl format in inbox!")
563ab60d 1932 (sit-for 3)
b77ab9e8
RS
1933 ;; Try to get back in sync with a real message.
1934 (if (re-search-forward
95b597ce 1935 (concat rmail-mmdf-delim1 "\\|^From") nil t)
b77ab9e8
RS
1936 (beginning-of-line)
1937 (goto-char (point-max)))))))
581d7e0b
RM
1938 (goto-char (point-min))
1939 (save-restriction
1940 (while (not (eobp))
d1e69dec 1941 (setq start (point))
19e2f1bf 1942 (cond ((looking-at "BABYL OPTIONS:") ;Babyl header
b77ab9e8
RS
1943 (if (search-forward "\n\^_" nil t)
1944 ;; If we find the proper terminator, delete through there.
1945 (delete-region (point-min) (point))
1946 (funcall invalid-input-resync)
a553316b 1947 (delete-region (point-min) (point))))
581d7e0b
RM
1948 ;; Babyl format message
1949 ((looking-at "\^L")
1950 (or (search-forward "\n\^_" nil t)
b77ab9e8 1951 (funcall invalid-input-resync))
581d7e0b
RM
1952 (setq count (1+ count))
1953 ;; Make sure there is no extra white space after the ^_
1954 ;; at the end of the message.
1955 ;; Narrowing will make sure that whatever follows the junk
1956 ;; will be treated properly.
1957 (delete-region (point)
1958 (save-excursion
1959 (skip-chars-forward " \t\n")
1960 (point)))
19e2f1bf
MR
1961 ;; The following let* form was wrapped in a `save-excursion'
1962 ;; which in one case caused infinite looping, see:
1963 ;; http://lists.gnu.org/archive/html/emacs-devel/2008-01/msg00968.html
1964 ;; Removing that form leaves `point' at the end of the
1965 ;; region decoded by `rmail-decode-region' which should
1966 ;; be correct.
1967 (let* ((header-end
1968 (progn
608aa380
KH
1969 (save-excursion
1970 (goto-char start)
19e2f1bf
MR
1971 (forward-line 1)
1972 (if (looking-at "0")
1973 (forward-line 1)
1974 (forward-line 2))
1975 (save-restriction
1976 (narrow-to-region (point) (point-max))
1977 (rfc822-goto-eoh)
1978 (point)))))
1979 (case-fold-search t)
1980 (quoted-printable-header-field-end
608aa380 1981 (save-excursion
19e2f1bf
MR
1982 (goto-char start)
1983 (re-search-forward
1984 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
1985 header-end t)))
1986 (base64-header-field-end
608aa380 1987 (save-excursion
19e2f1bf
MR
1988 (goto-char start)
1989 ;; Don't try to decode non-text data.
1990 (and (re-search-forward
1991 "^content-type:\\(\n?[\t ]\\)\\(text\\|message\\)/"
1992 header-end t)
1993 (goto-char start)
1994 (re-search-forward
1995 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
1996 header-end t)))))
1997 (if quoted-printable-header-field-end
1998 (save-excursion
1999 (unless
2000 (mail-unquote-printable-region header-end (point) nil t t)
2001 (message "Malformed MIME quoted-printable message"))
2002 ;; Change "quoted-printable" to "8bit",
2003 ;; to reflect the decoding we just did.
2004 (goto-char quoted-printable-header-field-end)
2005 (delete-region (point) (search-backward ":"))
2006 (insert ": 8bit")))
2007 (if base64-header-field-end
2008 (save-excursion
2009 (when
2010 (condition-case nil
2011 (progn
2012 (base64-decode-region (1+ header-end)
2013 (- (point) 2))
2014 t)
2015 (error nil))
2016 ;; Change "base64" to "8bit", to reflect the
2017 ;; decoding we just did.
2018 (goto-char base64-header-field-end)
2019 (delete-region (point) (search-backward ":"))
2020 (insert ": 8bit"))))
2021 (setq last-coding-system-used nil)
2022 (or rmail-enable-mime
2023 (not rmail-enable-multibyte)
2024 (let ((mime-charset
2025 (if (and rmail-decode-mime-charset
2026 (save-excursion
2027 (goto-char start)
2028 (search-forward "\n\n" nil t)
2029 (let ((case-fold-search t))
2030 (re-search-backward
2031 rmail-mime-charset-pattern
2032 start t))))
2033 (intern (downcase (match-string 1))))))
2034 (rmail-decode-region start (point) mime-charset))))
578b6415
RS
2035 ;; Add an X-Coding-System: header if we don't have one.
2036 (save-excursion
2037 (goto-char start)
2038 (forward-line 1)
2039 (if (looking-at "0")
2040 (forward-line 1)
2041 (forward-line 2))
2042 (or (save-restriction
2043 (narrow-to-region (point) (point-max))
2044 (rfc822-goto-eoh)
2045 (goto-char (point-min))
2046 (re-search-forward "^X-Coding-System:" nil t))
2047 (insert "X-Coding-System: "
2048 (symbol-name last-coding-system-used)
2049 "\n")))
608aa380
KH
2050 (narrow-to-region (point) (point-max))
2051 (and (= 0 (% count 10))
2052 (message "Converting to Babyl format...%d" count)))
581d7e0b
RM
2053 ;;*** MMDF format
2054 ((let ((case-fold-search t))
95b597ce 2055 (looking-at rmail-mmdf-delim1))
581d7e0b
RM
2056 (let ((case-fold-search t))
2057 (replace-match "\^L\n0, unseen,,\n*** EOOH ***\n")
95b597ce 2058 (re-search-forward rmail-mmdf-delim2 nil t)
581d7e0b
RM
2059 (replace-match "\^_"))
2060 (save-excursion
2061 (save-restriction
2062 (narrow-to-region start (1- (point)))
2063 (goto-char (point-min))
19e2f1bf
MR
2064 (while (search-forward "\n\^_" nil t) ; single char "\^_"
2065 (replace-match "\n^_")))) ; 2 chars: "^" and "_"
578b6415 2066 (setq last-coding-system-used nil)
d1e69dec 2067 (or rmail-enable-mime
7d862e07 2068 (not rmail-enable-multibyte)
426591c3 2069 (decode-coding-region start (point) 'undecided))
578b6415
RS
2070 (save-excursion
2071 (goto-char start)
2072 (forward-line 3)
2073 (insert "X-Coding-System: "
2074 (symbol-name last-coding-system-used)
2075 "\n"))
581d7e0b 2076 (narrow-to-region (point) (point-max))
608aa380
KH
2077 (setq count (1+ count))
2078 (and (= 0 (% count 10))
2079 (message "Converting to Babyl format...%d" count)))
581d7e0b
RM
2080 ;;*** Mail format
2081 ((looking-at "^From ")
581d7e0b
RM
2082 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
2083 (rmail-nuke-pinhead-header)
bd1f0f84
RS
2084 ;; If this message has a Content-Length field,
2085 ;; skip to the end of the contents.
2086 (let* ((header-end (save-excursion
2087 (and (re-search-forward "\n\n" nil t)
a865e55b 2088 (1- (point)))))
bd1f0f84 2089 (case-fold-search t)
ae9f695f
KH
2090 (quoted-printable-header-field-end
2091 (save-excursion
ae9f695f 2092 (re-search-forward
4877ba13 2093 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
ae9f695f 2094 header-end t)))
608aa380 2095 (base64-header-field-end
db0821c8 2096 (and
5c43b3ca 2097 ;; Don't decode non-text data.
db0821c8
EZ
2098 (save-excursion
2099 (re-search-forward
5c43b3ca 2100 "^content-type:\\(\n?[\t ]\\)\\(text\\|message\\)/"
db0821c8 2101 header-end t))
db0821c8
EZ
2102 (save-excursion
2103 (re-search-forward
5c43b3ca 2104 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
db0821c8 2105 header-end t))))
bd1f0f84
RS
2106 (size
2107 ;; Get the numeric value from the Content-Length field.
2108 (save-excursion
2109 ;; Back up to end of prev line,
2110 ;; in case the Content-Length field comes first.
2111 (forward-char -1)
2112 (and (search-forward "\ncontent-length: "
2113 header-end t)
2114 (let ((beg (point))
2115 (eol (progn (end-of-line) (point))))
027a4b6b 2116 (string-to-number (buffer-substring beg eol)))))))
5458f7f1
KH
2117 (and size
2118 (if (and (natnump size)
2119 (<= (+ header-end size) (point-max))
2120 ;; Make sure this would put us at a position
2121 ;; that we could continue from.
2122 (save-excursion
2123 (goto-char (+ header-end size))
2124 (skip-chars-forward "\n")
2125 (or (eobp)
2126 (and (looking-at "BABYL OPTIONS:")
2127 (search-forward "\n\^_" nil t))
2128 (and (looking-at "\^L")
2129 (search-forward "\n\^_" nil t))
2130 (let ((case-fold-search t))
95b597ce 2131 (looking-at rmail-mmdf-delim1))
5458f7f1
KH
2132 (looking-at "From "))))
2133 (goto-char (+ header-end size))
2134 (message "Ignoring invalid Content-Length field")
ae9f695f 2135 (sit-for 1 0 t)))
a4e19cc2
RS
2136 (if (let ((case-fold-search nil))
2137 (re-search-forward
2138 (concat "^[\^_]?\\("
2139 rmail-unix-mail-delimiter
2140 "\\|"
2141 rmail-mmdf-delim1 "\\|"
2142 "^BABYL OPTIONS:\\|"
2143 "\^L\n[01],\\)") nil t))
ae9f695f
KH
2144 (goto-char (match-beginning 1))
2145 (goto-char (point-max)))
2146 (setq count (1+ count))
2147 (if quoted-printable-header-field-end
2148 (save-excursion
608aa380 2149 (unless
6b61353c 2150 (mail-unquote-printable-region header-end (point) nil t t)
608aa380 2151 (message "Malformed MIME quoted-printable message"))
ae9f695f
KH
2152 ;; Change "quoted-printable" to "8bit",
2153 ;; to reflect the decoding we just did.
2154 (goto-char quoted-printable-header-field-end)
4877ba13 2155 (delete-region (point) (search-backward ":"))
608aa380
KH
2156 (insert ": 8bit")))
2157 (if base64-header-field-end
2158 (save-excursion
2159 (when
2160 (condition-case nil
2161 (progn
2162 (base64-decode-region
2163 (1+ header-end)
2164 (save-excursion
2165 ;; Prevent base64-decode-region
2166 ;; from removing newline characters.
2167 (skip-chars-backward "\n\t ")
2168 (point)))
2169 t)
2170 (error nil))
608aa380
KH
2171 ;; Change "base64" to "8bit", to reflect the
2172 ;; decoding we just did.
2173 (goto-char base64-header-field-end)
2174 (delete-region (point) (search-backward ":"))
2175 (insert ": 8bit")))))
ae9f695f 2176
581d7e0b
RM
2177 (save-excursion
2178 (save-restriction
2179 (narrow-to-region start (point))
2180 (goto-char (point-min))
19e2f1bf
MR
2181 (while (search-forward "\n\^_" nil t) ; single char
2182 (replace-match "\n^_")))) ; 2 chars: "^" and "_"
6b61353c
KH
2183 ;; This is for malformed messages that don't end in newline.
2184 ;; There shouldn't be any, but some users say occasionally
2185 ;; there are some.
2186 (or (bolp) (newline))
581d7e0b 2187 (insert ?\^_)
578b6415 2188 (setq last-coding-system-used nil)
d1e69dec 2189 (or rmail-enable-mime
7d862e07 2190 (not rmail-enable-multibyte)
aa2d5fe4
RS
2191 (let ((mime-charset
2192 (if (and rmail-decode-mime-charset
2193 (save-excursion
2194 (goto-char start)
2195 (search-forward "\n\n" nil t)
2196 (let ((case-fold-search t))
2197 (re-search-backward
2198 rmail-mime-charset-pattern
2199 start t))))
3b96a16d 2200 (intern (downcase (match-string 1))))))
aa2d5fe4 2201 (rmail-decode-region start (point) mime-charset)))
578b6415
RS
2202 (save-excursion
2203 (goto-char start)
2204 (forward-line 3)
2205 (insert "X-Coding-System: "
2206 (symbol-name last-coding-system-used)
2207 "\n"))
608aa380
KH
2208 (narrow-to-region (point) (point-max))
2209 (and (= 0 (% count 10))
2210 (message "Converting to Babyl format...%d" count)))
581d7e0b 2211 ;;
8896f2df
RS
2212 ;; This kludge is because some versions of sendmail.el
2213 ;; insert an extra newline at the beginning that shouldn't
2214 ;; be there. sendmail.el has been fixed, but old versions
2215 ;; may still be in use. -- rms, 7 May 1993.
2216 ((eolp) (delete-char 1))
a553316b 2217 (t (error "Cannot convert to babyl format")))))
ee1d889f 2218 (setq buffer-undo-list nil)
581d7e0b
RM
2219 count))
2220
2221;; Delete the "From ..." line, creating various other headers with
2222;; information from it if they don't already exist. Now puts the
c42722e3
RS
2223;; original line into a mail-from: header line for debugging and for
2224;; use by the rmail-output function.
581d7e0b
RM
2225(defun rmail-nuke-pinhead-header ()
2226 (save-excursion
2227 (save-restriction
2228 (let ((start (point))
2229 (end (progn
2230 (condition-case ()
2231 (search-forward "\n\n")
2232 (error
2233 (goto-char (point-max))
2234 (insert "\n\n")))
2235 (point)))
2236 has-from has-date)
2237 (narrow-to-region start end)
2238 (let ((case-fold-search t))
2239 (goto-char start)
2240 (setq has-from (search-forward "\nFrom:" nil t))
2241 (goto-char start)
2242 (setq has-date (and (search-forward "\nDate:" nil t) (point)))
2243 (goto-char start))
2244 (let ((case-fold-search nil))
46947372 2245 (if (re-search-forward (concat "^" rmail-unix-mail-delimiter) nil t)
581d7e0b
RM
2246 (replace-match
2247 (concat
2248 "Mail-from: \\&"
2249 ;; Keep and reformat the date if we don't
2250 ;; have a Date: field.
2251 (if has-date
2252 ""
b7cceaf1 2253 (concat
74d6e31c 2254 "Date: \\2, \\4 \\3 \\9 \\5 "
67f9d50e 2255
b7cceaf1
JB
2256 ;; The timezone could be matched by group 7 or group 10.
2257 ;; If neither of them matched, assume EST, since only
2258 ;; Easterners would be so sloppy.
2259 ;; It's a shame the substitution can't use "\\10".
2260 (cond
2261 ((/= (match-beginning 7) (match-end 7)) "\\7")
2262 ((/= (match-beginning 10) (match-end 10))
2263 (buffer-substring (match-beginning 10)
2264 (match-end 10)))
2265 (t "EST"))
2266 "\n"))
581d7e0b
RM
2267 ;; Keep and reformat the sender if we don't
2268 ;; have a From: field.
2269 (if has-from
2270 ""
15a36ac5
RS
2271 "From: \\1\n"))
2272 t)))))))
581d7e0b
RM
2273\f
2274;;;; *** Rmail Message Formatting and Header Manipulation ***
2275
2276(defun rmail-reformat-message (beg end)
2277 (goto-char beg)
2278 (forward-line 1)
2279 (if (/= (following-char) ?0)
55535639 2280 (error "Bad format in RMAIL file"))
ed104a87 2281 (let ((inhibit-read-only t)
581d7e0b
RM
2282 (delta (- (buffer-size) end)))
2283 (delete-char 1)
2284 (insert ?1)
2285 (forward-line 1)
15a36ac5 2286 (let ((case-fold-search t))
c42722e3
RS
2287 (while (looking-at "Summary-line:\\|Mail-From:")
2288 (forward-line 1)))
581d7e0b
RM
2289 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*\n")
2290 (delete-region (point)
2291 (progn (forward-line 1) (point))))
2292 (let ((str (buffer-substring (point)
2293 (save-excursion (search-forward "\n\n" end 'move)
2294 (point)))))
2295 (insert str "*** EOOH ***\n")
2296 (narrow-to-region (point) (- (buffer-size) delta)))
2297 (goto-char (point-min))
423c0dfa 2298 (if rmail-message-filter (funcall rmail-message-filter))
8095bf23
RS
2299 (if (or rmail-displayed-headers rmail-ignored-headers)
2300 (rmail-clear-headers))))
581d7e0b 2301
3db0cdac 2302(defun rmail-clear-headers (&optional ignored-headers)
8095bf23
RS
2303 "Delete all header fields that Rmail should not show.
2304If the optional argument IGNORED-HEADERS is non-nil,
2305delete all header fields whose names match that regexp.
2306Otherwise, if `rmail-displayed-headers' is non-nil,
2307delete all header fields *except* those whose names match that regexp.
5b9b5b89
RS
2308Otherwise, delete all header fields whose names match `rmail-ignored-headers'
2309unless they also match `rmail-nonignored-headers'."
f92f70bc
RS
2310 (when (search-forward "\n\n" nil t)
2311 (forward-char -1)
2312 (let ((case-fold-search t)
2313 (buffer-read-only nil))
2314 (if (and rmail-displayed-headers (null ignored-headers))
d155ff18
RS
2315 (save-restriction
2316 (narrow-to-region (point-min) (point))
2317 (let (lim next)
2318 (goto-char (point-min))
2319 (while (and (not (eobp))
2320 (save-excursion
2321 (if (re-search-forward "\n[^ \t]" nil t)
2322 (setq lim (match-beginning 0)
2323 next (1+ lim))
2324 (setq lim nil next (point-max)))))
2325 (if (save-excursion
2326 (re-search-forward rmail-displayed-headers lim t))
2327 (goto-char next)
2328 (delete-region (point) next))))
2329 (goto-char (point-min)))
f92f70bc
RS
2330 (or ignored-headers (setq ignored-headers rmail-ignored-headers))
2331 (save-restriction
2332 (narrow-to-region (point-min) (point))
5b9b5b89 2333 (goto-char (point-min))
bdbeb193 2334 (while (and ignored-headers
5b9b5b89 2335 (re-search-forward ignored-headers nil t))
f92f70bc 2336 (beginning-of-line)
be16d955
BG
2337 (if (and rmail-nonignored-headers
2338 (looking-at rmail-nonignored-headers))
5b9b5b89
RS
2339 (forward-line 1)
2340 (delete-region (point)
2341 (save-excursion
2342 (if (re-search-forward "\n[^ \t]" nil t)
2343 (1- (point))
2344 (point-max)))))))))))
581d7e0b 2345
dbf71ee9 2346(defun rmail-msg-is-pruned ()
581d7e0b 2347 (rmail-maybe-set-message-counters)
dbf71ee9 2348 (save-restriction
903e09b7 2349 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
dbf71ee9 2350 (save-excursion
903e09b7
RS
2351 (goto-char (point-min))
2352 (forward-line 1)
dbf71ee9
RS
2353 (= (following-char) ?1))))
2354
157521c5 2355(defun rmail-msg-restore-non-pruned-header ()
ed104a87
RS
2356 (let ((old-point (point))
2357 new-point
2358 new-start
2359 (inhibit-read-only t))
2360 (save-excursion
2361 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
157521c5
GM
2362 (goto-char (point-min))
2363 (forward-line 1)
2364 ;; Change 1 to 0.
2365 (delete-char 1)
2366 (insert ?0)
2367 ;; Insert new EOOH line at the proper place.
2368 (forward-line 1)
2369 (let ((case-fold-search t))
2370 (while (looking-at "Summary-Line:\\|Mail-From:")
2371 (forward-line 1)))
2372 (insert "*** EOOH ***\n")
2373 (setq new-start (point))
2374 ;; Delete the old reformatted header.
2375 (forward-char -1)
2376 (search-forward "\n*** EOOH ***\n")
2377 (forward-line -1)
2378 (let ((start (point)))
2379 (search-forward "\n\n")
ed104a87
RS
2380 (if (and (<= start old-point)
2381 (<= old-point (point)))
2382 (setq new-point new-start))
157521c5
GM
2383 (delete-region start (point)))
2384 ;; Narrow to after the new EOOH line.
ed104a87
RS
2385 (narrow-to-region new-start (point-max)))
2386 (if new-point
2387 (goto-char new-point))))
157521c5
GM
2388
2389(defun rmail-msg-prune-header ()
ed104a87
RS
2390 (let ((new-point
2391 (= (point) (point-min))))
2392 (save-excursion
2393 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2394 (rmail-reformat-message (point-min) (point-max)))
2395 (if new-point
2396 (goto-char (point-min)))))
157521c5 2397
dbf71ee9
RS
2398(defun rmail-toggle-header (&optional arg)
2399 "Show original message header if pruned header currently shown, or vice versa.
2400With argument ARG, show the message header pruned if ARG is greater than zero;
2401otherwise, show it in full."
2402 (interactive "P")
157521c5
GM
2403 (let* ((pruned (with-current-buffer rmail-buffer
2404 (rmail-msg-is-pruned)))
6a91b84a
KH
2405 (prune (if arg
2406 (> (prefix-numeric-value arg) 0)
2407 (not pruned))))
2408 (if (eq pruned prune)
2409 t
157521c5 2410 (set-buffer rmail-buffer)
6a91b84a 2411 (rmail-maybe-set-message-counters)
157521c5
GM
2412 (if rmail-enable-mime
2413 (let ((buffer-read-only nil))
2414 (if pruned
2415 (rmail-msg-restore-non-pruned-header)
2416 (rmail-msg-prune-header))
2417 (funcall rmail-show-mime-function))
2418 (let* ((buffer-read-only nil)
2419 (window (get-buffer-window (current-buffer)))
2420 (at-point-min (= (point) (point-min)))
2421 (all-headers-visible (= (window-start window) (point-min)))
2422 (on-header
2423 (save-excursion
2424 (and (not (search-backward "\n\n" nil t))
2425 (progn
2426 (end-of-line)
2427 (re-search-backward "^[-A-Za-z0-9]+:" nil t))
2428 (match-string 0))))
2429 (old-screen-line
2430 (rmail-count-screen-lines (window-start window) (point))))
f4b5ab4c 2431 (if pruned
157521c5
GM
2432 (rmail-msg-restore-non-pruned-header)
2433 (rmail-msg-prune-header))
2434 (cond (at-point-min
2435 (goto-char (point-min)))
2436 (on-header
2437 (goto-char (point-min))
2438 (search-forward "\n\n")
2439 (or (re-search-backward
2440 (concat "^" (regexp-quote on-header)) nil t)
2441 (goto-char (point-min))))
2442 (t
2443 (save-selected-window
2444 (select-window window)
2445 (recenter old-screen-line)
2446 (if (and all-headers-visible
2447 (not (= (window-start) (point-min))))
9d43f6f1 2448 (recenter (- (window-height) 2))))))))
6a91b84a
KH
2449 (rmail-highlight-headers))))
2450
ed104a87
RS
2451(defun rmail-narrow-to-non-pruned-header ()
2452 "Narrow to the whole (original) header of the current message."
2453 (let (start end)
2454 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2455 (goto-char (point-min))
2456 (forward-line 1)
2457 (if (= (following-char) ?1)
2458 (progn
2459 (forward-line 1)
2460 (setq start (point))
2461 (search-forward "*** EOOH ***\n")
2462 (setq end (match-beginning 0)))
2463 (forward-line 2)
2464 (setq start (point))
2465 (search-forward "\n\n")
2466 (setq end (1- (point))))
2467 (narrow-to-region start end)
2468 (goto-char start)))
2469
6a91b84a
KH
2470;; Lifted from repos-count-screen-lines.
2471;; Return number of screen lines between START and END.
2472(defun rmail-count-screen-lines (start end)
72f69d0f 2473 (save-excursion
6a91b84a
KH
2474 (save-restriction
2475 (narrow-to-region start end)
2476 (goto-char (point-min))
2477 (vertical-motion (- (point-max) (point-min))))))
581d7e0b
RM
2478\f
2479;;;; *** Rmail Attributes and Keywords ***
2480
2481;; Make a string describing current message's attributes and keywords
2482;; and set it up as the name of a minor mode
2483;; so it will appear in the mode line.
2484(defun rmail-display-labels ()
2485 (let ((blurb "") (beg (point-min-marker)) (end (point-max-marker)))
2486 (save-excursion
2487 (unwind-protect
2488 (progn
2489 (widen)
2490 (goto-char (rmail-msgbeg rmail-current-message))
2491 (forward-line 1)
2492 (if (looking-at "[01],")
2493 (progn
2494 (narrow-to-region (point) (progn (end-of-line) (point)))
2495 ;; Truly valid BABYL format requires a space before each
2496 ;; attribute or keyword name. Put them in if missing.
2497 (let (buffer-read-only)
2498 (goto-char (point-min))
2499 (while (search-forward "," nil t)
2500 (or (looking-at "[ ,]") (eobp)
2501 (insert " "))))
2502 (goto-char (point-max))
2503 (if (search-backward ",," nil 'move)
2504 (progn
2505 (if (> (point) (1+ (point-min)))
2506 (setq blurb (buffer-substring (+ 1 (point-min)) (point))))
2507 (if (> (- (point-max) (point)) 2)
2508 (setq blurb
2509 (concat blurb
2510 ";"
2511 (buffer-substring (+ (point) 3)
2512 (1- (point-max)))))))))))
2513 ;; Note: we don't use save-restriction because that does not work right
2514 ;; if changes are made outside the saved restriction
2515 ;; before that restriction is restored.
2516 (narrow-to-region beg end)
2517 (set-marker beg nil)
2518 (set-marker end nil)))
2519 (while (string-match " +," blurb)
2520 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
2521 (substring blurb (match-end 0)))))
2522 (while (string-match ", +" blurb)
2523 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
2524 (substring blurb (match-end 0)))))
2525 (setq mode-line-process
8095bf23 2526 (format " %d/%d%s"
6b59a5fc
GM
2527 rmail-current-message rmail-total-messages blurb))
2528 ;; If rmail-enable-mime is non-nil, we may have to update
2529 ;; `mode-line-process' of rmail-view-buffer too.
2530 (if (and rmail-enable-mime
2531 (not (eq (current-buffer) rmail-view-buffer))
2532 (buffer-live-p rmail-view-buffer))
2533 (let ((mlp mode-line-process))
2534 (with-current-buffer rmail-view-buffer
2535 (setq mode-line-process mlp))))))
581d7e0b
RM
2536
2537;; Turn an attribute of a message on or off according to STATE.
2538;; ATTR is the name of the attribute, as a string.
2539;; MSGNUM is message number to change; nil means current message.
2540(defun rmail-set-attribute (attr state &optional msgnum)
6b59a5fc 2541 (set-buffer rmail-buffer)
581d7e0b
RM
2542 (let ((omax (point-max-marker))
2543 (omin (point-min-marker))
2544 (buffer-read-only nil))
2545 (or msgnum (setq msgnum rmail-current-message))
708bfd4f
RS
2546 (if (> msgnum 0)
2547 (unwind-protect
2548 (save-excursion
2549 (widen)
2550 (goto-char (+ 3 (rmail-msgbeg msgnum)))
2551 (let ((curstate
2552 (not
2553 (null (search-backward (concat ", " attr ",")
2554 (prog1 (point) (end-of-line)) t)))))
2555 (or (eq curstate (not (not state)))
2556 (if curstate
2557 (delete-region (point) (1- (match-end 0)))
2558 (beginning-of-line)
2559 (forward-char 2)
2560 (insert " " attr ","))))
2561 (if (string= attr "deleted")
2562 (rmail-set-message-deleted-p msgnum state)))
2563 ;; Note: we don't use save-restriction because that does not work right
2564 ;; if changes are made outside the saved restriction
2565 ;; before that restriction is restored.
2566 (narrow-to-region omin omax)
2567 (set-marker omin nil)
2568 (set-marker omax nil)
2569 (if (= msgnum rmail-current-message)
2570 (rmail-display-labels))))))
581d7e0b
RM
2571
2572;; Return t if the attributes/keywords line of msg number MSG
2573;; contains a match for the regexp LABELS.
2574(defun rmail-message-labels-p (msg labels)
2575 (save-excursion
2576 (save-restriction
2577 (widen)
2578 (goto-char (rmail-msgbeg msg))
2579 (forward-char 3)
2580 (re-search-backward labels (prog1 (point) (end-of-line)) t))))
2581\f
2582;;;; *** Rmail Message Selection And Support ***
2583
2584(defun rmail-msgend (n)
2585 (marker-position (aref rmail-message-vector (1+ n))))
2586
2587(defun rmail-msgbeg (n)
2588 (marker-position (aref rmail-message-vector n)))
2589
2590(defun rmail-widen-to-current-msgbeg (function)
2591 "Call FUNCTION with point at start of internal data of current message.
2592Assumes that bounds were previously narrowed to display the message in Rmail.
2593The bounds are widened enough to move point where desired, then narrowed
2594again afterward.
2595
2596FUNCTION may not change the visible text of the message, but it may
2597change the invisible header text."
2598 (save-excursion
07ae0d66
GM
2599 (unwind-protect
2600 (progn
2601 (narrow-to-region (rmail-msgbeg rmail-current-message)
2602 (point-max))
2603 (goto-char (point-min))
2604 (funcall function))
581d7e0b
RM
2605 ;; Note: we don't use save-restriction because that does not work right
2606 ;; if changes are made outside the saved restriction
2607 ;; before that restriction is restored.
07ae0d66
GM
2608 (narrow-to-region (rmail-msgbeg rmail-current-message)
2609 (rmail-msgend rmail-current-message)))))
581d7e0b
RM
2610
2611(defun rmail-forget-messages ()
2612 (unwind-protect
2613 (if (vectorp rmail-message-vector)
2614 (let* ((i 0)
2615 (v rmail-message-vector)
2616 (n (length v)))
2617 (while (< i n)
2618 (move-marker (aref v i) nil)
2619 (setq i (1+ i)))))
2620 (setq rmail-message-vector nil)
0985b412 2621 (setq rmail-msgref-vector nil)
581d7e0b
RM
2622 (setq rmail-deleted-vector nil)))
2623
2624(defun rmail-maybe-set-message-counters ()
2625 (if (not (and rmail-deleted-vector
2626 rmail-message-vector
2627 rmail-current-message
2628 rmail-total-messages))
2629 (rmail-set-message-counters)))
2630
2631(defun rmail-count-new-messages (&optional nomsg)
2632 (let* ((case-fold-search nil)
2633 (total-messages 0)
2634 (messages-head nil)
2635 (deleted-head nil))
2636 (or nomsg (message "Counting new messages..."))
2637 (goto-char (point-max))
2638 ;; Put at the end of messages-head
2639 ;; the entry for message N+1, which marks
2640 ;; the end of message N. (N = number of messages).
2641 (search-backward "\n\^_")
2642 (forward-char 1)
2643 (setq messages-head (list (point-marker)))
2644 (rmail-set-message-counters-counter (point-min))
2645 (setq rmail-current-message (1+ rmail-total-messages))
2646 (setq rmail-total-messages
2647 (+ rmail-total-messages total-messages))
2648 (setq rmail-message-vector
2649 (vconcat rmail-message-vector (cdr messages-head)))
2650 (aset rmail-message-vector
2651 rmail-current-message (car messages-head))
2652 (setq rmail-deleted-vector
2653 (concat rmail-deleted-vector deleted-head))
2654 (setq rmail-summary-vector
2655 (vconcat rmail-summary-vector (make-vector total-messages nil)))
0985b412
RS
2656 (setq rmail-msgref-vector
2657 (vconcat rmail-msgref-vector (make-vector total-messages nil)))
2658 ;; Fill in the new elements of rmail-msgref-vector.
79d8d8d5 2659 (let ((i (1+ (- rmail-total-messages total-messages))))
0985b412
RS
2660 (while (<= i rmail-total-messages)
2661 (aset rmail-msgref-vector i (list i))
2662 (setq i (1+ i))))
581d7e0b
RM
2663 (goto-char (point-min))
2664 (or nomsg (message "Counting new messages...done (%d)" total-messages))))
2665
2666(defun rmail-set-message-counters ()
2667 (rmail-forget-messages)
2668 (save-excursion
2669 (save-restriction
2670 (widen)
2671 (let* ((point-save (point))
2672 (total-messages 0)
2673 (messages-after-point)
2674 (case-fold-search nil)
2675 (messages-head nil)
2676 (deleted-head nil))
2677 (message "Counting messages...")
2678 (goto-char (point-max))
2679 ;; Put at the end of messages-head
2680 ;; the entry for message N+1, which marks
2681 ;; the end of message N. (N = number of messages).
cf805d5d
RS
2682 (search-backward "\n\^_" nil t)
2683 (if (/= (point) (point-max)) (forward-char 1))
581d7e0b
RM
2684 (setq messages-head (list (point-marker)))
2685 (rmail-set-message-counters-counter (min (point) point-save))
2686 (setq messages-after-point total-messages)
2687 (rmail-set-message-counters-counter)
2688 (setq rmail-total-messages total-messages)
2689 (setq rmail-current-message
2690 (min total-messages
2691 (max 1 (- total-messages messages-after-point))))
2692 (setq rmail-message-vector
2693 (apply 'vector (cons (point-min-marker) messages-head))
2c0b1898 2694 rmail-deleted-vector (concat "0" deleted-head)
0985b412
RS
2695 rmail-summary-vector (make-vector rmail-total-messages nil)
2696 rmail-msgref-vector (make-vector (1+ rmail-total-messages) nil))
2697 (let ((i 0))
2698 (while (<= i rmail-total-messages)
2699 (aset rmail-msgref-vector i (list i))
2700 (setq i (1+ i))))
581d7e0b 2701 (message "Counting messages...done")))))
67f9d50e 2702
581d7e0b 2703(defun rmail-set-message-counters-counter (&optional stop)
582e5ff7
AI
2704 (let ((start (point))
2705 next)
2706 (while (search-backward "\n\^_\^L" stop t)
2707 ;; Detect messages that have been added with DOS line endings and
2708 ;; convert the line endings for such messages.
2709 (setq next (point))
2710 (if (looking-at "\n\^_\^L\r\n")
2711 (let ((buffer-read-only nil)
2712 (buffer-undo t))
2713 (message "Counting messages...(converting line endings)")
2714 (save-excursion
2715 (goto-char start)
2716 (while (search-backward "\r\n" next t)
2717 (delete-char 1)))))
2718 (setq start next)
2719 (forward-char 1)
2720 (setq messages-head (cons (point-marker) messages-head))
2721 (save-excursion
2722 (setq deleted-head
2723 (cons (if (search-backward ", deleted,"
2724 (prog1 (point)
2725 (forward-line 2))
2726 t)
2727 ?D ?\ )
2728 deleted-head)))
2729 (if (zerop (% (setq total-messages (1+ total-messages)) 20))
2730 (message "Counting messages...%d" total-messages)))))
581d7e0b
RM
2731
2732(defun rmail-beginning-of-message ()
2733 "Show current message starting from the beginning."
2734 (interactive)
3deac4a1
EZ
2735 (let ((rmail-show-message-hook
2736 (list (function (lambda ()
2737 (goto-char (point-min)))))))
2738 (rmail-show-message rmail-current-message)))
2739
2740(defun rmail-end-of-message ()
2741 "Show bottom of current message."
2742 (interactive)
2743 (let ((rmail-show-message-hook
2744 (list (function (lambda ()
2745 (goto-char (point-max))
2746 (recenter (1- (window-height))))))))
2747 (rmail-show-message rmail-current-message)))
581d7e0b 2748
8510cb94
RS
2749(defun rmail-unknown-mail-followup-to ()
2750 "Handle a \"Mail-Followup-To\" header field with an unknown mailing list.
2751Ask the user whether to add that list name to `mail-mailing-lists'."
2752 (save-restriction
2753 (rmail-narrow-to-non-pruned-header)
2754 (let ((mail-followup-to (mail-fetch-field "mail-followup-to" nil t)))
2755 (when mail-followup-to
2756 (let ((addresses
e84b4b86 2757 (split-string
8510cb94
RS
2758 (mail-strip-quoted-names mail-followup-to)
2759 ",[[:space:]]+" t)))
2760 (dolist (addr addresses)
2761 (when (and (not (member addr mail-mailing-lists))
2762 (not
2763 ;; taken from rmailsum.el
2764 (string-match
2765 (or rmail-user-mail-address-regexp
2766 (concat "^\\("
2767 (regexp-quote (user-login-name))
2768 "\\($\\|@\\)\\|"
2769 (regexp-quote
2770 (or user-mail-address
2771 (concat (user-login-name) "@"
2772 (or mail-host-address
2773 (system-name)))))
2774 "\\>\\)"))
2775 addr))
2776 (y-or-n-p
2777 (format "Add `%s' to `mail-mailing-lists'? "
2778 addr)))
2779 (customize-save-variable 'mail-mailing-lists
2780 (cons addr mail-mailing-lists)))))))))
2781
bdf03600 2782(defun rmail-show-message (&optional n no-summary)
bd1f0f84
RS
2783 "Show message number N (prefix argument), counting from start of file.
2784If summary buffer is currently displayed, update current message there also."
581d7e0b 2785 (interactive "p")
d1e69dec
KH
2786 (or (eq major-mode 'rmail-mode)
2787 (switch-to-buffer rmail-buffer))
581d7e0b
RM
2788 (rmail-maybe-set-message-counters)
2789 (widen)
2790 (if (zerop rmail-total-messages)
2791 (progn (narrow-to-region (point-min) (1- (point-max)))
2792 (goto-char (point-min))
2793 (setq mode-line-process nil))
578b6415 2794 (let (blurb coding-system)
581d7e0b
RM
2795 (if (not n)
2796 (setq n rmail-current-message)
2797 (cond ((<= n 0)
2798 (setq n 1
2799 rmail-current-message 1
2800 blurb "No previous message"))
2801 ((> n rmail-total-messages)
2802 (setq n rmail-total-messages
2803 rmail-current-message rmail-total-messages
2804 blurb "No following message"))
2805 (t
2806 (setq rmail-current-message n))))
563ab60d 2807 (let ((beg (rmail-msgbeg n)))
581d7e0b
RM
2808 (goto-char beg)
2809 (forward-line 1)
578b6415
RS
2810 (save-excursion
2811 (let ((end (rmail-msgend n)))
2812 (save-restriction
2813 (if (prog1 (= (following-char) ?0)
2814 (forward-line 2)
2751e20b
RS
2815 ;; If there's a Summary-line in the (otherwise empty)
2816 ;; header, we didn't yet get past the EOOH line.
2817 (if (looking-at "^\\*\\*\\* EOOH \\*\\*\\*\n")
2818 (forward-line 1))
578b6415
RS
2819 (narrow-to-region (point) end))
2820 (rfc822-goto-eoh)
2821 (search-forward "\n*** EOOH ***\n" end t))
2822 (narrow-to-region beg (point))
2823 (goto-char (point-min))
2824 (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t)
2825 (let ((coding-system (intern (match-string 1))))
de971ad1
RS
2826 (condition-case nil
2827 (progn
2828 (check-coding-system coding-system)
2829 (setq buffer-file-coding-system coding-system))
3e0b7b44 2830 (error
de971ad1 2831 (setq buffer-file-coding-system nil))))
578b6415 2832 (setq buffer-file-coding-system nil)))))
563ab60d
RS
2833 ;; Clear the "unseen" attribute when we show a message.
2834 (rmail-set-attribute "unseen" nil)
563ab60d 2835 (let ((end (rmail-msgend n)))
578b6415 2836 ;; Reformat the header, or else find the reformatted header.
563ab60d 2837 (if (= (following-char) ?0)
581d7e0b 2838 (rmail-reformat-message beg end)
563ab60d
RS
2839 (search-forward "\n*** EOOH ***\n" end t)
2840 (narrow-to-region (point) end)))
581d7e0b 2841 (goto-char (point-min))
6f19114e
GM
2842 (walk-windows
2843 (function (lambda (window)
2844 (if (eq (window-buffer window) (current-buffer))
2845 (set-window-point window (point)))))
2846 nil t)
581d7e0b 2847 (rmail-display-labels)
d1e69dec
KH
2848 (if (eq rmail-enable-mime t)
2849 (funcall rmail-show-mime-function)
8510cb94
RS
2850 (setq rmail-view-buffer rmail-buffer))
2851 (when mail-mailing-lists
2852 (rmail-unknown-mail-followup-to))
99887e16 2853 (rmail-highlight-headers)
d43277ab 2854 (if transient-mark-mode (deactivate-mark))
581d7e0b 2855 (run-hooks 'rmail-show-message-hook)
bd1f0f84
RS
2856 ;; If there is a summary buffer, try to move to this message
2857 ;; in that buffer. But don't complain if this message
2858 ;; is not mentioned in the summary.
bdf03600
RS
2859 ;; Don't do this at all if we were called on behalf
2860 ;; of cursor motion in the summary buffer.
2861 (and (rmail-summary-exists) (not no-summary)
2862 (let ((curr-msg rmail-current-message))
2863 (rmail-select-summary
2864 (rmail-summary-goto-msg curr-msg t t))))
6b59a5fc
GM
2865 (with-current-buffer rmail-buffer
2866 (rmail-auto-file))
581d7e0b
RM
2867 (if blurb
2868 (message blurb))))))
2869
d3e1986f 2870(defun rmail-redecode-body (coding &optional raw)
fd4976b8
EZ
2871 "Decode the body of the current message using coding system CODING.
2872This is useful with mail messages that have malformed or missing
2873charset= headers.
2874
2875This function assumes that the current message is already decoded
2876and displayed in the RMAIL buffer, but the coding system used to
2877decode it was incorrect. It then encodes the message back to its
4685e6b7 2878original form, and decodes it again, using the coding system CODING.
fd4976b8 2879
d3e1986f
EZ
2880Optional argument RAW, if non-nil, means don't encode the message
2881before decoding it with the new CODING. This is useful if the current
2882message text was produced by some function which invokes `insert',
2883since `insert' leaves unibyte character codes 128 through 255 unconverted
2884to multibyte. One example of such a situation is when the text was
2885produced by `base64-decode-region'.
2886
2887Interactively, invoke the function with a prefix argument to set RAW
2888non-nil.
2889
fd4976b8
EZ
2890Note that if Emacs erroneously auto-detected one of the iso-2022
2891encodings in the message, this function might fail because the escape
2892sequences that switch between character sets and also single-shift and
2893locking-shift codes are impossible to recover. This function is meant
2894to be used to fix messages encoded with 8-bit encodings, such as
2895iso-8859, koi8-r, etc."
2896 (interactive "zCoding system for re-decoding this message: ")
2897 (when (not rmail-enable-mime)
2898 (or (eq major-mode 'rmail-mode)
2899 (switch-to-buffer rmail-buffer))
2900 (save-excursion
d3e1986f
EZ
2901 (let ((pruned (rmail-msg-is-pruned))
2902 (raw (or raw current-prefix-arg)))
d54f26b1
EZ
2903 (unwind-protect
2904 (let ((msgbeg (rmail-msgbeg rmail-current-message))
2905 (msgend (rmail-msgend rmail-current-message))
2906 x-coding-header)
2907 ;; We need the message headers pruned (we later restore
2908 ;; the pruned stat to what it was, see the end of
2909 ;; unwind-protect form).
2910 (or pruned
2911 (rmail-toggle-header 1))
2912 (narrow-to-region msgbeg msgend)
2913 (goto-char (point-min))
2914 (when (search-forward "\n*** EOOH ***\n" (point-max) t)
2915 (narrow-to-region msgbeg (point)))
2916 (goto-char (point-min))
2917 (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t)
2918 (let ((old-coding (intern (match-string 1)))
2919 (buffer-read-only nil))
2920 (check-coding-system old-coding)
2921 ;; Make sure the new coding system uses the same EOL
2922 ;; conversion, to prevent ^M characters from popping
2923 ;; up all over the place.
2924 (setq coding
2925 (coding-system-change-eol-conversion
2926 coding
2927 (coding-system-eol-type old-coding)))
4baf35c9
EZ
2928 ;; If old-coding is `undecided', encode-coding-region
2929 ;; will not encode the text at all. Find a proper
2930 ;; non-trivial encoding to use.
2931 (if (memq (coding-system-base old-coding) '(nil undecided))
2932 (setq old-coding
2933 (car (find-coding-systems-region msgbeg msgend))))
d54f26b1
EZ
2934 (setq x-coding-header (point-marker))
2935 (narrow-to-region msgbeg msgend)
d3e1986f
EZ
2936 (and (null raw)
2937 ;; If old and new encoding are the same, it
2938 ;; clearly doesn't make sense to encode.
2939 (not (coding-system-equal
2940 (coding-system-base old-coding)
2941 (coding-system-base coding)))
2942 ;; If the body includes only eight-bit-*
2943 ;; characters, encoding might fail, e.g. with
2944 ;; UTF-8, and isn't needed anyway.
2945 (> (length (delq 'ascii
2946 (delq 'eight-bit-graphic
2947 (delq 'eight-bit-control
2948 (find-charset-region
2949 msgbeg msgend)))))
2950 0)
2951 (encode-coding-region (point) msgend old-coding))
d54f26b1
EZ
2952 (decode-coding-region (point) msgend coding)
2953 (setq last-coding-system-used coding)
2954 ;; Rewrite the coding-system header according
2955 ;; to what we did.
2956 (goto-char x-coding-header)
2957 (delete-region (point)
2958 (save-excursion
2959 (beginning-of-line)
2960 (point)))
2961 (insert "X-Coding-System: "
2962 (symbol-name last-coding-system-used))
2963 (set-marker x-coding-header nil)
2964 (rmail-show-message))
2965 (error "No X-Coding-System header found")))
2966 (or pruned
2967 (rmail-toggle-header 0)))))))
fd4976b8 2968
99887e16
KH
2969;; Find all occurrences of certain fields, and highlight them.
2970(defun rmail-highlight-headers ()
2971 ;; Do this only if the system supports faces.
f9a9d26e
RS
2972 (if (and (fboundp 'internal-find-face)
2973 rmail-highlighted-headers)
99887e16
KH
2974 (save-excursion
2975 (search-forward "\n\n" nil 'move)
2976 (save-restriction
2977 (narrow-to-region (point-min) (point))
2978 (let ((case-fold-search t)
2979 (inhibit-read-only t)
2980 ;; Highlight with boldface if that is available.
2981 ;; Otherwise use the `highlight' face.
765e147a 2982 (face (or 'rmail-highlight
3df6812a
RS
2983 (if (face-differs-from-default-p 'bold)
2984 'bold 'highlight)))
99887e16
KH
2985 ;; List of overlays to reuse.
2986 (overlays rmail-overlay-list))
2987 (goto-char (point-min))
2988 (while (re-search-forward rmail-highlighted-headers nil t)
92e06883 2989 (skip-chars-forward " \t")
99887e16
KH
2990 (let ((beg (point))
2991 overlay)
2992 (while (progn (forward-line 1)
2993 (looking-at "[ \t]")))
2994 ;; Back up over newline, then trailing spaces or tabs
2995 (forward-char -1)
2996 (while (member (preceding-char) '(? ?\t))
2997 (forward-char -1))
2998 (if overlays
2999 ;; Reuse an overlay we already have.
3000 (progn
3001 (setq overlay (car overlays)
3002 overlays (cdr overlays))
3003 (overlay-put overlay 'face face)
3004 (move-overlay overlay beg (point)))
3005 ;; Make a new overlay and add it to
3006 ;; rmail-overlay-list.
3007 (setq overlay (make-overlay beg (point)))
3008 (overlay-put overlay 'face face)
3009 (setq rmail-overlay-list
3010 (cons overlay rmail-overlay-list))))))))))
3011
ab59163d
DL
3012(defun rmail-auto-file ()
3013 "Automatically move a message into a sub-folder based on criteria.
3014Called when a new message is displayed."
3015 (if (or (rmail-message-labels-p rmail-current-message "filed")
3016 (not (string= (buffer-file-name)
3017 (expand-file-name rmail-file-name))))
3018 ;; Do nothing if it's already been filed.
3019 nil
3020 ;; Find out some basics (common fields)
3021 (let ((from (mail-fetch-field "from"))
3022 (subj (mail-fetch-field "subject"))
3023 (to (concat (mail-fetch-field "to") "," (mail-fetch-field "cc")))
3024 (d rmail-automatic-folder-directives)
3025 (directive-loop nil)
3026 (folder nil))
3027 (while d
3028 (setq folder (car (car d))
3029 directive-loop (cdr (car d)))
3030 (while (and (car directive-loop)
3031 (let ((f (cond
3032 ((string= (car directive-loop) "from") from)
3033 ((string= (car directive-loop) "to") to)
3034 ((string= (car directive-loop) "subject") subj)
3035 (t (mail-fetch-field (car directive-loop))))))
3036 (and f (string-match (car (cdr directive-loop)) f))))
3037 (setq directive-loop (cdr (cdr directive-loop))))
3038 ;; If there are no directives left, then it was a complete match.
3039 (if (null directive-loop)
3040 (if (null folder)
3041 (rmail-delete-forward)
3042 (if (string= "/dev/null" folder)
3043 (rmail-delete-message)
3044 (rmail-output-to-rmail-file folder 1 t)
3045 (setq d nil))))
3046 (setq d (cdr d))))))
3047
581d7e0b
RM
3048(defun rmail-next-message (n)
3049 "Show following message whether deleted or not.
3050With prefix arg N, moves forward N messages, or backward if N is negative."
3051 (interactive "p")
6b59a5fc 3052 (set-buffer rmail-buffer)
581d7e0b
RM
3053 (rmail-maybe-set-message-counters)
3054 (rmail-show-message (+ rmail-current-message n)))
3055
3056(defun rmail-previous-message (n)
3057 "Show previous message whether deleted or not.
3058With prefix arg N, moves backward N messages, or forward if N is negative."
3059 (interactive "p")
67f9d50e 3060 (rmail-next-message (- n)))
581d7e0b
RM
3061
3062(defun rmail-next-undeleted-message (n)
3063 "Show following non-deleted message.
3064With prefix arg N, moves forward N non-deleted messages,
c42722e3
RS
3065or backward if N is negative.
3066
3067Returns t if a new message is being shown, nil otherwise."
581d7e0b 3068 (interactive "p")
6b59a5fc 3069 (set-buffer rmail-buffer)
581d7e0b
RM
3070 (rmail-maybe-set-message-counters)
3071 (let ((lastwin rmail-current-message)
3072 (current rmail-current-message))
3073 (while (and (> n 0) (< current rmail-total-messages))
3074 (setq current (1+ current))
3075 (if (not (rmail-message-deleted-p current))
3076 (setq lastwin current n (1- n))))
3077 (while (and (< n 0) (> current 1))
3078 (setq current (1- current))
3079 (if (not (rmail-message-deleted-p current))
3080 (setq lastwin current n (1+ n))))
3081 (if (/= lastwin rmail-current-message)
c42722e3
RS
3082 (progn (rmail-show-message lastwin)
3083 t)
3084 (if (< n 0)
3085 (message "No previous nondeleted message"))
3086 (if (> n 0)
3087 (message "No following nondeleted message"))
3088 nil)))
581d7e0b
RM
3089
3090(defun rmail-previous-undeleted-message (n)
3091 "Show previous non-deleted message.
3092With prefix argument N, moves backward N non-deleted messages,
3093or forward if N is negative."
3094 (interactive "p")
3095 (rmail-next-undeleted-message (- n)))
3096
3097(defun rmail-first-message ()
3098 "Show first message in file."
3099 (interactive)
3100 (rmail-maybe-set-message-counters)
3101 (rmail-show-message 1))
3102
3103(defun rmail-last-message ()
3104 "Show last message in file."
3105 (interactive)
3106 (rmail-maybe-set-message-counters)
3107 (rmail-show-message rmail-total-messages))
3108
3109(defun rmail-what-message ()
3110 (let ((where (point))
3111 (low 1)
3112 (high rmail-total-messages)
3113 (mid (/ rmail-total-messages 2)))
3114 (while (> (- high low) 1)
3115 (if (>= where (rmail-msgbeg mid))
3116 (setq low mid)
3117 (setq high mid))
3118 (setq mid (+ low (/ (- high low) 2))))
3119 (if (>= where (rmail-msgbeg high)) high low)))
3120
bd1f0f84
RS
3121(defun rmail-message-recipients-p (msg recipients &optional primary-only)
3122 (save-restriction
3123 (goto-char (rmail-msgbeg msg))
b4b87b68 3124 (search-forward "\n*** EOOH ***\n")
bd1f0f84
RS
3125 (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
3126 (or (string-match recipients (or (mail-fetch-field "To") ""))
3127 (string-match recipients (or (mail-fetch-field "From") ""))
3128 (if (not primary-only)
3129 (string-match recipients (or (mail-fetch-field "Cc") ""))))))
3130
7cff9c6f
GM
3131(defun rmail-message-regexp-p (n regexp)
3132 "Return t, if for message number N, regexp REGEXP matches in the header."
3133 (let ((beg (rmail-msgbeg n))
3134 (end (rmail-msgend n)))
3135 (goto-char beg)
3136 (forward-line 1)
3137 (save-excursion
3138 (save-restriction
3139 (if (prog1 (= (following-char) ?0)
3140 (forward-line 2)
3141 ;; If there's a Summary-line in the (otherwise empty)
3142 ;; header, we didn't yet get past the EOOH line.
50df7214
GM
3143 (when (looking-at "^\\*\\*\\* EOOH \\*\\*\\*\n")
3144 (forward-line 1))
b2b8f8fa 3145 (setq beg (point))
7cff9c6f 3146 (narrow-to-region (point) end))
67f9d50e 3147 (progn
50df7214
GM
3148 (rfc822-goto-eoh)
3149 (setq end (point)))
b2b8f8fa 3150 (setq beg (point))
50df7214
GM
3151 (search-forward "\n*** EOOH ***\n" end t)
3152 (setq end (1+ (match-beginning 0)))))
b2b8f8fa 3153 (goto-char beg)
6b59a5fc
GM
3154 (if rmail-enable-mime
3155 (funcall rmail-search-mime-header-function n regexp end)
3156 (re-search-forward regexp end t)))))
3157
3158(defun rmail-search-message (msg regexp)
3159 "Return non-nil, if for message number MSG, regexp REGEXP matches."
3160 (goto-char (rmail-msgbeg msg))
3161 (if rmail-enable-mime
3162 (funcall rmail-search-mime-message-function msg regexp)
3163 (re-search-forward regexp (rmail-msgend msg) t)))
bd1f0f84 3164
581d7e0b 3165(defvar rmail-search-last-regexp nil)
e91f80c4 3166(defun rmail-search (regexp &optional n)
40e09f88 3167 "Show message containing next match for REGEXP (but not the current msg).
e91f80c4
RS
3168Prefix argument gives repeat count; negative argument means search
3169backwards (through earlier messages).
3170Interactively, empty argument means use same regexp used last time."
581d7e0b
RM
3171 (interactive
3172 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
3173 (prompt
5b76833f 3174 (concat (if reversep "Reverse " "") "Rmail search (regexp"))
581d7e0b 3175 regexp)
5b76833f
RF
3176 (setq prompt
3177 (concat prompt
3178 (if rmail-search-last-regexp
3179 (concat ", default "
3180 rmail-search-last-regexp "): ")
3181 "): ")))
581d7e0b
RM
3182 (setq regexp (read-string prompt))
3183 (cond ((not (equal regexp ""))
3184 (setq rmail-search-last-regexp regexp))
3185 ((not rmail-search-last-regexp)
3186 (error "No previous Rmail search string")))
e91f80c4
RS
3187 (list rmail-search-last-regexp
3188 (prefix-numeric-value current-prefix-arg))))
3189 (or n (setq n 1))
581d7e0b 3190 (message "%sRmail search for %s..."
df8a44dd 3191 (if (< n 0) "Reverse " "")
581d7e0b 3192 regexp)
6b59a5fc 3193 (set-buffer rmail-buffer)
581d7e0b
RM
3194 (rmail-maybe-set-message-counters)
3195 (let ((omin (point-min))
3196 (omax (point-max))
3197 (opoint (point))
3198 win
e91f80c4 3199 (reversep (< n 0))
581d7e0b
RM
3200 (msg rmail-current-message))
3201 (unwind-protect
3202 (progn
3203 (widen)
e91f80c4
RS
3204 (while (/= n 0)
3205 ;; Check messages one by one, advancing message number up or down
3206 ;; but searching forward through each message.
3207 (if reversep
3208 (while (and (null win) (> msg 1))
6b59a5fc
GM
3209 (setq msg (1- msg)
3210 win (rmail-search-message msg regexp)))
e91f80c4 3211 (while (and (null win) (< msg rmail-total-messages))
6b59a5fc
GM
3212 (setq msg (1+ msg)
3213 win (rmail-search-message msg regexp))))
df8a44dd 3214 (setq n (+ n (if reversep 1 -1)))))
581d7e0b
RM
3215 (if win
3216 (progn
6b59a5fc
GM
3217 (rmail-show-message msg)
3218 ;; Search forward (if this is a normal search) or backward
3219 ;; (if this is a reverse search) through this message to
3220 ;; position point. This search may fail because REGEXP
3221 ;; was found in the hidden portion of this message. In
3222 ;; that case, move point to the beginning of visible
3223 ;; portion.
581d7e0b
RM
3224 (if reversep
3225 (progn
6b59a5fc
GM
3226 (goto-char (point-max))
3227 (re-search-backward regexp nil 'move))
3228 (goto-char (point-min))
3229 (re-search-forward regexp nil t))
581d7e0b
RM
3230 (message "%sRmail search for %s...done"
3231 (if reversep "Reverse " "")
6b59a5fc 3232 regexp))
581d7e0b
RM
3233 (goto-char opoint)
3234 (narrow-to-region omin omax)
3235 (ding)
3236 (message "Search failed: %s" regexp)))))
3237
e91f80c4
RS
3238(defun rmail-search-backwards (regexp &optional n)
3239 "Show message containing previous match for REGEXP.
3240Prefix argument gives repeat count; negative argument means search
3241forward (through later messages).
3242Interactively, empty argument means use same regexp used last time."
3243 (interactive
df8a44dd 3244 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
e91f80c4 3245 (prompt
5b76833f 3246 (concat (if reversep "Reverse " "") "Rmail search (regexp"))
e91f80c4 3247 regexp)
5b76833f
RF
3248 (setq prompt
3249 (concat prompt
3250 (if rmail-search-last-regexp
3251 (concat ", default "
3252 rmail-search-last-regexp "): ")
3253 "): ")))
e91f80c4
RS
3254 (setq regexp (read-string prompt))
3255 (cond ((not (equal regexp ""))
3256 (setq rmail-search-last-regexp regexp))
3257 ((not rmail-search-last-regexp)
3258 (error "No previous Rmail search string")))
3259 (list rmail-search-last-regexp
3260 (prefix-numeric-value current-prefix-arg))))
68d30f2b 3261 (rmail-search regexp (- (or n 1))))
e91f80c4 3262
581d7e0b
RM
3263;; Show the first message which has the `unseen' attribute.
3264(defun rmail-first-unseen-message ()
117f4b92 3265 (rmail-maybe-set-message-counters)
581d7e0b
RM
3266 (let ((current 1)
3267 found)
3268 (save-restriction
3269 (widen)
bb9dcce1 3270 (while (and (not found) (<= current rmail-total-messages))
581d7e0b 3271 (if (rmail-message-labels-p current ", ?\\(unseen\\),")
462c1094
RM
3272 (setq found current))
3273 (setq current (1+ current))))
e9c735fa
JA
3274;; Let the caller show the message.
3275;; (if found
3276;; (rmail-show-message found))
3277 found))
60fb2b34 3278
80068231
AS
3279(defun rmail-current-subject ()
3280 "Return the current subject.
3281The subject is stripped of leading and trailing whitespace, and
3282of typical reply prefixes such as Re:."
3283 (let ((subject (or (mail-fetch-field "Subject") "")))
df211784
RS
3284 (if (string-match "\\`[ \t]+" subject)
3285 (setq subject (substring subject (match-end 0))))
80068231 3286 (if (string-match rmail-reply-regexp subject)
60fb2b34 3287 (setq subject (substring subject (match-end 0))))
df211784
RS
3288 (if (string-match "[ \t]+\\'" subject)
3289 (setq subject (substring subject 0 (match-beginning 0))))
80068231
AS
3290 subject))
3291
3292(defun rmail-current-subject-regexp ()
3293 "Return a regular expression matching the current subject.
3294The regular expression matches the subject header line of
3295messages about the same subject. The subject itself is stripped
3296of leading and trailing whitespace, of typical reply prefixes
3297such as Re: and whitespace within the subject is replaced by a
3298regular expression matching whitespace in general in order to
3299take into account that subject header lines may include newlines
3300and more whitespace. The returned regular expressions contains
3301`rmail-reply-regexp' and ends with a newline."
3302 (let ((subject (rmail-current-subject)))
7c86d70e
EZ
3303 ;; If Subject is long, mailers will break it into several lines at
3304 ;; arbitrary places, so replace whitespace with a regexp that will
3305 ;; match any sequence of spaces, TABs, and newlines.
3306 (setq subject (regexp-quote subject))
3307 (setq subject
3308 (replace-regexp-in-string "[ \t\n]+" "[ \t\n]+" subject t t))
cca24670
EZ
3309 ;; Some mailers insert extra spaces after "Subject:", so allow any
3310 ;; amount of them.
3311 (concat "^Subject:[ \t]+"
80068231
AS
3312 (if (string= "\\`" (substring rmail-reply-regexp 0 2))
3313 (substring rmail-reply-regexp 2)
3314 rmail-reply-regexp)
3315 subject "[ \t]*\n")))
3316
60fb2b34
RS
3317(defun rmail-next-same-subject (n)
3318 "Go to the next mail message having the same subject header.
3319With prefix argument N, do this N times.
3320If N is negative, go backwards instead."
3321 (interactive "p")
80068231 3322 (let ((search-regexp (rmail-current-subject-regexp))
e8417bb0
KH
3323 (forward (> n 0))
3324 (i rmail-current-message)
a21b845b 3325 (case-fold-search t)
80068231 3326 found)
60fb2b34
RS
3327 (save-excursion
3328 (save-restriction
3329 (widen)
3330 (while (and (/= n 0)
3331 (if forward
3332 (< i rmail-total-messages)
3333 (> i 1)))
3334 (let (done)
3335 (while (and (not done)
3336 (if forward
3337 (< i rmail-total-messages)
3338 (> i 1)))
3339 (setq i (if forward (1+ i) (1- i)))
3340 (goto-char (rmail-msgbeg i))
3341 (search-forward "\n*** EOOH ***\n")
3342 (let ((beg (point)) end)
3343 (search-forward "\n\n")
3344 (setq end (point))
3345 (goto-char beg)
3346 (setq done (re-search-forward search-regexp end t))))
3347 (if done (setq found i)))
3348 (setq n (if forward (1- n) (1+ n))))))
3349 (if found
3350 (rmail-show-message found)
3351 (error "No %s message with same subject"
3352 (if forward "following" "previous")))))
3353
3354(defun rmail-previous-same-subject (n)
3355 "Go to the previous mail message having the same subject header.
3356With prefix argument N, do this N times.
3357If N is negative, go forwards instead."
3358 (interactive "p")
3359 (rmail-next-same-subject (- n)))
581d7e0b
RM
3360\f
3361;;;; *** Rmail Message Deletion Commands ***
3362
3363(defun rmail-message-deleted-p (n)
3364 (= (aref rmail-deleted-vector n) ?D))
3365
3366(defun rmail-set-message-deleted-p (n state)
3367 (aset rmail-deleted-vector n (if state ?D ?\ )))
3368
3369(defun rmail-delete-message ()
3370 "Delete this message and stay on it."
3371 (interactive)
6c714afe
RS
3372 (rmail-set-attribute "deleted" t)
3373 (run-hooks 'rmail-delete-message-hook))
581d7e0b
RM
3374
3375(defun rmail-undelete-previous-message ()
3376 "Back up to deleted message, select it, and undelete it."
3377 (interactive)
6b59a5fc 3378 (set-buffer rmail-buffer)
581d7e0b
RM
3379 (let ((msg rmail-current-message))
3380 (while (and (> msg 0)
3381 (not (rmail-message-deleted-p msg)))
3382 (setq msg (1- msg)))
3383 (if (= msg 0)
3384 (error "No previous deleted message")
3385 (if (/= msg rmail-current-message)
3386 (rmail-show-message msg))
bd1f0f84
RS
3387 (rmail-set-attribute "deleted" nil)
3388 (if (rmail-summary-exists)
3389 (save-excursion
3390 (set-buffer rmail-summary-buffer)
3391 (rmail-summary-mark-undeleted msg)))
3392 (rmail-maybe-display-summary))))
581d7e0b
RM
3393
3394(defun rmail-delete-forward (&optional backward)
3395 "Delete this message and move to next nondeleted one.
3396Deleted messages stay in the file until the \\[rmail-expunge] command is given.
c42722e3
RS
3397With prefix argument, delete and move backward.
3398
3399Returns t if a new message is displayed after the delete, or nil otherwise."
581d7e0b
RM
3400 (interactive "P")
3401 (rmail-set-attribute "deleted" t)
6c714afe 3402 (run-hooks 'rmail-delete-message-hook)
bd1f0f84
RS
3403 (let ((del-msg rmail-current-message))
3404 (if (rmail-summary-exists)
4edaa169
RS
3405 (rmail-select-summary
3406 (rmail-summary-mark-deleted del-msg)))
c42722e3
RS
3407 (prog1 (rmail-next-undeleted-message (if backward -1 1))
3408 (rmail-maybe-display-summary))))
581d7e0b
RM
3409
3410(defun rmail-delete-backward ()
3411 "Delete this message and move to previous nondeleted one.
3412Deleted messages stay in the file until the \\[rmail-expunge] command is given."
3413 (interactive)
3414 (rmail-delete-forward t))
3415
1825bea1
RS
3416;; Compute the message number a given message would have after expunging.
3417;; The present number of the message is OLDNUM.
3418;; DELETEDVEC should be rmail-deleted-vector.
3419;; The value is nil for a message that would be deleted.
3420(defun rmail-msg-number-after-expunge (deletedvec oldnum)
3421 (if (or (null oldnum) (= (aref deletedvec oldnum) ?D))
3422 nil
3423 (let ((i 0)
3424 (newnum 0))
3425 (while (< i oldnum)
3426 (if (/= (aref deletedvec i) ?D)
3427 (setq newnum (1+ newnum)))
3428 (setq i (1+ i)))
3429 newnum)))
3430
e6be618b
GM
3431(defun rmail-expunge-confirmed ()
3432 "Return t if deleted message should be expunged. If necessary, ask the user.
3433See also user-option `rmail-confirm-expunge'."
6b59a5fc 3434 (set-buffer rmail-buffer)
e6be618b
GM
3435 (or (not (stringp rmail-deleted-vector))
3436 (not (string-match "D" rmail-deleted-vector))
3437 (null rmail-confirm-expunge)
3438 (funcall rmail-confirm-expunge
3439 "Erase deleted messages from Rmail file? ")))
3440
608aa380 3441(defun rmail-only-expunge (&optional dont-show)
581d7e0b
RM
3442 "Actually erase all deleted messages in the file."
3443 (interactive)
6b59a5fc 3444 (set-buffer rmail-buffer)
581d7e0b
RM
3445 (message "Expunging deleted messages...")
3446 ;; Discard all undo records for this buffer.
3447 (or (eq buffer-undo-list t)
3448 (setq buffer-undo-list nil))
3449 (rmail-maybe-set-message-counters)
3450 (let* ((omax (- (buffer-size) (point-max)))
3451 (omin (- (buffer-size) (point-min)))
3452 (opoint (if (and (> rmail-current-message 0)
881fd7eb
KH
3453 (rmail-message-deleted-p rmail-current-message))
3454 0
6b59a5fc
GM
3455 (if rmail-enable-mime
3456 (with-current-buffer rmail-view-buffer
3457 (- (point)(point-min)))
3458 (- (point) (point-min)))))
581d7e0b
RM
3459 (messages-head (cons (aref rmail-message-vector 0) nil))
3460 (messages-tail messages-head)
3461 ;; Don't make any undo records for the expunging.
3462 (buffer-undo-list t)
3463 (win))
3464 (unwind-protect
3465 (save-excursion
3466 (widen)
3467 (goto-char (point-min))
3468 (let ((counter 0)
3469 (number 1)
3470 (total rmail-total-messages)
3471 (new-message-number rmail-current-message)
3472 (new-summary nil)
0985b412 3473 (new-msgref (list (list 0)))
1825bea1 3474 (rmailbuf (current-buffer))
581d7e0b
RM
3475 (buffer-read-only nil)
3476 (messages rmail-message-vector)
3477 (deleted rmail-deleted-vector)
3478 (summary rmail-summary-vector))
3479 (setq rmail-total-messages nil
3480 rmail-current-message nil
3481 rmail-message-vector nil
3482 rmail-deleted-vector nil
3483 rmail-summary-vector nil)
1825bea1 3484
581d7e0b
RM
3485 (while (<= number total)
3486 (if (= (aref deleted number) ?D)
3487 (progn
3488 (delete-region
3489 (marker-position (aref messages number))
3490 (marker-position (aref messages (1+ number))))
3491 (move-marker (aref messages number) nil)
3492 (if (> new-message-number counter)
3493 (setq new-message-number (1- new-message-number))))
3494 (setq counter (1+ counter))
3495 (setq messages-tail
3496 (setcdr messages-tail
3497 (cons (aref messages number) nil)))
3498 (setq new-summary
3499 (cons (if (= counter number) (aref summary (1- number)))
0985b412
RS
3500 new-summary))
3501 (setq new-msgref
3502 (cons (aref rmail-msgref-vector number)
3503 new-msgref))
3504 (setcar (car new-msgref) counter))
581d7e0b
RM
3505 (if (zerop (% (setq number (1+ number)) 20))
3506 (message "Expunging deleted messages...%d" number)))
3507 (setq messages-tail
3508 (setcdr messages-tail
3509 (cons (aref messages number) nil)))
3510 (setq rmail-current-message new-message-number
3511 rmail-total-messages counter
3512 rmail-message-vector (apply 'vector messages-head)
3513 rmail-deleted-vector (make-string (1+ counter) ?\ )
3514 rmail-summary-vector (vconcat (nreverse new-summary))
0985b412 3515 rmail-msgref-vector (apply 'vector (nreverse new-msgref))
581d7e0b
RM
3516 win t)))
3517 (message "Expunging deleted messages...done")
3518 (if (not win)
3519 (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
608aa380
KH
3520 (if (not dont-show)
3521 (rmail-show-message
c32e5fb0
RS
3522 (if (zerop rmail-current-message) 1 nil)))
3523 (if rmail-enable-mime
3524 (goto-char (+ (point-min) opoint))
3525 (goto-char (+ (point) opoint))))))
bd1f0f84
RS
3526
3527(defun rmail-expunge ()
3528 "Erase deleted messages from Rmail file and summary buffer."
3529 (interactive)
e6be618b 3530 (when (rmail-expunge-confirmed)
9f7c6da9
GM
3531 (rmail-only-expunge)
3532 (if (rmail-summary-exists)
e6be618b 3533 (rmail-select-summary (rmail-update-summary)))))
581d7e0b
RM
3534\f
3535;;;; *** Rmail Mailing Commands ***
3536
bde7cb3f
RS
3537(defun rmail-start-mail (&optional noerase to subject in-reply-to cc
3538 replybuffer sendactions same-window others)
3539 (let (yank-action)
3540 (if replybuffer
3541 (setq yank-action (list 'insert-buffer replybuffer)))
3542 (setq others (cons (cons "cc" cc) others))
3543 (setq others (cons (cons "in-reply-to" in-reply-to) others))
3544 (if same-window
3545 (compose-mail to subject others
3546 noerase nil
3547 yank-action sendactions)
8a60950d 3548 (if rmail-mail-new-frame
bde7cb3f
RS
3549 (prog1
3550 (compose-mail to subject others
3551 noerase 'switch-to-buffer-other-frame
3552 yank-action sendactions)
3553 ;; This is not a standard frame parameter;
3554 ;; nothing except sendmail.el looks at it.
3555 (modify-frame-parameters (selected-frame)
3556 '((mail-dedicated-frame . t))))
3557 (compose-mail to subject others
3558 noerase 'switch-to-buffer-other-window
3559 yank-action sendactions)))))
94ed51e8 3560
581d7e0b 3561(defun rmail-mail ()
bd1f0f84
RS
3562 "Send mail in another window.
3563While composing the message, use \\[mail-yank-original] to yank the
3564original message into it."
581d7e0b 3565 (interactive)
d1e69dec 3566 (rmail-start-mail nil nil nil nil nil rmail-view-buffer))
581d7e0b
RM
3567
3568(defun rmail-continue ()
3569 "Continue composing outgoing message previously being composed."
3570 (interactive)
94ed51e8 3571 (rmail-start-mail t))
581d7e0b
RM
3572
3573(defun rmail-reply (just-sender)
3574 "Reply to the current message.
3575Normally include CC: to all other recipients of original message;
3576prefix argument means ignore them. While composing the reply,
3577use \\[mail-yank-original] to yank the original message into it."
3578 (interactive "P")
11aea8f5 3579 (let (from reply-to cc subject date to message-id references
037fa8ca 3580 resent-to resent-cc resent-reply-to
d1e69dec 3581 (msgnum rmail-current-message))
581d7e0b
RM
3582 (save-excursion
3583 (save-restriction
a2cc5c4f
GM
3584 (if rmail-enable-mime
3585 (narrow-to-region
3586 (goto-char (point-min))
3587 (if (search-forward "\n\n" nil 'move)
3588 (1+ (match-beginning 0))
3589 (point)))
6b59a5fc
GM
3590 (widen)
3591 (goto-char (rmail-msgbeg rmail-current-message))
3592 (forward-line 1)
3593 (if (= (following-char) ?0)
3594 (narrow-to-region
3595 (progn (forward-line 2)
3596 (point))
3597 (progn (search-forward "\n\n" (rmail-msgend rmail-current-message)
3598 'move)
3599 (point)))
3600 (narrow-to-region (point)
3601 (progn (search-forward "\n*** EOOH ***\n")
3602 (beginning-of-line) (point)))))
037fa8ca 3603 (setq from (mail-fetch-field "from")
5b9b5b89 3604 reply-to (or (mail-fetch-field "mail-reply-to" nil t)
8510cb94 3605 (mail-fetch-field "reply-to" nil t)
581d7e0b 3606 from)
037fa8ca
RS
3607 subject (mail-fetch-field "subject")
3608 date (mail-fetch-field "date")
037fa8ca 3609 message-id (mail-fetch-field "message-id")
11aea8f5
RS
3610 references (mail-fetch-field "references" nil nil t)
3611 resent-reply-to (mail-fetch-field "resent-reply-to" nil t)
037fa8ca
RS
3612 resent-cc (and (not just-sender)
3613 (mail-fetch-field "resent-cc" nil t))
3614 resent-to (or (mail-fetch-field "resent-to" nil t) "")
3615;;; resent-subject (mail-fetch-field "resent-subject")
3616;;; resent-date (mail-fetch-field "resent-date")
3617;;; resent-message-id (mail-fetch-field "resent-message-id")
5b9b5b89
RS
3618 )
3619 (unless just-sender
3620 (if (mail-fetch-field "mail-followup-to" nil t)
3621 ;; If this header field is present, use it instead of the To and CC fields.
3622 (setq to (mail-fetch-field "mail-followup-to" nil t))
3623 (setq cc (or (mail-fetch-field "cc" nil t) "")
3624 to (or (mail-fetch-field "to" nil t) ""))))
3625
3626 ))
3627
037fa8ca
RS
3628 ;; Merge the resent-to and resent-cc into the to and cc.
3629 (if (and resent-to (not (equal resent-to "")))
3630 (if (not (equal to ""))
3631 (setq to (concat to ", " resent-to))
3632 (setq to resent-to)))
3633 (if (and resent-cc (not (equal resent-cc "")))
3634 (if (not (equal cc ""))
3635 (setq cc (concat cc ", " resent-cc))
3636 (setq cc resent-cc)))
3637 ;; Add `Re: ' to subject if not there already.
bd1f0f84 3638 (and (stringp subject)
08960da1
KH
3639 (setq subject
3640 (concat rmail-reply-prefix
a21b845b
KH
3641 (if (let ((case-fold-search t))
3642 (string-match rmail-reply-regexp subject))
08960da1
KH
3643 (substring subject (match-end 0))
3644 subject))))
95b597ce
RS
3645 (rmail-start-mail
3646 nil
3647 ;; Using mail-strip-quoted-names is undesirable with newer mailers
3648 ;; since they can handle the names unstripped.
3649 ;; I don't know whether there are other mailers that still
3650 ;; need the names to be stripped.
ab233196 3651;;; (mail-strip-quoted-names reply-to)
d184e182
EZ
3652 ;; Remove unwanted names from reply-to, since Mail-Followup-To
3653 ;; header causes all the names in it to wind up in reply-to, not
3654 ;; in cc. But if what's left is an empty list, use the original.
3655 (let* ((reply-to-list (rmail-dont-reply-to reply-to)))
3656 (if (string= reply-to-list "") reply-to reply-to-list))
95b597ce
RS
3657 subject
3658 (rmail-make-in-reply-to-field from date message-id)
3659 (if just-sender
3660 nil
3661 ;; mail-strip-quoted-names is NOT necessary for rmail-dont-reply-to
3662 ;; to do its job.
3663 (let* ((cc-list (rmail-dont-reply-to
3664 (mail-strip-quoted-names
3665 (if (null cc) to (concat to ", " cc))))))
3666 (if (string= cc-list "") nil cc-list)))
3667 rmail-view-buffer
3668 (list (list 'rmail-mark-message
6b59a5fc
GM
3669 rmail-buffer
3670 (with-current-buffer rmail-buffer
3671 (aref rmail-msgref-vector msgnum))
95b597ce
RS
3672 "answered"))
3673 nil
3674 (list (cons "References" (concat (mapconcat 'identity references " ")
3675 " " message-id))))))
fa2795ca 3676
0985b412
RS
3677(defun rmail-mark-message (buffer msgnum-list attribute)
3678 "Give BUFFER's message number in MSGNUM-LIST the attribute ATTRIBUTE.
3679This is use in the send-actions for message buffers.
3680MSGNUM-LIST is a list of the form (MSGNUM)
3681which is an element of rmail-msgref-vector."
fa2795ca
RS
3682 (save-excursion
3683 (set-buffer buffer)
0985b412
RS
3684 (if (car msgnum-list)
3685 (rmail-set-attribute attribute t (car msgnum-list)))))
581d7e0b
RM
3686
3687(defun rmail-make-in-reply-to-field (from date message-id)
3688 (cond ((not from)
3689 (if message-id
3690 message-id
3691 nil))
3692 (mail-use-rfc822
3693 (require 'rfc822)
3694 (let ((tem (car (rfc822-addresses from))))
3695 (if message-id
8148a3ad
RS
3696 (if (or (not tem)
3697 (string-match
3698 (regexp-quote (if (string-match "@[^@]*\\'" tem)
3699 (substring tem 0
3700 (match-beginning 0))
3701 tem))
3702 message-id))
3703 ;; missing From, or Message-ID is sufficiently informative
581d7e0b
RM
3704 message-id
3705 (concat message-id " (" tem ")"))
1de48e7f
RS
3706 ;; Copy TEM, discarding text properties.
3707 (setq tem (copy-sequence tem))
3708 (set-text-properties 0 (length tem) nil tem)
3709 (setq tem (copy-sequence tem))
3710 ;; Use prin1 to fake RFC822 quoting
3711 (let ((field (prin1-to-string tem)))
3712 (if date
3713 (concat field "'s message of " date)
3714 field)))))
3210e730
RS
3715 ((let* ((foo "[^][\000-\037()<>@,;:\\\" ]+")
3716 (bar "[^][\000-\037()<>@,;:\\\"]+"))
3717 ;; These strings both match all non-ASCII characters.
581d7e0b
RM
3718 (or (string-match (concat "\\`[ \t]*\\(" bar
3719 "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
3720 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
3721 from)
3722 (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
3723 bar "\\))[ \t]*\\'")
3724 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
3725 from)))
3726 (let ((start (match-beginning 1))
3727 (end (match-end 1)))
3728 ;; Trim whitespace which above regexp match allows
3729 (while (and (< start end)
3730 (memq (aref from start) '(?\t ?\ )))
3731 (setq start (1+ start)))
3732 (while (and (< start end)
3733 (memq (aref from (1- end)) '(?\t ?\ )))
3734 (setq end (1- end)))
3735 (let ((field (substring from start end)))
3736 (if date (setq field (concat "message from " field " on " date)))
3737 (if message-id
3738 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
3739 (concat message-id " (" field ")")
3740 field))))
3741 (t
3742 ;; If we can't kludge it simply, do it correctly
3743 (let ((mail-use-rfc822 t))
3744 (rmail-make-in-reply-to-field from date message-id)))))
a16aef15 3745\f
8159f4af
RS
3746(defun rmail-forward (resend)
3747 "Forward the current message to another user.
3748With prefix argument, \"resend\" the message instead of forwarding it;
3749see the documentation of `rmail-resend'."
3750 (interactive "P")
3751 (if resend
3752 (call-interactively 'rmail-resend)
6b59a5fc 3753 (let ((forward-buffer rmail-buffer)
1825bea1 3754 (msgnum rmail-current-message)
8159f4af
RS
3755 (subject (concat "["
3756 (let ((from (or (mail-fetch-field "From")
3757 (mail-fetch-field ">From"))))
3758 (if from
3759 (concat (mail-strip-quoted-names from) ": ")
3760 ""))
3761 (or (mail-fetch-field "Subject") "")
3762 "]")))
bde7cb3f
RS
3763 (if (rmail-start-mail
3764 nil nil subject nil nil nil
fa2795ca 3765 (list (list 'rmail-mark-message
0985b412 3766 forward-buffer
6b59a5fc
GM
3767 (with-current-buffer rmail-buffer
3768 (aref rmail-msgref-vector msgnum))
fa2795ca 3769 "forwarded"))
bde7cb3f
RS
3770 ;; If only one window, use it for the mail buffer.
3771 ;; Otherwise, use another window for the mail buffer
3772 ;; so that the Rmail buffer remains visible
3773 ;; and sending the mail will get back to it.
3774 (and (not rmail-mail-new-frame) (one-window-p t)))
1825bea1 3775 ;; The mail buffer is now current.
b1d9bfa7 3776 (save-excursion
0f0a85b3 3777 ;; Insert after header separator--before signature if any.
c7ed80bc 3778 (goto-char (mail-text-start))
0c773047 3779 (if (or rmail-enable-mime rmail-enable-mime-composing)
6b59a5fc
GM
3780 (funcall rmail-insert-mime-forwarded-message-function
3781 forward-buffer)
3782 (insert "------- Start of forwarded message -------\n")
3783 ;; Quote lines with `- ' if they start with `-'.
3784 (let ((beg (point)) end)
3785 (setq end (point-marker))
3786 (set-marker-insertion-type end t)
3787 (insert-buffer-substring forward-buffer)
3788 (goto-char beg)
3789 (while (re-search-forward "^-" end t)
3790 (beginning-of-line)
3791 (insert "- ")
3792 (forward-line 1))
3793 (goto-char end)
3794 (skip-chars-backward "\n")
3795 (if (< (point) end)
3796 (forward-char 1))
3797 (delete-region (point) end)
3798 (set-marker end nil))
3799 (insert "------- End of forwarded message -------\n"))
1825bea1 3800 (push-mark))))))
a16aef15 3801\f
581d7e0b
RM
3802(defun rmail-resend (address &optional from comment mail-alias-file)
3803 "Resend current message to ADDRESSES.
d2fc297c 3804ADDRESSES should be a single address, a string consisting of several
581d7e0b
RM
3805addresses separated by commas, or a list of addresses.
3806
3807Optional FROM is the address to resend the message from, and
9a2fe7b2
RS
3808defaults from the value of `user-mail-address'.
3809Optional COMMENT is a string to insert as a comment in the resent message.
581d7e0b
RM
3810Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
3811typically for purposes of moderating a list."
3812 (interactive "sResend to: ")
9ad8712e
RS
3813 (require 'sendmail)
3814 (require 'mailalias)
9652931f
GM
3815 (unless (or (eq rmail-view-buffer (current-buffer))
3816 (eq rmail-buffer (current-buffer)))
70908594 3817 (error "Not an Rmail buffer"))
9a2fe7b2 3818 (if (not from) (setq from user-mail-address))
581d7e0b 3819 (let ((tembuf (generate-new-buffer " sendmail temp"))
581d7e0b 3820 (case-fold-search nil)
75e6b970
RS
3821 (mail-personal-alias-file
3822 (or mail-alias-file mail-personal-alias-file))
7d8a6e1f 3823 (mailbuf rmail-buffer))
581d7e0b 3824 (unwind-protect
7d8a6e1f 3825 (with-current-buffer tembuf
581d7e0b 3826 ;;>> Copy message into temp buffer
9652931f
GM
3827 (if rmail-enable-mime
3828 (funcall rmail-insert-mime-resent-message-function mailbuf)
3829 (insert-buffer-substring mailbuf))
581d7e0b 3830 (goto-char (point-min))
e20903fb 3831 ;; Delete any Sender field, since that's not specifiable.
d2fc297c
RS
3832 ; Only delete Sender fields in the actual header.
3833 (re-search-forward "^$" nil 'move)
3834 ; Using "while" here rather than "if" because some buggy mail
3835 ; software may have inserted multiple Sender fields.
3836 (while (re-search-backward "^Sender:" nil t)
3837 (let (beg)
3838 (setq beg (point))
3839 (forward-line 1)
3840 (while (looking-at "[ \t]")
3841 (forward-line 1))
3842 (delete-region beg (point))))
3843 ; Go back to the beginning of the buffer so the Resent- fields
3844 ; are inserted there.
3845 (goto-char (point-min))
581d7e0b
RM
3846 ;;>> Insert resent-from:
3847 (insert "Resent-From: " from "\n")
bcf12025 3848 (insert "Resent-Date: " (mail-rfc822-date) "\n")
581d7e0b
RM
3849 ;;>> Insert resent-to: and bcc if need be.
3850 (let ((before (point)))
c42722e3
RS
3851 (if mail-self-blind
3852 (insert "Resent-Bcc: " (user-login-name) "\n"))
581d7e0b
RM
3853 (insert "Resent-To: " (if (stringp address)
3854 address
3855 (mapconcat 'identity address ",\n\t"))
3856 "\n")
8095bf23 3857 ;; Expand abbrevs in the recipients.
50703776 3858 (save-excursion
8095bf23 3859 (if (featurep 'mailabbrev)
3f3495d3
RS
3860 (let ((end (point-marker))
3861 (local-abbrev-table mail-abbrevs)
3862 (old-syntax-table (syntax-table)))
3863 (if (and (not (vectorp mail-abbrevs))
3864 (file-exists-p mail-personal-alias-file))
3865 (build-mail-abbrevs))
ed1086b7
RS
3866 (unless mail-abbrev-syntax-table
3867 (mail-abbrev-make-syntax-table))
3f3495d3
RS
3868 (set-syntax-table mail-abbrev-syntax-table)
3869 (goto-char before)
3870 (while (and (< (point) end)
3871 (progn (forward-word 1)
3872 (<= (point) end)))
3873 (expand-abbrev))
3874 (set-syntax-table old-syntax-table))
8095bf23 3875 (expand-mail-aliases before (point)))))
581d7e0b
RM
3876 ;;>> Set up comment, if any.
3877 (if (and (sequencep comment) (not (zerop (length comment))))
3878 (let ((before (point))
3879 after)
3880 (insert comment)
3881 (or (eolp) (insert "\n"))
3882 (setq after (point))
3883 (goto-char before)
3884 (while (< (point) after)
3885 (insert "Resent-Comment: ")
3886 (forward-line 1))))
3887 ;; Don't expand aliases in the destination fields
3888 ;; of the original message.
3889 (let (mail-aliases)
389e8f11 3890 (funcall send-mail-function)))
8159f4af 3891 (kill-buffer tembuf))
7d8a6e1f
KH
3892 (with-current-buffer rmail-buffer
3893 (rmail-set-attribute "resent" t rmail-current-message))))
a16aef15 3894\f
72e609d0 3895(defvar mail-unsent-separator
f2da0c68
RS
3896 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
3897 "^ *---+ +Returned message +---+ *$\\|"
dc47993c 3898 "^ *---+ *Returned mail follows *---+ *$\\|"
20424e68 3899 "^Start of returned message$\\|"
0599b925 3900 "^---+ Below this line is a copy of the message.$\\|"
db32c193 3901 "^ *---+ +Original message +---+ *$\\|"
487fcdc0 3902 "^ *--+ +begin message +--+ *$\\|"
c42722e3 3903 "^ *---+ +Original message follows +---+ *$\\|"
f980ec39 3904 "^ *---+ +Your message follows +---+ *$\\|"
ea0d05b4 3905 "^|? *---+ +Message text follows: +---+ *|?$\\|"
c70d7359 3906 "^ *---+ +This is a copy of \\w+ message, including all the headers.*---+ *$")
03863002 3907 "A regexp that matches the separator before the text of a failed message.")
581d7e0b 3908
55a42e8c
RS
3909(defvar mail-mime-unsent-header "^Content-Type: message/rfc822 *$"
3910 "A regexp that matches the header of a MIME body part with a failed message.")
3911
581d7e0b
RM
3912(defun rmail-retry-failure ()
3913 "Edit a mail message which is based on the contents of the current message.
3914For a message rejected by the mail system, extract the interesting headers and
03863002 3915the body of the original message.
55a42e8c
RS
3916If the failed message is a MIME multipart message, it is searched for a
3917body part with a header which matches the variable `mail-mime-unsent-header'.
3918Otherwise, the variable `mail-unsent-separator' should match the string that
3db0cdac
RS
3919delimits the returned original message.
3920The variable `rmail-retry-ignored-headers' is a regular expression
3921specifying headers which should not be copied into the new message."
581d7e0b
RM
3922 (interactive)
3923 (require 'mail-utils)
2079601b 3924 (let ((rmail-this-buffer (current-buffer))
40e09f88 3925 (msgnum rmail-current-message)
ed104a87
RS
3926 bounce-start bounce-end bounce-indent resending
3927 ;; Fetch any content-type header in current message
3928 ;; Must search thru the whole unpruned header.
3929 (content-type
3930 (save-excursion
3931 (save-restriction
3932 (rmail-narrow-to-non-pruned-header)
3933 (mail-fetch-field "Content-Type") ))))
3934 (save-excursion
3935 (goto-char (point-min))
3936 (let ((case-fold-search t))
67f9d50e
FP
3937 (if (and content-type
3938 (string-match
3939 ";[\n\t ]*boundary=\"?\\([-0-9a-z'()+_,./:=? ]+\\)\"?"
ed104a87
RS
3940 content-type))
3941 ;; Handle a MIME multipart bounce message.
3942 (let ((codestring
3943 (concat "\n--"
67f9d50e 3944 (substring content-type (match-beginning 1)
ed104a87
RS
3945 (match-end 1)))))
3946 (unless (re-search-forward mail-mime-unsent-header nil t)
3947 (error "Cannot find beginning of header in failed message"))
3948 (unless (search-forward "\n\n" nil t)
3949 (error "Cannot find start of Mime data in failed message"))
3950 (setq bounce-start (point))
3951 (if (search-forward codestring nil t)
3952 (setq bounce-end (match-beginning 0))
3953 (setq bounce-end (point-max))))
3954 ;; Non-MIME bounce.
3955 (or (re-search-forward mail-unsent-separator nil t)
3956 (error "Cannot parse this as a failure message"))
3957 (skip-chars-forward "\n")
3958 ;; Support a style of failure message in which the original
3959 ;; message is indented, and included within lines saying
3960 ;; `Start of returned message' and `End of returned message'.
3961 (if (looking-at " +Received:")
3962 (progn
3963 (setq bounce-start (point))
3964 (skip-chars-forward " ")
3965 (setq bounce-indent (- (current-column)))
3966 (goto-char (point-max))
3967 (re-search-backward "^End of returned message$" nil t)
3968 (setq bounce-end (point)))
3969 ;; One message contained a few random lines before
3970 ;; the old message header. The first line of the
3971 ;; message started with two hyphens. A blank line
3972 ;; followed these random lines. The same line
3973 ;; beginning with two hyphens was possibly marking
3974 ;; the end of the message.
3975 (if (looking-at "^--")
3976 (let ((boundary (buffer-substring-no-properties
3977 (point)
3978 (progn (end-of-line) (point)))))
3979 (search-forward "\n\n")
3980 (skip-chars-forward "\n")
3981 (setq bounce-start (point))
3982 (goto-char (point-max))
3983 (search-backward (concat "\n\n" boundary) bounce-start t)
3984 (setq bounce-end (point)))
3985 (setq bounce-start (point)
3986 bounce-end (point-max)))
3987 (unless (search-forward "\n\n" nil t)
3988 (error "Cannot find end of header in failed message"))))))
3989 ;; We have found the message that bounced, within the current message.
3990 ;; Now start sending new message; default header fields from original.
3991 ;; Turn off the usual actions for initializing the message body
3992 ;; because we want to get only the text from the failure message.
3993 (let (mail-signature mail-setup-hook)
3994 (if (rmail-start-mail nil nil nil nil nil rmail-this-buffer
3995 (list (list 'rmail-mark-message
3996 rmail-this-buffer
3997 (aref rmail-msgref-vector msgnum)
3998 "retried")))
3999 ;; Insert original text as initial text of new draft message.
4000 ;; Bind inhibit-read-only since the header delimiter
4001 ;; of the previous message was probably read-only.
4002 (let ((inhibit-read-only t)
4003 rmail-displayed-headers
4004 rmail-ignored-headers)
4005 (erase-buffer)
4006 (insert-buffer-substring rmail-this-buffer
4007 bounce-start bounce-end)
4008 (goto-char (point-min))
4009 (if bounce-indent
4010 (indent-rigidly (point-min) (point-max) bounce-indent))
4011 (rmail-clear-headers rmail-retry-ignored-headers)
4012 (rmail-clear-headers "^sender:\\|^return-path:\\|^received:")
4013 (mail-sendmail-delimit-header)
4014 (save-restriction
4015 (narrow-to-region (point-min) (mail-header-end))
4016 (setq resending (mail-fetch-field "resent-to"))
4017 (if mail-self-blind
4018 (if resending
4019 (insert "Resent-Bcc: " (user-login-name) "\n")
4020 (insert "BCC: " (user-login-name) "\n"))))
4021 (goto-char (point-min))
4022 (mail-position-on-field (if resending "Resent-To" "To") t))))))
a16aef15 4023\f
bd1f0f84 4024(defun rmail-summary-exists ()
81bb49ce 4025 "Non-nil if in an RMAIL buffer and an associated summary buffer exists.
90254bb0 4026In fact, the non-nil value returned is the summary buffer itself."
bd1f0f84
RS
4027 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
4028 rmail-summary-buffer))
4029
4030(defun rmail-summary-displayed ()
81bb49ce 4031 "t if in RMAIL buffer and an associated summary buffer is displayed."
bd1f0f84
RS
4032 (and rmail-summary-buffer (get-buffer-window rmail-summary-buffer)))
4033
9bb97fe9 4034(defcustom rmail-redisplay-summary nil
bd1f0f84 4035 "*Non-nil means Rmail should show the summary when it changes.
9bb97fe9
RS
4036This has an effect only if a summary buffer exists."
4037 :type 'boolean
4038 :group 'rmail-summary)
4039
4040(defcustom rmail-summary-window-size nil
4041 "*Non-nil means specify the height for an Rmail summary window."
d177349a 4042 :type '(choice (const :tag "Disabled" nil) integer)
9bb97fe9 4043 :group 'rmail-summary)
90254bb0 4044
bd1f0f84
RS
4045;; Put the summary buffer back on the screen, if user wants that.
4046(defun rmail-maybe-display-summary ()
90254bb0
RS
4047 (let ((selected (selected-window))
4048 window)
4049 ;; If requested, make sure the summary is displayed.
4050 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
4051 rmail-redisplay-summary
6b265868
RS
4052 (if (get-buffer-window rmail-summary-buffer 0)
4053 ;; It's already in some frame; show that one.
4054 (let ((frame (window-frame
4055 (get-buffer-window rmail-summary-buffer 0))))
4056 (make-frame-visible frame)
4057 (raise-frame frame))
4058 (display-buffer rmail-summary-buffer)))
90254bb0
RS
4059 ;; If requested, set the height of the summary window.
4060 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
4061 rmail-summary-window-size
4062 (setq window (get-buffer-window rmail-summary-buffer))
6b265868
RS
4063 ;; Don't try to change the size if just one window in frame.
4064 (not (eq window (frame-root-window (window-frame window))))
67f9d50e 4065 (unwind-protect
90254bb0
RS
4066 (progn
4067 (select-window window)
35aba739 4068 (enlarge-window (- rmail-summary-window-size (window-height))))
90254bb0 4069 (select-window selected)))))
581d7e0b 4070\f
271c888a
SM
4071;;;; *** Rmail Local Fontification ***
4072
4073(defun rmail-fontify-buffer-function ()
4074 ;; This function's symbol is bound to font-lock-fontify-buffer-function.
f512e4c0 4075 (add-hook 'rmail-show-message-hook 'rmail-fontify-message nil t)
35aba739
SM
4076 ;; If we're already showing a message, fontify it now.
4077 (if rmail-current-message (rmail-fontify-message))
4078 ;; Prevent Font Lock mode from kicking in.
f512e4c0 4079 (setq font-lock-fontified t))
271c888a
SM
4080
4081(defun rmail-unfontify-buffer-function ()
4082 ;; This function's symbol is bound to font-lock-fontify-unbuffer-function.
f512e4c0
SM
4083 (let ((modified (buffer-modified-p))
4084 (buffer-undo-list t) (inhibit-read-only t)
4085 before-change-functions after-change-functions
4086 buffer-file-name buffer-file-truename)
4087 (save-restriction
4088 (widen)
4089 (remove-hook 'rmail-show-message-hook 'rmail-fontify-message t)
4090 (remove-text-properties (point-min) (point-max) '(rmail-fontified nil))
1afb7be2
RS
4091 (font-lock-default-unfontify-buffer)
4092 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))
271c888a
SM
4093
4094(defun rmail-fontify-message ()
4095 ;; Fontify the current message if it is not already fontified.
4096 (if (text-property-any (point-min) (point-max) 'rmail-fontified nil)
4097 (let ((modified (buffer-modified-p))
4098 (buffer-undo-list t) (inhibit-read-only t)
4099 before-change-functions after-change-functions
4100 buffer-file-name buffer-file-truename)
4101 (save-excursion
4102 (save-match-data
4103 (add-text-properties (point-min) (point-max) '(rmail-fontified t))
1afb7be2
RS
4104 (font-lock-fontify-region (point-min) (point-max))
4105 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))))
b04fa5c5 4106\f
03c9127e 4107;;; Speedbar support for RMAIL files.
b04fa5c5
KH
4108(eval-when-compile (require 'speedbar))
4109
4110(defvar rmail-speedbar-match-folder-regexp "^[A-Z0-9]+\\(\\.[A-Z0-9]+\\)?$"
82e736c1 4111 "*This regex is used to match folder names to be displayed in speedbar.
b04fa5c5
KH
4112Enabling this will permit speedbar to display your folders for easy
4113browsing, and moving of messages.")
03c9127e
EL
4114
4115(defvar rmail-speedbar-last-user nil
4116 "The last user to be displayed in the speedbar.")
4117
b04fa5c5
KH
4118(defvar rmail-speedbar-key-map nil
4119 "Keymap used when in rmail display mode.")
4120
4121(defun rmail-install-speedbar-variables ()
4122 "Install those variables used by speedbar to enhance rmail."
4123 (if rmail-speedbar-key-map
4124 nil
4125 (setq rmail-speedbar-key-map (speedbar-make-specialized-keymap))
4126
4127 (define-key rmail-speedbar-key-map "e" 'speedbar-edit-line)
4128 (define-key rmail-speedbar-key-map "r" 'speedbar-edit-line)
4129 (define-key rmail-speedbar-key-map "\C-m" 'speedbar-edit-line)
4130 (define-key rmail-speedbar-key-map "M"
4131 'rmail-speedbar-move-message-to-folder-on-line)))
4132
03c9127e 4133(defvar rmail-speedbar-menu-items
b04fa5c5
KH
4134 '(["Read Folder" speedbar-edit-line t]
4135 ["Move message to folder" rmail-speedbar-move-message-to-folder-on-line
03c9127e
EL
4136 (save-excursion (beginning-of-line)
4137 (looking-at "<M> "))])
4138 "Additional menu-items to add to speedbar frame.")
4139
b04fa5c5
KH
4140;; Make sure our special speedbar major mode is loaded
4141(if (featurep 'speedbar)
4142 (rmail-install-speedbar-variables)
4143 (add-hook 'speedbar-load-hook 'rmail-install-speedbar-variables))
4144
03c9127e
EL
4145(defun rmail-speedbar-buttons (buffer)
4146 "Create buttons for BUFFER containing rmail messages.
4147Click on the address under Reply to: to reply to this person.
4148Under Folders: Click a name to read it, or on the <M> to move the
4149current message into that RMAIL folder."
4150 (let ((from nil))
4151 (save-excursion
4152 (set-buffer buffer)
4153 (goto-char (point-min))
4154 (if (not (re-search-forward "^Reply-To: " nil t))
4155 (if (not (re-search-forward "^From:? " nil t))
4156 (setq from t)))
4157 (if from
4158 nil
4159 (setq from (buffer-substring (point) (save-excursion
4160 (end-of-line)
4161 (point))))))
4162 (goto-char (point-min))
4163 (if (and (looking-at "Reply to:")
4164 (equal from rmail-speedbar-last-user))
4165 nil
4166 (setq rmail-speedbar-last-user from)
4167 (erase-buffer)
4168 (insert "Reply To:\n")
4169 (if (stringp from)
4170 (speedbar-insert-button from 'speedbar-directory-face 'highlight
4171 'rmail-speedbar-button 'rmail-reply))
4172 (insert "Folders:\n")
4173 (let* ((case-fold-search nil)
4174 (df (directory-files (save-excursion (set-buffer buffer)
4175 default-directory)
b04fa5c5 4176 nil rmail-speedbar-match-folder-regexp)))
03c9127e
EL
4177 (while df
4178 (speedbar-insert-button "<M>" 'speedbar-button-face 'highlight
4179 'rmail-speedbar-move-message (car df))
4180 (speedbar-insert-button (car df) 'speedbar-file-face 'highlight
4181 'rmail-speedbar-find-file nil t)
4182 (setq df (cdr df)))))))
4183
4184(defun rmail-speedbar-button (text token indent)
4185 "Execute an rmail command specified by TEXT.
4186The command used is TOKEN. INDENT is not used."
4187 (speedbar-with-attached-buffer
4188 (funcall token t)))
4189
4190(defun rmail-speedbar-find-file (text token indent)
4191 "Load in the rmail file TEXT.
4192TOKEN and INDENT are not used."
4193 (speedbar-with-attached-buffer
4194 (message "Loading in RMAIL file %s..." text)
4195 (find-file text)))
4196
b04fa5c5 4197(defun rmail-speedbar-move-message-to-folder-on-line ()
03c9127e
EL
4198 "If the current line is a folder, move current message to it."
4199 (interactive)
4200 (save-excursion
4201 (beginning-of-line)
4202 (if (re-search-forward "<M> " (save-excursion (end-of-line) (point)) t)
4203 (progn
4204 (forward-char -2)
4205 (speedbar-do-function-pointer)))))
4206
4207(defun rmail-speedbar-move-message (text token indent)
4208 "From button TEXT, copy current message to the rmail file specified by TOKEN.
4209TEXT and INDENT are not used."
4210 (speedbar-with-attached-buffer
4211 (message "Moving message to %s" token)
4212 (rmail-output-to-rmail-file token)))
4213
639540ec
RS
4214; Functions for setting, getting and encoding the POP password.
4215; The password is encoded to prevent it from being easily accessible
4216; to "prying eyes." Obviously, this encoding isn't "real security,"
4217; nor is it meant to be.
4218
4219;;;###autoload
1086788e
EZ
4220(defun rmail-set-remote-password (password)
4221 "Set PASSWORD to be used for retrieving mail from a POP or IMAP server."
639540ec
RS
4222 (interactive "sPassword: ")
4223 (if password
1086788e 4224 (setq rmail-encoded-remote-password
639540ec 4225 (rmail-encode-string password (emacs-pid)))
1086788e
EZ
4226 (setq rmail-remote-password nil)
4227 (setq rmail-encoded-remote-password nil)))
639540ec 4228
1086788e
EZ
4229(defun rmail-get-remote-password (imap)
4230 "Get the password for retrieving mail from a POP or IMAP server. If none
639540ec 4231has been set, then prompt the user for one."
1086788e
EZ
4232 (when (not rmail-encoded-remote-password)
4233 (if (not rmail-remote-password)
4234 (setq rmail-remote-password
4235 (read-passwd (if imap
4236 "IMAP password: "
4237 "POP password: "))))
4238 (rmail-set-remote-password rmail-remote-password)
4239 (setq rmail-remote-password nil))
4240 (rmail-encode-string rmail-encoded-remote-password (emacs-pid)))
639540ec
RS
4241
4242(defun rmail-have-password ()
1086788e 4243 (or rmail-remote-password rmail-encoded-remote-password))
639540ec
RS
4244
4245(defun rmail-encode-string (string mask)
4246 "Encode STRING with integer MASK, by taking the exclusive OR of the
4247lowest byte in the mask with the first character of string, the
4248second-lowest-byte with the second character of the string, etc.,
4249restarting at the lowest byte of the mask whenever it runs out.
4250Returns the encoded string. Calling the function again with an
4251encoded string (and the same mask) will decode the string."
7d6d84c1 4252 (setq mask (abs mask)) ; doesn't work if negative
67f9d50e 4253 (let* ((string-vector (string-to-vector string)) (i 0)
639540ec
RS
4254 (len (length string-vector)) (curmask mask) charmask)
4255 (while (< i len)
4256 (if (= curmask 0)
4257 (setq curmask mask))
4258 (setq charmask (% curmask 256))
4259 (setq curmask (lsh curmask -8))
4260 (aset string-vector i (logxor charmask (aref string-vector i)))
4261 (setq i (1+ i)))
4262 (concat string-vector)))
03c9127e 4263
431f70e0
LH
4264;;;; Desktop support
4265
431f70e0
LH
4266(defun rmail-restore-desktop-buffer (desktop-buffer-file-name
4267 desktop-buffer-name
4268 desktop-buffer-misc)
4269 "Restore an rmail buffer specified in a desktop file."
4270 (condition-case error
4271 (progn
4272 (rmail-input desktop-buffer-file-name)
4273 (if (eq major-mode 'rmail-mode)
4274 (current-buffer)
4275 rmail-buffer))
4276 (file-locked
4277 (kill-buffer (current-buffer))
4278 nil)))
4279
9e7357b0
AS
4280(add-to-list 'desktop-buffer-mode-handlers
4281 '(rmail-mode . rmail-restore-desktop-buffer))
695a1fa4 4282
49116ac0
JB
4283(provide 'rmail)
4284
cbee283d 4285;; arch-tag: cff0a950-57fe-4f73-a86e-91ff75afd06c
c88ab9ce 4286;;; rmail.el ends here