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