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