aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lisp/image.el72
1 files changed, 21 insertions, 51 deletions
diff --git a/lisp/image.el b/lisp/image.el
index c9269abce3e..06f006de252 100644
--- a/lisp/image.el
+++ b/lisp/image.el
@@ -292,43 +292,28 @@ be determined."
292 "Determine the type of image file FILE from its name. 292 "Determine the type of image file FILE from its name.
293Value is a symbol specifying the image type, or nil if type cannot 293Value is a symbol specifying the image type, or nil if type cannot
294be determined." 294be determined."
295 (let ((types image-type-file-name-regexps) 295 (assoc-default file image-type-file-name-regexps 'string-match))
296 type)
297 (while types
298 (if (string-match (car (car types)) file)
299 (setq type (cdr (car types))
300 types nil)
301 (setq types (cdr types))))
302 type))
303 296
304 297
305;;;###autoload 298;;;###autoload
306(defun image-type (file-or-data &optional type data-p) 299(defun image-type (source &optional type data-p)
307 "Determine and return image type. 300 "Determine and return image type.
308FILE-OR-DATA is an image file name or image data. 301SOURCE is an image file name or image data.
309Optional TYPE is a symbol describing the image type. If TYPE is omitted 302Optional TYPE is a symbol describing the image type. If TYPE is omitted
310or nil, try to determine the image type from its first few bytes 303or nil, try to determine the image type from its first few bytes
311of image data. If that doesn't work, and FILE-OR-DATA is a file name, 304of image data. If that doesn't work, and SOURCE is a file name,
312use its file extension as image type. 305use its file extension as image type.
313Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data." 306Optional DATA-P non-nil means SOURCE is a string containing image data."
314 (when (and (not data-p) (not (stringp file-or-data))) 307 (when (and (not data-p) (not (stringp source)))
315 (error "Invalid image file name `%s'" file-or-data)) 308 (error "Invalid image file name `%s'" source))
316 (cond ((null data-p)
317 ;; FILE-OR-DATA is a file name.
318 (unless (or type
319 (setq type (image-type-from-file-header file-or-data)))
320 (let ((extension (file-name-extension file-or-data)))
321 (unless extension
322 (error "Cannot determine image type"))
323 (setq type (intern extension)))))
324 (t
325 ;; FILE-OR-DATA contains image data.
326 (unless type
327 (setq type (image-type-from-data file-or-data)))))
328 (unless type 309 (unless type
329 (error "Cannot determine image type")) 310 (setq type (if data-p
330 (unless (symbolp type) 311 (image-type-from-data source)
331 (error "Invalid image type `%s'" type)) 312 (or (image-type-from-file-header source)
313 (image-type-from-file-name source))))
314 (or type (error "Cannot determine image type")))
315 (or (memq type image-types)
316 (error "Invalid image type `%s'" type))
332 type) 317 type)
333 318
334 319
@@ -343,30 +328,15 @@ Image types are symbols like `xbm' or `jpeg'."
343;;;###autoload 328;;;###autoload
344(defun image-type-auto-detected-p () 329(defun image-type-auto-detected-p ()
345 "Return t iff the current buffer contains an auto-detectable image. 330 "Return t iff the current buffer contains an auto-detectable image.
346This function is intended to be used from `magic-mode-alist' (which see). 331This function is intended to be used from `magic-fallback-mode-alist'.
347 332
348First, compare the beginning of the buffer with `image-type-header-regexps'. 333The buffer is considered to contain an auto-detectable image if
349If an appropriate image type is found, check if that image type can be 334its beginning matches an image type in `image-type-header-regexps',
350autodetected using the variable `image-type-auto-detectable'. Finally, 335and that image type is present in `image-type-auto-detectable'."
351if `buffer-file-name' is non-nil, check if it matches another major mode
352in `auto-mode-alist' apart from `image-mode'; if there is another match,
353the autodetection is considered to have failed. Return t if all the above
354steps succeed."
355 (let* ((type (image-type-from-buffer)) 336 (let* ((type (image-type-from-buffer))
356 (auto (and type (cdr (assq type image-type-auto-detectable))))) 337 (auto (and type (cdr (assq type image-type-auto-detectable)))))
357 (and auto 338 (and type
358 (or (eq auto t) (image-type-available-p type)) 339 (or (eq auto t) (image-type-available-p type)))))
359 (or (null buffer-file-name)
360 (not (assoc-default
361 buffer-file-name
362 (delq nil (mapcar
363 (lambda (elt)
364 (unless (memq (or (car-safe (cdr elt))
365 (cdr elt))
366 '(image-mode image-mode-maybe))
367 elt))
368 auto-mode-alist))
369 'string-match))))))
370 340
371 341
372;;;###autoload 342;;;###autoload