1 ;;; metamail.el --- Metamail interface for GNU Emacs
3 ;; Copyright (C) 1993, 1996 Free Software Foundation, Inc.
5 ;; Author: Masanobu UMEDA <umerin@mse.kyutech.ac.jp>
6 ;; Keywords: mail, news, mime, multimedia
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; Note: Metamail does not have all the options which are compatible with
28 ;; the environment variables. For that reason, metamail.el has to
29 ;; hack the environment variables. In addition, there is no way to
30 ;; display all header fields without extra informative body messages
31 ;; which are suppressed by the "-q" option.
33 ;; The idea of using metamail to process MIME messages is from
34 ;; gnus-mime.el by Spike <Spike@world.std.com>.
38 (defgroup metamail nil
39 "Metamail interface for Emacs."
44 (defcustom metamail-program-name
"metamail"
45 "*Metamail program name."
49 (defcustom metamail-mailer-name
"emacs"
50 "*Mailer name set to MM_MAILER environment variable."
54 (defvar metamail-environment
'("KEYHEADS=*" "MM_QUIET=1")
55 "*Environment variables passed to `metamail'.
56 It must be a list of strings that have the format ENVVARNAME=VALUE.
57 It is not expected to be altered globally by `set' or `setq'.
58 Instead, change its value temporary using `let' or `let*' form.")
60 (defcustom metamail-switches
'("-x" "-d" "-z")
61 "*Switches for `metamail' program.
62 `-z' is required to remove zap file.
63 It is not expected to be altered globally by `set' or `setq'.
64 Instead, change its value temporary using `let' or `let*' form.
65 `-m MAILER' argument is automatically generated from the
66 `metamail-mailer-name' variable."
67 :type
'(repeat (string :tag
"Switch"))
71 (defun metamail-interpret-header ()
72 "Interpret a header part of a MIME message in current buffer.
73 Its body part is not interpreted at all."
76 (let* ((buffer-read-only nil
)
77 (metamail-switches ;Inhibit processing an empty body.
78 (append metamail-switches
'("-c" "text/plain" "-E" "7bit")))
80 (goto-char (point-min))
81 (search-forward "\n\n" nil
'move
)
82 ;; An extra newline is inserted by metamail if there
83 ;; is no body part. So, insert a dummy body by
87 (metamail-region (point-min) end nil nil
'nodisplay
)
88 ;; Remove an extra newline inserted by myself.
89 (goto-char (point-min))
90 (if (search-forward "\n\n\n" nil t
)
95 (defun metamail-interpret-body (&optional viewmode nodisplay
)
96 "Interpret a body part of a MIME message in current buffer.
97 Optional argument VIEWMODE specifies the value of the
98 EMACS_VIEW_MODE environment variable (defaulted to 1).
99 Optional argument NODISPLAY non-nil means buffer is not
100 redisplayed as output is inserted.
101 Its header part is not interpreted at all."
107 (goto-char (point-min))
108 (search-forward "\n\n" nil t
)
110 ;; Find Content-Type and Content-Transfer-Encoding from the header.
112 (narrow-to-region (point-min) end
)
114 (or (mail-fetch-field "Content-Type") "text/plain"))
116 (or (mail-fetch-field "Content-Transfer-Encoding") "7bit")))
117 ;; Interpret the body part only.
118 (let ((metamail-switches ;Process body part only.
119 (append metamail-switches
120 (list "-b" "-c" contype
"-E" encoding
))))
121 (metamail-region end
(point-max) viewmode nil nodisplay
))
122 ;; Mode specific hack.
123 (cond ((eq major-mode
'rmail-mode
)
124 ;; Adjust the marker of this message if in Rmail mode buffer.
125 (set-marker (aref rmail-message-vector
(1+ rmail-current-message
))
130 (defun metamail-buffer (&optional viewmode buffer nodisplay
)
131 "Process current buffer through `metamail'.
132 Optional argument VIEWMODE specifies the value of the
133 EMACS_VIEW_MODE environment variable (defaulted to 1).
134 Optional argument BUFFER specifies a buffer to be filled (nil
136 Optional argument NODISPLAY non-nil means buffer is not
137 redisplayed as output is inserted."
139 (metamail-region (point-min) (point-max) viewmode buffer nodisplay
))
142 (defun metamail-region (beg end
&optional viewmode buffer nodisplay
)
143 "Process current region through 'metamail'.
144 Optional argument VIEWMODE specifies the value of the
145 EMACS_VIEW_MODE environment variable (defaulted to 1).
146 Optional argument BUFFER specifies a buffer to be filled (nil
148 Optional argument NODISPLAY non-nil means buffer is not
149 redisplayed as output is inserted."
151 (let ((curbuf (current-buffer))
152 (buffer-read-only nil
)
153 (metafile (make-temp-file "metamail"))
155 (list (format "EMACS_VIEW_MODE=%d"
156 (if (numberp viewmode
) viewmode
1)))))
158 ;; Gee! Metamail does not ouput to stdout if input comes from
160 (let ((selective-display nil
)) ;Disable ^M to nl translation.
161 (write-region beg end metafile nil
'nomessage
))
164 (setq buffer-read-only nil
)
165 ;; Clear destination buffer.
166 (if (eq curbuf
(current-buffer))
167 (delete-region beg end
)
168 (delete-region (point-min) (point-max)))
169 ;; We have to pass the environment variable KEYHEADS to display
170 ;; all header fields. Metamail should have an optional argument
171 ;; to pass such information directly.
172 (let ((process-environment
173 (append process-environment
174 metamail-environment option-environment
))
175 (coding-system-for-read 'undecided
))
176 (apply (function call-process
)
177 metamail-program-name
179 t
;Output to current buffer
180 (not nodisplay
) ;Force redisplay
181 (append metamail-switches
182 (list "-m" (or metamail-mailer-name
"emacs"))
184 ;; `metamail' may not delete the temporary file!
185 (condition-case error
186 (delete-file metafile
)
192 ;;; arch-tag: 52c0cb6f-d800-4776-9789-f0275cb5490e
193 ;;; metamail.el ends here