(edebug-read-function): Fix handling of
[bpt/emacs.git] / lisp / image.el
CommitLineData
7840ced1
GM
1;;; image.el --- image API
2
2b8f2f46 3;; Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
04799cf5 4;; Keywords: multimedia
7840ced1
GM
5
6;; This file is part of GNU Emacs.
7
8;; GNU Emacs is free software; you can redistribute it and/or modify
9;; it under the terms of the GNU General Public License as published by
10;; the Free Software Foundation; either version 2, or (at your option)
11;; any later version.
12
13;; GNU Emacs is distributed in the hope that it will be useful,
14;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16;; GNU General Public License for more details.
17
18;; You should have received a copy of the GNU General Public License
19;; along with GNU Emacs; see the file COPYING. If not, write to the
20;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21;; Boston, MA 02111-1307, USA.
22
23;;; Commentary:
24
25;;; Code:
26
77f6105c
MB
27
28(defgroup image ()
29 "Image support."
30 :group 'multimedia)
31
32
7840ced1 33(defconst image-type-regexps
69ebef1d
GM
34 '(("\\`/\\*.*XPM.\\*/" . xpm)
35 ("\\`P[1-6]" . pbm)
36 ("\\`GIF8" . gif)
1155d483
GM
37 ;; The following is from JPEG File Interchange Format, Version 1.02.
38 ("\\`\xff\xd8\xff\xe0..JFIF\0" . jpeg)
69ebef1d
GM
39 ("\\`\211PNG\r\n" . png)
40 ("\\`#define" . xbm)
41 ("\\`\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff)
42 ("\\`%!PS" . postscript))
7840ced1
GM
43 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
44When the first bytes of an image file match REGEXP, it is assumed to
45be of image type IMAGE-TYPE.")
46
47
48;;;###autoload
162dec01
GM
49(defun image-type-from-data (data)
50 "Determine the image type from image data DATA.
51Value is a symbol specifying the image type or nil if type cannot
7840ced1 52be determined."
162dec01 53 (let ((types image-type-regexps)
7840ced1
GM
54 type)
55 (while (and types (null type))
56 (let ((regexp (car (car types)))
57 (image-type (cdr (car types))))
162dec01 58 (when (string-match regexp data)
7840ced1
GM
59 (setq type image-type))
60 (setq types (cdr types))))
61 type))
62
63
162dec01
GM
64;;;###autoload
65(defun image-type-from-file-header (file)
66 "Determine the type of image file FILE from its first few bytes.
67Value is a symbol specifying the image type, or nil if type cannot
68be determined."
69 (unless (file-name-directory file)
70 (setq file (expand-file-name file data-directory)))
71 (setq file (expand-file-name file))
72 (let ((header (with-temp-buffer
73 (insert-file-contents-literally file nil 0 256)
74 (buffer-string))))
75 (image-type-from-data header)))
76
77
7840ced1
GM
78;;;###autoload
79(defun image-type-available-p (type)
80 "Value is non-nil if image type TYPE is available.
81Image types are symbols like `xbm' or `jpeg'."
24978190 82 (and (boundp 'image-types) (not (null (memq type image-types)))))
7840ced1
GM
83
84
85;;;###autoload
162dec01
GM
86(defun create-image (file-or-data &optional type data-p &rest props)
87 "Create an image.
88FILE-OR-DATA is an image file name or image data.
7840ced1 89Optional TYPE is a symbol describing the image type. If TYPE is omitted
162dec01
GM
90or nil, try to determine the image type from its first few bytes
91of image data. If that doesn't work, and FILE-OR-DATA is a file name,
9ea8de1b 92use its file extension as image type.
162dec01 93Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
7840ced1 94Optional PROPS are additional image attributes to assign to the image,
d1d3c685 95like, e.g. `:mask MASK'.
7840ced1 96Value is the image created, or nil if images of type TYPE are not supported."
f64ae86c
GM
97 (when (and (not data-p) (not (stringp file-or-data)))
98 (error "Invalid image file name `%s'" file-or-data))
162dec01
GM
99 (cond ((null data-p)
100 ;; FILE-OR-DATA is a file name.
101 (unless (or type
102 (setq type (image-type-from-file-header file-or-data)))
103 (let ((extension (file-name-extension file-or-data)))
104 (unless extension
105 (error "Cannot determine image type"))
106 (setq type (intern extension)))))
107 (t
108 ;; FILE-OR-DATA contains image data.
109 (unless type
110 (setq type (image-type-from-data file-or-data)))))
111 (unless type
112 (error "Cannot determine image type"))
7840ced1 113 (unless (symbolp type)
162dec01 114 (error "Invalid image type `%s'" type))
7840ced1 115 (when (image-type-available-p type)
162dec01
GM
116 (append (list 'image :type type (if data-p :data :file) file-or-data)
117 props)))
7840ced1
GM
118
119
120;;;###autoload
cedbd8d0 121(defun put-image (image pos &optional string area)
1571b5ff 122 "Put image IMAGE in front of POS in the current buffer.
7840ced1 123IMAGE must be an image created with `create-image' or `defimage'.
46655c94
GM
124IMAGE is displayed by putting an overlay into the current buffer with a
125`before-string' STRING that has a `display' property whose value is the
cedbd8d0 126image. STRING is defaulted if you omit it.
7840ced1 127POS may be an integer or marker.
7840ced1
GM
128AREA is where to display the image. AREA nil or omitted means
129display it in the text area, a value of `left-margin' means
130display it in the left marginal area, a value of `right-margin'
46655c94 131means display it in the right marginal area."
0ad550ba 132 (unless string (setq string "x"))
1571b5ff 133 (let ((buffer (current-buffer)))
cedbd8d0 134 (unless (eq (car-safe image) 'image)
1571b5ff
GM
135 (error "Not an image: %s" image))
136 (unless (or (null area) (memq area '(left-margin right-margin)))
137 (error "Invalid area %s" area))
46655c94 138 (setq string (copy-sequence string))
1571b5ff 139 (let ((overlay (make-overlay pos pos buffer))
46655c94
GM
140 (prop (if (null area) image (list (list 'margin area) image))))
141 (put-text-property 0 (length string) 'display prop string)
1571b5ff
GM
142 (overlay-put overlay 'put-image t)
143 (overlay-put overlay 'before-string string))))
7840ced1
GM
144
145
146;;;###autoload
cedbd8d0 147(defun insert-image (image &optional string area)
7840ced1 148 "Insert IMAGE into current buffer at point.
46655c94 149IMAGE is displayed by inserting STRING into the current buffer
cedbd8d0
DL
150with a `display' property whose value is the image. STRING is
151defaulted if you omit it.
7840ced1
GM
152AREA is where to display the image. AREA nil or omitted means
153display it in the text area, a value of `left-margin' means
154display it in the left marginal area, a value of `right-margin'
46655c94 155means display it in the right marginal area."
0ad550ba
DL
156 ;; Use a space as least likely to cause trouble when it's a hidden
157 ;; character in the buffer.
158 (unless string (setq string " "))
cedbd8d0 159 (unless (eq (car-safe image) 'image)
7840ced1
GM
160 (error "Not an image: %s" image))
161 (unless (or (null area) (memq area '(left-margin right-margin)))
162 (error "Invalid area %s" area))
0dc91c57
DL
163 (if area
164 (setq image (list (list 'margin area) image))
165 ;; Cons up a new spec equal but not eq to `image' so that
166 ;; inserting it twice in a row (adjacently) displays two copies of
167 ;; the image. Don't try to avoid this by looking at the display
168 ;; properties on either side so that we DTRT more often with
169 ;; cut-and-paste. (Yanking killed image text next to another copy
170 ;; of it loses anyway.)
171 (setq image (cons 'image (cdr image))))
46655c94
GM
172 (let ((start (point)))
173 (insert string)
174 (add-text-properties start (point)
0dc91c57
DL
175 (list 'display image
176 ;; `image' has the right properties to
177 ;; mark an intangible field.
178 'intangible image
46655c94 179 'rear-nonsticky (list 'display)))))
10e2102e 180
7840ced1
GM
181
182;;;###autoload
183(defun remove-images (start end &optional buffer)
184 "Remove images between START and END in BUFFER.
185Remove only images that were put in BUFFER with calls to `put-image'.
186BUFFER nil or omitted means use the current buffer."
187 (unless buffer
188 (setq buffer (current-buffer)))
189 (let ((overlays (overlays-in start end)))
190 (while overlays
191 (let ((overlay (car overlays)))
192 (when (overlay-get overlay 'put-image)
0c898dd9
DL
193 (delete-overlay overlay)))
194 (setq overlays (cdr overlays)))))
7840ced1
GM
195
196
197;;;###autoload
349c034d
GM
198(defun find-image (specs)
199 "Find an image, choosing one of a list of image specifications.
7840ced1 200
cedbd8d0 201SPECS is a list of image specifications.
7840ced1
GM
202
203Each image specification in SPECS is a property list. The contents of
204a specification are image type dependent. All specifications must at
bc283707
WP
205least contain the properties `:type TYPE' and either `:file FILE' or
206`:data DATA', where TYPE is a symbol specifying the image type,
207e.g. `xbm', FILE is the file to load the image from, and DATA is a
cedbd8d0
DL
208string containing the actual image data. The specification whose TYPE
209is supported, and FILE exists, is used to construct the image
210specification to be returned. Return nil if no specification is
211satisfied.
212
213The image is looked for first on `load-path' and then in `data-directory'."
7840ced1
GM
214 (let (image)
215 (while (and specs (null image))
216 (let* ((spec (car specs))
217 (type (plist-get spec :type))
162dec01 218 (data (plist-get spec :data))
7f228379
GM
219 (file (plist-get spec :file))
220 found)
162dec01
GM
221 (when (image-type-available-p type)
222 (cond ((stringp file)
7f228379
GM
223 (let ((path load-path))
224 (while (and (not found) path)
225 (let ((try-file (expand-file-name file (car path))))
226 (when (file-readable-p try-file)
227 (setq found try-file)))
228 (setq path (cdr path)))
229 (unless found
cedbd8d0
DL
230 (let ((try-file (expand-file-name file data-directory)))
231 (if (file-readable-p try-file)
232 (setq found try-file))))
233 (if found
234 (setq image
9bb7a286
DL
235 (cons 'image (plist-put (copy-sequence spec)
236 :file found))))))
f64ae86c 237 ((not (null data))
162dec01
GM
238 (setq image (cons 'image spec)))))
239 (setq specs (cdr specs))))
349c034d
GM
240 image))
241
242
243;;;###autoload
244(defmacro defimage (symbol specs &optional doc)
245 "Define SYMBOL as an image.
246
247SPECS is a list of image specifications. DOC is an optional
248documentation string.
249
250Each image specification in SPECS is a property list. The contents of
251a specification are image type dependent. All specifications must at
252least contain the properties `:type TYPE' and either `:file FILE' or
253`:data DATA', where TYPE is a symbol specifying the image type,
254e.g. `xbm', FILE is the file to load the image from, and DATA is a
255string containing the actual image data. The first image
256specification whose TYPE is supported, and FILE exists, is used to
257define SYMBOL.
258
259Example:
260
261 (defimage test-image ((:type xpm :file \"~/test1.xpm\")
262 (:type xbm :file \"~/test1.xbm\")))"
263 `(defvar ,symbol (find-image ',specs) ,doc))
7840ced1
GM
264
265
266(provide 'image)
267
e43367a0 268;;; image.el ends here