1 ;;; rmail.el --- main code of "RMAIL" mail reader for Emacs.
3 ;; Copyright (C) 1985,86,87,88,93,94,95 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
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)
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;; Souped up by shane@mit-ajax based on ideas of rlk@athena.mit.edu
27 ;; New features include attribute and keyword support, message
28 ;; selection by dispatch table, summary by attributes and keywords,
29 ;; expunging by dispatch table, sticky options for file commands.
31 ;; Extended by Bob Weiner of Motorola
32 ;; New features include: rmail and rmail-summary buffers remain
33 ;; synchronized and key bindings basically operate the same way in both
34 ;; buffers, summary by topic or by regular expression, rmail-reply-prefix
35 ;; variable, and a bury rmail buffer (wipe) command.
40 ;; For Emacs V18 compatibility
41 (and (not (fboundp 'buffer-disable-undo))
42 (fboundp 'buffer-flush-undo)
43 (defalias 'buffer-disable-undo 'buffer-flush-undo))
45 ; These variables now declared in paths.el.
46 ;(defvar rmail-spool-directory "/usr/spool/mail/"
47 ; "This is the name of the directory used by the system mailer for\n\
48 ;delivering new mail. It's name should end with a slash.")
49 ;(defvar rmail-file-name
50 ; (expand-file-name "~/RMAIL")
53 (defvar rmail-movemail-program nil
54 "If non-nil, name of program for fetching new mail.")
57 (defvar rmail-dont-reply-to-names nil "\
58 *A regexp specifying names to prune of reply to messages.
59 A value of nil means exclude your own name only.")
62 (defvar rmail-default-dont-reply-to-names "info-" "\
63 A regular expression specifying part of the value of the default value of
64 the variable `rmail-dont-reply-to-names', for when the user does not set
65 `rmail-dont-reply-to-names' explicitly. (The other part of the default
66 value is the user's name.)
67 It is useful to set this variable in the site customization file.")
70 (defvar rmail-ignored-headers "^via:\\|^mail-from:\\|^origin:\\|^status:\\|^received:\\|^x400-originator:\\|^x400-recipients:\\|^x400-received:\\|^x400-mts-identifier:\\|^x400-content-type:\\|^\\(resent-\\|\\)message-id:\\|^summary-line:\\|^resent-date:\\|^nntp-posting-host:"
71 "*Regexp to match Header fields that Rmail should normally hide.")
74 (defvar rmail-displayed-headers nil
75 "*Regexp to match Header fields that Rmail should display.
76 If nil, display all header fields except those matched by
77 `rmail-ignored-headers'.")
80 (defvar rmail-retry-ignored-headers nil "\
81 *Headers that should be stripped when retrying a failed message.")
84 (defvar rmail-highlighted-headers "^From:\\|^Subject:" "\
85 *Regexp to match Header fields that Rmail should normally highlight.
86 A value of nil means don't highlight.
87 See also `rmail-highlight-face'.")
90 (defvar rmail-highlight-face nil "\
91 *Face used by Rmail for highlighting headers.")
94 (defvar rmail-delete-after-output nil "\
95 *Non-nil means automatically delete a message that is copied to a file.")
98 (defvar rmail-primary-inbox-list nil "\
99 *List of files which are inboxes for user's primary mail file `~/RMAIL'.
100 `nil' means the default, which is (\"/usr/spool/mail/$USER\")
101 \(the name varies depending on the operating system,
102 and the value of the environment variable MAIL overrides it).")
105 (defvar rmail-mail-new-frame nil
106 "*Non-nil means Rmail makes a new frame for composing outgoing mail.")
109 (defvar rmail-secondary-file-directory "~/"
110 "*Directory for additional secondary Rmail files.")
112 (defvar rmail-secondary-file-regexp "\\.xmail$"
113 "*Regexp for which files are secondary Rmail files.")
116 (defvar rmail-mode-hook nil
117 "List of functions to call when Rmail is invoked.")
120 (defvar rmail-get-new-mail-hook nil
121 "List of functions to call when Rmail has retrieved new mail.")
124 (defvar rmail-show-message-hook nil
125 "List of functions to call when Rmail displays a message.")
128 (defvar rmail-delete-message-hook nil
129 "List of functions to call when Rmail deletes a message.
130 When the hooks are called, the message has been marked deleted but is
131 still the current message in the Rmail buffer.")
133 ;; These may be altered by site-init.el to match the format of mmdf files
134 ;; delimiting used on a given host (delim1 and delim2 from the config
137 (defvar mmdf-delim1 "^\001\001\001\001\n"
138 "Regexp marking the start of an mmdf message")
139 (defvar mmdf-delim2 "^\001\001\001\001\n"
140 "Regexp marking the end of an mmdf message")
142 (defvar rmail-message-filter nil
143 "If non nil, a filter function for new messages in RMAIL.
144 Called with region narrowed to the message, including headers.")
146 (defvar rmail-reply-prefix "Re: "
147 "String to prepend to Subject line when replying to a message.")
149 ;; Some mailers use "Re(2):" or "Re^2:" or "Re: Re:".
150 ;; This pattern should catch all the common variants.
151 (defvar rmail-reply-regexp "\\`\\(Re\\(([0-9]+)\\|\\^[0-9]+\\)?: *\\)*"
152 "Regexp to delete from Subject line before inserting rmail-reply-prefix.")
154 (defvar rmail-display-summary nil
155 "If non nil, the summary buffer is always displayed.")
157 (defvar rmail-mode-map nil)
159 (defvar rmail-inbox-list nil)
160 (defvar rmail-keywords nil)
162 ;; Message counters and markers. Deleted flags.
164 (defvar rmail-current-message nil)
165 (defvar rmail-total-messages nil)
166 (defvar rmail-message-vector nil)
167 (defvar rmail-deleted-vector nil)
169 (defvar rmail-overlay-list nil)
171 (defvar rmail-font-lock-keywords
173 (let* ((cite-prefix "A-Za-z") (cite-suffix (concat cite-prefix "0-9_.@-")))
175 '("^\\(From\\|Sender\\):" . font-lock-function-name-face)
176 '("^Reply-To:.*$" . font-lock-function-name-face)
177 '("^Subject:" . font-lock-comment-face)
178 '("^\\(To\\|Apparently-To\\|Cc\\):" . font-lock-keyword-face)
179 (cons (concat "^[ \t]*"
180 "\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
182 'font-lock-reference-face)
183 '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\|Date\\):.*$"
184 . font-lock-string-face))))
185 "Additional expressions to highlight in Rmail mode.")
187 ;; These are used by autoloaded rmail-summary.
189 (defvar rmail-summary-buffer nil)
190 (defvar rmail-summary-vector nil)
192 ;; `Sticky' default variables.
194 ;; Last individual label specified to a or k.
195 (defvar rmail-last-label nil)
196 ;; Last set of values specified to C-M-n, C-M-p, C-M-s or C-M-l.
197 (defvar rmail-last-multi-labels nil)
198 (defvar rmail-last-regexp nil)
199 (defvar rmail-default-file "~/xmail"
200 "*Default file name for \\[rmail-output].")
201 (defvar rmail-default-rmail-file "~/XMAIL"
202 "*Default file name for \\[rmail-output-to-rmail-file].")
204 ;;; Regexp matching the delimiter of messages in UNIX mail format
205 ;;; (UNIX From lines), minus the initial ^. Note that if you change
206 ;;; this expression, you must change the code in rmail-nuke-pinhead-header
207 ;;; that knows the exact ordering of the \\( \\) subexpressions.
208 (defvar rmail-unix-mail-delimiter
209 (let ((time-zone-regexp
210 (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
211 "\\|[-+]?[0-9][0-9][0-9][0-9]"
217 ;; Username, perhaps with a quoted section that can contain spaces.
220 "\\(\\|\".*\"[^ \n]*\\)"
224 ;; The time the message was sent.
225 "\\([^ \n]*\\) *" ; day of the week
226 "\\([^ ]*\\) *" ; month
227 "\\([0-9]*\\) *" ; day of month
228 "\\([0-9:]*\\) *" ; time of day
230 ;; Perhaps a time zone, specified by an abbreviation, or by a
235 " [0-9][0-9]\\([0-9]*\\) *"
237 ;; On some systems the time zone can appear after the year, too.
241 "\\(remote from .*\\)?"
246 ;; Perform BODY in the summary buffer
247 ;; in such a way that its cursor is properly updated in its own window.
248 (defmacro rmail-select-summary (&rest body)
249 (` (let ((total rmail-total-messages))
250 (if (rmail-summary-displayed)
251 (let ((window (selected-window)))
255 (pop-to-buffer rmail-summary-buffer)
256 ;; rmail-total-messages is a buffer-local var
257 ;; in the rmail buffer.
258 ;; This way we make it available for the body
259 ;; even tho the rmail buffer is not current.
260 (let ((rmail-total-messages total))
262 (select-window window))))
264 (set-buffer rmail-summary-buffer)
265 (let ((rmail-total-messages total))
267 (rmail-maybe-display-summary))))
269 ;;;; *** Rmail Mode ***
272 (defun rmail (&optional file-name-arg)
273 "Read and edit incoming mail.
274 Moves messages into file named by `rmail-file-name' (a babyl format file)
275 and edits that file in RMAIL Mode.
276 Type \\[describe-mode] once editing that file, for a list of RMAIL commands.
278 May be called with file name as argument; then performs rmail editing on
279 that file, but does not copy any new mail into the file.
280 Interactively, if you supply a prefix argument, then you
281 have a chance to specify a file name with the minibuffer.
283 If `rmail-display-summary' is non-nil, make a summary for this RMAIL file."
284 (interactive (if current-prefix-arg
285 (list (read-file-name "Run rmail on RMAIL file: "))))
286 (let* ((file-name (expand-file-name (or file-name-arg rmail-file-name)))
287 (existed (get-file-buffer file-name)))
288 ;; Like find-file, but in the case where a buffer existed
289 ;; and the file was reverted, recompute the message-data.
290 (if (and existed (not (verify-visited-file-modtime existed)))
292 ;; Don't be confused by apparent local-variables spec
293 ;; in the last message in the RMAIL file.
294 (let ((enable-local-variables nil))
295 (find-file file-name))
296 (if (and (verify-visited-file-modtime existed)
297 (eq major-mode 'rmail-mode))
298 (progn (rmail-forget-messages)
299 (rmail-set-message-counters))))
300 (let ((enable-local-variables nil))
301 (find-file file-name)))
302 (if (eq major-mode 'rmail-edit-mode)
303 (error "Exit Rmail Edit mode before getting new mail."))
304 (if (and existed (> (buffer-size) 0))
305 ;; Buffer not new and not empty; ensure in proper mode, but that's all.
306 (or (eq major-mode 'rmail-mode)
309 ;; Convert all or part to Babyl file if possible.
311 (goto-char (point-max))
312 (if (null rmail-inbox-list)
314 (rmail-set-message-counters)
315 (rmail-show-message))))
316 (or (and (null file-name-arg)
317 (rmail-get-new-mail))
318 (rmail-show-message (rmail-first-unseen-message)))
319 (if rmail-display-summary (rmail-summary))
320 (rmail-construct-io-menu)))
322 ;; Given the value of MAILPATH, return a list of inbox file names.
323 ;; This is turned off because it is not clear that the user wants
324 ;; all these inboxes to feed into the primary rmail file.
325 ; (defun rmail-convert-mailpath (string)
327 ; (while (setq idx (string-match "[%:]" string))
328 ; (let ((this (substring string 0 idx)))
329 ; (setq string (substring string (1+ idx)))
330 ; (setq list (cons (if (string-match "%" this)
331 ; (substring this 0 (string-match "%" this))
336 ; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
337 ; will not cause emacs 18.55 problems.
339 (defun rmail-convert-file ()
342 (goto-char (point-min))
343 ;; If file doesn't start like a Babyl file,
344 ;; convert it to one, by adding a header and converting each message.
345 (cond ((looking-at "BABYL OPTIONS:"))
346 ((looking-at "Version: 5\n")
347 ;; Losing babyl file made by old version of Rmail.
348 ;; Just fix the babyl file header; don't make a new one,
349 ;; so we don't lose the Labels: file attribute, etc.
350 (let ((buffer-read-only nil))
351 (insert "BABYL OPTIONS: -*- rmail -*-\n")))
352 ((equal (point-min) (point-max))
353 ;; Empty RMAIL file. Just insert the header.
354 (rmail-insert-rmail-file-header))
356 ;; Non-empty file in non-RMAIL format. Add header and convert.
358 (rmail-insert-rmail-file-header)))
359 ;; If file was not a Babyl file or if there are
360 ;; Unix format messages added at the end,
361 ;; convert file as necessary.
364 (goto-char (point-max))
365 (search-backward "\n\^_")
367 (looking-at "\n*From ")))
368 (let ((buffer-read-only nil))
369 (message "Converting to Babyl format...")
370 ;; If file needs conversion, convert it all,
371 ;; except for the BABYL header.
372 ;; (rmail-convert-to-babyl-format would delete the header.)
373 (goto-char (point-min))
374 (search-forward "\n\^_" nil t)
375 (narrow-to-region (point) (point-max))
376 (rmail-convert-to-babyl-format)
377 (message "Converting to Babyl format...done")))))
379 ;;; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
380 ;;; will not cause emacs 18.55 problems.
382 (defun rmail-insert-rmail-file-header ()
383 (let ((buffer-read-only nil))
384 (insert "BABYL OPTIONS: -*- rmail -*-
387 Note: This is the header of an rmail file.
388 Note: If you are seeing it in rmail,
389 Note: it means the file has no messages in it.\n\^_")))
393 (setq rmail-mode-map (make-keymap))
394 (suppress-keymap rmail-mode-map)
395 (define-key rmail-mode-map "a" 'rmail-add-label)
396 (define-key rmail-mode-map "b" 'rmail-bury)
397 (define-key rmail-mode-map "c" 'rmail-continue)
398 (define-key rmail-mode-map "d" 'rmail-delete-forward)
399 (define-key rmail-mode-map "\C-d" 'rmail-delete-backward)
400 (define-key rmail-mode-map "e" 'rmail-edit-current-message)
401 (define-key rmail-mode-map "f" 'rmail-forward)
402 (define-key rmail-mode-map "g" 'rmail-get-new-mail)
403 (define-key rmail-mode-map "h" 'rmail-summary)
404 (define-key rmail-mode-map "i" 'rmail-input)
405 (define-key rmail-mode-map "j" 'rmail-show-message)
406 (define-key rmail-mode-map "k" 'rmail-kill-label)
407 (define-key rmail-mode-map "l" 'rmail-summary-by-labels)
408 (define-key rmail-mode-map "\e\C-h" 'rmail-summary)
409 (define-key rmail-mode-map "\e\C-l" 'rmail-summary-by-labels)
410 (define-key rmail-mode-map "\e\C-r" 'rmail-summary-by-recipients)
411 (define-key rmail-mode-map "\e\C-s" 'rmail-summary-by-regexp)
412 (define-key rmail-mode-map "\e\C-t" 'rmail-summary-by-topic)
413 (define-key rmail-mode-map "m" 'rmail-mail)
414 (define-key rmail-mode-map "\em" 'rmail-retry-failure)
415 (define-key rmail-mode-map "n" 'rmail-next-undeleted-message)
416 (define-key rmail-mode-map "\en" 'rmail-next-message)
417 (define-key rmail-mode-map "\e\C-n" 'rmail-next-labeled-message)
418 (define-key rmail-mode-map "o" 'rmail-output-to-rmail-file)
419 (define-key rmail-mode-map "\C-o" 'rmail-output)
420 (define-key rmail-mode-map "p" 'rmail-previous-undeleted-message)
421 (define-key rmail-mode-map "\ep" 'rmail-previous-message)
422 (define-key rmail-mode-map "\e\C-p" 'rmail-previous-labeled-message)
423 (define-key rmail-mode-map "q" 'rmail-quit)
424 (define-key rmail-mode-map "r" 'rmail-reply)
425 ;; I find I can't live without the default M-r command -- rms.
426 ;; (define-key rmail-mode-map "\er" 'rmail-search-backwards)
427 (define-key rmail-mode-map "s" 'rmail-expunge-and-save)
428 (define-key rmail-mode-map "\es" 'rmail-search)
429 (define-key rmail-mode-map "t" 'rmail-toggle-header)
430 (define-key rmail-mode-map "u" 'rmail-undelete-previous-message)
431 (define-key rmail-mode-map "w" 'rmail-edit-current-message)
432 (define-key rmail-mode-map "x" 'rmail-expunge)
433 (define-key rmail-mode-map "." 'rmail-beginning-of-message)
434 (define-key rmail-mode-map "<" 'rmail-first-message)
435 (define-key rmail-mode-map ">" 'rmail-last-message)
436 (define-key rmail-mode-map " " 'scroll-up)
437 (define-key rmail-mode-map "\177" 'scroll-down)
438 (define-key rmail-mode-map "?" 'describe-mode)
439 (define-key rmail-mode-map "\C-c\C-s\C-d" 'rmail-sort-by-date)
440 (define-key rmail-mode-map "\C-c\C-s\C-s" 'rmail-sort-by-subject)
441 (define-key rmail-mode-map "\C-c\C-s\C-a" 'rmail-sort-by-author)
442 (define-key rmail-mode-map "\C-c\C-s\C-r" 'rmail-sort-by-recipient)
443 (define-key rmail-mode-map "\C-c\C-s\C-c" 'rmail-sort-by-correspondent)
444 (define-key rmail-mode-map "\C-c\C-s\C-l" 'rmail-sort-by-lines)
445 (define-key rmail-mode-map "\C-c\C-s\C-k" 'rmail-sort-by-keywords)
446 (define-key rmail-mode-map "\C-c\C-n" 'rmail-next-same-subject)
447 (define-key rmail-mode-map "\C-c\C-p" 'rmail-previous-same-subject)
450 (define-key rmail-mode-map [menu-bar] (make-sparse-keymap))
452 (define-key rmail-mode-map [menu-bar classify]
453 (cons "Classify" (make-sparse-keymap "Classify")))
455 (define-key rmail-mode-map [menu-bar classify input-menu]
458 (define-key rmail-mode-map [menu-bar classify output-menu]
461 (define-key rmail-mode-map [menu-bar classify output-inbox]
462 '("Output (inbox)..." . rmail-output))
464 (define-key rmail-mode-map [menu-bar classify output]
465 '("Output (Rmail)..." . rmail-output-to-rmail-file))
467 (define-key rmail-mode-map [menu-bar classify kill-label]
468 '("Kill Label..." . rmail-kill-label))
470 (define-key rmail-mode-map [menu-bar classify add-label]
471 '("Add Label..." . rmail-add-label))
473 (define-key rmail-mode-map [menu-bar summary]
474 (cons "Summary" (make-sparse-keymap "Summary")))
476 (define-key rmail-mode-map [menu-bar summary senders]
477 '("By Senders..." . rmail-summary-by-senders))
479 (define-key rmail-mode-map [menu-bar summary labels]
480 '("By Labels..." . rmail-summary-by-labels))
482 (define-key rmail-mode-map [menu-bar summary recipients]
483 '("By Recipients..." . rmail-summary-by-recipients))
485 (define-key rmail-mode-map [menu-bar summary topic]
486 '("By Topic..." . rmail-summary-by-topic))
488 (define-key rmail-mode-map [menu-bar summary regexp]
489 '("By Regexp..." . rmail-summary-by-regexp))
491 (define-key rmail-mode-map [menu-bar summary all]
492 '("All" . rmail-summary))
494 (define-key rmail-mode-map [menu-bar mail]
495 (cons "Mail" (make-sparse-keymap "Mail")))
497 (define-key rmail-mode-map [menu-bar mail rmail-get-new-mail]
498 '("Get New Mail" . rmail-get-new-mail))
500 (define-key rmail-mode-map [menu-bar mail lambda]
503 (define-key rmail-mode-map [menu-bar mail continue]
504 '("Continue" . rmail-continue))
506 (define-key rmail-mode-map [menu-bar mail resend]
507 '("Re-send..." . rmail-resend))
509 (define-key rmail-mode-map [menu-bar mail forward]
510 '("Forward" . rmail-forward))
512 (define-key rmail-mode-map [menu-bar mail retry]
513 '("Retry" . rmail-retry-failure))
515 (define-key rmail-mode-map [menu-bar mail reply]
516 '("Reply" . rmail-reply))
518 (define-key rmail-mode-map [menu-bar mail mail]
519 '("Mail" . rmail-mail))
521 (define-key rmail-mode-map [menu-bar delete]
522 (cons "Delete" (make-sparse-keymap "Delete")))
524 (define-key rmail-mode-map [menu-bar delete expunge/save]
525 '("Expunge/Save" . rmail-expunge-and-save))
527 (define-key rmail-mode-map [menu-bar delete expunge]
528 '("Expunge" . rmail-expunge))
530 (define-key rmail-mode-map [menu-bar delete undelete]
531 '("Undelete" . rmail-undelete-previous-message))
533 (define-key rmail-mode-map [menu-bar delete delete]
534 '("Delete" . rmail-delete-forward))
536 (define-key rmail-mode-map [menu-bar move]
537 (cons "Move" (make-sparse-keymap "Move")))
539 (define-key rmail-mode-map [menu-bar move search-back]
540 '("Search Back..." . rmail-search-backwards))
542 (define-key rmail-mode-map [menu-bar move search]
543 '("Search..." . rmail-search))
545 (define-key rmail-mode-map [menu-bar move previous]
546 '("Previous Nondeleted" . rmail-previous-undeleted-message))
548 (define-key rmail-mode-map [menu-bar move next]
549 '("Next Nondeleted" . rmail-next-undeleted-message))
551 (define-key rmail-mode-map [menu-bar move last]
552 '("Last" . rmail-last-message))
554 (define-key rmail-mode-map [menu-bar move first]
555 '("First" . rmail-first-message))
557 (define-key rmail-mode-map [menu-bar move previous]
558 '("Previous" . rmail-previous-message))
560 (define-key rmail-mode-map [menu-bar move next]
561 '("Next" . rmail-next-message))
563 ;; Rmail mode is suitable only for specially formatted data.
564 (put 'rmail-mode 'mode-class 'special)
566 (defun rmail-mode-kill-summary ()
567 (if rmail-summary-buffer (kill-buffer rmail-summary-buffer)))
571 "Rmail Mode is used by \\<rmail-mode-map>\\[rmail] for editing Rmail files.
572 All normal editing commands are turned off.
573 Instead, these commands are available:
575 \\[rmail-beginning-of-message] Move point to front of this message (same as \\[beginning-of-buffer]).
576 \\[scroll-up] Scroll to next screen of this message.
577 \\[scroll-down] Scroll to previous screen of this message.
578 \\[rmail-next-undeleted-message] Move to Next non-deleted message.
579 \\[rmail-previous-undeleted-message] Move to Previous non-deleted message.
580 \\[rmail-next-message] Move to Next message whether deleted or not.
581 \\[rmail-previous-message] Move to Previous message whether deleted or not.
582 \\[rmail-first-message] Move to the first message in Rmail file.
583 \\[rmail-last-message] Move to the last message in Rmail file.
584 \\[rmail-show-message] Jump to message specified by numeric position in file.
585 \\[rmail-search] Search for string and show message it is found in.
586 \\[rmail-delete-forward] Delete this message, move to next nondeleted.
587 \\[rmail-delete-backward] Delete this message, move to previous nondeleted.
588 \\[rmail-undelete-previous-message] Undelete message. Tries current message, then earlier messages
589 till a deleted message is found.
590 \\[rmail-edit-current-message] Edit the current message. \\[rmail-cease-edit] to return to Rmail.
591 \\[rmail-expunge] Expunge deleted messages.
592 \\[rmail-expunge-and-save] Expunge and save the file.
593 \\[rmail-quit] Quit Rmail: expunge, save, then switch to another buffer.
594 \\[save-buffer] Save without expunging.
595 \\[rmail-get-new-mail] Move new mail from system spool directory into this file.
596 \\[rmail-mail] Mail a message (same as \\[mail-other-window]).
597 \\[rmail-continue] Continue composing outgoing message started before.
598 \\[rmail-reply] Reply to this message. Like \\[rmail-mail] but initializes some fields.
599 \\[rmail-retry-failure] Send this message again. Used on a mailer failure message.
600 \\[rmail-forward] Forward this message to another user.
601 \\[rmail-output-to-rmail-file] Output this message to an Rmail file (append it).
602 \\[rmail-output] Output this message to a Unix-format mail file (append it).
603 \\[rmail-input] Input Rmail file. Run Rmail on that file.
604 \\[rmail-add-label] Add label to message. It will be displayed in the mode line.
605 \\[rmail-kill-label] Kill label. Remove a label from current message.
606 \\[rmail-next-labeled-message] Move to Next message with specified label
607 (label defaults to last one specified).
608 Standard labels: filed, unseen, answered, forwarded, deleted.
609 Any other label is present only if you add it with \\[rmail-add-label].
610 \\[rmail-previous-labeled-message] Move to Previous message with specified label
611 \\[rmail-summary] Show headers buffer, with a one line summary of each message.
612 \\[rmail-summary-by-labels] Summarize only messages with particular label(s).
613 \\[rmail-summary-by-recipients] Summarize only messages with particular recipient(s).
614 \\[rmail-summary-by-regexp] Summarize only messages with particular regexp(s).
615 \\[rmail-summary-by-topic] Summarize only messages with subject line regexp(s).
616 \\[rmail-toggle-header] Toggle display of complete header."
619 (rmail-set-message-counters)
620 (rmail-show-message rmail-total-messages))
622 (defun rmail-mode-2 ()
623 (kill-all-local-variables)
626 (run-hooks 'rmail-mode-hook))
628 (defun rmail-mode-1 ()
629 (setq major-mode 'rmail-mode)
630 (setq mode-name "RMAIL")
631 (setq buffer-read-only t)
632 ;; No need to auto save RMAIL files in normal circumstances
633 ;; because they contain no info except attribute changes
634 ;; and deletion of messages.
635 ;; The one exception is when messages are copied into an Rmail mode buffer.
636 ;; rmail-output-to-rmail-file enables auto save when you do that.
637 (setq buffer-auto-save-file-name nil)
638 (if (boundp 'mode-line-modified)
639 (setq mode-line-modified "--- ")
640 (setq mode-line-format
641 (cons "--- " (cdr (default-value 'mode-line-format)))))
642 (use-local-map rmail-mode-map)
643 (set-syntax-table text-mode-syntax-table)
644 (setq local-abbrev-table text-mode-abbrev-table))
646 (defun rmail-variables ()
647 (make-local-variable 'revert-buffer-function)
648 (setq revert-buffer-function 'rmail-revert)
649 (make-local-variable 'font-lock-defaults)
650 (setq font-lock-defaults '(rmail-font-lock-keywords t))
651 (make-local-variable 'rmail-last-label)
652 (make-local-variable 'rmail-last-regexp)
653 (make-local-variable 'rmail-deleted-vector)
654 (make-local-variable 'rmail-summary-buffer)
655 (make-local-variable 'rmail-summary-vector)
656 (make-local-variable 'rmail-current-message)
657 (make-local-variable 'rmail-total-messages)
658 (make-local-variable 'require-final-newline)
659 (setq require-final-newline nil)
660 (make-local-variable 'rmail-overlay-list)
661 (setq rmail-overlay-list nil)
662 (make-local-variable 'version-control)
663 (setq version-control 'never)
664 (make-local-variable 'kill-buffer-hook)
665 (add-hook 'kill-buffer-hook 'rmail-mode-kill-summary)
666 (make-local-variable 'file-precious-flag)
667 (setq file-precious-flag t)
668 (make-local-variable 'rmail-message-vector)
669 (make-local-variable 'rmail-inbox-list)
670 (setq rmail-inbox-list (rmail-parse-file-inboxes))
671 ;; Provide default set of inboxes for primary mail file ~/RMAIL.
672 (and (null rmail-inbox-list)
673 (or (equal buffer-file-name (expand-file-name rmail-file-name))
674 (equal buffer-file-truename
675 (abbreviate-file-name (file-truename rmail-file-name))))
676 (setq rmail-inbox-list
677 (or rmail-primary-inbox-list
678 (list (or (getenv "MAIL")
679 (concat rmail-spool-directory
680 (user-login-name)))))))
681 (make-local-variable 'rmail-keywords)
682 ;; this gets generated as needed
683 (setq rmail-keywords nil))
685 ;; Handle M-x revert-buffer done in an rmail-mode buffer.
686 (defun rmail-revert (arg noconfirm)
687 (let ((revert-buffer-function (default-value 'revert-buffer-function)))
688 ;; Call our caller again, but this time it does the default thing.
689 (if (revert-buffer arg noconfirm)
690 ;; If the user said "yes", and we changed something,
691 ;; reparse the messages.
694 (goto-char (point-max))
697 ;; Return a list of files from this buffer's Mail: option.
698 ;; Does not assume that messages have been parsed.
699 ;; Just returns nil if buffer does not look like Babyl format.
700 (defun rmail-parse-file-inboxes ()
705 (cond ((looking-at "BABYL OPTIONS:")
706 (search-forward "\n\^_" nil 'move)
707 (narrow-to-region 1 (point))
709 (if (search-forward "\nMail:" nil t)
711 (narrow-to-region (point) (progn (end-of-line) (point)))
712 (goto-char (point-min))
713 (mail-parse-comma-list))))))))
715 (defun rmail-expunge-and-save ()
716 "Expunge and save RMAIL file."
720 (if (rmail-summary-exists)
721 (rmail-select-summary (set-buffer-modified-p nil))))
726 (rmail-expunge-and-save)
727 ;; Don't switch to the summary buffer even if it was recently visible.
728 (if rmail-summary-buffer
730 (replace-buffer-in-windows rmail-summary-buffer)
731 (bury-buffer rmail-summary-buffer)))
732 (let ((obuf (current-buffer)))
733 (replace-buffer-in-windows obuf)
737 "Bury current Rmail buffer and its summary buffer."
739 ;; This let var was called rmail-buffer, but that interfered
740 ;; with the buffer-local var used in summary buffers.
741 (let ((buffer-to-bury (current-buffer)))
742 (if (rmail-summary-exists)
744 (while (setq window (get-buffer-window rmail-summary-buffer))
745 (set-window-buffer window (other-buffer rmail-summary-buffer)))
746 (bury-buffer rmail-summary-buffer)))
747 (switch-to-buffer (other-buffer (current-buffer)))
748 (bury-buffer buffer-to-bury)))
750 (defun rmail-duplicate-message ()
751 "Create a duplicated copy of the current message.
752 The duplicate copy goes into the Rmail file just after the
756 (let ((buffer-read-only nil)
757 (number rmail-current-message)
758 (string (buffer-substring (rmail-msgbeg rmail-current-message)
759 (rmail-msgend rmail-current-message))))
760 (goto-char (rmail-msgend rmail-current-message))
762 (rmail-forget-messages)
763 (rmail-show-message number)
764 (message "Message duplicated")))
767 (defun rmail-input (filename)
768 "Run Rmail on file FILENAME."
769 (interactive "FRun rmail on RMAIL file: ")
772 ;; Return a list of file names for all files in or under START
773 ;; whose names match rmail-secondary-file-regexp.
774 ;; This includes START itself, if that name matches.
775 ;; But normally START is a directory.
776 (defun rmail-find-all-files (start)
777 (if (file-accessible-directory-p start)
779 (let ((files (directory-files start t
780 rmail-secondary-file-regexp t))
784 (setq file (car files))
785 (setq files (cdr files))
787 (rmail-find-all-files file)
789 ;; Sort here instead of in directory-files
790 ;; because this list is usually much shorter.
792 (let ((case-fold-search nil))
793 (if (string-match rmail-secondary-file-regexp start)
794 (list (file-name-nondirectory start))))))
796 (defun rmail-list-to-menu (menu-name l action &optional full-name)
797 (let ((menu (make-sparse-keymap menu-name)))
799 (function (lambda (item)
804 (rmail-list-to-menu (car item) (cdr item)
807 (concat full-name "/"
810 (setq name (car item)))
814 (list 'lambda () '(interactive)
818 (concat full-name "/" item)
820 rmail-secondary-file-directory))))))
821 (define-key menu (vector (intern name))
822 (cons name command)))))
826 ;; This command is always "disabled" when it appears in a menu.
827 (put 'rmail-disable-menu 'menu-enable ''nil)
829 (defun rmail-construct-io-menu ()
830 (let ((files (rmail-find-all-files rmail-secondary-file-directory)))
833 (define-key rmail-mode-map [menu-bar classify input-menu]
834 (cons "Input Rmail File"
835 (rmail-list-to-menu "Input Rmail File"
838 (define-key rmail-mode-map [menu-bar classify output-menu]
839 (cons "Output Rmail File"
840 (rmail-list-to-menu "Output Rmail File"
842 'rmail-output-to-rmail-file))))
844 (define-key rmail-mode-map [menu-bar classify input-menu]
845 '("Input Rmail File" . rmail-disable-menu))
846 (define-key rmail-mode-map [menu-bar classify output-menu]
847 '("Output Rmail File" . rmail-disable-menu)))))
850 ;;;; *** Rmail input ***
852 ;; RLK feature not added in this version:
853 ;; argument specifies inbox file or files in various ways.
855 (defun rmail-get-new-mail (&optional file-name)
856 "Move any new mail from this RMAIL file's inbox files.
857 The inbox files can be specified with the file's Mail: option. The
858 variable `rmail-primary-inbox-list' specifies the inboxes for your
859 primary RMAIL file if it has no Mail: option. By default, this is
860 your /usr/spool/mail/$USER.
862 You can also specify the file to get new mail from. In this case, the
863 file of new mail is not changed or deleted. Noninteractively, you can
864 pass the inbox file name as an argument. Interactively, a prefix
865 argument causes us to read a file name and use that file as the inbox.
867 This function runs `rmail-get-new-mail-hook' before saving the updated file.
868 It returns t if it got any new messages."
870 (list (if current-prefix-arg
871 (read-file-name "Get new mail from file: "))))
872 ;; If the disk file has been changed from under us,
873 ;; revert to it before we get new mail.
874 (or (verify-visited-file-modtime (current-buffer))
875 (find-file (buffer-file-name)))
876 (rmail-maybe-set-message-counters)
878 ;; Get rid of all undo records for this buffer.
879 (or (eq buffer-undo-list t)
880 (setq buffer-undo-list nil))
882 (let ((opoint (point))
885 ;; If buffer has not changed yet, and has not been saved yet,
886 ;; don't replace the old backup file now.
887 (make-backup-files (and make-backup-files (buffer-modified-p)))
888 (buffer-read-only nil)
889 ;; Don't make undo records for what we do in getting mail.
890 (buffer-undo-list t))
891 (goto-char (point-max))
892 (skip-chars-backward " \t\n") ; just in case of brain damage
893 (delete-region (point) (point-max)) ; caused by require-final-newline
896 (narrow-to-region (point) (point))
897 ;; Read in the contents of the inbox files,
898 ;; renaming them as necessary,
899 ;; and adding to the list of files to delete eventually.
901 (rmail-insert-inbox-text (list file-name) nil)
902 (setq delete-files (rmail-insert-inbox-text rmail-inbox-list t)))
903 ;; Scan the new text and convert each message to babyl format.
904 (goto-char (point-min))
906 (setq new-messages (rmail-convert-to-babyl-format)))
907 (or (zerop new-messages)
910 (search-backward "\n\^_" nil t)
911 (narrow-to-region (point) (point-max))
912 (goto-char (1+ (point-min)))
913 (rmail-count-new-messages)
914 (run-hooks 'rmail-get-new-mail-hook)
916 ;; Delete the old files, now that babyl file is saved.
919 ;; First, try deleting.
921 (delete-file (car delete-files))
923 ;; If we can't delete it, truncate it.
924 (write-region (point) (point) (car delete-files))))
926 (setq delete-files (cdr delete-files)))))
927 (if (= new-messages 0)
928 (progn (goto-char opoint)
929 (if (or file-name rmail-inbox-list)
930 (message "(No new mail has arrived)"))
932 (if (rmail-summary-exists)
933 (rmail-select-summary
934 (rmail-update-summary)))
935 (message "%d new message%s read"
936 new-messages (if (= 1 new-messages) "" "s"))
937 ;; Move to the first new message
938 ;; unless we have other unseen messages before it.
939 (rmail-show-message (rmail-first-unseen-message))
940 ;; Update the displayed time, since that will clear out
941 ;; the flag that says you have mail.
942 (if (eq (process-status "display-time") 'run)
943 (display-time-filter display-time-process ""))
945 ;; Don't leave the buffer screwed up if we get a disk-full error.
946 (rmail-show-message)))
948 (defun rmail-insert-inbox-text (files renamep)
949 (let (file tofile delete-files movemail popmail)
951 (setq file (file-truename
952 (expand-file-name (substitute-in-file-name (car files))))
953 tofile (expand-file-name
954 ;; Generate name to move to from inbox name,
955 ;; in case of multiple inboxes that need moving.
956 (concat ".newmail-" (file-name-nondirectory file))
957 ;; Use the directory of this rmail file
958 ;; because it's a nuisance to use the homedir
959 ;; if that is on a full disk and this rmail
962 (expand-file-name buffer-file-name))))
963 ;; Always use movemail to rename the file,
964 ;; since there can be mailboxes in various directories.
966 ;;; ;; If getting from mail spool directory,
967 ;;; ;; use movemail to move rather than just renaming,
968 ;;; ;; so as to interlock with the mailer.
969 ;;; (setq movemail (string= file
971 ;;; (concat rmail-spool-directory
972 ;;; (file-name-nondirectory file)))))
973 (setq popmail (string-match "^po:" (file-name-nondirectory file)))
974 (if popmail (setq file (file-name-nondirectory file)
978 ;; On some systems, /usr/spool/mail/foo is a directory
979 ;; and the actual inbox is /usr/spool/mail/foo/foo.
980 (if (file-directory-p file)
981 (setq file (expand-file-name (user-login-name)
984 (message "Getting mail from post office ..."))
985 ((and (file-exists-p tofile)
986 (/= 0 (nth 7 (file-attributes tofile))))
987 (message "Getting mail from %s..." tofile))
988 ((and (file-exists-p file)
989 (/= 0 (nth 7 (file-attributes file))))
990 (message "Getting mail from %s..." file)))
991 ;; Set TOFILE if have not already done so, and
992 ;; rename or copy the file FILE to TOFILE if and as appropriate.
995 ((or (file-exists-p tofile) (and (not popmail)
996 (not (file-exists-p file))))
998 ((and (not movemail) (not popmail))
999 ;; Try copying. If that fails (perhaps no space),
1002 (copy-file file tofile nil)
1004 ;; Third arg is t so we can replace existing file TOFILE.
1005 (rename-file file tofile t)))
1006 ;; Make the real inbox file empty.
1007 ;; Leaving it deleted could cause lossage
1008 ;; because mailers often won't create the file.
1010 (write-region (point) (point) file)
1016 (setq errors (generate-new-buffer " *rmail loss*"))
1017 (buffer-disable-undo errors)
1019 (or rmail-movemail-program
1020 (expand-file-name "movemail" exec-directory))
1021 nil errors nil file tofile)
1022 (if (not (buffer-modified-p errors))
1023 ;; No output => movemail won
1026 (subst-char-in-region (point-min) (point-max)
1028 (goto-char (point-max))
1029 (skip-chars-backward " \t")
1030 (delete-region (point) (point-max))
1031 (goto-char (point-min))
1032 (if (looking-at "movemail: ")
1033 (delete-region (point-min) (match-end 0)))
1035 (message (concat "movemail: "
1036 (buffer-substring (point-min)
1040 (if errors (kill-buffer errors))))))
1041 ;; At this point, TOFILE contains the name to read:
1042 ;; Either the alternate name (if we renamed)
1043 ;; or the actual inbox (if not renaming).
1044 (if (file-exists-p tofile)
1046 (goto-char (point-max))
1047 (setq size (nth 1 (insert-file-contents tofile)))
1048 (goto-char (point-max))
1049 (or (= (preceding-char) ?\n)
1052 (setq delete-files (cons tofile delete-files))))
1054 (setq files (cdr files)))
1057 ;; the rmail-break-forwarded-messages feature is not implemented
1058 (defun rmail-convert-to-babyl-format ()
1059 (let ((count 0) start
1060 (case-fold-search nil)
1061 (invalid-input-resync
1062 (function (lambda ()
1063 (message "Invalid Babyl format in inbox!")
1065 ;; Try to get back in sync with a real message.
1066 (if (re-search-forward
1067 (concat mmdf-delim1 "\\|^From") nil t)
1069 (goto-char (point-max)))))))
1070 (goto-char (point-min))
1073 (cond ((looking-at "BABYL OPTIONS:");Babyl header
1074 (if (search-forward "\n\^_" nil t)
1075 ;; If we find the proper terminator, delete through there.
1076 (delete-region (point-min) (point))
1077 (funcall invalid-input-resync)
1078 (delete-region (point-min) (point))))
1079 ;; Babyl format message
1081 (or (search-forward "\n\^_" nil t)
1082 (funcall invalid-input-resync))
1083 (setq count (1+ count))
1084 ;; Make sure there is no extra white space after the ^_
1085 ;; at the end of the message.
1086 ;; Narrowing will make sure that whatever follows the junk
1087 ;; will be treated properly.
1088 (delete-region (point)
1090 (skip-chars-forward " \t\n")
1092 (narrow-to-region (point) (point-max)))
1094 ((let ((case-fold-search t))
1095 (looking-at mmdf-delim1))
1096 (let ((case-fold-search t))
1097 (replace-match "\^L\n0, unseen,,\n*** EOOH ***\n")
1098 (setq start (point))
1099 (re-search-forward mmdf-delim2 nil t)
1100 (replace-match "\^_"))
1103 (narrow-to-region start (1- (point)))
1104 (goto-char (point-min))
1105 (while (search-forward "\n\^_" nil t); single char "\^_"
1106 (replace-match "\n^_")))); 2 chars: "^" and "_"
1107 (narrow-to-region (point) (point-max))
1108 (setq count (1+ count)))
1110 ((looking-at "^From ")
1111 (setq start (point))
1112 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
1113 (rmail-nuke-pinhead-header)
1114 ;; If this message has a Content-Length field,
1115 ;; skip to the end of the contents.
1116 (let* ((header-end (save-excursion
1117 (and (re-search-forward "\n\n" nil t)
1119 (case-fold-search t)
1121 ;; Get the numeric value from the Content-Length field.
1123 ;; Back up to end of prev line,
1124 ;; in case the Content-Length field comes first.
1126 (and (search-forward "\ncontent-length: "
1129 (eol (progn (end-of-line) (point))))
1130 (string-to-int (buffer-substring beg eol)))))))
1132 (if (and (natnump size)
1133 (<= (+ header-end size) (point-max))
1134 ;; Make sure this would put us at a position
1135 ;; that we could continue from.
1137 (goto-char (+ header-end size))
1138 (skip-chars-forward "\n")
1140 (and (looking-at "BABYL OPTIONS:")
1141 (search-forward "\n\^_" nil t))
1142 (and (looking-at "\^L")
1143 (search-forward "\n\^_" nil t))
1144 (let ((case-fold-search t))
1145 (looking-at mmdf-delim1))
1146 (looking-at "From "))))
1147 (goto-char (+ header-end size))
1148 (message "Ignoring invalid Content-Length field")
1151 (if (re-search-forward
1152 (concat "^[\^_]?\\("
1153 rmail-unix-mail-delimiter
1156 "^BABYL OPTIONS:\\|"
1157 "\^L\n[01],\\)") nil t)
1158 (goto-char (match-beginning 1))
1159 (goto-char (point-max)))
1160 (setq count (1+ count))
1163 (narrow-to-region start (point))
1164 (goto-char (point-min))
1165 (while (search-forward "\n\^_" nil t); single char
1166 (replace-match "\n^_")))); 2 chars: "^" and "_"
1168 (narrow-to-region (point) (point-max)))
1170 ;; This kludge is because some versions of sendmail.el
1171 ;; insert an extra newline at the beginning that shouldn't
1172 ;; be there. sendmail.el has been fixed, but old versions
1173 ;; may still be in use. -- rms, 7 May 1993.
1174 ((eolp) (delete-char 1))
1175 (t (error "Cannot convert to babyl format")))))
1178 ;; Delete the "From ..." line, creating various other headers with
1179 ;; information from it if they don't already exist. Now puts the
1180 ;; original line into a mail-from: header line for debugging and for
1181 ;; use by the rmail-output function.
1182 (defun rmail-nuke-pinhead-header ()
1185 (let ((start (point))
1188 (search-forward "\n\n")
1190 (goto-char (point-max))
1194 (narrow-to-region start end)
1195 (let ((case-fold-search t))
1197 (setq has-from (search-forward "\nFrom:" nil t))
1199 (setq has-date (and (search-forward "\nDate:" nil t) (point)))
1201 (let ((case-fold-search nil))
1202 (if (re-search-forward (concat "^" rmail-unix-mail-delimiter) nil t)
1206 ;; Keep and reformat the date if we don't
1207 ;; have a Date: field.
1211 "Date: \\3, \\5 \\4 \\9 \\6 "
1213 ;; The timezone could be matched by group 7 or group 10.
1214 ;; If neither of them matched, assume EST, since only
1215 ;; Easterners would be so sloppy.
1216 ;; It's a shame the substitution can't use "\\10".
1218 ((/= (match-beginning 7) (match-end 7)) "\\7")
1219 ((/= (match-beginning 10) (match-end 10))
1220 (buffer-substring (match-beginning 10)
1224 ;; Keep and reformat the sender if we don't
1225 ;; have a From: field.
1231 ;;;; *** Rmail Message Formatting and Header Manipulation ***
1233 (defun rmail-reformat-message (beg end)
1236 (if (/= (following-char) ?0)
1237 (error "Bad format in RMAIL file."))
1238 (let ((buffer-read-only nil)
1239 (delta (- (buffer-size) end)))
1243 (let ((case-fold-search t))
1244 (while (looking-at "Summary-line:\\|Mail-From:")
1246 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*\n")
1247 (delete-region (point)
1248 (progn (forward-line 1) (point))))
1249 (let ((str (buffer-substring (point)
1250 (save-excursion (search-forward "\n\n" end 'move)
1252 (insert str "*** EOOH ***\n")
1253 (narrow-to-region (point) (- (buffer-size) delta)))
1254 (goto-char (point-min))
1255 (if rmail-message-filter (funcall rmail-message-filter))
1256 (if (or rmail-displayed-headers rmail-ignored-headers)
1257 (rmail-clear-headers))))
1259 (defun rmail-clear-headers (&optional ignored-headers)
1260 "Delete all header fields that Rmail should not show.
1261 If the optional argument IGNORED-HEADERS is non-nil,
1262 delete all header fields whose names match that regexp.
1263 Otherwise, if `rmail-displayed-headers' is non-nil,
1264 delete all header fields *except* those whose names match that regexp.
1265 Otherwise, delete all header fields whose names match `rmail-ignored-headers'."
1266 (if (search-forward "\n\n" nil t)
1267 (if (and rmail-displayed-headers (null ignored-headers))
1269 (narrow-to-region (point-min) (point))
1270 (let ((buffer-read-only nil) lim)
1271 (goto-char (point-min))
1272 (while (save-excursion
1273 (re-search-forward "\n[^ \t]")
1275 (setq lim (1- (point)))))
1277 (re-search-forward rmail-displayed-headers lim t))
1279 (delete-region (point) lim))))
1280 (goto-char (point-min)))
1281 (or ignored-headers (setq ignored-headers rmail-ignored-headers))
1283 (narrow-to-region (point-min) (point))
1284 (let ((buffer-read-only nil))
1285 (while (let ((case-fold-search t))
1286 (goto-char (point-min))
1287 (re-search-forward ignored-headers nil t))
1289 (delete-region (point)
1290 (progn (re-search-forward "\n[^ \t]")
1291 (1- (point))))))))))
1293 (defun rmail-toggle-header ()
1294 "Show original message header if pruned header currently shown, or vice versa."
1296 (rmail-maybe-set-message-counters)
1297 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
1298 (let ((buffer-read-only nil))
1299 (goto-char (point-min))
1301 (if (= (following-char) ?1)
1302 (progn (delete-char 1)
1305 (let ((case-fold-search t))
1306 (while (looking-at "Summary-Line:\\|Mail-From:")
1308 (insert "*** EOOH ***\n")
1310 (search-forward "\n*** EOOH ***\n")
1312 (let ((temp (point)))
1313 (and (search-forward "\n\n" nil t)
1314 (delete-region temp (point))))
1315 (goto-char (point-min))
1316 (search-forward "\n*** EOOH ***\n")
1317 (narrow-to-region (point) (point-max)))
1318 (rmail-reformat-message (point-min) (point-max))))
1319 (rmail-highlight-headers))
1321 ;;;; *** Rmail Attributes and Keywords ***
1323 ;; Make a string describing current message's attributes and keywords
1324 ;; and set it up as the name of a minor mode
1325 ;; so it will appear in the mode line.
1326 (defun rmail-display-labels ()
1327 (let ((blurb "") (beg (point-min-marker)) (end (point-max-marker)))
1332 (goto-char (rmail-msgbeg rmail-current-message))
1334 (if (looking-at "[01],")
1336 (narrow-to-region (point) (progn (end-of-line) (point)))
1337 ;; Truly valid BABYL format requires a space before each
1338 ;; attribute or keyword name. Put them in if missing.
1339 (let (buffer-read-only)
1340 (goto-char (point-min))
1341 (while (search-forward "," nil t)
1342 (or (looking-at "[ ,]") (eobp)
1344 (goto-char (point-max))
1345 (if (search-backward ",," nil 'move)
1347 (if (> (point) (1+ (point-min)))
1348 (setq blurb (buffer-substring (+ 1 (point-min)) (point))))
1349 (if (> (- (point-max) (point)) 2)
1353 (buffer-substring (+ (point) 3)
1354 (1- (point-max)))))))))))
1355 ;; Note: we don't use save-restriction because that does not work right
1356 ;; if changes are made outside the saved restriction
1357 ;; before that restriction is restored.
1358 (narrow-to-region beg end)
1359 (set-marker beg nil)
1360 (set-marker end nil)))
1361 (while (string-match " +," blurb)
1362 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
1363 (substring blurb (match-end 0)))))
1364 (while (string-match ", +" blurb)
1365 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
1366 (substring blurb (match-end 0)))))
1367 (setq mode-line-process
1369 rmail-current-message rmail-total-messages blurb))))
1371 ;; Turn an attribute of a message on or off according to STATE.
1372 ;; ATTR is the name of the attribute, as a string.
1373 ;; MSGNUM is message number to change; nil means current message.
1374 (defun rmail-set-attribute (attr state &optional msgnum)
1375 (let ((omax (point-max-marker))
1376 (omin (point-min-marker))
1377 (buffer-read-only nil))
1378 (or msgnum (setq msgnum rmail-current-message))
1383 (goto-char (+ 3 (rmail-msgbeg msgnum)))
1386 (null (search-backward (concat ", " attr ",")
1387 (prog1 (point) (end-of-line)) t)))))
1388 (or (eq curstate (not (not state)))
1390 (delete-region (point) (1- (match-end 0)))
1393 (insert " " attr ","))))
1394 (if (string= attr "deleted")
1395 (rmail-set-message-deleted-p msgnum state)))
1396 ;; Note: we don't use save-restriction because that does not work right
1397 ;; if changes are made outside the saved restriction
1398 ;; before that restriction is restored.
1399 (narrow-to-region omin omax)
1400 (set-marker omin nil)
1401 (set-marker omax nil)
1402 (if (= msgnum rmail-current-message)
1403 (rmail-display-labels))))))
1405 ;; Return t if the attributes/keywords line of msg number MSG
1406 ;; contains a match for the regexp LABELS.
1407 (defun rmail-message-labels-p (msg labels)
1411 (goto-char (rmail-msgbeg msg))
1413 (re-search-backward labels (prog1 (point) (end-of-line)) t))))
1415 ;;;; *** Rmail Message Selection And Support ***
1417 (defun rmail-msgend (n)
1418 (marker-position (aref rmail-message-vector (1+ n))))
1420 (defun rmail-msgbeg (n)
1421 (marker-position (aref rmail-message-vector n)))
1423 (defun rmail-widen-to-current-msgbeg (function)
1424 "Call FUNCTION with point at start of internal data of current message.
1425 Assumes that bounds were previously narrowed to display the message in Rmail.
1426 The bounds are widened enough to move point where desired, then narrowed
1429 FUNCTION may not change the visible text of the message, but it may
1430 change the invisible header text."
1432 (let ((obeg (- (point-max) (point-min))))
1435 (narrow-to-region (rmail-msgbeg rmail-current-message)
1437 (goto-char (point-min))
1439 ;; Note: we don't use save-restriction because that does not work right
1440 ;; if changes are made outside the saved restriction
1441 ;; before that restriction is restored.
1442 ;; Here we assume that changes made by FUNCTION
1443 ;; occur before the visible region of the message.
1444 (narrow-to-region (- (point-max) obeg) (point-max))))))
1446 (defun rmail-forget-messages ()
1448 (if (vectorp rmail-message-vector)
1450 (v rmail-message-vector)
1453 (move-marker (aref v i) nil)
1455 (setq rmail-message-vector nil)
1456 (setq rmail-deleted-vector nil)))
1458 (defun rmail-maybe-set-message-counters ()
1459 (if (not (and rmail-deleted-vector
1460 rmail-message-vector
1461 rmail-current-message
1462 rmail-total-messages))
1463 (rmail-set-message-counters)))
1465 (defun rmail-count-new-messages (&optional nomsg)
1466 (let* ((case-fold-search nil)
1470 (or nomsg (message "Counting new messages..."))
1471 (goto-char (point-max))
1472 ;; Put at the end of messages-head
1473 ;; the entry for message N+1, which marks
1474 ;; the end of message N. (N = number of messages).
1475 (search-backward "\n\^_")
1477 (setq messages-head (list (point-marker)))
1478 (rmail-set-message-counters-counter (point-min))
1479 (setq rmail-current-message (1+ rmail-total-messages))
1480 (setq rmail-total-messages
1481 (+ rmail-total-messages total-messages))
1482 (setq rmail-message-vector
1483 (vconcat rmail-message-vector (cdr messages-head)))
1484 (aset rmail-message-vector
1485 rmail-current-message (car messages-head))
1486 (setq rmail-deleted-vector
1487 (concat rmail-deleted-vector deleted-head))
1488 (setq rmail-summary-vector
1489 (vconcat rmail-summary-vector (make-vector total-messages nil)))
1490 (goto-char (point-min))
1491 (or nomsg (message "Counting new messages...done (%d)" total-messages))))
1493 (defun rmail-set-message-counters ()
1494 (rmail-forget-messages)
1498 (let* ((point-save (point))
1500 (messages-after-point)
1501 (case-fold-search nil)
1504 (message "Counting messages...")
1505 (goto-char (point-max))
1506 ;; Put at the end of messages-head
1507 ;; the entry for message N+1, which marks
1508 ;; the end of message N. (N = number of messages).
1509 (search-backward "\n\^_" nil t)
1510 (if (/= (point) (point-max)) (forward-char 1))
1511 (setq messages-head (list (point-marker)))
1512 (rmail-set-message-counters-counter (min (point) point-save))
1513 (setq messages-after-point total-messages)
1514 (rmail-set-message-counters-counter)
1515 (setq rmail-total-messages total-messages)
1516 (setq rmail-current-message
1518 (max 1 (- total-messages messages-after-point))))
1519 (setq rmail-message-vector
1520 (apply 'vector (cons (point-min-marker) messages-head))
1521 rmail-deleted-vector (concat "D" deleted-head)
1522 rmail-summary-vector (make-vector rmail-total-messages nil))
1523 (message "Counting messages...done")))))
1525 (defun rmail-set-message-counters-counter (&optional stop)
1526 (while (search-backward "\n\^_\^L\n" stop t)
1528 (setq messages-head (cons (point-marker) messages-head))
1531 (cons (if (search-backward ", deleted,"
1537 (if (zerop (% (setq total-messages (1+ total-messages)) 20))
1538 (message "Counting messages...%d" total-messages))))
1540 (defun rmail-beginning-of-message ()
1541 "Show current message starting from the beginning."
1543 (rmail-show-message rmail-current-message))
1545 (defun rmail-show-message (&optional n no-summary)
1546 "Show message number N (prefix argument), counting from start of file.
1547 If summary buffer is currently displayed, update current message there also."
1549 (rmail-maybe-set-message-counters)
1551 (if (zerop rmail-total-messages)
1552 (progn (narrow-to-region (point-min) (1- (point-max)))
1553 (goto-char (point-min))
1554 (setq mode-line-process nil))
1557 (setq n rmail-current-message)
1560 rmail-current-message 1
1561 blurb "No previous message"))
1562 ((> n rmail-total-messages)
1563 (setq n rmail-total-messages
1564 rmail-current-message rmail-total-messages
1565 blurb "No following message"))
1567 (setq rmail-current-message n))))
1568 (let ((beg (rmail-msgbeg n))
1569 (end (rmail-msgend n)))
1572 (if (= (following-char) ?0)
1574 (rmail-reformat-message beg end)
1575 (rmail-set-attribute "unseen" nil))
1576 (search-forward "\n*** EOOH ***\n" end t)
1577 (narrow-to-region (point) end))
1578 (goto-char (point-min))
1579 (rmail-display-labels)
1580 (rmail-highlight-headers)
1581 (if transient-mark-mode (deactivate-mark))
1582 (run-hooks 'rmail-show-message-hook)
1583 ;; If there is a summary buffer, try to move to this message
1584 ;; in that buffer. But don't complain if this message
1585 ;; is not mentioned in the summary.
1586 ;; Don't do this at all if we were called on behalf
1587 ;; of cursor motion in the summary buffer.
1588 (and (rmail-summary-exists) (not no-summary)
1589 (let ((curr-msg rmail-current-message))
1590 (rmail-select-summary
1591 (rmail-summary-goto-msg curr-msg t t))))
1593 (message blurb))))))
1595 ;; Find all occurrences of certain fields, and highlight them.
1596 (defun rmail-highlight-headers ()
1597 ;; Do this only if the system supports faces.
1598 (if (and (fboundp 'internal-find-face)
1599 rmail-highlighted-headers)
1601 (search-forward "\n\n" nil 'move)
1603 (narrow-to-region (point-min) (point))
1604 (let ((case-fold-search t)
1605 (inhibit-read-only t)
1606 ;; Highlight with boldface if that is available.
1607 ;; Otherwise use the `highlight' face.
1608 (face (or rmail-highlight-face
1609 (if (face-differs-from-default-p 'bold)
1611 ;; List of overlays to reuse.
1612 (overlays rmail-overlay-list))
1613 (goto-char (point-min))
1614 (while (re-search-forward rmail-highlighted-headers nil t)
1615 (skip-chars-forward " \t")
1618 (while (progn (forward-line 1)
1619 (looking-at "[ \t]")))
1620 ;; Back up over newline, then trailing spaces or tabs
1622 (while (member (preceding-char) '(? ?\t))
1625 ;; Reuse an overlay we already have.
1627 (setq overlay (car overlays)
1628 overlays (cdr overlays))
1629 (overlay-put overlay 'face face)
1630 (move-overlay overlay beg (point)))
1631 ;; Make a new overlay and add it to
1632 ;; rmail-overlay-list.
1633 (setq overlay (make-overlay beg (point)))
1634 (overlay-put overlay 'face face)
1635 (setq rmail-overlay-list
1636 (cons overlay rmail-overlay-list))))))))))
1638 (defun rmail-next-message (n)
1639 "Show following message whether deleted or not.
1640 With prefix arg N, moves forward N messages, or backward if N is negative."
1642 (rmail-maybe-set-message-counters)
1643 (rmail-show-message (+ rmail-current-message n)))
1645 (defun rmail-previous-message (n)
1646 "Show previous message whether deleted or not.
1647 With prefix arg N, moves backward N messages, or forward if N is negative."
1649 (rmail-next-message (- n)))
1651 (defun rmail-next-undeleted-message (n)
1652 "Show following non-deleted message.
1653 With prefix arg N, moves forward N non-deleted messages,
1654 or backward if N is negative.
1656 Returns t if a new message is being shown, nil otherwise."
1658 (rmail-maybe-set-message-counters)
1659 (let ((lastwin rmail-current-message)
1660 (current rmail-current-message))
1661 (while (and (> n 0) (< current rmail-total-messages))
1662 (setq current (1+ current))
1663 (if (not (rmail-message-deleted-p current))
1664 (setq lastwin current n (1- n))))
1665 (while (and (< n 0) (> current 1))
1666 (setq current (1- current))
1667 (if (not (rmail-message-deleted-p current))
1668 (setq lastwin current n (1+ n))))
1669 (if (/= lastwin rmail-current-message)
1670 (progn (rmail-show-message lastwin)
1673 (message "No previous nondeleted message"))
1675 (message "No following nondeleted message"))
1678 (defun rmail-previous-undeleted-message (n)
1679 "Show previous non-deleted message.
1680 With prefix argument N, moves backward N non-deleted messages,
1681 or forward if N is negative."
1683 (rmail-next-undeleted-message (- n)))
1685 (defun rmail-first-message ()
1686 "Show first message in file."
1688 (rmail-maybe-set-message-counters)
1689 (rmail-show-message 1))
1691 (defun rmail-last-message ()
1692 "Show last message in file."
1694 (rmail-maybe-set-message-counters)
1695 (rmail-show-message rmail-total-messages))
1697 (defun rmail-what-message ()
1698 (let ((where (point))
1700 (high rmail-total-messages)
1701 (mid (/ rmail-total-messages 2)))
1702 (while (> (- high low) 1)
1703 (if (>= where (rmail-msgbeg mid))
1706 (setq mid (+ low (/ (- high low) 2))))
1707 (if (>= where (rmail-msgbeg high)) high low)))
1709 (defun rmail-message-recipients-p (msg recipients &optional primary-only)
1711 (goto-char (rmail-msgbeg msg))
1712 (search-forward "\n*** EOOH ***\n")
1713 (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
1714 (or (string-match recipients (or (mail-fetch-field "To") ""))
1715 (string-match recipients (or (mail-fetch-field "From") ""))
1716 (if (not primary-only)
1717 (string-match recipients (or (mail-fetch-field "Cc") ""))))))
1719 (defun rmail-message-regexp-p (msg regexp)
1720 "Return t, if for message number MSG, regexp REGEXP matches in the header."
1721 (goto-char (rmail-msgbeg msg))
1724 (search-forward "*** EOOH ***" (point-max)) (point))))
1725 (re-search-forward regexp end t)))
1727 (defvar rmail-search-last-regexp nil)
1728 (defun rmail-search (regexp &optional n)
1729 "Show message containing next match for REGEXP (but not the current msg).
1730 Prefix argument gives repeat count; negative argument means search
1731 backwards (through earlier messages).
1732 Interactively, empty argument means use same regexp used last time."
1734 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
1736 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
1738 (if rmail-search-last-regexp
1739 (setq prompt (concat prompt
1741 rmail-search-last-regexp
1743 (setq regexp (read-string prompt))
1744 (cond ((not (equal regexp ""))
1745 (setq rmail-search-last-regexp regexp))
1746 ((not rmail-search-last-regexp)
1747 (error "No previous Rmail search string")))
1748 (list rmail-search-last-regexp
1749 (prefix-numeric-value current-prefix-arg))))
1751 (message "%sRmail search for %s..."
1752 (if (< n 0) "Reverse " "")
1754 (rmail-maybe-set-message-counters)
1755 (let ((omin (point-min))
1760 (msg rmail-current-message))
1765 ;; Check messages one by one, advancing message number up or down
1766 ;; but searching forward through each message.
1768 (while (and (null win) (> msg 1))
1769 (goto-char (rmail-msgbeg (setq msg (1- msg))))
1770 (setq win (re-search-forward
1771 regexp (rmail-msgend msg) t)))
1772 (while (and (null win) (< msg rmail-total-messages))
1773 (goto-char (rmail-msgbeg (setq msg (1+ msg))))
1774 (setq win (re-search-forward regexp (rmail-msgend msg) t))))
1775 (setq n (+ n (if reversep 1 -1)))))
1778 ;; If this is a reverse search and we found a message,
1779 ;; search backward thru this message to position point.
1782 (goto-char (rmail-msgend msg))
1784 regexp (rmail-msgbeg msg) t)))
1786 (rmail-show-message msg)
1787 (message "%sRmail search for %s...done"
1788 (if reversep "Reverse " "")
1792 (narrow-to-region omin omax)
1794 (message "Search failed: %s" regexp)))))
1796 (defun rmail-search-backwards (regexp &optional n)
1797 "Show message containing previous match for REGEXP.
1798 Prefix argument gives repeat count; negative argument means search
1799 forward (through later messages).
1800 Interactively, empty argument means use same regexp used last time."
1802 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
1804 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
1806 (if rmail-search-last-regexp
1807 (setq prompt (concat prompt
1809 rmail-search-last-regexp
1811 (setq regexp (read-string prompt))
1812 (cond ((not (equal regexp ""))
1813 (setq rmail-search-last-regexp regexp))
1814 ((not rmail-search-last-regexp)
1815 (error "No previous Rmail search string")))
1816 (list rmail-search-last-regexp
1817 (prefix-numeric-value current-prefix-arg))))
1818 (rmail-search regexp (- (or n 1))))
1820 ;; Show the first message which has the `unseen' attribute.
1821 (defun rmail-first-unseen-message ()
1822 (rmail-maybe-set-message-counters)
1827 (while (and (not found) (<= current rmail-total-messages))
1828 (if (rmail-message-labels-p current ", ?\\(unseen\\),")
1829 (setq found current))
1830 (setq current (1+ current))))
1831 ;; Let the caller show the message.
1833 ;; (rmail-show-message found))
1836 (defun rmail-next-same-subject (n)
1837 "Go to the next mail message having the same subject header.
1838 With prefix argument N, do this N times.
1839 If N is negative, go backwards instead."
1841 (let* ((subject (mail-fetch-field "Subject"))
1842 (search-regexp (concat "^Subject: *\\(Re: *\\)?"
1843 (regexp-quote subject)
1846 (i rmail-current-message)
1848 (if (string-match "Re:[ \t]*" subject)
1849 (setq subject (substring subject (match-end 0))))
1853 (while (and (/= n 0)
1855 (< i rmail-total-messages)
1858 (while (and (not done)
1860 (< i rmail-total-messages)
1862 (setq i (if forward (1+ i) (1- i)))
1863 (goto-char (rmail-msgbeg i))
1864 (search-forward "\n*** EOOH ***\n")
1865 (let ((beg (point)) end)
1866 (search-forward "\n\n")
1869 (setq done (re-search-forward search-regexp end t))))
1870 (if done (setq found i)))
1871 (setq n (if forward (1- n) (1+ n))))))
1873 (rmail-show-message found)
1874 (error "No %s message with same subject"
1875 (if forward "following" "previous")))))
1877 (defun rmail-previous-same-subject (n)
1878 "Go to the previous mail message having the same subject header.
1879 With prefix argument N, do this N times.
1880 If N is negative, go forwards instead."
1882 (rmail-next-same-subject (- n)))
1884 ;;;; *** Rmail Message Deletion Commands ***
1886 (defun rmail-message-deleted-p (n)
1887 (= (aref rmail-deleted-vector n) ?D))
1889 (defun rmail-set-message-deleted-p (n state)
1890 (aset rmail-deleted-vector n (if state ?D ?\ )))
1892 (defun rmail-delete-message ()
1893 "Delete this message and stay on it."
1895 (rmail-set-attribute "deleted" t)
1896 (run-hooks 'rmail-delete-message-hook))
1898 (defun rmail-undelete-previous-message ()
1899 "Back up to deleted message, select it, and undelete it."
1901 (let ((msg rmail-current-message))
1902 (while (and (> msg 0)
1903 (not (rmail-message-deleted-p msg)))
1904 (setq msg (1- msg)))
1906 (error "No previous deleted message")
1907 (if (/= msg rmail-current-message)
1908 (rmail-show-message msg))
1909 (rmail-set-attribute "deleted" nil)
1910 (if (rmail-summary-exists)
1912 (set-buffer rmail-summary-buffer)
1913 (rmail-summary-mark-undeleted msg)))
1914 (rmail-maybe-display-summary))))
1916 (defun rmail-delete-forward (&optional backward)
1917 "Delete this message and move to next nondeleted one.
1918 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
1919 With prefix argument, delete and move backward.
1921 Returns t if a new message is displayed after the delete, or nil otherwise."
1923 (rmail-set-attribute "deleted" t)
1924 (run-hooks 'rmail-delete-message-hook)
1925 (let ((del-msg rmail-current-message))
1926 (if (rmail-summary-exists)
1927 (rmail-select-summary
1928 (rmail-summary-mark-deleted del-msg)))
1929 (prog1 (rmail-next-undeleted-message (if backward -1 1))
1930 (rmail-maybe-display-summary))))
1932 (defun rmail-delete-backward ()
1933 "Delete this message and move to previous nondeleted one.
1934 Deleted messages stay in the file until the \\[rmail-expunge] command is given."
1936 (rmail-delete-forward t))
1938 ;; Compute the message number a given message would have after expunging.
1939 ;; The present number of the message is OLDNUM.
1940 ;; DELETEDVEC should be rmail-deleted-vector.
1941 ;; The value is nil for a message that would be deleted.
1942 (defun rmail-msg-number-after-expunge (deletedvec oldnum)
1943 (if (or (null oldnum) (= (aref deletedvec oldnum) ?D))
1948 (if (/= (aref deletedvec i) ?D)
1949 (setq newnum (1+ newnum)))
1953 (defun rmail-only-expunge ()
1954 "Actually erase all deleted messages in the file."
1956 (message "Expunging deleted messages...")
1957 ;; Discard all undo records for this buffer.
1958 (or (eq buffer-undo-list t)
1959 (setq buffer-undo-list nil))
1960 (rmail-maybe-set-message-counters)
1961 (let* ((omax (- (buffer-size) (point-max)))
1962 (omin (- (buffer-size) (point-min)))
1963 (opoint (if (and (> rmail-current-message 0)
1964 (rmail-message-deleted-p rmail-current-message))
1966 (- (point) (point-min))))
1967 (messages-head (cons (aref rmail-message-vector 0) nil))
1968 (messages-tail messages-head)
1969 ;; Don't make any undo records for the expunging.
1970 (buffer-undo-list t)
1975 (goto-char (point-min))
1978 (total rmail-total-messages)
1979 (new-message-number rmail-current-message)
1981 (rmailbuf (current-buffer))
1982 (buffer-read-only nil)
1983 (messages rmail-message-vector)
1984 (deleted rmail-deleted-vector)
1985 (summary rmail-summary-vector))
1986 (setq rmail-total-messages nil
1987 rmail-current-message nil
1988 rmail-message-vector nil
1989 rmail-deleted-vector nil
1990 rmail-summary-vector nil)
1992 ;; Find each sendmail buffer that is set to reply
1993 ;; to a message in this buffer, and update its
1995 (let ((bufs (buffer-list)))
1998 (set-buffer (car bufs))
1999 (and (boundp 'rmail-send-actions-rmail-buffer)
2000 (eq rmail-send-actions-rmail-buffer rmailbuf)
2001 (setq rmail-send-actions-rmail-msg-number
2002 (rmail-msg-number-after-expunge
2004 rmail-send-actions-rmail-msg-number))))
2005 (setq bufs (cdr bufs))))
2007 (while (<= number total)
2008 (if (= (aref deleted number) ?D)
2011 (marker-position (aref messages number))
2012 (marker-position (aref messages (1+ number))))
2013 (move-marker (aref messages number) nil)
2014 (if (> new-message-number counter)
2015 (setq new-message-number (1- new-message-number))))
2016 (setq counter (1+ counter))
2018 (setcdr messages-tail
2019 (cons (aref messages number) nil)))
2021 (cons (if (= counter number) (aref summary (1- number)))
2023 (if (zerop (% (setq number (1+ number)) 20))
2024 (message "Expunging deleted messages...%d" number)))
2026 (setcdr messages-tail
2027 (cons (aref messages number) nil)))
2028 (setq rmail-current-message new-message-number
2029 rmail-total-messages counter
2030 rmail-message-vector (apply 'vector messages-head)
2031 rmail-deleted-vector (make-string (1+ counter) ?\ )
2032 rmail-summary-vector (vconcat (nreverse new-summary))
2034 (message "Expunging deleted messages...done")
2036 (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
2038 (if (zerop rmail-current-message) 1 nil))
2039 (forward-char opoint))))
2041 (defun rmail-expunge ()
2042 "Erase deleted messages from Rmail file and summary buffer."
2044 (rmail-only-expunge)
2045 (if (rmail-summary-exists)
2046 (rmail-select-summary
2047 (rmail-update-summary))))
2049 ;;;; *** Rmail Mailing Commands ***
2051 (defun rmail-start-mail (&rest args)
2052 (if (and window-system rmail-mail-new-frame)
2054 (apply 'mail-other-frame args)
2055 (modify-frame-parameters (selected-frame)
2056 '((dedicated . t))))
2057 (apply 'mail-other-window args)))
2059 (defun rmail-mail ()
2060 "Send mail in another window.
2061 While composing the message, use \\[mail-yank-original] to yank the
2062 original message into it."
2064 (rmail-start-mail nil nil nil nil nil (current-buffer)))
2066 (defun rmail-continue ()
2067 "Continue composing outgoing message previously being composed."
2069 (rmail-start-mail t))
2071 (defun rmail-reply (just-sender)
2072 "Reply to the current message.
2073 Normally include CC: to all other recipients of original message;
2074 prefix argument means ignore them. While composing the reply,
2075 use \\[mail-yank-original] to yank the original message into it."
2077 (let (from reply-to cc subject date to message-id resent-reply-to
2078 (msgnum rmail-current-message)
2079 (rmail-buffer (current-buffer)))
2083 (goto-char (rmail-msgbeg rmail-current-message))
2085 (if (= (following-char) ?0)
2087 (progn (forward-line 2)
2089 (progn (search-forward "\n\n" (rmail-msgend rmail-current-message)
2092 (narrow-to-region (point)
2093 (progn (search-forward "\n*** EOOH ***\n")
2094 (beginning-of-line) (point))))
2095 (setq resent-reply-to (mail-fetch-field "resent-reply-to" t)
2096 from (mail-fetch-field "from")
2097 reply-to (or resent-reply-to
2098 (mail-fetch-field "reply-to" nil t)
2100 cc (cond (just-sender nil)
2101 (resent-reply-to (mail-fetch-field "resent-cc" t))
2102 (t (mail-fetch-field "cc" nil t)))
2103 subject (or (and resent-reply-to
2104 (mail-fetch-field "resent-subject" t))
2105 (mail-fetch-field "subject"))
2106 date (or (and resent-reply-to
2107 (mail-fetch-field "resent-date" t))
2108 (mail-fetch-field "date"))
2109 to (cond (resent-reply-to
2110 (or (mail-fetch-field "resent-to" t)) "")
2111 ((mail-fetch-field "to" nil t))
2112 ;((mail-fetch-field "apparently-to")) ack gag barf
2114 message-id (cond (resent-reply-to
2115 (mail-fetch-field "resent-message-id" t))
2116 ((mail-fetch-field "message-id"))))))
2117 (and (stringp subject)
2119 (concat rmail-reply-prefix
2120 (if (string-match rmail-reply-regexp subject)
2121 (substring subject (match-end 0))
2123 (rmail-start-mail nil
2124 (mail-strip-quoted-names reply-to)
2126 (rmail-make-in-reply-to-field from date message-id)
2129 (let* ((cc-list (rmail-dont-reply-to
2130 (mail-strip-quoted-names
2131 (if (null cc) to (concat to ", " cc))))))
2132 (if (string= cc-list "") nil cc-list)))
2134 (list (list '(lambda ()
2135 (let ((msgnum rmail-send-actions-rmail-msg-number))
2137 (set-buffer rmail-send-actions-rmail-buffer)
2139 (rmail-set-attribute "answered" t msgnum))))))))
2140 ;; We keep the rmail buffer and message number in these
2141 ;; buffer-local vars in the sendmail buffer,
2142 ;; so that rmail-only-expunge can relocate the message number.
2143 (make-local-variable 'rmail-send-actions-rmail-buffer)
2144 (make-local-variable 'rmail-send-actions-rmail-msg-number)
2145 (setq rmail-send-actions-rmail-buffer rmail-buffer)
2146 (setq rmail-send-actions-rmail-msg-number msgnum)))
2148 (defun rmail-make-in-reply-to-field (from date message-id)
2155 (let ((tem (car (rfc822-addresses from))))
2158 (regexp-quote (if (string-match "@[^@]*\\'" tem)
2159 (substring tem 0 (match-beginning 0))
2162 ;; Message-ID is sufficiently informative
2164 (concat message-id " (" tem ")"))
2165 ;; Copy TEM, discarding text properties.
2166 (setq tem (copy-sequence tem))
2167 (set-text-properties 0 (length tem) nil tem)
2168 (setq tem (copy-sequence tem))
2169 ;; Use prin1 to fake RFC822 quoting
2170 (let ((field (prin1-to-string tem)))
2172 (concat field "'s message of " date)
2174 ((let* ((foo "[^][\000-\037\177-\377()<>@,;:\\\" ]+")
2175 (bar "[^][\000-\037\177-\377()<>@,;:\\\"]+"))
2176 ;; Can't use format because format loses on \000 (unix *^&%*^&%$!!)
2177 (or (string-match (concat "\\`[ \t]*\\(" bar
2178 "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
2179 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
2181 (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
2182 bar "\\))[ \t]*\\'")
2183 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
2185 (let ((start (match-beginning 1))
2186 (end (match-end 1)))
2187 ;; Trim whitespace which above regexp match allows
2188 (while (and (< start end)
2189 (memq (aref from start) '(?\t ?\ )))
2190 (setq start (1+ start)))
2191 (while (and (< start end)
2192 (memq (aref from (1- end)) '(?\t ?\ )))
2193 (setq end (1- end)))
2194 (let ((field (substring from start end)))
2195 (if date (setq field (concat "message from " field " on " date)))
2197 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
2198 (concat message-id " (" field ")")
2201 ;; If we can't kludge it simply, do it correctly
2202 (let ((mail-use-rfc822 t))
2203 (rmail-make-in-reply-to-field from date message-id)))))
2205 (defun rmail-forward (resend)
2206 "Forward the current message to another user.
2207 With prefix argument, \"resend\" the message instead of forwarding it;
2208 see the documentation of `rmail-resend'."
2211 (call-interactively 'rmail-resend)
2212 (let ((forward-buffer (current-buffer))
2213 (msgnum rmail-current-message)
2214 (subject (concat "["
2215 (let ((from (or (mail-fetch-field "From")
2216 (mail-fetch-field ">From"))))
2218 (concat (mail-strip-quoted-names from) ": ")
2220 (or (mail-fetch-field "Subject") "")
2222 ;; If only one window, use it for the mail buffer.
2223 ;; Otherwise, use another window for the mail buffer
2224 ;; so that the Rmail buffer remains visible
2225 ;; and sending the mail will get back to it.
2226 (if (funcall (if (and (not rmail-mail-new-frame) (one-window-p t))
2228 (function rmail-start-mail))
2229 nil nil subject nil nil nil
2230 (list (list (function
2233 rmail-send-actions-rmail-msg-number))
2235 (set-buffer rmail-send-actions-rmail-buffer)
2237 (rmail-set-attribute
2238 "forwarded" t msgnum)))))))))
2239 ;; The mail buffer is now current.
2241 ;; We keep the rmail buffer and message number in these
2242 ;; buffer-local vars in the sendmail buffer,
2243 ;; so that rmail-only-expunge can relocate the message number.
2244 (make-local-variable 'rmail-send-actions-rmail-buffer)
2245 (make-local-variable 'rmail-send-actions-rmail-msg-number)
2246 (setq rmail-send-actions-rmail-buffer forward-buffer)
2247 (setq rmail-send-actions-rmail-msg-number msgnum)
2248 ;; Insert after header separator--before signature if any.
2249 (goto-char (point-min))
2250 (search-forward-regexp
2251 (concat "^" (regexp-quote mail-header-separator) "$"))
2253 (insert "------- Start of forwarded message -------\n")
2254 (insert-buffer-substring forward-buffer)
2255 (let ((end (point)))
2256 (skip-chars-backward "\n")
2259 (delete-region (point) end))
2260 (insert "------- End of forwarded message -------\n")
2263 (defun rmail-resend (address &optional from comment mail-alias-file)
2264 "Resend current message to ADDRESSES.
2265 ADDRESSES should be a single address, a string consisting of several
2266 addresses separated by commas, or a list of addresses.
2268 Optional FROM is the address to resend the message from, and
2269 defaults to the username of the person redistributing the message.
2270 Optional COMMENT is a string that will be inserted as a comment in the
2272 Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
2273 typically for purposes of moderating a list."
2274 (interactive "sResend to: ")
2276 (require 'mailalias)
2277 (if (not from) (setq from (user-login-name)))
2278 (let ((tembuf (generate-new-buffer " sendmail temp"))
2279 (mail-header-separator "")
2280 (case-fold-search nil)
2281 (mailbuf (current-buffer)))
2284 ;;>> Copy message into temp buffer
2286 (insert-buffer-substring mailbuf)
2287 (goto-char (point-min))
2288 ;; Delete any Sender field, since that's not specifyable.
2289 ; Only delete Sender fields in the actual header.
2290 (re-search-forward "^$" nil 'move)
2291 ; Using "while" here rather than "if" because some buggy mail
2292 ; software may have inserted multiple Sender fields.
2293 (while (re-search-backward "^Sender:" nil t)
2297 (while (looking-at "[ \t]")
2299 (delete-region beg (point))))
2300 ; Go back to the beginning of the buffer so the Resent- fields
2301 ; are inserted there.
2302 (goto-char (point-min))
2303 ;;>> Insert resent-from:
2304 (insert "Resent-From: " from "\n")
2305 (insert "Resent-Date: " (mail-rfc822-date) "\n")
2306 ;;>> Insert resent-to: and bcc if need be.
2307 (let ((before (point)))
2309 (insert "Resent-Bcc: " (user-login-name) "\n"))
2310 (insert "Resent-To: " (if (stringp address)
2312 (mapconcat 'identity address ",\n\t"))
2314 ;; Expand abbrevs in the recipients.
2316 (if (featurep 'mailabbrev)
2317 (let ((end (point-marker))
2318 (local-abbrev-table mail-abbrevs)
2319 (old-syntax-table (syntax-table)))
2320 (if (and (not (vectorp mail-abbrevs))
2321 (file-exists-p mail-personal-alias-file))
2322 (build-mail-abbrevs))
2323 (set-syntax-table mail-abbrev-syntax-table)
2325 (while (and (< (point) end)
2326 (progn (forward-word 1)
2329 (set-syntax-table old-syntax-table))
2330 (expand-mail-aliases before (point)))))
2331 ;;>> Set up comment, if any.
2332 (if (and (sequencep comment) (not (zerop (length comment))))
2333 (let ((before (point))
2336 (or (eolp) (insert "\n"))
2337 (setq after (point))
2339 (while (< (point) after)
2340 (insert "Resent-Comment: ")
2342 ;; Don't expand aliases in the destination fields
2343 ;; of the original message.
2345 (funcall send-mail-function)))
2346 (kill-buffer tembuf))
2347 (rmail-set-attribute "resent" t rmail-current-message)))
2349 (defvar mail-unsent-separator
2350 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
2351 "^ *---+ +Returned message +---+ *$\\|"
2352 "^Start of returned message$\\|"
2353 "^ *---+ +Original message +---+ *$\\|"
2354 "^ *--+ +begin message +--+ *$\\|"
2355 "^ *---+ +Original message follows +---+ *$\\|"
2356 "^|? *---+ +Message text follows: +---+ *|?$")
2357 "A regexp that matches the separator before the text of a failed message.")
2359 (defun rmail-retry-failure ()
2360 "Edit a mail message which is based on the contents of the current message.
2361 For a message rejected by the mail system, extract the interesting headers and
2362 the body of the original message.
2363 The variable `mail-unsent-separator' should match the string that
2364 delimits the returned original message.
2365 The variable `rmail-retry-ignored-headers' is a regular expression
2366 specifying headers which should not be copied into the new message."
2368 (require 'mail-utils)
2369 (let ((rmail-buffer (current-buffer))
2370 (msgnum rmail-current-message)
2371 bounce-start bounce-end bounce-indent resending)
2373 ;; Narrow down to just the quoted original message
2374 (rmail-beginning-of-message)
2375 (let ((case-fold-search t))
2376 (if (search-forward "This is a MIME-encapsulated message\n\n--" nil t)
2378 (buffer-substring (progn (beginning-of-line) (point))
2379 (progn (end-of-line) (point)))))
2380 (re-search-forward mail-unsent-separator)
2381 (search-forward codestring)
2382 (or (search-forward "\n\n" nil t)
2383 (error "Cannot find end of Mime data in failed message"))
2384 (setq bounce-start (point))
2386 (goto-char (point-max))
2387 (search-backward codestring)
2388 (setq bounce-end (point)))
2389 (or (search-forward "\n\n" nil t)
2390 (error "Cannot find end of header in failed message")))
2391 (or (re-search-forward mail-unsent-separator nil t)
2392 (error "Cannot parse this as a failure message"))
2393 (skip-chars-forward "\n")
2394 ;; Support a style of failure message in which the original
2395 ;; message is indented, and included within lines saying
2396 ;; `Start of returned message' and `End of returned message'.
2397 (if (looking-at " +Received:")
2399 (setq bounce-start (point))
2400 (skip-chars-forward " ")
2401 (setq bounce-indent (- (current-column)))
2402 (goto-char (point-max))
2403 (re-search-backward "^End of returned message$" nil t)
2404 (setq bounce-end (point)))
2405 ;; One message contained a few random lines before the old
2406 ;; message header. The first line of the message started with
2407 ;; two hyphens. A blank line followed these random lines.
2408 ;; The same line beginning with two hyphens was possibly
2409 ;; marking the end of the message.
2410 (if (looking-at "^--")
2411 (let ((boundary (buffer-substring-no-properties
2413 (progn (end-of-line) (point)))))
2414 (search-forward "\n\n")
2415 (skip-chars-forward "\n")
2416 (setq bounce-start (point))
2417 (goto-char (point-max))
2418 (search-backward (concat "\n\n" boundary) bounce-start t)
2419 (setq bounce-end (point)))
2420 (setq bounce-start (point)
2421 bounce-end (point-max)))
2422 (or (search-forward "\n\n" nil t)
2423 (error "Cannot find end of header in failed message"))
2425 ;; Start sending a new message; default header fields from the original.
2426 ;; Turn off the usual actions for initializing the message body
2427 ;; because we want to get only the text from the failure message.
2429 ;; This function will be called when the user sends the retry.
2430 ;; It will mark the bounce message as "retried".
2431 (function (lambda ()
2432 (let ((msgnum rmail-send-actions-rmail-msg-number))
2434 (set-buffer rmail-send-actions-rmail-buffer)
2436 (rmail-set-attribute "retried" t msgnum)))))))
2437 mail-signature mail-setup-hook)
2438 (if (rmail-start-mail nil nil nil nil nil rmail-buffer
2439 (list (list action)))
2440 ;; Insert original text as initial text of new draft message.
2442 ;; We keep the rmail buffer and message number in these
2443 ;; buffer-local vars in the sendmail buffer,
2444 ;; so that the rmail-only-expunge can relocate the message number.
2445 (make-local-variable 'rmail-send-actions-rmail-buffer)
2446 (make-local-variable 'rmail-send-actions-rmail-msg-number)
2447 (setq rmail-send-actions-rmail-buffer rmail-buffer)
2448 (setq rmail-send-actions-rmail-msg-number msgnum)
2450 (insert-buffer-substring rmail-buffer bounce-start bounce-end)
2451 (goto-char (point-min))
2453 (indent-rigidly (point-min) (point-max) bounce-indent))
2454 (rmail-clear-headers rmail-retry-ignored-headers)
2455 (rmail-clear-headers "^sender:\\|^from:\\|^return-path:")
2456 (goto-char (point-min))
2458 (search-forward "\n\n")
2460 (narrow-to-region (point-min) (point))
2461 (setq resending (mail-fetch-field "resent-to"))
2464 (insert "Resent-Bcc: " (user-login-name) "\n")
2465 (insert "BCC: " (user-login-name) "\n"))))
2466 (insert mail-header-separator)
2467 (mail-position-on-field (if resending "Resent-To" "To") t)
2468 (set-buffer rmail-buffer)
2469 (rmail-beginning-of-message))))))
2471 (defun rmail-summary-exists ()
2472 "Non-nil iff in an RMAIL buffer and an associated summary buffer exists.
2473 In fact, the non-nil value returned is the summary buffer itself."
2474 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
2475 rmail-summary-buffer))
2477 (defun rmail-summary-displayed ()
2478 "t iff in RMAIL buffer and an associated summary buffer is displayed."
2479 (and rmail-summary-buffer (get-buffer-window rmail-summary-buffer)))
2481 (defvar rmail-redisplay-summary nil
2482 "*Non-nil means Rmail should show the summary when it changes.
2483 This has an effect only if a summary buffer exists.")
2485 (defvar rmail-summary-window-size nil
2486 "*Non-nil means specify the height for an Rmail summary window.")
2488 ;; Put the summary buffer back on the screen, if user wants that.
2489 (defun rmail-maybe-display-summary ()
2490 (let ((selected (selected-window))
2492 ;; If requested, make sure the summary is displayed.
2493 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
2494 rmail-redisplay-summary
2495 (if (get-buffer-window rmail-summary-buffer 0)
2496 ;; It's already in some frame; show that one.
2497 (let ((frame (window-frame
2498 (get-buffer-window rmail-summary-buffer 0))))
2499 (make-frame-visible frame)
2500 (raise-frame frame))
2501 (display-buffer rmail-summary-buffer)))
2502 ;; If requested, set the height of the summary window.
2503 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
2504 rmail-summary-window-size
2505 (setq window (get-buffer-window rmail-summary-buffer))
2506 ;; Don't try to change the size if just one window in frame.
2507 (not (eq window (frame-root-window (window-frame window))))
2510 (select-window window)
2511 (enlarge-window (- rmail-summary-window-size
2513 (select-window selected)))))
2515 ;;;; *** Rmail Specify Inbox Files ***
2517 (autoload 'set-rmail-inbox-list "rmailmsc"
2518 "Set the inbox list of the current RMAIL file to FILE-NAME.
2519 This may be a list of file names separated by commas.
2520 If FILE-NAME is empty, remove any inbox list."
2523 ;;;; *** Rmail Commands for Labels ***
2525 (autoload 'rmail-add-label "rmailkwd"
2526 "Add LABEL to labels associated with current RMAIL message.
2527 Completion is performed over known labels when reading."
2530 (autoload 'rmail-kill-label "rmailkwd"
2531 "Remove LABEL from labels associated with current RMAIL message.
2532 Completion is performed over known labels when reading."
2535 (autoload 'rmail-next-labeled-message "rmailkwd"
2536 "Show next message with LABEL. Defaults to last label used.
2537 With prefix argument N moves forward N messages with this label."
2540 (autoload 'rmail-previous-labeled-message "rmailkwd"
2541 "Show previous message with LABEL. Defaults to last label used.
2542 With prefix argument N moves backward N messages with this label."
2545 (autoload 'rmail-read-label "rmailkwd"
2546 "PROMPT and read with completion an Rmail message label."
2549 ;;;; *** Rmail Edit Mode ***
2551 (autoload 'rmail-edit-current-message "rmailedit"
2552 "Edit the contents of the current message"
2555 ;;;; *** Rmail Sorting ***
2557 (autoload 'rmail-sort-by-date "rmailsort"
2558 "Sort messages of current Rmail file by date.
2559 If prefix argument REVERSE is non-nil, sort them in reverse order." t)
2561 (autoload 'rmail-sort-by-subject "rmailsort"
2562 "Sort messages of current Rmail file by subject.
2563 If prefix argument REVERSE is non-nil, sort them in reverse order." t)
2565 (autoload 'rmail-sort-by-author "rmailsort"
2566 "Sort messages of current Rmail file by author.
2567 If prefix argument REVERSE is non-nil, sort them in reverse order." t)
2569 (autoload 'rmail-sort-by-recipient "rmailsort"
2570 "Sort messages of current Rmail file by recipient.
2571 If prefix argument REVERSE is non-nil, sort them in reverse order." t)
2573 (autoload 'rmail-sort-by-correspondent "rmailsort"
2574 "Sort messages of current Rmail file by other correspondent.
2575 If prefix argument REVERSE is non-nil, sort them in reverse order." t)
2577 (autoload 'rmail-sort-by-lines "rmailsort"
2578 "Sort messages of current Rmail file by number of lines.
2579 If prefix argument REVERSE is non-nil, sort them in reverse order." t)
2581 (autoload 'rmail-sort-by-keywords "rmailsort"
2582 "Sort messages of current Rmail file by labels.
2583 If prefix argument REVERSE is non-nil, sort them in reverse order.
2584 KEYWORDS is a comma-separated list of labels." t)
2586 ;;;; *** Rmail Summary Mode ***
2588 (autoload 'rmail-summary "rmailsum"
2589 "Display a summary of all messages, one line per message."
2592 (autoload 'rmail-summary-by-labels "rmailsum"
2593 "Display a summary of all messages with one or more LABELS.
2594 LABELS should be a string containing the desired labels, separated by commas."
2597 (autoload 'rmail-summary-by-recipients "rmailsum"
2598 "Display a summary of all messages with the given RECIPIENTS.
2599 Normally checks the To, From and Cc fields of headers; but if PRIMARY-ONLY
2600 is non-nil (prefix arg given), only look in the To and From fields.
2601 RECIPIENTS is a string of regexps separated by commas."
2604 (autoload 'rmail-summary-by-regexp "rmailsum"
2605 "Display a summary of all messages according to regexp REGEXP.
2606 If the regular expression is found in the header of the message
2607 \(including in the date and other lines, as well as the subject line),
2608 Emacs will list the header line in the RMAIL-summary."
2611 (autoload 'rmail-summary-by-topic "rmailsum"
2612 "Display a summary of all messages with the given SUBJECT.
2613 Normally checks the Subject field of headers;
2614 but if WHOLE-MESSAGE is non-nil (prefix arg given),
2615 look in the whole message.
2616 SUBJECT is a string of regexps separated by commas."
2619 (autoload 'rmail-summary-by-sender "rmailsum"
2620 "Display a summary of all messages with the given SENDERS.
2621 SENDERS is a string of names separated by commas."
2624 ;;;; *** Rmail output messages to files ***
2626 (autoload 'rmail-output-to-rmail-file "rmailout"
2627 "Append the current message to an Rmail file named FILE-NAME.
2628 If the file does not exist, ask if it should be created.
2629 If file is being visited, the message is appended to the Emacs
2630 buffer visiting that file."
2633 (autoload 'rmail-output "rmailout"
2634 "Append this message to Unix mail file named FILE-NAME."
2637 ;;;; *** Rmail undigestification ***
2639 (autoload 'undigestify-rmail-message "undigest"
2640 "Break up a digest message into its constituent messages.
2641 Leaves original message, deleted, before the undigestified messages."
2646 ;;; rmail.el ends here