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