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