(rmail-output): If message was shown with full headers,
[bpt/emacs.git] / lisp / mail / rmailout.el
1 ;;; rmailout.el --- "RMAIL" mail reader for Emacs: output message to a file.
2
3 ;; Copyright (C) 1985, 1987, 1993 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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Code:
25
26 ;; Temporary until Emacs always has this variable.
27 (defvar rmail-delete-after-output nil
28 "*Non-nil means automatically delete a message that is copied to a file.")
29
30 (defvar rmail-output-file-alist nil
31 "*Alist matching regexps to suggested output Rmail files.
32 This is a list of elements of the form (REGEXP . NAME-EXP).
33 NAME-EXP may be a string constant giving the file name to use,
34 or more generally it may be any kind of expression that returns
35 a file name as a string.")
36
37 ;;; There are functions elsewhere in Emacs that use this function; check
38 ;;; them out before you change the calling method.
39 (defun rmail-output-to-rmail-file (file-name &optional count)
40 "Append the current message to an Rmail file named FILE-NAME.
41 If the file does not exist, ask if it should be created.
42 If file is being visited, the message is appended to the Emacs
43 buffer visiting that file.
44 If the file exists and is not an Rmail file,
45 the message is appended in inbox format.
46
47 A prefix argument N says to output N consecutive messages
48 starting with the current one. Deleted messages are skipped and don't count."
49 (interactive
50 (let ((default-file
51 (let (answer tail)
52 (setq tail rmail-output-file-alist)
53 ;; Suggest a file based on a pattern match.
54 (while (and tail (not answer))
55 (save-excursion
56 (goto-char (point-min))
57 (if (re-search-forward (car (car tail)) nil t)
58 (setq answer (eval (cdr (car tail)))))
59 (setq tail (cdr tail))))
60 ;; If not suggestions, use same file as last time.
61 (or answer rmail-last-rmail-file))))
62 (list (setq rmail-last-rmail-file
63 (read-file-name
64 (concat "Output message to Rmail file: (default "
65 (file-name-nondirectory default-file)
66 ") ")
67 (file-name-directory default-file)
68 default-file))
69 (prefix-numeric-value current-prefix-arg))))
70 (or count (setq count 1))
71 (setq file-name
72 (expand-file-name file-name
73 (file-name-directory rmail-last-rmail-file)))
74 (if (and (file-readable-p file-name) (not (rmail-file-p file-name)))
75 (rmail-output file-name count)
76 (rmail-maybe-set-message-counters)
77 (setq file-name (abbreviate-file-name file-name))
78 (or (get-file-buffer file-name)
79 (file-exists-p file-name)
80 (if (yes-or-no-p
81 (concat "\"" file-name "\" does not exist, create it? "))
82 (let ((file-buffer (create-file-buffer file-name)))
83 (save-excursion
84 (set-buffer file-buffer)
85 (rmail-insert-rmail-file-header)
86 (let ((require-final-newline nil))
87 (write-region (point-min) (point-max) file-name t 1)))
88 (kill-buffer file-buffer))
89 (error "Output file does not exist")))
90 (while (> count 0)
91 (let (redelete)
92 (unwind-protect
93 (progn
94 (save-restriction
95 (widen)
96 (if (rmail-message-deleted-p rmail-current-message)
97 (progn (setq redelete t)
98 (rmail-set-attribute "deleted" nil)))
99 ;; Decide whether to append to a file or to an Emacs buffer.
100 (save-excursion
101 (let ((buf (get-file-buffer file-name))
102 (cur (current-buffer))
103 (beg (1+ (rmail-msgbeg rmail-current-message)))
104 (end (1+ (rmail-msgend rmail-current-message))))
105 (if (not buf)
106 (append-to-file beg end file-name)
107 (if (eq buf (current-buffer))
108 (error "Can't output message to same file it's already in"))
109 ;; File has been visited, in buffer BUF.
110 (set-buffer buf)
111 (let ((buffer-read-only nil)
112 (msg (and (boundp 'rmail-current-message)
113 rmail-current-message)))
114 ;; If MSG is non-nil, buffer is in RMAIL mode.
115 (if msg
116 (progn
117 (rmail-maybe-set-message-counters)
118 (widen)
119 (narrow-to-region (point-max) (point-max))
120 (insert-buffer-substring cur beg end)
121 (goto-char (point-min))
122 (widen)
123 (search-backward "\n\^_")
124 (narrow-to-region (point) (point-max))
125 (rmail-count-new-messages t)
126 (rmail-show-message msg))
127 ;; Output file not in rmail mode => just insert at the end.
128 (narrow-to-region (point-min) (1+ (buffer-size)))
129 (goto-char (point-max))
130 (insert-buffer-substring cur beg end)))))))
131 (rmail-set-attribute "filed" t))
132 (if redelete (rmail-set-attribute "deleted" t))))
133 (setq count (1- count))
134 (if rmail-delete-after-output
135 (rmail-delete-forward)
136 (if (> count 0)
137 (rmail-next-undeleted-message 1))))))
138
139 ;; Returns t if file FILE is an Rmail file.
140 (defun rmail-file-p (file)
141 (let ((buf (generate-new-buffer " *rmail-file-p*")))
142 (unwind-protect
143 (save-excursion
144 (set-buffer buf)
145 (insert-file-contents file nil 0 100)
146 (looking-at "BABYL OPTIONS:"))
147 (kill-buffer buf))))
148
149 ;;; There are functions elsewhere in Emacs that use this function; check
150 ;;; them out before you change the calling method.
151 (defun rmail-output (file-name &optional count noattribute)
152 "Append this message to Unix mail file named FILE-NAME.
153 A prefix argument N says to output N consecutive messages
154 starting with the current one. Deleted messages are skipped and don't count.
155 When called from lisp code, N may be omitted.
156
157 If the pruned message header is shown on the current message, then
158 messages will be appended with pruned headers; otherwise, messages
159 will be appended with their original headers.
160
161 The optional third argument NOATTRIBUTE, if non-nil, says not
162 to set the `filed' attribute, and not to display a message."
163 (interactive
164 (list (setq rmail-last-file
165 (read-file-name
166 (concat "Output message to Unix mail file"
167 (if rmail-last-file
168 (concat " (default "
169 (file-name-nondirectory rmail-last-file)
170 "): " )
171 ": "))
172 (and rmail-last-file (file-name-directory rmail-last-file))
173 rmail-last-file))
174 (prefix-numeric-value current-prefix-arg)))
175 (or count (setq count 1))
176 (setq file-name
177 (expand-file-name file-name
178 (and rmail-last-file
179 (file-name-directory rmail-last-file))))
180 (if (and (file-readable-p file-name) (rmail-file-p file-name))
181 (rmail-output-to-rmail-file file-name count)
182 (let ((orig-count count)
183 (rmailbuf (current-buffer))
184 (case-fold-search t)
185 (tembuf (get-buffer-create " rmail-output"))
186 (original-headers-p
187 (save-excursion
188 (save-restriction
189 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
190 (goto-char (point-min))
191 (forward-line 1)
192 (= (following-char) ?0))))
193 header-beginning
194 mail-from)
195 (while (> count 0)
196 (setq mail-from
197 (save-excursion
198 (save-restriction
199 (widen)
200 (goto-char (rmail-msgbeg rmail-current-message))
201 (setq header-beginning (point))
202 (search-forward "\n*** EOOH ***\n")
203 (narrow-to-region header-beginning (point))
204 (mail-fetch-field "Mail-From"))))
205 (save-excursion
206 (set-buffer tembuf)
207 (erase-buffer)
208 (insert-buffer-substring rmailbuf)
209 (insert "\n")
210 (goto-char (point-min))
211 (if mail-from
212 (insert mail-from "\n")
213 (insert "From "
214 (mail-strip-quoted-names (or (mail-fetch-field "from")
215 (mail-fetch-field "really-from")
216 (mail-fetch-field "sender")
217 "unknown"))
218 " " (current-time-string) "\n"))
219 ;; ``Quote'' "\nFrom " as "\n>From "
220 ;; (note that this isn't really quoting, as there is no requirement
221 ;; that "\n[>]+From " be quoted in the same transparent way.)
222 (while (search-forward "\nFrom " nil t)
223 (forward-char -5)
224 (insert ?>))
225 (write-region (point-min) (point-max) file-name t
226 (if noattribute 'nomsg)))
227 (or noattribute
228 (if (equal major-mode 'rmail-mode)
229 (rmail-set-attribute "filed" t)))
230 (setq count (1- count))
231 (let ((next-message-p
232 (if rmail-delete-after-output
233 (rmail-delete-forward)
234 (if (> count 0)
235 (rmail-next-undeleted-message 1))))
236 (num-appended (- orig-count count)))
237 (if (and next-message-p original-headers-p)
238 (rmail-toggle-header))
239 (if (and (> count 0) (not next-message-p))
240 (progn
241 (error
242 (save-excursion
243 (set-buffer rmailbuf)
244 (format "Only %d message%s appended" num-appended
245 (if (= num-appended 1) "" "s"))))
246 (setq count 0)))))
247 (kill-buffer tembuf))))
248
249 ;;; rmailout.el ends here