* nnslashdot.el (nnslashdot-sid-strip): New function.
[gnus] / lisp / mm-view.el
1 ;;; mm-view.el --- Functions for viewing MIME objects
2 ;; Copyright (C) 1998, 1999, 2000 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 "x")
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 (string-match "XEmacs" (emacs-version))
68       (fset 'mm-inline-image 'mm-inline-image-xemacs)
69     (fset '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 (w3-coding-system-for-mime-charset 
107                                (buffer-substring-no-properties 
108                                 (match-beginning 2) 
109                                 (match-end 2)))))
110             (delete-region (point-min) (point-max))
111             (insert (mm-decode-string text charset))
112             (save-window-excursion
113               (save-restriction
114                 (let ((w3-strict-width width)
115                       (url-standalone-mode t))
116                   (condition-case var
117                       (w3-region (point-min) (point-max))
118                     (error)))))
119             (mm-handle-set-undisplayer
120              handle
121              `(lambda ()
122                 (let (buffer-read-only)
123                   (if (functionp 'remove-specifier)
124                       (mapcar (lambda (prop)
125                                 (remove-specifier
126                                  (face-property 'default prop)
127                                  (current-buffer)))
128                               '(background background-pixmap foreground)))
129                   (delete-region ,(point-min-marker)
130                                  ,(point-max-marker)))))))))
131      ((or (equal type "enriched")
132           (equal type "richtext"))
133       (save-excursion
134         (mm-with-unibyte-buffer
135           (mm-insert-part handle)
136           (save-window-excursion
137             (enriched-decode (point-min) (point-max))
138             (setq text (buffer-string)))))
139       (mm-insert-inline handle text))
140      ((equal type "x-vcard")
141       (mm-insert-inline
142        handle
143        (concat "\n-- \n"
144                (if (fboundp 'vcard-pretty-print)
145                    (vcard-pretty-print (mm-get-part handle))
146                  (vcard-format-string
147                   (vcard-parse-string (mm-get-part handle)
148                                       'vcard-standard-filter))))))
149      (t
150       (let ((b (point))
151             (charset (mail-content-type-get
152                       (mm-handle-type handle) 'charset)))
153         (if (eq charset 'gnus-decoded)
154             (mm-insert-part handle)
155           (insert (mm-decode-string (mm-get-part handle) charset)))
156         (when (and (equal type "plain")
157                    (equal (cdr (assoc 'format (mm-handle-type handle)))
158                           "flowed"))
159           (save-restriction
160             (narrow-to-region b (point))
161             (goto-char b)
162             (fill-flowed)
163             (goto-char (point-max))))
164         (save-restriction
165           (narrow-to-region b (point))
166           (set-text-properties (point-min) (point-max) nil)
167           (mm-handle-set-undisplayer
168            handle
169            `(lambda ()
170               (let (buffer-read-only)
171                 (delete-region ,(point-min-marker)
172                                ,(point-max-marker)))))))))))
173
174 (defun mm-insert-inline (handle text)
175   "Insert TEXT inline from HANDLE."
176   (let ((b (point)))
177     (insert text)
178     (mm-handle-set-undisplayer
179      handle
180      `(lambda ()
181         (let (buffer-read-only)
182           (delete-region ,(set-marker (make-marker) b)
183                          ,(set-marker (make-marker) (point))))))))
184
185 (defun mm-inline-audio (handle)
186   (message "Not implemented"))
187
188 (defun mm-view-sound-file ()
189   (message "Not implemented"))
190
191 (defun mm-w3-prepare-buffer ()
192   (require 'w3)
193   (let ((url-standalone-mode t))
194     (w3-prepare-buffer)))
195
196 (defun mm-view-message ()
197   (mm-enable-multibyte)
198   (let (handles)
199     (let (gnus-article-mime-handles)
200       ;; Double decode problem may happen.  See mm-inline-message.
201       (run-hooks 'gnus-article-decode-hook)
202       (gnus-article-prepare-display)
203       (setq handles gnus-article-mime-handles))
204     (when handles
205       (setq gnus-article-mime-handles
206             (nconc gnus-article-mime-handles 
207                    (if (listp (car handles)) 
208                        handles (list handles))))))
209   (fundamental-mode)
210   (goto-char (point-min)))
211
212 (defun mm-inline-message (handle)
213   (let ((b (point))
214         (charset (mail-content-type-get
215                   (mm-handle-type handle) 'charset))
216         gnus-displaying-mime handles)
217     (when (and charset
218                (stringp charset))
219       (setq charset (intern (downcase charset)))
220       (when (eq charset 'us-ascii)
221         (setq charset nil)))
222     (save-excursion
223       (save-restriction
224         (narrow-to-region b b)
225         (mm-insert-part handle)
226         (let (gnus-article-mime-handles
227               ;; disable prepare hook 
228               gnus-article-prepare-hook  
229               (gnus-newsgroup-charset
230                (or charset gnus-newsgroup-charset)))
231           (run-hooks 'gnus-article-decode-hook)
232           (gnus-article-prepare-display)
233           (setq handles gnus-article-mime-handles))
234         (goto-char (point-max))
235         (unless (bolp)
236           (insert "\n"))
237         (insert "----------\n\n")
238         (when handles
239           (setq gnus-article-mime-handles
240                 (nconc gnus-article-mime-handles 
241                        (if (listp (car handles)) 
242                            handles (list handles)))))
243         (mm-handle-set-undisplayer
244          handle
245          `(lambda ()
246             (let (buffer-read-only)
247               (condition-case nil
248                   ;; This is only valid on XEmacs.
249                   (mapcar (lambda (prop)
250                             (remove-specifier
251                              (face-property 'default prop) (current-buffer)))
252                           '(background background-pixmap foreground))
253                 (error nil))
254               (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
255
256 (defun mm-display-patch-inline (handle)
257   (let (text)
258     (with-temp-buffer
259       (mm-insert-part handle)
260       (diff-mode)
261       (font-lock-fontify-buffer)
262       (when (fboundp 'extent-list)
263         (map-extents (lambda (ext ignored)
264                        (set-extent-property ext 'duplicable t)
265                        nil)
266                      nil nil nil nil nil 'text-prop))
267       (setq text (buffer-string)))
268     (mm-insert-inline handle text)))
269
270 (provide 'mm-view)
271
272 ;; mm-view.el ends here