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