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