1 ;;; cus-face.el -- XEmacs specific custom support.
3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Keywords: help, faces
8 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
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)))
27 (unless (fboundp 'face-doc-string)
28 ;; XEmacs function missing in Emacs.
29 (defun face-doc-string (face)
30 "Get the documentation string for FACE."
31 (get face 'face-doc-string)))
33 (unless (fboundp 'set-face-doc-string)
34 ;; XEmacs function missing in Emacs.
35 (defun set-face-doc-string (face string)
36 "Set the documentation string for FACE to STRING."
37 (put face 'face-doc-string string)))
39 (unless (fboundp 'x-color-values)
40 ;; Emacs function missing in XEmacs 19.14.
41 (defun x-color-values (color &optional frame)
42 "Return a description of the color named COLOR on frame FRAME.
43 The value is a list of integer RGB values--(RED GREEN BLUE).
44 These values appear to range from 0 to 65280 or 65535, depending
45 on the system; white is (65280 65280 65280) or (65535 65535 65535).
46 If FRAME is omitted or nil, use the selected frame."
47 (color-instance-rgb-components (make-color-instance color))))
49 ;; XEmacs and Emacs have different definitions of `facep'.
50 ;; The Emacs definition is the useful one, so emulate that.
51 (cond ((not (fboundp 'facep))
52 (defun custom-facep (face)
55 ((string-match "XEmacs" emacs-version)
56 (defalias 'custom-facep 'find-face))
58 (defalias 'custom-facep 'facep)))
60 (unless (fboundp 'make-empty-face)
61 ;; This should be moved to `faces.el'.
62 (if (string-match "XEmacs" emacs-version)
63 ;; Give up for old XEmacs pre 19.15/20.1.
64 (defalias 'make-empty-face 'make-face)
65 ;; Define for Emacs pre 19.35.
66 (defun make-empty-face (name)
67 "Define a new FACE on all frames, ignoring X resources."
68 (interactive "SMake face: ")
69 (or (internal-find-face name)
70 (let ((face (make-vector 8 nil)))
73 (let* ((frames (frame-list))
75 (id (internal-next-face-id)))
76 (make-face-internal id)
79 (set-frame-face-alist (car frames)
80 (cons (cons name (copy-sequence face))
81 (frame-face-alist (car frames))))
82 (setq frames (cdr frames)))
83 (setq global-face-data (cons (cons name face) global-face-data)))
85 (if (fboundp 'facemenu-add-new-face)
86 (facemenu-add-new-face name))
90 (defcustom initialize-face-resources t
91 "If non nil, allow X resources to initialize face properties.
92 This only affects faces declared with `defface', and only NT or X11 frames."
96 (cond ((fboundp 'initialize-face-resources)
97 ;; Already bound, do nothing.
99 ((fboundp 'make-face-x-resource-internal)
100 ;; Emacs or new XEmacs.
101 (defun initialize-face-resources (face &optional frame)
102 "Initialize face according to the X11 resources.
103 This might overwrite existing face properties.
104 Does nothing when the variable initialize-face-resources is nil."
105 (when initialize-face-resources
106 (make-face-x-resource-internal face frame t))))
108 ;; Too hard to do right on XEmacs.
109 (defalias 'initialize-face-resources 'ignore)))
111 (unless (fboundp 'reverse-face)
112 ;; This should be moved to `faces.el'.
113 (if (string-match "XEmacs" emacs-version)
115 (defun reverse-face (face &optional frame)
116 "Swap the foreground and background colors of face FACE.
117 If the colors are not specified in the face, use the default colors."
118 (interactive (list (read-face-name "Reverse face: ")))
119 (let ((fg (color-name (face-foreground face frame) frame))
120 (bg (color-name (face-background face frame) frame)))
121 (set-face-foreground face bg frame)
122 (set-face-background face fg frame)))
124 (defun reverse-face (face &optional frame)
125 "Swap the foreground and background colors of face FACE.
126 If the colors are not specified in the face, use the default colors."
127 (interactive (list (read-face-name "Reverse face: ")))
128 (let ((fg (or (face-foreground face frame)
129 (face-foreground 'default frame)
130 (frame-property (or frame (selected-frame))
133 (bg (or (face-background face frame)
134 (face-background 'default frame)
135 (frame-property (or frame (selected-frame))
138 (set-face-foreground face bg frame)
139 (set-face-background face fg frame)))))
141 (defcustom custom-background-mode nil
142 "The brightness of the background.
143 Set this to the symbol dark if your background color is dark, light if
144 your background is light, or nil (default) if you want Emacs to
145 examine the brightness for you."
147 :type '(choice (choice-item dark)
149 (choice-item :tag "default" nil)))
152 (if (string-match "XEmacs" emacs-version)
154 (defun custom-extract-frame-properties (frame)
155 "Return a plist with the frame properties of FRAME used by custom."
156 (list 'type (device-type (frame-device frame))
157 'class (device-class (frame-device frame))
158 'background (or custom-background-mode
159 (frame-property frame
161 (custom-background-mode frame))))
163 (defun custom-extract-frame-properties (frame)
164 "Return a plist with the frame properties of FRAME used by custom."
165 (list 'type window-system
166 'class (frame-property frame 'display-type)
167 'background (or custom-background-mode
168 (frame-property frame 'background-mode)
169 (custom-background-mode frame))))))
171 (defconst custom-face-attributes
172 '((:bold (toggle :format "Bold: %[%v%]\n") custom-set-face-bold)
173 (:italic (toggle :format "Italic: %[%v%]\n") custom-set-face-italic)
175 (toggle :format "Underline: %[%v%]\n") set-face-underline-p)
176 (:foreground (color :tag "Foreground") set-face-foreground)
177 (:background (color :tag "Background") set-face-background)
178 (:reverse (const :format "Reverse Video\n" t)
179 (lambda (face value &optional frame)
180 ;; We don't use VALUE.
181 (reverse-face face frame)))
182 (:stipple (editable-field :format "Stipple: %v") set-face-stipple))
183 "Alist of face attributes.
185 The elements are of the form (KEY TYPE SET) where KEY is a symbol
186 identifying the attribute, TYPE is a widget type for editing the
187 attibute, SET is a function for setting the attribute value.
189 The SET function should take three arguments, the face to modify, the
190 value of the attribute, and optionally the frame where the face should
193 ;;; Declaring a face.
196 (defun custom-declare-face (face spec doc &rest args)
197 "Like `defface', but FACE is evaluated as a normal argument."
198 (when (fboundp 'load-gc)
199 ;; This should be allowed, somehow.
200 (error "Attempt to declare a face during dump"))
201 (unless (get face 'factory-face)
202 (put face 'factory-face spec)
203 (when (fboundp 'facep)
204 (unless (and (custom-facep face)
205 (not (get face 'saved-face)))
206 ;; If the user has already created the face, respect that.
207 (let ((value (or (get face 'saved-face) spec))
208 (frames (custom-relevant-frames))
210 ;; Create global face.
211 (make-empty-face face)
212 (custom-face-display-set face value)
213 ;; Create frame local faces
215 (setq frame (car frames)
217 (custom-face-display-set face value frame))
218 (initialize-face-resources face))))
219 (when (and doc (null (face-doc-string face)))
220 (set-face-doc-string face doc))
221 (custom-handle-all-keywords face args 'custom-face)
222 (run-hooks 'custom-define-hook))
227 (defun custom-face-attribites-set (face frame &rest atts)
228 "For FACE on FRAME set the attributes [KEYWORD VALUE]....
229 Each keyword should be listed in `custom-face-attributes'.
231 If FRAME is nil, set the default face."
233 (let* ((name (nth 0 atts))
235 (fun (nth 2 (assq name custom-face-attributes))))
236 (setq atts (cdr (cdr atts)))
238 (funcall fun face value frame)
241 (defun custom-set-face-bold (face value &optional frame)
242 "Set the bold property of FACE to VALUE."
244 (make-face-bold face frame)
245 (make-face-unbold face frame)))
247 (defun custom-set-face-italic (face value &optional frame)
248 "Set the italic property of FACE to VALUE."
250 (make-face-italic face frame)
251 (make-face-unitalic face frame)))
253 (when (string-match "XEmacs" emacs-version)
254 ;; Support for special XEmacs font attributes.
255 (autoload 'font-create-object "font" nil)
257 (unless (fboundp 'face-font-name)
258 (defun face-font-name (face &rest args)
259 (apply 'face-font face args)))
261 (defun custom-set-face-font-size (face size &rest args)
262 "Set the font of FACE to SIZE"
263 (let* ((font (apply 'face-font-name face args))
264 (fontobj (font-create-object font)))
265 (set-font-size fontobj size)
266 (apply 'set-face-font face fontobj args)))
268 (defun custom-set-face-font-family (face family &rest args)
269 "Set the font of FACE to FAMILY"
270 (let* ((font (apply 'face-font-name face args))
271 (fontobj (font-create-object font)))
272 (set-font-family fontobj family)
273 (apply 'set-face-font face fontobj args)))
275 (nconc custom-face-attributes
276 '((:family (editable-field :format "Family: %v")
277 custom-set-face-font-family)
278 (:size (editable-field :format "Size: %v")
279 custom-set-face-font-size))))
283 (defun custom-face-display-set (face spec &optional frame)
284 "Set FACE to the attributes to the first matching entry in SPEC.
285 Iff optional FRAME is non-nil, set it for that frame only.
286 See `defface' for information about SPEC."
287 (when (fboundp 'make-face)
289 (let* ((entry (car spec))
290 (display (nth 0 entry))
291 (atts (nth 1 entry)))
292 (setq spec (cdr spec))
293 (when (custom-display-match-frame display frame)
294 ;; Avoid creating frame local duplicates of the global face.
295 (unless (and frame (eq display (get face 'custom-face-display)))
296 (apply 'custom-face-attribites-set face frame atts))
298 (put face 'custom-face-display display))
301 (defun custom-background-mode (frame)
302 "Kludge to detect background mode for FRAME."
305 (x-get-resource ".backgroundMode" "BackgroundMode" 'string)
308 (mode (cond (bg-resource
309 (intern (downcase bg-resource)))
310 ((and (setq color (condition-case ()
316 (face-background 'default))))
318 (or (string-match "XEmacs" emacs-version)
320 (< (apply '+ (x-color-values color))
321 (/ (apply '+ (x-color-values "white"))
325 (modify-frame-parameters frame (list (cons 'background-mode mode)))
328 (defvar custom-default-frame-properties nil
329 "The frame properties used for the global faces.
330 Frames who doesn't match these propertiess should have frame local faces.
331 The value should be nil, if uninitialized, or a plist otherwise.
332 See `defface' for a list of valid keys and values for the plist.")
334 (defun custom-get-frame-properties (&optional frame)
335 "Return a plist with the frame properties of FRAME used by custom.
336 If FRAME is nil, return the default frame properties."
338 ;; Try to get from cache.
339 (let ((cache (frame-property frame 'custom-properties)))
341 ;; Oh well, get it then.
342 (setq cache (custom-extract-frame-properties frame))
344 (modify-frame-parameters frame
345 (list (cons 'custom-properties cache))))
347 (custom-default-frame-properties)
349 (setq custom-default-frame-properties
350 (custom-extract-frame-properties (selected-frame))))))
352 (defun custom-display-match-frame (display frame)
353 "Non-nil iff DISPLAY matches FRAME.
354 If FRAME is nil, the current FRAME is used."
355 ;; This is a kludge to get started, we really should use specifiers!
358 (let* ((props (custom-get-frame-properties frame))
359 (type (plist-get props 'type))
360 (class (plist-get props 'class))
361 (background (plist-get props 'background))
365 (while (and entries match)
366 (setq entry (car entries)
367 entries (cdr entries)
370 match (cond ((eq req 'type)
373 (memq class options))
374 ((eq req 'background)
375 (memq background options))
377 (error "Unknown req `%S' with options `%S'"
381 (defun custom-relevant-frames ()
382 "List of frames whose custom properties differ from the default."
384 (default (custom-get-frame-properties))
385 (frames (frame-list))
388 (setq frame (car frames)
390 (unless (equal default (custom-get-frame-properties frame))
391 (push frame relevant)))
394 (defun custom-initialize-faces (&optional frame)
395 "Initialize all custom faces for FRAME.
396 If FRAME is nil or omitted, initialize them for all frames."
397 (mapcar (lambda (symbol)
398 (let ((spec (or (get symbol 'saved-face)
399 (get symbol 'factory-face))))
401 (custom-face-display-set symbol spec frame)
402 (initialize-face-resources symbol frame))))
405 (defun custom-initialize-frame (&optional frame)
406 "Initialize local faces for FRAME if necessary.
407 If FRAME is missing or nil, the first member (frame-list) is used."
409 (setq frame (car (frame-list))))
410 (unless (equal (custom-get-frame-properties)
411 (custom-get-frame-properties frame))
412 (custom-initialize-faces frame)))
414 ;; Enable. This should go away when bundled with Emacs.
415 (unless (string-match "XEmacs" emacs-version)
416 (add-hook 'after-make-frame-hook 'custom-initialize-frame))
420 (and (fboundp 'make-face)
421 (make-face 'custom-face-empty))
424 (defun custom-set-faces (&rest args)
425 "Initialize faces according to user preferences.
426 The arguments should be a list where each entry has the form:
430 SPEC will be stored as the saved value for FACE. If NOW is present
431 and non-nil, FACE will also be created according to SPEC.
433 See `defface' for the format of SPEC."
435 (let ((entry (car args)))
437 (let ((face (nth 0 entry))
440 (put face 'saved-face spec)
442 (put face 'force-face t)
443 (when (fboundp 'copy-face)
444 (copy-face 'custom-face-empty face))
445 (custom-face-display-set face spec))
446 (setq args (cdr args)))
447 ;; Old format, a plist of FACE SPEC pairs.
448 (let ((face (nth 0 args))
450 (put face 'saved-face spec))
451 (setq args (cdr (cdr args)))))))
457 ;; cus-face.el ends here