Merge from emacs--devo--0
[bpt/emacs.git] / lisp / mail / metamail.el
CommitLineData
9ed9f36f
RS
1;;; metamail.el --- Metamail interface for GNU Emacs
2
f2e3589a 3;; Copyright (C) 1993, 1996, 2001, 2002, 2003, 2004,
d7a0267c 4;; 2005, 2006, 2007 Free Software Foundation, Inc.
9ed9f36f
RS
5
6;; Author: Masanobu UMEDA <umerin@mse.kyutech.ac.jp>
9ed9f36f
RS
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
b578f267 22;; along with GNU Emacs; see the file COPYING. If not, write to the
3a35cf56
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
9ed9f36f
RS
25
26;;; Commentary:
27
e873dcf5
DL
28;; Note: Metamail does not have all the options which are compatible with
29;; the environment variables. For that reason, metamail.el has to
9ed9f36f
RS
30;; hack the environment variables. In addition, there is no way to
31;; display all header fields without extra informative body messages
e873dcf5 32;; which are suppressed by the "-q" option.
9ed9f36f
RS
33
34;; The idea of using metamail to process MIME messages is from
35;; gnus-mime.el by Spike <Spike@world.std.com>.
36
37;;; Code:
38
3675c8b1
JB
39(defvar rmail-current-message)
40(defvar rmail-message-vector)
41
69c1dd37
RS
42(defgroup metamail nil
43 "Metamail interface for Emacs."
44 :group 'mail
45 :group 'hypermedia
46 :group 'processes)
9ed9f36f 47
69c1dd37
RS
48(defcustom metamail-program-name "metamail"
49 "*Metamail program name."
50 :type 'string
51 :group 'metamail)
52
53(defcustom metamail-mailer-name "emacs"
54 "*Mailer name set to MM_MAILER environment variable."
55 :type 'string
56 :group 'metamail)
55be1314
RS
57
58(defvar metamail-environment '("KEYHEADS=*" "MM_QUIET=1")
9ed9f36f 59 "*Environment variables passed to `metamail'.
55be1314
RS
60It must be a list of strings that have the format ENVVARNAME=VALUE.
61It is not expected to be altered globally by `set' or `setq'.
62Instead, change its value temporary using `let' or `let*' form.")
9ed9f36f 63
69c1dd37 64(defcustom metamail-switches '("-x" "-d" "-z")
9ed9f36f 65 "*Switches for `metamail' program.
55be1314
RS
66`-z' is required to remove zap file.
67It is not expected to be altered globally by `set' or `setq'.
68Instead, change its value temporary using `let' or `let*' form.
69`-m MAILER' argument is automatically generated from the
69c1dd37
RS
70`metamail-mailer-name' variable."
71 :type '(repeat (string :tag "Switch"))
72 :group 'metamail)
55be1314
RS
73
74;;;###autoload
75(defun metamail-interpret-header ()
76 "Interpret a header part of a MIME message in current buffer.
77Its body part is not interpreted at all."
78 (interactive)
79 (save-excursion
80 (let* ((buffer-read-only nil)
81 (metamail-switches ;Inhibit processing an empty body.
82 (append metamail-switches '("-c" "text/plain" "-E" "7bit")))
83 (end (progn
84 (goto-char (point-min))
85 (search-forward "\n\n" nil 'move)
86 ;; An extra newline is inserted by metamail if there
87 ;; is no body part. So, insert a dummy body by
88 ;; itself.
89 (insert "\n")
90 (point))))
91 (metamail-region (point-min) end nil nil 'nodisplay)
92 ;; Remove an extra newline inserted by myself.
93 (goto-char (point-min))
94 (if (search-forward "\n\n\n" nil t)
95 (delete-char -1))
96 )))
9ed9f36f 97
55be1314
RS
98;;;###autoload
99(defun metamail-interpret-body (&optional viewmode nodisplay)
100 "Interpret a body part of a MIME message in current buffer.
101Optional argument VIEWMODE specifies the value of the
102EMACS_VIEW_MODE environment variable (defaulted to 1).
103Optional argument NODISPLAY non-nil means buffer is not
104redisplayed as output is inserted.
105Its header part is not interpreted at all."
106 (interactive "p")
107 (save-excursion
108 (let ((contype nil)
109 (encoding nil)
110 (end (progn
111 (goto-char (point-min))
112 (search-forward "\n\n" nil t)
113 (point))))
114 ;; Find Content-Type and Content-Transfer-Encoding from the header.
115 (save-restriction
116 (narrow-to-region (point-min) end)
a1506d29 117 (setq contype
55be1314 118 (or (mail-fetch-field "Content-Type") "text/plain"))
a1506d29 119 (setq encoding
55be1314
RS
120 (or (mail-fetch-field "Content-Transfer-Encoding") "7bit")))
121 ;; Interpret the body part only.
122 (let ((metamail-switches ;Process body part only.
123 (append metamail-switches
124 (list "-b" "-c" contype "-E" encoding))))
125 (metamail-region end (point-max) viewmode nil nodisplay))
126 ;; Mode specific hack.
127 (cond ((eq major-mode 'rmail-mode)
128 ;; Adjust the marker of this message if in Rmail mode buffer.
129 (set-marker (aref rmail-message-vector (1+ rmail-current-message))
130 (point-max))))
131 )))
132
133;;;###autoload
134(defun metamail-buffer (&optional viewmode buffer nodisplay)
9ed9f36f 135 "Process current buffer through `metamail'.
55be1314
RS
136Optional argument VIEWMODE specifies the value of the
137EMACS_VIEW_MODE environment variable (defaulted to 1).
138Optional argument BUFFER specifies a buffer to be filled (nil
9ed9f36f 139means current).
55be1314 140Optional argument NODISPLAY non-nil means buffer is not
9ed9f36f 141redisplayed as output is inserted."
55be1314
RS
142 (interactive "p")
143 (metamail-region (point-min) (point-max) viewmode buffer nodisplay))
9ed9f36f 144
55be1314
RS
145;;;###autoload
146(defun metamail-region (beg end &optional viewmode buffer nodisplay)
9ed9f36f 147 "Process current region through 'metamail'.
55be1314
RS
148Optional argument VIEWMODE specifies the value of the
149EMACS_VIEW_MODE environment variable (defaulted to 1).
150Optional argument BUFFER specifies a buffer to be filled (nil
9ed9f36f 151means current).
55be1314 152Optional argument NODISPLAY non-nil means buffer is not
9ed9f36f 153redisplayed as output is inserted."
55be1314 154 (interactive "r\np")
9ed9f36f
RS
155 (let ((curbuf (current-buffer))
156 (buffer-read-only nil)
767d12f2 157 (metafile (make-temp-file "metamail"))
55be1314 158 (option-environment
a1506d29 159 (list (format "EMACS_VIEW_MODE=%d"
55be1314 160 (if (numberp viewmode) viewmode 1)))))
9ed9f36f 161 (save-excursion
55be1314 162 ;; Gee! Metamail does not ouput to stdout if input comes from
9ed9f36f 163 ;; stdin.
5ab5daad 164 (let ((selective-display nil)) ;Disable ^M to nl translation.
55be1314 165 (write-region beg end metafile nil 'nomessage))
9ed9f36f
RS
166 (if buffer
167 (set-buffer buffer))
168 (setq buffer-read-only nil)
169 ;; Clear destination buffer.
170 (if (eq curbuf (current-buffer))
171 (delete-region beg end)
172 (delete-region (point-min) (point-max)))
173 ;; We have to pass the environment variable KEYHEADS to display
174 ;; all header fields. Metamail should have an optional argument
175 ;; to pass such information directly.
176 (let ((process-environment
55be1314 177 (append process-environment
5ab5daad
RS
178 metamail-environment option-environment))
179 (coding-system-for-read 'undecided))
9ed9f36f
RS
180 (apply (function call-process)
181 metamail-program-name
182 nil
183 t ;Output to current buffer
184 (not nodisplay) ;Force redisplay
55be1314
RS
185 (append metamail-switches
186 (list "-m" (or metamail-mailer-name "emacs"))
187 (list metafile))))
9ed9f36f
RS
188 ;; `metamail' may not delete the temporary file!
189 (condition-case error
190 (delete-file metafile)
191 (error nil))
192 )))
193
9ed9f36f
RS
194(provide 'metamail)
195
ab5796a9 196;;; arch-tag: 52c0cb6f-d800-4776-9789-f0275cb5490e
9ed9f36f 197;;; metamail.el ends here