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