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