Commit | Line | Data |
---|---|---|
9ed9f36f RS |
1 | ;;; metamail.el --- Metamail interface for GNU Emacs |
2 | ||
f2e3589a | 3 | ;; Copyright (C) 1993, 1996, 2001, 2002, 2003, 2004, |
5df4f04c | 4 | ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011 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 |
58 | It must be a list of strings that have the format ENVVARNAME=VALUE. |
59 | It is not expected to be altered globally by `set' or `setq'. | |
60 | Instead, 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. |
65 | It is not expected to be altered globally by `set' or `setq'. | |
66 | Instead, 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. | |
75 | Its 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. | |
99 | Optional argument VIEWMODE specifies the value of the | |
100 | EMACS_VIEW_MODE environment variable (defaulted to 1). | |
101 | Optional argument NODISPLAY non-nil means buffer is not | |
102 | redisplayed as output is inserted. | |
103 | Its 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)) | |
cfb4688e GM |
124 | ;; This mode specific hack is no longer appropriate in mbox Rmail. |
125 | ;; Pre-mbox, we have just modified the actual folder, so we | |
126 | ;; update the message-vector with the new end position of the | |
127 | ;; current message. In mbox Rmail, all we have done is modify a | |
128 | ;; display copy of the message. Note also that point-max is a | |
129 | ;; marker in the wrong buffer: the message-vector contains | |
130 | ;; markers in rmail-view-buffer (which is not in rmail-mode). | |
131 | ;; So this hack actually breaks the message-vector. | |
132 | ;; If you're calling this on the actual rmail-view-buffer (or a | |
133 | ;; non-swapped rmail-buffer), you would still need this hack. | |
134 | ;; But you're not going to do that. | |
135 | ;;; (cond ((eq major-mode 'rmail-mode) | |
136 | ;;; ;; Adjust the marker of this message if in Rmail mode buffer. | |
137 | ;;; (set-marker (aref rmail-message-vector (1+ rmail-current-message)) | |
138 | ;;; (point-max)))) | |
55be1314 RS |
139 | ))) |
140 | ||
141 | ;;;###autoload | |
142 | (defun metamail-buffer (&optional viewmode buffer nodisplay) | |
9ed9f36f | 143 | "Process current buffer through `metamail'. |
55be1314 RS |
144 | Optional argument VIEWMODE specifies the value of the |
145 | EMACS_VIEW_MODE environment variable (defaulted to 1). | |
146 | Optional argument BUFFER specifies a buffer to be filled (nil | |
9ed9f36f | 147 | means current). |
55be1314 | 148 | Optional argument NODISPLAY non-nil means buffer is not |
9ed9f36f | 149 | redisplayed as output is inserted." |
55be1314 RS |
150 | (interactive "p") |
151 | (metamail-region (point-min) (point-max) viewmode buffer nodisplay)) | |
9ed9f36f | 152 | |
55be1314 RS |
153 | ;;;###autoload |
154 | (defun metamail-region (beg end &optional viewmode buffer nodisplay) | |
9ed9f36f | 155 | "Process current region through 'metamail'. |
55be1314 RS |
156 | Optional argument VIEWMODE specifies the value of the |
157 | EMACS_VIEW_MODE environment variable (defaulted to 1). | |
158 | Optional argument BUFFER specifies a buffer to be filled (nil | |
9ed9f36f | 159 | means current). |
55be1314 | 160 | Optional argument NODISPLAY non-nil means buffer is not |
9ed9f36f | 161 | redisplayed as output is inserted." |
55be1314 | 162 | (interactive "r\np") |
9ed9f36f RS |
163 | (let ((curbuf (current-buffer)) |
164 | (buffer-read-only nil) | |
767d12f2 | 165 | (metafile (make-temp-file "metamail")) |
55be1314 | 166 | (option-environment |
a1506d29 | 167 | (list (format "EMACS_VIEW_MODE=%d" |
55be1314 | 168 | (if (numberp viewmode) viewmode 1))))) |
9ed9f36f | 169 | (save-excursion |
55be1314 | 170 | ;; Gee! Metamail does not ouput to stdout if input comes from |
9ed9f36f | 171 | ;; stdin. |
5ab5daad | 172 | (let ((selective-display nil)) ;Disable ^M to nl translation. |
55be1314 | 173 | (write-region beg end metafile nil 'nomessage)) |
9ed9f36f RS |
174 | (if buffer |
175 | (set-buffer buffer)) | |
176 | (setq buffer-read-only nil) | |
177 | ;; Clear destination buffer. | |
178 | (if (eq curbuf (current-buffer)) | |
179 | (delete-region beg end) | |
180 | (delete-region (point-min) (point-max))) | |
181 | ;; We have to pass the environment variable KEYHEADS to display | |
182 | ;; all header fields. Metamail should have an optional argument | |
183 | ;; to pass such information directly. | |
184 | (let ((process-environment | |
55be1314 | 185 | (append process-environment |
5ab5daad RS |
186 | metamail-environment option-environment)) |
187 | (coding-system-for-read 'undecided)) | |
9ed9f36f RS |
188 | (apply (function call-process) |
189 | metamail-program-name | |
190 | nil | |
191 | t ;Output to current buffer | |
192 | (not nodisplay) ;Force redisplay | |
55be1314 RS |
193 | (append metamail-switches |
194 | (list "-m" (or metamail-mailer-name "emacs")) | |
195 | (list metafile)))) | |
9ed9f36f RS |
196 | ;; `metamail' may not delete the temporary file! |
197 | (condition-case error | |
198 | (delete-file metafile) | |
199 | (error nil)) | |
200 | ))) | |
201 | ||
9ed9f36f RS |
202 | (provide 'metamail) |
203 | ||
cbee283d | 204 | ;; arch-tag: 52c0cb6f-d800-4776-9789-f0275cb5490e |
9ed9f36f | 205 | ;;; metamail.el ends here |