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