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