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