* semi-setup.el: Rename `mime-decrypt-application/pkcs7-mime' to
[elisp/semi.git] / mime-image.el
index 4eb0eef..341ee25 100644 (file)
@@ -4,7 +4,9 @@
 ;; Copyright (C) 1996 Dan Rich
 
 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
-;;         Dan Rich <drich@morpheus.corp.sgi.com>
+;;     Dan Rich <drich@morpheus.corp.sgi.com>
+;;     Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
+;;     Katsumi Yamaoka  <yamaoka@jpl.org>
 ;; Maintainer: MORIOKA Tomohiko <morioka@jaist.ac.jp>
 ;; Created: 1995/12/15
 ;;     Renamed: 1997/2/21 from tm-image.el
 
 ;;; Code:
 
+(eval-when-compile (require 'static))
+
 (require 'mime-view)
 (require 'alist)
-
-(cond ((featurep 'xemacs)
-       (require 'images)
-       
-       (defun-maybe image-inline-p (format)
-        (or (memq format image-native-formats)
-            (find-if (function
-                      (lambda (native)
-                        (image-converter-chain format native)
-                        ))
-                     image-native-formats)
-            ))
-       
-       (image-register-netpbm-utilities)
-       (image-register-converter 'pic 'ppm "pictoppm")
-       (image-register-converter 'mag 'ppm "magtoppm")
-       
-       (defun bitmap-insert-xbm-file (file)
-        (let ((gl (make-glyph (list (cons 'x file))))
-              (e (make-extent (point) (point)))
-              )
-          (set-extent-end-glyph e gl)
-          ))
-       
-       ;;
-       ;; X-Face
-       ;;
-       (autoload 'highlight-headers "highlight-headers")
-       
-       (defun mime-preview-x-face-function-use-highlight-headers ()
-        (highlight-headers (point-min) (re-search-forward "^$" nil t) t)
-        )
-       
-       (add-hook 'mime-view-content-header-filter-hook
-                'mime-preview-x-face-function-use-highlight-headers)
-       
-       )
-      ((featurep 'mule)
-       ;; for MULE 2.* or mule merged EMACS
-       (require 'x-face-mule)
-
-       (defvar image-native-formats '(xbm))
-       
-       (defun-maybe image-inline-p (format)
-        (memq format image-native-formats)
-        )
-       
-       (defun-maybe image-normalize (format data)
-        (and (eq format 'xbm)
-             (vector 'xbm ':data data)
-             ))
-       
-       ;;
-       ;; X-Face
-       ;;
-       (if (exec-installed-p uncompface-program exec-path)
-          (add-hook 'mime-view-content-header-filter-hook
-                    'x-face-decode-message-header)
-        )
-       ))
-
-(or (fboundp 'image-invalid-glyph-p)
-    (defsubst image-invalid-glyph-p (glyph)
-      (or (null (aref glyph 0))
-         (null (aref glyph 2))
-         (equal (aref glyph 2) "")
-         ))
-    )
-
-(defvar mime-view-image-converter-alist nil)
-
-(mapcar (function
-        (lambda (rule)
-          (let ((type    (car rule))
-                (subtype (nth 1 rule))
-                (format  (nth 2 rule)))
-            (if (image-inline-p format)
-                (let ((type/subtype (mime-type/subtype-string type subtype)))
-                   ;; (set-alist 'mime-view-content-filter-alist
-                   ;;            type/subtype #'mime-view-filter-for-image)
-                  (set-alist 'mime-view-image-converter-alist
-                             type/subtype format)
-                   ;; (add-to-list
-                   ;;  'mime-view-visible-media-type-list
-                   ;;  ctype)
-                  (ctree-set-calist-strictly
-                   'mime-preview-condition
-                   (list (cons 'type type)(cons 'subtype subtype)
-                         '(body . visible)
-                         (cons 'body-filter
-                               #'mime-view-filter-for-image)))
-                  )
-              ))))
-       '((image jpeg           jpeg)
-         (image gif            gif)
-         (image tiff           tiff)
-         (image x-tiff         tiff)
-         (image xbm            xbm)
-         (image x-xbm          xbm)
-         (image x-xpixmap      xpm)
-         (image x-pic          pic)
-         (image x-mag          mag)
-         (image png            png)
-         ))
-
-(defvar mime-view-ps-to-gif-command "pstogif")
+(require 'path-util)
+
+(cond
+ ((featurep 'xemacs)
+
+  (require 'images)
+
+  (defun-maybe image-inline-p (format)
+    (or (memq format image-native-formats)
+       (find-if (function
+                 (lambda (native)
+                   (image-converter-chain format native)))
+                image-native-formats)))
+
+  (image-register-netpbm-utilities)
+  (image-register-converter 'pic 'ppm "pictoppm")
+  (image-register-converter 'mag 'ppm "magtoppm")
+
+  (defun image-insert-at-point (image)
+    (let ((e (make-extent (point) (point))))
+      (set-extent-end-glyph e (make-glyph image))))
+
+  (defsubst-maybe image-invalid-glyph-p (glyph)
+    (or (null (aref glyph 0))
+       (null (aref glyph 2))
+       (equal (aref glyph 2) ""))))
+ ((featurep 'mule)
+
+  (eval-when-compile (ignore-errors (require 'image)))
+
+  (eval-and-compile
+    (autoload 'bitmap-insert-xbm-buffer "bitmap"))
+
+  (static-if (fboundp 'image-type-available-p)
+      (defalias-maybe 'image-inline-p 'image-type-available-p)
+    (defvar image-native-formats '(xbm))
+    (defun-maybe image-inline-p (format)
+      (memq format image-native-formats)))
+
+  (static-unless (or (not (fboundp 'create-image))
+                    (memq 'data-p (aref (symbol-function 'create-image) 0)))
+    (defadvice create-image
+      (around data-p (file-or-data &optional type data-p &rest props) activate)
+      (if (ad-get-arg 2)
+         (setq ad-return-value
+               (nconc 
+                (list 'image ':type (ad-get-arg 1) ':data (ad-get-arg 0))
+                props))
+       (ad-set-args 0 (list (ad-get-arg 0) (ad-get-arg 1) (ad-get-arg 3)))
+       ad-do-it)))
+
+  (defun-maybe image-normalize (format data)
+    (if (memq format '(xbm xpm))
+       (create-image data format 'data)
+      (let ((image-file
+            (make-temp-name
+             (expand-file-name "tm" temporary-file-directory))))
+       (with-temp-buffer
+         (insert data)
+         (write-region-as-binary (point-min)(point-max) image-file))
+       (create-image image-file format))))
+
+  (defun image-insert-at-point (image)
+    (static-if (fboundp 'insert-image)
+       (unwind-protect
+           (save-excursion
+             (static-if (condition-case nil
+                            (progn (insert-image '(image)) nil)
+                          (wrong-number-of-arguments t))
+                 (insert-image image "x")
+               (insert-image image))
+             (insert "\n")
+             (save-window-excursion
+               (set-window-buffer (selected-window)(current-buffer))
+               (sit-for 0)))
+         (let ((file (plist-get (cdr image) ':file)))
+           (and file (file-exists-p file)
+                (delete-file file))))
+      (when (eq (plist-get (cdr image) ':type) 'xbm)
+       (save-restriction
+         (narrow-to-region (point)(point))
+         (insert (plist-get (cdr image) ':data))
+         (let ((mark (set-marker (make-marker) (point))))
+           (bitmap-insert-xbm-buffer (current-buffer))
+           (delete-region (point-min) mark))))))
+
+  (defsubst-maybe image-invalid-glyph-p (glyph)
+    (not (eq 'image (nth 0 glyph))))))
+
+;;
+;; X-Face
+;;
+
+(cond
+ ((module-installed-p 'highlight-headers)
+  (eval-and-compile
+    (autoload 'highlight-headers "highlight-headers"))
+
+  (defun mime-preview-x-face-function-use-highlight-headers ()
+    (highlight-headers (point-min) (re-search-forward "^$" nil t) t))
+  (add-hook 'mime-display-header-hook
+           'mime-preview-x-face-function-use-highlight-headers))
+ ((featurep 'mule)
+  (require 'x-face-mule)
+  (when (exec-installed-p uncompface-program exec-path)
+    (add-hook 'mime-display-header-hook
+             'x-face-decode-message-header))))
+
+(defvar mime-image-format-alist
+  '((image jpeg                jpeg)
+    (image gif         gif)
+    (image tiff                tiff)
+    (image x-tiff      tiff)
+    (image xbm         xbm)
+    (image x-xbm       xbm)
+    (image x-xpixmap   xpm)
+    (image x-pic       pic)
+    (image x-mag       mag)
+    (image png         png)))
+
+(dolist (rule mime-image-format-alist)
+  (let ((type    (car rule))
+       (subtype (nth 1 rule))
+       (format  (nth 2 rule)))
+    (when (image-inline-p format)
+      (ctree-set-calist-strictly
+       'mime-preview-condition
+       (list (cons 'type type)(cons 'subtype subtype)
+            '(body . visible)
+            (cons 'body-presentation-method #'mime-display-image)
+            (cons 'image-format format))))))
 
 
 ;;; @ content filter for images
 ;;;
 ;;    (for XEmacs 19.12 or later)
 
-(defun mime-view-filter-for-image (ctype params encoding)
-  (let ((beg (point-min))
-       (end (point-max)))
-    (remove-text-properties beg end '(face nil))
-    (message "Decoding image...")
-    (mime-decode-region beg end encoding)
-    (let* ((minor (cdr (assoc ctype mime-view-image-converter-alist)))
-          (gl (image-normalize minor (buffer-string)))
-          e)
-      (delete-region (point-min)(point-max))
-      (cond ((image-invalid-glyph-p gl)
-            (setq gl nil)
-            (message "Invalid glyph!")
-            )
-           ((eq (aref gl 0) 'xbm)
-            (let ((xbm-file
-                   (make-temp-name
-                    (expand-file-name "tm" mime-temp-directory))))
-              (insert (aref gl 2))
-              (write-region (point-min)(point-max) xbm-file)
-              (message "Decoding image...")
-              (delete-region (point-min)(point-max))
-              (bitmap-insert-xbm-file xbm-file)
-              (delete-file xbm-file)
-              )
-            (message "Decoding image... done")
-            )
-           (t
-            (setq gl (make-glyph gl))
-            (setq e (make-extent (point) (point)))
-            (set-extent-end-glyph e gl)
-            (message "Decoding image... done")
-            ))
-      )
-    (insert "\n")
-    ))
-
-
-;;; @ content filter for Postscript
-;;;
-;;    (for XEmacs 19.14 or later)
-
-(defun mime-view-filter-for-application/postscript (ctype params encoding)
-  (let* ((beg (point-min)) (end (point-max))
-        (file-base
-         (make-temp-name (expand-file-name "tm" mime-temp-directory)))
-        (ps-file (concat file-base ".ps"))
-        (gif-file (concat file-base ".gif"))
-        )
-    (remove-text-properties beg end '(face nil))
-    (message "Decoding Postscript...")
-    (mime-decode-region beg end encoding)
-    (write-region (point-min)(point-max) ps-file) 
-    (message "Decoding Postscript...")
-    (delete-region (point-min)(point-max))
-    (call-process mime-view-ps-to-gif-command nil nil nil ps-file)
-    (set-extent-end-glyph (make-extent (point) (point))
-                         (make-glyph (vector 'gif :file gif-file)))
-    (message "Decoding Postscript... done")
-    (delete-file ps-file)
-    (delete-file gif-file)
-    ))
-
-;; If you would like to display inline Postscript image, please
-;; activate following:
-
-;; (set-alist 'mime-view-content-filter-alist
-;;            "application/postscript"
-;;            (function mime-view-filter-for-application/postscript))
-
-;; (if (featurep 'gif)
-;;     (add-to-list
-;;      'mime-view-visible-media-type-list "application/postscript")
-;;   )
+(eval-when-compile
+  (defmacro mime-image-normalize-xbm (entity)
+    (` (with-temp-buffer
+        (mime-insert-entity-content (, entity))
+        (let ((cur (current-buffer))
+              width height)
+          (goto-char (point-min))
+          (search-forward "width ")
+          (setq width (read cur))
+          (goto-char (point-min))
+          (search-forward "height ")
+          (setq height (read cur))
+          (goto-char (point-min))
+          (search-forward "{")
+          (delete-region (point-min) (point))
+          (insert "\"")
+          (search-forward "}")
+          (delete-region (1- (point)) (point-max))
+          (insert "\"")
+          (goto-char (point-min))
+          (while (re-search-forward "[^\"0-9A-FXa-fx]+" nil t)
+            (replace-match ""))
+          (goto-char (point-min))
+          (while (search-forward "0x" nil t)
+            (replace-match "\\\\x"))
+          (goto-char (point-min))
+          (, (if (featurep 'xemacs)
+                 (` (vector 'xbm :data
+                            (list width height (read cur))))
+               '(` (image :type xbm :width (, width) :height (, height)
+                          :data (, (read cur)))))))))))
+
+(defun mime-display-image (entity situation)
+  (message "Decoding image...")
+  (let* ((format (cdr (assq 'image-format situation)))
+        (image (if (or (featurep 'xemacs) (boundp 'image-types))
+                   (if (eq 'xbm format)
+                       (mime-image-normalize-xbm entity)
+                     (image-normalize format (mime-entity-content entity)))
+                 (image-normalize format (mime-entity-content entity)))))
+    (if (image-invalid-glyph-p image)
+       (message "Invalid glyph!")
+      (image-insert-at-point image)
+      (message "Decoding image... done")))
+  (static-when (featurep 'xemacs)
+    (insert "\n")))
 
 
 ;;; @ end