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