metamail.el 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. ;;; metamail.el --- Metamail interface for GNU Emacs
  2. ;; Copyright (C) 1993, 1996, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Masanobu UMEDA <umerin@mse.kyutech.ac.jp>
  4. ;; Keywords: mail, news, mime, multimedia
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; Note: Metamail does not have all the options which are compatible with
  18. ;; the environment variables. For that reason, metamail.el has to
  19. ;; hack the environment variables. In addition, there is no way to
  20. ;; display all header fields without extra informative body messages
  21. ;; which are suppressed by the "-q" option.
  22. ;; The idea of using metamail to process MIME messages is from
  23. ;; gnus-mime.el by Spike <Spike@world.std.com>.
  24. ;;; Code:
  25. (defvar rmail-current-message)
  26. (defvar rmail-message-vector)
  27. (defgroup metamail nil
  28. "Metamail interface for Emacs."
  29. :group 'mail
  30. :group 'processes)
  31. (defcustom metamail-program-name "metamail"
  32. "*Metamail program name."
  33. :type 'string
  34. :group 'metamail)
  35. (defcustom metamail-mailer-name "emacs"
  36. "*Mailer name set to MM_MAILER environment variable."
  37. :type 'string
  38. :group 'metamail)
  39. (defvar metamail-environment '("KEYHEADS=*" "MM_QUIET=1")
  40. "*Environment variables passed to `metamail'.
  41. It must be a list of strings that have the format ENVVARNAME=VALUE.
  42. It is not expected to be altered globally by `set' or `setq'.
  43. Instead, change its value temporary using `let' or `let*' form.")
  44. (defcustom metamail-switches '("-x" "-d" "-z")
  45. "*Switches for `metamail' program.
  46. `-z' is required to remove zap file.
  47. It is not expected to be altered globally by `set' or `setq'.
  48. Instead, change its value temporary using `let' or `let*' form.
  49. `-m MAILER' argument is automatically generated from the
  50. `metamail-mailer-name' variable."
  51. :type '(repeat (string :tag "Switch"))
  52. :group 'metamail)
  53. ;;;###autoload
  54. (defun metamail-interpret-header ()
  55. "Interpret a header part of a MIME message in current buffer.
  56. Its body part is not interpreted at all."
  57. (interactive)
  58. (save-excursion
  59. (let* ((buffer-read-only nil)
  60. (metamail-switches ;Inhibit processing an empty body.
  61. (append metamail-switches '("-c" "text/plain" "-E" "7bit")))
  62. (end (progn
  63. (goto-char (point-min))
  64. (search-forward "\n\n" nil 'move)
  65. ;; An extra newline is inserted by metamail if there
  66. ;; is no body part. So, insert a dummy body by
  67. ;; itself.
  68. (insert "\n")
  69. (point))))
  70. (metamail-region (point-min) end nil nil 'nodisplay)
  71. ;; Remove an extra newline inserted by myself.
  72. (goto-char (point-min))
  73. (if (search-forward "\n\n\n" nil t)
  74. (delete-char -1))
  75. )))
  76. ;;;###autoload
  77. (defun metamail-interpret-body (&optional viewmode nodisplay)
  78. "Interpret a body part of a MIME message in current buffer.
  79. Optional argument VIEWMODE specifies the value of the
  80. EMACS_VIEW_MODE environment variable (defaulted to 1).
  81. Optional argument NODISPLAY non-nil means buffer is not
  82. redisplayed as output is inserted.
  83. Its header part is not interpreted at all."
  84. (interactive "p")
  85. (save-excursion
  86. (let ((contype nil)
  87. (encoding nil)
  88. (end (progn
  89. (goto-char (point-min))
  90. (search-forward "\n\n" nil t)
  91. (point))))
  92. ;; Find Content-Type and Content-Transfer-Encoding from the header.
  93. (save-restriction
  94. (narrow-to-region (point-min) end)
  95. (setq contype
  96. (or (mail-fetch-field "Content-Type") "text/plain"))
  97. (setq encoding
  98. (or (mail-fetch-field "Content-Transfer-Encoding") "7bit")))
  99. ;; Interpret the body part only.
  100. (let ((metamail-switches ;Process body part only.
  101. (append metamail-switches
  102. (list "-b" "-c" contype "-E" encoding))))
  103. (metamail-region end (point-max) viewmode nil nodisplay))
  104. ;; This mode specific hack is no longer appropriate in mbox Rmail.
  105. ;; Pre-mbox, we have just modified the actual folder, so we
  106. ;; update the message-vector with the new end position of the
  107. ;; current message. In mbox Rmail, all we have done is modify a
  108. ;; display copy of the message. Note also that point-max is a
  109. ;; marker in the wrong buffer: the message-vector contains
  110. ;; markers in rmail-view-buffer (which is not in rmail-mode).
  111. ;; So this hack actually breaks the message-vector.
  112. ;; If you're calling this on the actual rmail-view-buffer (or a
  113. ;; non-swapped rmail-buffer), you would still need this hack.
  114. ;; But you're not going to do that.
  115. ;;; (cond ((eq major-mode 'rmail-mode)
  116. ;;; ;; Adjust the marker of this message if in Rmail mode buffer.
  117. ;;; (set-marker (aref rmail-message-vector (1+ rmail-current-message))
  118. ;;; (point-max))))
  119. )))
  120. ;;;###autoload
  121. (defun metamail-buffer (&optional viewmode buffer nodisplay)
  122. "Process current buffer through `metamail'.
  123. Optional argument VIEWMODE specifies the value of the
  124. EMACS_VIEW_MODE environment variable (defaulted to 1).
  125. Optional argument BUFFER specifies a buffer to be filled (nil
  126. means current).
  127. Optional argument NODISPLAY non-nil means buffer is not
  128. redisplayed as output is inserted."
  129. (interactive "p")
  130. (metamail-region (point-min) (point-max) viewmode buffer nodisplay))
  131. ;;;###autoload
  132. (defun metamail-region (beg end &optional viewmode buffer nodisplay)
  133. "Process current region through 'metamail'.
  134. Optional argument VIEWMODE specifies the value of the
  135. EMACS_VIEW_MODE environment variable (defaulted to 1).
  136. Optional argument BUFFER specifies a buffer to be filled (nil
  137. means current).
  138. Optional argument NODISPLAY non-nil means buffer is not
  139. redisplayed as output is inserted."
  140. (interactive "r\np")
  141. (let ((curbuf (current-buffer))
  142. (buffer-read-only nil)
  143. (metafile (make-temp-file "metamail"))
  144. (option-environment
  145. (list (format "EMACS_VIEW_MODE=%d"
  146. (if (numberp viewmode) viewmode 1)))))
  147. (save-excursion
  148. ;; Gee! Metamail does not output to stdout if input comes from
  149. ;; stdin.
  150. (let ((selective-display nil)) ;Disable ^M to nl translation.
  151. (write-region beg end metafile nil 'nomessage))
  152. (if buffer
  153. (set-buffer buffer))
  154. (setq buffer-read-only nil)
  155. ;; Clear destination buffer.
  156. (if (eq curbuf (current-buffer))
  157. (delete-region beg end)
  158. (delete-region (point-min) (point-max)))
  159. ;; We have to pass the environment variable KEYHEADS to display
  160. ;; all header fields. Metamail should have an optional argument
  161. ;; to pass such information directly.
  162. (let ((process-environment
  163. (append process-environment
  164. metamail-environment option-environment))
  165. (coding-system-for-read 'undecided))
  166. (apply (function call-process)
  167. metamail-program-name
  168. nil
  169. t ;Output to current buffer
  170. (not nodisplay) ;Force redisplay
  171. (append metamail-switches
  172. (list "-m" (or metamail-mailer-name "emacs"))
  173. (list metafile))))
  174. ;; `metamail' may not delete the temporary file!
  175. (condition-case error
  176. (delete-file metafile)
  177. (error nil))
  178. )))
  179. (provide 'metamail)
  180. ;;; metamail.el ends here