Commit 7d88e015 authored by Chong Yidong's avatar Chong Yidong

(image-type-from-file-name, image-type): Simplify.

(image-type-auto-detected-p): Don't scan auto-mode-alist.
parent b447c934
...@@ -292,43 +292,28 @@ be determined." ...@@ -292,43 +292,28 @@ be determined."
"Determine the type of image file FILE from its name. "Determine the type of image file FILE from its name.
Value is a symbol specifying the image type, or nil if type cannot Value is a symbol specifying the image type, or nil if type cannot
be determined." be determined."
(let ((types image-type-file-name-regexps) (assoc-default file image-type-file-name-regexps 'string-match))
type)
(while types
(if (string-match (car (car types)) file)
(setq type (cdr (car types))
types nil)
(setq types (cdr types))))
type))
;;;###autoload ;;;###autoload
(defun image-type (file-or-data &optional type data-p) (defun image-type (source &optional type data-p)
"Determine and return image type. "Determine and return image type.
FILE-OR-DATA is an image file name or image data. SOURCE is an image file name or image data.
Optional TYPE is a symbol describing the image type. If TYPE is omitted 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 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, of image data. If that doesn't work, and SOURCE 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 DATA-P non-nil means SOURCE is a string containing image data."
(when (and (not data-p) (not (stringp file-or-data))) (when (and (not data-p) (not (stringp source)))
(error "Invalid image file name `%s'" file-or-data)) (error "Invalid image file name `%s'" source))
(cond ((null data-p)
;; FILE-OR-DATA is a file name.
(unless (or type
(setq type (image-type-from-file-header file-or-data)))
(let ((extension (file-name-extension file-or-data)))
(unless extension
(error "Cannot determine image type"))
(setq type (intern extension)))))
(t
;; FILE-OR-DATA contains image data.
(unless type
(setq type (image-type-from-data file-or-data)))))
(unless type (unless type
(error "Cannot determine image type")) (setq type (if data-p
(unless (symbolp type) (image-type-from-data source)
(error "Invalid image type `%s'" type)) (or (image-type-from-file-header source)
(image-type-from-file-name source))))
(or type (error "Cannot determine image type")))
(or (memq type image-types)
(error "Invalid image type `%s'" type))
type) type)
...@@ -343,30 +328,15 @@ Image types are symbols like `xbm' or `jpeg'." ...@@ -343,30 +328,15 @@ Image types are symbols like `xbm' or `jpeg'."
;;;###autoload ;;;###autoload
(defun image-type-auto-detected-p () (defun image-type-auto-detected-p ()
"Return t iff the current buffer contains an auto-detectable image. "Return t iff the current buffer contains an auto-detectable image.
This function is intended to be used from `magic-mode-alist' (which see). This function is intended to be used from `magic-fallback-mode-alist'.
First, compare the beginning of the buffer with `image-type-header-regexps'. The buffer is considered to contain an auto-detectable image if
If an appropriate image type is found, check if that image type can be its beginning matches an image type in `image-type-header-regexps',
autodetected using the variable `image-type-auto-detectable'. Finally, and that image type is present in `image-type-auto-detectable'."
if `buffer-file-name' is non-nil, check if it matches another major mode
in `auto-mode-alist' apart from `image-mode'; if there is another match,
the autodetection is considered to have failed. Return t if all the above
steps succeed."
(let* ((type (image-type-from-buffer)) (let* ((type (image-type-from-buffer))
(auto (and type (cdr (assq type image-type-auto-detectable))))) (auto (and type (cdr (assq type image-type-auto-detectable)))))
(and auto (and type
(or (eq auto t) (image-type-available-p type)) (or (eq auto t) (image-type-available-p type)))))
(or (null buffer-file-name)
(not (assoc-default
buffer-file-name
(delq nil (mapcar
(lambda (elt)
(unless (memq (or (car-safe (cdr elt))
(cdr elt))
'(image-mode image-mode-maybe))
elt))
auto-mode-alist))
'string-match))))))
;;;###autoload ;;;###autoload
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment