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