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