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