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