1 ;;; rmailout.el --- "RMAIL" mail reader for Emacs: output message to a file
3 ;; Copyright (C) 1985, 1987, 1993, 1994, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
34 (defcustom rmail-output-file-alist nil
35 "*Alist matching regexps to suggested output Rmail files.
36 This is a list of elements of the form (REGEXP . NAME-EXP).
37 The suggestion is taken if REGEXP matches anywhere in the message buffer.
38 NAME-EXP may be a string constant giving the file name to use,
39 or more generally it may be any kind of expression that returns
40 a file name as a string."
41 :type
'(repeat (cons regexp
43 (string :tag
"File Name")
47 (defun rmail-output-read-rmail-file-name ()
48 "Read the file name to use for `rmail-output-to-rmail-file'.
49 Set `rmail-default-rmail-file' to this name as well as returning it."
52 (setq tail rmail-output-file-alist
)
53 ;; Suggest a file based on a pattern match.
54 (while (and tail
(not answer
))
56 (set-buffer rmail-buffer
)
57 (goto-char (point-min))
58 (if (re-search-forward (car (car tail
)) nil t
)
59 (setq answer
(eval (cdr (car tail
)))))
60 (setq tail
(cdr tail
))))
61 ;; If no suggestions, use same file as last time.
62 (expand-file-name (or answer rmail-default-rmail-file
)))))
66 (concat "Output message to Rmail file (default "
67 (file-name-nondirectory default-file
)
69 (file-name-directory default-file
)
70 (abbreviate-file-name default-file
))
71 (file-name-directory default-file
))))
72 ;; If the user enters just a directory,
73 ;; use the name within that directory chosen by the default.
74 (setq rmail-default-rmail-file
75 (if (file-directory-p read-file
)
76 (expand-file-name (file-name-nondirectory default-file
)
80 (defun rmail-output-read-file-name ()
81 "Read the file name to use for `rmail-output'.
82 Set `rmail-default-file' to this name as well as returning it."
85 (setq tail rmail-output-file-alist
)
86 ;; Suggest a file based on a pattern match.
87 (while (and tail
(not answer
))
89 (goto-char (point-min))
90 (if (re-search-forward (car (car tail
)) nil t
)
91 (setq answer
(eval (cdr (car tail
)))))
92 (setq tail
(cdr tail
))))
93 ;; If no suggestion, use same file as last time.
94 (or answer rmail-default-file
))))
98 (concat "Output message to Unix mail file (default "
99 (file-name-nondirectory default-file
)
101 (file-name-directory default-file
)
102 (abbreviate-file-name default-file
))
103 (file-name-directory default-file
))))
104 (setq rmail-default-file
105 (if (file-directory-p read-file
)
106 (expand-file-name (file-name-nondirectory default-file
)
109 (or read-file
(file-name-nondirectory default-file
))
110 (file-name-directory default-file
)))))))
112 ;;; There are functions elsewhere in Emacs that use this function;
113 ;;; look at them before you change the calling method.
115 (defun rmail-output-to-rmail-file (file-name &optional count stay
)
116 "Append the current message to an Rmail file named FILE-NAME.
117 If the file does not exist, ask if it should be created.
118 If file is being visited, the message is appended to the Emacs
119 buffer visiting that file.
120 If the file exists and is not an Rmail file, the message is
121 appended in inbox format, the same way `rmail-output' does it.
123 The default file name comes from `rmail-default-rmail-file',
124 which is updated to the name you use in this command.
126 A prefix argument COUNT says to output that many consecutive messages,
127 starting with the current one. Deleted messages are skipped and don't count.
129 If the optional argument STAY is non-nil, then leave the last filed
130 message up instead of moving forward to the next non-deleted message."
132 (list (rmail-output-read-rmail-file-name)
133 (prefix-numeric-value current-prefix-arg
)))
134 (or count
(setq count
1))
136 (expand-file-name file-name
137 (file-name-directory rmail-default-rmail-file
)))
138 (if (and (file-readable-p file-name
) (not (mail-file-babyl-p file-name
)))
139 (rmail-output file-name count
)
140 (rmail-maybe-set-message-counters)
141 (setq file-name
(abbreviate-file-name file-name
))
142 (or (find-buffer-visiting file-name
)
143 (file-exists-p file-name
)
145 (concat "\"" file-name
"\" does not exist, create it? "))
146 (let ((file-buffer (create-file-buffer file-name
)))
148 (set-buffer file-buffer
)
149 (rmail-insert-rmail-file-header)
150 (let ((require-final-newline nil
)
151 (coding-system-for-write
152 (or rmail-file-coding-system
154 (write-region (point-min) (point-max) file-name t
1)))
155 (kill-buffer file-buffer
))
156 (error "Output file does not exist")))
161 (set-buffer rmail-buffer
)
162 ;; Temporarily turn off Deleted attribute.
163 ;; Do this outside the save-restriction, since it would
164 ;; shift the place in the buffer where the visible text starts.
165 (if (rmail-message-deleted-p rmail-current-message
)
166 (progn (setq redelete t
)
167 (rmail-set-attribute "deleted" nil
)))
170 ;; Decide whether to append to a file or to an Emacs buffer.
172 (let ((buf (find-buffer-visiting file-name
))
173 (cur (current-buffer))
174 (beg (1+ (rmail-msgbeg rmail-current-message
)))
175 (end (1+ (rmail-msgend rmail-current-message
)))
176 (coding-system-for-write
177 (or rmail-file-coding-system
181 (if rmail-fields-not-to-output
182 ;; Delete some fields while we output.
183 (let ((obuf (current-buffer)))
184 (set-buffer (get-buffer-create " rmail-out-temp"))
185 (insert-buffer-substring obuf beg end
)
186 (rmail-delete-unwanted-fields)
187 (append-to-file (point-min) (point-max) file-name
)
189 (kill-buffer (get-buffer " rmail-out-temp")))
190 (append-to-file beg end file-name
))
191 (if (eq buf
(current-buffer))
192 (error "Can't output message to same file it's already in"))
193 ;; File has been visited, in buffer BUF.
195 (let ((buffer-read-only nil
)
196 (msg (and (boundp 'rmail-current-message
)
197 rmail-current-message
)))
198 ;; If MSG is non-nil, buffer is in RMAIL mode.
201 ;; Turn on auto save mode, if it's off in this
202 ;; buffer but enabled by default.
203 (and (not buffer-auto-save-file-name
)
206 (rmail-maybe-set-message-counters)
208 (narrow-to-region (point-max) (point-max))
209 (insert-buffer-substring cur beg end
)
210 (goto-char (point-min))
212 (search-backward "\n\^_")
213 (narrow-to-region (point) (point-max))
214 (rmail-delete-unwanted-fields)
215 (rmail-count-new-messages t
)
216 (if (rmail-summary-exists)
217 (rmail-select-summary
218 (rmail-update-summary)))
219 (rmail-show-message msg
))
220 ;; Output file not in rmail mode => just insert at the end.
221 (narrow-to-region (point-min) (1+ (buffer-size)))
222 (goto-char (point-max))
223 (insert-buffer-substring cur beg end
)
224 (rmail-delete-unwanted-fields)))))))
225 (rmail-set-attribute "filed" t
))
226 (if redelete
(rmail-set-attribute "deleted" t
))))
227 (setq count
(1- count
))
228 (if rmail-delete-after-output
230 (if (and (= count
0) stay
)
231 (rmail-delete-message)
232 (rmail-delete-forward))
236 (if (not stay
) (rmail-next-undeleted-message 1))
240 (defcustom rmail-fields-not-to-output nil
241 "*Regexp describing fields to exclude when outputting a message to a file."
242 :type
'(choice (const :tag
"None" nil
)
244 :group
'rmail-output
)
246 ;; Delete from the buffer header fields we don't want output.
247 ;; NOT-RMAIL if t means this buffer does not have the full header
248 ;; and *** EOOH *** that a message in an Rmail file has.
249 (defun rmail-delete-unwanted-fields (&optional not-rmail
)
250 (if rmail-fields-not-to-output
252 (goto-char (point-min))
253 ;; Find the end of the header.
254 (if (and (or not-rmail
(search-forward "\n*** EOOH ***\n" nil t
))
255 (search-forward "\n\n" nil t
))
256 (let ((end (point-marker)))
257 (goto-char (point-min))
258 (while (re-search-forward rmail-fields-not-to-output end t
)
260 (delete-region (point)
261 (progn (forward-line 1) (point)))))))))
263 ;;; There are functions elsewhere in Emacs that use this function;
264 ;;; look at them before you change the calling method.
266 (defun rmail-output (file-name &optional count noattribute from-gnus
)
267 "Append this message to system-inbox-format mail file named FILE-NAME.
268 A prefix argument COUNT says to output that many consecutive messages,
269 starting with the current one. Deleted messages are skipped and don't count.
270 When called from lisp code, COUNT may be omitted and defaults to 1.
272 If the pruned message header is shown on the current message, then
273 messages will be appended with pruned headers; otherwise, messages
274 will be appended with their original headers.
276 The default file name comes from `rmail-default-file',
277 which is updated to the name you use in this command.
279 The optional third argument NOATTRIBUTE, if non-nil, says not
280 to set the `filed' attribute, and not to display a message.
282 The optional fourth argument FROM-GNUS is set when called from GNUS."
284 (list (rmail-output-read-file-name)
285 (prefix-numeric-value current-prefix-arg
)))
286 (or count
(setq count
1))
288 (expand-file-name file-name
289 (and rmail-default-file
290 (file-name-directory rmail-default-file
))))
291 (if (and (file-readable-p file-name
) (mail-file-babyl-p file-name
))
292 (rmail-output-to-rmail-file file-name count
)
293 (set-buffer rmail-buffer
)
294 (let ((orig-count count
)
295 (rmailbuf (current-buffer))
297 (tembuf (get-buffer-create " rmail-output"))
302 (narrow-to-region (rmail-msgbeg rmail-current-message
) (point-max))
303 (goto-char (point-min))
305 (= (following-char) ?
0)))))
307 mail-from mime-version content-type
)
309 ;; Preserve the Mail-From and MIME-Version fields
310 ;; even if they have been pruned.
315 (goto-char (rmail-msgbeg rmail-current-message
))
316 (setq header-beginning
(point))
317 (search-forward "\n*** EOOH ***\n")
318 (narrow-to-region header-beginning
(point))
319 (setq mail-from
(mail-fetch-field "Mail-From"))
320 (unless rmail-enable-mime
321 (setq mime-version
(mail-fetch-field "MIME-Version")
322 content-type
(mail-fetch-field "Content-type"))))))
326 (insert-buffer-substring rmailbuf
)
327 (when rmail-enable-mime
328 (if original-headers-p
329 (delete-region (goto-char (point-min))
330 (if (search-forward "\n*** EOOH ***\n")
332 (goto-char (point-min))
334 (delete-region (point-min)(point))
335 (search-forward "\n*** EOOH ***\n")
336 (delete-region (match-beginning 0)
337 (if (search-forward "\n\n")
338 (1- (match-end 0)))))
339 (setq buffer-file-coding-system
(or rmail-file-coding-system
341 (rmail-delete-unwanted-fields t
)
342 (or (bolp) (insert "\n"))
343 (goto-char (point-min))
345 (insert mail-from
"\n")
347 (mail-strip-quoted-names (or (mail-fetch-field "from")
348 (mail-fetch-field "really-from")
349 (mail-fetch-field "sender")
351 " " (current-time-string) "\n"))
353 (insert "MIME-Version: " mime-version
)
354 ;; Some malformed MIME messages set content-type to nil.
356 (insert "\nContent-type: " content-type
"\n")))
357 ;; ``Quote'' "\nFrom " as "\n>From "
358 ;; (note that this isn't really quoting, as there is no requirement
359 ;; that "\n[>]+From " be quoted in the same transparent way.)
360 (let ((case-fold-search nil
))
361 (while (search-forward "\nFrom " nil t
)
364 (write-region (point-min) (point-max) file-name t
365 (if noattribute
'nomsg
)))
367 (if (equal major-mode
'rmail-mode
)
368 (rmail-set-attribute "filed" t
)))
369 (setq count
(1- count
))
371 (let ((next-message-p
372 (if rmail-delete-after-output
373 (rmail-delete-forward)
375 (rmail-next-undeleted-message 1))))
376 (num-appended (- orig-count count
)))
377 (if (and next-message-p original-headers-p
)
378 (rmail-toggle-header))
379 (if (and (> count
0) (not next-message-p
))
383 (set-buffer rmailbuf
)
384 (format "Only %d message%s appended" num-appended
385 (if (= num-appended
1) "" "s"))))
387 (kill-buffer tembuf
))))
390 (defun rmail-output-body-to-file (file-name)
391 "Write this message body to the file FILE-NAME.
392 FILE-NAME defaults, interactively, from the Subject field of the message."
395 (or (mail-fetch-field "Subject")
396 rmail-default-body-file
)))
397 (list (setq rmail-default-body-file
399 "Output message body to file: "
400 (and default-file
(file-name-directory default-file
))
402 nil default-file
)))))
404 (expand-file-name file-name
405 (and rmail-default-body-file
406 (file-name-directory rmail-default-body-file
))))
408 (goto-char (point-min))
409 (search-forward "\n\n")
410 (and (file-exists-p file-name
)
411 (not (y-or-n-p (format "File %s exists; overwrite? " file-name
)))
412 (error "Operation aborted"))
413 (write-region (point) (point-max) file-name
)
414 (if (equal major-mode
'rmail-mode
)
415 (rmail-set-attribute "stored" t
)))
416 (if rmail-delete-after-output
417 (rmail-delete-forward)))
419 ;;; arch-tag: 447117c6-1a9a-4b88-aa43-3101b043e3a4
420 ;;; rmailout.el ends here