1 ;;; rmail.el --- main code of "RMAIL" mail reader for Emacs
3 ;; Copyright (C) 1985,86,87,88,93,94,95,96,97,98,2000, 01, 2004
4 ;; Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
30 ;; Souped up by shane@mit-ajax based on ideas of rlk@athena.mit.edu
31 ;; New features include attribute and keyword support, message
32 ;; selection by dispatch table, summary by attributes and keywords,
33 ;; expunging by dispatch table, sticky options for file commands.
35 ;; Extended by Bob Weiner of Motorola
36 ;; New features include: rmail and rmail-summary buffers remain
37 ;; synchronized and key bindings basically operate the same way in both
38 ;; buffers, summary by topic or by regular expression, rmail-reply-prefix
39 ;; variable, and a bury rmail buffer (wipe) command.
43 (eval-when-compile (require 'mule-util
)) ; for detect-coding-with-priority
45 ; These variables now declared in paths.el.
46 ;(defvar rmail-spool-directory "/usr/spool/mail/"
47 ; "This is the name of the directory used by the system mailer for\n\
48 ;delivering new mail. Its name should end with a slash.")
49 ;(defvar rmail-file-name
50 ; (expand-file-name "~/RMAIL")
54 "Mail reader for Emacs."
57 (defgroup rmail-retrieve nil
58 "Rmail retrieval options."
62 (defgroup rmail-files nil
67 (defgroup rmail-headers nil
68 "Rmail header options."
72 (defgroup rmail-reply nil
73 "Rmail reply options."
77 (defgroup rmail-summary nil
78 "Rmail summary options."
80 :prefix
"rmail-summary-"
83 (defgroup rmail-output nil
84 "Output message to a file."
85 :prefix
"rmail-output-"
89 (defgroup rmail-edit nil
95 (defcustom rmail-movemail-program nil
96 "If non-nil, name of program for fetching new mail."
97 :group
'rmail-retrieve
98 :type
'(choice (const nil
) string
))
100 (defcustom rmail-pop-password nil
101 "*Password to use when reading mail from a POP server, if required."
102 :type
'(choice (string :tag
"Password")
103 (const :tag
"Not Required" nil
))
104 :group
'rmail-retrieve
)
106 (defcustom rmail-pop-password-required nil
107 "*Non-nil if a password is required when reading mail using POP."
109 :group
'rmail-retrieve
)
111 (defcustom rmail-movemail-flags nil
112 "*List of flags to pass to movemail.
113 Most commonly used to specify `-g' to enable GSS-API authentication
114 or `-k' to enable Kerberos authentication."
115 :type
'(repeat string
)
116 :group
'rmail-retrieve
119 (defvar rmail-pop-password-error
"invalid usercode or password\\|
120 unknown user name or bad password"
121 "Regular expression matching incorrect-password POP server error messages.
122 If you get an incorrect-password error that this expression does not match,
123 please report it with \\[report-emacs-bug].")
125 (defvar rmail-encoded-pop-password nil
)
127 (defcustom rmail-preserve-inbox nil
128 "*Non-nil if incoming mail should be left in the user's inbox,
129 rather than deleted, after it is retrieved."
131 :group
'rmail-retrieve
)
134 (defcustom rmail-dont-reply-to-names nil
"\
135 *A regexp specifying addresses to prune from a reply message.
136 A value of nil means exclude your own email address as an address
137 plus whatever is specified by `rmail-default-dont-reply-to-names'."
138 :type
'(choice regexp
(const :tag
"Your Name" nil
))
142 (defvar rmail-default-dont-reply-to-names
"\\`info-" "\
143 A regular expression specifying part of the default value of the
144 variable `rmail-dont-reply-to-names', for when the user does not set
145 `rmail-dont-reply-to-names' explicitly. (The other part of the default
146 value is the user's email address and name.)
147 It is useful to set this variable in the site customization file.")
150 (defcustom rmail-ignored-headers
151 (concat "^via:\\|^mail-from:\\|^origin:\\|^references:"
152 "\\|^status:\\|^received:\\|^x400-originator:\\|^x400-recipients:"
153 "\\|^x400-received:\\|^x400-mts-identifier:\\|^x400-content-type:"
154 "\\|^\\(resent-\\|\\)message-id:\\|^summary-line:\\|^resent-date:"
155 "\\|^nntp-posting-host:\\|^path:\\|^x-char.*:\\|^x-face:\\|^face:"
156 "\\|^x-mailer:\\|^delivered-to:\\|^lines:\\|^mime-version:"
157 "\\|^content-transfer-encoding:\\|^x-coding-system:"
158 "\\|^return-path:\\|^errors-to:\\|^return-receipt-to:"
159 "\\|^x-sign:\\|^x-beenthere:\\|^x-mailman-version:"
160 "\\|^precedence:\\|^list-help:\\|^list-post:\\|^list-subscribe:"
161 "\\|^list-id:\\|^list-unsubscribe:\\|^list-archive:"
162 "\\|^content-type:\\|^content-length:"
163 "\\|^x-attribution:\\|^x-disclaimer:\\|^x-trace:"
164 "\\|^x-complaints-to:\\|^nntp-posting-date:\\|^user-agent"
165 "\\|^importance:\\|^envelope-to:\\|^delivery-date"
166 "\\|^x.*-priority:\\|^x-mimeole:")
167 "*Regexp to match header fields that Rmail should normally hide.
168 This variable is used for reformatting the message header,
169 which normally happens once for each message,
170 when you view the message for the first time in Rmail.
171 To make a change in this variable take effect
172 for a message that you have already viewed,
173 go to that message and type \\[rmail-toggle-header] twice."
175 :group
'rmail-headers
)
178 (defcustom rmail-displayed-headers nil
179 "*Regexp to match Header fields that Rmail should display.
180 If nil, display all header fields except those matched by
181 `rmail-ignored-headers'."
182 :type
'(choice regexp
(const :tag
"All"))
183 :group
'rmail-headers
)
186 (defcustom rmail-retry-ignored-headers
"^x-authentication-warning:" "\
187 *Headers that should be stripped when retrying a failed message."
188 :type
'(choice regexp
(const nil
:tag
"None"))
189 :group
'rmail-headers
)
192 (defcustom rmail-highlighted-headers
"^From:\\|^Subject:" "\
193 *Regexp to match Header fields that Rmail should normally highlight.
194 A value of nil means don't highlight.
195 See also `rmail-highlight-face'."
197 :group
'rmail-headers
)
200 (defcustom rmail-highlight-face nil
"\
201 *Face used by Rmail for highlighting headers."
202 :type
'(choice (const :tag
"Default" nil
)
204 :group
'rmail-headers
)
207 (defcustom rmail-delete-after-output nil
"\
208 *Non-nil means automatically delete a message that is copied to a file."
213 (defcustom rmail-primary-inbox-list nil
"\
214 *List of files which are inboxes for user's primary mail file `~/RMAIL'.
215 nil means the default, which is (\"/usr/spool/mail/$USER\")
216 \(the name varies depending on the operating system,
217 and the value of the environment variable MAIL overrides it)."
218 ;; Don't use backquote here, because we don't want to need it
220 :type
(list 'choice
'(const :tag
"Default" nil
)
221 (list 'repeat
':value
(list (or (getenv "MAIL")
222 (concat "/var/spool/mail/"
225 :group
'rmail-retrieve
229 (defcustom rmail-mail-new-frame nil
230 "*Non-nil means Rmail makes a new frame for composing outgoing mail.
231 This is handy if you want to preserve the window configuration of
232 the frame where you have the RMAIL buffer displayed."
237 (defcustom rmail-secondary-file-directory
"~/"
238 "*Directory for additional secondary Rmail files."
242 (defcustom rmail-secondary-file-regexp
"\\.xmail$"
243 "*Regexp for which files are secondary Rmail files."
248 (defcustom rmail-confirm-expunge
'y-or-n-p
249 "*Whether and how to ask for confirmation before expunging deleted messages."
250 :type
'(choice (const :tag
"No confirmation" nil
)
251 (const :tag
"Confirm with y-or-n-p" y-or-n-p
)
252 (const :tag
"Confirm with yes-or-no-p" yes-or-no-p
))
257 (defvar rmail-mode-hook nil
258 "List of functions to call when Rmail is invoked.")
261 (defvar rmail-get-new-mail-hook nil
262 "List of functions to call when Rmail has retrieved new mail.")
265 (defcustom rmail-show-message-hook nil
266 "List of functions to call when Rmail displays a message."
268 :options
'(goto-address)
272 (defvar rmail-quit-hook nil
273 "List of functions to call when quitting out of Rmail.")
276 (defvar rmail-delete-message-hook nil
277 "List of functions to call when Rmail deletes a message.
278 When the hooks are called, the message has been marked deleted but is
279 still the current message in the Rmail buffer.")
281 ;; These may be altered by site-init.el to match the format of mmdf files
282 ;; delimiting used on a given host (delim1 and delim2 from the config
285 (defvar rmail-mmdf-delim1
"^\001\001\001\001\n"
286 "Regexp marking the start of an mmdf message.")
287 (defvar rmail-mmdf-delim2
"^\001\001\001\001\n"
288 "Regexp marking the end of an mmdf message.")
290 (defcustom rmail-message-filter nil
291 "If non-nil, a filter function for new messages in RMAIL.
292 Called with region narrowed to the message, including headers,
293 before obeying `rmail-ignored-headers'."
294 :group
'rmail-headers
297 (defcustom rmail-automatic-folder-directives nil
298 "List of directives specifying where to put a message.
299 Each element of the list is of the form:
301 (FOLDERNAME FIELD REGEXP [ FIELD REGEXP ] ... )
303 Where FOLDERNAME is the name of a BABYL format folder to put the
304 message. If any of the field regexp's are nil, then it is ignored.
306 If FOLDERNAME is \"/dev/null\", it is deleted.
307 If FOLDERNAME is nil then it is deleted, and skipped.
309 FIELD is the plain text name of a field in the message, such as
310 \"subject\" or \"from\". A FIELD of \"to\" will automatically include
311 all text from the \"cc\" field as well.
313 REGEXP is an expression to match in the preceeding specified FIELD.
314 FIELD/REGEXP pairs continue in the list.
317 (\"/dev/null\" \"from\" \"@spam.com\") ; delete all mail from spam.com
318 (\"RMS\" \"from\" \"rms@\") ; save all mail from RMS."
321 :type
'(repeat (sexp :tag
"Directive")))
323 (defvar rmail-reply-prefix
"Re: "
324 "String to prepend to Subject line when replying to a message.")
326 ;; Some mailers use "Re(2):" or "Re^2:" or "Re: Re:" or "Re[2]:".
327 ;; This pattern should catch all the common variants.
328 (defvar rmail-reply-regexp
"\\`\\(Re\\(([0-9]+)\\|\\[[0-9]+\\]\\|\\^[0-9]+\\)?: *\\)*"
329 "Regexp to delete from Subject line before inserting `rmail-reply-prefix'.")
331 (defcustom rmail-display-summary nil
332 "*If non-nil, Rmail always displays the summary buffer."
333 :group
'rmail-summary
336 (defvar rmail-inbox-list nil
)
337 (put 'rmail-inbox-list
'permanent-local t
)
339 (defvar rmail-keywords nil
)
340 (put 'rmail-keywords
'permanent-local t
)
342 (defvar rmail-buffer nil
343 "The RMAIL buffer related to the current buffer.
344 In an RMAIL buffer, this holds the RMAIL buffer itself.
345 In a summary buffer, this holds the RMAIL buffer it is a summary for.")
346 (put 'rmail-buffer
'permanent-local t
)
348 ;; Message counters and markers. Deleted flags.
350 (defvar rmail-current-message nil
)
351 (put 'rmail-current-message
'permanent-local t
)
353 (defvar rmail-total-messages nil
)
354 (put 'rmail-total-messages
'permanent-local t
)
356 (defvar rmail-message-vector nil
)
357 (put 'rmail-message-vector
'permanent-local t
)
359 (defvar rmail-deleted-vector nil
)
360 (put 'rmail-deleted-vector
'permanent-local t
)
362 (defvar rmail-msgref-vector nil
363 "In an Rmail buffer, a vector whose Nth element is a list (N).
364 When expunging renumbers messages, these lists are modified
365 by substituting the new message number into the existing list.")
366 (put 'rmail-msgref-vector
'permanent-local t
)
368 (defvar rmail-overlay-list nil
)
369 (put 'rmail-overlay-list
'permanent-local t
)
371 ;; These are used by autoloaded rmail-summary.
373 (defvar rmail-summary-buffer nil
)
374 (put 'rmail-summary-buffer
'permanent-local t
)
375 (defvar rmail-summary-vector nil
)
376 (put 'rmail-summary-vector
'permanent-local t
)
378 (defvar rmail-view-buffer nil
379 "Buffer which holds RMAIL message for MIME displaying.")
380 (put 'rmail-view-buffer
'permanent-local t
)
382 ;; `Sticky' default variables.
384 ;; Last individual label specified to a or k.
385 (defvar rmail-last-label nil
)
386 (put 'rmail-last-label
'permanent-local t
)
388 ;; Last set of values specified to C-M-n, C-M-p, C-M-s or C-M-l.
389 (defvar rmail-last-multi-labels nil
)
391 (defvar rmail-last-regexp nil
)
392 (put 'rmail-last-regexp
'permanent-local t
)
394 (defcustom rmail-default-file
"~/xmail"
395 "*Default file name for \\[rmail-output]."
398 (defcustom rmail-default-rmail-file
"~/XMAIL"
399 "*Default file name for \\[rmail-output-to-rmail-file]."
402 (defcustom rmail-default-body-file
"~/mailout"
403 "*Default file name for \\[rmail-output-body-to-file]."
408 ;; Mule and MIME related variables.
411 (defvar rmail-file-coding-system nil
412 "Coding system used in RMAIL file.
414 This is set to nil by default.")
417 (defcustom rmail-enable-mime nil
418 "*If non-nil, RMAIL uses MIME feature.
419 If the value is t, RMAIL automatically shows MIME decoded message.
420 If the value is neither t nor nil, RMAIL does not show MIME decoded message
421 until a user explicitly requires it."
422 :type
'(choice (const :tag
"on" t
)
423 (const :tag
"off" nil
)
424 (other :tag
"when asked" ask
))
427 (defvar rmail-enable-mime-composing nil
428 "*If non-nil, RMAIL uses `rmail-insert-mime-forwarded-message-function' to forward.")
431 (defvar rmail-show-mime-function nil
432 "Function to show MIME decoded message of RMAIL file.
433 This function is called when `rmail-enable-mime' is non-nil.
434 It is called with no argument.")
437 (defvar rmail-insert-mime-forwarded-message-function nil
438 "Function to insert a message in MIME format so it can be forwarded.
439 This function is called if `rmail-enable-mime' or
440 `rmail-enable-mime-composing' is non-nil.
441 It is called with one argument FORWARD-BUFFER, which is a
442 buffer containing the message to forward. The current buffer
443 is the outgoing mail buffer.")
446 (defvar rmail-insert-mime-resent-message-function nil
447 "Function to insert a message in MIME format so it can be resent.
448 This function is called if `rmail-enable-mime' is non-nil.
449 It is called with one argument FORWARD-BUFFER, which is a
450 buffer containing the message to forward. The current buffer
451 is the outgoing mail buffer.")
454 (defvar rmail-search-mime-message-function nil
455 "Function to check if a regexp matches a MIME message.
456 This function is called if `rmail-enable-mime' is non-nil.
457 It is called with two arguments MSG and REGEXP, where
458 MSG is the message number, REGEXP is the regular expression.")
461 (defvar rmail-search-mime-header-function nil
462 "Function to check if a regexp matches a header of MIME message.
463 This function is called if `rmail-enable-mime' is non-nil.
464 It is called with three arguments MSG, REGEXP, and LIMIT, where
465 MSG is the message number,
466 REGEXP is the regular expression,
467 LIMIT is the position specifying the end of header.")
470 (defvar rmail-mime-feature
'rmail-mime
471 "Feature to require to load MIME support in Rmail.
472 When starting Rmail, if `rmail-enable-mime' is non-nil,
473 this feature is required with `require'.")
476 (defvar rmail-decode-mime-charset t
477 "*Non-nil means a message is decoded by MIME's charset specification.
478 If this variable is nil, or the message has not MIME specification,
479 the message is decoded as normal way.
481 If the variable `rmail-enable-mime' is non-nil, this variables is
482 ignored, and all the decoding work is done by a feature specified by
483 the variable `rmail-mime-feature'.")
486 (defvar rmail-mime-charset-pattern
487 "^content-type:[ ]*text/plain;[ \t\n]*charset=\"?\\([^ \t\n\";]+\\)\"?"
488 "Regexp to match MIME-charset specification in a header of message.
489 The first parenthesized expression should match the MIME-charset name.")
492 ;;; Regexp matching the delimiter of messages in UNIX mail format
493 ;;; (UNIX From lines), minus the initial ^. Note that if you change
494 ;;; this expression, you must change the code in rmail-nuke-pinhead-header
495 ;;; that knows the exact ordering of the \\( \\) subexpressions.
496 (defvar rmail-unix-mail-delimiter
497 (let ((time-zone-regexp
498 (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
499 "\\|[-+]?[0-9][0-9][0-9][0-9]"
505 ;; Many things can happen to an RFC 822 mailbox before it is put into
506 ;; a `From' line. The leading phrase can be stripped, e.g.
507 ;; `Joe <@w.x:joe@y.z>' -> `<@w.x:joe@y.z>'. The <> can be stripped, e.g.
508 ;; `<@x.y:joe@y.z>' -> `@x.y:joe@y.z'. Everything starting with a CRLF
509 ;; can be removed, e.g.
510 ;; From: joe@y.z (Joe K
512 ;; can yield `From joe@y.z (Joe K Fri Mar 22 08:11:15 1996', and
515 ;; can yield `From Joe User Fri Mar 22 08:11:15 1996'.
516 ;; The mailbox can be removed or be replaced by white space, e.g.
517 ;; From: "Joe User"{space}{tab}
519 ;; can yield `From {space}{tab} Fri Mar 22 08:11:15 1996',
520 ;; where {space} and {tab} represent the Ascii space and tab characters.
521 ;; We want to match the results of any of these manglings.
522 ;; The following regexp rejects names whose first characters are
523 ;; obviously bogus, but after that anything goes.
524 "\\([^\0-\b\n-\r\^?].*\\)? "
526 ;; The time the message was sent.
527 "\\([^\0-\r \^?]+\\) +" ; day of the week
528 "\\([^\0-\r \^?]+\\) +" ; month
529 "\\([0-3]?[0-9]\\) +" ; day of month
530 "\\([0-2][0-9]:[0-5][0-9]\\(:[0-6][0-9]\\)?\\) *" ; time of day
532 ;; Perhaps a time zone, specified by an abbreviation, or by a
537 " \\([0-9][0-9]+\\) *"
539 ;; On some systems the time zone can appear after the year, too.
543 "\\(remote from .*\\)?"
548 (defvar rmail-font-lock-keywords
549 ;; These are all matched case-insensitively.
551 (let* ((cite-chars "[>|}]")
553 (cite-suffix (concat cite-prefix
"0-9_.@-`'\"")))
554 (list '("^\\(From\\|Sender\\|Resent-From\\):"
555 . font-lock-function-name-face
)
556 '("^Reply-To:.*$" . font-lock-function-name-face
)
557 '("^Subject:" . font-lock-comment-face
)
558 '("^\\(To\\|Apparently-To\\|Cc\\|Newsgroups\\):"
559 . font-lock-keyword-face
)
560 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
562 (,(concat "\\=[ \t]*"
563 "\\(\\([" cite-prefix
"]+[" cite-suffix
"]*\\)?"
564 "\\(" cite-chars
"[ \t]*\\)\\)+"
566 (beginning-of-line) (end-of-line)
567 (2 font-lock-constant-face nil t
)
568 (4 font-lock-comment-face nil t
)))
569 '("^\\(X-[a-z0-9-]+\\|In-reply-to\\|Date\\):.*\\(\n[ \t]+.*\\)*$"
570 . font-lock-string-face
))))
571 "Additional expressions to highlight in Rmail mode.")
573 ;; Perform BODY in the summary buffer
574 ;; in such a way that its cursor is properly updated in its own window.
575 (defmacro rmail-select-summary
(&rest body
)
576 `(let ((total rmail-total-messages
))
577 (if (rmail-summary-displayed)
578 (let ((window (selected-window)))
582 (pop-to-buffer rmail-summary-buffer
)
583 ;; rmail-total-messages is a buffer-local var
584 ;; in the rmail buffer.
585 ;; This way we make it available for the body
586 ;; even tho the rmail buffer is not current.
587 (let ((rmail-total-messages total
))
589 (select-window window
))))
591 (set-buffer rmail-summary-buffer
)
592 (let ((rmail-total-messages total
))
594 (rmail-maybe-display-summary)))
596 ;;;; *** Rmail Mode ***
598 ;; This variable is dynamically bound. The defvar is here to placate
599 ;; the byte compiler.
601 (defvar rmail-enable-multibyte nil
)
604 (defun rmail-require-mime-maybe ()
605 "Require `rmail-mime-feature' if that is non-nil.
606 Signal an error and set `rmail-mime-feature' to nil if the feature
608 (when rmail-enable-mime
610 (require rmail-mime-feature
)
612 (message "Feature `%s' not provided" rmail-mime-feature
)
614 (setq rmail-enable-mime nil
)))))
618 (defun rmail (&optional file-name-arg
)
619 "Read and edit incoming mail.
620 Moves messages into file named by `rmail-file-name' (a babyl format file)
621 and edits that file in RMAIL Mode.
622 Type \\[describe-mode] once editing that file, for a list of RMAIL commands.
624 May be called with file name as argument; then performs rmail editing on
625 that file, but does not copy any new mail into the file.
626 Interactively, if you supply a prefix argument, then you
627 have a chance to specify a file name with the minibuffer.
629 If `rmail-display-summary' is non-nil, make a summary for this RMAIL file."
630 (interactive (if current-prefix-arg
631 (list (read-file-name "Run rmail on RMAIL file: "))))
632 (rmail-require-mime-maybe)
633 (let* ((file-name (expand-file-name (or file-name-arg rmail-file-name
)))
634 ;; Use find-buffer-visiting, not get-file-buffer, for those users
635 ;; who have find-file-visit-truename set to t.
636 (existed (find-buffer-visiting file-name
))
637 ;; This binding is necessary because we must decide if we
638 ;; need code conversion while the buffer is unibyte
639 ;; (i.e. enable-multibyte-characters is nil).
640 (rmail-enable-multibyte
642 (with-current-buffer existed enable-multibyte-characters
)
643 (default-value 'enable-multibyte-characters
)))
644 ;; Since the file may contain messages of different encodings
645 ;; at the tail (non-BYBYL part), we can't decode them at once
646 ;; on reading. So, at first, we read the file without text
647 ;; code conversion, then decode the messages one by one by
648 ;; rmail-decode-babyl-format or
649 ;; rmail-convert-to-babyl-format.
650 (coding-system-for-read (and rmail-enable-multibyte
'raw-text
))
651 run-mail-hook msg-shown
)
652 ;; Like find-file, but in the case where a buffer existed
653 ;; and the file was reverted, recompute the message-data.
654 ;; We used to bind enable-local-variables to nil here,
655 ;; but that should not be needed now that rmail-mode
656 ;; sets it locally to nil.
657 ;; (Binding a variable locally with let is not safe if it has
658 ;; buffer-local bindings.)
659 (if (and existed
(not (verify-visited-file-modtime existed
)))
661 (find-file file-name
)
662 (if (and (verify-visited-file-modtime existed
)
663 (eq major-mode
'rmail-mode
))
664 (progn (rmail-forget-messages)
665 (rmail-set-message-counters))))
667 (let ((enable-local-variables nil
))
668 (find-file-noselect file-name
))))
669 (if (eq major-mode
'rmail-edit-mode
)
670 (error "Exit Rmail Edit mode before getting new mail"))
671 (if (and existed
(> (buffer-size) 0))
672 ;; Buffer not new and not empty; ensure in proper mode, but that's all.
673 (or (eq major-mode
'rmail-mode
)
674 (progn (rmail-mode-2)
675 (setq run-mail-hook t
)))
676 (setq run-mail-hook t
)
678 ;; Convert all or part to Babyl file if possible.
680 (goto-char (point-max)))
681 ;; As we have read a file by raw-text, the buffer is set to
682 ;; unibyte. We must make it multibyte if necessary.
683 (if (and rmail-enable-multibyte
684 (not enable-multibyte-characters
))
685 (set-buffer-multibyte t
))
686 ;; If necessary, scan to find all the messages.
687 (rmail-maybe-set-message-counters)
689 (unless (and (not file-name-arg
)
690 (rmail-get-new-mail))
691 (rmail-show-message (rmail-first-unseen-message)))
693 (if rmail-display-summary
(rmail-summary))
694 (rmail-construct-io-menu)
696 (run-hooks 'rmail-mode-hook
))))))
698 ;; Given the value of MAILPATH, return a list of inbox file names.
699 ;; This is turned off because it is not clear that the user wants
700 ;; all these inboxes to feed into the primary rmail file.
701 ; (defun rmail-convert-mailpath (string)
703 ; (while (setq idx (string-match "[%:]" string))
704 ; (let ((this (substring string 0 idx)))
705 ; (setq string (substring string (1+ idx)))
706 ; (setq list (cons (if (string-match "%" this)
707 ; (substring this 0 (string-match "%" this))
712 ; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
713 ; will not cause emacs 18.55 problems.
715 ;; This calls rmail-decode-babyl-format if the file is already Babyl.
717 (defun rmail-convert-file ()
720 (goto-char (point-min))
721 ;; If file doesn't start like a Babyl file,
722 ;; convert it to one, by adding a header and converting each message.
723 (cond ((looking-at "BABYL OPTIONS:"))
724 ((looking-at "Version: 5\n")
725 ;; Losing babyl file made by old version of Rmail.
726 ;; Just fix the babyl file header; don't make a new one,
727 ;; so we don't lose the Labels: file attribute, etc.
728 (let ((buffer-read-only nil
))
729 (insert "BABYL OPTIONS: -*- rmail -*-\n")))
730 ((equal (point-min) (point-max))
731 ;; Empty RMAIL file. Just insert the header.
732 (rmail-insert-rmail-file-header))
734 ;; Non-empty file in non-RMAIL format. Add header and convert.
736 (rmail-insert-rmail-file-header)))
737 ;; If file was not a Babyl file or if there are
738 ;; Unix format messages added at the end,
739 ;; convert file as necessary.
742 (goto-char (point-max))
743 (search-backward "\n\^_")
745 (looking-at "\n*From ")))
746 (let ((buffer-read-only nil
))
747 (message "Converting to Babyl format...")
748 ;; If file needs conversion, convert it all,
749 ;; except for the BABYL header.
750 ;; (rmail-convert-to-babyl-format would delete the header.)
751 (goto-char (point-min))
752 (search-forward "\n\^_" nil t
)
753 (narrow-to-region (point) (point-max))
754 (rmail-convert-to-babyl-format)
755 (message "Converting to Babyl format...done"))
756 (if (and (not rmail-enable-mime
)
757 rmail-enable-multibyte
)
758 ;; We still have to decode BABYL part.
759 (rmail-decode-babyl-format)))))
761 (defun rmail-insert-rmail-file-header ()
762 (let ((buffer-read-only nil
))
763 ;; -*-rmail-*- is here so that visiting the file normally
764 ;; recognizes it as an Rmail file.
765 (insert "BABYL OPTIONS: -*- rmail -*-
768 Note: This is the header of an rmail file.
769 Note: If you are seeing it in rmail,
770 Note: it means the file has no messages in it.\n\^_")))
772 ;; Decode Babyl formatted part at the head of current buffer by
773 ;; rmail-file-coding-system, or if it is nil, do auto conversion.
775 (defun rmail-decode-babyl-format ()
776 (let ((modifiedp (buffer-modified-p))
777 (buffer-read-only nil
)
778 (coding-system rmail-file-coding-system
)
780 (goto-char (point-min))
781 (search-forward "\n\^_" nil t
) ; Skip BABYL header.
783 (goto-char (point-max))
784 (search-backward "\n\^_" from
'mv
)
786 (unless (and coding-system
787 (coding-system-p coding-system
))
789 ;; Emacs 21.1 and later writes RMAIL files in emacs-mule, but
790 ;; earlier versions did that with the current buffer's encoding.
791 ;; So we want to favor detection of emacs-mule (whose normal
792 ;; priority is quite low), but still allow detection of other
793 ;; encodings if emacs-mule won't fit. The call to
794 ;; detect-coding-with-priority below achieves that.
795 (car (detect-coding-with-priority
797 '((coding-category-emacs-mule . emacs-mule
))))))
798 (unless (memq coding-system
799 '(undecided undecided-unix
))
800 (set-buffer-modified-p t
) ; avoid locking when decoding
801 (let ((buffer-undo-list t
))
802 (decode-coding-region from to coding-system
))
803 (setq coding-system last-coding-system-used
))
804 (set-buffer-modified-p modifiedp
)
805 (setq buffer-file-coding-system nil
)
806 (setq save-buffer-coding-system
807 (or coding-system
'undecided
))))
809 (defvar rmail-mode-map nil
)
812 (setq rmail-mode-map
(make-keymap))
813 (suppress-keymap rmail-mode-map
)
814 (define-key rmail-mode-map
"a" 'rmail-add-label
)
815 (define-key rmail-mode-map
"b" 'rmail-bury
)
816 (define-key rmail-mode-map
"c" 'rmail-continue
)
817 (define-key rmail-mode-map
"d" 'rmail-delete-forward
)
818 (define-key rmail-mode-map
"\C-d" 'rmail-delete-backward
)
819 (define-key rmail-mode-map
"e" 'rmail-edit-current-message
)
820 (define-key rmail-mode-map
"f" 'rmail-forward
)
821 (define-key rmail-mode-map
"g" 'rmail-get-new-mail
)
822 (define-key rmail-mode-map
"h" 'rmail-summary
)
823 (define-key rmail-mode-map
"i" 'rmail-input
)
824 (define-key rmail-mode-map
"j" 'rmail-show-message
)
825 (define-key rmail-mode-map
"k" 'rmail-kill-label
)
826 (define-key rmail-mode-map
"l" 'rmail-summary-by-labels
)
827 (define-key rmail-mode-map
"\e\C-h" 'rmail-summary
)
828 (define-key rmail-mode-map
"\e\C-l" 'rmail-summary-by-labels
)
829 (define-key rmail-mode-map
"\e\C-r" 'rmail-summary-by-recipients
)
830 (define-key rmail-mode-map
"\e\C-s" 'rmail-summary-by-regexp
)
831 (define-key rmail-mode-map
"\e\C-t" 'rmail-summary-by-topic
)
832 (define-key rmail-mode-map
"m" 'rmail-mail
)
833 (define-key rmail-mode-map
"\em" 'rmail-retry-failure
)
834 (define-key rmail-mode-map
"n" 'rmail-next-undeleted-message
)
835 (define-key rmail-mode-map
"\en" 'rmail-next-message
)
836 (define-key rmail-mode-map
"\e\C-n" 'rmail-next-labeled-message
)
837 (define-key rmail-mode-map
"o" 'rmail-output-to-rmail-file
)
838 (define-key rmail-mode-map
"\C-o" 'rmail-output
)
839 (define-key rmail-mode-map
"p" 'rmail-previous-undeleted-message
)
840 (define-key rmail-mode-map
"\ep" 'rmail-previous-message
)
841 (define-key rmail-mode-map
"\e\C-p" 'rmail-previous-labeled-message
)
842 (define-key rmail-mode-map
"q" 'rmail-quit
)
843 (define-key rmail-mode-map
"r" 'rmail-reply
)
844 ;; I find I can't live without the default M-r command -- rms.
845 ;; (define-key rmail-mode-map "\er" 'rmail-search-backwards)
846 (define-key rmail-mode-map
"s" 'rmail-expunge-and-save
)
847 (define-key rmail-mode-map
"\es" 'rmail-search
)
848 (define-key rmail-mode-map
"t" 'rmail-toggle-header
)
849 (define-key rmail-mode-map
"u" 'rmail-undelete-previous-message
)
850 (define-key rmail-mode-map
"w" 'rmail-output-body-to-file
)
851 (define-key rmail-mode-map
"x" 'rmail-expunge
)
852 (define-key rmail-mode-map
"." 'rmail-beginning-of-message
)
853 (define-key rmail-mode-map
"<" 'rmail-first-message
)
854 (define-key rmail-mode-map
">" 'rmail-last-message
)
855 (define-key rmail-mode-map
" " 'scroll-up
)
856 (define-key rmail-mode-map
"\177" 'scroll-down
)
857 (define-key rmail-mode-map
"?" 'describe-mode
)
858 (define-key rmail-mode-map
"\C-c\C-s\C-d" 'rmail-sort-by-date
)
859 (define-key rmail-mode-map
"\C-c\C-s\C-s" 'rmail-sort-by-subject
)
860 (define-key rmail-mode-map
"\C-c\C-s\C-a" 'rmail-sort-by-author
)
861 (define-key rmail-mode-map
"\C-c\C-s\C-r" 'rmail-sort-by-recipient
)
862 (define-key rmail-mode-map
"\C-c\C-s\C-c" 'rmail-sort-by-correspondent
)
863 (define-key rmail-mode-map
"\C-c\C-s\C-l" 'rmail-sort-by-lines
)
864 (define-key rmail-mode-map
"\C-c\C-s\C-k" 'rmail-sort-by-labels
)
865 (define-key rmail-mode-map
"\C-c\C-n" 'rmail-next-same-subject
)
866 (define-key rmail-mode-map
"\C-c\C-p" 'rmail-previous-same-subject
)
869 (define-key rmail-mode-map
[menu-bar
] (make-sparse-keymap))
871 (define-key rmail-mode-map
[menu-bar classify
]
872 (cons "Classify" (make-sparse-keymap "Classify")))
874 (define-key rmail-mode-map
[menu-bar classify input-menu
]
877 (define-key rmail-mode-map
[menu-bar classify output-menu
]
880 (define-key rmail-mode-map
[menu-bar classify output-body
]
881 '("Output body to file..." . rmail-output-body-to-file
))
883 (define-key rmail-mode-map
[menu-bar classify output-inbox
]
884 '("Output (inbox)..." . rmail-output
))
886 (define-key rmail-mode-map
[menu-bar classify output
]
887 '("Output (Rmail)..." . rmail-output-to-rmail-file
))
889 (define-key rmail-mode-map
[menu-bar classify kill-label
]
890 '("Kill Label..." . rmail-kill-label
))
892 (define-key rmail-mode-map
[menu-bar classify add-label
]
893 '("Add Label..." . rmail-add-label
))
895 (define-key rmail-mode-map
[menu-bar summary
]
896 (cons "Summary" (make-sparse-keymap "Summary")))
898 (define-key rmail-mode-map
[menu-bar summary senders
]
899 '("By Senders..." . rmail-summary-by-senders
))
901 (define-key rmail-mode-map
[menu-bar summary labels
]
902 '("By Labels..." . rmail-summary-by-labels
))
904 (define-key rmail-mode-map
[menu-bar summary recipients
]
905 '("By Recipients..." . rmail-summary-by-recipients
))
907 (define-key rmail-mode-map
[menu-bar summary topic
]
908 '("By Topic..." . rmail-summary-by-topic
))
910 (define-key rmail-mode-map
[menu-bar summary regexp
]
911 '("By Regexp..." . rmail-summary-by-regexp
))
913 (define-key rmail-mode-map
[menu-bar summary all
]
914 '("All" . rmail-summary
))
916 (define-key rmail-mode-map
[menu-bar mail
]
917 (cons "Mail" (make-sparse-keymap "Mail")))
919 (define-key rmail-mode-map
[menu-bar mail rmail-get-new-mail
]
920 '("Get New Mail" . rmail-get-new-mail
))
922 (define-key rmail-mode-map
[menu-bar mail lambda
]
925 (define-key rmail-mode-map
[menu-bar mail continue
]
926 '("Continue" . rmail-continue
))
928 (define-key rmail-mode-map
[menu-bar mail resend
]
929 '("Re-send..." . rmail-resend
))
931 (define-key rmail-mode-map
[menu-bar mail forward
]
932 '("Forward" . rmail-forward
))
934 (define-key rmail-mode-map
[menu-bar mail retry
]
935 '("Retry" . rmail-retry-failure
))
937 (define-key rmail-mode-map
[menu-bar mail reply
]
938 '("Reply" . rmail-reply
))
940 (define-key rmail-mode-map
[menu-bar mail mail
]
941 '("Mail" . rmail-mail
))
943 (define-key rmail-mode-map
[menu-bar delete
]
944 (cons "Delete" (make-sparse-keymap "Delete")))
946 (define-key rmail-mode-map
[menu-bar delete expunge
/save
]
947 '("Expunge/Save" . rmail-expunge-and-save
))
949 (define-key rmail-mode-map
[menu-bar delete expunge
]
950 '("Expunge" . rmail-expunge
))
952 (define-key rmail-mode-map
[menu-bar delete undelete
]
953 '("Undelete" . rmail-undelete-previous-message
))
955 (define-key rmail-mode-map
[menu-bar delete delete
]
956 '("Delete" . rmail-delete-forward
))
958 (define-key rmail-mode-map
[menu-bar move
]
959 (cons "Move" (make-sparse-keymap "Move")))
961 (define-key rmail-mode-map
[menu-bar move search-back
]
962 '("Search Back..." . rmail-search-backwards
))
964 (define-key rmail-mode-map
[menu-bar move search
]
965 '("Search..." . rmail-search
))
967 (define-key rmail-mode-map
[menu-bar move previous
]
968 '("Previous Nondeleted" . rmail-previous-undeleted-message
))
970 (define-key rmail-mode-map
[menu-bar move next
]
971 '("Next Nondeleted" . rmail-next-undeleted-message
))
973 (define-key rmail-mode-map
[menu-bar move last
]
974 '("Last" . rmail-last-message
))
976 (define-key rmail-mode-map
[menu-bar move first
]
977 '("First" . rmail-first-message
))
979 (define-key rmail-mode-map
[menu-bar move previous
]
980 '("Previous" . rmail-previous-message
))
982 (define-key rmail-mode-map
[menu-bar move next
]
983 '("Next" . rmail-next-message
))
985 ;; Rmail mode is suitable only for specially formatted data.
986 (put 'rmail-mode
'mode-class
'special
)
988 (defun rmail-mode-kill-summary ()
989 (if rmail-summary-buffer
(kill-buffer rmail-summary-buffer
)))
993 "Rmail Mode is used by \\<rmail-mode-map>\\[rmail] for editing Rmail files.
994 All normal editing commands are turned off.
995 Instead, these commands are available:
997 \\[rmail-beginning-of-message] Move point to front of this message (same as \\[beginning-of-buffer]).
998 \\[scroll-up] Scroll to next screen of this message.
999 \\[scroll-down] Scroll to previous screen of this message.
1000 \\[rmail-next-undeleted-message] Move to Next non-deleted message.
1001 \\[rmail-previous-undeleted-message] Move to Previous non-deleted message.
1002 \\[rmail-next-message] Move to Next message whether deleted or not.
1003 \\[rmail-previous-message] Move to Previous message whether deleted or not.
1004 \\[rmail-first-message] Move to the first message in Rmail file.
1005 \\[rmail-last-message] Move to the last message in Rmail file.
1006 \\[rmail-show-message] Jump to message specified by numeric position in file.
1007 \\[rmail-search] Search for string and show message it is found in.
1008 \\[rmail-delete-forward] Delete this message, move to next nondeleted.
1009 \\[rmail-delete-backward] Delete this message, move to previous nondeleted.
1010 \\[rmail-undelete-previous-message] Undelete message. Tries current message, then earlier messages
1011 till a deleted message is found.
1012 \\[rmail-edit-current-message] Edit the current message. \\[rmail-cease-edit] to return to Rmail.
1013 \\[rmail-expunge] Expunge deleted messages.
1014 \\[rmail-expunge-and-save] Expunge and save the file.
1015 \\[rmail-quit] Quit Rmail: expunge, save, then switch to another buffer.
1016 \\[save-buffer] Save without expunging.
1017 \\[rmail-get-new-mail] Move new mail from system spool directory into this file.
1018 \\[rmail-mail] Mail a message (same as \\[mail-other-window]).
1019 \\[rmail-continue] Continue composing outgoing message started before.
1020 \\[rmail-reply] Reply to this message. Like \\[rmail-mail] but initializes some fields.
1021 \\[rmail-retry-failure] Send this message again. Used on a mailer failure message.
1022 \\[rmail-forward] Forward this message to another user.
1023 \\[rmail-output-to-rmail-file] Output this message to an Rmail file (append it).
1024 \\[rmail-output] Output this message to a Unix-format mail file (append it).
1025 \\[rmail-output-body-to-file] Save message body to a file. Default filename comes from Subject line.
1026 \\[rmail-input] Input Rmail file. Run Rmail on that file.
1027 \\[rmail-add-label] Add label to message. It will be displayed in the mode line.
1028 \\[rmail-kill-label] Kill label. Remove a label from current message.
1029 \\[rmail-next-labeled-message] Move to Next message with specified label
1030 (label defaults to last one specified).
1031 Standard labels: filed, unseen, answered, forwarded, deleted.
1032 Any other label is present only if you add it with \\[rmail-add-label].
1033 \\[rmail-previous-labeled-message] Move to Previous message with specified label
1034 \\[rmail-summary] Show headers buffer, with a one line summary of each message.
1035 \\[rmail-summary-by-labels] Summarize only messages with particular label(s).
1036 \\[rmail-summary-by-recipients] Summarize only messages with particular recipient(s).
1037 \\[rmail-summary-by-regexp] Summarize only messages with particular regexp(s).
1038 \\[rmail-summary-by-topic] Summarize only messages with subject line regexp(s).
1039 \\[rmail-toggle-header] Toggle display of complete header."
1041 (let ((finding-rmail-file (not (eq major-mode
'rmail-mode
))))
1043 (when (and finding-rmail-file
1044 (null coding-system-for-read
)
1045 default-enable-multibyte-characters
)
1046 (let ((rmail-enable-multibyte t
))
1047 (rmail-require-mime-maybe)
1048 (rmail-convert-file)
1049 (goto-char (point-max))
1050 (set-buffer-multibyte t
)))
1051 (rmail-set-message-counters)
1052 (rmail-show-message rmail-total-messages
)
1053 (when finding-rmail-file
1054 (when rmail-display-summary
1056 (rmail-construct-io-menu))
1057 (run-hooks 'rmail-mode-hook
)))
1059 (defun rmail-mode-2 ()
1060 (kill-all-local-variables)
1062 (rmail-perm-variables)
1065 (defun rmail-mode-1 ()
1066 (setq major-mode
'rmail-mode
)
1067 (setq mode-name
"RMAIL")
1068 (setq buffer-read-only t
)
1069 ;; No need to auto save RMAIL files in normal circumstances
1070 ;; because they contain no info except attribute changes
1071 ;; and deletion of messages.
1072 ;; The one exception is when messages are copied into an Rmail mode buffer.
1073 ;; rmail-output-to-rmail-file enables auto save when you do that.
1074 (setq buffer-auto-save-file-name nil
)
1075 (setq mode-line-modified
"--")
1076 (use-local-map rmail-mode-map
)
1077 (set-syntax-table text-mode-syntax-table
)
1078 (setq local-abbrev-table text-mode-abbrev-table
))
1080 ;; Set up the permanent locals associated with an Rmail file.
1081 (defun rmail-perm-variables ()
1082 (make-local-variable 'rmail-last-label
)
1083 (make-local-variable 'rmail-last-regexp
)
1084 (make-local-variable 'rmail-deleted-vector
)
1085 (make-local-variable 'rmail-buffer
)
1086 (setq rmail-buffer
(current-buffer))
1087 (make-local-variable 'rmail-view-buffer
)
1088 (setq rmail-view-buffer rmail-buffer
)
1089 (make-local-variable 'rmail-summary-buffer
)
1090 (make-local-variable 'rmail-summary-vector
)
1091 (make-local-variable 'rmail-current-message
)
1092 (make-local-variable 'rmail-total-messages
)
1093 (make-local-variable 'rmail-overlay-list
)
1094 (setq rmail-overlay-list nil
)
1095 (make-local-variable 'rmail-message-vector
)
1096 (make-local-variable 'rmail-msgref-vector
)
1097 (make-local-variable 'rmail-inbox-list
)
1098 (setq rmail-inbox-list
(rmail-parse-file-inboxes))
1099 ;; Provide default set of inboxes for primary mail file ~/RMAIL.
1100 (and (null rmail-inbox-list
)
1101 (or (equal buffer-file-name
(expand-file-name rmail-file-name
))
1102 (equal buffer-file-truename
1103 (abbreviate-file-name (file-truename rmail-file-name
))))
1104 (setq rmail-inbox-list
1105 (or rmail-primary-inbox-list
1106 (list (or (getenv "MAIL")
1107 (concat rmail-spool-directory
1108 (user-login-name)))))))
1109 (make-local-variable 'rmail-keywords
)
1110 ;; this gets generated as needed
1111 (setq rmail-keywords nil
))
1113 ;; Set up the non-permanent locals associated with Rmail mode.
1114 (defun rmail-variables ()
1115 (make-local-variable 'save-buffer-coding-system
)
1116 ;; If we don't already have a value for save-buffer-coding-system,
1117 ;; get it from buffer-file-coding-system, and clear that
1118 ;; because it should be determined in rmail-show-message.
1119 (unless save-buffer-coding-system
1120 (setq save-buffer-coding-system
(or buffer-file-coding-system
'undecided
))
1121 (setq buffer-file-coding-system nil
))
1122 ;; Don't let a local variables list in a message cause confusion.
1123 (make-local-variable 'local-enable-local-variables
)
1124 (setq local-enable-local-variables nil
)
1125 (make-local-variable 'revert-buffer-function
)
1126 (setq revert-buffer-function
'rmail-revert
)
1127 (make-local-variable 'font-lock-defaults
)
1128 (setq font-lock-defaults
1129 '(rmail-font-lock-keywords
1131 (font-lock-maximum-size . nil
)
1132 (font-lock-fontify-buffer-function . rmail-fontify-buffer-function
)
1133 (font-lock-unfontify-buffer-function . rmail-unfontify-buffer-function
)
1134 (font-lock-inhibit-thing-lock .
(lazy-lock-mode fast-lock-mode
))))
1135 (make-local-variable 'require-final-newline
)
1136 (setq require-final-newline nil
)
1137 (make-local-variable 'version-control
)
1138 (setq version-control
'never
)
1139 (make-local-variable 'kill-buffer-hook
)
1140 (add-hook 'kill-buffer-hook
'rmail-mode-kill-summary
)
1141 (make-local-variable 'file-precious-flag
)
1142 (setq file-precious-flag t
)
1143 (make-local-variable 'desktop-save-buffer
)
1144 (setq desktop-save-buffer t
))
1146 ;; Handle M-x revert-buffer done in an rmail-mode buffer.
1147 (defun rmail-revert (arg noconfirm
)
1148 (set-buffer rmail-buffer
)
1149 (let* ((revert-buffer-function (default-value 'revert-buffer-function
))
1150 (rmail-enable-multibyte enable-multibyte-characters
)
1151 ;; See similar code in `rmail'.
1152 (coding-system-for-read (and rmail-enable-multibyte
'raw-text
)))
1153 ;; Call our caller again, but this time it does the default thing.
1154 (if (revert-buffer arg noconfirm
)
1155 ;; If the user said "yes", and we changed something,
1156 ;; reparse the messages.
1158 (set-buffer rmail-buffer
)
1160 ;; Convert all or part to Babyl file if possible.
1161 (rmail-convert-file)
1162 ;; We have read the file as raw-text, so the buffer is set to
1163 ;; unibyte. Make it multibyte if necessary.
1164 (if (and rmail-enable-multibyte
1165 (not enable-multibyte-characters
))
1166 (set-buffer-multibyte t
))
1167 (goto-char (point-max))
1168 (rmail-set-message-counters)
1169 (rmail-show-message rmail-total-messages
)
1170 (run-hooks 'rmail-mode-hook
)))))
1172 ;; Return a list of files from this buffer's Mail: option.
1173 ;; Does not assume that messages have been parsed.
1174 ;; Just returns nil if buffer does not look like Babyl format.
1175 (defun rmail-parse-file-inboxes ()
1180 (cond ((looking-at "BABYL OPTIONS:")
1181 (search-forward "\n\^_" nil
'move
)
1182 (narrow-to-region 1 (point))
1184 (if (search-forward "\nMail:" nil t
)
1186 (narrow-to-region (point) (progn (end-of-line) (point)))
1187 (goto-char (point-min))
1188 (mail-parse-comma-list))))))))
1190 (defun rmail-expunge-and-save ()
1191 "Expunge and save RMAIL file."
1194 (set-buffer rmail-buffer
)
1196 (if (rmail-summary-exists)
1197 (rmail-select-summary (set-buffer-modified-p nil
))))
1199 (defun rmail-quit ()
1201 Hook `rmail-quit-hook' is run after expunging."
1203 (rmail-expunge-and-save)
1204 (when (boundp 'rmail-quit-hook
)
1205 (run-hooks 'rmail-quit-hook
))
1206 ;; Don't switch to the summary buffer even if it was recently visible.
1207 (when rmail-summary-buffer
1208 (replace-buffer-in-windows rmail-summary-buffer
)
1209 (bury-buffer rmail-summary-buffer
))
1210 (if rmail-enable-mime
1211 (let ((obuf rmail-buffer
)
1212 (ovbuf rmail-view-buffer
))
1213 (set-buffer rmail-view-buffer
)
1215 (replace-buffer-in-windows ovbuf
)
1216 (replace-buffer-in-windows obuf
)
1218 (let ((obuf (current-buffer)))
1220 (replace-buffer-in-windows obuf
))))
1222 (defun rmail-bury ()
1223 "Bury current Rmail buffer and its summary buffer."
1225 ;; This let var was called rmail-buffer, but that interfered
1226 ;; with the buffer-local var used in summary buffers.
1227 (let ((buffer-to-bury (current-buffer)))
1228 (if (rmail-summary-exists)
1230 (while (setq window
(get-buffer-window rmail-summary-buffer
))
1231 (quit-window nil window
))
1232 (bury-buffer rmail-summary-buffer
)))
1235 (defun rmail-duplicate-message ()
1236 "Create a duplicated copy of the current message.
1237 The duplicate copy goes into the Rmail file just after the
1241 (let ((buffer-read-only nil
)
1242 (number rmail-current-message
)
1243 (string (buffer-substring (rmail-msgbeg rmail-current-message
)
1244 (rmail-msgend rmail-current-message
))))
1245 (goto-char (rmail-msgend rmail-current-message
))
1247 (rmail-forget-messages)
1248 (rmail-show-message number
)
1249 (message "Message duplicated")))
1252 (defun rmail-input (filename)
1253 "Run Rmail on file FILENAME."
1254 (interactive "FRun rmail on RMAIL file: ")
1258 ;; This used to scan subdirectories recursively, but someone pointed out
1259 ;; that if the user wants that, person can put all the files in one dir.
1260 ;; And the recursive scan was slow. So I took it out.
1262 (defun rmail-find-all-files (start)
1263 "Return list of file in dir START that match `rmail-secondary-file-regexp'."
1264 (if (file-accessible-directory-p start
)
1266 (let* ((case-fold-search t
)
1267 (files (directory-files start t rmail-secondary-file-regexp
)))
1268 ;; Sort here instead of in directory-files
1269 ;; because this list is usually much shorter.
1270 (sort files
'string
<))))
1272 (defun rmail-list-to-menu (menu-name l action
&optional full-name
)
1273 (let ((menu (make-sparse-keymap menu-name
)))
1275 (function (lambda (item)
1280 (rmail-list-to-menu (car item
) (cdr item
)
1283 (concat full-name
"/"
1286 (setq name
(car item
)))
1290 (list 'lambda
() '(interactive)
1294 (concat full-name
"/" item
)
1296 rmail-secondary-file-directory
))))))
1297 (define-key menu
(vector (intern name
))
1298 (cons name command
)))))
1302 ;; This command is always "disabled" when it appears in a menu.
1303 (put 'rmail-disable-menu
'menu-enable
''nil
)
1305 (defun rmail-construct-io-menu ()
1306 (let ((files (rmail-find-all-files rmail-secondary-file-directory
)))
1309 (define-key rmail-mode-map
[menu-bar classify input-menu
]
1310 (cons "Input Rmail File"
1311 (rmail-list-to-menu "Input Rmail File"
1314 (define-key rmail-mode-map
[menu-bar classify output-menu
]
1315 (cons "Output Rmail File"
1316 (rmail-list-to-menu "Output Rmail File"
1318 'rmail-output-to-rmail-file
))))
1320 (define-key rmail-mode-map
[menu-bar classify input-menu
]
1321 '("Input Rmail File" . rmail-disable-menu
))
1322 (define-key rmail-mode-map
[menu-bar classify output-menu
]
1323 '("Output Rmail File" . rmail-disable-menu
)))))
1326 ;;;; *** Rmail input ***
1328 ;; RLK feature not added in this version:
1329 ;; argument specifies inbox file or files in various ways.
1331 (defun rmail-get-new-mail (&optional file-name
)
1332 "Move any new mail from this RMAIL file's inbox files.
1333 The inbox files can be specified with the file's Mail: option. The
1334 variable `rmail-primary-inbox-list' specifies the inboxes for your
1335 primary RMAIL file if it has no Mail: option. By default, this is
1336 your /usr/spool/mail/$USER.
1338 You can also specify the file to get new mail from. In this case, the
1339 file of new mail is not changed or deleted. Noninteractively, you can
1340 pass the inbox file name as an argument. Interactively, a prefix
1341 argument causes us to read a file name and use that file as the inbox.
1343 If the variable `rmail-preserve-inbox' is non-nil, new mail will
1344 always be left in inbox files rather than deleted.
1346 This function runs `rmail-get-new-mail-hook' before saving the updated file.
1347 It returns t if it got any new messages."
1349 (list (if current-prefix-arg
1350 (read-file-name "Get new mail from file: "))))
1351 (run-hooks 'rmail-before-get-new-mail-hook
)
1352 ;; If the disk file has been changed from under us,
1353 ;; revert to it before we get new mail.
1354 (or (verify-visited-file-modtime (current-buffer))
1355 (find-file (buffer-file-name)))
1356 (set-buffer rmail-buffer
)
1357 (rmail-maybe-set-message-counters)
1359 ;; Get rid of all undo records for this buffer.
1360 (or (eq buffer-undo-list t
)
1361 (setq buffer-undo-list nil
))
1362 (let ((all-files (if file-name
(list file-name
)
1364 (rmail-enable-multibyte (default-value 'enable-multibyte-characters
))
1369 (let ((opoint (point))
1371 (rsf-number-of-spam 0)
1373 ;; If buffer has not changed yet, and has not been saved yet,
1374 ;; don't replace the old backup file now.
1375 (make-backup-files (and make-backup-files
(buffer-modified-p)))
1376 (buffer-read-only nil
)
1377 ;; Don't make undo records for what we do in getting mail.
1378 (buffer-undo-list t
)
1380 ;; Files to insert this time around.
1382 ;; Last names of those files.
1384 ;; Pull files off all-files onto files
1385 ;; as long as there is no name conflict.
1386 ;; A conflict happens when two inbox file names
1387 ;; have the same last component.
1388 (while (and all-files
1389 (not (member (file-name-nondirectory (car all-files
))
1391 (setq files
(cons (car all-files
) files
)
1393 (cons (file-name-nondirectory (car all-files
)) files
))
1394 (setq all-files
(cdr all-files
)))
1395 ;; Put them back in their original order.
1396 (setq files
(nreverse files
))
1398 (goto-char (point-max))
1399 (skip-chars-backward " \t\n") ; just in case of brain damage
1400 (delete-region (point) (point-max)) ; caused by require-final-newline
1403 (narrow-to-region (point) (point))
1404 ;; Read in the contents of the inbox files,
1405 ;; renaming them as necessary,
1406 ;; and adding to the list of files to delete eventually.
1408 (rmail-insert-inbox-text files nil
)
1409 (setq delete-files
(rmail-insert-inbox-text files t
)))
1410 ;; Scan the new text and convert each message to babyl format.
1411 (goto-char (point-min))
1414 (setq new-messages
(rmail-convert-to-babyl-format)
1416 ;; Try to delete the garbage just inserted.
1417 (or success
(delete-region (point-min) (point-max)))
1418 ;; If we could not convert the file's inboxes,
1419 ;; rename the files we tried to read
1420 ;; so we won't over and over again.
1421 (if (and (not file-name
) (not success
))
1422 (let ((delfiles delete-files
)
1425 (while (file-exists-p (format "RMAILOSE.%d" count
))
1426 (setq count
(1+ count
)))
1427 (rename-file (car delfiles
)
1428 (format "RMAILOSE.%d" count
))
1429 (setq delfiles
(cdr delfiles
))))))
1430 (or (zerop new-messages
)
1433 (search-backward "\n\^_" nil t
)
1434 (narrow-to-region (point) (point-max))
1435 (goto-char (1+ (point-min)))
1436 (rmail-count-new-messages)
1437 (run-hooks 'rmail-get-new-mail-hook
)
1439 ;; Delete the old files, now that babyl file is saved.
1442 ;; First, try deleting.
1444 (delete-file (car delete-files
))
1446 ;; If we can't delete it, truncate it.
1447 (write-region (point) (point) (car delete-files
))))
1449 (setq delete-files
(cdr delete-files
)))))
1450 (if (= new-messages
0)
1451 (progn (goto-char opoint
)
1452 (if (or file-name rmail-inbox-list
)
1453 (message "(No new mail has arrived)")))
1454 ;; check new messages to see if any of them is spam:
1455 (if (and (featurep 'rmail-spam-filter
)
1456 rmail-use-spam-filter
)
1458 ((old-messages (- rmail-total-messages new-messages
))
1459 (rsf-scanned-message-number (1+ old-messages
))
1460 ;; save deletion flags of old messages: vector starts
1461 ;; at zero (is one longer that no of messages),
1462 ;; therefore take 1+ old-messages
1464 (substring rmail-deleted-vector
0 (1+
1466 ;; set all messages to undeleted
1467 (setq rmail-deleted-vector
1468 (make-string (1+ rmail-total-messages
) ?\
))
1469 (while (<= rsf-scanned-message-number
1470 rmail-total-messages
)
1472 (if (not (rmail-spam-filter rsf-scanned-message-number
))
1473 (progn (setq rsf-number-of-spam
(1+ rsf-number-of-spam
)))
1475 (setq rsf-scanned-message-number
(1+ rsf-scanned-message-number
))
1477 (if (> rsf-number-of-spam
0)
1479 (when (rmail-expunge-confirmed)
1480 (rmail-only-expunge t
))
1482 (setq rmail-deleted-vector
1485 (make-string (- rmail-total-messages old-messages
)
1488 (if (rmail-summary-exists)
1489 (rmail-select-summary
1490 (rmail-update-summary)))
1491 (message "%d new message%s read%s"
1492 new-messages
(if (= 1 new-messages
) "" "s")
1493 ;; print out a message on number of spam messages found:
1494 (if (and (featurep 'rmail-spam-filter
)
1495 rmail-use-spam-filter
1496 (> rsf-number-of-spam
0))
1497 (if (= 1 new-messages
)
1498 ", and found to be a spam message"
1499 (if (> rsf-number-of-spam
1)
1500 (format ", %d of which found to be spam messages"
1502 ", one of which found to be a spam message"))
1504 (if (and (featurep 'rmail-spam-filter
)
1505 rmail-use-spam-filter
1506 (> rsf-number-of-spam
0))
1507 (progn (if rsf-beep
(beep t
))
1508 (sleep-for rsf-sleep-after-message
)))
1510 ;; Move to the first new message
1511 ;; unless we have other unseen messages before it.
1512 (rmail-show-message (rmail-first-unseen-message))
1513 (run-hooks 'rmail-after-get-new-mail-hook
)
1516 ;; Don't leave the buffer screwed up if we get a disk-full error.
1517 (or found
(rmail-show-message)))))
1519 (defun rmail-insert-inbox-text (files renamep
)
1520 ;; Detect a locked file now, so that we avoid moving mail
1521 ;; out of the real inbox file. (That could scare people.)
1522 (or (memq (file-locked-p buffer-file-name
) '(nil t
))
1523 (error "RMAIL file %s is locked"
1524 (file-name-nondirectory buffer-file-name
)))
1525 (let (file tofile delete-files movemail popmail got-password password
)
1527 ;; Handle POP mailbox names specially; don't expand as filenames
1528 ;; in case the userid contains a directory separator.
1529 (setq file
(car files
))
1530 (setq popmail
(string-match "^po:" file
))
1533 (setq file
(file-truename
1534 (substitute-in-file-name (expand-file-name file
)))))
1535 (setq tofile
(expand-file-name
1536 ;; Generate name to move to from inbox name,
1537 ;; in case of multiple inboxes that need moving.
1538 (concat ".newmail-" (file-name-nondirectory file
))
1539 ;; Use the directory of this rmail file
1540 ;; because it's a nuisance to use the homedir
1541 ;; if that is on a full disk and this rmail
1543 (file-name-directory
1544 (expand-file-name buffer-file-name
))))
1545 ;; Always use movemail to rename the file,
1546 ;; since there can be mailboxes in various directories.
1548 ;;; ;; If getting from mail spool directory,
1549 ;;; ;; use movemail to move rather than just renaming,
1550 ;;; ;; so as to interlock with the mailer.
1551 ;;; (setq movemail (string= file
1553 ;;; (concat rmail-spool-directory
1554 ;;; (file-name-nondirectory file)))))
1555 (if (and movemail
(not popmail
))
1557 ;; On some systems, /usr/spool/mail/foo is a directory
1558 ;; and the actual inbox is /usr/spool/mail/foo/foo.
1559 (if (file-directory-p file
)
1560 (setq file
(expand-file-name (user-login-name)
1563 (if rmail-pop-password-required
1564 (progn (setq got-password
(not (rmail-have-password)))
1565 (setq password
(rmail-get-pop-password))))
1566 (if (memq system-type
'(windows-nt cygwin
))
1567 ;; cannot have "po:" in file name
1570 (concat ".newmail-pop-"
1571 (file-name-nondirectory (substring file
3)))
1572 (file-name-directory
1573 (expand-file-name buffer-file-name
)))))
1574 (message "Getting mail from post office ..."))
1575 ((and (file-exists-p tofile
)
1576 (/= 0 (nth 7 (file-attributes tofile
))))
1577 (message "Getting mail from %s..." tofile
))
1578 ((and (file-exists-p file
)
1579 (/= 0 (nth 7 (file-attributes file
))))
1580 (message "Getting mail from %s..." file
)))
1581 ;; Set TOFILE if have not already done so, and
1582 ;; rename or copy the file FILE to TOFILE if and as appropriate.
1583 (cond ((not renamep
)
1585 ((or (file-exists-p tofile
) (and (not popmail
)
1586 (not (file-exists-p file
))))
1588 ((and (not movemail
) (not popmail
))
1589 ;; Try copying. If that fails (perhaps no space) and
1590 ;; we're allowed to blow away the inbox, rename instead.
1591 (if rmail-preserve-inbox
1592 (copy-file file tofile nil
)
1594 (copy-file file tofile nil
)
1596 ;; Third arg is t so we can replace existing file TOFILE.
1597 (rename-file file tofile t
))))
1598 ;; Make the real inbox file empty.
1599 ;; Leaving it deleted could cause lossage
1600 ;; because mailers often won't create the file.
1601 (if (not rmail-preserve-inbox
)
1603 (write-region (point) (point) file
)
1609 (setq errors
(generate-new-buffer " *rmail loss*"))
1610 (buffer-disable-undo errors
)
1613 (list (or rmail-movemail-program
1614 (expand-file-name "movemail"
1617 (if rmail-preserve-inbox
1620 rmail-movemail-flags
1622 (if password
(list password
) nil
))))
1623 (apply 'call-process args
))
1624 (if (not (buffer-modified-p errors
))
1625 ;; No output => movemail won
1628 (subst-char-in-region (point-min) (point-max)
1630 (goto-char (point-max))
1631 (skip-chars-backward " \t")
1632 (delete-region (point) (point-max))
1633 (goto-char (point-min))
1634 (if (looking-at "movemail: ")
1635 (delete-region (point-min) (match-end 0)))
1637 (message "movemail: %s"
1638 (buffer-substring (point-min)
1640 ;; If we just read the password, most likely it is
1641 ;; wrong. Otherwise, see if there is a specific
1642 ;; reason to think that the problem is a wrong passwd.
1643 (if (or got-password
1644 (re-search-forward rmail-pop-password-error
1646 (rmail-set-pop-password nil
))
1649 (if errors
(kill-buffer errors
))))))
1650 ;; At this point, TOFILE contains the name to read:
1651 ;; Either the alternate name (if we renamed)
1652 ;; or the actual inbox (if not renaming).
1653 (if (file-exists-p tofile
)
1654 (let ((coding-system-for-read 'no-conversion
)
1656 (goto-char (point-max))
1657 (setq size
(nth 1 (insert-file-contents tofile
)))
1658 (goto-char (point-max))
1659 (or (= (preceding-char) ?
\n)
1662 (if (not (and rmail-preserve-inbox
(string= file tofile
)))
1663 (setq delete-files
(cons tofile delete-files
)))))
1665 (setq files
(cdr files
)))
1668 ;; Decode the region specified by FROM and TO by CODING.
1669 ;; If CODING is nil or an invalid coding system, decode by `undecided'.
1670 (defun rmail-decode-region (from to coding
)
1671 (if (or (not coding
) (not (coding-system-p coding
)))
1672 (setq coding
'undecided
))
1673 ;; Use -dos decoding, to remove ^M characters left from base64 or
1674 ;; rogue qp-encoded text.
1675 (decode-coding-region from to
1676 (coding-system-change-eol-conversion coding
1))
1677 ;; Don't reveal the fact we used -dos decoding, as users generally
1678 ;; will not expect the RMAIL buffer to use DOS EOL format.
1679 (setq buffer-file-coding-system
1680 (setq last-coding-system-used
1681 (coding-system-change-eol-conversion coding
0))))
1683 ;; the rmail-break-forwarded-messages feature is not implemented
1684 (defun rmail-convert-to-babyl-format ()
1685 (let ((count 0) start
1686 (case-fold-search nil
)
1687 (invalid-input-resync
1688 (function (lambda ()
1689 (message "Invalid Babyl format in inbox!")
1691 ;; Try to get back in sync with a real message.
1692 (if (re-search-forward
1693 (concat rmail-mmdf-delim1
"\\|^From") nil t
)
1695 (goto-char (point-max)))))))
1696 (goto-char (point-min))
1699 (setq start
(point))
1700 (cond ((looking-at "BABYL OPTIONS:");Babyl header
1701 (if (search-forward "\n\^_" nil t
)
1702 ;; If we find the proper terminator, delete through there.
1703 (delete-region (point-min) (point))
1704 (funcall invalid-input-resync
)
1705 (delete-region (point-min) (point))))
1706 ;; Babyl format message
1708 (or (search-forward "\n\^_" nil t
)
1709 (funcall invalid-input-resync
))
1710 (setq count
(1+ count
))
1711 ;; Make sure there is no extra white space after the ^_
1712 ;; at the end of the message.
1713 ;; Narrowing will make sure that whatever follows the junk
1714 ;; will be treated properly.
1715 (delete-region (point)
1717 (skip-chars-forward " \t\n")
1725 (if (looking-at "0")
1729 (narrow-to-region (point) (point-max))
1732 (case-fold-search t
)
1733 (quoted-printable-header-field-end
1737 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
1739 (base64-header-field-end
1743 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
1745 (if quoted-printable-header-field-end
1748 (mail-unquote-printable-region header-end
(point) nil t t
)
1749 (message "Malformed MIME quoted-printable message"))
1750 ;; Change "quoted-printable" to "8bit",
1751 ;; to reflect the decoding we just did.
1752 (goto-char quoted-printable-header-field-end
)
1753 (delete-region (point) (search-backward ":"))
1755 (if base64-header-field-end
1760 (base64-decode-region (1+ header-end
)
1764 ;; Change "base64" to "8bit", to reflect the
1765 ;; decoding we just did.
1766 (goto-char base64-header-field-end
)
1767 (delete-region (point) (search-backward ":"))
1768 (insert ": 8bit"))))
1769 (setq last-coding-system-used nil
)
1770 (or rmail-enable-mime
1771 (not rmail-enable-multibyte
)
1773 (if (and rmail-decode-mime-charset
1776 (search-forward "\n\n" nil t
)
1777 (let ((case-fold-search t
))
1779 rmail-mime-charset-pattern
1781 (intern (downcase (match-string 1))))))
1782 (rmail-decode-region start
(point) mime-charset
)))))
1783 ;; Add an X-Coding-System: header if we don't have one.
1787 (if (looking-at "0")
1790 (or (save-restriction
1791 (narrow-to-region (point) (point-max))
1793 (goto-char (point-min))
1794 (re-search-forward "^X-Coding-System:" nil t
))
1795 (insert "X-Coding-System: "
1796 (symbol-name last-coding-system-used
)
1798 (narrow-to-region (point) (point-max))
1799 (and (= 0 (% count
10))
1800 (message "Converting to Babyl format...%d" count
)))
1802 ((let ((case-fold-search t
))
1803 (looking-at rmail-mmdf-delim1
))
1804 (let ((case-fold-search t
))
1805 (replace-match "\^L\n0, unseen,,\n*** EOOH ***\n")
1806 (re-search-forward rmail-mmdf-delim2 nil t
)
1807 (replace-match "\^_"))
1810 (narrow-to-region start
(1- (point)))
1811 (goto-char (point-min))
1812 (while (search-forward "\n\^_" nil t
); single char "\^_"
1813 (replace-match "\n^_")))); 2 chars: "^" and "_"
1814 (setq last-coding-system-used nil
)
1815 (or rmail-enable-mime
1816 (not rmail-enable-multibyte
)
1817 (decode-coding-region start
(point) 'undecided
))
1821 (insert "X-Coding-System: "
1822 (symbol-name last-coding-system-used
)
1824 (narrow-to-region (point) (point-max))
1825 (setq count
(1+ count
))
1826 (and (= 0 (% count
10))
1827 (message "Converting to Babyl format...%d" count
)))
1829 ((looking-at "^From ")
1830 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
1831 (rmail-nuke-pinhead-header)
1832 ;; If this message has a Content-Length field,
1833 ;; skip to the end of the contents.
1834 (let* ((header-end (save-excursion
1835 (and (re-search-forward "\n\n" nil t
)
1837 (case-fold-search t
)
1838 (quoted-printable-header-field-end
1841 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
1843 (base64-header-field-end
1846 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
1849 ;; Get the numeric value from the Content-Length field.
1851 ;; Back up to end of prev line,
1852 ;; in case the Content-Length field comes first.
1854 (and (search-forward "\ncontent-length: "
1857 (eol (progn (end-of-line) (point))))
1858 (string-to-int (buffer-substring beg eol
)))))))
1860 (if (and (natnump size
)
1861 (<= (+ header-end size
) (point-max))
1862 ;; Make sure this would put us at a position
1863 ;; that we could continue from.
1865 (goto-char (+ header-end size
))
1866 (skip-chars-forward "\n")
1868 (and (looking-at "BABYL OPTIONS:")
1869 (search-forward "\n\^_" nil t
))
1870 (and (looking-at "\^L")
1871 (search-forward "\n\^_" nil t
))
1872 (let ((case-fold-search t
))
1873 (looking-at rmail-mmdf-delim1
))
1874 (looking-at "From "))))
1875 (goto-char (+ header-end size
))
1876 (message "Ignoring invalid Content-Length field")
1878 (if (let ((case-fold-search nil
))
1880 (concat "^[\^_]?\\("
1881 rmail-unix-mail-delimiter
1883 rmail-mmdf-delim1
"\\|"
1884 "^BABYL OPTIONS:\\|"
1885 "\^L\n[01],\\)") nil t
))
1886 (goto-char (match-beginning 1))
1887 (goto-char (point-max)))
1888 (setq count
(1+ count
))
1889 (if quoted-printable-header-field-end
1892 (mail-unquote-printable-region header-end
(point) nil t t
)
1893 (message "Malformed MIME quoted-printable message"))
1894 ;; Change "quoted-printable" to "8bit",
1895 ;; to reflect the decoding we just did.
1896 (goto-char quoted-printable-header-field-end
)
1897 (delete-region (point) (search-backward ":"))
1899 (if base64-header-field-end
1904 (base64-decode-region
1907 ;; Prevent base64-decode-region
1908 ;; from removing newline characters.
1909 (skip-chars-backward "\n\t ")
1913 ;; Change "base64" to "8bit", to reflect the
1914 ;; decoding we just did.
1915 (goto-char base64-header-field-end
)
1916 (delete-region (point) (search-backward ":"))
1917 (insert ": 8bit")))))
1921 (narrow-to-region start
(point))
1922 (goto-char (point-min))
1923 (while (search-forward "\n\^_" nil t
); single char
1924 (replace-match "\n^_")))); 2 chars: "^" and "_"
1925 ;; This is for malformed messages that don't end in newline.
1926 ;; There shouldn't be any, but some users say occasionally
1928 (or (bolp) (newline))
1930 (setq last-coding-system-used nil
)
1931 (or rmail-enable-mime
1932 (not rmail-enable-multibyte
)
1934 (if (and rmail-decode-mime-charset
1937 (search-forward "\n\n" nil t
)
1938 (let ((case-fold-search t
))
1940 rmail-mime-charset-pattern
1942 (intern (downcase (match-string 1))))))
1943 (rmail-decode-region start
(point) mime-charset
)))
1947 (insert "X-Coding-System: "
1948 (symbol-name last-coding-system-used
)
1950 (narrow-to-region (point) (point-max))
1951 (and (= 0 (% count
10))
1952 (message "Converting to Babyl format...%d" count
)))
1954 ;; This kludge is because some versions of sendmail.el
1955 ;; insert an extra newline at the beginning that shouldn't
1956 ;; be there. sendmail.el has been fixed, but old versions
1957 ;; may still be in use. -- rms, 7 May 1993.
1958 ((eolp) (delete-char 1))
1959 (t (error "Cannot convert to babyl format")))))
1962 ;; Delete the "From ..." line, creating various other headers with
1963 ;; information from it if they don't already exist. Now puts the
1964 ;; original line into a mail-from: header line for debugging and for
1965 ;; use by the rmail-output function.
1966 (defun rmail-nuke-pinhead-header ()
1969 (let ((start (point))
1972 (search-forward "\n\n")
1974 (goto-char (point-max))
1978 (narrow-to-region start end
)
1979 (let ((case-fold-search t
))
1981 (setq has-from
(search-forward "\nFrom:" nil t
))
1983 (setq has-date
(and (search-forward "\nDate:" nil t
) (point)))
1985 (let ((case-fold-search nil
))
1986 (if (re-search-forward (concat "^" rmail-unix-mail-delimiter
) nil t
)
1990 ;; Keep and reformat the date if we don't
1991 ;; have a Date: field.
1995 "Date: \\2, \\4 \\3 \\9 \\5 "
1997 ;; The timezone could be matched by group 7 or group 10.
1998 ;; If neither of them matched, assume EST, since only
1999 ;; Easterners would be so sloppy.
2000 ;; It's a shame the substitution can't use "\\10".
2002 ((/= (match-beginning 7) (match-end 7)) "\\7")
2003 ((/= (match-beginning 10) (match-end 10))
2004 (buffer-substring (match-beginning 10)
2008 ;; Keep and reformat the sender if we don't
2009 ;; have a From: field.
2015 ;;;; *** Rmail Message Formatting and Header Manipulation ***
2017 (defun rmail-reformat-message (beg end
)
2020 (if (/= (following-char) ?
0)
2021 (error "Bad format in RMAIL file"))
2022 (let ((inhibit-read-only t
)
2023 (delta (- (buffer-size) end
)))
2027 (let ((case-fold-search t
))
2028 (while (looking-at "Summary-line:\\|Mail-From:")
2030 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*\n")
2031 (delete-region (point)
2032 (progn (forward-line 1) (point))))
2033 (let ((str (buffer-substring (point)
2034 (save-excursion (search-forward "\n\n" end
'move
)
2036 (insert str
"*** EOOH ***\n")
2037 (narrow-to-region (point) (- (buffer-size) delta
)))
2038 (goto-char (point-min))
2039 (if rmail-message-filter
(funcall rmail-message-filter
))
2040 (if (or rmail-displayed-headers rmail-ignored-headers
)
2041 (rmail-clear-headers))))
2043 (defun rmail-clear-headers (&optional ignored-headers
)
2044 "Delete all header fields that Rmail should not show.
2045 If the optional argument IGNORED-HEADERS is non-nil,
2046 delete all header fields whose names match that regexp.
2047 Otherwise, if `rmail-displayed-headers' is non-nil,
2048 delete all header fields *except* those whose names match that regexp.
2049 Otherwise, delete all header fields whose names match `rmail-ignored-headers'."
2050 (when (search-forward "\n\n" nil t
)
2052 (let ((case-fold-search t
)
2053 (buffer-read-only nil
))
2054 (if (and rmail-displayed-headers
(null ignored-headers
))
2056 (narrow-to-region (point-min) (point))
2058 (goto-char (point-min))
2059 (while (and (not (eobp))
2061 (if (re-search-forward "\n[^ \t]" nil t
)
2062 (setq lim
(match-beginning 0)
2064 (setq lim nil next
(point-max)))))
2066 (re-search-forward rmail-displayed-headers lim t
))
2068 (delete-region (point) next
))))
2069 (goto-char (point-min)))
2070 (or ignored-headers
(setq ignored-headers rmail-ignored-headers
))
2072 (narrow-to-region (point-min) (point))
2073 (while (and ignored-headers
2075 (goto-char (point-min))
2076 (re-search-forward ignored-headers nil t
)))
2078 (delete-region (point)
2079 (if (re-search-forward "\n[^ \t]" nil t
)
2083 (defun rmail-msg-is-pruned ()
2084 (rmail-maybe-set-message-counters)
2086 (narrow-to-region (rmail-msgbeg rmail-current-message
) (point-max))
2088 (goto-char (point-min))
2090 (= (following-char) ?
1))))
2092 (defun rmail-msg-restore-non-pruned-header ()
2093 (let ((old-point (point))
2096 (inhibit-read-only t
))
2098 (narrow-to-region (rmail-msgbeg rmail-current-message
) (point-max))
2099 (goto-char (point-min))
2104 ;; Insert new EOOH line at the proper place.
2106 (let ((case-fold-search t
))
2107 (while (looking-at "Summary-Line:\\|Mail-From:")
2109 (insert "*** EOOH ***\n")
2110 (setq new-start
(point))
2111 ;; Delete the old reformatted header.
2113 (search-forward "\n*** EOOH ***\n")
2115 (let ((start (point)))
2116 (search-forward "\n\n")
2117 (if (and (<= start old-point
)
2118 (<= old-point
(point)))
2119 (setq new-point new-start
))
2120 (delete-region start
(point)))
2121 ;; Narrow to after the new EOOH line.
2122 (narrow-to-region new-start
(point-max)))
2124 (goto-char new-point
))))
2126 (defun rmail-msg-prune-header ()
2128 (= (point) (point-min))))
2130 (narrow-to-region (rmail-msgbeg rmail-current-message
) (point-max))
2131 (rmail-reformat-message (point-min) (point-max)))
2133 (goto-char (point-min)))))
2135 (defun rmail-toggle-header (&optional arg
)
2136 "Show original message header if pruned header currently shown, or vice versa.
2137 With argument ARG, show the message header pruned if ARG is greater than zero;
2138 otherwise, show it in full."
2140 (let* ((pruned (with-current-buffer rmail-buffer
2141 (rmail-msg-is-pruned)))
2143 (> (prefix-numeric-value arg
) 0)
2145 (if (eq pruned prune
)
2147 (set-buffer rmail-buffer
)
2148 (rmail-maybe-set-message-counters)
2149 (if rmail-enable-mime
2150 (let ((buffer-read-only nil
))
2152 (rmail-msg-restore-non-pruned-header)
2153 (rmail-msg-prune-header))
2154 (funcall rmail-show-mime-function
))
2155 (let* ((buffer-read-only nil
)
2156 (window (get-buffer-window (current-buffer)))
2157 (at-point-min (= (point) (point-min)))
2158 (all-headers-visible (= (window-start window
) (point-min)))
2161 (and (not (search-backward "\n\n" nil t
))
2164 (re-search-backward "^[-A-Za-z0-9]+:" nil t
))
2167 (rmail-count-screen-lines (window-start window
) (point))))
2169 (rmail-msg-restore-non-pruned-header)
2170 (rmail-msg-prune-header))
2172 (goto-char (point-min)))
2174 (goto-char (point-min))
2175 (search-forward "\n\n")
2176 (or (re-search-backward
2177 (concat "^" (regexp-quote on-header
)) nil t
)
2178 (goto-char (point-min))))
2180 (save-selected-window
2181 (select-window window
)
2182 (recenter old-screen-line
)
2183 (if (and all-headers-visible
2184 (not (= (window-start) (point-min))))
2185 (recenter (- (window-height) 2))))))))
2186 (rmail-highlight-headers))))
2188 (defun rmail-narrow-to-non-pruned-header ()
2189 "Narrow to the whole (original) header of the current message."
2191 (narrow-to-region (rmail-msgbeg rmail-current-message
) (point-max))
2192 (goto-char (point-min))
2194 (if (= (following-char) ?
1)
2197 (setq start
(point))
2198 (search-forward "*** EOOH ***\n")
2199 (setq end
(match-beginning 0)))
2201 (setq start
(point))
2202 (search-forward "\n\n")
2203 (setq end
(1- (point))))
2204 (narrow-to-region start end
)
2207 ;; Lifted from repos-count-screen-lines.
2208 ;; Return number of screen lines between START and END.
2209 (defun rmail-count-screen-lines (start end
)
2212 (narrow-to-region start end
)
2213 (goto-char (point-min))
2214 (vertical-motion (- (point-max) (point-min))))))
2216 ;;;; *** Rmail Attributes and Keywords ***
2218 ;; Make a string describing current message's attributes and keywords
2219 ;; and set it up as the name of a minor mode
2220 ;; so it will appear in the mode line.
2221 (defun rmail-display-labels ()
2222 (let ((blurb "") (beg (point-min-marker)) (end (point-max-marker)))
2227 (goto-char (rmail-msgbeg rmail-current-message
))
2229 (if (looking-at "[01],")
2231 (narrow-to-region (point) (progn (end-of-line) (point)))
2232 ;; Truly valid BABYL format requires a space before each
2233 ;; attribute or keyword name. Put them in if missing.
2234 (let (buffer-read-only)
2235 (goto-char (point-min))
2236 (while (search-forward "," nil t
)
2237 (or (looking-at "[ ,]") (eobp)
2239 (goto-char (point-max))
2240 (if (search-backward ",," nil
'move
)
2242 (if (> (point) (1+ (point-min)))
2243 (setq blurb
(buffer-substring (+ 1 (point-min)) (point))))
2244 (if (> (- (point-max) (point)) 2)
2248 (buffer-substring (+ (point) 3)
2249 (1- (point-max)))))))))))
2250 ;; Note: we don't use save-restriction because that does not work right
2251 ;; if changes are made outside the saved restriction
2252 ;; before that restriction is restored.
2253 (narrow-to-region beg end
)
2254 (set-marker beg nil
)
2255 (set-marker end nil
)))
2256 (while (string-match " +," blurb
)
2257 (setq blurb
(concat (substring blurb
0 (match-beginning 0)) ","
2258 (substring blurb
(match-end 0)))))
2259 (while (string-match ", +" blurb
)
2260 (setq blurb
(concat (substring blurb
0 (match-beginning 0)) ","
2261 (substring blurb
(match-end 0)))))
2262 (setq mode-line-process
2264 rmail-current-message rmail-total-messages blurb
))
2265 ;; If rmail-enable-mime is non-nil, we may have to update
2266 ;; `mode-line-process' of rmail-view-buffer too.
2267 (if (and rmail-enable-mime
2268 (not (eq (current-buffer) rmail-view-buffer
))
2269 (buffer-live-p rmail-view-buffer
))
2270 (let ((mlp mode-line-process
))
2271 (with-current-buffer rmail-view-buffer
2272 (setq mode-line-process mlp
))))))
2274 ;; Turn an attribute of a message on or off according to STATE.
2275 ;; ATTR is the name of the attribute, as a string.
2276 ;; MSGNUM is message number to change; nil means current message.
2277 (defun rmail-set-attribute (attr state
&optional msgnum
)
2278 (set-buffer rmail-buffer
)
2279 (let ((omax (point-max-marker))
2280 (omin (point-min-marker))
2281 (buffer-read-only nil
))
2282 (or msgnum
(setq msgnum rmail-current-message
))
2287 (goto-char (+ 3 (rmail-msgbeg msgnum
)))
2290 (null (search-backward (concat ", " attr
",")
2291 (prog1 (point) (end-of-line)) t
)))))
2292 (or (eq curstate
(not (not state
)))
2294 (delete-region (point) (1- (match-end 0)))
2297 (insert " " attr
","))))
2298 (if (string= attr
"deleted")
2299 (rmail-set-message-deleted-p msgnum state
)))
2300 ;; Note: we don't use save-restriction because that does not work right
2301 ;; if changes are made outside the saved restriction
2302 ;; before that restriction is restored.
2303 (narrow-to-region omin omax
)
2304 (set-marker omin nil
)
2305 (set-marker omax nil
)
2306 (if (= msgnum rmail-current-message
)
2307 (rmail-display-labels))))))
2309 ;; Return t if the attributes/keywords line of msg number MSG
2310 ;; contains a match for the regexp LABELS.
2311 (defun rmail-message-labels-p (msg labels
)
2315 (goto-char (rmail-msgbeg msg
))
2317 (re-search-backward labels
(prog1 (point) (end-of-line)) t
))))
2319 ;;;; *** Rmail Message Selection And Support ***
2321 (defun rmail-msgend (n)
2322 (marker-position (aref rmail-message-vector
(1+ n
))))
2324 (defun rmail-msgbeg (n)
2325 (marker-position (aref rmail-message-vector n
)))
2327 (defun rmail-widen-to-current-msgbeg (function)
2328 "Call FUNCTION with point at start of internal data of current message.
2329 Assumes that bounds were previously narrowed to display the message in Rmail.
2330 The bounds are widened enough to move point where desired, then narrowed
2333 FUNCTION may not change the visible text of the message, but it may
2334 change the invisible header text."
2338 (narrow-to-region (rmail-msgbeg rmail-current-message
)
2340 (goto-char (point-min))
2342 ;; Note: we don't use save-restriction because that does not work right
2343 ;; if changes are made outside the saved restriction
2344 ;; before that restriction is restored.
2345 (narrow-to-region (rmail-msgbeg rmail-current-message
)
2346 (rmail-msgend rmail-current-message
)))))
2348 (defun rmail-forget-messages ()
2350 (if (vectorp rmail-message-vector
)
2352 (v rmail-message-vector
)
2355 (move-marker (aref v i
) nil
)
2357 (setq rmail-message-vector nil
)
2358 (setq rmail-msgref-vector nil
)
2359 (setq rmail-deleted-vector nil
)))
2361 (defun rmail-maybe-set-message-counters ()
2362 (if (not (and rmail-deleted-vector
2363 rmail-message-vector
2364 rmail-current-message
2365 rmail-total-messages
))
2366 (rmail-set-message-counters)))
2368 (defun rmail-count-new-messages (&optional nomsg
)
2369 (let* ((case-fold-search nil
)
2373 (or nomsg
(message "Counting new messages..."))
2374 (goto-char (point-max))
2375 ;; Put at the end of messages-head
2376 ;; the entry for message N+1, which marks
2377 ;; the end of message N. (N = number of messages).
2378 (search-backward "\n\^_")
2380 (setq messages-head
(list (point-marker)))
2381 (rmail-set-message-counters-counter (point-min))
2382 (setq rmail-current-message
(1+ rmail-total-messages
))
2383 (setq rmail-total-messages
2384 (+ rmail-total-messages total-messages
))
2385 (setq rmail-message-vector
2386 (vconcat rmail-message-vector
(cdr messages-head
)))
2387 (aset rmail-message-vector
2388 rmail-current-message
(car messages-head
))
2389 (setq rmail-deleted-vector
2390 (concat rmail-deleted-vector deleted-head
))
2391 (setq rmail-summary-vector
2392 (vconcat rmail-summary-vector
(make-vector total-messages nil
)))
2393 (setq rmail-msgref-vector
2394 (vconcat rmail-msgref-vector
(make-vector total-messages nil
)))
2395 ;; Fill in the new elements of rmail-msgref-vector.
2396 (let ((i (1+ (- rmail-total-messages total-messages
))))
2397 (while (<= i rmail-total-messages
)
2398 (aset rmail-msgref-vector i
(list i
))
2400 (goto-char (point-min))
2401 (or nomsg
(message "Counting new messages...done (%d)" total-messages
))))
2403 (defun rmail-set-message-counters ()
2404 (rmail-forget-messages)
2408 (let* ((point-save (point))
2410 (messages-after-point)
2411 (case-fold-search nil
)
2414 (message "Counting messages...")
2415 (goto-char (point-max))
2416 ;; Put at the end of messages-head
2417 ;; the entry for message N+1, which marks
2418 ;; the end of message N. (N = number of messages).
2419 (search-backward "\n\^_" nil t
)
2420 (if (/= (point) (point-max)) (forward-char 1))
2421 (setq messages-head
(list (point-marker)))
2422 (rmail-set-message-counters-counter (min (point) point-save
))
2423 (setq messages-after-point total-messages
)
2424 (rmail-set-message-counters-counter)
2425 (setq rmail-total-messages total-messages
)
2426 (setq rmail-current-message
2428 (max 1 (- total-messages messages-after-point
))))
2429 (setq rmail-message-vector
2430 (apply 'vector
(cons (point-min-marker) messages-head
))
2431 rmail-deleted-vector
(concat "0" deleted-head
)
2432 rmail-summary-vector
(make-vector rmail-total-messages nil
)
2433 rmail-msgref-vector
(make-vector (1+ rmail-total-messages
) nil
))
2435 (while (<= i rmail-total-messages
)
2436 (aset rmail-msgref-vector i
(list i
))
2438 (message "Counting messages...done")))))
2440 (defun rmail-set-message-counters-counter (&optional stop
)
2441 (let ((start (point))
2443 (while (search-backward "\n\^_\^L" stop t
)
2444 ;; Detect messages that have been added with DOS line endings and
2445 ;; convert the line endings for such messages.
2447 (if (looking-at "\n\^_\^L\r\n")
2448 (let ((buffer-read-only nil
)
2450 (message "Counting messages...(converting line endings)")
2453 (while (search-backward "\r\n" next t
)
2457 (setq messages-head
(cons (point-marker) messages-head
))
2460 (cons (if (search-backward ", deleted,"
2466 (if (zerop (%
(setq total-messages
(1+ total-messages
)) 20))
2467 (message "Counting messages...%d" total-messages
)))))
2469 (defun rmail-beginning-of-message ()
2470 "Show current message starting from the beginning."
2472 (rmail-show-message rmail-current-message
))
2474 (defun rmail-show-message (&optional n no-summary
)
2475 "Show message number N (prefix argument), counting from start of file.
2476 If summary buffer is currently displayed, update current message there also."
2478 (or (eq major-mode
'rmail-mode
)
2479 (switch-to-buffer rmail-buffer
))
2480 (rmail-maybe-set-message-counters)
2482 (if (zerop rmail-total-messages
)
2483 (progn (narrow-to-region (point-min) (1- (point-max)))
2484 (goto-char (point-min))
2485 (setq mode-line-process nil
))
2486 (let (blurb coding-system
)
2488 (setq n rmail-current-message
)
2491 rmail-current-message
1
2492 blurb
"No previous message"))
2493 ((> n rmail-total-messages
)
2494 (setq n rmail-total-messages
2495 rmail-current-message rmail-total-messages
2496 blurb
"No following message"))
2498 (setq rmail-current-message n
))))
2499 (let ((beg (rmail-msgbeg n
)))
2503 (let ((end (rmail-msgend n
)))
2505 (if (prog1 (= (following-char) ?
0)
2507 ;; If there's a Summary-line in the (otherwise empty)
2508 ;; header, we didn't yet get past the EOOH line.
2509 (if (looking-at "^\\*\\*\\* EOOH \\*\\*\\*\n")
2511 (narrow-to-region (point) end
))
2513 (search-forward "\n*** EOOH ***\n" end t
))
2514 (narrow-to-region beg
(point))
2515 (goto-char (point-min))
2516 (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t
)
2517 (let ((coding-system (intern (match-string 1))))
2520 (check-coding-system coding-system
)
2521 (setq buffer-file-coding-system coding-system
))
2523 (setq buffer-file-coding-system nil
))))
2524 (setq buffer-file-coding-system nil
)))))
2525 ;; Clear the "unseen" attribute when we show a message.
2526 (rmail-set-attribute "unseen" nil
)
2527 (let ((end (rmail-msgend n
)))
2528 ;; Reformat the header, or else find the reformatted header.
2529 (if (= (following-char) ?
0)
2530 (rmail-reformat-message beg end
)
2531 (search-forward "\n*** EOOH ***\n" end t
)
2532 (narrow-to-region (point) end
)))
2533 (goto-char (point-min))
2535 (function (lambda (window)
2536 (if (eq (window-buffer window
) (current-buffer))
2537 (set-window-point window
(point)))))
2539 (rmail-display-labels)
2540 (if (eq rmail-enable-mime t
)
2541 (funcall rmail-show-mime-function
)
2542 (setq rmail-view-buffer rmail-buffer
)
2544 (rmail-highlight-headers)
2545 (if transient-mark-mode
(deactivate-mark))
2546 (run-hooks 'rmail-show-message-hook
)
2547 ;; If there is a summary buffer, try to move to this message
2548 ;; in that buffer. But don't complain if this message
2549 ;; is not mentioned in the summary.
2550 ;; Don't do this at all if we were called on behalf
2551 ;; of cursor motion in the summary buffer.
2552 (and (rmail-summary-exists) (not no-summary
)
2553 (let ((curr-msg rmail-current-message
))
2554 (rmail-select-summary
2555 (rmail-summary-goto-msg curr-msg t t
))))
2556 (with-current-buffer rmail-buffer
2559 (message blurb
))))))
2561 (defun rmail-redecode-body (coding)
2562 "Decode the body of the current message using coding system CODING.
2563 This is useful with mail messages that have malformed or missing
2566 This function assumes that the current message is already decoded
2567 and displayed in the RMAIL buffer, but the coding system used to
2568 decode it was incorrect. It then encodes the message back to its
2569 original form, and decodes it again, using the coding system CODING.
2571 Note that if Emacs erroneously auto-detected one of the iso-2022
2572 encodings in the message, this function might fail because the escape
2573 sequences that switch between character sets and also single-shift and
2574 locking-shift codes are impossible to recover. This function is meant
2575 to be used to fix messages encoded with 8-bit encodings, such as
2576 iso-8859, koi8-r, etc."
2577 (interactive "zCoding system for re-decoding this message: ")
2578 (when (not rmail-enable-mime
)
2579 (or (eq major-mode
'rmail-mode
)
2580 (switch-to-buffer rmail-buffer
))
2582 (let ((pruned (rmail-msg-is-pruned)))
2584 (let ((msgbeg (rmail-msgbeg rmail-current-message
))
2585 (msgend (rmail-msgend rmail-current-message
))
2587 ;; We need the message headers pruned (we later restore
2588 ;; the pruned stat to what it was, see the end of
2589 ;; unwind-protect form).
2591 (rmail-toggle-header 1))
2592 (narrow-to-region msgbeg msgend
)
2593 (goto-char (point-min))
2594 (when (search-forward "\n*** EOOH ***\n" (point-max) t
)
2595 (narrow-to-region msgbeg
(point)))
2596 (goto-char (point-min))
2597 (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t
)
2598 (let ((old-coding (intern (match-string 1)))
2599 (buffer-read-only nil
))
2600 (check-coding-system old-coding
)
2601 ;; Make sure the new coding system uses the same EOL
2602 ;; conversion, to prevent ^M characters from popping
2603 ;; up all over the place.
2605 (coding-system-change-eol-conversion
2607 (coding-system-eol-type old-coding
)))
2608 (setq x-coding-header
(point-marker))
2609 (narrow-to-region msgbeg msgend
)
2610 (encode-coding-region (point) msgend old-coding
)
2611 (decode-coding-region (point) msgend coding
)
2612 (setq last-coding-system-used coding
)
2613 ;; Rewrite the coding-system header according
2615 (goto-char x-coding-header
)
2616 (delete-region (point)
2620 (insert "X-Coding-System: "
2621 (symbol-name last-coding-system-used
))
2622 (set-marker x-coding-header nil
)
2623 (rmail-show-message))
2624 (error "No X-Coding-System header found")))
2626 (rmail-toggle-header 0)))))))
2628 ;; Find all occurrences of certain fields, and highlight them.
2629 (defun rmail-highlight-headers ()
2630 ;; Do this only if the system supports faces.
2631 (if (and (fboundp 'internal-find-face
)
2632 rmail-highlighted-headers
)
2634 (search-forward "\n\n" nil
'move
)
2636 (narrow-to-region (point-min) (point))
2637 (let ((case-fold-search t
)
2638 (inhibit-read-only t
)
2639 ;; Highlight with boldface if that is available.
2640 ;; Otherwise use the `highlight' face.
2641 (face (or rmail-highlight-face
2642 (if (face-differs-from-default-p 'bold
)
2644 ;; List of overlays to reuse.
2645 (overlays rmail-overlay-list
))
2646 (goto-char (point-min))
2647 (while (re-search-forward rmail-highlighted-headers nil t
)
2648 (skip-chars-forward " \t")
2651 (while (progn (forward-line 1)
2652 (looking-at "[ \t]")))
2653 ;; Back up over newline, then trailing spaces or tabs
2655 (while (member (preceding-char) '(? ?
\t))
2658 ;; Reuse an overlay we already have.
2660 (setq overlay
(car overlays
)
2661 overlays
(cdr overlays
))
2662 (overlay-put overlay
'face face
)
2663 (move-overlay overlay beg
(point)))
2664 ;; Make a new overlay and add it to
2665 ;; rmail-overlay-list.
2666 (setq overlay
(make-overlay beg
(point)))
2667 (overlay-put overlay
'face face
)
2668 (setq rmail-overlay-list
2669 (cons overlay rmail-overlay-list
))))))))))
2671 (defun rmail-auto-file ()
2672 "Automatically move a message into a sub-folder based on criteria.
2673 Called when a new message is displayed."
2674 (if (or (rmail-message-labels-p rmail-current-message
"filed")
2675 (not (string= (buffer-file-name)
2676 (expand-file-name rmail-file-name
))))
2677 ;; Do nothing if it's already been filed.
2679 ;; Find out some basics (common fields)
2680 (let ((from (mail-fetch-field "from"))
2681 (subj (mail-fetch-field "subject"))
2682 (to (concat (mail-fetch-field "to") "," (mail-fetch-field "cc")))
2683 (d rmail-automatic-folder-directives
)
2684 (directive-loop nil
)
2687 (setq folder
(car (car d
))
2688 directive-loop
(cdr (car d
)))
2689 (while (and (car directive-loop
)
2691 ((string= (car directive-loop
) "from") from
)
2692 ((string= (car directive-loop
) "to") to
)
2693 ((string= (car directive-loop
) "subject") subj
)
2694 (t (mail-fetch-field (car directive-loop
))))))
2695 (and f
(string-match (car (cdr directive-loop
)) f
))))
2696 (setq directive-loop
(cdr (cdr directive-loop
))))
2697 ;; If there are no directives left, then it was a complete match.
2698 (if (null directive-loop
)
2700 (rmail-delete-forward)
2701 (if (string= "/dev/null" folder
)
2702 (rmail-delete-message)
2703 (rmail-output-to-rmail-file folder
1 t
)
2705 (setq d
(cdr d
))))))
2707 (defun rmail-next-message (n)
2708 "Show following message whether deleted or not.
2709 With prefix arg N, moves forward N messages, or backward if N is negative."
2711 (set-buffer rmail-buffer
)
2712 (rmail-maybe-set-message-counters)
2713 (rmail-show-message (+ rmail-current-message n
)))
2715 (defun rmail-previous-message (n)
2716 "Show previous message whether deleted or not.
2717 With prefix arg N, moves backward N messages, or forward if N is negative."
2719 (rmail-next-message (- n
)))
2721 (defun rmail-next-undeleted-message (n)
2722 "Show following non-deleted message.
2723 With prefix arg N, moves forward N non-deleted messages,
2724 or backward if N is negative.
2726 Returns t if a new message is being shown, nil otherwise."
2728 (set-buffer rmail-buffer
)
2729 (rmail-maybe-set-message-counters)
2730 (let ((lastwin rmail-current-message
)
2731 (current rmail-current-message
))
2732 (while (and (> n
0) (< current rmail-total-messages
))
2733 (setq current
(1+ current
))
2734 (if (not (rmail-message-deleted-p current
))
2735 (setq lastwin current n
(1- n
))))
2736 (while (and (< n
0) (> current
1))
2737 (setq current
(1- current
))
2738 (if (not (rmail-message-deleted-p current
))
2739 (setq lastwin current n
(1+ n
))))
2740 (if (/= lastwin rmail-current-message
)
2741 (progn (rmail-show-message lastwin
)
2744 (message "No previous nondeleted message"))
2746 (message "No following nondeleted message"))
2749 (defun rmail-previous-undeleted-message (n)
2750 "Show previous non-deleted message.
2751 With prefix argument N, moves backward N non-deleted messages,
2752 or forward if N is negative."
2754 (rmail-next-undeleted-message (- n
)))
2756 (defun rmail-first-message ()
2757 "Show first message in file."
2759 (rmail-maybe-set-message-counters)
2760 (rmail-show-message 1))
2762 (defun rmail-last-message ()
2763 "Show last message in file."
2765 (rmail-maybe-set-message-counters)
2766 (rmail-show-message rmail-total-messages
))
2768 (defun rmail-what-message ()
2769 (let ((where (point))
2771 (high rmail-total-messages
)
2772 (mid (/ rmail-total-messages
2)))
2773 (while (> (- high low
) 1)
2774 (if (>= where
(rmail-msgbeg mid
))
2777 (setq mid
(+ low
(/ (- high low
) 2))))
2778 (if (>= where
(rmail-msgbeg high
)) high low
)))
2780 (defun rmail-message-recipients-p (msg recipients
&optional primary-only
)
2782 (goto-char (rmail-msgbeg msg
))
2783 (search-forward "\n*** EOOH ***\n")
2784 (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
2785 (or (string-match recipients
(or (mail-fetch-field "To") ""))
2786 (string-match recipients
(or (mail-fetch-field "From") ""))
2787 (if (not primary-only
)
2788 (string-match recipients
(or (mail-fetch-field "Cc") ""))))))
2790 (defun rmail-message-regexp-p (n regexp
)
2791 "Return t, if for message number N, regexp REGEXP matches in the header."
2792 (let ((beg (rmail-msgbeg n
))
2793 (end (rmail-msgend n
)))
2798 (if (prog1 (= (following-char) ?
0)
2800 ;; If there's a Summary-line in the (otherwise empty)
2801 ;; header, we didn't yet get past the EOOH line.
2802 (when (looking-at "^\\*\\*\\* EOOH \\*\\*\\*\n")
2805 (narrow-to-region (point) end
))
2810 (search-forward "\n*** EOOH ***\n" end t
)
2811 (setq end
(1+ (match-beginning 0)))))
2813 (if rmail-enable-mime
2814 (funcall rmail-search-mime-header-function n regexp end
)
2815 (re-search-forward regexp end t
)))))
2817 (defun rmail-search-message (msg regexp
)
2818 "Return non-nil, if for message number MSG, regexp REGEXP matches."
2819 (goto-char (rmail-msgbeg msg
))
2820 (if rmail-enable-mime
2821 (funcall rmail-search-mime-message-function msg regexp
)
2822 (re-search-forward regexp
(rmail-msgend msg
) t
)))
2824 (defvar rmail-search-last-regexp nil
)
2825 (defun rmail-search (regexp &optional n
)
2826 "Show message containing next match for REGEXP (but not the current msg).
2827 Prefix argument gives repeat count; negative argument means search
2828 backwards (through earlier messages).
2829 Interactively, empty argument means use same regexp used last time."
2831 (let* ((reversep (< (prefix-numeric-value current-prefix-arg
) 0))
2833 (concat (if reversep
"Reverse " "") "Rmail search (regexp): "))
2835 (if rmail-search-last-regexp
2836 (setq prompt
(concat prompt
2838 rmail-search-last-regexp
2840 (setq regexp
(read-string prompt
))
2841 (cond ((not (equal regexp
""))
2842 (setq rmail-search-last-regexp regexp
))
2843 ((not rmail-search-last-regexp
)
2844 (error "No previous Rmail search string")))
2845 (list rmail-search-last-regexp
2846 (prefix-numeric-value current-prefix-arg
))))
2848 (message "%sRmail search for %s..."
2849 (if (< n
0) "Reverse " "")
2851 (set-buffer rmail-buffer
)
2852 (rmail-maybe-set-message-counters)
2853 (let ((omin (point-min))
2858 (msg rmail-current-message
))
2863 ;; Check messages one by one, advancing message number up or down
2864 ;; but searching forward through each message.
2866 (while (and (null win
) (> msg
1))
2868 win
(rmail-search-message msg regexp
)))
2869 (while (and (null win
) (< msg rmail-total-messages
))
2871 win
(rmail-search-message msg regexp
))))
2872 (setq n
(+ n
(if reversep
1 -
1)))))
2875 (rmail-show-message msg
)
2876 ;; Search forward (if this is a normal search) or backward
2877 ;; (if this is a reverse search) through this message to
2878 ;; position point. This search may fail because REGEXP
2879 ;; was found in the hidden portion of this message. In
2880 ;; that case, move point to the beginning of visible
2884 (goto-char (point-max))
2885 (re-search-backward regexp nil
'move
))
2886 (goto-char (point-min))
2887 (re-search-forward regexp nil t
))
2888 (message "%sRmail search for %s...done"
2889 (if reversep
"Reverse " "")
2892 (narrow-to-region omin omax
)
2894 (message "Search failed: %s" regexp
)))))
2896 (defun rmail-search-backwards (regexp &optional n
)
2897 "Show message containing previous match for REGEXP.
2898 Prefix argument gives repeat count; negative argument means search
2899 forward (through later messages).
2900 Interactively, empty argument means use same regexp used last time."
2902 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg
) 0))
2904 (concat (if reversep
"Reverse " "") "Rmail search (regexp): "))
2906 (if rmail-search-last-regexp
2907 (setq prompt
(concat prompt
2909 rmail-search-last-regexp
2911 (setq regexp
(read-string prompt
))
2912 (cond ((not (equal regexp
""))
2913 (setq rmail-search-last-regexp regexp
))
2914 ((not rmail-search-last-regexp
)
2915 (error "No previous Rmail search string")))
2916 (list rmail-search-last-regexp
2917 (prefix-numeric-value current-prefix-arg
))))
2918 (rmail-search regexp
(- (or n
1))))
2920 ;; Show the first message which has the `unseen' attribute.
2921 (defun rmail-first-unseen-message ()
2922 (rmail-maybe-set-message-counters)
2927 (while (and (not found
) (<= current rmail-total-messages
))
2928 (if (rmail-message-labels-p current
", ?\\(unseen\\),")
2929 (setq found current
))
2930 (setq current
(1+ current
))))
2931 ;; Let the caller show the message.
2933 ;; (rmail-show-message found))
2936 (defun rmail-next-same-subject (n)
2937 "Go to the next mail message having the same subject header.
2938 With prefix argument N, do this N times.
2939 If N is negative, go backwards instead."
2941 (let ((subject (mail-fetch-field "Subject"))
2943 (i rmail-current-message
)
2944 (case-fold-search t
)
2945 search-regexp found
)
2946 (if (string-match "\\`[ \t]+" subject
)
2947 (setq subject
(substring subject
(match-end 0))))
2948 (if (string-match "Re:[ \t]*" subject
)
2949 (setq subject
(substring subject
(match-end 0))))
2950 (if (string-match "[ \t]+\\'" subject
)
2951 (setq subject
(substring subject
0 (match-beginning 0))))
2952 (setq search-regexp
(concat "^Subject: *\\(Re:[ \t]*\\)?"
2953 (regexp-quote subject
)
2958 (while (and (/= n
0)
2960 (< i rmail-total-messages
)
2963 (while (and (not done
)
2965 (< i rmail-total-messages
)
2967 (setq i
(if forward
(1+ i
) (1- i
)))
2968 (goto-char (rmail-msgbeg i
))
2969 (search-forward "\n*** EOOH ***\n")
2970 (let ((beg (point)) end
)
2971 (search-forward "\n\n")
2974 (setq done
(re-search-forward search-regexp end t
))))
2975 (if done
(setq found i
)))
2976 (setq n
(if forward
(1- n
) (1+ n
))))))
2978 (rmail-show-message found
)
2979 (error "No %s message with same subject"
2980 (if forward
"following" "previous")))))
2982 (defun rmail-previous-same-subject (n)
2983 "Go to the previous mail message having the same subject header.
2984 With prefix argument N, do this N times.
2985 If N is negative, go forwards instead."
2987 (rmail-next-same-subject (- n
)))
2989 ;;;; *** Rmail Message Deletion Commands ***
2991 (defun rmail-message-deleted-p (n)
2992 (= (aref rmail-deleted-vector n
) ?D
))
2994 (defun rmail-set-message-deleted-p (n state
)
2995 (aset rmail-deleted-vector n
(if state ?D ?\
)))
2997 (defun rmail-delete-message ()
2998 "Delete this message and stay on it."
3000 (rmail-set-attribute "deleted" t
)
3001 (run-hooks 'rmail-delete-message-hook
))
3003 (defun rmail-undelete-previous-message ()
3004 "Back up to deleted message, select it, and undelete it."
3006 (set-buffer rmail-buffer
)
3007 (let ((msg rmail-current-message
))
3008 (while (and (> msg
0)
3009 (not (rmail-message-deleted-p msg
)))
3010 (setq msg
(1- msg
)))
3012 (error "No previous deleted message")
3013 (if (/= msg rmail-current-message
)
3014 (rmail-show-message msg
))
3015 (rmail-set-attribute "deleted" nil
)
3016 (if (rmail-summary-exists)
3018 (set-buffer rmail-summary-buffer
)
3019 (rmail-summary-mark-undeleted msg
)))
3020 (rmail-maybe-display-summary))))
3022 (defun rmail-delete-forward (&optional backward
)
3023 "Delete this message and move to next nondeleted one.
3024 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
3025 With prefix argument, delete and move backward.
3027 Returns t if a new message is displayed after the delete, or nil otherwise."
3029 (rmail-set-attribute "deleted" t
)
3030 (run-hooks 'rmail-delete-message-hook
)
3031 (let ((del-msg rmail-current-message
))
3032 (if (rmail-summary-exists)
3033 (rmail-select-summary
3034 (rmail-summary-mark-deleted del-msg
)))
3035 (prog1 (rmail-next-undeleted-message (if backward -
1 1))
3036 (rmail-maybe-display-summary))))
3038 (defun rmail-delete-backward ()
3039 "Delete this message and move to previous nondeleted one.
3040 Deleted messages stay in the file until the \\[rmail-expunge] command is given."
3042 (rmail-delete-forward t
))
3044 ;; Compute the message number a given message would have after expunging.
3045 ;; The present number of the message is OLDNUM.
3046 ;; DELETEDVEC should be rmail-deleted-vector.
3047 ;; The value is nil for a message that would be deleted.
3048 (defun rmail-msg-number-after-expunge (deletedvec oldnum
)
3049 (if (or (null oldnum
) (= (aref deletedvec oldnum
) ?D
))
3054 (if (/= (aref deletedvec i
) ?D
)
3055 (setq newnum
(1+ newnum
)))
3059 (defun rmail-expunge-confirmed ()
3060 "Return t if deleted message should be expunged. If necessary, ask the user.
3061 See also user-option `rmail-confirm-expunge'."
3062 (set-buffer rmail-buffer
)
3063 (or (not (stringp rmail-deleted-vector
))
3064 (not (string-match "D" rmail-deleted-vector
))
3065 (null rmail-confirm-expunge
)
3066 (funcall rmail-confirm-expunge
3067 "Erase deleted messages from Rmail file? ")))
3069 (defun rmail-only-expunge (&optional dont-show
)
3070 "Actually erase all deleted messages in the file."
3072 (set-buffer rmail-buffer
)
3073 (message "Expunging deleted messages...")
3074 ;; Discard all undo records for this buffer.
3075 (or (eq buffer-undo-list t
)
3076 (setq buffer-undo-list nil
))
3077 (rmail-maybe-set-message-counters)
3078 (let* ((omax (- (buffer-size) (point-max)))
3079 (omin (- (buffer-size) (point-min)))
3080 (opoint (if (and (> rmail-current-message
0)
3081 (rmail-message-deleted-p rmail-current-message
))
3083 (if rmail-enable-mime
3084 (with-current-buffer rmail-view-buffer
3085 (- (point)(point-min)))
3086 (- (point) (point-min)))))
3087 (messages-head (cons (aref rmail-message-vector
0) nil
))
3088 (messages-tail messages-head
)
3089 ;; Don't make any undo records for the expunging.
3090 (buffer-undo-list t
)
3095 (goto-char (point-min))
3098 (total rmail-total-messages
)
3099 (new-message-number rmail-current-message
)
3101 (new-msgref (list (list 0)))
3102 (rmailbuf (current-buffer))
3103 (buffer-read-only nil
)
3104 (messages rmail-message-vector
)
3105 (deleted rmail-deleted-vector
)
3106 (summary rmail-summary-vector
))
3107 (setq rmail-total-messages nil
3108 rmail-current-message nil
3109 rmail-message-vector nil
3110 rmail-deleted-vector nil
3111 rmail-summary-vector nil
)
3113 (while (<= number total
)
3114 (if (= (aref deleted number
) ?D
)
3117 (marker-position (aref messages number
))
3118 (marker-position (aref messages
(1+ number
))))
3119 (move-marker (aref messages number
) nil
)
3120 (if (> new-message-number counter
)
3121 (setq new-message-number
(1- new-message-number
))))
3122 (setq counter
(1+ counter
))
3124 (setcdr messages-tail
3125 (cons (aref messages number
) nil
)))
3127 (cons (if (= counter number
) (aref summary
(1- number
)))
3130 (cons (aref rmail-msgref-vector number
)
3132 (setcar (car new-msgref
) counter
))
3133 (if (zerop (%
(setq number
(1+ number
)) 20))
3134 (message "Expunging deleted messages...%d" number
)))
3136 (setcdr messages-tail
3137 (cons (aref messages number
) nil
)))
3138 (setq rmail-current-message new-message-number
3139 rmail-total-messages counter
3140 rmail-message-vector
(apply 'vector messages-head
)
3141 rmail-deleted-vector
(make-string (1+ counter
) ?\
)
3142 rmail-summary-vector
(vconcat (nreverse new-summary
))
3143 rmail-msgref-vector
(apply 'vector
(nreverse new-msgref
))
3145 (message "Expunging deleted messages...done")
3147 (narrow-to-region (- (buffer-size) omin
) (- (buffer-size) omax
)))
3150 (if (zerop rmail-current-message
) 1 nil
)
3151 (if rmail-enable-mime
3152 (goto-char (+ (point-min) opoint
))
3153 (goto-char (+ (point) opoint
))))))))
3155 (defun rmail-expunge ()
3156 "Erase deleted messages from Rmail file and summary buffer."
3158 (when (rmail-expunge-confirmed)
3159 (rmail-only-expunge)
3160 (if (rmail-summary-exists)
3161 (rmail-select-summary (rmail-update-summary)))))
3163 ;;;; *** Rmail Mailing Commands ***
3165 (defun rmail-start-mail (&optional noerase to subject in-reply-to cc
3166 replybuffer sendactions same-window others
)
3169 (setq yank-action
(list 'insert-buffer replybuffer
)))
3170 (setq others
(cons (cons "cc" cc
) others
))
3171 (setq others
(cons (cons "in-reply-to" in-reply-to
) others
))
3173 (compose-mail to subject others
3175 yank-action sendactions
)
3176 (if rmail-mail-new-frame
3178 (compose-mail to subject others
3179 noerase
'switch-to-buffer-other-frame
3180 yank-action sendactions
)
3181 ;; This is not a standard frame parameter;
3182 ;; nothing except sendmail.el looks at it.
3183 (modify-frame-parameters (selected-frame)
3184 '((mail-dedicated-frame . t
))))
3185 (compose-mail to subject others
3186 noerase
'switch-to-buffer-other-window
3187 yank-action sendactions
)))))
3189 (defun rmail-mail ()
3190 "Send mail in another window.
3191 While composing the message, use \\[mail-yank-original] to yank the
3192 original message into it."
3194 (rmail-start-mail nil nil nil nil nil rmail-view-buffer
))
3196 (defun rmail-continue ()
3197 "Continue composing outgoing message previously being composed."
3199 (rmail-start-mail t
))
3201 (defun rmail-reply (just-sender)
3202 "Reply to the current message.
3203 Normally include CC: to all other recipients of original message;
3204 prefix argument means ignore them. While composing the reply,
3205 use \\[mail-yank-original] to yank the original message into it."
3207 (let (from reply-to cc subject date to message-id references
3208 resent-to resent-cc resent-reply-to
3209 (msgnum rmail-current-message
))
3212 (if rmail-enable-mime
3214 (goto-char (point-min))
3215 (if (search-forward "\n\n" nil
'move
)
3216 (1+ (match-beginning 0))
3219 (goto-char (rmail-msgbeg rmail-current-message
))
3221 (if (= (following-char) ?
0)
3223 (progn (forward-line 2)
3225 (progn (search-forward "\n\n" (rmail-msgend rmail-current-message
)
3228 (narrow-to-region (point)
3229 (progn (search-forward "\n*** EOOH ***\n")
3230 (beginning-of-line) (point)))))
3231 (setq from
(mail-fetch-field "from")
3232 reply-to
(or (mail-fetch-field "reply-to" nil t
)
3234 cc
(and (not just-sender
)
3235 (mail-fetch-field "cc" nil t
))
3236 subject
(mail-fetch-field "subject")
3237 date
(mail-fetch-field "date")
3238 to
(or (mail-fetch-field "to" nil t
) "")
3239 message-id
(mail-fetch-field "message-id")
3240 references
(mail-fetch-field "references" nil nil t
)
3241 resent-reply-to
(mail-fetch-field "resent-reply-to" nil t
)
3242 resent-cc
(and (not just-sender
)
3243 (mail-fetch-field "resent-cc" nil t
))
3244 resent-to
(or (mail-fetch-field "resent-to" nil t
) "")
3245 ;;; resent-subject (mail-fetch-field "resent-subject")
3246 ;;; resent-date (mail-fetch-field "resent-date")
3247 ;;; resent-message-id (mail-fetch-field "resent-message-id")
3249 ;; Merge the resent-to and resent-cc into the to and cc.
3250 (if (and resent-to
(not (equal resent-to
"")))
3251 (if (not (equal to
""))
3252 (setq to
(concat to
", " resent-to
))
3253 (setq to resent-to
)))
3254 (if (and resent-cc
(not (equal resent-cc
"")))
3255 (if (not (equal cc
""))
3256 (setq cc
(concat cc
", " resent-cc
))
3257 (setq cc resent-cc
)))
3258 ;; Add `Re: ' to subject if not there already.
3259 (and (stringp subject
)
3261 (concat rmail-reply-prefix
3262 (if (let ((case-fold-search t
))
3263 (string-match rmail-reply-regexp subject
))
3264 (substring subject
(match-end 0))
3268 ;; Using mail-strip-quoted-names is undesirable with newer mailers
3269 ;; since they can handle the names unstripped.
3270 ;; I don't know whether there are other mailers that still
3271 ;; need the names to be stripped.
3272 ;;; (mail-strip-quoted-names reply-to)
3275 (rmail-make-in-reply-to-field from date message-id
)
3278 ;; mail-strip-quoted-names is NOT necessary for rmail-dont-reply-to
3280 (let* ((cc-list (rmail-dont-reply-to
3281 (mail-strip-quoted-names
3282 (if (null cc
) to
(concat to
", " cc
))))))
3283 (if (string= cc-list
"") nil cc-list
)))
3285 (list (list 'rmail-mark-message
3287 (with-current-buffer rmail-buffer
3288 (aref rmail-msgref-vector msgnum
))
3291 (list (cons "References" (concat (mapconcat 'identity references
" ")
3292 " " message-id
))))))
3294 (defun rmail-mark-message (buffer msgnum-list attribute
)
3295 "Give BUFFER's message number in MSGNUM-LIST the attribute ATTRIBUTE.
3296 This is use in the send-actions for message buffers.
3297 MSGNUM-LIST is a list of the form (MSGNUM)
3298 which is an element of rmail-msgref-vector."
3301 (if (car msgnum-list
)
3302 (rmail-set-attribute attribute t
(car msgnum-list
)))))
3304 (defun rmail-make-in-reply-to-field (from date message-id
)
3311 (let ((tem (car (rfc822-addresses from
))))
3315 (regexp-quote (if (string-match "@[^@]*\\'" tem
)
3317 (match-beginning 0))
3320 ;; missing From, or Message-ID is sufficiently informative
3322 (concat message-id
" (" tem
")"))
3323 ;; Copy TEM, discarding text properties.
3324 (setq tem
(copy-sequence tem
))
3325 (set-text-properties 0 (length tem
) nil tem
)
3326 (setq tem
(copy-sequence tem
))
3327 ;; Use prin1 to fake RFC822 quoting
3328 (let ((field (prin1-to-string tem
)))
3330 (concat field
"'s message of " date
)
3332 ((let* ((foo "[^][\000-\037()<>@,;:\\\" ]+")
3333 (bar "[^][\000-\037()<>@,;:\\\"]+"))
3334 ;; These strings both match all non-ASCII characters.
3335 (or (string-match (concat "\\`[ \t]*\\(" bar
3336 "\\)\\(<" foo
"@" foo
">\\)?[ \t]*\\'")
3337 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
3339 (string-match (concat "\\`[ \t]*<" foo
"@" foo
">[ \t]*(\\("
3340 bar
"\\))[ \t]*\\'")
3341 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
3343 (let ((start (match-beginning 1))
3344 (end (match-end 1)))
3345 ;; Trim whitespace which above regexp match allows
3346 (while (and (< start end
)
3347 (memq (aref from start
) '(?
\t ?\
)))
3348 (setq start
(1+ start
)))
3349 (while (and (< start end
)
3350 (memq (aref from
(1- end
)) '(?
\t ?\
)))
3351 (setq end
(1- end
)))
3352 (let ((field (substring from start end
)))
3353 (if date
(setq field
(concat "message from " field
" on " date
)))
3355 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
3356 (concat message-id
" (" field
")")
3359 ;; If we can't kludge it simply, do it correctly
3360 (let ((mail-use-rfc822 t
))
3361 (rmail-make-in-reply-to-field from date message-id
)))))
3363 (defun rmail-forward (resend)
3364 "Forward the current message to another user.
3365 With prefix argument, \"resend\" the message instead of forwarding it;
3366 see the documentation of `rmail-resend'."
3369 (call-interactively 'rmail-resend
)
3370 (let ((forward-buffer rmail-buffer
)
3371 (msgnum rmail-current-message
)
3372 (subject (concat "["
3373 (let ((from (or (mail-fetch-field "From")
3374 (mail-fetch-field ">From"))))
3376 (concat (mail-strip-quoted-names from
) ": ")
3378 (or (mail-fetch-field "Subject") "")
3380 (if (rmail-start-mail
3381 nil nil subject nil nil nil
3382 (list (list 'rmail-mark-message
3384 (with-current-buffer rmail-buffer
3385 (aref rmail-msgref-vector msgnum
))
3387 ;; If only one window, use it for the mail buffer.
3388 ;; Otherwise, use another window for the mail buffer
3389 ;; so that the Rmail buffer remains visible
3390 ;; and sending the mail will get back to it.
3391 (and (not rmail-mail-new-frame
) (one-window-p t
)))
3392 ;; The mail buffer is now current.
3394 ;; Insert after header separator--before signature if any.
3395 (goto-char (mail-text-start))
3396 (if (or rmail-enable-mime rmail-enable-mime-composing
)
3397 (funcall rmail-insert-mime-forwarded-message-function
3399 (insert "------- Start of forwarded message -------\n")
3400 ;; Quote lines with `- ' if they start with `-'.
3401 (let ((beg (point)) end
)
3402 (setq end
(point-marker))
3403 (set-marker-insertion-type end t
)
3404 (insert-buffer-substring forward-buffer
)
3406 (while (re-search-forward "^-" end t
)
3411 (skip-chars-backward "\n")
3414 (delete-region (point) end
)
3415 (set-marker end nil
))
3416 (insert "------- End of forwarded message -------\n"))
3419 (defun rmail-resend (address &optional from comment mail-alias-file
)
3420 "Resend current message to ADDRESSES.
3421 ADDRESSES should be a single address, a string consisting of several
3422 addresses separated by commas, or a list of addresses.
3424 Optional FROM is the address to resend the message from, and
3425 defaults from the value of `user-mail-address'.
3426 Optional COMMENT is a string to insert as a comment in the resent message.
3427 Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
3428 typically for purposes of moderating a list."
3429 (interactive "sResend to: ")
3431 (require 'mailalias
)
3432 (unless (or (eq rmail-view-buffer
(current-buffer))
3433 (eq rmail-buffer
(current-buffer)))
3434 (error "Not an Rmail buffer"))
3435 (if (not from
) (setq from user-mail-address
))
3436 (let ((tembuf (generate-new-buffer " sendmail temp"))
3437 (case-fold-search nil
)
3438 (mail-personal-alias-file
3439 (or mail-alias-file mail-personal-alias-file
))
3440 (mailbuf rmail-buffer
))
3442 (with-current-buffer tembuf
3443 ;;>> Copy message into temp buffer
3444 (if rmail-enable-mime
3445 (funcall rmail-insert-mime-resent-message-function mailbuf
)
3446 (insert-buffer-substring mailbuf
))
3447 (goto-char (point-min))
3448 ;; Delete any Sender field, since that's not specifiable.
3449 ; Only delete Sender fields in the actual header.
3450 (re-search-forward "^$" nil
'move
)
3451 ; Using "while" here rather than "if" because some buggy mail
3452 ; software may have inserted multiple Sender fields.
3453 (while (re-search-backward "^Sender:" nil t
)
3457 (while (looking-at "[ \t]")
3459 (delete-region beg
(point))))
3460 ; Go back to the beginning of the buffer so the Resent- fields
3461 ; are inserted there.
3462 (goto-char (point-min))
3463 ;;>> Insert resent-from:
3464 (insert "Resent-From: " from
"\n")
3465 (insert "Resent-Date: " (mail-rfc822-date) "\n")
3466 ;;>> Insert resent-to: and bcc if need be.
3467 (let ((before (point)))
3469 (insert "Resent-Bcc: " (user-login-name) "\n"))
3470 (insert "Resent-To: " (if (stringp address
)
3472 (mapconcat 'identity address
",\n\t"))
3474 ;; Expand abbrevs in the recipients.
3476 (if (featurep 'mailabbrev
)
3477 (let ((end (point-marker))
3478 (local-abbrev-table mail-abbrevs
)
3479 (old-syntax-table (syntax-table)))
3480 (if (and (not (vectorp mail-abbrevs
))
3481 (file-exists-p mail-personal-alias-file
))
3482 (build-mail-abbrevs))
3483 (unless mail-abbrev-syntax-table
3484 (mail-abbrev-make-syntax-table))
3485 (set-syntax-table mail-abbrev-syntax-table
)
3487 (while (and (< (point) end
)
3488 (progn (forward-word 1)
3491 (set-syntax-table old-syntax-table
))
3492 (expand-mail-aliases before
(point)))))
3493 ;;>> Set up comment, if any.
3494 (if (and (sequencep comment
) (not (zerop (length comment
))))
3495 (let ((before (point))
3498 (or (eolp) (insert "\n"))
3499 (setq after
(point))
3501 (while (< (point) after
)
3502 (insert "Resent-Comment: ")
3504 ;; Don't expand aliases in the destination fields
3505 ;; of the original message.
3507 (funcall send-mail-function
)))
3508 (kill-buffer tembuf
))
3509 (with-current-buffer rmail-buffer
3510 (rmail-set-attribute "resent" t rmail-current-message
))))
3512 (defvar mail-unsent-separator
3513 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
3514 "^ *---+ +Returned message +---+ *$\\|"
3515 "^ *---+ *Returned mail follows *---+ *$\\|"
3516 "^Start of returned message$\\|"
3517 "^---+ Below this line is a copy of the message.$\\|"
3518 "^ *---+ +Original message +---+ *$\\|"
3519 "^ *--+ +begin message +--+ *$\\|"
3520 "^ *---+ +Original message follows +---+ *$\\|"
3521 "^ *---+ +Your message follows +---+ *$\\|"
3522 "^|? *---+ +Message text follows: +---+ *|?$\\|"
3523 "^ *---+ +This is a copy of \\w+ message, including all the headers.*---+ *$")
3524 "A regexp that matches the separator before the text of a failed message.")
3526 (defvar mail-mime-unsent-header
"^Content-Type: message/rfc822 *$"
3527 "A regexp that matches the header of a MIME body part with a failed message.")
3529 (defun rmail-retry-failure ()
3530 "Edit a mail message which is based on the contents of the current message.
3531 For a message rejected by the mail system, extract the interesting headers and
3532 the body of the original message.
3533 If the failed message is a MIME multipart message, it is searched for a
3534 body part with a header which matches the variable `mail-mime-unsent-header'.
3535 Otherwise, the variable `mail-unsent-separator' should match the string that
3536 delimits the returned original message.
3537 The variable `rmail-retry-ignored-headers' is a regular expression
3538 specifying headers which should not be copied into the new message."
3540 (require 'mail-utils
)
3541 (let ((rmail-this-buffer (current-buffer))
3542 (msgnum rmail-current-message
)
3543 bounce-start bounce-end bounce-indent resending
3544 ;; Fetch any content-type header in current message
3545 ;; Must search thru the whole unpruned header.
3549 (rmail-narrow-to-non-pruned-header)
3550 (mail-fetch-field "Content-Type") ))))
3552 (goto-char (point-min))
3553 (let ((case-fold-search t
))
3554 (if (and content-type
3556 ";[\n\t ]*boundary=\"?\\([-0-9a-z'()+_,./:=? ]+\\)\"?"
3558 ;; Handle a MIME multipart bounce message.
3561 (substring content-type
(match-beginning 1)
3563 (unless (re-search-forward mail-mime-unsent-header nil t
)
3564 (error "Cannot find beginning of header in failed message"))
3565 (unless (search-forward "\n\n" nil t
)
3566 (error "Cannot find start of Mime data in failed message"))
3567 (setq bounce-start
(point))
3568 (if (search-forward codestring nil t
)
3569 (setq bounce-end
(match-beginning 0))
3570 (setq bounce-end
(point-max))))
3572 (or (re-search-forward mail-unsent-separator nil t
)
3573 (error "Cannot parse this as a failure message"))
3574 (skip-chars-forward "\n")
3575 ;; Support a style of failure message in which the original
3576 ;; message is indented, and included within lines saying
3577 ;; `Start of returned message' and `End of returned message'.
3578 (if (looking-at " +Received:")
3580 (setq bounce-start
(point))
3581 (skip-chars-forward " ")
3582 (setq bounce-indent
(- (current-column)))
3583 (goto-char (point-max))
3584 (re-search-backward "^End of returned message$" nil t
)
3585 (setq bounce-end
(point)))
3586 ;; One message contained a few random lines before
3587 ;; the old message header. The first line of the
3588 ;; message started with two hyphens. A blank line
3589 ;; followed these random lines. The same line
3590 ;; beginning with two hyphens was possibly marking
3591 ;; the end of the message.
3592 (if (looking-at "^--")
3593 (let ((boundary (buffer-substring-no-properties
3595 (progn (end-of-line) (point)))))
3596 (search-forward "\n\n")
3597 (skip-chars-forward "\n")
3598 (setq bounce-start
(point))
3599 (goto-char (point-max))
3600 (search-backward (concat "\n\n" boundary
) bounce-start t
)
3601 (setq bounce-end
(point)))
3602 (setq bounce-start
(point)
3603 bounce-end
(point-max)))
3604 (unless (search-forward "\n\n" nil t
)
3605 (error "Cannot find end of header in failed message"))))))
3606 ;; We have found the message that bounced, within the current message.
3607 ;; Now start sending new message; default header fields from original.
3608 ;; Turn off the usual actions for initializing the message body
3609 ;; because we want to get only the text from the failure message.
3610 (let (mail-signature mail-setup-hook
)
3611 (if (rmail-start-mail nil nil nil nil nil rmail-this-buffer
3612 (list (list 'rmail-mark-message
3614 (aref rmail-msgref-vector msgnum
)
3616 ;; Insert original text as initial text of new draft message.
3617 ;; Bind inhibit-read-only since the header delimiter
3618 ;; of the previous message was probably read-only.
3619 (let ((inhibit-read-only t
)
3620 rmail-displayed-headers
3621 rmail-ignored-headers
)
3623 (insert-buffer-substring rmail-this-buffer
3624 bounce-start bounce-end
)
3625 (goto-char (point-min))
3627 (indent-rigidly (point-min) (point-max) bounce-indent
))
3628 (rmail-clear-headers rmail-retry-ignored-headers
)
3629 (rmail-clear-headers "^sender:\\|^return-path:\\|^received:")
3630 (mail-sendmail-delimit-header)
3632 (narrow-to-region (point-min) (mail-header-end))
3633 (setq resending
(mail-fetch-field "resent-to"))
3636 (insert "Resent-Bcc: " (user-login-name) "\n")
3637 (insert "BCC: " (user-login-name) "\n"))))
3638 (goto-char (point-min))
3639 (mail-position-on-field (if resending
"Resent-To" "To") t
))))))
3641 (defun rmail-summary-exists ()
3642 "Non-nil iff in an RMAIL buffer and an associated summary buffer exists.
3643 In fact, the non-nil value returned is the summary buffer itself."
3644 (and rmail-summary-buffer
(buffer-name rmail-summary-buffer
)
3645 rmail-summary-buffer
))
3647 (defun rmail-summary-displayed ()
3648 "t iff in RMAIL buffer and an associated summary buffer is displayed."
3649 (and rmail-summary-buffer
(get-buffer-window rmail-summary-buffer
)))
3651 (defcustom rmail-redisplay-summary nil
3652 "*Non-nil means Rmail should show the summary when it changes.
3653 This has an effect only if a summary buffer exists."
3655 :group
'rmail-summary
)
3657 (defcustom rmail-summary-window-size nil
3658 "*Non-nil means specify the height for an Rmail summary window."
3659 :type
'(choice (const :tag
"Disabled" nil
) integer
)
3660 :group
'rmail-summary
)
3662 ;; Put the summary buffer back on the screen, if user wants that.
3663 (defun rmail-maybe-display-summary ()
3664 (let ((selected (selected-window))
3666 ;; If requested, make sure the summary is displayed.
3667 (and rmail-summary-buffer
(buffer-name rmail-summary-buffer
)
3668 rmail-redisplay-summary
3669 (if (get-buffer-window rmail-summary-buffer
0)
3670 ;; It's already in some frame; show that one.
3671 (let ((frame (window-frame
3672 (get-buffer-window rmail-summary-buffer
0))))
3673 (make-frame-visible frame
)
3674 (raise-frame frame
))
3675 (display-buffer rmail-summary-buffer
)))
3676 ;; If requested, set the height of the summary window.
3677 (and rmail-summary-buffer
(buffer-name rmail-summary-buffer
)
3678 rmail-summary-window-size
3679 (setq window
(get-buffer-window rmail-summary-buffer
))
3680 ;; Don't try to change the size if just one window in frame.
3681 (not (eq window
(frame-root-window (window-frame window
))))
3684 (select-window window
)
3685 (enlarge-window (- rmail-summary-window-size
(window-height))))
3686 (select-window selected
)))))
3688 ;;;; *** Rmail Local Fontification ***
3690 (defun rmail-fontify-buffer-function ()
3691 ;; This function's symbol is bound to font-lock-fontify-buffer-function.
3692 (add-hook 'rmail-show-message-hook
'rmail-fontify-message nil t
)
3693 ;; If we're already showing a message, fontify it now.
3694 (if rmail-current-message
(rmail-fontify-message))
3695 ;; Prevent Font Lock mode from kicking in.
3696 (setq font-lock-fontified t
))
3698 (defun rmail-unfontify-buffer-function ()
3699 ;; This function's symbol is bound to font-lock-fontify-unbuffer-function.
3700 (let ((modified (buffer-modified-p))
3701 (buffer-undo-list t
) (inhibit-read-only t
)
3702 before-change-functions after-change-functions
3703 buffer-file-name buffer-file-truename
)
3706 (remove-hook 'rmail-show-message-hook
'rmail-fontify-message t
)
3707 (remove-text-properties (point-min) (point-max) '(rmail-fontified nil
))
3708 (font-lock-default-unfontify-buffer)
3709 (and (not modified
) (buffer-modified-p) (set-buffer-modified-p nil
)))))
3711 (defun rmail-fontify-message ()
3712 ;; Fontify the current message if it is not already fontified.
3713 (if (text-property-any (point-min) (point-max) 'rmail-fontified nil
)
3714 (let ((modified (buffer-modified-p))
3715 (buffer-undo-list t
) (inhibit-read-only t
)
3716 before-change-functions after-change-functions
3717 buffer-file-name buffer-file-truename
)
3720 (add-text-properties (point-min) (point-max) '(rmail-fontified t
))
3721 (font-lock-fontify-region (point-min) (point-max))
3722 (and (not modified
) (buffer-modified-p) (set-buffer-modified-p nil
)))))))
3724 ;;; Speedbar support for RMAIL files.
3725 (eval-when-compile (require 'speedbar
))
3727 (defvar rmail-speedbar-match-folder-regexp
"^[A-Z0-9]+\\(\\.[A-Z0-9]+\\)?$"
3728 "*This regex is used to match folder names to be displayed in speedbar.
3729 Enabling this will permit speedbar to display your folders for easy
3730 browsing, and moving of messages.")
3732 (defvar rmail-speedbar-last-user nil
3733 "The last user to be displayed in the speedbar.")
3735 (defvar rmail-speedbar-key-map nil
3736 "Keymap used when in rmail display mode.")
3738 (defun rmail-install-speedbar-variables ()
3739 "Install those variables used by speedbar to enhance rmail."
3740 (if rmail-speedbar-key-map
3742 (setq rmail-speedbar-key-map
(speedbar-make-specialized-keymap))
3744 (define-key rmail-speedbar-key-map
"e" 'speedbar-edit-line
)
3745 (define-key rmail-speedbar-key-map
"r" 'speedbar-edit-line
)
3746 (define-key rmail-speedbar-key-map
"\C-m" 'speedbar-edit-line
)
3747 (define-key rmail-speedbar-key-map
"M"
3748 'rmail-speedbar-move-message-to-folder-on-line
)))
3750 (defvar rmail-speedbar-menu-items
3751 '(["Read Folder" speedbar-edit-line t
]
3752 ["Move message to folder" rmail-speedbar-move-message-to-folder-on-line
3753 (save-excursion (beginning-of-line)
3754 (looking-at "<M> "))])
3755 "Additional menu-items to add to speedbar frame.")
3757 ;; Make sure our special speedbar major mode is loaded
3758 (if (featurep 'speedbar
)
3759 (rmail-install-speedbar-variables)
3760 (add-hook 'speedbar-load-hook
'rmail-install-speedbar-variables
))
3762 (defun rmail-speedbar-buttons (buffer)
3763 "Create buttons for BUFFER containing rmail messages.
3764 Click on the address under Reply to: to reply to this person.
3765 Under Folders: Click a name to read it, or on the <M> to move the
3766 current message into that RMAIL folder."
3770 (goto-char (point-min))
3771 (if (not (re-search-forward "^Reply-To: " nil t
))
3772 (if (not (re-search-forward "^From:? " nil t
))
3776 (setq from
(buffer-substring (point) (save-excursion
3779 (goto-char (point-min))
3780 (if (and (looking-at "Reply to:")
3781 (equal from rmail-speedbar-last-user
))
3783 (setq rmail-speedbar-last-user from
)
3785 (insert "Reply To:\n")
3787 (speedbar-insert-button from
'speedbar-directory-face
'highlight
3788 'rmail-speedbar-button
'rmail-reply
))
3789 (insert "Folders:\n")
3790 (let* ((case-fold-search nil
)
3791 (df (directory-files (save-excursion (set-buffer buffer
)
3793 nil rmail-speedbar-match-folder-regexp
)))
3795 (speedbar-insert-button "<M>" 'speedbar-button-face
'highlight
3796 'rmail-speedbar-move-message
(car df
))
3797 (speedbar-insert-button (car df
) 'speedbar-file-face
'highlight
3798 'rmail-speedbar-find-file nil t
)
3799 (setq df
(cdr df
)))))))
3801 (defun rmail-speedbar-button (text token indent
)
3802 "Execute an rmail command specified by TEXT.
3803 The command used is TOKEN. INDENT is not used."
3804 (speedbar-with-attached-buffer
3807 (defun rmail-speedbar-find-file (text token indent
)
3808 "Load in the rmail file TEXT.
3809 TOKEN and INDENT are not used."
3810 (speedbar-with-attached-buffer
3811 (message "Loading in RMAIL file %s..." text
)
3814 (defun rmail-speedbar-move-message-to-folder-on-line ()
3815 "If the current line is a folder, move current message to it."
3819 (if (re-search-forward "<M> " (save-excursion (end-of-line) (point)) t
)
3822 (speedbar-do-function-pointer)))))
3824 (defun rmail-speedbar-move-message (text token indent
)
3825 "From button TEXT, copy current message to the rmail file specified by TOKEN.
3826 TEXT and INDENT are not used."
3827 (speedbar-with-attached-buffer
3828 (message "Moving message to %s" token
)
3829 (rmail-output-to-rmail-file token
)))
3831 ; Functions for setting, getting and encoding the POP password.
3832 ; The password is encoded to prevent it from being easily accessible
3833 ; to "prying eyes." Obviously, this encoding isn't "real security,"
3834 ; nor is it meant to be.
3837 (defun rmail-set-pop-password (password)
3838 "Set PASSWORD to be used for retrieving mail from a POP server."
3839 (interactive "sPassword: ")
3841 (setq rmail-encoded-pop-password
3842 (rmail-encode-string password
(emacs-pid)))
3843 (setq rmail-pop-password nil
)
3844 (setq rmail-encoded-pop-password nil
)))
3846 (defun rmail-get-pop-password ()
3847 "Get the password for retrieving mail from a POP server. If none
3848 has been set, then prompt the user for one."
3849 (if (not rmail-encoded-pop-password
)
3850 (progn (if (not rmail-pop-password
)
3851 (setq rmail-pop-password
(read-passwd "POP password: ")))
3852 (rmail-set-pop-password rmail-pop-password
)
3853 (setq rmail-pop-password nil
)))
3854 (rmail-encode-string rmail-encoded-pop-password
(emacs-pid)))
3856 (defun rmail-have-password ()
3857 (or rmail-pop-password rmail-encoded-pop-password
))
3859 (defun rmail-encode-string (string mask
)
3860 "Encode STRING with integer MASK, by taking the exclusive OR of the
3861 lowest byte in the mask with the first character of string, the
3862 second-lowest-byte with the second character of the string, etc.,
3863 restarting at the lowest byte of the mask whenever it runs out.
3864 Returns the encoded string. Calling the function again with an
3865 encoded string (and the same mask) will decode the string."
3866 (setq mask
(abs mask
)) ; doesn't work if negative
3867 (let* ((string-vector (string-to-vector string
)) (i 0)
3868 (len (length string-vector
)) (curmask mask
) charmask
)
3871 (setq curmask mask
))
3872 (setq charmask
(% curmask
256))
3873 (setq curmask
(lsh curmask -
8))
3874 (aset string-vector i
(logxor charmask
(aref string-vector i
)))
3876 (concat string-vector
)))
3878 ;;;; Desktop support
3881 (defun rmail-restore-desktop-buffer (desktop-buffer-file-name
3883 desktop-buffer-misc
)
3884 "Restore an rmail buffer specified in a desktop file."
3885 (condition-case error
3887 (rmail-input desktop-buffer-file-name
)
3888 (if (eq major-mode
'rmail-mode
)
3892 (kill-buffer (current-buffer))
3897 ;;; arch-tag: cff0a950-57fe-4f73-a86e-91ff75afd06c
3898 ;;; rmail.el ends here