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