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