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