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