639c32ebc51cd5ae7efb37c9a035bce9094a5d5b
[elisp/gnus.git-] / lisp / mm-view.el
1 ;;; mm-view.el --- Functions for viewing MIME objects
2 ;; Copyright (C) 1998, 1999, 2000, 2001 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 (eval-when-compile (require 'cl))
27 (require 'mail-parse)
28 (require 'mailcap)
29 (require 'mm-bodies)
30 (require 'mm-decode)
31
32 (eval-and-compile
33   (autoload 'gnus-article-prepare-display "gnus-art")
34   (autoload 'vcard-parse-string "vcard")
35   (autoload 'vcard-format-string "vcard")
36   (autoload 'fill-flowed "flow-fill")
37   (autoload 'diff-mode "diff-mode"))
38
39 ;;;
40 ;;; Functions for displaying various formats inline
41 ;;;
42 (defun mm-inline-image-emacs (handle)
43   (let ((b (point-marker))
44         buffer-read-only)
45     (insert "\n")
46     (put-image (mm-get-image handle) b)
47     (mm-handle-set-undisplayer
48      handle
49      `(lambda () (remove-images ,b (1+ ,b))))))
50
51 (defun mm-inline-image-xemacs (handle)
52   (let ((b (point))
53         (annot (make-annotation (mm-get-image handle) nil 'text))
54         buffer-read-only)
55     (insert "\n")
56     (mm-handle-set-undisplayer
57      handle
58      `(lambda ()
59         (let (buffer-read-only)
60           (delete-annotation ,annot)
61           (delete-region ,(set-marker (make-marker) b)
62                          ,(set-marker (make-marker) (point))))))
63     (set-extent-property annot 'mm t)
64     (set-extent-property annot 'duplicable t)))
65
66 (eval-and-compile
67   (if (featurep 'xemacs)
68       (defalias 'mm-inline-image 'mm-inline-image-xemacs)
69     (defalias 'mm-inline-image 'mm-inline-image-emacs)))
70
71 (defvar mm-w3-setup nil)
72 (defun mm-setup-w3 ()
73   (unless mm-w3-setup
74     (require 'w3)
75     (w3-do-setup)
76     (require 'url)
77     (require 'w3-vars)
78     (require 'url-vars)
79     (setq mm-w3-setup t)))
80
81 (defun mm-inline-text (handle)
82   (let ((type (mm-handle-media-subtype handle))
83         text buffer-read-only)
84     (cond
85      ((equal type "html")
86       (mm-setup-w3)
87       (setq text (mm-get-part handle))
88       (let ((b (point))
89             (url-standalone-mode t)
90             (url-current-object
91              (url-generic-parse-url (format "cid:%s" (mm-handle-id handle))))
92             (width (window-width))
93             (charset (mail-content-type-get
94                       (mm-handle-type handle) 'charset)))
95         (save-excursion
96           (insert text)
97           (save-restriction
98             (narrow-to-region b (point))
99             (goto-char (point-min))
100             (if (or (and (boundp 'w3-meta-content-type-charset-regexp)
101                          (re-search-forward
102                           w3-meta-content-type-charset-regexp nil t))
103                     (and (boundp 'w3-meta-charset-content-type-regexp)
104                          (re-search-forward
105                           w3-meta-charset-content-type-regexp nil t)))
106                 (setq charset (or (w3-coding-system-for-mime-charset
107                                    (buffer-substring-no-properties
108                                     (match-beginning 2)
109                                     (match-end 2)))
110                                   charset)))
111             (delete-region (point-min) (point-max))
112             (insert (mm-decode-string text charset))
113             (save-window-excursion
114               (save-restriction
115                 (let ((w3-strict-width width)
116                       ;; Don't let w3 set the global version of
117                       ;; this variable.
118                       (fill-column fill-column)
119                       (url-standalone-mode t))
120                   (condition-case var
121                       (w3-region (point-min) (point-max))
122                     (error
123                      (message
124                       "Error while rendering html; showing as text/plain"))))))
125             (mm-handle-set-undisplayer
126              handle
127              `(lambda ()
128                 (let (buffer-read-only)
129                   (if (functionp 'remove-specifier)
130                       (mapcar (lambda (prop)
131                                 (remove-specifier
132                                  (face-property 'default prop)
133                                  (current-buffer)))
134                               '(background background-pixmap foreground)))
135                   (delete-region ,(point-min-marker)
136                                  ,(point-max-marker)))))))))
137      ((or (equal type "enriched")
138           (equal type "richtext"))
139       (save-excursion
140         (mm-with-unibyte-buffer
141           (mm-insert-part handle)
142           (save-window-excursion
143             (enriched-decode (point-min) (point-max))
144             (setq text (buffer-string)))))
145       (mm-insert-inline handle text))
146      ((equal type "x-vcard")
147       (mm-insert-inline
148        handle
149        (concat "\n-- \n"
150                (if (fboundp 'vcard-pretty-print)
151                    (vcard-pretty-print (mm-get-part handle))
152                  (vcard-format-string
153                   (vcard-parse-string (mm-get-part handle)
154                                       'vcard-standard-filter))))))
155      (t
156       (let ((b (point))
157             (charset (mail-content-type-get
158                       (mm-handle-type handle) 'charset)))
159         (if (or (eq charset 'gnus-decoded)
160                 ;; This is probably not entirely correct, but
161                 ;; makes rfc822 parts with embedded multiparts work.
162                 (eq mail-parse-charset 'gnus-decoded))
163             (save-restriction
164               (narrow-to-region (point) (point))
165               (mm-insert-part handle)
166               (goto-char (point-max)))
167           (insert (mm-decode-string (mm-get-part handle) charset)))
168         (when (and (equal type "plain")
169                    (equal (cdr (assoc 'format (mm-handle-type handle)))
170                           "flowed"))
171           (save-restriction
172             (narrow-to-region b (point))
173             (goto-char b)
174             (fill-flowed)
175             (goto-char (point-max))))
176         (save-restriction
177           (narrow-to-region b (point))
178           (set-text-properties (point-min) (point-max) nil)
179           (mm-handle-set-undisplayer
180            handle
181            `(lambda ()
182               (let (buffer-read-only)
183                 (delete-region ,(point-min-marker)
184                                ,(point-max-marker)))))))))))
185
186 (defun mm-insert-inline (handle text)
187   "Insert TEXT inline from HANDLE."
188   (let ((b (point)))
189     (insert text)
190     (mm-handle-set-undisplayer
191      handle
192      `(lambda ()
193         (let (buffer-read-only)
194           (delete-region ,(set-marker (make-marker) b)
195                          ,(set-marker (make-marker) (point))))))))
196
197 (defun mm-inline-audio (handle)
198   (message "Not implemented"))
199
200 (defun mm-view-sound-file ()
201   (message "Not implemented"))
202
203 (defun mm-w3-prepare-buffer ()
204   (require 'w3)
205   (let ((url-standalone-mode t))
206     (w3-prepare-buffer)))
207
208 (defun mm-view-message ()
209   (mm-enable-multibyte)
210   (let (handles)
211     (let (gnus-article-mime-handles)
212       ;; Double decode problem may happen.  See mm-inline-message.
213       (run-hooks 'gnus-article-decode-hook)
214       (gnus-article-prepare-display)
215       (setq handles gnus-article-mime-handles))
216     (when handles
217       (setq gnus-article-mime-handles
218             (mm-merge-handles gnus-article-mime-handles handles))))
219   (fundamental-mode)
220   (goto-char (point-min)))
221
222 (defun mm-inline-message (handle)
223   (let ((b (point))
224         (bolp (bolp))
225         (charset (mail-content-type-get
226                   (mm-handle-type handle) 'charset))
227         gnus-displaying-mime handles)
228     (when (and charset
229                (stringp charset))
230       (setq charset (intern (downcase charset)))
231       (when (eq charset 'us-ascii)
232         (setq charset nil)))
233     (save-excursion
234       (save-restriction
235         (narrow-to-region b b)
236         (mm-insert-part handle)
237         (let (gnus-article-mime-handles
238               ;; disable prepare hook
239               gnus-article-prepare-hook
240               (gnus-newsgroup-charset
241                (or charset gnus-newsgroup-charset)))
242           (run-hooks 'gnus-article-decode-hook)
243           (gnus-article-prepare-display)
244           (setq handles gnus-article-mime-handles))
245         (goto-char (point-min))
246         (unless bolp
247           (insert "\n"))
248         (goto-char (point-max))
249         (unless (bolp)
250           (insert "\n"))
251         (insert "----------\n\n")
252         (when handles
253           (setq gnus-article-mime-handles
254                 (mm-merge-handles gnus-article-mime-handles handles)))
255         (mm-handle-set-undisplayer
256          handle
257          `(lambda ()
258             (let (buffer-read-only)
259               (if (fboundp 'remove-specifier)
260                   ;; This is only valid on XEmacs.
261                   (mapcar (lambda (prop)
262                             (remove-specifier
263                              (face-property 'default prop) (current-buffer)))
264                           '(background background-pixmap foreground)))
265               (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
266
267 (defun mm-display-inline-fontify (handle mode)
268   (let (text)
269     (with-temp-buffer
270       (mm-insert-part handle)
271       (funcall mode)
272       (font-lock-fontify-buffer)
273       (when (fboundp 'extent-list)
274         (map-extents (lambda (ext ignored)
275                        (set-extent-property ext 'duplicable t)
276                        nil)
277                      nil nil nil nil nil 'text-prop))
278       (setq text (buffer-string)))
279     (mm-insert-inline handle text)))
280
281 (defun mm-display-patch-inline (handle)
282   (mm-display-inline-fontify handle 'diff-mode))
283
284 (defun mm-display-elisp-inline (handle)
285   (mm-display-inline-fontify handle 'emacs-lisp-mode))
286
287 (provide 'mm-view)
288
289 ;; mm-view.el ends here