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