1 ;;; metamail.el --- Metamail interface for GNU Emacs
3 ;; Copyright (C) 1993, 1996, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Masanobu UMEDA <umerin@mse.kyutech.ac.jp>
7 ;; Keywords: mail, news, mime, multimedia
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/>.
26 ;; Note: Metamail does not have all the options which are compatible with
27 ;; the environment variables. For that reason, metamail.el has to
28 ;; hack the environment variables. In addition, there is no way to
29 ;; display all header fields without extra informative body messages
30 ;; which are suppressed by the "-q" option.
32 ;; The idea of using metamail to process MIME messages is from
33 ;; gnus-mime.el by Spike <Spike@world.std.com>.
37 (defvar rmail-current-message
)
38 (defvar rmail-message-vector
)
40 (defgroup metamail nil
41 "Metamail interface for Emacs."
46 (defcustom metamail-program-name
"metamail"
47 "*Metamail program name."
51 (defcustom metamail-mailer-name
"emacs"
52 "*Mailer name set to MM_MAILER environment variable."
56 (defvar metamail-environment
'("KEYHEADS=*" "MM_QUIET=1")
57 "*Environment variables passed to `metamail'.
58 It must be a list of strings that have the format ENVVARNAME=VALUE.
59 It is not expected to be altered globally by `set' or `setq'.
60 Instead, change its value temporary using `let' or `let*' form.")
62 (defcustom metamail-switches
'("-x" "-d" "-z")
63 "*Switches for `metamail' program.
64 `-z' is required to remove zap file.
65 It is not expected to be altered globally by `set' or `setq'.
66 Instead, change its value temporary using `let' or `let*' form.
67 `-m MAILER' argument is automatically generated from the
68 `metamail-mailer-name' variable."
69 :type
'(repeat (string :tag
"Switch"))
73 (defun metamail-interpret-header ()
74 "Interpret a header part of a MIME message in current buffer.
75 Its body part is not interpreted at all."
78 (let* ((buffer-read-only nil
)
79 (metamail-switches ;Inhibit processing an empty body.
80 (append metamail-switches
'("-c" "text/plain" "-E" "7bit")))
82 (goto-char (point-min))
83 (search-forward "\n\n" nil
'move
)
84 ;; An extra newline is inserted by metamail if there
85 ;; is no body part. So, insert a dummy body by
89 (metamail-region (point-min) end nil nil
'nodisplay
)
90 ;; Remove an extra newline inserted by myself.
91 (goto-char (point-min))
92 (if (search-forward "\n\n\n" nil t
)
97 (defun metamail-interpret-body (&optional viewmode nodisplay
)
98 "Interpret a body part of a MIME message in current buffer.
99 Optional argument VIEWMODE specifies the value of the
100 EMACS_VIEW_MODE environment variable (defaulted to 1).
101 Optional argument NODISPLAY non-nil means buffer is not
102 redisplayed as output is inserted.
103 Its header part is not interpreted at all."
109 (goto-char (point-min))
110 (search-forward "\n\n" nil t
)
112 ;; Find Content-Type and Content-Transfer-Encoding from the header.
114 (narrow-to-region (point-min) end
)
116 (or (mail-fetch-field "Content-Type") "text/plain"))
118 (or (mail-fetch-field "Content-Transfer-Encoding") "7bit")))
119 ;; Interpret the body part only.
120 (let ((metamail-switches ;Process body part only.
121 (append metamail-switches
122 (list "-b" "-c" contype
"-E" encoding
))))
123 (metamail-region end
(point-max) viewmode nil nodisplay
))
124 ;; Mode specific hack.
125 (cond ((eq major-mode
'rmail-mode
)
126 ;; Adjust the marker of this message if in Rmail mode buffer.
127 (set-marker (aref rmail-message-vector
(1+ rmail-current-message
))
132 (defun metamail-buffer (&optional viewmode buffer nodisplay
)
133 "Process current buffer 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
138 Optional argument NODISPLAY non-nil means buffer is not
139 redisplayed as output is inserted."
141 (metamail-region (point-min) (point-max) viewmode buffer nodisplay
))
144 (defun metamail-region (beg end
&optional viewmode buffer nodisplay
)
145 "Process current region through 'metamail'.
146 Optional argument VIEWMODE specifies the value of the
147 EMACS_VIEW_MODE environment variable (defaulted to 1).
148 Optional argument BUFFER specifies a buffer to be filled (nil
150 Optional argument NODISPLAY non-nil means buffer is not
151 redisplayed as output is inserted."
153 (let ((curbuf (current-buffer))
154 (buffer-read-only nil
)
155 (metafile (make-temp-file "metamail"))
157 (list (format "EMACS_VIEW_MODE=%d"
158 (if (numberp viewmode
) viewmode
1)))))
160 ;; Gee! Metamail does not ouput to stdout if input comes from
162 (let ((selective-display nil
)) ;Disable ^M to nl translation.
163 (write-region beg end metafile nil
'nomessage
))
166 (setq buffer-read-only nil
)
167 ;; Clear destination buffer.
168 (if (eq curbuf
(current-buffer))
169 (delete-region beg end
)
170 (delete-region (point-min) (point-max)))
171 ;; We have to pass the environment variable KEYHEADS to display
172 ;; all header fields. Metamail should have an optional argument
173 ;; to pass such information directly.
174 (let ((process-environment
175 (append process-environment
176 metamail-environment option-environment
))
177 (coding-system-for-read 'undecided
))
178 (apply (function call-process
)
179 metamail-program-name
181 t
;Output to current buffer
182 (not nodisplay
) ;Force redisplay
183 (append metamail-switches
184 (list "-m" (or metamail-mailer-name
"emacs"))
186 ;; `metamail' may not delete the temporary file!
187 (condition-case error
188 (delete-file metafile
)
194 ;; arch-tag: 52c0cb6f-d800-4776-9789-f0275cb5490e
195 ;;; metamail.el ends here