2000-11-02 16:53:32 ShengHuo ZHU <zsh@cs.rochester.edu>
[gnus] / lisp / mm-extern.el
1 ;;; mm-extern.el --- showing message/external-body
2 ;; Copyright (C) 2000 Free Software Foundation, Inc.
3
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
5 ;; Keywords: message external-body
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published
11 ;; by the Free Software Foundation; either version 2, or (at your
12 ;; option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 ;; General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile 
29   (require 'cl))
30
31 (require 'mm-util)
32 (require 'mm-decode)
33
34 (defvar mm-extern-function-alist
35   '((local-file . mm-extern-local-file)
36     (url . mm-extern-url)
37 ;;;     (ftp . mm-extern-ftp)
38 ;;;     (anon-ftp . mm-extern-anon-ftp)
39 ;;;     (tftp . mm-extern-tftp)
40 ;;;     (mail-server . mm-extern-mail-server))
41     ))
42
43 (defun mm-extern-local-file (handle)
44   (let ((name (cdr (assq 'name (cdr (mm-handle-type handle)))))
45         (coding-system-for-read mm-binary-coding-system))
46     (mm-disable-multibyte-mule4)
47     (mm-insert-file-contents name nil nil nil nil t)))
48
49 (defun mm-extern-url (handle)
50   (require 'url)
51   (let ((url (cdr (assq 'url (cdr (mm-handle-type handle)))))
52         (name buffer-file-name)
53         (coding-system-for-read mm-binary-coding-system))
54     (unless url
55       (error "URL is not specified"))
56     (mm-with-unibyte-current-buffer-mule4
57       (url-insert-file-contents url))
58     (mm-disable-multibyte-mule4)
59     (setq buffer-file-name name)))
60
61 ;;;###autoload
62 (defun mm-inline-external-body (handle &optional no-display)
63   "Show the external-body part of HANDLE.
64 This function replaces the buffer of HANDLE with a buffer contains 
65 the entire message.
66 If NO-DISPLAY is nil, display it. Otherwise, do nothing after replacing."
67   (let* ((access-type (cdr (assq 'access-type 
68                                  (cdr (mm-handle-type handle)))))
69          (func (cdr (assq (intern access-type) mm-extern-function-alist)))
70          gnus-displaying-mime buf
71          handles)
72     (unless (mm-handle-cache handle)
73       (unless func
74         (error (format "Access type (%s) is not supported." access-type)))
75       (with-temp-buffer
76         (mm-insert-part handle)
77         (goto-char (point-max))
78         (insert "\n\n")
79         (setq handles (mm-dissect-buffer t)))
80       (unless (bufferp (car handles))
81         (mm-destroy-parts handles)
82         (error "Multipart external body is not supported."))
83       (save-excursion ;; single part
84         (kill-buffer (mm-handle-buffer handles))
85         (set-buffer (setq buf (generate-new-buffer " *mm*")))
86         (condition-case err
87             (funcall func handle)
88           (error 
89            ;; Don't require gnus-util
90            (when (gnus-buffer-exists-p buf)
91              (kill-buffer buf))
92            (error err)))
93         (setcar handles (current-buffer))
94         (mm-handle-set-cache handle handles))
95       (push handles gnus-article-mime-handles))
96     (unless no-display
97       (save-excursion
98         (save-restriction
99           (narrow-to-region (point) (point))
100           (gnus-display-mime (mm-handle-cache handle))
101           (mm-handle-set-undisplayer
102            handle
103            `(lambda ()
104               (let (buffer-read-only)
105                 (condition-case nil
106                     ;; This is only valid on XEmacs.
107                     (mapcar (lambda (prop)
108                             (remove-specifier
109                              (face-property 'default prop) (current-buffer)))
110                             '(background background-pixmap foreground))
111                   (error nil))
112                 (delete-region ,(point-min-marker) ,(point-max-marker))))))))))
113
114 ;; mm-extern.el ends here