1 ;;; metamail.el --- Metamail interface for GNU Emacs
3 ;; Copyright (C) 1993, 1996, 2001-2012 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 3 of the License, or
13 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
25 ;; Note: Metamail does not have all the options which are compatible with
26 ;; the environment variables. For that reason, metamail.el has to
27 ;; hack the environment variables. In addition, there is no way to
28 ;; display all header fields without extra informative body messages
29 ;; which are suppressed by the "-q" option.
31 ;; The idea of using metamail to process MIME messages is from
32 ;; gnus-mime.el by Spike <Spike@world.std.com>.
36 (defvar rmail-current-message
)
37 (defvar rmail-message-vector
)
39 (defgroup metamail nil
40 "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 ;; This mode specific hack is no longer appropriate in mbox Rmail.
123 ;; Pre-mbox, we have just modified the actual folder, so we
124 ;; update the message-vector with the new end position of the
125 ;; current message. In mbox Rmail, all we have done is modify a
126 ;; display copy of the message. Note also that point-max is a
127 ;; marker in the wrong buffer: the message-vector contains
128 ;; markers in rmail-view-buffer (which is not in rmail-mode).
129 ;; So this hack actually breaks the message-vector.
130 ;; If you're calling this on the actual rmail-view-buffer (or a
131 ;; non-swapped rmail-buffer), you would still need this hack.
132 ;; But you're not going to do that.
133 ;;; (cond ((eq major-mode 'rmail-mode)
134 ;;; ;; Adjust the marker of this message if in Rmail mode buffer.
135 ;;; (set-marker (aref rmail-message-vector (1+ rmail-current-message))
140 (defun metamail-buffer (&optional viewmode buffer nodisplay
)
141 "Process current buffer through `metamail'.
142 Optional argument VIEWMODE specifies the value of the
143 EMACS_VIEW_MODE environment variable (defaulted to 1).
144 Optional argument BUFFER specifies a buffer to be filled (nil
146 Optional argument NODISPLAY non-nil means buffer is not
147 redisplayed as output is inserted."
149 (metamail-region (point-min) (point-max) viewmode buffer nodisplay
))
152 (defun metamail-region (beg end
&optional viewmode buffer nodisplay
)
153 "Process current region through 'metamail'.
154 Optional argument VIEWMODE specifies the value of the
155 EMACS_VIEW_MODE environment variable (defaulted to 1).
156 Optional argument BUFFER specifies a buffer to be filled (nil
158 Optional argument NODISPLAY non-nil means buffer is not
159 redisplayed as output is inserted."
161 (let ((curbuf (current-buffer))
162 (buffer-read-only nil
)
163 (metafile (make-temp-file "metamail"))
165 (list (format "EMACS_VIEW_MODE=%d"
166 (if (numberp viewmode
) viewmode
1)))))
168 ;; Gee! Metamail does not output to stdout if input comes from
170 (let ((selective-display nil
)) ;Disable ^M to nl translation.
171 (write-region beg end metafile nil
'nomessage
))
174 (setq buffer-read-only nil
)
175 ;; Clear destination buffer.
176 (if (eq curbuf
(current-buffer))
177 (delete-region beg end
)
178 (delete-region (point-min) (point-max)))
179 ;; We have to pass the environment variable KEYHEADS to display
180 ;; all header fields. Metamail should have an optional argument
181 ;; to pass such information directly.
182 (let ((process-environment
183 (append process-environment
184 metamail-environment option-environment
))
185 (coding-system-for-read 'undecided
))
186 (apply (function call-process
)
187 metamail-program-name
189 t
;Output to current buffer
190 (not nodisplay
) ;Force redisplay
191 (append metamail-switches
192 (list "-m" (or metamail-mailer-name
"emacs"))
194 ;; `metamail' may not delete the temporary file!
195 (condition-case error
196 (delete-file metafile
)
202 ;;; metamail.el ends here