rmailout.el 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. ;; "RMAIL" mail reader for Emacs: output message to a file.
  2. ;; Copyright (C) 1985, 1987 Free Software Foundation, Inc.
  3. ;; This file is part of GNU Emacs.
  4. ;; GNU Emacs is distributed in the hope that it will be useful,
  5. ;; but WITHOUT ANY WARRANTY. No author or distributor
  6. ;; accepts responsibility to anyone for the consequences of using it
  7. ;; or for whether it serves any particular purpose or works at all,
  8. ;; unless he says so in writing. Refer to the GNU Emacs General Public
  9. ;; License for full details.
  10. ;; Everyone is granted permission to copy, modify and redistribute
  11. ;; GNU Emacs, but only under the conditions described in the
  12. ;; GNU Emacs General Public License. A copy of this license is
  13. ;; supposed to have been given to you along with GNU Emacs so you
  14. ;; can know your rights and responsibilities. It should be in a
  15. ;; file named COPYING. Among other things, the copyright notice
  16. ;; and this notice must be preserved on all copies.
  17. ;; Temporary until Emacs always has this variable.
  18. (defvar rmail-delete-after-output nil
  19. "*Non-nil means automatically delete a message that is copied to a file.")
  20. (defun rmail-output-to-rmail-file (file-name)
  21. "Append the current message to an Rmail file named FILE-NAME.
  22. If the file does not exist, ask if it should be created.
  23. If file is being visited, the message is appended to the Emacs
  24. buffer visiting that file."
  25. (interactive (list (read-file-name
  26. (concat "Output message to Rmail file: (default "
  27. (file-name-nondirectory rmail-last-rmail-file)
  28. ") ")
  29. (file-name-directory rmail-last-rmail-file)
  30. rmail-last-rmail-file)))
  31. (setq file-name (expand-file-name file-name))
  32. (setq rmail-last-rmail-file file-name)
  33. (rmail-maybe-set-message-counters)
  34. (or (get-file-buffer file-name)
  35. (file-exists-p file-name)
  36. (if (yes-or-no-p
  37. (concat "\"" file-name "\" does not exist, create it? "))
  38. (let ((file-buffer (create-file-buffer file-name)))
  39. (save-excursion
  40. (set-buffer file-buffer)
  41. (rmail-insert-rmail-file-header)
  42. (let ((require-final-newline nil))
  43. (write-region (point-min) (point-max) file-name t 1)))
  44. (kill-buffer file-buffer))
  45. (error "Output file does not exist")))
  46. (save-restriction
  47. (widen)
  48. ;; Decide whether to append to a file or to an Emacs buffer.
  49. (save-excursion
  50. (let ((buf (get-file-buffer file-name))
  51. (cur (current-buffer))
  52. (beg (1+ (rmail-msgbeg rmail-current-message)))
  53. (end (1+ (rmail-msgend rmail-current-message))))
  54. (if (not buf)
  55. (append-to-file beg end file-name)
  56. ;; File has been visited, in buffer BUF.
  57. (set-buffer buf)
  58. (let ((buffer-read-only nil)
  59. (msg (and (boundp 'rmail-current-message)
  60. rmail-current-message)))
  61. ;; If MSG is non-nil, buffer is in RMAIL mode.
  62. (if msg
  63. (progn (widen)
  64. (narrow-to-region (point-max) (point-max))))
  65. (insert-buffer-substring cur beg end)
  66. (if msg
  67. (progn
  68. (goto-char (point-min))
  69. (widen)
  70. (search-backward "\^_")
  71. (narrow-to-region (point) (point-max))
  72. (goto-char (1+ (point-min)))
  73. (rmail-count-new-messages t)
  74. (rmail-show-message msg))))))))
  75. (rmail-set-attribute "filed" t)
  76. (and rmail-delete-after-output (rmail-delete-forward)))
  77. (defun rmail-output (file-name)
  78. "Append this message to Unix mail file named FILE-NAME."
  79. (interactive
  80. (list
  81. (read-file-name
  82. (concat "Output message to Unix mail file: (default "
  83. (file-name-nondirectory rmail-last-file)
  84. ") ")
  85. (file-name-directory rmail-last-file)
  86. rmail-last-file)))
  87. (setq file-name (expand-file-name file-name))
  88. (setq rmail-last-file file-name)
  89. (let ((rmailbuf (current-buffer))
  90. (tembuf (get-buffer-create " rmail-output"))
  91. (case-fold-search t))
  92. (save-excursion
  93. (set-buffer tembuf)
  94. (erase-buffer)
  95. (insert-buffer-substring rmailbuf)
  96. (insert "\n")
  97. (goto-char (point-min))
  98. (insert "From "
  99. (mail-strip-quoted-names (mail-fetch-field "from")) " "
  100. (current-time-string) "\n")
  101. ;; ``Quote'' "\nFrom " as "\n>From "
  102. ;; (note that this isn't really quoting, as there is no requirement
  103. ;; that "\n[>]+From " be quoted in the same transparent way.)
  104. (while (search-forward "\nFrom " nil t)
  105. (forward-char -5)
  106. (insert ?>))
  107. (append-to-file (point-min) (point-max) file-name))
  108. (kill-buffer tembuf))
  109. (if (equal major-mode 'rmail-mode)
  110. (progn
  111. (rmail-set-attribute "filed" t)
  112. (and rmail-delete-after-output (rmail-delete-forward)))))