(mime-edit-encrypt-pgp-mime): Encode header before encrypting.
[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               (static-if (condition-case nil
101                              (progn (insert-image '(image)) nil)
102                            (wrong-number-of-arguments t))
103                   (insert-image image "x")
104                 (insert-image image))
105               (insert "\n")
106               (save-window-excursion
107                 (set-window-buffer (selected-window)(current-buffer))
108                 (sit-for 0)))
109           (let ((file (plist-get (cdr image) ':file)))
110             (and file (file-exists-p file)
111                  (delete-file file)
112                  )))
113       (when (eq (plist-get (cdr image) ':type) 'xbm)
114         (save-restriction
115           (narrow-to-region (point)(point))
116           (insert (plist-get (cdr image) ':data))
117           (let ((mark (set-marker (make-marker) (point))))
118             (bitmap-insert-xbm-buffer (current-buffer))
119             (delete-region (point-min) mark))
120           ))))
121
122   (defsubst-maybe image-invalid-glyph-p (glyph)
123     (not (eq 'image (nth 0 glyph))))
124   ))
125
126 ;;
127 ;; X-Face
128 ;;
129
130 (cond
131  ((module-installed-p 'highlight-headers)
132   (eval-and-compile
133     (autoload 'highlight-headers "highlight-headers"))
134  
135   (defun mime-preview-x-face-function-use-highlight-headers ()
136     (highlight-headers (point-min) (re-search-forward "^$" nil t) t)
137     )
138   (add-hook 'mime-display-header-hook
139             'mime-preview-x-face-function-use-highlight-headers)
140   )
141  ((featurep 'mule)
142   (require 'x-face-mule)
143   (when (exec-installed-p uncompface-program exec-path)
144     (add-hook 'mime-display-header-hook
145               'x-face-decode-message-header))
146   ))
147
148 (defvar mime-image-format-alist
149   '((image jpeg         jpeg)
150     (image gif          gif)
151     (image tiff         tiff)
152     (image x-tiff       tiff)
153     (image xbm          xbm)
154     (image x-xbm        xbm)
155     (image x-xpixmap    xpm)
156     (image x-pic        pic)
157     (image x-mag        mag)
158     (image png          png)))
159
160 (dolist (rule mime-image-format-alist)
161   (let ((type    (car rule))
162         (subtype (nth 1 rule))
163         (format  (nth 2 rule)))
164     (when (image-inline-p format)
165       (ctree-set-calist-strictly
166        'mime-preview-condition
167        (list (cons 'type type)(cons 'subtype subtype)
168              '(body . visible)
169              (cons 'body-presentation-method #'mime-display-image)
170              (cons 'image-format format))
171        ))))
172
173
174 ;;; @ content filter for images
175 ;;;
176 ;;    (for XEmacs 19.12 or later)
177
178 (defun mime-display-image (entity situation)
179   (message "Decoding image...")
180   (let ((image (image-normalize (cdr (assq 'image-format situation))
181                                 (mime-entity-content entity))))
182     (if (image-invalid-glyph-p image)
183         (message "Invalid glyph!")
184       (image-insert-at-point image)
185       (message "Decoding image... done")))
186   (insert "\n")
187   )
188
189
190 ;;; @ end
191 ;;;
192
193 (provide 'mime-image)
194
195 ;;; mime-image.el ends here