comparison lisp/custom/cus-face.el @ 28:1917ad0d78d7 r19-15b97

Import from CVS: tag r19-15b97
author cvs
date Mon, 13 Aug 2007 08:51:55 +0200
parents
children ec9a17fef872
comparison
equal deleted inserted replaced
27:0a3286277d9b 28:1917ad0d78d7
1 ;;; cus-face.el -- XEmacs specific custom support.
2 ;;
3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Keywords: help, faces
7 ;; Version: 1.50
8 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
9
10 ;;; Commentary:
11 ;;
12 ;; See `custom.el'.
13
14 ;;; Code:
15
16 (require 'custom)
17
18 ;;; Compatibility.
19
20 (unless (fboundp 'frame-property)
21 ;; XEmacs function missing in Emacs 19.34.
22 (defun frame-property (frame property &optional default)
23 "Return FRAME's value for property PROPERTY."
24 (or (cdr (assq property (frame-parameters frame)))
25 default)))
26
27 (unless (fboundp 'x-color-values)
28 ;; Emacs function missing in XEmacs 19.14.
29 (defun x-color-values (color &optional frame)
30 "Return a description of the color named COLOR on frame FRAME.
31 The value is a list of integer RGB values--(RED GREEN BLUE).
32 These values appear to range from 0 to 65280 or 65535, depending
33 on the system; white is (65280 65280 65280) or (65535 65535 65535).
34 If FRAME is omitted or nil, use the selected frame."
35 (color-instance-rgb-components (make-color-instance color))))
36
37 ;; XEmacs and Emacs have different definitions of `facep'.
38 ;; The Emacs definition is the useful one, so emulate that.
39 (cond ((not (fboundp 'facep))
40 (defun custom-facep (face)
41 "No faces"
42 nil))
43 ((string-match "XEmacs" emacs-version)
44 (defalias 'custom-facep 'find-face))
45 (t
46 (defalias 'custom-facep 'facep)))
47
48 (unless (fboundp 'make-empty-face)
49 ;; This should be moved to `faces.el'.
50 (if (string-match "XEmacs" emacs-version)
51 (defalias 'make-empty-face 'make-face)
52 (defun make-empty-face (name)
53 "Define a new FACE on all frames, ignoring X resources."
54 (interactive "SMake face: ")
55 (or (internal-find-face name)
56 (let ((face (make-vector 8 nil)))
57 (aset face 0 'face)
58 (aset face 1 name)
59 (let* ((frames (frame-list))
60 (inhibit-quit t)
61 (id (internal-next-face-id)))
62 (make-face-internal id)
63 (aset face 2 id)
64 (while frames
65 (set-frame-face-alist (car frames)
66 (cons (cons name (copy-sequence face))
67 (frame-face-alist (car frames))))
68 (setq frames (cdr frames)))
69 (setq global-face-data (cons (cons name face) global-face-data)))
70 ;; add to menu
71 (if (fboundp 'facemenu-add-new-face)
72 (facemenu-add-new-face name))
73 face))
74 name)))
75
76 (defcustom initialize-face-resources t
77 ;; Not implemented in XEmacs.
78 "If non nil, allow X resources to initialize face properties.
79 This only affects faces declared with `defface', and only NT or X11 frames."
80 :group 'customize
81 :type 'boolean)
82
83 (cond ((fboundp 'initialize-face-resources)
84 ;; Already bound, do nothing.
85 )
86 ((fboundp 'make-face-x-resource-internal)
87 ;; Emacs or new XEmacs.
88 (defun initialize-face-resources (face &optional frame)
89 "Initialize face according to the X11 resources.
90 This might overwrite existing face properties.
91 Does nothing when the variable initialize-face-resources is nil."
92 (when initialize-face-resources
93 (make-face-x-resource-internal face frame t))))
94 (t
95 ;; Too hard to do right on XEmacs.
96 (defalias 'initialize-face-resources 'ignore)))
97
98 (if (string-match "XEmacs" emacs-version)
99 (progn
100 (defun custom-extract-frame-properties (frame)
101 "Return a plist with the frame properties of FRAME used by custom."
102 (list 'type (device-type (frame-device frame))
103 'class (device-class (frame-device frame))
104 'background (or custom-background-mode
105 (frame-property frame
106 'background-mode)
107 (custom-background-mode frame))))
108
109 (defun face-documentation (face)
110 "Get the documentation string for FACE."
111 (face-property face 'doc-string))
112
113 (defun set-face-documentation (face string)
114 "Set the documentation string for FACE to STRING."
115 (set-face-property face 'doc-string string)))
116
117 (defun custom-extract-frame-properties (frame)
118 "Return a plist with the frame properties of FRAME used by custom."
119 (list 'type window-system
120 'class (frame-property frame 'display-type)
121 'background (or custom-background-mode
122 (frame-property frame 'background-mode)
123 (custom-background-mode frame))))
124
125 (defun face-documentation (face)
126 "Get the documentation string for FACE."
127 (get face 'face-documentation))
128
129 (defun set-face-documentation (face string)
130 "Set the documentation string for FACE to STRING."
131 (put face 'face-documentation string)))
132
133 ;;; Declaring a face.
134
135 ;;;###autoload
136 (defun custom-declare-face (face spec doc &rest args)
137 "Like `defface', but FACE is evaluated as a normal argument."
138 (when (fboundp 'load-gc)
139 ;; This should be allowed, somehow.
140 (error "Attempt to declare a face during dump"))
141 (unless (get face 'factory-face)
142 (put face 'factory-face spec)
143 (when (fboundp 'facep)
144 (unless (and (custom-facep face)
145 (not (get face 'saved-face)))
146 ;; If the user has already created the face, respect that.
147 (let ((value (or (get face 'saved-face) spec))
148 (frames (custom-relevant-frames))
149 frame)
150 ;; Create global face.
151 (make-empty-face face)
152 (custom-face-display-set face value)
153 ;; Create frame local faces
154 (while frames
155 (setq frame (car frames)
156 frames (cdr frames))
157 (custom-face-display-set face value frame))
158 (initialize-face-resources face))))
159 (when (and doc (null (face-documentation face)))
160 (set-face-documentation face doc))
161 (custom-handle-all-keywords face args 'custom-face)
162 (run-hooks 'custom-define-hook))
163 face)
164
165 ;;; Font Attributes.
166
167 (defun custom-face-attribites-set (face frame &rest atts)
168 "For FACE on FRAME set the attributes [KEYWORD VALUE]....
169 Each keyword should be listed in `custom-face-attributes'.
170
171 If FRAME is nil, set the default face."
172 (while atts
173 (let* ((name (nth 0 atts))
174 (value (nth 1 atts))
175 (fun (nth 2 (assq name custom-face-attributes))))
176 (setq atts (cdr (cdr atts)))
177 (condition-case nil
178 (funcall fun face value frame)
179 (error nil)))))
180
181 (defconst custom-face-attributes
182 '((:bold (toggle :format "Bold: %[%v%]\n") custom-set-face-bold)
183 (:italic (toggle :format "Italic: %[%v%]\n") custom-set-face-italic)
184 (:underline
185 (toggle :format "Underline: %[%v%]\n") set-face-underline-p)
186 (:foreground (color :tag "Foreground") set-face-foreground)
187 (:background (color :tag "Background") set-face-background)
188 (:stipple (editable-field :format "Stipple: %v") set-face-stipple))
189 "Alist of face attributes.
190
191 The elements are of the form (KEY TYPE SET) where KEY is a symbol
192 identifying the attribute, TYPE is a widget type for editing the
193 attibute, SET is a function for setting the attribute value.
194
195 The SET function should take three arguments, the face to modify, the
196 value of the attribute, and optionally the frame where the face should
197 be changed.")
198
199 (defun custom-set-face-bold (face value &optional frame)
200 "Set the bold property of FACE to VALUE."
201 (if value
202 (make-face-bold face frame)
203 (make-face-unbold face frame)))
204
205 (defun custom-set-face-italic (face value &optional frame)
206 "Set the italic property of FACE to VALUE."
207 (if value
208 (make-face-italic face frame)
209 (make-face-unitalic face frame)))
210
211 (when (string-match "XEmacs" emacs-version)
212 ;; Support for special XEmacs font attributes.
213 (autoload 'font-create-object "font" nil)
214
215 (unless (fboundp 'face-font-name)
216 (defun face-font-name (face &rest args)
217 (apply 'face-font face args)))
218
219 (defun custom-set-face-font-size (face size &rest args)
220 "Set the font of FACE to SIZE"
221 (let* ((font (apply 'face-font-name face args))
222 (fontobj (font-create-object font)))
223 (set-font-size fontobj size)
224 (apply 'set-face-font face fontobj args)))
225
226 (defun custom-set-face-font-family (face family &rest args)
227 "Set the font of FACE to FAMILY"
228 (let* ((font (apply 'face-font-name face args))
229 (fontobj (font-create-object font)))
230 (set-font-family fontobj family)
231 (apply 'set-face-font face fontobj args)))
232
233 (nconc custom-face-attributes
234 '((:family (editable-field :format "Family: %v")
235 custom-set-face-font-family)
236 (:size (editable-field :format "Size: %v")
237 custom-set-face-font-size)))
238
239 ;; Disable frame local faces.
240 (setq custom-relevant-frames nil)
241 (remove-hook 'after-make-frame-hook 'custom-initialize-frame))
242
243 ;;; Frames.
244
245 (defun custom-face-display-set (face spec &optional frame)
246 "Set FACE to the attributes to the first matching entry in SPEC.
247 Iff optional FRAME is non-nil, set it for that frame only.
248 See `defface' for information about SPEC."
249 (when (fboundp 'make-face)
250 (while spec
251 (let* ((entry (car spec))
252 (display (nth 0 entry))
253 (atts (nth 1 entry)))
254 (setq spec (cdr spec))
255 (when (custom-display-match-frame display frame)
256 ;; Avoid creating frame local duplicates of the global face.
257 (unless (and frame (eq display (get face 'custom-face-display)))
258 (apply 'custom-face-attribites-set face frame atts))
259 (unless frame
260 (put face 'custom-face-display display))
261 (setq spec nil))))))
262
263 (defcustom custom-background-mode nil
264 "The brightness of the background.
265 Set this to the symbol dark if your background color is dark, light if
266 your background is light, or nil (default) if you want Emacs to
267 examine the brightness for you."
268 :group 'customize
269 :type '(choice (choice-item dark)
270 (choice-item light)
271 (choice-item :tag "default" nil)))
272
273 (defun custom-background-mode (frame)
274 "Kludge to detect background mode for FRAME."
275 (let* ((bg-resource
276 (condition-case ()
277 (x-get-resource ".backgroundMode" "BackgroundMode" 'string)
278 (error nil)))
279 color
280 (mode (cond (bg-resource
281 (intern (downcase bg-resource)))
282 ((and (setq color (condition-case ()
283 (or (frame-property
284 frame
285 'background-color)
286 (color-instance-name
287 (specifier-instance
288 (face-background 'default))))
289 (error nil)))
290 (or (string-match "XEmacs" emacs-version)
291 window-system)
292 (< (apply '+ (x-color-values color))
293 (/ (apply '+ (x-color-values "white"))
294 3)))
295 'dark)
296 (t 'light))))
297 (modify-frame-parameters frame (list (cons 'background-mode mode)))
298 mode))
299
300 (defvar custom-default-frame-properties nil
301 "The frame properties used for the global faces.
302 Frames who doesn't match these propertiess should have frame local faces.
303 The value should be nil, if uninitialized, or a plist otherwise.
304 See `defface' for a list of valid keys and values for the plist.")
305
306 (defun custom-get-frame-properties (&optional frame)
307 "Return a plist with the frame properties of FRAME used by custom.
308 If FRAME is nil, return the default frame properties."
309 (cond (frame
310 ;; Try to get from cache.
311 (let ((cache (frame-property frame 'custom-properties)))
312 (unless cache
313 ;; Oh well, get it then.
314 (setq cache (custom-extract-frame-properties frame))
315 ;; and cache it...
316 (modify-frame-parameters frame
317 (list (cons 'custom-properties cache))))
318 cache))
319 (custom-default-frame-properties)
320 (t
321 (setq custom-default-frame-properties
322 (custom-extract-frame-properties (selected-frame))))))
323
324 (defun custom-display-match-frame (display frame)
325 "Non-nil iff DISPLAY matches FRAME.
326 If FRAME is nil, the current FRAME is used."
327 ;; This is a kludge to get started, we really should use specifiers!
328 (if (eq display t)
329 t
330 (let* ((props (custom-get-frame-properties frame))
331 (type (plist-get props 'type))
332 (class (plist-get props 'class))
333 (background (plist-get props 'background))
334 (match t)
335 (entries display)
336 entry req options)
337 (while (and entries match)
338 (setq entry (car entries)
339 entries (cdr entries)
340 req (car entry)
341 options (cdr entry)
342 match (cond ((eq req 'type)
343 (memq type options))
344 ((eq req 'class)
345 (memq class options))
346 ((eq req 'background)
347 (memq background options))
348 (t
349 (error "Unknown req `%S' with options `%S'"
350 req options)))))
351 match)))
352
353 (defvar custom-relevant-frames t
354 "List of frames whose custom properties differ from the default.")
355
356 (defun custom-relevant-frames ()
357 "List of frames whose custom properties differ from the default."
358 (when (eq custom-relevant-frames t)
359 (setq custom-relevant-frames nil)
360 (let ((default (custom-get-frame-properties))
361 (frames (frame-list))
362 frame)
363 (while frames
364 (setq frame (car frames)
365 frames (cdr frames))
366 (unless (equal default (custom-get-frame-properties frame))
367 (push frame custom-relevant-frames)))))
368 custom-relevant-frames)
369
370 (defun custom-initialize-faces (&optional frame)
371 "Initialize all custom faces for FRAME.
372 If FRAME is nil or omitted, initialize them for all frames."
373 (mapcar (lambda (symbol)
374 (let ((spec (or (get symbol 'saved-face)
375 (get symbol 'factory-face))))
376 (when spec
377 (custom-face-display-set symbol spec frame)
378 (initialize-face-resources symbol frame))))
379 (face-list)))
380
381 (defun custom-initialize-frame (&optional frame)
382 "Initialize local faces for FRAME if necessary.
383 If FRAME is missing or nil, the first member (frame-list) is used."
384 (unless frame
385 (setq frame (car (frame-list))))
386 (unless (equal (custom-get-frame-properties)
387 (custom-get-frame-properties frame))
388 (custom-initialize-faces frame)
389 (custom-relevant-frames)
390 (push frame custom-relevant-frames)))
391
392 ;; Enable. This should go away when bundled with Emacs.
393 (add-hook 'after-make-frame-hook 'custom-initialize-frame)
394
395 ;;; Initializing.
396
397 (and (fboundp 'make-face)
398 (make-face 'custom-face-empty))
399
400 ;;;###autoload
401 (defun custom-set-faces (&rest args)
402 "Initialize faces according to user preferences.
403 The arguments should be a list where each entry has the form:
404
405 (FACE SPEC [NOW])
406
407 SPEC will be stored as the saved value for FACE. If NOW is present
408 and non-nil, FACE will also be created according to SPEC.
409
410 See `defface' for the format of SPEC."
411 (while args
412 (let ((entry (car args)))
413 (if (listp entry)
414 (let ((face (nth 0 entry))
415 (spec (nth 1 entry))
416 (now (nth 2 entry)))
417 (put face 'saved-face spec)
418 (when now
419 (put face 'force-face t)
420 (when (fboundp 'copy-face)
421 (copy-face 'custom-face-empty face))
422 (custom-face-display-set face spec))
423 (setq args (cdr args)))
424 ;; Old format, a plist of FACE SPEC pairs.
425 (let ((face (nth 0 args))
426 (spec (nth 1 args)))
427 (put face 'saved-face spec))
428 (setq args (cdr (cdr args)))))))
429
430 ;;; The End.
431
432 (provide 'cus-face)
433
434 ;; cus-face.el ends here