*** empty log message ***
[gnus] / lisp / cus-face.el
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.55
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 '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)))
32
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)))
38
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))))
48
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)
53          "No faces"
54          nil))
55       ((string-match "XEmacs" emacs-version)
56        (defalias 'custom-facep 'find-face))
57       (t
58        (defalias 'custom-facep 'facep)))
59
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)))
71             (aset face 0 'face)
72             (aset face 1 name)
73             (let* ((frames (frame-list))
74                    (inhibit-quit t)
75                    (id (internal-next-face-id)))
76               (make-face-internal id)
77               (aset face 2 id)
78               (while frames
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)))
84             ;; add to menu
85             (if (fboundp 'facemenu-add-new-face)
86                 (facemenu-add-new-face name))
87             face))
88       name)))
89
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."
93   :group 'customize
94   :type 'boolean)
95
96 (cond ((fboundp 'initialize-face-resources)
97        ;; Already bound, do nothing.
98        )
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))))
107       (t
108        ;; Too hard to do right on XEmacs.
109        (defalias 'initialize-face-resources 'ignore)))
110
111 (unless (fboundp 'reverse-face)
112   ;; This should be moved to `faces.el'.
113   (if (string-match "XEmacs" emacs-version)
114       ;; Xemacs.
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)))
123     ;; Emacs.
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))
131                                     'foreground-color)
132                     "black"))
133             (bg (or (face-background face frame)
134                     (face-background 'default frame)
135                     (frame-property (or frame (selected-frame))
136                                     'background-color)
137                     "white")))
138         (set-face-foreground face bg frame)
139         (set-face-background face fg frame)))))
140
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."
146   :group 'customize
147   :type '(choice (choice-item dark)
148                  (choice-item light)
149                  (choice-item :tag "default" nil)))
150
151 (eval-and-compile
152   (if (string-match "XEmacs" emacs-version)
153       ;; XEmacs.
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
160                                               'background-mode)
161                               (custom-background-mode frame))))
162     ;; Emacs.
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))))))
170
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)
174     (:underline
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.
184
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.
188
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
191 be changed.")
192
193 ;;; Declaring a face.
194
195 ;;;###autoload
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))
209               frame)
210           ;; Create global face.
211           (make-empty-face face)
212           (custom-face-display-set face value)
213           ;; Create frame local faces
214           (while frames
215             (setq frame (car frames)
216                   frames (cdr 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))
223   face)
224
225 ;;; Font Attributes.
226
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'.
230
231 If FRAME is nil, set the default face."
232   (while atts
233     (let* ((name (nth 0 atts))
234            (value (nth 1 atts))
235            (fun (nth 2 (assq name custom-face-attributes))))
236       (setq atts (cdr (cdr atts)))
237       (condition-case nil
238           (funcall fun face value frame)
239         (error nil)))))
240
241 (defun custom-set-face-bold (face value &optional frame)
242   "Set the bold property of FACE to VALUE."
243   (if value
244       (make-face-bold face frame)
245     (make-face-unbold face frame)))
246
247 (defun custom-set-face-italic (face value &optional frame)
248   "Set the italic property of FACE to VALUE."
249   (if value
250       (make-face-italic face frame)
251     (make-face-unitalic face frame)))
252
253 (when (string-match "XEmacs" emacs-version)
254   ;; Support for special XEmacs font attributes.
255   (autoload 'font-create-object "font" nil)
256
257   (unless (fboundp 'face-font-name)
258     (defun face-font-name (face &rest args)
259       (apply 'face-font face args)))
260
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)))
267
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)))
274
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))))
280
281 ;;; Frames.
282
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)
288     (while spec
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))
297           (unless frame
298             (put face 'custom-face-display display))
299           (setq spec nil))))))
300
301 (defun custom-background-mode (frame)
302   "Kludge to detect background mode for FRAME."
303   (let* ((bg-resource
304           (condition-case ()
305               (x-get-resource ".backgroundMode" "BackgroundMode" 'string)
306             (error nil)))
307          color
308          (mode (cond (bg-resource
309                       (intern (downcase bg-resource)))
310                      ((and (setq color (condition-case ()
311                                            (or (frame-property
312                                                 frame
313                                                 'background-color)
314                                                (color-instance-name
315                                                 (specifier-instance
316                                                  (face-background 'default))))
317                                          (error nil)))
318                            (or (string-match "XEmacs" emacs-version)
319                                window-system)
320                            (< (apply '+ (x-color-values color))
321                               (/ (apply '+ (x-color-values "white"))
322                                  3)))
323                       'dark)
324                      (t 'light))))
325     (modify-frame-parameters frame (list (cons 'background-mode mode)))
326     mode))
327
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.")
333
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."
337   (cond (frame
338          ;; Try to get from cache.
339          (let ((cache (frame-property frame 'custom-properties)))
340            (unless cache
341              ;; Oh well, get it then.
342              (setq cache (custom-extract-frame-properties frame))
343              ;; and cache it...
344              (modify-frame-parameters frame
345                                       (list (cons 'custom-properties cache))))
346            cache))
347         (custom-default-frame-properties)
348         (t
349          (setq custom-default-frame-properties
350                (custom-extract-frame-properties (selected-frame))))))
351
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!
356   (if (eq display t)
357       t
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))
362            (match t)
363            (entries display)
364            entry req options)
365       (while (and entries match)
366         (setq entry (car entries)
367               entries (cdr entries)
368               req (car entry)
369               options (cdr entry)
370               match (cond ((eq req 'type)
371                            (memq type options))
372                           ((eq req 'class)
373                            (memq class options))
374                           ((eq req 'background)
375                            (memq background options))
376                           (t
377                            (error "Unknown req `%S' with options `%S'"
378                                   req options)))))
379       match)))
380
381 (defun custom-relevant-frames ()
382   "List of frames whose custom properties differ from the default."
383   (let ((relevant nil)
384         (default (custom-get-frame-properties))
385         (frames (frame-list))
386         frame)
387     (while frames
388       (setq frame (car frames)
389             frames (cdr frames))
390       (unless (equal default (custom-get-frame-properties frame))
391         (push frame relevant)))
392     relevant))
393
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))))
400               (when spec
401                 (custom-face-display-set symbol spec frame)
402                 (initialize-face-resources symbol frame))))
403           (face-list)))
404
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."
408   (unless frame
409     (setq frame (car (frame-list))))
410   (unless (equal (custom-get-frame-properties)
411                  (custom-get-frame-properties frame))
412     (custom-initialize-faces frame)))
413
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))
417
418 ;;; Initializing.
419
420 (and (fboundp 'make-face)
421      (make-face 'custom-face-empty))
422
423 ;;;###autoload
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:
427
428   (FACE SPEC [NOW])
429
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.
432
433 See `defface' for the format of SPEC."
434   (while args
435     (let ((entry (car args)))
436       (if (listp entry)
437           (let ((face (nth 0 entry))
438                 (spec (nth 1 entry))
439                 (now (nth 2 entry)))
440             (put face 'saved-face spec)
441             (when now
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))
449               (spec (nth 1 args)))
450           (put face 'saved-face spec))
451         (setq args (cdr (cdr args)))))))
452
453 ;;; The End.
454
455 (provide 'cus-face)
456
457 ;; cus-face.el ends here