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, 2001
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 value of the default value of
144 the 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."
235 (defcustom rmail-secondary-file-directory
"~/"
236 "*Directory for additional secondary Rmail files."
240 (defcustom rmail-secondary-file-regexp
"\\.xmail$"
241 "*Regexp for which files are secondary Rmail files."
246 (defcustom rmail-confirm-expunge
'y-or-n-p
247 "*Whether and how to ask for confirmation before expunging deleted messages."
248 :type
'(choice (const :tag
"No confirmation" nil
)
249 (const :tag
"Confirm with y-or-n-p" y-or-n-p
)
250 (const :tag
"Confirm with yes-or-no-p" yes-or-no-p
))
255 (defvar rmail-mode-hook nil
256 "List of functions to call when Rmail is invoked.")
259 (defvar rmail-get-new-mail-hook nil
260 "List of functions to call when Rmail has retrieved new mail.")
263 (defcustom rmail-show-message-hook nil
264 "List of functions to call when Rmail displays a message."
266 :options
'(goto-address)
270 (defvar rmail-quit-hook nil
271 "List of functions to call when quitting out of Rmail.")
274 (defvar rmail-delete-message-hook nil
275 "List of functions to call when Rmail deletes a message.
276 When the hooks are called, the message has been marked deleted but is
277 still the current message in the Rmail buffer.")
279 ;; These may be altered by site-init.el to match the format of mmdf files
280 ;; delimiting used on a given host (delim1 and delim2 from the config
283 (defvar rmail-mmdf-delim1
"^\001\001\001\001\n"
284 "Regexp marking the start of an mmdf message.")
285 (defvar rmail-mmdf-delim2
"^\001\001\001\001\n"
286 "Regexp marking the end of an mmdf message.")
288 (defcustom rmail-message-filter nil
289 "If non-nil, a filter function for new messages in RMAIL.
290 Called with region narrowed to the message, including headers,
291 before obeying `rmail-ignored-headers'."
292 :group
'rmail-headers
295 (defcustom rmail-automatic-folder-directives nil
296 "List of directives specifying where to put a message.
297 Each element of the list is of the form:
299 (FOLDERNAME FIELD REGEXP [ FIELD REGEXP ] ... )
301 Where FOLDERNAME is the name of a BABYL format folder to put the
302 message. If any of the field regexp's are nil, then it is ignored.
304 If FOLDERNAME is \"/dev/null\", it is deleted.
305 If FOLDERNAME is nil then it is deleted, and skipped.
307 FIELD is the plain text name of a field in the message, such as
308 \"subject\" or \"from\". A FIELD of \"to\" will automatically include
309 all text from the \"cc\" field as well.
311 REGEXP is an expression to match in the preceeding specified FIELD.
312 FIELD/REGEXP pairs continue in the list.
315 (\"/dev/null\" \"from\" \"@spam.com\") ; delete all mail from spam.com
316 (\"RMS\" \"from\" \"rms@\") ; save all mail from RMS."
319 :type
'(repeat (sexp :tag
"Directive")))
321 (defvar rmail-reply-prefix
"Re: "
322 "String to prepend to Subject line when replying to a message.")
324 ;; Some mailers use "Re(2):" or "Re^2:" or "Re: Re:" or "Re[2]:".
325 ;; This pattern should catch all the common variants.
326 (defvar rmail-reply-regexp
"\\`\\(Re\\(([0-9]+)\\|\\[[0-9]+\\]\\|\\^[0-9]+\\)?: *\\)*"
327 "Regexp to delete from Subject line before inserting `rmail-reply-prefix'.")
329 (defcustom rmail-display-summary nil
330 "*If non-nil, Rmail always displays the summary buffer."
331 :group
'rmail-summary
334 (defvar rmail-inbox-list nil
)
335 (put 'rmail-inbox-list
'permanent-local t
)
337 (defvar rmail-keywords nil
)
338 (put 'rmail-keywords
'permanent-local t
)
340 (defvar rmail-buffer nil
341 "The RMAIL buffer related to the current buffer.
342 In an RMAIL buffer, this holds the RMAIL buffer itself.
343 In a summary buffer, this holds the RMAIL buffer it is a summary for.")
344 (put 'rmail-buffer
'permanent-local t
)
346 ;; Message counters and markers. Deleted flags.
348 (defvar rmail-current-message nil
)
349 (put 'rmail-current-message
'permanent-local t
)
351 (defvar rmail-total-messages nil
)
352 (put 'rmail-total-messages
'permanent-local t
)
354 (defvar rmail-message-vector nil
)
355 (put 'rmail-message-vector
'permanent-local t
)
357 (defvar rmail-deleted-vector nil
)
358 (put 'rmail-deleted-vector
'permanent-local t
)
360 (defvar rmail-msgref-vector nil
361 "In an Rmail buffer, a vector whose Nth element is a list (N).
362 When expunging renumbers messages, these lists are modified
363 by substituting the new message number into the existing list.")
364 (put 'rmail-msgref-vector
'permanent-local t
)
366 (defvar rmail-overlay-list nil
)
367 (put 'rmail-overlay-list
'permanent-local t
)
369 ;; These are used by autoloaded rmail-summary.
371 (defvar rmail-summary-buffer nil
)
372 (put 'rmail-summary-buffer
'permanent-local t
)
373 (defvar rmail-summary-vector nil
)
374 (put 'rmail-summary-vector
'permanent-local t
)
376 (defvar rmail-view-buffer nil
377 "Buffer which holds RMAIL message for MIME displaying.")
378 (put 'rmail-view-buffer
'permanent-local t
)
380 ;; `Sticky' default variables.
382 ;; Last individual label specified to a or k.
383 (defvar rmail-last-label nil
)
384 (put 'rmail-last-label
'permanent-local t
)
386 ;; Last set of values specified to C-M-n, C-M-p, C-M-s or C-M-l.
387 (defvar rmail-last-multi-labels nil
)
389 (defvar rmail-last-regexp nil
)
390 (put 'rmail-last-regexp
'permanent-local t
)
392 (defcustom rmail-default-file
"~/xmail"
393 "*Default file name for \\[rmail-output]."
396 (defcustom rmail-default-rmail-file
"~/XMAIL"
397 "*Default file name for \\[rmail-output-to-rmail-file]."
400 (defcustom rmail-default-body-file
"~/mailout"
401 "*Default file name for \\[rmail-output-body-to-file]."
406 ;; Mule and MIME related variables.
409 (defvar rmail-file-coding-system nil
410 "Coding system used in RMAIL file.
412 This is set to nil by default.")
415 (defcustom rmail-enable-mime nil
416 "*If non-nil, RMAIL uses MIME feature.
417 If the value is t, RMAIL automatically shows MIME decoded message.
418 If the value is neither t nor nil, RMAIL does not show MIME decoded message
419 until a user explicitly requires it."
420 :type
'(choice (const :tag
"on" t
)
421 (const :tag
"off" nil
)
422 (other :tag
"when asked" ask
))
425 (defvar rmail-enable-mime-composing nil
426 "*If non-nil, RMAIL uses `rmail-insert-mime-forwarded-message-function' to forward.")
429 (defvar rmail-show-mime-function nil
430 "Function to show MIME decoded message of RMAIL file.
431 This function is called when `rmail-enable-mime' is non-nil.
432 It is called with no argument.")
435 (defvar rmail-insert-mime-forwarded-message-function nil
436 "Function to insert a message in MIME format so it can be forwarded.
437 This function is called if `rmail-enable-mime' or
438 `rmail-enable-mime-composing' is non-nil.
439 It is called with one argument FORWARD-BUFFER, which is a
440 buffer containing the message to forward. The current buffer
441 is the outgoing mail buffer.")
444 (defvar rmail-insert-mime-resent-message-function nil
445 "Function to insert a message in MIME format so it can be resent.
446 This function is called if `rmail-enable-mime' is non-nil.
447 It is called with one argument FORWARD-BUFFER, which is a
448 buffer containing the message to forward. The current buffer
449 is the outgoing mail buffer.")
452 (defvar rmail-search-mime-message-function nil
453 "Function to check if a regexp matches a MIME message.
454 This function is called if `rmail-enable-mime' is non-nil.
455 It is called with two arguments MSG and REGEXP, where
456 MSG is the message number, REGEXP is the regular expression.")
459 (defvar rmail-search-mime-header-function nil
460 "Function to check if a regexp matches a header of MIME message.
461 This function is called if `rmail-enable-mime' is non-nil.
462 It is called with three arguments MSG, REGEXP, and LIMIT, where
463 MSG is the message number,
464 REGEXP is the regular expression,
465 LIMIT is the position specifying the end of header.")
468 (defvar rmail-mime-feature
'rmail-mime
469 "Feature to require to load MIME support in Rmail.
470 When starting Rmail, if `rmail-enable-mime' is non-nil,
471 this feature is required with `require'.")
474 (defvar rmail-decode-mime-charset t
475 "*Non-nil means a message is decoded by MIME's charset specification.
476 If this variable is nil, or the message has not MIME specification,
477 the message is decoded as normal way.
479 If the variable `rmail-enable-mime' is non-nil, this variables is
480 ignored, and all the decoding work is done by a feature specified by
481 the variable `rmail-mime-feature'.")
484 (defvar rmail-mime-charset-pattern
485 "^content-type:[ ]*text/plain;[ \t\n]*charset=\"?\\([^ \t\n\"]+\\)\"?"
486 "Regexp to match MIME-charset specification in a header of message.
487 The first parenthesized expression should match the MIME-charset name.")
490 ;;; Regexp matching the delimiter of messages in UNIX mail format
491 ;;; (UNIX From lines), minus the initial ^. Note that if you change
492 ;;; this expression, you must change the code in rmail-nuke-pinhead-header
493 ;;; that knows the exact ordering of the \\( \\) subexpressions.
494 (defvar rmail-unix-mail-delimiter
495 (let ((time-zone-regexp
496 (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
497 "\\|[-+]?[0-9][0-9][0-9][0-9]"
503 ;; Many things can happen to an RFC 822 mailbox before it is put into
504 ;; a `From' line. The leading phrase can be stripped, e.g.
505 ;; `Joe <@w.x:joe@y.z>' -> `<@w.x:joe@y.z>'. The <> can be stripped, e.g.
506 ;; `<@x.y:joe@y.z>' -> `@x.y:joe@y.z'. Everything starting with a CRLF
507 ;; can be removed, e.g.
508 ;; From: joe@y.z (Joe K
510 ;; can yield `From joe@y.z (Joe K Fri Mar 22 08:11:15 1996', and
513 ;; can yield `From Joe User Fri Mar 22 08:11:15 1996'.
514 ;; The mailbox can be removed or be replaced by white space, e.g.
515 ;; From: "Joe User"{space}{tab}
517 ;; can yield `From {space}{tab} Fri Mar 22 08:11:15 1996',
518 ;; where {space} and {tab} represent the Ascii space and tab characters.
519 ;; We want to match the results of any of these manglings.
520 ;; The following regexp rejects names whose first characters are
521 ;; obviously bogus, but after that anything goes.
522 "\\([^\0-\b\n-\r\^?].*\\)? "
524 ;; The time the message was sent.
525 "\\([^\0-\r \^?]+\\) +" ; day of the week
526 "\\([^\0-\r \^?]+\\) +" ; month
527 "\\([0-3]?[0-9]\\) +" ; day of month
528 "\\([0-2][0-9]:[0-5][0-9]\\(:[0-6][0-9]\\)?\\) *" ; time of day
530 ;; Perhaps a time zone, specified by an abbreviation, or by a
535 " \\([0-9][0-9]+\\) *"
537 ;; On some systems the time zone can appear after the year, too.
541 "\\(remote from .*\\)?"
546 (defvar rmail-font-lock-keywords
547 ;; These are all matched case-insensitively.
549 (let* ((cite-chars "[>|}]")
551 (cite-suffix (concat cite-prefix
"0-9_.@-`'\"")))
552 (list '("^\\(From\\|Sender\\|Resent-From\\):"
553 . font-lock-function-name-face
)
554 '("^Reply-To:.*$" . font-lock-function-name-face
)
555 '("^Subject:" . font-lock-comment-face
)
556 '("^\\(To\\|Apparently-To\\|Cc\\|Newsgroups\\):"
557 . font-lock-keyword-face
)
558 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
560 (,(concat "\\=[ \t]*"
561 "\\(\\([" cite-prefix
"]+[" cite-suffix
"]*\\)?"
562 "\\(" cite-chars
"[ \t]*\\)\\)+"
564 (beginning-of-line) (end-of-line)
565 (2 font-lock-constant-face nil t
)
566 (4 font-lock-comment-face nil t
)))
567 '("^\\(X-[a-z0-9-]+\\|In-reply-to\\|Date\\):.*\\(\n[ \t]+.*\\)*$"
568 . font-lock-string-face
))))
569 "Additional expressions to highlight in Rmail mode.")
571 ;; Perform BODY in the summary buffer
572 ;; in such a way that its cursor is properly updated in its own window.
573 (defmacro rmail-select-summary
(&rest body
)
574 `(let ((total rmail-total-messages
))
575 (if (rmail-summary-displayed)
576 (let ((window (selected-window)))
580 (pop-to-buffer rmail-summary-buffer
)
581 ;; rmail-total-messages is a buffer-local var
582 ;; in the rmail buffer.
583 ;; This way we make it available for the body
584 ;; even tho the rmail buffer is not current.
585 (let ((rmail-total-messages total
))
587 (select-window window
))))
589 (set-buffer rmail-summary-buffer
)
590 (let ((rmail-total-messages total
))
592 (rmail-maybe-display-summary)))
594 ;;;; *** Rmail Mode ***
596 ;; This variable is dynamically bound. The defvar is here to placate
597 ;; the byte compiler.
599 (defvar rmail-enable-multibyte nil
)
602 (defun rmail-require-mime-maybe ()
603 "Require `rmail-mime-feature' if that is non-nil.
604 Signal an error and set `rmail-mime-feature' to nil if the feature
606 (when rmail-enable-mime
608 (require rmail-mime-feature
)
610 (message "Feature `%s' not provided" rmail-mime-feature
)
612 (setq rmail-enable-mime nil
)))))
616 (defun rmail (&optional file-name-arg
)
617 "Read and edit incoming mail.
618 Moves messages into file named by `rmail-file-name' (a babyl format file)
619 and edits that file in RMAIL Mode.
620 Type \\[describe-mode] once editing that file, for a list of RMAIL commands.
622 May be called with file name as argument; then performs rmail editing on
623 that file, but does not copy any new mail into the file.
624 Interactively, if you supply a prefix argument, then you
625 have a chance to specify a file name with the minibuffer.
627 If `rmail-display-summary' is non-nil, make a summary for this RMAIL file."
628 (interactive (if current-prefix-arg
629 (list (read-file-name "Run rmail on RMAIL file: "))))
630 (rmail-require-mime-maybe)
631 (let* ((file-name (expand-file-name (or file-name-arg rmail-file-name
)))
632 ;; Use find-buffer-visiting, not get-file-buffer, for those users
633 ;; who have find-file-visit-truename set to t.
634 (existed (find-buffer-visiting file-name
))
635 ;; This binding is necessary because we must decide if we
636 ;; need code conversion while the buffer is unibyte
637 ;; (i.e. enable-multibyte-characters is nil).
638 (rmail-enable-multibyte
640 (with-current-buffer existed enable-multibyte-characters
)
641 (default-value 'enable-multibyte-characters
)))
642 ;; Since the file may contain messages of different encodings
643 ;; at the tail (non-BYBYL part), we can't decode them at once
644 ;; on reading. So, at first, we read the file without text
645 ;; code conversion, then decode the messages one by one by
646 ;; rmail-decode-babyl-format or
647 ;; rmail-convert-to-babyl-format.
648 (coding-system-for-read (and rmail-enable-multibyte
'raw-text
))
649 run-mail-hook msg-shown
)
650 ;; Like find-file, but in the case where a buffer existed
651 ;; and the file was reverted, recompute the message-data.
652 ;; We used to bind enable-local-variables to nil here,
653 ;; but that should not be needed now that rmail-mode
654 ;; sets it locally to nil.
655 ;; (Binding a variable locally with let is not safe if it has
656 ;; buffer-local bindings.)
657 (if (and existed
(not (verify-visited-file-modtime existed
)))
659 (find-file file-name
)
660 (if (and (verify-visited-file-modtime existed
)
661 (eq major-mode
'rmail-mode
))
662 (progn (rmail-forget-messages)
663 (rmail-set-message-counters))))
665 (let ((enable-local-variables nil
))
666 (find-file-noselect file-name
))))
667 (if (eq major-mode
'rmail-edit-mode
)
668 (error "Exit Rmail Edit mode before getting new mail"))
669 (if (and existed
(> (buffer-size) 0))
670 ;; Buffer not new and not empty; ensure in proper mode, but that's all.
671 (or (eq major-mode
'rmail-mode
)
672 (progn (rmail-mode-2)
673 (setq run-mail-hook t
)))
674 (setq run-mail-hook t
)
676 ;; Convert all or part to Babyl file if possible.
678 (goto-char (point-max)))
679 ;; As we have read a file by raw-text, the buffer is set to
680 ;; unibyte. We must make it multibyte if necessary.
681 (if (and rmail-enable-multibyte
682 (not enable-multibyte-characters
))
683 (set-buffer-multibyte t
))
684 ;; If necessary, scan to find all the messages.
685 (rmail-maybe-set-message-counters)
687 (unless (and (not file-name-arg
)
688 (rmail-get-new-mail))
689 (rmail-show-message (rmail-first-unseen-message)))
691 (if rmail-display-summary
(rmail-summary))
692 (rmail-construct-io-menu)
694 (run-hooks 'rmail-mode-hook
))))))
696 ;; Given the value of MAILPATH, return a list of inbox file names.
697 ;; This is turned off because it is not clear that the user wants
698 ;; all these inboxes to feed into the primary rmail file.
699 ; (defun rmail-convert-mailpath (string)
701 ; (while (setq idx (string-match "[%:]" string))
702 ; (let ((this (substring string 0 idx)))
703 ; (setq string (substring string (1+ idx)))
704 ; (setq list (cons (if (string-match "%" this)
705 ; (substring this 0 (string-match "%" this))
710 ; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
711 ; will not cause emacs 18.55 problems.
713 ;; This calls rmail-decode-babyl-format if the file is already Babyl.
715 (defun rmail-convert-file ()
718 (goto-char (point-min))
719 ;; If file doesn't start like a Babyl file,
720 ;; convert it to one, by adding a header and converting each message.
721 (cond ((looking-at "BABYL OPTIONS:"))
722 ((looking-at "Version: 5\n")
723 ;; Losing babyl file made by old version of Rmail.
724 ;; Just fix the babyl file header; don't make a new one,
725 ;; so we don't lose the Labels: file attribute, etc.
726 (let ((buffer-read-only nil
))
727 (insert "BABYL OPTIONS: -*- rmail -*-\n")))
728 ((equal (point-min) (point-max))
729 ;; Empty RMAIL file. Just insert the header.
730 (rmail-insert-rmail-file-header))
732 ;; Non-empty file in non-RMAIL format. Add header and convert.
734 (rmail-insert-rmail-file-header)))
735 ;; If file was not a Babyl file or if there are
736 ;; Unix format messages added at the end,
737 ;; convert file as necessary.
740 (goto-char (point-max))
741 (search-backward "\n\^_")
743 (looking-at "\n*From ")))
744 (let ((buffer-read-only nil
))
745 (message "Converting to Babyl format...")
746 ;; If file needs conversion, convert it all,
747 ;; except for the BABYL header.
748 ;; (rmail-convert-to-babyl-format would delete the header.)
749 (goto-char (point-min))
750 (search-forward "\n\^_" nil t
)
751 (narrow-to-region (point) (point-max))
752 (rmail-convert-to-babyl-format)
753 (message "Converting to Babyl format...done"))
754 (if (and (not rmail-enable-mime
)
755 rmail-enable-multibyte
)
756 ;; We still have to decode BABYL part.
757 (rmail-decode-babyl-format)))))
759 (defun rmail-insert-rmail-file-header ()
760 (let ((buffer-read-only nil
))
761 ;; -*-rmail-*- is here so that visiting the file normally
762 ;; recognizes it as an Rmail file.
763 (insert "BABYL OPTIONS: -*- rmail -*-
766 Note: This is the header of an rmail file.
767 Note: If you are seeing it in rmail,
768 Note: it means the file has no messages in it.\n\^_")))
770 ;; Decode Babyl formatted part at the head of current buffer by
771 ;; rmail-file-coding-system, or if it is nil, do auto conversion.
773 (defun rmail-decode-babyl-format ()
774 (let ((modifiedp (buffer-modified-p))
775 (buffer-read-only nil
)
776 (coding-system rmail-file-coding-system
)
778 (goto-char (point-min))
779 (search-forward "\n\^_" nil t
) ; Skip BABYL header.
781 (goto-char (point-max))
782 (search-backward "\n\^_" from
'mv
)
784 (unless (and coding-system
785 (coding-system-p coding-system
))
787 ;; Emacs 21.1 and later writes RMAIL files in emacs-mule, but
788 ;; earlier versions did that with the current buffer's encoding.
789 ;; So we want to favor detection of emacs-mule (whose normal
790 ;; priority is quite low), but still allow detection of other
791 ;; encodings if emacs-mule won't fit. The call to
792 ;; detect-coding-with-priority below achieves that.
793 (car (detect-coding-with-priority
795 '((coding-category-emacs-mule . emacs-mule
))))))
796 (unless (memq coding-system
797 '(undecided undecided-unix
))
798 (set-buffer-modified-p t
) ; avoid locking when decoding
799 (let ((buffer-undo-list t
))
800 (decode-coding-region from to coding-system
))
801 (setq coding-system last-coding-system-used
))
802 (set-buffer-modified-p modifiedp
)
803 (setq buffer-file-coding-system nil
)
804 (setq save-buffer-coding-system
805 (or coding-system
'undecided
))))
807 (defvar rmail-mode-map nil
)
810 (setq rmail-mode-map
(make-keymap))
811 (suppress-keymap rmail-mode-map
)
812 (define-key rmail-mode-map
"a" 'rmail-add-label
)
813 (define-key rmail-mode-map
"b" 'rmail-bury
)
814 (define-key rmail-mode-map
"c" 'rmail-continue
)
815 (define-key rmail-mode-map
"d" 'rmail-delete-forward
)
816 (define-key rmail-mode-map
"\C-d" 'rmail-delete-backward
)
817 (define-key rmail-mode-map
"e" 'rmail-edit-current-message
)
818 (define-key rmail-mode-map
"f" 'rmail-forward
)
819 (define-key rmail-mode-map
"g" 'rmail-get-new-mail
)
820 (define-key rmail-mode-map
"h" 'rmail-summary
)
821 (define-key rmail-mode-map
"i" 'rmail-input
)
822 (define-key rmail-mode-map
"j" 'rmail-show-message
)
823 (define-key rmail-mode-map
"k" 'rmail-kill-label
)
824 (define-key rmail-mode-map
"l" 'rmail-summary-by-labels
)
825 (define-key rmail-mode-map
"\e\C-h" 'rmail-summary
)
826 (define-key rmail-mode-map
"\e\C-l" 'rmail-summary-by-labels
)
827 (define-key rmail-mode-map
"\e\C-r" 'rmail-summary-by-recipients
)
828 (define-key rmail-mode-map
"\e\C-s" 'rmail-summary-by-regexp
)
829 (define-key rmail-mode-map
"\e\C-t" 'rmail-summary-by-topic
)
830 (define-key rmail-mode-map
"m" 'rmail-mail
)
831 (define-key rmail-mode-map
"\em" 'rmail-retry-failure
)
832 (define-key rmail-mode-map
"n" 'rmail-next-undeleted-message
)
833 (define-key rmail-mode-map
"\en" 'rmail-next-message
)
834 (define-key rmail-mode-map
"\e\C-n" 'rmail-next-labeled-message
)
835 (define-key rmail-mode-map
"o" 'rmail-output-to-rmail-file
)
836 (define-key rmail-mode-map
"\C-o" 'rmail-output
)
837 (define-key rmail-mode-map
"p" 'rmail-previous-undeleted-message
)
838 (define-key rmail-mode-map
"\ep" 'rmail-previous-message
)
839 (define-key rmail-mode-map
"\e\C-p" 'rmail-previous-labeled-message
)
840 (define-key rmail-mode-map
"q" 'rmail-quit
)
841 (define-key rmail-mode-map
"r" 'rmail-reply
)
842 ;; I find I can't live without the default M-r command -- rms.
843 ;; (define-key rmail-mode-map "\er" 'rmail-search-backwards)
844 (define-key rmail-mode-map
"s" 'rmail-expunge-and-save
)
845 (define-key rmail-mode-map
"\es" 'rmail-search
)
846 (define-key rmail-mode-map
"t" 'rmail-toggle-header
)
847 (define-key rmail-mode-map
"u" 'rmail-undelete-previous-message
)
848 (define-key rmail-mode-map
"w" 'rmail-output-body-to-file
)
849 (define-key rmail-mode-map
"x" 'rmail-expunge
)
850 (define-key rmail-mode-map
"." 'rmail-beginning-of-message
)
851 (define-key rmail-mode-map
"<" 'rmail-first-message
)
852 (define-key rmail-mode-map
">" 'rmail-last-message
)
853 (define-key rmail-mode-map
" " 'scroll-up
)
854 (define-key rmail-mode-map
"\177" 'scroll-down
)
855 (define-key rmail-mode-map
"?" 'describe-mode
)
856 (define-key rmail-mode-map
"\C-c\C-s\C-d" 'rmail-sort-by-date
)
857 (define-key rmail-mode-map
"\C-c\C-s\C-s" 'rmail-sort-by-subject
)
858 (define-key rmail-mode-map
"\C-c\C-s\C-a" 'rmail-sort-by-author
)
859 (define-key rmail-mode-map
"\C-c\C-s\C-r" 'rmail-sort-by-recipient
)
860 (define-key rmail-mode-map
"\C-c\C-s\C-c" 'rmail-sort-by-correspondent
)
861 (define-key rmail-mode-map
"\C-c\C-s\C-l" 'rmail-sort-by-lines
)
862 (define-key rmail-mode-map
"\C-c\C-s\C-k" 'rmail-sort-by-labels
)
863 (define-key rmail-mode-map
"\C-c\C-n" 'rmail-next-same-subject
)
864 (define-key rmail-mode-map
"\C-c\C-p" 'rmail-previous-same-subject
)
867 (define-key rmail-mode-map
[menu-bar
] (make-sparse-keymap))
869 (define-key rmail-mode-map
[menu-bar classify
]
870 (cons "Classify" (make-sparse-keymap "Classify")))
872 (define-key rmail-mode-map
[menu-bar classify input-menu
]
875 (define-key rmail-mode-map
[menu-bar classify output-menu
]
878 (define-key rmail-mode-map
[menu-bar classify output-body
]
879 '("Output body to file..." . rmail-output-body-to-file
))
881 (define-key rmail-mode-map
[menu-bar classify output-inbox
]
882 '("Output (inbox)..." . rmail-output
))
884 (define-key rmail-mode-map
[menu-bar classify output
]
885 '("Output (Rmail)..." . rmail-output-to-rmail-file
))
887 (define-key rmail-mode-map
[menu-bar classify kill-label
]
888 '("Kill Label..." . rmail-kill-label
))
890 (define-key rmail-mode-map
[menu-bar classify add-label
]
891 '("Add Label..." . rmail-add-label
))
893 (define-key rmail-mode-map
[menu-bar summary
]
894 (cons "Summary" (make-sparse-keymap "Summary")))
896 (define-key rmail-mode-map
[menu-bar summary senders
]
897 '("By Senders..." . rmail-summary-by-senders
))
899 (define-key rmail-mode-map
[menu-bar summary labels
]
900 '("By Labels..." . rmail-summary-by-labels
))
902 (define-key rmail-mode-map
[menu-bar summary recipients
]
903 '("By Recipients..." . rmail-summary-by-recipients
))
905 (define-key rmail-mode-map
[menu-bar summary topic
]
906 '("By Topic..." . rmail-summary-by-topic
))
908 (define-key rmail-mode-map
[menu-bar summary regexp
]
909 '("By Regexp..." . rmail-summary-by-regexp
))
911 (define-key rmail-mode-map
[menu-bar summary all
]
912 '("All" . rmail-summary
))
914 (define-key rmail-mode-map
[menu-bar mail
]
915 (cons "Mail" (make-sparse-keymap "Mail")))
917 (define-key rmail-mode-map
[menu-bar mail rmail-get-new-mail
]
918 '("Get New Mail" . rmail-get-new-mail
))
920 (define-key rmail-mode-map
[menu-bar mail lambda
]
923 (define-key rmail-mode-map
[menu-bar mail continue
]
924 '("Continue" . rmail-continue
))
926 (define-key rmail-mode-map
[menu-bar mail resend
]
927 '("Re-send..." . rmail-resend
))
929 (define-key rmail-mode-map
[menu-bar mail forward
]
930 '("Forward" . rmail-forward
))
932 (define-key rmail-mode-map
[menu-bar mail retry
]
933 '("Retry" . rmail-retry-failure
))
935 (define-key rmail-mode-map
[menu-bar mail reply
]
936 '("Reply" . rmail-reply
))
938 (define-key rmail-mode-map
[menu-bar mail mail
]
939 '("Mail" . rmail-mail
))
941 (define-key rmail-mode-map
[menu-bar delete
]
942 (cons "Delete" (make-sparse-keymap "Delete")))
944 (define-key rmail-mode-map
[menu-bar delete expunge
/save
]
945 '("Expunge/Save" . rmail-expunge-and-save
))
947 (define-key rmail-mode-map
[menu-bar delete expunge
]
948 '("Expunge" . rmail-expunge
))
950 (define-key rmail-mode-map
[menu-bar delete undelete
]
951 '("Undelete" . rmail-undelete-previous-message
))
953 (define-key rmail-mode-map
[menu-bar delete delete
]
954 '("Delete" . rmail-delete-forward
))
956 (define-key rmail-mode-map
[menu-bar move
]
957 (cons "Move" (make-sparse-keymap "Move")))
959 (define-key rmail-mode-map
[menu-bar move search-back
]
960 '("Search Back..." . rmail-search-backwards
))
962 (define-key rmail-mode-map
[menu-bar move search
]
963 '("Search..." . rmail-search
))
965 (define-key rmail-mode-map
[menu-bar move previous
]
966 '("Previous Nondeleted" . rmail-previous-undeleted-message
))
968 (define-key rmail-mode-map
[menu-bar move next
]
969 '("Next Nondeleted" . rmail-next-undeleted-message
))
971 (define-key rmail-mode-map
[menu-bar move last
]
972 '("Last" . rmail-last-message
))
974 (define-key rmail-mode-map
[menu-bar move first
]
975 '("First" . rmail-first-message
))
977 (define-key rmail-mode-map
[menu-bar move previous
]
978 '("Previous" . rmail-previous-message
))
980 (define-key rmail-mode-map
[menu-bar move next
]
981 '("Next" . rmail-next-message
))
983 ;; Rmail mode is suitable only for specially formatted data.
984 (put 'rmail-mode
'mode-class
'special
)
986 (defun rmail-mode-kill-summary ()
987 (if rmail-summary-buffer
(kill-buffer rmail-summary-buffer
)))
991 "Rmail Mode is used by \\<rmail-mode-map>\\[rmail] for editing Rmail files.
992 All normal editing commands are turned off.
993 Instead, these commands are available:
995 \\[rmail-beginning-of-message] Move point to front of this message (same as \\[beginning-of-buffer]).
996 \\[scroll-up] Scroll to next screen of this message.
997 \\[scroll-down] Scroll to previous screen of this message.
998 \\[rmail-next-undeleted-message] Move to Next non-deleted message.
999 \\[rmail-previous-undeleted-message] Move to Previous non-deleted message.
1000 \\[rmail-next-message] Move to Next message whether deleted or not.
1001 \\[rmail-previous-message] Move to Previous message whether deleted or not.
1002 \\[rmail-first-message] Move to the first message in Rmail file.
1003 \\[rmail-last-message] Move to the last message in Rmail file.
1004 \\[rmail-show-message] Jump to message specified by numeric position in file.
1005 \\[rmail-search] Search for string and show message it is found in.
1006 \\[rmail-delete-forward] Delete this message, move to next nondeleted.
1007 \\[rmail-delete-backward] Delete this message, move to previous nondeleted.
1008 \\[rmail-undelete-previous-message] Undelete message. Tries current message, then earlier messages
1009 till a deleted message is found.
1010 \\[rmail-edit-current-message] Edit the current message. \\[rmail-cease-edit] to return to Rmail.
1011 \\[rmail-expunge] Expunge deleted messages.
1012 \\[rmail-expunge-and-save] Expunge and save the file.
1013 \\[rmail-quit] Quit Rmail: expunge, save, then switch to another buffer.
1014 \\[save-buffer] Save without expunging.
1015 \\[rmail-get-new-mail] Move new mail from system spool directory into this file.
1016 \\[rmail-mail] Mail a message (same as \\[mail-other-window]).
1017 \\[rmail-continue] Continue composing outgoing message started before.
1018 \\[rmail-reply] Reply to this message. Like \\[rmail-mail] but initializes some fields.
1019 \\[rmail-retry-failure] Send this message again. Used on a mailer failure message.
1020 \\[rmail-forward] Forward this message to another user.
1021 \\[rmail-output-to-rmail-file] Output this message to an Rmail file (append it).
1022 \\[rmail-output] Output this message to a Unix-format mail file (append it).
1023 \\[rmail-output-body-to-file] Save message body to a file. Default filename comes from Subject line.
1024 \\[rmail-input] Input Rmail file. Run Rmail on that file.
1025 \\[rmail-add-label] Add label to message. It will be displayed in the mode line.
1026 \\[rmail-kill-label] Kill label. Remove a label from current message.
1027 \\[rmail-next-labeled-message] Move to Next message with specified label
1028 (label defaults to last one specified).
1029 Standard labels: filed, unseen, answered, forwarded, deleted.
1030 Any other label is present only if you add it with \\[rmail-add-label].
1031 \\[rmail-previous-labeled-message] Move to Previous message with specified label
1032 \\[rmail-summary] Show headers buffer, with a one line summary of each message.
1033 \\[rmail-summary-by-labels] Summarize only messages with particular label(s).
1034 \\[rmail-summary-by-recipients] Summarize only messages with particular recipient(s).
1035 \\[rmail-summary-by-regexp] Summarize only messages with particular regexp(s).
1036 \\[rmail-summary-by-topic] Summarize only messages with subject line regexp(s).
1037 \\[rmail-toggle-header] Toggle display of complete header."
1039 (let ((finding-rmail-file (not (eq major-mode
'rmail-mode
))))
1041 (when (and finding-rmail-file
1042 (null coding-system-for-read
)
1043 default-enable-multibyte-characters
)
1044 (let ((rmail-enable-multibyte t
))
1045 (rmail-require-mime-maybe)
1046 (rmail-convert-file)
1047 (goto-char (point-max))
1048 (set-buffer-multibyte t
)))
1049 (rmail-set-message-counters)
1050 (rmail-show-message rmail-total-messages
)
1051 (when finding-rmail-file
1052 (when rmail-display-summary
1054 (rmail-construct-io-menu))
1055 (run-hooks 'rmail-mode-hook
)))
1057 (defun rmail-mode-2 ()
1058 (kill-all-local-variables)
1060 (rmail-perm-variables)
1063 (defun rmail-mode-1 ()
1064 (setq major-mode
'rmail-mode
)
1065 (setq mode-name
"RMAIL")
1066 (setq buffer-read-only t
)
1067 ;; No need to auto save RMAIL files in normal circumstances
1068 ;; because they contain no info except attribute changes
1069 ;; and deletion of messages.
1070 ;; The one exception is when messages are copied into an Rmail mode buffer.
1071 ;; rmail-output-to-rmail-file enables auto save when you do that.
1072 (setq buffer-auto-save-file-name nil
)
1073 (setq mode-line-modified
"--")
1074 (use-local-map rmail-mode-map
)
1075 (set-syntax-table text-mode-syntax-table
)
1076 (setq local-abbrev-table text-mode-abbrev-table
))
1078 ;; Set up the permanent locals associated with an Rmail file.
1079 (defun rmail-perm-variables ()
1080 (make-local-variable 'rmail-last-label
)
1081 (make-local-variable 'rmail-last-regexp
)
1082 (make-local-variable 'rmail-deleted-vector
)
1083 (make-local-variable 'rmail-buffer
)
1084 (setq rmail-buffer
(current-buffer))
1085 (make-local-variable 'rmail-view-buffer
)
1086 (setq rmail-view-buffer rmail-buffer
)
1087 (make-local-variable 'rmail-summary-buffer
)
1088 (make-local-variable 'rmail-summary-vector
)
1089 (make-local-variable 'rmail-current-message
)
1090 (make-local-variable 'rmail-total-messages
)
1091 (make-local-variable 'rmail-overlay-list
)
1092 (setq rmail-overlay-list nil
)
1093 (make-local-variable 'rmail-message-vector
)
1094 (make-local-variable 'rmail-msgref-vector
)
1095 (make-local-variable 'rmail-inbox-list
)
1096 (setq rmail-inbox-list
(rmail-parse-file-inboxes))
1097 ;; Provide default set of inboxes for primary mail file ~/RMAIL.
1098 (and (null rmail-inbox-list
)
1099 (or (equal buffer-file-name
(expand-file-name rmail-file-name
))
1100 (equal buffer-file-truename
1101 (abbreviate-file-name (file-truename rmail-file-name
))))
1102 (setq rmail-inbox-list
1103 (or rmail-primary-inbox-list
1104 (list (or (getenv "MAIL")
1105 (concat rmail-spool-directory
1106 (user-login-name)))))))
1107 (make-local-variable 'rmail-keywords
)
1108 ;; this gets generated as needed
1109 (setq rmail-keywords nil
))
1111 ;; Set up the non-permanent locals associated with Rmail mode.
1112 (defun rmail-variables ()
1113 (make-local-variable 'save-buffer-coding-system
)
1114 ;; If we don't already have a value for save-buffer-coding-system,
1115 ;; get it from buffer-file-coding-system, and clear that
1116 ;; because it should be determined in rmail-show-message.
1117 (unless save-buffer-coding-system
1118 (setq save-buffer-coding-system
(or buffer-file-coding-system
'undecided
))
1119 (setq buffer-file-coding-system nil
))
1120 ;; Don't let a local variables list in a message cause confusion.
1121 (make-local-variable 'local-enable-local-variables
)
1122 (setq local-enable-local-variables nil
)
1123 (make-local-variable 'revert-buffer-function
)
1124 (setq revert-buffer-function
'rmail-revert
)
1125 (make-local-variable 'font-lock-defaults
)
1126 (setq font-lock-defaults
1127 '(rmail-font-lock-keywords
1129 (font-lock-maximum-size . nil
)
1130 (font-lock-fontify-buffer-function . rmail-fontify-buffer-function
)
1131 (font-lock-unfontify-buffer-function . rmail-unfontify-buffer-function
)
1132 (font-lock-inhibit-thing-lock .
(lazy-lock-mode fast-lock-mode
))))
1133 (make-local-variable 'require-final-newline
)
1134 (setq require-final-newline nil
)
1135 (make-local-variable 'version-control
)
1136 (setq version-control
'never
)
1137 (make-local-variable 'kill-buffer-hook
)
1138 (add-hook 'kill-buffer-hook
'rmail-mode-kill-summary
)
1139 (make-local-variable 'file-precious-flag
)
1140 (setq file-precious-flag t
))
1142 ;; Handle M-x revert-buffer done in an rmail-mode buffer.
1143 (defun rmail-revert (arg noconfirm
)
1144 (set-buffer rmail-buffer
)
1145 (let* ((revert-buffer-function (default-value 'revert-buffer-function
))
1146 (rmail-enable-multibyte enable-multibyte-characters
)
1147 ;; See similar code in `rmail'.
1148 (coding-system-for-read (and rmail-enable-multibyte
'raw-text
)))
1149 ;; Call our caller again, but this time it does the default thing.
1150 (if (revert-buffer arg noconfirm
)
1151 ;; If the user said "yes", and we changed something,
1152 ;; reparse the messages.
1154 (set-buffer rmail-buffer
)
1156 ;; Convert all or part to Babyl file if possible.
1157 (rmail-convert-file)
1158 ;; We have read the file as raw-text, so the buffer is set to
1159 ;; unibyte. Make it multibyte if necessary.
1160 (if (and rmail-enable-multibyte
1161 (not enable-multibyte-characters
))
1162 (set-buffer-multibyte t
))
1163 (goto-char (point-max))
1164 (rmail-set-message-counters)
1165 (rmail-show-message rmail-total-messages
)
1166 (run-hooks 'rmail-mode-hook
)))))
1168 ;; Return a list of files from this buffer's Mail: option.
1169 ;; Does not assume that messages have been parsed.
1170 ;; Just returns nil if buffer does not look like Babyl format.
1171 (defun rmail-parse-file-inboxes ()
1176 (cond ((looking-at "BABYL OPTIONS:")
1177 (search-forward "\n\^_" nil
'move
)
1178 (narrow-to-region 1 (point))
1180 (if (search-forward "\nMail:" nil t
)
1182 (narrow-to-region (point) (progn (end-of-line) (point)))
1183 (goto-char (point-min))
1184 (mail-parse-comma-list))))))))
1186 (defun rmail-expunge-and-save ()
1187 "Expunge and save RMAIL file."
1190 (set-buffer rmail-buffer
)
1192 (if (rmail-summary-exists)
1193 (rmail-select-summary (set-buffer-modified-p nil
))))
1195 (defun rmail-quit ()
1197 Hook `rmail-quit-hook' is run after expunging."
1199 (rmail-expunge-and-save)
1200 (when (boundp 'rmail-quit-hook
)
1201 (run-hooks 'rmail-quit-hook
))
1202 ;; Don't switch to the summary buffer even if it was recently visible.
1203 (when rmail-summary-buffer
1204 (replace-buffer-in-windows rmail-summary-buffer
)
1205 (bury-buffer rmail-summary-buffer
))
1206 (if rmail-enable-mime
1207 (let ((obuf rmail-buffer
)
1208 (ovbuf rmail-view-buffer
))
1209 (set-buffer rmail-view-buffer
)
1211 (replace-buffer-in-windows ovbuf
)
1212 (replace-buffer-in-windows obuf
)
1214 (let ((obuf (current-buffer)))
1216 (replace-buffer-in-windows obuf
))))
1218 (defun rmail-bury ()
1219 "Bury current Rmail buffer and its summary buffer."
1221 ;; This let var was called rmail-buffer, but that interfered
1222 ;; with the buffer-local var used in summary buffers.
1223 (let ((buffer-to-bury (current-buffer)))
1224 (if (rmail-summary-exists)
1226 (while (setq window
(get-buffer-window rmail-summary-buffer
))
1227 (quit-window nil window
))
1228 (bury-buffer rmail-summary-buffer
)))
1231 (defun rmail-duplicate-message ()
1232 "Create a duplicated copy of the current message.
1233 The duplicate copy goes into the Rmail file just after the
1237 (let ((buffer-read-only nil
)
1238 (number rmail-current-message
)
1239 (string (buffer-substring (rmail-msgbeg rmail-current-message
)
1240 (rmail-msgend rmail-current-message
))))
1241 (goto-char (rmail-msgend rmail-current-message
))
1243 (rmail-forget-messages)
1244 (rmail-show-message number
)
1245 (message "Message duplicated")))
1248 (defun rmail-input (filename)
1249 "Run Rmail on file FILENAME."
1250 (interactive "FRun rmail on RMAIL file: ")
1254 ;; This used to scan subdirectories recursively, but someone pointed out
1255 ;; that if the user wants that, person can put all the files in one dir.
1256 ;; And the recursive scan was slow. So I took it out.
1258 (defun rmail-find-all-files (start)
1259 "Return list of file in dir START that match `rmail-secondary-file-regexp'."
1260 (if (file-accessible-directory-p start
)
1262 (let* ((case-fold-search t
)
1263 (files (directory-files start t rmail-secondary-file-regexp
)))
1264 ;; Sort here instead of in directory-files
1265 ;; because this list is usually much shorter.
1266 (sort files
'string
<))))
1268 (defun rmail-list-to-menu (menu-name l action
&optional full-name
)
1269 (let ((menu (make-sparse-keymap menu-name
)))
1271 (function (lambda (item)
1276 (rmail-list-to-menu (car item
) (cdr item
)
1279 (concat full-name
"/"
1282 (setq name
(car item
)))
1286 (list 'lambda
() '(interactive)
1290 (concat full-name
"/" item
)
1292 rmail-secondary-file-directory
))))))
1293 (define-key menu
(vector (intern name
))
1294 (cons name command
)))))
1298 ;; This command is always "disabled" when it appears in a menu.
1299 (put 'rmail-disable-menu
'menu-enable
''nil
)
1301 (defun rmail-construct-io-menu ()
1302 (let ((files (rmail-find-all-files rmail-secondary-file-directory
)))
1305 (define-key rmail-mode-map
[menu-bar classify input-menu
]
1306 (cons "Input Rmail File"
1307 (rmail-list-to-menu "Input Rmail File"
1310 (define-key rmail-mode-map
[menu-bar classify output-menu
]
1311 (cons "Output Rmail File"
1312 (rmail-list-to-menu "Output Rmail File"
1314 'rmail-output-to-rmail-file
))))
1316 (define-key rmail-mode-map
[menu-bar classify input-menu
]
1317 '("Input Rmail File" . rmail-disable-menu
))
1318 (define-key rmail-mode-map
[menu-bar classify output-menu
]
1319 '("Output Rmail File" . rmail-disable-menu
)))))
1322 ;;;; *** Rmail input ***
1324 ;; RLK feature not added in this version:
1325 ;; argument specifies inbox file or files in various ways.
1327 (defun rmail-get-new-mail (&optional file-name
)
1328 "Move any new mail from this RMAIL file's inbox files.
1329 The inbox files can be specified with the file's Mail: option. The
1330 variable `rmail-primary-inbox-list' specifies the inboxes for your
1331 primary RMAIL file if it has no Mail: option. By default, this is
1332 your /usr/spool/mail/$USER.
1334 You can also specify the file to get new mail from. In this case, the
1335 file of new mail is not changed or deleted. Noninteractively, you can
1336 pass the inbox file name as an argument. Interactively, a prefix
1337 argument causes us to read a file name and use that file as the inbox.
1339 If the variable `rmail-preserve-inbox' is non-nil, new mail will
1340 always be left in inbox files rather than deleted.
1342 This function runs `rmail-get-new-mail-hook' before saving the updated file.
1343 It returns t if it got any new messages."
1345 (list (if current-prefix-arg
1346 (read-file-name "Get new mail from file: "))))
1347 (run-hooks 'rmail-before-get-new-mail-hook
)
1348 ;; If the disk file has been changed from under us,
1349 ;; revert to it before we get new mail.
1350 (or (verify-visited-file-modtime (current-buffer))
1351 (find-file (buffer-file-name)))
1352 (set-buffer rmail-buffer
)
1353 (rmail-maybe-set-message-counters)
1355 ;; Get rid of all undo records for this buffer.
1356 (or (eq buffer-undo-list t
)
1357 (setq buffer-undo-list nil
))
1358 (let ((all-files (if file-name
(list file-name
)
1360 (rmail-enable-multibyte (default-value 'enable-multibyte-characters
))
1365 (let ((opoint (point))
1368 ;; If buffer has not changed yet, and has not been saved yet,
1369 ;; don't replace the old backup file now.
1370 (make-backup-files (and make-backup-files
(buffer-modified-p)))
1371 (buffer-read-only nil
)
1372 ;; Don't make undo records for what we do in getting mail.
1373 (buffer-undo-list t
)
1375 ;; Files to insert this time around.
1377 ;; Last names of those files.
1379 ;; Pull files off all-files onto files
1380 ;; as long as there is no name conflict.
1381 ;; A conflict happens when two inbox file names
1382 ;; have the same last component.
1383 (while (and all-files
1384 (not (member (file-name-nondirectory (car all-files
))
1386 (setq files
(cons (car all-files
) files
)
1388 (cons (file-name-nondirectory (car all-files
)) files
))
1389 (setq all-files
(cdr all-files
)))
1390 ;; Put them back in their original order.
1391 (setq files
(nreverse files
))
1393 (goto-char (point-max))
1394 (skip-chars-backward " \t\n") ; just in case of brain damage
1395 (delete-region (point) (point-max)) ; caused by require-final-newline
1398 (narrow-to-region (point) (point))
1399 ;; Read in the contents of the inbox files,
1400 ;; renaming them as necessary,
1401 ;; and adding to the list of files to delete eventually.
1403 (rmail-insert-inbox-text files nil
)
1404 (setq delete-files
(rmail-insert-inbox-text files t
)))
1405 ;; Scan the new text and convert each message to babyl format.
1406 (goto-char (point-min))
1409 (setq new-messages
(rmail-convert-to-babyl-format)
1411 ;; Try to delete the garbage just inserted.
1412 (or success
(delete-region (point-min) (point-max)))
1413 ;; If we could not convert the file's inboxes,
1414 ;; rename the files we tried to read
1415 ;; so we won't over and over again.
1416 (if (and (not file-name
) (not success
))
1417 (let ((delfiles delete-files
)
1420 (while (file-exists-p (format "RMAILOSE.%d" count
))
1421 (setq count
(1+ count
)))
1422 (rename-file (car delfiles
)
1423 (format "RMAILOSE.%d" count
))
1424 (setq delfiles
(cdr delfiles
))))))
1425 (or (zerop new-messages
)
1428 (search-backward "\n\^_" nil t
)
1429 (narrow-to-region (point) (point-max))
1430 (goto-char (1+ (point-min)))
1431 (rmail-count-new-messages)
1432 (run-hooks 'rmail-get-new-mail-hook
)
1434 ;; Delete the old files, now that babyl file is saved.
1437 ;; First, try deleting.
1439 (delete-file (car delete-files
))
1441 ;; If we can't delete it, truncate it.
1442 (write-region (point) (point) (car delete-files
))))
1444 (setq delete-files
(cdr delete-files
)))))
1445 (if (= new-messages
0)
1446 (progn (goto-char opoint
)
1447 (if (or file-name rmail-inbox-list
)
1448 (message "(No new mail has arrived)")))
1449 (if (rmail-summary-exists)
1450 (rmail-select-summary
1451 (rmail-update-summary)))
1452 (message "%d new message%s read"
1453 new-messages
(if (= 1 new-messages
) "" "s"))
1454 ;; Move to the first new message
1455 ;; unless we have other unseen messages before it.
1456 (rmail-show-message (rmail-first-unseen-message))
1457 (run-hooks 'rmail-after-get-new-mail-hook
)
1460 ;; Don't leave the buffer screwed up if we get a disk-full error.
1461 (or found
(rmail-show-message)))))
1463 (defun rmail-insert-inbox-text (files renamep
)
1464 ;; Detect a locked file now, so that we avoid moving mail
1465 ;; out of the real inbox file. (That could scare people.)
1466 (or (memq (file-locked-p buffer-file-name
) '(nil t
))
1467 (error "RMAIL file %s is locked"
1468 (file-name-nondirectory buffer-file-name
)))
1469 (let (file tofile delete-files movemail popmail got-password password
)
1471 ;; Handle POP mailbox names specially; don't expand as filenames
1472 ;; in case the userid contains a directory separator.
1473 (setq file
(car files
))
1474 (setq popmail
(string-match "^po:" file
))
1477 (setq file
(file-truename
1478 (substitute-in-file-name (expand-file-name file
)))))
1479 (setq tofile
(expand-file-name
1480 ;; Generate name to move to from inbox name,
1481 ;; in case of multiple inboxes that need moving.
1482 (concat ".newmail-" (file-name-nondirectory file
))
1483 ;; Use the directory of this rmail file
1484 ;; because it's a nuisance to use the homedir
1485 ;; if that is on a full disk and this rmail
1487 (file-name-directory
1488 (expand-file-name buffer-file-name
))))
1489 ;; Always use movemail to rename the file,
1490 ;; since there can be mailboxes in various directories.
1492 ;;; ;; If getting from mail spool directory,
1493 ;;; ;; use movemail to move rather than just renaming,
1494 ;;; ;; so as to interlock with the mailer.
1495 ;;; (setq movemail (string= file
1497 ;;; (concat rmail-spool-directory
1498 ;;; (file-name-nondirectory file)))))
1499 (if (and movemail
(not popmail
))
1501 ;; On some systems, /usr/spool/mail/foo is a directory
1502 ;; and the actual inbox is /usr/spool/mail/foo/foo.
1503 (if (file-directory-p file
)
1504 (setq file
(expand-file-name (user-login-name)
1507 (if rmail-pop-password-required
1508 (progn (setq got-password
(not (rmail-have-password)))
1509 (setq password
(rmail-get-pop-password))))
1510 (if (memq system-type
'(windows-nt cygwin
))
1511 ;; cannot have "po:" in file name
1514 (concat ".newmail-pop-"
1515 (file-name-nondirectory (substring file
3)))
1516 (file-name-directory
1517 (expand-file-name buffer-file-name
)))))
1518 (message "Getting mail from post office ..."))
1519 ((and (file-exists-p tofile
)
1520 (/= 0 (nth 7 (file-attributes tofile
))))
1521 (message "Getting mail from %s..." tofile
))
1522 ((and (file-exists-p file
)
1523 (/= 0 (nth 7 (file-attributes file
))))
1524 (message "Getting mail from %s..." file
)))
1525 ;; Set TOFILE if have not already done so, and
1526 ;; rename or copy the file FILE to TOFILE if and as appropriate.
1527 (cond ((not renamep
)
1529 ((or (file-exists-p tofile
) (and (not popmail
)
1530 (not (file-exists-p file
))))
1532 ((and (not movemail
) (not popmail
))
1533 ;; Try copying. If that fails (perhaps no space) and
1534 ;; we're allowed to blow away the inbox, rename instead.
1535 (if rmail-preserve-inbox
1536 (copy-file file tofile nil
)
1538 (copy-file file tofile nil
)
1540 ;; Third arg is t so we can replace existing file TOFILE.
1541 (rename-file file tofile t
))))
1542 ;; Make the real inbox file empty.
1543 ;; Leaving it deleted could cause lossage
1544 ;; because mailers often won't create the file.
1545 (if (not rmail-preserve-inbox
)
1547 (write-region (point) (point) file
)
1553 (setq errors
(generate-new-buffer " *rmail loss*"))
1554 (buffer-disable-undo errors
)
1557 (list (or rmail-movemail-program
1558 (expand-file-name "movemail"
1561 (if rmail-preserve-inbox
1564 rmail-movemail-flags
1566 (if password
(list password
) nil
))))
1567 (apply 'call-process args
))
1568 (if (not (buffer-modified-p errors
))
1569 ;; No output => movemail won
1572 (subst-char-in-region (point-min) (point-max)
1574 (goto-char (point-max))
1575 (skip-chars-backward " \t")
1576 (delete-region (point) (point-max))
1577 (goto-char (point-min))
1578 (if (looking-at "movemail: ")
1579 (delete-region (point-min) (match-end 0)))
1581 (message "movemail: %s"
1582 (buffer-substring (point-min)
1584 ;; If we just read the password, most likely it is
1585 ;; wrong. Otherwise, see if there is a specific
1586 ;; reason to think that the problem is a wrong passwd.
1587 (if (or got-password
1588 (re-search-forward rmail-pop-password-error
1590 (rmail-set-pop-password nil
))
1593 (if errors
(kill-buffer errors
))))))
1594 ;; At this point, TOFILE contains the name to read:
1595 ;; Either the alternate name (if we renamed)
1596 ;; or the actual inbox (if not renaming).
1597 (if (file-exists-p tofile
)
1598 (let ((coding-system-for-read 'no-conversion
)
1600 (goto-char (point-max))
1601 (setq size
(nth 1 (insert-file-contents tofile
)))
1602 (goto-char (point-max))
1603 (or (= (preceding-char) ?
\n)
1606 (if (not (and rmail-preserve-inbox
(string= file tofile
)))
1607 (setq delete-files
(cons tofile delete-files
)))))
1609 (setq files
(cdr files
)))
1612 ;; Decode the region specified by FROM and TO by CODING.
1613 ;; If CODING is nil or an invalid coding system, decode by `undecided'.
1614 (defun rmail-decode-region (from to coding
)
1615 (if (or (not coding
) (not (coding-system-p coding
)))
1616 (setq coding
'undecided
))
1617 (decode-coding-region from to coding
))
1619 ;; the rmail-break-forwarded-messages feature is not implemented
1620 (defun rmail-convert-to-babyl-format ()
1621 (let ((count 0) start
1622 (case-fold-search nil
)
1623 (invalid-input-resync
1624 (function (lambda ()
1625 (message "Invalid Babyl format in inbox!")
1627 ;; Try to get back in sync with a real message.
1628 (if (re-search-forward
1629 (concat rmail-mmdf-delim1
"\\|^From") nil t
)
1631 (goto-char (point-max)))))))
1632 (goto-char (point-min))
1635 (setq start
(point))
1636 (cond ((looking-at "BABYL OPTIONS:");Babyl header
1637 (if (search-forward "\n\^_" nil t
)
1638 ;; If we find the proper terminator, delete through there.
1639 (delete-region (point-min) (point))
1640 (funcall invalid-input-resync
)
1641 (delete-region (point-min) (point))))
1642 ;; Babyl format message
1644 (or (search-forward "\n\^_" nil t
)
1645 (funcall invalid-input-resync
))
1646 (setq count
(1+ count
))
1647 ;; Make sure there is no extra white space after the ^_
1648 ;; at the end of the message.
1649 ;; Narrowing will make sure that whatever follows the junk
1650 ;; will be treated properly.
1651 (delete-region (point)
1653 (skip-chars-forward " \t\n")
1661 (if (looking-at "0")
1665 (narrow-to-region (point) (point-max))
1668 (case-fold-search t
)
1669 (quoted-printable-header-field-end
1673 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
1675 (base64-header-field-end
1679 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
1681 (if quoted-printable-header-field-end
1683 (rmail-decode-quoted-printable header-end
(point))
1684 ;; Change "quoted-printable" to "8bit",
1685 ;; to reflect the decoding we just did.
1686 (goto-char quoted-printable-header-field-end
)
1687 (delete-region (point) (search-backward ":"))
1689 (if base64-header-field-end
1692 (base64-decode-region (1+ header-end
)
1694 ;; Change "base64" to "8bit", to reflect the
1695 ;; decoding we just did.
1696 (goto-char (1+ header-end
))
1697 (while (search-forward "\r\n" (point-max) t
)
1698 (replace-match "\n"))
1699 (goto-char base64-header-field-end
)
1700 (delete-region (point) (search-backward ":"))
1703 (setq last-coding-system-used nil
)
1704 (or rmail-enable-mime
1705 (not rmail-enable-multibyte
)
1707 (if (and rmail-decode-mime-charset
1710 (search-forward "\n\n" nil t
)
1711 (let ((case-fold-search t
))
1713 rmail-mime-charset-pattern
1715 (intern (downcase (match-string 1))))))
1716 (rmail-decode-region start
(point) mime-charset
)))))
1717 ;; Add an X-Coding-System: header if we don't have one.
1721 (if (looking-at "0")
1724 (or (save-restriction
1725 (narrow-to-region (point) (point-max))
1727 (goto-char (point-min))
1728 (re-search-forward "^X-Coding-System:" nil t
))
1729 (insert "X-Coding-System: "
1730 (symbol-name last-coding-system-used
)
1732 (narrow-to-region (point) (point-max))
1733 (and (= 0 (% count
10))
1734 (message "Converting to Babyl format...%d" count
)))
1736 ((let ((case-fold-search t
))
1737 (looking-at rmail-mmdf-delim1
))
1738 (let ((case-fold-search t
))
1739 (replace-match "\^L\n0, unseen,,\n*** EOOH ***\n")
1740 (re-search-forward rmail-mmdf-delim2 nil t
)
1741 (replace-match "\^_"))
1744 (narrow-to-region start
(1- (point)))
1745 (goto-char (point-min))
1746 (while (search-forward "\n\^_" nil t
); single char "\^_"
1747 (replace-match "\n^_")))); 2 chars: "^" and "_"
1748 (setq last-coding-system-used nil
)
1749 (or rmail-enable-mime
1750 (not rmail-enable-multibyte
)
1751 (decode-coding-region start
(point) 'undecided
))
1755 (insert "X-Coding-System: "
1756 (symbol-name last-coding-system-used
)
1758 (narrow-to-region (point) (point-max))
1759 (setq count
(1+ count
))
1760 (and (= 0 (% count
10))
1761 (message "Converting to Babyl format...%d" count
)))
1763 ((looking-at "^From ")
1764 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
1765 (rmail-nuke-pinhead-header)
1766 ;; If this message has a Content-Length field,
1767 ;; skip to the end of the contents.
1768 (let* ((header-end (save-excursion
1769 (and (re-search-forward "\n\n" nil t
)
1771 (case-fold-search t
)
1772 (quoted-printable-header-field-end
1775 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
1777 (base64-header-field-end
1780 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
1783 ;; Get the numeric value from the Content-Length field.
1785 ;; Back up to end of prev line,
1786 ;; in case the Content-Length field comes first.
1788 (and (search-forward "\ncontent-length: "
1791 (eol (progn (end-of-line) (point))))
1792 (string-to-int (buffer-substring beg eol
)))))))
1794 (if (and (natnump size
)
1795 (<= (+ header-end size
) (point-max))
1796 ;; Make sure this would put us at a position
1797 ;; that we could continue from.
1799 (goto-char (+ header-end size
))
1800 (skip-chars-forward "\n")
1802 (and (looking-at "BABYL OPTIONS:")
1803 (search-forward "\n\^_" nil t
))
1804 (and (looking-at "\^L")
1805 (search-forward "\n\^_" nil t
))
1806 (let ((case-fold-search t
))
1807 (looking-at rmail-mmdf-delim1
))
1808 (looking-at "From "))))
1809 (goto-char (+ header-end size
))
1810 (message "Ignoring invalid Content-Length field")
1812 (if (let ((case-fold-search nil
))
1814 (concat "^[\^_]?\\("
1815 rmail-unix-mail-delimiter
1817 rmail-mmdf-delim1
"\\|"
1818 "^BABYL OPTIONS:\\|"
1819 "\^L\n[01],\\)") nil t
))
1820 (goto-char (match-beginning 1))
1821 (goto-char (point-max)))
1822 (setq count
(1+ count
))
1823 (if quoted-printable-header-field-end
1825 (rmail-decode-quoted-printable header-end
(point))
1826 ;; Change "quoted-printable" to "8bit",
1827 ;; to reflect the decoding we just did.
1828 (goto-char quoted-printable-header-field-end
)
1829 (delete-region (point) (search-backward ":"))
1831 (if base64-header-field-end
1834 (base64-decode-region header-end
(point))
1835 ;; Change "base64" to "8bit", to reflect the
1836 ;; decoding we just did.
1837 (goto-char base64-header-field-end
)
1838 (delete-region (point) (search-backward ":"))
1844 (narrow-to-region start
(point))
1845 (goto-char (point-min))
1846 (while (search-forward "\n\^_" nil t
); single char
1847 (replace-match "\n^_")))); 2 chars: "^" and "_"
1849 (setq last-coding-system-used nil
)
1850 (or rmail-enable-mime
1851 (not rmail-enable-multibyte
)
1853 (if (and rmail-decode-mime-charset
1856 (search-forward "\n\n" nil t
)
1857 (let ((case-fold-search t
))
1859 rmail-mime-charset-pattern
1861 (intern (downcase (match-string 1))))))
1862 (rmail-decode-region start
(point) mime-charset
)))
1866 (insert "X-Coding-System: "
1867 (symbol-name last-coding-system-used
)
1869 (narrow-to-region (point) (point-max))
1870 (and (= 0 (% count
10))
1871 (message "Converting to Babyl format...%d" count
)))
1873 ;; This kludge is because some versions of sendmail.el
1874 ;; insert an extra newline at the beginning that shouldn't
1875 ;; be there. sendmail.el has been fixed, but old versions
1876 ;; may still be in use. -- rms, 7 May 1993.
1877 ((eolp) (delete-char 1))
1878 (t (error "Cannot convert to babyl format")))))
1881 (defun rmail-hex-char-to-integer (character)
1882 "Return CHARACTER's value interpreted as a hex digit."
1883 (if (and (>= character ?
0) (<= character ?
9))
1885 (let ((ch (logior character
32)))
1886 (if (and (>= ch ?a
) (<= ch ?f
))
1888 (error "Invalid hex digit `%c'" ch
)))))
1890 (defun rmail-hex-string-to-integer (hex-string)
1891 "Return decimal integer for HEX-STRING."
1894 (while (< index
(length hex-string
))
1895 (setq hex-num
(+ (* hex-num
16)
1896 (rmail-hex-char-to-integer (aref hex-string index
))))
1897 (setq index
(1+ index
)))
1900 (defun rmail-decode-quoted-printable (from to
)
1901 "Decode Quoted-Printable in the region between FROM and TO."
1905 (setq to
(copy-marker to
)))
1906 (while (search-forward "=" to t
)
1907 (cond ((eq (following-char) ?
\n)
1910 ((looking-at "[0-9A-F][0-9A-F]")
1911 (let ((byte (rmail-hex-string-to-integer
1912 (buffer-substring (point) (+ 2 (point))))))
1913 (delete-region (1- (point)) (+ 2 (point)))
1918 (message "Malformed MIME quoted-printable message")))))
1920 ;; Delete the "From ..." line, creating various other headers with
1921 ;; information from it if they don't already exist. Now puts the
1922 ;; original line into a mail-from: header line for debugging and for
1923 ;; use by the rmail-output function.
1924 (defun rmail-nuke-pinhead-header ()
1927 (let ((start (point))
1930 (search-forward "\n\n")
1932 (goto-char (point-max))
1936 (narrow-to-region start end
)
1937 (let ((case-fold-search t
))
1939 (setq has-from
(search-forward "\nFrom:" nil t
))
1941 (setq has-date
(and (search-forward "\nDate:" nil t
) (point)))
1943 (let ((case-fold-search nil
))
1944 (if (re-search-forward (concat "^" rmail-unix-mail-delimiter
) nil t
)
1948 ;; Keep and reformat the date if we don't
1949 ;; have a Date: field.
1953 "Date: \\2, \\4 \\3 \\9 \\5 "
1955 ;; The timezone could be matched by group 7 or group 10.
1956 ;; If neither of them matched, assume EST, since only
1957 ;; Easterners would be so sloppy.
1958 ;; It's a shame the substitution can't use "\\10".
1960 ((/= (match-beginning 7) (match-end 7)) "\\7")
1961 ((/= (match-beginning 10) (match-end 10))
1962 (buffer-substring (match-beginning 10)
1966 ;; Keep and reformat the sender if we don't
1967 ;; have a From: field.
1973 ;;;; *** Rmail Message Formatting and Header Manipulation ***
1975 (defun rmail-reformat-message (beg end
)
1978 (if (/= (following-char) ?
0)
1979 (error "Bad format in RMAIL file"))
1980 (let ((inhibit-read-only t
)
1981 (delta (- (buffer-size) end
)))
1985 (let ((case-fold-search t
))
1986 (while (looking-at "Summary-line:\\|Mail-From:")
1988 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*\n")
1989 (delete-region (point)
1990 (progn (forward-line 1) (point))))
1991 (let ((str (buffer-substring (point)
1992 (save-excursion (search-forward "\n\n" end
'move
)
1994 (insert str
"*** EOOH ***\n")
1995 (narrow-to-region (point) (- (buffer-size) delta
)))
1996 (goto-char (point-min))
1997 (if rmail-message-filter
(funcall rmail-message-filter
))
1998 (if (or rmail-displayed-headers rmail-ignored-headers
)
1999 (rmail-clear-headers))))
2001 (defun rmail-clear-headers (&optional ignored-headers
)
2002 "Delete all header fields that Rmail should not show.
2003 If the optional argument IGNORED-HEADERS is non-nil,
2004 delete all header fields whose names match that regexp.
2005 Otherwise, if `rmail-displayed-headers' is non-nil,
2006 delete all header fields *except* those whose names match that regexp.
2007 Otherwise, delete all header fields whose names match `rmail-ignored-headers'."
2008 (when (search-forward "\n\n" nil t
)
2010 (let ((case-fold-search t
)
2011 (buffer-read-only nil
))
2012 (if (and rmail-displayed-headers
(null ignored-headers
))
2014 (narrow-to-region (point-min) (point))
2016 (goto-char (point-min))
2017 (while (and (not (eobp))
2019 (if (re-search-forward "\n[^ \t]" nil t
)
2020 (setq lim
(match-beginning 0)
2022 (setq lim nil next
(point-max)))))
2024 (re-search-forward rmail-displayed-headers lim t
))
2026 (delete-region (point) next
))))
2027 (goto-char (point-min)))
2028 (or ignored-headers
(setq ignored-headers rmail-ignored-headers
))
2030 (narrow-to-region (point-min) (point))
2031 (while (and ignored-headers
2033 (goto-char (point-min))
2034 (re-search-forward ignored-headers nil t
)))
2036 (delete-region (point)
2037 (if (re-search-forward "\n[^ \t]" nil t
)
2041 (defun rmail-msg-is-pruned ()
2042 (rmail-maybe-set-message-counters)
2044 (narrow-to-region (rmail-msgbeg rmail-current-message
) (point-max))
2046 (goto-char (point-min))
2048 (= (following-char) ?
1))))
2050 (defun rmail-msg-restore-non-pruned-header ()
2051 (let ((old-point (point))
2054 (inhibit-read-only t
))
2056 (narrow-to-region (rmail-msgbeg rmail-current-message
) (point-max))
2057 (goto-char (point-min))
2062 ;; Insert new EOOH line at the proper place.
2064 (let ((case-fold-search t
))
2065 (while (looking-at "Summary-Line:\\|Mail-From:")
2067 (insert "*** EOOH ***\n")
2068 (setq new-start
(point))
2069 ;; Delete the old reformatted header.
2071 (search-forward "\n*** EOOH ***\n")
2073 (let ((start (point)))
2074 (search-forward "\n\n")
2075 (if (and (<= start old-point
)
2076 (<= old-point
(point)))
2077 (setq new-point new-start
))
2078 (delete-region start
(point)))
2079 ;; Narrow to after the new EOOH line.
2080 (narrow-to-region new-start
(point-max)))
2082 (goto-char new-point
))))
2084 (defun rmail-msg-prune-header ()
2086 (= (point) (point-min))))
2088 (narrow-to-region (rmail-msgbeg rmail-current-message
) (point-max))
2089 (rmail-reformat-message (point-min) (point-max)))
2091 (goto-char (point-min)))))
2093 (defun rmail-toggle-header (&optional arg
)
2094 "Show original message header if pruned header currently shown, or vice versa.
2095 With argument ARG, show the message header pruned if ARG is greater than zero;
2096 otherwise, show it in full."
2098 (let* ((pruned (with-current-buffer rmail-buffer
2099 (rmail-msg-is-pruned)))
2101 (> (prefix-numeric-value arg
) 0)
2103 (if (eq pruned prune
)
2105 (set-buffer rmail-buffer
)
2106 (rmail-maybe-set-message-counters)
2107 (if rmail-enable-mime
2108 (let ((buffer-read-only nil
))
2110 (rmail-msg-restore-non-pruned-header)
2111 (rmail-msg-prune-header))
2112 (funcall rmail-show-mime-function
))
2113 (let* ((buffer-read-only nil
)
2114 (window (get-buffer-window (current-buffer)))
2115 (at-point-min (= (point) (point-min)))
2116 (all-headers-visible (= (window-start window
) (point-min)))
2119 (and (not (search-backward "\n\n" nil t
))
2122 (re-search-backward "^[-A-Za-z0-9]+:" nil t
))
2125 (rmail-count-screen-lines (window-start window
) (point))))
2127 (rmail-msg-restore-non-pruned-header)
2128 (rmail-msg-prune-header))
2130 (goto-char (point-min)))
2132 (goto-char (point-min))
2133 (search-forward "\n\n")
2134 (or (re-search-backward
2135 (concat "^" (regexp-quote on-header
)) nil t
)
2136 (goto-char (point-min))))
2138 (save-selected-window
2139 (select-window window
)
2140 (recenter old-screen-line
)
2141 (if (and all-headers-visible
2142 (not (= (window-start) (point-min))))
2143 (recenter (- (window-height) 2))))))))
2144 (rmail-highlight-headers))))
2146 (defun rmail-narrow-to-non-pruned-header ()
2147 "Narrow to the whole (original) header of the current message."
2149 (narrow-to-region (rmail-msgbeg rmail-current-message
) (point-max))
2150 (goto-char (point-min))
2152 (if (= (following-char) ?
1)
2155 (setq start
(point))
2156 (search-forward "*** EOOH ***\n")
2157 (setq end
(match-beginning 0)))
2159 (setq start
(point))
2160 (search-forward "\n\n")
2161 (setq end
(1- (point))))
2162 (narrow-to-region start end
)
2165 ;; Lifted from repos-count-screen-lines.
2166 ;; Return number of screen lines between START and END.
2167 (defun rmail-count-screen-lines (start end
)
2170 (narrow-to-region start end
)
2171 (goto-char (point-min))
2172 (vertical-motion (- (point-max) (point-min))))))
2174 ;;;; *** Rmail Attributes and Keywords ***
2176 ;; Make a string describing current message's attributes and keywords
2177 ;; and set it up as the name of a minor mode
2178 ;; so it will appear in the mode line.
2179 (defun rmail-display-labels ()
2180 (let ((blurb "") (beg (point-min-marker)) (end (point-max-marker)))
2185 (goto-char (rmail-msgbeg rmail-current-message
))
2187 (if (looking-at "[01],")
2189 (narrow-to-region (point) (progn (end-of-line) (point)))
2190 ;; Truly valid BABYL format requires a space before each
2191 ;; attribute or keyword name. Put them in if missing.
2192 (let (buffer-read-only)
2193 (goto-char (point-min))
2194 (while (search-forward "," nil t
)
2195 (or (looking-at "[ ,]") (eobp)
2197 (goto-char (point-max))
2198 (if (search-backward ",," nil
'move
)
2200 (if (> (point) (1+ (point-min)))
2201 (setq blurb
(buffer-substring (+ 1 (point-min)) (point))))
2202 (if (> (- (point-max) (point)) 2)
2206 (buffer-substring (+ (point) 3)
2207 (1- (point-max)))))))))))
2208 ;; Note: we don't use save-restriction because that does not work right
2209 ;; if changes are made outside the saved restriction
2210 ;; before that restriction is restored.
2211 (narrow-to-region beg end
)
2212 (set-marker beg nil
)
2213 (set-marker end nil
)))
2214 (while (string-match " +," blurb
)
2215 (setq blurb
(concat (substring blurb
0 (match-beginning 0)) ","
2216 (substring blurb
(match-end 0)))))
2217 (while (string-match ", +" blurb
)
2218 (setq blurb
(concat (substring blurb
0 (match-beginning 0)) ","
2219 (substring blurb
(match-end 0)))))
2220 (setq mode-line-process
2222 rmail-current-message rmail-total-messages blurb
))
2223 ;; If rmail-enable-mime is non-nil, we may have to update
2224 ;; `mode-line-process' of rmail-view-buffer too.
2225 (if (and rmail-enable-mime
2226 (not (eq (current-buffer) rmail-view-buffer
))
2227 (buffer-live-p rmail-view-buffer
))
2228 (let ((mlp mode-line-process
))
2229 (with-current-buffer rmail-view-buffer
2230 (setq mode-line-process mlp
))))))
2232 ;; Turn an attribute of a message on or off according to STATE.
2233 ;; ATTR is the name of the attribute, as a string.
2234 ;; MSGNUM is message number to change; nil means current message.
2235 (defun rmail-set-attribute (attr state
&optional msgnum
)
2236 (set-buffer rmail-buffer
)
2237 (let ((omax (point-max-marker))
2238 (omin (point-min-marker))
2239 (buffer-read-only nil
))
2240 (or msgnum
(setq msgnum rmail-current-message
))
2245 (goto-char (+ 3 (rmail-msgbeg msgnum
)))
2248 (null (search-backward (concat ", " attr
",")
2249 (prog1 (point) (end-of-line)) t
)))))
2250 (or (eq curstate
(not (not state
)))
2252 (delete-region (point) (1- (match-end 0)))
2255 (insert " " attr
","))))
2256 (if (string= attr
"deleted")
2257 (rmail-set-message-deleted-p msgnum state
)))
2258 ;; Note: we don't use save-restriction because that does not work right
2259 ;; if changes are made outside the saved restriction
2260 ;; before that restriction is restored.
2261 (narrow-to-region omin omax
)
2262 (set-marker omin nil
)
2263 (set-marker omax nil
)
2264 (if (= msgnum rmail-current-message
)
2265 (rmail-display-labels))))))
2267 ;; Return t if the attributes/keywords line of msg number MSG
2268 ;; contains a match for the regexp LABELS.
2269 (defun rmail-message-labels-p (msg labels
)
2273 (goto-char (rmail-msgbeg msg
))
2275 (re-search-backward labels
(prog1 (point) (end-of-line)) t
))))
2277 ;;;; *** Rmail Message Selection And Support ***
2279 (defun rmail-msgend (n)
2280 (marker-position (aref rmail-message-vector
(1+ n
))))
2282 (defun rmail-msgbeg (n)
2283 (marker-position (aref rmail-message-vector n
)))
2285 (defun rmail-widen-to-current-msgbeg (function)
2286 "Call FUNCTION with point at start of internal data of current message.
2287 Assumes that bounds were previously narrowed to display the message in Rmail.
2288 The bounds are widened enough to move point where desired, then narrowed
2291 FUNCTION may not change the visible text of the message, but it may
2292 change the invisible header text."
2296 (narrow-to-region (rmail-msgbeg rmail-current-message
)
2298 (goto-char (point-min))
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 (rmail-msgbeg rmail-current-message
)
2304 (rmail-msgend rmail-current-message
)))))
2306 (defun rmail-forget-messages ()
2308 (if (vectorp rmail-message-vector
)
2310 (v rmail-message-vector
)
2313 (move-marker (aref v i
) nil
)
2315 (setq rmail-message-vector nil
)
2316 (setq rmail-msgref-vector nil
)
2317 (setq rmail-deleted-vector nil
)))
2319 (defun rmail-maybe-set-message-counters ()
2320 (if (not (and rmail-deleted-vector
2321 rmail-message-vector
2322 rmail-current-message
2323 rmail-total-messages
))
2324 (rmail-set-message-counters)))
2326 (defun rmail-count-new-messages (&optional nomsg
)
2327 (let* ((case-fold-search nil
)
2331 (or nomsg
(message "Counting new messages..."))
2332 (goto-char (point-max))
2333 ;; Put at the end of messages-head
2334 ;; the entry for message N+1, which marks
2335 ;; the end of message N. (N = number of messages).
2336 (search-backward "\n\^_")
2338 (setq messages-head
(list (point-marker)))
2339 (rmail-set-message-counters-counter (point-min))
2340 (setq rmail-current-message
(1+ rmail-total-messages
))
2341 (setq rmail-total-messages
2342 (+ rmail-total-messages total-messages
))
2343 (setq rmail-message-vector
2344 (vconcat rmail-message-vector
(cdr messages-head
)))
2345 (aset rmail-message-vector
2346 rmail-current-message
(car messages-head
))
2347 (setq rmail-deleted-vector
2348 (concat rmail-deleted-vector deleted-head
))
2349 (setq rmail-summary-vector
2350 (vconcat rmail-summary-vector
(make-vector total-messages nil
)))
2351 (setq rmail-msgref-vector
2352 (vconcat rmail-msgref-vector
(make-vector total-messages nil
)))
2353 ;; Fill in the new elements of rmail-msgref-vector.
2354 (let ((i (1+ (- rmail-total-messages total-messages
))))
2355 (while (<= i rmail-total-messages
)
2356 (aset rmail-msgref-vector i
(list i
))
2358 (goto-char (point-min))
2359 (or nomsg
(message "Counting new messages...done (%d)" total-messages
))))
2361 (defun rmail-set-message-counters ()
2362 (rmail-forget-messages)
2366 (let* ((point-save (point))
2368 (messages-after-point)
2369 (case-fold-search nil
)
2372 (message "Counting messages...")
2373 (goto-char (point-max))
2374 ;; Put at the end of messages-head
2375 ;; the entry for message N+1, which marks
2376 ;; the end of message N. (N = number of messages).
2377 (search-backward "\n\^_" nil t
)
2378 (if (/= (point) (point-max)) (forward-char 1))
2379 (setq messages-head
(list (point-marker)))
2380 (rmail-set-message-counters-counter (min (point) point-save
))
2381 (setq messages-after-point total-messages
)
2382 (rmail-set-message-counters-counter)
2383 (setq rmail-total-messages total-messages
)
2384 (setq rmail-current-message
2386 (max 1 (- total-messages messages-after-point
))))
2387 (setq rmail-message-vector
2388 (apply 'vector
(cons (point-min-marker) messages-head
))
2389 rmail-deleted-vector
(concat "0" deleted-head
)
2390 rmail-summary-vector
(make-vector rmail-total-messages nil
)
2391 rmail-msgref-vector
(make-vector (1+ rmail-total-messages
) nil
))
2393 (while (<= i rmail-total-messages
)
2394 (aset rmail-msgref-vector i
(list i
))
2396 (message "Counting messages...done")))))
2398 (defun rmail-set-message-counters-counter (&optional stop
)
2399 (let ((start (point))
2401 (while (search-backward "\n\^_\^L" stop t
)
2402 ;; Detect messages that have been added with DOS line endings and
2403 ;; convert the line endings for such messages.
2405 (if (looking-at "\n\^_\^L\r\n")
2406 (let ((buffer-read-only nil
)
2408 (message "Counting messages...(converting line endings)")
2411 (while (search-backward "\r\n" next t
)
2415 (setq messages-head
(cons (point-marker) messages-head
))
2418 (cons (if (search-backward ", deleted,"
2424 (if (zerop (%
(setq total-messages
(1+ total-messages
)) 20))
2425 (message "Counting messages...%d" total-messages
)))))
2427 (defun rmail-beginning-of-message ()
2428 "Show current message starting from the beginning."
2430 (rmail-show-message rmail-current-message
))
2432 (defun rmail-show-message (&optional n no-summary
)
2433 "Show message number N (prefix argument), counting from start of file.
2434 If summary buffer is currently displayed, update current message there also."
2436 (or (eq major-mode
'rmail-mode
)
2437 (switch-to-buffer rmail-buffer
))
2438 (rmail-maybe-set-message-counters)
2440 (if (zerop rmail-total-messages
)
2441 (progn (narrow-to-region (point-min) (1- (point-max)))
2442 (goto-char (point-min))
2443 (setq mode-line-process nil
))
2444 (let (blurb coding-system
)
2446 (setq n rmail-current-message
)
2449 rmail-current-message
1
2450 blurb
"No previous message"))
2451 ((> n rmail-total-messages
)
2452 (setq n rmail-total-messages
2453 rmail-current-message rmail-total-messages
2454 blurb
"No following message"))
2456 (setq rmail-current-message n
))))
2457 (let ((beg (rmail-msgbeg n
)))
2461 (let ((end (rmail-msgend n
)))
2463 (if (prog1 (= (following-char) ?
0)
2465 ;; If there's a Summary-line in the (otherwise empty)
2466 ;; header, we didn't yet get past the EOOH line.
2467 (if (looking-at "^\\*\\*\\* EOOH \\*\\*\\*\n")
2469 (narrow-to-region (point) end
))
2471 (search-forward "\n*** EOOH ***\n" end t
))
2472 (narrow-to-region beg
(point))
2473 (goto-char (point-min))
2474 (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t
)
2475 (let ((coding-system (intern (match-string 1))))
2478 (check-coding-system coding-system
)
2479 (setq buffer-file-coding-system coding-system
))
2481 (setq buffer-file-coding-system nil
))))
2482 (setq buffer-file-coding-system nil
)))))
2483 ;; Clear the "unseen" attribute when we show a message.
2484 (rmail-set-attribute "unseen" nil
)
2485 (let ((end (rmail-msgend n
)))
2486 ;; Reformat the header, or else find the reformatted header.
2487 (if (= (following-char) ?
0)
2488 (rmail-reformat-message beg end
)
2489 (search-forward "\n*** EOOH ***\n" end t
)
2490 (narrow-to-region (point) end
)))
2491 (goto-char (point-min))
2493 (function (lambda (window)
2494 (if (eq (window-buffer window
) (current-buffer))
2495 (set-window-point window
(point)))))
2497 (rmail-display-labels)
2498 (if (eq rmail-enable-mime t
)
2499 (funcall rmail-show-mime-function
)
2500 (setq rmail-view-buffer rmail-buffer
)
2502 (rmail-highlight-headers)
2503 (if transient-mark-mode
(deactivate-mark))
2504 (run-hooks 'rmail-show-message-hook
)
2505 ;; If there is a summary buffer, try to move to this message
2506 ;; in that buffer. But don't complain if this message
2507 ;; is not mentioned in the summary.
2508 ;; Don't do this at all if we were called on behalf
2509 ;; of cursor motion in the summary buffer.
2510 (and (rmail-summary-exists) (not no-summary
)
2511 (let ((curr-msg rmail-current-message
))
2512 (rmail-select-summary
2513 (rmail-summary-goto-msg curr-msg t t
))))
2514 (with-current-buffer rmail-buffer
2517 (message blurb
))))))
2519 (defun rmail-redecode-body (coding)
2520 "Decode the body of the current message using coding system CODING.
2521 This is useful with mail messages that have malformed or missing
2524 This function assumes that the current message is already decoded
2525 and displayed in the RMAIL buffer, but the coding system used to
2526 decode it was incorrect. It then encodes the message back to its
2527 original form, and decodes it again, using the coding system CODING.
2529 Note that if Emacs erroneously auto-detected one of the iso-2022
2530 encodings in the message, this function might fail because the escape
2531 sequences that switch between character sets and also single-shift and
2532 locking-shift codes are impossible to recover. This function is meant
2533 to be used to fix messages encoded with 8-bit encodings, such as
2534 iso-8859, koi8-r, etc."
2535 (interactive "zCoding system for re-decoding this message: ")
2536 (when (not rmail-enable-mime
)
2537 (or (eq major-mode
'rmail-mode
)
2538 (switch-to-buffer rmail-buffer
))
2540 (let ((pruned (rmail-msg-is-pruned)))
2542 (let ((msgbeg (rmail-msgbeg rmail-current-message
))
2543 (msgend (rmail-msgend rmail-current-message
))
2545 ;; We need the message headers pruned (we later restore
2546 ;; the pruned stat to what it was, see the end of
2547 ;; unwind-protect form).
2549 (rmail-toggle-header 1))
2550 (narrow-to-region msgbeg msgend
)
2551 (goto-char (point-min))
2552 (when (search-forward "\n*** EOOH ***\n" (point-max) t
)
2553 (narrow-to-region msgbeg
(point)))
2554 (goto-char (point-min))
2555 (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t
)
2556 (let ((old-coding (intern (match-string 1)))
2557 (buffer-read-only nil
))
2558 (check-coding-system old-coding
)
2559 ;; Make sure the new coding system uses the same EOL
2560 ;; conversion, to prevent ^M characters from popping
2561 ;; up all over the place.
2563 (coding-system-change-eol-conversion
2565 (coding-system-eol-type old-coding
)))
2566 (setq x-coding-header
(point-marker))
2567 (narrow-to-region msgbeg msgend
)
2568 (encode-coding-region (point) msgend old-coding
)
2569 (decode-coding-region (point) msgend coding
)
2570 (setq last-coding-system-used coding
)
2571 ;; Rewrite the coding-system header according
2573 (goto-char x-coding-header
)
2574 (delete-region (point)
2578 (insert "X-Coding-System: "
2579 (symbol-name last-coding-system-used
))
2580 (set-marker x-coding-header nil
)
2581 (rmail-show-message))
2582 (error "No X-Coding-System header found")))
2584 (rmail-toggle-header 0)))))))
2586 ;; Find all occurrences of certain fields, and highlight them.
2587 (defun rmail-highlight-headers ()
2588 ;; Do this only if the system supports faces.
2589 (if (and (fboundp 'internal-find-face
)
2590 rmail-highlighted-headers
)
2592 (search-forward "\n\n" nil
'move
)
2594 (narrow-to-region (point-min) (point))
2595 (let ((case-fold-search t
)
2596 (inhibit-read-only t
)
2597 ;; Highlight with boldface if that is available.
2598 ;; Otherwise use the `highlight' face.
2599 (face (or rmail-highlight-face
2600 (if (face-differs-from-default-p 'bold
)
2602 ;; List of overlays to reuse.
2603 (overlays rmail-overlay-list
))
2604 (goto-char (point-min))
2605 (while (re-search-forward rmail-highlighted-headers nil t
)
2606 (skip-chars-forward " \t")
2609 (while (progn (forward-line 1)
2610 (looking-at "[ \t]")))
2611 ;; Back up over newline, then trailing spaces or tabs
2613 (while (member (preceding-char) '(? ?
\t))
2616 ;; Reuse an overlay we already have.
2618 (setq overlay
(car overlays
)
2619 overlays
(cdr overlays
))
2620 (overlay-put overlay
'face face
)
2621 (move-overlay overlay beg
(point)))
2622 ;; Make a new overlay and add it to
2623 ;; rmail-overlay-list.
2624 (setq overlay
(make-overlay beg
(point)))
2625 (overlay-put overlay
'face face
)
2626 (setq rmail-overlay-list
2627 (cons overlay rmail-overlay-list
))))))))))
2629 (defun rmail-auto-file ()
2630 "Automatically move a message into a sub-folder based on criteria.
2631 Called when a new message is displayed."
2632 (if (or (rmail-message-labels-p rmail-current-message
"filed")
2633 (not (string= (buffer-file-name)
2634 (expand-file-name rmail-file-name
))))
2635 ;; Do nothing if it's already been filed.
2637 ;; Find out some basics (common fields)
2638 (let ((from (mail-fetch-field "from"))
2639 (subj (mail-fetch-field "subject"))
2640 (to (concat (mail-fetch-field "to") "," (mail-fetch-field "cc")))
2641 (d rmail-automatic-folder-directives
)
2642 (directive-loop nil
)
2645 (setq folder
(car (car d
))
2646 directive-loop
(cdr (car d
)))
2647 (while (and (car directive-loop
)
2649 ((string= (car directive-loop
) "from") from
)
2650 ((string= (car directive-loop
) "to") to
)
2651 ((string= (car directive-loop
) "subject") subj
)
2652 (t (mail-fetch-field (car directive-loop
))))))
2653 (and f
(string-match (car (cdr directive-loop
)) f
))))
2654 (setq directive-loop
(cdr (cdr directive-loop
))))
2655 ;; If there are no directives left, then it was a complete match.
2656 (if (null directive-loop
)
2658 (rmail-delete-forward)
2659 (if (string= "/dev/null" folder
)
2660 (rmail-delete-message)
2661 (rmail-output-to-rmail-file folder
1 t
)
2663 (setq d
(cdr d
))))))
2665 (defun rmail-next-message (n)
2666 "Show following message whether deleted or not.
2667 With prefix arg N, moves forward N messages, or backward if N is negative."
2669 (set-buffer rmail-buffer
)
2670 (rmail-maybe-set-message-counters)
2671 (rmail-show-message (+ rmail-current-message n
)))
2673 (defun rmail-previous-message (n)
2674 "Show previous message whether deleted or not.
2675 With prefix arg N, moves backward N messages, or forward if N is negative."
2677 (rmail-next-message (- n
)))
2679 (defun rmail-next-undeleted-message (n)
2680 "Show following non-deleted message.
2681 With prefix arg N, moves forward N non-deleted messages,
2682 or backward if N is negative.
2684 Returns t if a new message is being shown, nil otherwise."
2686 (set-buffer rmail-buffer
)
2687 (rmail-maybe-set-message-counters)
2688 (let ((lastwin rmail-current-message
)
2689 (current rmail-current-message
))
2690 (while (and (> n
0) (< current rmail-total-messages
))
2691 (setq current
(1+ current
))
2692 (if (not (rmail-message-deleted-p current
))
2693 (setq lastwin current n
(1- n
))))
2694 (while (and (< n
0) (> current
1))
2695 (setq current
(1- current
))
2696 (if (not (rmail-message-deleted-p current
))
2697 (setq lastwin current n
(1+ n
))))
2698 (if (/= lastwin rmail-current-message
)
2699 (progn (rmail-show-message lastwin
)
2702 (message "No previous nondeleted message"))
2704 (message "No following nondeleted message"))
2707 (defun rmail-previous-undeleted-message (n)
2708 "Show previous non-deleted message.
2709 With prefix argument N, moves backward N non-deleted messages,
2710 or forward if N is negative."
2712 (rmail-next-undeleted-message (- n
)))
2714 (defun rmail-first-message ()
2715 "Show first message in file."
2717 (rmail-maybe-set-message-counters)
2718 (rmail-show-message 1))
2720 (defun rmail-last-message ()
2721 "Show last message in file."
2723 (rmail-maybe-set-message-counters)
2724 (rmail-show-message rmail-total-messages
))
2726 (defun rmail-what-message ()
2727 (let ((where (point))
2729 (high rmail-total-messages
)
2730 (mid (/ rmail-total-messages
2)))
2731 (while (> (- high low
) 1)
2732 (if (>= where
(rmail-msgbeg mid
))
2735 (setq mid
(+ low
(/ (- high low
) 2))))
2736 (if (>= where
(rmail-msgbeg high
)) high low
)))
2738 (defun rmail-message-recipients-p (msg recipients
&optional primary-only
)
2740 (goto-char (rmail-msgbeg msg
))
2741 (search-forward "\n*** EOOH ***\n")
2742 (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
2743 (or (string-match recipients
(or (mail-fetch-field "To") ""))
2744 (string-match recipients
(or (mail-fetch-field "From") ""))
2745 (if (not primary-only
)
2746 (string-match recipients
(or (mail-fetch-field "Cc") ""))))))
2748 (defun rmail-message-regexp-p (n regexp
)
2749 "Return t, if for message number N, regexp REGEXP matches in the header."
2750 (let ((beg (rmail-msgbeg n
))
2751 (end (rmail-msgend n
)))
2756 (if (prog1 (= (following-char) ?
0)
2758 ;; If there's a Summary-line in the (otherwise empty)
2759 ;; header, we didn't yet get past the EOOH line.
2760 (when (looking-at "^\\*\\*\\* EOOH \\*\\*\\*\n")
2763 (narrow-to-region (point) end
))
2768 (search-forward "\n*** EOOH ***\n" end t
)
2769 (setq end
(1+ (match-beginning 0)))))
2771 (if rmail-enable-mime
2772 (funcall rmail-search-mime-header-function n regexp end
)
2773 (re-search-forward regexp end t
)))))
2775 (defun rmail-search-message (msg regexp
)
2776 "Return non-nil, if for message number MSG, regexp REGEXP matches."
2777 (goto-char (rmail-msgbeg msg
))
2778 (if rmail-enable-mime
2779 (funcall rmail-search-mime-message-function msg regexp
)
2780 (re-search-forward regexp
(rmail-msgend msg
) t
)))
2782 (defvar rmail-search-last-regexp nil
)
2783 (defun rmail-search (regexp &optional n
)
2784 "Show message containing next match for REGEXP (but not the current msg).
2785 Prefix argument gives repeat count; negative argument means search
2786 backwards (through earlier messages).
2787 Interactively, empty argument means use same regexp used last time."
2789 (let* ((reversep (< (prefix-numeric-value current-prefix-arg
) 0))
2791 (concat (if reversep
"Reverse " "") "Rmail search (regexp): "))
2793 (if rmail-search-last-regexp
2794 (setq prompt
(concat prompt
2796 rmail-search-last-regexp
2798 (setq regexp
(read-string prompt
))
2799 (cond ((not (equal regexp
""))
2800 (setq rmail-search-last-regexp regexp
))
2801 ((not rmail-search-last-regexp
)
2802 (error "No previous Rmail search string")))
2803 (list rmail-search-last-regexp
2804 (prefix-numeric-value current-prefix-arg
))))
2806 (message "%sRmail search for %s..."
2807 (if (< n
0) "Reverse " "")
2809 (set-buffer rmail-buffer
)
2810 (rmail-maybe-set-message-counters)
2811 (let ((omin (point-min))
2816 (msg rmail-current-message
))
2821 ;; Check messages one by one, advancing message number up or down
2822 ;; but searching forward through each message.
2824 (while (and (null win
) (> msg
1))
2826 win
(rmail-search-message msg regexp
)))
2827 (while (and (null win
) (< msg rmail-total-messages
))
2829 win
(rmail-search-message msg regexp
))))
2830 (setq n
(+ n
(if reversep
1 -
1)))))
2833 (rmail-show-message msg
)
2834 ;; Search forward (if this is a normal search) or backward
2835 ;; (if this is a reverse search) through this message to
2836 ;; position point. This search may fail because REGEXP
2837 ;; was found in the hidden portion of this message. In
2838 ;; that case, move point to the beginning of visible
2842 (goto-char (point-max))
2843 (re-search-backward regexp nil
'move
))
2844 (goto-char (point-min))
2845 (re-search-forward regexp nil t
))
2846 (message "%sRmail search for %s...done"
2847 (if reversep
"Reverse " "")
2850 (narrow-to-region omin omax
)
2852 (message "Search failed: %s" regexp
)))))
2854 (defun rmail-search-backwards (regexp &optional n
)
2855 "Show message containing previous match for REGEXP.
2856 Prefix argument gives repeat count; negative argument means search
2857 forward (through later messages).
2858 Interactively, empty argument means use same regexp used last time."
2860 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg
) 0))
2862 (concat (if reversep
"Reverse " "") "Rmail search (regexp): "))
2864 (if rmail-search-last-regexp
2865 (setq prompt
(concat prompt
2867 rmail-search-last-regexp
2869 (setq regexp
(read-string prompt
))
2870 (cond ((not (equal regexp
""))
2871 (setq rmail-search-last-regexp regexp
))
2872 ((not rmail-search-last-regexp
)
2873 (error "No previous Rmail search string")))
2874 (list rmail-search-last-regexp
2875 (prefix-numeric-value current-prefix-arg
))))
2876 (rmail-search regexp
(- (or n
1))))
2878 ;; Show the first message which has the `unseen' attribute.
2879 (defun rmail-first-unseen-message ()
2880 (rmail-maybe-set-message-counters)
2885 (while (and (not found
) (<= current rmail-total-messages
))
2886 (if (rmail-message-labels-p current
", ?\\(unseen\\),")
2887 (setq found current
))
2888 (setq current
(1+ current
))))
2889 ;; Let the caller show the message.
2891 ;; (rmail-show-message found))
2894 (defun rmail-next-same-subject (n)
2895 "Go to the next mail message having the same subject header.
2896 With prefix argument N, do this N times.
2897 If N is negative, go backwards instead."
2899 (let ((subject (mail-fetch-field "Subject"))
2901 (i rmail-current-message
)
2902 (case-fold-search t
)
2903 search-regexp found
)
2904 (if (string-match "\\`[ \t]+" subject
)
2905 (setq subject
(substring subject
(match-end 0))))
2906 (if (string-match "Re:[ \t]*" subject
)
2907 (setq subject
(substring subject
(match-end 0))))
2908 (if (string-match "[ \t]+\\'" subject
)
2909 (setq subject
(substring subject
0 (match-beginning 0))))
2910 (setq search-regexp
(concat "^Subject: *\\(Re:[ \t]*\\)?"
2911 (regexp-quote subject
)
2916 (while (and (/= n
0)
2918 (< i rmail-total-messages
)
2921 (while (and (not done
)
2923 (< i rmail-total-messages
)
2925 (setq i
(if forward
(1+ i
) (1- i
)))
2926 (goto-char (rmail-msgbeg i
))
2927 (search-forward "\n*** EOOH ***\n")
2928 (let ((beg (point)) end
)
2929 (search-forward "\n\n")
2932 (setq done
(re-search-forward search-regexp end t
))))
2933 (if done
(setq found i
)))
2934 (setq n
(if forward
(1- n
) (1+ n
))))))
2936 (rmail-show-message found
)
2937 (error "No %s message with same subject"
2938 (if forward
"following" "previous")))))
2940 (defun rmail-previous-same-subject (n)
2941 "Go to the previous mail message having the same subject header.
2942 With prefix argument N, do this N times.
2943 If N is negative, go forwards instead."
2945 (rmail-next-same-subject (- n
)))
2947 ;;;; *** Rmail Message Deletion Commands ***
2949 (defun rmail-message-deleted-p (n)
2950 (= (aref rmail-deleted-vector n
) ?D
))
2952 (defun rmail-set-message-deleted-p (n state
)
2953 (aset rmail-deleted-vector n
(if state ?D ?\
)))
2955 (defun rmail-delete-message ()
2956 "Delete this message and stay on it."
2958 (rmail-set-attribute "deleted" t
)
2959 (run-hooks 'rmail-delete-message-hook
))
2961 (defun rmail-undelete-previous-message ()
2962 "Back up to deleted message, select it, and undelete it."
2964 (set-buffer rmail-buffer
)
2965 (let ((msg rmail-current-message
))
2966 (while (and (> msg
0)
2967 (not (rmail-message-deleted-p msg
)))
2968 (setq msg
(1- msg
)))
2970 (error "No previous deleted message")
2971 (if (/= msg rmail-current-message
)
2972 (rmail-show-message msg
))
2973 (rmail-set-attribute "deleted" nil
)
2974 (if (rmail-summary-exists)
2976 (set-buffer rmail-summary-buffer
)
2977 (rmail-summary-mark-undeleted msg
)))
2978 (rmail-maybe-display-summary))))
2980 (defun rmail-delete-forward (&optional backward
)
2981 "Delete this message and move to next nondeleted one.
2982 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
2983 With prefix argument, delete and move backward.
2985 Returns t if a new message is displayed after the delete, or nil otherwise."
2987 (rmail-set-attribute "deleted" t
)
2988 (run-hooks 'rmail-delete-message-hook
)
2989 (let ((del-msg rmail-current-message
))
2990 (if (rmail-summary-exists)
2991 (rmail-select-summary
2992 (rmail-summary-mark-deleted del-msg
)))
2993 (prog1 (rmail-next-undeleted-message (if backward -
1 1))
2994 (rmail-maybe-display-summary))))
2996 (defun rmail-delete-backward ()
2997 "Delete this message and move to previous nondeleted one.
2998 Deleted messages stay in the file until the \\[rmail-expunge] command is given."
3000 (rmail-delete-forward t
))
3002 ;; Compute the message number a given message would have after expunging.
3003 ;; The present number of the message is OLDNUM.
3004 ;; DELETEDVEC should be rmail-deleted-vector.
3005 ;; The value is nil for a message that would be deleted.
3006 (defun rmail-msg-number-after-expunge (deletedvec oldnum
)
3007 (if (or (null oldnum
) (= (aref deletedvec oldnum
) ?D
))
3012 (if (/= (aref deletedvec i
) ?D
)
3013 (setq newnum
(1+ newnum
)))
3017 (defun rmail-expunge-confirmed ()
3018 "Return t if deleted message should be expunged. If necessary, ask the user.
3019 See also user-option `rmail-confirm-expunge'."
3020 (set-buffer rmail-buffer
)
3021 (or (not (stringp rmail-deleted-vector
))
3022 (not (string-match "D" rmail-deleted-vector
))
3023 (null rmail-confirm-expunge
)
3024 (funcall rmail-confirm-expunge
3025 "Erase deleted messages from Rmail file? ")))
3027 (defun rmail-only-expunge ()
3028 "Actually erase all deleted messages in the file."
3030 (set-buffer rmail-buffer
)
3031 (message "Expunging deleted messages...")
3032 ;; Discard all undo records for this buffer.
3033 (or (eq buffer-undo-list t
)
3034 (setq buffer-undo-list nil
))
3035 (rmail-maybe-set-message-counters)
3036 (let* ((omax (- (buffer-size) (point-max)))
3037 (omin (- (buffer-size) (point-min)))
3038 (opoint (if (and (> rmail-current-message
0)
3039 (rmail-message-deleted-p rmail-current-message
))
3041 (if rmail-enable-mime
3042 (with-current-buffer rmail-view-buffer
3043 (- (point)(point-min)))
3044 (- (point) (point-min)))))
3045 (messages-head (cons (aref rmail-message-vector
0) nil
))
3046 (messages-tail messages-head
)
3047 ;; Don't make any undo records for the expunging.
3048 (buffer-undo-list t
)
3053 (goto-char (point-min))
3056 (total rmail-total-messages
)
3057 (new-message-number rmail-current-message
)
3059 (new-msgref (list (list 0)))
3060 (rmailbuf (current-buffer))
3061 (buffer-read-only nil
)
3062 (messages rmail-message-vector
)
3063 (deleted rmail-deleted-vector
)
3064 (summary rmail-summary-vector
))
3065 (setq rmail-total-messages nil
3066 rmail-current-message nil
3067 rmail-message-vector nil
3068 rmail-deleted-vector nil
3069 rmail-summary-vector nil
)
3071 (while (<= number total
)
3072 (if (= (aref deleted number
) ?D
)
3075 (marker-position (aref messages number
))
3076 (marker-position (aref messages
(1+ number
))))
3077 (move-marker (aref messages number
) nil
)
3078 (if (> new-message-number counter
)
3079 (setq new-message-number
(1- new-message-number
))))
3080 (setq counter
(1+ counter
))
3082 (setcdr messages-tail
3083 (cons (aref messages number
) nil
)))
3085 (cons (if (= counter number
) (aref summary
(1- number
)))
3088 (cons (aref rmail-msgref-vector number
)
3090 (setcar (car new-msgref
) counter
))
3091 (if (zerop (%
(setq number
(1+ number
)) 20))
3092 (message "Expunging deleted messages...%d" number
)))
3094 (setcdr messages-tail
3095 (cons (aref messages number
) nil
)))
3096 (setq rmail-current-message new-message-number
3097 rmail-total-messages counter
3098 rmail-message-vector
(apply 'vector messages-head
)
3099 rmail-deleted-vector
(make-string (1+ counter
) ?\
)
3100 rmail-summary-vector
(vconcat (nreverse new-summary
))
3101 rmail-msgref-vector
(apply 'vector
(nreverse new-msgref
))
3103 (message "Expunging deleted messages...done")
3105 (narrow-to-region (- (buffer-size) omin
) (- (buffer-size) omax
)))
3107 (if (zerop rmail-current-message
) 1 nil
))
3108 (if rmail-enable-mime
3109 (goto-char (+ (point-min) opoint
))
3110 (goto-char (+ (point) opoint
))))))
3112 (defun rmail-expunge ()
3113 "Erase deleted messages from Rmail file and summary buffer."
3115 (when (rmail-expunge-confirmed)
3116 (rmail-only-expunge)
3117 (if (rmail-summary-exists)
3118 (rmail-select-summary (rmail-update-summary)))))
3120 ;;;; *** Rmail Mailing Commands ***
3122 (defun rmail-start-mail (&optional noerase to subject in-reply-to cc
3123 replybuffer sendactions same-window others
)
3126 (setq yank-action
(list 'insert-buffer replybuffer
)))
3127 (setq others
(cons (cons "cc" cc
) others
))
3128 (setq others
(cons (cons "in-reply-to" in-reply-to
) others
))
3130 (compose-mail to subject others
3132 yank-action sendactions
)
3133 (if (and (display-multi-frame-p) rmail-mail-new-frame
)
3135 (compose-mail to subject others
3136 noerase
'switch-to-buffer-other-frame
3137 yank-action sendactions
)
3138 ;; This is not a standard frame parameter;
3139 ;; nothing except sendmail.el looks at it.
3140 (modify-frame-parameters (selected-frame)
3141 '((mail-dedicated-frame . t
))))
3142 (compose-mail to subject others
3143 noerase
'switch-to-buffer-other-window
3144 yank-action sendactions
)))))
3146 (defun rmail-mail ()
3147 "Send mail in another window.
3148 While composing the message, use \\[mail-yank-original] to yank the
3149 original message into it."
3151 (rmail-start-mail nil nil nil nil nil rmail-view-buffer
))
3153 (defun rmail-continue ()
3154 "Continue composing outgoing message previously being composed."
3156 (rmail-start-mail t
))
3158 (defun rmail-reply (just-sender)
3159 "Reply to the current message.
3160 Normally include CC: to all other recipients of original message;
3161 prefix argument means ignore them. While composing the reply,
3162 use \\[mail-yank-original] to yank the original message into it."
3164 (let (from reply-to cc subject date to message-id references
3165 resent-to resent-cc resent-reply-to
3166 (msgnum rmail-current-message
))
3169 (if rmail-enable-mime
3171 (goto-char (point-min))
3172 (if (search-forward "\n\n" nil
'move
)
3173 (1+ (match-beginning 0))
3176 (goto-char (rmail-msgbeg rmail-current-message
))
3178 (if (= (following-char) ?
0)
3180 (progn (forward-line 2)
3182 (progn (search-forward "\n\n" (rmail-msgend rmail-current-message
)
3185 (narrow-to-region (point)
3186 (progn (search-forward "\n*** EOOH ***\n")
3187 (beginning-of-line) (point)))))
3188 (setq from
(mail-fetch-field "from")
3189 reply-to
(or (mail-fetch-field "reply-to" nil t
)
3191 cc
(and (not just-sender
)
3192 (mail-fetch-field "cc" nil t
))
3193 subject
(mail-fetch-field "subject")
3194 date
(mail-fetch-field "date")
3195 to
(or (mail-fetch-field "to" nil t
) "")
3196 message-id
(mail-fetch-field "message-id")
3197 references
(mail-fetch-field "references" nil nil t
)
3198 resent-reply-to
(mail-fetch-field "resent-reply-to" nil t
)
3199 resent-cc
(and (not just-sender
)
3200 (mail-fetch-field "resent-cc" nil t
))
3201 resent-to
(or (mail-fetch-field "resent-to" nil t
) "")
3202 ;;; resent-subject (mail-fetch-field "resent-subject")
3203 ;;; resent-date (mail-fetch-field "resent-date")
3204 ;;; resent-message-id (mail-fetch-field "resent-message-id")
3206 ;; Merge the resent-to and resent-cc into the to and cc.
3207 (if (and resent-to
(not (equal resent-to
"")))
3208 (if (not (equal to
""))
3209 (setq to
(concat to
", " resent-to
))
3210 (setq to resent-to
)))
3211 (if (and resent-cc
(not (equal resent-cc
"")))
3212 (if (not (equal cc
""))
3213 (setq cc
(concat cc
", " resent-cc
))
3214 (setq cc resent-cc
)))
3215 ;; Add `Re: ' to subject if not there already.
3216 (and (stringp subject
)
3218 (concat rmail-reply-prefix
3219 (if (let ((case-fold-search t
))
3220 (string-match rmail-reply-regexp subject
))
3221 (substring subject
(match-end 0))
3225 ;; Using mail-strip-quoted-names is undesirable with newer mailers
3226 ;; since they can handle the names unstripped.
3227 ;; I don't know whether there are other mailers that still
3228 ;; need the names to be stripped.
3229 ;;; (mail-strip-quoted-names reply-to)
3232 (rmail-make-in-reply-to-field from date message-id
)
3235 ;; mail-strip-quoted-names is NOT necessary for rmail-dont-reply-to
3237 (let* ((cc-list (rmail-dont-reply-to
3238 (mail-strip-quoted-names
3239 (if (null cc
) to
(concat to
", " cc
))))))
3240 (if (string= cc-list
"") nil cc-list
)))
3242 (list (list 'rmail-mark-message
3244 (with-current-buffer rmail-buffer
3245 (aref rmail-msgref-vector msgnum
))
3248 (list (cons "References" (concat (mapconcat 'identity references
" ")
3249 " " message-id
))))))
3251 (defun rmail-mark-message (buffer msgnum-list attribute
)
3252 "Give BUFFER's message number in MSGNUM-LIST the attribute ATTRIBUTE.
3253 This is use in the send-actions for message buffers.
3254 MSGNUM-LIST is a list of the form (MSGNUM)
3255 which is an element of rmail-msgref-vector."
3258 (if (car msgnum-list
)
3259 (rmail-set-attribute attribute t
(car msgnum-list
)))))
3261 (defun rmail-make-in-reply-to-field (from date message-id
)
3268 (let ((tem (car (rfc822-addresses from
))))
3272 (regexp-quote (if (string-match "@[^@]*\\'" tem
)
3274 (match-beginning 0))
3277 ;; missing From, or Message-ID is sufficiently informative
3279 (concat message-id
" (" tem
")"))
3280 ;; Copy TEM, discarding text properties.
3281 (setq tem
(copy-sequence tem
))
3282 (set-text-properties 0 (length tem
) nil tem
)
3283 (setq tem
(copy-sequence tem
))
3284 ;; Use prin1 to fake RFC822 quoting
3285 (let ((field (prin1-to-string tem
)))
3287 (concat field
"'s message of " date
)
3289 ((let* ((foo "[^][\000-\037()<>@,;:\\\" ]+")
3290 (bar "[^][\000-\037()<>@,;:\\\"]+"))
3291 ;; These strings both match all non-ASCII characters.
3292 (or (string-match (concat "\\`[ \t]*\\(" bar
3293 "\\)\\(<" foo
"@" foo
">\\)?[ \t]*\\'")
3294 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
3296 (string-match (concat "\\`[ \t]*<" foo
"@" foo
">[ \t]*(\\("
3297 bar
"\\))[ \t]*\\'")
3298 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
3300 (let ((start (match-beginning 1))
3301 (end (match-end 1)))
3302 ;; Trim whitespace which above regexp match allows
3303 (while (and (< start end
)
3304 (memq (aref from start
) '(?
\t ?\
)))
3305 (setq start
(1+ start
)))
3306 (while (and (< start end
)
3307 (memq (aref from
(1- end
)) '(?
\t ?\
)))
3308 (setq end
(1- end
)))
3309 (let ((field (substring from start end
)))
3310 (if date
(setq field
(concat "message from " field
" on " date
)))
3312 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
3313 (concat message-id
" (" field
")")
3316 ;; If we can't kludge it simply, do it correctly
3317 (let ((mail-use-rfc822 t
))
3318 (rmail-make-in-reply-to-field from date message-id
)))))
3320 (defun rmail-forward (resend)
3321 "Forward the current message to another user.
3322 With prefix argument, \"resend\" the message instead of forwarding it;
3323 see the documentation of `rmail-resend'."
3326 (call-interactively 'rmail-resend
)
3327 (let ((forward-buffer rmail-buffer
)
3328 (msgnum rmail-current-message
)
3329 (subject (concat "["
3330 (let ((from (or (mail-fetch-field "From")
3331 (mail-fetch-field ">From"))))
3333 (concat (mail-strip-quoted-names from
) ": ")
3335 (or (mail-fetch-field "Subject") "")
3337 (if (rmail-start-mail
3338 nil nil subject nil nil nil
3339 (list (list 'rmail-mark-message
3341 (with-current-buffer rmail-buffer
3342 (aref rmail-msgref-vector msgnum
))
3344 ;; If only one window, use it for the mail buffer.
3345 ;; Otherwise, use another window for the mail buffer
3346 ;; so that the Rmail buffer remains visible
3347 ;; and sending the mail will get back to it.
3348 (and (not rmail-mail-new-frame
) (one-window-p t
)))
3349 ;; The mail buffer is now current.
3351 ;; Insert after header separator--before signature if any.
3352 (goto-char (mail-text-start))
3353 (if (or rmail-enable-mime rmail-enable-mime-composing
)
3354 (funcall rmail-insert-mime-forwarded-message-function
3356 (insert "------- Start of forwarded message -------\n")
3357 ;; Quote lines with `- ' if they start with `-'.
3358 (let ((beg (point)) end
)
3359 (setq end
(point-marker))
3360 (set-marker-insertion-type end t
)
3361 (insert-buffer-substring forward-buffer
)
3363 (while (re-search-forward "^-" end t
)
3368 (skip-chars-backward "\n")
3371 (delete-region (point) end
)
3372 (set-marker end nil
))
3373 (insert "------- End of forwarded message -------\n"))
3376 (defun rmail-resend (address &optional from comment mail-alias-file
)
3377 "Resend current message to ADDRESSES.
3378 ADDRESSES should be a single address, a string consisting of several
3379 addresses separated by commas, or a list of addresses.
3381 Optional FROM is the address to resend the message from, and
3382 defaults from the value of `user-mail-address'.
3383 Optional COMMENT is a string to insert as a comment in the resent message.
3384 Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
3385 typically for purposes of moderating a list."
3386 (interactive "sResend to: ")
3388 (require 'mailalias
)
3389 (unless (or (eq rmail-view-buffer
(current-buffer))
3390 (eq rmail-buffer
(current-buffer)))
3391 (error "Not an Rmail buffer"))
3392 (if (not from
) (setq from user-mail-address
))
3393 (let ((tembuf (generate-new-buffer " sendmail temp"))
3394 (case-fold-search nil
)
3395 (mailbuf rmail-buffer
))
3397 (with-current-buffer tembuf
3398 ;;>> Copy message into temp buffer
3399 (if rmail-enable-mime
3400 (funcall rmail-insert-mime-resent-message-function mailbuf
)
3401 (insert-buffer-substring mailbuf
))
3402 (goto-char (point-min))
3403 ;; Delete any Sender field, since that's not specifiable.
3404 ; Only delete Sender fields in the actual header.
3405 (re-search-forward "^$" nil
'move
)
3406 ; Using "while" here rather than "if" because some buggy mail
3407 ; software may have inserted multiple Sender fields.
3408 (while (re-search-backward "^Sender:" nil t
)
3412 (while (looking-at "[ \t]")
3414 (delete-region beg
(point))))
3415 ; Go back to the beginning of the buffer so the Resent- fields
3416 ; are inserted there.
3417 (goto-char (point-min))
3418 ;;>> Insert resent-from:
3419 (insert "Resent-From: " from
"\n")
3420 (insert "Resent-Date: " (mail-rfc822-date) "\n")
3421 ;;>> Insert resent-to: and bcc if need be.
3422 (let ((before (point)))
3424 (insert "Resent-Bcc: " (user-login-name) "\n"))
3425 (insert "Resent-To: " (if (stringp address
)
3427 (mapconcat 'identity address
",\n\t"))
3429 ;; Expand abbrevs in the recipients.
3431 (if (featurep 'mailabbrev
)
3432 (let ((end (point-marker))
3433 (local-abbrev-table mail-abbrevs
)
3434 (old-syntax-table (syntax-table)))
3435 (if (and (not (vectorp mail-abbrevs
))
3436 (file-exists-p mail-personal-alias-file
))
3437 (build-mail-abbrevs))
3438 (unless mail-abbrev-syntax-table
3439 (mail-abbrev-make-syntax-table))
3440 (set-syntax-table mail-abbrev-syntax-table
)
3442 (while (and (< (point) end
)
3443 (progn (forward-word 1)
3446 (set-syntax-table old-syntax-table
))
3447 (expand-mail-aliases before
(point)))))
3448 ;;>> Set up comment, if any.
3449 (if (and (sequencep comment
) (not (zerop (length comment
))))
3450 (let ((before (point))
3453 (or (eolp) (insert "\n"))
3454 (setq after
(point))
3456 (while (< (point) after
)
3457 (insert "Resent-Comment: ")
3459 ;; Don't expand aliases in the destination fields
3460 ;; of the original message.
3462 (funcall send-mail-function
)))
3463 (kill-buffer tembuf
))
3464 (with-current-buffer rmail-buffer
3465 (rmail-set-attribute "resent" t rmail-current-message
))))
3467 (defvar mail-unsent-separator
3468 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
3469 "^ *---+ +Returned message +---+ *$\\|"
3470 "^ *---+ *Returned mail follows *---+ *$\\|"
3471 "^Start of returned message$\\|"
3472 "^---+ Below this line is a copy of the message.$\\|"
3473 "^ *---+ +Original message +---+ *$\\|"
3474 "^ *--+ +begin message +--+ *$\\|"
3475 "^ *---+ +Original message follows +---+ *$\\|"
3476 "^ *---+ +Your message follows +---+ *$\\|"
3477 "^|? *---+ +Message text follows: +---+ *|?$\\|"
3478 "^ *---+ +This is a copy of \\w+ message, including all the headers.*---+ *$")
3479 "A regexp that matches the separator before the text of a failed message.")
3481 (defvar mail-mime-unsent-header
"^Content-Type: message/rfc822 *$"
3482 "A regexp that matches the header of a MIME body part with a failed message.")
3484 (defun rmail-retry-failure ()
3485 "Edit a mail message which is based on the contents of the current message.
3486 For a message rejected by the mail system, extract the interesting headers and
3487 the body of the original message.
3488 If the failed message is a MIME multipart message, it is searched for a
3489 body part with a header which matches the variable `mail-mime-unsent-header'.
3490 Otherwise, the variable `mail-unsent-separator' should match the string that
3491 delimits the returned original message.
3492 The variable `rmail-retry-ignored-headers' is a regular expression
3493 specifying headers which should not be copied into the new message."
3495 (require 'mail-utils
)
3496 (let ((rmail-this-buffer (current-buffer))
3497 (msgnum rmail-current-message
)
3498 bounce-start bounce-end bounce-indent resending
3499 ;; Fetch any content-type header in current message
3500 ;; Must search thru the whole unpruned header.
3504 (rmail-narrow-to-non-pruned-header)
3505 (mail-fetch-field "Content-Type") ))))
3507 (goto-char (point-min))
3508 (let ((case-fold-search t
))
3509 (if (and content-type
3511 ";[\n\t ]*boundary=\"?\\([-0-9a-z'()+_,./:=? ]+\\)\"?"
3513 ;; Handle a MIME multipart bounce message.
3516 (substring content-type
(match-beginning 1)
3518 (unless (re-search-forward mail-mime-unsent-header nil t
)
3519 (error "Cannot find beginning of header in failed message"))
3520 (unless (search-forward "\n\n" nil t
)
3521 (error "Cannot find start of Mime data in failed message"))
3522 (setq bounce-start
(point))
3523 (if (search-forward codestring nil t
)
3524 (setq bounce-end
(match-beginning 0))
3525 (setq bounce-end
(point-max))))
3527 (or (re-search-forward mail-unsent-separator nil t
)
3528 (error "Cannot parse this as a failure message"))
3529 (skip-chars-forward "\n")
3530 ;; Support a style of failure message in which the original
3531 ;; message is indented, and included within lines saying
3532 ;; `Start of returned message' and `End of returned message'.
3533 (if (looking-at " +Received:")
3535 (setq bounce-start
(point))
3536 (skip-chars-forward " ")
3537 (setq bounce-indent
(- (current-column)))
3538 (goto-char (point-max))
3539 (re-search-backward "^End of returned message$" nil t
)
3540 (setq bounce-end
(point)))
3541 ;; One message contained a few random lines before
3542 ;; the old message header. The first line of the
3543 ;; message started with two hyphens. A blank line
3544 ;; followed these random lines. The same line
3545 ;; beginning with two hyphens was possibly marking
3546 ;; the end of the message.
3547 (if (looking-at "^--")
3548 (let ((boundary (buffer-substring-no-properties
3550 (progn (end-of-line) (point)))))
3551 (search-forward "\n\n")
3552 (skip-chars-forward "\n")
3553 (setq bounce-start
(point))
3554 (goto-char (point-max))
3555 (search-backward (concat "\n\n" boundary
) bounce-start t
)
3556 (setq bounce-end
(point)))
3557 (setq bounce-start
(point)
3558 bounce-end
(point-max)))
3559 (unless (search-forward "\n\n" nil t
)
3560 (error "Cannot find end of header in failed message"))))))
3561 ;; We have found the message that bounced, within the current message.
3562 ;; Now start sending new message; default header fields from original.
3563 ;; Turn off the usual actions for initializing the message body
3564 ;; because we want to get only the text from the failure message.
3565 (let (mail-signature mail-setup-hook
)
3566 (if (rmail-start-mail nil nil nil nil nil rmail-this-buffer
3567 (list (list 'rmail-mark-message
3569 (aref rmail-msgref-vector msgnum
)
3571 ;; Insert original text as initial text of new draft message.
3572 ;; Bind inhibit-read-only since the header delimiter
3573 ;; of the previous message was probably read-only.
3574 (let ((inhibit-read-only t
)
3575 rmail-displayed-headers
3576 rmail-ignored-headers
)
3578 (insert-buffer-substring rmail-this-buffer
3579 bounce-start bounce-end
)
3580 (goto-char (point-min))
3582 (indent-rigidly (point-min) (point-max) bounce-indent
))
3583 (rmail-clear-headers rmail-retry-ignored-headers
)
3584 (rmail-clear-headers "^sender:\\|^return-path:\\|^received:")
3585 (mail-sendmail-delimit-header)
3587 (narrow-to-region (point-min) (mail-header-end))
3588 (setq resending
(mail-fetch-field "resent-to"))
3591 (insert "Resent-Bcc: " (user-login-name) "\n")
3592 (insert "BCC: " (user-login-name) "\n"))))
3593 (goto-char (point-min))
3594 (mail-position-on-field (if resending
"Resent-To" "To") t
))))))
3596 (defun rmail-summary-exists ()
3597 "Non-nil iff in an RMAIL buffer and an associated summary buffer exists.
3598 In fact, the non-nil value returned is the summary buffer itself."
3599 (and rmail-summary-buffer
(buffer-name rmail-summary-buffer
)
3600 rmail-summary-buffer
))
3602 (defun rmail-summary-displayed ()
3603 "t iff in RMAIL buffer and an associated summary buffer is displayed."
3604 (and rmail-summary-buffer
(get-buffer-window rmail-summary-buffer
)))
3606 (defcustom rmail-redisplay-summary nil
3607 "*Non-nil means Rmail should show the summary when it changes.
3608 This has an effect only if a summary buffer exists."
3610 :group
'rmail-summary
)
3612 (defcustom rmail-summary-window-size nil
3613 "*Non-nil means specify the height for an Rmail summary window."
3614 :type
'(choice (const :tag
"Disabled" nil
) integer
)
3615 :group
'rmail-summary
)
3617 ;; Put the summary buffer back on the screen, if user wants that.
3618 (defun rmail-maybe-display-summary ()
3619 (let ((selected (selected-window))
3621 ;; If requested, make sure the summary is displayed.
3622 (and rmail-summary-buffer
(buffer-name rmail-summary-buffer
)
3623 rmail-redisplay-summary
3624 (if (get-buffer-window rmail-summary-buffer
0)
3625 ;; It's already in some frame; show that one.
3626 (let ((frame (window-frame
3627 (get-buffer-window rmail-summary-buffer
0))))
3628 (make-frame-visible frame
)
3629 (raise-frame frame
))
3630 (display-buffer rmail-summary-buffer
)))
3631 ;; If requested, set the height of the summary window.
3632 (and rmail-summary-buffer
(buffer-name rmail-summary-buffer
)
3633 rmail-summary-window-size
3634 (setq window
(get-buffer-window rmail-summary-buffer
))
3635 ;; Don't try to change the size if just one window in frame.
3636 (not (eq window
(frame-root-window (window-frame window
))))
3639 (select-window window
)
3640 (enlarge-window (- rmail-summary-window-size
(window-height))))
3641 (select-window selected
)))))
3643 ;;;; *** Rmail Local Fontification ***
3645 (defun rmail-fontify-buffer-function ()
3646 ;; This function's symbol is bound to font-lock-fontify-buffer-function.
3647 (add-hook 'rmail-show-message-hook
'rmail-fontify-message nil t
)
3648 ;; If we're already showing a message, fontify it now.
3649 (if rmail-current-message
(rmail-fontify-message))
3650 ;; Prevent Font Lock mode from kicking in.
3651 (setq font-lock-fontified t
))
3653 (defun rmail-unfontify-buffer-function ()
3654 ;; This function's symbol is bound to font-lock-fontify-unbuffer-function.
3655 (let ((modified (buffer-modified-p))
3656 (buffer-undo-list t
) (inhibit-read-only t
)
3657 before-change-functions after-change-functions
3658 buffer-file-name buffer-file-truename
)
3661 (remove-hook 'rmail-show-message-hook
'rmail-fontify-message t
)
3662 (remove-text-properties (point-min) (point-max) '(rmail-fontified nil
))
3663 (font-lock-default-unfontify-buffer)
3664 (and (not modified
) (buffer-modified-p) (set-buffer-modified-p nil
)))))
3666 (defun rmail-fontify-message ()
3667 ;; Fontify the current message if it is not already fontified.
3668 (if (text-property-any (point-min) (point-max) 'rmail-fontified nil
)
3669 (let ((modified (buffer-modified-p))
3670 (buffer-undo-list t
) (inhibit-read-only t
)
3671 before-change-functions after-change-functions
3672 buffer-file-name buffer-file-truename
)
3675 (add-text-properties (point-min) (point-max) '(rmail-fontified t
))
3676 (font-lock-fontify-region (point-min) (point-max))
3677 (and (not modified
) (buffer-modified-p) (set-buffer-modified-p nil
)))))))
3679 ;;; Speedbar support for RMAIL files.
3680 (eval-when-compile (require 'speedbar
))
3682 (defvar rmail-speedbar-match-folder-regexp
"^[A-Z0-9]+\\(\\.[A-Z0-9]+\\)?$"
3683 "*This regex is used to match folder names to be displayed in speedbar.
3684 Enabling this will permit speedbar to display your folders for easy
3685 browsing, and moving of messages.")
3687 (defvar rmail-speedbar-last-user nil
3688 "The last user to be displayed in the speedbar.")
3690 (defvar rmail-speedbar-key-map nil
3691 "Keymap used when in rmail display mode.")
3693 (defun rmail-install-speedbar-variables ()
3694 "Install those variables used by speedbar to enhance rmail."
3695 (if rmail-speedbar-key-map
3697 (setq rmail-speedbar-key-map
(speedbar-make-specialized-keymap))
3699 (define-key rmail-speedbar-key-map
"e" 'speedbar-edit-line
)
3700 (define-key rmail-speedbar-key-map
"r" 'speedbar-edit-line
)
3701 (define-key rmail-speedbar-key-map
"\C-m" 'speedbar-edit-line
)
3702 (define-key rmail-speedbar-key-map
"M"
3703 'rmail-speedbar-move-message-to-folder-on-line
)))
3705 (defvar rmail-speedbar-menu-items
3706 '(["Read Folder" speedbar-edit-line t
]
3707 ["Move message to folder" rmail-speedbar-move-message-to-folder-on-line
3708 (save-excursion (beginning-of-line)
3709 (looking-at "<M> "))])
3710 "Additional menu-items to add to speedbar frame.")
3712 ;; Make sure our special speedbar major mode is loaded
3713 (if (featurep 'speedbar
)
3714 (rmail-install-speedbar-variables)
3715 (add-hook 'speedbar-load-hook
'rmail-install-speedbar-variables
))
3717 (defun rmail-speedbar-buttons (buffer)
3718 "Create buttons for BUFFER containing rmail messages.
3719 Click on the address under Reply to: to reply to this person.
3720 Under Folders: Click a name to read it, or on the <M> to move the
3721 current message into that RMAIL folder."
3725 (goto-char (point-min))
3726 (if (not (re-search-forward "^Reply-To: " nil t
))
3727 (if (not (re-search-forward "^From:? " nil t
))
3731 (setq from
(buffer-substring (point) (save-excursion
3734 (goto-char (point-min))
3735 (if (and (looking-at "Reply to:")
3736 (equal from rmail-speedbar-last-user
))
3738 (setq rmail-speedbar-last-user from
)
3740 (insert "Reply To:\n")
3742 (speedbar-insert-button from
'speedbar-directory-face
'highlight
3743 'rmail-speedbar-button
'rmail-reply
))
3744 (insert "Folders:\n")
3745 (let* ((case-fold-search nil
)
3746 (df (directory-files (save-excursion (set-buffer buffer
)
3748 nil rmail-speedbar-match-folder-regexp
)))
3750 (speedbar-insert-button "<M>" 'speedbar-button-face
'highlight
3751 'rmail-speedbar-move-message
(car df
))
3752 (speedbar-insert-button (car df
) 'speedbar-file-face
'highlight
3753 'rmail-speedbar-find-file nil t
)
3754 (setq df
(cdr df
)))))))
3756 (defun rmail-speedbar-button (text token indent
)
3757 "Execute an rmail command specified by TEXT.
3758 The command used is TOKEN. INDENT is not used."
3759 (speedbar-with-attached-buffer
3762 (defun rmail-speedbar-find-file (text token indent
)
3763 "Load in the rmail file TEXT.
3764 TOKEN and INDENT are not used."
3765 (speedbar-with-attached-buffer
3766 (message "Loading in RMAIL file %s..." text
)
3769 (defun rmail-speedbar-move-message-to-folder-on-line ()
3770 "If the current line is a folder, move current message to it."
3774 (if (re-search-forward "<M> " (save-excursion (end-of-line) (point)) t
)
3777 (speedbar-do-function-pointer)))))
3779 (defun rmail-speedbar-move-message (text token indent
)
3780 "From button TEXT, copy current message to the rmail file specified by TOKEN.
3781 TEXT and INDENT are not used."
3782 (speedbar-with-attached-buffer
3783 (message "Moving message to %s" token
)
3784 (rmail-output-to-rmail-file token
)))
3786 ; Functions for setting, getting and encoding the POP password.
3787 ; The password is encoded to prevent it from being easily accessible
3788 ; to "prying eyes." Obviously, this encoding isn't "real security,"
3789 ; nor is it meant to be.
3792 (defun rmail-set-pop-password (password)
3793 "Set PASSWORD to be used for retrieving mail from a POP server."
3794 (interactive "sPassword: ")
3796 (setq rmail-encoded-pop-password
3797 (rmail-encode-string password
(emacs-pid)))
3798 (setq rmail-pop-password nil
)
3799 (setq rmail-encoded-pop-password nil
)))
3801 (defun rmail-get-pop-password ()
3802 "Get the password for retrieving mail from a POP server. If none
3803 has been set, then prompt the user for one."
3804 (if (not rmail-encoded-pop-password
)
3805 (progn (if (not rmail-pop-password
)
3806 (setq rmail-pop-password
(read-passwd "POP password: ")))
3807 (rmail-set-pop-password rmail-pop-password
)
3808 (setq rmail-pop-password nil
)))
3809 (rmail-encode-string rmail-encoded-pop-password
(emacs-pid)))
3811 (defun rmail-have-password ()
3812 (or rmail-pop-password rmail-encoded-pop-password
))
3814 (defun rmail-encode-string (string mask
)
3815 "Encode STRING with integer MASK, by taking the exclusive OR of the
3816 lowest byte in the mask with the first character of string, the
3817 second-lowest-byte with the second character of the string, etc.,
3818 restarting at the lowest byte of the mask whenever it runs out.
3819 Returns the encoded string. Calling the function again with an
3820 encoded string (and the same mask) will decode the string."
3821 (setq mask
(abs mask
)) ; doesn't work if negative
3822 (let* ((string-vector (string-to-vector string
)) (i 0)
3823 (len (length string-vector
)) (curmask mask
) charmask
)
3826 (setq curmask mask
))
3827 (setq charmask
(% curmask
256))
3828 (setq curmask
(lsh curmask -
8))
3829 (aset string-vector i
(logxor charmask
(aref string-vector i
)))
3831 (concat string-vector
)))
3835 ;;; arch-tag: cff0a950-57fe-4f73-a86e-91ff75afd06c
3836 ;;; rmail.el ends here