* vc-dir.el (vc-dir-query-replace-regexp): Doc fix (Bug#7501).
[emacs.git] / lisp / mail / metamail.el
blob64c7c57f8db6c416727b8ec5f78ad77759d49f35
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 'hypermedia
44 :group 'processes)
46 (defcustom metamail-program-name "metamail"
47 "*Metamail program name."
48 :type 'string
49 :group 'metamail)
51 (defcustom metamail-mailer-name "emacs"
52 "*Mailer name set to MM_MAILER environment variable."
53 :type 'string
54 :group 'metamail)
56 (defvar metamail-environment '("KEYHEADS=*" "MM_QUIET=1")
57 "*Environment variables passed to `metamail'.
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.")
62 (defcustom metamail-switches '("-x" "-d" "-z")
63 "*Switches for `metamail' program.
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
68 `metamail-mailer-name' variable."
69 :type '(repeat (string :tag "Switch"))
70 :group 'metamail)
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 )))
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)
115 (setq contype
116 (or (mail-fetch-field "Content-Type") "text/plain"))
117 (setq encoding
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 ;; 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))))
141 ;;;###autoload
142 (defun metamail-buffer (&optional viewmode buffer nodisplay)
143 "Process current buffer through `metamail'.
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
147 means current).
148 Optional argument NODISPLAY non-nil means buffer is not
149 redisplayed as output is inserted."
150 (interactive "p")
151 (metamail-region (point-min) (point-max) viewmode buffer nodisplay))
153 ;;;###autoload
154 (defun metamail-region (beg end &optional viewmode buffer nodisplay)
155 "Process current region through 'metamail'.
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
159 means current).
160 Optional argument NODISPLAY non-nil means buffer is not
161 redisplayed as output is inserted."
162 (interactive "r\np")
163 (let ((curbuf (current-buffer))
164 (buffer-read-only nil)
165 (metafile (make-temp-file "metamail"))
166 (option-environment
167 (list (format "EMACS_VIEW_MODE=%d"
168 (if (numberp viewmode) viewmode 1)))))
169 (save-excursion
170 ;; Gee! Metamail does not ouput to stdout if input comes from
171 ;; stdin.
172 (let ((selective-display nil)) ;Disable ^M to nl translation.
173 (write-region beg end metafile nil 'nomessage))
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
185 (append process-environment
186 metamail-environment option-environment))
187 (coding-system-for-read 'undecided))
188 (apply (function call-process)
189 metamail-program-name
191 t ;Output to current buffer
192 (not nodisplay) ;Force redisplay
193 (append metamail-switches
194 (list "-m" (or metamail-mailer-name "emacs"))
195 (list metafile))))
196 ;; `metamail' may not delete the temporary file!
197 (condition-case error
198 (delete-file metafile)
199 (error nil))
202 (provide 'metamail)
204 ;; arch-tag: 52c0cb6f-d800-4776-9789-f0275cb5490e
205 ;;; metamail.el ends here