a4580a93f80f077b0b63baae2359aef5399c2f97
[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 (if (string-match "XEmacs" emacs-version)
142     ;; XEmacs.
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
149                                             'background-mode)
150                             (custom-background-mode frame))))
151   ;; Emacs.
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)))))  
159
160 ;;; Declaring a face.
161
162 ;;;###autoload
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))
176               frame)
177           ;; Create global face.
178           (make-empty-face face)
179           (custom-face-display-set face value)
180           ;; Create frame local faces
181           (while frames
182             (setq frame (car frames)
183                   frames (cdr 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))
190   face)
191
192 ;;; Font Attributes.
193
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'.
197
198 If FRAME is nil, set the default face."
199   (while atts 
200     (let* ((name (nth 0 atts))
201            (value (nth 1 atts))
202            (fun (nth 2 (assq name custom-face-attributes))))
203       (setq atts (cdr (cdr atts)))
204       (condition-case nil
205           (funcall fun face value frame)
206         (error nil)))))
207
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)
211     (:underline
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. 
221
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.
225
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
228 be changed.")
229
230 (defun custom-set-face-bold (face value &optional frame)
231   "Set the bold property of FACE to VALUE."
232   (if value
233       (make-face-bold face frame)
234     (make-face-unbold face frame)))
235
236 (defun custom-set-face-italic (face value &optional frame)
237   "Set the italic property of FACE to VALUE."
238   (if value
239       (make-face-italic face frame)
240     (make-face-unitalic face frame)))
241
242 (when (string-match "XEmacs" emacs-version)
243   ;; Support for special XEmacs font attributes.
244   (autoload 'font-create-object "font" nil)
245
246   (unless (fboundp 'face-font-name)
247     (defun face-font-name (face &rest args)
248       (apply 'face-font face args)))
249
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)))
256
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)))
263
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))))
269
270 ;;; Frames.
271
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)
277     (while spec 
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))
286           (unless frame
287             (put face 'custom-face-display display))
288           (setq spec nil))))))
289
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."
295   :group 'customize
296   :type '(choice (choice-item dark) 
297                  (choice-item light)
298                  (choice-item :tag "default" nil)))
299
300 (defun custom-background-mode (frame)
301   "Kludge to detect background mode for FRAME."
302   (let* ((bg-resource 
303           (condition-case ()
304               (x-get-resource ".backgroundMode" "BackgroundMode" 'string)
305             (error nil)))
306          color
307          (mode (cond (bg-resource
308                       (intern (downcase bg-resource)))
309                      ((and (setq color (condition-case ()
310                                            (or (frame-property
311                                                 frame
312                                                 'background-color)
313                                                (color-instance-name
314                                                 (specifier-instance
315                                                  (face-background 'default))))
316                                          (error nil)))
317                            (or (string-match "XEmacs" emacs-version)
318                                window-system)
319                            (< (apply '+ (x-color-values color))
320                               (/ (apply '+ (x-color-values "white"))
321                                  3)))
322                       'dark)
323                      (t 'light))))
324     (modify-frame-parameters frame (list (cons 'background-mode mode)))
325     mode))
326
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.")
332
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."
336   (cond (frame
337          ;; Try to get from cache.
338          (let ((cache (frame-property frame 'custom-properties)))
339            (unless cache
340              ;; Oh well, get it then.
341              (setq cache (custom-extract-frame-properties frame))
342              ;; and cache it...
343              (modify-frame-parameters frame 
344                                       (list (cons 'custom-properties cache))))
345            cache))
346         (custom-default-frame-properties)
347         (t
348          (setq custom-default-frame-properties
349                (custom-extract-frame-properties (selected-frame))))))
350
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!
355   (if (eq display t)
356       t
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))
361            (match t)
362            (entries display)
363            entry req options)
364       (while (and entries match)
365         (setq entry (car entries)
366               entries (cdr entries)
367               req (car entry)
368               options (cdr entry)
369               match (cond ((eq req 'type)
370                            (memq type options))
371                           ((eq req 'class)
372                            (memq class options))
373                           ((eq req 'background)
374                            (memq background options))
375                           (t
376                            (error "Unknown req `%S' with options `%S'" 
377                                   req options)))))
378       match)))
379
380 (defun custom-relevant-frames ()
381   "List of frames whose custom properties differ from the default."
382   (let ((relevant nil)
383         (default (custom-get-frame-properties))
384         (frames (frame-list))
385         frame)
386     (while frames
387       (setq frame (car frames)
388             frames (cdr frames))
389       (unless (equal default (custom-get-frame-properties frame))
390         (push frame relevant)))
391     relevant))
392
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))))
399               (when spec 
400                 (custom-face-display-set symbol spec frame)
401                 (initialize-face-resources symbol frame))))
402           (face-list)))
403
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."
407   (unless frame
408     (setq frame (car (frame-list))))
409   (unless (equal (custom-get-frame-properties) 
410                  (custom-get-frame-properties frame))
411     (custom-initialize-faces frame)))
412
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))
416
417 ;;; Initializing.
418
419 (and (fboundp 'make-face)
420      (make-face 'custom-face-empty))
421
422 ;;;###autoload
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:
426
427   (FACE SPEC [NOW])
428
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.
431
432 See `defface' for the format of SPEC."
433   (while args
434     (let ((entry (car args)))
435       (if (listp entry)
436           (let ((face (nth 0 entry))
437                 (spec (nth 1 entry))
438                 (now (nth 2 entry)))
439             (put face 'saved-face spec)
440             (when now
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))
448               (spec (nth 1 args)))
449           (put face 'saved-face spec))
450         (setq args (cdr (cdr args)))))))
451
452 ;;; The End.
453
454 (provide 'cus-face)
455
456 ;; cus-face.el ends here