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