(lisp-font-lock-keywords-2): Added `defpackage'.
[bpt/emacs.git] / lisp / mail / metamail.el
1 ;;; metamail.el --- Metamail interface for GNU Emacs
2
3 ;; Copyright (C) 1993, 1996 Masanobu UMEDA
4
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
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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.
25
26 ;;; Commentary:
27
28 ;; The latest version will be at:
29 ;; ftp://ftp.kyutech.ac.jp/pub/MultiMedia/mime/emacs-mime-tools.shar
30
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.
36
37 ;; The following definition is what I'm using with GNUS 4:
38 ;;(setq gnus-show-mime-method
39 ;; (function
40 ;; (lambda ()
41 ;; (metamail-interpret-header)
42 ;; (let ((metamail-switches ;Suppress header fields in a body.
43 ;; (append metamail-switches '("-q"))))
44 ;; (metamail-interpret-body)))))
45
46 ;; The idea of using metamail to process MIME messages is from
47 ;; gnus-mime.el by Spike <Spike@world.std.com>.
48
49 ;;; Code:
50
51 (defgroup metamail nil
52 "Metamail interface for Emacs."
53 :group 'mail
54 :group 'hypermedia
55 :group 'processes)
56
57 (defcustom metamail-program-name "metamail"
58 "*Metamail program name."
59 :type 'string
60 :group 'metamail)
61
62 (defcustom metamail-mailer-name "emacs"
63 "*Mailer name set to MM_MAILER environment variable."
64 :type 'string
65 :group 'metamail)
66
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.")
72
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"))
81 :group 'metamail)
82
83 ;;;###autoload
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."
87 (interactive)
88 (save-excursion
89 (let* ((buffer-read-only nil)
90 (metamail-switches ;Inhibit processing an empty body.
91 (append metamail-switches '("-c" "text/plain" "-E" "7bit")))
92 (end (progn
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
97 ;; itself.
98 (insert "\n")
99 (point))))
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)
104 (delete-char -1))
105 )))
106
107 ;;;###autoload
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."
115 (interactive "p")
116 (save-excursion
117 (let ((contype nil)
118 (encoding nil)
119 (end (progn
120 (goto-char (point-min))
121 (search-forward "\n\n" nil t)
122 (point))))
123 ;; Find Content-Type and Content-Transfer-Encoding from the header.
124 (save-restriction
125 (narrow-to-region (point-min) end)
126 (setq contype
127 (or (mail-fetch-field "Content-Type") "text/plain"))
128 (setq encoding
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))
139 (point-max))))
140 )))
141
142 ;;;###autoload
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
148 means current).
149 Optional argument NODISPLAY non-nil means buffer is not
150 redisplayed as output is inserted."
151 (interactive "p")
152 (metamail-region (point-min) (point-max) viewmode buffer nodisplay))
153
154 ;;;###autoload
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
160 means current).
161 Optional argument NODISPLAY non-nil means buffer is not
162 redisplayed as output is inserted."
163 (interactive "r\np")
164 (let ((curbuf (current-buffer))
165 (buffer-read-only nil)
166 (metafile (make-temp-name "/tmp/metamail"))
167 (option-environment
168 (list (concat "EMACS_VIEW_MODE="
169 (if (numberp viewmode) viewmode 1)))))
170 (save-excursion
171 ;; Gee! Metamail does not ouput to stdout if input comes from
172 ;; stdin.
173 (let ((selective-display nil)) ;Disable ^M to nl translation.
174 (write-region beg end metafile nil 'nomessage))
175 (if buffer
176 (set-buffer buffer))
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
191 nil
192 t ;Output to current buffer
193 (not nodisplay) ;Force redisplay
194 (append metamail-switches
195 (list "-m" (or metamail-mailer-name "emacs"))
196 (list metafile))))
197 ;; `metamail' may not delete the temporary file!
198 (condition-case error
199 (delete-file metafile)
200 (error nil))
201 )))
202
203 (provide 'metamail)
204
205 ;;; metamail.el ends here