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