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