Mercurial > emacs
annotate lisp/image.el @ 26474:027b7c42a65b
(Fx_show_busy_cursor): Doc-fix.
(Fx_hide_busy_cursor): Ditto.
author | Gerd Moellmann <gerd@gnu.org> |
---|---|
date | Wed, 17 Nov 1999 21:03:12 +0000 |
parents | 774b2504710b |
children | 6bb2a4a0413e |
rev | line source |
---|---|
25003 | 1 ;;; image.el --- image API |
2 | |
25857
fdc2bd91cf63
(defimage): Remove redundant code. Substitute file on image plist.
Dave Love <fx@gnu.org>
parents:
25816
diff
changeset
|
3 ;; Copyright (C) 1998, 1999 Free Software Foundation, Inc. |
25309 | 4 ;; Keywords: multimedia |
25003 | 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 | |
27 (defconst image-type-regexps | |
28 '(("^/\\*.*XPM.\\*/" . xpm) | |
29 ("^P[1-6]" . pbm) | |
30 ("^GIF8" . gif) | |
31 ("JFIF" . jpeg) | |
32 ("^\211PNG\r\n" . png) | |
33 ("^#define" . xbm) | |
34 ("^\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff) | |
35 ("^%!PS" . ghostscript)) | |
36 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types. | |
37 When the first bytes of an image file match REGEXP, it is assumed to | |
38 be of image type IMAGE-TYPE.") | |
39 | |
40 | |
41 ;;;###autoload | |
42 (defun image-type-from-file-header (file) | |
43 "Determine the type of image file FILE from its first few bytes. | |
44 Value is a symbol specifying the image type, or nil if type cannot | |
45 be determined." | |
46 (unless (file-name-directory file) | |
47 (setq file (concat data-directory file))) | |
48 (setq file (expand-file-name file)) | |
49 (let ((header (with-temp-buffer | |
50 (insert-file-contents-literally file nil 0 256) | |
51 (buffer-string))) | |
52 (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 header) | |
58 (setq type image-type)) | |
59 (setq types (cdr types)))) | |
60 type)) | |
61 | |
62 | |
63 ;;;###autoload | |
64 (defun image-type-available-p (type) | |
65 "Value is non-nil if image type TYPE is available. | |
66 Image types are symbols like `xbm' or `jpeg'." | |
67 (not (null (memq type image-types)))) | |
68 | |
69 | |
70 ;;;###autoload | |
71 (defun create-image (file &optional type &rest props) | |
72 "Create an image which will be loaded from FILE. | |
73 Optional TYPE is a symbol describing the image type. If TYPE is omitted | |
74 or nil, try to determine the image file type from its first few bytes. | |
75 If that doesn't work, use FILE's extension.as image type. | |
76 Optional PROPS are additional image attributes to assign to the image, | |
77 like, e.g. `:heuristic-mask t'. | |
78 Value is the image created, or nil if images of type TYPE are not supported." | |
79 (unless (stringp file) | |
80 (error "Invalid image file name %s" file)) | |
81 (unless (or type | |
82 (setq type (image-type-from-file-header file))) | |
83 (let ((extension (file-name-extension file))) | |
84 (unless extension | |
85 (error "Cannot determine image type")) | |
86 (setq type (intern extension)))) | |
87 (unless (symbolp type) | |
88 (error "Invalid image type %s" type)) | |
89 (when (image-type-available-p type) | |
90 (append (list 'image :type type :file file) props))) | |
91 | |
92 | |
93 ;;;###autoload | |
25816
2d53a03a3baa
(put-image, insert-image): Add string argument.
Gerd Moellmann <gerd@gnu.org>
parents:
25617
diff
changeset
|
94 (defun put-image (image pos string &optional area) |
25617
697b28471784
(put-image): Remove optional buffer parameter.
Gerd Moellmann <gerd@gnu.org>
parents:
25309
diff
changeset
|
95 "Put image IMAGE in front of POS in the current buffer. |
25003 | 96 IMAGE must be an image created with `create-image' or `defimage'. |
25816
2d53a03a3baa
(put-image, insert-image): Add string argument.
Gerd Moellmann <gerd@gnu.org>
parents:
25617
diff
changeset
|
97 IMAGE is displayed by putting an overlay into the current buffer with a |
2d53a03a3baa
(put-image, insert-image): Add string argument.
Gerd Moellmann <gerd@gnu.org>
parents:
25617
diff
changeset
|
98 `before-string' STRING that has a `display' property whose value is the |
2d53a03a3baa
(put-image, insert-image): Add string argument.
Gerd Moellmann <gerd@gnu.org>
parents:
25617
diff
changeset
|
99 image. |
25003 | 100 POS may be an integer or marker. |
101 AREA is where to display the image. AREA nil or omitted means | |
102 display it in the text area, a value of `left-margin' means | |
103 display it in the left marginal area, a value of `right-margin' | |
25816
2d53a03a3baa
(put-image, insert-image): Add string argument.
Gerd Moellmann <gerd@gnu.org>
parents:
25617
diff
changeset
|
104 means display it in the right marginal area." |
25617
697b28471784
(put-image): Remove optional buffer parameter.
Gerd Moellmann <gerd@gnu.org>
parents:
25309
diff
changeset
|
105 (let ((buffer (current-buffer))) |
697b28471784
(put-image): Remove optional buffer parameter.
Gerd Moellmann <gerd@gnu.org>
parents:
25309
diff
changeset
|
106 (unless (eq (car image) 'image) |
697b28471784
(put-image): Remove optional buffer parameter.
Gerd Moellmann <gerd@gnu.org>
parents:
25309
diff
changeset
|
107 (error "Not an image: %s" image)) |
697b28471784
(put-image): Remove optional buffer parameter.
Gerd Moellmann <gerd@gnu.org>
parents:
25309
diff
changeset
|
108 (unless (or (null area) (memq area '(left-margin right-margin))) |
697b28471784
(put-image): Remove optional buffer parameter.
Gerd Moellmann <gerd@gnu.org>
parents:
25309
diff
changeset
|
109 (error "Invalid area %s" area)) |
25816
2d53a03a3baa
(put-image, insert-image): Add string argument.
Gerd Moellmann <gerd@gnu.org>
parents:
25617
diff
changeset
|
110 (setq string (copy-sequence string)) |
25617
697b28471784
(put-image): Remove optional buffer parameter.
Gerd Moellmann <gerd@gnu.org>
parents:
25309
diff
changeset
|
111 (let ((overlay (make-overlay pos pos buffer)) |
25816
2d53a03a3baa
(put-image, insert-image): Add string argument.
Gerd Moellmann <gerd@gnu.org>
parents:
25617
diff
changeset
|
112 (prop (if (null area) image (list (list 'margin area) image)))) |
2d53a03a3baa
(put-image, insert-image): Add string argument.
Gerd Moellmann <gerd@gnu.org>
parents:
25617
diff
changeset
|
113 (put-text-property 0 (length string) 'display prop string) |
25617
697b28471784
(put-image): Remove optional buffer parameter.
Gerd Moellmann <gerd@gnu.org>
parents:
25309
diff
changeset
|
114 (overlay-put overlay 'put-image t) |
697b28471784
(put-image): Remove optional buffer parameter.
Gerd Moellmann <gerd@gnu.org>
parents:
25309
diff
changeset
|
115 (overlay-put overlay 'before-string string)))) |
25003 | 116 |
117 | |
118 ;;;###autoload | |
25816
2d53a03a3baa
(put-image, insert-image): Add string argument.
Gerd Moellmann <gerd@gnu.org>
parents:
25617
diff
changeset
|
119 (defun insert-image (image string &optional area) |
25003 | 120 "Insert IMAGE into current buffer at point. |
25816
2d53a03a3baa
(put-image, insert-image): Add string argument.
Gerd Moellmann <gerd@gnu.org>
parents:
25617
diff
changeset
|
121 IMAGE is displayed by inserting STRING into the current buffer |
2d53a03a3baa
(put-image, insert-image): Add string argument.
Gerd Moellmann <gerd@gnu.org>
parents:
25617
diff
changeset
|
122 with a `display' property whose value is the image. |
25003 | 123 AREA is where to display the image. AREA nil or omitted means |
124 display it in the text area, a value of `left-margin' means | |
125 display it in the left marginal area, a value of `right-margin' | |
25816
2d53a03a3baa
(put-image, insert-image): Add string argument.
Gerd Moellmann <gerd@gnu.org>
parents:
25617
diff
changeset
|
126 means display it in the right marginal area." |
25003 | 127 (unless (eq (car image) 'image) |
128 (error "Not an image: %s" image)) | |
129 (unless (or (null area) (memq area '(left-margin right-margin))) | |
130 (error "Invalid area %s" area)) | |
25816
2d53a03a3baa
(put-image, insert-image): Add string argument.
Gerd Moellmann <gerd@gnu.org>
parents:
25617
diff
changeset
|
131 (when area |
2d53a03a3baa
(put-image, insert-image): Add string argument.
Gerd Moellmann <gerd@gnu.org>
parents:
25617
diff
changeset
|
132 (setq image (list (list 'margin area) image))) |
2d53a03a3baa
(put-image, insert-image): Add string argument.
Gerd Moellmann <gerd@gnu.org>
parents:
25617
diff
changeset
|
133 (let ((start (point))) |
2d53a03a3baa
(put-image, insert-image): Add string argument.
Gerd Moellmann <gerd@gnu.org>
parents:
25617
diff
changeset
|
134 (insert string) |
26007
774b2504710b
(insert-image): Copy the image spec and add an intangible property.
Dave Love <fx@gnu.org>
parents:
25872
diff
changeset
|
135 ;; Copy `image' so that inserting it twice in a row (adjacently) |
774b2504710b
(insert-image): Copy the image spec and add an intangible property.
Dave Love <fx@gnu.org>
parents:
25872
diff
changeset
|
136 ;; displays two copies of the image. |
25816
2d53a03a3baa
(put-image, insert-image): Add string argument.
Gerd Moellmann <gerd@gnu.org>
parents:
25617
diff
changeset
|
137 (add-text-properties start (point) |
26007
774b2504710b
(insert-image): Copy the image spec and add an intangible property.
Dave Love <fx@gnu.org>
parents:
25872
diff
changeset
|
138 (list 'display (copy-sequence image) |
774b2504710b
(insert-image): Copy the image spec and add an intangible property.
Dave Love <fx@gnu.org>
parents:
25872
diff
changeset
|
139 'intangible (list t) ; something unique |
25816
2d53a03a3baa
(put-image, insert-image): Add string argument.
Gerd Moellmann <gerd@gnu.org>
parents:
25617
diff
changeset
|
140 'rear-nonsticky (list 'display))))) |
26007
774b2504710b
(insert-image): Copy the image spec and add an intangible property.
Dave Love <fx@gnu.org>
parents:
25872
diff
changeset
|
141 |
25003 | 142 |
143 ;;;###autoload | |
144 (defun remove-images (start end &optional buffer) | |
145 "Remove images between START and END in BUFFER. | |
146 Remove only images that were put in BUFFER with calls to `put-image'. | |
147 BUFFER nil or omitted means use the current buffer." | |
148 (unless buffer | |
149 (setq buffer (current-buffer))) | |
150 (let ((overlays (overlays-in start end))) | |
151 (while overlays | |
152 (let ((overlay (car overlays))) | |
153 (when (overlay-get overlay 'put-image) | |
25857
fdc2bd91cf63
(defimage): Remove redundant code. Substitute file on image plist.
Dave Love <fx@gnu.org>
parents:
25816
diff
changeset
|
154 (delete-overlay overlay))) |
fdc2bd91cf63
(defimage): Remove redundant code. Substitute file on image plist.
Dave Love <fx@gnu.org>
parents:
25816
diff
changeset
|
155 (setq overlays (cdr overlays))))) |
25003 | 156 |
157 | |
158 ;;;###autoload | |
159 (defmacro defimage (symbol specs &optional doc) | |
160 "Define SYMBOL as an image. | |
161 | |
162 SPECS is a list of image specifications. DOC is an optional | |
163 documentation string. | |
164 | |
165 Each image specification in SPECS is a property list. The contents of | |
166 a specification are image type dependent. All specifications must at | |
167 least contain the properties `:type TYPE' and `:file FILE', where TYPE | |
168 is a symbol specifying the image type, e.g. `xbm', and FILE is the | |
169 file to load the image from. The first image specification whose TYPE | |
170 is supported, and FILE exists, is used to define SYMBOL. | |
171 | |
172 Example: | |
173 | |
174 (defimage test-image ((:type xpm :file \"~/test1.xpm\") | |
175 (:type xbm :file \"~/test1.xbm\")))" | |
176 (let (image) | |
177 (while (and specs (null image)) | |
178 (let* ((spec (car specs)) | |
179 (type (plist-get spec :type)) | |
180 (file (plist-get spec :file))) | |
181 (when (and (image-type-available-p type) (stringp file)) | |
25857
fdc2bd91cf63
(defimage): Remove redundant code. Substitute file on image plist.
Dave Love <fx@gnu.org>
parents:
25816
diff
changeset
|
182 (setq file (expand-file-name file data-directory)) |
fdc2bd91cf63
(defimage): Remove redundant code. Substitute file on image plist.
Dave Love <fx@gnu.org>
parents:
25816
diff
changeset
|
183 (when (file-readable-p file) |
fdc2bd91cf63
(defimage): Remove redundant code. Substitute file on image plist.
Dave Love <fx@gnu.org>
parents:
25816
diff
changeset
|
184 (setq image (cons 'image (plist-put spec :file file))))) |
25003 | 185 (setq specs (cdr specs)))) |
186 `(defvar ,symbol ',image ,doc))) | |
187 | |
188 | |
189 (provide 'image) | |
190 | |
25872 | 191 ;;; image.el ends here |