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