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