Ensure that X selection convertors properly encode returned strings.
[emacs.git] / lisp / image.el
blobbe1b1ef8f489b69483ad1368b484029d86f53f26
1 ;;; image.el --- image API
3 ;; Copyright (C) 1998-2012 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: multimedia
7 ;; Package: emacs
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;;; Code:
29 (defgroup image ()
30 "Image support."
31 :group 'multimedia)
33 (defalias 'image-refresh 'image-flush)
35 (defconst image-type-header-regexps
36 `(("\\`/[\t\n\r ]*\\*.*XPM.\\*/" . xpm)
37 ("\\`P[1-6][[:space:]]+\\(?:#.*[[:space:]]+\\)*[0-9]+[[:space:]]+[0-9]+" . pbm)
38 ("\\`GIF8[79]a" . gif)
39 ("\\`\x89PNG\r\n\x1a\n" . png)
40 ("\\`[\t\n\r ]*#define \\([a-z0-9_]+\\)_width [0-9]+\n\
41 #define \\1_height [0-9]+\n\\(\
42 #define \\1_x_hot [0-9]+\n\
43 #define \\1_y_hot [0-9]+\n\\)?\
44 static \\(unsigned \\)?char \\1_bits" . xbm)
45 ("\\`\\(?:MM\0\\*\\|II\\*\0\\)" . tiff)
46 ("\\`[\t\n\r ]*%!PS" . postscript)
47 ("\\`\xff\xd8" . jpeg) ; used to be (image-jpeg-p . jpeg)
48 (,(let* ((incomment-re "\\(?:[^-]\\|-[^-]\\)")
49 (comment-re (concat "\\(?:!--" incomment-re "*-->[ \t\r\n]*<\\)")))
50 (concat "\\(?:<\\?xml[ \t\r\n]+[^>]*>\\)?[ \t\r\n]*<"
51 comment-re "*"
52 "\\(?:!DOCTYPE[ \t\r\n]+[^>]*>[ \t\r\n]*<[ \t\r\n]*" comment-re "*\\)?"
53 "[Ss][Vv][Gg]"))
54 . svg)
56 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
57 When the first bytes of an image file match REGEXP, it is assumed to
58 be of image type IMAGE-TYPE if IMAGE-TYPE is a symbol. If not a symbol,
59 IMAGE-TYPE must be a pair (PREDICATE . TYPE). PREDICATE is called
60 with one argument, a string containing the image data. If PREDICATE returns
61 a non-nil value, TYPE is the image's type.")
63 (defvar image-type-file-name-regexps
64 '(("\\.png\\'" . png)
65 ("\\.gif\\'" . gif)
66 ("\\.jpe?g\\'" . jpeg)
67 ("\\.bmp\\'" . bmp)
68 ("\\.xpm\\'" . xpm)
69 ("\\.pbm\\'" . pbm)
70 ("\\.xbm\\'" . xbm)
71 ("\\.ps\\'" . postscript)
72 ("\\.tiff?\\'" . tiff)
73 ("\\.svgz?\\'" . svg)
75 "Alist of (REGEXP . IMAGE-TYPE) pairs used to identify image files.
76 When the name of an image file match REGEXP, it is assumed to
77 be of image type IMAGE-TYPE.")
79 ;; We rely on `auto-mode-alist' to detect xbm and xpm files, instead
80 ;; of content autodetection. Their contents are just C code, so it is
81 ;; easy to generate false matches.
82 (defvar image-type-auto-detectable
83 '((pbm . t)
84 (xbm . nil)
85 (bmp . maybe)
86 (gif . maybe)
87 (png . maybe)
88 (xpm . nil)
89 (jpeg . maybe)
90 (tiff . maybe)
91 (svg . maybe)
92 (postscript . nil))
93 "Alist of (IMAGE-TYPE . AUTODETECT) pairs used to auto-detect image files.
94 \(See `image-type-auto-detected-p').
96 AUTODETECT can be
97 - t always auto-detect.
98 - nil never auto-detect.
99 - maybe auto-detect only if the image type is available
100 (see `image-type-available-p').")
102 (defcustom image-load-path
103 (list (file-name-as-directory (expand-file-name "images" data-directory))
104 'data-directory 'load-path)
105 "List of locations in which to search for image files.
106 If an element is a string, it defines a directory to search.
107 If an element is a variable symbol whose value is a string, that
108 value defines a directory to search.
109 If an element is a variable symbol whose value is a list, the
110 value is used as a list of directories to search."
111 :type '(repeat (choice directory variable))
112 :initialize 'custom-initialize-delay)
115 (defun image-load-path-for-library (library image &optional path no-error)
116 "Return a suitable search path for images used by LIBRARY.
118 It searches for IMAGE in `image-load-path' (excluding
119 \"`data-directory'/images\") and `load-path', followed by a path
120 suitable for LIBRARY, which includes \"../../etc/images\" and
121 \"../etc/images\" relative to the library file itself, and then
122 in \"`data-directory'/images\".
124 Then this function returns a list of directories which contains
125 first the directory in which IMAGE was found, followed by the
126 value of `load-path'. If PATH is given, it is used instead of
127 `load-path'.
129 If NO-ERROR is non-nil and a suitable path can't be found, don't
130 signal an error. Instead, return a list of directories as before,
131 except that nil appears in place of the image directory.
133 Here is an example that uses a common idiom to provide
134 compatibility with versions of Emacs that lack the variable
135 `image-load-path':
137 ;; Shush compiler.
138 (defvar image-load-path)
140 (let* ((load-path (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\"))
141 (image-load-path (cons (car load-path)
142 (when (boundp 'image-load-path)
143 image-load-path))))
144 (mh-tool-bar-folder-buttons-init))"
145 (unless library (error "No library specified"))
146 (unless image (error "No image specified"))
147 (let (image-directory image-directory-load-path)
148 ;; Check for images in image-load-path or load-path.
149 (let ((img image)
150 (dir (or
151 ;; Images in image-load-path.
152 (image-search-load-path image)
153 ;; Images in load-path.
154 (locate-library image)))
155 parent)
156 ;; Since the image might be in a nested directory (for
157 ;; example, mail/attach.pbm), adjust `image-directory'
158 ;; accordingly.
159 (when dir
160 (setq dir (file-name-directory dir))
161 (while (setq parent (file-name-directory img))
162 (setq img (directory-file-name parent)
163 dir (expand-file-name "../" dir))))
164 (setq image-directory-load-path dir))
166 ;; If `image-directory-load-path' isn't Emacs's image directory,
167 ;; it's probably a user preference, so use it. Then use a
168 ;; relative setting if possible; otherwise, use
169 ;; `image-directory-load-path'.
170 (cond
171 ;; User-modified image-load-path?
172 ((and image-directory-load-path
173 (not (equal image-directory-load-path
174 (file-name-as-directory
175 (expand-file-name "images" data-directory)))))
176 (setq image-directory image-directory-load-path))
177 ;; Try relative setting.
178 ((let (library-name d1ei d2ei)
179 ;; First, find library in the load-path.
180 (setq library-name (locate-library library))
181 (if (not library-name)
182 (error "Cannot find library %s in load-path" library))
183 ;; And then set image-directory relative to that.
184 (setq
185 ;; Go down 2 levels.
186 d2ei (file-name-as-directory
187 (expand-file-name
188 (concat (file-name-directory library-name) "../../etc/images")))
189 ;; Go down 1 level.
190 d1ei (file-name-as-directory
191 (expand-file-name
192 (concat (file-name-directory library-name) "../etc/images"))))
193 (setq image-directory
194 ;; Set it to nil if image is not found.
195 (cond ((file-exists-p (expand-file-name image d2ei)) d2ei)
196 ((file-exists-p (expand-file-name image d1ei)) d1ei)))))
197 ;; Use Emacs's image directory.
198 (image-directory-load-path
199 (setq image-directory image-directory-load-path))
200 (no-error
201 (message "Could not find image %s for library %s" image library))
203 (error "Could not find image %s for library %s" image library)))
205 ;; Return an augmented `path' or `load-path'.
206 (nconc (list image-directory)
207 (delete image-directory (copy-sequence (or path load-path))))))
210 ;; Used to be in image-type-header-regexps, but now not used anywhere
211 ;; (since 2009-08-28).
212 (defun image-jpeg-p (data)
213 "Value is non-nil if DATA, a string, consists of JFIF image data.
214 We accept the tag Exif because that is the same format."
215 (setq data (ignore-errors (string-to-unibyte data)))
216 (when (and data (string-match-p "\\`\xff\xd8" data))
217 (catch 'jfif
218 (let ((len (length data)) (i 2))
219 (while (< i len)
220 (when (/= (aref data i) #xff)
221 (throw 'jfif nil))
222 (setq i (1+ i))
223 (when (>= (+ i 2) len)
224 (throw 'jfif nil))
225 (let ((nbytes (+ (lsh (aref data (+ i 1)) 8)
226 (aref data (+ i 2))))
227 (code (aref data i)))
228 (when (and (>= code #xe0) (<= code #xef))
229 ;; APP0 LEN1 LEN2 "JFIF\0"
230 (throw 'jfif
231 (string-match-p "JFIF\\|Exif"
232 (substring data i (min (+ i nbytes) len)))))
233 (setq i (+ i 1 nbytes))))))))
236 ;;;###autoload
237 (defun image-type-from-data (data)
238 "Determine the image type from image data DATA.
239 Value is a symbol specifying the image type or nil if type cannot
240 be determined."
241 (let ((types image-type-header-regexps)
242 type)
243 (while types
244 (let ((regexp (car (car types)))
245 (image-type (cdr (car types))))
246 (if (or (and (symbolp image-type)
247 (string-match-p regexp data))
248 (and (consp image-type)
249 (funcall (car image-type) data)
250 (setq image-type (cdr image-type))))
251 (setq type image-type
252 types nil)
253 (setq types (cdr types)))))
254 type))
257 ;;;###autoload
258 (defun image-type-from-buffer ()
259 "Determine the image type from data in the current buffer.
260 Value is a symbol specifying the image type or nil if type cannot
261 be determined."
262 (let ((types image-type-header-regexps)
263 type
264 (opoint (point)))
265 (goto-char (point-min))
266 (while types
267 (let ((regexp (car (car types)))
268 (image-type (cdr (car types)))
269 data)
270 (if (or (and (symbolp image-type)
271 (looking-at-p regexp))
272 (and (consp image-type)
273 (funcall (car image-type)
274 (or data
275 (setq data
276 (buffer-substring
277 (point-min)
278 (min (point-max)
279 (+ (point-min) 256))))))
280 (setq image-type (cdr image-type))))
281 (setq type image-type
282 types nil)
283 (setq types (cdr types)))))
284 (goto-char opoint)
285 type))
288 ;;;###autoload
289 (defun image-type-from-file-header (file)
290 "Determine the type of image file FILE from its first few bytes.
291 Value is a symbol specifying the image type, or nil if type cannot
292 be determined."
293 (unless (or (file-readable-p file)
294 (file-name-absolute-p file))
295 (setq file (image-search-load-path file)))
296 (and file
297 (file-readable-p file)
298 (with-temp-buffer
299 (set-buffer-multibyte nil)
300 (insert-file-contents-literally file nil 0 256)
301 (image-type-from-buffer))))
304 ;;;###autoload
305 (defun image-type-from-file-name (file)
306 "Determine the type of image file FILE from its name.
307 Value is a symbol specifying the image type, or nil if type cannot
308 be determined."
309 (assoc-default file image-type-file-name-regexps 'string-match-p))
312 ;;;###autoload
313 (defun image-type (source &optional type data-p)
314 "Determine and return image type.
315 SOURCE is an image file name or image data.
316 Optional TYPE is a symbol describing the image type. If TYPE is omitted
317 or nil, try to determine the image type from its first few bytes
318 of image data. If that doesn't work, and SOURCE is a file name,
319 use its file extension as image type.
320 Optional DATA-P non-nil means SOURCE is a string containing image data."
321 (when (and (not data-p) (not (stringp source)))
322 (error "Invalid image file name `%s'" source))
323 (unless type
324 (setq type (if data-p
325 (image-type-from-data source)
326 (or (image-type-from-file-header source)
327 (image-type-from-file-name source))))
328 (or type (error "Cannot determine image type")))
329 (or (memq type (and (boundp 'image-types) image-types))
330 (error "Invalid image type `%s'" type))
331 type)
334 (if (fboundp 'image-metadata) ; eg not --without-x
335 (define-obsolete-function-alias 'image-extension-data
336 'image-metadata' "24.1"))
338 (define-obsolete-variable-alias
339 'image-library-alist
340 'dynamic-library-alist "24.1")
342 ;;;###autoload
343 (defun image-type-available-p (type)
344 "Return non-nil if image type TYPE is available.
345 Image types are symbols like `xbm' or `jpeg'."
346 (and (fboundp 'init-image-library)
347 (init-image-library type dynamic-library-alist)))
350 ;;;###autoload
351 (defun image-type-auto-detected-p ()
352 "Return t if the current buffer contains an auto-detectable image.
353 This function is intended to be used from `magic-fallback-mode-alist'.
355 The buffer is considered to contain an auto-detectable image if
356 its beginning matches an image type in `image-type-header-regexps',
357 and that image type is present in `image-type-auto-detectable' with a
358 non-nil value. If that value is non-nil, but not t, then the image type
359 must be available."
360 (let* ((type (image-type-from-buffer))
361 (auto (and type (cdr (assq type image-type-auto-detectable)))))
362 (and auto
363 (or (eq auto t) (image-type-available-p type)))))
366 ;;;###autoload
367 (defun create-image (file-or-data &optional type data-p &rest props)
368 "Create an image.
369 FILE-OR-DATA is an image file name or image data.
370 Optional TYPE is a symbol describing the image type. If TYPE is omitted
371 or nil, try to determine the image type from its first few bytes
372 of image data. If that doesn't work, and FILE-OR-DATA is a file name,
373 use its file extension as image type.
374 Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
375 Optional PROPS are additional image attributes to assign to the image,
376 like, e.g. `:mask MASK'.
377 Value is the image created, or nil if images of type TYPE are not supported.
379 Images should not be larger than specified by `max-image-size'.
381 Image file names that are not absolute are searched for in the
382 \"images\" sub-directory of `data-directory' and
383 `x-bitmap-file-path' (in that order)."
384 ;; It is x_find_image_file in image.c that sets the search path.
385 (setq type (image-type file-or-data type data-p))
386 (when (image-type-available-p type)
387 (append (list 'image :type type (if data-p :data :file) file-or-data)
388 props)))
391 ;;;###autoload
392 (defun put-image (image pos &optional string area)
393 "Put image IMAGE in front of POS in the current buffer.
394 IMAGE must be an image created with `create-image' or `defimage'.
395 IMAGE is displayed by putting an overlay into the current buffer with a
396 `before-string' STRING that has a `display' property whose value is the
397 image. STRING is defaulted if you omit it.
398 The overlay created will have the `put-image' property set to t.
399 POS may be an integer or marker.
400 AREA is where to display the image. AREA nil or omitted means
401 display it in the text area, a value of `left-margin' means
402 display it in the left marginal area, a value of `right-margin'
403 means display it in the right marginal area."
404 (unless string (setq string "x"))
405 (let ((buffer (current-buffer)))
406 (unless (eq (car-safe image) 'image)
407 (error "Not an image: %s" image))
408 (unless (or (null area) (memq area '(left-margin right-margin)))
409 (error "Invalid area %s" area))
410 (setq string (copy-sequence string))
411 (let ((overlay (make-overlay pos pos buffer))
412 (prop (if (null area) image (list (list 'margin area) image))))
413 (put-text-property 0 (length string) 'display prop string)
414 (overlay-put overlay 'put-image t)
415 (overlay-put overlay 'before-string string))))
418 ;;;###autoload
419 (defun insert-image (image &optional string area slice)
420 "Insert IMAGE into current buffer at point.
421 IMAGE is displayed by inserting STRING into the current buffer
422 with a `display' property whose value is the image. STRING
423 defaults to the empty string if you omit it.
424 AREA is where to display the image. AREA nil or omitted means
425 display it in the text area, a value of `left-margin' means
426 display it in the left marginal area, a value of `right-margin'
427 means display it in the right marginal area.
428 SLICE specifies slice of IMAGE to insert. SLICE nil or omitted
429 means insert whole image. SLICE is a list (X Y WIDTH HEIGHT)
430 specifying the X and Y positions and WIDTH and HEIGHT of image area
431 to insert. A float value 0.0 - 1.0 means relative to the width or
432 height of the image; integer values are taken as pixel values."
433 ;; Use a space as least likely to cause trouble when it's a hidden
434 ;; character in the buffer.
435 (unless string (setq string " "))
436 (unless (eq (car-safe image) 'image)
437 (error "Not an image: %s" image))
438 (unless (or (null area) (memq area '(left-margin right-margin)))
439 (error "Invalid area %s" area))
440 (if area
441 (setq image (list (list 'margin area) image))
442 ;; Cons up a new spec equal but not eq to `image' so that
443 ;; inserting it twice in a row (adjacently) displays two copies of
444 ;; the image. Don't try to avoid this by looking at the display
445 ;; properties on either side so that we DTRT more often with
446 ;; cut-and-paste. (Yanking killed image text next to another copy
447 ;; of it loses anyway.)
448 (setq image (cons 'image (cdr image))))
449 (let ((start (point)))
450 (insert string)
451 (add-text-properties start (point)
452 `(display ,(if slice
453 (list (cons 'slice slice) image)
454 image) rear-nonsticky (display)))))
457 ;;;###autoload
458 (defun insert-sliced-image (image &optional string area rows cols)
459 "Insert IMAGE into current buffer at point.
460 IMAGE is displayed by inserting STRING into the current buffer
461 with a `display' property whose value is the image. STRING is
462 defaulted if you omit it.
463 AREA is where to display the image. AREA nil or omitted means
464 display it in the text area, a value of `left-margin' means
465 display it in the left marginal area, a value of `right-margin'
466 means display it in the right marginal area.
467 The image is automatically split into ROWS x COLS slices."
468 (unless string (setq string " "))
469 (unless (eq (car-safe image) 'image)
470 (error "Not an image: %s" image))
471 (unless (or (null area) (memq area '(left-margin right-margin)))
472 (error "Invalid area %s" area))
473 (if area
474 (setq image (list (list 'margin area) image))
475 ;; Cons up a new spec equal but not eq to `image' so that
476 ;; inserting it twice in a row (adjacently) displays two copies of
477 ;; the image. Don't try to avoid this by looking at the display
478 ;; properties on either side so that we DTRT more often with
479 ;; cut-and-paste. (Yanking killed image text next to another copy
480 ;; of it loses anyway.)
481 (setq image (cons 'image (cdr image))))
482 (let ((x 0.0) (dx (/ 1.0001 (or cols 1)))
483 (y 0.0) (dy (/ 1.0001 (or rows 1))))
484 (while (< y 1.0)
485 (while (< x 1.0)
486 (let ((start (point)))
487 (insert string)
488 (add-text-properties start (point)
489 `(display ,(list (list 'slice x y dx dy) image)
490 rear-nonsticky (display)))
491 (setq x (+ x dx))))
492 (setq x 0.0
493 y (+ y dy))
494 (insert (propertize "\n" 'line-height t)))))
498 ;;;###autoload
499 (defun remove-images (start end &optional buffer)
500 "Remove images between START and END in BUFFER.
501 Remove only images that were put in BUFFER with calls to `put-image'.
502 BUFFER nil or omitted means use the current buffer."
503 (unless buffer
504 (setq buffer (current-buffer)))
505 (let ((overlays (overlays-in start end)))
506 (while overlays
507 (let ((overlay (car overlays)))
508 (when (overlay-get overlay 'put-image)
509 (delete-overlay overlay)))
510 (setq overlays (cdr overlays)))))
512 (defun image-search-load-path (file &optional path)
513 (unless path
514 (setq path image-load-path))
515 (let (element found filename)
516 (while (and (not found) (consp path))
517 (setq element (car path))
518 (cond
519 ((stringp element)
520 (setq found
521 (file-readable-p
522 (setq filename (expand-file-name file element)))))
523 ((and (symbolp element) (boundp element))
524 (setq element (symbol-value element))
525 (cond
526 ((stringp element)
527 (setq found
528 (file-readable-p
529 (setq filename (expand-file-name file element)))))
530 ((consp element)
531 (if (setq filename (image-search-load-path file element))
532 (setq found t))))))
533 (setq path (cdr path)))
534 (if found filename)))
536 ;;;###autoload
537 (defun find-image (specs)
538 "Find an image, choosing one of a list of image specifications.
540 SPECS is a list of image specifications.
542 Each image specification in SPECS is a property list. The contents of
543 a specification are image type dependent. All specifications must at
544 least contain the properties `:type TYPE' and either `:file FILE' or
545 `:data DATA', where TYPE is a symbol specifying the image type,
546 e.g. `xbm', FILE is the file to load the image from, and DATA is a
547 string containing the actual image data. The specification whose TYPE
548 is supported, and FILE exists, is used to construct the image
549 specification to be returned. Return nil if no specification is
550 satisfied.
552 The image is looked for in `image-load-path'.
554 Image files should not be larger than specified by `max-image-size'."
555 (let (image)
556 (while (and specs (null image))
557 (let* ((spec (car specs))
558 (type (plist-get spec :type))
559 (data (plist-get spec :data))
560 (file (plist-get spec :file))
561 found)
562 (when (image-type-available-p type)
563 (cond ((stringp file)
564 (if (setq found (image-search-load-path file))
565 (setq image
566 (cons 'image (plist-put (copy-sequence spec)
567 :file found)))))
568 ((not (null data))
569 (setq image (cons 'image spec)))))
570 (setq specs (cdr specs))))
571 image))
574 ;;;###autoload
575 (defmacro defimage (symbol specs &optional doc)
576 "Define SYMBOL as an image.
578 SPECS is a list of image specifications. DOC is an optional
579 documentation string.
581 Each image specification in SPECS is a property list. The contents of
582 a specification are image type dependent. All specifications must at
583 least contain the properties `:type TYPE' and either `:file FILE' or
584 `:data DATA', where TYPE is a symbol specifying the image type,
585 e.g. `xbm', FILE is the file to load the image from, and DATA is a
586 string containing the actual image data. The first image
587 specification whose TYPE is supported, and FILE exists, is used to
588 define SYMBOL.
590 Example:
592 (defimage test-image ((:type xpm :file \"~/test1.xpm\")
593 (:type xbm :file \"~/test1.xbm\")))"
594 (declare (doc-string 3))
595 `(defvar ,symbol (find-image ',specs) ,doc))
598 ;;; Animated image API
600 (defconst image-animated-types '(gif)
601 "List of supported animated image types.")
603 (defun image-animated-p (image)
604 "Return non-nil if IMAGE can be animated.
605 To be capable of being animated, an image must be of a type
606 listed in `image-animated-types', and contain more than one
607 sub-image, with a specified animation delay. The actual return
608 value is a cons (NIMAGES . DELAY), where NIMAGES is the number
609 of sub-images in the animated image and DELAY is the delay in
610 seconds until the next sub-image should be displayed."
611 (cond
612 ((memq (plist-get (cdr image) :type) image-animated-types)
613 (let* ((metadata (image-metadata image))
614 (images (plist-get metadata 'count))
615 (delay (plist-get metadata 'delay)))
616 (when (and images (> images 1) (numberp delay))
617 (if (< delay 0) (setq delay 0.1))
618 (cons images delay))))))
620 ;; "Destructively"?
621 (defun image-animate (image &optional index limit)
622 "Start animating IMAGE.
623 Animation occurs by destructively altering the IMAGE spec list.
625 With optional INDEX, begin animating from that animation frame.
626 LIMIT specifies how long to animate the image. If omitted or
627 nil, play the animation until the end. If t, loop forever. If a
628 number, play until that number of seconds has elapsed."
629 (let ((animation (image-animated-p image))
630 timer)
631 (when animation
632 (if (setq timer (image-animate-timer image))
633 (cancel-timer timer))
634 (run-with-timer 0.2 nil 'image-animate-timeout
635 image (or index 0) (car animation)
636 0 limit))))
638 (defun image-animate-timer (image)
639 "Return the animation timer for image IMAGE."
640 ;; See cancel-function-timers
641 (let ((tail timer-list) timer)
642 (while tail
643 (setq timer (car tail)
644 tail (cdr tail))
645 (if (and (eq (aref timer 5) 'image-animate-timeout)
646 (eq (car-safe (aref timer 6)) image))
647 (setq tail nil)
648 (setq timer nil)))
649 timer))
651 ;; FIXME? The delay may not be the same for different sub-images,
652 ;; hence we need to call image-animated-p to return it.
653 ;; But it also returns count, so why do we bother passing that as an
654 ;; argument?
655 (defun image-animate-timeout (image n count time-elapsed limit)
656 "Display animation frame N of IMAGE.
657 N=0 refers to the initial animation frame.
658 COUNT is the total number of frames in the animation.
659 TIME-ELAPSED is the total time that has elapsed since
660 `image-animate-start' was called.
661 LIMIT determines when to stop. If t, loop forever. If nil, stop
662 after displaying the last animation frame. Otherwise, stop
663 after LIMIT seconds have elapsed.
664 The minimum delay between successive frames is 0.01s."
665 (plist-put (cdr image) :index n)
666 (force-window-update)
667 (setq n (1+ n))
668 (let* ((time (float-time))
669 (animation (image-animated-p image))
670 ;; Subtract off the time we took to load the image from the
671 ;; stated delay time.
672 (delay (max (+ (cdr animation) time (- (float-time)))
673 0.01))
674 done)
675 (if (>= n count)
676 (if limit
677 (setq n 0)
678 (setq done t)))
679 (setq time-elapsed (+ delay time-elapsed))
680 (if (numberp limit)
681 (setq done (>= time-elapsed limit)))
682 (unless done
683 (run-with-timer delay nil 'image-animate-timeout
684 image n count time-elapsed limit))))
687 (defcustom imagemagick-types-inhibit
688 '(C HTML HTM TXT PDF)
689 "ImageMagick types that should not be visited in Image mode.
690 This should be a list of symbols, each of which should be one of
691 the ImageMagick types listed in `imagemagick-types'. These image
692 types are not registered by `imagemagick-register-types'.
694 If Emacs is compiled without ImageMagick support, this variable
695 has no effect."
696 :type '(choice (const :tag "Let ImageMagick handle all types it can" nil)
697 (repeat symbol))
698 ;; Ideally, would have a :set function that checks if we already did
699 ;; imagemagick-register-types, and if so undoes it, then redoes it.
700 :version "24.1"
701 :group 'image)
703 ;;;###autoload
704 (defun imagemagick-register-types ()
705 "Register file types that can be handled by ImageMagick.
706 This registers the ImageMagick types listed in `imagemagick-types',
707 excluding those listed in `imagemagick-types-inhibit'.
709 Registered image types are added to `auto-mode-alist', so that
710 Emacs visits them in Image mode. They are also added to
711 `image-type-file-name-regexps', so that the `image-type' function
712 recognizes these files as having image type `imagemagick'.
714 If Emacs is compiled without ImageMagick support, do nothing."
715 (when (fboundp 'imagemagick-types)
716 (let ((im-types '()))
717 (dolist (im-type (imagemagick-types))
718 (unless (memq im-type imagemagick-types-inhibit)
719 (push (downcase (symbol-name im-type)) im-types)))
720 (let ((extension (concat "\\." (regexp-opt im-types) "\\'")))
721 (push (cons extension 'image-mode) auto-mode-alist)
722 (push (cons extension 'imagemagick)
723 image-type-file-name-regexps)))))
725 (provide 'image)
727 ;;; image.el ends here