(quail-set-keyboard-layout): Typo fixed.
[bpt/emacs.git] / lisp / faces.el
CommitLineData
5f5c8ee5 1;;; faces.el --- Lisp faces
465fceed 2
84fe35f0 3;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000
5f5c8ee5 4;; Free Software Foundation, Inc.
465fceed
ER
5
6;; This file is part of GNU Emacs.
7
8;; GNU Emacs is free software; you can redistribute it and/or modify
9;; it under the terms of the GNU General Public License as published by
10;; the Free Software Foundation; either version 2, or (at your option)
11;; any later version.
12
13;; GNU Emacs is distributed in the hope that it will be useful,
14;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16;; GNU General Public License for more details.
17
18;; You should have received a copy of the GNU General Public License
b578f267
EN
19;; along with GNU Emacs; see the file COPYING. If not, write to the
20;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21;; Boston, MA 02111-1307, USA.
465fceed
ER
22
23;;; Commentary:
24
465fceed
ER
25;;; Code:
26
39b3b754 27(eval-when-compile
5f5c8ee5
GM
28 (require 'cl))
29
30(require 'cus-face)
31
32\f
33;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
34;;; Font selection.
35;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
36
37(defgroup font-selection nil
38 "Influencing face font selection."
39 :group 'faces)
40
41
42(defcustom face-font-selection-order
43 '(:width :height :weight :slant)
44 "*A list specifying how face font selection chooses fonts.
45Each of the four symbols `:width', `:height', `:weight', and `:slant'
46must appear once in the list, and the list must not contain any other
47elements. Font selection tries to find a best matching font for
48those face attributes first that appear first in the list. For
49example, if `:slant' appears before `:height', font selection first
50tries to find a font with a suitable slant, even if this results in
51a font height that isn't optimal."
52 :tag "Font selection order."
53 :group 'font-selection
54 :set #'(lambda (symbol value)
55 (set-default symbol value)
56 (internal-set-font-selection-order value)))
57
58
59(defcustom face-font-family-alternatives
60 '(("courier" "fixed")
3855860a 61 ("helv" "helvetica" "arial" "fixed"))
5f5c8ee5
GM
62 "*Alist of alternative font family names.
63Each element has the the form (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...).
64If fonts of family FAMILY can't be loaded, try ALTERNATIVE1, then
65ALTERNATIVE2 etc."
66 :tag "Alternative font families to try."
67 :group 'font-selection
68 :set #'(lambda (symbol value)
69 (set-default symbol value)
70 (internal-set-alternative-font-family-alist value)))
71
72
bdda3754 73\f
5f5c8ee5
GM
74;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
75;;; Creation, copying.
76;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
77
78
79(defun face-list ()
80 "Return a list of all defined face names."
81 (mapcar #'car face-new-frame-defaults))
82
83
84;;; ### If not frame-local initialize by what X resources?
85
86(defun make-face (face &optional no-init-from-resources)
87 "Define a new face with name FACE, a symbol.
88NO-INIT-FROM-RESOURCES non-nil means don't initialize frame-local
89variants of FACE from X resources. (X resources recognized are found
90in the global variable `face-x-resources'.) If FACE is already known
91as a face, leave it unmodified. Value is FACE."
92 (interactive "SMake face: ")
93 (unless (facep face)
94 ;; Make frame-local faces (this also makes the global one).
95 (dolist (frame (frame-list))
96 (internal-make-lisp-face face frame))
97 ;; Add the face to the face menu.
98 (when (fboundp 'facemenu-add-new-face)
99 (facemenu-add-new-face face))
100 ;; Define frame-local faces for all frames from X resources.
101 (unless no-init-from-resources
102 (make-face-x-resource-internal face)))
103 face)
104
105
106(defun make-empty-face (face)
107 "Define a new, empty face with name FACE.
108If the face already exists, it is left unmodified. Value is FACE."
109 (interactive "SMake empty face: ")
110 (make-face face 'no-init-from-resources))
111
112
113(defun copy-face (old-face new-face &optional frame new-frame)
114 "Define a face just like OLD-FACE, with name NEW-FACE.
115
116If NEW-FACE already exists as a face, it is modified to be like
117OLD-FACE. If it doesn't already exist, it is created.
118
119If the optional argument FRAME is given as a frame, NEW-FACE is
120changed on FRAME only.
121If FRAME is t, the frame-independent default specification for OLD-FACE
122is copied to NEW-FACE.
123If FRAME is nil, copying is done for the frame-independent defaults
124and for each existing frame.
125
126If the optional fourth argument NEW-FRAME is given,
127copy the information from face OLD-FACE on frame FRAME
128to NEW-FACE on frame NEW-FRAME."
129 (let ((inhibit-quit t))
130 (if (null frame)
131 (progn
132 (dolist (frame (frame-list))
133 (copy-face old-face new-face frame))
134 (copy-face old-face new-face t))
135 (internal-copy-lisp-face old-face new-face frame new-frame))
136 new-face))
137
138
139\f
140;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
141;;; Obsolete functions
142;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
143
144;; The functions in this section are defined because Lisp packages use
145;; them, despite the prefix `internal-' suggesting that they are
146;; private to the face implementation.
147
148(defun internal-find-face (name &optional frame)
149 "Retrieve the face named NAME.
150Return nil if there is no such face.
151If the optional argument FRAME is given, this gets the face NAME for
152that frame; otherwise, it uses the selected frame.
153If FRAME is the symbol t, then the global, non-frame face is returned.
154If NAME is already a face, it is simply returned.
155
156This function is defined for compatibility with Emacs 20.2. It
157should not be used anymore."
158 (facep name))
159
160
161(defun internal-get-face (name &optional frame)
162 "Retrieve the face named NAME; error if there is none.
163If the optional argument FRAME is given, this gets the face NAME for
164that frame; otherwise, it uses the selected frame.
165If FRAME is the symbol t, then the global, non-frame face is returned.
166If NAME is already a face, it is simply returned.
167
168This function is defined for compatibility with Emacs 20.2. It
169should not be used anymore."
170 (or (internal-find-face name frame)
171 (check-face name)))
172
173
174\f
175;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
176;;; Predicates, type checks.
177;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
178
179(defun facep (face)
180 "Return non-nil if FACE is a face name."
181 (internal-lisp-face-p face))
182
183
184(defun check-face (face)
185 "Signal an error if FACE doesn't name a face.
186Value is FACE."
187 (unless (facep face)
188 (error "Not a face: %s" face))
189 face)
190
191
192;; The ID returned is not to be confused with the internally used IDs
193;; of realized faces. The ID assigned to Lisp faces is used to
194;; support faces in display table entries.
195
196(defun face-id (face &optional frame)
197 "Return the interNal ID of face with name FACE.
198If optional argument FRAME is nil or omitted, use the selected frame."
199 (check-face face)
200 (get face 'face))
201
202
203(defun face-equal (face1 face2 &optional frame)
204 "Non-nil if faces FACE1 and FACE2 are equal.
205Faces are considered equal if all their attributes are equal.
206If the optional argument FRAME is given, report on face FACE in that frame.
207If FRAME is t, report on the defaults for face FACE (for new frames).
208If FRAME is omitted or nil, use the selected frame."
209 (internal-lisp-face-equal-p face1 face2 frame))
210
211
212(defun face-differs-from-default-p (face &optional frame)
213 "Non-nil if FACE displays differently from the default face.
214If the optional argument FRAME is given, report on face FACE in that frame.
215If FRAME is t, report on the defaults for face FACE (for new frames).
216If FRAME is omitted or nil, use the selected frame.
217A face is considered to be ``the same'' as the default face if it is
218actually specified in the same way (equal attributes) or if it is
219fully-unspecified, and thus inherits the attributes of any face it
220is displayed on top of."
6904df7a
GM
221 (cond ((eq frame t) (setq frame nil))
222 ((null frame) (setq frame (selected-frame))))
223 (let* ((v1 (internal-lisp-face-p face frame))
224 (n (if v1 (length v1) 0))
225 (v2 (internal-lisp-face-p 'default frame))
226 (i 1))
227 (unless v1
228 (error "Not a face: %S" face))
229 (while (and (< i n)
230 (or (eq 'unspecified (aref v1 i))
231 (equal (aref v1 i) (aref v2 i))))
232 (setq i (1+ i)))
233 (< i n)))
5f5c8ee5
GM
234
235
236(defun face-nontrivial-p (face &optional frame)
237 "True if face FACE has some non-nil attribute.
238If the optional argument FRAME is given, report on face FACE in that frame.
239If FRAME is t, report on the defaults for face FACE (for new frames).
240If FRAME is omitted or nil, use the selected frame."
241 (not (internal-lisp-face-empty-p face frame)))
242
243
244\f
245;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
246;;; Setting face attributes from X resources.
247;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
248
249(defcustom face-x-resources
250 '((:family (".attributeFamily" . "Face.AttributeFamily"))
251 (:width (".attributeWidth" . "Face.AttributeWidth"))
252 (:height (".attributeHeight" . "Face.AttributeHeight"))
253 (:weight (".attributeWeight" . "Face.AttributeWeight"))
254 (:slant (".attributeSlant" . "Face.AttributeSlant"))
255 (:foreground (".attributeForeground" . "Face.AttributeForeground"))
256 (:background (".attributeBackground" . "Face.AttributeBackground"))
257 (:overline (".attributeOverline" . "Face.AttributeOverline"))
258 (:strike-through (".attributeStrikeThrough" . "Face.AttributeStrikeThrough"))
259 (:box (".attributeBox" . "Face.AttributeBox"))
260 (:underline (".attributeUnderline" . "Face.AttributeUnderline"))
261 (:inverse-video (".attributeInverse" . "Face.AttributeInverse"))
262 (:stipple
263 (".attributeStipple" . "Face.AttributeStipple")
264 (".attributeBackgroundPixmap" . "Face.AttributeBackgroundPixmap"))
265 (:font (".attributeFont" . "Face.AttributeFont"))
266 (:bold (".attributeBold" . "Face.AttributeBold"))
267 (:italic (".attributeItalic" . "Face.AttributeItalic"))
268 (:font (".attributeFont" . "Face.AttributeFont")))
269 "*List of X resources and classes for face attributes.
270Each element has the form (ATTRIBUTE ENTRY1 ENTRY2...) where ATTRIBUTE is
271the name of a face attribute, and each ENTRY is a cons of the form
272(RESOURCE . CLASS) with RESOURCE being the resource and CLASS being the
273X resource class for the attribute."
274 :type 'sexp
275 :group 'faces)
276
277
278(defun set-face-attribute-from-resource (face attribute resource class frame)
279 "Set FACE's ATTRIBUTE from X resource RESOURCE, class CLASS on FRAME.
280Value is the attribute value specified by the resource, or nil
281if not present. This function displays a message if the resource
282specifies an invalid attribute."
283 (let* ((face-name (face-name face))
284 (value (internal-face-x-get-resource (concat face-name resource)
285 class frame)))
286 (when value
287 (condition-case ()
288 (internal-set-lisp-face-attribute-from-resource
289 face attribute (downcase value) frame)
290 (error
291 (message "Face %s, frame %s: invalid attribute %s %s from X resource"
292 face-name frame attribute value))))
293 value))
294
295
296(defun set-face-attributes-from-resources (face frame)
297 "Set attributes of FACE from X resources for FRAME."
298 (when (memq (framep frame) '(x w32))
299 (dolist (definition face-x-resources)
300 (let ((attribute (car definition)))
301 (dolist (entry (cdr definition))
302 (set-face-attribute-from-resource face attribute (car entry)
303 (cdr entry) frame))))))
304
305
306(defun make-face-x-resource-internal (face &optional frame)
307 "Fill frame-local FACE on FRAME from X resources.
308FRAME nil or not specified means do it for all frames."
309 (if (null frame)
310 (dolist (frame (frame-list))
311 (set-face-attributes-from-resources face frame))
312 (set-face-attributes-from-resources face frame)))
313
314
315\f
316;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
317;;; Retrieving face attributes.
318;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
319
feaad827 320(defun face-name (face)
465fceed 321 "Return the name of face FACE."
5f5c8ee5
GM
322 (symbol-name (check-face face)))
323
324
325(defun face-attribute (face attribute &optional frame)
326 "Return the value of FACE's ATTRIBUTE on FRAME.
327If the optional argument FRAME is given, report on face FACE in that frame.
328If FRAME is t, report on the defaults for face FACE (for new frames).
329If FRAME is omitted or nil, use the selected frame."
330 (internal-get-lisp-face-attribute face attribute frame))
331
332
333(defun face-foreground (face &optional frame)
334 "Return the foreground color name of FACE, or nil if unspecified.
335If the optional argument FRAME is given, report on face FACE in that frame.
336If FRAME is t, report on the defaults for face FACE (for new frames).
337If FRAME is omitted or nil, use the selected frame."
338 (internal-get-lisp-face-attribute face :foreground frame))
339
340
341(defun face-background (face &optional frame)
342 "Return the background color name of FACE, or nil if unspecified.
343If the optional argument FRAME is given, report on face FACE in that frame.
344If FRAME is t, report on the defaults for face FACE (for new frames).
345If FRAME is omitted or nil, use the selected frame."
346 (internal-get-lisp-face-attribute face :background frame))
347
348
349(defun face-stipple (face &optional frame)
350 "Return the stipple pixmap name of FACE, or nil if unspecified.
351If the optional argument FRAME is given, report on face FACE in that frame.
352If FRAME is t, report on the defaults for face FACE (for new frames).
353If FRAME is omitted or nil, use the selected frame."
354 (internal-get-lisp-face-attribute face :stipple frame))
355
356
357(defalias 'face-background-pixmap 'face-stipple)
358
359
360(defun face-underline-p (face &optional frame)
361 "Return non-nil if FACE is underlined.
362If the optional argument FRAME is given, report on face FACE in that frame.
363If FRAME is t, report on the defaults for face FACE (for new frames).
364If FRAME is omitted or nil, use the selected frame."
365 (eq (face-attribute face :underline frame) t))
366
367
368(defun face-inverse-video-p (face &optional frame)
369 "Return non-nil if FACE is in inverse video on FRAME.
370If the optional argument FRAME is given, report on face FACE in that frame.
371If FRAME is t, report on the defaults for face FACE (for new frames).
372If FRAME is omitted or nil, use the selected frame."
373 (eq (face-attribute face :inverse-video frame) t))
374
375
376(defun face-bold-p (face &optional frame)
377 "Return non-nil if the font of FACE is bold on FRAME.
378If the optional argument FRAME is given, report on face FACE in that frame.
379If FRAME is t, report on the defaults for face FACE (for new frames).
380If FRAME is omitted or nil, use the selected frame.
381Use `face-attribute' for finer control."
382 (let ((bold (face-attribute face :weight frame)))
da2c7b8c 383 (memq bold '(semi-bold bold extra-bold ultra-bold))))
5f5c8ee5
GM
384
385
386(defun face-italic-p (face &optional frame)
387 "Return non-nil if the font of FACE is italic on FRAME.
388If the optional argument FRAME is given, report on face FACE in that frame.
389If FRAME is t, report on the defaults for face FACE (for new frames).
390If FRAME is omitted or nil, use the selected frame.
391Use `face-attribute' for finer control."
392 (let ((italic (face-attribute face :slant frame)))
a48f6020 393 (memq italic '(italic oblique))))
5f5c8ee5
GM
394
395
396
397
398\f
399;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
400;;; Face documentation.
401;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
402
403(defun face-documentation (face)
404 "Get the documentation string for FACE."
405 (get face 'face-documentation))
406
407
408(defun set-face-documentation (face string)
409 "Set the documentation string for FACE to STRING."
291cfb96 410 ;; Perhaps the text should go in DOC.
49435801 411 (put face 'face-documentation (purecopy string)))
5f5c8ee5
GM
412
413
414(defalias 'face-doc-string 'face-documentation)
415(defalias 'set-face-doc-string 'set-face-documentation)
416
417
418\f
419;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
420;; Setting face attributes.
421;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
422
423
424(defun set-face-attribute (face frame &rest args)
425 "Set attributes of FACE on FRAME from ARGS.
426
427FRAME nil means change attributes on all frames. FRAME t means change
428the default for new frames (this is done automatically each time an
429attribute is changed on all frames).
430
431ARGS must come in pairs ATTRIBUTE VALUE. ATTRIBUTE must be a valid
432face attribute name. All attributes can be set to `unspecified';
433this fact is not further mentioned below.
434
435The following attributes are recognized:
436
437`:family'
438
439VALUE must be a string specifying the font family, e.g. ``courier'',
440or a fontset alias name. If a font family is specified, wild-cards `*'
441and `?' are allowed.
442
443`:width'
444
445VALUE specifies the relative proportionate width of the font to use.
446It must be one of the symbols `ultra-condensed', `extra-condensed',
447`condensed', `semi-condensed', `normal', `semi-expanded', `expanded',
448`extra-expanded', or `ultra-expanded'.
449
450`:height'
451
452VALUE must be an integer specifying the height of the font to use in
4531/10 pt.
454
455`:weight'
456
457VALUE specifies the weight of the font to use. It must be one of the
458symbols `ultra-bold', `extra-bold', `bold', `semi-bold', `normal',
459`semi-light', `light', `extra-light', `ultra-light'.
460
461`:slant'
462
463VALUE specifies the slant of the font to use. It must be one of the
464symbols `italic', `oblique', `normal', `reverse-italic', or
465`reverse-oblique'.
466
467`:foreground', `:background'
468
469VALUE must be a color name, a string.
470
471`:underline'
472
473VALUE specifies whether characters in FACE should be underlined. If
474VALUE is t, underline with foreground color of the face. If VALUE is
475a string, underline with that color. If VALUE is nil, explicitly
476don't underline.
477
478`:overline'
479
480VALUE specifies whether characters in FACE should be overlined. If
481VALUE is t, overline with foreground color of the face. If VALUE is a
482string, overline with that color. If VALUE is nil, explicitly don't
483overline.
484
485`:strike-through'
486
487VALUE specifies whether characters in FACE should be drawn with a line
488striking through them. If VALUE is t, use the foreground color of the
489face. If VALUE is a string, strike-through with that color. If VALUE
490is nil, explicitly don't strike through.
491
492`:box'
493
494VALUE specifies whether characters in FACE should have a box drawn
495around them. If VALUE is nil, explicitly don't draw boxes. If
496VALUE is t, draw a box with lines of width 1 in the foreground color
497of the face. If VALUE is a string, the string must be a color name,
498and the box is drawn in that color with a line width of 1. Otherwise,
499VALUE must be a property list of the form `(:line-width WIDTH
500:color COLOR :style STYLE)'. If a keyword/value pair is missing from
501the property list, a default value will be used for the value, as
502specified below. WIDTH specifies the width of the lines to draw; it
503defaults to 1. COLOR is the name of the color to draw in, default is
504the foreground color of the face for simple boxes, and the background
505color of the face for 3D boxes. STYLE specifies whether a 3D box
506should be draw. If STYLE is `released-button', draw a box looking
507like a released 3D button. If STYLE is `pressed-button' draw a box
508that appears like a pressed button. If STYLE is nil, the default if
509the property list doesn't contain a style specification, draw a 2D
510box.
511
512`:inverse-video'
513
514VALUE specifies whether characters in FACE should be displayed in
515inverse video. VALUE must be one of t or nil.
516
517`:stipple'
518
519If VALUE is a string, it must be the name of a file of pixmap data.
520The directories listed in the `x-bitmap-file-path' variable are
521searched. Alternatively, VALUE may be a list of the form (WIDTH
522HEIGHT DATA) where WIDTH and HEIGHT are the size in pixels, and DATA
523is a string containing the raw bits of the bitmap. VALUE nil means
524explicitly don't use a stipple pattern.
525
526For convenience, attributes `:family', `:width', `:height', `:weight',
527and `:slant' may also be set in one step from an X font name:
528
529`:font'
530
531Set font-related face attributes from VALUE. VALUE must be a valid
532XLFD font name. If it is a font name pattern, the first matching font
533will be used.
534
535For compatibility with Emacs 20, keywords `:bold' and `:italic' can
536be used to specify that a bold or italic font should be used. VALUE
537must be t or nil in that case. A value of `unspecified' is not allowed."
84fe35f0 538 (setq args (purecopy args))
5f5c8ee5
GM
539 (cond ((null frame)
540 ;; Change face on all frames.
541 (dolist (frame (frame-list))
542 (apply #'set-face-attribute face frame args))
543 ;; Record that as a default for new frames.
544 (apply #'set-face-attribute face t args))
545 (t
546 (while args
547 (internal-set-lisp-face-attribute face (car args)
291cfb96
DL
548 (purecopy (cadr args))
549 frame)
5f5c8ee5
GM
550 (setq args (cdr (cdr args)))))))
551
552
39cac3e7 553(defun make-face-bold (face &optional frame noerror)
5f5c8ee5
GM
554 "Make the font of FACE be bold, if possible.
555FRAME nil or not specified means change face on all frames.
39cac3e7 556Argument NOERROR is ignored and retained for compatibility.
5f5c8ee5 557Use `set-face-attribute' for finer control of the font weight."
84fe35f0 558 (interactive (list (read-face-name "Make which face bold ")))
5f5c8ee5
GM
559 (set-face-attribute face frame :weight 'bold))
560
561
39cac3e7 562(defun make-face-unbold (face &optional frame noerror)
5f5c8ee5 563 "Make the font of FACE be non-bold, if possible.
39cac3e7
GM
564FRAME nil or not specified means change face on all frames.
565Argument NOERROR is ignored and retained for compatibility."
84fe35f0 566 (interactive (list (read-face-name "Make which face non-bold ")))
5f5c8ee5
GM
567 (set-face-attribute face frame :weight 'normal))
568
569
39cac3e7 570(defun make-face-italic (face &optional frame noerror)
5f5c8ee5
GM
571 "Make the font of FACE be italic, if possible.
572FRAME nil or not specified means change face on all frames.
39cac3e7 573Argument NOERROR is ignored and retained for compatibility.
5f5c8ee5 574Use `set-face-attribute' for finer control of the font slant."
84fe35f0 575 (interactive (list (read-face-name "Make which face italic ")))
5f5c8ee5
GM
576 (set-face-attribute face frame :slant 'italic))
577
578
39cac3e7 579(defun make-face-unitalic (face &optional frame noerror)
5f5c8ee5
GM
580 "Make the font of FACE be non-italic, if possible.
581FRAME nil or not specified means change face on all frames."
84fe35f0 582 (interactive (list (read-face-name "Make which face non-italic ")))
5f5c8ee5
GM
583 (set-face-attribute face frame :slant 'normal))
584
585
39cac3e7 586(defun make-face-bold-italic (face &optional frame noerror)
5f5c8ee5
GM
587 "Make the font of FACE be bold and italic, if possible.
588FRAME nil or not specified means change face on all frames.
39cac3e7 589Argument NOERROR is ignored and retained for compatibility.
5f5c8ee5
GM
590Use `set-face-attribute' for finer control of font weight and slant."
591 (interactive (list (read-face-name "Make which face bold-italic: ")))
592 (set-face-attribute face frame :weight 'bold :slant 'italic))
593
594
595(defun set-face-font (face font &optional frame)
596 "Change font-related attributes of FACE to those of FONT (a string).
597FRAME nil or not specified means change face on all frames.
598This sets the attributes `:family', `:width', `:height', `:weight',
599and `:slant'. When called interactively, prompt for the face and font."
600 (interactive (read-face-and-attribute :font))
601 (set-face-attribute face frame :font font))
602
603
604;; Implementation note: Emulating gray background colors with a
605;; stipple pattern is now part of the face realization process, and is
606;; done in C depending on the frame on which the face is realized.
607
608(defun set-face-background (face color &optional frame)
609 "Change the background color of face FACE to COLOR (a string).
610FRAME nil or not specified means change face on all frames.
611When called interactively, prompt for the face and color."
612 (interactive (read-face-and-attribute :background))
613 (set-face-attribute face frame :background color))
614
615
616(defun set-face-foreground (face color &optional frame)
617 "Change the foreground color of face FACE to COLOR (a string).
618FRAME nil or not specified means change face on all frames.
619When called interactively, prompt for the face and color."
620 (interactive (read-face-and-attribute :foreground))
621 (set-face-attribute face frame :foreground color))
622
623
624(defun set-face-stipple (face stipple &optional frame)
625 "Change the stipple pixmap of face FACE to STIPPLE.
626FRAME nil or not specified means change face on all frames.
627STIPPLE. should be a string, the name of a file of pixmap data.
628The directories listed in the `x-bitmap-file-path' variable are searched.
629
630Alternatively, STIPPLE may be a list of the form (WIDTH HEIGHT DATA)
631where WIDTH and HEIGHT are the size in pixels,
632and DATA is a string, containing the raw bits of the bitmap."
633 (interactive (read-face-and-attribute :stipple))
634 (set-face-attribute face frame :stipple stipple))
635
636
637(defun set-face-underline (face underline &optional frame)
638 "Specify whether face FACE is underlined.
639UNDERLINE nil means FACE explicitly doesn't underline.
640UNDERLINE non-nil means FACE explicitly does underlining
641with the same of the foreground color.
642If UNDERLINE is a string, underline with the color named UNDERLINE.
643FRAME nil or not specified means change face on all frames.
644Use `set-face-attribute' to ``unspecify'' underlining."
645 (interactive
646 (let ((list (read-face-and-attribute :underline)))
647 (list (car list) (eq (car (cdr list)) t))))
648 (set-face-attribute face frame :underline underline))
649
650
651(defun set-face-underline-p (face underline-p &optional frame)
652 "Specify whether face FACE is underlined.
653UNDERLINE-P nil means FACE explicitly doesn't underline.
654UNDERLINE-P non-nil means FACE explicitly does underlining.
655FRAME nil or not specified means change face on all frames.
656Use `set-face-attribute' to ``unspecify'' underlining."
657 (interactive
658 (let ((list (read-face-and-attribute :underline)))
659 (list (car list) (eq (car (cdr list)) t))))
660 (set-face-attribute face frame :underline underline-p))
661
662
663(defun set-face-inverse-video-p (face inverse-video-p &optional frame)
664 "Specify whether face FACE is in inverse video.
665INVERSE-VIDEO-P non-nil means FACE displays explicitly in inverse video.
666INVERSE-VIDEO-P nil means FACE explicitly is not in inverse video.
667FRAME nil or not specified means change face on all frames.
668Use `set-face-attribute' to ``unspecify'' the inverse video attribute."
669 (interactive
670 (let ((list (read-face-and-attribute :inverse-video)))
671 (list (car list) (eq (car (cdr list)) t))))
672 (set-face-attribute face frame :inverse-video inverse-video-p))
673
674
675(defun set-face-bold-p (face bold-p &optional frame)
676 "Specify whether face FACE is bold.
677BOLD-P non-nil means FACE should explicitly display bold.
678BOLD-P nil means FACE should explicitly display non-bold.
679FRAME nil or not specified means change face on all frames.
680Use `set-face-attribute' or `modify-face' for finer control."
681 (if (null bold-p)
682 (make-face-unbold face frame)
683 (make-face-bold face frame)))
684
685
686(defun set-face-italic-p (face italic-p &optional frame)
687 "Specify whether face FACE is italic.
688ITALIC-P non-nil means FACE should explicitly display italic.
689ITALIC-P nil means FACE should explicitly display non-italic.
690FRAME nil or not specified means change face on all frames.
691Use `set-face-attribute' or `modify-face' for finer control."
692 (if (null italic-p)
693 (make-face-unitalic face frame)
694 (make-face-italic face frame)))
695
696
697(defalias 'set-face-background-pixmap 'set-face-stipple)
698
699
700(defun invert-face (face &optional frame)
701 "Swap the foreground and background colors of FACE.
702FRAME nil or not specified means change face on all frames.
703If FACE specifies neither foreground nor background color,
704set its foreground and background to the background and foreground
705of the default face. Value is FACE."
84fe35f0 706 (interactive (list (read-face-name "Invert face ")))
5f5c8ee5
GM
707 (let ((fg (face-attribute face :foreground frame))
708 (bg (face-attribute face :background frame)))
709 (if (or fg bg)
710 (set-face-attribute face frame :foreground bg :background fg)
711 (set-face-attribute face frame
712 :foreground
713 (face-attribute 'default :background frame)
714 :background
715 (face-attribute 'default :foreground frame))))
716 face)
717
718\f
719;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
720;;; Interactively modifying faces.
721;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
722
723(defun read-face-name (prompt)
724 "Read and return a face symbol, prompting with PROMPT.
725Value is a symbol naming a known face."
726 (let ((face-list (mapcar #'(lambda (x) (cons (symbol-name x) x))
727 (face-list)))
84fe35f0 728 (def (thing-at-point 'symbol))
5f5c8ee5 729 face)
84fe35f0
DL
730 (cond ((assoc def face-list)
731 (setq prompt (concat prompt "(default " def "): ")))
732 (t (setq def nil)
733 (setq prompt (concat prompt ": "))))
734 (while (equal "" (setq face (completing-read
735 prompt face-list nil t nil nil def))))
5f5c8ee5
GM
736 (intern face)))
737
738
739(defun face-valid-attribute-values (attribute &optional frame)
740 "Return valid values for face attribute ATTRIBUTE.
741The optional argument FRAME is used to determine available fonts
742and colors. If it is nil or not specified, the selected frame is
743used. Value is an alist of (NAME . VALUE) if ATTRIBUTE expects a value
744out of a set of discrete values. Value is `integerp' if ATTRIBUTE expects
745an integer value."
fbd5f1cc
GM
746 (let (valid)
747 (setq valid
748 (case attribute
749 (:family
750 (if window-system
751 (mapcar #'(lambda (x) (cons (car x) (car x)))
752 (x-font-family-list))
753 ;; Only one font on TTYs.
754 (list (cons "default" "default"))))
755 ((:width :weight :slant :inverse-video)
756 (mapcar #'(lambda (x) (cons (symbol-name x) x))
757 (internal-lisp-face-attribute-values attribute)))
758 ((:underline :overline :strike-through :box)
759 (if window-system
760 (nconc (mapcar #'(lambda (x) (cons (symbol-name x) x))
761 (internal-lisp-face-attribute-values attribute))
762 (mapcar #'(lambda (c) (cons c c))
763 (x-defined-colors frame)))
764 (mapcar #'(lambda (x) (cons (symbol-name x) x))
765 (internal-lisp-face-attribute-values attribute))))
766 ((:foreground :background)
767 (mapcar #'(lambda (c) (cons c c))
f795f633 768 (defined-colors frame)))
fbd5f1cc
GM
769 ((:height)
770 'integerp)
771 (:stipple
f3625fc0 772 (and (memq window-system '(x w32))
fbd5f1cc
GM
773 (mapcar #'list
774 (apply #'nconc (mapcar #'directory-files
775 x-bitmap-file-path)))))
776 (t
777 (error "Internal error"))))
778 (if (listp valid)
779 (nconc (list (cons "unspecified" 'unspecified)) valid)
780 valid)))
781
5f5c8ee5
GM
782
783
784(defvar face-attribute-name-alist
785 '((:family . "font family")
786 (:width . "character set width")
787 (:height . "height in 1/10 pt")
788 (:weight . "weight")
789 (:slant . "slant")
790 (:underline . "underline")
791 (:overline . "overline")
792 (:strike-through . "strike-through")
793 (:box . "box")
794 (:inverse-video . "inverse-video display")
795 (:foreground . "foreground color")
796 (:background . "background color")
797 (:stipple . "background stipple"))
798 "An alist of descriptive names for face attributes.
799Each element has the form (ATTRIBUTE-NAME . DESCRIPTION) where
800ATTRIBUTE-NAME is a face attribute name (a keyword symbol), and
801DESCRIPTION is a descriptive name for ATTRIBUTE-NAME.")
802
803
804(defun face-descriptive-attribute-name (attribute)
805 "Return a descriptive name for ATTRIBUTE."
806 (cdr (assq attribute face-attribute-name-alist)))
807
808
809(defun face-read-string (face default name &optional completion-alist)
810 "Interactively read a face attribute string value.
811FACE is the face whose attribute is read. DEFAULT is the default
812value to return if no new value is entered. NAME is a descriptive
813name of the attribute for prompting. COMPLETION-ALIST is an alist
814of valid values, if non-nil.
815
fbd5f1cc 816Entering nothing accepts the default value DEFAULT.
5f5c8ee5
GM
817Value is the new attribute value."
818 (let* ((completion-ignore-case t)
819 (value (completing-read
820 (if default
821 (format "Set face %s %s (default %s): "
822 face name (downcase (if (symbolp default)
823 (symbol-name default)
824 default)))
825 (format "Set face %s %s: " face name))
826 completion-alist)))
fbd5f1cc 827 (if (equal value "") default value)))
5f5c8ee5
GM
828
829
830(defun face-read-integer (face default name)
831 "Interactively read an integer face attribute value.
832FACE is the face whose attribute is read. DEFAULT is the default
833value to return if no new value is entered. NAME is a descriptive
834name of the attribute for prompting. Value is the new attribute value."
fbd5f1cc
GM
835 (let ((new-value
836 (face-read-string face
293b4814
EZ
837 (if (memq default
838 '(unspecified
839 "unspecified-fg"
840 "unspecified-bg"))
f9d2fdc4 841 default
fbd5f1cc
GM
842 (int-to-string default))
843 name
844 (list (cons "unspecified" 'unspecified)))))
293b4814 845 (if (memq new-value '(unspecified "unspecified-fg" "unspecified-bg"))
fbd5f1cc
GM
846 new-value
847 (string-to-int new-value))))
5f5c8ee5
GM
848
849
850(defun read-face-attribute (face attribute &optional frame)
851 "Interactively read a new value for FACE's ATTRIBUTE.
852Optional argument FRAME nil or unspecified means read an attribute value
853of a global face. Value is the new attribute value."
854 (let* ((old-value (face-attribute face attribute frame))
855 (attribute-name (face-descriptive-attribute-name attribute))
856 (valid (face-valid-attribute-values attribute frame))
857 new-value)
858 ;; Represent complex attribute values as strings by printing them
859 ;; out. Stipple can be a vector; (WIDTH HEIGHT DATA). Box can be
860 ;; a list `(:width WIDTH :color COLOR)' or `(:width WIDTH :shadow
861 ;; SHADOW)'.
862 (when (and (or (eq attribute :stipple)
863 (eq attribute :box))
864 (or (consp old-value)
865 (vectorp old-value)))
866 (setq old-value (prin1-to-string old-value)))
867 (cond ((listp valid)
868 (setq new-value
fbd5f1cc 869 (face-read-string face old-value attribute-name valid))
f795f633
EZ
870 ;; Terminal frames can support colors that don't appear
871 ;; explicitly in VALID, using color approximation code
872 ;; in tty-colors.el.
873 (if (and (memq attribute '(:foreground :background))
874 (not (memq window-system '(x w32 mac)))
f9d2fdc4 875 (not (memq new-value
293b4814
EZ
876 '(unspecified
877 "unspecified-fg"
878 "unspecified-bg"))))
37462f4c 879 (setq new-value (car (tty-color-desc new-value frame))))
fbd5f1cc
GM
880 (unless (eq new-value 'unspecified)
881 (setq new-value (cdr (assoc new-value valid)))))
5f5c8ee5
GM
882 ((eq valid 'integerp)
883 (setq new-value (face-read-integer face old-value attribute-name)))
884 (t (error "Internal error")))
885 ;; Convert stipple and box value text we read back to a list or
886 ;; vector if it looks like one. This makes the assumption that a
887 ;; pixmap file name won't start with an open-paren.
888 (when (and (or (eq attribute :stipple)
889 (eq attribute :box))
890 (stringp new-value)
891 (string-match "^[[(]" new-value))
892 (setq new-value (read new-value)))
893 new-value))
894
895
896(defun read-face-font (face &optional frame)
897 "Read the name of a font for FACE on FRAME.
898If optional argument FRAME Is nil or omitted, use the selected frame."
899 (let ((completion-ignore-case t))
900 (completing-read "Set font attributes of face %s from font: "
901 face (x-list-fonts "*" nil frame))))
902
903
904(defun read-all-face-attributes (face &optional frame)
905 "Interactively read all attributes for FACE.
906If optional argument FRAME Is nil or omitted, use the selected frame.
907Value is a property list of attribute names and new values."
908 (let (result)
909 (dolist (attribute face-attribute-name-alist result)
910 (setq result (cons (car attribute)
911 (cons (read-face-attribute face (car attribute) frame)
912 result))))))
913
914
915(defun modify-face (&optional frame)
916 "Modify attributes of faces interactively.
917If optional argument FRAME is nil or omitted, modify the face used
918for newly created frame, i.e. the global face."
919 (interactive)
84fe35f0 920 (let ((face (read-face-name "Modify face ")))
5f5c8ee5
GM
921 (apply #'set-face-attribute face frame
922 (read-all-face-attributes face frame))))
923
924
925(defun read-face-and-attribute (attribute &optional frame)
926 "Read face name and face attribute value.
927ATTRIBUTE is the attribute whose new value is read.
928FRAME nil or unspecified means read attribute value of global face.
929Value is a list (FACE NEW-VALUE) where FACE is the face read
930(a symbol), and NEW-VALUE is value read."
931 (cond ((eq attribute :font)
84fe35f0 932 (let* ((prompt (format "Set font-related attributes of face "))
5f5c8ee5
GM
933 (face (read-face-name prompt))
934 (font (read-face-font face frame)))
935 (list face font)))
936 (t
937 (let* ((attribute-name (face-descriptive-attribute-name attribute))
84fe35f0 938 (prompt (format "Set %s of face " attribute-name))
5f5c8ee5
GM
939 (face (read-face-name prompt))
940 (new-value (read-face-attribute face attribute frame)))
941 (list face new-value)))))
942
943
944\f
945;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
946;;; Listing faces.
947;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
948
949(defvar list-faces-sample-text
950 "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
951 "*Text string to display as the sample text for `list-faces-display'.")
952
953
954;; The name list-faces would be more consistent, but let's avoid a
955;; conflict with Lucid, which uses that name differently.
956
957(defun list-faces-display ()
958 "List all faces, using the same sample text in each.
959The sample text is a string that comes from the variable
960`list-faces-sample-text'."
961 (interactive)
962 (let ((faces (sort (face-list) #'string-lessp))
963 (face nil)
964 (frame (selected-frame))
84fe35f0 965 disp-frame window face-name)
5f5c8ee5
GM
966 (with-output-to-temp-buffer "*Faces*"
967 (save-excursion
968 (set-buffer standard-output)
969 (setq truncate-lines t)
84fe35f0
DL
970 (insert
971 (substitute-command-keys
972 (concat
973 "Use "
53c80cf6 974 (if (display-mouse-p) "\\[help-follow-mouse] or ")
32e33f60 975 "\\[help-follow] on a face name to customize it\n"
84fe35f0
DL
976 "or on its sample text for a decription of the face.\n\n")))
977 (setq help-xref-stack nil)
5f5c8ee5
GM
978 (while faces
979 (setq face (car faces))
980 (setq faces (cdr faces))
84fe35f0
DL
981 (setq face-name (symbol-name face))
982 (insert (format "%25s " face-name))
983 ;; Hyperlink to a customization buffer for the face. Using
984 ;; the help xref mechanism may not be the best way.
985 (save-excursion
986 (save-match-data
987 (search-backward face-name)
eab5a18b
DL
988 (help-xref-button 0 #'customize-face face-name
989 "mouse-2: customize this face")))
5f5c8ee5
GM
990 (let ((beg (point)))
991 (insert list-faces-sample-text)
84fe35f0
DL
992 ;; Hyperlink to a help buffer for the face.
993 (save-excursion
994 (save-match-data
995 (search-backward list-faces-sample-text)
eab5a18b
DL
996 (help-xref-button 0 #'describe-face face
997 "mouse-2: describe this face")))
5f5c8ee5
GM
998 (insert "\n")
999 (put-text-property beg (1- (point)) 'face face)
1000 ;; If the sample text has multiple lines, line up all of them.
1001 (goto-char beg)
1002 (forward-line 1)
1003 (while (not (eobp))
1004 (insert " ")
1005 (forward-line 1))))
1006 (goto-char (point-min)))
1007 (print-help-return-message))
1008 ;; If the *Faces* buffer appears in a different frame,
1009 ;; copy all the face definitions from FRAME,
1010 ;; so that the display will reflect the frame that was selected.
1011 (setq window (get-buffer-window (get-buffer "*Faces*") t))
1012 (setq disp-frame (if window (window-frame window)
1013 (car (frame-list))))
1014 (or (eq frame disp-frame)
1015 (let ((faces (face-list)))
1016 (while faces
1017 (copy-face (car faces) (car faces) frame disp-frame)
1018 (setq faces (cdr faces)))))))
1019
1020
1021(defun describe-face (face &optional frame)
1022 "Display the properties of face FACE on FRAME.
1023If the optional argument FRAME is given, report on face FACE in that frame.
1024If FRAME is t, report on the defaults for face FACE (for new frames).
1025If FRAME is omitted or nil, use the selected frame."
84fe35f0 1026 (interactive (list (read-face-name "Describe face ")))
5f5c8ee5
GM
1027 (let* ((attrs '((:family . "Family")
1028 (:width . "Width")
1029 (:height . "Height")
1030 (:weight . "Weight")
1031 (:slant . "Slant")
1032 (:foreground . "Foreground")
1033 (:background . "Background")
1034 (:underline . "Underline")
1035 (:overline . "Overline")
1036 (:strike-through . "Strike-through")
1037 (:box . "Box")
1038 (:inverse-video . "Inverse")
1039 (:stipple . "Stipple")))
1040 (max-width (apply #'max (mapcar #'(lambda (x) (length (cdr x)))
1041 attrs))))
1042 (with-output-to-temp-buffer "*Help*"
1043 (save-excursion
1044 (set-buffer standard-output)
1045 (dolist (a attrs)
1046 (let ((attr (face-attribute face (car a) frame)))
1047 (insert (make-string (- max-width (length (cdr a))) ?\ )
1048 (cdr a) ": " (format "%s" attr) "\n")))
1049 (insert "\nDocumentation:\n\n"
1050 (or (face-documentation face)
1051 "not documented as a face.")))
1052 (print-help-return-message))))
1053
1054
1055
1056\f
1057;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1058;;; Face specifications (defface).
1059;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1060
1061;; Parameter FRAME Is kept for call compatibility to with previous
1062;; face implementation.
1063
1064(defun face-attr-construct (face &optional frame)
1065 "Return a defface-style attribute list for FACE on FRAME.
1066Value is a property list of pairs ATTRIBUTE VALUE for all specified
1067face attributes of FACE where ATTRIBUTE is the attribute name and
1068VALUE is the specified value of that attribute."
1069 (let (result)
1070 (dolist (entry face-attribute-name-alist result)
1071 (let* ((attribute (car entry))
1072 (value (face-attribute face attribute)))
1073 (unless (eq value 'unspecified)
1074 (setq result (nconc (list attribute value) result)))))))
1075
1076
1077(defun face-spec-set-match-display (display frame)
1078 "Non-nil if DISPLAY matches FRAME.
1079DISPLAY is part of a spec such as can be used in `defface'.
1080If FRAME is nil, the current FRAME is used."
1081 (let* ((conjuncts display)
1082 conjunct req options
1083 ;; t means we have succeeded against all the conjuncts in
1084 ;; DISPLAY that have been tested so far.
1085 (match t))
1086 (if (eq conjuncts t)
1087 (setq conjuncts nil))
1088 (while (and conjuncts match)
1089 (setq conjunct (car conjuncts)
1090 conjuncts (cdr conjuncts)
1091 req (car conjunct)
1092 options (cdr conjunct)
1093 match (cond ((eq req 'type)
1094 (or (memq window-system options)
1095 (and (null window-system)
ee716db5
GM
1096 (memq 'tty options))
1097 (and (memq 'motif options)
1098 (featurep 'motif))
1099 (and (memq 'lucid options)
1100 (featurep 'x-toolkit)
1101 (not (featurep 'motif)))
1102 (and (memq 'x-toolkit options)
1103 (featurep 'x-toolkit))))
5f5c8ee5
GM
1104 ((eq req 'class)
1105 (memq (frame-parameter frame 'display-type) options))
1106 ((eq req 'background)
1107 (memq (frame-parameter frame 'background-mode)
1108 options))
1109 (t (error "Unknown req `%S' with options `%S'"
1110 req options)))))
1111 match))
1112
1113
1114(defun face-spec-choose (spec &optional frame)
1115 "Choose the proper attributes for FRAME, out of SPEC."
1116 (unless frame
1117 (setq frame (selected-frame)))
1118 (let ((tail spec)
1119 result)
1120 (while tail
1121 (let* ((entry (car tail))
1122 (display (nth 0 entry))
1123 (attrs (nth 1 entry)))
1124 (setq tail (cdr tail))
1125 (when (face-spec-set-match-display display frame)
1126 (setq result attrs tail nil))))
1127 result))
1128
1129
1130(defun face-spec-reset-face (face &optional frame)
1131 "Reset all attributes of FACE on FRAME to unspecified."
1132 (let ((attrs face-attribute-name-alist)
1133 params)
1134 (while attrs
1135 (let ((attr-and-name (car attrs)))
1136 (setq params (cons (car attr-and-name) (cons 'unspecified params))))
1137 (setq attrs (cdr attrs)))
1138 (apply #'set-face-attribute face frame params)))
1139
1140
1141(defun face-spec-set (face spec &optional frame)
1142 "Set FACE's attributes according to the first matching entry in SPEC.
1143FRAME is the frame whose frame-local face is set. FRAME nil means
1144do it on all frames. See `defface' for information about SPEC."
1145 (let ((attrs (face-spec-choose spec frame))
1146 params)
1147 (while attrs
1148 (let ((attribute (car attrs))
1149 (value (car (cdr attrs))))
1150 ;; Support some old-style attribute names and values.
1151 (case attribute
1152 (:bold (setq attribute :weight value (if value 'bold 'normal)))
1153 (:italic (setq attribute :slant value (if value 'italic 'normal))))
1154 (setq params (cons attribute (cons value params))))
1155 (setq attrs (cdr (cdr attrs))))
1156 (face-spec-reset-face face frame)
1157 (apply #'set-face-attribute face frame params)))
1158
1159
1160(defun face-attr-match-p (face attrs &optional frame)
1161 "Value is non-nil if attributes of FACE match values in plist ATTRS.
1162Optional parameter FRAME is the frame whose definition of FACE
1163is used. If nil or omitted, use the selected frame."
1164 (unless frame
1165 (setq frame (selected-frame)))
1166 (let ((list face-attribute-name-alist)
1167 (match t))
1168 (while (and match (not (null list)))
1169 (let* ((attr (car (car list)))
1170 (specified-value (plist-get attrs attr))
1171 (value-now (face-attribute face attr frame)))
1172 (when specified-value
1173 (setq match (equal specified-value value-now)))
1174 (setq list (cdr list))))
1175 match))
1176
1177
1178(defun face-spec-match-p (face spec &optional frame)
1179 "Return t if FACE, on FRAME, matches what SPEC says it should look like."
1180 (face-attr-match-p face (face-spec-choose spec frame) frame))
1181
1182
1183\f
f795f633
EZ
1184;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1185;;; Frame-type independent color support.
1186;;; We keep the old x-* names as aliases for back-compatibility.
1187;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1188
1189(defun defined-colors (&optional frame)
1190 "Return a list of colors supported for a particular frame.
1191The argument FRAME specifies which frame to try.
1192The value may be different for frames on different display types.
1193If FRAME doesn't support colors, the value is nil."
1194 (if (memq (framep (or frame (selected-frame))) '(x w32))
1195 (xw-defined-colors frame)
37462f4c 1196 (mapcar 'car (tty-color-alist frame))))
f795f633
EZ
1197(defalias 'x-defined-colors 'defined-colors)
1198
1199(defun color-defined-p (color &optional frame)
1200 "Return non-nil if color COLOR is supported on frame FRAME.
1201If FRAME is omitted or nil, use the selected frame.
293b4814
EZ
1202If COLOR is the symbol `unspecified' or one of the strings
1203\"unspecified-fg\" or \"unspecified-bg\", the value is nil."
1204 (if (memq color '(unspecified "unspecified-bg" "unspecified-fg"))
f795f633
EZ
1205 nil
1206 (if (memq (framep (or frame (selected-frame))) '(x w32))
1207 (xw-color-defined-p color frame)
37462f4c 1208 (numberp (tty-color-translate color frame)))))
f795f633
EZ
1209(defalias 'x-color-defined-p 'color-defined-p)
1210
1211(defun color-values (color &optional frame)
1212 "Return a description of the color named COLOR on frame FRAME.
1213The value is a list of integer RGB values--\(RED GREEN BLUE\).
1214These values appear to range from 0 to 65280 or 65535, depending
1215on the system; white is \(65280 65280 65280\) or \(65535 65535 65535\).
1216If FRAME is omitted or nil, use the selected frame.
1217If FRAME cannot display COLOR, the value is nil.
293b4814
EZ
1218If COLOR is the symbol `unspecified' or one of the strings
1219\"unspecified-fg\" or \"unspecified-bg\", the value is nil."
1220 (if (memq color '(unspecified "unspecified-fg" "unspecified-bg"))
f795f633
EZ
1221 nil
1222 (if (memq (framep (or frame (selected-frame))) '(x w32))
1223 (xw-color-values color frame)
1224 (tty-color-values color frame))))
1225(defalias 'x-color-values 'color-values)
1226
1227(defun display-color-p (&optional display)
1228 "Return t if DISPLAY supports color.
1229The optional argument DISPLAY specifies which display to ask about.
1230DISPLAY should be either a frame or a display name (a string).
1231If omitted or nil, that stands for the selected frame's display."
d5179a01
EZ
1232 (if (memq (framep-on-display display) '(x w32))
1233 (xw-display-color-p display)
1234 (tty-display-color-p display)))
f795f633
EZ
1235(defalias 'x-display-color-p 'display-color-p)
1236
d5179a01
EZ
1237(defun display-grayscale-p (&optional display)
1238 "Return non-nil if frames on DISPLAY can display shades of gray."
1239 (let ((frame-type (framep-on-display display)))
1240 (cond
1241 ((memq frame-type '(x w32 mac))
1242 (x-display-grayscale-p display))
1243 (t
1244 (> (tty-color-gray-shades display) 2)))))
1245
f795f633 1246\f
5f5c8ee5
GM
1247;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1248;;; Background mode.
1249;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1250
1251(defcustom frame-background-mode nil
1252 "*The brightness of the background.
1253Set this to the symbol `dark' if your background color is dark, `light' if
1254your background is light, or nil (default) if you want Emacs to
1255examine the brightness for you."
1256 :group 'faces
1257 :set #'(lambda (var value)
1258 (set var value)
1259 (mapcar 'frame-set-background-mode (frame-list)))
1260 :initialize 'custom-initialize-changed
1261 :type '(choice (choice-item dark)
1262 (choice-item light)
1263 (choice-item :tag "default" nil)))
1264
1265
1266(defun frame-set-background-mode (frame)
1267 "Set up the `background-mode' and `display-type' frame parameters for FRAME."
1268 (let* ((bg-resource
1269 (and window-system
1270 (x-get-resource ".backgroundMode" "BackgroundMode")))
1271 (params (frame-parameters frame))
1272 (bg-mode (cond (frame-background-mode)
1273 ((null window-system)
1274 ;; No way to determine this automatically (?).
1275 'dark)
1276 (bg-resource
1277 (intern (downcase bg-resource)))
1278 ((< (apply '+ (x-color-values
1279 (cdr (assq 'background-color
1280 params))
1281 frame))
1282 ;; Just looking at the screen, colors whose
1283 ;; values add up to .6 of the white total
1284 ;; still look dark to me.
1285 (* (apply '+ (x-color-values "white" frame)) .6))
1286 'dark)
1287 (t 'light)))
1288 (display-type (cond ((null window-system)
37462f4c 1289 (if (tty-display-color-p frame) 'color 'mono))
5f5c8ee5
GM
1290 ((x-display-color-p frame)
1291 'color)
1292 ((x-display-grayscale-p frame)
1293 'grayscale)
1294 (t 'mono))))
1295 (modify-frame-parameters frame
1296 (list (cons 'background-mode bg-mode)
1297 (cons 'display-type display-type))))
1298
1299 ;; For all named faces, choose face specs matching the new frame
1300 ;; parameters.
1301 (let ((face-list (face-list)))
1302 (while face-list
1303 (let* ((face (car face-list))
1304 (spec (get face 'face-defface-spec)))
1305 (when spec
1306 (face-spec-set face spec frame))
1307 (setq face-list (cdr face-list))))))
1308
1309
1310
1311\f
1312;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1313;;; Frame creation.
1314;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1315
1316(defun x-handle-named-frame-geometry (parameters)
1317 "Add geometry parameters for a named frame to parameter list PARAMETERS.
1318Value is the new parameter list."
1319 (let* ((name (or (cdr (assq 'name parameters))
1320 (cdr (assq 'name default-frame-alist))))
1321 (x-resource-name name)
1322 (res-geometry (if name (x-get-resource "geometry" "Geometry"))))
1323 (when res-geometry
1324 (let ((parsed (x-parse-geometry res-geometry)))
1325 ;; If the resource specifies a position, call the position
1326 ;; and size "user-specified".
1327 (when (or (assq 'top parsed)
1328 (assq 'left parsed))
1329 (setq parsed (append '((user-position . t) (user-size . t)) parsed)))
1330 ;; Put the geometry parameters at the end. Copy
1331 ;; default-frame-alist so that they go after it.
1332 (setq parameters (append parameters default-frame-alist parsed))))
1333 parameters))
1334
1335
1336(defun x-handle-reverse-video (frame parameters)
1337 "Handle the reverse-video frame parameter and X resource.
1338`x-create-frame' does not handle this one."
1339 (when (cdr (or (assq 'reverse parameters)
1340 (assq 'reverse default-frame-alist)
1341 (let ((resource (x-get-resource "reverseVideo"
1342 "ReverseVideo")))
1343 (if resource
1344 (cons nil (member (downcase resource)
1345 '("on" "true")))))))
1346 (let* ((params (frame-parameters frame))
1347 (bg (cdr (assq 'foreground-color params)))
1348 (fg (cdr (assq 'background-color params))))
1349 (modify-frame-parameters frame
1350 (list (cons 'foreground-color fg)
1351 (cons 'background-color bg)))
1352 (if (equal bg (cdr (assq 'border-color params)))
1353 (modify-frame-parameters frame
1354 (list (cons 'border-color fg))))
1355 (if (equal bg (cdr (assq 'mouse-color params)))
1356 (modify-frame-parameters frame
1357 (list (cons 'mouse-color fg))))
1358 (if (equal bg (cdr (assq 'cursor-color params)))
1359 (modify-frame-parameters frame
1360 (list (cons 'cursor-color fg)))))))
1361
1362
1363(defun x-create-frame-with-faces (&optional parameters)
1364 "Create a frame from optional frame parameters PARAMETERS.
1365Parameters not specified by PARAMETERS are taken from
1366`default-frame-alist'. If PARAMETERS specify a frame name,
1367handle X geometry resources for that name. If either PARAMETERS
1368or `default-frame-alist' contains a `reverse' parameter, or
1369the X resource ``reverseVideo'' is present, handle that.
1370Value is the new frame created."
1371 (setq parameters (x-handle-named-frame-geometry parameters))
1372 (let ((visibility-spec (assq 'visibility parameters))
1373 (frame-list (frame-list))
1374 (frame (x-create-frame (cons '(visibility . nil) parameters)))
1375 success)
1376 (unwind-protect
1377 (progn
1378 (x-handle-reverse-video frame parameters)
1379 (frame-set-background-mode frame)
1380 (face-set-after-frame-default frame)
1381 (if (or (null frame-list) (null visibility-spec))
1382 (make-frame-visible frame)
1383 (modify-frame-parameters frame (list visibility-spec)))
1384 (setq success t))
1385 (unless success
1386 (delete-frame frame)))
1387 frame))
1388
1389
1390(defun face-set-after-frame-default (frame)
f1cae29e
GM
1391 "Set frame-local faces of FRAME from face specs and resources.
1392Initialize colors of certain faces from frame parameters."
5f5c8ee5
GM
1393 (dolist (face (face-list))
1394 (let ((spec (or (get face 'saved-face)
1395 (get face 'face-defface-spec))))
1396 (when spec
1397 (face-spec-set face spec frame))
1398 (internal-merge-in-global-face face frame)
75014631 1399 (when (memq window-system '(x w32))
f1cae29e
GM
1400 (make-face-x-resource-internal face frame))))
1401
1402 ;; Initialize attributes from frame parameters.
1403 (let ((params '((foreground-color default :foreground)
1404 (background-color default :background)
1405 (border-color border :background)
1406 (cursor-color cursor :background)
1407 (scroll-bar-foreground scroll-bar :foreground)
1408 (scroll-bar-background scroll-bar :background)
1409 (mouse-color mouse :background))))
1410 (while params
1411 (let ((param-name (nth 0 (car params)))
1412 (face (nth 1 (car params)))
1413 (attr (nth 2 (car params)))
1414 value)
1415 (when (setq value (frame-parameter frame param-name))
1416 (set-face-attribute face frame attr value)))
1417 (setq params (cdr params)))))
5f5c8ee5
GM
1418
1419
1420(defun tty-create-frame-with-faces (&optional parameters)
1421 "Create a frame from optional frame parameters PARAMETERS.
1422Parameters not specified by PARAMETERS are taken from
1423`default-frame-alist'. If either PARAMETERS or `default-frame-alist'
1424contains a `reverse' parameter, handle that. Value is the new frame
1425created."
1426 (let ((frame (make-terminal-frame parameters))
1427 success)
1428 (unwind-protect
1429 (progn
1430 (frame-set-background-mode frame)
1431 (face-set-after-frame-default frame)
1432 (setq success t))
1433 (unless success
1434 (delete-frame frame)))
1435 frame))
465fceed 1436
465fceed 1437
5f5c8ee5
GM
1438;; Called from C function init_display to initialize faces of the
1439;; dumped terminal frame on startup.
465fceed 1440
5f5c8ee5
GM
1441(defun tty-set-up-initial-frame-faces ()
1442 (let ((frame (selected-frame)))
1443 (frame-set-background-mode frame)
1444 (face-set-after-frame-default frame)))
1445
465fceed 1446
465fceed 1447
5f5c8ee5
GM
1448\f
1449;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1450;;; Compatiblity with 20.2
1451;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
72418504 1452
5f5c8ee5 1453;; Update a frame's faces when we change its default font.
465fceed 1454
5f5c8ee5
GM
1455(defun frame-update-faces (frame)
1456 nil)
465fceed 1457
e8e4cda0 1458
5f5c8ee5
GM
1459;; Update the colors of FACE, after FRAME's own colors have been
1460;; changed.
1bcb155c 1461
5f5c8ee5
GM
1462(defun frame-update-face-colors (frame)
1463 (frame-set-background-mode frame))
d11fba25 1464
d11fba25 1465
bdda3754 1466\f
5f5c8ee5
GM
1467;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1468;;; Standard faces.
1469;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
465fceed 1470
5f5c8ee5
GM
1471(defgroup basic-faces nil
1472 "The standard faces of Emacs."
1473 :group 'faces)
ef436392 1474
465fceed 1475
5f5c8ee5
GM
1476(defface default
1477 '((t nil))
1478 "Basic default face."
1479 :group 'basic-faces)
465fceed 1480
72418504 1481
3759f14b 1482(defface mode-line
5f5c8ee5
GM
1483 '((((type x) (class color))
1484 (:box (:line-width 2 :style released-button) :background "grey75"))
3855860a
JR
1485 (((type w32) (class color))
1486 (:box (:line-width 2 :style released-button) :background "grey75"))
5f5c8ee5
GM
1487 (t
1488 (:inverse-video t)))
1489 "Basic mode line face."
a4391cf1 1490 :version "21.1"
84fe35f0 1491 :group 'modeline
5f5c8ee5 1492 :group 'basic-faces)
465fceed 1493
3759f14b
GM
1494;; Make `modeline' an alias for `mode-line', for compatibility.
1495(put 'modeline 'face-alias 'mode-line)
d11fba25 1496
e5e7779f 1497(defface header-line
5f5c8ee5
GM
1498 '((((type x) (class color))
1499 (:box (:line-width 2 :style released-button) :background "grey75"))
3855860a
JR
1500 (((type w32) (class color))
1501 (:box (:line-width 2 :style released-button) :background "grey75"))
5f5c8ee5
GM
1502 (t
1503 (:inverse-video t)))
e5e7779f 1504 "Basic header-line face."
a4391cf1 1505 :version "21.1"
5f5c8ee5 1506 :group 'basic-faces)
e8e4cda0 1507
d11fba25 1508
533322cd 1509(defface tool-bar
5f5c8ee5
GM
1510 '((((type x) (class color))
1511 (:box (:line-width 1 :style released-button) :background "grey75"))
9668a746
GM
1512 (((type x) (class mono))
1513 (:box (:line-width 1 :style released-button) :background "grey"))
3855860a
JR
1514 (((type w32) (class color))
1515 (:box (:line-width 1 :style released-button) :background "grey75"))
5f5c8ee5
GM
1516 (t
1517 ()))
533322cd 1518 "Basic tool-bar face."
a4391cf1 1519 :version "21.1"
5f5c8ee5 1520 :group 'basic-faces)
d11fba25 1521
bdda3754 1522
5f5c8ee5
GM
1523(defface region
1524 '((((type tty) (class color))
1525 (:background "blue" :foreground "white"))
1526 (((type tty) (class mono))
1527 (:inverse-video t))
1528 (((class color) (background dark))
1529 (:background "blue"))
1530 (((class color) (background light))
1531 (:background "lightblue"))
1532 (t (:background "gray")))
62f1e1cd 1533 "Basic face for highlighting the region."
5f5c8ee5 1534 :group 'basic-faces)
66a5c2c6 1535
bdda3754 1536
f1cae29e 1537(defface fringe
5f5c8ee5
GM
1538 '((((class color))
1539 (:background "grey95"))
3d089489
GM
1540 (t
1541 (:background "gray")))
f1cae29e
GM
1542 "Basic face for the fringes to the left and right of windows under X."
1543 :version "21.1"
84fe35f0 1544 :group 'frames
f1cae29e
GM
1545 :group 'basic-faces)
1546
1547
1548(defface scroll-bar '()
1549 "Basic face for the scroll bar colors under X."
1550 :version "21.1"
84fe35f0 1551 :group 'frames
f1cae29e
GM
1552 :group 'basic-faces)
1553
1554
ee716db5
GM
1555(defface menu
1556 '((((type x-toolkit)) ())
1557 (t (:inverse-video t)))
1558 "Basic menu face."
1559 :version "21.1"
84fe35f0 1560 :group 'menu
ee716db5
GM
1561 :group 'basic-faces)
1562
1563
f1cae29e
GM
1564(defface border '()
1565 "Basic face for the frame border under X."
1566 :version "21.1"
84fe35f0 1567 :group 'frames
f1cae29e
GM
1568 :group 'basic-faces)
1569
1570
1571(defface cursor '()
1572 "Basic face for the cursor color under X."
1573 :version "21.1"
84fe35f0 1574 :group 'cursor
f1cae29e
GM
1575 :group 'basic-faces)
1576
1577
1578(defface mouse '()
1579 "Basic face for the mouse color under X."
e59176e2 1580 :version "21.1"
84fe35f0 1581 :group 'mouse
5f5c8ee5 1582 :group 'basic-faces)
bdda3754 1583
bdda3754 1584
5f5c8ee5
GM
1585(defface bold '((t (:weight bold)))
1586 "Basic bold face."
1587 :group 'basic-faces)
bdda3754 1588
bdda3754 1589
5f5c8ee5
GM
1590(defface italic '((t (:slant italic)))
1591 "Basic italic font."
1592 :group 'basic-faces)
bdda3754 1593
bdda3754 1594
5f5c8ee5
GM
1595(defface bold-italic '((t (:weight bold :slant italic)))
1596 "Basic bold-italic face."
1597 :group 'basic-faces)
465fceed 1598
17b3a11b 1599
5f5c8ee5
GM
1600(defface underline '((t (:underline t)))
1601 "Basic underlined face."
1602 :group 'basic-faces)
465fceed 1603
2f2ddd1e 1604
5f5c8ee5
GM
1605(defface highlight
1606 '((((type tty) (class color))
1607 (:background "green"))
1608 (((class color) (background light))
1609 (:background "darkseagreen2"))
1610 (((class color) (background dark))
1611 (:background "darkolivegreen"))
1612 (t (:inverse-video t)))
a4391cf1
DL
1613 "Basic face for highlighting."
1614 :group 'basic-faces)
465fceed 1615
465fceed 1616
5f5c8ee5
GM
1617(defface secondary-selection
1618 '((((type tty) (class color))
1619 (:background "cyan"))
1620 (((class color) (background light))
230e947b 1621 (:background "yellow"))
5f5c8ee5 1622 (((class color) (background dark))
b261ffa4 1623 (:background "yellow"))
5f5c8ee5 1624 (t (:inverse-video t)))
a4391cf1
DL
1625 "Basic face for displaying the secondary selection."
1626 :group 'basic-faces)
465fceed 1627
93d6fcef 1628
3855860a 1629(defface fixed-pitch '((t (:family "courier")))
5f5c8ee5
GM
1630 "The basic fixed-pitch face."
1631 :group 'basic-faces)
465fceed 1632
5f5c8ee5 1633
3855860a 1634(defface variable-pitch '((t (:family "helv")))
5f5c8ee5
GM
1635 "The basic variable-pitch face."
1636 :group 'basic-faces)
1637
1638
1639(defface trailing-whitespace
1640 '((((class color) (background light))
1641 (:background "red"))
1642 (((class color) (background dark))
1643 (:background "red"))
1644 (t (:inverse-video t)))
a4391cf1
DL
1645 "Basic face for highlighting trailing whitespace."
1646 :version "21.1"
84fe35f0 1647 :group 'font-lock ; like `show-trailing-whitespace'
a4391cf1 1648 :group 'basic-faces)
465fceed
ER
1649
1650
465fceed 1651\f
5f5c8ee5
GM
1652;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1653;;; Manipulating font names.
1654;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1655
1656;; This is here for compatibilty with Emacs 20.2. For example,
1657;; international/fontset.el uses these functions to manipulate font
1658;; names. The following functions are not used in the face
1659;; implementation itself.
465fceed 1660
d7fa5aa2
RS
1661(defvar x-font-regexp nil)
1662(defvar x-font-regexp-head nil)
1663(defvar x-font-regexp-weight nil)
1664(defvar x-font-regexp-slant nil)
465fceed 1665
021ca129
KH
1666(defconst x-font-regexp-weight-subnum 1)
1667(defconst x-font-regexp-slant-subnum 2)
1668(defconst x-font-regexp-swidth-subnum 3)
1669(defconst x-font-regexp-adstyle-subnum 4)
1670
465fceed
ER
1671;;; Regexps matching font names in "Host Portable Character Representation."
1672;;;
1673(let ((- "[-?]")
1674 (foundry "[^-]+")
1675 (family "[^-]+")
1676 (weight "\\(bold\\|demibold\\|medium\\)") ; 1
1677; (weight\? "\\(\\*\\|bold\\|demibold\\|medium\\|\\)") ; 1
1678 (weight\? "\\([^-]*\\)") ; 1
1679 (slant "\\([ior]\\)") ; 2
1680; (slant\? "\\([ior?*]?\\)") ; 2
1681 (slant\? "\\([^-]?\\)") ; 2
1682; (swidth "\\(\\*\\|normal\\|semicondensed\\|\\)") ; 3
1683 (swidth "\\([^-]*\\)") ; 3
1684; (adstyle "\\(\\*\\|sans\\|\\)") ; 4
c8787b81 1685 (adstyle "\\([^-]*\\)") ; 4
465fceed
ER
1686 (pixelsize "[0-9]+")
1687 (pointsize "[0-9][0-9]+")
1688 (resx "[0-9][0-9]+")
1689 (resy "[0-9][0-9]+")
1690 (spacing "[cmp?*]")
1691 (avgwidth "[0-9]+")
1692 (registry "[^-]+")
1693 (encoding "[^-]+")
1694 )
1695 (setq x-font-regexp
1696 (concat "\\`\\*?[-?*]"
1697 foundry - family - weight\? - slant\? - swidth - adstyle -
e20d641f
RS
1698 pixelsize - pointsize - resx - resy - spacing - avgwidth -
1699 registry - encoding "\\*?\\'"
465fceed
ER
1700 ))
1701 (setq x-font-regexp-head
1702 (concat "\\`[-?*]" foundry - family - weight\? - slant\?
1703 "\\([-*?]\\|\\'\\)"))
1704 (setq x-font-regexp-slant (concat - slant -))
1705 (setq x-font-regexp-weight (concat - weight -))
1706 nil)
1707
5f5c8ee5 1708
281dc1c2
JB
1709(defun x-resolve-font-name (pattern &optional face frame)
1710 "Return a font name matching PATTERN.
1711All wildcards in PATTERN become substantiated.
10d89673
JB
1712If PATTERN is nil, return the name of the frame's base font, which never
1713contains wildcards.
e17dbede
RS
1714Given optional arguments FACE and FRAME, return a font which is
1715also the same size as FACE on FRAME, or fail."
14e6867c
RS
1716 (or (symbolp face)
1717 (setq face (face-name face)))
1718 (and (eq frame t)
1719 (setq frame nil))
10d89673 1720 (if pattern
8db93e45 1721 ;; Note that x-list-fonts has code to handle a face with nil as its font.
abd89b80 1722 (let ((fonts (x-list-fonts pattern face frame 1)))
10d89673
JB
1723 (or fonts
1724 (if face
962a60aa
RS
1725 (if (string-match "\\*" pattern)
1726 (if (null (face-font face))
1727 (error "No matching fonts are the same height as the frame default font")
1728 (error "No matching fonts are the same height as face `%s'" face))
1729 (if (null (face-font face))
1730 (error "Height of font `%s' doesn't match the frame default font"
1731 pattern)
1732 (error "Height of font `%s' doesn't match face `%s'"
1733 pattern face)))
7cf6fac1 1734 (error "No fonts match `%s'" pattern)))
10d89673
JB
1735 (car fonts))
1736 (cdr (assq 'font (frame-parameters (selected-frame))))))
281dc1c2 1737
5f5c8ee5 1738
465fceed 1739(defun x-frob-font-weight (font which)
b7ffee5f
SM
1740 (let ((case-fold-search t))
1741 (cond ((string-match x-font-regexp font)
1742 (concat (substring font 0
1743 (match-beginning x-font-regexp-weight-subnum))
1744 which
1745 (substring font (match-end x-font-regexp-weight-subnum)
1746 (match-beginning x-font-regexp-adstyle-subnum))
1747 ;; Replace the ADD_STYLE_NAME field with *
1748 ;; because the info in it may not be the same
1749 ;; for related fonts.
1750 "*"
1751 (substring font (match-end x-font-regexp-adstyle-subnum))))
528fbf51
RS
1752 ((string-match x-font-regexp-head font)
1753 (concat (substring font 0 (match-beginning 1)) which
1754 (substring font (match-end 1))))
1755 ((string-match x-font-regexp-weight font)
b7ffee5f
SM
1756 (concat (substring font 0 (match-beginning 1)) which
1757 (substring font (match-end 1)))))))
465fceed 1758
5f5c8ee5 1759
465fceed 1760(defun x-frob-font-slant (font which)
b7ffee5f
SM
1761 (let ((case-fold-search t))
1762 (cond ((string-match x-font-regexp font)
1763 (concat (substring font 0
1764 (match-beginning x-font-regexp-slant-subnum))
1765 which
1766 (substring font (match-end x-font-regexp-slant-subnum)
1767 (match-beginning x-font-regexp-adstyle-subnum))
1768 ;; Replace the ADD_STYLE_NAME field with *
1769 ;; because the info in it may not be the same
1770 ;; for related fonts.
1771 "*"
1772 (substring font (match-end x-font-regexp-adstyle-subnum))))
528fbf51
RS
1773 ((string-match x-font-regexp-head font)
1774 (concat (substring font 0 (match-beginning 2)) which
1775 (substring font (match-end 2))))
1776 ((string-match x-font-regexp-slant font)
b7ffee5f
SM
1777 (concat (substring font 0 (match-beginning 1)) which
1778 (substring font (match-end 1)))))))
465fceed 1779
5f5c8ee5 1780
465fceed 1781(defun x-make-font-bold (font)
f3f31ccf
RS
1782 "Given an X font specification, make a bold version of it.
1783If that can't be done, return nil."
465fceed
ER
1784 (x-frob-font-weight font "bold"))
1785
5f5c8ee5 1786
465fceed 1787(defun x-make-font-demibold (font)
f3f31ccf
RS
1788 "Given an X font specification, make a demibold version of it.
1789If that can't be done, return nil."
465fceed
ER
1790 (x-frob-font-weight font "demibold"))
1791
5f5c8ee5 1792
465fceed 1793(defun x-make-font-unbold (font)
f3f31ccf
RS
1794 "Given an X font specification, make a non-bold version of it.
1795If that can't be done, return nil."
465fceed
ER
1796 (x-frob-font-weight font "medium"))
1797
5f5c8ee5 1798
465fceed 1799(defun x-make-font-italic (font)
f3f31ccf
RS
1800 "Given an X font specification, make an italic version of it.
1801If that can't be done, return nil."
465fceed
ER
1802 (x-frob-font-slant font "i"))
1803
5f5c8ee5 1804
465fceed 1805(defun x-make-font-oblique (font) ; you say tomayto...
f3f31ccf
RS
1806 "Given an X font specification, make an oblique version of it.
1807If that can't be done, return nil."
465fceed
ER
1808 (x-frob-font-slant font "o"))
1809
5f5c8ee5 1810
465fceed 1811(defun x-make-font-unitalic (font)
f3f31ccf
RS
1812 "Given an X font specification, make a non-italic version of it.
1813If that can't be done, return nil."
465fceed 1814 (x-frob-font-slant font "r"))
b7d7285c 1815
5f5c8ee5 1816
b7d7285c
KH
1817(defun x-make-font-bold-italic (font)
1818 "Given an X font specification, make a bold and italic version of it.
1819If that can't be done, return nil."
1820 (and (setq font (x-make-font-bold font))
1821 (x-make-font-italic font)))
113ab303 1822
465fceed 1823
f0138172
JB
1824(provide 'faces)
1825
5f5c8ee5 1826;;; end of faces.el