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