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