Importing pgnus-0.62
[elisp/gnus.git-] / lisp / mm-view.el
1 ;;; mm-view.el --- Functions for viewing MIME objects
2 ;; Copyright (C) 1998 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 ;;;
32 ;;; Functions for displaying various formats inline
33 ;;;
34
35 (defun mm-inline-image (handle)
36   (let ((annot (make-annotation (mm-get-image handle) nil 'text))
37         buffer-read-only)
38     (mm-insert-inline handle ".\n")
39     (set-extent-property annot 'mm t)
40     (set-extent-property annot 'duplicable t)))
41
42 (defvar mm-w3-setup nil)
43 (defun mm-setup-w3 ()
44   (unless mm-w3-setup
45     (w3-do-setup)
46     (require 'url)
47     (require 'w3-vars)
48     (condition-case ()
49         (load "url-misc.el")
50       (error nil))
51     (setq mm-w3-setup t)))
52
53 (defun mm-inline-text (handle)
54   (let ((type (cadr (split-string (car (mm-handle-type handle)) "/")))
55         text buffer-read-only)
56     (cond
57      ((equal type "plain")
58       (setq text (mm-get-part handle))
59       (let ((b (point))
60             (charset (mail-content-type-get
61                       (mm-handle-type handle) 'charset)))
62         (insert (mm-decode-string text charset))
63         (save-restriction
64           (narrow-to-region b (point))
65           (mm-handle-set-undisplayer
66            handle
67            `(lambda ()
68               (let (buffer-read-only)
69                 (delete-region ,(point-min-marker) ,(point-max-marker))))))))
70      ((equal type "html")
71       (mm-setup-w3)
72       (setq text (mm-get-part handle))
73       (let ((b (point))
74             (width (window-width)))
75         (save-excursion
76           (insert text)
77           (save-restriction
78             (narrow-to-region b (point))
79             (save-window-excursion
80               (let ((w3-strict-width width))
81                 (w3-region (point-min) (point-max)))))
82           (mm-handle-set-undisplayer
83            handle
84            `(lambda ()
85               (let (buffer-read-only)
86               (mapc (lambda (prop)
87                       (remove-specifier
88                        (face-property 'default prop) (current-buffer)))
89                     '(background background-pixmap foreground))
90                 (delete-region ,(point-min-marker) ,(point-max-marker))))))))
91      ((or (equal type "enriched")
92           (equal type "richtext"))
93       (save-excursion
94         (mm-with-unibyte-buffer
95           (insert-buffer-substring (mm-handle-buffer handle))
96           (mm-decode-content-transfer-encoding
97            (mm-handle-encoding handle)
98            (car (mm-handle-type handle)))
99           (save-window-excursion
100             (enriched-decode (point-min) (point-max))
101             (setq text (buffer-string)))))
102       (mm-insert-inline handle text))
103      (t
104       (save-excursion
105         (mm-with-unibyte-buffer
106           (insert-buffer-substring (mm-handle-buffer handle))
107           (mm-decode-content-transfer-encoding
108            (mm-handle-encoding handle)
109            (car (mm-handle-type handle)))
110           (setq text (buffer-string))))
111       (mm-insert-inline handle text)))))
112
113 (defun mm-insert-inline (handle text)
114   "Insert TEXT inline from HANDLE."
115   (let ((b (point)))
116     (insert text)
117     (mm-handle-set-undisplayer
118      handle
119      `(lambda ()
120         (let (buffer-read-only)
121           (delete-region ,(set-marker (make-marker) b)
122                          ,(set-marker (make-marker) (point))))))))
123   
124 (defun mm-inline-audio (handle)
125   (message "Not implemented"))
126
127 (defun mm-view-sound-file ()
128   (message "Not implemented"))
129
130 (defun mm-w3-prepare-buffer ()
131   (require 'w3)
132   (w3-prepare-buffer))
133
134 (provide 'mm-view)
135
136 ;; mm-view.el ends here