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