a9051cb483547c806dbb8b3f581a6164430c3fab
[gnus] / lisp / mm-view.el
1 ;;; mm-view.el --- Functions for viewing MIME objects
2 ;; Copyright (C) 1998-2000 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; This file is part of GNU Emacs.
6
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)
10 ;; any later version.
11
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.
16
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.
21
22 ;;; Commentary:
23
24 ;;; Code:
25
26 (eval-when-compile (require 'cl))
27 (require 'mail-parse)
28 (require 'mailcap)
29 (require 'mm-bodies)
30 (require 'mm-decode)
31
32 (eval-and-compile
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"))
38
39 ;;;
40 ;;; Functions for displaying various formats inline
41 ;;;
42 (defun mm-inline-image-emacs (handle)
43   (let ((b (point))
44         (overlay nil)
45         (string (copy-sequence "[MM-INLINED-IMAGE]"))
46         buffer-read-only)
47     (insert "\n")
48     (buffer-name)
49     (setq overlay (make-overlay (point) (point) (current-buffer)))
50     (put-text-property 0 (length string) 'display (mm-get-image handle) string)
51     (overlay-put overlay 'before-string string)
52
53     (mm-handle-set-undisplayer
54      handle
55      `(lambda ()
56         (let (buffer-read-only)
57           (delete-overlay ,overlay)
58           (delete-region ,(set-marker (make-marker) b)
59                          ,(set-marker (make-marker) (point))))))))
60
61 (defun mm-inline-image-xemacs (handle)
62   (let ((b (point))
63         (annot (make-annotation (mm-get-image handle) nil 'text))
64         buffer-read-only)
65     (insert "\n")
66     (mm-handle-set-undisplayer
67      handle
68      `(lambda ()
69         (let (buffer-read-only)
70           (delete-annotation ,annot)
71           (delete-region ,(set-marker (make-marker) b)
72                          ,(set-marker (make-marker) (point))))))
73     (set-extent-property annot 'mm t)
74     (set-extent-property annot 'duplicable t)))
75
76 (defun mm-inline-image (handle)
77   (if mm-xemacs-p
78       (mm-inline-image-xemacs handle)
79     (mm-inline-image-emacs handle)))
80
81 (defvar mm-w3-setup nil)
82 (defun mm-setup-w3 ()
83   (unless mm-w3-setup
84     (require 'w3)
85     (w3-do-setup)
86     (require 'url)
87     (require 'w3-vars)
88     (require 'url-vars)
89     (setq mm-w3-setup t)))
90
91 (defun mm-inline-text (handle)
92   (let ((type (mm-handle-media-subtype handle))
93         text buffer-read-only)
94     (cond
95      ((equal type "html")
96       (mm-setup-w3)
97       (setq text (mm-get-part handle))
98       (let ((b (point))
99             (url-standalone-mode t)
100             (url-current-object
101              (url-generic-parse-url (format "cid:%s" (mm-handle-id handle))))
102             (width (window-width))
103             (charset (mail-content-type-get
104                       (mm-handle-type handle) 'charset)))
105         (save-excursion
106           (insert text)
107           (save-restriction
108             (narrow-to-region b (point))
109             (goto-char (point-min))
110             (if (or (and (boundp 'w3-meta-content-type-charset-regexp)
111                          (re-search-forward
112                           w3-meta-content-type-charset-regexp nil t))
113                     (and (boundp 'w3-meta-charset-content-type-regexp)
114                          (re-search-forward
115                           w3-meta-charset-content-type-regexp nil t)))
116                 (setq charset (w3-coding-system-for-mime-charset 
117                                (buffer-substring-no-properties 
118                                 (match-beginning 2) 
119                                 (match-end 2)))))
120             (delete-region (point-min) (point-max))
121             (insert (mm-decode-string text charset))
122             (save-window-excursion
123               (save-restriction
124                 (let ((w3-strict-width width)
125                       (url-standalone-mode t))
126                   (condition-case var
127                       (w3-region (point-min) (point-max))
128                     (error)))))
129             (mm-handle-set-undisplayer
130              handle
131              `(lambda ()
132                 (let (buffer-read-only)
133                   (if (functionp 'remove-specifier)
134                       (mapcar (lambda (prop)
135                                 (remove-specifier
136                                  (face-property 'default prop)
137                                  (current-buffer)))
138                               '(background background-pixmap foreground)))
139                   (delete-region ,(point-min-marker)
140                                  ,(point-max-marker)))))))))
141      ((or (equal type "enriched")
142           (equal type "richtext"))
143       (save-excursion
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")
151       (mm-insert-inline
152        handle
153        (concat "\n-- \n"
154                (if (fboundp 'vcard-pretty-print)
155                    (vcard-pretty-print (mm-get-part handle))
156                  (vcard-format-string
157                   (vcard-parse-string (mm-get-part handle)
158                                       'vcard-standard-filter))))))
159      (t
160       (setq text (mm-get-part handle))
161       (let ((b (point))
162             (charset (mail-content-type-get
163                       (mm-handle-type handle) 'charset)))
164         (insert (mm-decode-string text charset))
165         (when (and (equal type "plain")
166                    (equal (cdr (assoc 'format (mm-handle-type handle)))
167                           "flowed"))
168           (save-restriction
169             (narrow-to-region b (point))
170             (goto-char b)
171             (fill-flowed)
172             (goto-char (point-max))))
173         (save-restriction
174           (narrow-to-region b (point))
175           (set-text-properties (point-min) (point-max) nil)
176           (mm-handle-set-undisplayer
177            handle
178            `(lambda ()
179               (let (buffer-read-only)
180                 (delete-region ,(point-min-marker)
181                                ,(point-max-marker)))))))))))
182
183 (defun mm-insert-inline (handle text)
184   "Insert TEXT inline from HANDLE."
185   (let ((b (point)))
186     (insert text)
187     (mm-handle-set-undisplayer
188      handle
189      `(lambda ()
190         (let (buffer-read-only)
191           (delete-region ,(set-marker (make-marker) b)
192                          ,(set-marker (make-marker) (point))))))))
193
194 (defun mm-inline-audio (handle)
195   (message "Not implemented"))
196
197 (defun mm-view-sound-file ()
198   (message "Not implemented"))
199
200 (defun mm-w3-prepare-buffer ()
201   (require 'w3)
202   (let ((url-standalone-mode t))
203     (w3-prepare-buffer)))
204
205 (defun mm-view-message ()
206   (mm-enable-multibyte)
207   (let (handles)
208     (let (gnus-article-mime-handles)
209       ;; Double decode problem may happen.  See mm-inline-message.
210       (run-hooks 'gnus-article-decode-hook)
211       (gnus-article-prepare-display)
212       (setq handles gnus-article-mime-handles))
213     (when handles
214       (setq gnus-article-mime-handles
215             (nconc gnus-article-mime-handles 
216                    (if (listp (car handles)) 
217                        handles (list handles))))))
218   (fundamental-mode)
219   (goto-char (point-min)))
220
221 (defun mm-inline-message (handle)
222   (let ((b (point))
223         (charset (mail-content-type-get
224                   (mm-handle-type handle) 'charset))
225         gnus-displaying-mime handles)
226     (when (and charset
227                (stringp charset))
228       (setq charset (intern (downcase charset)))
229       (when (eq charset 'us-ascii)
230         (setq charset nil)))
231     (save-excursion
232       (save-restriction
233         (narrow-to-region b b)
234         (mm-insert-part handle)
235         (let (gnus-article-mime-handles
236               (gnus-newsgroup-charset
237                (or charset gnus-newsgroup-charset)))
238           (run-hooks 'gnus-article-decode-hook)
239           (gnus-article-prepare-display)
240           (setq handles gnus-article-mime-handles))
241         (goto-char (point-max))
242         (unless (bolp)
243           (insert "\n"))
244         (insert "----------\n\n")
245         (when handles
246           (setq gnus-article-mime-handles
247                 (nconc gnus-article-mime-handles 
248                        (if (listp (car handles)) 
249                            handles (list handles)))))
250         (mm-handle-set-undisplayer
251          handle
252          `(lambda ()
253             (let (buffer-read-only)
254               (condition-case nil
255                   ;; This is only valid on XEmacs.
256                   (mapcar (lambda (prop)
257                             (remove-specifier
258                              (face-property 'default prop) (current-buffer)))
259                           '(background background-pixmap foreground))
260                 (error nil))
261               (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
262
263 (defun mm-display-patch-inline (handle)
264   (let (text)
265     (with-temp-buffer
266       (mm-insert-part handle)
267       (diff-mode)
268       (font-lock-fontify-buffer)
269       (when (fboundp 'extent-list)
270         (map-extents (lambda (ext ignored)
271                        (set-extent-property ext 'duplicable t)
272                        nil)
273                      nil nil nil nil nil 'text-prop))
274       (setq text (buffer-string)))
275     (mm-insert-inline handle text)))
276
277 (provide 'mm-view)
278
279 ;; mm-view.el ends here