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;
44 ;;; look at them 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, the message is
52 appended in inbox format, the same way `rmail-output' does it.
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 (expand-file-name (or answer 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 (file-name-directory default-file
))))
81 ;; If the user enters just a directory,
82 ;; use the name within that directory chosen by the default.
83 (setq rmail-default-rmail-file
84 (if (file-directory-p read-file
)
85 (expand-file-name (file-name-nondirectory default-file
)
88 (list rmail-default-rmail-file
89 (prefix-numeric-value current-prefix-arg
))))
90 (or count
(setq count
1))
92 (expand-file-name file-name
93 (file-name-directory rmail-default-rmail-file
)))
94 (if (and (file-readable-p file-name
) (not (mail-file-babyl-p file-name
)))
95 (rmail-output file-name count
)
96 (rmail-maybe-set-message-counters)
97 (setq file-name
(abbreviate-file-name file-name
))
98 (or (find-buffer-visiting file-name
)
99 (file-exists-p file-name
)
101 (concat "\"" file-name
"\" does not exist, create it? "))
102 (let ((file-buffer (create-file-buffer file-name
)))
104 (set-buffer file-buffer
)
105 (rmail-insert-rmail-file-header)
106 (let ((require-final-newline nil
))
107 (write-region (point-min) (point-max) file-name t
1)))
108 (kill-buffer file-buffer
))
109 (error "Output file does not exist")))
114 ;; Temporarily turn off Deleted attribute.
115 ;; Do this outside the save-restriction, since it would
116 ;; shift the place in the buffer where the visible text starts.
117 (if (rmail-message-deleted-p rmail-current-message
)
118 (progn (setq redelete t
)
119 (rmail-set-attribute "deleted" nil
)))
122 ;; Decide whether to append to a file or to an Emacs buffer.
124 (let ((buf (find-buffer-visiting file-name
))
125 (cur (current-buffer))
126 (beg (1+ (rmail-msgbeg rmail-current-message
)))
127 (end (1+ (rmail-msgend rmail-current-message
)))
128 (coding-system-for-write
129 (or rmail-file-coding-system
133 (if rmail-fields-not-to-output
134 ;; Delete some fields while we output.
135 (let ((obuf (current-buffer)))
136 (set-buffer (get-buffer-create " rmail-out-temp"))
137 (insert-buffer-substring obuf beg end
)
138 (rmail-delete-unwanted-fields)
139 (append-to-file (point-min) (point-max) file-name
)
141 (kill-buffer (get-buffer " rmail-out-temp")))
142 (append-to-file beg end file-name
))
143 (if (eq buf
(current-buffer))
144 (error "Can't output message to same file it's already in"))
145 ;; File has been visited, in buffer BUF.
147 (let ((buffer-read-only nil
)
148 (msg (and (boundp 'rmail-current-message
)
149 rmail-current-message
)))
150 ;; If MSG is non-nil, buffer is in RMAIL mode.
153 ;; Turn on auto save mode, if it's off in this
154 ;; buffer but enabled by default.
155 (and (not buffer-auto-save-file-name
)
158 (rmail-maybe-set-message-counters)
160 (narrow-to-region (point-max) (point-max))
161 (insert-buffer-substring cur beg end
)
162 (goto-char (point-min))
164 (search-backward "\n\^_")
165 (narrow-to-region (point) (point-max))
166 (rmail-delete-unwanted-fields)
167 (rmail-count-new-messages t
)
168 (if (rmail-summary-exists)
169 (rmail-select-summary
170 (rmail-update-summary)))
171 (rmail-show-message msg
))
172 ;; Output file not in rmail mode => just insert at the end.
173 (narrow-to-region (point-min) (1+ (buffer-size)))
174 (goto-char (point-max))
175 (insert-buffer-substring cur beg end
)
176 (rmail-delete-unwanted-fields)))))))
177 (rmail-set-attribute "filed" t
))
178 (if redelete
(rmail-set-attribute "deleted" t
))))
179 (setq count
(1- count
))
180 (if rmail-delete-after-output
181 (rmail-delete-forward)
183 (rmail-next-undeleted-message 1))))))
186 (defcustom rmail-fields-not-to-output nil
187 "*Regexp describing fields to exclude when outputting a message to a file."
188 :type
'(choice (const :tag
"None" nil
)
190 :group
'rmail-output
)
192 ;; Delete from the buffer header fields we don't want output.
193 ;; NOT-RMAIL if t means this buffer does not have the full header
194 ;; and *** EOOH *** that a message in an Rmail file has.
195 (defun rmail-delete-unwanted-fields (&optional not-rmail
)
196 (if rmail-fields-not-to-output
198 (goto-char (point-min))
199 ;; Find the end of the header.
200 (if (and (or not-rmail
(search-forward "\n*** EOOH ***\n" nil t
))
201 (search-forward "\n\n" nil t
))
202 (let ((end (point-marker)))
203 (goto-char (point-min))
204 (while (re-search-forward rmail-fields-not-to-output end t
)
206 (delete-region (point)
207 (progn (forward-line 1) (point)))))))))
209 ;;; There are functions elsewhere in Emacs that use this function;
210 ;;; look at them before you change the calling method.
212 (defun rmail-output (file-name &optional count noattribute from-gnus
)
213 "Append this message to system-inbox-format mail file named FILE-NAME.
214 A prefix argument N says to output N consecutive messages
215 starting with the current one. Deleted messages are skipped and don't count.
216 When called from lisp code, N may be omitted.
218 If the pruned message header is shown on the current message, then
219 messages will be appended with pruned headers; otherwise, messages
220 will be appended with their original headers.
222 The default file name comes from `rmail-default-file',
223 which is updated to the name you use in this command.
225 The optional third argument NOATTRIBUTE, if non-nil, says not
226 to set the `filed' attribute, and not to display a message.
228 The optional fourth argument FROM-GNUS is set when called from GNUS."
232 (setq tail rmail-output-file-alist
)
233 ;; Suggest a file based on a pattern match.
234 (while (and tail
(not answer
))
236 (goto-char (point-min))
237 (if (re-search-forward (car (car tail
)) nil t
)
238 (setq answer
(eval (cdr (car tail
)))))
239 (setq tail
(cdr tail
))))
240 ;; If no suggestion, use same file as last time.
241 (or answer rmail-default-file
))))
242 (list (setq rmail-default-file
245 (concat "Output message to Unix mail file: (default "
246 (file-name-nondirectory default-file
)
248 (file-name-directory (expand-file-name default-file
))
249 (expand-file-name default-file
))))
250 (if (file-directory-p read-file
)
251 (expand-file-name (file-name-nondirectory default-file
)
254 (or read-file
(file-name-nondirectory default-file
))
255 (file-name-directory default-file
)))))
256 (prefix-numeric-value current-prefix-arg
))))
257 (or count
(setq count
1))
259 (expand-file-name file-name
260 (and rmail-default-file
261 (file-name-directory rmail-default-file
))))
262 (if (and (file-readable-p file-name
) (mail-file-babyl-p file-name
))
263 (rmail-output-to-rmail-file file-name count
)
264 (let ((orig-count count
)
265 (rmailbuf (current-buffer))
267 (tembuf (get-buffer-create " rmail-output"))
272 (narrow-to-region (rmail-msgbeg rmail-current-message
) (point-max))
273 (goto-char (point-min))
275 (= (following-char) ?
0)))))
277 mail-from mime-version
)
279 ;; Preserve the Mail-From and MIME-Version fields
280 ;; even if they have been pruned.
285 (goto-char (rmail-msgbeg rmail-current-message
))
286 (setq header-beginning
(point))
287 (search-forward "\n*** EOOH ***\n")
288 (narrow-to-region header-beginning
(point))
290 (mail-fetch-field "Mail-From")
292 (mail-fetch-field "MIME-Version")))))
296 (insert-buffer-substring rmailbuf
)
297 (rmail-delete-unwanted-fields t
)
298 (or (bolp) (insert "\n"))
299 (goto-char (point-min))
301 (insert mail-from
"\n")
303 (mail-strip-quoted-names (or (mail-fetch-field "from")
304 (mail-fetch-field "really-from")
305 (mail-fetch-field "sender")
307 " " (current-time-string) "\n"))
309 (insert "MIME-Version: " mime-version
"\n"))
310 ;; ``Quote'' "\nFrom " as "\n>From "
311 ;; (note that this isn't really quoting, as there is no requirement
312 ;; that "\n[>]+From " be quoted in the same transparent way.)
313 (let ((case-fold-search nil
))
314 (while (search-forward "\nFrom " nil t
)
317 (write-region (point-min) (point-max) file-name t
318 (if noattribute
'nomsg
)))
320 (if (equal major-mode
'rmail-mode
)
321 (rmail-set-attribute "filed" t
)))
322 (setq count
(1- count
))
324 (let ((next-message-p
325 (if rmail-delete-after-output
326 (rmail-delete-forward)
328 (rmail-next-undeleted-message 1))))
329 (num-appended (- orig-count count
)))
330 (if (and next-message-p original-headers-p
)
331 (rmail-toggle-header))
332 (if (and (> count
0) (not next-message-p
))
336 (set-buffer rmailbuf
)
337 (format "Only %d message%s appended" num-appended
338 (if (= num-appended
1) "" "s"))))
340 (kill-buffer tembuf
))))
343 (defun rmail-output-body-to-file (file-name)
344 "Write this message body to the file FILE-NAME.
345 FILE-NAME defaults, interactively, from the Subject field of the message."
348 (or (mail-fetch-field "Subject")
349 rmail-default-body-file
)))
350 (list (setq rmail-default-body-file
352 "Output message body to file: "
353 (and default-file
(file-name-directory default-file
))
355 nil default-file
)))))
357 (expand-file-name file-name
358 (and rmail-default-body-file
359 (file-name-directory rmail-default-body-file
))))
361 (goto-char (point-min))
362 (search-forward "\n\n")
363 (and (file-exists-p file-name
)
364 (not (y-or-n-p (message "File %s exists; overwrite? " file-name
)))
365 (error "Operation aborted"))
366 (write-region (point) (point-max) file-name
)
367 (if (equal major-mode
'rmail-mode
)
368 (rmail-set-attribute "stored" t
)))
369 (if rmail-delete-after-output
370 (rmail-delete-forward)))
372 ;;; rmailout.el ends here