rmail.el 121 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 4
;; Copyright (C) 1985,86,87,88,93,94,95,96,97,1998
;;		Free Software Foundation, Inc.
Eric S. Raymond's avatar
Eric S. Raymond committed
5

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

Roland McGrath's avatar
Roland McGrath committed
9 10 11 12
;; 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
13
;; the Free Software Foundation; either version 2, or (at your option)
Roland McGrath's avatar
Roland McGrath committed
14 15 16 17 18 19 20 21
;; 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
22 23 24
;; 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
25

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

;; 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.

33 34 35 36 37 38 39
;; 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
40
(require 'mail-utils)
41 42

; These variables now declared in paths.el.
Roland McGrath's avatar
Roland McGrath committed
43 44
;(defvar rmail-spool-directory "/usr/spool/mail/"
;  "This is the name of the directory used by the system mailer for\n\
45
;delivering new mail.  Its name should end with a slash.")
Roland McGrath's avatar
Roland McGrath committed
46 47 48 49
;(defvar rmail-file-name
;  (expand-file-name "~/RMAIL")
;  "")

50 51 52 53 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
(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)


87 88 89
(defcustom rmail-movemail-program nil
  "If non-nil, name of program for fetching new mail."
  :group 'rmail-retrieve
90
  :type '(choice (const nil) string))
91

92 93 94 95 96
(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)
97

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

103
(defcustom rmail-movemail-flags nil
Karl Heuer's avatar
Karl Heuer committed
104 105 106
  "*List of flags to pass to movemail.
Most commonly used to specify `-g' to enable GSS-API authentication
or `-k' to enable Kerberos authentication."
107
  :type '(repeat string)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
108 109
  :group 'rmail-retrieve
  :version "20.3")
110

111 112 113 114 115
(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].")

116 117
(defvar rmail-encoded-pop-password nil)

118
(defcustom rmail-preserve-inbox nil
119
  "*Non-nil if incoming mail should be left in the user's inbox,
120 121 122
rather than deleted, after it is retrieved."
  :type 'boolean
  :group 'rmail-retrieve)
123

Roland McGrath's avatar
Roland McGrath committed
124
;;;###autoload
125
(defcustom rmail-dont-reply-to-names nil "\
Roland McGrath's avatar
Roland McGrath committed
126
*A regexp specifying names to prune of reply to messages.
127 128
A value of nil means exclude your own login name as an address
plus whatever is specified by `rmail-default-dont-reply-to-names'."
129 130
  :type '(choice regexp (const :tag "Your Name" nil))
  :group 'rmail-reply)
Roland McGrath's avatar
Roland McGrath committed
131 132 133 134 135 136 137

;;;###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
138
It is useful to set this variable in the site customization file.")
Roland McGrath's avatar
Roland McGrath committed
139 140

;;;###autoload
141
(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:\\|^x-coding-system:\\|^return-path:\\|^errors-to:\\|^return-receipt-to:\\|^x-attribution:\\|^x-disclaimer:"
142 143 144
  "*Regexp to match header fields that Rmail should normally hide."
  :type 'regexp
  :group 'rmail-headers)
Roland McGrath's avatar
Roland McGrath committed
145

146
;;;###autoload
147
(defcustom rmail-displayed-headers nil
148 149
  "*Regexp to match Header fields that Rmail should display.
If nil, display all header fields except those matched by
150 151 152
`rmail-ignored-headers'."
  :type '(choice regexp (const :tag "All"))
  :group 'rmail-headers)
153

154
;;;###autoload
155 156 157 158
(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)
159

160
;;;###autoload
161
(defcustom rmail-highlighted-headers "^From:\\|^Subject:" "\
162
*Regexp to match Header fields that Rmail should normally highlight.
163
A value of nil means don't highlight.
164 165 166
See also `rmail-highlight-face'."
  :type 'regexp
  :group 'rmail-headers)
167

168
;;;###autoload
169 170 171 172 173
(defcustom rmail-highlight-face nil "\
*Face used by Rmail for highlighting headers."
  :type '(choice (const :tag "Default" nil)
		 face)
  :group 'rmail-headers)
174

Roland McGrath's avatar
Roland McGrath committed
175
;;;###autoload
176 177 178 179
(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
180 181

;;;###autoload
182
(defcustom rmail-primary-inbox-list nil "\
183 184 185
*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,
186
and the value of the environment variable MAIL overrides it)."
187 188 189
  ;; Don't use backquote here, because we don't want to need it
  ;; at load time.
  :type (list 'choice '(const :tag "Default" nil)
190 191 192
	      (list 'repeat ':value (list (or (getenv "MAIL")
					      (concat "/var/spool/mail/"
						      (getenv "USER"))))
193
		    'file))
194 195
  :group 'rmail-retrieve
  :group 'rmail-files)
Roland McGrath's avatar
Roland McGrath committed
196

197
;;;###autoload
198 199 200 201
(defcustom rmail-mail-new-frame nil
  "*Non-nil means Rmail makes a new frame for composing outgoing mail."
  :type 'boolean
  :group 'rmail-reply)
202

203
;;;###autoload
204 205 206 207
(defcustom rmail-secondary-file-directory "~/"
  "*Directory for additional secondary Rmail files."
  :type 'directory
  :group 'rmail-files)
208
;;;###autoload
209 210 211 212
(defcustom rmail-secondary-file-regexp "\\.xmail$"
  "*Regexp for which files are secondary Rmail files."
  :type 'regexp
  :group 'rmail-files)
213

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
;;;###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.")

232 233 234
;; 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
235

236
(defvar rmail-mmdf-delim1 "^\001\001\001\001\n"
Roland McGrath's avatar
Roland McGrath committed
237
  "Regexp marking the start of an mmdf message")
238
(defvar rmail-mmdf-delim2 "^\001\001\001\001\n"
Roland McGrath's avatar
Roland McGrath committed
239 240
  "Regexp marking the end of an mmdf message")

241
(defcustom rmail-message-filter nil
242 243
  "If non-nil, a filter function for new messages in RMAIL.
Called with region narrowed to the message, including headers,
244 245 246
before obeying `rmail-ignored-headers'."
  :group 'rmail-headers
  :type 'function)
Roland McGrath's avatar
Roland McGrath committed
247

248 249 250
(defvar rmail-reply-prefix "Re: "
  "String to prepend to Subject line when replying to a message.")

251
;; Some mailers use "Re(2):" or "Re^2:" or "Re: Re:" or "Re[2]:".
Karl Heuer's avatar
Karl Heuer committed
252
;; This pattern should catch all the common variants.
253
(defvar rmail-reply-regexp "\\`\\(Re\\(([0-9]+)\\|\\[[0-9]+\\]\\|\\^[0-9]+\\)?: *\\)*"
254
  "Regexp to delete from Subject line before inserting `rmail-reply-prefix'.")
Karl Heuer's avatar
Karl Heuer committed
255

256 257 258 259 260
(defcustom rmail-display-summary nil
  "*If non-nil, Rmail always displays the summary buffer."
  :group 'rmail-summary
  :type 'boolean)

Roland McGrath's avatar
Roland McGrath committed
261
(defvar rmail-inbox-list nil)
262 263
(put 'rmail-inbox-list 'permanent-local t)

Roland McGrath's avatar
Roland McGrath committed
264
(defvar rmail-keywords nil)
265
(put 'rmail-keywords 'permanent-local t)
Roland McGrath's avatar
Roland McGrath committed
266

267 268 269 270 271 272
(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
273 274 275
;; Message counters and markers.  Deleted flags.

(defvar rmail-current-message nil)
276 277
(put 'rmail-current-message 'permanent-local t)

Roland McGrath's avatar
Roland McGrath committed
278
(defvar rmail-total-messages nil)
279 280
(put 'rmail-total-messages 'permanent-local t)

Roland McGrath's avatar
Roland McGrath committed
281
(defvar rmail-message-vector nil)
282 283
(put 'rmail-message-vector 'permanent-local t)

Roland McGrath's avatar
Roland McGrath committed
284
(defvar rmail-deleted-vector nil)
285 286
(put 'rmail-deleted-vector 'permanent-local t)

287 288 289 290
(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.")
291
(put 'rmail-msgref-vector 'permanent-local t)
Roland McGrath's avatar
Roland McGrath committed
292

293
(defvar rmail-overlay-list nil)
294
(put 'rmail-overlay-list 'permanent-local t)
295

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

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

303 304 305 306
(defvar rmail-view-buffer nil
  "Buffer which holds RMAIL message for MIME displaying.")
(put 'rmail-view-buffer 'permanent-local t)

Roland McGrath's avatar
Roland McGrath committed
307 308 309 310
;; `Sticky' default variables.

;; Last individual label specified to a or k.
(defvar rmail-last-label nil)
311 312
(put 'rmail-last-label 'permanent-local t)

313
;; 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
314
(defvar rmail-last-multi-labels nil)
315

316
(defvar rmail-last-regexp nil)
317 318
(put 'rmail-last-regexp 'permanent-local t)

319 320 321 322 323 324 325 326
(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)
327 328 329 330 331
(defcustom rmail-default-body-file "~/mailout"
  "*Default file name for \\[rmail-output-body-to-file]."
  :type 'file
  :group 'rmail-files
  :version "20.3")
Jim Blandy's avatar
Jim Blandy committed
332

333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
;; 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
(defcustom rmail-enable-mime nil
  "*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
until a user explicitly requires it."
  :type '(choice (const :tag "on" t)
		 (const :tag "off" nil)
349
		 (other :tag "when asked" ask))
350 351 352 353 354 355 356 357
  :group 'rmail)

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

;;;###autoload
(defvar rmail-mime-feature 'rmail-mime
358 359 360
  "Feature to require to load MIME support in Rmail.
When starting Rmail, if `rmail-enable-mime' is non-nil,
this feature is required with `require'.")
361

Jim Blandy's avatar
Jim Blandy committed
362 363 364 365
;;; 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
366
(defvar rmail-unix-mail-delimiter
Jim Blandy's avatar
Jim Blandy committed
367
  (let ((time-zone-regexp
368
	 (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
Jim Blandy's avatar
Jim Blandy committed
369 370 371 372 373 374
		 "\\|[-+]?[0-9][0-9][0-9][0-9]"
		 "\\|"
		 "\\) *")))
    (concat
     "From "

375 376 377 378 379 380 381 382 383 384 385
     ;; 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'.
386 387 388 389 390
     ;; 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.
391 392 393
     ;; 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.
394
     "\\([^\0-\b\n-\r\^?].*\\)? "
Jim Blandy's avatar
Jim Blandy committed
395 396

     ;; The time the message was sent.
397 398 399 400
     "\\([^\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
401 402 403 404 405 406

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

     ;; The year.
407
     " \\([0-9][0-9]+\\) *"
Jim Blandy's avatar
Jim Blandy committed
408 409 410 411

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

Karl Heuer's avatar
Karl Heuer committed
412 413
     ;; Old uucp cruft.
     "\\(remote from .*\\)?"
Jim Blandy's avatar
Jim Blandy committed
414 415 416 417

     "\n"))
  nil)

418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
(defvar rmail-font-lock-keywords
  (eval-when-compile
    (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)
	    '("^\\(To\\|Apparently-To\\|Cc\\|Newsgroups\\):"
	      . font-lock-keyword-face)
	    ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
	    `(,cite-chars
	      (,(concat "\\=[ \t]*"
			"\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
			"\\(" cite-chars "[ \t]*\\)\\)+"
			"\\(.*\\)")
	       (beginning-of-line) (end-of-line)
	       (2 font-lock-constant-face nil t)
	       (4 font-lock-comment-face nil t)))
	    '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\|Date\\):.*$"
	      . font-lock-string-face))))
  "Additional expressions to highlight in Rmail mode.")

441 442 443
;; 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)
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
  (` (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))))
Roland McGrath's avatar
Roland McGrath committed
463 464 465

;;;; *** Rmail Mode ***

466 467
(defvar rmail-enable-multibyte nil)

Roland McGrath's avatar
Roland McGrath committed
468 469 470
;;;###autoload
(defun rmail (&optional file-name-arg)
  "Read and edit incoming mail.
471
Moves messages into file named by `rmail-file-name' (a babyl format file)
Roland McGrath's avatar
Roland McGrath committed
472 473 474
 and edits that file in RMAIL Mode.
Type \\[describe-mode] once editing that file, for a list of RMAIL commands.

475 476 477
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
478 479 480
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
481
  (interactive (if current-prefix-arg
482
		   (list (read-file-name "Run rmail on RMAIL file: "))))
483 484 485 486 487 488
  (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
489
  (let* ((file-name (expand-file-name (or file-name-arg rmail-file-name)))
490
	 (existed (get-file-buffer file-name))
491 492 493
	 ;; 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).
494 495 496 497
         (rmail-enable-multibyte
          (if existed 
	      (with-current-buffer existed enable-multibyte-characters)
            (default-value 'enable-multibyte-characters)))
498 499 500 501 502 503 504
	 ;; 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))
505
	 run-mail-hook msg-shown)
Roland McGrath's avatar
Roland McGrath committed
506 507
    ;; Like find-file, but in the case where a buffer existed
    ;; and the file was reverted, recompute the message-data.
508 509 510 511 512
    ;; We used to bind enable-local-variables to nil here,
    ;; but that should not be needed now that rmail-mode
    ;; sets it locally to nil.
    ;; (Binding a variable locally with let is not safe if it has
    ;; buffer-local bindings.)
Roland McGrath's avatar
Roland McGrath committed
513 514
    (if (and existed (not (verify-visited-file-modtime existed)))
	(progn
515
	  (find-file file-name)
516 517
	  (if (and (verify-visited-file-modtime existed)
		   (eq major-mode 'rmail-mode))
Roland McGrath's avatar
Roland McGrath committed
518 519
	      (progn (rmail-forget-messages)
		     (rmail-set-message-counters))))
520 521 522
      (switch-to-buffer 
       (let ((enable-local-variables nil))
	 (find-file-noselect file-name))))
523
    (if (eq major-mode 'rmail-edit-mode)
524
	(error "Exit Rmail Edit mode before getting new mail"))
Roland McGrath's avatar
Roland McGrath committed
525 526 527
    (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)
528 529 530
	    (progn (rmail-mode-2)
		   (setq run-mail-hook t)))
      (setq run-mail-hook t)
Roland McGrath's avatar
Roland McGrath committed
531 532 533
      (rmail-mode-2)
      ;; Convert all or part to Babyl file if possible.
      (rmail-convert-file)
534
      (goto-char (point-max)))
535 536 537 538 539
    ;; 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))
540 541
    ;; If necessary, scan to find all the messages.
    (rmail-maybe-set-message-counters)
542
    (unwind-protect
543 544 545
	(unless (and (not file-name-arg)
		     (rmail-get-new-mail))
	  (rmail-show-message (rmail-first-unseen-message)))
546 547 548 549 550
      (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
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568

;; 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.

569 570
;; This calls rmail-decode-babyl-format if the file is already Babyl.

Roland McGrath's avatar
Roland McGrath committed
571 572 573 574 575 576 577 578 579 580 581 582 583
(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")))
584 585 586
	  ((equal (point-min) (point-max))
	   ;; Empty RMAIL file.  Just insert the header.
	   (rmail-insert-rmail-file-header))
Roland McGrath's avatar
Roland McGrath committed
587
	  (t
588
	   ;; Non-empty file in non-RMAIL format.  Add header and convert.
Roland McGrath's avatar
Roland McGrath committed
589 590 591 592 593 594
	   (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
595 596
	    (save-excursion
	      (goto-char (point-max))
597 598
	      (search-backward "\n\^_")
	      (forward-char 2)
599
	      (looking-at "\n*From ")))
Roland McGrath's avatar
Roland McGrath committed
600 601
	(let ((buffer-read-only nil))
	  (message "Converting to Babyl format...")
602 603 604 605 606
	  ;; 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)
607
	  (narrow-to-region (point) (point-max))
Roland McGrath's avatar
Roland McGrath committed
608
	  (rmail-convert-to-babyl-format)
609 610 611 612 613
	  (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
614 615 616

(defun rmail-insert-rmail-file-header ()
  (let ((buffer-read-only nil))
617 618
    ;; -*-rmail-*- is here so that visiting the file normally
    ;; recognizes it as an Rmail file.
Roland McGrath's avatar
Roland McGrath committed
619 620 621 622 623 624 625
    (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\^_")))

626
;; Decode Babyl formated part at the head of current buffer by
627
;; rmail-file-coding-system, or if it is nil, do auto conversion.
628 629 630 631

(defun rmail-decode-babyl-format ()
  (let ((modifiedp (buffer-modified-p))
	(buffer-read-only nil)
632
	(coding-system rmail-file-coding-system)
633
	from to)
634
    (goto-char (point-min))
635
    (search-forward "\n\^_" nil t)	; Skip BABYL header.
636 637 638 639
    (setq from (point))
    (goto-char (point-max))
    (search-backward "\n\^_" from 'mv)
    (setq to (point))
640 641 642
    (unless (and coding-system
		 (coding-system-p coding-system))
      (setq coding-system (detect-coding-region from to t)))
643 644
    (unless (memq coding-system
		  '(undecided undecided-unix))
645 646 647 648 649 650
      (decode-coding-region from to coding-system)
      (setq coding-system last-coding-system-used))
    (set-buffer-modified-p modifiedp)
    (setq buffer-file-coding-system nil)
    (setq save-buffer-coding-system
	  (or coding-system 'undecided))))
651

652
(defvar rmail-mode-map nil)
Roland McGrath's avatar
Roland McGrath committed
653 654 655 656
(if rmail-mode-map
    nil
  (setq rmail-mode-map (make-keymap))
  (suppress-keymap rmail-mode-map)
657 658 659 660 661 662 663 664 665 666 667 668 669
  (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
670 671 672 673
  (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)
674 675 676 677 678 679 680 681 682 683 684 685 686
  (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)
687
;; I find I can't live without the default M-r command -- rms.
688 689 690 691 692
;;  (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)
693
  (define-key rmail-mode-map "w"      'rmail-output-body-to-file)
694 695 696 697 698 699 700
  (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)
701 702 703 704 705 706
  (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)
707
  (define-key rmail-mode-map "\C-c\C-s\C-k" 'rmail-sort-by-keywords)
708 709
  (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)
710
  )
711 712 713 714 715 716

(define-key rmail-mode-map [menu-bar] (make-sparse-keymap))

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

717
(define-key rmail-mode-map [menu-bar classify input-menu]
718
  nil)
719 720

(define-key rmail-mode-map [menu-bar classify output-menu]
721
  nil)
722

723 724 725
(define-key rmail-mode-map [menu-bar classify output-body]
  '("Output body to file..." . rmail-output-body-to-file))

726
(define-key rmail-mode-map [menu-bar classify output-inbox]
727
  '("Output (inbox)..." . rmail-output))
728 729

(define-key rmail-mode-map [menu-bar classify output]
730
  '("Output (Rmail)..." . rmail-output-to-rmail-file))
731 732

(define-key rmail-mode-map [menu-bar classify kill-label]
733
  '("Kill Label..." . rmail-kill-label))
734 735

(define-key rmail-mode-map [menu-bar classify add-label]
736
  '("Add Label..." . rmail-add-label))
737 738 739 740

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

741 742 743
(define-key rmail-mode-map [menu-bar summary senders]
  '("By Senders..." . rmail-summary-by-senders))

744
(define-key rmail-mode-map [menu-bar summary labels]
745
  '("By Labels..." . rmail-summary-by-labels))
746 747

(define-key rmail-mode-map [menu-bar summary recipients]
748
  '("By Recipients..." . rmail-summary-by-recipients))
749 750

(define-key rmail-mode-map [menu-bar summary topic]
751
  '("By Topic..." . rmail-summary-by-topic))
752 753

(define-key rmail-mode-map [menu-bar summary regexp]
754
  '("By Regexp..." . rmail-summary-by-regexp))
755 756 757 758 759 760

(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")))
761

762
(define-key rmail-mode-map [menu-bar mail rmail-get-new-mail]
763 764 765
  '("Get New Mail" . rmail-get-new-mail))

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

768 769 770
(define-key rmail-mode-map [menu-bar mail continue]
  '("Continue" . rmail-continue))

771
(define-key rmail-mode-map [menu-bar mail resend]
772
  '("Re-send..." . rmail-resend))
773

774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
(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
791

792 793 794 795 796 797 798 799 800 801 802 803 804
(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]
805
  '("Search Back..." . rmail-search-backwards))
806 807

(define-key rmail-mode-map [menu-bar move search]
808
  '("Search..." . rmail-search))
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827

(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
828 829 830
;; Rmail mode is suitable only for specially formatted data.
(put 'rmail-mode 'mode-class 'special)

831 832 833
(defun rmail-mode-kill-summary ()
  (if rmail-summary-buffer (kill-buffer rmail-summary-buffer)))

834
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
(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.
855
\\[rmail-edit-current-message]	Edit the current message.  \\[rmail-cease-edit] to return to Rmail.
Roland McGrath's avatar
Roland McGrath committed
856 857 858 859
\\[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.
860
\\[rmail-get-new-mail]	Move new mail from system spool directory into this file.
Roland McGrath's avatar
Roland McGrath committed
861 862
\\[rmail-mail]	Mail a message (same as \\[mail-other-window]).
\\[rmail-continue]	Continue composing outgoing message started before.
863 864
\\[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
865 866 867
\\[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).
Stephen Eglen's avatar
Stephen Eglen committed
868
\\[rmail-output-body-to-file]	Save message body to a file.  Default filename comes from Subject line.
Roland McGrath's avatar
Roland McGrath committed
869 870 871 872 873 874
\\[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.
875
          Any other label is present only if you add it with \\[rmail-add-label].
Roland McGrath's avatar
Roland McGrath committed
876 877
\\[rmail-previous-labeled-message]   Move to Previous message with specified label
\\[rmail-summary]	Show headers buffer, with a one line summary of each message.
878 879 880 881 882
\\[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
883 884 885
  (interactive)
  (rmail-mode-2)
  (rmail-set-message-counters)
886 887
  (rmail-show-message rmail-total-messages)
  (run-hooks 'rmail-mode-hook))
Roland McGrath's avatar
Roland McGrath committed
888 889 890 891

(defun rmail-mode-2 ()
  (kill-all-local-variables)
  (rmail-mode-1)
892
  (rmail-perm-variables)
893
  (rmail-variables))
Roland McGrath's avatar
Roland McGrath committed
894 895 896 897 898

(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
899 900 901 902 903
  ;; 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
904
  (setq buffer-auto-save-file-name nil)
905
  (setq mode-line-modified "--")
Roland McGrath's avatar
Roland McGrath committed
906 907 908 909
  (use-local-map rmail-mode-map)
  (set-syntax-table text-mode-syntax-table)
  (setq local-abbrev-table text-mode-abbrev-table))

910 911
;; Set up the permanent locals associated with an Rmail file.
(defun rmail-perm-variables ()
Roland McGrath's avatar
Roland McGrath committed
912
  (make-local-variable 'rmail-last-label)
913
  (make-local-variable 'rmail-last-regexp)
Roland McGrath's avatar
Roland McGrath committed
914
  (make-local-variable 'rmail-deleted-vector)
915 916 917 918
  (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
919 920 921 922
  (make-local-variable 'rmail-summary-buffer)
  (make-local-variable 'rmail-summary-vector)
  (make-local-variable 'rmail-current-message)
  (make-local-variable 'rmail-total-messages)
923 924
  (make-local-variable 'rmail-overlay-list)
  (setq rmail-overlay-list nil)
Roland McGrath's avatar
Roland McGrath committed
925
  (make-local-variable 'rmail-message-vector)
926
  (make-local-variable 'rmail-msgref-vector)
Roland McGrath's avatar
Roland McGrath committed
927 928
  (make-local-variable 'rmail-inbox-list)
  (setq rmail-inbox-list (rmail-parse-file-inboxes))
929 930
  ;; Provide default set of inboxes for primary mail file ~/RMAIL.
  (and (null rmail-inbox-list)
931
       (or (equal buffer-file-name (expand-file-name rmail-file-name))
932 933
	   (equal buffer-file-truename
		  (abbreviate-file-name (file-truename rmail-file-name))))
934 935 936 937
       (setq rmail-inbox-list
	     (or rmail-primary-inbox-list
		 (list (or (getenv "MAIL")
			   (concat rmail-spool-directory
938
				   (user-login-name)))))))
Roland McGrath's avatar
Roland McGrath committed
939 940
  (make-local-variable 'rmail-keywords)
  ;; this gets generated as needed
941
  (setq rmail-keywords nil))
Roland McGrath's avatar
Roland McGrath committed
942

943 944
;; Set up the non-permanent locals associated with Rmail mode.
(defun rmail-variables ()
945 946 947 948 949 950 951
  (make-local-variable 'save-buffer-coding-system)
  ;; If we don't already have a value for save-buffer-coding-system,
  ;; get it from buffer-file-coding-system, and clear that
  ;; because it should be determined in rmail-show-message.
  (unless save-buffer-coding-system
    (setq save-buffer-coding-system (or buffer-file-coding-system 'undecided))
    (setq buffer-file-coding-system nil))
952
  ;; Don't let a local variables list in a message cause confusion.
953 954
  (make-local-variable 'local-enable-local-variables)
  (setq local-enable-local-variables nil)
955 956 957 958
  (make-local-variable 'revert-buffer-function)
  (setq revert-buffer-function 'rmail-revert)
  (make-local-variable 'font-lock-defaults)
  (setq font-lock-defaults
959 960 961 962 963 964
	'(rmail-font-lock-keywords
	  t nil nil nil
	  (font-lock-maximum-size . nil)
	  (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))))
965 966 967 968 969 970 971 972 973
  (make-local-variable 'require-final-newline)
  (setq require-final-newline nil)
  (make-local-variable 'version-control)
  (setq version-control 'never)
  (make-local-variable 'kill-buffer-hook)
  (add-hook 'kill-buffer-hook 'rmail-mode-kill-summary)
  (make-local-variable 'file-precious-flag)
  (setq file-precious-flag t))

Roland McGrath's avatar
Roland McGrath committed
974 975
;; Handle M-x revert-buffer done in an rmail-mode buffer.
(defun rmail-revert (arg noconfirm)
976 977 978 979
  (let* ((revert-buffer-function (default-value 'revert-buffer-function))
	 (rmail-enable-multibyte enable-multibyte-characters)
	 ;; See similar code in `rmail'.
	 (coding-system-for-read (and rmail-enable-multibyte 'raw-text)))
Roland McGrath's avatar
Roland McGrath committed
980 981 982 983
    ;; 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.
984
	(progn
985 986
	  (rmail-mode-2)
	  ;; Convert all or part to Babyl file if possible.
Roland McGrath's avatar
Roland McGrath committed
987
	  (rmail-convert-file)
988 989 990 991 992
	  ;; We have read the file as raw-text, so the buffer is set to
	  ;; unibyte.  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
993
	  (goto-char (point-max))
994 995 996
	  (rmail-set-message-counters)
	  (rmail-show-message rmail-total-messages)
	  (run-hooks 'rmail-mode-hook)))))
Roland McGrath's avatar
Roland McGrath committed
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019

;; 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)
1020 1021 1022
  (save-buffer)
  (if (rmail-summary-exists)
      (rmail-select-summary (set-buffer-modified-p nil))))
Roland McGrath's avatar
Roland McGrath committed
1023 1024 1025 1026 1027 1028

(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.
1029 1030 1031
  (when rmail-summary-buffer
    (replace-buffer-in-windows rmail-summary-buffer)
    (bury-buffer rmail-summary-buffer))
Roland McGrath's avatar
Roland McGrath committed
1032
  (let ((obuf (current-buffer)))
1033 1034
    (quit-window)
    (replace-buffer-in-windows obuf)))
Roland McGrath's avatar
Roland McGrath committed
1035

1036 1037 1038 1039 1040 1041 1042 1043 1044
(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))
1045
	    (quit-window nil window))
1046
	  (bury-buffer rmail-summary-buffer)))
1047
    (quit-window)))
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064

(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
1065 1066
;;;###autoload
(defun rmail-input (filename)
1067
  "Run Rmail on file FILENAME."
Roland McGrath's avatar
Roland McGrath committed
1068 1069 1070
  (interactive "FRun rmail on RMAIL file: ")
  (rmail filename))

1071 1072 1073 1074 1075

;; 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.
1076
(defun rmail-find-all-files (start)
1077
  "Return list of file in dir START that match `rmail-secondary-file-regexp'."
1078
  (if (file-accessible-directory-p start)
1079
      ;; Don't sort here.
1080 1081
      (let* ((case-fold-search t)
	     (files (directory-files start t rmail-secondary-file-regexp)))
Richard M. Stallman's avatar