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