Cleanup syssignal.h.
[emacs.git] / lisp / mail / metamail.el
blob47326b636a12490caee89390cf12b56e75992c73
1 ;;; metamail.el --- Metamail interface for GNU Emacs
3 ;; Copyright (C) 1993, 1996, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Masanobu UMEDA <umerin@mse.kyutech.ac.jp>
7 ;; Keywords: mail, news, mime, multimedia
9 ;; This file is part of GNU Emacs.
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 3 of the License, or
14 ;; (at your option) any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Note: Metamail does not have all the options which are compatible with
27 ;; the environment variables. For that reason, metamail.el has to
28 ;; hack the environment variables. In addition, there is no way to
29 ;; display all header fields without extra informative body messages
30 ;; which are suppressed by the "-q" option.
32 ;; The idea of using metamail to process MIME messages is from
33 ;; gnus-mime.el by Spike <Spike@world.std.com>.
35 ;;; Code:
37 (defvar rmail-current-message)
38 (defvar rmail-message-vector)
40 (defgroup metamail nil
41 "Metamail interface for Emacs."
42 :group 'mail
43 :group 'processes)
45 (defcustom metamail-program-name "metamail"
46 "*Metamail program name."
47 :type 'string
48 :group 'metamail)
50 (defcustom metamail-mailer-name "emacs"
51 "*Mailer name set to MM_MAILER environment variable."
52 :type 'string
53 :group 'metamail)
55 (defvar metamail-environment '("KEYHEADS=*" "MM_QUIET=1")
56 "*Environment variables passed to `metamail'.
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.")
61 (defcustom metamail-switches '("-x" "-d" "-z")
62 "*Switches for `metamail' program.
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
67 `metamail-mailer-name' variable."
68 :type '(repeat (string :tag "Switch"))
69 :group 'metamail)
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 )))
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)
114 (setq contype
115 (or (mail-fetch-field "Content-Type") "text/plain"))
116 (setq encoding
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 ;; This mode specific hack is no longer appropriate in mbox Rmail.
124 ;; Pre-mbox, we have just modified the actual folder, so we
125 ;; update the message-vector with the new end position of the
126 ;; current message. In mbox Rmail, all we have done is modify a
127 ;; display copy of the message. Note also that point-max is a
128 ;; marker in the wrong buffer: the message-vector contains
129 ;; markers in rmail-view-buffer (which is not in rmail-mode).
130 ;; So this hack actually breaks the message-vector.
131 ;; If you're calling this on the actual rmail-view-buffer (or a
132 ;; non-swapped rmail-buffer), you would still need this hack.
133 ;; But you're not going to do that.
134 ;;; (cond ((eq major-mode 'rmail-mode)
135 ;;; ;; Adjust the marker of this message if in Rmail mode buffer.
136 ;;; (set-marker (aref rmail-message-vector (1+ rmail-current-message))
137 ;;; (point-max))))
140 ;;;###autoload
141 (defun metamail-buffer (&optional viewmode buffer nodisplay)
142 "Process current buffer through `metamail'.
143 Optional argument VIEWMODE specifies the value of the
144 EMACS_VIEW_MODE environment variable (defaulted to 1).
145 Optional argument BUFFER specifies a buffer to be filled (nil
146 means current).
147 Optional argument NODISPLAY non-nil means buffer is not
148 redisplayed as output is inserted."
149 (interactive "p")
150 (metamail-region (point-min) (point-max) viewmode buffer nodisplay))
152 ;;;###autoload
153 (defun metamail-region (beg end &optional viewmode buffer nodisplay)
154 "Process current region through 'metamail'.
155 Optional argument VIEWMODE specifies the value of the
156 EMACS_VIEW_MODE environment variable (defaulted to 1).
157 Optional argument BUFFER specifies a buffer to be filled (nil
158 means current).
159 Optional argument NODISPLAY non-nil means buffer is not
160 redisplayed as output is inserted."
161 (interactive "r\np")
162 (let ((curbuf (current-buffer))
163 (buffer-read-only nil)
164 (metafile (make-temp-file "metamail"))
165 (option-environment
166 (list (format "EMACS_VIEW_MODE=%d"
167 (if (numberp viewmode) viewmode 1)))))
168 (save-excursion
169 ;; Gee! Metamail does not ouput to stdout if input comes from
170 ;; stdin.
171 (let ((selective-display nil)) ;Disable ^M to nl translation.
172 (write-region beg end metafile nil 'nomessage))
173 (if buffer
174 (set-buffer buffer))
175 (setq buffer-read-only nil)
176 ;; Clear destination buffer.
177 (if (eq curbuf (current-buffer))
178 (delete-region beg end)
179 (delete-region (point-min) (point-max)))
180 ;; We have to pass the environment variable KEYHEADS to display
181 ;; all header fields. Metamail should have an optional argument
182 ;; to pass such information directly.
183 (let ((process-environment
184 (append process-environment
185 metamail-environment option-environment))
186 (coding-system-for-read 'undecided))
187 (apply (function call-process)
188 metamail-program-name
190 t ;Output to current buffer
191 (not nodisplay) ;Force redisplay
192 (append metamail-switches
193 (list "-m" (or metamail-mailer-name "emacs"))
194 (list metafile))))
195 ;; `metamail' may not delete the temporary file!
196 (condition-case error
197 (delete-file metafile)
198 (error nil))
201 (provide 'metamail)
203 ;; arch-tag: 52c0cb6f-d800-4776-9789-f0275cb5490e
204 ;;; metamail.el ends here