(skip_chars): Merge mule changes back in.
[bpt/emacs.git] / lisp / mail / rmail.el
CommitLineData
c88ab9ce
ER
1;;; rmail.el --- main code of "RMAIL" mail reader for Emacs.
2
9596811a 3;; Copyright (C) 1985,86,87,88,93,94,95,96 Free Software Foundation, Inc.
3a801d0c 4
e5167999 5;; Maintainer: FSF
d7b4d18f 6;; Keywords: mail
e5167999 7
581d7e0b
RM
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
e5167999 12;; the Free Software Foundation; either version 2, or (at your option)
581d7e0b
RM
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
b578f267
EN
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
581d7e0b 24
e5167999 25;;; Code:
581d7e0b
RM
26
27;; Souped up by shane@mit-ajax based on ideas of rlk@athena.mit.edu
28;; New features include attribute and keyword support, message
29;; selection by dispatch table, summary by attributes and keywords,
30;; expunging by dispatch table, sticky options for file commands.
31
bd1f0f84
RS
32;; Extended by Bob Weiner of Motorola
33;; New features include: rmail and rmail-summary buffers remain
34;; synchronized and key bindings basically operate the same way in both
35;; buffers, summary by topic or by regular expression, rmail-reply-prefix
36;; variable, and a bury rmail buffer (wipe) command.
37;;
38
581d7e0b 39(require 'mail-utils)
bd1f0f84
RS
40
41;; For Emacs V18 compatibility
bd1f0f84
RS
42(and (not (fboundp 'buffer-disable-undo))
43 (fboundp 'buffer-flush-undo)
31e1d920 44 (defalias 'buffer-disable-undo 'buffer-flush-undo))
bd1f0f84
RS
45
46; These variables now declared in paths.el.
581d7e0b
RM
47;(defvar rmail-spool-directory "/usr/spool/mail/"
48; "This is the name of the directory used by the system mailer for\n\
6af28e9f 49;delivering new mail. Its name should end with a slash.")
581d7e0b
RM
50;(defvar rmail-file-name
51; (expand-file-name "~/RMAIL")
52; "")
53
74562053
RS
54(defvar rmail-movemail-program nil
55 "If non-nil, name of program for fetching new mail.")
56
ceeb471c
RS
57(defvar rmail-pop-password nil
58 "*Password to use when reading mail from a POP server, if required.")
59
60(defvar rmail-pop-password-required nil
61 "*Non-nil if a password is required when reading mail using POP.")
62
4144e5cb
RS
63(defvar rmail-preserve-inbox nil
64 "*Non-nil if incoming mail should be left in the user's inbox,
65rather than deleted, after it is retrieved.")
66
581d7e0b
RM
67;;;###autoload
68(defvar rmail-dont-reply-to-names nil "\
69*A regexp specifying names to prune of reply to messages.
388348ae 70A value of nil means exclude your own name only.")
581d7e0b
RM
71
72;;;###autoload
73(defvar rmail-default-dont-reply-to-names "info-" "\
74A regular expression specifying part of the value of the default value of
75the variable `rmail-dont-reply-to-names', for when the user does not set
76`rmail-dont-reply-to-names' explicitly. (The other part of the default
77value is the user's name.)
388348ae 78It is useful to set this variable in the site customization file.")
581d7e0b
RM
79
80;;;###autoload
bf437d6d 81(defvar rmail-ignored-headers "^via:\\|^mail-from:\\|^origin:\\|^status:\\|^received:\\|^x400-originator:\\|^x400-recipients:\\|^x400-received:\\|^x400-mts-identifier:\\|^x400-content-type:\\|^\\(resent-\\|\\)message-id:\\|^summary-line:\\|^resent-date:\\|^nntp-posting-host:\\|^path:\\|^x-char.*:\\|^x-face:\\|^x-mailer:\\|^delivered-to:"
f14055b1 82 "*Regexp to match header fields that Rmail should normally hide.")
581d7e0b 83
8095bf23
RS
84;;;###autoload
85(defvar rmail-displayed-headers nil
86 "*Regexp to match Header fields that Rmail should display.
87If nil, display all header fields except those matched by
88`rmail-ignored-headers'.")
89
3db0cdac
RS
90;;;###autoload
91(defvar rmail-retry-ignored-headers nil "\
92*Headers that should be stripped when retrying a failed message.")
93
072c3cd2
RS
94;;;###autoload
95(defvar rmail-highlighted-headers "^From:\\|^Subject:" "\
3df6812a 96*Regexp to match Header fields that Rmail should normally highlight.
a2e667ec
RS
97A value of nil means don't highlight.
98See also `rmail-highlight-face'.")
072c3cd2 99
3df6812a
RS
100;;;###autoload
101(defvar rmail-highlight-face nil "\
102*Face used by Rmail for highlighting headers.")
103
581d7e0b
RM
104;;;###autoload
105(defvar rmail-delete-after-output nil "\
106*Non-nil means automatically delete a message that is copied to a file.")
107
108;;;###autoload
5009d9f0 109(defvar rmail-primary-inbox-list nil "\
3af9db89
RS
110*List of files which are inboxes for user's primary mail file `~/RMAIL'.
111`nil' means the default, which is (\"/usr/spool/mail/$USER\")
112\(the name varies depending on the operating system,
581d7e0b
RM
113and the value of the environment variable MAIL overrides it).")
114
94ed51e8
RS
115;;;###autoload
116(defvar rmail-mail-new-frame nil
117 "*Non-nil means Rmail makes a new frame for composing outgoing mail.")
118
90254bb0
RS
119;;;###autoload
120(defvar rmail-secondary-file-directory "~/"
121 "*Directory for additional secondary Rmail files.")
122;;;###autoload
123(defvar rmail-secondary-file-regexp "\\.xmail$"
124 "*Regexp for which files are secondary Rmail files.")
125
6c714afe
RS
126;;;###autoload
127(defvar rmail-mode-hook nil
128 "List of functions to call when Rmail is invoked.")
129
130;;;###autoload
131(defvar rmail-get-new-mail-hook nil
132 "List of functions to call when Rmail has retrieved new mail.")
133
134;;;###autoload
135(defvar rmail-show-message-hook nil
136 "List of functions to call when Rmail displays a message.")
137
138;;;###autoload
139(defvar rmail-delete-message-hook nil
140 "List of functions to call when Rmail deletes a message.
141When the hooks are called, the message has been marked deleted but is
142still the current message in the Rmail buffer.")
143
bd1f0f84
RS
144;; These may be altered by site-init.el to match the format of mmdf files
145;; delimiting used on a given host (delim1 and delim2 from the config
146;; files).
581d7e0b
RM
147
148(defvar mmdf-delim1 "^\001\001\001\001\n"
149 "Regexp marking the start of an mmdf message")
150(defvar mmdf-delim2 "^\001\001\001\001\n"
151 "Regexp marking the end of an mmdf message")
152
153(defvar rmail-message-filter nil
7424c13b
RS
154 "If non-nil, a filter function for new messages in RMAIL.
155Called with region narrowed to the message, including headers,
156before obeying `rmail-ignored-headers'.")
581d7e0b 157
bd1f0f84
RS
158(defvar rmail-reply-prefix "Re: "
159 "String to prepend to Subject line when replying to a message.")
160
a21b845b 161;; Some mailers use "Re(2):" or "Re^2:" or "Re: Re:" or "Re[2]:".
08960da1 162;; This pattern should catch all the common variants.
3d52d58a 163(defvar rmail-reply-regexp "\\`\\(Re\\(([0-9]+)\\|\\[[0-9]+\\]\\|\\^[0-9]+\\)?: *\\)*"
7424c13b 164 "Regexp to delete from Subject line before inserting `rmail-reply-prefix'.")
08960da1 165
a2e667ec 166(defvar rmail-display-summary nil
7424c13b 167 "If non-nil, Rmail always displays the summary buffer.")
a2e667ec 168
581d7e0b
RM
169(defvar rmail-mode-map nil)
170
171(defvar rmail-inbox-list nil)
172(defvar rmail-keywords nil)
173
174;; Message counters and markers. Deleted flags.
175
176(defvar rmail-current-message nil)
177(defvar rmail-total-messages nil)
178(defvar rmail-message-vector nil)
179(defvar rmail-deleted-vector nil)
180
24db549c
RS
181(defvar rmail-overlay-list nil)
182
a95caeed 183(defvar rmail-font-lock-keywords
1e0b8079 184 (eval-when-compile
34c3c889
SM
185 (let* ((cite-chars "[>|}]")
186 (cite-prefix "A-Za-z")
187 (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
188 (list '("^\\(From\\|Sender\\):" . font-lock-function-name-face)
189 '("^Reply-To:.*$" . font-lock-function-name-face)
190 '("^Subject:" . font-lock-comment-face)
3d51068d
SM
191 '("^\\(To\\|Apparently-To\\|Cc\\|Newsgroups\\):"
192 . font-lock-keyword-face)
34c3c889
SM
193 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
194 `(,cite-chars
195 (,(concat "\\=[ \t]*"
3d51068d
SM
196 "\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
197 "\\(" cite-chars "[ \t]*\\)\\)+"
198 "\\(.*\\)")
34c3c889 199 (beginning-of-line) (end-of-line)
3d51068d
SM
200 (2 font-lock-reference-face nil t)
201 (4 font-lock-comment-face nil t)))
34c3c889
SM
202 '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\|Date\\):.*$"
203 . font-lock-string-face))))
a95caeed
RS
204 "Additional expressions to highlight in Rmail mode.")
205
581d7e0b
RM
206;; These are used by autoloaded rmail-summary.
207
208(defvar rmail-summary-buffer nil)
209(defvar rmail-summary-vector nil)
210
211;; `Sticky' default variables.
212
213;; Last individual label specified to a or k.
214(defvar rmail-last-label nil)
bd1f0f84 215;; Last set of values specified to C-M-n, C-M-p, C-M-s or C-M-l.
581d7e0b 216(defvar rmail-last-multi-labels nil)
bd1f0f84 217(defvar rmail-last-regexp nil)
a7a39fbe 218(defvar rmail-default-file "~/xmail"
c08f13bf 219 "*Default file name for \\[rmail-output].")
a7a39fbe 220(defvar rmail-default-rmail-file "~/XMAIL"
c08f13bf 221 "*Default file name for \\[rmail-output-to-rmail-file].")
46947372 222
4746118a
JB
223;;; Regexp matching the delimiter of messages in UNIX mail format
224;;; (UNIX From lines), minus the initial ^. Note that if you change
225;;; this expression, you must change the code in rmail-nuke-pinhead-header
226;;; that knows the exact ordering of the \\( \\) subexpressions.
46947372 227(defvar rmail-unix-mail-delimiter
b7cceaf1 228 (let ((time-zone-regexp
258b0b37 229 (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
b7cceaf1
JB
230 "\\|[-+]?[0-9][0-9][0-9][0-9]"
231 "\\|"
232 "\\) *")))
233 (concat
234 "From "
235
74d6e31c
RS
236 ;; Many things can happen to an RFC 822 mailbox before it is put into
237 ;; a `From' line. The leading phrase can be stripped, e.g.
238 ;; `Joe <@w.x:joe@y.z>' -> `<@w.x:joe@y.z>'. The <> can be stripped, e.g.
239 ;; `<@x.y:joe@y.z>' -> `@x.y:joe@y.z'. Everything starting with a CRLF
240 ;; can be removed, e.g.
241 ;; From: joe@y.z (Joe K
242 ;; User)
243 ;; can yield `From joe@y.z (Joe K Fri Mar 22 08:11:15 1996', and
244 ;; From: Joe User
245 ;; <joe@y.z>
246 ;; can yield `From Joe User Fri Mar 22 08:11:15 1996'.
4c4a44f3
PE
247 ;; The mailbox can be removed or be replaced by white space, e.g.
248 ;; From: "Joe User"{space}{tab}
249 ;; <joe@y.z>
250 ;; can yield `From {space}{tab} Fri Mar 22 08:11:15 1996',
251 ;; where {space} and {tab} represent the Ascii space and tab characters.
74d6e31c
RS
252 ;; We want to match the results of any of these manglings.
253 ;; The following regexp rejects names whose first characters are
254 ;; obviously bogus, but after that anything goes.
4c4a44f3 255 "\\([^\0-\b\n-\r\^?].*\\)? "
b7cceaf1
JB
256
257 ;; The time the message was sent.
74d6e31c
RS
258 "\\([^\0-\r \^?]+\\) +" ; day of the week
259 "\\([^\0-\r \^?]+\\) +" ; month
260 "\\([0-3]?[0-9]\\) +" ; day of month
261 "\\([0-2][0-9]:[0-5][0-9]\\(:[0-6][0-9]\\)?\\) *" ; time of day
b7cceaf1
JB
262
263 ;; Perhaps a time zone, specified by an abbreviation, or by a
264 ;; numeric offset.
265 time-zone-regexp
266
267 ;; The year.
74d6e31c 268 " \\([0-9][0-9]+\\) *"
b7cceaf1
JB
269
270 ;; On some systems the time zone can appear after the year, too.
271 time-zone-regexp
272
881fd7eb
KH
273 ;; Old uucp cruft.
274 "\\(remote from .*\\)?"
b7cceaf1
JB
275
276 "\n"))
277 nil)
278
bd1f0f84
RS
279;; Perform BODY in the summary buffer
280;; in such a way that its cursor is properly updated in its own window.
281(defmacro rmail-select-summary (&rest body)
a05fc4d9
RS
282 (` (let ((total rmail-total-messages))
283 (if (rmail-summary-displayed)
284 (let ((window (selected-window)))
285 (save-excursion
286 (unwind-protect
287 (progn
288 (pop-to-buffer rmail-summary-buffer)
289 ;; rmail-total-messages is a buffer-local var
290 ;; in the rmail buffer.
291 ;; This way we make it available for the body
292 ;; even tho the rmail buffer is not current.
293 (let ((rmail-total-messages total))
294 (,@ body)))
295 (select-window window))))
296 (save-excursion
297 (set-buffer rmail-summary-buffer)
298 (let ((rmail-total-messages total))
299 (,@ body))))
300 (rmail-maybe-display-summary))))
d1e69dec
KH
301
302(defvar rmail-view-buffer nil
303 "Buffer which holds RMAIL message for displaying.")
304
305;; Mule and MIME related variables.
306
307;;;###autoload
308(defvar rmail-file-coding-system nil
309 "Coding system used in RMAIL file.
310
311This is set to nil by default.")
312
313;;;###autoload
314(defvar rmail-enable-mime nil
315 "*If non-nil, RMAIL uses MIME feature.
316If the value is t, RMAIL automatically shows MIME decoded message.
317If the value is neither t nor nil, RMAIL does not show MIME decoded message
318until a user explicitly require it.")
319
320;;;###autoload
321(defvar rmail-show-mime-function nil
322 "Function to show MIME decoded message of RMAIL file.")
323
324;;;###autoload
325(defvar rmail-mime-feature 'rmail-mime
326 "Feature to provide for using MIME feature in RMAIL.
327When starting rmail, this feature is requrired if rmail-enable-mime
328is non-nil.")
329
581d7e0b
RM
330\f
331;;;; *** Rmail Mode ***
332
333;;;###autoload
334(defun rmail (&optional file-name-arg)
335 "Read and edit incoming mail.
c42722e3 336Moves messages into file named by `rmail-file-name' (a babyl format file)
581d7e0b
RM
337 and edits that file in RMAIL Mode.
338Type \\[describe-mode] once editing that file, for a list of RMAIL commands.
339
d2fc297c
RS
340May be called with file name as argument; then performs rmail editing on
341that file, but does not copy any new mail into the file.
342Interactively, if you supply a prefix argument, then you
a2e667ec
RS
343have a chance to specify a file name with the minibuffer.
344
345If `rmail-display-summary' is non-nil, make a summary for this RMAIL file."
581d7e0b 346 (interactive (if current-prefix-arg
b5e10b23 347 (list (read-file-name "Run rmail on RMAIL file: "))))
d1e69dec
KH
348 (if rmail-enable-mime
349 (condition-case err
350 (require rmail-mime-feature)
351 (error (message "Feature `%s' not provided" rmail-mime-feature)
352 (sit-for 1)
353 (setq rmail-enable-mime nil))))
581d7e0b 354 (let* ((file-name (expand-file-name (or file-name-arg rmail-file-name)))
6af28e9f 355 (existed (get-file-buffer file-name))
d1e69dec 356 (coding-system-for-read 'no-conversion)
6af28e9f 357 run-mail-hook)
581d7e0b
RM
358 ;; Like find-file, but in the case where a buffer existed
359 ;; and the file was reverted, recompute the message-data.
360 (if (and existed (not (verify-visited-file-modtime existed)))
361 (progn
362 ;; Don't be confused by apparent local-variables spec
363 ;; in the last message in the RMAIL file.
8defe99b 364 (let ((enable-local-variables nil))
581d7e0b 365 (find-file file-name))
bd1f0f84
RS
366 (if (and (verify-visited-file-modtime existed)
367 (eq major-mode 'rmail-mode))
581d7e0b
RM
368 (progn (rmail-forget-messages)
369 (rmail-set-message-counters))))
8defe99b 370 (let ((enable-local-variables nil))
581d7e0b 371 (find-file file-name)))
bd1f0f84
RS
372 (if (eq major-mode 'rmail-edit-mode)
373 (error "Exit Rmail Edit mode before getting new mail."))
581d7e0b
RM
374 (if (and existed (> (buffer-size) 0))
375 ;; Buffer not new and not empty; ensure in proper mode, but that's all.
376 (or (eq major-mode 'rmail-mode)
6af28e9f
RS
377 (progn (rmail-mode-2)
378 (setq run-mail-hook t)))
379 (setq run-mail-hook t)
581d7e0b 380 (rmail-mode-2)
581d7e0b
RM
381 ;; Convert all or part to Babyl file if possible.
382 (rmail-convert-file)
383 (goto-char (point-max))
384 (if (null rmail-inbox-list)
385 (progn
386 (rmail-set-message-counters)
387 (rmail-show-message))))
bcddd52e
RS
388 (or (and (null file-name-arg)
389 (rmail-get-new-mail))
390 (rmail-show-message (rmail-first-unseen-message)))
8b7ef584 391 (if rmail-display-summary (rmail-summary))
6af28e9f
RS
392 (rmail-construct-io-menu)
393 (if run-mail-hook
394 (run-hooks 'rmail-mode-hook))))
581d7e0b
RM
395
396;; Given the value of MAILPATH, return a list of inbox file names.
397;; This is turned off because it is not clear that the user wants
398;; all these inboxes to feed into the primary rmail file.
399; (defun rmail-convert-mailpath (string)
400; (let (idx list)
401; (while (setq idx (string-match "[%:]" string))
402; (let ((this (substring string 0 idx)))
403; (setq string (substring string (1+ idx)))
404; (setq list (cons (if (string-match "%" this)
405; (substring this 0 (string-match "%" this))
406; this)
407; list))))
408; list))
409
410; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
411; will not cause emacs 18.55 problems.
412
413(defun rmail-convert-file ()
414 (let (convert)
415 (widen)
416 (goto-char (point-min))
417 ;; If file doesn't start like a Babyl file,
418 ;; convert it to one, by adding a header and converting each message.
419 (cond ((looking-at "BABYL OPTIONS:"))
420 ((looking-at "Version: 5\n")
421 ;; Losing babyl file made by old version of Rmail.
422 ;; Just fix the babyl file header; don't make a new one,
423 ;; so we don't lose the Labels: file attribute, etc.
424 (let ((buffer-read-only nil))
425 (insert "BABYL OPTIONS: -*- rmail -*-\n")))
760a3528
BF
426 ((equal (point-min) (point-max))
427 ;; Empty RMAIL file. Just insert the header.
428 (rmail-insert-rmail-file-header))
581d7e0b 429 (t
760a3528 430 ;; Non-empty file in non-RMAIL format. Add header and convert.
581d7e0b
RM
431 (setq convert t)
432 (rmail-insert-rmail-file-header)))
d1e69dec
KH
433 (if (and (not convert)
434 (not rmail-enable-mime)
435 rmail-file-coding-system)
436 ;; Decode BABYL part at the headq only. The remaining non
437 ;; BABYL parts are decode in rmail-convert-to-babyl-format if
438 ;; necessary..
439 (rmail-decode-babyl-format))
581d7e0b
RM
440 ;; If file was not a Babyl file or if there are
441 ;; Unix format messages added at the end,
442 ;; convert file as necessary.
443 (if (or convert
e3e87be8
RS
444 (save-excursion
445 (goto-char (point-max))
0b40717e
RS
446 (search-backward "\n\^_")
447 (forward-char 2)
e3e87be8 448 (looking-at "\n*From ")))
581d7e0b
RM
449 (let ((buffer-read-only nil))
450 (message "Converting to Babyl format...")
e3e87be8
RS
451 ;; If file needs conversion, convert it all,
452 ;; except for the BABYL header.
453 ;; (rmail-convert-to-babyl-format would delete the header.)
e3e87be8
RS
454 (goto-char (point-min))
455 (search-forward "\n\^_" nil t)
073fb956 456 (narrow-to-region (point) (point-max))
581d7e0b
RM
457 (rmail-convert-to-babyl-format)
458 (message "Converting to Babyl format...done")))))
459
760a3528
BF
460;;; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
461;;; will not cause emacs 18.55 problems.
581d7e0b
RM
462
463(defun rmail-insert-rmail-file-header ()
464 (let ((buffer-read-only nil))
465 (insert "BABYL OPTIONS: -*- rmail -*-
466Version: 5
467Labels:
468Note: This is the header of an rmail file.
469Note: If you are seeing it in rmail,
470Note: it means the file has no messages in it.\n\^_")))
471
d1e69dec
KH
472;; Decode Babyl formated part at the head of current buffer by
473;; rmail-file-coding-system.
474
475(defun rmail-decode-babyl-format ()
476 (let ((modifiedp (buffer-modified-p))
477 (buffer-read-only nil)
478 pos)
479 (goto-char (point-min))
480 (search-forward "\n\^_" nil t) ; Skip BYBYL header.
481 (setq pos (point))
482 (message "Decoding messages...")
483 (while (search-forward "\n\^_" nil t)
484 (decode-coding-region pos (point) rmail-file-coding-system)
485 (setq pos (point)))
486 (message "Decoding messages...done")
487 (set-buffer-file-coding-system rmail-file-coding-system)
488 (set-buffer-modified-p modifiedp)))
489
581d7e0b
RM
490(if rmail-mode-map
491 nil
492 (setq rmail-mode-map (make-keymap))
493 (suppress-keymap rmail-mode-map)
bd1f0f84
RS
494 (define-key rmail-mode-map "a" 'rmail-add-label)
495 (define-key rmail-mode-map "b" 'rmail-bury)
496 (define-key rmail-mode-map "c" 'rmail-continue)
497 (define-key rmail-mode-map "d" 'rmail-delete-forward)
498 (define-key rmail-mode-map "\C-d" 'rmail-delete-backward)
499 (define-key rmail-mode-map "e" 'rmail-edit-current-message)
500 (define-key rmail-mode-map "f" 'rmail-forward)
501 (define-key rmail-mode-map "g" 'rmail-get-new-mail)
502 (define-key rmail-mode-map "h" 'rmail-summary)
503 (define-key rmail-mode-map "i" 'rmail-input)
504 (define-key rmail-mode-map "j" 'rmail-show-message)
505 (define-key rmail-mode-map "k" 'rmail-kill-label)
506 (define-key rmail-mode-map "l" 'rmail-summary-by-labels)
581d7e0b 507 (define-key rmail-mode-map "\e\C-h" 'rmail-summary)
581d7e0b
RM
508 (define-key rmail-mode-map "\e\C-l" 'rmail-summary-by-labels)
509 (define-key rmail-mode-map "\e\C-r" 'rmail-summary-by-recipients)
510 (define-key rmail-mode-map "\e\C-s" 'rmail-summary-by-regexp)
bd1f0f84
RS
511 (define-key rmail-mode-map "\e\C-t" 'rmail-summary-by-topic)
512 (define-key rmail-mode-map "m" 'rmail-mail)
513 (define-key rmail-mode-map "\em" 'rmail-retry-failure)
514 (define-key rmail-mode-map "n" 'rmail-next-undeleted-message)
515 (define-key rmail-mode-map "\en" 'rmail-next-message)
516 (define-key rmail-mode-map "\e\C-n" 'rmail-next-labeled-message)
517 (define-key rmail-mode-map "o" 'rmail-output-to-rmail-file)
518 (define-key rmail-mode-map "\C-o" 'rmail-output)
519 (define-key rmail-mode-map "p" 'rmail-previous-undeleted-message)
520 (define-key rmail-mode-map "\ep" 'rmail-previous-message)
521 (define-key rmail-mode-map "\e\C-p" 'rmail-previous-labeled-message)
522 (define-key rmail-mode-map "q" 'rmail-quit)
523 (define-key rmail-mode-map "r" 'rmail-reply)
ed7ace63 524;; I find I can't live without the default M-r command -- rms.
bd1f0f84
RS
525;; (define-key rmail-mode-map "\er" 'rmail-search-backwards)
526 (define-key rmail-mode-map "s" 'rmail-expunge-and-save)
527 (define-key rmail-mode-map "\es" 'rmail-search)
528 (define-key rmail-mode-map "t" 'rmail-toggle-header)
529 (define-key rmail-mode-map "u" 'rmail-undelete-previous-message)
5eb9cf97 530 (define-key rmail-mode-map "w" 'rmail-output-body-to-file)
bd1f0f84
RS
531 (define-key rmail-mode-map "x" 'rmail-expunge)
532 (define-key rmail-mode-map "." 'rmail-beginning-of-message)
533 (define-key rmail-mode-map "<" 'rmail-first-message)
534 (define-key rmail-mode-map ">" 'rmail-last-message)
535 (define-key rmail-mode-map " " 'scroll-up)
536 (define-key rmail-mode-map "\177" 'scroll-down)
537 (define-key rmail-mode-map "?" 'describe-mode)
3bf526cf
RS
538 (define-key rmail-mode-map "\C-c\C-s\C-d" 'rmail-sort-by-date)
539 (define-key rmail-mode-map "\C-c\C-s\C-s" 'rmail-sort-by-subject)
540 (define-key rmail-mode-map "\C-c\C-s\C-a" 'rmail-sort-by-author)
541 (define-key rmail-mode-map "\C-c\C-s\C-r" 'rmail-sort-by-recipient)
542 (define-key rmail-mode-map "\C-c\C-s\C-c" 'rmail-sort-by-correspondent)
543 (define-key rmail-mode-map "\C-c\C-s\C-l" 'rmail-sort-by-lines)
9ae6d54a 544 (define-key rmail-mode-map "\C-c\C-s\C-k" 'rmail-sort-by-keywords)
60fb2b34
RS
545 (define-key rmail-mode-map "\C-c\C-n" 'rmail-next-same-subject)
546 (define-key rmail-mode-map "\C-c\C-p" 'rmail-previous-same-subject)
bd1f0f84 547 )
5201ddda
RS
548\f
549(define-key rmail-mode-map [menu-bar] (make-sparse-keymap))
550
551(define-key rmail-mode-map [menu-bar classify]
552 (cons "Classify" (make-sparse-keymap "Classify")))
553
4139208e 554(define-key rmail-mode-map [menu-bar classify input-menu]
8b7ef584 555 nil)
4139208e
RS
556
557(define-key rmail-mode-map [menu-bar classify output-menu]
8b7ef584 558 nil)
4139208e 559
5eb9cf97
RS
560(define-key rmail-mode-map [menu-bar classify output-body]
561 '("Output body to file..." . rmail-output-body-to-file))
562
5201ddda 563(define-key rmail-mode-map [menu-bar classify output-inbox]
37fdc96f 564 '("Output (inbox)..." . rmail-output))
5201ddda
RS
565
566(define-key rmail-mode-map [menu-bar classify output]
37fdc96f 567 '("Output (Rmail)..." . rmail-output-to-rmail-file))
5201ddda
RS
568
569(define-key rmail-mode-map [menu-bar classify kill-label]
37fdc96f 570 '("Kill Label..." . rmail-kill-label))
5201ddda
RS
571
572(define-key rmail-mode-map [menu-bar classify add-label]
37fdc96f 573 '("Add Label..." . rmail-add-label))
5201ddda
RS
574
575(define-key rmail-mode-map [menu-bar summary]
576 (cons "Summary" (make-sparse-keymap "Summary")))
577
c0a5a948
RS
578(define-key rmail-mode-map [menu-bar summary senders]
579 '("By Senders..." . rmail-summary-by-senders))
580
5201ddda 581(define-key rmail-mode-map [menu-bar summary labels]
37fdc96f 582 '("By Labels..." . rmail-summary-by-labels))
5201ddda
RS
583
584(define-key rmail-mode-map [menu-bar summary recipients]
37fdc96f 585 '("By Recipients..." . rmail-summary-by-recipients))
5201ddda
RS
586
587(define-key rmail-mode-map [menu-bar summary topic]
37fdc96f 588 '("By Topic..." . rmail-summary-by-topic))
5201ddda
RS
589
590(define-key rmail-mode-map [menu-bar summary regexp]
37fdc96f 591 '("By Regexp..." . rmail-summary-by-regexp))
5201ddda
RS
592
593(define-key rmail-mode-map [menu-bar summary all]
594 '("All" . rmail-summary))
595
596(define-key rmail-mode-map [menu-bar mail]
597 (cons "Mail" (make-sparse-keymap "Mail")))
386c099f 598
5358bc42 599(define-key rmail-mode-map [menu-bar mail rmail-get-new-mail]
12dd87fc
RS
600 '("Get New Mail" . rmail-get-new-mail))
601
602(define-key rmail-mode-map [menu-bar mail lambda]
6ae1efb4 603 '("----"))
12dd87fc 604
5201ddda
RS
605(define-key rmail-mode-map [menu-bar mail continue]
606 '("Continue" . rmail-continue))
607
754b4eb7 608(define-key rmail-mode-map [menu-bar mail resend]
37fdc96f 609 '("Re-send..." . rmail-resend))
754b4eb7 610
5201ddda
RS
611(define-key rmail-mode-map [menu-bar mail forward]
612 '("Forward" . rmail-forward))
613
614(define-key rmail-mode-map [menu-bar mail retry]
615 '("Retry" . rmail-retry-failure))
616
617(define-key rmail-mode-map [menu-bar mail reply]
618 '("Reply" . rmail-reply))
619
620(define-key rmail-mode-map [menu-bar mail mail]
621 '("Mail" . rmail-mail))
622
623(define-key rmail-mode-map [menu-bar delete]
624 (cons "Delete" (make-sparse-keymap "Delete")))
625
626(define-key rmail-mode-map [menu-bar delete expunge/save]
627 '("Expunge/Save" . rmail-expunge-and-save))
581d7e0b 628
5201ddda
RS
629(define-key rmail-mode-map [menu-bar delete expunge]
630 '("Expunge" . rmail-expunge))
631
632(define-key rmail-mode-map [menu-bar delete undelete]
633 '("Undelete" . rmail-undelete-previous-message))
634
635(define-key rmail-mode-map [menu-bar delete delete]
636 '("Delete" . rmail-delete-forward))
637
638(define-key rmail-mode-map [menu-bar move]
639 (cons "Move" (make-sparse-keymap "Move")))
640
641(define-key rmail-mode-map [menu-bar move search-back]
342aa439 642 '("Search Back..." . rmail-search-backwards))
5201ddda
RS
643
644(define-key rmail-mode-map [menu-bar move search]
37fdc96f 645 '("Search..." . rmail-search))
5201ddda
RS
646
647(define-key rmail-mode-map [menu-bar move previous]
648 '("Previous Nondeleted" . rmail-previous-undeleted-message))
649
650(define-key rmail-mode-map [menu-bar move next]
651 '("Next Nondeleted" . rmail-next-undeleted-message))
652
653(define-key rmail-mode-map [menu-bar move last]
654 '("Last" . rmail-last-message))
655
656(define-key rmail-mode-map [menu-bar move first]
657 '("First" . rmail-first-message))
658
659(define-key rmail-mode-map [menu-bar move previous]
660 '("Previous" . rmail-previous-message))
661
662(define-key rmail-mode-map [menu-bar move next]
663 '("Next" . rmail-next-message))
664\f
581d7e0b
RM
665;; Rmail mode is suitable only for specially formatted data.
666(put 'rmail-mode 'mode-class 'special)
667
3f320f98
RS
668(defun rmail-mode-kill-summary ()
669 (if rmail-summary-buffer (kill-buffer rmail-summary-buffer)))
670
9712b0bd 671;;;###autoload
581d7e0b
RM
672(defun rmail-mode ()
673 "Rmail Mode is used by \\<rmail-mode-map>\\[rmail] for editing Rmail files.
674All normal editing commands are turned off.
675Instead, these commands are available:
676
677\\[rmail-beginning-of-message] Move point to front of this message (same as \\[beginning-of-buffer]).
678\\[scroll-up] Scroll to next screen of this message.
679\\[scroll-down] Scroll to previous screen of this message.
680\\[rmail-next-undeleted-message] Move to Next non-deleted message.
681\\[rmail-previous-undeleted-message] Move to Previous non-deleted message.
682\\[rmail-next-message] Move to Next message whether deleted or not.
683\\[rmail-previous-message] Move to Previous message whether deleted or not.
684\\[rmail-first-message] Move to the first message in Rmail file.
685\\[rmail-last-message] Move to the last message in Rmail file.
686\\[rmail-show-message] Jump to message specified by numeric position in file.
687\\[rmail-search] Search for string and show message it is found in.
688\\[rmail-delete-forward] Delete this message, move to next nondeleted.
689\\[rmail-delete-backward] Delete this message, move to previous nondeleted.
690\\[rmail-undelete-previous-message] Undelete message. Tries current message, then earlier messages
691 till a deleted message is found.
bd1f0f84 692\\[rmail-edit-current-message] Edit the current message. \\[rmail-cease-edit] to return to Rmail.
581d7e0b
RM
693\\[rmail-expunge] Expunge deleted messages.
694\\[rmail-expunge-and-save] Expunge and save the file.
695\\[rmail-quit] Quit Rmail: expunge, save, then switch to another buffer.
696\\[save-buffer] Save without expunging.
3af9db89 697\\[rmail-get-new-mail] Move new mail from system spool directory into this file.
581d7e0b
RM
698\\[rmail-mail] Mail a message (same as \\[mail-other-window]).
699\\[rmail-continue] Continue composing outgoing message started before.
bd1f0f84
RS
700\\[rmail-reply] Reply to this message. Like \\[rmail-mail] but initializes some fields.
701\\[rmail-retry-failure] Send this message again. Used on a mailer failure message.
581d7e0b
RM
702\\[rmail-forward] Forward this message to another user.
703\\[rmail-output-to-rmail-file] Output this message to an Rmail file (append it).
704\\[rmail-output] Output this message to a Unix-format mail file (append it).
705\\[rmail-input] Input Rmail file. Run Rmail on that file.
706\\[rmail-add-label] Add label to message. It will be displayed in the mode line.
707\\[rmail-kill-label] Kill label. Remove a label from current message.
708\\[rmail-next-labeled-message] Move to Next message with specified label
709 (label defaults to last one specified).
710 Standard labels: filed, unseen, answered, forwarded, deleted.
bd1f0f84 711 Any other label is present only if you add it with \\[rmail-add-label].
581d7e0b
RM
712\\[rmail-previous-labeled-message] Move to Previous message with specified label
713\\[rmail-summary] Show headers buffer, with a one line summary of each message.
bd1f0f84
RS
714\\[rmail-summary-by-labels] Summarize only messages with particular label(s).
715\\[rmail-summary-by-recipients] Summarize only messages with particular recipient(s).
716\\[rmail-summary-by-regexp] Summarize only messages with particular regexp(s).
717\\[rmail-summary-by-topic] Summarize only messages with subject line regexp(s).
718\\[rmail-toggle-header] Toggle display of complete header."
581d7e0b
RM
719 (interactive)
720 (rmail-mode-2)
721 (rmail-set-message-counters)
6af28e9f
RS
722 (rmail-show-message rmail-total-messages)
723 (run-hooks 'rmail-mode-hook))
581d7e0b
RM
724
725(defun rmail-mode-2 ()
726 (kill-all-local-variables)
727 (rmail-mode-1)
6af28e9f 728 (rmail-variables))
581d7e0b
RM
729
730(defun rmail-mode-1 ()
731 (setq major-mode 'rmail-mode)
732 (setq mode-name "RMAIL")
733 (setq buffer-read-only t)
876cd689
RS
734 ;; No need to auto save RMAIL files in normal circumstances
735 ;; because they contain no info except attribute changes
736 ;; and deletion of messages.
737 ;; The one exception is when messages are copied into an Rmail mode buffer.
738 ;; rmail-output-to-rmail-file enables auto save when you do that.
581d7e0b
RM
739 (setq buffer-auto-save-file-name nil)
740 (if (boundp 'mode-line-modified)
741 (setq mode-line-modified "--- ")
742 (setq mode-line-format
743 (cons "--- " (cdr (default-value 'mode-line-format)))))
744 (use-local-map rmail-mode-map)
745 (set-syntax-table text-mode-syntax-table)
746 (setq local-abbrev-table text-mode-abbrev-table))
747
748(defun rmail-variables ()
749 (make-local-variable 'revert-buffer-function)
750 (setq revert-buffer-function 'rmail-revert)
f512e4c0 751 (make-local-variable 'font-lock-defaults)
271c888a
SM
752 (setq font-lock-defaults
753 '(rmail-font-lock-keywords t nil nil nil
1080879c 754 (font-lock-maximum-size . nil)
f512e4c0
SM
755 (font-lock-fontify-buffer-function . rmail-fontify-buffer-function)
756 (font-lock-unfontify-buffer-function . rmail-unfontify-buffer-function)
757 (font-lock-inhibit-thing-lock . (lazy-lock-mode fast-lock-mode))))
581d7e0b 758 (make-local-variable 'rmail-last-label)
bd1f0f84 759 (make-local-variable 'rmail-last-regexp)
581d7e0b 760 (make-local-variable 'rmail-deleted-vector)
d1e69dec
KH
761 (make-local-variable 'rmail-buffer)
762 (setq rmail-buffer (current-buffer))
763 (make-local-variable 'rmail-view-buffer)
764 (setq rmail-view-buffer rmail-buffer)
581d7e0b
RM
765 (make-local-variable 'rmail-summary-buffer)
766 (make-local-variable 'rmail-summary-vector)
767 (make-local-variable 'rmail-current-message)
768 (make-local-variable 'rmail-total-messages)
769 (make-local-variable 'require-final-newline)
770 (setq require-final-newline nil)
24db549c
RS
771 (make-local-variable 'rmail-overlay-list)
772 (setq rmail-overlay-list nil)
581d7e0b
RM
773 (make-local-variable 'version-control)
774 (setq version-control 'never)
3f320f98
RS
775 (make-local-variable 'kill-buffer-hook)
776 (add-hook 'kill-buffer-hook 'rmail-mode-kill-summary)
581d7e0b
RM
777 (make-local-variable 'file-precious-flag)
778 (setq file-precious-flag t)
779 (make-local-variable 'rmail-message-vector)
581d7e0b
RM
780 (make-local-variable 'rmail-inbox-list)
781 (setq rmail-inbox-list (rmail-parse-file-inboxes))
849056cc
RS
782 ;; Provide default set of inboxes for primary mail file ~/RMAIL.
783 (and (null rmail-inbox-list)
6cd37f8f 784 (or (equal buffer-file-name (expand-file-name rmail-file-name))
a38837b5
RS
785 (equal buffer-file-truename
786 (abbreviate-file-name (file-truename rmail-file-name))))
849056cc
RS
787 (setq rmail-inbox-list
788 (or rmail-primary-inbox-list
789 (list (or (getenv "MAIL")
790 (concat rmail-spool-directory
d0eecffa 791 (user-login-name)))))))
581d7e0b
RM
792 (make-local-variable 'rmail-keywords)
793 ;; this gets generated as needed
6016579d 794 (setq rmail-keywords nil))
581d7e0b
RM
795
796;; Handle M-x revert-buffer done in an rmail-mode buffer.
797(defun rmail-revert (arg noconfirm)
a9db66a4 798 (let ((revert-buffer-function (default-value 'revert-buffer-function)))
581d7e0b
RM
799 ;; Call our caller again, but this time it does the default thing.
800 (if (revert-buffer arg noconfirm)
801 ;; If the user said "yes", and we changed something,
802 ;; reparse the messages.
803 (progn
804 (rmail-convert-file)
805 (goto-char (point-max))
091d1757 806 (rmail-mode)))))
581d7e0b
RM
807
808;; Return a list of files from this buffer's Mail: option.
809;; Does not assume that messages have been parsed.
810;; Just returns nil if buffer does not look like Babyl format.
811(defun rmail-parse-file-inboxes ()
812 (save-excursion
813 (save-restriction
814 (widen)
815 (goto-char 1)
816 (cond ((looking-at "BABYL OPTIONS:")
817 (search-forward "\n\^_" nil 'move)
818 (narrow-to-region 1 (point))
819 (goto-char 1)
820 (if (search-forward "\nMail:" nil t)
821 (progn
822 (narrow-to-region (point) (progn (end-of-line) (point)))
823 (goto-char (point-min))
824 (mail-parse-comma-list))))))))
825
826(defun rmail-expunge-and-save ()
827 "Expunge and save RMAIL file."
828 (interactive)
829 (rmail-expunge)
c7065819
KH
830 (save-buffer)
831 (if (rmail-summary-exists)
832 (rmail-select-summary (set-buffer-modified-p nil))))
581d7e0b
RM
833
834(defun rmail-quit ()
835 "Quit out of RMAIL."
836 (interactive)
837 (rmail-expunge-and-save)
838 ;; Don't switch to the summary buffer even if it was recently visible.
839 (if rmail-summary-buffer
21f69e7e
RS
840 (progn
841 (replace-buffer-in-windows rmail-summary-buffer)
842 (bury-buffer rmail-summary-buffer)))
581d7e0b 843 (let ((obuf (current-buffer)))
21f69e7e 844 (replace-buffer-in-windows obuf)
581d7e0b
RM
845 (bury-buffer obuf)))
846
a16aef15
RS
847(defun rmail-bury ()
848 "Bury current Rmail buffer and its summary buffer."
849 (interactive)
850 ;; This let var was called rmail-buffer, but that interfered
851 ;; with the buffer-local var used in summary buffers.
852 (let ((buffer-to-bury (current-buffer)))
853 (if (rmail-summary-exists)
854 (let (window)
855 (while (setq window (get-buffer-window rmail-summary-buffer))
856 (set-window-buffer window (other-buffer rmail-summary-buffer)))
857 (bury-buffer rmail-summary-buffer)))
858 (switch-to-buffer (other-buffer (current-buffer)))
859 (bury-buffer buffer-to-bury)))
860
861(defun rmail-duplicate-message ()
862 "Create a duplicated copy of the current message.
863The duplicate copy goes into the Rmail file just after the
864original copy."
865 (interactive)
866 (widen)
867 (let ((buffer-read-only nil)
868 (number rmail-current-message)
869 (string (buffer-substring (rmail-msgbeg rmail-current-message)
870 (rmail-msgend rmail-current-message))))
871 (goto-char (rmail-msgend rmail-current-message))
872 (insert string)
873 (rmail-forget-messages)
874 (rmail-show-message number)
875 (message "Message duplicated")))
876
581d7e0b
RM
877;;;###autoload
878(defun rmail-input (filename)
bd1f0f84 879 "Run Rmail on file FILENAME."
581d7e0b
RM
880 (interactive "FRun rmail on RMAIL file: ")
881 (rmail filename))
882
563ab60d
RS
883
884;; This used to scan subdirectories recursively, but someone pointed out
885;; that if the user wants that, person can put all the files in one dir.
886;; And the recursive scan was slow. So I took it out.
887;; rms, Sep 1996.
8b7ef584 888(defun rmail-find-all-files (start)
563ab60d 889 "Return list of file in dir START that match `rmail-secondary-file-regexp'."
8b7ef584 890 (if (file-accessible-directory-p start)
1529a12f 891 ;; Don't sort here.
563ab60d
RS
892 (let* ((case-fold-search t)
893 (files (directory-files start t rmail-secondary-file-regexp)))
1529a12f
RS
894 ;; Sort here instead of in directory-files
895 ;; because this list is usually much shorter.
563ab60d 896 (sort files 'string<))))
8b7ef584
RS
897
898(defun rmail-list-to-menu (menu-name l action &optional full-name)
899 (let ((menu (make-sparse-keymap menu-name)))
900 (mapcar
901 (function (lambda (item)
bc8661ca
RS
902 (let (command)
903 (if (consp item)
904 (progn
905 (setq command
906 (rmail-list-to-menu (car item) (cdr item)
907 action
908 (if full-name
909 (concat full-name "/"
910 (car item))
911 (car item))))
912 (setq name (car item)))
8b7ef584 913 (progn
bc8661ca
RS
914 (setq name item)
915 (setq command
916 (list 'lambda () '(interactive)
917 (list action
918 (expand-file-name
919 (if full-name
920 (concat full-name "/" item)
921 item)
922 rmail-secondary-file-directory))))))
923 (define-key menu (vector (intern name))
924 (cons name command)))))
1529a12f 925 (reverse l))
8b7ef584
RS
926 menu))
927
1529a12f
RS
928;; This command is always "disabled" when it appears in a menu.
929(put 'rmail-disable-menu 'menu-enable ''nil)
930
8b7ef584
RS
931(defun rmail-construct-io-menu ()
932 (let ((files (rmail-find-all-files rmail-secondary-file-directory)))
1529a12f 933 (if files
8b7ef584
RS
934 (progn
935 (define-key rmail-mode-map [menu-bar classify input-menu]
936 (cons "Input Rmail File"
937 (rmail-list-to-menu "Input Rmail File"
1529a12f 938 files
8b7ef584
RS
939 'rmail-input)))
940 (define-key rmail-mode-map [menu-bar classify output-menu]
941 (cons "Output Rmail File"
942 (rmail-list-to-menu "Output Rmail File"
1529a12f
RS
943 files
944 'rmail-output-to-rmail-file))))
945
946 (define-key rmail-mode-map [menu-bar classify input-menu]
947 '("Input Rmail File" . rmail-disable-menu))
948 (define-key rmail-mode-map [menu-bar classify output-menu]
949 '("Output Rmail File" . rmail-disable-menu)))))
90254bb0 950
581d7e0b
RM
951\f
952;;;; *** Rmail input ***
953
954;; RLK feature not added in this version:
955;; argument specifies inbox file or files in various ways.
956
957(defun rmail-get-new-mail (&optional file-name)
958 "Move any new mail from this RMAIL file's inbox files.
959The inbox files can be specified with the file's Mail: option. The
960variable `rmail-primary-inbox-list' specifies the inboxes for your
3af9db89
RS
961primary RMAIL file if it has no Mail: option. By default, this is
962your /usr/spool/mail/$USER.
581d7e0b
RM
963
964You can also specify the file to get new mail from. In this case, the
965file of new mail is not changed or deleted. Noninteractively, you can
966pass the inbox file name as an argument. Interactively, a prefix
38e76742
RS
967argument causes us to read a file name and use that file as the inbox.
968
4144e5cb
RS
969If the variable `rmail-preserve-inbox' is non-nil, new mail will
970always be left in inbox files rather than deleted.
971
bcddd52e
RS
972This function runs `rmail-get-new-mail-hook' before saving the updated file.
973It returns t if it got any new messages."
581d7e0b
RM
974 (interactive
975 (list (if current-prefix-arg
976 (read-file-name "Get new mail from file: "))))
1d08d733 977 (run-hooks 'rmail-before-get-new-mail-hook)
3f3495d3
RS
978 ;; If the disk file has been changed from under us,
979 ;; revert to it before we get new mail.
581d7e0b 980 (or (verify-visited-file-modtime (current-buffer))
3f3495d3 981 (find-file (buffer-file-name)))
581d7e0b
RM
982 (rmail-maybe-set-message-counters)
983 (widen)
984 ;; Get rid of all undo records for this buffer.
985 (or (eq buffer-undo-list t)
986 (setq buffer-undo-list nil))
563ab60d
RS
987 (let ((all-files (if file-name (list file-name)
988 rmail-inbox-list)))
989 (unwind-protect
9a10bd0d
KH
990 (let (found)
991 (while all-files
992 (let ((opoint (point))
993 (new-messages 0)
994 (delete-files ())
995 ;; If buffer has not changed yet, and has not been saved yet,
996 ;; don't replace the old backup file now.
997 (make-backup-files (and make-backup-files (buffer-modified-p)))
998 (buffer-read-only nil)
999 ;; Don't make undo records for what we do in getting mail.
1000 (buffer-undo-list t)
1001 success
1002 ;; Files to insert this time around.
1003 files
1004 ;; Last names of those files.
1005 file-last-names)
1006 ;; Pull files off all-files onto files
1007 ;; as long as there is no name conflict.
1008 ;; A conflict happens when two inbox file names
1009 ;; have the same last component.
1010 (while (and all-files
1011 (not (member (file-name-nondirectory (car all-files))
1012 file-last-names)))
1013 (setq files (cons (car all-files) files)
1014 file-last-names
1015 (cons (file-name-nondirectory (car all-files)) files))
1016 (setq all-files (cdr all-files)))
1017 ;; Put them back in their original order.
1018 (setq files (nreverse files))
1019
1020 (goto-char (point-max))
1021 (skip-chars-backward " \t\n") ; just in case of brain damage
1022 (delete-region (point) (point-max)) ; caused by require-final-newline
1023 (save-excursion
1024 (save-restriction
1025 (narrow-to-region (point) (point))
1026 ;; Read in the contents of the inbox files,
1027 ;; renaming them as necessary,
1028 ;; and adding to the list of files to delete eventually.
1029 (if file-name
1030 (rmail-insert-inbox-text files nil)
1031 (setq delete-files (rmail-insert-inbox-text files t)))
1032 ;; Scan the new text and convert each message to babyl format.
1033 (goto-char (point-min))
1034 (unwind-protect
1035 (save-excursion
1036 (setq new-messages (rmail-convert-to-babyl-format)
1037 success t))
1038 ;; If we could not convert the file's inboxes,
1039 ;; rename the files we tried to read
1040 ;; so we won't over and over again.
1041 (if (and (not file-name) (not success))
1042 (let ((delfiles delete-files)
1043 (count 0))
1044 (while delfiles
1045 (while (file-exists-p (format "RMAILOSE.%d" count))
1046 (setq count (1+ count)))
1047 (rename-file (car delfiles)
1048 (format "RMAILOSE.%d" count))
1049 (setq delfiles (cdr delfiles))))))
1050 (or (zerop new-messages)
1051 (let (success)
1052 (widen)
1053 (search-backward "\n\^_" nil t)
1054 (narrow-to-region (point) (point-max))
1055 (goto-char (1+ (point-min)))
1056 (rmail-count-new-messages)
1057 (run-hooks 'rmail-get-new-mail-hook)
1058 (save-buffer)))
1059 ;; Delete the old files, now that babyl file is saved.
1060 (while delete-files
1061 (condition-case ()
1062 ;; First, try deleting.
1063 (condition-case ()
1064 (delete-file (car delete-files))
1065 (file-error
1066 ;; If we can't delete it, truncate it.
1067 (write-region (point) (point) (car delete-files))))
1068 (file-error nil))
1069 (setq delete-files (cdr delete-files)))))
1070 (if (= new-messages 0)
1071 (progn (goto-char opoint)
1072 (if (or file-name rmail-inbox-list)
1073 (message "(No new mail has arrived)")))
1074 (if (rmail-summary-exists)
1075 (rmail-select-summary
1076 (rmail-update-summary)))
1077 (message "%d new message%s read"
1078 new-messages (if (= 1 new-messages) "" "s"))
1079 ;; Move to the first new message
1080 ;; unless we have other unseen messages before it.
1081 (rmail-show-message (rmail-first-unseen-message))
1082 (run-hooks 'rmail-after-get-new-mail-hook)
1083 (setq found t))))
1084 found)
563ab60d
RS
1085 ;; Don't leave the buffer screwed up if we get a disk-full error.
1086 (rmail-show-message))))
581d7e0b
RM
1087
1088(defun rmail-insert-inbox-text (files renamep)
563ab60d
RS
1089 ;; Detect a locked file now, so that we avoid moving mail
1090 ;; out of the real inbox file. (That could scare people.)
1091 (or (memq (file-locked-p buffer-file-name) '(nil t))
1092 (error "RMAIL file %s is locked"
1093 (file-name-nondirectory buffer-file-name)))
bd1f0f84 1094 (let (file tofile delete-files movemail popmail)
581d7e0b 1095 (while files
5170c9cb
KH
1096 (setq file (file-truename
1097 (expand-file-name (substitute-in-file-name (car files))))
ec3bba39
RS
1098 tofile (expand-file-name
1099 ;; Generate name to move to from inbox name,
1100 ;; in case of multiple inboxes that need moving.
1101 (concat ".newmail-" (file-name-nondirectory file))
1102 ;; Use the directory of this rmail file
1103 ;; because it's a nuisance to use the homedir
1104 ;; if that is on a full disk and this rmail
1105 ;; file isn't.
1106 (file-name-directory
1107 (expand-file-name buffer-file-name))))
1108 ;; Always use movemail to rename the file,
1109 ;; since there can be mailboxes in various directories.
1110 (setq movemail t)
1111;;; ;; If getting from mail spool directory,
1112;;; ;; use movemail to move rather than just renaming,
1113;;; ;; so as to interlock with the mailer.
1114;;; (setq movemail (string= file
1115;;; (file-truename
1116;;; (concat rmail-spool-directory
1117;;; (file-name-nondirectory file)))))
1118 (setq popmail (string-match "^po:" (file-name-nondirectory file)))
bd1f0f84
RS
1119 (if popmail (setq file (file-name-nondirectory file)
1120 renamep t))
581d7e0b
RM
1121 (if movemail
1122 (progn
581d7e0b
RM
1123 ;; On some systems, /usr/spool/mail/foo is a directory
1124 ;; and the actual inbox is /usr/spool/mail/foo/foo.
1125 (if (file-directory-p file)
d0eecffa 1126 (setq file (expand-file-name (user-login-name)
581d7e0b 1127 file)))))
8095bf23 1128 (cond (popmail
ceeb471c
RS
1129 (if (and rmail-pop-password-required (not rmail-pop-password))
1130 (setq rmail-pop-password
1131 (rmail-read-passwd
1132 (format "Password for %s: "
1133 (substring file (+ popmail 3))))))
1134 (if (eq system-type 'windows-nt)
1135 ;; cannot have "po:" in file name
1136 (setq tofile
1137 (expand-file-name
1138 (concat ".newmail-pop-" (substring file (+ popmail 3)))
1139 (file-name-directory
1140 (expand-file-name buffer-file-name)))))
8095bf23
RS
1141 (message "Getting mail from post office ..."))
1142 ((and (file-exists-p tofile)
1143 (/= 0 (nth 7 (file-attributes tofile))))
1144 (message "Getting mail from %s..." tofile))
1145 ((and (file-exists-p file)
1146 (/= 0 (nth 7 (file-attributes file))))
1147 (message "Getting mail from %s..." file)))
581d7e0b
RM
1148 ;; Set TOFILE if have not already done so, and
1149 ;; rename or copy the file FILE to TOFILE if and as appropriate.
1150 (cond ((not renamep)
1151 (setq tofile file))
bd1f0f84
RS
1152 ((or (file-exists-p tofile) (and (not popmail)
1153 (not (file-exists-p file))))
581d7e0b 1154 nil)
bd1f0f84 1155 ((and (not movemail) (not popmail))
4144e5cb
RS
1156 ;; Try copying. If that fails (perhaps no space) and
1157 ;; we're allowed to blow away the inbox, rename instead.
1158 (if rmail-preserve-inbox
019bf2a3 1159 (copy-file file tofile nil)
4144e5cb
RS
1160 (condition-case nil
1161 (copy-file file tofile nil)
1162 (error
1163 ;; Third arg is t so we can replace existing file TOFILE.
1164 (rename-file file tofile t))))
ba0b5507
RS
1165 ;; Make the real inbox file empty.
1166 ;; Leaving it deleted could cause lossage
1167 ;; because mailers often won't create the file.
4144e5cb
RS
1168 (if (not rmail-preserve-inbox)
1169 (condition-case ()
1170 (write-region (point) (point) file)
1171 (file-error nil))))
581d7e0b
RM
1172 (t
1173 (let ((errors nil))
1174 (unwind-protect
1175 (save-excursion
1176 (setq errors (generate-new-buffer " *rmail loss*"))
1177 (buffer-disable-undo errors)
4144e5cb
RS
1178 (let ((args
1179 (append
1180 (list (or rmail-movemail-program
1181 (expand-file-name "movemail"
1182 exec-directory))
1183 nil errors nil)
1184 (if rmail-preserve-inbox
1185 (list "-p")
1186 nil)
1187 (list file tofile)
1188 (if rmail-pop-password
1189 (list rmail-pop-password)
1190 nil))))
1191 (apply 'call-process args))
581d7e0b
RM
1192 (if (not (buffer-modified-p errors))
1193 ;; No output => movemail won
1194 nil
1195 (set-buffer errors)
1196 (subst-char-in-region (point-min) (point-max)
1197 ?\n ?\ )
1198 (goto-char (point-max))
1199 (skip-chars-backward " \t")
1200 (delete-region (point) (point-max))
1201 (goto-char (point-min))
1202 (if (looking-at "movemail: ")
1203 (delete-region (point-min) (match-end 0)))
1204 (beep t)
1157e1bc
KH
1205 (message "movemail: %s"
1206 (buffer-substring (point-min)
1207 (point-max)))
581d7e0b
RM
1208 (sit-for 3)
1209 nil))
1210 (if errors (kill-buffer errors))))))
1211 ;; At this point, TOFILE contains the name to read:
1212 ;; Either the alternate name (if we renamed)
1213 ;; or the actual inbox (if not renaming).
1214 (if (file-exists-p tofile)
d1e69dec
KH
1215 (let ((coding-system-for-read 'no-conversion)
1216 size)
581d7e0b 1217 (goto-char (point-max))
d1e69dec 1218 (setq size (nth 1 (insert-file-contents tofile)))
581d7e0b
RM
1219 (goto-char (point-max))
1220 (or (= (preceding-char) ?\n)
1221 (zerop size)
1222 (insert ?\n))
4144e5cb
RS
1223 (if (not (and rmail-preserve-inbox (string= file tofile)))
1224 (setq delete-files (cons tofile delete-files)))))
581d7e0b
RM
1225 (message "")
1226 (setq files (cdr files)))
1227 delete-files))
1228
ceeb471c
RS
1229(defun rmail-read-passwd (prompt &optional default)
1230 "Read a password, echoing `.' for each character typed.
1231End with RET, LFD, or ESC. DEL or C-h rubs out. C-u kills line.
1232Optional DEFAULT is password to start with."
1233 (let ((pass (if default default ""))
1234 (c 0)
1235 (echo-keystrokes 0)
1236 (cursor-in-echo-area t))
1237 (while (progn (message "%s%s"
1238 prompt
1239 (make-string (length pass) ?.))
1240 (setq c (read-char))
1241 (and (/= c ?\r) (/= c ?\n) (/= c ?\e)))
1242 (if (= c ?\C-u)
1243 (setq pass "")
1244 (if (and (/= c ?\b) (/= c ?\177))
1245 (setq pass (concat pass (char-to-string c)))
1246 (if (> (length pass) 0)
1247 (setq pass (substring pass 0 -1))))))
1248 (message "")
1249 (message nil)
1250 pass))
1251
581d7e0b
RM
1252;; the rmail-break-forwarded-messages feature is not implemented
1253(defun rmail-convert-to-babyl-format ()
1254 (let ((count 0) start
b77ab9e8
RS
1255 (case-fold-search nil)
1256 (invalid-input-resync
1257 (function (lambda ()
1258 (message "Invalid Babyl format in inbox!")
563ab60d 1259 (sit-for 3)
b77ab9e8
RS
1260 ;; Try to get back in sync with a real message.
1261 (if (re-search-forward
1262 (concat mmdf-delim1 "\\|^From") nil t)
1263 (beginning-of-line)
1264 (goto-char (point-max)))))))
581d7e0b
RM
1265 (goto-char (point-min))
1266 (save-restriction
1267 (while (not (eobp))
d1e69dec 1268 (setq start (point))
581d7e0b 1269 (cond ((looking-at "BABYL OPTIONS:");Babyl header
b77ab9e8
RS
1270 (if (search-forward "\n\^_" nil t)
1271 ;; If we find the proper terminator, delete through there.
1272 (delete-region (point-min) (point))
1273 (funcall invalid-input-resync)
a553316b 1274 (delete-region (point-min) (point))))
581d7e0b
RM
1275 ;; Babyl format message
1276 ((looking-at "\^L")
1277 (or (search-forward "\n\^_" nil t)
b77ab9e8 1278 (funcall invalid-input-resync))
581d7e0b
RM
1279 (setq count (1+ count))
1280 ;; Make sure there is no extra white space after the ^_
1281 ;; at the end of the message.
1282 ;; Narrowing will make sure that whatever follows the junk
1283 ;; will be treated properly.
1284 (delete-region (point)
1285 (save-excursion
1286 (skip-chars-forward " \t\n")
1287 (point)))
d1e69dec
KH
1288 (or rmail-enable-mime
1289 (not rmail-file-coding-system)
1290 (decode-coding-region start (point)
1291 rmail-file-coding-system))
581d7e0b
RM
1292 (narrow-to-region (point) (point-max)))
1293 ;;*** MMDF format
1294 ((let ((case-fold-search t))
1295 (looking-at mmdf-delim1))
1296 (let ((case-fold-search t))
1297 (replace-match "\^L\n0, unseen,,\n*** EOOH ***\n")
581d7e0b
RM
1298 (re-search-forward mmdf-delim2 nil t)
1299 (replace-match "\^_"))
1300 (save-excursion
1301 (save-restriction
1302 (narrow-to-region start (1- (point)))
1303 (goto-char (point-min))
1304 (while (search-forward "\n\^_" nil t); single char "\^_"
1305 (replace-match "\n^_")))); 2 chars: "^" and "_"
d1e69dec
KH
1306 (or rmail-enable-mime
1307 (not rmail-file-coding-system)
1308 (decode-coding-region start (point) 'automatic-conversion))
581d7e0b
RM
1309 (narrow-to-region (point) (point-max))
1310 (setq count (1+ count)))
1311 ;;*** Mail format
1312 ((looking-at "^From ")
581d7e0b
RM
1313 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
1314 (rmail-nuke-pinhead-header)
bd1f0f84
RS
1315 ;; If this message has a Content-Length field,
1316 ;; skip to the end of the contents.
1317 (let* ((header-end (save-excursion
1318 (and (re-search-forward "\n\n" nil t)
a865e55b 1319 (1- (point)))))
bd1f0f84
RS
1320 (case-fold-search t)
1321 (size
1322 ;; Get the numeric value from the Content-Length field.
1323 (save-excursion
1324 ;; Back up to end of prev line,
1325 ;; in case the Content-Length field comes first.
1326 (forward-char -1)
1327 (and (search-forward "\ncontent-length: "
1328 header-end t)
1329 (let ((beg (point))
1330 (eol (progn (end-of-line) (point))))
5458f7f1
KH
1331 (string-to-int (buffer-substring beg eol)))))))
1332 (and size
1333 (if (and (natnump size)
1334 (<= (+ header-end size) (point-max))
1335 ;; Make sure this would put us at a position
1336 ;; that we could continue from.
1337 (save-excursion
1338 (goto-char (+ header-end size))
1339 (skip-chars-forward "\n")
1340 (or (eobp)
1341 (and (looking-at "BABYL OPTIONS:")
1342 (search-forward "\n\^_" nil t))
1343 (and (looking-at "\^L")
1344 (search-forward "\n\^_" nil t))
1345 (let ((case-fold-search t))
1346 (looking-at mmdf-delim1))
1347 (looking-at "From "))))
1348 (goto-char (+ header-end size))
1349 (message "Ignoring invalid Content-Length field")
1350 (sit-for 1 0 t))))
bd1f0f84 1351
581d7e0b
RM
1352 (if (re-search-forward
1353 (concat "^[\^_]?\\("
46947372
JB
1354 rmail-unix-mail-delimiter
1355 "\\|"
581d7e0b
RM
1356 mmdf-delim1 "\\|"
1357 "^BABYL OPTIONS:\\|"
1358 "\^L\n[01],\\)") nil t)
1359 (goto-char (match-beginning 1))
1360 (goto-char (point-max)))
1361 (setq count (1+ count))
1362 (save-excursion
1363 (save-restriction
1364 (narrow-to-region start (point))
1365 (goto-char (point-min))
1366 (while (search-forward "\n\^_" nil t); single char
1367 (replace-match "\n^_")))); 2 chars: "^" and "_"
1368 (insert ?\^_)
d1e69dec
KH
1369 (or rmail-enable-mime
1370 (not rmail-file-coding-system)
1371 (decode-coding-region start (point) 'automatic-conversion))
581d7e0b
RM
1372 (narrow-to-region (point) (point-max)))
1373 ;;
8896f2df
RS
1374 ;; This kludge is because some versions of sendmail.el
1375 ;; insert an extra newline at the beginning that shouldn't
1376 ;; be there. sendmail.el has been fixed, but old versions
1377 ;; may still be in use. -- rms, 7 May 1993.
1378 ((eolp) (delete-char 1))
a553316b 1379 (t (error "Cannot convert to babyl format")))))
581d7e0b
RM
1380 count))
1381
1382;; Delete the "From ..." line, creating various other headers with
1383;; information from it if they don't already exist. Now puts the
c42722e3
RS
1384;; original line into a mail-from: header line for debugging and for
1385;; use by the rmail-output function.
581d7e0b
RM
1386(defun rmail-nuke-pinhead-header ()
1387 (save-excursion
1388 (save-restriction
1389 (let ((start (point))
1390 (end (progn
1391 (condition-case ()
1392 (search-forward "\n\n")
1393 (error
1394 (goto-char (point-max))
1395 (insert "\n\n")))
1396 (point)))
1397 has-from has-date)
1398 (narrow-to-region start end)
1399 (let ((case-fold-search t))
1400 (goto-char start)
1401 (setq has-from (search-forward "\nFrom:" nil t))
1402 (goto-char start)
1403 (setq has-date (and (search-forward "\nDate:" nil t) (point)))
1404 (goto-char start))
1405 (let ((case-fold-search nil))
46947372 1406 (if (re-search-forward (concat "^" rmail-unix-mail-delimiter) nil t)
581d7e0b
RM
1407 (replace-match
1408 (concat
1409 "Mail-from: \\&"
1410 ;; Keep and reformat the date if we don't
1411 ;; have a Date: field.
1412 (if has-date
1413 ""
b7cceaf1 1414 (concat
74d6e31c 1415 "Date: \\2, \\4 \\3 \\9 \\5 "
b7cceaf1
JB
1416
1417 ;; The timezone could be matched by group 7 or group 10.
1418 ;; If neither of them matched, assume EST, since only
1419 ;; Easterners would be so sloppy.
1420 ;; It's a shame the substitution can't use "\\10".
1421 (cond
1422 ((/= (match-beginning 7) (match-end 7)) "\\7")
1423 ((/= (match-beginning 10) (match-end 10))
1424 (buffer-substring (match-beginning 10)
1425 (match-end 10)))
1426 (t "EST"))
1427 "\n"))
581d7e0b
RM
1428 ;; Keep and reformat the sender if we don't
1429 ;; have a From: field.
1430 (if has-from
1431 ""
15a36ac5
RS
1432 "From: \\1\n"))
1433 t)))))))
581d7e0b
RM
1434\f
1435;;;; *** Rmail Message Formatting and Header Manipulation ***
1436
1437(defun rmail-reformat-message (beg end)
1438 (goto-char beg)
1439 (forward-line 1)
1440 (if (/= (following-char) ?0)
1441 (error "Bad format in RMAIL file."))
1442 (let ((buffer-read-only nil)
1443 (delta (- (buffer-size) end)))
1444 (delete-char 1)
1445 (insert ?1)
1446 (forward-line 1)
15a36ac5 1447 (let ((case-fold-search t))
c42722e3
RS
1448 (while (looking-at "Summary-line:\\|Mail-From:")
1449 (forward-line 1)))
581d7e0b
RM
1450 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*\n")
1451 (delete-region (point)
1452 (progn (forward-line 1) (point))))
1453 (let ((str (buffer-substring (point)
1454 (save-excursion (search-forward "\n\n" end 'move)
1455 (point)))))
1456 (insert str "*** EOOH ***\n")
1457 (narrow-to-region (point) (- (buffer-size) delta)))
1458 (goto-char (point-min))
423c0dfa 1459 (if rmail-message-filter (funcall rmail-message-filter))
8095bf23
RS
1460 (if (or rmail-displayed-headers rmail-ignored-headers)
1461 (rmail-clear-headers))))
581d7e0b 1462
3db0cdac 1463(defun rmail-clear-headers (&optional ignored-headers)
8095bf23
RS
1464 "Delete all header fields that Rmail should not show.
1465If the optional argument IGNORED-HEADERS is non-nil,
1466delete all header fields whose names match that regexp.
1467Otherwise, if `rmail-displayed-headers' is non-nil,
1468delete all header fields *except* those whose names match that regexp.
1469Otherwise, delete all header fields whose names match `rmail-ignored-headers'."
581d7e0b 1470 (if (search-forward "\n\n" nil t)
88293b57
EN
1471 (let ((case-fold-search t)
1472 (buffer-read-only nil))
1473 (if (and rmail-displayed-headers (null ignored-headers))
1474 (save-restriction
1475 (narrow-to-region (point-min) (point))
1476 (let (lim)
1477 (goto-char (point-min))
1478 (while (save-excursion
1479 (re-search-forward "\n[^ \t]")
1480 (and (not (eobp))
1481 (setq lim (1- (point)))))
1482 (if (save-excursion
1483 (re-search-forward rmail-displayed-headers lim t))
1484 (goto-char lim)
1485 (delete-region (point) lim))))
1486 (goto-char (point-min)))
1487 (or ignored-headers (setq ignored-headers rmail-ignored-headers))
8095bf23
RS
1488 (save-restriction
1489 (narrow-to-region (point-min) (point))
88293b57 1490 (while (progn
8095bf23
RS
1491 (goto-char (point-min))
1492 (re-search-forward ignored-headers nil t))
1493 (beginning-of-line)
1494 (delete-region (point)
1495 (progn (re-search-forward "\n[^ \t]")
1496 (1- (point))))))))))
581d7e0b 1497
dbf71ee9 1498(defun rmail-msg-is-pruned ()
581d7e0b 1499 (rmail-maybe-set-message-counters)
dbf71ee9
RS
1500 (save-restriction
1501 (save-excursion
581d7e0b 1502 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
581d7e0b
RM
1503 (goto-char (point-min))
1504 (forward-line 1)
dbf71ee9
RS
1505 (= (following-char) ?1))))
1506
1507(defun rmail-toggle-header (&optional arg)
1508 "Show original message header if pruned header currently shown, or vice versa.
1509With argument ARG, show the message header pruned if ARG is greater than zero;
1510otherwise, show it in full."
1511 (interactive "P")
1512 (let* ((buffer-read-only nil)
1513 (pruned (rmail-msg-is-pruned))
1514 (prune (if arg
1515 (> (prefix-numeric-value arg) 0)
1516 (not pruned))))
1517 (if (eq pruned prune)
1518 t
1519 (rmail-maybe-set-message-counters)
1520 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
1521 (if pruned
1522 (progn (goto-char (point-min))
1523 (forward-line 1)
1524 (delete-char 1)
1525 (insert ?0)
1526 (forward-line 1)
1527 (let ((case-fold-search t))
1528 (while (looking-at "Summary-Line:\\|Mail-From:")
1529 (forward-line 1)))
1530 (insert "*** EOOH ***\n")
1531 (forward-char -1)
1532 (search-forward "\n*** EOOH ***\n")
1533 (forward-line -1)
1534 (let ((temp (point)))
1535 (and (search-forward "\n\n" nil t)
1536 (delete-region temp (point))))
1537 (goto-char (point-min))
1538 (search-forward "\n*** EOOH ***\n")
1539 (narrow-to-region (point) (point-max)))
1540 (rmail-reformat-message (point-min) (point-max)))
1541 (rmail-highlight-headers))))
581d7e0b
RM
1542\f
1543;;;; *** Rmail Attributes and Keywords ***
1544
1545;; Make a string describing current message's attributes and keywords
1546;; and set it up as the name of a minor mode
1547;; so it will appear in the mode line.
1548(defun rmail-display-labels ()
1549 (let ((blurb "") (beg (point-min-marker)) (end (point-max-marker)))
1550 (save-excursion
1551 (unwind-protect
1552 (progn
1553 (widen)
1554 (goto-char (rmail-msgbeg rmail-current-message))
1555 (forward-line 1)
1556 (if (looking-at "[01],")
1557 (progn
1558 (narrow-to-region (point) (progn (end-of-line) (point)))
1559 ;; Truly valid BABYL format requires a space before each
1560 ;; attribute or keyword name. Put them in if missing.
1561 (let (buffer-read-only)
1562 (goto-char (point-min))
1563 (while (search-forward "," nil t)
1564 (or (looking-at "[ ,]") (eobp)
1565 (insert " "))))
1566 (goto-char (point-max))
1567 (if (search-backward ",," nil 'move)
1568 (progn
1569 (if (> (point) (1+ (point-min)))
1570 (setq blurb (buffer-substring (+ 1 (point-min)) (point))))
1571 (if (> (- (point-max) (point)) 2)
1572 (setq blurb
1573 (concat blurb
1574 ";"
1575 (buffer-substring (+ (point) 3)
1576 (1- (point-max)))))))))))
1577 ;; Note: we don't use save-restriction because that does not work right
1578 ;; if changes are made outside the saved restriction
1579 ;; before that restriction is restored.
1580 (narrow-to-region beg end)
1581 (set-marker beg nil)
1582 (set-marker end nil)))
1583 (while (string-match " +," blurb)
1584 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
1585 (substring blurb (match-end 0)))))
1586 (while (string-match ", +" blurb)
1587 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
1588 (substring blurb (match-end 0)))))
1589 (setq mode-line-process
8095bf23
RS
1590 (format " %d/%d%s"
1591 rmail-current-message rmail-total-messages blurb))))
581d7e0b
RM
1592
1593;; Turn an attribute of a message on or off according to STATE.
1594;; ATTR is the name of the attribute, as a string.
1595;; MSGNUM is message number to change; nil means current message.
1596(defun rmail-set-attribute (attr state &optional msgnum)
1597 (let ((omax (point-max-marker))
1598 (omin (point-min-marker))
1599 (buffer-read-only nil))
1600 (or msgnum (setq msgnum rmail-current-message))
708bfd4f
RS
1601 (if (> msgnum 0)
1602 (unwind-protect
1603 (save-excursion
1604 (widen)
1605 (goto-char (+ 3 (rmail-msgbeg msgnum)))
1606 (let ((curstate
1607 (not
1608 (null (search-backward (concat ", " attr ",")
1609 (prog1 (point) (end-of-line)) t)))))
1610 (or (eq curstate (not (not state)))
1611 (if curstate
1612 (delete-region (point) (1- (match-end 0)))
1613 (beginning-of-line)
1614 (forward-char 2)
1615 (insert " " attr ","))))
1616 (if (string= attr "deleted")
1617 (rmail-set-message-deleted-p msgnum state)))
1618 ;; Note: we don't use save-restriction because that does not work right
1619 ;; if changes are made outside the saved restriction
1620 ;; before that restriction is restored.
1621 (narrow-to-region omin omax)
1622 (set-marker omin nil)
1623 (set-marker omax nil)
1624 (if (= msgnum rmail-current-message)
1625 (rmail-display-labels))))))
581d7e0b
RM
1626
1627;; Return t if the attributes/keywords line of msg number MSG
1628;; contains a match for the regexp LABELS.
1629(defun rmail-message-labels-p (msg labels)
1630 (save-excursion
1631 (save-restriction
1632 (widen)
1633 (goto-char (rmail-msgbeg msg))
1634 (forward-char 3)
1635 (re-search-backward labels (prog1 (point) (end-of-line)) t))))
1636\f
1637;;;; *** Rmail Message Selection And Support ***
1638
1639(defun rmail-msgend (n)
1640 (marker-position (aref rmail-message-vector (1+ n))))
1641
1642(defun rmail-msgbeg (n)
1643 (marker-position (aref rmail-message-vector n)))
1644
1645(defun rmail-widen-to-current-msgbeg (function)
1646 "Call FUNCTION with point at start of internal data of current message.
1647Assumes that bounds were previously narrowed to display the message in Rmail.
1648The bounds are widened enough to move point where desired, then narrowed
1649again afterward.
1650
1651FUNCTION may not change the visible text of the message, but it may
1652change the invisible header text."
1653 (save-excursion
c462c964 1654 (let ((obeg (- (point-max) (point-min))))
581d7e0b
RM
1655 (unwind-protect
1656 (progn
1657 (narrow-to-region (rmail-msgbeg rmail-current-message)
1658 (point-max))
1659 (goto-char (point-min))
1660 (funcall function))
1661 ;; Note: we don't use save-restriction because that does not work right
1662 ;; if changes are made outside the saved restriction
1663 ;; before that restriction is restored.
1664 ;; Here we assume that changes made by FUNCTION
1665 ;; occur before the visible region of the message.
c462c964 1666 (narrow-to-region (- (point-max) obeg) (point-max))))))
581d7e0b
RM
1667
1668(defun rmail-forget-messages ()
1669 (unwind-protect
1670 (if (vectorp rmail-message-vector)
1671 (let* ((i 0)
1672 (v rmail-message-vector)
1673 (n (length v)))
1674 (while (< i n)
1675 (move-marker (aref v i) nil)
1676 (setq i (1+ i)))))
1677 (setq rmail-message-vector nil)
1678 (setq rmail-deleted-vector nil)))
1679
1680(defun rmail-maybe-set-message-counters ()
1681 (if (not (and rmail-deleted-vector
1682 rmail-message-vector
1683 rmail-current-message
1684 rmail-total-messages))
1685 (rmail-set-message-counters)))
1686
1687(defun rmail-count-new-messages (&optional nomsg)
1688 (let* ((case-fold-search nil)
1689 (total-messages 0)
1690 (messages-head nil)
1691 (deleted-head nil))
1692 (or nomsg (message "Counting new messages..."))
1693 (goto-char (point-max))
1694 ;; Put at the end of messages-head
1695 ;; the entry for message N+1, which marks
1696 ;; the end of message N. (N = number of messages).
1697 (search-backward "\n\^_")
1698 (forward-char 1)
1699 (setq messages-head (list (point-marker)))
1700 (rmail-set-message-counters-counter (point-min))
1701 (setq rmail-current-message (1+ rmail-total-messages))
1702 (setq rmail-total-messages
1703 (+ rmail-total-messages total-messages))
1704 (setq rmail-message-vector
1705 (vconcat rmail-message-vector (cdr messages-head)))
1706 (aset rmail-message-vector
1707 rmail-current-message (car messages-head))
1708 (setq rmail-deleted-vector
1709 (concat rmail-deleted-vector deleted-head))
1710 (setq rmail-summary-vector
1711 (vconcat rmail-summary-vector (make-vector total-messages nil)))
1712 (goto-char (point-min))
1713 (or nomsg (message "Counting new messages...done (%d)" total-messages))))
1714
1715(defun rmail-set-message-counters ()
1716 (rmail-forget-messages)
1717 (save-excursion
1718 (save-restriction
1719 (widen)
1720 (let* ((point-save (point))
1721 (total-messages 0)
1722 (messages-after-point)
1723 (case-fold-search nil)
1724 (messages-head nil)
1725 (deleted-head nil))
1726 (message "Counting messages...")
1727 (goto-char (point-max))
1728 ;; Put at the end of messages-head
1729 ;; the entry for message N+1, which marks
1730 ;; the end of message N. (N = number of messages).
cf805d5d
RS
1731 (search-backward "\n\^_" nil t)
1732 (if (/= (point) (point-max)) (forward-char 1))
581d7e0b
RM
1733 (setq messages-head (list (point-marker)))
1734 (rmail-set-message-counters-counter (min (point) point-save))
1735 (setq messages-after-point total-messages)
1736 (rmail-set-message-counters-counter)
1737 (setq rmail-total-messages total-messages)
1738 (setq rmail-current-message
1739 (min total-messages
1740 (max 1 (- total-messages messages-after-point))))
1741 (setq rmail-message-vector
1742 (apply 'vector (cons (point-min-marker) messages-head))
1743 rmail-deleted-vector (concat "D" deleted-head)
1744 rmail-summary-vector (make-vector rmail-total-messages nil))
1745 (message "Counting messages...done")))))
1746
1747(defun rmail-set-message-counters-counter (&optional stop)
1748 (while (search-backward "\n\^_\^L\n" stop t)
1749 (forward-char 1)
1750 (setq messages-head (cons (point-marker) messages-head))
1751 (save-excursion
1752 (setq deleted-head
1753 (cons (if (search-backward ", deleted,"
1754 (prog1 (point)
1755 (forward-line 2))
1756 t)
1757 ?D ?\ )
1758 deleted-head)))
1759 (if (zerop (% (setq total-messages (1+ total-messages)) 20))
1760 (message "Counting messages...%d" total-messages))))
1761
1762(defun rmail-beginning-of-message ()
1763 "Show current message starting from the beginning."
1764 (interactive)
1765 (rmail-show-message rmail-current-message))
1766
bdf03600 1767(defun rmail-show-message (&optional n no-summary)
bd1f0f84
RS
1768 "Show message number N (prefix argument), counting from start of file.
1769If summary buffer is currently displayed, update current message there also."
581d7e0b 1770 (interactive "p")
d1e69dec
KH
1771 (or (eq major-mode 'rmail-mode)
1772 (switch-to-buffer rmail-buffer))
581d7e0b
RM
1773 (rmail-maybe-set-message-counters)
1774 (widen)
1775 (if (zerop rmail-total-messages)
1776 (progn (narrow-to-region (point-min) (1- (point-max)))
1777 (goto-char (point-min))
1778 (setq mode-line-process nil))
1779 (let (blurb)
1780 (if (not n)
1781 (setq n rmail-current-message)
1782 (cond ((<= n 0)
1783 (setq n 1
1784 rmail-current-message 1
1785 blurb "No previous message"))
1786 ((> n rmail-total-messages)
1787 (setq n rmail-total-messages
1788 rmail-current-message rmail-total-messages
1789 blurb "No following message"))
1790 (t
1791 (setq rmail-current-message n))))
563ab60d 1792 (let ((beg (rmail-msgbeg n)))
581d7e0b
RM
1793 (goto-char beg)
1794 (forward-line 1)
563ab60d
RS
1795 ;; Clear the "unseen" attribute when we show a message.
1796 (rmail-set-attribute "unseen" nil)
1797 ;; Reformat the header, or else find the reformatted header.
1798 (let ((end (rmail-msgend n)))
1799 (if (= (following-char) ?0)
581d7e0b 1800 (rmail-reformat-message beg end)
563ab60d
RS
1801 (search-forward "\n*** EOOH ***\n" end t)
1802 (narrow-to-region (point) end)))
581d7e0b
RM
1803 (goto-char (point-min))
1804 (rmail-display-labels)
d1e69dec
KH
1805 (if (eq rmail-enable-mime t)
1806 (funcall rmail-show-mime-function)
1807 (setq rmail-view-buffer rmail-buffer)
1808 )
99887e16 1809 (rmail-highlight-headers)
d43277ab 1810 (if transient-mark-mode (deactivate-mark))
581d7e0b 1811 (run-hooks 'rmail-show-message-hook)
bd1f0f84
RS
1812 ;; If there is a summary buffer, try to move to this message
1813 ;; in that buffer. But don't complain if this message
1814 ;; is not mentioned in the summary.
bdf03600
RS
1815 ;; Don't do this at all if we were called on behalf
1816 ;; of cursor motion in the summary buffer.
1817 (and (rmail-summary-exists) (not no-summary)
1818 (let ((curr-msg rmail-current-message))
1819 (rmail-select-summary
1820 (rmail-summary-goto-msg curr-msg t t))))
581d7e0b
RM
1821 (if blurb
1822 (message blurb))))))
1823
99887e16
KH
1824;; Find all occurrences of certain fields, and highlight them.
1825(defun rmail-highlight-headers ()
1826 ;; Do this only if the system supports faces.
f9a9d26e
RS
1827 (if (and (fboundp 'internal-find-face)
1828 rmail-highlighted-headers)
99887e16
KH
1829 (save-excursion
1830 (search-forward "\n\n" nil 'move)
1831 (save-restriction
1832 (narrow-to-region (point-min) (point))
1833 (let ((case-fold-search t)
1834 (inhibit-read-only t)
1835 ;; Highlight with boldface if that is available.
1836 ;; Otherwise use the `highlight' face.
3df6812a
RS
1837 (face (or rmail-highlight-face
1838 (if (face-differs-from-default-p 'bold)
1839 'bold 'highlight)))
99887e16
KH
1840 ;; List of overlays to reuse.
1841 (overlays rmail-overlay-list))
1842 (goto-char (point-min))
1843 (while (re-search-forward rmail-highlighted-headers nil t)
92e06883 1844 (skip-chars-forward " \t")
99887e16
KH
1845 (let ((beg (point))
1846 overlay)
1847 (while (progn (forward-line 1)
1848 (looking-at "[ \t]")))
1849 ;; Back up over newline, then trailing spaces or tabs
1850 (forward-char -1)
1851 (while (member (preceding-char) '(? ?\t))
1852 (forward-char -1))
1853 (if overlays
1854 ;; Reuse an overlay we already have.
1855 (progn
1856 (setq overlay (car overlays)
1857 overlays (cdr overlays))
1858 (overlay-put overlay 'face face)
1859 (move-overlay overlay beg (point)))
1860 ;; Make a new overlay and add it to
1861 ;; rmail-overlay-list.
1862 (setq overlay (make-overlay beg (point)))
1863 (overlay-put overlay 'face face)
1864 (setq rmail-overlay-list
1865 (cons overlay rmail-overlay-list))))))))))
1866
581d7e0b
RM
1867(defun rmail-next-message (n)
1868 "Show following message whether deleted or not.
1869With prefix arg N, moves forward N messages, or backward if N is negative."
1870 (interactive "p")
1871 (rmail-maybe-set-message-counters)
1872 (rmail-show-message (+ rmail-current-message n)))
1873
1874(defun rmail-previous-message (n)
1875 "Show previous message whether deleted or not.
1876With prefix arg N, moves backward N messages, or forward if N is negative."
1877 (interactive "p")
1878 (rmail-next-message (- n)))
1879
1880(defun rmail-next-undeleted-message (n)
1881 "Show following non-deleted message.
1882With prefix arg N, moves forward N non-deleted messages,
c42722e3
RS
1883or backward if N is negative.
1884
1885Returns t if a new message is being shown, nil otherwise."
581d7e0b
RM
1886 (interactive "p")
1887 (rmail-maybe-set-message-counters)
1888 (let ((lastwin rmail-current-message)
1889 (current rmail-current-message))
1890 (while (and (> n 0) (< current rmail-total-messages))
1891 (setq current (1+ current))
1892 (if (not (rmail-message-deleted-p current))
1893 (setq lastwin current n (1- n))))
1894 (while (and (< n 0) (> current 1))
1895 (setq current (1- current))
1896 (if (not (rmail-message-deleted-p current))
1897 (setq lastwin current n (1+ n))))
1898 (if (/= lastwin rmail-current-message)
c42722e3
RS
1899 (progn (rmail-show-message lastwin)
1900 t)
1901 (if (< n 0)
1902 (message "No previous nondeleted message"))
1903 (if (> n 0)
1904 (message "No following nondeleted message"))
1905 nil)))
581d7e0b
RM
1906
1907(defun rmail-previous-undeleted-message (n)
1908 "Show previous non-deleted message.
1909With prefix argument N, moves backward N non-deleted messages,
1910or forward if N is negative."
1911 (interactive "p")
1912 (rmail-next-undeleted-message (- n)))
1913
1914(defun rmail-first-message ()
1915 "Show first message in file."
1916 (interactive)
1917 (rmail-maybe-set-message-counters)
1918 (rmail-show-message 1))
1919
1920(defun rmail-last-message ()
1921 "Show last message in file."
1922 (interactive)
1923 (rmail-maybe-set-message-counters)
1924 (rmail-show-message rmail-total-messages))
1925
1926(defun rmail-what-message ()
1927 (let ((where (point))
1928 (low 1)
1929 (high rmail-total-messages)
1930 (mid (/ rmail-total-messages 2)))
1931 (while (> (- high low) 1)
1932 (if (>= where (rmail-msgbeg mid))
1933 (setq low mid)
1934 (setq high mid))
1935 (setq mid (+ low (/ (- high low) 2))))
1936 (if (>= where (rmail-msgbeg high)) high low)))
1937
bd1f0f84
RS
1938(defun rmail-message-recipients-p (msg recipients &optional primary-only)
1939 (save-restriction
1940 (goto-char (rmail-msgbeg msg))
1941 (search-forward "\n*** EOOH ***\n")
1942 (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
1943 (or (string-match recipients (or (mail-fetch-field "To") ""))
1944 (string-match recipients (or (mail-fetch-field "From") ""))
1945 (if (not primary-only)
1946 (string-match recipients (or (mail-fetch-field "Cc") ""))))))
1947
1948(defun rmail-message-regexp-p (msg regexp)
1949 "Return t, if for message number MSG, regexp REGEXP matches in the header."
1950 (goto-char (rmail-msgbeg msg))
1951 (let ((end
1952 (save-excursion
1953 (search-forward "*** EOOH ***" (point-max)) (point))))
1954 (re-search-forward regexp end t)))
1955
581d7e0b 1956(defvar rmail-search-last-regexp nil)
e91f80c4 1957(defun rmail-search (regexp &optional n)
40e09f88 1958 "Show message containing next match for REGEXP (but not the current msg).
e91f80c4
RS
1959Prefix argument gives repeat count; negative argument means search
1960backwards (through earlier messages).
1961Interactively, empty argument means use same regexp used last time."
581d7e0b
RM
1962 (interactive
1963 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
1964 (prompt
1965 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
1966 regexp)
1967 (if rmail-search-last-regexp
1968 (setq prompt (concat prompt
1969 "(default "
1970 rmail-search-last-regexp
1971 ") ")))
1972 (setq regexp (read-string prompt))
1973 (cond ((not (equal regexp ""))
1974 (setq rmail-search-last-regexp regexp))
1975 ((not rmail-search-last-regexp)
1976 (error "No previous Rmail search string")))
e91f80c4
RS
1977 (list rmail-search-last-regexp
1978 (prefix-numeric-value current-prefix-arg))))
1979 (or n (setq n 1))
581d7e0b 1980 (message "%sRmail search for %s..."
df8a44dd 1981 (if (< n 0) "Reverse " "")
581d7e0b
RM
1982 regexp)
1983 (rmail-maybe-set-message-counters)
1984 (let ((omin (point-min))
1985 (omax (point-max))
1986 (opoint (point))
1987 win
e91f80c4 1988 (reversep (< n 0))
581d7e0b
RM
1989 (msg rmail-current-message))
1990 (unwind-protect
1991 (progn
1992 (widen)
e91f80c4
RS
1993 (while (/= n 0)
1994 ;; Check messages one by one, advancing message number up or down
1995 ;; but searching forward through each message.
1996 (if reversep
1997 (while (and (null win) (> msg 1))
1998 (goto-char (rmail-msgbeg (setq msg (1- msg))))
1999 (setq win (re-search-forward
2000 regexp (rmail-msgend msg) t)))
2001 (while (and (null win) (< msg rmail-total-messages))
2002 (goto-char (rmail-msgbeg (setq msg (1+ msg))))
2003 (setq win (re-search-forward regexp (rmail-msgend msg) t))))
df8a44dd 2004 (setq n (+ n (if reversep 1 -1)))))
581d7e0b
RM
2005 (if win
2006 (progn
2007 ;; If this is a reverse search and we found a message,
2008 ;; search backward thru this message to position point.
2009 (if reversep
2010 (progn
2011 (goto-char (rmail-msgend msg))
2012 (re-search-backward
2013 regexp (rmail-msgbeg msg) t)))
2014 (setq win (point))
2015 (rmail-show-message msg)
2016 (message "%sRmail search for %s...done"
2017 (if reversep "Reverse " "")
2018 regexp)
2019 (goto-char win))
2020 (goto-char opoint)
2021 (narrow-to-region omin omax)
2022 (ding)
2023 (message "Search failed: %s" regexp)))))
2024
e91f80c4
RS
2025(defun rmail-search-backwards (regexp &optional n)
2026 "Show message containing previous match for REGEXP.
2027Prefix argument gives repeat count; negative argument means search
2028forward (through later messages).
2029Interactively, empty argument means use same regexp used last time."
2030 (interactive
df8a44dd 2031 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
e91f80c4
RS
2032 (prompt
2033 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
2034 regexp)
2035 (if rmail-search-last-regexp
2036 (setq prompt (concat prompt
2037 "(default "
2038 rmail-search-last-regexp
2039 ") ")))
2040 (setq regexp (read-string prompt))
2041 (cond ((not (equal regexp ""))
2042 (setq rmail-search-last-regexp regexp))
2043 ((not rmail-search-last-regexp)
2044 (error "No previous Rmail search string")))
2045 (list rmail-search-last-regexp
2046 (prefix-numeric-value current-prefix-arg))))
68d30f2b 2047 (rmail-search regexp (- (or n 1))))
e91f80c4 2048
581d7e0b
RM
2049;; Show the first message which has the `unseen' attribute.
2050(defun rmail-first-unseen-message ()
117f4b92 2051 (rmail-maybe-set-message-counters)
581d7e0b
RM
2052 (let ((current 1)
2053 found)
2054 (save-restriction
2055 (widen)
bb9dcce1 2056 (while (and (not found) (<= current rmail-total-messages))
581d7e0b 2057 (if (rmail-message-labels-p current ", ?\\(unseen\\),")
462c1094
RM
2058 (setq found current))
2059 (setq current (1+ current))))
e9c735fa
JA
2060;; Let the caller show the message.
2061;; (if found
2062;; (rmail-show-message found))
2063 found))
60fb2b34
RS
2064
2065(defun rmail-next-same-subject (n)
2066 "Go to the next mail message having the same subject header.
2067With prefix argument N, do this N times.
2068If N is negative, go backwards instead."
2069 (interactive "p")
e8417bb0
KH
2070 (let ((subject (mail-fetch-field "Subject"))
2071 (forward (> n 0))
2072 (i rmail-current-message)
a21b845b 2073 (case-fold-search t)
e8417bb0 2074 search-regexp found)
60fb2b34
RS
2075 (if (string-match "Re:[ \t]*" subject)
2076 (setq subject (substring subject (match-end 0))))
e8417bb0
KH
2077 (setq search-regexp (concat "^Subject: *\\(Re: *\\)?"
2078 (regexp-quote subject)
2079 "\n"))
60fb2b34
RS
2080 (save-excursion
2081 (save-restriction
2082 (widen)
2083 (while (and (/= n 0)
2084 (if forward
2085 (< i rmail-total-messages)
2086 (> i 1)))
2087 (let (done)
2088 (while (and (not done)
2089 (if forward
2090 (< i rmail-total-messages)
2091 (> i 1)))
2092 (setq i (if forward (1+ i) (1- i)))
2093 (goto-char (rmail-msgbeg i))
2094 (search-forward "\n*** EOOH ***\n")
2095 (let ((beg (point)) end)
2096 (search-forward "\n\n")
2097 (setq end (point))
2098 (goto-char beg)
2099 (setq done (re-search-forward search-regexp end t))))
2100 (if done (setq found i)))
2101 (setq n (if forward (1- n) (1+ n))))))
2102 (if found
2103 (rmail-show-message found)
2104 (error "No %s message with same subject"
2105 (if forward "following" "previous")))))
2106
2107(defun rmail-previous-same-subject (n)
2108 "Go to the previous mail message having the same subject header.
2109With prefix argument N, do this N times.
2110If N is negative, go forwards instead."
2111 (interactive "p")
2112 (rmail-next-same-subject (- n)))
581d7e0b
RM
2113\f
2114;;;; *** Rmail Message Deletion Commands ***
2115
2116(defun rmail-message-deleted-p (n)
2117 (= (aref rmail-deleted-vector n) ?D))
2118
2119(defun rmail-set-message-deleted-p (n state)
2120 (aset rmail-deleted-vector n (if state ?D ?\ )))
2121
2122(defun rmail-delete-message ()
2123 "Delete this message and stay on it."
2124 (interactive)
6c714afe
RS
2125 (rmail-set-attribute "deleted" t)
2126 (run-hooks 'rmail-delete-message-hook))
581d7e0b
RM
2127
2128(defun rmail-undelete-previous-message ()
2129 "Back up to deleted message, select it, and undelete it."
2130 (interactive)
2131 (let ((msg rmail-current-message))
2132 (while (and (> msg 0)
2133 (not (rmail-message-deleted-p msg)))
2134 (setq msg (1- msg)))
2135 (if (= msg 0)
2136 (error "No previous deleted message")
2137 (if (/= msg rmail-current-message)
2138 (rmail-show-message msg))
bd1f0f84
RS
2139 (rmail-set-attribute "deleted" nil)
2140 (if (rmail-summary-exists)
2141 (save-excursion
2142 (set-buffer rmail-summary-buffer)
2143 (rmail-summary-mark-undeleted msg)))
2144 (rmail-maybe-display-summary))))
581d7e0b
RM
2145
2146(defun rmail-delete-forward (&optional backward)
2147 "Delete this message and move to next nondeleted one.
2148Deleted messages stay in the file until the \\[rmail-expunge] command is given.
c42722e3
RS
2149With prefix argument, delete and move backward.
2150
2151Returns t if a new message is displayed after the delete, or nil otherwise."
581d7e0b
RM
2152 (interactive "P")
2153 (rmail-set-attribute "deleted" t)
6c714afe 2154 (run-hooks 'rmail-delete-message-hook)
bd1f0f84
RS
2155 (let ((del-msg rmail-current-message))
2156 (if (rmail-summary-exists)
4edaa169
RS
2157 (rmail-select-summary
2158 (rmail-summary-mark-deleted del-msg)))
c42722e3
RS
2159 (prog1 (rmail-next-undeleted-message (if backward -1 1))
2160 (rmail-maybe-display-summary))))
581d7e0b
RM
2161
2162(defun rmail-delete-backward ()
2163 "Delete this message and move to previous nondeleted one.
2164Deleted messages stay in the file until the \\[rmail-expunge] command is given."
2165 (interactive)
2166 (rmail-delete-forward t))
2167
1825bea1
RS
2168;; Compute the message number a given message would have after expunging.
2169;; The present number of the message is OLDNUM.
2170;; DELETEDVEC should be rmail-deleted-vector.
2171;; The value is nil for a message that would be deleted.
2172(defun rmail-msg-number-after-expunge (deletedvec oldnum)
2173 (if (or (null oldnum) (= (aref deletedvec oldnum) ?D))
2174 nil
2175 (let ((i 0)
2176 (newnum 0))
2177 (while (< i oldnum)
2178 (if (/= (aref deletedvec i) ?D)
2179 (setq newnum (1+ newnum)))
2180 (setq i (1+ i)))
2181 newnum)))
2182
bd1f0f84 2183(defun rmail-only-expunge ()
581d7e0b
RM
2184 "Actually erase all deleted messages in the file."
2185 (interactive)
2186 (message "Expunging deleted messages...")
2187 ;; Discard all undo records for this buffer.
2188 (or (eq buffer-undo-list t)
2189 (setq buffer-undo-list nil))
2190 (rmail-maybe-set-message-counters)
2191 (let* ((omax (- (buffer-size) (point-max)))
2192 (omin (- (buffer-size) (point-min)))
2193 (opoint (if (and (> rmail-current-message 0)
881fd7eb
KH
2194 (rmail-message-deleted-p rmail-current-message))
2195 0
2196 (- (point) (point-min))))
581d7e0b
RM
2197 (messages-head (cons (aref rmail-message-vector 0) nil))
2198 (messages-tail messages-head)
2199 ;; Don't make any undo records for the expunging.
2200 (buffer-undo-list t)
2201 (win))
2202 (unwind-protect
2203 (save-excursion
2204 (widen)
2205 (goto-char (point-min))
2206 (let ((counter 0)
2207 (number 1)
2208 (total rmail-total-messages)
2209 (new-message-number rmail-current-message)
2210 (new-summary nil)
1825bea1 2211 (rmailbuf (current-buffer))
581d7e0b
RM
2212 (buffer-read-only nil)
2213 (messages rmail-message-vector)
2214 (deleted rmail-deleted-vector)
2215 (summary rmail-summary-vector))
2216 (setq rmail-total-messages nil
2217 rmail-current-message nil
2218 rmail-message-vector nil
2219 rmail-deleted-vector nil
2220 rmail-summary-vector nil)
1825bea1
RS
2221
2222 ;; Find each sendmail buffer that is set to reply
2223 ;; to a message in this buffer, and update its
2224 ;; message number.
2225 (let ((bufs (buffer-list)))
2226 (while bufs
2227 (save-excursion
2228 (set-buffer (car bufs))
2229 (and (boundp 'rmail-send-actions-rmail-buffer)
2230 (eq rmail-send-actions-rmail-buffer rmailbuf)
2231 (setq rmail-send-actions-rmail-msg-number
2232 (rmail-msg-number-after-expunge
2233 deleted
2234 rmail-send-actions-rmail-msg-number))))
2235 (setq bufs (cdr bufs))))
2236
581d7e0b
RM
2237 (while (<= number total)
2238 (if (= (aref deleted number) ?D)
2239 (progn
2240 (delete-region
2241 (marker-position (aref messages number))
2242 (marker-position (aref messages (1+ number))))
2243 (move-marker (aref messages number) nil)
2244 (if (> new-message-number counter)
2245 (setq new-message-number (1- new-message-number))))
2246 (setq counter (1+ counter))
2247 (setq messages-tail
2248 (setcdr messages-tail
2249 (cons (aref messages number) nil)))
2250 (setq new-summary
2251 (cons (if (= counter number) (aref summary (1- number)))
2252 new-summary)))
2253 (if (zerop (% (setq number (1+ number)) 20))
2254 (message "Expunging deleted messages...%d" number)))
2255 (setq messages-tail
2256 (setcdr messages-tail
2257 (cons (aref messages number) nil)))
2258 (setq rmail-current-message new-message-number
2259 rmail-total-messages counter
2260 rmail-message-vector (apply 'vector messages-head)
2261 rmail-deleted-vector (make-string (1+ counter) ?\ )
2262 rmail-summary-vector (vconcat (nreverse new-summary))
2263 win t)))
2264 (message "Expunging deleted messages...done")
2265 (if (not win)
2266 (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
2267 (rmail-show-message
2268 (if (zerop rmail-current-message) 1 nil))
d0c3269a 2269 (goto-char (+ (point) opoint)))))
bd1f0f84
RS
2270
2271(defun rmail-expunge ()
2272 "Erase deleted messages from Rmail file and summary buffer."
2273 (interactive)
2274 (rmail-only-expunge)
2275 (if (rmail-summary-exists)
2276 (rmail-select-summary
2277 (rmail-update-summary))))
581d7e0b
RM
2278\f
2279;;;; *** Rmail Mailing Commands ***
2280
bde7cb3f
RS
2281(defun rmail-start-mail (&optional noerase to subject in-reply-to cc
2282 replybuffer sendactions same-window others)
2283 (let (yank-action)
2284 (if replybuffer
2285 (setq yank-action (list 'insert-buffer replybuffer)))
2286 (setq others (cons (cons "cc" cc) others))
2287 (setq others (cons (cons "in-reply-to" in-reply-to) others))
2288 (if same-window
2289 (compose-mail to subject others
2290 noerase nil
2291 yank-action sendactions)
2292 (if (and window-system rmail-mail-new-frame)
2293 (prog1
2294 (compose-mail to subject others
2295 noerase 'switch-to-buffer-other-frame
2296 yank-action sendactions)
2297 ;; This is not a standard frame parameter;
2298 ;; nothing except sendmail.el looks at it.
2299 (modify-frame-parameters (selected-frame)
2300 '((mail-dedicated-frame . t))))
2301 (compose-mail to subject others
2302 noerase 'switch-to-buffer-other-window
2303 yank-action sendactions)))))
94ed51e8 2304
581d7e0b 2305(defun rmail-mail ()
bd1f0f84
RS
2306 "Send mail in another window.
2307While composing the message, use \\[mail-yank-original] to yank the
2308original message into it."
581d7e0b 2309 (interactive)
d1e69dec 2310 (rmail-start-mail nil nil nil nil nil rmail-view-buffer))
581d7e0b
RM
2311
2312(defun rmail-continue ()
2313 "Continue composing outgoing message previously being composed."
2314 (interactive)
94ed51e8 2315 (rmail-start-mail t))
581d7e0b 2316
43432812
RS
2317(put 'rmail-send-actions-rmail-buffer 'permanent-local t)
2318(put 'rmail-send-actions-rmail-msg-number 'permanent-local t)
2319
581d7e0b
RM
2320(defun rmail-reply (just-sender)
2321 "Reply to the current message.
2322Normally include CC: to all other recipients of original message;
2323prefix argument means ignore them. While composing the reply,
2324use \\[mail-yank-original] to yank the original message into it."
2325 (interactive "P")
11aea8f5 2326 (let (from reply-to cc subject date to message-id references
037fa8ca 2327 resent-to resent-cc resent-reply-to
d1e69dec 2328 (msgnum rmail-current-message))
581d7e0b
RM
2329 (save-excursion
2330 (save-restriction
2331 (widen)
2332 (goto-char (rmail-msgbeg rmail-current-message))
2333 (forward-line 1)
2334 (if (= (following-char) ?0)
2335 (narrow-to-region
2336 (progn (forward-line 2)
2337 (point))
2338 (progn (search-forward "\n\n" (rmail-msgend rmail-current-message)
2339 'move)
2340 (point)))
2341 (narrow-to-region (point)
2342 (progn (search-forward "\n*** EOOH ***\n")
2343 (beginning-of-line) (point))))
037fa8ca
RS
2344 (setq from (mail-fetch-field "from")
2345 reply-to (or (mail-fetch-field "reply-to" nil t)
581d7e0b 2346 from)
037fa8ca
RS
2347 cc (and (not just-sender)
2348 (mail-fetch-field "cc" nil t))
2349 subject (mail-fetch-field "subject")
2350 date (mail-fetch-field "date")
2351 to (or (mail-fetch-field "to" nil t) "")
2352 message-id (mail-fetch-field "message-id")
11aea8f5
RS
2353 references (mail-fetch-field "references" nil nil t)
2354 resent-reply-to (mail-fetch-field "resent-reply-to" nil t)
037fa8ca
RS
2355 resent-cc (and (not just-sender)
2356 (mail-fetch-field "resent-cc" nil t))
2357 resent-to (or (mail-fetch-field "resent-to" nil t) "")
2358;;; resent-subject (mail-fetch-field "resent-subject")
2359;;; resent-date (mail-fetch-field "resent-date")
2360;;; resent-message-id (mail-fetch-field "resent-message-id")
2361 )))
2362 ;; Merge the resent-to and resent-cc into the to and cc.
2363 (if (and resent-to (not (equal resent-to "")))
2364 (if (not (equal to ""))
2365 (setq to (concat to ", " resent-to))
2366 (setq to resent-to)))
2367 (if (and resent-cc (not (equal resent-cc "")))
2368 (if (not (equal cc ""))
2369 (setq cc (concat cc ", " resent-cc))
2370 (setq cc resent-cc)))
2371 ;; Add `Re: ' to subject if not there already.
bd1f0f84 2372 (and (stringp subject)
08960da1
KH
2373 (setq subject
2374 (concat rmail-reply-prefix
a21b845b
KH
2375 (if (let ((case-fold-search t))
2376 (string-match rmail-reply-regexp subject))
08960da1
KH
2377 (substring subject (match-end 0))
2378 subject))))
94ed51e8 2379 (rmail-start-mail nil
581d7e0b
RM
2380 (mail-strip-quoted-names reply-to)
2381 subject
2382 (rmail-make-in-reply-to-field from date message-id)
2383 (if just-sender
2384 nil
2385 (let* ((cc-list (rmail-dont-reply-to
2386 (mail-strip-quoted-names
2387 (if (null cc) to (concat to ", " cc))))))
2388 (if (string= cc-list "") nil cc-list)))
d1e69dec 2389 rmail-view-buffer
1825bea1
RS
2390 (list (list '(lambda ()
2391 (let ((msgnum rmail-send-actions-rmail-msg-number))
2392 (save-excursion
2393 (set-buffer rmail-send-actions-rmail-buffer)
2394 (if msgnum
11aea8f5
RS
2395 (rmail-set-attribute "answered" t msgnum)))))))
2396 nil
d2d8b77e
RS
2397 (list (cons "References" (concat (mapconcat 'identity references " ")
2398 " " message-id))))
1825bea1
RS
2399 ;; We keep the rmail buffer and message number in these
2400 ;; buffer-local vars in the sendmail buffer,
2401 ;; so that rmail-only-expunge can relocate the message number.
2402 (make-local-variable 'rmail-send-actions-rmail-buffer)
2403 (make-local-variable 'rmail-send-actions-rmail-msg-number)
d1e69dec 2404 (setq rmail-send-actions-rmail-buffer rmail-view-buffer)
1825bea1 2405 (setq rmail-send-actions-rmail-msg-number msgnum)))
581d7e0b
RM
2406
2407(defun rmail-make-in-reply-to-field (from date message-id)
2408 (cond ((not from)
2409 (if message-id
2410 message-id
2411 nil))
2412 (mail-use-rfc822
2413 (require 'rfc822)
2414 (let ((tem (car (rfc822-addresses from))))
2415 (if message-id
2416 (if (string-match
2417 (regexp-quote (if (string-match "@[^@]*\\'" tem)
2418 (substring tem 0 (match-beginning 0))
2419 tem))
2420 message-id)
2421 ;; Message-ID is sufficiently informative
2422 message-id
2423 (concat message-id " (" tem ")"))
1de48e7f
RS
2424 ;; Copy TEM, discarding text properties.
2425 (setq tem (copy-sequence tem))
2426 (set-text-properties 0 (length tem) nil tem)
2427 (setq tem (copy-sequence tem))
2428 ;; Use prin1 to fake RFC822 quoting
2429 (let ((field (prin1-to-string tem)))
2430 (if date
2431 (concat field "'s message of " date)
2432 field)))))
581d7e0b
RM
2433 ((let* ((foo "[^][\000-\037\177-\377()<>@,;:\\\" ]+")
2434 (bar "[^][\000-\037\177-\377()<>@,;:\\\"]+"))
2435 ;; Can't use format because format loses on \000 (unix *^&%*^&%$!!)
2436 (or (string-match (concat "\\`[ \t]*\\(" bar
2437 "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
2438 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
2439 from)
2440 (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
2441 bar "\\))[ \t]*\\'")
2442 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
2443 from)))
2444 (let ((start (match-beginning 1))
2445 (end (match-end 1)))
2446 ;; Trim whitespace which above regexp match allows
2447 (while (and (< start end)
2448 (memq (aref from start) '(?\t ?\ )))
2449 (setq start (1+ start)))
2450 (while (and (< start end)
2451 (memq (aref from (1- end)) '(?\t ?\ )))
2452 (setq end (1- end)))
2453 (let ((field (substring from start end)))
2454 (if date (setq field (concat "message from " field " on " date)))
2455 (if message-id
2456 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
2457 (concat message-id " (" field ")")
2458 field))))
2459 (t
2460 ;; If we can't kludge it simply, do it correctly
2461 (let ((mail-use-rfc822 t))
2462 (rmail-make-in-reply-to-field from date message-id)))))
a16aef15 2463\f
8159f4af
RS
2464(defun rmail-forward (resend)
2465 "Forward the current message to another user.
2466With prefix argument, \"resend\" the message instead of forwarding it;
2467see the documentation of `rmail-resend'."
2468 (interactive "P")
2469 (if resend
2470 (call-interactively 'rmail-resend)
2471 (let ((forward-buffer (current-buffer))
1825bea1 2472 (msgnum rmail-current-message)
8159f4af
RS
2473 (subject (concat "["
2474 (let ((from (or (mail-fetch-field "From")
2475 (mail-fetch-field ">From"))))
2476 (if from
2477 (concat (mail-strip-quoted-names from) ": ")
2478 ""))
2479 (or (mail-fetch-field "Subject") "")
2480 "]")))
bde7cb3f
RS
2481 (if (rmail-start-mail
2482 nil nil subject nil nil nil
2483 (list (list (function
2484 (lambda ()
2485 (let ((msgnum
2486 rmail-send-actions-rmail-msg-number))
2487 (save-excursion
2488 (set-buffer rmail-send-actions-rmail-buffer)
2489 (if msgnum
2490 (rmail-set-attribute
2491 "forwarded" t msgnum))))))))
2492 ;; If only one window, use it for the mail buffer.
2493 ;; Otherwise, use another window for the mail buffer
2494 ;; so that the Rmail buffer remains visible
2495 ;; and sending the mail will get back to it.
2496 (and (not rmail-mail-new-frame) (one-window-p t)))
1825bea1 2497 ;; The mail buffer is now current.
b1d9bfa7 2498 (save-excursion
1825bea1
RS
2499 ;; We keep the rmail buffer and message number in these
2500 ;; buffer-local vars in the sendmail buffer,
2501 ;; so that rmail-only-expunge can relocate the message number.
2502 (make-local-variable 'rmail-send-actions-rmail-buffer)
2503 (make-local-variable 'rmail-send-actions-rmail-msg-number)
2504 (setq rmail-send-actions-rmail-buffer forward-buffer)
2505 (setq rmail-send-actions-rmail-msg-number msgnum)
0f0a85b3 2506 ;; Insert after header separator--before signature if any.
760a3528
BF
2507 (goto-char (point-min))
2508 (search-forward-regexp
99c3d926 2509 (concat "^" (regexp-quote mail-header-separator) "$"))
b1d9bfa7 2510 (forward-line 1)
8bdb9f4a 2511 (insert "------- Start of forwarded message -------\n")
c8a428c4
RS
2512 ;; Quote lines with `- ' if they start with `-'.
2513 (let ((beg (point)) end)
41c51dcb
RS
2514 (setq end (point-marker))
2515 (set-marker-insertion-type end t)
c8a428c4
RS
2516 (insert-buffer-substring forward-buffer)
2517 (goto-char beg)
6844ae32 2518 (while (re-search-forward "^-" end t)
c8a428c4
RS
2519 (beginning-of-line)
2520 (insert "- ")
2521 (forward-line 1))
41c51dcb 2522 (goto-char end)
4d4826ab
KH
2523 (skip-chars-backward "\n")
2524 (if (< (point) end)
2525 (forward-char 1))
41c51dcb
RS
2526 (delete-region (point) end)
2527 (set-marker end nil))
8bdb9f4a 2528 (insert "------- End of forwarded message -------\n")
1825bea1 2529 (push-mark))))))
a16aef15 2530\f
581d7e0b
RM
2531(defun rmail-resend (address &optional from comment mail-alias-file)
2532 "Resend current message to ADDRESSES.
d2fc297c 2533ADDRESSES should be a single address, a string consisting of several
581d7e0b
RM
2534addresses separated by commas, or a list of addresses.
2535
2536Optional FROM is the address to resend the message from, and
2537defaults to the username of the person redistributing the message.
2538Optional COMMENT is a string that will be inserted as a comment in the
2539resent message.
2540Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
2541typically for purposes of moderating a list."
2542 (interactive "sResend to: ")
9ad8712e
RS
2543 (require 'sendmail)
2544 (require 'mailalias)
581d7e0b
RM
2545 (if (not from) (setq from (user-login-name)))
2546 (let ((tembuf (generate-new-buffer " sendmail temp"))
2547 (mail-header-separator "")
2548 (case-fold-search nil)
2549 (mailbuf (current-buffer)))
2550 (unwind-protect
2551 (save-excursion
2552 ;;>> Copy message into temp buffer
2553 (set-buffer tembuf)
2554 (insert-buffer-substring mailbuf)
2555 (goto-char (point-min))
e20903fb 2556 ;; Delete any Sender field, since that's not specifiable.
d2fc297c
RS
2557 ; Only delete Sender fields in the actual header.
2558 (re-search-forward "^$" nil 'move)
2559 ; Using "while" here rather than "if" because some buggy mail
2560 ; software may have inserted multiple Sender fields.
2561 (while (re-search-backward "^Sender:" nil t)
2562 (let (beg)
2563 (setq beg (point))
2564 (forward-line 1)
2565 (while (looking-at "[ \t]")
2566 (forward-line 1))
2567 (delete-region beg (point))))
2568 ; Go back to the beginning of the buffer so the Resent- fields
2569 ; are inserted there.
2570 (goto-char (point-min))
581d7e0b
RM
2571 ;;>> Insert resent-from:
2572 (insert "Resent-From: " from "\n")
bcf12025 2573 (insert "Resent-Date: " (mail-rfc822-date) "\n")
581d7e0b
RM
2574 ;;>> Insert resent-to: and bcc if need be.
2575 (let ((before (point)))
c42722e3
RS
2576 (if mail-self-blind
2577 (insert "Resent-Bcc: " (user-login-name) "\n"))
581d7e0b
RM
2578 (insert "Resent-To: " (if (stringp address)
2579 address
2580 (mapconcat 'identity address ",\n\t"))
2581 "\n")
8095bf23 2582 ;; Expand abbrevs in the recipients.
50703776 2583 (save-excursion
8095bf23 2584 (if (featurep 'mailabbrev)
3f3495d3
RS
2585 (let ((end (point-marker))
2586 (local-abbrev-table mail-abbrevs)
2587 (old-syntax-table (syntax-table)))
2588 (if (and (not (vectorp mail-abbrevs))
2589 (file-exists-p mail-personal-alias-file))
2590 (build-mail-abbrevs))
2591 (set-syntax-table mail-abbrev-syntax-table)
2592 (goto-char before)
2593 (while (and (< (point) end)
2594 (progn (forward-word 1)
2595 (<= (point) end)))
2596 (expand-abbrev))
2597 (set-syntax-table old-syntax-table))
8095bf23 2598 (expand-mail-aliases before (point)))))
581d7e0b
RM
2599 ;;>> Set up comment, if any.
2600 (if (and (sequencep comment) (not (zerop (length comment))))
2601 (let ((before (point))
2602 after)
2603 (insert comment)
2604 (or (eolp) (insert "\n"))
2605 (setq after (point))
2606 (goto-char before)
2607 (while (< (point) after)
2608 (insert "Resent-Comment: ")
2609 (forward-line 1))))
2610 ;; Don't expand aliases in the destination fields
2611 ;; of the original message.
2612 (let (mail-aliases)
389e8f11 2613 (funcall send-mail-function)))
8159f4af
RS
2614 (kill-buffer tembuf))
2615 (rmail-set-attribute "resent" t rmail-current-message)))
a16aef15 2616\f
72e609d0 2617(defvar mail-unsent-separator
f2da0c68
RS
2618 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
2619 "^ *---+ +Returned message +---+ *$\\|"
20424e68 2620 "^Start of returned message$\\|"
db32c193 2621 "^ *---+ +Original message +---+ *$\\|"
487fcdc0 2622 "^ *--+ +begin message +--+ *$\\|"
c42722e3 2623 "^ *---+ +Original message follows +---+ *$\\|"
03863002
RS
2624 "^|? *---+ +Message text follows: +---+ *|?$")
2625 "A regexp that matches the separator before the text of a failed message.")
581d7e0b 2626
55a42e8c
RS
2627(defvar mail-mime-unsent-header "^Content-Type: message/rfc822 *$"
2628 "A regexp that matches the header of a MIME body part with a failed message.")
2629
581d7e0b
RM
2630(defun rmail-retry-failure ()
2631 "Edit a mail message which is based on the contents of the current message.
2632For a message rejected by the mail system, extract the interesting headers and
03863002 2633the body of the original message.
55a42e8c
RS
2634If the failed message is a MIME multipart message, it is searched for a
2635body part with a header which matches the variable `mail-mime-unsent-header'.
2636Otherwise, the variable `mail-unsent-separator' should match the string that
3db0cdac
RS
2637delimits the returned original message.
2638The variable `rmail-retry-ignored-headers' is a regular expression
2639specifying headers which should not be copied into the new message."
581d7e0b
RM
2640 (interactive)
2641 (require 'mail-utils)
40e09f88
KH
2642 (let ((rmail-buffer (current-buffer))
2643 (msgnum rmail-current-message)
2644 bounce-start bounce-end bounce-indent resending)
581d7e0b
RM
2645 (save-excursion
2646 ;; Narrow down to just the quoted original message
2647 (rmail-beginning-of-message)
55a42e8c
RS
2648 (let* ((case-fold-search t)
2649 (top (point))
2650 (content-type
2651 (save-restriction
2652 ;; Fetch any content-type header in current message
2653 (search-forward "\n\n") (narrow-to-region top (point))
2654 (mail-fetch-field "Content-Type") )) )
2655 ;; Handle MIME multipart bounce messages
2656 (if (and content-type
2657 (string-match
2658 ";[\n\t ]*boundary=\"?\\([-0-9a-z'()+_,./:=?]+\\)\"?"
2659 content-type))
92bde7a2 2660 (let ((codestring
55a42e8c
RS
2661 (concat "\n--"
2662 (substring content-type (match-beginning 1)
2663 (match-end 1)))))
2664 (or (re-search-forward mail-mime-unsent-header nil t)
e00c3a0c 2665 (error "Cannot find beginning of header in failed message"))
55a42e8c 2666 (or (search-forward "\n\n" nil t)
92bde7a2
RS
2667 (error "Cannot find end of Mime data in failed message"))
2668 (setq bounce-start (point))
55a42e8c
RS
2669 (or (search-forward codestring nil t)
2670 (error "Cannot find end of Mime data in failed message"))
2671 (setq bounce-end (match-beginning 0))
2672; (or (search-forward "\n\n" nil t)
2673; (error "Cannot find end of header in failed message"))
2674 )
2675 ;; non-MIME bounce
92bde7a2
RS
2676 (or (re-search-forward mail-unsent-separator nil t)
2677 (error "Cannot parse this as a failure message"))
20424e68
RS
2678 (skip-chars-forward "\n")
2679 ;; Support a style of failure message in which the original
2680 ;; message is indented, and included within lines saying
2681 ;; `Start of returned message' and `End of returned message'.
e75e2853
KH
2682 (if (looking-at " +Received:")
2683 (progn
2684 (setq bounce-start (point))
20424e68 2685 (skip-chars-forward " ")
e75e2853
KH
2686 (setq bounce-indent (- (current-column)))
2687 (goto-char (point-max))
2688 (re-search-backward "^End of returned message$" nil t)
2689 (setq bounce-end (point)))
2690 ;; One message contained a few random lines before the old
2691 ;; message header. The first line of the message started with
40e09f88
KH
2692 ;; two hyphens. A blank line followed these random lines.
2693 ;; The same line beginning with two hyphens was possibly
2694 ;; marking the end of the message.
e75e2853 2695 (if (looking-at "^--")
40e09f88
KH
2696 (let ((boundary (buffer-substring-no-properties
2697 (point)
2698 (progn (end-of-line) (point)))))
e75e2853 2699 (search-forward "\n\n")
40e09f88
KH
2700 (skip-chars-forward "\n")
2701 (setq bounce-start (point))
2702 (goto-char (point-max))
2703 (search-backward (concat "\n\n" boundary) bounce-start t)
2704 (setq bounce-end (point)))
2705 (setq bounce-start (point)
2706 bounce-end (point-max)))
e75e2853 2707 (or (search-forward "\n\n" nil t)
40e09f88
KH
2708 (error "Cannot find end of header in failed message"))
2709 ))))
581d7e0b 2710 ;; Start sending a new message; default header fields from the original.
854c16c5
RS
2711 ;; Turn off the usual actions for initializing the message body
2712 ;; because we want to get only the text from the failure message.
40e09f88
KH
2713 (let ((action
2714 ;; This function will be called when the user sends the retry.
2715 ;; It will mark the bounce message as "retried".
2716 (function (lambda ()
2717 (let ((msgnum rmail-send-actions-rmail-msg-number))
2718 (save-excursion
2719 (set-buffer rmail-send-actions-rmail-buffer)
2720 (if msgnum
2721 (rmail-set-attribute "retried" t msgnum)))))))
2722 mail-signature mail-setup-hook)
2723 (if (rmail-start-mail nil nil nil nil nil rmail-buffer
2724 (list (list action)))
854c16c5 2725 ;; Insert original text as initial text of new draft message.
e20903fb
RS
2726 ;; Bind inhibit-read-only since the header delimiter
2727 ;; of the previous message was probably read-only.
2728 (let ((inhibit-read-only t))
40e09f88
KH
2729 ;; We keep the rmail buffer and message number in these
2730 ;; buffer-local vars in the sendmail buffer,
2731 ;; so that the rmail-only-expunge can relocate the message number.
2732 (make-local-variable 'rmail-send-actions-rmail-buffer)
2733 (make-local-variable 'rmail-send-actions-rmail-msg-number)
2734 (setq rmail-send-actions-rmail-buffer rmail-buffer)
2735 (setq rmail-send-actions-rmail-msg-number msgnum)
3db0cdac 2736 (erase-buffer)
40e09f88 2737 (insert-buffer-substring rmail-buffer bounce-start bounce-end)
3db0cdac 2738 (goto-char (point-min))
e75e2853
KH
2739 (if bounce-indent
2740 (indent-rigidly (point-min) (point-max) bounce-indent))
3db0cdac 2741 (rmail-clear-headers rmail-retry-ignored-headers)
e75e2853 2742 (rmail-clear-headers "^sender:\\|^from:\\|^return-path:")
854c16c5 2743 (goto-char (point-min))
3db0cdac
RS
2744 (save-restriction
2745 (search-forward "\n\n")
2746 (forward-line -1)
2747 (narrow-to-region (point-min) (point))
2748 (setq resending (mail-fetch-field "resent-to"))
2749 (if mail-self-blind
2750 (if resending
2751 (insert "Resent-Bcc: " (user-login-name) "\n")
2752 (insert "BCC: " (user-login-name) "\n"))))
2753 (insert mail-header-separator)
2754 (mail-position-on-field (if resending "Resent-To" "To") t)
40e09f88 2755 (set-buffer rmail-buffer)
3db0cdac 2756 (rmail-beginning-of-message))))))
a16aef15 2757\f
bd1f0f84
RS
2758(defun rmail-summary-exists ()
2759 "Non-nil iff in an RMAIL buffer and an associated summary buffer exists.
90254bb0 2760In fact, the non-nil value returned is the summary buffer itself."
bd1f0f84
RS
2761 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
2762 rmail-summary-buffer))
2763
2764(defun rmail-summary-displayed ()
2765 "t iff in RMAIL buffer and an associated summary buffer is displayed."
2766 (and rmail-summary-buffer (get-buffer-window rmail-summary-buffer)))
2767
2768(defvar rmail-redisplay-summary nil
2769 "*Non-nil means Rmail should show the summary when it changes.
2770This has an effect only if a summary buffer exists.")
2771
90254bb0
RS
2772(defvar rmail-summary-window-size nil
2773 "*Non-nil means specify the height for an Rmail summary window.")
2774
bd1f0f84
RS
2775;; Put the summary buffer back on the screen, if user wants that.
2776(defun rmail-maybe-display-summary ()
90254bb0
RS
2777 (let ((selected (selected-window))
2778 window)
2779 ;; If requested, make sure the summary is displayed.
2780 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
2781 rmail-redisplay-summary
6b265868
RS
2782 (if (get-buffer-window rmail-summary-buffer 0)
2783 ;; It's already in some frame; show that one.
2784 (let ((frame (window-frame
2785 (get-buffer-window rmail-summary-buffer 0))))
2786 (make-frame-visible frame)
2787 (raise-frame frame))
2788 (display-buffer rmail-summary-buffer)))
90254bb0
RS
2789 ;; If requested, set the height of the summary window.
2790 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
2791 rmail-summary-window-size
2792 (setq window (get-buffer-window rmail-summary-buffer))
6b265868
RS
2793 ;; Don't try to change the size if just one window in frame.
2794 (not (eq window (frame-root-window (window-frame window))))
90254bb0
RS
2795 (unwind-protect
2796 (progn
2797 (select-window window)
35aba739 2798 (enlarge-window (- rmail-summary-window-size (window-height))))
90254bb0 2799 (select-window selected)))))
581d7e0b 2800\f
271c888a
SM
2801;;;; *** Rmail Local Fontification ***
2802
2803(defun rmail-fontify-buffer-function ()
2804 ;; This function's symbol is bound to font-lock-fontify-buffer-function.
f512e4c0
SM
2805 (make-local-hook 'rmail-show-message-hook)
2806 (add-hook 'rmail-show-message-hook 'rmail-fontify-message nil t)
35aba739
SM
2807 ;; If we're already showing a message, fontify it now.
2808 (if rmail-current-message (rmail-fontify-message))
2809 ;; Prevent Font Lock mode from kicking in.
f512e4c0 2810 (setq font-lock-fontified t))
271c888a
SM
2811
2812(defun rmail-unfontify-buffer-function ()
2813 ;; This function's symbol is bound to font-lock-fontify-unbuffer-function.
f512e4c0
SM
2814 (let ((modified (buffer-modified-p))
2815 (buffer-undo-list t) (inhibit-read-only t)
2816 before-change-functions after-change-functions
2817 buffer-file-name buffer-file-truename)
2818 (save-restriction
2819 (widen)
2820 (remove-hook 'rmail-show-message-hook 'rmail-fontify-message t)
2821 (remove-text-properties (point-min) (point-max) '(rmail-fontified nil))
1afb7be2
RS
2822 (font-lock-default-unfontify-buffer)
2823 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))
271c888a
SM
2824
2825(defun rmail-fontify-message ()
2826 ;; Fontify the current message if it is not already fontified.
2827 (if (text-property-any (point-min) (point-max) 'rmail-fontified nil)
2828 (let ((modified (buffer-modified-p))
2829 (buffer-undo-list t) (inhibit-read-only t)
2830 before-change-functions after-change-functions
2831 buffer-file-name buffer-file-truename)
2832 (save-excursion
2833 (save-match-data
2834 (add-text-properties (point-min) (point-max) '(rmail-fontified t))
1afb7be2
RS
2835 (font-lock-fontify-region (point-min) (point-max))
2836 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))))
6c4b1a6e 2837
49116ac0
JB
2838(provide 'rmail)
2839
c88ab9ce 2840;;; rmail.el ends here