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