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