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