diff options
| author | Chong Yidong | 2007-05-21 16:43:13 +0000 |
|---|---|---|
| committer | Chong Yidong | 2007-05-21 16:43:13 +0000 |
| commit | 7d88e015c4474c4e0936022fc353f8e8922ebfdc (patch) | |
| tree | ff350cc1ce232cb51930daf30b01491f53d3fad8 | |
| parent | b447c934c40f3bbbd08ae7ebd8b2fb71e1afbd37 (diff) | |
| download | emacs-7d88e015c4474c4e0936022fc353f8e8922ebfdc.tar.gz emacs-7d88e015c4474c4e0936022fc353f8e8922ebfdc.zip | |
(image-type-from-file-name, image-type): Simplify.
(image-type-auto-detected-p): Don't scan auto-mode-alist.
| -rw-r--r-- | lisp/image.el | 72 |
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. |
| 293 | Value is a symbol specifying the image type, or nil if type cannot | 293 | Value is a symbol specifying the image type, or nil if type cannot |
| 294 | be determined." | 294 | be 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. |
| 308 | FILE-OR-DATA is an image file name or image data. | 301 | SOURCE is an image file name or image data. |
| 309 | Optional TYPE is a symbol describing the image type. If TYPE is omitted | 302 | Optional TYPE is a symbol describing the image type. If TYPE is omitted |
| 310 | or nil, try to determine the image type from its first few bytes | 303 | or nil, try to determine the image type from its first few bytes |
| 311 | of image data. If that doesn't work, and FILE-OR-DATA is a file name, | 304 | of image data. If that doesn't work, and SOURCE is a file name, |
| 312 | use its file extension as image type. | 305 | use its file extension as image type. |
| 313 | Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data." | 306 | Optional 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. |
| 346 | This function is intended to be used from `magic-mode-alist' (which see). | 331 | This function is intended to be used from `magic-fallback-mode-alist'. |
| 347 | 332 | ||
| 348 | First, compare the beginning of the buffer with `image-type-header-regexps'. | 333 | The buffer is considered to contain an auto-detectable image if |
| 349 | If an appropriate image type is found, check if that image type can be | 334 | its beginning matches an image type in `image-type-header-regexps', |
| 350 | autodetected using the variable `image-type-auto-detectable'. Finally, | 335 | and that image type is present in `image-type-auto-detectable'." |
| 351 | if `buffer-file-name' is non-nil, check if it matches another major mode | ||
| 352 | in `auto-mode-alist' apart from `image-mode'; if there is another match, | ||
| 353 | the autodetection is considered to have failed. Return t if all the above | ||
| 354 | steps 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 |