(rmail-use-spam-filter): Add autoload cookie.
[bpt/emacs.git] / lisp / mail / rmail.el
CommitLineData
55535639 1;;; rmail.el --- main code of "RMAIL" mail reader for Emacs
c88ab9ce 2
0404d031 3;; Copyright (C) 1985,86,87,88,93,94,95,96,97,98,2000, 2001
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)
1491 (format ", and found to be a spam message"
1492 rsf-number-of-spam)
1493 (if (> rsf-number-of-spam 1)
1494 (format ", %d of which found to be spam messages"
1495 rsf-number-of-spam)
1496 (format ", one of which found to be a spam message"
1497 rsf-number-of-spam)))
1498 ""))
1499 (if (and rmail-use-spam-filter (> rsf-number-of-spam 0))
1500 (progn (if rmail-spam-filter-beep (beep t))
1501 (sleep-for rmail-spam-sleep-after-message)))
1502
9a10bd0d
KH
1503 ;; Move to the first new message
1504 ;; unless we have other unseen messages before it.
1505 (rmail-show-message (rmail-first-unseen-message))
1506 (run-hooks 'rmail-after-get-new-mail-hook)
1507 (setq found t))))
1508 found)
563ab60d 1509 ;; Don't leave the buffer screwed up if we get a disk-full error.
3ba3fa75 1510 (or found (rmail-show-message)))))
581d7e0b
RM
1511
1512(defun rmail-insert-inbox-text (files renamep)
563ab60d
RS
1513 ;; Detect a locked file now, so that we avoid moving mail
1514 ;; out of the real inbox file. (That could scare people.)
1515 (or (memq (file-locked-p buffer-file-name) '(nil t))
1516 (error "RMAIL file %s is locked"
1517 (file-name-nondirectory buffer-file-name)))
639540ec 1518 (let (file tofile delete-files movemail popmail got-password password)
581d7e0b 1519 (while files
1f0402c3
AI
1520 ;; Handle POP mailbox names specially; don't expand as filenames
1521 ;; in case the userid contains a directory separator.
1522 (setq file (car files))
1523 (setq popmail (string-match "^po:" file))
1524 (if popmail
1525 (setq renamep t)
1526 (setq file (file-truename
3a85a179 1527 (substitute-in-file-name (expand-file-name file)))))
1f0402c3 1528 (setq tofile (expand-file-name
ec3bba39
RS
1529 ;; Generate name to move to from inbox name,
1530 ;; in case of multiple inboxes that need moving.
1531 (concat ".newmail-" (file-name-nondirectory file))
1532 ;; Use the directory of this rmail file
1533 ;; because it's a nuisance to use the homedir
1534 ;; if that is on a full disk and this rmail
1535 ;; file isn't.
1536 (file-name-directory
1537 (expand-file-name buffer-file-name))))
1538 ;; Always use movemail to rename the file,
1539 ;; since there can be mailboxes in various directories.
1540 (setq movemail t)
1541;;; ;; If getting from mail spool directory,
1542;;; ;; use movemail to move rather than just renaming,
1543;;; ;; so as to interlock with the mailer.
1544;;; (setq movemail (string= file
1545;;; (file-truename
1546;;; (concat rmail-spool-directory
1547;;; (file-name-nondirectory file)))))
1f0402c3 1548 (if (and movemail (not popmail))
581d7e0b 1549 (progn
581d7e0b
RM
1550 ;; On some systems, /usr/spool/mail/foo is a directory
1551 ;; and the actual inbox is /usr/spool/mail/foo/foo.
1552 (if (file-directory-p file)
d0eecffa 1553 (setq file (expand-file-name (user-login-name)
581d7e0b 1554 file)))))
8095bf23 1555 (cond (popmail
639540ec
RS
1556 (if rmail-pop-password-required
1557 (progn (setq got-password (not (rmail-have-password)))
1558 (setq password (rmail-get-pop-password))))
ece5433e 1559 (if (memq system-type '(windows-nt cygwin))
ceeb471c
RS
1560 ;; cannot have "po:" in file name
1561 (setq tofile
1562 (expand-file-name
1f0402c3
AI
1563 (concat ".newmail-pop-"
1564 (file-name-nondirectory (substring file 3)))
ceeb471c
RS
1565 (file-name-directory
1566 (expand-file-name buffer-file-name)))))
8095bf23
RS
1567 (message "Getting mail from post office ..."))
1568 ((and (file-exists-p tofile)
1569 (/= 0 (nth 7 (file-attributes tofile))))
1570 (message "Getting mail from %s..." tofile))
1571 ((and (file-exists-p file)
1572 (/= 0 (nth 7 (file-attributes file))))
1573 (message "Getting mail from %s..." file)))
581d7e0b
RM
1574 ;; Set TOFILE if have not already done so, and
1575 ;; rename or copy the file FILE to TOFILE if and as appropriate.
1576 (cond ((not renamep)
1577 (setq tofile file))
bd1f0f84
RS
1578 ((or (file-exists-p tofile) (and (not popmail)
1579 (not (file-exists-p file))))
581d7e0b 1580 nil)
bd1f0f84 1581 ((and (not movemail) (not popmail))
4144e5cb
RS
1582 ;; Try copying. If that fails (perhaps no space) and
1583 ;; we're allowed to blow away the inbox, rename instead.
1584 (if rmail-preserve-inbox
019bf2a3 1585 (copy-file file tofile nil)
4144e5cb
RS
1586 (condition-case nil
1587 (copy-file file tofile nil)
1588 (error
1589 ;; Third arg is t so we can replace existing file TOFILE.
1590 (rename-file file tofile t))))
ba0b5507
RS
1591 ;; Make the real inbox file empty.
1592 ;; Leaving it deleted could cause lossage
1593 ;; because mailers often won't create the file.
4144e5cb
RS
1594 (if (not rmail-preserve-inbox)
1595 (condition-case ()
1596 (write-region (point) (point) file)
1597 (file-error nil))))
581d7e0b
RM
1598 (t
1599 (let ((errors nil))
1600 (unwind-protect
1601 (save-excursion
1602 (setq errors (generate-new-buffer " *rmail loss*"))
1603 (buffer-disable-undo errors)
67f9d50e
FP
1604 (let ((args
1605 (append
4144e5cb
RS
1606 (list (or rmail-movemail-program
1607 (expand-file-name "movemail"
1608 exec-directory))
1609 nil errors nil)
67f9d50e 1610 (if rmail-preserve-inbox
4144e5cb
RS
1611 (list "-p")
1612 nil)
757c2332 1613 rmail-movemail-flags
4144e5cb 1614 (list file tofile)
639540ec 1615 (if password (list password) nil))))
4144e5cb 1616 (apply 'call-process args))
581d7e0b
RM
1617 (if (not (buffer-modified-p errors))
1618 ;; No output => movemail won
1619 nil
1620 (set-buffer errors)
1621 (subst-char-in-region (point-min) (point-max)
1622 ?\n ?\ )
1623 (goto-char (point-max))
1624 (skip-chars-backward " \t")
1625 (delete-region (point) (point-max))
1626 (goto-char (point-min))
1627 (if (looking-at "movemail: ")
1628 (delete-region (point-min) (match-end 0)))
1629 (beep t)
1157e1bc
KH
1630 (message "movemail: %s"
1631 (buffer-substring (point-min)
1632 (point-max)))
f26d8cd6
RS
1633 ;; If we just read the password, most likely it is
1634 ;; wrong. Otherwise, see if there is a specific
1635 ;; reason to think that the problem is a wrong passwd.
0974f7bf 1636 (if (or got-password
0974f7bf
RS
1637 (re-search-forward rmail-pop-password-error
1638 nil t))
639540ec 1639 (rmail-set-pop-password nil))
581d7e0b
RM
1640 (sit-for 3)
1641 nil))
1642 (if errors (kill-buffer errors))))))
1643 ;; At this point, TOFILE contains the name to read:
1644 ;; Either the alternate name (if we renamed)
1645 ;; or the actual inbox (if not renaming).
1646 (if (file-exists-p tofile)
d1e69dec
KH
1647 (let ((coding-system-for-read 'no-conversion)
1648 size)
581d7e0b 1649 (goto-char (point-max))
d1e69dec 1650 (setq size (nth 1 (insert-file-contents tofile)))
581d7e0b
RM
1651 (goto-char (point-max))
1652 (or (= (preceding-char) ?\n)
1653 (zerop size)
1654 (insert ?\n))
4144e5cb
RS
1655 (if (not (and rmail-preserve-inbox (string= file tofile)))
1656 (setq delete-files (cons tofile delete-files)))))
581d7e0b
RM
1657 (message "")
1658 (setq files (cdr files)))
1659 delete-files))
1660
aa2d5fe4
RS
1661;; Decode the region specified by FROM and TO by CODING.
1662;; If CODING is nil or an invalid coding system, decode by `undecided'.
1663(defun rmail-decode-region (from to coding)
1664 (if (or (not coding) (not (coding-system-p coding)))
1665 (setq coding 'undecided))
1666 (decode-coding-region from to coding))
1667
581d7e0b
RM
1668;; the rmail-break-forwarded-messages feature is not implemented
1669(defun rmail-convert-to-babyl-format ()
1670 (let ((count 0) start
b77ab9e8
RS
1671 (case-fold-search nil)
1672 (invalid-input-resync
1673 (function (lambda ()
1674 (message "Invalid Babyl format in inbox!")
563ab60d 1675 (sit-for 3)
b77ab9e8
RS
1676 ;; Try to get back in sync with a real message.
1677 (if (re-search-forward
95b597ce 1678 (concat rmail-mmdf-delim1 "\\|^From") nil t)
b77ab9e8
RS
1679 (beginning-of-line)
1680 (goto-char (point-max)))))))
581d7e0b
RM
1681 (goto-char (point-min))
1682 (save-restriction
1683 (while (not (eobp))
d1e69dec 1684 (setq start (point))
581d7e0b 1685 (cond ((looking-at "BABYL OPTIONS:");Babyl header
b77ab9e8
RS
1686 (if (search-forward "\n\^_" nil t)
1687 ;; If we find the proper terminator, delete through there.
1688 (delete-region (point-min) (point))
1689 (funcall invalid-input-resync)
a553316b 1690 (delete-region (point-min) (point))))
581d7e0b
RM
1691 ;; Babyl format message
1692 ((looking-at "\^L")
1693 (or (search-forward "\n\^_" nil t)
b77ab9e8 1694 (funcall invalid-input-resync))
581d7e0b
RM
1695 (setq count (1+ count))
1696 ;; Make sure there is no extra white space after the ^_
1697 ;; at the end of the message.
1698 ;; Narrowing will make sure that whatever follows the junk
1699 ;; will be treated properly.
1700 (delete-region (point)
1701 (save-excursion
1702 (skip-chars-forward " \t\n")
1703 (point)))
17139abc
EZ
1704 (save-excursion
1705 (let* ((header-end
1706 (progn
1707 (save-excursion
1708 (goto-char start)
1709 (forward-line 1)
1710 (if (looking-at "0")
1711 (forward-line 1)
1712 (forward-line 2))
1713 (save-restriction
1714 (narrow-to-region (point) (point-max))
1715 (rfc822-goto-eoh)
1716 (point)))))
1717 (case-fold-search t)
1718 (quoted-printable-header-field-end
1719 (save-excursion
1720 (goto-char start)
1721 (re-search-forward
1722 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
1723 header-end t)))
1724 (base64-header-field-end
1725 (save-excursion
1726 (goto-char start)
1727 (re-search-forward
1728 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
1729 header-end t))))
1730 (if quoted-printable-header-field-end
1731 (save-excursion
09843470
RS
1732 (unless
1733 (mail-unquote-printable-region header-end (point) nil t)
1734 (message "Malformed MIME quoted-printable message"))
17139abc
EZ
1735 ;; Change "quoted-printable" to "8bit",
1736 ;; to reflect the decoding we just did.
1737 (goto-char quoted-printable-header-field-end)
1738 (delete-region (point) (search-backward ":"))
1739 (insert ": 8bit")))
1740 (if base64-header-field-end
fa8f290e
EZ
1741 (save-excursion
1742 (when
1743 (condition-case nil
1744 (progn
1745 (base64-decode-region (1+ header-end)
1746 (- (point) 2))
1747 t)
1748 (error nil))
1749 ;; Change "base64" to "8bit", to reflect the
1750 ;; decoding we just did.
1751 (goto-char (1+ header-end))
1752 (while (search-forward "\r\n" (point-max) t)
1753 (replace-match "\n"))
1754 (goto-char base64-header-field-end)
1755 (delete-region (point) (search-backward ":"))
1756 (insert ": 8bit"))))
17139abc
EZ
1757 (setq last-coding-system-used nil)
1758 (or rmail-enable-mime
1759 (not rmail-enable-multibyte)
1760 (let ((mime-charset
1761 (if (and rmail-decode-mime-charset
1762 (save-excursion
1763 (goto-char start)
1764 (search-forward "\n\n" nil t)
1765 (let ((case-fold-search t))
1766 (re-search-backward
1767 rmail-mime-charset-pattern
1768 start t))))
1769 (intern (downcase (match-string 1))))))
1770 (rmail-decode-region start (point) mime-charset)))))
578b6415
RS
1771 ;; Add an X-Coding-System: header if we don't have one.
1772 (save-excursion
1773 (goto-char start)
1774 (forward-line 1)
1775 (if (looking-at "0")
1776 (forward-line 1)
1777 (forward-line 2))
1778 (or (save-restriction
1779 (narrow-to-region (point) (point-max))
1780 (rfc822-goto-eoh)
1781 (goto-char (point-min))
1782 (re-search-forward "^X-Coding-System:" nil t))
1783 (insert "X-Coding-System: "
1784 (symbol-name last-coding-system-used)
1785 "\n")))
d33e73c1
EZ
1786 (narrow-to-region (point) (point-max))
1787 (and (= 0 (% count 10))
a9212241 1788 (message "Converting to Babyl format...%d" count)))
581d7e0b
RM
1789 ;;*** MMDF format
1790 ((let ((case-fold-search t))
95b597ce 1791 (looking-at rmail-mmdf-delim1))
581d7e0b
RM
1792 (let ((case-fold-search t))
1793 (replace-match "\^L\n0, unseen,,\n*** EOOH ***\n")
95b597ce 1794 (re-search-forward rmail-mmdf-delim2 nil t)
581d7e0b
RM
1795 (replace-match "\^_"))
1796 (save-excursion
1797 (save-restriction
1798 (narrow-to-region start (1- (point)))
1799 (goto-char (point-min))
1800 (while (search-forward "\n\^_" nil t); single char "\^_"
1801 (replace-match "\n^_")))); 2 chars: "^" and "_"
578b6415 1802 (setq last-coding-system-used nil)
d1e69dec 1803 (or rmail-enable-mime
7d862e07 1804 (not rmail-enable-multibyte)
426591c3 1805 (decode-coding-region start (point) 'undecided))
578b6415
RS
1806 (save-excursion
1807 (goto-char start)
1808 (forward-line 3)
1809 (insert "X-Coding-System: "
1810 (symbol-name last-coding-system-used)
1811 "\n"))
581d7e0b 1812 (narrow-to-region (point) (point-max))
d33e73c1
EZ
1813 (setq count (1+ count))
1814 (and (= 0 (% count 10))
a9212241 1815 (message "Converting to Babyl format...%d" count)))
581d7e0b
RM
1816 ;;*** Mail format
1817 ((looking-at "^From ")
581d7e0b
RM
1818 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
1819 (rmail-nuke-pinhead-header)
bd1f0f84
RS
1820 ;; If this message has a Content-Length field,
1821 ;; skip to the end of the contents.
1822 (let* ((header-end (save-excursion
1823 (and (re-search-forward "\n\n" nil t)
a865e55b 1824 (1- (point)))))
bd1f0f84 1825 (case-fold-search t)
ae9f695f
KH
1826 (quoted-printable-header-field-end
1827 (save-excursion
ae9f695f 1828 (re-search-forward
4877ba13 1829 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
ae9f695f 1830 header-end t)))
17139abc
EZ
1831 (base64-header-field-end
1832 (save-excursion
1833 (re-search-forward
1834 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
1835 header-end t)))
bd1f0f84
RS
1836 (size
1837 ;; Get the numeric value from the Content-Length field.
1838 (save-excursion
1839 ;; Back up to end of prev line,
1840 ;; in case the Content-Length field comes first.
1841 (forward-char -1)
1842 (and (search-forward "\ncontent-length: "
1843 header-end t)
1844 (let ((beg (point))
1845 (eol (progn (end-of-line) (point))))
5458f7f1
KH
1846 (string-to-int (buffer-substring beg eol)))))))
1847 (and size
1848 (if (and (natnump size)
1849 (<= (+ header-end size) (point-max))
1850 ;; Make sure this would put us at a position
1851 ;; that we could continue from.
1852 (save-excursion
1853 (goto-char (+ header-end size))
1854 (skip-chars-forward "\n")
1855 (or (eobp)
1856 (and (looking-at "BABYL OPTIONS:")
1857 (search-forward "\n\^_" nil t))
1858 (and (looking-at "\^L")
1859 (search-forward "\n\^_" nil t))
1860 (let ((case-fold-search t))
95b597ce 1861 (looking-at rmail-mmdf-delim1))
5458f7f1
KH
1862 (looking-at "From "))))
1863 (goto-char (+ header-end size))
1864 (message "Ignoring invalid Content-Length field")
ae9f695f 1865 (sit-for 1 0 t)))
a4e19cc2
RS
1866 (if (let ((case-fold-search nil))
1867 (re-search-forward
1868 (concat "^[\^_]?\\("
1869 rmail-unix-mail-delimiter
1870 "\\|"
1871 rmail-mmdf-delim1 "\\|"
1872 "^BABYL OPTIONS:\\|"
1873 "\^L\n[01],\\)") nil t))
ae9f695f
KH
1874 (goto-char (match-beginning 1))
1875 (goto-char (point-max)))
1876 (setq count (1+ count))
1877 (if quoted-printable-header-field-end
1878 (save-excursion
09843470
RS
1879 (unless
1880 (mail-unquote-printable-region header-end (point) nil t)
1881
1882 (message "Malformed MIME quoted-printable message"))
ae9f695f
KH
1883 ;; Change "quoted-printable" to "8bit",
1884 ;; to reflect the decoding we just did.
1885 (goto-char quoted-printable-header-field-end)
4877ba13 1886 (delete-region (point) (search-backward ":"))
17139abc
EZ
1887 (insert ": 8bit")))
1888 (if base64-header-field-end
fa8f290e
EZ
1889 (save-excursion
1890 (when
1891 (condition-case nil
1892 (progn
37ec692a 1893 (base64-decode-region (1+ header-end) (point))
fa8f290e
EZ
1894 t)
1895 (error nil))
37ec692a
EZ
1896 (goto-char header-end)
1897 (while (search-forward "\r\n" (point-max) t)
1898 (replace-match "\n"))
fa8f290e
EZ
1899 ;; Change "base64" to "8bit", to reflect the
1900 ;; decoding we just did.
1901 (goto-char base64-header-field-end)
1902 (delete-region (point) (search-backward ":"))
1903 (insert ": 8bit")))))
ae9f695f 1904
581d7e0b
RM
1905 (save-excursion
1906 (save-restriction
1907 (narrow-to-region start (point))
1908 (goto-char (point-min))
1909 (while (search-forward "\n\^_" nil t); single char
1910 (replace-match "\n^_")))); 2 chars: "^" and "_"
1911 (insert ?\^_)
578b6415 1912 (setq last-coding-system-used nil)
d1e69dec 1913 (or rmail-enable-mime
7d862e07 1914 (not rmail-enable-multibyte)
aa2d5fe4
RS
1915 (let ((mime-charset
1916 (if (and rmail-decode-mime-charset
1917 (save-excursion
1918 (goto-char start)
1919 (search-forward "\n\n" nil t)
1920 (let ((case-fold-search t))
1921 (re-search-backward
1922 rmail-mime-charset-pattern
1923 start t))))
1924 (intern (downcase (match-string 1))))))
1925 (rmail-decode-region start (point) mime-charset)))
578b6415
RS
1926 (save-excursion
1927 (goto-char start)
1928 (forward-line 3)
1929 (insert "X-Coding-System: "
1930 (symbol-name last-coding-system-used)
1931 "\n"))
d33e73c1
EZ
1932 (narrow-to-region (point) (point-max))
1933 (and (= 0 (% count 10))
a9212241 1934 (message "Converting to Babyl format...%d" count)))
581d7e0b 1935 ;;
8896f2df
RS
1936 ;; This kludge is because some versions of sendmail.el
1937 ;; insert an extra newline at the beginning that shouldn't
1938 ;; be there. sendmail.el has been fixed, but old versions
1939 ;; may still be in use. -- rms, 7 May 1993.
1940 ((eolp) (delete-char 1))
a553316b 1941 (t (error "Cannot convert to babyl format")))))
581d7e0b
RM
1942 count))
1943
1944;; Delete the "From ..." line, creating various other headers with
1945;; information from it if they don't already exist. Now puts the
c42722e3
RS
1946;; original line into a mail-from: header line for debugging and for
1947;; use by the rmail-output function.
581d7e0b
RM
1948(defun rmail-nuke-pinhead-header ()
1949 (save-excursion
1950 (save-restriction
1951 (let ((start (point))
1952 (end (progn
1953 (condition-case ()
1954 (search-forward "\n\n")
1955 (error
1956 (goto-char (point-max))
1957 (insert "\n\n")))
1958 (point)))
1959 has-from has-date)
1960 (narrow-to-region start end)
1961 (let ((case-fold-search t))
1962 (goto-char start)
1963 (setq has-from (search-forward "\nFrom:" nil t))
1964 (goto-char start)
1965 (setq has-date (and (search-forward "\nDate:" nil t) (point)))
1966 (goto-char start))
1967 (let ((case-fold-search nil))
46947372 1968 (if (re-search-forward (concat "^" rmail-unix-mail-delimiter) nil t)
581d7e0b
RM
1969 (replace-match
1970 (concat
1971 "Mail-from: \\&"
1972 ;; Keep and reformat the date if we don't
1973 ;; have a Date: field.
1974 (if has-date
1975 ""
b7cceaf1 1976 (concat
74d6e31c 1977 "Date: \\2, \\4 \\3 \\9 \\5 "
67f9d50e 1978
b7cceaf1
JB
1979 ;; The timezone could be matched by group 7 or group 10.
1980 ;; If neither of them matched, assume EST, since only
1981 ;; Easterners would be so sloppy.
1982 ;; It's a shame the substitution can't use "\\10".
1983 (cond
1984 ((/= (match-beginning 7) (match-end 7)) "\\7")
1985 ((/= (match-beginning 10) (match-end 10))
1986 (buffer-substring (match-beginning 10)
1987 (match-end 10)))
1988 (t "EST"))
1989 "\n"))
581d7e0b
RM
1990 ;; Keep and reformat the sender if we don't
1991 ;; have a From: field.
1992 (if has-from
1993 ""
15a36ac5
RS
1994 "From: \\1\n"))
1995 t)))))))
581d7e0b
RM
1996\f
1997;;;; *** Rmail Message Formatting and Header Manipulation ***
1998
1999(defun rmail-reformat-message (beg end)
2000 (goto-char beg)
2001 (forward-line 1)
2002 (if (/= (following-char) ?0)
55535639 2003 (error "Bad format in RMAIL file"))
ed104a87 2004 (let ((inhibit-read-only t)
581d7e0b
RM
2005 (delta (- (buffer-size) end)))
2006 (delete-char 1)
2007 (insert ?1)
2008 (forward-line 1)
15a36ac5 2009 (let ((case-fold-search t))
c42722e3
RS
2010 (while (looking-at "Summary-line:\\|Mail-From:")
2011 (forward-line 1)))
581d7e0b
RM
2012 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*\n")
2013 (delete-region (point)
2014 (progn (forward-line 1) (point))))
2015 (let ((str (buffer-substring (point)
2016 (save-excursion (search-forward "\n\n" end 'move)
2017 (point)))))
2018 (insert str "*** EOOH ***\n")
2019 (narrow-to-region (point) (- (buffer-size) delta)))
2020 (goto-char (point-min))
423c0dfa 2021 (if rmail-message-filter (funcall rmail-message-filter))
8095bf23
RS
2022 (if (or rmail-displayed-headers rmail-ignored-headers)
2023 (rmail-clear-headers))))
581d7e0b 2024
3db0cdac 2025(defun rmail-clear-headers (&optional ignored-headers)
8095bf23
RS
2026 "Delete all header fields that Rmail should not show.
2027If the optional argument IGNORED-HEADERS is non-nil,
2028delete all header fields whose names match that regexp.
2029Otherwise, if `rmail-displayed-headers' is non-nil,
2030delete all header fields *except* those whose names match that regexp.
2031Otherwise, delete all header fields whose names match `rmail-ignored-headers'."
f92f70bc
RS
2032 (when (search-forward "\n\n" nil t)
2033 (forward-char -1)
2034 (let ((case-fold-search t)
2035 (buffer-read-only nil))
2036 (if (and rmail-displayed-headers (null ignored-headers))
d155ff18
RS
2037 (save-restriction
2038 (narrow-to-region (point-min) (point))
2039 (let (lim next)
2040 (goto-char (point-min))
2041 (while (and (not (eobp))
2042 (save-excursion
2043 (if (re-search-forward "\n[^ \t]" nil t)
2044 (setq lim (match-beginning 0)
2045 next (1+ lim))
2046 (setq lim nil next (point-max)))))
2047 (if (save-excursion
2048 (re-search-forward rmail-displayed-headers lim t))
2049 (goto-char next)
2050 (delete-region (point) next))))
2051 (goto-char (point-min)))
f92f70bc
RS
2052 (or ignored-headers (setq ignored-headers rmail-ignored-headers))
2053 (save-restriction
2054 (narrow-to-region (point-min) (point))
bdbeb193
GM
2055 (while (and ignored-headers
2056 (progn
2057 (goto-char (point-min))
2058 (re-search-forward ignored-headers nil t)))
f92f70bc
RS
2059 (beginning-of-line)
2060 (delete-region (point)
d155ff18
RS
2061 (if (re-search-forward "\n[^ \t]" nil t)
2062 (1- (point))
2063 (point-max)))))))))
581d7e0b 2064
dbf71ee9 2065(defun rmail-msg-is-pruned ()
581d7e0b 2066 (rmail-maybe-set-message-counters)
dbf71ee9 2067 (save-restriction
903e09b7 2068 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
dbf71ee9 2069 (save-excursion
903e09b7
RS
2070 (goto-char (point-min))
2071 (forward-line 1)
dbf71ee9
RS
2072 (= (following-char) ?1))))
2073
157521c5 2074(defun rmail-msg-restore-non-pruned-header ()
ed104a87
RS
2075 (let ((old-point (point))
2076 new-point
2077 new-start
2078 (inhibit-read-only t))
2079 (save-excursion
2080 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
157521c5
GM
2081 (goto-char (point-min))
2082 (forward-line 1)
2083 ;; Change 1 to 0.
2084 (delete-char 1)
2085 (insert ?0)
2086 ;; Insert new EOOH line at the proper place.
2087 (forward-line 1)
2088 (let ((case-fold-search t))
2089 (while (looking-at "Summary-Line:\\|Mail-From:")
2090 (forward-line 1)))
2091 (insert "*** EOOH ***\n")
2092 (setq new-start (point))
2093 ;; Delete the old reformatted header.
2094 (forward-char -1)
2095 (search-forward "\n*** EOOH ***\n")
2096 (forward-line -1)
2097 (let ((start (point)))
2098 (search-forward "\n\n")
ed104a87
RS
2099 (if (and (<= start old-point)
2100 (<= old-point (point)))
2101 (setq new-point new-start))
157521c5
GM
2102 (delete-region start (point)))
2103 ;; Narrow to after the new EOOH line.
ed104a87
RS
2104 (narrow-to-region new-start (point-max)))
2105 (if new-point
2106 (goto-char new-point))))
157521c5
GM
2107
2108(defun rmail-msg-prune-header ()
ed104a87
RS
2109 (let ((new-point
2110 (= (point) (point-min))))
2111 (save-excursion
2112 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2113 (rmail-reformat-message (point-min) (point-max)))
2114 (if new-point
2115 (goto-char (point-min)))))
157521c5 2116
dbf71ee9
RS
2117(defun rmail-toggle-header (&optional arg)
2118 "Show original message header if pruned header currently shown, or vice versa.
2119With argument ARG, show the message header pruned if ARG is greater than zero;
2120otherwise, show it in full."
2121 (interactive "P")
157521c5
GM
2122 (let* ((pruned (with-current-buffer rmail-buffer
2123 (rmail-msg-is-pruned)))
6a91b84a
KH
2124 (prune (if arg
2125 (> (prefix-numeric-value arg) 0)
2126 (not pruned))))
2127 (if (eq pruned prune)
2128 t
157521c5 2129 (set-buffer rmail-buffer)
6a91b84a 2130 (rmail-maybe-set-message-counters)
157521c5
GM
2131 (if rmail-enable-mime
2132 (let ((buffer-read-only nil))
2133 (if pruned
2134 (rmail-msg-restore-non-pruned-header)
2135 (rmail-msg-prune-header))
2136 (funcall rmail-show-mime-function))
2137 (let* ((buffer-read-only nil)
2138 (window (get-buffer-window (current-buffer)))
2139 (at-point-min (= (point) (point-min)))
2140 (all-headers-visible (= (window-start window) (point-min)))
2141 (on-header
2142 (save-excursion
2143 (and (not (search-backward "\n\n" nil t))
2144 (progn
2145 (end-of-line)
2146 (re-search-backward "^[-A-Za-z0-9]+:" nil t))
2147 (match-string 0))))
2148 (old-screen-line
2149 (rmail-count-screen-lines (window-start window) (point))))
f4b5ab4c 2150 (if pruned
157521c5
GM
2151 (rmail-msg-restore-non-pruned-header)
2152 (rmail-msg-prune-header))
2153 (cond (at-point-min
2154 (goto-char (point-min)))
2155 (on-header
2156 (goto-char (point-min))
2157 (search-forward "\n\n")
2158 (or (re-search-backward
2159 (concat "^" (regexp-quote on-header)) nil t)
2160 (goto-char (point-min))))
2161 (t
2162 (save-selected-window
2163 (select-window window)
2164 (recenter old-screen-line)
2165 (if (and all-headers-visible
2166 (not (= (window-start) (point-min))))
9d43f6f1 2167 (recenter (- (window-height) 2))))))))
6a91b84a
KH
2168 (rmail-highlight-headers))))
2169
ed104a87
RS
2170(defun rmail-narrow-to-non-pruned-header ()
2171 "Narrow to the whole (original) header of the current message."
2172 (let (start end)
2173 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2174 (goto-char (point-min))
2175 (forward-line 1)
2176 (if (= (following-char) ?1)
2177 (progn
2178 (forward-line 1)
2179 (setq start (point))
2180 (search-forward "*** EOOH ***\n")
2181 (setq end (match-beginning 0)))
2182 (forward-line 2)
2183 (setq start (point))
2184 (search-forward "\n\n")
2185 (setq end (1- (point))))
2186 (narrow-to-region start end)
2187 (goto-char start)))
2188
6a91b84a
KH
2189;; Lifted from repos-count-screen-lines.
2190;; Return number of screen lines between START and END.
2191(defun rmail-count-screen-lines (start end)
72f69d0f 2192 (save-excursion
6a91b84a
KH
2193 (save-restriction
2194 (narrow-to-region start end)
2195 (goto-char (point-min))
2196 (vertical-motion (- (point-max) (point-min))))))
581d7e0b
RM
2197\f
2198;;;; *** Rmail Attributes and Keywords ***
2199
2200;; Make a string describing current message's attributes and keywords
2201;; and set it up as the name of a minor mode
2202;; so it will appear in the mode line.
2203(defun rmail-display-labels ()
2204 (let ((blurb "") (beg (point-min-marker)) (end (point-max-marker)))
2205 (save-excursion
2206 (unwind-protect
2207 (progn
2208 (widen)
2209 (goto-char (rmail-msgbeg rmail-current-message))
2210 (forward-line 1)
2211 (if (looking-at "[01],")
2212 (progn
2213 (narrow-to-region (point) (progn (end-of-line) (point)))
2214 ;; Truly valid BABYL format requires a space before each
2215 ;; attribute or keyword name. Put them in if missing.
2216 (let (buffer-read-only)
2217 (goto-char (point-min))
2218 (while (search-forward "," nil t)
2219 (or (looking-at "[ ,]") (eobp)
2220 (insert " "))))
2221 (goto-char (point-max))
2222 (if (search-backward ",," nil 'move)
2223 (progn
2224 (if (> (point) (1+ (point-min)))
2225 (setq blurb (buffer-substring (+ 1 (point-min)) (point))))
2226 (if (> (- (point-max) (point)) 2)
2227 (setq blurb
2228 (concat blurb
2229 ";"
2230 (buffer-substring (+ (point) 3)
2231 (1- (point-max)))))))))))
2232 ;; Note: we don't use save-restriction because that does not work right
2233 ;; if changes are made outside the saved restriction
2234 ;; before that restriction is restored.
2235 (narrow-to-region beg end)
2236 (set-marker beg nil)
2237 (set-marker end nil)))
2238 (while (string-match " +," blurb)
2239 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
2240 (substring blurb (match-end 0)))))
2241 (while (string-match ", +" blurb)
2242 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
2243 (substring blurb (match-end 0)))))
2244 (setq mode-line-process
8095bf23 2245 (format " %d/%d%s"
6b59a5fc
GM
2246 rmail-current-message rmail-total-messages blurb))
2247 ;; If rmail-enable-mime is non-nil, we may have to update
2248 ;; `mode-line-process' of rmail-view-buffer too.
2249 (if (and rmail-enable-mime
2250 (not (eq (current-buffer) rmail-view-buffer))
2251 (buffer-live-p rmail-view-buffer))
2252 (let ((mlp mode-line-process))
2253 (with-current-buffer rmail-view-buffer
2254 (setq mode-line-process mlp))))))
581d7e0b
RM
2255
2256;; Turn an attribute of a message on or off according to STATE.
2257;; ATTR is the name of the attribute, as a string.
2258;; MSGNUM is message number to change; nil means current message.
2259(defun rmail-set-attribute (attr state &optional msgnum)
6b59a5fc 2260 (set-buffer rmail-buffer)
581d7e0b
RM
2261 (let ((omax (point-max-marker))
2262 (omin (point-min-marker))
2263 (buffer-read-only nil))
2264 (or msgnum (setq msgnum rmail-current-message))
708bfd4f
RS
2265 (if (> msgnum 0)
2266 (unwind-protect
2267 (save-excursion
2268 (widen)
2269 (goto-char (+ 3 (rmail-msgbeg msgnum)))
2270 (let ((curstate
2271 (not
2272 (null (search-backward (concat ", " attr ",")
2273 (prog1 (point) (end-of-line)) t)))))
2274 (or (eq curstate (not (not state)))
2275 (if curstate
2276 (delete-region (point) (1- (match-end 0)))
2277 (beginning-of-line)
2278 (forward-char 2)
2279 (insert " " attr ","))))
2280 (if (string= attr "deleted")
2281 (rmail-set-message-deleted-p msgnum state)))
2282 ;; Note: we don't use save-restriction because that does not work right
2283 ;; if changes are made outside the saved restriction
2284 ;; before that restriction is restored.
2285 (narrow-to-region omin omax)
2286 (set-marker omin nil)
2287 (set-marker omax nil)
2288 (if (= msgnum rmail-current-message)
2289 (rmail-display-labels))))))
581d7e0b
RM
2290
2291;; Return t if the attributes/keywords line of msg number MSG
2292;; contains a match for the regexp LABELS.
2293(defun rmail-message-labels-p (msg labels)
2294 (save-excursion
2295 (save-restriction
2296 (widen)
2297 (goto-char (rmail-msgbeg msg))
2298 (forward-char 3)
2299 (re-search-backward labels (prog1 (point) (end-of-line)) t))))
2300\f
2301;;;; *** Rmail Message Selection And Support ***
2302
2303(defun rmail-msgend (n)
2304 (marker-position (aref rmail-message-vector (1+ n))))
2305
2306(defun rmail-msgbeg (n)
2307 (marker-position (aref rmail-message-vector n)))
2308
2309(defun rmail-widen-to-current-msgbeg (function)
2310 "Call FUNCTION with point at start of internal data of current message.
2311Assumes that bounds were previously narrowed to display the message in Rmail.
2312The bounds are widened enough to move point where desired, then narrowed
2313again afterward.
2314
2315FUNCTION may not change the visible text of the message, but it may
2316change the invisible header text."
2317 (save-excursion
07ae0d66
GM
2318 (unwind-protect
2319 (progn
2320 (narrow-to-region (rmail-msgbeg rmail-current-message)
2321 (point-max))
2322 (goto-char (point-min))
2323 (funcall function))
581d7e0b
RM
2324 ;; Note: we don't use save-restriction because that does not work right
2325 ;; if changes are made outside the saved restriction
2326 ;; before that restriction is restored.
07ae0d66
GM
2327 (narrow-to-region (rmail-msgbeg rmail-current-message)
2328 (rmail-msgend rmail-current-message)))))
581d7e0b
RM
2329
2330(defun rmail-forget-messages ()
2331 (unwind-protect
2332 (if (vectorp rmail-message-vector)
2333 (let* ((i 0)
2334 (v rmail-message-vector)
2335 (n (length v)))
2336 (while (< i n)
2337 (move-marker (aref v i) nil)
2338 (setq i (1+ i)))))
2339 (setq rmail-message-vector nil)
0985b412 2340 (setq rmail-msgref-vector nil)
581d7e0b
RM
2341 (setq rmail-deleted-vector nil)))
2342
2343(defun rmail-maybe-set-message-counters ()
2344 (if (not (and rmail-deleted-vector
2345 rmail-message-vector
2346 rmail-current-message
2347 rmail-total-messages))
2348 (rmail-set-message-counters)))
2349
2350(defun rmail-count-new-messages (&optional nomsg)
2351 (let* ((case-fold-search nil)
2352 (total-messages 0)
2353 (messages-head nil)
2354 (deleted-head nil))
2355 (or nomsg (message "Counting new messages..."))
2356 (goto-char (point-max))
2357 ;; Put at the end of messages-head
2358 ;; the entry for message N+1, which marks
2359 ;; the end of message N. (N = number of messages).
2360 (search-backward "\n\^_")
2361 (forward-char 1)
2362 (setq messages-head (list (point-marker)))
2363 (rmail-set-message-counters-counter (point-min))
2364 (setq rmail-current-message (1+ rmail-total-messages))
2365 (setq rmail-total-messages
2366 (+ rmail-total-messages total-messages))
2367 (setq rmail-message-vector
2368 (vconcat rmail-message-vector (cdr messages-head)))
2369 (aset rmail-message-vector
2370 rmail-current-message (car messages-head))
2371 (setq rmail-deleted-vector
2372 (concat rmail-deleted-vector deleted-head))
2373 (setq rmail-summary-vector
2374 (vconcat rmail-summary-vector (make-vector total-messages nil)))
0985b412
RS
2375 (setq rmail-msgref-vector
2376 (vconcat rmail-msgref-vector (make-vector total-messages nil)))
2377 ;; Fill in the new elements of rmail-msgref-vector.
79d8d8d5 2378 (let ((i (1+ (- rmail-total-messages total-messages))))
0985b412
RS
2379 (while (<= i rmail-total-messages)
2380 (aset rmail-msgref-vector i (list i))
2381 (setq i (1+ i))))
581d7e0b
RM
2382 (goto-char (point-min))
2383 (or nomsg (message "Counting new messages...done (%d)" total-messages))))
2384
2385(defun rmail-set-message-counters ()
2386 (rmail-forget-messages)
2387 (save-excursion
2388 (save-restriction
2389 (widen)
2390 (let* ((point-save (point))
2391 (total-messages 0)
2392 (messages-after-point)
2393 (case-fold-search nil)
2394 (messages-head nil)
2395 (deleted-head nil))
2396 (message "Counting messages...")
2397 (goto-char (point-max))
2398 ;; Put at the end of messages-head
2399 ;; the entry for message N+1, which marks
2400 ;; the end of message N. (N = number of messages).
cf805d5d
RS
2401 (search-backward "\n\^_" nil t)
2402 (if (/= (point) (point-max)) (forward-char 1))
581d7e0b
RM
2403 (setq messages-head (list (point-marker)))
2404 (rmail-set-message-counters-counter (min (point) point-save))
2405 (setq messages-after-point total-messages)
2406 (rmail-set-message-counters-counter)
2407 (setq rmail-total-messages total-messages)
2408 (setq rmail-current-message
2409 (min total-messages
2410 (max 1 (- total-messages messages-after-point))))
2411 (setq rmail-message-vector
2412 (apply 'vector (cons (point-min-marker) messages-head))
2c0b1898 2413 rmail-deleted-vector (concat "0" deleted-head)
0985b412
RS
2414 rmail-summary-vector (make-vector rmail-total-messages nil)
2415 rmail-msgref-vector (make-vector (1+ rmail-total-messages) nil))
2416 (let ((i 0))
2417 (while (<= i rmail-total-messages)
2418 (aset rmail-msgref-vector i (list i))
2419 (setq i (1+ i))))
581d7e0b 2420 (message "Counting messages...done")))))
67f9d50e 2421
581d7e0b 2422(defun rmail-set-message-counters-counter (&optional stop)
582e5ff7
AI
2423 (let ((start (point))
2424 next)
2425 (while (search-backward "\n\^_\^L" stop t)
2426 ;; Detect messages that have been added with DOS line endings and
2427 ;; convert the line endings for such messages.
2428 (setq next (point))
2429 (if (looking-at "\n\^_\^L\r\n")
2430 (let ((buffer-read-only nil)
2431 (buffer-undo t))
2432 (message "Counting messages...(converting line endings)")
2433 (save-excursion
2434 (goto-char start)
2435 (while (search-backward "\r\n" next t)
2436 (delete-char 1)))))
2437 (setq start next)
2438 (forward-char 1)
2439 (setq messages-head (cons (point-marker) messages-head))
2440 (save-excursion
2441 (setq deleted-head
2442 (cons (if (search-backward ", deleted,"
2443 (prog1 (point)
2444 (forward-line 2))
2445 t)
2446 ?D ?\ )
2447 deleted-head)))
2448 (if (zerop (% (setq total-messages (1+ total-messages)) 20))
2449 (message "Counting messages...%d" total-messages)))))
581d7e0b
RM
2450
2451(defun rmail-beginning-of-message ()
2452 "Show current message starting from the beginning."
2453 (interactive)
2454 (rmail-show-message rmail-current-message))
2455
bdf03600 2456(defun rmail-show-message (&optional n no-summary)
bd1f0f84
RS
2457 "Show message number N (prefix argument), counting from start of file.
2458If summary buffer is currently displayed, update current message there also."
581d7e0b 2459 (interactive "p")
d1e69dec
KH
2460 (or (eq major-mode 'rmail-mode)
2461 (switch-to-buffer rmail-buffer))
581d7e0b
RM
2462 (rmail-maybe-set-message-counters)
2463 (widen)
2464 (if (zerop rmail-total-messages)
2465 (progn (narrow-to-region (point-min) (1- (point-max)))
2466 (goto-char (point-min))
2467 (setq mode-line-process nil))
578b6415 2468 (let (blurb coding-system)
581d7e0b
RM
2469 (if (not n)
2470 (setq n rmail-current-message)
2471 (cond ((<= n 0)
2472 (setq n 1
2473 rmail-current-message 1
2474 blurb "No previous message"))
2475 ((> n rmail-total-messages)
2476 (setq n rmail-total-messages
2477 rmail-current-message rmail-total-messages
2478 blurb "No following message"))
2479 (t
2480 (setq rmail-current-message n))))
563ab60d 2481 (let ((beg (rmail-msgbeg n)))
581d7e0b
RM
2482 (goto-char beg)
2483 (forward-line 1)
578b6415
RS
2484 (save-excursion
2485 (let ((end (rmail-msgend n)))
2486 (save-restriction
2487 (if (prog1 (= (following-char) ?0)
2488 (forward-line 2)
2751e20b
RS
2489 ;; If there's a Summary-line in the (otherwise empty)
2490 ;; header, we didn't yet get past the EOOH line.
2491 (if (looking-at "^\\*\\*\\* EOOH \\*\\*\\*\n")
2492 (forward-line 1))
578b6415
RS
2493 (narrow-to-region (point) end))
2494 (rfc822-goto-eoh)
2495 (search-forward "\n*** EOOH ***\n" end t))
2496 (narrow-to-region beg (point))
2497 (goto-char (point-min))
2498 (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t)
2499 (let ((coding-system (intern (match-string 1))))
de971ad1
RS
2500 (condition-case nil
2501 (progn
2502 (check-coding-system coding-system)
2503 (setq buffer-file-coding-system coding-system))
3e0b7b44 2504 (error
de971ad1 2505 (setq buffer-file-coding-system nil))))
578b6415 2506 (setq buffer-file-coding-system nil)))))
563ab60d
RS
2507 ;; Clear the "unseen" attribute when we show a message.
2508 (rmail-set-attribute "unseen" nil)
563ab60d 2509 (let ((end (rmail-msgend n)))
578b6415 2510 ;; Reformat the header, or else find the reformatted header.
563ab60d 2511 (if (= (following-char) ?0)
581d7e0b 2512 (rmail-reformat-message beg end)
563ab60d
RS
2513 (search-forward "\n*** EOOH ***\n" end t)
2514 (narrow-to-region (point) end)))
581d7e0b 2515 (goto-char (point-min))
6f19114e
GM
2516 (walk-windows
2517 (function (lambda (window)
2518 (if (eq (window-buffer window) (current-buffer))
2519 (set-window-point window (point)))))
2520 nil t)
581d7e0b 2521 (rmail-display-labels)
d1e69dec
KH
2522 (if (eq rmail-enable-mime t)
2523 (funcall rmail-show-mime-function)
2524 (setq rmail-view-buffer rmail-buffer)
2525 )
99887e16 2526 (rmail-highlight-headers)
d43277ab 2527 (if transient-mark-mode (deactivate-mark))
581d7e0b 2528 (run-hooks 'rmail-show-message-hook)
bd1f0f84
RS
2529 ;; If there is a summary buffer, try to move to this message
2530 ;; in that buffer. But don't complain if this message
2531 ;; is not mentioned in the summary.
bdf03600
RS
2532 ;; Don't do this at all if we were called on behalf
2533 ;; of cursor motion in the summary buffer.
2534 (and (rmail-summary-exists) (not no-summary)
2535 (let ((curr-msg rmail-current-message))
2536 (rmail-select-summary
2537 (rmail-summary-goto-msg curr-msg t t))))
6b59a5fc
GM
2538 (with-current-buffer rmail-buffer
2539 (rmail-auto-file))
581d7e0b
RM
2540 (if blurb
2541 (message blurb))))))
2542
fd4976b8
EZ
2543(defun rmail-redecode-body (coding)
2544 "Decode the body of the current message using coding system CODING.
2545This is useful with mail messages that have malformed or missing
2546charset= headers.
2547
2548This function assumes that the current message is already decoded
2549and displayed in the RMAIL buffer, but the coding system used to
2550decode it was incorrect. It then encodes the message back to its
4685e6b7 2551original form, and decodes it again, using the coding system CODING.
fd4976b8
EZ
2552
2553Note that if Emacs erroneously auto-detected one of the iso-2022
2554encodings in the message, this function might fail because the escape
2555sequences that switch between character sets and also single-shift and
2556locking-shift codes are impossible to recover. This function is meant
2557to be used to fix messages encoded with 8-bit encodings, such as
2558iso-8859, koi8-r, etc."
2559 (interactive "zCoding system for re-decoding this message: ")
2560 (when (not rmail-enable-mime)
2561 (or (eq major-mode 'rmail-mode)
2562 (switch-to-buffer rmail-buffer))
2563 (save-excursion
d54f26b1
EZ
2564 (let ((pruned (rmail-msg-is-pruned)))
2565 (unwind-protect
2566 (let ((msgbeg (rmail-msgbeg rmail-current-message))
2567 (msgend (rmail-msgend rmail-current-message))
2568 x-coding-header)
2569 ;; We need the message headers pruned (we later restore
2570 ;; the pruned stat to what it was, see the end of
2571 ;; unwind-protect form).
2572 (or pruned
2573 (rmail-toggle-header 1))
2574 (narrow-to-region msgbeg msgend)
2575 (goto-char (point-min))
2576 (when (search-forward "\n*** EOOH ***\n" (point-max) t)
2577 (narrow-to-region msgbeg (point)))
2578 (goto-char (point-min))
2579 (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t)
2580 (let ((old-coding (intern (match-string 1)))
2581 (buffer-read-only nil))
2582 (check-coding-system old-coding)
2583 ;; Make sure the new coding system uses the same EOL
2584 ;; conversion, to prevent ^M characters from popping
2585 ;; up all over the place.
2586 (setq coding
2587 (coding-system-change-eol-conversion
2588 coding
2589 (coding-system-eol-type old-coding)))
2590 (setq x-coding-header (point-marker))
2591 (narrow-to-region msgbeg msgend)
2592 (encode-coding-region (point) msgend old-coding)
2593 (decode-coding-region (point) msgend coding)
2594 (setq last-coding-system-used coding)
2595 ;; Rewrite the coding-system header according
2596 ;; to what we did.
2597 (goto-char x-coding-header)
2598 (delete-region (point)
2599 (save-excursion
2600 (beginning-of-line)
2601 (point)))
2602 (insert "X-Coding-System: "
2603 (symbol-name last-coding-system-used))
2604 (set-marker x-coding-header nil)
2605 (rmail-show-message))
2606 (error "No X-Coding-System header found")))
2607 (or pruned
2608 (rmail-toggle-header 0)))))))
fd4976b8 2609
99887e16
KH
2610;; Find all occurrences of certain fields, and highlight them.
2611(defun rmail-highlight-headers ()
2612 ;; Do this only if the system supports faces.
f9a9d26e
RS
2613 (if (and (fboundp 'internal-find-face)
2614 rmail-highlighted-headers)
99887e16
KH
2615 (save-excursion
2616 (search-forward "\n\n" nil 'move)
2617 (save-restriction
2618 (narrow-to-region (point-min) (point))
2619 (let ((case-fold-search t)
2620 (inhibit-read-only t)
2621 ;; Highlight with boldface if that is available.
2622 ;; Otherwise use the `highlight' face.
3df6812a
RS
2623 (face (or rmail-highlight-face
2624 (if (face-differs-from-default-p 'bold)
2625 'bold 'highlight)))
99887e16
KH
2626 ;; List of overlays to reuse.
2627 (overlays rmail-overlay-list))
2628 (goto-char (point-min))
2629 (while (re-search-forward rmail-highlighted-headers nil t)
92e06883 2630 (skip-chars-forward " \t")
99887e16
KH
2631 (let ((beg (point))
2632 overlay)
2633 (while (progn (forward-line 1)
2634 (looking-at "[ \t]")))
2635 ;; Back up over newline, then trailing spaces or tabs
2636 (forward-char -1)
2637 (while (member (preceding-char) '(? ?\t))
2638 (forward-char -1))
2639 (if overlays
2640 ;; Reuse an overlay we already have.
2641 (progn
2642 (setq overlay (car overlays)
2643 overlays (cdr overlays))
2644 (overlay-put overlay 'face face)
2645 (move-overlay overlay beg (point)))
2646 ;; Make a new overlay and add it to
2647 ;; rmail-overlay-list.
2648 (setq overlay (make-overlay beg (point)))
2649 (overlay-put overlay 'face face)
2650 (setq rmail-overlay-list
2651 (cons overlay rmail-overlay-list))))))))))
2652
ab59163d
DL
2653(defun rmail-auto-file ()
2654 "Automatically move a message into a sub-folder based on criteria.
2655Called when a new message is displayed."
2656 (if (or (rmail-message-labels-p rmail-current-message "filed")
2657 (not (string= (buffer-file-name)
2658 (expand-file-name rmail-file-name))))
2659 ;; Do nothing if it's already been filed.
2660 nil
2661 ;; Find out some basics (common fields)
2662 (let ((from (mail-fetch-field "from"))
2663 (subj (mail-fetch-field "subject"))
2664 (to (concat (mail-fetch-field "to") "," (mail-fetch-field "cc")))
2665 (d rmail-automatic-folder-directives)
2666 (directive-loop nil)
2667 (folder nil))
2668 (while d
2669 (setq folder (car (car d))
2670 directive-loop (cdr (car d)))
2671 (while (and (car directive-loop)
2672 (let ((f (cond
2673 ((string= (car directive-loop) "from") from)
2674 ((string= (car directive-loop) "to") to)
2675 ((string= (car directive-loop) "subject") subj)
2676 (t (mail-fetch-field (car directive-loop))))))
2677 (and f (string-match (car (cdr directive-loop)) f))))
2678 (setq directive-loop (cdr (cdr directive-loop))))
2679 ;; If there are no directives left, then it was a complete match.
2680 (if (null directive-loop)
2681 (if (null folder)
2682 (rmail-delete-forward)
2683 (if (string= "/dev/null" folder)
2684 (rmail-delete-message)
2685 (rmail-output-to-rmail-file folder 1 t)
2686 (setq d nil))))
2687 (setq d (cdr d))))))
2688
581d7e0b
RM
2689(defun rmail-next-message (n)
2690 "Show following message whether deleted or not.
2691With prefix arg N, moves forward N messages, or backward if N is negative."
2692 (interactive "p")
6b59a5fc 2693 (set-buffer rmail-buffer)
581d7e0b
RM
2694 (rmail-maybe-set-message-counters)
2695 (rmail-show-message (+ rmail-current-message n)))
2696
2697(defun rmail-previous-message (n)
2698 "Show previous message whether deleted or not.
2699With prefix arg N, moves backward N messages, or forward if N is negative."
2700 (interactive "p")
67f9d50e 2701 (rmail-next-message (- n)))
581d7e0b
RM
2702
2703(defun rmail-next-undeleted-message (n)
2704 "Show following non-deleted message.
2705With prefix arg N, moves forward N non-deleted messages,
c42722e3
RS
2706or backward if N is negative.
2707
2708Returns t if a new message is being shown, nil otherwise."
581d7e0b 2709 (interactive "p")
6b59a5fc 2710 (set-buffer rmail-buffer)
581d7e0b
RM
2711 (rmail-maybe-set-message-counters)
2712 (let ((lastwin rmail-current-message)
2713 (current rmail-current-message))
2714 (while (and (> n 0) (< current rmail-total-messages))
2715 (setq current (1+ current))
2716 (if (not (rmail-message-deleted-p current))
2717 (setq lastwin current n (1- n))))
2718 (while (and (< n 0) (> current 1))
2719 (setq current (1- current))
2720 (if (not (rmail-message-deleted-p current))
2721 (setq lastwin current n (1+ n))))
2722 (if (/= lastwin rmail-current-message)
c42722e3
RS
2723 (progn (rmail-show-message lastwin)
2724 t)
2725 (if (< n 0)
2726 (message "No previous nondeleted message"))
2727 (if (> n 0)
2728 (message "No following nondeleted message"))
2729 nil)))
581d7e0b
RM
2730
2731(defun rmail-previous-undeleted-message (n)
2732 "Show previous non-deleted message.
2733With prefix argument N, moves backward N non-deleted messages,
2734or forward if N is negative."
2735 (interactive "p")
2736 (rmail-next-undeleted-message (- n)))
2737
2738(defun rmail-first-message ()
2739 "Show first message in file."
2740 (interactive)
2741 (rmail-maybe-set-message-counters)
2742 (rmail-show-message 1))
2743
2744(defun rmail-last-message ()
2745 "Show last message in file."
2746 (interactive)
2747 (rmail-maybe-set-message-counters)
2748 (rmail-show-message rmail-total-messages))
2749
2750(defun rmail-what-message ()
2751 (let ((where (point))
2752 (low 1)
2753 (high rmail-total-messages)
2754 (mid (/ rmail-total-messages 2)))
2755 (while (> (- high low) 1)
2756 (if (>= where (rmail-msgbeg mid))
2757 (setq low mid)
2758 (setq high mid))
2759 (setq mid (+ low (/ (- high low) 2))))
2760 (if (>= where (rmail-msgbeg high)) high low)))
2761
bd1f0f84
RS
2762(defun rmail-message-recipients-p (msg recipients &optional primary-only)
2763 (save-restriction
2764 (goto-char (rmail-msgbeg msg))
b4b87b68 2765 (search-forward "\n*** EOOH ***\n")
bd1f0f84
RS
2766 (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
2767 (or (string-match recipients (or (mail-fetch-field "To") ""))
2768 (string-match recipients (or (mail-fetch-field "From") ""))
2769 (if (not primary-only)
2770 (string-match recipients (or (mail-fetch-field "Cc") ""))))))
2771
7cff9c6f
GM
2772(defun rmail-message-regexp-p (n regexp)
2773 "Return t, if for message number N, regexp REGEXP matches in the header."
2774 (let ((beg (rmail-msgbeg n))
2775 (end (rmail-msgend n)))
2776 (goto-char beg)
2777 (forward-line 1)
2778 (save-excursion
2779 (save-restriction
2780 (if (prog1 (= (following-char) ?0)
2781 (forward-line 2)
2782 ;; If there's a Summary-line in the (otherwise empty)
2783 ;; header, we didn't yet get past the EOOH line.
50df7214
GM
2784 (when (looking-at "^\\*\\*\\* EOOH \\*\\*\\*\n")
2785 (forward-line 1))
b2b8f8fa 2786 (setq beg (point))
7cff9c6f 2787 (narrow-to-region (point) end))
67f9d50e 2788 (progn
50df7214
GM
2789 (rfc822-goto-eoh)
2790 (setq end (point)))
b2b8f8fa 2791 (setq beg (point))
50df7214
GM
2792 (search-forward "\n*** EOOH ***\n" end t)
2793 (setq end (1+ (match-beginning 0)))))
b2b8f8fa 2794 (goto-char beg)
6b59a5fc
GM
2795 (if rmail-enable-mime
2796 (funcall rmail-search-mime-header-function n regexp end)
2797 (re-search-forward regexp end t)))))
2798
2799(defun rmail-search-message (msg regexp)
2800 "Return non-nil, if for message number MSG, regexp REGEXP matches."
2801 (goto-char (rmail-msgbeg msg))
2802 (if rmail-enable-mime
2803 (funcall rmail-search-mime-message-function msg regexp)
2804 (re-search-forward regexp (rmail-msgend msg) t)))
bd1f0f84 2805
581d7e0b 2806(defvar rmail-search-last-regexp nil)
e91f80c4 2807(defun rmail-search (regexp &optional n)
40e09f88 2808 "Show message containing next match for REGEXP (but not the current msg).
e91f80c4
RS
2809Prefix argument gives repeat count; negative argument means search
2810backwards (through earlier messages).
2811Interactively, empty argument means use same regexp used last time."
581d7e0b
RM
2812 (interactive
2813 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
2814 (prompt
2815 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
2816 regexp)
2817 (if rmail-search-last-regexp
2818 (setq prompt (concat prompt
2819 "(default "
2820 rmail-search-last-regexp
2821 ") ")))
2822 (setq regexp (read-string prompt))
2823 (cond ((not (equal regexp ""))
2824 (setq rmail-search-last-regexp regexp))
2825 ((not rmail-search-last-regexp)
2826 (error "No previous Rmail search string")))
e91f80c4
RS
2827 (list rmail-search-last-regexp
2828 (prefix-numeric-value current-prefix-arg))))
2829 (or n (setq n 1))
581d7e0b 2830 (message "%sRmail search for %s..."
df8a44dd 2831 (if (< n 0) "Reverse " "")
581d7e0b 2832 regexp)
6b59a5fc 2833 (set-buffer rmail-buffer)
581d7e0b
RM
2834 (rmail-maybe-set-message-counters)
2835 (let ((omin (point-min))
2836 (omax (point-max))
2837 (opoint (point))
2838 win
e91f80c4 2839 (reversep (< n 0))
581d7e0b
RM
2840 (msg rmail-current-message))
2841 (unwind-protect
2842 (progn
2843 (widen)
e91f80c4
RS
2844 (while (/= n 0)
2845 ;; Check messages one by one, advancing message number up or down
2846 ;; but searching forward through each message.
2847 (if reversep
2848 (while (and (null win) (> msg 1))
6b59a5fc
GM
2849 (setq msg (1- msg)
2850 win (rmail-search-message msg regexp)))
e91f80c4 2851 (while (and (null win) (< msg rmail-total-messages))
6b59a5fc
GM
2852 (setq msg (1+ msg)
2853 win (rmail-search-message msg regexp))))
df8a44dd 2854 (setq n (+ n (if reversep 1 -1)))))
581d7e0b
RM
2855 (if win
2856 (progn
6b59a5fc
GM
2857 (rmail-show-message msg)
2858 ;; Search forward (if this is a normal search) or backward
2859 ;; (if this is a reverse search) through this message to
2860 ;; position point. This search may fail because REGEXP
2861 ;; was found in the hidden portion of this message. In
2862 ;; that case, move point to the beginning of visible
2863 ;; portion.
581d7e0b
RM
2864 (if reversep
2865 (progn
6b59a5fc
GM
2866 (goto-char (point-max))
2867 (re-search-backward regexp nil 'move))
2868 (goto-char (point-min))
2869 (re-search-forward regexp nil t))
581d7e0b
RM
2870 (message "%sRmail search for %s...done"
2871 (if reversep "Reverse " "")
6b59a5fc 2872 regexp))
581d7e0b
RM
2873 (goto-char opoint)
2874 (narrow-to-region omin omax)
2875 (ding)
2876 (message "Search failed: %s" regexp)))))
2877
e91f80c4
RS
2878(defun rmail-search-backwards (regexp &optional n)
2879 "Show message containing previous match for REGEXP.
2880Prefix argument gives repeat count; negative argument means search
2881forward (through later messages).
2882Interactively, empty argument means use same regexp used last time."
2883 (interactive
df8a44dd 2884 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
e91f80c4
RS
2885 (prompt
2886 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
2887 regexp)
2888 (if rmail-search-last-regexp
2889 (setq prompt (concat prompt
2890 "(default "
2891 rmail-search-last-regexp
2892 ") ")))
2893 (setq regexp (read-string prompt))
2894 (cond ((not (equal regexp ""))
2895 (setq rmail-search-last-regexp regexp))
2896 ((not rmail-search-last-regexp)
2897 (error "No previous Rmail search string")))
2898 (list rmail-search-last-regexp
2899 (prefix-numeric-value current-prefix-arg))))
68d30f2b 2900 (rmail-search regexp (- (or n 1))))
e91f80c4 2901
581d7e0b
RM
2902;; Show the first message which has the `unseen' attribute.
2903(defun rmail-first-unseen-message ()
117f4b92 2904 (rmail-maybe-set-message-counters)
581d7e0b
RM
2905 (let ((current 1)
2906 found)
2907 (save-restriction
2908 (widen)
bb9dcce1 2909 (while (and (not found) (<= current rmail-total-messages))
581d7e0b 2910 (if (rmail-message-labels-p current ", ?\\(unseen\\),")
462c1094
RM
2911 (setq found current))
2912 (setq current (1+ current))))
e9c735fa
JA
2913;; Let the caller show the message.
2914;; (if found
2915;; (rmail-show-message found))
2916 found))
60fb2b34
RS
2917
2918(defun rmail-next-same-subject (n)
2919 "Go to the next mail message having the same subject header.
2920With prefix argument N, do this N times.
2921If N is negative, go backwards instead."
2922 (interactive "p")
e8417bb0
KH
2923 (let ((subject (mail-fetch-field "Subject"))
2924 (forward (> n 0))
2925 (i rmail-current-message)
a21b845b 2926 (case-fold-search t)
e8417bb0 2927 search-regexp found)
df211784
RS
2928 (if (string-match "\\`[ \t]+" subject)
2929 (setq subject (substring subject (match-end 0))))
60fb2b34
RS
2930 (if (string-match "Re:[ \t]*" subject)
2931 (setq subject (substring subject (match-end 0))))
df211784
RS
2932 (if (string-match "[ \t]+\\'" subject)
2933 (setq subject (substring subject 0 (match-beginning 0))))
805297bb 2934 (setq search-regexp (concat "^Subject: *\\(Re:[ \t]*\\)?"
e8417bb0 2935 (regexp-quote subject)
805297bb 2936 "[ \t]*\n"))
60fb2b34
RS
2937 (save-excursion
2938 (save-restriction
2939 (widen)
2940 (while (and (/= n 0)
2941 (if forward
2942 (< i rmail-total-messages)
2943 (> i 1)))
2944 (let (done)
2945 (while (and (not done)
2946 (if forward
2947 (< i rmail-total-messages)
2948 (> i 1)))
2949 (setq i (if forward (1+ i) (1- i)))
2950 (goto-char (rmail-msgbeg i))
2951 (search-forward "\n*** EOOH ***\n")
2952 (let ((beg (point)) end)
2953 (search-forward "\n\n")
2954 (setq end (point))
2955 (goto-char beg)
2956 (setq done (re-search-forward search-regexp end t))))
2957 (if done (setq found i)))
2958 (setq n (if forward (1- n) (1+ n))))))
2959 (if found
2960 (rmail-show-message found)
2961 (error "No %s message with same subject"
2962 (if forward "following" "previous")))))
2963
2964(defun rmail-previous-same-subject (n)
2965 "Go to the previous mail message having the same subject header.
2966With prefix argument N, do this N times.
2967If N is negative, go forwards instead."
2968 (interactive "p")
2969 (rmail-next-same-subject (- n)))
581d7e0b
RM
2970\f
2971;;;; *** Rmail Message Deletion Commands ***
2972
2973(defun rmail-message-deleted-p (n)
2974 (= (aref rmail-deleted-vector n) ?D))
2975
2976(defun rmail-set-message-deleted-p (n state)
2977 (aset rmail-deleted-vector n (if state ?D ?\ )))
2978
2979(defun rmail-delete-message ()
2980 "Delete this message and stay on it."
2981 (interactive)
6c714afe
RS
2982 (rmail-set-attribute "deleted" t)
2983 (run-hooks 'rmail-delete-message-hook))
581d7e0b
RM
2984
2985(defun rmail-undelete-previous-message ()
2986 "Back up to deleted message, select it, and undelete it."
2987 (interactive)
6b59a5fc 2988 (set-buffer rmail-buffer)
581d7e0b
RM
2989 (let ((msg rmail-current-message))
2990 (while (and (> msg 0)
2991 (not (rmail-message-deleted-p msg)))
2992 (setq msg (1- msg)))
2993 (if (= msg 0)
2994 (error "No previous deleted message")
2995 (if (/= msg rmail-current-message)
2996 (rmail-show-message msg))
bd1f0f84
RS
2997 (rmail-set-attribute "deleted" nil)
2998 (if (rmail-summary-exists)
2999 (save-excursion
3000 (set-buffer rmail-summary-buffer)
3001 (rmail-summary-mark-undeleted msg)))
3002 (rmail-maybe-display-summary))))
581d7e0b
RM
3003
3004(defun rmail-delete-forward (&optional backward)
3005 "Delete this message and move to next nondeleted one.
3006Deleted messages stay in the file until the \\[rmail-expunge] command is given.
c42722e3
RS
3007With prefix argument, delete and move backward.
3008
3009Returns t if a new message is displayed after the delete, or nil otherwise."
581d7e0b
RM
3010 (interactive "P")
3011 (rmail-set-attribute "deleted" t)
6c714afe 3012 (run-hooks 'rmail-delete-message-hook)
bd1f0f84
RS
3013 (let ((del-msg rmail-current-message))
3014 (if (rmail-summary-exists)
4edaa169
RS
3015 (rmail-select-summary
3016 (rmail-summary-mark-deleted del-msg)))
c42722e3
RS
3017 (prog1 (rmail-next-undeleted-message (if backward -1 1))
3018 (rmail-maybe-display-summary))))
581d7e0b
RM
3019
3020(defun rmail-delete-backward ()
3021 "Delete this message and move to previous nondeleted one.
3022Deleted messages stay in the file until the \\[rmail-expunge] command is given."
3023 (interactive)
3024 (rmail-delete-forward t))
3025
1825bea1
RS
3026;; Compute the message number a given message would have after expunging.
3027;; The present number of the message is OLDNUM.
3028;; DELETEDVEC should be rmail-deleted-vector.
3029;; The value is nil for a message that would be deleted.
3030(defun rmail-msg-number-after-expunge (deletedvec oldnum)
3031 (if (or (null oldnum) (= (aref deletedvec oldnum) ?D))
3032 nil
3033 (let ((i 0)
3034 (newnum 0))
3035 (while (< i oldnum)
3036 (if (/= (aref deletedvec i) ?D)
3037 (setq newnum (1+ newnum)))
3038 (setq i (1+ i)))
3039 newnum)))
3040
e6be618b
GM
3041(defun rmail-expunge-confirmed ()
3042 "Return t if deleted message should be expunged. If necessary, ask the user.
3043See also user-option `rmail-confirm-expunge'."
6b59a5fc 3044 (set-buffer rmail-buffer)
e6be618b
GM
3045 (or (not (stringp rmail-deleted-vector))
3046 (not (string-match "D" rmail-deleted-vector))
3047 (null rmail-confirm-expunge)
3048 (funcall rmail-confirm-expunge
3049 "Erase deleted messages from Rmail file? ")))
3050
a5369237 3051(defun rmail-only-expunge (&optional dont-show)
581d7e0b
RM
3052 "Actually erase all deleted messages in the file."
3053 (interactive)
6b59a5fc 3054 (set-buffer rmail-buffer)
581d7e0b
RM
3055 (message "Expunging deleted messages...")
3056 ;; Discard all undo records for this buffer.
3057 (or (eq buffer-undo-list t)
3058 (setq buffer-undo-list nil))
3059 (rmail-maybe-set-message-counters)
3060 (let* ((omax (- (buffer-size) (point-max)))
3061 (omin (- (buffer-size) (point-min)))
3062 (opoint (if (and (> rmail-current-message 0)
881fd7eb
KH
3063 (rmail-message-deleted-p rmail-current-message))
3064 0
6b59a5fc
GM
3065 (if rmail-enable-mime
3066 (with-current-buffer rmail-view-buffer
3067 (- (point)(point-min)))
3068 (- (point) (point-min)))))
581d7e0b
RM
3069 (messages-head (cons (aref rmail-message-vector 0) nil))
3070 (messages-tail messages-head)
3071 ;; Don't make any undo records for the expunging.
3072 (buffer-undo-list t)
3073 (win))
3074 (unwind-protect
3075 (save-excursion
3076 (widen)
3077 (goto-char (point-min))
3078 (let ((counter 0)
3079 (number 1)
3080 (total rmail-total-messages)
3081 (new-message-number rmail-current-message)
3082 (new-summary nil)
0985b412 3083 (new-msgref (list (list 0)))
1825bea1 3084 (rmailbuf (current-buffer))
581d7e0b
RM
3085 (buffer-read-only nil)
3086 (messages rmail-message-vector)
3087 (deleted rmail-deleted-vector)
3088 (summary rmail-summary-vector))
3089 (setq rmail-total-messages nil
3090 rmail-current-message nil
3091 rmail-message-vector nil
3092 rmail-deleted-vector nil
3093 rmail-summary-vector nil)
1825bea1 3094
581d7e0b
RM
3095 (while (<= number total)
3096 (if (= (aref deleted number) ?D)
3097 (progn
3098 (delete-region
3099 (marker-position (aref messages number))
3100 (marker-position (aref messages (1+ number))))
3101 (move-marker (aref messages number) nil)
3102 (if (> new-message-number counter)
3103 (setq new-message-number (1- new-message-number))))
3104 (setq counter (1+ counter))
3105 (setq messages-tail
3106 (setcdr messages-tail
3107 (cons (aref messages number) nil)))
3108 (setq new-summary
3109 (cons (if (= counter number) (aref summary (1- number)))
0985b412
RS
3110 new-summary))
3111 (setq new-msgref
3112 (cons (aref rmail-msgref-vector number)
3113 new-msgref))
3114 (setcar (car new-msgref) counter))
581d7e0b
RM
3115 (if (zerop (% (setq number (1+ number)) 20))
3116 (message "Expunging deleted messages...%d" number)))
3117 (setq messages-tail
3118 (setcdr messages-tail
3119 (cons (aref messages number) nil)))
3120 (setq rmail-current-message new-message-number
3121 rmail-total-messages counter
3122 rmail-message-vector (apply 'vector messages-head)
3123 rmail-deleted-vector (make-string (1+ counter) ?\ )
3124 rmail-summary-vector (vconcat (nreverse new-summary))
0985b412 3125 rmail-msgref-vector (apply 'vector (nreverse new-msgref))
581d7e0b
RM
3126 win t)))
3127 (message "Expunging deleted messages...done")
3128 (if (not win)
3129 (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
a5369237
RS
3130 (if (not dont-show)
3131 (rmail-show-message
3132 (if (zerop rmail-current-message) 1 nil)
3133 (if rmail-enable-mime
3134 (goto-char (+ (point-min) opoint))
3135 (goto-char (+ (point) opoint))))))))
bd1f0f84
RS
3136
3137(defun rmail-expunge ()
3138 "Erase deleted messages from Rmail file and summary buffer."
3139 (interactive)
e6be618b 3140 (when (rmail-expunge-confirmed)
9f7c6da9
GM
3141 (rmail-only-expunge)
3142 (if (rmail-summary-exists)
e6be618b 3143 (rmail-select-summary (rmail-update-summary)))))
581d7e0b
RM
3144\f
3145;;;; *** Rmail Mailing Commands ***
3146
bde7cb3f
RS
3147(defun rmail-start-mail (&optional noerase to subject in-reply-to cc
3148 replybuffer sendactions same-window others)
3149 (let (yank-action)
3150 (if replybuffer
3151 (setq yank-action (list 'insert-buffer replybuffer)))
3152 (setq others (cons (cons "cc" cc) others))
3153 (setq others (cons (cons "in-reply-to" in-reply-to) others))
3154 (if same-window
3155 (compose-mail to subject others
3156 noerase nil
3157 yank-action sendactions)
7e2605e7 3158 (if (and (display-multi-frame-p) rmail-mail-new-frame)
bde7cb3f
RS
3159 (prog1
3160 (compose-mail to subject others
3161 noerase 'switch-to-buffer-other-frame
3162 yank-action sendactions)
3163 ;; This is not a standard frame parameter;
3164 ;; nothing except sendmail.el looks at it.
3165 (modify-frame-parameters (selected-frame)
3166 '((mail-dedicated-frame . t))))
3167 (compose-mail to subject others
3168 noerase 'switch-to-buffer-other-window
3169 yank-action sendactions)))))
94ed51e8 3170
581d7e0b 3171(defun rmail-mail ()
bd1f0f84
RS
3172 "Send mail in another window.
3173While composing the message, use \\[mail-yank-original] to yank the
3174original message into it."
581d7e0b 3175 (interactive)
d1e69dec 3176 (rmail-start-mail nil nil nil nil nil rmail-view-buffer))
581d7e0b
RM
3177
3178(defun rmail-continue ()
3179 "Continue composing outgoing message previously being composed."
3180 (interactive)
94ed51e8 3181 (rmail-start-mail t))
581d7e0b
RM
3182
3183(defun rmail-reply (just-sender)
3184 "Reply to the current message.
3185Normally include CC: to all other recipients of original message;
3186prefix argument means ignore them. While composing the reply,
3187use \\[mail-yank-original] to yank the original message into it."
3188 (interactive "P")
11aea8f5 3189 (let (from reply-to cc subject date to message-id references
037fa8ca 3190 resent-to resent-cc resent-reply-to
d1e69dec 3191 (msgnum rmail-current-message))
581d7e0b
RM
3192 (save-excursion
3193 (save-restriction
a2cc5c4f
GM
3194 (if rmail-enable-mime
3195 (narrow-to-region
3196 (goto-char (point-min))
3197 (if (search-forward "\n\n" nil 'move)
3198 (1+ (match-beginning 0))
3199 (point)))
6b59a5fc
GM
3200 (widen)
3201 (goto-char (rmail-msgbeg rmail-current-message))
3202 (forward-line 1)
3203 (if (= (following-char) ?0)
3204 (narrow-to-region
3205 (progn (forward-line 2)
3206 (point))
3207 (progn (search-forward "\n\n" (rmail-msgend rmail-current-message)
3208 'move)
3209 (point)))
3210 (narrow-to-region (point)
3211 (progn (search-forward "\n*** EOOH ***\n")
3212 (beginning-of-line) (point)))))
037fa8ca
RS
3213 (setq from (mail-fetch-field "from")
3214 reply-to (or (mail-fetch-field "reply-to" nil t)
581d7e0b 3215 from)
037fa8ca
RS
3216 cc (and (not just-sender)
3217 (mail-fetch-field "cc" nil t))
3218 subject (mail-fetch-field "subject")
3219 date (mail-fetch-field "date")
3220 to (or (mail-fetch-field "to" nil t) "")
3221 message-id (mail-fetch-field "message-id")
11aea8f5
RS
3222 references (mail-fetch-field "references" nil nil t)
3223 resent-reply-to (mail-fetch-field "resent-reply-to" nil t)
037fa8ca
RS
3224 resent-cc (and (not just-sender)
3225 (mail-fetch-field "resent-cc" nil t))
3226 resent-to (or (mail-fetch-field "resent-to" nil t) "")
3227;;; resent-subject (mail-fetch-field "resent-subject")
3228;;; resent-date (mail-fetch-field "resent-date")
3229;;; resent-message-id (mail-fetch-field "resent-message-id")
3230 )))
3231 ;; Merge the resent-to and resent-cc into the to and cc.
3232 (if (and resent-to (not (equal resent-to "")))
3233 (if (not (equal to ""))
3234 (setq to (concat to ", " resent-to))
3235 (setq to resent-to)))
3236 (if (and resent-cc (not (equal resent-cc "")))
3237 (if (not (equal cc ""))
3238 (setq cc (concat cc ", " resent-cc))
3239 (setq cc resent-cc)))
3240 ;; Add `Re: ' to subject if not there already.
bd1f0f84 3241 (and (stringp subject)
08960da1
KH
3242 (setq subject
3243 (concat rmail-reply-prefix
a21b845b
KH
3244 (if (let ((case-fold-search t))
3245 (string-match rmail-reply-regexp subject))
08960da1
KH
3246 (substring subject (match-end 0))
3247 subject))))
95b597ce
RS
3248 (rmail-start-mail
3249 nil
3250 ;; Using mail-strip-quoted-names is undesirable with newer mailers
3251 ;; since they can handle the names unstripped.
3252 ;; I don't know whether there are other mailers that still
3253 ;; need the names to be stripped.
ab233196
RS
3254;;; (mail-strip-quoted-names reply-to)
3255 reply-to
95b597ce
RS
3256 subject
3257 (rmail-make-in-reply-to-field from date message-id)
3258 (if just-sender
3259 nil
3260 ;; mail-strip-quoted-names is NOT necessary for rmail-dont-reply-to
3261 ;; to do its job.
3262 (let* ((cc-list (rmail-dont-reply-to
3263 (mail-strip-quoted-names
3264 (if (null cc) to (concat to ", " cc))))))
3265 (if (string= cc-list "") nil cc-list)))
3266 rmail-view-buffer
3267 (list (list 'rmail-mark-message
6b59a5fc
GM
3268 rmail-buffer
3269 (with-current-buffer rmail-buffer
3270 (aref rmail-msgref-vector msgnum))
95b597ce
RS
3271 "answered"))
3272 nil
3273 (list (cons "References" (concat (mapconcat 'identity references " ")
3274 " " message-id))))))
fa2795ca 3275
0985b412
RS
3276(defun rmail-mark-message (buffer msgnum-list attribute)
3277 "Give BUFFER's message number in MSGNUM-LIST the attribute ATTRIBUTE.
3278This is use in the send-actions for message buffers.
3279MSGNUM-LIST is a list of the form (MSGNUM)
3280which is an element of rmail-msgref-vector."
fa2795ca
RS
3281 (save-excursion
3282 (set-buffer buffer)
0985b412
RS
3283 (if (car msgnum-list)
3284 (rmail-set-attribute attribute t (car msgnum-list)))))
581d7e0b
RM
3285
3286(defun rmail-make-in-reply-to-field (from date message-id)
3287 (cond ((not from)
3288 (if message-id
3289 message-id
3290 nil))
3291 (mail-use-rfc822
3292 (require 'rfc822)
3293 (let ((tem (car (rfc822-addresses from))))
3294 (if message-id
8148a3ad
RS
3295 (if (or (not tem)
3296 (string-match
3297 (regexp-quote (if (string-match "@[^@]*\\'" tem)
3298 (substring tem 0
3299 (match-beginning 0))
3300 tem))
3301 message-id))
3302 ;; missing From, or Message-ID is sufficiently informative
581d7e0b
RM
3303 message-id
3304 (concat message-id " (" tem ")"))
1de48e7f
RS
3305 ;; Copy TEM, discarding text properties.
3306 (setq tem (copy-sequence tem))
3307 (set-text-properties 0 (length tem) nil tem)
3308 (setq tem (copy-sequence tem))
3309 ;; Use prin1 to fake RFC822 quoting
3310 (let ((field (prin1-to-string tem)))
3311 (if date
3312 (concat field "'s message of " date)
3313 field)))))
3210e730
RS
3314 ((let* ((foo "[^][\000-\037()<>@,;:\\\" ]+")
3315 (bar "[^][\000-\037()<>@,;:\\\"]+"))
3316 ;; These strings both match all non-ASCII characters.
581d7e0b
RM
3317 (or (string-match (concat "\\`[ \t]*\\(" bar
3318 "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
3319 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
3320 from)
3321 (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
3322 bar "\\))[ \t]*\\'")
3323 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
3324 from)))
3325 (let ((start (match-beginning 1))
3326 (end (match-end 1)))
3327 ;; Trim whitespace which above regexp match allows
3328 (while (and (< start end)
3329 (memq (aref from start) '(?\t ?\ )))
3330 (setq start (1+ start)))
3331 (while (and (< start end)
3332 (memq (aref from (1- end)) '(?\t ?\ )))
3333 (setq end (1- end)))
3334 (let ((field (substring from start end)))
3335 (if date (setq field (concat "message from " field " on " date)))
3336 (if message-id
3337 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
3338 (concat message-id " (" field ")")
3339 field))))
3340 (t
3341 ;; If we can't kludge it simply, do it correctly
3342 (let ((mail-use-rfc822 t))
3343 (rmail-make-in-reply-to-field from date message-id)))))
a16aef15 3344\f
8159f4af
RS
3345(defun rmail-forward (resend)
3346 "Forward the current message to another user.
3347With prefix argument, \"resend\" the message instead of forwarding it;
3348see the documentation of `rmail-resend'."
3349 (interactive "P")
3350 (if resend
3351 (call-interactively 'rmail-resend)
6b59a5fc 3352 (let ((forward-buffer rmail-buffer)
1825bea1 3353 (msgnum rmail-current-message)
8159f4af
RS
3354 (subject (concat "["
3355 (let ((from (or (mail-fetch-field "From")
3356 (mail-fetch-field ">From"))))
3357 (if from
3358 (concat (mail-strip-quoted-names from) ": ")
3359 ""))
3360 (or (mail-fetch-field "Subject") "")
3361 "]")))
bde7cb3f
RS
3362 (if (rmail-start-mail
3363 nil nil subject nil nil nil
fa2795ca 3364 (list (list 'rmail-mark-message
0985b412 3365 forward-buffer
6b59a5fc
GM
3366 (with-current-buffer rmail-buffer
3367 (aref rmail-msgref-vector msgnum))
fa2795ca 3368 "forwarded"))
bde7cb3f
RS
3369 ;; If only one window, use it for the mail buffer.
3370 ;; Otherwise, use another window for the mail buffer
3371 ;; so that the Rmail buffer remains visible
3372 ;; and sending the mail will get back to it.
3373 (and (not rmail-mail-new-frame) (one-window-p t)))
1825bea1 3374 ;; The mail buffer is now current.
b1d9bfa7 3375 (save-excursion
0f0a85b3 3376 ;; Insert after header separator--before signature if any.
c7ed80bc 3377 (goto-char (mail-text-start))
0c773047 3378 (if (or rmail-enable-mime rmail-enable-mime-composing)
6b59a5fc
GM
3379 (funcall rmail-insert-mime-forwarded-message-function
3380 forward-buffer)
3381 (insert "------- Start of forwarded message -------\n")
3382 ;; Quote lines with `- ' if they start with `-'.
3383 (let ((beg (point)) end)
3384 (setq end (point-marker))
3385 (set-marker-insertion-type end t)
3386 (insert-buffer-substring forward-buffer)
3387 (goto-char beg)
3388 (while (re-search-forward "^-" end t)
3389 (beginning-of-line)
3390 (insert "- ")
3391 (forward-line 1))
3392 (goto-char end)
3393 (skip-chars-backward "\n")
3394 (if (< (point) end)
3395 (forward-char 1))
3396 (delete-region (point) end)
3397 (set-marker end nil))
3398 (insert "------- End of forwarded message -------\n"))
1825bea1 3399 (push-mark))))))
a16aef15 3400\f
581d7e0b
RM
3401(defun rmail-resend (address &optional from comment mail-alias-file)
3402 "Resend current message to ADDRESSES.
d2fc297c 3403ADDRESSES should be a single address, a string consisting of several
581d7e0b
RM
3404addresses separated by commas, or a list of addresses.
3405
3406Optional FROM is the address to resend the message from, and
9a2fe7b2
RS
3407defaults from the value of `user-mail-address'.
3408Optional COMMENT is a string to insert as a comment in the resent message.
581d7e0b
RM
3409Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
3410typically for purposes of moderating a list."
3411 (interactive "sResend to: ")
9ad8712e
RS
3412 (require 'sendmail)
3413 (require 'mailalias)
9652931f
GM
3414 (unless (or (eq rmail-view-buffer (current-buffer))
3415 (eq rmail-buffer (current-buffer)))
70908594 3416 (error "Not an Rmail buffer"))
9a2fe7b2 3417 (if (not from) (setq from user-mail-address))
581d7e0b 3418 (let ((tembuf (generate-new-buffer " sendmail temp"))
581d7e0b 3419 (case-fold-search nil)
7d8a6e1f 3420 (mailbuf rmail-buffer))
581d7e0b 3421 (unwind-protect
7d8a6e1f 3422 (with-current-buffer tembuf
581d7e0b 3423 ;;>> Copy message into temp buffer
9652931f
GM
3424 (if rmail-enable-mime
3425 (funcall rmail-insert-mime-resent-message-function mailbuf)
3426 (insert-buffer-substring mailbuf))
581d7e0b 3427 (goto-char (point-min))
e20903fb 3428 ;; Delete any Sender field, since that's not specifiable.
d2fc297c
RS
3429 ; Only delete Sender fields in the actual header.
3430 (re-search-forward "^$" nil 'move)
3431 ; Using "while" here rather than "if" because some buggy mail
3432 ; software may have inserted multiple Sender fields.
3433 (while (re-search-backward "^Sender:" nil t)
3434 (let (beg)
3435 (setq beg (point))
3436 (forward-line 1)
3437 (while (looking-at "[ \t]")
3438 (forward-line 1))
3439 (delete-region beg (point))))
3440 ; Go back to the beginning of the buffer so the Resent- fields
3441 ; are inserted there.
3442 (goto-char (point-min))
581d7e0b
RM
3443 ;;>> Insert resent-from:
3444 (insert "Resent-From: " from "\n")
bcf12025 3445 (insert "Resent-Date: " (mail-rfc822-date) "\n")
581d7e0b
RM
3446 ;;>> Insert resent-to: and bcc if need be.
3447 (let ((before (point)))
c42722e3
RS
3448 (if mail-self-blind
3449 (insert "Resent-Bcc: " (user-login-name) "\n"))
581d7e0b
RM
3450 (insert "Resent-To: " (if (stringp address)
3451 address
3452 (mapconcat 'identity address ",\n\t"))
3453 "\n")
8095bf23 3454 ;; Expand abbrevs in the recipients.
50703776 3455 (save-excursion
8095bf23 3456 (if (featurep 'mailabbrev)
3f3495d3
RS
3457 (let ((end (point-marker))
3458 (local-abbrev-table mail-abbrevs)
3459 (old-syntax-table (syntax-table)))
3460 (if (and (not (vectorp mail-abbrevs))
3461 (file-exists-p mail-personal-alias-file))
3462 (build-mail-abbrevs))
ed1086b7
RS
3463 (unless mail-abbrev-syntax-table
3464 (mail-abbrev-make-syntax-table))
3f3495d3
RS
3465 (set-syntax-table mail-abbrev-syntax-table)
3466 (goto-char before)
3467 (while (and (< (point) end)
3468 (progn (forward-word 1)
3469 (<= (point) end)))
3470 (expand-abbrev))
3471 (set-syntax-table old-syntax-table))
8095bf23 3472 (expand-mail-aliases before (point)))))
581d7e0b
RM
3473 ;;>> Set up comment, if any.
3474 (if (and (sequencep comment) (not (zerop (length comment))))
3475 (let ((before (point))
3476 after)
3477 (insert comment)
3478 (or (eolp) (insert "\n"))
3479 (setq after (point))
3480 (goto-char before)
3481 (while (< (point) after)
3482 (insert "Resent-Comment: ")
3483 (forward-line 1))))
3484 ;; Don't expand aliases in the destination fields
3485 ;; of the original message.
3486 (let (mail-aliases)
389e8f11 3487 (funcall send-mail-function)))
8159f4af 3488 (kill-buffer tembuf))
7d8a6e1f
KH
3489 (with-current-buffer rmail-buffer
3490 (rmail-set-attribute "resent" t rmail-current-message))))
a16aef15 3491\f
72e609d0 3492(defvar mail-unsent-separator
f2da0c68
RS
3493 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
3494 "^ *---+ +Returned message +---+ *$\\|"
dc47993c 3495 "^ *---+ *Returned mail follows *---+ *$\\|"
20424e68 3496 "^Start of returned message$\\|"
0599b925 3497 "^---+ Below this line is a copy of the message.$\\|"
db32c193 3498 "^ *---+ +Original message +---+ *$\\|"
487fcdc0 3499 "^ *--+ +begin message +--+ *$\\|"
c42722e3 3500 "^ *---+ +Original message follows +---+ *$\\|"
f980ec39 3501 "^ *---+ +Your message follows +---+ *$\\|"
ea0d05b4 3502 "^|? *---+ +Message text follows: +---+ *|?$\\|"
c70d7359 3503 "^ *---+ +This is a copy of \\w+ message, including all the headers.*---+ *$")
03863002 3504 "A regexp that matches the separator before the text of a failed message.")
581d7e0b 3505
55a42e8c
RS
3506(defvar mail-mime-unsent-header "^Content-Type: message/rfc822 *$"
3507 "A regexp that matches the header of a MIME body part with a failed message.")
3508
581d7e0b
RM
3509(defun rmail-retry-failure ()
3510 "Edit a mail message which is based on the contents of the current message.
3511For a message rejected by the mail system, extract the interesting headers and
03863002 3512the body of the original message.
55a42e8c
RS
3513If the failed message is a MIME multipart message, it is searched for a
3514body part with a header which matches the variable `mail-mime-unsent-header'.
3515Otherwise, the variable `mail-unsent-separator' should match the string that
3db0cdac
RS
3516delimits the returned original message.
3517The variable `rmail-retry-ignored-headers' is a regular expression
3518specifying headers which should not be copied into the new message."
581d7e0b
RM
3519 (interactive)
3520 (require 'mail-utils)
2079601b 3521 (let ((rmail-this-buffer (current-buffer))
40e09f88 3522 (msgnum rmail-current-message)
ed104a87
RS
3523 bounce-start bounce-end bounce-indent resending
3524 ;; Fetch any content-type header in current message
3525 ;; Must search thru the whole unpruned header.
3526 (content-type
3527 (save-excursion
3528 (save-restriction
3529 (rmail-narrow-to-non-pruned-header)
3530 (mail-fetch-field "Content-Type") ))))
3531 (save-excursion
3532 (goto-char (point-min))
3533 (let ((case-fold-search t))
67f9d50e
FP
3534 (if (and content-type
3535 (string-match
3536 ";[\n\t ]*boundary=\"?\\([-0-9a-z'()+_,./:=? ]+\\)\"?"
ed104a87
RS
3537 content-type))
3538 ;; Handle a MIME multipart bounce message.
3539 (let ((codestring
3540 (concat "\n--"
67f9d50e 3541 (substring content-type (match-beginning 1)
ed104a87
RS
3542 (match-end 1)))))
3543 (unless (re-search-forward mail-mime-unsent-header nil t)
3544 (error "Cannot find beginning of header in failed message"))
3545 (unless (search-forward "\n\n" nil t)
3546 (error "Cannot find start of Mime data in failed message"))
3547 (setq bounce-start (point))
3548 (if (search-forward codestring nil t)
3549 (setq bounce-end (match-beginning 0))
3550 (setq bounce-end (point-max))))
3551 ;; Non-MIME bounce.
3552 (or (re-search-forward mail-unsent-separator nil t)
3553 (error "Cannot parse this as a failure message"))
3554 (skip-chars-forward "\n")
3555 ;; Support a style of failure message in which the original
3556 ;; message is indented, and included within lines saying
3557 ;; `Start of returned message' and `End of returned message'.
3558 (if (looking-at " +Received:")
3559 (progn
3560 (setq bounce-start (point))
3561 (skip-chars-forward " ")
3562 (setq bounce-indent (- (current-column)))
3563 (goto-char (point-max))
3564 (re-search-backward "^End of returned message$" nil t)
3565 (setq bounce-end (point)))
3566 ;; One message contained a few random lines before
3567 ;; the old message header. The first line of the
3568 ;; message started with two hyphens. A blank line
3569 ;; followed these random lines. The same line
3570 ;; beginning with two hyphens was possibly marking
3571 ;; the end of the message.
3572 (if (looking-at "^--")
3573 (let ((boundary (buffer-substring-no-properties
3574 (point)
3575 (progn (end-of-line) (point)))))
3576 (search-forward "\n\n")
3577 (skip-chars-forward "\n")
3578 (setq bounce-start (point))
3579 (goto-char (point-max))
3580 (search-backward (concat "\n\n" boundary) bounce-start t)
3581 (setq bounce-end (point)))
3582 (setq bounce-start (point)
3583 bounce-end (point-max)))
3584 (unless (search-forward "\n\n" nil t)
3585 (error "Cannot find end of header in failed message"))))))
3586 ;; We have found the message that bounced, within the current message.
3587 ;; Now start sending new message; default header fields from original.
3588 ;; Turn off the usual actions for initializing the message body
3589 ;; because we want to get only the text from the failure message.
3590 (let (mail-signature mail-setup-hook)
3591 (if (rmail-start-mail nil nil nil nil nil rmail-this-buffer
3592 (list (list 'rmail-mark-message
3593 rmail-this-buffer
3594 (aref rmail-msgref-vector msgnum)
3595 "retried")))
3596 ;; Insert original text as initial text of new draft message.
3597 ;; Bind inhibit-read-only since the header delimiter
3598 ;; of the previous message was probably read-only.
3599 (let ((inhibit-read-only t)
3600 rmail-displayed-headers
3601 rmail-ignored-headers)
3602 (erase-buffer)
3603 (insert-buffer-substring rmail-this-buffer
3604 bounce-start bounce-end)
3605 (goto-char (point-min))
3606 (if bounce-indent
3607 (indent-rigidly (point-min) (point-max) bounce-indent))
3608 (rmail-clear-headers rmail-retry-ignored-headers)
3609 (rmail-clear-headers "^sender:\\|^return-path:\\|^received:")
3610 (mail-sendmail-delimit-header)
3611 (save-restriction
3612 (narrow-to-region (point-min) (mail-header-end))
3613 (setq resending (mail-fetch-field "resent-to"))
3614 (if mail-self-blind
3615 (if resending
3616 (insert "Resent-Bcc: " (user-login-name) "\n")
3617 (insert "BCC: " (user-login-name) "\n"))))
3618 (goto-char (point-min))
3619 (mail-position-on-field (if resending "Resent-To" "To") t))))))
a16aef15 3620\f
bd1f0f84
RS
3621(defun rmail-summary-exists ()
3622 "Non-nil iff in an RMAIL buffer and an associated summary buffer exists.
90254bb0 3623In fact, the non-nil value returned is the summary buffer itself."
bd1f0f84
RS
3624 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
3625 rmail-summary-buffer))
3626
3627(defun rmail-summary-displayed ()
3628 "t iff in RMAIL buffer and an associated summary buffer is displayed."
3629 (and rmail-summary-buffer (get-buffer-window rmail-summary-buffer)))
3630
9bb97fe9 3631(defcustom rmail-redisplay-summary nil
bd1f0f84 3632 "*Non-nil means Rmail should show the summary when it changes.
9bb97fe9
RS
3633This has an effect only if a summary buffer exists."
3634 :type 'boolean
3635 :group 'rmail-summary)
3636
3637(defcustom rmail-summary-window-size nil
3638 "*Non-nil means specify the height for an Rmail summary window."
d177349a 3639 :type '(choice (const :tag "Disabled" nil) integer)
9bb97fe9 3640 :group 'rmail-summary)
90254bb0 3641
bd1f0f84
RS
3642;; Put the summary buffer back on the screen, if user wants that.
3643(defun rmail-maybe-display-summary ()
90254bb0
RS
3644 (let ((selected (selected-window))
3645 window)
3646 ;; If requested, make sure the summary is displayed.
3647 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
3648 rmail-redisplay-summary
6b265868
RS
3649 (if (get-buffer-window rmail-summary-buffer 0)
3650 ;; It's already in some frame; show that one.
3651 (let ((frame (window-frame
3652 (get-buffer-window rmail-summary-buffer 0))))
3653 (make-frame-visible frame)
3654 (raise-frame frame))
3655 (display-buffer rmail-summary-buffer)))
90254bb0
RS
3656 ;; If requested, set the height of the summary window.
3657 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
3658 rmail-summary-window-size
3659 (setq window (get-buffer-window rmail-summary-buffer))
6b265868
RS
3660 ;; Don't try to change the size if just one window in frame.
3661 (not (eq window (frame-root-window (window-frame window))))
67f9d50e 3662 (unwind-protect
90254bb0
RS
3663 (progn
3664 (select-window window)
35aba739 3665 (enlarge-window (- rmail-summary-window-size (window-height))))
90254bb0 3666 (select-window selected)))))
581d7e0b 3667\f
271c888a
SM
3668;;;; *** Rmail Local Fontification ***
3669
3670(defun rmail-fontify-buffer-function ()
3671 ;; This function's symbol is bound to font-lock-fontify-buffer-function.
f512e4c0 3672 (add-hook 'rmail-show-message-hook 'rmail-fontify-message nil t)
35aba739
SM
3673 ;; If we're already showing a message, fontify it now.
3674 (if rmail-current-message (rmail-fontify-message))
3675 ;; Prevent Font Lock mode from kicking in.
f512e4c0 3676 (setq font-lock-fontified t))
271c888a
SM
3677
3678(defun rmail-unfontify-buffer-function ()
3679 ;; This function's symbol is bound to font-lock-fontify-unbuffer-function.
f512e4c0
SM
3680 (let ((modified (buffer-modified-p))
3681 (buffer-undo-list t) (inhibit-read-only t)
3682 before-change-functions after-change-functions
3683 buffer-file-name buffer-file-truename)
3684 (save-restriction
3685 (widen)
3686 (remove-hook 'rmail-show-message-hook 'rmail-fontify-message t)
3687 (remove-text-properties (point-min) (point-max) '(rmail-fontified nil))
1afb7be2
RS
3688 (font-lock-default-unfontify-buffer)
3689 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))
271c888a
SM
3690
3691(defun rmail-fontify-message ()
3692 ;; Fontify the current message if it is not already fontified.
3693 (if (text-property-any (point-min) (point-max) 'rmail-fontified nil)
3694 (let ((modified (buffer-modified-p))
3695 (buffer-undo-list t) (inhibit-read-only t)
3696 before-change-functions after-change-functions
3697 buffer-file-name buffer-file-truename)
3698 (save-excursion
3699 (save-match-data
3700 (add-text-properties (point-min) (point-max) '(rmail-fontified t))
1afb7be2
RS
3701 (font-lock-fontify-region (point-min) (point-max))
3702 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))))
b04fa5c5 3703\f
03c9127e 3704;;; Speedbar support for RMAIL files.
b04fa5c5
KH
3705(eval-when-compile (require 'speedbar))
3706
3707(defvar rmail-speedbar-match-folder-regexp "^[A-Z0-9]+\\(\\.[A-Z0-9]+\\)?$"
82e736c1 3708 "*This regex is used to match folder names to be displayed in speedbar.
b04fa5c5
KH
3709Enabling this will permit speedbar to display your folders for easy
3710browsing, and moving of messages.")
03c9127e
EL
3711
3712(defvar rmail-speedbar-last-user nil
3713 "The last user to be displayed in the speedbar.")
3714
b04fa5c5
KH
3715(defvar rmail-speedbar-key-map nil
3716 "Keymap used when in rmail display mode.")
3717
3718(defun rmail-install-speedbar-variables ()
3719 "Install those variables used by speedbar to enhance rmail."
3720 (if rmail-speedbar-key-map
3721 nil
3722 (setq rmail-speedbar-key-map (speedbar-make-specialized-keymap))
3723
3724 (define-key rmail-speedbar-key-map "e" 'speedbar-edit-line)
3725 (define-key rmail-speedbar-key-map "r" 'speedbar-edit-line)
3726 (define-key rmail-speedbar-key-map "\C-m" 'speedbar-edit-line)
3727 (define-key rmail-speedbar-key-map "M"
3728 'rmail-speedbar-move-message-to-folder-on-line)))
3729
03c9127e 3730(defvar rmail-speedbar-menu-items
b04fa5c5
KH
3731 '(["Read Folder" speedbar-edit-line t]
3732 ["Move message to folder" rmail-speedbar-move-message-to-folder-on-line
03c9127e
EL
3733 (save-excursion (beginning-of-line)
3734 (looking-at "<M> "))])
3735 "Additional menu-items to add to speedbar frame.")
3736
b04fa5c5
KH
3737;; Make sure our special speedbar major mode is loaded
3738(if (featurep 'speedbar)
3739 (rmail-install-speedbar-variables)
3740 (add-hook 'speedbar-load-hook 'rmail-install-speedbar-variables))
3741
03c9127e
EL
3742(defun rmail-speedbar-buttons (buffer)
3743 "Create buttons for BUFFER containing rmail messages.
3744Click on the address under Reply to: to reply to this person.
3745Under Folders: Click a name to read it, or on the <M> to move the
3746current message into that RMAIL folder."
3747 (let ((from nil))
3748 (save-excursion
3749 (set-buffer buffer)
3750 (goto-char (point-min))
3751 (if (not (re-search-forward "^Reply-To: " nil t))
3752 (if (not (re-search-forward "^From:? " nil t))
3753 (setq from t)))
3754 (if from
3755 nil
3756 (setq from (buffer-substring (point) (save-excursion
3757 (end-of-line)
3758 (point))))))
3759 (goto-char (point-min))
3760 (if (and (looking-at "Reply to:")
3761 (equal from rmail-speedbar-last-user))
3762 nil
3763 (setq rmail-speedbar-last-user from)
3764 (erase-buffer)
3765 (insert "Reply To:\n")
3766 (if (stringp from)
3767 (speedbar-insert-button from 'speedbar-directory-face 'highlight
3768 'rmail-speedbar-button 'rmail-reply))
3769 (insert "Folders:\n")
3770 (let* ((case-fold-search nil)
3771 (df (directory-files (save-excursion (set-buffer buffer)
3772 default-directory)
b04fa5c5 3773 nil rmail-speedbar-match-folder-regexp)))
03c9127e
EL
3774 (while df
3775 (speedbar-insert-button "<M>" 'speedbar-button-face 'highlight
3776 'rmail-speedbar-move-message (car df))
3777 (speedbar-insert-button (car df) 'speedbar-file-face 'highlight
3778 'rmail-speedbar-find-file nil t)
3779 (setq df (cdr df)))))))
3780
3781(defun rmail-speedbar-button (text token indent)
3782 "Execute an rmail command specified by TEXT.
3783The command used is TOKEN. INDENT is not used."
3784 (speedbar-with-attached-buffer
3785 (funcall token t)))
3786
3787(defun rmail-speedbar-find-file (text token indent)
3788 "Load in the rmail file TEXT.
3789TOKEN and INDENT are not used."
3790 (speedbar-with-attached-buffer
3791 (message "Loading in RMAIL file %s..." text)
3792 (find-file text)))
3793
b04fa5c5 3794(defun rmail-speedbar-move-message-to-folder-on-line ()
03c9127e
EL
3795 "If the current line is a folder, move current message to it."
3796 (interactive)
3797 (save-excursion
3798 (beginning-of-line)
3799 (if (re-search-forward "<M> " (save-excursion (end-of-line) (point)) t)
3800 (progn
3801 (forward-char -2)
3802 (speedbar-do-function-pointer)))))
3803
3804(defun rmail-speedbar-move-message (text token indent)
3805 "From button TEXT, copy current message to the rmail file specified by TOKEN.
3806TEXT and INDENT are not used."
3807 (speedbar-with-attached-buffer
3808 (message "Moving message to %s" token)
3809 (rmail-output-to-rmail-file token)))
3810
639540ec
RS
3811; Functions for setting, getting and encoding the POP password.
3812; The password is encoded to prevent it from being easily accessible
3813; to "prying eyes." Obviously, this encoding isn't "real security,"
3814; nor is it meant to be.
3815
3816;;;###autoload
3817(defun rmail-set-pop-password (password)
3818 "Set PASSWORD to be used for retrieving mail from a POP server."
3819 (interactive "sPassword: ")
3820 (if password
67f9d50e 3821 (setq rmail-encoded-pop-password
639540ec
RS
3822 (rmail-encode-string password (emacs-pid)))
3823 (setq rmail-pop-password nil)
3824 (setq rmail-encoded-pop-password nil)))
3825
3826(defun rmail-get-pop-password ()
3827 "Get the password for retrieving mail from a POP server. If none
3828has been set, then prompt the user for one."
3829 (if (not rmail-encoded-pop-password)
3830 (progn (if (not rmail-pop-password)
8b9a64c2 3831 (setq rmail-pop-password (read-passwd "POP password: ")))
639540ec
RS
3832 (rmail-set-pop-password rmail-pop-password)
3833 (setq rmail-pop-password nil)))
3834 (rmail-encode-string rmail-encoded-pop-password (emacs-pid)))
3835
3836(defun rmail-have-password ()
3837 (or rmail-pop-password rmail-encoded-pop-password))
3838
3839(defun rmail-encode-string (string mask)
3840 "Encode STRING with integer MASK, by taking the exclusive OR of the
3841lowest byte in the mask with the first character of string, the
3842second-lowest-byte with the second character of the string, etc.,
3843restarting at the lowest byte of the mask whenever it runs out.
3844Returns the encoded string. Calling the function again with an
3845encoded string (and the same mask) will decode the string."
7d6d84c1 3846 (setq mask (abs mask)) ; doesn't work if negative
67f9d50e 3847 (let* ((string-vector (string-to-vector string)) (i 0)
639540ec
RS
3848 (len (length string-vector)) (curmask mask) charmask)
3849 (while (< i len)
3850 (if (= curmask 0)
3851 (setq curmask mask))
3852 (setq charmask (% curmask 256))
3853 (setq curmask (lsh curmask -8))
3854 (aset string-vector i (logxor charmask (aref string-vector i)))
3855 (setq i (1+ i)))
3856 (concat string-vector)))
03c9127e 3857
49116ac0
JB
3858(provide 'rmail)
3859
ab5796a9 3860;;; arch-tag: cff0a950-57fe-4f73-a86e-91ff75afd06c
c88ab9ce 3861;;; rmail.el ends here