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