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