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