*** empty log message ***
[bpt/emacs.git] / lisp / faces.el
CommitLineData
5f5c8ee5 1;;; faces.el --- Lisp faces
465fceed 2
0d30b337 3;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
409cc4a3 4;; 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
465fceed 5
6228c05b 6;; Maintainer: FSF
30764597 7;; Keywords: internal
6228c05b 8
465fceed
ER
9;; This file is part of GNU Emacs.
10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
465fceed 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
465fceed
ER
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
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
465fceed
ER
23
24;;; Commentary:
25
465fceed
ER
26;;; Code:
27
39b3b754 28(eval-when-compile
f31b1257
DN
29 (require 'cl))
30
31(declare-function xw-defined-colors "term/x-win" (&optional frame))
5f5c8ee5 32
b782a458 33(defvar help-xref-stack-item)
5f5c8ee5
GM
34\f
35;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
36;;; Font selection.
37;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
38
39(defgroup font-selection nil
40 "Influencing face font selection."
41 :group 'faces)
42
43
44(defcustom face-font-selection-order
45 '(:width :height :weight :slant)
46 "*A list specifying how face font selection chooses fonts.
47Each of the four symbols `:width', `:height', `:weight', and `:slant'
48must appear once in the list, and the list must not contain any other
7c626e9d 49elements. Font selection first tries to find a best matching font
b73f65b6 50for those face attributes that appear before in the list. For
5f5c8ee5
GM
51example, if `:slant' appears before `:height', font selection first
52tries to find a font with a suitable slant, even if this results in
53a font height that isn't optimal."
239ad97f 54 :tag "Font selection order"
94354bdd 55 :type '(list symbol symbol symbol symbol)
5f5c8ee5
GM
56 :group 'font-selection
57 :set #'(lambda (symbol value)
58 (set-default symbol value)
59 (internal-set-font-selection-order value)))
60
18f1dcb4
GM
61
62;; This is defined originally in xfaces.c.
5f5c8ee5
GM
63(defcustom face-font-family-alternatives
64 '(("courier" "fixed")
3855860a 65 ("helv" "helvetica" "arial" "fixed"))
5f5c8ee5 66 "*Alist of alternative font family names.
86886bce 67Each element has the form (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...).
5f5c8ee5
GM
68If fonts of family FAMILY can't be loaded, try ALTERNATIVE1, then
69ALTERNATIVE2 etc."
239ad97f 70 :tag "Alternative font families to try"
94354bdd 71 :type '(repeat (repeat string))
5f5c8ee5
GM
72 :group 'font-selection
73 :set #'(lambda (symbol value)
74 (set-default symbol value)
75 (internal-set-alternative-font-family-alist value)))
76
77
18f1dcb4
GM
78;; This is defined originally in xfaces.c.
79(defcustom face-font-registry-alternatives
c97db4e4 80 (if (eq system-type 'windows-nt)
990d68dd 81 '(("iso8859-1" "ms-oemlatin")
e38c812e 82 ("gb2312.1980" "gb2312" "gbk" "gb18030")
c97db4e4
AI
83 ("jisx0208.1990" "jisx0208.1983" "jisx0208.1978")
84 ("ksc5601.1989" "ksx1001.1992" "ksc5601.1987")
85 ("muletibetan-2" "muletibetan-0"))
e38c812e 86 '(("gb2312.1980" "gb2312.80&gb8565.88" "gbk" "gb18030")
c97db4e4
AI
87 ("jisx0208.1990" "jisx0208.1983" "jisx0208.1978")
88 ("ksc5601.1989" "ksx1001.1992" "ksc5601.1987")
89 ("muletibetan-2" "muletibetan-0")))
18f1dcb4 90 "*Alist of alternative font registry names.
86886bce 91Each element has the form (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...).
8ac2d32f
KH
92If fonts of registry REGISTRY can be loaded, font selection
93tries to find a best matching font among all fonts of registry
94REGISTRY, ALTERNATIVE1, ALTERNATIVE2, and etc."
239ad97f 95 :tag "Alternative font registries to try"
18f1dcb4 96 :type '(repeat (repeat string))
f676894c 97 :version "21.1"
18f1dcb4
GM
98 :group 'font-selection
99 :set #'(lambda (symbol value)
100 (set-default symbol value)
101 (internal-set-alternative-font-registry-alist value)))
102
bdda3754 103\f
5f5c8ee5
GM
104;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
105;;; Creation, copying.
106;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
107
108
109(defun face-list ()
110 "Return a list of all defined face names."
111 (mapcar #'car face-new-frame-defaults))
112
113
114;;; ### If not frame-local initialize by what X resources?
115
116(defun make-face (face &optional no-init-from-resources)
117 "Define a new face with name FACE, a symbol.
118NO-INIT-FROM-RESOURCES non-nil means don't initialize frame-local
119variants of FACE from X resources. (X resources recognized are found
120in the global variable `face-x-resources'.) If FACE is already known
121as a face, leave it unmodified. Value is FACE."
122 (interactive "SMake face: ")
123 (unless (facep face)
124 ;; Make frame-local faces (this also makes the global one).
125 (dolist (frame (frame-list))
126 (internal-make-lisp-face face frame))
127 ;; Add the face to the face menu.
128 (when (fboundp 'facemenu-add-new-face)
129 (facemenu-add-new-face face))
130 ;; Define frame-local faces for all frames from X resources.
131 (unless no-init-from-resources
132 (make-face-x-resource-internal face)))
133 face)
134
135
136(defun make-empty-face (face)
137 "Define a new, empty face with name FACE.
138If the face already exists, it is left unmodified. Value is FACE."
139 (interactive "SMake empty face: ")
140 (make-face face 'no-init-from-resources))
141
142
143(defun copy-face (old-face new-face &optional frame new-frame)
144 "Define a face just like OLD-FACE, with name NEW-FACE.
145
146If NEW-FACE already exists as a face, it is modified to be like
147OLD-FACE. If it doesn't already exist, it is created.
148
1c521fb0 149If the optional argument FRAME is given as a frame, NEW-FACE is
5f5c8ee5
GM
150changed on FRAME only.
151If FRAME is t, the frame-independent default specification for OLD-FACE
152is copied to NEW-FACE.
153If FRAME is nil, copying is done for the frame-independent defaults
154and for each existing frame.
155
156If the optional fourth argument NEW-FRAME is given,
157copy the information from face OLD-FACE on frame FRAME
341a1bfb 158to NEW-FACE on frame NEW-FRAME. In this case, FRAME may not be nil."
5f5c8ee5
GM
159 (let ((inhibit-quit t))
160 (if (null frame)
161 (progn
341a1bfb
RS
162 (when new-frame
163 (error "Copying face %s from all frames to one frame"
164 old-face))
165 (make-empty-face new-face)
5f5c8ee5
GM
166 (dolist (frame (frame-list))
167 (copy-face old-face new-face frame))
168 (copy-face old-face new-face t))
341a1bfb 169 (make-empty-face new-face)
5f5c8ee5
GM
170 (internal-copy-lisp-face old-face new-face frame new-frame))
171 new-face))
172
173
174\f
175;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
176;;; Obsolete functions
177;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
178
179;; The functions in this section are defined because Lisp packages use
180;; them, despite the prefix `internal-' suggesting that they are
70d0c3b0 181;; private to the face implementation.
5f5c8ee5
GM
182
183(defun internal-find-face (name &optional frame)
184 "Retrieve the face named NAME.
185Return nil if there is no such face.
39a897a5
JB
186If NAME is already a face, it is simply returned.
187The optional argument FRAME is ignored."
5f5c8ee5 188 (facep name))
2598a293 189(make-obsolete 'internal-find-face 'facep "21.1")
5f5c8ee5
GM
190
191
192(defun internal-get-face (name &optional frame)
193 "Retrieve the face named NAME; error if there is none.
39a897a5
JB
194If NAME is already a face, it is simply returned.
195The optional argument FRAME is ignored."
3fc67165 196 (or (facep name)
5f5c8ee5 197 (check-face name)))
4b22f844 198(make-obsolete 'internal-get-face "see `facep' and `check-face'." "21.1")
5f5c8ee5
GM
199
200\f
201;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
202;;; Predicates, type checks.
203;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
204
205(defun facep (face)
20d29b0b
RS
206 "Return non-nil if FACE is a face name; nil otherwise.
207A face name can be a string or a symbol."
5f5c8ee5
GM
208 (internal-lisp-face-p face))
209
210
211(defun check-face (face)
212 "Signal an error if FACE doesn't name a face.
213Value is FACE."
214 (unless (facep face)
215 (error "Not a face: %s" face))
216 face)
217
218
219;; The ID returned is not to be confused with the internally used IDs
220;; of realized faces. The ID assigned to Lisp faces is used to
221;; support faces in display table entries.
222
223(defun face-id (face &optional frame)
2df447f2 224 "Return the internal ID of face with name FACE.
b2d5aa46 225If FACE is a face-alias, return the ID of the target face.
9ae89a1a
RS
226The optional argument FRAME is ignored, since the internal face ID
227of a face name is the same for all frames."
5f5c8ee5 228 (check-face face)
b2d5aa46
JB
229 (or (get face 'face)
230 (face-id (get face 'face-alias))))
5f5c8ee5
GM
231
232(defun face-equal (face1 face2 &optional frame)
233 "Non-nil if faces FACE1 and FACE2 are equal.
234Faces are considered equal if all their attributes are equal.
a09be93a
JB
235If the optional argument FRAME is given, report on FACE1 and FACE2 in that frame.
236If FRAME is t, report on the defaults for FACE1 and FACE2 (for new frames).
5f5c8ee5
GM
237If FRAME is omitted or nil, use the selected frame."
238 (internal-lisp-face-equal-p face1 face2 frame))
239
240
241(defun face-differs-from-default-p (face &optional frame)
7e07a66d 242 "Return non-nil if FACE displays differently from the default face.
5f5c8ee5
GM
243If the optional argument FRAME is given, report on face FACE in that frame.
244If FRAME is t, report on the defaults for face FACE (for new frames).
7e07a66d 245If FRAME is omitted or nil, use the selected frame."
1151d617 246 (let ((attrs
833547aa 247 (delq :inherit (mapcar 'car face-attribute-name-alist)))
1151d617
MB
248 (differs nil))
249 (while (and attrs (not differs))
250 (let* ((attr (pop attrs))
251 (attr-val (face-attribute face attr frame t)))
252 (when (and
253 (not (eq attr-val 'unspecified))
254 (display-supports-face-attributes-p (list attr attr-val)
255 frame))
256 (setq differs attr))))
257 differs))
5f5c8ee5
GM
258
259
260(defun face-nontrivial-p (face &optional frame)
261 "True if face FACE has some non-nil attribute.
262If the optional argument FRAME is given, report on face FACE in that frame.
263If FRAME is t, report on the defaults for face FACE (for new frames).
264If FRAME is omitted or nil, use the selected frame."
265 (not (internal-lisp-face-empty-p face frame)))
266
267
268\f
269;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
270;;; Setting face attributes from X resources.
271;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
272
273(defcustom face-x-resources
274 '((:family (".attributeFamily" . "Face.AttributeFamily"))
275 (:width (".attributeWidth" . "Face.AttributeWidth"))
276 (:height (".attributeHeight" . "Face.AttributeHeight"))
277 (:weight (".attributeWeight" . "Face.AttributeWeight"))
278 (:slant (".attributeSlant" . "Face.AttributeSlant"))
279 (:foreground (".attributeForeground" . "Face.AttributeForeground"))
280 (:background (".attributeBackground" . "Face.AttributeBackground"))
281 (:overline (".attributeOverline" . "Face.AttributeOverline"))
282 (:strike-through (".attributeStrikeThrough" . "Face.AttributeStrikeThrough"))
283 (:box (".attributeBox" . "Face.AttributeBox"))
284 (:underline (".attributeUnderline" . "Face.AttributeUnderline"))
285 (:inverse-video (".attributeInverse" . "Face.AttributeInverse"))
286 (:stipple
287 (".attributeStipple" . "Face.AttributeStipple")
288 (".attributeBackgroundPixmap" . "Face.AttributeBackgroundPixmap"))
5f5c8ee5
GM
289 (:bold (".attributeBold" . "Face.AttributeBold"))
290 (:italic (".attributeItalic" . "Face.AttributeItalic"))
a0e5a5a4
MB
291 (:font (".attributeFont" . "Face.AttributeFont"))
292 (:inherit (".attributeInherit" . "Face.AttributeInherit")))
5f5c8ee5
GM
293 "*List of X resources and classes for face attributes.
294Each element has the form (ATTRIBUTE ENTRY1 ENTRY2...) where ATTRIBUTE is
295the name of a face attribute, and each ENTRY is a cons of the form
2cb95d1b 296\(RESOURCE . CLASS) with RESOURCE being the resource and CLASS being the
5f5c8ee5 297X resource class for the attribute."
b5325e78 298 :type '(repeat (cons symbol (repeat (cons string string))))
5f5c8ee5
GM
299 :group 'faces)
300
301
aa360da1
GM
302(declare-function internal-face-x-get-resource "xfaces.c"
303 (resource class frame))
304
305(declare-function internal-set-lisp-face-attribute-from-resource "xfaces.c"
306 (face attr value &optional frame))
307
5f5c8ee5
GM
308(defun set-face-attribute-from-resource (face attribute resource class frame)
309 "Set FACE's ATTRIBUTE from X resource RESOURCE, class CLASS on FRAME.
310Value is the attribute value specified by the resource, or nil
311if not present. This function displays a message if the resource
312specifies an invalid attribute."
313 (let* ((face-name (face-name face))
314 (value (internal-face-x-get-resource (concat face-name resource)
315 class frame)))
316 (when value
317 (condition-case ()
318 (internal-set-lisp-face-attribute-from-resource
319 face attribute (downcase value) frame)
320 (error
321 (message "Face %s, frame %s: invalid attribute %s %s from X resource"
322 face-name frame attribute value))))
323 value))
324
325
326(defun set-face-attributes-from-resources (face frame)
327 "Set attributes of FACE from X resources for FRAME."
1a578e9b 328 (when (memq (framep frame) '(x w32 mac))
5f5c8ee5
GM
329 (dolist (definition face-x-resources)
330 (let ((attribute (car definition)))
331 (dolist (entry (cdr definition))
332 (set-face-attribute-from-resource face attribute (car entry)
333 (cdr entry) frame))))))
00f51890
SS
334
335
5f5c8ee5
GM
336(defun make-face-x-resource-internal (face &optional frame)
337 "Fill frame-local FACE on FRAME from X resources.
338FRAME nil or not specified means do it for all frames."
339 (if (null frame)
340 (dolist (frame (frame-list))
341 (set-face-attributes-from-resources face frame))
342 (set-face-attributes-from-resources face frame)))
343
344
345\f
346;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
347;;; Retrieving face attributes.
348;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
349
feaad827 350(defun face-name (face)
465fceed 351 "Return the name of face FACE."
5f5c8ee5
GM
352 (symbol-name (check-face face)))
353
354
833547aa
RS
355(defun face-all-attributes (face &optional frame)
356 "Return an alist stating the attributes of FACE.
357Each element of the result has the form (ATTR-NAME . ATTR-VALUE).
358Normally the value describes the default attributes,
359but if you specify FRAME, the value describes the attributes
360of FACE on FRAME."
606c9f59
RS
361 (mapcar (lambda (pair)
362 (let ((attr (car pair)))
363 (cons attr (face-attribute face attr (or frame t)))))
833547aa
RS
364 face-attribute-name-alist))
365
c7f814cb 366(defun face-attribute (face attribute &optional frame inherit)
5f5c8ee5
GM
367 "Return the value of FACE's ATTRIBUTE on FRAME.
368If the optional argument FRAME is given, report on face FACE in that frame.
369If FRAME is t, report on the defaults for face FACE (for new frames).
c7f814cb 370If FRAME is omitted or nil, use the selected frame.
5f5c8ee5 371
c7f814cb
MB
372If INHERIT is nil, only attributes directly defined by FACE are considered,
373 so the return value may be `unspecified', or a relative value.
374If INHERIT is non-nil, FACE's definition of ATTRIBUTE is merged with the
375 faces specified by its `:inherit' attribute; however the return value
376 may still be `unspecified' or relative.
377If INHERIT is a face or a list of faces, then the result is further merged
378 with that face (or faces), until it becomes specified and absolute.
379
380To ensure that the return value is always specified and absolute, use a
381value of `default' for INHERIT; this will resolve any unspecified or
382relative values by merging with the `default' face (which is always
383completely specified)."
384 (let ((value (internal-get-lisp-face-attribute face attribute frame)))
385 (when (and inherit (face-attribute-relative-p attribute value))
386 ;; VALUE is relative, so merge with inherited faces
387 (let ((inh-from (face-attribute face :inherit frame)))
388 (unless (or (null inh-from) (eq inh-from 'unspecified))
621e71ee
SM
389 (condition-case nil
390 (setq value
391 (face-attribute-merged-with attribute value inh-from frame))
392 ;; The `inherit' attribute may point to non existent faces.
393 (error nil)))))
c7f814cb
MB
394 (when (and inherit
395 (not (eq inherit t))
396 (face-attribute-relative-p attribute value))
b6b90dfc 397 ;; We should merge with INHERIT as well
c7f814cb
MB
398 (setq value (face-attribute-merged-with attribute value inherit frame)))
399 value))
5f5c8ee5 400
c7f814cb
MB
401(defun face-attribute-merged-with (attribute value faces &optional frame)
402 "Merges ATTRIBUTE, initially VALUE, with faces from FACES until absolute.
403FACES may be either a single face or a list of faces.
522fb950 404\[This is an internal function.]"
c7f814cb
MB
405 (cond ((not (face-attribute-relative-p attribute value))
406 value)
407 ((null faces)
408 value)
409 ((consp faces)
410 (face-attribute-merged-with
411 attribute
412 (face-attribute-merged-with attribute value (car faces) frame)
413 (cdr faces)
414 frame))
415 (t
416 (merge-face-attribute attribute
417 value
418 (face-attribute faces attribute frame t)))))
419
420
421(defmacro face-attribute-specified-or (value &rest body)
422 "Return VALUE, unless it's `unspecified', in which case evaluate BODY and return the result."
423 (let ((temp (make-symbol "value")))
424 `(let ((,temp ,value))
425 (if (not (eq ,temp 'unspecified))
426 ,temp
427 ,@body))))
428
429(defun face-foreground (face &optional frame inherit)
5f5c8ee5
GM
430 "Return the foreground color name of FACE, or nil if unspecified.
431If the optional argument FRAME is given, report on face FACE in that frame.
432If FRAME is t, report on the defaults for face FACE (for new frames).
c7f814cb 433If FRAME is omitted or nil, use the selected frame.
5f5c8ee5 434
c7f814cb
MB
435If INHERIT is nil, only a foreground color directly defined by FACE is
436 considered, so the return value may be nil.
437If INHERIT is t, and FACE doesn't define a foreground color, then any
438 foreground color that FACE inherits through its `:inherit' attribute
439 is considered as well; however the return value may still be nil.
440If INHERIT is a face or a list of faces, then it is used to try to
441 resolve an unspecified foreground color.
442
443To ensure that a valid color is always returned, use a value of
444`default' for INHERIT; this will resolve any unspecified values by
445merging with the `default' face (which is always completely specified)."
446 (face-attribute-specified-or (face-attribute face :foreground frame inherit)
447 nil))
448
449(defun face-background (face &optional frame inherit)
5f5c8ee5
GM
450 "Return the background color name of FACE, or nil if unspecified.
451If the optional argument FRAME is given, report on face FACE in that frame.
452If FRAME is t, report on the defaults for face FACE (for new frames).
c7f814cb 453If FRAME is omitted or nil, use the selected frame.
5f5c8ee5 454
c7f814cb
MB
455If INHERIT is nil, only a background color directly defined by FACE is
456 considered, so the return value may be nil.
457If INHERIT is t, and FACE doesn't define a background color, then any
458 background color that FACE inherits through its `:inherit' attribute
459 is considered as well; however the return value may still be nil.
460If INHERIT is a face or a list of faces, then it is used to try to
461 resolve an unspecified background color.
462
463To ensure that a valid color is always returned, use a value of
464`default' for INHERIT; this will resolve any unspecified values by
465merging with the `default' face (which is always completely specified)."
466 (face-attribute-specified-or (face-attribute face :background frame inherit)
467 nil))
468
469(defun face-stipple (face &optional frame inherit)
5f5c8ee5
GM
470 "Return the stipple pixmap name of FACE, or nil if unspecified.
471If the optional argument FRAME is given, report on face FACE in that frame.
472If FRAME is t, report on the defaults for face FACE (for new frames).
c7f814cb
MB
473If FRAME is omitted or nil, use the selected frame.
474
475If INHERIT is nil, only a stipple directly defined by FACE is
476 considered, so the return value may be nil.
477If INHERIT is t, and FACE doesn't define a stipple, then any stipple
478 that FACE inherits through its `:inherit' attribute is considered as
479 well; however the return value may still be nil.
480If INHERIT is a face or a list of faces, then it is used to try to
481 resolve an unspecified stipple.
482
483To ensure that a valid stipple or nil is always returned, use a value of
484`default' for INHERIT; this will resolve any unspecified values by merging
485with the `default' face (which is always completely specified)."
486 (face-attribute-specified-or (face-attribute face :stipple frame inherit)
487 nil))
5f5c8ee5
GM
488
489
490(defalias 'face-background-pixmap 'face-stipple)
491
492
493(defun face-underline-p (face &optional frame)
494 "Return non-nil if FACE is underlined.
495If the optional argument FRAME is given, report on face FACE in that frame.
496If FRAME is t, report on the defaults for face FACE (for new frames).
497If FRAME is omitted or nil, use the selected frame."
498 (eq (face-attribute face :underline frame) t))
499
500
501(defun face-inverse-video-p (face &optional frame)
502 "Return non-nil if FACE is in inverse video on FRAME.
503If the optional argument FRAME is given, report on face FACE in that frame.
504If FRAME is t, report on the defaults for face FACE (for new frames).
505If FRAME is omitted or nil, use the selected frame."
506 (eq (face-attribute face :inverse-video frame) t))
507
508
509(defun face-bold-p (face &optional frame)
510 "Return non-nil if the font of FACE is bold on FRAME.
511If the optional argument FRAME is given, report on face FACE in that frame.
512If FRAME is t, report on the defaults for face FACE (for new frames).
513If FRAME is omitted or nil, use the selected frame.
514Use `face-attribute' for finer control."
515 (let ((bold (face-attribute face :weight frame)))
da2c7b8c 516 (memq bold '(semi-bold bold extra-bold ultra-bold))))
5f5c8ee5
GM
517
518
519(defun face-italic-p (face &optional frame)
520 "Return non-nil if the font of FACE is italic on FRAME.
521If the optional argument FRAME is given, report on face FACE in that frame.
522If FRAME is t, report on the defaults for face FACE (for new frames).
523If FRAME is omitted or nil, use the selected frame.
524Use `face-attribute' for finer control."
525 (let ((italic (face-attribute face :slant frame)))
a48f6020 526 (memq italic '(italic oblique))))
00f51890 527
5f5c8ee5 528
5f5c8ee5
GM
529\f
530;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
531;;; Face documentation.
532;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
533
534(defun face-documentation (face)
9a558a07
GM
535 "Get the documentation string for FACE.
536If FACE is a face-alias, get the documentation for the target face."
537 (let ((alias (get face 'face-alias))
538 doc)
539 (if alias
540 (progn
541 (setq doc (get alias 'face-documentation))
542 (format "%s is an alias for the face `%s'.%s" face alias
543 (if doc (format "\n%s" doc)
544 "")))
545 (get face 'face-documentation))))
5f5c8ee5
GM
546
547
548(defun set-face-documentation (face string)
549 "Set the documentation string for FACE to STRING."
291cfb96 550 ;; Perhaps the text should go in DOC.
49435801 551 (put face 'face-documentation (purecopy string)))
5f5c8ee5
GM
552
553
554(defalias 'face-doc-string 'face-documentation)
555(defalias 'set-face-doc-string 'set-face-documentation)
556
557
558\f
559;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
560;; Setting face attributes.
561;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
562
563
1df64254
RS
564(defvar inhibit-face-set-after-frame-default nil
565 "If non-nil, that tells `face-set-after-frame-default' to do nothing.")
566
5f5c8ee5
GM
567(defun set-face-attribute (face frame &rest args)
568 "Set attributes of FACE on FRAME from ARGS.
569
570FRAME nil means change attributes on all frames. FRAME t means change
571the default for new frames (this is done automatically each time an
572attribute is changed on all frames).
573
574ARGS must come in pairs ATTRIBUTE VALUE. ATTRIBUTE must be a valid
a7b0a0e3 575face attribute name. All attributes can be set to `unspecified';
5f5c8ee5
GM
576this fact is not further mentioned below.
577
578The following attributes are recognized:
579
580`:family'
581
582VALUE must be a string specifying the font family, e.g. ``courier'',
583or a fontset alias name. If a font family is specified, wild-cards `*'
584and `?' are allowed.
585
586`:width'
587
588VALUE specifies the relative proportionate width of the font to use.
589It must be one of the symbols `ultra-condensed', `extra-condensed',
590`condensed', `semi-condensed', `normal', `semi-expanded', `expanded',
591`extra-expanded', or `ultra-expanded'.
592
593`:height'
594
19feb949
MB
595VALUE must be either an integer specifying the height of the font to use
596in 1/10 pt, a floating point number specifying the amount by which to
597scale any underlying face, or a function, which is called with the old
598height (from the underlying face), and should return the new height.
5f5c8ee5
GM
599
600`:weight'
601
602VALUE specifies the weight of the font to use. It must be one of the
603symbols `ultra-bold', `extra-bold', `bold', `semi-bold', `normal',
604`semi-light', `light', `extra-light', `ultra-light'.
605
606`:slant'
607
608VALUE specifies the slant of the font to use. It must be one of the
609symbols `italic', `oblique', `normal', `reverse-italic', or
610`reverse-oblique'.
611
612`:foreground', `:background'
613
614VALUE must be a color name, a string.
615
616`:underline'
617
618VALUE specifies whether characters in FACE should be underlined. If
619VALUE is t, underline with foreground color of the face. If VALUE is
620a string, underline with that color. If VALUE is nil, explicitly
621don't underline.
622
623`:overline'
624
625VALUE specifies whether characters in FACE should be overlined. If
626VALUE is t, overline with foreground color of the face. If VALUE is a
627string, overline with that color. If VALUE is nil, explicitly don't
628overline.
629
630`:strike-through'
631
632VALUE specifies whether characters in FACE should be drawn with a line
633striking through them. If VALUE is t, use the foreground color of the
634face. If VALUE is a string, strike-through with that color. If VALUE
635is nil, explicitly don't strike through.
636
637`:box'
638
639VALUE specifies whether characters in FACE should have a box drawn
640around them. If VALUE is nil, explicitly don't draw boxes. If
641VALUE is t, draw a box with lines of width 1 in the foreground color
642of the face. If VALUE is a string, the string must be a color name,
643and the box is drawn in that color with a line width of 1. Otherwise,
644VALUE must be a property list of the form `(:line-width WIDTH
645:color COLOR :style STYLE)'. If a keyword/value pair is missing from
646the property list, a default value will be used for the value, as
647specified below. WIDTH specifies the width of the lines to draw; it
0f937ebd
KH
648defaults to 1. If WIDTH is negative, the absolute value is the width
649of the lines, and draw top/bottom lines inside the characters area,
650not around it. COLOR is the name of the color to draw in, default is
5f5c8ee5
GM
651the foreground color of the face for simple boxes, and the background
652color of the face for 3D boxes. STYLE specifies whether a 3D box
653should be draw. If STYLE is `released-button', draw a box looking
654like a released 3D button. If STYLE is `pressed-button' draw a box
655that appears like a pressed button. If STYLE is nil, the default if
656the property list doesn't contain a style specification, draw a 2D
657box.
658
659`:inverse-video'
660
661VALUE specifies whether characters in FACE should be displayed in
70d0c3b0 662inverse video. VALUE must be one of t or nil.
5f5c8ee5
GM
663
664`:stipple'
665
666If VALUE is a string, it must be the name of a file of pixmap data.
667The directories listed in the `x-bitmap-file-path' variable are
668searched. Alternatively, VALUE may be a list of the form (WIDTH
669HEIGHT DATA) where WIDTH and HEIGHT are the size in pixels, and DATA
670is a string containing the raw bits of the bitmap. VALUE nil means
671explicitly don't use a stipple pattern.
672
673For convenience, attributes `:family', `:width', `:height', `:weight',
674and `:slant' may also be set in one step from an X font name:
675
676`:font'
677
678Set font-related face attributes from VALUE. VALUE must be a valid
679XLFD font name. If it is a font name pattern, the first matching font
680will be used.
681
682For compatibility with Emacs 20, keywords `:bold' and `:italic' can
683be used to specify that a bold or italic font should be used. VALUE
19feb949
MB
684must be t or nil in that case. A value of `unspecified' is not allowed.
685
686`:inherit'
687
688VALUE is the name of a face from which to inherit attributes, or a list
689of face names. Attributes from inherited faces are merged into the face
690like an underlying face would be, with higher priority than underlying faces."
70a267c9
GM
691 (let ((where (if (null frame) 0 frame)))
692 (setq args (purecopy args))
680da3f8
RS
693 ;; If we set the new-frame defaults, this face is modified outside Custom.
694 (if (memq where '(0 t))
9a558a07 695 (put (or (get face 'face-alias) face) 'face-modified t))
70a267c9 696 (while args
1df64254
RS
697 ;; Don't recursively set the attributes from the frame's font param
698 ;; when we update the frame's font param fro the attributes.
699 (let ((inhibit-face-set-after-frame-default t))
700 (internal-set-lisp-face-attribute face (car args)
701 (purecopy (cadr args))
702 where))
70a267c9 703 (setq args (cdr (cdr args))))))
5f5c8ee5
GM
704
705
39cac3e7 706(defun make-face-bold (face &optional frame noerror)
5f5c8ee5
GM
707 "Make the font of FACE be bold, if possible.
708FRAME nil or not specified means change face on all frames.
39cac3e7 709Argument NOERROR is ignored and retained for compatibility.
5f5c8ee5 710Use `set-face-attribute' for finer control of the font weight."
95125512 711 (interactive (list (read-face-name "Make which face bold")))
5f5c8ee5
GM
712 (set-face-attribute face frame :weight 'bold))
713
714
39cac3e7 715(defun make-face-unbold (face &optional frame noerror)
5f5c8ee5 716 "Make the font of FACE be non-bold, if possible.
39cac3e7
GM
717FRAME nil or not specified means change face on all frames.
718Argument NOERROR is ignored and retained for compatibility."
95125512 719 (interactive (list (read-face-name "Make which face non-bold")))
5f5c8ee5
GM
720 (set-face-attribute face frame :weight 'normal))
721
00f51890 722
39cac3e7 723(defun make-face-italic (face &optional frame noerror)
5f5c8ee5
GM
724 "Make the font of FACE be italic, if possible.
725FRAME nil or not specified means change face on all frames.
39cac3e7 726Argument NOERROR is ignored and retained for compatibility.
5f5c8ee5 727Use `set-face-attribute' for finer control of the font slant."
95125512 728 (interactive (list (read-face-name "Make which face italic")))
5f5c8ee5
GM
729 (set-face-attribute face frame :slant 'italic))
730
731
39cac3e7 732(defun make-face-unitalic (face &optional frame noerror)
5f5c8ee5 733 "Make the font of FACE be non-italic, if possible.
70d0c3b0
DL
734FRAME nil or not specified means change face on all frames.
735Argument NOERROR is ignored and retained for compatibility."
95125512 736 (interactive (list (read-face-name "Make which face non-italic")))
5f5c8ee5
GM
737 (set-face-attribute face frame :slant 'normal))
738
00f51890 739
39cac3e7 740(defun make-face-bold-italic (face &optional frame noerror)
5f5c8ee5
GM
741 "Make the font of FACE be bold and italic, if possible.
742FRAME nil or not specified means change face on all frames.
39cac3e7 743Argument NOERROR is ignored and retained for compatibility.
5f5c8ee5 744Use `set-face-attribute' for finer control of font weight and slant."
95125512 745 (interactive (list (read-face-name "Make which face bold-italic")))
5f5c8ee5
GM
746 (set-face-attribute face frame :weight 'bold :slant 'italic))
747
748
749(defun set-face-font (face font &optional frame)
750 "Change font-related attributes of FACE to those of FONT (a string).
751FRAME nil or not specified means change face on all frames.
752This sets the attributes `:family', `:width', `:height', `:weight',
753and `:slant'. When called interactively, prompt for the face and font."
754 (interactive (read-face-and-attribute :font))
755 (set-face-attribute face frame :font font))
756
757
758;; Implementation note: Emulating gray background colors with a
759;; stipple pattern is now part of the face realization process, and is
760;; done in C depending on the frame on which the face is realized.
761
762(defun set-face-background (face color &optional frame)
763 "Change the background color of face FACE to COLOR (a string).
764FRAME nil or not specified means change face on all frames.
72e6f142
RS
765COLOR can be a system-defined color name (see `list-colors-display')
766or a hex spec of the form #RRGGBB.
767When called interactively, prompts for the face and color."
5f5c8ee5 768 (interactive (read-face-and-attribute :background))
78ad1cd5 769 (set-face-attribute face frame :background (or color 'unspecified)))
5f5c8ee5
GM
770
771
772(defun set-face-foreground (face color &optional frame)
773 "Change the foreground color of face FACE to COLOR (a string).
774FRAME nil or not specified means change face on all frames.
72e6f142
RS
775COLOR can be a system-defined color name (see `list-colors-display')
776or a hex spec of the form #RRGGBB.
777When called interactively, prompts for the face and color."
5f5c8ee5 778 (interactive (read-face-and-attribute :foreground))
78ad1cd5 779 (set-face-attribute face frame :foreground (or color 'unspecified)))
5f5c8ee5
GM
780
781
782(defun set-face-stipple (face stipple &optional frame)
783 "Change the stipple pixmap of face FACE to STIPPLE.
784FRAME nil or not specified means change face on all frames.
70d0c3b0 785STIPPLE should be a string, the name of a file of pixmap data.
5f5c8ee5
GM
786The directories listed in the `x-bitmap-file-path' variable are searched.
787
788Alternatively, STIPPLE may be a list of the form (WIDTH HEIGHT DATA)
789where WIDTH and HEIGHT are the size in pixels,
790and DATA is a string, containing the raw bits of the bitmap."
791 (interactive (read-face-and-attribute :stipple))
78ad1cd5 792 (set-face-attribute face frame :stipple (or stipple 'unspecified)))
5f5c8ee5
GM
793
794
a61afeac 795(defun set-face-underline-p (face underline &optional frame)
5f5c8ee5
GM
796 "Specify whether face FACE is underlined.
797UNDERLINE nil means FACE explicitly doesn't underline.
798UNDERLINE non-nil means FACE explicitly does underlining
799with the same of the foreground color.
800If UNDERLINE is a string, underline with the color named UNDERLINE.
801FRAME nil or not specified means change face on all frames.
5f5c8ee5
GM
802Use `set-face-attribute' to ``unspecify'' underlining."
803 (interactive
804 (let ((list (read-face-and-attribute :underline)))
805 (list (car list) (eq (car (cdr list)) t))))
a61afeac 806 (set-face-attribute face frame :underline underline))
5f5c8ee5 807
9b82fc01
JL
808(define-obsolete-function-alias 'set-face-underline
809 'set-face-underline-p "22.1")
810
5f5c8ee5
GM
811
812(defun set-face-inverse-video-p (face inverse-video-p &optional frame)
813 "Specify whether face FACE is in inverse video.
814INVERSE-VIDEO-P non-nil means FACE displays explicitly in inverse video.
815INVERSE-VIDEO-P nil means FACE explicitly is not in inverse video.
816FRAME nil or not specified means change face on all frames.
817Use `set-face-attribute' to ``unspecify'' the inverse video attribute."
818 (interactive
819 (let ((list (read-face-and-attribute :inverse-video)))
820 (list (car list) (eq (car (cdr list)) t))))
821 (set-face-attribute face frame :inverse-video inverse-video-p))
822
823
824(defun set-face-bold-p (face bold-p &optional frame)
825 "Specify whether face FACE is bold.
826BOLD-P non-nil means FACE should explicitly display bold.
827BOLD-P nil means FACE should explicitly display non-bold.
828FRAME nil or not specified means change face on all frames.
829Use `set-face-attribute' or `modify-face' for finer control."
830 (if (null bold-p)
831 (make-face-unbold face frame)
832 (make-face-bold face frame)))
833
834
835(defun set-face-italic-p (face italic-p &optional frame)
836 "Specify whether face FACE is italic.
837ITALIC-P non-nil means FACE should explicitly display italic.
838ITALIC-P nil means FACE should explicitly display non-italic.
839FRAME nil or not specified means change face on all frames.
840Use `set-face-attribute' or `modify-face' for finer control."
841 (if (null italic-p)
842 (make-face-unitalic face frame)
843 (make-face-italic face frame)))
844
845
846(defalias 'set-face-background-pixmap 'set-face-stipple)
847
848
849(defun invert-face (face &optional frame)
850 "Swap the foreground and background colors of FACE.
e458cb49 851If FRAME is omitted or nil, it means change face on all frames.
5f5c8ee5
GM
852If FACE specifies neither foreground nor background color,
853set its foreground and background to the background and foreground
854of the default face. Value is FACE."
95125512 855 (interactive (list (read-face-name "Invert face")))
5f5c8ee5
GM
856 (let ((fg (face-attribute face :foreground frame))
857 (bg (face-attribute face :background frame)))
e458cb49 858 (if (not (and (eq fg 'unspecified) (eq bg 'unspecified)))
5f5c8ee5
GM
859 (set-face-attribute face frame :foreground bg :background fg)
860 (set-face-attribute face frame
861 :foreground
862 (face-attribute 'default :background frame)
863 :background
864 (face-attribute 'default :foreground frame))))
865 face)
866
867\f
868;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
869;;; Interactively modifying faces.
870;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
871
a482f364
RS
872(defun read-face-name (prompt &optional string-describing-default multiple)
873 "Read a face, defaulting to the face or faces on the char after point.
c7b93b44 874If it has the property `read-face-name', that overrides the `face' property.
3748d13d
RS
875PROMPT should be a string that describes what the caller will do with the face;
876it should not end in a space.
877STRING-DESCRIBING-DEFAULT should describe what default the caller will use if
878the user just types RET; you can omit it.
a482f364
RS
879If MULTIPLE is non-nil, return a list of faces (possibly only one).
880Otherwise, return a single face."
881 (let ((faceprop (or (get-char-property (point) 'read-face-name)
882 (get-char-property (point) 'face)))
e3b5b35b
SM
883 (aliasfaces nil)
884 (nonaliasfaces nil)
a482f364 885 faces)
224d108e
JL
886 ;; Try to get a face name from the buffer.
887 (if (memq (intern-soft (thing-at-point 'symbol)) (face-list))
888 (setq faces (list (intern-soft (thing-at-point 'symbol)))))
889 ;; Add the named faces that the `face' property uses.
1bd59f82
RS
890 (if (and (listp faceprop)
891 ;; Don't treat an attribute spec as a list of faces.
892 (not (keywordp (car faceprop)))
893 (not (memq (car faceprop) '(foreground-color background-color))))
a482f364
RS
894 (dolist (f faceprop)
895 (if (symbolp f)
896 (push f faces)))
897 (if (symbolp faceprop)
1bd59f82 898 (push faceprop faces)))
81435dc4 899 (delete-dups faces)
a482f364 900
e3b5b35b
SM
901 ;; Build up the completion tables.
902 (mapatoms (lambda (s)
903 (if (custom-facep s)
904 (if (get s 'face-alias)
905 (push (symbol-name s) aliasfaces)
906 (push (symbol-name s) nonaliasfaces)))))
907
a482f364
RS
908 ;; If we only want one, and the default is more than one,
909 ;; discard the unwanted ones now.
910 (unless multiple
911 (if faces
912 (setq faces (list (car faces)))))
224d108e 913 (require 'crm)
a482f364
RS
914 (let* ((input
915 ;; Read the input.
224d108e 916 (completing-read-multiple
a482f364
RS
917 (if (or faces string-describing-default)
918 (format "%s (default %s): " prompt
224d108e 919 (if faces (mapconcat 'symbol-name faces ",")
a482f364 920 string-describing-default))
3b4ef34b 921 (format "%s: " prompt))
45555ebe 922 (completion-table-in-turn nonaliasfaces aliasfaces)
224d108e
JL
923 nil t nil nil
924 (if faces (mapconcat 'symbol-name faces ","))))
a482f364
RS
925 ;; Canonicalize the output.
926 (output
224d108e
JL
927 (cond ((or (equal input "") (equal input '("")))
928 faces)
929 ((stringp input)
930 (mapcar 'intern (split-string input ", *" t)))
931 ((listp input)
932 (mapcar 'intern input))
933 (input))))
a482f364
RS
934 ;; Return either a list of faces or just one face.
935 (if multiple
936 output
937 (car output)))))
938
aa360da1
GM
939;; Not defined without X, but behind window-system test.
940(defvar x-bitmap-file-path)
a482f364 941
5f5c8ee5
GM
942(defun face-valid-attribute-values (attribute &optional frame)
943 "Return valid values for face attribute ATTRIBUTE.
944The optional argument FRAME is used to determine available fonts
945and colors. If it is nil or not specified, the selected frame is
946used. Value is an alist of (NAME . VALUE) if ATTRIBUTE expects a value
947out of a set of discrete values. Value is `integerp' if ATTRIBUTE expects
948an integer value."
00f51890
SS
949 (let ((valid
950 (case attribute
951 (:family
428a555e 952 (if (window-system frame)
00f51890 953 (mapcar #'(lambda (x) (cons (car x) (car x)))
6e783e91 954 (font-family-list))
00f51890
SS
955 ;; Only one font on TTYs.
956 (list (cons "default" "default"))))
6e783e91
KH
957 (:width
958 (mapcar #'(lambda (x) (cons (symbol-name (car x)) (car x)))
959 font-width-table))
960 (:weight
961 (mapcar #'(lambda (x) (cons (symbol-name (car x)) (car x)))
962 font-weight-table))
963 (:slant
964 (mapcar #'(lambda (x) (cons (symbol-name (car x)) (car x)))
965 font-slant-table))
966 (:inverse-video
967 (mapcar #'(lambda (x) (cons (symbol-name x) x))
968 (internal-lisp-face-attribute-values attribute)))
00f51890 969 ((:underline :overline :strike-through :box)
428a555e 970 (if (window-system frame)
00f51890
SS
971 (nconc (mapcar #'(lambda (x) (cons (symbol-name x) x))
972 (internal-lisp-face-attribute-values attribute))
973 (mapcar #'(lambda (c) (cons c c))
b533d0ae 974 (defined-colors frame)))
00f51890
SS
975 (mapcar #'(lambda (x) (cons (symbol-name x) x))
976 (internal-lisp-face-attribute-values attribute))))
977 ((:foreground :background)
978 (mapcar #'(lambda (c) (cons c c))
979 (defined-colors frame)))
980 ((:height)
981 'integerp)
982 (:stipple
428a555e 983 (and (memq (window-system frame) '(x w32 mac))
00f51890
SS
984 (mapcar #'list
985 (apply #'nconc
986 (mapcar (lambda (dir)
987 (and (file-readable-p dir)
988 (file-directory-p dir)
989 (directory-files dir)))
990 x-bitmap-file-path)))))
991 (:inherit
992 (cons '("none" . nil)
993 (mapcar #'(lambda (c) (cons (symbol-name c) c))
994 (face-list))))
995 (t
996 (error "Internal error")))))
19feb949 997 (if (and (listp valid) (not (memq attribute '(:inherit))))
fbd5f1cc
GM
998 (nconc (list (cons "unspecified" 'unspecified)) valid)
999 valid)))
5f5c8ee5
GM
1000
1001
1002(defvar face-attribute-name-alist
1003 '((:family . "font family")
1004 (:width . "character set width")
1005 (:height . "height in 1/10 pt")
1006 (:weight . "weight")
1007 (:slant . "slant")
1008 (:underline . "underline")
1009 (:overline . "overline")
1010 (:strike-through . "strike-through")
1011 (:box . "box")
1012 (:inverse-video . "inverse-video display")
1013 (:foreground . "foreground color")
1014 (:background . "background color")
19feb949
MB
1015 (:stipple . "background stipple")
1016 (:inherit . "inheritance"))
5f5c8ee5
GM
1017 "An alist of descriptive names for face attributes.
1018Each element has the form (ATTRIBUTE-NAME . DESCRIPTION) where
1019ATTRIBUTE-NAME is a face attribute name (a keyword symbol), and
1020DESCRIPTION is a descriptive name for ATTRIBUTE-NAME.")
1021
1022
1023(defun face-descriptive-attribute-name (attribute)
1024 "Return a descriptive name for ATTRIBUTE."
1025 (cdr (assq attribute face-attribute-name-alist)))
1026
1027
1028(defun face-read-string (face default name &optional completion-alist)
1029 "Interactively read a face attribute string value.
19feb949
MB
1030FACE is the face whose attribute is read. If non-nil, DEFAULT is the
1031default string to return if no new value is entered. NAME is a
1032descriptive name of the attribute for prompting. COMPLETION-ALIST is an
1033alist of valid values, if non-nil.
5f5c8ee5 1034
19feb949 1035Entering nothing accepts the default string DEFAULT.
5f5c8ee5 1036Value is the new attribute value."
19feb949
MB
1037 ;; Capitalize NAME (we don't use `capitalize' because that capitalizes
1038 ;; each word in a string separately).
1039 (setq name (concat (upcase (substring name 0 1)) (substring name 1)))
5f5c8ee5
GM
1040 (let* ((completion-ignore-case t)
1041 (value (completing-read
1042 (if default
19feb949
MB
1043 (format "%s for face `%s' (default %s): "
1044 name face default)
1045 (format "%s for face `%s': " name face))
d69ab4ba 1046 completion-alist nil nil nil nil default)))
fbd5f1cc 1047 (if (equal value "") default value)))
5f5c8ee5
GM
1048
1049
1050(defun face-read-integer (face default name)
1051 "Interactively read an integer face attribute value.
1052FACE is the face whose attribute is read. DEFAULT is the default
1053value to return if no new value is entered. NAME is a descriptive
1054name of the attribute for prompting. Value is the new attribute value."
fbd5f1cc
GM
1055 (let ((new-value
1056 (face-read-string face
19feb949 1057 (format "%s" default)
fbd5f1cc
GM
1058 name
1059 (list (cons "unspecified" 'unspecified)))))
19feb949
MB
1060 (cond ((equal new-value "unspecified")
1061 'unspecified)
1062 ((member new-value '("unspecified-fg" "unspecified-bg"))
1063 new-value)
1064 (t
027a4b6b 1065 (string-to-number new-value)))))
5f5c8ee5
GM
1066
1067
1068(defun read-face-attribute (face attribute &optional frame)
1069 "Interactively read a new value for FACE's ATTRIBUTE.
1070Optional argument FRAME nil or unspecified means read an attribute value
1071of a global face. Value is the new attribute value."
1072 (let* ((old-value (face-attribute face attribute frame))
1073 (attribute-name (face-descriptive-attribute-name attribute))
1074 (valid (face-valid-attribute-values attribute frame))
1075 new-value)
1076 ;; Represent complex attribute values as strings by printing them
1077 ;; out. Stipple can be a vector; (WIDTH HEIGHT DATA). Box can be
1078 ;; a list `(:width WIDTH :color COLOR)' or `(:width WIDTH :shadow
1079 ;; SHADOW)'.
1080 (when (and (or (eq attribute :stipple)
1081 (eq attribute :box))
1082 (or (consp old-value)
1083 (vectorp old-value)))
1084 (setq old-value (prin1-to-string old-value)))
1085 (cond ((listp valid)
19feb949
MB
1086 (let ((default
1087 (or (car (rassoc old-value valid))
1088 (format "%s" old-value))))
1089 (setq new-value
1090 (face-read-string face default attribute-name valid))
1091 (if (equal new-value default)
1092 ;; Nothing changed, so don't bother with all the stuff
1093 ;; below. In particular, this avoids a non-tty color
1094 ;; from being canonicalized for a tty when the user
1095 ;; just uses the default.
1096 (setq new-value old-value)
1097 ;; Terminal frames can support colors that don't appear
1098 ;; explicitly in VALID, using color approximation code
1099 ;; in tty-colors.el.
f9056dd9 1100 (when (and (memq attribute '(:foreground :background))
428a555e 1101 (not (memq (window-system frame) '(x w32 mac)))
f9056dd9
MB
1102 (not (member new-value
1103 '("unspecified"
1104 "unspecified-fg" "unspecified-bg"))))
4385f1b0 1105 (setq new-value (car (tty-color-desc new-value frame))))
f9056dd9
MB
1106 (when (assoc new-value valid)
1107 (setq new-value (cdr (assoc new-value valid)))))))
5f5c8ee5
GM
1108 ((eq valid 'integerp)
1109 (setq new-value (face-read-integer face old-value attribute-name)))
1110 (t (error "Internal error")))
1111 ;; Convert stipple and box value text we read back to a list or
1112 ;; vector if it looks like one. This makes the assumption that a
1113 ;; pixmap file name won't start with an open-paren.
1114 (when (and (or (eq attribute :stipple)
1115 (eq attribute :box))
1116 (stringp new-value)
1117 (string-match "^[[(]" new-value))
1118 (setq new-value (read new-value)))
1119 new-value))
1120
aa360da1
GM
1121(declare-function fontset-list "fontset.c" ())
1122(declare-function x-list-fonts "xfaces.c"
1123 (pattern &optional face frame maximum width))
5f5c8ee5
GM
1124
1125(defun read-face-font (face &optional frame)
1126 "Read the name of a font for FACE on FRAME.
522fb950 1127If optional argument FRAME is nil or omitted, use the selected frame."
5f5c8ee5 1128 (let ((completion-ignore-case t))
48ec9939 1129 (completing-read (format "Set font attributes of face `%s' from font: " face)
6b61353c 1130 (append (fontset-list) (x-list-fonts "*" nil frame)))))
5f5c8ee5
GM
1131
1132
1133(defun read-all-face-attributes (face &optional frame)
1134 "Interactively read all attributes for FACE.
522fb950 1135If optional argument FRAME is nil or omitted, use the selected frame.
5f5c8ee5
GM
1136Value is a property list of attribute names and new values."
1137 (let (result)
1138 (dolist (attribute face-attribute-name-alist result)
1139 (setq result (cons (car attribute)
1140 (cons (read-face-attribute face (car attribute) frame)
1141 result))))))
1142
7cd512f2 1143(defun modify-face (&optional face foreground background stipple
a61afeac 1144 bold-p italic-p underline inverse-p frame)
5f5c8ee5
GM
1145 "Modify attributes of faces interactively.
1146If optional argument FRAME is nil or omitted, modify the face used
7cd512f2
SM
1147for newly created frame, i.e. the global face.
1148For non-interactive use, `set-face-attribute' is preferred.
522fb950 1149When called from Lisp, if FACE is nil, all arguments but FRAME are ignored
7cd512f2 1150and the face and its settings are obtained by querying the user."
5f5c8ee5 1151 (interactive)
7cd512f2
SM
1152 (if face
1153 (set-face-attribute face frame
1154 :foreground (or foreground 'unspecified)
1155 :background (or background 'unspecified)
1156 :stipple stipple
1157 :bold bold-p
1158 :italic italic-p
a61afeac 1159 :underline underline
7cd512f2
SM
1160 :inverse-video inverse-p)
1161 (setq face (read-face-name "Modify face"))
5f5c8ee5
GM
1162 (apply #'set-face-attribute face frame
1163 (read-all-face-attributes face frame))))
1164
5f5c8ee5
GM
1165(defun read-face-and-attribute (attribute &optional frame)
1166 "Read face name and face attribute value.
1167ATTRIBUTE is the attribute whose new value is read.
1168FRAME nil or unspecified means read attribute value of global face.
1169Value is a list (FACE NEW-VALUE) where FACE is the face read
7cd512f2 1170\(a symbol), and NEW-VALUE is value read."
5f5c8ee5 1171 (cond ((eq attribute :font)
48ec9939 1172 (let* ((prompt "Set font-related attributes of face")
5f5c8ee5
GM
1173 (face (read-face-name prompt))
1174 (font (read-face-font face frame)))
1175 (list face font)))
1176 (t
1177 (let* ((attribute-name (face-descriptive-attribute-name attribute))
19feb949 1178 (prompt (format "Set %s of face" attribute-name))
5f5c8ee5
GM
1179 (face (read-face-name prompt))
1180 (new-value (read-face-attribute face attribute frame)))
1181 (list face new-value)))))
1182
1183
1184\f
1185;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1186;;; Listing faces.
1187;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1188
1189(defvar list-faces-sample-text
1190 "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1191 "*Text string to display as the sample text for `list-faces-display'.")
1192
1193
1194;; The name list-faces would be more consistent, but let's avoid a
1195;; conflict with Lucid, which uses that name differently.
1196
645a1038 1197(defvar help-xref-stack)
e2c11e3b 1198(defun list-faces-display (&optional regexp)
5f5c8ee5
GM
1199 "List all faces, using the same sample text in each.
1200The sample text is a string that comes from the variable
e2c11e3b
RS
1201`list-faces-sample-text'.
1202
1203If REGEXP is non-nil, list only those faces with names matching
1204this regular expression. When called interactively with a prefix
1205arg, prompt for a regular expression."
1206 (interactive (list (and current-prefix-arg
1207 (read-string "List faces matching regexp: "))))
011131fd 1208 (let ((all-faces (zerop (length regexp)))
5f5c8ee5 1209 (frame (selected-frame))
011131fd
JB
1210 (max-length 0)
1211 faces line-format
84fe35f0 1212 disp-frame window face-name)
011131fd
JB
1213 ;; We filter and take the max length in one pass
1214 (setq faces
1215 (delq nil
1216 (mapcar (lambda (f)
1217 (let ((s (symbol-name f)))
1218 (when (or all-faces (string-match regexp s))
1219 (setq max-length (max (length s) max-length))
1220 f)))
1221 (sort (face-list) #'string-lessp))))
1222 (unless faces
1223 (error "No faces matching \"%s\"" regexp))
1224 (setq max-length (1+ max-length)
1225 line-format (format "%%-%ds" max-length))
b766be6a 1226 (with-help-window "*Faces*"
5f5c8ee5
GM
1227 (save-excursion
1228 (set-buffer standard-output)
1229 (setq truncate-lines t)
84fe35f0
DL
1230 (insert
1231 (substitute-command-keys
1232 (concat
1233 "Use "
53c80cf6 1234 (if (display-mouse-p) "\\[help-follow-mouse] or ")
32e33f60 1235 "\\[help-follow] on a face name to customize it\n"
46057f5f 1236 "or on its sample text for a description of the face.\n\n")))
84fe35f0 1237 (setq help-xref-stack nil)
e2c11e3b 1238 (dolist (face faces)
84fe35f0 1239 (setq face-name (symbol-name face))
011131fd 1240 (insert (format line-format face-name))
84fe35f0
DL
1241 ;; Hyperlink to a customization buffer for the face. Using
1242 ;; the help xref mechanism may not be the best way.
1243 (save-excursion
1244 (save-match-data
1245 (search-backward face-name)
35a0a8aa 1246 (setq help-xref-stack-item `(list-faces-display ,regexp))
a482f364
RS
1247 (help-xref-button 0 'help-customize-face face)))
1248 (let ((beg (point))
1249 (line-beg (line-beginning-position)))
5f5c8ee5 1250 (insert list-faces-sample-text)
84fe35f0
DL
1251 ;; Hyperlink to a help buffer for the face.
1252 (save-excursion
1253 (save-match-data
1254 (search-backward list-faces-sample-text)
8940232b 1255 (help-xref-button 0 'help-face face)))
5f5c8ee5
GM
1256 (insert "\n")
1257 (put-text-property beg (1- (point)) 'face face)
a482f364
RS
1258 ;; Make all face commands default to the proper face
1259 ;; anywhere in the line.
1260 (put-text-property line-beg (1- (point)) 'read-face-name face)
5f5c8ee5
GM
1261 ;; If the sample text has multiple lines, line up all of them.
1262 (goto-char beg)
1263 (forward-line 1)
1264 (while (not (eobp))
011131fd 1265 (insert-char ?\s max-length)
5f5c8ee5 1266 (forward-line 1))))
b766be6a 1267 (goto-char (point-min))))
5f5c8ee5
GM
1268 ;; If the *Faces* buffer appears in a different frame,
1269 ;; copy all the face definitions from FRAME,
1270 ;; so that the display will reflect the frame that was selected.
1271 (setq window (get-buffer-window (get-buffer "*Faces*") t))
1272 (setq disp-frame (if window (window-frame window)
1273 (car (frame-list))))
1274 (or (eq frame disp-frame)
1275 (let ((faces (face-list)))
1276 (while faces
1277 (copy-face (car faces) (car faces) frame disp-frame)
1278 (setq faces (cdr faces)))))))
1279
e2c11e3b 1280
5f5c8ee5
GM
1281(defun describe-face (face &optional frame)
1282 "Display the properties of face FACE on FRAME.
6afa5404 1283Interactively, FACE defaults to the faces of the character after point
a482f364
RS
1284and FRAME defaults to the selected frame.
1285
5f5c8ee5
GM
1286If the optional argument FRAME is given, report on face FACE in that frame.
1287If FRAME is t, report on the defaults for face FACE (for new frames).
1288If FRAME is omitted or nil, use the selected frame."
a482f364 1289 (interactive (list (read-face-name "Describe face" "= `default' face" t)))
5f5c8ee5
GM
1290 (let* ((attrs '((:family . "Family")
1291 (:width . "Width")
1292 (:height . "Height")
1293 (:weight . "Weight")
1294 (:slant . "Slant")
1295 (:foreground . "Foreground")
1296 (:background . "Background")
1297 (:underline . "Underline")
1298 (:overline . "Overline")
1299 (:strike-through . "Strike-through")
1300 (:box . "Box")
1301 (:inverse-video . "Inverse")
b32631c8 1302 (:stipple . "Stipple")
0540dc5e
KH
1303 (:font . "Font")
1304 (:fontset . "Fontset")
25ac7b52 1305 (:inherit . "Inherit")))
5f5c8ee5
GM
1306 (max-width (apply #'max (mapcar #'(lambda (x) (length (cdr x)))
1307 attrs))))
2cb95d1b 1308 (help-setup-xref (list #'describe-face face) (interactive-p))
a482f364
RS
1309 (unless face
1310 (setq face 'default))
1311 (if (not (listp face))
1312 (setq face (list face)))
b766be6a 1313 (with-help-window (help-buffer)
5f5c8ee5
GM
1314 (save-excursion
1315 (set-buffer standard-output)
a482f364 1316 (dolist (f face)
faa3d27b 1317 (if (stringp f) (setq f (intern f)))
a482f364
RS
1318 (insert "Face: " (symbol-name f))
1319 (if (not (facep f))
1320 (insert " undefined face.\n")
19941ff5
RS
1321 (let ((customize-label "customize this face")
1322 file-name)
1b2b8998 1323 (insert (concat " (" (propertize "sample" 'font-lock-face f) ")"))
a482f364
RS
1324 (princ (concat " (" customize-label ")\n"))
1325 (insert "Documentation: "
1326 (or (face-documentation f)
f79f9fb2 1327 "Not documented as a face.")
19941ff5 1328 "\n")
a482f364
RS
1329 (with-current-buffer standard-output
1330 (save-excursion
1331 (re-search-backward
1332 (concat "\\(" customize-label "\\)") nil t)
1333 (help-xref-button 1 'help-customize-face f)))
19941ff5
RS
1334 ;; The next 4 sexps are copied from describe-function-1
1335 ;; and simplified.
1336 (setq file-name (symbol-file f 'defface))
d1a74d58 1337 (setq file-name (describe-simplify-lib-file-name file-name))
19941ff5
RS
1338 (when file-name
1339 (princ "Defined in `")
1340 (princ file-name)
1341 (princ "'")
1342 ;; Make a hyperlink to the library.
1343 (save-excursion
1344 (re-search-backward "`\\([^`']+\\)'" nil t)
1345 (help-xref-button 1 'help-face-def f file-name))
1346 (princ ".")
1347 (terpri)
1348 (terpri))
a482f364
RS
1349 (dolist (a attrs)
1350 (let ((attr (face-attribute f (car a) frame)))
522fb950 1351 (insert (make-string (- max-width (length (cdr a))) ?\s)
9b82fc01
JL
1352 (cdr a) ": " (format "%s" attr))
1353 (if (and (eq (car a) :inherit)
1354 (not (eq attr 'unspecified)))
1355 ;; Make a hyperlink to the parent face.
1356 (save-excursion
1357 (re-search-backward ": \\([^:]+\\)" nil t)
1358 (help-xref-button 1 'help-face attr)))
1359 (insert "\n")))))
b766be6a 1360 (terpri))))))
8940232b 1361
5f5c8ee5
GM
1362\f
1363;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1364;;; Face specifications (defface).
1365;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1366
1367;; Parameter FRAME Is kept for call compatibility to with previous
1368;; face implementation.
1369
1370(defun face-attr-construct (face &optional frame)
522fb950 1371 "Return a `defface'-style attribute list for FACE on FRAME.
5f5c8ee5
GM
1372Value is a property list of pairs ATTRIBUTE VALUE for all specified
1373face attributes of FACE where ATTRIBUTE is the attribute name and
1374VALUE is the specified value of that attribute."
1375 (let (result)
1376 (dolist (entry face-attribute-name-alist result)
1377 (let* ((attribute (car entry))
1378 (value (face-attribute face attribute)))
1379 (unless (eq value 'unspecified)
1380 (setq result (nconc (list attribute value) result)))))))
00f51890 1381
5f5c8ee5
GM
1382
1383(defun face-spec-set-match-display (display frame)
1384 "Non-nil if DISPLAY matches FRAME.
1385DISPLAY is part of a spec such as can be used in `defface'.
1386If FRAME is nil, the current FRAME is used."
1387 (let* ((conjuncts display)
1388 conjunct req options
1389 ;; t means we have succeeded against all the conjuncts in
1390 ;; DISPLAY that have been tested so far.
1391 (match t))
1392 (if (eq conjuncts t)
1393 (setq conjuncts nil))
1394 (while (and conjuncts match)
1395 (setq conjunct (car conjuncts)
1396 conjuncts (cdr conjuncts)
1397 req (car conjunct)
1398 options (cdr conjunct)
1399 match (cond ((eq req 'type)
428a555e 1400 (or (memq (window-system frame) options)
1695ca2b
EZ
1401 ;; FIXME: This should be revisited to use
1402 ;; display-graphic-p, provided that the
1403 ;; color selection depends on the number
1404 ;; of supported colors, and all defface's
1405 ;; are changed to look at number of colors
1406 ;; instead of (type graphic) etc.
428a555e 1407 (and (null (window-system frame))
1695ca2b 1408 (memq 'tty options))
ee716db5
GM
1409 (and (memq 'motif options)
1410 (featurep 'motif))
9b44aa8b
JD
1411 (and (memq 'gtk options)
1412 (featurep 'gtk))
ee716db5
GM
1413 (and (memq 'lucid options)
1414 (featurep 'x-toolkit)
9b44aa8b
JD
1415 (not (featurep 'motif))
1416 (not (featurep 'gtk)))
ee716db5
GM
1417 (and (memq 'x-toolkit options)
1418 (featurep 'x-toolkit))))
6b61353c
KH
1419 ((eq req 'min-colors)
1420 (>= (display-color-cells frame) (car options)))
5f5c8ee5
GM
1421 ((eq req 'class)
1422 (memq (frame-parameter frame 'display-type) options))
1423 ((eq req 'background)
1424 (memq (frame-parameter frame 'background-mode)
1425 options))
16320ac7
MB
1426 ((eq req 'supports)
1427 (display-supports-face-attributes-p options frame))
70d0c3b0 1428 (t (error "Unknown req `%S' with options `%S'"
5f5c8ee5
GM
1429 req options)))))
1430 match))
1431
1432
1433(defun face-spec-choose (spec &optional frame)
96c2938f
MB
1434 "Choose the proper attributes for FRAME, out of SPEC.
1435If SPEC is nil, return nil."
5f5c8ee5
GM
1436 (unless frame
1437 (setq frame (selected-frame)))
1438 (let ((tail spec)
cf459bb7 1439 result defaults)
5f5c8ee5 1440 (while tail
073f69e6
MB
1441 (let* ((entry (pop tail))
1442 (display (car entry))
cf459bb7
RS
1443 (attrs (cdr entry))
1444 thisval)
1445 ;; Get the attributes as actually specified by this alternative.
1446 (setq thisval
1447 (if (null (cdr attrs)) ;; was (listp (car attrs))
1448 ;; Old-style entry, the attribute list is the
1449 ;; first element.
1450 (car attrs)
1451 attrs))
1452
1453 ;; If the condition is `default', that sets the default
1454 ;; for following conditions.
1455 (if (eq display 'default)
1456 (setq defaults thisval)
1457 ;; Otherwise, if it matches, use it.
1458 (when (face-spec-set-match-display display frame)
1459 (setq result thisval)
b6b90dfc 1460 (setq tail nil)))))
cf459bb7 1461 (if defaults (append result defaults) result)))
5f5c8ee5
GM
1462
1463
1464(defun face-spec-reset-face (face &optional frame)
1465 "Reset all attributes of FACE on FRAME to unspecified."
bfe27647 1466 (let ((attrs face-attribute-name-alist))
5f5c8ee5
GM
1467 (while attrs
1468 (let ((attr-and-name (car attrs)))
bfe27647
GM
1469 (set-face-attribute face frame (car attr-and-name) 'unspecified))
1470 (setq attrs (cdr attrs)))))
5f5c8ee5
GM
1471
1472
24837f13 1473(defun face-spec-set (face spec &optional for-defface)
a1792b9b 1474 "Set FACE's face spec, which controls its appearance, to SPEC.
24837f13
RS
1475If FOR-DEFFACE is t, set the base spec, the one that `defface'
1476 and Custom set. (In that case, the caller must put it in the
1477 appropriate property, because that depends on the caller.)
1478If FOR-DEFFACE is nil, set the overriding spec (and store it
1479 in the `face-override-spec' property of FACE).
1480
1481The appearance of FACE is controlled by the base spec,
1482by any custom theme specs on top of that, and by the
a1792b9b 1483overriding spec on top of all the rest.
24837f13
RS
1484
1485FOR-DEFFACE can also be a frame, in which case we set the
1486frame-specific attributes of FACE for that frame based on SPEC.
1487That usage is deprecated.
1488
1489See `defface' for information about the format and meaning of SPEC."
1490 (if (framep for-defface)
1491 ;; Handle the deprecated case where third arg is a frame.
1492 (face-spec-set-2 face for-defface spec)
1493 (if for-defface
1494 ;; When we reset the face based on its custom spec, then it is
1495 ;; unmodified as far as Custom is concerned.
1496 (put (or (get face 'face-alias) face) 'face-modified nil)
1497 ;; When we change a face based on a spec from outside custom,
1498 ;; record it for future frames.
1499 (put (or (get face 'face-alias) face) 'face-override-spec spec))
1500;;; RMS 29 dec 2007: Perhaps this code should be reinstated.
1501;;; That depends on whether the overriding spec
1502;;; or the default face attributes
1503;;; should take priority.
1504;;; ;; Clear all the new-frame default attributes for this face.
26c07a69
RS
1505;;; ;; face-spec-reset-face won't do it right.
1506;;; (let ((facevec (cdr (assq face face-new-frame-defaults))))
1507;;; (dotimes (i (length facevec))
1508;;; (unless (= i 0)
1509;;; (aset facevec i 'unspecified))))
24837f13 1510 ;; Reset each frame according to the rules implied by all its specs.
01d2eac8 1511 (dolist (frame (frame-list))
24837f13
RS
1512 (face-spec-recalc face frame))))
1513
1514(defun face-spec-recalc (face frame)
1515 "Reset the face attributes of FACE on FRAME according to its specs.
1516This applies the defface/custom spec first, then the custom theme specs,
1517then the override spec."
1518 (face-spec-reset-face face frame)
1519 (let ((face-sym (or (get face 'face-alias) face)))
7ecc4011
CY
1520 (or (get face 'customized-face)
1521 (get face 'saved-face)
1522 (face-spec-set-2 face frame (face-default-spec face)))
24837f13
RS
1523 (let ((theme-faces (reverse (get face-sym 'theme-face))))
1524 (dolist (spec theme-faces)
1525 (face-spec-set-2 face frame (cadr spec))))
1526 (face-spec-set-2 face frame (get face-sym 'face-override-spec))))
1527
1528(defun face-spec-set-2 (face frame spec)
1529 "Set the face attributes of FACE on FRAME according to SPEC."
1530 (let* ((attrs (face-spec-choose spec frame)))
1531 (while attrs
1532 (let ((attribute (car attrs))
1533 (value (car (cdr attrs))))
1534 ;; Support some old-style attribute names and values.
1535 (case attribute
1536 (:bold (setq attribute :weight value (if value 'bold 'normal)))
1537 (:italic (setq attribute :slant value (if value 'italic 'normal)))
1538 ((:foreground :background)
1539 ;; Compatibility with 20.x. Some bogus face specs seem to
1540 ;; exist containing things like `:foreground nil'.
1541 (if (null value) (setq value 'unspecified)))
1542 (t (unless (assq attribute face-x-resources)
1543 (setq attribute nil))))
1544 (when attribute
1545 (set-face-attribute face frame attribute value)))
1546 (setq attrs (cdr (cdr attrs))))))
5f5c8ee5
GM
1547
1548(defun face-attr-match-p (face attrs &optional frame)
a9de7d29 1549 "Return t if attributes of FACE match values in plist ATTRS.
5f5c8ee5
GM
1550Optional parameter FRAME is the frame whose definition of FACE
1551is used. If nil or omitted, use the selected frame."
1552 (unless frame
1553 (setq frame (selected-frame)))
1554 (let ((list face-attribute-name-alist)
1555 (match t))
1556 (while (and match (not (null list)))
1557 (let* ((attr (car (car list)))
a9de7d29
MB
1558 (specified-value
1559 (if (plist-member attrs attr)
1560 (plist-get attrs attr)
1561 'unspecified))
5f5c8ee5 1562 (value-now (face-attribute face attr frame)))
a9de7d29 1563 (setq match (equal specified-value value-now))
5f5c8ee5
GM
1564 (setq list (cdr list))))
1565 match))
1566
5f5c8ee5
GM
1567(defun face-spec-match-p (face spec &optional frame)
1568 "Return t if FACE, on FRAME, matches what SPEC says it should look like."
1569 (face-attr-match-p face (face-spec-choose spec frame) frame))
1570
94fe8a31 1571(defsubst face-default-spec (face)
96c2938f
MB
1572 "Return the default face-spec for FACE, ignoring any user customization.
1573If there is no default for FACE, return nil."
1574 (get face 'face-defface-spec))
5f5c8ee5 1575
94fe8a31
MB
1576(defsubst face-user-default-spec (face)
1577 "Return the user's customized face-spec for FACE, or the default if none.
7cd512f2 1578If there is neither a user setting nor a default for FACE, return nil."
e44d251c
JL
1579 (or (get face 'customized-face)
1580 (get face 'saved-face)
94fe8a31
MB
1581 (face-default-spec face)))
1582
5f5c8ee5 1583\f
f795f633
EZ
1584;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1585;;; Frame-type independent color support.
1586;;; We keep the old x-* names as aliases for back-compatibility.
1587;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1588
1589(defun defined-colors (&optional frame)
1590 "Return a list of colors supported for a particular frame.
1591The argument FRAME specifies which frame to try.
1592The value may be different for frames on different display types.
43c58260
RS
1593If FRAME doesn't support colors, the value is nil.
1594If FRAME is nil, that stands for the selected frame."
1a578e9b 1595 (if (memq (framep (or frame (selected-frame))) '(x w32 mac))
f795f633 1596 (xw-defined-colors frame)
37462f4c 1597 (mapcar 'car (tty-color-alist frame))))
f795f633
EZ
1598(defalias 'x-defined-colors 'defined-colors)
1599
aa360da1
GM
1600(declare-function xw-color-defined-p "xfns.c" (color &optional frame))
1601
f795f633
EZ
1602(defun color-defined-p (color &optional frame)
1603 "Return non-nil if color COLOR is supported on frame FRAME.
1604If FRAME is omitted or nil, use the selected frame.
293b4814
EZ
1605If COLOR is the symbol `unspecified' or one of the strings
1606\"unspecified-fg\" or \"unspecified-bg\", the value is nil."
1e221c16 1607 (if (member color '(unspecified "unspecified-bg" "unspecified-fg"))
f795f633 1608 nil
1a578e9b 1609 (if (member (framep (or frame (selected-frame))) '(x w32 mac))
f795f633 1610 (xw-color-defined-p color frame)
37462f4c 1611 (numberp (tty-color-translate color frame)))))
f795f633
EZ
1612(defalias 'x-color-defined-p 'color-defined-p)
1613
aa360da1
GM
1614(declare-function xw-color-values "xfns.c" (color &optional frame))
1615
f795f633
EZ
1616(defun color-values (color &optional frame)
1617 "Return a description of the color named COLOR on frame FRAME.
22bbca61
JB
1618The value is a list of integer RGB values--(RED GREEN BLUE).
1619These values appear to range from 0 to 65280 or 65535, depending
1620on the system; white is \(65280 65280 65280\) or \(65535 65535 65535\).
f795f633
EZ
1621If FRAME is omitted or nil, use the selected frame.
1622If FRAME cannot display COLOR, the value is nil.
293b4814
EZ
1623If COLOR is the symbol `unspecified' or one of the strings
1624\"unspecified-fg\" or \"unspecified-bg\", the value is nil."
1e221c16 1625 (if (member color '(unspecified "unspecified-fg" "unspecified-bg"))
f795f633 1626 nil
1a578e9b 1627 (if (memq (framep (or frame (selected-frame))) '(x w32 mac))
f795f633
EZ
1628 (xw-color-values color frame)
1629 (tty-color-values color frame))))
1630(defalias 'x-color-values 'color-values)
1631
aa360da1
GM
1632(declare-function xw-display-color-p "xfns.c" (&optional terminal))
1633
f795f633
EZ
1634(defun display-color-p (&optional display)
1635 "Return t if DISPLAY supports color.
1636The optional argument DISPLAY specifies which display to ask about.
1637DISPLAY should be either a frame or a display name (a string).
1638If omitted or nil, that stands for the selected frame's display."
1a578e9b 1639 (if (memq (framep-on-display display) '(x w32 mac))
d5179a01
EZ
1640 (xw-display-color-p display)
1641 (tty-display-color-p display)))
f795f633
EZ
1642(defalias 'x-display-color-p 'display-color-p)
1643
aa360da1
GM
1644(declare-function x-display-grayscale-p "xfns.c" (&optional terminal))
1645
d5179a01
EZ
1646(defun display-grayscale-p (&optional display)
1647 "Return non-nil if frames on DISPLAY can display shades of gray."
1648 (let ((frame-type (framep-on-display display)))
1649 (cond
1650 ((memq frame-type '(x w32 mac))
1651 (x-display-grayscale-p display))
1652 (t
1653 (> (tty-color-gray-shades display) 2)))))
1654
26c07a69
RS
1655(defun read-color (&optional prompt convert-to-RGB-p allow-empty-name-p msg-p)
1656 "Read a color name or RGB hex value: #RRRRGGGGBBBB.
1657Completion is available for color names, but not for RGB hex strings.
1658If the user inputs an RGB hex string, it must have the form
1659#XXXXXXXXXXXX or XXXXXXXXXXXX, where each X is a hex digit. The
1660number of Xs must be a multiple of 3, with the same number of Xs for
1661each of red, green, and blue. The order is red, green, blue.
1662
1663In addition to standard color names and RGB hex values, the following
1664are available as color candidates. In each case, the corresponding
1665color is used.
1666
1667 * `foreground at point' - foreground under the cursor
1668 * `background at point' - background under the cursor
1669
1670Checks input to be sure it represents a valid color. If not, raises
1671an error (but see exception for empty input with non-nil
1672ALLOW-EMPTY-NAME-P).
1673
1674Optional arg PROMPT is the prompt; if nil, uses a default prompt.
1675
1676Interactively, or with optional arg CONVERT-TO-RGB-P non-nil, converts
1677an input color name to an RGB hex string. Returns the RGB hex string.
1678
1679Optional arg ALLOW-EMPTY-NAME-P controls what happens if the user
1680enters an empty color name (that is, just hits `RET'). If non-nil,
1681then returns an empty color name, \"\". If nil, then raises an error.
1682Programs must test for \"\" if ALLOW-EMPTY-NAME-P is non-nil. They
1683can then perform an appropriate action in case of empty input.
1684
1685Interactively, or with optional arg MSG-P non-nil, echoes the color in
1686a message."
1687 (interactive "i\np\ni\np") ; Always convert to RGB interactively.
1688 (let* ((completion-ignore-case t)
1689 (colors (append '("foreground at point" "background at point")
1690 (defined-colors)))
1691 (color (completing-read (or prompt "Color (name or #R+G+B+): ")
1692 colors))
1693 hex-string)
1694 (cond ((string= "foreground at point" color)
1695 (setq color (foreground-color-at-point)))
1696 ((string= "background at point" color)
1697 (setq color (background-color-at-point))))
1698 (unless color
1699 (setq color ""))
1700 (setq hex-string
1701 (string-match "^#?\\([a-fA-F0-9][a-fA-F0-9][a-fA-F0-9]\\)+$" color))
1702 (if (and allow-empty-name-p (string= "" color))
1703 ""
1704 (when (and hex-string (not (eq (aref color 0) ?#)))
1705 (setq color (concat "#" color))) ; No #; add it.
1706 (unless hex-string
1707 (when (or (string= "" color) (not (test-completion color colors)))
1708 (error "No such color: %S" color))
1709 (when convert-to-RGB-p
1710 (let ((components (x-color-values color)))
1711 (unless components (error "No such color: %S" color))
1712 (unless (string-match "^#\\([a-fA-F0-9][a-fA-F0-9][a-fA-F0-9]\\)+$" color)
1713 (setq color (format "#%04X%04X%04X"
1714 (logand 65535 (nth 0 components))
1715 (logand 65535 (nth 1 components))
1716 (logand 65535 (nth 2 components))))))))
1717 (when msg-p (message "Color: `%s'" color))
1718 color)))
1719
1720;; Commented out because I decided it is better to include the
1721;; duplicates in read-color's completion list.
1722
1723;; (defun defined-colors-without-duplicates ()
1724;; "Return the list of defined colors, without the no-space versions.
1725;; For each color name, we keep the variant that DOES have spaces."
1726;; (let ((result (copy-sequence (defined-colors)))
1727;; to-be-rejected)
1728;; (save-match-data
1729;; (dolist (this result)
1730;; (if (string-match " " this)
986845af 1731;; (push (replace-regexp-in-string " " ""
26c07a69
RS
1732;; this)
1733;; to-be-rejected)))
1734;; (dolist (elt to-be-rejected)
1735;; (let ((as-found (car (member-ignore-case elt result))))
1736;; (setq result (delete as-found result)))))
1737;; result))
1738
1739(defun face-at-point ()
1740 "Return the face of the character after point.
1741If it has more than one face, return the first one.
1742Return nil if it has no specified face."
1743 (let* ((faceprop (or (get-char-property (point) 'read-face-name)
1744 (get-char-property (point) 'face)
1745 'default))
1746 (face (cond ((symbolp faceprop) faceprop)
1747 ;; List of faces (don't treat an attribute spec).
1748 ;; Just use the first face.
1749 ((and (consp faceprop) (not (keywordp (car faceprop)))
1750 (not (memq (car faceprop)
1751 '(foreground-color background-color))))
1752 (car faceprop))
1753 (t nil)))) ; Invalid face value.
1754 (if (facep face) face nil)))
1755
1756(defun foreground-color-at-point ()
1757 "Return the foreground color of the character after point."
1758 ;; `face-at-point' alone is not sufficient. It only gets named faces.
1759 ;; Need also pick up any face properties that are not associated with named faces.
1760 (let ((face (or (face-at-point)
1761 (get-char-property (point) 'read-face-name)
1762 (get-char-property (point) 'face))))
1763 (cond ((and face (symbolp face))
1764 (let ((value (face-foreground face nil 'default)))
1765 (if (member value '("unspecified-fg" "unspecified-bg"))
1766 nil
1767 value)))
1768 ((consp face)
1769 (cond ((memq 'foreground-color face) (cdr (memq 'foreground-color face)))
1770 ((memq ':foreground face) (cadr (memq ':foreground face)))))
1771 (t nil)))) ; Invalid face value.
1772
1773(defun background-color-at-point ()
1774 "Return the background color of the character after point."
1775 ;; `face-at-point' alone is not sufficient. It only gets named faces.
1776 ;; Need also pick up any face properties that are not associated with named faces.
1777 (let ((face (or (face-at-point)
1778 (get-char-property (point) 'read-face-name)
1779 (get-char-property (point) 'face))))
1780 (cond ((and face (symbolp face))
1781 (let ((value (face-background face nil 'default)))
1782 (if (member value '("unspecified-fg" "unspecified-bg"))
1783 nil
1784 value)))
1785 ((consp face)
1786 (cond ((memq 'background-color face) (cdr (memq 'background-color face)))
1787 ((memq ':background face) (cadr (memq ':background face)))))
1788 (t nil)))) ; Invalid face value.
f795f633 1789\f
5f5c8ee5
GM
1790;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1791;;; Background mode.
1792;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1793
1794(defcustom frame-background-mode nil
1795 "*The brightness of the background.
e44d251c 1796Set this to the symbol `dark' if your background color is dark,
d4c6530f
JL
1797`light' if your background is light, or nil (automatic by default)
1798if you want Emacs to examine the brightness for you. Don't set this
1799variable with `setq'; this won't have the expected effect."
5f5c8ee5
GM
1800 :group 'faces
1801 :set #'(lambda (var value)
c20d5073 1802 (set-default var value)
b5325e78 1803 (mapc 'frame-set-background-mode (frame-list)))
5f5c8ee5 1804 :initialize 'custom-initialize-changed
d4c6530f
JL
1805 :type '(choice (const dark)
1806 (const light)
1807 (const :tag "automatic" nil)))
5f5c8ee5
GM
1808
1809
aa360da1
GM
1810(declare-function x-get-resource "frame.c"
1811 (attribute class &optional component subclass))
1812
5f5c8ee5 1813(defun frame-set-background-mode (frame)
d8abcd91
MB
1814 "Set up display-dependent faces on FRAME.
1815Display-dependent faces are those which have different definitions
1816according to the `background-mode' and `display-type' frame parameters."
5f5c8ee5 1817 (let* ((bg-resource
428a555e 1818 (and (window-system frame)
30aa9e51 1819 (x-get-resource "backgroundMode" "BackgroundMode")))
772139c0 1820 (bg-color (frame-parameter frame 'background-color))
8cbd7bed
KL
1821 (terminal-bg-mode (terminal-parameter frame 'background-mode))
1822 (tty-type (tty-type frame))
f161d539
MB
1823 (bg-mode
1824 (cond (frame-background-mode)
f161d539
MB
1825 (bg-resource
1826 (intern (downcase bg-resource)))
8cbd7bed 1827 (terminal-bg-mode)
2666355c
KL
1828 ((and (null (window-system frame))
1829 ;; Unspecified frame background color can only
1830 ;; happen on tty's.
1831 (member bg-color '(nil unspecified "unspecified-bg")))
1832 ;; There is no way to determine the background mode
1833 ;; automatically, so we make a guess based on the
1834 ;; terminal type.
1835 (if (and tty-type
1836 (string-match "^\\(xterm\\|rxvt\\|dtterm\\|eterm\\)"
1837 tty-type))
1838 'light
1839 'dark))
1e221c16 1840 ((equal bg-color "unspecified-fg") ; inverted colors
2666355c
KL
1841 (if (and tty-type
1842 (string-match "^\\(xterm\\|rxvt\\|dtterm\\|eterm\\)"
1843 tty-type))
1844 'dark
1845 'light))
ff52c940 1846 ((>= (apply '+ (color-values bg-color frame))
f161d539
MB
1847 ;; Just looking at the screen, colors whose
1848 ;; values add up to .6 of the white total
1849 ;; still look dark to me.
ff52c940 1850 (* (apply '+ (color-values "white" frame)) .6))
772139c0
EZ
1851 'light)
1852 (t 'dark)))
f161d539 1853 (display-type
428a555e 1854 (cond ((null (window-system frame))
f161d539 1855 (if (tty-display-color-p frame) 'color 'mono))
ff52c940 1856 ((display-color-p frame)
f161d539
MB
1857 'color)
1858 ((x-display-grayscale-p frame)
1859 'grayscale)
1860 (t 'mono)))
1861 (old-bg-mode
1862 (frame-parameter frame 'background-mode))
1863 (old-display-type
1864 (frame-parameter frame 'display-type)))
1865
1866 (unless (and (eq bg-mode old-bg-mode) (eq display-type old-display-type))
6e424019
MB
1867 (let ((locally-modified-faces nil))
1868 ;; Before modifying the frame parameters, we collect a list of
1869 ;; faces that don't match what their face-spec says they should
24837f13
RS
1870 ;; look like; we then avoid changing these faces below.
1871 ;; These are the faces whose attributes were modified on FRAME.
1872 ;; We use a negative list on the assumption that most faces will
6e424019
MB
1873 ;; be unmodified, so we can avoid consing in the common case.
1874 (dolist (face (face-list))
24837f13
RS
1875 (and (not (get face 'face-override-spec))
1876 (not (face-spec-match-p face
1877 (face-user-default-spec face)
1878 (selected-frame)))
1879 (push face locally-modified-faces)))
6e424019
MB
1880 ;; Now change to the new frame parameters
1881 (modify-frame-parameters frame
1882 (list (cons 'background-mode bg-mode)
1883 (cons 'display-type display-type)))
1884 ;; For all named faces, choose face specs matching the new frame
1885 ;; parameters, unless they have been locally modified.
1886 (dolist (face (face-list))
1887 (unless (memq face locally-modified-faces)
24837f13 1888 (face-spec-recalc face frame)))))))
5f5c8ee5 1889
5f5c8ee5
GM
1890\f
1891;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1892;;; Frame creation.
1893;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1894
aa360da1
GM
1895(declare-function x-parse-geometry "frame.c" (string))
1896
5f5c8ee5
GM
1897(defun x-handle-named-frame-geometry (parameters)
1898 "Add geometry parameters for a named frame to parameter list PARAMETERS.
1899Value is the new parameter list."
1900 (let* ((name (or (cdr (assq 'name parameters))
1901 (cdr (assq 'name default-frame-alist))))
1902 (x-resource-name name)
1903 (res-geometry (if name (x-get-resource "geometry" "Geometry"))))
1904 (when res-geometry
1905 (let ((parsed (x-parse-geometry res-geometry)))
1906 ;; If the resource specifies a position, call the position
1907 ;; and size "user-specified".
1908 (when (or (assq 'top parsed)
1909 (assq 'left parsed))
1910 (setq parsed (append '((user-position . t) (user-size . t)) parsed)))
1911 ;; Put the geometry parameters at the end. Copy
1912 ;; default-frame-alist so that they go after it.
1913 (setq parameters (append parameters default-frame-alist parsed))))
1914 parameters))
1915
1916
1917(defun x-handle-reverse-video (frame parameters)
1918 "Handle the reverse-video frame parameter and X resource.
1919`x-create-frame' does not handle this one."
1920 (when (cdr (or (assq 'reverse parameters)
1921 (assq 'reverse default-frame-alist)
1922 (let ((resource (x-get-resource "reverseVideo"
1923 "ReverseVideo")))
1924 (if resource
1925 (cons nil (member (downcase resource)
1926 '("on" "true")))))))
1927 (let* ((params (frame-parameters frame))
1928 (bg (cdr (assq 'foreground-color params)))
1929 (fg (cdr (assq 'background-color params))))
1930 (modify-frame-parameters frame
1931 (list (cons 'foreground-color fg)
1932 (cons 'background-color bg)))
1933 (if (equal bg (cdr (assq 'border-color params)))
1934 (modify-frame-parameters frame
1935 (list (cons 'border-color fg))))
1936 (if (equal bg (cdr (assq 'mouse-color params)))
1937 (modify-frame-parameters frame
1938 (list (cons 'mouse-color fg))))
1939 (if (equal bg (cdr (assq 'cursor-color params)))
1940 (modify-frame-parameters frame
1941 (list (cons 'cursor-color fg)))))))
1942
aa360da1
GM
1943(declare-function x-create-frame "xfns.c" (parms))
1944(declare-function x-setup-function-keys "term/x-win" (frame))
1945(declare-function tool-bar-setup "tool-bar" (&optional frame))
5f5c8ee5
GM
1946
1947(defun x-create-frame-with-faces (&optional parameters)
1948 "Create a frame from optional frame parameters PARAMETERS.
1949Parameters not specified by PARAMETERS are taken from
1950`default-frame-alist'. If PARAMETERS specify a frame name,
1951handle X geometry resources for that name. If either PARAMETERS
1952or `default-frame-alist' contains a `reverse' parameter, or
1953the X resource ``reverseVideo'' is present, handle that.
1954Value is the new frame created."
1955 (setq parameters (x-handle-named-frame-geometry parameters))
1956 (let ((visibility-spec (assq 'visibility parameters))
7f19e125 1957 (frame (x-create-frame `((visibility . nil) . ,parameters)))
5f5c8ee5
GM
1958 success)
1959 (unwind-protect
1960 (progn
2bb819d5 1961 (x-setup-function-keys frame)
5f5c8ee5
GM
1962 (x-handle-reverse-video frame parameters)
1963 (frame-set-background-mode frame)
448bcb40 1964 (face-set-after-frame-default frame)
095fe281
KL
1965 ;; Make sure the tool-bar is ready to be enabled. The
1966 ;; `tool-bar-lines' frame parameter will not take effect
1967 ;; without this call.
1968 (tool-bar-setup frame)
1969 (if (null visibility-spec)
5f5c8ee5
GM
1970 (make-frame-visible frame)
1971 (modify-frame-parameters frame (list visibility-spec)))
1972 (setq success t))
1973 (unless success
1974 (delete-frame frame)))
1975 frame))
1976
5f5c8ee5 1977(defun face-set-after-frame-default (frame)
f1cae29e
GM
1978 "Set frame-local faces of FRAME from face specs and resources.
1979Initialize colors of certain faces from frame parameters."
1df64254
RS
1980 (unless inhibit-face-set-after-frame-default
1981 (if (face-attribute 'default :font t)
1982 (set-face-attribute 'default frame :font
1983 (face-attribute 'default :font t))
1984 (set-face-attribute 'default frame :family
1985 (face-attribute 'default :family t))
1986 (set-face-attribute 'default frame :height
1987 (face-attribute 'default :height t))
1988 (set-face-attribute 'default frame :slant
1989 (face-attribute 'default :slant t))
1990 (set-face-attribute 'default frame :weight
1991 (face-attribute 'default :weight t))
1992 (set-face-attribute 'default frame :width
1993 (face-attribute 'default :width t))))
8db9c5ee
CY
1994 ;; Find attributes that should be initialized from frame parameters.
1995 (let ((face-params '((foreground-color default :foreground)
1996 (background-color default :background)
1997 (border-color border :background)
1998 (cursor-color cursor :background)
1999 (scroll-bar-foreground scroll-bar :foreground)
2000 (scroll-bar-background scroll-bar :background)
2001 (mouse-color mouse :background)))
2002 apply-params)
2003 (dolist (param face-params)
2004 (let* ((value (frame-parameter frame (nth 0 param)))
2005 (face (nth 1 param))
2006 (attr (nth 2 param))
2007 (default-value (face-attribute face attr t)))
2008 ;; Compile a list of face attributes to set, but don't set
2009 ;; them yet. The call to make-face-x-resource-internal,
2010 ;; below, can change frame parameters, and the final set of
2011 ;; frame parameters should be the ones acquired at this step.
2012 (if (eq default-value 'unspecified)
2013 ;; The face spec does not specify a new-frame value for
2014 ;; this attribute. Check if the existing frame parameter
2015 ;; specifies it.
2016 (if value
2017 (push (list face frame attr value) apply-params))
2018 ;; The face spec specifies a value for this attribute, to be
2019 ;; applied to the face on all new frames.
2020 (push (list face frame attr default-value) apply-params))))
2021 ;; Initialize faces from face specs and X resources. The
2022 ;; condition-case prevents invalid specs from causing frame
2023 ;; creation to fail.
2024 (dolist (face (delq 'default (face-list)))
2025 (condition-case ()
2026 (progn
24837f13 2027 (face-spec-recalc face frame)
f65f7603 2028 (if (memq (window-system frame) '(x w32 mac))
87a0fbbe 2029 (make-face-x-resource-internal face frame))
6aa2269d 2030 (internal-merge-in-global-face face frame))
8db9c5ee
CY
2031 (error nil)))
2032 ;; Apply the attributes specified by frame parameters. This
2033 ;; rewrites parameters changed by make-face-x-resource-internal
2034 (dolist (param apply-params)
2035 (apply 'set-face-attribute param))))
5f5c8ee5 2036
53990c84
EZ
2037(defun tty-handle-reverse-video (frame parameters)
2038 "Handle the reverse-video frame parameter for terminal frames."
2039 (when (cdr (or (assq 'reverse parameters)
2040 (assq 'reverse default-frame-alist)))
53990c84
EZ
2041 (let* ((params (frame-parameters frame))
2042 (bg (cdr (assq 'foreground-color params)))
2043 (fg (cdr (assq 'background-color params))))
2044 (modify-frame-parameters frame
2045 (list (cons 'foreground-color fg)
2046 (cons 'background-color bg)))
2047 (if (equal bg (cdr (assq 'mouse-color params)))
2048 (modify-frame-parameters frame
2049 (list (cons 'mouse-color fg))))
2050 (if (equal bg (cdr (assq 'cursor-color params)))
2051 (modify-frame-parameters frame
2052 (list (cons 'cursor-color fg)))))))
2053
5f5c8ee5
GM
2054
2055(defun tty-create-frame-with-faces (&optional parameters)
2056 "Create a frame from optional frame parameters PARAMETERS.
2057Parameters not specified by PARAMETERS are taken from
2058`default-frame-alist'. If either PARAMETERS or `default-frame-alist'
2059contains a `reverse' parameter, handle that. Value is the new frame
2060created."
2061 (let ((frame (make-terminal-frame parameters))
2062 success)
2063 (unwind-protect
061e4e7f 2064 (with-selected-frame frame
53990c84 2065 (tty-handle-reverse-video frame (frame-parameters frame))
cd85984a 2066
8782c744
SM
2067 (unless (terminal-parameter frame 'terminal-initted)
2068 (set-terminal-parameter frame 'terminal-initted t)
2069 (set-locale-environment nil frame)
2070 (tty-run-terminal-initialization frame))
5f5c8ee5
GM
2071 (frame-set-background-mode frame)
2072 (face-set-after-frame-default frame)
2073 (setq success t))
2074 (unless success
2075 (delete-frame frame)))
2076 frame))
465fceed 2077
b34c34da
KL
2078(defun tty-find-type (pred type)
2079 "Return the longest prefix of TYPE to which PRED returns non-nil.
2080TYPE should be a tty type name such as \"xterm-16color\".
2081
2082The function tries only those prefixes that are followed by a
2083dash or underscore in the original type name, like \"xterm\" in
2084the above example."
2085 (let (hyphend)
2086 (while (and type
2087 (not (funcall pred type)))
2088 ;; Strip off last hyphen and what follows, then try again
2089 (setq type
2090 (if (setq hyphend (string-match "[-_][^-_]+$" type))
2091 (substring type 0 hyphend)
2092 nil))))
2093 type)
2094
8cbd7bed
KL
2095(defun tty-run-terminal-initialization (frame &optional type)
2096 "Run the special initialization code for the terminal type of FRAME.
2097The optional TYPE parameter may be used to override the autodetected
2098terminal type to a different value."
2099 (setq type (or type (tty-type frame)))
cd85984a
KL
2100 ;; Load library for our terminal type.
2101 ;; User init file can set term-file-prefix to nil to prevent this.
2102 (with-selected-frame frame
8782c744 2103 (unless (null term-file-prefix)
b34c34da
KL
2104 (let* (term-init-func)
2105 ;; First, load the terminal initialization file, if it is
2106 ;; available and it hasn't been loaded already.
2107 (tty-find-type #'(lambda (type)
2108 (let ((file (locate-library (concat term-file-prefix type))))
2109 (and file
2110 (or (assoc file load-history)
2111 (load file t t)))))
8cbd7bed 2112 type)
b34c34da
KL
2113 ;; Next, try to find a matching initialization function, and call it.
2114 (tty-find-type #'(lambda (type)
2115 (fboundp (setq term-init-func
2116 (intern (concat "terminal-init-" type)))))
8cbd7bed 2117 type)
567c8878 2118 (when (fboundp term-init-func)
b34c34da
KL
2119 (funcall term-init-func))
2120 (set-terminal-parameter frame 'terminal-initted term-init-func)))))
465fceed 2121
5f5c8ee5
GM
2122;; Called from C function init_display to initialize faces of the
2123;; dumped terminal frame on startup.
465fceed 2124
5f5c8ee5
GM
2125(defun tty-set-up-initial-frame-faces ()
2126 (let ((frame (selected-frame)))
2127 (frame-set-background-mode frame)
2128 (face-set-after-frame-default frame)))
00f51890 2129
465fceed 2130
465fceed 2131
5f5c8ee5
GM
2132\f
2133;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2134;;; Compatiblity with 20.2
2135;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
72418504 2136
5f5c8ee5 2137;; Update a frame's faces when we change its default font.
465fceed 2138
e16e7eac 2139(defalias 'frame-update-faces 'ignore "")
4b22f844 2140(make-obsolete 'frame-update-faces "no longer necessary." "21.1")
e8e4cda0 2141
5f5c8ee5
GM
2142;; Update the colors of FACE, after FRAME's own colors have been
2143;; changed.
1bcb155c 2144
522fb950
JB
2145(define-obsolete-function-alias 'frame-update-face-colors
2146 'frame-set-background-mode "21.1")
d11fba25 2147
bdda3754 2148\f
5f5c8ee5
GM
2149;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2150;;; Standard faces.
2151;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
465fceed 2152
5f5c8ee5
GM
2153(defgroup basic-faces nil
2154 "The standard faces of Emacs."
2155 :group 'faces)
ef436392 2156
bacbbe8f
CY
2157(defface default
2158 '((t nil)) ; If this were nil, face-defface-spec would not be set.
5f5c8ee5
GM
2159 "Basic default face."
2160 :group 'basic-faces)
465fceed 2161
51d44a36
JL
2162(defface bold
2163 '((t :weight bold))
2164 "Basic bold face."
2165 :group 'basic-faces)
2166
2167(defface italic
2168 '((((supports :slant italic))
2169 :slant italic)
2170 (((supports :underline t))
2171 :underline t)
2172 (t
2173 ;; default to italic, even it doesn't appear to be supported,
2174 ;; because in some cases the display engine will do it's own
2175 ;; workaround (to `dim' on ttys)
2176 :slant italic))
2177 "Basic italic face."
2178 :group 'basic-faces)
2179
2180(defface bold-italic
2181 '((t :weight bold :slant italic))
2182 "Basic bold-italic face."
2183 :group 'basic-faces)
2184
2185(defface underline
2186 '((((supports :underline t))
2187 :underline t)
2188 (((supports :weight bold))
2189 :weight bold)
2190 (t :underline t))
2191 "Basic underlined face."
2192 :group 'basic-faces)
2193
2194(defface fixed-pitch
2195 '((t :family "courier"))
2196 "The basic fixed-pitch face."
2197 :group 'basic-faces)
2198
2199(defface variable-pitch
2200 '((t :family "helv"))
2201 "The basic variable-pitch face."
2202 :group 'basic-faces)
2203
2204(defface shadow
2205 '((((class color grayscale) (min-colors 88) (background light))
2206 :foreground "grey50")
2207 (((class color grayscale) (min-colors 88) (background dark))
2208 :foreground "grey70")
2209 (((class color) (min-colors 8) (background light))
2210 :foreground "green")
2211 (((class color) (min-colors 8) (background dark))
2212 :foreground "yellow"))
2213 "Basic face for shadowed text."
2214 :group 'basic-faces
2215 :version "22.1")
2216
59746720
JL
2217(defface link
2218 '((((class color) (min-colors 88) (background light))
2219 :foreground "blue1" :underline t)
2220 (((class color) (background light))
2221 :foreground "blue" :underline t)
2222 (((class color) (min-colors 88) (background dark))
2223 :foreground "cyan1" :underline t)
2224 (((class color) (background dark))
2225 :foreground "cyan" :underline t)
2226 (t :inherit underline))
2227 "Basic face for unvisited links."
2228 :group 'basic-faces
2229 :version "22.1")
2230
2231(defface link-visited
2232 '((default :inherit link)
2233 (((class color) (background light)) :foreground "magenta4")
2234 (((class color) (background dark)) :foreground "violet"))
2235 "Basic face for visited links."
2236 :group 'basic-faces
2237 :version "22.1")
2238
51d44a36
JL
2239(defface highlight
2240 '((((class color) (min-colors 88) (background light))
2241 :background "darkseagreen2")
2242 (((class color) (min-colors 88) (background dark))
2243 :background "darkolivegreen")
2244 (((class color) (min-colors 16) (background light))
2245 :background "darkseagreen2")
2246 (((class color) (min-colors 16) (background dark))
2247 :background "darkolivegreen")
2248 (((class color) (min-colors 8))
2249 :background "green" :foreground "black")
2250 (t :inverse-video t))
2251 "Basic face for highlighting."
2252 :group 'basic-faces)
2253
51d44a36
JL
2254(defface region
2255 '((((class color) (min-colors 88) (background dark))
2256 :background "blue3")
2257 (((class color) (min-colors 88) (background light))
2258 :background "lightgoldenrod2")
2259 (((class color) (min-colors 16) (background dark))
2260 :background "blue3")
2261 (((class color) (min-colors 16) (background light))
2262 :background "lightgoldenrod2")
2263 (((class color) (min-colors 8))
2264 :background "blue" :foreground "white")
2265 (((type tty) (class mono))
2266 :inverse-video t)
2267 (t :background "gray"))
2268 "Basic face for highlighting the region."
2269 :version "21.1"
2270 :group 'basic-faces)
2271
2272(defface secondary-selection
2273 '((((class color) (min-colors 88) (background light))
2274 :background "yellow1")
2275 (((class color) (min-colors 88) (background dark))
2276 :background "SkyBlue4")
2277 (((class color) (min-colors 16) (background light))
2278 :background "yellow")
2279 (((class color) (min-colors 16) (background dark))
2280 :background "SkyBlue4")
2281 (((class color) (min-colors 8))
2282 :background "cyan" :foreground "black")
2283 (t :inverse-video t))
2284 "Basic face for displaying the secondary selection."
2285 :group 'basic-faces)
2286
2287(defface trailing-whitespace
2288 '((((class color) (background light))
2289 :background "red1")
2290 (((class color) (background dark))
2291 :background "red1")
2292 (t :inverse-video t))
2293 "Basic face for highlighting trailing whitespace."
2294 :version "21.1"
d4c6530f 2295 :group 'whitespace-faces ; like `show-trailing-whitespace'
51d44a36
JL
2296 :group 'basic-faces)
2297
2298(defface escape-glyph
2299 '((((background dark)) :foreground "cyan")
2300 ;; See the comment in minibuffer-prompt for
2301 ;; the reason not to use blue on MS-DOS.
2302 (((type pc)) :foreground "magenta")
2303 ;; red4 is too dark, but some say blue is too loud.
2304 ;; brown seems to work ok. -- rms.
2305 (t :foreground "brown"))
1a77c985 2306 "Face for characters displayed as sequences using `^' or `\\'."
51d44a36
JL
2307 :group 'basic-faces
2308 :version "22.1")
2309
2310(defface nobreak-space
2311 '((((class color) (min-colors 88)) :inherit escape-glyph :underline t)
2312 (((class color) (min-colors 8)) :background "magenta")
2313 (t :inverse-video t))
2314 "Face for displaying nobreak space."
2315 :group 'basic-faces
2316 :version "22.1")
72418504 2317
97bca259
JL
2318(defgroup mode-line-faces nil
2319 "Faces used in the mode line."
6d968826 2320 :group 'mode-line
97bca259
JL
2321 :group 'faces
2322 :version "22.1")
2323
3759f14b 2324(defface mode-line
11c2175c 2325 '((((class color) (min-colors 88))
b6b90dfc
SM
2326 :box (:line-width -1 :style released-button)
2327 :background "grey75" :foreground "black")
5f5c8ee5 2328 (t
b6b90dfc 2329 :inverse-video t))
c5752dcb 2330 "Basic mode line face for selected window."
a4391cf1 2331 :version "21.1"
97bca259 2332 :group 'mode-line-faces
5f5c8ee5 2333 :group 'basic-faces)
465fceed 2334
c5752dcb 2335(defface mode-line-inactive
cf459bb7 2336 '((default
b6b90dfc 2337 :inherit mode-line)
11c2175c 2338 (((class color) (min-colors 88) (background light))
c5752dcb
KS
2339 :weight light
2340 :box (:line-width -1 :color "grey75" :style nil)
2341 :foreground "grey20" :background "grey90")
11c2175c 2342 (((class color) (min-colors 88) (background dark) )
53a2f6e1
MB
2343 :weight light
2344 :box (:line-width -1 :color "grey40" :style nil)
b6b90dfc 2345 :foreground "grey80" :background "grey30"))
c5752dcb 2346 "Basic mode line face for non-selected windows."
bf247b6e 2347 :version "22.1"
97bca259
JL
2348 :group 'mode-line-faces
2349 :group 'basic-faces)
2350
2351(defface mode-line-highlight
2352 '((((class color) (min-colors 88))
2353 :box (:line-width 2 :color "grey40" :style released-button))
2354 (t
2355 :inherit highlight))
2356 "Basic mode line face for highlighting."
2357 :version "22.1"
2358 :group 'mode-line-faces
2359 :group 'basic-faces)
2360
313e2fc4
GM
2361(defface mode-line-emphasis
2362 '((t (:weight bold)))
2363 "Face used to emphasize certain mode line features.
2364Use the face `mode-line-highlight' for features that can be selected."
2365 :version "23.1"
2366 :group 'mode-line-faces
2367 :group 'basic-faces)
2368
97bca259
JL
2369(defface mode-line-buffer-id
2370 '((t (:weight bold)))
2371 "Face used for buffer identification parts of the mode line."
2372 :version "22.1"
2373 :group 'mode-line-faces
c5752dcb
KS
2374 :group 'basic-faces)
2375
3759f14b
GM
2376;; Make `modeline' an alias for `mode-line', for compatibility.
2377(put 'modeline 'face-alias 'mode-line)
c5752dcb 2378(put 'modeline-inactive 'face-alias 'mode-line-inactive)
e0dcaa3f 2379(put 'modeline-highlight 'face-alias 'mode-line-highlight)
97bca259 2380(put 'modeline-buffer-id 'face-alias 'mode-line-buffer-id)
d11fba25 2381
e5e7779f 2382(defface header-line
cf459bb7 2383 '((default
6b61353c
KH
2384 :inherit mode-line)
2385 (((type tty))
1cb4393e
MB
2386 ;; This used to be `:inverse-video t', but that doesn't look very
2387 ;; good when combined with inverse-video mode-lines and multiple
2388 ;; windows. Underlining looks better, and is more consistent with
2389 ;; the window-system face variants, which deemphasize the
2390 ;; header-line in relation to the mode-line face. If a terminal
2391 ;; can't underline, then the header-line will end up without any
2392 ;; highlighting; this may be too confusing in general, although it
2393 ;; happens to look good with the only current use of header-lines,
2394 ;; the info browser. XXX
6b61353c 2395 :inverse-video nil ;Override the value inherited from mode-line.
a7ac92b6 2396 :underline t)
98490598 2397 (((class color grayscale) (background light))
bdab1d43 2398 :background "grey90" :foreground "grey20"
6b61353c 2399 :box nil)
98490598 2400 (((class color grayscale) (background dark))
bdab1d43 2401 :background "grey20" :foreground "grey90"
6b61353c 2402 :box nil)
98490598 2403 (((class mono) (background light))
bdab1d43
MB
2404 :background "white" :foreground "black"
2405 :inverse-video nil
2406 :box nil
6b61353c 2407 :underline t)
98490598 2408 (((class mono) (background dark))
bdab1d43
MB
2409 :background "black" :foreground "white"
2410 :inverse-video nil
2411 :box nil
6b61353c 2412 :underline t))
e5e7779f 2413 "Basic header-line face."
a4391cf1 2414 :version "21.1"
5f5c8ee5 2415 :group 'basic-faces)
e8e4cda0 2416
51d44a36
JL
2417(defface vertical-border
2418 '((((type tty)) :inherit mode-line-inactive))
2419 "Face used for vertical window dividers on ttys."
2420 :version "22.1"
5f5c8ee5 2421 :group 'basic-faces)
d11fba25 2422
9ed779e8
JL
2423(defface minibuffer-prompt
2424 '((((background dark)) :foreground "cyan")
2425 ;; Don't use blue because many users of the MS-DOS port customize
2426 ;; their foreground color to be blue.
2427 (((type pc)) :foreground "magenta")
c57a1dc6 2428 (t :foreground "medium blue"))
9ed779e8
JL
2429 "Face for minibuffer prompts.
2430By default, Emacs automatically adds this face to the value of
2431`minibuffer-prompt-properties', which is a list of text properties
2432used to display the prompt text."
bf247b6e 2433 :version "22.1"
31cf46d3
RS
2434 :group 'basic-faces)
2435
2436(setq minibuffer-prompt-properties
2437 (append minibuffer-prompt-properties (list 'face 'minibuffer-prompt)))
2438
f1cae29e 2439(defface fringe
d6d59057 2440 '((((class color) (background light))
b6b90dfc
SM
2441 :background "grey95")
2442 (((class color) (background dark))
2443 :background "grey10")
2444 (t
2445 :background "gray"))
f1cae29e
GM
2446 "Basic face for the fringes to the left and right of windows under X."
2447 :version "21.1"
84fe35f0 2448 :group 'frames
f1cae29e
GM
2449 :group 'basic-faces)
2450
d390b4d2 2451(defface scroll-bar '((t nil))
f1cae29e
GM
2452 "Basic face for the scroll bar colors under X."
2453 :version "21.1"
84fe35f0 2454 :group 'frames
f1cae29e
GM
2455 :group 'basic-faces)
2456
d390b4d2 2457(defface border '((t nil))
f1cae29e
GM
2458 "Basic face for the frame border under X."
2459 :version "21.1"
84fe35f0 2460 :group 'frames
f1cae29e
GM
2461 :group 'basic-faces)
2462
d390b4d2 2463(defface cursor '((t nil))
8d413c2b
KS
2464 "Basic face for the cursor color under X.
2465Note: Other faces cannot inherit from the cursor face."
f1cae29e 2466 :version "21.1"
84fe35f0 2467 :group 'cursor
f1cae29e
GM
2468 :group 'basic-faces)
2469
8d413c2b 2470(put 'cursor 'face-no-inherit t)
f1cae29e 2471
d390b4d2 2472(defface mouse '((t nil))
f1cae29e 2473 "Basic face for the mouse color under X."
e59176e2 2474 :version "21.1"
84fe35f0 2475 :group 'mouse
5f5c8ee5 2476 :group 'basic-faces)
bdda3754 2477
51d44a36
JL
2478(defface tool-bar
2479 '((default
2480 :box (:line-width 1 :style released-button)
2481 :foreground "black")
2482 (((type x w32 mac) (class color))
2483 :background "grey75")
2484 (((type x) (class mono))
2485 :background "grey"))
2486 "Basic tool-bar face."
2487 :version "21.1"
5f5c8ee5 2488 :group 'basic-faces)
bdda3754 2489
51d44a36
JL
2490(defface menu
2491 '((((type tty))
2492 :inverse-video t)
2493 (((type x-toolkit))
2494 )
16320ac7 2495 (t
51d44a36
JL
2496 :inverse-video t))
2497 "Basic face for the font and colors of the menu bar and popup menus."
a4391cf1 2498 :version "21.1"
51d44a36 2499 :group 'menu
a4391cf1 2500 :group 'basic-faces)
465fceed 2501
465fceed 2502\f
5f5c8ee5
GM
2503;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2504;;; Manipulating font names.
2505;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2506
2507;; This is here for compatibilty with Emacs 20.2. For example,
07f27947
DL
2508;; international/fontset.el uses x-resolve-font-name. The following
2509;; functions are not used in the face implementation itself.
465fceed 2510
d7fa5aa2
RS
2511(defvar x-font-regexp nil)
2512(defvar x-font-regexp-head nil)
2513(defvar x-font-regexp-weight nil)
2514(defvar x-font-regexp-slant nil)
465fceed 2515
021ca129
KH
2516(defconst x-font-regexp-weight-subnum 1)
2517(defconst x-font-regexp-slant-subnum 2)
2518(defconst x-font-regexp-swidth-subnum 3)
2519(defconst x-font-regexp-adstyle-subnum 4)
2520
465fceed
ER
2521;;; Regexps matching font names in "Host Portable Character Representation."
2522;;;
2523(let ((- "[-?]")
2524 (foundry "[^-]+")
2525 (family "[^-]+")
2526 (weight "\\(bold\\|demibold\\|medium\\)") ; 1
2527; (weight\? "\\(\\*\\|bold\\|demibold\\|medium\\|\\)") ; 1
2528 (weight\? "\\([^-]*\\)") ; 1
2529 (slant "\\([ior]\\)") ; 2
2530; (slant\? "\\([ior?*]?\\)") ; 2
2531 (slant\? "\\([^-]?\\)") ; 2
2532; (swidth "\\(\\*\\|normal\\|semicondensed\\|\\)") ; 3
2533 (swidth "\\([^-]*\\)") ; 3
2534; (adstyle "\\(\\*\\|sans\\|\\)") ; 4
c8787b81 2535 (adstyle "\\([^-]*\\)") ; 4
465fceed
ER
2536 (pixelsize "[0-9]+")
2537 (pointsize "[0-9][0-9]+")
2538 (resx "[0-9][0-9]+")
2539 (resy "[0-9][0-9]+")
2540 (spacing "[cmp?*]")
2541 (avgwidth "[0-9]+")
2542 (registry "[^-]+")
2543 (encoding "[^-]+")
2544 )
2545 (setq x-font-regexp
2546 (concat "\\`\\*?[-?*]"
2547 foundry - family - weight\? - slant\? - swidth - adstyle -
e20d641f
RS
2548 pixelsize - pointsize - resx - resy - spacing - avgwidth -
2549 registry - encoding "\\*?\\'"
465fceed
ER
2550 ))
2551 (setq x-font-regexp-head
2552 (concat "\\`[-?*]" foundry - family - weight\? - slant\?
2553 "\\([-*?]\\|\\'\\)"))
2554 (setq x-font-regexp-slant (concat - slant -))
2555 (setq x-font-regexp-weight (concat - weight -))
70d0c3b0 2556 nil)
465fceed 2557
5f5c8ee5 2558
281dc1c2
JB
2559(defun x-resolve-font-name (pattern &optional face frame)
2560 "Return a font name matching PATTERN.
ac4c2e6b 2561All wildcards in PATTERN are instantiated.
10d89673
JB
2562If PATTERN is nil, return the name of the frame's base font, which never
2563contains wildcards.
e17dbede
RS
2564Given optional arguments FACE and FRAME, return a font which is
2565also the same size as FACE on FRAME, or fail."
14e6867c
RS
2566 (or (symbolp face)
2567 (setq face (face-name face)))
2568 (and (eq frame t)
2569 (setq frame nil))
10d89673 2570 (if pattern
8db93e45 2571 ;; Note that x-list-fonts has code to handle a face with nil as its font.
abd89b80 2572 (let ((fonts (x-list-fonts pattern face frame 1)))
10d89673
JB
2573 (or fonts
2574 (if face
962a60aa
RS
2575 (if (string-match "\\*" pattern)
2576 (if (null (face-font face))
2577 (error "No matching fonts are the same height as the frame default font")
2578 (error "No matching fonts are the same height as face `%s'" face))
2579 (if (null (face-font face))
2580 (error "Height of font `%s' doesn't match the frame default font"
2581 pattern)
2582 (error "Height of font `%s' doesn't match face `%s'"
2583 pattern face)))
7cf6fac1 2584 (error "No fonts match `%s'" pattern)))
10d89673
JB
2585 (car fonts))
2586 (cdr (assq 'font (frame-parameters (selected-frame))))))
281dc1c2 2587
5f5c8ee5 2588
465fceed 2589(defun x-frob-font-weight (font which)
b7ffee5f
SM
2590 (let ((case-fold-search t))
2591 (cond ((string-match x-font-regexp font)
2592 (concat (substring font 0
2593 (match-beginning x-font-regexp-weight-subnum))
2594 which
2595 (substring font (match-end x-font-regexp-weight-subnum)
2596 (match-beginning x-font-regexp-adstyle-subnum))
2597 ;; Replace the ADD_STYLE_NAME field with *
2598 ;; because the info in it may not be the same
2599 ;; for related fonts.
2600 "*"
2601 (substring font (match-end x-font-regexp-adstyle-subnum))))
528fbf51
RS
2602 ((string-match x-font-regexp-head font)
2603 (concat (substring font 0 (match-beginning 1)) which
2604 (substring font (match-end 1))))
2605 ((string-match x-font-regexp-weight font)
b7ffee5f
SM
2606 (concat (substring font 0 (match-beginning 1)) which
2607 (substring font (match-end 1)))))))
2598a293 2608(make-obsolete 'x-frob-font-weight 'make-face-... "21.1")
5f5c8ee5 2609
465fceed 2610(defun x-frob-font-slant (font which)
b7ffee5f
SM
2611 (let ((case-fold-search t))
2612 (cond ((string-match x-font-regexp font)
2613 (concat (substring font 0
2614 (match-beginning x-font-regexp-slant-subnum))
2615 which
2616 (substring font (match-end x-font-regexp-slant-subnum)
2617 (match-beginning x-font-regexp-adstyle-subnum))
2618 ;; Replace the ADD_STYLE_NAME field with *
2619 ;; because the info in it may not be the same
2620 ;; for related fonts.
2621 "*"
2622 (substring font (match-end x-font-regexp-adstyle-subnum))))
528fbf51
RS
2623 ((string-match x-font-regexp-head font)
2624 (concat (substring font 0 (match-beginning 2)) which
2625 (substring font (match-end 2))))
2626 ((string-match x-font-regexp-slant font)
b7ffee5f
SM
2627 (concat (substring font 0 (match-beginning 1)) which
2628 (substring font (match-end 1)))))))
2598a293 2629(make-obsolete 'x-frob-font-slant 'make-face-... "21.1")
5f5c8ee5 2630
3fc67165
EZ
2631;; These aliases are here so that we don't get warnings about obsolete
2632;; functions from the byte compiler.
2633(defalias 'internal-frob-font-weight 'x-frob-font-weight)
2634(defalias 'internal-frob-font-slant 'x-frob-font-slant)
2635
465fceed 2636(defun x-make-font-bold (font)
f3f31ccf
RS
2637 "Given an X font specification, make a bold version of it.
2638If that can't be done, return nil."
3fc67165 2639 (internal-frob-font-weight font "bold"))
2598a293 2640(make-obsolete 'x-make-font-bold 'make-face-bold "21.1")
5f5c8ee5 2641
465fceed 2642(defun x-make-font-demibold (font)
f3f31ccf
RS
2643 "Given an X font specification, make a demibold version of it.
2644If that can't be done, return nil."
3fc67165 2645 (internal-frob-font-weight font "demibold"))
2598a293 2646(make-obsolete 'x-make-font-demibold 'make-face-bold "21.1")
5f5c8ee5 2647
465fceed 2648(defun x-make-font-unbold (font)
f3f31ccf
RS
2649 "Given an X font specification, make a non-bold version of it.
2650If that can't be done, return nil."
3fc67165 2651 (internal-frob-font-weight font "medium"))
2598a293 2652(make-obsolete 'x-make-font-unbold 'make-face-unbold "21.1")
5f5c8ee5 2653
465fceed 2654(defun x-make-font-italic (font)
f3f31ccf
RS
2655 "Given an X font specification, make an italic version of it.
2656If that can't be done, return nil."
3fc67165 2657 (internal-frob-font-slant font "i"))
2598a293 2658(make-obsolete 'x-make-font-italic 'make-face-italic "21.1")
5f5c8ee5 2659
465fceed 2660(defun x-make-font-oblique (font) ; you say tomayto...
f3f31ccf
RS
2661 "Given an X font specification, make an oblique version of it.
2662If that can't be done, return nil."
3fc67165 2663 (internal-frob-font-slant font "o"))
2598a293 2664(make-obsolete 'x-make-font-oblique 'make-face-italic "21.1")
5f5c8ee5 2665
465fceed 2666(defun x-make-font-unitalic (font)
f3f31ccf
RS
2667 "Given an X font specification, make a non-italic version of it.
2668If that can't be done, return nil."
3fc67165 2669 (internal-frob-font-slant font "r"))
2598a293 2670(make-obsolete 'x-make-font-unitalic 'make-face-unitalic "21.1")
5f5c8ee5 2671
b7d7285c
KH
2672(defun x-make-font-bold-italic (font)
2673 "Given an X font specification, make a bold and italic version of it.
2674If that can't be done, return nil."
3fc67165
EZ
2675 (and (setq font (internal-frob-font-weight font "bold"))
2676 (internal-frob-font-slant font "i")))
2598a293 2677(make-obsolete 'x-make-font-bold-italic 'make-face-bold-italic "21.1")
113ab303 2678
f0138172
JB
2679(provide 'faces)
2680
e3b5b35b 2681;; arch-tag: 19a4759f-2963-445f-b004-425b9aadd7d6
70d0c3b0 2682;;; faces.el ends here