ezimage.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. ;;; ezimage --- Generalized Image management
  2. ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <zappo@gnu.org>
  4. ;; Keywords: file, tags, tools
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;; A few routines for placing an image over text that will work for any
  19. ;; Emacs implementation without error. When images are not supported, then
  20. ;; they are just not displayed.
  21. ;;
  22. ;; The idea is that gui buffers (trees, buttons, etc) will have text
  23. ;; representations of the GUI elements. These routines will replace the text
  24. ;; with an image when images are available.
  25. ;;
  26. ;; This file requires the `image' package if it is available.
  27. (condition-case nil ; for older XEmacs
  28. (require 'image)
  29. (error nil))
  30. ;;; Code:
  31. (defcustom ezimage-use-images (if (featurep 'xemacs)
  32. (and (fboundp 'make-image-specifier)
  33. window-system)
  34. (and (display-images-p)
  35. (image-type-available-p 'xpm)))
  36. "Non-nil means ezimage should display icons."
  37. :group 'ezimage
  38. :version "21.1"
  39. :type 'boolean)
  40. ;;; Create our own version of defimage
  41. (eval-and-compile
  42. (if (featurep 'emacs)
  43. (progn
  44. (defmacro defezimage (variable imagespec docstring)
  45. "Define VARIABLE as an image if `defimage' is not available.
  46. IMAGESPEC is the image data, and DOCSTRING is documentation for the image."
  47. `(progn
  48. (defimage ,variable ,imagespec ,docstring)
  49. (put (quote ,variable) 'ezimage t)))
  50. ;; This hack is for the ezimage install which has an icons directory for
  51. ;; the default icons to be used.
  52. ;; (add-to-list 'load-path
  53. ;; (concat (file-name-directory
  54. ;; (locate-library "ezimage.el"))
  55. ;; "icons"))
  56. )
  57. ;; XEmacs.
  58. (if (not (fboundp 'make-glyph))
  59. (defmacro defezimage (variable _imagespec docstring)
  60. "Don't bother loading up an image...
  61. Argument VARIABLE is the variable to define.
  62. Argument IMAGESPEC is the list defining the image to create.
  63. Argument DOCSTRING is the documentation for VARIABLE."
  64. `(defvar ,variable nil ,docstring))
  65. (defun ezimage-find-image-on-load-path (image)
  66. "Find the image file IMAGE on the load path."
  67. (let ((l (cons
  68. ;; In XEmacs, try the data directory first (for an
  69. ;; install in XEmacs proper.) Search the load
  70. ;; path next (for user installs)
  71. (locate-data-directory "ezimage")
  72. load-path))
  73. (r nil))
  74. (while (and l (not r))
  75. (if (file-exists-p (concat (car l) "/" image))
  76. (setq r (concat (car l) "/" image))
  77. (if (file-exists-p (concat (car l) "/icons/" image))
  78. (setq r (concat (car l) "/icons/" image))
  79. ))
  80. (setq l (cdr l)))
  81. r))
  82. (defun ezimage-convert-emacs21-imagespec-to-xemacs (spec)
  83. "Convert the Emacs21 image SPEC into an XEmacs image spec.
  84. The Emacs 21 spec is what I first learned, and is easy to convert."
  85. (let* ((sl (car spec))
  86. (itype (nth 1 sl))
  87. (ifile (nth 3 sl)))
  88. (vector itype ':file (ezimage-find-image-on-load-path ifile))))
  89. (defmacro defezimage (variable imagespec docstring)
  90. "Define VARIABLE as an image if `defimage' is not available.
  91. IMAGESPEC is the image data, and DOCSTRING is documentation for the image."
  92. `(progn
  93. (defvar ,variable
  94. ;; The Emacs21 version of defimage looks just like the XEmacs image
  95. ;; specifier, except that it needs a :type keyword. If we line
  96. ;; stuff up right, we can use this cheat to support XEmacs specifiers.
  97. (condition-case nil
  98. (make-glyph
  99. (make-image-specifier
  100. (ezimage-convert-emacs21-imagespec-to-xemacs (quote ,imagespec)))
  101. 'buffer)
  102. (error nil))
  103. ,docstring)
  104. (put ',variable 'ezimage t)))
  105. )))
  106. (defezimage ezimage-directory
  107. ((:type xpm :file "ezimage/dir.xpm" :ascent center))
  108. "Image used for empty directories.")
  109. (defezimage ezimage-directory-plus
  110. ((:type xpm :file "ezimage/dir-plus.xpm" :ascent center))
  111. "Image used for closed directories with stuff in them.")
  112. (defezimage ezimage-directory-minus
  113. ((:type xpm :file "ezimage/dir-minus.xpm" :ascent center))
  114. "Image used for open directories with stuff in them.")
  115. (defezimage ezimage-page-plus
  116. ((:type xpm :file "ezimage/page-plus.xpm" :ascent center))
  117. "Image used for closed files with stuff in them.")
  118. (defezimage ezimage-page-minus
  119. ((:type xpm :file "ezimage/page-minus.xpm" :ascent center))
  120. "Image used for open files with stuff in them.")
  121. (defezimage ezimage-page
  122. ((:type xpm :file "ezimage/page.xpm" :ascent center))
  123. "Image used for files with nothing interesting in it.")
  124. (defezimage ezimage-tag
  125. ((:type xpm :file "ezimage/tag.xpm" :ascent center))
  126. "Image used for tags.")
  127. (defezimage ezimage-tag-plus
  128. ((:type xpm :file "ezimage/tag-plus.xpm" :ascent center))
  129. "Image used for closed tag groups.")
  130. (defezimage ezimage-tag-minus
  131. ((:type xpm :file "ezimage/tag-minus.xpm" :ascent center))
  132. "Image used for open tags.")
  133. (defezimage ezimage-tag-gt
  134. ((:type xpm :file "ezimage/tag-gt.xpm" :ascent center))
  135. "Image used for closed tags (with twist arrow).")
  136. (defezimage ezimage-tag-v
  137. ((:type xpm :file "ezimage/tag-v.xpm" :ascent center))
  138. "Image used for open tags (with twist arrow).")
  139. (defezimage ezimage-tag-type
  140. ((:type xpm :file "ezimage/tag-type.xpm" :ascent center))
  141. "Image used for tags that represent a data type.")
  142. (defezimage ezimage-box-plus
  143. ((:type xpm :file "ezimage/box-plus.xpm" :ascent center))
  144. "Image of a closed box.")
  145. (defezimage ezimage-box-minus
  146. ((:type xpm :file "ezimage/box-minus.xpm" :ascent center))
  147. "Image of an open box.")
  148. (defezimage ezimage-mail
  149. ((:type xpm :file "ezimage/mail.xpm" :ascent center))
  150. "Image of an envelope.")
  151. (defezimage ezimage-checkout
  152. ((:type xpm :file "ezimage/checkmark.xpm" :ascent center))
  153. "Image representing a checkmark. For files checked out of a VC.")
  154. (defezimage ezimage-object
  155. ((:type xpm :file "ezimage/bits.xpm" :ascent center))
  156. "Image representing bits (an object file.)")
  157. (defezimage ezimage-object-out-of-date
  158. ((:type xpm :file "ezimage/bitsbang.xpm" :ascent center))
  159. "Image representing bits with a ! in it. (An out of data object file.)")
  160. (defezimage ezimage-label
  161. ((:type xpm :file "ezimage/label.xpm" :ascent center))
  162. "Image used for label prefix.")
  163. (defezimage ezimage-lock
  164. ((:type xpm :file "ezimage/lock.xpm" :ascent center))
  165. "Image of a lock. Used for Read Only, or private.")
  166. (defezimage ezimage-unlock
  167. ((:type xpm :file "ezimage/unlock.xpm" :ascent center))
  168. "Image of an unlocked lock.")
  169. (defezimage ezimage-key
  170. ((:type xpm :file "ezimage/key.xpm" :ascent center))
  171. "Image of a key.")
  172. (defezimage ezimage-document-tag
  173. ((:type xpm :file "ezimage/doc.xpm" :ascent center))
  174. "Image used to indicate documentation available.")
  175. (defezimage ezimage-document-plus
  176. ((:type xpm :file "ezimage/doc-plus.xpm" :ascent center))
  177. "Image used to indicate closed documentation.")
  178. (defezimage ezimage-document-minus
  179. ((:type xpm :file "ezimage/doc-minus.xpm" :ascent center))
  180. "Image used to indicate open documentation.")
  181. (defezimage ezimage-info-tag
  182. ((:type xpm :file "ezimage/info.xpm" :ascent center))
  183. "Image used to indicate more information available.")
  184. (defvar ezimage-expand-image-button-alist
  185. '(
  186. ;; here are some standard representations
  187. ("<+>" . ezimage-directory-plus)
  188. ("<->" . ezimage-directory-minus)
  189. ("< >" . ezimage-directory)
  190. ("[+]" . ezimage-page-plus)
  191. ("[-]" . ezimage-page-minus)
  192. ("[?]" . ezimage-page)
  193. ("[ ]" . ezimage-page)
  194. ("{+}" . ezimage-box-plus)
  195. ("{-}" . ezimage-box-minus)
  196. ;; Some vaguely representative entries
  197. ("*" . ezimage-checkout)
  198. ("#" . ezimage-object)
  199. ("!" . ezimage-object-out-of-date)
  200. ("%" . ezimage-lock)
  201. )
  202. "List of text and image associations.")
  203. (defun ezimage-insert-image-button-maybe (start length &optional string)
  204. "Insert an image button based on text starting at START for LENGTH chars.
  205. If buttontext is unknown, just insert that text.
  206. If we have an image associated with it, use that image.
  207. Optional argument STRING is a string upon which to add text properties."
  208. (when ezimage-use-images
  209. (let* ((bt (buffer-substring start (+ length start)))
  210. (a (assoc bt ezimage-expand-image-button-alist)))
  211. ;; Regular images (created with `insert-image' are intangible
  212. ;; which (I suppose) make them more compatible with XEmacs 21.
  213. ;; Unfortunately, there is a giant pile of code dependent on the
  214. ;; underlying text. This means if we leave it tangible, then I
  215. ;; don't have to change said giant piles of code.
  216. (if (and a (symbol-value (cdr a)))
  217. (ezimage-insert-over-text (symbol-value (cdr a))
  218. start
  219. (+ start (length bt))))))
  220. string)
  221. (defun ezimage-image-over-string (string &optional alist)
  222. "Insert over the text in STRING an image found in ALIST.
  223. Return STRING with properties applied."
  224. (if ezimage-use-images
  225. (let ((a (assoc string alist)))
  226. (if (and a (symbol-value (cdr a)))
  227. (ezimage-insert-over-text (symbol-value (cdr a))
  228. 0 (length string)
  229. string)
  230. string))
  231. string))
  232. (defun ezimage-insert-over-text (image start end &optional string)
  233. "Place IMAGE over the text between START and END.
  234. Assumes the image is part of a GUI and can be clicked on.
  235. Optional argument STRING is a string upon which to add text properties."
  236. (when ezimage-use-images
  237. (add-text-properties start end
  238. (if (featurep 'xemacs)
  239. (list 'end-glyph image
  240. 'rear-nonsticky (list 'display)
  241. 'invisible t
  242. 'detachable t)
  243. (list 'display image
  244. 'rear-nonsticky (list 'display)))
  245. string))
  246. string)
  247. (defun ezimage-image-association-dump ()
  248. "Dump out the current state of the Ezimage image alist.
  249. See `ezimage-expand-image-button-alist' for details."
  250. (interactive)
  251. (with-output-to-temp-buffer "*Ezimage Images*"
  252. (with-current-buffer "*Ezimage Images*"
  253. (goto-char (point-max))
  254. (insert "Ezimage image cache.\n\n")
  255. (let ((start (point)) (end nil))
  256. (insert "Image\tText\tImage Name")
  257. (setq end (point))
  258. (insert "\n")
  259. (put-text-property start end 'face 'underline))
  260. (let ((ia ezimage-expand-image-button-alist))
  261. (while ia
  262. (let ((start (point)))
  263. (insert (car (car ia)))
  264. (insert "\t")
  265. (ezimage-insert-image-button-maybe start
  266. (length (car (car ia))))
  267. (insert (car (car ia)) "\t" (format "%s" (cdr (car ia))) "\n"))
  268. (setq ia (cdr ia)))))))
  269. (defun ezimage-image-dump ()
  270. "Dump out the current state of the Ezimage image alist.
  271. See `ezimage-expand-image-button-alist' for details."
  272. (interactive)
  273. (with-output-to-temp-buffer "*Ezimage Images*"
  274. (with-current-buffer "*Ezimage Images*"
  275. (goto-char (point-max))
  276. (insert "Ezimage image cache.\n\n")
  277. (let ((start (point)) (end nil))
  278. (insert "Image\tImage Name")
  279. (setq end (point))
  280. (insert "\n")
  281. (put-text-property start end 'face 'underline))
  282. (let ((ia (ezimage-all-images)))
  283. (while ia
  284. (let ((start (point)))
  285. (insert "cm")
  286. (ezimage-insert-over-text (symbol-value (car ia)) start (point))
  287. (insert "\t" (format "%s" (car ia)) "\n"))
  288. (setq ia (cdr ia)))))))
  289. (defun ezimage-all-images ()
  290. "Return a list of all variables containing ez images."
  291. (let ((ans nil))
  292. (mapatoms (lambda (sym)
  293. (if (get sym 'ezimage) (setq ans (cons sym ans)))))
  294. (setq ans (sort ans (lambda (a b)
  295. (string< (symbol-name a) (symbol-name b)))))
  296. ans))
  297. (provide 'ezimage)
  298. ;;; sb-image.el ends here