]> code.delx.au - gnu-emacs/blob - lisp/cus-face.el
Merged from miles@gnu.org--gnu-2005 (patch 469)
[gnu-emacs] / lisp / cus-face.el
1 ;;; cus-face.el --- customization support for faces
2 ;;
3 ;; Copyright (C) 1996, 1997, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Keywords: help, faces
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26 ;;
27 ;; See `custom.el'.
28
29 ;;; Code:
30
31 (defalias 'custom-facep 'facep)
32
33 ;;; Declaring a face.
34
35 ;;;###autoload
36 (defun custom-declare-face (face spec doc &rest args)
37 "Like `defface', but FACE is evaluated as a normal argument."
38 (unless (get face 'face-defface-spec)
39 (when (fboundp 'facep)
40 (unless (facep face)
41 ;; If the user has already created the face, respect that.
42 (let ((value (or (get face 'saved-face) spec))
43 (have-window-system (memq initial-window-system '(x w32))))
44 ;; Create global face.
45 (make-empty-face face)
46 ;; Create frame-local faces
47 (dolist (frame (frame-list))
48 (face-spec-set face value frame)
49 (when (memq (window-system frame) '(x w32))
50 (setq have-window-system t)))
51 ;; When making a face after frames already exist
52 (if have-window-system
53 (make-face-x-resource-internal face)))))
54 ;; Don't record SPEC until we see it causes no errors.
55 (put face 'face-defface-spec spec)
56 (push (cons 'defface face) current-load-list)
57 (when (and doc (null (face-documentation face)))
58 (set-face-documentation face (purecopy doc)))
59 (custom-handle-all-keywords face args 'custom-face)
60 (run-hooks 'custom-define-hook))
61 face)
62
63 ;;; Face attributes.
64
65 ;;;###autoload
66 (defconst custom-face-attributes
67 '((:family
68 (string :tag "Font Family"
69 :help-echo "Font family or fontset alias name."))
70
71 (:width
72 (choice :tag "Width"
73 :help-echo "Font width."
74 :value normal ; default
75 (const :tag "compressed" condensed)
76 (const :tag "condensed" condensed)
77 (const :tag "demiexpanded" semi-expanded)
78 (const :tag "expanded" expanded)
79 (const :tag "extracondensed" extra-condensed)
80 (const :tag "extraexpanded" extra-expanded)
81 (const :tag "medium" normal)
82 (const :tag "narrow" condensed)
83 (const :tag "normal" normal)
84 (const :tag "regular" normal)
85 (const :tag "semicondensed" semi-condensed)
86 (const :tag "semiexpanded" semi-expanded)
87 (const :tag "ultracondensed" ultra-condensed)
88 (const :tag "ultraexpanded" ultra-expanded)
89 (const :tag "wide" extra-expanded)))
90
91 (:height
92 (choice :tag "Height"
93 :help-echo "Face's font height."
94 :value 1.0 ; default
95 (integer :tag "Height in 1/10 pt")
96 (number :tag "Scale" 1.0)))
97
98 (:weight
99 (choice :tag "Weight"
100 :help-echo "Font weight."
101 :value normal ; default
102 (const :tag "black" ultra-bold)
103 (const :tag "bold" bold)
104 (const :tag "book" semi-light)
105 (const :tag "demibold" semi-bold)
106 (const :tag "extralight" extra-light)
107 (const :tag "extrabold" extra-bold)
108 (const :tag "heavy" extra-bold)
109 (const :tag "light" light)
110 (const :tag "medium" normal)
111 (const :tag "normal" normal)
112 (const :tag "regular" normal)
113 (const :tag "semibold" semi-bold)
114 (const :tag "semilight" semi-light)
115 (const :tag "ultralight" ultra-light)
116 (const :tag "ultrabold" ultra-bold)))
117
118 (:slant
119 (choice :tag "Slant"
120 :help-echo "Font slant."
121 :value normal ; default
122 (const :tag "italic" italic)
123 (const :tag "oblique" oblique)
124 (const :tag "normal" normal)))
125
126 (:underline
127 (choice :tag "Underline"
128 :help-echo "Control text underlining."
129 (const :tag "Off" nil)
130 (const :tag "On" t)
131 (color :tag "Colored")))
132
133 (:overline
134 (choice :tag "Overline"
135 :help-echo "Control text overlining."
136 (const :tag "Off" nil)
137 (const :tag "On" t)
138 (color :tag "Colored")))
139
140 (:strike-through
141 (choice :tag "Strike-through"
142 :help-echo "Control text strike-through."
143 (const :tag "Off" nil)
144 (const :tag "On" t)
145 (color :tag "Colored")))
146
147 (:box
148 ;; Fixme: this can probably be done better.
149 (choice :tag "Box around text"
150 :help-echo "Control box around text."
151 (const :tag "Off" nil)
152 (list :tag "Box"
153 :value (:line-width 2 :color "grey75" :style released-button)
154 (const :format "" :value :line-width)
155 (integer :tag "Width")
156 (const :format "" :value :color)
157 (choice :tag "Color" (const :tag "*" nil) color)
158 (const :format "" :value :style)
159 (choice :tag "Style"
160 (const :tag "Raised" released-button)
161 (const :tag "Sunken" pressed-button)
162 (const :tag "None" nil))))
163 ;; filter to make value suitable for customize
164 (lambda (real-value)
165 (and real-value
166 (let ((lwidth
167 (or (and (consp real-value)
168 (plist-get real-value :line-width))
169 (and (integerp real-value) real-value)
170 1))
171 (color
172 (or (and (consp real-value) (plist-get real-value :color))
173 (and (stringp real-value) real-value)
174 nil))
175 (style
176 (and (consp real-value) (plist-get real-value :style))))
177 (list :line-width lwidth :color color :style style))))
178 ;; filter to make customized-value suitable for storing
179 (lambda (cus-value)
180 (and cus-value
181 (let ((lwidth (plist-get cus-value :line-width))
182 (color (plist-get cus-value :color))
183 (style (plist-get cus-value :style)))
184 (cond ((and (null color) (null style))
185 lwidth)
186 ((and (null lwidth) (null style))
187 ;; actually can't happen, because LWIDTH is always an int
188 color)
189 (t
190 ;; Keep as a plist, but remove null entries
191 (nconc (and lwidth `(:line-width ,lwidth))
192 (and color `(:color ,color))
193 (and style `(:style ,style)))))))))
194
195 (:inverse-video
196 (choice :tag "Inverse-video"
197 :help-echo "Control whether text should be in inverse-video."
198 (const :tag "Off" nil)
199 (const :tag "On" t)))
200
201 (:foreground
202 (color :tag "Foreground"
203 :help-echo "Set foreground color (name or #RRGGBB hex spec)."))
204
205 (:background
206 (color :tag "Background"
207 :help-echo "Set background color (name or #RRGGBB hex spec)."))
208
209 (:stipple
210 (choice :tag "Stipple"
211 :help-echo "Background bit-mask"
212 (const :tag "None" nil)
213 (file :tag "File"
214 :help-echo "Name of bitmap file."
215 :must-match t)))
216
217 (:inherit
218 (repeat :tag "Inherit"
219 :help-echo "List of faces to inherit attributes from."
220 (face :Tag "Face" default))
221 ;; filter to make value suitable for customize
222 (lambda (real-value)
223 (cond ((or (null real-value) (eq real-value 'unspecified))
224 nil)
225 ((symbolp real-value)
226 (list real-value))
227 (t
228 real-value)))
229 ;; filter to make customized-value suitable for storing
230 (lambda (cus-value)
231 (if (and (consp cus-value) (null (cdr cus-value)))
232 (car cus-value)
233 cus-value))))
234
235 "Alist of face attributes.
236
237 The elements are of the form (KEY TYPE PRE-FILTER POST-FILTER),
238 where KEY is the name of the attribute, TYPE is a widget type for
239 editing the attribute, PRE-FILTER is a function to make the attribute's
240 value suitable for the customization widget, and POST-FILTER is a
241 function to make the customized value suitable for storing. PRE-FILTER
242 and POST-FILTER are optional.
243
244 The PRE-FILTER should take a single argument, the attribute value as
245 stored, and should return a value for customization (using the
246 customization type TYPE).
247
248 The POST-FILTER should also take a single argument, the value after
249 being customized, and should return a value suitable for setting the
250 given face attribute.")
251
252 (defun custom-face-attributes-get (face frame)
253 "For FACE on FRAME, return an alternating list describing its attributes.
254 The list has the form (KEYWORD VALUE KEYWORD VALUE...).
255 Each keyword should be listed in `custom-face-attributes'.
256
257 If FRAME is nil, use the global defaults for FACE."
258 (let ((attrs custom-face-attributes)
259 plist)
260 (while attrs
261 (let* ((attribute (car (car attrs)))
262 (value (face-attribute face attribute frame)))
263 (setq attrs (cdr attrs))
264 (unless (or (eq value 'unspecified)
265 (and (null value) (memq attribute '(:inherit))))
266 (setq plist (cons attribute (cons value plist))))))
267 plist))
268
269 ;;; Initializing.
270
271 ;;;###autoload
272 (defun custom-set-faces (&rest args)
273 "Initialize faces according to user preferences.
274 This associates the settings with the `user' theme.
275 The arguments should be a list where each entry has the form:
276
277 (FACE SPEC [NOW [COMMENT]])
278
279 SPEC is stored as the saved value for FACE, as well as the value for the
280 `user' theme. The `user' theme is one of the default themes known to Emacs.
281 See `custom-known-themes' for more information on the known themes.
282 See `custom-theme-set-faces' for more information on the interplay
283 between themes and faces.
284 See `defface' for the format of SPEC.
285
286 If NOW is present and non-nil, FACE is created now, according to SPEC.
287 COMMENT is a string comment about FACE."
288 (apply 'custom-theme-set-faces 'user args))
289
290 (defun custom-theme-set-faces (theme &rest args)
291 "Initialize faces for theme THEME.
292 The arguments should be a list where each entry has the form:
293
294 (FACE SPEC [NOW [COMMENT]])
295
296 SPEC is stored as the saved value for FACE, as well as the value for the
297 `user' theme. The `user' theme is one of the default themes known to Emacs.
298 See `custom-known-themes' for more information on the known themes.
299 See `custom-theme-set-faces' for more information on the interplay
300 between themes and faces.
301 See `defface' for the format of SPEC.
302
303 If NOW is present and non-nil, FACE is created now, according to SPEC.
304 COMMENT is a string comment about FACE.
305
306 Several properties of THEME and FACE are used in the process:
307
308 If THEME property `theme-immediate' is non-nil, this is equivalent of
309 providing the NOW argument to all faces in the argument list: FACE is
310 created now. The only difference is FACE property `force-face': if NOW
311 is non-nil, FACE property `force-face' is set to the symbol `rogue', else
312 if THEME property `theme-immediate' is non-nil, FACE property `force-face'
313 is set to the symbol `immediate'.
314
315 SPEC itself is saved in FACE property `saved-face' and it is stored in
316 FACE's list property `theme-face' \(using `custom-push-theme')."
317 (custom-check-theme theme)
318 (let ((immediate (get theme 'theme-immediate)))
319 (while args
320 (let ((entry (car args)))
321 (if (listp entry)
322 (let ((face (nth 0 entry))
323 (spec (nth 1 entry))
324 (now (nth 2 entry))
325 (comment (nth 3 entry)))
326 ;; If FACE is actually an alias, customize the face it
327 ;; is aliased to.
328 (if (get face 'face-alias)
329 (setq face (get face 'face-alias)))
330 (put face 'saved-face spec)
331 (put face 'saved-face-comment comment)
332 (custom-push-theme 'theme-face face theme 'set spec)
333 (when (or now immediate)
334 (put face 'force-face (if now 'rogue 'immediate)))
335 (when (or now immediate (facep face))
336 (unless (facep face)
337 (make-empty-face face))
338 (put face 'face-comment comment)
339 (face-spec-set face spec))
340 (setq args (cdr args)))
341 ;; Old format, a plist of FACE SPEC pairs.
342 (let ((face (nth 0 args))
343 (spec (nth 1 args)))
344 (if (get face 'face-alias)
345 (setq face (get face 'face-alias)))
346 (put face 'saved-face spec)
347 (custom-push-theme 'theme-face face theme 'set spec))
348 (setq args (cdr (cdr args))))))))
349
350 ;;;###autoload
351 (defun custom-theme-face-value (face theme)
352 "Return spec of FACE in THEME if THEME modifies FACE.
353 Value is nil otherwise. The association between theme and spec for FACE
354 is stored in FACE's property `theme-face'. The appropriate face
355 is retrieved using `custom-theme-value'."
356 ;; Returns car because the value is stored inside a one element list
357 (car-safe (custom-theme-value theme (get face 'theme-face))))
358
359 (defun custom-theme-reset-internal-face (face to-theme)
360 "Reset FACE to the value defined by TO-THEME.
361 If FACE is not defined in TO-THEME, reset FACE to the standard
362 value. See `custom-theme-face-value'. The standard value is
363 stored in SYMBOL's property `face-defface-spec' by `defface'."
364 (let ((spec (custom-theme-face-value face to-theme))
365 was-in-theme)
366 (setq was-in-theme spec)
367 (setq spec (or spec (get face 'face-defface-spec)))
368 (when spec
369 (put face 'save-face was-in-theme)
370 (when (or (get face 'force-face) (facep face))
371 (unless (facep face)
372 (make-empty-face face))
373 (face-spec-set face spec)))
374 spec))
375
376 ;;;###autoload
377 (defun custom-theme-reset-faces (theme &rest args)
378 "Reset the value of the face to values previously defined.
379 Associate this setting with THEME.
380
381 ARGS is a list of lists of the form
382
383 (FACE TO-THEME)
384
385 This means reset FACE to its value in TO-THEME."
386 (custom-check-theme theme)
387 (mapcar '(lambda (arg)
388 (apply 'custom-theme-reset-internal-face arg)
389 (custom-push-theme 'theme-face (car arg) theme 'reset (cadr arg)))
390 args))
391
392 ;;;###autoload
393 (defun custom-reset-faces (&rest args)
394 "Reset the value of the face to values previously saved.
395 This is the setting assosiated the `user' theme.
396
397 ARGS is defined as for `custom-theme-reset-faces'"
398 (apply 'custom-theme-reset-faces 'user args))
399
400 ;;; The End.
401
402 (provide 'cus-face)
403
404 ;;; arch-tag: 9a5c4b63-0d27-4c92-a5af-f2c7ed764c2b
405 ;;; cus-face.el ends here