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