* lisp/mail/rmail.el (rmail-set-header-1): Handle multi-line headers
[bpt/emacs.git] / lisp / mail / rmail.el
1 ;;; rmail.el --- main code of "RMAIL" mail reader for Emacs
2
3 ;; Copyright (C) 1985-1988, 1993-1998, 2000-2013 Free Software
4 ;; Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: mail
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 ;; Souped up by shane@mit-ajax based on ideas of rlk@athena.mit.edu
29 ;; New features include attribute and keyword support, message
30 ;; selection by dispatch table, summary by attributes and keywords,
31 ;; expunging by dispatch table, sticky options for file commands.
32
33 ;; Extended by Bob Weiner of Motorola
34 ;; New features include: rmail and rmail-summary buffers remain
35 ;; synchronized and key bindings basically operate the same way in both
36 ;; buffers, summary by topic or by regular expression, rmail-reply-prefix
37 ;; variable, and a bury rmail buffer (wipe) command.
38 ;;
39
40 (require 'mail-utils)
41 (require 'rfc2047)
42
43 (declare-function compilation--message->loc "compile" (cl-x) t)
44 (declare-function epa--find-coding-system-for-mime-charset "epa" (mime-charset))
45
46 (defconst rmail-attribute-header "X-RMAIL-ATTRIBUTES"
47 "The header that stores the Rmail attribute data.")
48
49 (defconst rmail-keyword-header "X-RMAIL-KEYWORDS"
50 "The header that stores the Rmail keyword data.")
51
52 ;;; Attribute indexes
53
54 (defconst rmail-answered-attr-index 0
55 "The index for the `answered' attribute.")
56
57 (defconst rmail-deleted-attr-index 1
58 "The index for the `deleted' attribute.")
59
60 (defconst rmail-edited-attr-index 2
61 "The index for the `edited' attribute.")
62
63 (defconst rmail-filed-attr-index 3
64 "The index for the `filed' attribute.")
65
66 (defconst rmail-retried-attr-index 4
67 "The index for the `retried' attribute.")
68
69 (defconst rmail-forwarded-attr-index 5
70 "The index for the `forwarded' attribute.")
71
72 (defconst rmail-unseen-attr-index 6
73 "The index for the `unseen' attribute.")
74
75 (defconst rmail-resent-attr-index 7
76 "The index for the `resent' attribute.")
77
78 (defconst rmail-attr-array
79 '[(?A "answered")
80 (?D "deleted")
81 (?E "edited")
82 (?F "filed")
83 (?R "retried")
84 (?S "forwarded")
85 (?U "unseen")
86 (?r "resent")]
87 "An array that provides a mapping between an attribute index,
88 its character representation and its display representation.")
89
90 (defvar deleted-head)
91 (defvar font-lock-fontified)
92 (defvar mail-abbrev-syntax-table)
93 (defvar mail-abbrevs)
94 (defvar messages-head)
95 (defvar total-messages)
96 (defvar tool-bar-map)
97 (defvar mail-encode-mml)
98
99 (defvar rmail-header-style 'normal
100 "The current header display style choice, one of
101 'normal (selected headers) or 'full (all headers).")
102
103 (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 headers, set header NAME to VALUE, replacing existing if present.
2177 VALUE nil means to remove NAME altogether.
2178
2179 Only changes the first instance of NAME. If VALUE is multi-line,
2180 continuation lines should already be indented. VALUE should not
2181 end in a newline."
2182 (if (search-forward "\n\n" nil t)
2183 (progn
2184 (forward-char -1)
2185 (narrow-to-region (point-min) (point))
2186 ;; cf mail-fetch-field.
2187 (goto-char (point-min))
2188 (if (let ((case-fold-search t))
2189 (re-search-forward (concat "^" (regexp-quote name) "[ \t]*:")
2190 nil 'move))
2191 (let ((start (point))
2192 end)
2193 (while (and (zerop (forward-line 1))
2194 (looking-at "[ \t]")))
2195 ;; Back up over newline.
2196 (forward-char -1)
2197 (setq end (point))
2198 (goto-char start)
2199 (if value
2200 (progn
2201 (delete-region start end)
2202 (insert " " value))
2203 (delete-region (line-beginning-position) (1+ end))))
2204 ;; Not already present: insert at end of headers.
2205 (if value (insert name ": " value "\n"))))
2206 (rmail-error-bad-format)))
2207
2208 (defun rmail-set-header (name &optional msgnum value)
2209 "Set message header NAME to VALUE in message number MSGNUM.
2210 If MSGNUM is nil, use the current message. NAME and VALUE are strings.
2211 VALUE may also be nil, meaning to remove the header."
2212 (rmail-apply-in-message msgnum 'rmail-set-header-1 name value)
2213 (with-current-buffer rmail-buffer
2214 ;; Ensure header changes get saved.
2215 ;; (Note replacing a header with an identical copy modifies.)
2216 (set-buffer-modified-p t)
2217 ;; However: don't save in mbox format over a Babyl file
2218 ;; merely because of this.
2219 (rmail-dont-modify-format)))
2220 \f
2221 ;;;; *** Rmail Attributes and Keywords ***
2222
2223 (defun rmail-get-attr-names (&optional msg)
2224 "Return the message attributes in a comma separated string.
2225 MSG specifies the message number to get it from.
2226 If MSG is nil, use the current message."
2227 (let ((value (rmail-get-header rmail-attribute-header msg))
2228 (nmax (length rmail-attr-array))
2229 result temp)
2230 (when value
2231 (if (> (length value) nmax)
2232 (message "Warning: corrupt attribute header in message")
2233 (dotimes (index (length value))
2234 (setq temp (and (not (= ?- (aref value index)))
2235 (nth 1 (aref rmail-attr-array index)))
2236 result
2237 (cond
2238 ((and temp result) (format "%s, %s" result temp))
2239 (temp temp)
2240 (t result)))))
2241 result)))
2242
2243 (defun rmail-get-keywords (&optional msg)
2244 "Return the message keywords in a comma separated string.
2245 MSG, if non-nil, identifies the message number to use.
2246 If nil, that means the current message."
2247 (rmail-get-header rmail-keyword-header msg))
2248
2249 (defun rmail-get-labels (&optional msg)
2250 "Return a string with the labels (attributes and keywords) of msg MSG.
2251 It is put in comma-separated form.
2252 MSG, if non-nil, identifies the message number to use.
2253 If nil, that means the current message."
2254 (or msg (setq msg rmail-current-message))
2255 (let (attr-names keywords)
2256 ;; Combine the message attributes and keywords
2257 ;; into a comma-separated list.
2258 (setq attr-names (rmail-get-attr-names msg)
2259 keywords (rmail-get-keywords msg))
2260 (if (string= keywords "")
2261 (setq keywords nil))
2262 (cond
2263 ;; FIXME ? old rmail did not have spaces in the comma-separated lists.
2264 ((and attr-names keywords) (concat " " attr-names "; " keywords))
2265 (attr-names (concat " " attr-names))
2266 (keywords (concat " " keywords))
2267 (t ""))))
2268
2269 (defun rmail-display-labels ()
2270 "Update the current messages's attributes and keywords in mode line."
2271 (let ((blurb (rmail-get-labels)))
2272 (setq mode-line-process
2273 (format " %d/%d%s"
2274 rmail-current-message rmail-total-messages blurb))))
2275
2276 (defun rmail-get-attr-value (attr state)
2277 "Return the character value for ATTR.
2278 ATTR is a (numeric) index, an offset into the mbox attribute
2279 header value. STATE is one of nil, t, or a character value."
2280 (cond
2281 ((numberp state) state)
2282 ((not state) ?-)
2283 (t (nth 0 (aref rmail-attr-array attr)))))
2284
2285 (defun rmail-set-attribute-1 (attr state)
2286 "Subroutine of `rmail-set-attribute'.
2287 Set Rmail attribute ATTR to STATE in `rmail-attribute-header',
2288 creating the header if necessary. Returns non-nil if a
2289 significant attribute change was made."
2290 (let ((limit (search-forward "\n\n" nil t))
2291 (value (rmail-get-attr-value attr state))
2292 (inhibit-read-only t)
2293 altered)
2294 (goto-char (point-min))
2295 (if (search-forward (concat rmail-attribute-header ": ") limit t)
2296 ;; If this message already records attributes, just change the
2297 ;; value for this one.
2298 (let ((missing (- (+ (point) attr) (line-end-position))))
2299 ;; Position point at this attribute, adding attributes if necessary.
2300 (if (> missing 0)
2301 (progn
2302 (end-of-line)
2303 (insert-char ?- missing)
2304 (backward-char 1))
2305 (forward-char attr))
2306 ;; Change this attribute.
2307 (when (/= value (char-after))
2308 (setq altered t)
2309 (delete-char 1)
2310 (insert value)))
2311 ;; Otherwise add a header line to record the attributes and set
2312 ;; all but this one to no.
2313 (let ((header-value "--------"))
2314 (aset header-value attr value)
2315 (goto-char (if limit (1- limit) (point-max)))
2316 (setq altered (/= value ?-))
2317 (insert rmail-attribute-header ": " header-value "\n")))
2318 altered))
2319
2320 (defun rmail-set-attribute (attr state &optional msgnum)
2321 "Turn an attribute of a message on or off according to STATE.
2322 STATE is either nil or the character (numeric) value associated
2323 with the state (nil represents off and non-nil represents on).
2324 ATTR is either the index number of the attribute, or a string,
2325 both from `rmail-attr-array'. MSGNUM is message number to
2326 change; nil means current message."
2327 (let ((n 0)
2328 (nmax (length rmail-attr-array)))
2329 (while (and (stringp attr)
2330 (< n nmax))
2331 (if (string-equal attr (cadr (aref rmail-attr-array n)))
2332 (setq attr n))
2333 (setq n (1+ n))))
2334 (if (stringp attr)
2335 (error "Unknown attribute `%s'" attr))
2336 ;; Ask for confirmation before setting any attribute except `unseen'
2337 ;; if it would force a format change.
2338 (unless (= attr rmail-unseen-attr-index)
2339 (rmail-modify-format))
2340 (with-current-buffer rmail-buffer
2341 (or msgnum (setq msgnum rmail-current-message))
2342 (when (> msgnum 0)
2343 ;; The "deleted" attribute is also stored in a special vector so
2344 ;; update that too.
2345 (if (= attr rmail-deleted-attr-index)
2346 (rmail-set-message-deleted-p msgnum state))
2347 (if (prog1
2348 (rmail-apply-in-message msgnum 'rmail-set-attribute-1 attr state)
2349 (if (= msgnum rmail-current-message)
2350 (rmail-display-labels)))
2351 ;; Don't save in mbox format over a Babyl file
2352 ;; merely because of a change in `unseen' attribute.
2353 (if (= attr rmail-unseen-attr-index)
2354 (rmail-dont-modify-format)
2355 ;; Otherwise, if we modified the file text via the view buffer,
2356 ;; mark the main buffer modified too.
2357 (set-buffer-modified-p t))))))
2358
2359 (defun rmail-message-attr-p (msg attrs)
2360 "Return non-nil if message number MSG has attributes matching regexp ATTRS."
2361 (let ((value (rmail-get-header rmail-attribute-header msg)))
2362 (and value (string-match attrs value))))
2363
2364 (defun rmail-message-unseen-p (msgnum)
2365 "Return non-nil if message number MSGNUM has the unseen attribute."
2366 (rmail-message-attr-p msgnum "......U"))
2367
2368 ;; FIXME rmail-get-labels does some formatting (eg leading space, `;'
2369 ;; between attributes and labels), so this might not do what you want.
2370 ;; Eg see rmail-sort-by-labels. rmail-get-labels could have an
2371 ;; optional `noformat' argument.
2372 (defun rmail-message-labels-p (msg labels)
2373 "Return non-nil if message number MSG has labels matching regexp LABELS."
2374 (string-match labels (rmail-get-labels msg)))
2375 \f
2376 ;;;; *** Rmail Message Selection And Support ***
2377
2378 (defun rmail-msgend (n)
2379 "Return the end position for message number N."
2380 (marker-position (aref rmail-message-vector (1+ n))))
2381
2382 (defun rmail-msgbeg (n)
2383 "Return the start position for message number N."
2384 (marker-position (aref rmail-message-vector n)))
2385
2386 (defun rmail-apply-in-message (msgnum function &rest args)
2387 "Call FUNCTION on ARGS while narrowed to message MSGNUM.
2388 Point is at the start of the message.
2389 This returns what the call to FUNCTION returns.
2390 If MSGNUM is nil, use the current message."
2391 (with-current-buffer rmail-buffer
2392 (or msgnum (setq msgnum rmail-current-message))
2393 (when (> msgnum 0)
2394 (let (msgbeg msgend)
2395 (setq msgbeg (rmail-msgbeg msgnum))
2396 (setq msgend (rmail-msgend msgnum))
2397 ;; All access to the rmail-buffer's local variables is now finished...
2398 (save-excursion
2399 ;; ... so it is ok to go to a different buffer.
2400 (if (rmail-buffers-swapped-p) (set-buffer rmail-view-buffer))
2401 (save-excursion
2402 (save-restriction
2403 (widen)
2404 (goto-char msgbeg)
2405 (narrow-to-region msgbeg msgend)
2406 (apply function args))))))))
2407
2408 ;; Unused (save for commented out code in rmailedit.el).
2409 (defun rmail-widen-to-current-msgbeg (function)
2410 "Call FUNCTION with point at start of internal data of current message.
2411 Assumes that bounds were previously narrowed to display the message in Rmail.
2412 The bounds are widened enough to move point where desired, then narrowed
2413 again afterward.
2414
2415 FUNCTION may not change the visible text of the message, but it may
2416 change the invisible header text."
2417 (save-excursion
2418 (unwind-protect
2419 (progn
2420 (narrow-to-region (rmail-msgbeg rmail-current-message)
2421 (point-max))
2422 (goto-char (point-min))
2423 (funcall function))
2424 ;; Note: we don't use save-restriction because that does not work right
2425 ;; if changes are made outside the saved restriction
2426 ;; before that restriction is restored.
2427 (narrow-to-region (rmail-msgbeg rmail-current-message)
2428 (rmail-msgend rmail-current-message)))))
2429 \f
2430 ;; Manage the message vectors and counters.
2431
2432 (defun rmail-forget-messages ()
2433 (unwind-protect
2434 (if (vectorp rmail-message-vector)
2435 (let* ((v rmail-message-vector)
2436 (n (length v)))
2437 (dotimes (i n)
2438 (if (aref v i)
2439 (move-marker (aref v i) nil)))))
2440 (setq rmail-message-vector nil)
2441 (setq rmail-msgref-vector nil)
2442 (setq rmail-deleted-vector nil)))
2443
2444 (defun rmail-maybe-set-message-counters ()
2445 (if (not (and rmail-deleted-vector
2446 rmail-message-vector
2447 rmail-current-message
2448 rmail-total-messages))
2449 (rmail-set-message-counters)))
2450
2451 (defun rmail-count-new-messages (&optional nomsg)
2452 "Count the number of new messages.
2453 The buffer should be narrowed to include only the new messages.
2454 Output a helpful message unless NOMSG is non-nil."
2455 (let* ((case-fold-search nil)
2456 (total-messages 0)
2457 (messages-head nil)
2458 (deleted-head nil))
2459 (or nomsg (message "Counting new messages..."))
2460 (goto-char (point-max))
2461 ;; Put at the end of messages-head
2462 ;; the entry for message N+1, which marks
2463 ;; the end of message N. (N = number of messages).
2464 (setq messages-head (list (point-marker)))
2465 (rmail-set-message-counters-counter (point-min))
2466 (setq rmail-current-message (1+ rmail-total-messages))
2467 (setq rmail-total-messages
2468 (+ rmail-total-messages total-messages))
2469 (setq rmail-message-vector
2470 (vconcat rmail-message-vector (cdr messages-head)))
2471 (aset rmail-message-vector
2472 rmail-current-message (car messages-head))
2473 (setq rmail-deleted-vector
2474 (concat rmail-deleted-vector deleted-head))
2475 (setq rmail-summary-vector
2476 (vconcat rmail-summary-vector (make-vector total-messages nil)))
2477 (setq rmail-msgref-vector
2478 (vconcat rmail-msgref-vector (make-vector total-messages nil)))
2479 ;; Fill in the new elements of rmail-msgref-vector.
2480 (let ((i (1+ (- rmail-total-messages total-messages))))
2481 (while (<= i rmail-total-messages)
2482 (aset rmail-msgref-vector i (list i))
2483 (setq i (1+ i))))
2484 (goto-char (point-min))
2485 (or nomsg (message "Counting new messages...done (%d)" total-messages))))
2486
2487 (defun rmail-set-message-counters ()
2488 (rmail-forget-messages)
2489 (save-excursion
2490 (save-restriction
2491 (widen)
2492 (let* ((point-save (point))
2493 (total-messages 0)
2494 (messages-after-point)
2495 (case-fold-search nil)
2496 (messages-head nil)
2497 (deleted-head nil))
2498 ;; Determine how many messages follow point.
2499 (message "Counting messages...")
2500 (goto-char (point-max))
2501 ;; Put at the end of messages-head
2502 ;; the entry for message N+1, which marks
2503 ;; the end of message N. (N = number of messages).
2504 (setq messages-head (list (point-marker)))
2505 (setq messages-after-point
2506 (or (rmail-set-message-counters-counter (min (point) point-save))
2507 0))
2508
2509 (setq rmail-total-messages total-messages)
2510 (setq rmail-current-message
2511 (min total-messages
2512 (max 1 (- total-messages messages-after-point))))
2513
2514 ;; Make an element 0 in rmail-message-vector and rmail-deleted-vector
2515 ;; which will never be used.
2516 (push nil messages-head)
2517 (push ?0 deleted-head)
2518 (setq rmail-message-vector (apply 'vector messages-head)
2519 rmail-deleted-vector (concat deleted-head))
2520
2521 (setq rmail-summary-vector (make-vector rmail-total-messages nil)
2522 rmail-msgref-vector (make-vector (1+ rmail-total-messages) nil))
2523
2524 (let ((i 0))
2525 (while (<= i rmail-total-messages)
2526 (aset rmail-msgref-vector i (list i))
2527 (setq i (1+ i))))
2528 (let ((i 0))
2529 (while (<= i rmail-total-messages)
2530 (rmail-set-message-deleted-p i (rmail-message-attr-p i ".D"))
2531 (setq i (1+ i))))
2532 (message "Counting messages...done")))))
2533
2534
2535 (defsubst rmail-collect-deleted (message-end)
2536 "Collect the message deletion flags for each message.
2537 MESSAGE-END is the buffer position corresponding to the end of
2538 the message. Point is at the beginning of the message."
2539 ;; NOTE: This piece of code will be executed on a per-message basis.
2540 ;; In the face of thousands of messages, it has to be as fast as
2541 ;; possible, hence some brute force constant use is employed in
2542 ;; addition to inlining.
2543 (save-excursion
2544 (setq deleted-head
2545 (cons (if (and (search-forward (concat rmail-attribute-header ": ") message-end t)
2546 (looking-at "?D"))
2547 ?D
2548 ?\s) deleted-head))))
2549
2550 (defun rmail-set-message-counters-counter (&optional spot-to-find)
2551 "Collect the start positions of messages in list `messages-head'.
2552 Return the number of messages after the one containing SPOT-TO-FIND."
2553 (let ((start (point))
2554 messages-after-spot)
2555 (while (search-backward "\n\nFrom " nil t)
2556 (forward-char 2)
2557 (when (looking-at rmail-unix-mail-delimiter)
2558 (if (and (<= (point) spot-to-find)
2559 (null messages-after-spot))
2560 (setq messages-after-spot total-messages))
2561 (rmail-collect-deleted start)
2562 (setq messages-head (cons (point-marker) messages-head)
2563 total-messages (1+ total-messages)
2564 start (point))
2565 ;; Show progress after every 20 messages or so.
2566 (if (zerop (% total-messages 20))
2567 (message "Counting messages...%d" total-messages))))
2568 ;; Handle the first message, maybe.
2569 (goto-char (point-min))
2570 (unless (not (looking-at rmail-unix-mail-delimiter))
2571 (if (and (<= (point) spot-to-find)
2572 (null messages-after-spot))
2573 (setq messages-after-spot total-messages))
2574 (rmail-collect-deleted start)
2575 (setq messages-head (cons (point-marker) messages-head)
2576 total-messages (1+ total-messages)))
2577 messages-after-spot))
2578 \f
2579 ;; Display a message.
2580
2581 ;;;; *** Rmail Message Formatting and Header Manipulation ***
2582
2583 ;; This is used outside of rmail.
2584 (defun rmail-msg-is-pruned ()
2585 "Return nil if the current message is showing full headers."
2586 (with-current-buffer (if (rmail-buffers-swapped-p) rmail-view-buffer
2587 rmail-buffer)
2588 (eq rmail-header-style 'normal)))
2589
2590 (defun rmail-toggle-header (&optional arg)
2591 "Toggle between showing full and normal message headers.
2592 With optional integer ARG, show the normal message header if ARG
2593 is greater than zero; otherwise, show it in full."
2594 (interactive "P")
2595 (let ((rmail-header-style
2596 (if (numberp arg)
2597 (if (> arg 0) 'normal 'full)
2598 (if (rmail-msg-is-pruned) 'full 'normal))))
2599 (rmail-show-message)))
2600
2601 (defun rmail-beginning-of-message ()
2602 "Show current message starting from the beginning."
2603 (interactive)
2604 (let ((rmail-show-message-hook '((lambda () (goto-char (point-min)))))
2605 (rmail-header-style (with-current-buffer (if (rmail-buffers-swapped-p)
2606 rmail-view-buffer
2607 rmail-buffer)
2608 rmail-header-style)))
2609 (rmail-show-message rmail-current-message)))
2610
2611 (defun rmail-end-of-message ()
2612 "Show bottom of current message."
2613 (interactive)
2614 (let ((rmail-show-message-hook '((lambda ()
2615 (goto-char (point-max))
2616 (recenter (1- (window-height))))))
2617 (rmail-header-style (with-current-buffer (if (rmail-buffers-swapped-p)
2618 rmail-view-buffer
2619 rmail-buffer)
2620 rmail-header-style)))
2621 (rmail-show-message rmail-current-message)))
2622
2623 (defun rmail-unknown-mail-followup-to ()
2624 "Handle a \"Mail-Followup-To\" header field with an unknown mailing list.
2625 Ask the user whether to add that list name to `mail-mailing-lists'."
2626 ;; FIXME s-r not needed? Use rmail-get-header?
2627 ;; We have not narrowed to the headers at ths point?
2628 (save-restriction
2629 (let ((mail-followup-to (mail-fetch-field "mail-followup-to" nil t)))
2630 (when mail-followup-to
2631 (let ((addresses
2632 (split-string
2633 (mail-strip-quoted-names mail-followup-to)
2634 ",[[:space:]]+" t)))
2635 (dolist (addr addresses)
2636 (when (and (not (member addr mail-mailing-lists))
2637 (not
2638 ;; taken from rmailsum.el
2639 (string-match
2640 (or rmail-user-mail-address-regexp
2641 (concat "^\\("
2642 (regexp-quote (user-login-name))
2643 "\\($\\|@\\)\\|"
2644 (regexp-quote
2645 (or user-mail-address
2646 (concat (user-login-name) "@"
2647 (or mail-host-address
2648 (system-name)))))
2649 "\\>\\)"))
2650 addr))
2651 (y-or-n-p
2652 (format "Add `%s' to `mail-mailing-lists'? "
2653 addr)))
2654 (customize-save-variable 'mail-mailing-lists
2655 (cons addr mail-mailing-lists)))))))))
2656
2657 (defun rmail-widen ()
2658 "Display the entire mailbox file."
2659 (interactive)
2660 (rmail-swap-buffers-maybe)
2661 (widen))
2662 \f
2663 (defun rmail-no-mail-p ()
2664 "Return nil if there is mail, else \"No mail.\"."
2665 (if (zerop rmail-total-messages)
2666 (save-excursion
2667 ;; Eg we deleted all the messages, so remove the old N/M mark.
2668 (with-current-buffer rmail-buffer (setq mode-line-process nil))
2669 (with-current-buffer rmail-view-buffer
2670 (erase-buffer)
2671 "No mail."))))
2672
2673 (defun rmail-show-message (&optional n no-summary)
2674 "Show message number N (prefix argument), counting from start of file.
2675 If summary buffer is currently displayed, update current message there also.
2676 N defaults to the current message."
2677 (interactive "p")
2678 (or (eq major-mode 'rmail-mode)
2679 (switch-to-buffer rmail-buffer))
2680 ;; FIXME: Why do we swap the raw data back in?
2681 (rmail-swap-buffers-maybe)
2682 (rmail-maybe-set-message-counters)
2683 (widen)
2684 (let ((blurb (rmail-show-message-1 n)))
2685 (or (zerop rmail-total-messages)
2686 (progn
2687 (when mail-mailing-lists
2688 (rmail-unknown-mail-followup-to))
2689 (if transient-mark-mode (deactivate-mark))
2690 ;; If there is a summary buffer, try to move to this message
2691 ;; in that buffer. But don't complain if this message is
2692 ;; not mentioned in the summary. Don't do this at all if we
2693 ;; were called on behalf of cursor motion in the summary
2694 ;; buffer.
2695 (and (rmail-summary-exists) (not no-summary)
2696 (let ((curr-msg rmail-current-message))
2697 (rmail-select-summary
2698 (rmail-summary-goto-msg curr-msg t t))))
2699 (with-current-buffer rmail-buffer
2700 (rmail-auto-file))))
2701 (if blurb
2702 (message blurb))))
2703
2704 (defun rmail-is-text-p ()
2705 "Return t if the region contains a text message, nil otherwise."
2706 (save-excursion
2707 (let ((text-regexp "\\(text\\|message\\)/")
2708 (content-type-header (mail-fetch-field "content-type")))
2709 ;; The message is text if either there is no content type header
2710 ;; (a default of "text/plain; charset=US-ASCII" is assumed) or
2711 ;; the base content type is either text or message.
2712 (or (not content-type-header)
2713 (string-match text-regexp content-type-header)))))
2714
2715 (defcustom rmail-show-message-verbose-min 200000
2716 "Message size at which to show progress messages for displaying it."
2717 :type 'integer
2718 :group 'rmail
2719 :version "23.1")
2720
2721 ;; FIXME?
2722 ;; rmail-show-mime-function does not unquote >From lines. Should it?
2723 (defcustom rmail-mbox-format 'mboxrd
2724 "The mbox format that your system uses.
2725 There is no way to determine this, so you should set the appropriate value.
2726 The formats quote lines containing \"From \" differently.
2727 The choices are:
2728 `mboxo' : lines that start with \"From \" quoted as \">From \"
2729 `mboxrd': lines that start with \">*From \" quoted with another \">\"
2730 The `mboxo' format is ambiguous, in that one cannot know whether
2731 a line starting with \">From \" originally had a \">\" or not.
2732
2733 It is not critical to set this to the correct value; it only affects
2734 how Rmail displays lines starting with \">*From \" in non-MIME messages.
2735
2736 See also `unrmail-mbox-format'."
2737 :type '(choice (const mboxrd)
2738 (const mboxro))
2739 :version "24.4"
2740 :group 'rmail-files)
2741
2742 (defun rmail-show-message-1 (&optional msg)
2743 "Show message MSG (default: current message) using `rmail-view-buffer'.
2744 Return text to display in the minibuffer if MSG is out of
2745 range (displaying a reasonable choice as well), nil otherwise.
2746 The current mail message becomes the message displayed."
2747 (let ((mbox-buf rmail-buffer)
2748 (view-buf rmail-view-buffer)
2749 blurb beg end body-start coding-system character-coding
2750 is-text-message header-style)
2751 (if (not msg)
2752 (setq msg rmail-current-message))
2753 (unless (setq blurb (rmail-no-mail-p))
2754 (cond ((<= msg 0)
2755 (setq msg 1
2756 rmail-current-message 1
2757 blurb "No previous message"))
2758 ((> msg rmail-total-messages)
2759 (setq msg rmail-total-messages
2760 rmail-current-message rmail-total-messages
2761 blurb "No following message"))
2762 (t (setq rmail-current-message msg)))
2763 (with-current-buffer rmail-buffer
2764 (setq header-style rmail-header-style)
2765 ;; Mark the message as seen, but preserve buffer modified flag.
2766 (let ((modiff (buffer-modified-p)))
2767 (rmail-set-attribute rmail-unseen-attr-index nil)
2768 (unless modiff
2769 (restore-buffer-modified-p modiff)))
2770 ;; bracket the message in the mail
2771 ;; buffer and determine the coding system the transfer encoding.
2772 (rmail-swap-buffers-maybe)
2773 (setq beg (rmail-msgbeg msg)
2774 end (rmail-msgend msg))
2775 (when (> (- end beg) rmail-show-message-verbose-min)
2776 (message "Showing message %d" msg))
2777 (narrow-to-region beg end)
2778 (goto-char beg)
2779 (with-current-buffer rmail-view-buffer
2780 ;; We give the view buffer a buffer-local value of
2781 ;; rmail-header-style based on the binding in effect when
2782 ;; this function is called; `rmail-toggle-headers' can
2783 ;; inspect this value to determine how to toggle.
2784 (set (make-local-variable 'rmail-header-style) header-style))
2785 (if (and rmail-enable-mime
2786 rmail-show-mime-function
2787 (re-search-forward "mime-version: 1.0" nil t))
2788 (let ((rmail-buffer mbox-buf)
2789 (rmail-view-buffer view-buf))
2790 (set (make-local-variable 'rmail-mime-decoded) t)
2791 (funcall rmail-show-mime-function))
2792 (setq body-start (search-forward "\n\n" nil t))
2793 (narrow-to-region beg (point))
2794 (goto-char beg)
2795 (save-excursion
2796 (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t)
2797 (setq coding-system (intern (match-string 1)))
2798 (setq coding-system (rmail-get-coding-system))))
2799 (setq character-coding (mail-fetch-field "content-transfer-encoding")
2800 is-text-message (rmail-is-text-p))
2801 (if character-coding
2802 (setq character-coding (downcase character-coding)))
2803 (narrow-to-region beg end)
2804 ;; Decode the message body into an empty view buffer using a
2805 ;; unibyte temporary buffer where the character decoding takes
2806 ;; place.
2807 (with-current-buffer rmail-view-buffer
2808 (erase-buffer))
2809 (if (null character-coding)
2810 ;; Do it directly since that is fast.
2811 (rmail-decode-region body-start end coding-system view-buf)
2812 ;; Can this be done directly, skipping the temp buffer?
2813 (with-temp-buffer
2814 (set-buffer-multibyte nil)
2815 (insert-buffer-substring mbox-buf body-start end)
2816 (cond
2817 ((string= character-coding "quoted-printable")
2818 ;; See bug#5441.
2819 (or (mail-unquote-printable-region (point-min) (point-max)
2820 nil t 'unibyte)
2821 (message "Malformed MIME quoted-printable message")))
2822 ((and (string= character-coding "base64") is-text-message)
2823 (condition-case err
2824 (base64-decode-region (point-min) (point-max))
2825 (error (message "%s" (cdr err)))))
2826 ((eq character-coding 'uuencode)
2827 (error "uuencoded messages are not supported yet"))
2828 (t))
2829 (rmail-decode-region (point-min) (point-max)
2830 coding-system view-buf)))
2831 (with-current-buffer rmail-view-buffer
2832 ;; Prepare the separator (blank line) before the body.
2833 (goto-char (point-min))
2834 (insert "\n")
2835 ;; Unquote quoted From lines.
2836 (let ((fromline (if (eq 'mboxrd rmail-mbox-format)
2837 "^>+From "
2838 "^>From "))
2839 case-fold-search)
2840 (while (re-search-forward fromline nil t)
2841 (beginning-of-line)
2842 (delete-char 1)
2843 (forward-line)))
2844 (goto-char (point-min)))
2845 ;; Copy the headers to the front of the message view buffer.
2846 (rmail-copy-headers beg end)
2847 ;; Decode any RFC2047 encoded message headers.
2848 (if rmail-enable-mime
2849 (with-current-buffer rmail-view-buffer
2850 (rfc2047-decode-region
2851 (point-min)
2852 (progn
2853 (search-forward "\n\n" nil 'move)
2854 (point))))))
2855 ;; highlight the message, activate any URL like text and add
2856 ;; special highlighting for and quoted material.
2857 (with-current-buffer rmail-view-buffer
2858 (goto-char (point-min))
2859 (rmail-highlight-headers)
2860 ;(rmail-activate-urls)
2861 ;(rmail-process-quoted-material)
2862 )
2863 ;; Update the mode-line with message status information and swap
2864 ;; the view buffer/mail buffer contents.
2865 (rmail-display-labels)
2866 (rmail-swap-buffers)
2867 (setq rmail-buffer-swapped t)
2868 (run-hooks 'rmail-show-message-hook)
2869 (when (> (- end beg) rmail-show-message-verbose-min)
2870 (message "Showing message %d...done" msg))))
2871 blurb))
2872
2873 (defun rmail-copy-headers (beg end &optional ignored-headers)
2874 "Copy displayed header fields to the message viewer buffer.
2875 BEG and END marks the start and end positions of the message in
2876 the mbox buffer. If the optional argument IGNORED-HEADERS is
2877 non-nil, ignore all header fields whose names match that regexp.
2878 Otherwise, if `rmail-displayed-headers' is non-nil, copy only
2879 those header fields whose names match that regexp. Otherwise,
2880 copy all header fields whose names do not match
2881 `rmail-ignored-headers' (unless they also match
2882 `rmail-nonignored-headers'). Moves point in the message viewer
2883 buffer to the end of the headers."
2884 (let ((header-start-regexp "\n[^ \t]")
2885 lim)
2886 (with-current-buffer rmail-buffer
2887 (when (search-forward "\n\n" nil t)
2888 (forward-char -1)
2889 (save-restriction
2890 ;; Put point right after the From header line.
2891 (narrow-to-region beg (point))
2892 (goto-char (point-min))
2893 (unless (re-search-forward header-start-regexp nil t)
2894 (rmail-error-bad-format))
2895 (forward-char -1)
2896 (cond
2897 ;; Handle the case where all headers should be copied.
2898 ((eq rmail-header-style 'full)
2899 (prepend-to-buffer rmail-view-buffer beg (point-max))
2900 ;; rmail-show-message-1 expects this function to leave point
2901 ;; at the end of the headers.
2902
2903 (let ((len (- (point-max) beg)))
2904 (with-current-buffer rmail-view-buffer
2905 (goto-char (1+ len)))))
2906
2907 ;; Handle the case where the headers matching the displayed
2908 ;; headers regexp should be copied.
2909 ((and rmail-displayed-headers (null ignored-headers))
2910 (while (not (eobp))
2911 (save-excursion
2912 (setq lim (if (re-search-forward header-start-regexp nil t)
2913 (1+ (match-beginning 0))
2914 (point-max))))
2915 (when (looking-at rmail-displayed-headers)
2916 (append-to-buffer rmail-view-buffer (point) lim))
2917 (goto-char lim)))
2918 ;; Handle the ignored headers.
2919 ((or ignored-headers (setq ignored-headers rmail-ignored-headers))
2920 (while (and ignored-headers (not (eobp)))
2921 (save-excursion
2922 (setq lim (if (re-search-forward header-start-regexp nil t)
2923 (1+ (match-beginning 0))
2924 (point-max))))
2925 (if (and (looking-at ignored-headers)
2926 (not (looking-at rmail-nonignored-headers)))
2927 (goto-char lim)
2928 (append-to-buffer rmail-view-buffer (point) lim)
2929 (goto-char lim))))
2930 (t (error "No headers selected for display!"))))))))
2931
2932 (defun rmail-redecode-body (coding)
2933 "Decode the body of the current message using coding system CODING.
2934 This is useful with mail messages that have malformed or missing
2935 charset= headers.
2936
2937 This function assumes that the current message is already decoded
2938 and displayed in the RMAIL buffer, but the coding system used to
2939 decode it was incorrect. It then decodes the message again,
2940 using the coding system CODING."
2941 (interactive "zCoding system for re-decoding this message: ")
2942 (when (not rmail-enable-mime)
2943 (with-current-buffer rmail-buffer
2944 (rmail-swap-buffers-maybe)
2945 (save-restriction
2946 (widen)
2947 (let ((msgbeg (rmail-msgbeg rmail-current-message))
2948 (msgend (rmail-msgend rmail-current-message))
2949 (buffer-read-only nil)
2950 body-start x-coding-header old-coding)
2951 (narrow-to-region msgbeg msgend)
2952 (goto-char (point-min))
2953 (unless (setq body-start (search-forward "\n\n" (point-max) 1))
2954 (error "No message body"))
2955
2956 (save-restriction
2957 ;; Narrow to headers
2958 (narrow-to-region (point-min) body-start)
2959 (setq x-coding-header (goto-char (point-min)))
2960 (if (not (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t))
2961 (setq old-coding (rmail-get-coding-system))
2962 (setq old-coding (intern (match-string 1)))
2963 (setq x-coding-header (point)))
2964 (check-coding-system old-coding)
2965 ;; Make sure the new coding system uses the same EOL
2966 ;; conversion, to prevent ^M characters from popping up
2967 ;; all over the place.
2968 (let ((eol-type (coding-system-eol-type old-coding)))
2969 (if (numberp eol-type)
2970 (setq coding
2971 (coding-system-change-eol-conversion coding eol-type))))
2972 (when (not (coding-system-equal
2973 (coding-system-base old-coding)
2974 (coding-system-base coding)))
2975 ;; Rewrite the coding-system header.
2976 (goto-char x-coding-header)
2977 (if (> (point) (point-min))
2978 (delete-region (line-beginning-position) (point))
2979 (forward-line)
2980 (insert "\n")
2981 (forward-line -1))
2982 (insert "X-Coding-System: "
2983 (symbol-name coding))))
2984 (rmail-show-message))))))
2985
2986 (defun rmail-highlight-headers ()
2987 "Highlight the headers specified by `rmail-highlighted-headers'.
2988 Uses the face specified by `rmail-highlight-face'."
2989 (if rmail-highlighted-headers
2990 (save-excursion
2991 (search-forward "\n\n" nil 'move)
2992 (save-restriction
2993 (narrow-to-region (point-min) (point))
2994 (let ((case-fold-search t)
2995 (inhibit-read-only t)
2996 ;; When rmail-highlight-face is removed, just
2997 ;; use 'rmail-highlight here.
2998 (face (or rmail-highlight-face
2999 (if (face-differs-from-default-p 'bold)
3000 'bold 'highlight)))
3001 ;; List of overlays to reuse.
3002 (overlays rmail-overlay-list))
3003 (goto-char (point-min))
3004 (while (re-search-forward rmail-highlighted-headers nil t)
3005 (skip-chars-forward " \t")
3006 (let ((beg (point))
3007 overlay)
3008 (while (progn (forward-line 1)
3009 (looking-at "[ \t]")))
3010 ;; Back up over newline, then trailing spaces or tabs
3011 (forward-char -1)
3012 (while (member (preceding-char) '(? ?\t))
3013 (forward-char -1))
3014 (if overlays
3015 ;; Reuse an overlay we already have.
3016 (progn
3017 (setq overlay (car overlays)
3018 overlays (cdr overlays))
3019 (overlay-put overlay 'face face)
3020 (move-overlay overlay beg (point)))
3021 ;; Make a new overlay and add it to
3022 ;; rmail-overlay-list.
3023 (setq overlay (make-overlay beg (point)))
3024 (overlay-put overlay 'face face)
3025 (setq rmail-overlay-list
3026 (cons overlay rmail-overlay-list))))))))))
3027
3028 (defun rmail-auto-file ()
3029 "Automatically move a message into another sfolder based on criteria.
3030 This moves messages according to `rmail-automatic-folder-directives'.
3031 It only does something in the folder that `rmail-file-name' specifies.
3032 The function `rmail-show-message' calls this whenever it shows a message.
3033 This leaves a message alone if it already has the `filed' attribute."
3034 (if (or (zerop rmail-total-messages)
3035 (rmail-message-attr-p rmail-current-message "...F")
3036 (not (string= (buffer-file-name)
3037 (expand-file-name rmail-file-name))))
3038 ;; Do nothing if the message has already been filed or if there
3039 ;; are no messages.
3040 nil
3041 ;; Find out some basics (common fields)
3042 (let ((from (mail-fetch-field "from"))
3043 (subj (mail-fetch-field "subject"))
3044 (to (concat (mail-fetch-field "to") "," (mail-fetch-field "cc")))
3045 (d rmail-automatic-folder-directives)
3046 (directive-loop nil)
3047 (folder nil))
3048 (while d
3049 (setq folder (car (car d))
3050 directive-loop (cdr (car d)))
3051 (while (and (car directive-loop)
3052 (let ((f (cond
3053 ((string= (downcase (car directive-loop)) "from")
3054 from)
3055 ((string= (downcase (car directive-loop)) "to")
3056 to)
3057 ((string= (downcase (car directive-loop))
3058 "subject") subj)
3059 (t (mail-fetch-field (car directive-loop))))))
3060 ;; FIXME - shouldn't this ignore case?
3061 (and f (string-match (car (cdr directive-loop)) f))))
3062 (setq directive-loop (cdr (cdr directive-loop))))
3063 ;; If there are no directives left, then it was a complete match.
3064 (if (null directive-loop)
3065 (if (null folder)
3066 (rmail-delete-forward)
3067 (if (string= "/dev/null" folder)
3068 (rmail-delete-message)
3069 (rmail-output folder 1)
3070 (setq d nil))))
3071 (setq d (cdr d))))))
3072 \f
3073 ;; Simple message motion commands.
3074
3075 (defun rmail-next-message (n)
3076 "Show following message whether deleted or not.
3077 With prefix arg N, moves forward N messages, or backward if N is negative."
3078 (interactive "p")
3079 (set-buffer rmail-buffer)
3080 (rmail-maybe-set-message-counters)
3081 (rmail-show-message (+ rmail-current-message n)))
3082
3083 (defun rmail-previous-message (n)
3084 "Show previous message whether deleted or not.
3085 With prefix arg N, moves backward N messages, or forward if N is negative."
3086 (interactive "p")
3087 (rmail-next-message (- n)))
3088
3089 (defun rmail-next-undeleted-message (n)
3090 "Show following non-deleted message.
3091 With prefix arg N, moves forward N non-deleted messages,
3092 or backward if N is negative.
3093
3094 Returns t if a new message is being shown, nil otherwise."
3095 (interactive "p")
3096 (set-buffer rmail-buffer)
3097 (rmail-maybe-set-message-counters)
3098 (let ((lastwin rmail-current-message)
3099 (current rmail-current-message))
3100 (while (and (> n 0) (< current rmail-total-messages))
3101 (setq current (1+ current))
3102 (if (not (rmail-message-deleted-p current))
3103 (setq lastwin current n (1- n))))
3104 (while (and (< n 0) (> current 1))
3105 (setq current (1- current))
3106 (if (not (rmail-message-deleted-p current))
3107 (setq lastwin current n (1+ n))))
3108 (if (/= lastwin rmail-current-message)
3109 (progn (rmail-show-message lastwin)
3110 t)
3111 (if (< n 0)
3112 (message "No previous nondeleted message"))
3113 (if (> n 0)
3114 (message "No following nondeleted message"))
3115 nil)))
3116
3117 (defun rmail-previous-undeleted-message (n)
3118 "Show previous non-deleted message.
3119 With prefix argument N, moves backward N non-deleted messages,
3120 or forward if N is negative."
3121 (interactive "p")
3122 (rmail-next-undeleted-message (- n)))
3123
3124 (defun rmail-first-message ()
3125 "Show first message in file."
3126 (interactive)
3127 (rmail-maybe-set-message-counters)
3128 (rmail-show-message 1))
3129
3130 (defun rmail-last-message ()
3131 "Show last message in file."
3132 (interactive)
3133 (rmail-maybe-set-message-counters)
3134 (rmail-show-message rmail-total-messages))
3135
3136 (defun rmail-next-error-move (msg-pos bad-marker)
3137 "Move to an error locus (probably grep hit) in an Rmail buffer.
3138 MSG-POS is a marker pointing at the error message in the grep buffer.
3139 BAD-MARKER is a marker that ought to point at where to move to,
3140 but probably is garbage."
3141
3142 (let* ((message-loc (compilation--message->loc
3143 (get-text-property msg-pos 'compilation-message
3144 (marker-buffer msg-pos))))
3145 (column (car message-loc))
3146 (linenum (cadr message-loc))
3147 line-text
3148 pos
3149 msgnum msgbeg msgend
3150 header-field
3151 line-number-within)
3152
3153 ;; Look at the whole Rmail file.
3154 (rmail-swap-buffers-maybe)
3155
3156 (save-restriction
3157 (widen)
3158 (save-excursion
3159 ;; Find the line that the error message points at.
3160 (goto-char (point-min))
3161 (forward-line (1- linenum))
3162 (setq pos (point))
3163
3164 ;; Find the text at the start of the line,
3165 ;; before the first = sign.
3166 ;; This text has a good chance of being also in the
3167 ;; decoded message.
3168 (save-excursion
3169 (skip-chars-forward "^=\n")
3170 (setq line-text (buffer-substring pos (point))))
3171
3172 ;; Find which message this position is in,
3173 ;; and the limits of that message.
3174 (setq msgnum (rmail-what-message pos))
3175 (setq msgbeg (rmail-msgbeg msgnum))
3176 (setq msgend (rmail-msgend msgnum))
3177
3178 ;; Find which header this locus is in,
3179 ;; or if it's in the message body,
3180 ;; and the line-based position within that.
3181 (goto-char msgbeg)
3182 (let ((header-end msgend))
3183 (if (search-forward "\n\n" nil t)
3184 (setq header-end (point)))
3185 (if (>= pos header-end)
3186 (setq line-number-within
3187 (count-lines header-end pos))
3188 (goto-char pos)
3189 (unless (looking-at "^[^ \t]")
3190 (re-search-backward "^[^ \t]"))
3191 (looking-at "[^:\n]*[:\n]")
3192 (setq header-field (match-string 0)
3193 line-number-within (count-lines (point) pos))))))
3194
3195 ;; Display the right message.
3196 (rmail-show-message msgnum)
3197
3198 ;; Move to the right position within the displayed message.
3199 ;; Or at least try. The decoded message's lines may not
3200 ;; correspond to the lines in the inbox file.
3201 (goto-char (point-min))
3202 (if header-field
3203 (progn
3204 (re-search-forward (concat "^" (regexp-quote header-field)) nil t)
3205 (forward-line line-number-within))
3206 (search-forward "\n\n" nil t)
3207 (if (re-search-forward (concat "^" (regexp-quote line-text)) nil t)
3208 (goto-char (match-beginning 0))))
3209 (if (eobp)
3210 ;; If the decoded message doesn't have enough lines,
3211 ;; go to the beginning rather than the end.
3212 (goto-char (point-min))
3213 ;; Otherwise, go to the right column.
3214 (if column
3215 (forward-char column)))))
3216
3217 (defun rmail-what-message (&optional pos)
3218 "Return message number POS (or point) is in."
3219 (let* ((high rmail-total-messages)
3220 (mid (/ high 2))
3221 (low 1)
3222 (where (or pos
3223 (with-current-buffer (if (rmail-buffers-swapped-p)
3224 rmail-view-buffer
3225 (current-buffer))
3226 (point)))))
3227 (while (> (- high low) 1)
3228 (if (>= where (rmail-msgbeg mid))
3229 (setq low mid)
3230 (setq high mid))
3231 (setq mid (+ low (/ (- high low) 2))))
3232 (if (>= where (rmail-msgbeg high)) high low)))
3233 \f
3234 ;; Searching in Rmail file.
3235
3236 (defun rmail-search-message (msg regexp)
3237 "Return non-nil, if for message number MSG, regexp REGEXP matches."
3238 ;; This is adequate because its only caller, rmail-search,
3239 ;; unswaps the buffers.
3240 (goto-char (rmail-msgbeg msg))
3241 (if (and rmail-enable-mime
3242 rmail-search-mime-message-function)
3243 (funcall rmail-search-mime-message-function msg regexp)
3244 (re-search-forward regexp (rmail-msgend msg) t)))
3245
3246 (defvar rmail-search-last-regexp nil)
3247 (defun rmail-search (regexp &optional n)
3248 "Show message containing next match for REGEXP (but not the current msg).
3249 Prefix argument gives repeat count; negative argument means search
3250 backwards (through earlier messages).
3251 Interactively, empty argument means use same regexp used last time."
3252 (interactive
3253 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
3254 (prompt
3255 (concat (if reversep "Reverse " "") "Rmail search (regexp"))
3256 regexp)
3257 (setq prompt
3258 (concat prompt
3259 (if rmail-search-last-regexp
3260 (concat ", default "
3261 rmail-search-last-regexp "): ")
3262 "): ")))
3263 (setq regexp (read-string prompt))
3264 (cond ((not (equal regexp ""))
3265 (setq rmail-search-last-regexp regexp))
3266 ((not rmail-search-last-regexp)
3267 (error "No previous Rmail search string")))
3268 (list rmail-search-last-regexp
3269 (prefix-numeric-value current-prefix-arg))))
3270 (or n (setq n 1))
3271 (message "%sRmail search for %s..."
3272 (if (< n 0) "Reverse " "")
3273 regexp)
3274 (set-buffer rmail-buffer)
3275 (let ((orig-message rmail-current-message)
3276 (msg rmail-current-message)
3277 (reversep (< n 0))
3278 (opoint (if (rmail-buffers-swapped-p) (point)))
3279 found)
3280 (rmail-swap-buffers-maybe)
3281 (rmail-maybe-set-message-counters)
3282 (widen)
3283 (unwind-protect
3284 (while (/= n 0)
3285 ;; Check messages one by one, advancing message number up or
3286 ;; down but searching forward through each message.
3287 (if reversep
3288 (while (and (null found) (> msg 1))
3289 (setq msg (1- msg)
3290 found (rmail-search-message msg regexp)))
3291 (while (and (null found) (< msg rmail-total-messages))
3292 (setq msg (1+ msg)
3293 found (rmail-search-message msg regexp))))
3294 (setq n (+ n (if reversep 1 -1))))
3295 (if found
3296 (progn
3297 (rmail-show-message msg)
3298 ;; Search forward (if this is a normal search) or backward
3299 ;; (if this is a reverse search) through this message to
3300 ;; position point. This search may fail because REGEXP
3301 ;; was found in the hidden portion of this message. In
3302 ;; that case, move point to the beginning of visible
3303 ;; portion.
3304 (if reversep
3305 (progn
3306 (goto-char (point-max))
3307 (re-search-backward regexp nil 'move))
3308 (goto-char (point-min))
3309 (re-search-forward regexp nil t))
3310 (message "%sRmail search for %s...done"
3311 (if reversep "Reverse " "")
3312 regexp))
3313 (rmail-show-message orig-message)
3314 (if opoint (goto-char opoint))
3315 (ding)
3316 (message "Search failed: %s" regexp)))))
3317
3318 (defun rmail-search-backwards (regexp &optional n)
3319 "Show message containing previous match for REGEXP.
3320 Prefix argument gives repeat count; negative argument means search
3321 forward (through later messages).
3322 Interactively, empty argument means use same regexp used last time."
3323 (interactive
3324 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
3325 (prompt
3326 (concat (if reversep "Reverse " "") "Rmail search (regexp"))
3327 regexp)
3328 (setq prompt
3329 (concat prompt
3330 (if rmail-search-last-regexp
3331 (concat ", default "
3332 rmail-search-last-regexp "): ")
3333 "): ")))
3334 (setq regexp (read-string prompt))
3335 (cond ((not (equal regexp ""))
3336 (setq rmail-search-last-regexp regexp))
3337 ((not rmail-search-last-regexp)
3338 (error "No previous Rmail search string")))
3339 (list rmail-search-last-regexp
3340 (prefix-numeric-value current-prefix-arg))))
3341 (rmail-search regexp (- (or n 1))))
3342 \f
3343 ;; Scan for attributes, and compare subjects.
3344
3345 (defun rmail-first-unseen-message ()
3346 "Return message number of first message which has `unseen' attribute."
3347 (rmail-maybe-set-message-counters)
3348 (let ((current 1)
3349 found)
3350 (save-restriction
3351 (widen)
3352 (while (and (not found) (<= current rmail-total-messages))
3353 (if (rmail-message-attr-p current "......U")
3354 (setq found current))
3355 (setq current (1+ current))))
3356 found))
3357
3358 (defun rmail-simplified-subject (&optional msgnum)
3359 "Return the simplified subject of message MSGNUM (or current message).
3360 Simplifying the subject means stripping leading and trailing whitespace,
3361 and typical reply prefixes such as Re:."
3362 (let ((subject (or (rmail-get-header "Subject" msgnum) "")))
3363 (setq subject (rfc2047-decode-string subject))
3364 (if (string-match "\\`[ \t]+" subject)
3365 (setq subject (substring subject (match-end 0))))
3366 (if (string-match rmail-reply-regexp subject)
3367 (setq subject (substring subject (match-end 0))))
3368 (if (string-match "[ \t]+\\'" subject)
3369 (setq subject (substring subject 0 (match-beginning 0))))
3370 ;; If Subject is long, mailers will break it into several lines at
3371 ;; arbitrary places, so normalize whitespace by replacing every
3372 ;; run of whitespace characters with a single space.
3373 (setq subject (replace-regexp-in-string "[ \t\n]+" " " subject))
3374 subject))
3375
3376 (defun rmail-simplified-subject-regexp ()
3377 "Return a regular expression matching the current simplified subject.
3378 The idea is to match it against simplified subjects of other messages."
3379 (let ((subject (rmail-simplified-subject)))
3380 (setq subject (regexp-quote subject))
3381 ;; Hide commas so it will work ok if parsed as a comma-separated list
3382 ;; of regexps.
3383 (setq subject
3384 (replace-regexp-in-string "," "\054" subject t t))
3385 (concat "\\`" subject "\\'")))
3386
3387 (defun rmail-next-same-subject (n)
3388 "Go to the next mail message having the same subject header.
3389 With prefix argument N, do this N times.
3390 If N is negative, go backwards instead."
3391 (interactive "p")
3392 (let ((subject (rmail-simplified-subject))
3393 (forward (> n 0))
3394 (i rmail-current-message)
3395 found)
3396 (while (and (/= n 0)
3397 (if forward
3398 (< i rmail-total-messages)
3399 (> i 1)))
3400 (let (done)
3401 (while (and (not done)
3402 (if forward
3403 (< i rmail-total-messages)
3404 (> i 1)))
3405 (setq i (if forward (1+ i) (1- i)))
3406 (setq done (string-equal subject (rmail-simplified-subject i))))
3407 (if done (setq found i)))
3408 (setq n (if forward (1- n) (1+ n))))
3409 (if found
3410 (rmail-show-message found)
3411 (error "No %s message with same subject"
3412 (if forward "following" "previous")))))
3413
3414 (defun rmail-previous-same-subject (n)
3415 "Go to the previous mail message having the same subject header.
3416 With prefix argument N, do this N times.
3417 If N is negative, go forwards instead."
3418 (interactive "p")
3419 (rmail-next-same-subject (- n)))
3420 \f
3421 ;;;; *** Rmail Message Deletion Commands ***
3422
3423 (defun rmail-message-deleted-p (n)
3424 "Return non-nil if message number N is deleted (in `rmail-deleted-vector')."
3425 (= (aref rmail-deleted-vector n) ?D))
3426
3427 (defun rmail-set-message-deleted-p (n state)
3428 "Set the deleted state of message number N (in `rmail-deleted-vector').
3429 STATE non-nil means mark as deleted."
3430 (aset rmail-deleted-vector n (if state ?D ?\s)))
3431
3432 (defun rmail-delete-message ()
3433 "Delete this message and stay on it."
3434 (interactive)
3435 (rmail-set-attribute rmail-deleted-attr-index t)
3436 (run-hooks 'rmail-delete-message-hook))
3437
3438 (defun rmail-undelete-previous-message ()
3439 "Back up to deleted message, select it, and undelete it."
3440 (interactive)
3441 (set-buffer rmail-buffer)
3442 (let ((msg rmail-current-message))
3443 (while (and (> msg 0)
3444 (not (rmail-message-deleted-p msg)))
3445 (setq msg (1- msg)))
3446 (if (= msg 0)
3447 (error "No previous deleted message")
3448 (if (/= msg rmail-current-message)
3449 (rmail-show-message msg))
3450 (rmail-set-attribute rmail-deleted-attr-index nil)
3451 (if (rmail-summary-exists)
3452 (with-current-buffer rmail-summary-buffer
3453 (rmail-summary-mark-undeleted msg)))
3454 (rmail-maybe-display-summary))))
3455
3456 (defun rmail-delete-forward (&optional backward)
3457 "Delete this message and move to next nondeleted one.
3458 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
3459 With prefix argument, delete and move backward.
3460
3461 Returns t if a new message is displayed after the delete, or nil otherwise."
3462 (interactive "P")
3463 (rmail-set-attribute rmail-deleted-attr-index t)
3464 (run-hooks 'rmail-delete-message-hook)
3465 (let ((del-msg rmail-current-message))
3466 (if (rmail-summary-exists)
3467 (rmail-select-summary
3468 (rmail-summary-mark-deleted del-msg)))
3469 (prog1 (rmail-next-undeleted-message (if backward -1 1))
3470 (rmail-maybe-display-summary))))
3471
3472 (defun rmail-delete-backward ()
3473 "Delete this message and move to previous nondeleted one.
3474 Deleted messages stay in the file until the \\[rmail-expunge] command is given."
3475 (interactive)
3476 (rmail-delete-forward t))
3477 \f
3478 ;; Expunging.
3479
3480 ;; Compute the message number a given message would have after expunging.
3481 ;; The present number of the message is OLDNUM.
3482 ;; DELETEDVEC should be rmail-deleted-vector.
3483 ;; The value is nil for a message that would be deleted.
3484 (defun rmail-msg-number-after-expunge (deletedvec oldnum)
3485 (if (or (null oldnum) (= (aref deletedvec oldnum) ?D))
3486 nil
3487 (let ((i 0)
3488 (newnum 0))
3489 (while (< i oldnum)
3490 (if (/= (aref deletedvec i) ?D)
3491 (setq newnum (1+ newnum)))
3492 (setq i (1+ i)))
3493 newnum)))
3494
3495 (defun rmail-expunge-confirmed ()
3496 "Return t if expunge is needed and desirable.
3497 If `rmail-confirm-expunge' is non-nil, ask user to confirm."
3498 (set-buffer rmail-buffer)
3499 (and (stringp rmail-deleted-vector)
3500 (string-match "D" rmail-deleted-vector)
3501 (if rmail-confirm-expunge
3502 (funcall rmail-confirm-expunge
3503 "Erase deleted messages from Rmail file? ")
3504 t)))
3505
3506 (defun rmail-only-expunge (&optional dont-show)
3507 "Actually erase all deleted messages in the file."
3508 (interactive)
3509 (rmail-swap-buffers-maybe)
3510 (set-buffer rmail-buffer)
3511 (message "Expunging deleted messages...")
3512 ;; Discard all undo records for this buffer.
3513 (or (eq buffer-undo-list t)
3514 (setq buffer-undo-list nil))
3515 (rmail-maybe-set-message-counters)
3516 (let* ((omax (- (buffer-size) (point-max)))
3517 (omin (- (buffer-size) (point-min)))
3518 (opoint (if (and (> rmail-current-message 0)
3519 (rmail-message-deleted-p rmail-current-message))
3520 0
3521 (if rmail-enable-mime
3522 (with-current-buffer rmail-view-buffer
3523 (- (point)(point-min)))
3524 (- (point) (point-min)))))
3525 (messages-head (cons (aref rmail-message-vector 0) nil))
3526 (messages-tail messages-head)
3527 ;; Don't make any undo records for the expunging.
3528 (buffer-undo-list t)
3529 (win))
3530 (unwind-protect
3531 (save-excursion
3532 (widen)
3533 (goto-char (point-min))
3534 (let ((counter 0)
3535 (number 1)
3536 new-summary
3537 (new-msgref (list (list 0)))
3538 (buffer-read-only nil)
3539 (total rmail-total-messages)
3540 (new-message-number rmail-current-message)
3541 (messages rmail-message-vector)
3542 (deleted rmail-deleted-vector)
3543 (summary rmail-summary-vector))
3544 (setq rmail-total-messages nil
3545 rmail-current-message nil
3546 rmail-message-vector nil
3547 rmail-deleted-vector nil
3548 rmail-summary-vector nil)
3549
3550 (while (<= number total)
3551 (if (= (aref deleted number) ?D)
3552 (progn
3553 (delete-region (aref messages number)
3554 (aref messages (1+ number)))
3555 (move-marker (aref messages number) nil)
3556 (if (> new-message-number counter)
3557 (setq new-message-number (1- new-message-number))))
3558 (setq counter (1+ counter))
3559 (setq messages-tail
3560 (setcdr messages-tail
3561 (cons (aref messages number) nil)))
3562 (setq new-summary
3563 (cons (if (= counter number) (aref summary (1- number)))
3564 new-summary))
3565 (setq new-msgref
3566 (cons (aref rmail-msgref-vector number)
3567 new-msgref))
3568 (setcar (car new-msgref) counter))
3569 (if (zerop (% (setq number (1+ number)) 20))
3570 (message "Expunging deleted messages...%d" number)))
3571 (setq messages-tail
3572 (setcdr messages-tail
3573 (cons (aref messages number) nil)))
3574 (setq rmail-current-message new-message-number
3575 rmail-total-messages counter
3576 rmail-message-vector (apply 'vector messages-head)
3577 rmail-deleted-vector (make-string (1+ counter) ?\s)
3578 rmail-summary-vector (vconcat (nreverse new-summary))
3579 rmail-msgref-vector (apply 'vector (nreverse new-msgref))
3580 win t)))
3581 (message "Expunging deleted messages...done")
3582 (if (not win)
3583 (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
3584 (if (not dont-show)
3585 (rmail-show-message (min rmail-current-message rmail-total-messages)))
3586 (if rmail-enable-mime
3587 (goto-char (+ (point-min) opoint))
3588 (goto-char (+ (point) opoint))))))
3589
3590 ;; The DONT-SHOW argument is new in 23. Does not seem very important.
3591 (defun rmail-expunge (&optional dont-show)
3592 "Erase deleted messages from Rmail file and summary buffer.
3593 This always shows a message (so as not to leave the Rmail buffer
3594 unswapped), and always updates any summary (so that it remains
3595 consistent with the Rmail buffer). If DONT-SHOW is non-nil, it
3596 does not pop any summary buffer."
3597 (interactive)
3598 (when (rmail-expunge-confirmed)
3599 (rmail-modify-format)
3600 (let ((was-deleted (rmail-message-deleted-p rmail-current-message))
3601 (was-swapped (rmail-buffers-swapped-p)))
3602 (rmail-only-expunge t)
3603 ;; We always update the summary buffer, so that the contents
3604 ;; remain consistent with the rmail buffer.
3605 ;; The only difference is, in the dont-show case, we use a
3606 ;; cut-down version of rmail-select-summary that does not pop
3607 ;; the summary buffer. It's only used by rmail-quit, which is
3608 ;; just going to bury any summary immediately after. If we made
3609 ;; rmail-quit bury the summary first, dont-show could be removed.
3610 ;; But the expunge might not be confirmed...
3611 (if (rmail-summary-exists)
3612 (if dont-show
3613 (let ((total rmail-total-messages))
3614 (with-current-buffer rmail-summary-buffer
3615 (let ((rmail-total-messages total))
3616 (rmail-update-summary))))
3617 (rmail-select-summary (rmail-update-summary))))
3618 ;; We always show a message, because (rmail-only-expunge t)
3619 ;; leaves the rmail buffer unswapped.
3620 ;; If we expunged the current message, a new one is current now,
3621 ;; so show it. If we weren't showing a message, show it.
3622 (if (or was-deleted (not was-swapped))
3623 (rmail-show-message-1 rmail-current-message)
3624 ;; We can just show the same message that was being shown before.
3625 (rmail-display-labels)
3626 (rmail-swap-buffers)
3627 (setq rmail-buffer-swapped t)))))
3628 \f
3629 ;;;; *** Rmail Mailing Commands ***
3630
3631 (defun rmail-yank-current-message (buffer)
3632 "Yank into the current buffer the current message of Rmail buffer BUFFER.
3633 If BUFFER is swapped with its message viewer buffer, yank out of BUFFER.
3634 If BUFFER is not swapped, yank out of its message viewer buffer."
3635 (with-current-buffer buffer
3636 (unless (rmail-buffers-swapped-p)
3637 (setq buffer rmail-view-buffer)))
3638 (insert-buffer-substring buffer)
3639 ;; If they yank the text of BUFFER, the encoding of BUFFER is a
3640 ;; better default for the reply message than the default value of
3641 ;; buffer-file-coding-system.
3642 (and (coding-system-equal (default-value 'buffer-file-coding-system)
3643 buffer-file-coding-system)
3644 (setq buffer-file-coding-system
3645 (coding-system-change-text-conversion
3646 buffer-file-coding-system (coding-system-base
3647 (with-current-buffer buffer
3648 buffer-file-coding-system))))))
3649
3650 (defun rmail-start-mail (&optional noerase to subject in-reply-to cc
3651 replybuffer sendactions same-window
3652 other-headers)
3653 (let ((switch-function
3654 (cond (same-window nil)
3655 (rmail-mail-new-frame 'switch-to-buffer-other-frame)
3656 (t 'switch-to-buffer-other-window)))
3657 yank-action)
3658 (if replybuffer
3659 ;; The function used here must behave like insert-buffer wrt
3660 ;; point and mark (see doc of sc-cite-original).
3661 (setq yank-action
3662 `(rmail-yank-current-message ,replybuffer)))
3663 (push (cons "cc" cc) other-headers)
3664 (push (cons "in-reply-to" in-reply-to) other-headers)
3665 (setq other-headers
3666 (mapcar #'(lambda (elt)
3667 (cons (car elt) (if (stringp (cdr elt))
3668 (rfc2047-decode-string (cdr elt)))))
3669 other-headers))
3670 (if (stringp to) (setq to (rfc2047-decode-string to)))
3671 (if (stringp in-reply-to)
3672 (setq in-reply-to (rfc2047-decode-string in-reply-to)))
3673 (if (stringp cc) (setq cc (rfc2047-decode-string cc)))
3674 (if (stringp subject) (setq subject (rfc2047-decode-string subject)))
3675 (prog1
3676 (compose-mail to subject other-headers noerase
3677 switch-function yank-action sendactions
3678 (if replybuffer `(rmail-mail-return ,replybuffer)))
3679 (if (eq switch-function 'switch-to-buffer-other-frame)
3680 ;; This is not a standard frame parameter; nothing except
3681 ;; sendmail.el looks at it.
3682 (modify-frame-parameters (selected-frame)
3683 '((mail-dedicated-frame . t)))))))
3684
3685 (defun rmail-mail-return (&optional newbuf)
3686 "Try to return to Rmail from the mail window.
3687 If optional argument NEWBUF is specified, it is the Rmail buffer
3688 to switch to."
3689 (cond
3690 ;; If there is only one visible frame with no special handling,
3691 ;; consider deleting the mail window to return to Rmail.
3692 ((or (null (delq (selected-frame) (visible-frame-list)))
3693 (not (or (window-dedicated-p (frame-selected-window))
3694 (and pop-up-frames (one-window-p))
3695 (cdr (assq 'mail-dedicated-frame
3696 (frame-parameters))))))
3697 (let (rmail-flag summary-buffer)
3698 (unless (one-window-p)
3699 (with-current-buffer
3700 (window-buffer (next-window (selected-window) 'not))
3701 (setq rmail-flag (eq major-mode 'rmail-mode))
3702 (setq summary-buffer
3703 (and (boundp 'mail-bury-selects-summary)
3704 mail-bury-selects-summary
3705 (boundp 'rmail-summary-buffer)
3706 rmail-summary-buffer
3707 (buffer-name rmail-summary-buffer)
3708 (not (get-buffer-window rmail-summary-buffer))
3709 rmail-summary-buffer))))
3710 (cond ((null rmail-flag)
3711 ;; If the Rmail buffer is not in the next window, switch
3712 ;; directly to the Rmail buffer specified by NEWBUF.
3713 (if (buffer-live-p newbuf)
3714 (switch-to-buffer newbuf)))
3715 ;; If the Rmail buffer is in the next window, switch to
3716 ;; the summary buffer if `mail-bury-selects-summary' is
3717 ;; non-nil. Otherwise just delete this window.
3718 (summary-buffer
3719 (switch-to-buffer summary-buffer))
3720 (t
3721 (delete-window)))))
3722 ;; If the frame was probably made for this buffer, the user
3723 ;; probably wants to delete it now.
3724 ((display-multi-frame-p)
3725 (delete-frame (selected-frame)))
3726 ;; The previous frame is where normally they have the Rmail buffer
3727 ;; displayed.
3728 (t (other-frame -1))))
3729
3730 (defun rmail-mail ()
3731 "Send mail in another window.
3732 While composing the message, use \\[mail-yank-original] to yank the
3733 original message into it."
3734 (interactive)
3735 (rmail-start-mail nil nil nil nil nil rmail-buffer))
3736
3737 ;; FIXME should complain if there is nothing to continue.
3738 (defun rmail-continue ()
3739 "Continue composing outgoing message previously being composed."
3740 (interactive)
3741 (rmail-start-mail t))
3742
3743 (defun rmail-reply (just-sender)
3744 "Reply to the current message.
3745 Normally include CC: to all other recipients of original message;
3746 prefix argument means ignore them. While composing the reply,
3747 use \\[mail-yank-original] to yank the original message into it."
3748 (interactive "P")
3749 (if (zerop rmail-current-message)
3750 (error "There is no message to reply to"))
3751 (let (from reply-to cc subject date to message-id references
3752 resent-to resent-cc resent-reply-to
3753 (msgnum rmail-current-message))
3754 (rmail-apply-in-message
3755 rmail-current-message
3756 (lambda ()
3757 (search-forward "\n\n" nil 'move)
3758 (narrow-to-region (point-min) (point))
3759 (setq from (mail-fetch-field "from")
3760 reply-to (or (mail-fetch-field "mail-reply-to" nil t)
3761 (mail-fetch-field "reply-to" nil t)
3762 from)
3763 subject (mail-fetch-field "subject")
3764 date (mail-fetch-field "date")
3765 message-id (mail-fetch-field "message-id")
3766 references (mail-fetch-field "references" nil nil t)
3767 resent-reply-to (mail-fetch-field "resent-reply-to" nil t)
3768 ;; Bug#512. It's inappropriate to reply to these addresses.
3769 ;;; resent-cc (and (not just-sender)
3770 ;;; (mail-fetch-field "resent-cc" nil t))
3771 ;;; resent-to (or (mail-fetch-field "resent-to" nil t) "")
3772 ;;; resent-subject (mail-fetch-field "resent-subject")
3773 ;;; resent-date (mail-fetch-field "resent-date")
3774 ;;; resent-message-id (mail-fetch-field "resent-message-id")
3775 )
3776 (unless just-sender
3777 (if (mail-fetch-field "mail-followup-to" nil t)
3778 ;; If this header field is present, use it instead of the
3779 ;; To and CC fields.
3780 (setq to (mail-fetch-field "mail-followup-to" nil t))
3781 (setq cc (or (mail-fetch-field "cc" nil t) "")
3782 to (or (mail-fetch-field "to" nil t) ""))))))
3783 ;; Merge the resent-to and resent-cc into the to and cc.
3784 ;; Bug#512. It's inappropriate to reply to these addresses.
3785 ;;; (if (and resent-to (not (equal resent-to "")))
3786 ;;; (if (not (equal to ""))
3787 ;;; (setq to (concat to ", " resent-to))
3788 ;;; (setq to resent-to)))
3789 ;;; (if (and resent-cc (not (equal resent-cc "")))
3790 ;;; (if (not (equal cc ""))
3791 ;;; (setq cc (concat cc ", " resent-cc))
3792 ;;; (setq cc resent-cc)))
3793 ;; Add `Re: ' to subject if not there already.
3794 (and (stringp subject)
3795 (setq subject
3796 (concat rmail-reply-prefix
3797 (if (let ((case-fold-search t))
3798 (string-match rmail-reply-regexp subject))
3799 (substring subject (match-end 0))
3800 subject))))
3801 (rmail-start-mail
3802 nil
3803 ;; Using mail-strip-quoted-names is undesirable with newer mailers
3804 ;; since they can handle the names unstripped.
3805 ;; I don't know whether there are other mailers that still
3806 ;; need the names to be stripped.
3807 ;;; (mail-strip-quoted-names reply-to)
3808 ;; Remove unwanted names from reply-to, since Mail-Followup-To
3809 ;; header causes all the names in it to wind up in reply-to, not
3810 ;; in cc. But if what's left is an empty list, use the original.
3811 (let* ((reply-to-list (mail-dont-reply-to reply-to)))
3812 (if (string= reply-to-list "") reply-to reply-to-list))
3813 subject
3814 (rmail-make-in-reply-to-field from date message-id)
3815 (if just-sender
3816 nil
3817 ;; `mail-dont-reply-to' doesn't need `mail-strip-quoted-names'.
3818 (let* ((cc-list (mail-dont-reply-to
3819 (mail-strip-quoted-names
3820 (if (null cc) to (concat to ", " cc))))))
3821 (if (string= cc-list "") nil cc-list)))
3822 rmail-buffer
3823 (list (list 'rmail-mark-message
3824 rmail-buffer
3825 (with-current-buffer rmail-buffer
3826 (aref rmail-msgref-vector msgnum))
3827 rmail-answered-attr-index))
3828 nil
3829 (if (or references message-id)
3830 (list (cons "References" (if references
3831 (concat
3832 (mapconcat 'identity references " ")
3833 " " message-id)
3834 message-id)))))))
3835 \f
3836 (defun rmail-mark-message (buffer msgnum-list attribute)
3837 "Give BUFFER's message number in MSGNUM-LIST the attribute ATTRIBUTE.
3838 This is use in the send-actions for message buffers.
3839 MSGNUM-LIST is a list of the form (MSGNUM)
3840 which is an element of rmail-msgref-vector."
3841 (with-current-buffer buffer
3842 (if (car msgnum-list)
3843 (rmail-set-attribute attribute t (car msgnum-list)))))
3844
3845 (defun rmail-make-in-reply-to-field (from date message-id)
3846 (cond ((not from)
3847 (if message-id
3848 message-id
3849 nil))
3850 (mail-use-rfc822
3851 (require 'rfc822)
3852 (let ((tem (car (rfc822-addresses from))))
3853 (if message-id
3854 (if (or (not tem)
3855 (string-match
3856 (regexp-quote (if (string-match "@[^@]*\\'" tem)
3857 (substring tem 0
3858 (match-beginning 0))
3859 tem))
3860 message-id))
3861 ;; missing From, or Message-ID is sufficiently informative
3862 message-id
3863 (concat message-id " (" tem ")"))
3864 ;; Copy TEM, discarding text properties.
3865 (setq tem (copy-sequence tem))
3866 (set-text-properties 0 (length tem) nil tem)
3867 (setq tem (copy-sequence tem))
3868 ;; Use prin1 to fake RFC822 quoting
3869 (let ((field (prin1-to-string tem)))
3870 (if date
3871 (concat field "'s message of " date)
3872 field)))))
3873 ((let* ((foo "[^][\000-\037()<>@,;:\\\" ]+")
3874 (bar "[^][\000-\037()<>@,;:\\\"]+"))
3875 ;; These strings both match all non-ASCII characters.
3876 (or (string-match (concat "\\`[ \t]*\\(" bar
3877 "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
3878 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
3879 from)
3880 (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
3881 bar "\\))[ \t]*\\'")
3882 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
3883 from)))
3884 (let ((start (match-beginning 1))
3885 (end (match-end 1)))
3886 ;; Trim whitespace which above regexp match allows
3887 (while (and (< start end)
3888 (memq (aref from start) '(?\t ?\s)))
3889 (setq start (1+ start)))
3890 (while (and (< start end)
3891 (memq (aref from (1- end)) '(?\t ?\s)))
3892 (setq end (1- end)))
3893 (let ((field (substring from start end)))
3894 (if date (setq field (concat "message from " field " on " date)))
3895 (if message-id
3896 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
3897 (concat message-id " (" field ")")
3898 field))))
3899 (t
3900 ;; If we can't kludge it simply, do it correctly
3901 (let ((mail-use-rfc822 t))
3902 (rmail-make-in-reply-to-field from date message-id)))))
3903 \f
3904 (defun rmail-forward (resend)
3905 "Forward the current message to another user.
3906 With prefix argument, \"resend\" the message instead of forwarding it;
3907 see the documentation of `rmail-resend'."
3908 (interactive "P")
3909 (if (zerop rmail-current-message)
3910 (error "No message to forward"))
3911 (if resend
3912 (call-interactively 'rmail-resend)
3913 (let ((forward-buffer rmail-buffer)
3914 (msgnum rmail-current-message)
3915 (subject (concat "["
3916 (let ((from (or (mail-fetch-field "From")
3917 ;; FIXME - huh?
3918 (mail-fetch-field ">From"))))
3919 (if from
3920 (concat (mail-strip-quoted-names from) ": ")
3921 ""))
3922 (or (mail-fetch-field "Subject") "")
3923 "]")))
3924 (if (rmail-start-mail
3925 nil nil subject nil nil rmail-buffer
3926 (list (list 'rmail-mark-message
3927 forward-buffer
3928 (with-current-buffer rmail-buffer
3929 (aref rmail-msgref-vector msgnum))
3930 rmail-forwarded-attr-index))
3931 ;; If only one window, use it for the mail buffer.
3932 ;; Otherwise, use another window for the mail buffer
3933 ;; so that the Rmail buffer remains visible
3934 ;; and sending the mail will get back to it.
3935 (and (not rmail-mail-new-frame) (one-window-p t)))
3936 ;; The mail buffer is now current.
3937 (save-excursion
3938 ;; Insert after header separator--before signature if any.
3939 (rfc822-goto-eoh)
3940 (forward-line 1)
3941 (if (and rmail-enable-mime rmail-enable-mime-composing
3942 rmail-insert-mime-forwarded-message-function)
3943 (prog1
3944 (funcall rmail-insert-mime-forwarded-message-function
3945 forward-buffer)
3946 ;; rmail-insert-mime-forwarded-message-function
3947 ;; works by inserting MML tags into forward-buffer.
3948 ;; The MUA will need to convert it to MIME before
3949 ;; sending. mail-encode-mml tells them to do that.
3950 ;; message.el does that automagically.
3951 (or (eq mail-user-agent 'message-user-agent)
3952 (setq mail-encode-mml t)))
3953 (insert "------- Start of forwarded message -------\n")
3954 ;; Quote lines with `- ' if they start with `-'.
3955 (let ((beg (point)) end)
3956 (setq end (point-marker))
3957 (set-marker-insertion-type end t)
3958 (insert-buffer-substring forward-buffer)
3959 (goto-char beg)
3960 (while (re-search-forward "^-" end t)
3961 (beginning-of-line)
3962 (insert "- ")
3963 (forward-line 1))
3964 (goto-char end)
3965 (skip-chars-backward "\n")
3966 (if (< (point) end)
3967 (forward-char 1))
3968 (delete-region (point) end)
3969 (set-marker end nil))
3970 (insert "------- End of forwarded message -------\n"))
3971 (push-mark))))))
3972 \f
3973 (defun rmail-resend (address &optional from comment mail-alias-file)
3974 "Resend current message to ADDRESSES.
3975 ADDRESSES should be a single address, a string consisting of several
3976 addresses separated by commas, or a list of addresses.
3977
3978 Optional FROM is the address to resend the message from, and
3979 defaults from the value of `user-mail-address'.
3980 Optional COMMENT is a string to insert as a comment in the resent message.
3981 Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
3982 typically for purposes of moderating a list."
3983 (interactive "sResend to: ")
3984 (require 'sendmail)
3985 (require 'mailalias)
3986 (unless (or (eq rmail-view-buffer (current-buffer))
3987 (eq rmail-buffer (current-buffer)))
3988 (error "Not an Rmail buffer"))
3989 (if (not from) (setq from user-mail-address))
3990 (let ((tembuf (generate-new-buffer " sendmail temp"))
3991 (case-fold-search nil)
3992 (mail-personal-alias-file
3993 (or mail-alias-file mail-personal-alias-file))
3994 (mailbuf rmail-buffer))
3995 (unwind-protect
3996 (with-current-buffer tembuf
3997 ;;>> Copy message into temp buffer
3998 (if (and rmail-enable-mime
3999 rmail-insert-mime-resent-message-function)
4000 (funcall rmail-insert-mime-resent-message-function mailbuf)
4001 (insert-buffer-substring mailbuf))
4002 (goto-char (point-min))
4003 ;; Delete any Sender field, since that's not specifiable.
4004 ; Only delete Sender fields in the actual header.
4005 (re-search-forward "^$" nil 'move)
4006 ; Using "while" here rather than "if" because some buggy mail
4007 ; software may have inserted multiple Sender fields.
4008 (while (re-search-backward "^Sender:" nil t)
4009 (let (beg)
4010 (setq beg (point))
4011 (forward-line 1)
4012 (while (looking-at "[ \t]")
4013 (forward-line 1))
4014 (delete-region beg (point))))
4015 ; Go back to the beginning of the buffer so the Resent- fields
4016 ; are inserted there.
4017 (goto-char (point-min))
4018 ;;>> Insert resent-from:
4019 (insert "Resent-From: " from "\n")
4020 (insert "Resent-Date: " (mail-rfc822-date) "\n")
4021 ;;>> Insert resent-to: and bcc if need be.
4022 (let ((before (point)))
4023 (if mail-self-blind
4024 (insert "Resent-Bcc: " (user-login-name) "\n"))
4025 (insert "Resent-To: " (if (stringp address)
4026 address
4027 (mapconcat 'identity address ",\n\t"))
4028 "\n")
4029 ;; Expand abbrevs in the recipients.
4030 (save-excursion
4031 (if (featurep 'mailabbrev)
4032 (let ((end (point-marker))
4033 (local-abbrev-table mail-abbrevs)
4034 (old-syntax-table (syntax-table)))
4035 (if (and (not (vectorp mail-abbrevs))
4036 (file-exists-p mail-personal-alias-file))
4037 (build-mail-abbrevs))
4038 (unless mail-abbrev-syntax-table
4039 (mail-abbrev-make-syntax-table))
4040 (set-syntax-table mail-abbrev-syntax-table)
4041 (goto-char before)
4042 (while (and (< (point) end)
4043 (progn (forward-word 1)
4044 (<= (point) end)))
4045 (expand-abbrev))
4046 (set-syntax-table old-syntax-table))
4047 (expand-mail-aliases before (point)))))
4048 ;;>> Set up comment, if any.
4049 (if (and (sequencep comment) (not (zerop (length comment))))
4050 (let ((before (point))
4051 after)
4052 (insert comment)
4053 (or (eolp) (insert "\n"))
4054 (setq after (point))
4055 (goto-char before)
4056 (while (< (point) after)
4057 (insert "Resent-Comment: ")
4058 (forward-line 1))))
4059 ;; Don't expand aliases in the destination fields
4060 ;; of the original message.
4061 (let (mail-aliases)
4062 (funcall send-mail-function)))
4063 (kill-buffer tembuf))
4064 (with-current-buffer rmail-buffer
4065 (rmail-set-attribute rmail-resent-attr-index t rmail-current-message))))
4066 \f
4067 (defvar mail-unsent-separator
4068 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
4069 "^ *---+ +Returned message +---+ *$\\|"
4070 "^ *---+ *Returned mail follows *---+ *$\\|"
4071 "^Start of returned message$\\|"
4072 "^---+ Below this line is a copy of the message.$\\|"
4073 "^ *---+ +Original message +---+ *$\\|"
4074 "^ *--+ +begin message +--+ *$\\|"
4075 "^ *---+ +Original message follows +---+ *$\\|"
4076 "^ *---+ +Your message follows +---+ *$\\|"
4077 "^|? *---+ +Message text follows: +---+ *|?$\\|"
4078 "^ *---+ +This is a copy of \\w+ message, including all the headers.*---+ *$")
4079 "A regexp that matches the separator before the text of a failed message.")
4080
4081 (defvar mail-mime-unsent-header "^Content-Type: message/rfc822 *$"
4082 "A regexp that matches the header of a MIME body part with a failed message.")
4083
4084 ;; This is a cut-down version of rmail-clear-headers from Emacs 22.
4085 ;; It doesn't have the same functionality, hence the name change.
4086 (defun rmail-delete-headers (regexp)
4087 "Delete any mail headers matching REGEXP.
4088 The message should be narrowed to just the headers."
4089 (when regexp
4090 (goto-char (point-min))
4091 (while (re-search-forward regexp nil t)
4092 (beginning-of-line)
4093 ;; This code from Emacs 22 doesn't seem right, since r-n-h is
4094 ;; just for display.
4095 ;;; (if (looking-at rmail-nonignored-headers)
4096 ;;; (forward-line 1)
4097 (delete-region (point)
4098 (save-excursion
4099 (if (re-search-forward "\n[^ \t]" nil t)
4100 (1- (point))
4101 (point-max)))))))
4102
4103 (autoload 'mail-position-on-field "sendmail")
4104
4105 (defun rmail-retry-failure ()
4106 "Edit a mail message which is based on the contents of the current message.
4107 For a message rejected by the mail system, extract the interesting headers and
4108 the body of the original message.
4109 If the failed message is a MIME multipart message, it is searched for a
4110 body part with a header which matches the variable `mail-mime-unsent-header'.
4111 Otherwise, the variable `mail-unsent-separator' should match the string that
4112 delimits the returned original message.
4113 The variable `rmail-retry-ignored-headers' is a regular expression
4114 specifying headers which should not be copied into the new message."
4115 (interactive)
4116 (require 'mail-utils)
4117 (if rmail-enable-mime
4118 (with-current-buffer rmail-buffer
4119 (if (rmail-mime-message-p)
4120 (let ((rmail-mime-mbox-buffer rmail-view-buffer)
4121 (rmail-mime-view-buffer rmail-buffer))
4122 (rmail-mime-toggle-raw 'raw)))))
4123
4124 (let ((rmail-this-buffer (current-buffer))
4125 (msgnum rmail-current-message)
4126 bounce-start bounce-end bounce-indent resending
4127 (content-type (rmail-get-header "Content-Type")))
4128 (save-excursion
4129 (goto-char (point-min))
4130 (let ((case-fold-search t))
4131 (if (and content-type
4132 (string-match
4133 ";[\n\t ]*boundary=\"?\\([-0-9a-z'()+_,./:=? ]+\\)\"?"
4134 content-type))
4135 ;; Handle a MIME multipart bounce message.
4136 (let ((codestring
4137 (concat "\n--"
4138 (substring content-type (match-beginning 1)
4139 (match-end 1)))))
4140 (unless (re-search-forward mail-mime-unsent-header nil t)
4141 (error "Cannot find beginning of header in failed message"))
4142 (unless (search-forward "\n\n" nil t)
4143 (error "Cannot find start of Mime data in failed message"))
4144 (setq bounce-start (point))
4145 (if (search-forward codestring nil t)
4146 (setq bounce-end (match-beginning 0))
4147 (setq bounce-end (point-max))))
4148 ;; Non-MIME bounce.
4149 (or (re-search-forward mail-unsent-separator nil t)
4150 (error "Cannot parse this as a failure message"))
4151 (skip-chars-forward "\n")
4152 ;; Support a style of failure message in which the original
4153 ;; message is indented, and included within lines saying
4154 ;; `Start of returned message' and `End of returned message'.
4155 (if (looking-at " +Received:")
4156 (progn
4157 (setq bounce-start (point))
4158 (skip-chars-forward " ")
4159 (setq bounce-indent (- (current-column)))
4160 (goto-char (point-max))
4161 (re-search-backward "^End of returned message$" nil t)
4162 (setq bounce-end (point)))
4163 ;; One message contained a few random lines before
4164 ;; the old message header. The first line of the
4165 ;; message started with two hyphens. A blank line
4166 ;; followed these random lines. The same line
4167 ;; beginning with two hyphens was possibly marking
4168 ;; the end of the message.
4169 (if (looking-at "^--")
4170 (let ((boundary (buffer-substring-no-properties
4171 (point)
4172 (progn (end-of-line) (point)))))
4173 (search-forward "\n\n")
4174 (skip-chars-forward "\n")
4175 (setq bounce-start (point))
4176 (goto-char (point-max))
4177 (search-backward (concat "\n\n" boundary) bounce-start t)
4178 (setq bounce-end (point)))
4179 (setq bounce-start (point)
4180 bounce-end (point-max)))
4181 (unless (search-forward "\n\n" nil t)
4182 (error "Cannot find end of header in failed message"))))))
4183 ;; We have found the message that bounced, within the current message.
4184 ;; Now start sending new message; default header fields from original.
4185 ;; Turn off the usual actions for initializing the message body
4186 ;; because we want to get only the text from the failure message.
4187 (let (mail-signature mail-setup-hook)
4188 (if (rmail-start-mail nil nil nil nil nil rmail-this-buffer
4189 (list (list 'rmail-mark-message
4190 rmail-this-buffer
4191 (aref rmail-msgref-vector msgnum)
4192 rmail-retried-attr-index)))
4193 ;; Insert original text as initial text of new draft message.
4194 ;; Bind inhibit-read-only since the header delimiter
4195 ;; of the previous message was probably read-only.
4196 (let ((inhibit-read-only t)
4197 eoh)
4198 (erase-buffer)
4199 (insert-buffer-substring rmail-this-buffer
4200 bounce-start bounce-end)
4201 (goto-char (point-min))
4202 (if bounce-indent
4203 (indent-rigidly (point-min) (point-max) bounce-indent))
4204 (rfc822-goto-eoh)
4205 (setq eoh (point))
4206 (insert mail-header-separator)
4207 (save-restriction
4208 (narrow-to-region (point-min) eoh)
4209 (rmail-delete-headers rmail-retry-ignored-headers)
4210 (rmail-delete-headers "^\\(sender\\|return-path\\|received\\):")
4211 (setq resending (mail-fetch-field "resent-to"))
4212 (if mail-self-blind
4213 (if resending
4214 (insert "Resent-Bcc: " (user-login-name) "\n")
4215 (insert "BCC: " (user-login-name) "\n"))))
4216 (goto-char (point-min))
4217 (mail-position-on-field (if resending "Resent-To" "To") t))))))
4218 \f
4219 (defun rmail-summary-exists ()
4220 "Non-nil if in an RMAIL buffer and an associated summary buffer exists.
4221 In fact, the non-nil value returned is the summary buffer itself."
4222 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
4223 rmail-summary-buffer))
4224
4225 (defun rmail-summary-displayed ()
4226 "t if in RMAIL buffer and an associated summary buffer is displayed."
4227 (and rmail-summary-buffer (get-buffer-window rmail-summary-buffer)))
4228
4229 (defcustom rmail-redisplay-summary nil
4230 "Non-nil means Rmail should show the summary when it changes.
4231 This has an effect only if a summary buffer exists."
4232 :type 'boolean
4233 :group 'rmail-summary)
4234
4235 (defcustom rmail-summary-window-size nil
4236 "Non-nil means specify the height for an Rmail summary window."
4237 :type '(choice (const :tag "Disabled" nil) integer)
4238 :group 'rmail-summary)
4239
4240 ;; Put the summary buffer back on the screen, if user wants that.
4241 (defun rmail-maybe-display-summary ()
4242 (cond
4243 ((or (not rmail-summary-buffer)
4244 (not (buffer-name rmail-summary-buffer))))
4245 (rmail-redisplay-summary
4246 ;; If `rmail-redisplay-summary' is non-nil, make sure the summary
4247 ;; buffer is displayed.
4248 (display-buffer
4249 rmail-summary-buffer
4250 `(nil
4251 (reusable-frames . 0)
4252 ,(when rmail-summary-window-size
4253 `(window-height . ,rmail-summary-window-size)))))
4254 (rmail-summary-window-size
4255 ;; If `rmail-summary-window-size' is non-nil and the summary buffer
4256 ;; is displayed, make sure it gets resized.
4257 (let ((window (get-buffer-window rmail-summary-buffer 0)))
4258 (when window
4259 (window-resize-no-error
4260 window (- rmail-summary-window-size (window-height window))))))))
4261 \f
4262 ;;;; *** Rmail Local Fontification ***
4263
4264 (defun rmail-fontify-buffer-function ()
4265 ;; This function's symbol is bound to font-lock-fontify-buffer-function.
4266 (add-hook 'rmail-show-message-hook 'rmail-fontify-message nil t)
4267 ;; If we're already showing a message, fontify it now.
4268 (if rmail-current-message (rmail-fontify-message))
4269 ;; Prevent Font Lock mode from kicking in.
4270 (setq font-lock-fontified t))
4271
4272 (defun rmail-unfontify-buffer-function ()
4273 ;; This function's symbol is bound to font-lock-fontify-unbuffer-function.
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-restriction
4279 (widen)
4280 (remove-hook 'rmail-show-message-hook 'rmail-fontify-message t)
4281 (remove-text-properties (point-min) (point-max) '(rmail-fontified nil))
4282 (font-lock-default-unfontify-buffer)
4283 (and (not modified) (buffer-modified-p)
4284 (restore-buffer-modified-p nil)))))
4285
4286 (defun rmail-fontify-message ()
4287 ;; Fontify the current message if it is not already fontified.
4288 (if (text-property-any (point-min) (point-max) 'rmail-fontified nil)
4289 (let ((modified (buffer-modified-p))
4290 (buffer-undo-list t) (inhibit-read-only t)
4291 before-change-functions after-change-functions
4292 buffer-file-name buffer-file-truename)
4293 (save-excursion
4294 (save-match-data
4295 (add-text-properties (point-min) (point-max) '(rmail-fontified t))
4296 (font-lock-fontify-region (point-min) (point-max))
4297 (and (not modified) (buffer-modified-p)
4298 (restore-buffer-modified-p nil)))))))
4299 \f
4300 ;;; Speedbar support for RMAIL files.
4301 (eval-when-compile (require 'speedbar))
4302
4303 (defcustom rmail-speedbar-match-folder-regexp "^[A-Z0-9]+\\(\\.[A-Z0-9]+\\)?$"
4304 "Regexp matching Rmail folder names to be displayed in Speedbar.
4305 Enabling this permits Speedbar to display your folders for easy
4306 browsing, and moving of messages."
4307 :type 'regexp
4308 :group 'rmail
4309 :group 'speedbar)
4310
4311 (defvar rmail-speedbar-last-user nil
4312 "The last user to be displayed in the speedbar.")
4313
4314 (defvar rmail-speedbar-key-map nil
4315 "Keymap used when in rmail display mode.")
4316
4317 (defun rmail-install-speedbar-variables ()
4318 "Install those variables used by speedbar to enhance rmail."
4319 (if rmail-speedbar-key-map
4320 nil
4321 (setq rmail-speedbar-key-map (speedbar-make-specialized-keymap))
4322
4323 (define-key rmail-speedbar-key-map "e" 'speedbar-edit-line)
4324 (define-key rmail-speedbar-key-map "r" 'speedbar-edit-line)
4325 (define-key rmail-speedbar-key-map "\C-m" 'speedbar-edit-line)
4326 (define-key rmail-speedbar-key-map "M"
4327 'rmail-speedbar-move-message-to-folder-on-line)))
4328
4329 ;; Mouse-3.
4330 (defvar rmail-speedbar-menu-items
4331 '(["Read Folder" speedbar-edit-line t]
4332 ["Move message to folder" rmail-speedbar-move-message-to-folder-on-line
4333 (save-excursion (beginning-of-line)
4334 (looking-at "<M> "))])
4335 "Additional menu-items to add to speedbar frame.")
4336
4337 ;; Make sure our special speedbar major mode is loaded
4338 (if (featurep 'speedbar)
4339 (rmail-install-speedbar-variables)
4340 (add-hook 'speedbar-load-hook 'rmail-install-speedbar-variables))
4341
4342 (defun rmail-speedbar-buttons (buffer)
4343 "Create buttons for BUFFER containing rmail messages.
4344 Click on the address under Reply to: to reply to this person.
4345 Under Folders: Click a name to read it, or on the <M> to move the
4346 current message into that RMAIL folder."
4347 (let ((from nil))
4348 (with-current-buffer buffer
4349 (goto-char (point-min))
4350 (if (not (re-search-forward "^Reply-To: " nil t))
4351 (if (not (re-search-forward "^From:? " nil t))
4352 (setq from t)))
4353 (if from
4354 nil
4355 (setq from (buffer-substring (point) (line-end-position)))))
4356 (goto-char (point-min))
4357 (if (and (looking-at "Reply to:")
4358 (equal from rmail-speedbar-last-user))
4359 nil
4360 (setq rmail-speedbar-last-user from)
4361 (erase-buffer)
4362 (insert "Reply To:\n")
4363 (if (stringp from)
4364 (speedbar-insert-button from 'speedbar-directory-face 'highlight
4365 'rmail-speedbar-button 'rmail-reply))
4366 (insert "Folders:\n")
4367 (let* ((case-fold-search nil)
4368 (df (directory-files (with-current-buffer buffer
4369 default-directory)
4370 nil rmail-speedbar-match-folder-regexp)))
4371 (dolist (file df)
4372 (when (file-regular-p file)
4373 (speedbar-insert-button "<M>" 'speedbar-button-face 'highlight
4374 'rmail-speedbar-move-message file)
4375 (speedbar-insert-button file 'speedbar-file-face 'highlight
4376 'rmail-speedbar-find-file nil t)))))))
4377
4378 (defun rmail-speedbar-button (text token indent)
4379 "Execute an rmail command specified by TEXT.
4380 The command used is TOKEN. INDENT is not used."
4381 (speedbar-with-attached-buffer
4382 (funcall token t)))
4383
4384 (defun rmail-speedbar-find-file (text token indent)
4385 "Load in the rmail file TEXT.
4386 TOKEN and INDENT are not used."
4387 (speedbar-with-attached-buffer
4388 (message "Loading in RMAIL file %s..." text)
4389 (rmail text)))
4390
4391 (defun rmail-speedbar-move-message-to-folder-on-line ()
4392 "If the current line is a folder, move current message to it."
4393 (interactive)
4394 (save-excursion
4395 (beginning-of-line)
4396 (if (re-search-forward "<M> " (line-end-position) t)
4397 (progn
4398 (forward-char -2)
4399 (speedbar-do-function-pointer)))))
4400
4401 (defun rmail-speedbar-move-message (text token indent)
4402 "From button TEXT, copy current message to the rmail file specified by TOKEN.
4403 TEXT and INDENT are not used."
4404 (speedbar-with-attached-buffer
4405 (message "Moving message to %s" token)
4406 ;; expand-file-name is needed due to the unhelpful way in which
4407 ;; rmail-output expands non-absolute filenames against rmail-default-file.
4408 ;; What is the point of that, anyway?
4409 (rmail-output (expand-file-name token))))
4410 \f
4411 ;; Functions for setting, getting and encoding the POP password.
4412 ;; The password is encoded to prevent it from being easily accessible
4413 ;; to "prying eyes." Obviously, this encoding isn't "real security,"
4414 ;; nor is it meant to be.
4415
4416 ;;;###autoload
4417 (defun rmail-set-remote-password (password)
4418 "Set PASSWORD to be used for retrieving mail from a POP or IMAP server."
4419 (interactive "sPassword: ")
4420 (if password
4421 (setq rmail-encoded-remote-password
4422 (rmail-encode-string password (emacs-pid)))
4423 (setq rmail-remote-password nil)
4424 (setq rmail-encoded-remote-password nil)))
4425
4426 (defun rmail-get-remote-password (imap)
4427 "Get the password for retrieving mail from a POP or IMAP server. If none
4428 has been set, then prompt the user for one."
4429 (when (not rmail-encoded-remote-password)
4430 (if (not rmail-remote-password)
4431 (setq rmail-remote-password
4432 (read-passwd (if imap
4433 "IMAP password: "
4434 "POP password: "))))
4435 (rmail-set-remote-password rmail-remote-password)
4436 (setq rmail-remote-password nil))
4437 (rmail-encode-string rmail-encoded-remote-password (emacs-pid)))
4438
4439 (defun rmail-have-password ()
4440 (or rmail-remote-password rmail-encoded-remote-password))
4441
4442 (defun rmail-encode-string (string mask)
4443 "Encode STRING with integer MASK, by taking the exclusive OR of the
4444 lowest byte in the mask with the first character of string, the
4445 second-lowest-byte with the second character of the string, etc.,
4446 restarting at the lowest byte of the mask whenever it runs out.
4447 Returns the encoded string. Calling the function again with an
4448 encoded string (and the same mask) will decode the string."
4449 (setq mask (abs mask)) ; doesn't work if negative
4450 (let* ((string-vector (string-to-vector string)) (i 0)
4451 (len (length string-vector)) (curmask mask) charmask)
4452 (while (< i len)
4453 (if (= curmask 0)
4454 (setq curmask mask))
4455 (setq charmask (% curmask 256))
4456 (setq curmask (lsh curmask -8))
4457 (aset string-vector i (logxor charmask (aref string-vector i)))
4458 (setq i (1+ i)))
4459 (concat string-vector)))
4460
4461 ;; Should this have a key-binding, or be in a menu?
4462 ;; There doesn't really seem to be an appropriate menu.
4463 ;; Eg the edit command is not in a menu either.
4464 (defun rmail-epa-decrypt ()
4465 "Decrypt OpenPGP armors in current message."
4466 (interactive)
4467
4468 ;; Save the current buffer here for cleanliness, in case we
4469 ;; change it in one of the calls to `epa-decrypt-region'.
4470
4471 (save-excursion
4472 (let (decrypts)
4473 (goto-char (point-min))
4474
4475 ;; In case the encrypted data is inside a mime attachment,
4476 ;; show it. This is a kludge; to be clean, it should not
4477 ;; modify the buffer, but I don't see how to do that.
4478 (when (search-forward "octet-stream" nil t)
4479 (beginning-of-line)
4480 (forward-button 1)
4481 (if (looking-at "Show")
4482 (rmail-mime-toggle-hidden)))
4483
4484 ;; Now find all armored messages in the buffer
4485 ;; and decrypt them one by one.
4486 (goto-char (point-min))
4487 (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
4488 (let ((coding-system-for-read coding-system-for-read)
4489 armor-start armor-end after-end)
4490 (setq armor-start (match-beginning 0)
4491 armor-end (re-search-forward "^-----END PGP MESSAGE-----$"
4492 nil t))
4493 (unless armor-end
4494 (error "Encryption armor beginning has no matching end"))
4495 (goto-char armor-start)
4496
4497 ;; Because epa--find-coding-system-for-mime-charset not autoloaded.
4498 (require 'epa)
4499
4500 ;; Use the charset specified in the armor.
4501 (unless coding-system-for-read
4502 (if (re-search-forward "^Charset: \\(.*\\)" armor-end t)
4503 (setq coding-system-for-read
4504 (epa--find-coding-system-for-mime-charset
4505 (intern (downcase (match-string 1)))))))
4506
4507 ;; Advance over this armor.
4508 (goto-char armor-end)
4509 (setq after-end (- (point-max) armor-end))
4510
4511 ;; Decrypt it, maybe in place, maybe making new buffer.
4512 (epa-decrypt-region
4513 armor-start armor-end
4514 ;; Call back this function to prepare the output.
4515 (lambda ()
4516 (let ((inhibit-read-only t))
4517 (delete-region armor-start armor-end)
4518 (goto-char armor-start)
4519 (current-buffer))))
4520
4521 (push (list armor-start (- (point-max) after-end))
4522 decrypts)))
4523
4524 (when (and decrypts (rmail-buffers-swapped-p))
4525 (when (y-or-n-p "Replace the original message? ")
4526 (setq decrypts (nreverse decrypts))
4527 (let ((beg (rmail-msgbeg rmail-current-message))
4528 (end (rmail-msgend rmail-current-message))
4529 (from-buffer (current-buffer)))
4530 (with-current-buffer rmail-view-buffer
4531 (narrow-to-region beg end)
4532 (goto-char (point-min))
4533 (dolist (d decrypts)
4534 (if (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
4535 (let (armor-start armor-end)
4536 (setq armor-start (match-beginning 0)
4537 armor-end (re-search-forward "^-----END PGP MESSAGE-----$"
4538 nil t))
4539 (when armor-end
4540 (delete-region armor-start armor-end)
4541 (insert-buffer-substring from-buffer (nth 0 d) (nth 1 d)))))))))))))
4542 \f
4543 ;;;; Desktop support
4544
4545 (defun rmail-restore-desktop-buffer (desktop-buffer-file-name
4546 desktop-buffer-name
4547 desktop-buffer-misc)
4548 "Restore an rmail buffer specified in a desktop file."
4549 (condition-case error
4550 (progn
4551 (rmail-input desktop-buffer-file-name)
4552 (if (eq major-mode 'rmail-mode)
4553 (current-buffer)
4554 rmail-buffer))
4555 (file-locked
4556 (kill-buffer (current-buffer))
4557 nil)))
4558
4559 (add-to-list 'desktop-buffer-mode-handlers
4560 '(rmail-mode . rmail-restore-desktop-buffer))
4561
4562 ;; We use this to record the encoding of the current message before
4563 ;; saving the message collection.
4564 (defvar rmail-message-encoding nil)
4565
4566 ;; Used in `write-region-annotate-functions' to write rmail files.
4567 (defun rmail-write-region-annotate (start end)
4568 (when (and (null start) rmail-buffer-swapped)
4569 (unless (buffer-live-p rmail-view-buffer)
4570 (error "Buffer `%s' with real text of `%s' has disappeared"
4571 (buffer-name rmail-view-buffer)
4572 (buffer-name (current-buffer))))
4573 (setq rmail-message-encoding buffer-file-coding-system)
4574 (set-buffer rmail-view-buffer)
4575 (widen)
4576 nil))
4577
4578 ;; Used to restore the encoding of the buffer where we show the
4579 ;; current message, after we save the message collection. This is
4580 ;; needed because rmail-write-region-annotate switches buffers behind
4581 ;; save-file's back, with the side effect that last-coding-system-used
4582 ;; is assigned to buffer-file-coding-system of the wrong buffer.
4583 (defun rmail-after-save-hook ()
4584 (if (or (eq rmail-view-buffer (current-buffer))
4585 (eq rmail-buffer (current-buffer)))
4586 (with-current-buffer
4587 (if (rmail-buffers-swapped-p) rmail-buffer rmail-view-buffer)
4588 (setq buffer-file-coding-system rmail-message-encoding))))
4589 (add-hook 'after-save-hook 'rmail-after-save-hook)
4590
4591 \f
4592 ;;; Start of automatically extracted autoloads.
4593 \f
4594 ;;;### (autoloads (rmail-edit-current-message) "rmailedit" "rmailedit.el"
4595 ;;;;;; "0b056146d4775080a1847b8ce7527bc5")
4596 ;;; Generated autoloads from rmailedit.el
4597
4598 (autoload 'rmail-edit-current-message "rmailedit" "\
4599 Edit the contents of this message.
4600
4601 \(fn)" t nil)
4602
4603 ;;;***
4604 \f
4605 ;;;### (autoloads (rmail-next-labeled-message rmail-previous-labeled-message
4606 ;;;;;; rmail-read-label rmail-kill-label rmail-add-label) "rmailkwd"
4607 ;;;;;; "rmailkwd.el" "b5337290fd35bbc11888afb25d767195")
4608 ;;; Generated autoloads from rmailkwd.el
4609
4610 (autoload 'rmail-add-label "rmailkwd" "\
4611 Add LABEL to labels associated with current RMAIL message.
4612 Completes (see `rmail-read-label') over known labels when reading.
4613 LABEL may be a symbol or string. Only one label is allowed.
4614
4615 \(fn LABEL)" t nil)
4616
4617 (autoload 'rmail-kill-label "rmailkwd" "\
4618 Remove LABEL from labels associated with current RMAIL message.
4619 Completes (see `rmail-read-label') over known labels when reading.
4620 LABEL may be a symbol or string. Only one label is allowed.
4621
4622 \(fn LABEL)" t nil)
4623
4624 (autoload 'rmail-read-label "rmailkwd" "\
4625 Read a label with completion, prompting with PROMPT.
4626 Completions are chosen from `rmail-label-obarray'. The default
4627 is `rmail-last-label', if that is non-nil. Updates `rmail-last-label'
4628 according to the choice made, and returns a symbol.
4629
4630 \(fn PROMPT)" nil nil)
4631
4632 (autoload 'rmail-previous-labeled-message "rmailkwd" "\
4633 Show previous message with one of the labels LABELS.
4634 LABELS should be a comma-separated list of label names.
4635 If LABELS is empty, the last set of labels specified is used.
4636 With prefix argument N moves backward N messages with these labels.
4637
4638 \(fn N LABELS)" t nil)
4639
4640 (autoload 'rmail-next-labeled-message "rmailkwd" "\
4641 Show next message with one of the labels LABELS.
4642 LABELS should be a comma-separated list of label names.
4643 If LABELS is empty, the last set of labels specified is used.
4644 With prefix argument N moves forward N messages with these labels.
4645
4646 \(fn N LABELS)" t nil)
4647
4648 ;;;***
4649 \f
4650 ;;;### (autoloads (rmail-mime) "rmailmm" "rmailmm.el" "1f33964668345a1a1f3119fece148227")
4651 ;;; Generated autoloads from rmailmm.el
4652
4653 (autoload 'rmail-mime "rmailmm" "\
4654 Toggle the display of a MIME message.
4655
4656 The actual behavior depends on the value of `rmail-enable-mime'.
4657
4658 If `rmail-enable-mime' is non-nil (the default), this command toggles
4659 the display of a MIME message between decoded presentation form and
4660 raw data. With optional prefix argument ARG, it toggles the display only
4661 of the MIME entity at point, if there is one. The optional argument
4662 STATE forces a particular display state, rather than toggling.
4663 `raw' forces raw mode, any other non-nil value forces decoded mode.
4664
4665 If `rmail-enable-mime' is nil, this creates a temporary \"*RMAIL*\"
4666 buffer holding a decoded copy of the message. Inline content-types are
4667 handled according to `rmail-mime-media-type-handlers-alist'.
4668 By default, this displays text and multipart messages, and offers to
4669 download attachments as specified by `rmail-mime-attachment-dirs-alist'.
4670 The arguments ARG and STATE have no effect in this case.
4671
4672 \(fn &optional ARG STATE)" t nil)
4673
4674 ;;;***
4675 \f
4676 ;;;### (autoloads (set-rmail-inbox-list) "rmailmsc" "rmailmsc.el"
4677 ;;;;;; "8a2466563b4a463710531d01766c07a3")
4678 ;;; Generated autoloads from rmailmsc.el
4679
4680 (autoload 'set-rmail-inbox-list "rmailmsc" "\
4681 Set the inbox list of the current RMAIL file to FILE-NAME.
4682 You can specify one file name, or several names separated by commas.
4683 If FILE-NAME is empty, remove any existing inbox list.
4684
4685 This applies only to the current session.
4686
4687 \(fn FILE-NAME)" t nil)
4688
4689 ;;;***
4690 \f
4691 ;;;### (autoloads (rmail-sort-by-labels rmail-sort-by-lines rmail-sort-by-correspondent
4692 ;;;;;; rmail-sort-by-recipient rmail-sort-by-author rmail-sort-by-subject
4693 ;;;;;; rmail-sort-by-date) "rmailsort" "rmailsort.el" "3e3a30326fc95d7f17835906c2ccb19f")
4694 ;;; Generated autoloads from rmailsort.el
4695
4696 (autoload 'rmail-sort-by-date "rmailsort" "\
4697 Sort messages of current Rmail buffer by \"Date\" header.
4698 If prefix argument REVERSE is non-nil, sorts in reverse order.
4699
4700 \(fn REVERSE)" t nil)
4701
4702 (autoload 'rmail-sort-by-subject "rmailsort" "\
4703 Sort messages of current Rmail buffer by \"Subject\" header.
4704 Ignores any \"Re: \" prefix. If prefix argument REVERSE is
4705 non-nil, sorts in reverse order.
4706
4707 \(fn REVERSE)" t nil)
4708
4709 (autoload 'rmail-sort-by-author "rmailsort" "\
4710 Sort messages of current Rmail buffer by author.
4711 This uses either the \"From\" or \"Sender\" header, downcased.
4712 If prefix argument REVERSE is non-nil, sorts in reverse order.
4713
4714 \(fn REVERSE)" t nil)
4715
4716 (autoload 'rmail-sort-by-recipient "rmailsort" "\
4717 Sort messages of current Rmail buffer by recipient.
4718 This uses either the \"To\" or \"Apparently-To\" header, downcased.
4719 If prefix argument REVERSE is non-nil, sorts in reverse order.
4720
4721 \(fn REVERSE)" t nil)
4722
4723 (autoload 'rmail-sort-by-correspondent "rmailsort" "\
4724 Sort messages of current Rmail buffer by other correspondent.
4725 This uses either the \"From\", \"Sender\", \"To\", or
4726 \"Apparently-To\" header, downcased. Uses the first header not
4727 excluded by `mail-dont-reply-to-names'. If prefix argument
4728 REVERSE is non-nil, sorts in reverse order.
4729
4730 \(fn REVERSE)" t nil)
4731
4732 (autoload 'rmail-sort-by-lines "rmailsort" "\
4733 Sort messages of current Rmail buffer by the number of lines.
4734 If prefix argument REVERSE is non-nil, sorts in reverse order.
4735
4736 \(fn REVERSE)" t nil)
4737
4738 (autoload 'rmail-sort-by-labels "rmailsort" "\
4739 Sort messages of current Rmail buffer by labels.
4740 LABELS is a comma-separated list of labels. The order of these
4741 labels specifies the order of messages: messages with the first
4742 label come first, messages with the second label come second, and
4743 so on. Messages that have none of these labels come last.
4744 If prefix argument REVERSE is non-nil, sorts in reverse order.
4745
4746 \(fn REVERSE LABELS)" t nil)
4747
4748 ;;;***
4749 \f
4750 ;;;### (autoloads (rmail-summary-by-senders rmail-summary-by-topic
4751 ;;;;;; rmail-summary-by-regexp rmail-summary-by-recipients rmail-summary-by-labels
4752 ;;;;;; rmail-summary) "rmailsum" "rmailsum.el" "341825201e892b8fc875c1ae49ffd560")
4753 ;;; Generated autoloads from rmailsum.el
4754
4755 (autoload 'rmail-summary "rmailsum" "\
4756 Display a summary of all messages, one line per message.
4757
4758 \(fn)" t nil)
4759
4760 (autoload 'rmail-summary-by-labels "rmailsum" "\
4761 Display a summary of all messages with one or more LABELS.
4762 LABELS should be a string containing the desired labels, separated by commas.
4763
4764 \(fn LABELS)" t nil)
4765
4766 (autoload 'rmail-summary-by-recipients "rmailsum" "\
4767 Display a summary of all messages with the given RECIPIENTS.
4768 Normally checks the To, From and Cc fields of headers;
4769 but if PRIMARY-ONLY is non-nil (prefix arg given),
4770 only look in the To and From fields.
4771 RECIPIENTS is a string of regexps separated by commas.
4772
4773 \(fn RECIPIENTS &optional PRIMARY-ONLY)" t nil)
4774
4775 (autoload 'rmail-summary-by-regexp "rmailsum" "\
4776 Display a summary of all messages according to regexp REGEXP.
4777 If the regular expression is found in the header of the message
4778 \(including in the date and other lines, as well as the subject line),
4779 Emacs will list the message in the summary.
4780
4781 \(fn REGEXP)" t nil)
4782
4783 (autoload 'rmail-summary-by-topic "rmailsum" "\
4784 Display a summary of all messages with the given SUBJECT.
4785 Normally checks just the Subject field of headers; but with prefix
4786 argument WHOLE-MESSAGE is non-nil, looks in the whole message.
4787 SUBJECT is a string of regexps separated by commas.
4788
4789 \(fn SUBJECT &optional WHOLE-MESSAGE)" t nil)
4790
4791 (autoload 'rmail-summary-by-senders "rmailsum" "\
4792 Display a summary of all messages whose \"From\" field matches SENDERS.
4793 SENDERS is a string of regexps separated by commas.
4794
4795 \(fn SENDERS)" t nil)
4796
4797 ;;;***
4798 \f
4799 ;;;### (autoloads (unforward-rmail-message undigestify-rmail-message)
4800 ;;;;;; "undigest" "undigest.el" "9b273a3e15b5496ab6121b585d8bd3b3")
4801 ;;; Generated autoloads from undigest.el
4802
4803 (autoload 'undigestify-rmail-message "undigest" "\
4804 Break up a digest message into its constituent messages.
4805 Leaves original message, deleted, before the undigestified messages.
4806
4807 \(fn)" t nil)
4808
4809 (autoload 'unforward-rmail-message "undigest" "\
4810 Extract a forwarded message from the containing message.
4811 This puts the forwarded message into a separate rmail message following
4812 the containing message. This command is only useful when messages are
4813 forwarded with `rmail-enable-mime-composing' set to nil.
4814
4815 \(fn)" t nil)
4816
4817 ;;;***
4818 \f
4819 ;;; End of automatically extracted autoloads.
4820
4821
4822 (provide 'rmail)
4823
4824 ;;; rmail.el ends here