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/
18 (eval-and-compile (require 'cl))
22 (if (string-match "XEmacs" emacs-version)
23 (defun custom-face-background (face &optional frame)
25 "Return the background color name of face FACE, or nil if unspecified."
26 (color-instance-name (specifier-instance (face-background face) frame)))
27 (defalias 'custom-face-background 'face-background))
29 (if (string-match "XEmacs" emacs-version)
30 (defun custom-face-foreground (face &optional frame)
32 "Return the background color name of face FACE, or nil if unspecified."
33 (color-instance-name (specifier-instance (face-foreground face) frame)))
34 (defalias 'custom-face-foreground 'face-foreground))
37 (unless (fboundp 'frame-property)
38 ;; XEmacs function missing in Emacs 19.34.
39 (defun frame-property (frame property &optional default)
40 "Return FRAME's value for property PROPERTY."
41 (or (cdr (assq property (frame-parameters frame)))
44 (unless (fboundp 'face-doc-string)
45 ;; XEmacs function missing in Emacs.
46 (defun face-doc-string (face)
47 "Get the documentation string for FACE."
48 (get face 'face-doc-string)))
50 (unless (fboundp 'set-face-doc-string)
51 ;; XEmacs function missing in Emacs.
52 (defun set-face-doc-string (face string)
53 "Set the documentation string for FACE to STRING."
54 (put face 'face-doc-string string)))
56 (when (and (not (fboundp 'set-face-stipple))
57 (fboundp 'set-face-background-pixmap))
58 ;; Emacs function missing in XEmacs 19.15.
59 (defun set-face-stipple (face pixmap &optional frame)
60 ;; Written by Kyle Jones.
61 "Change the stipple pixmap of face FACE to PIXMAP.
62 PIXMAP should be a string, the name of a file of pixmap data.
63 The directories listed in the `x-bitmap-file-path' variable are searched.
65 Alternatively, PIXMAP may be a list of the form (WIDTH HEIGHT DATA)
66 where WIDTH and HEIGHT are the size in pixels,
67 and DATA is a string, containing the raw bits of the bitmap.
69 If the optional FRAME argument is provided, change only
70 in that frame; otherwise change each frame."
71 (while (not (find-face face))
72 (setq face (signal 'wrong-type-argument (list 'facep face))))
73 (while (cond ((stringp pixmap)
74 (unless (file-readable-p pixmap)
75 (setq pixmap (vector 'xbm ':file pixmap)))
77 ((and (consp pixmap) (= (length pixmap) 3))
78 (setq pixmap (vector 'xbm ':data pixmap))
81 (setq pixmap (signal 'wrong-type-argument
82 (list 'stipple-pixmap-p pixmap))))
83 (while (and frame (not (framep frame)))
84 (setq frame (signal 'wrong-type-argument (list 'framep frame))))
85 (set-face-background-pixmap face pixmap frame))))
87 (unless (fboundp 'x-color-values)
88 ;; Emacs function missing in XEmacs 19.14.
89 (defun x-color-values (color &optional frame)
90 "Return a description of the color named COLOR on frame FRAME.
91 The value is a list of integer RGB values--(RED GREEN BLUE).
92 These values appear to range from 0 to 65280 or 65535, depending
93 on the system; white is (65280 65280 65280) or (65535 65535 65535).
94 If FRAME is omitted or nil, use the selected frame."
95 (color-instance-rgb-components (make-color-instance color))))
97 ;; XEmacs and Emacs have different definitions of `facep'.
98 ;; The Emacs definition is the useful one, so emulate that.
99 (cond ((not (fboundp 'facep))
100 (defun custom-facep (face)
103 ((string-match "XEmacs" emacs-version)
104 (defalias 'custom-facep 'find-face))
106 (defalias 'custom-facep 'facep)))
108 (unless (fboundp 'make-empty-face)
109 ;; This should be moved to `faces.el'.
110 (if (string-match "XEmacs" emacs-version)
111 ;; Give up for old XEmacs pre 19.15/20.1.
112 (defalias 'make-empty-face 'make-face)
113 ;; Define for Emacs pre 19.35.
114 (defun make-empty-face (name)
115 "Define a new FACE on all frames, ignoring X resources."
116 (interactive "SMake face: ")
117 (or (internal-find-face name)
118 (let ((face (make-vector 8 nil)))
121 (let* ((frames (frame-list))
123 (id (internal-next-face-id)))
124 (make-face-internal id)
127 (set-frame-face-alist (car frames)
128 (cons (cons name (copy-sequence face))
129 (frame-face-alist (car frames))))
130 (setq frames (cdr frames)))
131 (setq global-face-data (cons (cons name face) global-face-data)))
133 (if (fboundp 'facemenu-add-new-face)
134 (facemenu-add-new-face name))
138 (defcustom initialize-face-resources t
139 "If non nil, allow X resources to initialize face properties.
140 This only affects faces declared with `defface', and only NT or X11 frames."
144 (cond ((fboundp 'initialize-face-resources)
145 ;; Already bound, do nothing.
147 ((fboundp 'make-face-x-resource-internal)
148 ;; Emacs or new XEmacs.
149 (defun initialize-face-resources (face &optional frame)
150 "Initialize face according to the X11 resources.
151 This might overwrite existing face properties.
152 Does nothing when the variable initialize-face-resources is nil."
153 (when initialize-face-resources
154 (make-face-x-resource-internal face frame t))))
156 ;; Too hard to do right on XEmacs.
157 (defalias 'initialize-face-resources 'ignore)))
159 ;;(if (string-match "XEmacs" emacs-version)
161 ;; (defun custom-invert-face (face &optional frame)
162 ;; "Swap the foreground and background colors of face FACE.
163 ;;If the colors are not specified in the face, use the default colors."
164 ;; (interactive (list (read-face-name "Reverse face: ")))
165 ;; (let ((fg (color-name (face-foreground face frame) frame))
166 ;; (bg (color-name (face-background face frame) frame)))
167 ;; (set-face-foreground face bg frame)
168 ;; (set-face-background face fg frame)))
170 ;; (defun custom-invert-face (face &optional frame)
171 ;; "Swap the foreground and background colors of face FACE.
172 ;;If the colors are not specified in the face, use the default colors."
173 ;; (interactive (list (read-face-name "Reverse face: ")))
174 ;; (let ((fg (or (face-foreground face frame)
175 ;; (face-foreground 'default frame)
176 ;; (frame-property (or frame (selected-frame))
177 ;; 'foreground-color)
179 ;; (bg (or (face-background face frame)
180 ;; (face-background 'default frame)
181 ;; (frame-property (or frame (selected-frame))
182 ;; 'background-color)
184 ;; (set-face-foreground face bg frame)
185 ;; (set-face-background face fg frame))))
187 (defcustom custom-background-mode nil
188 "The brightness of the background.
189 Set this to the symbol dark if your background color is dark, light if
190 your background is light, or nil (default) if you want Emacs to
191 examine the brightness for you."
193 :type '(choice (choice-item dark)
195 (choice-item :tag "default" nil)))
197 (defun custom-background-mode (frame)
198 "Kludge to detect background mode for FRAME."
201 (x-get-resource ".backgroundMode" "BackgroundMode" 'string)
204 (mode (cond (bg-resource
205 (intern (downcase bg-resource)))
206 ((and (setq color (condition-case ()
210 (custom-face-background
213 (or (string-match "XEmacs" emacs-version)
215 (< (apply '+ (x-color-values color))
216 (/ (apply '+ (x-color-values "white"))
220 (modify-frame-parameters frame (list (cons 'background-mode mode)))
224 (if (string-match "XEmacs" emacs-version)
226 (defun custom-extract-frame-properties (frame)
227 "Return a plist with the frame properties of FRAME used by custom."
228 (list 'type (device-type (frame-device frame))
229 'class (device-class (frame-device frame))
230 'background (or custom-background-mode
231 (frame-property frame
233 (custom-background-mode frame))))
235 (defun custom-extract-frame-properties (frame)
236 "Return a plist with the frame properties of FRAME used by custom."
237 (list 'type window-system
238 'class (frame-property frame 'display-type)
239 'background (or custom-background-mode
240 (frame-property frame 'background-mode)
241 (custom-background-mode frame))))))
243 ;;; Declaring a face.
246 (defun custom-declare-face (face spec doc &rest args)
247 "Like `defface', but FACE is evaluated as a normal argument."
248 (when (fboundp 'load-gc)
249 ;; This should be allowed, somehow.
250 (error "Attempt to declare a face during dump"))
251 (unless (get face 'factory-face)
252 (put face 'factory-face spec)
253 (when (fboundp 'facep)
254 (unless (custom-facep face)
255 ;; If the user has already created the face, respect that.
256 (let ((value (or (get face 'saved-face) spec))
257 (frames (custom-relevant-frames))
259 ;; Create global face.
260 (make-empty-face face)
261 (custom-face-display-set face value)
262 ;; Create frame local faces
264 (setq frame (car frames)
266 (custom-face-display-set face value frame))
267 (initialize-face-resources face))))
268 (when (and doc (null (face-doc-string face)))
269 (set-face-doc-string face doc))
270 (custom-handle-all-keywords face args 'custom-face)
271 (run-hooks 'custom-define-hook))
276 (defconst custom-face-attributes
277 '((:bold (toggle :format "Bold: %[%v%]\n"
278 :help-echo "Control whether a bold font should be used.")
279 custom-set-face-bold)
280 (:italic (toggle :format "Italic: %[%v%]\n"
282 Control whether an italic font should be used.")
283 custom-set-face-italic)
284 (:underline (toggle :format "Underline: %[%v%]\n"
286 Control whether the text should be underlined.")
289 (:foreground (color :tag "Foreground"
291 :help-echo "Set foreground color.")
293 custom-face-foreground)
294 (:background (color :tag "Background"
296 :help-echo "Set background color.")
298 custom-face-background)
299 ;; (:invert (const :format "Invert Face\n"
300 ;; :sibling-args (:help-echo "
301 ;;Reverse the foreground and background color.
302 ;;If you haven't specified them for the face, the default colors will be used.")
304 ;; (lambda (face value &optional frame)
305 ;; ;; We don't use VALUE.
306 ;; (custom-invert-face face frame)))
307 (:stipple (editable-field :format "Stipple: %v"
308 :help-echo "Name of background bitmap file.")
310 "Alist of face attributes.
312 The elements are of the form (KEY TYPE SET GET) where KEY is a symbol
313 identifying the attribute, TYPE is a widget type for editing the
314 attibute, SET is a function for setting the attribute value, and GET is a function for getiing the attribute value.
316 The SET function should take three arguments, the face to modify, the
317 value of the attribute, and optionally the frame where the face should
320 The GET function should take two arguments, the face to examine, and
321 optonally the frame where the face should be examined.")
323 (defun custom-face-attributes-set (face frame &rest atts)
324 "For FACE on FRAME set the attributes [KEYWORD VALUE]....
325 Each keyword should be listed in `custom-face-attributes'.
327 If FRAME is nil, set the default face."
329 (let* ((name (nth 0 atts))
331 (fun (nth 2 (assq name custom-face-attributes))))
332 (setq atts (cdr (cdr atts)))
334 (funcall fun face value frame)
337 (defun custom-face-attributes-get (face frame)
338 "For FACE on FRAME get the attributes [KEYWORD VALUE]....
339 Each keyword should be listed in `custom-face-attributes'.
341 If FRAME is nil, use the default face."
342 (let ((atts custom-face-attributes)
349 (let ((answer (funcall get face frame)))
350 (unless (equal answer (funcall get 'default frame))
351 (when (widget-apply (nth 1 att) :match answer)
352 (setq result (cons (nth 0 att) (cons answer result))))))))
355 (defun custom-set-face-bold (face value &optional frame)
356 "Set the bold property of FACE to VALUE."
358 (make-face-bold face frame)
359 (make-face-unbold face frame)))
361 (defun custom-set-face-italic (face value &optional frame)
362 "Set the italic property of FACE to VALUE."
364 (make-face-italic face frame)
365 (make-face-unitalic face frame)))
367 (when (string-match "XEmacs" emacs-version)
368 ;; Support for special XEmacs font attributes.
369 (autoload 'font-create-object "font" nil)
371 (unless (fboundp 'face-font-name)
372 (defun face-font-name (face &rest args)
373 (apply 'face-font face args)))
375 (defun custom-set-face-font-size (face size &rest args)
376 "Set the font of FACE to SIZE"
377 (let* ((font (apply 'face-font-name face args))
378 (fontobj (font-create-object font)))
379 (set-font-size fontobj size)
380 (apply 'font-set-face-font face fontobj args)))
382 (defun custom-set-face-font-family (face family &rest args)
383 "Set the font of FACE to FAMILY"
384 (let* ((font (apply 'face-font-name face args))
385 (fontobj (font-create-object font)))
386 (set-font-family fontobj family)
387 (apply 'font-set-face-font face fontobj args)))
389 (nconc custom-face-attributes
390 '((:family (editable-field :format "Font Family: %v"
392 Name of font family to use (e.g. times).")
393 custom-set-face-font-family)
394 (:size (editable-field :format "Size: %v"
396 Text size (e.g. 9pt or 2mm).")
397 custom-set-face-font-size))))
401 (defun custom-face-display-set (face spec &optional frame)
402 "Set FACE to the attributes to the first matching entry in SPEC.
403 Iff optional FRAME is non-nil, set it for that frame only.
404 See `defface' for information about SPEC."
405 (when (fboundp 'make-face)
407 (let* ((entry (car spec))
408 (display (nth 0 entry))
409 (atts (nth 1 entry)))
410 (setq spec (cdr spec))
411 (when (custom-display-match-frame display frame)
412 ;; Avoid creating frame local duplicates of the global face.
413 (unless (and frame (eq display (get face 'custom-face-display)))
414 (apply 'custom-face-attributes-set face frame atts))
416 (put face 'custom-face-display display))
419 (defvar custom-default-frame-properties nil
420 "The frame properties used for the global faces.
421 Frames who doesn't match these propertiess should have frame local faces.
422 The value should be nil, if uninitialized, or a plist otherwise.
423 See `defface' for a list of valid keys and values for the plist.")
425 (defun custom-get-frame-properties (&optional frame)
426 "Return a plist with the frame properties of FRAME used by custom.
427 If FRAME is nil, return the default frame properties."
429 ;; Try to get from cache.
430 (let ((cache (frame-property frame 'custom-properties)))
432 ;; Oh well, get it then.
433 (setq cache (custom-extract-frame-properties frame))
435 (modify-frame-parameters frame
436 (list (cons 'custom-properties cache))))
438 (custom-default-frame-properties)
440 (setq custom-default-frame-properties
441 (custom-extract-frame-properties (selected-frame))))))
443 (defun custom-display-match-frame (display frame)
444 "Non-nil iff DISPLAY matches FRAME.
445 If FRAME is nil, the current FRAME is used."
446 ;; This is a kludge to get started, we really should use specifiers!
449 (let* ((props (custom-get-frame-properties frame))
450 (type (plist-get props 'type))
451 (class (plist-get props 'class))
452 (background (plist-get props 'background))
456 (while (and entries match)
457 (setq entry (car entries)
458 entries (cdr entries)
461 match (cond ((eq req 'type)
464 (memq class options))
465 ((eq req 'background)
466 (memq background options))
468 (error "Unknown req `%S' with options `%S'"
472 (defun custom-relevant-frames ()
473 "List of frames whose custom properties differ from the default."
475 (default (custom-get-frame-properties))
476 (frames (frame-list))
479 (setq frame (car frames)
481 (unless (equal default (custom-get-frame-properties frame))
482 (push frame relevant)))
485 (defun custom-initialize-faces (&optional frame)
486 "Initialize all custom faces for FRAME.
487 If FRAME is nil or omitted, initialize them for all frames."
488 (mapcar (lambda (symbol)
489 (let ((spec (or (get symbol 'saved-face)
490 (get symbol 'factory-face))))
492 (custom-face-display-set symbol spec frame)
493 (initialize-face-resources symbol frame))))
496 (defun custom-initialize-frame (&optional frame)
497 "Initialize local faces for FRAME if necessary.
498 If FRAME is missing or nil, the first member of (frame-list) is used."
500 (setq frame (car (frame-list))))
501 (unless (equal (custom-get-frame-properties)
502 (custom-get-frame-properties frame))
503 (custom-initialize-faces frame)))
505 ;; Enable. This should go away when bundled with Emacs.
506 (unless (string-match "XEmacs" emacs-version)
507 (add-hook 'after-make-frame-hook 'custom-initialize-frame))
511 (and (fboundp 'make-face)
512 (make-face 'custom-face-empty))
515 (defun custom-set-faces (&rest args)
516 "Initialize faces according to user preferences.
517 The arguments should be a list where each entry has the form:
521 SPEC will be stored as the saved value for FACE. If NOW is present
522 and non-nil, FACE will also be created according to SPEC.
524 See `defface' for the format of SPEC."
526 (let ((entry (car args)))
528 (let ((face (nth 0 entry))
531 (put face 'saved-face spec)
533 (put face 'force-face t))
534 (when (or now (custom-facep face))
535 (when (fboundp 'copy-face)
536 (copy-face 'custom-face-empty face))
537 (custom-face-display-set face spec))
538 (setq args (cdr args)))
539 ;; Old format, a plist of FACE SPEC pairs.
540 (let ((face (nth 0 args))
542 (put face 'saved-face spec))
543 (setq args (cdr (cdr args)))))))
549 ;; cus-face.el ends here