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 (declare-function rmail-update-summary
"rmailsum" (&rest ignore
))
114 ;;; There are functions elsewhere in Emacs that use this function;
115 ;;; look at them before you change the calling method.
117 (defun rmail-output-to-rmail-file (file-name &optional count stay
)
118 "Append the current message to an Rmail file named FILE-NAME.
119 If the file does not exist, ask if it should be created.
120 If file is being visited, the message is appended to the Emacs
121 buffer visiting that file.
122 If the file exists and is not an Rmail file, the message is
123 appended in inbox format, the same way `rmail-output' does it.
125 The default file name comes from `rmail-default-rmail-file',
126 which is updated to the name you use in this command.
128 A prefix argument COUNT says to output that many consecutive messages,
129 starting with the current one. Deleted messages are skipped and don't count.
131 If the optional argument STAY is non-nil, then leave the last filed
132 message up instead of moving forward to the next non-deleted message."
134 (list (rmail-output-read-rmail-file-name)
135 (prefix-numeric-value current-prefix-arg
)))
136 (or count
(setq count
1))
138 (expand-file-name file-name
139 (file-name-directory rmail-default-rmail-file
)))
140 (if (and (file-readable-p file-name
) (not (mail-file-babyl-p file-name
)))
141 (rmail-output file-name count
)
142 (rmail-maybe-set-message-counters)
143 (setq file-name
(abbreviate-file-name file-name
))
144 (or (find-buffer-visiting file-name
)
145 (file-exists-p file-name
)
147 (concat "\"" file-name
"\" does not exist, create it? "))
148 (let ((file-buffer (create-file-buffer file-name
)))
150 (set-buffer file-buffer
)
151 (rmail-insert-rmail-file-header)
152 (let ((require-final-newline nil
)
153 (coding-system-for-write
154 (or rmail-file-coding-system
156 (write-region (point-min) (point-max) file-name t
1)))
157 (kill-buffer file-buffer
))
158 (error "Output file does not exist")))
163 (set-buffer rmail-buffer
)
164 ;; Temporarily turn off Deleted attribute.
165 ;; Do this outside the save-restriction, since it would
166 ;; shift the place in the buffer where the visible text starts.
167 (if (rmail-message-deleted-p rmail-current-message
)
168 (progn (setq redelete t
)
169 (rmail-set-attribute "deleted" nil
)))
172 ;; Decide whether to append to a file or to an Emacs buffer.
174 (let ((buf (find-buffer-visiting file-name
))
175 (cur (current-buffer))
176 (beg (1+ (rmail-msgbeg rmail-current-message
)))
177 (end (1+ (rmail-msgend rmail-current-message
)))
178 (coding-system-for-write
179 (or rmail-file-coding-system
183 (if rmail-fields-not-to-output
184 ;; Delete some fields while we output.
185 (let ((obuf (current-buffer)))
186 (set-buffer (get-buffer-create " rmail-out-temp"))
187 (insert-buffer-substring obuf beg end
)
188 (rmail-delete-unwanted-fields)
189 (append-to-file (point-min) (point-max) file-name
)
191 (kill-buffer (get-buffer " rmail-out-temp")))
192 (append-to-file beg end file-name
))
193 (if (eq buf
(current-buffer))
194 (error "Can't output message to same file it's already in"))
195 ;; File has been visited, in buffer BUF.
197 (let ((buffer-read-only nil
)
198 (msg (and (boundp 'rmail-current-message
)
199 rmail-current-message
)))
200 ;; If MSG is non-nil, buffer is in RMAIL mode.
203 ;; Turn on auto save mode, if it's off in this
204 ;; buffer but enabled by default.
205 (and (not buffer-auto-save-file-name
)
208 (rmail-maybe-set-message-counters)
210 (narrow-to-region (point-max) (point-max))
211 (insert-buffer-substring cur beg end
)
212 (goto-char (point-min))
214 (search-backward "\n\^_")
215 (narrow-to-region (point) (point-max))
216 (rmail-delete-unwanted-fields)
217 (rmail-count-new-messages t
)
218 (if (rmail-summary-exists)
219 (rmail-select-summary
220 (rmail-update-summary)))
221 (rmail-show-message msg
))
222 ;; Output file not in rmail mode => just insert at the end.
223 (narrow-to-region (point-min) (1+ (buffer-size)))
224 (goto-char (point-max))
225 (insert-buffer-substring cur beg end
)
226 (rmail-delete-unwanted-fields)))))))
227 (rmail-set-attribute "filed" t
))
228 (if redelete
(rmail-set-attribute "deleted" t
))))
229 (setq count
(1- count
))
230 (if rmail-delete-after-output
232 (if (and (= count
0) stay
)
233 (rmail-delete-message)
234 (rmail-delete-forward))
238 (if (not stay
) (rmail-next-undeleted-message 1))
242 (defcustom rmail-fields-not-to-output nil
243 "*Regexp describing fields to exclude when outputting a message to a file."
244 :type
'(choice (const :tag
"None" nil
)
246 :group
'rmail-output
)
248 ;; Delete from the buffer header fields we don't want output.
249 ;; NOT-RMAIL if t means this buffer does not have the full header
250 ;; and *** EOOH *** that a message in an Rmail file has.
251 (defun rmail-delete-unwanted-fields (&optional not-rmail
)
252 (if rmail-fields-not-to-output
254 (goto-char (point-min))
255 ;; Find the end of the header.
256 (if (and (or not-rmail
(search-forward "\n*** EOOH ***\n" nil t
))
257 (search-forward "\n\n" nil t
))
258 (let ((end (point-marker)))
259 (goto-char (point-min))
260 (while (re-search-forward rmail-fields-not-to-output end t
)
262 (delete-region (point)
263 (progn (forward-line 1) (point)))))))))
265 ;;; There are functions elsewhere in Emacs that use this function;
266 ;;; look at them before you change the calling method.
268 (defun rmail-output (file-name &optional count noattribute from-gnus
)
269 "Append this message to system-inbox-format mail file named FILE-NAME.
270 A prefix argument COUNT says to output that many consecutive messages,
271 starting with the current one. Deleted messages are skipped and don't count.
272 When called from lisp code, COUNT may be omitted and defaults to 1.
274 If the pruned message header is shown on the current message, then
275 messages will be appended with pruned headers; otherwise, messages
276 will be appended with their original headers.
278 The default file name comes from `rmail-default-file',
279 which is updated to the name you use in this command.
281 The optional third argument NOATTRIBUTE, if non-nil, says not
282 to set the `filed' attribute, and not to display a message.
284 The optional fourth argument FROM-GNUS is set when called from GNUS."
286 (list (rmail-output-read-file-name)
287 (prefix-numeric-value current-prefix-arg
)))
288 (or count
(setq count
1))
290 (expand-file-name file-name
291 (and rmail-default-file
292 (file-name-directory rmail-default-file
))))
293 (if (and (file-readable-p file-name
) (mail-file-babyl-p file-name
))
294 (rmail-output-to-rmail-file file-name count
)
295 (set-buffer rmail-buffer
)
296 (let ((orig-count count
)
297 (rmailbuf (current-buffer))
299 (tembuf (get-buffer-create " rmail-output"))
304 (narrow-to-region (rmail-msgbeg rmail-current-message
) (point-max))
305 (goto-char (point-min))
307 (= (following-char) ?
0)))))
309 mail-from mime-version content-type
)
311 ;; Preserve the Mail-From and MIME-Version fields
312 ;; even if they have been pruned.
317 (goto-char (rmail-msgbeg rmail-current-message
))
318 (setq header-beginning
(point))
319 (search-forward "\n*** EOOH ***\n")
320 (narrow-to-region header-beginning
(point))
321 (setq mail-from
(mail-fetch-field "Mail-From"))
322 (unless rmail-enable-mime
323 (setq mime-version
(mail-fetch-field "MIME-Version")
324 content-type
(mail-fetch-field "Content-type"))))))
328 (insert-buffer-substring rmailbuf
)
329 (when rmail-enable-mime
330 (if original-headers-p
331 (delete-region (goto-char (point-min))
332 (if (search-forward "\n*** EOOH ***\n")
334 (goto-char (point-min))
336 (delete-region (point-min)(point))
337 (search-forward "\n*** EOOH ***\n")
338 (delete-region (match-beginning 0)
339 (if (search-forward "\n\n")
340 (1- (match-end 0)))))
341 (setq buffer-file-coding-system
(or rmail-file-coding-system
343 (rmail-delete-unwanted-fields t
)
344 (or (bolp) (insert "\n"))
345 (goto-char (point-min))
347 (insert mail-from
"\n")
349 (mail-strip-quoted-names (or (mail-fetch-field "from")
350 (mail-fetch-field "really-from")
351 (mail-fetch-field "sender")
353 " " (current-time-string) "\n"))
355 (insert "MIME-Version: " mime-version
)
356 ;; Some malformed MIME messages set content-type to nil.
358 (insert "\nContent-type: " content-type
"\n")))
359 ;; ``Quote'' "\nFrom " as "\n>From "
360 ;; (note that this isn't really quoting, as there is no requirement
361 ;; that "\n[>]+From " be quoted in the same transparent way.)
362 (let ((case-fold-search nil
))
363 (while (search-forward "\nFrom " nil t
)
366 (write-region (point-min) (point-max) file-name t
367 (if noattribute
'nomsg
)))
369 (if (equal major-mode
'rmail-mode
)
370 (rmail-set-attribute "filed" t
)))
371 (setq count
(1- count
))
373 (let ((next-message-p
374 (if rmail-delete-after-output
375 (rmail-delete-forward)
377 (rmail-next-undeleted-message 1))))
378 (num-appended (- orig-count count
)))
379 (if (and next-message-p original-headers-p
)
380 (rmail-toggle-header))
381 (if (and (> count
0) (not next-message-p
))
385 (set-buffer rmailbuf
)
386 (format "Only %d message%s appended" num-appended
387 (if (= num-appended
1) "" "s"))))
389 (kill-buffer tembuf
))))
392 (defun rmail-output-body-to-file (file-name)
393 "Write this message body to the file FILE-NAME.
394 FILE-NAME defaults, interactively, from the Subject field of the message."
397 (or (mail-fetch-field "Subject")
398 rmail-default-body-file
)))
399 (list (setq rmail-default-body-file
401 "Output message body to file: "
402 (and default-file
(file-name-directory default-file
))
404 nil default-file
)))))
406 (expand-file-name file-name
407 (and rmail-default-body-file
408 (file-name-directory rmail-default-body-file
))))
410 (goto-char (point-min))
411 (search-forward "\n\n")
412 (and (file-exists-p file-name
)
413 (not (y-or-n-p (format "File %s exists; overwrite? " file-name
)))
414 (error "Operation aborted"))
415 (write-region (point) (point-max) file-name
)
416 (if (equal major-mode
'rmail-mode
)
417 (rmail-set-attribute "stored" t
)))
418 (if rmail-delete-after-output
419 (rmail-delete-forward)))
421 ;;; arch-tag: 447117c6-1a9a-4b88-aa43-3101b043e3a4
422 ;;; rmailout.el ends here