rmailout.el 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. ;;; rmailout.el --- "RMAIL" mail reader for Emacs: output message to a file
  2. ;; Copyright (C) 1985, 1987, 1993-1994, 2001-2012
  3. ;; Free Software Foundation, Inc.
  4. ;; Maintainer: FSF
  5. ;; Keywords: mail
  6. ;; Package: rmail
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;;; Code:
  20. (require 'rmail)
  21. (provide 'rmailout)
  22. (defcustom rmail-output-decode-coding nil
  23. "If non-nil, do coding system decoding when outputting message as Babyl."
  24. :type 'boolean
  25. :group 'rmail-output)
  26. ;; FIXME risky?
  27. (defcustom rmail-output-file-alist nil
  28. "Alist matching regexps to suggested output Rmail files.
  29. This is a list of elements of the form (REGEXP . NAME-EXP).
  30. The suggestion is taken if REGEXP matches anywhere in the message buffer.
  31. NAME-EXP may be a string constant giving the file name to use,
  32. or more generally it may be any kind of expression that returns
  33. a file name as a string."
  34. :type '(repeat (cons regexp
  35. (choice :value ""
  36. (string :tag "File Name")
  37. sexp)))
  38. :group 'rmail-output)
  39. ;;;###autoload(put 'rmail-output-file-alist 'risky-local-variable t)
  40. (defcustom rmail-fields-not-to-output nil
  41. "Regexp describing fields to exclude when outputting a message to a file.
  42. The function `rmail-delete-unwanted-fields' uses this, ignoring case."
  43. :type '(choice (const :tag "None" nil)
  44. regexp)
  45. :group 'rmail-output)
  46. (defun rmail-output-read-file-name ()
  47. "Read the file name to use for `rmail-output'.
  48. Set `rmail-default-file' to this name as well as returning it."
  49. (let ((default-file
  50. (let (answer tail)
  51. (setq tail rmail-output-file-alist)
  52. ;; Suggest a file based on a pattern match.
  53. (while (and tail (not answer))
  54. (save-excursion
  55. (goto-char (point-min))
  56. (if (re-search-forward (car (car tail)) nil t)
  57. (setq answer (eval (cdr (car tail)))))
  58. (setq tail (cdr tail))))
  59. ;; If no suggestion, use same file as last time.
  60. (or answer rmail-default-file))))
  61. (let ((read-file
  62. (expand-file-name
  63. (read-file-name
  64. (concat "Output message to mail file (default "
  65. (file-name-nondirectory default-file)
  66. "): ")
  67. (file-name-directory default-file)
  68. (abbreviate-file-name default-file))
  69. (file-name-directory default-file))))
  70. (setq rmail-default-file
  71. (if (file-directory-p read-file)
  72. (expand-file-name (file-name-nondirectory default-file)
  73. read-file)
  74. (expand-file-name
  75. (or read-file (file-name-nondirectory default-file))
  76. (file-name-directory default-file)))))))
  77. (defun rmail-delete-unwanted-fields (preserve)
  78. "Delete all headers matching `rmail-fields-not-to-output'.
  79. Retains headers matching the regexp PRESERVE. Ignores case.
  80. The buffer should be narrowed to just the header."
  81. (if rmail-fields-not-to-output
  82. (save-excursion
  83. (goto-char (point-min))
  84. (let ((case-fold-search t))
  85. (while (re-search-forward rmail-fields-not-to-output nil t)
  86. (beginning-of-line)
  87. (unless (looking-at preserve)
  88. (delete-region (point) (line-beginning-position 2))))))))
  89. (defun rmail-output-as-babyl (file-name nomsg)
  90. "Convert the current buffer's text to Babyl and output to FILE-NAME.
  91. Alters the current buffer's text, so it should be a temporary buffer.
  92. If a buffer is visiting FILE-NAME, adds the text to that buffer
  93. rather than saving the file directly. If the buffer is an Rmail buffer,
  94. updates it accordingly. If no buffer is visiting FILE-NAME, appends
  95. the text directly to FILE-NAME, and displays a \"Wrote file\" message
  96. unless NOMSG is a symbol (neither nil nor t)."
  97. (let ((coding-system-for-write 'emacs-mule-unix))
  98. (save-restriction
  99. (goto-char (point-min))
  100. (search-forward "\n\n" nil 'move)
  101. (narrow-to-region (point-min) (point))
  102. (if rmail-fields-not-to-output
  103. (rmail-delete-unwanted-fields nil)))
  104. ;; Convert to Babyl format.
  105. (rmail-convert-to-babyl-format)
  106. ;; Write it into the file, or its buffer.
  107. (let ((buf (find-buffer-visiting file-name))
  108. (tembuf (current-buffer)))
  109. (if (null buf)
  110. (write-region (point-min) (point-max) file-name t nomsg)
  111. (if (eq buf (current-buffer))
  112. (error "Can't output message to same file it's already in"))
  113. ;; File has been visited, in buffer BUF.
  114. (set-buffer buf)
  115. (let ((inhibit-read-only t)
  116. (msg (bound-and-true-p rmail-current-message)))
  117. ;; If MSG is non-nil, buffer is in RMAIL mode.
  118. (if msg
  119. (rmail-output-to-babyl-buffer tembuf msg)
  120. ;; Output file not in rmail mode => just insert at the end.
  121. (narrow-to-region (point-min) (1+ (buffer-size)))
  122. (goto-char (point-max))
  123. (insert-buffer-substring tembuf)))))))
  124. ;; Called only if rmail-summary-exists, which means rmailsum is loaded.
  125. (declare-function rmail-update-summary "rmailsum" (&rest ignore))
  126. (defun rmail-output-to-babyl-buffer (tembuf msg)
  127. "Copy message in TEMBUF into the current Babyl Rmail buffer.
  128. Do what is necessary to make Rmail know about the new message, then
  129. display message number MSG."
  130. ;; Turn on Auto Save mode, if it's off in this buffer but enabled by default.
  131. (and (not buffer-auto-save-file-name)
  132. auto-save-default
  133. (auto-save-mode t))
  134. (rmail-maybe-set-message-counters)
  135. (widen)
  136. (narrow-to-region (point-max) (point-max))
  137. (insert-buffer-substring tembuf)
  138. (goto-char (point-min))
  139. (widen)
  140. (search-backward "\n\^_")
  141. (narrow-to-region (point) (point-max))
  142. (rmail-count-new-messages t)
  143. (if (rmail-summary-exists)
  144. (rmail-select-summary (rmail-update-summary)))
  145. (rmail-show-message-1 msg))
  146. (defun rmail-convert-to-babyl-format ()
  147. "Convert the mbox message in the current buffer to Babyl format."
  148. (let ((count 0) (start (point-min))
  149. (case-fold-search nil)
  150. (buffer-undo-list t))
  151. (goto-char (point-min))
  152. (save-restriction
  153. (unless (looking-at "^From ")
  154. (error "Invalid mbox message"))
  155. (insert "\^L\n0,,\n*** EOOH ***\n")
  156. (rmail-nuke-pinhead-header)
  157. ;; Decode base64 or quoted printable contents, Rmail style.
  158. (let* ((header-end (save-excursion
  159. (and (re-search-forward "\n\n" nil t)
  160. (1- (point)))))
  161. (case-fold-search t)
  162. (quoted-printable-header-field-end
  163. (save-excursion
  164. (re-search-forward
  165. "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
  166. header-end t)))
  167. (base64-header-field-end
  168. (and
  169. ;; Don't decode non-text data.
  170. (save-excursion
  171. (re-search-forward
  172. "^content-type:\\(\n?[\t ]\\)\\(text\\|message\\)/"
  173. header-end t))
  174. (save-excursion
  175. (re-search-forward
  176. "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
  177. header-end t)))))
  178. (goto-char (point-max))
  179. (if quoted-printable-header-field-end
  180. (save-excursion
  181. (unless (mail-unquote-printable-region
  182. header-end (point) nil t t)
  183. (message "Malformed MIME quoted-printable message"))
  184. ;; Change "quoted-printable" to "8bit",
  185. ;; to reflect the decoding we just did.
  186. (goto-char quoted-printable-header-field-end)
  187. (delete-region (point) (search-backward ":"))
  188. (insert ": 8bit")))
  189. (if base64-header-field-end
  190. (save-excursion
  191. (when (condition-case nil
  192. (progn
  193. (base64-decode-region
  194. (1+ header-end)
  195. (save-excursion
  196. ;; Prevent base64-decode-region
  197. ;; from removing newline characters.
  198. (skip-chars-backward "\n\t ")
  199. (point)))
  200. t)
  201. (error nil))
  202. ;; Change "base64" to "8bit", to reflect the
  203. ;; decoding we just did.
  204. (goto-char base64-header-field-end)
  205. (delete-region (point) (search-backward ":"))
  206. (insert ": 8bit")))))
  207. ;; Transform anything within the message text
  208. ;; that might appear to be the end of a Babyl-format message.
  209. (save-excursion
  210. (save-restriction
  211. (narrow-to-region start (point))
  212. (goto-char (point-min))
  213. (while (search-forward "\n\^_" nil t) ; single char
  214. (replace-match "\n^_")))) ; 2 chars: "^" and "_"
  215. ;; This is for malformed messages that don't end in newline.
  216. ;; There shouldn't be any, but some users say occasionally
  217. ;; there are some.
  218. (or (bolp) (newline))
  219. (insert ?\^_)
  220. (setq last-coding-system-used nil)
  221. ;; Decode coding system, following specs in the message header,
  222. ;; and record what coding system was decoded.
  223. (if rmail-output-decode-coding
  224. (let ((mime-charset
  225. (if (save-excursion
  226. (goto-char start)
  227. (search-forward "\n\n" nil t)
  228. (let ((case-fold-search t))
  229. (re-search-backward
  230. rmail-mime-charset-pattern
  231. start t)))
  232. (intern (downcase (match-string 1))))))
  233. (rmail-decode-region start (point) mime-charset)))
  234. (save-excursion
  235. (goto-char start)
  236. (forward-line 3)
  237. (insert "X-Coding-System: "
  238. (symbol-name last-coding-system-used)
  239. "\n")))))
  240. (defun rmail-nuke-pinhead-header ()
  241. "Delete the \"From \" line in the current mbox message.
  242. The variable `rmail-unix-mail-delimiter' specifies the From line format.
  243. Replaces the From line with a \"Mail-from\" header. Adds \"Date\" and
  244. \"From\" headers if they are not already present."
  245. (save-excursion
  246. (save-restriction
  247. (let ((start (point))
  248. (end (progn
  249. (condition-case ()
  250. (search-forward "\n\n")
  251. (error
  252. (goto-char (point-max))
  253. (insert "\n\n")))
  254. (point)))
  255. has-from has-date)
  256. (narrow-to-region start end)
  257. (let ((case-fold-search t))
  258. (goto-char start)
  259. (setq has-from (search-forward "\nFrom:" nil t))
  260. (goto-char start)
  261. (setq has-date (and (search-forward "\nDate:" nil t) (point)))
  262. (goto-char start))
  263. (let ((case-fold-search nil))
  264. (if (re-search-forward (concat "^" rmail-unix-mail-delimiter) nil t)
  265. (replace-match
  266. (concat
  267. "Mail-from: \\&"
  268. ;; Keep and reformat the date if we don't
  269. ;; have a Date: field.
  270. (if has-date
  271. ""
  272. (concat
  273. "Date: \\2, \\4 \\3 \\9 \\5 "
  274. ;; The timezone could be matched by group 7 or group 10.
  275. ;; If neither of them matched, assume EST, since only
  276. ;; Easterners would be so sloppy.
  277. ;; It's a shame the substitution can't use "\\10".
  278. (cond
  279. ((/= (match-beginning 7) (match-end 7)) "\\7")
  280. ((/= (match-beginning 10) (match-end 10))
  281. (buffer-substring (match-beginning 10)
  282. (match-end 10)))
  283. (t "EST"))
  284. "\n"))
  285. ;; Keep and reformat the sender if we don't
  286. ;; have a From: field.
  287. (if has-from
  288. ""
  289. "From: \\1\n"))
  290. t)))))))
  291. (autoload 'mail-mbox-from "mail-utils")
  292. (defun rmail-output-as-mbox (file-name nomsg &optional as-seen)
  293. "Convert the current buffer's text to mbox and output to FILE-NAME.
  294. Alters the current buffer's text, so it should be a temporary buffer.
  295. If a buffer is visiting FILE-NAME, adds the text to that buffer
  296. rather than saving the file directly. If the buffer is an Rmail buffer,
  297. updates it accordingly. If no buffer is visiting FILE-NAME, appends
  298. the text directly to FILE-NAME, and displays a \"Wrote file\" message
  299. unless NOMSG is a symbol (neither nil nor t).
  300. AS-SEEN is non-nil if we are copying the message \"as seen\"."
  301. (let ((case-fold-search t)
  302. from date)
  303. (goto-char (point-min))
  304. ;; Preserve the Mail-From and MIME-Version fields
  305. ;; even if they have been pruned.
  306. (search-forward "\n\n" nil 'move)
  307. (narrow-to-region (point-min) (point))
  308. (rmail-delete-unwanted-fields
  309. (if rmail-enable-mime "Mail-From"
  310. "Mail-From\\|MIME-Version\\|Content-type"))
  311. (goto-char (point-min))
  312. (or (looking-at "From ")
  313. (insert (mail-mbox-from)))
  314. (widen)
  315. ;; Make sure message ends with blank line.
  316. (goto-char (point-max))
  317. (rmail-ensure-blank-line)
  318. (goto-char (point-min))
  319. (let ((buf (find-buffer-visiting file-name))
  320. (tembuf (current-buffer)))
  321. (if (null buf)
  322. (let ((coding-system-for-write 'raw-text-unix))
  323. ;; FIXME should ensure existing file ends with a blank line.
  324. (write-region (point-min) (point-max) file-name t nomsg))
  325. (if (eq buf (current-buffer))
  326. (error "Can't output message to same file it's already in"))
  327. ;; File has been visited, in buffer BUF.
  328. (set-buffer buf)
  329. (let ((inhibit-read-only t)
  330. (msg (and (boundp 'rmail-current-message)
  331. rmail-current-message)))
  332. (and msg as-seen
  333. (error "Can't output \"as seen\" to a visited Rmail file"))
  334. (if msg
  335. (rmail-output-to-rmail-buffer tembuf msg)
  336. ;; Output file not in Rmail mode => just insert at the end.
  337. (narrow-to-region (point-min) (1+ (buffer-size)))
  338. (goto-char (point-max))
  339. (insert-buffer-substring tembuf)))))))
  340. (defun rmail-output-to-rmail-buffer (tembuf msg)
  341. "Copy message in TEMBUF into the current Rmail buffer.
  342. Do what is necessary to make Rmail know about the new message. then
  343. display message number MSG."
  344. (save-excursion
  345. (rmail-swap-buffers-maybe)
  346. (rmail-modify-format)
  347. ;; Turn on Auto Save mode, if it's off in this buffer but enabled
  348. ;; by default.
  349. (and (not buffer-auto-save-file-name)
  350. auto-save-default
  351. (auto-save-mode t))
  352. (rmail-maybe-set-message-counters)
  353. ;; Insert the new message after the last old message.
  354. (widen)
  355. (unless (zerop (buffer-size))
  356. ;; Make sure the last old message ends with a blank line.
  357. (goto-char (point-max))
  358. (rmail-ensure-blank-line)
  359. ;; Insert the new message at the end.
  360. (narrow-to-region (point-max) (point-max)))
  361. (insert-buffer-substring tembuf)
  362. (rmail-count-new-messages t)
  363. ;; FIXME should re-use existing windows.
  364. (if (rmail-summary-exists)
  365. (rmail-select-summary (rmail-update-summary)))
  366. (rmail-show-message-1 msg)))
  367. ;;; There are functions elsewhere in Emacs that use this function;
  368. ;;; look at them before you change the calling method.
  369. ;;;###autoload
  370. (defun rmail-output (file-name &optional count noattribute not-rmail)
  371. "Append this message to mail file FILE-NAME.
  372. Writes mbox format, unless FILE-NAME exists and is Babyl format, in which
  373. case it writes Babyl.
  374. Interactively, the default file name comes from `rmail-default-file',
  375. which is updated to the name you use in this command. In all uses, if
  376. FILE-NAME is not absolute, it is expanded with the directory part of
  377. `rmail-default-file'.
  378. If a buffer is visiting FILE-NAME, adds the text to that buffer
  379. rather than saving the file directly. If the buffer is an Rmail
  380. buffer, updates it accordingly.
  381. This command always outputs the complete message header, even if
  382. the header display is currently pruned.
  383. Optional prefix argument COUNT (default 1) says to output that
  384. many consecutive messages, starting with the current one (ignoring
  385. deleted messages). If `rmail-delete-after-output' is non-nil, deletes
  386. messages after output.
  387. The optional third argument NOATTRIBUTE, if non-nil, says not to
  388. set the `filed' attribute, and not to display a \"Wrote file\"
  389. message (if writing a file directly).
  390. Set the optional fourth argument NOT-RMAIL non-nil if you call this
  391. from a non-Rmail buffer. In this case, COUNT is ignored."
  392. (interactive
  393. (list (rmail-output-read-file-name)
  394. (prefix-numeric-value current-prefix-arg)))
  395. (or count (setq count 1))
  396. (setq file-name
  397. (expand-file-name file-name
  398. (and rmail-default-file
  399. (file-name-directory rmail-default-file))))
  400. ;; Warn about creating new file.
  401. (or (find-buffer-visiting file-name)
  402. (file-exists-p file-name)
  403. (yes-or-no-p (concat "\"" file-name "\" does not exist, create it? "))
  404. (error "Output file does not exist"))
  405. (if noattribute (setq noattribute 'nomsg))
  406. (let ((babyl-format (and (file-readable-p file-name)
  407. (mail-file-babyl-p file-name)))
  408. (cur (current-buffer))
  409. (buf (find-buffer-visiting file-name)))
  410. ;; If a babyl file is visited in a buffer, is it visited as babyl
  411. ;; or as mbox?
  412. (and babyl-format buf
  413. (with-current-buffer buf
  414. (save-restriction
  415. (widen)
  416. (save-excursion
  417. (goto-char (point-min))
  418. (setq babyl-format
  419. (looking-at "BABYL OPTIONS:"))))))
  420. (if not-rmail ; eg via message-fcc-handler-function
  421. (with-temp-buffer
  422. (insert-buffer-substring cur)
  423. ;; Output in the appropriate format.
  424. (if babyl-format
  425. (progn
  426. (goto-char (point-min))
  427. ;; rmail-convert-to-babyl-format errors if no From line,
  428. ;; whereas rmail-output-as-mbox inserts one.
  429. (or (looking-at "From ")
  430. (insert (mail-mbox-from)))
  431. (rmail-output-as-babyl file-name noattribute))
  432. (rmail-output-as-mbox file-name noattribute)))
  433. ;; Called from an Rmail buffer.
  434. (if rmail-buffer
  435. (set-buffer rmail-buffer)
  436. (error "There is no Rmail buffer"))
  437. (if (zerop rmail-total-messages)
  438. (error "No messages to output"))
  439. (let ((orig-count count)
  440. beg end)
  441. (while (> count 0)
  442. (setq beg (rmail-msgbeg rmail-current-message)
  443. end (rmail-msgend rmail-current-message))
  444. ;; All access to the buffer's local variables is now finished...
  445. (save-excursion
  446. ;; ... so it is ok to go to a different buffer.
  447. (if (rmail-buffers-swapped-p) (set-buffer rmail-view-buffer))
  448. (setq cur (current-buffer))
  449. (save-restriction
  450. (widen)
  451. (with-temp-buffer
  452. (insert-buffer-substring cur beg end)
  453. (if babyl-format
  454. (rmail-output-as-babyl file-name noattribute)
  455. (rmail-output-as-mbox file-name noattribute)))))
  456. (or noattribute ; mark message as "filed"
  457. (rmail-set-attribute rmail-filed-attr-index t))
  458. (setq count (1- count))
  459. (let ((next-message-p
  460. (if rmail-delete-after-output
  461. (rmail-delete-forward)
  462. (if (> count 0)
  463. (rmail-next-undeleted-message 1))))
  464. (num-appended (- orig-count count)))
  465. (if (and (> count 0) (not next-message-p))
  466. (error "Only %d message%s appended" num-appended
  467. (if (= num-appended 1) "" "s")))))))))
  468. ;; FIXME nothing outside uses this, so NOT-RMAIL could be dropped.
  469. ;; FIXME this duplicates code from rmail-output.
  470. ;;;###autoload
  471. (defun rmail-output-as-seen (file-name &optional count noattribute not-rmail)
  472. "Append this message to mbox file named FILE-NAME.
  473. The details are as for `rmail-output', except that:
  474. i) the header is output as currently seen
  475. ii) this function cannot write to Babyl files
  476. iii) an Rmail buffer cannot be visiting FILE-NAME
  477. Note that if NOT-RMAIL is non-nil, there is no difference between this
  478. function and `rmail-output'. This argument may be removed in future,
  479. so you should call `rmail-output' directly in that case."
  480. (interactive
  481. (list (rmail-output-read-file-name)
  482. (prefix-numeric-value current-prefix-arg)))
  483. (if not-rmail
  484. (rmail-output file-name count noattribute not-rmail)
  485. (or count (setq count 1))
  486. (setq file-name
  487. (expand-file-name file-name
  488. (and rmail-default-file
  489. (file-name-directory rmail-default-file))))
  490. ;; Warn about creating new file.
  491. (or (find-buffer-visiting file-name)
  492. (file-exists-p file-name)
  493. (yes-or-no-p (concat "\"" file-name "\" does not exist, create it? "))
  494. (error "Output file does not exist"))
  495. ;; FIXME why not?
  496. (if (and (file-readable-p file-name) (mail-file-babyl-p file-name))
  497. (error "Cannot output `as seen' to a Babyl file"))
  498. (if noattribute (setq noattribute 'nomsg))
  499. (if rmail-buffer
  500. (set-buffer rmail-buffer)
  501. (error "There is no Rmail buffer"))
  502. (if (zerop rmail-total-messages)
  503. (error "No messages to output"))
  504. (let ((orig-count count)
  505. (cur (current-buffer)))
  506. (while (> count 0)
  507. (let (beg end)
  508. ;; If operating from whole-mbox buffer, get message bounds.
  509. (or (rmail-buffers-swapped-p)
  510. (setq beg (rmail-msgbeg rmail-current-message)
  511. end (rmail-msgend rmail-current-message)))
  512. (save-restriction
  513. (widen)
  514. ;; If operating from the view buffer, get the bounds.
  515. (or beg
  516. (setq beg (point-min)
  517. end (point-max)))
  518. (with-temp-buffer
  519. (insert-buffer-substring cur beg end)
  520. (rmail-output-as-mbox file-name noattribute t))))
  521. (or noattribute ; mark message as "filed"
  522. (rmail-set-attribute rmail-filed-attr-index t))
  523. (setq count (1- count))
  524. (let ((next-message-p
  525. (if rmail-delete-after-output
  526. (rmail-delete-forward)
  527. (if (> count 0)
  528. (rmail-next-undeleted-message 1))))
  529. (num-appended (- orig-count count)))
  530. (if (and (> count 0) (not next-message-p))
  531. (error "Only %d message%s appended" num-appended
  532. (if (= num-appended 1) "" "s"))))))))
  533. ;;;###autoload
  534. (defun rmail-output-body-to-file (file-name)
  535. "Write this message body to the file FILE-NAME.
  536. Interactively, the default file name comes from either the message
  537. \"Subject\" header, or from `rmail-default-body-file'. Updates the value
  538. of `rmail-default-body-file' accordingly. In all uses, if FILE-NAME
  539. is not absolute, it is expanded with the directory part of
  540. `rmail-default-body-file'.
  541. Note that this overwrites FILE-NAME (after confirmation), rather
  542. than appending to it. Deletes the message after writing if
  543. `rmail-delete-after-output' is non-nil."
  544. (interactive
  545. (let ((default-file
  546. (or (mail-fetch-field "Subject")
  547. rmail-default-body-file)))
  548. (setq default-file
  549. (replace-regexp-in-string ":" "-" default-file))
  550. (setq default-file
  551. (replace-regexp-in-string " " "-" default-file))
  552. (list (setq rmail-default-body-file
  553. (read-file-name
  554. "Output message body to file: "
  555. (and default-file (file-name-directory default-file))
  556. default-file
  557. nil default-file)))))
  558. (setq file-name
  559. (expand-file-name file-name
  560. (and rmail-default-body-file
  561. (file-name-directory rmail-default-body-file))))
  562. (if (zerop rmail-current-message)
  563. (error "No message to output"))
  564. (save-excursion
  565. (goto-char (point-min))
  566. (search-forward "\n\n")
  567. (and (file-exists-p file-name)
  568. (not (y-or-n-p (format "File %s exists; overwrite? " file-name)))
  569. (error "Operation aborted"))
  570. (write-region (point) (point-max) file-name))
  571. (if rmail-delete-after-output
  572. (rmail-delete-forward)))
  573. ;;; rmailout.el ends here