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