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