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