(dabbrev-ignored-buffer-regexps): Renamed from
[emacs.git] / lisp / image.el
blobb412a3359b2fc57fe157d0d734ba09eeac29b45a
1 ;;; image.el --- image API
3 ;; Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
4 ;; Keywords: multimedia
6 ;; This file is part of GNU Emacs.
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.
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.
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.
23 ;;; Commentary:
25 ;;; Code:
28 (defgroup image ()
29 "Image support."
30 :group 'multimedia)
33 (defconst image-type-regexps
34 '(("^/\\*.*XPM.\\*/" . xpm)
35 ("^P[1-6]" . pbm)
36 ("^GIF8" . gif)
37 ("JFIF" . jpeg)
38 ("^\211PNG\r\n" . png)
39 ("^#define" . xbm)
40 ("^\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff)
41 ("^%!PS" . ghostscript))
42 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
43 When the first bytes of an image file match REGEXP, it is assumed to
44 be of image type IMAGE-TYPE.")
47 ;;;###autoload
48 (defun image-type-from-data (data)
49 "Determine the image type from image data DATA.
50 Value is a symbol specifying the image type or nil if type cannot
51 be determined."
52 (let ((types image-type-regexps)
53 type)
54 (while (and types (null type))
55 (let ((regexp (car (car types)))
56 (image-type (cdr (car types))))
57 (when (string-match regexp data)
58 (setq type image-type))
59 (setq types (cdr types))))
60 type))
63 ;;;###autoload
64 (defun image-type-from-file-header (file)
65 "Determine the type of image file FILE from its first few bytes.
66 Value is a symbol specifying the image type, or nil if type cannot
67 be determined."
68 (unless (file-name-directory file)
69 (setq file (expand-file-name file data-directory)))
70 (setq file (expand-file-name file))
71 (let ((header (with-temp-buffer
72 (insert-file-contents-literally file nil 0 256)
73 (buffer-string))))
74 (image-type-from-data header)))
77 ;;;###autoload
78 (defun image-type-available-p (type)
79 "Value is non-nil if image type TYPE is available.
80 Image types are symbols like `xbm' or `jpeg'."
81 (and (boundp 'image-types) (not (null (memq type image-types)))))
84 ;;;###autoload
85 (defun create-image (file-or-data &optional type data-p &rest props)
86 "Create an image.
87 FILE-OR-DATA is an image file name or image data.
88 Optional TYPE is a symbol describing the image type. If TYPE is omitted
89 or nil, try to determine the image type from its first few bytes
90 of image data. If that doesn't work, and FILE-OR-DATA is a file name,
91 use its file extension as image type.
92 Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
93 Optional PROPS are additional image attributes to assign to the image,
94 like, e.g. `:mask MASK'.
95 Value is the image created, or nil if images of type TYPE are not supported."
96 (when (and (not data-p) (not (stringp file-or-data)))
97 (error "Invalid image file name `%s'" file-or-data))
98 (cond ((null data-p)
99 ;; FILE-OR-DATA is a file name.
100 (unless (or type
101 (setq type (image-type-from-file-header file-or-data)))
102 (let ((extension (file-name-extension file-or-data)))
103 (unless extension
104 (error "Cannot determine image type"))
105 (setq type (intern extension)))))
107 ;; FILE-OR-DATA contains image data.
108 (unless type
109 (setq type (image-type-from-data file-or-data)))))
110 (unless type
111 (error "Cannot determine image type"))
112 (unless (symbolp type)
113 (error "Invalid image type `%s'" type))
114 (when (image-type-available-p type)
115 (append (list 'image :type type (if data-p :data :file) file-or-data)
116 props)))
119 ;;;###autoload
120 (defun put-image (image pos &optional string area)
121 "Put image IMAGE in front of POS in the current buffer.
122 IMAGE must be an image created with `create-image' or `defimage'.
123 IMAGE is displayed by putting an overlay into the current buffer with a
124 `before-string' STRING that has a `display' property whose value is the
125 image. STRING is defaulted if you omit it.
126 POS may be an integer or marker.
127 AREA is where to display the image. AREA nil or omitted means
128 display it in the text area, a value of `left-margin' means
129 display it in the left marginal area, a value of `right-margin'
130 means display it in the right marginal area."
131 (unless string (setq string "x"))
132 (let ((buffer (current-buffer)))
133 (unless (eq (car-safe image) 'image)
134 (error "Not an image: %s" image))
135 (unless (or (null area) (memq area '(left-margin right-margin)))
136 (error "Invalid area %s" area))
137 (setq string (copy-sequence string))
138 (let ((overlay (make-overlay pos pos buffer))
139 (prop (if (null area) image (list (list 'margin area) image))))
140 (put-text-property 0 (length string) 'display prop string)
141 (overlay-put overlay 'put-image t)
142 (overlay-put overlay 'before-string string))))
145 ;;;###autoload
146 (defun insert-image (image &optional string area)
147 "Insert IMAGE into current buffer at point.
148 IMAGE is displayed by inserting STRING into the current buffer
149 with a `display' property whose value is the image. STRING is
150 defaulted if you omit it.
151 AREA is where to display the image. AREA nil or omitted means
152 display it in the text area, a value of `left-margin' means
153 display it in the left marginal area, a value of `right-margin'
154 means display it in the right marginal area."
155 ;; Use a space as least likely to cause trouble when it's a hidden
156 ;; character in the buffer.
157 (unless string (setq string " "))
158 (unless (eq (car-safe image) 'image)
159 (error "Not an image: %s" image))
160 (unless (or (null area) (memq area '(left-margin right-margin)))
161 (error "Invalid area %s" area))
162 (if area
163 (setq image (list (list 'margin area) image))
164 ;; Cons up a new spec equal but not eq to `image' so that
165 ;; inserting it twice in a row (adjacently) displays two copies of
166 ;; the image. Don't try to avoid this by looking at the display
167 ;; properties on either side so that we DTRT more often with
168 ;; cut-and-paste. (Yanking killed image text next to another copy
169 ;; of it loses anyway.)
170 (setq image (cons 'image (cdr image))))
171 (let ((start (point)))
172 (insert string)
173 (add-text-properties start (point)
174 (list 'display image
175 ;; `image' has the right properties to
176 ;; mark an intangible field.
177 'intangible image
178 'rear-nonsticky (list 'display)))))
181 ;;;###autoload
182 (defun remove-images (start end &optional buffer)
183 "Remove images between START and END in BUFFER.
184 Remove only images that were put in BUFFER with calls to `put-image'.
185 BUFFER nil or omitted means use the current buffer."
186 (unless buffer
187 (setq buffer (current-buffer)))
188 (let ((overlays (overlays-in start end)))
189 (while overlays
190 (let ((overlay (car overlays)))
191 (when (overlay-get overlay 'put-image)
192 (delete-overlay overlay)))
193 (setq overlays (cdr overlays)))))
196 ;;;###autoload
197 (defun find-image (specs)
198 "Find an image, choosing one of a list of image specifications.
200 SPECS is a list of image specifications.
202 Each image specification in SPECS is a property list. The contents of
203 a specification are image type dependent. All specifications must at
204 least contain the properties `:type TYPE' and either `:file FILE' or
205 `:data DATA', where TYPE is a symbol specifying the image type,
206 e.g. `xbm', FILE is the file to load the image from, and DATA is a
207 string containing the actual image data. The specification whose TYPE
208 is supported, and FILE exists, is used to construct the image
209 specification to be returned. Return nil if no specification is
210 satisfied.
212 The image is looked for first on `load-path' and then in `data-directory'."
213 (let (image)
214 (while (and specs (null image))
215 (let* ((spec (car specs))
216 (type (plist-get spec :type))
217 (data (plist-get spec :data))
218 (file (plist-get spec :file))
219 found)
220 (when (image-type-available-p type)
221 (cond ((stringp file)
222 (let ((path load-path))
223 (while (and (not found) path)
224 (let ((try-file (expand-file-name file (car path))))
225 (when (file-readable-p try-file)
226 (setq found try-file)))
227 (setq path (cdr path)))
228 (unless found
229 (let ((try-file (expand-file-name file data-directory)))
230 (if (file-readable-p try-file)
231 (setq found try-file))))
232 (if found
233 (setq image
234 (cons 'image (plist-put (copy-sequence spec)
235 :file found))))))
236 ((not (null data))
237 (setq image (cons 'image spec)))))
238 (setq specs (cdr specs))))
239 image))
242 ;;;###autoload
243 (defmacro defimage (symbol specs &optional doc)
244 "Define SYMBOL as an image.
246 SPECS is a list of image specifications. DOC is an optional
247 documentation string.
249 Each image specification in SPECS is a property list. The contents of
250 a specification are image type dependent. All specifications must at
251 least contain the properties `:type TYPE' and either `:file FILE' or
252 `:data DATA', where TYPE is a symbol specifying the image type,
253 e.g. `xbm', FILE is the file to load the image from, and DATA is a
254 string containing the actual image data. The first image
255 specification whose TYPE is supported, and FILE exists, is used to
256 define SYMBOL.
258 Example:
260 (defimage test-image ((:type xpm :file \"~/test1.xpm\")
261 (:type xbm :file \"~/test1.xbm\")))"
262 `(defvar ,symbol (find-image ',specs) ,doc))
265 (provide 'image)
267 ;;; image.el ends here