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