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