1 ;;; mm-view.el --- Functions for viewing MIME objects
2 ;; Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
26 (eval-when-compile (require 'cl))
33 (autoload 'gnus-article-prepare-display "gnus-art")
34 (autoload 'vcard-parse-string "vcard")
35 (autoload 'vcard-format-string "vcard")
36 (autoload 'fill-flowed "flow-fill")
37 (autoload 'diff-mode "diff-mode"))
40 ;;; Functions for displaying various formats inline
42 (defun mm-inline-image-emacs (handle)
43 (let ((b (point-marker))
46 (put-image (mm-get-image handle) b)
47 (mm-handle-set-undisplayer
49 `(lambda () (remove-images ,b (1+ ,b))))))
51 (defun mm-inline-image-xemacs (handle)
55 (annot (make-annotation (mm-get-image handle) nil 'text))
57 (mm-handle-set-undisplayer
60 (let (buffer-read-only)
61 (delete-annotation ,annot)
62 (delete-region ,(set-marker (make-marker) b)
63 ,(set-marker (make-marker) (point))))))
64 (set-extent-property annot 'mm t)
65 (set-extent-property annot 'duplicable t)))
68 (if (featurep 'xemacs)
69 (defalias 'mm-inline-image 'mm-inline-image-xemacs)
70 (defalias 'mm-inline-image 'mm-inline-image-emacs)))
72 (defvar mm-w3-setup nil)
80 (setq mm-w3-setup t)))
82 (defun mm-inline-text (handle)
83 (let ((type (mm-handle-media-subtype handle))
84 text buffer-read-only)
88 (setq text (mm-get-part handle))
90 (url-standalone-mode t)
92 (url-generic-parse-url (format "cid:%s" (mm-handle-id handle))))
93 (width (window-width))
94 (charset (mail-content-type-get
95 (mm-handle-type handle) 'charset)))
99 (narrow-to-region b (point))
100 (goto-char (point-min))
101 (if (or (and (boundp 'w3-meta-content-type-charset-regexp)
103 w3-meta-content-type-charset-regexp nil t))
104 (and (boundp 'w3-meta-charset-content-type-regexp)
106 w3-meta-charset-content-type-regexp nil t)))
108 (or (let ((bsubstr (buffer-substring-no-properties
111 (if (fboundp 'w3-coding-system-for-mime-charset)
112 (w3-coding-system-for-mime-charset bsubstr)
113 (mm-charset-to-coding-system bsubstr)))
115 (delete-region (point-min) (point-max))
116 (insert (mm-decode-string text charset))
117 (save-window-excursion
119 (let ((w3-strict-width width)
120 ;; Don't let w3 set the global version of
122 (fill-column fill-column)
123 (url-standalone-mode t))
125 (w3-region (point-min) (point-max))
128 "Error while rendering html; showing as text/plain"))))))
129 (mm-handle-set-undisplayer
132 (let (buffer-read-only)
133 (if (functionp 'remove-specifier)
134 (mapcar (lambda (prop)
136 (face-property 'default prop)
138 '(background background-pixmap foreground)))
139 (delete-region ,(point-min-marker)
140 ,(point-max-marker)))))))))
141 ((or (equal type "enriched")
142 (equal type "richtext"))
144 (mm-with-unibyte-buffer
145 (mm-insert-part handle)
146 (save-window-excursion
147 (enriched-decode (point-min) (point-max))
148 (setq text (buffer-string)))))
149 (mm-insert-inline handle text))
150 ((equal type "x-vcard")
154 (if (fboundp 'vcard-pretty-print)
155 (vcard-pretty-print (mm-get-part handle))
157 (vcard-parse-string (mm-get-part handle)
158 'vcard-standard-filter))))))
161 (charset (mail-content-type-get
162 (mm-handle-type handle) 'charset)))
163 (if (or (eq charset 'gnus-decoded)
164 ;; This is probably not entirely correct, but
165 ;; makes rfc822 parts with embedded multiparts work.
166 (eq mail-parse-charset 'gnus-decoded))
168 (narrow-to-region (point) (point))
169 (mm-insert-part handle)
170 (goto-char (point-max)))
171 (insert (mm-decode-string (mm-get-part handle) charset)))
172 (when (and (equal type "plain")
173 (equal (cdr (assoc 'format (mm-handle-type handle)))
176 (narrow-to-region b (point))
179 (goto-char (point-max))))
181 (narrow-to-region b (point))
182 (set-text-properties (point-min) (point-max) nil)
183 (mm-handle-set-undisplayer
186 (let (buffer-read-only)
187 (delete-region ,(point-min-marker)
188 ,(point-max-marker)))))))))))
190 (defun mm-insert-inline (handle text)
191 "Insert TEXT inline from HANDLE."
194 (mm-handle-set-undisplayer
197 (let (buffer-read-only)
198 (delete-region ,(set-marker (make-marker) b)
199 ,(set-marker (make-marker) (point))))))))
201 (defun mm-inline-audio (handle)
202 (message "Not implemented"))
204 (defun mm-view-sound-file ()
205 (message "Not implemented"))
207 (defun mm-w3-prepare-buffer ()
209 (let ((url-standalone-mode t))
210 (w3-prepare-buffer)))
212 (defun mm-view-message ()
213 (mm-enable-multibyte)
215 (let (gnus-article-mime-handles)
216 ;; Double decode problem may happen. See mm-inline-message.
217 (run-hooks 'gnus-article-decode-hook)
218 (gnus-article-prepare-display)
219 (setq handles gnus-article-mime-handles))
221 (setq gnus-article-mime-handles
222 (mm-merge-handles gnus-article-mime-handles handles))))
224 (goto-char (point-min)))
226 (defun mm-inline-message (handle)
229 (charset (mail-content-type-get
230 (mm-handle-type handle) 'charset))
231 gnus-displaying-mime handles)
234 (setq charset (intern (downcase charset)))
235 (when (eq charset 'us-ascii)
239 (narrow-to-region b b)
240 (mm-insert-part handle)
241 (let (gnus-article-mime-handles
242 ;; disable prepare hook
243 gnus-article-prepare-hook
244 (gnus-newsgroup-charset
245 (or charset gnus-newsgroup-charset)))
246 (run-hooks 'gnus-article-decode-hook)
247 (gnus-article-prepare-display)
248 (setq handles gnus-article-mime-handles))
249 (goto-char (point-min))
252 (goto-char (point-max))
255 (insert "----------\n\n")
257 (setq gnus-article-mime-handles
258 (mm-merge-handles gnus-article-mime-handles handles)))
259 (mm-handle-set-undisplayer
262 (let (buffer-read-only)
263 (if (fboundp 'remove-specifier)
264 ;; This is only valid on XEmacs.
265 (mapcar (lambda (prop)
267 (face-property 'default prop) (current-buffer)))
268 '(background background-pixmap foreground)))
269 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
271 (defun mm-display-inline-fontify (handle mode)
273 ;; XEmacs @#$@ version of font-lock refuses to fully turn itself
274 ;; on for buffers whose name begins with " ". That's why we use
275 ;; save-current-buffer/get-buffer-create rather than
278 (set-buffer (generate-new-buffer "*fontification*"))
281 (buffer-disable-undo)
282 (mm-insert-part handle)
284 (let ((font-lock-verbose nil))
285 ;; I find font-lock a bit too verbose.
286 (font-lock-fontify-buffer))
287 ;; By default, XEmacs font-lock uses non-duplicable text
288 ;; properties. This code forces all the text properties
289 ;; to be copied along with the text.
290 (when (fboundp 'extent-list)
291 (map-extents (lambda (ext ignored)
292 (set-extent-property ext 'duplicable t)
294 nil nil nil nil nil 'text-prop))
295 (setq text (buffer-string)))
296 (kill-buffer (current-buffer))))
297 (mm-insert-inline handle text)))
299 ;; Shouldn't these functions check whether the user even wants to use
300 ;; font-lock? At least under XEmacs, this fontification is pretty
301 ;; much unconditional. Also, it would be nice to change for the size
302 ;; of the fontified region.
304 (defun mm-display-patch-inline (handle)
305 (mm-display-inline-fontify handle 'diff-mode))
307 (defun mm-display-elisp-inline (handle)
308 (mm-display-inline-fontify handle 'emacs-lisp-mode))
312 ;; mm-view.el ends here