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