b81c78b760e629387b356e9d511885bc03bb59a7
[gnus] / 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           (narrow-to-region (1+ (point-min)) (point-max))
78           (mm-handle-set-undisplayer
79            handle
80            `(lambda ()
81               (let (buffer-read-only)
82                 (mapc (lambda (prop)
83                         (remove-specifier
84                          (face-property 'default prop) (current-buffer)))
85                         '(background background-pixmap foreground))
86                 (delete-region ,(point-min-marker)
87                                ,(point-max-marker))))))))
88      ((or (equal type "enriched")
89           (equal type "richtext"))
90       (save-excursion
91         (mm-with-unibyte-buffer
92           (mm-insert-part handle)
93           (save-window-excursion
94             (enriched-decode (point-min) (point-max))
95             (setq text (buffer-string)))))
96       (mm-insert-inline handle text))
97      ((equal type "x-vcard")
98       (mm-insert-inline
99        handle
100        (concat "\n-- \n"
101                (vcard-format-string
102                 (vcard-parse-string (mm-get-part handle)
103                                     'vcard-standard-filter)))))
104      (t
105       (setq text (mm-get-part handle))
106       (let ((b (point))
107             (charset (mail-content-type-get
108                       (mm-handle-type handle) 'charset)))
109         (insert (mm-decode-string text charset))
110         (save-restriction
111           (narrow-to-region b (point))
112           (set-text-properties (point-min) (point-max) nil)
113           (mm-handle-set-undisplayer
114            handle
115            `(lambda ()
116               (let (buffer-read-only)
117                 (delete-region ,(point-min-marker)
118                                ,(point-max-marker)))))))))))
119
120 (defun mm-insert-inline (handle text)
121   "Insert TEXT inline from HANDLE."
122   (let ((b (point)))
123     (insert text)
124     (mm-handle-set-undisplayer
125      handle
126      `(lambda ()
127         (let (buffer-read-only)
128           (delete-region ,(set-marker (make-marker) b)
129                          ,(set-marker (make-marker) (point))))))))
130
131 (defun mm-inline-audio (handle)
132   (message "Not implemented"))
133
134 (defun mm-view-sound-file ()
135   (message "Not implemented"))
136
137 (defun mm-w3-prepare-buffer ()
138   (require 'w3)
139   (let ((url-standalone-mode t))
140     (w3-prepare-buffer)))
141
142 (defun mm-view-message ()
143   (mm-enable-multibyte)
144   (gnus-article-prepare-display)
145   (run-hooks 'gnus-article-decode-hook)
146   (fundamental-mode)
147   (goto-char (point-min)))
148
149 (defun mm-inline-message (handle)
150   (let ((b (point)))
151     (save-excursion
152       (save-restriction
153         (narrow-to-region b b)
154         (mm-insert-part handle)
155         (run-hooks 'gnus-article-decode-hook)
156         (gnus-article-prepare-display)
157         (mm-handle-set-undisplayer
158          handle
159          `(lambda ()
160             (let (buffer-read-only)
161               (mapc (lambda (prop)
162                       (remove-specifier
163                        (face-property 'default prop) (current-buffer)))
164                     '(background background-pixmap foreground))
165               (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
166
167 (provide 'mm-view)
168
169 ;; mm-view.el ends here