rmail.el 111 KB
Newer Older
Eric S. Raymond's avatar
Eric S. Raymond committed
1 2
;;; 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.
Eric S. Raymond's avatar
Eric S. Raymond committed
4

Eric S. Raymond's avatar
Eric S. Raymond committed
5
;; Maintainer: FSF
Eric S. Raymond's avatar
Eric S. Raymond committed
6
;; Keywords: mail
Eric S. Raymond's avatar
Eric S. Raymond committed
7

Roland McGrath's avatar
Roland McGrath committed
8 9 10 11
;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
Eric S. Raymond's avatar
Eric S. Raymond committed
12
;; the Free Software Foundation; either version 2, or (at your option)
Roland McGrath's avatar
Roland McGrath committed
13 14 15 16 17 18 19 20
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
Erik Naggum's avatar
Erik Naggum committed
21 22 23
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
Roland McGrath's avatar
Roland McGrath committed
24

Eric S. Raymond's avatar
Eric S. Raymond committed
25
;;; Code:
Roland McGrath's avatar
Roland McGrath committed
26 27 28 29 30 31

;; Souped up by shane@mit-ajax based on ideas of rlk@athena.mit.edu
;;   New features include attribute and keyword support, message
;;   selection by dispatch table, summary by attributes and keywords,
;;   expunging by dispatch table, sticky options for file commands.

32 33 34 35 36 37 38
;; Extended by Bob Weiner of Motorola
;;   New features include: rmail and rmail-summary buffers remain
;;   synchronized and key bindings basically operate the same way in both
;;   buffers, summary by topic or by regular expression, rmail-reply-prefix
;;   variable, and a bury rmail buffer (wipe) command.
;;

Roland McGrath's avatar
Roland McGrath committed
39
(require 'mail-utils)
40 41 42 43

;; For Emacs V18 compatibility
(and (not (fboundp 'buffer-disable-undo))
     (fboundp 'buffer-flush-undo)
44
     (defalias 'buffer-disable-undo 'buffer-flush-undo))
45 46

; These variables now declared in paths.el.
Roland McGrath's avatar
Roland McGrath committed
47 48
;(defvar rmail-spool-directory "/usr/spool/mail/"
;  "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.")
Roland McGrath's avatar
Roland McGrath committed
50 51 52 53
;(defvar rmail-file-name
;  (expand-file-name "~/RMAIL")
;  "")

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
(defgroup rmail nil
  "Mail reader for Emacs."
  :group 'mail)

(defgroup rmail-retrieve nil
  "Rmail retrieval options."
  :prefix "rmail-"
  :group 'rmail)

(defgroup rmail-files nil
  "Rmail files."
  :prefix "rmail-"
  :group 'rmail)

(defgroup rmail-headers nil
  "Rmail header options."
  :prefix "rmail-"
  :group 'rmail)

(defgroup rmail-reply nil
  "Rmail reply options."
  :prefix "rmail-"
  :group 'rmail)

(defgroup rmail-summary nil
  "Rmail summary options."
  :prefix "rmail-"
  :prefix "rmail-summary-"
  :group 'rmail)

(defgroup rmail-output nil
  "Output message to a file."
  :prefix "rmail-output-"
  :prefix "rmail-"
  :group 'rmail)


91 92 93
(defvar rmail-movemail-program nil
  "If non-nil, name of program for fetching new mail.")

94 95 96 97 98
(defcustom rmail-pop-password nil
  "*Password to use when reading mail from a POP server, if required."
  :type '(choice (string :tag "Password")
		 (const :tag "Not Required" nil))
  :group 'rmail-retrieve)
99

100 101 102 103
(defcustom rmail-pop-password-required nil
  "*Non-nil if a password is required when reading mail using POP."
  :type 'boolean
  :group 'rmail-retrieve)
104

105 106 107 108 109
(defvar rmail-pop-password-error "invalid usercode or password"
  "Regular expression matching incorrect-password POP server error messages.
If you get an incorrect-password error that this expression does not match,
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 113 114
rather than deleted, after it is retrieved."
  :type 'boolean
  :group 'rmail-retrieve)
115

Roland McGrath's avatar
Roland McGrath committed
116
;;;###autoload
117
(defcustom rmail-dont-reply-to-names nil "\
Roland McGrath's avatar
Roland McGrath committed
118
*A regexp specifying names to prune of reply to messages.
119 120 121
A value of nil means exclude your own name only."
  :type '(choice regexp (const :tag "Your Name" nil))
  :group 'rmail-reply)
Roland McGrath's avatar
Roland McGrath committed
122 123 124 125 126 127 128

;;;###autoload
(defvar rmail-default-dont-reply-to-names "info-" "\
A regular expression specifying part of the value of the default value of
the variable `rmail-dont-reply-to-names', for when the user does not set
`rmail-dont-reply-to-names' explicitly.  (The other part of the default
value is the user's name.)
Richard M. Stallman's avatar
Richard M. Stallman committed
129
It is useful to set this variable in the site customization file.")
Roland McGrath's avatar
Roland McGrath committed
130 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 134 135
  "*Regexp to match header fields that Rmail should normally hide."
  :type 'regexp
  :group 'rmail-headers)
Roland McGrath's avatar
Roland McGrath committed
136

137
;;;###autoload
138
(defcustom rmail-displayed-headers nil
139 140
  "*Regexp to match Header fields that Rmail should display.
If nil, display all header fields except those matched by
141 142 143
`rmail-ignored-headers'."
  :type '(choice regexp (const :tag "All"))
  :group 'rmail-headers)
144

145
;;;###autoload
146 147 148 149
(defcustom rmail-retry-ignored-headers nil "\
*Headers that should be stripped when retrying a failed message."
  :type '(choice regexp (const nil :tag "None"))
  :group 'rmail-headers)
150

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 156 157
See also `rmail-highlight-face'."
  :type 'regexp
  :group 'rmail-headers)
158

159
;;;###autoload
160 161 162 163 164
(defcustom rmail-highlight-face nil "\
*Face used by Rmail for highlighting headers."
  :type '(choice (const :tag "Default" nil)
		 face)
  :group 'rmail-headers)
165

Roland McGrath's avatar
Roland McGrath committed
166
;;;###autoload
167 168 169 170
(defcustom rmail-delete-after-output nil "\
*Non-nil means automatically delete a message that is copied to a file."
  :type 'boolean
  :group 'rmail-files)
Roland McGrath's avatar
Roland McGrath committed
171 172

;;;###autoload
173
(defcustom rmail-primary-inbox-list nil "\
174 175 176
*List of files which are inboxes for user's primary mail file `~/RMAIL'.
`nil' means the default, which is (\"/usr/spool/mail/$USER\")
\(the name varies depending on the operating system,
177
and the value of the environment variable MAIL overrides it)."
178 179 180
  ;; Don't use backquote here, because we don't want to need it
  ;; at load time.
  :type (list 'choice '(const :tag "Default" nil)
Richard M. Stallman's avatar
Richard M. Stallman committed
181 182 183
	      (list 'repeat ':value (or (getenv "MAIL")
					(concat "/var/spool/mail/"
						(getenv "USER")))
184
		    'file))
185 186
  :group 'rmail-retrieve
  :group 'rmail-files)
Roland McGrath's avatar
Roland McGrath committed
187

188
;;;###autoload
189 190 191 192
(defcustom rmail-mail-new-frame nil
  "*Non-nil means Rmail makes a new frame for composing outgoing mail."
  :type 'boolean
  :group 'rmail-reply)
193

194
;;;###autoload
195 196 197 198
(defcustom rmail-secondary-file-directory "~/"
  "*Directory for additional secondary Rmail files."
  :type 'directory
  :group 'rmail-files)
199
;;;###autoload
200 201 202 203
(defcustom rmail-secondary-file-regexp "\\.xmail$"
  "*Regexp for which files are secondary Rmail files."
  :type 'regexp
  :group 'rmail-files)
204

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
;;;###autoload
(defvar rmail-mode-hook nil
  "List of functions to call when Rmail is invoked.")

;;;###autoload
(defvar rmail-get-new-mail-hook nil
  "List of functions to call when Rmail has retrieved new mail.")

;;;###autoload
(defvar rmail-show-message-hook nil
  "List of functions to call when Rmail displays a message.")

;;;###autoload
(defvar rmail-delete-message-hook nil
  "List of functions to call when Rmail deletes a message.
When the hooks are called, the message has been marked deleted but is
still the current message in the Rmail buffer.")

223 224 225
;; These may be altered by site-init.el to match the format of mmdf files
;;  delimiting used on a given host (delim1 and delim2 from the config
;;  files).
Roland McGrath's avatar
Roland McGrath committed
226

227
(defvar rmail-mmdf-delim1 "^\001\001\001\001\n"
Roland McGrath's avatar
Roland McGrath committed
228
  "Regexp marking the start of an mmdf message")
229
(defvar rmail-mmdf-delim2 "^\001\001\001\001\n"
Roland McGrath's avatar
Roland McGrath committed
230 231 232
  "Regexp marking the end of an mmdf message")

(defvar rmail-message-filter nil
233 234 235
  "If non-nil, a filter function for new messages in RMAIL.
Called with region narrowed to the message, including headers,
before obeying `rmail-ignored-headers'.")
Roland McGrath's avatar
Roland McGrath committed
236

237 238 239
(defvar rmail-reply-prefix "Re: "
  "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]:".
Karl Heuer's avatar
Karl Heuer committed
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'.")
Karl Heuer's avatar
Karl Heuer committed
244

245
(defvar rmail-display-summary nil
246
  "If non-nil, Rmail always displays the summary buffer.")
247

Roland McGrath's avatar
Roland McGrath committed
248 249 250 251 252
(defvar rmail-mode-map nil)

(defvar rmail-inbox-list nil)
(defvar rmail-keywords nil)

253 254 255 256 257 258
(defvar rmail-buffer nil
  "The RMAIL buffer related to the current buffer.
In an RMAIL buffer, this holds the RMAIL buffer itself.
In a summary buffer, this holds the RMAIL buffer it is a summary for.")
(put 'rmail-buffer 'permanent-local t)

Roland McGrath's avatar
Roland McGrath committed
259 260 261 262 263 264
;; Message counters and markers.  Deleted flags.

(defvar rmail-current-message nil)
(defvar rmail-total-messages nil)
(defvar rmail-message-vector nil)
(defvar rmail-deleted-vector nil)
265 266 267 268
(defvar rmail-msgref-vector nil
  "In an Rmail buffer, a vector whose Nth element is a list (N).
When expunging renumbers messages, these lists are modified
by substituting the new message number into the existing list.")
Roland McGrath's avatar
Roland McGrath committed
269

270 271
(defvar rmail-overlay-list nil)

272
(defvar rmail-font-lock-keywords
273
  (eval-when-compile
274 275 276 277 278 279
    (let* ((cite-chars "[>|}]")
	   (cite-prefix "A-Za-z")
	   (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
      (list '("^\\(From\\|Sender\\):" . font-lock-function-name-face)
	    '("^Reply-To:.*$" . font-lock-function-name-face)
	    '("^Subject:" . font-lock-comment-face)
Simon Marshall's avatar
Simon Marshall committed
280 281
	    '("^\\(To\\|Apparently-To\\|Cc\\|Newsgroups\\):"
	      . font-lock-keyword-face)
282 283 284
	    ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
	    `(,cite-chars
	      (,(concat "\\=[ \t]*"
Simon Marshall's avatar
Simon Marshall committed
285 286 287
			"\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
			"\\(" cite-chars "[ \t]*\\)\\)+"
			"\\(.*\\)")
288
	       (beginning-of-line) (end-of-line)
Simon Marshall's avatar
Simon Marshall committed
289 290
	       (2 font-lock-reference-face nil t)
	       (4 font-lock-comment-face nil t)))
291 292
	    '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\|Date\\):.*$"
	      . font-lock-string-face))))
293 294
  "Additional expressions to highlight in Rmail mode.")

Roland McGrath's avatar
Roland McGrath committed
295 296 297
;; These are used by autoloaded rmail-summary.

(defvar rmail-summary-buffer nil)
298
(put 'rmail-summary-buffer 'permanent-local t)
Roland McGrath's avatar
Roland McGrath committed
299
(defvar rmail-summary-vector nil)
300
(put 'rmail-summary-vector 'permanent-local t)
Roland McGrath's avatar
Roland McGrath committed
301 302 303 304 305

;; `Sticky' default variables.

;; Last individual label specified to a or k.
(defvar rmail-last-label nil)
306
;; Last set of values specified to C-M-n, C-M-p, C-M-s or C-M-l.
Roland McGrath's avatar
Roland McGrath committed
307
(defvar rmail-last-multi-labels nil)
308
(defvar rmail-last-regexp nil)
309 310 311 312 313 314 315 316
(defcustom rmail-default-file "~/xmail"
  "*Default file name for \\[rmail-output]."
  :type 'file
  :group 'rmail-files)
(defcustom rmail-default-rmail-file "~/XMAIL"
  "*Default file name for \\[rmail-output-to-rmail-file]."
  :type 'file
  :group 'rmail-files)
Jim Blandy's avatar
Jim Blandy committed
317

Jim Blandy's avatar
Jim Blandy committed
318 319 320 321
;;; Regexp matching the delimiter of messages in UNIX mail format
;;; (UNIX From lines), minus the initial ^.  Note that if you change
;;; this expression, you must change the code in rmail-nuke-pinhead-header
;;; that knows the exact ordering of the \\( \\) subexpressions.
Jim Blandy's avatar
Jim Blandy committed
322
(defvar rmail-unix-mail-delimiter
Jim Blandy's avatar
Jim Blandy committed
323
  (let ((time-zone-regexp
324
	 (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
Jim Blandy's avatar
Jim Blandy committed
325 326 327 328 329 330
		 "\\|[-+]?[0-9][0-9][0-9][0-9]"
		 "\\|"
		 "\\) *")))
    (concat
     "From "

331 332 333 334 335 336 337 338 339 340 341
     ;; Many things can happen to an RFC 822 mailbox before it is put into
     ;; a `From' line.  The leading phrase can be stripped, e.g.
     ;; `Joe <@w.x:joe@y.z>' -> `<@w.x:joe@y.z>'.  The <> can be stripped, e.g.
     ;; `<@x.y:joe@y.z>' -> `@x.y:joe@y.z'.  Everything starting with a CRLF
     ;; can be removed, e.g.
     ;;		From: joe@y.z (Joe	K
     ;;			User)
     ;; can yield `From joe@y.z (Joe 	K Fri Mar 22 08:11:15 1996', and
     ;;		From: Joe User
     ;;			<joe@y.z>
     ;; can yield `From Joe User Fri Mar 22 08:11:15 1996'.
342 343 344 345 346
     ;; The mailbox can be removed or be replaced by white space, e.g.
     ;;		From: "Joe User"{space}{tab}
     ;;			<joe@y.z>
     ;; can yield `From {space}{tab} Fri Mar 22 08:11:15 1996',
     ;; where {space} and {tab} represent the Ascii space and tab characters.
347 348 349
     ;; We want to match the results of any of these manglings.
     ;; The following regexp rejects names whose first characters are
     ;; obviously bogus, but after that anything goes.
350
     "\\([^\0-\b\n-\r\^?].*\\)? "
Jim Blandy's avatar
Jim Blandy committed
351 352

     ;; The time the message was sent.
353 354 355 356
     "\\([^\0-\r \^?]+\\) +"				; day of the week
     "\\([^\0-\r \^?]+\\) +"				; month
     "\\([0-3]?[0-9]\\) +"				; day of month
     "\\([0-2][0-9]:[0-5][0-9]\\(:[0-6][0-9]\\)?\\) *"	; time of day
Jim Blandy's avatar
Jim Blandy committed
357 358 359 360 361 362

     ;; Perhaps a time zone, specified by an abbreviation, or by a
     ;; numeric offset.
     time-zone-regexp

     ;; The year.
363
     " \\([0-9][0-9]+\\) *"
Jim Blandy's avatar
Jim Blandy committed
364 365 366 367

     ;; On some systems the time zone can appear after the year, too.
     time-zone-regexp

Karl Heuer's avatar
Karl Heuer committed
368 369
     ;; Old uucp cruft.
     "\\(remote from .*\\)?"
Jim Blandy's avatar
Jim Blandy committed
370 371 372 373

     "\n"))
  nil)

374 375 376
;; Perform BODY in the summary buffer
;; in such a way that its cursor is properly updated in its own window.
(defmacro rmail-select-summary (&rest body)
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
  (` (let ((total rmail-total-messages))
       (if (rmail-summary-displayed)
	   (let ((window (selected-window)))
	     (save-excursion
	       (unwind-protect
		   (progn
		     (pop-to-buffer rmail-summary-buffer)
		     ;; rmail-total-messages is a buffer-local var
		     ;; in the rmail buffer.
		     ;; This way we make it available for the body
		     ;; even tho the rmail buffer is not current.
		     (let ((rmail-total-messages total))
		       (,@ body)))
		 (select-window window))))
	 (save-excursion
	   (set-buffer rmail-summary-buffer)
	   (let ((rmail-total-messages total))
	     (,@ body))))
       (rmail-maybe-display-summary))))
396 397

(defvar rmail-view-buffer nil
398
  "Buffer which holds RMAIL message for MIME displaying.")
399 400 401 402 403 404 405 406 407 408

;; Mule and MIME related variables.

;;;###autoload
(defvar rmail-file-coding-system nil
  "Coding system used in RMAIL file.

This is set to nil by default.")

;;;###autoload
409
(defcustom rmail-enable-mime nil
410 411 412
  "*If non-nil, RMAIL uses MIME feature.
If the value is t, RMAIL automatically shows MIME decoded message.
If the value is neither t nor nil, RMAIL does not show MIME decoded message
413
until a user explicitly requires it."
414 415
  :type '(choice (const :tag "on" t)
		 (const :tag "off" nil)
416
		 (sexp :tag "when asked" :format "%t\n" ask))
417
  :group 'rmail)
418 419 420 421 422 423 424 425 426 427 428

;;;###autoload
(defvar rmail-show-mime-function nil
  "Function to show MIME decoded message of RMAIL file.")

;;;###autoload
(defvar rmail-mime-feature 'rmail-mime
  "Feature to provide for using MIME feature in RMAIL.
When starting rmail, this feature is requrired if rmail-enable-mime
is non-nil.")

Roland McGrath's avatar
Roland McGrath committed
429 430 431

;;;; *** Rmail Mode ***

432 433
(defvar rmail-enable-multibyte nil)

Roland McGrath's avatar
Roland McGrath committed
434 435 436
;;;###autoload
(defun rmail (&optional file-name-arg)
  "Read and edit incoming mail.
437
Moves messages into file named by `rmail-file-name' (a babyl format file)
Roland McGrath's avatar
Roland McGrath committed
438 439 440
 and edits that file in RMAIL Mode.
Type \\[describe-mode] once editing that file, for a list of RMAIL commands.

441 442 443
May be called with file name as argument; then performs rmail editing on
that file, but does not copy any new mail into the file.
Interactively, if you supply a prefix argument, then you
444 445 446
have a chance to specify a file name with the minibuffer.

If `rmail-display-summary' is non-nil, make a summary for this RMAIL file."
Roland McGrath's avatar
Roland McGrath committed
447
  (interactive (if current-prefix-arg
448
		   (list (read-file-name "Run rmail on RMAIL file: "))))
449 450 451 452 453 454
  (if rmail-enable-mime
      (condition-case err
	  (require rmail-mime-feature)
	(error (message "Feature `%s' not provided" rmail-mime-feature)
	       (sit-for 1)
	       (setq rmail-enable-mime nil))))
Roland McGrath's avatar
Roland McGrath committed
455
  (let* ((file-name (expand-file-name (or file-name-arg rmail-file-name)))
456
	 (existed (get-file-buffer file-name))
457 458 459
	 ;; This binding is necessary because we much decide if we
	 ;; need code conversion while the buffer is unibyte
	 ;; (i.e. enable-multibyte-characters is nil).
460 461 462 463 464 465 466 467
	 (rmail-enable-multibyte (default-value 'enable-multibyte-characters))
	 ;; Since the file may contain messages of different encodings
	 ;; at the tail (non-BYBYL part), we can't decode them at once
	 ;; on reading.  So, at first, we read the file without text
	 ;; code conversion, then decode the messages one by one by
	 ;; rmail-decode-babyl-format or
	 ;; rmail-convert-to-babyl-format.
	 (coding-system-for-read (and rmail-enable-multibyte 'raw-text))
468
	 run-mail-hook msg-shown)
Roland McGrath's avatar
Roland McGrath committed
469 470 471 472 473 474
    ;; Like find-file, but in the case where a buffer existed
    ;; and the file was reverted, recompute the message-data.
    (if (and existed (not (verify-visited-file-modtime existed)))
	(progn
	  ;; Don't be confused by apparent local-variables spec
	  ;; in the last message in the RMAIL file.
475 476
	  (let ((enable-local-variables nil))
	    (find-file file-name))
477 478
	  (if (and (verify-visited-file-modtime existed)
		   (eq major-mode 'rmail-mode))
Roland McGrath's avatar
Roland McGrath committed
479 480
	      (progn (rmail-forget-messages)
		     (rmail-set-message-counters))))
481 482
      (let ((enable-local-variables nil))
	(find-file file-name)))
483
    (if (eq major-mode 'rmail-edit-mode)
484
	(error "Exit Rmail Edit mode before getting new mail"))
Roland McGrath's avatar
Roland McGrath committed
485 486 487
    (if (and existed (> (buffer-size) 0))
	;; Buffer not new and not empty; ensure in proper mode, but that's all.
	(or (eq major-mode 'rmail-mode)
488 489 490
	    (progn (rmail-mode-2)
		   (setq run-mail-hook t)))
      (setq run-mail-hook t)
Roland McGrath's avatar
Roland McGrath committed
491 492 493
      (rmail-mode-2)
      ;; Convert all or part to Babyl file if possible.
      (rmail-convert-file)
494 495 496 497 498
      ;; As we have read a file by raw-text, the buffer is set to
      ;; unibyte.  We must make it multibyte if necessary.
      (if (and rmail-enable-multibyte
	       (not enable-multibyte-characters))
	  (set-buffer-multibyte t))
Roland McGrath's avatar
Roland McGrath committed
499 500 501 502
      (goto-char (point-max))
      (if (null rmail-inbox-list)
	  (progn
	    (rmail-set-message-counters)
503 504
	    (rmail-show-message)
	    (setq msg-shown t))))
505 506 507 508 509 510 511 512 513
    (unwind-protect
	(or (and (null file-name-arg)
		 (rmail-get-new-mail))
	    (or msg-shown (rmail-show-message (rmail-first-unseen-message))))
      (progn
	(if rmail-display-summary (rmail-summary))
	(rmail-construct-io-menu)
	(if run-mail-hook
	    (run-hooks 'rmail-mode-hook))))))
Roland McGrath's avatar
Roland McGrath committed
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544

;; Given the value of MAILPATH, return a list of inbox file names.
;; This is turned off because it is not clear that the user wants
;; all these inboxes to feed into the primary rmail file.
; (defun rmail-convert-mailpath (string)
;   (let (idx list)
;     (while (setq idx (string-match "[%:]" string))
;       (let ((this (substring string 0 idx)))
; 	(setq string (substring string (1+ idx)))
; 	(setq list (cons (if (string-match "%" this)
; 			     (substring this 0 (string-match "%" this))
; 			   this)
; 			 list))))
;     list))

; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
; will not cause emacs 18.55 problems.

(defun rmail-convert-file ()
  (let (convert)
    (widen)
    (goto-char (point-min))
    ;; If file doesn't start like a Babyl file,
    ;; convert it to one, by adding a header and converting each message.
    (cond ((looking-at "BABYL OPTIONS:"))
	  ((looking-at "Version: 5\n")
	   ;; Losing babyl file made by old version of Rmail.
	   ;; Just fix the babyl file header; don't make a new one,
	   ;; so we don't lose the Labels: file attribute, etc.
	   (let ((buffer-read-only nil))
	     (insert "BABYL OPTIONS: -*- rmail -*-\n")))
545 546 547
	  ((equal (point-min) (point-max))
	   ;; Empty RMAIL file.  Just insert the header.
	   (rmail-insert-rmail-file-header))
Roland McGrath's avatar
Roland McGrath committed
548
	  (t
549
	   ;; Non-empty file in non-RMAIL format.  Add header and convert.
Roland McGrath's avatar
Roland McGrath committed
550 551 552 553 554 555
	   (setq convert t)
	   (rmail-insert-rmail-file-header)))
    ;; If file was not a Babyl file or if there are
    ;; Unix format messages added at the end,
    ;; convert file as necessary.
    (if (or convert
556 557
	    (save-excursion
	      (goto-char (point-max))
558 559
	      (search-backward "\n\^_")
	      (forward-char 2)
560
	      (looking-at "\n*From ")))
Roland McGrath's avatar
Roland McGrath committed
561 562
	(let ((buffer-read-only nil))
	  (message "Converting to Babyl format...")
563 564 565 566 567
	  ;; If file needs conversion, convert it all,
	  ;; except for the BABYL header.
	  ;; (rmail-convert-to-babyl-format would delete the header.)
	  (goto-char (point-min))
	  (search-forward "\n\^_" nil t)
568
	  (narrow-to-region (point) (point-max))
Roland McGrath's avatar
Roland McGrath committed
569
	  (rmail-convert-to-babyl-format)
570 571 572 573 574
	  (message "Converting to Babyl format...done"))
      (if (and (not rmail-enable-mime)
	       rmail-enable-multibyte)
	  ;; We still have to decode BABYL part.
	  (rmail-decode-babyl-format)))))
Roland McGrath's avatar
Roland McGrath committed
575

576 577
;;; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
;;; will not cause emacs 18.55 problems.
Roland McGrath's avatar
Roland McGrath committed
578 579 580 581 582 583 584 585 586 587

(defun rmail-insert-rmail-file-header ()
  (let ((buffer-read-only nil))
    (insert "BABYL OPTIONS: -*- rmail -*-
Version: 5
Labels:
Note:   This is the header of an rmail file.
Note:   If you are seeing it in rmail,
Note:    it means the file has no messages in it.\n\^_")))

588
;; Decode Babyl formated part at the head of current buffer by
589
;; rmail-file-coding-system, or if it is nil, do auto conversion.
590 591 592 593

(defun rmail-decode-babyl-format ()
  (let ((modifiedp (buffer-modified-p))
	(buffer-read-only nil)
594
	from to)
595 596
    (goto-char (point-min))
    (search-forward "\n\^_" nil t)	; Skip BYBYL header.
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
    (setq from (point))
    (goto-char (point-max))
    (search-backward "\n\^_" from 'mv)
    (setq to (point))
    (if (not (and rmail-file-coding-system
		  (coding-system-p rmail-file-coding-system)))
	(setq rmail-file-coding-system (detect-coding-region from to t)))
    (if (not (eq rmail-file-coding-system 'undecided))
	(let ((count 1))
	  (goto-char from)
	  (while (search-forward "\n\^_" nil t)
	    (decode-coding-region from (1- (point)) rmail-file-coding-system)
	    (goto-char (point))
	    (setq from (point))
	    (if (= (% count 10) 0)
612
		(message "Decoding messages...%d" count))
613
	    (setq count (1+ count)))
614
	  (message "Decoding messages...done")
615 616
	  (set-buffer-file-coding-system rmail-file-coding-system)
	  (set-buffer-modified-p modifiedp)))))
617

Roland McGrath's avatar
Roland McGrath committed
618 619 620 621
(if rmail-mode-map
    nil
  (setq rmail-mode-map (make-keymap))
  (suppress-keymap rmail-mode-map)
622 623 624 625 626 627 628 629 630 631 632 633 634
  (define-key rmail-mode-map "a"      'rmail-add-label)
  (define-key rmail-mode-map "b"      'rmail-bury)
  (define-key rmail-mode-map "c"      'rmail-continue)
  (define-key rmail-mode-map "d"      'rmail-delete-forward)
  (define-key rmail-mode-map "\C-d"   'rmail-delete-backward)
  (define-key rmail-mode-map "e"      'rmail-edit-current-message)
  (define-key rmail-mode-map "f"      'rmail-forward)
  (define-key rmail-mode-map "g"      'rmail-get-new-mail)
  (define-key rmail-mode-map "h"      'rmail-summary)
  (define-key rmail-mode-map "i"      'rmail-input)
  (define-key rmail-mode-map "j"      'rmail-show-message)
  (define-key rmail-mode-map "k"      'rmail-kill-label)
  (define-key rmail-mode-map "l"      'rmail-summary-by-labels)
Roland McGrath's avatar
Roland McGrath committed
635 636 637 638
  (define-key rmail-mode-map "\e\C-h" 'rmail-summary)
  (define-key rmail-mode-map "\e\C-l" 'rmail-summary-by-labels)
  (define-key rmail-mode-map "\e\C-r" 'rmail-summary-by-recipients)
  (define-key rmail-mode-map "\e\C-s" 'rmail-summary-by-regexp)
639 640 641 642 643 644 645 646 647 648 649 650 651
  (define-key rmail-mode-map "\e\C-t" 'rmail-summary-by-topic)
  (define-key rmail-mode-map "m"      'rmail-mail)
  (define-key rmail-mode-map "\em"    'rmail-retry-failure)
  (define-key rmail-mode-map "n"      'rmail-next-undeleted-message)
  (define-key rmail-mode-map "\en"    'rmail-next-message)
  (define-key rmail-mode-map "\e\C-n" 'rmail-next-labeled-message)
  (define-key rmail-mode-map "o"      'rmail-output-to-rmail-file)
  (define-key rmail-mode-map "\C-o"   'rmail-output)
  (define-key rmail-mode-map "p"      'rmail-previous-undeleted-message)
  (define-key rmail-mode-map "\ep"    'rmail-previous-message)
  (define-key rmail-mode-map "\e\C-p" 'rmail-previous-labeled-message)
  (define-key rmail-mode-map "q"      'rmail-quit)
  (define-key rmail-mode-map "r"      'rmail-reply)
652
;; I find I can't live without the default M-r command -- rms.
653 654 655 656 657
;;  (define-key rmail-mode-map "\er"  'rmail-search-backwards)
  (define-key rmail-mode-map "s"      'rmail-expunge-and-save)
  (define-key rmail-mode-map "\es"    'rmail-search)
  (define-key rmail-mode-map "t"      'rmail-toggle-header)
  (define-key rmail-mode-map "u"      'rmail-undelete-previous-message)
658
  (define-key rmail-mode-map "w"      'rmail-output-body-to-file)
659 660 661 662 663 664 665
  (define-key rmail-mode-map "x"      'rmail-expunge)
  (define-key rmail-mode-map "."      'rmail-beginning-of-message)
  (define-key rmail-mode-map "<"      'rmail-first-message)
  (define-key rmail-mode-map ">"      'rmail-last-message)
  (define-key rmail-mode-map " "      'scroll-up)
  (define-key rmail-mode-map "\177"   'scroll-down)
  (define-key rmail-mode-map "?"      'describe-mode)
666 667 668 669 670 671
  (define-key rmail-mode-map "\C-c\C-s\C-d" 'rmail-sort-by-date)
  (define-key rmail-mode-map "\C-c\C-s\C-s" 'rmail-sort-by-subject)
  (define-key rmail-mode-map "\C-c\C-s\C-a" 'rmail-sort-by-author)
  (define-key rmail-mode-map "\C-c\C-s\C-r" 'rmail-sort-by-recipient)
  (define-key rmail-mode-map "\C-c\C-s\C-c" 'rmail-sort-by-correspondent)
  (define-key rmail-mode-map "\C-c\C-s\C-l" 'rmail-sort-by-lines)
672
  (define-key rmail-mode-map "\C-c\C-s\C-k" 'rmail-sort-by-keywords)
673 674
  (define-key rmail-mode-map "\C-c\C-n" 'rmail-next-same-subject)
  (define-key rmail-mode-map "\C-c\C-p" 'rmail-previous-same-subject)
675
  )
676 677 678 679 680 681

(define-key rmail-mode-map [menu-bar] (make-sparse-keymap))

(define-key rmail-mode-map [menu-bar classify]
  (cons "Classify" (make-sparse-keymap "Classify")))

682
(define-key rmail-mode-map [menu-bar classify input-menu]
683
  nil)
684 685

(define-key rmail-mode-map [menu-bar classify output-menu]
686
  nil)
687

688 689 690
(define-key rmail-mode-map [menu-bar classify output-body]
  '("Output body to file..." . rmail-output-body-to-file))

691
(define-key rmail-mode-map [menu-bar classify output-inbox]
692
  '("Output (inbox)..." . rmail-output))
693 694

(define-key rmail-mode-map [menu-bar classify output]
695
  '("Output (Rmail)..." . rmail-output-to-rmail-file))
696 697

(define-key rmail-mode-map [menu-bar classify kill-label]
698
  '("Kill Label..." . rmail-kill-label))
699 700

(define-key rmail-mode-map [menu-bar classify add-label]
701
  '("Add Label..." . rmail-add-label))
702 703 704 705

(define-key rmail-mode-map [menu-bar summary]
  (cons "Summary" (make-sparse-keymap "Summary")))

706 707 708
(define-key rmail-mode-map [menu-bar summary senders]
  '("By Senders..." . rmail-summary-by-senders))

709
(define-key rmail-mode-map [menu-bar summary labels]
710
  '("By Labels..." . rmail-summary-by-labels))
711 712

(define-key rmail-mode-map [menu-bar summary recipients]
713
  '("By Recipients..." . rmail-summary-by-recipients))
714 715

(define-key rmail-mode-map [menu-bar summary topic]
716
  '("By Topic..." . rmail-summary-by-topic))
717 718

(define-key rmail-mode-map [menu-bar summary regexp]
719
  '("By Regexp..." . rmail-summary-by-regexp))
720 721 722 723 724 725

(define-key rmail-mode-map [menu-bar summary all]
  '("All" . rmail-summary))

(define-key rmail-mode-map [menu-bar mail]
  (cons "Mail" (make-sparse-keymap "Mail")))
726

727
(define-key rmail-mode-map [menu-bar mail rmail-get-new-mail]
728 729 730
  '("Get New Mail" . rmail-get-new-mail))

(define-key rmail-mode-map [menu-bar mail lambda]
731
  '("----"))
732

733 734 735
(define-key rmail-mode-map [menu-bar mail continue]
  '("Continue" . rmail-continue))

736
(define-key rmail-mode-map [menu-bar mail resend]
737
  '("Re-send..." . rmail-resend))
738

739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
(define-key rmail-mode-map [menu-bar mail forward]
  '("Forward" . rmail-forward))

(define-key rmail-mode-map [menu-bar mail retry]
  '("Retry" . rmail-retry-failure))

(define-key rmail-mode-map [menu-bar mail reply]
  '("Reply" . rmail-reply))

(define-key rmail-mode-map [menu-bar mail mail]
  '("Mail" . rmail-mail))

(define-key rmail-mode-map [menu-bar delete]
  (cons "Delete" (make-sparse-keymap "Delete")))

(define-key rmail-mode-map [menu-bar delete expunge/save]
  '("Expunge/Save" . rmail-expunge-and-save))
Roland McGrath's avatar
Roland McGrath committed
756

757 758 759 760 761 762 763 764 765 766 767 768 769
(define-key rmail-mode-map [menu-bar delete expunge]
  '("Expunge" . rmail-expunge))

(define-key rmail-mode-map [menu-bar delete undelete]
  '("Undelete" . rmail-undelete-previous-message))

(define-key rmail-mode-map [menu-bar delete delete]
  '("Delete" . rmail-delete-forward))

(define-key rmail-mode-map [menu-bar move]
  (cons "Move" (make-sparse-keymap "Move")))

(define-key rmail-mode-map [menu-bar move search-back]
770
  '("Search Back..." . rmail-search-backwards))
771 772

(define-key rmail-mode-map [menu-bar move search]
773
  '("Search..." . rmail-search))
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792

(define-key rmail-mode-map [menu-bar move previous]
  '("Previous Nondeleted" . rmail-previous-undeleted-message))

(define-key rmail-mode-map [menu-bar move next]
  '("Next Nondeleted" . rmail-next-undeleted-message))

(define-key rmail-mode-map [menu-bar move last]
  '("Last" . rmail-last-message))

(define-key rmail-mode-map [menu-bar move first]
  '("First" . rmail-first-message))

(define-key rmail-mode-map [menu-bar move previous]
  '("Previous" . rmail-previous-message))

(define-key rmail-mode-map [menu-bar move next]
  '("Next" . rmail-next-message))

Roland McGrath's avatar
Roland McGrath committed
793 794 795
;; Rmail mode is suitable only for specially formatted data.
(put 'rmail-mode 'mode-class 'special)

796 797 798
(defun rmail-mode-kill-summary ()
  (if rmail-summary-buffer (kill-buffer rmail-summary-buffer)))

799
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
(defun rmail-mode ()
  "Rmail Mode is used by \\<rmail-mode-map>\\[rmail] for editing Rmail files.
All normal editing commands are turned off.
Instead, these commands are available:

\\[rmail-beginning-of-message]	Move point to front of this message (same as \\[beginning-of-buffer]).
\\[scroll-up]	Scroll to next screen of this message.
\\[scroll-down]	Scroll to previous screen of this message.
\\[rmail-next-undeleted-message]	Move to Next non-deleted message.
\\[rmail-previous-undeleted-message]	Move to Previous non-deleted message.
\\[rmail-next-message]	Move to Next message whether deleted or not.
\\[rmail-previous-message]	Move to Previous message whether deleted or not.
\\[rmail-first-message]	Move to the first message in Rmail file.
\\[rmail-last-message]	Move to the last message in Rmail file.
\\[rmail-show-message]	Jump to message specified by numeric position in file.
\\[rmail-search]	Search for string and show message it is found in.
\\[rmail-delete-forward]	Delete this message, move to next nondeleted.
\\[rmail-delete-backward]	Delete this message, move to previous nondeleted.
\\[rmail-undelete-previous-message]	Undelete message.  Tries current message, then earlier messages
	till a deleted message is found.
820
\\[rmail-edit-current-message]	Edit the current message.  \\[rmail-cease-edit] to return to Rmail.
Roland McGrath's avatar
Roland McGrath committed
821 822 823 824
\\[rmail-expunge]	Expunge deleted messages.
\\[rmail-expunge-and-save]	Expunge and save the file.
\\[rmail-quit]       Quit Rmail: expunge, save, then switch to another buffer.
\\[save-buffer] Save without expunging.
825
\\[rmail-get-new-mail]	Move new mail from system spool directory into this file.
Roland McGrath's avatar
Roland McGrath committed
826 827
\\[rmail-mail]	Mail a message (same as \\[mail-other-window]).
\\[rmail-continue]	Continue composing outgoing message started before.
828 829
\\[rmail-reply]	Reply to this message.  Like \\[rmail-mail] but initializes some fields.
\\[rmail-retry-failure]	Send this message again.  Used on a mailer failure message.
Roland McGrath's avatar
Roland McGrath committed
830 831 832 833 834 835 836 837 838
\\[rmail-forward]	Forward this message to another user.
\\[rmail-output-to-rmail-file]       Output this message to an Rmail file (append it).
\\[rmail-output]	Output this message to a Unix-format mail file (append it).
\\[rmail-input]	Input Rmail file.  Run Rmail on that file.
\\[rmail-add-label]	Add label to message.  It will be displayed in the mode line.
\\[rmail-kill-label]	Kill label.  Remove a label from current message.
\\[rmail-next-labeled-message]   Move to Next message with specified label
          (label defaults to last one specified).
          Standard labels: filed, unseen, answered, forwarded, deleted.
839
          Any other label is present only if you add it with \\[rmail-add-label].
Roland McGrath's avatar
Roland McGrath committed
840 841
\\[rmail-previous-labeled-message]   Move to Previous message with specified label
\\[rmail-summary]	Show headers buffer, with a one line summary of each message.
842 843 844 845 846
\\[rmail-summary-by-labels]	Summarize only messages with particular label(s).
\\[rmail-summary-by-recipients]   Summarize only messages with particular recipient(s).
\\[rmail-summary-by-regexp]   Summarize only messages with particular regexp(s).
\\[rmail-summary-by-topic]   Summarize only messages with subject line regexp(s).
\\[rmail-toggle-header]	Toggle display of complete header."
Roland McGrath's avatar
Roland McGrath committed
847 848 849
  (interactive)
  (rmail-mode-2)
  (rmail-set-message-counters)
850 851
  (rmail-show-message rmail-total-messages)
  (run-hooks 'rmail-mode-hook))
Roland McGrath's avatar
Roland McGrath committed
852 853 854 855

(defun rmail-mode-2 ()
  (kill-all-local-variables)
  (rmail-mode-1)
856
  (rmail-variables))
Roland McGrath's avatar
Roland McGrath committed
857 858 859 860 861

(defun rmail-mode-1 ()
  (setq major-mode 'rmail-mode)
  (setq mode-name "RMAIL")
  (setq buffer-read-only t)
Richard M. Stallman's avatar
Richard M. Stallman committed
862 863 864 865 866
  ;; No need to auto save RMAIL files in normal circumstances
  ;; because they contain no info except attribute changes
  ;; and deletion of messages.
  ;; The one exception is when messages are copied into an Rmail mode buffer.
  ;; rmail-output-to-rmail-file enables auto save when you do that.
Roland McGrath's avatar
Roland McGrath committed
867
  (setq buffer-auto-save-file-name nil)
868
  (setq mode-line-modified "--")
Roland McGrath's avatar
Roland McGrath committed
869 870 871 872 873 874 875
  (use-local-map rmail-mode-map)
  (set-syntax-table text-mode-syntax-table)
  (setq local-abbrev-table text-mode-abbrev-table))

(defun rmail-variables ()
  (make-local-variable 'revert-buffer-function)
  (setq revert-buffer-function 'rmail-revert)
876
  (make-local-variable 'font-lock-defaults)
877 878
  (setq font-lock-defaults
   '(rmail-font-lock-keywords t nil nil nil
879
     (font-lock-maximum-size . nil)
880 881 882
     (font-lock-fontify-buffer-function . rmail-fontify-buffer-function)
     (font-lock-unfontify-buffer-function . rmail-unfontify-buffer-function)
     (font-lock-inhibit-thing-lock . (lazy-lock-mode fast-lock-mode))))
Roland McGrath's avatar
Roland McGrath committed
883
  (make-local-variable 'rmail-last-label)
884
  (make-local-variable 'rmail-last-regexp)
Roland McGrath's avatar
Roland McGrath committed
885
  (make-local-variable 'rmail-deleted-vector)
886 887 888 889
  (make-local-variable 'rmail-buffer)
  (setq rmail-buffer (current-buffer))
  (make-local-variable 'rmail-view-buffer)
  (setq rmail-view-buffer rmail-buffer)
Roland McGrath's avatar
Roland McGrath committed
890 891 892 893 894 895
  (make-local-variable 'rmail-summary-buffer)
  (make-local-variable 'rmail-summary-vector)
  (make-local-variable 'rmail-current-message)
  (make-local-variable 'rmail-total-messages)
  (make-local-variable 'require-final-newline)
  (setq require-final-newline nil)
896 897
  (make-local-variable 'rmail-overlay-list)
  (setq rmail-overlay-list nil)
Roland McGrath's avatar
Roland McGrath committed
898 899
  (make-local-variable 'version-control)
  (setq version-control 'never)
900 901
  (make-local-variable 'kill-buffer-hook)
  (add-hook 'kill-buffer-hook 'rmail-mode-kill-summary)
Roland McGrath's avatar
Roland McGrath committed
902 903 904
  (make-local-variable 'file-precious-flag)
  (setq file-precious-flag t)
  (make-local-variable 'rmail-message-vector)
905
  (make-local-variable 'rmail-msgref-vector)
Roland McGrath's avatar
Roland McGrath committed
906 907
  (make-local-variable 'rmail-inbox-list)
  (setq rmail-inbox-list (rmail-parse-file-inboxes))
908 909
  ;; Provide default set of inboxes for primary mail file ~/RMAIL.
  (and (null rmail-inbox-list)
910
       (or (equal buffer-file-name (expand-file-name rmail-file-name))
911 912
	   (equal buffer-file-truename
		  (abbreviate-file-name (file-truename rmail-file-name))))
913 914 915 916
       (setq rmail-inbox-list
	     (or rmail-primary-inbox-list
		 (list (or (getenv "MAIL")
			   (concat rmail-spool-directory
917
				   (user-login-name)))))))
Roland McGrath's avatar
Roland McGrath committed
918 919
  (make-local-variable 'rmail-keywords)
  ;; this gets generated as needed
920
  (setq rmail-keywords nil))
Roland McGrath's avatar
Roland McGrath committed
921 922 923

;; Handle M-x revert-buffer done in an rmail-mode buffer.
(defun rmail-revert (arg noconfirm)
924
  (let ((revert-buffer-function (default-value 'revert-buffer-function)))
Roland McGrath's avatar
Roland McGrath committed
925 926 927 928
    ;; Call our caller again, but this time it does the default thing.
    (if (revert-buffer arg noconfirm)
	;; If the user said "yes", and we changed something,
	;; reparse the messages.
929
	(progn
Roland McGrath's avatar
Roland McGrath committed
930 931
	  (rmail-convert-file)
	  (goto-char (point-max))
932
	  (rmail-mode)))))
Roland McGrath's avatar
Roland McGrath committed
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955

;; Return a list of files from this buffer's Mail: option.
;; Does not assume that messages have been parsed.
;; Just returns nil if buffer does not look like Babyl format.
(defun rmail-parse-file-inboxes ()
  (save-excursion
    (save-restriction
      (widen)
      (goto-char 1)
      (cond ((looking-at "BABYL OPTIONS:")
	     (search-forward "\n\^_" nil 'move)
	     (narrow-to-region 1 (point))
	     (goto-char 1)
	     (if (search-forward "\nMail:" nil t)
		 (progn
		   (narrow-to-region (point) (progn (end-of-line) (point)))
		   (goto-char (point-min))
		   (mail-parse-comma-list))))))))

(defun rmail-expunge-and-save ()
  "Expunge and save RMAIL file."
  (interactive)
  (rmail-expunge)
956 957 958
  (save-buffer)
  (if (rmail-summary-exists)
      (rmail-select-summary (set-buffer-modified-p nil))))
Roland McGrath's avatar
Roland McGrath committed
959 960 961 962 963 964 965

(defun rmail-quit ()
  "Quit out of RMAIL."
  (interactive)
  (rmail-expunge-and-save)
  ;; Don't switch to the summary buffer even if it was recently visible.
  (if rmail-summary-buffer
966 967 968
      (progn
	(replace-buffer-in-windows rmail-summary-buffer)
	(bury-buffer rmail-summary-buffer)))
Roland McGrath's avatar
Roland McGrath committed
969
  (let ((obuf (current-buffer)))
970
    (replace-buffer-in-windows obuf)
Roland McGrath's avatar
Roland McGrath committed
971 972
    (bury-buffer obuf)))

973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
(defun rmail-bury ()
  "Bury current Rmail buffer and its summary buffer."
  (interactive)
  ;; This let var was called rmail-buffer, but that interfered
  ;; with the buffer-local var used in summary buffers.
  (let ((buffer-to-bury (current-buffer)))
    (if (rmail-summary-exists)
	(let (window)
	  (while (setq window (get-buffer-window rmail-summary-buffer))
	    (set-window-buffer window (other-buffer rmail-summary-buffer)))
	  (bury-buffer rmail-summary-buffer)))
    (switch-to-buffer (other-buffer (current-buffer)))
    (bury-buffer buffer-to-bury)))

(defun rmail-duplicate-message ()
  "Create a duplicated copy of the current message.
The duplicate copy goes into the Rmail file just after the
original copy."
  (interactive)
  (widen)
  (let ((buffer-read-only nil)
	(number rmail-current-message)
	(string (buffer-substring (rmail-msgbeg rmail-current-message)
				  (rmail-msgend rmail-current-message))))
    (goto-char (rmail-msgend rmail-current-message))
    (insert string)
    (rmail-forget-messages)
    (rmail-show-message number)
    (message "Message duplicated")))

Roland McGrath's avatar
Roland McGrath committed
1003 1004
;;;###autoload
(defun rmail-input (filename)
1005
  "Run Rmail on file FILENAME."
Roland McGrath's avatar
Roland McGrath committed
1006 1007 1008
  (interactive "FRun rmail on RMAIL file: ")
  (rmail filename))

1009 1010 1011 1012 1013

;; This used to scan subdirectories recursively, but someone pointed out
;; that if the user wants that, person can put all the files in one dir.
;; And the recursive scan was slow.  So I took it out.
;; rms, Sep 1996.
1014
(defun rmail-find-all-files (start)
1015
  "Return list of file in dir START that match `rmail-secondary-file-regexp'."
1016
  (if (file-accessible-directory-p start)
1017
      ;; Don't sort here.
1018 1019
      (let* ((case-fold-search t)
	     (files (directory-files start t rmail-secondary-file-regexp)))
1020 1021
	;; Sort here instead of in directory-files
	;; because this list is usually much shorter.
1022
	(sort files 'string<))))
1023 1024 1025 1026 1027

(defun rmail-list-to-menu (menu-name l action &optional full-name)
  (let ((menu (make-sparse-keymap menu-name)))
    (mapcar
     (function (lambda (item)
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
		 (let (command)
		   (if (consp item)
		       (progn
			 (setq command
			       (rmail-list-to-menu (car item) (cdr item) 
						   action 
						   (if full-name
						       (concat full-name "/"
							       (car item))
						     (car item))))
			 (setq name (car item)))
1039
		     (progn
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
		       (setq name item)
		       (setq command 
			     (list 'lambda () '(interactive)
				   (list action
					 (expand-file-name 
					  (if full-name
					      (concat full-name "/" item)
					    item)
					  rmail-secondary-file-directory))))))
		   (define-key menu (vector (intern name))
		     (cons name command)))))
1051
     (reverse l))
1052 1053
    menu))
 
1054 1055 1056
;; This command is always "disabled" when it appears in a menu.
(put 'rmail-disable-menu 'menu-enable ''nil)

1057 1058
(defun rmail-construct-io-menu ()
  (let ((files (rmail-find-all-files rmail-secondary-file-directory)))
1059
    (if files
1060 1061 1062 1063
	(progn
	  (define-key rmail-mode-map [menu-bar classify input-menu]
	    (cons "Input Rmail File" 
		  (rmail-list-to-menu "Input Rmail File" 
1064
				      files
1065 1066 1067 1068
				      'rmail-input)))
	  (define-key rmail-mode-map [menu-bar classify output-menu]
	    (cons "Output Rmail File" 
		  (rmail-list-to-menu "Output Rmail File" 
1069 1070 1071 1072 1073 1074 1075
				      files
				      'rmail-output-to-rmail-file))))

      (define-key rmail-mode-map [menu-bar classify input-menu]
	'("Input Rmail File" . rmail-disable-menu))
      (define-key rmail-mode-map [menu-bar classify output-menu]
	'("Output Rmail File" . rmail-disable-menu)))))
1076

Roland McGrath's avatar
Roland McGrath committed
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086

;;;; *** Rmail input ***

;; RLK feature not added in this version:
;; argument specifies inbox file or files in various ways.

(defun rmail-get-new-mail (&optional file-name)
  "Move any new mail from this RMAIL file's inbox files.
The inbox files can be specified with the file's Mail: option.  The
variable `rmail-primary-inbox-list' specifies the inboxes for your
1087 1088
primary RMAIL file if it has no Mail: option.  By default, this is
your /usr/spool/mail/$USER.
Roland McGrath's avatar
Roland McGrath committed
1089 1090 1091 1092

You can also specify the file to get new mail from.  In this case, the
file of new mail is not changed or deleted.  Noninteractively, you can
pass the inbox file name as an argument.  Interactively, a prefix
1093 1094
argument causes us to read a file name and use that file as the inbox.

1095 1096 1097
If the variable `rmail-preserve-inbox' is non-nil, new mail will
always be left in inbox files rather than deleted.

1098 1099
This function runs `rmail-get-new-mail-hook' before saving the updated file.
It returns t if it got any new messages."
Roland McGrath's avatar
Roland McGrath committed
1100 1101 1102
  (interactive
   (list (if current-prefix-arg
	     (read-file-name "Get new mail from file: "))))
1103
  (run-hooks 'rmail-before-get-new-mail-hook)
1104 1105
  ;; If the disk file has been changed from under us,
  ;; revert to it before we get new mail.
Roland McGrath's avatar
Roland McGrath committed
1106
  (or (verify-visited-file-modtime (current-buffer))
1107
      (find-file (buffer-file-name)))
Roland McGrath's avatar
Roland McGrath committed
1108 1109 1110 1111 1112
  (rmail-maybe-set-message-counters)
  (widen)
  ;; Get rid of all undo records for this buffer.
  (or (eq buffer-undo-list t)
      (setq buffer-undo-list nil))