Install to the CVS repository what I forgot to install in my
[emacs.git] / lisp / ezimage.el
blobdd3a19443061e9e57bc68986d43f87344f935e57
1 ;;; ezimage --- Generalized Image management
3 ;;; Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Keywords: file, tags, tools
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
25 ;;; Commentary:
27 ;; A few routines for placing an image over text that will work for any
28 ;; Emacs implementation without error. When images are not supported, then
29 ;; they are justnot displayed.
31 ;; The idea is that gui buffers (trees, buttons, etc) will have text
32 ;; representations of the GUI elements. These routines will replace the text
33 ;; with an image when images are available.
35 ;; This file requires the `image' package if it is available.
37 (condition-case nil
38 (require 'image)
39 (error nil))
41 ;;; Code:
42 (defcustom ezimage-use-images
43 (and (or (fboundp 'defimage) ; emacs 21
44 (fboundp 'make-image-specifier)) ; xemacs
45 (if (fboundp 'display-graphic-p) ; emacs 21
46 (display-graphic-p)
47 window-system) ; old emacs & xemacs
48 (or (not (fboundp 'image-type-available-p)) ; xemacs?
49 (image-type-available-p 'xpm))) ; emacs 21
50 "*Non-nil if ezimage should display icons."
51 :group 'ezimage
52 :version "21.1"
53 :type 'boolean)
55 ;;; Create our own version of defimage
56 (eval-and-compile
58 (if (fboundp 'defimage)
60 (progn
62 (defmacro defezimage (variable imagespec docstring)
63 "Define VARIABLE as an image if `defimage' is not available.
64 IMAGESPEC is the image data, and DOCSTRING is documentation for the image."
65 `(progn
66 (defimage ,variable ,imagespec ,docstring)
67 (put (quote ,variable) 'ezimage t)))
69 ; (defalias 'defezimage 'defimage)
71 ;; This hack is for the ezimage install which has an icons direcory for
72 ;; the default icons to be used.
73 ;; (add-to-list 'load-path
74 ;; (concat (file-name-directory
75 ;; (locate-library "ezimage.el"))
76 ;; "icons"))
79 (if (not (fboundp 'make-glyph))
81 (defmacro defezimage (variable imagespec docstring)
82 "Don't bother loading up an image...
83 Argument VARIABLE is the variable to define.
84 Argument IMAGESPEC is the list defining the image to create.
85 Argument DOCSTRING is the documentation for VARIABLE."
86 `(defvar ,variable nil ,docstring))
88 ;; ELSE
89 (with-no-warnings
90 (defun ezimage-find-image-on-load-path (image)
91 "Find the image file IMAGE on the load path."
92 (let ((l (cons
93 ;; In XEmacs, try the data directory first (for an
94 ;; install in XEmacs proper.) Search the load
95 ;; path next (for user installs)
96 (locate-data-directory "ezimage")
97 load-path))
98 (r nil))
99 (while (and l (not r))
100 (if (file-exists-p (concat (car l) "/" image))
101 (setq r (concat (car l) "/" image))
102 (if (file-exists-p (concat (car l) "/icons/" image))
103 (setq r (concat (car l) "/icons/" image))
105 (setq l (cdr l)))
107 );with-no-warnings
109 (with-no-warnings
110 (defun ezimage-convert-emacs21-imagespec-to-xemacs (spec)
111 "Convert the Emacs21 image SPEC into an XEmacs image spec.
112 The Emacs 21 spec is what I first learned, and is easy to convert."
113 (let* ((sl (car spec))
114 (itype (nth 1 sl))
115 (ifile (nth 3 sl)))
116 (vector itype ':file (ezimage-find-image-on-load-path ifile))))
117 );with-no-warnings
119 (defmacro defezimage (variable imagespec docstring)
120 "Define VARIABLE as an image if `defimage' is not available.
121 IMAGESPEC is the image data, and DOCSTRING is documentation for the image."
122 `(progn
123 (defvar ,variable
124 ;; The Emacs21 version of defimage looks just like the XEmacs image
125 ;; specifier, except that it needs a :type keyword. If we line
126 ;; stuff up right, we can use this cheat to support XEmacs specifiers.
127 (condition-case nil
128 (make-glyph
129 (make-image-specifier
130 (ezimage-convert-emacs21-imagespec-to-xemacs (quote ,imagespec)))
131 'buffer)
132 (error nil))
133 ,docstring)
134 (put ',variable 'ezimage t)))
138 (defezimage ezimage-directory
139 ((:type xpm :file "ezimage/dir.xpm" :ascent center))
140 "Image used for empty directories.")
142 (defezimage ezimage-directory-plus
143 ((:type xpm :file "ezimage/dir-plus.xpm" :ascent center))
144 "Image used for closed directories with stuff in them.")
146 (defezimage ezimage-directory-minus
147 ((:type xpm :file "ezimage/dir-minus.xpm" :ascent center))
148 "Image used for open directories with stuff in them.")
150 (defezimage ezimage-page-plus
151 ((:type xpm :file "ezimage/page-plus.xpm" :ascent center))
152 "Image used for closed files with stuff in them.")
154 (defezimage ezimage-page-minus
155 ((:type xpm :file "ezimage/page-minus.xpm" :ascent center))
156 "Image used for open files with stuff in them.")
158 (defezimage ezimage-page
159 ((:type xpm :file "ezimage/page.xpm" :ascent center))
160 "Image used for files with nothing interesting in it.")
162 (defezimage ezimage-tag
163 ((:type xpm :file "ezimage/tag.xpm" :ascent center))
164 "Image used for tags.")
166 (defezimage ezimage-tag-plus
167 ((:type xpm :file "ezimage/tag-plus.xpm" :ascent center))
168 "Image used for closed tag groups.")
170 (defezimage ezimage-tag-minus
171 ((:type xpm :file "ezimage/tag-minus.xpm" :ascent center))
172 "Image used for open tags.")
174 (defezimage ezimage-tag-gt
175 ((:type xpm :file "ezimage/tag-gt.xpm" :ascent center))
176 "Image used for closed tags (with twist arrow).")
178 (defezimage ezimage-tag-v
179 ((:type xpm :file "ezimage/tag-v.xpm" :ascent center))
180 "Image used for open tags (with twist arrow).")
182 (defezimage ezimage-tag-type
183 ((:type xpm :file "ezimage/tag-type.xpm" :ascent center))
184 "Image used for tags that represent a data type.")
186 (defezimage ezimage-box-plus
187 ((:type xpm :file "ezimage/box-plus.xpm" :ascent center))
188 "Image of a closed box.")
190 (defezimage ezimage-box-minus
191 ((:type xpm :file "ezimage/box-minus.xpm" :ascent center))
192 "Image of an open box.")
194 (defezimage ezimage-mail
195 ((:type xpm :file "ezimage/mail.xpm" :ascent center))
196 "Image if an envelope.")
198 (defezimage ezimage-checkout
199 ((:type xpm :file "ezimage/checkmark.xpm" :ascent center))
200 "Image representing a checkmark. For files checked out of a VC.")
202 (defezimage ezimage-object
203 ((:type xpm :file "ezimage/bits.xpm" :ascent center))
204 "Image representing bits (an object file.)")
206 (defezimage ezimage-object-out-of-date
207 ((:type xpm :file "ezimage/bitsbang.xpm" :ascent center))
208 "Image representing bits with a ! in it. (an out of data object file.)")
210 (defezimage ezimage-label
211 ((:type xpm :file "ezimage/label.xpm" :ascent center))
212 "Image used for label prefix.")
214 (defezimage ezimage-lock
215 ((:type xpm :file "ezimage/lock.xpm" :ascent center))
216 "Image of a lock. Used for Read Only, or private.")
218 (defezimage ezimage-unlock
219 ((:type xpm :file "ezimage/unlock.xpm" :ascent center))
220 "Image of an unlocked lock.")
222 (defezimage ezimage-key
223 ((:type xpm :file "ezimage/key.xpm" :ascent center))
224 "Image of a key.")
226 (defezimage ezimage-document-tag
227 ((:type xpm :file "ezimage/doc.xpm" :ascent center))
228 "Image used to indicate documentation available.")
230 (defezimage ezimage-document-plus
231 ((:type xpm :file "ezimage/doc-plus.xpm" :ascent center))
232 "Image used to indicate closed documentation.")
234 (defezimage ezimage-document-minus
235 ((:type xpm :file "ezimage/doc-minus.xpm" :ascent center))
236 "Image used to indicate open documentation.")
238 (defezimage ezimage-info-tag
239 ((:type xpm :file "ezimage/info.xpm" :ascent center))
240 "Image used to indicate more information available.")
242 (defvar ezimage-expand-image-button-alist
244 ;; here are some standard representations
245 ("<+>" . ezimage-directory-plus)
246 ("<->" . ezimage-directory-minus)
247 ("< >" . ezimage-directory)
248 ("[+]" . ezimage-page-plus)
249 ("[-]" . ezimage-page-minus)
250 ("[?]" . ezimage-page)
251 ("[ ]" . ezimage-page)
252 ("{+}" . ezimage-box-plus)
253 ("{-}" . ezimage-box-minus)
254 ;; Some vaguely representitive entries
255 ("*" . ezimage-checkout)
256 ("#" . ezimage-object)
257 ("!" . ezimage-object-out-of-date)
258 ("%" . ezimage-lock)
260 "List of text and image associations.")
262 (defun ezimage-insert-image-button-maybe (start length &optional string)
263 "Insert an image button based on text starting at START for LENGTH chars.
264 If buttontext is unknown, just insert that text.
265 If we have an image associated with it, use that image.
266 Optional argument STRING is a st ring upon which to add text properties."
267 (when ezimage-use-images
268 (let* ((bt (buffer-substring start (+ length start)))
269 (a (assoc bt ezimage-expand-image-button-alist)))
270 ;; Regular images (created with `insert-image' are intangible
271 ;; which (I suppose) make them more compatible with XEmacs 21.
272 ;; Unfortunatly, there is a giant pile o code dependent on the
273 ;; underlying text. This means if we leave it tangible, then I
274 ;; don't have to change said giant piles o code.
275 (if (and a (symbol-value (cdr a)))
276 (ezimage-insert-over-text (symbol-value (cdr a))
277 start
278 (+ start (length bt))))))
279 string)
281 (defun ezimage-image-over-string (string &optional alist)
282 "Insert over the text in STRING an image found in ALIST.
283 Return STRING with properties applied."
284 (if ezimage-use-images
285 (let ((a (assoc string alist)))
286 (if (and a (symbol-value (cdr a)))
287 (ezimage-insert-over-text (symbol-value (cdr a))
288 0 (length string)
289 string)
290 string))
291 string))
293 (defun ezimage-insert-over-text (image start end &optional string)
294 "Place IMAGE over the text between START and END.
295 Assumes the image is part of a gui and can be clicked on.
296 Optional argument STRING is a string upon which to add text properties."
297 (when ezimage-use-images
298 (if (featurep 'xemacs)
299 (add-text-properties start end
300 (list 'end-glyph image
301 'rear-nonsticky (list 'display)
302 'invisible t
303 'detachable t)
304 string)
305 (add-text-properties start end
306 (list 'display image
307 'rear-nonsticky (list 'display))
308 string)))
309 string)
311 (defun ezimage-image-association-dump ()
312 "Dump out the current state of the Ezimage image alist.
313 See `ezimage-expand-image-button-alist' for details."
314 (interactive)
315 (with-output-to-temp-buffer "*Ezimage Images*"
316 (save-excursion
317 (set-buffer "*Ezimage Images*")
318 (goto-char (point-max))
319 (insert "Ezimage image cache.\n\n")
320 (let ((start (point)) (end nil))
321 (insert "Image\tText\tImage Name")
322 (setq end (point))
323 (insert "\n")
324 (put-text-property start end 'face 'underline))
325 (let ((ia ezimage-expand-image-button-alist))
326 (while ia
327 (let ((start (point)))
328 (insert (car (car ia)))
329 (insert "\t")
330 (ezimage-insert-image-button-maybe start
331 (length (car (car ia))))
332 (insert (car (car ia)) "\t" (format "%s" (cdr (car ia))) "\n"))
333 (setq ia (cdr ia)))))))
335 (defun ezimage-image-dump ()
336 "Dump out the current state of the Ezimage image alist.
337 See `ezimage-expand-image-button-alist' for details."
338 (interactive)
339 (with-output-to-temp-buffer "*Ezimage Images*"
340 (save-excursion
341 (set-buffer "*Ezimage Images*")
342 (goto-char (point-max))
343 (insert "Ezimage image cache.\n\n")
344 (let ((start (point)) (end nil))
345 (insert "Image\tImage Name")
346 (setq end (point))
347 (insert "\n")
348 (put-text-property start end 'face 'underline))
349 (let ((ia (ezimage-all-images)))
350 (while ia
351 (let ((start (point)))
352 (insert "cm")
353 (ezimage-insert-over-text (symbol-value (car ia)) start (point))
354 (insert "\t" (format "%s" (car ia)) "\n"))
355 (setq ia (cdr ia)))))))
357 (defun ezimage-all-images ()
358 "Return a list of all variables containing ez images."
359 (let ((ans nil))
360 (mapatoms (lambda (sym)
361 (if (get sym 'ezimage) (setq ans (cons sym ans))))
363 (setq ans (sort ans (lambda (a b)
364 (string< (symbol-name a) (symbol-name b)))))
365 ans)
368 (provide 'ezimage)
370 ;; arch-tag: d4ea2d93-3c7a-4cb3-b5a6-c1b9178183aa
371 ;;; sb-image.el ends here