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