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