1 ;;; mm-view.el --- functions for viewing MIME objects
2 ;; Copyright (C) 1998, 1999, 2000, 01, 2004 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 (unless (fboundp 'diff-mode)
38 (autoload 'diff-mode "diff-mode" "" t nil)))
41 ;;; Functions for displaying various formats inline
43 (defun mm-inline-image-emacs (handle)
44 (let ((b (point-marker))
47 (put-image (mm-get-image handle) b)
48 (mm-handle-set-undisplayer
50 `(lambda () (remove-images ,b (1+ ,b))))))
52 (defun mm-inline-image-xemacs (handle)
56 (annot (make-annotation (mm-get-image handle) nil 'text))
58 (mm-handle-set-undisplayer
61 (let (buffer-read-only)
62 (delete-annotation ,annot)
63 (delete-region ,(set-marker (make-marker) b)
64 ,(set-marker (make-marker) (point))))))
65 (set-extent-property annot 'mm t)
66 (set-extent-property annot 'duplicable t)))
69 (if (featurep 'xemacs)
70 (defalias 'mm-inline-image 'mm-inline-image-xemacs)
71 (defalias 'mm-inline-image 'mm-inline-image-emacs)))
73 (defvar mm-w3-setup nil)
81 (setq mm-w3-setup t)))
83 (defun mm-inline-text (handle)
84 (let ((type (mm-handle-media-subtype handle))
85 text buffer-read-only)
89 (setq text (mm-get-part handle))
91 (url-standalone-mode t)
92 (url-gateway-unplugged t)
94 (url-generic-parse-url (format "cid:%s" (mm-handle-id handle))))
95 (width (window-width))
96 (charset (mail-content-type-get
97 (mm-handle-type handle) 'charset)))
101 (narrow-to-region b (point))
102 (goto-char (point-min))
103 (if (or (and (boundp 'w3-meta-content-type-charset-regexp)
105 w3-meta-content-type-charset-regexp nil t))
106 (and (boundp 'w3-meta-charset-content-type-regexp)
108 w3-meta-charset-content-type-regexp nil t)))
110 (or (let ((bsubstr (buffer-substring-no-properties
113 (if (fboundp 'w3-coding-system-for-mime-charset)
114 (w3-coding-system-for-mime-charset bsubstr)
115 (mm-charset-to-coding-system bsubstr)))
117 (delete-region (point-min) (point-max))
118 (insert (mm-decode-string text charset))
119 (save-window-excursion
121 (let ((w3-strict-width width)
122 ;; Don't let w3 set the global version of
124 (fill-column fill-column))
126 (w3-region (point-min) (point-max))
128 (delete-region (point-min) (point-max))
130 (charset (mail-content-type-get
131 (mm-handle-type handle) 'charset)))
132 (if (or (eq charset 'gnus-decoded)
133 (eq mail-parse-charset 'gnus-decoded))
135 (narrow-to-region (point) (point))
136 (mm-insert-part handle)
137 (goto-char (point-max)))
138 (insert (mm-decode-string (mm-get-part handle)
141 "Error while rendering html; showing as text/plain"))))))
142 (mm-handle-set-undisplayer
145 (let (buffer-read-only)
146 (if (functionp 'remove-specifier)
147 (mapcar (lambda (prop)
149 (face-property 'default prop)
151 '(background background-pixmap foreground)))
152 (delete-region ,(point-min-marker)
153 ,(point-max-marker)))))))))
154 ((equal type "x-vcard")
159 (if (fboundp 'vcard-pretty-print)
160 (vcard-pretty-print (mm-get-part handle))
162 (vcard-parse-string (mm-get-part handle)
163 'vcard-standard-filter)))))))
166 (charset (mail-content-type-get
167 (mm-handle-type handle) 'charset)))
168 (if (or (eq charset 'gnus-decoded)
169 ;; This is probably not entirely correct, but
170 ;; makes rfc822 parts with embedded multiparts work.
171 (eq mail-parse-charset 'gnus-decoded))
173 (narrow-to-region (point) (point))
174 (mm-insert-part handle)
175 (goto-char (point-max)))
176 (insert (mm-decode-string (mm-get-part handle) charset)))
177 (when (and (equal type "plain")
178 (equal (cdr (assoc 'format (mm-handle-type handle)))
181 (narrow-to-region b (point))
184 (goto-char (point-max))))
186 (narrow-to-region b (point))
187 (set-text-properties (point-min) (point-max) nil)
188 (when (or (equal type "enriched")
189 (equal type "richtext"))
190 (enriched-decode (point-min) (point-max)))
191 (mm-handle-set-undisplayer
194 (let (buffer-read-only)
195 (delete-region ,(point-min-marker)
196 ,(point-max-marker)))))))))))
198 (defun mm-insert-inline (handle text)
199 "Insert TEXT inline from HANDLE."
201 (inhibit-read-only t))
203 (mm-handle-set-undisplayer
206 (let (buffer-read-only)
207 (delete-region ,(set-marker (make-marker) b)
208 ,(set-marker (make-marker) (point))))))))
210 (defun mm-inline-audio (handle)
211 (message "Not implemented"))
213 (defun mm-view-sound-file ()
214 (message "Not implemented"))
216 (defun mm-w3-prepare-buffer ()
218 (let ((url-standalone-mode t)
219 (url-gateway-unplugged t))
220 (w3-prepare-buffer)))
222 (defun mm-view-message ()
223 (mm-enable-multibyte)
225 (let (gnus-article-mime-handles)
226 ;; Double decode problem may happen. See mm-inline-message.
227 (run-hooks 'gnus-article-decode-hook)
228 (gnus-article-prepare-display)
229 (setq handles gnus-article-mime-handles))
231 (setq gnus-article-mime-handles
232 (nconc gnus-article-mime-handles
233 (if (listp (car handles))
234 handles (list handles))))))
236 (goto-char (point-min)))
238 (defun mm-inline-message (handle)
241 (charset (mail-content-type-get
242 (mm-handle-type handle) 'charset))
243 gnus-displaying-mime handles)
246 (setq charset (intern (downcase charset)))
247 (when (eq charset 'us-ascii)
251 (narrow-to-region b b)
252 (mm-insert-part handle)
253 (let (gnus-article-mime-handles
254 ;; disable prepare hook
255 gnus-article-prepare-hook
256 (gnus-newsgroup-charset
257 (or charset gnus-newsgroup-charset)))
258 (run-hooks 'gnus-article-decode-hook)
259 (gnus-article-prepare-display)
260 (setq handles gnus-article-mime-handles))
261 (goto-char (point-min))
264 (goto-char (point-max))
267 (insert "----------\n\n")
269 (setq gnus-article-mime-handles
270 (nconc gnus-article-mime-handles
271 (if (listp (car handles))
272 handles (list handles)))))
273 (mm-handle-set-undisplayer
276 (let (buffer-read-only)
277 (if (fboundp 'remove-specifier)
278 ;; This is only valid on XEmacs.
279 (mapcar (lambda (prop)
281 (face-property 'default prop) (current-buffer)))
282 '(background background-pixmap foreground)))
283 (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
285 (defun mm-display-inline-fontify (handle mode)
288 (mm-insert-part handle)
290 (font-lock-fontify-buffer)
291 (when (fboundp 'extent-list)
292 (map-extents (lambda (ext ignored)
293 (set-extent-property ext 'duplicable t)
295 nil nil nil nil nil 'text-prop))
296 (setq text (buffer-string)))
297 (mm-insert-inline handle text)))
299 (defun mm-display-patch-inline (handle)
300 (mm-display-inline-fontify handle 'diff-mode))
302 (defun mm-display-elisp-inline (handle)
303 (mm-display-inline-fontify handle 'emacs-lisp-mode))
307 ;;; arch-tag: b60e749a-d05c-47f2-bccd-bdaa59327cb2
308 ;;; mm-view.el ends here