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 (if (string-match "XEmacs" emacs-version)
143 (defun custom-extract-frame-properties (frame)
144 "Return a plist with the frame properties of FRAME used by custom."
145 (list 'type (device-type (frame-device frame))
146 'class (device-class (frame-device frame))
147 'background (or custom-background-mode
148 (frame-property frame
150 (custom-background-mode frame))))
152 (defun custom-extract-frame-properties (frame)
153 "Return a plist with the frame properties of FRAME used by custom."
154 (list 'type window-system
155 'class (frame-property frame 'display-type)
156 'background (or custom-background-mode
157 (frame-property frame 'background-mode)
158 (custom-background-mode frame)))))
160 ;;; Declaring a face.
163 (defun custom-declare-face (face spec doc &rest args)
164 "Like `defface', but FACE is evaluated as a normal argument."
165 (when (fboundp 'load-gc)
166 ;; This should be allowed, somehow.
167 (error "Attempt to declare a face during dump"))
168 (unless (get face 'factory-face)
169 (put face 'factory-face spec)
170 (when (fboundp 'facep)
171 (unless (and (custom-facep face)
172 (not (get face 'saved-face)))
173 ;; If the user has already created the face, respect that.
174 (let ((value (or (get face 'saved-face) spec))
175 (frames (custom-relevant-frames))
177 ;; Create global face.
178 (make-empty-face face)
179 (custom-face-display-set face value)
180 ;; Create frame local faces
182 (setq frame (car frames)
184 (custom-face-display-set face value frame))
185 (initialize-face-resources face))))
186 (when (and doc (null (face-doc-string face)))
187 (set-face-doc-string face doc))
188 (custom-handle-all-keywords face args 'custom-face)
189 (run-hooks 'custom-define-hook))
194 (defun custom-face-attribites-set (face frame &rest atts)
195 "For FACE on FRAME set the attributes [KEYWORD VALUE]....
196 Each keyword should be listed in `custom-face-attributes'.
198 If FRAME is nil, set the default face."
200 (let* ((name (nth 0 atts))
202 (fun (nth 2 (assq name custom-face-attributes))))
203 (setq atts (cdr (cdr atts)))
205 (funcall fun face value frame)
208 (defconst custom-face-attributes
209 '((:bold (toggle :format "Bold: %[%v%]\n") custom-set-face-bold)
210 (:italic (toggle :format "Italic: %[%v%]\n") custom-set-face-italic)
212 (toggle :format "Underline: %[%v%]\n") set-face-underline-p)
213 (:foreground (color :tag "Foreground") set-face-foreground)
214 (:background (color :tag "Background") set-face-background)
215 (:reverse (const :format "Reverse Video\n" t)
216 (lambda (face value &optional frame)
217 ;; We don't use VALUE.
218 (reverse-face face frame)))
219 (:stipple (editable-field :format "Stipple: %v") set-face-stipple))
220 "Alist of face attributes.
222 The elements are of the form (KEY TYPE SET) where KEY is a symbol
223 identifying the attribute, TYPE is a widget type for editing the
224 attibute, SET is a function for setting the attribute value.
226 The SET function should take three arguments, the face to modify, the
227 value of the attribute, and optionally the frame where the face should
230 (defun custom-set-face-bold (face value &optional frame)
231 "Set the bold property of FACE to VALUE."
233 (make-face-bold face frame)
234 (make-face-unbold face frame)))
236 (defun custom-set-face-italic (face value &optional frame)
237 "Set the italic property of FACE to VALUE."
239 (make-face-italic face frame)
240 (make-face-unitalic face frame)))
242 (when (string-match "XEmacs" emacs-version)
243 ;; Support for special XEmacs font attributes.
244 (autoload 'font-create-object "font" nil)
246 (unless (fboundp 'face-font-name)
247 (defun face-font-name (face &rest args)
248 (apply 'face-font face args)))
250 (defun custom-set-face-font-size (face size &rest args)
251 "Set the font of FACE to SIZE"
252 (let* ((font (apply 'face-font-name face args))
253 (fontobj (font-create-object font)))
254 (set-font-size fontobj size)
255 (apply 'set-face-font face fontobj args)))
257 (defun custom-set-face-font-family (face family &rest args)
258 "Set the font of FACE to FAMILY"
259 (let* ((font (apply 'face-font-name face args))
260 (fontobj (font-create-object font)))
261 (set-font-family fontobj family)
262 (apply 'set-face-font face fontobj args)))
264 (nconc custom-face-attributes
265 '((:family (editable-field :format "Family: %v")
266 custom-set-face-font-family)
267 (:size (editable-field :format "Size: %v")
268 custom-set-face-font-size))))
272 (defun custom-face-display-set (face spec &optional frame)
273 "Set FACE to the attributes to the first matching entry in SPEC.
274 Iff optional FRAME is non-nil, set it for that frame only.
275 See `defface' for information about SPEC."
276 (when (fboundp 'make-face)
278 (let* ((entry (car spec))
279 (display (nth 0 entry))
280 (atts (nth 1 entry)))
281 (setq spec (cdr spec))
282 (when (custom-display-match-frame display frame)
283 ;; Avoid creating frame local duplicates of the global face.
284 (unless (and frame (eq display (get face 'custom-face-display)))
285 (apply 'custom-face-attribites-set face frame atts))
287 (put face 'custom-face-display display))
290 (defcustom custom-background-mode nil
291 "The brightness of the background.
292 Set this to the symbol dark if your background color is dark, light if
293 your background is light, or nil (default) if you want Emacs to
294 examine the brightness for you."
296 :type '(choice (choice-item dark)
298 (choice-item :tag "default" nil)))
300 (defun custom-background-mode (frame)
301 "Kludge to detect background mode for FRAME."
304 (x-get-resource ".backgroundMode" "BackgroundMode" 'string)
307 (mode (cond (bg-resource
308 (intern (downcase bg-resource)))
309 ((and (setq color (condition-case ()
315 (face-background 'default))))
317 (or (string-match "XEmacs" emacs-version)
319 (< (apply '+ (x-color-values color))
320 (/ (apply '+ (x-color-values "white"))
324 (modify-frame-parameters frame (list (cons 'background-mode mode)))
327 (defvar custom-default-frame-properties nil
328 "The frame properties used for the global faces.
329 Frames who doesn't match these propertiess should have frame local faces.
330 The value should be nil, if uninitialized, or a plist otherwise.
331 See `defface' for a list of valid keys and values for the plist.")
333 (defun custom-get-frame-properties (&optional frame)
334 "Return a plist with the frame properties of FRAME used by custom.
335 If FRAME is nil, return the default frame properties."
337 ;; Try to get from cache.
338 (let ((cache (frame-property frame 'custom-properties)))
340 ;; Oh well, get it then.
341 (setq cache (custom-extract-frame-properties frame))
343 (modify-frame-parameters frame
344 (list (cons 'custom-properties cache))))
346 (custom-default-frame-properties)
348 (setq custom-default-frame-properties
349 (custom-extract-frame-properties (selected-frame))))))
351 (defun custom-display-match-frame (display frame)
352 "Non-nil iff DISPLAY matches FRAME.
353 If FRAME is nil, the current FRAME is used."
354 ;; This is a kludge to get started, we really should use specifiers!
357 (let* ((props (custom-get-frame-properties frame))
358 (type (plist-get props 'type))
359 (class (plist-get props 'class))
360 (background (plist-get props 'background))
364 (while (and entries match)
365 (setq entry (car entries)
366 entries (cdr entries)
369 match (cond ((eq req 'type)
372 (memq class options))
373 ((eq req 'background)
374 (memq background options))
376 (error "Unknown req `%S' with options `%S'"
380 (defun custom-relevant-frames ()
381 "List of frames whose custom properties differ from the default."
383 (default (custom-get-frame-properties))
384 (frames (frame-list))
387 (setq frame (car frames)
389 (unless (equal default (custom-get-frame-properties frame))
390 (push frame relevant)))
393 (defun custom-initialize-faces (&optional frame)
394 "Initialize all custom faces for FRAME.
395 If FRAME is nil or omitted, initialize them for all frames."
396 (mapcar (lambda (symbol)
397 (let ((spec (or (get symbol 'saved-face)
398 (get symbol 'factory-face))))
400 (custom-face-display-set symbol spec frame)
401 (initialize-face-resources symbol frame))))
404 (defun custom-initialize-frame (&optional frame)
405 "Initialize local faces for FRAME if necessary.
406 If FRAME is missing or nil, the first member (frame-list) is used."
408 (setq frame (car (frame-list))))
409 (unless (equal (custom-get-frame-properties)
410 (custom-get-frame-properties frame))
411 (custom-initialize-faces frame)))
413 ;; Enable. This should go away when bundled with Emacs.
414 (unless (string-match "XEmacs" emacs-version)
415 (add-hook 'after-make-frame-hook 'custom-initialize-frame))
419 (and (fboundp 'make-face)
420 (make-face 'custom-face-empty))
423 (defun custom-set-faces (&rest args)
424 "Initialize faces according to user preferences.
425 The arguments should be a list where each entry has the form:
429 SPEC will be stored as the saved value for FACE. If NOW is present
430 and non-nil, FACE will also be created according to SPEC.
432 See `defface' for the format of SPEC."
434 (let ((entry (car args)))
436 (let ((face (nth 0 entry))
439 (put face 'saved-face spec)
441 (put face 'force-face t)
442 (when (fboundp 'copy-face)
443 (copy-face 'custom-face-empty face))
444 (custom-face-display-set face spec))
445 (setq args (cdr args)))
446 ;; Old format, a plist of FACE SPEC pairs.
447 (let ((face (nth 0 args))
449 (put face 'saved-face spec))
450 (setq args (cdr (cdr args)))))))
456 ;; cus-face.el ends here