Synch with Oort Gnus.
[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   (insert "\n")
53   (forward-char -1)
54   (let ((b (point))
55         (annot (make-annotation (mm-get-image handle) nil 'text))
56         buffer-read-only)
57     (mm-handle-set-undisplayer
58      handle
59      `(lambda ()
60         (let (buffer-read-only)
61           (delete-annotation ,annot)
62           (delete-region ,(set-marker (make-marker) b)
63                          ,(set-marker (make-marker) (point))))))
64     (set-extent-property annot 'mm t)
65     (set-extent-property annot 'duplicable t)))
66
67 (eval-and-compile
68   (if (featurep 'xemacs)
69       (defalias 'mm-inline-image 'mm-inline-image-xemacs)
70     (defalias 'mm-inline-image 'mm-inline-image-emacs)))
71
72 (defvar mm-w3-setup nil)
73 (defun mm-setup-w3 ()
74   (unless mm-w3-setup
75     (require 'w3)
76     (w3-do-setup)
77     (require 'url)
78     (require 'w3-vars)
79     (require 'url-vars)
80     (setq mm-w3-setup t)))
81
82 (defun mm-inline-text (handle)
83   (let ((type (mm-handle-media-subtype handle))
84         text buffer-read-only)
85     (cond
86      ((equal type "html")
87       (mm-setup-w3)
88       (setq text (mm-get-part handle))
89       (let ((b (point))
90             (url-standalone-mode t)
91             (url-current-object
92              (url-generic-parse-url (format "cid:%s" (mm-handle-id handle))))
93             (width (window-width))
94             (charset (mail-content-type-get
95                       (mm-handle-type handle) 'charset)))
96         (save-excursion
97           (insert text)
98           (save-restriction
99             (narrow-to-region b (point))
100             (goto-char (point-min))
101             (if (or (and (boundp 'w3-meta-content-type-charset-regexp)
102                          (re-search-forward
103                           w3-meta-content-type-charset-regexp nil t))
104                     (and (boundp 'w3-meta-charset-content-type-regexp)
105                          (re-search-forward
106                           w3-meta-charset-content-type-regexp nil t)))
107                 (setq charset (or (w3-coding-system-for-mime-charset
108                                    (buffer-substring-no-properties
109                                     (match-beginning 2)
110                                     (match-end 2)))
111                                   charset)))
112             (delete-region (point-min) (point-max))
113             (insert (mm-decode-string text charset))
114             (save-window-excursion
115               (save-restriction
116                 (let ((w3-strict-width width)
117                       ;; Don't let w3 set the global version of
118                       ;; this variable.
119                       (fill-column fill-column)
120                       (url-standalone-mode t))
121                   (condition-case var
122                       (w3-region (point-min) (point-max))
123                     (error
124                      (message
125                       "Error while rendering html; showing as text/plain"))))))
126             (mm-handle-set-undisplayer
127              handle
128              `(lambda ()
129                 (let (buffer-read-only)
130                   (if (functionp 'remove-specifier)
131                       (mapcar (lambda (prop)
132                                 (remove-specifier
133                                  (face-property 'default prop)
134                                  (current-buffer)))
135                               '(background background-pixmap foreground)))
136                   (delete-region ,(point-min-marker)
137                                  ,(point-max-marker)))))))))
138      ((or (equal type "enriched")
139           (equal type "richtext"))
140       (save-excursion
141         (mm-with-unibyte-buffer
142           (mm-insert-part handle)
143           (save-window-excursion
144             (enriched-decode (point-min) (point-max))
145             (setq text (buffer-string)))))
146       (mm-insert-inline handle text))
147      ((equal type "x-vcard")
148       (mm-insert-inline
149        handle
150        (concat "\n-- \n"
151                (if (fboundp 'vcard-pretty-print)
152                    (vcard-pretty-print (mm-get-part handle))
153                  (vcard-format-string
154                   (vcard-parse-string (mm-get-part handle)
155                                       'vcard-standard-filter))))))
156      (t
157       (let ((b (point))
158             (charset (mail-content-type-get
159                       (mm-handle-type handle) 'charset)))
160         (if (or (eq charset 'gnus-decoded)
161                 ;; This is probably not entirely correct, but
162                 ;; makes rfc822 parts with embedded multiparts work.
163                 (eq mail-parse-charset 'gnus-decoded))
164             (save-restriction
165               (narrow-to-region (point) (point))
166               (mm-insert-part handle)
167               (goto-char (point-max)))
168           (insert (mm-decode-string (mm-get-part handle) charset)))
169         (when (and (equal type "plain")
170                    (equal (cdr (assoc 'format (mm-handle-type handle)))
171                           "flowed"))
172           (save-restriction
173             (narrow-to-region b (point))
174             (goto-char b)
175             (fill-flowed)
176             (goto-char (point-max))))
177         (save-restriction
178           (narrow-to-region b (point))
179           (set-text-properties (point-min) (point-max) nil)
180           (mm-handle-set-undisplayer
181            handle
182            `(lambda ()
183               (let (buffer-read-only)
184                 (delete-region ,(point-min-marker)
185                                ,(point-max-marker)))))))))))
186
187 (defun mm-insert-inline (handle text)
188   "Insert TEXT inline from HANDLE."
189   (let ((b (point)))
190     (insert text)
191     (mm-handle-set-undisplayer
192      handle
193      `(lambda ()
194         (let (buffer-read-only)
195           (delete-region ,(set-marker (make-marker) b)
196                          ,(set-marker (make-marker) (point))))))))
197
198 (defun mm-inline-audio (handle)
199   (message "Not implemented"))
200
201 (defun mm-view-sound-file ()
202   (message "Not implemented"))
203
204 (defun mm-w3-prepare-buffer ()
205   (require 'w3)
206   (let ((url-standalone-mode t))
207     (w3-prepare-buffer)))
208
209 (defun mm-view-message ()
210   (mm-enable-multibyte)
211   (let (handles)
212     (let (gnus-article-mime-handles)
213       ;; Double decode problem may happen.  See mm-inline-message.
214       (run-hooks 'gnus-article-decode-hook)
215       (gnus-article-prepare-display)
216       (setq handles gnus-article-mime-handles))
217     (when handles
218       (setq gnus-article-mime-handles
219             (mm-merge-handles gnus-article-mime-handles handles))))
220   (fundamental-mode)
221   (goto-char (point-min)))
222
223 (defun mm-inline-message (handle)
224   (let ((b (point))
225         (bolp (bolp))
226         (charset (mail-content-type-get
227                   (mm-handle-type handle) 'charset))
228         gnus-displaying-mime handles)
229     (when (and charset
230                (stringp charset))
231       (setq charset (intern (downcase charset)))
232       (when (eq charset 'us-ascii)
233         (setq charset nil)))
234     (save-excursion
235       (save-restriction
236         (narrow-to-region b b)
237         (mm-insert-part handle)
238         (let (gnus-article-mime-handles
239               ;; disable prepare hook
240               gnus-article-prepare-hook
241               (gnus-newsgroup-charset
242                (or charset gnus-newsgroup-charset)))
243           (run-hooks 'gnus-article-decode-hook)
244           (gnus-article-prepare-display)
245           (setq handles gnus-article-mime-handles))
246         (goto-char (point-min))
247         (unless bolp
248           (insert "\n"))
249         (goto-char (point-max))
250         (unless (bolp)
251           (insert "\n"))
252         (insert "----------\n\n")
253         (when handles
254           (setq gnus-article-mime-handles
255                 (mm-merge-handles gnus-article-mime-handles handles)))
256         (mm-handle-set-undisplayer
257          handle
258          `(lambda ()
259             (let (buffer-read-only)
260               (if (fboundp 'remove-specifier)
261                   ;; This is only valid on XEmacs.
262                   (mapcar (lambda (prop)
263                             (remove-specifier
264                              (face-property 'default prop) (current-buffer)))
265                           '(background background-pixmap foreground)))
266               (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
267
268 (defun mm-display-inline-fontify (handle mode)
269   (let (text)
270     ;; XEmacs @#$@ version of font-lock refuses to fully turn itself
271     ;; on for buffers whose name begins with " ".  That's why we use
272     ;; save-current-buffer/get-buffer-create rather than
273     ;; with-temp-buffer.
274     (save-current-buffer
275       (set-buffer (generate-new-buffer "*fontification*"))
276       (unwind-protect
277           (progn
278             (buffer-disable-undo)
279             (mm-insert-part handle)
280             (funcall mode)
281             (let ((font-lock-verbose nil))
282               ;; I find font-lock a bit too verbose.
283               (font-lock-fontify-buffer))
284             ;; By default, XEmacs font-lock uses non-duplicable text
285             ;; properties.  This code forces all the text properties
286             ;; to be copied along with the text.
287             (when (fboundp 'extent-list)
288               (map-extents (lambda (ext ignored)
289                              (set-extent-property ext 'duplicable t)
290                              nil)
291                            nil nil nil nil nil 'text-prop))
292             (setq text (buffer-string)))
293         (kill-buffer (current-buffer))))
294     (mm-insert-inline handle text)))
295
296 ;; Shouldn't these functions check whether the user even wants to use
297 ;; font-lock?  At least under XEmacs, this fontification is pretty
298 ;; much unconditional.  Also, it would be nice to change for the size
299 ;; of the fontified region.
300
301 (defun mm-display-patch-inline (handle)
302   (mm-display-inline-fontify handle 'diff-mode))
303
304 (defun mm-display-elisp-inline (handle)
305   (mm-display-inline-fontify handle 'emacs-lisp-mode))
306
307 (provide 'mm-view)
308
309 ;; mm-view.el ends here