Delete file local var.
[emacs/old-mirror.git] / lisp / mail / rmailout.el
blobc2354ad9602cab40f867890dc0eb55c307c4cc9d
1 ;;; rmailout.el --- "RMAIL" mail reader for Emacs: output message to a file
3 ;; Copyright (C) 1985, 1987, 1993, 1994, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008, 2009 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 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;;; Code:
28 (require 'rmail)
29 (provide 'rmailout)
31 ;;;###autoload
32 (defcustom rmail-output-decode-coding nil
33 "*If non-nil, do coding system decoding when outputting message as Babyl."
34 :type '(choice (const :tag "on" t)
35 (const :tag "off" nil))
36 :group 'rmail)
38 ;;;###autoload
39 (defcustom rmail-output-file-alist nil
40 "*Alist matching regexps to suggested output Rmail files.
41 This is a list of elements of the form (REGEXP . NAME-EXP).
42 The suggestion is taken if REGEXP matches anywhere in the message buffer.
43 NAME-EXP may be a string constant giving the file name to use,
44 or more generally it may be any kind of expression that returns
45 a file name as a string."
46 :type '(repeat (cons regexp
47 (choice :value ""
48 (string :tag "File Name")
49 sexp)))
50 :group 'rmail-output)
52 (defun rmail-output-read-file-name ()
53 "Read the file name to use for `rmail-output'.
54 Set `rmail-default-file' to this name as well as returning it."
55 (let ((default-file
56 (let (answer tail)
57 (setq tail rmail-output-file-alist)
58 ;; Suggest a file based on a pattern match.
59 (while (and tail (not answer))
60 (save-excursion
61 (goto-char (point-min))
62 (if (re-search-forward (car (car tail)) nil t)
63 (setq answer (eval (cdr (car tail)))))
64 (setq tail (cdr tail))))
65 ;; If no suggestion, use same file as last time.
66 (or answer rmail-default-file))))
67 (let ((read-file
68 (expand-file-name
69 (read-file-name
70 (concat "Output message to mail file (default "
71 (file-name-nondirectory default-file)
72 "): ")
73 (file-name-directory default-file)
74 (abbreviate-file-name default-file))
75 (file-name-directory default-file))))
76 (setq rmail-default-file
77 (if (file-directory-p read-file)
78 (expand-file-name (file-name-nondirectory default-file)
79 read-file)
80 (expand-file-name
81 (or read-file (file-name-nondirectory default-file))
82 (file-name-directory default-file)))))))
84 ;;;###autoload
85 (defcustom rmail-fields-not-to-output nil
86 "*Regexp describing fields to exclude when outputting a message to a file."
87 :type '(choice (const :tag "None" nil)
88 regexp)
89 :group 'rmail-output)
91 ;; Delete from the buffer header fields we don't want output.
92 ;; Buffer should be pre-narrowed to the header.
93 ;; PRESERVE is a regexp for fields NEVER to delete.
94 (defun rmail-delete-unwanted-fields (preserve)
95 (if rmail-fields-not-to-output
96 (save-excursion
97 (goto-char (point-min))
98 (while (re-search-forward rmail-fields-not-to-output nil t)
99 (beginning-of-line)
100 (unless (looking-at preserve)
101 (delete-region (point)
102 (progn (forward-line 1) (point))))))))
104 (defun rmail-output-as-babyl (file-name nomsg)
105 "Convert the current buffer's text to Babyl and output to FILE-NAME.
106 It alters the current buffer's text, so it should be a temp buffer."
107 (let ((coding-system-for-write
108 'emacs-mule-unix))
109 (save-restriction
110 (goto-char (point-min))
111 (search-forward "\n\n" nil 'move)
112 (narrow-to-region (point-min) (point))
113 (if rmail-fields-not-to-output
114 (rmail-delete-unwanted-fields nil)))
116 ;; Convert to Babyl format.
117 (rmail-convert-to-babyl-format)
118 ;; Write it into the file, or its buffer.
119 (let ((buf (find-buffer-visiting file-name))
120 (tembuf (current-buffer)))
121 (if (null buf)
122 (write-region (point-min) (point-max) file-name t nomsg)
123 (if (eq buf (current-buffer))
124 (error "Can't output message to same file it's already in"))
125 ;; File has been visited, in buffer BUF.
126 (set-buffer buf)
127 (let ((inhibit-read-only t)
128 (msg (with-no-warnings
129 (and (boundp 'rmail-current-message)
130 rmail-current-message))))
131 ;; If MSG is non-nil, buffer is in RMAIL mode.
132 (if msg
133 (rmail-output-to-r-mail-buffer tembuf msg)
134 ;; Output file not in rmail mode => just insert at the end.
135 (narrow-to-region (point-min) (1+ (buffer-size)))
136 (goto-char (point-max))
137 (insert-buffer-substring tembuf)))))))
139 ;; When Rmail is really installed, if we delete or rename the old Rmail
140 ;; we should do likewise with this function.
142 (defun rmail-output-to-r-mail-buffer (tembuf msg)
143 "Copy msg in TEMBUF from BEG to END into this old R-mail BABYL buffer.
144 Do what is necessary to make babyl R-mail know about the new message.
145 Then display message number MSG."
146 (with-no-warnings
147 ;; Turn on Auto Save mode, if it's off in this
148 ;; buffer but enabled by default.
149 (and (not buffer-auto-save-file-name)
150 auto-save-default
151 (auto-save-mode t))
152 (rmail-maybe-set-message-counters)
153 (widen)
154 (narrow-to-region (point-max) (point-max))
155 (insert-buffer-substring tembuf)
156 (goto-char (point-min))
157 (widen)
158 (search-backward "\n\^_")
159 (narrow-to-region (point) (point-max))
160 (rmail-count-new-messages t)
161 (if (rmail-summary-exists)
162 (rmail-select-summary
163 (rmail-update-summary)))
164 (rmail-show-message msg)))
166 (defun rmail-convert-to-babyl-format ()
167 (let ((count 0) (start (point-min))
168 (case-fold-search nil)
169 (buffer-undo-list t))
170 (goto-char (point-min))
171 (save-restriction
172 (unless (looking-at "^From ")
173 (error "Invalid mbox message"))
174 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
175 (rmail-nuke-pinhead-header)
176 ;; Decode base64 or quoted printable contents, Rmail style.
177 (let* ((header-end (save-excursion
178 (and (re-search-forward "\n\n" nil t)
179 (1- (point)))))
180 (case-fold-search t)
181 (quoted-printable-header-field-end
182 (save-excursion
183 (re-search-forward
184 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
185 header-end t)))
186 (base64-header-field-end
187 (and
188 ;; Don't decode non-text data.
189 (save-excursion
190 (re-search-forward
191 "^content-type:\\(\n?[\t ]\\)\\(text\\|message\\)/"
192 header-end t))
193 (save-excursion
194 (re-search-forward
195 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
196 header-end t)))))
198 (goto-char (point-max))
199 (if quoted-printable-header-field-end
200 (save-excursion
201 (unless (mail-unquote-printable-region
202 header-end (point) nil t t)
203 (message "Malformed MIME quoted-printable message"))
204 ;; Change "quoted-printable" to "8bit",
205 ;; to reflect the decoding we just did.
206 (goto-char quoted-printable-header-field-end)
207 (delete-region (point) (search-backward ":"))
208 (insert ": 8bit")))
209 (if base64-header-field-end
210 (save-excursion
211 (when (condition-case nil
212 (progn
213 (base64-decode-region
214 (1+ header-end)
215 (save-excursion
216 ;; Prevent base64-decode-region
217 ;; from removing newline characters.
218 (skip-chars-backward "\n\t ")
219 (point)))
221 (error nil))
222 ;; Change "base64" to "8bit", to reflect the
223 ;; decoding we just did.
224 (goto-char base64-header-field-end)
225 (delete-region (point) (search-backward ":"))
226 (insert ": 8bit")))))
227 ;; Transform anything within the message text
228 ;; that might appear to be the end of a Babyl-format message.
229 (save-excursion
230 (save-restriction
231 (narrow-to-region start (point))
232 (goto-char (point-min))
233 (while (search-forward "\n\^_" nil t) ; single char
234 (replace-match "\n^_")))) ; 2 chars: "^" and "_"
235 ;; This is for malformed messages that don't end in newline.
236 ;; There shouldn't be any, but some users say occasionally
237 ;; there are some.
238 (or (bolp) (newline))
239 (insert ?\^_)
240 (setq last-coding-system-used nil)
241 ;; Decode coding system, following specs in the message header,
242 ;; and record what coding system was decoded.
243 (if rmail-output-decode-coding
244 (let ((mime-charset
245 (if (save-excursion
246 (goto-char start)
247 (search-forward "\n\n" nil t)
248 (let ((case-fold-search t))
249 (re-search-backward
250 rmail-mime-charset-pattern
251 start t)))
252 (intern (downcase (match-string 1))))))
253 (rmail-decode-region start (point) mime-charset)))
254 (save-excursion
255 (goto-char start)
256 (forward-line 3)
257 (insert "X-Coding-System: "
258 (symbol-name last-coding-system-used)
259 "\n")))))
261 ;; Delete the "From ..." line, creating various other headers with
262 ;; information from it if they don't already exist. Now puts the
263 ;; original line into a mail-from: header line for debugging and for
264 ;; use by the rmail-output function.
265 (defun rmail-nuke-pinhead-header ()
266 (save-excursion
267 (save-restriction
268 (let ((start (point))
269 (end (progn
270 (condition-case ()
271 (search-forward "\n\n")
272 (error
273 (goto-char (point-max))
274 (insert "\n\n")))
275 (point)))
276 has-from has-date)
277 (narrow-to-region start end)
278 (let ((case-fold-search t))
279 (goto-char start)
280 (setq has-from (search-forward "\nFrom:" nil t))
281 (goto-char start)
282 (setq has-date (and (search-forward "\nDate:" nil t) (point)))
283 (goto-char start))
284 (let ((case-fold-search nil))
285 (if (re-search-forward (concat "^" rmail-unix-mail-delimiter) nil t)
286 (replace-match
287 (concat
288 "Mail-from: \\&"
289 ;; Keep and reformat the date if we don't
290 ;; have a Date: field.
291 (if has-date
293 (concat
294 "Date: \\2, \\4 \\3 \\9 \\5 "
296 ;; The timezone could be matched by group 7 or group 10.
297 ;; If neither of them matched, assume EST, since only
298 ;; Easterners would be so sloppy.
299 ;; It's a shame the substitution can't use "\\10".
300 (cond
301 ((/= (match-beginning 7) (match-end 7)) "\\7")
302 ((/= (match-beginning 10) (match-end 10))
303 (buffer-substring (match-beginning 10)
304 (match-end 10)))
305 (t "EST"))
306 "\n"))
307 ;; Keep and reformat the sender if we don't
308 ;; have a From: field.
309 (if has-from
311 "From: \\1\n"))
312 t)))))))
314 (defun rmail-output-as-mbox (file-name nomsg &optional as-seen)
315 "Convert the current buffer's text to mbox Babyl and output to FILE-NAME.
316 It alters the current buffer's text, so call with a temp buffer current.
317 If FILE-NAME is visited, output into its buffer instead.
318 AS-SEEN is non-nil if we are copying the message \"as seen\"."
319 (let ((case-fold-search t)
320 mail-from mime-version content-type)
322 ;; Preserve the Mail-From and MIME-Version fields
323 ;; even if they have been pruned.
324 (search-forward "\n\n" nil 'move)
325 (narrow-to-region (point-min) (point))
327 (rmail-delete-unwanted-fields
328 (if rmail-enable-mime "Mail-From"
329 "Mail-From\\|MIME-Version\\|Content-type"))
331 (widen)
333 ;; Make sure message ends with blank line.
334 (goto-char (point-max))
335 (unless (bolp)
336 (insert "\n"))
337 (unless (looking-back "\n\n")
338 (insert "\n"))
340 ;; Generate a From line from other header fields
341 ;; if necessary.
342 (goto-char (point-min))
343 (unless (looking-at "From ")
344 (insert "From "
345 (mail-strip-quoted-names
346 (save-excursion
347 (save-restriction
348 (goto-char (point-min))
349 (narrow-to-region
350 (point)
351 (or (search-forward "\n\n" nil)
352 (point-max)))
353 (or (mail-fetch-field "from")
354 (mail-fetch-field "really-from")
355 (mail-fetch-field "sender")
356 "unknown"))))
357 " " (current-time-string) "\n"))
359 (let ((buf (find-buffer-visiting file-name))
360 (tembuf (current-buffer)))
361 (if (null buf)
362 (let ((coding-system-for-write 'raw-text-unix))
363 (write-region (point-min) (point-max) file-name t nomsg))
364 (if (eq buf (current-buffer))
365 (error "Can't output message to same file it's already in"))
366 ;; File has been visited, in buffer BUF.
367 (set-buffer buf)
368 (let ((inhibit-read-only t)
369 (msg (and (boundp 'rmail-current-message)
370 rmail-current-message)))
371 (and msg as-seen
372 (error "Can't output \"as seen\" to a visited Rmail file"))
373 (if msg
374 (rmail-output-to-rmail-buffer tembuf msg)
375 ;; Output file not in Rmail mode => just insert at the end.
376 (narrow-to-region (point-min) (1+ (buffer-size)))
377 (goto-char (point-max))
378 (insert-buffer-substring tembuf)))))))
380 ;; Called only if rmail-summary-exists, which means rmailsum is loaded.
381 (declare-function rmail-update-summary "rmailsum" (&rest ignore))
383 (defun rmail-output-to-rmail-buffer (tembuf msg)
384 "Copy msg in TEMBUF from BEG to END into this Rmail buffer.
385 Do what is necessary to make Rmail know about the new message.
386 Then display message number MSG."
387 (save-excursion
388 (rmail-swap-buffers-maybe)
389 ;; Turn on Auto Save mode, if it's off in this
390 ;; buffer but enabled by default.
391 (and (not buffer-auto-save-file-name)
392 auto-save-default
393 (auto-save-mode t))
394 (rmail-maybe-set-message-counters)
395 (narrow-to-region (point-max) (point-max))
396 (insert-buffer-substring tembuf)
397 (rmail-count-new-messages t)
398 (if (rmail-summary-exists)
399 (rmail-select-summary
400 (rmail-update-summary)))
401 (rmail-show-message msg)))
403 ;;; There are functions elsewhere in Emacs that use this function;
404 ;;; look at them before you change the calling method.
405 ;;;###autoload
406 (defun rmail-output (file-name &optional count noattribute from-gnus)
407 "Append this message to mail file FILE-NAME.
408 This works with both mbox format and Babyl format files,
409 outputting in the appropriate format for each.
410 The default file name comes from `rmail-default-file',
411 which is updated to the name you use in this command.
413 A prefix argument COUNT says to output that many consecutive messages,
414 starting with the current one. Deleted messages are skipped and don't count.
415 When called from Lisp code, COUNT may be omitted and defaults to 1.
417 This command always outputs the complete message header,
418 even the header display is currently pruned.
420 The optional third argument NOATTRIBUTE, if non-nil, says not
421 to set the `filed' attribute, and not to display a message.
423 The optional fourth argument FROM-GNUS is set when called from GNUS."
424 (interactive
425 (list (rmail-output-read-file-name)
426 (prefix-numeric-value current-prefix-arg)))
427 (or count (setq count 1))
428 (setq file-name
429 (expand-file-name file-name
430 (and rmail-default-file
431 (file-name-directory rmail-default-file))))
433 ;; Warn about creating new file.
434 (or (find-buffer-visiting file-name)
435 (file-exists-p file-name)
436 (yes-or-no-p
437 (concat "\"" file-name "\" does not exist, create it? "))
438 (error "Output file does not exist"))
440 (set-buffer rmail-buffer)
442 (let ((orig-count count)
443 (case-fold-search t)
444 (tembuf (get-buffer-create " rmail-output"))
445 (babyl-format
446 (and (file-readable-p file-name) (mail-file-babyl-p file-name))))
448 (unwind-protect
449 (while (> count 0)
450 (with-current-buffer rmail-buffer
451 (let (cur beg end)
452 (setq beg (rmail-msgbeg rmail-current-message)
453 end (rmail-msgend rmail-current-message))
454 ;; All access to the buffer's local variables is now finished...
455 (save-excursion
456 ;; ... so it is ok to go to a different buffer.
457 (if (rmail-buffers-swapped-p) (set-buffer rmail-view-buffer))
458 (setq cur (current-buffer))
459 (save-restriction
460 (widen)
461 (with-current-buffer tembuf
462 (insert-buffer-substring cur beg end)
463 ;; Convert the text to one format or another and output.
464 (if babyl-format
465 (rmail-output-as-babyl file-name (if noattribute 'nomsg))
466 (rmail-output-as-mbox file-name
467 (if noattribute 'nomsg))))))))
469 ;; Mark message as "filed".
470 (unless noattribute
471 (rmail-set-attribute rmail-filed-attr-index t))
473 (setq count (1- count))
475 (or from-gnus
476 (let ((next-message-p
477 (if rmail-delete-after-output
478 (rmail-delete-forward)
479 (if (> count 0)
480 (rmail-next-undeleted-message 1))))
481 (num-appended (- orig-count count)))
482 (if (and (> count 0) (not next-message-p))
483 (error "Only %d message%s appended" num-appended
484 (if (= num-appended 1) "" "s"))))))
485 (kill-buffer tembuf))))
487 (defun rmail-output-as-seen (file-name &optional count noattribute from-gnus)
488 "Append this message to system-inbox-format mail file named FILE-NAME.
489 A prefix argument COUNT says to output that many consecutive messages,
490 starting with the current one. Deleted messages are skipped and don't count.
491 When called from Lisp code, COUNT may be omitted and defaults to 1.
493 This outputs the message header as you see it.
495 The default file name comes from `rmail-default-file',
496 which is updated to the name you use in this command.
498 The optional third argument NOATTRIBUTE, if non-nil, says not
499 to set the `filed' attribute, and not to display a message.
501 The optional fourth argument FROM-GNUS is set when called from GNUS."
502 (interactive
503 (list (rmail-output-read-file-name)
504 (prefix-numeric-value current-prefix-arg)))
505 (or count (setq count 1))
506 (setq file-name
507 (expand-file-name file-name
508 (and rmail-default-file
509 (file-name-directory rmail-default-file))))
510 (set-buffer rmail-buffer)
512 ;; Warn about creating new file.
513 (or (find-buffer-visiting file-name)
514 (file-exists-p file-name)
515 (yes-or-no-p
516 (concat "\"" file-name "\" does not exist, create it? "))
517 (error "Output file does not exist"))
519 (if (and (file-readable-p file-name) (mail-file-babyl-p file-name))
520 (error "Cannot output `as seen' to a Babyl file"))
522 (let ((orig-count count)
523 (case-fold-search t)
524 (tembuf (get-buffer-create " rmail-output")))
526 (unwind-protect
527 (while (> count 0)
528 (let (cur beg end)
529 ;; If operating from whole-mbox buffer, get message bounds.
530 (if (not (rmail-buffers-swapped-p))
531 (setq beg (rmail-msgbeg rmail-current-message)
532 end (rmail-msgend rmail-current-message)))
533 ;; All access to the buffer's local variables is now finished...
534 (save-excursion
535 (setq cur (current-buffer))
536 (save-restriction
537 (widen)
538 ;; If operating from the view buffer, get the bounds.
539 (unless beg
540 (setq beg (point-min)
541 end (point-max)))
543 (with-current-buffer tembuf
544 (insert-buffer-substring cur beg end)
545 ;; Convert the text to one format or another and output.
546 (rmail-output-as-mbox file-name
547 (if noattribute 'nomsg)
548 t)))))
550 ;; Mark message as "filed".
551 (unless noattribute
552 (rmail-set-attribute rmail-filed-attr-index t))
554 (setq count (1- count))
556 (or from-gnus
557 (let ((next-message-p
558 (if rmail-delete-after-output
559 (rmail-delete-forward)
560 (if (> count 0)
561 (rmail-next-undeleted-message 1))))
562 (num-appended (- orig-count count)))
563 (if (and (> count 0) (not next-message-p))
564 (error "Only %d message%s appended" num-appended
565 (if (= num-appended 1) "" "s"))))))
566 (kill-buffer tembuf))))
569 ;;;###autoload
570 (defun rmail-output-body-to-file (file-name)
571 "Write this message body to the file FILE-NAME.
572 FILE-NAME defaults, interactively, from the Subject field of the message."
573 (interactive
574 (let ((default-file
575 (or (mail-fetch-field "Subject")
576 rmail-default-body-file)))
577 (list (setq rmail-default-body-file
578 (read-file-name
579 "Output message body to file: "
580 (and default-file (file-name-directory default-file))
581 default-file
582 nil default-file)))))
583 (setq file-name
584 (expand-file-name file-name
585 (and rmail-default-body-file
586 (file-name-directory rmail-default-body-file))))
587 (save-excursion
588 (goto-char (point-min))
589 (search-forward "\n\n")
590 (and (file-exists-p file-name)
591 (not (y-or-n-p (format "File %s exists; overwrite? " file-name)))
592 (error "Operation aborted"))
593 (write-region (point) (point-max) file-name))
594 (if rmail-delete-after-output
595 (rmail-delete-forward)))
597 ;; arch-tag: 4059abf0-f249-4be4-8e0d-602d370d01d1
598 ;;; rmailout.el ends here