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