This commit was generated by cvs2svn to compensate for changes in r1705,
[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@xemacs.org>
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   ;; #### should we possibly reset force-face here?
34   (unless (get face 'face-defface-spec)
35     (put face 'face-defface-spec spec)
36     (unless (find-face face)
37       ;; If the user has already created the face, respect that.
38       (let ((value (or (get face 'saved-face) spec))
39             (frames (relevant-custom-frames))
40             frame)
41         ;; Create global face.
42         (make-empty-face face)
43         (face-display-set face value nil '(custom))
44         ;; Create frame local faces
45         (while frames
46           (setq frame (car frames)
47                 frames (cdr frames))
48           (face-display-set face value frame '(custom)))
49         (init-face-from-resources face)))
50     (when (and doc (null (face-doc-string face)))
51       (set-face-doc-string face doc))
52     (custom-handle-all-keywords face args 'custom-face)
53     (run-hooks 'custom-define-hook))
54   face)
55
56 ;;; Font Attributes.
57
58 (defconst custom-face-attributes
59   '((:foreground (color :tag "Foreground"
60                         :value ""
61                         :help-echo "Set foreground color.")
62                  set-face-foreground face-foreground-name)
63     (:background (color :tag "Background"
64                         :value ""
65                         :help-echo "Set background color.")
66                  set-face-background face-background-name)
67     (:size (editable-field :format "Size: %v"
68                            :help-echo "\
69 Text size (e.g. 9pt or 2mm).")
70            custom-set-face-font-size custom-face-font-size)
71     (:family (editable-field :format "Font Family: %v"
72                              :help-echo "\
73 Name of font family to use (e.g. times).")
74              custom-set-face-font-family custom-face-font-family)
75     (:background-pixmap (editable-field :format "Background pixmap: %v"
76                                         :help-echo "\
77 Name of background pixmap file.")
78               set-face-background-pixmap custom-face-background-pixmap)
79     (:dim (toggle :format "%[Dim%]: %v\n"
80                   :help-echo "Control whether the text should be dimmed.")
81           set-face-dim-p face-dim-p)
82     (:bold (toggle :format "%[Bold%]: %v\n"
83                    :help-echo "Control whether a bold font should be used.")
84            custom-set-face-bold custom-face-bold)
85     (:italic (toggle :format "%[Italic%]: %v\n"
86                      :help-echo "\
87 Control whether an italic font should be used.")
88              custom-set-face-italic custom-face-italic)
89     (:underline (toggle :format "%[Underline%]: %v\n"
90                         :help-echo "\
91 Control whether the text should be underlined.")
92                 set-face-underline-p face-underline-p)
93     (:strikethru (toggle :format "%[Strikethru%]: %v\n"
94                          :help-echo "\
95 Control whether the text should be strikethru.")
96                  set-face-strikethru-p face-strikethru-p)
97     (:inverse-video (toggle :format "%[Inverse Video%]: %v\n"
98                             :help-echo "\
99 Control whether the text should be inverted.  Works only on TTY-s")
100                     set-face-reverse-p face-reverse-p))
101   "Alist of face attributes. 
102
103 The elements are of the form (KEY TYPE SET GET) where KEY is a symbol
104 identifying the attribute, TYPE is a widget type for editing the
105 attibute, SET is a function for setting the attribute value, and GET is a function for getiing the attribute value. 
106
107 The SET function should take three arguments, the face to modify, the
108 value of the attribute, and optionally the frame where the face should
109 be changed.
110
111 The GET function should take two arguments, the face to examine, and
112 optonally the frame where the face should be examined.")
113
114 (defun face-custom-attributes-set (face frame tags &rest atts)
115   "For FACE on FRAME set the attributes [KEYWORD VALUE]....
116 Each keyword should be listed in `custom-face-attributes'.
117
118 If FRAME is nil, set the default face."
119   (while atts
120     (let* ((name (nth 0 atts))
121            (value (nth 1 atts))
122            (fun (nth 2 (assq name custom-face-attributes))))
123       (setq atts (cdr (cdr atts)))
124       (condition-case nil
125           (funcall fun face value frame tags)
126         (error nil)))))
127
128 (defun face-custom-attributes-get (face frame)
129   "For FACE on FRAME get the attributes [KEYWORD VALUE]....
130 Each keyword should be listed in `custom-face-attributes'.
131
132 If FRAME is nil, use the default face."
133   (condition-case nil
134       ;; Attempt to get `font.el' from w3.
135       (require 'font)
136     (error nil))
137   (let ((atts custom-face-attributes)
138         att result get)
139     (while atts
140       (setq att (car atts)
141             atts (cdr atts)
142             get (nth 3 att))
143       (condition-case nil
144           ;; This may fail if w3 doesn't exist.
145           (when get
146             (let ((answer (funcall get face frame)))
147               (unless (equal answer (funcall get 'default frame))
148                 (when (widget-apply (nth 1 att) :match answer)
149                   (setq result (cons (nth 0 att) (cons answer result)))))))
150         (error nil)))
151     result))
152
153 (defsubst custom-face-get-spec (symbol)
154   (or (get symbol 'customized-face)
155       (get symbol 'saved-face)
156       (get symbol 'face-defface-spec)
157       ;; Attempt to construct it.
158       (list (list t (face-custom-attributes-get
159                      symbol (selected-frame))))))
160
161 (defun custom-set-face-bold (face value &optional frame tags)
162   "Set the bold property of FACE to VALUE."
163   (if value
164       (make-face-bold face frame tags)
165     (make-face-unbold face frame tags)))
166
167 ;; Really, we should get rid of these font.el dependencies...  They
168 ;; are still presenting a problem with dumping the faces (font.el is
169 ;; too bloated for us to dump).  I am thinking about hacking up
170 ;; font-like functionality myself for the sake of this file.  It will
171 ;; probably be to-the-point and more efficient.
172
173 (defun custom-face-bold (face &rest args)
174   "Return non-nil if the font of FACE is bold."
175   (let* ((font (apply 'face-font-name face args))
176          ;; Gag
177          (fontobj (font-create-object font)))
178     (font-bold-p fontobj)))
179
180 (defun custom-set-face-italic (face value &optional frame tags)
181   "Set the italic property of FACE to VALUE."
182   (if value
183       (make-face-italic face frame tags)
184     (make-face-unitalic face frame tags)))
185
186 (defun custom-face-italic (face &rest args)
187   "Return non-nil if the font of FACE is italic."
188   (let* ((font (apply 'face-font-name face args))
189          ;; Gag
190          (fontobj (font-create-object font)))
191     (font-italic-p fontobj)))
192
193 (defun custom-face-background-pixmap (face &rest args)
194   "Return the name of the background pixmap file used for FACE."
195   (let ((image  (apply 'specifier-instance 
196                        (face-background-pixmap face) args)))
197     (and image 
198          (image-instance-file-name image))))
199
200 (defun custom-set-face-font-size (face size &optional locale tags)
201   "Set the font of FACE to SIZE"
202   (let* ((font (apply 'face-font-name face locale))
203          ;; Gag
204          (fontobj (font-create-object font)))
205     (set-font-size fontobj size)
206     (apply 'font-set-face-font face fontobj locale tags)))
207
208 (defun custom-face-font-size (face &rest args)
209   "Return the size of the font of FACE as a string."
210   (let* ((font (apply 'face-font-name face args))
211          ;; Gag
212          (fontobj (font-create-object font)))
213     (format "%s" (font-size fontobj))))
214
215 (defun custom-set-face-font-family (face family &optional locale tags)
216   "Set the font of FACE to FAMILY."
217   (let* ((font (apply 'face-font-name face locale))
218          ;; Gag
219          (fontobj (font-create-object font)))
220     (set-font-family fontobj family)
221     (apply 'font-set-face-font face fontobj locale tags)))
222
223 (defun custom-face-font-family (face &rest args)
224   "Return the name of the font family of FACE."
225   (let* ((font (apply 'face-font-name face args))
226          ;; Gag
227          (fontobj (font-create-object font)))
228     (font-family fontobj)))
229
230 ;;;###autoload
231 (defun custom-set-face-update-spec (face display plist)
232   "Customize the FACE for display types matching DISPLAY, merging
233   in the new items from PLIST"
234   (let ((spec (face-spec-update-all-matching (custom-face-get-spec face)
235                                              display plist)))
236     (put face 'customized-face spec)
237     (face-spec-set face spec nil '(custom))))
238
239 ;;; Initializing.
240
241 ;;;###autoload
242 (defun custom-set-faces (&rest args)
243   "Initialize faces according to user preferences.
244 This asociates the setting with the USER theme.
245 The arguments should be a list where each entry has the form:
246
247   (FACE SPEC [NOW [COMMENT]])
248
249 SPEC will be stored as the saved value for FACE.  If NOW is present
250 and non-nil, FACE will also be created according to SPEC.
251 COMMENT is a string comment about FACE.
252
253 See `defface' for the format of SPEC."
254   (apply #'custom-theme-set-faces 'user args))
255
256 ;;;###autoload
257 (defun custom-theme-set-faces (theme &rest args)
258   "Initialize faces according to settings specified by args.
259 Records the settings as belonging to THEME.
260
261 See `custom-set-faces' for a description of the arguments ARGS."
262   (custom-check-theme theme)
263   (let ((immediate (get theme 'theme-immediate)))
264     (while args
265       (let ((entry (car args)))
266         (if (listp entry)
267             (let ((face (nth 0 entry))
268                   (spec (nth 1 entry))
269                   (now (nth 2 entry))
270                   (comment (nth 3 entry)))
271               (put face 'saved-face spec)
272               (custom-push-theme 'theme-face face theme 'set spec)
273               (put face 'saved-face-comment comment)
274               (when (or now immediate)
275                 (put face 'force-face (if now 'rogue 'immediate)))
276               (when (or now immediate (find-face face))
277                 (put face 'face-comment comment)
278                 (unless (find-face face)
279                   (make-empty-face face))
280                 (face-spec-set face spec nil '(custom)))
281               (setq args (cdr args)))
282           ;; Old format, a plist of FACE SPEC pairs.
283           (let ((face (nth 0 args))
284                 (spec (nth 1 args)))
285             (put face 'saved-face spec)
286             (custom-push-theme 'theme-face face theme 'set spec))
287           (setq args (cdr (cdr args))))))))
288
289 ;;;###autoload
290 (defun custom-theme-face-value (face theme)
291   "Return spec of FACE in THEME if the THEME modifies the
292 FACE.  Nil otherwise."
293   (car-safe (custom-theme-value theme (get face 'theme-face))))
294
295 (defun custom-theme-reset-internal-face (face to-theme)
296   (let ((spec (custom-theme-face-value face to-theme))
297         was-in-theme)
298     (setq was-in-theme spec)
299     (setq spec (or spec (get face 'standard-value)))
300     (when spec
301       (put face 'save-face was-in-theme)
302       (when (or (get face 'force-face) (find-face face))
303               (unless (find-face face)
304                 (make-empty-face face))
305               (face-spec-set face spec)))
306     spec))
307
308 ;;;###autoload
309 (defun custom-theme-reset-faces (theme &rest args)
310   (custom-check-theme theme)
311   "Reset the value of the face to values previously defined.
312 Associate this setting with THEME.
313
314 ARGS is a list of lists of the form
315
316     (face to-theme)
317
318 This means reset face to its value in to-theme."
319   (mapc #'(lambda (arg)
320             (apply #'custom-theme-reset-internal-face arg)
321             (custom-push-theme (car arg) 'theme-face theme 'reset (cadr arg)))
322         args))
323
324 ;;;###autoload
325 (defun custom-reset-faces (&rest args)
326   "Reset the value of the face to values previously defined.
327 Associate this setting with the 'user' theme.
328
329 ARGS is defined as for `custom-theme-reset-faces'"
330   (apply #'custom-theme-reset-faces 'user args))
331
332
333 ;;; The End.
334
335 (provide 'cus-face)
336
337 ;; cus-face.el ends here