Importing pgnus-0.79
[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 (require 'mail-parse)
27 (require 'mailcap)
28 (require 'mm-bodies)
29 (require 'mm-decode)
30
31 (eval-and-compile
32   (autoload 'gnus-article-prepare-display "gnus-art")
33   (autoload 'vcard-parse-string "vcard")
34   (autoload 'vcard-format-string "vcard"))
35
36 ;;;
37 ;;; Functions for displaying various formats inline
38 ;;;
39
40 (defun mm-inline-image (handle)
41   (let ((annot (make-annotation (mm-get-image handle) nil 'text))
42         buffer-read-only)
43     (mm-insert-inline handle "\n")
44     (set-extent-property annot 'mm t)
45     (set-extent-property annot 'duplicable t)))
46
47 (defvar mm-w3-setup nil)
48 (defun mm-setup-w3 ()
49   (unless mm-w3-setup
50     (require 'w3)
51     (w3-do-setup)
52     (require 'url)
53     (require 'w3-vars)
54     (require 'url-vars)
55     (setq mm-w3-setup t)))
56
57 (defun mm-inline-text (handle)
58   (let ((type (cadr (split-string (car (mm-handle-type handle)) "/")))
59         text buffer-read-only)
60     (cond
61      ((equal type "html")
62       (mm-setup-w3)
63       (setq text (mm-get-part handle))
64       (let ((b (point))
65             (url-standalone-mode t)
66             (url-current-object
67              (url-generic-parse-url (format "cid:%s" (mm-handle-id handle))))
68             (width (window-width)))
69         (save-excursion
70           (insert text)
71           (save-restriction
72             (narrow-to-region b (point))
73             (save-window-excursion
74               (let ((w3-strict-width width)
75                     (url-standalone-mode t))
76                 (w3-region (point-min) (point-max)))))
77           (mm-handle-set-undisplayer
78            handle
79            `(lambda ()
80               (let (buffer-read-only)
81               (mapc (lambda (prop)
82                       (remove-specifier
83                        (face-property 'default prop) (current-buffer)))
84                     '(background background-pixmap foreground))
85                 (delete-region ,(point-min-marker) ,(point-max-marker))))))))
86      ((or (equal type "enriched")
87           (equal type "richtext"))
88       (save-excursion
89         (mm-with-unibyte-buffer
90           (mm-insert-part handle)
91           (save-window-excursion
92             (enriched-decode (point-min) (point-max))
93             (setq text (buffer-string)))))
94       (mm-insert-inline handle text))
95      ((equal type "x-vcard")
96       (mm-insert-inline
97        handle
98        (concat "\n-- \n"
99                (vcard-format-string
100                 (vcard-parse-string (mm-get-part handle)
101                                     'vcard-standard-filter)))))
102      (t
103       (setq text (mm-get-part handle))
104       (let ((b (point))
105             (charset (mail-content-type-get
106                       (mm-handle-type handle) 'charset)))
107         (insert (mm-decode-string text charset))
108         (save-restriction
109           (narrow-to-region b (point))
110           (set-text-properties (point-min) (point-max) nil)
111           (mm-handle-set-undisplayer
112            handle
113            `(lambda ()
114               (let (buffer-read-only)
115                 (delete-region ,(point-min-marker)
116                                ,(point-max-marker)))))))))))
117
118 (defun mm-insert-inline (handle text)
119   "Insert TEXT inline from HANDLE."
120   (let ((b (point)))
121     (insert text)
122     (mm-handle-set-undisplayer
123      handle
124      `(lambda ()
125         (let (buffer-read-only)
126           (delete-region ,(set-marker (make-marker) b)
127                          ,(set-marker (make-marker) (point))))))))
128
129 (defun mm-inline-audio (handle)
130   (message "Not implemented"))
131
132 (defun mm-view-sound-file ()
133   (message "Not implemented"))
134
135 (defun mm-w3-prepare-buffer ()
136   (require 'w3)
137   (let ((url-standalone-mode t))
138     (w3-prepare-buffer)))
139
140 (defun mm-view-message ()
141   (mm-enable-multibyte)
142   (gnus-article-prepare-display)
143   (run-hooks 'gnus-article-decode-hook)
144   (fundamental-mode)
145   (goto-char (point-min)))
146
147 (defun mm-inline-message (handle)
148   (let ((b (point)))
149     (save-excursion
150       (save-restriction
151         (narrow-to-region b b)
152         (mm-insert-part handle)
153         (run-hooks 'gnus-article-decode-hook)
154         (gnus-article-prepare-display)
155         (mm-handle-set-undisplayer
156          handle
157          `(lambda ()
158             (let (buffer-read-only)
159               (mapc (lambda (prop)
160                       (remove-specifier
161                        (face-property 'default prop) (current-buffer)))
162                     '(background background-pixmap foreground))
163               (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
164
165 (provide 'mm-view)
166
167 ;; mm-view.el ends here