1 ;;; cus-face.el -- Support for Custom faces.
3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Maintainer: Hrvoje Niksic <hniksic@xemacs.org>
7 ;; Keywords: help, faces
9 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
15 ;; This file should probably be dissolved, and code moved to faces.el,
22 ;; To elude the warnings for font functions.
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))
41 ;; Create global face.
42 (make-empty-face face)
43 (face-display-set face value nil '(custom))
44 ;; Create frame local faces
46 (setq frame (car 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))
58 (defconst custom-face-attributes
59 '((:foreground (color :tag "Foreground"
61 :help-echo "Set foreground color.")
62 set-face-foreground face-foreground-name)
63 (:background (color :tag "Background"
65 :help-echo "Set background color.")
66 set-face-background face-background-name)
67 (:size (editable-field :format "Size: %v"
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"
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"
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"
87 Control whether an italic font should be used.")
88 custom-set-face-italic custom-face-italic)
89 (:underline (toggle :format "%[Underline%]: %v\n"
91 Control whether the text should be underlined.")
92 set-face-underline-p face-underline-p)
93 (:strikethru (toggle :format "%[Strikethru%]: %v\n"
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"
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.
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.
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
111 The GET function should take two arguments, the face to examine, and
112 optonally the frame where the face should be examined.")
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'.
118 If FRAME is nil, set the default face."
120 (let* ((name (nth 0 atts))
122 (fun (nth 2 (assq name custom-face-attributes))))
123 (setq atts (cdr (cdr atts)))
125 (funcall fun face value frame tags)
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'.
132 If FRAME is nil, use the default face."
134 ;; Attempt to get `font.el' from w3.
137 (let ((atts custom-face-attributes)
144 ;; This may fail if w3 doesn't exist.
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)))))))
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))))))
161 (defun custom-set-face-bold (face value &optional frame tags)
162 "Set the bold property of FACE to VALUE."
164 (make-face-bold face frame tags)
165 (make-face-unbold face frame tags)))
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.
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))
177 (fontobj (font-create-object font)))
178 (font-bold-p fontobj)))
180 (defun custom-set-face-italic (face value &optional frame tags)
181 "Set the italic property of FACE to VALUE."
183 (make-face-italic face frame tags)
184 (make-face-unitalic face frame tags)))
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))
190 (fontobj (font-create-object font)))
191 (font-italic-p fontobj)))
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)))
198 (image-instance-file-name image))))
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))
204 (fontobj (font-create-object font)))
205 (set-font-size fontobj size)
206 (apply 'font-set-face-font face fontobj locale tags)))
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))
212 (fontobj (font-create-object font)))
213 (format "%s" (font-size fontobj))))
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))
219 (fontobj (font-create-object font)))
220 (set-font-family fontobj family)
221 (apply 'font-set-face-font face fontobj locale tags)))
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))
227 (fontobj (font-create-object font)))
228 (font-family fontobj)))
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)
236 (put face 'customized-face spec)
237 (face-spec-set face spec nil '(custom))))
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:
247 (FACE SPEC [NOW [COMMENT]])
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.
253 See `defface' for the format of SPEC."
254 (apply #'custom-theme-set-faces 'user args))
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.
261 See `custom-set-faces' for a description of the arguments ARGS."
262 (custom-check-theme theme)
263 (let ((immediate (get theme 'theme-immediate)))
265 (let ((entry (car args)))
267 (let ((face (nth 0 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))
285 (put face 'saved-face spec)
286 (custom-push-theme 'theme-face face theme 'set spec))
287 (setq args (cdr (cdr args))))))))
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))))
295 (defun custom-theme-reset-internal-face (face to-theme)
296 (let ((spec (custom-theme-face-value face to-theme))
298 (setq was-in-theme spec)
299 (setq spec (or spec (get face 'standard-value)))
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)))
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 Assosiate this setting with THEME.
314 ARGS is a list of lists of the form
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)))
325 (defun custom-reset-faces (&rest args)
326 "Reset the value of the face to values previously defined.
327 Assosiate this setting with the 'user' theme.
329 ARGS is defined as for `custom-theme-reset-faces'"
330 (apply #'custom-theme-reset-faces 'user args))
337 ;; cus-face.el ends here