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