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