1 ;;; rmail.el --- main code of "RMAIL" mail reader for Emacs
3 ;; Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1996, 1997, 1998,
4 ;; 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 ;; Free Software Foundation, Inc.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
31 ;; Souped up by shane@mit-ajax based on ideas of rlk@athena.mit.edu
32 ;; New features include attribute and keyword support, message
33 ;; selection by dispatch table, summary by attributes and keywords,
34 ;; expunging by dispatch table, sticky options for file commands.
36 ;; Extended by Bob Weiner of Motorola
37 ;; New features include: rmail and rmail-summary buffers remain
38 ;; synchronized and key bindings basically operate the same way in both
39 ;; buffers, summary by topic or by regular expression, rmail-reply-prefix
40 ;; variable, and a bury rmail buffer (wipe) command.
44 (eval-when-compile (require 'mule-util)) ; for detect-coding-with-priority
47 (defvar font-lock-fontified)
48 (defvar mail-abbrev-syntax-table)
50 (defvar messages-head)
51 (defvar rmail-use-spam-filter)
53 (defvar rsf-sleep-after-message)
54 (defvar total-messages)
57 ; These variables now declared in paths.el.
58 ;(defvar rmail-spool-directory "/usr/spool/mail/"
59 ; "This is the name of the directory used by the system mailer for\n\
60 ;delivering new mail. Its name should end with a slash.")
61 ;(defvar rmail-file-name
62 ; (expand-file-name "~/RMAIL")
66 "Mail reader for Emacs."
69 (defgroup rmail-retrieve nil
70 "Rmail retrieval options."
74 (defgroup rmail-files nil
79 (defgroup rmail-headers nil
80 "Rmail header options."
84 (defgroup rmail-reply nil
85 "Rmail reply options."
89 (defgroup rmail-summary nil
90 "Rmail summary options."
92 :prefix "rmail-summary-"
95 (defgroup rmail-output nil
96 "Output message to a file."
97 :prefix "rmail-output-"
101 (defgroup rmail-edit nil
103 :prefix "rmail-edit-"
106 (defgroup rmail-obsolete nil
107 "Rmail obsolete customization variables."
110 (defcustom rmail-movemail-program nil
111 "If non-nil, the file name of the `movemail' program."
112 :group 'rmail-retrieve
113 :type '(choice (const nil) string))
115 (defcustom rmail-pop-password nil
116 "*Password to use when reading mail from POP server.
117 Please use `rmail-remote-password' instead."
118 :type '(choice (string :tag "Password")
119 (const :tag "Not Required" nil))
120 :group 'rmail-obsolete)
122 (defcustom rmail-pop-password-required nil
123 "*Non-nil if a password is required when reading mail from a POP server.
124 Please use rmail-remote-password-required instead."
126 :group 'rmail-obsolete)
128 (defcustom rmail-remote-password nil
129 "*Password to use when reading mail from a remote server.
130 This setting is ignored for mailboxes whose URL already contains a password."
131 :type '(choice (string :tag "Password")
132 (const :tag "Not Required" nil))
133 :set-after '(rmail-pop-password)
134 :set #'(lambda (symbol value)
137 (boundp 'rmail-pop-password)
141 (setq rmail-pop-password nil))
142 :group 'rmail-retrieve
145 (defcustom rmail-remote-password-required nil
146 "*Non-nil if a password is required when reading mail from a remote server."
148 :set-after '(rmail-pop-password-required)
149 :set #'(lambda (symbol value)
152 (boundp 'rmail-pop-password-required)
153 rmail-pop-password-required)
154 rmail-pop-password-required
156 (setq rmail-pop-password-required nil))
157 :group 'rmail-retrieve
160 (defcustom rmail-movemail-flags nil
161 "*List of flags to pass to movemail.
162 Most commonly used to specify `-g' to enable GSS-API authentication
163 or `-k' to enable Kerberos authentication."
164 :type '(repeat string)
165 :group 'rmail-retrieve
168 (defvar rmail-remote-password-error "invalid usercode or password\\|
169 unknown user name or bad password\\|Authentication failed\\|MU_ERR_AUTH_FAILURE"
170 "Regular expression matching incorrect-password POP or IMAP server error
172 If you get an incorrect-password error that this expression does not match,
173 please report it with \\[report-emacs-bug].")
175 (defvar rmail-encoded-remote-password nil)
177 (defcustom rmail-preserve-inbox nil
178 "*Non-nil means leave incoming mail in the user's inbox--don't delete it."
180 :group 'rmail-retrieve)
182 (defcustom rmail-movemail-search-path nil
183 "*List of directories to search for movemail (in addition to `exec-path')."
184 :group 'rmail-retrieve
185 :type '(repeat (directory)))
187 (declare-function mail-position-on-field "sendmail" (field &optional soft))
188 (declare-function mail-text-start "sendmail" ())
189 (declare-function rmail-update-summary "rmailsum" (&rest ignore))
191 (defun rmail-probe (prog)
192 "Determine what flavor of movemail PROG is.
193 We do this by executing it with `--version' and analyzing its output."
195 (let ((tbuf (current-buffer)))
196 (buffer-disable-undo tbuf)
197 (call-process prog nil tbuf nil "--version")
198 (if (not (buffer-modified-p tbuf))
199 ;; Should not happen...
201 (goto-char (point-min))
203 ((looking-at ".*movemail: invalid option")
204 'emacs) ;; Possibly...
205 ((looking-at "movemail (GNU Mailutils .*)")
211 (defun rmail-autodetect ()
212 "Determine the file name of the `movemail' program and return its flavor.
213 If `rmail-movemail-program' is non-nil, use it.
214 Otherwise, look for `movemail' in the directories in
215 `rmail-movemail-search-path', those in `exec-path', and `exec-directory'."
216 (if rmail-movemail-program
217 (rmail-probe rmail-movemail-program)
219 (dolist (dir (append rmail-movemail-search-path exec-path
220 (list exec-directory)))
221 (when (and dir (file-accessible-directory-p dir))
222 (let ((progname (expand-file-name "movemail" dir)))
223 (when (and (not (file-directory-p progname))
224 (file-executable-p progname))
225 (let ((x (rmail-probe progname)))
227 (setq rmail-movemail-program progname)
228 (throw 'scan x))))))))))
230 (defvar rmail-movemail-variant-in-use nil
231 "The movemail variant currently in use. Known variants are:
233 `emacs' Means any implementation, compatible with the native Emacs one.
235 `mailutils' Means GNU mailutils implementation, capable of handling full
236 mail URLs as the source mailbox.")
239 (defun rmail-movemail-variant-p (&rest variants)
240 "Return t if the current movemail variant is any of VARIANTS.
241 Currently known variants are 'emacs and 'mailutils."
242 (when (not rmail-movemail-variant-in-use)
244 (setq rmail-movemail-variant-in-use (rmail-autodetect)))
245 (not (null (member rmail-movemail-variant-in-use variants))))
247 ;; Call for effect, to set rmail-movemail-program (if not set by the
248 ;; user), and rmail-movemail-variant-in-use. Used by various functions.
249 ;; I'm not sure if M-x rmail is the only entry point to this package.
250 ;; If so, this can be moved there.
251 (rmail-movemail-variant-p)
254 (defcustom rmail-dont-reply-to-names nil "\
255 *A regexp specifying addresses to prune from a reply message.
256 A value of nil means exclude your own email address as an address
257 plus whatever is specified by `rmail-default-dont-reply-to-names'."
258 :type '(choice regexp (const :tag "Your Name" nil))
262 (defvar rmail-default-dont-reply-to-names "\\`info-" "\
263 A regular expression specifying part of the default value of the
264 variable `rmail-dont-reply-to-names', for when the user does not set
265 `rmail-dont-reply-to-names' explicitly. (The other part of the default
266 value is the user's email address and name.)
267 It is useful to set this variable in the site customization file.")
270 (defcustom rmail-ignored-headers
271 (concat "^via:\\|^mail-from:\\|^origin:\\|^references:\\|^sender:"
272 "\\|^status:\\|^received:\\|^x400-originator:\\|^x400-recipients:"
273 "\\|^x400-received:\\|^x400-mts-identifier:\\|^x400-content-type:"
274 "\\|^\\(resent-\\|\\)message-id:\\|^summary-line:\\|^resent-date:"
275 "\\|^nntp-posting-host:\\|^path:\\|^x-char.*:\\|^x-face:\\|^face:"
276 "\\|^x-mailer:\\|^delivered-to:\\|^lines:"
277 "\\|^content-transfer-encoding:\\|^x-coding-system:"
278 "\\|^return-path:\\|^errors-to:\\|^return-receipt-to:"
279 "\\|^precedence:\\|^list-help:\\|^list-post:\\|^list-subscribe:"
280 "\\|^list-id:\\|^list-unsubscribe:\\|^list-archive:"
281 "\\|^content-length:\\|^nntp-posting-date:\\|^user-agent"
282 "\\|^importance:\\|^envelope-to:\\|^delivery-date\\|^openpgp:"
283 "\\|^mbox-line:\\|^cancel-lock:\\|^DomainKey-Signature:"
284 "\\|^resent-face:\\|^resent-x.*:\\|^resent-organization:\\|^resent-openpgp:"
287 "*Regexp to match header fields that Rmail should normally hide.
288 \(See also `rmail-nonignored-headers', which overrides this regexp.)
289 This variable is used for reformatting the message header,
290 which normally happens once for each message,
291 when you view the message for the first time in Rmail.
292 To make a change in this variable take effect
293 for a message that you have already viewed,
294 go to that message and type \\[rmail-toggle-header] twice."
296 :group 'rmail-headers)
298 (defcustom rmail-nonignored-headers "^x-spam-status:"
299 "*Regexp to match X header fields that Rmail should show.
300 This regexp overrides `rmail-ignored-headers'; if both this regexp
301 and that one match a certain header field, Rmail shows the field.
303 This variable is used for reformatting the message header,
304 which normally happens once for each message,
305 when you view the message for the first time in Rmail.
306 To make a change in this variable take effect
307 for a message that you have already viewed,
308 go to that message and type \\[rmail-toggle-header] twice."
310 :group 'rmail-headers)
313 (defcustom rmail-displayed-headers nil
314 "*Regexp to match Header fields that Rmail should display.
315 If nil, display all header fields except those matched by
316 `rmail-ignored-headers'."
317 :type '(choice regexp (const :tag "All"))
318 :group 'rmail-headers)
321 (defcustom rmail-retry-ignored-headers "^x-authentication-warning:" "\
322 *Headers that should be stripped when retrying a failed message."
323 :type '(choice regexp (const nil :tag "None"))
324 :group 'rmail-headers)
327 (defcustom rmail-highlighted-headers "^From:\\|^Subject:" "\
328 *Regexp to match Header fields that Rmail should normally highlight.
329 A value of nil means don't highlight.
330 See also `rmail-highlight-face'."
332 :group 'rmail-headers)
334 (defface rmail-highlight
335 '((t :default highlight))
336 "Face to use for highlighting the most important header fields."
337 :group 'rmail-headers
341 (defcustom rmail-highlight-face 'rmail-highlight "\
342 *Face used by Rmail for highlighting headers."
343 :type '(choice (const :tag "Default" nil)
345 :group 'rmail-headers)
348 (defcustom rmail-delete-after-output nil "\
349 *Non-nil means automatically delete a message that is copied to a file."
354 (defcustom rmail-primary-inbox-list nil "\
355 *List of files which are inboxes for user's primary mail file `~/RMAIL'.
356 nil means the default, which is (\"/usr/spool/mail/$USER\")
357 \(the name varies depending on the operating system,
358 and the value of the environment variable MAIL overrides it)."
359 ;; Don't use backquote here, because we don't want to need it
361 :type (list 'choice '(const :tag "Default" nil)
362 (list 'repeat ':value (list (or (getenv "MAIL")
363 (concat "/var/spool/mail/"
366 :group 'rmail-retrieve
370 (defcustom rmail-mail-new-frame nil
371 "*Non-nil means Rmail makes a new frame for composing outgoing mail.
372 This is handy if you want to preserve the window configuration of
373 the frame where you have the RMAIL buffer displayed."
378 (defcustom rmail-secondary-file-directory "~/"
379 "*Directory for additional secondary Rmail files."
383 (defcustom rmail-secondary-file-regexp "\\.xmail$"
384 "*Regexp for which files are secondary Rmail files."
389 (defcustom rmail-confirm-expunge 'y-or-n-p
390 "*Whether and how to ask for confirmation before expunging deleted messages."
391 :type '(choice (const :tag "No confirmation" nil)
392 (const :tag "Confirm with y-or-n-p" y-or-n-p)
393 (const :tag "Confirm with yes-or-no-p" yes-or-no-p))
398 (defvar rmail-mode-hook nil
399 "List of functions to call when Rmail is invoked.")
402 (defvar rmail-get-new-mail-hook nil
403 "List of functions to call when Rmail has retrieved new mail.")
406 (defcustom rmail-show-message-hook nil
407 "List of functions to call when Rmail displays a message."
409 :options '(goto-address)
413 (defvar rmail-quit-hook nil
414 "List of functions to call when quitting out of Rmail.")
417 (defvar rmail-delete-message-hook nil
418 "List of functions to call when Rmail deletes a message.
419 When the hooks are called, the message has been marked deleted but is
420 still the current message in the Rmail buffer.")
422 ;; These may be altered by site-init.el to match the format of mmdf files
423 ;; delimiting used on a given host (delim1 and delim2 from the config
426 (defvar rmail-mmdf-delim1 "^\001\001\001\001\n"
427 "Regexp marking the start of an mmdf message.")
428 (defvar rmail-mmdf-delim2 "^\001\001\001\001\n"
429 "Regexp marking the end of an mmdf message.")
431 (defcustom rmail-message-filter nil
432 "If non-nil, a filter function for new messages in RMAIL.
433 Called with region narrowed to the message, including headers,
434 before obeying `rmail-ignored-headers'."
435 :group 'rmail-headers
436 :type '(choice (const nil) function))
438 (defcustom rmail-automatic-folder-directives nil
439 "List of directives specifying where to put a message.
440 Each element of the list is of the form:
442 (FOLDERNAME FIELD REGEXP [ FIELD REGEXP ] ... )
444 Where FOLDERNAME is the name of a BABYL format folder to put the
445 message. If any of the field regexp's are nil, then it is ignored.
447 If FOLDERNAME is \"/dev/null\", it is deleted.
448 If FOLDERNAME is nil then it is deleted, and skipped.
450 FIELD is the plain text name of a field in the message, such as
451 \"subject\" or \"from\". A FIELD of \"to\" will automatically include
452 all text from the \"cc\" field as well.
454 REGEXP is an expression to match in the preceeding specified FIELD.
455 FIELD/REGEXP pairs continue in the list.
458 (\"/dev/null\" \"from\" \"@spam.com\") ; delete all mail from spam.com
459 (\"RMS\" \"from\" \"rms@\") ; save all mail from RMS."
462 :type '(repeat (sexp :tag "Directive")))
464 (defvar rmail-reply-prefix "Re: "
465 "String to prepend to Subject line when replying to a message.")
467 ;; Some mailers use "Re(2):" or "Re^2:" or "Re: Re:" or "Re[2]:".
468 ;; This pattern should catch all the common variants.
469 ;; rms: I deleted the change to delete tags in square brackets
470 ;; because they mess up RT tags.
471 (defvar rmail-reply-regexp "\\`\\(Re\\(([0-9]+)\\|\\[[0-9]+\\]\\|\\^[0-9]+\\)?: *\\)*"
472 "Regexp to delete from Subject line before inserting `rmail-reply-prefix'.")
474 (defcustom rmail-display-summary nil
475 "*If non-nil, Rmail always displays the summary buffer."
476 :group 'rmail-summary
479 (defvar rmail-inbox-list nil)
480 (put 'rmail-inbox-list 'permanent-local t)
482 (defvar rmail-keywords nil)
483 (put 'rmail-keywords 'permanent-local t)
485 (defvar rmail-buffer nil
486 "The RMAIL buffer related to the current buffer.
487 In an RMAIL buffer, this holds the RMAIL buffer itself.
488 In a summary buffer, this holds the RMAIL buffer it is a summary for.")
489 (put 'rmail-buffer 'permanent-local t)
491 ;; Message counters and markers. Deleted flags.
493 (defvar rmail-current-message nil)
494 (put 'rmail-current-message 'permanent-local t)
496 (defvar rmail-total-messages nil)
497 (put 'rmail-total-messages 'permanent-local t)
499 (defvar rmail-message-vector nil)
500 (put 'rmail-message-vector 'permanent-local t)
502 (defvar rmail-deleted-vector nil)
503 (put 'rmail-deleted-vector 'permanent-local t)
505 (defvar rmail-msgref-vector nil
506 "In an Rmail buffer, a vector whose Nth element is a list (N).
507 When expunging renumbers messages, these lists are modified
508 by substituting the new message number into the existing list.")
509 (put 'rmail-msgref-vector 'permanent-local t)
511 (defvar rmail-overlay-list nil)
512 (put 'rmail-overlay-list 'permanent-local t)
514 ;; These are used by autoloaded rmail-summary.
516 (defvar rmail-summary-buffer nil)
517 (put 'rmail-summary-buffer 'permanent-local t)
518 (defvar rmail-summary-vector nil)
519 (put 'rmail-summary-vector 'permanent-local t)
521 (defvar rmail-view-buffer nil
522 "Buffer which holds RMAIL message for MIME displaying.")
523 (put 'rmail-view-buffer 'permanent-local t)
525 ;; `Sticky' default variables.
527 ;; Last individual label specified to a or k.
528 (defvar rmail-last-label nil)
529 (put 'rmail-last-label 'permanent-local t)
531 ;; Last set of values specified to C-M-n, C-M-p, C-M-s or C-M-l.
532 (defvar rmail-last-multi-labels nil)
534 (defvar rmail-last-regexp nil)
535 (put 'rmail-last-regexp 'permanent-local t)
537 (defcustom rmail-default-file "~/xmail"
538 "*Default file name for \\[rmail-output]."
541 (defcustom rmail-default-rmail-file "~/XMAIL"
542 "*Default file name for \\[rmail-output-to-rmail-file]."
545 (defcustom rmail-default-body-file "~/mailout"
546 "*Default file name for \\[rmail-output-body-to-file]."
551 ;; Mule and MIME related variables.
554 (defvar rmail-file-coding-system nil
555 "Coding system used in RMAIL file.
557 This is set to nil by default.")
560 (defcustom rmail-enable-mime nil
561 "*If non-nil, RMAIL uses MIME feature.
562 If the value is t, RMAIL automatically shows MIME decoded message.
563 If the value is neither t nor nil, RMAIL does not show MIME decoded message
564 until a user explicitly requires it.
566 Even if the value is non-nil, you can't use MIME feature
567 if the feature specified by `rmail-mime-feature' is not available
569 :type '(choice (const :tag "on" t)
570 (const :tag "off" nil)
571 (other :tag "when asked" ask))
574 (defvar rmail-enable-mime-composing nil
575 "*If non-nil, RMAIL uses `rmail-insert-mime-forwarded-message-function' to forward.")
578 (defvar rmail-show-mime-function nil
579 "Function to show MIME decoded message of RMAIL file.
580 This function is called when `rmail-enable-mime' is non-nil.
581 It is called with no argument.")
584 (defvar rmail-insert-mime-forwarded-message-function nil
585 "Function to insert a message in MIME format so it can be forwarded.
586 This function is called if `rmail-enable-mime' or
587 `rmail-enable-mime-composing' is non-nil.
588 It is called with one argument FORWARD-BUFFER, which is a
589 buffer containing the message to forward. The current buffer
590 is the outgoing mail buffer.")
593 (defvar rmail-insert-mime-resent-message-function nil
594 "Function to insert a message in MIME format so it can be resent.
595 This function is called if `rmail-enable-mime' is non-nil.
596 It is called with one argument FORWARD-BUFFER, which is a
597 buffer containing the message to forward. The current buffer
598 is the outgoing mail buffer.")
601 (defvar rmail-search-mime-message-function nil
602 "Function to check if a regexp matches a MIME message.
603 This function is called if `rmail-enable-mime' is non-nil.
604 It is called with two arguments MSG and REGEXP, where
605 MSG is the message number, REGEXP is the regular expression.")
608 (defvar rmail-search-mime-header-function nil
609 "Function to check if a regexp matches a header of MIME message.
610 This function is called if `rmail-enable-mime' is non-nil.
611 It is called with three arguments MSG, REGEXP, and LIMIT, where
612 MSG is the message number,
613 REGEXP is the regular expression,
614 LIMIT is the position specifying the end of header.")
617 (defvar rmail-mime-feature 'rmail-mime
618 "Feature to require to load MIME support in Rmail.
619 When starting Rmail, if `rmail-enable-mime' is non-nil,
620 this feature is required with `require'.
622 The default value is `rmail-mime'. This feature is provided by
623 the rmail-mime package available at <http://www.m17n.org/rmail-mime/>.")
626 (defvar rmail-decode-mime-charset t
627 "*Non-nil means a message is decoded by MIME's charset specification.
628 If this variable is nil, or the message has not MIME specification,
629 the message is decoded as normal way.
631 If the variable `rmail-enable-mime' is non-nil, this variables is
632 ignored, and all the decoding work is done by a feature specified by
633 the variable `rmail-mime-feature'.")
636 (defvar rmail-mime-charset-pattern
637 (concat "^content-type:[ \t]*text/plain;"
638 "\\(?:[ \t\n]*\\(?:format\\|delsp\\)=\"?[-a-z0-9]+\"?;\\)*"
639 "[ \t\n]*charset=\"?\\([^ \t\n\";]+\\)\"?")
640 "Regexp to match MIME-charset specification in a header of message.
641 The first parenthesized expression should match the MIME-charset name.")
644 ;;; Regexp matching the delimiter of messages in UNIX mail format
645 ;;; (UNIX From lines), minus the initial ^. Note that if you change
646 ;;; this expression, you must change the code in rmail-nuke-pinhead-header
647 ;;; that knows the exact ordering of the \\( \\) subexpressions.
648 (defvar rmail-unix-mail-delimiter
649 (let ((time-zone-regexp
650 (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
651 "\\|[-+]?[0-9][0-9][0-9][0-9]"
657 ;; Many things can happen to an RFC 822 mailbox before it is put into
658 ;; a `From' line. The leading phrase can be stripped, e.g.
659 ;; `Joe <@w.x:joe@y.z>' -> `<@w.x:joe@y.z>'. The <> can be stripped, e.g.
660 ;; `<@x.y:joe@y.z>' -> `@x.y:joe@y.z'. Everything starting with a CRLF
661 ;; can be removed, e.g.
662 ;; From: joe@y.z (Joe K
664 ;; can yield `From joe@y.z (Joe K Fri Mar 22 08:11:15 1996', and
667 ;; can yield `From Joe User Fri Mar 22 08:11:15 1996'.
668 ;; The mailbox can be removed or be replaced by white space, e.g.
669 ;; From: "Joe User"{space}{tab}
671 ;; can yield `From {space}{tab} Fri Mar 22 08:11:15 1996',
672 ;; where {space} and {tab} represent the Ascii space and tab characters.
673 ;; We want to match the results of any of these manglings.
674 ;; The following regexp rejects names whose first characters are
675 ;; obviously bogus, but after that anything goes.
676 "\\([^\0-\b\n-\r\^?].*\\)? "
678 ;; The time the message was sent.
679 "\\([^\0-\r \^?]+\\) +" ; day of the week
680 "\\([^\0-\r \^?]+\\) +" ; month
681 "\\([0-3]?[0-9]\\) +" ; day of month
682 "\\([0-2][0-9]:[0-5][0-9]\\(:[0-6][0-9]\\)?\\) *" ; time of day
684 ;; Perhaps a time zone, specified by an abbreviation, or by a
689 " \\([0-9][0-9]+\\) *"
691 ;; On some systems the time zone can appear after the year, too.
695 "\\(remote from .*\\)?"
700 (defvar rmail-font-lock-keywords
701 ;; These are all matched case-insensitively.
703 (let* ((cite-chars "[>|}]")
705 (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
706 (list '("^\\(From\\|Sender\\|Resent-From\\):"
707 . font-lock-function-name-face)
708 '("^Reply-To:.*$" . font-lock-function-name-face)
709 '("^Subject:" . font-lock-comment-face)
710 '("^X-Spam-Status:" . font-lock-keyword-face)
711 '("^\\(To\\|Apparently-To\\|Cc\\|Newsgroups\\):"
712 . font-lock-keyword-face)
713 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
715 (,(concat "\\=[ \t]*"
716 "\\(\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
717 "\\(" cite-chars "[ \t]*\\)\\)+\\)"
719 (beginning-of-line) (end-of-line)
720 (1 font-lock-comment-delimiter-face nil t)
721 (5 font-lock-comment-face nil t)))
722 '("^\\(X-[a-z0-9-]+\\|In-reply-to\\|Date\\):.*\\(\n[ \t]+.*\\)*$"
723 . font-lock-string-face))))
724 "Additional expressions to highlight in Rmail mode.")
726 ;; Perform BODY in the summary buffer
727 ;; in such a way that its cursor is properly updated in its own window.
728 (defmacro rmail-select-summary (&rest body)
729 `(let ((total rmail-total-messages))
730 (if (rmail-summary-displayed)
731 (let ((window (selected-window)))
735 (pop-to-buffer rmail-summary-buffer)
736 ;; rmail-total-messages is a buffer-local var
737 ;; in the rmail buffer.
738 ;; This way we make it available for the body
739 ;; even tho the rmail buffer is not current.
740 (let ((rmail-total-messages total))
742 (select-window window))))
744 (set-buffer rmail-summary-buffer)
745 (let ((rmail-total-messages total))
747 (rmail-maybe-display-summary)))
749 ;;;; *** Rmail Mode ***
751 ;; This variable is dynamically bound. The defvar is here to placate
752 ;; the byte compiler.
754 (defvar rmail-enable-multibyte nil)
757 (defun rmail-require-mime-maybe ()
758 "Require `rmail-mime-feature' if that is non-nil.
759 Signal an error and set `rmail-mime-feature' to nil if the feature
761 (when rmail-enable-mime
763 (require rmail-mime-feature)
767 (format "Although MIME support is requested
768 by setting `rmail-enable-mime' to non-nil, the required feature
769 `%s' (the value of `rmail-mime-feature')
770 is not available in the current session.
771 So, the MIME support is turned off for the moment."
773 (setq rmail-enable-mime nil)))))
777 (defun rmail (&optional file-name-arg)
778 "Read and edit incoming mail.
779 Moves messages into file named by `rmail-file-name' (a babyl format file)
780 and edits that file in RMAIL Mode.
781 Type \\[describe-mode] once editing that file, for a list of RMAIL commands.
783 May be called with file name as argument; then performs rmail editing on
784 that file, but does not copy any new mail into the file.
785 Interactively, if you supply a prefix argument, then you
786 have a chance to specify a file name with the minibuffer.
788 If `rmail-display-summary' is non-nil, make a summary for this RMAIL file."
789 (interactive (if current-prefix-arg
790 (list (read-file-name "Run rmail on RMAIL file: "))))
791 (rmail-require-mime-maybe)
792 (let* ((file-name (expand-file-name (or file-name-arg rmail-file-name)))
793 ;; Use find-buffer-visiting, not get-file-buffer, for those users
794 ;; who have find-file-visit-truename set to t.
795 (existed (find-buffer-visiting file-name))
796 ;; This binding is necessary because we must decide if we
797 ;; need code conversion while the buffer is unibyte
798 ;; (i.e. enable-multibyte-characters is nil).
799 (rmail-enable-multibyte
801 (with-current-buffer existed enable-multibyte-characters)
802 (default-value 'enable-multibyte-characters)))
803 ;; Since the file may contain messages of different encodings
804 ;; at the tail (non-BYBYL part), we can't decode them at once
805 ;; on reading. So, at first, we read the file without text
806 ;; code conversion, then decode the messages one by one by
807 ;; rmail-decode-babyl-format or
808 ;; rmail-convert-to-babyl-format.
809 (coding-system-for-read (and rmail-enable-multibyte 'raw-text))
810 run-mail-hook msg-shown)
811 ;; Like find-file, but in the case where a buffer existed
812 ;; and the file was reverted, recompute the message-data.
813 ;; We used to bind enable-local-variables to nil here,
814 ;; but that should not be needed now that rmail-mode
815 ;; sets it locally to nil.
816 ;; (Binding a variable locally with let is not safe if it has
817 ;; buffer-local bindings.)
818 (if (and existed (not (verify-visited-file-modtime existed)))
820 (find-file file-name)
821 (if (and (verify-visited-file-modtime existed)
822 (eq major-mode 'rmail-mode))
823 (progn (rmail-forget-messages)
824 (rmail-set-message-counters))))
826 (let ((enable-local-variables nil))
827 (find-file-noselect file-name))))
828 (if (eq major-mode 'rmail-edit-mode)
829 (error "Exit Rmail Edit mode before getting new mail"))
830 (if (and existed (> (buffer-size) 0))
831 ;; Buffer not new and not empty; ensure in proper mode, but that's all.
832 (or (eq major-mode 'rmail-mode)
833 (progn (rmail-mode-2)
834 (setq run-mail-hook t)))
835 (setq run-mail-hook t)
837 ;; Convert all or part to Babyl file if possible.
839 (goto-char (point-max)))
840 ;; As we have read a file by raw-text, the buffer is set to
841 ;; unibyte. We must make it multibyte if necessary.
842 (if (and rmail-enable-multibyte
843 (not enable-multibyte-characters))
844 (set-buffer-multibyte t))
845 ;; If necessary, scan to find all the messages.
846 (rmail-maybe-set-message-counters)
848 (unless (and (not file-name-arg)
849 (rmail-get-new-mail))
850 (rmail-show-message (rmail-first-unseen-message)))
852 (if rmail-display-summary (rmail-summary))
853 (rmail-construct-io-menu)
855 (run-hooks 'rmail-mode-hook))))))
857 ;; Given the value of MAILPATH, return a list of inbox file names.
858 ;; This is turned off because it is not clear that the user wants
859 ;; all these inboxes to feed into the primary rmail file.
860 ; (defun rmail-convert-mailpath (string)
862 ; (while (setq idx (string-match "[%:]" string))
863 ; (let ((this (substring string 0 idx)))
864 ; (setq string (substring string (1+ idx)))
865 ; (setq list (cons (if (string-match "%" this)
866 ; (substring this 0 (string-match "%" this))
871 ; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
872 ; will not cause emacs 18.55 problems.
874 ;; This calls rmail-decode-babyl-format if the file is already Babyl.
876 (defun rmail-convert-file ()
879 (goto-char (point-min))
880 ;; If file doesn't start like a Babyl file,
881 ;; convert it to one, by adding a header and converting each message.
882 (cond ((looking-at "BABYL OPTIONS:"))
883 ((looking-at "Version: 5\n")
884 ;; Losing babyl file made by old version of Rmail.
885 ;; Just fix the babyl file header; don't make a new one,
886 ;; so we don't lose the Labels: file attribute, etc.
887 (let ((buffer-read-only nil))
888 (insert "BABYL OPTIONS: -*- rmail -*-\n")))
889 ((equal (point-min) (point-max))
890 ;; Empty RMAIL file. Just insert the header.
891 (rmail-insert-rmail-file-header))
893 ;; Non-empty file in non-RMAIL format. Add header and convert.
895 (rmail-insert-rmail-file-header)))
896 ;; If file was not a Babyl file or if there are
897 ;; Unix format messages added at the end,
898 ;; convert file as necessary.
901 (goto-char (point-max))
902 (search-backward "\n\^_")
904 (looking-at "\n*From ")))
905 (let ((buffer-read-only nil))
906 (message "Converting to Babyl format...")
907 ;; If file needs conversion, convert it all,
908 ;; except for the BABYL header.
909 ;; (rmail-convert-to-babyl-format would delete the header.)
910 (goto-char (point-min))
911 (search-forward "\n\^_" nil t)
912 (narrow-to-region (point) (point-max))
913 (rmail-convert-to-babyl-format)
914 (message "Converting to Babyl format...done"))
915 (if (and (not rmail-enable-mime)
916 rmail-enable-multibyte)
917 ;; We still have to decode BABYL part.
918 (rmail-decode-babyl-format)))))
920 (defun rmail-insert-rmail-file-header ()
921 (let ((buffer-read-only nil))
922 ;; -*-rmail-*- is here so that visiting the file normally
923 ;; recognizes it as an Rmail file.
924 (insert "BABYL OPTIONS: -*- rmail -*-
927 Note: This is the header of an rmail file.
928 Note: If you are seeing it in rmail,
929 Note: it means the file has no messages in it.\n\^_")))
931 ;; Decode Babyl formatted part at the head of current buffer by
932 ;; rmail-file-coding-system, or if it is nil, do auto conversion.
934 (defun rmail-decode-babyl-format ()
935 (let ((modifiedp (buffer-modified-p))
936 (buffer-read-only nil)
937 (coding-system rmail-file-coding-system)
939 (goto-char (point-min))
940 (search-forward "\n\^_" nil t) ; Skip BABYL header.
942 (goto-char (point-max))
943 (search-backward "\n\^_" from 'mv)
945 (unless (and coding-system
946 (coding-system-p coding-system))
948 ;; If rmail-file-coding-system is nil, Emacs 21 writes
949 ;; RMAIL files in emacs-mule, Emacs 22 in utf-8, but
950 ;; earlier versions did that with the current buffer's
951 ;; encoding. So we want to favor detection of emacs-mule
952 ;; (whose normal priority is quite low) and utf-8, but
953 ;; still allow detection of other encodings if they won't
954 ;; fit. The call to with-coding-priority below achieves
956 (with-coding-priority '(emacs-mule utf-8)
957 (detect-coding-region from to 'highest))))
958 (unless (eq (coding-system-type coding-system) 'undecided)
959 (set-buffer-modified-p t) ; avoid locking when decoding
960 (let ((buffer-undo-list t))
961 (decode-coding-region from to coding-system))
962 (setq coding-system last-coding-system-used))
963 (set-buffer-modified-p modifiedp)
964 (setq buffer-file-coding-system nil)
965 (setq save-buffer-coding-system
966 (or coding-system 'undecided))))
968 (defvar rmail-mode-map nil)
971 (setq rmail-mode-map (make-keymap))
972 (suppress-keymap rmail-mode-map)
973 (define-key rmail-mode-map "a" 'rmail-add-label)
974 (define-key rmail-mode-map "b" 'rmail-bury)
975 (define-key rmail-mode-map "c" 'rmail-continue)
976 (define-key rmail-mode-map "d" 'rmail-delete-forward)
977 (define-key rmail-mode-map "\C-d" 'rmail-delete-backward)
978 (define-key rmail-mode-map "e" 'rmail-edit-current-message)
979 (define-key rmail-mode-map "f" 'rmail-forward)
980 (define-key rmail-mode-map "g" 'rmail-get-new-mail)
981 (define-key rmail-mode-map "h" 'rmail-summary)
982 (define-key rmail-mode-map "i" 'rmail-input)
983 (define-key rmail-mode-map "j" 'rmail-show-message)
984 (define-key rmail-mode-map "k" 'rmail-kill-label)
985 (define-key rmail-mode-map "l" 'rmail-summary-by-labels)
986 (define-key rmail-mode-map "\e\C-h" 'rmail-summary)
987 (define-key rmail-mode-map "\e\C-l" 'rmail-summary-by-labels)
988 (define-key rmail-mode-map "\e\C-r" 'rmail-summary-by-recipients)
989 (define-key rmail-mode-map "\e\C-s" 'rmail-summary-by-regexp)
990 (define-key rmail-mode-map "\e\C-t" 'rmail-summary-by-topic)
991 (define-key rmail-mode-map "m" 'rmail-mail)
992 (define-key rmail-mode-map "\em" 'rmail-retry-failure)
993 (define-key rmail-mode-map "n" 'rmail-next-undeleted-message)
994 (define-key rmail-mode-map "\en" 'rmail-next-message)
995 (define-key rmail-mode-map "\e\C-n" 'rmail-next-labeled-message)
996 (define-key rmail-mode-map "o" 'rmail-output-to-rmail-file)
997 (define-key rmail-mode-map "\C-o" 'rmail-output)
998 (define-key rmail-mode-map "p" 'rmail-previous-undeleted-message)
999 (define-key rmail-mode-map "\ep" 'rmail-previous-message)
1000 (define-key rmail-mode-map "\e\C-p" 'rmail-previous-labeled-message)
1001 (define-key rmail-mode-map "q" 'rmail-quit)
1002 (define-key rmail-mode-map "r" 'rmail-reply)
1003 ;; I find I can't live without the default M-r command -- rms.
1004 ;; (define-key rmail-mode-map "\er" 'rmail-search-backwards)
1005 (define-key rmail-mode-map "s" 'rmail-expunge-and-save)
1006 (define-key rmail-mode-map "\es" 'rmail-search)
1007 (define-key rmail-mode-map "t" 'rmail-toggle-header)
1008 (define-key rmail-mode-map "u" 'rmail-undelete-previous-message)
1009 (define-key rmail-mode-map "w" 'rmail-output-body-to-file)
1010 (define-key rmail-mode-map "x" 'rmail-expunge)
1011 (define-key rmail-mode-map "." 'rmail-beginning-of-message)
1012 (define-key rmail-mode-map "/" 'rmail-end-of-message)
1013 (define-key rmail-mode-map "<" 'rmail-first-message)
1014 (define-key rmail-mode-map ">" 'rmail-last-message)
1015 (define-key rmail-mode-map " " 'scroll-up)
1016 (define-key rmail-mode-map "\177" 'scroll-down)
1017 (define-key rmail-mode-map "?" 'describe-mode)
1018 (define-key rmail-mode-map "\C-c\C-s\C-d" 'rmail-sort-by-date)
1019 (define-key rmail-mode-map "\C-c\C-s\C-s" 'rmail-sort-by-subject)
1020 (define-key rmail-mode-map "\C-c\C-s\C-a" 'rmail-sort-by-author)
1021 (define-key rmail-mode-map "\C-c\C-s\C-r" 'rmail-sort-by-recipient)
1022 (define-key rmail-mode-map "\C-c\C-s\C-c" 'rmail-sort-by-correspondent)
1023 (define-key rmail-mode-map "\C-c\C-s\C-l" 'rmail-sort-by-lines)
1024 (define-key rmail-mode-map "\C-c\C-s\C-k" 'rmail-sort-by-labels)
1025 (define-key rmail-mode-map "\C-c\C-n" 'rmail-next-same-subject)
1026 (define-key rmail-mode-map "\C-c\C-p" 'rmail-previous-same-subject)
1029 (define-key rmail-mode-map [menu-bar] (make-sparse-keymap))
1031 (define-key rmail-mode-map [menu-bar classify]
1032 (cons "Classify" (make-sparse-keymap "Classify")))
1034 (define-key rmail-mode-map [menu-bar classify input-menu]
1037 (define-key rmail-mode-map [menu-bar classify output-menu]
1040 (define-key rmail-mode-map [menu-bar classify output-body]
1041 '("Output body to file..." . rmail-output-body-to-file))
1043 (define-key rmail-mode-map [menu-bar classify output-inbox]
1044 '("Output (inbox)..." . rmail-output))
1046 (define-key rmail-mode-map [menu-bar classify output]
1047 '("Output (Rmail)..." . rmail-output-to-rmail-file))
1049 (define-key rmail-mode-map [menu-bar classify kill-label]
1050 '("Kill Label..." . rmail-kill-label))
1052 (define-key rmail-mode-map [menu-bar classify add-label]
1053 '("Add Label..." . rmail-add-label))
1055 (define-key rmail-mode-map [menu-bar summary]
1056 (cons "Summary" (make-sparse-keymap "Summary")))
1058 (define-key rmail-mode-map [menu-bar summary senders]
1059 '("By Senders..." . rmail-summary-by-senders))
1061 (define-key rmail-mode-map [menu-bar summary labels]
1062 '("By Labels..." . rmail-summary-by-labels))
1064 (define-key rmail-mode-map [menu-bar summary recipients]
1065 '("By Recipients..." . rmail-summary-by-recipients))
1067 (define-key rmail-mode-map [menu-bar summary topic]
1068 '("By Topic..." . rmail-summary-by-topic))
1070 (define-key rmail-mode-map [menu-bar summary regexp]
1071 '("By Regexp..." . rmail-summary-by-regexp))
1073 (define-key rmail-mode-map [menu-bar summary all]
1074 '("All" . rmail-summary))
1076 (define-key rmail-mode-map [menu-bar mail]
1077 (cons "Mail" (make-sparse-keymap "Mail")))
1079 (define-key rmail-mode-map [menu-bar mail rmail-get-new-mail]
1080 '("Get New Mail" . rmail-get-new-mail))
1082 (define-key rmail-mode-map [menu-bar mail lambda]
1085 (define-key rmail-mode-map [menu-bar mail continue]
1086 '("Continue" . rmail-continue))
1088 (define-key rmail-mode-map [menu-bar mail resend]
1089 '("Re-send..." . rmail-resend))
1091 (define-key rmail-mode-map [menu-bar mail forward]
1092 '("Forward" . rmail-forward))
1094 (define-key rmail-mode-map [menu-bar mail retry]
1095 '("Retry" . rmail-retry-failure))
1097 (define-key rmail-mode-map [menu-bar mail reply]
1098 '("Reply" . rmail-reply))
1100 (define-key rmail-mode-map [menu-bar mail mail]
1101 '("Mail" . rmail-mail))
1103 (define-key rmail-mode-map [menu-bar delete]
1104 (cons "Delete" (make-sparse-keymap "Delete")))
1106 (define-key rmail-mode-map [menu-bar delete expunge/save]
1107 '("Expunge/Save" . rmail-expunge-and-save))
1109 (define-key rmail-mode-map [menu-bar delete expunge]
1110 '("Expunge" . rmail-expunge))
1112 (define-key rmail-mode-map [menu-bar delete undelete]
1113 '("Undelete" . rmail-undelete-previous-message))
1115 (define-key rmail-mode-map [menu-bar delete delete]
1116 '("Delete" . rmail-delete-forward))
1118 (define-key rmail-mode-map [menu-bar move]
1119 (cons "Move" (make-sparse-keymap "Move")))
1121 (define-key rmail-mode-map [menu-bar move search-back]
1122 '("Search Back..." . rmail-search-backwards))
1124 (define-key rmail-mode-map [menu-bar move search]
1125 '("Search..." . rmail-search))
1127 (define-key rmail-mode-map [menu-bar move previous]
1128 '("Previous Nondeleted" . rmail-previous-undeleted-message))
1130 (define-key rmail-mode-map [menu-bar move next]
1131 '("Next Nondeleted" . rmail-next-undeleted-message))
1133 (define-key rmail-mode-map [menu-bar move last]
1134 '("Last" . rmail-last-message))
1136 (define-key rmail-mode-map [menu-bar move first]
1137 '("First" . rmail-first-message))
1139 (define-key rmail-mode-map [menu-bar move previous]
1140 '("Previous" . rmail-previous-message))
1142 (define-key rmail-mode-map [menu-bar move next]
1143 '("Next" . rmail-next-message))
1146 (defvar rmail-tool-bar-map
1147 (if (display-graphic-p)
1148 (let ((map (make-sparse-keymap)))
1149 (tool-bar-local-item-from-menu 'rmail-get-new-mail "mail/inbox"
1151 (tool-bar-local-item-from-menu 'rmail-next-undeleted-message "right-arrow"
1153 (tool-bar-local-item-from-menu 'rmail-previous-undeleted-message "left-arrow"
1155 (tool-bar-local-item-from-menu 'rmail-search "search"
1157 (tool-bar-local-item-from-menu 'rmail-input "open"
1159 (tool-bar-local-item-from-menu 'rmail-mail "mail/compose"
1161 (tool-bar-local-item-from-menu 'rmail-reply "mail/reply-all"
1163 (tool-bar-local-item-from-menu 'rmail-forward "mail/forward"
1165 (tool-bar-local-item-from-menu 'rmail-delete-forward "close"
1167 (tool-bar-local-item-from-menu 'rmail-output "mail/move"
1169 (tool-bar-local-item-from-menu 'rmail-output-body-to-file "mail/save"
1171 (tool-bar-local-item-from-menu 'rmail-expunge "delete"
1177 ;; Rmail mode is suitable only for specially formatted data.
1178 (put 'rmail-mode 'mode-class 'special)
1180 (defun rmail-mode-kill-summary ()
1181 (if rmail-summary-buffer (kill-buffer rmail-summary-buffer)))
1184 (defun rmail-mode ()
1185 "Rmail Mode is used by \\<rmail-mode-map>\\[rmail] for editing Rmail files.
1186 All normal editing commands are turned off.
1187 Instead, these commands are available:
1189 \\[rmail-beginning-of-message] Move point to front of this message.
1190 \\[rmail-end-of-message] Move point to bottom of this message.
1191 \\[scroll-up] Scroll to next screen of this message.
1192 \\[scroll-down] Scroll to previous screen of this message.
1193 \\[rmail-next-undeleted-message] Move to Next non-deleted message.
1194 \\[rmail-previous-undeleted-message] Move to Previous non-deleted message.
1195 \\[rmail-next-message] Move to Next message whether deleted or not.
1196 \\[rmail-previous-message] Move to Previous message whether deleted or not.
1197 \\[rmail-first-message] Move to the first message in Rmail file.
1198 \\[rmail-last-message] Move to the last message in Rmail file.
1199 \\[rmail-show-message] Jump to message specified by numeric position in file.
1200 \\[rmail-search] Search for string and show message it is found in.
1201 \\[rmail-delete-forward] Delete this message, move to next nondeleted.
1202 \\[rmail-delete-backward] Delete this message, move to previous nondeleted.
1203 \\[rmail-undelete-previous-message] Undelete message. Tries current message, then earlier messages
1204 till a deleted message is found.
1205 \\[rmail-edit-current-message] Edit the current message. \\[rmail-cease-edit] to return to Rmail.
1206 \\[rmail-expunge] Expunge deleted messages.
1207 \\[rmail-expunge-and-save] Expunge and save the file.
1208 \\[rmail-quit] Quit Rmail: expunge, save, then switch to another buffer.
1209 \\[save-buffer] Save without expunging.
1210 \\[rmail-get-new-mail] Move new mail from system spool directory into this file.
1211 \\[rmail-mail] Mail a message (same as \\[mail-other-window]).
1212 \\[rmail-continue] Continue composing outgoing message started before.
1213 \\[rmail-reply] Reply to this message. Like \\[rmail-mail] but initializes some fields.
1214 \\[rmail-retry-failure] Send this message again. Used on a mailer failure message.
1215 \\[rmail-forward] Forward this message to another user.
1216 \\[rmail-output-to-rmail-file] Output this message to an Rmail file (append it).
1217 \\[rmail-output] Output this message to a Unix-format mail file (append it).
1218 \\[rmail-output-body-to-file] Save message body to a file. Default filename comes from Subject line.
1219 \\[rmail-input] Input Rmail file. Run Rmail on that file.
1220 \\[rmail-add-label] Add label to message. It will be displayed in the mode line.
1221 \\[rmail-kill-label] Kill label. Remove a label from current message.
1222 \\[rmail-next-labeled-message] Move to Next message with specified label
1223 (label defaults to last one specified).
1224 Standard labels: filed, unseen, answered, forwarded, deleted.
1225 Any other label is present only if you add it with \\[rmail-add-label].
1226 \\[rmail-previous-labeled-message] Move to Previous message with specified label
1227 \\[rmail-summary] Show headers buffer, with a one line summary of each message.
1228 \\[rmail-summary-by-labels] Summarize only messages with particular label(s).
1229 \\[rmail-summary-by-recipients] Summarize only messages with particular recipient(s).
1230 \\[rmail-summary-by-regexp] Summarize only messages with particular regexp(s).
1231 \\[rmail-summary-by-topic] Summarize only messages with subject line regexp(s).
1232 \\[rmail-toggle-header] Toggle display of complete header."
1234 (let ((finding-rmail-file (not (eq major-mode 'rmail-mode))))
1236 (when (and finding-rmail-file
1237 (null coding-system-for-read)
1238 default-enable-multibyte-characters)
1239 (let ((rmail-enable-multibyte t))
1240 (rmail-require-mime-maybe)
1241 (rmail-convert-file)
1242 (goto-char (point-max))
1243 (set-buffer-multibyte t)))
1244 (rmail-set-message-counters)
1245 (rmail-show-message rmail-total-messages)
1246 (when finding-rmail-file
1247 (when rmail-display-summary
1249 (rmail-construct-io-menu))
1250 (run-mode-hooks 'rmail-mode-hook)))
1252 (defun rmail-mode-2 ()
1253 (kill-all-local-variables)
1255 (rmail-perm-variables)
1258 (defun rmail-mode-1 ()
1259 (setq major-mode 'rmail-mode)
1260 (setq mode-name "RMAIL")
1261 (setq buffer-read-only t)
1262 ;; No need to auto save RMAIL files in normal circumstances
1263 ;; because they contain no info except attribute changes
1264 ;; and deletion of messages.
1265 ;; The one exception is when messages are copied into an Rmail mode buffer.
1266 ;; rmail-output-to-rmail-file enables auto save when you do that.
1267 (setq buffer-auto-save-file-name nil)
1268 (setq mode-line-modified "--")
1269 (use-local-map rmail-mode-map)
1270 (set-syntax-table text-mode-syntax-table)
1271 (setq local-abbrev-table text-mode-abbrev-table))
1273 ;; Set up the permanent locals associated with an Rmail file.
1274 (defun rmail-perm-variables ()
1275 (make-local-variable 'rmail-last-label)
1276 (make-local-variable 'rmail-last-regexp)
1277 (make-local-variable 'rmail-deleted-vector)
1278 (make-local-variable 'rmail-buffer)
1279 (setq rmail-buffer (current-buffer))
1280 (make-local-variable 'rmail-view-buffer)
1281 (setq rmail-view-buffer rmail-buffer)
1282 (make-local-variable 'rmail-summary-buffer)
1283 (make-local-variable 'rmail-summary-vector)
1284 (make-local-variable 'rmail-current-message)
1285 (make-local-variable 'rmail-total-messages)
1286 (make-local-variable 'rmail-overlay-list)
1287 (setq rmail-overlay-list nil)
1288 (make-local-variable 'rmail-message-vector)
1289 (make-local-variable 'rmail-msgref-vector)
1290 (make-local-variable 'rmail-inbox-list)
1291 (setq rmail-inbox-list (rmail-parse-file-inboxes))
1292 ;; Provide default set of inboxes for primary mail file ~/RMAIL.
1293 (and (null rmail-inbox-list)
1294 (or (equal buffer-file-name (expand-file-name rmail-file-name))
1295 (equal buffer-file-truename
1296 (abbreviate-file-name (file-truename rmail-file-name))))
1297 (setq rmail-inbox-list
1298 (or rmail-primary-inbox-list
1299 (list (or (getenv "MAIL")
1300 (concat rmail-spool-directory
1301 (user-login-name)))))))
1302 (make-local-variable 'rmail-keywords)
1303 (set (make-local-variable 'tool-bar-map) rmail-tool-bar-map)
1304 ;; this gets generated as needed
1305 (setq rmail-keywords nil))
1307 ;; Set up the non-permanent locals associated with Rmail mode.
1308 (defun rmail-variables ()
1309 (make-local-variable 'save-buffer-coding-system)
1310 ;; If we don't already have a value for save-buffer-coding-system,
1311 ;; get it from buffer-file-coding-system, and clear that
1312 ;; because it should be determined in rmail-show-message.
1313 (unless save-buffer-coding-system
1314 (setq save-buffer-coding-system (or buffer-file-coding-system 'undecided))
1315 (setq buffer-file-coding-system nil))
1316 ;; Don't let a local variables list in a message cause confusion.
1317 (make-local-variable 'local-enable-local-variables)
1318 (setq local-enable-local-variables nil)
1319 (make-local-variable 'revert-buffer-function)
1320 (setq revert-buffer-function 'rmail-revert)
1321 (make-local-variable 'font-lock-defaults)
1322 (setq font-lock-defaults
1323 '(rmail-font-lock-keywords
1325 (font-lock-maximum-size . nil)
1326 (font-lock-fontify-buffer-function . rmail-fontify-buffer-function)
1327 (font-lock-unfontify-buffer-function . rmail-unfontify-buffer-function)
1328 (font-lock-inhibit-thing-lock . (lazy-lock-mode fast-lock-mode))))
1329 (make-local-variable 'require-final-newline)
1330 (setq require-final-newline nil)
1331 (make-local-variable 'version-control)
1332 (setq version-control 'never)
1333 (make-local-variable 'kill-buffer-hook)
1334 (add-hook 'kill-buffer-hook 'rmail-mode-kill-summary)
1335 (make-local-variable 'file-precious-flag)
1336 (setq file-precious-flag t)
1337 (make-local-variable 'desktop-save-buffer)
1338 (setq desktop-save-buffer t))
1340 ;; Handle M-x revert-buffer done in an rmail-mode buffer.
1341 (defun rmail-revert (arg noconfirm)
1342 (set-buffer rmail-buffer)
1343 (let* ((revert-buffer-function (default-value 'revert-buffer-function))
1344 (rmail-enable-multibyte enable-multibyte-characters)
1345 ;; See similar code in `rmail'.
1346 (coding-system-for-read (and rmail-enable-multibyte 'raw-text)))
1347 ;; Call our caller again, but this time it does the default thing.
1348 (if (revert-buffer arg noconfirm)
1349 ;; If the user said "yes", and we changed something,
1350 ;; reparse the messages.
1352 (set-buffer rmail-buffer)
1354 ;; Convert all or part to Babyl file if possible.
1355 (rmail-convert-file)
1356 ;; We have read the file as raw-text, so the buffer is set to
1357 ;; unibyte. Make it multibyte if necessary.
1358 (if (and rmail-enable-multibyte
1359 (not enable-multibyte-characters))
1360 (set-buffer-multibyte t))
1361 (goto-char (point-max))
1362 (rmail-set-message-counters)
1363 (rmail-show-message rmail-total-messages)
1364 (run-hooks 'rmail-mode-hook)))))
1366 ;; Return a list of files from this buffer's Mail: option.
1367 ;; Does not assume that messages have been parsed.
1368 ;; Just returns nil if buffer does not look like Babyl format.
1369 (defun rmail-parse-file-inboxes ()
1374 (cond ((looking-at "BABYL OPTIONS:")
1375 (search-forward "\n\^_" nil 'move)
1376 (narrow-to-region 1 (point))
1378 (if (search-forward "\nMail:" nil t)
1380 (narrow-to-region (point) (progn (end-of-line) (point)))
1381 (goto-char (point-min))
1382 (mail-parse-comma-list))))))))
1384 (defun rmail-expunge-and-save ()
1385 "Expunge and save RMAIL file."
1388 (set-buffer rmail-buffer)
1390 (if (rmail-summary-exists)
1391 (rmail-select-summary (set-buffer-modified-p nil))))
1393 (defun rmail-quit ()
1395 Hook `rmail-quit-hook' is run after expunging."
1397 (rmail-expunge-and-save)
1398 (when (boundp 'rmail-quit-hook)
1399 (run-hooks 'rmail-quit-hook))
1400 ;; Don't switch to the summary buffer even if it was recently visible.
1401 (when rmail-summary-buffer
1402 (replace-buffer-in-windows rmail-summary-buffer)
1403 (bury-buffer rmail-summary-buffer))
1404 (if rmail-enable-mime
1405 (let ((obuf rmail-buffer)
1406 (ovbuf rmail-view-buffer))
1407 (set-buffer rmail-view-buffer)
1409 (replace-buffer-in-windows ovbuf)
1410 (replace-buffer-in-windows obuf)
1412 (let ((obuf (current-buffer)))
1414 (replace-buffer-in-windows obuf))))
1416 (defun rmail-bury ()
1417 "Bury current Rmail buffer and its summary buffer."
1419 ;; This let var was called rmail-buffer, but that interfered
1420 ;; with the buffer-local var used in summary buffers.
1421 (let ((buffer-to-bury (current-buffer)))
1422 (if (rmail-summary-exists)
1424 (while (setq window (get-buffer-window rmail-summary-buffer))
1425 (quit-window nil window))
1426 (bury-buffer rmail-summary-buffer)))
1429 (defun rmail-duplicate-message ()
1430 "Create a duplicated copy of the current message.
1431 The duplicate copy goes into the Rmail file just after the
1435 (let ((buffer-read-only nil)
1436 (number rmail-current-message)
1437 (string (buffer-substring (rmail-msgbeg rmail-current-message)
1438 (rmail-msgend rmail-current-message))))
1439 (goto-char (rmail-msgend rmail-current-message))
1441 (rmail-forget-messages)
1442 (rmail-show-message number)
1443 (message "Message duplicated")))
1446 (defun rmail-input (filename)
1447 "Run Rmail on file FILENAME."
1448 (interactive "FRun rmail on RMAIL file: ")
1452 ;; This used to scan subdirectories recursively, but someone pointed out
1453 ;; that if the user wants that, person can put all the files in one dir.
1454 ;; And the recursive scan was slow. So I took it out.
1456 (defun rmail-find-all-files (start)
1457 "Return list of file in dir START that match `rmail-secondary-file-regexp'."
1458 (if (file-accessible-directory-p start)
1460 (let* ((case-fold-search t)
1461 (files (directory-files start t rmail-secondary-file-regexp)))
1462 ;; Sort here instead of in directory-files
1463 ;; because this list is usually much shorter.
1464 (sort files 'string<))))
1466 (defun rmail-list-to-menu (menu-name l action &optional full-name)
1467 (let ((menu (make-sparse-keymap menu-name)))
1469 (function (lambda (item)
1474 (rmail-list-to-menu (car item) (cdr item)
1477 (concat full-name "/"
1480 (setq name (car item)))
1484 (list 'lambda () '(interactive)
1488 (concat full-name "/" item)
1490 rmail-secondary-file-directory))))))
1491 (define-key menu (vector (intern name))
1492 (cons name command)))))
1496 ;; This command is always "disabled" when it appears in a menu.
1497 (put 'rmail-disable-menu 'menu-enable ''nil)
1499 (defun rmail-construct-io-menu ()
1500 (let ((files (rmail-find-all-files rmail-secondary-file-directory)))
1503 (define-key rmail-mode-map [menu-bar classify input-menu]
1504 (cons "Input Rmail File"
1505 (rmail-list-to-menu "Input Rmail File"
1508 (define-key rmail-mode-map [menu-bar classify output-menu]
1509 (cons "Output Rmail File"
1510 (rmail-list-to-menu "Output Rmail File"
1512 'rmail-output-to-rmail-file))))
1514 (define-key rmail-mode-map [menu-bar classify input-menu]
1515 '("Input Rmail File" . rmail-disable-menu))
1516 (define-key rmail-mode-map [menu-bar classify output-menu]
1517 '("Output Rmail File" . rmail-disable-menu)))))
1520 ;;;; *** Rmail input ***
1522 (declare-function rmail-spam-filter "rmail-spam-filter" (msg))
1523 (declare-function rmail-summary-goto-msg "rmailsum" (&optional n nowarn skip-rmail))
1524 (declare-function rmail-summary-mark-undeleted "rmailsum" (n))
1525 (declare-function rmail-summary-mark-deleted "rmailsum" (&optional n undel))
1526 (declare-function rfc822-addresses "rfc822" (header-text))
1527 (declare-function mail-abbrev-make-syntax-table "mailabbrev.el" ())
1528 (declare-function mail-sendmail-delimit-header "sendmail" ())
1529 (declare-function mail-header-end "sendmail" ())
1531 ;; RLK feature not added in this version:
1532 ;; argument specifies inbox file or files in various ways.
1534 (defun rmail-get-new-mail (&optional file-name)
1535 "Move any new mail from this RMAIL file's inbox files.
1536 The inbox files can be specified with the file's Mail: option. The
1537 variable `rmail-primary-inbox-list' specifies the inboxes for your
1538 primary RMAIL file if it has no Mail: option. By default, this is
1539 your /usr/spool/mail/$USER.
1541 You can also specify the file to get new mail from. In this case, the
1542 file of new mail is not changed or deleted. Noninteractively, you can
1543 pass the inbox file name as an argument. Interactively, a prefix
1544 argument causes us to read a file name and use that file as the inbox.
1546 If the variable `rmail-preserve-inbox' is non-nil, new mail will
1547 always be left in inbox files rather than deleted.
1549 This function runs `rmail-get-new-mail-hook' before saving the updated file.
1550 It returns t if it got any new messages."
1552 (list (if current-prefix-arg
1553 (read-file-name "Get new mail from file: "))))
1554 (run-hooks 'rmail-before-get-new-mail-hook)
1555 ;; If the disk file has been changed from under us,
1556 ;; revert to it before we get new mail.
1557 (or (verify-visited-file-modtime (current-buffer))
1558 (find-file (buffer-file-name)))
1559 (set-buffer rmail-buffer)
1560 (rmail-maybe-set-message-counters)
1562 ;; Get rid of all undo records for this buffer.
1563 (or (eq buffer-undo-list t)
1564 (setq buffer-undo-list nil))
1565 (let ((all-files (if file-name (list file-name)
1567 (rmail-enable-multibyte (default-value 'enable-multibyte-characters))
1572 (let ((opoint (point))
1574 (rsf-number-of-spam 0)
1576 ;; If buffer has not changed yet, and has not been saved yet,
1577 ;; don't replace the old backup file now.
1578 (make-backup-files (and make-backup-files (buffer-modified-p)))
1579 (buffer-read-only nil)
1580 ;; Don't make undo records for what we do in getting mail.
1581 (buffer-undo-list t)
1583 ;; Files to insert this time around.
1585 ;; Last names of those files.
1587 ;; Pull files off all-files onto files
1588 ;; as long as there is no name conflict.
1589 ;; A conflict happens when two inbox file names
1590 ;; have the same last component.
1591 (while (and all-files
1592 (not (member (file-name-nondirectory (car all-files))
1594 (setq files (cons (car all-files) files)
1596 (cons (file-name-nondirectory (car all-files)) files))
1597 (setq all-files (cdr all-files)))
1598 ;; Put them back in their original order.
1599 (setq files (nreverse files))
1601 (goto-char (point-max))
1602 (skip-chars-backward " \t\n") ; just in case of brain damage
1603 (delete-region (point) (point-max)) ; caused by require-final-newline
1606 (narrow-to-region (point) (point))
1607 ;; Read in the contents of the inbox files,
1608 ;; renaming them as necessary,
1609 ;; and adding to the list of files to delete eventually.
1611 (rmail-insert-inbox-text files nil)
1612 (setq delete-files (rmail-insert-inbox-text files t)))
1613 ;; Scan the new text and convert each message to babyl format.
1614 (goto-char (point-min))
1617 (setq new-messages (rmail-convert-to-babyl-format)
1619 ;; Try to delete the garbage just inserted.
1620 (or success (delete-region (point-min) (point-max)))
1621 ;; If we could not convert the file's inboxes,
1622 ;; rename the files we tried to read
1623 ;; so we won't over and over again.
1624 (if (and (not file-name) (not success))
1625 (let ((delfiles delete-files)
1628 (while (file-exists-p (format "RMAILOSE.%d" count))
1629 (setq count (1+ count)))
1630 (rename-file (car delfiles)
1631 (format "RMAILOSE.%d" count))
1632 (setq delfiles (cdr delfiles))))))
1633 (or (zerop new-messages)
1636 (search-backward "\n\^_" nil t)
1637 (narrow-to-region (point) (point-max))
1638 (goto-char (1+ (point-min)))
1639 (rmail-count-new-messages)
1640 (run-hooks 'rmail-get-new-mail-hook)
1642 ;; Delete the old files, now that babyl file is saved.
1645 ;; First, try deleting.
1647 (delete-file (car delete-files))
1649 ;; If we can't delete it, truncate it.
1650 (write-region (point) (point) (car delete-files))))
1652 (setq delete-files (cdr delete-files)))))
1653 (if (= new-messages 0)
1654 (progn (goto-char opoint)
1655 (if (or file-name rmail-inbox-list)
1656 (message "(No new mail has arrived)")))
1657 ;; check new messages to see if any of them is spam:
1658 (if (and (featurep 'rmail-spam-filter)
1659 rmail-use-spam-filter)
1661 ((old-messages (- rmail-total-messages new-messages))
1662 (rsf-scanned-message-number (1+ old-messages))
1663 ;; save deletion flags of old messages: vector starts
1664 ;; at zero (is one longer that no of messages),
1665 ;; therefore take 1+ old-messages
1667 (substring rmail-deleted-vector 0 (1+
1669 ;; set all messages to undeleted
1670 (setq rmail-deleted-vector
1671 (make-string (1+ rmail-total-messages) ?\ ))
1672 (while (<= rsf-scanned-message-number
1673 rmail-total-messages)
1675 (if (not (rmail-spam-filter rsf-scanned-message-number))
1676 (progn (setq rsf-number-of-spam (1+ rsf-number-of-spam)))
1678 (setq rsf-scanned-message-number (1+ rsf-scanned-message-number))
1680 (if (> rsf-number-of-spam 0)
1682 (when (rmail-expunge-confirmed)
1683 (rmail-only-expunge t))
1685 (setq rmail-deleted-vector
1688 (make-string (- rmail-total-messages old-messages)
1691 (if (rmail-summary-exists)
1692 (rmail-select-summary
1693 (rmail-update-summary)))
1694 (message "%d new message%s read%s"
1695 new-messages (if (= 1 new-messages) "" "s")
1696 ;; print out a message on number of spam messages found:
1697 (if (and (featurep 'rmail-spam-filter)
1698 rmail-use-spam-filter
1699 (> rsf-number-of-spam 0))
1700 (cond ((= 1 new-messages)
1701 ", and appears to be spam")
1702 ((= rsf-number-of-spam new-messages)
1703 ", and all appear to be spam")
1704 ((> rsf-number-of-spam 1)
1705 (format ", and %d appear to be spam"
1706 rsf-number-of-spam))
1708 ", and 1 appears to be spam"))
1710 (if (and (featurep 'rmail-spam-filter)
1711 rmail-use-spam-filter
1712 (> rsf-number-of-spam 0))
1713 (progn (if rsf-beep (beep t))
1714 (sleep-for rsf-sleep-after-message)))
1716 ;; Move to the first new message
1717 ;; unless we have other unseen messages before it.
1718 (rmail-show-message (rmail-first-unseen-message))
1719 (run-hooks 'rmail-after-get-new-mail-hook)
1722 ;; Don't leave the buffer screwed up if we get a disk-full error.
1723 (or found (rmail-show-message)))))
1725 (defun rmail-parse-url (file)
1726 "Parse the supplied URL. Return (list MAILBOX-NAME REMOTE PASSWORD GOT-PASSWORD)
1727 WHERE MAILBOX-NAME is the name of the mailbox suitable as argument to the
1728 actual version of `movemail', REMOTE is non-nil if MAILBOX-NAME refers to
1729 a remote mailbox, PASSWORD is the password if it should be
1730 supplied as a separate argument to `movemail' or nil otherwise, GOT-PASSWORD
1731 is non-nil if the user has supplied the password interactively.
1734 ((string-match "^\\([^:]+\\)://\\(\\([^:@]+\\)\\(:\\([^@]+\\)\\)?@\\)?.*" file)
1735 (let (got-password supplied-password
1736 (proto (match-string 1 file))
1737 (user (match-string 3 file))
1738 (pass (match-string 5 file))
1739 (host (substring file (or (match-end 2)
1740 (+ 3 (match-end 1))))))
1743 (when rmail-remote-password-required
1744 (setq got-password (not (rmail-have-password)))
1745 (setq supplied-password (rmail-get-remote-password
1746 (string-equal proto "imap")))))
1748 (if (rmail-movemail-variant-p 'emacs)
1749 (if (string-equal proto "pop")
1750 (list (concat "po:" user ":" host)
1752 (or pass supplied-password)
1754 (error "Emacs movemail does not support %s protocol" proto))
1756 (or (string-equal proto "pop") (string-equal proto "imap"))
1760 ((string-match "^po:\\([^:]+\\)\\(:\\(.*\\)\\)?" file)
1761 (let (got-password supplied-password
1763 (user (match-string 1 file))
1764 (host (match-string 3 file)))
1766 (when rmail-remote-password-required
1767 (setq got-password (not (rmail-have-password)))
1768 (setq supplied-password (rmail-get-remote-password nil)))
1770 (list file "pop" supplied-password got-password)))
1773 (list file nil nil nil))))
1775 (defun rmail-insert-inbox-text (files renamep)
1776 ;; Detect a locked file now, so that we avoid moving mail
1777 ;; out of the real inbox file. (That could scare people.)
1778 (or (memq (file-locked-p buffer-file-name) '(nil t))
1779 (error "RMAIL file %s is locked"
1780 (file-name-nondirectory buffer-file-name)))
1781 (let (file tofile delete-files movemail popmail got-password password)
1783 ;; Handle remote mailbox names specially; don't expand as filenames
1784 ;; in case the userid contains a directory separator.
1785 (setq file (car files))
1786 (let ((url-data (rmail-parse-url file)))
1787 (setq file (nth 0 url-data))
1788 (setq popmail (nth 1 url-data))
1789 (setq password (nth 2 url-data))
1790 (setq got-password (nth 3 url-data)))
1794 (setq file (file-truename
1795 (substitute-in-file-name (expand-file-name file)))))
1796 (setq tofile (expand-file-name
1797 ;; Generate name to move to from inbox name,
1798 ;; in case of multiple inboxes that need moving.
1800 (file-name-nondirectory
1801 (if (memq system-type '(windows-nt cygwin))
1802 ;; cannot have "po:" in file name
1805 ;; Use the directory of this rmail file
1806 ;; because it's a nuisance to use the homedir
1807 ;; if that is on a full disk and this rmail
1809 (file-name-directory
1810 (expand-file-name buffer-file-name))))
1811 ;; Always use movemail to rename the file,
1812 ;; since there can be mailboxes in various directories.
1815 ;; On some systems, /usr/spool/mail/foo is a directory
1816 ;; and the actual inbox is /usr/spool/mail/foo/foo.
1817 (if (file-directory-p file)
1818 (setq file (expand-file-name (user-login-name)
1821 (message "Getting mail from the remote server ..."))
1822 ((and (file-exists-p tofile)
1823 (/= 0 (nth 7 (file-attributes tofile))))
1824 (message "Getting mail from %s..." tofile))
1825 ((and (file-exists-p file)
1826 (/= 0 (nth 7 (file-attributes file))))
1827 (message "Getting mail from %s..." file)))
1828 ;; Set TOFILE if have not already done so, and
1829 ;; rename or copy the file FILE to TOFILE if and as appropriate.
1830 (cond ((not renamep)
1832 ((or (file-exists-p tofile) (and (not popmail)
1833 (not (file-exists-p file))))
1837 (let ((errors (current-buffer)))
1838 (buffer-disable-undo errors)
1841 (list rmail-movemail-program nil errors nil)
1842 (if rmail-preserve-inbox
1845 (if (rmail-movemail-variant-p 'mailutils)
1846 (append (list "--emacs") rmail-movemail-flags)
1847 rmail-movemail-flags)
1849 (if password (list password) nil))))
1850 (apply 'call-process args))
1851 (if (not (buffer-modified-p errors))
1852 ;; No output => movemail won
1855 (subst-char-in-region (point-min) (point-max)
1857 (goto-char (point-max))
1858 (skip-chars-backward " \t")
1859 (delete-region (point) (point-max))
1860 (goto-char (point-min))
1861 (if (looking-at "movemail: ")
1862 (delete-region (point-min) (match-end 0)))
1864 ;; If we just read the password, most likely it is
1865 ;; wrong. Otherwise, see if there is a specific
1866 ;; reason to think that the problem is a wrong passwd.
1867 (if (or got-password
1868 (re-search-forward rmail-remote-password-error
1870 (rmail-set-remote-password nil))
1872 ;; If using Mailutils, remove initial error code
1874 (when (rmail-movemail-variant-p 'mailutils)
1875 (goto-char (point-min))
1876 (when (looking-at "[A-Z][A-Z0-9_]*:")
1877 (delete-region (point-min) (match-end 0))))
1879 (message "movemail: %s"
1880 (buffer-substring (point-min)
1886 ;; At this point, TOFILE contains the name to read:
1887 ;; Either the alternate name (if we renamed)
1888 ;; or the actual inbox (if not renaming).
1889 (if (file-exists-p tofile)
1890 (let ((coding-system-for-read 'no-conversion)
1892 (goto-char (point-max))
1893 (setq size (nth 1 (insert-file-contents tofile)))
1894 (goto-char (point-max))
1895 (or (= (preceding-char) ?\n)
1898 (if (not (and rmail-preserve-inbox (string= file tofile)))
1899 (setq delete-files (cons tofile delete-files)))))
1901 (setq files (cdr files)))
1904 ;; Decode the region specified by FROM and TO by CODING.
1905 ;; If CODING is nil or an invalid coding system, decode by `undecided'.
1906 (defun rmail-decode-region (from to coding)
1907 (if (or (not coding) (not (coding-system-p coding)))
1908 (setq coding 'undecided))
1909 ;; Use -dos decoding, to remove ^M characters left from base64 or
1910 ;; rogue qp-encoded text.
1911 (decode-coding-region from to
1912 (coding-system-change-eol-conversion coding 1))
1913 ;; Don't reveal the fact we used -dos decoding, as users generally
1914 ;; will not expect the RMAIL buffer to use DOS EOL format.
1915 (setq buffer-file-coding-system
1916 (setq last-coding-system-used
1917 (coding-system-change-eol-conversion coding 0))))
1919 ;; the rmail-break-forwarded-messages feature is not implemented
1920 (defun rmail-convert-to-babyl-format ()
1921 (let ((count 0) start
1922 (case-fold-search nil)
1923 (buffer-undo-list t)
1924 (invalid-input-resync
1925 (function (lambda ()
1926 (message "Invalid Babyl format in inbox!")
1928 ;; Try to get back in sync with a real message.
1929 (if (re-search-forward
1930 (concat rmail-mmdf-delim1 "\\|^From") nil t)
1932 (goto-char (point-max)))))))
1933 (goto-char (point-min))
1936 (setq start (point))
1937 (cond ((looking-at "BABYL OPTIONS:") ;Babyl header
1938 (if (search-forward "\n\^_" nil t)
1939 ;; If we find the proper terminator, delete through there.
1940 (delete-region (point-min) (point))
1941 (funcall invalid-input-resync)
1942 (delete-region (point-min) (point))))
1943 ;; Babyl format message
1945 (or (search-forward "\n\^_" nil t)
1946 (funcall invalid-input-resync))
1947 (setq count (1+ count))
1948 ;; Make sure there is no extra white space after the ^_
1949 ;; at the end of the message.
1950 ;; Narrowing will make sure that whatever follows the junk
1951 ;; will be treated properly.
1952 (delete-region (point)
1954 (skip-chars-forward " \t\n")
1956 ;; The following let* form was wrapped in a `save-excursion'
1957 ;; which in one case caused infinite looping, see:
1958 ;; http://lists.gnu.org/archive/html/emacs-devel/2008-01/msg00968.html
1959 ;; Removing that form leaves `point' at the end of the
1960 ;; region decoded by `rmail-decode-region' which should
1967 (if (looking-at "0")
1971 (narrow-to-region (point) (point-max))
1974 (case-fold-search t)
1975 (quoted-printable-header-field-end
1979 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
1981 (base64-header-field-end
1984 ;; Don't try to decode non-text data.
1985 (and (re-search-forward
1986 "^content-type:\\(\n?[\t ]\\)\\(text\\|message\\)/"
1990 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
1992 (if quoted-printable-header-field-end
1995 (mail-unquote-printable-region header-end (point) nil t t)
1996 (message "Malformed MIME quoted-printable message"))
1997 ;; Change "quoted-printable" to "8bit",
1998 ;; to reflect the decoding we just did.
1999 (goto-char quoted-printable-header-field-end)
2000 (delete-region (point) (search-backward ":"))
2002 (if base64-header-field-end
2007 (base64-decode-region (1+ header-end)
2011 ;; Change "base64" to "8bit", to reflect the
2012 ;; decoding we just did.
2013 (goto-char base64-header-field-end)
2014 (delete-region (point) (search-backward ":"))
2015 (insert ": 8bit"))))
2016 (setq last-coding-system-used nil)
2017 (or rmail-enable-mime
2018 (not rmail-enable-multibyte)
2020 (if (and rmail-decode-mime-charset
2023 (search-forward "\n\n" nil t)
2024 (let ((case-fold-search t))
2026 rmail-mime-charset-pattern
2028 (intern (downcase (match-string 1))))))
2029 (rmail-decode-region start (point) mime-charset))))
2030 ;; Add an X-Coding-System: header if we don't have one.
2034 (if (looking-at "0")
2037 (or (save-restriction
2038 (narrow-to-region (point) (point-max))
2040 (goto-char (point-min))
2041 (re-search-forward "^X-Coding-System:" nil t))
2042 (insert "X-Coding-System: "
2043 (symbol-name last-coding-system-used)
2045 (narrow-to-region (point) (point-max))
2046 (and (= 0 (% count 10))
2047 (message "Converting to Babyl format...%d" count)))
2049 ((let ((case-fold-search t))
2050 (looking-at rmail-mmdf-delim1))
2051 (let ((case-fold-search t))
2052 (replace-match "\^L\n0, unseen,,\n*** EOOH ***\n")
2053 (re-search-forward rmail-mmdf-delim2 nil t)
2054 (replace-match "\^_"))
2057 (narrow-to-region start (1- (point)))
2058 (goto-char (point-min))
2059 (while (search-forward "\n\^_" nil t) ; single char "\^_"
2060 (replace-match "\n^_")))) ; 2 chars: "^" and "_"
2061 (setq last-coding-system-used nil)
2062 (or rmail-enable-mime
2063 (not rmail-enable-multibyte)
2064 (decode-coding-region start (point) 'undecided))
2068 (insert "X-Coding-System: "
2069 (symbol-name last-coding-system-used)
2071 (narrow-to-region (point) (point-max))
2072 (setq count (1+ count))
2073 (and (= 0 (% count 10))
2074 (message "Converting to Babyl format...%d" count)))
2076 ((looking-at "^From ")
2077 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
2078 (rmail-nuke-pinhead-header)
2079 ;; If this message has a Content-Length field,
2080 ;; skip to the end of the contents.
2081 (let* ((header-end (save-excursion
2082 (and (re-search-forward "\n\n" nil t)
2084 (case-fold-search t)
2085 (quoted-printable-header-field-end
2088 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
2090 (base64-header-field-end
2092 ;; Don't decode non-text data.
2095 "^content-type:\\(\n?[\t ]\\)\\(text\\|message\\)/"
2099 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
2102 ;; Get the numeric value from the Content-Length field.
2104 ;; Back up to end of prev line,
2105 ;; in case the Content-Length field comes first.
2107 (and (search-forward "\ncontent-length: "
2110 (eol (progn (end-of-line) (point))))
2111 (string-to-number (buffer-substring beg eol)))))))
2113 (if (and (natnump size)
2114 (<= (+ header-end size) (point-max))
2115 ;; Make sure this would put us at a position
2116 ;; that we could continue from.
2118 (goto-char (+ header-end size))
2119 (skip-chars-forward "\n")
2121 (and (looking-at "BABYL OPTIONS:")
2122 (search-forward "\n\^_" nil t))
2123 (and (looking-at "\^L")
2124 (search-forward "\n\^_" nil t))
2125 (let ((case-fold-search t))
2126 (looking-at rmail-mmdf-delim1))
2127 (looking-at "From "))))
2128 (goto-char (+ header-end size))
2129 (message "Ignoring invalid Content-Length field")
2131 (if (let ((case-fold-search nil))
2133 (concat "^[\^_]?\\("
2134 rmail-unix-mail-delimiter
2136 rmail-mmdf-delim1 "\\|"
2137 "^BABYL OPTIONS:\\|"
2138 "\^L\n[01],\\)") nil t))
2139 (goto-char (match-beginning 1))
2140 (goto-char (point-max)))
2141 (setq count (1+ count))
2142 (if quoted-printable-header-field-end
2145 (mail-unquote-printable-region header-end (point) nil t t)
2146 (message "Malformed MIME quoted-printable message"))
2147 ;; Change "quoted-printable" to "8bit",
2148 ;; to reflect the decoding we just did.
2149 (goto-char quoted-printable-header-field-end)
2150 (delete-region (point) (search-backward ":"))
2152 (if base64-header-field-end
2157 (base64-decode-region
2160 ;; Prevent base64-decode-region
2161 ;; from removing newline characters.
2162 (skip-chars-backward "\n\t ")
2166 ;; Change "base64" to "8bit", to reflect the
2167 ;; decoding we just did.
2168 (goto-char base64-header-field-end)
2169 (delete-region (point) (search-backward ":"))
2170 (insert ": 8bit")))))
2174 (narrow-to-region start (point))
2175 (goto-char (point-min))
2176 (while (search-forward "\n\^_" nil t) ; single char
2177 (replace-match "\n^_")))) ; 2 chars: "^" and "_"
2178 ;; This is for malformed messages that don't end in newline.
2179 ;; There shouldn't be any, but some users say occasionally
2181 (or (bolp) (newline))
2183 (setq last-coding-system-used nil)
2184 (or rmail-enable-mime
2185 (not rmail-enable-multibyte)
2187 (if (and rmail-decode-mime-charset
2190 (search-forward "\n\n" nil t)
2191 (let ((case-fold-search t))
2193 rmail-mime-charset-pattern
2195 (intern (downcase (match-string 1))))))
2196 (rmail-decode-region start (point) mime-charset)))
2200 (insert "X-Coding-System: "
2201 (symbol-name last-coding-system-used)
2203 (narrow-to-region (point) (point-max))
2204 (and (= 0 (% count 10))
2205 (message "Converting to Babyl format...%d" count)))
2207 ;; This kludge is because some versions of sendmail.el
2208 ;; insert an extra newline at the beginning that shouldn't
2209 ;; be there. sendmail.el has been fixed, but old versions
2210 ;; may still be in use. -- rms, 7 May 1993.
2211 ((eolp) (delete-char 1))
2212 (t (error "Cannot convert to babyl format")))))
2213 (setq buffer-undo-list nil)
2216 ;; Delete the "From ..." line, creating various other headers with
2217 ;; information from it if they don't already exist. Now puts the
2218 ;; original line into a mail-from: header line for debugging and for
2219 ;; use by the rmail-output function.
2220 (defun rmail-nuke-pinhead-header ()
2223 (let ((start (point))
2226 (search-forward "\n\n")
2228 (goto-char (point-max))
2232 (narrow-to-region start end)
2233 (let ((case-fold-search t))
2235 (setq has-from (search-forward "\nFrom:" nil t))
2237 (setq has-date (and (search-forward "\nDate:" nil t) (point)))
2239 (let ((case-fold-search nil))
2240 (if (re-search-forward (concat "^" rmail-unix-mail-delimiter) nil t)
2244 ;; Keep and reformat the date if we don't
2245 ;; have a Date: field.
2249 "Date: \\2, \\4 \\3 \\9 \\5 "
2251 ;; The timezone could be matched by group 7 or group 10.
2252 ;; If neither of them matched, assume EST, since only
2253 ;; Easterners would be so sloppy.
2254 ;; It's a shame the substitution can't use "\\10".
2256 ((/= (match-beginning 7) (match-end 7)) "\\7")
2257 ((/= (match-beginning 10) (match-end 10))
2258 (buffer-substring (match-beginning 10)
2262 ;; Keep and reformat the sender if we don't
2263 ;; have a From: field.
2269 ;;;; *** Rmail Message Formatting and Header Manipulation ***
2271 (defun rmail-reformat-message (beg end)
2274 (if (/= (following-char) ?0)
2275 (error "Bad format in RMAIL file"))
2276 (let ((inhibit-read-only t)
2277 (delta (- (buffer-size) end)))
2281 (let ((case-fold-search t))
2282 (while (looking-at "Summary-line:\\|Mail-From:")
2284 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*\n")
2285 (delete-region (point)
2286 (progn (forward-line 1) (point))))
2287 (let ((str (buffer-substring (point)
2288 (save-excursion (search-forward "\n\n" end 'move)
2290 (insert str "*** EOOH ***\n")
2291 (narrow-to-region (point) (- (buffer-size) delta)))
2292 (goto-char (point-min))
2293 (if rmail-message-filter (funcall rmail-message-filter))
2294 (if (or rmail-displayed-headers rmail-ignored-headers)
2295 (rmail-clear-headers))))
2297 (defun rmail-clear-headers (&optional ignored-headers)
2298 "Delete all header fields that Rmail should not show.
2299 If the optional argument IGNORED-HEADERS is non-nil,
2300 delete all header fields whose names match that regexp.
2301 Otherwise, if `rmail-displayed-headers' is non-nil,
2302 delete all header fields *except* those whose names match that regexp.
2303 Otherwise, delete all header fields whose names match `rmail-ignored-headers'
2304 unless they also match `rmail-nonignored-headers'."
2305 (when (search-forward "\n\n" nil t)
2307 (let ((case-fold-search t)
2308 (buffer-read-only nil))
2309 (if (and rmail-displayed-headers (null ignored-headers))
2311 (narrow-to-region (point-min) (point))
2313 (goto-char (point-min))
2314 (while (and (not (eobp))
2316 (if (re-search-forward "\n[^ \t]" nil t)
2317 (setq lim (match-beginning 0)
2319 (setq lim nil next (point-max)))))
2321 (re-search-forward rmail-displayed-headers lim t))
2323 (delete-region (point) next))))
2324 (goto-char (point-min)))
2325 (or ignored-headers (setq ignored-headers rmail-ignored-headers))
2327 (narrow-to-region (point-min) (point))
2328 (goto-char (point-min))
2329 (while (and ignored-headers
2330 (re-search-forward ignored-headers nil t))
2332 (if (looking-at rmail-nonignored-headers)
2334 (delete-region (point)
2336 (if (re-search-forward "\n[^ \t]" nil t)
2338 (point-max)))))))))))
2340 (defun rmail-msg-is-pruned ()
2341 (rmail-maybe-set-message-counters)
2343 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2345 (goto-char (point-min))
2347 (= (following-char) ?1))))
2349 (defun rmail-msg-restore-non-pruned-header ()
2350 (let ((old-point (point))
2353 (inhibit-read-only t))
2355 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2356 (goto-char (point-min))
2361 ;; Insert new EOOH line at the proper place.
2363 (let ((case-fold-search t))
2364 (while (looking-at "Summary-Line:\\|Mail-From:")
2366 (insert "*** EOOH ***\n")
2367 (setq new-start (point))
2368 ;; Delete the old reformatted header.
2370 (search-forward "\n*** EOOH ***\n")
2372 (let ((start (point)))
2373 (search-forward "\n\n")
2374 (if (and (<= start old-point)
2375 (<= old-point (point)))
2376 (setq new-point new-start))
2377 (delete-region start (point)))
2378 ;; Narrow to after the new EOOH line.
2379 (narrow-to-region new-start (point-max)))
2381 (goto-char new-point))))
2383 (defun rmail-msg-prune-header ()
2385 (= (point) (point-min))))
2387 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2388 (rmail-reformat-message (point-min) (point-max)))
2390 (goto-char (point-min)))))
2392 (defun rmail-toggle-header (&optional arg)
2393 "Show original message header if pruned header currently shown, or vice versa.
2394 With argument ARG, show the message header pruned if ARG is greater than zero;
2395 otherwise, show it in full."
2397 (let* ((pruned (with-current-buffer rmail-buffer
2398 (rmail-msg-is-pruned)))
2400 (> (prefix-numeric-value arg) 0)
2402 (if (eq pruned prune)
2404 (set-buffer rmail-buffer)
2405 (rmail-maybe-set-message-counters)
2406 (if rmail-enable-mime
2407 (let ((buffer-read-only nil))
2409 (rmail-msg-restore-non-pruned-header)
2410 (rmail-msg-prune-header))
2411 (funcall rmail-show-mime-function))
2412 (let* ((buffer-read-only nil)
2413 (window (get-buffer-window (current-buffer)))
2414 (at-point-min (= (point) (point-min)))
2415 (all-headers-visible (= (window-start window) (point-min)))
2418 (and (not (search-backward "\n\n" nil t))
2421 (re-search-backward "^[-A-Za-z0-9]+:" nil t))
2424 (rmail-count-screen-lines (window-start window) (point))))
2426 (rmail-msg-restore-non-pruned-header)
2427 (rmail-msg-prune-header))
2429 (goto-char (point-min)))
2431 (goto-char (point-min))
2432 (search-forward "\n\n")
2433 (or (re-search-backward
2434 (concat "^" (regexp-quote on-header)) nil t)
2435 (goto-char (point-min))))
2437 (save-selected-window
2438 (select-window window)
2439 (recenter old-screen-line)
2440 (if (and all-headers-visible
2441 (not (= (window-start) (point-min))))
2442 (recenter (- (window-height) 2))))))))
2443 (rmail-highlight-headers))))
2445 (defun rmail-narrow-to-non-pruned-header ()
2446 "Narrow to the whole (original) header of the current message."
2448 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2449 (goto-char (point-min))
2451 (if (= (following-char) ?1)
2454 (setq start (point))
2455 (search-forward "*** EOOH ***\n")
2456 (setq end (match-beginning 0)))
2458 (setq start (point))
2459 (search-forward "\n\n")
2460 (setq end (1- (point))))
2461 (narrow-to-region start end)
2464 ;; Lifted from repos-count-screen-lines.
2465 ;; Return number of screen lines between START and END.
2466 (defun rmail-count-screen-lines (start end)
2469 (narrow-to-region start end)
2470 (goto-char (point-min))
2471 (vertical-motion (- (point-max) (point-min))))))
2473 ;;;; *** Rmail Attributes and Keywords ***
2475 ;; Make a string describing current message's attributes and keywords
2476 ;; and set it up as the name of a minor mode
2477 ;; so it will appear in the mode line.
2478 (defun rmail-display-labels ()
2479 (let ((blurb "") (beg (point-min-marker)) (end (point-max-marker)))
2484 (goto-char (rmail-msgbeg rmail-current-message))
2486 (if (looking-at "[01],")
2488 (narrow-to-region (point) (progn (end-of-line) (point)))
2489 ;; Truly valid BABYL format requires a space before each
2490 ;; attribute or keyword name. Put them in if missing.
2491 (let (buffer-read-only)
2492 (goto-char (point-min))
2493 (while (search-forward "," nil t)
2494 (or (looking-at "[ ,]") (eobp)
2496 (goto-char (point-max))
2497 (if (search-backward ",," nil 'move)
2499 (if (> (point) (1+ (point-min)))
2500 (setq blurb (buffer-substring (+ 1 (point-min)) (point))))
2501 (if (> (- (point-max) (point)) 2)
2505 (buffer-substring (+ (point) 3)
2506 (1- (point-max)))))))))))
2507 ;; Note: we don't use save-restriction because that does not work right
2508 ;; if changes are made outside the saved restriction
2509 ;; before that restriction is restored.
2510 (narrow-to-region beg end)
2511 (set-marker beg nil)
2512 (set-marker end nil)))
2513 (while (string-match " +," blurb)
2514 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
2515 (substring blurb (match-end 0)))))
2516 (while (string-match ", +" blurb)
2517 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
2518 (substring blurb (match-end 0)))))
2519 (setq mode-line-process
2521 rmail-current-message rmail-total-messages blurb))
2522 ;; If rmail-enable-mime is non-nil, we may have to update
2523 ;; `mode-line-process' of rmail-view-buffer too.
2524 (if (and rmail-enable-mime
2525 (not (eq (current-buffer) rmail-view-buffer))
2526 (buffer-live-p rmail-view-buffer))
2527 (let ((mlp mode-line-process))
2528 (with-current-buffer rmail-view-buffer
2529 (setq mode-line-process mlp))))))
2531 ;; Turn an attribute of a message on or off according to STATE.
2532 ;; ATTR is the name of the attribute, as a string.
2533 ;; MSGNUM is message number to change; nil means current message.
2534 (defun rmail-set-attribute (attr state &optional msgnum)
2535 (set-buffer rmail-buffer)
2536 (let ((omax (point-max-marker))
2537 (omin (point-min-marker))
2538 (buffer-read-only nil))
2539 (or msgnum (setq msgnum rmail-current-message))
2544 (goto-char (+ 3 (rmail-msgbeg msgnum)))
2547 (null (search-backward (concat ", " attr ",")
2548 (prog1 (point) (end-of-line)) t)))))
2549 (or (eq curstate (not (not state)))
2551 (delete-region (point) (1- (match-end 0)))
2554 (insert " " attr ","))))
2555 (if (string= attr "deleted")
2556 (rmail-set-message-deleted-p msgnum state)))
2557 ;; Note: we don't use save-restriction because that does not work right
2558 ;; if changes are made outside the saved restriction
2559 ;; before that restriction is restored.
2560 (narrow-to-region omin omax)
2561 (set-marker omin nil)
2562 (set-marker omax nil)
2563 (if (= msgnum rmail-current-message)
2564 (rmail-display-labels))))))
2566 ;; Return t if the attributes/keywords line of msg number MSG
2567 ;; contains a match for the regexp LABELS.
2568 (defun rmail-message-labels-p (msg labels)
2572 (goto-char (rmail-msgbeg msg))
2574 (re-search-backward labels (prog1 (point) (end-of-line)) t))))
2576 ;;;; *** Rmail Message Selection And Support ***
2578 (defun rmail-msgend (n)
2579 (marker-position (aref rmail-message-vector (1+ n))))
2581 (defun rmail-msgbeg (n)
2582 (marker-position (aref rmail-message-vector n)))
2584 (defun rmail-widen-to-current-msgbeg (function)
2585 "Call FUNCTION with point at start of internal data of current message.
2586 Assumes that bounds were previously narrowed to display the message in Rmail.
2587 The bounds are widened enough to move point where desired, then narrowed
2590 FUNCTION may not change the visible text of the message, but it may
2591 change the invisible header text."
2595 (narrow-to-region (rmail-msgbeg rmail-current-message)
2597 (goto-char (point-min))
2599 ;; Note: we don't use save-restriction because that does not work right
2600 ;; if changes are made outside the saved restriction
2601 ;; before that restriction is restored.
2602 (narrow-to-region (rmail-msgbeg rmail-current-message)
2603 (rmail-msgend rmail-current-message)))))
2605 (defun rmail-forget-messages ()
2607 (if (vectorp rmail-message-vector)
2609 (v rmail-message-vector)
2612 (move-marker (aref v i) nil)
2614 (setq rmail-message-vector nil)
2615 (setq rmail-msgref-vector nil)
2616 (setq rmail-deleted-vector nil)))
2618 (defun rmail-maybe-set-message-counters ()
2619 (if (not (and rmail-deleted-vector
2620 rmail-message-vector
2621 rmail-current-message
2622 rmail-total-messages))
2623 (rmail-set-message-counters)))
2625 (defun rmail-count-new-messages (&optional nomsg)
2626 (let* ((case-fold-search nil)
2630 (or nomsg (message "Counting new messages..."))
2631 (goto-char (point-max))
2632 ;; Put at the end of messages-head
2633 ;; the entry for message N+1, which marks
2634 ;; the end of message N. (N = number of messages).
2635 (search-backward "\n\^_")
2637 (setq messages-head (list (point-marker)))
2638 (rmail-set-message-counters-counter (point-min))
2639 (setq rmail-current-message (1+ rmail-total-messages))
2640 (setq rmail-total-messages
2641 (+ rmail-total-messages total-messages))
2642 (setq rmail-message-vector
2643 (vconcat rmail-message-vector (cdr messages-head)))
2644 (aset rmail-message-vector
2645 rmail-current-message (car messages-head))
2646 (setq rmail-deleted-vector
2647 (concat rmail-deleted-vector deleted-head))
2648 (setq rmail-summary-vector
2649 (vconcat rmail-summary-vector (make-vector total-messages nil)))
2650 (setq rmail-msgref-vector
2651 (vconcat rmail-msgref-vector (make-vector total-messages nil)))
2652 ;; Fill in the new elements of rmail-msgref-vector.
2653 (let ((i (1+ (- rmail-total-messages total-messages))))
2654 (while (<= i rmail-total-messages)
2655 (aset rmail-msgref-vector i (list i))
2657 (goto-char (point-min))
2658 (or nomsg (message "Counting new messages...done (%d)" total-messages))))
2660 (defun rmail-set-message-counters ()
2661 (rmail-forget-messages)
2665 (let* ((point-save (point))
2667 (messages-after-point)
2668 (case-fold-search nil)
2671 (message "Counting messages...")
2672 (goto-char (point-max))
2673 ;; Put at the end of messages-head
2674 ;; the entry for message N+1, which marks
2675 ;; the end of message N. (N = number of messages).
2676 (search-backward "\n\^_" nil t)
2677 (if (/= (point) (point-max)) (forward-char 1))
2678 (setq messages-head (list (point-marker)))
2679 (rmail-set-message-counters-counter (min (point) point-save))
2680 (setq messages-after-point total-messages)
2681 (rmail-set-message-counters-counter)
2682 (setq rmail-total-messages total-messages)
2683 (setq rmail-current-message
2685 (max 1 (- total-messages messages-after-point))))
2686 (setq rmail-message-vector
2687 (apply 'vector (cons (point-min-marker) messages-head))
2688 rmail-deleted-vector (concat "0" deleted-head)
2689 rmail-summary-vector (make-vector rmail-total-messages nil)
2690 rmail-msgref-vector (make-vector (1+ rmail-total-messages) nil))
2692 (while (<= i rmail-total-messages)
2693 (aset rmail-msgref-vector i (list i))
2695 (message "Counting messages...done")))))
2697 (defun rmail-set-message-counters-counter (&optional stop)
2698 (let ((start (point))
2700 (while (search-backward "\n\^_\^L" stop t)
2701 ;; Detect messages that have been added with DOS line endings and
2702 ;; convert the line endings for such messages.
2704 (if (looking-at "\n\^_\^L\r\n")
2705 (let ((buffer-read-only nil)
2707 (message "Counting messages...(converting line endings)")
2710 (while (search-backward "\r\n" next t)
2714 (setq messages-head (cons (point-marker) messages-head))
2717 (cons (if (search-backward ", deleted,"
2723 (if (zerop (% (setq total-messages (1+ total-messages)) 20))
2724 (message "Counting messages...%d" total-messages)))))
2726 (defun rmail-beginning-of-message ()
2727 "Show current message starting from the beginning."
2729 (let ((rmail-show-message-hook
2730 (list (function (lambda ()
2731 (goto-char (point-min)))))))
2732 (rmail-show-message rmail-current-message)))
2734 (defun rmail-end-of-message ()
2735 "Show bottom of current message."
2737 (let ((rmail-show-message-hook
2738 (list (function (lambda ()
2739 (goto-char (point-max))
2740 (recenter (1- (window-height))))))))
2741 (rmail-show-message rmail-current-message)))
2743 (defun rmail-unknown-mail-followup-to ()
2744 "Handle a \"Mail-Followup-To\" header field with an unknown mailing list.
2745 Ask the user whether to add that list name to `mail-mailing-lists'."
2747 (rmail-narrow-to-non-pruned-header)
2748 (let ((mail-followup-to (mail-fetch-field "mail-followup-to" nil t)))
2749 (when mail-followup-to
2752 (mail-strip-quoted-names mail-followup-to)
2753 ",[[:space:]]+" t)))
2754 (dolist (addr addresses)
2755 (when (and (not (member addr mail-mailing-lists))
2757 ;; taken from rmailsum.el
2759 (or rmail-user-mail-address-regexp
2761 (regexp-quote (user-login-name))
2764 (or user-mail-address
2765 (concat (user-login-name) "@"
2766 (or mail-host-address
2771 (format "Add `%s' to `mail-mailing-lists'? "
2773 (customize-save-variable 'mail-mailing-lists
2774 (cons addr mail-mailing-lists)))))))))
2776 (defun rmail-show-message (&optional n no-summary)
2777 "Show message number N (prefix argument), counting from start of file.
2778 If summary buffer is currently displayed, update current message there also."
2780 (or (eq major-mode 'rmail-mode)
2781 (switch-to-buffer rmail-buffer))
2782 (rmail-maybe-set-message-counters)
2784 (if (zerop rmail-total-messages)
2785 (progn (narrow-to-region (point-min) (1- (point-max)))
2786 (goto-char (point-min))
2787 (setq mode-line-process nil))
2788 (let (blurb coding-system)
2790 (setq n rmail-current-message)
2793 rmail-current-message 1
2794 blurb "No previous message"))
2795 ((> n rmail-total-messages)
2796 (setq n rmail-total-messages
2797 rmail-current-message rmail-total-messages
2798 blurb "No following message"))
2800 (setq rmail-current-message n))))
2801 (let ((beg (rmail-msgbeg n)))
2805 (let ((end (rmail-msgend n)))
2807 (if (prog1 (= (following-char) ?0)
2809 ;; If there's a Summary-line in the (otherwise empty)
2810 ;; header, we didn't yet get past the EOOH line.
2811 (if (looking-at "^\\*\\*\\* EOOH \\*\\*\\*\n")
2813 (narrow-to-region (point) end))
2815 (search-forward "\n*** EOOH ***\n" end t))
2816 (narrow-to-region beg (point))
2817 (goto-char (point-min))
2818 (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t)
2819 (let ((coding-system (intern (match-string 1))))
2822 (check-coding-system coding-system)
2823 (setq buffer-file-coding-system coding-system))
2825 (setq buffer-file-coding-system nil))))
2826 (setq buffer-file-coding-system nil)))))
2827 ;; Clear the "unseen" attribute when we show a message.
2828 (rmail-set-attribute "unseen" nil)
2829 (let ((end (rmail-msgend n)))
2830 ;; Reformat the header, or else find the reformatted header.
2831 (if (= (following-char) ?0)
2832 (rmail-reformat-message beg end)
2833 (search-forward "\n*** EOOH ***\n" end t)
2834 (narrow-to-region (point) end)))
2835 (goto-char (point-min))
2837 (function (lambda (window)
2838 (if (eq (window-buffer window) (current-buffer))
2839 (set-window-point window (point)))))
2841 (rmail-display-labels)
2842 (if (eq rmail-enable-mime t)
2843 (funcall rmail-show-mime-function)
2844 (setq rmail-view-buffer rmail-buffer))
2845 (when mail-mailing-lists
2846 (rmail-unknown-mail-followup-to))
2847 (rmail-highlight-headers)
2848 (if transient-mark-mode (deactivate-mark))
2849 (run-hooks 'rmail-show-message-hook)
2850 ;; If there is a summary buffer, try to move to this message
2851 ;; in that buffer. But don't complain if this message
2852 ;; is not mentioned in the summary.
2853 ;; Don't do this at all if we were called on behalf
2854 ;; of cursor motion in the summary buffer.
2855 (and (rmail-summary-exists) (not no-summary)
2856 (let ((curr-msg rmail-current-message))
2857 (rmail-select-summary
2858 (rmail-summary-goto-msg curr-msg t t))))
2859 (with-current-buffer rmail-buffer
2862 (message blurb))))))
2864 (defun rmail-redecode-body (coding &optional raw)
2865 "Decode the body of the current message using coding system CODING.
2866 This is useful with mail messages that have malformed or missing
2869 This function assumes that the current message is already decoded
2870 and displayed in the RMAIL buffer, but the coding system used to
2871 decode it was incorrect. It then encodes the message back to its
2872 original form, and decodes it again, using the coding system CODING.
2874 Optional argument RAW, if non-nil, means don't encode the message
2875 before decoding it with the new CODING. This is useful if the current
2876 message text was produced by some function which invokes `insert',
2877 since `insert' leaves unibyte character codes 128 through 255 unconverted
2878 to multibyte. One example of such a situation is when the text was
2879 produced by `base64-decode-region'.
2881 Interactively, invoke the function with a prefix argument to set RAW
2884 Note that if Emacs erroneously auto-detected one of the iso-2022
2885 encodings in the message, this function might fail because the escape
2886 sequences that switch between character sets and also single-shift and
2887 locking-shift codes are impossible to recover. This function is meant
2888 to be used to fix messages encoded with 8-bit encodings, such as
2889 iso-8859, koi8-r, etc."
2890 (interactive "zCoding system for re-decoding this message: ")
2891 (when (not rmail-enable-mime)
2892 (or (eq major-mode 'rmail-mode)
2893 (switch-to-buffer rmail-buffer))
2895 (let ((pruned (rmail-msg-is-pruned))
2896 (raw (or raw current-prefix-arg)))
2898 (let ((msgbeg (rmail-msgbeg rmail-current-message))
2899 (msgend (rmail-msgend rmail-current-message))
2901 ;; We need the message headers pruned (we later restore
2902 ;; the pruned stat to what it was, see the end of
2903 ;; unwind-protect form).
2905 (rmail-toggle-header 1))
2906 (narrow-to-region msgbeg msgend)
2907 (goto-char (point-min))
2908 (when (search-forward "\n*** EOOH ***\n" (point-max) t)
2909 (narrow-to-region msgbeg (point)))
2910 (goto-char (point-min))
2911 (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t)
2912 (let ((old-coding (intern (match-string 1)))
2913 (buffer-read-only nil))
2914 (check-coding-system old-coding)
2915 ;; Make sure the new coding system uses the same EOL
2916 ;; conversion, to prevent ^M characters from popping
2917 ;; up all over the place.
2919 (coding-system-change-eol-conversion
2921 (coding-system-eol-type old-coding)))
2922 ;; If old-coding is `undecided', encode-coding-region
2923 ;; will not encode the text at all. Find a proper
2924 ;; non-trivial encoding to use.
2925 (if (memq (coding-system-base old-coding) '(nil undecided))
2927 (car (find-coding-systems-region msgbeg msgend))))
2928 (setq x-coding-header (point-marker))
2929 (narrow-to-region msgbeg msgend)
2931 ;; If old and new encoding are the same, it
2932 ;; clearly doesn't make sense to encode.
2933 (not (coding-system-equal
2934 (coding-system-base old-coding)
2935 (coding-system-base coding)))
2936 ;; If the body includes only eight-bit-*
2937 ;; characters, encoding might fail, e.g. with
2938 ;; UTF-8, and isn't needed anyway.
2939 (> (length (delq 'ascii
2940 (delq 'eight-bit-graphic
2941 (delq 'eight-bit-control
2942 (find-charset-region
2945 (encode-coding-region (point) msgend old-coding))
2946 (decode-coding-region (point) msgend coding)
2947 (setq last-coding-system-used coding)
2948 ;; Rewrite the coding-system header according
2950 (goto-char x-coding-header)
2951 (delete-region (point)
2955 (insert "X-Coding-System: "
2956 (symbol-name last-coding-system-used))
2957 (set-marker x-coding-header nil)
2958 (rmail-show-message))
2959 (error "No X-Coding-System header found")))
2961 (rmail-toggle-header 0)))))))
2963 ;; Find all occurrences of certain fields, and highlight them.
2964 (defun rmail-highlight-headers ()
2965 ;; Do this only if the system supports faces.
2966 (if (and (fboundp 'internal-find-face)
2967 rmail-highlighted-headers)
2969 (search-forward "\n\n" nil 'move)
2971 (narrow-to-region (point-min) (point))
2972 (let ((case-fold-search t)
2973 (inhibit-read-only t)
2974 ;; Highlight with boldface if that is available.
2975 ;; Otherwise use the `highlight' face.
2976 (face (or rmail-highlight-face
2977 (if (face-differs-from-default-p 'bold)
2979 ;; List of overlays to reuse.
2980 (overlays rmail-overlay-list))
2981 (goto-char (point-min))
2982 (while (re-search-forward rmail-highlighted-headers nil t)
2983 (skip-chars-forward " \t")
2986 (while (progn (forward-line 1)
2987 (looking-at "[ \t]")))
2988 ;; Back up over newline, then trailing spaces or tabs
2990 (while (member (preceding-char) '(? ?\t))
2993 ;; Reuse an overlay we already have.
2995 (setq overlay (car overlays)
2996 overlays (cdr overlays))
2997 (overlay-put overlay 'face face)
2998 (move-overlay overlay beg (point)))
2999 ;; Make a new overlay and add it to
3000 ;; rmail-overlay-list.
3001 (setq overlay (make-overlay beg (point)))
3002 (overlay-put overlay 'face face)
3003 (setq rmail-overlay-list
3004 (cons overlay rmail-overlay-list))))))))))
3006 (defun rmail-auto-file ()
3007 "Automatically move a message into a sub-folder based on criteria.
3008 Called when a new message is displayed."
3009 (if (or (rmail-message-labels-p rmail-current-message "filed")
3010 (not (string= (buffer-file-name)
3011 (expand-file-name rmail-file-name))))
3012 ;; Do nothing if it's already been filed.
3014 ;; Find out some basics (common fields)
3015 (let ((from (mail-fetch-field "from"))
3016 (subj (mail-fetch-field "subject"))
3017 (to (concat (mail-fetch-field "to") "," (mail-fetch-field "cc")))
3018 (d rmail-automatic-folder-directives)
3019 (directive-loop nil)
3022 (setq folder (car (car d))
3023 directive-loop (cdr (car d)))
3024 (while (and (car directive-loop)
3026 ((string= (car directive-loop) "from") from)
3027 ((string= (car directive-loop) "to") to)
3028 ((string= (car directive-loop) "subject") subj)
3029 (t (mail-fetch-field (car directive-loop))))))
3030 (and f (string-match (car (cdr directive-loop)) f))))
3031 (setq directive-loop (cdr (cdr directive-loop))))
3032 ;; If there are no directives left, then it was a complete match.
3033 (if (null directive-loop)
3035 (rmail-delete-forward)
3036 (if (string= "/dev/null" folder)
3037 (rmail-delete-message)
3038 (rmail-output-to-rmail-file folder 1 t)
3040 (setq d (cdr d))))))
3042 (defun rmail-next-message (n)
3043 "Show following message whether deleted or not.
3044 With prefix arg N, moves forward N messages, or backward if N is negative."
3046 (set-buffer rmail-buffer)
3047 (rmail-maybe-set-message-counters)
3048 (rmail-show-message (+ rmail-current-message n)))
3050 (defun rmail-previous-message (n)
3051 "Show previous message whether deleted or not.
3052 With prefix arg N, moves backward N messages, or forward if N is negative."
3054 (rmail-next-message (- n)))
3056 (defun rmail-next-undeleted-message (n)
3057 "Show following non-deleted message.
3058 With prefix arg N, moves forward N non-deleted messages,
3059 or backward if N is negative.
3061 Returns t if a new message is being shown, nil otherwise."
3063 (set-buffer rmail-buffer)
3064 (rmail-maybe-set-message-counters)
3065 (let ((lastwin rmail-current-message)
3066 (current rmail-current-message))
3067 (while (and (> n 0) (< current rmail-total-messages))
3068 (setq current (1+ current))
3069 (if (not (rmail-message-deleted-p current))
3070 (setq lastwin current n (1- n))))
3071 (while (and (< n 0) (> current 1))
3072 (setq current (1- current))
3073 (if (not (rmail-message-deleted-p current))
3074 (setq lastwin current n (1+ n))))
3075 (if (/= lastwin rmail-current-message)
3076 (progn (rmail-show-message lastwin)
3079 (message "No previous nondeleted message"))
3081 (message "No following nondeleted message"))
3084 (defun rmail-previous-undeleted-message (n)
3085 "Show previous non-deleted message.
3086 With prefix argument N, moves backward N non-deleted messages,
3087 or forward if N is negative."
3089 (rmail-next-undeleted-message (- n)))
3091 (defun rmail-first-message ()
3092 "Show first message in file."
3094 (rmail-maybe-set-message-counters)
3095 (rmail-show-message 1))
3097 (defun rmail-last-message ()
3098 "Show last message in file."
3100 (rmail-maybe-set-message-counters)
3101 (rmail-show-message rmail-total-messages))
3103 (defun rmail-what-message ()
3104 (let ((where (point))
3106 (high rmail-total-messages)
3107 (mid (/ rmail-total-messages 2)))
3108 (while (> (- high low) 1)
3109 (if (>= where (rmail-msgbeg mid))
3112 (setq mid (+ low (/ (- high low) 2))))
3113 (if (>= where (rmail-msgbeg high)) high low)))
3115 (defun rmail-message-recipients-p (msg recipients &optional primary-only)
3117 (goto-char (rmail-msgbeg msg))
3118 (search-forward "\n*** EOOH ***\n")
3119 (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
3120 (or (string-match recipients (or (mail-fetch-field "To") ""))
3121 (string-match recipients (or (mail-fetch-field "From") ""))
3122 (if (not primary-only)
3123 (string-match recipients (or (mail-fetch-field "Cc") ""))))))
3125 (defun rmail-message-regexp-p (n regexp)
3126 "Return t, if for message number N, regexp REGEXP matches in the header."
3127 (let ((beg (rmail-msgbeg n))
3128 (end (rmail-msgend n)))
3133 (if (prog1 (= (following-char) ?0)
3135 ;; If there's a Summary-line in the (otherwise empty)
3136 ;; header, we didn't yet get past the EOOH line.
3137 (when (looking-at "^\\*\\*\\* EOOH \\*\\*\\*\n")
3140 (narrow-to-region (point) end))
3145 (search-forward "\n*** EOOH ***\n" end t)
3146 (setq end (1+ (match-beginning 0)))))
3148 (if rmail-enable-mime
3149 (funcall rmail-search-mime-header-function n regexp end)
3150 (re-search-forward regexp end t)))))
3152 (defun rmail-search-message (msg regexp)
3153 "Return non-nil, if for message number MSG, regexp REGEXP matches."
3154 (goto-char (rmail-msgbeg msg))
3155 (if rmail-enable-mime
3156 (funcall rmail-search-mime-message-function msg regexp)
3157 (re-search-forward regexp (rmail-msgend msg) t)))
3159 (defvar rmail-search-last-regexp nil)
3160 (defun rmail-search (regexp &optional n)
3161 "Show message containing next match for REGEXP (but not the current msg).
3162 Prefix argument gives repeat count; negative argument means search
3163 backwards (through earlier messages).
3164 Interactively, empty argument means use same regexp used last time."
3166 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
3168 (concat (if reversep "Reverse " "") "Rmail search (regexp"))
3172 (if rmail-search-last-regexp
3173 (concat ", default "
3174 rmail-search-last-regexp "): ")
3176 (setq regexp (read-string prompt))
3177 (cond ((not (equal regexp ""))
3178 (setq rmail-search-last-regexp regexp))
3179 ((not rmail-search-last-regexp)
3180 (error "No previous Rmail search string")))
3181 (list rmail-search-last-regexp
3182 (prefix-numeric-value current-prefix-arg))))
3184 (message "%sRmail search for %s..."
3185 (if (< n 0) "Reverse " "")
3187 (set-buffer rmail-buffer)
3188 (rmail-maybe-set-message-counters)
3189 (let ((omin (point-min))
3194 (msg rmail-current-message))
3199 ;; Check messages one by one, advancing message number up or down
3200 ;; but searching forward through each message.
3202 (while (and (null win) (> msg 1))
3204 win (rmail-search-message msg regexp)))
3205 (while (and (null win) (< msg rmail-total-messages))
3207 win (rmail-search-message msg regexp))))
3208 (setq n (+ n (if reversep 1 -1)))))
3211 (rmail-show-message msg)
3212 ;; Search forward (if this is a normal search) or backward
3213 ;; (if this is a reverse search) through this message to
3214 ;; position point. This search may fail because REGEXP
3215 ;; was found in the hidden portion of this message. In
3216 ;; that case, move point to the beginning of visible
3220 (goto-char (point-max))
3221 (re-search-backward regexp nil 'move))
3222 (goto-char (point-min))
3223 (re-search-forward regexp nil t))
3224 (message "%sRmail search for %s...done"
3225 (if reversep "Reverse " "")
3228 (narrow-to-region omin omax)
3230 (message "Search failed: %s" regexp)))))
3232 (defun rmail-search-backwards (regexp &optional n)
3233 "Show message containing previous match for REGEXP.
3234 Prefix argument gives repeat count; negative argument means search
3235 forward (through later messages).
3236 Interactively, empty argument means use same regexp used last time."
3238 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
3240 (concat (if reversep "Reverse " "") "Rmail search (regexp"))
3244 (if rmail-search-last-regexp
3245 (concat ", default "
3246 rmail-search-last-regexp "): ")
3248 (setq regexp (read-string prompt))
3249 (cond ((not (equal regexp ""))
3250 (setq rmail-search-last-regexp regexp))
3251 ((not rmail-search-last-regexp)
3252 (error "No previous Rmail search string")))
3253 (list rmail-search-last-regexp
3254 (prefix-numeric-value current-prefix-arg))))
3255 (rmail-search regexp (- (or n 1))))
3257 ;; Show the first message which has the `unseen' attribute.
3258 (defun rmail-first-unseen-message ()
3259 (rmail-maybe-set-message-counters)
3264 (while (and (not found) (<= current rmail-total-messages))
3265 (if (rmail-message-labels-p current ", ?\\(unseen\\),")
3266 (setq found current))
3267 (setq current (1+ current))))
3268 ;; Let the caller show the message.
3270 ;; (rmail-show-message found))
3273 (defun rmail-current-subject ()
3274 "Return the current subject.
3275 The subject is stripped of leading and trailing whitespace, and
3276 of typical reply prefixes such as Re:."
3277 (let ((subject (or (mail-fetch-field "Subject") "")))
3278 (if (string-match "\\`[ \t]+" subject)
3279 (setq subject (substring subject (match-end 0))))
3280 (if (string-match rmail-reply-regexp subject)
3281 (setq subject (substring subject (match-end 0))))
3282 (if (string-match "[ \t]+\\'" subject)
3283 (setq subject (substring subject 0 (match-beginning 0))))
3286 (defun rmail-current-subject-regexp ()
3287 "Return a regular expression matching the current subject.
3288 The regular expression matches the subject header line of
3289 messages about the same subject. The subject itself is stripped
3290 of leading and trailing whitespace, of typical reply prefixes
3291 such as Re: and whitespace within the subject is replaced by a
3292 regular expression matching whitespace in general in order to
3293 take into account that subject header lines may include newlines
3294 and more whitespace. The returned regular expressions contains
3295 `rmail-reply-regexp' and ends with a newline."
3296 (let ((subject (rmail-current-subject)))
3297 ;; If Subject is long, mailers will break it into several lines at
3298 ;; arbitrary places, so replace whitespace with a regexp that will
3299 ;; match any sequence of spaces, TABs, and newlines.
3300 (setq subject (regexp-quote subject))
3302 (replace-regexp-in-string "[ \t\n]+" "[ \t\n]+" subject t t))
3303 ;; Some mailers insert extra spaces after "Subject:", so allow any
3305 (concat "^Subject:[ \t]+"
3306 (if (string= "\\`" (substring rmail-reply-regexp 0 2))
3307 (substring rmail-reply-regexp 2)
3309 subject "[ \t]*\n")))
3311 (defun rmail-next-same-subject (n)
3312 "Go to the next mail message having the same subject header.
3313 With prefix argument N, do this N times.
3314 If N is negative, go backwards instead."
3316 (let ((search-regexp (rmail-current-subject-regexp))
3318 (i rmail-current-message)
3319 (case-fold-search t)
3324 (while (and (/= n 0)
3326 (< i rmail-total-messages)
3329 (while (and (not done)
3331 (< i rmail-total-messages)
3333 (setq i (if forward (1+ i) (1- i)))
3334 (goto-char (rmail-msgbeg i))
3335 (search-forward "\n*** EOOH ***\n")
3336 (let ((beg (point)) end)
3337 (search-forward "\n\n")
3340 (setq done (re-search-forward search-regexp end t))))
3341 (if done (setq found i)))
3342 (setq n (if forward (1- n) (1+ n))))))
3344 (rmail-show-message found)
3345 (error "No %s message with same subject"
3346 (if forward "following" "previous")))))
3348 (defun rmail-previous-same-subject (n)
3349 "Go to the previous mail message having the same subject header.
3350 With prefix argument N, do this N times.
3351 If N is negative, go forwards instead."
3353 (rmail-next-same-subject (- n)))
3355 ;;;; *** Rmail Message Deletion Commands ***
3357 (defun rmail-message-deleted-p (n)
3358 (= (aref rmail-deleted-vector n) ?D))
3360 (defun rmail-set-message-deleted-p (n state)
3361 (aset rmail-deleted-vector n (if state ?D ?\ )))
3363 (defun rmail-delete-message ()
3364 "Delete this message and stay on it."
3366 (rmail-set-attribute "deleted" t)
3367 (run-hooks 'rmail-delete-message-hook))
3369 (defun rmail-undelete-previous-message ()
3370 "Back up to deleted message, select it, and undelete it."
3372 (set-buffer rmail-buffer)
3373 (let ((msg rmail-current-message))
3374 (while (and (> msg 0)
3375 (not (rmail-message-deleted-p msg)))
3376 (setq msg (1- msg)))
3378 (error "No previous deleted message")
3379 (if (/= msg rmail-current-message)
3380 (rmail-show-message msg))
3381 (rmail-set-attribute "deleted" nil)
3382 (if (rmail-summary-exists)
3384 (set-buffer rmail-summary-buffer)
3385 (rmail-summary-mark-undeleted msg)))
3386 (rmail-maybe-display-summary))))
3388 (defun rmail-delete-forward (&optional backward)
3389 "Delete this message and move to next nondeleted one.
3390 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
3391 With prefix argument, delete and move backward.
3393 Returns t if a new message is displayed after the delete, or nil otherwise."
3395 (rmail-set-attribute "deleted" t)
3396 (run-hooks 'rmail-delete-message-hook)
3397 (let ((del-msg rmail-current-message))
3398 (if (rmail-summary-exists)
3399 (rmail-select-summary
3400 (rmail-summary-mark-deleted del-msg)))
3401 (prog1 (rmail-next-undeleted-message (if backward -1 1))
3402 (rmail-maybe-display-summary))))
3404 (defun rmail-delete-backward ()
3405 "Delete this message and move to previous nondeleted one.
3406 Deleted messages stay in the file until the \\[rmail-expunge] command is given."
3408 (rmail-delete-forward t))
3410 ;; Compute the message number a given message would have after expunging.
3411 ;; The present number of the message is OLDNUM.
3412 ;; DELETEDVEC should be rmail-deleted-vector.
3413 ;; The value is nil for a message that would be deleted.
3414 (defun rmail-msg-number-after-expunge (deletedvec oldnum)
3415 (if (or (null oldnum) (= (aref deletedvec oldnum) ?D))
3420 (if (/= (aref deletedvec i) ?D)
3421 (setq newnum (1+ newnum)))
3425 (defun rmail-expunge-confirmed ()
3426 "Return t if deleted message should be expunged. If necessary, ask the user.
3427 See also user-option `rmail-confirm-expunge'."
3428 (set-buffer rmail-buffer)
3429 (or (not (stringp rmail-deleted-vector))
3430 (not (string-match "D" rmail-deleted-vector))
3431 (null rmail-confirm-expunge)
3432 (funcall rmail-confirm-expunge
3433 "Erase deleted messages from Rmail file? ")))
3435 (defun rmail-only-expunge (&optional dont-show)
3436 "Actually erase all deleted messages in the file."
3438 (set-buffer rmail-buffer)
3439 (message "Expunging deleted messages...")
3440 ;; Discard all undo records for this buffer.
3441 (or (eq buffer-undo-list t)
3442 (setq buffer-undo-list nil))
3443 (rmail-maybe-set-message-counters)
3444 (let* ((omax (- (buffer-size) (point-max)))
3445 (omin (- (buffer-size) (point-min)))
3446 (opoint (if (and (> rmail-current-message 0)
3447 (rmail-message-deleted-p rmail-current-message))
3449 (if rmail-enable-mime
3450 (with-current-buffer rmail-view-buffer
3451 (- (point)(point-min)))
3452 (- (point) (point-min)))))
3453 (messages-head (cons (aref rmail-message-vector 0) nil))
3454 (messages-tail messages-head)
3455 ;; Don't make any undo records for the expunging.
3456 (buffer-undo-list t)
3461 (goto-char (point-min))
3464 (total rmail-total-messages)
3465 (new-message-number rmail-current-message)
3467 (new-msgref (list (list 0)))
3468 (rmailbuf (current-buffer))
3469 (buffer-read-only nil)
3470 (messages rmail-message-vector)
3471 (deleted rmail-deleted-vector)
3472 (summary rmail-summary-vector))
3473 (setq rmail-total-messages nil
3474 rmail-current-message nil
3475 rmail-message-vector nil
3476 rmail-deleted-vector nil
3477 rmail-summary-vector nil)
3479 (while (<= number total)
3480 (if (= (aref deleted number) ?D)
3483 (marker-position (aref messages number))
3484 (marker-position (aref messages (1+ number))))
3485 (move-marker (aref messages number) nil)
3486 (if (> new-message-number counter)
3487 (setq new-message-number (1- new-message-number))))
3488 (setq counter (1+ counter))
3490 (setcdr messages-tail
3491 (cons (aref messages number) nil)))
3493 (cons (if (= counter number) (aref summary (1- number)))
3496 (cons (aref rmail-msgref-vector number)
3498 (setcar (car new-msgref) counter))
3499 (if (zerop (% (setq number (1+ number)) 20))
3500 (message "Expunging deleted messages...%d" number)))
3502 (setcdr messages-tail
3503 (cons (aref messages number) nil)))
3504 (setq rmail-current-message new-message-number
3505 rmail-total-messages counter
3506 rmail-message-vector (apply 'vector messages-head)
3507 rmail-deleted-vector (make-string (1+ counter) ?\ )
3508 rmail-summary-vector (vconcat (nreverse new-summary))
3509 rmail-msgref-vector (apply 'vector (nreverse new-msgref))
3511 (message "Expunging deleted messages...done")
3513 (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
3516 (if (zerop rmail-current-message) 1 nil)))
3517 (if rmail-enable-mime
3518 (goto-char (+ (point-min) opoint))
3519 (goto-char (+ (point) opoint))))))
3521 (defun rmail-expunge ()
3522 "Erase deleted messages from Rmail file and summary buffer."
3524 (when (rmail-expunge-confirmed)
3525 (rmail-only-expunge)
3526 (if (rmail-summary-exists)
3527 (rmail-select-summary (rmail-update-summary)))))
3529 ;;;; *** Rmail Mailing Commands ***
3531 (defun rmail-start-mail (&optional noerase to subject in-reply-to cc
3532 replybuffer sendactions same-window others)
3535 (setq yank-action (list 'insert-buffer replybuffer)))
3536 (setq others (cons (cons "cc" cc) others))
3537 (setq others (cons (cons "in-reply-to" in-reply-to) others))
3539 (compose-mail to subject others
3541 yank-action sendactions)
3542 (if rmail-mail-new-frame
3544 (compose-mail to subject others
3545 noerase 'switch-to-buffer-other-frame
3546 yank-action sendactions)
3547 ;; This is not a standard frame parameter;
3548 ;; nothing except sendmail.el looks at it.
3549 (modify-frame-parameters (selected-frame)
3550 '((mail-dedicated-frame . t))))
3551 (compose-mail to subject others
3552 noerase 'switch-to-buffer-other-window
3553 yank-action sendactions)))))
3555 (defun rmail-mail ()
3556 "Send mail in another window.
3557 While composing the message, use \\[mail-yank-original] to yank the
3558 original message into it."
3560 (rmail-start-mail nil nil nil nil nil rmail-view-buffer))
3562 (defun rmail-continue ()
3563 "Continue composing outgoing message previously being composed."
3565 (rmail-start-mail t))
3567 (defun rmail-reply (just-sender)
3568 "Reply to the current message.
3569 Normally include CC: to all other recipients of original message;
3570 prefix argument means ignore them. While composing the reply,
3571 use \\[mail-yank-original] to yank the original message into it."
3573 (let (from reply-to cc subject date to message-id references
3574 resent-to resent-cc resent-reply-to
3575 (msgnum rmail-current-message))
3578 (if rmail-enable-mime
3580 (goto-char (point-min))
3581 (if (search-forward "\n\n" nil 'move)
3582 (1+ (match-beginning 0))
3585 (goto-char (rmail-msgbeg rmail-current-message))
3587 (if (= (following-char) ?0)
3589 (progn (forward-line 2)
3591 (progn (search-forward "\n\n" (rmail-msgend rmail-current-message)
3594 (narrow-to-region (point)
3595 (progn (search-forward "\n*** EOOH ***\n")
3596 (beginning-of-line) (point)))))
3597 (setq from (mail-fetch-field "from")
3598 reply-to (or (mail-fetch-field "mail-reply-to" nil t)
3599 (mail-fetch-field "reply-to" nil t)
3601 subject (mail-fetch-field "subject")
3602 date (mail-fetch-field "date")
3603 message-id (mail-fetch-field "message-id")
3604 references (mail-fetch-field "references" nil nil t)
3605 resent-reply-to (mail-fetch-field "resent-reply-to" nil t)
3606 resent-cc (and (not just-sender)
3607 (mail-fetch-field "resent-cc" nil t))
3608 resent-to (or (mail-fetch-field "resent-to" nil t) "")
3609 ;;; resent-subject (mail-fetch-field "resent-subject")
3610 ;;; resent-date (mail-fetch-field "resent-date")
3611 ;;; resent-message-id (mail-fetch-field "resent-message-id")
3614 (if (mail-fetch-field "mail-followup-to" nil t)
3615 ;; If this header field is present, use it instead of the To and CC fields.
3616 (setq to (mail-fetch-field "mail-followup-to" nil t))
3617 (setq cc (or (mail-fetch-field "cc" nil t) "")
3618 to (or (mail-fetch-field "to" nil t) ""))))
3622 ;; Merge the resent-to and resent-cc into the to and cc.
3623 (if (and resent-to (not (equal resent-to "")))
3624 (if (not (equal to ""))
3625 (setq to (concat to ", " resent-to))
3626 (setq to resent-to)))
3627 (if (and resent-cc (not (equal resent-cc "")))
3628 (if (not (equal cc ""))
3629 (setq cc (concat cc ", " resent-cc))
3630 (setq cc resent-cc)))
3631 ;; Add `Re: ' to subject if not there already.
3632 (and (stringp subject)
3634 (concat rmail-reply-prefix
3635 (if (let ((case-fold-search t))
3636 (string-match rmail-reply-regexp subject))
3637 (substring subject (match-end 0))
3641 ;; Using mail-strip-quoted-names is undesirable with newer mailers
3642 ;; since they can handle the names unstripped.
3643 ;; I don't know whether there are other mailers that still
3644 ;; need the names to be stripped.
3645 ;;; (mail-strip-quoted-names reply-to)
3646 ;; Remove unwanted names from reply-to, since Mail-Followup-To
3647 ;; header causes all the names in it to wind up in reply-to, not
3648 ;; in cc. But if what's left is an empty list, use the original.
3649 (let* ((reply-to-list (rmail-dont-reply-to reply-to)))
3650 (if (string= reply-to-list "") reply-to reply-to-list))
3652 (rmail-make-in-reply-to-field from date message-id)
3655 ;; mail-strip-quoted-names is NOT necessary for rmail-dont-reply-to
3657 (let* ((cc-list (rmail-dont-reply-to
3658 (mail-strip-quoted-names
3659 (if (null cc) to (concat to ", " cc))))))
3660 (if (string= cc-list "") nil cc-list)))
3662 (list (list 'rmail-mark-message
3664 (with-current-buffer rmail-buffer
3665 (aref rmail-msgref-vector msgnum))
3668 (list (cons "References" (concat (mapconcat 'identity references " ")
3669 " " message-id))))))
3671 (defun rmail-mark-message (buffer msgnum-list attribute)
3672 "Give BUFFER's message number in MSGNUM-LIST the attribute ATTRIBUTE.
3673 This is use in the send-actions for message buffers.
3674 MSGNUM-LIST is a list of the form (MSGNUM)
3675 which is an element of rmail-msgref-vector."
3678 (if (car msgnum-list)
3679 (rmail-set-attribute attribute t (car msgnum-list)))))
3681 (defun rmail-make-in-reply-to-field (from date message-id)
3688 (let ((tem (car (rfc822-addresses from))))
3692 (regexp-quote (if (string-match "@[^@]*\\'" tem)
3694 (match-beginning 0))
3697 ;; missing From, or Message-ID is sufficiently informative
3699 (concat message-id " (" tem ")"))
3700 ;; Copy TEM, discarding text properties.
3701 (setq tem (copy-sequence tem))
3702 (set-text-properties 0 (length tem) nil tem)
3703 (setq tem (copy-sequence tem))
3704 ;; Use prin1 to fake RFC822 quoting
3705 (let ((field (prin1-to-string tem)))
3707 (concat field "'s message of " date)
3709 ((let* ((foo "[^][\000-\037()<>@,;:\\\" ]+")
3710 (bar "[^][\000-\037()<>@,;:\\\"]+"))
3711 ;; These strings both match all non-ASCII characters.
3712 (or (string-match (concat "\\`[ \t]*\\(" bar
3713 "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
3714 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
3716 (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
3717 bar "\\))[ \t]*\\'")
3718 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
3720 (let ((start (match-beginning 1))
3721 (end (match-end 1)))
3722 ;; Trim whitespace which above regexp match allows
3723 (while (and (< start end)
3724 (memq (aref from start) '(?\t ?\ )))
3725 (setq start (1+ start)))
3726 (while (and (< start end)
3727 (memq (aref from (1- end)) '(?\t ?\ )))
3728 (setq end (1- end)))
3729 (let ((field (substring from start end)))
3730 (if date (setq field (concat "message from " field " on " date)))
3732 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
3733 (concat message-id " (" field ")")
3736 ;; If we can't kludge it simply, do it correctly
3737 (let ((mail-use-rfc822 t))
3738 (rmail-make-in-reply-to-field from date message-id)))))
3740 (defun rmail-forward (resend)
3741 "Forward the current message to another user.
3742 With prefix argument, \"resend\" the message instead of forwarding it;
3743 see the documentation of `rmail-resend'."
3746 (call-interactively 'rmail-resend)
3747 (let ((forward-buffer rmail-buffer)
3748 (msgnum rmail-current-message)
3749 (subject (concat "["
3750 (let ((from (or (mail-fetch-field "From")
3751 (mail-fetch-field ">From"))))
3753 (concat (mail-strip-quoted-names from) ": ")
3755 (or (mail-fetch-field "Subject") "")
3757 (if (rmail-start-mail
3758 nil nil subject nil nil nil
3759 (list (list 'rmail-mark-message
3761 (with-current-buffer rmail-buffer
3762 (aref rmail-msgref-vector msgnum))
3764 ;; If only one window, use it for the mail buffer.
3765 ;; Otherwise, use another window for the mail buffer
3766 ;; so that the Rmail buffer remains visible
3767 ;; and sending the mail will get back to it.
3768 (and (not rmail-mail-new-frame) (one-window-p t)))
3769 ;; The mail buffer is now current.
3771 ;; Insert after header separator--before signature if any.
3772 (goto-char (mail-text-start))
3773 (if (or rmail-enable-mime rmail-enable-mime-composing)
3774 (funcall rmail-insert-mime-forwarded-message-function
3776 (insert "------- Start of forwarded message -------\n")
3777 ;; Quote lines with `- ' if they start with `-'.
3778 (let ((beg (point)) end)
3779 (setq end (point-marker))
3780 (set-marker-insertion-type end t)
3781 (insert-buffer-substring forward-buffer)
3783 (while (re-search-forward "^-" end t)
3788 (skip-chars-backward "\n")
3791 (delete-region (point) end)
3792 (set-marker end nil))
3793 (insert "------- End of forwarded message -------\n"))
3796 (defun rmail-resend (address &optional from comment mail-alias-file)
3797 "Resend current message to ADDRESSES.
3798 ADDRESSES should be a single address, a string consisting of several
3799 addresses separated by commas, or a list of addresses.
3801 Optional FROM is the address to resend the message from, and
3802 defaults from the value of `user-mail-address'.
3803 Optional COMMENT is a string to insert as a comment in the resent message.
3804 Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
3805 typically for purposes of moderating a list."
3806 (interactive "sResend to: ")
3808 (require 'mailalias)
3809 (unless (or (eq rmail-view-buffer (current-buffer))
3810 (eq rmail-buffer (current-buffer)))
3811 (error "Not an Rmail buffer"))
3812 (if (not from) (setq from user-mail-address))
3813 (let ((tembuf (generate-new-buffer " sendmail temp"))
3814 (case-fold-search nil)
3815 (mail-personal-alias-file
3816 (or mail-alias-file mail-personal-alias-file))
3817 (mailbuf rmail-buffer))
3819 (with-current-buffer tembuf
3820 ;;>> Copy message into temp buffer
3821 (if rmail-enable-mime
3822 (funcall rmail-insert-mime-resent-message-function mailbuf)
3823 (insert-buffer-substring mailbuf))
3824 (goto-char (point-min))
3825 ;; Delete any Sender field, since that's not specifiable.
3826 ; Only delete Sender fields in the actual header.
3827 (re-search-forward "^$" nil 'move)
3828 ; Using "while" here rather than "if" because some buggy mail
3829 ; software may have inserted multiple Sender fields.
3830 (while (re-search-backward "^Sender:" nil t)
3834 (while (looking-at "[ \t]")
3836 (delete-region beg (point))))
3837 ; Go back to the beginning of the buffer so the Resent- fields
3838 ; are inserted there.
3839 (goto-char (point-min))
3840 ;;>> Insert resent-from:
3841 (insert "Resent-From: " from "\n")
3842 (insert "Resent-Date: " (mail-rfc822-date) "\n")
3843 ;;>> Insert resent-to: and bcc if need be.
3844 (let ((before (point)))
3846 (insert "Resent-Bcc: " (user-login-name) "\n"))
3847 (insert "Resent-To: " (if (stringp address)
3849 (mapconcat 'identity address ",\n\t"))
3851 ;; Expand abbrevs in the recipients.
3853 (if (featurep 'mailabbrev)
3854 (let ((end (point-marker))
3855 (local-abbrev-table mail-abbrevs)
3856 (old-syntax-table (syntax-table)))
3857 (if (and (not (vectorp mail-abbrevs))
3858 (file-exists-p mail-personal-alias-file))
3859 (build-mail-abbrevs))
3860 (unless mail-abbrev-syntax-table
3861 (mail-abbrev-make-syntax-table))
3862 (set-syntax-table mail-abbrev-syntax-table)
3864 (while (and (< (point) end)
3865 (progn (forward-word 1)
3868 (set-syntax-table old-syntax-table))
3869 (expand-mail-aliases before (point)))))
3870 ;;>> Set up comment, if any.
3871 (if (and (sequencep comment) (not (zerop (length comment))))
3872 (let ((before (point))
3875 (or (eolp) (insert "\n"))
3876 (setq after (point))
3878 (while (< (point) after)
3879 (insert "Resent-Comment: ")
3881 ;; Don't expand aliases in the destination fields
3882 ;; of the original message.
3884 (funcall send-mail-function)))
3885 (kill-buffer tembuf))
3886 (with-current-buffer rmail-buffer
3887 (rmail-set-attribute "resent" t rmail-current-message))))
3889 (defvar mail-unsent-separator
3890 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
3891 "^ *---+ +Returned message +---+ *$\\|"
3892 "^ *---+ *Returned mail follows *---+ *$\\|"
3893 "^Start of returned message$\\|"
3894 "^---+ Below this line is a copy of the message.$\\|"
3895 "^ *---+ +Original message +---+ *$\\|"
3896 "^ *--+ +begin message +--+ *$\\|"
3897 "^ *---+ +Original message follows +---+ *$\\|"
3898 "^ *---+ +Your message follows +---+ *$\\|"
3899 "^|? *---+ +Message text follows: +---+ *|?$\\|"
3900 "^ *---+ +This is a copy of \\w+ message, including all the headers.*---+ *$")
3901 "A regexp that matches the separator before the text of a failed message.")
3903 (defvar mail-mime-unsent-header "^Content-Type: message/rfc822 *$"
3904 "A regexp that matches the header of a MIME body part with a failed message.")
3906 (defun rmail-retry-failure ()
3907 "Edit a mail message which is based on the contents of the current message.
3908 For a message rejected by the mail system, extract the interesting headers and
3909 the body of the original message.
3910 If the failed message is a MIME multipart message, it is searched for a
3911 body part with a header which matches the variable `mail-mime-unsent-header'.
3912 Otherwise, the variable `mail-unsent-separator' should match the string that
3913 delimits the returned original message.
3914 The variable `rmail-retry-ignored-headers' is a regular expression
3915 specifying headers which should not be copied into the new message."
3917 (require 'mail-utils)
3918 (let ((rmail-this-buffer (current-buffer))
3919 (msgnum rmail-current-message)
3920 bounce-start bounce-end bounce-indent resending
3921 ;; Fetch any content-type header in current message
3922 ;; Must search thru the whole unpruned header.
3926 (rmail-narrow-to-non-pruned-header)
3927 (mail-fetch-field "Content-Type") ))))
3929 (goto-char (point-min))
3930 (let ((case-fold-search t))
3931 (if (and content-type
3933 ";[\n\t ]*boundary=\"?\\([-0-9a-z'()+_,./:=? ]+\\)\"?"
3935 ;; Handle a MIME multipart bounce message.
3938 (substring content-type (match-beginning 1)
3940 (unless (re-search-forward mail-mime-unsent-header nil t)
3941 (error "Cannot find beginning of header in failed message"))
3942 (unless (search-forward "\n\n" nil t)
3943 (error "Cannot find start of Mime data in failed message"))
3944 (setq bounce-start (point))
3945 (if (search-forward codestring nil t)
3946 (setq bounce-end (match-beginning 0))
3947 (setq bounce-end (point-max))))
3949 (or (re-search-forward mail-unsent-separator nil t)
3950 (error "Cannot parse this as a failure message"))
3951 (skip-chars-forward "\n")
3952 ;; Support a style of failure message in which the original
3953 ;; message is indented, and included within lines saying
3954 ;; `Start of returned message' and `End of returned message'.
3955 (if (looking-at " +Received:")
3957 (setq bounce-start (point))
3958 (skip-chars-forward " ")
3959 (setq bounce-indent (- (current-column)))
3960 (goto-char (point-max))
3961 (re-search-backward "^End of returned message$" nil t)
3962 (setq bounce-end (point)))
3963 ;; One message contained a few random lines before
3964 ;; the old message header. The first line of the
3965 ;; message started with two hyphens. A blank line
3966 ;; followed these random lines. The same line
3967 ;; beginning with two hyphens was possibly marking
3968 ;; the end of the message.
3969 (if (looking-at "^--")
3970 (let ((boundary (buffer-substring-no-properties
3972 (progn (end-of-line) (point)))))
3973 (search-forward "\n\n")
3974 (skip-chars-forward "\n")
3975 (setq bounce-start (point))
3976 (goto-char (point-max))
3977 (search-backward (concat "\n\n" boundary) bounce-start t)
3978 (setq bounce-end (point)))
3979 (setq bounce-start (point)
3980 bounce-end (point-max)))
3981 (unless (search-forward "\n\n" nil t)
3982 (error "Cannot find end of header in failed message"))))))
3983 ;; We have found the message that bounced, within the current message.
3984 ;; Now start sending new message; default header fields from original.
3985 ;; Turn off the usual actions for initializing the message body
3986 ;; because we want to get only the text from the failure message.
3987 (let (mail-signature mail-setup-hook)
3988 (if (rmail-start-mail nil nil nil nil nil rmail-this-buffer
3989 (list (list 'rmail-mark-message
3991 (aref rmail-msgref-vector msgnum)
3993 ;; Insert original text as initial text of new draft message.
3994 ;; Bind inhibit-read-only since the header delimiter
3995 ;; of the previous message was probably read-only.
3996 (let ((inhibit-read-only t)
3997 rmail-displayed-headers
3998 rmail-ignored-headers)
4000 (insert-buffer-substring rmail-this-buffer
4001 bounce-start bounce-end)
4002 (goto-char (point-min))
4004 (indent-rigidly (point-min) (point-max) bounce-indent))
4005 (rmail-clear-headers rmail-retry-ignored-headers)
4006 (rmail-clear-headers "^sender:\\|^return-path:\\|^received:")
4007 (mail-sendmail-delimit-header)
4009 (narrow-to-region (point-min) (mail-header-end))
4010 (setq resending (mail-fetch-field "resent-to"))
4013 (insert "Resent-Bcc: " (user-login-name) "\n")
4014 (insert "BCC: " (user-login-name) "\n"))))
4015 (goto-char (point-min))
4016 (mail-position-on-field (if resending "Resent-To" "To") t))))))
4018 (defun rmail-summary-exists ()
4019 "Non-nil if in an RMAIL buffer and an associated summary buffer exists.
4020 In fact, the non-nil value returned is the summary buffer itself."
4021 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
4022 rmail-summary-buffer))
4024 (defun rmail-summary-displayed ()
4025 "t if in RMAIL buffer and an associated summary buffer is displayed."
4026 (and rmail-summary-buffer (get-buffer-window rmail-summary-buffer)))
4028 (defcustom rmail-redisplay-summary nil
4029 "*Non-nil means Rmail should show the summary when it changes.
4030 This has an effect only if a summary buffer exists."
4032 :group 'rmail-summary)
4034 (defcustom rmail-summary-window-size nil
4035 "*Non-nil means specify the height for an Rmail summary window."
4036 :type '(choice (const :tag "Disabled" nil) integer)
4037 :group 'rmail-summary)
4039 ;; Put the summary buffer back on the screen, if user wants that.
4040 (defun rmail-maybe-display-summary ()
4041 (let ((selected (selected-window))
4043 ;; If requested, make sure the summary is displayed.
4044 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
4045 rmail-redisplay-summary
4046 (if (get-buffer-window rmail-summary-buffer 0)
4047 ;; It's already in some frame; show that one.
4048 (let ((frame (window-frame
4049 (get-buffer-window rmail-summary-buffer 0))))
4050 (make-frame-visible frame)
4051 (raise-frame frame))
4052 (display-buffer rmail-summary-buffer)))
4053 ;; If requested, set the height of the summary window.
4054 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
4055 rmail-summary-window-size
4056 (setq window (get-buffer-window rmail-summary-buffer))
4057 ;; Don't try to change the size if just one window in frame.
4058 (not (eq window (frame-root-window (window-frame window))))
4061 (select-window window)
4062 (enlarge-window (- rmail-summary-window-size (window-height))))
4063 (select-window selected)))))
4065 ;;;; *** Rmail Local Fontification ***
4067 (defun rmail-fontify-buffer-function ()
4068 ;; This function's symbol is bound to font-lock-fontify-buffer-function.
4069 (add-hook 'rmail-show-message-hook 'rmail-fontify-message nil t)
4070 ;; If we're already showing a message, fontify it now.
4071 (if rmail-current-message (rmail-fontify-message))
4072 ;; Prevent Font Lock mode from kicking in.
4073 (setq font-lock-fontified t))
4075 (defun rmail-unfontify-buffer-function ()
4076 ;; This function's symbol is bound to font-lock-fontify-unbuffer-function.
4077 (let ((modified (buffer-modified-p))
4078 (buffer-undo-list t) (inhibit-read-only t)
4079 before-change-functions after-change-functions
4080 buffer-file-name buffer-file-truename)
4083 (remove-hook 'rmail-show-message-hook 'rmail-fontify-message t)
4084 (remove-text-properties (point-min) (point-max) '(rmail-fontified nil))
4085 (font-lock-default-unfontify-buffer)
4086 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))
4088 (defun rmail-fontify-message ()
4089 ;; Fontify the current message if it is not already fontified.
4090 (if (text-property-any (point-min) (point-max) 'rmail-fontified nil)
4091 (let ((modified (buffer-modified-p))
4092 (buffer-undo-list t) (inhibit-read-only t)
4093 before-change-functions after-change-functions
4094 buffer-file-name buffer-file-truename)
4097 (add-text-properties (point-min) (point-max) '(rmail-fontified t))
4098 (font-lock-fontify-region (point-min) (point-max))
4099 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))))
4101 ;;; Speedbar support for RMAIL files.
4102 (eval-when-compile (require 'speedbar))
4104 (defvar rmail-speedbar-match-folder-regexp "^[A-Z0-9]+\\(\\.[A-Z0-9]+\\)?$"
4105 "*This regex is used to match folder names to be displayed in speedbar.
4106 Enabling this will permit speedbar to display your folders for easy
4107 browsing, and moving of messages.")
4109 (defvar rmail-speedbar-last-user nil
4110 "The last user to be displayed in the speedbar.")
4112 (defvar rmail-speedbar-key-map nil
4113 "Keymap used when in rmail display mode.")
4115 (defun rmail-install-speedbar-variables ()
4116 "Install those variables used by speedbar to enhance rmail."
4117 (if rmail-speedbar-key-map
4119 (setq rmail-speedbar-key-map (speedbar-make-specialized-keymap))
4121 (define-key rmail-speedbar-key-map "e" 'speedbar-edit-line)
4122 (define-key rmail-speedbar-key-map "r" 'speedbar-edit-line)
4123 (define-key rmail-speedbar-key-map "\C-m" 'speedbar-edit-line)
4124 (define-key rmail-speedbar-key-map "M"
4125 'rmail-speedbar-move-message-to-folder-on-line)))
4127 (defvar rmail-speedbar-menu-items
4128 '(["Read Folder" speedbar-edit-line t]
4129 ["Move message to folder" rmail-speedbar-move-message-to-folder-on-line
4130 (save-excursion (beginning-of-line)
4131 (looking-at "<M> "))])
4132 "Additional menu-items to add to speedbar frame.")
4134 ;; Make sure our special speedbar major mode is loaded
4135 (if (featurep 'speedbar)
4136 (rmail-install-speedbar-variables)
4137 (add-hook 'speedbar-load-hook 'rmail-install-speedbar-variables))
4139 (defun rmail-speedbar-buttons (buffer)
4140 "Create buttons for BUFFER containing rmail messages.
4141 Click on the address under Reply to: to reply to this person.
4142 Under Folders: Click a name to read it, or on the <M> to move the
4143 current message into that RMAIL folder."
4147 (goto-char (point-min))
4148 (if (not (re-search-forward "^Reply-To: " nil t))
4149 (if (not (re-search-forward "^From:? " nil t))
4153 (setq from (buffer-substring (point) (save-excursion
4156 (goto-char (point-min))
4157 (if (and (looking-at "Reply to:")
4158 (equal from rmail-speedbar-last-user))
4160 (setq rmail-speedbar-last-user from)
4162 (insert "Reply To:\n")
4164 (speedbar-insert-button from 'speedbar-directory-face 'highlight
4165 'rmail-speedbar-button 'rmail-reply))
4166 (insert "Folders:\n")
4167 (let* ((case-fold-search nil)
4168 (df (directory-files (save-excursion (set-buffer buffer)
4170 nil rmail-speedbar-match-folder-regexp)))
4172 (speedbar-insert-button "<M>" 'speedbar-button-face 'highlight
4173 'rmail-speedbar-move-message (car df))
4174 (speedbar-insert-button (car df) 'speedbar-file-face 'highlight
4175 'rmail-speedbar-find-file nil t)
4176 (setq df (cdr df)))))))
4178 (defun rmail-speedbar-button (text token indent)
4179 "Execute an rmail command specified by TEXT.
4180 The command used is TOKEN. INDENT is not used."
4181 (speedbar-with-attached-buffer
4184 (defun rmail-speedbar-find-file (text token indent)
4185 "Load in the rmail file TEXT.
4186 TOKEN and INDENT are not used."
4187 (speedbar-with-attached-buffer
4188 (message "Loading in RMAIL file %s..." text)
4191 (defun rmail-speedbar-move-message-to-folder-on-line ()
4192 "If the current line is a folder, move current message to it."
4196 (if (re-search-forward "<M> " (save-excursion (end-of-line) (point)) t)
4199 (speedbar-do-function-pointer)))))
4201 (defun rmail-speedbar-move-message (text token indent)
4202 "From button TEXT, copy current message to the rmail file specified by TOKEN.
4203 TEXT and INDENT are not used."
4204 (speedbar-with-attached-buffer
4205 (message "Moving message to %s" token)
4206 (rmail-output-to-rmail-file token)))
4208 ; Functions for setting, getting and encoding the POP password.
4209 ; The password is encoded to prevent it from being easily accessible
4210 ; to "prying eyes." Obviously, this encoding isn't "real security,"
4211 ; nor is it meant to be.
4214 (defun rmail-set-remote-password (password)
4215 "Set PASSWORD to be used for retrieving mail from a POP or IMAP server."
4216 (interactive "sPassword: ")
4218 (setq rmail-encoded-remote-password
4219 (rmail-encode-string password (emacs-pid)))
4220 (setq rmail-remote-password nil)
4221 (setq rmail-encoded-remote-password nil)))
4223 (defun rmail-get-remote-password (imap)
4224 "Get the password for retrieving mail from a POP or IMAP server. If none
4225 has been set, then prompt the user for one."
4226 (when (not rmail-encoded-remote-password)
4227 (if (not rmail-remote-password)
4228 (setq rmail-remote-password
4229 (read-passwd (if imap
4231 "POP password: "))))
4232 (rmail-set-remote-password rmail-remote-password)
4233 (setq rmail-remote-password nil))
4234 (rmail-encode-string rmail-encoded-remote-password (emacs-pid)))
4236 (defun rmail-have-password ()
4237 (or rmail-remote-password rmail-encoded-remote-password))
4239 (defun rmail-encode-string (string mask)
4240 "Encode STRING with integer MASK, by taking the exclusive OR of the
4241 lowest byte in the mask with the first character of string, the
4242 second-lowest-byte with the second character of the string, etc.,
4243 restarting at the lowest byte of the mask whenever it runs out.
4244 Returns the encoded string. Calling the function again with an
4245 encoded string (and the same mask) will decode the string."
4246 (setq mask (abs mask)) ; doesn't work if negative
4247 (let* ((string-vector (string-to-vector string)) (i 0)
4248 (len (length string-vector)) (curmask mask) charmask)
4251 (setq curmask mask))
4252 (setq charmask (% curmask 256))
4253 (setq curmask (lsh curmask -8))
4254 (aset string-vector i (logxor charmask (aref string-vector i)))
4256 (concat string-vector)))
4258 ;;;; Desktop support
4260 (defun rmail-restore-desktop-buffer (desktop-buffer-file-name
4262 desktop-buffer-misc)
4263 "Restore an rmail buffer specified in a desktop file."
4264 (condition-case error
4266 (rmail-input desktop-buffer-file-name)
4267 (if (eq major-mode 'rmail-mode)
4271 (kill-buffer (current-buffer))
4274 (add-to-list 'desktop-buffer-mode-handlers
4275 '(rmail-mode . rmail-restore-desktop-buffer))
4279 ;;; arch-tag: cff0a950-57fe-4f73-a86e-91ff75afd06c
4280 ;;; rmail.el ends here