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