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