Commit | Line | Data |
---|---|---|
7840ced1 GM |
1 | ;;; image.el --- image API |
2 | ||
0d30b337 | 3 | ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, |
d7a0267c | 4 | ;; 2004, 2005, 2006, 2007 Free Software Foundation, Inc. |
634f4a67 PJ |
5 | |
6 | ;; Maintainer: FSF | |
04799cf5 | 7 | ;; Keywords: multimedia |
7840ced1 GM |
8 | |
9 | ;; This file is part of GNU Emacs. | |
10 | ||
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 | |
b4aa6026 | 13 | ;; the Free Software Foundation; either version 3, or (at your option) |
7840ced1 GM |
14 | ;; any later version. |
15 | ||
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. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
086add15 LK |
23 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 | ;; Boston, MA 02110-1301, USA. | |
7840ced1 GM |
25 | |
26 | ;;; Commentary: | |
27 | ||
28 | ;;; Code: | |
29 | ||
77f6105c MB |
30 | |
31 | (defgroup image () | |
32 | "Image support." | |
33 | :group 'multimedia) | |
34 | ||
35 | ||
4fde92ef | 36 | (defconst image-type-header-regexps |
6642c904 JB |
37 | '(("\\`/[\t\n\r ]*\\*.*XPM.\\*/" . xpm) |
38 | ("\\`P[1-6][[:space:]]+\\(?:#.*[[:space:]]+\\)*[0-9]+[[:space:]]+[0-9]+" . pbm) | |
3205431d | 39 | ("\\`GIF8[79]a" . gif) |
6642c904 | 40 | ("\\`\x89PNG\r\n\x1a\n" . png) |
2dca7106 RS |
41 | ("\\`[\t\n\r ]*#define \\([a-z0-9]+\\)_width [0-9]+\n\ |
42 | #define \\1_height [0-9]+\n\ | |
43 | static char \\1_bits" . xbm) | |
6642c904 JB |
44 | ("\\`\\(?:MM\0\\*\\|II\\*\0\\)" . tiff) |
45 | ("\\`[\t\n\r ]*%!PS" . postscript) | |
9c818156 GM |
46 | ("\\`\xff\xd8" . (image-jpeg-p . jpeg)) |
47 | ("\\`<\\?xml " . svg)) | |
6642c904 | 48 | "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types. |
7840ced1 | 49 | When the first bytes of an image file match REGEXP, it is assumed to |
6642c904 JB |
50 | be of image type IMAGE-TYPE if IMAGE-TYPE is a symbol. If not a symbol, |
51 | IMAGE-TYPE must be a pair (PREDICATE . TYPE). PREDICATE is called | |
52 | with one argument, a string containing the image data. If PREDICATE returns | |
53 | a non-nil value, TYPE is the image's type.") | |
8a8ef149 | 54 | |
4fde92ef KS |
55 | (defconst image-type-file-name-regexps |
56 | '(("\\.png\\'" . png) | |
57 | ("\\.gif\\'" . gif) | |
58 | ("\\.jpe?g\\'" . jpeg) | |
59 | ("\\.bmp\\'" . bmp) | |
60 | ("\\.xpm\\'" . xpm) | |
61 | ("\\.pbm\\'" . pbm) | |
62 | ("\\.xbm\\'" . xbm) | |
63 | ("\\.ps\\'" . postscript) | |
64 | ("\\.tiff?\\'" . tiff)) | |
65 | "Alist of (REGEXP . IMAGE-TYPE) pairs used to identify image files. | |
66 | When the name of an image file match REGEXP, it is assumed to | |
67 | be of image type IMAGE-TYPE.") | |
68 | ||
a9bb04bb CY |
69 | ;; We rely on `auto-mode-alist' to detect xbm and xpm files, instead |
70 | ;; of content autodetection. Their contents are just C code, so it is | |
71 | ;; easy to generate false matches. | |
6642c904 JB |
72 | (defvar image-type-auto-detectable |
73 | '((pbm . t) | |
a9bb04bb | 74 | (xbm . nil) |
6642c904 JB |
75 | (bmp . maybe) |
76 | (gif . maybe) | |
77 | (png . maybe) | |
a9bb04bb | 78 | (xpm . nil) |
6642c904 JB |
79 | (jpeg . maybe) |
80 | (tiff . maybe) | |
81 | (postscript . nil)) | |
82 | "Alist of (IMAGE-TYPE . AUTODETECT) pairs used to auto-detect image files. | |
83 | \(See `image-type-auto-detected-p'). | |
84 | ||
85 | AUTODETECT can be | |
86 | - t always auto-detect. | |
87 | - nil never auto-detect. | |
88 | - maybe auto-detect only if the image type is available | |
89 | (see `image-type-available-p').") | |
4fde92ef | 90 | |
dada660a | 91 | (defvar image-load-path nil |
5fc5ac38 | 92 | "List of locations in which to search for image files. |
93a75651 CY |
93 | If an element is a string, it defines a directory to search. |
94 | If an element is a variable symbol whose value is a string, that | |
95 | value defines a directory to search. | |
96 | If an element is a variable symbol whose value is a list, the | |
97 | value is used as a list of directories to search.") | |
5fc5ac38 | 98 | |
dada660a RS |
99 | (eval-at-startup |
100 | (setq image-load-path | |
101 | (list (file-name-as-directory (expand-file-name "images" data-directory)) | |
102 | 'data-directory 'load-path))) | |
103 | ||
5248a565 | 104 | |
20c65d08 | 105 | (defun image-load-path-for-library (library image &optional path no-error) |
ae77c7ff | 106 | "Return a suitable search path for images used by LIBRARY. |
7c565097 | 107 | |
40db64d2 | 108 | It searches for IMAGE in `image-load-path' (excluding |
c0696e1b BW |
109 | \"`data-directory'/images\") and `load-path', followed by a path |
110 | suitable for LIBRARY, which includes \"../../etc/images\" and | |
111 | \"../etc/images\" relative to the library file itself, and then | |
112 | in \"`data-directory'/images\". | |
7c565097 | 113 | |
5248a565 BW |
114 | Then this function returns a list of directories which contains |
115 | first the directory in which IMAGE was found, followed by the | |
116 | value of `load-path'. If PATH is given, it is used instead of | |
117 | `load-path'. | |
20c65d08 | 118 | |
5248a565 BW |
119 | If NO-ERROR is non-nil and a suitable path can't be found, don't |
120 | signal an error. Instead, return a list of directories as before, | |
121 | except that nil appears in place of the image directory. | |
7c565097 BW |
122 | |
123 | Here is an example that uses a common idiom to provide | |
124 | compatibility with versions of Emacs that lack the variable | |
125 | `image-load-path': | |
126 | ||
9f036d33 BW |
127 | ;; Shush compiler. |
128 | (defvar image-load-path) | |
5248a565 BW |
129 | |
130 | (let* ((load-path (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\")) | |
9f036d33 BW |
131 | (image-load-path (cons (car load-path) |
132 | (when (boundp 'image-load-path) | |
133 | image-load-path)))) | |
5248a565 | 134 | (mh-tool-bar-folder-buttons-init))" |
7c565097 BW |
135 | (unless library (error "No library specified")) |
136 | (unless image (error "No image specified")) | |
c0696e1b BW |
137 | (let (image-directory image-directory-load-path) |
138 | ;; Check for images in image-load-path or load-path. | |
139 | (let ((img image) | |
140 | (dir (or | |
141 | ;; Images in image-load-path. | |
142 | (image-search-load-path image) | |
143 | ;; Images in load-path. | |
144 | (locate-library image))) | |
145 | parent) | |
146 | ;; Since the image might be in a nested directory (for | |
147 | ;; example, mail/attach.pbm), adjust `image-directory' | |
148 | ;; accordingly. | |
149 | (when dir | |
150 | (setq dir (file-name-directory dir)) | |
151 | (while (setq parent (file-name-directory img)) | |
152 | (setq img (directory-file-name parent) | |
153 | dir (expand-file-name "../" dir)))) | |
154 | (setq image-directory-load-path dir)) | |
155 | ||
156 | ;; If `image-directory-load-path' isn't Emacs' image directory, | |
157 | ;; it's probably a user preference, so use it. Then use a | |
158 | ;; relative setting if possible; otherwise, use | |
159 | ;; `image-directory-load-path'. | |
7c565097 | 160 | (cond |
c0696e1b BW |
161 | ;; User-modified image-load-path? |
162 | ((and image-directory-load-path | |
163 | (not (equal image-directory-load-path | |
164 | (file-name-as-directory | |
165 | (expand-file-name "images" data-directory))))) | |
166 | (setq image-directory image-directory-load-path)) | |
7c565097 BW |
167 | ;; Try relative setting. |
168 | ((let (library-name d1ei d2ei) | |
169 | ;; First, find library in the load-path. | |
170 | (setq library-name (locate-library library)) | |
171 | (if (not library-name) | |
172 | (error "Cannot find library %s in load-path" library)) | |
173 | ;; And then set image-directory relative to that. | |
174 | (setq | |
175 | ;; Go down 2 levels. | |
c0696e1b BW |
176 | d2ei (file-name-as-directory |
177 | (expand-file-name | |
178 | (concat (file-name-directory library-name) "../../etc/images"))) | |
7c565097 | 179 | ;; Go down 1 level. |
c0696e1b BW |
180 | d1ei (file-name-as-directory |
181 | (expand-file-name | |
182 | (concat (file-name-directory library-name) "../etc/images")))) | |
7c565097 BW |
183 | (setq image-directory |
184 | ;; Set it to nil if image is not found. | |
185 | (cond ((file-exists-p (expand-file-name image d2ei)) d2ei) | |
186 | ((file-exists-p (expand-file-name image d1ei)) d1ei))))) | |
c0696e1b BW |
187 | ;; Use Emacs' image directory. |
188 | (image-directory-load-path | |
189 | (setq image-directory image-directory-load-path)) | |
20c65d08 | 190 | (no-error |
20c65d08 | 191 | (message "Could not find image %s for library %s" image library)) |
7c565097 BW |
192 | (t |
193 | (error "Could not find image %s for library %s" image library))) | |
194 | ||
5248a565 BW |
195 | ;; Return an augmented `path' or `load-path'. |
196 | (nconc (list image-directory) | |
197 | (delete image-directory (copy-sequence (or path load-path)))))) | |
198 | ||
7c565097 | 199 | |
8a8ef149 | 200 | (defun image-jpeg-p (data) |
9b78a6a3 RS |
201 | "Value is non-nil if DATA, a string, consists of JFIF image data. |
202 | We accept the tag Exif because that is the same format." | |
8a8ef149 GM |
203 | (when (string-match "\\`\xff\xd8" data) |
204 | (catch 'jfif | |
205 | (let ((len (length data)) (i 2)) | |
206 | (while (< i len) | |
207 | (when (/= (aref data i) #xff) | |
208 | (throw 'jfif nil)) | |
209 | (setq i (1+ i)) | |
210 | (when (>= (+ i 2) len) | |
211 | (throw 'jfif nil)) | |
212 | (let ((nbytes (+ (lsh (aref data (+ i 1)) 8) | |
4a9bf8a4 GM |
213 | (aref data (+ i 2)))) |
214 | (code (aref data i))) | |
215 | (when (and (>= code #xe0) (<= code #xef)) | |
8a8ef149 | 216 | ;; APP0 LEN1 LEN2 "JFIF\0" |
71296446 | 217 | (throw 'jfif |
32b3c766 JPW |
218 | (string-match "JFIF\\|Exif" |
219 | (substring data i (min (+ i nbytes) len))))) | |
3bdbead3 | 220 | (setq i (+ i 1 nbytes)))))))) |
7840ced1 GM |
221 | |
222 | ||
223 | ;;;###autoload | |
162dec01 GM |
224 | (defun image-type-from-data (data) |
225 | "Determine the image type from image data DATA. | |
226 | Value is a symbol specifying the image type or nil if type cannot | |
7840ced1 | 227 | be determined." |
4fde92ef | 228 | (let ((types image-type-header-regexps) |
7840ced1 | 229 | type) |
4fde92ef | 230 | (while types |
7840ced1 | 231 | (let ((regexp (car (car types))) |
6642c904 | 232 | (image-type (cdr (car types)))) |
4fde92ef KS |
233 | (if (or (and (symbolp image-type) |
234 | (string-match regexp data)) | |
235 | (and (consp image-type) | |
236 | (funcall (car image-type) data) | |
237 | (setq image-type (cdr image-type)))) | |
238 | (setq type image-type | |
239 | types nil) | |
240 | (setq types (cdr types))))) | |
241 | type)) | |
242 | ||
243 | ||
244 | ;;;###autoload | |
6642c904 | 245 | (defun image-type-from-buffer () |
4fde92ef | 246 | "Determine the image type from data in the current buffer. |
6642c904 JB |
247 | Value is a symbol specifying the image type or nil if type cannot |
248 | be determined." | |
4fde92ef KS |
249 | (let ((types image-type-header-regexps) |
250 | type | |
251 | (opoint (point))) | |
252 | (goto-char (point-min)) | |
253 | (while types | |
254 | (let ((regexp (car (car types))) | |
6642c904 | 255 | (image-type (cdr (car types))) |
4fde92ef KS |
256 | data) |
257 | (if (or (and (symbolp image-type) | |
258 | (looking-at regexp)) | |
259 | (and (consp image-type) | |
260 | (funcall (car image-type) | |
261 | (or data | |
262 | (setq data | |
263 | (buffer-substring | |
264 | (point-min) | |
265 | (min (point-max) | |
266 | (+ (point-min) 256)))))) | |
267 | (setq image-type (cdr image-type)))) | |
6642c904 | 268 | (setq type image-type |
4fde92ef KS |
269 | types nil) |
270 | (setq types (cdr types))))) | |
271 | (goto-char opoint) | |
7840ced1 GM |
272 | type)) |
273 | ||
274 | ||
162dec01 GM |
275 | ;;;###autoload |
276 | (defun image-type-from-file-header (file) | |
277 | "Determine the type of image file FILE from its first few bytes. | |
278 | Value is a symbol specifying the image type, or nil if type cannot | |
279 | be determined." | |
4fde92ef KS |
280 | (unless (or (file-readable-p file) |
281 | (file-name-absolute-p file)) | |
282 | (setq file (image-search-load-path file))) | |
283 | (and file | |
284 | (file-readable-p file) | |
285 | (with-temp-buffer | |
286 | (set-buffer-multibyte nil) | |
287 | (insert-file-contents-literally file nil 0 256) | |
6642c904 | 288 | (image-type-from-buffer)))) |
4fde92ef KS |
289 | |
290 | ||
291 | ;;;###autoload | |
292 | (defun image-type-from-file-name (file) | |
293 | "Determine the type of image file FILE from its name. | |
294 | Value is a symbol specifying the image type, or nil if type cannot | |
295 | be determined." | |
7d88e015 | 296 | (assoc-default file image-type-file-name-regexps 'string-match)) |
162dec01 GM |
297 | |
298 | ||
7840ced1 | 299 | ;;;###autoload |
7d88e015 | 300 | (defun image-type (source &optional type data-p) |
e7968373 | 301 | "Determine and return image type. |
7d88e015 | 302 | SOURCE is an image file name or image data. |
7840ced1 | 303 | Optional TYPE is a symbol describing the image type. If TYPE is omitted |
162dec01 | 304 | or nil, try to determine the image type from its first few bytes |
7d88e015 | 305 | of image data. If that doesn't work, and SOURCE is a file name, |
9ea8de1b | 306 | use its file extension as image type. |
7d88e015 CY |
307 | Optional DATA-P non-nil means SOURCE is a string containing image data." |
308 | (when (and (not data-p) (not (stringp source))) | |
309 | (error "Invalid image file name `%s'" source)) | |
162dec01 | 310 | (unless type |
7d88e015 CY |
311 | (setq type (if data-p |
312 | (image-type-from-data source) | |
313 | (or (image-type-from-file-header source) | |
314 | (image-type-from-file-name source)))) | |
315 | (or type (error "Cannot determine image type"))) | |
316 | (or (memq type image-types) | |
317 | (error "Invalid image type `%s'" type)) | |
e7968373 KS |
318 | type) |
319 | ||
6642c904 | 320 | |
e7968373 KS |
321 | ;;;###autoload |
322 | (defun image-type-available-p (type) | |
323 | "Return non-nil if image type TYPE is available. | |
324 | Image types are symbols like `xbm' or `jpeg'." | |
325 | (and (fboundp 'init-image-library) | |
326 | (init-image-library type image-library-alist))) | |
327 | ||
328 | ||
6642c904 JB |
329 | ;;;###autoload |
330 | (defun image-type-auto-detected-p () | |
4837b516 | 331 | "Return t if the current buffer contains an auto-detectable image. |
7d88e015 CY |
332 | This function is intended to be used from `magic-fallback-mode-alist'. |
333 | ||
334 | The buffer is considered to contain an auto-detectable image if | |
335 | its beginning matches an image type in `image-type-header-regexps', | |
336 | and that image type is present in `image-type-auto-detectable'." | |
6642c904 JB |
337 | (let* ((type (image-type-from-buffer)) |
338 | (auto (and type (cdr (assq type image-type-auto-detectable))))) | |
7d88e015 CY |
339 | (and type |
340 | (or (eq auto t) (image-type-available-p type))))) | |
6642c904 JB |
341 | |
342 | ||
e7968373 KS |
343 | ;;;###autoload |
344 | (defun create-image (file-or-data &optional type data-p &rest props) | |
345 | "Create an image. | |
346 | FILE-OR-DATA is an image file name or image data. | |
347 | Optional TYPE is a symbol describing the image type. If TYPE is omitted | |
348 | or nil, try to determine the image type from its first few bytes | |
349 | of image data. If that doesn't work, and FILE-OR-DATA is a file name, | |
350 | use its file extension as image type. | |
351 | Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data. | |
352 | Optional PROPS are additional image attributes to assign to the image, | |
353 | like, e.g. `:mask MASK'. | |
354 | Value is the image created, or nil if images of type TYPE are not supported. | |
355 | ||
2467875c GM |
356 | Images should not be larger than specified by `max-image-size'. |
357 | ||
358 | Image file names that are not absolute are searched for in the | |
359 | \"images\" sub-directory of `data-directory' and | |
360 | `x-bitmap-file-path' (in that order)." | |
361 | ;; It is x_find_image_file in image.c that sets the search path. | |
e7968373 | 362 | (setq type (image-type file-or-data type data-p)) |
7840ced1 | 363 | (when (image-type-available-p type) |
162dec01 GM |
364 | (append (list 'image :type type (if data-p :data :file) file-or-data) |
365 | props))) | |
7840ced1 GM |
366 | |
367 | ||
368 | ;;;###autoload | |
cedbd8d0 | 369 | (defun put-image (image pos &optional string area) |
1571b5ff | 370 | "Put image IMAGE in front of POS in the current buffer. |
7840ced1 | 371 | IMAGE must be an image created with `create-image' or `defimage'. |
46655c94 GM |
372 | IMAGE is displayed by putting an overlay into the current buffer with a |
373 | `before-string' STRING that has a `display' property whose value is the | |
cedbd8d0 | 374 | image. STRING is defaulted if you omit it. |
7840ced1 | 375 | POS may be an integer or marker. |
7840ced1 GM |
376 | AREA is where to display the image. AREA nil or omitted means |
377 | display it in the text area, a value of `left-margin' means | |
378 | display it in the left marginal area, a value of `right-margin' | |
46655c94 | 379 | means display it in the right marginal area." |
0ad550ba | 380 | (unless string (setq string "x")) |
1571b5ff | 381 | (let ((buffer (current-buffer))) |
cedbd8d0 | 382 | (unless (eq (car-safe image) 'image) |
1571b5ff GM |
383 | (error "Not an image: %s" image)) |
384 | (unless (or (null area) (memq area '(left-margin right-margin))) | |
385 | (error "Invalid area %s" area)) | |
46655c94 | 386 | (setq string (copy-sequence string)) |
1571b5ff | 387 | (let ((overlay (make-overlay pos pos buffer)) |
46655c94 GM |
388 | (prop (if (null area) image (list (list 'margin area) image)))) |
389 | (put-text-property 0 (length string) 'display prop string) | |
1571b5ff GM |
390 | (overlay-put overlay 'put-image t) |
391 | (overlay-put overlay 'before-string string)))) | |
7840ced1 GM |
392 | |
393 | ||
394 | ;;;###autoload | |
5af275e0 | 395 | (defun insert-image (image &optional string area slice) |
7840ced1 | 396 | "Insert IMAGE into current buffer at point. |
46655c94 | 397 | IMAGE is displayed by inserting STRING into the current buffer |
cedbd8d0 DL |
398 | with a `display' property whose value is the image. STRING is |
399 | defaulted if you omit it. | |
7840ced1 GM |
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' | |
5af275e0 KS |
403 | means display it in the right marginal area. |
404 | SLICE specifies slice of IMAGE to insert. SLICE nil or omitted | |
405 | means insert whole image. SLICE is a list (X Y WIDTH HEIGHT) | |
406 | specifying the X and Y positions and WIDTH and HEIGHT of image area | |
407 | to insert. A float value 0.0 - 1.0 means relative to the width or | |
408 | height of the image; integer values are taken as pixel values." | |
0ad550ba DL |
409 | ;; Use a space as least likely to cause trouble when it's a hidden |
410 | ;; character in the buffer. | |
411 | (unless string (setq string " ")) | |
cedbd8d0 | 412 | (unless (eq (car-safe image) 'image) |
7840ced1 GM |
413 | (error "Not an image: %s" image)) |
414 | (unless (or (null area) (memq area '(left-margin right-margin))) | |
415 | (error "Invalid area %s" area)) | |
0dc91c57 DL |
416 | (if area |
417 | (setq image (list (list 'margin area) image)) | |
418 | ;; Cons up a new spec equal but not eq to `image' so that | |
419 | ;; inserting it twice in a row (adjacently) displays two copies of | |
420 | ;; the image. Don't try to avoid this by looking at the display | |
421 | ;; properties on either side so that we DTRT more often with | |
422 | ;; cut-and-paste. (Yanking killed image text next to another copy | |
423 | ;; of it loses anyway.) | |
424 | (setq image (cons 'image (cdr image)))) | |
46655c94 GM |
425 | (let ((start (point))) |
426 | (insert string) | |
427 | (add-text-properties start (point) | |
5af275e0 KS |
428 | `(display ,(if slice |
429 | (list (cons 'slice slice) image) | |
430 | image) rear-nonsticky (display))))) | |
431 | ||
432 | ||
f22c36d3 | 433 | ;;;###autoload |
5af275e0 | 434 | (defun insert-sliced-image (image &optional string area rows cols) |
b48bc937 KS |
435 | "Insert IMAGE into current buffer at point. |
436 | IMAGE is displayed by inserting STRING into the current buffer | |
437 | with a `display' property whose value is the image. STRING is | |
438 | defaulted if you omit it. | |
439 | AREA is where to display the image. AREA nil or omitted means | |
440 | display it in the text area, a value of `left-margin' means | |
441 | display it in the left marginal area, a value of `right-margin' | |
442 | means display it in the right marginal area. | |
443 | The image is automatically split into ROW x COLS slices." | |
5af275e0 KS |
444 | (unless string (setq string " ")) |
445 | (unless (eq (car-safe image) 'image) | |
446 | (error "Not an image: %s" image)) | |
447 | (unless (or (null area) (memq area '(left-margin right-margin))) | |
448 | (error "Invalid area %s" area)) | |
449 | (if area | |
450 | (setq image (list (list 'margin area) image)) | |
451 | ;; Cons up a new spec equal but not eq to `image' so that | |
452 | ;; inserting it twice in a row (adjacently) displays two copies of | |
453 | ;; the image. Don't try to avoid this by looking at the display | |
454 | ;; properties on either side so that we DTRT more often with | |
455 | ;; cut-and-paste. (Yanking killed image text next to another copy | |
456 | ;; of it loses anyway.) | |
457 | (setq image (cons 'image (cdr image)))) | |
458 | (let ((x 0.0) (dx (/ 1.0001 (or cols 1))) | |
459 | (y 0.0) (dy (/ 1.0001 (or rows 1)))) | |
460 | (while (< y 1.0) | |
461 | (while (< x 1.0) | |
462 | (let ((start (point))) | |
463 | (insert string) | |
464 | (add-text-properties start (point) | |
465 | `(display ,(list (list 'slice x y dx dy) image) | |
466 | rear-nonsticky (display))) | |
467 | (setq x (+ x dx)))) | |
468 | (setq x 0.0 | |
469 | y (+ y dy)) | |
82dc617b | 470 | (insert (propertize "\n" 'line-height t))))) |
5af275e0 | 471 | |
10e2102e | 472 | |
7840ced1 GM |
473 | |
474 | ;;;###autoload | |
475 | (defun remove-images (start end &optional buffer) | |
476 | "Remove images between START and END in BUFFER. | |
477 | Remove only images that were put in BUFFER with calls to `put-image'. | |
478 | BUFFER nil or omitted means use the current buffer." | |
479 | (unless buffer | |
480 | (setq buffer (current-buffer))) | |
481 | (let ((overlays (overlays-in start end))) | |
482 | (while overlays | |
483 | (let ((overlay (car overlays))) | |
484 | (when (overlay-get overlay 'put-image) | |
0c898dd9 DL |
485 | (delete-overlay overlay))) |
486 | (setq overlays (cdr overlays))))) | |
7840ced1 | 487 | |
4fde92ef KS |
488 | (defun image-search-load-path (file &optional path) |
489 | (unless path | |
490 | (setq path image-load-path)) | |
491 | (let (element found filename) | |
5fc5ac38 | 492 | (while (and (not found) (consp path)) |
93a75651 | 493 | (setq element (car path)) |
5fc5ac38 | 494 | (cond |
93a75651 | 495 | ((stringp element) |
5fc5ac38 CY |
496 | (setq found |
497 | (file-readable-p | |
4fde92ef | 498 | (setq filename (expand-file-name file element))))) |
93a75651 CY |
499 | ((and (symbolp element) (boundp element)) |
500 | (setq element (symbol-value element)) | |
501 | (cond | |
502 | ((stringp element) | |
503 | (setq found | |
504 | (file-readable-p | |
4fde92ef | 505 | (setq filename (expand-file-name file element))))) |
93a75651 | 506 | ((consp element) |
4fde92ef | 507 | (if (setq filename (image-search-load-path file element)) |
93a75651 | 508 | (setq found t)))))) |
5fc5ac38 | 509 | (setq path (cdr path))) |
4fde92ef | 510 | (if found filename))) |
7840ced1 GM |
511 | |
512 | ;;;###autoload | |
349c034d GM |
513 | (defun find-image (specs) |
514 | "Find an image, choosing one of a list of image specifications. | |
7840ced1 | 515 | |
cedbd8d0 | 516 | SPECS is a list of image specifications. |
7840ced1 GM |
517 | |
518 | Each image specification in SPECS is a property list. The contents of | |
519 | a specification are image type dependent. All specifications must at | |
bc283707 WP |
520 | least contain the properties `:type TYPE' and either `:file FILE' or |
521 | `:data DATA', where TYPE is a symbol specifying the image type, | |
522 | e.g. `xbm', FILE is the file to load the image from, and DATA is a | |
cedbd8d0 DL |
523 | string containing the actual image data. The specification whose TYPE |
524 | is supported, and FILE exists, is used to construct the image | |
525 | specification to be returned. Return nil if no specification is | |
526 | satisfied. | |
527 | ||
8646a62e CY |
528 | The image is looked for in `image-load-path'. |
529 | ||
530 | Image files should not be larger than specified by `max-image-size'." | |
7840ced1 GM |
531 | (let (image) |
532 | (while (and specs (null image)) | |
533 | (let* ((spec (car specs)) | |
534 | (type (plist-get spec :type)) | |
162dec01 | 535 | (data (plist-get spec :data)) |
7f228379 GM |
536 | (file (plist-get spec :file)) |
537 | found) | |
162dec01 GM |
538 | (when (image-type-available-p type) |
539 | (cond ((stringp file) | |
4fde92ef | 540 | (if (setq found (image-search-load-path file)) |
5fc5ac38 CY |
541 | (setq image |
542 | (cons 'image (plist-put (copy-sequence spec) | |
543 | :file found))))) | |
f64ae86c | 544 | ((not (null data)) |
162dec01 GM |
545 | (setq image (cons 'image spec))))) |
546 | (setq specs (cdr specs)))) | |
349c034d GM |
547 | image)) |
548 | ||
549 | ||
550 | ;;;###autoload | |
551 | (defmacro defimage (symbol specs &optional doc) | |
552 | "Define SYMBOL as an image. | |
553 | ||
554 | SPECS is a list of image specifications. DOC is an optional | |
555 | documentation string. | |
556 | ||
557 | Each image specification in SPECS is a property list. The contents of | |
558 | a specification are image type dependent. All specifications must at | |
559 | least contain the properties `:type TYPE' and either `:file FILE' or | |
560 | `:data DATA', where TYPE is a symbol specifying the image type, | |
561 | e.g. `xbm', FILE is the file to load the image from, and DATA is a | |
562 | string containing the actual image data. The first image | |
563 | specification whose TYPE is supported, and FILE exists, is used to | |
564 | define SYMBOL. | |
565 | ||
566 | Example: | |
567 | ||
568 | (defimage test-image ((:type xpm :file \"~/test1.xpm\") | |
569 | (:type xbm :file \"~/test1.xbm\")))" | |
fd8e7a80 | 570 | (declare (doc-string 3)) |
349c034d | 571 | `(defvar ,symbol (find-image ',specs) ,doc)) |
7840ced1 GM |
572 | |
573 | ||
574 | (provide 'image) | |
575 | ||
fd8e7a80 | 576 | ;; arch-tag: 8e76a07b-eb48-4f3e-a7a0-1a7ba9f096b3 |
e43367a0 | 577 | ;;; image.el ends here |