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