Bump version to 23.2.92.
[emacs.git] / lisp / mail / rmail.el
blob370999dabf85319ac43f5b2b49ceef5b277b0a73
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, 2011
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 (with-current-buffer rmail-view-buffer
2695 ;; We give the view buffer a buffer-local value of
2696 ;; rmail-header-style based on the binding in effect when
2697 ;; this function is called; `rmail-toggle-headers' can
2698 ;; inspect this value to determine how to toggle.
2699 (set (make-local-variable 'rmail-header-style) header-style))
2700 (if (and rmail-enable-mime
2701 (re-search-forward "mime-version: 1.0" nil t))
2702 (let ((rmail-buffer mbox-buf)
2703 (rmail-view-buffer view-buf))
2704 (funcall rmail-show-mime-function))
2705 (setq body-start (search-forward "\n\n" nil t))
2706 (narrow-to-region beg (point))
2707 (goto-char beg)
2708 (save-excursion
2709 (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t)
2710 (setq coding-system (intern (match-string 1)))
2711 (setq coding-system (rmail-get-coding-system))))
2712 (setq character-coding (mail-fetch-field "content-transfer-encoding")
2713 is-text-message (rmail-is-text-p))
2714 (if character-coding
2715 (setq character-coding (downcase character-coding)))
2716 (narrow-to-region beg end)
2717 ;; Decode the message body into an empty view buffer using a
2718 ;; unibyte temporary buffer where the character decoding takes
2719 ;; place.
2720 (with-current-buffer rmail-view-buffer
2721 (erase-buffer))
2722 (if (null character-coding)
2723 ;; Do it directly since that is fast.
2724 (rmail-decode-region body-start end coding-system view-buf)
2725 ;; Can this be done directly, skipping the temp buffer?
2726 (with-temp-buffer
2727 (set-buffer-multibyte nil)
2728 (insert-buffer-substring mbox-buf body-start end)
2729 (cond
2730 ((string= character-coding "quoted-printable")
2731 ;; See bug#5441.
2732 (or (mail-unquote-printable-region (point-min) (point-max)
2733 nil t 'unibyte)
2734 (message "Malformed MIME quoted-printable message")))
2735 ((and (string= character-coding "base64") is-text-message)
2736 (condition-case err
2737 (base64-decode-region (point-min) (point-max))
2738 (error (message "%s" (cdr err)))))
2739 ((eq character-coding 'uuencode)
2740 (error "uuencoded messages are not supported yet"))
2741 (t))
2742 (rmail-decode-region (point-min) (point-max)
2743 coding-system view-buf)))
2744 (with-current-buffer rmail-view-buffer
2745 ;; Prepare the separator (blank line) before the body.
2746 (goto-char (point-min))
2747 (insert "\n")
2748 ;; Unquote quoted From lines
2749 (while (re-search-forward "^>+From " nil t)
2750 (beginning-of-line)
2751 (delete-char 1)
2752 (forward-line))
2753 (goto-char (point-min)))
2754 ;; Copy the headers to the front of the message view buffer.
2755 (rmail-copy-headers beg end))
2756 ;; highlight the message, activate any URL like text and add
2757 ;; special highlighting for and quoted material.
2758 (with-current-buffer rmail-view-buffer
2759 (goto-char (point-min))
2760 (rmail-highlight-headers)
2761 ;(rmail-activate-urls)
2762 ;(rmail-process-quoted-material)
2764 ;; Update the mode-line with message status information and swap
2765 ;; the view buffer/mail buffer contents.
2766 (rmail-display-labels)
2767 (rmail-swap-buffers)
2768 (setq rmail-buffer-swapped t)
2769 (run-hooks 'rmail-show-message-hook)
2770 (when (> (- end beg) rmail-show-message-verbose-min)
2771 (message "Showing message %d...done" msg))))
2772 blurb))
2774 (defun rmail-copy-headers (beg end &optional ignored-headers)
2775 "Copy displayed header fields to the message viewer buffer.
2776 BEG and END marks the start and end positions of the message in
2777 the mbox buffer. If the optional argument IGNORED-HEADERS is
2778 non-nil, ignore all header fields whose names match that regexp.
2779 Otherwise, if `rmail-displayed-headers' is non-nil, copy only
2780 those header fields whose names match that regexp. Otherwise,
2781 copy all header fields whose names do not match
2782 `rmail-ignored-headers' (unless they also match
2783 `rmail-nonignored-headers'). Moves point in the message viewer
2784 buffer to the end of the headers."
2785 (let ((header-start-regexp "\n[^ \t]")
2786 lim)
2787 (with-current-buffer rmail-buffer
2788 (when (search-forward "\n\n" nil t)
2789 (forward-char -1)
2790 (save-restriction
2791 ;; Put point right after the From header line.
2792 (narrow-to-region beg (point))
2793 (goto-char (point-min))
2794 (unless (re-search-forward header-start-regexp nil t)
2795 (rmail-error-bad-format))
2796 (forward-char -1)
2797 (cond
2798 ;; Handle the case where all headers should be copied.
2799 ((eq rmail-header-style 'full)
2800 (prepend-to-buffer rmail-view-buffer beg (point-max))
2801 ;; rmail-show-message-1 expects this function to leave point
2802 ;; at the end of the headers.
2804 (let ((len (- (point-max) beg)))
2805 (with-current-buffer rmail-view-buffer
2806 (goto-char (1+ len)))))
2808 ;; Handle the case where the headers matching the displayed
2809 ;; headers regexp should be copied.
2810 ((and rmail-displayed-headers (null ignored-headers))
2811 (while (not (eobp))
2812 (save-excursion
2813 (setq lim (if (re-search-forward header-start-regexp nil t)
2814 (1+ (match-beginning 0))
2815 (point-max))))
2816 (when (looking-at rmail-displayed-headers)
2817 (append-to-buffer rmail-view-buffer (point) lim))
2818 (goto-char lim)))
2819 ;; Handle the ignored headers.
2820 ((or ignored-headers (setq ignored-headers rmail-ignored-headers))
2821 (while (and ignored-headers (not (eobp)))
2822 (save-excursion
2823 (setq lim (if (re-search-forward header-start-regexp nil t)
2824 (1+ (match-beginning 0))
2825 (point-max))))
2826 (if (and (looking-at ignored-headers)
2827 (not (looking-at rmail-nonignored-headers)))
2828 (goto-char lim)
2829 (append-to-buffer rmail-view-buffer (point) lim)
2830 (goto-char lim))))
2831 (t (error "No headers selected for display!"))))))))
2833 (defun rmail-redecode-body (coding)
2834 "Decode the body of the current message using coding system CODING.
2835 This is useful with mail messages that have malformed or missing
2836 charset= headers.
2838 This function assumes that the current message is already decoded
2839 and displayed in the RMAIL buffer, but the coding system used to
2840 decode it was incorrect. It then decodes the message again,
2841 using the coding system CODING."
2842 (interactive "zCoding system for re-decoding this message: ")
2843 (when (not rmail-enable-mime)
2844 (with-current-buffer rmail-buffer
2845 (rmail-swap-buffers-maybe)
2846 (save-restriction
2847 (widen)
2848 (let ((msgbeg (rmail-msgbeg rmail-current-message))
2849 (msgend (rmail-msgend rmail-current-message))
2850 (buffer-read-only nil)
2851 body-start x-coding-header old-coding)
2852 (narrow-to-region msgbeg msgend)
2853 (goto-char (point-min))
2854 (unless (setq body-start (search-forward "\n\n" (point-max) 1))
2855 (error "No message body"))
2857 (save-restriction
2858 ;; Narrow to headers
2859 (narrow-to-region (point-min) body-start)
2860 (setq x-coding-header (goto-char (point-min)))
2861 (if (not (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t))
2862 (setq old-coding (rmail-get-coding-system))
2863 (setq old-coding (intern (match-string 1)))
2864 (setq x-coding-header (point)))
2865 (check-coding-system old-coding)
2866 ;; Make sure the new coding system uses the same EOL
2867 ;; conversion, to prevent ^M characters from popping up
2868 ;; all over the place.
2869 (let ((eol-type (coding-system-eol-type old-coding)))
2870 (if (numberp eol-type)
2871 (setq coding
2872 (coding-system-change-eol-conversion coding eol-type))))
2873 (when (not (coding-system-equal
2874 (coding-system-base old-coding)
2875 (coding-system-base coding)))
2876 ;; Rewrite the coding-system header.
2877 (goto-char x-coding-header)
2878 (if (> (point) (point-min))
2879 (delete-region (line-beginning-position) (point))
2880 (forward-line)
2881 (insert "\n")
2882 (forward-line -1))
2883 (insert "X-Coding-System: "
2884 (symbol-name coding))))
2885 (rmail-show-message))))))
2887 (defun rmail-highlight-headers ()
2888 "Highlight the headers specified by `rmail-highlighted-headers'.
2889 Uses the face specified by `rmail-highlight-face'."
2890 (if rmail-highlighted-headers
2891 (save-excursion
2892 (search-forward "\n\n" nil 'move)
2893 (save-restriction
2894 (narrow-to-region (point-min) (point))
2895 (let ((case-fold-search t)
2896 (inhibit-read-only t)
2897 ;; When rmail-highlight-face is removed, just
2898 ;; use 'rmail-highlight here.
2899 (face (or rmail-highlight-face
2900 (if (face-differs-from-default-p 'bold)
2901 'bold 'highlight)))
2902 ;; List of overlays to reuse.
2903 (overlays rmail-overlay-list))
2904 (goto-char (point-min))
2905 (while (re-search-forward rmail-highlighted-headers nil t)
2906 (skip-chars-forward " \t")
2907 (let ((beg (point))
2908 overlay)
2909 (while (progn (forward-line 1)
2910 (looking-at "[ \t]")))
2911 ;; Back up over newline, then trailing spaces or tabs
2912 (forward-char -1)
2913 (while (member (preceding-char) '(? ?\t))
2914 (forward-char -1))
2915 (if overlays
2916 ;; Reuse an overlay we already have.
2917 (progn
2918 (setq overlay (car overlays)
2919 overlays (cdr overlays))
2920 (overlay-put overlay 'face face)
2921 (move-overlay overlay beg (point)))
2922 ;; Make a new overlay and add it to
2923 ;; rmail-overlay-list.
2924 (setq overlay (make-overlay beg (point)))
2925 (overlay-put overlay 'face face)
2926 (setq rmail-overlay-list
2927 (cons overlay rmail-overlay-list))))))))))
2929 (defun rmail-auto-file ()
2930 "Automatically move a message into a sub-folder based on criteria.
2931 Called when a new message is displayed."
2932 (if (or (zerop rmail-total-messages)
2933 (rmail-message-attr-p rmail-current-message "...F")
2934 (not (string= (buffer-file-name)
2935 (expand-file-name rmail-file-name))))
2936 ;; Do nothing if the message has already been filed or if there
2937 ;; are no messages.
2939 ;; Find out some basics (common fields)
2940 (let ((from (mail-fetch-field "from"))
2941 (subj (mail-fetch-field "subject"))
2942 (to (concat (mail-fetch-field "to") "," (mail-fetch-field "cc")))
2943 (d rmail-automatic-folder-directives)
2944 (directive-loop nil)
2945 (folder nil))
2946 (while d
2947 (setq folder (car (car d))
2948 directive-loop (cdr (car d)))
2949 (while (and (car directive-loop)
2950 (let ((f (cond
2951 ((string= (car directive-loop) "from") from)
2952 ((string= (car directive-loop) "to") to)
2953 ((string= (car directive-loop) "subject") subj)
2954 (t (mail-fetch-field (car directive-loop))))))
2955 (and f (string-match (car (cdr directive-loop)) f))))
2956 (setq directive-loop (cdr (cdr directive-loop))))
2957 ;; If there are no directives left, then it was a complete match.
2958 (if (null directive-loop)
2959 (if (null folder)
2960 (rmail-delete-forward)
2961 (if (string= "/dev/null" folder)
2962 (rmail-delete-message)
2963 (rmail-output folder 1)
2964 (setq d nil))))
2965 (setq d (cdr d))))))
2967 ;; Simple message motion commands.
2969 (defun rmail-next-message (n)
2970 "Show following message whether deleted or not.
2971 With prefix arg N, moves forward N messages, or backward if N is negative."
2972 (interactive "p")
2973 (set-buffer rmail-buffer)
2974 (rmail-maybe-set-message-counters)
2975 (rmail-show-message (+ rmail-current-message n)))
2977 (defun rmail-previous-message (n)
2978 "Show previous message whether deleted or not.
2979 With prefix arg N, moves backward N messages, or forward if N is negative."
2980 (interactive "p")
2981 (rmail-next-message (- n)))
2983 (defun rmail-next-undeleted-message (n)
2984 "Show following non-deleted message.
2985 With prefix arg N, moves forward N non-deleted messages,
2986 or backward if N is negative.
2988 Returns t if a new message is being shown, nil otherwise."
2989 (interactive "p")
2990 (set-buffer rmail-buffer)
2991 (rmail-maybe-set-message-counters)
2992 (let ((lastwin rmail-current-message)
2993 (current rmail-current-message))
2994 (while (and (> n 0) (< current rmail-total-messages))
2995 (setq current (1+ current))
2996 (if (not (rmail-message-deleted-p current))
2997 (setq lastwin current n (1- n))))
2998 (while (and (< n 0) (> current 1))
2999 (setq current (1- current))
3000 (if (not (rmail-message-deleted-p current))
3001 (setq lastwin current n (1+ n))))
3002 (if (/= lastwin rmail-current-message)
3003 (progn (rmail-show-message lastwin)
3005 (if (< n 0)
3006 (message "No previous nondeleted message"))
3007 (if (> n 0)
3008 (message "No following nondeleted message"))
3009 nil)))
3011 (defun rmail-previous-undeleted-message (n)
3012 "Show previous non-deleted message.
3013 With prefix argument N, moves backward N non-deleted messages,
3014 or forward if N is negative."
3015 (interactive "p")
3016 (rmail-next-undeleted-message (- n)))
3018 (defun rmail-first-message ()
3019 "Show first message in file."
3020 (interactive)
3021 (rmail-maybe-set-message-counters)
3022 (rmail-show-message 1))
3024 (defun rmail-last-message ()
3025 "Show last message in file."
3026 (interactive)
3027 (rmail-maybe-set-message-counters)
3028 (rmail-show-message rmail-total-messages))
3030 (defun rmail-what-message ()
3031 "For debugging Rmail: find the message number that point is in."
3032 (let* ((high rmail-total-messages)
3033 (mid (/ high 2))
3034 (low 1)
3035 (where (with-current-buffer (if (rmail-buffers-swapped-p)
3036 rmail-view-buffer
3037 (current-buffer))
3038 (point))))
3039 (while (> (- high low) 1)
3040 (if (>= where (rmail-msgbeg mid))
3041 (setq low mid)
3042 (setq high mid))
3043 (setq mid (+ low (/ (- high low) 2))))
3044 (if (>= where (rmail-msgbeg high)) high low)))
3046 ;; Searching in Rmail file.
3048 (defun rmail-search-message (msg regexp)
3049 "Return non-nil, if for message number MSG, regexp REGEXP matches."
3050 ;; This is adequate because its only caller, rmail-search,
3051 ;; unswaps the buffers.
3052 (goto-char (rmail-msgbeg msg))
3053 (if rmail-enable-mime
3054 (if rmail-search-mime-message-function
3055 (funcall rmail-search-mime-message-function msg regexp)
3056 (error "You must set `rmail-search-mime-message-function'"))
3057 (re-search-forward regexp (rmail-msgend msg) t)))
3059 (defvar rmail-search-last-regexp nil)
3060 (defun rmail-search (regexp &optional n)
3061 "Show message containing next match for REGEXP (but not the current msg).
3062 Prefix argument gives repeat count; negative argument means search
3063 backwards (through earlier messages).
3064 Interactively, empty argument means use same regexp used last time."
3065 (interactive
3066 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
3067 (prompt
3068 (concat (if reversep "Reverse " "") "Rmail search (regexp"))
3069 regexp)
3070 (setq prompt
3071 (concat prompt
3072 (if rmail-search-last-regexp
3073 (concat ", default "
3074 rmail-search-last-regexp "): ")
3075 "): ")))
3076 (setq regexp (read-string prompt))
3077 (cond ((not (equal regexp ""))
3078 (setq rmail-search-last-regexp regexp))
3079 ((not rmail-search-last-regexp)
3080 (error "No previous Rmail search string")))
3081 (list rmail-search-last-regexp
3082 (prefix-numeric-value current-prefix-arg))))
3083 (or n (setq n 1))
3084 (message "%sRmail search for %s..."
3085 (if (< n 0) "Reverse " "")
3086 regexp)
3087 (set-buffer rmail-buffer)
3088 (let ((orig-message rmail-current-message)
3089 (msg rmail-current-message)
3090 (reversep (< n 0))
3091 (opoint (if (rmail-buffers-swapped-p) (point)))
3092 found)
3093 (rmail-swap-buffers-maybe)
3094 (rmail-maybe-set-message-counters)
3095 (widen)
3096 (unwind-protect
3097 (while (/= n 0)
3098 ;; Check messages one by one, advancing message number up or
3099 ;; down but searching forward through each message.
3100 (if reversep
3101 (while (and (null found) (> msg 1))
3102 (setq msg (1- msg)
3103 found (rmail-search-message msg regexp)))
3104 (while (and (null found) (< msg rmail-total-messages))
3105 (setq msg (1+ msg)
3106 found (rmail-search-message msg regexp))))
3107 (setq n (+ n (if reversep 1 -1))))
3108 (if found
3109 (progn
3110 (rmail-show-message msg)
3111 ;; Search forward (if this is a normal search) or backward
3112 ;; (if this is a reverse search) through this message to
3113 ;; position point. This search may fail because REGEXP
3114 ;; was found in the hidden portion of this message. In
3115 ;; that case, move point to the beginning of visible
3116 ;; portion.
3117 (if reversep
3118 (progn
3119 (goto-char (point-max))
3120 (re-search-backward regexp nil 'move))
3121 (goto-char (point-min))
3122 (re-search-forward regexp nil t))
3123 (message "%sRmail search for %s...done"
3124 (if reversep "Reverse " "")
3125 regexp))
3126 (rmail-show-message orig-message)
3127 (if opoint (goto-char opoint))
3128 (ding)
3129 (message "Search failed: %s" regexp)))))
3131 (defun rmail-search-backwards (regexp &optional n)
3132 "Show message containing previous match for REGEXP.
3133 Prefix argument gives repeat count; negative argument means search
3134 forward (through later messages).
3135 Interactively, empty argument means use same regexp used last time."
3136 (interactive
3137 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
3138 (prompt
3139 (concat (if reversep "Reverse " "") "Rmail search (regexp"))
3140 regexp)
3141 (setq prompt
3142 (concat prompt
3143 (if rmail-search-last-regexp
3144 (concat ", default "
3145 rmail-search-last-regexp "): ")
3146 "): ")))
3147 (setq regexp (read-string prompt))
3148 (cond ((not (equal regexp ""))
3149 (setq rmail-search-last-regexp regexp))
3150 ((not rmail-search-last-regexp)
3151 (error "No previous Rmail search string")))
3152 (list rmail-search-last-regexp
3153 (prefix-numeric-value current-prefix-arg))))
3154 (rmail-search regexp (- (or n 1))))
3156 ;; Scan for attributes, and compare subjects.
3158 (defun rmail-first-unseen-message ()
3159 "Return message number of first message which has `unseen' attribute."
3160 (rmail-maybe-set-message-counters)
3161 (let ((current 1)
3162 found)
3163 (save-restriction
3164 (widen)
3165 (while (and (not found) (<= current rmail-total-messages))
3166 (if (rmail-message-attr-p current "......U")
3167 (setq found current))
3168 (setq current (1+ current))))
3169 found))
3171 (defun rmail-simplified-subject (&optional msgnum)
3172 "Return the simplified subject of message MSGNUM (or current message).
3173 Simplifying the subject means stripping leading and trailing whitespace,
3174 and typical reply prefixes such as Re:."
3175 (let ((subject (or (rmail-get-header "Subject" msgnum) "")))
3176 (if (string-match "\\`[ \t]+" subject)
3177 (setq subject (substring subject (match-end 0))))
3178 (if (string-match rmail-reply-regexp subject)
3179 (setq subject (substring subject (match-end 0))))
3180 (if (string-match "[ \t]+\\'" subject)
3181 (setq subject (substring subject 0 (match-beginning 0))))
3182 ;; If Subject is long, mailers will break it into several lines at
3183 ;; arbitrary places, so normalize whitespace by replacing every
3184 ;; run of whitespace characters with a single space.
3185 (setq subject (replace-regexp-in-string "[ \t\n]+" " " subject))
3186 subject))
3188 (defun rmail-simplified-subject-regexp ()
3189 "Return a regular expression matching the current simplified subject.
3190 The idea is to match it against simplified subjects of other messages."
3191 (let ((subject (rmail-simplified-subject)))
3192 (setq subject (regexp-quote subject))
3193 ;; Hide commas so it will work ok if parsed as a comma-separated list
3194 ;; of regexps.
3195 (setq subject
3196 (replace-regexp-in-string "," "\054" subject t t))
3197 (concat "\\`" subject "\\'")))
3199 (defun rmail-next-same-subject (n)
3200 "Go to the next mail message having the same subject header.
3201 With prefix argument N, do this N times.
3202 If N is negative, go backwards instead."
3203 (interactive "p")
3204 (let ((subject (rmail-simplified-subject))
3205 (forward (> n 0))
3206 (i rmail-current-message)
3207 found)
3208 (while (and (/= n 0)
3209 (if forward
3210 (< i rmail-total-messages)
3211 (> i 1)))
3212 (let (done)
3213 (while (and (not done)
3214 (if forward
3215 (< i rmail-total-messages)
3216 (> i 1)))
3217 (setq i (if forward (1+ i) (1- i)))
3218 (setq done (string-equal subject (rmail-simplified-subject i))))
3219 (if done (setq found i)))
3220 (setq n (if forward (1- n) (1+ n))))
3221 (if found
3222 (rmail-show-message found)
3223 (error "No %s message with same subject"
3224 (if forward "following" "previous")))))
3226 (defun rmail-previous-same-subject (n)
3227 "Go to the previous mail message having the same subject header.
3228 With prefix argument N, do this N times.
3229 If N is negative, go forwards instead."
3230 (interactive "p")
3231 (rmail-next-same-subject (- n)))
3233 ;;;; *** Rmail Message Deletion Commands ***
3235 (defun rmail-message-deleted-p (n)
3236 "Return non-nil if message number N is deleted (in `rmail-deleted-vector')."
3237 (= (aref rmail-deleted-vector n) ?D))
3239 (defun rmail-set-message-deleted-p (n state)
3240 "Set the deleted state of message number N (in `rmail-deleted-vector').
3241 STATE non-nil means mark as deleted."
3242 (aset rmail-deleted-vector n (if state ?D ?\s)))
3244 (defun rmail-delete-message ()
3245 "Delete this message and stay on it."
3246 (interactive)
3247 (rmail-set-attribute rmail-deleted-attr-index t)
3248 (run-hooks 'rmail-delete-message-hook))
3250 (defun rmail-undelete-previous-message ()
3251 "Back up to deleted message, select it, and undelete it."
3252 (interactive)
3253 (set-buffer rmail-buffer)
3254 (let ((msg rmail-current-message))
3255 (while (and (> msg 0)
3256 (not (rmail-message-deleted-p msg)))
3257 (setq msg (1- msg)))
3258 (if (= msg 0)
3259 (error "No previous deleted message")
3260 (if (/= msg rmail-current-message)
3261 (rmail-show-message msg))
3262 (rmail-set-attribute rmail-deleted-attr-index nil)
3263 (if (rmail-summary-exists)
3264 (with-current-buffer rmail-summary-buffer
3265 (rmail-summary-mark-undeleted msg)))
3266 (rmail-maybe-display-summary))))
3268 (defun rmail-delete-forward (&optional backward)
3269 "Delete this message and move to next nondeleted one.
3270 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
3271 With prefix argument, delete and move backward.
3273 Returns t if a new message is displayed after the delete, or nil otherwise."
3274 (interactive "P")
3275 (rmail-set-attribute rmail-deleted-attr-index t)
3276 (run-hooks 'rmail-delete-message-hook)
3277 (let ((del-msg rmail-current-message))
3278 (if (rmail-summary-exists)
3279 (rmail-select-summary
3280 (rmail-summary-mark-deleted del-msg)))
3281 (prog1 (rmail-next-undeleted-message (if backward -1 1))
3282 (rmail-maybe-display-summary))))
3284 (defun rmail-delete-backward ()
3285 "Delete this message and move to previous nondeleted one.
3286 Deleted messages stay in the file until the \\[rmail-expunge] command is given."
3287 (interactive)
3288 (rmail-delete-forward t))
3290 ;; Expunging.
3292 ;; Compute the message number a given message would have after expunging.
3293 ;; The present number of the message is OLDNUM.
3294 ;; DELETEDVEC should be rmail-deleted-vector.
3295 ;; The value is nil for a message that would be deleted.
3296 (defun rmail-msg-number-after-expunge (deletedvec oldnum)
3297 (if (or (null oldnum) (= (aref deletedvec oldnum) ?D))
3299 (let ((i 0)
3300 (newnum 0))
3301 (while (< i oldnum)
3302 (if (/= (aref deletedvec i) ?D)
3303 (setq newnum (1+ newnum)))
3304 (setq i (1+ i)))
3305 newnum)))
3307 (defun rmail-expunge-confirmed ()
3308 "Return t if expunge is needed and desirable.
3309 If `rmail-confirm-expunge' is non-nil, ask user to confirm."
3310 (set-buffer rmail-buffer)
3311 (and (stringp rmail-deleted-vector)
3312 (string-match "D" rmail-deleted-vector)
3313 (if rmail-confirm-expunge
3314 (funcall rmail-confirm-expunge
3315 "Erase deleted messages from Rmail file? ")
3316 t)))
3318 (defun rmail-only-expunge (&optional dont-show)
3319 "Actually erase all deleted messages in the file."
3320 (interactive)
3321 (rmail-swap-buffers-maybe)
3322 (set-buffer rmail-buffer)
3323 (message "Expunging deleted messages...")
3324 ;; Discard all undo records for this buffer.
3325 (or (eq buffer-undo-list t)
3326 (setq buffer-undo-list nil))
3327 (rmail-maybe-set-message-counters)
3328 (let* ((omax (- (buffer-size) (point-max)))
3329 (omin (- (buffer-size) (point-min)))
3330 (opoint (if (and (> rmail-current-message 0)
3331 (rmail-message-deleted-p rmail-current-message))
3333 (if rmail-enable-mime
3334 (with-current-buffer rmail-view-buffer
3335 (- (point)(point-min)))
3336 (- (point) (point-min)))))
3337 (messages-head (cons (aref rmail-message-vector 0) nil))
3338 (messages-tail messages-head)
3339 ;; Don't make any undo records for the expunging.
3340 (buffer-undo-list t)
3341 (win))
3342 (unwind-protect
3343 (save-excursion
3344 (widen)
3345 (goto-char (point-min))
3346 (let ((counter 0)
3347 (number 1)
3348 new-summary
3349 (new-msgref (list (list 0)))
3350 (buffer-read-only nil)
3351 (total rmail-total-messages)
3352 (new-message-number rmail-current-message)
3353 (messages rmail-message-vector)
3354 (deleted rmail-deleted-vector)
3355 (summary rmail-summary-vector))
3356 (setq rmail-total-messages nil
3357 rmail-current-message nil
3358 rmail-message-vector nil
3359 rmail-deleted-vector nil
3360 rmail-summary-vector nil)
3362 (while (<= number total)
3363 (if (= (aref deleted number) ?D)
3364 (progn
3365 (delete-region (aref messages number)
3366 (aref messages (1+ number)))
3367 (move-marker (aref messages number) nil)
3368 (if (> new-message-number counter)
3369 (setq new-message-number (1- new-message-number))))
3370 (setq counter (1+ counter))
3371 (setq messages-tail
3372 (setcdr messages-tail
3373 (cons (aref messages number) nil)))
3374 (setq new-summary
3375 (cons (if (= counter number) (aref summary (1- number)))
3376 new-summary))
3377 (setq new-msgref
3378 (cons (aref rmail-msgref-vector number)
3379 new-msgref))
3380 (setcar (car new-msgref) counter))
3381 (if (zerop (% (setq number (1+ number)) 20))
3382 (message "Expunging deleted messages...%d" number)))
3383 (setq messages-tail
3384 (setcdr messages-tail
3385 (cons (aref messages number) nil)))
3386 (setq rmail-current-message new-message-number
3387 rmail-total-messages counter
3388 rmail-message-vector (apply 'vector messages-head)
3389 rmail-deleted-vector (make-string (1+ counter) ?\s)
3390 rmail-summary-vector (vconcat (nreverse new-summary))
3391 rmail-msgref-vector (apply 'vector (nreverse new-msgref))
3392 win t)))
3393 (message "Expunging deleted messages...done")
3394 (if (not win)
3395 (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
3396 (if (not dont-show)
3397 (rmail-show-message (min rmail-current-message rmail-total-messages)))
3398 (if rmail-enable-mime
3399 (goto-char (+ (point-min) opoint))
3400 (goto-char (+ (point) opoint))))))
3402 ;; The DONT-SHOW argument is new in 23. Does not seem very important.
3403 (defun rmail-expunge (&optional dont-show)
3404 "Erase deleted messages from Rmail file and summary buffer.
3405 This always shows a message (so as not to leave the Rmail buffer
3406 unswapped), and always updates any summary (so that it remains
3407 consistent with the Rmail buffer). If DONT-SHOW is non-nil, it
3408 does not pop any summary buffer."
3409 (interactive)
3410 (when (rmail-expunge-confirmed)
3411 (rmail-modify-format)
3412 (let ((was-deleted (rmail-message-deleted-p rmail-current-message))
3413 (was-swapped (rmail-buffers-swapped-p)))
3414 (rmail-only-expunge t)
3415 ;; We always update the summary buffer, so that the contents
3416 ;; remain consistent with the rmail buffer.
3417 ;; The only difference is, in the dont-show case, we use a
3418 ;; cut-down version of rmail-select-summary that does not pop
3419 ;; the summary buffer. It's only used by rmail-quit, which is
3420 ;; just going to bury any summary immediately after. If we made
3421 ;; rmail-quit bury the summary first, dont-show could be removed.
3422 ;; But the expunge might not be confirmed...
3423 (if (rmail-summary-exists)
3424 (if dont-show
3425 (let ((total rmail-total-messages))
3426 (with-current-buffer rmail-summary-buffer
3427 (let ((rmail-total-messages total))
3428 (rmail-update-summary))))
3429 (rmail-select-summary (rmail-update-summary))))
3430 ;; We always show a message, because (rmail-only-expunge t)
3431 ;; leaves the rmail buffer unswapped.
3432 ;; If we expunged the current message, a new one is current now,
3433 ;; so show it. If we weren't showing a message, show it.
3434 (if (or was-deleted (not was-swapped))
3435 (rmail-show-message-1 rmail-current-message)
3436 ;; We can just show the same message that was being shown before.
3437 (rmail-display-labels)
3438 (rmail-swap-buffers)
3439 (setq rmail-buffer-swapped t)))))
3441 ;;;; *** Rmail Mailing Commands ***
3443 (defun rmail-start-mail (&optional noerase to subject in-reply-to cc
3444 replybuffer sendactions same-window others)
3445 (let (yank-action)
3446 (if replybuffer
3447 ;; The function used here must behave like insert-buffer wrt
3448 ;; point and mark (see doc of sc-cite-original).
3449 (setq yank-action (list 'insert-buffer replybuffer)))
3450 (setq others (cons (cons "cc" cc) others))
3451 (setq others (cons (cons "in-reply-to" in-reply-to) others))
3452 (if same-window
3453 (compose-mail to subject others
3454 noerase nil
3455 yank-action sendactions)
3456 (if rmail-mail-new-frame
3457 (prog1
3458 (compose-mail to subject others
3459 noerase 'switch-to-buffer-other-frame
3460 yank-action sendactions)
3461 ;; This is not a standard frame parameter;
3462 ;; nothing except sendmail.el looks at it.
3463 (modify-frame-parameters (selected-frame)
3464 '((mail-dedicated-frame . t))))
3465 (compose-mail to subject others
3466 noerase 'switch-to-buffer-other-window
3467 yank-action sendactions)))))
3469 (defun rmail-mail ()
3470 "Send mail in another window.
3471 While composing the message, use \\[mail-yank-original] to yank the
3472 original message into it."
3473 (interactive)
3474 (rmail-start-mail nil nil nil nil nil rmail-view-buffer))
3476 ;; FIXME should complain if there is nothing to continue.
3477 (defun rmail-continue ()
3478 "Continue composing outgoing message previously being composed."
3479 (interactive)
3480 (rmail-start-mail t))
3482 (defun rmail-reply (just-sender)
3483 "Reply to the current message.
3484 Normally include CC: to all other recipients of original message;
3485 prefix argument means ignore them. While composing the reply,
3486 use \\[mail-yank-original] to yank the original message into it."
3487 (interactive "P")
3488 (if (zerop rmail-current-message)
3489 (error "There is no message to reply to"))
3490 (let (from reply-to cc subject date to message-id references
3491 resent-to resent-cc resent-reply-to
3492 (msgnum rmail-current-message))
3493 (rmail-apply-in-message
3494 rmail-current-message
3495 (lambda ()
3496 (search-forward "\n\n" nil 'move)
3497 (narrow-to-region (point-min) (point))
3498 (setq from (mail-fetch-field "from")
3499 reply-to (or (mail-fetch-field "mail-reply-to" nil t)
3500 (mail-fetch-field "reply-to" nil t)
3501 from)
3502 subject (mail-fetch-field "subject")
3503 date (mail-fetch-field "date")
3504 message-id (mail-fetch-field "message-id")
3505 references (mail-fetch-field "references" nil nil t)
3506 resent-reply-to (mail-fetch-field "resent-reply-to" nil t)
3507 ;; Bug#512. It's inappropriate to reply to these addresses.
3508 ;;; resent-cc (and (not just-sender)
3509 ;;; (mail-fetch-field "resent-cc" nil t))
3510 ;;; resent-to (or (mail-fetch-field "resent-to" nil t) "")
3511 ;;; resent-subject (mail-fetch-field "resent-subject")
3512 ;;; resent-date (mail-fetch-field "resent-date")
3513 ;;; resent-message-id (mail-fetch-field "resent-message-id")
3515 (unless just-sender
3516 (if (mail-fetch-field "mail-followup-to" nil t)
3517 ;; If this header field is present, use it instead of the
3518 ;; To and CC fields.
3519 (setq to (mail-fetch-field "mail-followup-to" nil t))
3520 (setq cc (or (mail-fetch-field "cc" nil t) "")
3521 to (or (mail-fetch-field "to" nil t) ""))))))
3522 ;; Merge the resent-to and resent-cc into the to and cc.
3523 ;; Bug#512. It's inappropriate to reply to these addresses.
3524 ;;; (if (and resent-to (not (equal resent-to "")))
3525 ;;; (if (not (equal to ""))
3526 ;;; (setq to (concat to ", " resent-to))
3527 ;;; (setq to resent-to)))
3528 ;;; (if (and resent-cc (not (equal resent-cc "")))
3529 ;;; (if (not (equal cc ""))
3530 ;;; (setq cc (concat cc ", " resent-cc))
3531 ;;; (setq cc resent-cc)))
3532 ;; Add `Re: ' to subject if not there already.
3533 (and (stringp subject)
3534 (setq subject
3535 (concat rmail-reply-prefix
3536 (if (let ((case-fold-search t))
3537 (string-match rmail-reply-regexp subject))
3538 (substring subject (match-end 0))
3539 subject))))
3540 (rmail-start-mail
3542 ;; Using mail-strip-quoted-names is undesirable with newer mailers
3543 ;; since they can handle the names unstripped.
3544 ;; I don't know whether there are other mailers that still
3545 ;; need the names to be stripped.
3546 ;;; (mail-strip-quoted-names reply-to)
3547 ;; Remove unwanted names from reply-to, since Mail-Followup-To
3548 ;; header causes all the names in it to wind up in reply-to, not
3549 ;; in cc. But if what's left is an empty list, use the original.
3550 (let* ((reply-to-list (rmail-dont-reply-to reply-to)))
3551 (if (string= reply-to-list "") reply-to reply-to-list))
3552 subject
3553 (rmail-make-in-reply-to-field from date message-id)
3554 (if just-sender
3556 ;; mail-strip-quoted-names is NOT necessary for rmail-dont-reply-to
3557 ;; to do its job.
3558 (let* ((cc-list (rmail-dont-reply-to
3559 (mail-strip-quoted-names
3560 (if (null cc) to (concat to ", " cc))))))
3561 (if (string= cc-list "") nil cc-list)))
3562 (if (rmail-buffers-swapped-p)
3563 rmail-buffer
3564 rmail-view-buffer)
3565 (list (list 'rmail-mark-message
3566 rmail-buffer
3567 (with-current-buffer rmail-buffer
3568 (aref rmail-msgref-vector msgnum))
3569 rmail-answered-attr-index))
3571 (if (or references message-id)
3572 (list (cons "References" (if references
3573 (concat
3574 (mapconcat 'identity references " ")
3575 " " message-id)
3576 message-id)))))))
3578 (defun rmail-mark-message (buffer msgnum-list attribute)
3579 "Give BUFFER's message number in MSGNUM-LIST the attribute ATTRIBUTE.
3580 This is use in the send-actions for message buffers.
3581 MSGNUM-LIST is a list of the form (MSGNUM)
3582 which is an element of rmail-msgref-vector."
3583 (with-current-buffer buffer
3584 (if (car msgnum-list)
3585 (rmail-set-attribute attribute t (car msgnum-list)))))
3587 (defun rmail-make-in-reply-to-field (from date message-id)
3588 (cond ((not from)
3589 (if message-id
3590 message-id
3591 nil))
3592 (mail-use-rfc822
3593 (require 'rfc822)
3594 (let ((tem (car (rfc822-addresses from))))
3595 (if message-id
3596 (if (or (not tem)
3597 (string-match
3598 (regexp-quote (if (string-match "@[^@]*\\'" tem)
3599 (substring tem 0
3600 (match-beginning 0))
3601 tem))
3602 message-id))
3603 ;; missing From, or Message-ID is sufficiently informative
3604 message-id
3605 (concat message-id " (" tem ")"))
3606 ;; Copy TEM, discarding text properties.
3607 (setq tem (copy-sequence tem))
3608 (set-text-properties 0 (length tem) nil tem)
3609 (setq tem (copy-sequence tem))
3610 ;; Use prin1 to fake RFC822 quoting
3611 (let ((field (prin1-to-string tem)))
3612 (if date
3613 (concat field "'s message of " date)
3614 field)))))
3615 ((let* ((foo "[^][\000-\037()<>@,;:\\\" ]+")
3616 (bar "[^][\000-\037()<>@,;:\\\"]+"))
3617 ;; These strings both match all non-ASCII characters.
3618 (or (string-match (concat "\\`[ \t]*\\(" bar
3619 "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
3620 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
3621 from)
3622 (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
3623 bar "\\))[ \t]*\\'")
3624 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
3625 from)))
3626 (let ((start (match-beginning 1))
3627 (end (match-end 1)))
3628 ;; Trim whitespace which above regexp match allows
3629 (while (and (< start end)
3630 (memq (aref from start) '(?\t ?\s)))
3631 (setq start (1+ start)))
3632 (while (and (< start end)
3633 (memq (aref from (1- end)) '(?\t ?\s)))
3634 (setq end (1- end)))
3635 (let ((field (substring from start end)))
3636 (if date (setq field (concat "message from " field " on " date)))
3637 (if message-id
3638 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
3639 (concat message-id " (" field ")")
3640 field))))
3642 ;; If we can't kludge it simply, do it correctly
3643 (let ((mail-use-rfc822 t))
3644 (rmail-make-in-reply-to-field from date message-id)))))
3646 (defun rmail-forward (resend)
3647 "Forward the current message to another user.
3648 With prefix argument, \"resend\" the message instead of forwarding it;
3649 see the documentation of `rmail-resend'."
3650 (interactive "P")
3651 (if resend
3652 (call-interactively 'rmail-resend)
3653 (let ((forward-buffer rmail-buffer)
3654 (msgnum rmail-current-message)
3655 (subject (concat "["
3656 (let ((from (or (mail-fetch-field "From")
3657 (mail-fetch-field ">From"))))
3658 (if from
3659 (concat (mail-strip-quoted-names from) ": ")
3660 ""))
3661 (or (mail-fetch-field "Subject") "")
3662 "]")))
3663 (if (rmail-start-mail
3664 nil nil subject nil nil nil
3665 (list (list 'rmail-mark-message
3666 forward-buffer
3667 (with-current-buffer rmail-buffer
3668 (aref rmail-msgref-vector msgnum))
3669 rmail-forwarded-attr-index))
3670 ;; If only one window, use it for the mail buffer.
3671 ;; Otherwise, use another window for the mail buffer
3672 ;; so that the Rmail buffer remains visible
3673 ;; and sending the mail will get back to it.
3674 (and (not rmail-mail-new-frame) (one-window-p t)))
3675 ;; The mail buffer is now current.
3676 (save-excursion
3677 ;; Insert after header separator--before signature if any.
3678 (rfc822-goto-eoh)
3679 (forward-line 1)
3680 (if (or rmail-enable-mime rmail-enable-mime-composing)
3681 (funcall rmail-insert-mime-forwarded-message-function
3682 forward-buffer)
3683 (insert "------- Start of forwarded message -------\n")
3684 ;; Quote lines with `- ' if they start with `-'.
3685 (let ((beg (point)) end)
3686 (setq end (point-marker))
3687 (set-marker-insertion-type end t)
3688 (insert-buffer-substring forward-buffer)
3689 (goto-char beg)
3690 (while (re-search-forward "^-" end t)
3691 (beginning-of-line)
3692 (insert "- ")
3693 (forward-line 1))
3694 (goto-char end)
3695 (skip-chars-backward "\n")
3696 (if (< (point) end)
3697 (forward-char 1))
3698 (delete-region (point) end)
3699 (set-marker end nil))
3700 (insert "------- End of forwarded message -------\n"))
3701 (push-mark))))))
3703 (defun rmail-resend (address &optional from comment mail-alias-file)
3704 "Resend current message to ADDRESSES.
3705 ADDRESSES should be a single address, a string consisting of several
3706 addresses separated by commas, or a list of addresses.
3708 Optional FROM is the address to resend the message from, and
3709 defaults from the value of `user-mail-address'.
3710 Optional COMMENT is a string to insert as a comment in the resent message.
3711 Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
3712 typically for purposes of moderating a list."
3713 (interactive "sResend to: ")
3714 (require 'sendmail)
3715 (require 'mailalias)
3716 (unless (or (eq rmail-view-buffer (current-buffer))
3717 (eq rmail-buffer (current-buffer)))
3718 (error "Not an Rmail buffer"))
3719 (if (not from) (setq from user-mail-address))
3720 (let ((tembuf (generate-new-buffer " sendmail temp"))
3721 (case-fold-search nil)
3722 (mail-personal-alias-file
3723 (or mail-alias-file mail-personal-alias-file))
3724 (mailbuf rmail-buffer))
3725 (unwind-protect
3726 (with-current-buffer tembuf
3727 ;;>> Copy message into temp buffer
3728 (if rmail-enable-mime
3729 (if rmail-insert-mime-resent-message-function
3730 (funcall rmail-insert-mime-resent-message-function mailbuf)
3731 (error "You must set `rmail-insert-mime-resent-message-function'"))
3732 (insert-buffer-substring mailbuf))
3733 (goto-char (point-min))
3734 ;; Delete any Sender field, since that's not specifiable.
3735 ; Only delete Sender fields in the actual header.
3736 (re-search-forward "^$" nil 'move)
3737 ; Using "while" here rather than "if" because some buggy mail
3738 ; software may have inserted multiple Sender fields.
3739 (while (re-search-backward "^Sender:" nil t)
3740 (let (beg)
3741 (setq beg (point))
3742 (forward-line 1)
3743 (while (looking-at "[ \t]")
3744 (forward-line 1))
3745 (delete-region beg (point))))
3746 ; Go back to the beginning of the buffer so the Resent- fields
3747 ; are inserted there.
3748 (goto-char (point-min))
3749 ;;>> Insert resent-from:
3750 (insert "Resent-From: " from "\n")
3751 (insert "Resent-Date: " (mail-rfc822-date) "\n")
3752 ;;>> Insert resent-to: and bcc if need be.
3753 (let ((before (point)))
3754 (if mail-self-blind
3755 (insert "Resent-Bcc: " (user-login-name) "\n"))
3756 (insert "Resent-To: " (if (stringp address)
3757 address
3758 (mapconcat 'identity address ",\n\t"))
3759 "\n")
3760 ;; Expand abbrevs in the recipients.
3761 (save-excursion
3762 (if (featurep 'mailabbrev)
3763 (let ((end (point-marker))
3764 (local-abbrev-table mail-abbrevs)
3765 (old-syntax-table (syntax-table)))
3766 (if (and (not (vectorp mail-abbrevs))
3767 (file-exists-p mail-personal-alias-file))
3768 (build-mail-abbrevs))
3769 (unless mail-abbrev-syntax-table
3770 (mail-abbrev-make-syntax-table))
3771 (set-syntax-table mail-abbrev-syntax-table)
3772 (goto-char before)
3773 (while (and (< (point) end)
3774 (progn (forward-word 1)
3775 (<= (point) end)))
3776 (expand-abbrev))
3777 (set-syntax-table old-syntax-table))
3778 (expand-mail-aliases before (point)))))
3779 ;;>> Set up comment, if any.
3780 (if (and (sequencep comment) (not (zerop (length comment))))
3781 (let ((before (point))
3782 after)
3783 (insert comment)
3784 (or (eolp) (insert "\n"))
3785 (setq after (point))
3786 (goto-char before)
3787 (while (< (point) after)
3788 (insert "Resent-Comment: ")
3789 (forward-line 1))))
3790 ;; Don't expand aliases in the destination fields
3791 ;; of the original message.
3792 (let (mail-aliases)
3793 (funcall send-mail-function)))
3794 (kill-buffer tembuf))
3795 (with-current-buffer rmail-buffer
3796 (rmail-set-attribute rmail-resent-attr-index t rmail-current-message))))
3798 (defvar mail-unsent-separator
3799 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
3800 "^ *---+ +Returned message +---+ *$\\|"
3801 "^ *---+ *Returned mail follows *---+ *$\\|"
3802 "^Start of returned message$\\|"
3803 "^---+ Below this line is a copy of the message.$\\|"
3804 "^ *---+ +Original message +---+ *$\\|"
3805 "^ *--+ +begin message +--+ *$\\|"
3806 "^ *---+ +Original message follows +---+ *$\\|"
3807 "^ *---+ +Your message follows +---+ *$\\|"
3808 "^|? *---+ +Message text follows: +---+ *|?$\\|"
3809 "^ *---+ +This is a copy of \\w+ message, including all the headers.*---+ *$")
3810 "A regexp that matches the separator before the text of a failed message.")
3812 (defvar mail-mime-unsent-header "^Content-Type: message/rfc822 *$"
3813 "A regexp that matches the header of a MIME body part with a failed message.")
3815 ;; This is a cut-down version of rmail-clear-headers from Emacs 22.
3816 ;; It doesn't have the same functionality, hence the name change.
3817 (defun rmail-delete-headers (regexp)
3818 "Delete any mail headers matching REGEXP.
3819 The message should be narrowed to just the headers."
3820 (when regexp
3821 (goto-char (point-min))
3822 (while (re-search-forward regexp nil t)
3823 (beginning-of-line)
3824 ;; This code from Emacs 22 doesn't seem right, since r-n-h is
3825 ;; just for display.
3826 ;;; (if (looking-at rmail-nonignored-headers)
3827 ;;; (forward-line 1)
3828 (delete-region (point)
3829 (save-excursion
3830 (if (re-search-forward "\n[^ \t]" nil t)
3831 (1- (point))
3832 (point-max)))))))
3834 (declare-function mail-sendmail-delimit-header "sendmail" ())
3835 (declare-function mail-header-end "sendmail" ())
3836 (declare-function mail-position-on-field "sendmail" (field &optional soft))
3838 (defun rmail-retry-failure ()
3839 "Edit a mail message which is based on the contents of the current message.
3840 For a message rejected by the mail system, extract the interesting headers and
3841 the body of the original message.
3842 If the failed message is a MIME multipart message, it is searched for a
3843 body part with a header which matches the variable `mail-mime-unsent-header'.
3844 Otherwise, the variable `mail-unsent-separator' should match the string that
3845 delimits the returned original message.
3846 The variable `rmail-retry-ignored-headers' is a regular expression
3847 specifying headers which should not be copied into the new message."
3848 (interactive)
3849 (require 'mail-utils)
3850 (let ((rmail-this-buffer (current-buffer))
3851 (msgnum rmail-current-message)
3852 bounce-start bounce-end bounce-indent resending
3853 (content-type (rmail-get-header "Content-Type")))
3854 (save-excursion
3855 (goto-char (point-min))
3856 (let ((case-fold-search t))
3857 (if (and content-type
3858 (string-match
3859 ";[\n\t ]*boundary=\"?\\([-0-9a-z'()+_,./:=? ]+\\)\"?"
3860 content-type))
3861 ;; Handle a MIME multipart bounce message.
3862 (let ((codestring
3863 (concat "\n--"
3864 (substring content-type (match-beginning 1)
3865 (match-end 1)))))
3866 (unless (re-search-forward mail-mime-unsent-header nil t)
3867 (error "Cannot find beginning of header in failed message"))
3868 (unless (search-forward "\n\n" nil t)
3869 (error "Cannot find start of Mime data in failed message"))
3870 (setq bounce-start (point))
3871 (if (search-forward codestring nil t)
3872 (setq bounce-end (match-beginning 0))
3873 (setq bounce-end (point-max))))
3874 ;; Non-MIME bounce.
3875 (or (re-search-forward mail-unsent-separator nil t)
3876 (error "Cannot parse this as a failure message"))
3877 (skip-chars-forward "\n")
3878 ;; Support a style of failure message in which the original
3879 ;; message is indented, and included within lines saying
3880 ;; `Start of returned message' and `End of returned message'.
3881 (if (looking-at " +Received:")
3882 (progn
3883 (setq bounce-start (point))
3884 (skip-chars-forward " ")
3885 (setq bounce-indent (- (current-column)))
3886 (goto-char (point-max))
3887 (re-search-backward "^End of returned message$" nil t)
3888 (setq bounce-end (point)))
3889 ;; One message contained a few random lines before
3890 ;; the old message header. The first line of the
3891 ;; message started with two hyphens. A blank line
3892 ;; followed these random lines. The same line
3893 ;; beginning with two hyphens was possibly marking
3894 ;; the end of the message.
3895 (if (looking-at "^--")
3896 (let ((boundary (buffer-substring-no-properties
3897 (point)
3898 (progn (end-of-line) (point)))))
3899 (search-forward "\n\n")
3900 (skip-chars-forward "\n")
3901 (setq bounce-start (point))
3902 (goto-char (point-max))
3903 (search-backward (concat "\n\n" boundary) bounce-start t)
3904 (setq bounce-end (point)))
3905 (setq bounce-start (point)
3906 bounce-end (point-max)))
3907 (unless (search-forward "\n\n" nil t)
3908 (error "Cannot find end of header in failed message"))))))
3909 ;; We have found the message that bounced, within the current message.
3910 ;; Now start sending new message; default header fields from original.
3911 ;; Turn off the usual actions for initializing the message body
3912 ;; because we want to get only the text from the failure message.
3913 (let (mail-signature mail-setup-hook)
3914 (if (rmail-start-mail nil nil nil nil nil rmail-this-buffer
3915 (list (list 'rmail-mark-message
3916 rmail-this-buffer
3917 (aref rmail-msgref-vector msgnum)
3918 rmail-retried-attr-index)))
3919 ;; Insert original text as initial text of new draft message.
3920 ;; Bind inhibit-read-only since the header delimiter
3921 ;; of the previous message was probably read-only.
3922 (let ((inhibit-read-only t))
3923 (erase-buffer)
3924 (insert-buffer-substring rmail-this-buffer
3925 bounce-start bounce-end)
3926 (goto-char (point-min))
3927 (if bounce-indent
3928 (indent-rigidly (point-min) (point-max) bounce-indent))
3929 ;; FIXME better to replace sendmail functions.
3930 (require 'sendmail)
3931 (mail-sendmail-delimit-header)
3932 (save-restriction
3933 (narrow-to-region (point-min) (mail-header-end))
3934 (rmail-delete-headers rmail-retry-ignored-headers)
3935 (rmail-delete-headers "^\\(sender\\|return-path\\|received\\):")
3936 (setq resending (mail-fetch-field "resent-to"))
3937 (if mail-self-blind
3938 (if resending
3939 (insert "Resent-Bcc: " (user-login-name) "\n")
3940 (insert "BCC: " (user-login-name) "\n"))))
3941 (goto-char (point-min))
3942 (mail-position-on-field (if resending "Resent-To" "To") t))))))
3944 (defun rmail-summary-exists ()
3945 "Non-nil if in an RMAIL buffer and an associated summary buffer exists.
3946 In fact, the non-nil value returned is the summary buffer itself."
3947 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
3948 rmail-summary-buffer))
3950 (defun rmail-summary-displayed ()
3951 "t if in RMAIL buffer and an associated summary buffer is displayed."
3952 (and rmail-summary-buffer (get-buffer-window rmail-summary-buffer)))
3954 (defcustom rmail-redisplay-summary nil
3955 "Non-nil means Rmail should show the summary when it changes.
3956 This has an effect only if a summary buffer exists."
3957 :type 'boolean
3958 :group 'rmail-summary)
3960 (defcustom rmail-summary-window-size nil
3961 "Non-nil means specify the height for an Rmail summary window."
3962 :type '(choice (const :tag "Disabled" nil) integer)
3963 :group 'rmail-summary)
3965 ;; Put the summary buffer back on the screen, if user wants that.
3966 (defun rmail-maybe-display-summary ()
3967 (let ((selected (selected-window))
3968 window)
3969 ;; If requested, make sure the summary is displayed.
3970 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
3971 rmail-redisplay-summary
3972 (if (get-buffer-window rmail-summary-buffer 0)
3973 ;; It's already in some frame; show that one.
3974 (let ((frame (window-frame
3975 (get-buffer-window rmail-summary-buffer 0))))
3976 (make-frame-visible frame)
3977 (raise-frame frame))
3978 (display-buffer rmail-summary-buffer)))
3979 ;; If requested, set the height of the summary window.
3980 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
3981 rmail-summary-window-size
3982 (setq window (get-buffer-window rmail-summary-buffer))
3983 ;; Don't try to change the size if just one window in frame.
3984 (not (eq window (frame-root-window (window-frame window))))
3985 (unwind-protect
3986 (progn
3987 (select-window window)
3988 (enlarge-window (- rmail-summary-window-size (window-height))))
3989 (select-window selected)))))
3991 ;;;; *** Rmail Local Fontification ***
3993 (defun rmail-fontify-buffer-function ()
3994 ;; This function's symbol is bound to font-lock-fontify-buffer-function.
3995 (add-hook 'rmail-show-message-hook 'rmail-fontify-message nil t)
3996 ;; If we're already showing a message, fontify it now.
3997 (if rmail-current-message (rmail-fontify-message))
3998 ;; Prevent Font Lock mode from kicking in.
3999 (setq font-lock-fontified t))
4001 (defun rmail-unfontify-buffer-function ()
4002 ;; This function's symbol is bound to font-lock-fontify-unbuffer-function.
4003 (let ((modified (buffer-modified-p))
4004 (buffer-undo-list t) (inhibit-read-only t)
4005 before-change-functions after-change-functions
4006 buffer-file-name buffer-file-truename)
4007 (save-restriction
4008 (widen)
4009 (remove-hook 'rmail-show-message-hook 'rmail-fontify-message t)
4010 (remove-text-properties (point-min) (point-max) '(rmail-fontified nil))
4011 (font-lock-default-unfontify-buffer)
4012 (and (not modified) (buffer-modified-p)
4013 (restore-buffer-modified-p nil)))))
4015 (defun rmail-fontify-message ()
4016 ;; Fontify the current message if it is not already fontified.
4017 (if (text-property-any (point-min) (point-max) 'rmail-fontified nil)
4018 (let ((modified (buffer-modified-p))
4019 (buffer-undo-list t) (inhibit-read-only t)
4020 before-change-functions after-change-functions
4021 buffer-file-name buffer-file-truename)
4022 (save-excursion
4023 (save-match-data
4024 (add-text-properties (point-min) (point-max) '(rmail-fontified t))
4025 (font-lock-fontify-region (point-min) (point-max))
4026 (and (not modified) (buffer-modified-p)
4027 (restore-buffer-modified-p nil)))))))
4029 ;;; Speedbar support for RMAIL files.
4030 (eval-when-compile (require 'speedbar))
4032 (defvar rmail-speedbar-match-folder-regexp "^[A-Z0-9]+\\(\\.[A-Z0-9]+\\)?$"
4033 "*This regex is used to match folder names to be displayed in speedbar.
4034 Enabling this will permit speedbar to display your folders for easy
4035 browsing, and moving of messages.")
4037 (defvar rmail-speedbar-last-user nil
4038 "The last user to be displayed in the speedbar.")
4040 (defvar rmail-speedbar-key-map nil
4041 "Keymap used when in rmail display mode.")
4043 (defun rmail-install-speedbar-variables ()
4044 "Install those variables used by speedbar to enhance rmail."
4045 (if rmail-speedbar-key-map
4047 (setq rmail-speedbar-key-map (speedbar-make-specialized-keymap))
4049 (define-key rmail-speedbar-key-map "e" 'speedbar-edit-line)
4050 (define-key rmail-speedbar-key-map "r" 'speedbar-edit-line)
4051 (define-key rmail-speedbar-key-map "\C-m" 'speedbar-edit-line)
4052 (define-key rmail-speedbar-key-map "M"
4053 'rmail-speedbar-move-message-to-folder-on-line)))
4055 ;; Mouse-3.
4056 (defvar rmail-speedbar-menu-items
4057 '(["Read Folder" speedbar-edit-line t]
4058 ["Move message to folder" rmail-speedbar-move-message-to-folder-on-line
4059 (save-excursion (beginning-of-line)
4060 (looking-at "<M> "))])
4061 "Additional menu-items to add to speedbar frame.")
4063 ;; Make sure our special speedbar major mode is loaded
4064 (if (featurep 'speedbar)
4065 (rmail-install-speedbar-variables)
4066 (add-hook 'speedbar-load-hook 'rmail-install-speedbar-variables))
4068 (defun rmail-speedbar-buttons (buffer)
4069 "Create buttons for BUFFER containing rmail messages.
4070 Click on the address under Reply to: to reply to this person.
4071 Under Folders: Click a name to read it, or on the <M> to move the
4072 current message into that RMAIL folder."
4073 (let ((from nil))
4074 (with-current-buffer buffer
4075 (goto-char (point-min))
4076 (if (not (re-search-forward "^Reply-To: " nil t))
4077 (if (not (re-search-forward "^From:? " nil t))
4078 (setq from t)))
4079 (if from
4081 (setq from (buffer-substring (point) (line-end-position)))))
4082 (goto-char (point-min))
4083 (if (and (looking-at "Reply to:")
4084 (equal from rmail-speedbar-last-user))
4086 (setq rmail-speedbar-last-user from)
4087 (erase-buffer)
4088 (insert "Reply To:\n")
4089 (if (stringp from)
4090 (speedbar-insert-button from 'speedbar-directory-face 'highlight
4091 'rmail-speedbar-button 'rmail-reply))
4092 (insert "Folders:\n")
4093 (let* ((case-fold-search nil)
4094 (df (directory-files (with-current-buffer buffer
4095 default-directory)
4096 nil rmail-speedbar-match-folder-regexp)))
4097 (dolist (file df)
4098 (when (file-regular-p file)
4099 (speedbar-insert-button "<M>" 'speedbar-button-face 'highlight
4100 'rmail-speedbar-move-message file)
4101 (speedbar-insert-button file 'speedbar-file-face 'highlight
4102 'rmail-speedbar-find-file nil t)))))))
4104 (defun rmail-speedbar-button (text token indent)
4105 "Execute an rmail command specified by TEXT.
4106 The command used is TOKEN. INDENT is not used."
4107 (speedbar-with-attached-buffer
4108 (funcall token t)))
4110 (defun rmail-speedbar-find-file (text token indent)
4111 "Load in the rmail file TEXT.
4112 TOKEN and INDENT are not used."
4113 (speedbar-with-attached-buffer
4114 (message "Loading in RMAIL file %s..." text)
4115 (rmail text)))
4117 (defun rmail-speedbar-move-message-to-folder-on-line ()
4118 "If the current line is a folder, move current message to it."
4119 (interactive)
4120 (save-excursion
4121 (beginning-of-line)
4122 (if (re-search-forward "<M> " (line-end-position) t)
4123 (progn
4124 (forward-char -2)
4125 (speedbar-do-function-pointer)))))
4127 (defun rmail-speedbar-move-message (text token indent)
4128 "From button TEXT, copy current message to the rmail file specified by TOKEN.
4129 TEXT and INDENT are not used."
4130 (speedbar-with-attached-buffer
4131 (message "Moving message to %s" token)
4132 ;; expand-file-name is needed due to the unhelpful way in which
4133 ;; rmail-output expands non-absolute filenames against rmail-default-file.
4134 ;; What is the point of that, anyway?
4135 (rmail-output (expand-file-name token))))
4137 ;; Functions for setting, getting and encoding the POP password.
4138 ;; The password is encoded to prevent it from being easily accessible
4139 ;; to "prying eyes." Obviously, this encoding isn't "real security,"
4140 ;; nor is it meant to be.
4142 ;;;###autoload
4143 (defun rmail-set-remote-password (password)
4144 "Set PASSWORD to be used for retrieving mail from a POP or IMAP server."
4145 (interactive "sPassword: ")
4146 (if password
4147 (setq rmail-encoded-remote-password
4148 (rmail-encode-string password (emacs-pid)))
4149 (setq rmail-remote-password nil)
4150 (setq rmail-encoded-remote-password nil)))
4152 (defun rmail-get-remote-password (imap)
4153 "Get the password for retrieving mail from a POP or IMAP server. If none
4154 has been set, then prompt the user for one."
4155 (when (not rmail-encoded-remote-password)
4156 (if (not rmail-remote-password)
4157 (setq rmail-remote-password
4158 (read-passwd (if imap
4159 "IMAP password: "
4160 "POP password: "))))
4161 (rmail-set-remote-password rmail-remote-password)
4162 (setq rmail-remote-password nil))
4163 (rmail-encode-string rmail-encoded-remote-password (emacs-pid)))
4165 (defun rmail-have-password ()
4166 (or rmail-remote-password rmail-encoded-remote-password))
4168 (defun rmail-encode-string (string mask)
4169 "Encode STRING with integer MASK, by taking the exclusive OR of the
4170 lowest byte in the mask with the first character of string, the
4171 second-lowest-byte with the second character of the string, etc.,
4172 restarting at the lowest byte of the mask whenever it runs out.
4173 Returns the encoded string. Calling the function again with an
4174 encoded string (and the same mask) will decode the string."
4175 (setq mask (abs mask)) ; doesn't work if negative
4176 (let* ((string-vector (string-to-vector string)) (i 0)
4177 (len (length string-vector)) (curmask mask) charmask)
4178 (while (< i len)
4179 (if (= curmask 0)
4180 (setq curmask mask))
4181 (setq charmask (% curmask 256))
4182 (setq curmask (lsh curmask -8))
4183 (aset string-vector i (logxor charmask (aref string-vector i)))
4184 (setq i (1+ i)))
4185 (concat string-vector)))
4187 ;;;; Desktop support
4189 (defun rmail-restore-desktop-buffer (desktop-buffer-file-name
4190 desktop-buffer-name
4191 desktop-buffer-misc)
4192 "Restore an rmail buffer specified in a desktop file."
4193 (condition-case error
4194 (progn
4195 (rmail-input desktop-buffer-file-name)
4196 (if (eq major-mode 'rmail-mode)
4197 (current-buffer)
4198 rmail-buffer))
4199 (file-locked
4200 (kill-buffer (current-buffer))
4201 nil)))
4203 (add-to-list 'desktop-buffer-mode-handlers
4204 '(rmail-mode . rmail-restore-desktop-buffer))
4206 ;; We use this to record the encoding of the current message before
4207 ;; saving the message collection.
4208 (defvar rmail-message-encoding nil)
4210 ;; Used in `write-region-annotate-functions' to write rmail files.
4211 (defun rmail-write-region-annotate (start end)
4212 (when (and (null start) (rmail-buffers-swapped-p))
4213 (setq rmail-message-encoding buffer-file-coding-system)
4214 (set-buffer rmail-view-buffer)
4215 (widen)
4216 nil))
4218 ;; Used to restore the encoding of the buffer where we show the
4219 ;; current message, after we save the message collection. This is
4220 ;; needed because rmail-write-region-annotate switches buffers behind
4221 ;; save-file's back, with the side effect that last-coding-system-used
4222 ;; is assigned to buffer-file-coding-system of the wrong buffer.
4223 (defun rmail-after-save-hook ()
4224 (if (or (eq rmail-view-buffer (current-buffer))
4225 (eq rmail-buffer (current-buffer)))
4226 (with-current-buffer
4227 (if (rmail-buffers-swapped-p) rmail-buffer rmail-view-buffer)
4228 (setq buffer-file-coding-system rmail-message-encoding))))
4229 (add-hook 'after-save-hook 'rmail-after-save-hook)
4232 ;;; Start of automatically extracted autoloads.
4234 ;;;### (autoloads (rmail-edit-current-message) "rmailedit" "rmailedit.el"
4235 ;;;;;; "ecd28d8d92983488673388eced6fbf50")
4236 ;;; Generated autoloads from rmailedit.el
4238 (autoload 'rmail-edit-current-message "rmailedit" "\
4239 Edit the contents of this message.
4241 \(fn)" t nil)
4243 ;;;***
4245 ;;;### (autoloads (rmail-next-labeled-message rmail-previous-labeled-message
4246 ;;;;;; rmail-read-label rmail-kill-label rmail-add-label) "rmailkwd"
4247 ;;;;;; "rmailkwd.el" "bc72ffe3652be6f4c72048ae8f226fce")
4248 ;;; Generated autoloads from rmailkwd.el
4250 (autoload 'rmail-add-label "rmailkwd" "\
4251 Add LABEL to labels associated with current RMAIL message.
4252 Completes (see `rmail-read-label') over known labels when reading.
4253 LABEL may be a symbol or string. Only one label is allowed.
4255 \(fn LABEL)" t nil)
4257 (autoload 'rmail-kill-label "rmailkwd" "\
4258 Remove LABEL from labels associated with current RMAIL message.
4259 Completes (see `rmail-read-label') over known labels when reading.
4260 LABEL may be a symbol or string. Only one label is allowed.
4262 \(fn LABEL)" t nil)
4264 (autoload 'rmail-read-label "rmailkwd" "\
4265 Read a label with completion, prompting with PROMPT.
4266 Completions are chosen from `rmail-label-obarray'. The default
4267 is `rmail-last-label', if that is non-nil. Updates `rmail-last-label'
4268 according to the choice made, and returns a symbol.
4270 \(fn PROMPT)" nil nil)
4272 (autoload 'rmail-previous-labeled-message "rmailkwd" "\
4273 Show previous message with one of the labels LABELS.
4274 LABELS should be a comma-separated list of label names.
4275 If LABELS is empty, the last set of labels specified is used.
4276 With prefix argument N moves backward N messages with these labels.
4278 \(fn N LABELS)" t nil)
4280 (autoload 'rmail-next-labeled-message "rmailkwd" "\
4281 Show next message with one of the labels LABELS.
4282 LABELS should be a comma-separated list of label names.
4283 If LABELS is empty, the last set of labels specified is used.
4284 With prefix argument N moves forward N messages with these labels.
4286 \(fn N LABELS)" t nil)
4288 ;;;***
4290 ;;;### (autoloads (rmail-mime) "rmailmm" "rmailmm.el" "7f6ad821b4543a18139fee9250beea5c")
4291 ;;; Generated autoloads from rmailmm.el
4293 (autoload 'rmail-mime "rmailmm" "\
4294 Toggle displaying of a MIME message.
4296 The actualy behavior depends on the value of `rmail-enable-mime'.
4298 If `rmail-enable-mime' is t (default), this command change the
4299 displaying of a MIME message between decoded presentation form
4300 and raw data.
4302 With ARG, toggle the displaying of the current MIME entity only.
4304 If `rmail-enable-mime' is nil, this creates a temporary
4305 \"*RMAIL*\" buffer holding a decoded copy of the message. Inline
4306 content-types are handled according to
4307 `rmail-mime-media-type-handlers-alist'. By default, this
4308 displays text and multipart messages, and offers to download
4309 attachments as specfied by `rmail-mime-attachment-dirs-alist'.
4311 \(fn &optional ARG)" t nil)
4313 ;;;***
4315 ;;;### (autoloads (set-rmail-inbox-list) "rmailmsc" "rmailmsc.el"
4316 ;;;;;; "9319e5b606ad5786c0c5994a307a38e8")
4317 ;;; Generated autoloads from rmailmsc.el
4319 (autoload 'set-rmail-inbox-list "rmailmsc" "\
4320 Set the inbox list of the current RMAIL file to FILE-NAME.
4321 You can specify one file name, or several names separated by commas.
4322 If FILE-NAME is empty, remove any existing inbox list.
4324 This applies only to the current session.
4326 \(fn FILE-NAME)" t nil)
4328 ;;;***
4330 ;;;### (autoloads (rmail-sort-by-labels rmail-sort-by-lines rmail-sort-by-correspondent
4331 ;;;;;; rmail-sort-by-recipient rmail-sort-by-author rmail-sort-by-subject
4332 ;;;;;; rmail-sort-by-date) "rmailsort" "rmailsort.el" "16144a77fdc880034875fd624e4d73e6")
4333 ;;; Generated autoloads from rmailsort.el
4335 (autoload 'rmail-sort-by-date "rmailsort" "\
4336 Sort messages of current Rmail buffer by \"Date\" header.
4337 If prefix argument REVERSE is non-nil, sorts in reverse order.
4339 \(fn REVERSE)" t nil)
4341 (autoload 'rmail-sort-by-subject "rmailsort" "\
4342 Sort messages of current Rmail buffer by \"Subject\" header.
4343 Ignores any \"Re: \" prefix. If prefix argument REVERSE is
4344 non-nil, sorts in reverse order.
4346 \(fn REVERSE)" t nil)
4348 (autoload 'rmail-sort-by-author "rmailsort" "\
4349 Sort messages of current Rmail buffer by author.
4350 This uses either the \"From\" or \"Sender\" header, downcased.
4351 If prefix argument REVERSE is non-nil, sorts in reverse order.
4353 \(fn REVERSE)" t nil)
4355 (autoload 'rmail-sort-by-recipient "rmailsort" "\
4356 Sort messages of current Rmail buffer by recipient.
4357 This uses either the \"To\" or \"Apparently-To\" header, downcased.
4358 If prefix argument REVERSE is non-nil, sorts in reverse order.
4360 \(fn REVERSE)" t nil)
4362 (autoload 'rmail-sort-by-correspondent "rmailsort" "\
4363 Sort messages of current Rmail buffer by other correspondent.
4364 This uses either the \"From\", \"Sender\", \"To\", or
4365 \"Apparently-To\" header, downcased. Uses the first header not
4366 excluded by `rmail-dont-reply-to-names'. If prefix argument
4367 REVERSE is non-nil, sorts in reverse order.
4369 \(fn REVERSE)" t nil)
4371 (autoload 'rmail-sort-by-lines "rmailsort" "\
4372 Sort messages of current Rmail buffer by the number of lines.
4373 If prefix argument REVERSE is non-nil, sorts in reverse order.
4375 \(fn REVERSE)" t nil)
4377 (autoload 'rmail-sort-by-labels "rmailsort" "\
4378 Sort messages of current Rmail buffer by labels.
4379 LABELS is a comma-separated list of labels. The order of these
4380 labels specifies the order of messages: messages with the first
4381 label come first, messages with the second label come second, and
4382 so on. Messages that have none of these labels come last.
4383 If prefix argument REVERSE is non-nil, sorts in reverse order.
4385 \(fn REVERSE LABELS)" t nil)
4387 ;;;***
4389 ;;;### (autoloads (rmail-summary-by-senders rmail-summary-by-topic
4390 ;;;;;; rmail-summary-by-regexp rmail-summary-by-recipients rmail-summary-by-labels
4391 ;;;;;; rmail-summary) "rmailsum" "rmailsum.el" "6bcfd5937a56902944a929b89b33adaa")
4392 ;;; Generated autoloads from rmailsum.el
4394 (autoload 'rmail-summary "rmailsum" "\
4395 Display a summary of all messages, one line per message.
4397 \(fn)" t nil)
4399 (autoload 'rmail-summary-by-labels "rmailsum" "\
4400 Display a summary of all messages with one or more LABELS.
4401 LABELS should be a string containing the desired labels, separated by commas.
4403 \(fn LABELS)" t nil)
4405 (autoload 'rmail-summary-by-recipients "rmailsum" "\
4406 Display a summary of all messages with the given RECIPIENTS.
4407 Normally checks the To, From and Cc fields of headers;
4408 but if PRIMARY-ONLY is non-nil (prefix arg given),
4409 only look in the To and From fields.
4410 RECIPIENTS is a string of regexps separated by commas.
4412 \(fn RECIPIENTS &optional PRIMARY-ONLY)" t nil)
4414 (autoload 'rmail-summary-by-regexp "rmailsum" "\
4415 Display a summary of all messages according to regexp REGEXP.
4416 If the regular expression is found in the header of the message
4417 \(including in the date and other lines, as well as the subject line),
4418 Emacs will list the message in the summary.
4420 \(fn REGEXP)" t nil)
4422 (autoload 'rmail-summary-by-topic "rmailsum" "\
4423 Display a summary of all messages with the given SUBJECT.
4424 Normally checks just the Subject field of headers; but with prefix
4425 argument WHOLE-MESSAGE is non-nil, looks in the whole message.
4426 SUBJECT is a string of regexps separated by commas.
4428 \(fn SUBJECT &optional WHOLE-MESSAGE)" t nil)
4430 (autoload 'rmail-summary-by-senders "rmailsum" "\
4431 Display a summary of all messages whose \"From\" field matches SENDERS.
4432 SENDERS is a string of regexps separated by commas.
4434 \(fn SENDERS)" t nil)
4436 ;;;***
4438 ;;;### (autoloads (unforward-rmail-message undigestify-rmail-message)
4439 ;;;;;; "undigest" "undigest.el" "2869c38a0051d0acab1a5968627fa57d")
4440 ;;; Generated autoloads from undigest.el
4442 (autoload 'undigestify-rmail-message "undigest" "\
4443 Break up a digest message into its constituent messages.
4444 Leaves original message, deleted, before the undigestified messages.
4446 \(fn)" t nil)
4448 (autoload 'unforward-rmail-message "undigest" "\
4449 Extract a forwarded message from the containing message.
4450 This puts the forwarded message into a separate rmail message
4451 following the containing message.
4453 \(fn)" t nil)
4455 ;;;***
4457 ;;; End of automatically extracted autoloads.
4460 (provide 'rmail)
4462 ;; arch-tag: 65d257d3-c281-4a65-9c38-e61af95af2f0
4463 ;;; rmail.el ends here