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