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