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