(x_delete_display): Don't call XrmDestroyDatabase here.
[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,
ae940284 4;; 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 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)
9201cc28 52 "A list specifying how face font selection chooses fonts.
5f5c8ee5
GM
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 72 '(("Monospace" "courier" "fixed")
f37a389e 73 ("courier" "CMU Typewriter Text" "fixed")
ed008205 74 ("Sans Serif" "helv" "helvetica" "arial" "fixed")
3855860a 75 ("helv" "helvetica" "arial" "fixed"))
9201cc28 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")))
9201cc28 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")))
9201cc28 306 "List of X resources and classes for face attributes.
5f5c8ee5
GM
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."
e51fe00a 341 (when (memq (framep frame) '(x w32))
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)
95ac8fbd 991 (mapcar (lambda (x) (cons 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
0db2e6af 1023 (and (memq (window-system frame) '(x ns)) ; No stipple on w32
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)))
0d3b1f4e
CY
1360 ;; We may get called for anonymous faces (i.e., faces
1361 ;; expressed using prop-value plists). Those can't be
1362 ;; usefully customized, so ignore them.
1363 (when (symbolp f)
1364 (insert "Face: " (symbol-name f))
1365 (if (not (facep f))
1366 (insert " undefined face.\n")
1367 (let ((customize-label "customize this face")
1368 file-name)
1369 (insert (concat " (" (propertize "sample" 'font-lock-face f) ")"))
1370 (princ (concat " (" customize-label ")\n"))
1371 (insert "Documentation: "
1372 (or (face-documentation f)
1373 "Not documented as a face.")
1374 "\n")
1375 (with-current-buffer standard-output
1376 (save-excursion
1377 (re-search-backward
1378 (concat "\\(" customize-label "\\)") nil t)
1379 (help-xref-button 1 'help-customize-face f)))
1380 (setq file-name (find-lisp-object-file-name f 'defface))
1381 (when file-name
1382 (princ "Defined in `")
1383 (princ (file-name-nondirectory file-name))
1384 (princ "'")
1385 ;; Make a hyperlink to the library.
1386 (save-excursion
1387 (re-search-backward "`\\([^`']+\\)'" nil t)
1388 (help-xref-button 1 'help-face-def f file-name))
1389 (princ ".")
1390 (terpri)
1391 (terpri))
1392 (dolist (a attrs)
1393 (let ((attr (face-attribute f (car a) frame)))
1394 (insert (make-string (- max-width (length (cdr a))) ?\s)
1395 (cdr a) ": " (format "%s" attr))
1396 (if (and (eq (car a) :inherit)
1397 (not (eq attr 'unspecified)))
1398 ;; Make a hyperlink to the parent face.
1399 (save-excursion
1400 (re-search-backward ": \\([^:]+\\)" nil t)
1401 (help-xref-button 1 'help-face attr)))
1402 (insert "\n")))))
1403 (terpri)))))))
8940232b 1404
5f5c8ee5
GM
1405\f
1406;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1407;;; Face specifications (defface).
1408;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1409
1410;; Parameter FRAME Is kept for call compatibility to with previous
1411;; face implementation.
1412
1413(defun face-attr-construct (face &optional frame)
522fb950 1414 "Return a `defface'-style attribute list for FACE on FRAME.
5f5c8ee5
GM
1415Value is a property list of pairs ATTRIBUTE VALUE for all specified
1416face attributes of FACE where ATTRIBUTE is the attribute name and
1417VALUE is the specified value of that attribute."
1418 (let (result)
1419 (dolist (entry face-attribute-name-alist result)
1420 (let* ((attribute (car entry))
1421 (value (face-attribute face attribute)))
1422 (unless (eq value 'unspecified)
1423 (setq result (nconc (list attribute value) result)))))))
00f51890 1424
5f5c8ee5
GM
1425
1426(defun face-spec-set-match-display (display frame)
1427 "Non-nil if DISPLAY matches FRAME.
1428DISPLAY is part of a spec such as can be used in `defface'.
1429If FRAME is nil, the current FRAME is used."
1430 (let* ((conjuncts display)
1431 conjunct req options
1432 ;; t means we have succeeded against all the conjuncts in
1433 ;; DISPLAY that have been tested so far.
1434 (match t))
1435 (if (eq conjuncts t)
1436 (setq conjuncts nil))
1437 (while (and conjuncts match)
1438 (setq conjunct (car conjuncts)
1439 conjuncts (cdr conjuncts)
1440 req (car conjunct)
1441 options (cdr conjunct)
1442 match (cond ((eq req 'type)
428a555e 1443 (or (memq (window-system frame) options)
1695ca2b
EZ
1444 ;; FIXME: This should be revisited to use
1445 ;; display-graphic-p, provided that the
1446 ;; color selection depends on the number
1447 ;; of supported colors, and all defface's
1448 ;; are changed to look at number of colors
1449 ;; instead of (type graphic) etc.
428a555e 1450 (and (null (window-system frame))
1695ca2b 1451 (memq 'tty options))
ee716db5
GM
1452 (and (memq 'motif options)
1453 (featurep 'motif))
9b44aa8b
JD
1454 (and (memq 'gtk options)
1455 (featurep 'gtk))
ee716db5
GM
1456 (and (memq 'lucid options)
1457 (featurep 'x-toolkit)
9b44aa8b
JD
1458 (not (featurep 'motif))
1459 (not (featurep 'gtk)))
ee716db5
GM
1460 (and (memq 'x-toolkit options)
1461 (featurep 'x-toolkit))))
6b61353c
KH
1462 ((eq req 'min-colors)
1463 (>= (display-color-cells frame) (car options)))
5f5c8ee5
GM
1464 ((eq req 'class)
1465 (memq (frame-parameter frame 'display-type) options))
1466 ((eq req 'background)
1467 (memq (frame-parameter frame 'background-mode)
1468 options))
16320ac7
MB
1469 ((eq req 'supports)
1470 (display-supports-face-attributes-p options frame))
70d0c3b0 1471 (t (error "Unknown req `%S' with options `%S'"
5f5c8ee5
GM
1472 req options)))))
1473 match))
1474
1475
1476(defun face-spec-choose (spec &optional frame)
96c2938f
MB
1477 "Choose the proper attributes for FRAME, out of SPEC.
1478If SPEC is nil, return nil."
5f5c8ee5
GM
1479 (unless frame
1480 (setq frame (selected-frame)))
1481 (let ((tail spec)
cf459bb7 1482 result defaults)
5f5c8ee5 1483 (while tail
073f69e6
MB
1484 (let* ((entry (pop tail))
1485 (display (car entry))
cf459bb7
RS
1486 (attrs (cdr entry))
1487 thisval)
1488 ;; Get the attributes as actually specified by this alternative.
1489 (setq thisval
1490 (if (null (cdr attrs)) ;; was (listp (car attrs))
1491 ;; Old-style entry, the attribute list is the
1492 ;; first element.
1493 (car attrs)
1494 attrs))
1495
1496 ;; If the condition is `default', that sets the default
1497 ;; for following conditions.
1498 (if (eq display 'default)
1499 (setq defaults thisval)
1500 ;; Otherwise, if it matches, use it.
1501 (when (face-spec-set-match-display display frame)
1502 (setq result thisval)
b6b90dfc 1503 (setq tail nil)))))
cf459bb7 1504 (if defaults (append result defaults) result)))
5f5c8ee5
GM
1505
1506
1507(defun face-spec-reset-face (face &optional frame)
1508 "Reset all attributes of FACE on FRAME to unspecified."
bfe27647 1509 (let ((attrs face-attribute-name-alist))
5f5c8ee5
GM
1510 (while attrs
1511 (let ((attr-and-name (car attrs)))
bfe27647
GM
1512 (set-face-attribute face frame (car attr-and-name) 'unspecified))
1513 (setq attrs (cdr attrs)))))
5f5c8ee5
GM
1514
1515
24837f13 1516(defun face-spec-set (face spec &optional for-defface)
a1792b9b 1517 "Set FACE's face spec, which controls its appearance, to SPEC.
24837f13
RS
1518If FOR-DEFFACE is t, set the base spec, the one that `defface'
1519 and Custom set. (In that case, the caller must put it in the
1520 appropriate property, because that depends on the caller.)
1521If FOR-DEFFACE is nil, set the overriding spec (and store it
1522 in the `face-override-spec' property of FACE).
1523
1524The appearance of FACE is controlled by the base spec,
1525by any custom theme specs on top of that, and by the
a1792b9b 1526overriding spec on top of all the rest.
24837f13
RS
1527
1528FOR-DEFFACE can also be a frame, in which case we set the
1529frame-specific attributes of FACE for that frame based on SPEC.
1530That usage is deprecated.
1531
1532See `defface' for information about the format and meaning of SPEC."
1533 (if (framep for-defface)
1534 ;; Handle the deprecated case where third arg is a frame.
1535 (face-spec-set-2 face for-defface spec)
1536 (if for-defface
1537 ;; When we reset the face based on its custom spec, then it is
1538 ;; unmodified as far as Custom is concerned.
1539 (put (or (get face 'face-alias) face) 'face-modified nil)
1540 ;; When we change a face based on a spec from outside custom,
1541 ;; record it for future frames.
1542 (put (or (get face 'face-alias) face) 'face-override-spec spec))
24837f13 1543 ;; Reset each frame according to the rules implied by all its specs.
01d2eac8 1544 (dolist (frame (frame-list))
24837f13
RS
1545 (face-spec-recalc face frame))))
1546
1547(defun face-spec-recalc (face frame)
1548 "Reset the face attributes of FACE on FRAME according to its specs.
1549This applies the defface/custom spec first, then the custom theme specs,
1550then the override spec."
1551 (face-spec-reset-face face frame)
1552 (let ((face-sym (or (get face 'face-alias) face)))
7ecc4011
CY
1553 (or (get face 'customized-face)
1554 (get face 'saved-face)
1555 (face-spec-set-2 face frame (face-default-spec face)))
24837f13
RS
1556 (let ((theme-faces (reverse (get face-sym 'theme-face))))
1557 (dolist (spec theme-faces)
1558 (face-spec-set-2 face frame (cadr spec))))
1559 (face-spec-set-2 face frame (get face-sym 'face-override-spec))))
1560
1561(defun face-spec-set-2 (face frame spec)
1562 "Set the face attributes of FACE on FRAME according to SPEC."
4f77c25d
CY
1563 (let* ((spec (face-spec-choose spec frame))
1564 attrs)
1565 (while spec
1566 (when (assq (car spec) face-x-resources)
1567 (push (car spec) attrs)
1568 (push (cadr spec) attrs))
1569 (setq spec (cddr spec)))
1570 (apply 'set-face-attribute face frame (nreverse attrs))))
5f5c8ee5
GM
1571
1572(defun face-attr-match-p (face attrs &optional frame)
a9de7d29 1573 "Return t if attributes of FACE match values in plist ATTRS.
5f5c8ee5
GM
1574Optional parameter FRAME is the frame whose definition of FACE
1575is used. If nil or omitted, use the selected frame."
1576 (unless frame
1577 (setq frame (selected-frame)))
1578 (let ((list face-attribute-name-alist)
1579 (match t))
1580 (while (and match (not (null list)))
1581 (let* ((attr (car (car list)))
a9de7d29
MB
1582 (specified-value
1583 (if (plist-member attrs attr)
1584 (plist-get attrs attr)
1585 'unspecified))
5f5c8ee5 1586 (value-now (face-attribute face attr frame)))
a9de7d29 1587 (setq match (equal specified-value value-now))
5f5c8ee5
GM
1588 (setq list (cdr list))))
1589 match))
1590
5f5c8ee5
GM
1591(defun face-spec-match-p (face spec &optional frame)
1592 "Return t if FACE, on FRAME, matches what SPEC says it should look like."
1593 (face-attr-match-p face (face-spec-choose spec frame) frame))
1594
94fe8a31 1595(defsubst face-default-spec (face)
96c2938f
MB
1596 "Return the default face-spec for FACE, ignoring any user customization.
1597If there is no default for FACE, return nil."
1598 (get face 'face-defface-spec))
5f5c8ee5 1599
94fe8a31
MB
1600(defsubst face-user-default-spec (face)
1601 "Return the user's customized face-spec for FACE, or the default if none.
7cd512f2 1602If there is neither a user setting nor a default for FACE, return nil."
e44d251c
JL
1603 (or (get face 'customized-face)
1604 (get face 'saved-face)
94fe8a31
MB
1605 (face-default-spec face)))
1606
5f5c8ee5 1607\f
f795f633
EZ
1608;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1609;;; Frame-type independent color support.
1610;;; We keep the old x-* names as aliases for back-compatibility.
1611;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1612
1613(defun defined-colors (&optional frame)
1614 "Return a list of colors supported for a particular frame.
1615The argument FRAME specifies which frame to try.
1616The value may be different for frames on different display types.
43c58260
RS
1617If FRAME doesn't support colors, the value is nil.
1618If FRAME is nil, that stands for the selected frame."
9e2a2647 1619 (if (memq (framep (or frame (selected-frame))) '(x w32 ns))
f795f633 1620 (xw-defined-colors frame)
37462f4c 1621 (mapcar 'car (tty-color-alist frame))))
f795f633
EZ
1622(defalias 'x-defined-colors 'defined-colors)
1623
aa360da1
GM
1624(declare-function xw-color-defined-p "xfns.c" (color &optional frame))
1625
f795f633
EZ
1626(defun color-defined-p (color &optional frame)
1627 "Return non-nil if color COLOR is supported on frame FRAME.
1628If FRAME is omitted or nil, use the selected frame.
293b4814
EZ
1629If COLOR is the symbol `unspecified' or one of the strings
1630\"unspecified-fg\" or \"unspecified-bg\", the value is nil."
1e221c16 1631 (if (member color '(unspecified "unspecified-bg" "unspecified-fg"))
f795f633 1632 nil
9e2a2647 1633 (if (member (framep (or frame (selected-frame))) '(x w32 ns))
f795f633 1634 (xw-color-defined-p color frame)
37462f4c 1635 (numberp (tty-color-translate color frame)))))
f795f633
EZ
1636(defalias 'x-color-defined-p 'color-defined-p)
1637
aa360da1
GM
1638(declare-function xw-color-values "xfns.c" (color &optional frame))
1639
f795f633
EZ
1640(defun color-values (color &optional frame)
1641 "Return a description of the color named COLOR on frame FRAME.
22bbca61
JB
1642The value is a list of integer RGB values--(RED GREEN BLUE).
1643These values appear to range from 0 to 65280 or 65535, depending
1644on the system; white is \(65280 65280 65280\) or \(65535 65535 65535\).
f795f633
EZ
1645If FRAME is omitted or nil, use the selected frame.
1646If FRAME cannot display COLOR, the value is nil.
293b4814
EZ
1647If COLOR is the symbol `unspecified' or one of the strings
1648\"unspecified-fg\" or \"unspecified-bg\", the value is nil."
1e221c16 1649 (if (member color '(unspecified "unspecified-fg" "unspecified-bg"))
f795f633 1650 nil
9e2a2647 1651 (if (memq (framep (or frame (selected-frame))) '(x w32 ns))
f795f633
EZ
1652 (xw-color-values color frame)
1653 (tty-color-values color frame))))
1654(defalias 'x-color-values 'color-values)
1655
aa360da1
GM
1656(declare-function xw-display-color-p "xfns.c" (&optional terminal))
1657
f795f633
EZ
1658(defun display-color-p (&optional display)
1659 "Return t if DISPLAY supports color.
1660The optional argument DISPLAY specifies which display to ask about.
1661DISPLAY should be either a frame or a display name (a string).
1662If omitted or nil, that stands for the selected frame's display."
9e2a2647 1663 (if (memq (framep-on-display display) '(x w32 ns))
d5179a01
EZ
1664 (xw-display-color-p display)
1665 (tty-display-color-p display)))
f795f633
EZ
1666(defalias 'x-display-color-p 'display-color-p)
1667
aa360da1
GM
1668(declare-function x-display-grayscale-p "xfns.c" (&optional terminal))
1669
d5179a01
EZ
1670(defun display-grayscale-p (&optional display)
1671 "Return non-nil if frames on DISPLAY can display shades of gray."
1672 (let ((frame-type (framep-on-display display)))
1673 (cond
9e2a2647 1674 ((memq frame-type '(x w32 ns))
d5179a01
EZ
1675 (x-display-grayscale-p display))
1676 (t
1677 (> (tty-color-gray-shades display) 2)))))
1678
26c07a69
RS
1679(defun read-color (&optional prompt convert-to-RGB-p allow-empty-name-p msg-p)
1680 "Read a color name or RGB hex value: #RRRRGGGGBBBB.
1681Completion is available for color names, but not for RGB hex strings.
1682If the user inputs an RGB hex string, it must have the form
1683#XXXXXXXXXXXX or XXXXXXXXXXXX, where each X is a hex digit. The
1684number of Xs must be a multiple of 3, with the same number of Xs for
1685each of red, green, and blue. The order is red, green, blue.
1686
1687In addition to standard color names and RGB hex values, the following
1688are available as color candidates. In each case, the corresponding
1689color is used.
1690
1691 * `foreground at point' - foreground under the cursor
1692 * `background at point' - background under the cursor
1693
1694Checks input to be sure it represents a valid color. If not, raises
1695an error (but see exception for empty input with non-nil
1696ALLOW-EMPTY-NAME-P).
1697
1698Optional arg PROMPT is the prompt; if nil, uses a default prompt.
1699
1700Interactively, or with optional arg CONVERT-TO-RGB-P non-nil, converts
1701an input color name to an RGB hex string. Returns the RGB hex string.
1702
1703Optional arg ALLOW-EMPTY-NAME-P controls what happens if the user
1704enters an empty color name (that is, just hits `RET'). If non-nil,
1705then returns an empty color name, \"\". If nil, then raises an error.
1706Programs must test for \"\" if ALLOW-EMPTY-NAME-P is non-nil. They
1707can then perform an appropriate action in case of empty input.
1708
1709Interactively, or with optional arg MSG-P non-nil, echoes the color in
1710a message."
1711 (interactive "i\np\ni\np") ; Always convert to RGB interactively.
1712 (let* ((completion-ignore-case t)
1713 (colors (append '("foreground at point" "background at point")
1714 (defined-colors)))
1715 (color (completing-read (or prompt "Color (name or #R+G+B+): ")
1716 colors))
1717 hex-string)
1718 (cond ((string= "foreground at point" color)
1719 (setq color (foreground-color-at-point)))
1720 ((string= "background at point" color)
1721 (setq color (background-color-at-point))))
1722 (unless color
1723 (setq color ""))
1724 (setq hex-string
1725 (string-match "^#?\\([a-fA-F0-9][a-fA-F0-9][a-fA-F0-9]\\)+$" color))
1726 (if (and allow-empty-name-p (string= "" color))
1727 ""
1728 (when (and hex-string (not (eq (aref color 0) ?#)))
1729 (setq color (concat "#" color))) ; No #; add it.
1730 (unless hex-string
1731 (when (or (string= "" color) (not (test-completion color colors)))
1732 (error "No such color: %S" color))
1733 (when convert-to-RGB-p
1734 (let ((components (x-color-values color)))
1735 (unless components (error "No such color: %S" color))
1736 (unless (string-match "^#\\([a-fA-F0-9][a-fA-F0-9][a-fA-F0-9]\\)+$" color)
1737 (setq color (format "#%04X%04X%04X"
1738 (logand 65535 (nth 0 components))
1739 (logand 65535 (nth 1 components))
1740 (logand 65535 (nth 2 components))))))))
1741 (when msg-p (message "Color: `%s'" color))
1742 color)))
1743
1744;; Commented out because I decided it is better to include the
1745;; duplicates in read-color's completion list.
1746
1747;; (defun defined-colors-without-duplicates ()
1748;; "Return the list of defined colors, without the no-space versions.
1749;; For each color name, we keep the variant that DOES have spaces."
1750;; (let ((result (copy-sequence (defined-colors)))
1751;; to-be-rejected)
1752;; (save-match-data
1753;; (dolist (this result)
1754;; (if (string-match " " this)
986845af 1755;; (push (replace-regexp-in-string " " ""
26c07a69
RS
1756;; this)
1757;; to-be-rejected)))
1758;; (dolist (elt to-be-rejected)
1759;; (let ((as-found (car (member-ignore-case elt result))))
1760;; (setq result (delete as-found result)))))
1761;; result))
1762
1763(defun face-at-point ()
1764 "Return the face of the character after point.
1765If it has more than one face, return the first one.
1766Return nil if it has no specified face."
1767 (let* ((faceprop (or (get-char-property (point) 'read-face-name)
1768 (get-char-property (point) 'face)
1769 'default))
1770 (face (cond ((symbolp faceprop) faceprop)
1771 ;; List of faces (don't treat an attribute spec).
1772 ;; Just use the first face.
1773 ((and (consp faceprop) (not (keywordp (car faceprop)))
1774 (not (memq (car faceprop)
1775 '(foreground-color background-color))))
1776 (car faceprop))
1777 (t nil)))) ; Invalid face value.
1778 (if (facep face) face nil)))
1779
1780(defun foreground-color-at-point ()
1781 "Return the foreground color of the character after point."
1782 ;; `face-at-point' alone is not sufficient. It only gets named faces.
1783 ;; Need also pick up any face properties that are not associated with named faces.
1784 (let ((face (or (face-at-point)
1785 (get-char-property (point) 'read-face-name)
1786 (get-char-property (point) 'face))))
1787 (cond ((and face (symbolp face))
1788 (let ((value (face-foreground face nil 'default)))
1789 (if (member value '("unspecified-fg" "unspecified-bg"))
1790 nil
1791 value)))
1792 ((consp face)
1793 (cond ((memq 'foreground-color face) (cdr (memq 'foreground-color face)))
1794 ((memq ':foreground face) (cadr (memq ':foreground face)))))
1795 (t nil)))) ; Invalid face value.
1796
1797(defun background-color-at-point ()
1798 "Return the background color of the character after point."
1799 ;; `face-at-point' alone is not sufficient. It only gets named faces.
1800 ;; Need also pick up any face properties that are not associated with named faces.
1801 (let ((face (or (face-at-point)
1802 (get-char-property (point) 'read-face-name)
1803 (get-char-property (point) 'face))))
1804 (cond ((and face (symbolp face))
1805 (let ((value (face-background face nil 'default)))
1806 (if (member value '("unspecified-fg" "unspecified-bg"))
1807 nil
1808 value)))
1809 ((consp face)
1810 (cond ((memq 'background-color face) (cdr (memq 'background-color face)))
1811 ((memq ':background face) (cadr (memq ':background face)))))
1812 (t nil)))) ; Invalid face value.
f795f633 1813\f
5f5c8ee5
GM
1814;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1815;;; Background mode.
1816;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1817
1818(defcustom frame-background-mode nil
9201cc28 1819 "The brightness of the background.
e44d251c 1820Set this to the symbol `dark' if your background color is dark,
d4c6530f
JL
1821`light' if your background is light, or nil (automatic by default)
1822if you want Emacs to examine the brightness for you. Don't set this
1823variable with `setq'; this won't have the expected effect."
5f5c8ee5
GM
1824 :group 'faces
1825 :set #'(lambda (var value)
c20d5073 1826 (set-default var value)
b5325e78 1827 (mapc 'frame-set-background-mode (frame-list)))
5f5c8ee5 1828 :initialize 'custom-initialize-changed
d4c6530f
JL
1829 :type '(choice (const dark)
1830 (const light)
1831 (const :tag "automatic" nil)))
5f5c8ee5
GM
1832
1833
aa360da1
GM
1834(declare-function x-get-resource "frame.c"
1835 (attribute class &optional component subclass))
1836
2d0e0565
CY
1837(defvar inhibit-frame-set-background-mode nil)
1838
5f5c8ee5 1839(defun frame-set-background-mode (frame)
d8abcd91
MB
1840 "Set up display-dependent faces on FRAME.
1841Display-dependent faces are those which have different definitions
1842according to the `background-mode' and `display-type' frame parameters."
2d0e0565
CY
1843 (unless inhibit-frame-set-background-mode
1844 (let* ((bg-resource
1845 (and (window-system frame)
1846 (x-get-resource "backgroundMode" "BackgroundMode")))
1847 (bg-color (frame-parameter frame 'background-color))
1848 (terminal-bg-mode (terminal-parameter frame 'background-mode))
1849 (tty-type (tty-type frame))
df93e513
JR
1850 (default-bg-mode
1851 (if (or (window-system frame)
1852 (and tty-type
1853 (string-match "^\\(xterm\\|\\rxvt\\|dtterm\\|eterm\\)"
1854 tty-type)))
1855 'light
1856 'dark))
1857 (non-default-bg-mode (if (eq default-bg-mode 'light) 'dark 'light))
2d0e0565
CY
1858 (bg-mode
1859 (cond (frame-background-mode)
1860 (bg-resource (intern (downcase bg-resource)))
1861 (terminal-bg-mode)
2d0e0565 1862 ((equal bg-color "unspecified-fg") ; inverted colors
df93e513
JR
1863 non-default-bg-mode)
1864 ((not (color-values bg-color frame))
1865 default-bg-mode)
2d0e0565
CY
1866 ((>= (apply '+ (color-values bg-color frame))
1867 ;; Just looking at the screen, colors whose
1868 ;; values add up to .6 of the white total
1869 ;; still look dark to me.
1870 (* (apply '+ (color-values "white" frame)) .6))
1871 'light)
1872 (t 'dark)))
1873 (display-type
1874 (cond ((null (window-system frame))
1875 (if (tty-display-color-p frame) 'color 'mono))
1876 ((display-color-p frame)
1877 'color)
1878 ((x-display-grayscale-p frame)
1879 'grayscale)
1880 (t 'mono)))
1881 (old-bg-mode
1882 (frame-parameter frame 'background-mode))
1883 (old-display-type
1884 (frame-parameter frame 'display-type)))
1885
1886 (unless (and (eq bg-mode old-bg-mode) (eq display-type old-display-type))
1887 (let ((locally-modified-faces nil)
1888 ;; Prevent face-spec-recalc from calling this function
1889 ;; again, resulting in a loop (bug#911).
1890 (inhibit-frame-set-background-mode t))
1891 ;; Before modifying the frame parameters, collect a list of
1892 ;; faces that don't match what their face-spec says they
1893 ;; should look like. We then avoid changing these faces
1894 ;; below. These are the faces whose attributes were
1895 ;; modified on FRAME. We use a negative list on the
1896 ;; assumption that most faces will be unmodified, so we can
1897 ;; avoid consing in the common case.
1898 (dolist (face (face-list))
1899 (and (not (get face 'face-override-spec))
1900 (not (face-spec-match-p face
1901 (face-user-default-spec face)
1902 (selected-frame)))
1903 (push face locally-modified-faces)))
1904 ;; Now change to the new frame parameters
1905 (modify-frame-parameters frame
1906 (list (cons 'background-mode bg-mode)
1907 (cons 'display-type display-type)))
1908 ;; For all named faces, choose face specs matching the new frame
1909 ;; parameters, unless they have been locally modified.
1910 (dolist (face (face-list))
1911 (unless (memq face locally-modified-faces)
1912 (face-spec-recalc face frame))))))))
5f5c8ee5 1913
5f5c8ee5
GM
1914\f
1915;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1916;;; Frame creation.
1917;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1918
aa360da1
GM
1919(declare-function x-parse-geometry "frame.c" (string))
1920
5f5c8ee5
GM
1921(defun x-handle-named-frame-geometry (parameters)
1922 "Add geometry parameters for a named frame to parameter list PARAMETERS.
1923Value is the new parameter list."
1924 (let* ((name (or (cdr (assq 'name parameters))
1925 (cdr (assq 'name default-frame-alist))))
1926 (x-resource-name name)
8c61dd01
CY
1927 (res-geometry (when name
1928 ;; FIXME: x-get-resource fails if the X
1929 ;; connection is not open, e.g. if we call
1930 ;; make-frame-on-display. We should detect
1931 ;; this case here, and open the connection.
1932 ;; (Bug#3194).
1933 (ignore-errors
1934 (x-get-resource "geometry" "Geometry")))))
5f5c8ee5
GM
1935 (when res-geometry
1936 (let ((parsed (x-parse-geometry res-geometry)))
1937 ;; If the resource specifies a position, call the position
1938 ;; and size "user-specified".
1939 (when (or (assq 'top parsed)
1940 (assq 'left parsed))
1941 (setq parsed (append '((user-position . t) (user-size . t)) parsed)))
1942 ;; Put the geometry parameters at the end. Copy
1943 ;; default-frame-alist so that they go after it.
1944 (setq parameters (append parameters default-frame-alist parsed))))
1945 parameters))
1946
1947
1948(defun x-handle-reverse-video (frame parameters)
1949 "Handle the reverse-video frame parameter and X resource.
1950`x-create-frame' does not handle this one."
1951 (when (cdr (or (assq 'reverse parameters)
1952 (assq 'reverse default-frame-alist)
1953 (let ((resource (x-get-resource "reverseVideo"
1954 "ReverseVideo")))
1955 (if resource
1956 (cons nil (member (downcase resource)
1957 '("on" "true")))))))
1958 (let* ((params (frame-parameters frame))
1959 (bg (cdr (assq 'foreground-color params)))
1960 (fg (cdr (assq 'background-color params))))
1961 (modify-frame-parameters frame
1962 (list (cons 'foreground-color fg)
1963 (cons 'background-color bg)))
1964 (if (equal bg (cdr (assq 'border-color params)))
1965 (modify-frame-parameters frame
1966 (list (cons 'border-color fg))))
1967 (if (equal bg (cdr (assq 'mouse-color params)))
1968 (modify-frame-parameters frame
1969 (list (cons 'mouse-color fg))))
1970 (if (equal bg (cdr (assq 'cursor-color params)))
1971 (modify-frame-parameters frame
1972 (list (cons 'cursor-color fg)))))))
1973
aa360da1
GM
1974(declare-function x-create-frame "xfns.c" (parms))
1975(declare-function x-setup-function-keys "term/x-win" (frame))
5f5c8ee5
GM
1976
1977(defun x-create-frame-with-faces (&optional parameters)
1978 "Create a frame from optional frame parameters PARAMETERS.
1979Parameters not specified by PARAMETERS are taken from
1980`default-frame-alist'. If PARAMETERS specify a frame name,
1981handle X geometry resources for that name. If either PARAMETERS
1982or `default-frame-alist' contains a `reverse' parameter, or
1983the X resource ``reverseVideo'' is present, handle that.
1984Value is the new frame created."
1985 (setq parameters (x-handle-named-frame-geometry parameters))
41e6403e
CY
1986 (let* ((params (copy-tree parameters))
1987 (visibility-spec (assq 'visibility parameters))
1988 (delayed-params '(foreground-color background-color font
1989 border-color cursor-color mouse-color
1990 visibility scroll-bar-foreground
1991 scroll-bar-background))
1992 frame success)
1993 (dolist (param delayed-params)
1994 (setq params (assq-delete-all param params)))
1995 (setq frame (x-create-frame `((visibility . nil) . ,params)))
5f5c8ee5
GM
1996 (unwind-protect
1997 (progn
2bb819d5 1998 (x-setup-function-keys frame)
5f5c8ee5
GM
1999 (x-handle-reverse-video frame parameters)
2000 (frame-set-background-mode frame)
fa7015f4 2001 (face-set-after-frame-default frame parameters)
095fe281 2002 (if (null visibility-spec)
5f5c8ee5
GM
2003 (make-frame-visible frame)
2004 (modify-frame-parameters frame (list visibility-spec)))
2005 (setq success t))
2006 (unless success
2007 (delete-frame frame)))
2008 frame))
2009
fa7015f4
CY
2010(defun face-set-after-frame-default (frame &optional parameters)
2011 "Initialize the frame-local faces of FRAME.
2012Calculate the face definitions using the face specs, custom theme
41e6403e 2013settings, X resources, and `face-new-frame-defaults'.
fa7015f4
CY
2014Finally, apply any relevant face attributes found amongst the
2015frame parameters in PARAMETERS and `default-frame-alist'."
2016 (dolist (face (nreverse (face-list)))
2017 (condition-case ()
fa7015f4
CY
2018 (progn
2019 ;; Initialize faces from face spec and custom theme.
2020 (face-spec-recalc face frame)
41e6403e
CY
2021 ;; X resouces for the default face are applied during
2022 ;; x-create-frame.
2023 (and (not (eq face 'default))
e51fe00a 2024 (memq (window-system frame) '(x w32))
41e6403e 2025 (make-face-x-resource-internal face frame))
fa7015f4
CY
2026 ;; Apply attributes specified by face-new-frame-defaults
2027 (internal-merge-in-global-face face frame))
2028 ;; Don't let invalid specs prevent frame creation.
2029 (error nil)))
2030 ;; Apply attributes specified by frame parameters.
8db9c5ee 2031 (let ((face-params '((foreground-color default :foreground)
fa7015f4
CY
2032 (background-color default :background)
2033 (font default :font)
2034 (border-color border :background)
2035 (cursor-color cursor :background)
2036 (scroll-bar-foreground scroll-bar :foreground)
2037 (scroll-bar-background scroll-bar :background)
2038 (mouse-color mouse :background))))
8db9c5ee 2039 (dolist (param face-params)
fa7015f4
CY
2040 (let* ((param-name (nth 0 param))
2041 (value (cdr (or (assq param-name parameters)
2042 (assq param-name default-frame-alist)))))
2043 (if value
2044 (set-face-attribute (nth 1 param) frame
2045 (nth 2 param) value))))))
5f5c8ee5 2046
53990c84
EZ
2047(defun tty-handle-reverse-video (frame parameters)
2048 "Handle the reverse-video frame parameter for terminal frames."
2049 (when (cdr (or (assq 'reverse parameters)
2050 (assq 'reverse default-frame-alist)))
53990c84
EZ
2051 (let* ((params (frame-parameters frame))
2052 (bg (cdr (assq 'foreground-color params)))
2053 (fg (cdr (assq 'background-color params))))
2054 (modify-frame-parameters frame
2055 (list (cons 'foreground-color fg)
2056 (cons 'background-color bg)))
2057 (if (equal bg (cdr (assq 'mouse-color params)))
2058 (modify-frame-parameters frame
2059 (list (cons 'mouse-color fg))))
2060 (if (equal bg (cdr (assq 'cursor-color params)))
2061 (modify-frame-parameters frame
2062 (list (cons 'cursor-color fg)))))))
2063
5f5c8ee5
GM
2064
2065(defun tty-create-frame-with-faces (&optional parameters)
2066 "Create a frame from optional frame parameters PARAMETERS.
2067Parameters not specified by PARAMETERS are taken from
2068`default-frame-alist'. If either PARAMETERS or `default-frame-alist'
2069contains a `reverse' parameter, handle that. Value is the new frame
2070created."
2071 (let ((frame (make-terminal-frame parameters))
2072 success)
2073 (unwind-protect
061e4e7f 2074 (with-selected-frame frame
53990c84 2075 (tty-handle-reverse-video frame (frame-parameters frame))
cd85984a 2076
8782c744
SM
2077 (unless (terminal-parameter frame 'terminal-initted)
2078 (set-terminal-parameter frame 'terminal-initted t)
2079 (set-locale-environment nil frame)
2080 (tty-run-terminal-initialization frame))
5f5c8ee5 2081 (frame-set-background-mode frame)
fa7015f4 2082 (face-set-after-frame-default frame parameters)
5f5c8ee5
GM
2083 (setq success t))
2084 (unless success
2085 (delete-frame frame)))
2086 frame))
465fceed 2087
b34c34da
KL
2088(defun tty-find-type (pred type)
2089 "Return the longest prefix of TYPE to which PRED returns non-nil.
2090TYPE should be a tty type name such as \"xterm-16color\".
2091
2092The function tries only those prefixes that are followed by a
2093dash or underscore in the original type name, like \"xterm\" in
2094the above example."
2095 (let (hyphend)
2096 (while (and type
2097 (not (funcall pred type)))
2098 ;; Strip off last hyphen and what follows, then try again
2099 (setq type
2100 (if (setq hyphend (string-match "[-_][^-_]+$" type))
2101 (substring type 0 hyphend)
2102 nil))))
2103 type)
2104
8cbd7bed
KL
2105(defun tty-run-terminal-initialization (frame &optional type)
2106 "Run the special initialization code for the terminal type of FRAME.
2107The optional TYPE parameter may be used to override the autodetected
2108terminal type to a different value."
2109 (setq type (or type (tty-type frame)))
cd85984a
KL
2110 ;; Load library for our terminal type.
2111 ;; User init file can set term-file-prefix to nil to prevent this.
2112 (with-selected-frame frame
8782c744 2113 (unless (null term-file-prefix)
b34c34da
KL
2114 (let* (term-init-func)
2115 ;; First, load the terminal initialization file, if it is
2116 ;; available and it hasn't been loaded already.
2117 (tty-find-type #'(lambda (type)
2118 (let ((file (locate-library (concat term-file-prefix type))))
2119 (and file
2120 (or (assoc file load-history)
2121 (load file t t)))))
8cbd7bed 2122 type)
b34c34da
KL
2123 ;; Next, try to find a matching initialization function, and call it.
2124 (tty-find-type #'(lambda (type)
2125 (fboundp (setq term-init-func
2126 (intern (concat "terminal-init-" type)))))
8cbd7bed 2127 type)
567c8878 2128 (when (fboundp term-init-func)
b34c34da
KL
2129 (funcall term-init-func))
2130 (set-terminal-parameter frame 'terminal-initted term-init-func)))))
465fceed 2131
5f5c8ee5
GM
2132;; Called from C function init_display to initialize faces of the
2133;; dumped terminal frame on startup.
465fceed 2134
5f5c8ee5
GM
2135(defun tty-set-up-initial-frame-faces ()
2136 (let ((frame (selected-frame)))
2137 (frame-set-background-mode frame)
2138 (face-set-after-frame-default frame)))
00f51890 2139
465fceed 2140
465fceed 2141
5f5c8ee5
GM
2142\f
2143;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2144;;; Compatiblity with 20.2
2145;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
72418504 2146
5f5c8ee5 2147;; Update a frame's faces when we change its default font.
465fceed 2148
e16e7eac 2149(defalias 'frame-update-faces 'ignore "")
4b22f844 2150(make-obsolete 'frame-update-faces "no longer necessary." "21.1")
e8e4cda0 2151
5f5c8ee5
GM
2152;; Update the colors of FACE, after FRAME's own colors have been
2153;; changed.
1bcb155c 2154
522fb950
JB
2155(define-obsolete-function-alias 'frame-update-face-colors
2156 'frame-set-background-mode "21.1")
d11fba25 2157
bdda3754 2158\f
5f5c8ee5
GM
2159;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2160;;; Standard faces.
2161;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
465fceed 2162
5f5c8ee5
GM
2163(defgroup basic-faces nil
2164 "The standard faces of Emacs."
2165 :group 'faces)
ef436392 2166
bacbbe8f
CY
2167(defface default
2168 '((t nil)) ; If this were nil, face-defface-spec would not be set.
5f5c8ee5
GM
2169 "Basic default face."
2170 :group 'basic-faces)
465fceed 2171
51d44a36
JL
2172(defface bold
2173 '((t :weight bold))
2174 "Basic bold face."
2175 :group 'basic-faces)
2176
2177(defface italic
2178 '((((supports :slant italic))
2179 :slant italic)
2180 (((supports :underline t))
2181 :underline t)
2182 (t
2183 ;; default to italic, even it doesn't appear to be supported,
2184 ;; because in some cases the display engine will do it's own
2185 ;; workaround (to `dim' on ttys)
2186 :slant italic))
2187 "Basic italic face."
2188 :group 'basic-faces)
2189
2190(defface bold-italic
2191 '((t :weight bold :slant italic))
2192 "Basic bold-italic face."
2193 :group 'basic-faces)
2194
2195(defface underline
2196 '((((supports :underline t))
2197 :underline t)
2198 (((supports :weight bold))
2199 :weight bold)
2200 (t :underline t))
2201 "Basic underlined face."
2202 :group 'basic-faces)
2203
2204(defface fixed-pitch
d5f57335 2205 '((t :family "Monospace"))
51d44a36
JL
2206 "The basic fixed-pitch face."
2207 :group 'basic-faces)
2208
2209(defface variable-pitch
ed008205 2210 '((t :family "Sans Serif"))
51d44a36
JL
2211 "The basic variable-pitch face."
2212 :group 'basic-faces)
2213
2214(defface shadow
2215 '((((class color grayscale) (min-colors 88) (background light))
2216 :foreground "grey50")
2217 (((class color grayscale) (min-colors 88) (background dark))
2218 :foreground "grey70")
2219 (((class color) (min-colors 8) (background light))
2220 :foreground "green")
2221 (((class color) (min-colors 8) (background dark))
2222 :foreground "yellow"))
2223 "Basic face for shadowed text."
2224 :group 'basic-faces
2225 :version "22.1")
2226
59746720
JL
2227(defface link
2228 '((((class color) (min-colors 88) (background light))
2229 :foreground "blue1" :underline t)
2230 (((class color) (background light))
2231 :foreground "blue" :underline t)
2232 (((class color) (min-colors 88) (background dark))
2233 :foreground "cyan1" :underline t)
2234 (((class color) (background dark))
2235 :foreground "cyan" :underline t)
2236 (t :inherit underline))
2237 "Basic face for unvisited links."
2238 :group 'basic-faces
2239 :version "22.1")
2240
2241(defface link-visited
2242 '((default :inherit link)
2243 (((class color) (background light)) :foreground "magenta4")
2244 (((class color) (background dark)) :foreground "violet"))
2245 "Basic face for visited links."
2246 :group 'basic-faces
2247 :version "22.1")
2248
51d44a36
JL
2249(defface highlight
2250 '((((class color) (min-colors 88) (background light))
2251 :background "darkseagreen2")
2252 (((class color) (min-colors 88) (background dark))
2253 :background "darkolivegreen")
2254 (((class color) (min-colors 16) (background light))
2255 :background "darkseagreen2")
2256 (((class color) (min-colors 16) (background dark))
2257 :background "darkolivegreen")
2258 (((class color) (min-colors 8))
2259 :background "green" :foreground "black")
2260 (t :inverse-video t))
2261 "Basic face for highlighting."
2262 :group 'basic-faces)
2263
51d44a36
JL
2264(defface region
2265 '((((class color) (min-colors 88) (background dark))
2266 :background "blue3")
2267 (((class color) (min-colors 88) (background light))
2268 :background "lightgoldenrod2")
2269 (((class color) (min-colors 16) (background dark))
2270 :background "blue3")
2271 (((class color) (min-colors 16) (background light))
2272 :background "lightgoldenrod2")
2273 (((class color) (min-colors 8))
2274 :background "blue" :foreground "white")
2275 (((type tty) (class mono))
2276 :inverse-video t)
2277 (t :background "gray"))
2278 "Basic face for highlighting the region."
2279 :version "21.1"
2280 :group 'basic-faces)
2281
2282(defface secondary-selection
2283 '((((class color) (min-colors 88) (background light))
2284 :background "yellow1")
2285 (((class color) (min-colors 88) (background dark))
2286 :background "SkyBlue4")
2287 (((class color) (min-colors 16) (background light))
2288 :background "yellow")
2289 (((class color) (min-colors 16) (background dark))
2290 :background "SkyBlue4")
2291 (((class color) (min-colors 8))
2292 :background "cyan" :foreground "black")
2293 (t :inverse-video t))
2294 "Basic face for displaying the secondary selection."
2295 :group 'basic-faces)
2296
2297(defface trailing-whitespace
2298 '((((class color) (background light))
2299 :background "red1")
2300 (((class color) (background dark))
2301 :background "red1")
2302 (t :inverse-video t))
2303 "Basic face for highlighting trailing whitespace."
2304 :version "21.1"
d4c6530f 2305 :group 'whitespace-faces ; like `show-trailing-whitespace'
51d44a36
JL
2306 :group 'basic-faces)
2307
2308(defface escape-glyph
2309 '((((background dark)) :foreground "cyan")
2310 ;; See the comment in minibuffer-prompt for
2311 ;; the reason not to use blue on MS-DOS.
2312 (((type pc)) :foreground "magenta")
2313 ;; red4 is too dark, but some say blue is too loud.
2314 ;; brown seems to work ok. -- rms.
2315 (t :foreground "brown"))
1a77c985 2316 "Face for characters displayed as sequences using `^' or `\\'."
51d44a36
JL
2317 :group 'basic-faces
2318 :version "22.1")
2319
2320(defface nobreak-space
2321 '((((class color) (min-colors 88)) :inherit escape-glyph :underline t)
2322 (((class color) (min-colors 8)) :background "magenta")
2323 (t :inverse-video t))
2324 "Face for displaying nobreak space."
2325 :group 'basic-faces
2326 :version "22.1")
72418504 2327
97bca259
JL
2328(defgroup mode-line-faces nil
2329 "Faces used in the mode line."
6d968826 2330 :group 'mode-line
97bca259
JL
2331 :group 'faces
2332 :version "22.1")
2333
3759f14b 2334(defface mode-line
11c2175c 2335 '((((class color) (min-colors 88))
b6b90dfc
SM
2336 :box (:line-width -1 :style released-button)
2337 :background "grey75" :foreground "black")
5f5c8ee5 2338 (t
b6b90dfc 2339 :inverse-video t))
c5752dcb 2340 "Basic mode line face for selected window."
a4391cf1 2341 :version "21.1"
97bca259 2342 :group 'mode-line-faces
5f5c8ee5 2343 :group 'basic-faces)
465fceed 2344
c5752dcb 2345(defface mode-line-inactive
cf459bb7 2346 '((default
b6b90dfc 2347 :inherit mode-line)
11c2175c 2348 (((class color) (min-colors 88) (background light))
c5752dcb
KS
2349 :weight light
2350 :box (:line-width -1 :color "grey75" :style nil)
2351 :foreground "grey20" :background "grey90")
11c2175c 2352 (((class color) (min-colors 88) (background dark) )
53a2f6e1
MB
2353 :weight light
2354 :box (:line-width -1 :color "grey40" :style nil)
b6b90dfc 2355 :foreground "grey80" :background "grey30"))
c5752dcb 2356 "Basic mode line face for non-selected windows."
bf247b6e 2357 :version "22.1"
97bca259
JL
2358 :group 'mode-line-faces
2359 :group 'basic-faces)
2360
2361(defface mode-line-highlight
2362 '((((class color) (min-colors 88))
2363 :box (:line-width 2 :color "grey40" :style released-button))
2364 (t
2365 :inherit highlight))
2366 "Basic mode line face for highlighting."
2367 :version "22.1"
2368 :group 'mode-line-faces
2369 :group 'basic-faces)
2370
313e2fc4
GM
2371(defface mode-line-emphasis
2372 '((t (:weight bold)))
2373 "Face used to emphasize certain mode line features.
2374Use the face `mode-line-highlight' for features that can be selected."
2375 :version "23.1"
2376 :group 'mode-line-faces
2377 :group 'basic-faces)
2378
97bca259
JL
2379(defface mode-line-buffer-id
2380 '((t (:weight bold)))
2381 "Face used for buffer identification parts of the mode line."
2382 :version "22.1"
2383 :group 'mode-line-faces
c5752dcb
KS
2384 :group 'basic-faces)
2385
3759f14b
GM
2386;; Make `modeline' an alias for `mode-line', for compatibility.
2387(put 'modeline 'face-alias 'mode-line)
c5752dcb 2388(put 'modeline-inactive 'face-alias 'mode-line-inactive)
e0dcaa3f 2389(put 'modeline-highlight 'face-alias 'mode-line-highlight)
97bca259 2390(put 'modeline-buffer-id 'face-alias 'mode-line-buffer-id)
d11fba25 2391
e5e7779f 2392(defface header-line
cf459bb7 2393 '((default
6b61353c
KH
2394 :inherit mode-line)
2395 (((type tty))
1cb4393e
MB
2396 ;; This used to be `:inverse-video t', but that doesn't look very
2397 ;; good when combined with inverse-video mode-lines and multiple
2398 ;; windows. Underlining looks better, and is more consistent with
2399 ;; the window-system face variants, which deemphasize the
2400 ;; header-line in relation to the mode-line face. If a terminal
2401 ;; can't underline, then the header-line will end up without any
2402 ;; highlighting; this may be too confusing in general, although it
2403 ;; happens to look good with the only current use of header-lines,
2404 ;; the info browser. XXX
6b61353c 2405 :inverse-video nil ;Override the value inherited from mode-line.
a7ac92b6 2406 :underline t)
98490598 2407 (((class color grayscale) (background light))
bdab1d43 2408 :background "grey90" :foreground "grey20"
6b61353c 2409 :box nil)
98490598 2410 (((class color grayscale) (background dark))
bdab1d43 2411 :background "grey20" :foreground "grey90"
6b61353c 2412 :box nil)
98490598 2413 (((class mono) (background light))
bdab1d43
MB
2414 :background "white" :foreground "black"
2415 :inverse-video nil
2416 :box nil
6b61353c 2417 :underline t)
98490598 2418 (((class mono) (background dark))
bdab1d43
MB
2419 :background "black" :foreground "white"
2420 :inverse-video nil
2421 :box nil
6b61353c 2422 :underline t))
e5e7779f 2423 "Basic header-line face."
a4391cf1 2424 :version "21.1"
5f5c8ee5 2425 :group 'basic-faces)
e8e4cda0 2426
51d44a36
JL
2427(defface vertical-border
2428 '((((type tty)) :inherit mode-line-inactive))
2429 "Face used for vertical window dividers on ttys."
2430 :version "22.1"
5f5c8ee5 2431 :group 'basic-faces)
d11fba25 2432
9ed779e8
JL
2433(defface minibuffer-prompt
2434 '((((background dark)) :foreground "cyan")
2435 ;; Don't use blue because many users of the MS-DOS port customize
2436 ;; their foreground color to be blue.
2437 (((type pc)) :foreground "magenta")
c57a1dc6 2438 (t :foreground "medium blue"))
9ed779e8
JL
2439 "Face for minibuffer prompts.
2440By default, Emacs automatically adds this face to the value of
2441`minibuffer-prompt-properties', which is a list of text properties
2442used to display the prompt text."
bf247b6e 2443 :version "22.1"
31cf46d3
RS
2444 :group 'basic-faces)
2445
2446(setq minibuffer-prompt-properties
2447 (append minibuffer-prompt-properties (list 'face 'minibuffer-prompt)))
2448
f1cae29e 2449(defface fringe
d6d59057 2450 '((((class color) (background light))
b6b90dfc
SM
2451 :background "grey95")
2452 (((class color) (background dark))
2453 :background "grey10")
2454 (t
2455 :background "gray"))
f1cae29e
GM
2456 "Basic face for the fringes to the left and right of windows under X."
2457 :version "21.1"
84fe35f0 2458 :group 'frames
f1cae29e
GM
2459 :group 'basic-faces)
2460
d390b4d2 2461(defface scroll-bar '((t nil))
f1cae29e
GM
2462 "Basic face for the scroll bar colors under X."
2463 :version "21.1"
84fe35f0 2464 :group 'frames
f1cae29e
GM
2465 :group 'basic-faces)
2466
d390b4d2 2467(defface border '((t nil))
f1cae29e
GM
2468 "Basic face for the frame border under X."
2469 :version "21.1"
84fe35f0 2470 :group 'frames
f1cae29e
GM
2471 :group 'basic-faces)
2472
d390b4d2 2473(defface cursor '((t nil))
8d413c2b
KS
2474 "Basic face for the cursor color under X.
2475Note: Other faces cannot inherit from the cursor face."
f1cae29e 2476 :version "21.1"
84fe35f0 2477 :group 'cursor
f1cae29e
GM
2478 :group 'basic-faces)
2479
8d413c2b 2480(put 'cursor 'face-no-inherit t)
f1cae29e 2481
d390b4d2 2482(defface mouse '((t nil))
f1cae29e 2483 "Basic face for the mouse color under X."
e59176e2 2484 :version "21.1"
84fe35f0 2485 :group 'mouse
5f5c8ee5 2486 :group 'basic-faces)
bdda3754 2487
51d44a36
JL
2488(defface tool-bar
2489 '((default
2490 :box (:line-width 1 :style released-button)
2491 :foreground "black")
9e2a2647 2492 (((type x w32 ns) (class color))
51d44a36
JL
2493 :background "grey75")
2494 (((type x) (class mono))
2495 :background "grey"))
2496 "Basic tool-bar face."
2497 :version "21.1"
5f5c8ee5 2498 :group 'basic-faces)
bdda3754 2499
51d44a36
JL
2500(defface menu
2501 '((((type tty))
2502 :inverse-video t)
2503 (((type x-toolkit))
2504 )
16320ac7 2505 (t
51d44a36
JL
2506 :inverse-video t))
2507 "Basic face for the font and colors of the menu bar and popup menus."
a4391cf1 2508 :version "21.1"
51d44a36 2509 :group 'menu
a4391cf1 2510 :group 'basic-faces)
465fceed 2511
465fceed 2512\f
5f5c8ee5
GM
2513;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2514;;; Manipulating font names.
2515;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2516
2517;; This is here for compatibilty with Emacs 20.2. For example,
07f27947
DL
2518;; international/fontset.el uses x-resolve-font-name. The following
2519;; functions are not used in the face implementation itself.
465fceed 2520
d7fa5aa2
RS
2521(defvar x-font-regexp nil)
2522(defvar x-font-regexp-head nil)
2523(defvar x-font-regexp-weight nil)
2524(defvar x-font-regexp-slant nil)
465fceed 2525
021ca129
KH
2526(defconst x-font-regexp-weight-subnum 1)
2527(defconst x-font-regexp-slant-subnum 2)
2528(defconst x-font-regexp-swidth-subnum 3)
2529(defconst x-font-regexp-adstyle-subnum 4)
2530
465fceed
ER
2531;;; Regexps matching font names in "Host Portable Character Representation."
2532;;;
2533(let ((- "[-?]")
2534 (foundry "[^-]+")
2535 (family "[^-]+")
2536 (weight "\\(bold\\|demibold\\|medium\\)") ; 1
2537; (weight\? "\\(\\*\\|bold\\|demibold\\|medium\\|\\)") ; 1
2538 (weight\? "\\([^-]*\\)") ; 1
2539 (slant "\\([ior]\\)") ; 2
2540; (slant\? "\\([ior?*]?\\)") ; 2
2541 (slant\? "\\([^-]?\\)") ; 2
2542; (swidth "\\(\\*\\|normal\\|semicondensed\\|\\)") ; 3
2543 (swidth "\\([^-]*\\)") ; 3
2544; (adstyle "\\(\\*\\|sans\\|\\)") ; 4
c8787b81 2545 (adstyle "\\([^-]*\\)") ; 4
465fceed
ER
2546 (pixelsize "[0-9]+")
2547 (pointsize "[0-9][0-9]+")
2548 (resx "[0-9][0-9]+")
2549 (resy "[0-9][0-9]+")
2550 (spacing "[cmp?*]")
2551 (avgwidth "[0-9]+")
2552 (registry "[^-]+")
2553 (encoding "[^-]+")
2554 )
2555 (setq x-font-regexp
2556 (concat "\\`\\*?[-?*]"
2557 foundry - family - weight\? - slant\? - swidth - adstyle -
e20d641f
RS
2558 pixelsize - pointsize - resx - resy - spacing - avgwidth -
2559 registry - encoding "\\*?\\'"
465fceed
ER
2560 ))
2561 (setq x-font-regexp-head
2562 (concat "\\`[-?*]" foundry - family - weight\? - slant\?
2563 "\\([-*?]\\|\\'\\)"))
2564 (setq x-font-regexp-slant (concat - slant -))
2565 (setq x-font-regexp-weight (concat - weight -))
70d0c3b0 2566 nil)
465fceed 2567
5f5c8ee5 2568
281dc1c2
JB
2569(defun x-resolve-font-name (pattern &optional face frame)
2570 "Return a font name matching PATTERN.
ac4c2e6b 2571All wildcards in PATTERN are instantiated.
10d89673
JB
2572If PATTERN is nil, return the name of the frame's base font, which never
2573contains wildcards.
e17dbede
RS
2574Given optional arguments FACE and FRAME, return a font which is
2575also the same size as FACE on FRAME, or fail."
14e6867c
RS
2576 (or (symbolp face)
2577 (setq face (face-name face)))
2578 (and (eq frame t)
2579 (setq frame nil))
10d89673 2580 (if pattern
8db93e45 2581 ;; Note that x-list-fonts has code to handle a face with nil as its font.
abd89b80 2582 (let ((fonts (x-list-fonts pattern face frame 1)))
10d89673
JB
2583 (or fonts
2584 (if face
962a60aa
RS
2585 (if (string-match "\\*" pattern)
2586 (if (null (face-font face))
2587 (error "No matching fonts are the same height as the frame default font")
2588 (error "No matching fonts are the same height as face `%s'" face))
2589 (if (null (face-font face))
2590 (error "Height of font `%s' doesn't match the frame default font"
2591 pattern)
2592 (error "Height of font `%s' doesn't match face `%s'"
2593 pattern face)))
7cf6fac1 2594 (error "No fonts match `%s'" pattern)))
10d89673
JB
2595 (car fonts))
2596 (cdr (assq 'font (frame-parameters (selected-frame))))))
281dc1c2 2597
5f5c8ee5 2598
465fceed 2599(defun x-frob-font-weight (font which)
b7ffee5f
SM
2600 (let ((case-fold-search t))
2601 (cond ((string-match x-font-regexp font)
2602 (concat (substring font 0
2603 (match-beginning x-font-regexp-weight-subnum))
2604 which
2605 (substring font (match-end x-font-regexp-weight-subnum)
2606 (match-beginning x-font-regexp-adstyle-subnum))
2607 ;; Replace the ADD_STYLE_NAME field with *
2608 ;; because the info in it may not be the same
2609 ;; for related fonts.
2610 "*"
2611 (substring font (match-end x-font-regexp-adstyle-subnum))))
528fbf51
RS
2612 ((string-match x-font-regexp-head font)
2613 (concat (substring font 0 (match-beginning 1)) which
2614 (substring font (match-end 1))))
2615 ((string-match x-font-regexp-weight font)
b7ffee5f
SM
2616 (concat (substring font 0 (match-beginning 1)) which
2617 (substring font (match-end 1)))))))
2598a293 2618(make-obsolete 'x-frob-font-weight 'make-face-... "21.1")
5f5c8ee5 2619
465fceed 2620(defun x-frob-font-slant (font which)
b7ffee5f
SM
2621 (let ((case-fold-search t))
2622 (cond ((string-match x-font-regexp font)
2623 (concat (substring font 0
2624 (match-beginning x-font-regexp-slant-subnum))
2625 which
2626 (substring font (match-end x-font-regexp-slant-subnum)
2627 (match-beginning x-font-regexp-adstyle-subnum))
2628 ;; Replace the ADD_STYLE_NAME field with *
2629 ;; because the info in it may not be the same
2630 ;; for related fonts.
2631 "*"
2632 (substring font (match-end x-font-regexp-adstyle-subnum))))
528fbf51
RS
2633 ((string-match x-font-regexp-head font)
2634 (concat (substring font 0 (match-beginning 2)) which
2635 (substring font (match-end 2))))
2636 ((string-match x-font-regexp-slant font)
b7ffee5f
SM
2637 (concat (substring font 0 (match-beginning 1)) which
2638 (substring font (match-end 1)))))))
2598a293 2639(make-obsolete 'x-frob-font-slant 'make-face-... "21.1")
5f5c8ee5 2640
3fc67165
EZ
2641;; These aliases are here so that we don't get warnings about obsolete
2642;; functions from the byte compiler.
2643(defalias 'internal-frob-font-weight 'x-frob-font-weight)
2644(defalias 'internal-frob-font-slant 'x-frob-font-slant)
2645
465fceed 2646(defun x-make-font-bold (font)
f3f31ccf
RS
2647 "Given an X font specification, make a bold version of it.
2648If that can't be done, return nil."
3fc67165 2649 (internal-frob-font-weight font "bold"))
2598a293 2650(make-obsolete 'x-make-font-bold 'make-face-bold "21.1")
5f5c8ee5 2651
465fceed 2652(defun x-make-font-demibold (font)
f3f31ccf
RS
2653 "Given an X font specification, make a demibold version of it.
2654If that can't be done, return nil."
3fc67165 2655 (internal-frob-font-weight font "demibold"))
2598a293 2656(make-obsolete 'x-make-font-demibold 'make-face-bold "21.1")
5f5c8ee5 2657
465fceed 2658(defun x-make-font-unbold (font)
f3f31ccf
RS
2659 "Given an X font specification, make a non-bold version of it.
2660If that can't be done, return nil."
3fc67165 2661 (internal-frob-font-weight font "medium"))
2598a293 2662(make-obsolete 'x-make-font-unbold 'make-face-unbold "21.1")
5f5c8ee5 2663
465fceed 2664(defun x-make-font-italic (font)
f3f31ccf
RS
2665 "Given an X font specification, make an italic version of it.
2666If that can't be done, return nil."
3fc67165 2667 (internal-frob-font-slant font "i"))
2598a293 2668(make-obsolete 'x-make-font-italic 'make-face-italic "21.1")
5f5c8ee5 2669
465fceed 2670(defun x-make-font-oblique (font) ; you say tomayto...
f3f31ccf
RS
2671 "Given an X font specification, make an oblique version of it.
2672If that can't be done, return nil."
3fc67165 2673 (internal-frob-font-slant font "o"))
2598a293 2674(make-obsolete 'x-make-font-oblique 'make-face-italic "21.1")
5f5c8ee5 2675
465fceed 2676(defun x-make-font-unitalic (font)
f3f31ccf
RS
2677 "Given an X font specification, make a non-italic version of it.
2678If that can't be done, return nil."
3fc67165 2679 (internal-frob-font-slant font "r"))
2598a293 2680(make-obsolete 'x-make-font-unitalic 'make-face-unitalic "21.1")
5f5c8ee5 2681
b7d7285c
KH
2682(defun x-make-font-bold-italic (font)
2683 "Given an X font specification, make a bold and italic version of it.
2684If that can't be done, return nil."
3fc67165
EZ
2685 (and (setq font (internal-frob-font-weight font "bold"))
2686 (internal-frob-font-slant font "i")))
2598a293 2687(make-obsolete 'x-make-font-bold-italic 'make-face-bold-italic "21.1")
113ab303 2688
f0138172
JB
2689(provide 'faces)
2690
e3b5b35b 2691;; arch-tag: 19a4759f-2963-445f-b004-425b9aadd7d6
70d0c3b0 2692;;; faces.el ends here