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