XEmacs 21.2.14.
[chise/xemacs-chise.git.1] / lisp / cus-face.el
1 ;;; cus-face.el -- Support for Custom faces.
2 ;;
3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Maintainer: Hrvoje Niksic <hniksic@srce.hr>
7 ;; Keywords: help, faces
8 ;; Version: 1.9960-x
9 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
10
11 ;;; Commentary:
12 ;;
13 ;; See `custom.el'.
14
15 ;; This file should probably be dissolved, and code moved to faces.el,
16 ;; like Stallman did.
17
18 ;;; Code:
19
20 (require 'custom)
21
22 ;; To elude the warnings for font functions.
23 (eval-when-compile
24   (require 'font))
25
26 ;;; Declaring a face.
27
28 ;;;###autoload
29 (defun custom-declare-face (face spec doc &rest args)
30   "Like `defface', but FACE is evaluated as a normal argument."
31   ;; (when (fboundp 'pureload)
32     ;; (error "Attempt to declare a face during dump"))
33   (unless (get face 'face-defface-spec)
34     (put face 'face-defface-spec spec)
35     (unless (find-face face)
36       ;; If the user has already created the face, respect that.
37       (let ((value (or (get face 'saved-face) spec))
38             (frames (relevant-custom-frames))
39             frame)
40         ;; Create global face.
41         (make-empty-face face)
42         (face-display-set face value)
43         ;; Create frame local faces
44         (while frames
45           (setq frame (car frames)
46                 frames (cdr frames))
47           (face-display-set face value frame))
48         (init-face-from-resources face)))
49     (when (and doc (null (face-doc-string face)))
50       (set-face-doc-string face doc))
51     (custom-handle-all-keywords face args 'custom-face)
52     (run-hooks 'custom-define-hook))
53   face)
54
55 ;;; Font Attributes.
56
57 (defconst custom-face-attributes
58   '((:foreground (color :tag "Foreground"
59                         :value ""
60                         :help-echo "Set foreground color.")
61                  set-face-foreground face-foreground-name)
62     (:background (color :tag "Background"
63                         :value ""
64                         :help-echo "Set background color.")
65                  set-face-background face-background-name)
66     (:size (editable-field :format "Size: %v"
67                            :help-echo "\
68 Text size (e.g. 9pt or 2mm).")
69            custom-set-face-font-size custom-face-font-size)
70     (:family (editable-field :format "Font Family: %v"
71                              :help-echo "\
72 Name of font family to use (e.g. times).") 
73              custom-set-face-font-family custom-face-font-family)
74     (:background-pixmap (editable-field :format "Background pixmap: %v"
75                                         :help-echo "\
76 Name of background pixmap file.")
77               set-face-background-pixmap custom-face-background-pixmap)
78     (:dim (toggle :format "%[Dim%]: %v\n"
79                   :help-echo "Control whether the text should be dimmed.")
80           set-face-dim-p face-dim-p)
81     (:bold (toggle :format "%[Bold%]: %v\n"
82                    :help-echo "Control whether a bold font should be used.")
83            custom-set-face-bold custom-face-bold)
84     (:italic (toggle :format "%[Italic%]: %v\n"
85                      :help-echo "\
86 Control whether an italic font should be used.")
87              custom-set-face-italic custom-face-italic)
88     (:underline (toggle :format "%[Underline%]: %v\n"
89                         :help-echo "\
90 Control whether the text should be underlined.")
91                 set-face-underline-p face-underline-p)
92     (:strikethru (toggle :format "%[Strikethru%]: %v\n"
93                          :help-echo "\
94 Control whether the text should be strikethru.")
95                  set-face-strikethru-p face-strikethru-p)
96     (:inverse-video (toggle :format "%[Inverse Video%]: %v\n"
97                             :help-echo "\
98 Control whether the text should be inverted.  Works only on TTY-s")
99                     set-face-reverse-p face-reverse-p))
100   "Alist of face attributes. 
101
102 The elements are of the form (KEY TYPE SET GET) where KEY is a symbol
103 identifying the attribute, TYPE is a widget type for editing the
104 attibute, SET is a function for setting the attribute value, and GET is a function for getiing the attribute value. 
105
106 The SET function should take three arguments, the face to modify, the
107 value of the attribute, and optionally the frame where the face should
108 be changed.
109
110 The GET function should take two arguments, the face to examine, and
111 optonally the frame where the face should be examined.")
112
113 (defun face-custom-attributes-set (face frame &rest atts)
114   "For FACE on FRAME set the attributes [KEYWORD VALUE]....
115 Each keyword should be listed in `custom-face-attributes'.
116
117 If FRAME is nil, set the default face."
118   (while atts
119     (let* ((name (nth 0 atts))
120            (value (nth 1 atts))
121            (fun (nth 2 (assq name custom-face-attributes))))
122       (setq atts (cdr (cdr atts)))
123       (condition-case nil
124           (funcall fun face value frame)
125         (error nil)))))
126
127 (defun face-custom-attributes-get (face frame)
128   "For FACE on FRAME get the attributes [KEYWORD VALUE]....
129 Each keyword should be listed in `custom-face-attributes'.
130
131 If FRAME is nil, use the default face."
132   (condition-case nil
133       ;; Attempt to get `font.el' from w3.
134       (require 'font)
135     (error nil))
136   (let ((atts custom-face-attributes)
137         att result get)
138     (while atts
139       (setq att (car atts)
140             atts (cdr atts)
141             get (nth 3 att))
142       (condition-case nil
143           ;; This may fail if w3 doesn't exist.
144           (when get
145             (let ((answer (funcall get face frame)))
146               (unless (equal answer (funcall get 'default frame))
147                 (when (widget-apply (nth 1 att) :match answer)
148                   (setq result (cons (nth 0 att) (cons answer result)))))))
149         (error nil)))
150     result))
151
152 (defsubst custom-face-get-spec (symbol)
153   (or (get symbol 'customized-face)
154       (get symbol 'saved-face)
155       (get symbol 'face-defface-spec)
156       ;; Attempt to construct it.
157       (list (list t (face-custom-attributes-get
158                      symbol (selected-frame))))))
159
160 (defun custom-set-face-bold (face value &optional frame)
161   "Set the bold property of FACE to VALUE."
162   (if value
163       (make-face-bold face frame)
164     (make-face-unbold face frame)))
165
166 ;; Really, we should get rid of these font.el dependencies...  They
167 ;; are still presenting a problem with dumping the faces (font.el is
168 ;; too bloated for us to dump).  I am thinking about hacking up
169 ;; font-like functionality myself for the sake of this file.  It will
170 ;; probably be to-the-point and more efficient.
171
172 (defun custom-face-bold (face &rest args)
173   "Return non-nil if the font of FACE is bold."
174   (let* ((font (apply 'face-font-name face args))
175          ;; Gag
176          (fontobj (font-create-object font)))
177     (font-bold-p fontobj)))
178
179 (defun custom-set-face-italic (face value &optional frame)
180   "Set the italic property of FACE to VALUE."
181   (if value
182       (make-face-italic face frame)
183     (make-face-unitalic face frame)))
184
185 (defun custom-face-italic (face &rest args)
186   "Return non-nil if the font of FACE is italic."
187   (let* ((font (apply 'face-font-name face args))
188          ;; Gag
189          (fontobj (font-create-object font)))
190     (font-italic-p fontobj)))
191
192 (defun custom-face-background-pixmap (face &rest args)
193   "Return the name of the background pixmap file used for FACE."
194   (let ((image  (apply 'specifier-instance 
195                        (face-background-pixmap face) args)))
196     (and image 
197          (image-instance-file-name image))))
198
199 (defun custom-set-face-font-size (face size &rest args)
200   "Set the font of FACE to SIZE"
201   (let* ((font (apply 'face-font-name face args))
202          ;; Gag
203          (fontobj (font-create-object font)))
204     (set-font-size fontobj size)
205     (apply 'font-set-face-font face fontobj args)))
206
207 (defun custom-face-font-size (face &rest args)
208   "Return the size of the font of FACE as a string."
209   (let* ((font (apply 'face-font-name face args))
210          ;; Gag
211          (fontobj (font-create-object font)))
212     (format "%s" (font-size fontobj))))
213
214 (defun custom-set-face-font-family (face family &rest args)
215   "Set the font of FACE to FAMILY."
216   (let* ((font (apply 'face-font-name face args))
217          ;; Gag
218          (fontobj (font-create-object font)))
219     (set-font-family fontobj family)
220     (apply 'font-set-face-font face fontobj args)))
221
222 (defun custom-face-font-family (face &rest args)
223   "Return the name of the font family of FACE."
224   (let* ((font (apply 'face-font-name face args))
225          ;; Gag
226          (fontobj (font-create-object font)))
227     (font-family fontobj)))
228
229 ;;;###autoload
230 (defun custom-set-face-update-spec (face display plist)
231   "Customize the FACE for display types matching DISPLAY, merging
232   in the new items from PLIST"
233   (let ((spec (face-spec-update-all-matching (custom-face-get-spec face)
234                                              display plist)))
235     (put face 'customized-face spec)
236     (face-spec-set face spec)))
237
238 ;;; Initializing.
239
240 ;;;###autoload
241 (defun custom-set-faces (&rest args)
242   "Initialize faces according to user preferences.
243 The arguments should be a list where each entry has the form:
244
245   (FACE SPEC [NOW])
246
247 SPEC will be stored as the saved value for FACE.  If NOW is present
248 and non-nil, FACE will also be created according to SPEC.
249
250 See `defface' for the format of SPEC."
251   (while args
252     (let ((entry (car args)))
253       (if (listp entry)
254           (let ((face (nth 0 entry))
255                 (spec (nth 1 entry))
256                 (now (nth 2 entry)))
257             (put face 'saved-face spec)
258             (when now
259               (put face 'force-face t))
260             (when (or now (find-face face))
261               (unless (find-face face)
262                 (make-empty-face face))
263               (face-spec-set face spec))
264             (setq args (cdr args)))
265         ;; Old format, a plist of FACE SPEC pairs.
266         (let ((face (nth 0 args))
267               (spec (nth 1 args)))
268           (put face 'saved-face spec))
269         (setq args (cdr (cdr args)))))))
270
271 ;;; The End.
272
273 (provide 'cus-face)
274
275 ;; cus-face.el ends here