1 ;;; rmail.el --- main code of "RMAIL" mail reader for Emacs.
3 ;; Copyright (C) 1985,86,87,88,93,94,95,96,97,1998
4 ;; Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; Souped up by shane@mit-ajax based on ideas of rlk@athena.mit.edu
29 ;; New features include attribute and keyword support, message
30 ;; selection by dispatch table, summary by attributes and keywords,
31 ;; expunging by dispatch table, sticky options for file commands.
33 ;; Extended by Bob Weiner of Motorola
34 ;; New features include: rmail and rmail-summary buffers remain
35 ;; synchronized and key bindings basically operate the same way in both
36 ;; buffers, summary by topic or by regular expression, rmail-reply-prefix
37 ;; variable, and a bury rmail buffer (wipe) command.
42 ; These variables now declared in paths.el.
43 ;(defvar rmail-spool-directory "/usr/spool/mail/"
44 ; "This is the name of the directory used by the system mailer for\n\
45 ;delivering new mail. Its name should end with a slash.")
46 ;(defvar rmail-file-name
47 ; (expand-file-name "~/RMAIL")
51 "Mail reader for Emacs."
54 (defgroup rmail-retrieve nil
55 "Rmail retrieval options."
59 (defgroup rmail-files nil
64 (defgroup rmail-headers nil
65 "Rmail header options."
69 (defgroup rmail-reply nil
70 "Rmail reply options."
74 (defgroup rmail-summary nil
75 "Rmail summary options."
77 :prefix "rmail-summary-"
80 (defgroup rmail-output nil
81 "Output message to a file."
82 :prefix "rmail-output-"
87 (defcustom rmail-movemail-program nil
88 "If non-nil, name of program for fetching new mail."
89 :group 'rmail-retrieve
92 (defcustom rmail-pop-password nil
93 "*Password to use when reading mail from a POP server, if required."
94 :type '(choice (string :tag "Password")
95 (const :tag "Not Required" nil))
96 :group 'rmail-retrieve)
98 (defcustom rmail-pop-password-required nil
99 "*Non-nil if a password is required when reading mail using POP."
101 :group 'rmail-retrieve)
103 (defcustom rmail-movemail-flags nil
104 "*List of flags to pass to movemail.
105 Most commonly used to specify `-g' to enable GSS-API authentication
106 or `-k' to enable Kerberos authentication."
107 :type '(repeat string)
108 :group 'rmail-retrieve
111 (defvar rmail-pop-password-error "invalid usercode or password"
112 "Regular expression matching incorrect-password POP server error messages.
113 If you get an incorrect-password error that this expression does not match,
114 please report it with \\[report-emacs-bug].")
116 (defcustom rmail-preserve-inbox nil
117 "*Non-nil if incoming mail should be left in the user's inbox,
118 rather than deleted, after it is retrieved."
120 :group 'rmail-retrieve)
123 (defcustom rmail-dont-reply-to-names nil "\
124 *A regexp specifying names to prune of reply to messages.
125 A value of nil means exclude your own name only."
126 :type '(choice regexp (const :tag "Your Name" nil))
130 (defvar rmail-default-dont-reply-to-names "info-" "\
131 A regular expression specifying part of the value of the default value of
132 the variable `rmail-dont-reply-to-names', for when the user does not set
133 `rmail-dont-reply-to-names' explicitly. (The other part of the default
134 value is the user's name.)
135 It is useful to set this variable in the site customization file.")
138 (defcustom rmail-ignored-headers "^via:\\|^mail-from:\\|^origin:\\|^references:\\|^status:\\|^received:\\|^x400-originator:\\|^x400-recipients:\\|^x400-received:\\|^x400-mts-identifier:\\|^x400-content-type:\\|^\\(resent-\\|\\)message-id:\\|^summary-line:\\|^resent-date:\\|^nntp-posting-host:\\|^path:\\|^x-char.*:\\|^x-face:\\|^x-mailer:\\|^delivered-to:\\|^lines:\\|^mime-version:\\|^content-transfer-encoding:\\|^x-coding-system:\\|^return-path:\\|^errors-to:\\|^return-receipt-to:\\|^x-attribution:\\|^x-disclaimer:"
139 "*Regexp to match header fields that Rmail should normally hide."
141 :group 'rmail-headers)
144 (defcustom rmail-displayed-headers nil
145 "*Regexp to match Header fields that Rmail should display.
146 If nil, display all header fields except those matched by
147 `rmail-ignored-headers'."
148 :type '(choice regexp (const :tag "All"))
149 :group 'rmail-headers)
152 (defcustom rmail-retry-ignored-headers nil "\
153 *Headers that should be stripped when retrying a failed message."
154 :type '(choice regexp (const nil :tag "None"))
155 :group 'rmail-headers)
158 (defcustom rmail-highlighted-headers "^From:\\|^Subject:" "\
159 *Regexp to match Header fields that Rmail should normally highlight.
160 A value of nil means don't highlight.
161 See also `rmail-highlight-face'."
163 :group 'rmail-headers)
166 (defcustom rmail-highlight-face nil "\
167 *Face used by Rmail for highlighting headers."
168 :type '(choice (const :tag "Default" nil)
170 :group 'rmail-headers)
173 (defcustom rmail-delete-after-output nil "\
174 *Non-nil means automatically delete a message that is copied to a file."
179 (defcustom rmail-primary-inbox-list nil "\
180 *List of files which are inboxes for user's primary mail file `~/RMAIL'.
181 `nil' means the default, which is (\"/usr/spool/mail/$USER\")
182 \(the name varies depending on the operating system,
183 and the value of the environment variable MAIL overrides it)."
184 ;; Don't use backquote here, because we don't want to need it
186 :type (list 'choice '(const :tag "Default" nil)
187 (list 'repeat ':value (or (getenv "MAIL")
188 (concat "/var/spool/mail/"
191 :group 'rmail-retrieve
195 (defcustom rmail-mail-new-frame nil
196 "*Non-nil means Rmail makes a new frame for composing outgoing mail."
201 (defcustom rmail-secondary-file-directory "~/"
202 "*Directory for additional secondary Rmail files."
206 (defcustom rmail-secondary-file-regexp "\\.xmail$"
207 "*Regexp for which files are secondary Rmail files."
212 (defvar rmail-mode-hook nil
213 "List of functions to call when Rmail is invoked.")
216 (defvar rmail-get-new-mail-hook nil
217 "List of functions to call when Rmail has retrieved new mail.")
220 (defvar rmail-show-message-hook nil
221 "List of functions to call when Rmail displays a message.")
224 (defvar rmail-delete-message-hook nil
225 "List of functions to call when Rmail deletes a message.
226 When the hooks are called, the message has been marked deleted but is
227 still the current message in the Rmail buffer.")
229 ;; These may be altered by site-init.el to match the format of mmdf files
230 ;; delimiting used on a given host (delim1 and delim2 from the config
233 (defvar rmail-mmdf-delim1 "^\001\001\001\001\n"
234 "Regexp marking the start of an mmdf message")
235 (defvar rmail-mmdf-delim2 "^\001\001\001\001\n"
236 "Regexp marking the end of an mmdf message")
238 (defcustom rmail-message-filter nil
239 "If non-nil, a filter function for new messages in RMAIL.
240 Called with region narrowed to the message, including headers,
241 before obeying `rmail-ignored-headers'."
242 :group 'rmail-headers
245 (defvar rmail-reply-prefix "Re: "
246 "String to prepend to Subject line when replying to a message.")
248 ;; Some mailers use "Re(2):" or "Re^2:" or "Re: Re:" or "Re[2]:".
249 ;; This pattern should catch all the common variants.
250 (defvar rmail-reply-regexp "\\`\\(Re\\(([0-9]+)\\|\\[[0-9]+\\]\\|\\^[0-9]+\\)?: *\\)*"
251 "Regexp to delete from Subject line before inserting `rmail-reply-prefix'.")
253 (defcustom rmail-display-summary nil
254 "*If non-nil, Rmail always displays the summary buffer."
255 :group 'rmail-summary
258 (defvar rmail-inbox-list nil)
259 (put 'rmail-inbox-list 'permanent-local t)
261 (defvar rmail-keywords nil)
262 (put 'rmail-keywords 'permanent-local t)
264 (defvar rmail-buffer nil
265 "The RMAIL buffer related to the current buffer.
266 In an RMAIL buffer, this holds the RMAIL buffer itself.
267 In a summary buffer, this holds the RMAIL buffer it is a summary for.")
268 (put 'rmail-buffer 'permanent-local t)
270 ;; Message counters and markers. Deleted flags.
272 (defvar rmail-current-message nil)
273 (put 'rmail-current-message 'permanent-local t)
275 (defvar rmail-total-messages nil)
276 (put 'rmail-total-messages 'permanent-local t)
278 (defvar rmail-message-vector nil)
279 (put 'rmail-message-vector 'permanent-local t)
281 (defvar rmail-deleted-vector nil)
282 (put 'rmail-deleted-vector 'permanent-local t)
284 (defvar rmail-msgref-vector nil
285 "In an Rmail buffer, a vector whose Nth element is a list (N).
286 When expunging renumbers messages, these lists are modified
287 by substituting the new message number into the existing list.")
288 (put 'rmail-msgref-vector 'permanent-local t)
290 (defvar rmail-overlay-list nil)
291 (put 'rmail-overlay-list 'permanent-local t)
293 ;; These are used by autoloaded rmail-summary.
295 (defvar rmail-summary-buffer nil)
296 (put 'rmail-summary-buffer 'permanent-local t)
297 (defvar rmail-summary-vector nil)
298 (put 'rmail-summary-vector 'permanent-local t)
300 (defvar rmail-view-buffer nil
301 "Buffer which holds RMAIL message for MIME displaying.")
302 (put 'rmail-view-buffer 'permanent-local t)
304 ;; `Sticky' default variables.
306 ;; Last individual label specified to a or k.
307 (defvar rmail-last-label nil)
308 (put 'rmail-last-label 'permanent-local t)
310 ;; Last set of values specified to C-M-n, C-M-p, C-M-s or C-M-l.
311 (defvar rmail-last-multi-labels nil)
313 (defvar rmail-last-regexp nil)
314 (put 'rmail-last-regexp 'permanent-local t)
316 (defcustom rmail-default-file "~/xmail"
317 "*Default file name for \\[rmail-output]."
320 (defcustom rmail-default-rmail-file "~/XMAIL"
321 "*Default file name for \\[rmail-output-to-rmail-file]."
324 (defcustom rmail-default-body-file "~/mailout"
325 "*Default file name for \\[rmail-output-body-to-file]."
330 ;; Mule and MIME related variables.
333 (defvar rmail-file-coding-system nil
334 "Coding system used in RMAIL file.
336 This is set to nil by default.")
339 (defcustom rmail-enable-mime nil
340 "*If non-nil, RMAIL uses MIME feature.
341 If the value is t, RMAIL automatically shows MIME decoded message.
342 If the value is neither t nor nil, RMAIL does not show MIME decoded message
343 until a user explicitly requires it."
344 :type '(choice (const :tag "on" t)
345 (const :tag "off" nil)
346 (sexp :tag "when asked" :format "%t\n" ask))
350 (defvar rmail-show-mime-function nil
351 "Function to show MIME decoded message of RMAIL file.")
354 (defvar rmail-mime-feature 'rmail-mime
355 "Feature to require to load MIME support in Rmail.
356 When starting Rmail, if `rmail-enable-mime' is non-nil,
357 this feature is required with `require'.")
359 ;;; Regexp matching the delimiter of messages in UNIX mail format
360 ;;; (UNIX From lines), minus the initial ^. Note that if you change
361 ;;; this expression, you must change the code in rmail-nuke-pinhead-header
362 ;;; that knows the exact ordering of the \\( \\) subexpressions.
363 (defvar rmail-unix-mail-delimiter
364 (let ((time-zone-regexp
365 (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
366 "\\|[-+]?[0-9][0-9][0-9][0-9]"
372 ;; Many things can happen to an RFC 822 mailbox before it is put into
373 ;; a `From' line. The leading phrase can be stripped, e.g.
374 ;; `Joe <@w.x:joe@y.z>' -> `<@w.x:joe@y.z>'. The <> can be stripped, e.g.
375 ;; `<@x.y:joe@y.z>' -> `@x.y:joe@y.z'. Everything starting with a CRLF
376 ;; can be removed, e.g.
377 ;; From: joe@y.z (Joe K
379 ;; can yield `From joe@y.z (Joe K Fri Mar 22 08:11:15 1996', and
382 ;; can yield `From Joe User Fri Mar 22 08:11:15 1996'.
383 ;; The mailbox can be removed or be replaced by white space, e.g.
384 ;; From: "Joe User"{space}{tab}
386 ;; can yield `From {space}{tab} Fri Mar 22 08:11:15 1996',
387 ;; where {space} and {tab} represent the Ascii space and tab characters.
388 ;; We want to match the results of any of these manglings.
389 ;; The following regexp rejects names whose first characters are
390 ;; obviously bogus, but after that anything goes.
391 "\\([^\0-\b\n-\r\^?].*\\)? "
393 ;; The time the message was sent.
394 "\\([^\0-\r \^?]+\\) +" ; day of the week
395 "\\([^\0-\r \^?]+\\) +" ; month
396 "\\([0-3]?[0-9]\\) +" ; day of month
397 "\\([0-2][0-9]:[0-5][0-9]\\(:[0-6][0-9]\\)?\\) *" ; time of day
399 ;; Perhaps a time zone, specified by an abbreviation, or by a
404 " \\([0-9][0-9]+\\) *"
406 ;; On some systems the time zone can appear after the year, too.
410 "\\(remote from .*\\)?"
415 (defvar rmail-font-lock-keywords
417 (let* ((cite-chars "[>|}]")
418 (cite-prefix "A-Za-z")
419 (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
420 (list '("^\\(From\\|Sender\\):" . font-lock-function-name-face)
421 '("^Reply-To:.*$" . font-lock-function-name-face)
422 '("^Subject:" . font-lock-comment-face)
423 '("^\\(To\\|Apparently-To\\|Cc\\|Newsgroups\\):"
424 . font-lock-keyword-face)
425 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
427 (,(concat "\\=[ \t]*"
428 "\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
429 "\\(" cite-chars "[ \t]*\\)\\)+"
431 (beginning-of-line) (end-of-line)
432 (2 font-lock-constant-face nil t)
433 (4 font-lock-comment-face nil t)))
434 '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\|Date\\):.*$"
435 . font-lock-string-face))))
436 "Additional expressions to highlight in Rmail mode.")
438 ;; Perform BODY in the summary buffer
439 ;; in such a way that its cursor is properly updated in its own window.
440 (defmacro rmail-select-summary (&rest body)
441 (` (let ((total rmail-total-messages))
442 (if (rmail-summary-displayed)
443 (let ((window (selected-window)))
447 (pop-to-buffer rmail-summary-buffer)
448 ;; rmail-total-messages is a buffer-local var
449 ;; in the rmail buffer.
450 ;; This way we make it available for the body
451 ;; even tho the rmail buffer is not current.
452 (let ((rmail-total-messages total))
454 (select-window window))))
456 (set-buffer rmail-summary-buffer)
457 (let ((rmail-total-messages total))
459 (rmail-maybe-display-summary))))
461 ;;;; *** Rmail Mode ***
463 (defvar rmail-enable-multibyte nil)
466 (defun rmail (&optional file-name-arg)
467 "Read and edit incoming mail.
468 Moves messages into file named by `rmail-file-name' (a babyl format file)
469 and edits that file in RMAIL Mode.
470 Type \\[describe-mode] once editing that file, for a list of RMAIL commands.
472 May be called with file name as argument; then performs rmail editing on
473 that file, but does not copy any new mail into the file.
474 Interactively, if you supply a prefix argument, then you
475 have a chance to specify a file name with the minibuffer.
477 If `rmail-display-summary' is non-nil, make a summary for this RMAIL file."
478 (interactive (if current-prefix-arg
479 (list (read-file-name "Run rmail on RMAIL file: "))))
480 (if rmail-enable-mime
482 (require rmail-mime-feature)
483 (error (message "Feature `%s' not provided" rmail-mime-feature)
485 (setq rmail-enable-mime nil))))
486 (let* ((file-name (expand-file-name (or file-name-arg rmail-file-name)))
487 (existed (get-file-buffer file-name))
488 ;; This binding is necessary because we much decide if we
489 ;; need code conversion while the buffer is unibyte
490 ;; (i.e. enable-multibyte-characters is nil).
491 (rmail-enable-multibyte (default-value 'enable-multibyte-characters))
492 ;; Since the file may contain messages of different encodings
493 ;; at the tail (non-BYBYL part), we can't decode them at once
494 ;; on reading. So, at first, we read the file without text
495 ;; code conversion, then decode the messages one by one by
496 ;; rmail-decode-babyl-format or
497 ;; rmail-convert-to-babyl-format.
498 (coding-system-for-read (and rmail-enable-multibyte 'raw-text))
499 run-mail-hook msg-shown)
500 ;; Like find-file, but in the case where a buffer existed
501 ;; and the file was reverted, recompute the message-data.
502 ;; We used to bind enable-local-variables to nil here,
503 ;; but that should not be needed now that rmail-mode
504 ;; sets it locally to nil.
505 ;; (Binding a variable locally with let is not safe if it has
506 ;; buffer-local bindings.)
507 (if (and existed (not (verify-visited-file-modtime existed)))
509 (find-file file-name)
510 (if (and (verify-visited-file-modtime existed)
511 (eq major-mode 'rmail-mode))
512 (progn (rmail-forget-messages)
513 (rmail-set-message-counters))))
514 (find-file file-name))
515 (if (eq major-mode 'rmail-edit-mode)
516 (error "Exit Rmail Edit mode before getting new mail"))
517 (if (and existed (> (buffer-size) 0))
518 ;; Buffer not new and not empty; ensure in proper mode, but that's all.
519 (or (eq major-mode 'rmail-mode)
520 (progn (rmail-mode-2)
521 (setq run-mail-hook t)))
522 (setq run-mail-hook t)
524 ;; Convert all or part to Babyl file if possible.
526 ;; As we have read a file by raw-text, the buffer is set to
527 ;; unibyte. We must make it multibyte if necessary.
528 (if (and rmail-enable-multibyte
529 (not enable-multibyte-characters))
530 (set-buffer-multibyte t))
531 (goto-char (point-max))
532 (if (null rmail-inbox-list)
534 (rmail-set-message-counters)
536 (setq msg-shown t))))
538 (or (and (null file-name-arg)
539 (rmail-get-new-mail))
540 (or msg-shown (rmail-show-message (rmail-first-unseen-message))))
542 (if rmail-display-summary (rmail-summary))
543 (rmail-construct-io-menu)
545 (run-hooks 'rmail-mode-hook))))))
547 ;; Given the value of MAILPATH, return a list of inbox file names.
548 ;; This is turned off because it is not clear that the user wants
549 ;; all these inboxes to feed into the primary rmail file.
550 ; (defun rmail-convert-mailpath (string)
552 ; (while (setq idx (string-match "[%:]" string))
553 ; (let ((this (substring string 0 idx)))
554 ; (setq string (substring string (1+ idx)))
555 ; (setq list (cons (if (string-match "%" this)
556 ; (substring this 0 (string-match "%" this))
561 ; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
562 ; will not cause emacs 18.55 problems.
564 ;; This calls rmail-decode-babyl-format if the file is already Babyl.
566 (defun rmail-convert-file ()
569 (goto-char (point-min))
570 ;; If file doesn't start like a Babyl file,
571 ;; convert it to one, by adding a header and converting each message.
572 (cond ((looking-at "BABYL OPTIONS:"))
573 ((looking-at "Version: 5\n")
574 ;; Losing babyl file made by old version of Rmail.
575 ;; Just fix the babyl file header; don't make a new one,
576 ;; so we don't lose the Labels: file attribute, etc.
577 (let ((buffer-read-only nil))
578 (insert "BABYL OPTIONS: -*- rmail -*-\n")))
579 ((equal (point-min) (point-max))
580 ;; Empty RMAIL file. Just insert the header.
581 (rmail-insert-rmail-file-header))
583 ;; Non-empty file in non-RMAIL format. Add header and convert.
585 (rmail-insert-rmail-file-header)))
586 ;; If file was not a Babyl file or if there are
587 ;; Unix format messages added at the end,
588 ;; convert file as necessary.
591 (goto-char (point-max))
592 (search-backward "\n\^_")
594 (looking-at "\n*From ")))
595 (let ((buffer-read-only nil))
596 (message "Converting to Babyl format...")
597 ;; If file needs conversion, convert it all,
598 ;; except for the BABYL header.
599 ;; (rmail-convert-to-babyl-format would delete the header.)
600 (goto-char (point-min))
601 (search-forward "\n\^_" nil t)
602 (narrow-to-region (point) (point-max))
603 (rmail-convert-to-babyl-format)
604 (message "Converting to Babyl format...done"))
605 (if (and (not rmail-enable-mime)
606 rmail-enable-multibyte)
607 ;; We still have to decode BABYL part.
608 (rmail-decode-babyl-format)))))
610 (defun rmail-insert-rmail-file-header ()
611 (let ((buffer-read-only nil))
612 ;; -*-rmail-*- is here so that visiting the file normally
613 ;; recognizes it as an Rmail file.
614 (insert "BABYL OPTIONS: -*- rmail -*-
617 Note: This is the header of an rmail file.
618 Note: If you are seeing it in rmail,
619 Note: it means the file has no messages in it.\n\^_")))
621 ;; Decode Babyl formated part at the head of current buffer by
622 ;; rmail-file-coding-system, or if it is nil, do auto conversion.
624 (defun rmail-decode-babyl-format ()
625 (let ((modifiedp (buffer-modified-p))
626 (buffer-read-only nil)
627 (coding-system rmail-file-coding-system)
629 (goto-char (point-min))
630 (search-forward "\n\^_" nil t) ; Skip BABYL header.
632 (goto-char (point-max))
633 (search-backward "\n\^_" from 'mv)
635 (unless (and coding-system
636 (coding-system-p coding-system))
637 (setq coding-system (detect-coding-region from to t)))
638 (unless (eq coding-system 'undecided)
639 (decode-coding-region from to coding-system)
640 (setq coding-system last-coding-system-used))
641 (set-buffer-modified-p modifiedp)
642 (setq buffer-file-coding-system nil)
643 (setq save-buffer-coding-system
644 (or coding-system 'undecided))))
646 (defvar rmail-mode-map nil)
649 (setq rmail-mode-map (make-keymap))
650 (suppress-keymap rmail-mode-map)
651 (define-key rmail-mode-map "a" 'rmail-add-label)
652 (define-key rmail-mode-map "b" 'rmail-bury)
653 (define-key rmail-mode-map "c" 'rmail-continue)
654 (define-key rmail-mode-map "d" 'rmail-delete-forward)
655 (define-key rmail-mode-map "\C-d" 'rmail-delete-backward)
656 (define-key rmail-mode-map "e" 'rmail-edit-current-message)
657 (define-key rmail-mode-map "f" 'rmail-forward)
658 (define-key rmail-mode-map "g" 'rmail-get-new-mail)
659 (define-key rmail-mode-map "h" 'rmail-summary)
660 (define-key rmail-mode-map "i" 'rmail-input)
661 (define-key rmail-mode-map "j" 'rmail-show-message)
662 (define-key rmail-mode-map "k" 'rmail-kill-label)
663 (define-key rmail-mode-map "l" 'rmail-summary-by-labels)
664 (define-key rmail-mode-map "\e\C-h" 'rmail-summary)
665 (define-key rmail-mode-map "\e\C-l" 'rmail-summary-by-labels)
666 (define-key rmail-mode-map "\e\C-r" 'rmail-summary-by-recipients)
667 (define-key rmail-mode-map "\e\C-s" 'rmail-summary-by-regexp)
668 (define-key rmail-mode-map "\e\C-t" 'rmail-summary-by-topic)
669 (define-key rmail-mode-map "m" 'rmail-mail)
670 (define-key rmail-mode-map "\em" 'rmail-retry-failure)
671 (define-key rmail-mode-map "n" 'rmail-next-undeleted-message)
672 (define-key rmail-mode-map "\en" 'rmail-next-message)
673 (define-key rmail-mode-map "\e\C-n" 'rmail-next-labeled-message)
674 (define-key rmail-mode-map "o" 'rmail-output-to-rmail-file)
675 (define-key rmail-mode-map "\C-o" 'rmail-output)
676 (define-key rmail-mode-map "p" 'rmail-previous-undeleted-message)
677 (define-key rmail-mode-map "\ep" 'rmail-previous-message)
678 (define-key rmail-mode-map "\e\C-p" 'rmail-previous-labeled-message)
679 (define-key rmail-mode-map "q" 'rmail-quit)
680 (define-key rmail-mode-map "r" 'rmail-reply)
681 ;; I find I can't live without the default M-r command -- rms.
682 ;; (define-key rmail-mode-map "\er" 'rmail-search-backwards)
683 (define-key rmail-mode-map "s" 'rmail-expunge-and-save)
684 (define-key rmail-mode-map "\es" 'rmail-search)
685 (define-key rmail-mode-map "t" 'rmail-toggle-header)
686 (define-key rmail-mode-map "u" 'rmail-undelete-previous-message)
687 (define-key rmail-mode-map "w" 'rmail-output-body-to-file)
688 (define-key rmail-mode-map "x" 'rmail-expunge)
689 (define-key rmail-mode-map "." 'rmail-beginning-of-message)
690 (define-key rmail-mode-map "<" 'rmail-first-message)
691 (define-key rmail-mode-map ">" 'rmail-last-message)
692 (define-key rmail-mode-map " " 'scroll-up)
693 (define-key rmail-mode-map "\177" 'scroll-down)
694 (define-key rmail-mode-map "?" 'describe-mode)
695 (define-key rmail-mode-map "\C-c\C-s\C-d" 'rmail-sort-by-date)
696 (define-key rmail-mode-map "\C-c\C-s\C-s" 'rmail-sort-by-subject)
697 (define-key rmail-mode-map "\C-c\C-s\C-a" 'rmail-sort-by-author)
698 (define-key rmail-mode-map "\C-c\C-s\C-r" 'rmail-sort-by-recipient)
699 (define-key rmail-mode-map "\C-c\C-s\C-c" 'rmail-sort-by-correspondent)
700 (define-key rmail-mode-map "\C-c\C-s\C-l" 'rmail-sort-by-lines)
701 (define-key rmail-mode-map "\C-c\C-s\C-k" 'rmail-sort-by-keywords)
702 (define-key rmail-mode-map "\C-c\C-n" 'rmail-next-same-subject)
703 (define-key rmail-mode-map "\C-c\C-p" 'rmail-previous-same-subject)
706 (define-key rmail-mode-map [menu-bar] (make-sparse-keymap))
708 (define-key rmail-mode-map [menu-bar classify]
709 (cons "Classify" (make-sparse-keymap "Classify")))
711 (define-key rmail-mode-map [menu-bar classify input-menu]
714 (define-key rmail-mode-map [menu-bar classify output-menu]
717 (define-key rmail-mode-map [menu-bar classify output-body]
718 '("Output body to file..." . rmail-output-body-to-file))
720 (define-key rmail-mode-map [menu-bar classify output-inbox]
721 '("Output (inbox)..." . rmail-output))
723 (define-key rmail-mode-map [menu-bar classify output]
724 '("Output (Rmail)..." . rmail-output-to-rmail-file))
726 (define-key rmail-mode-map [menu-bar classify kill-label]
727 '("Kill Label..." . rmail-kill-label))
729 (define-key rmail-mode-map [menu-bar classify add-label]
730 '("Add Label..." . rmail-add-label))
732 (define-key rmail-mode-map [menu-bar summary]
733 (cons "Summary" (make-sparse-keymap "Summary")))
735 (define-key rmail-mode-map [menu-bar summary senders]
736 '("By Senders..." . rmail-summary-by-senders))
738 (define-key rmail-mode-map [menu-bar summary labels]
739 '("By Labels..." . rmail-summary-by-labels))
741 (define-key rmail-mode-map [menu-bar summary recipients]
742 '("By Recipients..." . rmail-summary-by-recipients))
744 (define-key rmail-mode-map [menu-bar summary topic]
745 '("By Topic..." . rmail-summary-by-topic))
747 (define-key rmail-mode-map [menu-bar summary regexp]
748 '("By Regexp..." . rmail-summary-by-regexp))
750 (define-key rmail-mode-map [menu-bar summary all]
751 '("All" . rmail-summary))
753 (define-key rmail-mode-map [menu-bar mail]
754 (cons "Mail" (make-sparse-keymap "Mail")))
756 (define-key rmail-mode-map [menu-bar mail rmail-get-new-mail]
757 '("Get New Mail" . rmail-get-new-mail))
759 (define-key rmail-mode-map [menu-bar mail lambda]
762 (define-key rmail-mode-map [menu-bar mail continue]
763 '("Continue" . rmail-continue))
765 (define-key rmail-mode-map [menu-bar mail resend]
766 '("Re-send..." . rmail-resend))
768 (define-key rmail-mode-map [menu-bar mail forward]
769 '("Forward" . rmail-forward))
771 (define-key rmail-mode-map [menu-bar mail retry]
772 '("Retry" . rmail-retry-failure))
774 (define-key rmail-mode-map [menu-bar mail reply]
775 '("Reply" . rmail-reply))
777 (define-key rmail-mode-map [menu-bar mail mail]
778 '("Mail" . rmail-mail))
780 (define-key rmail-mode-map [menu-bar delete]
781 (cons "Delete" (make-sparse-keymap "Delete")))
783 (define-key rmail-mode-map [menu-bar delete expunge/save]
784 '("Expunge/Save" . rmail-expunge-and-save))
786 (define-key rmail-mode-map [menu-bar delete expunge]
787 '("Expunge" . rmail-expunge))
789 (define-key rmail-mode-map [menu-bar delete undelete]
790 '("Undelete" . rmail-undelete-previous-message))
792 (define-key rmail-mode-map [menu-bar delete delete]
793 '("Delete" . rmail-delete-forward))
795 (define-key rmail-mode-map [menu-bar move]
796 (cons "Move" (make-sparse-keymap "Move")))
798 (define-key rmail-mode-map [menu-bar move search-back]
799 '("Search Back..." . rmail-search-backwards))
801 (define-key rmail-mode-map [menu-bar move search]
802 '("Search..." . rmail-search))
804 (define-key rmail-mode-map [menu-bar move previous]
805 '("Previous Nondeleted" . rmail-previous-undeleted-message))
807 (define-key rmail-mode-map [menu-bar move next]
808 '("Next Nondeleted" . rmail-next-undeleted-message))
810 (define-key rmail-mode-map [menu-bar move last]
811 '("Last" . rmail-last-message))
813 (define-key rmail-mode-map [menu-bar move first]
814 '("First" . rmail-first-message))
816 (define-key rmail-mode-map [menu-bar move previous]
817 '("Previous" . rmail-previous-message))
819 (define-key rmail-mode-map [menu-bar move next]
820 '("Next" . rmail-next-message))
822 ;; Rmail mode is suitable only for specially formatted data.
823 (put 'rmail-mode 'mode-class 'special)
825 (defun rmail-mode-kill-summary ()
826 (if rmail-summary-buffer (kill-buffer rmail-summary-buffer)))
830 "Rmail Mode is used by \\<rmail-mode-map>\\[rmail] for editing Rmail files.
831 All normal editing commands are turned off.
832 Instead, these commands are available:
834 \\[rmail-beginning-of-message] Move point to front of this message (same as \\[beginning-of-buffer]).
835 \\[scroll-up] Scroll to next screen of this message.
836 \\[scroll-down] Scroll to previous screen of this message.
837 \\[rmail-next-undeleted-message] Move to Next non-deleted message.
838 \\[rmail-previous-undeleted-message] Move to Previous non-deleted message.
839 \\[rmail-next-message] Move to Next message whether deleted or not.
840 \\[rmail-previous-message] Move to Previous message whether deleted or not.
841 \\[rmail-first-message] Move to the first message in Rmail file.
842 \\[rmail-last-message] Move to the last message in Rmail file.
843 \\[rmail-show-message] Jump to message specified by numeric position in file.
844 \\[rmail-search] Search for string and show message it is found in.
845 \\[rmail-delete-forward] Delete this message, move to next nondeleted.
846 \\[rmail-delete-backward] Delete this message, move to previous nondeleted.
847 \\[rmail-undelete-previous-message] Undelete message. Tries current message, then earlier messages
848 till a deleted message is found.
849 \\[rmail-edit-current-message] Edit the current message. \\[rmail-cease-edit] to return to Rmail.
850 \\[rmail-expunge] Expunge deleted messages.
851 \\[rmail-expunge-and-save] Expunge and save the file.
852 \\[rmail-quit] Quit Rmail: expunge, save, then switch to another buffer.
853 \\[save-buffer] Save without expunging.
854 \\[rmail-get-new-mail] Move new mail from system spool directory into this file.
855 \\[rmail-mail] Mail a message (same as \\[mail-other-window]).
856 \\[rmail-continue] Continue composing outgoing message started before.
857 \\[rmail-reply] Reply to this message. Like \\[rmail-mail] but initializes some fields.
858 \\[rmail-retry-failure] Send this message again. Used on a mailer failure message.
859 \\[rmail-forward] Forward this message to another user.
860 \\[rmail-output-to-rmail-file] Output this message to an Rmail file (append it).
861 \\[rmail-output] Output this message to a Unix-format mail file (append it).
862 \\[rmail-output-body-to-file] Save message body to a file. Default filename comes from Subject line.
863 \\[rmail-input] Input Rmail file. Run Rmail on that file.
864 \\[rmail-add-label] Add label to message. It will be displayed in the mode line.
865 \\[rmail-kill-label] Kill label. Remove a label from current message.
866 \\[rmail-next-labeled-message] Move to Next message with specified label
867 (label defaults to last one specified).
868 Standard labels: filed, unseen, answered, forwarded, deleted.
869 Any other label is present only if you add it with \\[rmail-add-label].
870 \\[rmail-previous-labeled-message] Move to Previous message with specified label
871 \\[rmail-summary] Show headers buffer, with a one line summary of each message.
872 \\[rmail-summary-by-labels] Summarize only messages with particular label(s).
873 \\[rmail-summary-by-recipients] Summarize only messages with particular recipient(s).
874 \\[rmail-summary-by-regexp] Summarize only messages with particular regexp(s).
875 \\[rmail-summary-by-topic] Summarize only messages with subject line regexp(s).
876 \\[rmail-toggle-header] Toggle display of complete header."
879 (rmail-set-message-counters)
880 (rmail-show-message rmail-total-messages)
881 (run-hooks 'rmail-mode-hook))
883 (defun rmail-mode-2 ()
884 (kill-all-local-variables)
886 (rmail-perm-variables)
889 (defun rmail-mode-1 ()
890 (setq major-mode 'rmail-mode)
891 (setq mode-name "RMAIL")
892 (setq buffer-read-only t)
893 ;; No need to auto save RMAIL files in normal circumstances
894 ;; because they contain no info except attribute changes
895 ;; and deletion of messages.
896 ;; The one exception is when messages are copied into an Rmail mode buffer.
897 ;; rmail-output-to-rmail-file enables auto save when you do that.
898 (setq buffer-auto-save-file-name nil)
899 (setq mode-line-modified "--")
900 (use-local-map rmail-mode-map)
901 (set-syntax-table text-mode-syntax-table)
902 (setq local-abbrev-table text-mode-abbrev-table))
904 ;; Set up the permanent locals associated with an Rmail file.
905 (defun rmail-perm-variables ()
906 (make-local-variable 'rmail-last-label)
907 (make-local-variable 'rmail-last-regexp)
908 (make-local-variable 'rmail-deleted-vector)
909 (make-local-variable 'rmail-buffer)
910 (setq rmail-buffer (current-buffer))
911 (make-local-variable 'rmail-view-buffer)
912 (setq rmail-view-buffer rmail-buffer)
913 (make-local-variable 'rmail-summary-buffer)
914 (make-local-variable 'rmail-summary-vector)
915 (make-local-variable 'rmail-current-message)
916 (make-local-variable 'rmail-total-messages)
917 (make-local-variable 'rmail-overlay-list)
918 (setq rmail-overlay-list nil)
919 (make-local-variable 'rmail-message-vector)
920 (make-local-variable 'rmail-msgref-vector)
921 (make-local-variable 'rmail-inbox-list)
922 (setq rmail-inbox-list (rmail-parse-file-inboxes))
923 ;; Provide default set of inboxes for primary mail file ~/RMAIL.
924 (and (null rmail-inbox-list)
925 (or (equal buffer-file-name (expand-file-name rmail-file-name))
926 (equal buffer-file-truename
927 (abbreviate-file-name (file-truename rmail-file-name))))
928 (setq rmail-inbox-list
929 (or rmail-primary-inbox-list
930 (list (or (getenv "MAIL")
931 (concat rmail-spool-directory
932 (user-login-name)))))))
933 (make-local-variable 'rmail-keywords)
934 ;; this gets generated as needed
935 (setq rmail-keywords nil))
937 ;; Set up the non-permanent locals associated with Rmail mode.
938 (defun rmail-variables ()
939 (make-local-variable 'save-buffer-coding-system)
940 ;; If we don't already have a value for save-buffer-coding-system,
941 ;; get it from buffer-file-coding-system, and clear that
942 ;; because it should be determined in rmail-show-message.
943 (unless save-buffer-coding-system
944 (setq save-buffer-coding-system (or buffer-file-coding-system 'undecided))
945 (setq buffer-file-coding-system nil))
946 ;; Don't let a local variables list in a message cause confusion.
947 (make-local-variable 'enable-local-variables)
948 (setq enable-local-variables nil)
949 (make-local-variable 'revert-buffer-function)
950 (setq revert-buffer-function 'rmail-revert)
951 (make-local-variable 'font-lock-defaults)
952 (setq font-lock-defaults
953 '(rmail-font-lock-keywords
955 (font-lock-maximum-size . nil)
956 (font-lock-fontify-buffer-function . rmail-fontify-buffer-function)
957 (font-lock-unfontify-buffer-function . rmail-unfontify-buffer-function)
958 (font-lock-inhibit-thing-lock . (lazy-lock-mode fast-lock-mode))))
959 (make-local-variable 'require-final-newline)
960 (setq require-final-newline nil)
961 (make-local-variable 'version-control)
962 (setq version-control 'never)
963 (make-local-variable 'kill-buffer-hook)
964 (add-hook 'kill-buffer-hook 'rmail-mode-kill-summary)
965 (make-local-variable 'file-precious-flag)
966 (setq file-precious-flag t))
968 ;; Handle M-x revert-buffer done in an rmail-mode buffer.
969 (defun rmail-revert (arg noconfirm)
970 (let ((revert-buffer-function (default-value 'revert-buffer-function)))
971 ;; Call our caller again, but this time it does the default thing.
972 (if (revert-buffer arg noconfirm)
973 ;; If the user said "yes", and we changed something,
974 ;; reparse the messages.
977 (goto-char (point-max))
980 ;; Return a list of files from this buffer's Mail: option.
981 ;; Does not assume that messages have been parsed.
982 ;; Just returns nil if buffer does not look like Babyl format.
983 (defun rmail-parse-file-inboxes ()
988 (cond ((looking-at "BABYL OPTIONS:")
989 (search-forward "\n\^_" nil 'move)
990 (narrow-to-region 1 (point))
992 (if (search-forward "\nMail:" nil t)
994 (narrow-to-region (point) (progn (end-of-line) (point)))
995 (goto-char (point-min))
996 (mail-parse-comma-list))))))))
998 (defun rmail-expunge-and-save ()
999 "Expunge and save RMAIL file."
1003 (if (rmail-summary-exists)
1004 (rmail-select-summary (set-buffer-modified-p nil))))
1006 (defun rmail-quit ()
1007 "Quit out of RMAIL."
1009 (rmail-expunge-and-save)
1010 ;; Don't switch to the summary buffer even if it was recently visible.
1011 (when rmail-summary-buffer
1012 (replace-buffer-in-windows rmail-summary-buffer)
1013 (bury-buffer rmail-summary-buffer))
1014 (let ((obuf (current-buffer)))
1016 (replace-buffer-in-windows obuf)))
1018 (defun rmail-bury ()
1019 "Bury current Rmail buffer and its summary buffer."
1021 ;; This let var was called rmail-buffer, but that interfered
1022 ;; with the buffer-local var used in summary buffers.
1023 (let ((buffer-to-bury (current-buffer)))
1024 (if (rmail-summary-exists)
1026 (while (setq window (get-buffer-window rmail-summary-buffer))
1027 (quit-window nil window))
1028 (bury-buffer rmail-summary-buffer)))
1031 (defun rmail-duplicate-message ()
1032 "Create a duplicated copy of the current message.
1033 The duplicate copy goes into the Rmail file just after the
1037 (let ((buffer-read-only nil)
1038 (number rmail-current-message)
1039 (string (buffer-substring (rmail-msgbeg rmail-current-message)
1040 (rmail-msgend rmail-current-message))))
1041 (goto-char (rmail-msgend rmail-current-message))
1043 (rmail-forget-messages)
1044 (rmail-show-message number)
1045 (message "Message duplicated")))
1048 (defun rmail-input (filename)
1049 "Run Rmail on file FILENAME."
1050 (interactive "FRun rmail on RMAIL file: ")
1054 ;; This used to scan subdirectories recursively, but someone pointed out
1055 ;; that if the user wants that, person can put all the files in one dir.
1056 ;; And the recursive scan was slow. So I took it out.
1058 (defun rmail-find-all-files (start)
1059 "Return list of file in dir START that match `rmail-secondary-file-regexp'."
1060 (if (file-accessible-directory-p start)
1062 (let* ((case-fold-search t)
1063 (files (directory-files start t rmail-secondary-file-regexp)))
1064 ;; Sort here instead of in directory-files
1065 ;; because this list is usually much shorter.
1066 (sort files 'string<))))
1068 (defun rmail-list-to-menu (menu-name l action &optional full-name)
1069 (let ((menu (make-sparse-keymap menu-name)))
1071 (function (lambda (item)
1076 (rmail-list-to-menu (car item) (cdr item)
1079 (concat full-name "/"
1082 (setq name (car item)))
1086 (list 'lambda () '(interactive)
1090 (concat full-name "/" item)
1092 rmail-secondary-file-directory))))))
1093 (define-key menu (vector (intern name))
1094 (cons name command)))))
1098 ;; This command is always "disabled" when it appears in a menu.
1099 (put 'rmail-disable-menu 'menu-enable ''nil)
1101 (defun rmail-construct-io-menu ()
1102 (let ((files (rmail-find-all-files rmail-secondary-file-directory)))
1105 (define-key rmail-mode-map [menu-bar classify input-menu]
1106 (cons "Input Rmail File"
1107 (rmail-list-to-menu "Input Rmail File"
1110 (define-key rmail-mode-map [menu-bar classify output-menu]
1111 (cons "Output Rmail File"
1112 (rmail-list-to-menu "Output Rmail File"
1114 'rmail-output-to-rmail-file))))
1116 (define-key rmail-mode-map [menu-bar classify input-menu]
1117 '("Input Rmail File" . rmail-disable-menu))
1118 (define-key rmail-mode-map [menu-bar classify output-menu]
1119 '("Output Rmail File" . rmail-disable-menu)))))
1122 ;;;; *** Rmail input ***
1124 ;; RLK feature not added in this version:
1125 ;; argument specifies inbox file or files in various ways.
1127 (defun rmail-get-new-mail (&optional file-name)
1128 "Move any new mail from this RMAIL file's inbox files.
1129 The inbox files can be specified with the file's Mail: option. The
1130 variable `rmail-primary-inbox-list' specifies the inboxes for your
1131 primary RMAIL file if it has no Mail: option. By default, this is
1132 your /usr/spool/mail/$USER.
1134 You can also specify the file to get new mail from. In this case, the
1135 file of new mail is not changed or deleted. Noninteractively, you can
1136 pass the inbox file name as an argument. Interactively, a prefix
1137 argument causes us to read a file name and use that file as the inbox.
1139 If the variable `rmail-preserve-inbox' is non-nil, new mail will
1140 always be left in inbox files rather than deleted.
1142 This function runs `rmail-get-new-mail-hook' before saving the updated file.
1143 It returns t if it got any new messages."
1145 (list (if current-prefix-arg
1146 (read-file-name "Get new mail from file: "))))
1147 (run-hooks 'rmail-before-get-new-mail-hook)
1148 ;; If the disk file has been changed from under us,
1149 ;; revert to it before we get new mail.
1150 (or (verify-visited-file-modtime (current-buffer))
1151 (find-file (buffer-file-name)))
1152 (rmail-maybe-set-message-counters)
1154 ;; Get rid of all undo records for this buffer.
1155 (or (eq buffer-undo-list t)
1156 (setq buffer-undo-list nil))
1157 (let ((all-files (if file-name (list file-name)
1159 (rmail-enable-multibyte (default-value 'enable-multibyte-characters))
1164 (let ((opoint (point))
1167 ;; If buffer has not changed yet, and has not been saved yet,
1168 ;; don't replace the old backup file now.
1169 (make-backup-files (and make-backup-files (buffer-modified-p)))
1170 (buffer-read-only nil)
1171 ;; Don't make undo records for what we do in getting mail.
1172 (buffer-undo-list t)
1174 ;; Files to insert this time around.
1176 ;; Last names of those files.
1178 ;; Pull files off all-files onto files
1179 ;; as long as there is no name conflict.
1180 ;; A conflict happens when two inbox file names
1181 ;; have the same last component.
1182 (while (and all-files
1183 (not (member (file-name-nondirectory (car all-files))
1185 (setq files (cons (car all-files) files)
1187 (cons (file-name-nondirectory (car all-files)) files))
1188 (setq all-files (cdr all-files)))
1189 ;; Put them back in their original order.
1190 (setq files (nreverse files))
1192 (goto-char (point-max))
1193 (skip-chars-backward " \t\n") ; just in case of brain damage
1194 (delete-region (point) (point-max)) ; caused by require-final-newline
1197 (narrow-to-region (point) (point))
1198 ;; Read in the contents of the inbox files,
1199 ;; renaming them as necessary,
1200 ;; and adding to the list of files to delete eventually.
1202 (rmail-insert-inbox-text files nil)
1203 (setq delete-files (rmail-insert-inbox-text files t)))
1204 ;; Scan the new text and convert each message to babyl format.
1205 (goto-char (point-min))
1208 (setq new-messages (rmail-convert-to-babyl-format)
1210 ;; If we could not convert the file's inboxes,
1211 ;; rename the files we tried to read
1212 ;; so we won't over and over again.
1213 (if (and (not file-name) (not success))
1214 (let ((delfiles delete-files)
1216 ;; Try to delete the garbage just inserted.
1217 (delete-region (point-min) (point-max))
1219 (while (file-exists-p (format "RMAILOSE.%d" count))
1220 (setq count (1+ count)))
1221 (rename-file (car delfiles)
1222 (format "RMAILOSE.%d" count))
1223 (setq delfiles (cdr delfiles))))))
1224 (or (zerop new-messages)
1227 (search-backward "\n\^_" nil t)
1228 (narrow-to-region (point) (point-max))
1229 (goto-char (1+ (point-min)))
1230 (rmail-count-new-messages)
1231 (run-hooks 'rmail-get-new-mail-hook)
1233 ;; Delete the old files, now that babyl file is saved.
1236 ;; First, try deleting.
1238 (delete-file (car delete-files))
1240 ;; If we can't delete it, truncate it.
1241 (write-region (point) (point) (car delete-files))))
1243 (setq delete-files (cdr delete-files)))))
1244 (if (= new-messages 0)
1245 (progn (goto-char opoint)
1246 (if (or file-name rmail-inbox-list)
1247 (message "(No new mail has arrived)")))
1248 (if (rmail-summary-exists)
1249 (rmail-select-summary
1250 (rmail-update-summary)))
1251 (message "%d new message%s read"
1252 new-messages (if (= 1 new-messages) "" "s"))
1253 ;; Move to the first new message
1254 ;; unless we have other unseen messages before it.
1255 (rmail-show-message (rmail-first-unseen-message))
1256 (run-hooks 'rmail-after-get-new-mail-hook)
1259 ;; Don't leave the buffer screwed up if we get a disk-full error.
1260 (or found (rmail-show-message)))))
1262 (defun rmail-insert-inbox-text (files renamep)
1263 ;; Detect a locked file now, so that we avoid moving mail
1264 ;; out of the real inbox file. (That could scare people.)
1265 (or (memq (file-locked-p buffer-file-name) '(nil t))
1266 (error "RMAIL file %s is locked"
1267 (file-name-nondirectory buffer-file-name)))
1268 (let (file tofile delete-files movemail popmail got-password)
1270 (setq file (file-truename
1271 (expand-file-name (substitute-in-file-name (car files))))
1272 tofile (expand-file-name
1273 ;; Generate name to move to from inbox name,
1274 ;; in case of multiple inboxes that need moving.
1275 (concat ".newmail-" (file-name-nondirectory file))
1276 ;; Use the directory of this rmail file
1277 ;; because it's a nuisance to use the homedir
1278 ;; if that is on a full disk and this rmail
1280 (file-name-directory
1281 (expand-file-name buffer-file-name))))
1282 ;; Always use movemail to rename the file,
1283 ;; since there can be mailboxes in various directories.
1285 ;;; ;; If getting from mail spool directory,
1286 ;;; ;; use movemail to move rather than just renaming,
1287 ;;; ;; so as to interlock with the mailer.
1288 ;;; (setq movemail (string= file
1290 ;;; (concat rmail-spool-directory
1291 ;;; (file-name-nondirectory file)))))
1292 (setq popmail (string-match "^po:" (file-name-nondirectory file)))
1293 (if popmail (setq file (file-name-nondirectory file)
1297 ;; On some systems, /usr/spool/mail/foo is a directory
1298 ;; and the actual inbox is /usr/spool/mail/foo/foo.
1299 (if (file-directory-p file)
1300 (setq file (expand-file-name (user-login-name)
1303 (if (and rmail-pop-password-required (not rmail-pop-password))
1304 (setq rmail-pop-password
1306 (format "Password for %s: "
1307 (substring file (+ popmail 3))))
1309 (if (eq system-type 'windows-nt)
1310 ;; cannot have "po:" in file name
1313 (concat ".newmail-pop-" (substring file (+ popmail 3)))
1314 (file-name-directory
1315 (expand-file-name buffer-file-name)))))
1316 (message "Getting mail from post office ..."))
1317 ((and (file-exists-p tofile)
1318 (/= 0 (nth 7 (file-attributes tofile))))
1319 (message "Getting mail from %s..." tofile))
1320 ((and (file-exists-p file)
1321 (/= 0 (nth 7 (file-attributes file))))
1322 (message "Getting mail from %s..." file)))
1323 ;; Set TOFILE if have not already done so, and
1324 ;; rename or copy the file FILE to TOFILE if and as appropriate.
1325 (cond ((not renamep)
1327 ((or (file-exists-p tofile) (and (not popmail)
1328 (not (file-exists-p file))))
1330 ((and (not movemail) (not popmail))
1331 ;; Try copying. If that fails (perhaps no space) and
1332 ;; we're allowed to blow away the inbox, rename instead.
1333 (if rmail-preserve-inbox
1334 (copy-file file tofile nil)
1336 (copy-file file tofile nil)
1338 ;; Third arg is t so we can replace existing file TOFILE.
1339 (rename-file file tofile t))))
1340 ;; Make the real inbox file empty.
1341 ;; Leaving it deleted could cause lossage
1342 ;; because mailers often won't create the file.
1343 (if (not rmail-preserve-inbox)
1345 (write-region (point) (point) file)
1351 (setq errors (generate-new-buffer " *rmail loss*"))
1352 (buffer-disable-undo errors)
1355 (list (or rmail-movemail-program
1356 (expand-file-name "movemail"
1359 (if rmail-preserve-inbox
1362 rmail-movemail-flags
1364 (if rmail-pop-password
1365 (list rmail-pop-password)
1367 (apply 'call-process args))
1368 (if (not (buffer-modified-p errors))
1369 ;; No output => movemail won
1372 (subst-char-in-region (point-min) (point-max)
1374 (goto-char (point-max))
1375 (skip-chars-backward " \t")
1376 (delete-region (point) (point-max))
1377 (goto-char (point-min))
1378 (if (looking-at "movemail: ")
1379 (delete-region (point-min) (match-end 0)))
1381 (message "movemail: %s"
1382 (buffer-substring (point-min)
1384 ;; If we just read the password, most likely it is
1385 ;; wrong. Otherwise, see if there is a specific
1386 ;; reason to think that the problem is a wrong passwd.
1387 (if (or got-password
1388 (re-search-forward rmail-pop-password-error
1390 (setq rmail-pop-password nil))
1393 (if errors (kill-buffer errors))))))
1394 ;; At this point, TOFILE contains the name to read:
1395 ;; Either the alternate name (if we renamed)
1396 ;; or the actual inbox (if not renaming).
1397 (if (file-exists-p tofile)
1398 (let ((coding-system-for-read 'no-conversion)
1400 (goto-char (point-max))
1401 (setq size (nth 1 (insert-file-contents tofile)))
1402 (goto-char (point-max))
1403 (or (= (preceding-char) ?\n)
1406 (if (not (and rmail-preserve-inbox (string= file tofile)))
1407 (setq delete-files (cons tofile delete-files)))))
1409 (setq files (cdr files)))
1412 (defun rmail-read-passwd (prompt &optional default)
1413 "Read a password, echoing `.' for each character typed.
1414 End with RET, LFD, or ESC. DEL or C-h rubs out. C-u kills line.
1415 Optional DEFAULT is password to start with."
1416 (let ((pass (if default default ""))
1419 (cursor-in-echo-area t))
1420 (while (progn (message "%s%s"
1422 (make-string (length pass) ?.))
1423 (setq c (read-char))
1424 (and (/= c ?\r) (/= c ?\n) (/= c ?\e)))
1427 (if (and (/= c ?\b) (/= c ?\177))
1428 (setq pass (concat pass (char-to-string c)))
1429 (if (> (length pass) 0)
1430 (setq pass (substring pass 0 -1))))))
1435 ;; the rmail-break-forwarded-messages feature is not implemented
1436 (defun rmail-convert-to-babyl-format ()
1437 (let ((count 0) start
1438 (case-fold-search nil)
1439 (invalid-input-resync
1440 (function (lambda ()
1441 (message "Invalid Babyl format in inbox!")
1443 ;; Try to get back in sync with a real message.
1444 (if (re-search-forward
1445 (concat rmail-mmdf-delim1 "\\|^From") nil t)
1447 (goto-char (point-max)))))))
1448 (goto-char (point-min))
1451 (setq start (point))
1452 (cond ((looking-at "BABYL OPTIONS:");Babyl header
1453 (if (search-forward "\n\^_" nil t)
1454 ;; If we find the proper terminator, delete through there.
1455 (delete-region (point-min) (point))
1456 (funcall invalid-input-resync)
1457 (delete-region (point-min) (point))))
1458 ;; Babyl format message
1460 (or (search-forward "\n\^_" nil t)
1461 (funcall invalid-input-resync))
1462 (setq count (1+ count))
1463 ;; Make sure there is no extra white space after the ^_
1464 ;; at the end of the message.
1465 ;; Narrowing will make sure that whatever follows the junk
1466 ;; will be treated properly.
1467 (delete-region (point)
1469 (skip-chars-forward " \t\n")
1471 (setq last-coding-system-used nil)
1472 (or rmail-enable-mime
1473 (not rmail-enable-multibyte)
1474 (decode-coding-region start (point)
1475 (or rmail-file-coding-system
1477 ;; Add an X-Coding-System: header if we don't have one.
1481 (if (looking-at "0")
1484 (or (save-restriction
1485 (narrow-to-region (point) (point-max))
1487 (goto-char (point-min))
1488 (re-search-forward "^X-Coding-System:" nil t))
1489 (insert "X-Coding-System: "
1490 (symbol-name last-coding-system-used)
1492 (narrow-to-region (point) (point-max)))
1494 ((let ((case-fold-search t))
1495 (looking-at rmail-mmdf-delim1))
1496 (let ((case-fold-search t))
1497 (replace-match "\^L\n0, unseen,,\n*** EOOH ***\n")
1498 (re-search-forward rmail-mmdf-delim2 nil t)
1499 (replace-match "\^_"))
1502 (narrow-to-region start (1- (point)))
1503 (goto-char (point-min))
1504 (while (search-forward "\n\^_" nil t); single char "\^_"
1505 (replace-match "\n^_")))); 2 chars: "^" and "_"
1506 (setq last-coding-system-used nil)
1507 (or rmail-enable-mime
1508 (not rmail-enable-multibyte)
1509 (decode-coding-region start (point) 'undecided))
1513 (insert "X-Coding-System: "
1514 (symbol-name last-coding-system-used)
1516 (narrow-to-region (point) (point-max))
1517 (setq count (1+ count)))
1519 ((looking-at "^From ")
1520 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
1521 (rmail-nuke-pinhead-header)
1522 ;; If this message has a Content-Length field,
1523 ;; skip to the end of the contents.
1524 (let* ((header-end (save-excursion
1525 (and (re-search-forward "\n\n" nil t)
1527 (case-fold-search t)
1528 (quoted-printable-header-field-end
1531 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
1534 ;; Get the numeric value from the Content-Length field.
1536 ;; Back up to end of prev line,
1537 ;; in case the Content-Length field comes first.
1539 (and (search-forward "\ncontent-length: "
1542 (eol (progn (end-of-line) (point))))
1543 (string-to-int (buffer-substring beg eol)))))))
1545 (if (and (natnump size)
1546 (<= (+ header-end size) (point-max))
1547 ;; Make sure this would put us at a position
1548 ;; that we could continue from.
1550 (goto-char (+ header-end size))
1551 (skip-chars-forward "\n")
1553 (and (looking-at "BABYL OPTIONS:")
1554 (search-forward "\n\^_" nil t))
1555 (and (looking-at "\^L")
1556 (search-forward "\n\^_" nil t))
1557 (let ((case-fold-search t))
1558 (looking-at rmail-mmdf-delim1))
1559 (looking-at "From "))))
1560 (goto-char (+ header-end size))
1561 (message "Ignoring invalid Content-Length field")
1563 (if (re-search-forward
1564 (concat "^[\^_]?\\("
1565 rmail-unix-mail-delimiter
1567 rmail-mmdf-delim1 "\\|"
1568 "^BABYL OPTIONS:\\|"
1569 "\^L\n[01],\\)") nil t)
1570 (goto-char (match-beginning 1))
1571 (goto-char (point-max)))
1572 (setq count (1+ count))
1573 (if quoted-printable-header-field-end
1575 (rmail-decode-quoted-printable header-end (point))
1576 ;; Change "quoted-printable" to "8bit",
1577 ;; to reflect the decoding we just did.
1578 (goto-char quoted-printable-header-field-end)
1579 (delete-region (point) (search-backward ":"))
1580 (insert ": 8bit"))))
1584 (narrow-to-region start (point))
1585 (goto-char (point-min))
1586 (while (search-forward "\n\^_" nil t); single char
1587 (replace-match "\n^_")))); 2 chars: "^" and "_"
1589 (setq last-coding-system-used nil)
1590 (or rmail-enable-mime
1591 (not rmail-enable-multibyte)
1592 (decode-coding-region start (point) 'undecided))
1596 (insert "X-Coding-System: "
1597 (symbol-name last-coding-system-used)
1599 (narrow-to-region (point) (point-max)))
1601 ;; This kludge is because some versions of sendmail.el
1602 ;; insert an extra newline at the beginning that shouldn't
1603 ;; be there. sendmail.el has been fixed, but old versions
1604 ;; may still be in use. -- rms, 7 May 1993.
1605 ((eolp) (delete-char 1))
1606 (t (error "Cannot convert to babyl format")))))
1609 (defun rmail-hex-char-to-integer (character)
1610 "Return CHARACTER's value interpreted as a hex digit."
1611 (if (and (>= character ?0) (<= character ?9))
1613 (let ((ch (logior character 32)))
1614 (if (and (>= ch ?a) (<= ch ?f))
1616 (error "Invalid hex digit `%c'" ch)))))
1618 (defun rmail-hex-string-to-integer (hex-string)
1619 "Return decimal integer for HEX-STRING."
1622 (while (< index (length hex-string))
1623 (setq hex-num (+ (* hex-num 16)
1624 (rmail-hex-char-to-integer (aref hex-string index))))
1625 (setq index (1+ index)))
1628 (defun rmail-decode-quoted-printable (from to)
1629 "Decode Quoted-Printable in the region between FROM and TO."
1633 (setq to (copy-marker to)))
1634 (while (search-forward "=" to t)
1635 (cond ((eq (following-char) ?\n)
1638 ((looking-at "[0-9A-F][0-9A-F]")
1639 (subst-char-in-region
1640 (1- (point)) (point) ?=
1641 (rmail-hex-string-to-integer
1642 (buffer-substring (point) (+ 2 (point)))))
1647 (message "Malformed MIME quoted-printable message")))))
1649 ;; Delete the "From ..." line, creating various other headers with
1650 ;; information from it if they don't already exist. Now puts the
1651 ;; original line into a mail-from: header line for debugging and for
1652 ;; use by the rmail-output function.
1653 (defun rmail-nuke-pinhead-header ()
1656 (let ((start (point))
1659 (search-forward "\n\n")
1661 (goto-char (point-max))
1665 (narrow-to-region start end)
1666 (let ((case-fold-search t))
1668 (setq has-from (search-forward "\nFrom:" nil t))
1670 (setq has-date (and (search-forward "\nDate:" nil t) (point)))
1672 (let ((case-fold-search nil))
1673 (if (re-search-forward (concat "^" rmail-unix-mail-delimiter) nil t)
1677 ;; Keep and reformat the date if we don't
1678 ;; have a Date: field.
1682 "Date: \\2, \\4 \\3 \\9 \\5 "
1684 ;; The timezone could be matched by group 7 or group 10.
1685 ;; If neither of them matched, assume EST, since only
1686 ;; Easterners would be so sloppy.
1687 ;; It's a shame the substitution can't use "\\10".
1689 ((/= (match-beginning 7) (match-end 7)) "\\7")
1690 ((/= (match-beginning 10) (match-end 10))
1691 (buffer-substring (match-beginning 10)
1695 ;; Keep and reformat the sender if we don't
1696 ;; have a From: field.
1702 ;;;; *** Rmail Message Formatting and Header Manipulation ***
1704 (defun rmail-reformat-message (beg end)
1707 (if (/= (following-char) ?0)
1708 (error "Bad format in RMAIL file."))
1709 (let ((buffer-read-only nil)
1710 (delta (- (buffer-size) end)))
1714 (let ((case-fold-search t))
1715 (while (looking-at "Summary-line:\\|Mail-From:")
1717 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*\n")
1718 (delete-region (point)
1719 (progn (forward-line 1) (point))))
1720 (let ((str (buffer-substring (point)
1721 (save-excursion (search-forward "\n\n" end 'move)
1723 (insert str "*** EOOH ***\n")
1724 (narrow-to-region (point) (- (buffer-size) delta)))
1725 (goto-char (point-min))
1726 (if rmail-message-filter (funcall rmail-message-filter))
1727 (if (or rmail-displayed-headers rmail-ignored-headers)
1728 (rmail-clear-headers))))
1730 (defun rmail-clear-headers (&optional ignored-headers)
1731 "Delete all header fields that Rmail should not show.
1732 If the optional argument IGNORED-HEADERS is non-nil,
1733 delete all header fields whose names match that regexp.
1734 Otherwise, if `rmail-displayed-headers' is non-nil,
1735 delete all header fields *except* those whose names match that regexp.
1736 Otherwise, delete all header fields whose names match `rmail-ignored-headers'."
1737 (when (search-forward "\n\n" nil t)
1739 (let ((case-fold-search t)
1740 (buffer-read-only nil))
1741 (if (and rmail-displayed-headers (null ignored-headers))
1743 (narrow-to-region (point-min) (point))
1745 (goto-char (point-min))
1746 (while (and (not (eobp))
1748 (if (re-search-forward "\n[^ \t]" nil t)
1749 (setq lim (match-beginning 0)
1751 (setq lim nil next (point-max)))))
1753 (re-search-forward rmail-displayed-headers lim t))
1755 (delete-region (point) next))))
1756 (goto-char (point-min)))
1757 (or ignored-headers (setq ignored-headers rmail-ignored-headers))
1759 (narrow-to-region (point-min) (point))
1761 (goto-char (point-min))
1762 (re-search-forward ignored-headers nil t))
1764 (delete-region (point)
1765 (if (re-search-forward "\n[^ \t]" nil t)
1769 (defun rmail-msg-is-pruned ()
1770 (rmail-maybe-set-message-counters)
1772 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
1774 (goto-char (point-min))
1776 (= (following-char) ?1))))
1778 (defun rmail-toggle-header (&optional arg)
1779 "Show original message header if pruned header currently shown, or vice versa.
1780 With argument ARG, show the message header pruned if ARG is greater than zero;
1781 otherwise, show it in full."
1783 (let* ((buffer-read-only nil)
1784 (pruned (rmail-msg-is-pruned))
1786 (> (prefix-numeric-value arg) 0)
1788 (if (eq pruned prune)
1790 (rmail-maybe-set-message-counters)
1791 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
1793 (progn (goto-char (point-min))
1798 (let ((case-fold-search t))
1799 (while (looking-at "Summary-Line:\\|Mail-From:")
1801 (insert "*** EOOH ***\n")
1803 (search-forward "\n*** EOOH ***\n")
1805 (let ((temp (point)))
1806 (and (search-forward "\n\n" nil t)
1807 (delete-region temp (point))))
1808 (goto-char (point-min))
1809 (search-forward "\n*** EOOH ***\n")
1810 (narrow-to-region (point) (point-max)))
1811 (rmail-reformat-message (point-min) (point-max)))
1812 (rmail-highlight-headers))))
1814 ;;;; *** Rmail Attributes and Keywords ***
1816 ;; Make a string describing current message's attributes and keywords
1817 ;; and set it up as the name of a minor mode
1818 ;; so it will appear in the mode line.
1819 (defun rmail-display-labels ()
1820 (let ((blurb "") (beg (point-min-marker)) (end (point-max-marker)))
1825 (goto-char (rmail-msgbeg rmail-current-message))
1827 (if (looking-at "[01],")
1829 (narrow-to-region (point) (progn (end-of-line) (point)))
1830 ;; Truly valid BABYL format requires a space before each
1831 ;; attribute or keyword name. Put them in if missing.
1832 (let (buffer-read-only)
1833 (goto-char (point-min))
1834 (while (search-forward "," nil t)
1835 (or (looking-at "[ ,]") (eobp)
1837 (goto-char (point-max))
1838 (if (search-backward ",," nil 'move)
1840 (if (> (point) (1+ (point-min)))
1841 (setq blurb (buffer-substring (+ 1 (point-min)) (point))))
1842 (if (> (- (point-max) (point)) 2)
1846 (buffer-substring (+ (point) 3)
1847 (1- (point-max)))))))))))
1848 ;; Note: we don't use save-restriction because that does not work right
1849 ;; if changes are made outside the saved restriction
1850 ;; before that restriction is restored.
1851 (narrow-to-region beg end)
1852 (set-marker beg nil)
1853 (set-marker end nil)))
1854 (while (string-match " +," blurb)
1855 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
1856 (substring blurb (match-end 0)))))
1857 (while (string-match ", +" blurb)
1858 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
1859 (substring blurb (match-end 0)))))
1860 (setq mode-line-process
1862 rmail-current-message rmail-total-messages blurb))))
1864 ;; Turn an attribute of a message on or off according to STATE.
1865 ;; ATTR is the name of the attribute, as a string.
1866 ;; MSGNUM is message number to change; nil means current message.
1867 (defun rmail-set-attribute (attr state &optional msgnum)
1868 (let ((omax (point-max-marker))
1869 (omin (point-min-marker))
1870 (buffer-read-only nil))
1871 (or msgnum (setq msgnum rmail-current-message))
1876 (goto-char (+ 3 (rmail-msgbeg msgnum)))
1879 (null (search-backward (concat ", " attr ",")
1880 (prog1 (point) (end-of-line)) t)))))
1881 (or (eq curstate (not (not state)))
1883 (delete-region (point) (1- (match-end 0)))
1886 (insert " " attr ","))))
1887 (if (string= attr "deleted")
1888 (rmail-set-message-deleted-p msgnum state)))
1889 ;; Note: we don't use save-restriction because that does not work right
1890 ;; if changes are made outside the saved restriction
1891 ;; before that restriction is restored.
1892 (narrow-to-region omin omax)
1893 (set-marker omin nil)
1894 (set-marker omax nil)
1895 (if (= msgnum rmail-current-message)
1896 (rmail-display-labels))))))
1898 ;; Return t if the attributes/keywords line of msg number MSG
1899 ;; contains a match for the regexp LABELS.
1900 (defun rmail-message-labels-p (msg labels)
1904 (goto-char (rmail-msgbeg msg))
1906 (re-search-backward labels (prog1 (point) (end-of-line)) t))))
1908 ;;;; *** Rmail Message Selection And Support ***
1910 (defun rmail-msgend (n)
1911 (marker-position (aref rmail-message-vector (1+ n))))
1913 (defun rmail-msgbeg (n)
1914 (marker-position (aref rmail-message-vector n)))
1916 (defun rmail-widen-to-current-msgbeg (function)
1917 "Call FUNCTION with point at start of internal data of current message.
1918 Assumes that bounds were previously narrowed to display the message in Rmail.
1919 The bounds are widened enough to move point where desired, then narrowed
1922 FUNCTION may not change the visible text of the message, but it may
1923 change the invisible header text."
1925 (let ((obeg (- (point-max) (point-min))))
1928 (narrow-to-region (rmail-msgbeg rmail-current-message)
1930 (goto-char (point-min))
1932 ;; Note: we don't use save-restriction because that does not work right
1933 ;; if changes are made outside the saved restriction
1934 ;; before that restriction is restored.
1935 ;; Here we assume that changes made by FUNCTION
1936 ;; occur before the visible region of the message.
1937 (narrow-to-region (- (point-max) obeg) (point-max))))))
1939 (defun rmail-forget-messages ()
1941 (if (vectorp rmail-message-vector)
1943 (v rmail-message-vector)
1946 (move-marker (aref v i) nil)
1948 (setq rmail-message-vector nil)
1949 (setq rmail-msgref-vector nil)
1950 (setq rmail-deleted-vector nil)))
1952 (defun rmail-maybe-set-message-counters ()
1953 (if (not (and rmail-deleted-vector
1954 rmail-message-vector
1955 rmail-current-message
1956 rmail-total-messages))
1957 (rmail-set-message-counters)))
1959 (defun rmail-count-new-messages (&optional nomsg)
1960 (let* ((case-fold-search nil)
1964 (or nomsg (message "Counting new messages..."))
1965 (goto-char (point-max))
1966 ;; Put at the end of messages-head
1967 ;; the entry for message N+1, which marks
1968 ;; the end of message N. (N = number of messages).
1969 (search-backward "\n\^_")
1971 (setq messages-head (list (point-marker)))
1972 (rmail-set-message-counters-counter (point-min))
1973 (setq rmail-current-message (1+ rmail-total-messages))
1974 (setq rmail-total-messages
1975 (+ rmail-total-messages total-messages))
1976 (setq rmail-message-vector
1977 (vconcat rmail-message-vector (cdr messages-head)))
1978 (aset rmail-message-vector
1979 rmail-current-message (car messages-head))
1980 (setq rmail-deleted-vector
1981 (concat rmail-deleted-vector deleted-head))
1982 (setq rmail-summary-vector
1983 (vconcat rmail-summary-vector (make-vector total-messages nil)))
1984 (setq rmail-msgref-vector
1985 (vconcat rmail-msgref-vector (make-vector total-messages nil)))
1986 ;; Fill in the new elements of rmail-msgref-vector.
1987 (let ((i (1+ (- rmail-total-messages total-messages))))
1988 (while (<= i rmail-total-messages)
1989 (aset rmail-msgref-vector i (list i))
1991 (goto-char (point-min))
1992 (or nomsg (message "Counting new messages...done (%d)" total-messages))))
1994 (defun rmail-set-message-counters ()
1995 (rmail-forget-messages)
1999 (let* ((point-save (point))
2001 (messages-after-point)
2002 (case-fold-search nil)
2005 (message "Counting messages...")
2006 (goto-char (point-max))
2007 ;; Put at the end of messages-head
2008 ;; the entry for message N+1, which marks
2009 ;; the end of message N. (N = number of messages).
2010 (search-backward "\n\^_" nil t)
2011 (if (/= (point) (point-max)) (forward-char 1))
2012 (setq messages-head (list (point-marker)))
2013 (rmail-set-message-counters-counter (min (point) point-save))
2014 (setq messages-after-point total-messages)
2015 (rmail-set-message-counters-counter)
2016 (setq rmail-total-messages total-messages)
2017 (setq rmail-current-message
2019 (max 1 (- total-messages messages-after-point))))
2020 (setq rmail-message-vector
2021 (apply 'vector (cons (point-min-marker) messages-head))
2022 rmail-deleted-vector (concat "D" deleted-head)
2023 rmail-summary-vector (make-vector rmail-total-messages nil)
2024 rmail-msgref-vector (make-vector (1+ rmail-total-messages) nil))
2026 (while (<= i rmail-total-messages)
2027 (aset rmail-msgref-vector i (list i))
2029 (message "Counting messages...done")))))
2031 (defun rmail-set-message-counters-counter (&optional stop)
2032 (while (search-backward "\n\^_\^L\n" stop t)
2034 (setq messages-head (cons (point-marker) messages-head))
2037 (cons (if (search-backward ", deleted,"
2043 (if (zerop (% (setq total-messages (1+ total-messages)) 20))
2044 (message "Counting messages...%d" total-messages))))
2046 (defun rmail-beginning-of-message ()
2047 "Show current message starting from the beginning."
2049 (rmail-show-message rmail-current-message))
2051 (defun rmail-show-message (&optional n no-summary)
2052 "Show message number N (prefix argument), counting from start of file.
2053 If summary buffer is currently displayed, update current message there also."
2055 (or (eq major-mode 'rmail-mode)
2056 (switch-to-buffer rmail-buffer))
2057 (rmail-maybe-set-message-counters)
2059 (if (zerop rmail-total-messages)
2060 (progn (narrow-to-region (point-min) (1- (point-max)))
2061 (goto-char (point-min))
2062 (setq mode-line-process nil))
2063 (let (blurb coding-system)
2065 (setq n rmail-current-message)
2068 rmail-current-message 1
2069 blurb "No previous message"))
2070 ((> n rmail-total-messages)
2071 (setq n rmail-total-messages
2072 rmail-current-message rmail-total-messages
2073 blurb "No following message"))
2075 (setq rmail-current-message n))))
2076 (let ((beg (rmail-msgbeg n)))
2080 (let ((end (rmail-msgend n)))
2082 (if (prog1 (= (following-char) ?0)
2084 (narrow-to-region (point) end))
2086 (search-forward "\n*** EOOH ***\n" end t))
2087 (narrow-to-region beg (point))
2088 (goto-char (point-min))
2089 (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t)
2090 (let ((coding-system (intern (match-string 1))))
2091 (check-coding-system coding-system)
2092 (setq buffer-file-coding-system coding-system))
2093 (setq buffer-file-coding-system nil)))))
2094 ;; Clear the "unseen" attribute when we show a message.
2095 (rmail-set-attribute "unseen" nil)
2096 (let ((end (rmail-msgend n)))
2097 ;; Reformat the header, or else find the reformatted header.
2098 (if (= (following-char) ?0)
2099 (rmail-reformat-message beg end)
2100 (search-forward "\n*** EOOH ***\n" end t)
2101 (narrow-to-region (point) end)))
2102 (goto-char (point-min))
2103 (rmail-display-labels)
2104 (if (eq rmail-enable-mime t)
2105 (funcall rmail-show-mime-function)
2106 (setq rmail-view-buffer rmail-buffer)
2108 (rmail-highlight-headers)
2109 (if transient-mark-mode (deactivate-mark))
2110 (run-hooks 'rmail-show-message-hook)
2111 ;; If there is a summary buffer, try to move to this message
2112 ;; in that buffer. But don't complain if this message
2113 ;; is not mentioned in the summary.
2114 ;; Don't do this at all if we were called on behalf
2115 ;; of cursor motion in the summary buffer.
2116 (and (rmail-summary-exists) (not no-summary)
2117 (let ((curr-msg rmail-current-message))
2118 (rmail-select-summary
2119 (rmail-summary-goto-msg curr-msg t t))))
2121 (message blurb))))))
2123 ;; Find all occurrences of certain fields, and highlight them.
2124 (defun rmail-highlight-headers ()
2125 ;; Do this only if the system supports faces.
2126 (if (and (fboundp 'internal-find-face)
2127 rmail-highlighted-headers)
2129 (search-forward "\n\n" nil 'move)
2131 (narrow-to-region (point-min) (point))
2132 (let ((case-fold-search t)
2133 (inhibit-read-only t)
2134 ;; Highlight with boldface if that is available.
2135 ;; Otherwise use the `highlight' face.
2136 (face (or rmail-highlight-face
2137 (if (face-differs-from-default-p 'bold)
2139 ;; List of overlays to reuse.
2140 (overlays rmail-overlay-list))
2141 (goto-char (point-min))
2142 (while (re-search-forward rmail-highlighted-headers nil t)
2143 (skip-chars-forward " \t")
2146 (while (progn (forward-line 1)
2147 (looking-at "[ \t]")))
2148 ;; Back up over newline, then trailing spaces or tabs
2150 (while (member (preceding-char) '(? ?\t))
2153 ;; Reuse an overlay we already have.
2155 (setq overlay (car overlays)
2156 overlays (cdr overlays))
2157 (overlay-put overlay 'face face)
2158 (move-overlay overlay beg (point)))
2159 ;; Make a new overlay and add it to
2160 ;; rmail-overlay-list.
2161 (setq overlay (make-overlay beg (point)))
2162 (overlay-put overlay 'face face)
2163 (setq rmail-overlay-list
2164 (cons overlay rmail-overlay-list))))))))))
2166 (defun rmail-next-message (n)
2167 "Show following message whether deleted or not.
2168 With prefix arg N, moves forward N messages, or backward if N is negative."
2170 (rmail-maybe-set-message-counters)
2171 (rmail-show-message (+ rmail-current-message n)))
2173 (defun rmail-previous-message (n)
2174 "Show previous message whether deleted or not.
2175 With prefix arg N, moves backward N messages, or forward if N is negative."
2177 (rmail-next-message (- n)))
2179 (defun rmail-next-undeleted-message (n)
2180 "Show following non-deleted message.
2181 With prefix arg N, moves forward N non-deleted messages,
2182 or backward if N is negative.
2184 Returns t if a new message is being shown, nil otherwise."
2186 (rmail-maybe-set-message-counters)
2187 (let ((lastwin rmail-current-message)
2188 (current rmail-current-message))
2189 (while (and (> n 0) (< current rmail-total-messages))
2190 (setq current (1+ current))
2191 (if (not (rmail-message-deleted-p current))
2192 (setq lastwin current n (1- n))))
2193 (while (and (< n 0) (> current 1))
2194 (setq current (1- current))
2195 (if (not (rmail-message-deleted-p current))
2196 (setq lastwin current n (1+ n))))
2197 (if (/= lastwin rmail-current-message)
2198 (progn (rmail-show-message lastwin)
2201 (message "No previous nondeleted message"))
2203 (message "No following nondeleted message"))
2206 (defun rmail-previous-undeleted-message (n)
2207 "Show previous non-deleted message.
2208 With prefix argument N, moves backward N non-deleted messages,
2209 or forward if N is negative."
2211 (rmail-next-undeleted-message (- n)))
2213 (defun rmail-first-message ()
2214 "Show first message in file."
2216 (rmail-maybe-set-message-counters)
2217 (rmail-show-message 1))
2219 (defun rmail-last-message ()
2220 "Show last message in file."
2222 (rmail-maybe-set-message-counters)
2223 (rmail-show-message rmail-total-messages))
2225 (defun rmail-what-message ()
2226 (let ((where (point))
2228 (high rmail-total-messages)
2229 (mid (/ rmail-total-messages 2)))
2230 (while (> (- high low) 1)
2231 (if (>= where (rmail-msgbeg mid))
2234 (setq mid (+ low (/ (- high low) 2))))
2235 (if (>= where (rmail-msgbeg high)) high low)))
2237 (defun rmail-message-recipients-p (msg recipients &optional primary-only)
2239 (goto-char (rmail-msgbeg msg))
2240 (search-forward "\n*** EOOH ***\n")
2241 (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
2242 (or (string-match recipients (or (mail-fetch-field "To") ""))
2243 (string-match recipients (or (mail-fetch-field "From") ""))
2244 (if (not primary-only)
2245 (string-match recipients (or (mail-fetch-field "Cc") ""))))))
2247 (defun rmail-message-regexp-p (msg regexp)
2248 "Return t, if for message number MSG, regexp REGEXP matches in the header."
2250 (goto-char (rmail-msgbeg msg))
2256 (search-forward "\n*** EOOH ***\n" (point-max))
2257 (when (= beg (match-beginning 0))
2259 (search-forward "\n\n" (point-max)))
2262 (re-search-forward regexp end t))))
2264 (defvar rmail-search-last-regexp nil)
2265 (defun rmail-search (regexp &optional n)
2266 "Show message containing next match for REGEXP (but not the current msg).
2267 Prefix argument gives repeat count; negative argument means search
2268 backwards (through earlier messages).
2269 Interactively, empty argument means use same regexp used last time."
2271 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
2273 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
2275 (if rmail-search-last-regexp
2276 (setq prompt (concat prompt
2278 rmail-search-last-regexp
2280 (setq regexp (read-string prompt))
2281 (cond ((not (equal regexp ""))
2282 (setq rmail-search-last-regexp regexp))
2283 ((not rmail-search-last-regexp)
2284 (error "No previous Rmail search string")))
2285 (list rmail-search-last-regexp
2286 (prefix-numeric-value current-prefix-arg))))
2288 (message "%sRmail search for %s..."
2289 (if (< n 0) "Reverse " "")
2291 (rmail-maybe-set-message-counters)
2292 (let ((omin (point-min))
2297 (msg rmail-current-message))
2302 ;; Check messages one by one, advancing message number up or down
2303 ;; but searching forward through each message.
2305 (while (and (null win) (> msg 1))
2306 (goto-char (rmail-msgbeg (setq msg (1- msg))))
2307 (setq win (re-search-forward
2308 regexp (rmail-msgend msg) t)))
2309 (while (and (null win) (< msg rmail-total-messages))
2310 (goto-char (rmail-msgbeg (setq msg (1+ msg))))
2311 (setq win (re-search-forward regexp (rmail-msgend msg) t))))
2312 (setq n (+ n (if reversep 1 -1)))))
2315 ;; If this is a reverse search and we found a message,
2316 ;; search backward thru this message to position point.
2319 (goto-char (rmail-msgend msg))
2321 regexp (rmail-msgbeg msg) t)))
2323 (rmail-show-message msg)
2324 (message "%sRmail search for %s...done"
2325 (if reversep "Reverse " "")
2329 (narrow-to-region omin omax)
2331 (message "Search failed: %s" regexp)))))
2333 (defun rmail-search-backwards (regexp &optional n)
2334 "Show message containing previous match for REGEXP.
2335 Prefix argument gives repeat count; negative argument means search
2336 forward (through later messages).
2337 Interactively, empty argument means use same regexp used last time."
2339 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
2341 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
2343 (if rmail-search-last-regexp
2344 (setq prompt (concat prompt
2346 rmail-search-last-regexp
2348 (setq regexp (read-string prompt))
2349 (cond ((not (equal regexp ""))
2350 (setq rmail-search-last-regexp regexp))
2351 ((not rmail-search-last-regexp)
2352 (error "No previous Rmail search string")))
2353 (list rmail-search-last-regexp
2354 (prefix-numeric-value current-prefix-arg))))
2355 (rmail-search regexp (- (or n 1))))
2357 ;; Show the first message which has the `unseen' attribute.
2358 (defun rmail-first-unseen-message ()
2359 (rmail-maybe-set-message-counters)
2364 (while (and (not found) (<= current rmail-total-messages))
2365 (if (rmail-message-labels-p current ", ?\\(unseen\\),")
2366 (setq found current))
2367 (setq current (1+ current))))
2368 ;; Let the caller show the message.
2370 ;; (rmail-show-message found))
2373 (defun rmail-next-same-subject (n)
2374 "Go to the next mail message having the same subject header.
2375 With prefix argument N, do this N times.
2376 If N is negative, go backwards instead."
2378 (let ((subject (mail-fetch-field "Subject"))
2380 (i rmail-current-message)
2381 (case-fold-search t)
2382 search-regexp found)
2383 (if (string-match "\\`[ \t]+" subject)
2384 (setq subject (substring subject (match-end 0))))
2385 (if (string-match "Re:[ \t]*" subject)
2386 (setq subject (substring subject (match-end 0))))
2387 (if (string-match "[ \t]+\\'" subject)
2388 (setq subject (substring subject 0 (match-beginning 0))))
2389 (setq search-regexp (concat "^Subject: *\\(Re: *\\)?"
2390 (regexp-quote subject)
2395 (while (and (/= n 0)
2397 (< i rmail-total-messages)
2400 (while (and (not done)
2402 (< i rmail-total-messages)
2404 (setq i (if forward (1+ i) (1- i)))
2405 (goto-char (rmail-msgbeg i))
2406 (search-forward "\n*** EOOH ***\n")
2407 (let ((beg (point)) end)
2408 (search-forward "\n\n")
2411 (setq done (re-search-forward search-regexp end t))))
2412 (if done (setq found i)))
2413 (setq n (if forward (1- n) (1+ n))))))
2415 (rmail-show-message found)
2416 (error "No %s message with same subject"
2417 (if forward "following" "previous")))))
2419 (defun rmail-previous-same-subject (n)
2420 "Go to the previous mail message having the same subject header.
2421 With prefix argument N, do this N times.
2422 If N is negative, go forwards instead."
2424 (rmail-next-same-subject (- n)))
2426 ;;;; *** Rmail Message Deletion Commands ***
2428 (defun rmail-message-deleted-p (n)
2429 (= (aref rmail-deleted-vector n) ?D))
2431 (defun rmail-set-message-deleted-p (n state)
2432 (aset rmail-deleted-vector n (if state ?D ?\ )))
2434 (defun rmail-delete-message ()
2435 "Delete this message and stay on it."
2437 (rmail-set-attribute "deleted" t)
2438 (run-hooks 'rmail-delete-message-hook))
2440 (defun rmail-undelete-previous-message ()
2441 "Back up to deleted message, select it, and undelete it."
2443 (let ((msg rmail-current-message))
2444 (while (and (> msg 0)
2445 (not (rmail-message-deleted-p msg)))
2446 (setq msg (1- msg)))
2448 (error "No previous deleted message")
2449 (if (/= msg rmail-current-message)
2450 (rmail-show-message msg))
2451 (rmail-set-attribute "deleted" nil)
2452 (if (rmail-summary-exists)
2454 (set-buffer rmail-summary-buffer)
2455 (rmail-summary-mark-undeleted msg)))
2456 (rmail-maybe-display-summary))))
2458 (defun rmail-delete-forward (&optional backward)
2459 "Delete this message and move to next nondeleted one.
2460 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
2461 With prefix argument, delete and move backward.
2463 Returns t if a new message is displayed after the delete, or nil otherwise."
2465 (rmail-set-attribute "deleted" t)
2466 (run-hooks 'rmail-delete-message-hook)
2467 (let ((del-msg rmail-current-message))
2468 (if (rmail-summary-exists)
2469 (rmail-select-summary
2470 (rmail-summary-mark-deleted del-msg)))
2471 (prog1 (rmail-next-undeleted-message (if backward -1 1))
2472 (rmail-maybe-display-summary))))
2474 (defun rmail-delete-backward ()
2475 "Delete this message and move to previous nondeleted one.
2476 Deleted messages stay in the file until the \\[rmail-expunge] command is given."
2478 (rmail-delete-forward t))
2480 ;; Compute the message number a given message would have after expunging.
2481 ;; The present number of the message is OLDNUM.
2482 ;; DELETEDVEC should be rmail-deleted-vector.
2483 ;; The value is nil for a message that would be deleted.
2484 (defun rmail-msg-number-after-expunge (deletedvec oldnum)
2485 (if (or (null oldnum) (= (aref deletedvec oldnum) ?D))
2490 (if (/= (aref deletedvec i) ?D)
2491 (setq newnum (1+ newnum)))
2495 (defun rmail-only-expunge ()
2496 "Actually erase all deleted messages in the file."
2498 (message "Expunging deleted messages...")
2499 ;; Discard all undo records for this buffer.
2500 (or (eq buffer-undo-list t)
2501 (setq buffer-undo-list nil))
2502 (rmail-maybe-set-message-counters)
2503 (let* ((omax (- (buffer-size) (point-max)))
2504 (omin (- (buffer-size) (point-min)))
2505 (opoint (if (and (> rmail-current-message 0)
2506 (rmail-message-deleted-p rmail-current-message))
2508 (- (point) (point-min))))
2509 (messages-head (cons (aref rmail-message-vector 0) nil))
2510 (messages-tail messages-head)
2511 ;; Don't make any undo records for the expunging.
2512 (buffer-undo-list t)
2517 (goto-char (point-min))
2520 (total rmail-total-messages)
2521 (new-message-number rmail-current-message)
2523 (new-msgref (list (list 0)))
2524 (rmailbuf (current-buffer))
2525 (buffer-read-only nil)
2526 (messages rmail-message-vector)
2527 (deleted rmail-deleted-vector)
2528 (summary rmail-summary-vector))
2529 (setq rmail-total-messages nil
2530 rmail-current-message nil
2531 rmail-message-vector nil
2532 rmail-deleted-vector nil
2533 rmail-summary-vector nil)
2535 (while (<= number total)
2536 (if (= (aref deleted number) ?D)
2539 (marker-position (aref messages number))
2540 (marker-position (aref messages (1+ number))))
2541 (move-marker (aref messages number) nil)
2542 (if (> new-message-number counter)
2543 (setq new-message-number (1- new-message-number))))
2544 (setq counter (1+ counter))
2546 (setcdr messages-tail
2547 (cons (aref messages number) nil)))
2549 (cons (if (= counter number) (aref summary (1- number)))
2552 (cons (aref rmail-msgref-vector number)
2554 (setcar (car new-msgref) counter))
2555 (if (zerop (% (setq number (1+ number)) 20))
2556 (message "Expunging deleted messages...%d" number)))
2558 (setcdr messages-tail
2559 (cons (aref messages number) nil)))
2560 (setq rmail-current-message new-message-number
2561 rmail-total-messages counter
2562 rmail-message-vector (apply 'vector messages-head)
2563 rmail-deleted-vector (make-string (1+ counter) ?\ )
2564 rmail-summary-vector (vconcat (nreverse new-summary))
2565 rmail-msgref-vector (apply 'vector (nreverse new-msgref))
2567 (message "Expunging deleted messages...done")
2569 (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
2571 (if (zerop rmail-current-message) 1 nil))
2572 (goto-char (+ (point) opoint)))))
2574 (defun rmail-expunge ()
2575 "Erase deleted messages from Rmail file and summary buffer."
2577 (rmail-only-expunge)
2578 (if (rmail-summary-exists)
2579 (rmail-select-summary
2580 (rmail-update-summary))))
2582 ;;;; *** Rmail Mailing Commands ***
2584 (defun rmail-start-mail (&optional noerase to subject in-reply-to cc
2585 replybuffer sendactions same-window others)
2588 (setq yank-action (list 'insert-buffer replybuffer)))
2589 (setq others (cons (cons "cc" cc) others))
2590 (setq others (cons (cons "in-reply-to" in-reply-to) others))
2592 (compose-mail to subject others
2594 yank-action sendactions)
2595 (if (and window-system rmail-mail-new-frame)
2597 (compose-mail to subject others
2598 noerase 'switch-to-buffer-other-frame
2599 yank-action sendactions)
2600 ;; This is not a standard frame parameter;
2601 ;; nothing except sendmail.el looks at it.
2602 (modify-frame-parameters (selected-frame)
2603 '((mail-dedicated-frame . t))))
2604 (compose-mail to subject others
2605 noerase 'switch-to-buffer-other-window
2606 yank-action sendactions)))))
2608 (defun rmail-mail ()
2609 "Send mail in another window.
2610 While composing the message, use \\[mail-yank-original] to yank the
2611 original message into it."
2613 (rmail-start-mail nil nil nil nil nil rmail-view-buffer))
2615 (defun rmail-continue ()
2616 "Continue composing outgoing message previously being composed."
2618 (rmail-start-mail t))
2620 (defun rmail-reply (just-sender)
2621 "Reply to the current message.
2622 Normally include CC: to all other recipients of original message;
2623 prefix argument means ignore them. While composing the reply,
2624 use \\[mail-yank-original] to yank the original message into it."
2626 (let (from reply-to cc subject date to message-id references
2627 resent-to resent-cc resent-reply-to
2628 (msgnum rmail-current-message))
2632 (goto-char (rmail-msgbeg rmail-current-message))
2634 (if (= (following-char) ?0)
2636 (progn (forward-line 2)
2638 (progn (search-forward "\n\n" (rmail-msgend rmail-current-message)
2641 (narrow-to-region (point)
2642 (progn (search-forward "\n*** EOOH ***\n")
2643 (beginning-of-line) (point))))
2644 (setq from (mail-fetch-field "from")
2645 reply-to (or (mail-fetch-field "reply-to" nil t)
2647 cc (and (not just-sender)
2648 (mail-fetch-field "cc" nil t))
2649 subject (mail-fetch-field "subject")
2650 date (mail-fetch-field "date")
2651 to (or (mail-fetch-field "to" nil t) "")
2652 message-id (mail-fetch-field "message-id")
2653 references (mail-fetch-field "references" nil nil t)
2654 resent-reply-to (mail-fetch-field "resent-reply-to" nil t)
2655 resent-cc (and (not just-sender)
2656 (mail-fetch-field "resent-cc" nil t))
2657 resent-to (or (mail-fetch-field "resent-to" nil t) "")
2658 ;;; resent-subject (mail-fetch-field "resent-subject")
2659 ;;; resent-date (mail-fetch-field "resent-date")
2660 ;;; resent-message-id (mail-fetch-field "resent-message-id")
2662 ;; Merge the resent-to and resent-cc into the to and cc.
2663 (if (and resent-to (not (equal resent-to "")))
2664 (if (not (equal to ""))
2665 (setq to (concat to ", " resent-to))
2666 (setq to resent-to)))
2667 (if (and resent-cc (not (equal resent-cc "")))
2668 (if (not (equal cc ""))
2669 (setq cc (concat cc ", " resent-cc))
2670 (setq cc resent-cc)))
2671 ;; Add `Re: ' to subject if not there already.
2672 (and (stringp subject)
2674 (concat rmail-reply-prefix
2675 (if (let ((case-fold-search t))
2676 (string-match rmail-reply-regexp subject))
2677 (substring subject (match-end 0))
2681 ;; Using mail-strip-quoted-names is undesirable with newer mailers
2682 ;; since they can handle the names unstripped.
2683 ;; I don't know whether there are other mailers that still
2684 ;; need the names to be stripped.
2685 (mail-strip-quoted-names reply-to)
2687 (rmail-make-in-reply-to-field from date message-id)
2690 ;; mail-strip-quoted-names is NOT necessary for rmail-dont-reply-to
2692 (let* ((cc-list (rmail-dont-reply-to
2693 (mail-strip-quoted-names
2694 (if (null cc) to (concat to ", " cc))))))
2695 (if (string= cc-list "") nil cc-list)))
2697 (list (list 'rmail-mark-message
2699 (aref rmail-msgref-vector msgnum)
2702 (list (cons "References" (concat (mapconcat 'identity references " ")
2703 " " message-id))))))
2705 (defun rmail-mark-message (buffer msgnum-list attribute)
2706 "Give BUFFER's message number in MSGNUM-LIST the attribute ATTRIBUTE.
2707 This is use in the send-actions for message buffers.
2708 MSGNUM-LIST is a list of the form (MSGNUM)
2709 which is an element of rmail-msgref-vector."
2712 (if (car msgnum-list)
2713 (rmail-set-attribute attribute t (car msgnum-list)))))
2715 (defun rmail-make-in-reply-to-field (from date message-id)
2722 (let ((tem (car (rfc822-addresses from))))
2726 (regexp-quote (if (string-match "@[^@]*\\'" tem)
2728 (match-beginning 0))
2731 ;; missing From, or Message-ID is sufficiently informative
2733 (concat message-id " (" tem ")"))
2734 ;; Copy TEM, discarding text properties.
2735 (setq tem (copy-sequence tem))
2736 (set-text-properties 0 (length tem) nil tem)
2737 (setq tem (copy-sequence tem))
2738 ;; Use prin1 to fake RFC822 quoting
2739 (let ((field (prin1-to-string tem)))
2741 (concat field "'s message of " date)
2743 ((let* ((foo "[^][\000-\037()<>@,;:\\\" ]+")
2744 (bar "[^][\000-\037()<>@,;:\\\"]+"))
2745 ;; These strings both match all non-ASCII characters.
2746 (or (string-match (concat "\\`[ \t]*\\(" bar
2747 "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
2748 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
2750 (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
2751 bar "\\))[ \t]*\\'")
2752 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
2754 (let ((start (match-beginning 1))
2755 (end (match-end 1)))
2756 ;; Trim whitespace which above regexp match allows
2757 (while (and (< start end)
2758 (memq (aref from start) '(?\t ?\ )))
2759 (setq start (1+ start)))
2760 (while (and (< start end)
2761 (memq (aref from (1- end)) '(?\t ?\ )))
2762 (setq end (1- end)))
2763 (let ((field (substring from start end)))
2764 (if date (setq field (concat "message from " field " on " date)))
2766 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
2767 (concat message-id " (" field ")")
2770 ;; If we can't kludge it simply, do it correctly
2771 (let ((mail-use-rfc822 t))
2772 (rmail-make-in-reply-to-field from date message-id)))))
2774 (defun rmail-forward (resend)
2775 "Forward the current message to another user.
2776 With prefix argument, \"resend\" the message instead of forwarding it;
2777 see the documentation of `rmail-resend'."
2780 (call-interactively 'rmail-resend)
2781 (let ((forward-buffer (current-buffer))
2782 (msgnum rmail-current-message)
2783 (subject (concat "["
2784 (let ((from (or (mail-fetch-field "From")
2785 (mail-fetch-field ">From"))))
2787 (concat (mail-strip-quoted-names from) ": ")
2789 (or (mail-fetch-field "Subject") "")
2791 (if (rmail-start-mail
2792 nil nil subject nil nil nil
2793 (list (list 'rmail-mark-message
2795 (aref rmail-msgref-vector msgnum)
2797 ;; If only one window, use it for the mail buffer.
2798 ;; Otherwise, use another window for the mail buffer
2799 ;; so that the Rmail buffer remains visible
2800 ;; and sending the mail will get back to it.
2801 (and (not rmail-mail-new-frame) (one-window-p t)))
2802 ;; The mail buffer is now current.
2804 ;; Insert after header separator--before signature if any.
2805 (goto-char (mail-text-start))
2806 (insert "------- Start of forwarded message -------\n")
2807 ;; Quote lines with `- ' if they start with `-'.
2808 (let ((beg (point)) end)
2809 (setq end (point-marker))
2810 (set-marker-insertion-type end t)
2811 (insert-buffer-substring forward-buffer)
2813 (while (re-search-forward "^-" end t)
2818 (skip-chars-backward "\n")
2821 (delete-region (point) end)
2822 (set-marker end nil))
2823 (insert "------- End of forwarded message -------\n")
2826 (defun rmail-resend (address &optional from comment mail-alias-file)
2827 "Resend current message to ADDRESSES.
2828 ADDRESSES should be a single address, a string consisting of several
2829 addresses separated by commas, or a list of addresses.
2831 Optional FROM is the address to resend the message from, and
2832 defaults from the value of `user-mail-address'.
2833 Optional COMMENT is a string to insert as a comment in the resent message.
2834 Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
2835 typically for purposes of moderating a list."
2836 (interactive "sResend to: ")
2838 (require 'mailalias)
2839 (if (not from) (setq from user-mail-address))
2840 (let ((tembuf (generate-new-buffer " sendmail temp"))
2841 (case-fold-search nil)
2842 (mailbuf (current-buffer)))
2845 ;;>> Copy message into temp buffer
2847 (insert-buffer-substring mailbuf)
2848 (goto-char (point-min))
2849 ;; Delete any Sender field, since that's not specifiable.
2850 ; Only delete Sender fields in the actual header.
2851 (re-search-forward "^$" nil 'move)
2852 ; Using "while" here rather than "if" because some buggy mail
2853 ; software may have inserted multiple Sender fields.
2854 (while (re-search-backward "^Sender:" nil t)
2858 (while (looking-at "[ \t]")
2860 (delete-region beg (point))))
2861 ; Go back to the beginning of the buffer so the Resent- fields
2862 ; are inserted there.
2863 (goto-char (point-min))
2864 ;;>> Insert resent-from:
2865 (insert "Resent-From: " from "\n")
2866 (insert "Resent-Date: " (mail-rfc822-date) "\n")
2867 ;;>> Insert resent-to: and bcc if need be.
2868 (let ((before (point)))
2870 (insert "Resent-Bcc: " (user-login-name) "\n"))
2871 (insert "Resent-To: " (if (stringp address)
2873 (mapconcat 'identity address ",\n\t"))
2875 ;; Expand abbrevs in the recipients.
2877 (if (featurep 'mailabbrev)
2878 (let ((end (point-marker))
2879 (local-abbrev-table mail-abbrevs)
2880 (old-syntax-table (syntax-table)))
2881 (if (and (not (vectorp mail-abbrevs))
2882 (file-exists-p mail-personal-alias-file))
2883 (build-mail-abbrevs))
2884 (set-syntax-table mail-abbrev-syntax-table)
2886 (while (and (< (point) end)
2887 (progn (forward-word 1)
2890 (set-syntax-table old-syntax-table))
2891 (expand-mail-aliases before (point)))))
2892 ;;>> Set up comment, if any.
2893 (if (and (sequencep comment) (not (zerop (length comment))))
2894 (let ((before (point))
2897 (or (eolp) (insert "\n"))
2898 (setq after (point))
2900 (while (< (point) after)
2901 (insert "Resent-Comment: ")
2903 ;; Don't expand aliases in the destination fields
2904 ;; of the original message.
2906 (funcall send-mail-function)))
2907 (kill-buffer tembuf))
2908 (rmail-set-attribute "resent" t rmail-current-message)))
2910 (defvar mail-unsent-separator
2911 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
2912 "^ *---+ +Returned message +---+ *$\\|"
2913 "^ *---+ *Returned mail follows *---+ *$\\|"
2914 "^Start of returned message$\\|"
2915 "^ *---+ +Original message +---+ *$\\|"
2916 "^ *--+ +begin message +--+ *$\\|"
2917 "^ *---+ +Original message follows +---+ *$\\|"
2918 "^ *---+ +Your message follows +---+ *$\\|"
2919 "^|? *---+ +Message text follows: +---+ *|?$")
2920 "A regexp that matches the separator before the text of a failed message.")
2922 (defvar mail-mime-unsent-header "^Content-Type: message/rfc822 *$"
2923 "A regexp that matches the header of a MIME body part with a failed message.")
2925 (defun rmail-retry-failure ()
2926 "Edit a mail message which is based on the contents of the current message.
2927 For a message rejected by the mail system, extract the interesting headers and
2928 the body of the original message.
2929 If the failed message is a MIME multipart message, it is searched for a
2930 body part with a header which matches the variable `mail-mime-unsent-header'.
2931 Otherwise, the variable `mail-unsent-separator' should match the string that
2932 delimits the returned original message.
2933 The variable `rmail-retry-ignored-headers' is a regular expression
2934 specifying headers which should not be copied into the new message."
2936 (require 'mail-utils)
2937 (let ((rmail-this-buffer (current-buffer))
2938 (msgnum rmail-current-message)
2939 bounce-start bounce-end bounce-indent resending)
2941 ;; Narrow down to just the quoted original message
2942 (rmail-beginning-of-message)
2943 (let* ((case-fold-search t)
2947 ;; Fetch any content-type header in current message
2948 (search-forward "\n\n") (narrow-to-region top (point))
2949 (mail-fetch-field "Content-Type") )) )
2950 ;; Handle MIME multipart bounce messages
2951 (if (and content-type
2953 ";[\n\t ]*boundary=\"?\\([-0-9a-z'()+_,./:=?]+\\)\"?"
2957 (substring content-type (match-beginning 1)
2959 (or (re-search-forward mail-mime-unsent-header nil t)
2960 (error "Cannot find beginning of header in failed message"))
2961 (or (search-forward "\n\n" nil t)
2962 (error "Cannot find start of Mime data in failed message"))
2963 (setq bounce-start (point))
2964 (if (search-forward codestring nil t)
2965 (setq bounce-end (match-beginning 0))
2966 (setq bounce-end (point-max)))
2969 (or (re-search-forward mail-unsent-separator nil t)
2970 (error "Cannot parse this as a failure message"))
2971 (skip-chars-forward "\n")
2972 ;; Support a style of failure message in which the original
2973 ;; message is indented, and included within lines saying
2974 ;; `Start of returned message' and `End of returned message'.
2975 (if (looking-at " +Received:")
2977 (setq bounce-start (point))
2978 (skip-chars-forward " ")
2979 (setq bounce-indent (- (current-column)))
2980 (goto-char (point-max))
2981 (re-search-backward "^End of returned message$" nil t)
2982 (setq bounce-end (point)))
2983 ;; One message contained a few random lines before the old
2984 ;; message header. The first line of the message started with
2985 ;; two hyphens. A blank line followed these random lines.
2986 ;; The same line beginning with two hyphens was possibly
2987 ;; marking the end of the message.
2988 (if (looking-at "^--")
2989 (let ((boundary (buffer-substring-no-properties
2991 (progn (end-of-line) (point)))))
2992 (search-forward "\n\n")
2993 (skip-chars-forward "\n")
2994 (setq bounce-start (point))
2995 (goto-char (point-max))
2996 (search-backward (concat "\n\n" boundary) bounce-start t)
2997 (setq bounce-end (point)))
2998 (setq bounce-start (point)
2999 bounce-end (point-max)))
3000 (or (search-forward "\n\n" nil t)
3001 (error "Cannot find end of header in failed message"))
3003 ;; Start sending a new message; default header fields from the original.
3004 ;; Turn off the usual actions for initializing the message body
3005 ;; because we want to get only the text from the failure message.
3006 (let (mail-signature mail-setup-hook)
3007 (if (rmail-start-mail nil nil nil nil nil rmail-this-buffer
3008 (list (list 'rmail-mark-message
3010 (aref rmail-msgref-vector msgnum)
3012 ;; Insert original text as initial text of new draft message.
3013 ;; Bind inhibit-read-only since the header delimiter
3014 ;; of the previous message was probably read-only.
3015 (let ((inhibit-read-only t))
3017 (insert-buffer-substring rmail-this-buffer bounce-start bounce-end)
3018 (goto-char (point-min))
3020 (indent-rigidly (point-min) (point-max) bounce-indent))
3021 (rmail-clear-headers rmail-retry-ignored-headers)
3022 (rmail-clear-headers "^sender:\\|^from:\\|^return-path:")
3023 (mail-sendmail-delimit-header)
3025 (narrow-to-region (point-min) (mail-header-end))
3026 (setq resending (mail-fetch-field "resent-to"))
3029 (insert "Resent-Bcc: " (user-login-name) "\n")
3030 (insert "BCC: " (user-login-name) "\n"))))
3031 (goto-char (point-min))
3032 (mail-position-on-field (if resending "Resent-To" "To") t)
3033 (set-buffer rmail-this-buffer)
3034 (rmail-beginning-of-message))))))
3036 (defun rmail-summary-exists ()
3037 "Non-nil iff in an RMAIL buffer and an associated summary buffer exists.
3038 In fact, the non-nil value returned is the summary buffer itself."
3039 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
3040 rmail-summary-buffer))
3042 (defun rmail-summary-displayed ()
3043 "t iff in RMAIL buffer and an associated summary buffer is displayed."
3044 (and rmail-summary-buffer (get-buffer-window rmail-summary-buffer)))
3046 (defcustom rmail-redisplay-summary nil
3047 "*Non-nil means Rmail should show the summary when it changes.
3048 This has an effect only if a summary buffer exists."
3050 :group 'rmail-summary)
3052 (defcustom rmail-summary-window-size nil
3053 "*Non-nil means specify the height for an Rmail summary window."
3054 :type '(choice (const :tag "Disabled" nil) integer)
3055 :group 'rmail-summary)
3057 ;; Put the summary buffer back on the screen, if user wants that.
3058 (defun rmail-maybe-display-summary ()
3059 (let ((selected (selected-window))
3061 ;; If requested, make sure the summary is displayed.
3062 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
3063 rmail-redisplay-summary
3064 (if (get-buffer-window rmail-summary-buffer 0)
3065 ;; It's already in some frame; show that one.
3066 (let ((frame (window-frame
3067 (get-buffer-window rmail-summary-buffer 0))))
3068 (make-frame-visible frame)
3069 (raise-frame frame))
3070 (display-buffer rmail-summary-buffer)))
3071 ;; If requested, set the height of the summary window.
3072 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
3073 rmail-summary-window-size
3074 (setq window (get-buffer-window rmail-summary-buffer))
3075 ;; Don't try to change the size if just one window in frame.
3076 (not (eq window (frame-root-window (window-frame window))))
3079 (select-window window)
3080 (enlarge-window (- rmail-summary-window-size (window-height))))
3081 (select-window selected)))))
3083 ;;;; *** Rmail Local Fontification ***
3085 (defun rmail-fontify-buffer-function ()
3086 ;; This function's symbol is bound to font-lock-fontify-buffer-function.
3087 (make-local-hook 'rmail-show-message-hook)
3088 (add-hook 'rmail-show-message-hook 'rmail-fontify-message nil t)
3089 ;; If we're already showing a message, fontify it now.
3090 (if rmail-current-message (rmail-fontify-message))
3091 ;; Prevent Font Lock mode from kicking in.
3092 (setq font-lock-fontified t))
3094 (defun rmail-unfontify-buffer-function ()
3095 ;; This function's symbol is bound to font-lock-fontify-unbuffer-function.
3096 (let ((modified (buffer-modified-p))
3097 (buffer-undo-list t) (inhibit-read-only t)
3098 before-change-functions after-change-functions
3099 buffer-file-name buffer-file-truename)
3102 (remove-hook 'rmail-show-message-hook 'rmail-fontify-message t)
3103 (remove-text-properties (point-min) (point-max) '(rmail-fontified nil))
3104 (font-lock-default-unfontify-buffer)
3105 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))
3107 (defun rmail-fontify-message ()
3108 ;; Fontify the current message if it is not already fontified.
3109 (if (text-property-any (point-min) (point-max) 'rmail-fontified nil)
3110 (let ((modified (buffer-modified-p))
3111 (buffer-undo-list t) (inhibit-read-only t)
3112 before-change-functions after-change-functions
3113 buffer-file-name buffer-file-truename)
3116 (add-text-properties (point-min) (point-max) '(rmail-fontified t))
3117 (font-lock-fontify-region (point-min) (point-max))
3118 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))))
3120 ;;; Speedbar support for RMAIL files.
3121 (eval-when-compile (require 'speedbspec))
3123 (defvar rmail-speedbar-last-user nil
3124 "The last user to be displayed in the speedbar.")
3126 (defvar rmail-speedbar-menu-items
3127 '(["Browse Item On Line" speedbar-edit-line t]
3128 ["Move message to folder" rmail-move-message-to-folder-on-line
3129 (save-excursion (beginning-of-line)
3130 (looking-at "<M> "))])
3131 "Additional menu-items to add to speedbar frame.")
3133 (defun rmail-speedbar-buttons (buffer)
3134 "Create buttons for BUFFER containing rmail messages.
3135 Click on the address under Reply to: to reply to this person.
3136 Under Folders: Click a name to read it, or on the <M> to move the
3137 current message into that RMAIL folder."
3141 (goto-char (point-min))
3142 (if (not (re-search-forward "^Reply-To: " nil t))
3143 (if (not (re-search-forward "^From:? " nil t))
3147 (setq from (buffer-substring (point) (save-excursion
3150 (goto-char (point-min))
3151 (if (and (looking-at "Reply to:")
3152 (equal from rmail-speedbar-last-user))
3154 (setq rmail-speedbar-last-user from)
3156 (insert "Reply To:\n")
3158 (speedbar-insert-button from 'speedbar-directory-face 'highlight
3159 'rmail-speedbar-button 'rmail-reply))
3160 (insert "Folders:\n")
3161 (let* ((case-fold-search nil)
3162 (df (directory-files (save-excursion (set-buffer buffer)
3164 nil "^[A-Z0-9]+\\(\\.[A-Z0-9]+\\)?$")))
3166 (speedbar-insert-button "<M>" 'speedbar-button-face 'highlight
3167 'rmail-speedbar-move-message (car df))
3168 (speedbar-insert-button (car df) 'speedbar-file-face 'highlight
3169 'rmail-speedbar-find-file nil t)
3170 (setq df (cdr df)))))))
3172 (defun rmail-speedbar-button (text token indent)
3173 "Execute an rmail command specified by TEXT.
3174 The command used is TOKEN. INDENT is not used."
3175 (speedbar-with-attached-buffer
3178 (defun rmail-speedbar-find-file (text token indent)
3179 "Load in the rmail file TEXT.
3180 TOKEN and INDENT are not used."
3181 (speedbar-with-attached-buffer
3182 (message "Loading in RMAIL file %s..." text)
3185 (defun rmail-move-message-to-folder-on-line ()
3186 "If the current line is a folder, move current message to it."
3190 (if (re-search-forward "<M> " (save-excursion (end-of-line) (point)) t)
3193 (speedbar-do-function-pointer)))))
3195 (defun rmail-speedbar-move-message (text token indent)
3196 "From button TEXT, copy current message to the rmail file specified by TOKEN.
3197 TEXT and INDENT are not used."
3198 (speedbar-with-attached-buffer
3199 (message "Moving message to %s" token)
3200 (rmail-output-to-rmail-file token)))
3205 ;;; rmail.el ends here