(set-version): Add doc/lispref/vol1.texi,vol2.texi.
[emacs.git] / lisp / mail / metamail.el
blobfb293267646a498745f7b69f28a825a9543bb873
1 ;;; metamail.el --- Metamail interface for GNU Emacs
3 ;; Copyright (C) 1993, 1996, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 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 ;; Mode specific hack.
125 (cond ((eq major-mode 'rmail-mode)
126 ;; Adjust the marker of this message if in Rmail mode buffer.
127 (set-marker (aref rmail-message-vector (1+ rmail-current-message))
128 (point-max))))
131 ;;;###autoload
132 (defun metamail-buffer (&optional viewmode buffer nodisplay)
133 "Process current buffer through `metamail'.
134 Optional argument VIEWMODE specifies the value of the
135 EMACS_VIEW_MODE environment variable (defaulted to 1).
136 Optional argument BUFFER specifies a buffer to be filled (nil
137 means current).
138 Optional argument NODISPLAY non-nil means buffer is not
139 redisplayed as output is inserted."
140 (interactive "p")
141 (metamail-region (point-min) (point-max) viewmode buffer nodisplay))
143 ;;;###autoload
144 (defun metamail-region (beg end &optional viewmode buffer nodisplay)
145 "Process current region through 'metamail'.
146 Optional argument VIEWMODE specifies the value of the
147 EMACS_VIEW_MODE environment variable (defaulted to 1).
148 Optional argument BUFFER specifies a buffer to be filled (nil
149 means current).
150 Optional argument NODISPLAY non-nil means buffer is not
151 redisplayed as output is inserted."
152 (interactive "r\np")
153 (let ((curbuf (current-buffer))
154 (buffer-read-only nil)
155 (metafile (make-temp-file "metamail"))
156 (option-environment
157 (list (format "EMACS_VIEW_MODE=%d"
158 (if (numberp viewmode) viewmode 1)))))
159 (save-excursion
160 ;; Gee! Metamail does not ouput to stdout if input comes from
161 ;; stdin.
162 (let ((selective-display nil)) ;Disable ^M to nl translation.
163 (write-region beg end metafile nil 'nomessage))
164 (if buffer
165 (set-buffer buffer))
166 (setq buffer-read-only nil)
167 ;; Clear destination buffer.
168 (if (eq curbuf (current-buffer))
169 (delete-region beg end)
170 (delete-region (point-min) (point-max)))
171 ;; We have to pass the environment variable KEYHEADS to display
172 ;; all header fields. Metamail should have an optional argument
173 ;; to pass such information directly.
174 (let ((process-environment
175 (append process-environment
176 metamail-environment option-environment))
177 (coding-system-for-read 'undecided))
178 (apply (function call-process)
179 metamail-program-name
181 t ;Output to current buffer
182 (not nodisplay) ;Force redisplay
183 (append metamail-switches
184 (list "-m" (or metamail-mailer-name "emacs"))
185 (list metafile))))
186 ;; `metamail' may not delete the temporary file!
187 (condition-case error
188 (delete-file metafile)
189 (error nil))
192 (provide 'metamail)
194 ;; arch-tag: 52c0cb6f-d800-4776-9789-f0275cb5490e
195 ;;; metamail.el ends here