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