Source

edit-utils / icon-themes.el

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
;;-------- icon-themes.el
;;  This implements themes for icons in the toolbar
;;  and adds a menu item "icon-themes"
;;  each theme is a list of icon files and sizes
;;  all stored in the file "icon-themes-list.el"
;;  see that file for a template to add themes.
;;
;;  Authors: 
;;   Byrel Mitchell byrel.mitchell@gmail.com
;;   Steve Mitchell smitchel@bnin.net
;;
;; Copyright (C) 2011 Byrel Mitchell <byrel.mitchell@gmail.com> 
;; Copyright (C) 2011 Steven Mitchell <smitchel@bnin.net>
;;
;; 
;; This file is part of XEmacs.

;; XEmacs is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.

;; XEmacs is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with XEmacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Synched up with: Not in FSF.
;;


(require 'menubar)  ;contains add-sub-menu

;---- declare variables for icon-themes ------------------------------

(defvar theme-theme-list nil
  "A list of all currently loaded themes.")
;;----  function definitions -----------------------------------------

;--- General Procedure to update the toolbar:
;--- first save a copy of the default toolbar.  
;--- then remove the toolbar, 
;--- then put the saved copy back in, which updates the toolbar
;--- doesn't seem to be any other way...
(defun theme-update-toolbar ()
  (let ((toolbar (copy-specifier default-toolbar)))
    (remove-specifier default-toolbar)
    (sit-for 0)
    (set-specifier default-toolbar toolbar)))



;--- adds theme-icon-search-path to the data-directory-list path
(defun theme-expand-path (file)
  (setq file (substitute-in-file-name file))
  (if (file-name-absolute-p file)
      (if (file-exists-p file)
	  file
	nil)
    (locate-file file (append theme-icon-search-path data-directory-list)))) ; when this is a package, would this path already be set?



;--- theme check files confirms the list of icons in icon-themes-list-el
;--- to exist on disk so we don't get any missing icon file problems
;;;###autoload
(defun theme-check-files ()
  "Check to make sure all files can be found in all loaded themes."
  (interactive)
  (catch 'check-abort
    (loop for theme-symbol in theme-theme-list
      for theme = (symbol-value theme-symbol)
      for theme-name = (car theme) do
      (loop for (x y . buttons) in (cdr theme) do
	(loop for (target . icons) in buttons do
	  (unless (assq target themeable-icons)
	    (unless (y-or-n-p  (concat (prin1-to-string target) " not in themable icons, in theme " theme-name ". Continue? "))	;or should it be continue checking?
	      (throw 'check-abort nil)))
	  (if (eq (type-of icons) 'string)
	      (setq icons (list icons)))
	  (loop for image in icons do
	    (when image
	      (unless (theme-expand-path image)
		(unless (y-or-n-p  (concat (prin1-to-string image) " (from theme " theme-name ") not found in path. Continue? "))
		  (throw 'check-abort nil))))))))
    t))
  

; takes the icons of a given theme/size and makes glyphs of them
(defun theme-load-theme ( theme-list )
  "Loads theme given by `THEME-LIST' into appropriate glyphs."
  (loop for (target . icons) in theme-list
    for target-var = (cdr (assq target themeable-icons))
    for target-icon = (when (boundp target-var)
			(symbol-value target-var)) do
    (when target-var ; Only update icon variable if target is listed in themable icons
      (unless (boundp target-var) ;if not defined, define target variable
	(set target-var nil)) 
      (if (eq (type-of icons) 'string)
	  (setq icons (list icons)))
      (when (> (length icons) (length target-icon)) ;length of our list vs target list, includes "pressed",etc. up to 6 items in list   
	(setf (nthcdr (length target-icon) target-icon) (nthcdr (length target-icon) icons))) ;make sure target list is at least as long as the source list
      (loop for a from 0 to (1- (min (length target-icon) (length icons))) 
	for source = (nth a icons) do
	(if source
	    (if (theme-expand-path source) ;If we can resolve it to a file, use that. If it doesn't look like a filename, use the string as a glyph
		(setf (nth a target-icon) (make-glyph (theme-expand-path source)))
	      (unless (string-match "\\.\\(jpe?g\\|xpm\\|png\\)$" source)
		(setf (nth a target-icon) (make-glyph source))))
	  (setf (nth a target-icon) nil)))))
  (theme-update-toolbar))	      ; at the end, update the toolbar


;;;###autoload
(defun theme-read-theme (theme-list &rest expressions)
  "Used for loading a theme.
A theme definition file should contain this function with the theme as
an argument. If there are any further arguments after a theme definition,
they are evaluated and run as lisp commands."
  (let ((theme-name (downcase (car theme-list))) ;Tango for instance has uppercase "T" in name
	theme-symbol)
    (while (string-match "[ \t\n]" theme-name)
      (setq theme-name (replace-match "-" nil nil theme-name)))
    (setq theme-symbol (intern
			(concat "theme-"
				theme-name
				"-theme-def")))
    (set theme-symbol theme-list)
    (unless (member theme-symbol theme-theme-list)
      (push theme-symbol theme-theme-list)))
  (eval `(progn ,@expressions))) ;the part that evals any additional commands 
					;it is used in the text icons to set the
					; toolbar face to bold.


;--- when a new theme is chosen, this function 
;--- sets the toolbar height and sets the current theme
;    variables and loads the theme.
;;;###autoload
(defun theme-set-theme (symbol theme-symbol &optional x y)
  "Set the customizable variable theme-current-theme.
Also set theme-current-size if `X' and `Y' are specified.
`SYMBOL' is ignored."
  (if (symbol-value theme-symbol)
      (progn
	(unless (and x y)	     ;unless an x and y are specified 
	  (setq x (caadr (symbol-value theme-symbol)) ; get x from 1st size in theme
		y (cadadr (symbol-value theme-symbol)))) ;get y from first size in theme
	(let ((oursize
	       (loop for (newx newy . icon-list) in (cdr (symbol-value theme-symbol))
		 until (and (eq x newx)	;once we find a size that matches
			    (eq y newy))                 
		 finally return icon-list))) ;return the size we found
	  (theme-load-theme oursize)	;loads the theme with the size
	  (set-default 'theme-current-theme theme-symbol) ; set name of current theme
	  (set-specifier default-toolbar-height (+ 4 y)) ; set toolbar height to match icon height
	  (set-default 'theme-current-size (cons x y)))) ; set current size of theme
    (when theme-symbol
      (setq theme-current-theme theme-symbol)) ;update current theme name
    (when x
      (setf (car theme-current-size) x)) ;update width of icons
    (when y
      (setf (cdr theme-current-size) y)))) ;update height of icons
      


;--- theme-set-size sets the size of the current theme
;--- and checks that it is a legitimate size
;;;###autoload
(defun theme-set-size (symbol size)
  "Setting function for `theme-current-size' customizable variable."
  (if (zerop (loop for (newx newy . icon-list) in (cdr (symbol-value theme-current-theme))
	       count (and (eq (car size) newx)
			  (eq (cdr size) newy))))
      (message "No such size found")
    (setq theme-current-size size)))


;--- theme-load-menu adds the new menu-item "icon-themes"
;--- the "current: <theme name>" is added to the title line 
;      of the submenu that lists available themes (by reading
;      the file "theme-theme-list.el").
;--- the section below starting with "collect (vector..."
;      is to populate a sub-menu for each theme, 
;      showing sizes available in that theme      
;
;;;###autoload
(defun theme-load-menu (menu-path &optional before)
  "Adds an icon-themes menu to the toolbar.
`menu-path' is a list representing the menu to add the icon theme menu
to. `before' is a string indicating what element to add it before. For
more details, see `current-menubar'."
  (add-submenu menu-path
	       '( "Icon Themes" :filter
		  (lambda (list)
		    (cons [ "Current: " :suffix (or (car (symbol-value theme-current-theme) ) "none")] ;shows name of cur. theme (or none) 
					;in submenu title
			  (loop for theme-symbol in theme-theme-list
			    for theme = (symbol-value theme-symbol)
			    collect (cons (car theme) ;
					  (loop for (x yo) in (cdr theme)
					    for y = (eval yo)
					    collect (vector (format "%i x %i" x y) ; assemble a string of x y sizes
							    `(theme-set-theme nil (quote ,theme-symbol) ,x ,y)    
							    :style 'radio :selected `(and (eq (quote ,theme-symbol) theme-current-theme)
										      (eq ,x (car theme-current-size))
										      (eq ,y (cdr theme-current-size))))))))))
	       before))


(theme-read-theme
'("Text-Icons"
   (32 (+ 10 (string-to-number  (custom-face-font-size 'toolbar )))       
                ; adjust 2nd number above for button height w/your font
                ;height needs to be 4-6 pixels higher than font height
                ;or text won't show in the toolbar buttons, 
                ;but buttons will still work 
     (open	   " Open " nil nil nil nil nil)   ; to compact the length of toolbar
     (dired	   " Dired " nil nil nil nil nil)  ; remove the leading/trailing spaces
     (save         " Save " nil nil nil nil nil)   ; and abbrev. the strings as desired.
     (print        " Print " nil nil nil nil nil)
     (cut	   " Cut " nil nil nil nil nil)
     (copy	   " Copy " nil nil nil nil nil)
     (paste    	   " Paste " nil nil nil nil nil)
     (prev-window  " Prev-win " nil nil nil nil nil)
     (next-window  " Next-win " nil nil nil nil nil)
     (replace      " Find-Replace " nil nil nil nil nil)
     (spell        " Spell " nil nil nil nil nil)
     (undo         " Undo " nil nil nil nil nil)
     (compile      " Compile " nil nil nil nil nil)
     (debug        " Debug " nil nil nil nil nil)
     (email        " Email " nil nil nil nil nil)
     (info         " Info " nil nil nil nil nil)
     (news-reader  " News-Reader " nil nil nil nil nil)))
      ;after the list of icons, any lisp commands here will be eval'd 
     (custom-set-face-bold 'toolbar t ))    ;set toolbar face, bold on


(theme-read-theme
 '("Default-Icons"
   (28 (cond ((specifier-instance toolbar-buttons-captioned-p) 38) (30))
       (open  "toolbar/file-up.xpm"
	      nil 
	      "toolbar/file-xx.xpm"
	      "toolbar/file-cap-up.xpm"
	      nil
	      "toolbar/file-cap-xx.xpm")
       (dired "toolbar/folder-up.xpm"
	      nil 
	      "toolbar/folder-xx.xpm"
	      "toolbar/folder-cap-up.xpm"
	      nil
	      "toolbar/folder-cap-xx.xpm")
       (save  "toolbar/disk-up.xpm"
	      nil 
	      "toolbar/disk-xx.xpm"
	      "toolbar/disk-cap-up.xpm"
	      nil
	      "toolbar/disk-cap-xx.xpm")
       (print "toolbar/printer-up.xpm"
	      nil 
	      "toolbar/printer-xx.xpm"
	      "toolbar/printer-cap-up.xpm"
	      nil
	      "toolbar/printer-cap-xx.xpm")
       (cut	  "toolbar/cut-up.xpm"
		  nil 
		  "toolbar/cut-xx.xpm"
		  "toolbar/cut-cap-up.xpm"
		  nil
		  "toolbar/cut-cap-xx.xpm")
       (copy  "toolbar/copy-up.xpm"
	      nil 
	      "toolbar/copy-xx.xpm"
	      "toolbar/copy-cap-up.xpm"
	      nil
	      "toolbar/copy-cap-xx.xpm")
       (paste "toolbar/paste-up.xpm"
	      nil 
	      "toolbar/paste-xx.xpm"
	      "toolbar/paste-cap-up.xpm"
	      nil
	      "toolbar/paste-cap-xx.xpm")
       (prev-window "toolbar/last-win-up.xpm"
		    nil 
		    "toolbar/last-win-xx.xpm"
		    "toolbar/last-win-cap-up.xpm"
		    nil
		    "toolbar/last-win-cap-xx.xpm")
       (next-window "toolbar/next-win-up.xpm"
		    nil 
		    "toolbar/next-win-xx.xpm"
		    "toolbar/next-win-cap-up.xpm"
		    nil
		    "toolbar/next-win-cap-xx.xpm")
       (replace  "toolbar/replace-up.xpm"
		 nil 
		 "toolbar/replace-xx.xpm"
		 "toolbar/replace-cap-up.xpm"
		 nil
		 "toolbar/replace-cap-xx.xpm")
       (spell    "toolbar/spell-up.xpm"
		 nil 
		 "toolbar/spell-xx.xpm"
		 "toolbar/spell-cap-up.xpm"
		 nil
		 "toolbar/spell-cap-xx.xpm")
       (undo     "toolbar/undo-up.xpm"
		 nil 
		 "toolbar/undo-xx.xpm"
		 "toolbar/undo-cap-up.xpm"
		 nil
		 "toolbar/undo-cap-xx.xpm")
       (info     "toolbar/info-def-up.xpm"
		 nil 
		 "toolbar/info-xx.xpm"
		 "toolbar/info-cap-up.xpm"
		 nil
		 "toolbar/info-cap-xx.xpm")
       (compile  "toolbar/compile-up.xpm"
		 nil 
		 "toolbar/compile-xx.xpm"
		 "toolbar/compile-cap-up.xpm"
		 nil
		 "toolbar/compile-cap-xx.xpm")
       (debug    "toolbar/debug-up.xpm"
		 nil 
		 "toolbar/debug-xx.xpm"
		 "toolbar/debug-cap-up.xpm"
		 nil
		 "toolbar/debug-cap-xx.xpm")
       (email    "toolbar/mail-up.xpm"
		 nil 
		 "toolbar/mail-xx.xpm"
		 "toolbar/mail-cap-up.xpm"
		 nil
		 "toolbar/mail-cap-xx.xpm")
       (news-reader "toolbar/news-up.xpm"
		    nil 
		    "toolbar/news-xx.xpm"
		    "toolbar/news-cap-up.xpm"
		    nil
		    "toolbar/news-cap-xx.xpm"))))

;;;;--- initialize icon-themes ------------------------------------------

;;;###autoload
(theme-load-menu '("Options") "Display") 


;;;;---- customize group and variables for icon-themes ------------------------------

(defgroup icon-theme nil
  "Icon theme customize group")

(defcustom theme-current-size '(28 28)
  "The current icon theme size.
Should be a list of two numbers, (x y)."
  :type '(cons integer integer)
  :version "21.5"
  :set (lambda (unused size)
	 (theme-set-size nil size))
  :group 'icon-theme
  :initialize 'custom-initialize-default)


(defcustom theme-current-theme 'theme-default-icons-theme-def
  "Current icon theme."
  :initialize 'custom-initialize-default
  :type 'variable
  :version "21.5"
  :set (lambda (unused theme)
	 (theme-set-theme nil theme))
  :group 'icon-theme)


(defcustom theme-icon-search-path '("/usr/local/share/xemacs/xemacs-packages/etc/icon-themes")
  "A list of directories to search for icons with relative path names.
This is in addition to the contents of 'data-directory-list'."
  :type '(repeat :tag "Path" (string :tag "Directory"))
  :version "21.5")

(defcustom themeable-icons '((open . toolbar-file-icon)
			     (dired . toolbar-folder-icon)
			     (save . toolbar-disk-icon)
			     (print . toolbar-printer-icon)
			     (cut . toolbar-cut-icon)
			     (copy . toolbar-copy-icon)
			     (paste . toolbar-paste-icon)
			     (prev-window . toolbar-last-win-icon)
			     (next-window . toolbar-next-win-icon)
			     (replace . toolbar-replace-icon)
			     (spell . toolbar-spell-icon)
			     (undo . toolbar-undo-icon)
			     (compile . toolbar-compile-icon)
			     (debug . toolbar-debug-icon)
			     (email . toolbar-mail-icon)
			     (calc . toolbar-calc-icon)
			     (news-reader . toolbar-news-icon)
			     (info . toolbar-info-icon)
			     (info-exit . info::toolbar-exit-icon)
			     (info-next . info::toolbar-next-icon)
			     (info-prev . info::toolbar-prev-icon)
			     (info-up . info::toolbar-up-icon)
			     (xpm-show-chars . xpm-show-chars-icon)
			     (xpm-hide-chars . xpm-hide-chars-icon)
			     (xpm-add-color . xpm-color-icon)
			     (xpm-crop . xpm-crop-icon)
			     (xpm-pad . xpm-enlarge-icon)
			     (xpm-help . xpm-help-icon)
			     (xpm-mirror-h . xpm-m-horiz-icon)
			     (xpm-mirror-v . xpm-m-vert-icon)
			     (xpm-rotate-ccw . xpm-r-ccw-icon)
			     (xpm-rotate-cw . xpm-r-cw-icon)
			     (xpm-save . xpm-save-icon)   
			     (xpm-shift-d . xpm-sh-d-icon)
			     (xpm-shift-l . xpm-sh-l-icon)
			     (xpm-shift-r . xpm-sh-r-icon)
			     (xpm-shift-u . xpm-sh-u-icon)
			     (xpm-undo . xpm-undo-icon)
			     (w3-back  . w3-toolbar-back-icon)
			     (w3-find  . w3-toolbar-find-icon)  
			     (w3-forward  . w3-toolbar-forw-icon)
			     (w3-help  . w3-toolbar-help-icon)  
			     (w3-home  . w3-toolbar-home-icon)  
			     (w3-hotlink  . w3-toolbar-hotl-icon)
			     (w3-image  . w3-toolbar-imag-icon)  
			     (w3-open  . w3-toolbar-open-icon)  
			     (w3-print  . w3-toolbar-print-icon) 
			     (w3-reload  . w3-toolbar-reld-icon)  
			     (w3-stop  . w3-toolbar-stop-icon))
  "Association list of icon names to variables they are stored in."
  :type '(repeat
	  (cons
	   (symbol :tag "Name")
	   (variable :tag "Variable")))
  :tag "Themable Icons"
  :version "21.5")


(provide 'icon-themes)

;; icon-themes.el ends here