Make --without-x compatible with --enable-gcc-warnings.
[bpt/emacs.git] / src / xfaces.c
CommitLineData
82641697 1/* xfaces.c -- "Face" primitives.
e9bffc61 2
ab422c4d 3Copyright (C) 1993-1994, 1998-2013 Free Software Foundation, Inc.
7b7739b1 4
c115973b
JB
5This file is part of GNU Emacs.
6
9ec0b715 7GNU Emacs is free software: you can redistribute it and/or modify
c115973b 8it under the terms of the GNU General Public License as published by
9ec0b715
GM
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
c115973b
JB
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
9ec0b715 18along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
c115973b 19
82641697
GM
20/* New face implementation by Gerd Moellmann <gerd@gnu.org>. */
21
22/* Faces.
23
24 When using Emacs with X, the display style of characters can be
25 changed by defining `faces'. Each face can specify the following
26 display attributes:
27
39506348 28 1. Font family name.
178c5d9c 29
40a8bdf6 30 2. Font foundry name.
53aaf1e2
KH
31
32 3. Relative proportionate width, aka character set width or set
82641697 33 width (swidth), e.g. `semi-compressed'.
178c5d9c 34
53aaf1e2 35 4. Font height in 1/10pt.
178c5d9c 36
53aaf1e2 37 5. Font weight, e.g. `bold'.
178c5d9c 38
53aaf1e2 39 6. Font slant, e.g. `italic'.
178c5d9c 40
53aaf1e2 41 7. Foreground color.
178c5d9c 42
53aaf1e2 43 8. Background color.
82641697 44
53aaf1e2 45 9. Whether or not characters should be underlined, and in what color.
82641697 46
53aaf1e2 47 10. Whether or not characters should be displayed in inverse video.
82641697 48
53aaf1e2 49 11. A background stipple, a bitmap.
82641697 50
53aaf1e2 51 12. Whether or not characters should be overlined, and in what color.
82641697 52
53aaf1e2 53 13. Whether or not characters should be strike-through, and in what
82641697
GM
54 color.
55
53aaf1e2 56 14. Whether or not a box should be drawn around characters, the box
82641697
GM
57 type, and, for simple boxes, in what color.
58
53aaf1e2 59 15. Font-spec, or nil. This is a special attribute.
2dee4c0b
KH
60
61 A font-spec is a collection of font attributes (specs).
62
63 When this attribute is specified, the face uses a font matching
64 with the specs as is except for what overwritten by the specs in
65 the fontset (see below). In addition, the other font-related
66 attributes (1st thru 5th) are updated from the spec.
67
39506348 68 On the other hand, if one of the other font-related attributes are
e4769531 69 specified, the corresponding specs in this attribute is set to nil.
39506348 70
2c20458f
MB
71 15. A face name or list of face names from which to inherit attributes.
72
a08332c0
GM
73 16. A specified average font width, which is invisible from Lisp,
74 and is used to ensure that a font specified on the command line,
75 for example, can be matched exactly.
76
2dee4c0b
KH
77 17. A fontset name. This is another special attribute.
78
79 A fontset is a mappings from characters to font-specs, and the
80 specs overwrite the font-spec in the 14th attribute.
81
763bc839 82
82641697
GM
83 Faces are frame-local by nature because Emacs allows to define the
84 same named face (face names are symbols) differently for different
85 frames. Each frame has an alist of face definitions for all named
86 faces. The value of a named face in such an alist is a Lisp vector
39506348
KH
87 with the symbol `face' in slot 0, and a slot for each of the face
88 attributes mentioned above.
82641697
GM
89
90 There is also a global face alist `Vface_new_frame_defaults'. Face
91 definitions from this list are used to initialize faces of newly
92 created frames.
178c5d9c 93
82641697 94 A face doesn't have to specify all attributes. Those not specified
39506348
KH
95 have a value of `unspecified'. Faces specifying all attributes but
96 the 14th are called `fully-specified'.
82641697
GM
97
98
99 Face merging.
100
101 The display style of a given character in the text is determined by
102 combining several faces. This process is called `face merging'.
103 Any aspect of the display style that isn't specified by overlays or
104 text properties is taken from the `default' face. Since it is made
105 sure that the default face is always fully-specified, face merging
106 always results in a fully-specified face.
107
108
109 Face realization.
178c5d9c 110
82641697
GM
111 After all face attributes for a character have been determined by
112 merging faces of that character, that face is `realized'. The
113 realization process maps face attributes to what is physically
114 available on the system where Emacs runs. The result is a
bad98418 115 `realized face' in the form of a struct face which is stored in the
82641697
GM
116 face cache of the frame on which it was realized.
117
39506348
KH
118 Face realization is done in the context of the character to display
119 because different fonts may be used for different characters. In
120 other words, for characters that have different font
121 specifications, different realized faces are needed to display
82641697
GM
122 them.
123
39506348
KH
124 Font specification is done by fontsets. See the comment in
125 fontset.c for the details. In the current implementation, all ASCII
126 characters share the same font in a fontset.
127
128 Faces are at first realized for ASCII characters, and, at that
129 time, assigned a specific realized fontset. Hereafter, we call
130 such a face as `ASCII face'. When a face for a multibyte character
131 is realized, it inherits (thus shares) a fontset of an ASCII face
132 that has the same attributes other than font-related ones.
133
763bc839 134 Thus, all realized faces have a realized fontset.
82641697
GM
135
136
137 Unibyte text.
138
39506348
KH
139 Unibyte text (i.e. raw 8-bit characters) is displayed with the same
140 font as ASCII characters. That is because it is expected that
141 unibyte text users specify a font that is suitable both for ASCII
142 and raw 8-bit characters.
143
82641697
GM
144
145 Font selection.
146
147 Font selection tries to find the best available matching font for a
39506348 148 given (character, face) combination.
82641697 149
39506348
KH
150 If the face specifies a fontset name, that fontset determines a
151 pattern for fonts of the given character. If the face specifies a
152 font name or the other font-related attributes, a fontset is
153 realized from the default fontset. In that case, that
154 specification determines a pattern for ASCII characters and the
155 default fontset determines a pattern for multibyte characters.
82641697
GM
156
157 Available fonts on the system on which Emacs runs are then matched
158 against the font pattern. The result of font selection is the best
159 match for the given face attributes in this font list.
160
161 Font selection can be influenced by the user.
162
163 1. The user can specify the relative importance he gives the face
164 attributes width, height, weight, and slant by setting
165 face-font-selection-order (faces.el) to a list of face attribute
166 names. The default is '(:width :height :weight :slant), and means
167 that font selection first tries to find a good match for the font
168 width specified by a face, then---within fonts with that
169 width---tries to find a best match for the specified font height,
170 etc.
171
c824bfbc 172 2. Setting face-font-family-alternatives allows the user to
82641697
GM
173 specify alternative font families to try if a family specified by a
174 face doesn't exist.
175
c824bfbc
KH
176 3. Setting face-font-registry-alternatives allows the user to
177 specify all alternative font registries to try for a face
178 specifying a registry.
179
180 4. Setting face-ignored-fonts allows the user to ignore specific
181 fonts.
182
82641697 183
ec7a10e3 184 Character composition.
39506348
KH
185
186 Usually, the realization process is already finished when Emacs
187 actually reflects the desired glyph matrix on the screen. However,
188 on displaying a composition (sequence of characters to be composed
189 on the screen), a suitable font for the components of the
190 composition is selected and realized while drawing them on the
191 screen, i.e. the realization process is delayed but in principle
192 the same.
82641697 193
178c5d9c 194
82641697
GM
195 Initialization of basic faces.
196
197 The faces `default', `modeline' are considered `basic faces'.
198 When redisplay happens the first time for a newly created frame,
199 basic faces are realized for CHARSET_ASCII. Frame parameters are
200 used to fill in unspecified attributes of the default face. */
201
68c45bf0 202#include <config.h>
406af475 203#include "sysstdio.h"
c115973b
JB
204#include <sys/types.h>
205#include <sys/stat.h>
7ee72033 206
c115973b 207#include "lisp.h"
81b39386 208#include "character.h"
a8517066 209#include "charset.h"
9763806e 210#include "keyboard.h"
b5c53576 211#include "frame.h"
428a555e 212#include "termhooks.h"
b5c53576 213
87485d6f 214#ifdef HAVE_X_WINDOWS
c115973b 215#include "xterm.h"
c7ae3284
GM
216#ifdef USE_MOTIF
217#include <Xm/Xm.h>
218#include <Xm/XmStrDefs.h>
219#endif /* USE_MOTIF */
d12d0a9b 220#endif /* HAVE_X_WINDOWS */
82641697 221
87485d6f
MW
222#ifdef MSDOS
223#include "dosfns.h"
224#endif
82641697 225
17a2cbbd
DC
226#ifdef HAVE_WINDOW_SYSTEM
227#include TERM_HEADER
c3cee013 228#include "fontset.h"
0fda9b75 229#ifdef HAVE_NTGUI
9763806e 230#undef FRAME_X_DISPLAY_INFO
c3cee013
JR
231#define FRAME_X_DISPLAY_INFO FRAME_W32_DISPLAY_INFO
232#define x_display_info w32_display_info
c3cee013 233#define GCGraphicsExposures 0
0fda9b75 234#endif /* HAVE_NTGUI */
c3cee013 235
edfda783 236#ifdef HAVE_NS
edfda783
AR
237#undef FRAME_X_DISPLAY_INFO
238#define FRAME_X_DISPLAY_INFO FRAME_NS_DISPLAY_INFO
edfda783
AR
239#define GCGraphicsExposures 0
240#endif /* HAVE_NS */
257b3b03
DA
241
242/* Number of pt per inch (from the TeXbook). */
243
244#define PT_PER_INCH 72.27
245
17a2cbbd 246#endif /* HAVE_WINDOW_SYSTEM */
edfda783 247
c115973b 248#include "buffer.h"
f211082d 249#include "dispextern.h"
357f32fc 250#include "blockinput.h"
b6d40e46 251#include "window.h"
bde7c500 252#include "intervals.h"
28d440ab 253#include "termchar.h"
c115973b 254
426b2119 255#include "font.h"
426b2119 256
87485d6f 257#ifdef HAVE_X_WINDOWS
82641697
GM
258
259/* Compensate for a bug in Xos.h on some systems, on which it requires
657070ac
JB
260 time.h. On some such systems, Xos.h tries to redefine struct
261 timeval and struct timezone if USG is #defined while it is
262 #included. */
657070ac 263
82641697 264#ifdef XOS_NEEDS_TIME_H
e11d186d 265#include <time.h>
657070ac
JB
266#undef USG
267#include <X11/Xos.h>
268#define USG
e11d186d 269#define __TIMEVAL__
875975e9
PE
270#if defined USG || defined __TIMEVAL__ /* Don't warn about unused macros. */
271#endif
82641697
GM
272#else /* not XOS_NEEDS_TIME_H */
273#include <X11/Xos.h>
274#endif /* not XOS_NEEDS_TIME_H */
e11d186d 275
82641697 276#endif /* HAVE_X_WINDOWS */
7a4d2269 277
620f13b0 278#include <c-ctype.h>
c115973b 279
82641697
GM
280/* Non-zero if face attribute ATTR is unspecified. */
281
282#define UNSPECIFIEDP(ATTR) EQ ((ATTR), Qunspecified)
283
2ff10663
CY
284/* Non-zero if face attribute ATTR is `ignore-defface'. */
285
a3720aa2 286#define IGNORE_DEFFACE_P(ATTR) EQ ((ATTR), QCignore_defface)
2ff10663 287
82641697
GM
288/* Value is the number of elements of VECTOR. */
289
290#define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
291
178c5d9c 292/* Size of hash table of realized faces in face caches (should be a
82641697
GM
293 prime number). */
294
295#define FACE_CACHE_BUCKETS_SIZE 1001
296
297/* Keyword symbols used for face attribute names. */
298
955cbe7b
PE
299Lisp_Object QCfamily, QCheight, QCweight, QCslant;
300static Lisp_Object QCunderline;
301static Lisp_Object QCinverse_video, QCstipple;
302Lisp_Object QCforeground, QCbackground;
303Lisp_Object QCwidth;
304static Lisp_Object QCfont, QCbold, QCitalic;
305static Lisp_Object QCreverse_video;
306static Lisp_Object QCoverline, QCstrike_through, QCbox, QCinherit;
307static Lisp_Object QCfontset;
82641697
GM
308
309/* Symbols used for attribute values. */
310
955cbe7b 311Lisp_Object Qnormal;
cc39a9db 312Lisp_Object Qbold;
9b0e3eba 313static Lisp_Object Qline, Qwave;
a411ac43 314Lisp_Object Qextra_light, Qlight;
f2045622 315Lisp_Object Qsemi_light, Qsemi_bold, Qextra_bold, Qultra_bold;
a411ac43 316Lisp_Object Qoblique;
cc39a9db 317Lisp_Object Qitalic;
955cbe7b
PE
318static Lisp_Object Qreleased_button, Qpressed_button;
319static Lisp_Object QCstyle, QCcolor, QCline_width;
1b2de274 320Lisp_Object Qunspecified; /* used in dosfns.c */
a3720aa2 321static Lisp_Object QCignore_defface;
ef917393
EZ
322
323char unspecified_fg[] = "unspecified-fg", unspecified_bg[] = "unspecified-bg";
82641697 324
92610620 325/* The name of the function to call when the background of the frame
c20577bc 326 has changed, frame_set_background_mode. */
92610620 327
19d5c50c 328static Lisp_Object Qframe_set_background_mode;
92610620 329
82641697
GM
330/* Names of basic faces. */
331
955cbe7b
PE
332Lisp_Object Qdefault, Qtool_bar, Qfringe;
333static Lisp_Object Qregion;
334Lisp_Object Qheader_line, Qscroll_bar, Qcursor;
335static Lisp_Object Qborder, Qmouse, Qmenu;
336Lisp_Object Qmode_line_inactive;
337static Lisp_Object Qvertical_border;
8bd201d6 338
92610620
GM
339/* The symbol `face-alias'. A symbols having that property is an
340 alias for another face. Value of the property is the name of
341 the aliased face. */
342
955cbe7b 343static Lisp_Object Qface_alias;
92610620 344
82641697
GM
345/* Alist of alternative font families. Each element is of the form
346 (FAMILY FAMILY1 FAMILY2 ...). If fonts of FAMILY can't be loaded,
347 try FAMILY1, then FAMILY2, ... */
348
349Lisp_Object Vface_alternative_font_family_alist;
350
32fcc231
GM
351/* Alist of alternative font registries. Each element is of the form
352 (REGISTRY REGISTRY1 REGISTRY2...). If fonts of REGISTRY can't be
353 loaded, try REGISTRY1, then REGISTRY2, ... */
354
355Lisp_Object Vface_alternative_font_registry_alist;
356
82641697
GM
357/* Allowed scalable fonts. A value of nil means don't allow any
358 scalable fonts. A value of t means allow the use of any scalable
359 font. Otherwise, value must be a list of regular expressions. A
360 font may be scaled if its name matches a regular expression in the
361 list. */
362
955cbe7b 363static Lisp_Object Qscalable_fonts_allowed;
c824bfbc 364
82641697
GM
365/* The symbols `foreground-color' and `background-color' which can be
366 used as part of a `face' property. This is for compatibility with
367 Emacs 20.2. */
368
369Lisp_Object Qforeground_color, Qbackground_color;
370
371/* The symbols `face' and `mouse-face' used as text properties. */
7b7739b1 372
ff83dbb1 373Lisp_Object Qface;
82641697 374
46b00436
KS
375/* Property for basic faces which other faces cannot inherit. */
376
955cbe7b 377static Lisp_Object Qface_no_inherit;
46b00436 378
82641697
GM
379/* Error symbol for wrong_type_argument in load_pixmap. */
380
955cbe7b 381static Lisp_Object Qbitmap_spec_p;
f211082d 382
82641697 383/* The next ID to assign to Lisp faces. */
cb637678 384
82641697 385static int next_lface_id;
c115973b 386
82641697 387/* A vector mapping Lisp face Id's to face names. */
c115973b 388
82641697 389static Lisp_Object *lface_id_to_name;
0065d054 390static ptrdiff_t lface_id_to_name_size;
c115973b 391
ae4b4ba5
GM
392/* TTY color-related functions (defined in tty-colors.el). */
393
955cbe7b 394static Lisp_Object Qtty_color_desc, Qtty_color_by_index, Qtty_color_standard_values;
82641697 395
ae4b4ba5
GM
396/* The name of the function used to compute colors on TTYs. */
397
955cbe7b 398static Lisp_Object Qtty_color_alist;
ae4b4ba5 399
257b3b03
DA
400#ifdef HAVE_WINDOW_SYSTEM
401
82641697
GM
402/* Counter for calls to clear_face_cache. If this counter reaches
403 CLEAR_FONT_TABLE_COUNT, and a frame has more than
404 CLEAR_FONT_TABLE_NFONTS load, unused fonts are freed. */
405
406static int clear_font_table_count;
407#define CLEAR_FONT_TABLE_COUNT 100
408#define CLEAR_FONT_TABLE_NFONTS 10
409
257b3b03
DA
410#endif /* HAVE_WINDOW_SYSTEM */
411
82641697
GM
412/* Non-zero means face attributes have been changed since the last
413 redisplay. Used in redisplay_internal. */
414
415int face_change_count;
416
a4a76b61
GM
417/* Non-zero means don't display bold text if a face's foreground
418 and background colors are the inverse of the default colors of the
419 display. This is a kluge to suppress `bold black' foreground text
420 which is hard to read on an LCD monitor. */
421
435f4c28 422static int tty_suppress_bold_inverse_default_colors_p;
a4a76b61 423
dbc968b8
GM
424/* A list of the form `((x . y))' used to avoid consing in
425 Finternal_set_lisp_face_attribute. */
426
427static Lisp_Object Vparam_value_alist;
428
82641697
GM
429/* The total number of colors currently allocated. */
430
e509cfa6 431#ifdef GLYPH_DEBUG
82641697
GM
432static int ncolors_allocated;
433static int npixmaps_allocated;
434static int ngcs;
435#endif
436
ceeda019
GM
437/* Non-zero means the definition of the `menu' face for new frames has
438 been changed. */
439
435f4c28 440static int menu_face_changed_default;
82641697 441
a0a23346 442struct named_merge_point;
82641697 443
f57e2426 444static struct face *realize_face (struct face_cache *, Lisp_Object *,
b5f03016 445 int);
f57e2426
J
446static struct face *realize_x_face (struct face_cache *, Lisp_Object *);
447static struct face *realize_tty_face (struct face_cache *, Lisp_Object *);
d7e6881a
DA
448static bool realize_basic_faces (struct frame *);
449static bool realize_default_face (struct frame *);
f57e2426 450static void realize_named_face (struct frame *, Lisp_Object, int);
f57e2426 451static struct face_cache *make_face_cache (struct frame *);
f57e2426 452static void free_face_cache (struct face_cache *);
f57e2426 453static int merge_face_ref (struct frame *, Lisp_Object, Lisp_Object *,
b5f03016 454 int, struct named_merge_point *);
c115973b 455
257b3b03
DA
456#ifdef HAVE_WINDOW_SYSTEM
457static void set_font_frame_param (Lisp_Object, Lisp_Object);
458static void clear_face_gcs (struct face_cache *);
459static struct face *realize_non_ascii_face (struct frame *, Lisp_Object,
460 struct face *);
461#endif /* HAVE_WINDOW_SYSTEM */
462
82641697
GM
463/***********************************************************************
464 Utilities
465 ***********************************************************************/
c115973b 466
87485d6f 467#ifdef HAVE_X_WINDOWS
cb637678 468
a435fc2a
GM
469#ifdef DEBUG_X_COLORS
470
471/* The following is a poor mans infrastructure for debugging X color
472 allocation problems on displays with PseudoColor-8. Some X servers
473 like 3.3.5 XF86_SVGA with Matrox cards apparently don't implement
474 color reference counts completely so that they don't signal an
475 error when a color is freed whose reference count is already 0.
476 Other X servers do. To help me debug this, the following code
477 implements a simple reference counting schema of its own, for a
478 single display/screen. --gerd. */
479
480/* Reference counts for pixel colors. */
481
482int color_count[256];
483
484/* Register color PIXEL as allocated. */
485
486void
1dae0f0a 487register_color (unsigned long pixel)
a435fc2a 488{
a54e2c05 489 eassert (pixel < 256);
a435fc2a
GM
490 ++color_count[pixel];
491}
492
493
494/* Register color PIXEL as deallocated. */
495
496void
1dae0f0a 497unregister_color (unsigned long pixel)
a435fc2a 498{
a54e2c05 499 eassert (pixel < 256);
a435fc2a
GM
500 if (color_count[pixel] > 0)
501 --color_count[pixel];
502 else
1088b922 503 emacs_abort ();
a435fc2a
GM
504}
505
506
507/* Register N colors from PIXELS as deallocated. */
508
509void
1dae0f0a 510unregister_colors (unsigned long *pixels, int n)
a435fc2a
GM
511{
512 int i;
513 for (i = 0; i < n; ++i)
514 unregister_color (pixels[i]);
515}
516
08dc08dc 517
a7ca3326 518DEFUN ("dump-colors", Fdump_colors, Sdump_colors, 0, 0, 0,
7ee72033 519 doc: /* Dump currently allocated colors to stderr. */)
5842a27b 520 (void)
08dc08dc
GM
521{
522 int i, n;
523
524 fputc ('\n', stderr);
178c5d9c 525
08dc08dc
GM
526 for (i = n = 0; i < sizeof color_count / sizeof color_count[0]; ++i)
527 if (color_count[i])
528 {
529 fprintf (stderr, "%3d: %5d", i, color_count[i]);
530 ++n;
531 if (n % 5 == 0)
532 fputc ('\n', stderr);
533 else
534 fputc ('\t', stderr);
535 }
536
537 if (n % 5 != 0)
538 fputc ('\n', stderr);
539 return Qnil;
540}
541
a435fc2a
GM
542#endif /* DEBUG_X_COLORS */
543
d12d0a9b 544
1f847cf8
GM
545/* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
546 color values. Interrupt input must be blocked when this function
547 is called. */
548
549void
971de7fb 550x_free_colors (struct frame *f, long unsigned int *pixels, int npixels)
1f847cf8
GM
551{
552 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
553
554 /* If display has an immutable color map, freeing colors is not
555 necessary and some servers don't allow it. So don't do it. */
556 if (class != StaticColor && class != StaticGray && class != TrueColor)
557 {
a435fc2a 558#ifdef DEBUG_X_COLORS
08dc08dc 559 unregister_colors (pixels, npixels);
a435fc2a 560#endif
513c5806
GM
561 XFreeColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
562 pixels, npixels, 0);
08dc08dc
GM
563 }
564}
565
566
435f4c28
PE
567#ifdef USE_X_TOOLKIT
568
08dc08dc
GM
569/* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
570 color values. Interrupt input must be blocked when this function
571 is called. */
572
573void
b5f03016
AS
574x_free_dpy_colors (Display *dpy, Screen *screen, Colormap cmap,
575 long unsigned int *pixels, int npixels)
08dc08dc
GM
576{
577 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
578 int class = dpyinfo->visual->class;
579
580 /* If display has an immutable color map, freeing colors is not
581 necessary and some servers don't allow it. So don't do it. */
582 if (class != StaticColor && class != StaticGray && class != TrueColor)
583 {
a435fc2a 584#ifdef DEBUG_X_COLORS
08dc08dc 585 unregister_colors (pixels, npixels);
a435fc2a 586#endif
513c5806 587 XFreeColors (dpy, cmap, pixels, npixels, 0);
1f847cf8
GM
588 }
589}
435f4c28 590#endif /* USE_X_TOOLKIT */
08dc08dc 591
82641697
GM
592/* Create and return a GC for use on frame F. GC values and mask
593 are given by XGCV and MASK. */
594
b0ab8123 595static GC
971de7fb 596x_create_gc (struct frame *f, long unsigned int mask, XGCValues *xgcv)
c115973b
JB
597{
598 GC gc;
4d7e6e51 599 block_input ();
82641697 600 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), mask, xgcv);
4d7e6e51 601 unblock_input ();
82641697
GM
602 IF_DEBUG (++ngcs);
603 return gc;
604}
c115973b 605
42120bc7 606
82641697
GM
607/* Free GC which was used on frame F. */
608
b0ab8123 609static void
971de7fb 610x_free_gc (struct frame *f, GC gc)
82641697 611{
4d7e6e51 612 eassert (input_blocked_p ());
a54e2c05 613 IF_DEBUG (eassert (--ngcs >= 0));
82641697 614 XFreeGC (FRAME_X_DISPLAY (f), gc);
82641697 615}
660ed669 616
82641697 617#endif /* HAVE_X_WINDOWS */
660ed669 618
0fda9b75 619#ifdef HAVE_NTGUI
c3cee013
JR
620/* W32 emulation of GCs */
621
b0ab8123 622static GC
7c3320d8 623x_create_gc (struct frame *f, unsigned long mask, XGCValues *xgcv)
c3cee013
JR
624{
625 GC gc;
4d7e6e51 626 block_input ();
c3cee013 627 gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, xgcv);
4d7e6e51 628 unblock_input ();
c3cee013
JR
629 IF_DEBUG (++ngcs);
630 return gc;
631}
632
633
634/* Free GC which was used on frame F. */
635
b0ab8123 636static void
7c3320d8 637x_free_gc (struct frame *f, GC gc)
c3cee013 638{
a54e2c05 639 IF_DEBUG (eassert (--ngcs >= 0));
c3cee013 640 xfree (gc);
c3cee013
JR
641}
642
0fda9b75 643#endif /* HAVE_NTGUI */
660ed669 644
edfda783
AR
645#ifdef HAVE_NS
646/* NS emulation of GCs */
647
b0ab8123 648static GC
3d608a86 649x_create_gc (struct frame *f,
b5f03016
AS
650 unsigned long mask,
651 XGCValues *xgcv)
edfda783 652{
38182d90 653 GC gc = xmalloc (sizeof *gc);
ae1d87e2 654 *gc = *xgcv;
edfda783
AR
655 return gc;
656}
657
b0ab8123 658static void
3d608a86 659x_free_gc (struct frame *f, GC gc)
edfda783 660{
5f445726 661 xfree (gc);
edfda783
AR
662}
663#endif /* HAVE_NS */
664
82641697
GM
665/***********************************************************************
666 Frames and faces
667 ***********************************************************************/
cd0bb842 668
82641697 669/* Initialize face cache and basic faces for frame F. */
cb637678 670
82641697 671void
971de7fb 672init_frame_faces (struct frame *f)
cb637678 673{
82641697
GM
674 /* Make a face cache, if F doesn't have one. */
675 if (FRAME_FACE_CACHE (f) == NULL)
676 FRAME_FACE_CACHE (f) = make_face_cache (f);
178c5d9c 677
c3cee013 678#ifdef HAVE_WINDOW_SYSTEM
82641697 679 /* Make the image cache. */
c3cee013 680 if (FRAME_WINDOW_P (f))
82641697 681 {
91c37b7e
CY
682 /* We initialize the image cache when creating the first frame
683 on a terminal, and not during terminal creation. This way,
684 `x-open-connection' on a tty won't create an image cache. */
354884c4 685 if (FRAME_IMAGE_CACHE (f) == NULL)
354884c4
SM
686 FRAME_IMAGE_CACHE (f) = make_image_cache ();
687 ++FRAME_IMAGE_CACHE (f)->refcount;
82641697 688 }
c3cee013 689#endif /* HAVE_WINDOW_SYSTEM */
cb637678 690
178c5d9c 691 /* Realize basic faces. Must have enough information in frame
82641697
GM
692 parameters to realize basic faces at this point. */
693#ifdef HAVE_X_WINDOWS
694 if (!FRAME_X_P (f) || FRAME_X_WINDOW (f))
c3cee013 695#endif
0fda9b75 696#ifdef HAVE_NTGUI
c3cee013 697 if (!FRAME_WINDOW_P (f) || FRAME_W32_WINDOW (f))
f00691a3 698#endif
edfda783
AR
699#ifdef HAVE_NS
700 if (!FRAME_NS_P (f) || FRAME_NS_WINDOW (f))
82641697
GM
701#endif
702 if (!realize_basic_faces (f))
1088b922 703 emacs_abort ();
82641697 704}
cb637678 705
cb637678 706
e2749141 707/* Free face cache of frame F. Called from delete_frame. */
cb637678 708
82641697 709void
971de7fb 710free_frame_faces (struct frame *f)
cb637678 711{
82641697 712 struct face_cache *face_cache = FRAME_FACE_CACHE (f);
178c5d9c 713
82641697
GM
714 if (face_cache)
715 {
716 free_face_cache (face_cache);
717 FRAME_FACE_CACHE (f) = NULL;
718 }
660ed669 719
c3cee013
JR
720#ifdef HAVE_WINDOW_SYSTEM
721 if (FRAME_WINDOW_P (f))
195f798e 722 {
354884c4 723 struct image_cache *image_cache = FRAME_IMAGE_CACHE (f);
82641697 724 if (image_cache)
195f798e 725 {
82641697
GM
726 --image_cache->refcount;
727 if (image_cache->refcount == 0)
728 free_image_cache (f);
195f798e
RS
729 }
730 }
c3cee013 731#endif /* HAVE_WINDOW_SYSTEM */
cb637678
JB
732}
733
82641697 734
8bd201d6
GM
735/* Clear face caches, and recompute basic faces for frame F. Call
736 this after changing frame parameters on which those faces depend,
737 or when realized faces have been freed due to changing attributes
738 of named faces. */
82641697
GM
739
740void
971de7fb 741recompute_basic_faces (struct frame *f)
cb637678 742{
82641697
GM
743 if (FRAME_FACE_CACHE (f))
744 {
8bd201d6 745 clear_face_cache (0);
18df9369 746 if (!realize_basic_faces (f))
1088b922 747 emacs_abort ();
82641697
GM
748 }
749}
cb637678 750
cb637678 751
82641697
GM
752/* Clear the face caches of all frames. CLEAR_FONTS_P non-zero means
753 try to free unused fonts, too. */
cb637678 754
adfea139 755void
971de7fb 756clear_face_cache (int clear_fonts_p)
cb637678 757{
c3cee013 758#ifdef HAVE_WINDOW_SYSTEM
82641697 759 Lisp_Object tail, frame;
828e66d1 760
82641697
GM
761 if (clear_fonts_p
762 || ++clear_font_table_count == CLEAR_FONT_TABLE_COUNT)
828e66d1 763 {
82641697
GM
764 /* From time to time see if we can unload some fonts. This also
765 frees all realized faces on all frames. Fonts needed by
766 faces will be loaded again when faces are realized again. */
767 clear_font_table_count = 0;
195f798e 768
82641697 769 FOR_EACH_FRAME (tail, frame)
195f798e 770 {
d5641fc5 771 struct frame *f = XFRAME (frame);
c3cee013 772 if (FRAME_WINDOW_P (f)
82641697 773 && FRAME_X_DISPLAY_INFO (f)->n_fonts > CLEAR_FONT_TABLE_NFONTS)
a45543bc
DA
774 {
775 clear_font_cache (f);
776 free_all_realized_faces (frame);
777 }
82641697
GM
778 }
779 }
780 else
781 {
782 /* Clear GCs of realized faces. */
783 FOR_EACH_FRAME (tail, frame)
784 {
071048a3 785 struct frame *f = XFRAME (frame);
c3cee013 786 if (FRAME_WINDOW_P (f))
82641697 787 clear_face_gcs (FRAME_FACE_CACHE (f));
195f798e 788 }
a2bc5bdd 789 clear_image_caches (Qnil);
828e66d1 790 }
c3cee013 791#endif /* HAVE_WINDOW_SYSTEM */
cd0bb842
RS
792}
793
82641697 794
a7ca3326 795DEFUN ("clear-face-cache", Fclear_face_cache, Sclear_face_cache, 0, 1, 0,
7ee72033
MB
796 doc: /* Clear face caches on all frames.
797Optional THOROUGHLY non-nil means try to free unused fonts, too. */)
5842a27b 798 (Lisp_Object thoroughly)
cd0bb842 799{
6a3f48c7 800 clear_face_cache (!NILP (thoroughly));
ae4b4ba5
GM
801 ++face_change_count;
802 ++windows_or_buffers_changed;
82641697
GM
803 return Qnil;
804}
805
82641697
GM
806\f
807/***********************************************************************
808 X Pixmaps
809 ***********************************************************************/
810
c3cee013 811#ifdef HAVE_WINDOW_SYSTEM
82641697 812
fef04523 813DEFUN ("bitmap-spec-p", Fbitmap_spec_p, Sbitmap_spec_p, 1, 1, 0,
7ee72033 814 doc: /* Value is non-nil if OBJECT is a valid bitmap specification.
228299fa
GM
815A bitmap specification is either a string, a file name, or a list
816\(WIDTH HEIGHT DATA) where WIDTH is the pixel width of the bitmap,
817HEIGHT is its height, and DATA is a string containing the bits of
818the pixmap. Bits are stored row by row, each row occupies
7ee72033 819\(WIDTH + 7)/8 bytes. */)
5842a27b 820 (Lisp_Object object)
82641697 821{
d7e6881a 822 bool pixmap_p = 0;
178c5d9c 823
c7ae3284
GM
824 if (STRINGP (object))
825 /* If OBJECT is a string, it's a file name. */
826 pixmap_p = 1;
827 else if (CONSP (object))
828 {
829 /* Otherwise OBJECT must be (WIDTH HEIGHT DATA), WIDTH and
d311d28c 830 HEIGHT must be ints > 0, and DATA must be string large
c7ae3284
GM
831 enough to hold a bitmap of the specified size. */
832 Lisp_Object width, height, data;
833
834 height = width = data = Qnil;
178c5d9c 835
c7ae3284
GM
836 if (CONSP (object))
837 {
838 width = XCAR (object);
839 object = XCDR (object);
840 if (CONSP (object))
841 {
842 height = XCAR (object);
843 object = XCDR (object);
844 if (CONSP (object))
845 data = XCAR (object);
846 }
847 }
cd0bb842 848
c8907a93 849 if (STRINGP (data)
d311d28c
PE
850 && RANGED_INTEGERP (1, width, INT_MAX)
851 && RANGED_INTEGERP (1, height, INT_MAX))
c7ae3284 852 {
d311d28c
PE
853 int bytes_per_row = ((XINT (width) + BITS_PER_CHAR - 1)
854 / BITS_PER_CHAR);
c8907a93 855 if (XINT (height) <= SBYTES (data) / bytes_per_row)
c7ae3284
GM
856 pixmap_p = 1;
857 }
858 }
859
860 return pixmap_p ? Qt : Qnil;
cd0bb842
RS
861}
862
cd0bb842 863
82641697
GM
864/* Load a bitmap according to NAME (which is either a file name or a
865 pixmap spec) for use on frame F. Value is the bitmap_id (see
866 xfns.c). If NAME is nil, return with a bitmap id of zero. If
867 bitmap cannot be loaded, display a message saying so, and return
868 zero. Store the bitmap width in *W_PTR and its height in *H_PTR,
869 if these pointers are not null. */
cd0bb842 870
0766b489 871static ptrdiff_t
a10c8269 872load_pixmap (struct frame *f, Lisp_Object name, unsigned int *w_ptr,
b5f03016 873 unsigned int *h_ptr)
cd0bb842 874{
0766b489 875 ptrdiff_t bitmap_id;
cd0bb842
RS
876
877 if (NILP (name))
82641697 878 return 0;
cd0bb842 879
d5ddd1a3 880 CHECK_TYPE (!NILP (Fbitmap_spec_p (name)), Qbitmap_spec_p, name);
cd0bb842 881
4d7e6e51 882 block_input ();
cd0bb842
RS
883 if (CONSP (name))
884 {
885 /* Decode a bitmap spec into a bitmap. */
886
887 int h, w;
888 Lisp_Object bits;
889
890 w = XINT (Fcar (name));
891 h = XINT (Fcar (Fcdr (name)));
892 bits = Fcar (Fcdr (Fcdr (name)));
893
42a5b22f 894 bitmap_id = x_create_bitmap_from_data (f, SSDATA (bits),
cd0bb842
RS
895 w, h);
896 }
897 else
898 {
899 /* It must be a string -- a file name. */
900 bitmap_id = x_create_bitmap_from_file (f, name);
901 }
4d7e6e51 902 unblock_input ();
cd0bb842 903
7812a96f 904 if (bitmap_id < 0)
82641697 905 {
2eb582ae 906 add_to_log ("Invalid or undefined bitmap `%s'", name, Qnil);
82641697 907 bitmap_id = 0;
cd0bb842 908
82641697
GM
909 if (w_ptr)
910 *w_ptr = 0;
911 if (h_ptr)
912 *h_ptr = 0;
913 }
914 else
915 {
e509cfa6 916#ifdef GLYPH_DEBUG
82641697
GM
917 ++npixmaps_allocated;
918#endif
919 if (w_ptr)
920 *w_ptr = x_bitmap_width (f, bitmap_id);
921
922 if (h_ptr)
923 *h_ptr = x_bitmap_height (f, bitmap_id);
924 }
cd0bb842
RS
925
926 return bitmap_id;
cb637678 927}
87485d6f 928
c3cee013 929#endif /* HAVE_WINDOW_SYSTEM */
82641697 930
87485d6f 931
82641697 932\f
82641697
GM
933/***********************************************************************
934 X Colors
935 ***********************************************************************/
936
b35df831
MB
937/* Parse RGB_LIST, and fill in the RGB fields of COLOR.
938 RGB_LIST should contain (at least) 3 lisp integers.
939 Return 0 if there's a problem with RGB_LIST, otherwise return 1. */
940
941static int
971de7fb 942parse_rgb_list (Lisp_Object rgb_list, XColor *color)
b35df831
MB
943{
944#define PARSE_RGB_LIST_FIELD(field) \
945 if (CONSP (rgb_list) && INTEGERP (XCAR (rgb_list))) \
946 { \
947 color->field = XINT (XCAR (rgb_list)); \
948 rgb_list = XCDR (rgb_list); \
949 } \
950 else \
951 return 0;
952
953 PARSE_RGB_LIST_FIELD (red);
954 PARSE_RGB_LIST_FIELD (green);
955 PARSE_RGB_LIST_FIELD (blue);
956
957 return 1;
958}
959
960
961/* Lookup on frame F the color described by the lisp string COLOR.
962 The resulting tty color is returned in TTY_COLOR; if STD_COLOR is
963 non-zero, then the `standard' definition of the same color is
964 returned in it. */
965
578098f3 966static bool
b5f03016
AS
967tty_lookup_color (struct frame *f, Lisp_Object color, XColor *tty_color,
968 XColor *std_color)
b35df831
MB
969{
970 Lisp_Object frame, color_desc;
971
972 if (!STRINGP (color) || NILP (Ffboundp (Qtty_color_desc)))
973 return 0;
974
975 XSETFRAME (frame, f);
976
977 color_desc = call2 (Qtty_color_desc, color, frame);
978 if (CONSP (color_desc) && CONSP (XCDR (color_desc)))
979 {
980 Lisp_Object rgb;
981
982 if (! INTEGERP (XCAR (XCDR (color_desc))))
983 return 0;
984
985 tty_color->pixel = XINT (XCAR (XCDR (color_desc)));
986
987 rgb = XCDR (XCDR (color_desc));
988 if (! parse_rgb_list (rgb, tty_color))
989 return 0;
990
991 /* Should we fill in STD_COLOR too? */
992 if (std_color)
993 {
994 /* Default STD_COLOR to the same as TTY_COLOR. */
995 *std_color = *tty_color;
996
997 /* Do a quick check to see if the returned descriptor is
998 actually _exactly_ equal to COLOR, otherwise we have to
999 lookup STD_COLOR separately. If it's impossible to lookup
1000 a standard color, we just give up and use TTY_COLOR. */
1001 if ((!STRINGP (XCAR (color_desc))
1002 || NILP (Fstring_equal (color, XCAR (color_desc))))
51f86bfc 1003 && !NILP (Ffboundp (Qtty_color_standard_values)))
b35df831
MB
1004 {
1005 /* Look up STD_COLOR separately. */
1006 rgb = call1 (Qtty_color_standard_values, color);
1007 if (! parse_rgb_list (rgb, std_color))
1008 return 0;
1009 }
1010 }
1011
1012 return 1;
1013 }
1014 else if (NILP (Fsymbol_value (intern ("tty-defined-color-alist"))))
1015 /* We were called early during startup, and the colors are not
1016 yet set up in tty-defined-color-alist. Don't return a failure
1017 indication, since this produces the annoying "Unable to
1018 load color" messages in the *Messages* buffer. */
1019 return 1;
1020 else
1021 /* tty-color-desc seems to have returned a bad value. */
1022 return 0;
1023}
1024
2d764c78 1025/* A version of defined_color for non-X frames. */
08dc08dc 1026
578098f3 1027static bool
eec47d6b 1028tty_defined_color (struct frame *f, const char *color_name,
578098f3 1029 XColor *color_def, bool alloc)
2d764c78 1030{
578098f3 1031 bool status = 1;
2d764c78 1032
b35df831 1033 /* Defaults. */
177c0ea7 1034 color_def->pixel = FACE_TTY_DEFAULT_COLOR;
b35df831
MB
1035 color_def->red = 0;
1036 color_def->blue = 0;
1037 color_def->green = 0;
a61c12d5 1038
b35df831 1039 if (*color_name)
3062711f 1040 status = tty_lookup_color (f, build_string (color_name), color_def, NULL);
b35df831
MB
1041
1042 if (color_def->pixel == FACE_TTY_DEFAULT_COLOR && *color_name)
f9d2fdc4
EZ
1043 {
1044 if (strcmp (color_name, "unspecified-fg") == 0)
b35df831 1045 color_def->pixel = FACE_TTY_DEFAULT_FG_COLOR;
f9d2fdc4 1046 else if (strcmp (color_name, "unspecified-bg") == 0)
b35df831 1047 color_def->pixel = FACE_TTY_DEFAULT_BG_COLOR;
f9d2fdc4
EZ
1048 }
1049
b35df831 1050 if (color_def->pixel != FACE_TTY_DEFAULT_COLOR)
3b451f74
EZ
1051 status = 1;
1052
2d764c78
EZ
1053 return status;
1054}
1055
08dc08dc
GM
1056
1057/* Decide if color named COLOR_NAME is valid for the display
1058 associated with the frame F; if so, return the rgb values in
578098f3 1059 COLOR_DEF. If ALLOC, allocate a new colormap cell.
2d764c78
EZ
1060
1061 This does the right thing for any type of frame. */
08dc08dc 1062
578098f3 1063static bool
b5f03016 1064defined_color (struct frame *f, const char *color_name, XColor *color_def,
578098f3 1065 bool alloc)
2d764c78
EZ
1066{
1067 if (!FRAME_WINDOW_P (f))
1068 return tty_defined_color (f, color_name, color_def, alloc);
82641697 1069#ifdef HAVE_X_WINDOWS
2d764c78
EZ
1070 else if (FRAME_X_P (f))
1071 return x_defined_color (f, color_name, color_def, alloc);
1072#endif
0fda9b75 1073#ifdef HAVE_NTGUI
2d764c78 1074 else if (FRAME_W32_P (f))
2d764c78
EZ
1075 return w32_defined_color (f, color_name, color_def, alloc);
1076#endif
edfda783
AR
1077#ifdef HAVE_NS
1078 else if (FRAME_NS_P (f))
1079 return ns_defined_color (f, color_name, color_def, alloc, 1);
2d764c78
EZ
1080#endif
1081 else
1088b922 1082 emacs_abort ();
2d764c78
EZ
1083}
1084
08dc08dc
GM
1085
1086/* Given the index IDX of a tty color on frame F, return its name, a
1087 Lisp string. */
2d764c78
EZ
1088
1089Lisp_Object
971de7fb 1090tty_color_name (struct frame *f, int idx)
2d764c78 1091{
2d764c78
EZ
1092 if (idx >= 0 && !NILP (Ffboundp (Qtty_color_by_index)))
1093 {
a61c12d5
EZ
1094 Lisp_Object frame;
1095 Lisp_Object coldesc;
1096
1097 XSETFRAME (frame, f);
1098 coldesc = call2 (Qtty_color_by_index, make_number (idx), frame);
2d764c78
EZ
1099
1100 if (!NILP (coldesc))
1101 return XCAR (coldesc);
1102 }
1103#ifdef MSDOS
1104 /* We can have an MSDOG frame under -nw for a short window of
1105 opportunity before internal_terminal_init is called. DTRT. */
1106 if (FRAME_MSDOS_P (f) && !inhibit_window_system)
1107 return msdos_stdcolor_name (idx);
1108#endif
1109
ef917393
EZ
1110 if (idx == FACE_TTY_DEFAULT_FG_COLOR)
1111 return build_string (unspecified_fg);
1112 if (idx == FACE_TTY_DEFAULT_BG_COLOR)
1113 return build_string (unspecified_bg);
c3cee013 1114
ef917393 1115 return Qunspecified;
2d764c78 1116}
82641697 1117
08dc08dc 1118
82641697 1119/* Return non-zero if COLOR_NAME is a shade of gray (or white or
71433d39
RS
1120 black) on frame F.
1121
1122 The criterion implemented here is not a terribly sophisticated one. */
82641697
GM
1123
1124static int
eec47d6b 1125face_color_gray_p (struct frame *f, const char *color_name)
82641697
GM
1126{
1127 XColor color;
1128 int gray_p;
1129
1130 if (defined_color (f, color_name, &color, 0))
e4769531 1131 gray_p = (/* Any color sufficiently close to black counts as gray. */
71433d39
RS
1132 (color.red < 5000 && color.green < 5000 && color.blue < 5000)
1133 ||
1ea40aa2 1134 ((eabs (color.red - color.green)
71433d39 1135 < max (color.red, color.green) / 20)
1ea40aa2 1136 && (eabs (color.green - color.blue)
71433d39 1137 < max (color.green, color.blue) / 20)
1ea40aa2 1138 && (eabs (color.blue - color.red)
71433d39 1139 < max (color.blue, color.red) / 20)));
87485d6f 1140 else
82641697 1141 gray_p = 0;
178c5d9c 1142
82641697 1143 return gray_p;
87485d6f 1144}
87485d6f 1145
cb637678 1146
82641697
GM
1147/* Return non-zero if color COLOR_NAME can be displayed on frame F.
1148 BACKGROUND_P non-zero means the color will be used as background
1149 color. */
1150
1151static int
b5f03016
AS
1152face_color_supported_p (struct frame *f, const char *color_name,
1153 int background_p)
82641697
GM
1154{
1155 Lisp_Object frame;
2d764c78 1156 XColor not_used;
82641697
GM
1157
1158 XSETFRAME (frame, f);
6b61353c 1159 return
6a46b96b 1160#ifdef HAVE_WINDOW_SYSTEM
6b61353c
KH
1161 FRAME_WINDOW_P (f)
1162 ? (!NILP (Fxw_display_color_p (frame))
05131107
JR
1163 || xstrcasecmp (color_name, "black") == 0
1164 || xstrcasecmp (color_name, "white") == 0
6b61353c
KH
1165 || (background_p
1166 && face_color_gray_p (f, color_name))
1167 || (!NILP (Fx_display_grayscale_p (frame))
1168 && face_color_gray_p (f, color_name)))
1169 :
1170#endif
1171 tty_defined_color (f, color_name, &not_used, 0);
2d764c78 1172}
82641697
GM
1173
1174
da47150d 1175DEFUN ("color-gray-p", Fcolor_gray_p, Scolor_gray_p, 1, 2, 0,
7ee72033 1176 doc: /* Return non-nil if COLOR is a shade of gray (or white or black).
228299fa 1177FRAME specifies the frame and thus the display for interpreting COLOR.
7ee72033 1178If FRAME is nil or omitted, use the selected frame. */)
5842a27b 1179 (Lisp_Object color, Lisp_Object frame)
cb637678 1180{
b7826503 1181 CHECK_STRING (color);
d9f07150
DA
1182 return (face_color_gray_p (decode_any_frame (frame), SSDATA (color))
1183 ? Qt : Qnil);
82641697 1184}
660ed669 1185
fffc2367 1186
da47150d 1187DEFUN ("color-supported-p", Fcolor_supported_p,
2e1bb1c3 1188 Scolor_supported_p, 1, 3, 0,
7ee72033 1189 doc: /* Return non-nil if COLOR can be displayed on FRAME.
228299fa 1190BACKGROUND-P non-nil means COLOR is used as a background.
fc820cc5 1191Otherwise, this function tells whether it can be used as a foreground.
228299fa 1192If FRAME is nil or omitted, use the selected frame.
7ee72033 1193COLOR must be a valid color name. */)
5842a27b 1194 (Lisp_Object color, Lisp_Object frame, Lisp_Object background_p)
82641697 1195{
b7826503 1196 CHECK_STRING (color);
d9f07150
DA
1197 return (face_color_supported_p (decode_any_frame (frame),
1198 SSDATA (color), !NILP (background_p))
1199 ? Qt : Qnil);
82641697 1200}
7b37f67b 1201
08dc08dc 1202
82641697
GM
1203/* Load color with name NAME for use by face FACE on frame F.
1204 TARGET_INDEX must be one of LFACE_FOREGROUND_INDEX,
1205 LFACE_BACKGROUND_INDEX, LFACE_UNDERLINE_INDEX, LFACE_OVERLINE_INDEX,
1206 LFACE_STRIKE_THROUGH_INDEX, or LFACE_BOX_INDEX. Value is the
1207 pixel color. If color cannot be loaded, display a message, and
1208 return the foreground, background or underline color of F, but
1209 record that fact in flags of the face so that we don't try to free
1210 these colors. */
1211
44747bd0 1212unsigned long
b5f03016
AS
1213load_color (struct frame *f, struct face *face, Lisp_Object name,
1214 enum lface_attribute_index target_index)
82641697
GM
1215{
1216 XColor color;
178c5d9c 1217
a54e2c05
DA
1218 eassert (STRINGP (name));
1219 eassert (target_index == LFACE_FOREGROUND_INDEX
82641697
GM
1220 || target_index == LFACE_BACKGROUND_INDEX
1221 || target_index == LFACE_UNDERLINE_INDEX
1222 || target_index == LFACE_OVERLINE_INDEX
1223 || target_index == LFACE_STRIKE_THROUGH_INDEX
1224 || target_index == LFACE_BOX_INDEX);
178c5d9c 1225
82641697
GM
1226 /* if the color map is full, defined_color will return a best match
1227 to the values in an existing cell. */
42a5b22f 1228 if (!defined_color (f, SSDATA (name), &color, 1))
82641697 1229 {
2d764c78 1230 add_to_log ("Unable to load color \"%s\"", name, Qnil);
178c5d9c 1231
82641697 1232 switch (target_index)
1120eb5e 1233 {
82641697
GM
1234 case LFACE_FOREGROUND_INDEX:
1235 face->foreground_defaulted_p = 1;
1236 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1237 break;
178c5d9c 1238
82641697
GM
1239 case LFACE_BACKGROUND_INDEX:
1240 face->background_defaulted_p = 1;
1241 color.pixel = FRAME_BACKGROUND_PIXEL (f);
1242 break;
178c5d9c 1243
82641697
GM
1244 case LFACE_UNDERLINE_INDEX:
1245 face->underline_defaulted_p = 1;
1246 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1247 break;
178c5d9c 1248
82641697
GM
1249 case LFACE_OVERLINE_INDEX:
1250 face->overline_color_defaulted_p = 1;
1251 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1120eb5e 1252 break;
178c5d9c 1253
82641697
GM
1254 case LFACE_STRIKE_THROUGH_INDEX:
1255 face->strike_through_color_defaulted_p = 1;
1256 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1257 break;
178c5d9c 1258
82641697
GM
1259 case LFACE_BOX_INDEX:
1260 face->box_color_defaulted_p = 1;
1261 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1262 break;
1263
1264 default:
1088b922 1265 emacs_abort ();
1120eb5e 1266 }
82641697 1267 }
e509cfa6 1268#ifdef GLYPH_DEBUG
82641697
GM
1269 else
1270 ++ncolors_allocated;
1271#endif
178c5d9c 1272
82641697
GM
1273 return color.pixel;
1274}
1120eb5e 1275
08dc08dc 1276
c3cee013 1277#ifdef HAVE_WINDOW_SYSTEM
1120eb5e 1278
82641697
GM
1279/* Load colors for face FACE which is used on frame F. Colors are
1280 specified by slots LFACE_BACKGROUND_INDEX and LFACE_FOREGROUND_INDEX
1281 of ATTRS. If the background color specified is not supported on F,
1282 try to emulate gray colors with a stipple from Vface_default_stipple. */
1283
1284static void
4973679b
PE
1285load_face_colors (struct frame *f, struct face *face,
1286 Lisp_Object attrs[LFACE_VECTOR_SIZE])
82641697
GM
1287{
1288 Lisp_Object fg, bg;
1289
1290 bg = attrs[LFACE_BACKGROUND_INDEX];
1291 fg = attrs[LFACE_FOREGROUND_INDEX];
1292
1293 /* Swap colors if face is inverse-video. */
1294 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1295 {
1296 Lisp_Object tmp;
1297 tmp = fg;
1298 fg = bg;
1299 bg = tmp;
1300 }
1301
1302 /* Check for support for foreground, not for background because
1303 face_color_supported_p is smart enough to know that grays are
1304 "supported" as background because we are supposed to use stipple
1305 for them. */
42a5b22f 1306 if (!face_color_supported_p (f, SSDATA (bg), 0)
fef04523 1307 && !NILP (Fbitmap_spec_p (Vface_default_stipple)))
82641697
GM
1308 {
1309 x_destroy_bitmap (f, face->stipple);
1310 face->stipple = load_pixmap (f, Vface_default_stipple,
1311 &face->pixmap_w, &face->pixmap_h);
1312 }
82641697 1313
be8a72f4 1314 face->background = load_color (f, face, bg, LFACE_BACKGROUND_INDEX);
82641697 1315 face->foreground = load_color (f, face, fg, LFACE_FOREGROUND_INDEX);
cb637678
JB
1316}
1317
660ed669 1318
82641697 1319/* Free color PIXEL on frame F. */
cd0bb842 1320
cb637678 1321void
971de7fb 1322unload_color (struct frame *f, long unsigned int pixel)
cb637678 1323{
c3cee013 1324#ifdef HAVE_X_WINDOWS
30a7ac22
GM
1325 if (pixel != -1)
1326 {
4d7e6e51 1327 block_input ();
30a7ac22 1328 x_free_colors (f, &pixel, 1);
4d7e6e51 1329 unblock_input ();
30a7ac22 1330 }
c3cee013 1331#endif
82641697
GM
1332}
1333
1334
1335/* Free colors allocated for FACE. */
1336
1337static void
971de7fb 1338free_face_colors (struct frame *f, struct face *face)
82641697 1339{
edfda783 1340/* PENDING(NS): need to do something here? */
c3cee013 1341#ifdef HAVE_X_WINDOWS
28a072fe
GM
1342 if (face->colors_copied_bitwise_p)
1343 return;
1344
4d7e6e51 1345 block_input ();
178c5d9c 1346
08dc08dc
GM
1347 if (!face->foreground_defaulted_p)
1348 {
1349 x_free_colors (f, &face->foreground, 1);
1350 IF_DEBUG (--ncolors_allocated);
1351 }
178c5d9c 1352
08dc08dc
GM
1353 if (!face->background_defaulted_p)
1354 {
1355 x_free_colors (f, &face->background, 1);
1356 IF_DEBUG (--ncolors_allocated);
1357 }
82641697 1358
08dc08dc
GM
1359 if (face->underline_p
1360 && !face->underline_defaulted_p)
1361 {
1362 x_free_colors (f, &face->underline_color, 1);
1363 IF_DEBUG (--ncolors_allocated);
1364 }
82641697 1365
08dc08dc
GM
1366 if (face->overline_p
1367 && !face->overline_color_defaulted_p)
1368 {
1369 x_free_colors (f, &face->overline_color, 1);
1370 IF_DEBUG (--ncolors_allocated);
1371 }
82641697 1372
08dc08dc
GM
1373 if (face->strike_through_p
1374 && !face->strike_through_color_defaulted_p)
1375 {
1376 x_free_colors (f, &face->strike_through_color, 1);
1377 IF_DEBUG (--ncolors_allocated);
1378 }
82641697 1379
08dc08dc
GM
1380 if (face->box != FACE_NO_BOX
1381 && !face->box_color_defaulted_p)
1382 {
1383 x_free_colors (f, &face->box_color, 1);
1384 IF_DEBUG (--ncolors_allocated);
82641697 1385 }
08dc08dc 1386
4d7e6e51 1387 unblock_input ();
2d764c78 1388#endif /* HAVE_X_WINDOWS */
c3cee013 1389}
08dc08dc 1390
c3cee013 1391#endif /* HAVE_WINDOW_SYSTEM */
82641697
GM
1392
1393
1394\f
1395/***********************************************************************
1396 XLFD Font Names
1397 ***********************************************************************/
1398
1399/* An enumerator for each field of an XLFD font name. */
1400
1401enum xlfd_field
1402{
1403 XLFD_FOUNDRY,
1404 XLFD_FAMILY,
1405 XLFD_WEIGHT,
1406 XLFD_SLANT,
1407 XLFD_SWIDTH,
1408 XLFD_ADSTYLE,
1409 XLFD_PIXEL_SIZE,
1410 XLFD_POINT_SIZE,
1411 XLFD_RESX,
1412 XLFD_RESY,
1413 XLFD_SPACING,
1414 XLFD_AVGWIDTH,
1415 XLFD_REGISTRY,
1416 XLFD_ENCODING,
1417 XLFD_LAST
1418};
1419
178c5d9c 1420/* An enumerator for each possible slant value of a font. Taken from
82641697
GM
1421 the XLFD specification. */
1422
1423enum xlfd_slant
1424{
1425 XLFD_SLANT_UNKNOWN,
1426 XLFD_SLANT_ROMAN,
1427 XLFD_SLANT_ITALIC,
1428 XLFD_SLANT_OBLIQUE,
1429 XLFD_SLANT_REVERSE_ITALIC,
1430 XLFD_SLANT_REVERSE_OBLIQUE,
1431 XLFD_SLANT_OTHER
1432};
1433
1434/* Relative font weight according to XLFD documentation. */
1435
1436enum xlfd_weight
1437{
1438 XLFD_WEIGHT_UNKNOWN,
1439 XLFD_WEIGHT_ULTRA_LIGHT, /* 10 */
1440 XLFD_WEIGHT_EXTRA_LIGHT, /* 20 */
1441 XLFD_WEIGHT_LIGHT, /* 30 */
1442 XLFD_WEIGHT_SEMI_LIGHT, /* 40: SemiLight, Book, ... */
1443 XLFD_WEIGHT_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1444 XLFD_WEIGHT_SEMI_BOLD, /* 60: SemiBold, DemiBold, ... */
1445 XLFD_WEIGHT_BOLD, /* 70: Bold, ... */
1446 XLFD_WEIGHT_EXTRA_BOLD, /* 80: ExtraBold, Heavy, ... */
1447 XLFD_WEIGHT_ULTRA_BOLD /* 90: UltraBold, Black, ... */
1448};
1449
1450/* Relative proportionate width. */
1451
1452enum xlfd_swidth
1453{
1454 XLFD_SWIDTH_UNKNOWN,
1455 XLFD_SWIDTH_ULTRA_CONDENSED, /* 10 */
1456 XLFD_SWIDTH_EXTRA_CONDENSED, /* 20 */
1457 XLFD_SWIDTH_CONDENSED, /* 30: Condensed, Narrow, Compressed, ... */
1458 XLFD_SWIDTH_SEMI_CONDENSED, /* 40: semicondensed */
1459 XLFD_SWIDTH_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1460 XLFD_SWIDTH_SEMI_EXPANDED, /* 60: SemiExpanded, DemiExpanded, ... */
1461 XLFD_SWIDTH_EXPANDED, /* 70: Expanded... */
1462 XLFD_SWIDTH_EXTRA_EXPANDED, /* 80: ExtraExpanded, Wide... */
1463 XLFD_SWIDTH_ULTRA_EXPANDED /* 90: UltraExpanded... */
1464};
1465
82641697
GM
1466/* Order by which font selection chooses fonts. The default values
1467 mean `first, find a best match for the font width, then for the
1468 font height, then for weight, then for slant.' This variable can be
1469 set via set-face-font-sort-order. */
1470
1471static int font_sort_order[4];
82641697 1472
2dee4c0b
KH
1473#ifdef HAVE_WINDOW_SYSTEM
1474
2dee4c0b 1475static enum font_property_index font_props_for_sorting[FONT_SIZE_INDEX];
82641697 1476
2dee4c0b 1477static int
971de7fb 1478compare_fonts_by_sort_order (const void *v1, const void *v2)
2dee4c0b 1479{
98c6f1e3
PE
1480 Lisp_Object const *p1 = v1;
1481 Lisp_Object const *p2 = v2;
1482 Lisp_Object font1 = *p1;
1483 Lisp_Object font2 = *p2;
2dee4c0b 1484 int i;
d5ab09cd 1485
2dee4c0b
KH
1486 for (i = 0; i < FONT_SIZE_INDEX; i++)
1487 {
1488 enum font_property_index idx = font_props_for_sorting[i];
1489 Lisp_Object val1 = AREF (font1, idx), val2 = AREF (font2, idx);
1490 int result;
82641697 1491
2dee4c0b
KH
1492 if (idx <= FONT_REGISTRY_INDEX)
1493 {
1494 if (STRINGP (val1))
42a5b22f 1495 result = STRINGP (val2) ? strcmp (SSDATA (val1), SSDATA (val2)) : -1;
2dee4c0b
KH
1496 else
1497 result = STRINGP (val2) ? 1 : 0;
1498 }
1499 else
1500 {
1501 if (INTEGERP (val1))
d311d28c
PE
1502 result = (INTEGERP (val2) && XINT (val1) >= XINT (val2)
1503 ? XINT (val1) > XINT (val2)
1504 : -1);
2dee4c0b
KH
1505 else
1506 result = INTEGERP (val2) ? 1 : 0;
1507 }
1508 if (result)
1509 return result;
1510 }
1511 return 0;
82641697
GM
1512}
1513
2dee4c0b
KH
1514DEFUN ("x-family-fonts", Fx_family_fonts, Sx_family_fonts, 0, 2, 0,
1515 doc: /* Return a list of available fonts of family FAMILY on FRAME.
1516If FAMILY is omitted or nil, list all families.
1517Otherwise, FAMILY must be a string, possibly containing wildcards
1518`?' and `*'.
1519If FRAME is omitted or nil, use the selected frame.
1520Each element of the result is a vector [FAMILY WIDTH POINT-SIZE WEIGHT
1521SLANT FIXED-P FULL REGISTRY-AND-ENCODING].
1522FAMILY is the font family name. POINT-SIZE is the size of the
1523font in 1/10 pt. WIDTH, WEIGHT, and SLANT are symbols describing the
1524width, weight and slant of the font. These symbols are the same as for
1525face attributes. FIXED-P is non-nil if the font is fixed-pitch.
1526FULL is the full name of the font, and REGISTRY-AND-ENCODING is a string
1527giving the registry and encoding of the font.
1528The result list is sorted according to the current setting of
1529the face font sort order. */)
5842a27b 1530 (Lisp_Object family, Lisp_Object frame)
2dee4c0b 1531{
022eef62 1532 Lisp_Object font_spec, list, *drivers, vec;
fdb396e2 1533 struct frame *f = decode_live_frame (frame);
d311d28c
PE
1534 ptrdiff_t i, nfonts;
1535 EMACS_INT ndrivers;
2dee4c0b 1536 Lisp_Object result;
d311d28c 1537 USE_SAFE_ALLOCA;
82641697 1538
62057df6 1539 font_spec = Ffont_spec (0, NULL);
2dee4c0b
KH
1540 if (!NILP (family))
1541 {
1542 CHECK_STRING (family);
6b1f69f1 1543 font_parse_family_registry (family, Qnil, font_spec);
2dee4c0b 1544 }
022eef62 1545
fdb396e2 1546 list = font_list_entities (f, font_spec);
022eef62 1547 if (NILP (list))
2dee4c0b 1548 return Qnil;
022eef62
CY
1549
1550 /* Sort the font entities. */
1551 for (i = 0; i < 4; i++)
1552 switch (font_sort_order[i])
1553 {
1554 case XLFD_SWIDTH:
1555 font_props_for_sorting[i] = FONT_WIDTH_INDEX; break;
1556 case XLFD_POINT_SIZE:
1557 font_props_for_sorting[i] = FONT_SIZE_INDEX; break;
1558 case XLFD_WEIGHT:
1559 font_props_for_sorting[i] = FONT_WEIGHT_INDEX; break;
1560 default:
1561 font_props_for_sorting[i] = FONT_SLANT_INDEX; break;
1562 }
1563 font_props_for_sorting[i++] = FONT_FAMILY_INDEX;
1564 font_props_for_sorting[i++] = FONT_FOUNDRY_INDEX;
1565 font_props_for_sorting[i++] = FONT_ADSTYLE_INDEX;
1566 font_props_for_sorting[i++] = FONT_REGISTRY_INDEX;
1567
1568 ndrivers = XINT (Flength (list));
d311d28c 1569 SAFE_ALLOCA_LISP (drivers, ndrivers);
022eef62
CY
1570 for (i = 0; i < ndrivers; i++, list = XCDR (list))
1571 drivers[i] = XCAR (list);
1572 vec = Fvconcat (ndrivers, drivers);
1573 nfonts = ASIZE (vec);
1574
663e2b3f 1575 qsort (XVECTOR (vec)->contents, nfonts, word_size,
022eef62 1576 compare_fonts_by_sort_order);
82641697 1577
2dee4c0b
KH
1578 result = Qnil;
1579 for (i = nfonts - 1; i >= 0; --i)
1580 {
1581 Lisp_Object font = AREF (vec, i);
25721f5b 1582 Lisp_Object v = make_uninit_vector (8);
2dee4c0b
KH
1583 int point;
1584 Lisp_Object spacing;
1585
1586 ASET (v, 0, AREF (font, FONT_FAMILY_INDEX));
1587 ASET (v, 1, FONT_WIDTH_SYMBOLIC (font));
1588 point = PIXEL_TO_POINT (XINT (AREF (font, FONT_SIZE_INDEX)) * 10,
fdb396e2 1589 FRAME_RES_Y (f));
2dee4c0b
KH
1590 ASET (v, 2, make_number (point));
1591 ASET (v, 3, FONT_WEIGHT_SYMBOLIC (font));
1592 ASET (v, 4, FONT_SLANT_SYMBOLIC (font));
1593 spacing = Ffont_get (font, QCspacing);
1594 ASET (v, 5, (NILP (spacing) || EQ (spacing, Qp)) ? Qnil : Qt);
62057df6 1595 ASET (v, 6, Ffont_xlfd_name (font, Qnil));
2dee4c0b 1596 ASET (v, 7, AREF (font, FONT_REGISTRY_INDEX));
82641697 1597
2dee4c0b
KH
1598 result = Fcons (v, result);
1599 }
82641697 1600
d311d28c 1601 SAFE_FREE ();
2dee4c0b 1602 return result;
82641697
GM
1603}
1604
2dee4c0b
KH
1605DEFUN ("x-list-fonts", Fx_list_fonts, Sx_list_fonts, 1, 5, 0,
1606 doc: /* Return a list of the names of available fonts matching PATTERN.
1607If optional arguments FACE and FRAME are specified, return only fonts
1608the same size as FACE on FRAME.
21bdb81a
CY
1609
1610PATTERN should be a string containing a font name in the XLFD,
1611Fontconfig, or GTK format. A font name given in the XLFD format may
1612contain wildcard characters:
2dee4c0b
KH
1613 the * character matches any substring, and
1614 the ? character matches any single character.
1615 PATTERN is case-insensitive.
2dee4c0b
KH
1616
1617The return value is a list of strings, suitable as arguments to
d5ab09cd 1618`set-face-font'.
82641697 1619
2dee4c0b
KH
1620Fonts Emacs can't use may or may not be excluded
1621even if they match PATTERN and FACE.
1622The optional fourth argument MAXIMUM sets a limit on how many
1623fonts to match. The first MAXIMUM fonts are reported.
1624The optional fifth argument WIDTH, if specified, is a number of columns
1625occupied by a character of a font. In that case, return only fonts
1626the WIDTH times as wide as FACE on FRAME. */)
b5f03016
AS
1627 (Lisp_Object pattern, Lisp_Object face, Lisp_Object frame,
1628 Lisp_Object maximum, Lisp_Object width)
82641697 1629{
2dee4c0b 1630 struct frame *f;
1e9966ea 1631 int size, avgwidth IF_LINT (= 0);
82641697 1632
7452b7bd 1633 check_window_system (NULL);
2dee4c0b 1634 CHECK_STRING (pattern);
82641697 1635
2dee4c0b
KH
1636 if (! NILP (maximum))
1637 CHECK_NATNUM (maximum);
82641697 1638
2dee4c0b
KH
1639 if (!NILP (width))
1640 CHECK_NUMBER (width);
82641697 1641
7452b7bd
DA
1642 /* We can't simply call decode_window_system_frame because
1643 this function may be called before any frame is created. */
d9f07150 1644 f = decode_live_frame (frame);
2dee4c0b
KH
1645 if (! FRAME_WINDOW_P (f))
1646 {
1647 /* Perhaps we have not yet created any frame. */
1648 f = NULL;
1649 frame = Qnil;
1650 face = Qnil;
1651 }
d9f07150
DA
1652 else
1653 XSETFRAME (frame, f);
82641697 1654
2dee4c0b 1655 /* Determine the width standard for comparison with the fonts we find. */
82641697 1656
2dee4c0b
KH
1657 if (NILP (face))
1658 size = 0;
1659 else
1660 {
1661 /* This is of limited utility since it works with character
1662 widths. Keep it for compatibility. --gerd. */
1663 int face_id = lookup_named_face (f, face, 0);
071048a3
PE
1664 struct face *width_face = (face_id < 0
1665 ? NULL
1666 : FACE_FROM_ID (f, face_id));
82641697 1667
071048a3 1668 if (width_face && width_face->font)
2dee4c0b 1669 {
071048a3
PE
1670 size = width_face->font->pixel_size;
1671 avgwidth = width_face->font->average_width;
2dee4c0b
KH
1672 }
1673 else
1674 {
1675 size = FRAME_FONT (f)->pixel_size;
1676 avgwidth = FRAME_FONT (f)->average_width;
1677 }
1678 if (!NILP (width))
1679 avgwidth *= XINT (width);
1680 }
82641697 1681
2dee4c0b
KH
1682 {
1683 Lisp_Object font_spec;
a4eec626 1684 Lisp_Object args[2], tail;
82641697 1685
2dee4c0b 1686 font_spec = font_spec_from_name (pattern);
21bdb81a
CY
1687 if (!FONTP (font_spec))
1688 signal_error ("Invalid font name", pattern);
1689
2dee4c0b
KH
1690 if (size)
1691 {
1692 Ffont_put (font_spec, QCsize, make_number (size));
1693 Ffont_put (font_spec, QCavgwidth, make_number (avgwidth));
1694 }
34e97272 1695 args[0] = Flist_fonts (font_spec, frame, maximum, font_spec);
a4eec626 1696 for (tail = args[0]; CONSP (tail); tail = XCDR (tail))
493dcf2c
KH
1697 {
1698 Lisp_Object font_entity;
1699
1700 font_entity = XCAR (tail);
1701 if ((NILP (AREF (font_entity, FONT_SIZE_INDEX))
1702 || XINT (AREF (font_entity, FONT_SIZE_INDEX)) == 0)
1703 && ! NILP (AREF (font_spec, FONT_SIZE_INDEX)))
1704 {
1705 /* This is a scalable font. For backward compatibility,
1706 we set the specified size. */
92470028 1707 font_entity = copy_font_spec (font_entity);
493dcf2c
KH
1708 ASET (font_entity, FONT_SIZE_INDEX,
1709 AREF (font_spec, FONT_SIZE_INDEX));
1710 }
1711 XSETCAR (tail, Ffont_xlfd_name (font_entity, Qnil));
1712 }
2dee4c0b
KH
1713 if (NILP (frame))
1714 /* We don't have to check fontsets. */
1715 return args[0];
1716 args[1] = list_fontsets (f, pattern, size);
1717 return Fnconc (2, args);
1718 }
82641697 1719}
178c5d9c 1720
2dee4c0b 1721#endif /* HAVE_WINDOW_SYSTEM */
82641697 1722
82641697
GM
1723\f
1724/***********************************************************************
1725 Lisp Faces
1726 ***********************************************************************/
1727
a08332c0
GM
1728/* Access face attributes of face LFACE, a Lisp vector. */
1729
1730#define LFACE_FAMILY(LFACE) AREF ((LFACE), LFACE_FAMILY_INDEX)
53aaf1e2 1731#define LFACE_FOUNDRY(LFACE) AREF ((LFACE), LFACE_FOUNDRY_INDEX)
a08332c0
GM
1732#define LFACE_HEIGHT(LFACE) AREF ((LFACE), LFACE_HEIGHT_INDEX)
1733#define LFACE_WEIGHT(LFACE) AREF ((LFACE), LFACE_WEIGHT_INDEX)
1734#define LFACE_SLANT(LFACE) AREF ((LFACE), LFACE_SLANT_INDEX)
1735#define LFACE_UNDERLINE(LFACE) AREF ((LFACE), LFACE_UNDERLINE_INDEX)
1736#define LFACE_INVERSE(LFACE) AREF ((LFACE), LFACE_INVERSE_INDEX)
1737#define LFACE_FOREGROUND(LFACE) AREF ((LFACE), LFACE_FOREGROUND_INDEX)
1738#define LFACE_BACKGROUND(LFACE) AREF ((LFACE), LFACE_BACKGROUND_INDEX)
1739#define LFACE_STIPPLE(LFACE) AREF ((LFACE), LFACE_STIPPLE_INDEX)
1740#define LFACE_SWIDTH(LFACE) AREF ((LFACE), LFACE_SWIDTH_INDEX)
1741#define LFACE_OVERLINE(LFACE) AREF ((LFACE), LFACE_OVERLINE_INDEX)
1742#define LFACE_STRIKE_THROUGH(LFACE) AREF ((LFACE), LFACE_STRIKE_THROUGH_INDEX)
1743#define LFACE_BOX(LFACE) AREF ((LFACE), LFACE_BOX_INDEX)
1744#define LFACE_FONT(LFACE) AREF ((LFACE), LFACE_FONT_INDEX)
1745#define LFACE_INHERIT(LFACE) AREF ((LFACE), LFACE_INHERIT_INDEX)
763bc839 1746#define LFACE_FONTSET(LFACE) AREF ((LFACE), LFACE_FONTSET_INDEX)
82641697
GM
1747
1748/* Non-zero if LFACE is a Lisp face. A Lisp face is a vector of size
1749 LFACE_VECTOR_SIZE which has the symbol `face' in slot 0. */
1750
1751#define LFACEP(LFACE) \
1752 (VECTORP (LFACE) \
77b37c05 1753 && ASIZE (LFACE) == LFACE_VECTOR_SIZE \
a08332c0 1754 && EQ (AREF (LFACE, 0), Qface))
82641697 1755
178c5d9c 1756
e509cfa6 1757#ifdef GLYPH_DEBUG
82641697
GM
1758
1759/* Check consistency of Lisp face attribute vector ATTRS. */
1760
1761static void
4973679b 1762check_lface_attrs (Lisp_Object attrs[LFACE_VECTOR_SIZE])
82641697 1763{
a54e2c05 1764 eassert (UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
2ff10663 1765 || IGNORE_DEFFACE_P (attrs[LFACE_FAMILY_INDEX])
82641697 1766 || STRINGP (attrs[LFACE_FAMILY_INDEX]));
a54e2c05 1767 eassert (UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
53aaf1e2
KH
1768 || IGNORE_DEFFACE_P (attrs[LFACE_FOUNDRY_INDEX])
1769 || STRINGP (attrs[LFACE_FOUNDRY_INDEX]));
a54e2c05 1770 eassert (UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
2ff10663 1771 || IGNORE_DEFFACE_P (attrs[LFACE_SWIDTH_INDEX])
82641697 1772 || SYMBOLP (attrs[LFACE_SWIDTH_INDEX]));
a54e2c05 1773 eassert (UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
2ff10663 1774 || IGNORE_DEFFACE_P (attrs[LFACE_HEIGHT_INDEX])
2c20458f
MB
1775 || INTEGERP (attrs[LFACE_HEIGHT_INDEX])
1776 || FLOATP (attrs[LFACE_HEIGHT_INDEX])
1777 || FUNCTIONP (attrs[LFACE_HEIGHT_INDEX]));
a54e2c05 1778 eassert (UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
2ff10663 1779 || IGNORE_DEFFACE_P (attrs[LFACE_WEIGHT_INDEX])
82641697 1780 || SYMBOLP (attrs[LFACE_WEIGHT_INDEX]));
a54e2c05 1781 eassert (UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
2ff10663 1782 || IGNORE_DEFFACE_P (attrs[LFACE_SLANT_INDEX])
82641697 1783 || SYMBOLP (attrs[LFACE_SLANT_INDEX]));
a54e2c05 1784 eassert (UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
2ff10663 1785 || IGNORE_DEFFACE_P (attrs[LFACE_UNDERLINE_INDEX])
82641697 1786 || SYMBOLP (attrs[LFACE_UNDERLINE_INDEX])
9b0e3eba
AA
1787 || STRINGP (attrs[LFACE_UNDERLINE_INDEX])
1788 || CONSP (attrs[LFACE_UNDERLINE_INDEX]));
a54e2c05 1789 eassert (UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
2ff10663 1790 || IGNORE_DEFFACE_P (attrs[LFACE_OVERLINE_INDEX])
82641697
GM
1791 || SYMBOLP (attrs[LFACE_OVERLINE_INDEX])
1792 || STRINGP (attrs[LFACE_OVERLINE_INDEX]));
a54e2c05 1793 eassert (UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
2ff10663 1794 || IGNORE_DEFFACE_P (attrs[LFACE_STRIKE_THROUGH_INDEX])
82641697
GM
1795 || SYMBOLP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1796 || STRINGP (attrs[LFACE_STRIKE_THROUGH_INDEX]));
a54e2c05 1797 eassert (UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
2ff10663 1798 || IGNORE_DEFFACE_P (attrs[LFACE_BOX_INDEX])
82641697
GM
1799 || SYMBOLP (attrs[LFACE_BOX_INDEX])
1800 || STRINGP (attrs[LFACE_BOX_INDEX])
1801 || INTEGERP (attrs[LFACE_BOX_INDEX])
1802 || CONSP (attrs[LFACE_BOX_INDEX]));
a54e2c05 1803 eassert (UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
2ff10663 1804 || IGNORE_DEFFACE_P (attrs[LFACE_INVERSE_INDEX])
82641697 1805 || SYMBOLP (attrs[LFACE_INVERSE_INDEX]));
a54e2c05 1806 eassert (UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
2ff10663 1807 || IGNORE_DEFFACE_P (attrs[LFACE_FOREGROUND_INDEX])
82641697 1808 || STRINGP (attrs[LFACE_FOREGROUND_INDEX]));
a54e2c05 1809 eassert (UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
2ff10663 1810 || IGNORE_DEFFACE_P (attrs[LFACE_BACKGROUND_INDEX])
82641697 1811 || STRINGP (attrs[LFACE_BACKGROUND_INDEX]));
a54e2c05 1812 eassert (UNSPECIFIEDP (attrs[LFACE_INHERIT_INDEX])
2ff10663 1813 || IGNORE_DEFFACE_P (attrs[LFACE_INHERIT_INDEX])
2c20458f
MB
1814 || NILP (attrs[LFACE_INHERIT_INDEX])
1815 || SYMBOLP (attrs[LFACE_INHERIT_INDEX])
1816 || CONSP (attrs[LFACE_INHERIT_INDEX]));
82641697 1817#ifdef HAVE_WINDOW_SYSTEM
a54e2c05 1818 eassert (UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
2ff10663 1819 || IGNORE_DEFFACE_P (attrs[LFACE_STIPPLE_INDEX])
82641697 1820 || SYMBOLP (attrs[LFACE_STIPPLE_INDEX])
fef04523 1821 || !NILP (Fbitmap_spec_p (attrs[LFACE_STIPPLE_INDEX])));
a54e2c05 1822 eassert (UNSPECIFIEDP (attrs[LFACE_FONT_INDEX])
2ff10663 1823 || IGNORE_DEFFACE_P (attrs[LFACE_FONT_INDEX])
2dee4c0b 1824 || FONTP (attrs[LFACE_FONT_INDEX]));
a54e2c05 1825 eassert (UNSPECIFIEDP (attrs[LFACE_FONTSET_INDEX])
f25e39b4
EZ
1826 || STRINGP (attrs[LFACE_FONTSET_INDEX])
1827 || NILP (attrs[LFACE_FONTSET_INDEX]));
82641697
GM
1828#endif
1829}
1830
1831
1832/* Check consistency of attributes of Lisp face LFACE (a Lisp vector). */
1833
1834static void
7d7d0045 1835check_lface (Lisp_Object lface)
82641697
GM
1836{
1837 if (!NILP (lface))
1838 {
a54e2c05 1839 eassert (LFACEP (lface));
82641697
GM
1840 check_lface_attrs (XVECTOR (lface)->contents);
1841 }
1842}
1843
e509cfa6 1844#else /* not GLYPH_DEBUG */
82641697
GM
1845
1846#define check_lface_attrs(attrs) (void) 0
1847#define check_lface(lface) (void) 0
1848
e509cfa6 1849#endif /* GLYPH_DEBUG */
82641697
GM
1850
1851
a0a23346
MB
1852\f
1853/* Face-merge cycle checking. */
1854
f2cec7a9
MB
1855enum named_merge_point_kind
1856{
1857 NAMED_MERGE_POINT_NORMAL,
1858 NAMED_MERGE_POINT_REMAP
1859};
1860
a0a23346
MB
1861/* A `named merge point' is simply a point during face-merging where we
1862 look up a face by name. We keep a stack of which named lookups we're
1863 currently processing so that we can easily detect cycles, using a
1864 linked- list of struct named_merge_point structures, typically
1865 allocated on the stack frame of the named lookup functions which are
1866 active (so no consing is required). */
1867struct named_merge_point
1868{
1869 Lisp_Object face_name;
f2cec7a9 1870 enum named_merge_point_kind named_merge_point_kind;
a0a23346
MB
1871 struct named_merge_point *prev;
1872};
1873
1874
1875/* If a face merging cycle is detected for FACE_NAME, return 0,
1876 otherwise add NEW_NAMED_MERGE_POINT, which is initialized using
f2cec7a9
MB
1877 FACE_NAME and NAMED_MERGE_POINT_KIND, as the head of the linked list
1878 pointed to by NAMED_MERGE_POINTS, and return 1. */
a0a23346 1879
b0ab8123 1880static int
a0a23346
MB
1881push_named_merge_point (struct named_merge_point *new_named_merge_point,
1882 Lisp_Object face_name,
f2cec7a9 1883 enum named_merge_point_kind named_merge_point_kind,
a0a23346
MB
1884 struct named_merge_point **named_merge_points)
1885{
1886 struct named_merge_point *prev;
1887
1888 for (prev = *named_merge_points; prev; prev = prev->prev)
1889 if (EQ (face_name, prev->face_name))
f2cec7a9
MB
1890 {
1891 if (prev->named_merge_point_kind == named_merge_point_kind)
1892 /* A cycle, so fail. */
1893 return 0;
1894 else if (prev->named_merge_point_kind == NAMED_MERGE_POINT_REMAP)
1895 /* A remap `hides ' any previous normal merge points
1896 (because the remap means that it's actually different face),
1897 so as we know the current merge point must be normal, we
1898 can just assume it's OK. */
1899 break;
1900 }
a0a23346
MB
1901
1902 new_named_merge_point->face_name = face_name;
f2cec7a9 1903 new_named_merge_point->named_merge_point_kind = named_merge_point_kind;
a0a23346
MB
1904 new_named_merge_point->prev = *named_merge_points;
1905
1906 *named_merge_points = new_named_merge_point;
1907
1908 return 1;
1909}
1910
1911\f
39506348 1912/* Resolve face name FACE_NAME. If FACE_NAME is a string, intern it
d1bf13b3
KS
1913 to make it a symbol. If FACE_NAME is an alias for another face,
1914 return that face's name.
1915
1916 Return default face in case of errors. */
c7ae3284
GM
1917
1918static Lisp_Object
971de7fb 1919resolve_face_name (Lisp_Object face_name, int signal_p)
c7ae3284 1920{
d1bf13b3
KS
1921 Lisp_Object orig_face;
1922 Lisp_Object tortoise, hare;
178c5d9c 1923
c7ae3284 1924 if (STRINGP (face_name))
42a5b22f 1925 face_name = intern (SSDATA (face_name));
c7ae3284 1926
d1bf13b3
KS
1927 if (NILP (face_name) || !SYMBOLP (face_name))
1928 return face_name;
1929
1930 orig_face = face_name;
1931 tortoise = hare = face_name;
1932
1933 while (1)
c7ae3284 1934 {
d1bf13b3
KS
1935 face_name = hare;
1936 hare = Fget (hare, Qface_alias);
1937 if (NILP (hare) || !SYMBOLP (hare))
c7ae3284 1938 break;
d1bf13b3
KS
1939
1940 face_name = hare;
1941 hare = Fget (hare, Qface_alias);
1942 if (NILP (hare) || !SYMBOLP (hare))
f3745204 1943 break;
d1bf13b3
KS
1944
1945 tortoise = Fget (tortoise, Qface_alias);
1946 if (EQ (hare, tortoise))
1947 {
1948 if (signal_p)
8009eb44 1949 xsignal1 (Qcircular_list, orig_face);
d1bf13b3
KS
1950 return Qdefault;
1951 }
c7ae3284
GM
1952 }
1953
1954 return face_name;
1955}
1956
1957
82641697 1958/* Return the face definition of FACE_NAME on frame F. F null means
45d9f1ef
GM
1959 return the definition for new frames. FACE_NAME may be a string or
1960 a symbol (apparently Emacs 20.2 allowed strings as face names in
f2cec7a9
MB
1961 face text properties; Ediff uses that). If SIGNAL_P is non-zero,
1962 signal an error if FACE_NAME is not a valid face name. If SIGNAL_P
1963 is zero, value is nil if FACE_NAME is not a valid face name. */
b0ab8123 1964static Lisp_Object
b5f03016
AS
1965lface_from_face_name_no_resolve (struct frame *f, Lisp_Object face_name,
1966 int signal_p)
82641697 1967{
c7ae3284 1968 Lisp_Object lface;
82641697
GM
1969
1970 if (f)
e69b0960 1971 lface = assq_no_quit (face_name, f->face_alist);
82641697
GM
1972 else
1973 lface = assq_no_quit (face_name, Vface_new_frame_defaults);
1974
1975 if (CONSP (lface))
1976 lface = XCDR (lface);
1977 else if (signal_p)
1978 signal_error ("Invalid face", face_name);
1979
1980 check_lface (lface);
f2cec7a9 1981
82641697
GM
1982 return lface;
1983}
1984
f2cec7a9
MB
1985/* Return the face definition of FACE_NAME on frame F. F null means
1986 return the definition for new frames. FACE_NAME may be a string or
1987 a symbol (apparently Emacs 20.2 allowed strings as face names in
1988 face text properties; Ediff uses that). If FACE_NAME is an alias
1989 for another face, return that face's definition. If SIGNAL_P is
1990 non-zero, signal an error if FACE_NAME is not a valid face name.
1991 If SIGNAL_P is zero, value is nil if FACE_NAME is not a valid face
1992 name. */
b0ab8123 1993static Lisp_Object
971de7fb 1994lface_from_face_name (struct frame *f, Lisp_Object face_name, int signal_p)
f2cec7a9
MB
1995{
1996 face_name = resolve_face_name (face_name, signal_p);
1997 return lface_from_face_name_no_resolve (f, face_name, signal_p);
1998}
1999
82641697 2000
e7d7fd8c
MB
2001/* Get face attributes of face FACE_NAME from frame-local faces on
2002 frame F. Store the resulting attributes in ATTRS which must point
2003 to a vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If SIGNAL_P
2004 is non-zero, signal an error if FACE_NAME does not name a face.
2005 Otherwise, value is zero if FACE_NAME is not a face. */
2006
b0ab8123 2007static int
b5f03016 2008get_lface_attributes_no_remap (struct frame *f, Lisp_Object face_name,
4973679b
PE
2009 Lisp_Object attrs[LFACE_VECTOR_SIZE],
2010 int signal_p)
e7d7fd8c
MB
2011{
2012 Lisp_Object lface;
e7d7fd8c 2013
f2cec7a9
MB
2014 lface = lface_from_face_name_no_resolve (f, face_name, signal_p);
2015
2016 if (! NILP (lface))
72af86bd
AS
2017 memcpy (attrs, XVECTOR (lface)->contents,
2018 LFACE_VECTOR_SIZE * sizeof *attrs);
f2cec7a9
MB
2019
2020 return !NILP (lface);
2021}
2022
2023/* Get face attributes of face FACE_NAME from frame-local faces on frame
2024 F. Store the resulting attributes in ATTRS which must point to a
2025 vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If FACE_NAME is an
2026 alias for another face, use that face's definition. If SIGNAL_P is
2027 non-zero, signal an error if FACE_NAME does not name a face.
2028 Otherwise, value is zero if FACE_NAME is not a face. */
2029
b0ab8123 2030static int
b5f03016 2031get_lface_attributes (struct frame *f, Lisp_Object face_name,
4973679b 2032 Lisp_Object attrs[LFACE_VECTOR_SIZE], int signal_p,
b5f03016 2033 struct named_merge_point *named_merge_points)
f2cec7a9
MB
2034{
2035 Lisp_Object face_remapping;
2036
2037 face_name = resolve_face_name (face_name, signal_p);
2038
2039 /* See if SYMBOL has been remapped to some other face (usually this
2040 is done buffer-locally). */
2041 face_remapping = assq_no_quit (face_name, Vface_remapping_alist);
2042 if (CONSP (face_remapping))
e7d7fd8c 2043 {
f2cec7a9
MB
2044 struct named_merge_point named_merge_point;
2045
2046 if (push_named_merge_point (&named_merge_point,
2047 face_name, NAMED_MERGE_POINT_REMAP,
2048 &named_merge_points))
2049 {
2050 int i;
2051
2052 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2053 attrs[i] = Qunspecified;
2054
2055 return merge_face_ref (f, XCDR (face_remapping), attrs,
2056 signal_p, named_merge_points);
2057 }
e7d7fd8c 2058 }
e7d7fd8c 2059
f2cec7a9
MB
2060 /* Default case, no remapping. */
2061 return get_lface_attributes_no_remap (f, face_name, attrs, signal_p);
e7d7fd8c
MB
2062}
2063
2064
82641697
GM
2065/* Non-zero if all attributes in face attribute vector ATTRS are
2066 specified, i.e. are non-nil. */
2067
2068static int
4973679b 2069lface_fully_specified_p (Lisp_Object attrs[LFACE_VECTOR_SIZE])
82641697
GM
2070{
2071 int i;
2072
2073 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2dee4c0b 2074 if (i != LFACE_FONT_INDEX && i != LFACE_INHERIT_INDEX)
9e2a2647 2075 if ((UNSPECIFIEDP (attrs[i]) || IGNORE_DEFFACE_P (attrs[i])))
b5f03016 2076 break;
82641697
GM
2077
2078 return i == LFACE_VECTOR_SIZE;
2079}
2080
c3cee013 2081#ifdef HAVE_WINDOW_SYSTEM
82641697 2082
2dee4c0b
KH
2083/* Set font-related attributes of Lisp face LFACE from FONT-OBJECT.
2084 If FORCE_P is zero, set only unspecified attributes of LFACE. The
2085 exception is `font' attribute. It is set to FONT_OBJECT regardless
2086 of FORCE_P. */
178c5d9c 2087
82641697 2088static int
b5f03016
AS
2089set_lface_from_font (struct frame *f, Lisp_Object lface,
2090 Lisp_Object font_object, int force_p)
f4d3bea3 2091{
f4d3bea3 2092 Lisp_Object val;
2dee4c0b 2093 struct font *font = XFONT_OBJECT (font_object);
f4d3bea3
KH
2094
2095 /* Set attributes only if unspecified, otherwise face defaults for
2096 new frames would never take effect. If the font doesn't have a
2097 specific property, set a normal value for that. */
2098
2099 if (force_p || UNSPECIFIEDP (LFACE_FAMILY (lface)))
2100 {
2dee4c0b 2101 Lisp_Object family = AREF (font_object, FONT_FAMILY_INDEX);
f4d3bea3 2102
4939150c 2103 ASET (lface, LFACE_FAMILY_INDEX, SYMBOL_NAME (family));
53aaf1e2
KH
2104 }
2105
2106 if (force_p || UNSPECIFIEDP (LFACE_FOUNDRY (lface)))
2107 {
2108 Lisp_Object foundry = AREF (font_object, FONT_FOUNDRY_INDEX);
2109
4939150c 2110 ASET (lface, LFACE_FOUNDRY_INDEX, SYMBOL_NAME (foundry));
f4d3bea3
KH
2111 }
2112
2113 if (force_p || UNSPECIFIEDP (LFACE_HEIGHT (lface)))
2114 {
42143acd 2115 int pt = PIXEL_TO_POINT (font->pixel_size * 10, FRAME_RES_Y (f));
f4d3bea3 2116
a54e2c05 2117 eassert (pt > 0);
4939150c 2118 ASET (lface, LFACE_HEIGHT_INDEX, make_number (pt));
f4d3bea3
KH
2119 }
2120
f4d3bea3
KH
2121 if (force_p || UNSPECIFIEDP (LFACE_WEIGHT (lface)))
2122 {
2dee4c0b 2123 val = FONT_WEIGHT_FOR_FACE (font_object);
4939150c 2124 ASET (lface, LFACE_WEIGHT_INDEX, ! NILP (val) ? val :Qnormal);
f4d3bea3
KH
2125 }
2126 if (force_p || UNSPECIFIEDP (LFACE_SLANT (lface)))
2127 {
2dee4c0b 2128 val = FONT_SLANT_FOR_FACE (font_object);
4939150c 2129 ASET (lface, LFACE_SLANT_INDEX, ! NILP (val) ? val : Qnormal);
f4d3bea3
KH
2130 }
2131 if (force_p || UNSPECIFIEDP (LFACE_SWIDTH (lface)))
2132 {
2dee4c0b 2133 val = FONT_WIDTH_FOR_FACE (font_object);
4939150c 2134 ASET (lface, LFACE_SWIDTH_INDEX, ! NILP (val) ? val : Qnormal);
f4d3bea3
KH
2135 }
2136
4939150c 2137 ASET (lface, LFACE_FONT_INDEX, font_object);
2dee4c0b 2138 return 1;
f4d3bea3 2139}
f4d3bea3 2140
c3cee013 2141#endif /* HAVE_WINDOW_SYSTEM */
82641697
GM
2142
2143
2c20458f
MB
2144/* Merges the face height FROM with the face height TO, and returns the
2145 merged height. If FROM is an invalid height, then INVALID is
cdfaafa9
MB
2146 returned instead. FROM and TO may be either absolute face heights or
2147 `relative' heights; the returned value is always an absolute height
fe69cf00 2148 unless both FROM and TO are relative. */
2c20458f 2149
6b463e58 2150static Lisp_Object
971de7fb 2151merge_face_heights (Lisp_Object from, Lisp_Object to, Lisp_Object invalid)
2c20458f 2152{
cdfaafa9 2153 Lisp_Object result = invalid;
2c20458f
MB
2154
2155 if (INTEGERP (from))
cdfaafa9
MB
2156 /* FROM is absolute, just use it as is. */
2157 result = from;
2158 else if (FLOATP (from))
2159 /* FROM is a scale, use it to adjust TO. */
2160 {
2161 if (INTEGERP (to))
2162 /* relative X absolute => absolute */
d311d28c 2163 result = make_number (XFLOAT_DATA (from) * XINT (to));
cdfaafa9
MB
2164 else if (FLOATP (to))
2165 /* relative X relative => relative */
2166 result = make_float (XFLOAT_DATA (from) * XFLOAT_DATA (to));
a0a23346
MB
2167 else if (UNSPECIFIEDP (to))
2168 result = from;
2c20458f 2169 }
2c20458f 2170 else if (FUNCTIONP (from))
cdfaafa9 2171 /* FROM is a function, which use to adjust TO. */
2c20458f
MB
2172 {
2173 /* Call function with current height as argument.
2174 From is the new height. */
6cd7a139 2175 result = safe_call1 (from, to);
2c20458f 2176
cdfaafa9
MB
2177 /* Ensure that if TO was absolute, so is the result. */
2178 if (INTEGERP (to) && !INTEGERP (result))
2179 result = invalid;
2c20458f
MB
2180 }
2181
cdfaafa9 2182 return result;
2c20458f
MB
2183}
2184
2185
2186/* Merge two Lisp face attribute vectors on frame F, FROM and TO, and
613fa7f2 2187 store the resulting attributes in TO, which must be already be
e7d7fd8c
MB
2188 completely specified and contain only absolute attributes. Every
2189 specified attribute of FROM overrides the corresponding attribute of
2190 TO; relative attributes in FROM are merged with the absolute value in
2191 TO and replace it. NAMED_MERGE_POINTS is used internally to detect
f2cec7a9
MB
2192 loops in face inheritance/remapping; it should be 0 when called from
2193 other places. */
82641697 2194
b0ab8123 2195static void
b5f03016
AS
2196merge_face_vectors (struct frame *f, Lisp_Object *from, Lisp_Object *to,
2197 struct named_merge_point *named_merge_points)
82641697
GM
2198{
2199 int i;
383dcbf9 2200 Lisp_Object font = Qnil;
2c20458f
MB
2201
2202 /* If FROM inherits from some other faces, merge their attributes into
2203 TO before merging FROM's direct attributes. Note that an :inherit
2204 attribute of `unspecified' is the same as one of nil; we never
2205 merge :inherit attributes, so nil is more correct, but lots of
2206 other code uses `unspecified' as a generic value for face attributes. */
2207 if (!UNSPECIFIEDP (from[LFACE_INHERIT_INDEX])
2208 && !NILP (from[LFACE_INHERIT_INDEX]))
a0a23346 2209 merge_face_ref (f, from[LFACE_INHERIT_INDEX], to, 0, named_merge_points);
2c20458f 2210
383dcbf9 2211 if (FONT_SPEC_P (from[LFACE_FONT_INDEX]))
2dee4c0b 2212 {
383dcbf9
CY
2213 if (!UNSPECIFIEDP (to[LFACE_FONT_INDEX]))
2214 font = merge_font_spec (from[LFACE_FONT_INDEX], to[LFACE_FONT_INDEX]);
2dee4c0b 2215 else
383dcbf9
CY
2216 font = copy_font_spec (from[LFACE_FONT_INDEX]);
2217 to[LFACE_FONT_INDEX] = font;
2dee4c0b 2218 }
87188200 2219
82641697
GM
2220 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2221 if (!UNSPECIFIEDP (from[i]))
ab8469eb
PJ
2222 {
2223 if (i == LFACE_HEIGHT_INDEX && !INTEGERP (from[i]))
2dee4c0b
KH
2224 {
2225 to[i] = merge_face_heights (from[i], to[i], to[i]);
2226 font_clear_prop (to, FONT_SIZE_INDEX);
2227 }
383dcbf9 2228 else if (i != LFACE_FONT_INDEX && ! EQ (to[i], from[i]))
2dee4c0b
KH
2229 {
2230 to[i] = from[i];
2231 if (i >= LFACE_FAMILY_INDEX && i <=LFACE_SLANT_INDEX)
2232 font_clear_prop (to,
2233 (i == LFACE_FAMILY_INDEX ? FONT_FAMILY_INDEX
53aaf1e2 2234 : i == LFACE_FOUNDRY_INDEX ? FONT_FOUNDRY_INDEX
2dee4c0b
KH
2235 : i == LFACE_SWIDTH_INDEX ? FONT_WIDTH_INDEX
2236 : i == LFACE_HEIGHT_INDEX ? FONT_SIZE_INDEX
2237 : i == LFACE_WEIGHT_INDEX ? FONT_WEIGHT_INDEX
2238 : FONT_SLANT_INDEX));
2239 }
ab8469eb 2240 }
2c20458f 2241
383dcbf9
CY
2242 /* If FROM specifies a font spec, make its contents take precedence
2243 over :family and other attributes. This is needed for face
2244 remapping using :font to work. */
2245
2246 if (!NILP (font))
2247 {
2248 if (! NILP (AREF (font, FONT_FOUNDRY_INDEX)))
2249 to[LFACE_FOUNDRY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FOUNDRY_INDEX));
2250 if (! NILP (AREF (font, FONT_FAMILY_INDEX)))
2251 to[LFACE_FAMILY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FAMILY_INDEX));
2252 if (! NILP (AREF (font, FONT_WEIGHT_INDEX)))
2253 to[LFACE_WEIGHT_INDEX] = FONT_WEIGHT_FOR_FACE (font);
2254 if (! NILP (AREF (font, FONT_SLANT_INDEX)))
2255 to[LFACE_SLANT_INDEX] = FONT_SLANT_FOR_FACE (font);
2256 if (! NILP (AREF (font, FONT_WIDTH_INDEX)))
2257 to[LFACE_SWIDTH_INDEX] = FONT_WIDTH_FOR_FACE (font);
2258 ASET (font, FONT_SIZE_INDEX, Qnil);
2259 }
2260
2c20458f
MB
2261 /* TO is always an absolute face, which should inherit from nothing.
2262 We blindly copy the :inherit attribute above and fix it up here. */
2263 to[LFACE_INHERIT_INDEX] = Qnil;
2264}
2265
a0a23346
MB
2266/* Merge the named face FACE_NAME on frame F, into the vector of face
2267 attributes TO. NAMED_MERGE_POINTS is used to detect loops in face
2268 inheritance. Returns true if FACE_NAME is a valid face name and
2269 merging succeeded. */
6288c62f 2270
a0a23346 2271static int
b5f03016
AS
2272merge_named_face (struct frame *f, Lisp_Object face_name, Lisp_Object *to,
2273 struct named_merge_point *named_merge_points)
2c20458f 2274{
a0a23346 2275 struct named_merge_point named_merge_point;
2c20458f 2276
a0a23346 2277 if (push_named_merge_point (&named_merge_point,
f2cec7a9
MB
2278 face_name, NAMED_MERGE_POINT_NORMAL,
2279 &named_merge_points))
2c20458f 2280 {
e1e419ec 2281 struct gcpro gcpro1;
e7d7fd8c 2282 Lisp_Object from[LFACE_VECTOR_SIZE];
f2cec7a9 2283 int ok = get_lface_attributes (f, face_name, from, 0, named_merge_points);
2c20458f 2284
e7d7fd8c
MB
2285 if (ok)
2286 {
2287 GCPRO1 (named_merge_point.face_name);
2288 merge_face_vectors (f, from, to, named_merge_points);
2289 UNGCPRO;
2290 }
2291
2292 return ok;
2c20458f 2293 }
a0a23346
MB
2294 else
2295 return 0;
82641697
GM
2296}
2297
2298
a0a23346
MB
2299/* Merge face attributes from the lisp `face reference' FACE_REF on
2300 frame F into the face attribute vector TO. If ERR_MSGS is non-zero,
2301 problems with FACE_REF cause an error message to be shown. Return
2302 non-zero if no errors occurred (regardless of the value of ERR_MSGS).
2303 NAMED_MERGE_POINTS is used to detect loops in face inheritance or
2304 list structure; it may be 0 for most callers.
2305
2306 FACE_REF may be a single face specification or a list of such
2307 specifications. Each face specification can be:
82641697
GM
2308
2309 1. A symbol or string naming a Lisp face.
2310
2311 2. A property list of the form (KEYWORD VALUE ...) where each
2312 KEYWORD is a face attribute name, and value is an appropriate value
2313 for that attribute.
2314
2315 3. Conses or the form (FOREGROUND-COLOR . COLOR) or
2316 (BACKGROUND-COLOR . COLOR) where COLOR is a color name. This is
2317 for compatibility with 20.2.
2318
2319 Face specifications earlier in lists take precedence over later
2320 specifications. */
178c5d9c 2321
a0a23346 2322static int
b5f03016
AS
2323merge_face_ref (struct frame *f, Lisp_Object face_ref, Lisp_Object *to,
2324 int err_msgs, struct named_merge_point *named_merge_points)
82641697 2325{
a0a23346
MB
2326 int ok = 1; /* Succeed without an error? */
2327
2328 if (CONSP (face_ref))
82641697 2329 {
a0a23346 2330 Lisp_Object first = XCAR (face_ref);
178c5d9c 2331
82641697
GM
2332 if (EQ (first, Qforeground_color)
2333 || EQ (first, Qbackground_color))
2334 {
2335 /* One of (FOREGROUND-COLOR . COLOR) or (BACKGROUND-COLOR
2336 . COLOR). COLOR must be a string. */
a0a23346 2337 Lisp_Object color_name = XCDR (face_ref);
82641697
GM
2338 Lisp_Object color = first;
2339
2340 if (STRINGP (color_name))
2341 {
2342 if (EQ (color, Qforeground_color))
2343 to[LFACE_FOREGROUND_INDEX] = color_name;
2344 else
2345 to[LFACE_BACKGROUND_INDEX] = color_name;
2346 }
2347 else
a0a23346
MB
2348 {
2349 if (err_msgs)
2350 add_to_log ("Invalid face color", color_name, Qnil);
2351 ok = 0;
2352 }
82641697
GM
2353 }
2354 else if (SYMBOLP (first)
d5db4077 2355 && *SDATA (SYMBOL_NAME (first)) == ':')
82641697
GM
2356 {
2357 /* Assume this is the property list form. */
a0a23346 2358 while (CONSP (face_ref) && CONSP (XCDR (face_ref)))
82641697 2359 {
a0a23346
MB
2360 Lisp_Object keyword = XCAR (face_ref);
2361 Lisp_Object value = XCAR (XCDR (face_ref));
2362 int err = 0;
82641697 2363
d9639b25
RS
2364 /* Specifying `unspecified' is a no-op. */
2365 if (EQ (value, Qunspecified))
2366 ;
2367 else if (EQ (keyword, QCfamily))
82641697
GM
2368 {
2369 if (STRINGP (value))
2dee4c0b
KH
2370 {
2371 to[LFACE_FAMILY_INDEX] = value;
2372 font_clear_prop (to, FONT_FAMILY_INDEX);
2373 }
82641697 2374 else
a0a23346 2375 err = 1;
82641697 2376 }
53aaf1e2
KH
2377 else if (EQ (keyword, QCfoundry))
2378 {
2379 if (STRINGP (value))
2380 {
2381 to[LFACE_FOUNDRY_INDEX] = value;
2382 font_clear_prop (to, FONT_FOUNDRY_INDEX);
2383 }
2384 else
2385 err = 1;
2386 }
82641697
GM
2387 else if (EQ (keyword, QCheight))
2388 {
2c20458f 2389 Lisp_Object new_height =
e1e419ec 2390 merge_face_heights (value, to[LFACE_HEIGHT_INDEX], Qnil);
2c20458f 2391
a0a23346 2392 if (! NILP (new_height))
2dee4c0b
KH
2393 {
2394 to[LFACE_HEIGHT_INDEX] = new_height;
2395 font_clear_prop (to, FONT_SIZE_INDEX);
2396 }
a0a23346
MB
2397 else
2398 err = 1;
82641697
GM
2399 }
2400 else if (EQ (keyword, QCweight))
2401 {
2dee4c0b
KH
2402 if (SYMBOLP (value) && FONT_WEIGHT_NAME_NUMERIC (value) >= 0)
2403 {
2404 to[LFACE_WEIGHT_INDEX] = value;
2405 font_clear_prop (to, FONT_WEIGHT_INDEX);
2406 }
82641697 2407 else
a0a23346 2408 err = 1;
82641697
GM
2409 }
2410 else if (EQ (keyword, QCslant))
2411 {
2dee4c0b
KH
2412 if (SYMBOLP (value) && FONT_SLANT_NAME_NUMERIC (value) >= 0)
2413 {
2414 to[LFACE_SLANT_INDEX] = value;
2415 font_clear_prop (to, FONT_SLANT_INDEX);
2416 }
82641697 2417 else
a0a23346 2418 err = 1;
82641697
GM
2419 }
2420 else if (EQ (keyword, QCunderline))
2421 {
2422 if (EQ (value, Qt)
2423 || NILP (value)
9b0e3eba
AA
2424 || STRINGP (value)
2425 || CONSP (value))
82641697
GM
2426 to[LFACE_UNDERLINE_INDEX] = value;
2427 else
a0a23346 2428 err = 1;
82641697
GM
2429 }
2430 else if (EQ (keyword, QCoverline))
2431 {
2432 if (EQ (value, Qt)
2433 || NILP (value)
2434 || STRINGP (value))
2435 to[LFACE_OVERLINE_INDEX] = value;
2436 else
a0a23346 2437 err = 1;
82641697
GM
2438 }
2439 else if (EQ (keyword, QCstrike_through))
2440 {
2441 if (EQ (value, Qt)
2442 || NILP (value)
2443 || STRINGP (value))
2444 to[LFACE_STRIKE_THROUGH_INDEX] = value;
2445 else
a0a23346 2446 err = 1;
82641697
GM
2447 }
2448 else if (EQ (keyword, QCbox))
2449 {
2450 if (EQ (value, Qt))
2451 value = make_number (1);
2452 if (INTEGERP (value)
2453 || STRINGP (value)
2454 || CONSP (value)
2455 || NILP (value))
2456 to[LFACE_BOX_INDEX] = value;
2457 else
a0a23346 2458 err = 1;
82641697
GM
2459 }
2460 else if (EQ (keyword, QCinverse_video)
2461 || EQ (keyword, QCreverse_video))
2462 {
2463 if (EQ (value, Qt) || NILP (value))
2464 to[LFACE_INVERSE_INDEX] = value;
2465 else
a0a23346 2466 err = 1;
82641697
GM
2467 }
2468 else if (EQ (keyword, QCforeground))
2469 {
2470 if (STRINGP (value))
2471 to[LFACE_FOREGROUND_INDEX] = value;
2472 else
a0a23346 2473 err = 1;
82641697
GM
2474 }
2475 else if (EQ (keyword, QCbackground))
2476 {
2477 if (STRINGP (value))
2478 to[LFACE_BACKGROUND_INDEX] = value;
2479 else
a0a23346 2480 err = 1;
82641697
GM
2481 }
2482 else if (EQ (keyword, QCstipple))
2483 {
17a2cbbd 2484#if defined (HAVE_WINDOW_SYSTEM)
fef04523 2485 Lisp_Object pixmap_p = Fbitmap_spec_p (value);
82641697
GM
2486 if (!NILP (pixmap_p))
2487 to[LFACE_STIPPLE_INDEX] = value;
2488 else
a0a23346 2489 err = 1;
17a2cbbd 2490#endif /* HAVE_WINDOW_SYSTEM */
82641697
GM
2491 }
2492 else if (EQ (keyword, QCwidth))
2493 {
2dee4c0b
KH
2494 if (SYMBOLP (value) && FONT_WIDTH_NAME_NUMERIC (value) >= 0)
2495 {
2496 to[LFACE_SWIDTH_INDEX] = value;
2497 font_clear_prop (to, FONT_WIDTH_INDEX);
2498 }
82641697 2499 else
a0a23346 2500 err = 1;
82641697 2501 }
383dcbf9
CY
2502 else if (EQ (keyword, QCfont))
2503 {
2504 if (FONTP (value))
2505 to[LFACE_FONT_INDEX] = value;
2506 else
2507 err = 1;
2508 }
2c20458f
MB
2509 else if (EQ (keyword, QCinherit))
2510 {
a0a23346
MB
2511 /* This is not really very useful; it's just like a
2512 normal face reference. */
2513 if (! merge_face_ref (f, value, to,
2514 err_msgs, named_merge_points))
2515 err = 1;
2c20458f 2516 }
82641697 2517 else
a0a23346 2518 err = 1;
82641697 2519
a0a23346
MB
2520 if (err)
2521 {
2522 add_to_log ("Invalid face attribute %S %S", keyword, value);
2523 ok = 0;
2524 }
82641697 2525
a0a23346 2526 face_ref = XCDR (XCDR (face_ref));
82641697
GM
2527 }
2528 }
2529 else
2530 {
a0a23346
MB
2531 /* This is a list of face refs. Those at the beginning of the
2532 list take precedence over what follows, so we have to merge
2533 from the end backwards. */
2534 Lisp_Object next = XCDR (face_ref);
2535
2536 if (! NILP (next))
2537 ok = merge_face_ref (f, next, to, err_msgs, named_merge_points);
2538
2539 if (! merge_face_ref (f, first, to, err_msgs, named_merge_points))
2540 ok = 0;
82641697
GM
2541 }
2542 }
2543 else
2544 {
a0a23346
MB
2545 /* FACE_REF ought to be a face name. */
2546 ok = merge_named_face (f, face_ref, to, named_merge_points);
2547 if (!ok && err_msgs)
2548 add_to_log ("Invalid face reference: %s", face_ref, Qnil);
82641697 2549 }
a0a23346
MB
2550
2551 return ok;
82641697
GM
2552}
2553
2554
2555DEFUN ("internal-make-lisp-face", Finternal_make_lisp_face,
2556 Sinternal_make_lisp_face, 1, 2, 0,
7ee72033 2557 doc: /* Make FACE, a symbol, a Lisp face with all attributes nil.
228299fa
GM
2558If FACE was not known as a face before, create a new one.
2559If optional argument FRAME is specified, make a frame-local face
2560for that frame. Otherwise operate on the global face definition.
7ee72033 2561Value is a vector of face attributes. */)
5842a27b 2562 (Lisp_Object face, Lisp_Object frame)
82641697
GM
2563{
2564 Lisp_Object global_lface, lface;
2565 struct frame *f;
2566 int i;
2567
b7826503 2568 CHECK_SYMBOL (face);
82641697 2569 global_lface = lface_from_face_name (NULL, face, 0);
178c5d9c 2570
82641697
GM
2571 if (!NILP (frame))
2572 {
b7826503 2573 CHECK_LIVE_FRAME (frame);
82641697
GM
2574 f = XFRAME (frame);
2575 lface = lface_from_face_name (f, face, 0);
2576 }
2577 else
2578 f = NULL, lface = Qnil;
2579
2580 /* Add a global definition if there is none. */
2581 if (NILP (global_lface))
2582 {
2583 global_lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2584 Qunspecified);
dfae5f9a 2585 ASET (global_lface, 0, Qface);
178c5d9c 2586 Vface_new_frame_defaults = Fcons (Fcons (face, global_lface),
82641697 2587 Vface_new_frame_defaults);
178c5d9c 2588
82641697
GM
2589 /* Assign the new Lisp face a unique ID. The mapping from Lisp
2590 face id to Lisp face is given by the vector lface_id_to_name.
2591 The mapping from Lisp face to Lisp face id is given by the
2592 property `face' of the Lisp face name. */
2593 if (next_lface_id == lface_id_to_name_size)
0065d054 2594 lface_id_to_name =
d311d28c 2595 xpalloc (lface_id_to_name, &lface_id_to_name_size, 1, MAX_FACE_ID,
0065d054 2596 sizeof *lface_id_to_name);
178c5d9c 2597
82641697
GM
2598 lface_id_to_name[next_lface_id] = face;
2599 Fput (face, Qface, make_number (next_lface_id));
2600 ++next_lface_id;
2601 }
2602 else if (f == NULL)
2603 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
dfae5f9a 2604 ASET (global_lface, i, Qunspecified);
178c5d9c 2605
82641697
GM
2606 /* Add a frame-local definition. */
2607 if (f)
2608 {
2609 if (NILP (lface))
2610 {
2611 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2612 Qunspecified);
dfae5f9a 2613 ASET (lface, 0, Qface);
f00af5b1 2614 fset_face_alist (f, Fcons (Fcons (face, lface), f->face_alist));
82641697
GM
2615 }
2616 else
2617 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
dfae5f9a 2618 ASET (lface, i, Qunspecified);
82641697
GM
2619 }
2620 else
2621 lface = global_lface;
2622
33565969
RS
2623 /* Changing a named face means that all realized faces depending on
2624 that face are invalid. Since we cannot tell which realized faces
2625 depend on the face, make sure they are all removed. This is done
2626 by incrementing face_change_count. The next call to
2627 init_iterator will then free realized faces. */
46b00436
KS
2628 if (NILP (Fget (face, Qface_no_inherit)))
2629 {
2630 ++face_change_count;
2631 ++windows_or_buffers_changed;
2632 }
33565969 2633
a54e2c05 2634 eassert (LFACEP (lface));
82641697
GM
2635 check_lface (lface);
2636 return lface;
2637}
2638
2639
2640DEFUN ("internal-lisp-face-p", Finternal_lisp_face_p,
2641 Sinternal_lisp_face_p, 1, 2, 0,
7ee72033 2642 doc: /* Return non-nil if FACE names a face.
b84ce3c5 2643FACE should be a symbol or string.
1d256714 2644If optional second argument FRAME is non-nil, check for the
228299fa 2645existence of a frame-local face with name FACE on that frame.
7ee72033 2646Otherwise check for the existence of a global face. */)
5842a27b 2647 (Lisp_Object face, Lisp_Object frame)
82641697
GM
2648{
2649 Lisp_Object lface;
178c5d9c 2650
65fddb74
JB
2651 face = resolve_face_name (face, 1);
2652
82641697
GM
2653 if (!NILP (frame))
2654 {
b7826503 2655 CHECK_LIVE_FRAME (frame);
82641697
GM
2656 lface = lface_from_face_name (XFRAME (frame), face, 0);
2657 }
2658 else
2659 lface = lface_from_face_name (NULL, face, 0);
2660
2661 return lface;
2662}
2663
2664
2665DEFUN ("internal-copy-lisp-face", Finternal_copy_lisp_face,
2666 Sinternal_copy_lisp_face, 4, 4, 0,
7ee72033 2667 doc: /* Copy face FROM to TO.
5e9bab6f
RS
2668If FRAME is t, copy the global face definition of FROM.
2669Otherwise, copy the frame-local definition of FROM on FRAME.
2670If NEW-FRAME is a frame, copy that data into the frame-local
d5ab09cd 2671definition of TO on NEW-FRAME. If NEW-FRAME is nil,
5e9bab6f 2672FRAME controls where the data is copied to.
228299fa 2673
5e9bab6f 2674The value is TO. */)
5842a27b 2675 (Lisp_Object from, Lisp_Object to, Lisp_Object frame, Lisp_Object new_frame)
82641697
GM
2676{
2677 Lisp_Object lface, copy;
178c5d9c 2678
b7826503
PJ
2679 CHECK_SYMBOL (from);
2680 CHECK_SYMBOL (to);
82641697
GM
2681
2682 if (EQ (frame, Qt))
2683 {
2684 /* Copy global definition of FROM. We don't make copies of
2685 strings etc. because 20.2 didn't do it either. */
2686 lface = lface_from_face_name (NULL, from, 1);
2687 copy = Finternal_make_lisp_face (to, Qnil);
2688 }
2689 else
2690 {
2691 /* Copy frame-local definition of FROM. */
5e9bab6f
RS
2692 if (NILP (new_frame))
2693 new_frame = frame;
b7826503
PJ
2694 CHECK_LIVE_FRAME (frame);
2695 CHECK_LIVE_FRAME (new_frame);
82641697
GM
2696 lface = lface_from_face_name (XFRAME (frame), from, 1);
2697 copy = Finternal_make_lisp_face (to, new_frame);
2698 }
178c5d9c 2699
086ca913 2700 vcopy (copy, 0, XVECTOR (lface)->contents, LFACE_VECTOR_SIZE);
178c5d9c 2701
33565969
RS
2702 /* Changing a named face means that all realized faces depending on
2703 that face are invalid. Since we cannot tell which realized faces
2704 depend on the face, make sure they are all removed. This is done
2705 by incrementing face_change_count. The next call to
2706 init_iterator will then free realized faces. */
46b00436
KS
2707 if (NILP (Fget (to, Qface_no_inherit)))
2708 {
2709 ++face_change_count;
2710 ++windows_or_buffers_changed;
2711 }
33565969 2712
82641697
GM
2713 return to;
2714}
2715
2716
2717DEFUN ("internal-set-lisp-face-attribute", Finternal_set_lisp_face_attribute,
2718 Sinternal_set_lisp_face_attribute, 3, 4, 0,
7ee72033 2719 doc: /* Set attribute ATTR of FACE to VALUE.
228299fa
GM
2720FRAME being a frame means change the face on that frame.
2721FRAME nil means change the face of the selected frame.
2722FRAME t means change the default for new frames.
2723FRAME 0 means change the face on all frames, and change the default
7ee72033 2724 for new frames. */)
5842a27b 2725 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
82641697
GM
2726{
2727 Lisp_Object lface;
2728 Lisp_Object old_value = Qnil;
2dee4c0b
KH
2729 /* Set one of enum font_property_index (> 0) if ATTR is one of
2730 font-related attributes other than QCfont and QCfontset. */
2731 enum font_property_index prop_index = 0;
178c5d9c 2732
b7826503
PJ
2733 CHECK_SYMBOL (face);
2734 CHECK_SYMBOL (attr);
82641697 2735
d1bf13b3 2736 face = resolve_face_name (face, 1);
c7ae3284 2737
628436fb
GM
2738 /* If FRAME is 0, change face on all frames, and change the
2739 default for new frames. */
2740 if (INTEGERP (frame) && XINT (frame) == 0)
2741 {
2742 Lisp_Object tail;
d12d0a9b 2743 Finternal_set_lisp_face_attribute (face, attr, value, Qt);
628436fb
GM
2744 FOR_EACH_FRAME (tail, frame)
2745 Finternal_set_lisp_face_attribute (face, attr, value, frame);
d12d0a9b 2746 return face;
628436fb
GM
2747 }
2748
82641697
GM
2749 /* Set lface to the Lisp attribute vector of FACE. */
2750 if (EQ (frame, Qt))
0268cef3
CY
2751 {
2752 lface = lface_from_face_name (NULL, face, 1);
67d23e01
CY
2753
2754 /* When updating face-new-frame-defaults, we put :ignore-defface
2755 where the caller wants `unspecified'. This forces the frame
2756 defaults to ignore the defface value. Otherwise, the defface
2757 will take effect, which is generally not what is intended.
2758 The value of that attribute will be inherited from some other
2759 face during face merging. See internal_merge_in_global_face. */
0268cef3 2760 if (UNSPECIFIEDP (value))
a3720aa2 2761 value = QCignore_defface;
0268cef3 2762 }
82641697
GM
2763 else
2764 {
2765 if (NILP (frame))
c0617987 2766 frame = selected_frame;
178c5d9c 2767
b7826503 2768 CHECK_LIVE_FRAME (frame);
82641697 2769 lface = lface_from_face_name (XFRAME (frame), face, 0);
178c5d9c 2770
82641697
GM
2771 /* If a frame-local face doesn't exist yet, create one. */
2772 if (NILP (lface))
2773 lface = Finternal_make_lisp_face (face, frame);
2774 }
2775
2776 if (EQ (attr, QCfamily))
2777 {
2ff10663 2778 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
82641697 2779 {
b7826503 2780 CHECK_STRING (value);
d5db4077 2781 if (SCHARS (value) == 0)
82641697
GM
2782 signal_error ("Invalid face family", value);
2783 }
2784 old_value = LFACE_FAMILY (lface);
4939150c 2785 ASET (lface, LFACE_FAMILY_INDEX, value);
2dee4c0b 2786 prop_index = FONT_FAMILY_INDEX;
82641697 2787 }
53aaf1e2
KH
2788 else if (EQ (attr, QCfoundry))
2789 {
2790 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2791 {
2792 CHECK_STRING (value);
2793 if (SCHARS (value) == 0)
2794 signal_error ("Invalid face foundry", value);
2795 }
2796 old_value = LFACE_FOUNDRY (lface);
4939150c 2797 ASET (lface, LFACE_FOUNDRY_INDEX, value);
53aaf1e2
KH
2798 prop_index = FONT_FOUNDRY_INDEX;
2799 }
82641697
GM
2800 else if (EQ (attr, QCheight))
2801 {
2ff10663 2802 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
82641697 2803 {
e66d5363
CY
2804 if (EQ (face, Qdefault))
2805 {
2806 /* The default face must have an absolute size. */
2807 if (!INTEGERP (value) || XINT (value) <= 0)
b5f03016
AS
2808 signal_error ("Default face height not absolute and positive",
2809 value);
e66d5363
CY
2810 }
2811 else
2812 {
2813 /* For non-default faces, do a test merge with a random
2814 height to see if VALUE's ok. */
2815 Lisp_Object test = merge_face_heights (value,
2816 make_number (10),
2817 Qnil);
2818 if (!INTEGERP (test) || XINT (test) <= 0)
b5f03016
AS
2819 signal_error ("Face height does not produce a positive integer",
2820 value);
e66d5363 2821 }
82641697 2822 }
2c20458f 2823
82641697 2824 old_value = LFACE_HEIGHT (lface);
4939150c 2825 ASET (lface, LFACE_HEIGHT_INDEX, value);
2dee4c0b 2826 prop_index = FONT_SIZE_INDEX;
82641697
GM
2827 }
2828 else if (EQ (attr, QCweight))
2829 {
2ff10663 2830 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
82641697 2831 {
b7826503 2832 CHECK_SYMBOL (value);
2dee4c0b 2833 if (FONT_WEIGHT_NAME_NUMERIC (value) < 0)
82641697
GM
2834 signal_error ("Invalid face weight", value);
2835 }
2836 old_value = LFACE_WEIGHT (lface);
4939150c 2837 ASET (lface, LFACE_WEIGHT_INDEX, value);
2dee4c0b 2838 prop_index = FONT_WEIGHT_INDEX;
82641697
GM
2839 }
2840 else if (EQ (attr, QCslant))
2841 {
2ff10663 2842 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
82641697 2843 {
b7826503 2844 CHECK_SYMBOL (value);
2dee4c0b 2845 if (FONT_SLANT_NAME_NUMERIC (value) < 0)
82641697
GM
2846 signal_error ("Invalid face slant", value);
2847 }
2848 old_value = LFACE_SLANT (lface);
4939150c 2849 ASET (lface, LFACE_SLANT_INDEX, value);
2dee4c0b 2850 prop_index = FONT_SLANT_INDEX;
82641697
GM
2851 }
2852 else if (EQ (attr, QCunderline))
2853 {
d7e6881a 2854 bool valid_p = 0;
38182d90 2855
9b0e3eba
AA
2856 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2857 valid_p = 1;
2858 else if (NILP (value) || EQ (value, Qt))
2859 valid_p = 1;
2860 else if (STRINGP (value) && SCHARS (value) > 0)
2861 valid_p = 1;
2862 else if (CONSP (value))
2863 {
2864 Lisp_Object key, val, list;
2865
2866 list = value;
bde3c6c0
GM
2867 /* FIXME? This errs on the side of acceptance. Eg it accepts:
2868 (defface foo '((t :underline 'foo) "doc")
2869 Maybe this is intentional, maybe it isn't.
2870 Non-nil symbols other than t are not documented as being valid.
2871 Eg compare with inverse-video, which explicitly rejects them.
2872 */
9b0e3eba
AA
2873 valid_p = 1;
2874
2875 while (!NILP (CAR_SAFE(list)))
2876 {
2877 key = CAR_SAFE (list);
2878 list = CDR_SAFE (list);
2879 val = CAR_SAFE (list);
2880 list = CDR_SAFE (list);
38182d90
PE
2881
2882 if (NILP (key) || NILP (val))
9b0e3eba
AA
2883 {
2884 valid_p = 0;
2885 break;
2886 }
2887
2888 else if (EQ (key, QCcolor)
2889 && !(EQ (val, Qforeground_color)
2890 || (STRINGP (val) && SCHARS (val) > 0)))
2891 {
2892 valid_p = 0;
2893 break;
2894 }
38182d90
PE
2895
2896 else if (EQ (key, QCstyle)
9b0e3eba
AA
2897 && !(EQ (val, Qline) || EQ (val, Qwave)))
2898 {
2899 valid_p = 0;
2900 break;
2901 }
2902 }
2903 }
38182d90 2904
9b0e3eba
AA
2905 if (!valid_p)
2906 signal_error ("Invalid face underline", value);
38182d90 2907
82641697 2908 old_value = LFACE_UNDERLINE (lface);
4939150c 2909 ASET (lface, LFACE_UNDERLINE_INDEX, value);
82641697
GM
2910 }
2911 else if (EQ (attr, QCoverline))
2912 {
2ff10663 2913 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
82641697
GM
2914 if ((SYMBOLP (value)
2915 && !EQ (value, Qt)
2916 && !EQ (value, Qnil))
2917 /* Overline color. */
2918 || (STRINGP (value)
d5db4077 2919 && SCHARS (value) == 0))
82641697 2920 signal_error ("Invalid face overline", value);
178c5d9c 2921
82641697 2922 old_value = LFACE_OVERLINE (lface);
4939150c 2923 ASET (lface, LFACE_OVERLINE_INDEX, value);
82641697
GM
2924 }
2925 else if (EQ (attr, QCstrike_through))
2926 {
2ff10663 2927 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
82641697
GM
2928 if ((SYMBOLP (value)
2929 && !EQ (value, Qt)
2930 && !EQ (value, Qnil))
2931 /* Strike-through color. */
2932 || (STRINGP (value)
d5db4077 2933 && SCHARS (value) == 0))
82641697 2934 signal_error ("Invalid face strike-through", value);
178c5d9c 2935
82641697 2936 old_value = LFACE_STRIKE_THROUGH (lface);
4939150c 2937 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, value);
82641697
GM
2938 }
2939 else if (EQ (attr, QCbox))
2940 {
d7e6881a 2941 bool valid_p;
178c5d9c 2942
82641697 2943 /* Allow t meaning a simple box of width 1 in foreground color
b5f03016 2944 of the face. */
82641697
GM
2945 if (EQ (value, Qt))
2946 value = make_number (1);
2947
2ff10663 2948 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
82641697
GM
2949 valid_p = 1;
2950 else if (NILP (value))
2951 valid_p = 1;
2952 else if (INTEGERP (value))
89624b8b 2953 valid_p = XINT (value) != 0;
82641697 2954 else if (STRINGP (value))
d5db4077 2955 valid_p = SCHARS (value) > 0;
82641697
GM
2956 else if (CONSP (value))
2957 {
2958 Lisp_Object tem;
178c5d9c 2959
82641697
GM
2960 tem = value;
2961 while (CONSP (tem))
2962 {
2963 Lisp_Object k, v;
2964
2965 k = XCAR (tem);
2966 tem = XCDR (tem);
2967 if (!CONSP (tem))
2968 break;
2969 v = XCAR (tem);
2970 tem = XCDR (tem);
178c5d9c 2971
82641697
GM
2972 if (EQ (k, QCline_width))
2973 {
89624b8b 2974 if (!INTEGERP (v) || XINT (v) == 0)
82641697
GM
2975 break;
2976 }
2977 else if (EQ (k, QCcolor))
2978 {
91c212f1 2979 if (!NILP (v) && (!STRINGP (v) || SCHARS (v) == 0))
82641697
GM
2980 break;
2981 }
2982 else if (EQ (k, QCstyle))
2983 {
2984 if (!EQ (v, Qpressed_button) && !EQ (v, Qreleased_button))
2985 break;
2986 }
2987 else
2988 break;
2989 }
2990
2991 valid_p = NILP (tem);
2992 }
2993 else
2994 valid_p = 0;
2995
2996 if (!valid_p)
2997 signal_error ("Invalid face box", value);
178c5d9c 2998
82641697 2999 old_value = LFACE_BOX (lface);
4939150c 3000 ASET (lface, LFACE_BOX_INDEX, value);
82641697
GM
3001 }
3002 else if (EQ (attr, QCinverse_video)
3003 || EQ (attr, QCreverse_video))
3004 {
2ff10663 3005 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
82641697 3006 {
b7826503 3007 CHECK_SYMBOL (value);
82641697
GM
3008 if (!EQ (value, Qt) && !NILP (value))
3009 signal_error ("Invalid inverse-video face attribute value", value);
3010 }
3011 old_value = LFACE_INVERSE (lface);
4939150c 3012 ASET (lface, LFACE_INVERSE_INDEX, value);
82641697
GM
3013 }
3014 else if (EQ (attr, QCforeground))
3015 {
ca6888b6
CY
3016 /* Compatibility with 20.x. */
3017 if (NILP (value))
3018 value = Qunspecified;
2ff10663 3019 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
82641697
GM
3020 {
3021 /* Don't check for valid color names here because it depends
3022 on the frame (display) whether the color will be valid
3023 when the face is realized. */
b7826503 3024 CHECK_STRING (value);
d5db4077 3025 if (SCHARS (value) == 0)
82641697
GM
3026 signal_error ("Empty foreground color value", value);
3027 }
3028 old_value = LFACE_FOREGROUND (lface);
4939150c 3029 ASET (lface, LFACE_FOREGROUND_INDEX, value);
82641697
GM
3030 }
3031 else if (EQ (attr, QCbackground))
3032 {
ca6888b6
CY
3033 /* Compatibility with 20.x. */
3034 if (NILP (value))
3035 value = Qunspecified;
2ff10663 3036 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
82641697
GM
3037 {
3038 /* Don't check for valid color names here because it depends
3039 on the frame (display) whether the color will be valid
3040 when the face is realized. */
b7826503 3041 CHECK_STRING (value);
d5db4077 3042 if (SCHARS (value) == 0)
82641697
GM
3043 signal_error ("Empty background color value", value);
3044 }
3045 old_value = LFACE_BACKGROUND (lface);
4939150c 3046 ASET (lface, LFACE_BACKGROUND_INDEX, value);
82641697
GM
3047 }
3048 else if (EQ (attr, QCstipple))
3049 {
17a2cbbd 3050#if defined (HAVE_WINDOW_SYSTEM)
2ff10663 3051 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
82641697 3052 && !NILP (value)
fef04523 3053 && NILP (Fbitmap_spec_p (value)))
82641697
GM
3054 signal_error ("Invalid stipple attribute", value);
3055 old_value = LFACE_STIPPLE (lface);
4939150c 3056 ASET (lface, LFACE_STIPPLE_INDEX, value);
17a2cbbd 3057#endif /* HAVE_WINDOW_SYSTEM */
82641697
GM
3058 }
3059 else if (EQ (attr, QCwidth))
3060 {
2ff10663 3061 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
82641697 3062 {
b7826503 3063 CHECK_SYMBOL (value);
2dee4c0b 3064 if (FONT_WIDTH_NAME_NUMERIC (value) < 0)
82641697
GM
3065 signal_error ("Invalid face width", value);
3066 }
3067 old_value = LFACE_SWIDTH (lface);
4939150c 3068 ASET (lface, LFACE_SWIDTH_INDEX, value);
2dee4c0b 3069 prop_index = FONT_WIDTH_INDEX;
82641697 3070 }
2dee4c0b 3071 else if (EQ (attr, QCfont))
82641697 3072 {
c3cee013 3073#ifdef HAVE_WINDOW_SYSTEM
3d90c96c 3074 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
96fbd2c6 3075 {
2dee4c0b 3076 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
f4d3bea3 3077 {
a10c8269 3078 struct frame *f;
2dee4c0b
KH
3079
3080 old_value = LFACE_FONT (lface);
3081 if (! FONTP (value))
f4d3bea3 3082 {
2dee4c0b
KH
3083 if (STRINGP (value))
3084 {
21bdb81a
CY
3085 Lisp_Object name = value;
3086 int fontset = fs_query_fontset (name, 0);
2dee4c0b
KH
3087
3088 if (fontset >= 0)
21bdb81a
CY
3089 name = fontset_ascii (fontset);
3090 value = font_spec_from_name (name);
3091 if (!FONTP (value))
3092 signal_error ("Invalid font name", name);
2dee4c0b
KH
3093 }
3094 else
3095 signal_error ("Invalid font or font-spec", value);
f4d3bea3 3096 }
2dee4c0b
KH
3097 if (EQ (frame, Qt))
3098 f = XFRAME (selected_frame);
f4d3bea3 3099 else
2dee4c0b
KH
3100 f = XFRAME (frame);
3101 if (! FONT_OBJECT_P (value))
f4d3bea3 3102 {
2dee4c0b 3103 Lisp_Object *attrs = XVECTOR (lface)->contents;
f4d3bea3
KH
3104 Lisp_Object font_object;
3105
2dee4c0b 3106 font_object = font_load_for_lface (f, attrs, value);
db1c10a2 3107 if (NILP (font_object))
2dee4c0b
KH
3108 signal_error ("Font not available", value);
3109 value = font_object;
f4d3bea3 3110 }
2dee4c0b 3111 set_lface_from_font (f, lface, value, 1);
f4d3bea3
KH
3112 }
3113 else
4939150c 3114 ASET (lface, LFACE_FONT_INDEX, value);
2dee4c0b
KH
3115 }
3116#endif /* HAVE_WINDOW_SYSTEM */
3117 }
3118 else if (EQ (attr, QCfontset))
3119 {
3120#ifdef HAVE_WINDOW_SYSTEM
3121 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
3122 {
3123 Lisp_Object tmp;
39506348 3124
2dee4c0b
KH
3125 old_value = LFACE_FONTSET (lface);
3126 tmp = Fquery_fontset (value, Qnil);
3127 if (NILP (tmp))
3128 signal_error ("Invalid fontset name", value);
4939150c 3129 ASET (lface, LFACE_FONTSET_INDEX, value = tmp);
96fbd2c6 3130 }
c3cee013 3131#endif /* HAVE_WINDOW_SYSTEM */
82641697 3132 }
2c20458f
MB
3133 else if (EQ (attr, QCinherit))
3134 {
3135 Lisp_Object tail;
3136 if (SYMBOLP (value))
3137 tail = Qnil;
3138 else
3139 for (tail = value; CONSP (tail); tail = XCDR (tail))
3140 if (!SYMBOLP (XCAR (tail)))
3141 break;
3142 if (NILP (tail))
4939150c 3143 ASET (lface, LFACE_INHERIT_INDEX, value);
2c20458f 3144 else
6288c62f 3145 signal_error ("Invalid face inheritance", value);
2c20458f 3146 }
82641697
GM
3147 else if (EQ (attr, QCbold))
3148 {
3149 old_value = LFACE_WEIGHT (lface);
4939150c 3150 ASET (lface, LFACE_WEIGHT_INDEX, NILP (value) ? Qnormal : Qbold);
2dee4c0b 3151 prop_index = FONT_WEIGHT_INDEX;
82641697
GM
3152 }
3153 else if (EQ (attr, QCitalic))
3154 {
2dee4c0b 3155 attr = QCslant;
82641697 3156 old_value = LFACE_SLANT (lface);
4939150c 3157 ASET (lface, LFACE_SLANT_INDEX, NILP (value) ? Qnormal : Qitalic);
2dee4c0b 3158 prop_index = FONT_SLANT_INDEX;
82641697
GM
3159 }
3160 else
3161 signal_error ("Invalid face attribute name", attr);
3162
2dee4c0b 3163 if (prop_index)
e69a00b7
CY
3164 {
3165 /* If a font-related attribute other than QCfont and QCfontset
3166 is specified, and if the original QCfont attribute has a font
3167 (font-spec or font-object), set the corresponding property in
3168 the font to nil so that the font selector doesn't think that
3169 the attribute is mandatory. Also, clear the average
3170 width. */
3171 font_clear_prop (XVECTOR (lface)->contents, prop_index);
e69a00b7 3172 }
39506348 3173
82641697
GM
3174 /* Changing a named face means that all realized faces depending on
3175 that face are invalid. Since we cannot tell which realized faces
3176 depend on the face, make sure they are all removed. This is done
3177 by incrementing face_change_count. The next call to
3178 init_iterator will then free realized faces. */
3179 if (!EQ (frame, Qt)
46b00436 3180 && NILP (Fget (face, Qface_no_inherit))
2dee4c0b 3181 && NILP (Fequal (old_value, value)))
82641697
GM
3182 {
3183 ++face_change_count;
3184 ++windows_or_buffers_changed;
3185 }
3186
2ff10663 3187 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
82641697 3188 && NILP (Fequal (old_value, value)))
8bd201d6
GM
3189 {
3190 Lisp_Object param;
3191
3192 param = Qnil;
178c5d9c 3193
8bd201d6
GM
3194 if (EQ (face, Qdefault))
3195 {
b9c769f8 3196#ifdef HAVE_WINDOW_SYSTEM
8bd201d6
GM
3197 /* Changed font-related attributes of the `default' face are
3198 reflected in changed `font' frame parameters. */
ceeda019 3199 if (FRAMEP (frame)
2dee4c0b 3200 && (prop_index || EQ (attr, QCfont))
8bd201d6
GM
3201 && lface_fully_specified_p (XVECTOR (lface)->contents))
3202 set_font_frame_param (frame, lface);
b9c769f8
EZ
3203 else
3204#endif /* HAVE_WINDOW_SYSTEM */
3205
3206 if (EQ (attr, QCforeground))
8bd201d6
GM
3207 param = Qforeground_color;
3208 else if (EQ (attr, QCbackground))
3209 param = Qbackground_color;
3210 }
b9c769f8 3211#ifdef HAVE_WINDOW_SYSTEM
0fda9b75 3212#ifndef HAVE_NTGUI
8bd201d6
GM
3213 else if (EQ (face, Qscroll_bar))
3214 {
3215 /* Changing the colors of `scroll-bar' sets frame parameters
3216 `scroll-bar-foreground' and `scroll-bar-background'. */
3217 if (EQ (attr, QCforeground))
3218 param = Qscroll_bar_foreground;
3219 else if (EQ (attr, QCbackground))
3220 param = Qscroll_bar_background;
3221 }
0fda9b75 3222#endif /* not HAVE_NTGUI */
8bd201d6
GM
3223 else if (EQ (face, Qborder))
3224 {
3225 /* Changing background color of `border' sets frame parameter
3226 `border-color'. */
3227 if (EQ (attr, QCbackground))
3228 param = Qborder_color;
3229 }
3230 else if (EQ (face, Qcursor))
3231 {
3232 /* Changing background color of `cursor' sets frame parameter
3233 `cursor-color'. */
3234 if (EQ (attr, QCbackground))
3235 param = Qcursor_color;
3236 }
3237 else if (EQ (face, Qmouse))
3238 {
3239 /* Changing background color of `mouse' sets frame parameter
3240 `mouse-color'. */
3241 if (EQ (attr, QCbackground))
3242 param = Qmouse_color;
3243 }
b9c769f8 3244#endif /* HAVE_WINDOW_SYSTEM */
563f68f1 3245 else if (EQ (face, Qmenu))
ceeda019 3246 {
bee6a2c7
DA
3247 /* Indicate that we have to update the menu bar when realizing
3248 faces on FRAME. FRAME t change the default for new frames.
3249 We do this by setting the flag in new face caches. */
ceeda019
GM
3250 if (FRAMEP (frame))
3251 {
3252 struct frame *f = XFRAME (frame);
3253 if (FRAME_FACE_CACHE (f) == NULL)
3254 FRAME_FACE_CACHE (f) = make_face_cache (f);
3255 FRAME_FACE_CACHE (f)->menu_face_changed_p = 1;
3256 }
3257 else
3258 menu_face_changed_default = 1;
3259 }
8bd201d6 3260
39506348 3261 if (!NILP (param))
ab8469eb
PJ
3262 {
3263 if (EQ (frame, Qt))
3264 /* Update `default-frame-alist', which is used for new frames. */
3265 {
3266 store_in_alist (&Vdefault_frame_alist, param, value);
3267 }
3268 else
3269 /* Update the current frame's parameters. */
3270 {
3271 Lisp_Object cons;
3272 cons = XCAR (Vparam_value_alist);
3273 XSETCAR (cons, param);
3274 XSETCDR (cons, value);
3275 Fmodify_frame_parameters (frame, Vparam_value_alist);
3276 }
3277 }
8bd201d6 3278 }
82641697 3279
82641697
GM
3280 return face;
3281}
3282
3283
8bd201d6
GM
3284/* Update the corresponding face when frame parameter PARAM on frame F
3285 has been assigned the value NEW_VALUE. */
3286
3287void
b5f03016
AS
3288update_face_from_frame_parameter (struct frame *f, Lisp_Object param,
3289 Lisp_Object new_value)
8bd201d6 3290{
46b00436 3291 Lisp_Object face = Qnil;
8bd201d6
GM
3292 Lisp_Object lface;
3293
3294 /* If there are no faces yet, give up. This is the case when called
3295 from Fx_create_frame, and we do the necessary things later in
92610620 3296 face-set-after-frame-defaults. */
e69b0960 3297 if (NILP (f->face_alist))
8bd201d6 3298 return;
178c5d9c 3299
8bd201d6
GM
3300 if (EQ (param, Qforeground_color))
3301 {
46b00436
KS
3302 face = Qdefault;
3303 lface = lface_from_face_name (f, face, 1);
4939150c
PE
3304 ASET (lface, LFACE_FOREGROUND_INDEX,
3305 (STRINGP (new_value) ? new_value : Qunspecified));
8bd201d6
GM
3306 realize_basic_faces (f);
3307 }
3308 else if (EQ (param, Qbackground_color))
3309 {
92610620
GM
3310 Lisp_Object frame;
3311
3312 /* Changing the background color might change the background
c20577bc 3313 mode, so that we have to load new defface specs.
64677d90 3314 Call frame-set-background-mode to do that. */
92610620 3315 XSETFRAME (frame, f);
c20577bc 3316 call1 (Qframe_set_background_mode, frame);
178c5d9c 3317
46b00436
KS
3318 face = Qdefault;
3319 lface = lface_from_face_name (f, face, 1);
4939150c
PE
3320 ASET (lface, LFACE_BACKGROUND_INDEX,
3321 (STRINGP (new_value) ? new_value : Qunspecified));
8bd201d6
GM
3322 realize_basic_faces (f);
3323 }
76ea4cc9 3324#ifdef HAVE_WINDOW_SYSTEM
46b00436 3325 else if (EQ (param, Qborder_color))
8bd201d6 3326 {
46b00436
KS
3327 face = Qborder;
3328 lface = lface_from_face_name (f, face, 1);
4939150c
PE
3329 ASET (lface, LFACE_BACKGROUND_INDEX,
3330 (STRINGP (new_value) ? new_value : Qunspecified));
8bd201d6
GM
3331 }
3332 else if (EQ (param, Qcursor_color))
3333 {
46b00436
KS
3334 face = Qcursor;
3335 lface = lface_from_face_name (f, face, 1);
4939150c
PE
3336 ASET (lface, LFACE_BACKGROUND_INDEX,
3337 (STRINGP (new_value) ? new_value : Qunspecified));
8bd201d6
GM
3338 }
3339 else if (EQ (param, Qmouse_color))
3340 {
46b00436
KS
3341 face = Qmouse;
3342 lface = lface_from_face_name (f, face, 1);
4939150c
PE
3343 ASET (lface, LFACE_BACKGROUND_INDEX,
3344 (STRINGP (new_value) ? new_value : Qunspecified));
8bd201d6 3345 }
76ea4cc9 3346#endif
46b00436
KS
3347
3348 /* Changing a named face means that all realized faces depending on
3349 that face are invalid. Since we cannot tell which realized faces
3350 depend on the face, make sure they are all removed. This is done
3351 by incrementing face_change_count. The next call to
3352 init_iterator will then free realized faces. */
3353 if (!NILP (face)
3354 && NILP (Fget (face, Qface_no_inherit)))
3355 {
3356 ++face_change_count;
3357 ++windows_or_buffers_changed;
3358 }
8bd201d6
GM
3359}
3360
3361
76ea4cc9
EZ
3362#ifdef HAVE_WINDOW_SYSTEM
3363
3364/* Set the `font' frame parameter of FRAME determined from the
3365 font-object set in `default' face attributes LFACE. */
3366
3367static void
971de7fb 3368set_font_frame_param (Lisp_Object frame, Lisp_Object lface)
76ea4cc9
EZ
3369{
3370 struct frame *f = XFRAME (frame);
3371 Lisp_Object font;
3372
3373 if (FRAME_WINDOW_P (f)
3374 /* Don't do anything if the font is `unspecified'. This can
3375 happen during frame creation. */
3376 && (font = LFACE_FONT (lface),
3377 ! UNSPECIFIEDP (font)))
3378 {
3379 if (FONT_SPEC_P (font))
3380 {
3381 font = font_load_for_lface (f, XVECTOR (lface)->contents, font);
3382 if (NILP (font))
3383 return;
4939150c 3384 ASET (lface, LFACE_FONT_INDEX, font);
76ea4cc9
EZ
3385 }
3386 f->default_face_done_p = 0;
6c6f1994 3387 Fmodify_frame_parameters (frame, list1 (Fcons (Qfont, font)));
76ea4cc9
EZ
3388 }
3389}
3390
82641697 3391DEFUN ("internal-face-x-get-resource", Finternal_face_x_get_resource,
2bf7d27a
DA
3392 Sinternal_face_x_get_resource, 2, 3, 0,
3393 doc: /* Get the value of X resource RESOURCE, class CLASS.
3394Returned value is for the display of frame FRAME. If FRAME is not
3395specified or nil, use selected frame. This function exists because
3396ordinary `x-get-resource' doesn't take a frame argument. */)
5842a27b 3397 (Lisp_Object resource, Lisp_Object class, Lisp_Object frame)
82641697 3398{
c3cee013 3399 Lisp_Object value = Qnil;
2bf7d27a
DA
3400 struct frame *f;
3401
b7826503
PJ
3402 CHECK_STRING (resource);
3403 CHECK_STRING (class);
2bf7d27a 3404 f = decode_live_frame (frame);
4d7e6e51 3405 block_input ();
2bf7d27a 3406 value = display_x_get_resource (FRAME_X_DISPLAY_INFO (f),
82641697 3407 resource, class, Qnil, Qnil);
4d7e6e51 3408 unblock_input ();
82641697
GM
3409 return value;
3410}
3411
3412
3413/* Return resource string VALUE as a boolean value, i.e. nil, or t.
3414 If VALUE is "on" or "true", return t. If VALUE is "off" or
3415 "false", return nil. Otherwise, if SIGNAL_P is non-zero, signal an
3416 error; if SIGNAL_P is zero, return 0. */
178c5d9c 3417
82641697 3418static Lisp_Object
971de7fb 3419face_boolean_x_resource_value (Lisp_Object value, int signal_p)
82641697
GM
3420{
3421 Lisp_Object result = make_number (0);
3422
a54e2c05 3423 eassert (STRINGP (value));
178c5d9c 3424
25a48bd0
PE
3425 if (xstrcasecmp (SSDATA (value), "on") == 0
3426 || xstrcasecmp (SSDATA (value), "true") == 0)
82641697 3427 result = Qt;
25a48bd0
PE
3428 else if (xstrcasecmp (SSDATA (value), "off") == 0
3429 || xstrcasecmp (SSDATA (value), "false") == 0)
82641697 3430 result = Qnil;
25a48bd0 3431 else if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
82641697
GM
3432 result = Qunspecified;
3433 else if (signal_p)
3434 signal_error ("Invalid face attribute value from X resource", value);
3435
3436 return result;
3437}
3438
3439
3440DEFUN ("internal-set-lisp-face-attribute-from-resource",
3441 Finternal_set_lisp_face_attribute_from_resource,
3442 Sinternal_set_lisp_face_attribute_from_resource,
7ee72033 3443 3, 4, 0, doc: /* */)
5842a27b 3444 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
82641697 3445{
b7826503
PJ
3446 CHECK_SYMBOL (face);
3447 CHECK_SYMBOL (attr);
3448 CHECK_STRING (value);
82641697 3449
25a48bd0 3450 if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
82641697
GM
3451 value = Qunspecified;
3452 else if (EQ (attr, QCheight))
3453 {
3454 value = Fstring_to_number (value, make_number (10));
3455 if (XINT (value) <= 0)
3456 signal_error ("Invalid face height from X resource", value);
3457 }
3458 else if (EQ (attr, QCbold) || EQ (attr, QCitalic))
3459 value = face_boolean_x_resource_value (value, 1);
3460 else if (EQ (attr, QCweight) || EQ (attr, QCslant) || EQ (attr, QCwidth))
42a5b22f 3461 value = intern (SSDATA (value));
82641697
GM
3462 else if (EQ (attr, QCreverse_video) || EQ (attr, QCinverse_video))
3463 value = face_boolean_x_resource_value (value, 1);
3464 else if (EQ (attr, QCunderline)
3465 || EQ (attr, QCoverline)
27188d11 3466 || EQ (attr, QCstrike_through))
82641697
GM
3467 {
3468 Lisp_Object boolean_value;
3469
3470 /* If the result of face_boolean_x_resource_value is t or nil,
3471 VALUE does NOT specify a color. */
3472 boolean_value = face_boolean_x_resource_value (value, 0);
3473 if (SYMBOLP (boolean_value))
3474 value = boolean_value;
3475 }
0ad040ff 3476 else if (EQ (attr, QCbox) || EQ (attr, QCinherit))
27188d11 3477 value = Fcar (Fread_from_string (value, Qnil, Qnil));
82641697
GM
3478
3479 return Finternal_set_lisp_face_attribute (face, attr, value, frame);
3480}
3481
c3cee013 3482#endif /* HAVE_WINDOW_SYSTEM */
82641697 3483
c7ae3284
GM
3484\f
3485/***********************************************************************
3486 Menu face
3487 ***********************************************************************/
3488
bce72079 3489#if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
c7ae3284 3490
bce72079
GM
3491/* Make menus on frame F appear as specified by the `menu' face. */
3492
3493static void
ebd15611 3494x_update_menu_appearance (struct frame *f)
c7ae3284 3495{
a03ad468 3496 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
bce72079 3497 XrmDatabase rdb;
178c5d9c 3498
bce72079
GM
3499 if (dpyinfo
3500 && (rdb = XrmGetDatabase (FRAME_X_DISPLAY (f)),
3501 rdb != NULL))
c7ae3284 3502 {
bce72079 3503 char line[512];
670741ab
PE
3504 char *buf = line;
3505 ptrdiff_t bufsize = sizeof line;
bce72079
GM
3506 Lisp_Object lface = lface_from_face_name (f, Qmenu, 1);
3507 struct face *face = FACE_FROM_ID (f, MENU_FACE_ID);
42a5b22f 3508 const char *myname = SSDATA (Vx_resource_name);
d7e6881a 3509 bool changed_p = 0;
8ad582ac
GM
3510#ifdef USE_MOTIF
3511 const char *popup_path = "popup_menu";
3512#else
3513 const char *popup_path = "menu.popup";
3514#endif
177c0ea7 3515
bce72079
GM
3516 if (STRINGP (LFACE_FOREGROUND (lface)))
3517 {
670741ab
PE
3518 exprintf (&buf, &bufsize, line, -1, "%s.%s*foreground: %s",
3519 myname, popup_path,
3520 SDATA (LFACE_FOREGROUND (lface)));
bce72079 3521 XrmPutLineResource (&rdb, line);
670741ab
PE
3522 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*foreground: %s",
3523 myname, SDATA (LFACE_FOREGROUND (lface)));
bce72079
GM
3524 XrmPutLineResource (&rdb, line);
3525 changed_p = 1;
3526 }
178c5d9c 3527
bce72079
GM
3528 if (STRINGP (LFACE_BACKGROUND (lface)))
3529 {
670741ab
PE
3530 exprintf (&buf, &bufsize, line, -1, "%s.%s*background: %s",
3531 myname, popup_path,
3532 SDATA (LFACE_BACKGROUND (lface)));
bce72079 3533 XrmPutLineResource (&rdb, line);
670741ab
PE
3534
3535 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*background: %s",
3536 myname, SDATA (LFACE_BACKGROUND (lface)));
bce72079
GM
3537 XrmPutLineResource (&rdb, line);
3538 changed_p = 1;
3539 }
177c0ea7 3540
2dee4c0b 3541 if (face->font
23888717
CY
3542 /* On Solaris 5.8, it's been reported that the `menu' face
3543 can be unspecified here, during startup. Why this
3544 happens remains unknown. -- cyd */
3545 && FONTP (LFACE_FONT (lface))
bce72079 3546 && (!UNSPECIFIEDP (LFACE_FAMILY (lface))
53aaf1e2 3547 || !UNSPECIFIEDP (LFACE_FOUNDRY (lface))
bce72079 3548 || !UNSPECIFIEDP (LFACE_SWIDTH (lface))
bce72079
GM
3549 || !UNSPECIFIEDP (LFACE_WEIGHT (lface))
3550 || !UNSPECIFIEDP (LFACE_SLANT (lface))
3551 || !UNSPECIFIEDP (LFACE_HEIGHT (lface))))
3552 {
47ae3723 3553 Lisp_Object xlfd = Ffont_xlfd_name (LFACE_FONT (lface), Qnil);
a03ad468 3554#ifdef USE_MOTIF
bce72079 3555 const char *suffix = "List";
211f2210 3556 Bool motif = True;
a2d7e722
JD
3557#else
3558#if defined HAVE_X_I18N
3559
3560 const char *suffix = "Set";
a03ad468 3561#else
bce72079 3562 const char *suffix = "";
a2d7e722 3563#endif
211f2210 3564 Bool motif = False;
2defe37f 3565#endif
2dee4c0b
KH
3566
3567 if (! NILP (xlfd))
3568 {
211f2210 3569#if defined HAVE_X_I18N
42a5b22f 3570 char *fontsetname = xic_create_fontsetname (SSDATA (xlfd), motif);
2defe37f 3571#else
51b59d79 3572 char *fontsetname = SSDATA (xlfd);
bce72079 3573#endif
670741ab
PE
3574 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*font%s: %s",
3575 myname, suffix, fontsetname);
2dee4c0b 3576 XrmPutLineResource (&rdb, line);
670741ab
PE
3577
3578 exprintf (&buf, &bufsize, line, -1, "%s.%s*font%s: %s",
3579 myname, popup_path, suffix, fontsetname);
2dee4c0b
KH
3580 XrmPutLineResource (&rdb, line);
3581 changed_p = 1;
51b59d79 3582 if (fontsetname != SSDATA (xlfd))
2dee4c0b
KH
3583 xfree (fontsetname);
3584 }
bce72079 3585 }
c7ae3284 3586
bce72079 3587 if (changed_p && f->output_data.x->menubar_widget)
ac17f0bf 3588 free_frame_menubar (f);
670741ab
PE
3589
3590 if (buf != line)
3591 xfree (buf);
c7ae3284
GM
3592 }
3593}
3594
bce72079 3595#endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
82641697
GM
3596
3597
177c0ea7 3598DEFUN ("face-attribute-relative-p", Fface_attribute_relative_p,
cdfaafa9
MB
3599 Sface_attribute_relative_p,
3600 2, 2, 0,
2d6228e7
RS
3601 doc: /* Check whether a face attribute value is relative.
3602Specifically, this function returns t if the attribute ATTRIBUTE
3603with the value VALUE is relative.
3604
3605A relative value is one that doesn't entirely override whatever is
3606inherited from another face. For most possible attributes,
3607the only relative value that users see is `unspecified'.
3608However, for :height, floating point values are also relative. */)
5842a27b 3609 (Lisp_Object attribute, Lisp_Object value)
cdfaafa9 3610{
a3720aa2 3611 if (EQ (value, Qunspecified) || (EQ (value, QCignore_defface)))
cdfaafa9
MB
3612 return Qt;
3613 else if (EQ (attribute, QCheight))
3614 return INTEGERP (value) ? Qnil : Qt;
3615 else
3616 return Qnil;
3617}
3618
3619DEFUN ("merge-face-attribute", Fmerge_face_attribute, Smerge_face_attribute,
3620 3, 3, 0,
3621 doc: /* Return face ATTRIBUTE VALUE1 merged with VALUE2.
3622If VALUE1 or VALUE2 are absolute (see `face-attribute-relative-p'), then
3623the result will be absolute, otherwise it will be relative. */)
5842a27b 3624 (Lisp_Object attribute, Lisp_Object value1, Lisp_Object value2)
cdfaafa9 3625{
a3720aa2 3626 if (EQ (value1, Qunspecified) || EQ (value1, QCignore_defface))
cdfaafa9
MB
3627 return value2;
3628 else if (EQ (attribute, QCheight))
e1e419ec 3629 return merge_face_heights (value1, value2, value1);
cdfaafa9
MB
3630 else
3631 return value1;
3632}
3633
82641697
GM
3634
3635DEFUN ("internal-get-lisp-face-attribute", Finternal_get_lisp_face_attribute,
3636 Sinternal_get_lisp_face_attribute,
3637 2, 3, 0,
7ee72033 3638 doc: /* Return face attribute KEYWORD of face SYMBOL.
228299fa
GM
3639If SYMBOL does not name a valid Lisp face or KEYWORD isn't a valid
3640face attribute name, signal an error.
7fc92635
JB
3641If the optional argument FRAME is given, report on face SYMBOL in that
3642frame. If FRAME is t, report on the defaults for face SYMBOL (for new
7ee72033 3643frames). If FRAME is omitted or nil, use the selected frame. */)
5842a27b 3644 (Lisp_Object symbol, Lisp_Object keyword, Lisp_Object frame)
82641697 3645{
d9f07150
DA
3646 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3647 Lisp_Object lface = lface_from_face_name (f, symbol, 1), value = Qnil;
178c5d9c 3648
b7826503
PJ
3649 CHECK_SYMBOL (symbol);
3650 CHECK_SYMBOL (keyword);
82641697 3651
82641697
GM
3652 if (EQ (keyword, QCfamily))
3653 value = LFACE_FAMILY (lface);
53aaf1e2
KH
3654 else if (EQ (keyword, QCfoundry))
3655 value = LFACE_FOUNDRY (lface);
82641697
GM
3656 else if (EQ (keyword, QCheight))
3657 value = LFACE_HEIGHT (lface);
3658 else if (EQ (keyword, QCweight))
3659 value = LFACE_WEIGHT (lface);
3660 else if (EQ (keyword, QCslant))
3661 value = LFACE_SLANT (lface);
3662 else if (EQ (keyword, QCunderline))
3663 value = LFACE_UNDERLINE (lface);
3664 else if (EQ (keyword, QCoverline))
3665 value = LFACE_OVERLINE (lface);
3666 else if (EQ (keyword, QCstrike_through))
3667 value = LFACE_STRIKE_THROUGH (lface);
3668 else if (EQ (keyword, QCbox))
3669 value = LFACE_BOX (lface);
3670 else if (EQ (keyword, QCinverse_video)
3671 || EQ (keyword, QCreverse_video))
3672 value = LFACE_INVERSE (lface);
3673 else if (EQ (keyword, QCforeground))
3674 value = LFACE_FOREGROUND (lface);
3675 else if (EQ (keyword, QCbackground))
3676 value = LFACE_BACKGROUND (lface);
3677 else if (EQ (keyword, QCstipple))
3678 value = LFACE_STIPPLE (lface);
3679 else if (EQ (keyword, QCwidth))
3680 value = LFACE_SWIDTH (lface);
2c20458f
MB
3681 else if (EQ (keyword, QCinherit))
3682 value = LFACE_INHERIT (lface);
39506348
KH
3683 else if (EQ (keyword, QCfont))
3684 value = LFACE_FONT (lface);
763bc839
KH
3685 else if (EQ (keyword, QCfontset))
3686 value = LFACE_FONTSET (lface);
82641697
GM
3687 else
3688 signal_error ("Invalid face attribute name", keyword);
3689
0268cef3
CY
3690 if (IGNORE_DEFFACE_P (value))
3691 return Qunspecified;
3692
82641697
GM
3693 return value;
3694}
3695
3696
3697DEFUN ("internal-lisp-face-attribute-values",
3698 Finternal_lisp_face_attribute_values,
3699 Sinternal_lisp_face_attribute_values, 1, 1, 0,
7ee72033
MB
3700 doc: /* Return a list of valid discrete values for face attribute ATTR.
3701Value is nil if ATTR doesn't have a discrete set of valid values. */)
5842a27b 3702 (Lisp_Object attr)
82641697
GM
3703{
3704 Lisp_Object result = Qnil;
178c5d9c 3705
b7826503 3706 CHECK_SYMBOL (attr);
178c5d9c 3707
6c6f1994
PE
3708 if (EQ (attr, QCunderline) || EQ (attr, QCoverline)
3709 || EQ (attr, QCstrike_through)
3710 || EQ (attr, QCinverse_video) || EQ (attr, QCreverse_video))
3711 result = list2 (Qt, Qnil);
82641697
GM
3712
3713 return result;
3714}
178c5d9c 3715
82641697
GM
3716
3717DEFUN ("internal-merge-in-global-face", Finternal_merge_in_global_face,
178c5d9c 3718 Sinternal_merge_in_global_face, 2, 2, 0,
e3cd9e7f 3719 doc: /* Add attributes from frame-default definition of FACE to FACE on FRAME.
7ee72033 3720Default face attributes override any local face attributes. */)
5842a27b 3721 (Lisp_Object face, Lisp_Object frame)
82641697 3722{
aad40737
MB
3723 int i;
3724 Lisp_Object global_lface, local_lface, *gvec, *lvec;
8d3810fd 3725 struct frame *f = XFRAME (frame);
aad40737 3726
b7826503 3727 CHECK_LIVE_FRAME (frame);
82641697 3728 global_lface = lface_from_face_name (NULL, face, 1);
8d3810fd 3729 local_lface = lface_from_face_name (f, face, 0);
82641697
GM
3730 if (NILP (local_lface))
3731 local_lface = Finternal_make_lisp_face (face, frame);
aad40737 3732
cec33c90
SM
3733 /* Make every specified global attribute override the local one.
3734 BEWARE!! This is only used from `face-set-after-frame-default' where
3735 the local frame is defined from default specs in `face-defface-spec'
3736 and those should be overridden by global settings. Hence the strange
3737 "global before local" priority. */
aad40737
MB
3738 lvec = XVECTOR (local_lface)->contents;
3739 gvec = XVECTOR (global_lface)->contents;
3740 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
8d3810fd 3741 if (IGNORE_DEFFACE_P (gvec[i]))
086ca913 3742 ASET (local_lface, i, Qunspecified);
8d3810fd 3743 else if (! UNSPECIFIEDP (gvec[i]))
086ca913 3744 ASET (local_lface, i, AREF (global_lface, i));
8d3810fd 3745
fa9fa316 3746 /* If the default face was changed, update the face cache and the
811029d3 3747 `font' frame parameter. */
8d3810fd
CY
3748 if (EQ (face, Qdefault))
3749 {
3750 struct face_cache *c = FRAME_FACE_CACHE (f);
3751 struct face *newface, *oldface = FACE_FROM_ID (f, DEFAULT_FACE_ID);
3752 Lisp_Object attrs[LFACE_VECTOR_SIZE];
3753
0421a7a7
CY
3754 /* This can be NULL (e.g., in batch mode). */
3755 if (oldface)
8d3810fd 3756 {
fa9fa316
CY
3757 /* Ensure that the face vector is fully specified by merging
3758 the previously-cached vector. */
72af86bd 3759 memcpy (attrs, oldface->lface, sizeof attrs);
0421a7a7 3760 merge_face_vectors (f, lvec, attrs, 0);
086ca913 3761 vcopy (local_lface, 0, attrs, LFACE_VECTOR_SIZE);
fa9fa316 3762 newface = realize_face (c, lvec, DEFAULT_FACE_ID);
0421a7a7
CY
3763
3764 if ((! UNSPECIFIEDP (gvec[LFACE_FAMILY_INDEX])
3765 || ! UNSPECIFIEDP (gvec[LFACE_FOUNDRY_INDEX])
3766 || ! UNSPECIFIEDP (gvec[LFACE_HEIGHT_INDEX])
3767 || ! UNSPECIFIEDP (gvec[LFACE_WEIGHT_INDEX])
3768 || ! UNSPECIFIEDP (gvec[LFACE_SLANT_INDEX])
3769 || ! UNSPECIFIEDP (gvec[LFACE_SWIDTH_INDEX])
3770 || ! UNSPECIFIEDP (gvec[LFACE_FONT_INDEX]))
3771 && newface->font)
3772 {
3773 Lisp_Object name = newface->font->props[FONT_NAME_INDEX];
6c6f1994 3774 Fmodify_frame_parameters (frame, list1 (Fcons (Qfont, name)));
0421a7a7 3775 }
1485f4c0
CY
3776
3777 if (STRINGP (gvec[LFACE_FOREGROUND_INDEX]))
3778 Fmodify_frame_parameters (frame,
6c6f1994
PE
3779 list1 (Fcons (Qforeground_color,
3780 gvec[LFACE_FOREGROUND_INDEX])));
1485f4c0
CY
3781
3782 if (STRINGP (gvec[LFACE_BACKGROUND_INDEX]))
3783 Fmodify_frame_parameters (frame,
6c6f1994
PE
3784 list1 (Fcons (Qbackground_color,
3785 gvec[LFACE_BACKGROUND_INDEX])));
8d3810fd
CY
3786 }
3787 }
334a2e2a
GM
3788
3789 return Qnil;
82641697
GM
3790}
3791
3792
3793/* The following function is implemented for compatibility with 20.2.
3794 The function is used in x-resolve-fonts when it is asked to
3795 return fonts with the same size as the font of a face. This is
3796 done in fontset.el. */
3797
2db4bfe5 3798DEFUN ("face-font", Fface_font, Sface_font, 1, 3, 0,
e3cd9e7f 3799 doc: /* Return the font name of face FACE, or nil if it is unspecified.
2db4bfe5 3800The font name is, by default, for ASCII characters.
228299fa
GM
3801If the optional argument FRAME is given, report on face FACE in that frame.
3802If FRAME is t, report on the defaults for face FACE (for new frames).
3803 The font default for a face is either nil, or a list
3804 of the form (bold), (italic) or (bold italic).
2db4bfe5
KH
3805If FRAME is omitted or nil, use the selected frame. And, in this case,
3806if the optional third argument CHARACTER is given,
3807return the font name used for CHARACTER. */)
5842a27b 3808 (Lisp_Object face, Lisp_Object frame, Lisp_Object character)
82641697
GM
3809{
3810 if (EQ (frame, Qt))
3811 {
3812 Lisp_Object result = Qnil;
3813 Lisp_Object lface = lface_from_face_name (NULL, face, 1);
3814
3815 if (!UNSPECIFIEDP (LFACE_WEIGHT (lface))
3816 && !EQ (LFACE_WEIGHT (lface), Qnormal))
3817 result = Fcons (Qbold, result);
178c5d9c 3818
0f2c6573 3819 if (!UNSPECIFIEDP (LFACE_SLANT (lface))
82641697
GM
3820 && !EQ (LFACE_SLANT (lface), Qnormal))
3821 result = Fcons (Qitalic, result);
178c5d9c 3822
82641697
GM
3823 return result;
3824 }
3825 else
3826 {
d9f07150 3827 struct frame *f = decode_live_frame (frame);
000fc2b1 3828 int face_id = lookup_named_face (f, face, 1);
071048a3 3829 struct face *fface = FACE_FROM_ID (f, face_id);
2db4bfe5 3830
071048a3 3831 if (! fface)
2db4bfe5 3832 return Qnil;
a1a552b3 3833#ifdef HAVE_WINDOW_SYSTEM
75dad34a 3834 if (FRAME_WINDOW_P (f) && !NILP (character))
a1a552b3
KH
3835 {
3836 CHECK_CHARACTER (character);
071048a3
PE
3837 face_id = FACE_FOR_CHAR (f, fface, XINT (character), -1, Qnil);
3838 fface = FACE_FROM_ID (f, face_id);
a1a552b3 3839 }
071048a3
PE
3840 return (fface->font
3841 ? fface->font->props[FONT_NAME_INDEX]
2dee4c0b 3842 : Qnil);
1ccdfd33
EZ
3843#else /* !HAVE_WINDOW_SYSTEM */
3844 return build_string (FRAME_MSDOS_P (f)
3845 ? "ms-dos"
3846 : FRAME_W32_P (f) ? "w32term"
3847 :"tty");
3848#endif
82641697
GM
3849 }
3850}
3851
3852
9717e36c 3853/* Compare face-attribute values v1 and v2 for equality. Value is non-zero if
82641697
GM
3854 all attributes are `equal'. Tries to be fast because this function
3855 is called quite often. */
3856
d7e6881a 3857static bool
971de7fb 3858face_attr_equal_p (Lisp_Object v1, Lisp_Object v2)
82641697 3859{
9717e36c
MB
3860 /* Type can differ, e.g. when one attribute is unspecified, i.e. nil,
3861 and the other is specified. */
3862 if (XTYPE (v1) != XTYPE (v2))
3863 return 0;
82641697 3864
9717e36c
MB
3865 if (EQ (v1, v2))
3866 return 1;
82641697 3867
9717e36c
MB
3868 switch (XTYPE (v1))
3869 {
3870 case Lisp_String:
3871 if (SBYTES (v1) != SBYTES (v2))
3872 return 0;
82641697 3873
72af86bd 3874 return memcmp (SDATA (v1), SDATA (v2), SBYTES (v1)) == 0;
178c5d9c 3875
2de9f71c 3876 case_Lisp_Int:
9717e36c
MB
3877 case Lisp_Symbol:
3878 return 0;
178c5d9c 3879
9717e36c
MB
3880 default:
3881 return !NILP (Fequal (v1, v2));
82641697 3882 }
9717e36c
MB
3883}
3884
3885
3886/* Compare face vectors V1 and V2 for equality. Value is non-zero if
3887 all attributes are `equal'. Tries to be fast because this function
3888 is called quite often. */
3889
d7e6881a 3890static bool
971de7fb 3891lface_equal_p (Lisp_Object *v1, Lisp_Object *v2)
9717e36c 3892{
d7e6881a
DA
3893 int i;
3894 bool equal_p = 1;
9717e36c
MB
3895
3896 for (i = 1; i < LFACE_VECTOR_SIZE && equal_p; ++i)
3897 equal_p = face_attr_equal_p (v1[i], v2[i]);
178c5d9c 3898
82641697
GM
3899 return equal_p;
3900}
3901
3902
3903DEFUN ("internal-lisp-face-equal-p", Finternal_lisp_face_equal_p,
3904 Sinternal_lisp_face_equal_p, 2, 3, 0,
7ee72033 3905 doc: /* True if FACE1 and FACE2 are equal.
03f11322
JB
3906If the optional argument FRAME is given, report on FACE1 and FACE2 in that frame.
3907If FRAME is t, report on the defaults for FACE1 and FACE2 (for new frames).
7ee72033 3908If FRAME is omitted or nil, use the selected frame. */)
5842a27b 3909 (Lisp_Object face1, Lisp_Object face2, Lisp_Object frame)
82641697
GM
3910{
3911 int equal_p;
7b953c9c 3912 struct frame *f;
82641697 3913 Lisp_Object lface1, lface2;
178c5d9c 3914
7452b7bd
DA
3915 /* Don't use decode_window_system_frame here because this function
3916 is called before X frames exist. At that time, if FRAME is nil,
d9f07150
DA
3917 selected_frame will be used which is the frame dumped with
3918 Emacs. That frame is not an X frame. */
3919 f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
7b953c9c 3920
03f11322
JB
3921 lface1 = lface_from_face_name (f, face1, 1);
3922 lface2 = lface_from_face_name (f, face2, 1);
82641697
GM
3923 equal_p = lface_equal_p (XVECTOR (lface1)->contents,
3924 XVECTOR (lface2)->contents);
3925 return equal_p ? Qt : Qnil;
3926}
3927
178c5d9c 3928
82641697
GM
3929DEFUN ("internal-lisp-face-empty-p", Finternal_lisp_face_empty_p,
3930 Sinternal_lisp_face_empty_p, 1, 2, 0,
7ee72033 3931 doc: /* True if FACE has no attribute specified.
228299fa
GM
3932If the optional argument FRAME is given, report on face FACE in that frame.
3933If FRAME is t, report on the defaults for face FACE (for new frames).
7ee72033 3934If FRAME is omitted or nil, use the selected frame. */)
5842a27b 3935 (Lisp_Object face, Lisp_Object frame)
82641697 3936{
d9f07150
DA
3937 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3938 Lisp_Object lface = lface_from_face_name (f, face, 1);
82641697
GM
3939 int i;
3940
82641697 3941 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
a08332c0 3942 if (!UNSPECIFIEDP (AREF (lface, i)))
82641697 3943 break;
178c5d9c 3944
82641697
GM
3945 return i == LFACE_VECTOR_SIZE ? Qt : Qnil;
3946}
3947
3948
3949DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist,
178c5d9c 3950 0, 1, 0,
7ee72033
MB
3951 doc: /* Return an alist of frame-local faces defined on FRAME.
3952For internal use only. */)
5842a27b 3953 (Lisp_Object frame)
82641697 3954{
d9f07150 3955 return decode_live_frame (frame)->face_alist;
82641697
GM
3956}
3957
3958
3959/* Return a hash code for Lisp string STRING with case ignored. Used
3960 below in computing a hash value for a Lisp face. */
3961
b0ab8123 3962static unsigned
971de7fb 3963hash_string_case_insensitive (Lisp_Object string)
82641697 3964{
53c208f6 3965 const unsigned char *s;
82641697 3966 unsigned hash = 0;
a54e2c05 3967 eassert (STRINGP (string));
d5db4077 3968 for (s = SDATA (string); *s; ++s)
620f13b0 3969 hash = (hash << 1) ^ c_tolower (*s);
82641697
GM
3970 return hash;
3971}
3972
3973
3974/* Return a hash code for face attribute vector V. */
3975
b0ab8123 3976static unsigned
971de7fb 3977lface_hash (Lisp_Object *v)
82641697
GM
3978{
3979 return (hash_string_case_insensitive (v[LFACE_FAMILY_INDEX])
53aaf1e2 3980 ^ hash_string_case_insensitive (v[LFACE_FOUNDRY_INDEX])
82641697
GM
3981 ^ hash_string_case_insensitive (v[LFACE_FOREGROUND_INDEX])
3982 ^ hash_string_case_insensitive (v[LFACE_BACKGROUND_INDEX])
a2bc5bdd
SM
3983 ^ XHASH (v[LFACE_WEIGHT_INDEX])
3984 ^ XHASH (v[LFACE_SLANT_INDEX])
3985 ^ XHASH (v[LFACE_SWIDTH_INDEX])
3986 ^ XHASH (v[LFACE_HEIGHT_INDEX]));
82641697
GM
3987}
3988
257b3b03 3989#ifdef HAVE_WINDOW_SYSTEM
82641697
GM
3990
3991/* Return non-zero if LFACE1 and LFACE2 specify the same font (without
3992 considering charsets/registries). They do if they specify the same
2dee4c0b 3993 family, point size, weight, width, slant, and font. Both
763bc839 3994 LFACE1 and LFACE2 must be fully-specified. */
82641697 3995
b0ab8123 3996static int
971de7fb 3997lface_same_font_attributes_p (Lisp_Object *lface1, Lisp_Object *lface2)
82641697 3998{
a54e2c05 3999 eassert (lface_fully_specified_p (lface1)
82641697 4000 && lface_fully_specified_p (lface2));
25a48bd0 4001 return (xstrcasecmp (SSDATA (lface1[LFACE_FAMILY_INDEX]),
b5f03016 4002 SSDATA (lface2[LFACE_FAMILY_INDEX])) == 0
25a48bd0
PE
4003 && xstrcasecmp (SSDATA (lface1[LFACE_FOUNDRY_INDEX]),
4004 SSDATA (lface2[LFACE_FOUNDRY_INDEX])) == 0
2c20458f 4005 && EQ (lface1[LFACE_HEIGHT_INDEX], lface2[LFACE_HEIGHT_INDEX])
82641697
GM
4006 && EQ (lface1[LFACE_SWIDTH_INDEX], lface2[LFACE_SWIDTH_INDEX])
4007 && EQ (lface1[LFACE_WEIGHT_INDEX], lface2[LFACE_WEIGHT_INDEX])
39506348 4008 && EQ (lface1[LFACE_SLANT_INDEX], lface2[LFACE_SLANT_INDEX])
2dee4c0b 4009 && EQ (lface1[LFACE_FONT_INDEX], lface2[LFACE_FONT_INDEX])
763bc839
KH
4010 && (EQ (lface1[LFACE_FONTSET_INDEX], lface2[LFACE_FONTSET_INDEX])
4011 || (STRINGP (lface1[LFACE_FONTSET_INDEX])
4012 && STRINGP (lface2[LFACE_FONTSET_INDEX])
25a48bd0 4013 && ! xstrcasecmp (SSDATA (lface1[LFACE_FONTSET_INDEX]),
b5f03016 4014 SSDATA (lface2[LFACE_FONTSET_INDEX]))))
763bc839 4015 );
82641697
GM
4016}
4017
257b3b03 4018#endif /* HAVE_WINDOW_SYSTEM */
82641697 4019
82641697
GM
4020/***********************************************************************
4021 Realized Faces
4022 ***********************************************************************/
4023
4024/* Allocate and return a new realized face for Lisp face attribute
39506348 4025 vector ATTR. */
82641697
GM
4026
4027static struct face *
971de7fb 4028make_realized_face (Lisp_Object *attr)
82641697 4029{
23f86fce 4030 struct face *face = xzalloc (sizeof *face);
39506348 4031 face->ascii_face = face;
72af86bd 4032 memcpy (face->lface, attr, sizeof face->lface);
82641697
GM
4033 return face;
4034}
4035
4036
4037/* Free realized face FACE, including its X resources. FACE may
4038 be null. */
4039
435f4c28 4040static void
971de7fb 4041free_realized_face (struct frame *f, struct face *face)
82641697
GM
4042{
4043 if (face)
4044 {
c3cee013
JR
4045#ifdef HAVE_WINDOW_SYSTEM
4046 if (FRAME_WINDOW_P (f))
82641697 4047 {
39506348
KH
4048 /* Free fontset of FACE if it is ASCII face. */
4049 if (face->fontset >= 0 && face == face->ascii_face)
4050 free_face_fontset (f, face);
82641697
GM
4051 if (face->gc)
4052 {
4d7e6e51 4053 block_input ();
2dee4c0b 4054 if (face->font)
426b2119 4055 font_done_for_face (f, face);
82641697
GM
4056 x_free_gc (f, face->gc);
4057 face->gc = 0;
4d7e6e51 4058 unblock_input ();
82641697 4059 }
178c5d9c 4060
82641697
GM
4061 free_face_colors (f, face);
4062 x_destroy_bitmap (f, face->stipple);
4063 }
c3cee013 4064#endif /* HAVE_WINDOW_SYSTEM */
82641697
GM
4065
4066 xfree (face);
4067 }
4068}
4069
4070
4071/* Prepare face FACE for subsequent display on frame F. This
4072 allocated GCs if they haven't been allocated yet or have been freed
4073 by clearing the face cache. */
4074
4075void
971de7fb 4076prepare_face_for_display (struct frame *f, struct face *face)
82641697 4077{
c3cee013 4078#ifdef HAVE_WINDOW_SYSTEM
a54e2c05 4079 eassert (FRAME_WINDOW_P (f));
178c5d9c 4080
82641697
GM
4081 if (face->gc == 0)
4082 {
4083 XGCValues xgcv;
4084 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
4085
4086 xgcv.foreground = face->foreground;
4087 xgcv.background = face->background;
c3cee013 4088#ifdef HAVE_X_WINDOWS
82641697 4089 xgcv.graphics_exposures = False;
c3cee013 4090#endif
82641697 4091
4d7e6e51 4092 block_input ();
c3cee013 4093#ifdef HAVE_X_WINDOWS
82641697
GM
4094 if (face->stipple)
4095 {
be8a72f4 4096 xgcv.fill_style = FillOpaqueStippled;
82641697
GM
4097 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
4098 mask |= GCFillStyle | GCStipple;
4099 }
c3cee013 4100#endif
82641697 4101 face->gc = x_create_gc (f, mask, &xgcv);
2dee4c0b 4102 if (face->font)
426b2119 4103 font_prepare_for_face (f, face);
4d7e6e51 4104 unblock_input ();
82641697 4105 }
c3cee013 4106#endif /* HAVE_WINDOW_SYSTEM */
82641697
GM
4107}
4108
82641697 4109\f
b35df831
MB
4110/* Returns the `distance' between the colors X and Y. */
4111
4112static int
971de7fb 4113color_distance (XColor *x, XColor *y)
b35df831 4114{
da6062e6 4115 /* This formula is from a paper titled `Colour metric' by Thiadmer Riemersma.
b35df831
MB
4116 Quoting from that paper:
4117
b5f03016
AS
4118 This formula has results that are very close to L*u*v* (with the
4119 modified lightness curve) and, more importantly, it is a more even
da6062e6 4120 algorithm: it does not have a range of colors where it suddenly
b5f03016 4121 gives far from optimal results.
b35df831
MB
4122
4123 See <http://www.compuphase.com/cmetric.htm> for more info. */
4124
4125 long r = (x->red - y->red) >> 8;
4126 long g = (x->green - y->green) >> 8;
4127 long b = (x->blue - y->blue) >> 8;
4128 long r_mean = (x->red + y->red) >> 9;
4129
4130 return
4131 (((512 + r_mean) * r * r) >> 8)
4132 + 4 * g * g
4133 + (((767 - r_mean) * b * b) >> 8);
4134}
4135
4136
4137DEFUN ("color-distance", Fcolor_distance, Scolor_distance, 2, 3, 0,
4138 doc: /* Return an integer distance between COLOR1 and COLOR2 on FRAME.
4139COLOR1 and COLOR2 may be either strings containing the color name,
4140or lists of the form (RED GREEN BLUE).
4141If FRAME is unspecified or nil, the current frame is used. */)
5842a27b 4142 (Lisp_Object color1, Lisp_Object color2, Lisp_Object frame)
b35df831 4143{
d9f07150 4144 struct frame *f = decode_live_frame (frame);
b35df831
MB
4145 XColor cdef1, cdef2;
4146
6c8e1d62 4147 if (!(CONSP (color1) && parse_rgb_list (color1, &cdef1))
42a5b22f 4148 && !(STRINGP (color1) && defined_color (f, SSDATA (color1), &cdef1, 0)))
b35df831 4149 signal_error ("Invalid color", color1);
6c8e1d62 4150 if (!(CONSP (color2) && parse_rgb_list (color2, &cdef2))
42a5b22f 4151 && !(STRINGP (color2) && defined_color (f, SSDATA (color2), &cdef2, 0)))
b35df831
MB
4152 signal_error ("Invalid color", color2);
4153
4154 return make_number (color_distance (&cdef1, &cdef2));
4155}
4156
4157\f
82641697
GM
4158/***********************************************************************
4159 Face Cache
4160 ***********************************************************************/
4161
4162/* Return a new face cache for frame F. */
4163
4164static struct face_cache *
971de7fb 4165make_face_cache (struct frame *f)
82641697 4166{
bee6a2c7 4167 struct face_cache *c = xmalloc (sizeof *c);
82641697 4168
bee6a2c7 4169 c->buckets = xzalloc (FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets);
82641697 4170 c->size = 50;
bee6a2c7 4171 c->used = 0;
23f86fce 4172 c->faces_by_id = xmalloc (c->size * sizeof *c->faces_by_id);
82641697 4173 c->f = f;
ceeda019 4174 c->menu_face_changed_p = menu_face_changed_default;
82641697
GM
4175 return c;
4176}
4177
257b3b03 4178#ifdef HAVE_WINDOW_SYSTEM
82641697
GM
4179
4180/* Clear out all graphics contexts for all realized faces, except for
4181 the basic faces. This should be done from time to time just to avoid
4182 keeping too many graphics contexts that are no longer needed. */
4183
4184static void
971de7fb 4185clear_face_gcs (struct face_cache *c)
82641697 4186{
c3cee013 4187 if (c && FRAME_WINDOW_P (c->f))
82641697 4188 {
82641697
GM
4189 int i;
4190 for (i = BASIC_FACE_ID_SENTINEL; i < c->used; ++i)
4191 {
4192 struct face *face = c->faces_by_id[i];
4193 if (face && face->gc)
4194 {
4d7e6e51 4195 block_input ();
2dee4c0b 4196 if (face->font)
426b2119 4197 font_done_for_face (c->f, face);
82641697
GM
4198 x_free_gc (c->f, face->gc);
4199 face->gc = 0;
4d7e6e51 4200 unblock_input ();
82641697
GM
4201 }
4202 }
82641697
GM
4203 }
4204}
4205
257b3b03 4206#endif /* HAVE_WINDOW_SYSTEM */
82641697 4207
7fc92635
JB
4208/* Free all realized faces in face cache C, including basic faces.
4209 C may be null. If faces are freed, make sure the frame's current
82641697
GM
4210 matrix is marked invalid, so that a display caused by an expose
4211 event doesn't try to use faces we destroyed. */
4212
4213static void
971de7fb 4214free_realized_faces (struct face_cache *c)
82641697
GM
4215{
4216 if (c && c->used)
4217 {
4218 int i, size;
4219 struct frame *f = c->f;
4220
84ec3b4b
GM
4221 /* We must block input here because we can't process X events
4222 safely while only some faces are freed, or when the frame's
4223 current matrix still references freed faces. */
4d7e6e51 4224 block_input ();
84ec3b4b 4225
82641697
GM
4226 for (i = 0; i < c->used; ++i)
4227 {
4228 free_realized_face (f, c->faces_by_id[i]);
4229 c->faces_by_id[i] = NULL;
4230 }
178c5d9c 4231
82641697
GM
4232 c->used = 0;
4233 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
72af86bd 4234 memset (c->buckets, 0, size);
82641697
GM
4235
4236 /* Must do a thorough redisplay the next time. Mark current
4237 matrices as invalid because they will reference faces freed
4238 above. This function is also called when a frame is
4239 destroyed. In this case, the root window of F is nil. */
e69b0960 4240 if (WINDOWP (f->root_window))
82641697
GM
4241 {
4242 clear_current_matrices (f);
4243 ++windows_or_buffers_changed;
4244 }
84ec3b4b 4245
4d7e6e51 4246 unblock_input ();
82641697
GM
4247 }
4248}
4249
4250
4251/* Free all realized faces on FRAME or on all frames if FRAME is nil.
4252 This is done after attributes of a named face have been changed,
4253 because we can't tell which realized faces depend on that face. */
4254
4255void
971de7fb 4256free_all_realized_faces (Lisp_Object frame)
82641697
GM
4257{
4258 if (NILP (frame))
4259 {
4260 Lisp_Object rest;
4261 FOR_EACH_FRAME (rest, frame)
4262 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4263 }
4264 else
4265 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4266}
4267
4268
4269/* Free face cache C and faces in it, including their X resources. */
4270
4271static void
971de7fb 4272free_face_cache (struct face_cache *c)
82641697
GM
4273{
4274 if (c)
4275 {
4276 free_realized_faces (c);
4277 xfree (c->buckets);
4278 xfree (c->faces_by_id);
4279 xfree (c);
4280 }
4281}
4282
4283
4284/* Cache realized face FACE in face cache C. HASH is the hash value
af53b43c
KH
4285 of FACE. If FACE is for ASCII characters (i.e. FACE->ascii_face ==
4286 FACE), insert the new face to the beginning of the collision list
4287 of the face hash table of C. Otherwise, add the new face to the
4288 end of the collision list. This way, lookup_face can quickly find
4289 that a requested face is not cached. */
82641697
GM
4290
4291static void
971de7fb 4292cache_face (struct face_cache *c, struct face *face, unsigned int hash)
82641697
GM
4293{
4294 int i = hash % FACE_CACHE_BUCKETS_SIZE;
4295
4296 face->hash = hash;
4297
af53b43c 4298 if (face->ascii_face != face)
82641697
GM
4299 {
4300 struct face *last = c->buckets[i];
4301 if (last)
4302 {
4303 while (last->next)
4304 last = last->next;
4305 last->next = face;
4306 face->prev = last;
4307 face->next = NULL;
4308 }
4309 else
4310 {
4311 c->buckets[i] = face;
4312 face->prev = face->next = NULL;
4313 }
4314 }
4315 else
4316 {
4317 face->prev = NULL;
4318 face->next = c->buckets[i];
4319 if (face->next)
4320 face->next->prev = face;
4321 c->buckets[i] = face;
4322 }
4323
4324 /* Find a free slot in C->faces_by_id and use the index of the free
4325 slot as FACE->id. */
4326 for (i = 0; i < c->used; ++i)
4327 if (c->faces_by_id[i] == NULL)
4328 break;
4329 face->id = i;
178c5d9c 4330
e509cfa6 4331#ifdef GLYPH_DEBUG
85fece3e
PE
4332 /* Check that FACE got a unique id. */
4333 {
4334 int j, n;
4335 struct face *face1;
4336
4337 for (j = n = 0; j < FACE_CACHE_BUCKETS_SIZE; ++j)
4338 for (face1 = c->buckets[j]; face1; face1 = face1->next)
4339 if (face1->id == i)
4340 ++n;
4341
a54e2c05 4342 eassert (n == 1);
85fece3e
PE
4343 }
4344#endif /* GLYPH_DEBUG */
4345
82641697 4346 /* Maybe enlarge C->faces_by_id. */
6b61353c 4347 if (i == c->used)
82641697 4348 {
6b61353c 4349 if (c->used == c->size)
0065d054
PE
4350 c->faces_by_id = xpalloc (c->faces_by_id, &c->size, 1, MAX_FACE_ID,
4351 sizeof *c->faces_by_id);
6b61353c 4352 c->used++;
82641697
GM
4353 }
4354
82641697 4355 c->faces_by_id[i] = face;
82641697
GM
4356}
4357
4358
4359/* Remove face FACE from cache C. */
4360
4361static void
971de7fb 4362uncache_face (struct face_cache *c, struct face *face)
82641697
GM
4363{
4364 int i = face->hash % FACE_CACHE_BUCKETS_SIZE;
178c5d9c 4365
82641697
GM
4366 if (face->prev)
4367 face->prev->next = face->next;
4368 else
4369 c->buckets[i] = face->next;
178c5d9c 4370
82641697
GM
4371 if (face->next)
4372 face->next->prev = face->prev;
178c5d9c 4373
82641697
GM
4374 c->faces_by_id[face->id] = NULL;
4375 if (face->id == c->used)
4376 --c->used;
4377}
4378
4379
4380/* Look up a realized face with face attributes ATTR in the face cache
af53b43c
KH
4381 of frame F. The face will be used to display ASCII characters.
4382 Value is the ID of the face found. If no suitable face is found,
4383 realize a new one. */
82641697 4384
b0ab8123 4385static int
971de7fb 4386lookup_face (struct frame *f, Lisp_Object *attr)
82641697 4387{
39506348 4388 struct face_cache *cache = FRAME_FACE_CACHE (f);
82641697
GM
4389 unsigned hash;
4390 int i;
4391 struct face *face;
4392
a54e2c05 4393 eassert (cache != NULL);
82641697
GM
4394 check_lface_attrs (attr);
4395
4396 /* Look up ATTR in the face cache. */
4397 hash = lface_hash (attr);
4398 i = hash % FACE_CACHE_BUCKETS_SIZE;
178c5d9c 4399
39506348 4400 for (face = cache->buckets[i]; face; face = face->next)
af53b43c
KH
4401 {
4402 if (face->ascii_face != face)
4403 {
4404 /* There's no more ASCII face. */
4405 face = NULL;
4406 break;
4407 }
4408 if (face->hash == hash
4409 && lface_equal_p (face->lface, attr))
4410 break;
4411 }
82641697
GM
4412
4413 /* If not found, realize a new face. */
4414 if (face == NULL)
af53b43c 4415 face = realize_face (cache, attr, -1);
82641697 4416
e509cfa6 4417#ifdef GLYPH_DEBUG
a54e2c05 4418 eassert (face == FACE_FROM_ID (f, face->id));
82641697 4419#endif /* GLYPH_DEBUG */
178c5d9c 4420
82641697
GM
4421 return face->id;
4422}
4423
2dee4c0b
KH
4424#ifdef HAVE_WINDOW_SYSTEM
4425/* Look up a realized face that has the same attributes as BASE_FACE
4426 except for the font in the face cache of frame F. If FONT-OBJECT
4427 is not nil, it is an already opened font. If FONT-OBJECT is nil,
4428 the face has no font. Value is the ID of the face found. If no
4429 suitable face is found, realize a new one. */
426b2119 4430
426b2119 4431int
971de7fb 4432face_for_font (struct frame *f, Lisp_Object font_object, struct face *base_face)
426b2119
KH
4433{
4434 struct face_cache *cache = FRAME_FACE_CACHE (f);
4435 unsigned hash;
4436 int i;
4437 struct face *face;
4438
a54e2c05 4439 eassert (cache != NULL);
426b2119
KH
4440 base_face = base_face->ascii_face;
4441 hash = lface_hash (base_face->lface);
4442 i = hash % FACE_CACHE_BUCKETS_SIZE;
4443
4444 for (face = cache->buckets[i]; face; face = face->next)
4445 {
4446 if (face->ascii_face == face)
4447 continue;
4448 if (face->ascii_face == base_face
2dee4c0b
KH
4449 && face->font == (NILP (font_object) ? NULL
4450 : XFONT_OBJECT (font_object))
4451 && lface_equal_p (face->lface, base_face->lface))
426b2119
KH
4452 return face->id;
4453 }
4454
4455 /* If not found, realize a new face. */
2dee4c0b 4456 face = realize_non_ascii_face (f, font_object, base_face);
426b2119
KH
4457 return face->id;
4458}
8c6204de 4459#endif /* HAVE_WINDOW_SYSTEM */
82641697 4460
82641697 4461/* Return the face id of the realized face for named face SYMBOL on
af53b43c
KH
4462 frame F suitable for displaying ASCII characters. Value is -1 if
4463 the face couldn't be determined, which might happen if the default
4464 face isn't realized and cannot be realized. */
82641697
GM
4465
4466int
971de7fb 4467lookup_named_face (struct frame *f, Lisp_Object symbol, int signal_p)
82641697 4468{
e7d7fd8c
MB
4469 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4470 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
82641697
GM
4471 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4472
b5de343d
GM
4473 if (default_face == NULL)
4474 {
4475 if (!realize_basic_faces (f))
4476 return -1;
4477 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
418ca4d2 4478 if (default_face == NULL)
1088b922 4479 emacs_abort (); /* realize_basic_faces must have set it up */
b5de343d
GM
4480 }
4481
f2cec7a9 4482 if (! get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
2272e967
KS
4483 return -1;
4484
72af86bd 4485 memcpy (attrs, default_face->lface, sizeof attrs);
e7d7fd8c 4486 merge_face_vectors (f, symbol_attrs, attrs, 0);
a0a23346 4487
af53b43c 4488 return lookup_face (f, attrs);
82641697
GM
4489}
4490
4491
1682701f 4492/* Return the display face-id of the basic face whose canonical face-id
f2cec7a9
MB
4493 is FACE_ID. The return value will usually simply be FACE_ID, unless that
4494 basic face has bee remapped via Vface_remapping_alist. This function is
4495 conservative: if something goes wrong, it will simply return FACE_ID
4496 rather than signal an error. */
4497
4498int
971de7fb 4499lookup_basic_face (struct frame *f, int face_id)
f2cec7a9
MB
4500{
4501 Lisp_Object name, mapping;
4502 int remapped_face_id;
4503
4504 if (NILP (Vface_remapping_alist))
4505 return face_id; /* Nothing to do. */
4506
4507 switch (face_id)
4508 {
4509 case DEFAULT_FACE_ID: name = Qdefault; break;
4510 case MODE_LINE_FACE_ID: name = Qmode_line; break;
4511 case MODE_LINE_INACTIVE_FACE_ID: name = Qmode_line_inactive; break;
4512 case HEADER_LINE_FACE_ID: name = Qheader_line; break;
4513 case TOOL_BAR_FACE_ID: name = Qtool_bar; break;
4514 case FRINGE_FACE_ID: name = Qfringe; break;
4515 case SCROLL_BAR_FACE_ID: name = Qscroll_bar; break;
4516 case BORDER_FACE_ID: name = Qborder; break;
4517 case CURSOR_FACE_ID: name = Qcursor; break;
4518 case MOUSE_FACE_ID: name = Qmouse; break;
4519 case MENU_FACE_ID: name = Qmenu; break;
4520
4521 default:
1088b922 4522 emacs_abort (); /* the caller is supposed to pass us a basic face id */
f2cec7a9
MB
4523 }
4524
4525 /* Do a quick scan through Vface_remapping_alist, and return immediately
4526 if there is no remapping for face NAME. This is just an optimization
4527 for the very common no-remapping case. */
4528 mapping = assq_no_quit (name, Vface_remapping_alist);
4529 if (NILP (mapping))
4530 return face_id; /* Give up. */
4531
4532 /* If there is a remapping entry, lookup the face using NAME, which will
4533 handle the remapping too. */
4534 remapped_face_id = lookup_named_face (f, name, 0);
4535 if (remapped_face_id < 0)
4536 return face_id; /* Give up. */
4537
4538 return remapped_face_id;
4539}
4540
4541
82641697
GM
4542/* Return a face for charset ASCII that is like the face with id
4543 FACE_ID on frame F, but has a font that is STEPS steps smaller.
4544 STEPS < 0 means larger. Value is the id of the face. */
4545
4546int
971de7fb 4547smaller_face (struct frame *f, int face_id, int steps)
39506348 4548{
c3cee013 4549#ifdef HAVE_WINDOW_SYSTEM
82641697
GM
4550 struct face *face;
4551 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4552 int pt, last_pt, last_height;
4553 int delta;
4554 int new_face_id;
4555 struct face *new_face;
4556
4557 /* If not called for an X frame, just return the original face. */
4558 if (FRAME_TERMCAP_P (f))
4559 return face_id;
4560
4561 /* Try in increments of 1/2 pt. */
4562 delta = steps < 0 ? 5 : -5;
1ea40aa2 4563 steps = eabs (steps);
178c5d9c 4564
82641697 4565 face = FACE_FROM_ID (f, face_id);
72af86bd 4566 memcpy (attrs, face->lface, sizeof attrs);
82641697
GM
4567 pt = last_pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
4568 new_face_id = face_id;
4569 last_height = FONT_HEIGHT (face->font);
4570
4571 while (steps
4572 && pt + delta > 0
4573 /* Give up if we cannot find a font within 10pt. */
1ea40aa2 4574 && eabs (last_pt - pt) < 100)
82641697
GM
4575 {
4576 /* Look up a face for a slightly smaller/larger font. */
4577 pt += delta;
4578 attrs[LFACE_HEIGHT_INDEX] = make_number (pt);
af53b43c 4579 new_face_id = lookup_face (f, attrs);
82641697
GM
4580 new_face = FACE_FROM_ID (f, new_face_id);
4581
4582 /* If height changes, count that as one step. */
b4c3ca09
GM
4583 if ((delta < 0 && FONT_HEIGHT (new_face->font) < last_height)
4584 || (delta > 0 && FONT_HEIGHT (new_face->font) > last_height))
82641697
GM
4585 {
4586 --steps;
4587 last_height = FONT_HEIGHT (new_face->font);
4588 last_pt = pt;
4589 }
4590 }
4591
4592 return new_face_id;
4593
c3cee013 4594#else /* not HAVE_WINDOW_SYSTEM */
82641697
GM
4595
4596 return face_id;
178c5d9c 4597
c3cee013 4598#endif /* not HAVE_WINDOW_SYSTEM */
82641697
GM
4599}
4600
4601
4602/* Return a face for charset ASCII that is like the face with id
4603 FACE_ID on frame F, but has height HEIGHT. */
4604
4605int
971de7fb 4606face_with_height (struct frame *f, int face_id, int height)
82641697 4607{
c3cee013 4608#ifdef HAVE_WINDOW_SYSTEM
82641697
GM
4609 struct face *face;
4610 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4611
4612 if (FRAME_TERMCAP_P (f)
4613 || height <= 0)
4614 return face_id;
4615
4616 face = FACE_FROM_ID (f, face_id);
72af86bd 4617 memcpy (attrs, face->lface, sizeof attrs);
82641697 4618 attrs[LFACE_HEIGHT_INDEX] = make_number (height);
a8c0cc18 4619 font_clear_prop (attrs, FONT_SIZE_INDEX);
af53b43c 4620 face_id = lookup_face (f, attrs);
c3cee013 4621#endif /* HAVE_WINDOW_SYSTEM */
178c5d9c 4622
82641697
GM
4623 return face_id;
4624}
4625
b5de343d 4626
44747bd0 4627/* Return the face id of the realized face for named face SYMBOL on
af53b43c
KH
4628 frame F suitable for displaying ASCII characters, and use
4629 attributes of the face FACE_ID for attributes that aren't
4630 completely specified by SYMBOL. This is like lookup_named_face,
4631 except that the default attributes come from FACE_ID, not from the
4632 default face. FACE_ID is assumed to be already realized. */
44747bd0
EZ
4633
4634int
b5f03016
AS
4635lookup_derived_face (struct frame *f, Lisp_Object symbol, int face_id,
4636 int signal_p)
44747bd0 4637{
e7d7fd8c 4638 Lisp_Object attrs[LFACE_VECTOR_SIZE];
44747bd0
EZ
4639 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4640 struct face *default_face = FACE_FROM_ID (f, face_id);
4641
4642 if (!default_face)
1088b922 4643 emacs_abort ();
44747bd0 4644
d8453278
CY
4645 if (!get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4646 return -1;
4647
72af86bd 4648 memcpy (attrs, default_face->lface, sizeof attrs);
e7d7fd8c 4649 merge_face_vectors (f, symbol_attrs, attrs, 0);
af53b43c 4650 return lookup_face (f, attrs);
44747bd0
EZ
4651}
4652
f6608d5c
RS
4653DEFUN ("face-attributes-as-vector", Fface_attributes_as_vector,
4654 Sface_attributes_as_vector, 1, 1, 0,
4bb962be 4655 doc: /* Return a vector of face attributes corresponding to PLIST. */)
5842a27b 4656 (Lisp_Object plist)
f6608d5c
RS
4657{
4658 Lisp_Object lface;
4659 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
4660 Qunspecified);
a0a23346
MB
4661 merge_face_ref (XFRAME (selected_frame), plist, XVECTOR (lface)->contents,
4662 1, 0);
f6608d5c
RS
4663 return lface;
4664}
4665
82641697
GM
4666
4667\f
9717e36c
MB
4668/***********************************************************************
4669 Face capability testing
4670 ***********************************************************************/
4671
4672
4673/* If the distance (as returned by color_distance) between two colors is
4674 less than this, then they are considered the same, for determining
4675 whether a color is supported or not. The range of values is 0-65535. */
4676
4677#define TTY_SAME_COLOR_THRESHOLD 10000
4678
ccda4e3c 4679#ifdef HAVE_WINDOW_SYSTEM
9717e36c
MB
4680
4681/* Return non-zero if all the face attributes in ATTRS are supported
4682 on the window-system frame F.
4683
4684 The definition of `supported' is somewhat heuristic, but basically means
4685 that a face containing all the attributes in ATTRS, when merged with the
4686 default face for display, can be represented in a way that's
4687
4688 \(1) different in appearance than the default face, and
8e330b22 4689 \(2) `close in spirit' to what the attributes specify, if not exact. */
9717e36c
MB
4690
4691static int
4973679b
PE
4692x_supports_face_attributes_p (struct frame *f,
4693 Lisp_Object attrs[LFACE_VECTOR_SIZE],
b5f03016 4694 struct face *def_face)
9717e36c 4695{
8e330b22 4696 Lisp_Object *def_attrs = def_face->lface;
9717e36c
MB
4697
4698 /* Check that other specified attributes are different that the default
4699 face. */
4700 if ((!UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
4701 && face_attr_equal_p (attrs[LFACE_UNDERLINE_INDEX],
4702 def_attrs[LFACE_UNDERLINE_INDEX]))
4703 || (!UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
4704 && face_attr_equal_p (attrs[LFACE_INVERSE_INDEX],
4705 def_attrs[LFACE_INVERSE_INDEX]))
4706 || (!UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
4707 && face_attr_equal_p (attrs[LFACE_FOREGROUND_INDEX],
4708 def_attrs[LFACE_FOREGROUND_INDEX]))
4709 || (!UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
4710 && face_attr_equal_p (attrs[LFACE_BACKGROUND_INDEX],
4711 def_attrs[LFACE_BACKGROUND_INDEX]))
4712 || (!UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4713 && face_attr_equal_p (attrs[LFACE_STIPPLE_INDEX],
4714 def_attrs[LFACE_STIPPLE_INDEX]))
4715 || (!UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4716 && face_attr_equal_p (attrs[LFACE_OVERLINE_INDEX],
4717 def_attrs[LFACE_OVERLINE_INDEX]))
4718 || (!UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4719 && face_attr_equal_p (attrs[LFACE_STRIKE_THROUGH_INDEX],
4720 def_attrs[LFACE_STRIKE_THROUGH_INDEX]))
4721 || (!UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
4722 && face_attr_equal_p (attrs[LFACE_BOX_INDEX],
4723 def_attrs[LFACE_BOX_INDEX])))
4724 return 0;
4725
4726 /* Check font-related attributes, as those are the most commonly
4727 "unsupported" on a window-system (because of missing fonts). */
4728 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
53aaf1e2 4729 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
9717e36c
MB
4730 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4731 || !UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
4732 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
2dee4c0b 4733 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX]))
9717e36c 4734 {
327719ee 4735 int face_id;
9717e36c
MB
4736 struct face *face;
4737 Lisp_Object merged_attrs[LFACE_VECTOR_SIZE];
2dee4c0b 4738 int i;
9717e36c 4739
72af86bd 4740 memcpy (merged_attrs, def_attrs, sizeof merged_attrs);
9717e36c 4741
a0a23346 4742 merge_face_vectors (f, attrs, merged_attrs, 0);
9717e36c 4743
327719ee
MB
4744 face_id = lookup_face (f, merged_attrs);
4745 face = FACE_FROM_ID (f, face_id);
9717e36c
MB
4746
4747 if (! face)
2010ba8c 4748 error ("Cannot make face");
9717e36c 4749
4fc1984a
KH
4750 /* If the font is the same, or no font is found, then not
4751 supported. */
4752 if (face->font == def_face->font
4753 || ! face->font)
9717e36c 4754 return 0;
2dee4c0b
KH
4755 for (i = FONT_TYPE_INDEX; i <= FONT_SIZE_INDEX; i++)
4756 if (! EQ (face->font->props[i], def_face->font->props[i]))
4757 {
4758 Lisp_Object s1, s2;
4759
4760 if (i < FONT_FOUNDRY_INDEX || i > FONT_REGISTRY_INDEX
4761 || face->font->driver->case_sensitive)
4762 return 1;
4763 s1 = SYMBOL_NAME (face->font->props[i]);
4764 s2 = SYMBOL_NAME (def_face->font->props[i]);
4765 if (! EQ (Fcompare_strings (s1, make_number (0), Qnil,
4766 s2, make_number (0), Qnil, Qt), Qt))
4767 return 1;
4768 }
4769 return 0;
9717e36c
MB
4770 }
4771
4772 /* Everything checks out, this face is supported. */
4773 return 1;
4774}
4775
ccda4e3c 4776#endif /* HAVE_WINDOW_SYSTEM */
9717e36c
MB
4777
4778/* Return non-zero if all the face attributes in ATTRS are supported
4779 on the tty frame F.
4780
4781 The definition of `supported' is somewhat heuristic, but basically means
4782 that a face containing all the attributes in ATTRS, when merged
4783 with the default face for display, can be represented in a way that's
4784
4785 \(1) different in appearance than the default face, and
4786 \(2) `close in spirit' to what the attributes specify, if not exact.
4787
4788 Point (2) implies that a `:weight black' attribute will be satisfied
4789 by any terminal that can display bold, and a `:foreground "yellow"' as
4790 long as the terminal can display a yellowish color, but `:slant italic'
4791 will _not_ be satisfied by the tty display code's automatic
4792 substitution of a `dim' face for italic. */
4793
4794static int
4973679b
PE
4795tty_supports_face_attributes_p (struct frame *f,
4796 Lisp_Object attrs[LFACE_VECTOR_SIZE],
b5f03016 4797 struct face *def_face)
9717e36c 4798{
cd4eb164 4799 int weight, slant;
9717e36c
MB
4800 Lisp_Object val, fg, bg;
4801 XColor fg_tty_color, fg_std_color;
4802 XColor bg_tty_color, bg_std_color;
4803 unsigned test_caps = 0;
8e330b22 4804 Lisp_Object *def_attrs = def_face->lface;
9717e36c 4805
8e330b22
MB
4806 /* First check some easy-to-check stuff; ttys support none of the
4807 following attributes, so we can just return false if any are requested
4808 (even if `nominal' values are specified, we should still return false,
4809 as that will be the same value that the default face uses). We
4810 consider :slant unsupportable on ttys, even though the face code
4811 actually `fakes' them using a dim attribute if possible. This is
4812 because the faked result is too different from what the face
4813 specifies. */
4814 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
53aaf1e2 4815 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
8e330b22
MB
4816 || !UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4817 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4818 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
4819 || !UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4820 || !UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
cd4eb164 4821 || !UNSPECIFIEDP (attrs[LFACE_BOX_INDEX]))
9717e36c
MB
4822 return 0;
4823
9717e36c
MB
4824 /* Test for terminal `capabilities' (non-color character attributes). */
4825
4826 /* font weight (bold/dim) */
337fbd17
CY
4827 val = attrs[LFACE_WEIGHT_INDEX];
4828 if (!UNSPECIFIEDP (val)
4829 && (weight = FONT_WEIGHT_NAME_NUMERIC (val), weight >= 0))
9717e36c 4830 {
2dee4c0b 4831 int def_weight = FONT_WEIGHT_NAME_NUMERIC (def_attrs[LFACE_WEIGHT_INDEX]);
8e330b22 4832
2dee4c0b 4833 if (weight > 100)
8e330b22 4834 {
2dee4c0b 4835 if (def_weight > 100)
8e330b22
MB
4836 return 0; /* same as default */
4837 test_caps = TTY_CAP_BOLD;
4838 }
2dee4c0b 4839 else if (weight < 100)
8e330b22 4840 {
2dee4c0b 4841 if (def_weight < 100)
8e330b22
MB
4842 return 0; /* same as default */
4843 test_caps = TTY_CAP_DIM;
4844 }
2dee4c0b 4845 else if (def_weight == 100)
8e330b22 4846 return 0; /* same as default */
9717e36c
MB
4847 }
4848
cd4eb164
CY
4849 /* font slant */
4850 val = attrs[LFACE_SLANT_INDEX];
4851 if (!UNSPECIFIEDP (val)
4852 && (slant = FONT_SLANT_NAME_NUMERIC (val), slant >= 0))
4853 {
4854 int def_slant = FONT_SLANT_NAME_NUMERIC (def_attrs[LFACE_SLANT_INDEX]);
4855 if (slant == 100 || slant == def_slant)
4856 return 0; /* same as default */
4857 else
4858 test_caps |= TTY_CAP_ITALIC;
4859 }
4860
9717e36c
MB
4861 /* underlining */
4862 val = attrs[LFACE_UNDERLINE_INDEX];
8e330b22 4863 if (!UNSPECIFIEDP (val))
9717e36c
MB
4864 {
4865 if (STRINGP (val))
8e330b22 4866 return 0; /* ttys can't use colored underlines */
073ca75b
JL
4867 else if (EQ (CAR_SAFE (val), QCstyle) && EQ (CAR_SAFE (CDR_SAFE (val)), Qwave))
4868 return 0; /* ttys can't use wave underlines */
8e330b22
MB
4869 else if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
4870 return 0; /* same as default */
9717e36c
MB
4871 else
4872 test_caps |= TTY_CAP_UNDERLINE;
4873 }
4874
4875 /* inverse video */
4876 val = attrs[LFACE_INVERSE_INDEX];
8e330b22
MB
4877 if (!UNSPECIFIEDP (val))
4878 {
a13ab63f 4879 if (face_attr_equal_p (val, def_attrs[LFACE_INVERSE_INDEX]))
8e330b22
MB
4880 return 0; /* same as default */
4881 else
4882 test_caps |= TTY_CAP_INVERSE;
4883 }
9717e36c
MB
4884
4885
4886 /* Color testing. */
4887
4888 /* Default the color indices in FG_TTY_COLOR and BG_TTY_COLOR, since
4889 we use them when calling `tty_capable_p' below, even if the face
4890 specifies no colors. */
4891 fg_tty_color.pixel = FACE_TTY_DEFAULT_FG_COLOR;
4892 bg_tty_color.pixel = FACE_TTY_DEFAULT_BG_COLOR;
4893
4894 /* Check if foreground color is close enough. */
4895 fg = attrs[LFACE_FOREGROUND_INDEX];
4896 if (STRINGP (fg))
4897 {
8e330b22
MB
4898 Lisp_Object def_fg = def_attrs[LFACE_FOREGROUND_INDEX];
4899
4900 if (face_attr_equal_p (fg, def_fg))
4901 return 0; /* same as default */
4902 else if (! tty_lookup_color (f, fg, &fg_tty_color, &fg_std_color))
4903 return 0; /* not a valid color */
9717e36c
MB
4904 else if (color_distance (&fg_tty_color, &fg_std_color)
4905 > TTY_SAME_COLOR_THRESHOLD)
8e330b22
MB
4906 return 0; /* displayed color is too different */
4907 else
4908 /* Make sure the color is really different than the default. */
4909 {
4910 XColor def_fg_color;
4911 if (tty_lookup_color (f, def_fg, &def_fg_color, 0)
4912 && (color_distance (&fg_tty_color, &def_fg_color)
4913 <= TTY_SAME_COLOR_THRESHOLD))
4914 return 0;
4915 }
9717e36c
MB
4916 }
4917
4918 /* Check if background color is close enough. */
4919 bg = attrs[LFACE_BACKGROUND_INDEX];
4920 if (STRINGP (bg))
4921 {
a13ab63f 4922 Lisp_Object def_bg = def_attrs[LFACE_BACKGROUND_INDEX];
8e330b22
MB
4923
4924 if (face_attr_equal_p (bg, def_bg))
4925 return 0; /* same as default */
4926 else if (! tty_lookup_color (f, bg, &bg_tty_color, &bg_std_color))
4927 return 0; /* not a valid color */
9717e36c
MB
4928 else if (color_distance (&bg_tty_color, &bg_std_color)
4929 > TTY_SAME_COLOR_THRESHOLD)
8e330b22
MB
4930 return 0; /* displayed color is too different */
4931 else
4932 /* Make sure the color is really different than the default. */
4933 {
4934 XColor def_bg_color;
4935 if (tty_lookup_color (f, def_bg, &def_bg_color, 0)
4936 && (color_distance (&bg_tty_color, &def_bg_color)
4937 <= TTY_SAME_COLOR_THRESHOLD))
4938 return 0;
4939 }
9717e36c
MB
4940 }
4941
4942 /* If both foreground and background are requested, see if the
4943 distance between them is OK. We just check to see if the distance
4944 between the tty's foreground and background is close enough to the
4945 distance between the standard foreground and background. */
4946 if (STRINGP (fg) && STRINGP (bg))
4947 {
4948 int delta_delta
4949 = (color_distance (&fg_std_color, &bg_std_color)
4950 - color_distance (&fg_tty_color, &bg_tty_color));
4951 if (delta_delta > TTY_SAME_COLOR_THRESHOLD
4952 || delta_delta < -TTY_SAME_COLOR_THRESHOLD)
4953 return 0;
4954 }
4955
4956
4957 /* See if the capabilities we selected above are supported, with the
4958 given colors. */
4959 if (test_caps != 0 &&
b5f03016
AS
4960 ! tty_capable_p (FRAME_TTY (f), test_caps, fg_tty_color.pixel,
4961 bg_tty_color.pixel))
9717e36c
MB
4962 return 0;
4963
4964
4965 /* Hmmm, everything checks out, this terminal must support this face. */
4966 return 1;
4967}
4968
4969
4970DEFUN ("display-supports-face-attributes-p",
4971 Fdisplay_supports_face_attributes_p, Sdisplay_supports_face_attributes_p,
4972 1, 2, 0,
4973 doc: /* Return non-nil if all the face attributes in ATTRIBUTES are supported.
4974The optional argument DISPLAY can be a display name, a frame, or
9fed2905 4975nil (meaning the selected frame's display).
9717e36c
MB
4976
4977The definition of `supported' is somewhat heuristic, but basically means
4978that a face containing all the attributes in ATTRIBUTES, when merged
4979with the default face for display, can be represented in a way that's
4980
4981 \(1) different in appearance than the default face, and
4982 \(2) `close in spirit' to what the attributes specify, if not exact.
4983
4984Point (2) implies that a `:weight black' attribute will be satisfied by
4985any display that can display bold, and a `:foreground \"yellow\"' as long
4986as it can display a yellowish color, but `:slant italic' will _not_ be
4987satisfied by the tty display code's automatic substitution of a `dim'
9fed2905 4988face for italic. */)
5842a27b 4989 (Lisp_Object attributes, Lisp_Object display)
9717e36c 4990{
221439a0 4991 int supports = 0, i;
9717e36c
MB
4992 Lisp_Object frame;
4993 struct frame *f;
8e330b22 4994 struct face *def_face;
9717e36c
MB
4995 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4996
0722292b
MB
4997 if (noninteractive || !initialized)
4998 /* We may not be able to access low-level face information in batch
4999 mode, or before being dumped, and this function is not going to
5000 be very useful in those cases anyway, so just give up. */
5001 return Qnil;
5002
9717e36c
MB
5003 if (NILP (display))
5004 frame = selected_frame;
5005 else if (FRAMEP (display))
5006 frame = display;
5007 else
5008 {
5009 /* Find any frame on DISPLAY. */
5b04e9f9 5010 Lisp_Object tail;
9717e36c
MB
5011
5012 frame = Qnil;
5b04e9f9
DA
5013 FOR_EACH_FRAME (tail, frame)
5014 if (!NILP (Fequal (Fcdr (Fassq (Qdisplay,
5015 XFRAME (frame)->param_alist)),
5016 display)))
5017 break;
9717e36c
MB
5018 }
5019
5020 CHECK_LIVE_FRAME (frame);
5021 f = XFRAME (frame);
5022
5023 for (i = 0; i < LFACE_VECTOR_SIZE; i++)
5024 attrs[i] = Qunspecified;
a0a23346 5025 merge_face_ref (f, attributes, attrs, 1, 0);
9717e36c 5026
8e330b22
MB
5027 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5028 if (def_face == NULL)
5029 {
5030 if (! realize_basic_faces (f))
734e9514 5031 error ("Cannot realize default face");
8e330b22 5032 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
418ca4d2 5033 if (def_face == NULL)
1088b922 5034 emacs_abort (); /* realize_basic_faces must have set it up */
8e330b22
MB
5035 }
5036
9717e36c
MB
5037 /* Dispatch to the appropriate handler. */
5038 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
8e330b22 5039 supports = tty_supports_face_attributes_p (f, attrs, def_face);
ccda4e3c 5040#ifdef HAVE_WINDOW_SYSTEM
9717e36c 5041 else
8e330b22
MB
5042 supports = x_supports_face_attributes_p (f, attrs, def_face);
5043#endif
9717e36c
MB
5044
5045 return supports ? Qt : Qnil;
5046}
5047
5048\f
82641697
GM
5049/***********************************************************************
5050 Font selection
5051 ***********************************************************************/
5052
2c7d1565 5053DEFUN ("internal-set-font-selection-order",
82641697
GM
5054 Finternal_set_font_selection_order,
5055 Sinternal_set_font_selection_order, 1, 1, 0,
7ee72033 5056 doc: /* Set font selection order for face font selection to ORDER.
228299fa
GM
5057ORDER must be a list of length 4 containing the symbols `:width',
5058`:height', `:weight', and `:slant'. Face attributes appearing
5059first in ORDER are matched first, e.g. if `:height' appears before
5060`:weight' in ORDER, font selection first tries to find a font with
5061a suitable height, and then tries to match the font weight.
7ee72033 5062Value is ORDER. */)
5842a27b 5063 (Lisp_Object order)
82641697
GM
5064{
5065 Lisp_Object list;
5066 int i;
a08332c0 5067 int indices[DIM (font_sort_order)];
178c5d9c 5068
b7826503 5069 CHECK_LIST (order);
72af86bd 5070 memset (indices, 0, sizeof indices);
82641697
GM
5071 i = 0;
5072
5073 for (list = order;
5074 CONSP (list) && i < DIM (indices);
5075 list = XCDR (list), ++i)
5076 {
5077 Lisp_Object attr = XCAR (list);
5078 int xlfd;
5079
5080 if (EQ (attr, QCwidth))
5081 xlfd = XLFD_SWIDTH;
5082 else if (EQ (attr, QCheight))
5083 xlfd = XLFD_POINT_SIZE;
5084 else if (EQ (attr, QCweight))
5085 xlfd = XLFD_WEIGHT;
5086 else if (EQ (attr, QCslant))
5087 xlfd = XLFD_SLANT;
5088 else
5089 break;
5090
5091 if (indices[i] != 0)
5092 break;
5093 indices[i] = xlfd;
5094 }
5095
a08332c0 5096 if (!NILP (list) || i != DIM (indices))
82641697 5097 signal_error ("Invalid font sort order", order);
a08332c0
GM
5098 for (i = 0; i < DIM (font_sort_order); ++i)
5099 if (indices[i] == 0)
5100 signal_error ("Invalid font sort order", order);
82641697 5101
72af86bd 5102 if (memcmp (indices, font_sort_order, sizeof indices) != 0)
82641697 5103 {
72af86bd 5104 memcpy (font_sort_order, indices, sizeof font_sort_order);
82641697
GM
5105 free_all_realized_faces (Qnil);
5106 }
178c5d9c 5107
2dee4c0b 5108 font_update_sort_order (font_sort_order);
426b2119 5109
82641697
GM
5110 return Qnil;
5111}
5112
5113
5114DEFUN ("internal-set-alternative-font-family-alist",
5115 Finternal_set_alternative_font_family_alist,
5116 Sinternal_set_alternative_font_family_alist, 1, 1, 0,
c71f3632 5117 doc: /* Define alternative font families to try in face font selection.
228299fa
GM
5118ALIST is an alist of (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5119Each ALTERNATIVE is tried in order if no fonts of font family FAMILY can
7ee72033 5120be found. Value is ALIST. */)
5842a27b 5121 (Lisp_Object alist)
82641697 5122{
a77d5bb2 5123 Lisp_Object entry, tail, tail2;
53aaf1e2 5124
b7826503 5125 CHECK_LIST (alist);
53aaf1e2
KH
5126 alist = Fcopy_sequence (alist);
5127 for (tail = alist; CONSP (tail); tail = XCDR (tail))
a77d5bb2
CY
5128 {
5129 entry = XCAR (tail);
5130 CHECK_LIST (entry);
5131 entry = Fcopy_sequence (entry);
5132 XSETCAR (tail, entry);
5133 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5134 XSETCAR (tail2, Fintern (XCAR (tail2), Qnil));
5135 }
5136
82641697
GM
5137 Vface_alternative_font_family_alist = alist;
5138 free_all_realized_faces (Qnil);
5139 return alist;
5140}
5141
5142
32fcc231
GM
5143DEFUN ("internal-set-alternative-font-registry-alist",
5144 Finternal_set_alternative_font_registry_alist,
5145 Sinternal_set_alternative_font_registry_alist, 1, 1, 0,
e3cd9e7f 5146 doc: /* Define alternative font registries to try in face font selection.
228299fa
GM
5147ALIST is an alist of (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5148Each ALTERNATIVE is tried in order if no fonts of font registry REGISTRY can
7ee72033 5149be found. Value is ALIST. */)
5842a27b 5150 (Lisp_Object alist)
32fcc231 5151{
a77d5bb2 5152 Lisp_Object entry, tail, tail2;
53aaf1e2 5153
b7826503 5154 CHECK_LIST (alist);
53aaf1e2
KH
5155 alist = Fcopy_sequence (alist);
5156 for (tail = alist; CONSP (tail); tail = XCDR (tail))
a77d5bb2
CY
5157 {
5158 entry = XCAR (tail);
5159 CHECK_LIST (entry);
5160 entry = Fcopy_sequence (entry);
5161 XSETCAR (tail, entry);
5162 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5163 XSETCAR (tail2, Fdowncase (XCAR (tail2)));
5164 }
32fcc231
GM
5165 Vface_alternative_font_registry_alist = alist;
5166 free_all_realized_faces (Qnil);
5167 return alist;
5168}
5169
5170
c3cee013 5171#ifdef HAVE_WINDOW_SYSTEM
82641697 5172
39506348
KH
5173/* Return the fontset id of the base fontset name or alias name given
5174 by the fontset attribute of ATTRS. Value is -1 if the fontset
5175 attribute of ATTRS doesn't name a fontset. */
82641697
GM
5176
5177static int
4973679b 5178face_fontset (Lisp_Object attrs[LFACE_VECTOR_SIZE])
82641697 5179{
39506348 5180 Lisp_Object name;
178c5d9c 5181
763bc839 5182 name = attrs[LFACE_FONTSET_INDEX];
39506348
KH
5183 if (!STRINGP (name))
5184 return -1;
5185 return fs_query_fontset (name, 0);
82641697
GM
5186}
5187
c3cee013 5188#endif /* HAVE_WINDOW_SYSTEM */
82641697
GM
5189
5190
5191\f
5192/***********************************************************************
5193 Face Realization
5194 ***********************************************************************/
5195
5196/* Realize basic faces on frame F. Value is zero if frame parameters
5197 of F don't contain enough information needed to realize the default
5198 face. */
5199
d7e6881a 5200static bool
971de7fb 5201realize_basic_faces (struct frame *f)
82641697 5202{
d7e6881a 5203 bool success_p = 0;
d311d28c 5204 ptrdiff_t count = SPECPDL_INDEX ();
17e8204b 5205
04386463
GM
5206 /* Block input here so that we won't be surprised by an X expose
5207 event, for instance, without having the faces set up. */
4d7e6e51 5208 block_input ();
eeffb293 5209 specbind (Qscalable_fonts_allowed, Qt);
178c5d9c 5210
82641697
GM
5211 if (realize_default_face (f))
5212 {
92610620 5213 realize_named_face (f, Qmode_line, MODE_LINE_FACE_ID);
039b6394 5214 realize_named_face (f, Qmode_line_inactive, MODE_LINE_INACTIVE_FACE_ID);
9ea173e8 5215 realize_named_face (f, Qtool_bar, TOOL_BAR_FACE_ID);
4e50fa8b 5216 realize_named_face (f, Qfringe, FRINGE_FACE_ID);
045dee35 5217 realize_named_face (f, Qheader_line, HEADER_LINE_FACE_ID);
8bd201d6
GM
5218 realize_named_face (f, Qscroll_bar, SCROLL_BAR_FACE_ID);
5219 realize_named_face (f, Qborder, BORDER_FACE_ID);
5220 realize_named_face (f, Qcursor, CURSOR_FACE_ID);
5221 realize_named_face (f, Qmouse, MOUSE_FACE_ID);
c7ae3284 5222 realize_named_face (f, Qmenu, MENU_FACE_ID);
53abc3bf 5223 realize_named_face (f, Qvertical_border, VERTICAL_BORDER_FACE_ID);
563f68f1 5224
b5de343d 5225 /* Reflect changes in the `menu' face in menu bars. */
ceeda019 5226 if (FRAME_FACE_CACHE (f)->menu_face_changed_p)
563f68f1 5227 {
ceeda019 5228 FRAME_FACE_CACHE (f)->menu_face_changed_p = 0;
563f68f1 5229#ifdef USE_X_TOOLKIT
b5f03016
AS
5230 if (FRAME_WINDOW_P (f))
5231 x_update_menu_appearance (f);
a03ad468 5232#endif
563f68f1 5233 }
177c0ea7 5234
82641697
GM
5235 success_p = 1;
5236 }
5237
eeffb293 5238 unbind_to (count, Qnil);
4d7e6e51 5239 unblock_input ();
82641697
GM
5240 return success_p;
5241}
5242
5243
5244/* Realize the default face on frame F. If the face is not fully
5245 specified, make it fully-specified. Attributes of the default face
5246 that are not explicitly specified are taken from frame parameters. */
5247
d7e6881a 5248static bool
971de7fb 5249realize_default_face (struct frame *f)
82641697
GM
5250{
5251 struct face_cache *c = FRAME_FACE_CACHE (f);
5252 Lisp_Object lface;
5253 Lisp_Object attrs[LFACE_VECTOR_SIZE];
82641697 5254 struct face *face;
82641697
GM
5255
5256 /* If the `default' face is not yet known, create it. */
5257 lface = lface_from_face_name (f, Qdefault, 0);
5258 if (NILP (lface))
1682701f 5259 {
07446869
GM
5260 Lisp_Object frame;
5261 XSETFRAME (frame, f);
5262 lface = Finternal_make_lisp_face (Qdefault, frame);
1682701f 5263 }
07446869 5264
c3cee013
JR
5265#ifdef HAVE_WINDOW_SYSTEM
5266 if (FRAME_WINDOW_P (f))
82641697 5267 {
2dee4c0b
KH
5268 Lisp_Object font_object;
5269
5270 XSETFONT (font_object, FRAME_FONT (f));
5271 set_lface_from_font (f, lface, font_object, f->default_face_done_p);
4939150c 5272 ASET (lface, LFACE_FONTSET_INDEX, fontset_name (FRAME_FONTSET (f)));
a5f696ac 5273 f->default_face_done_p = 1;
82641697 5274 }
c3cee013 5275#endif /* HAVE_WINDOW_SYSTEM */
82641697 5276
44747bd0 5277 if (!FRAME_WINDOW_P (f))
82641697 5278 {
4939150c
PE
5279 ASET (lface, LFACE_FAMILY_INDEX, build_string ("default"));
5280 ASET (lface, LFACE_FOUNDRY_INDEX, LFACE_FAMILY (lface));
5281 ASET (lface, LFACE_SWIDTH_INDEX, Qnormal);
5282 ASET (lface, LFACE_HEIGHT_INDEX, make_number (1));
c1e7532d 5283 if (UNSPECIFIEDP (LFACE_WEIGHT (lface)))
4939150c 5284 ASET (lface, LFACE_WEIGHT_INDEX, Qnormal);
c1e7532d 5285 if (UNSPECIFIEDP (LFACE_SLANT (lface)))
4939150c 5286 ASET (lface, LFACE_SLANT_INDEX, Qnormal);
70d6ecc6 5287 if (UNSPECIFIEDP (LFACE_FONTSET (lface)))
4939150c 5288 ASET (lface, LFACE_FONTSET_INDEX, Qnil);
82641697 5289 }
178c5d9c 5290
82641697 5291 if (UNSPECIFIEDP (LFACE_UNDERLINE (lface)))
4939150c 5292 ASET (lface, LFACE_UNDERLINE_INDEX, Qnil);
178c5d9c 5293
82641697 5294 if (UNSPECIFIEDP (LFACE_OVERLINE (lface)))
4939150c 5295 ASET (lface, LFACE_OVERLINE_INDEX, Qnil);
178c5d9c 5296
82641697 5297 if (UNSPECIFIEDP (LFACE_STRIKE_THROUGH (lface)))
4939150c 5298 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, Qnil);
178c5d9c 5299
82641697 5300 if (UNSPECIFIEDP (LFACE_BOX (lface)))
4939150c 5301 ASET (lface, LFACE_BOX_INDEX, Qnil);
178c5d9c 5302
82641697 5303 if (UNSPECIFIEDP (LFACE_INVERSE (lface)))
4939150c 5304 ASET (lface, LFACE_INVERSE_INDEX, Qnil);
178c5d9c 5305
82641697
GM
5306 if (UNSPECIFIEDP (LFACE_FOREGROUND (lface)))
5307 {
5308 /* This function is called so early that colors are not yet
5309 set in the frame parameter list. */
e69b0960 5310 Lisp_Object color = Fassq (Qforeground_color, f->param_alist);
178c5d9c 5311
82641697 5312 if (CONSP (color) && STRINGP (XCDR (color)))
4939150c 5313 ASET (lface, LFACE_FOREGROUND_INDEX, XCDR (color));
c3cee013 5314 else if (FRAME_WINDOW_P (f))
82641697 5315 return 0;
3224dac1 5316 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
4939150c 5317 ASET (lface, LFACE_FOREGROUND_INDEX, build_string (unspecified_fg));
f9d2fdc4 5318 else
1088b922 5319 emacs_abort ();
82641697 5320 }
178c5d9c 5321
82641697
GM
5322 if (UNSPECIFIEDP (LFACE_BACKGROUND (lface)))
5323 {
5324 /* This function is called so early that colors are not yet
5325 set in the frame parameter list. */
e69b0960 5326 Lisp_Object color = Fassq (Qbackground_color, f->param_alist);
82641697 5327 if (CONSP (color) && STRINGP (XCDR (color)))
4939150c 5328 ASET (lface, LFACE_BACKGROUND_INDEX, XCDR (color));
c3cee013 5329 else if (FRAME_WINDOW_P (f))
82641697 5330 return 0;
3224dac1 5331 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
4939150c 5332 ASET (lface, LFACE_BACKGROUND_INDEX, build_string (unspecified_bg));
f9d2fdc4 5333 else
1088b922 5334 emacs_abort ();
82641697 5335 }
178c5d9c 5336
82641697 5337 if (UNSPECIFIEDP (LFACE_STIPPLE (lface)))
4939150c 5338 ASET (lface, LFACE_STIPPLE_INDEX, Qnil);
82641697
GM
5339
5340 /* Realize the face; it must be fully-specified now. */
a54e2c05 5341 eassert (lface_fully_specified_p (XVECTOR (lface)->contents));
82641697 5342 check_lface (lface);
72af86bd 5343 memcpy (attrs, XVECTOR (lface)->contents, sizeof attrs);
af53b43c 5344 face = realize_face (c, attrs, DEFAULT_FACE_ID);
4da9c136
KH
5345
5346#ifdef HAVE_WINDOW_SYSTEM
41a9b76e 5347#ifdef HAVE_X_WINDOWS
361c0d6e 5348 if (FRAME_X_P (f) && face->font != FRAME_FONT (f))
73158a39
CY
5349 {
5350 /* This can happen when making a frame on a display that does
b5f03016 5351 not support the default font. */
73158a39 5352 if (!face->font)
b5f03016 5353 return 0;
d5ab09cd 5354
73158a39 5355 /* Otherwise, the font specified for the frame was not
b5f03016
AS
5356 acceptable as a font for the default face (perhaps because
5357 auto-scaled fonts are rejected), so we must adjust the frame
5358 font. */
2dee4c0b 5359 x_set_font (f, LFACE_FONT (lface), Qnil);
73158a39 5360 }
4da9c136
KH
5361#endif /* HAVE_X_WINDOWS */
5362#endif /* HAVE_WINDOW_SYSTEM */
82641697
GM
5363 return 1;
5364}
5365
5366
5367/* Realize basic faces other than the default face in face cache C.
5368 SYMBOL is the face name, ID is the face id the realized face must
5369 have. The default face must have been realized already. */
5370
5371static void
971de7fb 5372realize_named_face (struct frame *f, Lisp_Object symbol, int id)
82641697 5373{
e7d7fd8c 5374 struct face_cache *c = FRAME_FACE_CACHE (f);
82641697
GM
5375 Lisp_Object lface = lface_from_face_name (f, symbol, 0);
5376 Lisp_Object attrs[LFACE_VECTOR_SIZE];
e7d7fd8c 5377 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
82641697
GM
5378
5379 /* The default face must exist and be fully specified. */
f2cec7a9 5380 get_lface_attributes_no_remap (f, Qdefault, attrs, 1);
82641697 5381 check_lface_attrs (attrs);
a54e2c05 5382 eassert (lface_fully_specified_p (attrs));
82641697 5383
e7d7fd8c 5384 /* If SYMBOL isn't know as a face, create it. */
82641697
GM
5385 if (NILP (lface))
5386 {
5387 Lisp_Object frame;
5388 XSETFRAME (frame, f);
5389 lface = Finternal_make_lisp_face (symbol, frame);
5390 }
5391
5392 /* Merge SYMBOL's face with the default face. */
f2cec7a9 5393 get_lface_attributes_no_remap (f, symbol, symbol_attrs, 1);
e7d7fd8c
MB
5394 merge_face_vectors (f, symbol_attrs, attrs, 0);
5395
5396 /* Realize the face. */
a5a62657 5397 realize_face (c, attrs, id);
82641697
GM
5398}
5399
5400
5401/* Realize the fully-specified face with attributes ATTRS in face
af53b43c
KH
5402 cache CACHE for ASCII characters. If FORMER_FACE_ID is
5403 non-negative, it is an ID of face to remove before caching the new
5404 face. Value is a pointer to the newly created realized face. */
82641697
GM
5405
5406static struct face *
4973679b
PE
5407realize_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE],
5408 int former_face_id)
82641697
GM
5409{
5410 struct face *face;
178c5d9c 5411
82641697 5412 /* LFACE must be fully specified. */
a54e2c05 5413 eassert (cache != NULL);
82641697
GM
5414 check_lface_attrs (attrs);
5415
39506348
KH
5416 if (former_face_id >= 0 && cache->used > former_face_id)
5417 {
5418 /* Remove the former face. */
5419 struct face *former_face = cache->faces_by_id[former_face_id];
5420 uncache_face (cache, former_face);
5421 free_realized_face (cache->f, former_face);
7c33a057 5422 SET_FRAME_GARBAGED (cache->f);
39506348
KH
5423 }
5424
5425 if (FRAME_WINDOW_P (cache->f))
af53b43c 5426 face = realize_x_face (cache, attrs);
e689ec06 5427 else if (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f))
af53b43c 5428 face = realize_tty_face (cache, attrs);
d448e982
KL
5429 else if (FRAME_INITIAL_P (cache->f))
5430 {
5431 /* Create a dummy face. */
5432 face = make_realized_face (attrs);
5433 }
82641697 5434 else
1088b922 5435 emacs_abort ();
82641697 5436
39506348
KH
5437 /* Insert the new face. */
5438 cache_face (cache, face, lface_hash (attrs));
af53b43c
KH
5439 return face;
5440}
5441
5442
8c6204de 5443#ifdef HAVE_WINDOW_SYSTEM
2dee4c0b
KH
5444/* Realize the fully-specified face that uses FONT-OBJECT and has the
5445 same attributes as BASE_FACE except for the font on frame F.
5446 FONT-OBJECT may be nil, in which case, realized a face of
5447 no-font. */
af53b43c
KH
5448
5449static struct face *
b5f03016
AS
5450realize_non_ascii_face (struct frame *f, Lisp_Object font_object,
5451 struct face *base_face)
af53b43c
KH
5452{
5453 struct face_cache *cache = FRAME_FACE_CACHE (f);
8f924df7 5454 struct face *face;
af53b43c 5455
23f86fce 5456 face = xmalloc (sizeof *face);
af53b43c
KH
5457 *face = *base_face;
5458 face->gc = 0;
bdd10de6 5459 face->extra = NULL;
2dee4c0b
KH
5460 face->overstrike
5461 = (! NILP (font_object)
5462 && FONT_WEIGHT_NAME_NUMERIC (face->lface[LFACE_WEIGHT_INDEX]) > 100
5463 && FONT_WEIGHT_NUMERIC (font_object) <= 100);
af53b43c
KH
5464
5465 /* Don't try to free the colors copied bitwise from BASE_FACE. */
5466 face->colors_copied_bitwise_p = 1;
2dee4c0b 5467 face->font = NILP (font_object) ? NULL : XFONT_OBJECT (font_object);
af53b43c
KH
5468 face->gc = 0;
5469
5470 cache_face (cache, face, face->hash);
5471
82641697
GM
5472 return face;
5473}
8c6204de 5474#endif /* HAVE_WINDOW_SYSTEM */
82641697
GM
5475
5476
5477/* Realize the fully-specified face with attributes ATTRS in face
af53b43c
KH
5478 cache CACHE for ASCII characters. Do it for X frame CACHE->f. If
5479 the new face doesn't share font with the default face, a fontname
5480 is allocated from the heap and set in `font_name' of the new face,
5481 but it is not yet loaded here. Value is a pointer to the newly
5482 created realized face. */
82641697
GM
5483
5484static struct face *
4973679b 5485realize_x_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE])
82641697 5486{
7d603e3f 5487 struct face *face = NULL;
c3cee013 5488#ifdef HAVE_WINDOW_SYSTEM
7d603e3f 5489 struct face *default_face;
78d2079c 5490 struct frame *f;
9b0e3eba 5491 Lisp_Object stipple, underline, overline, strike_through, box;
82641697 5492
a54e2c05 5493 eassert (FRAME_WINDOW_P (cache->f));
82641697
GM
5494
5495 /* Allocate a new realized face. */
39506348 5496 face = make_realized_face (attrs);
af53b43c 5497 face->ascii_face = face;
39506348
KH
5498
5499 f = cache->f;
5500
82641697
GM
5501 /* Determine the font to use. Most of the time, the font will be
5502 the same as the font of the default face, so try that first. */
5503 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5504 if (default_face
82641697
GM
5505 && lface_same_font_attributes_p (default_face->lface, attrs))
5506 {
5507 face->font = default_face->font;
76f54ecc
KH
5508 face->fontset
5509 = make_fontset_for_ascii_face (f, default_face->fontset, face);
82641697
GM
5510 }
5511 else
5512 {
39506348 5513 /* If the face attribute ATTRS specifies a fontset, use it as
fc8c4797
KH
5514 the base of a new realized fontset. Otherwise, use the same
5515 base fontset as of the default face. The base determines
5516 registry and encoding of a font. It may also determine
5517 foundry and family. The other fields of font name pattern
5518 are constructed from ATTRS. */
5519 int fontset = face_fontset (attrs);
5520
af53b43c
KH
5521 /* If we are realizing the default face, ATTRS should specify a
5522 fontset. In other words, if FONTSET is -1, we are not
5523 realizing the default face, thus the default face should have
5524 already been realized. */
5525 if (fontset == -1)
d78494f9
CY
5526 {
5527 if (default_face)
5528 fontset = default_face->fontset;
5529 if (fontset == -1)
1088b922 5530 emacs_abort ();
d78494f9 5531 }
2dee4c0b
KH
5532 if (! FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5533 attrs[LFACE_FONT_INDEX]
5534 = font_load_for_lface (f, attrs, attrs[LFACE_FONT_INDEX]);
5535 if (FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5536 {
5537 face->font = XFONT_OBJECT (attrs[LFACE_FONT_INDEX]);
5538 face->fontset = make_fontset_for_ascii_face (f, fontset, face);
5539 }
426b2119 5540 else
2dee4c0b
KH
5541 {
5542 face->font = NULL;
5543 face->fontset = -1;
5544 }
82641697
GM
5545 }
5546
2dee4c0b
KH
5547 if (face->font
5548 && FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]) > 100
5549 && FONT_WEIGHT_NUMERIC (attrs[LFACE_FONT_INDEX]) <= 100)
5550 face->overstrike = 1;
5551
82641697 5552 /* Load colors, and set remaining attributes. */
178c5d9c 5553
82641697 5554 load_face_colors (f, face, attrs);
660ed669 5555
82641697
GM
5556 /* Set up box. */
5557 box = attrs[LFACE_BOX_INDEX];
5558 if (STRINGP (box))
cb637678 5559 {
82641697
GM
5560 /* A simple box of line width 1 drawn in color given by
5561 the string. */
5562 face->box_color = load_color (f, face, attrs[LFACE_BOX_INDEX],
5563 LFACE_BOX_INDEX);
5564 face->box = FACE_SIMPLE_BOX;
5565 face->box_line_width = 1;
cb637678 5566 }
82641697 5567 else if (INTEGERP (box))
42120bc7 5568 {
82641697 5569 /* Simple box of specified line width in foreground color of the
b5f03016 5570 face. */
a54e2c05 5571 eassert (XINT (box) != 0);
82641697 5572 face->box = FACE_SIMPLE_BOX;
89624b8b 5573 face->box_line_width = XINT (box);
82641697
GM
5574 face->box_color = face->foreground;
5575 face->box_color_defaulted_p = 1;
5576 }
5577 else if (CONSP (box))
5578 {
5579 /* `(:width WIDTH :color COLOR :shadow SHADOW)'. SHADOW
5580 being one of `raised' or `sunken'. */
5581 face->box = FACE_SIMPLE_BOX;
5582 face->box_color = face->foreground;
5583 face->box_color_defaulted_p = 1;
5584 face->box_line_width = 1;
5585
5586 while (CONSP (box))
42120bc7 5587 {
82641697
GM
5588 Lisp_Object keyword, value;
5589
5590 keyword = XCAR (box);
5591 box = XCDR (box);
5592
5593 if (!CONSP (box))
5594 break;
5595 value = XCAR (box);
5596 box = XCDR (box);
5597
5598 if (EQ (keyword, QCline_width))
5599 {
89624b8b
KH
5600 if (INTEGERP (value) && XINT (value) != 0)
5601 face->box_line_width = XINT (value);
82641697
GM
5602 }
5603 else if (EQ (keyword, QCcolor))
5604 {
5605 if (STRINGP (value))
5606 {
5607 face->box_color = load_color (f, face, value,
5608 LFACE_BOX_INDEX);
5609 face->use_box_color_for_shadows_p = 1;
5610 }
5611 }
5612 else if (EQ (keyword, QCstyle))
a8517066 5613 {
82641697
GM
5614 if (EQ (value, Qreleased_button))
5615 face->box = FACE_RAISED_BOX;
5616 else if (EQ (value, Qpressed_button))
5617 face->box = FACE_SUNKEN_BOX;
a8517066 5618 }
42120bc7
RS
5619 }
5620 }
195f798e 5621
82641697 5622 /* Text underline, overline, strike-through. */
178c5d9c 5623
9b0e3eba
AA
5624 underline = attrs[LFACE_UNDERLINE_INDEX];
5625 if (EQ (underline, Qt))
178c5d9c 5626 {
82641697
GM
5627 /* Use default color (same as foreground color). */
5628 face->underline_p = 1;
9b0e3eba 5629 face->underline_type = FACE_UNDER_LINE;
82641697
GM
5630 face->underline_defaulted_p = 1;
5631 face->underline_color = 0;
5632 }
9b0e3eba 5633 else if (STRINGP (underline))
195f798e 5634 {
82641697
GM
5635 /* Use specified color. */
5636 face->underline_p = 1;
9b0e3eba 5637 face->underline_type = FACE_UNDER_LINE;
82641697
GM
5638 face->underline_defaulted_p = 0;
5639 face->underline_color
9b0e3eba 5640 = load_color (f, face, underline,
82641697 5641 LFACE_UNDERLINE_INDEX);
195f798e 5642 }
9b0e3eba 5643 else if (NILP (underline))
7b00de84 5644 {
82641697
GM
5645 face->underline_p = 0;
5646 face->underline_defaulted_p = 0;
5647 face->underline_color = 0;
7b00de84 5648 }
9b0e3eba
AA
5649 else if (CONSP (underline))
5650 {
38182d90 5651 /* `(:color COLOR :style STYLE)'.
9b0e3eba
AA
5652 STYLE being one of `line' or `wave'. */
5653 face->underline_p = 1;
5654 face->underline_color = 0;
5655 face->underline_defaulted_p = 1;
5656 face->underline_type = FACE_UNDER_LINE;
5657
bde3c6c0
GM
5658 /* FIXME? This is also not robust about checking the precise form.
5659 See comments in Finternal_set_lisp_face_attribute. */
9b0e3eba
AA
5660 while (CONSP (underline))
5661 {
5662 Lisp_Object keyword, value;
5663
5664 keyword = XCAR (underline);
5665 underline = XCDR (underline);
5666
5667 if (!CONSP (underline))
5668 break;
5669 value = XCAR (underline);
5670 underline = XCDR (underline);
5671
5672 if (EQ (keyword, QCcolor))
5673 {
5674 if (EQ (value, Qforeground_color))
5675 {
5676 face->underline_defaulted_p = 1;
5677 face->underline_color = 0;
5678 }
5679 else if (STRINGP (value))
5680 {
5681 face->underline_defaulted_p = 0;
5682 face->underline_color = load_color (f, face, value,
5683 LFACE_UNDERLINE_INDEX);
5684 }
5685 }
5686 else if (EQ (keyword, QCstyle))
5687 {
5688 if (EQ (value, Qline))
5689 face->underline_type = FACE_UNDER_LINE;
5690 else if (EQ (value, Qwave))
5691 face->underline_type = FACE_UNDER_WAVE;
5692 }
5693 }
5694 }
38182d90 5695
82641697
GM
5696 overline = attrs[LFACE_OVERLINE_INDEX];
5697 if (STRINGP (overline))
cb637678 5698 {
82641697
GM
5699 face->overline_color
5700 = load_color (f, face, attrs[LFACE_OVERLINE_INDEX],
5701 LFACE_OVERLINE_INDEX);
5702 face->overline_p = 1;
cb637678 5703 }
82641697 5704 else if (EQ (overline, Qt))
cb637678 5705 {
82641697
GM
5706 face->overline_color = face->foreground;
5707 face->overline_color_defaulted_p = 1;
5708 face->overline_p = 1;
cb637678
JB
5709 }
5710
82641697
GM
5711 strike_through = attrs[LFACE_STRIKE_THROUGH_INDEX];
5712 if (STRINGP (strike_through))
5713 {
5714 face->strike_through_color
5715 = load_color (f, face, attrs[LFACE_STRIKE_THROUGH_INDEX],
5716 LFACE_STRIKE_THROUGH_INDEX);
5717 face->strike_through_p = 1;
5718 }
5719 else if (EQ (strike_through, Qt))
5720 {
5721 face->strike_through_color = face->foreground;
5722 face->strike_through_color_defaulted_p = 1;
5723 face->strike_through_p = 1;
5724 }
867dd159 5725
82641697
GM
5726 stipple = attrs[LFACE_STIPPLE_INDEX];
5727 if (!NILP (stipple))
5728 face->stipple = load_pixmap (f, stipple, &face->pixmap_w, &face->pixmap_h);
c3cee013 5729#endif /* HAVE_WINDOW_SYSTEM */
660ed669 5730
82641697 5731 return face;
660ed669
JB
5732}
5733
729425b1 5734
ae4b4ba5
GM
5735/* Map a specified color of face FACE on frame F to a tty color index.
5736 IDX is either LFACE_FOREGROUND_INDEX or LFACE_BACKGROUND_INDEX, and
5737 specifies which color to map. Set *DEFAULTED to 1 if mapping to the
5738 default foreground/background colors. */
5739
5740static void
b5f03016
AS
5741map_tty_color (struct frame *f, struct face *face,
5742 enum lface_attribute_index idx, int *defaulted)
ae4b4ba5
GM
5743{
5744 Lisp_Object frame, color, def;
5745 int foreground_p = idx == LFACE_FOREGROUND_INDEX;
a5a62657
PE
5746 unsigned long default_pixel =
5747 foreground_p ? FACE_TTY_DEFAULT_FG_COLOR : FACE_TTY_DEFAULT_BG_COLOR;
5748 unsigned long pixel = default_pixel;
5749#ifdef MSDOS
5750 unsigned long default_other_pixel =
5751 foreground_p ? FACE_TTY_DEFAULT_BG_COLOR : FACE_TTY_DEFAULT_FG_COLOR;
5752#endif
ae4b4ba5 5753
a54e2c05 5754 eassert (idx == LFACE_FOREGROUND_INDEX || idx == LFACE_BACKGROUND_INDEX);
ae4b4ba5 5755
ae4b4ba5
GM
5756 XSETFRAME (frame, f);
5757 color = face->lface[idx];
177c0ea7 5758
ae4b4ba5 5759 if (STRINGP (color)
d5db4077 5760 && SCHARS (color)
ae4b4ba5
GM
5761 && CONSP (Vtty_defined_color_alist)
5762 && (def = assq_no_quit (color, call1 (Qtty_color_alist, frame)),
5763 CONSP (def)))
5764 {
5765 /* Associations in tty-defined-color-alist are of the form
5766 (NAME INDEX R G B). We need the INDEX part. */
5767 pixel = XINT (XCAR (XCDR (def)));
5768 }
5769
5770 if (pixel == default_pixel && STRINGP (color))
5771 {
5772 pixel = load_color (f, face, color, idx);
5773
8d05ec51 5774#ifdef MSDOS
ae4b4ba5
GM
5775 /* If the foreground of the default face is the default color,
5776 use the foreground color defined by the frame. */
ae4b4ba5
GM
5777 if (FRAME_MSDOS_P (f))
5778 {
ae4b4ba5
GM
5779 if (pixel == default_pixel
5780 || pixel == FACE_TTY_DEFAULT_COLOR)
5781 {
5782 if (foreground_p)
5783 pixel = FRAME_FOREGROUND_PIXEL (f);
5784 else
5785 pixel = FRAME_BACKGROUND_PIXEL (f);
5786 face->lface[idx] = tty_color_name (f, pixel);
5787 *defaulted = 1;
5788 }
5789 else if (pixel == default_other_pixel)
5790 {
5791 if (foreground_p)
5792 pixel = FRAME_BACKGROUND_PIXEL (f);
5793 else
5794 pixel = FRAME_FOREGROUND_PIXEL (f);
5795 face->lface[idx] = tty_color_name (f, pixel);
5796 *defaulted = 1;
5797 }
b5f03016 5798 }
8d05ec51 5799#endif /* MSDOS */
ae4b4ba5
GM
5800 }
5801
5802 if (foreground_p)
5803 face->foreground = pixel;
5804 else
5805 face->background = pixel;
5806}
5807
5808
82641697 5809/* Realize the fully-specified face with attributes ATTRS in face
af53b43c
KH
5810 cache CACHE for ASCII characters. Do it for TTY frame CACHE->f.
5811 Value is a pointer to the newly created realized face. */
a8517066 5812
82641697 5813static struct face *
4973679b
PE
5814realize_tty_face (struct face_cache *cache,
5815 Lisp_Object attrs[LFACE_VECTOR_SIZE])
82641697
GM
5816{
5817 struct face *face;
5818 int weight, slant;
2d764c78 5819 int face_colors_defaulted = 0;
ae4b4ba5 5820 struct frame *f = cache->f;
729425b1 5821
82641697 5822 /* Frame must be a termcap frame. */
a54e2c05 5823 eassert (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f));
178c5d9c 5824
82641697 5825 /* Allocate a new realized face. */
39506348 5826 face = make_realized_face (attrs);
2dee4c0b 5827#if 0
e689ec06 5828 face->font_name = FRAME_MSDOS_P (cache->f) ? "ms-dos" : "tty";
2dee4c0b 5829#endif
82641697 5830
cd4eb164 5831 /* Map face attributes to TTY appearances. */
2dee4c0b
KH
5832 weight = FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]);
5833 slant = FONT_SLANT_NAME_NUMERIC (attrs[LFACE_SLANT_INDEX]);
5834 if (weight > 100)
82641697 5835 face->tty_bold_p = 1;
cd4eb164
CY
5836 if (slant != 100)
5837 face->tty_italic_p = 1;
82641697
GM
5838 if (!NILP (attrs[LFACE_UNDERLINE_INDEX]))
5839 face->tty_underline_p = 1;
5840 if (!NILP (attrs[LFACE_INVERSE_INDEX]))
5841 face->tty_reverse_p = 1;
5842
5843 /* Map color names to color indices. */
ae4b4ba5
GM
5844 map_tty_color (f, face, LFACE_FOREGROUND_INDEX, &face_colors_defaulted);
5845 map_tty_color (f, face, LFACE_BACKGROUND_INDEX, &face_colors_defaulted);
177c0ea7 5846
2d764c78
EZ
5847 /* Swap colors if face is inverse-video. If the colors are taken
5848 from the frame colors, they are already inverted, since the
5849 frame-creation function calls x-handle-reverse-video. */
5850 if (face->tty_reverse_p && !face_colors_defaulted)
44747bd0
EZ
5851 {
5852 unsigned long tem = face->foreground;
44747bd0
EZ
5853 face->foreground = face->background;
5854 face->background = tem;
5855 }
44747bd0 5856
a4a76b61
GM
5857 if (tty_suppress_bold_inverse_default_colors_p
5858 && face->tty_bold_p
5859 && face->background == FACE_TTY_DEFAULT_FG_COLOR
5860 && face->foreground == FACE_TTY_DEFAULT_BG_COLOR)
5861 face->tty_bold_p = 0;
5862
82641697 5863 return face;
729425b1 5864}
867dd159 5865
82641697 5866
a4a76b61
GM
5867DEFUN ("tty-suppress-bold-inverse-default-colors",
5868 Ftty_suppress_bold_inverse_default_colors,
5869 Stty_suppress_bold_inverse_default_colors, 1, 1, 0,
e3cd9e7f 5870 doc: /* Suppress/allow boldness of faces with inverse default colors.
228299fa
GM
5871SUPPRESS non-nil means suppress it.
5872This affects bold faces on TTYs whose foreground is the default background
5873color of the display and whose background is the default foreground color.
5874For such faces, the bold face attribute is ignored if this variable
7ee72033 5875is non-nil. */)
5842a27b 5876 (Lisp_Object suppress)
a4a76b61
GM
5877{
5878 tty_suppress_bold_inverse_default_colors_p = !NILP (suppress);
5879 ++face_change_count;
5880 return suppress;
5881}
5882
5883
82641697
GM
5884\f
5885/***********************************************************************
5886 Computing Faces
5887 ***********************************************************************/
5888
5889/* Return the ID of the face to use to display character CH with face
5890 property PROP on frame F in current_buffer. */
2e16580f
RS
5891
5892int
971de7fb 5893compute_char_face (struct frame *f, int ch, Lisp_Object prop)
2e16580f 5894{
82641697 5895 int face_id;
39506348 5896
4b4deea2 5897 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
522d42f7 5898 ch = 0;
178c5d9c 5899
82641697 5900 if (NILP (prop))
39506348
KH
5901 {
5902 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
779c6fb6 5903 face_id = FACE_FOR_CHAR (f, face, ch, -1, Qnil);
39506348 5904 }
82641697 5905 else
2e16580f 5906 {
82641697
GM
5907 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5908 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
72af86bd 5909 memcpy (attrs, default_face->lface, sizeof attrs);
a0a23346 5910 merge_face_ref (f, prop, attrs, 1, 0);
af53b43c 5911 face_id = lookup_face (f, attrs);
2e16580f
RS
5912 }
5913
82641697 5914 return face_id;
2e16580f 5915}
bc0db68d 5916
82641697
GM
5917/* Return the face ID associated with buffer position POS for
5918 displaying ASCII characters. Return in *ENDPTR the position at
5919 which a different face is needed, as far as text properties and
5920 overlays are concerned. W is a window displaying current_buffer.
5921
5922 REGION_BEG, REGION_END delimit the region, so it can be
5923 highlighted.
6f134486 5924
82641697
GM
5925 LIMIT is a position not to scan beyond. That is to limit the time
5926 this function can take.
5927
5928 If MOUSE is non-zero, use the character's mouse-face, not its face.
5929
6970f632
CY
5930 BASE_FACE_ID, if non-negative, specifies a base face id to use
5931 instead of DEFAULT_FACE_ID.
5932
39506348 5933 The face returned is suitable for displaying ASCII characters. */
bc0db68d 5934
cb637678 5935int
d311d28c
PE
5936face_at_buffer_position (struct window *w, ptrdiff_t pos,
5937 ptrdiff_t region_beg, ptrdiff_t region_end,
5938 ptrdiff_t *endptr, ptrdiff_t limit,
d5a3eaaf 5939 int mouse, int base_face_id)
7b7739b1 5940{
d3d50620 5941 struct frame *f = XFRAME (w->frame);
82641697 5942 Lisp_Object attrs[LFACE_VECTOR_SIZE];
b6d40e46 5943 Lisp_Object prop, position;
b081724f 5944 ptrdiff_t i, noverlays;
7b7739b1 5945 Lisp_Object *overlay_vec;
d311d28c 5946 ptrdiff_t endpos;
82641697
GM
5947 Lisp_Object propname = mouse ? Qmouse_face : Qface;
5948 Lisp_Object limit1, end;
5949 struct face *default_face;
f6b98e0b
JB
5950
5951 /* W must display the current buffer. We could write this function
5952 to use the frame and buffer of W, but right now it doesn't. */
e74aeda8 5953 /* eassert (XBUFFER (w->contents) == current_buffer); */
f211082d 5954
82641697 5955 XSETFASTINT (position, pos);
7b7739b1 5956
f6b98e0b 5957 endpos = ZV;
bc0db68d
RS
5958 if (pos < region_beg && region_beg < endpos)
5959 endpos = region_beg;
f6b98e0b 5960
82641697
GM
5961 /* Get the `face' or `mouse_face' text property at POS, and
5962 determine the next position at which the property changes. */
e74aeda8 5963 prop = Fget_text_property (position, propname, w->contents);
82641697 5964 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
e74aeda8 5965 end = Fnext_single_property_change (position, propname, w->contents, limit1);
82641697
GM
5966 if (INTEGERP (end))
5967 endpos = XINT (end);
6f134486 5968
82641697 5969 /* Look at properties from overlays. */
b6d40e46 5970 {
d311d28c 5971 ptrdiff_t next_overlay;
b6d40e46 5972
0bc90bba 5973 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, &next_overlay, 0);
f6b98e0b
JB
5974 if (next_overlay < endpos)
5975 endpos = next_overlay;
b6d40e46
JB
5976 }
5977
5978 *endptr = endpos;
7b7739b1 5979
823564e5
EZ
5980 {
5981 int face_id;
5982
5983 if (base_face_id >= 0)
5984 face_id = base_face_id;
5985 else if (NILP (Vface_remapping_alist))
5986 face_id = DEFAULT_FACE_ID;
5987 else
5988 face_id = lookup_basic_face (f, DEFAULT_FACE_ID);
5989
5990 default_face = FACE_FROM_ID (f, face_id);
5991 }
178c5d9c 5992
82641697
GM
5993 /* Optimize common cases where we can use the default face. */
5994 if (noverlays == 0
5995 && NILP (prop)
39506348 5996 && !(pos >= region_beg && pos < region_end))
f2cec7a9 5997 return default_face->id;
82641697
GM
5998
5999 /* Begin with attributes from the default face. */
72af86bd 6000 memcpy (attrs, default_face->lface, sizeof attrs);
82641697
GM
6001
6002 /* Merge in attributes specified via text properties. */
6003 if (!NILP (prop))
a0a23346 6004 merge_face_ref (f, prop, attrs, 1, 0);
82641697
GM
6005
6006 /* Now merge the overlay data. */
18195655 6007 noverlays = sort_overlays (overlay_vec, noverlays, w);
18195655 6008 for (i = 0; i < noverlays; i++)
4699e6d2 6009 {
18195655 6010 Lisp_Object oend;
56adbe62 6011 ptrdiff_t oendpos;
18195655
RS
6012
6013 prop = Foverlay_get (overlay_vec[i], propname);
82641697 6014 if (!NILP (prop))
a0a23346 6015 merge_face_ref (f, prop, attrs, 1, 0);
18195655
RS
6016
6017 oend = OVERLAY_END (overlay_vec[i]);
6018 oendpos = OVERLAY_POSITION (oend);
6019 if (oendpos < endpos)
6020 endpos = oendpos;
6021 }
6022
82641697 6023 /* If in the region, merge in the region face. */
18195655
RS
6024 if (pos >= region_beg && pos < region_end)
6025 {
a0a23346 6026 merge_named_face (f, Qregion, attrs, 0);
178c5d9c 6027
18195655
RS
6028 if (region_end < endpos)
6029 endpos = region_end;
18195655
RS
6030 }
6031
6032 *endptr = endpos;
6033
82641697 6034 /* Look up a realized face with the given face attributes,
39506348 6035 or realize a new one for ASCII characters. */
af53b43c 6036 return lookup_face (f, attrs);
18195655
RS
6037}
6038
a193ecf1
RS
6039/* Return the face ID at buffer position POS for displaying ASCII
6040 characters associated with overlay strings for overlay OVERLAY.
6041
6042 Like face_at_buffer_position except for OVERLAY. Currently it
6043 simply disregards the `face' properties of all overlays. */
03e1d617
RS
6044
6045int
d311d28c
PE
6046face_for_overlay_string (struct window *w, ptrdiff_t pos,
6047 ptrdiff_t region_beg, ptrdiff_t region_end,
6048 ptrdiff_t *endptr, ptrdiff_t limit,
d5a3eaaf 6049 int mouse, Lisp_Object overlay)
03e1d617 6050{
d3d50620 6051 struct frame *f = XFRAME (w->frame);
03e1d617
RS
6052 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6053 Lisp_Object prop, position;
56adbe62 6054 ptrdiff_t endpos;
03e1d617
RS
6055 Lisp_Object propname = mouse ? Qmouse_face : Qface;
6056 Lisp_Object limit1, end;
6057 struct face *default_face;
6058
6059 /* W must display the current buffer. We could write this function
6060 to use the frame and buffer of W, but right now it doesn't. */
e74aeda8 6061 /* eassert (XBUFFER (w->contents) == current_buffer); */
03e1d617 6062
03e1d617
RS
6063 XSETFASTINT (position, pos);
6064
6065 endpos = ZV;
6066 if (pos < region_beg && region_beg < endpos)
6067 endpos = region_beg;
6068
6069 /* Get the `face' or `mouse_face' text property at POS, and
6070 determine the next position at which the property changes. */
e74aeda8 6071 prop = Fget_text_property (position, propname, w->contents);
03e1d617 6072 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
e74aeda8 6073 end = Fnext_single_property_change (position, propname, w->contents, limit1);
03e1d617
RS
6074 if (INTEGERP (end))
6075 endpos = XINT (end);
6076
6077 *endptr = endpos;
6078
1682701f 6079 /* Optimize common case where we can use the default face. */
03e1d617 6080 if (NILP (prop)
1682701f
CY
6081 && !(pos >= region_beg && pos < region_end)
6082 && NILP (Vface_remapping_alist))
03e1d617
RS
6083 return DEFAULT_FACE_ID;
6084
6085 /* Begin with attributes from the default face. */
1682701f 6086 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
72af86bd 6087 memcpy (attrs, default_face->lface, sizeof attrs);
03e1d617
RS
6088
6089 /* Merge in attributes specified via text properties. */
6090 if (!NILP (prop))
6091 merge_face_ref (f, prop, attrs, 1, 0);
6092
6093 /* If in the region, merge in the region face. */
6094 if (pos >= region_beg && pos < region_end)
6095 {
6096 merge_named_face (f, Qregion, attrs, 0);
6097
6098 if (region_end < endpos)
6099 endpos = region_end;
6100 }
6101
6102 *endptr = endpos;
6103
6104 /* Look up a realized face with the given face attributes,
6105 or realize a new one for ASCII characters. */
ce9c2e7b 6106 return lookup_face (f, attrs);
03e1d617
RS
6107}
6108
60573a90 6109
82641697 6110/* Compute the face at character position POS in Lisp string STRING on
39506348 6111 window W, for ASCII characters.
7b7739b1 6112
82641697
GM
6113 If STRING is an overlay string, it comes from position BUFPOS in
6114 current_buffer, otherwise BUFPOS is zero to indicate that STRING is
6115 not an overlay string. W must display the current buffer.
6116 REGION_BEG and REGION_END give the start and end positions of the
8714a182
GM
6117 region; both are -1 if no region is visible.
6118
6119 BASE_FACE_ID is the id of a face to merge with. For strings coming
6120 from overlays or the `display' property it is the face at BUFPOS.
178c5d9c 6121
48a4ca99
GM
6122 If MOUSE_P is non-zero, use the character's mouse-face, not its face.
6123
82641697
GM
6124 Set *ENDPTR to the next position where to check for faces in
6125 STRING; -1 if the face is constant from POS to the end of the
6126 string.
18195655 6127
82641697 6128 Value is the id of the face to use. The face returned is suitable
39506348 6129 for displaying ASCII characters. */
fffc2367 6130
82641697 6131int
d5a3eaaf 6132face_at_string_position (struct window *w, Lisp_Object string,
d311d28c
PE
6133 ptrdiff_t pos, ptrdiff_t bufpos,
6134 ptrdiff_t region_beg, ptrdiff_t region_end,
6135 ptrdiff_t *endptr, enum face_id base_face_id,
d5a3eaaf 6136 int mouse_p)
660ed669 6137{
82641697
GM
6138 Lisp_Object prop, position, end, limit;
6139 struct frame *f = XFRAME (WINDOW_FRAME (w));
6140 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6141 struct face *base_face;
0063fdb1 6142 bool multibyte_p = STRING_MULTIBYTE (string);
48a4ca99 6143 Lisp_Object prop_name = mouse_p ? Qmouse_face : Qface;
82641697
GM
6144
6145 /* Get the value of the face property at the current position within
6146 STRING. Value is nil if there is no face property. */
6147 XSETFASTINT (position, pos);
48a4ca99 6148 prop = Fget_text_property (position, prop_name, string);
82641697
GM
6149
6150 /* Get the next position at which to check for faces. Value of end
6151 is nil if face is constant all the way to the end of the string.
6152 Otherwise it is a string position where to check faces next.
6153 Limit is the maximum position up to which to check for property
6154 changes in Fnext_single_property_change. Strings are usually
6155 short, so set the limit to the end of the string. */
d5db4077 6156 XSETFASTINT (limit, SCHARS (string));
48a4ca99 6157 end = Fnext_single_property_change (position, prop_name, string, limit);
82641697
GM
6158 if (INTEGERP (end))
6159 *endptr = XFASTINT (end);
6160 else
6161 *endptr = -1;
6162
6163 base_face = FACE_FROM_ID (f, base_face_id);
a54e2c05 6164 eassert (base_face);
82641697
GM
6165
6166 /* Optimize the default case that there is no face property and we
6167 are not in the region. */
6168 if (NILP (prop)
6169 && (base_face_id != DEFAULT_FACE_ID
6170 /* BUFPOS <= 0 means STRING is not an overlay string, so
6171 that the region doesn't have to be taken into account. */
6172 || bufpos <= 0
6173 || bufpos < region_beg
6174 || bufpos >= region_end)
6175 && (multibyte_p
6176 /* We can't realize faces for different charsets differently
6177 if we don't have fonts, so we can stop here if not working
6178 on a window-system frame. */
6179 || !FRAME_WINDOW_P (f)
239f9db9 6180 || FACE_SUITABLE_FOR_ASCII_CHAR_P (base_face, 0)))
82641697
GM
6181 return base_face->id;
6182
6183 /* Begin with attributes from the base face. */
72af86bd 6184 memcpy (attrs, base_face->lface, sizeof attrs);
82641697
GM
6185
6186 /* Merge in attributes specified via text properties. */
6187 if (!NILP (prop))
a0a23346 6188 merge_face_ref (f, prop, attrs, 1, 0);
82641697
GM
6189
6190 /* If in the region, merge in the region face. */
6191 if (bufpos
6192 && bufpos >= region_beg
6193 && bufpos < region_end)
a0a23346 6194 merge_named_face (f, Qregion, attrs, 0);
660ed669 6195
82641697 6196 /* Look up a realized face with the given face attributes,
39506348 6197 or realize a new one for ASCII characters. */
af53b43c 6198 return lookup_face (f, attrs);
660ed669
JB
6199}
6200
6201
fd998c7f
KS
6202/* Merge a face into a realized face.
6203
6204 F is frame where faces are (to be) realized.
6205
dc91a0ed
KS
6206 FACE_NAME is named face to merge.
6207
6208 If FACE_NAME is nil, FACE_ID is face_id of realized face to merge.
6209
6210 If FACE_NAME is t, FACE_ID is lface_id of face to merge.
fd998c7f
KS
6211
6212 BASE_FACE_ID is realized face to merge into.
6213
dc91a0ed 6214 Return new face id.
fd998c7f
KS
6215*/
6216
6217int
d311d28c 6218merge_faces (struct frame *f, Lisp_Object face_name, int face_id,
9910e595 6219 int base_face_id)
fd998c7f
KS
6220{
6221 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6222 struct face *base_face;
6223
6224 base_face = FACE_FROM_ID (f, base_face_id);
6225 if (!base_face)
6226 return base_face_id;
6227
dc91a0ed
KS
6228 if (EQ (face_name, Qt))
6229 {
6230 if (face_id < 0 || face_id >= lface_id_to_name_size)
6231 return base_face_id;
6232 face_name = lface_id_to_name[face_id];
d8453278
CY
6233 /* When called during make-frame, lookup_derived_face may fail
6234 if the faces are uninitialized. Don't signal an error. */
6235 face_id = lookup_derived_face (f, face_name, base_face_id, 0);
6236 return (face_id >= 0 ? face_id : base_face_id);
dc91a0ed
KS
6237 }
6238
fd998c7f 6239 /* Begin with attributes from the base face. */
72af86bd 6240 memcpy (attrs, base_face->lface, sizeof attrs);
fd998c7f
KS
6241
6242 if (!NILP (face_name))
6243 {
6244 if (!merge_named_face (f, face_name, attrs, 0))
6245 return base_face_id;
6246 }
6247 else
6248 {
6249 struct face *face;
dc91a0ed
KS
6250 if (face_id < 0)
6251 return base_face_id;
fd998c7f
KS
6252 face = FACE_FROM_ID (f, face_id);
6253 if (!face)
6254 return base_face_id;
6255 merge_face_vectors (f, face->lface, attrs, 0);
6256 }
6257
6258 /* Look up a realized face with the given face attributes,
6259 or realize a new one for ASCII characters. */
0e3ae538 6260 return lookup_face (f, attrs);
fd998c7f
KS
6261}
6262
c115973b 6263\f
7ded3383
AR
6264
6265#ifndef HAVE_X_WINDOWS
6266DEFUN ("x-load-color-file", Fx_load_color_file,
6267 Sx_load_color_file, 1, 1, 0,
6268 doc: /* Create an alist of color entries from an external file.
6269
6270The file should define one named RGB color per line like so:
6271 R G B name
6272where R,G,B are numbers between 0 and 255 and name is an arbitrary string. */)
5842a27b 6273 (Lisp_Object filename)
7ded3383
AR
6274{
6275 FILE *fp;
6276 Lisp_Object cmap = Qnil;
6277 Lisp_Object abspath;
6278
6279 CHECK_STRING (filename);
6280 abspath = Fexpand_file_name (filename, Qnil);
6281
3f5bef16 6282 block_input ();
406af475 6283 fp = emacs_fopen (SSDATA (abspath), "rt");
7ded3383
AR
6284 if (fp)
6285 {
6286 char buf[512];
6287 int red, green, blue;
6288 int num;
6289
7ded3383
AR
6290 while (fgets (buf, sizeof (buf), fp) != NULL) {
6291 if (sscanf (buf, "%u %u %u %n", &red, &green, &blue, &num) == 3)
6292 {
0fda9b75 6293#ifdef HAVE_NTGUI
3f5bef16 6294 int color = RGB (red, green, blue);
ebadbfa6 6295#else
3f5bef16 6296 int color = (red << 16) | (green << 8) | blue;
ebadbfa6 6297#endif
3f5bef16
PE
6298 char *name = buf + num;
6299 ptrdiff_t len = strlen (name);
6300 len -= 0 < len && name[len - 1] == '\n';
6301 cmap = Fcons (Fcons (make_string (name, len), make_number (color)),
7ded3383
AR
6302 cmap);
6303 }
6304 }
6305 fclose (fp);
7ded3383 6306 }
3f5bef16 6307 unblock_input ();
7ded3383
AR
6308 return cmap;
6309}
6310#endif
6311
6312\f
82641697
GM
6313/***********************************************************************
6314 Tests
6315 ***********************************************************************/
c115973b 6316
e509cfa6 6317#ifdef GLYPH_DEBUG
c115973b 6318
82641697 6319/* Print the contents of the realized face FACE to stderr. */
c115973b 6320
82641697 6321static void
7d7d0045 6322dump_realized_face (struct face *face)
82641697
GM
6323{
6324 fprintf (stderr, "ID: %d\n", face->id);
6325#ifdef HAVE_X_WINDOWS
2defe37f 6326 fprintf (stderr, "gc: %ld\n", (long) face->gc);
82641697
GM
6327#endif
6328 fprintf (stderr, "foreground: 0x%lx (%s)\n",
6329 face->foreground,
d5db4077 6330 SDATA (face->lface[LFACE_FOREGROUND_INDEX]));
82641697
GM
6331 fprintf (stderr, "background: 0x%lx (%s)\n",
6332 face->background,
d5db4077 6333 SDATA (face->lface[LFACE_BACKGROUND_INDEX]));
2dee4c0b
KH
6334 if (face->font)
6335 fprintf (stderr, "font_name: %s (%s)\n",
6336 SDATA (face->font->props[FONT_NAME_INDEX]),
6337 SDATA (face->lface[LFACE_FAMILY_INDEX]));
82641697
GM
6338#ifdef HAVE_X_WINDOWS
6339 fprintf (stderr, "font = %p\n", face->font);
6340#endif
82641697
GM
6341 fprintf (stderr, "fontset: %d\n", face->fontset);
6342 fprintf (stderr, "underline: %d (%s)\n",
6343 face->underline_p,
d5db4077 6344 SDATA (Fsymbol_name (face->lface[LFACE_UNDERLINE_INDEX])));
82641697 6345 fprintf (stderr, "hash: %d\n", face->hash);
c115973b
JB
6346}
6347
6348
a7ca3326 6349DEFUN ("dump-face", Fdump_face, Sdump_face, 0, 1, 0, doc: /* */)
5842a27b 6350 (Lisp_Object n)
c115973b 6351{
82641697 6352 if (NILP (n))
c115973b 6353 {
82641697 6354 int i;
178c5d9c 6355
82641697
GM
6356 fprintf (stderr, "font selection order: ");
6357 for (i = 0; i < DIM (font_sort_order); ++i)
6358 fprintf (stderr, "%d ", font_sort_order[i]);
6359 fprintf (stderr, "\n");
6360
6361 fprintf (stderr, "alternative fonts: ");
6362 debug_print (Vface_alternative_font_family_alist);
6363 fprintf (stderr, "\n");
178c5d9c 6364
c0617987 6365 for (i = 0; i < FRAME_FACE_CACHE (SELECTED_FRAME ())->used; ++i)
82641697 6366 Fdump_face (make_number (i));
c115973b
JB
6367 }
6368 else
f5e278c7 6369 {
82641697 6370 struct face *face;
b7826503 6371 CHECK_NUMBER (n);
c0617987 6372 face = FACE_FROM_ID (SELECTED_FRAME (), XINT (n));
82641697
GM
6373 if (face == NULL)
6374 error ("Not a valid face");
6375 dump_realized_face (face);
f5e278c7 6376 }
178c5d9c 6377
c115973b
JB
6378 return Qnil;
6379}
b5c53576 6380
b5c53576 6381
a7ca3326 6382DEFUN ("show-face-resources", Fshow_face_resources, Sshow_face_resources,
7ee72033 6383 0, 0, 0, doc: /* */)
5842a27b 6384 (void)
b5c53576 6385{
82641697
GM
6386 fprintf (stderr, "number of colors = %d\n", ncolors_allocated);
6387 fprintf (stderr, "number of pixmaps = %d\n", npixmaps_allocated);
6388 fprintf (stderr, "number of GCs = %d\n", ngcs);
6389 return Qnil;
b5c53576
RS
6390}
6391
e509cfa6 6392#endif /* GLYPH_DEBUG */
82641697 6393
b5c53576 6394
c115973b 6395\f
82641697
GM
6396/***********************************************************************
6397 Initialization
6398 ***********************************************************************/
cb637678 6399
c115973b 6400void
971de7fb 6401syms_of_xfaces (void)
c115973b 6402{
cd3520a4
JB
6403 DEFSYM (Qface, "face");
6404 DEFSYM (Qface_no_inherit, "face-no-inherit");
6405 DEFSYM (Qbitmap_spec_p, "bitmap-spec-p");
6406 DEFSYM (Qframe_set_background_mode, "frame-set-background-mode");
178c5d9c 6407
82641697 6408 /* Lisp face attribute keywords. */
cd3520a4
JB
6409 DEFSYM (QCfamily, ":family");
6410 DEFSYM (QCheight, ":height");
6411 DEFSYM (QCweight, ":weight");
6412 DEFSYM (QCslant, ":slant");
6413 DEFSYM (QCunderline, ":underline");
6414 DEFSYM (QCinverse_video, ":inverse-video");
6415 DEFSYM (QCreverse_video, ":reverse-video");
6416 DEFSYM (QCforeground, ":foreground");
6417 DEFSYM (QCbackground, ":background");
6418 DEFSYM (QCstipple, ":stipple");
6419 DEFSYM (QCwidth, ":width");
6420 DEFSYM (QCfont, ":font");
6421 DEFSYM (QCfontset, ":fontset");
6422 DEFSYM (QCbold, ":bold");
6423 DEFSYM (QCitalic, ":italic");
6424 DEFSYM (QCoverline, ":overline");
6425 DEFSYM (QCstrike_through, ":strike-through");
6426 DEFSYM (QCbox, ":box");
6427 DEFSYM (QCinherit, ":inherit");
82641697
GM
6428
6429 /* Symbols used for Lisp face attribute values. */
cd3520a4
JB
6430 DEFSYM (QCcolor, ":color");
6431 DEFSYM (QCline_width, ":line-width");
6432 DEFSYM (QCstyle, ":style");
9b0e3eba
AA
6433 DEFSYM (Qline, "line");
6434 DEFSYM (Qwave, "wave");
cd3520a4
JB
6435 DEFSYM (Qreleased_button, "released-button");
6436 DEFSYM (Qpressed_button, "pressed-button");
6437 DEFSYM (Qnormal, "normal");
cd3520a4
JB
6438 DEFSYM (Qextra_light, "extra-light");
6439 DEFSYM (Qlight, "light");
6440 DEFSYM (Qsemi_light, "semi-light");
6441 DEFSYM (Qsemi_bold, "semi-bold");
6442 DEFSYM (Qbold, "bold");
6443 DEFSYM (Qextra_bold, "extra-bold");
6444 DEFSYM (Qultra_bold, "ultra-bold");
6445 DEFSYM (Qoblique, "oblique");
6446 DEFSYM (Qitalic, "italic");
cd3520a4
JB
6447 DEFSYM (Qbackground_color, "background-color");
6448 DEFSYM (Qforeground_color, "foreground-color");
6449 DEFSYM (Qunspecified, "unspecified");
a3720aa2 6450 DEFSYM (QCignore_defface, ":ignore-defface");
cd3520a4
JB
6451
6452 DEFSYM (Qface_alias, "face-alias");
6453 DEFSYM (Qdefault, "default");
6454 DEFSYM (Qtool_bar, "tool-bar");
6455 DEFSYM (Qregion, "region");
6456 DEFSYM (Qfringe, "fringe");
6457 DEFSYM (Qheader_line, "header-line");
6458 DEFSYM (Qscroll_bar, "scroll-bar");
6459 DEFSYM (Qmenu, "menu");
6460 DEFSYM (Qcursor, "cursor");
6461 DEFSYM (Qborder, "border");
6462 DEFSYM (Qmouse, "mouse");
6463 DEFSYM (Qmode_line_inactive, "mode-line-inactive");
6464 DEFSYM (Qvertical_border, "vertical-border");
6465 DEFSYM (Qtty_color_desc, "tty-color-desc");
6466 DEFSYM (Qtty_color_standard_values, "tty-color-standard-values");
6467 DEFSYM (Qtty_color_by_index, "tty-color-by-index");
6468 DEFSYM (Qtty_color_alist, "tty-color-alist");
6469 DEFSYM (Qscalable_fonts_allowed, "scalable-fonts-allowed");
82641697 6470
6c6f1994 6471 Vparam_value_alist = list1 (Fcons (Qnil, Qnil));
dbc968b8 6472 staticpro (&Vparam_value_alist);
434b9cc5
GM
6473 Vface_alternative_font_family_alist = Qnil;
6474 staticpro (&Vface_alternative_font_family_alist);
32fcc231
GM
6475 Vface_alternative_font_registry_alist = Qnil;
6476 staticpro (&Vface_alternative_font_registry_alist);
434b9cc5 6477
82641697
GM
6478 defsubr (&Sinternal_make_lisp_face);
6479 defsubr (&Sinternal_lisp_face_p);
6480 defsubr (&Sinternal_set_lisp_face_attribute);
c3cee013 6481#ifdef HAVE_WINDOW_SYSTEM
82641697 6482 defsubr (&Sinternal_set_lisp_face_attribute_from_resource);
42608ba8 6483#endif
ea4fa0af
GM
6484 defsubr (&Scolor_gray_p);
6485 defsubr (&Scolor_supported_p);
7ded3383
AR
6486#ifndef HAVE_X_WINDOWS
6487 defsubr (&Sx_load_color_file);
6488#endif
cdfaafa9
MB
6489 defsubr (&Sface_attribute_relative_p);
6490 defsubr (&Smerge_face_attribute);
82641697
GM
6491 defsubr (&Sinternal_get_lisp_face_attribute);
6492 defsubr (&Sinternal_lisp_face_attribute_values);
6493 defsubr (&Sinternal_lisp_face_equal_p);
6494 defsubr (&Sinternal_lisp_face_empty_p);
6495 defsubr (&Sinternal_copy_lisp_face);
6496 defsubr (&Sinternal_merge_in_global_face);
6497 defsubr (&Sface_font);
6498 defsubr (&Sframe_face_alist);
9717e36c 6499 defsubr (&Sdisplay_supports_face_attributes_p);
b35df831 6500 defsubr (&Scolor_distance);
82641697
GM
6501 defsubr (&Sinternal_set_font_selection_order);
6502 defsubr (&Sinternal_set_alternative_font_family_alist);
32fcc231 6503 defsubr (&Sinternal_set_alternative_font_registry_alist);
f6608d5c 6504 defsubr (&Sface_attributes_as_vector);
e509cfa6 6505#ifdef GLYPH_DEBUG
82641697
GM
6506 defsubr (&Sdump_face);
6507 defsubr (&Sshow_face_resources);
6508#endif /* GLYPH_DEBUG */
6509 defsubr (&Sclear_face_cache);
a4a76b61 6510 defsubr (&Stty_suppress_bold_inverse_default_colors);
82641697 6511
38426dee 6512#if defined DEBUG_X_COLORS && defined HAVE_X_WINDOWS
08dc08dc
GM
6513 defsubr (&Sdump_colors);
6514#endif
6515
29208e82 6516 DEFVAR_LISP ("face-new-frame-defaults", Vface_new_frame_defaults,
7ee72033 6517 doc: /* List of global face definitions (for internal use only.) */);
82641697 6518 Vface_new_frame_defaults = Qnil;
178c5d9c 6519
29208e82 6520 DEFVAR_LISP ("face-default-stipple", Vface_default_stipple,
fb7ada5f 6521 doc: /* Default stipple pattern used on monochrome displays.
228299fa
GM
6522This stipple pattern is used on monochrome displays
6523instead of shades of gray for a face background color.
6524See `set-face-stipple' for possible values for this variable. */);
2a0213a6 6525 Vface_default_stipple = build_pure_c_string ("gray3");
82641697 6526
29208e82 6527 DEFVAR_LISP ("tty-defined-color-alist", Vtty_defined_color_alist,
46710489
GM
6528 doc: /* An alist of defined terminal colors and their RGB values.
6529See the docstring of `tty-color-alist' for the details. */);
ae4b4ba5
GM
6530 Vtty_defined_color_alist = Qnil;
6531
29208e82 6532 DEFVAR_LISP ("scalable-fonts-allowed", Vscalable_fonts_allowed,
7ee72033 6533 doc: /* Allowed scalable fonts.
228299fa
GM
6534A value of nil means don't allow any scalable fonts.
6535A value of t means allow any scalable font.
6536Otherwise, value must be a list of regular expressions. A font may be
6537scaled if its name matches a regular expression in the list.
6538Note that if value is nil, a scalable font might still be used, if no
6539other font of the appropriate family and registry is available. */);
3cf80731 6540 Vscalable_fonts_allowed = Qnil;
b5c53576 6541
29208e82 6542 DEFVAR_LISP ("face-ignored-fonts", Vface_ignored_fonts,
7ee72033 6543 doc: /* List of ignored fonts.
228299fa
GM
6544Each element is a regular expression that matches names of fonts to
6545ignore. */);
c824bfbc
KH
6546 Vface_ignored_fonts = Qnil;
6547
29208e82 6548 DEFVAR_LISP ("face-remapping-alist", Vface_remapping_alist,
f2cec7a9
MB
6549 doc: /* Alist of face remappings.
6550Each element is of the form:
6551
fb5b8aca 6552 (FACE . REPLACEMENT),
f2cec7a9 6553
fb5b8aca
CY
6554which causes display of the face FACE to use REPLACEMENT instead.
6555REPLACEMENT is a face specification, i.e. one of the following:
f2cec7a9 6556
fb5b8aca
CY
6557 (1) a face name
6558 (2) a property list of attribute/value pairs, or
6559 (3) a list in which each element has the form of (1) or (2).
f2cec7a9 6560
fb5b8aca
CY
6561List values for REPLACEMENT are merged to form the final face
6562specification, with earlier entries taking precedence, in the same as
6563as in the `face' text property.
6564
6565Face-name remapping cycles are suppressed; recursive references use
6566the underlying face instead of the remapped face. So a remapping of
6567the form:
f2cec7a9
MB
6568
6569 (FACE EXTRA-FACE... FACE)
6570
6571or:
6572
6573 (FACE (FACE-ATTR VAL ...) FACE)
6574
fb5b8aca
CY
6575causes EXTRA-FACE... or (FACE-ATTR VAL ...) to be _merged_ with the
6576existing definition of FACE. Note that this isn't necessary for the
6577default face, since every face inherits from the default face.
f2cec7a9 6578
fb5b8aca
CY
6579If this variable is made buffer-local, the face remapping takes effect
6580only in that buffer. For instance, the mode my-mode could define a
6581face `my-mode-default', and then in the mode setup function, do:
f2cec7a9
MB
6582
6583 (set (make-local-variable 'face-remapping-alist)
b5f03016 6584 '((default my-mode-default)))).
635c0aa1
CY
6585
6586Because Emacs normally only redraws screen areas when the underlying
6587buffer contents change, you may need to call `redraw-display' after
6588changing this variable for it to take effect. */);
f2cec7a9
MB
6589 Vface_remapping_alist = Qnil;
6590
29208e82 6591 DEFVAR_LISP ("face-font-rescale-alist", Vface_font_rescale_alist,
f70400f2 6592 doc: /* Alist of fonts vs the rescaling factors.
96f9306b
KH
6593Each element is a cons (FONT-PATTERN . RESCALE-RATIO), where
6594FONT-PATTERN is a font-spec or a regular expression matching a font name, and
f70400f2
KH
6595RESCALE-RATIO is a floating point number to specify how much larger
6596\(or smaller) font we should use. For instance, if a face requests
6597a font of 10 point, we actually use a font of 10 * RESCALE-RATIO point. */);
6598 Vface_font_rescale_alist = Qnil;
6599
c3cee013 6600#ifdef HAVE_WINDOW_SYSTEM
fef04523 6601 defsubr (&Sbitmap_spec_p);
82641697
GM
6602 defsubr (&Sx_list_fonts);
6603 defsubr (&Sinternal_face_x_get_resource);
92610620 6604 defsubr (&Sx_family_fonts);
32247e3d 6605#endif
c115973b 6606}