1 ;;; rmailout.el --- "RMAIL" mail reader for Emacs: output message to a file.
3 ;; Copyright (C) 1985, 1987, 1993, 1994 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
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)
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.
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.
31 (defcustom rmail-output-file-alist nil
32 "*Alist matching regexps to suggested output Rmail files.
33 This is a list of elements of the form (REGEXP . NAME-EXP).
34 The suggestion is taken if REGEXP matches anywhere in the message buffer.
35 NAME-EXP may be a string constant giving the file name to use,
36 or more generally it may be any kind of expression that returns
37 a file name as a string."
38 :type
'(repeat (cons regexp
40 (string :tag
"File Name")
44 (defun rmail-output-read-rmail-file-name ()
45 "Read the file name to use for `rmail-output-to-rmail-file'.
46 Set `rmail-default-rmail-file' to this name as well as returning it."
49 (setq tail rmail-output-file-alist
)
50 ;; Suggest a file based on a pattern match.
51 (while (and tail
(not answer
))
53 (if (eq major-mode
'rmail-summary-mode
)
54 (set-buffer rmail-buffer
))
55 (goto-char (point-min))
56 (if (re-search-forward (car (car tail
)) nil t
)
57 (setq answer
(eval (cdr (car tail
)))))
58 (setq tail
(cdr tail
))))
59 ;; If no suggestions, use same file as last time.
60 (expand-file-name (or answer rmail-default-rmail-file
)))))
64 (concat "Output message to Rmail file: (default "
65 (file-name-nondirectory default-file
)
67 (file-name-directory default-file
)
68 (abbreviate-file-name default-file
))
69 (file-name-directory default-file
))))
70 ;; If the user enters just a directory,
71 ;; use the name within that directory chosen by the default.
72 (setq rmail-default-rmail-file
73 (if (file-directory-p read-file
)
74 (expand-file-name (file-name-nondirectory default-file
)
78 (defun rmail-output-read-file-name ()
79 "Read the file name to use for `rmail-output'.
80 Set `rmail-default-file' to this name as well as returning it."
83 (setq tail rmail-output-file-alist
)
84 ;; Suggest a file based on a pattern match.
85 (while (and tail
(not answer
))
87 (goto-char (point-min))
88 (if (re-search-forward (car (car tail
)) nil t
)
89 (setq answer
(eval (cdr (car tail
)))))
90 (setq tail
(cdr tail
))))
91 ;; If no suggestion, use same file as last time.
92 (or answer rmail-default-file
))))
96 (concat "Output message to Unix mail file: (default "
97 (file-name-nondirectory default-file
)
99 (file-name-directory default-file
)
100 (abbreviate-file-name default-file
))
101 (file-name-directory default-file
))))
102 (setq rmail-default-file
103 (if (file-directory-p read-file
)
104 (expand-file-name (file-name-nondirectory default-file
)
107 (or read-file
(file-name-nondirectory default-file
))
108 (file-name-directory default-file
)))))))
110 ;;; There are functions elsewhere in Emacs that use this function;
111 ;;; look at them before you change the calling method.
113 (defun rmail-output-to-rmail-file (file-name &optional count
)
114 "Append the current message to an Rmail file named FILE-NAME.
115 If the file does not exist, ask if it should be created.
116 If file is being visited, the message is appended to the Emacs
117 buffer visiting that file.
118 If the file exists and is not an Rmail file, the message is
119 appended in inbox format, the same way `rmail-output' does it.
121 The default file name comes from `rmail-default-rmail-file',
122 which is updated to the name you use in this command.
124 A prefix argument N says to output N consecutive messages
125 starting with the current one. Deleted messages are skipped and don't count."
127 (list (rmail-output-read-rmail-file-name)
128 (prefix-numeric-value current-prefix-arg
)))
129 (or count
(setq count
1))
131 (expand-file-name file-name
132 (file-name-directory rmail-default-rmail-file
)))
133 (if (and (file-readable-p file-name
) (not (mail-file-babyl-p file-name
)))
134 (rmail-output file-name count
)
135 (rmail-maybe-set-message-counters)
136 (setq file-name
(abbreviate-file-name file-name
))
137 (or (find-buffer-visiting file-name
)
138 (file-exists-p file-name
)
140 (concat "\"" file-name
"\" does not exist, create it? "))
141 (let ((file-buffer (create-file-buffer file-name
)))
143 (set-buffer file-buffer
)
144 (rmail-insert-rmail-file-header)
145 (let ((require-final-newline nil
))
146 (write-region (point-min) (point-max) file-name t
1)))
147 (kill-buffer file-buffer
))
148 (error "Output file does not exist")))
153 ;; Temporarily turn off Deleted attribute.
154 ;; Do this outside the save-restriction, since it would
155 ;; shift the place in the buffer where the visible text starts.
156 (if (rmail-message-deleted-p rmail-current-message
)
157 (progn (setq redelete t
)
158 (rmail-set-attribute "deleted" nil
)))
161 ;; Decide whether to append to a file or to an Emacs buffer.
163 (let ((buf (find-buffer-visiting file-name
))
164 (cur (current-buffer))
165 (beg (1+ (rmail-msgbeg rmail-current-message
)))
166 (end (1+ (rmail-msgend rmail-current-message
)))
167 (coding-system-for-write
168 (or rmail-file-coding-system
172 (if rmail-fields-not-to-output
173 ;; Delete some fields while we output.
174 (let ((obuf (current-buffer)))
175 (set-buffer (get-buffer-create " rmail-out-temp"))
176 (insert-buffer-substring obuf beg end
)
177 (rmail-delete-unwanted-fields)
178 (append-to-file (point-min) (point-max) file-name
)
180 (kill-buffer (get-buffer " rmail-out-temp")))
181 (append-to-file beg end file-name
))
182 (if (eq buf
(current-buffer))
183 (error "Can't output message to same file it's already in"))
184 ;; File has been visited, in buffer BUF.
186 (let ((buffer-read-only nil
)
187 (msg (and (boundp 'rmail-current-message
)
188 rmail-current-message
)))
189 ;; If MSG is non-nil, buffer is in RMAIL mode.
192 ;; Turn on auto save mode, if it's off in this
193 ;; buffer but enabled by default.
194 (and (not buffer-auto-save-file-name
)
197 (rmail-maybe-set-message-counters)
199 (narrow-to-region (point-max) (point-max))
200 (insert-buffer-substring cur beg end
)
201 (goto-char (point-min))
203 (search-backward "\n\^_")
204 (narrow-to-region (point) (point-max))
205 (rmail-delete-unwanted-fields)
206 (rmail-count-new-messages t
)
207 (if (rmail-summary-exists)
208 (rmail-select-summary
209 (rmail-update-summary)))
210 (rmail-show-message msg
))
211 ;; Output file not in rmail mode => just insert at the end.
212 (narrow-to-region (point-min) (1+ (buffer-size)))
213 (goto-char (point-max))
214 (insert-buffer-substring cur beg end
)
215 (rmail-delete-unwanted-fields)))))))
216 (rmail-set-attribute "filed" t
))
217 (if redelete
(rmail-set-attribute "deleted" t
))))
218 (setq count
(1- count
))
219 (if rmail-delete-after-output
220 (unless (rmail-delete-forward) (setq count
0))
222 (unless (rmail-next-undeleted-message 1) (setq count
0)))))))
225 (defcustom rmail-fields-not-to-output nil
226 "*Regexp describing fields to exclude when outputting a message to a file."
227 :type
'(choice (const :tag
"None" nil
)
229 :group
'rmail-output
)
231 ;; Delete from the buffer header fields we don't want output.
232 ;; NOT-RMAIL if t means this buffer does not have the full header
233 ;; and *** EOOH *** that a message in an Rmail file has.
234 (defun rmail-delete-unwanted-fields (&optional not-rmail
)
235 (if rmail-fields-not-to-output
237 (goto-char (point-min))
238 ;; Find the end of the header.
239 (if (and (or not-rmail
(search-forward "\n*** EOOH ***\n" nil t
))
240 (search-forward "\n\n" nil t
))
241 (let ((end (point-marker)))
242 (goto-char (point-min))
243 (while (re-search-forward rmail-fields-not-to-output end t
)
245 (delete-region (point)
246 (progn (forward-line 1) (point)))))))))
248 ;;; There are functions elsewhere in Emacs that use this function;
249 ;;; look at them before you change the calling method.
251 (defun rmail-output (file-name &optional count noattribute from-gnus
)
252 "Append this message to system-inbox-format mail file named FILE-NAME.
253 A prefix argument N says to output N consecutive messages
254 starting with the current one. Deleted messages are skipped and don't count.
255 When called from lisp code, N may be omitted.
257 If the pruned message header is shown on the current message, then
258 messages will be appended with pruned headers; otherwise, messages
259 will be appended with their original headers.
261 The default file name comes from `rmail-default-file',
262 which is updated to the name you use in this command.
264 The optional third argument NOATTRIBUTE, if non-nil, says not
265 to set the `filed' attribute, and not to display a message.
267 The optional fourth argument FROM-GNUS is set when called from GNUS."
269 (list (rmail-output-read-file-name)
270 (prefix-numeric-value current-prefix-arg
)))
271 (or count
(setq count
1))
273 (expand-file-name file-name
274 (and rmail-default-file
275 (file-name-directory rmail-default-file
))))
276 (if (and (file-readable-p file-name
) (mail-file-babyl-p file-name
))
277 (rmail-output-to-rmail-file file-name count
)
278 (let ((orig-count count
)
279 (rmailbuf (current-buffer))
281 (tembuf (get-buffer-create " rmail-output"))
286 (narrow-to-region (rmail-msgbeg rmail-current-message
) (point-max))
287 (goto-char (point-min))
289 (= (following-char) ?
0)))))
291 mail-from mime-version
)
293 ;; Preserve the Mail-From and MIME-Version fields
294 ;; even if they have been pruned.
299 (goto-char (rmail-msgbeg rmail-current-message
))
300 (setq header-beginning
(point))
301 (search-forward "\n*** EOOH ***\n")
302 (narrow-to-region header-beginning
(point))
304 (mail-fetch-field "Mail-From")
306 (mail-fetch-field "MIME-Version")))))
310 (insert-buffer-substring rmailbuf
)
311 (rmail-delete-unwanted-fields t
)
312 (or (bolp) (insert "\n"))
313 (goto-char (point-min))
315 (insert mail-from
"\n")
317 (mail-strip-quoted-names (or (mail-fetch-field "from")
318 (mail-fetch-field "really-from")
319 (mail-fetch-field "sender")
321 " " (current-time-string) "\n"))
323 (insert "MIME-Version: " mime-version
"\n"))
324 ;; ``Quote'' "\nFrom " as "\n>From "
325 ;; (note that this isn't really quoting, as there is no requirement
326 ;; that "\n[>]+From " be quoted in the same transparent way.)
327 (let ((case-fold-search nil
))
328 (while (search-forward "\nFrom " nil t
)
331 (write-region (point-min) (point-max) file-name t
332 (if noattribute
'nomsg
)))
334 (if (equal major-mode
'rmail-mode
)
335 (rmail-set-attribute "filed" t
)))
336 (setq count
(1- count
))
338 (let ((next-message-p
339 (if rmail-delete-after-output
340 (rmail-delete-forward)
342 (rmail-next-undeleted-message 1))))
343 (num-appended (- orig-count count
)))
344 (if (and next-message-p original-headers-p
)
345 (rmail-toggle-header))
346 (if (and (> count
0) (not next-message-p
))
350 (set-buffer rmailbuf
)
351 (format "Only %d message%s appended" num-appended
352 (if (= num-appended
1) "" "s"))))
354 (kill-buffer tembuf
))))
357 (defun rmail-output-body-to-file (file-name)
358 "Write this message body to the file FILE-NAME.
359 FILE-NAME defaults, interactively, from the Subject field of the message."
362 (or (mail-fetch-field "Subject")
363 rmail-default-body-file
)))
364 (list (setq rmail-default-body-file
366 "Output message body to file: "
367 (and default-file
(file-name-directory default-file
))
369 nil default-file
)))))
371 (expand-file-name file-name
372 (and rmail-default-body-file
373 (file-name-directory rmail-default-body-file
))))
375 (goto-char (point-min))
376 (search-forward "\n\n")
377 (and (file-exists-p file-name
)
378 (not (y-or-n-p (message "File %s exists; overwrite? " file-name
)))
379 (error "Operation aborted"))
380 (write-region (point) (point-max) file-name
)
381 (if (equal major-mode
'rmail-mode
)
382 (rmail-set-attribute "stored" t
)))
383 (if rmail-delete-after-output
384 (rmail-delete-forward)))
386 ;;; rmailout.el ends here