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