Sync up with Pterodactyl Gnus v0.96.
[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 ;; Avoid byte compile warning.
37 (defvar gnus-article-mime-handles)
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                       (mapc (lambda (prop)
112                               (remove-specifier
113                                (face-property 'default prop) (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   (gnus-article-prepare-display)
174   (run-hooks 'gnus-article-decode-hook)
175   (fundamental-mode)
176   (goto-char (point-min)))
177
178 (defun mm-inline-message (handle)
179   (let ((b (point))
180         gnus-displaying-mime handles)
181     (save-excursion
182       (save-restriction
183         (narrow-to-region b b)
184         (mm-insert-part handle)
185         (let (gnus-article-mime-handles)
186           (run-hooks 'gnus-article-decode-hook)
187           (gnus-article-prepare-display)
188           (setq handles gnus-article-mime-handles))
189         (when handles
190           (setq gnus-article-mime-handles
191                 (append gnus-article-mime-handles handles)))
192         (mm-handle-set-undisplayer
193          handle
194          `(lambda ()
195             (let (buffer-read-only)
196               (ignore-errors
197                 ;; This is only valid on XEmacs.
198                 (mapc (lambda (prop)
199                         (remove-specifier
200                          (face-property 'default prop) (current-buffer)))
201                       '(background background-pixmap foreground)))
202               (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
203
204 (provide 'mm-view)
205
206 ;; mm-view.el ends here