(hebrew-calendar-leap-year-p)
[emacs.git] / lisp / mail / rmailout.el
blob482091f716efda9fd1053188fefdcf9b770810a1
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.
6 ;; Maintainer: FSF
7 ;; Keywords: mail
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)
14 ;; any later version.
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.
26 ;;; Commentary:
28 ;;; Code:
30 (require 'rmail)
31 (provide 'rmailout)
33 ;;;###autoload
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
42 (choice :value ""
43 (string :tag "File Name")
44 sexp)))
45 :group 'rmail-output)
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."
50 (let ((default-file
51 (let (answer tail)
52 (setq tail rmail-output-file-alist)
53 ;; Suggest a file based on a pattern match.
54 (while (and tail (not answer))
55 (save-excursion
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)))))
63 (let ((read-file
64 (expand-file-name
65 (read-file-name
66 (concat "Output message to Rmail file (default "
67 (file-name-nondirectory default-file)
68 "): ")
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)
77 read-file)
78 read-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."
83 (let ((default-file
84 (let (answer tail)
85 (setq tail rmail-output-file-alist)
86 ;; Suggest a file based on a pattern match.
87 (while (and tail (not answer))
88 (save-excursion
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))))
95 (let ((read-file
96 (expand-file-name
97 (read-file-name
98 (concat "Output message to Unix mail file (default "
99 (file-name-nondirectory default-file)
100 "): ")
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)
107 read-file)
108 (expand-file-name
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.
116 ;;;###autoload
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."
133 (interactive
134 (list (rmail-output-read-rmail-file-name)
135 (prefix-numeric-value current-prefix-arg)))
136 (or count (setq count 1))
137 (setq file-name
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)
146 (if (yes-or-no-p
147 (concat "\"" file-name "\" does not exist, create it? "))
148 (let ((file-buffer (create-file-buffer file-name)))
149 (save-excursion
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
155 'emacs-mule-unix)))
156 (write-region (point-min) (point-max) file-name t 1)))
157 (kill-buffer file-buffer))
158 (error "Output file does not exist")))
159 (while (> count 0)
160 (let (redelete)
161 (unwind-protect
162 (progn
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)))
170 (save-restriction
171 (widen)
172 ;; Decide whether to append to a file or to an Emacs buffer.
173 (save-excursion
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
180 'emacs-mule-unix)))
181 (if (not buf)
182 ;; Output to a file.
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)
190 (set-buffer obuf)
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.
196 (set-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.
201 (if msg
202 (progn
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)
206 auto-save-default
207 (auto-save-mode t))
208 (rmail-maybe-set-message-counters)
209 (widen)
210 (narrow-to-region (point-max) (point-max))
211 (insert-buffer-substring cur beg end)
212 (goto-char (point-min))
213 (widen)
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
231 (unless
232 (if (and (= count 0) stay)
233 (rmail-delete-message)
234 (rmail-delete-forward))
235 (setq count 0))
236 (if (> count 0)
237 (unless
238 (if (not stay) (rmail-next-undeleted-message 1))
239 (setq count 0)))))))
241 ;;;###autoload
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)
245 regexp)
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
253 (save-excursion
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)
261 (beginning-of-line)
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.
267 ;;;###autoload
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."
285 (interactive
286 (list (rmail-output-read-file-name)
287 (prefix-numeric-value current-prefix-arg)))
288 (or count (setq count 1))
289 (setq file-name
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))
298 (case-fold-search t)
299 (tembuf (get-buffer-create " rmail-output"))
300 (original-headers-p
301 (and (not from-gnus)
302 (save-excursion
303 (save-restriction
304 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
305 (goto-char (point-min))
306 (forward-line 1)
307 (= (following-char) ?0)))))
308 header-beginning
309 mail-from mime-version content-type)
310 (while (> count 0)
311 ;; Preserve the Mail-From and MIME-Version fields
312 ;; even if they have been pruned.
313 (or from-gnus
314 (save-excursion
315 (save-restriction
316 (widen)
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"))))))
325 (save-excursion
326 (set-buffer tembuf)
327 (erase-buffer)
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")
333 (match-end 0)))
334 (goto-char (point-min))
335 (forward-line 2)
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
342 'raw-text)))
343 (rmail-delete-unwanted-fields t)
344 (or (bolp) (insert "\n"))
345 (goto-char (point-min))
346 (if mail-from
347 (insert mail-from "\n")
348 (insert "From "
349 (mail-strip-quoted-names (or (mail-fetch-field "from")
350 (mail-fetch-field "really-from")
351 (mail-fetch-field "sender")
352 "unknown"))
353 " " (current-time-string) "\n"))
354 (when mime-version
355 (insert "MIME-Version: " mime-version)
356 ;; Some malformed MIME messages set content-type to nil.
357 (when content-type
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)
364 (forward-char -5)
365 (insert ?>)))
366 (write-region (point-min) (point-max) file-name t
367 (if noattribute 'nomsg)))
368 (or noattribute
369 (if (equal major-mode 'rmail-mode)
370 (rmail-set-attribute "filed" t)))
371 (setq count (1- count))
372 (or from-gnus
373 (let ((next-message-p
374 (if rmail-delete-after-output
375 (rmail-delete-forward)
376 (if (> count 0)
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))
382 (progn
383 (error "%s"
384 (save-excursion
385 (set-buffer rmailbuf)
386 (format "Only %d message%s appended" num-appended
387 (if (= num-appended 1) "" "s"))))
388 (setq count 0))))))
389 (kill-buffer tembuf))))
391 ;;;###autoload
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."
395 (interactive
396 (let ((default-file
397 (or (mail-fetch-field "Subject")
398 rmail-default-body-file)))
399 (list (setq rmail-default-body-file
400 (read-file-name
401 "Output message body to file: "
402 (and default-file (file-name-directory default-file))
403 default-file
404 nil default-file)))))
405 (setq file-name
406 (expand-file-name file-name
407 (and rmail-default-body-file
408 (file-name-directory rmail-default-body-file))))
409 (save-excursion
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