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