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