[HAVE_TERMCAP_H]: Include <termcap.h>.
[emacs.git] / lisp / mail / metamail.el
blobc3f017f023123e5a2d10ebeb7bcd2a037bfaebb9
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 ;; Version: $Id: metamail.el,v 1.12 1999/08/28 18:25:16 rms Exp $
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)
14 ;; 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; 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.
26 ;;; Commentary:
28 ;; The latest version will be at:
29 ;; ftp://ftp.kyutech.ac.jp/pub/MultiMedia/mime/emacs-mime-tools.shar
30 ;; [This is probably _not_ the latest version; as of Jan 1999, it
31 ;; is dated 1995.]
33 ;; Note: Metamail does not have all options which is compatible with
34 ;; the environment variables. For that reason, matamail.el have to
35 ;; hack the environment variables. In addition, there is no way to
36 ;; display all header fields without extra informative body messages
37 ;; which are suppressed by "-q" option.
39 ;; The following definition is what I'm using with GNUS 4:
40 ;;(setq gnus-show-mime-method
41 ;; (function
42 ;; (lambda ()
43 ;; (metamail-interpret-header)
44 ;; (let ((metamail-switches ;Suppress header fields in a body.
45 ;; (append metamail-switches '("-q"))))
46 ;; (metamail-interpret-body)))))
48 ;; The idea of using metamail to process MIME messages is from
49 ;; gnus-mime.el by Spike <Spike@world.std.com>.
51 ;;; Code:
53 (defgroup metamail nil
54 "Metamail interface for Emacs."
55 :group 'mail
56 :group 'hypermedia
57 :group 'processes)
59 (defcustom metamail-program-name "metamail"
60 "*Metamail program name."
61 :type 'string
62 :group 'metamail)
64 (defcustom metamail-mailer-name "emacs"
65 "*Mailer name set to MM_MAILER environment variable."
66 :type 'string
67 :group 'metamail)
69 (defvar metamail-environment '("KEYHEADS=*" "MM_QUIET=1")
70 "*Environment variables passed to `metamail'.
71 It must be a list of strings that have the format ENVVARNAME=VALUE.
72 It is not expected to be altered globally by `set' or `setq'.
73 Instead, change its value temporary using `let' or `let*' form.")
75 (defcustom metamail-switches '("-x" "-d" "-z")
76 "*Switches for `metamail' program.
77 `-z' is required to remove zap file.
78 It is not expected to be altered globally by `set' or `setq'.
79 Instead, change its value temporary using `let' or `let*' form.
80 `-m MAILER' argument is automatically generated from the
81 `metamail-mailer-name' variable."
82 :type '(repeat (string :tag "Switch"))
83 :group 'metamail)
85 ;;;###autoload
86 (defun metamail-interpret-header ()
87 "Interpret a header part of a MIME message in current buffer.
88 Its body part is not interpreted at all."
89 (interactive)
90 (save-excursion
91 (let* ((buffer-read-only nil)
92 (metamail-switches ;Inhibit processing an empty body.
93 (append metamail-switches '("-c" "text/plain" "-E" "7bit")))
94 (end (progn
95 (goto-char (point-min))
96 (search-forward "\n\n" nil 'move)
97 ;; An extra newline is inserted by metamail if there
98 ;; is no body part. So, insert a dummy body by
99 ;; itself.
100 (insert "\n")
101 (point))))
102 (metamail-region (point-min) end nil nil 'nodisplay)
103 ;; Remove an extra newline inserted by myself.
104 (goto-char (point-min))
105 (if (search-forward "\n\n\n" nil t)
106 (delete-char -1))
109 ;;;###autoload
110 (defun metamail-interpret-body (&optional viewmode nodisplay)
111 "Interpret a body part of a MIME message in current buffer.
112 Optional argument VIEWMODE specifies the value of the
113 EMACS_VIEW_MODE environment variable (defaulted to 1).
114 Optional argument NODISPLAY non-nil means buffer is not
115 redisplayed as output is inserted.
116 Its header part is not interpreted at all."
117 (interactive "p")
118 (save-excursion
119 (let ((contype nil)
120 (encoding nil)
121 (end (progn
122 (goto-char (point-min))
123 (search-forward "\n\n" nil t)
124 (point))))
125 ;; Find Content-Type and Content-Transfer-Encoding from the header.
126 (save-restriction
127 (narrow-to-region (point-min) end)
128 (setq contype
129 (or (mail-fetch-field "Content-Type") "text/plain"))
130 (setq encoding
131 (or (mail-fetch-field "Content-Transfer-Encoding") "7bit")))
132 ;; Interpret the body part only.
133 (let ((metamail-switches ;Process body part only.
134 (append metamail-switches
135 (list "-b" "-c" contype "-E" encoding))))
136 (metamail-region end (point-max) viewmode nil nodisplay))
137 ;; Mode specific hack.
138 (cond ((eq major-mode 'rmail-mode)
139 ;; Adjust the marker of this message if in Rmail mode buffer.
140 (set-marker (aref rmail-message-vector (1+ rmail-current-message))
141 (point-max))))
144 ;;;###autoload
145 (defun metamail-buffer (&optional viewmode buffer nodisplay)
146 "Process current buffer through `metamail'.
147 Optional argument VIEWMODE specifies the value of the
148 EMACS_VIEW_MODE environment variable (defaulted to 1).
149 Optional argument BUFFER specifies a buffer to be filled (nil
150 means current).
151 Optional argument NODISPLAY non-nil means buffer is not
152 redisplayed as output is inserted."
153 (interactive "p")
154 (metamail-region (point-min) (point-max) viewmode buffer nodisplay))
156 ;;;###autoload
157 (defun metamail-region (beg end &optional viewmode buffer nodisplay)
158 "Process current region through 'metamail'.
159 Optional argument VIEWMODE specifies the value of the
160 EMACS_VIEW_MODE environment variable (defaulted to 1).
161 Optional argument BUFFER specifies a buffer to be filled (nil
162 means current).
163 Optional argument NODISPLAY non-nil means buffer is not
164 redisplayed as output is inserted."
165 (interactive "r\np")
166 (let ((curbuf (current-buffer))
167 (buffer-read-only nil)
168 (metafile (make-temp-file "metamail"))
169 (option-environment
170 (list (format "EMACS_VIEW_MODE=%d"
171 (if (numberp viewmode) viewmode 1)))))
172 (save-excursion
173 ;; Gee! Metamail does not ouput to stdout if input comes from
174 ;; stdin.
175 (let ((selective-display nil)) ;Disable ^M to nl translation.
176 (write-region beg end metafile nil 'nomessage))
177 (if buffer
178 (set-buffer buffer))
179 (setq buffer-read-only nil)
180 ;; Clear destination buffer.
181 (if (eq curbuf (current-buffer))
182 (delete-region beg end)
183 (delete-region (point-min) (point-max)))
184 ;; We have to pass the environment variable KEYHEADS to display
185 ;; all header fields. Metamail should have an optional argument
186 ;; to pass such information directly.
187 (let ((process-environment
188 (append process-environment
189 metamail-environment option-environment))
190 (coding-system-for-read 'undecided))
191 (apply (function call-process)
192 metamail-program-name
194 t ;Output to current buffer
195 (not nodisplay) ;Force redisplay
196 (append metamail-switches
197 (list "-m" (or metamail-mailer-name "emacs"))
198 (list metafile))))
199 ;; `metamail' may not delete the temporary file!
200 (condition-case error
201 (delete-file metafile)
202 (error nil))
205 (provide 'metamail)
207 ;;; metamail.el ends here