1 ;;; metamail.el --- Metamail interface for GNU Emacs
3 ;; Copyright (C) 1993, 1996 Masanobu UMEDA
5 ;; Author: Masanobu UMEDA <umerin@mse.kyutech.ac.jp>
6 ;; Version: $Id: metamail.el,v 1.7 1997/05/05 11:51:44 rms Exp rms $
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 2, or (at your option)
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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; The latest version will be at:
29 ;; ftp://ftp.kyutech.ac.jp/pub/MultiMedia/mime/emacs-mime-tools.shar
31 ;; Note: Metamail does not have all options which is compatible with
32 ;; the environment variables. For that reason, matamail.el have to
33 ;; hack the environment variables. In addition, there is no way to
34 ;; display all header fields without extra informative body messages
35 ;; which are suppressed by "-q" option.
37 ;; The following definition is what I'm using with GNUS 4:
38 ;;(setq gnus-show-mime-method
41 ;; (metamail-interpret-header)
42 ;; (let ((metamail-switches ;Suppress header fields in a body.
43 ;; (append metamail-switches '("-q"))))
44 ;; (metamail-interpret-body)))))
46 ;; The idea of using metamail to process MIME messages is from
47 ;; gnus-mime.el by Spike <Spike@world.std.com>.
51 (defgroup metamail nil
52 "Metamail interface for Emacs."
57 (defcustom metamail-program-name
"metamail"
58 "*Metamail program name."
62 (defcustom metamail-mailer-name
"emacs"
63 "*Mailer name set to MM_MAILER environment variable."
67 (defvar metamail-environment
'("KEYHEADS=*" "MM_QUIET=1")
68 "*Environment variables passed to `metamail'.
69 It must be a list of strings that have the format ENVVARNAME=VALUE.
70 It is not expected to be altered globally by `set' or `setq'.
71 Instead, change its value temporary using `let' or `let*' form.")
73 (defcustom metamail-switches
'("-x" "-d" "-z")
74 "*Switches for `metamail' program.
75 `-z' is required to remove zap file.
76 It is not expected to be altered globally by `set' or `setq'.
77 Instead, change its value temporary using `let' or `let*' form.
78 `-m MAILER' argument is automatically generated from the
79 `metamail-mailer-name' variable."
80 :type
'(repeat (string :tag
"Switch"))
84 (defun metamail-interpret-header ()
85 "Interpret a header part of a MIME message in current buffer.
86 Its body part is not interpreted at all."
89 (let* ((buffer-read-only nil
)
90 (metamail-switches ;Inhibit processing an empty body.
91 (append metamail-switches
'("-c" "text/plain" "-E" "7bit")))
93 (goto-char (point-min))
94 (search-forward "\n\n" nil
'move
)
95 ;; An extra newline is inserted by metamail if there
96 ;; is no body part. So, insert a dummy body by
100 (metamail-region (point-min) end nil nil
'nodisplay
)
101 ;; Remove an extra newline inserted by myself.
102 (goto-char (point-min))
103 (if (search-forward "\n\n\n" nil t
)
108 (defun metamail-interpret-body (&optional viewmode nodisplay
)
109 "Interpret a body part of a MIME message in current buffer.
110 Optional argument VIEWMODE specifies the value of the
111 EMACS_VIEW_MODE environment variable (defaulted to 1).
112 Optional argument NODISPLAY non-nil means buffer is not
113 redisplayed as output is inserted.
114 Its header part is not interpreted at all."
120 (goto-char (point-min))
121 (search-forward "\n\n" nil t
)
123 ;; Find Content-Type and Content-Transfer-Encoding from the header.
125 (narrow-to-region (point-min) end
)
127 (or (mail-fetch-field "Content-Type") "text/plain"))
129 (or (mail-fetch-field "Content-Transfer-Encoding") "7bit")))
130 ;; Interpret the body part only.
131 (let ((metamail-switches ;Process body part only.
132 (append metamail-switches
133 (list "-b" "-c" contype
"-E" encoding
))))
134 (metamail-region end
(point-max) viewmode nil nodisplay
))
135 ;; Mode specific hack.
136 (cond ((eq major-mode
'rmail-mode
)
137 ;; Adjust the marker of this message if in Rmail mode buffer.
138 (set-marker (aref rmail-message-vector
(1+ rmail-current-message
))
143 (defun metamail-buffer (&optional viewmode buffer nodisplay
)
144 "Process current buffer through `metamail'.
145 Optional argument VIEWMODE specifies the value of the
146 EMACS_VIEW_MODE environment variable (defaulted to 1).
147 Optional argument BUFFER specifies a buffer to be filled (nil
149 Optional argument NODISPLAY non-nil means buffer is not
150 redisplayed as output is inserted."
152 (metamail-region (point-min) (point-max) viewmode buffer nodisplay
))
155 (defun metamail-region (beg end
&optional viewmode buffer nodisplay
)
156 "Process current region through 'metamail'.
157 Optional argument VIEWMODE specifies the value of the
158 EMACS_VIEW_MODE environment variable (defaulted to 1).
159 Optional argument BUFFER specifies a buffer to be filled (nil
161 Optional argument NODISPLAY non-nil means buffer is not
162 redisplayed as output is inserted."
164 (let ((curbuf (current-buffer))
165 (buffer-read-only nil
)
166 (metafile (make-temp-name "/tmp/metamail"))
168 (list (concat "EMACS_VIEW_MODE="
169 (if (numberp viewmode
) viewmode
1)))))
171 ;; Gee! Metamail does not ouput to stdout if input comes from
173 (let ((selective-display nil
)) ;Disable ^M to nl translation.
174 (write-region beg end metafile nil
'nomessage
))
177 (setq buffer-read-only nil
)
178 ;; Clear destination buffer.
179 (if (eq curbuf
(current-buffer))
180 (delete-region beg end
)
181 (delete-region (point-min) (point-max)))
182 ;; We have to pass the environment variable KEYHEADS to display
183 ;; all header fields. Metamail should have an optional argument
184 ;; to pass such information directly.
185 (let ((process-environment
186 (append process-environment
187 metamail-environment option-environment
))
188 (coding-system-for-read 'undecided
))
189 (apply (function call-process
)
190 metamail-program-name
192 t
;Output to current buffer
193 (not nodisplay
) ;Force redisplay
194 (append metamail-switches
195 (list "-m" (or metamail-mailer-name
"emacs"))
197 ;; `metamail' may not delete the temporary file!
198 (condition-case error
199 (delete-file metafile
)
205 ;;; metamail.el ends here