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