]> code.delx.au - gnu-emacs/blobdiff - lisp/image.el
File regenerated.
[gnu-emacs] / lisp / image.el
index 9b04784ec0071b0fef514cdc4df67e2584a16f14..e4c2387d78e3a86b0e89fd74d1ae211bebe16b55 100644 (file)
@@ -1,6 +1,6 @@
 ;;; image.el --- image API
 
-;; Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
+;; Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
 ;; Keywords: multimedia
 
 ;; This file is part of GNU Emacs.
 
 ;;; Code:
 
+
+(defgroup image ()
+  "Image support."
+  :group 'multimedia)
+
+
 (defconst image-type-regexps
-  '(("^/\\*.*XPM.\\*/" . xpm)
-    ("^P[1-6]" . pbm)
-    ("^GIF8" . gif)
-    ("JFIF" . jpeg)
-    ("^\211PNG\r\n" . png)
-    ("^#define" . xbm)
-    ("^\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff)
-    ("^%!PS" . ghostscript))
+  '(("\\`/[\t\n\r ]*\\*.*XPM.\\*/" . xpm)
+    ("\\`P[1-6]" . pbm)
+    ("\\`GIF8" . gif)
+    ("\\`\211PNG\r\n" . png)
+    ("\\`[\t\n\r ]*#define" . xbm)
+    ("\\`\\(MM\0\\*\\|II\\*\0\\)" . tiff)
+    ("\\`[\t\n\r ]*%!PS" . postscript)
+    ("\\`\xff\xd8" . (image-jpeg-p . jpeg)))
   "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
 When the first bytes of an image file match REGEXP, it is assumed to
-be of image type IMAGE-TYPE.")
+be of image type IMAGE-TYPE if IMAGE-TYPE is a symbol.  If not a symbol,
+IMAGE-TYPE must be a pair (PREDICATE . TYPE).  PREDICATE is called
+with one argument, a string containing the image data.  If PREDICATE returns
+a non-nil value, TYPE is the image's type ")
+
+
+(defun image-jpeg-p (data)
+  "Value is non-nil if DATA, a string, consists of JFIF image data."
+  (when (string-match "\\`\xff\xd8" data)
+    (catch 'jfif
+      (let ((len (length data)) (i 2))
+       (while (< i len)
+         (when (/= (aref data i) #xff)
+           (throw 'jfif nil))
+         (setq i (1+ i))
+         (when (>= (+ i 2) len)
+           (throw 'jfif nil))
+         (let ((nbytes (+ (lsh (aref data (+ i 1)) 8)
+                          (aref data (+ i 2))))
+               (code (aref data i)))
+           (when (and (>= code #xe0) (<= code #xef))
+             ;; APP0 LEN1 LEN2 "JFIF\0"
+             (throw 'jfif 
+                    (string-match "JFIF" (substring data i (+ i nbytes)))))
+           (setq i (+ i 1 nbytes))))))))
 
 
 ;;;###autoload
@@ -48,7 +78,11 @@ be determined."
     (while (and types (null type))
       (let ((regexp (car (car types)))
            (image-type (cdr (car types))))
-       (when (string-match regexp data)
+       (when (or (and (symbolp image-type)
+                      (string-match regexp data))
+                 (and (consp image-type)
+                      (funcall (car image-type) data)
+                      (setq image-type (cdr image-type))))
          (setq type image-type))
        (setq types (cdr types))))
     type))
@@ -82,10 +116,10 @@ FILE-OR-DATA is an image file name or image data.
 Optional TYPE is a symbol describing the image type.  If TYPE is omitted
 or nil, try to determine the image type from its first few bytes
 of image data.  If that doesn't work, and FILE-OR-DATA is a file name,
-use its file extension.as image type.
+use its file extension as image type.
 Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
 Optional PROPS are additional image attributes to assign to the image,
-like, e.g. `:heuristic-mask t'.
+like, e.g. `:mask MASK'.
 Value is the image created, or nil if images of type TYPE are not supported."
   (when (and (not data-p) (not (stringp file-or-data)))
     (error "Invalid image file name `%s'" file-or-data))
@@ -146,20 +180,29 @@ AREA is where to display the image.  AREA nil or omitted means
 display it in the text area, a value of `left-margin' means
 display it in the left marginal area, a value of `right-margin'
 means display it in the right marginal area."
-  (unless string (setq string "x"))
+  ;; Use a space as least likely to cause trouble when it's a hidden
+  ;; character in the buffer.
+  (unless string (setq string " "))
   (unless (eq (car-safe image) 'image)
     (error "Not an image: %s" image))
   (unless (or (null area) (memq area '(left-margin right-margin)))
     (error "Invalid area %s" area))
-  (when area
-    (setq image (list (list 'margin area) image)))
+  (if area
+      (setq image (list (list 'margin area) image))
+    ;; Cons up a new spec equal but not eq to `image' so that
+    ;; inserting it twice in a row (adjacently) displays two copies of
+    ;; the image.  Don't try to avoid this by looking at the display
+    ;; properties on either side so that we DTRT more often with
+    ;; cut-and-paste.  (Yanking killed image text next to another copy
+    ;; of it loses anyway.)
+    (setq image (cons 'image (cdr image))))
   (let ((start (point)))
     (insert string)
-    ;; Copy `image' so that inserting it twice in a row (adjacently)
-    ;; displays two copies of the image.
     (add-text-properties start (point)
-                        (list 'display (copy-sequence image)
-                              'intangible (list t) ; something unique
+                        (list 'display image
+                              ;; `image' has the right properties to
+                              ;; mark an intangible field.
+                              'intangible image
                               'rear-nonsticky (list 'display)))))
 
 
@@ -216,7 +259,8 @@ The image is looked for first on `load-path' and then in `data-directory'."
                           (setq found try-file))))
                   (if found
                       (setq image
-                            (cons 'image (plist-put spec :file found))))))
+                            (cons 'image (plist-put (copy-sequence spec)
+                                                    :file found))))))
                ((not (null data))
                 (setq image (cons 'image spec)))))
        (setq specs (cdr specs))))