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