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