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.
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/>.
31 (defcustom rmail-output-decode-coding nil
32 "If non-nil, do coding system decoding when outputting message as Babyl."
37 (defcustom rmail-output-file-alist nil
38 "Alist matching regexps to suggested output Rmail files.
39 This is a list of elements of the form (REGEXP . NAME-EXP).
40 The suggestion is taken if REGEXP matches anywhere in the message buffer.
41 NAME-EXP may be a string constant giving the file name to use,
42 or more generally it may be any kind of expression that returns
43 a file name as a string."
44 :type
'(repeat (cons regexp
46 (string :tag
"File Name")
49 ;;;###autoload(put 'rmail-output-file-alist 'risky-local-variable t)
51 (defcustom rmail-fields-not-to-output nil
52 "Regexp describing fields to exclude when outputting a message to a file.
53 The function `rmail-delete-unwanted-fields' uses this, ignoring case."
54 :type
'(choice (const :tag
"None" nil
)
58 (defun rmail-output-read-file-name ()
59 "Read the file name to use for `rmail-output'.
60 Set `rmail-default-file' to this name as well as returning it."
63 (setq tail rmail-output-file-alist
)
64 ;; Suggest a file based on a pattern match.
65 (while (and tail
(not answer
))
67 (goto-char (point-min))
68 (if (re-search-forward (car (car tail
)) nil t
)
69 (setq answer
(eval (cdr (car tail
)))))
70 (setq tail
(cdr tail
))))
71 ;; If no suggestion, use same file as last time.
72 (or answer rmail-default-file
))))
76 (concat "Output message to mail file (default "
77 (file-name-nondirectory default-file
)
79 (file-name-directory default-file
)
80 (abbreviate-file-name default-file
))
81 (file-name-directory default-file
))))
82 (setq rmail-default-file
83 (if (file-directory-p read-file
)
84 (expand-file-name (file-name-nondirectory default-file
)
87 (or read-file
(file-name-nondirectory default-file
))
88 (file-name-directory default-file
)))))))
90 (defun rmail-delete-unwanted-fields (preserve)
91 "Delete all headers matching `rmail-fields-not-to-output'.
92 Retains headers matching the regexp PRESERVE. Ignores case.
93 The buffer should be narrowed to just the header."
94 (if rmail-fields-not-to-output
96 (goto-char (point-min))
97 (let ((case-fold-search t
))
98 (while (re-search-forward rmail-fields-not-to-output nil t
)
100 (unless (looking-at preserve
)
101 (delete-region (point) (line-beginning-position 2))))))))
103 (defun rmail-output-as-babyl (file-name nomsg
)
104 "Convert the current buffer's text to Babyl and output to FILE-NAME.
105 Alters the current buffer's text, so it should be a temporary buffer.
106 If a buffer is visiting FILE-NAME, adds the text to that buffer
107 rather than saving the file directly. If the buffer is an Rmail buffer,
108 updates it accordingly. If no buffer is visiting FILE-NAME, appends
109 the text directly to FILE-NAME, and displays a \"Wrote file\" message
110 unless NOMSG is a symbol (neither nil nor t)."
111 (let ((coding-system-for-write 'emacs-mule-unix
))
113 (goto-char (point-min))
114 (search-forward "\n\n" nil
'move
)
115 (narrow-to-region (point-min) (point))
116 (if rmail-fields-not-to-output
117 (rmail-delete-unwanted-fields nil
)))
119 ;; Convert to Babyl format.
120 (rmail-convert-to-babyl-format)
121 ;; Write it into the file, or its buffer.
122 (let ((buf (find-buffer-visiting file-name
))
123 (tembuf (current-buffer)))
125 (write-region (point-min) (point-max) file-name t nomsg
)
126 (if (eq buf
(current-buffer))
127 (error "Can't output message to same file it's already in"))
128 ;; File has been visited, in buffer BUF.
130 (let ((inhibit-read-only t
)
131 (msg (bound-and-true-p rmail-current-message
)))
132 ;; If MSG is non-nil, buffer is in RMAIL mode.
134 (rmail-output-to-babyl-buffer tembuf msg
)
135 ;; Output file not in rmail mode => just insert at the end.
136 (narrow-to-region (point-min) (1+ (buffer-size)))
137 (goto-char (point-max))
138 (insert-buffer-substring tembuf
)))))))
140 ;; Called only if rmail-summary-exists, which means rmailsum is loaded.
141 (declare-function rmail-update-summary
"rmailsum" (&rest ignore
))
143 (defun rmail-output-to-babyl-buffer (tembuf msg
)
144 "Copy message in TEMBUF into the current Babyl Rmail buffer.
145 Do what is necessary to make Rmail know about the new message, then
146 display message number MSG."
147 ;; Turn on Auto Save mode, if it's off in this buffer but enabled by default.
148 (and (not buffer-auto-save-file-name
)
151 (rmail-maybe-set-message-counters)
153 (narrow-to-region (point-max) (point-max))
154 (insert-buffer-substring tembuf
)
155 (goto-char (point-min))
157 (search-backward "\n\^_")
158 (narrow-to-region (point) (point-max))
159 (rmail-count-new-messages t
)
160 (if (rmail-summary-exists)
161 (rmail-select-summary (rmail-update-summary)))
162 (rmail-show-message-1 msg
))
164 (defun rmail-convert-to-babyl-format ()
165 "Convert the mbox message in the current buffer to Babyl format."
166 (let ((count 0) (start (point-min))
167 (case-fold-search nil
)
168 (buffer-undo-list t
))
169 (goto-char (point-min))
171 (unless (looking-at "^From ")
172 (error "Invalid mbox message"))
173 (insert "\^L\n0,,\n*** EOOH ***\n")
174 (rmail-nuke-pinhead-header)
175 ;; Decode base64 or quoted printable contents, Rmail style.
176 (let* ((header-end (save-excursion
177 (and (re-search-forward "\n\n" nil t
)
180 (quoted-printable-header-field-end
183 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
185 (base64-header-field-end
187 ;; Don't decode non-text data.
190 "^content-type:\\(\n?[\t ]\\)\\(text\\|message\\)/"
194 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
197 (goto-char (point-max))
198 (if quoted-printable-header-field-end
200 (unless (mail-unquote-printable-region
201 header-end
(point) nil t t
)
202 (message "Malformed MIME quoted-printable message"))
203 ;; Change "quoted-printable" to "8bit",
204 ;; to reflect the decoding we just did.
205 (goto-char quoted-printable-header-field-end
)
206 (delete-region (point) (search-backward ":"))
208 (if base64-header-field-end
210 (when (condition-case nil
212 (base64-decode-region
215 ;; Prevent base64-decode-region
216 ;; from removing newline characters.
217 (skip-chars-backward "\n\t ")
221 ;; Change "base64" to "8bit", to reflect the
222 ;; decoding we just did.
223 (goto-char base64-header-field-end
)
224 (delete-region (point) (search-backward ":"))
225 (insert ": 8bit")))))
226 ;; Transform anything within the message text
227 ;; that might appear to be the end of a Babyl-format message.
230 (narrow-to-region start
(point))
231 (goto-char (point-min))
232 (while (search-forward "\n\^_" nil t
) ; single char
233 (replace-match "\n^_")))) ; 2 chars: "^" and "_"
234 ;; This is for malformed messages that don't end in newline.
235 ;; There shouldn't be any, but some users say occasionally
237 (or (bolp) (newline))
239 (setq last-coding-system-used nil
)
240 ;; Decode coding system, following specs in the message header,
241 ;; and record what coding system was decoded.
242 (if rmail-output-decode-coding
246 (search-forward "\n\n" nil t
)
247 (let ((case-fold-search t
))
249 rmail-mime-charset-pattern
251 (intern (downcase (match-string 1))))))
252 (rmail-decode-region start
(point) mime-charset
)))
256 (insert "X-Coding-System: "
257 (symbol-name last-coding-system-used
)
260 (defun rmail-nuke-pinhead-header ()
261 "Delete the \"From \" line in the current mbox message.
262 The variable `rmail-unix-mail-delimiter' specifies the From line format.
263 Replaces the From line with a \"Mail-from\" header. Adds \"Date\" and
264 \"From\" headers if they are not already present."
267 (let ((start (point))
270 (search-forward "\n\n")
272 (goto-char (point-max))
276 (narrow-to-region start end
)
277 (let ((case-fold-search t
))
279 (setq has-from
(search-forward "\nFrom:" nil t
))
281 (setq has-date
(and (search-forward "\nDate:" nil t
) (point)))
283 (let ((case-fold-search nil
))
284 (if (re-search-forward (concat "^" rmail-unix-mail-delimiter
) nil t
)
288 ;; Keep and reformat the date if we don't
289 ;; have a Date: field.
293 "Date: \\2, \\4 \\3 \\9 \\5 "
295 ;; The timezone could be matched by group 7 or group 10.
296 ;; If neither of them matched, assume EST, since only
297 ;; Easterners would be so sloppy.
298 ;; It's a shame the substitution can't use "\\10".
300 ((/= (match-beginning 7) (match-end 7)) "\\7")
301 ((/= (match-beginning 10) (match-end 10))
302 (buffer-substring (match-beginning 10)
306 ;; Keep and reformat the sender if we don't
307 ;; have a From: field.
313 (autoload 'mail-mbox-from
"mail-utils")
315 (defun rmail-output-as-mbox (file-name nomsg
&optional as-seen
)
316 "Convert the current buffer's text to mbox and output to FILE-NAME.
317 Alters the current buffer's text, so it should be a temporary buffer.
318 If a buffer is visiting FILE-NAME, adds the text to that buffer
319 rather than saving the file directly. If the buffer is an Rmail buffer,
320 updates it accordingly. If no buffer is visiting FILE-NAME, appends
321 the text directly to FILE-NAME, and displays a \"Wrote file\" message
322 unless NOMSG is a symbol (neither nil nor t).
323 AS-SEEN is non-nil if we are copying the message \"as seen\"."
324 (let ((case-fold-search t
)
326 (goto-char (point-min))
327 ;; Preserve the Mail-From and MIME-Version fields
328 ;; even if they have been pruned.
329 (search-forward "\n\n" nil
'move
)
330 (narrow-to-region (point-min) (point))
331 (rmail-delete-unwanted-fields
332 (if rmail-enable-mime
"Mail-From"
333 "Mail-From\\|MIME-Version\\|Content-type"))
334 (goto-char (point-min))
335 (or (looking-at "From ")
336 (insert (mail-mbox-from)))
338 ;; Make sure message ends with blank line.
339 (goto-char (point-max))
340 (rmail-ensure-blank-line)
341 (goto-char (point-min))
342 (let ((buf (find-buffer-visiting file-name
))
343 (tembuf (current-buffer)))
345 (let ((coding-system-for-write 'raw-text-unix
))
346 ;; FIXME should ensure existing file ends with a blank line.
347 (write-region (point-min) (point-max) file-name t nomsg
))
348 (if (eq buf
(current-buffer))
349 (error "Can't output message to same file it's already in"))
350 ;; File has been visited, in buffer BUF.
352 (let ((inhibit-read-only t
)
353 (msg (and (boundp 'rmail-current-message
)
354 rmail-current-message
)))
356 (error "Can't output \"as seen\" to a visited Rmail file"))
358 (rmail-output-to-rmail-buffer tembuf msg
)
359 ;; Output file not in Rmail mode => just insert at the end.
360 (narrow-to-region (point-min) (1+ (buffer-size)))
361 (goto-char (point-max))
362 (insert-buffer-substring tembuf
)))))))
364 (defun rmail-output-to-rmail-buffer (tembuf msg
)
365 "Copy message in TEMBUF into the current Rmail buffer.
366 Do what is necessary to make Rmail know about the new message. then
367 display message number MSG."
369 (rmail-swap-buffers-maybe)
370 (rmail-modify-format)
371 ;; Turn on Auto Save mode, if it's off in this buffer but enabled
373 (and (not buffer-auto-save-file-name
)
376 (rmail-maybe-set-message-counters)
377 ;; Insert the new message after the last old message.
379 ;; Make sure the last old message ends with a blank line.
380 (goto-char (point-max))
381 (rmail-ensure-blank-line)
382 ;; Insert the new message at the end.
383 (narrow-to-region (point-max) (point-max))
384 (insert-buffer-substring tembuf
)
385 (rmail-count-new-messages t
)
386 ;; FIXME should re-use existing windows.
387 (if (rmail-summary-exists)
388 (rmail-select-summary (rmail-update-summary)))
389 (rmail-show-message-1 msg
)))
391 ;;; There are functions elsewhere in Emacs that use this function;
392 ;;; look at them before you change the calling method.
394 (defun rmail-output (file-name &optional count noattribute not-rmail
)
395 "Append this message to mail file FILE-NAME.
396 Writes mbox format, unless FILE-NAME exists and is Babyl format, in which
397 case it writes Babyl.
399 Interactively, the default file name comes from `rmail-default-file',
400 which is updated to the name you use in this command. In all uses, if
401 FILE-NAME is not absolute, it is expanded with the directory part of
402 `rmail-default-file'.
404 If a buffer is visiting FILE-NAME, adds the text to that buffer
405 rather than saving the file directly. If the buffer is an Rmail
406 buffer, updates it accordingly.
408 This command always outputs the complete message header, even if
409 the header display is currently pruned.
411 Optional prefix argument COUNT (default 1) says to output that
412 many consecutive messages, starting with the current one (ignoring
413 deleted messages). If `rmail-delete-after-output' is non-nil, deletes
414 messages after output.
416 The optional third argument NOATTRIBUTE, if non-nil, says not to
417 set the `filed' attribute, and not to display a \"Wrote file\"
418 message (if writing a file directly).
420 Set the optional fourth argument NOT-RMAIL non-nil if you call this
421 from a non-Rmail buffer. In this case, COUNT is ignored."
423 (list (rmail-output-read-file-name)
424 (prefix-numeric-value current-prefix-arg
)))
425 (or count
(setq count
1))
427 (expand-file-name file-name
428 (and rmail-default-file
429 (file-name-directory rmail-default-file
))))
430 ;; Warn about creating new file.
431 (or (find-buffer-visiting file-name
)
432 (file-exists-p file-name
)
433 (yes-or-no-p (concat "\"" file-name
"\" does not exist, create it? "))
434 (error "Output file does not exist"))
435 (if noattribute
(setq noattribute
'nomsg
))
436 (let ((babyl-format (and (file-readable-p file-name
)
437 (mail-file-babyl-p file-name
)))
438 (cur (current-buffer))
439 (buf (find-buffer-visiting file-name
)))
441 ;; If a babyl file is visited in a buffer, is it visited as babyl
443 (and babyl-format buf
444 (with-current-buffer buf
448 (goto-char (point-min))
450 (looking-at "BABYL OPTIONS:"))))))
452 (if not-rmail
; eg via message-fcc-handler-function
454 (insert-buffer-substring cur
)
455 ;; Output in the appropriate format.
458 (goto-char (point-min))
459 ;; rmail-convert-to-babyl-format errors if no From line,
460 ;; whereas rmail-output-as-mbox inserts one.
461 (or (looking-at "From ")
462 (insert (mail-mbox-from)))
463 (rmail-output-as-babyl file-name noattribute
))
464 (rmail-output-as-mbox file-name noattribute
)))
465 ;; Called from an Rmail buffer.
467 (set-buffer rmail-buffer
)
468 (error "There is no Rmail buffer"))
469 (let ((orig-count count
)
472 (setq beg
(rmail-msgbeg rmail-current-message
)
473 end
(rmail-msgend rmail-current-message
))
474 ;; All access to the buffer's local variables is now finished...
476 ;; ... so it is ok to go to a different buffer.
477 (if (rmail-buffers-swapped-p) (set-buffer rmail-view-buffer
))
478 (setq cur
(current-buffer))
482 (insert-buffer-substring cur beg end
)
484 (rmail-output-as-babyl file-name noattribute
)
485 (rmail-output-as-mbox file-name noattribute
)))))
486 (or noattribute
; mark message as "filed"
487 (rmail-set-attribute rmail-filed-attr-index t
))
488 (setq count
(1- count
))
489 (let ((next-message-p
490 (if rmail-delete-after-output
491 (rmail-delete-forward)
493 (rmail-next-undeleted-message 1))))
494 (num-appended (- orig-count count
)))
495 (if (and (> count
0) (not next-message-p
))
496 (error "Only %d message%s appended" num-appended
497 (if (= num-appended
1) "" "s")))))))))
499 ;; FIXME nothing outside uses this, so NOT-RMAIL could be dropped.
500 ;; FIXME this duplicates code from rmail-output.
502 (defun rmail-output-as-seen (file-name &optional count noattribute not-rmail
)
503 "Append this message to mbox file named FILE-NAME.
504 The details are as for `rmail-output', except that:
505 i) the header is output as currently seen
506 ii) this function cannot write to Babyl files
507 iii) an Rmail buffer cannot be visiting FILE-NAME
509 Note that if NOT-RMAIL is non-nil, there is no difference between this
510 function and `rmail-output'. This argument may be removed in future,
511 so you should call `rmail-output' directly in that case."
513 (list (rmail-output-read-file-name)
514 (prefix-numeric-value current-prefix-arg
)))
516 (rmail-output file-name count noattribute not-rmail
)
517 (or count
(setq count
1))
519 (expand-file-name file-name
520 (and rmail-default-file
521 (file-name-directory rmail-default-file
))))
522 ;; Warn about creating new file.
523 (or (find-buffer-visiting file-name
)
524 (file-exists-p file-name
)
525 (yes-or-no-p (concat "\"" file-name
"\" does not exist, create it? "))
526 (error "Output file does not exist"))
528 (if (and (file-readable-p file-name
) (mail-file-babyl-p file-name
))
529 (error "Cannot output `as seen' to a Babyl file"))
530 (if noattribute
(setq noattribute
'nomsg
))
532 (set-buffer rmail-buffer
)
533 (error "There is no Rmail buffer"))
534 (let ((orig-count count
)
535 (cur (current-buffer)))
538 ;; If operating from whole-mbox buffer, get message bounds.
539 (or (rmail-buffers-swapped-p)
540 (setq beg
(rmail-msgbeg rmail-current-message
)
541 end
(rmail-msgend rmail-current-message
)))
544 ;; If operating from the view buffer, get the bounds.
546 (setq beg
(point-min)
549 (insert-buffer-substring cur beg end
)
550 (rmail-output-as-mbox file-name noattribute t
))))
551 (or noattribute
; mark message as "filed"
552 (rmail-set-attribute rmail-filed-attr-index t
))
553 (setq count
(1- count
))
554 (let ((next-message-p
555 (if rmail-delete-after-output
556 (rmail-delete-forward)
558 (rmail-next-undeleted-message 1))))
559 (num-appended (- orig-count count
)))
560 (if (and (> count
0) (not next-message-p
))
561 (error "Only %d message%s appended" num-appended
562 (if (= num-appended
1) "" "s"))))))))
566 (defun rmail-output-body-to-file (file-name)
567 "Write this message body to the file FILE-NAME.
568 Interactively, the default file name comes from either the message
569 \"Subject\" header, or from `rmail-default-body-file'. Updates the value
570 of `rmail-default-body-file' accordingly. In all uses, if FILE-NAME
571 is not absolute, it is expanded with the directory part of
572 `rmail-default-body-file'.
574 Note that this overwrites FILE-NAME (after confirmation), rather
575 than appending to it. Deletes the message after writing if
576 `rmail-delete-after-output' is non-nil."
579 (or (mail-fetch-field "Subject")
580 rmail-default-body-file
)))
582 (replace-regexp-in-string ":" "-" default-file
))
584 (replace-regexp-in-string " " "-" default-file
))
585 (list (setq rmail-default-body-file
587 "Output message body to file: "
588 (and default-file
(file-name-directory default-file
))
590 nil default-file
)))))
592 (expand-file-name file-name
593 (and rmail-default-body-file
594 (file-name-directory rmail-default-body-file
))))
596 (goto-char (point-min))
597 (search-forward "\n\n")
598 (and (file-exists-p file-name
)
599 (not (y-or-n-p (format "File %s exists; overwrite? " file-name
)))
600 (error "Operation aborted"))
601 (write-region (point) (point-max) file-name
))
602 (if rmail-delete-after-output
603 (rmail-delete-forward)))
605 ;; arch-tag: 4059abf0-f249-4be4-8e0d-602d370d01d1
606 ;;; rmailout.el ends here