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