1 ;;; rmailout.el --- "RMAIL" mail reader for Emacs: output message to a file.
3 ;; Copyright (C) 1985, 1987, 1993 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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;; Temporary until Emacs always has this variable.
27 (defvar rmail-delete-after-output nil
28 "*Non-nil means automatically delete a message that is copied to a file.")
30 (defvar 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 NAME-EXP may be a string constant giving the file name to use,
34 or more generally it may be any kind of expression that returns
35 a file name as a string.")
37 ;;; There are functions elsewhere in Emacs that use this function; check
38 ;;; them out before you change the calling method.
39 (defun rmail-output-to-rmail-file (file-name &optional count
)
40 "Append the current message to an Rmail file named FILE-NAME.
41 If the file does not exist, ask if it should be created.
42 If file is being visited, the message is appended to the Emacs
43 buffer visiting that file.
44 If the file exists and is not an Rmail file,
45 the message is appended in inbox format.
47 A prefix argument N says to output N consecutive messages
48 starting with the current one. Deleted messages are skipped and don't count."
52 (setq tail rmail-output-file-alist
)
53 ;; Suggest a file based on a pattern match.
54 (while (and tail
(not answer
))
56 (goto-char (point-min))
57 (if (re-search-forward (car (car tail
)) nil t
)
58 (setq answer
(eval (cdr (car tail
)))))
59 (setq tail
(cdr tail
))))
60 ;; If not suggestions, use same file as last time.
61 (or answer rmail-last-rmail-file
))))
62 (list (setq rmail-last-rmail-file
64 (concat "Output message to Rmail file: (default "
65 (file-name-nondirectory default-file
)
67 (file-name-directory default-file
)
69 (prefix-numeric-value current-prefix-arg
))))
70 (or count
(setq count
1))
72 (expand-file-name file-name
73 (file-name-directory rmail-last-rmail-file
)))
74 (if (and (file-readable-p file-name
) (not (rmail-file-p file-name
)))
75 (rmail-output file-name count
)
76 (rmail-maybe-set-message-counters)
77 (setq file-name
(abbreviate-file-name file-name
))
78 (or (get-file-buffer file-name
)
79 (file-exists-p file-name
)
81 (concat "\"" file-name
"\" does not exist, create it? "))
82 (let ((file-buffer (create-file-buffer file-name
)))
84 (set-buffer file-buffer
)
85 (rmail-insert-rmail-file-header)
86 (let ((require-final-newline nil
))
87 (write-region (point-min) (point-max) file-name t
1)))
88 (kill-buffer file-buffer
))
89 (error "Output file does not exist")))
96 (if (rmail-message-deleted-p rmail-current-message
)
97 (progn (setq redelete t
)
98 (rmail-set-attribute "deleted" nil
)))
99 ;; Decide whether to append to a file or to an Emacs buffer.
101 (let ((buf (get-file-buffer file-name
))
102 (cur (current-buffer))
103 (beg (1+ (rmail-msgbeg rmail-current-message
)))
104 (end (1+ (rmail-msgend rmail-current-message
))))
106 (append-to-file beg end file-name
)
107 (if (eq buf
(current-buffer))
108 (error "Can't output message to same file it's already in"))
109 ;; File has been visited, in buffer BUF.
111 (let ((buffer-read-only nil
)
112 (msg (and (boundp 'rmail-current-message
)
113 rmail-current-message
)))
114 ;; If MSG is non-nil, buffer is in RMAIL mode.
117 (rmail-maybe-set-message-counters)
119 (narrow-to-region (point-max) (point-max))
120 (insert-buffer-substring cur beg end
)
121 (goto-char (point-min))
123 (search-backward "\n\^_")
124 (narrow-to-region (point) (point-max))
125 (rmail-count-new-messages t
)
126 (rmail-show-message msg
))
127 ;; Output file not in rmail mode => just insert at the end.
128 (narrow-to-region (point-min) (1+ (buffer-size)))
129 (goto-char (point-max))
130 (insert-buffer-substring cur beg end
)))))))
131 (rmail-set-attribute "filed" t
))
132 (if redelete
(rmail-set-attribute "deleted" t
))))
133 (setq count
(1- count
))
134 (if rmail-delete-after-output
135 (rmail-delete-forward)
137 (rmail-next-undeleted-message 1))))))
139 ;; Returns t if file FILE is an Rmail file.
140 (defun rmail-file-p (file)
141 (let ((buf (generate-new-buffer " *rmail-file-p*")))
145 (insert-file-contents file nil
0 100)
146 (looking-at "BABYL OPTIONS:"))
149 ;;; There are functions elsewhere in Emacs that use this function; check
150 ;;; them out before you change the calling method.
151 (defun rmail-output (file-name &optional count noattribute
)
152 "Append this message to Unix mail file named FILE-NAME.
153 A prefix argument N says to output N consecutive messages
154 starting with the current one. Deleted messages are skipped and don't count.
155 When called from lisp code, N may be omitted.
157 If the pruned message header is shown on the current message, then
158 messages will be appended with pruned headers; otherwise, messages
159 will be appended with their original headers.
161 The optional third argument NOATTRIBUTE, if non-nil, says not
162 to set the `filed' attribute, and not to display a message."
164 (list (setq rmail-last-file
166 (concat "Output message to Unix mail file"
169 (file-name-nondirectory rmail-last-file
)
172 (and rmail-last-file
(file-name-directory rmail-last-file
))
174 (prefix-numeric-value current-prefix-arg
)))
175 (or count
(setq count
1))
177 (expand-file-name file-name
179 (file-name-directory rmail-last-file
))))
180 (if (and (file-readable-p file-name
) (rmail-file-p file-name
))
181 (rmail-output-to-rmail-file file-name count
)
182 (let ((orig-count count
)
183 (rmailbuf (current-buffer))
185 (tembuf (get-buffer-create " rmail-output"))
189 (narrow-to-region (rmail-msgbeg rmail-current-message
) (point-max))
190 (goto-char (point-min))
192 (= (following-char) ?
0))))
200 (goto-char (rmail-msgbeg rmail-current-message
))
201 (setq header-beginning
(point))
202 (search-forward "\n*** EOOH ***\n")
203 (narrow-to-region header-beginning
(point))
204 (mail-fetch-field "Mail-From"))))
208 (insert-buffer-substring rmailbuf
)
210 (goto-char (point-min))
212 (insert mail-from
"\n")
214 (mail-strip-quoted-names (or (mail-fetch-field "from")
215 (mail-fetch-field "really-from")
216 (mail-fetch-field "sender")
218 " " (current-time-string) "\n"))
219 ;; ``Quote'' "\nFrom " as "\n>From "
220 ;; (note that this isn't really quoting, as there is no requirement
221 ;; that "\n[>]+From " be quoted in the same transparent way.)
222 (while (search-forward "\nFrom " nil t
)
225 (write-region (point-min) (point-max) file-name t
226 (if noattribute
'nomsg
)))
228 (if (equal major-mode
'rmail-mode
)
229 (rmail-set-attribute "filed" t
)))
230 (setq count
(1- count
))
231 (let ((next-message-p
232 (if rmail-delete-after-output
233 (rmail-delete-forward)
235 (rmail-next-undeleted-message 1))))
236 (num-appended (- orig-count count
)))
237 (if (and next-message-p original-headers-p
)
238 (rmail-toggle-header))
239 (if (and (> count
0) (not next-message-p
))
243 (set-buffer rmailbuf
)
244 (format "Only %d message%s appended" num-appended
245 (if (= num-appended
1) "" "s"))))
247 (kill-buffer tembuf
))))
249 ;;; rmailout.el ends here