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