(generic-mode-ini-file-find-file-hook):
[emacs.git] / lisp / mail / rmail.el
blobde88b37d91e6a33f5861a41c64284f41ecd1a0ce
1 ;;; rmail.el --- main code of "RMAIL" mail reader for Emacs
3 ;; Copyright (C) 1985,86,87,88,93,94,95,96,97,98,2000,01,2004,2005
4 ;; Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: mail
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)
14 ;; any later version.
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.
26 ;;; Commentary:
28 ;;; Code:
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.
42 (require 'mail-utils)
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")
51 ; "")
53 (defgroup rmail nil
54 "Mail reader for Emacs."
55 :group 'mail)
57 (defgroup rmail-retrieve nil
58 "Rmail retrieval options."
59 :prefix "rmail-"
60 :group 'rmail)
62 (defgroup rmail-files nil
63 "Rmail files."
64 :prefix "rmail-"
65 :group 'rmail)
67 (defgroup rmail-headers nil
68 "Rmail header options."
69 :prefix "rmail-"
70 :group 'rmail)
72 (defgroup rmail-reply nil
73 "Rmail reply options."
74 :prefix "rmail-"
75 :group 'rmail)
77 (defgroup rmail-summary nil
78 "Rmail summary options."
79 :prefix "rmail-"
80 :prefix "rmail-summary-"
81 :group 'rmail)
83 (defgroup rmail-output nil
84 "Output message to a file."
85 :prefix "rmail-output-"
86 :prefix "rmail-"
87 :group 'rmail)
89 (defgroup rmail-edit nil
90 "Rmail editing."
91 :prefix "rmail-edit-"
92 :group 'rmail)
94 (defgroup rmail-obsolete nil
95 "Rmail obsolete customization variables."
96 :group 'rmail)
98 (defcustom rmail-movemail-program nil
99 "If non-nil, the file name of the `movemail' program."
100 :group 'rmail-retrieve
101 :type '(choice (const nil) string))
103 (defcustom rmail-pop-password nil
104 "*Password to use when reading mail from POP server.
105 Please use `rmail-remote-password' instead."
106 :type '(choice (string :tag "Password")
107 (const :tag "Not Required" nil))
108 :group 'rmail-obsolete)
110 (defcustom rmail-pop-password-required nil
111 "*Non-nil if a password is required when reading mail from a POP server.
112 Please use rmail-remote-password-required instead."
113 :type 'boolean
114 :group 'rmail-obsolete)
116 (defcustom rmail-remote-password nil
117 "*Password to use when reading mail from a remote server.
118 This setting is ignored for mailboxes whose URL already contains a password."
119 :type '(choice (string :tag "Password")
120 (const :tag "Not Required" nil))
121 :set-after '(rmail-pop-password)
122 :set #'(lambda (symbol value)
123 (set-default symbol
124 (if (and (not value)
125 (boundp 'rmail-pop-password)
126 rmail-pop-password)
127 rmail-pop-password
128 value))
129 (setq rmail-pop-password nil))
130 :group 'rmail-retrieve
131 :version "22.1")
133 (defcustom rmail-remote-password-required nil
134 "*Non-nil if a password is required when reading mail from a remote server."
135 :type 'boolean
136 :set-after '(rmail-pop-password-required)
137 :set #'(lambda (symbol value)
138 (set-default symbol
139 (if (and (not value)
140 (boundp 'rmail-pop-password-required)
141 rmail-pop-password-required)
142 rmail-pop-password-required
143 value))
144 (setq rmail-pop-password-required nil))
145 :group 'rmail-retrieve
146 :version "22.1")
148 (defcustom rmail-movemail-flags nil
149 "*List of flags to pass to movemail.
150 Most commonly used to specify `-g' to enable GSS-API authentication
151 or `-k' to enable Kerberos authentication."
152 :type '(repeat string)
153 :group 'rmail-retrieve
154 :version "20.3")
156 (defvar rmail-remote-password-error "invalid usercode or password\\|
157 unknown user name or bad password\\|Authentication failed\\|MU_ERR_AUTH_FAILURE"
158 "Regular expression matching incorrect-password POP or IMAP server error
159 messages.
160 If you get an incorrect-password error that this expression does not match,
161 please report it with \\[report-emacs-bug].")
163 (defvar rmail-encoded-remote-password nil)
165 (defcustom rmail-preserve-inbox nil
166 "*Non-nil means leave incoming mail in the user's inbox--don't delete it."
167 :type 'boolean
168 :group 'rmail-retrieve)
170 (defcustom rmail-movemail-search-path nil
171 "*List of directories to search for movemail (in addition to `exec-path')."
172 :group 'rmail-retrieve
173 :type '(repeat (directory)))
175 (defun rmail-probe (prog)
176 "Determine what flavor of movemail PROG is.
177 We do this by executing it with `--version' and analyzing its output."
178 (with-temp-buffer
179 (let ((tbuf (current-buffer)))
180 (buffer-disable-undo tbuf)
181 (call-process prog nil tbuf nil "--version")
182 (if (not (buffer-modified-p tbuf))
183 ;; Should not happen...
185 (goto-char (point-min))
186 (cond
187 ((looking-at ".*movemail: invalid option")
188 'emacs) ;; Possibly...
189 ((looking-at "movemail (GNU Mailutils .*)")
190 'mailutils)
192 ;; FIXME:
193 'emacs))))))
195 (defun rmail-autodetect ()
196 "Determine and return the file name of the `movemail' program.
197 If `rmail-movemail-program' is non-nil, use it.
198 Otherwise, look for `movemail' in the directories in
199 `rmail-movemail-search-path', those in `exec-path', and `exec-directory'."
200 (if rmail-movemail-program
201 (rmail-probe rmail-movemail-program)
202 (catch 'scan
203 (dolist (dir (append rmail-movemail-search-path exec-path
204 (list exec-directory)))
205 (when (and dir (file-accessible-directory-p dir))
206 (let ((progname (expand-file-name "movemail" dir)))
207 (when (and (not (file-directory-p progname))
208 (file-executable-p progname))
209 (let ((x (rmail-probe progname)))
210 (when x
211 (setq rmail-movemail-program progname)
212 (throw 'scan x))))))))))
214 (defvar rmail-movemail-variant-in-use nil
215 "The movemail variant currently in use. Known variants are:
217 `emacs' Means any implementation, compatible with the native Emacs one.
218 This is the default;
219 `mailutils' Means GNU mailutils implementation, capable of handling full
220 mail URLs as the source mailbox;")
222 ;;;###autoload
223 (defun rmail-movemail-variant-p (&rest variants)
224 "Return t if the current movemail variant is any of VARIANTS.
225 Currently known variants are 'emacs and 'mailutils."
226 (when (not rmail-movemail-variant-in-use)
227 ;; Autodetect
228 (setq rmail-movemail-variant-in-use (rmail-autodetect)))
229 (not (null (member rmail-movemail-variant-in-use variants))))
231 ;;;###autoload
232 (defcustom rmail-dont-reply-to-names nil "\
233 *A regexp specifying addresses to prune from a reply message.
234 A value of nil means exclude your own email address as an address
235 plus whatever is specified by `rmail-default-dont-reply-to-names'."
236 :type '(choice regexp (const :tag "Your Name" nil))
237 :group 'rmail-reply)
239 ;;;###autoload
240 (defvar rmail-default-dont-reply-to-names "\\`info-" "\
241 A regular expression specifying part of the default value of the
242 variable `rmail-dont-reply-to-names', for when the user does not set
243 `rmail-dont-reply-to-names' explicitly. (The other part of the default
244 value is the user's email address and name.)
245 It is useful to set this variable in the site customization file.")
247 ;;;###autoload
248 (defcustom rmail-ignored-headers
249 (concat "^via:\\|^mail-from:\\|^origin:\\|^references:"
250 "\\|^status:\\|^received:\\|^x400-originator:\\|^x400-recipients:"
251 "\\|^x400-received:\\|^x400-mts-identifier:\\|^x400-content-type:"
252 "\\|^\\(resent-\\|\\)message-id:\\|^summary-line:\\|^resent-date:"
253 "\\|^nntp-posting-host:\\|^path:\\|^x-char.*:\\|^x-face:\\|^face:"
254 "\\|^x-mailer:\\|^delivered-to:\\|^lines:\\|^mime-version:"
255 "\\|^content-transfer-encoding:\\|^x-coding-system:"
256 "\\|^return-path:\\|^errors-to:\\|^return-receipt-to:"
257 "\\|^x-sign:\\|^x-beenthere:\\|^x-mailman-version:"
258 "\\|^precedence:\\|^list-help:\\|^list-post:\\|^list-subscribe:"
259 "\\|^list-id:\\|^list-unsubscribe:\\|^list-archive:"
260 "\\|^content-type:\\|^content-length:"
261 "\\|^x-attribution:\\|^x-disclaimer:\\|^x-trace:"
262 "\\|^x-complaints-to:\\|^nntp-posting-date:\\|^user-agent"
263 "\\|^importance:\\|^envelope-to:\\|^delivery-date"
264 "\\|^x.*-priority:\\|^x-mimeole:")
265 "*Regexp to match header fields that Rmail should normally hide.
266 This variable is used for reformatting the message header,
267 which normally happens once for each message,
268 when you view the message for the first time in Rmail.
269 To make a change in this variable take effect
270 for a message that you have already viewed,
271 go to that message and type \\[rmail-toggle-header] twice."
272 :type 'regexp
273 :group 'rmail-headers)
275 ;;;###autoload
276 (defcustom rmail-displayed-headers nil
277 "*Regexp to match Header fields that Rmail should display.
278 If nil, display all header fields except those matched by
279 `rmail-ignored-headers'."
280 :type '(choice regexp (const :tag "All"))
281 :group 'rmail-headers)
283 ;;;###autoload
284 (defcustom rmail-retry-ignored-headers "^x-authentication-warning:" "\
285 *Headers that should be stripped when retrying a failed message."
286 :type '(choice regexp (const nil :tag "None"))
287 :group 'rmail-headers)
289 ;;;###autoload
290 (defcustom rmail-highlighted-headers "^From:\\|^Subject:" "\
291 *Regexp to match Header fields that Rmail should normally highlight.
292 A value of nil means don't highlight.
293 See also `rmail-highlight-face'."
294 :type 'regexp
295 :group 'rmail-headers)
297 ;;;###autoload
298 (defcustom rmail-highlight-face nil "\
299 *Face used by Rmail for highlighting headers."
300 :type '(choice (const :tag "Default" nil)
301 face)
302 :group 'rmail-headers)
304 ;;;###autoload
305 (defcustom rmail-delete-after-output nil "\
306 *Non-nil means automatically delete a message that is copied to a file."
307 :type 'boolean
308 :group 'rmail-files)
310 ;;;###autoload
311 (defcustom rmail-primary-inbox-list nil "\
312 *List of files which are inboxes for user's primary mail file `~/RMAIL'.
313 nil means the default, which is (\"/usr/spool/mail/$USER\")
314 \(the name varies depending on the operating system,
315 and the value of the environment variable MAIL overrides it)."
316 ;; Don't use backquote here, because we don't want to need it
317 ;; at load time.
318 :type (list 'choice '(const :tag "Default" nil)
319 (list 'repeat ':value (list (or (getenv "MAIL")
320 (concat "/var/spool/mail/"
321 (getenv "USER"))))
322 'file))
323 :group 'rmail-retrieve
324 :group 'rmail-files)
326 ;;;###autoload
327 (defcustom rmail-mail-new-frame nil
328 "*Non-nil means Rmail makes a new frame for composing outgoing mail.
329 This is handy if you want to preserve the window configuration of
330 the frame where you have the RMAIL buffer displayed."
331 :type 'boolean
332 :group 'rmail-reply)
334 ;;;###autoload
335 (defcustom rmail-secondary-file-directory "~/"
336 "*Directory for additional secondary Rmail files."
337 :type 'directory
338 :group 'rmail-files)
339 ;;;###autoload
340 (defcustom rmail-secondary-file-regexp "\\.xmail$"
341 "*Regexp for which files are secondary Rmail files."
342 :type 'regexp
343 :group 'rmail-files)
345 ;;;###autoload
346 (defcustom rmail-confirm-expunge 'y-or-n-p
347 "*Whether and how to ask for confirmation before expunging deleted messages."
348 :type '(choice (const :tag "No confirmation" nil)
349 (const :tag "Confirm with y-or-n-p" y-or-n-p)
350 (const :tag "Confirm with yes-or-no-p" yes-or-no-p))
351 :version "21.1"
352 :group 'rmail-files)
354 ;;;###autoload
355 (defvar rmail-mode-hook nil
356 "List of functions to call when Rmail is invoked.")
358 ;;;###autoload
359 (defvar rmail-get-new-mail-hook nil
360 "List of functions to call when Rmail has retrieved new mail.")
362 ;;;###autoload
363 (defcustom rmail-show-message-hook nil
364 "List of functions to call when Rmail displays a message."
365 :type 'hook
366 :options '(goto-address)
367 :group 'rmail)
369 ;;;###autoload
370 (defvar rmail-quit-hook nil
371 "List of functions to call when quitting out of Rmail.")
373 ;;;###autoload
374 (defvar rmail-delete-message-hook nil
375 "List of functions to call when Rmail deletes a message.
376 When the hooks are called, the message has been marked deleted but is
377 still the current message in the Rmail buffer.")
379 ;; These may be altered by site-init.el to match the format of mmdf files
380 ;; delimiting used on a given host (delim1 and delim2 from the config
381 ;; files).
383 (defvar rmail-mmdf-delim1 "^\001\001\001\001\n"
384 "Regexp marking the start of an mmdf message.")
385 (defvar rmail-mmdf-delim2 "^\001\001\001\001\n"
386 "Regexp marking the end of an mmdf message.")
388 (defcustom rmail-message-filter nil
389 "If non-nil, a filter function for new messages in RMAIL.
390 Called with region narrowed to the message, including headers,
391 before obeying `rmail-ignored-headers'."
392 :group 'rmail-headers
393 :type 'function)
395 (defcustom rmail-automatic-folder-directives nil
396 "List of directives specifying where to put a message.
397 Each element of the list is of the form:
399 (FOLDERNAME FIELD REGEXP [ FIELD REGEXP ] ... )
401 Where FOLDERNAME is the name of a BABYL format folder to put the
402 message. If any of the field regexp's are nil, then it is ignored.
404 If FOLDERNAME is \"/dev/null\", it is deleted.
405 If FOLDERNAME is nil then it is deleted, and skipped.
407 FIELD is the plain text name of a field in the message, such as
408 \"subject\" or \"from\". A FIELD of \"to\" will automatically include
409 all text from the \"cc\" field as well.
411 REGEXP is an expression to match in the preceeding specified FIELD.
412 FIELD/REGEXP pairs continue in the list.
414 examples:
415 (\"/dev/null\" \"from\" \"@spam.com\") ; delete all mail from spam.com
416 (\"RMS\" \"from\" \"rms@\") ; save all mail from RMS."
417 :group 'rmail
418 :version "21.1"
419 :type '(repeat (sexp :tag "Directive")))
421 (defvar rmail-reply-prefix "Re: "
422 "String to prepend to Subject line when replying to a message.")
424 ;; Some mailers use "Re(2):" or "Re^2:" or "Re: Re:" or "Re[2]:".
425 ;; This pattern should catch all the common variants.
426 (defvar rmail-reply-regexp "\\`\\(Re\\(([0-9]+)\\|\\[[0-9]+\\]\\|\\^[0-9]+\\)?: *\\)*"
427 "Regexp to delete from Subject line before inserting `rmail-reply-prefix'.")
429 (defcustom rmail-display-summary nil
430 "*If non-nil, Rmail always displays the summary buffer."
431 :group 'rmail-summary
432 :type 'boolean)
434 (defvar rmail-inbox-list nil)
435 (put 'rmail-inbox-list 'permanent-local t)
437 (defvar rmail-keywords nil)
438 (put 'rmail-keywords 'permanent-local t)
440 (defvar rmail-buffer nil
441 "The RMAIL buffer related to the current buffer.
442 In an RMAIL buffer, this holds the RMAIL buffer itself.
443 In a summary buffer, this holds the RMAIL buffer it is a summary for.")
444 (put 'rmail-buffer 'permanent-local t)
446 ;; Message counters and markers. Deleted flags.
448 (defvar rmail-current-message nil)
449 (put 'rmail-current-message 'permanent-local t)
451 (defvar rmail-total-messages nil)
452 (put 'rmail-total-messages 'permanent-local t)
454 (defvar rmail-message-vector nil)
455 (put 'rmail-message-vector 'permanent-local t)
457 (defvar rmail-deleted-vector nil)
458 (put 'rmail-deleted-vector 'permanent-local t)
460 (defvar rmail-msgref-vector nil
461 "In an Rmail buffer, a vector whose Nth element is a list (N).
462 When expunging renumbers messages, these lists are modified
463 by substituting the new message number into the existing list.")
464 (put 'rmail-msgref-vector 'permanent-local t)
466 (defvar rmail-overlay-list nil)
467 (put 'rmail-overlay-list 'permanent-local t)
469 ;; These are used by autoloaded rmail-summary.
471 (defvar rmail-summary-buffer nil)
472 (put 'rmail-summary-buffer 'permanent-local t)
473 (defvar rmail-summary-vector nil)
474 (put 'rmail-summary-vector 'permanent-local t)
476 (defvar rmail-view-buffer nil
477 "Buffer which holds RMAIL message for MIME displaying.")
478 (put 'rmail-view-buffer 'permanent-local t)
480 ;; `Sticky' default variables.
482 ;; Last individual label specified to a or k.
483 (defvar rmail-last-label nil)
484 (put 'rmail-last-label 'permanent-local t)
486 ;; Last set of values specified to C-M-n, C-M-p, C-M-s or C-M-l.
487 (defvar rmail-last-multi-labels nil)
489 (defvar rmail-last-regexp nil)
490 (put 'rmail-last-regexp 'permanent-local t)
492 (defcustom rmail-default-file "~/xmail"
493 "*Default file name for \\[rmail-output]."
494 :type 'file
495 :group 'rmail-files)
496 (defcustom rmail-default-rmail-file "~/XMAIL"
497 "*Default file name for \\[rmail-output-to-rmail-file]."
498 :type 'file
499 :group 'rmail-files)
500 (defcustom rmail-default-body-file "~/mailout"
501 "*Default file name for \\[rmail-output-body-to-file]."
502 :type 'file
503 :group 'rmail-files
504 :version "20.3")
506 ;; Mule and MIME related variables.
508 ;;;###autoload
509 (defvar rmail-file-coding-system nil
510 "Coding system used in RMAIL file.
512 This is set to nil by default.")
514 ;;;###autoload
515 (defcustom rmail-enable-mime nil
516 "*If non-nil, RMAIL uses MIME feature.
517 If the value is t, RMAIL automatically shows MIME decoded message.
518 If the value is neither t nor nil, RMAIL does not show MIME decoded message
519 until a user explicitly requires it."
520 :type '(choice (const :tag "on" t)
521 (const :tag "off" nil)
522 (other :tag "when asked" ask))
523 :group 'rmail)
525 (defvar rmail-enable-mime-composing nil
526 "*If non-nil, RMAIL uses `rmail-insert-mime-forwarded-message-function' to forward.")
528 ;;;###autoload
529 (defvar rmail-show-mime-function nil
530 "Function to show MIME decoded message of RMAIL file.
531 This function is called when `rmail-enable-mime' is non-nil.
532 It is called with no argument.")
534 ;;;###autoload
535 (defvar rmail-insert-mime-forwarded-message-function nil
536 "Function to insert a message in MIME format so it can be forwarded.
537 This function is called if `rmail-enable-mime' or
538 `rmail-enable-mime-composing' is non-nil.
539 It is called with one argument FORWARD-BUFFER, which is a
540 buffer containing the message to forward. The current buffer
541 is the outgoing mail buffer.")
543 ;;;###autoload
544 (defvar rmail-insert-mime-resent-message-function nil
545 "Function to insert a message in MIME format so it can be resent.
546 This function is called if `rmail-enable-mime' is non-nil.
547 It is called with one argument FORWARD-BUFFER, which is a
548 buffer containing the message to forward. The current buffer
549 is the outgoing mail buffer.")
551 ;;;###autoload
552 (defvar rmail-search-mime-message-function nil
553 "Function to check if a regexp matches a MIME message.
554 This function is called if `rmail-enable-mime' is non-nil.
555 It is called with two arguments MSG and REGEXP, where
556 MSG is the message number, REGEXP is the regular expression.")
558 ;;;###autoload
559 (defvar rmail-search-mime-header-function nil
560 "Function to check if a regexp matches a header of MIME message.
561 This function is called if `rmail-enable-mime' is non-nil.
562 It is called with three arguments MSG, REGEXP, and LIMIT, where
563 MSG is the message number,
564 REGEXP is the regular expression,
565 LIMIT is the position specifying the end of header.")
567 ;;;###autoload
568 (defvar rmail-mime-feature 'rmail-mime
569 "Feature to require to load MIME support in Rmail.
570 When starting Rmail, if `rmail-enable-mime' is non-nil,
571 this feature is required with `require'.")
573 ;;;###autoload
574 (defvar rmail-decode-mime-charset t
575 "*Non-nil means a message is decoded by MIME's charset specification.
576 If this variable is nil, or the message has not MIME specification,
577 the message is decoded as normal way.
579 If the variable `rmail-enable-mime' is non-nil, this variables is
580 ignored, and all the decoding work is done by a feature specified by
581 the variable `rmail-mime-feature'.")
583 ;;;###autoload
584 (defvar rmail-mime-charset-pattern
585 "^content-type:[ ]*text/plain;[ \t\n]*charset=\"?\\([^ \t\n\";]+\\)\"?"
586 "Regexp to match MIME-charset specification in a header of message.
587 The first parenthesized expression should match the MIME-charset name.")
590 ;;; Regexp matching the delimiter of messages in UNIX mail format
591 ;;; (UNIX From lines), minus the initial ^. Note that if you change
592 ;;; this expression, you must change the code in rmail-nuke-pinhead-header
593 ;;; that knows the exact ordering of the \\( \\) subexpressions.
594 (defvar rmail-unix-mail-delimiter
595 (let ((time-zone-regexp
596 (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
597 "\\|[-+]?[0-9][0-9][0-9][0-9]"
598 "\\|"
599 "\\) *")))
600 (concat
601 "From "
603 ;; Many things can happen to an RFC 822 mailbox before it is put into
604 ;; a `From' line. The leading phrase can be stripped, e.g.
605 ;; `Joe <@w.x:joe@y.z>' -> `<@w.x:joe@y.z>'. The <> can be stripped, e.g.
606 ;; `<@x.y:joe@y.z>' -> `@x.y:joe@y.z'. Everything starting with a CRLF
607 ;; can be removed, e.g.
608 ;; From: joe@y.z (Joe K
609 ;; User)
610 ;; can yield `From joe@y.z (Joe K Fri Mar 22 08:11:15 1996', and
611 ;; From: Joe User
612 ;; <joe@y.z>
613 ;; can yield `From Joe User Fri Mar 22 08:11:15 1996'.
614 ;; The mailbox can be removed or be replaced by white space, e.g.
615 ;; From: "Joe User"{space}{tab}
616 ;; <joe@y.z>
617 ;; can yield `From {space}{tab} Fri Mar 22 08:11:15 1996',
618 ;; where {space} and {tab} represent the Ascii space and tab characters.
619 ;; We want to match the results of any of these manglings.
620 ;; The following regexp rejects names whose first characters are
621 ;; obviously bogus, but after that anything goes.
622 "\\([^\0-\b\n-\r\^?].*\\)? "
624 ;; The time the message was sent.
625 "\\([^\0-\r \^?]+\\) +" ; day of the week
626 "\\([^\0-\r \^?]+\\) +" ; month
627 "\\([0-3]?[0-9]\\) +" ; day of month
628 "\\([0-2][0-9]:[0-5][0-9]\\(:[0-6][0-9]\\)?\\) *" ; time of day
630 ;; Perhaps a time zone, specified by an abbreviation, or by a
631 ;; numeric offset.
632 time-zone-regexp
634 ;; The year.
635 " \\([0-9][0-9]+\\) *"
637 ;; On some systems the time zone can appear after the year, too.
638 time-zone-regexp
640 ;; Old uucp cruft.
641 "\\(remote from .*\\)?"
643 "\n"))
644 nil)
646 (defvar rmail-font-lock-keywords
647 ;; These are all matched case-insensitively.
648 (eval-when-compile
649 (let* ((cite-chars "[>|}]")
650 (cite-prefix "a-z")
651 (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
652 (list '("^\\(From\\|Sender\\|Resent-From\\):"
653 . font-lock-function-name-face)
654 '("^Reply-To:.*$" . font-lock-function-name-face)
655 '("^Subject:" . font-lock-comment-face)
656 '("^\\(To\\|Apparently-To\\|Cc\\|Newsgroups\\):"
657 . font-lock-keyword-face)
658 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
659 `(,cite-chars
660 (,(concat "\\=[ \t]*"
661 "\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
662 "\\(" cite-chars "[ \t]*\\)\\)+"
663 "\\(.*\\)")
664 (beginning-of-line) (end-of-line)
665 (2 font-lock-constant-face nil t)
666 (4 font-lock-comment-face nil t)))
667 '("^\\(X-[a-z0-9-]+\\|In-reply-to\\|Date\\):.*\\(\n[ \t]+.*\\)*$"
668 . font-lock-string-face))))
669 "Additional expressions to highlight in Rmail mode.")
671 ;; Perform BODY in the summary buffer
672 ;; in such a way that its cursor is properly updated in its own window.
673 (defmacro rmail-select-summary (&rest body)
674 `(let ((total rmail-total-messages))
675 (if (rmail-summary-displayed)
676 (let ((window (selected-window)))
677 (save-excursion
678 (unwind-protect
679 (progn
680 (pop-to-buffer rmail-summary-buffer)
681 ;; rmail-total-messages is a buffer-local var
682 ;; in the rmail buffer.
683 ;; This way we make it available for the body
684 ;; even tho the rmail buffer is not current.
685 (let ((rmail-total-messages total))
686 ,@body))
687 (select-window window))))
688 (save-excursion
689 (set-buffer rmail-summary-buffer)
690 (let ((rmail-total-messages total))
691 ,@body)))
692 (rmail-maybe-display-summary)))
694 ;;;; *** Rmail Mode ***
696 ;; This variable is dynamically bound. The defvar is here to placate
697 ;; the byte compiler.
699 (defvar rmail-enable-multibyte nil)
702 (defun rmail-require-mime-maybe ()
703 "Require `rmail-mime-feature' if that is non-nil.
704 Signal an error and set `rmail-mime-feature' to nil if the feature
705 isn't provided."
706 (when rmail-enable-mime
707 (condition-case err
708 (require rmail-mime-feature)
709 (error
710 (message "Feature `%s' not provided" rmail-mime-feature)
711 (sit-for 1)
712 (setq rmail-enable-mime nil)))))
715 ;;;###autoload
716 (defun rmail (&optional file-name-arg)
717 "Read and edit incoming mail.
718 Moves messages into file named by `rmail-file-name' (a babyl format file)
719 and edits that file in RMAIL Mode.
720 Type \\[describe-mode] once editing that file, for a list of RMAIL commands.
722 May be called with file name as argument; then performs rmail editing on
723 that file, but does not copy any new mail into the file.
724 Interactively, if you supply a prefix argument, then you
725 have a chance to specify a file name with the minibuffer.
727 If `rmail-display-summary' is non-nil, make a summary for this RMAIL file."
728 (interactive (if current-prefix-arg
729 (list (read-file-name "Run rmail on RMAIL file: "))))
730 (rmail-require-mime-maybe)
731 (let* ((file-name (expand-file-name (or file-name-arg rmail-file-name)))
732 ;; Use find-buffer-visiting, not get-file-buffer, for those users
733 ;; who have find-file-visit-truename set to t.
734 (existed (find-buffer-visiting file-name))
735 ;; This binding is necessary because we must decide if we
736 ;; need code conversion while the buffer is unibyte
737 ;; (i.e. enable-multibyte-characters is nil).
738 (rmail-enable-multibyte
739 (if existed
740 (with-current-buffer existed enable-multibyte-characters)
741 (default-value 'enable-multibyte-characters)))
742 ;; Since the file may contain messages of different encodings
743 ;; at the tail (non-BYBYL part), we can't decode them at once
744 ;; on reading. So, at first, we read the file without text
745 ;; code conversion, then decode the messages one by one by
746 ;; rmail-decode-babyl-format or
747 ;; rmail-convert-to-babyl-format.
748 (coding-system-for-read (and rmail-enable-multibyte 'raw-text))
749 run-mail-hook msg-shown)
750 ;; Like find-file, but in the case where a buffer existed
751 ;; and the file was reverted, recompute the message-data.
752 ;; We used to bind enable-local-variables to nil here,
753 ;; but that should not be needed now that rmail-mode
754 ;; sets it locally to nil.
755 ;; (Binding a variable locally with let is not safe if it has
756 ;; buffer-local bindings.)
757 (if (and existed (not (verify-visited-file-modtime existed)))
758 (progn
759 (find-file file-name)
760 (if (and (verify-visited-file-modtime existed)
761 (eq major-mode 'rmail-mode))
762 (progn (rmail-forget-messages)
763 (rmail-set-message-counters))))
764 (switch-to-buffer
765 (let ((enable-local-variables nil))
766 (find-file-noselect file-name))))
767 (if (eq major-mode 'rmail-edit-mode)
768 (error "Exit Rmail Edit mode before getting new mail"))
769 (if (and existed (> (buffer-size) 0))
770 ;; Buffer not new and not empty; ensure in proper mode, but that's all.
771 (or (eq major-mode 'rmail-mode)
772 (progn (rmail-mode-2)
773 (setq run-mail-hook t)))
774 (setq run-mail-hook t)
775 (rmail-mode-2)
776 ;; Convert all or part to Babyl file if possible.
777 (rmail-convert-file)
778 (goto-char (point-max)))
779 ;; As we have read a file by raw-text, the buffer is set to
780 ;; unibyte. We must make it multibyte if necessary.
781 (if (and rmail-enable-multibyte
782 (not enable-multibyte-characters))
783 (set-buffer-multibyte t))
784 ;; If necessary, scan to find all the messages.
785 (rmail-maybe-set-message-counters)
786 (unwind-protect
787 (unless (and (not file-name-arg)
788 (rmail-get-new-mail))
789 (rmail-show-message (rmail-first-unseen-message)))
790 (progn
791 (if rmail-display-summary (rmail-summary))
792 (rmail-construct-io-menu)
793 (if run-mail-hook
794 (run-hooks 'rmail-mode-hook))))))
796 ;; Given the value of MAILPATH, return a list of inbox file names.
797 ;; This is turned off because it is not clear that the user wants
798 ;; all these inboxes to feed into the primary rmail file.
799 ; (defun rmail-convert-mailpath (string)
800 ; (let (idx list)
801 ; (while (setq idx (string-match "[%:]" string))
802 ; (let ((this (substring string 0 idx)))
803 ; (setq string (substring string (1+ idx)))
804 ; (setq list (cons (if (string-match "%" this)
805 ; (substring this 0 (string-match "%" this))
806 ; this)
807 ; list))))
808 ; list))
810 ; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
811 ; will not cause emacs 18.55 problems.
813 ;; This calls rmail-decode-babyl-format if the file is already Babyl.
815 (defun rmail-convert-file ()
816 (let (convert)
817 (widen)
818 (goto-char (point-min))
819 ;; If file doesn't start like a Babyl file,
820 ;; convert it to one, by adding a header and converting each message.
821 (cond ((looking-at "BABYL OPTIONS:"))
822 ((looking-at "Version: 5\n")
823 ;; Losing babyl file made by old version of Rmail.
824 ;; Just fix the babyl file header; don't make a new one,
825 ;; so we don't lose the Labels: file attribute, etc.
826 (let ((buffer-read-only nil))
827 (insert "BABYL OPTIONS: -*- rmail -*-\n")))
828 ((equal (point-min) (point-max))
829 ;; Empty RMAIL file. Just insert the header.
830 (rmail-insert-rmail-file-header))
832 ;; Non-empty file in non-RMAIL format. Add header and convert.
833 (setq convert t)
834 (rmail-insert-rmail-file-header)))
835 ;; If file was not a Babyl file or if there are
836 ;; Unix format messages added at the end,
837 ;; convert file as necessary.
838 (if (or convert
839 (save-excursion
840 (goto-char (point-max))
841 (search-backward "\n\^_")
842 (forward-char 2)
843 (looking-at "\n*From ")))
844 (let ((buffer-read-only nil))
845 (message "Converting to Babyl format...")
846 ;; If file needs conversion, convert it all,
847 ;; except for the BABYL header.
848 ;; (rmail-convert-to-babyl-format would delete the header.)
849 (goto-char (point-min))
850 (search-forward "\n\^_" nil t)
851 (narrow-to-region (point) (point-max))
852 (rmail-convert-to-babyl-format)
853 (message "Converting to Babyl format...done"))
854 (if (and (not rmail-enable-mime)
855 rmail-enable-multibyte)
856 ;; We still have to decode BABYL part.
857 (rmail-decode-babyl-format)))))
859 (defun rmail-insert-rmail-file-header ()
860 (let ((buffer-read-only nil))
861 ;; -*-rmail-*- is here so that visiting the file normally
862 ;; recognizes it as an Rmail file.
863 (insert "BABYL OPTIONS: -*- rmail -*-
864 Version: 5
865 Labels:
866 Note: This is the header of an rmail file.
867 Note: If you are seeing it in rmail,
868 Note: it means the file has no messages in it.\n\^_")))
870 ;; Decode Babyl formatted part at the head of current buffer by
871 ;; rmail-file-coding-system, or if it is nil, do auto conversion.
873 (defun rmail-decode-babyl-format ()
874 (let ((modifiedp (buffer-modified-p))
875 (buffer-read-only nil)
876 (coding-system rmail-file-coding-system)
877 from to)
878 (goto-char (point-min))
879 (search-forward "\n\^_" nil t) ; Skip BABYL header.
880 (setq from (point))
881 (goto-char (point-max))
882 (search-backward "\n\^_" from 'mv)
883 (setq to (point))
884 (unless (and coding-system
885 (coding-system-p coding-system))
886 (setq coding-system
887 ;; Emacs 21.1 and later writes RMAIL files in emacs-mule, but
888 ;; earlier versions did that with the current buffer's encoding.
889 ;; So we want to favor detection of emacs-mule (whose normal
890 ;; priority is quite low), but still allow detection of other
891 ;; encodings if emacs-mule won't fit. The call to
892 ;; detect-coding-with-priority below achieves that.
893 (car (detect-coding-with-priority
894 from to
895 '((coding-category-emacs-mule . emacs-mule))))))
896 (unless (memq coding-system
897 '(undecided undecided-unix))
898 (set-buffer-modified-p t) ; avoid locking when decoding
899 (let ((buffer-undo-list t))
900 (decode-coding-region from to coding-system))
901 (setq coding-system last-coding-system-used))
902 (set-buffer-modified-p modifiedp)
903 (setq buffer-file-coding-system nil)
904 (setq save-buffer-coding-system
905 (or coding-system 'undecided))))
907 (defvar rmail-mode-map nil)
908 (if rmail-mode-map
910 (setq rmail-mode-map (make-keymap))
911 (suppress-keymap rmail-mode-map)
912 (define-key rmail-mode-map "a" 'rmail-add-label)
913 (define-key rmail-mode-map "b" 'rmail-bury)
914 (define-key rmail-mode-map "c" 'rmail-continue)
915 (define-key rmail-mode-map "d" 'rmail-delete-forward)
916 (define-key rmail-mode-map "\C-d" 'rmail-delete-backward)
917 (define-key rmail-mode-map "e" 'rmail-edit-current-message)
918 (define-key rmail-mode-map "f" 'rmail-forward)
919 (define-key rmail-mode-map "g" 'rmail-get-new-mail)
920 (define-key rmail-mode-map "h" 'rmail-summary)
921 (define-key rmail-mode-map "i" 'rmail-input)
922 (define-key rmail-mode-map "j" 'rmail-show-message)
923 (define-key rmail-mode-map "k" 'rmail-kill-label)
924 (define-key rmail-mode-map "l" 'rmail-summary-by-labels)
925 (define-key rmail-mode-map "\e\C-h" 'rmail-summary)
926 (define-key rmail-mode-map "\e\C-l" 'rmail-summary-by-labels)
927 (define-key rmail-mode-map "\e\C-r" 'rmail-summary-by-recipients)
928 (define-key rmail-mode-map "\e\C-s" 'rmail-summary-by-regexp)
929 (define-key rmail-mode-map "\e\C-t" 'rmail-summary-by-topic)
930 (define-key rmail-mode-map "m" 'rmail-mail)
931 (define-key rmail-mode-map "\em" 'rmail-retry-failure)
932 (define-key rmail-mode-map "n" 'rmail-next-undeleted-message)
933 (define-key rmail-mode-map "\en" 'rmail-next-message)
934 (define-key rmail-mode-map "\e\C-n" 'rmail-next-labeled-message)
935 (define-key rmail-mode-map "o" 'rmail-output-to-rmail-file)
936 (define-key rmail-mode-map "\C-o" 'rmail-output)
937 (define-key rmail-mode-map "p" 'rmail-previous-undeleted-message)
938 (define-key rmail-mode-map "\ep" 'rmail-previous-message)
939 (define-key rmail-mode-map "\e\C-p" 'rmail-previous-labeled-message)
940 (define-key rmail-mode-map "q" 'rmail-quit)
941 (define-key rmail-mode-map "r" 'rmail-reply)
942 ;; I find I can't live without the default M-r command -- rms.
943 ;; (define-key rmail-mode-map "\er" 'rmail-search-backwards)
944 (define-key rmail-mode-map "s" 'rmail-expunge-and-save)
945 (define-key rmail-mode-map "\es" 'rmail-search)
946 (define-key rmail-mode-map "t" 'rmail-toggle-header)
947 (define-key rmail-mode-map "u" 'rmail-undelete-previous-message)
948 (define-key rmail-mode-map "w" 'rmail-output-body-to-file)
949 (define-key rmail-mode-map "x" 'rmail-expunge)
950 (define-key rmail-mode-map "." 'rmail-beginning-of-message)
951 (define-key rmail-mode-map "<" 'rmail-first-message)
952 (define-key rmail-mode-map ">" 'rmail-last-message)
953 (define-key rmail-mode-map " " 'scroll-up)
954 (define-key rmail-mode-map "\177" 'scroll-down)
955 (define-key rmail-mode-map "?" 'describe-mode)
956 (define-key rmail-mode-map "\C-c\C-s\C-d" 'rmail-sort-by-date)
957 (define-key rmail-mode-map "\C-c\C-s\C-s" 'rmail-sort-by-subject)
958 (define-key rmail-mode-map "\C-c\C-s\C-a" 'rmail-sort-by-author)
959 (define-key rmail-mode-map "\C-c\C-s\C-r" 'rmail-sort-by-recipient)
960 (define-key rmail-mode-map "\C-c\C-s\C-c" 'rmail-sort-by-correspondent)
961 (define-key rmail-mode-map "\C-c\C-s\C-l" 'rmail-sort-by-lines)
962 (define-key rmail-mode-map "\C-c\C-s\C-k" 'rmail-sort-by-labels)
963 (define-key rmail-mode-map "\C-c\C-n" 'rmail-next-same-subject)
964 (define-key rmail-mode-map "\C-c\C-p" 'rmail-previous-same-subject)
967 (define-key rmail-mode-map [menu-bar] (make-sparse-keymap))
969 (define-key rmail-mode-map [menu-bar classify]
970 (cons "Classify" (make-sparse-keymap "Classify")))
972 (define-key rmail-mode-map [menu-bar classify input-menu]
973 nil)
975 (define-key rmail-mode-map [menu-bar classify output-menu]
976 nil)
978 (define-key rmail-mode-map [menu-bar classify output-body]
979 '("Output body to file..." . rmail-output-body-to-file))
981 (define-key rmail-mode-map [menu-bar classify output-inbox]
982 '("Output (inbox)..." . rmail-output))
984 (define-key rmail-mode-map [menu-bar classify output]
985 '("Output (Rmail)..." . rmail-output-to-rmail-file))
987 (define-key rmail-mode-map [menu-bar classify kill-label]
988 '("Kill Label..." . rmail-kill-label))
990 (define-key rmail-mode-map [menu-bar classify add-label]
991 '("Add Label..." . rmail-add-label))
993 (define-key rmail-mode-map [menu-bar summary]
994 (cons "Summary" (make-sparse-keymap "Summary")))
996 (define-key rmail-mode-map [menu-bar summary senders]
997 '("By Senders..." . rmail-summary-by-senders))
999 (define-key rmail-mode-map [menu-bar summary labels]
1000 '("By Labels..." . rmail-summary-by-labels))
1002 (define-key rmail-mode-map [menu-bar summary recipients]
1003 '("By Recipients..." . rmail-summary-by-recipients))
1005 (define-key rmail-mode-map [menu-bar summary topic]
1006 '("By Topic..." . rmail-summary-by-topic))
1008 (define-key rmail-mode-map [menu-bar summary regexp]
1009 '("By Regexp..." . rmail-summary-by-regexp))
1011 (define-key rmail-mode-map [menu-bar summary all]
1012 '("All" . rmail-summary))
1014 (define-key rmail-mode-map [menu-bar mail]
1015 (cons "Mail" (make-sparse-keymap "Mail")))
1017 (define-key rmail-mode-map [menu-bar mail rmail-get-new-mail]
1018 '("Get New Mail" . rmail-get-new-mail))
1020 (define-key rmail-mode-map [menu-bar mail lambda]
1021 '("----"))
1023 (define-key rmail-mode-map [menu-bar mail continue]
1024 '("Continue" . rmail-continue))
1026 (define-key rmail-mode-map [menu-bar mail resend]
1027 '("Re-send..." . rmail-resend))
1029 (define-key rmail-mode-map [menu-bar mail forward]
1030 '("Forward" . rmail-forward))
1032 (define-key rmail-mode-map [menu-bar mail retry]
1033 '("Retry" . rmail-retry-failure))
1035 (define-key rmail-mode-map [menu-bar mail reply]
1036 '("Reply" . rmail-reply))
1038 (define-key rmail-mode-map [menu-bar mail mail]
1039 '("Mail" . rmail-mail))
1041 (define-key rmail-mode-map [menu-bar delete]
1042 (cons "Delete" (make-sparse-keymap "Delete")))
1044 (define-key rmail-mode-map [menu-bar delete expunge/save]
1045 '("Expunge/Save" . rmail-expunge-and-save))
1047 (define-key rmail-mode-map [menu-bar delete expunge]
1048 '("Expunge" . rmail-expunge))
1050 (define-key rmail-mode-map [menu-bar delete undelete]
1051 '("Undelete" . rmail-undelete-previous-message))
1053 (define-key rmail-mode-map [menu-bar delete delete]
1054 '("Delete" . rmail-delete-forward))
1056 (define-key rmail-mode-map [menu-bar move]
1057 (cons "Move" (make-sparse-keymap "Move")))
1059 (define-key rmail-mode-map [menu-bar move search-back]
1060 '("Search Back..." . rmail-search-backwards))
1062 (define-key rmail-mode-map [menu-bar move search]
1063 '("Search..." . rmail-search))
1065 (define-key rmail-mode-map [menu-bar move previous]
1066 '("Previous Nondeleted" . rmail-previous-undeleted-message))
1068 (define-key rmail-mode-map [menu-bar move next]
1069 '("Next Nondeleted" . rmail-next-undeleted-message))
1071 (define-key rmail-mode-map [menu-bar move last]
1072 '("Last" . rmail-last-message))
1074 (define-key rmail-mode-map [menu-bar move first]
1075 '("First" . rmail-first-message))
1077 (define-key rmail-mode-map [menu-bar move previous]
1078 '("Previous" . rmail-previous-message))
1080 (define-key rmail-mode-map [menu-bar move next]
1081 '("Next" . rmail-next-message))
1083 ;; Rmail mode is suitable only for specially formatted data.
1084 (put 'rmail-mode 'mode-class 'special)
1086 (defun rmail-mode-kill-summary ()
1087 (if rmail-summary-buffer (kill-buffer rmail-summary-buffer)))
1089 ;;;###autoload
1090 (defun rmail-mode ()
1091 "Rmail Mode is used by \\<rmail-mode-map>\\[rmail] for editing Rmail files.
1092 All normal editing commands are turned off.
1093 Instead, these commands are available:
1095 \\[rmail-beginning-of-message] Move point to front of this message (same as \\[beginning-of-buffer]).
1096 \\[scroll-up] Scroll to next screen of this message.
1097 \\[scroll-down] Scroll to previous screen of this message.
1098 \\[rmail-next-undeleted-message] Move to Next non-deleted message.
1099 \\[rmail-previous-undeleted-message] Move to Previous non-deleted message.
1100 \\[rmail-next-message] Move to Next message whether deleted or not.
1101 \\[rmail-previous-message] Move to Previous message whether deleted or not.
1102 \\[rmail-first-message] Move to the first message in Rmail file.
1103 \\[rmail-last-message] Move to the last message in Rmail file.
1104 \\[rmail-show-message] Jump to message specified by numeric position in file.
1105 \\[rmail-search] Search for string and show message it is found in.
1106 \\[rmail-delete-forward] Delete this message, move to next nondeleted.
1107 \\[rmail-delete-backward] Delete this message, move to previous nondeleted.
1108 \\[rmail-undelete-previous-message] Undelete message. Tries current message, then earlier messages
1109 till a deleted message is found.
1110 \\[rmail-edit-current-message] Edit the current message. \\[rmail-cease-edit] to return to Rmail.
1111 \\[rmail-expunge] Expunge deleted messages.
1112 \\[rmail-expunge-and-save] Expunge and save the file.
1113 \\[rmail-quit] Quit Rmail: expunge, save, then switch to another buffer.
1114 \\[save-buffer] Save without expunging.
1115 \\[rmail-get-new-mail] Move new mail from system spool directory into this file.
1116 \\[rmail-mail] Mail a message (same as \\[mail-other-window]).
1117 \\[rmail-continue] Continue composing outgoing message started before.
1118 \\[rmail-reply] Reply to this message. Like \\[rmail-mail] but initializes some fields.
1119 \\[rmail-retry-failure] Send this message again. Used on a mailer failure message.
1120 \\[rmail-forward] Forward this message to another user.
1121 \\[rmail-output-to-rmail-file] Output this message to an Rmail file (append it).
1122 \\[rmail-output] Output this message to a Unix-format mail file (append it).
1123 \\[rmail-output-body-to-file] Save message body to a file. Default filename comes from Subject line.
1124 \\[rmail-input] Input Rmail file. Run Rmail on that file.
1125 \\[rmail-add-label] Add label to message. It will be displayed in the mode line.
1126 \\[rmail-kill-label] Kill label. Remove a label from current message.
1127 \\[rmail-next-labeled-message] Move to Next message with specified label
1128 (label defaults to last one specified).
1129 Standard labels: filed, unseen, answered, forwarded, deleted.
1130 Any other label is present only if you add it with \\[rmail-add-label].
1131 \\[rmail-previous-labeled-message] Move to Previous message with specified label
1132 \\[rmail-summary] Show headers buffer, with a one line summary of each message.
1133 \\[rmail-summary-by-labels] Summarize only messages with particular label(s).
1134 \\[rmail-summary-by-recipients] Summarize only messages with particular recipient(s).
1135 \\[rmail-summary-by-regexp] Summarize only messages with particular regexp(s).
1136 \\[rmail-summary-by-topic] Summarize only messages with subject line regexp(s).
1137 \\[rmail-toggle-header] Toggle display of complete header."
1138 (interactive)
1139 (let ((finding-rmail-file (not (eq major-mode 'rmail-mode))))
1140 (rmail-mode-2)
1141 (when (and finding-rmail-file
1142 (null coding-system-for-read)
1143 default-enable-multibyte-characters)
1144 (let ((rmail-enable-multibyte t))
1145 (rmail-require-mime-maybe)
1146 (rmail-convert-file)
1147 (goto-char (point-max))
1148 (set-buffer-multibyte t)))
1149 (rmail-set-message-counters)
1150 (rmail-show-message rmail-total-messages)
1151 (when finding-rmail-file
1152 (when rmail-display-summary
1153 (rmail-summary))
1154 (rmail-construct-io-menu))
1155 (run-hooks 'rmail-mode-hook)))
1157 (defun rmail-mode-2 ()
1158 (kill-all-local-variables)
1159 (rmail-mode-1)
1160 (rmail-perm-variables)
1161 (rmail-variables))
1163 (defun rmail-mode-1 ()
1164 (setq major-mode 'rmail-mode)
1165 (setq mode-name "RMAIL")
1166 (setq buffer-read-only t)
1167 ;; No need to auto save RMAIL files in normal circumstances
1168 ;; because they contain no info except attribute changes
1169 ;; and deletion of messages.
1170 ;; The one exception is when messages are copied into an Rmail mode buffer.
1171 ;; rmail-output-to-rmail-file enables auto save when you do that.
1172 (setq buffer-auto-save-file-name nil)
1173 (setq mode-line-modified "--")
1174 (use-local-map rmail-mode-map)
1175 (set-syntax-table text-mode-syntax-table)
1176 (setq local-abbrev-table text-mode-abbrev-table))
1178 ;; Set up the permanent locals associated with an Rmail file.
1179 (defun rmail-perm-variables ()
1180 (make-local-variable 'rmail-last-label)
1181 (make-local-variable 'rmail-last-regexp)
1182 (make-local-variable 'rmail-deleted-vector)
1183 (make-local-variable 'rmail-buffer)
1184 (setq rmail-buffer (current-buffer))
1185 (make-local-variable 'rmail-view-buffer)
1186 (setq rmail-view-buffer rmail-buffer)
1187 (make-local-variable 'rmail-summary-buffer)
1188 (make-local-variable 'rmail-summary-vector)
1189 (make-local-variable 'rmail-current-message)
1190 (make-local-variable 'rmail-total-messages)
1191 (make-local-variable 'rmail-overlay-list)
1192 (setq rmail-overlay-list nil)
1193 (make-local-variable 'rmail-message-vector)
1194 (make-local-variable 'rmail-msgref-vector)
1195 (make-local-variable 'rmail-inbox-list)
1196 (setq rmail-inbox-list (rmail-parse-file-inboxes))
1197 ;; Provide default set of inboxes for primary mail file ~/RMAIL.
1198 (and (null rmail-inbox-list)
1199 (or (equal buffer-file-name (expand-file-name rmail-file-name))
1200 (equal buffer-file-truename
1201 (abbreviate-file-name (file-truename rmail-file-name))))
1202 (setq rmail-inbox-list
1203 (or rmail-primary-inbox-list
1204 (list (or (getenv "MAIL")
1205 (concat rmail-spool-directory
1206 (user-login-name)))))))
1207 (make-local-variable 'rmail-keywords)
1208 ;; this gets generated as needed
1209 (setq rmail-keywords nil))
1211 ;; Set up the non-permanent locals associated with Rmail mode.
1212 (defun rmail-variables ()
1213 (make-local-variable 'save-buffer-coding-system)
1214 ;; If we don't already have a value for save-buffer-coding-system,
1215 ;; get it from buffer-file-coding-system, and clear that
1216 ;; because it should be determined in rmail-show-message.
1217 (unless save-buffer-coding-system
1218 (setq save-buffer-coding-system (or buffer-file-coding-system 'undecided))
1219 (setq buffer-file-coding-system nil))
1220 ;; Don't let a local variables list in a message cause confusion.
1221 (make-local-variable 'local-enable-local-variables)
1222 (setq local-enable-local-variables nil)
1223 (make-local-variable 'revert-buffer-function)
1224 (setq revert-buffer-function 'rmail-revert)
1225 (make-local-variable 'font-lock-defaults)
1226 (setq font-lock-defaults
1227 '(rmail-font-lock-keywords
1228 t t nil nil
1229 (font-lock-maximum-size . nil)
1230 (font-lock-fontify-buffer-function . rmail-fontify-buffer-function)
1231 (font-lock-unfontify-buffer-function . rmail-unfontify-buffer-function)
1232 (font-lock-inhibit-thing-lock . (lazy-lock-mode fast-lock-mode))))
1233 (make-local-variable 'require-final-newline)
1234 (setq require-final-newline nil)
1235 (make-local-variable 'version-control)
1236 (setq version-control 'never)
1237 (make-local-variable 'kill-buffer-hook)
1238 (add-hook 'kill-buffer-hook 'rmail-mode-kill-summary)
1239 (make-local-variable 'file-precious-flag)
1240 (setq file-precious-flag t)
1241 (make-local-variable 'desktop-save-buffer)
1242 (setq desktop-save-buffer t))
1244 ;; Handle M-x revert-buffer done in an rmail-mode buffer.
1245 (defun rmail-revert (arg noconfirm)
1246 (set-buffer rmail-buffer)
1247 (let* ((revert-buffer-function (default-value 'revert-buffer-function))
1248 (rmail-enable-multibyte enable-multibyte-characters)
1249 ;; See similar code in `rmail'.
1250 (coding-system-for-read (and rmail-enable-multibyte 'raw-text)))
1251 ;; Call our caller again, but this time it does the default thing.
1252 (if (revert-buffer arg noconfirm)
1253 ;; If the user said "yes", and we changed something,
1254 ;; reparse the messages.
1255 (progn
1256 (set-buffer rmail-buffer)
1257 (rmail-mode-2)
1258 ;; Convert all or part to Babyl file if possible.
1259 (rmail-convert-file)
1260 ;; We have read the file as raw-text, so the buffer is set to
1261 ;; unibyte. Make it multibyte if necessary.
1262 (if (and rmail-enable-multibyte
1263 (not enable-multibyte-characters))
1264 (set-buffer-multibyte t))
1265 (goto-char (point-max))
1266 (rmail-set-message-counters)
1267 (rmail-show-message rmail-total-messages)
1268 (run-hooks 'rmail-mode-hook)))))
1270 ;; Return a list of files from this buffer's Mail: option.
1271 ;; Does not assume that messages have been parsed.
1272 ;; Just returns nil if buffer does not look like Babyl format.
1273 (defun rmail-parse-file-inboxes ()
1274 (save-excursion
1275 (save-restriction
1276 (widen)
1277 (goto-char 1)
1278 (cond ((looking-at "BABYL OPTIONS:")
1279 (search-forward "\n\^_" nil 'move)
1280 (narrow-to-region 1 (point))
1281 (goto-char 1)
1282 (if (search-forward "\nMail:" nil t)
1283 (progn
1284 (narrow-to-region (point) (progn (end-of-line) (point)))
1285 (goto-char (point-min))
1286 (mail-parse-comma-list))))))))
1288 (defun rmail-expunge-and-save ()
1289 "Expunge and save RMAIL file."
1290 (interactive)
1291 (rmail-expunge)
1292 (set-buffer rmail-buffer)
1293 (save-buffer)
1294 (if (rmail-summary-exists)
1295 (rmail-select-summary (set-buffer-modified-p nil))))
1297 (defun rmail-quit ()
1298 "Quit out of RMAIL.
1299 Hook `rmail-quit-hook' is run after expunging."
1300 (interactive)
1301 (rmail-expunge-and-save)
1302 (when (boundp 'rmail-quit-hook)
1303 (run-hooks 'rmail-quit-hook))
1304 ;; Don't switch to the summary buffer even if it was recently visible.
1305 (when rmail-summary-buffer
1306 (replace-buffer-in-windows rmail-summary-buffer)
1307 (bury-buffer rmail-summary-buffer))
1308 (if rmail-enable-mime
1309 (let ((obuf rmail-buffer)
1310 (ovbuf rmail-view-buffer))
1311 (set-buffer rmail-view-buffer)
1312 (quit-window)
1313 (replace-buffer-in-windows ovbuf)
1314 (replace-buffer-in-windows obuf)
1315 (bury-buffer obuf))
1316 (let ((obuf (current-buffer)))
1317 (quit-window)
1318 (replace-buffer-in-windows obuf))))
1320 (defun rmail-bury ()
1321 "Bury current Rmail buffer and its summary buffer."
1322 (interactive)
1323 ;; This let var was called rmail-buffer, but that interfered
1324 ;; with the buffer-local var used in summary buffers.
1325 (let ((buffer-to-bury (current-buffer)))
1326 (if (rmail-summary-exists)
1327 (let (window)
1328 (while (setq window (get-buffer-window rmail-summary-buffer))
1329 (quit-window nil window))
1330 (bury-buffer rmail-summary-buffer)))
1331 (quit-window)))
1333 (defun rmail-duplicate-message ()
1334 "Create a duplicated copy of the current message.
1335 The duplicate copy goes into the Rmail file just after the
1336 original copy."
1337 (interactive)
1338 (widen)
1339 (let ((buffer-read-only nil)
1340 (number rmail-current-message)
1341 (string (buffer-substring (rmail-msgbeg rmail-current-message)
1342 (rmail-msgend rmail-current-message))))
1343 (goto-char (rmail-msgend rmail-current-message))
1344 (insert string)
1345 (rmail-forget-messages)
1346 (rmail-show-message number)
1347 (message "Message duplicated")))
1349 ;;;###autoload
1350 (defun rmail-input (filename)
1351 "Run Rmail on file FILENAME."
1352 (interactive "FRun rmail on RMAIL file: ")
1353 (rmail filename))
1356 ;; This used to scan subdirectories recursively, but someone pointed out
1357 ;; that if the user wants that, person can put all the files in one dir.
1358 ;; And the recursive scan was slow. So I took it out.
1359 ;; rms, Sep 1996.
1360 (defun rmail-find-all-files (start)
1361 "Return list of file in dir START that match `rmail-secondary-file-regexp'."
1362 (if (file-accessible-directory-p start)
1363 ;; Don't sort here.
1364 (let* ((case-fold-search t)
1365 (files (directory-files start t rmail-secondary-file-regexp)))
1366 ;; Sort here instead of in directory-files
1367 ;; because this list is usually much shorter.
1368 (sort files 'string<))))
1370 (defun rmail-list-to-menu (menu-name l action &optional full-name)
1371 (let ((menu (make-sparse-keymap menu-name)))
1372 (mapcar
1373 (function (lambda (item)
1374 (let (command)
1375 (if (consp item)
1376 (progn
1377 (setq command
1378 (rmail-list-to-menu (car item) (cdr item)
1379 action
1380 (if full-name
1381 (concat full-name "/"
1382 (car item))
1383 (car item))))
1384 (setq name (car item)))
1385 (progn
1386 (setq name item)
1387 (setq command
1388 (list 'lambda () '(interactive)
1389 (list action
1390 (expand-file-name
1391 (if full-name
1392 (concat full-name "/" item)
1393 item)
1394 rmail-secondary-file-directory))))))
1395 (define-key menu (vector (intern name))
1396 (cons name command)))))
1397 (reverse l))
1398 menu))
1400 ;; This command is always "disabled" when it appears in a menu.
1401 (put 'rmail-disable-menu 'menu-enable ''nil)
1403 (defun rmail-construct-io-menu ()
1404 (let ((files (rmail-find-all-files rmail-secondary-file-directory)))
1405 (if files
1406 (progn
1407 (define-key rmail-mode-map [menu-bar classify input-menu]
1408 (cons "Input Rmail File"
1409 (rmail-list-to-menu "Input Rmail File"
1410 files
1411 'rmail-input)))
1412 (define-key rmail-mode-map [menu-bar classify output-menu]
1413 (cons "Output Rmail File"
1414 (rmail-list-to-menu "Output Rmail File"
1415 files
1416 'rmail-output-to-rmail-file))))
1418 (define-key rmail-mode-map [menu-bar classify input-menu]
1419 '("Input Rmail File" . rmail-disable-menu))
1420 (define-key rmail-mode-map [menu-bar classify output-menu]
1421 '("Output Rmail File" . rmail-disable-menu)))))
1424 ;;;; *** Rmail input ***
1426 ;; RLK feature not added in this version:
1427 ;; argument specifies inbox file or files in various ways.
1429 (defun rmail-get-new-mail (&optional file-name)
1430 "Move any new mail from this RMAIL file's inbox files.
1431 The inbox files can be specified with the file's Mail: option. The
1432 variable `rmail-primary-inbox-list' specifies the inboxes for your
1433 primary RMAIL file if it has no Mail: option. By default, this is
1434 your /usr/spool/mail/$USER.
1436 You can also specify the file to get new mail from. In this case, the
1437 file of new mail is not changed or deleted. Noninteractively, you can
1438 pass the inbox file name as an argument. Interactively, a prefix
1439 argument causes us to read a file name and use that file as the inbox.
1441 If the variable `rmail-preserve-inbox' is non-nil, new mail will
1442 always be left in inbox files rather than deleted.
1444 This function runs `rmail-get-new-mail-hook' before saving the updated file.
1445 It returns t if it got any new messages."
1446 (interactive
1447 (list (if current-prefix-arg
1448 (read-file-name "Get new mail from file: "))))
1449 (run-hooks 'rmail-before-get-new-mail-hook)
1450 ;; If the disk file has been changed from under us,
1451 ;; revert to it before we get new mail.
1452 (or (verify-visited-file-modtime (current-buffer))
1453 (find-file (buffer-file-name)))
1454 (set-buffer rmail-buffer)
1455 (rmail-maybe-set-message-counters)
1456 (widen)
1457 ;; Get rid of all undo records for this buffer.
1458 (or (eq buffer-undo-list t)
1459 (setq buffer-undo-list nil))
1460 (let ((all-files (if file-name (list file-name)
1461 rmail-inbox-list))
1462 (rmail-enable-multibyte (default-value 'enable-multibyte-characters))
1463 found)
1464 (unwind-protect
1465 (progn
1466 (while all-files
1467 (let ((opoint (point))
1468 (new-messages 0)
1469 (rsf-number-of-spam 0)
1470 (delete-files ())
1471 ;; If buffer has not changed yet, and has not been saved yet,
1472 ;; don't replace the old backup file now.
1473 (make-backup-files (and make-backup-files (buffer-modified-p)))
1474 (buffer-read-only nil)
1475 ;; Don't make undo records for what we do in getting mail.
1476 (buffer-undo-list t)
1477 success
1478 ;; Files to insert this time around.
1479 files
1480 ;; Last names of those files.
1481 file-last-names)
1482 ;; Pull files off all-files onto files
1483 ;; as long as there is no name conflict.
1484 ;; A conflict happens when two inbox file names
1485 ;; have the same last component.
1486 (while (and all-files
1487 (not (member (file-name-nondirectory (car all-files))
1488 file-last-names)))
1489 (setq files (cons (car all-files) files)
1490 file-last-names
1491 (cons (file-name-nondirectory (car all-files)) files))
1492 (setq all-files (cdr all-files)))
1493 ;; Put them back in their original order.
1494 (setq files (nreverse files))
1496 (goto-char (point-max))
1497 (skip-chars-backward " \t\n") ; just in case of brain damage
1498 (delete-region (point) (point-max)) ; caused by require-final-newline
1499 (save-excursion
1500 (save-restriction
1501 (narrow-to-region (point) (point))
1502 ;; Read in the contents of the inbox files,
1503 ;; renaming them as necessary,
1504 ;; and adding to the list of files to delete eventually.
1505 (if file-name
1506 (rmail-insert-inbox-text files nil)
1507 (setq delete-files (rmail-insert-inbox-text files t)))
1508 ;; Scan the new text and convert each message to babyl format.
1509 (goto-char (point-min))
1510 (unwind-protect
1511 (save-excursion
1512 (setq new-messages (rmail-convert-to-babyl-format)
1513 success t))
1514 ;; Try to delete the garbage just inserted.
1515 (or success (delete-region (point-min) (point-max)))
1516 ;; If we could not convert the file's inboxes,
1517 ;; rename the files we tried to read
1518 ;; so we won't over and over again.
1519 (if (and (not file-name) (not success))
1520 (let ((delfiles delete-files)
1521 (count 0))
1522 (while delfiles
1523 (while (file-exists-p (format "RMAILOSE.%d" count))
1524 (setq count (1+ count)))
1525 (rename-file (car delfiles)
1526 (format "RMAILOSE.%d" count))
1527 (setq delfiles (cdr delfiles))))))
1528 (or (zerop new-messages)
1529 (let (success)
1530 (widen)
1531 (search-backward "\n\^_" nil t)
1532 (narrow-to-region (point) (point-max))
1533 (goto-char (1+ (point-min)))
1534 (rmail-count-new-messages)
1535 (run-hooks 'rmail-get-new-mail-hook)
1536 (save-buffer)))
1537 ;; Delete the old files, now that babyl file is saved.
1538 (while delete-files
1539 (condition-case ()
1540 ;; First, try deleting.
1541 (condition-case ()
1542 (delete-file (car delete-files))
1543 (file-error
1544 ;; If we can't delete it, truncate it.
1545 (write-region (point) (point) (car delete-files))))
1546 (file-error nil))
1547 (setq delete-files (cdr delete-files)))))
1548 (if (= new-messages 0)
1549 (progn (goto-char opoint)
1550 (if (or file-name rmail-inbox-list)
1551 (message "(No new mail has arrived)")))
1552 ;; check new messages to see if any of them is spam:
1553 (if (and (featurep 'rmail-spam-filter)
1554 rmail-use-spam-filter)
1555 (let*
1556 ((old-messages (- rmail-total-messages new-messages))
1557 (rsf-scanned-message-number (1+ old-messages))
1558 ;; save deletion flags of old messages: vector starts
1559 ;; at zero (is one longer that no of messages),
1560 ;; therefore take 1+ old-messages
1561 (save-deleted
1562 (substring rmail-deleted-vector 0 (1+
1563 old-messages))))
1564 ;; set all messages to undeleted
1565 (setq rmail-deleted-vector
1566 (make-string (1+ rmail-total-messages) ?\ ))
1567 (while (<= rsf-scanned-message-number
1568 rmail-total-messages)
1569 (progn
1570 (if (not (rmail-spam-filter rsf-scanned-message-number))
1571 (progn (setq rsf-number-of-spam (1+ rsf-number-of-spam)))
1573 (setq rsf-scanned-message-number (1+ rsf-scanned-message-number))
1575 (if (> rsf-number-of-spam 0)
1576 (progn
1577 (when (rmail-expunge-confirmed)
1578 (rmail-only-expunge t))
1580 (setq rmail-deleted-vector
1581 (concat
1582 save-deleted
1583 (make-string (- rmail-total-messages old-messages)
1584 ?\ )))
1586 (if (rmail-summary-exists)
1587 (rmail-select-summary
1588 (rmail-update-summary)))
1589 (message "%d new message%s read%s"
1590 new-messages (if (= 1 new-messages) "" "s")
1591 ;; print out a message on number of spam messages found:
1592 (if (and (featurep 'rmail-spam-filter)
1593 rmail-use-spam-filter
1594 (> rsf-number-of-spam 0))
1595 (if (= 1 new-messages)
1596 ", and found to be a spam message"
1597 (if (> rsf-number-of-spam 1)
1598 (format ", %d of which found to be spam messages"
1599 rsf-number-of-spam)
1600 ", one of which found to be a spam message"))
1601 ""))
1602 (if (and (featurep 'rmail-spam-filter)
1603 rmail-use-spam-filter
1604 (> rsf-number-of-spam 0))
1605 (progn (if rsf-beep (beep t))
1606 (sleep-for rsf-sleep-after-message)))
1608 ;; Move to the first new message
1609 ;; unless we have other unseen messages before it.
1610 (rmail-show-message (rmail-first-unseen-message))
1611 (run-hooks 'rmail-after-get-new-mail-hook)
1612 (setq found t))))
1613 found)
1614 ;; Don't leave the buffer screwed up if we get a disk-full error.
1615 (or found (rmail-show-message)))))
1617 (defun rmail-parse-url (file)
1618 "Parse the supplied URL. Return (list MAILBOX-NAME REMOTE PASSWORD GOT-PASSWORD)
1619 WHERE MAILBOX-NAME is the name of the mailbox suitable as argument to the
1620 actual version of `movemail', REMOTE is non-nil if MAILBOX-NAME refers to
1621 a remote mailbox, PASSWORD is the password if it should be
1622 supplied as a separate argument to `movemail' or nil otherwise, GOT-PASSWORD
1623 is non-nil if the user has supplied the password interactively.
1625 (cond
1626 ((string-match "^\\([^:]+\\)://\\(\\([^:@]+\\)\\(:\\([^@]+\\)\\)?@\\)?.*" file)
1627 (let (got-password supplied-password
1628 (proto (match-string 1 file))
1629 (user (match-string 3 file))
1630 (pass (match-string 5 file))
1631 (host (substring file (or (match-end 2)
1632 (+ 3 (match-end 1))))))
1634 (if (not pass)
1635 (when rmail-remote-password-required
1636 (setq got-password (not (rmail-have-password)))
1637 (setq supplied-password (rmail-get-remote-password
1638 (string-equal proto "imap")))))
1640 (if (rmail-movemail-variant-p 'emacs)
1641 (if (string-equal proto "pop")
1642 (list (concat "po:" user ":" host)
1644 (or pass supplied-password)
1645 got-password)
1646 (error "Emacs movemail does not support %s protocol" proto))
1647 (list file
1648 (or (string-equal proto "pop") (string-equal proto "imap"))
1649 supplied-password
1650 got-password))))
1652 ((string-match "^po:\\([^:]+\\)\\(:\\(.*\\)\\)?" file)
1653 (let (got-password supplied-password
1654 (proto "pop")
1655 (user (match-string 1 file))
1656 (host (match-string 3 file)))
1658 (when rmail-remote-password-required
1659 (setq got-password (not (rmail-have-password)))
1660 (setq supplied-password (rmail-get-remote-password nil)))
1662 (list file "pop" supplied-password got-password)))
1665 (list file nil nil nil))))
1667 (defun rmail-insert-inbox-text (files renamep)
1668 ;; Detect a locked file now, so that we avoid moving mail
1669 ;; out of the real inbox file. (That could scare people.)
1670 (or (memq (file-locked-p buffer-file-name) '(nil t))
1671 (error "RMAIL file %s is locked"
1672 (file-name-nondirectory buffer-file-name)))
1673 (let (file tofile delete-files movemail popmail got-password password)
1674 (while files
1675 ;; Handle remote mailbox names specially; don't expand as filenames
1676 ;; in case the userid contains a directory separator.
1677 (setq file (car files))
1678 (let ((url-data (rmail-parse-url file)))
1679 (setq file (nth 0 url-data))
1680 (setq popmail (nth 1 url-data))
1681 (setq password (nth 2 url-data))
1682 (setq got-password (nth 3 url-data)))
1684 (if popmail
1685 (setq renamep t)
1686 (setq file (file-truename
1687 (substitute-in-file-name (expand-file-name file)))))
1688 (setq tofile (expand-file-name
1689 ;; Generate name to move to from inbox name,
1690 ;; in case of multiple inboxes that need moving.
1691 (concat ".newmail-"
1692 (file-name-nondirectory
1693 (if (memq system-type '(windows-nt cygwin))
1694 ;; cannot have "po:" in file name
1695 (substring file 3)
1696 file)))
1697 ;; Use the directory of this rmail file
1698 ;; because it's a nuisance to use the homedir
1699 ;; if that is on a full disk and this rmail
1700 ;; file isn't.
1701 (file-name-directory
1702 (expand-file-name buffer-file-name))))
1703 ;; Always use movemail to rename the file,
1704 ;; since there can be mailboxes in various directories.
1705 (if (not popmail)
1706 (progn
1707 ;; On some systems, /usr/spool/mail/foo is a directory
1708 ;; and the actual inbox is /usr/spool/mail/foo/foo.
1709 (if (file-directory-p file)
1710 (setq file (expand-file-name (user-login-name)
1711 file)))))
1712 (cond (popmail
1713 (message "Getting mail from the remote server ..."))
1714 ((and (file-exists-p tofile)
1715 (/= 0 (nth 7 (file-attributes tofile))))
1716 (message "Getting mail from %s..." tofile))
1717 ((and (file-exists-p file)
1718 (/= 0 (nth 7 (file-attributes file))))
1719 (message "Getting mail from %s..." file)))
1720 ;; Set TOFILE if have not already done so, and
1721 ;; rename or copy the file FILE to TOFILE if and as appropriate.
1722 (cond ((not renamep)
1723 (setq tofile file))
1724 ((or (file-exists-p tofile) (and (not popmail)
1725 (not (file-exists-p file))))
1726 nil)
1728 (with-temp-buffer
1729 (let ((errors (current-buffer)))
1730 (buffer-disable-undo errors)
1731 (let ((args
1732 (append
1733 (list (or rmail-movemail-program
1734 (expand-file-name "movemail"
1735 exec-directory))
1736 nil errors nil)
1737 (if rmail-preserve-inbox
1738 (list "-p")
1739 nil)
1740 (if (rmail-movemail-variant-p 'mailutils)
1741 (append (list "--emacs") rmail-movemail-flags)
1742 rmail-movemail-flags)
1743 (list file tofile)
1744 (if password (list password) nil))))
1745 (apply 'call-process args))
1746 (if (not (buffer-modified-p errors))
1747 ;; No output => movemail won
1749 (set-buffer errors)
1750 (subst-char-in-region (point-min) (point-max)
1751 ?\n ?\ )
1752 (goto-char (point-max))
1753 (skip-chars-backward " \t")
1754 (delete-region (point) (point-max))
1755 (goto-char (point-min))
1756 (if (looking-at "movemail: ")
1757 (delete-region (point-min) (match-end 0)))
1758 (beep t)
1759 ;; If we just read the password, most likely it is
1760 ;; wrong. Otherwise, see if there is a specific
1761 ;; reason to think that the problem is a wrong passwd.
1762 (if (or got-password
1763 (re-search-forward rmail-remote-password-error
1764 nil t))
1765 (rmail-set-remote-password nil))
1767 ;; If using Mailutils, remove initial error code
1768 ;; abbreviation
1769 (when (rmail-movemail-variant-p 'mailutils)
1770 (goto-char (point-min))
1771 (when (looking-at "[A-Z][A-Z0-9_]*:")
1772 (delete-region (point-min) (match-end 0))))
1774 (message "movemail: %s"
1775 (buffer-substring (point-min)
1776 (point-max)))
1778 (sit-for 3)
1779 nil)))))
1781 ;; At this point, TOFILE contains the name to read:
1782 ;; Either the alternate name (if we renamed)
1783 ;; or the actual inbox (if not renaming).
1784 (if (file-exists-p tofile)
1785 (let ((coding-system-for-read 'no-conversion)
1786 size)
1787 (goto-char (point-max))
1788 (setq size (nth 1 (insert-file-contents tofile)))
1789 (goto-char (point-max))
1790 (or (= (preceding-char) ?\n)
1791 (zerop size)
1792 (insert ?\n))
1793 (if (not (and rmail-preserve-inbox (string= file tofile)))
1794 (setq delete-files (cons tofile delete-files)))))
1795 (message "")
1796 (setq files (cdr files)))
1797 delete-files))
1799 ;; Decode the region specified by FROM and TO by CODING.
1800 ;; If CODING is nil or an invalid coding system, decode by `undecided'.
1801 (defun rmail-decode-region (from to coding)
1802 (if (or (not coding) (not (coding-system-p coding)))
1803 (setq coding 'undecided))
1804 ;; Use -dos decoding, to remove ^M characters left from base64 or
1805 ;; rogue qp-encoded text.
1806 (decode-coding-region from to
1807 (coding-system-change-eol-conversion coding 1))
1808 ;; Don't reveal the fact we used -dos decoding, as users generally
1809 ;; will not expect the RMAIL buffer to use DOS EOL format.
1810 (setq buffer-file-coding-system
1811 (setq last-coding-system-used
1812 (coding-system-change-eol-conversion coding 0))))
1814 ;; the rmail-break-forwarded-messages feature is not implemented
1815 (defun rmail-convert-to-babyl-format ()
1816 (let ((count 0) start
1817 (case-fold-search nil)
1818 (invalid-input-resync
1819 (function (lambda ()
1820 (message "Invalid Babyl format in inbox!")
1821 (sit-for 3)
1822 ;; Try to get back in sync with a real message.
1823 (if (re-search-forward
1824 (concat rmail-mmdf-delim1 "\\|^From") nil t)
1825 (beginning-of-line)
1826 (goto-char (point-max)))))))
1827 (goto-char (point-min))
1828 (save-restriction
1829 (while (not (eobp))
1830 (setq start (point))
1831 (cond ((looking-at "BABYL OPTIONS:");Babyl header
1832 (if (search-forward "\n\^_" nil t)
1833 ;; If we find the proper terminator, delete through there.
1834 (delete-region (point-min) (point))
1835 (funcall invalid-input-resync)
1836 (delete-region (point-min) (point))))
1837 ;; Babyl format message
1838 ((looking-at "\^L")
1839 (or (search-forward "\n\^_" nil t)
1840 (funcall invalid-input-resync))
1841 (setq count (1+ count))
1842 ;; Make sure there is no extra white space after the ^_
1843 ;; at the end of the message.
1844 ;; Narrowing will make sure that whatever follows the junk
1845 ;; will be treated properly.
1846 (delete-region (point)
1847 (save-excursion
1848 (skip-chars-forward " \t\n")
1849 (point)))
1850 (save-excursion
1851 (let* ((header-end
1852 (progn
1853 (save-excursion
1854 (goto-char start)
1855 (forward-line 1)
1856 (if (looking-at "0")
1857 (forward-line 1)
1858 (forward-line 2))
1859 (save-restriction
1860 (narrow-to-region (point) (point-max))
1861 (rfc822-goto-eoh)
1862 (point)))))
1863 (case-fold-search t)
1864 (quoted-printable-header-field-end
1865 (save-excursion
1866 (goto-char start)
1867 (re-search-forward
1868 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
1869 header-end t)))
1870 (base64-header-field-end
1871 (save-excursion
1872 (goto-char start)
1873 (re-search-forward
1874 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
1875 header-end t))))
1876 (if quoted-printable-header-field-end
1877 (save-excursion
1878 (unless
1879 (mail-unquote-printable-region header-end (point) nil t t)
1880 (message "Malformed MIME quoted-printable message"))
1881 ;; Change "quoted-printable" to "8bit",
1882 ;; to reflect the decoding we just did.
1883 (goto-char quoted-printable-header-field-end)
1884 (delete-region (point) (search-backward ":"))
1885 (insert ": 8bit")))
1886 (if base64-header-field-end
1887 (save-excursion
1888 (when
1889 (condition-case nil
1890 (progn
1891 (base64-decode-region (1+ header-end)
1892 (- (point) 2))
1894 (error nil))
1895 ;; Change "base64" to "8bit", to reflect the
1896 ;; decoding we just did.
1897 (goto-char base64-header-field-end)
1898 (delete-region (point) (search-backward ":"))
1899 (insert ": 8bit"))))
1900 (setq last-coding-system-used nil)
1901 (or rmail-enable-mime
1902 (not rmail-enable-multibyte)
1903 (let ((mime-charset
1904 (if (and rmail-decode-mime-charset
1905 (save-excursion
1906 (goto-char start)
1907 (search-forward "\n\n" nil t)
1908 (let ((case-fold-search t))
1909 (re-search-backward
1910 rmail-mime-charset-pattern
1911 start t))))
1912 (intern (downcase (match-string 1))))))
1913 (rmail-decode-region start (point) mime-charset)))))
1914 ;; Add an X-Coding-System: header if we don't have one.
1915 (save-excursion
1916 (goto-char start)
1917 (forward-line 1)
1918 (if (looking-at "0")
1919 (forward-line 1)
1920 (forward-line 2))
1921 (or (save-restriction
1922 (narrow-to-region (point) (point-max))
1923 (rfc822-goto-eoh)
1924 (goto-char (point-min))
1925 (re-search-forward "^X-Coding-System:" nil t))
1926 (insert "X-Coding-System: "
1927 (symbol-name last-coding-system-used)
1928 "\n")))
1929 (narrow-to-region (point) (point-max))
1930 (and (= 0 (% count 10))
1931 (message "Converting to Babyl format...%d" count)))
1932 ;;*** MMDF format
1933 ((let ((case-fold-search t))
1934 (looking-at rmail-mmdf-delim1))
1935 (let ((case-fold-search t))
1936 (replace-match "\^L\n0, unseen,,\n*** EOOH ***\n")
1937 (re-search-forward rmail-mmdf-delim2 nil t)
1938 (replace-match "\^_"))
1939 (save-excursion
1940 (save-restriction
1941 (narrow-to-region start (1- (point)))
1942 (goto-char (point-min))
1943 (while (search-forward "\n\^_" nil t); single char "\^_"
1944 (replace-match "\n^_")))); 2 chars: "^" and "_"
1945 (setq last-coding-system-used nil)
1946 (or rmail-enable-mime
1947 (not rmail-enable-multibyte)
1948 (decode-coding-region start (point) 'undecided))
1949 (save-excursion
1950 (goto-char start)
1951 (forward-line 3)
1952 (insert "X-Coding-System: "
1953 (symbol-name last-coding-system-used)
1954 "\n"))
1955 (narrow-to-region (point) (point-max))
1956 (setq count (1+ count))
1957 (and (= 0 (% count 10))
1958 (message "Converting to Babyl format...%d" count)))
1959 ;;*** Mail format
1960 ((looking-at "^From ")
1961 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
1962 (rmail-nuke-pinhead-header)
1963 ;; If this message has a Content-Length field,
1964 ;; skip to the end of the contents.
1965 (let* ((header-end (save-excursion
1966 (and (re-search-forward "\n\n" nil t)
1967 (1- (point)))))
1968 (case-fold-search t)
1969 (quoted-printable-header-field-end
1970 (save-excursion
1971 (re-search-forward
1972 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
1973 header-end t)))
1974 (base64-header-field-end
1975 (save-excursion
1976 (re-search-forward
1977 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
1978 header-end t)))
1979 (size
1980 ;; Get the numeric value from the Content-Length field.
1981 (save-excursion
1982 ;; Back up to end of prev line,
1983 ;; in case the Content-Length field comes first.
1984 (forward-char -1)
1985 (and (search-forward "\ncontent-length: "
1986 header-end t)
1987 (let ((beg (point))
1988 (eol (progn (end-of-line) (point))))
1989 (string-to-int (buffer-substring beg eol)))))))
1990 (and size
1991 (if (and (natnump size)
1992 (<= (+ header-end size) (point-max))
1993 ;; Make sure this would put us at a position
1994 ;; that we could continue from.
1995 (save-excursion
1996 (goto-char (+ header-end size))
1997 (skip-chars-forward "\n")
1998 (or (eobp)
1999 (and (looking-at "BABYL OPTIONS:")
2000 (search-forward "\n\^_" nil t))
2001 (and (looking-at "\^L")
2002 (search-forward "\n\^_" nil t))
2003 (let ((case-fold-search t))
2004 (looking-at rmail-mmdf-delim1))
2005 (looking-at "From "))))
2006 (goto-char (+ header-end size))
2007 (message "Ignoring invalid Content-Length field")
2008 (sit-for 1 0 t)))
2009 (if (let ((case-fold-search nil))
2010 (re-search-forward
2011 (concat "^[\^_]?\\("
2012 rmail-unix-mail-delimiter
2013 "\\|"
2014 rmail-mmdf-delim1 "\\|"
2015 "^BABYL OPTIONS:\\|"
2016 "\^L\n[01],\\)") nil t))
2017 (goto-char (match-beginning 1))
2018 (goto-char (point-max)))
2019 (setq count (1+ count))
2020 (if quoted-printable-header-field-end
2021 (save-excursion
2022 (unless
2023 (mail-unquote-printable-region header-end (point) nil t t)
2024 (message "Malformed MIME quoted-printable message"))
2025 ;; Change "quoted-printable" to "8bit",
2026 ;; to reflect the decoding we just did.
2027 (goto-char quoted-printable-header-field-end)
2028 (delete-region (point) (search-backward ":"))
2029 (insert ": 8bit")))
2030 (if base64-header-field-end
2031 (save-excursion
2032 (when
2033 (condition-case nil
2034 (progn
2035 (base64-decode-region
2036 (1+ header-end)
2037 (save-excursion
2038 ;; Prevent base64-decode-region
2039 ;; from removing newline characters.
2040 (skip-chars-backward "\n\t ")
2041 (point)))
2043 (error nil))
2044 ;; Change "base64" to "8bit", to reflect the
2045 ;; decoding we just did.
2046 (goto-char base64-header-field-end)
2047 (delete-region (point) (search-backward ":"))
2048 (insert ": 8bit")))))
2050 (save-excursion
2051 (save-restriction
2052 (narrow-to-region start (point))
2053 (goto-char (point-min))
2054 (while (search-forward "\n\^_" nil t); single char
2055 (replace-match "\n^_")))); 2 chars: "^" and "_"
2056 ;; This is for malformed messages that don't end in newline.
2057 ;; There shouldn't be any, but some users say occasionally
2058 ;; there are some.
2059 (or (bolp) (newline))
2060 (insert ?\^_)
2061 (setq last-coding-system-used nil)
2062 (or rmail-enable-mime
2063 (not rmail-enable-multibyte)
2064 (let ((mime-charset
2065 (if (and rmail-decode-mime-charset
2066 (save-excursion
2067 (goto-char start)
2068 (search-forward "\n\n" nil t)
2069 (let ((case-fold-search t))
2070 (re-search-backward
2071 rmail-mime-charset-pattern
2072 start t))))
2073 (intern (downcase (match-string 1))))))
2074 (rmail-decode-region start (point) mime-charset)))
2075 (save-excursion
2076 (goto-char start)
2077 (forward-line 3)
2078 (insert "X-Coding-System: "
2079 (symbol-name last-coding-system-used)
2080 "\n"))
2081 (narrow-to-region (point) (point-max))
2082 (and (= 0 (% count 10))
2083 (message "Converting to Babyl format...%d" count)))
2085 ;; This kludge is because some versions of sendmail.el
2086 ;; insert an extra newline at the beginning that shouldn't
2087 ;; be there. sendmail.el has been fixed, but old versions
2088 ;; may still be in use. -- rms, 7 May 1993.
2089 ((eolp) (delete-char 1))
2090 (t (error "Cannot convert to babyl format")))))
2091 count))
2093 ;; Delete the "From ..." line, creating various other headers with
2094 ;; information from it if they don't already exist. Now puts the
2095 ;; original line into a mail-from: header line for debugging and for
2096 ;; use by the rmail-output function.
2097 (defun rmail-nuke-pinhead-header ()
2098 (save-excursion
2099 (save-restriction
2100 (let ((start (point))
2101 (end (progn
2102 (condition-case ()
2103 (search-forward "\n\n")
2104 (error
2105 (goto-char (point-max))
2106 (insert "\n\n")))
2107 (point)))
2108 has-from has-date)
2109 (narrow-to-region start end)
2110 (let ((case-fold-search t))
2111 (goto-char start)
2112 (setq has-from (search-forward "\nFrom:" nil t))
2113 (goto-char start)
2114 (setq has-date (and (search-forward "\nDate:" nil t) (point)))
2115 (goto-char start))
2116 (let ((case-fold-search nil))
2117 (if (re-search-forward (concat "^" rmail-unix-mail-delimiter) nil t)
2118 (replace-match
2119 (concat
2120 "Mail-from: \\&"
2121 ;; Keep and reformat the date if we don't
2122 ;; have a Date: field.
2123 (if has-date
2125 (concat
2126 "Date: \\2, \\4 \\3 \\9 \\5 "
2128 ;; The timezone could be matched by group 7 or group 10.
2129 ;; If neither of them matched, assume EST, since only
2130 ;; Easterners would be so sloppy.
2131 ;; It's a shame the substitution can't use "\\10".
2132 (cond
2133 ((/= (match-beginning 7) (match-end 7)) "\\7")
2134 ((/= (match-beginning 10) (match-end 10))
2135 (buffer-substring (match-beginning 10)
2136 (match-end 10)))
2137 (t "EST"))
2138 "\n"))
2139 ;; Keep and reformat the sender if we don't
2140 ;; have a From: field.
2141 (if has-from
2143 "From: \\1\n"))
2144 t)))))))
2146 ;;;; *** Rmail Message Formatting and Header Manipulation ***
2148 (defun rmail-reformat-message (beg end)
2149 (goto-char beg)
2150 (forward-line 1)
2151 (if (/= (following-char) ?0)
2152 (error "Bad format in RMAIL file"))
2153 (let ((inhibit-read-only t)
2154 (delta (- (buffer-size) end)))
2155 (delete-char 1)
2156 (insert ?1)
2157 (forward-line 1)
2158 (let ((case-fold-search t))
2159 (while (looking-at "Summary-line:\\|Mail-From:")
2160 (forward-line 1)))
2161 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*\n")
2162 (delete-region (point)
2163 (progn (forward-line 1) (point))))
2164 (let ((str (buffer-substring (point)
2165 (save-excursion (search-forward "\n\n" end 'move)
2166 (point)))))
2167 (insert str "*** EOOH ***\n")
2168 (narrow-to-region (point) (- (buffer-size) delta)))
2169 (goto-char (point-min))
2170 (if rmail-message-filter (funcall rmail-message-filter))
2171 (if (or rmail-displayed-headers rmail-ignored-headers)
2172 (rmail-clear-headers))))
2174 (defun rmail-clear-headers (&optional ignored-headers)
2175 "Delete all header fields that Rmail should not show.
2176 If the optional argument IGNORED-HEADERS is non-nil,
2177 delete all header fields whose names match that regexp.
2178 Otherwise, if `rmail-displayed-headers' is non-nil,
2179 delete all header fields *except* those whose names match that regexp.
2180 Otherwise, delete all header fields whose names match `rmail-ignored-headers'."
2181 (when (search-forward "\n\n" nil t)
2182 (forward-char -1)
2183 (let ((case-fold-search t)
2184 (buffer-read-only nil))
2185 (if (and rmail-displayed-headers (null ignored-headers))
2186 (save-restriction
2187 (narrow-to-region (point-min) (point))
2188 (let (lim next)
2189 (goto-char (point-min))
2190 (while (and (not (eobp))
2191 (save-excursion
2192 (if (re-search-forward "\n[^ \t]" nil t)
2193 (setq lim (match-beginning 0)
2194 next (1+ lim))
2195 (setq lim nil next (point-max)))))
2196 (if (save-excursion
2197 (re-search-forward rmail-displayed-headers lim t))
2198 (goto-char next)
2199 (delete-region (point) next))))
2200 (goto-char (point-min)))
2201 (or ignored-headers (setq ignored-headers rmail-ignored-headers))
2202 (save-restriction
2203 (narrow-to-region (point-min) (point))
2204 (while (and ignored-headers
2205 (progn
2206 (goto-char (point-min))
2207 (re-search-forward ignored-headers nil t)))
2208 (beginning-of-line)
2209 (delete-region (point)
2210 (if (re-search-forward "\n[^ \t]" nil t)
2211 (1- (point))
2212 (point-max)))))))))
2214 (defun rmail-msg-is-pruned ()
2215 (rmail-maybe-set-message-counters)
2216 (save-restriction
2217 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2218 (save-excursion
2219 (goto-char (point-min))
2220 (forward-line 1)
2221 (= (following-char) ?1))))
2223 (defun rmail-msg-restore-non-pruned-header ()
2224 (let ((old-point (point))
2225 new-point
2226 new-start
2227 (inhibit-read-only t))
2228 (save-excursion
2229 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2230 (goto-char (point-min))
2231 (forward-line 1)
2232 ;; Change 1 to 0.
2233 (delete-char 1)
2234 (insert ?0)
2235 ;; Insert new EOOH line at the proper place.
2236 (forward-line 1)
2237 (let ((case-fold-search t))
2238 (while (looking-at "Summary-Line:\\|Mail-From:")
2239 (forward-line 1)))
2240 (insert "*** EOOH ***\n")
2241 (setq new-start (point))
2242 ;; Delete the old reformatted header.
2243 (forward-char -1)
2244 (search-forward "\n*** EOOH ***\n")
2245 (forward-line -1)
2246 (let ((start (point)))
2247 (search-forward "\n\n")
2248 (if (and (<= start old-point)
2249 (<= old-point (point)))
2250 (setq new-point new-start))
2251 (delete-region start (point)))
2252 ;; Narrow to after the new EOOH line.
2253 (narrow-to-region new-start (point-max)))
2254 (if new-point
2255 (goto-char new-point))))
2257 (defun rmail-msg-prune-header ()
2258 (let ((new-point
2259 (= (point) (point-min))))
2260 (save-excursion
2261 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2262 (rmail-reformat-message (point-min) (point-max)))
2263 (if new-point
2264 (goto-char (point-min)))))
2266 (defun rmail-toggle-header (&optional arg)
2267 "Show original message header if pruned header currently shown, or vice versa.
2268 With argument ARG, show the message header pruned if ARG is greater than zero;
2269 otherwise, show it in full."
2270 (interactive "P")
2271 (let* ((pruned (with-current-buffer rmail-buffer
2272 (rmail-msg-is-pruned)))
2273 (prune (if arg
2274 (> (prefix-numeric-value arg) 0)
2275 (not pruned))))
2276 (if (eq pruned prune)
2278 (set-buffer rmail-buffer)
2279 (rmail-maybe-set-message-counters)
2280 (if rmail-enable-mime
2281 (let ((buffer-read-only nil))
2282 (if pruned
2283 (rmail-msg-restore-non-pruned-header)
2284 (rmail-msg-prune-header))
2285 (funcall rmail-show-mime-function))
2286 (let* ((buffer-read-only nil)
2287 (window (get-buffer-window (current-buffer)))
2288 (at-point-min (= (point) (point-min)))
2289 (all-headers-visible (= (window-start window) (point-min)))
2290 (on-header
2291 (save-excursion
2292 (and (not (search-backward "\n\n" nil t))
2293 (progn
2294 (end-of-line)
2295 (re-search-backward "^[-A-Za-z0-9]+:" nil t))
2296 (match-string 0))))
2297 (old-screen-line
2298 (rmail-count-screen-lines (window-start window) (point))))
2299 (if pruned
2300 (rmail-msg-restore-non-pruned-header)
2301 (rmail-msg-prune-header))
2302 (cond (at-point-min
2303 (goto-char (point-min)))
2304 (on-header
2305 (goto-char (point-min))
2306 (search-forward "\n\n")
2307 (or (re-search-backward
2308 (concat "^" (regexp-quote on-header)) nil t)
2309 (goto-char (point-min))))
2311 (save-selected-window
2312 (select-window window)
2313 (recenter old-screen-line)
2314 (if (and all-headers-visible
2315 (not (= (window-start) (point-min))))
2316 (recenter (- (window-height) 2))))))))
2317 (rmail-highlight-headers))))
2319 (defun rmail-narrow-to-non-pruned-header ()
2320 "Narrow to the whole (original) header of the current message."
2321 (let (start end)
2322 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2323 (goto-char (point-min))
2324 (forward-line 1)
2325 (if (= (following-char) ?1)
2326 (progn
2327 (forward-line 1)
2328 (setq start (point))
2329 (search-forward "*** EOOH ***\n")
2330 (setq end (match-beginning 0)))
2331 (forward-line 2)
2332 (setq start (point))
2333 (search-forward "\n\n")
2334 (setq end (1- (point))))
2335 (narrow-to-region start end)
2336 (goto-char start)))
2338 ;; Lifted from repos-count-screen-lines.
2339 ;; Return number of screen lines between START and END.
2340 (defun rmail-count-screen-lines (start end)
2341 (save-excursion
2342 (save-restriction
2343 (narrow-to-region start end)
2344 (goto-char (point-min))
2345 (vertical-motion (- (point-max) (point-min))))))
2347 ;;;; *** Rmail Attributes and Keywords ***
2349 ;; Make a string describing current message's attributes and keywords
2350 ;; and set it up as the name of a minor mode
2351 ;; so it will appear in the mode line.
2352 (defun rmail-display-labels ()
2353 (let ((blurb "") (beg (point-min-marker)) (end (point-max-marker)))
2354 (save-excursion
2355 (unwind-protect
2356 (progn
2357 (widen)
2358 (goto-char (rmail-msgbeg rmail-current-message))
2359 (forward-line 1)
2360 (if (looking-at "[01],")
2361 (progn
2362 (narrow-to-region (point) (progn (end-of-line) (point)))
2363 ;; Truly valid BABYL format requires a space before each
2364 ;; attribute or keyword name. Put them in if missing.
2365 (let (buffer-read-only)
2366 (goto-char (point-min))
2367 (while (search-forward "," nil t)
2368 (or (looking-at "[ ,]") (eobp)
2369 (insert " "))))
2370 (goto-char (point-max))
2371 (if (search-backward ",," nil 'move)
2372 (progn
2373 (if (> (point) (1+ (point-min)))
2374 (setq blurb (buffer-substring (+ 1 (point-min)) (point))))
2375 (if (> (- (point-max) (point)) 2)
2376 (setq blurb
2377 (concat blurb
2379 (buffer-substring (+ (point) 3)
2380 (1- (point-max)))))))))))
2381 ;; Note: we don't use save-restriction because that does not work right
2382 ;; if changes are made outside the saved restriction
2383 ;; before that restriction is restored.
2384 (narrow-to-region beg end)
2385 (set-marker beg nil)
2386 (set-marker end nil)))
2387 (while (string-match " +," blurb)
2388 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
2389 (substring blurb (match-end 0)))))
2390 (while (string-match ", +" blurb)
2391 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
2392 (substring blurb (match-end 0)))))
2393 (setq mode-line-process
2394 (format " %d/%d%s"
2395 rmail-current-message rmail-total-messages blurb))
2396 ;; If rmail-enable-mime is non-nil, we may have to update
2397 ;; `mode-line-process' of rmail-view-buffer too.
2398 (if (and rmail-enable-mime
2399 (not (eq (current-buffer) rmail-view-buffer))
2400 (buffer-live-p rmail-view-buffer))
2401 (let ((mlp mode-line-process))
2402 (with-current-buffer rmail-view-buffer
2403 (setq mode-line-process mlp))))))
2405 ;; Turn an attribute of a message on or off according to STATE.
2406 ;; ATTR is the name of the attribute, as a string.
2407 ;; MSGNUM is message number to change; nil means current message.
2408 (defun rmail-set-attribute (attr state &optional msgnum)
2409 (set-buffer rmail-buffer)
2410 (let ((omax (point-max-marker))
2411 (omin (point-min-marker))
2412 (buffer-read-only nil))
2413 (or msgnum (setq msgnum rmail-current-message))
2414 (if (> msgnum 0)
2415 (unwind-protect
2416 (save-excursion
2417 (widen)
2418 (goto-char (+ 3 (rmail-msgbeg msgnum)))
2419 (let ((curstate
2420 (not
2421 (null (search-backward (concat ", " attr ",")
2422 (prog1 (point) (end-of-line)) t)))))
2423 (or (eq curstate (not (not state)))
2424 (if curstate
2425 (delete-region (point) (1- (match-end 0)))
2426 (beginning-of-line)
2427 (forward-char 2)
2428 (insert " " attr ","))))
2429 (if (string= attr "deleted")
2430 (rmail-set-message-deleted-p msgnum state)))
2431 ;; Note: we don't use save-restriction because that does not work right
2432 ;; if changes are made outside the saved restriction
2433 ;; before that restriction is restored.
2434 (narrow-to-region omin omax)
2435 (set-marker omin nil)
2436 (set-marker omax nil)
2437 (if (= msgnum rmail-current-message)
2438 (rmail-display-labels))))))
2440 ;; Return t if the attributes/keywords line of msg number MSG
2441 ;; contains a match for the regexp LABELS.
2442 (defun rmail-message-labels-p (msg labels)
2443 (save-excursion
2444 (save-restriction
2445 (widen)
2446 (goto-char (rmail-msgbeg msg))
2447 (forward-char 3)
2448 (re-search-backward labels (prog1 (point) (end-of-line)) t))))
2450 ;;;; *** Rmail Message Selection And Support ***
2452 (defun rmail-msgend (n)
2453 (marker-position (aref rmail-message-vector (1+ n))))
2455 (defun rmail-msgbeg (n)
2456 (marker-position (aref rmail-message-vector n)))
2458 (defun rmail-widen-to-current-msgbeg (function)
2459 "Call FUNCTION with point at start of internal data of current message.
2460 Assumes that bounds were previously narrowed to display the message in Rmail.
2461 The bounds are widened enough to move point where desired, then narrowed
2462 again afterward.
2464 FUNCTION may not change the visible text of the message, but it may
2465 change the invisible header text."
2466 (save-excursion
2467 (unwind-protect
2468 (progn
2469 (narrow-to-region (rmail-msgbeg rmail-current-message)
2470 (point-max))
2471 (goto-char (point-min))
2472 (funcall function))
2473 ;; Note: we don't use save-restriction because that does not work right
2474 ;; if changes are made outside the saved restriction
2475 ;; before that restriction is restored.
2476 (narrow-to-region (rmail-msgbeg rmail-current-message)
2477 (rmail-msgend rmail-current-message)))))
2479 (defun rmail-forget-messages ()
2480 (unwind-protect
2481 (if (vectorp rmail-message-vector)
2482 (let* ((i 0)
2483 (v rmail-message-vector)
2484 (n (length v)))
2485 (while (< i n)
2486 (move-marker (aref v i) nil)
2487 (setq i (1+ i)))))
2488 (setq rmail-message-vector nil)
2489 (setq rmail-msgref-vector nil)
2490 (setq rmail-deleted-vector nil)))
2492 (defun rmail-maybe-set-message-counters ()
2493 (if (not (and rmail-deleted-vector
2494 rmail-message-vector
2495 rmail-current-message
2496 rmail-total-messages))
2497 (rmail-set-message-counters)))
2499 (defun rmail-count-new-messages (&optional nomsg)
2500 (let* ((case-fold-search nil)
2501 (total-messages 0)
2502 (messages-head nil)
2503 (deleted-head nil))
2504 (or nomsg (message "Counting new messages..."))
2505 (goto-char (point-max))
2506 ;; Put at the end of messages-head
2507 ;; the entry for message N+1, which marks
2508 ;; the end of message N. (N = number of messages).
2509 (search-backward "\n\^_")
2510 (forward-char 1)
2511 (setq messages-head (list (point-marker)))
2512 (rmail-set-message-counters-counter (point-min))
2513 (setq rmail-current-message (1+ rmail-total-messages))
2514 (setq rmail-total-messages
2515 (+ rmail-total-messages total-messages))
2516 (setq rmail-message-vector
2517 (vconcat rmail-message-vector (cdr messages-head)))
2518 (aset rmail-message-vector
2519 rmail-current-message (car messages-head))
2520 (setq rmail-deleted-vector
2521 (concat rmail-deleted-vector deleted-head))
2522 (setq rmail-summary-vector
2523 (vconcat rmail-summary-vector (make-vector total-messages nil)))
2524 (setq rmail-msgref-vector
2525 (vconcat rmail-msgref-vector (make-vector total-messages nil)))
2526 ;; Fill in the new elements of rmail-msgref-vector.
2527 (let ((i (1+ (- rmail-total-messages total-messages))))
2528 (while (<= i rmail-total-messages)
2529 (aset rmail-msgref-vector i (list i))
2530 (setq i (1+ i))))
2531 (goto-char (point-min))
2532 (or nomsg (message "Counting new messages...done (%d)" total-messages))))
2534 (defun rmail-set-message-counters ()
2535 (rmail-forget-messages)
2536 (save-excursion
2537 (save-restriction
2538 (widen)
2539 (let* ((point-save (point))
2540 (total-messages 0)
2541 (messages-after-point)
2542 (case-fold-search nil)
2543 (messages-head nil)
2544 (deleted-head nil))
2545 (message "Counting messages...")
2546 (goto-char (point-max))
2547 ;; Put at the end of messages-head
2548 ;; the entry for message N+1, which marks
2549 ;; the end of message N. (N = number of messages).
2550 (search-backward "\n\^_" nil t)
2551 (if (/= (point) (point-max)) (forward-char 1))
2552 (setq messages-head (list (point-marker)))
2553 (rmail-set-message-counters-counter (min (point) point-save))
2554 (setq messages-after-point total-messages)
2555 (rmail-set-message-counters-counter)
2556 (setq rmail-total-messages total-messages)
2557 (setq rmail-current-message
2558 (min total-messages
2559 (max 1 (- total-messages messages-after-point))))
2560 (setq rmail-message-vector
2561 (apply 'vector (cons (point-min-marker) messages-head))
2562 rmail-deleted-vector (concat "0" deleted-head)
2563 rmail-summary-vector (make-vector rmail-total-messages nil)
2564 rmail-msgref-vector (make-vector (1+ rmail-total-messages) nil))
2565 (let ((i 0))
2566 (while (<= i rmail-total-messages)
2567 (aset rmail-msgref-vector i (list i))
2568 (setq i (1+ i))))
2569 (message "Counting messages...done")))))
2571 (defun rmail-set-message-counters-counter (&optional stop)
2572 (let ((start (point))
2573 next)
2574 (while (search-backward "\n\^_\^L" stop t)
2575 ;; Detect messages that have been added with DOS line endings and
2576 ;; convert the line endings for such messages.
2577 (setq next (point))
2578 (if (looking-at "\n\^_\^L\r\n")
2579 (let ((buffer-read-only nil)
2580 (buffer-undo t))
2581 (message "Counting messages...(converting line endings)")
2582 (save-excursion
2583 (goto-char start)
2584 (while (search-backward "\r\n" next t)
2585 (delete-char 1)))))
2586 (setq start next)
2587 (forward-char 1)
2588 (setq messages-head (cons (point-marker) messages-head))
2589 (save-excursion
2590 (setq deleted-head
2591 (cons (if (search-backward ", deleted,"
2592 (prog1 (point)
2593 (forward-line 2))
2595 ?D ?\ )
2596 deleted-head)))
2597 (if (zerop (% (setq total-messages (1+ total-messages)) 20))
2598 (message "Counting messages...%d" total-messages)))))
2600 (defun rmail-beginning-of-message ()
2601 "Show current message starting from the beginning."
2602 (interactive)
2603 (rmail-show-message rmail-current-message))
2605 (defun rmail-unknown-mail-followup-to ()
2606 "Handle a \"Mail-Followup-To\" header field with an unknown mailing list.
2607 Ask the user whether to add that list name to `mail-mailing-lists'."
2608 (save-restriction
2609 (rmail-narrow-to-non-pruned-header)
2610 (let ((mail-followup-to (mail-fetch-field "mail-followup-to" nil t)))
2611 (when mail-followup-to
2612 (let ((addresses
2613 (split-string
2614 (mail-strip-quoted-names mail-followup-to)
2615 ",[[:space:]]+" t)))
2616 (dolist (addr addresses)
2617 (when (and (not (member addr mail-mailing-lists))
2618 (not
2619 ;; taken from rmailsum.el
2620 (string-match
2621 (or rmail-user-mail-address-regexp
2622 (concat "^\\("
2623 (regexp-quote (user-login-name))
2624 "\\($\\|@\\)\\|"
2625 (regexp-quote
2626 (or user-mail-address
2627 (concat (user-login-name) "@"
2628 (or mail-host-address
2629 (system-name)))))
2630 "\\>\\)"))
2631 addr))
2632 (y-or-n-p
2633 (format "Add `%s' to `mail-mailing-lists'? "
2634 addr)))
2635 (customize-save-variable 'mail-mailing-lists
2636 (cons addr mail-mailing-lists)))))))))
2638 (defun rmail-show-message (&optional n no-summary)
2639 "Show message number N (prefix argument), counting from start of file.
2640 If summary buffer is currently displayed, update current message there also."
2641 (interactive "p")
2642 (or (eq major-mode 'rmail-mode)
2643 (switch-to-buffer rmail-buffer))
2644 (rmail-maybe-set-message-counters)
2645 (widen)
2646 (if (zerop rmail-total-messages)
2647 (progn (narrow-to-region (point-min) (1- (point-max)))
2648 (goto-char (point-min))
2649 (setq mode-line-process nil))
2650 (let (blurb coding-system)
2651 (if (not n)
2652 (setq n rmail-current-message)
2653 (cond ((<= n 0)
2654 (setq n 1
2655 rmail-current-message 1
2656 blurb "No previous message"))
2657 ((> n rmail-total-messages)
2658 (setq n rmail-total-messages
2659 rmail-current-message rmail-total-messages
2660 blurb "No following message"))
2662 (setq rmail-current-message n))))
2663 (let ((beg (rmail-msgbeg n)))
2664 (goto-char beg)
2665 (forward-line 1)
2666 (save-excursion
2667 (let ((end (rmail-msgend n)))
2668 (save-restriction
2669 (if (prog1 (= (following-char) ?0)
2670 (forward-line 2)
2671 ;; If there's a Summary-line in the (otherwise empty)
2672 ;; header, we didn't yet get past the EOOH line.
2673 (if (looking-at "^\\*\\*\\* EOOH \\*\\*\\*\n")
2674 (forward-line 1))
2675 (narrow-to-region (point) end))
2676 (rfc822-goto-eoh)
2677 (search-forward "\n*** EOOH ***\n" end t))
2678 (narrow-to-region beg (point))
2679 (goto-char (point-min))
2680 (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t)
2681 (let ((coding-system (intern (match-string 1))))
2682 (condition-case nil
2683 (progn
2684 (check-coding-system coding-system)
2685 (setq buffer-file-coding-system coding-system))
2686 (error
2687 (setq buffer-file-coding-system nil))))
2688 (setq buffer-file-coding-system nil)))))
2689 ;; Clear the "unseen" attribute when we show a message.
2690 (rmail-set-attribute "unseen" nil)
2691 (let ((end (rmail-msgend n)))
2692 ;; Reformat the header, or else find the reformatted header.
2693 (if (= (following-char) ?0)
2694 (rmail-reformat-message beg end)
2695 (search-forward "\n*** EOOH ***\n" end t)
2696 (narrow-to-region (point) end)))
2697 (goto-char (point-min))
2698 (walk-windows
2699 (function (lambda (window)
2700 (if (eq (window-buffer window) (current-buffer))
2701 (set-window-point window (point)))))
2702 nil t)
2703 (rmail-display-labels)
2704 (if (eq rmail-enable-mime t)
2705 (funcall rmail-show-mime-function)
2706 (setq rmail-view-buffer rmail-buffer))
2707 (when mail-mailing-lists
2708 (rmail-unknown-mail-followup-to))
2709 (rmail-highlight-headers)
2710 (if transient-mark-mode (deactivate-mark))
2711 (run-hooks 'rmail-show-message-hook)
2712 ;; If there is a summary buffer, try to move to this message
2713 ;; in that buffer. But don't complain if this message
2714 ;; is not mentioned in the summary.
2715 ;; Don't do this at all if we were called on behalf
2716 ;; of cursor motion in the summary buffer.
2717 (and (rmail-summary-exists) (not no-summary)
2718 (let ((curr-msg rmail-current-message))
2719 (rmail-select-summary
2720 (rmail-summary-goto-msg curr-msg t t))))
2721 (with-current-buffer rmail-buffer
2722 (rmail-auto-file))
2723 (if blurb
2724 (message blurb))))))
2726 (defun rmail-redecode-body (coding)
2727 "Decode the body of the current message using coding system CODING.
2728 This is useful with mail messages that have malformed or missing
2729 charset= headers.
2731 This function assumes that the current message is already decoded
2732 and displayed in the RMAIL buffer, but the coding system used to
2733 decode it was incorrect. It then encodes the message back to its
2734 original form, and decodes it again, using the coding system CODING.
2736 Note that if Emacs erroneously auto-detected one of the iso-2022
2737 encodings in the message, this function might fail because the escape
2738 sequences that switch between character sets and also single-shift and
2739 locking-shift codes are impossible to recover. This function is meant
2740 to be used to fix messages encoded with 8-bit encodings, such as
2741 iso-8859, koi8-r, etc."
2742 (interactive "zCoding system for re-decoding this message: ")
2743 (when (not rmail-enable-mime)
2744 (or (eq major-mode 'rmail-mode)
2745 (switch-to-buffer rmail-buffer))
2746 (save-excursion
2747 (let ((pruned (rmail-msg-is-pruned)))
2748 (unwind-protect
2749 (let ((msgbeg (rmail-msgbeg rmail-current-message))
2750 (msgend (rmail-msgend rmail-current-message))
2751 x-coding-header)
2752 ;; We need the message headers pruned (we later restore
2753 ;; the pruned stat to what it was, see the end of
2754 ;; unwind-protect form).
2755 (or pruned
2756 (rmail-toggle-header 1))
2757 (narrow-to-region msgbeg msgend)
2758 (goto-char (point-min))
2759 (when (search-forward "\n*** EOOH ***\n" (point-max) t)
2760 (narrow-to-region msgbeg (point)))
2761 (goto-char (point-min))
2762 (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t)
2763 (let ((old-coding (intern (match-string 1)))
2764 (buffer-read-only nil))
2765 (check-coding-system old-coding)
2766 ;; Make sure the new coding system uses the same EOL
2767 ;; conversion, to prevent ^M characters from popping
2768 ;; up all over the place.
2769 (setq coding
2770 (coding-system-change-eol-conversion
2771 coding
2772 (coding-system-eol-type old-coding)))
2773 (setq x-coding-header (point-marker))
2774 (narrow-to-region msgbeg msgend)
2775 (encode-coding-region (point) msgend old-coding)
2776 (decode-coding-region (point) msgend coding)
2777 (setq last-coding-system-used coding)
2778 ;; Rewrite the coding-system header according
2779 ;; to what we did.
2780 (goto-char x-coding-header)
2781 (delete-region (point)
2782 (save-excursion
2783 (beginning-of-line)
2784 (point)))
2785 (insert "X-Coding-System: "
2786 (symbol-name last-coding-system-used))
2787 (set-marker x-coding-header nil)
2788 (rmail-show-message))
2789 (error "No X-Coding-System header found")))
2790 (or pruned
2791 (rmail-toggle-header 0)))))))
2793 ;; Find all occurrences of certain fields, and highlight them.
2794 (defun rmail-highlight-headers ()
2795 ;; Do this only if the system supports faces.
2796 (if (and (fboundp 'internal-find-face)
2797 rmail-highlighted-headers)
2798 (save-excursion
2799 (search-forward "\n\n" nil 'move)
2800 (save-restriction
2801 (narrow-to-region (point-min) (point))
2802 (let ((case-fold-search t)
2803 (inhibit-read-only t)
2804 ;; Highlight with boldface if that is available.
2805 ;; Otherwise use the `highlight' face.
2806 (face (or rmail-highlight-face
2807 (if (face-differs-from-default-p 'bold)
2808 'bold 'highlight)))
2809 ;; List of overlays to reuse.
2810 (overlays rmail-overlay-list))
2811 (goto-char (point-min))
2812 (while (re-search-forward rmail-highlighted-headers nil t)
2813 (skip-chars-forward " \t")
2814 (let ((beg (point))
2815 overlay)
2816 (while (progn (forward-line 1)
2817 (looking-at "[ \t]")))
2818 ;; Back up over newline, then trailing spaces or tabs
2819 (forward-char -1)
2820 (while (member (preceding-char) '(? ?\t))
2821 (forward-char -1))
2822 (if overlays
2823 ;; Reuse an overlay we already have.
2824 (progn
2825 (setq overlay (car overlays)
2826 overlays (cdr overlays))
2827 (overlay-put overlay 'face face)
2828 (move-overlay overlay beg (point)))
2829 ;; Make a new overlay and add it to
2830 ;; rmail-overlay-list.
2831 (setq overlay (make-overlay beg (point)))
2832 (overlay-put overlay 'face face)
2833 (setq rmail-overlay-list
2834 (cons overlay rmail-overlay-list))))))))))
2836 (defun rmail-auto-file ()
2837 "Automatically move a message into a sub-folder based on criteria.
2838 Called when a new message is displayed."
2839 (if (or (rmail-message-labels-p rmail-current-message "filed")
2840 (not (string= (buffer-file-name)
2841 (expand-file-name rmail-file-name))))
2842 ;; Do nothing if it's already been filed.
2844 ;; Find out some basics (common fields)
2845 (let ((from (mail-fetch-field "from"))
2846 (subj (mail-fetch-field "subject"))
2847 (to (concat (mail-fetch-field "to") "," (mail-fetch-field "cc")))
2848 (d rmail-automatic-folder-directives)
2849 (directive-loop nil)
2850 (folder nil))
2851 (while d
2852 (setq folder (car (car d))
2853 directive-loop (cdr (car d)))
2854 (while (and (car directive-loop)
2855 (let ((f (cond
2856 ((string= (car directive-loop) "from") from)
2857 ((string= (car directive-loop) "to") to)
2858 ((string= (car directive-loop) "subject") subj)
2859 (t (mail-fetch-field (car directive-loop))))))
2860 (and f (string-match (car (cdr directive-loop)) f))))
2861 (setq directive-loop (cdr (cdr directive-loop))))
2862 ;; If there are no directives left, then it was a complete match.
2863 (if (null directive-loop)
2864 (if (null folder)
2865 (rmail-delete-forward)
2866 (if (string= "/dev/null" folder)
2867 (rmail-delete-message)
2868 (rmail-output-to-rmail-file folder 1 t)
2869 (setq d nil))))
2870 (setq d (cdr d))))))
2872 (defun rmail-next-message (n)
2873 "Show following message whether deleted or not.
2874 With prefix arg N, moves forward N messages, or backward if N is negative."
2875 (interactive "p")
2876 (set-buffer rmail-buffer)
2877 (rmail-maybe-set-message-counters)
2878 (rmail-show-message (+ rmail-current-message n)))
2880 (defun rmail-previous-message (n)
2881 "Show previous message whether deleted or not.
2882 With prefix arg N, moves backward N messages, or forward if N is negative."
2883 (interactive "p")
2884 (rmail-next-message (- n)))
2886 (defun rmail-next-undeleted-message (n)
2887 "Show following non-deleted message.
2888 With prefix arg N, moves forward N non-deleted messages,
2889 or backward if N is negative.
2891 Returns t if a new message is being shown, nil otherwise."
2892 (interactive "p")
2893 (set-buffer rmail-buffer)
2894 (rmail-maybe-set-message-counters)
2895 (let ((lastwin rmail-current-message)
2896 (current rmail-current-message))
2897 (while (and (> n 0) (< current rmail-total-messages))
2898 (setq current (1+ current))
2899 (if (not (rmail-message-deleted-p current))
2900 (setq lastwin current n (1- n))))
2901 (while (and (< n 0) (> current 1))
2902 (setq current (1- current))
2903 (if (not (rmail-message-deleted-p current))
2904 (setq lastwin current n (1+ n))))
2905 (if (/= lastwin rmail-current-message)
2906 (progn (rmail-show-message lastwin)
2908 (if (< n 0)
2909 (message "No previous nondeleted message"))
2910 (if (> n 0)
2911 (message "No following nondeleted message"))
2912 nil)))
2914 (defun rmail-previous-undeleted-message (n)
2915 "Show previous non-deleted message.
2916 With prefix argument N, moves backward N non-deleted messages,
2917 or forward if N is negative."
2918 (interactive "p")
2919 (rmail-next-undeleted-message (- n)))
2921 (defun rmail-first-message ()
2922 "Show first message in file."
2923 (interactive)
2924 (rmail-maybe-set-message-counters)
2925 (rmail-show-message 1))
2927 (defun rmail-last-message ()
2928 "Show last message in file."
2929 (interactive)
2930 (rmail-maybe-set-message-counters)
2931 (rmail-show-message rmail-total-messages))
2933 (defun rmail-what-message ()
2934 (let ((where (point))
2935 (low 1)
2936 (high rmail-total-messages)
2937 (mid (/ rmail-total-messages 2)))
2938 (while (> (- high low) 1)
2939 (if (>= where (rmail-msgbeg mid))
2940 (setq low mid)
2941 (setq high mid))
2942 (setq mid (+ low (/ (- high low) 2))))
2943 (if (>= where (rmail-msgbeg high)) high low)))
2945 (defun rmail-message-recipients-p (msg recipients &optional primary-only)
2946 (save-restriction
2947 (goto-char (rmail-msgbeg msg))
2948 (search-forward "\n*** EOOH ***\n")
2949 (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
2950 (or (string-match recipients (or (mail-fetch-field "To") ""))
2951 (string-match recipients (or (mail-fetch-field "From") ""))
2952 (if (not primary-only)
2953 (string-match recipients (or (mail-fetch-field "Cc") ""))))))
2955 (defun rmail-message-regexp-p (n regexp)
2956 "Return t, if for message number N, regexp REGEXP matches in the header."
2957 (let ((beg (rmail-msgbeg n))
2958 (end (rmail-msgend n)))
2959 (goto-char beg)
2960 (forward-line 1)
2961 (save-excursion
2962 (save-restriction
2963 (if (prog1 (= (following-char) ?0)
2964 (forward-line 2)
2965 ;; If there's a Summary-line in the (otherwise empty)
2966 ;; header, we didn't yet get past the EOOH line.
2967 (when (looking-at "^\\*\\*\\* EOOH \\*\\*\\*\n")
2968 (forward-line 1))
2969 (setq beg (point))
2970 (narrow-to-region (point) end))
2971 (progn
2972 (rfc822-goto-eoh)
2973 (setq end (point)))
2974 (setq beg (point))
2975 (search-forward "\n*** EOOH ***\n" end t)
2976 (setq end (1+ (match-beginning 0)))))
2977 (goto-char beg)
2978 (if rmail-enable-mime
2979 (funcall rmail-search-mime-header-function n regexp end)
2980 (re-search-forward regexp end t)))))
2982 (defun rmail-search-message (msg regexp)
2983 "Return non-nil, if for message number MSG, regexp REGEXP matches."
2984 (goto-char (rmail-msgbeg msg))
2985 (if rmail-enable-mime
2986 (funcall rmail-search-mime-message-function msg regexp)
2987 (re-search-forward regexp (rmail-msgend msg) t)))
2989 (defvar rmail-search-last-regexp nil)
2990 (defun rmail-search (regexp &optional n)
2991 "Show message containing next match for REGEXP (but not the current msg).
2992 Prefix argument gives repeat count; negative argument means search
2993 backwards (through earlier messages).
2994 Interactively, empty argument means use same regexp used last time."
2995 (interactive
2996 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
2997 (prompt
2998 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
2999 regexp)
3000 (if rmail-search-last-regexp
3001 (setq prompt (concat prompt
3002 "(default "
3003 rmail-search-last-regexp
3004 ") ")))
3005 (setq regexp (read-string prompt))
3006 (cond ((not (equal regexp ""))
3007 (setq rmail-search-last-regexp regexp))
3008 ((not rmail-search-last-regexp)
3009 (error "No previous Rmail search string")))
3010 (list rmail-search-last-regexp
3011 (prefix-numeric-value current-prefix-arg))))
3012 (or n (setq n 1))
3013 (message "%sRmail search for %s..."
3014 (if (< n 0) "Reverse " "")
3015 regexp)
3016 (set-buffer rmail-buffer)
3017 (rmail-maybe-set-message-counters)
3018 (let ((omin (point-min))
3019 (omax (point-max))
3020 (opoint (point))
3022 (reversep (< n 0))
3023 (msg rmail-current-message))
3024 (unwind-protect
3025 (progn
3026 (widen)
3027 (while (/= n 0)
3028 ;; Check messages one by one, advancing message number up or down
3029 ;; but searching forward through each message.
3030 (if reversep
3031 (while (and (null win) (> msg 1))
3032 (setq msg (1- msg)
3033 win (rmail-search-message msg regexp)))
3034 (while (and (null win) (< msg rmail-total-messages))
3035 (setq msg (1+ msg)
3036 win (rmail-search-message msg regexp))))
3037 (setq n (+ n (if reversep 1 -1)))))
3038 (if win
3039 (progn
3040 (rmail-show-message msg)
3041 ;; Search forward (if this is a normal search) or backward
3042 ;; (if this is a reverse search) through this message to
3043 ;; position point. This search may fail because REGEXP
3044 ;; was found in the hidden portion of this message. In
3045 ;; that case, move point to the beginning of visible
3046 ;; portion.
3047 (if reversep
3048 (progn
3049 (goto-char (point-max))
3050 (re-search-backward regexp nil 'move))
3051 (goto-char (point-min))
3052 (re-search-forward regexp nil t))
3053 (message "%sRmail search for %s...done"
3054 (if reversep "Reverse " "")
3055 regexp))
3056 (goto-char opoint)
3057 (narrow-to-region omin omax)
3058 (ding)
3059 (message "Search failed: %s" regexp)))))
3061 (defun rmail-search-backwards (regexp &optional n)
3062 "Show message containing previous match for REGEXP.
3063 Prefix argument gives repeat count; negative argument means search
3064 forward (through later messages).
3065 Interactively, empty argument means use same regexp used last time."
3066 (interactive
3067 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
3068 (prompt
3069 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
3070 regexp)
3071 (if rmail-search-last-regexp
3072 (setq prompt (concat prompt
3073 "(default "
3074 rmail-search-last-regexp
3075 ") ")))
3076 (setq regexp (read-string prompt))
3077 (cond ((not (equal regexp ""))
3078 (setq rmail-search-last-regexp regexp))
3079 ((not rmail-search-last-regexp)
3080 (error "No previous Rmail search string")))
3081 (list rmail-search-last-regexp
3082 (prefix-numeric-value current-prefix-arg))))
3083 (rmail-search regexp (- (or n 1))))
3085 ;; Show the first message which has the `unseen' attribute.
3086 (defun rmail-first-unseen-message ()
3087 (rmail-maybe-set-message-counters)
3088 (let ((current 1)
3089 found)
3090 (save-restriction
3091 (widen)
3092 (while (and (not found) (<= current rmail-total-messages))
3093 (if (rmail-message-labels-p current ", ?\\(unseen\\),")
3094 (setq found current))
3095 (setq current (1+ current))))
3096 ;; Let the caller show the message.
3097 ;; (if found
3098 ;; (rmail-show-message found))
3099 found))
3101 (defun rmail-next-same-subject (n)
3102 "Go to the next mail message having the same subject header.
3103 With prefix argument N, do this N times.
3104 If N is negative, go backwards instead."
3105 (interactive "p")
3106 (let ((subject (mail-fetch-field "Subject"))
3107 (forward (> n 0))
3108 (i rmail-current-message)
3109 (case-fold-search t)
3110 search-regexp found)
3111 (if (string-match "\\`[ \t]+" subject)
3112 (setq subject (substring subject (match-end 0))))
3113 (if (string-match "Re:[ \t]*" subject)
3114 (setq subject (substring subject (match-end 0))))
3115 (if (string-match "[ \t]+\\'" subject)
3116 (setq subject (substring subject 0 (match-beginning 0))))
3117 (setq search-regexp (concat "^Subject: *\\(Re:[ \t]*\\)?"
3118 (regexp-quote subject)
3119 "[ \t]*\n"))
3120 (save-excursion
3121 (save-restriction
3122 (widen)
3123 (while (and (/= n 0)
3124 (if forward
3125 (< i rmail-total-messages)
3126 (> i 1)))
3127 (let (done)
3128 (while (and (not done)
3129 (if forward
3130 (< i rmail-total-messages)
3131 (> i 1)))
3132 (setq i (if forward (1+ i) (1- i)))
3133 (goto-char (rmail-msgbeg i))
3134 (search-forward "\n*** EOOH ***\n")
3135 (let ((beg (point)) end)
3136 (search-forward "\n\n")
3137 (setq end (point))
3138 (goto-char beg)
3139 (setq done (re-search-forward search-regexp end t))))
3140 (if done (setq found i)))
3141 (setq n (if forward (1- n) (1+ n))))))
3142 (if found
3143 (rmail-show-message found)
3144 (error "No %s message with same subject"
3145 (if forward "following" "previous")))))
3147 (defun rmail-previous-same-subject (n)
3148 "Go to the previous mail message having the same subject header.
3149 With prefix argument N, do this N times.
3150 If N is negative, go forwards instead."
3151 (interactive "p")
3152 (rmail-next-same-subject (- n)))
3154 ;;;; *** Rmail Message Deletion Commands ***
3156 (defun rmail-message-deleted-p (n)
3157 (= (aref rmail-deleted-vector n) ?D))
3159 (defun rmail-set-message-deleted-p (n state)
3160 (aset rmail-deleted-vector n (if state ?D ?\ )))
3162 (defun rmail-delete-message ()
3163 "Delete this message and stay on it."
3164 (interactive)
3165 (rmail-set-attribute "deleted" t)
3166 (run-hooks 'rmail-delete-message-hook))
3168 (defun rmail-undelete-previous-message ()
3169 "Back up to deleted message, select it, and undelete it."
3170 (interactive)
3171 (set-buffer rmail-buffer)
3172 (let ((msg rmail-current-message))
3173 (while (and (> msg 0)
3174 (not (rmail-message-deleted-p msg)))
3175 (setq msg (1- msg)))
3176 (if (= msg 0)
3177 (error "No previous deleted message")
3178 (if (/= msg rmail-current-message)
3179 (rmail-show-message msg))
3180 (rmail-set-attribute "deleted" nil)
3181 (if (rmail-summary-exists)
3182 (save-excursion
3183 (set-buffer rmail-summary-buffer)
3184 (rmail-summary-mark-undeleted msg)))
3185 (rmail-maybe-display-summary))))
3187 (defun rmail-delete-forward (&optional backward)
3188 "Delete this message and move to next nondeleted one.
3189 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
3190 With prefix argument, delete and move backward.
3192 Returns t if a new message is displayed after the delete, or nil otherwise."
3193 (interactive "P")
3194 (rmail-set-attribute "deleted" t)
3195 (run-hooks 'rmail-delete-message-hook)
3196 (let ((del-msg rmail-current-message))
3197 (if (rmail-summary-exists)
3198 (rmail-select-summary
3199 (rmail-summary-mark-deleted del-msg)))
3200 (prog1 (rmail-next-undeleted-message (if backward -1 1))
3201 (rmail-maybe-display-summary))))
3203 (defun rmail-delete-backward ()
3204 "Delete this message and move to previous nondeleted one.
3205 Deleted messages stay in the file until the \\[rmail-expunge] command is given."
3206 (interactive)
3207 (rmail-delete-forward t))
3209 ;; Compute the message number a given message would have after expunging.
3210 ;; The present number of the message is OLDNUM.
3211 ;; DELETEDVEC should be rmail-deleted-vector.
3212 ;; The value is nil for a message that would be deleted.
3213 (defun rmail-msg-number-after-expunge (deletedvec oldnum)
3214 (if (or (null oldnum) (= (aref deletedvec oldnum) ?D))
3216 (let ((i 0)
3217 (newnum 0))
3218 (while (< i oldnum)
3219 (if (/= (aref deletedvec i) ?D)
3220 (setq newnum (1+ newnum)))
3221 (setq i (1+ i)))
3222 newnum)))
3224 (defun rmail-expunge-confirmed ()
3225 "Return t if deleted message should be expunged. If necessary, ask the user.
3226 See also user-option `rmail-confirm-expunge'."
3227 (set-buffer rmail-buffer)
3228 (or (not (stringp rmail-deleted-vector))
3229 (not (string-match "D" rmail-deleted-vector))
3230 (null rmail-confirm-expunge)
3231 (funcall rmail-confirm-expunge
3232 "Erase deleted messages from Rmail file? ")))
3234 (defun rmail-only-expunge (&optional dont-show)
3235 "Actually erase all deleted messages in the file."
3236 (interactive)
3237 (set-buffer rmail-buffer)
3238 (message "Expunging deleted messages...")
3239 ;; Discard all undo records for this buffer.
3240 (or (eq buffer-undo-list t)
3241 (setq buffer-undo-list nil))
3242 (rmail-maybe-set-message-counters)
3243 (let* ((omax (- (buffer-size) (point-max)))
3244 (omin (- (buffer-size) (point-min)))
3245 (opoint (if (and (> rmail-current-message 0)
3246 (rmail-message-deleted-p rmail-current-message))
3248 (if rmail-enable-mime
3249 (with-current-buffer rmail-view-buffer
3250 (- (point)(point-min)))
3251 (- (point) (point-min)))))
3252 (messages-head (cons (aref rmail-message-vector 0) nil))
3253 (messages-tail messages-head)
3254 ;; Don't make any undo records for the expunging.
3255 (buffer-undo-list t)
3256 (win))
3257 (unwind-protect
3258 (save-excursion
3259 (widen)
3260 (goto-char (point-min))
3261 (let ((counter 0)
3262 (number 1)
3263 (total rmail-total-messages)
3264 (new-message-number rmail-current-message)
3265 (new-summary nil)
3266 (new-msgref (list (list 0)))
3267 (rmailbuf (current-buffer))
3268 (buffer-read-only nil)
3269 (messages rmail-message-vector)
3270 (deleted rmail-deleted-vector)
3271 (summary rmail-summary-vector))
3272 (setq rmail-total-messages nil
3273 rmail-current-message nil
3274 rmail-message-vector nil
3275 rmail-deleted-vector nil
3276 rmail-summary-vector nil)
3278 (while (<= number total)
3279 (if (= (aref deleted number) ?D)
3280 (progn
3281 (delete-region
3282 (marker-position (aref messages number))
3283 (marker-position (aref messages (1+ number))))
3284 (move-marker (aref messages number) nil)
3285 (if (> new-message-number counter)
3286 (setq new-message-number (1- new-message-number))))
3287 (setq counter (1+ counter))
3288 (setq messages-tail
3289 (setcdr messages-tail
3290 (cons (aref messages number) nil)))
3291 (setq new-summary
3292 (cons (if (= counter number) (aref summary (1- number)))
3293 new-summary))
3294 (setq new-msgref
3295 (cons (aref rmail-msgref-vector number)
3296 new-msgref))
3297 (setcar (car new-msgref) counter))
3298 (if (zerop (% (setq number (1+ number)) 20))
3299 (message "Expunging deleted messages...%d" number)))
3300 (setq messages-tail
3301 (setcdr messages-tail
3302 (cons (aref messages number) nil)))
3303 (setq rmail-current-message new-message-number
3304 rmail-total-messages counter
3305 rmail-message-vector (apply 'vector messages-head)
3306 rmail-deleted-vector (make-string (1+ counter) ?\ )
3307 rmail-summary-vector (vconcat (nreverse new-summary))
3308 rmail-msgref-vector (apply 'vector (nreverse new-msgref))
3309 win t)))
3310 (message "Expunging deleted messages...done")
3311 (if (not win)
3312 (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
3313 (if (not dont-show)
3314 (rmail-show-message
3315 (if (zerop rmail-current-message) 1 nil)
3316 (if rmail-enable-mime
3317 (goto-char (+ (point-min) opoint))
3318 (goto-char (+ (point) opoint))))))))
3320 (defun rmail-expunge ()
3321 "Erase deleted messages from Rmail file and summary buffer."
3322 (interactive)
3323 (when (rmail-expunge-confirmed)
3324 (rmail-only-expunge)
3325 (if (rmail-summary-exists)
3326 (rmail-select-summary (rmail-update-summary)))))
3328 ;;;; *** Rmail Mailing Commands ***
3330 (defun rmail-start-mail (&optional noerase to subject in-reply-to cc
3331 replybuffer sendactions same-window others)
3332 (let (yank-action)
3333 (if replybuffer
3334 (setq yank-action (list 'insert-buffer replybuffer)))
3335 (setq others (cons (cons "cc" cc) others))
3336 (setq others (cons (cons "in-reply-to" in-reply-to) others))
3337 (if same-window
3338 (compose-mail to subject others
3339 noerase nil
3340 yank-action sendactions)
3341 (if rmail-mail-new-frame
3342 (prog1
3343 (compose-mail to subject others
3344 noerase 'switch-to-buffer-other-frame
3345 yank-action sendactions)
3346 ;; This is not a standard frame parameter;
3347 ;; nothing except sendmail.el looks at it.
3348 (modify-frame-parameters (selected-frame)
3349 '((mail-dedicated-frame . t))))
3350 (compose-mail to subject others
3351 noerase 'switch-to-buffer-other-window
3352 yank-action sendactions)))))
3354 (defun rmail-mail ()
3355 "Send mail in another window.
3356 While composing the message, use \\[mail-yank-original] to yank the
3357 original message into it."
3358 (interactive)
3359 (rmail-start-mail nil nil nil nil nil rmail-view-buffer))
3361 (defun rmail-continue ()
3362 "Continue composing outgoing message previously being composed."
3363 (interactive)
3364 (rmail-start-mail t))
3366 (defun rmail-reply (just-sender)
3367 "Reply to the current message.
3368 Normally include CC: to all other recipients of original message;
3369 prefix argument means ignore them. While composing the reply,
3370 use \\[mail-yank-original] to yank the original message into it."
3371 (interactive "P")
3372 (let (from reply-to cc subject date to message-id references
3373 resent-to resent-cc resent-reply-to
3374 (msgnum rmail-current-message))
3375 (save-excursion
3376 (save-restriction
3377 (if rmail-enable-mime
3378 (narrow-to-region
3379 (goto-char (point-min))
3380 (if (search-forward "\n\n" nil 'move)
3381 (1+ (match-beginning 0))
3382 (point)))
3383 (widen)
3384 (goto-char (rmail-msgbeg rmail-current-message))
3385 (forward-line 1)
3386 (if (= (following-char) ?0)
3387 (narrow-to-region
3388 (progn (forward-line 2)
3389 (point))
3390 (progn (search-forward "\n\n" (rmail-msgend rmail-current-message)
3391 'move)
3392 (point)))
3393 (narrow-to-region (point)
3394 (progn (search-forward "\n*** EOOH ***\n")
3395 (beginning-of-line) (point)))))
3396 (setq from (mail-fetch-field "from")
3397 reply-to (or (if just-sender
3398 (mail-fetch-field "mail-reply-to" nil t)
3399 (mail-fetch-field "mail-followup-to" nil t))
3400 (mail-fetch-field "reply-to" nil t)
3401 from)
3402 cc (and (not just-sender)
3403 ;; mail-followup-to, if given, overrides cc.
3404 (not (mail-fetch-field "mail-followup-to" nil t))
3405 (mail-fetch-field "cc" nil t))
3406 subject (mail-fetch-field "subject")
3407 date (mail-fetch-field "date")
3408 to (or (mail-fetch-field "to" nil t) "")
3409 message-id (mail-fetch-field "message-id")
3410 references (mail-fetch-field "references" nil nil t)
3411 resent-reply-to (mail-fetch-field "resent-reply-to" nil t)
3412 resent-cc (and (not just-sender)
3413 (mail-fetch-field "resent-cc" nil t))
3414 resent-to (or (mail-fetch-field "resent-to" nil t) "")
3415 ;;; resent-subject (mail-fetch-field "resent-subject")
3416 ;;; resent-date (mail-fetch-field "resent-date")
3417 ;;; resent-message-id (mail-fetch-field "resent-message-id")
3419 ;; Merge the resent-to and resent-cc into the to and cc.
3420 (if (and resent-to (not (equal resent-to "")))
3421 (if (not (equal to ""))
3422 (setq to (concat to ", " resent-to))
3423 (setq to resent-to)))
3424 (if (and resent-cc (not (equal resent-cc "")))
3425 (if (not (equal cc ""))
3426 (setq cc (concat cc ", " resent-cc))
3427 (setq cc resent-cc)))
3428 ;; Add `Re: ' to subject if not there already.
3429 (and (stringp subject)
3430 (setq subject
3431 (concat rmail-reply-prefix
3432 (if (let ((case-fold-search t))
3433 (string-match rmail-reply-regexp subject))
3434 (substring subject (match-end 0))
3435 subject))))
3436 (rmail-start-mail
3438 ;; Using mail-strip-quoted-names is undesirable with newer mailers
3439 ;; since they can handle the names unstripped.
3440 ;; I don't know whether there are other mailers that still
3441 ;; need the names to be stripped.
3442 ;;; (mail-strip-quoted-names reply-to)
3443 reply-to
3444 subject
3445 (rmail-make-in-reply-to-field from date message-id)
3446 (if just-sender
3448 ;; mail-strip-quoted-names is NOT necessary for rmail-dont-reply-to
3449 ;; to do its job.
3450 (let* ((cc-list (rmail-dont-reply-to
3451 (mail-strip-quoted-names
3452 (if (null cc) to (concat to ", " cc))))))
3453 (if (string= cc-list "") nil cc-list)))
3454 rmail-view-buffer
3455 (list (list 'rmail-mark-message
3456 rmail-buffer
3457 (with-current-buffer rmail-buffer
3458 (aref rmail-msgref-vector msgnum))
3459 "answered"))
3461 (list (cons "References" (concat (mapconcat 'identity references " ")
3462 " " message-id))))))
3464 (defun rmail-mark-message (buffer msgnum-list attribute)
3465 "Give BUFFER's message number in MSGNUM-LIST the attribute ATTRIBUTE.
3466 This is use in the send-actions for message buffers.
3467 MSGNUM-LIST is a list of the form (MSGNUM)
3468 which is an element of rmail-msgref-vector."
3469 (save-excursion
3470 (set-buffer buffer)
3471 (if (car msgnum-list)
3472 (rmail-set-attribute attribute t (car msgnum-list)))))
3474 (defun rmail-make-in-reply-to-field (from date message-id)
3475 (cond ((not from)
3476 (if message-id
3477 message-id
3478 nil))
3479 (mail-use-rfc822
3480 (require 'rfc822)
3481 (let ((tem (car (rfc822-addresses from))))
3482 (if message-id
3483 (if (or (not tem)
3484 (string-match
3485 (regexp-quote (if (string-match "@[^@]*\\'" tem)
3486 (substring tem 0
3487 (match-beginning 0))
3488 tem))
3489 message-id))
3490 ;; missing From, or Message-ID is sufficiently informative
3491 message-id
3492 (concat message-id " (" tem ")"))
3493 ;; Copy TEM, discarding text properties.
3494 (setq tem (copy-sequence tem))
3495 (set-text-properties 0 (length tem) nil tem)
3496 (setq tem (copy-sequence tem))
3497 ;; Use prin1 to fake RFC822 quoting
3498 (let ((field (prin1-to-string tem)))
3499 (if date
3500 (concat field "'s message of " date)
3501 field)))))
3502 ((let* ((foo "[^][\000-\037()<>@,;:\\\" ]+")
3503 (bar "[^][\000-\037()<>@,;:\\\"]+"))
3504 ;; These strings both match all non-ASCII characters.
3505 (or (string-match (concat "\\`[ \t]*\\(" bar
3506 "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
3507 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
3508 from)
3509 (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
3510 bar "\\))[ \t]*\\'")
3511 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
3512 from)))
3513 (let ((start (match-beginning 1))
3514 (end (match-end 1)))
3515 ;; Trim whitespace which above regexp match allows
3516 (while (and (< start end)
3517 (memq (aref from start) '(?\t ?\ )))
3518 (setq start (1+ start)))
3519 (while (and (< start end)
3520 (memq (aref from (1- end)) '(?\t ?\ )))
3521 (setq end (1- end)))
3522 (let ((field (substring from start end)))
3523 (if date (setq field (concat "message from " field " on " date)))
3524 (if message-id
3525 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
3526 (concat message-id " (" field ")")
3527 field))))
3529 ;; If we can't kludge it simply, do it correctly
3530 (let ((mail-use-rfc822 t))
3531 (rmail-make-in-reply-to-field from date message-id)))))
3533 (defun rmail-forward (resend)
3534 "Forward the current message to another user.
3535 With prefix argument, \"resend\" the message instead of forwarding it;
3536 see the documentation of `rmail-resend'."
3537 (interactive "P")
3538 (if resend
3539 (call-interactively 'rmail-resend)
3540 (let ((forward-buffer rmail-buffer)
3541 (msgnum rmail-current-message)
3542 (subject (concat "["
3543 (let ((from (or (mail-fetch-field "From")
3544 (mail-fetch-field ">From"))))
3545 (if from
3546 (concat (mail-strip-quoted-names from) ": ")
3547 ""))
3548 (or (mail-fetch-field "Subject") "")
3549 "]")))
3550 (if (rmail-start-mail
3551 nil nil subject nil nil nil
3552 (list (list 'rmail-mark-message
3553 forward-buffer
3554 (with-current-buffer rmail-buffer
3555 (aref rmail-msgref-vector msgnum))
3556 "forwarded"))
3557 ;; If only one window, use it for the mail buffer.
3558 ;; Otherwise, use another window for the mail buffer
3559 ;; so that the Rmail buffer remains visible
3560 ;; and sending the mail will get back to it.
3561 (and (not rmail-mail-new-frame) (one-window-p t)))
3562 ;; The mail buffer is now current.
3563 (save-excursion
3564 ;; Insert after header separator--before signature if any.
3565 (goto-char (mail-text-start))
3566 (if (or rmail-enable-mime rmail-enable-mime-composing)
3567 (funcall rmail-insert-mime-forwarded-message-function
3568 forward-buffer)
3569 (insert "------- Start of forwarded message -------\n")
3570 ;; Quote lines with `- ' if they start with `-'.
3571 (let ((beg (point)) end)
3572 (setq end (point-marker))
3573 (set-marker-insertion-type end t)
3574 (insert-buffer-substring forward-buffer)
3575 (goto-char beg)
3576 (while (re-search-forward "^-" end t)
3577 (beginning-of-line)
3578 (insert "- ")
3579 (forward-line 1))
3580 (goto-char end)
3581 (skip-chars-backward "\n")
3582 (if (< (point) end)
3583 (forward-char 1))
3584 (delete-region (point) end)
3585 (set-marker end nil))
3586 (insert "------- End of forwarded message -------\n"))
3587 (push-mark))))))
3589 (defun rmail-resend (address &optional from comment mail-alias-file)
3590 "Resend current message to ADDRESSES.
3591 ADDRESSES should be a single address, a string consisting of several
3592 addresses separated by commas, or a list of addresses.
3594 Optional FROM is the address to resend the message from, and
3595 defaults from the value of `user-mail-address'.
3596 Optional COMMENT is a string to insert as a comment in the resent message.
3597 Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
3598 typically for purposes of moderating a list."
3599 (interactive "sResend to: ")
3600 (require 'sendmail)
3601 (require 'mailalias)
3602 (unless (or (eq rmail-view-buffer (current-buffer))
3603 (eq rmail-buffer (current-buffer)))
3604 (error "Not an Rmail buffer"))
3605 (if (not from) (setq from user-mail-address))
3606 (let ((tembuf (generate-new-buffer " sendmail temp"))
3607 (case-fold-search nil)
3608 (mail-personal-alias-file
3609 (or mail-alias-file mail-personal-alias-file))
3610 (mailbuf rmail-buffer))
3611 (unwind-protect
3612 (with-current-buffer tembuf
3613 ;;>> Copy message into temp buffer
3614 (if rmail-enable-mime
3615 (funcall rmail-insert-mime-resent-message-function mailbuf)
3616 (insert-buffer-substring mailbuf))
3617 (goto-char (point-min))
3618 ;; Delete any Sender field, since that's not specifiable.
3619 ; Only delete Sender fields in the actual header.
3620 (re-search-forward "^$" nil 'move)
3621 ; Using "while" here rather than "if" because some buggy mail
3622 ; software may have inserted multiple Sender fields.
3623 (while (re-search-backward "^Sender:" nil t)
3624 (let (beg)
3625 (setq beg (point))
3626 (forward-line 1)
3627 (while (looking-at "[ \t]")
3628 (forward-line 1))
3629 (delete-region beg (point))))
3630 ; Go back to the beginning of the buffer so the Resent- fields
3631 ; are inserted there.
3632 (goto-char (point-min))
3633 ;;>> Insert resent-from:
3634 (insert "Resent-From: " from "\n")
3635 (insert "Resent-Date: " (mail-rfc822-date) "\n")
3636 ;;>> Insert resent-to: and bcc if need be.
3637 (let ((before (point)))
3638 (if mail-self-blind
3639 (insert "Resent-Bcc: " (user-login-name) "\n"))
3640 (insert "Resent-To: " (if (stringp address)
3641 address
3642 (mapconcat 'identity address ",\n\t"))
3643 "\n")
3644 ;; Expand abbrevs in the recipients.
3645 (save-excursion
3646 (if (featurep 'mailabbrev)
3647 (let ((end (point-marker))
3648 (local-abbrev-table mail-abbrevs)
3649 (old-syntax-table (syntax-table)))
3650 (if (and (not (vectorp mail-abbrevs))
3651 (file-exists-p mail-personal-alias-file))
3652 (build-mail-abbrevs))
3653 (unless mail-abbrev-syntax-table
3654 (mail-abbrev-make-syntax-table))
3655 (set-syntax-table mail-abbrev-syntax-table)
3656 (goto-char before)
3657 (while (and (< (point) end)
3658 (progn (forward-word 1)
3659 (<= (point) end)))
3660 (expand-abbrev))
3661 (set-syntax-table old-syntax-table))
3662 (expand-mail-aliases before (point)))))
3663 ;;>> Set up comment, if any.
3664 (if (and (sequencep comment) (not (zerop (length comment))))
3665 (let ((before (point))
3666 after)
3667 (insert comment)
3668 (or (eolp) (insert "\n"))
3669 (setq after (point))
3670 (goto-char before)
3671 (while (< (point) after)
3672 (insert "Resent-Comment: ")
3673 (forward-line 1))))
3674 ;; Don't expand aliases in the destination fields
3675 ;; of the original message.
3676 (let (mail-aliases)
3677 (funcall send-mail-function)))
3678 (kill-buffer tembuf))
3679 (with-current-buffer rmail-buffer
3680 (rmail-set-attribute "resent" t rmail-current-message))))
3682 (defvar mail-unsent-separator
3683 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
3684 "^ *---+ +Returned message +---+ *$\\|"
3685 "^ *---+ *Returned mail follows *---+ *$\\|"
3686 "^Start of returned message$\\|"
3687 "^---+ Below this line is a copy of the message.$\\|"
3688 "^ *---+ +Original message +---+ *$\\|"
3689 "^ *--+ +begin message +--+ *$\\|"
3690 "^ *---+ +Original message follows +---+ *$\\|"
3691 "^ *---+ +Your message follows +---+ *$\\|"
3692 "^|? *---+ +Message text follows: +---+ *|?$\\|"
3693 "^ *---+ +This is a copy of \\w+ message, including all the headers.*---+ *$")
3694 "A regexp that matches the separator before the text of a failed message.")
3696 (defvar mail-mime-unsent-header "^Content-Type: message/rfc822 *$"
3697 "A regexp that matches the header of a MIME body part with a failed message.")
3699 (defun rmail-retry-failure ()
3700 "Edit a mail message which is based on the contents of the current message.
3701 For a message rejected by the mail system, extract the interesting headers and
3702 the body of the original message.
3703 If the failed message is a MIME multipart message, it is searched for a
3704 body part with a header which matches the variable `mail-mime-unsent-header'.
3705 Otherwise, the variable `mail-unsent-separator' should match the string that
3706 delimits the returned original message.
3707 The variable `rmail-retry-ignored-headers' is a regular expression
3708 specifying headers which should not be copied into the new message."
3709 (interactive)
3710 (require 'mail-utils)
3711 (let ((rmail-this-buffer (current-buffer))
3712 (msgnum rmail-current-message)
3713 bounce-start bounce-end bounce-indent resending
3714 ;; Fetch any content-type header in current message
3715 ;; Must search thru the whole unpruned header.
3716 (content-type
3717 (save-excursion
3718 (save-restriction
3719 (rmail-narrow-to-non-pruned-header)
3720 (mail-fetch-field "Content-Type") ))))
3721 (save-excursion
3722 (goto-char (point-min))
3723 (let ((case-fold-search t))
3724 (if (and content-type
3725 (string-match
3726 ";[\n\t ]*boundary=\"?\\([-0-9a-z'()+_,./:=? ]+\\)\"?"
3727 content-type))
3728 ;; Handle a MIME multipart bounce message.
3729 (let ((codestring
3730 (concat "\n--"
3731 (substring content-type (match-beginning 1)
3732 (match-end 1)))))
3733 (unless (re-search-forward mail-mime-unsent-header nil t)
3734 (error "Cannot find beginning of header in failed message"))
3735 (unless (search-forward "\n\n" nil t)
3736 (error "Cannot find start of Mime data in failed message"))
3737 (setq bounce-start (point))
3738 (if (search-forward codestring nil t)
3739 (setq bounce-end (match-beginning 0))
3740 (setq bounce-end (point-max))))
3741 ;; Non-MIME bounce.
3742 (or (re-search-forward mail-unsent-separator nil t)
3743 (error "Cannot parse this as a failure message"))
3744 (skip-chars-forward "\n")
3745 ;; Support a style of failure message in which the original
3746 ;; message is indented, and included within lines saying
3747 ;; `Start of returned message' and `End of returned message'.
3748 (if (looking-at " +Received:")
3749 (progn
3750 (setq bounce-start (point))
3751 (skip-chars-forward " ")
3752 (setq bounce-indent (- (current-column)))
3753 (goto-char (point-max))
3754 (re-search-backward "^End of returned message$" nil t)
3755 (setq bounce-end (point)))
3756 ;; One message contained a few random lines before
3757 ;; the old message header. The first line of the
3758 ;; message started with two hyphens. A blank line
3759 ;; followed these random lines. The same line
3760 ;; beginning with two hyphens was possibly marking
3761 ;; the end of the message.
3762 (if (looking-at "^--")
3763 (let ((boundary (buffer-substring-no-properties
3764 (point)
3765 (progn (end-of-line) (point)))))
3766 (search-forward "\n\n")
3767 (skip-chars-forward "\n")
3768 (setq bounce-start (point))
3769 (goto-char (point-max))
3770 (search-backward (concat "\n\n" boundary) bounce-start t)
3771 (setq bounce-end (point)))
3772 (setq bounce-start (point)
3773 bounce-end (point-max)))
3774 (unless (search-forward "\n\n" nil t)
3775 (error "Cannot find end of header in failed message"))))))
3776 ;; We have found the message that bounced, within the current message.
3777 ;; Now start sending new message; default header fields from original.
3778 ;; Turn off the usual actions for initializing the message body
3779 ;; because we want to get only the text from the failure message.
3780 (let (mail-signature mail-setup-hook)
3781 (if (rmail-start-mail nil nil nil nil nil rmail-this-buffer
3782 (list (list 'rmail-mark-message
3783 rmail-this-buffer
3784 (aref rmail-msgref-vector msgnum)
3785 "retried")))
3786 ;; Insert original text as initial text of new draft message.
3787 ;; Bind inhibit-read-only since the header delimiter
3788 ;; of the previous message was probably read-only.
3789 (let ((inhibit-read-only t)
3790 rmail-displayed-headers
3791 rmail-ignored-headers)
3792 (erase-buffer)
3793 (insert-buffer-substring rmail-this-buffer
3794 bounce-start bounce-end)
3795 (goto-char (point-min))
3796 (if bounce-indent
3797 (indent-rigidly (point-min) (point-max) bounce-indent))
3798 (rmail-clear-headers rmail-retry-ignored-headers)
3799 (rmail-clear-headers "^sender:\\|^return-path:\\|^received:")
3800 (mail-sendmail-delimit-header)
3801 (save-restriction
3802 (narrow-to-region (point-min) (mail-header-end))
3803 (setq resending (mail-fetch-field "resent-to"))
3804 (if mail-self-blind
3805 (if resending
3806 (insert "Resent-Bcc: " (user-login-name) "\n")
3807 (insert "BCC: " (user-login-name) "\n"))))
3808 (goto-char (point-min))
3809 (mail-position-on-field (if resending "Resent-To" "To") t))))))
3811 (defun rmail-summary-exists ()
3812 "Non-nil iff in an RMAIL buffer and an associated summary buffer exists.
3813 In fact, the non-nil value returned is the summary buffer itself."
3814 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
3815 rmail-summary-buffer))
3817 (defun rmail-summary-displayed ()
3818 "t iff in RMAIL buffer and an associated summary buffer is displayed."
3819 (and rmail-summary-buffer (get-buffer-window rmail-summary-buffer)))
3821 (defcustom rmail-redisplay-summary nil
3822 "*Non-nil means Rmail should show the summary when it changes.
3823 This has an effect only if a summary buffer exists."
3824 :type 'boolean
3825 :group 'rmail-summary)
3827 (defcustom rmail-summary-window-size nil
3828 "*Non-nil means specify the height for an Rmail summary window."
3829 :type '(choice (const :tag "Disabled" nil) integer)
3830 :group 'rmail-summary)
3832 ;; Put the summary buffer back on the screen, if user wants that.
3833 (defun rmail-maybe-display-summary ()
3834 (let ((selected (selected-window))
3835 window)
3836 ;; If requested, make sure the summary is displayed.
3837 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
3838 rmail-redisplay-summary
3839 (if (get-buffer-window rmail-summary-buffer 0)
3840 ;; It's already in some frame; show that one.
3841 (let ((frame (window-frame
3842 (get-buffer-window rmail-summary-buffer 0))))
3843 (make-frame-visible frame)
3844 (raise-frame frame))
3845 (display-buffer rmail-summary-buffer)))
3846 ;; If requested, set the height of the summary window.
3847 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
3848 rmail-summary-window-size
3849 (setq window (get-buffer-window rmail-summary-buffer))
3850 ;; Don't try to change the size if just one window in frame.
3851 (not (eq window (frame-root-window (window-frame window))))
3852 (unwind-protect
3853 (progn
3854 (select-window window)
3855 (enlarge-window (- rmail-summary-window-size (window-height))))
3856 (select-window selected)))))
3858 ;;;; *** Rmail Local Fontification ***
3860 (defun rmail-fontify-buffer-function ()
3861 ;; This function's symbol is bound to font-lock-fontify-buffer-function.
3862 (add-hook 'rmail-show-message-hook 'rmail-fontify-message nil t)
3863 ;; If we're already showing a message, fontify it now.
3864 (if rmail-current-message (rmail-fontify-message))
3865 ;; Prevent Font Lock mode from kicking in.
3866 (setq font-lock-fontified t))
3868 (defun rmail-unfontify-buffer-function ()
3869 ;; This function's symbol is bound to font-lock-fontify-unbuffer-function.
3870 (let ((modified (buffer-modified-p))
3871 (buffer-undo-list t) (inhibit-read-only t)
3872 before-change-functions after-change-functions
3873 buffer-file-name buffer-file-truename)
3874 (save-restriction
3875 (widen)
3876 (remove-hook 'rmail-show-message-hook 'rmail-fontify-message t)
3877 (remove-text-properties (point-min) (point-max) '(rmail-fontified nil))
3878 (font-lock-default-unfontify-buffer)
3879 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))
3881 (defun rmail-fontify-message ()
3882 ;; Fontify the current message if it is not already fontified.
3883 (if (text-property-any (point-min) (point-max) 'rmail-fontified nil)
3884 (let ((modified (buffer-modified-p))
3885 (buffer-undo-list t) (inhibit-read-only t)
3886 before-change-functions after-change-functions
3887 buffer-file-name buffer-file-truename)
3888 (save-excursion
3889 (save-match-data
3890 (add-text-properties (point-min) (point-max) '(rmail-fontified t))
3891 (font-lock-fontify-region (point-min) (point-max))
3892 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))))
3894 ;;; Speedbar support for RMAIL files.
3895 (eval-when-compile (require 'speedbar))
3897 (defvar rmail-speedbar-match-folder-regexp "^[A-Z0-9]+\\(\\.[A-Z0-9]+\\)?$"
3898 "*This regex is used to match folder names to be displayed in speedbar.
3899 Enabling this will permit speedbar to display your folders for easy
3900 browsing, and moving of messages.")
3902 (defvar rmail-speedbar-last-user nil
3903 "The last user to be displayed in the speedbar.")
3905 (defvar rmail-speedbar-key-map nil
3906 "Keymap used when in rmail display mode.")
3908 (defun rmail-install-speedbar-variables ()
3909 "Install those variables used by speedbar to enhance rmail."
3910 (if rmail-speedbar-key-map
3912 (setq rmail-speedbar-key-map (speedbar-make-specialized-keymap))
3914 (define-key rmail-speedbar-key-map "e" 'speedbar-edit-line)
3915 (define-key rmail-speedbar-key-map "r" 'speedbar-edit-line)
3916 (define-key rmail-speedbar-key-map "\C-m" 'speedbar-edit-line)
3917 (define-key rmail-speedbar-key-map "M"
3918 'rmail-speedbar-move-message-to-folder-on-line)))
3920 (defvar rmail-speedbar-menu-items
3921 '(["Read Folder" speedbar-edit-line t]
3922 ["Move message to folder" rmail-speedbar-move-message-to-folder-on-line
3923 (save-excursion (beginning-of-line)
3924 (looking-at "<M> "))])
3925 "Additional menu-items to add to speedbar frame.")
3927 ;; Make sure our special speedbar major mode is loaded
3928 (if (featurep 'speedbar)
3929 (rmail-install-speedbar-variables)
3930 (add-hook 'speedbar-load-hook 'rmail-install-speedbar-variables))
3932 (defun rmail-speedbar-buttons (buffer)
3933 "Create buttons for BUFFER containing rmail messages.
3934 Click on the address under Reply to: to reply to this person.
3935 Under Folders: Click a name to read it, or on the <M> to move the
3936 current message into that RMAIL folder."
3937 (let ((from nil))
3938 (save-excursion
3939 (set-buffer buffer)
3940 (goto-char (point-min))
3941 (if (not (re-search-forward "^Reply-To: " nil t))
3942 (if (not (re-search-forward "^From:? " nil t))
3943 (setq from t)))
3944 (if from
3946 (setq from (buffer-substring (point) (save-excursion
3947 (end-of-line)
3948 (point))))))
3949 (goto-char (point-min))
3950 (if (and (looking-at "Reply to:")
3951 (equal from rmail-speedbar-last-user))
3953 (setq rmail-speedbar-last-user from)
3954 (erase-buffer)
3955 (insert "Reply To:\n")
3956 (if (stringp from)
3957 (speedbar-insert-button from 'speedbar-directory-face 'highlight
3958 'rmail-speedbar-button 'rmail-reply))
3959 (insert "Folders:\n")
3960 (let* ((case-fold-search nil)
3961 (df (directory-files (save-excursion (set-buffer buffer)
3962 default-directory)
3963 nil rmail-speedbar-match-folder-regexp)))
3964 (while df
3965 (speedbar-insert-button "<M>" 'speedbar-button-face 'highlight
3966 'rmail-speedbar-move-message (car df))
3967 (speedbar-insert-button (car df) 'speedbar-file-face 'highlight
3968 'rmail-speedbar-find-file nil t)
3969 (setq df (cdr df)))))))
3971 (defun rmail-speedbar-button (text token indent)
3972 "Execute an rmail command specified by TEXT.
3973 The command used is TOKEN. INDENT is not used."
3974 (speedbar-with-attached-buffer
3975 (funcall token t)))
3977 (defun rmail-speedbar-find-file (text token indent)
3978 "Load in the rmail file TEXT.
3979 TOKEN and INDENT are not used."
3980 (speedbar-with-attached-buffer
3981 (message "Loading in RMAIL file %s..." text)
3982 (find-file text)))
3984 (defun rmail-speedbar-move-message-to-folder-on-line ()
3985 "If the current line is a folder, move current message to it."
3986 (interactive)
3987 (save-excursion
3988 (beginning-of-line)
3989 (if (re-search-forward "<M> " (save-excursion (end-of-line) (point)) t)
3990 (progn
3991 (forward-char -2)
3992 (speedbar-do-function-pointer)))))
3994 (defun rmail-speedbar-move-message (text token indent)
3995 "From button TEXT, copy current message to the rmail file specified by TOKEN.
3996 TEXT and INDENT are not used."
3997 (speedbar-with-attached-buffer
3998 (message "Moving message to %s" token)
3999 (rmail-output-to-rmail-file token)))
4001 ; Functions for setting, getting and encoding the POP password.
4002 ; The password is encoded to prevent it from being easily accessible
4003 ; to "prying eyes." Obviously, this encoding isn't "real security,"
4004 ; nor is it meant to be.
4006 ;;;###autoload
4007 (defun rmail-set-remote-password (password)
4008 "Set PASSWORD to be used for retrieving mail from a POP or IMAP server."
4009 (interactive "sPassword: ")
4010 (if password
4011 (setq rmail-encoded-remote-password
4012 (rmail-encode-string password (emacs-pid)))
4013 (setq rmail-remote-password nil)
4014 (setq rmail-encoded-remote-password nil)))
4016 (defun rmail-get-remote-password (imap)
4017 "Get the password for retrieving mail from a POP or IMAP server. If none
4018 has been set, then prompt the user for one."
4019 (when (not rmail-encoded-remote-password)
4020 (if (not rmail-remote-password)
4021 (setq rmail-remote-password
4022 (read-passwd (if imap
4023 "IMAP password: "
4024 "POP password: "))))
4025 (rmail-set-remote-password rmail-remote-password)
4026 (setq rmail-remote-password nil))
4027 (rmail-encode-string rmail-encoded-remote-password (emacs-pid)))
4029 (defun rmail-have-password ()
4030 (or rmail-remote-password rmail-encoded-remote-password))
4032 (defun rmail-encode-string (string mask)
4033 "Encode STRING with integer MASK, by taking the exclusive OR of the
4034 lowest byte in the mask with the first character of string, the
4035 second-lowest-byte with the second character of the string, etc.,
4036 restarting at the lowest byte of the mask whenever it runs out.
4037 Returns the encoded string. Calling the function again with an
4038 encoded string (and the same mask) will decode the string."
4039 (setq mask (abs mask)) ; doesn't work if negative
4040 (let* ((string-vector (string-to-vector string)) (i 0)
4041 (len (length string-vector)) (curmask mask) charmask)
4042 (while (< i len)
4043 (if (= curmask 0)
4044 (setq curmask mask))
4045 (setq charmask (% curmask 256))
4046 (setq curmask (lsh curmask -8))
4047 (aset string-vector i (logxor charmask (aref string-vector i)))
4048 (setq i (1+ i)))
4049 (concat string-vector)))
4051 ;;;; Desktop support
4053 ;;;###autoload
4054 (defun rmail-restore-desktop-buffer (desktop-buffer-file-name
4055 desktop-buffer-name
4056 desktop-buffer-misc)
4057 "Restore an rmail buffer specified in a desktop file."
4058 (condition-case error
4059 (progn
4060 (rmail-input desktop-buffer-file-name)
4061 (if (eq major-mode 'rmail-mode)
4062 (current-buffer)
4063 rmail-buffer))
4064 (file-locked
4065 (kill-buffer (current-buffer))
4066 nil)))
4068 (provide 'rmail)
4070 ;;; arch-tag: cff0a950-57fe-4f73-a86e-91ff75afd06c
4071 ;;; rmail.el ends here