Commit | Line | Data |
---|---|---|
9ed9f36f RS |
1 | ;;; metamail.el --- Metamail interface for GNU Emacs |
2 | ||
11d86ba0 | 3 | ;; Copyright (C) 1993, 1996 Free Software Foundation, Inc. |
9ed9f36f RS |
4 | |
5 | ;; Author: Masanobu UMEDA <umerin@mse.kyutech.ac.jp> | |
a1506d29 | 6 | ;; Version: $Id: metamail.el,v 1.14 2000/11/12 00:22:02 fx Exp $ |
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 EN |
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. | |
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 | ||
69c1dd37 RS |
39 | (defgroup metamail nil |
40 | "Metamail interface for Emacs." | |
41 | :group 'mail | |
42 | :group 'hypermedia | |
43 | :group 'processes) | |
9ed9f36f | 44 | |
69c1dd37 RS |
45 | (defcustom metamail-program-name "metamail" |
46 | "*Metamail program name." | |
47 | :type 'string | |
48 | :group 'metamail) | |
49 | ||
50 | (defcustom metamail-mailer-name "emacs" | |
51 | "*Mailer name set to MM_MAILER environment variable." | |
52 | :type 'string | |
53 | :group 'metamail) | |
55be1314 RS |
54 | |
55 | (defvar metamail-environment '("KEYHEADS=*" "MM_QUIET=1") | |
9ed9f36f | 56 | "*Environment variables passed to `metamail'. |
55be1314 RS |
57 | It must be a list of strings that have the format ENVVARNAME=VALUE. |
58 | It is not expected to be altered globally by `set' or `setq'. | |
59 | Instead, change its value temporary using `let' or `let*' form.") | |
9ed9f36f | 60 | |
69c1dd37 | 61 | (defcustom metamail-switches '("-x" "-d" "-z") |
9ed9f36f | 62 | "*Switches for `metamail' program. |
55be1314 RS |
63 | `-z' is required to remove zap file. |
64 | It is not expected to be altered globally by `set' or `setq'. | |
65 | Instead, change its value temporary using `let' or `let*' form. | |
66 | `-m MAILER' argument is automatically generated from the | |
69c1dd37 RS |
67 | `metamail-mailer-name' variable." |
68 | :type '(repeat (string :tag "Switch")) | |
69 | :group 'metamail) | |
55be1314 RS |
70 | |
71 | ;;;###autoload | |
72 | (defun metamail-interpret-header () | |
73 | "Interpret a header part of a MIME message in current buffer. | |
74 | Its body part is not interpreted at all." | |
75 | (interactive) | |
76 | (save-excursion | |
77 | (let* ((buffer-read-only nil) | |
78 | (metamail-switches ;Inhibit processing an empty body. | |
79 | (append metamail-switches '("-c" "text/plain" "-E" "7bit"))) | |
80 | (end (progn | |
81 | (goto-char (point-min)) | |
82 | (search-forward "\n\n" nil 'move) | |
83 | ;; An extra newline is inserted by metamail if there | |
84 | ;; is no body part. So, insert a dummy body by | |
85 | ;; itself. | |
86 | (insert "\n") | |
87 | (point)))) | |
88 | (metamail-region (point-min) end nil nil 'nodisplay) | |
89 | ;; Remove an extra newline inserted by myself. | |
90 | (goto-char (point-min)) | |
91 | (if (search-forward "\n\n\n" nil t) | |
92 | (delete-char -1)) | |
93 | ))) | |
9ed9f36f | 94 | |
55be1314 RS |
95 | ;;;###autoload |
96 | (defun metamail-interpret-body (&optional viewmode nodisplay) | |
97 | "Interpret a body part of a MIME message in current buffer. | |
98 | Optional argument VIEWMODE specifies the value of the | |
99 | EMACS_VIEW_MODE environment variable (defaulted to 1). | |
100 | Optional argument NODISPLAY non-nil means buffer is not | |
101 | redisplayed as output is inserted. | |
102 | Its header part is not interpreted at all." | |
103 | (interactive "p") | |
104 | (save-excursion | |
105 | (let ((contype nil) | |
106 | (encoding nil) | |
107 | (end (progn | |
108 | (goto-char (point-min)) | |
109 | (search-forward "\n\n" nil t) | |
110 | (point)))) | |
111 | ;; Find Content-Type and Content-Transfer-Encoding from the header. | |
112 | (save-restriction | |
113 | (narrow-to-region (point-min) end) | |
a1506d29 | 114 | (setq contype |
55be1314 | 115 | (or (mail-fetch-field "Content-Type") "text/plain")) |
a1506d29 | 116 | (setq encoding |
55be1314 RS |
117 | (or (mail-fetch-field "Content-Transfer-Encoding") "7bit"))) |
118 | ;; Interpret the body part only. | |
119 | (let ((metamail-switches ;Process body part only. | |
120 | (append metamail-switches | |
121 | (list "-b" "-c" contype "-E" encoding)))) | |
122 | (metamail-region end (point-max) viewmode nil nodisplay)) | |
123 | ;; Mode specific hack. | |
124 | (cond ((eq major-mode 'rmail-mode) | |
125 | ;; Adjust the marker of this message if in Rmail mode buffer. | |
126 | (set-marker (aref rmail-message-vector (1+ rmail-current-message)) | |
127 | (point-max)))) | |
128 | ))) | |
129 | ||
130 | ;;;###autoload | |
131 | (defun metamail-buffer (&optional viewmode buffer nodisplay) | |
9ed9f36f | 132 | "Process current buffer through `metamail'. |
55be1314 RS |
133 | Optional argument VIEWMODE specifies the value of the |
134 | EMACS_VIEW_MODE environment variable (defaulted to 1). | |
135 | Optional argument BUFFER specifies a buffer to be filled (nil | |
9ed9f36f | 136 | means current). |
55be1314 | 137 | Optional argument NODISPLAY non-nil means buffer is not |
9ed9f36f | 138 | redisplayed as output is inserted." |
55be1314 RS |
139 | (interactive "p") |
140 | (metamail-region (point-min) (point-max) viewmode buffer nodisplay)) | |
9ed9f36f | 141 | |
55be1314 RS |
142 | ;;;###autoload |
143 | (defun metamail-region (beg end &optional viewmode buffer nodisplay) | |
9ed9f36f | 144 | "Process current region through 'metamail'. |
55be1314 RS |
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 | |
9ed9f36f | 148 | means current). |
55be1314 | 149 | Optional argument NODISPLAY non-nil means buffer is not |
9ed9f36f | 150 | redisplayed as output is inserted." |
55be1314 | 151 | (interactive "r\np") |
9ed9f36f RS |
152 | (let ((curbuf (current-buffer)) |
153 | (buffer-read-only nil) | |
767d12f2 | 154 | (metafile (make-temp-file "metamail")) |
55be1314 | 155 | (option-environment |
a1506d29 | 156 | (list (format "EMACS_VIEW_MODE=%d" |
55be1314 | 157 | (if (numberp viewmode) viewmode 1))))) |
9ed9f36f | 158 | (save-excursion |
55be1314 | 159 | ;; Gee! Metamail does not ouput to stdout if input comes from |
9ed9f36f | 160 | ;; stdin. |
5ab5daad | 161 | (let ((selective-display nil)) ;Disable ^M to nl translation. |
55be1314 | 162 | (write-region beg end metafile nil 'nomessage)) |
9ed9f36f RS |
163 | (if buffer |
164 | (set-buffer buffer)) | |
165 | (setq buffer-read-only nil) | |
166 | ;; Clear destination buffer. | |
167 | (if (eq curbuf (current-buffer)) | |
168 | (delete-region beg end) | |
169 | (delete-region (point-min) (point-max))) | |
170 | ;; We have to pass the environment variable KEYHEADS to display | |
171 | ;; all header fields. Metamail should have an optional argument | |
172 | ;; to pass such information directly. | |
173 | (let ((process-environment | |
55be1314 | 174 | (append process-environment |
5ab5daad RS |
175 | metamail-environment option-environment)) |
176 | (coding-system-for-read 'undecided)) | |
9ed9f36f RS |
177 | (apply (function call-process) |
178 | metamail-program-name | |
179 | nil | |
180 | t ;Output to current buffer | |
181 | (not nodisplay) ;Force redisplay | |
55be1314 RS |
182 | (append metamail-switches |
183 | (list "-m" (or metamail-mailer-name "emacs")) | |
184 | (list metafile)))) | |
9ed9f36f RS |
185 | ;; `metamail' may not delete the temporary file! |
186 | (condition-case error | |
187 | (delete-file metafile) | |
188 | (error nil)) | |
189 | ))) | |
190 | ||
9ed9f36f RS |
191 | (provide 'metamail) |
192 | ||
193 | ;;; metamail.el ends here |