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.
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)
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.
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.
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")
55 "Mail reader for Emacs."
58 (defgroup rmail-retrieve nil
59 "Rmail retrieval options."
63 (defgroup rmail-files nil
68 (defgroup rmail-headers nil
69 "Rmail header options."
73 (defgroup rmail-reply nil
74 "Rmail reply options."
78 (defgroup rmail-summary nil
79 "Rmail summary options."
81 :prefix "rmail-summary-"
84 (defgroup rmail-output nil
85 "Output message to a file."
86 :prefix "rmail-output-"
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."
103 :group 'rmail-retrieve)
105 (defcustom rmail-preserve-inbox nil
106 "*Non-nil if incoming mail should be left in the user's inbox,
107 rather than deleted, after it is retrieved."
109 :group 'rmail-retrieve)
112 (defcustom rmail-dont-reply-to-names nil "\
113 *A regexp specifying names to prune of reply to messages.
114 A value of nil means exclude your own name only."
115 :type '(choice regexp (const :tag "Your Name" nil))
119 (defvar rmail-default-dont-reply-to-names "info-" "\
120 A regular expression specifying part of the value of the default value of
121 the variable `rmail-dont-reply-to-names', for when the user does not set
122 `rmail-dont-reply-to-names' explicitly. (The other part of the default
123 value is the user's name.)
124 It is useful to set this variable in the site customization file.")
127 (defcustom rmail-ignored-headers "^via:\\|^mail-from:\\|^origin:\\|^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:"
128 "*Regexp to match header fields that Rmail should normally hide."
130 :group 'rmail-headers)
133 (defcustom rmail-displayed-headers nil
134 "*Regexp to match Header fields that Rmail should display.
135 If nil, display all header fields except those matched by
136 `rmail-ignored-headers'."
137 :type '(choice regexp (const :tag "All"))
138 :group 'rmail-headers)
141 (defcustom rmail-retry-ignored-headers nil "\
142 *Headers that should be stripped when retrying a failed message."
143 :type '(choice regexp (const nil :tag "None"))
144 :group 'rmail-headers)
147 (defcustom rmail-highlighted-headers "^From:\\|^Subject:" "\
148 *Regexp to match Header fields that Rmail should normally highlight.
149 A value of nil means don't highlight.
150 See also `rmail-highlight-face'."
152 :group 'rmail-headers)
155 (defcustom rmail-highlight-face nil "\
156 *Face used by Rmail for highlighting headers."
157 :type '(choice (const :tag "Default" nil)
159 :group 'rmail-headers)
162 (defcustom rmail-delete-after-output nil "\
163 *Non-nil means automatically delete a message that is copied to a file."
168 (defcustom rmail-primary-inbox-list nil "\
169 *List of files which are inboxes for user's primary mail file `~/RMAIL'.
170 `nil' means the default, which is (\"/usr/spool/mail/$USER\")
171 \(the name varies depending on the operating system,
172 and the value of the environment variable MAIL overrides it)."
173 :type `(choice (const :tag "Default" nil)
174 (repeat :value (,(or (getenv "MAIL")
175 (concat "/var/spool/mail/"
178 :group 'rmail-retrieve
182 (defcustom rmail-mail-new-frame nil
183 "*Non-nil means Rmail makes a new frame for composing outgoing mail."
188 (defcustom rmail-secondary-file-directory "~/"
189 "*Directory for additional secondary Rmail files."
193 (defcustom rmail-secondary-file-regexp "\\.xmail$"
194 "*Regexp for which files are secondary Rmail files."
199 (defvar rmail-mode-hook nil
200 "List of functions to call when Rmail is invoked.")
203 (defvar rmail-get-new-mail-hook nil
204 "List of functions to call when Rmail has retrieved new mail.")
207 (defvar rmail-show-message-hook nil
208 "List of functions to call when Rmail displays a message.")
211 (defvar rmail-delete-message-hook nil
212 "List of functions to call when Rmail deletes a message.
213 When the hooks are called, the message has been marked deleted but is
214 still the current message in the Rmail buffer.")
216 ;; These may be altered by site-init.el to match the format of mmdf files
217 ;; delimiting used on a given host (delim1 and delim2 from the config
220 (defvar mmdf-delim1 "^\001\001\001\001\n"
221 "Regexp marking the start of an mmdf message")
222 (defvar mmdf-delim2 "^\001\001\001\001\n"
223 "Regexp marking the end of an mmdf message")
225 (defvar rmail-message-filter nil
226 "If non-nil, a filter function for new messages in RMAIL.
227 Called with region narrowed to the message, including headers,
228 before obeying `rmail-ignored-headers'.")
230 (defvar rmail-reply-prefix "Re: "
231 "String to prepend to Subject line when replying to a message.")
233 ;; Some mailers use "Re(2):" or "Re^2:" or "Re: Re:" or "Re[2]:".
234 ;; This pattern should catch all the common variants.
235 (defvar rmail-reply-regexp "\\`\\(Re\\(([0-9]+)\\|\\[[0-9]+\\]\\|\\^[0-9]+\\)?: *\\)*"
236 "Regexp to delete from Subject line before inserting `rmail-reply-prefix'.")
238 (defvar rmail-display-summary nil
239 "If non-nil, Rmail always displays the summary buffer.")
241 (defvar rmail-mode-map nil)
243 (defvar rmail-inbox-list nil)
244 (defvar rmail-keywords nil)
246 ;; Message counters and markers. Deleted flags.
248 (defvar rmail-current-message nil)
249 (defvar rmail-total-messages nil)
250 (defvar rmail-message-vector nil)
251 (defvar rmail-deleted-vector nil)
252 (defvar rmail-msgref-vector nil
253 "In an Rmail buffer, a vector whose Nth element is a list (N).
254 When expunging renumbers messages, these lists are modified
255 by substituting the new message number into the existing list.")
257 (defvar rmail-overlay-list nil)
259 (defvar rmail-font-lock-keywords
261 (let* ((cite-chars "[>|}]")
262 (cite-prefix "A-Za-z")
263 (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
264 (list '("^\\(From\\|Sender\\):" . font-lock-function-name-face)
265 '("^Reply-To:.*$" . font-lock-function-name-face)
266 '("^Subject:" . font-lock-comment-face)
267 '("^\\(To\\|Apparently-To\\|Cc\\|Newsgroups\\):"
268 . font-lock-keyword-face)
269 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
271 (,(concat "\\=[ \t]*"
272 "\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
273 "\\(" cite-chars "[ \t]*\\)\\)+"
275 (beginning-of-line) (end-of-line)
276 (2 font-lock-reference-face nil t)
277 (4 font-lock-comment-face nil t)))
278 '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\|Date\\):.*$"
279 . font-lock-string-face))))
280 "Additional expressions to highlight in Rmail mode.")
282 ;; These are used by autoloaded rmail-summary.
284 (defvar rmail-summary-buffer nil)
285 (defvar rmail-summary-vector nil)
287 ;; `Sticky' default variables.
289 ;; Last individual label specified to a or k.
290 (defvar rmail-last-label nil)
291 ;; Last set of values specified to C-M-n, C-M-p, C-M-s or C-M-l.
292 (defvar rmail-last-multi-labels nil)
293 (defvar rmail-last-regexp nil)
294 (defcustom rmail-default-file "~/xmail"
295 "*Default file name for \\[rmail-output]."
298 (defcustom rmail-default-rmail-file "~/XMAIL"
299 "*Default file name for \\[rmail-output-to-rmail-file]."
303 ;;; Regexp matching the delimiter of messages in UNIX mail format
304 ;;; (UNIX From lines), minus the initial ^. Note that if you change
305 ;;; this expression, you must change the code in rmail-nuke-pinhead-header
306 ;;; that knows the exact ordering of the \\( \\) subexpressions.
307 (defvar rmail-unix-mail-delimiter
308 (let ((time-zone-regexp
309 (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
310 "\\|[-+]?[0-9][0-9][0-9][0-9]"
316 ;; Many things can happen to an RFC 822 mailbox before it is put into
317 ;; a `From' line. The leading phrase can be stripped, e.g.
318 ;; `Joe <@w.x:joe@y.z>' -> `<@w.x:joe@y.z>'. The <> can be stripped, e.g.
319 ;; `<@x.y:joe@y.z>' -> `@x.y:joe@y.z'. Everything starting with a CRLF
320 ;; can be removed, e.g.
321 ;; From: joe@y.z (Joe K
323 ;; can yield `From joe@y.z (Joe K Fri Mar 22 08:11:15 1996', and
326 ;; can yield `From Joe User Fri Mar 22 08:11:15 1996'.
327 ;; The mailbox can be removed or be replaced by white space, e.g.
328 ;; From: "Joe User"{space}{tab}
330 ;; can yield `From {space}{tab} Fri Mar 22 08:11:15 1996',
331 ;; where {space} and {tab} represent the Ascii space and tab characters.
332 ;; We want to match the results of any of these manglings.
333 ;; The following regexp rejects names whose first characters are
334 ;; obviously bogus, but after that anything goes.
335 "\\([^\0-\b\n-\r\^?].*\\)? "
337 ;; The time the message was sent.
338 "\\([^\0-\r \^?]+\\) +" ; day of the week
339 "\\([^\0-\r \^?]+\\) +" ; month
340 "\\([0-3]?[0-9]\\) +" ; day of month
341 "\\([0-2][0-9]:[0-5][0-9]\\(:[0-6][0-9]\\)?\\) *" ; time of day
343 ;; Perhaps a time zone, specified by an abbreviation, or by a
348 " \\([0-9][0-9]+\\) *"
350 ;; On some systems the time zone can appear after the year, too.
354 "\\(remote from .*\\)?"
359 ;; Perform BODY in the summary buffer
360 ;; in such a way that its cursor is properly updated in its own window.
361 (defmacro rmail-select-summary (&rest body)
362 (` (let ((total rmail-total-messages))
363 (if (rmail-summary-displayed)
364 (let ((window (selected-window)))
368 (pop-to-buffer rmail-summary-buffer)
369 ;; rmail-total-messages is a buffer-local var
370 ;; in the rmail buffer.
371 ;; This way we make it available for the body
372 ;; even tho the rmail buffer is not current.
373 (let ((rmail-total-messages total))
375 (select-window window))))
377 (set-buffer rmail-summary-buffer)
378 (let ((rmail-total-messages total))
380 (rmail-maybe-display-summary))))
382 (defvar rmail-view-buffer nil
383 "Buffer which holds RMAIL message for displaying.")
385 ;; Mule and MIME related variables.
388 (defvar rmail-file-coding-system nil
389 "Coding system used in RMAIL file.
391 This is set to nil by default.")
394 (defcustom rmail-enable-mime nil
395 "*If non-nil, RMAIL uses MIME feature.
396 If the value is t, RMAIL automatically shows MIME decoded message.
397 If the value is neither t nor nil, RMAIL does not show MIME decoded message
398 until a user explicitly require it."
399 :type '(choice (const :tag "on" t)
400 (const :tag "off" nil)
401 (sexp :tag "When Asked" :format "%t\n" ask))
405 (defvar rmail-show-mime-function nil
406 "Function to show MIME decoded message of RMAIL file.")
409 (defvar rmail-mime-feature 'rmail-mime
410 "Feature to provide for using MIME feature in RMAIL.
411 When starting rmail, this feature is requrired if rmail-enable-mime
415 ;;;; *** Rmail Mode ***
418 (defun rmail (&optional file-name-arg)
419 "Read and edit incoming mail.
420 Moves messages into file named by `rmail-file-name' (a babyl format file)
421 and edits that file in RMAIL Mode.
422 Type \\[describe-mode] once editing that file, for a list of RMAIL commands.
424 May be called with file name as argument; then performs rmail editing on
425 that file, but does not copy any new mail into the file.
426 Interactively, if you supply a prefix argument, then you
427 have a chance to specify a file name with the minibuffer.
429 If `rmail-display-summary' is non-nil, make a summary for this RMAIL file."
430 (interactive (if current-prefix-arg
431 (list (read-file-name "Run rmail on RMAIL file: "))))
432 (if rmail-enable-mime
434 (require rmail-mime-feature)
435 (error (message "Feature `%s' not provided" rmail-mime-feature)
437 (setq rmail-enable-mime nil))))
438 (let* ((file-name (expand-file-name (or file-name-arg rmail-file-name)))
439 (existed (get-file-buffer file-name))
440 (coding-system-for-read 'no-conversion)
442 ;; Like find-file, but in the case where a buffer existed
443 ;; and the file was reverted, recompute the message-data.
444 (if (and existed (not (verify-visited-file-modtime existed)))
446 ;; Don't be confused by apparent local-variables spec
447 ;; in the last message in the RMAIL file.
448 (let ((enable-local-variables nil))
449 (find-file file-name))
450 (if (and (verify-visited-file-modtime existed)
451 (eq major-mode 'rmail-mode))
452 (progn (rmail-forget-messages)
453 (rmail-set-message-counters))))
454 (let ((enable-local-variables nil))
455 (find-file file-name)))
456 (if (eq major-mode 'rmail-edit-mode)
457 (error "Exit Rmail Edit mode before getting new mail."))
458 (if (and existed (> (buffer-size) 0))
459 ;; Buffer not new and not empty; ensure in proper mode, but that's all.
460 (or (eq major-mode 'rmail-mode)
461 (progn (rmail-mode-2)
462 (setq run-mail-hook t)))
463 (setq run-mail-hook t)
465 ;; Convert all or part to Babyl file if possible.
467 (goto-char (point-max))
468 (if (null rmail-inbox-list)
470 (rmail-set-message-counters)
471 (rmail-show-message))))
472 (or (and (null file-name-arg)
473 (rmail-get-new-mail))
474 (rmail-show-message (rmail-first-unseen-message)))
475 (if rmail-display-summary (rmail-summary))
476 (rmail-construct-io-menu)
478 (run-hooks 'rmail-mode-hook))))
480 ;; Given the value of MAILPATH, return a list of inbox file names.
481 ;; This is turned off because it is not clear that the user wants
482 ;; all these inboxes to feed into the primary rmail file.
483 ; (defun rmail-convert-mailpath (string)
485 ; (while (setq idx (string-match "[%:]" string))
486 ; (let ((this (substring string 0 idx)))
487 ; (setq string (substring string (1+ idx)))
488 ; (setq list (cons (if (string-match "%" this)
489 ; (substring this 0 (string-match "%" this))
494 ; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
495 ; will not cause emacs 18.55 problems.
497 (defun rmail-convert-file ()
500 (goto-char (point-min))
501 ;; If file doesn't start like a Babyl file,
502 ;; convert it to one, by adding a header and converting each message.
503 (cond ((looking-at "BABYL OPTIONS:"))
504 ((looking-at "Version: 5\n")
505 ;; Losing babyl file made by old version of Rmail.
506 ;; Just fix the babyl file header; don't make a new one,
507 ;; so we don't lose the Labels: file attribute, etc.
508 (let ((buffer-read-only nil))
509 (insert "BABYL OPTIONS: -*- rmail -*-\n")))
510 ((equal (point-min) (point-max))
511 ;; Empty RMAIL file. Just insert the header.
512 (rmail-insert-rmail-file-header))
514 ;; Non-empty file in non-RMAIL format. Add header and convert.
516 (rmail-insert-rmail-file-header)))
517 (if (and (not convert)
518 (not rmail-enable-mime)
519 rmail-file-coding-system)
520 ;; Decode BABYL part at the headq only. The remaining non
521 ;; BABYL parts are decode in rmail-convert-to-babyl-format if
523 (rmail-decode-babyl-format))
524 ;; If file was not a Babyl file or if there are
525 ;; Unix format messages added at the end,
526 ;; convert file as necessary.
529 (goto-char (point-max))
530 (search-backward "\n\^_")
532 (looking-at "\n*From ")))
533 (let ((buffer-read-only nil))
534 (message "Converting to Babyl format...")
535 ;; If file needs conversion, convert it all,
536 ;; except for the BABYL header.
537 ;; (rmail-convert-to-babyl-format would delete the header.)
538 (goto-char (point-min))
539 (search-forward "\n\^_" nil t)
540 (narrow-to-region (point) (point-max))
541 (rmail-convert-to-babyl-format)
542 (message "Converting to Babyl format...done")))))
544 ;;; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
545 ;;; will not cause emacs 18.55 problems.
547 (defun rmail-insert-rmail-file-header ()
548 (let ((buffer-read-only nil))
549 (insert "BABYL OPTIONS: -*- rmail -*-
552 Note: This is the header of an rmail file.
553 Note: If you are seeing it in rmail,
554 Note: it means the file has no messages in it.\n\^_")))
556 ;; Decode Babyl formated part at the head of current buffer by
557 ;; rmail-file-coding-system.
559 (defun rmail-decode-babyl-format ()
560 (let ((modifiedp (buffer-modified-p))
561 (buffer-read-only nil)
563 (goto-char (point-min))
564 (search-forward "\n\^_" nil t) ; Skip BYBYL header.
566 (message "Decoding messages...")
567 (while (search-forward "\n\^_" nil t)
568 (decode-coding-region pos (point) rmail-file-coding-system)
570 (message "Decoding messages...done")
571 (set-buffer-file-coding-system rmail-file-coding-system)
572 (set-buffer-modified-p modifiedp)))
576 (setq rmail-mode-map (make-keymap))
577 (suppress-keymap rmail-mode-map)
578 (define-key rmail-mode-map "a" 'rmail-add-label)
579 (define-key rmail-mode-map "b" 'rmail-bury)
580 (define-key rmail-mode-map "c" 'rmail-continue)
581 (define-key rmail-mode-map "d" 'rmail-delete-forward)
582 (define-key rmail-mode-map "\C-d" 'rmail-delete-backward)
583 (define-key rmail-mode-map "e" 'rmail-edit-current-message)
584 (define-key rmail-mode-map "f" 'rmail-forward)
585 (define-key rmail-mode-map "g" 'rmail-get-new-mail)
586 (define-key rmail-mode-map "h" 'rmail-summary)
587 (define-key rmail-mode-map "i" 'rmail-input)
588 (define-key rmail-mode-map "j" 'rmail-show-message)
589 (define-key rmail-mode-map "k" 'rmail-kill-label)
590 (define-key rmail-mode-map "l" 'rmail-summary-by-labels)
591 (define-key rmail-mode-map "\e\C-h" 'rmail-summary)
592 (define-key rmail-mode-map "\e\C-l" 'rmail-summary-by-labels)
593 (define-key rmail-mode-map "\e\C-r" 'rmail-summary-by-recipients)
594 (define-key rmail-mode-map "\e\C-s" 'rmail-summary-by-regexp)
595 (define-key rmail-mode-map "\e\C-t" 'rmail-summary-by-topic)
596 (define-key rmail-mode-map "m" 'rmail-mail)
597 (define-key rmail-mode-map "\em" 'rmail-retry-failure)
598 (define-key rmail-mode-map "n" 'rmail-next-undeleted-message)
599 (define-key rmail-mode-map "\en" 'rmail-next-message)
600 (define-key rmail-mode-map "\e\C-n" 'rmail-next-labeled-message)
601 (define-key rmail-mode-map "o" 'rmail-output-to-rmail-file)
602 (define-key rmail-mode-map "\C-o" 'rmail-output)
603 (define-key rmail-mode-map "p" 'rmail-previous-undeleted-message)
604 (define-key rmail-mode-map "\ep" 'rmail-previous-message)
605 (define-key rmail-mode-map "\e\C-p" 'rmail-previous-labeled-message)
606 (define-key rmail-mode-map "q" 'rmail-quit)
607 (define-key rmail-mode-map "r" 'rmail-reply)
608 ;; I find I can't live without the default M-r command -- rms.
609 ;; (define-key rmail-mode-map "\er" 'rmail-search-backwards)
610 (define-key rmail-mode-map "s" 'rmail-expunge-and-save)
611 (define-key rmail-mode-map "\es" 'rmail-search)
612 (define-key rmail-mode-map "t" 'rmail-toggle-header)
613 (define-key rmail-mode-map "u" 'rmail-undelete-previous-message)
614 (define-key rmail-mode-map "w" 'rmail-output-body-to-file)
615 (define-key rmail-mode-map "x" 'rmail-expunge)
616 (define-key rmail-mode-map "." 'rmail-beginning-of-message)
617 (define-key rmail-mode-map "<" 'rmail-first-message)
618 (define-key rmail-mode-map ">" 'rmail-last-message)
619 (define-key rmail-mode-map " " 'scroll-up)
620 (define-key rmail-mode-map "\177" 'scroll-down)
621 (define-key rmail-mode-map "?" 'describe-mode)
622 (define-key rmail-mode-map "\C-c\C-s\C-d" 'rmail-sort-by-date)
623 (define-key rmail-mode-map "\C-c\C-s\C-s" 'rmail-sort-by-subject)
624 (define-key rmail-mode-map "\C-c\C-s\C-a" 'rmail-sort-by-author)
625 (define-key rmail-mode-map "\C-c\C-s\C-r" 'rmail-sort-by-recipient)
626 (define-key rmail-mode-map "\C-c\C-s\C-c" 'rmail-sort-by-correspondent)
627 (define-key rmail-mode-map "\C-c\C-s\C-l" 'rmail-sort-by-lines)
628 (define-key rmail-mode-map "\C-c\C-s\C-k" 'rmail-sort-by-keywords)
629 (define-key rmail-mode-map "\C-c\C-n" 'rmail-next-same-subject)
630 (define-key rmail-mode-map "\C-c\C-p" 'rmail-previous-same-subject)
633 (define-key rmail-mode-map [menu-bar] (make-sparse-keymap))
635 (define-key rmail-mode-map [menu-bar classify]
636 (cons "Classify" (make-sparse-keymap "Classify")))
638 (define-key rmail-mode-map [menu-bar classify input-menu]
641 (define-key rmail-mode-map [menu-bar classify output-menu]
644 (define-key rmail-mode-map [menu-bar classify output-body]
645 '("Output body to file..." . rmail-output-body-to-file))
647 (define-key rmail-mode-map [menu-bar classify output-inbox]
648 '("Output (inbox)..." . rmail-output))
650 (define-key rmail-mode-map [menu-bar classify output]
651 '("Output (Rmail)..." . rmail-output-to-rmail-file))
653 (define-key rmail-mode-map [menu-bar classify kill-label]
654 '("Kill Label..." . rmail-kill-label))
656 (define-key rmail-mode-map [menu-bar classify add-label]
657 '("Add Label..." . rmail-add-label))
659 (define-key rmail-mode-map [menu-bar summary]
660 (cons "Summary" (make-sparse-keymap "Summary")))
662 (define-key rmail-mode-map [menu-bar summary senders]
663 '("By Senders..." . rmail-summary-by-senders))
665 (define-key rmail-mode-map [menu-bar summary labels]
666 '("By Labels..." . rmail-summary-by-labels))
668 (define-key rmail-mode-map [menu-bar summary recipients]
669 '("By Recipients..." . rmail-summary-by-recipients))
671 (define-key rmail-mode-map [menu-bar summary topic]
672 '("By Topic..." . rmail-summary-by-topic))
674 (define-key rmail-mode-map [menu-bar summary regexp]
675 '("By Regexp..." . rmail-summary-by-regexp))
677 (define-key rmail-mode-map [menu-bar summary all]
678 '("All" . rmail-summary))
680 (define-key rmail-mode-map [menu-bar mail]
681 (cons "Mail" (make-sparse-keymap "Mail")))
683 (define-key rmail-mode-map [menu-bar mail rmail-get-new-mail]
684 '("Get New Mail" . rmail-get-new-mail))
686 (define-key rmail-mode-map [menu-bar mail lambda]
689 (define-key rmail-mode-map [menu-bar mail continue]
690 '("Continue" . rmail-continue))
692 (define-key rmail-mode-map [menu-bar mail resend]
693 '("Re-send..." . rmail-resend))
695 (define-key rmail-mode-map [menu-bar mail forward]
696 '("Forward" . rmail-forward))
698 (define-key rmail-mode-map [menu-bar mail retry]
699 '("Retry" . rmail-retry-failure))
701 (define-key rmail-mode-map [menu-bar mail reply]
702 '("Reply" . rmail-reply))
704 (define-key rmail-mode-map [menu-bar mail mail]
705 '("Mail" . rmail-mail))
707 (define-key rmail-mode-map [menu-bar delete]
708 (cons "Delete" (make-sparse-keymap "Delete")))
710 (define-key rmail-mode-map [menu-bar delete expunge/save]
711 '("Expunge/Save" . rmail-expunge-and-save))
713 (define-key rmail-mode-map [menu-bar delete expunge]
714 '("Expunge" . rmail-expunge))
716 (define-key rmail-mode-map [menu-bar delete undelete]
717 '("Undelete" . rmail-undelete-previous-message))
719 (define-key rmail-mode-map [menu-bar delete delete]
720 '("Delete" . rmail-delete-forward))
722 (define-key rmail-mode-map [menu-bar move]
723 (cons "Move" (make-sparse-keymap "Move")))
725 (define-key rmail-mode-map [menu-bar move search-back]
726 '("Search Back..." . rmail-search-backwards))
728 (define-key rmail-mode-map [menu-bar move search]
729 '("Search..." . rmail-search))
731 (define-key rmail-mode-map [menu-bar move previous]
732 '("Previous Nondeleted" . rmail-previous-undeleted-message))
734 (define-key rmail-mode-map [menu-bar move next]
735 '("Next Nondeleted" . rmail-next-undeleted-message))
737 (define-key rmail-mode-map [menu-bar move last]
738 '("Last" . rmail-last-message))
740 (define-key rmail-mode-map [menu-bar move first]
741 '("First" . rmail-first-message))
743 (define-key rmail-mode-map [menu-bar move previous]
744 '("Previous" . rmail-previous-message))
746 (define-key rmail-mode-map [menu-bar move next]
747 '("Next" . rmail-next-message))
749 ;; Rmail mode is suitable only for specially formatted data.
750 (put 'rmail-mode 'mode-class 'special)
752 (defun rmail-mode-kill-summary ()
753 (if rmail-summary-buffer (kill-buffer rmail-summary-buffer)))
757 "Rmail Mode is used by \\<rmail-mode-map>\\[rmail] for editing Rmail files.
758 All normal editing commands are turned off.
759 Instead, these commands are available:
761 \\[rmail-beginning-of-message] Move point to front of this message (same as \\[beginning-of-buffer]).
762 \\[scroll-up] Scroll to next screen of this message.
763 \\[scroll-down] Scroll to previous screen of this message.
764 \\[rmail-next-undeleted-message] Move to Next non-deleted message.
765 \\[rmail-previous-undeleted-message] Move to Previous non-deleted message.
766 \\[rmail-next-message] Move to Next message whether deleted or not.
767 \\[rmail-previous-message] Move to Previous message whether deleted or not.
768 \\[rmail-first-message] Move to the first message in Rmail file.
769 \\[rmail-last-message] Move to the last message in Rmail file.
770 \\[rmail-show-message] Jump to message specified by numeric position in file.
771 \\[rmail-search] Search for string and show message it is found in.
772 \\[rmail-delete-forward] Delete this message, move to next nondeleted.
773 \\[rmail-delete-backward] Delete this message, move to previous nondeleted.
774 \\[rmail-undelete-previous-message] Undelete message. Tries current message, then earlier messages
775 till a deleted message is found.
776 \\[rmail-edit-current-message] Edit the current message. \\[rmail-cease-edit] to return to Rmail.
777 \\[rmail-expunge] Expunge deleted messages.
778 \\[rmail-expunge-and-save] Expunge and save the file.
779 \\[rmail-quit] Quit Rmail: expunge, save, then switch to another buffer.
780 \\[save-buffer] Save without expunging.
781 \\[rmail-get-new-mail] Move new mail from system spool directory into this file.
782 \\[rmail-mail] Mail a message (same as \\[mail-other-window]).
783 \\[rmail-continue] Continue composing outgoing message started before.
784 \\[rmail-reply] Reply to this message. Like \\[rmail-mail] but initializes some fields.
785 \\[rmail-retry-failure] Send this message again. Used on a mailer failure message.
786 \\[rmail-forward] Forward this message to another user.
787 \\[rmail-output-to-rmail-file] Output this message to an Rmail file (append it).
788 \\[rmail-output] Output this message to a Unix-format mail file (append it).
789 \\[rmail-input] Input Rmail file. Run Rmail on that file.
790 \\[rmail-add-label] Add label to message. It will be displayed in the mode line.
791 \\[rmail-kill-label] Kill label. Remove a label from current message.
792 \\[rmail-next-labeled-message] Move to Next message with specified label
793 (label defaults to last one specified).
794 Standard labels: filed, unseen, answered, forwarded, deleted.
795 Any other label is present only if you add it with \\[rmail-add-label].
796 \\[rmail-previous-labeled-message] Move to Previous message with specified label
797 \\[rmail-summary] Show headers buffer, with a one line summary of each message.
798 \\[rmail-summary-by-labels] Summarize only messages with particular label(s).
799 \\[rmail-summary-by-recipients] Summarize only messages with particular recipient(s).
800 \\[rmail-summary-by-regexp] Summarize only messages with particular regexp(s).
801 \\[rmail-summary-by-topic] Summarize only messages with subject line regexp(s).
802 \\[rmail-toggle-header] Toggle display of complete header."
805 (rmail-set-message-counters)
806 (rmail-show-message rmail-total-messages)
807 (run-hooks 'rmail-mode-hook))
809 (defun rmail-mode-2 ()
810 (kill-all-local-variables)
814 (defun rmail-mode-1 ()
815 (setq major-mode 'rmail-mode)
816 (setq mode-name "RMAIL")
817 (setq buffer-read-only t)
818 ;; No need to auto save RMAIL files in normal circumstances
819 ;; because they contain no info except attribute changes
820 ;; and deletion of messages.
821 ;; The one exception is when messages are copied into an Rmail mode buffer.
822 ;; rmail-output-to-rmail-file enables auto save when you do that.
823 (setq buffer-auto-save-file-name nil)
824 (if (boundp 'mode-line-modified)
825 (setq mode-line-modified "--- ")
826 (setq mode-line-format
827 (cons "--- " (cdr (default-value 'mode-line-format)))))
828 (use-local-map rmail-mode-map)
829 (set-syntax-table text-mode-syntax-table)
830 (setq local-abbrev-table text-mode-abbrev-table))
832 (defun rmail-variables ()
833 (make-local-variable 'revert-buffer-function)
834 (setq revert-buffer-function 'rmail-revert)
835 (make-local-variable 'font-lock-defaults)
836 (setq font-lock-defaults
837 '(rmail-font-lock-keywords t nil nil nil
838 (font-lock-maximum-size . nil)
839 (font-lock-fontify-buffer-function . rmail-fontify-buffer-function)
840 (font-lock-unfontify-buffer-function . rmail-unfontify-buffer-function)
841 (font-lock-inhibit-thing-lock . (lazy-lock-mode fast-lock-mode))))
842 (make-local-variable 'rmail-last-label)
843 (make-local-variable 'rmail-last-regexp)
844 (make-local-variable 'rmail-deleted-vector)
845 (make-local-variable 'rmail-buffer)
846 (setq rmail-buffer (current-buffer))
847 (make-local-variable 'rmail-view-buffer)
848 (setq rmail-view-buffer rmail-buffer)
849 (make-local-variable 'rmail-summary-buffer)
850 (make-local-variable 'rmail-summary-vector)
851 (make-local-variable 'rmail-current-message)
852 (make-local-variable 'rmail-total-messages)
853 (make-local-variable 'require-final-newline)
854 (setq require-final-newline nil)
855 (make-local-variable 'rmail-overlay-list)
856 (setq rmail-overlay-list nil)
857 (make-local-variable 'version-control)
858 (setq version-control 'never)
859 (make-local-variable 'kill-buffer-hook)
860 (add-hook 'kill-buffer-hook 'rmail-mode-kill-summary)
861 (make-local-variable 'file-precious-flag)
862 (setq file-precious-flag t)
863 (make-local-variable 'rmail-message-vector)
864 (make-local-variable 'rmail-msgref-vector)
865 (make-local-variable 'rmail-inbox-list)
866 (setq rmail-inbox-list (rmail-parse-file-inboxes))
867 ;; Provide default set of inboxes for primary mail file ~/RMAIL.
868 (and (null rmail-inbox-list)
869 (or (equal buffer-file-name (expand-file-name rmail-file-name))
870 (equal buffer-file-truename
871 (abbreviate-file-name (file-truename rmail-file-name))))
872 (setq rmail-inbox-list
873 (or rmail-primary-inbox-list
874 (list (or (getenv "MAIL")
875 (concat rmail-spool-directory
876 (user-login-name)))))))
877 (make-local-variable 'rmail-keywords)
878 ;; this gets generated as needed
879 (setq rmail-keywords nil))
881 ;; Handle M-x revert-buffer done in an rmail-mode buffer.
882 (defun rmail-revert (arg noconfirm)
883 (let ((revert-buffer-function (default-value 'revert-buffer-function)))
884 ;; Call our caller again, but this time it does the default thing.
885 (if (revert-buffer arg noconfirm)
886 ;; If the user said "yes", and we changed something,
887 ;; reparse the messages.
890 (goto-char (point-max))
893 ;; Return a list of files from this buffer's Mail: option.
894 ;; Does not assume that messages have been parsed.
895 ;; Just returns nil if buffer does not look like Babyl format.
896 (defun rmail-parse-file-inboxes ()
901 (cond ((looking-at "BABYL OPTIONS:")
902 (search-forward "\n\^_" nil 'move)
903 (narrow-to-region 1 (point))
905 (if (search-forward "\nMail:" nil t)
907 (narrow-to-region (point) (progn (end-of-line) (point)))
908 (goto-char (point-min))
909 (mail-parse-comma-list))))))))
911 (defun rmail-expunge-and-save ()
912 "Expunge and save RMAIL file."
916 (if (rmail-summary-exists)
917 (rmail-select-summary (set-buffer-modified-p nil))))
922 (rmail-expunge-and-save)
923 ;; Don't switch to the summary buffer even if it was recently visible.
924 (if rmail-summary-buffer
926 (replace-buffer-in-windows rmail-summary-buffer)
927 (bury-buffer rmail-summary-buffer)))
928 (let ((obuf (current-buffer)))
929 (replace-buffer-in-windows obuf)
933 "Bury current Rmail buffer and its summary buffer."
935 ;; This let var was called rmail-buffer, but that interfered
936 ;; with the buffer-local var used in summary buffers.
937 (let ((buffer-to-bury (current-buffer)))
938 (if (rmail-summary-exists)
940 (while (setq window (get-buffer-window rmail-summary-buffer))
941 (set-window-buffer window (other-buffer rmail-summary-buffer)))
942 (bury-buffer rmail-summary-buffer)))
943 (switch-to-buffer (other-buffer (current-buffer)))
944 (bury-buffer buffer-to-bury)))
946 (defun rmail-duplicate-message ()
947 "Create a duplicated copy of the current message.
948 The duplicate copy goes into the Rmail file just after the
952 (let ((buffer-read-only nil)
953 (number rmail-current-message)
954 (string (buffer-substring (rmail-msgbeg rmail-current-message)
955 (rmail-msgend rmail-current-message))))
956 (goto-char (rmail-msgend rmail-current-message))
958 (rmail-forget-messages)
959 (rmail-show-message number)
960 (message "Message duplicated")))
963 (defun rmail-input (filename)
964 "Run Rmail on file FILENAME."
965 (interactive "FRun rmail on RMAIL file: ")
969 ;; This used to scan subdirectories recursively, but someone pointed out
970 ;; that if the user wants that, person can put all the files in one dir.
971 ;; And the recursive scan was slow. So I took it out.
973 (defun rmail-find-all-files (start)
974 "Return list of file in dir START that match `rmail-secondary-file-regexp'."
975 (if (file-accessible-directory-p start)
977 (let* ((case-fold-search t)
978 (files (directory-files start t rmail-secondary-file-regexp)))
979 ;; Sort here instead of in directory-files
980 ;; because this list is usually much shorter.
981 (sort files 'string<))))
983 (defun rmail-list-to-menu (menu-name l action &optional full-name)
984 (let ((menu (make-sparse-keymap menu-name)))
986 (function (lambda (item)
991 (rmail-list-to-menu (car item) (cdr item)
994 (concat full-name "/"
997 (setq name (car item)))
1001 (list 'lambda () '(interactive)
1005 (concat full-name "/" item)
1007 rmail-secondary-file-directory))))))
1008 (define-key menu (vector (intern name))
1009 (cons name command)))))
1013 ;; This command is always "disabled" when it appears in a menu.
1014 (put 'rmail-disable-menu 'menu-enable ''nil)
1016 (defun rmail-construct-io-menu ()
1017 (let ((files (rmail-find-all-files rmail-secondary-file-directory)))
1020 (define-key rmail-mode-map [menu-bar classify input-menu]
1021 (cons "Input Rmail File"
1022 (rmail-list-to-menu "Input Rmail File"
1025 (define-key rmail-mode-map [menu-bar classify output-menu]
1026 (cons "Output Rmail File"
1027 (rmail-list-to-menu "Output Rmail File"
1029 'rmail-output-to-rmail-file))))
1031 (define-key rmail-mode-map [menu-bar classify input-menu]
1032 '("Input Rmail File" . rmail-disable-menu))
1033 (define-key rmail-mode-map [menu-bar classify output-menu]
1034 '("Output Rmail File" . rmail-disable-menu)))))
1037 ;;;; *** Rmail input ***
1039 ;; RLK feature not added in this version:
1040 ;; argument specifies inbox file or files in various ways.
1042 (defun rmail-get-new-mail (&optional file-name)
1043 "Move any new mail from this RMAIL file's inbox files.
1044 The inbox files can be specified with the file's Mail: option. The
1045 variable `rmail-primary-inbox-list' specifies the inboxes for your
1046 primary RMAIL file if it has no Mail: option. By default, this is
1047 your /usr/spool/mail/$USER.
1049 You can also specify the file to get new mail from. In this case, the
1050 file of new mail is not changed or deleted. Noninteractively, you can
1051 pass the inbox file name as an argument. Interactively, a prefix
1052 argument causes us to read a file name and use that file as the inbox.
1054 If the variable `rmail-preserve-inbox' is non-nil, new mail will
1055 always be left in inbox files rather than deleted.
1057 This function runs `rmail-get-new-mail-hook' before saving the updated file.
1058 It returns t if it got any new messages."
1060 (list (if current-prefix-arg
1061 (read-file-name "Get new mail from file: "))))
1062 (run-hooks 'rmail-before-get-new-mail-hook)
1063 ;; If the disk file has been changed from under us,
1064 ;; revert to it before we get new mail.
1065 (or (verify-visited-file-modtime (current-buffer))
1066 (find-file (buffer-file-name)))
1067 (rmail-maybe-set-message-counters)
1069 ;; Get rid of all undo records for this buffer.
1070 (or (eq buffer-undo-list t)
1071 (setq buffer-undo-list nil))
1072 (let ((all-files (if file-name (list file-name)
1077 (let ((opoint (point))
1080 ;; If buffer has not changed yet, and has not been saved yet,
1081 ;; don't replace the old backup file now.
1082 (make-backup-files (and make-backup-files (buffer-modified-p)))
1083 (buffer-read-only nil)
1084 ;; Don't make undo records for what we do in getting mail.
1085 (buffer-undo-list t)
1087 ;; Files to insert this time around.
1089 ;; Last names of those files.
1091 ;; Pull files off all-files onto files
1092 ;; as long as there is no name conflict.
1093 ;; A conflict happens when two inbox file names
1094 ;; have the same last component.
1095 (while (and all-files
1096 (not (member (file-name-nondirectory (car all-files))
1098 (setq files (cons (car all-files) files)
1100 (cons (file-name-nondirectory (car all-files)) files))
1101 (setq all-files (cdr all-files)))
1102 ;; Put them back in their original order.
1103 (setq files (nreverse files))
1105 (goto-char (point-max))
1106 (skip-chars-backward " \t\n") ; just in case of brain damage
1107 (delete-region (point) (point-max)) ; caused by require-final-newline
1110 (narrow-to-region (point) (point))
1111 ;; Read in the contents of the inbox files,
1112 ;; renaming them as necessary,
1113 ;; and adding to the list of files to delete eventually.
1115 (rmail-insert-inbox-text files nil)
1116 (setq delete-files (rmail-insert-inbox-text files t)))
1117 ;; Scan the new text and convert each message to babyl format.
1118 (goto-char (point-min))
1121 (setq new-messages (rmail-convert-to-babyl-format)
1123 ;; If we could not convert the file's inboxes,
1124 ;; rename the files we tried to read
1125 ;; so we won't over and over again.
1126 (if (and (not file-name) (not success))
1127 (let ((delfiles delete-files)
1130 (while (file-exists-p (format "RMAILOSE.%d" count))
1131 (setq count (1+ count)))
1132 (rename-file (car delfiles)
1133 (format "RMAILOSE.%d" count))
1134 (setq delfiles (cdr delfiles))))))
1135 (or (zerop new-messages)
1138 (search-backward "\n\^_" nil t)
1139 (narrow-to-region (point) (point-max))
1140 (goto-char (1+ (point-min)))
1141 (rmail-count-new-messages)
1142 (run-hooks 'rmail-get-new-mail-hook)
1144 ;; Delete the old files, now that babyl file is saved.
1147 ;; First, try deleting.
1149 (delete-file (car delete-files))
1151 ;; If we can't delete it, truncate it.
1152 (write-region (point) (point) (car delete-files))))
1154 (setq delete-files (cdr delete-files)))))
1155 (if (= new-messages 0)
1156 (progn (goto-char opoint)
1157 (if (or file-name rmail-inbox-list)
1158 (message "(No new mail has arrived)")))
1159 (if (rmail-summary-exists)
1160 (rmail-select-summary
1161 (rmail-update-summary)))
1162 (message "%d new message%s read"
1163 new-messages (if (= 1 new-messages) "" "s"))
1164 ;; Move to the first new message
1165 ;; unless we have other unseen messages before it.
1166 (rmail-show-message (rmail-first-unseen-message))
1167 (run-hooks 'rmail-after-get-new-mail-hook)
1170 ;; Don't leave the buffer screwed up if we get a disk-full error.
1171 (rmail-show-message))))
1173 (defun rmail-insert-inbox-text (files renamep)
1174 ;; Detect a locked file now, so that we avoid moving mail
1175 ;; out of the real inbox file. (That could scare people.)
1176 (or (memq (file-locked-p buffer-file-name) '(nil t))
1177 (error "RMAIL file %s is locked"
1178 (file-name-nondirectory buffer-file-name)))
1179 (let (file tofile delete-files movemail popmail)
1181 (setq file (file-truename
1182 (expand-file-name (substitute-in-file-name (car files))))
1183 tofile (expand-file-name
1184 ;; Generate name to move to from inbox name,
1185 ;; in case of multiple inboxes that need moving.
1186 (concat ".newmail-" (file-name-nondirectory file))
1187 ;; Use the directory of this rmail file
1188 ;; because it's a nuisance to use the homedir
1189 ;; if that is on a full disk and this rmail
1191 (file-name-directory
1192 (expand-file-name buffer-file-name))))
1193 ;; Always use movemail to rename the file,
1194 ;; since there can be mailboxes in various directories.
1196 ;;; ;; If getting from mail spool directory,
1197 ;;; ;; use movemail to move rather than just renaming,
1198 ;;; ;; so as to interlock with the mailer.
1199 ;;; (setq movemail (string= file
1201 ;;; (concat rmail-spool-directory
1202 ;;; (file-name-nondirectory file)))))
1203 (setq popmail (string-match "^po:" (file-name-nondirectory file)))
1204 (if popmail (setq file (file-name-nondirectory file)
1208 ;; On some systems, /usr/spool/mail/foo is a directory
1209 ;; and the actual inbox is /usr/spool/mail/foo/foo.
1210 (if (file-directory-p file)
1211 (setq file (expand-file-name (user-login-name)
1214 (if (and rmail-pop-password-required (not rmail-pop-password))
1215 (setq rmail-pop-password
1217 (format "Password for %s: "
1218 (substring file (+ popmail 3))))))
1219 (if (eq system-type 'windows-nt)
1220 ;; cannot have "po:" in file name
1223 (concat ".newmail-pop-" (substring file (+ popmail 3)))
1224 (file-name-directory
1225 (expand-file-name buffer-file-name)))))
1226 (message "Getting mail from post office ..."))
1227 ((and (file-exists-p tofile)
1228 (/= 0 (nth 7 (file-attributes tofile))))
1229 (message "Getting mail from %s..." tofile))
1230 ((and (file-exists-p file)
1231 (/= 0 (nth 7 (file-attributes file))))
1232 (message "Getting mail from %s..." file)))
1233 ;; Set TOFILE if have not already done so, and
1234 ;; rename or copy the file FILE to TOFILE if and as appropriate.
1235 (cond ((not renamep)
1237 ((or (file-exists-p tofile) (and (not popmail)
1238 (not (file-exists-p file))))
1240 ((and (not movemail) (not popmail))
1241 ;; Try copying. If that fails (perhaps no space) and
1242 ;; we're allowed to blow away the inbox, rename instead.
1243 (if rmail-preserve-inbox
1244 (copy-file file tofile nil)
1246 (copy-file file tofile nil)
1248 ;; Third arg is t so we can replace existing file TOFILE.
1249 (rename-file file tofile t))))
1250 ;; Make the real inbox file empty.
1251 ;; Leaving it deleted could cause lossage
1252 ;; because mailers often won't create the file.
1253 (if (not rmail-preserve-inbox)
1255 (write-region (point) (point) file)
1261 (setq errors (generate-new-buffer " *rmail loss*"))
1262 (buffer-disable-undo errors)
1265 (list (or rmail-movemail-program
1266 (expand-file-name "movemail"
1269 (if rmail-preserve-inbox
1273 (if rmail-pop-password
1274 (list rmail-pop-password)
1276 (apply 'call-process args))
1277 (if (not (buffer-modified-p errors))
1278 ;; No output => movemail won
1281 (subst-char-in-region (point-min) (point-max)
1283 (goto-char (point-max))
1284 (skip-chars-backward " \t")
1285 (delete-region (point) (point-max))
1286 (goto-char (point-min))
1287 (if (looking-at "movemail: ")
1288 (delete-region (point-min) (match-end 0)))
1290 (message "movemail: %s"
1291 (buffer-substring (point-min)
1295 (if errors (kill-buffer errors))))))
1296 ;; At this point, TOFILE contains the name to read:
1297 ;; Either the alternate name (if we renamed)
1298 ;; or the actual inbox (if not renaming).
1299 (if (file-exists-p tofile)
1300 (let ((coding-system-for-read 'no-conversion)
1302 (goto-char (point-max))
1303 (setq size (nth 1 (insert-file-contents tofile)))
1304 (goto-char (point-max))
1305 (or (= (preceding-char) ?\n)
1308 (if (not (and rmail-preserve-inbox (string= file tofile)))
1309 (setq delete-files (cons tofile delete-files)))))
1311 (setq files (cdr files)))
1314 (defun rmail-read-passwd (prompt &optional default)
1315 "Read a password, echoing `.' for each character typed.
1316 End with RET, LFD, or ESC. DEL or C-h rubs out. C-u kills line.
1317 Optional DEFAULT is password to start with."
1318 (let ((pass (if default default ""))
1321 (cursor-in-echo-area t))
1322 (while (progn (message "%s%s"
1324 (make-string (length pass) ?.))
1325 (setq c (read-char))
1326 (and (/= c ?\r) (/= c ?\n) (/= c ?\e)))
1329 (if (and (/= c ?\b) (/= c ?\177))
1330 (setq pass (concat pass (char-to-string c)))
1331 (if (> (length pass) 0)
1332 (setq pass (substring pass 0 -1))))))
1337 ;; the rmail-break-forwarded-messages feature is not implemented
1338 (defun rmail-convert-to-babyl-format ()
1339 (let ((count 0) start
1340 (case-fold-search nil)
1341 (invalid-input-resync
1342 (function (lambda ()
1343 (message "Invalid Babyl format in inbox!")
1345 ;; Try to get back in sync with a real message.
1346 (if (re-search-forward
1347 (concat mmdf-delim1 "\\|^From") nil t)
1349 (goto-char (point-max)))))))
1350 (goto-char (point-min))
1353 (setq start (point))
1354 (cond ((looking-at "BABYL OPTIONS:");Babyl header
1355 (if (search-forward "\n\^_" nil t)
1356 ;; If we find the proper terminator, delete through there.
1357 (delete-region (point-min) (point))
1358 (funcall invalid-input-resync)
1359 (delete-region (point-min) (point))))
1360 ;; Babyl format message
1362 (or (search-forward "\n\^_" nil t)
1363 (funcall invalid-input-resync))
1364 (setq count (1+ count))
1365 ;; Make sure there is no extra white space after the ^_
1366 ;; at the end of the message.
1367 ;; Narrowing will make sure that whatever follows the junk
1368 ;; will be treated properly.
1369 (delete-region (point)
1371 (skip-chars-forward " \t\n")
1373 (or rmail-enable-mime
1374 (not rmail-file-coding-system)
1375 (decode-coding-region start (point)
1376 rmail-file-coding-system))
1377 (narrow-to-region (point) (point-max)))
1379 ((let ((case-fold-search t))
1380 (looking-at mmdf-delim1))
1381 (let ((case-fold-search t))
1382 (replace-match "\^L\n0, unseen,,\n*** EOOH ***\n")
1383 (re-search-forward mmdf-delim2 nil t)
1384 (replace-match "\^_"))
1387 (narrow-to-region start (1- (point)))
1388 (goto-char (point-min))
1389 (while (search-forward "\n\^_" nil t); single char "\^_"
1390 (replace-match "\n^_")))); 2 chars: "^" and "_"
1391 (or rmail-enable-mime
1392 (not rmail-file-coding-system)
1393 (decode-coding-region start (point) 'automatic-conversion))
1394 (narrow-to-region (point) (point-max))
1395 (setq count (1+ count)))
1397 ((looking-at "^From ")
1398 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
1399 (rmail-nuke-pinhead-header)
1400 ;; If this message has a Content-Length field,
1401 ;; skip to the end of the contents.
1402 (let* ((header-end (save-excursion
1403 (and (re-search-forward "\n\n" nil t)
1405 (case-fold-search t)
1407 ;; Get the numeric value from the Content-Length field.
1409 ;; Back up to end of prev line,
1410 ;; in case the Content-Length field comes first.
1412 (and (search-forward "\ncontent-length: "
1415 (eol (progn (end-of-line) (point))))
1416 (string-to-int (buffer-substring beg eol)))))))
1418 (if (and (natnump size)
1419 (<= (+ header-end size) (point-max))
1420 ;; Make sure this would put us at a position
1421 ;; that we could continue from.
1423 (goto-char (+ header-end size))
1424 (skip-chars-forward "\n")
1426 (and (looking-at "BABYL OPTIONS:")
1427 (search-forward "\n\^_" nil t))
1428 (and (looking-at "\^L")
1429 (search-forward "\n\^_" nil t))
1430 (let ((case-fold-search t))
1431 (looking-at mmdf-delim1))
1432 (looking-at "From "))))
1433 (goto-char (+ header-end size))
1434 (message "Ignoring invalid Content-Length field")
1437 (if (re-search-forward
1438 (concat "^[\^_]?\\("
1439 rmail-unix-mail-delimiter
1442 "^BABYL OPTIONS:\\|"
1443 "\^L\n[01],\\)") nil t)
1444 (goto-char (match-beginning 1))
1445 (goto-char (point-max)))
1446 (setq count (1+ count))
1449 (narrow-to-region start (point))
1450 (goto-char (point-min))
1451 (while (search-forward "\n\^_" nil t); single char
1452 (replace-match "\n^_")))); 2 chars: "^" and "_"
1454 (or rmail-enable-mime
1455 (not rmail-file-coding-system)
1456 (decode-coding-region start (point) 'automatic-conversion))
1457 (narrow-to-region (point) (point-max)))
1459 ;; This kludge is because some versions of sendmail.el
1460 ;; insert an extra newline at the beginning that shouldn't
1461 ;; be there. sendmail.el has been fixed, but old versions
1462 ;; may still be in use. -- rms, 7 May 1993.
1463 ((eolp) (delete-char 1))
1464 (t (error "Cannot convert to babyl format")))))
1467 ;; Delete the "From ..." line, creating various other headers with
1468 ;; information from it if they don't already exist. Now puts the
1469 ;; original line into a mail-from: header line for debugging and for
1470 ;; use by the rmail-output function.
1471 (defun rmail-nuke-pinhead-header ()
1474 (let ((start (point))
1477 (search-forward "\n\n")
1479 (goto-char (point-max))
1483 (narrow-to-region start end)
1484 (let ((case-fold-search t))
1486 (setq has-from (search-forward "\nFrom:" nil t))
1488 (setq has-date (and (search-forward "\nDate:" nil t) (point)))
1490 (let ((case-fold-search nil))
1491 (if (re-search-forward (concat "^" rmail-unix-mail-delimiter) nil t)
1495 ;; Keep and reformat the date if we don't
1496 ;; have a Date: field.
1500 "Date: \\2, \\4 \\3 \\9 \\5 "
1502 ;; The timezone could be matched by group 7 or group 10.
1503 ;; If neither of them matched, assume EST, since only
1504 ;; Easterners would be so sloppy.
1505 ;; It's a shame the substitution can't use "\\10".
1507 ((/= (match-beginning 7) (match-end 7)) "\\7")
1508 ((/= (match-beginning 10) (match-end 10))
1509 (buffer-substring (match-beginning 10)
1513 ;; Keep and reformat the sender if we don't
1514 ;; have a From: field.
1520 ;;;; *** Rmail Message Formatting and Header Manipulation ***
1522 (defun rmail-reformat-message (beg end)
1525 (if (/= (following-char) ?0)
1526 (error "Bad format in RMAIL file."))
1527 (let ((buffer-read-only nil)
1528 (delta (- (buffer-size) end)))
1532 (let ((case-fold-search t))
1533 (while (looking-at "Summary-line:\\|Mail-From:")
1535 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*\n")
1536 (delete-region (point)
1537 (progn (forward-line 1) (point))))
1538 (let ((str (buffer-substring (point)
1539 (save-excursion (search-forward "\n\n" end 'move)
1541 (insert str "*** EOOH ***\n")
1542 (narrow-to-region (point) (- (buffer-size) delta)))
1543 (goto-char (point-min))
1544 (if rmail-message-filter (funcall rmail-message-filter))
1545 (if (or rmail-displayed-headers rmail-ignored-headers)
1546 (rmail-clear-headers))))
1548 (defun rmail-clear-headers (&optional ignored-headers)
1549 "Delete all header fields that Rmail should not show.
1550 If the optional argument IGNORED-HEADERS is non-nil,
1551 delete all header fields whose names match that regexp.
1552 Otherwise, if `rmail-displayed-headers' is non-nil,
1553 delete all header fields *except* those whose names match that regexp.
1554 Otherwise, delete all header fields whose names match `rmail-ignored-headers'."
1555 (if (search-forward "\n\n" nil t)
1556 (let ((case-fold-search t)
1557 (buffer-read-only nil))
1558 (if (and rmail-displayed-headers (null ignored-headers))
1560 (narrow-to-region (point-min) (point))
1562 (goto-char (point-min))
1563 (while (save-excursion
1564 (re-search-forward "\n[^ \t]")
1566 (setq lim (1- (point)))))
1568 (re-search-forward rmail-displayed-headers lim t))
1570 (delete-region (point) lim))))
1571 (goto-char (point-min)))
1572 (or ignored-headers (setq ignored-headers rmail-ignored-headers))
1574 (narrow-to-region (point-min) (point))
1576 (goto-char (point-min))
1577 (re-search-forward ignored-headers nil t))
1579 (delete-region (point)
1580 (progn (re-search-forward "\n[^ \t]")
1581 (1- (point))))))))))
1583 (defun rmail-msg-is-pruned ()
1584 (rmail-maybe-set-message-counters)
1587 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
1588 (goto-char (point-min))
1590 (= (following-char) ?1))))
1592 (defun rmail-toggle-header (&optional arg)
1593 "Show original message header if pruned header currently shown, or vice versa.
1594 With argument ARG, show the message header pruned if ARG is greater than zero;
1595 otherwise, show it in full."
1597 (let* ((buffer-read-only nil)
1598 (pruned (rmail-msg-is-pruned))
1600 (> (prefix-numeric-value arg) 0)
1602 (if (eq pruned prune)
1604 (rmail-maybe-set-message-counters)
1605 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
1607 (progn (goto-char (point-min))
1612 (let ((case-fold-search t))
1613 (while (looking-at "Summary-Line:\\|Mail-From:")
1615 (insert "*** EOOH ***\n")
1617 (search-forward "\n*** EOOH ***\n")
1619 (let ((temp (point)))
1620 (and (search-forward "\n\n" nil t)
1621 (delete-region temp (point))))
1622 (goto-char (point-min))
1623 (search-forward "\n*** EOOH ***\n")
1624 (narrow-to-region (point) (point-max)))
1625 (rmail-reformat-message (point-min) (point-max)))
1626 (rmail-highlight-headers))))
1628 ;;;; *** Rmail Attributes and Keywords ***
1630 ;; Make a string describing current message's attributes and keywords
1631 ;; and set it up as the name of a minor mode
1632 ;; so it will appear in the mode line.
1633 (defun rmail-display-labels ()
1634 (let ((blurb "") (beg (point-min-marker)) (end (point-max-marker)))
1639 (goto-char (rmail-msgbeg rmail-current-message))
1641 (if (looking-at "[01],")
1643 (narrow-to-region (point) (progn (end-of-line) (point)))
1644 ;; Truly valid BABYL format requires a space before each
1645 ;; attribute or keyword name. Put them in if missing.
1646 (let (buffer-read-only)
1647 (goto-char (point-min))
1648 (while (search-forward "," nil t)
1649 (or (looking-at "[ ,]") (eobp)
1651 (goto-char (point-max))
1652 (if (search-backward ",," nil 'move)
1654 (if (> (point) (1+ (point-min)))
1655 (setq blurb (buffer-substring (+ 1 (point-min)) (point))))
1656 (if (> (- (point-max) (point)) 2)
1660 (buffer-substring (+ (point) 3)
1661 (1- (point-max)))))))))))
1662 ;; Note: we don't use save-restriction because that does not work right
1663 ;; if changes are made outside the saved restriction
1664 ;; before that restriction is restored.
1665 (narrow-to-region beg end)
1666 (set-marker beg nil)
1667 (set-marker end nil)))
1668 (while (string-match " +," blurb)
1669 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
1670 (substring blurb (match-end 0)))))
1671 (while (string-match ", +" blurb)
1672 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
1673 (substring blurb (match-end 0)))))
1674 (setq mode-line-process
1676 rmail-current-message rmail-total-messages blurb))))
1678 ;; Turn an attribute of a message on or off according to STATE.
1679 ;; ATTR is the name of the attribute, as a string.
1680 ;; MSGNUM is message number to change; nil means current message.
1681 (defun rmail-set-attribute (attr state &optional msgnum)
1682 (let ((omax (point-max-marker))
1683 (omin (point-min-marker))
1684 (buffer-read-only nil))
1685 (or msgnum (setq msgnum rmail-current-message))
1690 (goto-char (+ 3 (rmail-msgbeg msgnum)))
1693 (null (search-backward (concat ", " attr ",")
1694 (prog1 (point) (end-of-line)) t)))))
1695 (or (eq curstate (not (not state)))
1697 (delete-region (point) (1- (match-end 0)))
1700 (insert " " attr ","))))
1701 (if (string= attr "deleted")
1702 (rmail-set-message-deleted-p msgnum state)))
1703 ;; Note: we don't use save-restriction because that does not work right
1704 ;; if changes are made outside the saved restriction
1705 ;; before that restriction is restored.
1706 (narrow-to-region omin omax)
1707 (set-marker omin nil)
1708 (set-marker omax nil)
1709 (if (= msgnum rmail-current-message)
1710 (rmail-display-labels))))))
1712 ;; Return t if the attributes/keywords line of msg number MSG
1713 ;; contains a match for the regexp LABELS.
1714 (defun rmail-message-labels-p (msg labels)
1718 (goto-char (rmail-msgbeg msg))
1720 (re-search-backward labels (prog1 (point) (end-of-line)) t))))
1722 ;;;; *** Rmail Message Selection And Support ***
1724 (defun rmail-msgend (n)
1725 (marker-position (aref rmail-message-vector (1+ n))))
1727 (defun rmail-msgbeg (n)
1728 (marker-position (aref rmail-message-vector n)))
1730 (defun rmail-widen-to-current-msgbeg (function)
1731 "Call FUNCTION with point at start of internal data of current message.
1732 Assumes that bounds were previously narrowed to display the message in Rmail.
1733 The bounds are widened enough to move point where desired, then narrowed
1736 FUNCTION may not change the visible text of the message, but it may
1737 change the invisible header text."
1739 (let ((obeg (- (point-max) (point-min))))
1742 (narrow-to-region (rmail-msgbeg rmail-current-message)
1744 (goto-char (point-min))
1746 ;; Note: we don't use save-restriction because that does not work right
1747 ;; if changes are made outside the saved restriction
1748 ;; before that restriction is restored.
1749 ;; Here we assume that changes made by FUNCTION
1750 ;; occur before the visible region of the message.
1751 (narrow-to-region (- (point-max) obeg) (point-max))))))
1753 (defun rmail-forget-messages ()
1755 (if (vectorp rmail-message-vector)
1757 (v rmail-message-vector)
1760 (move-marker (aref v i) nil)
1762 (setq rmail-message-vector nil)
1763 (setq rmail-msgref-vector nil)
1764 (setq rmail-deleted-vector nil)))
1766 (defun rmail-maybe-set-message-counters ()
1767 (if (not (and rmail-deleted-vector
1768 rmail-message-vector
1769 rmail-current-message
1770 rmail-total-messages))
1771 (rmail-set-message-counters)))
1773 (defun rmail-count-new-messages (&optional nomsg)
1774 (let* ((case-fold-search nil)
1778 (or nomsg (message "Counting new messages..."))
1779 (goto-char (point-max))
1780 ;; Put at the end of messages-head
1781 ;; the entry for message N+1, which marks
1782 ;; the end of message N. (N = number of messages).
1783 (search-backward "\n\^_")
1785 (setq messages-head (list (point-marker)))
1786 (rmail-set-message-counters-counter (point-min))
1787 (setq rmail-current-message (1+ rmail-total-messages))
1788 (setq rmail-total-messages
1789 (+ rmail-total-messages total-messages))
1790 (setq rmail-message-vector
1791 (vconcat rmail-message-vector (cdr messages-head)))
1792 (aset rmail-message-vector
1793 rmail-current-message (car messages-head))
1794 (setq rmail-deleted-vector
1795 (concat rmail-deleted-vector deleted-head))
1796 (setq rmail-summary-vector
1797 (vconcat rmail-summary-vector (make-vector total-messages nil)))
1798 (setq rmail-msgref-vector
1799 (vconcat rmail-msgref-vector (make-vector total-messages nil)))
1800 ;; Fill in the new elements of rmail-msgref-vector.
1801 (let ((i (- rmail-total-messages old-total-messages)))
1802 (while (<= i rmail-total-messages)
1803 (aset rmail-msgref-vector i (list i))
1805 (goto-char (point-min))
1806 (or nomsg (message "Counting new messages...done (%d)" total-messages))))
1808 (defun rmail-set-message-counters ()
1809 (rmail-forget-messages)
1813 (let* ((point-save (point))
1815 (messages-after-point)
1816 (case-fold-search nil)
1819 (message "Counting messages...")
1820 (goto-char (point-max))
1821 ;; Put at the end of messages-head
1822 ;; the entry for message N+1, which marks
1823 ;; the end of message N. (N = number of messages).
1824 (search-backward "\n\^_" nil t)
1825 (if (/= (point) (point-max)) (forward-char 1))
1826 (setq messages-head (list (point-marker)))
1827 (rmail-set-message-counters-counter (min (point) point-save))
1828 (setq messages-after-point total-messages)
1829 (rmail-set-message-counters-counter)
1830 (setq rmail-total-messages total-messages)
1831 (setq rmail-current-message
1833 (max 1 (- total-messages messages-after-point))))
1834 (setq rmail-message-vector
1835 (apply 'vector (cons (point-min-marker) messages-head))
1836 rmail-deleted-vector (concat "D" deleted-head)
1837 rmail-summary-vector (make-vector rmail-total-messages nil)
1838 rmail-msgref-vector (make-vector (1+ rmail-total-messages) nil))
1840 (while (<= i rmail-total-messages)
1841 (aset rmail-msgref-vector i (list i))
1843 (message "Counting messages...done")))))
1845 (defun rmail-set-message-counters-counter (&optional stop)
1846 (while (search-backward "\n\^_\^L\n" stop t)
1848 (setq messages-head (cons (point-marker) messages-head))
1851 (cons (if (search-backward ", deleted,"
1857 (if (zerop (% (setq total-messages (1+ total-messages)) 20))
1858 (message "Counting messages...%d" total-messages))))
1860 (defun rmail-beginning-of-message ()
1861 "Show current message starting from the beginning."
1863 (rmail-show-message rmail-current-message))
1865 (defun rmail-show-message (&optional n no-summary)
1866 "Show message number N (prefix argument), counting from start of file.
1867 If summary buffer is currently displayed, update current message there also."
1869 (or (eq major-mode 'rmail-mode)
1870 (switch-to-buffer rmail-buffer))
1871 (rmail-maybe-set-message-counters)
1873 (if (zerop rmail-total-messages)
1874 (progn (narrow-to-region (point-min) (1- (point-max)))
1875 (goto-char (point-min))
1876 (setq mode-line-process nil))
1879 (setq n rmail-current-message)
1882 rmail-current-message 1
1883 blurb "No previous message"))
1884 ((> n rmail-total-messages)
1885 (setq n rmail-total-messages
1886 rmail-current-message rmail-total-messages
1887 blurb "No following message"))
1889 (setq rmail-current-message n))))
1890 (let ((beg (rmail-msgbeg n)))
1893 ;; Clear the "unseen" attribute when we show a message.
1894 (rmail-set-attribute "unseen" nil)
1895 ;; Reformat the header, or else find the reformatted header.
1896 (let ((end (rmail-msgend n)))
1897 (if (= (following-char) ?0)
1898 (rmail-reformat-message beg end)
1899 (search-forward "\n*** EOOH ***\n" end t)
1900 (narrow-to-region (point) end)))
1901 (goto-char (point-min))
1902 (rmail-display-labels)
1903 (if (eq rmail-enable-mime t)
1904 (funcall rmail-show-mime-function)
1905 (setq rmail-view-buffer rmail-buffer)
1907 (rmail-highlight-headers)
1908 (if transient-mark-mode (deactivate-mark))
1909 (run-hooks 'rmail-show-message-hook)
1910 ;; If there is a summary buffer, try to move to this message
1911 ;; in that buffer. But don't complain if this message
1912 ;; is not mentioned in the summary.
1913 ;; Don't do this at all if we were called on behalf
1914 ;; of cursor motion in the summary buffer.
1915 (and (rmail-summary-exists) (not no-summary)
1916 (let ((curr-msg rmail-current-message))
1917 (rmail-select-summary
1918 (rmail-summary-goto-msg curr-msg t t))))
1920 (message blurb))))))
1922 ;; Find all occurrences of certain fields, and highlight them.
1923 (defun rmail-highlight-headers ()
1924 ;; Do this only if the system supports faces.
1925 (if (and (fboundp 'internal-find-face)
1926 rmail-highlighted-headers)
1928 (search-forward "\n\n" nil 'move)
1930 (narrow-to-region (point-min) (point))
1931 (let ((case-fold-search t)
1932 (inhibit-read-only t)
1933 ;; Highlight with boldface if that is available.
1934 ;; Otherwise use the `highlight' face.
1935 (face (or rmail-highlight-face
1936 (if (face-differs-from-default-p 'bold)
1938 ;; List of overlays to reuse.
1939 (overlays rmail-overlay-list))
1940 (goto-char (point-min))
1941 (while (re-search-forward rmail-highlighted-headers nil t)
1942 (skip-chars-forward " \t")
1945 (while (progn (forward-line 1)
1946 (looking-at "[ \t]")))
1947 ;; Back up over newline, then trailing spaces or tabs
1949 (while (member (preceding-char) '(? ?\t))
1952 ;; Reuse an overlay we already have.
1954 (setq overlay (car overlays)
1955 overlays (cdr overlays))
1956 (overlay-put overlay 'face face)
1957 (move-overlay overlay beg (point)))
1958 ;; Make a new overlay and add it to
1959 ;; rmail-overlay-list.
1960 (setq overlay (make-overlay beg (point)))
1961 (overlay-put overlay 'face face)
1962 (setq rmail-overlay-list
1963 (cons overlay rmail-overlay-list))))))))))
1965 (defun rmail-next-message (n)
1966 "Show following message whether deleted or not.
1967 With prefix arg N, moves forward N messages, or backward if N is negative."
1969 (rmail-maybe-set-message-counters)
1970 (rmail-show-message (+ rmail-current-message n)))
1972 (defun rmail-previous-message (n)
1973 "Show previous message whether deleted or not.
1974 With prefix arg N, moves backward N messages, or forward if N is negative."
1976 (rmail-next-message (- n)))
1978 (defun rmail-next-undeleted-message (n)
1979 "Show following non-deleted message.
1980 With prefix arg N, moves forward N non-deleted messages,
1981 or backward if N is negative.
1983 Returns t if a new message is being shown, nil otherwise."
1985 (rmail-maybe-set-message-counters)
1986 (let ((lastwin rmail-current-message)
1987 (current rmail-current-message))
1988 (while (and (> n 0) (< current rmail-total-messages))
1989 (setq current (1+ current))
1990 (if (not (rmail-message-deleted-p current))
1991 (setq lastwin current n (1- n))))
1992 (while (and (< n 0) (> current 1))
1993 (setq current (1- current))
1994 (if (not (rmail-message-deleted-p current))
1995 (setq lastwin current n (1+ n))))
1996 (if (/= lastwin rmail-current-message)
1997 (progn (rmail-show-message lastwin)
2000 (message "No previous nondeleted message"))
2002 (message "No following nondeleted message"))
2005 (defun rmail-previous-undeleted-message (n)
2006 "Show previous non-deleted message.
2007 With prefix argument N, moves backward N non-deleted messages,
2008 or forward if N is negative."
2010 (rmail-next-undeleted-message (- n)))
2012 (defun rmail-first-message ()
2013 "Show first message in file."
2015 (rmail-maybe-set-message-counters)
2016 (rmail-show-message 1))
2018 (defun rmail-last-message ()
2019 "Show last message in file."
2021 (rmail-maybe-set-message-counters)
2022 (rmail-show-message rmail-total-messages))
2024 (defun rmail-what-message ()
2025 (let ((where (point))
2027 (high rmail-total-messages)
2028 (mid (/ rmail-total-messages 2)))
2029 (while (> (- high low) 1)
2030 (if (>= where (rmail-msgbeg mid))
2033 (setq mid (+ low (/ (- high low) 2))))
2034 (if (>= where (rmail-msgbeg high)) high low)))
2036 (defun rmail-message-recipients-p (msg recipients &optional primary-only)
2038 (goto-char (rmail-msgbeg msg))
2039 (search-forward "\n*** EOOH ***\n")
2040 (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
2041 (or (string-match recipients (or (mail-fetch-field "To") ""))
2042 (string-match recipients (or (mail-fetch-field "From") ""))
2043 (if (not primary-only)
2044 (string-match recipients (or (mail-fetch-field "Cc") ""))))))
2046 (defun rmail-message-regexp-p (msg regexp)
2047 "Return t, if for message number MSG, regexp REGEXP matches in the header."
2048 (goto-char (rmail-msgbeg msg))
2051 (search-forward "*** EOOH ***" (point-max)) (point))))
2052 (re-search-forward regexp end t)))
2054 (defvar rmail-search-last-regexp nil)
2055 (defun rmail-search (regexp &optional n)
2056 "Show message containing next match for REGEXP (but not the current msg).
2057 Prefix argument gives repeat count; negative argument means search
2058 backwards (through earlier messages).
2059 Interactively, empty argument means use same regexp used last time."
2061 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
2063 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
2065 (if rmail-search-last-regexp
2066 (setq prompt (concat prompt
2068 rmail-search-last-regexp
2070 (setq regexp (read-string prompt))
2071 (cond ((not (equal regexp ""))
2072 (setq rmail-search-last-regexp regexp))
2073 ((not rmail-search-last-regexp)
2074 (error "No previous Rmail search string")))
2075 (list rmail-search-last-regexp
2076 (prefix-numeric-value current-prefix-arg))))
2078 (message "%sRmail search for %s..."
2079 (if (< n 0) "Reverse " "")
2081 (rmail-maybe-set-message-counters)
2082 (let ((omin (point-min))
2087 (msg rmail-current-message))
2092 ;; Check messages one by one, advancing message number up or down
2093 ;; but searching forward through each message.
2095 (while (and (null win) (> msg 1))
2096 (goto-char (rmail-msgbeg (setq msg (1- msg))))
2097 (setq win (re-search-forward
2098 regexp (rmail-msgend msg) t)))
2099 (while (and (null win) (< msg rmail-total-messages))
2100 (goto-char (rmail-msgbeg (setq msg (1+ msg))))
2101 (setq win (re-search-forward regexp (rmail-msgend msg) t))))
2102 (setq n (+ n (if reversep 1 -1)))))
2105 ;; If this is a reverse search and we found a message,
2106 ;; search backward thru this message to position point.
2109 (goto-char (rmail-msgend msg))
2111 regexp (rmail-msgbeg msg) t)))
2113 (rmail-show-message msg)
2114 (message "%sRmail search for %s...done"
2115 (if reversep "Reverse " "")
2119 (narrow-to-region omin omax)
2121 (message "Search failed: %s" regexp)))))
2123 (defun rmail-search-backwards (regexp &optional n)
2124 "Show message containing previous match for REGEXP.
2125 Prefix argument gives repeat count; negative argument means search
2126 forward (through later messages).
2127 Interactively, empty argument means use same regexp used last time."
2129 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
2131 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
2133 (if rmail-search-last-regexp
2134 (setq prompt (concat prompt
2136 rmail-search-last-regexp
2138 (setq regexp (read-string prompt))
2139 (cond ((not (equal regexp ""))
2140 (setq rmail-search-last-regexp regexp))
2141 ((not rmail-search-last-regexp)
2142 (error "No previous Rmail search string")))
2143 (list rmail-search-last-regexp
2144 (prefix-numeric-value current-prefix-arg))))
2145 (rmail-search regexp (- (or n 1))))
2147 ;; Show the first message which has the `unseen' attribute.
2148 (defun rmail-first-unseen-message ()
2149 (rmail-maybe-set-message-counters)
2154 (while (and (not found) (<= current rmail-total-messages))
2155 (if (rmail-message-labels-p current ", ?\\(unseen\\),")
2156 (setq found current))
2157 (setq current (1+ current))))
2158 ;; Let the caller show the message.
2160 ;; (rmail-show-message found))
2163 (defun rmail-next-same-subject (n)
2164 "Go to the next mail message having the same subject header.
2165 With prefix argument N, do this N times.
2166 If N is negative, go backwards instead."
2168 (let ((subject (mail-fetch-field "Subject"))
2170 (i rmail-current-message)
2171 (case-fold-search t)
2172 search-regexp found)
2173 (if (string-match "Re:[ \t]*" subject)
2174 (setq subject (substring subject (match-end 0))))
2175 (setq search-regexp (concat "^Subject: *\\(Re: *\\)?"
2176 (regexp-quote subject)
2181 (while (and (/= n 0)
2183 (< i rmail-total-messages)
2186 (while (and (not done)
2188 (< i rmail-total-messages)
2190 (setq i (if forward (1+ i) (1- i)))
2191 (goto-char (rmail-msgbeg i))
2192 (search-forward "\n*** EOOH ***\n")
2193 (let ((beg (point)) end)
2194 (search-forward "\n\n")
2197 (setq done (re-search-forward search-regexp end t))))
2198 (if done (setq found i)))
2199 (setq n (if forward (1- n) (1+ n))))))
2201 (rmail-show-message found)
2202 (error "No %s message with same subject"
2203 (if forward "following" "previous")))))
2205 (defun rmail-previous-same-subject (n)
2206 "Go to the previous mail message having the same subject header.
2207 With prefix argument N, do this N times.
2208 If N is negative, go forwards instead."
2210 (rmail-next-same-subject (- n)))
2212 ;;;; *** Rmail Message Deletion Commands ***
2214 (defun rmail-message-deleted-p (n)
2215 (= (aref rmail-deleted-vector n) ?D))
2217 (defun rmail-set-message-deleted-p (n state)
2218 (aset rmail-deleted-vector n (if state ?D ?\ )))
2220 (defun rmail-delete-message ()
2221 "Delete this message and stay on it."
2223 (rmail-set-attribute "deleted" t)
2224 (run-hooks 'rmail-delete-message-hook))
2226 (defun rmail-undelete-previous-message ()
2227 "Back up to deleted message, select it, and undelete it."
2229 (let ((msg rmail-current-message))
2230 (while (and (> msg 0)
2231 (not (rmail-message-deleted-p msg)))
2232 (setq msg (1- msg)))
2234 (error "No previous deleted message")
2235 (if (/= msg rmail-current-message)
2236 (rmail-show-message msg))
2237 (rmail-set-attribute "deleted" nil)
2238 (if (rmail-summary-exists)
2240 (set-buffer rmail-summary-buffer)
2241 (rmail-summary-mark-undeleted msg)))
2242 (rmail-maybe-display-summary))))
2244 (defun rmail-delete-forward (&optional backward)
2245 "Delete this message and move to next nondeleted one.
2246 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
2247 With prefix argument, delete and move backward.
2249 Returns t if a new message is displayed after the delete, or nil otherwise."
2251 (rmail-set-attribute "deleted" t)
2252 (run-hooks 'rmail-delete-message-hook)
2253 (let ((del-msg rmail-current-message))
2254 (if (rmail-summary-exists)
2255 (rmail-select-summary
2256 (rmail-summary-mark-deleted del-msg)))
2257 (prog1 (rmail-next-undeleted-message (if backward -1 1))
2258 (rmail-maybe-display-summary))))
2260 (defun rmail-delete-backward ()
2261 "Delete this message and move to previous nondeleted one.
2262 Deleted messages stay in the file until the \\[rmail-expunge] command is given."
2264 (rmail-delete-forward t))
2266 ;; Compute the message number a given message would have after expunging.
2267 ;; The present number of the message is OLDNUM.
2268 ;; DELETEDVEC should be rmail-deleted-vector.
2269 ;; The value is nil for a message that would be deleted.
2270 (defun rmail-msg-number-after-expunge (deletedvec oldnum)
2271 (if (or (null oldnum) (= (aref deletedvec oldnum) ?D))
2276 (if (/= (aref deletedvec i) ?D)
2277 (setq newnum (1+ newnum)))
2281 (defun rmail-only-expunge ()
2282 "Actually erase all deleted messages in the file."
2284 (message "Expunging deleted messages...")
2285 ;; Discard all undo records for this buffer.
2286 (or (eq buffer-undo-list t)
2287 (setq buffer-undo-list nil))
2288 (rmail-maybe-set-message-counters)
2289 (let* ((omax (- (buffer-size) (point-max)))
2290 (omin (- (buffer-size) (point-min)))
2291 (opoint (if (and (> rmail-current-message 0)
2292 (rmail-message-deleted-p rmail-current-message))
2294 (- (point) (point-min))))
2295 (messages-head (cons (aref rmail-message-vector 0) nil))
2296 (messages-tail messages-head)
2297 ;; Don't make any undo records for the expunging.
2298 (buffer-undo-list t)
2303 (goto-char (point-min))
2306 (total rmail-total-messages)
2307 (new-message-number rmail-current-message)
2309 (new-msgref (list (list 0)))
2310 (rmailbuf (current-buffer))
2311 (buffer-read-only nil)
2312 (messages rmail-message-vector)
2313 (deleted rmail-deleted-vector)
2314 (summary rmail-summary-vector))
2315 (setq rmail-total-messages nil
2316 rmail-current-message nil
2317 rmail-message-vector nil
2318 rmail-deleted-vector nil
2319 rmail-summary-vector nil)
2321 (while (<= number total)
2322 (if (= (aref deleted number) ?D)
2325 (marker-position (aref messages number))
2326 (marker-position (aref messages (1+ number))))
2327 (move-marker (aref messages number) nil)
2328 (if (> new-message-number counter)
2329 (setq new-message-number (1- new-message-number))))
2330 (setq counter (1+ counter))
2332 (setcdr messages-tail
2333 (cons (aref messages number) nil)))
2335 (cons (if (= counter number) (aref summary (1- number)))
2338 (cons (aref rmail-msgref-vector number)
2340 (setcar (car new-msgref) counter))
2341 (if (zerop (% (setq number (1+ number)) 20))
2342 (message "Expunging deleted messages...%d" number)))
2344 (setcdr messages-tail
2345 (cons (aref messages number) nil)))
2346 (setq rmail-current-message new-message-number
2347 rmail-total-messages counter
2348 rmail-message-vector (apply 'vector messages-head)
2349 rmail-deleted-vector (make-string (1+ counter) ?\ )
2350 rmail-summary-vector (vconcat (nreverse new-summary))
2351 rmail-msgref-vector (apply 'vector (nreverse new-msgref))
2353 (message "Expunging deleted messages...done")
2355 (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
2357 (if (zerop rmail-current-message) 1 nil))
2358 (goto-char (+ (point) opoint)))))
2360 (defun rmail-expunge ()
2361 "Erase deleted messages from Rmail file and summary buffer."
2363 (rmail-only-expunge)
2364 (if (rmail-summary-exists)
2365 (rmail-select-summary
2366 (rmail-update-summary))))
2368 ;;;; *** Rmail Mailing Commands ***
2370 (defun rmail-start-mail (&optional noerase to subject in-reply-to cc
2371 replybuffer sendactions same-window others)
2374 (setq yank-action (list 'insert-buffer replybuffer)))
2375 (setq others (cons (cons "cc" cc) others))
2376 (setq others (cons (cons "in-reply-to" in-reply-to) others))
2378 (compose-mail to subject others
2380 yank-action sendactions)
2381 (if (and window-system rmail-mail-new-frame)
2383 (compose-mail to subject others
2384 noerase 'switch-to-buffer-other-frame
2385 yank-action sendactions)
2386 ;; This is not a standard frame parameter;
2387 ;; nothing except sendmail.el looks at it.
2388 (modify-frame-parameters (selected-frame)
2389 '((mail-dedicated-frame . t))))
2390 (compose-mail to subject others
2391 noerase 'switch-to-buffer-other-window
2392 yank-action sendactions)))))
2394 (defun rmail-mail ()
2395 "Send mail in another window.
2396 While composing the message, use \\[mail-yank-original] to yank the
2397 original message into it."
2399 (rmail-start-mail nil nil nil nil nil rmail-view-buffer))
2401 (defun rmail-continue ()
2402 "Continue composing outgoing message previously being composed."
2404 (rmail-start-mail t))
2406 (defun rmail-reply (just-sender)
2407 "Reply to the current message.
2408 Normally include CC: to all other recipients of original message;
2409 prefix argument means ignore them. While composing the reply,
2410 use \\[mail-yank-original] to yank the original message into it."
2412 (let (from reply-to cc subject date to message-id references
2413 resent-to resent-cc resent-reply-to
2414 (msgnum rmail-current-message))
2418 (goto-char (rmail-msgbeg rmail-current-message))
2420 (if (= (following-char) ?0)
2422 (progn (forward-line 2)
2424 (progn (search-forward "\n\n" (rmail-msgend rmail-current-message)
2427 (narrow-to-region (point)
2428 (progn (search-forward "\n*** EOOH ***\n")
2429 (beginning-of-line) (point))))
2430 (setq from (mail-fetch-field "from")
2431 reply-to (or (mail-fetch-field "reply-to" nil t)
2433 cc (and (not just-sender)
2434 (mail-fetch-field "cc" nil t))
2435 subject (mail-fetch-field "subject")
2436 date (mail-fetch-field "date")
2437 to (or (mail-fetch-field "to" nil t) "")
2438 message-id (mail-fetch-field "message-id")
2439 references (mail-fetch-field "references" nil nil t)
2440 resent-reply-to (mail-fetch-field "resent-reply-to" nil t)
2441 resent-cc (and (not just-sender)
2442 (mail-fetch-field "resent-cc" nil t))
2443 resent-to (or (mail-fetch-field "resent-to" nil t) "")
2444 ;;; resent-subject (mail-fetch-field "resent-subject")
2445 ;;; resent-date (mail-fetch-field "resent-date")
2446 ;;; resent-message-id (mail-fetch-field "resent-message-id")
2448 ;; Merge the resent-to and resent-cc into the to and cc.
2449 (if (and resent-to (not (equal resent-to "")))
2450 (if (not (equal to ""))
2451 (setq to (concat to ", " resent-to))
2452 (setq to resent-to)))
2453 (if (and resent-cc (not (equal resent-cc "")))
2454 (if (not (equal cc ""))
2455 (setq cc (concat cc ", " resent-cc))
2456 (setq cc resent-cc)))
2457 ;; Add `Re: ' to subject if not there already.
2458 (and (stringp subject)
2460 (concat rmail-reply-prefix
2461 (if (let ((case-fold-search t))
2462 (string-match rmail-reply-regexp subject))
2463 (substring subject (match-end 0))
2465 (rmail-start-mail nil
2466 (mail-strip-quoted-names reply-to)
2468 (rmail-make-in-reply-to-field from date message-id)
2471 (let* ((cc-list (rmail-dont-reply-to
2472 (mail-strip-quoted-names
2473 (if (null cc) to (concat to ", " cc))))))
2474 (if (string= cc-list "") nil cc-list)))
2476 (list (list 'rmail-mark-message
2478 (aref rmail-msgref-vector msgnum)
2481 (list (cons "References" (concat (mapconcat 'identity references " ")
2482 " " message-id))))))
2484 (defun rmail-mark-message (buffer msgnum-list attribute)
2485 "Give BUFFER's message number in MSGNUM-LIST the attribute ATTRIBUTE.
2486 This is use in the send-actions for message buffers.
2487 MSGNUM-LIST is a list of the form (MSGNUM)
2488 which is an element of rmail-msgref-vector."
2491 (if (car msgnum-list)
2492 (rmail-set-attribute attribute t (car msgnum-list)))))
2494 (defun rmail-make-in-reply-to-field (from date message-id)
2501 (let ((tem (car (rfc822-addresses from))))
2504 (regexp-quote (if (string-match "@[^@]*\\'" tem)
2505 (substring tem 0 (match-beginning 0))
2508 ;; Message-ID is sufficiently informative
2510 (concat message-id " (" tem ")"))
2511 ;; Copy TEM, discarding text properties.
2512 (setq tem (copy-sequence tem))
2513 (set-text-properties 0 (length tem) nil tem)
2514 (setq tem (copy-sequence tem))
2515 ;; Use prin1 to fake RFC822 quoting
2516 (let ((field (prin1-to-string tem)))
2518 (concat field "'s message of " date)
2520 ((let* ((foo "[^][\000-\037\177-\377()<>@,;:\\\" ]+")
2521 (bar "[^][\000-\037\177-\377()<>@,;:\\\"]+"))
2522 ;; Can't use format because format loses on \000 (unix *^&%*^&%$!!)
2523 (or (string-match (concat "\\`[ \t]*\\(" bar
2524 "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
2525 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
2527 (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
2528 bar "\\))[ \t]*\\'")
2529 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
2531 (let ((start (match-beginning 1))
2532 (end (match-end 1)))
2533 ;; Trim whitespace which above regexp match allows
2534 (while (and (< start end)
2535 (memq (aref from start) '(?\t ?\ )))
2536 (setq start (1+ start)))
2537 (while (and (< start end)
2538 (memq (aref from (1- end)) '(?\t ?\ )))
2539 (setq end (1- end)))
2540 (let ((field (substring from start end)))
2541 (if date (setq field (concat "message from " field " on " date)))
2543 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
2544 (concat message-id " (" field ")")
2547 ;; If we can't kludge it simply, do it correctly
2548 (let ((mail-use-rfc822 t))
2549 (rmail-make-in-reply-to-field from date message-id)))))
2551 (defun rmail-forward (resend)
2552 "Forward the current message to another user.
2553 With prefix argument, \"resend\" the message instead of forwarding it;
2554 see the documentation of `rmail-resend'."
2557 (call-interactively 'rmail-resend)
2558 (let ((forward-buffer (current-buffer))
2559 (msgnum rmail-current-message)
2560 (subject (concat "["
2561 (let ((from (or (mail-fetch-field "From")
2562 (mail-fetch-field ">From"))))
2564 (concat (mail-strip-quoted-names from) ": ")
2566 (or (mail-fetch-field "Subject") "")
2568 (if (rmail-start-mail
2569 nil nil subject nil nil nil
2570 (list (list 'rmail-mark-message
2572 (aref rmail-msgref-vector msgnum)
2574 ;; If only one window, use it for the mail buffer.
2575 ;; Otherwise, use another window for the mail buffer
2576 ;; so that the Rmail buffer remains visible
2577 ;; and sending the mail will get back to it.
2578 (and (not rmail-mail-new-frame) (one-window-p t)))
2579 ;; The mail buffer is now current.
2581 ;; Insert after header separator--before signature if any.
2582 (goto-char (point-min))
2583 (search-forward-regexp
2584 (concat "^" (regexp-quote mail-header-separator) "$"))
2586 (insert "------- Start of forwarded message -------\n")
2587 ;; Quote lines with `- ' if they start with `-'.
2588 (let ((beg (point)) end)
2589 (setq end (point-marker))
2590 (set-marker-insertion-type end t)
2591 (insert-buffer-substring forward-buffer)
2593 (while (re-search-forward "^-" end t)
2598 (skip-chars-backward "\n")
2601 (delete-region (point) end)
2602 (set-marker end nil))
2603 (insert "------- End of forwarded message -------\n")
2606 (defun rmail-resend (address &optional from comment mail-alias-file)
2607 "Resend current message to ADDRESSES.
2608 ADDRESSES should be a single address, a string consisting of several
2609 addresses separated by commas, or a list of addresses.
2611 Optional FROM is the address to resend the message from, and
2612 defaults to the username of the person redistributing the message.
2613 Optional COMMENT is a string that will be inserted as a comment in the
2615 Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
2616 typically for purposes of moderating a list."
2617 (interactive "sResend to: ")
2619 (require 'mailalias)
2620 (if (not from) (setq from (user-login-name)))
2621 (let ((tembuf (generate-new-buffer " sendmail temp"))
2622 (mail-header-separator "")
2623 (case-fold-search nil)
2624 (mailbuf (current-buffer)))
2627 ;;>> Copy message into temp buffer
2629 (insert-buffer-substring mailbuf)
2630 (goto-char (point-min))
2631 ;; Delete any Sender field, since that's not specifiable.
2632 ; Only delete Sender fields in the actual header.
2633 (re-search-forward "^$" nil 'move)
2634 ; Using "while" here rather than "if" because some buggy mail
2635 ; software may have inserted multiple Sender fields.
2636 (while (re-search-backward "^Sender:" nil t)
2640 (while (looking-at "[ \t]")
2642 (delete-region beg (point))))
2643 ; Go back to the beginning of the buffer so the Resent- fields
2644 ; are inserted there.
2645 (goto-char (point-min))
2646 ;;>> Insert resent-from:
2647 (insert "Resent-From: " from "\n")
2648 (insert "Resent-Date: " (mail-rfc822-date) "\n")
2649 ;;>> Insert resent-to: and bcc if need be.
2650 (let ((before (point)))
2652 (insert "Resent-Bcc: " (user-login-name) "\n"))
2653 (insert "Resent-To: " (if (stringp address)
2655 (mapconcat 'identity address ",\n\t"))
2657 ;; Expand abbrevs in the recipients.
2659 (if (featurep 'mailabbrev)
2660 (let ((end (point-marker))
2661 (local-abbrev-table mail-abbrevs)
2662 (old-syntax-table (syntax-table)))
2663 (if (and (not (vectorp mail-abbrevs))
2664 (file-exists-p mail-personal-alias-file))
2665 (build-mail-abbrevs))
2666 (set-syntax-table mail-abbrev-syntax-table)
2668 (while (and (< (point) end)
2669 (progn (forward-word 1)
2672 (set-syntax-table old-syntax-table))
2673 (expand-mail-aliases before (point)))))
2674 ;;>> Set up comment, if any.
2675 (if (and (sequencep comment) (not (zerop (length comment))))
2676 (let ((before (point))
2679 (or (eolp) (insert "\n"))
2680 (setq after (point))
2682 (while (< (point) after)
2683 (insert "Resent-Comment: ")
2685 ;; Don't expand aliases in the destination fields
2686 ;; of the original message.
2688 (funcall send-mail-function)))
2689 (kill-buffer tembuf))
2690 (rmail-set-attribute "resent" t rmail-current-message)))
2692 (defvar mail-unsent-separator
2693 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
2694 "^ *---+ +Returned message +---+ *$\\|"
2695 "^Start of returned message$\\|"
2696 "^ *---+ +Original message +---+ *$\\|"
2697 "^ *--+ +begin message +--+ *$\\|"
2698 "^ *---+ +Original message follows +---+ *$\\|"
2699 "^|? *---+ +Message text follows: +---+ *|?$")
2700 "A regexp that matches the separator before the text of a failed message.")
2702 (defvar mail-mime-unsent-header "^Content-Type: message/rfc822 *$"
2703 "A regexp that matches the header of a MIME body part with a failed message.")
2705 (defun rmail-retry-failure ()
2706 "Edit a mail message which is based on the contents of the current message.
2707 For a message rejected by the mail system, extract the interesting headers and
2708 the body of the original message.
2709 If the failed message is a MIME multipart message, it is searched for a
2710 body part with a header which matches the variable `mail-mime-unsent-header'.
2711 Otherwise, the variable `mail-unsent-separator' should match the string that
2712 delimits the returned original message.
2713 The variable `rmail-retry-ignored-headers' is a regular expression
2714 specifying headers which should not be copied into the new message."
2716 (require 'mail-utils)
2717 (let ((rmail-buffer (current-buffer))
2718 (msgnum rmail-current-message)
2719 bounce-start bounce-end bounce-indent resending)
2721 ;; Narrow down to just the quoted original message
2722 (rmail-beginning-of-message)
2723 (let* ((case-fold-search t)
2727 ;; Fetch any content-type header in current message
2728 (search-forward "\n\n") (narrow-to-region top (point))
2729 (mail-fetch-field "Content-Type") )) )
2730 ;; Handle MIME multipart bounce messages
2731 (if (and content-type
2733 ";[\n\t ]*boundary=\"?\\([-0-9a-z'()+_,./:=?]+\\)\"?"
2737 (substring content-type (match-beginning 1)
2739 (or (re-search-forward mail-mime-unsent-header nil t)
2740 (error "Cannot find beginning of header in failed message"))
2741 (or (search-forward "\n\n" nil t)
2742 (error "Cannot find end of Mime data in failed message"))
2743 (setq bounce-start (point))
2744 (or (search-forward codestring nil t)
2745 (error "Cannot find end of Mime data in failed message"))
2746 (setq bounce-end (match-beginning 0))
2747 ; (or (search-forward "\n\n" nil t)
2748 ; (error "Cannot find end of header in failed message"))
2751 (or (re-search-forward mail-unsent-separator nil t)
2752 (error "Cannot parse this as a failure message"))
2753 (skip-chars-forward "\n")
2754 ;; Support a style of failure message in which the original
2755 ;; message is indented, and included within lines saying
2756 ;; `Start of returned message' and `End of returned message'.
2757 (if (looking-at " +Received:")
2759 (setq bounce-start (point))
2760 (skip-chars-forward " ")
2761 (setq bounce-indent (- (current-column)))
2762 (goto-char (point-max))
2763 (re-search-backward "^End of returned message$" nil t)
2764 (setq bounce-end (point)))
2765 ;; One message contained a few random lines before the old
2766 ;; message header. The first line of the message started with
2767 ;; two hyphens. A blank line followed these random lines.
2768 ;; The same line beginning with two hyphens was possibly
2769 ;; marking the end of the message.
2770 (if (looking-at "^--")
2771 (let ((boundary (buffer-substring-no-properties
2773 (progn (end-of-line) (point)))))
2774 (search-forward "\n\n")
2775 (skip-chars-forward "\n")
2776 (setq bounce-start (point))
2777 (goto-char (point-max))
2778 (search-backward (concat "\n\n" boundary) bounce-start t)
2779 (setq bounce-end (point)))
2780 (setq bounce-start (point)
2781 bounce-end (point-max)))
2782 (or (search-forward "\n\n" nil t)
2783 (error "Cannot find end of header in failed message"))
2785 ;; Start sending a new message; default header fields from the original.
2786 ;; Turn off the usual actions for initializing the message body
2787 ;; because we want to get only the text from the failure message.
2788 (let (mail-signature mail-setup-hook)
2789 (if (rmail-start-mail nil nil nil nil nil rmail-buffer
2790 (list (list 'rmail-mark-message
2792 (aref rmail-msgref-vector msgnum)
2794 ;; Insert original text as initial text of new draft message.
2795 ;; Bind inhibit-read-only since the header delimiter
2796 ;; of the previous message was probably read-only.
2797 (let ((inhibit-read-only t))
2799 (insert-buffer-substring rmail-buffer bounce-start bounce-end)
2800 (goto-char (point-min))
2802 (indent-rigidly (point-min) (point-max) bounce-indent))
2803 (rmail-clear-headers rmail-retry-ignored-headers)
2804 (rmail-clear-headers "^sender:\\|^from:\\|^return-path:")
2805 (goto-char (point-min))
2807 (search-forward "\n\n")
2809 (narrow-to-region (point-min) (point))
2810 (setq resending (mail-fetch-field "resent-to"))
2813 (insert "Resent-Bcc: " (user-login-name) "\n")
2814 (insert "BCC: " (user-login-name) "\n"))))
2815 (insert mail-header-separator)
2816 (mail-position-on-field (if resending "Resent-To" "To") t)
2817 (set-buffer rmail-buffer)
2818 (rmail-beginning-of-message))))))
2820 (defun rmail-summary-exists ()
2821 "Non-nil iff in an RMAIL buffer and an associated summary buffer exists.
2822 In fact, the non-nil value returned is the summary buffer itself."
2823 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
2824 rmail-summary-buffer))
2826 (defun rmail-summary-displayed ()
2827 "t iff in RMAIL buffer and an associated summary buffer is displayed."
2828 (and rmail-summary-buffer (get-buffer-window rmail-summary-buffer)))
2830 (defcustom rmail-redisplay-summary nil
2831 "*Non-nil means Rmail should show the summary when it changes.
2832 This has an effect only if a summary buffer exists."
2834 :group 'rmail-summary)
2836 (defcustom rmail-summary-window-size nil
2837 "*Non-nil means specify the height for an Rmail summary window."
2839 :group 'rmail-summary)
2841 ;; Put the summary buffer back on the screen, if user wants that.
2842 (defun rmail-maybe-display-summary ()
2843 (let ((selected (selected-window))
2845 ;; If requested, make sure the summary is displayed.
2846 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
2847 rmail-redisplay-summary
2848 (if (get-buffer-window rmail-summary-buffer 0)
2849 ;; It's already in some frame; show that one.
2850 (let ((frame (window-frame
2851 (get-buffer-window rmail-summary-buffer 0))))
2852 (make-frame-visible frame)
2853 (raise-frame frame))
2854 (display-buffer rmail-summary-buffer)))
2855 ;; If requested, set the height of the summary window.
2856 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
2857 rmail-summary-window-size
2858 (setq window (get-buffer-window rmail-summary-buffer))
2859 ;; Don't try to change the size if just one window in frame.
2860 (not (eq window (frame-root-window (window-frame window))))
2863 (select-window window)
2864 (enlarge-window (- rmail-summary-window-size (window-height))))
2865 (select-window selected)))))
2867 ;;;; *** Rmail Local Fontification ***
2869 (defun rmail-fontify-buffer-function ()
2870 ;; This function's symbol is bound to font-lock-fontify-buffer-function.
2871 (make-local-hook 'rmail-show-message-hook)
2872 (add-hook 'rmail-show-message-hook 'rmail-fontify-message nil t)
2873 ;; If we're already showing a message, fontify it now.
2874 (if rmail-current-message (rmail-fontify-message))
2875 ;; Prevent Font Lock mode from kicking in.
2876 (setq font-lock-fontified t))
2878 (defun rmail-unfontify-buffer-function ()
2879 ;; This function's symbol is bound to font-lock-fontify-unbuffer-function.
2880 (let ((modified (buffer-modified-p))
2881 (buffer-undo-list t) (inhibit-read-only t)
2882 before-change-functions after-change-functions
2883 buffer-file-name buffer-file-truename)
2886 (remove-hook 'rmail-show-message-hook 'rmail-fontify-message t)
2887 (remove-text-properties (point-min) (point-max) '(rmail-fontified nil))
2888 (font-lock-default-unfontify-buffer)
2889 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))
2891 (defun rmail-fontify-message ()
2892 ;; Fontify the current message if it is not already fontified.
2893 (if (text-property-any (point-min) (point-max) 'rmail-fontified nil)
2894 (let ((modified (buffer-modified-p))
2895 (buffer-undo-list t) (inhibit-read-only t)
2896 before-change-functions after-change-functions
2897 buffer-file-name buffer-file-truename)
2900 (add-text-properties (point-min) (point-max) '(rmail-fontified t))
2901 (font-lock-fontify-region (point-min) (point-max))
2902 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))))
2906 ;;; rmail.el ends here