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