* semi-setup.el: Delete default setting of
[elisp/semi.git] / mime-image.el
1 ;;; mime-image.el --- mime-view filter to display images
2
3 ;; Copyright (C) 1995,1996,1997,1998 MORIOKA Tomohiko
4 ;; Copyright (C) 1996 Dan Rich
5
6 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;         Dan Rich <drich@morpheus.corp.sgi.com>
8 ;; Maintainer: MORIOKA Tomohiko <morioka@jaist.ac.jp>
9 ;; Created: 1995/12/15
10 ;;      Renamed: 1997/2/21 from tm-image.el
11
12 ;; Keywords: image, picture, X-Face, MIME, multimedia, mail, news
13
14 ;; This file is part of SEMI (Showy Emacs MIME Interfaces).
15
16 ;; This program is free software; you can redistribute it and/or
17 ;; modify it under the terms of the GNU General Public License as
18 ;; published by the Free Software Foundation; either version 2, or (at
19 ;; your option) any later version.
20
21 ;; This program is distributed in the hope that it will be useful, but
22 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24 ;; General Public License for more details.
25
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU XEmacs; see the file COPYING.  If not, write to the
28 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 ;; Boston, MA 02111-1307, USA.
30
31 ;;; Commentary:
32 ;;      If you use this program with MULE, please install
33 ;;      etl8x16-bitmap.bdf font included in tl package.
34
35 ;;; Code:
36
37 (require 'mime-view)
38 (require 'alist)
39 (require 'path-util)
40
41 (cond 
42  ((featurep 'xemacs) 
43
44   (require 'images)
45
46   (defun-maybe image-inline-p (format)
47     (or (memq format image-native-formats)
48         (find-if (function
49                   (lambda (native)
50                     (image-converter-chain format native)
51                     ))
52                  image-native-formats)
53         ))
54        
55   (image-register-netpbm-utilities)
56   (image-register-converter 'pic 'ppm "pictoppm")
57   (image-register-converter 'mag 'ppm "magtoppm")
58        
59   (defun image-insert-at-point (image)
60     (let ((e (make-extent (point) (point))))
61       (set-extent-end-glyph e (make-glyph image))))
62
63   (defsubst-maybe image-invalid-glyph-p (glyph)
64     (or (null (aref glyph 0))
65         (null (aref glyph 2))
66         (equal (aref glyph 2) "")
67         ))
68   )
69  ((featurep 'mule) 
70
71   (eval-when-compile (require 'static))
72
73   (eval-and-compile
74     (autoload 'bitmap-insert-xbm-buffer "bitmap")
75     )
76
77   (static-if (fboundp 'image-type-available-p)
78       (defalias-maybe 'image-inline-p 'image-type-available-p)
79     (defvar image-native-formats '(xbm))
80     (defun-maybe image-inline-p (format)
81       (memq format image-native-formats)))
82
83   (defun-maybe image-normalize (format data)
84     (if (memq format '(xbm xpm))
85         (list 'image ':type format ':data data)
86       (let ((image-file 
87              (make-temp-name 
88               (expand-file-name "tm" temporary-file-directory))))
89         (with-temp-buffer
90           (set-buffer-multibyte nil)
91           (insert data)
92           (write-region (point-min)(point-max) image-file))
93         (list 'image ':type format ':file image-file)
94         )))
95
96   (defun image-insert-at-point (image)
97     (static-if (fboundp 'insert-image)
98         (unwind-protect
99             (save-excursion
100               (insert-image image)
101               (save-window-excursion
102                 (set-window-buffer (selected-window)(current-buffer))
103                 (sit-for 0)))
104           (let ((file (plist-get (cdr image) ':file)))
105             (and file (file-exists-p file)
106                  (delete-file file)
107                  )))
108       (when (eq (plist-get (cdr image) ':type) 'xbm)
109         (save-restriction
110           (narrow-to-region (point)(point))
111           (insert (plist-get (cdr image) ':data))
112           (let ((mark (set-marker (make-marker) (point))))
113             (bitmap-insert-xbm-buffer (current-buffer))
114             (delete-region (point-min) mark))
115           ))))
116
117   (defsubst-maybe image-invalid-glyph-p (glyph)
118     (not (eq 'image (nth 0 glyph))))
119   ))
120
121 ;;
122 ;; X-Face
123 ;;
124
125 (cond
126  ((module-installed-p 'highlight-headers)
127   (eval-and-compile
128     (autoload 'highlight-headers "highlight-headers"))
129  
130   (defun mime-preview-x-face-function-use-highlight-headers ()
131     (highlight-headers (point-min) (re-search-forward "^$" nil t) t)
132     )
133   (add-hook 'mime-display-header-hook
134             'mime-preview-x-face-function-use-highlight-headers)
135   )
136  ((featurep 'mule)
137   (require 'x-face-mule)
138   (when (exec-installed-p uncompface-program exec-path)
139     (add-hook 'mime-display-header-hook
140               'x-face-decode-message-header))
141   ))
142
143 (defvar mime-image-format-alist
144   '((image jpeg         jpeg)
145     (image gif          gif)
146     (image tiff         tiff)
147     (image x-tiff       tiff)
148     (image xbm          xbm)
149     (image x-xbm        xbm)
150     (image x-xpixmap    xpm)
151     (image x-pic        pic)
152     (image x-mag        mag)
153     (image png          png)))
154
155 (dolist (rule mime-image-format-alist)
156   (let ((type    (car rule))
157         (subtype (nth 1 rule))
158         (format  (nth 2 rule)))
159     (when (image-inline-p format)
160       (ctree-set-calist-strictly
161        'mime-preview-condition
162        (list (cons 'type type)(cons 'subtype subtype)
163              '(body . visible)
164              (cons 'body-presentation-method #'mime-display-image)
165              (cons 'image-format format))
166        ))))
167
168
169 ;;; @ content filter for images
170 ;;;
171 ;;    (for XEmacs 19.12 or later)
172
173 (defun mime-display-image (entity situation)
174   (message "Decoding image...")
175   (let ((gl (image-normalize (cdr (assq 'image-format situation))
176                              (mime-entity-content entity))))
177     (cond ((image-invalid-glyph-p gl)
178            (setq gl nil)
179            (message "Invalid glyph!")
180            )
181           ((eq (aref gl 0) 'xbm)
182            (let ((xbm-file
183                   (make-temp-name
184                    (expand-file-name "tm" temporary-file-directory))))
185              (with-temp-buffer
186                (insert (aref gl 2))
187                (write-region (point-min)(point-max) xbm-file)
188                )
189              (message "Decoding image...")
190              (bitmap-insert-xbm-file xbm-file)
191              (delete-file xbm-file)
192              )
193            (message "Decoding image... done")
194            )
195           (t
196            (setq gl (make-glyph gl))
197            (let ((e (make-extent (point) (point))))
198              (set-extent-end-glyph e gl)
199              )
200            (message "Decoding image... done")
201            ))
202     )
203   (insert "\n")
204   )
205
206
207 ;;; @ end
208 ;;;
209
210 (provide 'mime-image)
211
212 ;;; mime-image.el ends here