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.
30 (defcustom 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 The suggestion is taken if REGEXP matches anywhere in the message buffer.
34 NAME-EXP may be a string constant giving the file name to use,
35 or more generally it may be any kind of expression that returns
36 a file name as a string."
37 :type
'(repeat (cons regexp
39 (string :tag
"File Name")
43 ;;; There are functions elsewhere in Emacs that use this function; check
44 ;;; them out before you change the calling method.
46 (defun rmail-output-to-rmail-file (file-name &optional count
)
47 "Append the current message to an Rmail file named FILE-NAME.
48 If the file does not exist, ask if it should be created.
49 If file is being visited, the message is appended to the Emacs
50 buffer visiting that file.
51 If the file exists and is not an Rmail file,
52 the message is appended in inbox format.
54 The default file name comes from `rmail-default-rmail-file',
55 which is updated to the name you use in this command.
57 A prefix argument N says to output N consecutive messages
58 starting with the current one. Deleted messages are skipped and don't count."
62 (setq tail rmail-output-file-alist
)
63 ;; Suggest a file based on a pattern match.
64 (while (and tail
(not answer
))
66 (goto-char (point-min))
67 (if (re-search-forward (car (car tail
)) nil t
)
68 (setq answer
(eval (cdr (car tail
)))))
69 (setq tail
(cdr tail
))))
70 ;; If not suggestions, use same file as last time.
71 (or answer rmail-default-rmail-file
))))
72 (list (setq rmail-default-rmail-file
75 (concat "Output message to Rmail file: (default "
76 (file-name-nondirectory default-file
)
78 (file-name-directory default-file
)
80 (if (file-directory-p read-file
)
81 (expand-file-name (file-name-nondirectory default-file
)
84 (or read-file default-file
)
85 (file-name-directory default-file
)))))
86 (prefix-numeric-value current-prefix-arg
))))
87 (or count
(setq count
1))
89 (expand-file-name file-name
90 (file-name-directory rmail-default-rmail-file
)))
91 (if (and (file-readable-p file-name
) (not (mail-file-babyl-p file-name
)))
92 (rmail-output file-name count
)
93 (rmail-maybe-set-message-counters)
94 (setq file-name
(abbreviate-file-name file-name
))
95 (or (find-buffer-visiting file-name
)
96 (file-exists-p file-name
)
98 (concat "\"" file-name
"\" does not exist, create it? "))
99 (let ((file-buffer (create-file-buffer file-name
)))
101 (set-buffer file-buffer
)
102 (rmail-insert-rmail-file-header)
103 (let ((require-final-newline nil
))
104 (write-region (point-min) (point-max) file-name t
1)))
105 (kill-buffer file-buffer
))
106 (error "Output file does not exist")))
111 ;; Temporarily turn off Deleted attribute.
112 ;; Do this outside the save-restriction, since it would
113 ;; shift the place in the buffer where the visible text starts.
114 (if (rmail-message-deleted-p rmail-current-message
)
115 (progn (setq redelete t
)
116 (rmail-set-attribute "deleted" nil
)))
119 ;; Decide whether to append to a file or to an Emacs buffer.
121 (let ((buf (find-buffer-visiting file-name
))
122 (cur (current-buffer))
123 (beg (1+ (rmail-msgbeg rmail-current-message
)))
124 (end (1+ (rmail-msgend rmail-current-message
))))
127 (if rmail-fields-not-to-output
128 ;; Delete some fields while we output.
129 (let ((obuf (current-buffer)))
130 (set-buffer (get-buffer-create " rmail-out-temp"))
131 (insert-buffer-substring obuf beg end
)
132 (rmail-delete-unwanted-fields)
133 (append-to-file (point-min) (point-max) file-name
)
135 (kill-buffer (get-buffer " rmail-out-temp")))
136 (append-to-file beg end file-name
))
137 (if (eq buf
(current-buffer))
138 (error "Can't output message to same file it's already in"))
139 ;; File has been visited, in buffer BUF.
141 (let ((buffer-read-only nil
)
142 (msg (and (boundp 'rmail-current-message
)
143 rmail-current-message
)))
144 ;; If MSG is non-nil, buffer is in RMAIL mode.
147 ;; Turn on auto save mode, if it's off in this
148 ;; buffer but enabled by default.
149 (and (not buffer-auto-save-file-name
)
152 (rmail-maybe-set-message-counters)
154 (narrow-to-region (point-max) (point-max))
155 (insert-buffer-substring cur beg end
)
156 (goto-char (point-min))
158 (search-backward "\n\^_")
159 (narrow-to-region (point) (point-max))
160 (rmail-delete-unwanted-fields)
161 (rmail-count-new-messages t
)
162 (if (rmail-summary-exists)
163 (rmail-select-summary
164 (rmail-update-summary)))
165 (rmail-show-message msg
))
166 ;; Output file not in rmail mode => just insert at the end.
167 (narrow-to-region (point-min) (1+ (buffer-size)))
168 (goto-char (point-max))
169 (insert-buffer-substring cur beg end
)
170 (rmail-delete-unwanted-fields)))))))
171 (rmail-set-attribute "filed" t
))
172 (if redelete
(rmail-set-attribute "deleted" t
))))
173 (setq count
(1- count
))
174 (if rmail-delete-after-output
175 (rmail-delete-forward)
177 (rmail-next-undeleted-message 1))))))
180 (defcustom rmail-fields-not-to-output nil
181 "*Regexp describing fields to exclude when outputting a message to a file."
182 :type
'(choice (const :tag
"None" nil
)
184 :group
'rmail-output
)
186 ;; Delete from the buffer header fields we don't want output.
187 ;; NOT-RMAIL if t means this buffer does not have the full header
188 ;; and *** EOOH *** that a message in an Rmail file has.
189 (defun rmail-delete-unwanted-fields (&optional not-rmail
)
190 (if rmail-fields-not-to-output
192 (goto-char (point-min))
193 ;; Find the end of the header.
194 (if (and (or not-rmail
(search-forward "\n*** EOOH ***\n" nil t
))
195 (search-forward "\n\n" nil t
))
196 (let ((end (point-marker)))
197 (goto-char (point-min))
198 (while (re-search-forward rmail-fields-not-to-output end t
)
200 (delete-region (point)
201 (progn (forward-line 1) (point)))))))))
203 ;;; There are functions elsewhere in Emacs that use this function; check
204 ;;; them out before you change the calling method.
206 (defun rmail-output (file-name &optional count noattribute from-gnus
)
207 "Append this message to system-inbox-format mail file named FILE-NAME.
208 A prefix argument N says to output N consecutive messages
209 starting with the current one. Deleted messages are skipped and don't count.
210 When called from lisp code, N may be omitted.
212 If the pruned message header is shown on the current message, then
213 messages will be appended with pruned headers; otherwise, messages
214 will be appended with their original headers.
216 The default file name comes from `rmail-default-file',
217 which is updated to the name you use in this command.
219 The optional third argument NOATTRIBUTE, if non-nil, says not
220 to set the `filed' attribute, and not to display a message.
222 The optional fourth argument FROM-GNUS is set when called from GNUS."
226 (setq tail rmail-output-file-alist
)
227 ;; Suggest a file based on a pattern match.
228 (while (and tail
(not answer
))
230 (goto-char (point-min))
231 (if (re-search-forward (car (car tail
)) nil t
)
232 (setq answer
(eval (cdr (car tail
)))))
233 (setq tail
(cdr tail
))))
234 ;; If not suggestions, use same file as last time.
235 (or answer rmail-default-file
))))
236 (list (setq rmail-default-file
239 (concat "Output message to Unix mail file: (default "
240 (file-name-nondirectory default-file
)
242 (file-name-directory default-file
)
244 (if (file-directory-p read-file
)
245 (expand-file-name (file-name-nondirectory default-file
)
248 (or read-file default-file
)
249 (file-name-directory default-file
)))))
250 (prefix-numeric-value current-prefix-arg
))))
251 (or count
(setq count
1))
253 (expand-file-name file-name
254 (and rmail-default-file
255 (file-name-directory rmail-default-file
))))
256 (if (and (file-readable-p file-name
) (mail-file-babyl-p file-name
))
257 (rmail-output-to-rmail-file file-name count
)
258 (let ((orig-count count
)
259 (rmailbuf (current-buffer))
261 (tembuf (get-buffer-create " rmail-output"))
266 (narrow-to-region (rmail-msgbeg rmail-current-message
) (point-max))
267 (goto-char (point-min))
269 (= (following-char) ?
0)))))
278 (goto-char (rmail-msgbeg rmail-current-message
))
279 (setq header-beginning
(point))
280 (search-forward "\n*** EOOH ***\n")
281 (narrow-to-region header-beginning
(point))
282 (mail-fetch-field "Mail-From")))))
286 (insert-buffer-substring rmailbuf
)
287 (rmail-delete-unwanted-fields t
)
289 (goto-char (point-min))
291 (insert mail-from
"\n")
293 (mail-strip-quoted-names (or (mail-fetch-field "from")
294 (mail-fetch-field "really-from")
295 (mail-fetch-field "sender")
297 " " (current-time-string) "\n"))
298 ;; ``Quote'' "\nFrom " as "\n>From "
299 ;; (note that this isn't really quoting, as there is no requirement
300 ;; that "\n[>]+From " be quoted in the same transparent way.)
301 (let ((case-fold-search nil
))
302 (while (search-forward "\nFrom " nil t
)
305 (write-region (point-min) (point-max) file-name t
306 (if noattribute
'nomsg
)))
308 (if (equal major-mode
'rmail-mode
)
309 (rmail-set-attribute "filed" t
)))
310 (setq count
(1- count
))
312 (let ((next-message-p
313 (if rmail-delete-after-output
314 (rmail-delete-forward)
316 (rmail-next-undeleted-message 1))))
317 (num-appended (- orig-count count
)))
318 (if (and next-message-p original-headers-p
)
319 (rmail-toggle-header))
320 (if (and (> count
0) (not next-message-p
))
324 (set-buffer rmailbuf
)
325 (format "Only %d message%s appended" num-appended
326 (if (= num-appended
1) "" "s"))))
328 (kill-buffer tembuf
))))
331 (defun rmail-output-body-to-file (file-name)
332 "Write this message body to the file FILE-NAME.
333 FILE-NAME defaults, interactively, from the Subject field of the message."
336 (mail-fetch-field "Subject")))
337 (list (read-file-name
338 "Output message body to file: "
339 (file-name-directory default-file
)
343 (goto-char (point-min))
344 (search-forward "\n\n")
345 (and (file-exists-p file-name
)
346 (not (y-or-n-p (message "File %s exists; overwrite? " file-name
)))
347 (error "Operation aborted"))
348 (write-region (point) (point-max) file-name
)
349 (if (equal major-mode
'rmail-mode
)
350 (rmail-set-attribute "stored" t
)))
351 (if rmail-delete-after-output
352 (rmail-delete-forward)))
354 ;;; rmailout.el ends here