Add "Package:" file headers to denote built-in packages.
[emacs.git] / lisp / mail / rmailout.el
blob93d512336dc3b9fafdae33e50929afd416562168
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, 2010 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: mail
8 ;; Package: rmail
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;;; Code:
29 (require 'rmail)
30 (provide 'rmailout)
32 (defcustom rmail-output-decode-coding nil
33 "If non-nil, do coding system decoding when outputting message as Babyl."
34 :type 'boolean
35 :group 'rmail-output)
37 ;; FIXME risky?
38 (defcustom rmail-output-file-alist nil
39 "Alist matching regexps to suggested output Rmail files.
40 This is a list of elements of the form (REGEXP . NAME-EXP).
41 The suggestion is taken if REGEXP matches anywhere in the message buffer.
42 NAME-EXP may be a string constant giving the file name to use,
43 or more generally it may be any kind of expression that returns
44 a file name as a string."
45 :type '(repeat (cons regexp
46 (choice :value ""
47 (string :tag "File Name")
48 sexp)))
49 :group 'rmail-output)
50 ;;;###autoload(put 'rmail-output-file-alist 'risky-local-variable t)
52 (defcustom rmail-fields-not-to-output nil
53 "Regexp describing fields to exclude when outputting a message to a file.
54 The function `rmail-delete-unwanted-fields' uses this, ignoring case."
55 :type '(choice (const :tag "None" nil)
56 regexp)
57 :group 'rmail-output)
59 (defun rmail-output-read-file-name ()
60 "Read the file name to use for `rmail-output'.
61 Set `rmail-default-file' to this name as well as returning it."
62 (let ((default-file
63 (let (answer tail)
64 (setq tail rmail-output-file-alist)
65 ;; Suggest a file based on a pattern match.
66 (while (and tail (not answer))
67 (save-excursion
68 (goto-char (point-min))
69 (if (re-search-forward (car (car tail)) nil t)
70 (setq answer (eval (cdr (car tail)))))
71 (setq tail (cdr tail))))
72 ;; If no suggestion, use same file as last time.
73 (or answer rmail-default-file))))
74 (let ((read-file
75 (expand-file-name
76 (read-file-name
77 (concat "Output message to mail file (default "
78 (file-name-nondirectory default-file)
79 "): ")
80 (file-name-directory default-file)
81 (abbreviate-file-name default-file))
82 (file-name-directory default-file))))
83 (setq rmail-default-file
84 (if (file-directory-p read-file)
85 (expand-file-name (file-name-nondirectory default-file)
86 read-file)
87 (expand-file-name
88 (or read-file (file-name-nondirectory default-file))
89 (file-name-directory default-file)))))))
91 (defun rmail-delete-unwanted-fields (preserve)
92 "Delete all headers matching `rmail-fields-not-to-output'.
93 Retains headers matching the regexp PRESERVE. Ignores case.
94 The buffer should be narrowed to just the header."
95 (if rmail-fields-not-to-output
96 (save-excursion
97 (goto-char (point-min))
98 (let ((case-fold-search t))
99 (while (re-search-forward rmail-fields-not-to-output nil t)
100 (beginning-of-line)
101 (unless (looking-at preserve)
102 (delete-region (point) (line-beginning-position 2))))))))
104 (defun rmail-output-as-babyl (file-name nomsg)
105 "Convert the current buffer's text to Babyl and output to FILE-NAME.
106 Alters the current buffer's text, so it should be a temporary buffer.
107 If a buffer is visiting FILE-NAME, adds the text to that buffer
108 rather than saving the file directly. If the buffer is an Rmail buffer,
109 updates it accordingly. If no buffer is visiting FILE-NAME, appends
110 the text directly to FILE-NAME, and displays a \"Wrote file\" message
111 unless NOMSG is a symbol (neither nil nor t)."
112 (let ((coding-system-for-write 'emacs-mule-unix))
113 (save-restriction
114 (goto-char (point-min))
115 (search-forward "\n\n" nil 'move)
116 (narrow-to-region (point-min) (point))
117 (if rmail-fields-not-to-output
118 (rmail-delete-unwanted-fields nil)))
120 ;; Convert to Babyl format.
121 (rmail-convert-to-babyl-format)
122 ;; Write it into the file, or its buffer.
123 (let ((buf (find-buffer-visiting file-name))
124 (tembuf (current-buffer)))
125 (if (null buf)
126 (write-region (point-min) (point-max) file-name t nomsg)
127 (if (eq buf (current-buffer))
128 (error "Can't output message to same file it's already in"))
129 ;; File has been visited, in buffer BUF.
130 (set-buffer buf)
131 (let ((inhibit-read-only t)
132 (msg (bound-and-true-p rmail-current-message)))
133 ;; If MSG is non-nil, buffer is in RMAIL mode.
134 (if msg
135 (rmail-output-to-babyl-buffer tembuf msg)
136 ;; Output file not in rmail mode => just insert at the end.
137 (narrow-to-region (point-min) (1+ (buffer-size)))
138 (goto-char (point-max))
139 (insert-buffer-substring tembuf)))))))
141 ;; Called only if rmail-summary-exists, which means rmailsum is loaded.
142 (declare-function rmail-update-summary "rmailsum" (&rest ignore))
144 (defun rmail-output-to-babyl-buffer (tembuf msg)
145 "Copy message in TEMBUF into the current Babyl Rmail buffer.
146 Do what is necessary to make Rmail know about the new message, then
147 display message number MSG."
148 ;; Turn on Auto Save mode, if it's off in this 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 (rmail-update-summary)))
163 (rmail-show-message-1 msg))
165 (defun rmail-convert-to-babyl-format ()
166 "Convert the mbox message in the current buffer 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,,\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 (defun rmail-nuke-pinhead-header ()
262 "Delete the \"From \" line in the current mbox message.
263 The variable `rmail-unix-mail-delimiter' specifies the From line format.
264 Replaces the From line with a \"Mail-from\" header. Adds \"Date\" and
265 \"From\" headers if they are not already present."
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 (autoload 'mail-mbox-from "mail-utils")
316 (defun rmail-output-as-mbox (file-name nomsg &optional as-seen)
317 "Convert the current buffer's text to mbox and output to FILE-NAME.
318 Alters the current buffer's text, so it should be a temporary buffer.
319 If a buffer is visiting FILE-NAME, adds the text to that buffer
320 rather than saving the file directly. If the buffer is an Rmail buffer,
321 updates it accordingly. If no buffer is visiting FILE-NAME, appends
322 the text directly to FILE-NAME, and displays a \"Wrote file\" message
323 unless NOMSG is a symbol (neither nil nor t).
324 AS-SEEN is non-nil if we are copying the message \"as seen\"."
325 (let ((case-fold-search t)
326 from date)
327 (goto-char (point-min))
328 ;; Preserve the Mail-From and MIME-Version fields
329 ;; even if they have been pruned.
330 (search-forward "\n\n" nil 'move)
331 (narrow-to-region (point-min) (point))
332 (rmail-delete-unwanted-fields
333 (if rmail-enable-mime "Mail-From"
334 "Mail-From\\|MIME-Version\\|Content-type"))
335 (goto-char (point-min))
336 (or (looking-at "From ")
337 (insert (mail-mbox-from)))
338 (widen)
339 ;; Make sure message ends with blank line.
340 (goto-char (point-max))
341 (rmail-ensure-blank-line)
342 (goto-char (point-min))
343 (let ((buf (find-buffer-visiting file-name))
344 (tembuf (current-buffer)))
345 (if (null buf)
346 (let ((coding-system-for-write 'raw-text-unix))
347 ;; FIXME should ensure existing file ends with a blank line.
348 (write-region (point-min) (point-max) file-name t nomsg))
349 (if (eq buf (current-buffer))
350 (error "Can't output message to same file it's already in"))
351 ;; File has been visited, in buffer BUF.
352 (set-buffer buf)
353 (let ((inhibit-read-only t)
354 (msg (and (boundp 'rmail-current-message)
355 rmail-current-message)))
356 (and msg as-seen
357 (error "Can't output \"as seen\" to a visited Rmail file"))
358 (if msg
359 (rmail-output-to-rmail-buffer tembuf msg)
360 ;; Output file not in Rmail mode => just insert at the end.
361 (narrow-to-region (point-min) (1+ (buffer-size)))
362 (goto-char (point-max))
363 (insert-buffer-substring tembuf)))))))
365 (defun rmail-output-to-rmail-buffer (tembuf msg)
366 "Copy message in TEMBUF into the current Rmail buffer.
367 Do what is necessary to make Rmail know about the new message. then
368 display message number MSG."
369 (save-excursion
370 (rmail-swap-buffers-maybe)
371 (rmail-modify-format)
372 ;; Turn on Auto Save mode, if it's off in this buffer but enabled
373 ;; by default.
374 (and (not buffer-auto-save-file-name)
375 auto-save-default
376 (auto-save-mode t))
377 (rmail-maybe-set-message-counters)
378 ;; Insert the new message after the last old message.
379 (widen)
380 ;; Make sure the last old message ends with a blank line.
381 (goto-char (point-max))
382 (rmail-ensure-blank-line)
383 ;; Insert the new message at the end.
384 (narrow-to-region (point-max) (point-max))
385 (insert-buffer-substring tembuf)
386 (rmail-count-new-messages t)
387 ;; FIXME should re-use existing windows.
388 (if (rmail-summary-exists)
389 (rmail-select-summary (rmail-update-summary)))
390 (rmail-show-message-1 msg)))
392 ;;; There are functions elsewhere in Emacs that use this function;
393 ;;; look at them before you change the calling method.
394 ;;;###autoload
395 (defun rmail-output (file-name &optional count noattribute not-rmail)
396 "Append this message to mail file FILE-NAME.
397 Writes mbox format, unless FILE-NAME exists and is Babyl format, in which
398 case it writes Babyl.
400 Interactively, the default file name comes from `rmail-default-file',
401 which is updated to the name you use in this command. In all uses, if
402 FILE-NAME is not absolute, it is expanded with the directory part of
403 `rmail-default-file'.
405 If a buffer is visiting FILE-NAME, adds the text to that buffer
406 rather than saving the file directly. If the buffer is an Rmail
407 buffer, updates it accordingly.
409 This command always outputs the complete message header, even if
410 the header display is currently pruned.
412 Optional prefix argument COUNT (default 1) says to output that
413 many consecutive messages, starting with the current one (ignoring
414 deleted messages). If `rmail-delete-after-output' is non-nil, deletes
415 messages after output.
417 The optional third argument NOATTRIBUTE, if non-nil, says not to
418 set the `filed' attribute, and not to display a \"Wrote file\"
419 message (if writing a file directly).
421 Set the optional fourth argument NOT-RMAIL non-nil if you call this
422 from a non-Rmail buffer. In this case, COUNT is ignored."
423 (interactive
424 (list (rmail-output-read-file-name)
425 (prefix-numeric-value current-prefix-arg)))
426 (or count (setq count 1))
427 (setq file-name
428 (expand-file-name file-name
429 (and rmail-default-file
430 (file-name-directory rmail-default-file))))
431 ;; Warn about creating new file.
432 (or (find-buffer-visiting file-name)
433 (file-exists-p file-name)
434 (yes-or-no-p (concat "\"" file-name "\" does not exist, create it? "))
435 (error "Output file does not exist"))
436 (if noattribute (setq noattribute 'nomsg))
437 (let ((babyl-format (and (file-readable-p file-name)
438 (mail-file-babyl-p file-name)))
439 (cur (current-buffer))
440 (buf (find-buffer-visiting file-name)))
442 ;; If a babyl file is visited in a buffer, is it visited as babyl
443 ;; or as mbox?
444 (and babyl-format buf
445 (with-current-buffer buf
446 (save-restriction
447 (widen)
448 (save-excursion
449 (goto-char (point-min))
450 (setq babyl-format
451 (looking-at "BABYL OPTIONS:"))))))
453 (if not-rmail ; eg via message-fcc-handler-function
454 (with-temp-buffer
455 (insert-buffer-substring cur)
456 ;; Output in the appropriate format.
457 (if babyl-format
458 (progn
459 (goto-char (point-min))
460 ;; rmail-convert-to-babyl-format errors if no From line,
461 ;; whereas rmail-output-as-mbox inserts one.
462 (or (looking-at "From ")
463 (insert (mail-mbox-from)))
464 (rmail-output-as-babyl file-name noattribute))
465 (rmail-output-as-mbox file-name noattribute)))
466 ;; Called from an Rmail buffer.
467 (if rmail-buffer
468 (set-buffer rmail-buffer)
469 (error "There is no Rmail buffer"))
470 (let ((orig-count count)
471 beg end)
472 (while (> count 0)
473 (setq beg (rmail-msgbeg rmail-current-message)
474 end (rmail-msgend rmail-current-message))
475 ;; All access to the buffer's local variables is now finished...
476 (save-excursion
477 ;; ... so it is ok to go to a different buffer.
478 (if (rmail-buffers-swapped-p) (set-buffer rmail-view-buffer))
479 (setq cur (current-buffer))
480 (save-restriction
481 (widen)
482 (with-temp-buffer
483 (insert-buffer-substring cur beg end)
484 (if babyl-format
485 (rmail-output-as-babyl file-name noattribute)
486 (rmail-output-as-mbox file-name noattribute)))))
487 (or noattribute ; mark message as "filed"
488 (rmail-set-attribute rmail-filed-attr-index t))
489 (setq count (1- count))
490 (let ((next-message-p
491 (if rmail-delete-after-output
492 (rmail-delete-forward)
493 (if (> count 0)
494 (rmail-next-undeleted-message 1))))
495 (num-appended (- orig-count count)))
496 (if (and (> count 0) (not next-message-p))
497 (error "Only %d message%s appended" num-appended
498 (if (= num-appended 1) "" "s")))))))))
500 ;; FIXME nothing outside uses this, so NOT-RMAIL could be dropped.
501 ;; FIXME this duplicates code from rmail-output.
502 ;;;###autoload
503 (defun rmail-output-as-seen (file-name &optional count noattribute not-rmail)
504 "Append this message to mbox file named FILE-NAME.
505 The details are as for `rmail-output', except that:
506 i) the header is output as currently seen
507 ii) this function cannot write to Babyl files
508 iii) an Rmail buffer cannot be visiting FILE-NAME
510 Note that if NOT-RMAIL is non-nil, there is no difference between this
511 function and `rmail-output'. This argument may be removed in future,
512 so you should call `rmail-output' directly in that case."
513 (interactive
514 (list (rmail-output-read-file-name)
515 (prefix-numeric-value current-prefix-arg)))
516 (if not-rmail
517 (rmail-output file-name count noattribute not-rmail)
518 (or count (setq count 1))
519 (setq file-name
520 (expand-file-name file-name
521 (and rmail-default-file
522 (file-name-directory rmail-default-file))))
523 ;; Warn about creating new file.
524 (or (find-buffer-visiting file-name)
525 (file-exists-p file-name)
526 (yes-or-no-p (concat "\"" file-name "\" does not exist, create it? "))
527 (error "Output file does not exist"))
528 ;; FIXME why not?
529 (if (and (file-readable-p file-name) (mail-file-babyl-p file-name))
530 (error "Cannot output `as seen' to a Babyl file"))
531 (if noattribute (setq noattribute 'nomsg))
532 (if rmail-buffer
533 (set-buffer rmail-buffer)
534 (error "There is no Rmail buffer"))
535 (let ((orig-count count)
536 (cur (current-buffer)))
537 (while (> count 0)
538 (let (beg end)
539 ;; If operating from whole-mbox buffer, get message bounds.
540 (or (rmail-buffers-swapped-p)
541 (setq beg (rmail-msgbeg rmail-current-message)
542 end (rmail-msgend rmail-current-message)))
543 (save-restriction
544 (widen)
545 ;; If operating from the view buffer, get the bounds.
546 (or beg
547 (setq beg (point-min)
548 end (point-max)))
549 (with-temp-buffer
550 (insert-buffer-substring cur beg end)
551 (rmail-output-as-mbox file-name noattribute t))))
552 (or noattribute ; mark message as "filed"
553 (rmail-set-attribute rmail-filed-attr-index t))
554 (setq count (1- count))
555 (let ((next-message-p
556 (if rmail-delete-after-output
557 (rmail-delete-forward)
558 (if (> count 0)
559 (rmail-next-undeleted-message 1))))
560 (num-appended (- orig-count count)))
561 (if (and (> count 0) (not next-message-p))
562 (error "Only %d message%s appended" num-appended
563 (if (= num-appended 1) "" "s"))))))))
566 ;;;###autoload
567 (defun rmail-output-body-to-file (file-name)
568 "Write this message body to the file FILE-NAME.
569 Interactively, the default file name comes from either the message
570 \"Subject\" header, or from `rmail-default-body-file'. Updates the value
571 of `rmail-default-body-file' accordingly. In all uses, if FILE-NAME
572 is not absolute, it is expanded with the directory part of
573 `rmail-default-body-file'.
575 Note that this overwrites FILE-NAME (after confirmation), rather
576 than appending to it. Deletes the message after writing if
577 `rmail-delete-after-output' is non-nil."
578 (interactive
579 (let ((default-file
580 (or (mail-fetch-field "Subject")
581 rmail-default-body-file)))
582 (setq default-file
583 (replace-regexp-in-string ":" "-" default-file))
584 (setq default-file
585 (replace-regexp-in-string " " "-" default-file))
586 (list (setq rmail-default-body-file
587 (read-file-name
588 "Output message body to file: "
589 (and default-file (file-name-directory default-file))
590 default-file
591 nil default-file)))))
592 (setq file-name
593 (expand-file-name file-name
594 (and rmail-default-body-file
595 (file-name-directory rmail-default-body-file))))
596 (save-excursion
597 (goto-char (point-min))
598 (search-forward "\n\n")
599 (and (file-exists-p file-name)
600 (not (y-or-n-p (format "File %s exists; overwrite? " file-name)))
601 (error "Operation aborted"))
602 (write-region (point) (point-max) file-name))
603 (if rmail-delete-after-output
604 (rmail-delete-forward)))
606 ;; arch-tag: 4059abf0-f249-4be4-8e0d-602d370d01d1
607 ;;; rmailout.el ends here