Some fixes to follow coding conventions in files maintained by FSF.
[emacs.git] / lisp / mail / rmail.el
blobe35932f64df8fa37ddb5d677130572cf189fe56e
1 ;;; rmail.el --- main code of "RMAIL" mail reader for Emacs
3 ;; Copyright (C) 1985,86,87,88,93,94,95,96,97,98,2000, 2001
4 ;; Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: mail
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
28 ;;; Code:
30 ;; Souped up by shane@mit-ajax based on ideas of rlk@athena.mit.edu
31 ;; New features include attribute and keyword support, message
32 ;; selection by dispatch table, summary by attributes and keywords,
33 ;; expunging by dispatch table, sticky options for file commands.
35 ;; Extended by Bob Weiner of Motorola
36 ;; New features include: rmail and rmail-summary buffers remain
37 ;; synchronized and key bindings basically operate the same way in both
38 ;; buffers, summary by topic or by regular expression, rmail-reply-prefix
39 ;; variable, and a bury rmail buffer (wipe) command.
42 (require 'mail-utils)
44 ; These variables now declared in paths.el.
45 ;(defvar rmail-spool-directory "/usr/spool/mail/"
46 ; "This is the name of the directory used by the system mailer for\n\
47 ;delivering new mail. Its name should end with a slash.")
48 ;(defvar rmail-file-name
49 ; (expand-file-name "~/RMAIL")
50 ; "")
52 (defgroup rmail nil
53 "Mail reader for Emacs."
54 :group 'mail)
56 (defgroup rmail-retrieve nil
57 "Rmail retrieval options."
58 :prefix "rmail-"
59 :group 'rmail)
61 (defgroup rmail-files nil
62 "Rmail files."
63 :prefix "rmail-"
64 :group 'rmail)
66 (defgroup rmail-headers nil
67 "Rmail header options."
68 :prefix "rmail-"
69 :group 'rmail)
71 (defgroup rmail-reply nil
72 "Rmail reply options."
73 :prefix "rmail-"
74 :group 'rmail)
76 (defgroup rmail-summary nil
77 "Rmail summary options."
78 :prefix "rmail-"
79 :prefix "rmail-summary-"
80 :group 'rmail)
82 (defgroup rmail-output nil
83 "Output message to a file."
84 :prefix "rmail-output-"
85 :prefix "rmail-"
86 :group 'rmail)
88 (defgroup rmail-edit nil
89 "Rmail editing."
90 :prefix "rmail-edit-"
91 :group 'rmail)
94 (defcustom rmail-movemail-program nil
95 "If non-nil, name of program for fetching new mail."
96 :group 'rmail-retrieve
97 :type '(choice (const nil) string))
99 (defcustom rmail-pop-password nil
100 "*Password to use when reading mail from a POP server, if required."
101 :type '(choice (string :tag "Password")
102 (const :tag "Not Required" nil))
103 :group 'rmail-retrieve)
105 (defcustom rmail-pop-password-required nil
106 "*Non-nil if a password is required when reading mail using POP."
107 :type 'boolean
108 :group 'rmail-retrieve)
110 (defcustom rmail-movemail-flags nil
111 "*List of flags to pass to movemail.
112 Most commonly used to specify `-g' to enable GSS-API authentication
113 or `-k' to enable Kerberos authentication."
114 :type '(repeat string)
115 :group 'rmail-retrieve
116 :version "20.3")
118 (defvar rmail-pop-password-error "invalid usercode or password\\|
119 unknown user name or bad password"
120 "Regular expression matching incorrect-password POP server error messages.
121 If you get an incorrect-password error that this expression does not match,
122 please report it with \\[report-emacs-bug].")
124 (defvar rmail-encoded-pop-password nil)
126 (defcustom rmail-preserve-inbox nil
127 "*Non-nil if incoming mail should be left in the user's inbox,
128 rather than deleted, after it is retrieved."
129 :type 'boolean
130 :group 'rmail-retrieve)
132 ;;;###autoload
133 (defcustom rmail-dont-reply-to-names nil "\
134 *A regexp specifying names to prune of reply to messages.
135 A value of nil means exclude your own login name as an address
136 plus whatever is specified by `rmail-default-dont-reply-to-names'."
137 :type '(choice regexp (const :tag "Your Name" nil))
138 :group 'rmail-reply)
140 ;;;###autoload
141 (defvar rmail-default-dont-reply-to-names "info-" "\
142 A regular expression specifying part of the value of the default value of
143 the variable `rmail-dont-reply-to-names', for when the user does not set
144 `rmail-dont-reply-to-names' explicitly. (The other part of the default
145 value is the user's name.)
146 It is useful to set this variable in the site customization file.")
148 ;;;###autoload
149 (defcustom rmail-ignored-headers
150 (concat "^via:\\|^mail-from:\\|^origin:\\|^references:"
151 "\\|^status:\\|^received:\\|^x400-originator:\\|^x400-recipients:"
152 "\\|^x400-received:\\|^x400-mts-identifier:\\|^x400-content-type:"
153 "\\|^\\(resent-\\|\\)message-id:\\|^summary-line:\\|^resent-date:"
154 "\\|^nntp-posting-host:\\|^path:\\|^x-char.*:\\|^x-face:"
155 "\\|^x-mailer:\\|^delivered-to:\\|^lines:\\|^mime-version:"
156 "\\|^content-transfer-encoding:\\|^x-coding-system:"
157 "\\|^return-path:\\|^errors-to:\\|^return-receipt-to:"
158 "\\|^x-sign:\\|^x-beenthere:\\|^x-mailman-version:"
159 "\\|^precedence:\\|^list-help:\\|^list-post:\\|^list-subscribe:"
160 "\\|^list-id:\\|^list-unsubscribe:\\|^list-archive:"
161 "\\|^content-type:\\|^content-length:"
162 "\\|^x-attribution:\\|^x-disclaimer:\\|^x-trace:"
163 "\\|^x-complaints-to:\\|^nntp-posting-date:\\|^user-agent:")
164 "*Regexp to match header fields that Rmail should normally hide.
165 This variable is used for reformatting the message header,
166 which normally happens once for each message,
167 when you view the message for the first time in Rmail.
168 To make a change in this variable take effect
169 for a message that you have already viewed,
170 go to that message and type \\[rmail-toggle-header] twice."
171 :type 'regexp
172 :group 'rmail-headers)
174 ;;;###autoload
175 (defcustom rmail-displayed-headers nil
176 "*Regexp to match Header fields that Rmail should display.
177 If nil, display all header fields except those matched by
178 `rmail-ignored-headers'."
179 :type '(choice regexp (const :tag "All"))
180 :group 'rmail-headers)
182 ;;;###autoload
183 (defcustom rmail-retry-ignored-headers "^x-authentication-warning:" "\
184 *Headers that should be stripped when retrying a failed message."
185 :type '(choice regexp (const nil :tag "None"))
186 :group 'rmail-headers)
188 ;;;###autoload
189 (defcustom rmail-highlighted-headers "^From:\\|^Subject:" "\
190 *Regexp to match Header fields that Rmail should normally highlight.
191 A value of nil means don't highlight.
192 See also `rmail-highlight-face'."
193 :type 'regexp
194 :group 'rmail-headers)
196 ;;;###autoload
197 (defcustom rmail-highlight-face nil "\
198 *Face used by Rmail for highlighting headers."
199 :type '(choice (const :tag "Default" nil)
200 face)
201 :group 'rmail-headers)
203 ;;;###autoload
204 (defcustom rmail-delete-after-output nil "\
205 *Non-nil means automatically delete a message that is copied to a file."
206 :type 'boolean
207 :group 'rmail-files)
209 ;;;###autoload
210 (defcustom rmail-primary-inbox-list nil "\
211 *List of files which are inboxes for user's primary mail file `~/RMAIL'.
212 `nil' means the default, which is (\"/usr/spool/mail/$USER\")
213 \(the name varies depending on the operating system,
214 and the value of the environment variable MAIL overrides it)."
215 ;; Don't use backquote here, because we don't want to need it
216 ;; at load time.
217 :type (list 'choice '(const :tag "Default" nil)
218 (list 'repeat ':value (list (or (getenv "MAIL")
219 (concat "/var/spool/mail/"
220 (getenv "USER"))))
221 'file))
222 :group 'rmail-retrieve
223 :group 'rmail-files)
225 ;;;###autoload
226 (defcustom rmail-mail-new-frame nil
227 "*Non-nil means Rmail makes a new frame for composing outgoing mail."
228 :type 'boolean
229 :group 'rmail-reply)
231 ;;;###autoload
232 (defcustom rmail-secondary-file-directory "~/"
233 "*Directory for additional secondary Rmail files."
234 :type 'directory
235 :group 'rmail-files)
236 ;;;###autoload
237 (defcustom rmail-secondary-file-regexp "\\.xmail$"
238 "*Regexp for which files are secondary Rmail files."
239 :type 'regexp
240 :group 'rmail-files)
242 ;;;###autoload
243 (defcustom rmail-confirm-expunge 'y-or-n-p
244 "*Whether and how to ask for confirmation before expunging deleted messages."
245 :type '(choice (const :tag "No confirmation" nil)
246 (const :tag "Confirm with y-or-n-p" y-or-n-p)
247 (const :tag "Confirm with yes-or-no-p" yes-or-no-p))
248 :version "21.1"
249 :group 'rmail-files)
251 ;;;###autoload
252 (defvar rmail-mode-hook nil
253 "List of functions to call when Rmail is invoked.")
255 ;;;###autoload
256 (defvar rmail-get-new-mail-hook nil
257 "List of functions to call when Rmail has retrieved new mail.")
259 ;;;###autoload
260 (defcustom rmail-show-message-hook nil
261 "List of functions to call when Rmail displays a message."
262 :type 'hook
263 :options '(goto-addr)
264 :group 'rmail)
266 ;;;###autoload
267 (defvar rmail-quit-hook nil
268 "List of functions to call when quitting out of Rmail.")
270 ;;;###autoload
271 (defvar rmail-delete-message-hook nil
272 "List of functions to call when Rmail deletes a message.
273 When the hooks are called, the message has been marked deleted but is
274 still the current message in the Rmail buffer.")
276 ;; These may be altered by site-init.el to match the format of mmdf files
277 ;; delimiting used on a given host (delim1 and delim2 from the config
278 ;; files).
280 (defvar rmail-mmdf-delim1 "^\001\001\001\001\n"
281 "Regexp marking the start of an mmdf message")
282 (defvar rmail-mmdf-delim2 "^\001\001\001\001\n"
283 "Regexp marking the end of an mmdf message")
285 (defcustom rmail-message-filter nil
286 "If non-nil, a filter function for new messages in RMAIL.
287 Called with region narrowed to the message, including headers,
288 before obeying `rmail-ignored-headers'."
289 :group 'rmail-headers
290 :type 'function)
292 (defcustom rmail-automatic-folder-directives nil
293 "List of directives specifying where to put a message.
294 Each element of the list is of the form:
296 (FOLDERNAME FIELD REGEXP [ FIELD REGEXP ] ... )
298 Where FOLDERNAME is the name of a BABYL format folder to put the
299 message. If any of the field regexp's are nil, then it is ignored.
301 If FOLDERNAME is \"/dev/null\", it is deleted.
302 If FOLDERNAME is nil then it is deleted, and skipped.
304 FIELD is the plain text name of a field in the message, such as
305 \"subject\" or \"from\". A FIELD of \"to\" will automatically include
306 all text from the \"cc\" field as well.
308 REGEXP is an expression to match in the preceeding specified FIELD.
309 FIELD/REGEXP pairs continue in the list.
311 examples:
312 (\"/dev/null\" \"from\" \"@spam.com\") ; delete all mail from spam.com
313 (\"RMS\" \"from\" \"rms@\") ; save all mail from RMS."
314 :group 'rmail
315 :version "21.1"
316 :type '(repeat (sexp :tag "Directive")))
318 (defvar rmail-reply-prefix "Re: "
319 "String to prepend to Subject line when replying to a message.")
321 ;; Some mailers use "Re(2):" or "Re^2:" or "Re: Re:" or "Re[2]:".
322 ;; This pattern should catch all the common variants.
323 (defvar rmail-reply-regexp "\\`\\(Re\\(([0-9]+)\\|\\[[0-9]+\\]\\|\\^[0-9]+\\)?: *\\)*"
324 "Regexp to delete from Subject line before inserting `rmail-reply-prefix'.")
326 (defcustom rmail-display-summary nil
327 "*If non-nil, Rmail always displays the summary buffer."
328 :group 'rmail-summary
329 :type 'boolean)
331 (defvar rmail-inbox-list nil)
332 (put 'rmail-inbox-list 'permanent-local t)
334 (defvar rmail-keywords nil)
335 (put 'rmail-keywords 'permanent-local t)
337 (defvar rmail-buffer nil
338 "The RMAIL buffer related to the current buffer.
339 In an RMAIL buffer, this holds the RMAIL buffer itself.
340 In a summary buffer, this holds the RMAIL buffer it is a summary for.")
341 (put 'rmail-buffer 'permanent-local t)
343 ;; Message counters and markers. Deleted flags.
345 (defvar rmail-current-message nil)
346 (put 'rmail-current-message 'permanent-local t)
348 (defvar rmail-total-messages nil)
349 (put 'rmail-total-messages 'permanent-local t)
351 (defvar rmail-message-vector nil)
352 (put 'rmail-message-vector 'permanent-local t)
354 (defvar rmail-deleted-vector nil)
355 (put 'rmail-deleted-vector 'permanent-local t)
357 (defvar rmail-msgref-vector nil
358 "In an Rmail buffer, a vector whose Nth element is a list (N).
359 When expunging renumbers messages, these lists are modified
360 by substituting the new message number into the existing list.")
361 (put 'rmail-msgref-vector 'permanent-local t)
363 (defvar rmail-overlay-list nil)
364 (put 'rmail-overlay-list 'permanent-local t)
366 ;; These are used by autoloaded rmail-summary.
368 (defvar rmail-summary-buffer nil)
369 (put 'rmail-summary-buffer 'permanent-local t)
370 (defvar rmail-summary-vector nil)
371 (put 'rmail-summary-vector 'permanent-local t)
373 (defvar rmail-view-buffer nil
374 "Buffer which holds RMAIL message for MIME displaying.")
375 (put 'rmail-view-buffer 'permanent-local t)
377 ;; `Sticky' default variables.
379 ;; Last individual label specified to a or k.
380 (defvar rmail-last-label nil)
381 (put 'rmail-last-label 'permanent-local t)
383 ;; Last set of values specified to C-M-n, C-M-p, C-M-s or C-M-l.
384 (defvar rmail-last-multi-labels nil)
386 (defvar rmail-last-regexp nil)
387 (put 'rmail-last-regexp 'permanent-local t)
389 (defcustom rmail-default-file "~/xmail"
390 "*Default file name for \\[rmail-output]."
391 :type 'file
392 :group 'rmail-files)
393 (defcustom rmail-default-rmail-file "~/XMAIL"
394 "*Default file name for \\[rmail-output-to-rmail-file]."
395 :type 'file
396 :group 'rmail-files)
397 (defcustom rmail-default-body-file "~/mailout"
398 "*Default file name for \\[rmail-output-body-to-file]."
399 :type 'file
400 :group 'rmail-files
401 :version "20.3")
403 ;; Mule and MIME related variables.
405 ;;;###autoload
406 (defvar rmail-file-coding-system nil
407 "Coding system used in RMAIL file.
409 This is set to nil by default.")
411 ;;;###autoload
412 (defcustom rmail-enable-mime nil
413 "*If non-nil, RMAIL uses MIME feature.
414 If the value is t, RMAIL automatically shows MIME decoded message.
415 If the value is neither t nor nil, RMAIL does not show MIME decoded message
416 until a user explicitly requires it."
417 :type '(choice (const :tag "on" t)
418 (const :tag "off" nil)
419 (other :tag "when asked" ask))
420 :group 'rmail)
422 ;;;###autoload
423 (defvar rmail-show-mime-function nil
424 "Function to show MIME decoded message of RMAIL file.
425 This function is called when `rmail-enable-mime' is non-nil.
426 It is called with no argument.")
428 ;;;###autoload
429 (defvar rmail-insert-mime-forwarded-message-function nil
430 "Function to insert a message in MIME format so it can be forwarded.
431 This function is called if `rmail-enable-mime' is non-nil.
432 It is called with one argument FORWARD-BUFFER, which is a
433 buffer containing the message to forward. The current buffer
434 is the outgoing mail buffer.")
436 ;;;###autoload
437 (defvar rmail-insert-mime-resent-message-function nil
438 "Function to insert a message in MIME format so it can be resent.
439 This function is called if `rmail-enable-mime' is non-nil.
440 It is called with one argument FORWARD-BUFFER, which is a
441 buffer containing the message to forward. The current buffer
442 is the outgoing mail buffer.")
444 ;;;###autoload
445 (defvar rmail-search-mime-message-function nil
446 "Function to check if a regexp matches a MIME message.
447 This function is called if `rmail-enable-mime' is non-nil.
448 It is called with two arguments MSG and REGEXP, where
449 MSG is the message number, REGEXP is the regular expression.")
451 ;;;###autoload
452 (defvar rmail-search-mime-header-function nil
453 "Function to check if a regexp matches a header of MIME message.
454 This function is called if `rmail-enable-mime' is non-nil.
455 It is called with four arguments MSG, REGEXP, and LIMIT, where
456 MSG is the message number,
457 REGEXP is the regular expression,
458 LIMIT is the position specifying the end of header.")
460 ;;;###autoload
461 (defvar rmail-mime-feature 'rmail-mime
462 "Feature to require to load MIME support in Rmail.
463 When starting Rmail, if `rmail-enable-mime' is non-nil,
464 this feature is required with `require'.")
466 ;;;###autoload
467 (defvar rmail-decode-mime-charset t
468 "*Non-nil means a message is decoded by MIME's charset specification.
469 If this variable is nil, or the message has not MIME specification,
470 the message is decoded as normal way.
472 If the variable `rmail-enable-mime' is non-nil, this variables is
473 ignored, and all the decoding work is done by a feature specified by
474 the variable `rmail-mime-feature'.")
476 ;;;###autoload
477 (defvar rmail-mime-charset-pattern
478 "^content-type:[ ]*text/plain;[ \t\n]*charset=\"?\\([^ \t\n\"]+\\)\"?"
479 "Regexp to match MIME-charset specification in a header of message.
480 The first parenthesized expression should match the MIME-charset name.")
483 ;;; Regexp matching the delimiter of messages in UNIX mail format
484 ;;; (UNIX From lines), minus the initial ^. Note that if you change
485 ;;; this expression, you must change the code in rmail-nuke-pinhead-header
486 ;;; that knows the exact ordering of the \\( \\) subexpressions.
487 (defvar rmail-unix-mail-delimiter
488 (let ((time-zone-regexp
489 (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
490 "\\|[-+]?[0-9][0-9][0-9][0-9]"
491 "\\|"
492 "\\) *")))
493 (concat
494 "From "
496 ;; Many things can happen to an RFC 822 mailbox before it is put into
497 ;; a `From' line. The leading phrase can be stripped, e.g.
498 ;; `Joe <@w.x:joe@y.z>' -> `<@w.x:joe@y.z>'. The <> can be stripped, e.g.
499 ;; `<@x.y:joe@y.z>' -> `@x.y:joe@y.z'. Everything starting with a CRLF
500 ;; can be removed, e.g.
501 ;; From: joe@y.z (Joe K
502 ;; User)
503 ;; can yield `From joe@y.z (Joe K Fri Mar 22 08:11:15 1996', and
504 ;; From: Joe User
505 ;; <joe@y.z>
506 ;; can yield `From Joe User Fri Mar 22 08:11:15 1996'.
507 ;; The mailbox can be removed or be replaced by white space, e.g.
508 ;; From: "Joe User"{space}{tab}
509 ;; <joe@y.z>
510 ;; can yield `From {space}{tab} Fri Mar 22 08:11:15 1996',
511 ;; where {space} and {tab} represent the Ascii space and tab characters.
512 ;; We want to match the results of any of these manglings.
513 ;; The following regexp rejects names whose first characters are
514 ;; obviously bogus, but after that anything goes.
515 "\\([^\0-\b\n-\r\^?].*\\)? "
517 ;; The time the message was sent.
518 "\\([^\0-\r \^?]+\\) +" ; day of the week
519 "\\([^\0-\r \^?]+\\) +" ; month
520 "\\([0-3]?[0-9]\\) +" ; day of month
521 "\\([0-2][0-9]:[0-5][0-9]\\(:[0-6][0-9]\\)?\\) *" ; time of day
523 ;; Perhaps a time zone, specified by an abbreviation, or by a
524 ;; numeric offset.
525 time-zone-regexp
527 ;; The year.
528 " \\([0-9][0-9]+\\) *"
530 ;; On some systems the time zone can appear after the year, too.
531 time-zone-regexp
533 ;; Old uucp cruft.
534 "\\(remote from .*\\)?"
536 "\n"))
537 nil)
539 (defvar rmail-font-lock-keywords
540 (eval-when-compile
541 (let* ((cite-chars "[>|}]")
542 (cite-prefix "A-Za-z")
543 (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
544 (list '("^\\(From\\|Sender\\):" . font-lock-function-name-face)
545 '("^Reply-To:.*$" . font-lock-function-name-face)
546 '("^Subject:" . font-lock-comment-face)
547 '("^\\(To\\|Apparently-To\\|Cc\\|Newsgroups\\):"
548 . font-lock-keyword-face)
549 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
550 `(,cite-chars
551 (,(concat "\\=[ \t]*"
552 "\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
553 "\\(" cite-chars "[ \t]*\\)\\)+"
554 "\\(.*\\)")
555 (beginning-of-line) (end-of-line)
556 (2 font-lock-constant-face nil t)
557 (4 font-lock-comment-face nil t)))
558 '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\|Date\\):.*$"
559 . font-lock-string-face))))
560 "Additional expressions to highlight in Rmail mode.")
562 ;; Perform BODY in the summary buffer
563 ;; in such a way that its cursor is properly updated in its own window.
564 (defmacro rmail-select-summary (&rest body)
565 `(let ((total rmail-total-messages))
566 (if (rmail-summary-displayed)
567 (let ((window (selected-window)))
568 (save-excursion
569 (unwind-protect
570 (progn
571 (pop-to-buffer rmail-summary-buffer)
572 ;; rmail-total-messages is a buffer-local var
573 ;; in the rmail buffer.
574 ;; This way we make it available for the body
575 ;; even tho the rmail buffer is not current.
576 (let ((rmail-total-messages total))
577 ,@body))
578 (select-window window))))
579 (save-excursion
580 (set-buffer rmail-summary-buffer)
581 (let ((rmail-total-messages total))
582 ,@body)))
583 (rmail-maybe-display-summary)))
585 ;;;; *** Rmail Mode ***
587 ;; This variable is dynamically bound. The defvar is here to placate
588 ;; the byte compiler.
590 (defvar rmail-enable-multibyte nil)
593 (defun rmail-require-mime-maybe ()
594 "Require `rmail-mime-feature' if that is non-nil.
595 Signal an error and set `rmail-mime-feature' to nil if the feature
596 isn't provided."
597 (when rmail-enable-mime
598 (condition-case err
599 (require rmail-mime-feature)
600 (error
601 (message "Feature `%s' not provided" rmail-mime-feature)
602 (sit-for 1)
603 (setq rmail-enable-mime nil)))))
606 ;;;###autoload
607 (defun rmail (&optional file-name-arg)
608 "Read and edit incoming mail.
609 Moves messages into file named by `rmail-file-name' (a babyl format file)
610 and edits that file in RMAIL Mode.
611 Type \\[describe-mode] once editing that file, for a list of RMAIL commands.
613 May be called with file name as argument; then performs rmail editing on
614 that file, but does not copy any new mail into the file.
615 Interactively, if you supply a prefix argument, then you
616 have a chance to specify a file name with the minibuffer.
618 If `rmail-display-summary' is non-nil, make a summary for this RMAIL file."
619 (interactive (if current-prefix-arg
620 (list (read-file-name "Run rmail on RMAIL file: "))))
621 (rmail-require-mime-maybe)
622 (let* ((file-name (expand-file-name (or file-name-arg rmail-file-name)))
623 (existed (get-file-buffer file-name))
624 ;; This binding is necessary because we much decide if we
625 ;; need code conversion while the buffer is unibyte
626 ;; (i.e. enable-multibyte-characters is nil).
627 (rmail-enable-multibyte
628 (if existed
629 (with-current-buffer existed enable-multibyte-characters)
630 (default-value 'enable-multibyte-characters)))
631 ;; Since the file may contain messages of different encodings
632 ;; at the tail (non-BYBYL part), we can't decode them at once
633 ;; on reading. So, at first, we read the file without text
634 ;; code conversion, then decode the messages one by one by
635 ;; rmail-decode-babyl-format or
636 ;; rmail-convert-to-babyl-format.
637 (coding-system-for-read (and rmail-enable-multibyte 'raw-text))
638 run-mail-hook msg-shown)
639 ;; Like find-file, but in the case where a buffer existed
640 ;; and the file was reverted, recompute the message-data.
641 ;; We used to bind enable-local-variables to nil here,
642 ;; but that should not be needed now that rmail-mode
643 ;; sets it locally to nil.
644 ;; (Binding a variable locally with let is not safe if it has
645 ;; buffer-local bindings.)
646 (if (and existed (not (verify-visited-file-modtime existed)))
647 (progn
648 (find-file file-name)
649 (if (and (verify-visited-file-modtime existed)
650 (eq major-mode 'rmail-mode))
651 (progn (rmail-forget-messages)
652 (rmail-set-message-counters))))
653 (switch-to-buffer
654 (let ((enable-local-variables nil))
655 (find-file-noselect file-name))))
656 (if (eq major-mode 'rmail-edit-mode)
657 (error "Exit Rmail Edit mode before getting new mail"))
658 (if (and existed (> (buffer-size) 0))
659 ;; Buffer not new and not empty; ensure in proper mode, but that's all.
660 (or (eq major-mode 'rmail-mode)
661 (progn (rmail-mode-2)
662 (setq run-mail-hook t)))
663 (setq run-mail-hook t)
664 (rmail-mode-2)
665 ;; Convert all or part to Babyl file if possible.
666 (rmail-convert-file)
667 (goto-char (point-max)))
668 ;; As we have read a file by raw-text, the buffer is set to
669 ;; unibyte. We must make it multibyte if necessary.
670 (if (and rmail-enable-multibyte
671 (not enable-multibyte-characters))
672 (set-buffer-multibyte t))
673 ;; If necessary, scan to find all the messages.
674 (rmail-maybe-set-message-counters)
675 (unwind-protect
676 (unless (and (not file-name-arg)
677 (rmail-get-new-mail))
678 (rmail-show-message (rmail-first-unseen-message)))
679 (progn
680 (if rmail-display-summary (rmail-summary))
681 (rmail-construct-io-menu)
682 (if run-mail-hook
683 (run-hooks 'rmail-mode-hook))))))
685 ;; Given the value of MAILPATH, return a list of inbox file names.
686 ;; This is turned off because it is not clear that the user wants
687 ;; all these inboxes to feed into the primary rmail file.
688 ; (defun rmail-convert-mailpath (string)
689 ; (let (idx list)
690 ; (while (setq idx (string-match "[%:]" string))
691 ; (let ((this (substring string 0 idx)))
692 ; (setq string (substring string (1+ idx)))
693 ; (setq list (cons (if (string-match "%" this)
694 ; (substring this 0 (string-match "%" this))
695 ; this)
696 ; list))))
697 ; list))
699 ; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
700 ; will not cause emacs 18.55 problems.
702 ;; This calls rmail-decode-babyl-format if the file is already Babyl.
704 (defun rmail-convert-file ()
705 (let (convert)
706 (widen)
707 (goto-char (point-min))
708 ;; If file doesn't start like a Babyl file,
709 ;; convert it to one, by adding a header and converting each message.
710 (cond ((looking-at "BABYL OPTIONS:"))
711 ((looking-at "Version: 5\n")
712 ;; Losing babyl file made by old version of Rmail.
713 ;; Just fix the babyl file header; don't make a new one,
714 ;; so we don't lose the Labels: file attribute, etc.
715 (let ((buffer-read-only nil))
716 (insert "BABYL OPTIONS: -*- rmail -*-\n")))
717 ((equal (point-min) (point-max))
718 ;; Empty RMAIL file. Just insert the header.
719 (rmail-insert-rmail-file-header))
721 ;; Non-empty file in non-RMAIL format. Add header and convert.
722 (setq convert t)
723 (rmail-insert-rmail-file-header)))
724 ;; If file was not a Babyl file or if there are
725 ;; Unix format messages added at the end,
726 ;; convert file as necessary.
727 (if (or convert
728 (save-excursion
729 (goto-char (point-max))
730 (search-backward "\n\^_")
731 (forward-char 2)
732 (looking-at "\n*From ")))
733 (let ((buffer-read-only nil))
734 (message "Converting to Babyl format...")
735 ;; If file needs conversion, convert it all,
736 ;; except for the BABYL header.
737 ;; (rmail-convert-to-babyl-format would delete the header.)
738 (goto-char (point-min))
739 (search-forward "\n\^_" nil t)
740 (narrow-to-region (point) (point-max))
741 (rmail-convert-to-babyl-format)
742 (message "Converting to Babyl format...done"))
743 (if (and (not rmail-enable-mime)
744 rmail-enable-multibyte)
745 ;; We still have to decode BABYL part.
746 (rmail-decode-babyl-format)))))
748 (defun rmail-insert-rmail-file-header ()
749 (let ((buffer-read-only nil))
750 ;; -*-rmail-*- is here so that visiting the file normally
751 ;; recognizes it as an Rmail file.
752 (insert "BABYL OPTIONS: -*- rmail -*-
753 Version: 5
754 Labels:
755 Note: This is the header of an rmail file.
756 Note: If you are seeing it in rmail,
757 Note: it means the file has no messages in it.\n\^_")))
759 ;; Decode Babyl formated part at the head of current buffer by
760 ;; rmail-file-coding-system, or if it is nil, do auto conversion.
762 (defun rmail-decode-babyl-format ()
763 (let ((modifiedp (buffer-modified-p))
764 (buffer-read-only nil)
765 (coding-system rmail-file-coding-system)
766 from to)
767 (goto-char (point-min))
768 (search-forward "\n\^_" nil t) ; Skip BABYL header.
769 (setq from (point))
770 (goto-char (point-max))
771 (search-backward "\n\^_" from 'mv)
772 (setq to (point))
773 (unless (and coding-system
774 (coding-system-p coding-system))
775 (setq coding-system (detect-coding-region from to t)))
776 (unless (memq coding-system
777 '(undecided undecided-unix))
778 (set-buffer-modified-p t) ; avoid locking when decoding
779 (decode-coding-region from to coding-system)
780 (setq coding-system last-coding-system-used))
781 (set-buffer-modified-p modifiedp)
782 (setq buffer-file-coding-system nil)
783 (setq save-buffer-coding-system
784 (or coding-system 'undecided))))
786 (defvar rmail-mode-map nil)
787 (if rmail-mode-map
789 (setq rmail-mode-map (make-keymap))
790 (suppress-keymap rmail-mode-map)
791 (define-key rmail-mode-map "a" 'rmail-add-label)
792 (define-key rmail-mode-map "b" 'rmail-bury)
793 (define-key rmail-mode-map "c" 'rmail-continue)
794 (define-key rmail-mode-map "d" 'rmail-delete-forward)
795 (define-key rmail-mode-map "\C-d" 'rmail-delete-backward)
796 (define-key rmail-mode-map "e" 'rmail-edit-current-message)
797 (define-key rmail-mode-map "f" 'rmail-forward)
798 (define-key rmail-mode-map "g" 'rmail-get-new-mail)
799 (define-key rmail-mode-map "h" 'rmail-summary)
800 (define-key rmail-mode-map "i" 'rmail-input)
801 (define-key rmail-mode-map "j" 'rmail-show-message)
802 (define-key rmail-mode-map "k" 'rmail-kill-label)
803 (define-key rmail-mode-map "l" 'rmail-summary-by-labels)
804 (define-key rmail-mode-map "\e\C-h" 'rmail-summary)
805 (define-key rmail-mode-map "\e\C-l" 'rmail-summary-by-labels)
806 (define-key rmail-mode-map "\e\C-r" 'rmail-summary-by-recipients)
807 (define-key rmail-mode-map "\e\C-s" 'rmail-summary-by-regexp)
808 (define-key rmail-mode-map "\e\C-t" 'rmail-summary-by-topic)
809 (define-key rmail-mode-map "m" 'rmail-mail)
810 (define-key rmail-mode-map "\em" 'rmail-retry-failure)
811 (define-key rmail-mode-map "n" 'rmail-next-undeleted-message)
812 (define-key rmail-mode-map "\en" 'rmail-next-message)
813 (define-key rmail-mode-map "\e\C-n" 'rmail-next-labeled-message)
814 (define-key rmail-mode-map "o" 'rmail-output-to-rmail-file)
815 (define-key rmail-mode-map "\C-o" 'rmail-output)
816 (define-key rmail-mode-map "p" 'rmail-previous-undeleted-message)
817 (define-key rmail-mode-map "\ep" 'rmail-previous-message)
818 (define-key rmail-mode-map "\e\C-p" 'rmail-previous-labeled-message)
819 (define-key rmail-mode-map "q" 'rmail-quit)
820 (define-key rmail-mode-map "r" 'rmail-reply)
821 ;; I find I can't live without the default M-r command -- rms.
822 ;; (define-key rmail-mode-map "\er" 'rmail-search-backwards)
823 (define-key rmail-mode-map "s" 'rmail-expunge-and-save)
824 (define-key rmail-mode-map "\es" 'rmail-search)
825 (define-key rmail-mode-map "t" 'rmail-toggle-header)
826 (define-key rmail-mode-map "u" 'rmail-undelete-previous-message)
827 (define-key rmail-mode-map "w" 'rmail-output-body-to-file)
828 (define-key rmail-mode-map "x" 'rmail-expunge)
829 (define-key rmail-mode-map "." 'rmail-beginning-of-message)
830 (define-key rmail-mode-map "<" 'rmail-first-message)
831 (define-key rmail-mode-map ">" 'rmail-last-message)
832 (define-key rmail-mode-map " " 'scroll-up)
833 (define-key rmail-mode-map "\177" 'scroll-down)
834 (define-key rmail-mode-map "?" 'describe-mode)
835 (define-key rmail-mode-map "\C-c\C-s\C-d" 'rmail-sort-by-date)
836 (define-key rmail-mode-map "\C-c\C-s\C-s" 'rmail-sort-by-subject)
837 (define-key rmail-mode-map "\C-c\C-s\C-a" 'rmail-sort-by-author)
838 (define-key rmail-mode-map "\C-c\C-s\C-r" 'rmail-sort-by-recipient)
839 (define-key rmail-mode-map "\C-c\C-s\C-c" 'rmail-sort-by-correspondent)
840 (define-key rmail-mode-map "\C-c\C-s\C-l" 'rmail-sort-by-lines)
841 (define-key rmail-mode-map "\C-c\C-s\C-k" 'rmail-sort-by-labels)
842 (define-key rmail-mode-map "\C-c\C-n" 'rmail-next-same-subject)
843 (define-key rmail-mode-map "\C-c\C-p" 'rmail-previous-same-subject)
846 (define-key rmail-mode-map [menu-bar] (make-sparse-keymap))
848 (define-key rmail-mode-map [menu-bar classify]
849 (cons "Classify" (make-sparse-keymap "Classify")))
851 (define-key rmail-mode-map [menu-bar classify input-menu]
852 nil)
854 (define-key rmail-mode-map [menu-bar classify output-menu]
855 nil)
857 (define-key rmail-mode-map [menu-bar classify output-body]
858 '("Output body to file..." . rmail-output-body-to-file))
860 (define-key rmail-mode-map [menu-bar classify output-inbox]
861 '("Output (inbox)..." . rmail-output))
863 (define-key rmail-mode-map [menu-bar classify output]
864 '("Output (Rmail)..." . rmail-output-to-rmail-file))
866 (define-key rmail-mode-map [menu-bar classify kill-label]
867 '("Kill Label..." . rmail-kill-label))
869 (define-key rmail-mode-map [menu-bar classify add-label]
870 '("Add Label..." . rmail-add-label))
872 (define-key rmail-mode-map [menu-bar summary]
873 (cons "Summary" (make-sparse-keymap "Summary")))
875 (define-key rmail-mode-map [menu-bar summary senders]
876 '("By Senders..." . rmail-summary-by-senders))
878 (define-key rmail-mode-map [menu-bar summary labels]
879 '("By Labels..." . rmail-summary-by-labels))
881 (define-key rmail-mode-map [menu-bar summary recipients]
882 '("By Recipients..." . rmail-summary-by-recipients))
884 (define-key rmail-mode-map [menu-bar summary topic]
885 '("By Topic..." . rmail-summary-by-topic))
887 (define-key rmail-mode-map [menu-bar summary regexp]
888 '("By Regexp..." . rmail-summary-by-regexp))
890 (define-key rmail-mode-map [menu-bar summary all]
891 '("All" . rmail-summary))
893 (define-key rmail-mode-map [menu-bar mail]
894 (cons "Mail" (make-sparse-keymap "Mail")))
896 (define-key rmail-mode-map [menu-bar mail rmail-get-new-mail]
897 '("Get New Mail" . rmail-get-new-mail))
899 (define-key rmail-mode-map [menu-bar mail lambda]
900 '("----"))
902 (define-key rmail-mode-map [menu-bar mail continue]
903 '("Continue" . rmail-continue))
905 (define-key rmail-mode-map [menu-bar mail resend]
906 '("Re-send..." . rmail-resend))
908 (define-key rmail-mode-map [menu-bar mail forward]
909 '("Forward" . rmail-forward))
911 (define-key rmail-mode-map [menu-bar mail retry]
912 '("Retry" . rmail-retry-failure))
914 (define-key rmail-mode-map [menu-bar mail reply]
915 '("Reply" . rmail-reply))
917 (define-key rmail-mode-map [menu-bar mail mail]
918 '("Mail" . rmail-mail))
920 (define-key rmail-mode-map [menu-bar delete]
921 (cons "Delete" (make-sparse-keymap "Delete")))
923 (define-key rmail-mode-map [menu-bar delete expunge/save]
924 '("Expunge/Save" . rmail-expunge-and-save))
926 (define-key rmail-mode-map [menu-bar delete expunge]
927 '("Expunge" . rmail-expunge))
929 (define-key rmail-mode-map [menu-bar delete undelete]
930 '("Undelete" . rmail-undelete-previous-message))
932 (define-key rmail-mode-map [menu-bar delete delete]
933 '("Delete" . rmail-delete-forward))
935 (define-key rmail-mode-map [menu-bar move]
936 (cons "Move" (make-sparse-keymap "Move")))
938 (define-key rmail-mode-map [menu-bar move search-back]
939 '("Search Back..." . rmail-search-backwards))
941 (define-key rmail-mode-map [menu-bar move search]
942 '("Search..." . rmail-search))
944 (define-key rmail-mode-map [menu-bar move previous]
945 '("Previous Nondeleted" . rmail-previous-undeleted-message))
947 (define-key rmail-mode-map [menu-bar move next]
948 '("Next Nondeleted" . rmail-next-undeleted-message))
950 (define-key rmail-mode-map [menu-bar move last]
951 '("Last" . rmail-last-message))
953 (define-key rmail-mode-map [menu-bar move first]
954 '("First" . rmail-first-message))
956 (define-key rmail-mode-map [menu-bar move previous]
957 '("Previous" . rmail-previous-message))
959 (define-key rmail-mode-map [menu-bar move next]
960 '("Next" . rmail-next-message))
962 ;; Rmail mode is suitable only for specially formatted data.
963 (put 'rmail-mode 'mode-class 'special)
965 (defun rmail-mode-kill-summary ()
966 (if rmail-summary-buffer (kill-buffer rmail-summary-buffer)))
968 ;;;###autoload
969 (defun rmail-mode ()
970 "Rmail Mode is used by \\<rmail-mode-map>\\[rmail] for editing Rmail files.
971 All normal editing commands are turned off.
972 Instead, these commands are available:
974 \\[rmail-beginning-of-message] Move point to front of this message (same as \\[beginning-of-buffer]).
975 \\[scroll-up] Scroll to next screen of this message.
976 \\[scroll-down] Scroll to previous screen of this message.
977 \\[rmail-next-undeleted-message] Move to Next non-deleted message.
978 \\[rmail-previous-undeleted-message] Move to Previous non-deleted message.
979 \\[rmail-next-message] Move to Next message whether deleted or not.
980 \\[rmail-previous-message] Move to Previous message whether deleted or not.
981 \\[rmail-first-message] Move to the first message in Rmail file.
982 \\[rmail-last-message] Move to the last message in Rmail file.
983 \\[rmail-show-message] Jump to message specified by numeric position in file.
984 \\[rmail-search] Search for string and show message it is found in.
985 \\[rmail-delete-forward] Delete this message, move to next nondeleted.
986 \\[rmail-delete-backward] Delete this message, move to previous nondeleted.
987 \\[rmail-undelete-previous-message] Undelete message. Tries current message, then earlier messages
988 till a deleted message is found.
989 \\[rmail-edit-current-message] Edit the current message. \\[rmail-cease-edit] to return to Rmail.
990 \\[rmail-expunge] Expunge deleted messages.
991 \\[rmail-expunge-and-save] Expunge and save the file.
992 \\[rmail-quit] Quit Rmail: expunge, save, then switch to another buffer.
993 \\[save-buffer] Save without expunging.
994 \\[rmail-get-new-mail] Move new mail from system spool directory into this file.
995 \\[rmail-mail] Mail a message (same as \\[mail-other-window]).
996 \\[rmail-continue] Continue composing outgoing message started before.
997 \\[rmail-reply] Reply to this message. Like \\[rmail-mail] but initializes some fields.
998 \\[rmail-retry-failure] Send this message again. Used on a mailer failure message.
999 \\[rmail-forward] Forward this message to another user.
1000 \\[rmail-output-to-rmail-file] Output this message to an Rmail file (append it).
1001 \\[rmail-output] Output this message to a Unix-format mail file (append it).
1002 \\[rmail-output-body-to-file] Save message body to a file. Default filename comes from Subject line.
1003 \\[rmail-input] Input Rmail file. Run Rmail on that file.
1004 \\[rmail-add-label] Add label to message. It will be displayed in the mode line.
1005 \\[rmail-kill-label] Kill label. Remove a label from current message.
1006 \\[rmail-next-labeled-message] Move to Next message with specified label
1007 (label defaults to last one specified).
1008 Standard labels: filed, unseen, answered, forwarded, deleted.
1009 Any other label is present only if you add it with \\[rmail-add-label].
1010 \\[rmail-previous-labeled-message] Move to Previous message with specified label
1011 \\[rmail-summary] Show headers buffer, with a one line summary of each message.
1012 \\[rmail-summary-by-labels] Summarize only messages with particular label(s).
1013 \\[rmail-summary-by-recipients] Summarize only messages with particular recipient(s).
1014 \\[rmail-summary-by-regexp] Summarize only messages with particular regexp(s).
1015 \\[rmail-summary-by-topic] Summarize only messages with subject line regexp(s).
1016 \\[rmail-toggle-header] Toggle display of complete header."
1017 (interactive)
1018 (let ((finding-rmail-file (not (eq major-mode 'rmail-mode))))
1019 (rmail-mode-2)
1020 (when (and finding-rmail-file
1021 (null coding-system-for-read)
1022 default-enable-multibyte-characters)
1023 (let ((rmail-enable-multibyte t))
1024 (rmail-require-mime-maybe)
1025 (rmail-convert-file)
1026 (goto-char (point-max))
1027 (set-buffer-multibyte t)))
1028 (rmail-set-message-counters)
1029 (rmail-show-message rmail-total-messages)
1030 (when finding-rmail-file
1031 (when rmail-display-summary
1032 (rmail-summary))
1033 (rmail-construct-io-menu))
1034 (run-hooks 'rmail-mode-hook)))
1036 (defun rmail-mode-2 ()
1037 (kill-all-local-variables)
1038 (rmail-mode-1)
1039 (rmail-perm-variables)
1040 (rmail-variables))
1042 (defun rmail-mode-1 ()
1043 (setq major-mode 'rmail-mode)
1044 (setq mode-name "RMAIL")
1045 (setq buffer-read-only t)
1046 ;; No need to auto save RMAIL files in normal circumstances
1047 ;; because they contain no info except attribute changes
1048 ;; and deletion of messages.
1049 ;; The one exception is when messages are copied into an Rmail mode buffer.
1050 ;; rmail-output-to-rmail-file enables auto save when you do that.
1051 (setq buffer-auto-save-file-name nil)
1052 (setq mode-line-modified "--")
1053 (use-local-map rmail-mode-map)
1054 (set-syntax-table text-mode-syntax-table)
1055 (setq local-abbrev-table text-mode-abbrev-table))
1057 ;; Set up the permanent locals associated with an Rmail file.
1058 (defun rmail-perm-variables ()
1059 (make-local-variable 'rmail-last-label)
1060 (make-local-variable 'rmail-last-regexp)
1061 (make-local-variable 'rmail-deleted-vector)
1062 (make-local-variable 'rmail-buffer)
1063 (setq rmail-buffer (current-buffer))
1064 (make-local-variable 'rmail-view-buffer)
1065 (setq rmail-view-buffer rmail-buffer)
1066 (make-local-variable 'rmail-summary-buffer)
1067 (make-local-variable 'rmail-summary-vector)
1068 (make-local-variable 'rmail-current-message)
1069 (make-local-variable 'rmail-total-messages)
1070 (make-local-variable 'rmail-overlay-list)
1071 (setq rmail-overlay-list nil)
1072 (make-local-variable 'rmail-message-vector)
1073 (make-local-variable 'rmail-msgref-vector)
1074 (make-local-variable 'rmail-inbox-list)
1075 (setq rmail-inbox-list (rmail-parse-file-inboxes))
1076 ;; Provide default set of inboxes for primary mail file ~/RMAIL.
1077 (and (null rmail-inbox-list)
1078 (or (equal buffer-file-name (expand-file-name rmail-file-name))
1079 (equal buffer-file-truename
1080 (abbreviate-file-name (file-truename rmail-file-name))))
1081 (setq rmail-inbox-list
1082 (or rmail-primary-inbox-list
1083 (list (or (getenv "MAIL")
1084 (concat rmail-spool-directory
1085 (user-login-name)))))))
1086 (make-local-variable 'rmail-keywords)
1087 ;; this gets generated as needed
1088 (setq rmail-keywords nil))
1090 ;; Set up the non-permanent locals associated with Rmail mode.
1091 (defun rmail-variables ()
1092 (make-local-variable 'save-buffer-coding-system)
1093 ;; If we don't already have a value for save-buffer-coding-system,
1094 ;; get it from buffer-file-coding-system, and clear that
1095 ;; because it should be determined in rmail-show-message.
1096 (unless save-buffer-coding-system
1097 (setq save-buffer-coding-system (or buffer-file-coding-system 'undecided))
1098 (setq buffer-file-coding-system nil))
1099 ;; Don't let a local variables list in a message cause confusion.
1100 (make-local-variable 'local-enable-local-variables)
1101 (setq local-enable-local-variables nil)
1102 (make-local-variable 'revert-buffer-function)
1103 (setq revert-buffer-function 'rmail-revert)
1104 (make-local-variable 'font-lock-defaults)
1105 (setq font-lock-defaults
1106 '(rmail-font-lock-keywords
1107 t nil nil nil
1108 (font-lock-maximum-size . nil)
1109 (font-lock-fontify-buffer-function . rmail-fontify-buffer-function)
1110 (font-lock-unfontify-buffer-function . rmail-unfontify-buffer-function)
1111 (font-lock-inhibit-thing-lock . (lazy-lock-mode fast-lock-mode))))
1112 (make-local-variable 'require-final-newline)
1113 (setq require-final-newline nil)
1114 (make-local-variable 'version-control)
1115 (setq version-control 'never)
1116 (make-local-variable 'kill-buffer-hook)
1117 (add-hook 'kill-buffer-hook 'rmail-mode-kill-summary)
1118 (make-local-variable 'file-precious-flag)
1119 (setq file-precious-flag t))
1121 ;; Handle M-x revert-buffer done in an rmail-mode buffer.
1122 (defun rmail-revert (arg noconfirm)
1123 (set-buffer rmail-buffer)
1124 (let* ((revert-buffer-function (default-value 'revert-buffer-function))
1125 (rmail-enable-multibyte enable-multibyte-characters)
1126 ;; See similar code in `rmail'.
1127 (coding-system-for-read (and rmail-enable-multibyte 'raw-text)))
1128 ;; Call our caller again, but this time it does the default thing.
1129 (if (revert-buffer arg noconfirm)
1130 ;; If the user said "yes", and we changed something,
1131 ;; reparse the messages.
1132 (progn
1133 (set-buffer rmail-buffer)
1134 (rmail-mode-2)
1135 ;; Convert all or part to Babyl file if possible.
1136 (rmail-convert-file)
1137 ;; We have read the file as raw-text, so the buffer is set to
1138 ;; unibyte. Make it multibyte if necessary.
1139 (if (and rmail-enable-multibyte
1140 (not enable-multibyte-characters))
1141 (set-buffer-multibyte t))
1142 (goto-char (point-max))
1143 (rmail-set-message-counters)
1144 (rmail-show-message rmail-total-messages)
1145 (run-hooks 'rmail-mode-hook)))))
1147 ;; Return a list of files from this buffer's Mail: option.
1148 ;; Does not assume that messages have been parsed.
1149 ;; Just returns nil if buffer does not look like Babyl format.
1150 (defun rmail-parse-file-inboxes ()
1151 (save-excursion
1152 (save-restriction
1153 (widen)
1154 (goto-char 1)
1155 (cond ((looking-at "BABYL OPTIONS:")
1156 (search-forward "\n\^_" nil 'move)
1157 (narrow-to-region 1 (point))
1158 (goto-char 1)
1159 (if (search-forward "\nMail:" nil t)
1160 (progn
1161 (narrow-to-region (point) (progn (end-of-line) (point)))
1162 (goto-char (point-min))
1163 (mail-parse-comma-list))))))))
1165 (defun rmail-expunge-and-save ()
1166 "Expunge and save RMAIL file."
1167 (interactive)
1168 (rmail-expunge)
1169 (set-buffer rmail-buffer)
1170 (save-buffer)
1171 (if (rmail-summary-exists)
1172 (rmail-select-summary (set-buffer-modified-p nil))))
1174 (defun rmail-quit ()
1175 "Quit out of RMAIL.
1176 Hook `rmail-quit-hook' is run after expunging."
1177 (interactive)
1178 (rmail-expunge-and-save)
1179 (when (boundp 'rmail-quit-hook)
1180 (run-hooks 'rmail-quit-hook))
1181 ;; Don't switch to the summary buffer even if it was recently visible.
1182 (when rmail-summary-buffer
1183 (replace-buffer-in-windows rmail-summary-buffer)
1184 (bury-buffer rmail-summary-buffer))
1185 (if rmail-enable-mime
1186 (let ((obuf rmail-buffer)
1187 (ovbuf rmail-view-buffer))
1188 (set-buffer rmail-view-buffer)
1189 (quit-window)
1190 (replace-buffer-in-windows ovbuf)
1191 (replace-buffer-in-windows obuf)
1192 (bury-buffer obuf))
1193 (let ((obuf (current-buffer)))
1194 (quit-window)
1195 (replace-buffer-in-windows obuf))))
1197 (defun rmail-bury ()
1198 "Bury current Rmail buffer and its summary buffer."
1199 (interactive)
1200 ;; This let var was called rmail-buffer, but that interfered
1201 ;; with the buffer-local var used in summary buffers.
1202 (let ((buffer-to-bury (current-buffer)))
1203 (if (rmail-summary-exists)
1204 (let (window)
1205 (while (setq window (get-buffer-window rmail-summary-buffer))
1206 (quit-window nil window))
1207 (bury-buffer rmail-summary-buffer)))
1208 (quit-window)))
1210 (defun rmail-duplicate-message ()
1211 "Create a duplicated copy of the current message.
1212 The duplicate copy goes into the Rmail file just after the
1213 original copy."
1214 (interactive)
1215 (widen)
1216 (let ((buffer-read-only nil)
1217 (number rmail-current-message)
1218 (string (buffer-substring (rmail-msgbeg rmail-current-message)
1219 (rmail-msgend rmail-current-message))))
1220 (goto-char (rmail-msgend rmail-current-message))
1221 (insert string)
1222 (rmail-forget-messages)
1223 (rmail-show-message number)
1224 (message "Message duplicated")))
1226 ;;;###autoload
1227 (defun rmail-input (filename)
1228 "Run Rmail on file FILENAME."
1229 (interactive "FRun rmail on RMAIL file: ")
1230 (rmail filename))
1233 ;; This used to scan subdirectories recursively, but someone pointed out
1234 ;; that if the user wants that, person can put all the files in one dir.
1235 ;; And the recursive scan was slow. So I took it out.
1236 ;; rms, Sep 1996.
1237 (defun rmail-find-all-files (start)
1238 "Return list of file in dir START that match `rmail-secondary-file-regexp'."
1239 (if (file-accessible-directory-p start)
1240 ;; Don't sort here.
1241 (let* ((case-fold-search t)
1242 (files (directory-files start t rmail-secondary-file-regexp)))
1243 ;; Sort here instead of in directory-files
1244 ;; because this list is usually much shorter.
1245 (sort files 'string<))))
1247 (defun rmail-list-to-menu (menu-name l action &optional full-name)
1248 (let ((menu (make-sparse-keymap menu-name)))
1249 (mapcar
1250 (function (lambda (item)
1251 (let (command)
1252 (if (consp item)
1253 (progn
1254 (setq command
1255 (rmail-list-to-menu (car item) (cdr item)
1256 action
1257 (if full-name
1258 (concat full-name "/"
1259 (car item))
1260 (car item))))
1261 (setq name (car item)))
1262 (progn
1263 (setq name item)
1264 (setq command
1265 (list 'lambda () '(interactive)
1266 (list action
1267 (expand-file-name
1268 (if full-name
1269 (concat full-name "/" item)
1270 item)
1271 rmail-secondary-file-directory))))))
1272 (define-key menu (vector (intern name))
1273 (cons name command)))))
1274 (reverse l))
1275 menu))
1277 ;; This command is always "disabled" when it appears in a menu.
1278 (put 'rmail-disable-menu 'menu-enable ''nil)
1280 (defun rmail-construct-io-menu ()
1281 (let ((files (rmail-find-all-files rmail-secondary-file-directory)))
1282 (if files
1283 (progn
1284 (define-key rmail-mode-map [menu-bar classify input-menu]
1285 (cons "Input Rmail File"
1286 (rmail-list-to-menu "Input Rmail File"
1287 files
1288 'rmail-input)))
1289 (define-key rmail-mode-map [menu-bar classify output-menu]
1290 (cons "Output Rmail File"
1291 (rmail-list-to-menu "Output Rmail File"
1292 files
1293 'rmail-output-to-rmail-file))))
1295 (define-key rmail-mode-map [menu-bar classify input-menu]
1296 '("Input Rmail File" . rmail-disable-menu))
1297 (define-key rmail-mode-map [menu-bar classify output-menu]
1298 '("Output Rmail File" . rmail-disable-menu)))))
1301 ;;;; *** Rmail input ***
1303 ;; RLK feature not added in this version:
1304 ;; argument specifies inbox file or files in various ways.
1306 (defun rmail-get-new-mail (&optional file-name)
1307 "Move any new mail from this RMAIL file's inbox files.
1308 The inbox files can be specified with the file's Mail: option. The
1309 variable `rmail-primary-inbox-list' specifies the inboxes for your
1310 primary RMAIL file if it has no Mail: option. By default, this is
1311 your /usr/spool/mail/$USER.
1313 You can also specify the file to get new mail from. In this case, the
1314 file of new mail is not changed or deleted. Noninteractively, you can
1315 pass the inbox file name as an argument. Interactively, a prefix
1316 argument causes us to read a file name and use that file as the inbox.
1318 If the variable `rmail-preserve-inbox' is non-nil, new mail will
1319 always be left in inbox files rather than deleted.
1321 This function runs `rmail-get-new-mail-hook' before saving the updated file.
1322 It returns t if it got any new messages."
1323 (interactive
1324 (list (if current-prefix-arg
1325 (read-file-name "Get new mail from file: "))))
1326 (run-hooks 'rmail-before-get-new-mail-hook)
1327 ;; If the disk file has been changed from under us,
1328 ;; revert to it before we get new mail.
1329 (or (verify-visited-file-modtime (current-buffer))
1330 (find-file (buffer-file-name)))
1331 (set-buffer rmail-buffer)
1332 (rmail-maybe-set-message-counters)
1333 (widen)
1334 ;; Get rid of all undo records for this buffer.
1335 (or (eq buffer-undo-list t)
1336 (setq buffer-undo-list nil))
1337 (let ((all-files (if file-name (list file-name)
1338 rmail-inbox-list))
1339 (rmail-enable-multibyte (default-value 'enable-multibyte-characters))
1340 found)
1341 (unwind-protect
1342 (progn
1343 (while all-files
1344 (let ((opoint (point))
1345 (new-messages 0)
1346 (delete-files ())
1347 ;; If buffer has not changed yet, and has not been saved yet,
1348 ;; don't replace the old backup file now.
1349 (make-backup-files (and make-backup-files (buffer-modified-p)))
1350 (buffer-read-only nil)
1351 ;; Don't make undo records for what we do in getting mail.
1352 (buffer-undo-list t)
1353 success
1354 ;; Files to insert this time around.
1355 files
1356 ;; Last names of those files.
1357 file-last-names)
1358 ;; Pull files off all-files onto files
1359 ;; as long as there is no name conflict.
1360 ;; A conflict happens when two inbox file names
1361 ;; have the same last component.
1362 (while (and all-files
1363 (not (member (file-name-nondirectory (car all-files))
1364 file-last-names)))
1365 (setq files (cons (car all-files) files)
1366 file-last-names
1367 (cons (file-name-nondirectory (car all-files)) files))
1368 (setq all-files (cdr all-files)))
1369 ;; Put them back in their original order.
1370 (setq files (nreverse files))
1372 (goto-char (point-max))
1373 (skip-chars-backward " \t\n") ; just in case of brain damage
1374 (delete-region (point) (point-max)) ; caused by require-final-newline
1375 (save-excursion
1376 (save-restriction
1377 (narrow-to-region (point) (point))
1378 ;; Read in the contents of the inbox files,
1379 ;; renaming them as necessary,
1380 ;; and adding to the list of files to delete eventually.
1381 (if file-name
1382 (rmail-insert-inbox-text files nil)
1383 (setq delete-files (rmail-insert-inbox-text files t)))
1384 ;; Scan the new text and convert each message to babyl format.
1385 (goto-char (point-min))
1386 (unwind-protect
1387 (save-excursion
1388 (setq new-messages (rmail-convert-to-babyl-format)
1389 success t))
1390 ;; Try to delete the garbage just inserted.
1391 (or success (delete-region (point-min) (point-max)))
1392 ;; If we could not convert the file's inboxes,
1393 ;; rename the files we tried to read
1394 ;; so we won't over and over again.
1395 (if (and (not file-name) (not success))
1396 (let ((delfiles delete-files)
1397 (count 0))
1398 (while delfiles
1399 (while (file-exists-p (format "RMAILOSE.%d" count))
1400 (setq count (1+ count)))
1401 (rename-file (car delfiles)
1402 (format "RMAILOSE.%d" count))
1403 (setq delfiles (cdr delfiles))))))
1404 (or (zerop new-messages)
1405 (let (success)
1406 (widen)
1407 (search-backward "\n\^_" nil t)
1408 (narrow-to-region (point) (point-max))
1409 (goto-char (1+ (point-min)))
1410 (rmail-count-new-messages)
1411 (run-hooks 'rmail-get-new-mail-hook)
1412 (save-buffer)))
1413 ;; Delete the old files, now that babyl file is saved.
1414 (while delete-files
1415 (condition-case ()
1416 ;; First, try deleting.
1417 (condition-case ()
1418 (delete-file (car delete-files))
1419 (file-error
1420 ;; If we can't delete it, truncate it.
1421 (write-region (point) (point) (car delete-files))))
1422 (file-error nil))
1423 (setq delete-files (cdr delete-files)))))
1424 (if (= new-messages 0)
1425 (progn (goto-char opoint)
1426 (if (or file-name rmail-inbox-list)
1427 (message "(No new mail has arrived)")))
1428 (if (rmail-summary-exists)
1429 (rmail-select-summary
1430 (rmail-update-summary)))
1431 (message "%d new message%s read"
1432 new-messages (if (= 1 new-messages) "" "s"))
1433 ;; Move to the first new message
1434 ;; unless we have other unseen messages before it.
1435 (rmail-show-message (rmail-first-unseen-message))
1436 (run-hooks 'rmail-after-get-new-mail-hook)
1437 (setq found t))))
1438 found)
1439 ;; Don't leave the buffer screwed up if we get a disk-full error.
1440 (or found (rmail-show-message)))))
1442 (defun rmail-insert-inbox-text (files renamep)
1443 ;; Detect a locked file now, so that we avoid moving mail
1444 ;; out of the real inbox file. (That could scare people.)
1445 (or (memq (file-locked-p buffer-file-name) '(nil t))
1446 (error "RMAIL file %s is locked"
1447 (file-name-nondirectory buffer-file-name)))
1448 (let (file tofile delete-files movemail popmail got-password password)
1449 (while files
1450 ;; Handle POP mailbox names specially; don't expand as filenames
1451 ;; in case the userid contains a directory separator.
1452 (setq file (car files))
1453 (setq popmail (string-match "^po:" file))
1454 (if popmail
1455 (setq renamep t)
1456 (setq file (file-truename
1457 (substitute-in-file-name (expand-file-name file)))))
1458 (setq tofile (expand-file-name
1459 ;; Generate name to move to from inbox name,
1460 ;; in case of multiple inboxes that need moving.
1461 (concat ".newmail-" (file-name-nondirectory file))
1462 ;; Use the directory of this rmail file
1463 ;; because it's a nuisance to use the homedir
1464 ;; if that is on a full disk and this rmail
1465 ;; file isn't.
1466 (file-name-directory
1467 (expand-file-name buffer-file-name))))
1468 ;; Always use movemail to rename the file,
1469 ;; since there can be mailboxes in various directories.
1470 (setq movemail t)
1471 ;;; ;; If getting from mail spool directory,
1472 ;;; ;; use movemail to move rather than just renaming,
1473 ;;; ;; so as to interlock with the mailer.
1474 ;;; (setq movemail (string= file
1475 ;;; (file-truename
1476 ;;; (concat rmail-spool-directory
1477 ;;; (file-name-nondirectory file)))))
1478 (if (and movemail (not popmail))
1479 (progn
1480 ;; On some systems, /usr/spool/mail/foo is a directory
1481 ;; and the actual inbox is /usr/spool/mail/foo/foo.
1482 (if (file-directory-p file)
1483 (setq file (expand-file-name (user-login-name)
1484 file)))))
1485 (cond (popmail
1486 (if rmail-pop-password-required
1487 (progn (setq got-password (not (rmail-have-password)))
1488 (setq password (rmail-get-pop-password))))
1489 (if (eq system-type 'windows-nt)
1490 ;; cannot have "po:" in file name
1491 (setq tofile
1492 (expand-file-name
1493 (concat ".newmail-pop-"
1494 (file-name-nondirectory (substring file 3)))
1495 (file-name-directory
1496 (expand-file-name buffer-file-name)))))
1497 (message "Getting mail from post office ..."))
1498 ((and (file-exists-p tofile)
1499 (/= 0 (nth 7 (file-attributes tofile))))
1500 (message "Getting mail from %s..." tofile))
1501 ((and (file-exists-p file)
1502 (/= 0 (nth 7 (file-attributes file))))
1503 (message "Getting mail from %s..." file)))
1504 ;; Set TOFILE if have not already done so, and
1505 ;; rename or copy the file FILE to TOFILE if and as appropriate.
1506 (cond ((not renamep)
1507 (setq tofile file))
1508 ((or (file-exists-p tofile) (and (not popmail)
1509 (not (file-exists-p file))))
1510 nil)
1511 ((and (not movemail) (not popmail))
1512 ;; Try copying. If that fails (perhaps no space) and
1513 ;; we're allowed to blow away the inbox, rename instead.
1514 (if rmail-preserve-inbox
1515 (copy-file file tofile nil)
1516 (condition-case nil
1517 (copy-file file tofile nil)
1518 (error
1519 ;; Third arg is t so we can replace existing file TOFILE.
1520 (rename-file file tofile t))))
1521 ;; Make the real inbox file empty.
1522 ;; Leaving it deleted could cause lossage
1523 ;; because mailers often won't create the file.
1524 (if (not rmail-preserve-inbox)
1525 (condition-case ()
1526 (write-region (point) (point) file)
1527 (file-error nil))))
1529 (let ((errors nil))
1530 (unwind-protect
1531 (save-excursion
1532 (setq errors (generate-new-buffer " *rmail loss*"))
1533 (buffer-disable-undo errors)
1534 (let ((args
1535 (append
1536 (list (or rmail-movemail-program
1537 (expand-file-name "movemail"
1538 exec-directory))
1539 nil errors nil)
1540 (if rmail-preserve-inbox
1541 (list "-p")
1542 nil)
1543 rmail-movemail-flags
1544 (list file tofile)
1545 (if password (list password) nil))))
1546 (apply 'call-process args))
1547 (if (not (buffer-modified-p errors))
1548 ;; No output => movemail won
1550 (set-buffer errors)
1551 (subst-char-in-region (point-min) (point-max)
1552 ?\n ?\ )
1553 (goto-char (point-max))
1554 (skip-chars-backward " \t")
1555 (delete-region (point) (point-max))
1556 (goto-char (point-min))
1557 (if (looking-at "movemail: ")
1558 (delete-region (point-min) (match-end 0)))
1559 (beep t)
1560 (message "movemail: %s"
1561 (buffer-substring (point-min)
1562 (point-max)))
1563 ;; If we just read the password, most likely it is
1564 ;; wrong. Otherwise, see if there is a specific
1565 ;; reason to think that the problem is a wrong passwd.
1566 (if (or got-password
1567 (re-search-forward rmail-pop-password-error
1568 nil t))
1569 (rmail-set-pop-password nil))
1570 (sit-for 3)
1571 nil))
1572 (if errors (kill-buffer errors))))))
1573 ;; At this point, TOFILE contains the name to read:
1574 ;; Either the alternate name (if we renamed)
1575 ;; or the actual inbox (if not renaming).
1576 (if (file-exists-p tofile)
1577 (let ((coding-system-for-read 'no-conversion)
1578 size)
1579 (goto-char (point-max))
1580 (setq size (nth 1 (insert-file-contents tofile)))
1581 (goto-char (point-max))
1582 (or (= (preceding-char) ?\n)
1583 (zerop size)
1584 (insert ?\n))
1585 (if (not (and rmail-preserve-inbox (string= file tofile)))
1586 (setq delete-files (cons tofile delete-files)))))
1587 (message "")
1588 (setq files (cdr files)))
1589 delete-files))
1591 ;; Decode the region specified by FROM and TO by CODING.
1592 ;; If CODING is nil or an invalid coding system, decode by `undecided'.
1593 (defun rmail-decode-region (from to coding)
1594 (if (or (not coding) (not (coding-system-p coding)))
1595 (setq coding 'undecided))
1596 (decode-coding-region from to coding))
1598 ;; the rmail-break-forwarded-messages feature is not implemented
1599 (defun rmail-convert-to-babyl-format ()
1600 (let ((count 0) start
1601 (case-fold-search nil)
1602 (invalid-input-resync
1603 (function (lambda ()
1604 (message "Invalid Babyl format in inbox!")
1605 (sit-for 3)
1606 ;; Try to get back in sync with a real message.
1607 (if (re-search-forward
1608 (concat rmail-mmdf-delim1 "\\|^From") nil t)
1609 (beginning-of-line)
1610 (goto-char (point-max)))))))
1611 (goto-char (point-min))
1612 (save-restriction
1613 (while (not (eobp))
1614 (setq start (point))
1615 (cond ((looking-at "BABYL OPTIONS:");Babyl header
1616 (if (search-forward "\n\^_" nil t)
1617 ;; If we find the proper terminator, delete through there.
1618 (delete-region (point-min) (point))
1619 (funcall invalid-input-resync)
1620 (delete-region (point-min) (point))))
1621 ;; Babyl format message
1622 ((looking-at "\^L")
1623 (or (search-forward "\n\^_" nil t)
1624 (funcall invalid-input-resync))
1625 (setq count (1+ count))
1626 ;; Make sure there is no extra white space after the ^_
1627 ;; at the end of the message.
1628 ;; Narrowing will make sure that whatever follows the junk
1629 ;; will be treated properly.
1630 (delete-region (point)
1631 (save-excursion
1632 (skip-chars-forward " \t\n")
1633 (point)))
1634 (setq last-coding-system-used nil)
1635 (or rmail-enable-mime
1636 (not rmail-enable-multibyte)
1637 (decode-coding-region start (point)
1638 (or rmail-file-coding-system
1639 'undecided)))
1640 ;; Add an X-Coding-System: header if we don't have one.
1641 (save-excursion
1642 (goto-char start)
1643 (forward-line 1)
1644 (if (looking-at "0")
1645 (forward-line 1)
1646 (forward-line 2))
1647 (or (save-restriction
1648 (narrow-to-region (point) (point-max))
1649 (rfc822-goto-eoh)
1650 (goto-char (point-min))
1651 (re-search-forward "^X-Coding-System:" nil t))
1652 (insert "X-Coding-System: "
1653 (symbol-name last-coding-system-used)
1654 "\n")))
1655 (narrow-to-region (point) (point-max)))
1656 ;;*** MMDF format
1657 ((let ((case-fold-search t))
1658 (looking-at rmail-mmdf-delim1))
1659 (let ((case-fold-search t))
1660 (replace-match "\^L\n0, unseen,,\n*** EOOH ***\n")
1661 (re-search-forward rmail-mmdf-delim2 nil t)
1662 (replace-match "\^_"))
1663 (save-excursion
1664 (save-restriction
1665 (narrow-to-region start (1- (point)))
1666 (goto-char (point-min))
1667 (while (search-forward "\n\^_" nil t); single char "\^_"
1668 (replace-match "\n^_")))); 2 chars: "^" and "_"
1669 (setq last-coding-system-used nil)
1670 (or rmail-enable-mime
1671 (not rmail-enable-multibyte)
1672 (decode-coding-region start (point) 'undecided))
1673 (save-excursion
1674 (goto-char start)
1675 (forward-line 3)
1676 (insert "X-Coding-System: "
1677 (symbol-name last-coding-system-used)
1678 "\n"))
1679 (narrow-to-region (point) (point-max))
1680 (setq count (1+ count)))
1681 ;;*** Mail format
1682 ((looking-at "^From ")
1683 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
1684 (rmail-nuke-pinhead-header)
1685 ;; If this message has a Content-Length field,
1686 ;; skip to the end of the contents.
1687 (let* ((header-end (save-excursion
1688 (and (re-search-forward "\n\n" nil t)
1689 (1- (point)))))
1690 (case-fold-search t)
1691 (quoted-printable-header-field-end
1692 (save-excursion
1693 (re-search-forward
1694 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
1695 header-end t)))
1696 (size
1697 ;; Get the numeric value from the Content-Length field.
1698 (save-excursion
1699 ;; Back up to end of prev line,
1700 ;; in case the Content-Length field comes first.
1701 (forward-char -1)
1702 (and (search-forward "\ncontent-length: "
1703 header-end t)
1704 (let ((beg (point))
1705 (eol (progn (end-of-line) (point))))
1706 (string-to-int (buffer-substring beg eol)))))))
1707 (and size
1708 (if (and (natnump size)
1709 (<= (+ header-end size) (point-max))
1710 ;; Make sure this would put us at a position
1711 ;; that we could continue from.
1712 (save-excursion
1713 (goto-char (+ header-end size))
1714 (skip-chars-forward "\n")
1715 (or (eobp)
1716 (and (looking-at "BABYL OPTIONS:")
1717 (search-forward "\n\^_" nil t))
1718 (and (looking-at "\^L")
1719 (search-forward "\n\^_" nil t))
1720 (let ((case-fold-search t))
1721 (looking-at rmail-mmdf-delim1))
1722 (looking-at "From "))))
1723 (goto-char (+ header-end size))
1724 (message "Ignoring invalid Content-Length field")
1725 (sit-for 1 0 t)))
1726 (if (re-search-forward
1727 (concat "^[\^_]?\\("
1728 rmail-unix-mail-delimiter
1729 "\\|"
1730 rmail-mmdf-delim1 "\\|"
1731 "^BABYL OPTIONS:\\|"
1732 "\^L\n[01],\\)") nil t)
1733 (goto-char (match-beginning 1))
1734 (goto-char (point-max)))
1735 (setq count (1+ count))
1736 (if quoted-printable-header-field-end
1737 (save-excursion
1738 (rmail-decode-quoted-printable header-end (point))
1739 ;; Change "quoted-printable" to "8bit",
1740 ;; to reflect the decoding we just did.
1741 (goto-char quoted-printable-header-field-end)
1742 (delete-region (point) (search-backward ":"))
1743 (insert ": 8bit"))))
1745 (save-excursion
1746 (save-restriction
1747 (narrow-to-region start (point))
1748 (goto-char (point-min))
1749 (while (search-forward "\n\^_" nil t); single char
1750 (replace-match "\n^_")))); 2 chars: "^" and "_"
1751 (insert ?\^_)
1752 (setq last-coding-system-used nil)
1753 (or rmail-enable-mime
1754 (not rmail-enable-multibyte)
1755 (let ((mime-charset
1756 (if (and rmail-decode-mime-charset
1757 (save-excursion
1758 (goto-char start)
1759 (search-forward "\n\n" nil t)
1760 (let ((case-fold-search t))
1761 (re-search-backward
1762 rmail-mime-charset-pattern
1763 start t))))
1764 (intern (downcase (match-string 1))))))
1765 (rmail-decode-region start (point) mime-charset)))
1766 (save-excursion
1767 (goto-char start)
1768 (forward-line 3)
1769 (insert "X-Coding-System: "
1770 (symbol-name last-coding-system-used)
1771 "\n"))
1772 (narrow-to-region (point) (point-max)))
1774 ;; This kludge is because some versions of sendmail.el
1775 ;; insert an extra newline at the beginning that shouldn't
1776 ;; be there. sendmail.el has been fixed, but old versions
1777 ;; may still be in use. -- rms, 7 May 1993.
1778 ((eolp) (delete-char 1))
1779 (t (error "Cannot convert to babyl format")))))
1780 count))
1782 (defun rmail-hex-char-to-integer (character)
1783 "Return CHARACTER's value interpreted as a hex digit."
1784 (if (and (>= character ?0) (<= character ?9))
1785 (- character ?0)
1786 (let ((ch (logior character 32)))
1787 (if (and (>= ch ?a) (<= ch ?f))
1788 (- ch (- ?a 10))
1789 (error "Invalid hex digit `%c'" ch)))))
1791 (defun rmail-hex-string-to-integer (hex-string)
1792 "Return decimal integer for HEX-STRING."
1793 (let ((hex-num 0)
1794 (index 0))
1795 (while (< index (length hex-string))
1796 (setq hex-num (+ (* hex-num 16)
1797 (rmail-hex-char-to-integer (aref hex-string index))))
1798 (setq index (1+ index)))
1799 hex-num))
1801 (defun rmail-decode-quoted-printable (from to)
1802 "Decode Quoted-Printable in the region between FROM and TO."
1803 (interactive "r")
1804 (goto-char from)
1805 (or (markerp to)
1806 (setq to (copy-marker to)))
1807 (while (search-forward "=" to t)
1808 (cond ((eq (following-char) ?\n)
1809 (delete-char -1)
1810 (delete-char 1))
1811 ((looking-at "[0-9A-F][0-9A-F]")
1812 (let ((byte (rmail-hex-string-to-integer
1813 (buffer-substring (point) (+ 2 (point))))))
1814 (delete-region (1- (point)) (+ 2 (point)))
1815 (insert byte)))
1816 ((looking-at "=")
1817 (delete-char 1))
1819 (message "Malformed MIME quoted-printable message")))))
1821 ;; Delete the "From ..." line, creating various other headers with
1822 ;; information from it if they don't already exist. Now puts the
1823 ;; original line into a mail-from: header line for debugging and for
1824 ;; use by the rmail-output function.
1825 (defun rmail-nuke-pinhead-header ()
1826 (save-excursion
1827 (save-restriction
1828 (let ((start (point))
1829 (end (progn
1830 (condition-case ()
1831 (search-forward "\n\n")
1832 (error
1833 (goto-char (point-max))
1834 (insert "\n\n")))
1835 (point)))
1836 has-from has-date)
1837 (narrow-to-region start end)
1838 (let ((case-fold-search t))
1839 (goto-char start)
1840 (setq has-from (search-forward "\nFrom:" nil t))
1841 (goto-char start)
1842 (setq has-date (and (search-forward "\nDate:" nil t) (point)))
1843 (goto-char start))
1844 (let ((case-fold-search nil))
1845 (if (re-search-forward (concat "^" rmail-unix-mail-delimiter) nil t)
1846 (replace-match
1847 (concat
1848 "Mail-from: \\&"
1849 ;; Keep and reformat the date if we don't
1850 ;; have a Date: field.
1851 (if has-date
1853 (concat
1854 "Date: \\2, \\4 \\3 \\9 \\5 "
1856 ;; The timezone could be matched by group 7 or group 10.
1857 ;; If neither of them matched, assume EST, since only
1858 ;; Easterners would be so sloppy.
1859 ;; It's a shame the substitution can't use "\\10".
1860 (cond
1861 ((/= (match-beginning 7) (match-end 7)) "\\7")
1862 ((/= (match-beginning 10) (match-end 10))
1863 (buffer-substring (match-beginning 10)
1864 (match-end 10)))
1865 (t "EST"))
1866 "\n"))
1867 ;; Keep and reformat the sender if we don't
1868 ;; have a From: field.
1869 (if has-from
1871 "From: \\1\n"))
1872 t)))))))
1874 ;;;; *** Rmail Message Formatting and Header Manipulation ***
1876 (defun rmail-reformat-message (beg end)
1877 (goto-char beg)
1878 (forward-line 1)
1879 (if (/= (following-char) ?0)
1880 (error "Bad format in RMAIL file"))
1881 (let ((inhibit-read-only t)
1882 (delta (- (buffer-size) end)))
1883 (delete-char 1)
1884 (insert ?1)
1885 (forward-line 1)
1886 (let ((case-fold-search t))
1887 (while (looking-at "Summary-line:\\|Mail-From:")
1888 (forward-line 1)))
1889 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*\n")
1890 (delete-region (point)
1891 (progn (forward-line 1) (point))))
1892 (let ((str (buffer-substring (point)
1893 (save-excursion (search-forward "\n\n" end 'move)
1894 (point)))))
1895 (insert str "*** EOOH ***\n")
1896 (narrow-to-region (point) (- (buffer-size) delta)))
1897 (goto-char (point-min))
1898 (if rmail-message-filter (funcall rmail-message-filter))
1899 (if (or rmail-displayed-headers rmail-ignored-headers)
1900 (rmail-clear-headers))))
1902 (defun rmail-clear-headers (&optional ignored-headers)
1903 "Delete all header fields that Rmail should not show.
1904 If the optional argument IGNORED-HEADERS is non-nil,
1905 delete all header fields whose names match that regexp.
1906 Otherwise, if `rmail-displayed-headers' is non-nil,
1907 delete all header fields *except* those whose names match that regexp.
1908 Otherwise, delete all header fields whose names match `rmail-ignored-headers'."
1909 (when (search-forward "\n\n" nil t)
1910 (forward-char -1)
1911 (let ((case-fold-search t)
1912 (buffer-read-only nil))
1913 (if (and rmail-displayed-headers (null ignored-headers))
1914 (save-restriction
1915 (narrow-to-region (point-min) (point))
1916 (let (lim next)
1917 (goto-char (point-min))
1918 (while (and (not (eobp))
1919 (save-excursion
1920 (if (re-search-forward "\n[^ \t]" nil t)
1921 (setq lim (match-beginning 0)
1922 next (1+ lim))
1923 (setq lim nil next (point-max)))))
1924 (if (save-excursion
1925 (re-search-forward rmail-displayed-headers lim t))
1926 (goto-char next)
1927 (delete-region (point) next))))
1928 (goto-char (point-min)))
1929 (or ignored-headers (setq ignored-headers rmail-ignored-headers))
1930 (save-restriction
1931 (narrow-to-region (point-min) (point))
1932 (while (and ignored-headers
1933 (progn
1934 (goto-char (point-min))
1935 (re-search-forward ignored-headers nil t)))
1936 (beginning-of-line)
1937 (delete-region (point)
1938 (if (re-search-forward "\n[^ \t]" nil t)
1939 (1- (point))
1940 (point-max)))))))))
1942 (defun rmail-msg-is-pruned ()
1943 (rmail-maybe-set-message-counters)
1944 (save-restriction
1945 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
1946 (save-excursion
1947 (goto-char (point-min))
1948 (forward-line 1)
1949 (= (following-char) ?1))))
1951 (defun rmail-msg-restore-non-pruned-header ()
1952 (let ((old-point (point))
1953 new-point
1954 new-start
1955 (inhibit-read-only t))
1956 (save-excursion
1957 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
1958 (goto-char (point-min))
1959 (forward-line 1)
1960 ;; Change 1 to 0.
1961 (delete-char 1)
1962 (insert ?0)
1963 ;; Insert new EOOH line at the proper place.
1964 (forward-line 1)
1965 (let ((case-fold-search t))
1966 (while (looking-at "Summary-Line:\\|Mail-From:")
1967 (forward-line 1)))
1968 (insert "*** EOOH ***\n")
1969 (setq new-start (point))
1970 ;; Delete the old reformatted header.
1971 (forward-char -1)
1972 (search-forward "\n*** EOOH ***\n")
1973 (forward-line -1)
1974 (let ((start (point)))
1975 (search-forward "\n\n")
1976 (if (and (<= start old-point)
1977 (<= old-point (point)))
1978 (setq new-point new-start))
1979 (delete-region start (point)))
1980 ;; Narrow to after the new EOOH line.
1981 (narrow-to-region new-start (point-max)))
1982 (if new-point
1983 (goto-char new-point))))
1985 (defun rmail-msg-prune-header ()
1986 (let ((new-point
1987 (= (point) (point-min))))
1988 (save-excursion
1989 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
1990 (rmail-reformat-message (point-min) (point-max)))
1991 (if new-point
1992 (goto-char (point-min)))))
1994 (defun rmail-toggle-header (&optional arg)
1995 "Show original message header if pruned header currently shown, or vice versa.
1996 With argument ARG, show the message header pruned if ARG is greater than zero;
1997 otherwise, show it in full."
1998 (interactive "P")
1999 (let* ((pruned (with-current-buffer rmail-buffer
2000 (rmail-msg-is-pruned)))
2001 (prune (if arg
2002 (> (prefix-numeric-value arg) 0)
2003 (not pruned))))
2004 (if (eq pruned prune)
2006 (set-buffer rmail-buffer)
2007 (rmail-maybe-set-message-counters)
2008 (if rmail-enable-mime
2009 (let ((buffer-read-only nil))
2010 (if pruned
2011 (rmail-msg-restore-non-pruned-header)
2012 (rmail-msg-prune-header))
2013 (funcall rmail-show-mime-function))
2014 (let* ((buffer-read-only nil)
2015 (window (get-buffer-window (current-buffer)))
2016 (at-point-min (= (point) (point-min)))
2017 (all-headers-visible (= (window-start window) (point-min)))
2018 (on-header
2019 (save-excursion
2020 (and (not (search-backward "\n\n" nil t))
2021 (progn
2022 (end-of-line)
2023 (re-search-backward "^[-A-Za-z0-9]+:" nil t))
2024 (match-string 0))))
2025 (old-screen-line
2026 (rmail-count-screen-lines (window-start window) (point))))
2027 (if pruned
2028 (rmail-msg-restore-non-pruned-header)
2029 (rmail-msg-prune-header))
2030 (cond (at-point-min
2031 (goto-char (point-min)))
2032 (on-header
2033 (goto-char (point-min))
2034 (search-forward "\n\n")
2035 (or (re-search-backward
2036 (concat "^" (regexp-quote on-header)) nil t)
2037 (goto-char (point-min))))
2039 (save-selected-window
2040 (select-window window)
2041 (recenter old-screen-line)
2042 (if (and all-headers-visible
2043 (not (= (window-start) (point-min))))
2044 (let ((lines-offscreen (rmail-count-screen-lines
2045 (point-min)
2046 (window-start window))))
2047 (recenter (min (+ old-screen-line lines-offscreen)
2048 ;; last line of window
2049 (- (window-height) 2))))))))))
2050 (rmail-highlight-headers))))
2052 (defun rmail-narrow-to-non-pruned-header ()
2053 "Narrow to the whole (original) header of the current message."
2054 (let (start end)
2055 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2056 (goto-char (point-min))
2057 (forward-line 1)
2058 (if (= (following-char) ?1)
2059 (progn
2060 (forward-line 1)
2061 (setq start (point))
2062 (search-forward "*** EOOH ***\n")
2063 (setq end (match-beginning 0)))
2064 (forward-line 2)
2065 (setq start (point))
2066 (search-forward "\n\n")
2067 (setq end (1- (point))))
2068 (narrow-to-region start end)
2069 (goto-char start)))
2071 ;; Lifted from repos-count-screen-lines.
2072 ;; Return number of screen lines between START and END.
2073 (defun rmail-count-screen-lines (start end)
2074 (save-excursion
2075 (save-restriction
2076 (narrow-to-region start end)
2077 (goto-char (point-min))
2078 (vertical-motion (- (point-max) (point-min))))))
2080 ;;;; *** Rmail Attributes and Keywords ***
2082 ;; Make a string describing current message's attributes and keywords
2083 ;; and set it up as the name of a minor mode
2084 ;; so it will appear in the mode line.
2085 (defun rmail-display-labels ()
2086 (let ((blurb "") (beg (point-min-marker)) (end (point-max-marker)))
2087 (save-excursion
2088 (unwind-protect
2089 (progn
2090 (widen)
2091 (goto-char (rmail-msgbeg rmail-current-message))
2092 (forward-line 1)
2093 (if (looking-at "[01],")
2094 (progn
2095 (narrow-to-region (point) (progn (end-of-line) (point)))
2096 ;; Truly valid BABYL format requires a space before each
2097 ;; attribute or keyword name. Put them in if missing.
2098 (let (buffer-read-only)
2099 (goto-char (point-min))
2100 (while (search-forward "," nil t)
2101 (or (looking-at "[ ,]") (eobp)
2102 (insert " "))))
2103 (goto-char (point-max))
2104 (if (search-backward ",," nil 'move)
2105 (progn
2106 (if (> (point) (1+ (point-min)))
2107 (setq blurb (buffer-substring (+ 1 (point-min)) (point))))
2108 (if (> (- (point-max) (point)) 2)
2109 (setq blurb
2110 (concat blurb
2112 (buffer-substring (+ (point) 3)
2113 (1- (point-max)))))))))))
2114 ;; Note: we don't use save-restriction because that does not work right
2115 ;; if changes are made outside the saved restriction
2116 ;; before that restriction is restored.
2117 (narrow-to-region beg end)
2118 (set-marker beg nil)
2119 (set-marker end nil)))
2120 (while (string-match " +," blurb)
2121 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
2122 (substring blurb (match-end 0)))))
2123 (while (string-match ", +" blurb)
2124 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
2125 (substring blurb (match-end 0)))))
2126 (setq mode-line-process
2127 (format " %d/%d%s"
2128 rmail-current-message rmail-total-messages blurb))
2129 ;; If rmail-enable-mime is non-nil, we may have to update
2130 ;; `mode-line-process' of rmail-view-buffer too.
2131 (if (and rmail-enable-mime
2132 (not (eq (current-buffer) rmail-view-buffer))
2133 (buffer-live-p rmail-view-buffer))
2134 (let ((mlp mode-line-process))
2135 (with-current-buffer rmail-view-buffer
2136 (setq mode-line-process mlp))))))
2138 ;; Turn an attribute of a message on or off according to STATE.
2139 ;; ATTR is the name of the attribute, as a string.
2140 ;; MSGNUM is message number to change; nil means current message.
2141 (defun rmail-set-attribute (attr state &optional msgnum)
2142 (set-buffer rmail-buffer)
2143 (let ((omax (point-max-marker))
2144 (omin (point-min-marker))
2145 (buffer-read-only nil))
2146 (or msgnum (setq msgnum rmail-current-message))
2147 (if (> msgnum 0)
2148 (unwind-protect
2149 (save-excursion
2150 (widen)
2151 (goto-char (+ 3 (rmail-msgbeg msgnum)))
2152 (let ((curstate
2153 (not
2154 (null (search-backward (concat ", " attr ",")
2155 (prog1 (point) (end-of-line)) t)))))
2156 (or (eq curstate (not (not state)))
2157 (if curstate
2158 (delete-region (point) (1- (match-end 0)))
2159 (beginning-of-line)
2160 (forward-char 2)
2161 (insert " " attr ","))))
2162 (if (string= attr "deleted")
2163 (rmail-set-message-deleted-p msgnum state)))
2164 ;; Note: we don't use save-restriction because that does not work right
2165 ;; if changes are made outside the saved restriction
2166 ;; before that restriction is restored.
2167 (narrow-to-region omin omax)
2168 (set-marker omin nil)
2169 (set-marker omax nil)
2170 (if (= msgnum rmail-current-message)
2171 (rmail-display-labels))))))
2173 ;; Return t if the attributes/keywords line of msg number MSG
2174 ;; contains a match for the regexp LABELS.
2175 (defun rmail-message-labels-p (msg labels)
2176 (save-excursion
2177 (save-restriction
2178 (widen)
2179 (goto-char (rmail-msgbeg msg))
2180 (forward-char 3)
2181 (re-search-backward labels (prog1 (point) (end-of-line)) t))))
2183 ;;;; *** Rmail Message Selection And Support ***
2185 (defun rmail-msgend (n)
2186 (marker-position (aref rmail-message-vector (1+ n))))
2188 (defun rmail-msgbeg (n)
2189 (marker-position (aref rmail-message-vector n)))
2191 (defun rmail-widen-to-current-msgbeg (function)
2192 "Call FUNCTION with point at start of internal data of current message.
2193 Assumes that bounds were previously narrowed to display the message in Rmail.
2194 The bounds are widened enough to move point where desired, then narrowed
2195 again afterward.
2197 FUNCTION may not change the visible text of the message, but it may
2198 change the invisible header text."
2199 (save-excursion
2200 (unwind-protect
2201 (progn
2202 (narrow-to-region (rmail-msgbeg rmail-current-message)
2203 (point-max))
2204 (goto-char (point-min))
2205 (funcall function))
2206 ;; Note: we don't use save-restriction because that does not work right
2207 ;; if changes are made outside the saved restriction
2208 ;; before that restriction is restored.
2209 (narrow-to-region (rmail-msgbeg rmail-current-message)
2210 (rmail-msgend rmail-current-message)))))
2212 (defun rmail-forget-messages ()
2213 (unwind-protect
2214 (if (vectorp rmail-message-vector)
2215 (let* ((i 0)
2216 (v rmail-message-vector)
2217 (n (length v)))
2218 (while (< i n)
2219 (move-marker (aref v i) nil)
2220 (setq i (1+ i)))))
2221 (setq rmail-message-vector nil)
2222 (setq rmail-msgref-vector nil)
2223 (setq rmail-deleted-vector nil)))
2225 (defun rmail-maybe-set-message-counters ()
2226 (if (not (and rmail-deleted-vector
2227 rmail-message-vector
2228 rmail-current-message
2229 rmail-total-messages))
2230 (rmail-set-message-counters)))
2232 (defun rmail-count-new-messages (&optional nomsg)
2233 (let* ((case-fold-search nil)
2234 (total-messages 0)
2235 (messages-head nil)
2236 (deleted-head nil))
2237 (or nomsg (message "Counting new messages..."))
2238 (goto-char (point-max))
2239 ;; Put at the end of messages-head
2240 ;; the entry for message N+1, which marks
2241 ;; the end of message N. (N = number of messages).
2242 (search-backward "\n\^_")
2243 (forward-char 1)
2244 (setq messages-head (list (point-marker)))
2245 (rmail-set-message-counters-counter (point-min))
2246 (setq rmail-current-message (1+ rmail-total-messages))
2247 (setq rmail-total-messages
2248 (+ rmail-total-messages total-messages))
2249 (setq rmail-message-vector
2250 (vconcat rmail-message-vector (cdr messages-head)))
2251 (aset rmail-message-vector
2252 rmail-current-message (car messages-head))
2253 (setq rmail-deleted-vector
2254 (concat rmail-deleted-vector deleted-head))
2255 (setq rmail-summary-vector
2256 (vconcat rmail-summary-vector (make-vector total-messages nil)))
2257 (setq rmail-msgref-vector
2258 (vconcat rmail-msgref-vector (make-vector total-messages nil)))
2259 ;; Fill in the new elements of rmail-msgref-vector.
2260 (let ((i (1+ (- rmail-total-messages total-messages))))
2261 (while (<= i rmail-total-messages)
2262 (aset rmail-msgref-vector i (list i))
2263 (setq i (1+ i))))
2264 (goto-char (point-min))
2265 (or nomsg (message "Counting new messages...done (%d)" total-messages))))
2267 (defun rmail-set-message-counters ()
2268 (rmail-forget-messages)
2269 (save-excursion
2270 (save-restriction
2271 (widen)
2272 (let* ((point-save (point))
2273 (total-messages 0)
2274 (messages-after-point)
2275 (case-fold-search nil)
2276 (messages-head nil)
2277 (deleted-head nil))
2278 (message "Counting messages...")
2279 (goto-char (point-max))
2280 ;; Put at the end of messages-head
2281 ;; the entry for message N+1, which marks
2282 ;; the end of message N. (N = number of messages).
2283 (search-backward "\n\^_" nil t)
2284 (if (/= (point) (point-max)) (forward-char 1))
2285 (setq messages-head (list (point-marker)))
2286 (rmail-set-message-counters-counter (min (point) point-save))
2287 (setq messages-after-point total-messages)
2288 (rmail-set-message-counters-counter)
2289 (setq rmail-total-messages total-messages)
2290 (setq rmail-current-message
2291 (min total-messages
2292 (max 1 (- total-messages messages-after-point))))
2293 (setq rmail-message-vector
2294 (apply 'vector (cons (point-min-marker) messages-head))
2295 rmail-deleted-vector (concat "0" deleted-head)
2296 rmail-summary-vector (make-vector rmail-total-messages nil)
2297 rmail-msgref-vector (make-vector (1+ rmail-total-messages) nil))
2298 (let ((i 0))
2299 (while (<= i rmail-total-messages)
2300 (aset rmail-msgref-vector i (list i))
2301 (setq i (1+ i))))
2302 (message "Counting messages...done")))))
2304 (defun rmail-set-message-counters-counter (&optional stop)
2305 (let ((start (point))
2306 next)
2307 (while (search-backward "\n\^_\^L" stop t)
2308 ;; Detect messages that have been added with DOS line endings and
2309 ;; convert the line endings for such messages.
2310 (setq next (point))
2311 (if (looking-at "\n\^_\^L\r\n")
2312 (let ((buffer-read-only nil)
2313 (buffer-undo t))
2314 (message "Counting messages...(converting line endings)")
2315 (save-excursion
2316 (goto-char start)
2317 (while (search-backward "\r\n" next t)
2318 (delete-char 1)))))
2319 (setq start next)
2320 (forward-char 1)
2321 (setq messages-head (cons (point-marker) messages-head))
2322 (save-excursion
2323 (setq deleted-head
2324 (cons (if (search-backward ", deleted,"
2325 (prog1 (point)
2326 (forward-line 2))
2328 ?D ?\ )
2329 deleted-head)))
2330 (if (zerop (% (setq total-messages (1+ total-messages)) 20))
2331 (message "Counting messages...%d" total-messages)))))
2333 (defun rmail-beginning-of-message ()
2334 "Show current message starting from the beginning."
2335 (interactive)
2336 (rmail-show-message rmail-current-message))
2338 (defun rmail-show-message (&optional n no-summary)
2339 "Show message number N (prefix argument), counting from start of file.
2340 If summary buffer is currently displayed, update current message there also."
2341 (interactive "p")
2342 (or (eq major-mode 'rmail-mode)
2343 (switch-to-buffer rmail-buffer))
2344 (rmail-maybe-set-message-counters)
2345 (widen)
2346 (if (zerop rmail-total-messages)
2347 (progn (narrow-to-region (point-min) (1- (point-max)))
2348 (goto-char (point-min))
2349 (setq mode-line-process nil))
2350 (let (blurb coding-system)
2351 (if (not n)
2352 (setq n rmail-current-message)
2353 (cond ((<= n 0)
2354 (setq n 1
2355 rmail-current-message 1
2356 blurb "No previous message"))
2357 ((> n rmail-total-messages)
2358 (setq n rmail-total-messages
2359 rmail-current-message rmail-total-messages
2360 blurb "No following message"))
2362 (setq rmail-current-message n))))
2363 (let ((beg (rmail-msgbeg n)))
2364 (goto-char beg)
2365 (forward-line 1)
2366 (save-excursion
2367 (let ((end (rmail-msgend n)))
2368 (save-restriction
2369 (if (prog1 (= (following-char) ?0)
2370 (forward-line 2)
2371 ;; If there's a Summary-line in the (otherwise empty)
2372 ;; header, we didn't yet get past the EOOH line.
2373 (if (looking-at "^\\*\\*\\* EOOH \\*\\*\\*\n")
2374 (forward-line 1))
2375 (narrow-to-region (point) end))
2376 (rfc822-goto-eoh)
2377 (search-forward "\n*** EOOH ***\n" end t))
2378 (narrow-to-region beg (point))
2379 (goto-char (point-min))
2380 (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t)
2381 (let ((coding-system (intern (match-string 1))))
2382 (check-coding-system coding-system)
2383 (setq buffer-file-coding-system coding-system))
2384 (setq buffer-file-coding-system nil)))))
2385 ;; Clear the "unseen" attribute when we show a message.
2386 (rmail-set-attribute "unseen" nil)
2387 (let ((end (rmail-msgend n)))
2388 ;; Reformat the header, or else find the reformatted header.
2389 (if (= (following-char) ?0)
2390 (rmail-reformat-message beg end)
2391 (search-forward "\n*** EOOH ***\n" end t)
2392 (narrow-to-region (point) end)))
2393 (goto-char (point-min))
2394 (walk-windows
2395 (function (lambda (window)
2396 (if (eq (window-buffer window) (current-buffer))
2397 (set-window-point window (point)))))
2398 nil t)
2399 (rmail-display-labels)
2400 (if (eq rmail-enable-mime t)
2401 (funcall rmail-show-mime-function)
2402 (setq rmail-view-buffer rmail-buffer)
2404 (rmail-highlight-headers)
2405 (if transient-mark-mode (deactivate-mark))
2406 (run-hooks 'rmail-show-message-hook)
2407 ;; If there is a summary buffer, try to move to this message
2408 ;; in that buffer. But don't complain if this message
2409 ;; is not mentioned in the summary.
2410 ;; Don't do this at all if we were called on behalf
2411 ;; of cursor motion in the summary buffer.
2412 (and (rmail-summary-exists) (not no-summary)
2413 (let ((curr-msg rmail-current-message))
2414 (rmail-select-summary
2415 (rmail-summary-goto-msg curr-msg t t))))
2416 (with-current-buffer rmail-buffer
2417 (rmail-auto-file))
2418 (if blurb
2419 (message blurb))))))
2421 (defun rmail-redecode-body (coding)
2422 "Decode the body of the current message using coding system CODING.
2423 This is useful with mail messages that have malformed or missing
2424 charset= headers.
2426 This function assumes that the current message is already decoded
2427 and displayed in the RMAIL buffer, but the coding system used to
2428 decode it was incorrect. It then encodes the message back to its
2429 original form, and decodes it again, using the coding system CODING.
2431 Note that if Emacs erroneously auto-detected one of the iso-2022
2432 encodings in the message, this function might fail because the escape
2433 sequences that switch between character sets and also single-shift and
2434 locking-shift codes are impossible to recover. This function is meant
2435 to be used to fix messages encoded with 8-bit encodings, such as
2436 iso-8859, koi8-r, etc."
2437 (interactive "zCoding system for re-decoding this message: ")
2438 (when (not rmail-enable-mime)
2439 (or (eq major-mode 'rmail-mode)
2440 (switch-to-buffer rmail-buffer))
2441 (save-excursion
2442 (unwind-protect
2443 (let ((msgbeg (rmail-msgbeg rmail-current-message))
2444 (msgend (rmail-msgend rmail-current-message))
2445 x-coding-header)
2446 (narrow-to-region msgbeg msgend)
2447 (goto-char (point-min))
2448 (when (search-forward "\n*** EOOH ***\n" (point-max) t)
2449 (narrow-to-region msgbeg (point)))
2450 (goto-char (point-min))
2451 (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t)
2452 (let ((old-coding (intern (match-string 1)))
2453 (buffer-read-only nil))
2454 (check-coding-system old-coding)
2455 ;; Make sure the new coding system uses the same EOL
2456 ;; conversion, to prevent ^M characters from popping
2457 ;; up all over the place.
2458 (setq coding
2459 (coding-system-change-eol-conversion
2460 coding
2461 (coding-system-eol-type old-coding)))
2462 (setq x-coding-header (point-marker))
2463 (narrow-to-region msgbeg msgend)
2464 (encode-coding-region (point) msgend old-coding)
2465 (decode-coding-region (point) msgend coding)
2466 (setq last-coding-system-used coding)
2467 ;; Rewrite the coding-system header according
2468 ;; to what we did.
2469 (goto-char x-coding-header)
2470 (delete-region (point)
2471 (save-excursion
2472 (beginning-of-line)
2473 (point)))
2474 (insert "X-Coding-System: "
2475 (symbol-name last-coding-system-used))
2476 (set-marker x-coding-header nil)
2477 (rmail-show-message))
2478 (error "No X-Coding-System header found")))))))
2480 ;; Find all occurrences of certain fields, and highlight them.
2481 (defun rmail-highlight-headers ()
2482 ;; Do this only if the system supports faces.
2483 (if (and (fboundp 'internal-find-face)
2484 rmail-highlighted-headers)
2485 (save-excursion
2486 (search-forward "\n\n" nil 'move)
2487 (save-restriction
2488 (narrow-to-region (point-min) (point))
2489 (let ((case-fold-search t)
2490 (inhibit-read-only t)
2491 ;; Highlight with boldface if that is available.
2492 ;; Otherwise use the `highlight' face.
2493 (face (or rmail-highlight-face
2494 (if (face-differs-from-default-p 'bold)
2495 'bold 'highlight)))
2496 ;; List of overlays to reuse.
2497 (overlays rmail-overlay-list))
2498 (goto-char (point-min))
2499 (while (re-search-forward rmail-highlighted-headers nil t)
2500 (skip-chars-forward " \t")
2501 (let ((beg (point))
2502 overlay)
2503 (while (progn (forward-line 1)
2504 (looking-at "[ \t]")))
2505 ;; Back up over newline, then trailing spaces or tabs
2506 (forward-char -1)
2507 (while (member (preceding-char) '(? ?\t))
2508 (forward-char -1))
2509 (if overlays
2510 ;; Reuse an overlay we already have.
2511 (progn
2512 (setq overlay (car overlays)
2513 overlays (cdr overlays))
2514 (overlay-put overlay 'face face)
2515 (move-overlay overlay beg (point)))
2516 ;; Make a new overlay and add it to
2517 ;; rmail-overlay-list.
2518 (setq overlay (make-overlay beg (point)))
2519 (overlay-put overlay 'face face)
2520 (setq rmail-overlay-list
2521 (cons overlay rmail-overlay-list))))))))))
2523 (defun rmail-auto-file ()
2524 "Automatically move a message into a sub-folder based on criteria.
2525 Called when a new message is displayed."
2526 (if (or (rmail-message-labels-p rmail-current-message "filed")
2527 (not (string= (buffer-file-name)
2528 (expand-file-name rmail-file-name))))
2529 ;; Do nothing if it's already been filed.
2531 ;; Find out some basics (common fields)
2532 (let ((from (mail-fetch-field "from"))
2533 (subj (mail-fetch-field "subject"))
2534 (to (concat (mail-fetch-field "to") "," (mail-fetch-field "cc")))
2535 (d rmail-automatic-folder-directives)
2536 (directive-loop nil)
2537 (folder nil))
2538 (while d
2539 (setq folder (car (car d))
2540 directive-loop (cdr (car d)))
2541 (while (and (car directive-loop)
2542 (let ((f (cond
2543 ((string= (car directive-loop) "from") from)
2544 ((string= (car directive-loop) "to") to)
2545 ((string= (car directive-loop) "subject") subj)
2546 (t (mail-fetch-field (car directive-loop))))))
2547 (and f (string-match (car (cdr directive-loop)) f))))
2548 (setq directive-loop (cdr (cdr directive-loop))))
2549 ;; If there are no directives left, then it was a complete match.
2550 (if (null directive-loop)
2551 (if (null folder)
2552 (rmail-delete-forward)
2553 (if (string= "/dev/null" folder)
2554 (rmail-delete-message)
2555 (rmail-output-to-rmail-file folder 1 t)
2556 (setq d nil))))
2557 (setq d (cdr d))))))
2559 (defun rmail-next-message (n)
2560 "Show following message whether deleted or not.
2561 With prefix arg N, moves forward N messages, or backward if N is negative."
2562 (interactive "p")
2563 (set-buffer rmail-buffer)
2564 (rmail-maybe-set-message-counters)
2565 (rmail-show-message (+ rmail-current-message n)))
2567 (defun rmail-previous-message (n)
2568 "Show previous message whether deleted or not.
2569 With prefix arg N, moves backward N messages, or forward if N is negative."
2570 (interactive "p")
2571 (rmail-next-message (- n)))
2573 (defun rmail-next-undeleted-message (n)
2574 "Show following non-deleted message.
2575 With prefix arg N, moves forward N non-deleted messages,
2576 or backward if N is negative.
2578 Returns t if a new message is being shown, nil otherwise."
2579 (interactive "p")
2580 (set-buffer rmail-buffer)
2581 (rmail-maybe-set-message-counters)
2582 (let ((lastwin rmail-current-message)
2583 (current rmail-current-message))
2584 (while (and (> n 0) (< current rmail-total-messages))
2585 (setq current (1+ current))
2586 (if (not (rmail-message-deleted-p current))
2587 (setq lastwin current n (1- n))))
2588 (while (and (< n 0) (> current 1))
2589 (setq current (1- current))
2590 (if (not (rmail-message-deleted-p current))
2591 (setq lastwin current n (1+ n))))
2592 (if (/= lastwin rmail-current-message)
2593 (progn (rmail-show-message lastwin)
2595 (if (< n 0)
2596 (message "No previous nondeleted message"))
2597 (if (> n 0)
2598 (message "No following nondeleted message"))
2599 nil)))
2601 (defun rmail-previous-undeleted-message (n)
2602 "Show previous non-deleted message.
2603 With prefix argument N, moves backward N non-deleted messages,
2604 or forward if N is negative."
2605 (interactive "p")
2606 (rmail-next-undeleted-message (- n)))
2608 (defun rmail-first-message ()
2609 "Show first message in file."
2610 (interactive)
2611 (rmail-maybe-set-message-counters)
2612 (rmail-show-message 1))
2614 (defun rmail-last-message ()
2615 "Show last message in file."
2616 (interactive)
2617 (rmail-maybe-set-message-counters)
2618 (rmail-show-message rmail-total-messages))
2620 (defun rmail-what-message ()
2621 (let ((where (point))
2622 (low 1)
2623 (high rmail-total-messages)
2624 (mid (/ rmail-total-messages 2)))
2625 (while (> (- high low) 1)
2626 (if (>= where (rmail-msgbeg mid))
2627 (setq low mid)
2628 (setq high mid))
2629 (setq mid (+ low (/ (- high low) 2))))
2630 (if (>= where (rmail-msgbeg high)) high low)))
2632 (defun rmail-message-recipients-p (msg recipients &optional primary-only)
2633 (save-restriction
2634 (goto-char (rmail-msgbeg msg))
2635 (search-forward "\n*** EOOH ***\n")
2636 (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
2637 (or (string-match recipients (or (mail-fetch-field "To") ""))
2638 (string-match recipients (or (mail-fetch-field "From") ""))
2639 (if (not primary-only)
2640 (string-match recipients (or (mail-fetch-field "Cc") ""))))))
2642 (defun rmail-message-regexp-p (n regexp)
2643 "Return t, if for message number N, regexp REGEXP matches in the header."
2644 (let ((beg (rmail-msgbeg n))
2645 (end (rmail-msgend n)))
2646 (goto-char beg)
2647 (forward-line 1)
2648 (save-excursion
2649 (save-restriction
2650 (if (prog1 (= (following-char) ?0)
2651 (forward-line 2)
2652 ;; If there's a Summary-line in the (otherwise empty)
2653 ;; header, we didn't yet get past the EOOH line.
2654 (when (looking-at "^\\*\\*\\* EOOH \\*\\*\\*\n")
2655 (forward-line 1))
2656 (setq beg (point))
2657 (narrow-to-region (point) end))
2658 (progn
2659 (rfc822-goto-eoh)
2660 (setq end (point)))
2661 (setq beg (point))
2662 (search-forward "\n*** EOOH ***\n" end t)
2663 (setq end (1+ (match-beginning 0)))))
2664 (goto-char beg)
2665 (if rmail-enable-mime
2666 (funcall rmail-search-mime-header-function n regexp end)
2667 (re-search-forward regexp end t)))))
2669 (defun rmail-search-message (msg regexp)
2670 "Return non-nil, if for message number MSG, regexp REGEXP matches."
2671 (goto-char (rmail-msgbeg msg))
2672 (if rmail-enable-mime
2673 (funcall rmail-search-mime-message-function msg regexp)
2674 (re-search-forward regexp (rmail-msgend msg) t)))
2676 (defvar rmail-search-last-regexp nil)
2677 (defun rmail-search (regexp &optional n)
2678 "Show message containing next match for REGEXP (but not the current msg).
2679 Prefix argument gives repeat count; negative argument means search
2680 backwards (through earlier messages).
2681 Interactively, empty argument means use same regexp used last time."
2682 (interactive
2683 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
2684 (prompt
2685 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
2686 regexp)
2687 (if rmail-search-last-regexp
2688 (setq prompt (concat prompt
2689 "(default "
2690 rmail-search-last-regexp
2691 ") ")))
2692 (setq regexp (read-string prompt))
2693 (cond ((not (equal regexp ""))
2694 (setq rmail-search-last-regexp regexp))
2695 ((not rmail-search-last-regexp)
2696 (error "No previous Rmail search string")))
2697 (list rmail-search-last-regexp
2698 (prefix-numeric-value current-prefix-arg))))
2699 (or n (setq n 1))
2700 (message "%sRmail search for %s..."
2701 (if (< n 0) "Reverse " "")
2702 regexp)
2703 (set-buffer rmail-buffer)
2704 (rmail-maybe-set-message-counters)
2705 (let ((omin (point-min))
2706 (omax (point-max))
2707 (opoint (point))
2709 (reversep (< n 0))
2710 (msg rmail-current-message))
2711 (unwind-protect
2712 (progn
2713 (widen)
2714 (while (/= n 0)
2715 ;; Check messages one by one, advancing message number up or down
2716 ;; but searching forward through each message.
2717 (if reversep
2718 (while (and (null win) (> msg 1))
2719 (setq msg (1- msg)
2720 win (rmail-search-message msg regexp)))
2721 (while (and (null win) (< msg rmail-total-messages))
2722 (setq msg (1+ msg)
2723 win (rmail-search-message msg regexp))))
2724 (setq n (+ n (if reversep 1 -1)))))
2725 (if win
2726 (progn
2727 (rmail-show-message msg)
2728 ;; Search forward (if this is a normal search) or backward
2729 ;; (if this is a reverse search) through this message to
2730 ;; position point. This search may fail because REGEXP
2731 ;; was found in the hidden portion of this message. In
2732 ;; that case, move point to the beginning of visible
2733 ;; portion.
2734 (if reversep
2735 (progn
2736 (goto-char (point-max))
2737 (re-search-backward regexp nil 'move))
2738 (goto-char (point-min))
2739 (re-search-forward regexp nil t))
2740 (message "%sRmail search for %s...done"
2741 (if reversep "Reverse " "")
2742 regexp))
2743 (goto-char opoint)
2744 (narrow-to-region omin omax)
2745 (ding)
2746 (message "Search failed: %s" regexp)))))
2748 (defun rmail-search-backwards (regexp &optional n)
2749 "Show message containing previous match for REGEXP.
2750 Prefix argument gives repeat count; negative argument means search
2751 forward (through later messages).
2752 Interactively, empty argument means use same regexp used last time."
2753 (interactive
2754 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
2755 (prompt
2756 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
2757 regexp)
2758 (if rmail-search-last-regexp
2759 (setq prompt (concat prompt
2760 "(default "
2761 rmail-search-last-regexp
2762 ") ")))
2763 (setq regexp (read-string prompt))
2764 (cond ((not (equal regexp ""))
2765 (setq rmail-search-last-regexp regexp))
2766 ((not rmail-search-last-regexp)
2767 (error "No previous Rmail search string")))
2768 (list rmail-search-last-regexp
2769 (prefix-numeric-value current-prefix-arg))))
2770 (rmail-search regexp (- (or n 1))))
2772 ;; Show the first message which has the `unseen' attribute.
2773 (defun rmail-first-unseen-message ()
2774 (rmail-maybe-set-message-counters)
2775 (let ((current 1)
2776 found)
2777 (save-restriction
2778 (widen)
2779 (while (and (not found) (<= current rmail-total-messages))
2780 (if (rmail-message-labels-p current ", ?\\(unseen\\),")
2781 (setq found current))
2782 (setq current (1+ current))))
2783 ;; Let the caller show the message.
2784 ;; (if found
2785 ;; (rmail-show-message found))
2786 found))
2788 (defun rmail-next-same-subject (n)
2789 "Go to the next mail message having the same subject header.
2790 With prefix argument N, do this N times.
2791 If N is negative, go backwards instead."
2792 (interactive "p")
2793 (let ((subject (mail-fetch-field "Subject"))
2794 (forward (> n 0))
2795 (i rmail-current-message)
2796 (case-fold-search t)
2797 search-regexp found)
2798 (if (string-match "\\`[ \t]+" subject)
2799 (setq subject (substring subject (match-end 0))))
2800 (if (string-match "Re:[ \t]*" subject)
2801 (setq subject (substring subject (match-end 0))))
2802 (if (string-match "[ \t]+\\'" subject)
2803 (setq subject (substring subject 0 (match-beginning 0))))
2804 (setq search-regexp (concat "^Subject: *\\(Re:[ \t]*\\)?"
2805 (regexp-quote subject)
2806 "[ \t]*\n"))
2807 (save-excursion
2808 (save-restriction
2809 (widen)
2810 (while (and (/= n 0)
2811 (if forward
2812 (< i rmail-total-messages)
2813 (> i 1)))
2814 (let (done)
2815 (while (and (not done)
2816 (if forward
2817 (< i rmail-total-messages)
2818 (> i 1)))
2819 (setq i (if forward (1+ i) (1- i)))
2820 (goto-char (rmail-msgbeg i))
2821 (search-forward "\n*** EOOH ***\n")
2822 (let ((beg (point)) end)
2823 (search-forward "\n\n")
2824 (setq end (point))
2825 (goto-char beg)
2826 (setq done (re-search-forward search-regexp end t))))
2827 (if done (setq found i)))
2828 (setq n (if forward (1- n) (1+ n))))))
2829 (if found
2830 (rmail-show-message found)
2831 (error "No %s message with same subject"
2832 (if forward "following" "previous")))))
2834 (defun rmail-previous-same-subject (n)
2835 "Go to the previous mail message having the same subject header.
2836 With prefix argument N, do this N times.
2837 If N is negative, go forwards instead."
2838 (interactive "p")
2839 (rmail-next-same-subject (- n)))
2841 ;;;; *** Rmail Message Deletion Commands ***
2843 (defun rmail-message-deleted-p (n)
2844 (= (aref rmail-deleted-vector n) ?D))
2846 (defun rmail-set-message-deleted-p (n state)
2847 (aset rmail-deleted-vector n (if state ?D ?\ )))
2849 (defun rmail-delete-message ()
2850 "Delete this message and stay on it."
2851 (interactive)
2852 (rmail-set-attribute "deleted" t)
2853 (run-hooks 'rmail-delete-message-hook))
2855 (defun rmail-undelete-previous-message ()
2856 "Back up to deleted message, select it, and undelete it."
2857 (interactive)
2858 (set-buffer rmail-buffer)
2859 (let ((msg rmail-current-message))
2860 (while (and (> msg 0)
2861 (not (rmail-message-deleted-p msg)))
2862 (setq msg (1- msg)))
2863 (if (= msg 0)
2864 (error "No previous deleted message")
2865 (if (/= msg rmail-current-message)
2866 (rmail-show-message msg))
2867 (rmail-set-attribute "deleted" nil)
2868 (if (rmail-summary-exists)
2869 (save-excursion
2870 (set-buffer rmail-summary-buffer)
2871 (rmail-summary-mark-undeleted msg)))
2872 (rmail-maybe-display-summary))))
2874 (defun rmail-delete-forward (&optional backward)
2875 "Delete this message and move to next nondeleted one.
2876 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
2877 With prefix argument, delete and move backward.
2879 Returns t if a new message is displayed after the delete, or nil otherwise."
2880 (interactive "P")
2881 (rmail-set-attribute "deleted" t)
2882 (run-hooks 'rmail-delete-message-hook)
2883 (let ((del-msg rmail-current-message))
2884 (if (rmail-summary-exists)
2885 (rmail-select-summary
2886 (rmail-summary-mark-deleted del-msg)))
2887 (prog1 (rmail-next-undeleted-message (if backward -1 1))
2888 (rmail-maybe-display-summary))))
2890 (defun rmail-delete-backward ()
2891 "Delete this message and move to previous nondeleted one.
2892 Deleted messages stay in the file until the \\[rmail-expunge] command is given."
2893 (interactive)
2894 (rmail-delete-forward t))
2896 ;; Compute the message number a given message would have after expunging.
2897 ;; The present number of the message is OLDNUM.
2898 ;; DELETEDVEC should be rmail-deleted-vector.
2899 ;; The value is nil for a message that would be deleted.
2900 (defun rmail-msg-number-after-expunge (deletedvec oldnum)
2901 (if (or (null oldnum) (= (aref deletedvec oldnum) ?D))
2903 (let ((i 0)
2904 (newnum 0))
2905 (while (< i oldnum)
2906 (if (/= (aref deletedvec i) ?D)
2907 (setq newnum (1+ newnum)))
2908 (setq i (1+ i)))
2909 newnum)))
2911 (defun rmail-expunge-confirmed ()
2912 "Return t if deleted message should be expunged. If necessary, ask the user.
2913 See also user-option `rmail-confirm-expunge'."
2914 (set-buffer rmail-buffer)
2915 (or (not (stringp rmail-deleted-vector))
2916 (not (string-match "D" rmail-deleted-vector))
2917 (null rmail-confirm-expunge)
2918 (funcall rmail-confirm-expunge
2919 "Erase deleted messages from Rmail file? ")))
2921 (defun rmail-only-expunge ()
2922 "Actually erase all deleted messages in the file."
2923 (interactive)
2924 (set-buffer rmail-buffer)
2925 (message "Expunging deleted messages...")
2926 ;; Discard all undo records for this buffer.
2927 (or (eq buffer-undo-list t)
2928 (setq buffer-undo-list nil))
2929 (rmail-maybe-set-message-counters)
2930 (let* ((omax (- (buffer-size) (point-max)))
2931 (omin (- (buffer-size) (point-min)))
2932 (opoint (if (and (> rmail-current-message 0)
2933 (rmail-message-deleted-p rmail-current-message))
2935 (if rmail-enable-mime
2936 (with-current-buffer rmail-view-buffer
2937 (- (point)(point-min)))
2938 (- (point) (point-min)))))
2939 (messages-head (cons (aref rmail-message-vector 0) nil))
2940 (messages-tail messages-head)
2941 ;; Don't make any undo records for the expunging.
2942 (buffer-undo-list t)
2943 (win))
2944 (unwind-protect
2945 (save-excursion
2946 (widen)
2947 (goto-char (point-min))
2948 (let ((counter 0)
2949 (number 1)
2950 (total rmail-total-messages)
2951 (new-message-number rmail-current-message)
2952 (new-summary nil)
2953 (new-msgref (list (list 0)))
2954 (rmailbuf (current-buffer))
2955 (buffer-read-only nil)
2956 (messages rmail-message-vector)
2957 (deleted rmail-deleted-vector)
2958 (summary rmail-summary-vector))
2959 (setq rmail-total-messages nil
2960 rmail-current-message nil
2961 rmail-message-vector nil
2962 rmail-deleted-vector nil
2963 rmail-summary-vector nil)
2965 (while (<= number total)
2966 (if (= (aref deleted number) ?D)
2967 (progn
2968 (delete-region
2969 (marker-position (aref messages number))
2970 (marker-position (aref messages (1+ number))))
2971 (move-marker (aref messages number) nil)
2972 (if (> new-message-number counter)
2973 (setq new-message-number (1- new-message-number))))
2974 (setq counter (1+ counter))
2975 (setq messages-tail
2976 (setcdr messages-tail
2977 (cons (aref messages number) nil)))
2978 (setq new-summary
2979 (cons (if (= counter number) (aref summary (1- number)))
2980 new-summary))
2981 (setq new-msgref
2982 (cons (aref rmail-msgref-vector number)
2983 new-msgref))
2984 (setcar (car new-msgref) counter))
2985 (if (zerop (% (setq number (1+ number)) 20))
2986 (message "Expunging deleted messages...%d" number)))
2987 (setq messages-tail
2988 (setcdr messages-tail
2989 (cons (aref messages number) nil)))
2990 (setq rmail-current-message new-message-number
2991 rmail-total-messages counter
2992 rmail-message-vector (apply 'vector messages-head)
2993 rmail-deleted-vector (make-string (1+ counter) ?\ )
2994 rmail-summary-vector (vconcat (nreverse new-summary))
2995 rmail-msgref-vector (apply 'vector (nreverse new-msgref))
2996 win t)))
2997 (message "Expunging deleted messages...done")
2998 (if (not win)
2999 (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
3000 (rmail-show-message
3001 (if (zerop rmail-current-message) 1 nil))
3002 (if rmail-enable-mime
3003 (goto-char (+ (point-min) opoint))
3004 (goto-char (+ (point) opoint))))))
3006 (defun rmail-expunge ()
3007 "Erase deleted messages from Rmail file and summary buffer."
3008 (interactive)
3009 (when (rmail-expunge-confirmed)
3010 (rmail-only-expunge)
3011 (if (rmail-summary-exists)
3012 (rmail-select-summary (rmail-update-summary)))))
3014 ;;;; *** Rmail Mailing Commands ***
3016 (defun rmail-start-mail (&optional noerase to subject in-reply-to cc
3017 replybuffer sendactions same-window others)
3018 (let (yank-action)
3019 (if replybuffer
3020 (setq yank-action (list 'insert-buffer replybuffer)))
3021 (setq others (cons (cons "cc" cc) others))
3022 (setq others (cons (cons "in-reply-to" in-reply-to) others))
3023 (if same-window
3024 (compose-mail to subject others
3025 noerase nil
3026 yank-action sendactions)
3027 (if (and (display-multi-frame-p) rmail-mail-new-frame)
3028 (prog1
3029 (compose-mail to subject others
3030 noerase 'switch-to-buffer-other-frame
3031 yank-action sendactions)
3032 ;; This is not a standard frame parameter;
3033 ;; nothing except sendmail.el looks at it.
3034 (modify-frame-parameters (selected-frame)
3035 '((mail-dedicated-frame . t))))
3036 (compose-mail to subject others
3037 noerase 'switch-to-buffer-other-window
3038 yank-action sendactions)))))
3040 (defun rmail-mail ()
3041 "Send mail in another window.
3042 While composing the message, use \\[mail-yank-original] to yank the
3043 original message into it."
3044 (interactive)
3045 (rmail-start-mail nil nil nil nil nil rmail-view-buffer))
3047 (defun rmail-continue ()
3048 "Continue composing outgoing message previously being composed."
3049 (interactive)
3050 (rmail-start-mail t))
3052 (defun rmail-reply (just-sender)
3053 "Reply to the current message.
3054 Normally include CC: to all other recipients of original message;
3055 prefix argument means ignore them. While composing the reply,
3056 use \\[mail-yank-original] to yank the original message into it."
3057 (interactive "P")
3058 (let (from reply-to cc subject date to message-id references
3059 resent-to resent-cc resent-reply-to
3060 (msgnum rmail-current-message))
3061 (save-excursion
3062 (save-restriction
3063 (if rmail-enable-mime
3064 (narrow-to-region
3065 (goto-char (point-min))
3066 (if (search-forward "\n\n" nil 'move)
3067 (1+ (match-beginning 0))
3068 (point)))
3069 (widen)
3070 (goto-char (rmail-msgbeg rmail-current-message))
3071 (forward-line 1)
3072 (if (= (following-char) ?0)
3073 (narrow-to-region
3074 (progn (forward-line 2)
3075 (point))
3076 (progn (search-forward "\n\n" (rmail-msgend rmail-current-message)
3077 'move)
3078 (point)))
3079 (narrow-to-region (point)
3080 (progn (search-forward "\n*** EOOH ***\n")
3081 (beginning-of-line) (point)))))
3082 (setq from (mail-fetch-field "from")
3083 reply-to (or (mail-fetch-field "reply-to" nil t)
3084 from)
3085 cc (and (not just-sender)
3086 (mail-fetch-field "cc" nil t))
3087 subject (mail-fetch-field "subject")
3088 date (mail-fetch-field "date")
3089 to (or (mail-fetch-field "to" nil t) "")
3090 message-id (mail-fetch-field "message-id")
3091 references (mail-fetch-field "references" nil nil t)
3092 resent-reply-to (mail-fetch-field "resent-reply-to" nil t)
3093 resent-cc (and (not just-sender)
3094 (mail-fetch-field "resent-cc" nil t))
3095 resent-to (or (mail-fetch-field "resent-to" nil t) "")
3096 ;;; resent-subject (mail-fetch-field "resent-subject")
3097 ;;; resent-date (mail-fetch-field "resent-date")
3098 ;;; resent-message-id (mail-fetch-field "resent-message-id")
3100 ;; Merge the resent-to and resent-cc into the to and cc.
3101 (if (and resent-to (not (equal resent-to "")))
3102 (if (not (equal to ""))
3103 (setq to (concat to ", " resent-to))
3104 (setq to resent-to)))
3105 (if (and resent-cc (not (equal resent-cc "")))
3106 (if (not (equal cc ""))
3107 (setq cc (concat cc ", " resent-cc))
3108 (setq cc resent-cc)))
3109 ;; Add `Re: ' to subject if not there already.
3110 (and (stringp subject)
3111 (setq subject
3112 (concat rmail-reply-prefix
3113 (if (let ((case-fold-search t))
3114 (string-match rmail-reply-regexp subject))
3115 (substring subject (match-end 0))
3116 subject))))
3117 (rmail-start-mail
3119 ;; Using mail-strip-quoted-names is undesirable with newer mailers
3120 ;; since they can handle the names unstripped.
3121 ;; I don't know whether there are other mailers that still
3122 ;; need the names to be stripped.
3123 (mail-strip-quoted-names reply-to)
3124 subject
3125 (rmail-make-in-reply-to-field from date message-id)
3126 (if just-sender
3128 ;; mail-strip-quoted-names is NOT necessary for rmail-dont-reply-to
3129 ;; to do its job.
3130 (let* ((cc-list (rmail-dont-reply-to
3131 (mail-strip-quoted-names
3132 (if (null cc) to (concat to ", " cc))))))
3133 (if (string= cc-list "") nil cc-list)))
3134 rmail-view-buffer
3135 (list (list 'rmail-mark-message
3136 rmail-buffer
3137 (with-current-buffer rmail-buffer
3138 (aref rmail-msgref-vector msgnum))
3139 "answered"))
3141 (list (cons "References" (concat (mapconcat 'identity references " ")
3142 " " message-id))))))
3144 (defun rmail-mark-message (buffer msgnum-list attribute)
3145 "Give BUFFER's message number in MSGNUM-LIST the attribute ATTRIBUTE.
3146 This is use in the send-actions for message buffers.
3147 MSGNUM-LIST is a list of the form (MSGNUM)
3148 which is an element of rmail-msgref-vector."
3149 (save-excursion
3150 (set-buffer buffer)
3151 (if (car msgnum-list)
3152 (rmail-set-attribute attribute t (car msgnum-list)))))
3154 (defun rmail-make-in-reply-to-field (from date message-id)
3155 (cond ((not from)
3156 (if message-id
3157 message-id
3158 nil))
3159 (mail-use-rfc822
3160 (require 'rfc822)
3161 (let ((tem (car (rfc822-addresses from))))
3162 (if message-id
3163 (if (or (not tem)
3164 (string-match
3165 (regexp-quote (if (string-match "@[^@]*\\'" tem)
3166 (substring tem 0
3167 (match-beginning 0))
3168 tem))
3169 message-id))
3170 ;; missing From, or Message-ID is sufficiently informative
3171 message-id
3172 (concat message-id " (" tem ")"))
3173 ;; Copy TEM, discarding text properties.
3174 (setq tem (copy-sequence tem))
3175 (set-text-properties 0 (length tem) nil tem)
3176 (setq tem (copy-sequence tem))
3177 ;; Use prin1 to fake RFC822 quoting
3178 (let ((field (prin1-to-string tem)))
3179 (if date
3180 (concat field "'s message of " date)
3181 field)))))
3182 ((let* ((foo "[^][\000-\037()<>@,;:\\\" ]+")
3183 (bar "[^][\000-\037()<>@,;:\\\"]+"))
3184 ;; These strings both match all non-ASCII characters.
3185 (or (string-match (concat "\\`[ \t]*\\(" bar
3186 "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
3187 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
3188 from)
3189 (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
3190 bar "\\))[ \t]*\\'")
3191 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
3192 from)))
3193 (let ((start (match-beginning 1))
3194 (end (match-end 1)))
3195 ;; Trim whitespace which above regexp match allows
3196 (while (and (< start end)
3197 (memq (aref from start) '(?\t ?\ )))
3198 (setq start (1+ start)))
3199 (while (and (< start end)
3200 (memq (aref from (1- end)) '(?\t ?\ )))
3201 (setq end (1- end)))
3202 (let ((field (substring from start end)))
3203 (if date (setq field (concat "message from " field " on " date)))
3204 (if message-id
3205 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
3206 (concat message-id " (" field ")")
3207 field))))
3209 ;; If we can't kludge it simply, do it correctly
3210 (let ((mail-use-rfc822 t))
3211 (rmail-make-in-reply-to-field from date message-id)))))
3213 (defun rmail-forward (resend)
3214 "Forward the current message to another user.
3215 With prefix argument, \"resend\" the message instead of forwarding it;
3216 see the documentation of `rmail-resend'."
3217 (interactive "P")
3218 (if resend
3219 (call-interactively 'rmail-resend)
3220 (let ((forward-buffer rmail-buffer)
3221 (msgnum rmail-current-message)
3222 (subject (concat "["
3223 (let ((from (or (mail-fetch-field "From")
3224 (mail-fetch-field ">From"))))
3225 (if from
3226 (concat (mail-strip-quoted-names from) ": ")
3227 ""))
3228 (or (mail-fetch-field "Subject") "")
3229 "]")))
3230 (if (rmail-start-mail
3231 nil nil subject nil nil nil
3232 (list (list 'rmail-mark-message
3233 forward-buffer
3234 (with-current-buffer rmail-buffer
3235 (aref rmail-msgref-vector msgnum))
3236 "forwarded"))
3237 ;; If only one window, use it for the mail buffer.
3238 ;; Otherwise, use another window for the mail buffer
3239 ;; so that the Rmail buffer remains visible
3240 ;; and sending the mail will get back to it.
3241 (and (not rmail-mail-new-frame) (one-window-p t)))
3242 ;; The mail buffer is now current.
3243 (save-excursion
3244 ;; Insert after header separator--before signature if any.
3245 (goto-char (mail-text-start))
3246 (if rmail-enable-mime
3247 (funcall rmail-insert-mime-forwarded-message-function
3248 forward-buffer)
3249 (insert "------- Start of forwarded message -------\n")
3250 ;; Quote lines with `- ' if they start with `-'.
3251 (let ((beg (point)) end)
3252 (setq end (point-marker))
3253 (set-marker-insertion-type end t)
3254 (insert-buffer-substring forward-buffer)
3255 (goto-char beg)
3256 (while (re-search-forward "^-" end t)
3257 (beginning-of-line)
3258 (insert "- ")
3259 (forward-line 1))
3260 (goto-char end)
3261 (skip-chars-backward "\n")
3262 (if (< (point) end)
3263 (forward-char 1))
3264 (delete-region (point) end)
3265 (set-marker end nil))
3266 (insert "------- End of forwarded message -------\n"))
3267 (push-mark))))))
3269 (defun rmail-resend (address &optional from comment mail-alias-file)
3270 "Resend current message to ADDRESSES.
3271 ADDRESSES should be a single address, a string consisting of several
3272 addresses separated by commas, or a list of addresses.
3274 Optional FROM is the address to resend the message from, and
3275 defaults from the value of `user-mail-address'.
3276 Optional COMMENT is a string to insert as a comment in the resent message.
3277 Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
3278 typically for purposes of moderating a list."
3279 (interactive "sResend to: ")
3280 (require 'sendmail)
3281 (require 'mailalias)
3282 (unless (or (eq rmail-view-buffer (current-buffer))
3283 (eq rmail-buffer (current-buffer)))
3284 (error "Not an Rmail buffer"))
3285 (if (not from) (setq from user-mail-address))
3286 (let ((tembuf (generate-new-buffer " sendmail temp"))
3287 (case-fold-search nil)
3288 (mailbuf rmail-buffer))
3289 (unwind-protect
3290 (with-current-buffer tembuf
3291 ;;>> Copy message into temp buffer
3292 (if rmail-enable-mime
3293 (funcall rmail-insert-mime-resent-message-function mailbuf)
3294 (insert-buffer-substring mailbuf))
3295 (goto-char (point-min))
3296 ;; Delete any Sender field, since that's not specifiable.
3297 ; Only delete Sender fields in the actual header.
3298 (re-search-forward "^$" nil 'move)
3299 ; Using "while" here rather than "if" because some buggy mail
3300 ; software may have inserted multiple Sender fields.
3301 (while (re-search-backward "^Sender:" nil t)
3302 (let (beg)
3303 (setq beg (point))
3304 (forward-line 1)
3305 (while (looking-at "[ \t]")
3306 (forward-line 1))
3307 (delete-region beg (point))))
3308 ; Go back to the beginning of the buffer so the Resent- fields
3309 ; are inserted there.
3310 (goto-char (point-min))
3311 ;;>> Insert resent-from:
3312 (insert "Resent-From: " from "\n")
3313 (insert "Resent-Date: " (mail-rfc822-date) "\n")
3314 ;;>> Insert resent-to: and bcc if need be.
3315 (let ((before (point)))
3316 (if mail-self-blind
3317 (insert "Resent-Bcc: " (user-login-name) "\n"))
3318 (insert "Resent-To: " (if (stringp address)
3319 address
3320 (mapconcat 'identity address ",\n\t"))
3321 "\n")
3322 ;; Expand abbrevs in the recipients.
3323 (save-excursion
3324 (if (featurep 'mailabbrev)
3325 (let ((end (point-marker))
3326 (local-abbrev-table mail-abbrevs)
3327 (old-syntax-table (syntax-table)))
3328 (if (and (not (vectorp mail-abbrevs))
3329 (file-exists-p mail-personal-alias-file))
3330 (build-mail-abbrevs))
3331 (set-syntax-table mail-abbrev-syntax-table)
3332 (goto-char before)
3333 (while (and (< (point) end)
3334 (progn (forward-word 1)
3335 (<= (point) end)))
3336 (expand-abbrev))
3337 (set-syntax-table old-syntax-table))
3338 (expand-mail-aliases before (point)))))
3339 ;;>> Set up comment, if any.
3340 (if (and (sequencep comment) (not (zerop (length comment))))
3341 (let ((before (point))
3342 after)
3343 (insert comment)
3344 (or (eolp) (insert "\n"))
3345 (setq after (point))
3346 (goto-char before)
3347 (while (< (point) after)
3348 (insert "Resent-Comment: ")
3349 (forward-line 1))))
3350 ;; Don't expand aliases in the destination fields
3351 ;; of the original message.
3352 (let (mail-aliases)
3353 (funcall send-mail-function)))
3354 (kill-buffer tembuf))
3355 (with-current-buffer rmail-buffer
3356 (rmail-set-attribute "resent" t rmail-current-message))))
3358 (defvar mail-unsent-separator
3359 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
3360 "^ *---+ +Returned message +---+ *$\\|"
3361 "^ *---+ *Returned mail follows *---+ *$\\|"
3362 "^Start of returned message$\\|"
3363 "^---+ Below this line is a copy of the message.$\\|"
3364 "^ *---+ +Original message +---+ *$\\|"
3365 "^ *--+ +begin message +--+ *$\\|"
3366 "^ *---+ +Original message follows +---+ *$\\|"
3367 "^ *---+ +Your message follows +---+ *$\\|"
3368 "^|? *---+ +Message text follows: +---+ *|?$\\|"
3369 "^ *---+ +This is a copy of \\w+ message, including all the headers.*---+ *$")
3370 "A regexp that matches the separator before the text of a failed message.")
3372 (defvar mail-mime-unsent-header "^Content-Type: message/rfc822 *$"
3373 "A regexp that matches the header of a MIME body part with a failed message.")
3375 (defun rmail-retry-failure ()
3376 "Edit a mail message which is based on the contents of the current message.
3377 For a message rejected by the mail system, extract the interesting headers and
3378 the body of the original message.
3379 If the failed message is a MIME multipart message, it is searched for a
3380 body part with a header which matches the variable `mail-mime-unsent-header'.
3381 Otherwise, the variable `mail-unsent-separator' should match the string that
3382 delimits the returned original message.
3383 The variable `rmail-retry-ignored-headers' is a regular expression
3384 specifying headers which should not be copied into the new message."
3385 (interactive)
3386 (require 'mail-utils)
3387 (let ((rmail-this-buffer (current-buffer))
3388 (msgnum rmail-current-message)
3389 bounce-start bounce-end bounce-indent resending
3390 ;; Fetch any content-type header in current message
3391 ;; Must search thru the whole unpruned header.
3392 (content-type
3393 (save-excursion
3394 (save-restriction
3395 (rmail-narrow-to-non-pruned-header)
3396 (mail-fetch-field "Content-Type") ))))
3397 (save-excursion
3398 (goto-char (point-min))
3399 (let ((case-fold-search t))
3400 (if (and content-type
3401 (string-match
3402 ";[\n\t ]*boundary=\"?\\([-0-9a-z'()+_,./:=? ]+\\)\"?"
3403 content-type))
3404 ;; Handle a MIME multipart bounce message.
3405 (let ((codestring
3406 (concat "\n--"
3407 (substring content-type (match-beginning 1)
3408 (match-end 1)))))
3409 (unless (re-search-forward mail-mime-unsent-header nil t)
3410 (error "Cannot find beginning of header in failed message"))
3411 (unless (search-forward "\n\n" nil t)
3412 (error "Cannot find start of Mime data in failed message"))
3413 (setq bounce-start (point))
3414 (if (search-forward codestring nil t)
3415 (setq bounce-end (match-beginning 0))
3416 (setq bounce-end (point-max))))
3417 ;; Non-MIME bounce.
3418 (or (re-search-forward mail-unsent-separator nil t)
3419 (error "Cannot parse this as a failure message"))
3420 (skip-chars-forward "\n")
3421 ;; Support a style of failure message in which the original
3422 ;; message is indented, and included within lines saying
3423 ;; `Start of returned message' and `End of returned message'.
3424 (if (looking-at " +Received:")
3425 (progn
3426 (setq bounce-start (point))
3427 (skip-chars-forward " ")
3428 (setq bounce-indent (- (current-column)))
3429 (goto-char (point-max))
3430 (re-search-backward "^End of returned message$" nil t)
3431 (setq bounce-end (point)))
3432 ;; One message contained a few random lines before
3433 ;; the old message header. The first line of the
3434 ;; message started with two hyphens. A blank line
3435 ;; followed these random lines. The same line
3436 ;; beginning with two hyphens was possibly marking
3437 ;; the end of the message.
3438 (if (looking-at "^--")
3439 (let ((boundary (buffer-substring-no-properties
3440 (point)
3441 (progn (end-of-line) (point)))))
3442 (search-forward "\n\n")
3443 (skip-chars-forward "\n")
3444 (setq bounce-start (point))
3445 (goto-char (point-max))
3446 (search-backward (concat "\n\n" boundary) bounce-start t)
3447 (setq bounce-end (point)))
3448 (setq bounce-start (point)
3449 bounce-end (point-max)))
3450 (unless (search-forward "\n\n" nil t)
3451 (error "Cannot find end of header in failed message"))))))
3452 ;; We have found the message that bounced, within the current message.
3453 ;; Now start sending new message; default header fields from original.
3454 ;; Turn off the usual actions for initializing the message body
3455 ;; because we want to get only the text from the failure message.
3456 (let (mail-signature mail-setup-hook)
3457 (if (rmail-start-mail nil nil nil nil nil rmail-this-buffer
3458 (list (list 'rmail-mark-message
3459 rmail-this-buffer
3460 (aref rmail-msgref-vector msgnum)
3461 "retried")))
3462 ;; Insert original text as initial text of new draft message.
3463 ;; Bind inhibit-read-only since the header delimiter
3464 ;; of the previous message was probably read-only.
3465 (let ((inhibit-read-only t)
3466 rmail-displayed-headers
3467 rmail-ignored-headers)
3468 (erase-buffer)
3469 (insert-buffer-substring rmail-this-buffer
3470 bounce-start bounce-end)
3471 (goto-char (point-min))
3472 (if bounce-indent
3473 (indent-rigidly (point-min) (point-max) bounce-indent))
3474 (rmail-clear-headers rmail-retry-ignored-headers)
3475 (rmail-clear-headers "^sender:\\|^return-path:\\|^received:")
3476 (mail-sendmail-delimit-header)
3477 (save-restriction
3478 (narrow-to-region (point-min) (mail-header-end))
3479 (setq resending (mail-fetch-field "resent-to"))
3480 (if mail-self-blind
3481 (if resending
3482 (insert "Resent-Bcc: " (user-login-name) "\n")
3483 (insert "BCC: " (user-login-name) "\n"))))
3484 (goto-char (point-min))
3485 (mail-position-on-field (if resending "Resent-To" "To") t))))))
3487 (defun rmail-summary-exists ()
3488 "Non-nil iff in an RMAIL buffer and an associated summary buffer exists.
3489 In fact, the non-nil value returned is the summary buffer itself."
3490 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
3491 rmail-summary-buffer))
3493 (defun rmail-summary-displayed ()
3494 "t iff in RMAIL buffer and an associated summary buffer is displayed."
3495 (and rmail-summary-buffer (get-buffer-window rmail-summary-buffer)))
3497 (defcustom rmail-redisplay-summary nil
3498 "*Non-nil means Rmail should show the summary when it changes.
3499 This has an effect only if a summary buffer exists."
3500 :type 'boolean
3501 :group 'rmail-summary)
3503 (defcustom rmail-summary-window-size nil
3504 "*Non-nil means specify the height for an Rmail summary window."
3505 :type '(choice (const :tag "Disabled" nil) integer)
3506 :group 'rmail-summary)
3508 ;; Put the summary buffer back on the screen, if user wants that.
3509 (defun rmail-maybe-display-summary ()
3510 (let ((selected (selected-window))
3511 window)
3512 ;; If requested, make sure the summary is displayed.
3513 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
3514 rmail-redisplay-summary
3515 (if (get-buffer-window rmail-summary-buffer 0)
3516 ;; It's already in some frame; show that one.
3517 (let ((frame (window-frame
3518 (get-buffer-window rmail-summary-buffer 0))))
3519 (make-frame-visible frame)
3520 (raise-frame frame))
3521 (display-buffer rmail-summary-buffer)))
3522 ;; If requested, set the height of the summary window.
3523 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
3524 rmail-summary-window-size
3525 (setq window (get-buffer-window rmail-summary-buffer))
3526 ;; Don't try to change the size if just one window in frame.
3527 (not (eq window (frame-root-window (window-frame window))))
3528 (unwind-protect
3529 (progn
3530 (select-window window)
3531 (enlarge-window (- rmail-summary-window-size (window-height))))
3532 (select-window selected)))))
3534 ;;;; *** Rmail Local Fontification ***
3536 (defun rmail-fontify-buffer-function ()
3537 ;; This function's symbol is bound to font-lock-fontify-buffer-function.
3538 (make-local-hook 'rmail-show-message-hook)
3539 (add-hook 'rmail-show-message-hook 'rmail-fontify-message nil t)
3540 ;; If we're already showing a message, fontify it now.
3541 (if rmail-current-message (rmail-fontify-message))
3542 ;; Prevent Font Lock mode from kicking in.
3543 (setq font-lock-fontified t))
3545 (defun rmail-unfontify-buffer-function ()
3546 ;; This function's symbol is bound to font-lock-fontify-unbuffer-function.
3547 (let ((modified (buffer-modified-p))
3548 (buffer-undo-list t) (inhibit-read-only t)
3549 before-change-functions after-change-functions
3550 buffer-file-name buffer-file-truename)
3551 (save-restriction
3552 (widen)
3553 (remove-hook 'rmail-show-message-hook 'rmail-fontify-message t)
3554 (remove-text-properties (point-min) (point-max) '(rmail-fontified nil))
3555 (font-lock-default-unfontify-buffer)
3556 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))
3558 (defun rmail-fontify-message ()
3559 ;; Fontify the current message if it is not already fontified.
3560 (if (text-property-any (point-min) (point-max) 'rmail-fontified nil)
3561 (let ((modified (buffer-modified-p))
3562 (buffer-undo-list t) (inhibit-read-only t)
3563 before-change-functions after-change-functions
3564 buffer-file-name buffer-file-truename)
3565 (save-excursion
3566 (save-match-data
3567 (add-text-properties (point-min) (point-max) '(rmail-fontified t))
3568 (font-lock-fontify-region (point-min) (point-max))
3569 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))))
3571 ;;; Speedbar support for RMAIL files.
3572 (eval-when-compile (require 'speedbar))
3574 (defvar rmail-speedbar-match-folder-regexp "^[A-Z0-9]+\\(\\.[A-Z0-9]+\\)?$"
3575 "*This regex us used to match folder names to be displayed in speedbar.
3576 Enabling this will permit speedbar to display your folders for easy
3577 browsing, and moving of messages.")
3579 (defvar rmail-speedbar-last-user nil
3580 "The last user to be displayed in the speedbar.")
3582 (defvar rmail-speedbar-key-map nil
3583 "Keymap used when in rmail display mode.")
3585 (defun rmail-install-speedbar-variables ()
3586 "Install those variables used by speedbar to enhance rmail."
3587 (if rmail-speedbar-key-map
3589 (setq rmail-speedbar-key-map (speedbar-make-specialized-keymap))
3591 (define-key rmail-speedbar-key-map "e" 'speedbar-edit-line)
3592 (define-key rmail-speedbar-key-map "r" 'speedbar-edit-line)
3593 (define-key rmail-speedbar-key-map "\C-m" 'speedbar-edit-line)
3594 (define-key rmail-speedbar-key-map "M"
3595 'rmail-speedbar-move-message-to-folder-on-line)))
3597 (defvar rmail-speedbar-menu-items
3598 '(["Read Folder" speedbar-edit-line t]
3599 ["Move message to folder" rmail-speedbar-move-message-to-folder-on-line
3600 (save-excursion (beginning-of-line)
3601 (looking-at "<M> "))])
3602 "Additional menu-items to add to speedbar frame.")
3604 ;; Make sure our special speedbar major mode is loaded
3605 (if (featurep 'speedbar)
3606 (rmail-install-speedbar-variables)
3607 (add-hook 'speedbar-load-hook 'rmail-install-speedbar-variables))
3609 (defun rmail-speedbar-buttons (buffer)
3610 "Create buttons for BUFFER containing rmail messages.
3611 Click on the address under Reply to: to reply to this person.
3612 Under Folders: Click a name to read it, or on the <M> to move the
3613 current message into that RMAIL folder."
3614 (let ((from nil))
3615 (save-excursion
3616 (set-buffer buffer)
3617 (goto-char (point-min))
3618 (if (not (re-search-forward "^Reply-To: " nil t))
3619 (if (not (re-search-forward "^From:? " nil t))
3620 (setq from t)))
3621 (if from
3623 (setq from (buffer-substring (point) (save-excursion
3624 (end-of-line)
3625 (point))))))
3626 (goto-char (point-min))
3627 (if (and (looking-at "Reply to:")
3628 (equal from rmail-speedbar-last-user))
3630 (setq rmail-speedbar-last-user from)
3631 (erase-buffer)
3632 (insert "Reply To:\n")
3633 (if (stringp from)
3634 (speedbar-insert-button from 'speedbar-directory-face 'highlight
3635 'rmail-speedbar-button 'rmail-reply))
3636 (insert "Folders:\n")
3637 (let* ((case-fold-search nil)
3638 (df (directory-files (save-excursion (set-buffer buffer)
3639 default-directory)
3640 nil rmail-speedbar-match-folder-regexp)))
3641 (while df
3642 (speedbar-insert-button "<M>" 'speedbar-button-face 'highlight
3643 'rmail-speedbar-move-message (car df))
3644 (speedbar-insert-button (car df) 'speedbar-file-face 'highlight
3645 'rmail-speedbar-find-file nil t)
3646 (setq df (cdr df)))))))
3648 (defun rmail-speedbar-button (text token indent)
3649 "Execute an rmail command specified by TEXT.
3650 The command used is TOKEN. INDENT is not used."
3651 (speedbar-with-attached-buffer
3652 (funcall token t)))
3654 (defun rmail-speedbar-find-file (text token indent)
3655 "Load in the rmail file TEXT.
3656 TOKEN and INDENT are not used."
3657 (speedbar-with-attached-buffer
3658 (message "Loading in RMAIL file %s..." text)
3659 (find-file text)))
3661 (defun rmail-speedbar-move-message-to-folder-on-line ()
3662 "If the current line is a folder, move current message to it."
3663 (interactive)
3664 (save-excursion
3665 (beginning-of-line)
3666 (if (re-search-forward "<M> " (save-excursion (end-of-line) (point)) t)
3667 (progn
3668 (forward-char -2)
3669 (speedbar-do-function-pointer)))))
3671 (defun rmail-speedbar-move-message (text token indent)
3672 "From button TEXT, copy current message to the rmail file specified by TOKEN.
3673 TEXT and INDENT are not used."
3674 (speedbar-with-attached-buffer
3675 (message "Moving message to %s" token)
3676 (rmail-output-to-rmail-file token)))
3678 ; Functions for setting, getting and encoding the POP password.
3679 ; The password is encoded to prevent it from being easily accessible
3680 ; to "prying eyes." Obviously, this encoding isn't "real security,"
3681 ; nor is it meant to be.
3683 ;;;###autoload
3684 (defun rmail-set-pop-password (password)
3685 "Set PASSWORD to be used for retrieving mail from a POP server."
3686 (interactive "sPassword: ")
3687 (if password
3688 (setq rmail-encoded-pop-password
3689 (rmail-encode-string password (emacs-pid)))
3690 (setq rmail-pop-password nil)
3691 (setq rmail-encoded-pop-password nil)))
3693 (defun rmail-get-pop-password ()
3694 "Get the password for retrieving mail from a POP server. If none
3695 has been set, then prompt the user for one."
3696 (if (not rmail-encoded-pop-password)
3697 (progn (if (not rmail-pop-password)
3698 (setq rmail-pop-password (read-passwd "POP password: ")))
3699 (rmail-set-pop-password rmail-pop-password)
3700 (setq rmail-pop-password nil)))
3701 (rmail-encode-string rmail-encoded-pop-password (emacs-pid)))
3703 (defun rmail-have-password ()
3704 (or rmail-pop-password rmail-encoded-pop-password))
3706 (defun rmail-encode-string (string mask)
3707 "Encode STRING with integer MASK, by taking the exclusive OR of the
3708 lowest byte in the mask with the first character of string, the
3709 second-lowest-byte with the second character of the string, etc.,
3710 restarting at the lowest byte of the mask whenever it runs out.
3711 Returns the encoded string. Calling the function again with an
3712 encoded string (and the same mask) will decode the string."
3713 (setq mask (abs mask)) ; doesn't work if negative
3714 (let* ((string-vector (string-to-vector string)) (i 0)
3715 (len (length string-vector)) (curmask mask) charmask)
3716 (while (< i len)
3717 (if (= curmask 0)
3718 (setq curmask mask))
3719 (setq charmask (% curmask 256))
3720 (setq curmask (lsh curmask -8))
3721 (aset string-vector i (logxor charmask (aref string-vector i)))
3722 (setq i (1+ i)))
3723 (concat string-vector)))
3725 (provide 'rmail)
3727 ;;; rmail.el ends here