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