]> code.delx.au - gnu-emacs/blob - lisp/mail/rmail.el
(rmail-movemail-flags): Fix customize type.
[gnu-emacs] / lisp / mail / rmail.el
1 ;;; rmail.el --- main code of "RMAIL" mail reader for Emacs.
2
3 ;; Copyright (C) 1985,86,87,88,93,94,95,96,97 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: mail
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 ;; Souped up by shane@mit-ajax based on ideas of rlk@athena.mit.edu
28 ;; New features include attribute and keyword support, message
29 ;; selection by dispatch table, summary by attributes and keywords,
30 ;; expunging by dispatch table, sticky options for file commands.
31
32 ;; Extended by Bob Weiner of Motorola
33 ;; New features include: rmail and rmail-summary buffers remain
34 ;; synchronized and key bindings basically operate the same way in both
35 ;; buffers, summary by topic or by regular expression, rmail-reply-prefix
36 ;; variable, and a bury rmail buffer (wipe) command.
37 ;;
38
39 (require 'mail-utils)
40
41 ; These variables now declared in paths.el.
42 ;(defvar rmail-spool-directory "/usr/spool/mail/"
43 ; "This is the name of the directory used by the system mailer for\n\
44 ;delivering new mail. Its name should end with a slash.")
45 ;(defvar rmail-file-name
46 ; (expand-file-name "~/RMAIL")
47 ; "")
48
49 (defgroup rmail nil
50 "Mail reader for Emacs."
51 :group 'mail)
52
53 (defgroup rmail-retrieve nil
54 "Rmail retrieval options."
55 :prefix "rmail-"
56 :group 'rmail)
57
58 (defgroup rmail-files nil
59 "Rmail files."
60 :prefix "rmail-"
61 :group 'rmail)
62
63 (defgroup rmail-headers nil
64 "Rmail header options."
65 :prefix "rmail-"
66 :group 'rmail)
67
68 (defgroup rmail-reply nil
69 "Rmail reply options."
70 :prefix "rmail-"
71 :group 'rmail)
72
73 (defgroup rmail-summary nil
74 "Rmail summary options."
75 :prefix "rmail-"
76 :prefix "rmail-summary-"
77 :group 'rmail)
78
79 (defgroup rmail-output nil
80 "Output message to a file."
81 :prefix "rmail-output-"
82 :prefix "rmail-"
83 :group 'rmail)
84
85
86 (defcustom rmail-movemail-program nil
87 "If non-nil, name of program for fetching new mail."
88 :group 'rmail-retrieve
89 :type 'string)
90
91 (defcustom rmail-pop-password nil
92 "*Password to use when reading mail from a POP server, if required."
93 :type '(choice (string :tag "Password")
94 (const :tag "Not Required" nil))
95 :group 'rmail-retrieve)
96
97 (defcustom rmail-pop-password-required nil
98 "*Non-nil if a password is required when reading mail using POP."
99 :type 'boolean
100 :group 'rmail-retrieve)
101
102 (defcustom rmail-movemail-flags nil
103 "*List of flags to pass to movemail.
104 Most commonly used to specify `-g' to enable GSS-API authentication
105 or `-k' to enable Kerberos authentication."
106 :type '(repeat string)
107 :group 'rmail-retrieve)
108
109 (defvar rmail-pop-password-error "invalid usercode or password"
110 "Regular expression matching incorrect-password POP server error messages.
111 If you get an incorrect-password error that this expression does not match,
112 please report it with \\[report-emacs-bug].")
113
114 (defcustom rmail-preserve-inbox nil
115 "*Non-nil if incoming mail should be left in the user's inbox,
116 rather than deleted, after it is retrieved."
117 :type 'boolean
118 :group 'rmail-retrieve)
119
120 ;;;###autoload
121 (defcustom rmail-dont-reply-to-names nil "\
122 *A regexp specifying names to prune of reply to messages.
123 A value of nil means exclude your own name only."
124 :type '(choice regexp (const :tag "Your Name" nil))
125 :group 'rmail-reply)
126
127 ;;;###autoload
128 (defvar rmail-default-dont-reply-to-names "info-" "\
129 A regular expression specifying part of the value of the default value of
130 the variable `rmail-dont-reply-to-names', for when the user does not set
131 `rmail-dont-reply-to-names' explicitly. (The other part of the default
132 value is the user's name.)
133 It is useful to set this variable in the site customization file.")
134
135 ;;;###autoload
136 (defcustom rmail-ignored-headers "^via:\\|^mail-from:\\|^origin:\\|^references:\\|^status:\\|^received:\\|^x400-originator:\\|^x400-recipients:\\|^x400-received:\\|^x400-mts-identifier:\\|^x400-content-type:\\|^\\(resent-\\|\\)message-id:\\|^summary-line:\\|^resent-date:\\|^nntp-posting-host:\\|^path:\\|^x-char.*:\\|^x-face:\\|^x-mailer:\\|^delivered-to:\\|^lines:\\|^mime-version:\\|^content-transfer-encoding:"
137 "*Regexp to match header fields that Rmail should normally hide."
138 :type 'regexp
139 :group 'rmail-headers)
140
141 ;;;###autoload
142 (defcustom rmail-displayed-headers nil
143 "*Regexp to match Header fields that Rmail should display.
144 If nil, display all header fields except those matched by
145 `rmail-ignored-headers'."
146 :type '(choice regexp (const :tag "All"))
147 :group 'rmail-headers)
148
149 ;;;###autoload
150 (defcustom rmail-retry-ignored-headers nil "\
151 *Headers that should be stripped when retrying a failed message."
152 :type '(choice regexp (const nil :tag "None"))
153 :group 'rmail-headers)
154
155 ;;;###autoload
156 (defcustom rmail-highlighted-headers "^From:\\|^Subject:" "\
157 *Regexp to match Header fields that Rmail should normally highlight.
158 A value of nil means don't highlight.
159 See also `rmail-highlight-face'."
160 :type 'regexp
161 :group 'rmail-headers)
162
163 ;;;###autoload
164 (defcustom rmail-highlight-face nil "\
165 *Face used by Rmail for highlighting headers."
166 :type '(choice (const :tag "Default" nil)
167 face)
168 :group 'rmail-headers)
169
170 ;;;###autoload
171 (defcustom rmail-delete-after-output nil "\
172 *Non-nil means automatically delete a message that is copied to a file."
173 :type 'boolean
174 :group 'rmail-files)
175
176 ;;;###autoload
177 (defcustom rmail-primary-inbox-list nil "\
178 *List of files which are inboxes for user's primary mail file `~/RMAIL'.
179 `nil' means the default, which is (\"/usr/spool/mail/$USER\")
180 \(the name varies depending on the operating system,
181 and the value of the environment variable MAIL overrides it)."
182 ;; Don't use backquote here, because we don't want to need it
183 ;; at load time.
184 :type (list 'choice '(const :tag "Default" nil)
185 (list 'repeat ':value (or (getenv "MAIL")
186 (concat "/var/spool/mail/"
187 (getenv "USER")))
188 'file))
189 :group 'rmail-retrieve
190 :group 'rmail-files)
191
192 ;;;###autoload
193 (defcustom rmail-mail-new-frame nil
194 "*Non-nil means Rmail makes a new frame for composing outgoing mail."
195 :type 'boolean
196 :group 'rmail-reply)
197
198 ;;;###autoload
199 (defcustom rmail-secondary-file-directory "~/"
200 "*Directory for additional secondary Rmail files."
201 :type 'directory
202 :group 'rmail-files)
203 ;;;###autoload
204 (defcustom rmail-secondary-file-regexp "\\.xmail$"
205 "*Regexp for which files are secondary Rmail files."
206 :type 'regexp
207 :group 'rmail-files)
208
209 ;;;###autoload
210 (defvar rmail-mode-hook nil
211 "List of functions to call when Rmail is invoked.")
212
213 ;;;###autoload
214 (defvar rmail-get-new-mail-hook nil
215 "List of functions to call when Rmail has retrieved new mail.")
216
217 ;;;###autoload
218 (defvar rmail-show-message-hook nil
219 "List of functions to call when Rmail displays a message.")
220
221 ;;;###autoload
222 (defvar rmail-delete-message-hook nil
223 "List of functions to call when Rmail deletes a message.
224 When the hooks are called, the message has been marked deleted but is
225 still the current message in the Rmail buffer.")
226
227 ;; These may be altered by site-init.el to match the format of mmdf files
228 ;; delimiting used on a given host (delim1 and delim2 from the config
229 ;; files).
230
231 (defvar rmail-mmdf-delim1 "^\001\001\001\001\n"
232 "Regexp marking the start of an mmdf message")
233 (defvar rmail-mmdf-delim2 "^\001\001\001\001\n"
234 "Regexp marking the end of an mmdf message")
235
236 (defcustom rmail-message-filter nil
237 "If non-nil, a filter function for new messages in RMAIL.
238 Called with region narrowed to the message, including headers,
239 before obeying `rmail-ignored-headers'."
240 :group 'rmail-headers
241 :type 'function)
242
243 (defvar rmail-reply-prefix "Re: "
244 "String to prepend to Subject line when replying to a message.")
245
246 ;; Some mailers use "Re(2):" or "Re^2:" or "Re: Re:" or "Re[2]:".
247 ;; This pattern should catch all the common variants.
248 (defvar rmail-reply-regexp "\\`\\(Re\\(([0-9]+)\\|\\[[0-9]+\\]\\|\\^[0-9]+\\)?: *\\)*"
249 "Regexp to delete from Subject line before inserting `rmail-reply-prefix'.")
250
251 (defcustom rmail-display-summary nil
252 "*If non-nil, Rmail always displays the summary buffer."
253 :group 'rmail-summary
254 :type 'boolean)
255 \f
256 (defvar rmail-inbox-list nil)
257 (put 'rmail-inbox-list 'permanent-local t)
258
259 (defvar rmail-keywords nil)
260 (put 'rmail-keywords 'permanent-local t)
261
262 (defvar rmail-buffer nil
263 "The RMAIL buffer related to the current buffer.
264 In an RMAIL buffer, this holds the RMAIL buffer itself.
265 In a summary buffer, this holds the RMAIL buffer it is a summary for.")
266 (put 'rmail-buffer 'permanent-local t)
267
268 ;; Message counters and markers. Deleted flags.
269
270 (defvar rmail-current-message nil)
271 (put 'rmail-current-message 'permanent-local t)
272
273 (defvar rmail-total-messages nil)
274 (put 'rmail-total-messages 'permanent-local t)
275
276 (defvar rmail-message-vector nil)
277 (put 'rmail-message-vector 'permanent-local t)
278
279 (defvar rmail-deleted-vector nil)
280 (put 'rmail-deleted-vector 'permanent-local t)
281
282 (defvar rmail-msgref-vector nil
283 "In an Rmail buffer, a vector whose Nth element is a list (N).
284 When expunging renumbers messages, these lists are modified
285 by substituting the new message number into the existing list.")
286 (put 'rmail-msgref-vector 'permanent-local t)
287
288 (defvar rmail-overlay-list nil)
289 (put 'rmail-overlay-list 'permanent-local t)
290
291 ;; These are used by autoloaded rmail-summary.
292
293 (defvar rmail-summary-buffer nil)
294 (put 'rmail-summary-buffer 'permanent-local t)
295 (defvar rmail-summary-vector nil)
296 (put 'rmail-summary-vector 'permanent-local t)
297
298 (defvar rmail-view-buffer nil
299 "Buffer which holds RMAIL message for MIME displaying.")
300 (put 'rmail-view-buffer 'permanent-local t)
301 \f
302 ;; `Sticky' default variables.
303
304 ;; Last individual label specified to a or k.
305 (defvar rmail-last-label nil)
306 (put 'rmail-last-label 'permanent-local t)
307
308 ;; Last set of values specified to C-M-n, C-M-p, C-M-s or C-M-l.
309 (defvar rmail-last-multi-labels nil)
310
311 (defvar rmail-last-regexp nil)
312 (put 'rmail-last-regexp 'permanent-local t)
313
314 (defcustom rmail-default-file "~/xmail"
315 "*Default file name for \\[rmail-output]."
316 :type 'file
317 :group 'rmail-files)
318 (defcustom rmail-default-rmail-file "~/XMAIL"
319 "*Default file name for \\[rmail-output-to-rmail-file]."
320 :type 'file
321 :group 'rmail-files)
322
323 ;; Mule and MIME related variables.
324
325 ;;;###autoload
326 (defvar rmail-file-coding-system nil
327 "Coding system used in RMAIL file.
328
329 This is set to nil by default.")
330
331 ;;;###autoload
332 (defcustom rmail-enable-mime nil
333 "*If non-nil, RMAIL uses MIME feature.
334 If the value is t, RMAIL automatically shows MIME decoded message.
335 If the value is neither t nor nil, RMAIL does not show MIME decoded message
336 until a user explicitly requires it."
337 :type '(choice (const :tag "on" t)
338 (const :tag "off" nil)
339 (sexp :tag "when asked" :format "%t\n" ask))
340 :group 'rmail)
341
342 ;;;###autoload
343 (defvar rmail-show-mime-function nil
344 "Function to show MIME decoded message of RMAIL file.")
345
346 ;;;###autoload
347 (defvar rmail-mime-feature 'rmail-mime
348 "Feature to provide for using MIME feature in RMAIL.
349 When starting rmail, this feature is requrired if rmail-enable-mime
350 is non-nil.")
351
352 \f
353 ;;; Regexp matching the delimiter of messages in UNIX mail format
354 ;;; (UNIX From lines), minus the initial ^. Note that if you change
355 ;;; this expression, you must change the code in rmail-nuke-pinhead-header
356 ;;; that knows the exact ordering of the \\( \\) subexpressions.
357 (defvar rmail-unix-mail-delimiter
358 (let ((time-zone-regexp
359 (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
360 "\\|[-+]?[0-9][0-9][0-9][0-9]"
361 "\\|"
362 "\\) *")))
363 (concat
364 "From "
365
366 ;; Many things can happen to an RFC 822 mailbox before it is put into
367 ;; a `From' line. The leading phrase can be stripped, e.g.
368 ;; `Joe <@w.x:joe@y.z>' -> `<@w.x:joe@y.z>'. The <> can be stripped, e.g.
369 ;; `<@x.y:joe@y.z>' -> `@x.y:joe@y.z'. Everything starting with a CRLF
370 ;; can be removed, e.g.
371 ;; From: joe@y.z (Joe K
372 ;; User)
373 ;; can yield `From joe@y.z (Joe K Fri Mar 22 08:11:15 1996', and
374 ;; From: Joe User
375 ;; <joe@y.z>
376 ;; can yield `From Joe User Fri Mar 22 08:11:15 1996'.
377 ;; The mailbox can be removed or be replaced by white space, e.g.
378 ;; From: "Joe User"{space}{tab}
379 ;; <joe@y.z>
380 ;; can yield `From {space}{tab} Fri Mar 22 08:11:15 1996',
381 ;; where {space} and {tab} represent the Ascii space and tab characters.
382 ;; We want to match the results of any of these manglings.
383 ;; The following regexp rejects names whose first characters are
384 ;; obviously bogus, but after that anything goes.
385 "\\([^\0-\b\n-\r\^?].*\\)? "
386
387 ;; The time the message was sent.
388 "\\([^\0-\r \^?]+\\) +" ; day of the week
389 "\\([^\0-\r \^?]+\\) +" ; month
390 "\\([0-3]?[0-9]\\) +" ; day of month
391 "\\([0-2][0-9]:[0-5][0-9]\\(:[0-6][0-9]\\)?\\) *" ; time of day
392
393 ;; Perhaps a time zone, specified by an abbreviation, or by a
394 ;; numeric offset.
395 time-zone-regexp
396
397 ;; The year.
398 " \\([0-9][0-9]+\\) *"
399
400 ;; On some systems the time zone can appear after the year, too.
401 time-zone-regexp
402
403 ;; Old uucp cruft.
404 "\\(remote from .*\\)?"
405
406 "\n"))
407 nil)
408
409 (defvar rmail-font-lock-keywords
410 (eval-when-compile
411 (let* ((cite-chars "[>|}]")
412 (cite-prefix "A-Za-z")
413 (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
414 (list '("^\\(From\\|Sender\\):" . font-lock-function-name-face)
415 '("^Reply-To:.*$" . font-lock-function-name-face)
416 '("^Subject:" . font-lock-comment-face)
417 '("^\\(To\\|Apparently-To\\|Cc\\|Newsgroups\\):"
418 . font-lock-keyword-face)
419 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
420 `(,cite-chars
421 (,(concat "\\=[ \t]*"
422 "\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
423 "\\(" cite-chars "[ \t]*\\)\\)+"
424 "\\(.*\\)")
425 (beginning-of-line) (end-of-line)
426 (2 font-lock-constant-face nil t)
427 (4 font-lock-comment-face nil t)))
428 '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\|Date\\):.*$"
429 . font-lock-string-face))))
430 "Additional expressions to highlight in Rmail mode.")
431
432 ;; Perform BODY in the summary buffer
433 ;; in such a way that its cursor is properly updated in its own window.
434 (defmacro rmail-select-summary (&rest body)
435 (` (let ((total rmail-total-messages))
436 (if (rmail-summary-displayed)
437 (let ((window (selected-window)))
438 (save-excursion
439 (unwind-protect
440 (progn
441 (pop-to-buffer rmail-summary-buffer)
442 ;; rmail-total-messages is a buffer-local var
443 ;; in the rmail buffer.
444 ;; This way we make it available for the body
445 ;; even tho the rmail buffer is not current.
446 (let ((rmail-total-messages total))
447 (,@ body)))
448 (select-window window))))
449 (save-excursion
450 (set-buffer rmail-summary-buffer)
451 (let ((rmail-total-messages total))
452 (,@ body))))
453 (rmail-maybe-display-summary))))
454 \f
455 ;;;; *** Rmail Mode ***
456
457 (defvar rmail-enable-multibyte nil)
458
459 ;;;###autoload
460 (defun rmail (&optional file-name-arg)
461 "Read and edit incoming mail.
462 Moves messages into file named by `rmail-file-name' (a babyl format file)
463 and edits that file in RMAIL Mode.
464 Type \\[describe-mode] once editing that file, for a list of RMAIL commands.
465
466 May be called with file name as argument; then performs rmail editing on
467 that file, but does not copy any new mail into the file.
468 Interactively, if you supply a prefix argument, then you
469 have a chance to specify a file name with the minibuffer.
470
471 If `rmail-display-summary' is non-nil, make a summary for this RMAIL file."
472 (interactive (if current-prefix-arg
473 (list (read-file-name "Run rmail on RMAIL file: "))))
474 (if rmail-enable-mime
475 (condition-case err
476 (require rmail-mime-feature)
477 (error (message "Feature `%s' not provided" rmail-mime-feature)
478 (sit-for 1)
479 (setq rmail-enable-mime nil))))
480 (let* ((file-name (expand-file-name (or file-name-arg rmail-file-name)))
481 (existed (get-file-buffer file-name))
482 ;; This binding is necessary because we much decide if we
483 ;; need code conversion while the buffer is unibyte
484 ;; (i.e. enable-multibyte-characters is nil).
485 (rmail-enable-multibyte (default-value 'enable-multibyte-characters))
486 ;; Since the file may contain messages of different encodings
487 ;; at the tail (non-BYBYL part), we can't decode them at once
488 ;; on reading. So, at first, we read the file without text
489 ;; code conversion, then decode the messages one by one by
490 ;; rmail-decode-babyl-format or
491 ;; rmail-convert-to-babyl-format.
492 (coding-system-for-read (and rmail-enable-multibyte 'raw-text))
493 run-mail-hook msg-shown)
494 ;; Like find-file, but in the case where a buffer existed
495 ;; and the file was reverted, recompute the message-data.
496 (if (and existed (not (verify-visited-file-modtime existed)))
497 (progn
498 ;; Don't be confused by apparent local-variables spec
499 ;; in the last message in the RMAIL file.
500 (let ((enable-local-variables nil))
501 (find-file file-name))
502 (if (and (verify-visited-file-modtime existed)
503 (eq major-mode 'rmail-mode))
504 (progn (rmail-forget-messages)
505 (rmail-set-message-counters))))
506 (let ((enable-local-variables nil))
507 (find-file file-name)))
508 (if (eq major-mode 'rmail-edit-mode)
509 (error "Exit Rmail Edit mode before getting new mail"))
510 (if (and existed (> (buffer-size) 0))
511 ;; Buffer not new and not empty; ensure in proper mode, but that's all.
512 (or (eq major-mode 'rmail-mode)
513 (progn (rmail-mode-2)
514 (setq run-mail-hook t)))
515 (setq run-mail-hook t)
516 (rmail-mode-2)
517 ;; Convert all or part to Babyl file if possible.
518 (rmail-convert-file)
519 ;; As we have read a file by raw-text, the buffer is set to
520 ;; unibyte. We must make it multibyte if necessary.
521 (if (and rmail-enable-multibyte
522 (not enable-multibyte-characters))
523 (set-buffer-multibyte t))
524 (goto-char (point-max))
525 (if (null rmail-inbox-list)
526 (progn
527 (rmail-set-message-counters)
528 (rmail-show-message)
529 (setq msg-shown t))))
530 (unwind-protect
531 (or (and (null file-name-arg)
532 (rmail-get-new-mail))
533 (or msg-shown (rmail-show-message (rmail-first-unseen-message))))
534 (progn
535 (if rmail-display-summary (rmail-summary))
536 (rmail-construct-io-menu)
537 (if run-mail-hook
538 (run-hooks 'rmail-mode-hook))))))
539
540 ;; Given the value of MAILPATH, return a list of inbox file names.
541 ;; This is turned off because it is not clear that the user wants
542 ;; all these inboxes to feed into the primary rmail file.
543 ; (defun rmail-convert-mailpath (string)
544 ; (let (idx list)
545 ; (while (setq idx (string-match "[%:]" string))
546 ; (let ((this (substring string 0 idx)))
547 ; (setq string (substring string (1+ idx)))
548 ; (setq list (cons (if (string-match "%" this)
549 ; (substring this 0 (string-match "%" this))
550 ; this)
551 ; list))))
552 ; list))
553
554 ; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
555 ; will not cause emacs 18.55 problems.
556
557 (defun rmail-convert-file ()
558 (let (convert)
559 (widen)
560 (goto-char (point-min))
561 ;; If file doesn't start like a Babyl file,
562 ;; convert it to one, by adding a header and converting each message.
563 (cond ((looking-at "BABYL OPTIONS:"))
564 ((looking-at "Version: 5\n")
565 ;; Losing babyl file made by old version of Rmail.
566 ;; Just fix the babyl file header; don't make a new one,
567 ;; so we don't lose the Labels: file attribute, etc.
568 (let ((buffer-read-only nil))
569 (insert "BABYL OPTIONS: -*- rmail -*-\n")))
570 ((equal (point-min) (point-max))
571 ;; Empty RMAIL file. Just insert the header.
572 (rmail-insert-rmail-file-header))
573 (t
574 ;; Non-empty file in non-RMAIL format. Add header and convert.
575 (setq convert t)
576 (rmail-insert-rmail-file-header)))
577 ;; If file was not a Babyl file or if there are
578 ;; Unix format messages added at the end,
579 ;; convert file as necessary.
580 (if (or convert
581 (save-excursion
582 (goto-char (point-max))
583 (search-backward "\n\^_")
584 (forward-char 2)
585 (looking-at "\n*From ")))
586 (let ((buffer-read-only nil))
587 (message "Converting to Babyl format...")
588 ;; If file needs conversion, convert it all,
589 ;; except for the BABYL header.
590 ;; (rmail-convert-to-babyl-format would delete the header.)
591 (goto-char (point-min))
592 (search-forward "\n\^_" nil t)
593 (narrow-to-region (point) (point-max))
594 (rmail-convert-to-babyl-format)
595 (message "Converting to Babyl format...done"))
596 (if (and (not rmail-enable-mime)
597 rmail-enable-multibyte)
598 ;; We still have to decode BABYL part.
599 (rmail-decode-babyl-format)))))
600
601 ;;; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
602 ;;; will not cause emacs 18.55 problems.
603
604 (defun rmail-insert-rmail-file-header ()
605 (let ((buffer-read-only nil))
606 (insert "BABYL OPTIONS: -*- rmail -*-
607 Version: 5
608 Labels:
609 Note: This is the header of an rmail file.
610 Note: If you are seeing it in rmail,
611 Note: it means the file has no messages in it.\n\^_")))
612
613 ;; Decode Babyl formated part at the head of current buffer by
614 ;; rmail-file-coding-system, or if it is nil, do auto conversion.
615
616 (defun rmail-decode-babyl-format ()
617 (let ((modifiedp (buffer-modified-p))
618 (buffer-read-only nil)
619 from to)
620 (goto-char (point-min))
621 (search-forward "\n\^_" nil t) ; Skip BYBYL header.
622 (setq from (point))
623 (goto-char (point-max))
624 (search-backward "\n\^_" from 'mv)
625 (setq to (point))
626 (if (not (and rmail-file-coding-system
627 (coding-system-p rmail-file-coding-system)))
628 (setq rmail-file-coding-system (detect-coding-region from to t)))
629 (if (not (eq rmail-file-coding-system 'undecided))
630 (let ((count 1))
631 (goto-char from)
632 (while (search-forward "\n\^_" nil t)
633 (decode-coding-region from (1- (point)) rmail-file-coding-system)
634 (goto-char (point))
635 (setq from (point))
636 (if (= (% count 10) 0)
637 (message "Decoding messages...%d" count))
638 (setq count (1+ count)))
639 (message "Decoding messages...done")
640 (set-buffer-file-coding-system rmail-file-coding-system)
641 (set-buffer-modified-p modifiedp)))))
642
643 (defvar rmail-mode-map nil)
644 (if rmail-mode-map
645 nil
646 (setq rmail-mode-map (make-keymap))
647 (suppress-keymap rmail-mode-map)
648 (define-key rmail-mode-map "a" 'rmail-add-label)
649 (define-key rmail-mode-map "b" 'rmail-bury)
650 (define-key rmail-mode-map "c" 'rmail-continue)
651 (define-key rmail-mode-map "d" 'rmail-delete-forward)
652 (define-key rmail-mode-map "\C-d" 'rmail-delete-backward)
653 (define-key rmail-mode-map "e" 'rmail-edit-current-message)
654 (define-key rmail-mode-map "f" 'rmail-forward)
655 (define-key rmail-mode-map "g" 'rmail-get-new-mail)
656 (define-key rmail-mode-map "h" 'rmail-summary)
657 (define-key rmail-mode-map "i" 'rmail-input)
658 (define-key rmail-mode-map "j" 'rmail-show-message)
659 (define-key rmail-mode-map "k" 'rmail-kill-label)
660 (define-key rmail-mode-map "l" 'rmail-summary-by-labels)
661 (define-key rmail-mode-map "\e\C-h" 'rmail-summary)
662 (define-key rmail-mode-map "\e\C-l" 'rmail-summary-by-labels)
663 (define-key rmail-mode-map "\e\C-r" 'rmail-summary-by-recipients)
664 (define-key rmail-mode-map "\e\C-s" 'rmail-summary-by-regexp)
665 (define-key rmail-mode-map "\e\C-t" 'rmail-summary-by-topic)
666 (define-key rmail-mode-map "m" 'rmail-mail)
667 (define-key rmail-mode-map "\em" 'rmail-retry-failure)
668 (define-key rmail-mode-map "n" 'rmail-next-undeleted-message)
669 (define-key rmail-mode-map "\en" 'rmail-next-message)
670 (define-key rmail-mode-map "\e\C-n" 'rmail-next-labeled-message)
671 (define-key rmail-mode-map "o" 'rmail-output-to-rmail-file)
672 (define-key rmail-mode-map "\C-o" 'rmail-output)
673 (define-key rmail-mode-map "p" 'rmail-previous-undeleted-message)
674 (define-key rmail-mode-map "\ep" 'rmail-previous-message)
675 (define-key rmail-mode-map "\e\C-p" 'rmail-previous-labeled-message)
676 (define-key rmail-mode-map "q" 'rmail-quit)
677 (define-key rmail-mode-map "r" 'rmail-reply)
678 ;; I find I can't live without the default M-r command -- rms.
679 ;; (define-key rmail-mode-map "\er" 'rmail-search-backwards)
680 (define-key rmail-mode-map "s" 'rmail-expunge-and-save)
681 (define-key rmail-mode-map "\es" 'rmail-search)
682 (define-key rmail-mode-map "t" 'rmail-toggle-header)
683 (define-key rmail-mode-map "u" 'rmail-undelete-previous-message)
684 (define-key rmail-mode-map "w" 'rmail-output-body-to-file)
685 (define-key rmail-mode-map "x" 'rmail-expunge)
686 (define-key rmail-mode-map "." 'rmail-beginning-of-message)
687 (define-key rmail-mode-map "<" 'rmail-first-message)
688 (define-key rmail-mode-map ">" 'rmail-last-message)
689 (define-key rmail-mode-map " " 'scroll-up)
690 (define-key rmail-mode-map "\177" 'scroll-down)
691 (define-key rmail-mode-map "?" 'describe-mode)
692 (define-key rmail-mode-map "\C-c\C-s\C-d" 'rmail-sort-by-date)
693 (define-key rmail-mode-map "\C-c\C-s\C-s" 'rmail-sort-by-subject)
694 (define-key rmail-mode-map "\C-c\C-s\C-a" 'rmail-sort-by-author)
695 (define-key rmail-mode-map "\C-c\C-s\C-r" 'rmail-sort-by-recipient)
696 (define-key rmail-mode-map "\C-c\C-s\C-c" 'rmail-sort-by-correspondent)
697 (define-key rmail-mode-map "\C-c\C-s\C-l" 'rmail-sort-by-lines)
698 (define-key rmail-mode-map "\C-c\C-s\C-k" 'rmail-sort-by-keywords)
699 (define-key rmail-mode-map "\C-c\C-n" 'rmail-next-same-subject)
700 (define-key rmail-mode-map "\C-c\C-p" 'rmail-previous-same-subject)
701 )
702 \f
703 (define-key rmail-mode-map [menu-bar] (make-sparse-keymap))
704
705 (define-key rmail-mode-map [menu-bar classify]
706 (cons "Classify" (make-sparse-keymap "Classify")))
707
708 (define-key rmail-mode-map [menu-bar classify input-menu]
709 nil)
710
711 (define-key rmail-mode-map [menu-bar classify output-menu]
712 nil)
713
714 (define-key rmail-mode-map [menu-bar classify output-body]
715 '("Output body to file..." . rmail-output-body-to-file))
716
717 (define-key rmail-mode-map [menu-bar classify output-inbox]
718 '("Output (inbox)..." . rmail-output))
719
720 (define-key rmail-mode-map [menu-bar classify output]
721 '("Output (Rmail)..." . rmail-output-to-rmail-file))
722
723 (define-key rmail-mode-map [menu-bar classify kill-label]
724 '("Kill Label..." . rmail-kill-label))
725
726 (define-key rmail-mode-map [menu-bar classify add-label]
727 '("Add Label..." . rmail-add-label))
728
729 (define-key rmail-mode-map [menu-bar summary]
730 (cons "Summary" (make-sparse-keymap "Summary")))
731
732 (define-key rmail-mode-map [menu-bar summary senders]
733 '("By Senders..." . rmail-summary-by-senders))
734
735 (define-key rmail-mode-map [menu-bar summary labels]
736 '("By Labels..." . rmail-summary-by-labels))
737
738 (define-key rmail-mode-map [menu-bar summary recipients]
739 '("By Recipients..." . rmail-summary-by-recipients))
740
741 (define-key rmail-mode-map [menu-bar summary topic]
742 '("By Topic..." . rmail-summary-by-topic))
743
744 (define-key rmail-mode-map [menu-bar summary regexp]
745 '("By Regexp..." . rmail-summary-by-regexp))
746
747 (define-key rmail-mode-map [menu-bar summary all]
748 '("All" . rmail-summary))
749
750 (define-key rmail-mode-map [menu-bar mail]
751 (cons "Mail" (make-sparse-keymap "Mail")))
752
753 (define-key rmail-mode-map [menu-bar mail rmail-get-new-mail]
754 '("Get New Mail" . rmail-get-new-mail))
755
756 (define-key rmail-mode-map [menu-bar mail lambda]
757 '("----"))
758
759 (define-key rmail-mode-map [menu-bar mail continue]
760 '("Continue" . rmail-continue))
761
762 (define-key rmail-mode-map [menu-bar mail resend]
763 '("Re-send..." . rmail-resend))
764
765 (define-key rmail-mode-map [menu-bar mail forward]
766 '("Forward" . rmail-forward))
767
768 (define-key rmail-mode-map [menu-bar mail retry]
769 '("Retry" . rmail-retry-failure))
770
771 (define-key rmail-mode-map [menu-bar mail reply]
772 '("Reply" . rmail-reply))
773
774 (define-key rmail-mode-map [menu-bar mail mail]
775 '("Mail" . rmail-mail))
776
777 (define-key rmail-mode-map [menu-bar delete]
778 (cons "Delete" (make-sparse-keymap "Delete")))
779
780 (define-key rmail-mode-map [menu-bar delete expunge/save]
781 '("Expunge/Save" . rmail-expunge-and-save))
782
783 (define-key rmail-mode-map [menu-bar delete expunge]
784 '("Expunge" . rmail-expunge))
785
786 (define-key rmail-mode-map [menu-bar delete undelete]
787 '("Undelete" . rmail-undelete-previous-message))
788
789 (define-key rmail-mode-map [menu-bar delete delete]
790 '("Delete" . rmail-delete-forward))
791
792 (define-key rmail-mode-map [menu-bar move]
793 (cons "Move" (make-sparse-keymap "Move")))
794
795 (define-key rmail-mode-map [menu-bar move search-back]
796 '("Search Back..." . rmail-search-backwards))
797
798 (define-key rmail-mode-map [menu-bar move search]
799 '("Search..." . rmail-search))
800
801 (define-key rmail-mode-map [menu-bar move previous]
802 '("Previous Nondeleted" . rmail-previous-undeleted-message))
803
804 (define-key rmail-mode-map [menu-bar move next]
805 '("Next Nondeleted" . rmail-next-undeleted-message))
806
807 (define-key rmail-mode-map [menu-bar move last]
808 '("Last" . rmail-last-message))
809
810 (define-key rmail-mode-map [menu-bar move first]
811 '("First" . rmail-first-message))
812
813 (define-key rmail-mode-map [menu-bar move previous]
814 '("Previous" . rmail-previous-message))
815
816 (define-key rmail-mode-map [menu-bar move next]
817 '("Next" . rmail-next-message))
818 \f
819 ;; Rmail mode is suitable only for specially formatted data.
820 (put 'rmail-mode 'mode-class 'special)
821
822 (defun rmail-mode-kill-summary ()
823 (if rmail-summary-buffer (kill-buffer rmail-summary-buffer)))
824
825 ;;;###autoload
826 (defun rmail-mode ()
827 "Rmail Mode is used by \\<rmail-mode-map>\\[rmail] for editing Rmail files.
828 All normal editing commands are turned off.
829 Instead, these commands are available:
830
831 \\[rmail-beginning-of-message] Move point to front of this message (same as \\[beginning-of-buffer]).
832 \\[scroll-up] Scroll to next screen of this message.
833 \\[scroll-down] Scroll to previous screen of this message.
834 \\[rmail-next-undeleted-message] Move to Next non-deleted message.
835 \\[rmail-previous-undeleted-message] Move to Previous non-deleted message.
836 \\[rmail-next-message] Move to Next message whether deleted or not.
837 \\[rmail-previous-message] Move to Previous message whether deleted or not.
838 \\[rmail-first-message] Move to the first message in Rmail file.
839 \\[rmail-last-message] Move to the last message in Rmail file.
840 \\[rmail-show-message] Jump to message specified by numeric position in file.
841 \\[rmail-search] Search for string and show message it is found in.
842 \\[rmail-delete-forward] Delete this message, move to next nondeleted.
843 \\[rmail-delete-backward] Delete this message, move to previous nondeleted.
844 \\[rmail-undelete-previous-message] Undelete message. Tries current message, then earlier messages
845 till a deleted message is found.
846 \\[rmail-edit-current-message] Edit the current message. \\[rmail-cease-edit] to return to Rmail.
847 \\[rmail-expunge] Expunge deleted messages.
848 \\[rmail-expunge-and-save] Expunge and save the file.
849 \\[rmail-quit] Quit Rmail: expunge, save, then switch to another buffer.
850 \\[save-buffer] Save without expunging.
851 \\[rmail-get-new-mail] Move new mail from system spool directory into this file.
852 \\[rmail-mail] Mail a message (same as \\[mail-other-window]).
853 \\[rmail-continue] Continue composing outgoing message started before.
854 \\[rmail-reply] Reply to this message. Like \\[rmail-mail] but initializes some fields.
855 \\[rmail-retry-failure] Send this message again. Used on a mailer failure message.
856 \\[rmail-forward] Forward this message to another user.
857 \\[rmail-output-to-rmail-file] Output this message to an Rmail file (append it).
858 \\[rmail-output] Output this message to a Unix-format mail file (append it).
859 \\[rmail-output-body-to-file] Save message body to a file. Default filename comes from Subject line.
860 \\[rmail-input] Input Rmail file. Run Rmail on that file.
861 \\[rmail-add-label] Add label to message. It will be displayed in the mode line.
862 \\[rmail-kill-label] Kill label. Remove a label from current message.
863 \\[rmail-next-labeled-message] Move to Next message with specified label
864 (label defaults to last one specified).
865 Standard labels: filed, unseen, answered, forwarded, deleted.
866 Any other label is present only if you add it with \\[rmail-add-label].
867 \\[rmail-previous-labeled-message] Move to Previous message with specified label
868 \\[rmail-summary] Show headers buffer, with a one line summary of each message.
869 \\[rmail-summary-by-labels] Summarize only messages with particular label(s).
870 \\[rmail-summary-by-recipients] Summarize only messages with particular recipient(s).
871 \\[rmail-summary-by-regexp] Summarize only messages with particular regexp(s).
872 \\[rmail-summary-by-topic] Summarize only messages with subject line regexp(s).
873 \\[rmail-toggle-header] Toggle display of complete header."
874 (interactive)
875 (rmail-mode-2)
876 (rmail-set-message-counters)
877 (rmail-show-message rmail-total-messages)
878 (run-hooks 'rmail-mode-hook))
879
880 (defun rmail-mode-2 ()
881 (kill-all-local-variables)
882 (rmail-mode-1)
883 (rmail-perm-variables)
884 (rmail-variables))
885
886 (defun rmail-mode-1 ()
887 (setq major-mode 'rmail-mode)
888 (setq mode-name "RMAIL")
889 (setq buffer-read-only t)
890 ;; No need to auto save RMAIL files in normal circumstances
891 ;; because they contain no info except attribute changes
892 ;; and deletion of messages.
893 ;; The one exception is when messages are copied into an Rmail mode buffer.
894 ;; rmail-output-to-rmail-file enables auto save when you do that.
895 (setq buffer-auto-save-file-name nil)
896 (setq mode-line-modified "--")
897 (use-local-map rmail-mode-map)
898 (set-syntax-table text-mode-syntax-table)
899 (setq local-abbrev-table text-mode-abbrev-table))
900
901 ;; Set up the permanent locals associated with an Rmail file.
902 (defun rmail-perm-variables ()
903 (make-local-variable 'rmail-last-label)
904 (make-local-variable 'rmail-last-regexp)
905 (make-local-variable 'rmail-deleted-vector)
906 (make-local-variable 'rmail-buffer)
907 (setq rmail-buffer (current-buffer))
908 (make-local-variable 'rmail-view-buffer)
909 (setq rmail-view-buffer rmail-buffer)
910 (make-local-variable 'rmail-summary-buffer)
911 (make-local-variable 'rmail-summary-vector)
912 (make-local-variable 'rmail-current-message)
913 (make-local-variable 'rmail-total-messages)
914 (make-local-variable 'rmail-overlay-list)
915 (setq rmail-overlay-list nil)
916 (make-local-variable 'rmail-message-vector)
917 (make-local-variable 'rmail-msgref-vector)
918 (make-local-variable 'rmail-inbox-list)
919 (setq rmail-inbox-list (rmail-parse-file-inboxes))
920 ;; Provide default set of inboxes for primary mail file ~/RMAIL.
921 (and (null rmail-inbox-list)
922 (or (equal buffer-file-name (expand-file-name rmail-file-name))
923 (equal buffer-file-truename
924 (abbreviate-file-name (file-truename rmail-file-name))))
925 (setq rmail-inbox-list
926 (or rmail-primary-inbox-list
927 (list (or (getenv "MAIL")
928 (concat rmail-spool-directory
929 (user-login-name)))))))
930 (make-local-variable 'rmail-keywords)
931 ;; this gets generated as needed
932 (setq rmail-keywords nil))
933
934 ;; Set up the non-permanent locals associated with Rmail mode.
935 (defun rmail-variables ()
936 ;; Don't let a local variables list in a message cause confusion.
937 (make-local-variable 'enable-local-variables)
938 (setq enable-local-variables nil)
939 (make-local-variable 'revert-buffer-function)
940 (setq revert-buffer-function 'rmail-revert)
941 (make-local-variable 'font-lock-defaults)
942 (setq font-lock-defaults
943 '(rmail-font-lock-keywords t nil nil nil
944 (font-lock-maximum-size . nil)
945 (font-lock-fontify-buffer-function . rmail-fontify-buffer-function)
946 (font-lock-unfontify-buffer-function . rmail-unfontify-buffer-function)
947 (font-lock-inhibit-thing-lock . (lazy-lock-mode fast-lock-mode))))
948 (make-local-variable 'require-final-newline)
949 (setq require-final-newline nil)
950 (make-local-variable 'version-control)
951 (setq version-control 'never)
952 (make-local-variable 'kill-buffer-hook)
953 (add-hook 'kill-buffer-hook 'rmail-mode-kill-summary)
954 (make-local-variable 'file-precious-flag)
955 (setq file-precious-flag t))
956
957 ;; Handle M-x revert-buffer done in an rmail-mode buffer.
958 (defun rmail-revert (arg noconfirm)
959 (let ((revert-buffer-function (default-value 'revert-buffer-function)))
960 ;; Call our caller again, but this time it does the default thing.
961 (if (revert-buffer arg noconfirm)
962 ;; If the user said "yes", and we changed something,
963 ;; reparse the messages.
964 (progn
965 (rmail-convert-file)
966 (goto-char (point-max))
967 (rmail-mode)))))
968
969 ;; Return a list of files from this buffer's Mail: option.
970 ;; Does not assume that messages have been parsed.
971 ;; Just returns nil if buffer does not look like Babyl format.
972 (defun rmail-parse-file-inboxes ()
973 (save-excursion
974 (save-restriction
975 (widen)
976 (goto-char 1)
977 (cond ((looking-at "BABYL OPTIONS:")
978 (search-forward "\n\^_" nil 'move)
979 (narrow-to-region 1 (point))
980 (goto-char 1)
981 (if (search-forward "\nMail:" nil t)
982 (progn
983 (narrow-to-region (point) (progn (end-of-line) (point)))
984 (goto-char (point-min))
985 (mail-parse-comma-list))))))))
986
987 (defun rmail-expunge-and-save ()
988 "Expunge and save RMAIL file."
989 (interactive)
990 (rmail-expunge)
991 (save-buffer)
992 (if (rmail-summary-exists)
993 (rmail-select-summary (set-buffer-modified-p nil))))
994
995 (defun rmail-quit ()
996 "Quit out of RMAIL."
997 (interactive)
998 (rmail-expunge-and-save)
999 ;; Don't switch to the summary buffer even if it was recently visible.
1000 (when rmail-summary-buffer
1001 (replace-buffer-in-windows rmail-summary-buffer)
1002 (bury-buffer rmail-summary-buffer))
1003 (let ((obuf (current-buffer)))
1004 (quit-window)
1005 (replace-buffer-in-windows obuf)))
1006
1007 (defun rmail-bury ()
1008 "Bury current Rmail buffer and its summary buffer."
1009 (interactive)
1010 ;; This let var was called rmail-buffer, but that interfered
1011 ;; with the buffer-local var used in summary buffers.
1012 (let ((buffer-to-bury (current-buffer)))
1013 (if (rmail-summary-exists)
1014 (let (window)
1015 (while (setq window (get-buffer-window rmail-summary-buffer))
1016 (quit-window nil window))
1017 (bury-buffer rmail-summary-buffer)))
1018 (quit-window)))
1019
1020 (defun rmail-duplicate-message ()
1021 "Create a duplicated copy of the current message.
1022 The duplicate copy goes into the Rmail file just after the
1023 original copy."
1024 (interactive)
1025 (widen)
1026 (let ((buffer-read-only nil)
1027 (number rmail-current-message)
1028 (string (buffer-substring (rmail-msgbeg rmail-current-message)
1029 (rmail-msgend rmail-current-message))))
1030 (goto-char (rmail-msgend rmail-current-message))
1031 (insert string)
1032 (rmail-forget-messages)
1033 (rmail-show-message number)
1034 (message "Message duplicated")))
1035
1036 ;;;###autoload
1037 (defun rmail-input (filename)
1038 "Run Rmail on file FILENAME."
1039 (interactive "FRun rmail on RMAIL file: ")
1040 (rmail filename))
1041
1042
1043 ;; This used to scan subdirectories recursively, but someone pointed out
1044 ;; that if the user wants that, person can put all the files in one dir.
1045 ;; And the recursive scan was slow. So I took it out.
1046 ;; rms, Sep 1996.
1047 (defun rmail-find-all-files (start)
1048 "Return list of file in dir START that match `rmail-secondary-file-regexp'."
1049 (if (file-accessible-directory-p start)
1050 ;; Don't sort here.
1051 (let* ((case-fold-search t)
1052 (files (directory-files start t rmail-secondary-file-regexp)))
1053 ;; Sort here instead of in directory-files
1054 ;; because this list is usually much shorter.
1055 (sort files 'string<))))
1056
1057 (defun rmail-list-to-menu (menu-name l action &optional full-name)
1058 (let ((menu (make-sparse-keymap menu-name)))
1059 (mapcar
1060 (function (lambda (item)
1061 (let (command)
1062 (if (consp item)
1063 (progn
1064 (setq command
1065 (rmail-list-to-menu (car item) (cdr item)
1066 action
1067 (if full-name
1068 (concat full-name "/"
1069 (car item))
1070 (car item))))
1071 (setq name (car item)))
1072 (progn
1073 (setq name item)
1074 (setq command
1075 (list 'lambda () '(interactive)
1076 (list action
1077 (expand-file-name
1078 (if full-name
1079 (concat full-name "/" item)
1080 item)
1081 rmail-secondary-file-directory))))))
1082 (define-key menu (vector (intern name))
1083 (cons name command)))))
1084 (reverse l))
1085 menu))
1086
1087 ;; This command is always "disabled" when it appears in a menu.
1088 (put 'rmail-disable-menu 'menu-enable ''nil)
1089
1090 (defun rmail-construct-io-menu ()
1091 (let ((files (rmail-find-all-files rmail-secondary-file-directory)))
1092 (if files
1093 (progn
1094 (define-key rmail-mode-map [menu-bar classify input-menu]
1095 (cons "Input Rmail File"
1096 (rmail-list-to-menu "Input Rmail File"
1097 files
1098 'rmail-input)))
1099 (define-key rmail-mode-map [menu-bar classify output-menu]
1100 (cons "Output Rmail File"
1101 (rmail-list-to-menu "Output Rmail File"
1102 files
1103 'rmail-output-to-rmail-file))))
1104
1105 (define-key rmail-mode-map [menu-bar classify input-menu]
1106 '("Input Rmail File" . rmail-disable-menu))
1107 (define-key rmail-mode-map [menu-bar classify output-menu]
1108 '("Output Rmail File" . rmail-disable-menu)))))
1109
1110 \f
1111 ;;;; *** Rmail input ***
1112
1113 ;; RLK feature not added in this version:
1114 ;; argument specifies inbox file or files in various ways.
1115
1116 (defun rmail-get-new-mail (&optional file-name)
1117 "Move any new mail from this RMAIL file's inbox files.
1118 The inbox files can be specified with the file's Mail: option. The
1119 variable `rmail-primary-inbox-list' specifies the inboxes for your
1120 primary RMAIL file if it has no Mail: option. By default, this is
1121 your /usr/spool/mail/$USER.
1122
1123 You can also specify the file to get new mail from. In this case, the
1124 file of new mail is not changed or deleted. Noninteractively, you can
1125 pass the inbox file name as an argument. Interactively, a prefix
1126 argument causes us to read a file name and use that file as the inbox.
1127
1128 If the variable `rmail-preserve-inbox' is non-nil, new mail will
1129 always be left in inbox files rather than deleted.
1130
1131 This function runs `rmail-get-new-mail-hook' before saving the updated file.
1132 It returns t if it got any new messages."
1133 (interactive
1134 (list (if current-prefix-arg
1135 (read-file-name "Get new mail from file: "))))
1136 (run-hooks 'rmail-before-get-new-mail-hook)
1137 ;; If the disk file has been changed from under us,
1138 ;; revert to it before we get new mail.
1139 (or (verify-visited-file-modtime (current-buffer))
1140 (find-file (buffer-file-name)))
1141 (rmail-maybe-set-message-counters)
1142 (widen)
1143 ;; Get rid of all undo records for this buffer.
1144 (or (eq buffer-undo-list t)
1145 (setq buffer-undo-list nil))
1146 (let ((all-files (if file-name (list file-name)
1147 rmail-inbox-list))
1148 (rmail-enable-multibyte (default-value 'enable-multibyte-characters))
1149 found)
1150 (unwind-protect
1151 (progn
1152 (while all-files
1153 (let ((opoint (point))
1154 (new-messages 0)
1155 (delete-files ())
1156 ;; If buffer has not changed yet, and has not been saved yet,
1157 ;; don't replace the old backup file now.
1158 (make-backup-files (and make-backup-files (buffer-modified-p)))
1159 (buffer-read-only nil)
1160 ;; Don't make undo records for what we do in getting mail.
1161 (buffer-undo-list t)
1162 success
1163 ;; Files to insert this time around.
1164 files
1165 ;; Last names of those files.
1166 file-last-names)
1167 ;; Pull files off all-files onto files
1168 ;; as long as there is no name conflict.
1169 ;; A conflict happens when two inbox file names
1170 ;; have the same last component.
1171 (while (and all-files
1172 (not (member (file-name-nondirectory (car all-files))
1173 file-last-names)))
1174 (setq files (cons (car all-files) files)
1175 file-last-names
1176 (cons (file-name-nondirectory (car all-files)) files))
1177 (setq all-files (cdr all-files)))
1178 ;; Put them back in their original order.
1179 (setq files (nreverse files))
1180
1181 (goto-char (point-max))
1182 (skip-chars-backward " \t\n") ; just in case of brain damage
1183 (delete-region (point) (point-max)) ; caused by require-final-newline
1184 (save-excursion
1185 (save-restriction
1186 (narrow-to-region (point) (point))
1187 ;; Read in the contents of the inbox files,
1188 ;; renaming them as necessary,
1189 ;; and adding to the list of files to delete eventually.
1190 (if file-name
1191 (rmail-insert-inbox-text files nil)
1192 (setq delete-files (rmail-insert-inbox-text files t)))
1193 ;; Scan the new text and convert each message to babyl format.
1194 (goto-char (point-min))
1195 (unwind-protect
1196 (save-excursion
1197 (setq new-messages (rmail-convert-to-babyl-format)
1198 success t))
1199 ;; If we could not convert the file's inboxes,
1200 ;; rename the files we tried to read
1201 ;; so we won't over and over again.
1202 (if (and (not file-name) (not success))
1203 (let ((delfiles delete-files)
1204 (count 0))
1205 ;; Try to delete the garbage just inserted.
1206 (delete-region (point-min) (point-max))
1207 (while delfiles
1208 (while (file-exists-p (format "RMAILOSE.%d" count))
1209 (setq count (1+ count)))
1210 (rename-file (car delfiles)
1211 (format "RMAILOSE.%d" count))
1212 (setq delfiles (cdr delfiles))))))
1213 (or (zerop new-messages)
1214 (let (success)
1215 (widen)
1216 (search-backward "\n\^_" nil t)
1217 (narrow-to-region (point) (point-max))
1218 (goto-char (1+ (point-min)))
1219 (rmail-count-new-messages)
1220 (run-hooks 'rmail-get-new-mail-hook)
1221 (save-buffer)))
1222 ;; Delete the old files, now that babyl file is saved.
1223 (while delete-files
1224 (condition-case ()
1225 ;; First, try deleting.
1226 (condition-case ()
1227 (delete-file (car delete-files))
1228 (file-error
1229 ;; If we can't delete it, truncate it.
1230 (write-region (point) (point) (car delete-files))))
1231 (file-error nil))
1232 (setq delete-files (cdr delete-files)))))
1233 (if (= new-messages 0)
1234 (progn (goto-char opoint)
1235 (if (or file-name rmail-inbox-list)
1236 (message "(No new mail has arrived)")))
1237 (if (rmail-summary-exists)
1238 (rmail-select-summary
1239 (rmail-update-summary)))
1240 (message "%d new message%s read"
1241 new-messages (if (= 1 new-messages) "" "s"))
1242 ;; Move to the first new message
1243 ;; unless we have other unseen messages before it.
1244 (rmail-show-message (rmail-first-unseen-message))
1245 (run-hooks 'rmail-after-get-new-mail-hook)
1246 (setq found t))))
1247 found)
1248 ;; Don't leave the buffer screwed up if we get a disk-full error.
1249 (or found (rmail-show-message)))))
1250
1251 (defun rmail-insert-inbox-text (files renamep)
1252 ;; Detect a locked file now, so that we avoid moving mail
1253 ;; out of the real inbox file. (That could scare people.)
1254 (or (memq (file-locked-p buffer-file-name) '(nil t))
1255 (error "RMAIL file %s is locked"
1256 (file-name-nondirectory buffer-file-name)))
1257 (let (file tofile delete-files movemail popmail got-password)
1258 (while files
1259 (setq file (file-truename
1260 (expand-file-name (substitute-in-file-name (car files))))
1261 tofile (expand-file-name
1262 ;; Generate name to move to from inbox name,
1263 ;; in case of multiple inboxes that need moving.
1264 (concat ".newmail-" (file-name-nondirectory file))
1265 ;; Use the directory of this rmail file
1266 ;; because it's a nuisance to use the homedir
1267 ;; if that is on a full disk and this rmail
1268 ;; file isn't.
1269 (file-name-directory
1270 (expand-file-name buffer-file-name))))
1271 ;; Always use movemail to rename the file,
1272 ;; since there can be mailboxes in various directories.
1273 (setq movemail t)
1274 ;;; ;; If getting from mail spool directory,
1275 ;;; ;; use movemail to move rather than just renaming,
1276 ;;; ;; so as to interlock with the mailer.
1277 ;;; (setq movemail (string= file
1278 ;;; (file-truename
1279 ;;; (concat rmail-spool-directory
1280 ;;; (file-name-nondirectory file)))))
1281 (setq popmail (string-match "^po:" (file-name-nondirectory file)))
1282 (if popmail (setq file (file-name-nondirectory file)
1283 renamep t))
1284 (if movemail
1285 (progn
1286 ;; On some systems, /usr/spool/mail/foo is a directory
1287 ;; and the actual inbox is /usr/spool/mail/foo/foo.
1288 (if (file-directory-p file)
1289 (setq file (expand-file-name (user-login-name)
1290 file)))))
1291 (cond (popmail
1292 (if (and rmail-pop-password-required (not rmail-pop-password))
1293 (setq rmail-pop-password
1294 (rmail-read-passwd
1295 (format "Password for %s: "
1296 (substring file (+ popmail 3))))
1297 got-password t))
1298 (if (eq system-type 'windows-nt)
1299 ;; cannot have "po:" in file name
1300 (setq tofile
1301 (expand-file-name
1302 (concat ".newmail-pop-" (substring file (+ popmail 3)))
1303 (file-name-directory
1304 (expand-file-name buffer-file-name)))))
1305 (message "Getting mail from post office ..."))
1306 ((and (file-exists-p tofile)
1307 (/= 0 (nth 7 (file-attributes tofile))))
1308 (message "Getting mail from %s..." tofile))
1309 ((and (file-exists-p file)
1310 (/= 0 (nth 7 (file-attributes file))))
1311 (message "Getting mail from %s..." file)))
1312 ;; Set TOFILE if have not already done so, and
1313 ;; rename or copy the file FILE to TOFILE if and as appropriate.
1314 (cond ((not renamep)
1315 (setq tofile file))
1316 ((or (file-exists-p tofile) (and (not popmail)
1317 (not (file-exists-p file))))
1318 nil)
1319 ((and (not movemail) (not popmail))
1320 ;; Try copying. If that fails (perhaps no space) and
1321 ;; we're allowed to blow away the inbox, rename instead.
1322 (if rmail-preserve-inbox
1323 (copy-file file tofile nil)
1324 (condition-case nil
1325 (copy-file file tofile nil)
1326 (error
1327 ;; Third arg is t so we can replace existing file TOFILE.
1328 (rename-file file tofile t))))
1329 ;; Make the real inbox file empty.
1330 ;; Leaving it deleted could cause lossage
1331 ;; because mailers often won't create the file.
1332 (if (not rmail-preserve-inbox)
1333 (condition-case ()
1334 (write-region (point) (point) file)
1335 (file-error nil))))
1336 (t
1337 (let ((errors nil))
1338 (unwind-protect
1339 (save-excursion
1340 (setq errors (generate-new-buffer " *rmail loss*"))
1341 (buffer-disable-undo errors)
1342 (let ((args
1343 (append
1344 (list (or rmail-movemail-program
1345 (expand-file-name "movemail"
1346 exec-directory))
1347 nil errors nil)
1348 (if rmail-preserve-inbox
1349 (list "-p")
1350 nil)
1351 rmail-movemail-flags
1352 (list file tofile)
1353 (if rmail-pop-password
1354 (list rmail-pop-password)
1355 nil))))
1356 (apply 'call-process args))
1357 (if (not (buffer-modified-p errors))
1358 ;; No output => movemail won
1359 nil
1360 (set-buffer errors)
1361 (subst-char-in-region (point-min) (point-max)
1362 ?\n ?\ )
1363 (goto-char (point-max))
1364 (skip-chars-backward " \t")
1365 (delete-region (point) (point-max))
1366 (goto-char (point-min))
1367 (if (looking-at "movemail: ")
1368 (delete-region (point-min) (match-end 0)))
1369 (beep t)
1370 (message "movemail: %s"
1371 (buffer-substring (point-min)
1372 (point-max)))
1373 ;; If we just read the password, most likely it is
1374 ;; wrong. Otherwise, see if there is a specific
1375 ;; reason to think that the problem is a wrong passwd.
1376 (if (or got-password
1377 (re-search-forward rmail-pop-password-error
1378 nil t))
1379 (setq rmail-pop-password nil))
1380 (sit-for 3)
1381 nil))
1382 (if errors (kill-buffer errors))))))
1383 ;; At this point, TOFILE contains the name to read:
1384 ;; Either the alternate name (if we renamed)
1385 ;; or the actual inbox (if not renaming).
1386 (if (file-exists-p tofile)
1387 (let ((coding-system-for-read 'no-conversion)
1388 size)
1389 (goto-char (point-max))
1390 (setq size (nth 1 (insert-file-contents tofile)))
1391 (goto-char (point-max))
1392 (or (= (preceding-char) ?\n)
1393 (zerop size)
1394 (insert ?\n))
1395 (if (not (and rmail-preserve-inbox (string= file tofile)))
1396 (setq delete-files (cons tofile delete-files)))))
1397 (message "")
1398 (setq files (cdr files)))
1399 delete-files))
1400
1401 (defun rmail-read-passwd (prompt &optional default)
1402 "Read a password, echoing `.' for each character typed.
1403 End with RET, LFD, or ESC. DEL or C-h rubs out. C-u kills line.
1404 Optional DEFAULT is password to start with."
1405 (let ((pass (if default default ""))
1406 (c 0)
1407 (echo-keystrokes 0)
1408 (cursor-in-echo-area t))
1409 (while (progn (message "%s%s"
1410 prompt
1411 (make-string (length pass) ?.))
1412 (setq c (read-char))
1413 (and (/= c ?\r) (/= c ?\n) (/= c ?\e)))
1414 (if (= c ?\C-u)
1415 (setq pass "")
1416 (if (and (/= c ?\b) (/= c ?\177))
1417 (setq pass (concat pass (char-to-string c)))
1418 (if (> (length pass) 0)
1419 (setq pass (substring pass 0 -1))))))
1420 (message "")
1421 (message nil)
1422 pass))
1423
1424 ;; the rmail-break-forwarded-messages feature is not implemented
1425 (defun rmail-convert-to-babyl-format ()
1426 (let ((count 0) start
1427 (case-fold-search nil)
1428 (invalid-input-resync
1429 (function (lambda ()
1430 (message "Invalid Babyl format in inbox!")
1431 (sit-for 3)
1432 ;; Try to get back in sync with a real message.
1433 (if (re-search-forward
1434 (concat rmail-mmdf-delim1 "\\|^From") nil t)
1435 (beginning-of-line)
1436 (goto-char (point-max)))))))
1437 (goto-char (point-min))
1438 (save-restriction
1439 (while (not (eobp))
1440 (setq start (point))
1441 (cond ((looking-at "BABYL OPTIONS:");Babyl header
1442 (if (search-forward "\n\^_" nil t)
1443 ;; If we find the proper terminator, delete through there.
1444 (delete-region (point-min) (point))
1445 (funcall invalid-input-resync)
1446 (delete-region (point-min) (point))))
1447 ;; Babyl format message
1448 ((looking-at "\^L")
1449 (or (search-forward "\n\^_" nil t)
1450 (funcall invalid-input-resync))
1451 (setq count (1+ count))
1452 ;; Make sure there is no extra white space after the ^_
1453 ;; at the end of the message.
1454 ;; Narrowing will make sure that whatever follows the junk
1455 ;; will be treated properly.
1456 (delete-region (point)
1457 (save-excursion
1458 (skip-chars-forward " \t\n")
1459 (point)))
1460 (or rmail-enable-mime
1461 (not rmail-enable-multibyte)
1462 (decode-coding-region start (point)
1463 (or rmail-file-coding-system
1464 'undecided)))
1465 (narrow-to-region (point) (point-max)))
1466 ;;*** MMDF format
1467 ((let ((case-fold-search t))
1468 (looking-at rmail-mmdf-delim1))
1469 (let ((case-fold-search t))
1470 (replace-match "\^L\n0, unseen,,\n*** EOOH ***\n")
1471 (re-search-forward rmail-mmdf-delim2 nil t)
1472 (replace-match "\^_"))
1473 (save-excursion
1474 (save-restriction
1475 (narrow-to-region start (1- (point)))
1476 (goto-char (point-min))
1477 (while (search-forward "\n\^_" nil t); single char "\^_"
1478 (replace-match "\n^_")))); 2 chars: "^" and "_"
1479 (or rmail-enable-mime
1480 (not rmail-enable-multibyte)
1481 (decode-coding-region start (point) 'undecided))
1482 (narrow-to-region (point) (point-max))
1483 (setq count (1+ count)))
1484 ;;*** Mail format
1485 ((looking-at "^From ")
1486 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
1487 (rmail-nuke-pinhead-header)
1488 ;; If this message has a Content-Length field,
1489 ;; skip to the end of the contents.
1490 (let* ((header-end (save-excursion
1491 (and (re-search-forward "\n\n" nil t)
1492 (1- (point)))))
1493 (case-fold-search t)
1494 (quoted-printable-header-field-end
1495 (save-excursion
1496 (re-search-forward
1497 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
1498 header-end t)))
1499 (size
1500 ;; Get the numeric value from the Content-Length field.
1501 (save-excursion
1502 ;; Back up to end of prev line,
1503 ;; in case the Content-Length field comes first.
1504 (forward-char -1)
1505 (and (search-forward "\ncontent-length: "
1506 header-end t)
1507 (let ((beg (point))
1508 (eol (progn (end-of-line) (point))))
1509 (string-to-int (buffer-substring beg eol)))))))
1510 (and size
1511 (if (and (natnump size)
1512 (<= (+ header-end size) (point-max))
1513 ;; Make sure this would put us at a position
1514 ;; that we could continue from.
1515 (save-excursion
1516 (goto-char (+ header-end size))
1517 (skip-chars-forward "\n")
1518 (or (eobp)
1519 (and (looking-at "BABYL OPTIONS:")
1520 (search-forward "\n\^_" nil t))
1521 (and (looking-at "\^L")
1522 (search-forward "\n\^_" nil t))
1523 (let ((case-fold-search t))
1524 (looking-at rmail-mmdf-delim1))
1525 (looking-at "From "))))
1526 (goto-char (+ header-end size))
1527 (message "Ignoring invalid Content-Length field")
1528 (sit-for 1 0 t)))
1529 (if (re-search-forward
1530 (concat "^[\^_]?\\("
1531 rmail-unix-mail-delimiter
1532 "\\|"
1533 rmail-mmdf-delim1 "\\|"
1534 "^BABYL OPTIONS:\\|"
1535 "\^L\n[01],\\)") nil t)
1536 (goto-char (match-beginning 1))
1537 (goto-char (point-max)))
1538 (setq count (1+ count))
1539 (if quoted-printable-header-field-end
1540 (save-excursion
1541 (rmail-decode-quoted-printable header-end (point))
1542 ;; Change "quoted-printable" to "8bit",
1543 ;; to reflect the decoding we just did.
1544 (goto-char quoted-printable-header-field-end)
1545 (delete-region (point) (search-backward ":"))
1546 (insert ": 8bit"))))
1547
1548 (save-excursion
1549 (save-restriction
1550 (narrow-to-region start (point))
1551 (goto-char (point-min))
1552 (while (search-forward "\n\^_" nil t); single char
1553 (replace-match "\n^_")))); 2 chars: "^" and "_"
1554 (insert ?\^_)
1555 (or rmail-enable-mime
1556 (not rmail-enable-multibyte)
1557 (decode-coding-region start (point) 'undecided))
1558 (narrow-to-region (point) (point-max)))
1559 ;;
1560 ;; This kludge is because some versions of sendmail.el
1561 ;; insert an extra newline at the beginning that shouldn't
1562 ;; be there. sendmail.el has been fixed, but old versions
1563 ;; may still be in use. -- rms, 7 May 1993.
1564 ((eolp) (delete-char 1))
1565 (t (error "Cannot convert to babyl format")))))
1566 count))
1567
1568 (defun rmail-hex-char-to-integer (character)
1569 "Return CHARACTER's value interpreted as a hex digit."
1570 (if (and (>= character ?0) (<= character ?9))
1571 (- character ?0)
1572 (let ((ch (logior character 32)))
1573 (if (and (>= ch ?a) (<= ch ?f))
1574 (- ch (- ?a 10))
1575 (error "Invalid hex digit `%c'" ch)))))
1576
1577 (defun rmail-hex-string-to-integer (hex-string)
1578 "Return decimal integer for HEX-STRING."
1579 (let ((hex-num 0)
1580 (index 0))
1581 (while (< index (length hex-string))
1582 (setq hex-num (+ (* hex-num 16)
1583 (rmail-hex-char-to-integer (aref hex-string index))))
1584 (setq index (1+ index)))
1585 hex-num))
1586
1587 (defun rmail-decode-quoted-printable (from to)
1588 "Decode Quoted-Printable in the region between FROM and TO."
1589 (interactive "r")
1590 (goto-char from)
1591 (or (markerp to)
1592 (setq to (copy-marker to)))
1593 (while (search-forward "=" to t)
1594 (cond ((eq (following-char) ?\n)
1595 (delete-char -1)
1596 (delete-char 1))
1597 ((looking-at "[0-9A-F][0-9A-F]")
1598 (subst-char-in-region
1599 (1- (point)) (point) ?=
1600 (rmail-hex-string-to-integer
1601 (buffer-substring (point) (+ 2 (point)))))
1602 (delete-char 2))
1603 ((looking-at "=")
1604 (delete-char 1))
1605 (t
1606 (message "Malformed MIME quoted-printable message")))))
1607
1608 ;; Delete the "From ..." line, creating various other headers with
1609 ;; information from it if they don't already exist. Now puts the
1610 ;; original line into a mail-from: header line for debugging and for
1611 ;; use by the rmail-output function.
1612 (defun rmail-nuke-pinhead-header ()
1613 (save-excursion
1614 (save-restriction
1615 (let ((start (point))
1616 (end (progn
1617 (condition-case ()
1618 (search-forward "\n\n")
1619 (error
1620 (goto-char (point-max))
1621 (insert "\n\n")))
1622 (point)))
1623 has-from has-date)
1624 (narrow-to-region start end)
1625 (let ((case-fold-search t))
1626 (goto-char start)
1627 (setq has-from (search-forward "\nFrom:" nil t))
1628 (goto-char start)
1629 (setq has-date (and (search-forward "\nDate:" nil t) (point)))
1630 (goto-char start))
1631 (let ((case-fold-search nil))
1632 (if (re-search-forward (concat "^" rmail-unix-mail-delimiter) nil t)
1633 (replace-match
1634 (concat
1635 "Mail-from: \\&"
1636 ;; Keep and reformat the date if we don't
1637 ;; have a Date: field.
1638 (if has-date
1639 ""
1640 (concat
1641 "Date: \\2, \\4 \\3 \\9 \\5 "
1642
1643 ;; The timezone could be matched by group 7 or group 10.
1644 ;; If neither of them matched, assume EST, since only
1645 ;; Easterners would be so sloppy.
1646 ;; It's a shame the substitution can't use "\\10".
1647 (cond
1648 ((/= (match-beginning 7) (match-end 7)) "\\7")
1649 ((/= (match-beginning 10) (match-end 10))
1650 (buffer-substring (match-beginning 10)
1651 (match-end 10)))
1652 (t "EST"))
1653 "\n"))
1654 ;; Keep and reformat the sender if we don't
1655 ;; have a From: field.
1656 (if has-from
1657 ""
1658 "From: \\1\n"))
1659 t)))))))
1660 \f
1661 ;;;; *** Rmail Message Formatting and Header Manipulation ***
1662
1663 (defun rmail-reformat-message (beg end)
1664 (goto-char beg)
1665 (forward-line 1)
1666 (if (/= (following-char) ?0)
1667 (error "Bad format in RMAIL file."))
1668 (let ((buffer-read-only nil)
1669 (delta (- (buffer-size) end)))
1670 (delete-char 1)
1671 (insert ?1)
1672 (forward-line 1)
1673 (let ((case-fold-search t))
1674 (while (looking-at "Summary-line:\\|Mail-From:")
1675 (forward-line 1)))
1676 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*\n")
1677 (delete-region (point)
1678 (progn (forward-line 1) (point))))
1679 (let ((str (buffer-substring (point)
1680 (save-excursion (search-forward "\n\n" end 'move)
1681 (point)))))
1682 (insert str "*** EOOH ***\n")
1683 (narrow-to-region (point) (- (buffer-size) delta)))
1684 (goto-char (point-min))
1685 (if rmail-message-filter (funcall rmail-message-filter))
1686 (if (or rmail-displayed-headers rmail-ignored-headers)
1687 (rmail-clear-headers))))
1688
1689 (defun rmail-clear-headers (&optional ignored-headers)
1690 "Delete all header fields that Rmail should not show.
1691 If the optional argument IGNORED-HEADERS is non-nil,
1692 delete all header fields whose names match that regexp.
1693 Otherwise, if `rmail-displayed-headers' is non-nil,
1694 delete all header fields *except* those whose names match that regexp.
1695 Otherwise, delete all header fields whose names match `rmail-ignored-headers'."
1696 (when (search-forward "\n\n" nil t)
1697 (forward-char -1)
1698 (let ((case-fold-search t)
1699 (buffer-read-only nil))
1700 (if (and rmail-displayed-headers (null ignored-headers))
1701 (save-restriction
1702 (narrow-to-region (point-min) (point))
1703 (let (lim next)
1704 (goto-char (point-min))
1705 (while (and (not (eobp))
1706 (save-excursion
1707 (if (re-search-forward "\n[^ \t]" nil t)
1708 (setq lim (match-beginning 0)
1709 next (1+ lim))
1710 (setq lim nil next (point-max)))))
1711 (if (save-excursion
1712 (re-search-forward rmail-displayed-headers lim t))
1713 (goto-char next)
1714 (delete-region (point) next))))
1715 (goto-char (point-min)))
1716 (or ignored-headers (setq ignored-headers rmail-ignored-headers))
1717 (save-restriction
1718 (narrow-to-region (point-min) (point))
1719 (while (progn
1720 (goto-char (point-min))
1721 (re-search-forward ignored-headers nil t))
1722 (beginning-of-line)
1723 (delete-region (point)
1724 (if (re-search-forward "\n[^ \t]" nil t)
1725 (1- (point))
1726 (point-max)))))))))
1727
1728 (defun rmail-msg-is-pruned ()
1729 (rmail-maybe-set-message-counters)
1730 (save-restriction
1731 (save-excursion
1732 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
1733 (goto-char (point-min))
1734 (forward-line 1)
1735 (= (following-char) ?1))))
1736
1737 (defun rmail-toggle-header (&optional arg)
1738 "Show original message header if pruned header currently shown, or vice versa.
1739 With argument ARG, show the message header pruned if ARG is greater than zero;
1740 otherwise, show it in full."
1741 (interactive "P")
1742 (let* ((buffer-read-only nil)
1743 (pruned (rmail-msg-is-pruned))
1744 (prune (if arg
1745 (> (prefix-numeric-value arg) 0)
1746 (not pruned))))
1747 (if (eq pruned prune)
1748 t
1749 (rmail-maybe-set-message-counters)
1750 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
1751 (if pruned
1752 (progn (goto-char (point-min))
1753 (forward-line 1)
1754 (delete-char 1)
1755 (insert ?0)
1756 (forward-line 1)
1757 (let ((case-fold-search t))
1758 (while (looking-at "Summary-Line:\\|Mail-From:")
1759 (forward-line 1)))
1760 (insert "*** EOOH ***\n")
1761 (forward-char -1)
1762 (search-forward "\n*** EOOH ***\n")
1763 (forward-line -1)
1764 (let ((temp (point)))
1765 (and (search-forward "\n\n" nil t)
1766 (delete-region temp (point))))
1767 (goto-char (point-min))
1768 (search-forward "\n*** EOOH ***\n")
1769 (narrow-to-region (point) (point-max)))
1770 (rmail-reformat-message (point-min) (point-max)))
1771 (rmail-highlight-headers))))
1772 \f
1773 ;;;; *** Rmail Attributes and Keywords ***
1774
1775 ;; Make a string describing current message's attributes and keywords
1776 ;; and set it up as the name of a minor mode
1777 ;; so it will appear in the mode line.
1778 (defun rmail-display-labels ()
1779 (let ((blurb "") (beg (point-min-marker)) (end (point-max-marker)))
1780 (save-excursion
1781 (unwind-protect
1782 (progn
1783 (widen)
1784 (goto-char (rmail-msgbeg rmail-current-message))
1785 (forward-line 1)
1786 (if (looking-at "[01],")
1787 (progn
1788 (narrow-to-region (point) (progn (end-of-line) (point)))
1789 ;; Truly valid BABYL format requires a space before each
1790 ;; attribute or keyword name. Put them in if missing.
1791 (let (buffer-read-only)
1792 (goto-char (point-min))
1793 (while (search-forward "," nil t)
1794 (or (looking-at "[ ,]") (eobp)
1795 (insert " "))))
1796 (goto-char (point-max))
1797 (if (search-backward ",," nil 'move)
1798 (progn
1799 (if (> (point) (1+ (point-min)))
1800 (setq blurb (buffer-substring (+ 1 (point-min)) (point))))
1801 (if (> (- (point-max) (point)) 2)
1802 (setq blurb
1803 (concat blurb
1804 ";"
1805 (buffer-substring (+ (point) 3)
1806 (1- (point-max)))))))))))
1807 ;; Note: we don't use save-restriction because that does not work right
1808 ;; if changes are made outside the saved restriction
1809 ;; before that restriction is restored.
1810 (narrow-to-region beg end)
1811 (set-marker beg nil)
1812 (set-marker end nil)))
1813 (while (string-match " +," blurb)
1814 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
1815 (substring blurb (match-end 0)))))
1816 (while (string-match ", +" blurb)
1817 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
1818 (substring blurb (match-end 0)))))
1819 (setq mode-line-process
1820 (format " %d/%d%s"
1821 rmail-current-message rmail-total-messages blurb))))
1822
1823 ;; Turn an attribute of a message on or off according to STATE.
1824 ;; ATTR is the name of the attribute, as a string.
1825 ;; MSGNUM is message number to change; nil means current message.
1826 (defun rmail-set-attribute (attr state &optional msgnum)
1827 (let ((omax (point-max-marker))
1828 (omin (point-min-marker))
1829 (buffer-read-only nil))
1830 (or msgnum (setq msgnum rmail-current-message))
1831 (if (> msgnum 0)
1832 (unwind-protect
1833 (save-excursion
1834 (widen)
1835 (goto-char (+ 3 (rmail-msgbeg msgnum)))
1836 (let ((curstate
1837 (not
1838 (null (search-backward (concat ", " attr ",")
1839 (prog1 (point) (end-of-line)) t)))))
1840 (or (eq curstate (not (not state)))
1841 (if curstate
1842 (delete-region (point) (1- (match-end 0)))
1843 (beginning-of-line)
1844 (forward-char 2)
1845 (insert " " attr ","))))
1846 (if (string= attr "deleted")
1847 (rmail-set-message-deleted-p msgnum state)))
1848 ;; Note: we don't use save-restriction because that does not work right
1849 ;; if changes are made outside the saved restriction
1850 ;; before that restriction is restored.
1851 (narrow-to-region omin omax)
1852 (set-marker omin nil)
1853 (set-marker omax nil)
1854 (if (= msgnum rmail-current-message)
1855 (rmail-display-labels))))))
1856
1857 ;; Return t if the attributes/keywords line of msg number MSG
1858 ;; contains a match for the regexp LABELS.
1859 (defun rmail-message-labels-p (msg labels)
1860 (save-excursion
1861 (save-restriction
1862 (widen)
1863 (goto-char (rmail-msgbeg msg))
1864 (forward-char 3)
1865 (re-search-backward labels (prog1 (point) (end-of-line)) t))))
1866 \f
1867 ;;;; *** Rmail Message Selection And Support ***
1868
1869 (defun rmail-msgend (n)
1870 (marker-position (aref rmail-message-vector (1+ n))))
1871
1872 (defun rmail-msgbeg (n)
1873 (marker-position (aref rmail-message-vector n)))
1874
1875 (defun rmail-widen-to-current-msgbeg (function)
1876 "Call FUNCTION with point at start of internal data of current message.
1877 Assumes that bounds were previously narrowed to display the message in Rmail.
1878 The bounds are widened enough to move point where desired, then narrowed
1879 again afterward.
1880
1881 FUNCTION may not change the visible text of the message, but it may
1882 change the invisible header text."
1883 (save-excursion
1884 (let ((obeg (- (point-max) (point-min))))
1885 (unwind-protect
1886 (progn
1887 (narrow-to-region (rmail-msgbeg rmail-current-message)
1888 (point-max))
1889 (goto-char (point-min))
1890 (funcall function))
1891 ;; Note: we don't use save-restriction because that does not work right
1892 ;; if changes are made outside the saved restriction
1893 ;; before that restriction is restored.
1894 ;; Here we assume that changes made by FUNCTION
1895 ;; occur before the visible region of the message.
1896 (narrow-to-region (- (point-max) obeg) (point-max))))))
1897
1898 (defun rmail-forget-messages ()
1899 (unwind-protect
1900 (if (vectorp rmail-message-vector)
1901 (let* ((i 0)
1902 (v rmail-message-vector)
1903 (n (length v)))
1904 (while (< i n)
1905 (move-marker (aref v i) nil)
1906 (setq i (1+ i)))))
1907 (setq rmail-message-vector nil)
1908 (setq rmail-msgref-vector nil)
1909 (setq rmail-deleted-vector nil)))
1910
1911 (defun rmail-maybe-set-message-counters ()
1912 (if (not (and rmail-deleted-vector
1913 rmail-message-vector
1914 rmail-current-message
1915 rmail-total-messages))
1916 (rmail-set-message-counters)))
1917
1918 (defun rmail-count-new-messages (&optional nomsg)
1919 (let* ((case-fold-search nil)
1920 (total-messages 0)
1921 (messages-head nil)
1922 (deleted-head nil))
1923 (or nomsg (message "Counting new messages..."))
1924 (goto-char (point-max))
1925 ;; Put at the end of messages-head
1926 ;; the entry for message N+1, which marks
1927 ;; the end of message N. (N = number of messages).
1928 (search-backward "\n\^_")
1929 (forward-char 1)
1930 (setq messages-head (list (point-marker)))
1931 (rmail-set-message-counters-counter (point-min))
1932 (setq rmail-current-message (1+ rmail-total-messages))
1933 (setq rmail-total-messages
1934 (+ rmail-total-messages total-messages))
1935 (setq rmail-message-vector
1936 (vconcat rmail-message-vector (cdr messages-head)))
1937 (aset rmail-message-vector
1938 rmail-current-message (car messages-head))
1939 (setq rmail-deleted-vector
1940 (concat rmail-deleted-vector deleted-head))
1941 (setq rmail-summary-vector
1942 (vconcat rmail-summary-vector (make-vector total-messages nil)))
1943 (setq rmail-msgref-vector
1944 (vconcat rmail-msgref-vector (make-vector total-messages nil)))
1945 ;; Fill in the new elements of rmail-msgref-vector.
1946 (let ((i (1+ (- rmail-total-messages total-messages))))
1947 (while (<= i rmail-total-messages)
1948 (aset rmail-msgref-vector i (list i))
1949 (setq i (1+ i))))
1950 (goto-char (point-min))
1951 (or nomsg (message "Counting new messages...done (%d)" total-messages))))
1952
1953 (defun rmail-set-message-counters ()
1954 (rmail-forget-messages)
1955 (save-excursion
1956 (save-restriction
1957 (widen)
1958 (let* ((point-save (point))
1959 (total-messages 0)
1960 (messages-after-point)
1961 (case-fold-search nil)
1962 (messages-head nil)
1963 (deleted-head nil))
1964 (message "Counting messages...")
1965 (goto-char (point-max))
1966 ;; Put at the end of messages-head
1967 ;; the entry for message N+1, which marks
1968 ;; the end of message N. (N = number of messages).
1969 (search-backward "\n\^_" nil t)
1970 (if (/= (point) (point-max)) (forward-char 1))
1971 (setq messages-head (list (point-marker)))
1972 (rmail-set-message-counters-counter (min (point) point-save))
1973 (setq messages-after-point total-messages)
1974 (rmail-set-message-counters-counter)
1975 (setq rmail-total-messages total-messages)
1976 (setq rmail-current-message
1977 (min total-messages
1978 (max 1 (- total-messages messages-after-point))))
1979 (setq rmail-message-vector
1980 (apply 'vector (cons (point-min-marker) messages-head))
1981 rmail-deleted-vector (concat "D" deleted-head)
1982 rmail-summary-vector (make-vector rmail-total-messages nil)
1983 rmail-msgref-vector (make-vector (1+ rmail-total-messages) nil))
1984 (let ((i 0))
1985 (while (<= i rmail-total-messages)
1986 (aset rmail-msgref-vector i (list i))
1987 (setq i (1+ i))))
1988 (message "Counting messages...done")))))
1989
1990 (defun rmail-set-message-counters-counter (&optional stop)
1991 (while (search-backward "\n\^_\^L\n" stop t)
1992 (forward-char 1)
1993 (setq messages-head (cons (point-marker) messages-head))
1994 (save-excursion
1995 (setq deleted-head
1996 (cons (if (search-backward ", deleted,"
1997 (prog1 (point)
1998 (forward-line 2))
1999 t)
2000 ?D ?\ )
2001 deleted-head)))
2002 (if (zerop (% (setq total-messages (1+ total-messages)) 20))
2003 (message "Counting messages...%d" total-messages))))
2004
2005 (defun rmail-beginning-of-message ()
2006 "Show current message starting from the beginning."
2007 (interactive)
2008 (rmail-show-message rmail-current-message))
2009
2010 (defun rmail-show-message (&optional n no-summary)
2011 "Show message number N (prefix argument), counting from start of file.
2012 If summary buffer is currently displayed, update current message there also."
2013 (interactive "p")
2014 (or (eq major-mode 'rmail-mode)
2015 (switch-to-buffer rmail-buffer))
2016 (rmail-maybe-set-message-counters)
2017 (widen)
2018 (if (zerop rmail-total-messages)
2019 (progn (narrow-to-region (point-min) (1- (point-max)))
2020 (goto-char (point-min))
2021 (setq mode-line-process nil))
2022 (let (blurb)
2023 (if (not n)
2024 (setq n rmail-current-message)
2025 (cond ((<= n 0)
2026 (setq n 1
2027 rmail-current-message 1
2028 blurb "No previous message"))
2029 ((> n rmail-total-messages)
2030 (setq n rmail-total-messages
2031 rmail-current-message rmail-total-messages
2032 blurb "No following message"))
2033 (t
2034 (setq rmail-current-message n))))
2035 (let ((beg (rmail-msgbeg n)))
2036 (goto-char beg)
2037 (forward-line 1)
2038 ;; Clear the "unseen" attribute when we show a message.
2039 (rmail-set-attribute "unseen" nil)
2040 ;; Reformat the header, or else find the reformatted header.
2041 (let ((end (rmail-msgend n)))
2042 (if (= (following-char) ?0)
2043 (rmail-reformat-message beg end)
2044 (search-forward "\n*** EOOH ***\n" end t)
2045 (narrow-to-region (point) end)))
2046 (goto-char (point-min))
2047 (rmail-display-labels)
2048 (if (eq rmail-enable-mime t)
2049 (funcall rmail-show-mime-function)
2050 (setq rmail-view-buffer rmail-buffer)
2051 )
2052 (rmail-highlight-headers)
2053 (if transient-mark-mode (deactivate-mark))
2054 (run-hooks 'rmail-show-message-hook)
2055 ;; If there is a summary buffer, try to move to this message
2056 ;; in that buffer. But don't complain if this message
2057 ;; is not mentioned in the summary.
2058 ;; Don't do this at all if we were called on behalf
2059 ;; of cursor motion in the summary buffer.
2060 (and (rmail-summary-exists) (not no-summary)
2061 (let ((curr-msg rmail-current-message))
2062 (rmail-select-summary
2063 (rmail-summary-goto-msg curr-msg t t))))
2064 (if blurb
2065 (message blurb))))))
2066
2067 ;; Find all occurrences of certain fields, and highlight them.
2068 (defun rmail-highlight-headers ()
2069 ;; Do this only if the system supports faces.
2070 (if (and (fboundp 'internal-find-face)
2071 rmail-highlighted-headers)
2072 (save-excursion
2073 (search-forward "\n\n" nil 'move)
2074 (save-restriction
2075 (narrow-to-region (point-min) (point))
2076 (let ((case-fold-search t)
2077 (inhibit-read-only t)
2078 ;; Highlight with boldface if that is available.
2079 ;; Otherwise use the `highlight' face.
2080 (face (or rmail-highlight-face
2081 (if (face-differs-from-default-p 'bold)
2082 'bold 'highlight)))
2083 ;; List of overlays to reuse.
2084 (overlays rmail-overlay-list))
2085 (goto-char (point-min))
2086 (while (re-search-forward rmail-highlighted-headers nil t)
2087 (skip-chars-forward " \t")
2088 (let ((beg (point))
2089 overlay)
2090 (while (progn (forward-line 1)
2091 (looking-at "[ \t]")))
2092 ;; Back up over newline, then trailing spaces or tabs
2093 (forward-char -1)
2094 (while (member (preceding-char) '(? ?\t))
2095 (forward-char -1))
2096 (if overlays
2097 ;; Reuse an overlay we already have.
2098 (progn
2099 (setq overlay (car overlays)
2100 overlays (cdr overlays))
2101 (overlay-put overlay 'face face)
2102 (move-overlay overlay beg (point)))
2103 ;; Make a new overlay and add it to
2104 ;; rmail-overlay-list.
2105 (setq overlay (make-overlay beg (point)))
2106 (overlay-put overlay 'face face)
2107 (setq rmail-overlay-list
2108 (cons overlay rmail-overlay-list))))))))))
2109
2110 (defun rmail-next-message (n)
2111 "Show following message whether deleted or not.
2112 With prefix arg N, moves forward N messages, or backward if N is negative."
2113 (interactive "p")
2114 (rmail-maybe-set-message-counters)
2115 (rmail-show-message (+ rmail-current-message n)))
2116
2117 (defun rmail-previous-message (n)
2118 "Show previous message whether deleted or not.
2119 With prefix arg N, moves backward N messages, or forward if N is negative."
2120 (interactive "p")
2121 (rmail-next-message (- n)))
2122
2123 (defun rmail-next-undeleted-message (n)
2124 "Show following non-deleted message.
2125 With prefix arg N, moves forward N non-deleted messages,
2126 or backward if N is negative.
2127
2128 Returns t if a new message is being shown, nil otherwise."
2129 (interactive "p")
2130 (rmail-maybe-set-message-counters)
2131 (let ((lastwin rmail-current-message)
2132 (current rmail-current-message))
2133 (while (and (> n 0) (< current rmail-total-messages))
2134 (setq current (1+ current))
2135 (if (not (rmail-message-deleted-p current))
2136 (setq lastwin current n (1- n))))
2137 (while (and (< n 0) (> current 1))
2138 (setq current (1- current))
2139 (if (not (rmail-message-deleted-p current))
2140 (setq lastwin current n (1+ n))))
2141 (if (/= lastwin rmail-current-message)
2142 (progn (rmail-show-message lastwin)
2143 t)
2144 (if (< n 0)
2145 (message "No previous nondeleted message"))
2146 (if (> n 0)
2147 (message "No following nondeleted message"))
2148 nil)))
2149
2150 (defun rmail-previous-undeleted-message (n)
2151 "Show previous non-deleted message.
2152 With prefix argument N, moves backward N non-deleted messages,
2153 or forward if N is negative."
2154 (interactive "p")
2155 (rmail-next-undeleted-message (- n)))
2156
2157 (defun rmail-first-message ()
2158 "Show first message in file."
2159 (interactive)
2160 (rmail-maybe-set-message-counters)
2161 (rmail-show-message 1))
2162
2163 (defun rmail-last-message ()
2164 "Show last message in file."
2165 (interactive)
2166 (rmail-maybe-set-message-counters)
2167 (rmail-show-message rmail-total-messages))
2168
2169 (defun rmail-what-message ()
2170 (let ((where (point))
2171 (low 1)
2172 (high rmail-total-messages)
2173 (mid (/ rmail-total-messages 2)))
2174 (while (> (- high low) 1)
2175 (if (>= where (rmail-msgbeg mid))
2176 (setq low mid)
2177 (setq high mid))
2178 (setq mid (+ low (/ (- high low) 2))))
2179 (if (>= where (rmail-msgbeg high)) high low)))
2180
2181 (defun rmail-message-recipients-p (msg recipients &optional primary-only)
2182 (save-restriction
2183 (goto-char (rmail-msgbeg msg))
2184 (search-forward "\n*** EOOH ***\n" (point-max) t)
2185 (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
2186 (or (string-match recipients (or (mail-fetch-field "To") ""))
2187 (string-match recipients (or (mail-fetch-field "From") ""))
2188 (if (not primary-only)
2189 (string-match recipients (or (mail-fetch-field "Cc") ""))))))
2190
2191 (defun rmail-message-regexp-p (msg regexp)
2192 "Return t, if for message number MSG, regexp REGEXP matches in the header."
2193 (goto-char (rmail-msgbeg msg))
2194 (let ((end
2195 (save-excursion
2196 (or (search-forward "\n*** EOOH ***\n" (point-max) t)
2197 (search-forward "\n\n" (point-max)))
2198 (point))))
2199 (re-search-forward regexp end t)))
2200
2201 (defvar rmail-search-last-regexp nil)
2202 (defun rmail-search (regexp &optional n)
2203 "Show message containing next match for REGEXP (but not the current msg).
2204 Prefix argument gives repeat count; negative argument means search
2205 backwards (through earlier messages).
2206 Interactively, empty argument means use same regexp used last time."
2207 (interactive
2208 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
2209 (prompt
2210 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
2211 regexp)
2212 (if rmail-search-last-regexp
2213 (setq prompt (concat prompt
2214 "(default "
2215 rmail-search-last-regexp
2216 ") ")))
2217 (setq regexp (read-string prompt))
2218 (cond ((not (equal regexp ""))
2219 (setq rmail-search-last-regexp regexp))
2220 ((not rmail-search-last-regexp)
2221 (error "No previous Rmail search string")))
2222 (list rmail-search-last-regexp
2223 (prefix-numeric-value current-prefix-arg))))
2224 (or n (setq n 1))
2225 (message "%sRmail search for %s..."
2226 (if (< n 0) "Reverse " "")
2227 regexp)
2228 (rmail-maybe-set-message-counters)
2229 (let ((omin (point-min))
2230 (omax (point-max))
2231 (opoint (point))
2232 win
2233 (reversep (< n 0))
2234 (msg rmail-current-message))
2235 (unwind-protect
2236 (progn
2237 (widen)
2238 (while (/= n 0)
2239 ;; Check messages one by one, advancing message number up or down
2240 ;; but searching forward through each message.
2241 (if reversep
2242 (while (and (null win) (> msg 1))
2243 (goto-char (rmail-msgbeg (setq msg (1- msg))))
2244 (setq win (re-search-forward
2245 regexp (rmail-msgend msg) t)))
2246 (while (and (null win) (< msg rmail-total-messages))
2247 (goto-char (rmail-msgbeg (setq msg (1+ msg))))
2248 (setq win (re-search-forward regexp (rmail-msgend msg) t))))
2249 (setq n (+ n (if reversep 1 -1)))))
2250 (if win
2251 (progn
2252 ;; If this is a reverse search and we found a message,
2253 ;; search backward thru this message to position point.
2254 (if reversep
2255 (progn
2256 (goto-char (rmail-msgend msg))
2257 (re-search-backward
2258 regexp (rmail-msgbeg msg) t)))
2259 (setq win (point))
2260 (rmail-show-message msg)
2261 (message "%sRmail search for %s...done"
2262 (if reversep "Reverse " "")
2263 regexp)
2264 (goto-char win))
2265 (goto-char opoint)
2266 (narrow-to-region omin omax)
2267 (ding)
2268 (message "Search failed: %s" regexp)))))
2269
2270 (defun rmail-search-backwards (regexp &optional n)
2271 "Show message containing previous match for REGEXP.
2272 Prefix argument gives repeat count; negative argument means search
2273 forward (through later messages).
2274 Interactively, empty argument means use same regexp used last time."
2275 (interactive
2276 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
2277 (prompt
2278 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
2279 regexp)
2280 (if rmail-search-last-regexp
2281 (setq prompt (concat prompt
2282 "(default "
2283 rmail-search-last-regexp
2284 ") ")))
2285 (setq regexp (read-string prompt))
2286 (cond ((not (equal regexp ""))
2287 (setq rmail-search-last-regexp regexp))
2288 ((not rmail-search-last-regexp)
2289 (error "No previous Rmail search string")))
2290 (list rmail-search-last-regexp
2291 (prefix-numeric-value current-prefix-arg))))
2292 (rmail-search regexp (- (or n 1))))
2293
2294 ;; Show the first message which has the `unseen' attribute.
2295 (defun rmail-first-unseen-message ()
2296 (rmail-maybe-set-message-counters)
2297 (let ((current 1)
2298 found)
2299 (save-restriction
2300 (widen)
2301 (while (and (not found) (<= current rmail-total-messages))
2302 (if (rmail-message-labels-p current ", ?\\(unseen\\),")
2303 (setq found current))
2304 (setq current (1+ current))))
2305 ;; Let the caller show the message.
2306 ;; (if found
2307 ;; (rmail-show-message found))
2308 found))
2309
2310 (defun rmail-next-same-subject (n)
2311 "Go to the next mail message having the same subject header.
2312 With prefix argument N, do this N times.
2313 If N is negative, go backwards instead."
2314 (interactive "p")
2315 (let ((subject (mail-fetch-field "Subject"))
2316 (forward (> n 0))
2317 (i rmail-current-message)
2318 (case-fold-search t)
2319 search-regexp found)
2320 (if (string-match "\\`[ \t]+" subject)
2321 (setq subject (substring subject (match-end 0))))
2322 (if (string-match "Re:[ \t]*" subject)
2323 (setq subject (substring subject (match-end 0))))
2324 (if (string-match "[ \t]+\\'" subject)
2325 (setq subject (substring subject 0 (match-beginning 0))))
2326 (setq search-regexp (concat "^Subject: *\\(Re: *\\)?"
2327 (regexp-quote subject)
2328 "\n"))
2329 (save-excursion
2330 (save-restriction
2331 (widen)
2332 (while (and (/= n 0)
2333 (if forward
2334 (< i rmail-total-messages)
2335 (> i 1)))
2336 (let (done)
2337 (while (and (not done)
2338 (if forward
2339 (< i rmail-total-messages)
2340 (> i 1)))
2341 (setq i (if forward (1+ i) (1- i)))
2342 (goto-char (rmail-msgbeg i))
2343 (search-forward "\n*** EOOH ***\n")
2344 (let ((beg (point)) end)
2345 (search-forward "\n\n")
2346 (setq end (point))
2347 (goto-char beg)
2348 (setq done (re-search-forward search-regexp end t))))
2349 (if done (setq found i)))
2350 (setq n (if forward (1- n) (1+ n))))))
2351 (if found
2352 (rmail-show-message found)
2353 (error "No %s message with same subject"
2354 (if forward "following" "previous")))))
2355
2356 (defun rmail-previous-same-subject (n)
2357 "Go to the previous mail message having the same subject header.
2358 With prefix argument N, do this N times.
2359 If N is negative, go forwards instead."
2360 (interactive "p")
2361 (rmail-next-same-subject (- n)))
2362 \f
2363 ;;;; *** Rmail Message Deletion Commands ***
2364
2365 (defun rmail-message-deleted-p (n)
2366 (= (aref rmail-deleted-vector n) ?D))
2367
2368 (defun rmail-set-message-deleted-p (n state)
2369 (aset rmail-deleted-vector n (if state ?D ?\ )))
2370
2371 (defun rmail-delete-message ()
2372 "Delete this message and stay on it."
2373 (interactive)
2374 (rmail-set-attribute "deleted" t)
2375 (run-hooks 'rmail-delete-message-hook))
2376
2377 (defun rmail-undelete-previous-message ()
2378 "Back up to deleted message, select it, and undelete it."
2379 (interactive)
2380 (let ((msg rmail-current-message))
2381 (while (and (> msg 0)
2382 (not (rmail-message-deleted-p msg)))
2383 (setq msg (1- msg)))
2384 (if (= msg 0)
2385 (error "No previous deleted message")
2386 (if (/= msg rmail-current-message)
2387 (rmail-show-message msg))
2388 (rmail-set-attribute "deleted" nil)
2389 (if (rmail-summary-exists)
2390 (save-excursion
2391 (set-buffer rmail-summary-buffer)
2392 (rmail-summary-mark-undeleted msg)))
2393 (rmail-maybe-display-summary))))
2394
2395 (defun rmail-delete-forward (&optional backward)
2396 "Delete this message and move to next nondeleted one.
2397 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
2398 With prefix argument, delete and move backward.
2399
2400 Returns t if a new message is displayed after the delete, or nil otherwise."
2401 (interactive "P")
2402 (rmail-set-attribute "deleted" t)
2403 (run-hooks 'rmail-delete-message-hook)
2404 (let ((del-msg rmail-current-message))
2405 (if (rmail-summary-exists)
2406 (rmail-select-summary
2407 (rmail-summary-mark-deleted del-msg)))
2408 (prog1 (rmail-next-undeleted-message (if backward -1 1))
2409 (rmail-maybe-display-summary))))
2410
2411 (defun rmail-delete-backward ()
2412 "Delete this message and move to previous nondeleted one.
2413 Deleted messages stay in the file until the \\[rmail-expunge] command is given."
2414 (interactive)
2415 (rmail-delete-forward t))
2416
2417 ;; Compute the message number a given message would have after expunging.
2418 ;; The present number of the message is OLDNUM.
2419 ;; DELETEDVEC should be rmail-deleted-vector.
2420 ;; The value is nil for a message that would be deleted.
2421 (defun rmail-msg-number-after-expunge (deletedvec oldnum)
2422 (if (or (null oldnum) (= (aref deletedvec oldnum) ?D))
2423 nil
2424 (let ((i 0)
2425 (newnum 0))
2426 (while (< i oldnum)
2427 (if (/= (aref deletedvec i) ?D)
2428 (setq newnum (1+ newnum)))
2429 (setq i (1+ i)))
2430 newnum)))
2431
2432 (defun rmail-only-expunge ()
2433 "Actually erase all deleted messages in the file."
2434 (interactive)
2435 (message "Expunging deleted messages...")
2436 ;; Discard all undo records for this buffer.
2437 (or (eq buffer-undo-list t)
2438 (setq buffer-undo-list nil))
2439 (rmail-maybe-set-message-counters)
2440 (let* ((omax (- (buffer-size) (point-max)))
2441 (omin (- (buffer-size) (point-min)))
2442 (opoint (if (and (> rmail-current-message 0)
2443 (rmail-message-deleted-p rmail-current-message))
2444 0
2445 (- (point) (point-min))))
2446 (messages-head (cons (aref rmail-message-vector 0) nil))
2447 (messages-tail messages-head)
2448 ;; Don't make any undo records for the expunging.
2449 (buffer-undo-list t)
2450 (win))
2451 (unwind-protect
2452 (save-excursion
2453 (widen)
2454 (goto-char (point-min))
2455 (let ((counter 0)
2456 (number 1)
2457 (total rmail-total-messages)
2458 (new-message-number rmail-current-message)
2459 (new-summary nil)
2460 (new-msgref (list (list 0)))
2461 (rmailbuf (current-buffer))
2462 (buffer-read-only nil)
2463 (messages rmail-message-vector)
2464 (deleted rmail-deleted-vector)
2465 (summary rmail-summary-vector))
2466 (setq rmail-total-messages nil
2467 rmail-current-message nil
2468 rmail-message-vector nil
2469 rmail-deleted-vector nil
2470 rmail-summary-vector nil)
2471
2472 (while (<= number total)
2473 (if (= (aref deleted number) ?D)
2474 (progn
2475 (delete-region
2476 (marker-position (aref messages number))
2477 (marker-position (aref messages (1+ number))))
2478 (move-marker (aref messages number) nil)
2479 (if (> new-message-number counter)
2480 (setq new-message-number (1- new-message-number))))
2481 (setq counter (1+ counter))
2482 (setq messages-tail
2483 (setcdr messages-tail
2484 (cons (aref messages number) nil)))
2485 (setq new-summary
2486 (cons (if (= counter number) (aref summary (1- number)))
2487 new-summary))
2488 (setq new-msgref
2489 (cons (aref rmail-msgref-vector number)
2490 new-msgref))
2491 (setcar (car new-msgref) counter))
2492 (if (zerop (% (setq number (1+ number)) 20))
2493 (message "Expunging deleted messages...%d" number)))
2494 (setq messages-tail
2495 (setcdr messages-tail
2496 (cons (aref messages number) nil)))
2497 (setq rmail-current-message new-message-number
2498 rmail-total-messages counter
2499 rmail-message-vector (apply 'vector messages-head)
2500 rmail-deleted-vector (make-string (1+ counter) ?\ )
2501 rmail-summary-vector (vconcat (nreverse new-summary))
2502 rmail-msgref-vector (apply 'vector (nreverse new-msgref))
2503 win t)))
2504 (message "Expunging deleted messages...done")
2505 (if (not win)
2506 (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
2507 (rmail-show-message
2508 (if (zerop rmail-current-message) 1 nil))
2509 (goto-char (+ (point) opoint)))))
2510
2511 (defun rmail-expunge ()
2512 "Erase deleted messages from Rmail file and summary buffer."
2513 (interactive)
2514 (rmail-only-expunge)
2515 (if (rmail-summary-exists)
2516 (rmail-select-summary
2517 (rmail-update-summary))))
2518 \f
2519 ;;;; *** Rmail Mailing Commands ***
2520
2521 (defun rmail-start-mail (&optional noerase to subject in-reply-to cc
2522 replybuffer sendactions same-window others)
2523 (let (yank-action)
2524 (if replybuffer
2525 (setq yank-action (list 'insert-buffer replybuffer)))
2526 (setq others (cons (cons "cc" cc) others))
2527 (setq others (cons (cons "in-reply-to" in-reply-to) others))
2528 (if same-window
2529 (compose-mail to subject others
2530 noerase nil
2531 yank-action sendactions)
2532 (if (and window-system rmail-mail-new-frame)
2533 (prog1
2534 (compose-mail to subject others
2535 noerase 'switch-to-buffer-other-frame
2536 yank-action sendactions)
2537 ;; This is not a standard frame parameter;
2538 ;; nothing except sendmail.el looks at it.
2539 (modify-frame-parameters (selected-frame)
2540 '((mail-dedicated-frame . t))))
2541 (compose-mail to subject others
2542 noerase 'switch-to-buffer-other-window
2543 yank-action sendactions)))))
2544
2545 (defun rmail-mail ()
2546 "Send mail in another window.
2547 While composing the message, use \\[mail-yank-original] to yank the
2548 original message into it."
2549 (interactive)
2550 (rmail-start-mail nil nil nil nil nil rmail-view-buffer))
2551
2552 (defun rmail-continue ()
2553 "Continue composing outgoing message previously being composed."
2554 (interactive)
2555 (rmail-start-mail t))
2556
2557 (defun rmail-reply (just-sender)
2558 "Reply to the current message.
2559 Normally include CC: to all other recipients of original message;
2560 prefix argument means ignore them. While composing the reply,
2561 use \\[mail-yank-original] to yank the original message into it."
2562 (interactive "P")
2563 (let (from reply-to cc subject date to message-id references
2564 resent-to resent-cc resent-reply-to
2565 (msgnum rmail-current-message))
2566 (save-excursion
2567 (save-restriction
2568 (widen)
2569 (goto-char (rmail-msgbeg rmail-current-message))
2570 (forward-line 1)
2571 (if (= (following-char) ?0)
2572 (narrow-to-region
2573 (progn (forward-line 2)
2574 (point))
2575 (progn (search-forward "\n\n" (rmail-msgend rmail-current-message)
2576 'move)
2577 (point)))
2578 (narrow-to-region (point)
2579 (progn (search-forward "\n*** EOOH ***\n")
2580 (beginning-of-line) (point))))
2581 (setq from (mail-fetch-field "from")
2582 reply-to (or (mail-fetch-field "reply-to" nil t)
2583 from)
2584 cc (and (not just-sender)
2585 (mail-fetch-field "cc" nil t))
2586 subject (mail-fetch-field "subject")
2587 date (mail-fetch-field "date")
2588 to (or (mail-fetch-field "to" nil t) "")
2589 message-id (mail-fetch-field "message-id")
2590 references (mail-fetch-field "references" nil nil t)
2591 resent-reply-to (mail-fetch-field "resent-reply-to" nil t)
2592 resent-cc (and (not just-sender)
2593 (mail-fetch-field "resent-cc" nil t))
2594 resent-to (or (mail-fetch-field "resent-to" nil t) "")
2595 ;;; resent-subject (mail-fetch-field "resent-subject")
2596 ;;; resent-date (mail-fetch-field "resent-date")
2597 ;;; resent-message-id (mail-fetch-field "resent-message-id")
2598 )))
2599 ;; Merge the resent-to and resent-cc into the to and cc.
2600 (if (and resent-to (not (equal resent-to "")))
2601 (if (not (equal to ""))
2602 (setq to (concat to ", " resent-to))
2603 (setq to resent-to)))
2604 (if (and resent-cc (not (equal resent-cc "")))
2605 (if (not (equal cc ""))
2606 (setq cc (concat cc ", " resent-cc))
2607 (setq cc resent-cc)))
2608 ;; Add `Re: ' to subject if not there already.
2609 (and (stringp subject)
2610 (setq subject
2611 (concat rmail-reply-prefix
2612 (if (let ((case-fold-search t))
2613 (string-match rmail-reply-regexp subject))
2614 (substring subject (match-end 0))
2615 subject))))
2616 (rmail-start-mail
2617 nil
2618 ;; Using mail-strip-quoted-names is undesirable with newer mailers
2619 ;; since they can handle the names unstripped.
2620 ;; I don't know whether there are other mailers that still
2621 ;; need the names to be stripped.
2622 (mail-strip-quoted-names reply-to)
2623 subject
2624 (rmail-make-in-reply-to-field from date message-id)
2625 (if just-sender
2626 nil
2627 ;; mail-strip-quoted-names is NOT necessary for rmail-dont-reply-to
2628 ;; to do its job.
2629 (let* ((cc-list (rmail-dont-reply-to
2630 (mail-strip-quoted-names
2631 (if (null cc) to (concat to ", " cc))))))
2632 (if (string= cc-list "") nil cc-list)))
2633 rmail-view-buffer
2634 (list (list 'rmail-mark-message
2635 rmail-view-buffer
2636 (aref rmail-msgref-vector msgnum)
2637 "answered"))
2638 nil
2639 (list (cons "References" (concat (mapconcat 'identity references " ")
2640 " " message-id))))))
2641
2642 (defun rmail-mark-message (buffer msgnum-list attribute)
2643 "Give BUFFER's message number in MSGNUM-LIST the attribute ATTRIBUTE.
2644 This is use in the send-actions for message buffers.
2645 MSGNUM-LIST is a list of the form (MSGNUM)
2646 which is an element of rmail-msgref-vector."
2647 (save-excursion
2648 (set-buffer buffer)
2649 (if (car msgnum-list)
2650 (rmail-set-attribute attribute t (car msgnum-list)))))
2651
2652 (defun rmail-make-in-reply-to-field (from date message-id)
2653 (cond ((not from)
2654 (if message-id
2655 message-id
2656 nil))
2657 (mail-use-rfc822
2658 (require 'rfc822)
2659 (let ((tem (car (rfc822-addresses from))))
2660 (if message-id
2661 (if (or (not tem)
2662 (string-match
2663 (regexp-quote (if (string-match "@[^@]*\\'" tem)
2664 (substring tem 0
2665 (match-beginning 0))
2666 tem))
2667 message-id))
2668 ;; missing From, or Message-ID is sufficiently informative
2669 message-id
2670 (concat message-id " (" tem ")"))
2671 ;; Copy TEM, discarding text properties.
2672 (setq tem (copy-sequence tem))
2673 (set-text-properties 0 (length tem) nil tem)
2674 (setq tem (copy-sequence tem))
2675 ;; Use prin1 to fake RFC822 quoting
2676 (let ((field (prin1-to-string tem)))
2677 (if date
2678 (concat field "'s message of " date)
2679 field)))))
2680 ((let* ((foo "[^][\000-\037()<>@,;:\\\" ]+")
2681 (bar "[^][\000-\037()<>@,;:\\\"]+"))
2682 ;; These strings both match all non-ASCII characters.
2683 (or (string-match (concat "\\`[ \t]*\\(" bar
2684 "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
2685 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
2686 from)
2687 (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
2688 bar "\\))[ \t]*\\'")
2689 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
2690 from)))
2691 (let ((start (match-beginning 1))
2692 (end (match-end 1)))
2693 ;; Trim whitespace which above regexp match allows
2694 (while (and (< start end)
2695 (memq (aref from start) '(?\t ?\ )))
2696 (setq start (1+ start)))
2697 (while (and (< start end)
2698 (memq (aref from (1- end)) '(?\t ?\ )))
2699 (setq end (1- end)))
2700 (let ((field (substring from start end)))
2701 (if date (setq field (concat "message from " field " on " date)))
2702 (if message-id
2703 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
2704 (concat message-id " (" field ")")
2705 field))))
2706 (t
2707 ;; If we can't kludge it simply, do it correctly
2708 (let ((mail-use-rfc822 t))
2709 (rmail-make-in-reply-to-field from date message-id)))))
2710 \f
2711 (defun rmail-forward (resend)
2712 "Forward the current message to another user.
2713 With prefix argument, \"resend\" the message instead of forwarding it;
2714 see the documentation of `rmail-resend'."
2715 (interactive "P")
2716 (if resend
2717 (call-interactively 'rmail-resend)
2718 (let ((forward-buffer (current-buffer))
2719 (msgnum rmail-current-message)
2720 (subject (concat "["
2721 (let ((from (or (mail-fetch-field "From")
2722 (mail-fetch-field ">From"))))
2723 (if from
2724 (concat (mail-strip-quoted-names from) ": ")
2725 ""))
2726 (or (mail-fetch-field "Subject") "")
2727 "]")))
2728 (if (rmail-start-mail
2729 nil nil subject nil nil nil
2730 (list (list 'rmail-mark-message
2731 forward-buffer
2732 (aref rmail-msgref-vector msgnum)
2733 "forwarded"))
2734 ;; If only one window, use it for the mail buffer.
2735 ;; Otherwise, use another window for the mail buffer
2736 ;; so that the Rmail buffer remains visible
2737 ;; and sending the mail will get back to it.
2738 (and (not rmail-mail-new-frame) (one-window-p t)))
2739 ;; The mail buffer is now current.
2740 (save-excursion
2741 ;; Insert after header separator--before signature if any.
2742 (goto-char (point-min))
2743 (search-forward-regexp
2744 (concat "^" (regexp-quote mail-header-separator) "$"))
2745 (forward-line 1)
2746 (insert "------- Start of forwarded message -------\n")
2747 ;; Quote lines with `- ' if they start with `-'.
2748 (let ((beg (point)) end)
2749 (setq end (point-marker))
2750 (set-marker-insertion-type end t)
2751 (insert-buffer-substring forward-buffer)
2752 (goto-char beg)
2753 (while (re-search-forward "^-" end t)
2754 (beginning-of-line)
2755 (insert "- ")
2756 (forward-line 1))
2757 (goto-char end)
2758 (skip-chars-backward "\n")
2759 (if (< (point) end)
2760 (forward-char 1))
2761 (delete-region (point) end)
2762 (set-marker end nil))
2763 (insert "------- End of forwarded message -------\n")
2764 (push-mark))))))
2765 \f
2766 (defun rmail-resend (address &optional from comment mail-alias-file)
2767 "Resend current message to ADDRESSES.
2768 ADDRESSES should be a single address, a string consisting of several
2769 addresses separated by commas, or a list of addresses.
2770
2771 Optional FROM is the address to resend the message from, and
2772 defaults from the value of `user-mail-address'.
2773 Optional COMMENT is a string to insert as a comment in the resent message.
2774 Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
2775 typically for purposes of moderating a list."
2776 (interactive "sResend to: ")
2777 (require 'sendmail)
2778 (require 'mailalias)
2779 (if (not from) (setq from user-mail-address))
2780 (let ((tembuf (generate-new-buffer " sendmail temp"))
2781 (mail-header-separator "")
2782 (case-fold-search nil)
2783 (mailbuf (current-buffer)))
2784 (unwind-protect
2785 (save-excursion
2786 ;;>> Copy message into temp buffer
2787 (set-buffer tembuf)
2788 (insert-buffer-substring mailbuf)
2789 (goto-char (point-min))
2790 ;; Delete any Sender field, since that's not specifiable.
2791 ; Only delete Sender fields in the actual header.
2792 (re-search-forward "^$" nil 'move)
2793 ; Using "while" here rather than "if" because some buggy mail
2794 ; software may have inserted multiple Sender fields.
2795 (while (re-search-backward "^Sender:" nil t)
2796 (let (beg)
2797 (setq beg (point))
2798 (forward-line 1)
2799 (while (looking-at "[ \t]")
2800 (forward-line 1))
2801 (delete-region beg (point))))
2802 ; Go back to the beginning of the buffer so the Resent- fields
2803 ; are inserted there.
2804 (goto-char (point-min))
2805 ;;>> Insert resent-from:
2806 (insert "Resent-From: " from "\n")
2807 (insert "Resent-Date: " (mail-rfc822-date) "\n")
2808 ;;>> Insert resent-to: and bcc if need be.
2809 (let ((before (point)))
2810 (if mail-self-blind
2811 (insert "Resent-Bcc: " (user-login-name) "\n"))
2812 (insert "Resent-To: " (if (stringp address)
2813 address
2814 (mapconcat 'identity address ",\n\t"))
2815 "\n")
2816 ;; Expand abbrevs in the recipients.
2817 (save-excursion
2818 (if (featurep 'mailabbrev)
2819 (let ((end (point-marker))
2820 (local-abbrev-table mail-abbrevs)
2821 (old-syntax-table (syntax-table)))
2822 (if (and (not (vectorp mail-abbrevs))
2823 (file-exists-p mail-personal-alias-file))
2824 (build-mail-abbrevs))
2825 (set-syntax-table mail-abbrev-syntax-table)
2826 (goto-char before)
2827 (while (and (< (point) end)
2828 (progn (forward-word 1)
2829 (<= (point) end)))
2830 (expand-abbrev))
2831 (set-syntax-table old-syntax-table))
2832 (expand-mail-aliases before (point)))))
2833 ;;>> Set up comment, if any.
2834 (if (and (sequencep comment) (not (zerop (length comment))))
2835 (let ((before (point))
2836 after)
2837 (insert comment)
2838 (or (eolp) (insert "\n"))
2839 (setq after (point))
2840 (goto-char before)
2841 (while (< (point) after)
2842 (insert "Resent-Comment: ")
2843 (forward-line 1))))
2844 ;; Don't expand aliases in the destination fields
2845 ;; of the original message.
2846 (let (mail-aliases)
2847 (funcall send-mail-function)))
2848 (kill-buffer tembuf))
2849 (rmail-set-attribute "resent" t rmail-current-message)))
2850 \f
2851 (defvar mail-unsent-separator
2852 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
2853 "^ *---+ +Returned message +---+ *$\\|"
2854 "^Start of returned message$\\|"
2855 "^ *---+ +Original message +---+ *$\\|"
2856 "^ *--+ +begin message +--+ *$\\|"
2857 "^ *---+ +Original message follows +---+ *$\\|"
2858 "^ *---+ +Your message follows +---+ *$\\|"
2859 "^|? *---+ +Message text follows: +---+ *|?$")
2860 "A regexp that matches the separator before the text of a failed message.")
2861
2862 (defvar mail-mime-unsent-header "^Content-Type: message/rfc822 *$"
2863 "A regexp that matches the header of a MIME body part with a failed message.")
2864
2865 (defun rmail-retry-failure ()
2866 "Edit a mail message which is based on the contents of the current message.
2867 For a message rejected by the mail system, extract the interesting headers and
2868 the body of the original message.
2869 If the failed message is a MIME multipart message, it is searched for a
2870 body part with a header which matches the variable `mail-mime-unsent-header'.
2871 Otherwise, the variable `mail-unsent-separator' should match the string that
2872 delimits the returned original message.
2873 The variable `rmail-retry-ignored-headers' is a regular expression
2874 specifying headers which should not be copied into the new message."
2875 (interactive)
2876 (require 'mail-utils)
2877 (let ((rmail-this-buffer (current-buffer))
2878 (msgnum rmail-current-message)
2879 bounce-start bounce-end bounce-indent resending)
2880 (save-excursion
2881 ;; Narrow down to just the quoted original message
2882 (rmail-beginning-of-message)
2883 (let* ((case-fold-search t)
2884 (top (point))
2885 (content-type
2886 (save-restriction
2887 ;; Fetch any content-type header in current message
2888 (search-forward "\n\n") (narrow-to-region top (point))
2889 (mail-fetch-field "Content-Type") )) )
2890 ;; Handle MIME multipart bounce messages
2891 (if (and content-type
2892 (string-match
2893 ";[\n\t ]*boundary=\"?\\([-0-9a-z'()+_,./:=?]+\\)\"?"
2894 content-type))
2895 (let ((codestring
2896 (concat "\n--"
2897 (substring content-type (match-beginning 1)
2898 (match-end 1)))))
2899 (or (re-search-forward mail-mime-unsent-header nil t)
2900 (error "Cannot find beginning of header in failed message"))
2901 (or (search-forward "\n\n" nil t)
2902 (error "Cannot find start of Mime data in failed message"))
2903 (setq bounce-start (point))
2904 (if (search-forward codestring nil t)
2905 (setq bounce-end (match-beginning 0))
2906 (setq bounce-end (point-max)))
2907 )
2908 ;; non-MIME bounce
2909 (or (re-search-forward mail-unsent-separator nil t)
2910 (error "Cannot parse this as a failure message"))
2911 (skip-chars-forward "\n")
2912 ;; Support a style of failure message in which the original
2913 ;; message is indented, and included within lines saying
2914 ;; `Start of returned message' and `End of returned message'.
2915 (if (looking-at " +Received:")
2916 (progn
2917 (setq bounce-start (point))
2918 (skip-chars-forward " ")
2919 (setq bounce-indent (- (current-column)))
2920 (goto-char (point-max))
2921 (re-search-backward "^End of returned message$" nil t)
2922 (setq bounce-end (point)))
2923 ;; One message contained a few random lines before the old
2924 ;; message header. The first line of the message started with
2925 ;; two hyphens. A blank line followed these random lines.
2926 ;; The same line beginning with two hyphens was possibly
2927 ;; marking the end of the message.
2928 (if (looking-at "^--")
2929 (let ((boundary (buffer-substring-no-properties
2930 (point)
2931 (progn (end-of-line) (point)))))
2932 (search-forward "\n\n")
2933 (skip-chars-forward "\n")
2934 (setq bounce-start (point))
2935 (goto-char (point-max))
2936 (search-backward (concat "\n\n" boundary) bounce-start t)
2937 (setq bounce-end (point)))
2938 (setq bounce-start (point)
2939 bounce-end (point-max)))
2940 (or (search-forward "\n\n" nil t)
2941 (error "Cannot find end of header in failed message"))
2942 ))))
2943 ;; Start sending a new message; default header fields from the original.
2944 ;; Turn off the usual actions for initializing the message body
2945 ;; because we want to get only the text from the failure message.
2946 (let (mail-signature mail-setup-hook)
2947 (if (rmail-start-mail nil nil nil nil nil rmail-this-buffer
2948 (list (list 'rmail-mark-message
2949 rmail-this-buffer
2950 (aref rmail-msgref-vector msgnum)
2951 "retried")))
2952 ;; Insert original text as initial text of new draft message.
2953 ;; Bind inhibit-read-only since the header delimiter
2954 ;; of the previous message was probably read-only.
2955 (let ((inhibit-read-only t))
2956 (erase-buffer)
2957 (insert-buffer-substring rmail-this-buffer bounce-start bounce-end)
2958 (goto-char (point-min))
2959 (if bounce-indent
2960 (indent-rigidly (point-min) (point-max) bounce-indent))
2961 (rmail-clear-headers rmail-retry-ignored-headers)
2962 (rmail-clear-headers "^sender:\\|^from:\\|^return-path:")
2963 (goto-char (point-min))
2964 (save-restriction
2965 (search-forward "\n\n")
2966 (forward-line -1)
2967 (narrow-to-region (point-min) (point))
2968 (setq resending (mail-fetch-field "resent-to"))
2969 (if mail-self-blind
2970 (if resending
2971 (insert "Resent-Bcc: " (user-login-name) "\n")
2972 (insert "BCC: " (user-login-name) "\n"))))
2973 (insert mail-header-separator)
2974 (mail-position-on-field (if resending "Resent-To" "To") t)
2975 (set-buffer rmail-this-buffer)
2976 (rmail-beginning-of-message))))))
2977 \f
2978 (defun rmail-summary-exists ()
2979 "Non-nil iff in an RMAIL buffer and an associated summary buffer exists.
2980 In fact, the non-nil value returned is the summary buffer itself."
2981 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
2982 rmail-summary-buffer))
2983
2984 (defun rmail-summary-displayed ()
2985 "t iff in RMAIL buffer and an associated summary buffer is displayed."
2986 (and rmail-summary-buffer (get-buffer-window rmail-summary-buffer)))
2987
2988 (defcustom rmail-redisplay-summary nil
2989 "*Non-nil means Rmail should show the summary when it changes.
2990 This has an effect only if a summary buffer exists."
2991 :type 'boolean
2992 :group 'rmail-summary)
2993
2994 (defcustom rmail-summary-window-size nil
2995 "*Non-nil means specify the height for an Rmail summary window."
2996 :type '(choice (const :tag "Disabled" nil) integer)
2997 :group 'rmail-summary)
2998
2999 ;; Put the summary buffer back on the screen, if user wants that.
3000 (defun rmail-maybe-display-summary ()
3001 (let ((selected (selected-window))
3002 window)
3003 ;; If requested, make sure the summary is displayed.
3004 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
3005 rmail-redisplay-summary
3006 (if (get-buffer-window rmail-summary-buffer 0)
3007 ;; It's already in some frame; show that one.
3008 (let ((frame (window-frame
3009 (get-buffer-window rmail-summary-buffer 0))))
3010 (make-frame-visible frame)
3011 (raise-frame frame))
3012 (display-buffer rmail-summary-buffer)))
3013 ;; If requested, set the height of the summary window.
3014 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
3015 rmail-summary-window-size
3016 (setq window (get-buffer-window rmail-summary-buffer))
3017 ;; Don't try to change the size if just one window in frame.
3018 (not (eq window (frame-root-window (window-frame window))))
3019 (unwind-protect
3020 (progn
3021 (select-window window)
3022 (enlarge-window (- rmail-summary-window-size (window-height))))
3023 (select-window selected)))))
3024 \f
3025 ;;;; *** Rmail Local Fontification ***
3026
3027 (defun rmail-fontify-buffer-function ()
3028 ;; This function's symbol is bound to font-lock-fontify-buffer-function.
3029 (make-local-hook 'rmail-show-message-hook)
3030 (add-hook 'rmail-show-message-hook 'rmail-fontify-message nil t)
3031 ;; If we're already showing a message, fontify it now.
3032 (if rmail-current-message (rmail-fontify-message))
3033 ;; Prevent Font Lock mode from kicking in.
3034 (setq font-lock-fontified t))
3035
3036 (defun rmail-unfontify-buffer-function ()
3037 ;; This function's symbol is bound to font-lock-fontify-unbuffer-function.
3038 (let ((modified (buffer-modified-p))
3039 (buffer-undo-list t) (inhibit-read-only t)
3040 before-change-functions after-change-functions
3041 buffer-file-name buffer-file-truename)
3042 (save-restriction
3043 (widen)
3044 (remove-hook 'rmail-show-message-hook 'rmail-fontify-message t)
3045 (remove-text-properties (point-min) (point-max) '(rmail-fontified nil))
3046 (font-lock-default-unfontify-buffer)
3047 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))
3048
3049 (defun rmail-fontify-message ()
3050 ;; Fontify the current message if it is not already fontified.
3051 (if (text-property-any (point-min) (point-max) 'rmail-fontified nil)
3052 (let ((modified (buffer-modified-p))
3053 (buffer-undo-list t) (inhibit-read-only t)
3054 before-change-functions after-change-functions
3055 buffer-file-name buffer-file-truename)
3056 (save-excursion
3057 (save-match-data
3058 (add-text-properties (point-min) (point-max) '(rmail-fontified t))
3059 (font-lock-fontify-region (point-min) (point-max))
3060 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))))
3061
3062 ;;; Speedbar support for RMAIL files.
3063 (eval-when-compile (require 'speedbspec))
3064
3065 (defvar rmail-speedbar-last-user nil
3066 "The last user to be displayed in the speedbar.")
3067
3068 (defvar rmail-speedbar-menu-items
3069 '(["Browse Item On Line" speedbar-edit-line t]
3070 ["Move message to folder" rmail-move-message-to-folder-on-line
3071 (save-excursion (beginning-of-line)
3072 (looking-at "<M> "))])
3073 "Additional menu-items to add to speedbar frame.")
3074
3075 (defun rmail-speedbar-buttons (buffer)
3076 "Create buttons for BUFFER containing rmail messages.
3077 Click on the address under Reply to: to reply to this person.
3078 Under Folders: Click a name to read it, or on the <M> to move the
3079 current message into that RMAIL folder."
3080 (let ((from nil))
3081 (save-excursion
3082 (set-buffer buffer)
3083 (goto-char (point-min))
3084 (if (not (re-search-forward "^Reply-To: " nil t))
3085 (if (not (re-search-forward "^From:? " nil t))
3086 (setq from t)))
3087 (if from
3088 nil
3089 (setq from (buffer-substring (point) (save-excursion
3090 (end-of-line)
3091 (point))))))
3092 (goto-char (point-min))
3093 (if (and (looking-at "Reply to:")
3094 (equal from rmail-speedbar-last-user))
3095 nil
3096 (setq rmail-speedbar-last-user from)
3097 (erase-buffer)
3098 (insert "Reply To:\n")
3099 (if (stringp from)
3100 (speedbar-insert-button from 'speedbar-directory-face 'highlight
3101 'rmail-speedbar-button 'rmail-reply))
3102 (insert "Folders:\n")
3103 (let* ((case-fold-search nil)
3104 (df (directory-files (save-excursion (set-buffer buffer)
3105 default-directory)
3106 nil "^[A-Z0-9]+\\(\\.[A-Z0-9]+\\)?$")))
3107 (while df
3108 (speedbar-insert-button "<M>" 'speedbar-button-face 'highlight
3109 'rmail-speedbar-move-message (car df))
3110 (speedbar-insert-button (car df) 'speedbar-file-face 'highlight
3111 'rmail-speedbar-find-file nil t)
3112 (setq df (cdr df)))))))
3113
3114 (defun rmail-speedbar-button (text token indent)
3115 "Execute an rmail command specified by TEXT.
3116 The command used is TOKEN. INDENT is not used."
3117 (speedbar-with-attached-buffer
3118 (funcall token t)))
3119
3120 (defun rmail-speedbar-find-file (text token indent)
3121 "Load in the rmail file TEXT.
3122 TOKEN and INDENT are not used."
3123 (speedbar-with-attached-buffer
3124 (message "Loading in RMAIL file %s..." text)
3125 (find-file text)))
3126
3127 (defun rmail-move-message-to-folder-on-line ()
3128 "If the current line is a folder, move current message to it."
3129 (interactive)
3130 (save-excursion
3131 (beginning-of-line)
3132 (if (re-search-forward "<M> " (save-excursion (end-of-line) (point)) t)
3133 (progn
3134 (forward-char -2)
3135 (speedbar-do-function-pointer)))))
3136
3137 (defun rmail-speedbar-move-message (text token indent)
3138 "From button TEXT, copy current message to the rmail file specified by TOKEN.
3139 TEXT and INDENT are not used."
3140 (speedbar-with-attached-buffer
3141 (message "Moving message to %s" token)
3142 (rmail-output-to-rmail-file token)))
3143
3144
3145 (provide 'rmail)
3146
3147 ;;; rmail.el ends here