(match): Use yellow background on light-bg terminals.
[emacs.git] / lisp / mail / metamail.el
blob22149d79026da60e4ee8d1ef410f087efa9da808
1 ;;; metamail.el --- Metamail interface for GNU Emacs
3 ;; Copyright (C) 1993, 1996, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 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 2, or (at your option)
14 ;; 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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; Note: Metamail does not have all the options which are compatible with
29 ;; the environment variables. For that reason, metamail.el has to
30 ;; hack the environment variables. In addition, there is no way to
31 ;; display all header fields without extra informative body messages
32 ;; which are suppressed by the "-q" option.
34 ;; The idea of using metamail to process MIME messages is from
35 ;; gnus-mime.el by Spike <Spike@world.std.com>.
37 ;;; Code:
39 (defvar rmail-current-message)
40 (defvar rmail-message-vector)
42 (defgroup metamail nil
43 "Metamail interface for Emacs."
44 :group 'mail
45 :group 'hypermedia
46 :group 'processes)
48 (defcustom metamail-program-name "metamail"
49 "*Metamail program name."
50 :type 'string
51 :group 'metamail)
53 (defcustom metamail-mailer-name "emacs"
54 "*Mailer name set to MM_MAILER environment variable."
55 :type 'string
56 :group 'metamail)
58 (defvar metamail-environment '("KEYHEADS=*" "MM_QUIET=1")
59 "*Environment variables passed to `metamail'.
60 It must be a list of strings that have the format ENVVARNAME=VALUE.
61 It is not expected to be altered globally by `set' or `setq'.
62 Instead, change its value temporary using `let' or `let*' form.")
64 (defcustom metamail-switches '("-x" "-d" "-z")
65 "*Switches for `metamail' program.
66 `-z' is required to remove zap file.
67 It is not expected to be altered globally by `set' or `setq'.
68 Instead, change its value temporary using `let' or `let*' form.
69 `-m MAILER' argument is automatically generated from the
70 `metamail-mailer-name' variable."
71 :type '(repeat (string :tag "Switch"))
72 :group 'metamail)
74 ;;;###autoload
75 (defun metamail-interpret-header ()
76 "Interpret a header part of a MIME message in current buffer.
77 Its body part is not interpreted at all."
78 (interactive)
79 (save-excursion
80 (let* ((buffer-read-only nil)
81 (metamail-switches ;Inhibit processing an empty body.
82 (append metamail-switches '("-c" "text/plain" "-E" "7bit")))
83 (end (progn
84 (goto-char (point-min))
85 (search-forward "\n\n" nil 'move)
86 ;; An extra newline is inserted by metamail if there
87 ;; is no body part. So, insert a dummy body by
88 ;; itself.
89 (insert "\n")
90 (point))))
91 (metamail-region (point-min) end nil nil 'nodisplay)
92 ;; Remove an extra newline inserted by myself.
93 (goto-char (point-min))
94 (if (search-forward "\n\n\n" nil t)
95 (delete-char -1))
96 )))
98 ;;;###autoload
99 (defun metamail-interpret-body (&optional viewmode nodisplay)
100 "Interpret a body part of a MIME message in current buffer.
101 Optional argument VIEWMODE specifies the value of the
102 EMACS_VIEW_MODE environment variable (defaulted to 1).
103 Optional argument NODISPLAY non-nil means buffer is not
104 redisplayed as output is inserted.
105 Its header part is not interpreted at all."
106 (interactive "p")
107 (save-excursion
108 (let ((contype nil)
109 (encoding nil)
110 (end (progn
111 (goto-char (point-min))
112 (search-forward "\n\n" nil t)
113 (point))))
114 ;; Find Content-Type and Content-Transfer-Encoding from the header.
115 (save-restriction
116 (narrow-to-region (point-min) end)
117 (setq contype
118 (or (mail-fetch-field "Content-Type") "text/plain"))
119 (setq encoding
120 (or (mail-fetch-field "Content-Transfer-Encoding") "7bit")))
121 ;; Interpret the body part only.
122 (let ((metamail-switches ;Process body part only.
123 (append metamail-switches
124 (list "-b" "-c" contype "-E" encoding))))
125 (metamail-region end (point-max) viewmode nil nodisplay))
126 ;; Mode specific hack.
127 (cond ((eq major-mode 'rmail-mode)
128 ;; Adjust the marker of this message if in Rmail mode buffer.
129 (set-marker (aref rmail-message-vector (1+ rmail-current-message))
130 (point-max))))
133 ;;;###autoload
134 (defun metamail-buffer (&optional viewmode buffer nodisplay)
135 "Process current buffer through `metamail'.
136 Optional argument VIEWMODE specifies the value of the
137 EMACS_VIEW_MODE environment variable (defaulted to 1).
138 Optional argument BUFFER specifies a buffer to be filled (nil
139 means current).
140 Optional argument NODISPLAY non-nil means buffer is not
141 redisplayed as output is inserted."
142 (interactive "p")
143 (metamail-region (point-min) (point-max) viewmode buffer nodisplay))
145 ;;;###autoload
146 (defun metamail-region (beg end &optional viewmode buffer nodisplay)
147 "Process current region through 'metamail'.
148 Optional argument VIEWMODE specifies the value of the
149 EMACS_VIEW_MODE environment variable (defaulted to 1).
150 Optional argument BUFFER specifies a buffer to be filled (nil
151 means current).
152 Optional argument NODISPLAY non-nil means buffer is not
153 redisplayed as output is inserted."
154 (interactive "r\np")
155 (let ((curbuf (current-buffer))
156 (buffer-read-only nil)
157 (metafile (make-temp-file "metamail"))
158 (option-environment
159 (list (format "EMACS_VIEW_MODE=%d"
160 (if (numberp viewmode) viewmode 1)))))
161 (save-excursion
162 ;; Gee! Metamail does not ouput to stdout if input comes from
163 ;; stdin.
164 (let ((selective-display nil)) ;Disable ^M to nl translation.
165 (write-region beg end metafile nil 'nomessage))
166 (if buffer
167 (set-buffer buffer))
168 (setq buffer-read-only nil)
169 ;; Clear destination buffer.
170 (if (eq curbuf (current-buffer))
171 (delete-region beg end)
172 (delete-region (point-min) (point-max)))
173 ;; We have to pass the environment variable KEYHEADS to display
174 ;; all header fields. Metamail should have an optional argument
175 ;; to pass such information directly.
176 (let ((process-environment
177 (append process-environment
178 metamail-environment option-environment))
179 (coding-system-for-read 'undecided))
180 (apply (function call-process)
181 metamail-program-name
183 t ;Output to current buffer
184 (not nodisplay) ;Force redisplay
185 (append metamail-switches
186 (list "-m" (or metamail-mailer-name "emacs"))
187 (list metafile))))
188 ;; `metamail' may not delete the temporary file!
189 (condition-case error
190 (delete-file metafile)
191 (error nil))
194 (provide 'metamail)
196 ;;; arch-tag: 52c0cb6f-d800-4776-9789-f0275cb5490e
197 ;;; metamail.el ends here