* src/term.c: Remove dead code.
[emacs.git] / src / xfaces.c
blob8d88281d43e3ce34f42f0969c4d36ba202219e14
1 /* xfaces.c -- "Face" primitives.
2 Copyright (C) 1993, 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* New face implementation by Gerd Moellmann <gerd@gnu.org>. */
22 /* Faces.
24 When using Emacs with X, the display style of characters can be
25 changed by defining `faces'. Each face can specify the following
26 display attributes:
28 1. Font family name.
30 2. Font foundary name.
32 3. Relative proportionate width, aka character set width or set
33 width (swidth), e.g. `semi-compressed'.
35 4. Font height in 1/10pt.
37 5. Font weight, e.g. `bold'.
39 6. Font slant, e.g. `italic'.
41 7. Foreground color.
43 8. Background color.
45 9. Whether or not characters should be underlined, and in what color.
47 10. Whether or not characters should be displayed in inverse video.
49 11. A background stipple, a bitmap.
51 12. Whether or not characters should be overlined, and in what color.
53 13. Whether or not characters should be strike-through, and in what
54 color.
56 14. Whether or not a box should be drawn around characters, the box
57 type, and, for simple boxes, in what color.
59 15. Font-spec, or nil. This is a special attribute.
61 A font-spec is a collection of font attributes (specs).
63 When this attribute is specified, the face uses a font matching
64 with the specs as is except for what overwritten by the specs in
65 the fontset (see below). In addition, the other font-related
66 attributes (1st thru 5th) are updated from the spec.
68 On the other hand, if one of the other font-related attributes are
69 specified, the correspoinding specs in this attribute is set to nil.
71 15. A face name or list of face names from which to inherit attributes.
73 16. A specified average font width, which is invisible from Lisp,
74 and is used to ensure that a font specified on the command line,
75 for example, can be matched exactly.
77 17. A fontset name. This is another special attribute.
79 A fontset is a mappings from characters to font-specs, and the
80 specs overwrite the font-spec in the 14th attribute.
83 Faces are frame-local by nature because Emacs allows to define the
84 same named face (face names are symbols) differently for different
85 frames. Each frame has an alist of face definitions for all named
86 faces. The value of a named face in such an alist is a Lisp vector
87 with the symbol `face' in slot 0, and a slot for each of the face
88 attributes mentioned above.
90 There is also a global face alist `Vface_new_frame_defaults'. Face
91 definitions from this list are used to initialize faces of newly
92 created frames.
94 A face doesn't have to specify all attributes. Those not specified
95 have a value of `unspecified'. Faces specifying all attributes but
96 the 14th are called `fully-specified'.
99 Face merging.
101 The display style of a given character in the text is determined by
102 combining several faces. This process is called `face merging'.
103 Any aspect of the display style that isn't specified by overlays or
104 text properties is taken from the `default' face. Since it is made
105 sure that the default face is always fully-specified, face merging
106 always results in a fully-specified face.
109 Face realization.
111 After all face attributes for a character have been determined by
112 merging faces of that character, that face is `realized'. The
113 realization process maps face attributes to what is physically
114 available on the system where Emacs runs. The result is a
115 `realized face' in form of a struct face which is stored in the
116 face cache of the frame on which it was realized.
118 Face realization is done in the context of the character to display
119 because different fonts may be used for different characters. In
120 other words, for characters that have different font
121 specifications, different realized faces are needed to display
122 them.
124 Font specification is done by fontsets. See the comment in
125 fontset.c for the details. In the current implementation, all ASCII
126 characters share the same font in a fontset.
128 Faces are at first realized for ASCII characters, and, at that
129 time, assigned a specific realized fontset. Hereafter, we call
130 such a face as `ASCII face'. When a face for a multibyte character
131 is realized, it inherits (thus shares) a fontset of an ASCII face
132 that has the same attributes other than font-related ones.
134 Thus, all realized faces have a realized fontset.
137 Unibyte text.
139 Unibyte text (i.e. raw 8-bit characters) is displayed with the same
140 font as ASCII characters. That is because it is expected that
141 unibyte text users specify a font that is suitable both for ASCII
142 and raw 8-bit characters.
145 Font selection.
147 Font selection tries to find the best available matching font for a
148 given (character, face) combination.
150 If the face specifies a fontset name, that fontset determines a
151 pattern for fonts of the given character. If the face specifies a
152 font name or the other font-related attributes, a fontset is
153 realized from the default fontset. In that case, that
154 specification determines a pattern for ASCII characters and the
155 default fontset determines a pattern for multibyte characters.
157 Available fonts on the system on which Emacs runs are then matched
158 against the font pattern. The result of font selection is the best
159 match for the given face attributes in this font list.
161 Font selection can be influenced by the user.
163 1. The user can specify the relative importance he gives the face
164 attributes width, height, weight, and slant by setting
165 face-font-selection-order (faces.el) to a list of face attribute
166 names. The default is '(:width :height :weight :slant), and means
167 that font selection first tries to find a good match for the font
168 width specified by a face, then---within fonts with that
169 width---tries to find a best match for the specified font height,
170 etc.
172 2. Setting face-font-family-alternatives allows the user to
173 specify alternative font families to try if a family specified by a
174 face doesn't exist.
176 3. Setting face-font-registry-alternatives allows the user to
177 specify all alternative font registries to try for a face
178 specifying a registry.
180 4. Setting face-ignored-fonts allows the user to ignore specific
181 fonts.
184 Character composition.
186 Usually, the realization process is already finished when Emacs
187 actually reflects the desired glyph matrix on the screen. However,
188 on displaying a composition (sequence of characters to be composed
189 on the screen), a suitable font for the components of the
190 composition is selected and realized while drawing them on the
191 screen, i.e. the realization process is delayed but in principle
192 the same.
195 Initialization of basic faces.
197 The faces `default', `modeline' are considered `basic faces'.
198 When redisplay happens the first time for a newly created frame,
199 basic faces are realized for CHARSET_ASCII. Frame parameters are
200 used to fill in unspecified attributes of the default face. */
202 #include <config.h>
203 #include <stdio.h>
204 #include <sys/types.h>
205 #include <sys/stat.h>
206 #include <stdio.h> /* This needs to be before termchar.h */
207 #include <setjmp.h>
209 #include "lisp.h"
210 #include "character.h"
211 #include "charset.h"
212 #include "keyboard.h"
213 #include "frame.h"
214 #include "termhooks.h"
216 #ifdef HAVE_X_WINDOWS
217 #include "xterm.h"
218 #ifdef USE_MOTIF
219 #include <Xm/Xm.h>
220 #include <Xm/XmStrDefs.h>
221 #endif /* USE_MOTIF */
222 #endif /* HAVE_X_WINDOWS */
224 #ifdef MSDOS
225 #include "dosfns.h"
226 #endif
228 #ifdef WINDOWSNT
229 #include "w32term.h"
230 #include "fontset.h"
231 /* Redefine X specifics to W32 equivalents to avoid cluttering the
232 code with #ifdef blocks. */
233 #undef FRAME_X_DISPLAY_INFO
234 #define FRAME_X_DISPLAY_INFO FRAME_W32_DISPLAY_INFO
235 #define x_display_info w32_display_info
236 #define FRAME_X_FONT_TABLE FRAME_W32_FONT_TABLE
237 #define check_x check_w32
238 #define GCGraphicsExposures 0
239 #endif /* WINDOWSNT */
241 #ifdef HAVE_NS
242 #include "nsterm.h"
243 #undef FRAME_X_DISPLAY_INFO
244 #define FRAME_X_DISPLAY_INFO FRAME_NS_DISPLAY_INFO
245 #define x_display_info ns_display_info
246 #define FRAME_X_FONT_TABLE FRAME_NS_FONT_TABLE
247 #define check_x check_ns
248 #define GCGraphicsExposures 0
249 #endif /* HAVE_NS */
251 #include "buffer.h"
252 #include "dispextern.h"
253 #include "blockinput.h"
254 #include "window.h"
255 #include "intervals.h"
256 #include "termchar.h"
258 #include "font.h"
259 #ifdef HAVE_WINDOW_SYSTEM
260 #include "fontset.h"
261 #endif /* HAVE_WINDOW_SYSTEM */
263 #ifdef HAVE_X_WINDOWS
265 /* Compensate for a bug in Xos.h on some systems, on which it requires
266 time.h. On some such systems, Xos.h tries to redefine struct
267 timeval and struct timezone if USG is #defined while it is
268 #included. */
270 #ifdef XOS_NEEDS_TIME_H
271 #include <time.h>
272 #undef USG
273 #include <X11/Xos.h>
274 #define USG
275 #define __TIMEVAL__
276 #else /* not XOS_NEEDS_TIME_H */
277 #include <X11/Xos.h>
278 #endif /* not XOS_NEEDS_TIME_H */
280 #endif /* HAVE_X_WINDOWS */
282 #include <ctype.h>
284 /* Number of pt per inch (from the TeXbook). */
286 #define PT_PER_INCH 72.27
288 /* Non-zero if face attribute ATTR is unspecified. */
290 #define UNSPECIFIEDP(ATTR) EQ ((ATTR), Qunspecified)
292 /* Non-zero if face attribute ATTR is `ignore-defface'. */
294 #define IGNORE_DEFFACE_P(ATTR) EQ ((ATTR), Qignore_defface)
296 /* Value is the number of elements of VECTOR. */
298 #define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
300 /* Make a copy of string S on the stack using alloca. Value is a pointer
301 to the copy. */
303 #define STRDUPA(S) strcpy ((char *) alloca (strlen ((S)) + 1), (S))
305 /* Make a copy of the contents of Lisp string S on the stack using
306 alloca. Value is a pointer to the copy. */
308 #define LSTRDUPA(S) STRDUPA (SDATA ((S)))
310 /* Size of hash table of realized faces in face caches (should be a
311 prime number). */
313 #define FACE_CACHE_BUCKETS_SIZE 1001
315 /* Keyword symbols used for face attribute names. */
317 Lisp_Object QCfamily, QCheight, QCweight, QCslant, QCunderline;
318 Lisp_Object QCinverse_video, QCforeground, QCbackground, QCstipple;
319 Lisp_Object QCwidth, QCfont, QCbold, QCitalic;
320 Lisp_Object QCreverse_video;
321 Lisp_Object QCoverline, QCstrike_through, QCbox, QCinherit;
322 Lisp_Object QCfontset;
324 /* Keywords symbols used for font properties. */
325 extern Lisp_Object QCfoundry, QCadstyle, QCregistry;
326 extern Lisp_Object QCspacing, QCsize, QCavgwidth;
327 extern Lisp_Object Qp;
329 /* Symbols used for attribute values. */
331 Lisp_Object Qnormal, Qbold, Qultra_light, Qextra_light, Qlight;
332 Lisp_Object Qsemi_light, Qsemi_bold, Qextra_bold, Qultra_bold;
333 Lisp_Object Qoblique, Qitalic, Qreverse_oblique, Qreverse_italic;
334 Lisp_Object Qultra_condensed, Qextra_condensed, Qcondensed;
335 Lisp_Object Qsemi_condensed, Qsemi_expanded, Qexpanded, Qextra_expanded;
336 Lisp_Object Qultra_expanded;
337 Lisp_Object Qreleased_button, Qpressed_button;
338 Lisp_Object QCstyle, QCcolor, QCline_width;
339 Lisp_Object Qunspecified;
340 Lisp_Object Qignore_defface;
342 char unspecified_fg[] = "unspecified-fg", unspecified_bg[] = "unspecified-bg";
344 /* The name of the function to call when the background of the frame
345 has changed, frame_set_background_mode. */
347 Lisp_Object Qframe_set_background_mode;
349 /* Names of basic faces. */
351 Lisp_Object Qdefault, Qtool_bar, Qregion, Qfringe;
352 Lisp_Object Qheader_line, Qscroll_bar, Qcursor, Qborder, Qmouse, Qmenu;
353 Lisp_Object Qmode_line_inactive, Qvertical_border;
354 extern Lisp_Object Qmode_line;
356 /* The symbol `face-alias'. A symbols having that property is an
357 alias for another face. Value of the property is the name of
358 the aliased face. */
360 Lisp_Object Qface_alias;
362 extern Lisp_Object Qcircular_list;
364 /* Default stipple pattern used on monochrome displays. This stipple
365 pattern is used on monochrome displays instead of shades of gray
366 for a face background color. See `set-face-stipple' for possible
367 values for this variable. */
369 Lisp_Object Vface_default_stipple;
371 /* Alist of alternative font families. Each element is of the form
372 (FAMILY FAMILY1 FAMILY2 ...). If fonts of FAMILY can't be loaded,
373 try FAMILY1, then FAMILY2, ... */
375 Lisp_Object Vface_alternative_font_family_alist;
377 /* Alist of alternative font registries. Each element is of the form
378 (REGISTRY REGISTRY1 REGISTRY2...). If fonts of REGISTRY can't be
379 loaded, try REGISTRY1, then REGISTRY2, ... */
381 Lisp_Object Vface_alternative_font_registry_alist;
383 /* Allowed scalable fonts. A value of nil means don't allow any
384 scalable fonts. A value of t means allow the use of any scalable
385 font. Otherwise, value must be a list of regular expressions. A
386 font may be scaled if its name matches a regular expression in the
387 list. */
389 Lisp_Object Vscalable_fonts_allowed, Qscalable_fonts_allowed;
391 /* List of regular expressions that matches names of fonts to ignore. */
393 Lisp_Object Vface_ignored_fonts;
395 /* Alist of font name patterns vs the rescaling factor. */
397 Lisp_Object Vface_font_rescale_alist;
399 /* Maximum number of fonts to consider in font_list. If not an
400 integer > 0, DEFAULT_FONT_LIST_LIMIT is used instead. */
402 Lisp_Object Vfont_list_limit;
403 #define DEFAULT_FONT_LIST_LIMIT 100
405 /* The symbols `foreground-color' and `background-color' which can be
406 used as part of a `face' property. This is for compatibility with
407 Emacs 20.2. */
409 Lisp_Object Qforeground_color, Qbackground_color;
411 /* The symbols `face' and `mouse-face' used as text properties. */
413 Lisp_Object Qface;
415 /* Property for basic faces which other faces cannot inherit. */
417 Lisp_Object Qface_no_inherit;
419 /* Error symbol for wrong_type_argument in load_pixmap. */
421 Lisp_Object Qbitmap_spec_p;
423 /* Alist of global face definitions. Each element is of the form
424 (FACE . LFACE) where FACE is a symbol naming a face and LFACE
425 is a Lisp vector of face attributes. These faces are used
426 to initialize faces for new frames. */
428 Lisp_Object Vface_new_frame_defaults;
430 /* Alist of face remappings. Each element is of the form:
431 (FACE REPLACEMENT...) which causes display of the face FACE to use
432 REPLACEMENT... instead. REPLACEMENT... is interpreted the same way
433 the value of a `face' text property is: it may be (1) A face name,
434 (2) A list of face names, (3) A property-list of face attribute/value
435 pairs, or (4) A list of face names intermixed with lists containing
436 face attribute/value pairs.
438 Multiple entries in REPLACEMENT... are merged together to form the final
439 result, with faces or attributes earlier in the list taking precedence
440 over those that are later.
442 Face-name remapping cycles are suppressed; recursive references use
443 the underlying face instead of the remapped face. */
445 Lisp_Object Vface_remapping_alist;
447 /* The next ID to assign to Lisp faces. */
449 static int next_lface_id;
451 /* A vector mapping Lisp face Id's to face names. */
453 static Lisp_Object *lface_id_to_name;
454 static int lface_id_to_name_size;
456 /* TTY color-related functions (defined in tty-colors.el). */
458 Lisp_Object Qtty_color_desc, Qtty_color_by_index, Qtty_color_standard_values;
460 /* The name of the function used to compute colors on TTYs. */
462 Lisp_Object Qtty_color_alist;
464 /* An alist of defined terminal colors and their RGB values. */
466 Lisp_Object Vtty_defined_color_alist;
468 /* Counter for calls to clear_face_cache. If this counter reaches
469 CLEAR_FONT_TABLE_COUNT, and a frame has more than
470 CLEAR_FONT_TABLE_NFONTS load, unused fonts are freed. */
472 static int clear_font_table_count;
473 #define CLEAR_FONT_TABLE_COUNT 100
474 #define CLEAR_FONT_TABLE_NFONTS 10
476 /* Non-zero means face attributes have been changed since the last
477 redisplay. Used in redisplay_internal. */
479 int face_change_count;
481 /* Non-zero means don't display bold text if a face's foreground
482 and background colors are the inverse of the default colors of the
483 display. This is a kluge to suppress `bold black' foreground text
484 which is hard to read on an LCD monitor. */
486 int tty_suppress_bold_inverse_default_colors_p;
488 /* A list of the form `((x . y))' used to avoid consing in
489 Finternal_set_lisp_face_attribute. */
491 static Lisp_Object Vparam_value_alist;
493 /* The total number of colors currently allocated. */
495 #if GLYPH_DEBUG
496 static int ncolors_allocated;
497 static int npixmaps_allocated;
498 static int ngcs;
499 #endif
501 /* Non-zero means the definition of the `menu' face for new frames has
502 been changed. */
504 int menu_face_changed_default;
507 /* Function prototypes. */
509 struct table_entry;
510 struct named_merge_point;
512 static void map_tty_color (struct frame *, struct face *,
513 enum lface_attribute_index, int *);
514 static Lisp_Object resolve_face_name (Lisp_Object, int);
515 static void set_font_frame_param (Lisp_Object, Lisp_Object);
516 static int get_lface_attributes (struct frame *, Lisp_Object, Lisp_Object *,
517 int, struct named_merge_point *);
518 static int load_pixmap (struct frame *, Lisp_Object, unsigned *, unsigned *);
519 static struct frame *frame_or_selected_frame (Lisp_Object, int);
520 static void load_face_colors (struct frame *, struct face *, Lisp_Object *);
521 static void free_face_colors (struct frame *, struct face *);
522 static int face_color_gray_p (struct frame *, const char *);
523 static struct face *realize_face (struct face_cache *, Lisp_Object *,
524 int);
525 static struct face *realize_non_ascii_face (struct frame *, Lisp_Object,
526 struct face *);
527 static struct face *realize_x_face (struct face_cache *, Lisp_Object *);
528 static struct face *realize_tty_face (struct face_cache *, Lisp_Object *);
529 static int realize_basic_faces (struct frame *);
530 static int realize_default_face (struct frame *);
531 static void realize_named_face (struct frame *, Lisp_Object, int);
532 static int lface_fully_specified_p (Lisp_Object *);
533 static int lface_equal_p (Lisp_Object *, Lisp_Object *);
534 static unsigned hash_string_case_insensitive (Lisp_Object);
535 static unsigned lface_hash (Lisp_Object *);
536 static int lface_same_font_attributes_p (Lisp_Object *, Lisp_Object *);
537 static struct face_cache *make_face_cache (struct frame *);
538 static void clear_face_gcs (struct face_cache *);
539 static void free_face_cache (struct face_cache *);
540 static int face_fontset (Lisp_Object *);
541 static void merge_face_vectors (struct frame *, Lisp_Object *, Lisp_Object*,
542 struct named_merge_point *);
543 static int merge_face_ref (struct frame *, Lisp_Object, Lisp_Object *,
544 int, struct named_merge_point *);
545 static int set_lface_from_font (struct frame *, Lisp_Object, Lisp_Object,
546 int);
547 static Lisp_Object lface_from_face_name (struct frame *, Lisp_Object, int);
548 static struct face *make_realized_face (Lisp_Object *);
549 static void cache_face (struct face_cache *, struct face *, unsigned);
550 static void uncache_face (struct face_cache *, struct face *);
552 #ifdef HAVE_WINDOW_SYSTEM
554 static GC x_create_gc (struct frame *, unsigned long, XGCValues *);
555 static void x_free_gc (struct frame *, GC);
557 #ifdef USE_X_TOOLKIT
558 static void x_update_menu_appearance (struct frame *);
560 extern void free_frame_menubar (struct frame *);
561 #endif /* USE_X_TOOLKIT */
563 #endif /* HAVE_WINDOW_SYSTEM */
566 /***********************************************************************
567 Utilities
568 ***********************************************************************/
570 #ifdef HAVE_X_WINDOWS
572 #ifdef DEBUG_X_COLORS
574 /* The following is a poor mans infrastructure for debugging X color
575 allocation problems on displays with PseudoColor-8. Some X servers
576 like 3.3.5 XF86_SVGA with Matrox cards apparently don't implement
577 color reference counts completely so that they don't signal an
578 error when a color is freed whose reference count is already 0.
579 Other X servers do. To help me debug this, the following code
580 implements a simple reference counting schema of its own, for a
581 single display/screen. --gerd. */
583 /* Reference counts for pixel colors. */
585 int color_count[256];
587 /* Register color PIXEL as allocated. */
589 void
590 register_color (pixel)
591 unsigned long pixel;
593 xassert (pixel < 256);
594 ++color_count[pixel];
598 /* Register color PIXEL as deallocated. */
600 void
601 unregister_color (pixel)
602 unsigned long pixel;
604 xassert (pixel < 256);
605 if (color_count[pixel] > 0)
606 --color_count[pixel];
607 else
608 abort ();
612 /* Register N colors from PIXELS as deallocated. */
614 void
615 unregister_colors (pixels, n)
616 unsigned long *pixels;
617 int n;
619 int i;
620 for (i = 0; i < n; ++i)
621 unregister_color (pixels[i]);
625 DEFUN ("dump-colors", Fdump_colors, Sdump_colors, 0, 0, 0,
626 doc: /* Dump currently allocated colors to stderr. */)
627 (void)
629 int i, n;
631 fputc ('\n', stderr);
633 for (i = n = 0; i < sizeof color_count / sizeof color_count[0]; ++i)
634 if (color_count[i])
636 fprintf (stderr, "%3d: %5d", i, color_count[i]);
637 ++n;
638 if (n % 5 == 0)
639 fputc ('\n', stderr);
640 else
641 fputc ('\t', stderr);
644 if (n % 5 != 0)
645 fputc ('\n', stderr);
646 return Qnil;
649 #endif /* DEBUG_X_COLORS */
652 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
653 color values. Interrupt input must be blocked when this function
654 is called. */
656 void
657 x_free_colors (struct frame *f, long unsigned int *pixels, int npixels)
659 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
661 /* If display has an immutable color map, freeing colors is not
662 necessary and some servers don't allow it. So don't do it. */
663 if (class != StaticColor && class != StaticGray && class != TrueColor)
665 #ifdef DEBUG_X_COLORS
666 unregister_colors (pixels, npixels);
667 #endif
668 XFreeColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
669 pixels, npixels, 0);
674 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
675 color values. Interrupt input must be blocked when this function
676 is called. */
678 void
679 x_free_dpy_colors (Display *dpy, Screen *screen, Colormap cmap, long unsigned int *pixels, int npixels)
681 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
682 int class = dpyinfo->visual->class;
684 /* If display has an immutable color map, freeing colors is not
685 necessary and some servers don't allow it. So don't do it. */
686 if (class != StaticColor && class != StaticGray && class != TrueColor)
688 #ifdef DEBUG_X_COLORS
689 unregister_colors (pixels, npixels);
690 #endif
691 XFreeColors (dpy, cmap, pixels, npixels, 0);
696 /* Create and return a GC for use on frame F. GC values and mask
697 are given by XGCV and MASK. */
699 static INLINE GC
700 x_create_gc (struct frame *f, long unsigned int mask, XGCValues *xgcv)
702 GC gc;
703 BLOCK_INPUT;
704 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), mask, xgcv);
705 UNBLOCK_INPUT;
706 IF_DEBUG (++ngcs);
707 return gc;
711 /* Free GC which was used on frame F. */
713 static INLINE void
714 x_free_gc (struct frame *f, GC gc)
716 eassert (interrupt_input_blocked);
717 IF_DEBUG (xassert (--ngcs >= 0));
718 XFreeGC (FRAME_X_DISPLAY (f), gc);
721 #endif /* HAVE_X_WINDOWS */
723 #ifdef WINDOWSNT
724 /* W32 emulation of GCs */
726 static INLINE GC
727 x_create_gc (struct frame *f, unsigned long mask, XGCValues *xgcv)
729 GC gc;
730 BLOCK_INPUT;
731 gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, xgcv);
732 UNBLOCK_INPUT;
733 IF_DEBUG (++ngcs);
734 return gc;
738 /* Free GC which was used on frame F. */
740 static INLINE void
741 x_free_gc (struct frame *f, GC gc)
743 IF_DEBUG (xassert (--ngcs >= 0));
744 xfree (gc);
747 #endif /* WINDOWSNT */
749 #ifdef HAVE_NS
750 /* NS emulation of GCs */
752 static INLINE GC
753 x_create_gc (f, mask, xgcv)
754 struct frame *f;
755 unsigned long mask;
756 XGCValues *xgcv;
758 GC gc = xmalloc (sizeof (*gc));
759 if (gc)
760 memcpy (gc, xgcv, sizeof (XGCValues));
761 return gc;
764 static INLINE void
765 x_free_gc (f, gc)
766 struct frame *f;
767 GC gc;
769 xfree (gc);
771 #endif /* HAVE_NS */
773 /* Like strcasecmp/stricmp. Used to compare parts of font names which
774 are in ISO8859-1. */
777 xstrcasecmp (const unsigned char *s1, const unsigned char *s2)
779 while (*s1 && *s2)
781 unsigned char c1 = tolower (*s1);
782 unsigned char c2 = tolower (*s2);
783 if (c1 != c2)
784 return c1 < c2 ? -1 : 1;
785 ++s1, ++s2;
788 if (*s1 == 0)
789 return *s2 == 0 ? 0 : -1;
790 return 1;
794 /* If FRAME is nil, return a pointer to the selected frame.
795 Otherwise, check that FRAME is a live frame, and return a pointer
796 to it. NPARAM is the parameter number of FRAME, for
797 CHECK_LIVE_FRAME. This is here because it's a frequent pattern in
798 Lisp function definitions. */
800 static INLINE struct frame *
801 frame_or_selected_frame (Lisp_Object frame, int nparam)
803 if (NILP (frame))
804 frame = selected_frame;
806 CHECK_LIVE_FRAME (frame);
807 return XFRAME (frame);
811 /***********************************************************************
812 Frames and faces
813 ***********************************************************************/
815 /* Initialize face cache and basic faces for frame F. */
817 void
818 init_frame_faces (struct frame *f)
820 /* Make a face cache, if F doesn't have one. */
821 if (FRAME_FACE_CACHE (f) == NULL)
822 FRAME_FACE_CACHE (f) = make_face_cache (f);
824 #ifdef HAVE_WINDOW_SYSTEM
825 /* Make the image cache. */
826 if (FRAME_WINDOW_P (f))
828 /* We initialize the image cache when creating the first frame
829 on a terminal, and not during terminal creation. This way,
830 `x-open-connection' on a tty won't create an image cache. */
831 if (FRAME_IMAGE_CACHE (f) == NULL)
832 FRAME_IMAGE_CACHE (f) = make_image_cache ();
833 ++FRAME_IMAGE_CACHE (f)->refcount;
835 #endif /* HAVE_WINDOW_SYSTEM */
837 /* Realize basic faces. Must have enough information in frame
838 parameters to realize basic faces at this point. */
839 #ifdef HAVE_X_WINDOWS
840 if (!FRAME_X_P (f) || FRAME_X_WINDOW (f))
841 #endif
842 #ifdef WINDOWSNT
843 if (!FRAME_WINDOW_P (f) || FRAME_W32_WINDOW (f))
844 #endif
845 #ifdef HAVE_NS
846 if (!FRAME_NS_P (f) || FRAME_NS_WINDOW (f))
847 #endif
848 if (!realize_basic_faces (f))
849 abort ();
853 /* Free face cache of frame F. Called from delete_frame. */
855 void
856 free_frame_faces (struct frame *f)
858 struct face_cache *face_cache = FRAME_FACE_CACHE (f);
860 if (face_cache)
862 free_face_cache (face_cache);
863 FRAME_FACE_CACHE (f) = NULL;
866 #ifdef HAVE_WINDOW_SYSTEM
867 if (FRAME_WINDOW_P (f))
869 struct image_cache *image_cache = FRAME_IMAGE_CACHE (f);
870 if (image_cache)
872 --image_cache->refcount;
873 if (image_cache->refcount == 0)
874 free_image_cache (f);
877 #endif /* HAVE_WINDOW_SYSTEM */
881 /* Clear face caches, and recompute basic faces for frame F. Call
882 this after changing frame parameters on which those faces depend,
883 or when realized faces have been freed due to changing attributes
884 of named faces. */
886 void
887 recompute_basic_faces (struct frame *f)
889 if (FRAME_FACE_CACHE (f))
891 clear_face_cache (0);
892 if (!realize_basic_faces (f))
893 abort ();
898 /* Clear the face caches of all frames. CLEAR_FONTS_P non-zero means
899 try to free unused fonts, too. */
901 void
902 clear_face_cache (int clear_fonts_p)
904 #ifdef HAVE_WINDOW_SYSTEM
905 Lisp_Object tail, frame;
906 struct frame *f;
908 if (clear_fonts_p
909 || ++clear_font_table_count == CLEAR_FONT_TABLE_COUNT)
911 #if 0
912 /* Not yet implemented. */
913 clear_font_cache (frame);
914 #endif
916 /* From time to time see if we can unload some fonts. This also
917 frees all realized faces on all frames. Fonts needed by
918 faces will be loaded again when faces are realized again. */
919 clear_font_table_count = 0;
921 FOR_EACH_FRAME (tail, frame)
923 struct frame *f = XFRAME (frame);
924 if (FRAME_WINDOW_P (f)
925 && FRAME_X_DISPLAY_INFO (f)->n_fonts > CLEAR_FONT_TABLE_NFONTS)
926 free_all_realized_faces (frame);
929 else
931 /* Clear GCs of realized faces. */
932 FOR_EACH_FRAME (tail, frame)
934 f = XFRAME (frame);
935 if (FRAME_WINDOW_P (f))
936 clear_face_gcs (FRAME_FACE_CACHE (f));
938 clear_image_caches (Qnil);
940 #endif /* HAVE_WINDOW_SYSTEM */
944 DEFUN ("clear-face-cache", Fclear_face_cache, Sclear_face_cache, 0, 1, 0,
945 doc: /* Clear face caches on all frames.
946 Optional THOROUGHLY non-nil means try to free unused fonts, too. */)
947 (Lisp_Object thoroughly)
949 clear_face_cache (!NILP (thoroughly));
950 ++face_change_count;
951 ++windows_or_buffers_changed;
952 return Qnil;
956 /***********************************************************************
957 X Pixmaps
958 ***********************************************************************/
960 #ifdef HAVE_WINDOW_SYSTEM
962 DEFUN ("bitmap-spec-p", Fbitmap_spec_p, Sbitmap_spec_p, 1, 1, 0,
963 doc: /* Value is non-nil if OBJECT is a valid bitmap specification.
964 A bitmap specification is either a string, a file name, or a list
965 \(WIDTH HEIGHT DATA) where WIDTH is the pixel width of the bitmap,
966 HEIGHT is its height, and DATA is a string containing the bits of
967 the pixmap. Bits are stored row by row, each row occupies
968 \(WIDTH + 7)/8 bytes. */)
969 (Lisp_Object object)
971 int pixmap_p = 0;
973 if (STRINGP (object))
974 /* If OBJECT is a string, it's a file name. */
975 pixmap_p = 1;
976 else if (CONSP (object))
978 /* Otherwise OBJECT must be (WIDTH HEIGHT DATA), WIDTH and
979 HEIGHT must be integers > 0, and DATA must be string large
980 enough to hold a bitmap of the specified size. */
981 Lisp_Object width, height, data;
983 height = width = data = Qnil;
985 if (CONSP (object))
987 width = XCAR (object);
988 object = XCDR (object);
989 if (CONSP (object))
991 height = XCAR (object);
992 object = XCDR (object);
993 if (CONSP (object))
994 data = XCAR (object);
998 if (NATNUMP (width) && NATNUMP (height) && STRINGP (data))
1000 int bytes_per_row = ((XFASTINT (width) + BITS_PER_CHAR - 1)
1001 / BITS_PER_CHAR);
1002 if (SBYTES (data) >= bytes_per_row * XINT (height))
1003 pixmap_p = 1;
1007 return pixmap_p ? Qt : Qnil;
1011 /* Load a bitmap according to NAME (which is either a file name or a
1012 pixmap spec) for use on frame F. Value is the bitmap_id (see
1013 xfns.c). If NAME is nil, return with a bitmap id of zero. If
1014 bitmap cannot be loaded, display a message saying so, and return
1015 zero. Store the bitmap width in *W_PTR and its height in *H_PTR,
1016 if these pointers are not null. */
1018 static int
1019 load_pixmap (FRAME_PTR f, Lisp_Object name, unsigned int *w_ptr, unsigned int *h_ptr)
1021 int bitmap_id;
1023 if (NILP (name))
1024 return 0;
1026 CHECK_TYPE (!NILP (Fbitmap_spec_p (name)), Qbitmap_spec_p, name);
1028 BLOCK_INPUT;
1029 if (CONSP (name))
1031 /* Decode a bitmap spec into a bitmap. */
1033 int h, w;
1034 Lisp_Object bits;
1036 w = XINT (Fcar (name));
1037 h = XINT (Fcar (Fcdr (name)));
1038 bits = Fcar (Fcdr (Fcdr (name)));
1040 bitmap_id = x_create_bitmap_from_data (f, SDATA (bits),
1041 w, h);
1043 else
1045 /* It must be a string -- a file name. */
1046 bitmap_id = x_create_bitmap_from_file (f, name);
1048 UNBLOCK_INPUT;
1050 if (bitmap_id < 0)
1052 add_to_log ("Invalid or undefined bitmap `%s'", name, Qnil);
1053 bitmap_id = 0;
1055 if (w_ptr)
1056 *w_ptr = 0;
1057 if (h_ptr)
1058 *h_ptr = 0;
1060 else
1062 #if GLYPH_DEBUG
1063 ++npixmaps_allocated;
1064 #endif
1065 if (w_ptr)
1066 *w_ptr = x_bitmap_width (f, bitmap_id);
1068 if (h_ptr)
1069 *h_ptr = x_bitmap_height (f, bitmap_id);
1072 return bitmap_id;
1075 #endif /* HAVE_WINDOW_SYSTEM */
1079 /***********************************************************************
1080 X Colors
1081 ***********************************************************************/
1083 /* Parse RGB_LIST, and fill in the RGB fields of COLOR.
1084 RGB_LIST should contain (at least) 3 lisp integers.
1085 Return 0 if there's a problem with RGB_LIST, otherwise return 1. */
1087 static int
1088 parse_rgb_list (Lisp_Object rgb_list, XColor *color)
1090 #define PARSE_RGB_LIST_FIELD(field) \
1091 if (CONSP (rgb_list) && INTEGERP (XCAR (rgb_list))) \
1093 color->field = XINT (XCAR (rgb_list)); \
1094 rgb_list = XCDR (rgb_list); \
1096 else \
1097 return 0;
1099 PARSE_RGB_LIST_FIELD (red);
1100 PARSE_RGB_LIST_FIELD (green);
1101 PARSE_RGB_LIST_FIELD (blue);
1103 return 1;
1107 /* Lookup on frame F the color described by the lisp string COLOR.
1108 The resulting tty color is returned in TTY_COLOR; if STD_COLOR is
1109 non-zero, then the `standard' definition of the same color is
1110 returned in it. */
1112 static int
1113 tty_lookup_color (struct frame *f, Lisp_Object color, XColor *tty_color, XColor *std_color)
1115 Lisp_Object frame, color_desc;
1117 if (!STRINGP (color) || NILP (Ffboundp (Qtty_color_desc)))
1118 return 0;
1120 XSETFRAME (frame, f);
1122 color_desc = call2 (Qtty_color_desc, color, frame);
1123 if (CONSP (color_desc) && CONSP (XCDR (color_desc)))
1125 Lisp_Object rgb;
1127 if (! INTEGERP (XCAR (XCDR (color_desc))))
1128 return 0;
1130 tty_color->pixel = XINT (XCAR (XCDR (color_desc)));
1132 rgb = XCDR (XCDR (color_desc));
1133 if (! parse_rgb_list (rgb, tty_color))
1134 return 0;
1136 /* Should we fill in STD_COLOR too? */
1137 if (std_color)
1139 /* Default STD_COLOR to the same as TTY_COLOR. */
1140 *std_color = *tty_color;
1142 /* Do a quick check to see if the returned descriptor is
1143 actually _exactly_ equal to COLOR, otherwise we have to
1144 lookup STD_COLOR separately. If it's impossible to lookup
1145 a standard color, we just give up and use TTY_COLOR. */
1146 if ((!STRINGP (XCAR (color_desc))
1147 || NILP (Fstring_equal (color, XCAR (color_desc))))
1148 && !NILP (Ffboundp (Qtty_color_standard_values)))
1150 /* Look up STD_COLOR separately. */
1151 rgb = call1 (Qtty_color_standard_values, color);
1152 if (! parse_rgb_list (rgb, std_color))
1153 return 0;
1157 return 1;
1159 else if (NILP (Fsymbol_value (intern ("tty-defined-color-alist"))))
1160 /* We were called early during startup, and the colors are not
1161 yet set up in tty-defined-color-alist. Don't return a failure
1162 indication, since this produces the annoying "Unable to
1163 load color" messages in the *Messages* buffer. */
1164 return 1;
1165 else
1166 /* tty-color-desc seems to have returned a bad value. */
1167 return 0;
1170 /* A version of defined_color for non-X frames. */
1173 tty_defined_color (struct frame *f, const char *color_name,
1174 XColor *color_def, int alloc)
1176 int status = 1;
1178 /* Defaults. */
1179 color_def->pixel = FACE_TTY_DEFAULT_COLOR;
1180 color_def->red = 0;
1181 color_def->blue = 0;
1182 color_def->green = 0;
1184 if (*color_name)
1185 status = tty_lookup_color (f, build_string (color_name), color_def, NULL);
1187 if (color_def->pixel == FACE_TTY_DEFAULT_COLOR && *color_name)
1189 if (strcmp (color_name, "unspecified-fg") == 0)
1190 color_def->pixel = FACE_TTY_DEFAULT_FG_COLOR;
1191 else if (strcmp (color_name, "unspecified-bg") == 0)
1192 color_def->pixel = FACE_TTY_DEFAULT_BG_COLOR;
1195 if (color_def->pixel != FACE_TTY_DEFAULT_COLOR)
1196 status = 1;
1198 return status;
1202 /* Decide if color named COLOR_NAME is valid for the display
1203 associated with the frame F; if so, return the rgb values in
1204 COLOR_DEF. If ALLOC is nonzero, allocate a new colormap cell.
1206 This does the right thing for any type of frame. */
1209 defined_color (struct frame *f, const char *color_name, XColor *color_def, int alloc)
1211 if (!FRAME_WINDOW_P (f))
1212 return tty_defined_color (f, color_name, color_def, alloc);
1213 #ifdef HAVE_X_WINDOWS
1214 else if (FRAME_X_P (f))
1215 return x_defined_color (f, color_name, color_def, alloc);
1216 #endif
1217 #ifdef WINDOWSNT
1218 else if (FRAME_W32_P (f))
1219 return w32_defined_color (f, color_name, color_def, alloc);
1220 #endif
1221 #ifdef HAVE_NS
1222 else if (FRAME_NS_P (f))
1223 return ns_defined_color (f, color_name, color_def, alloc, 1);
1224 #endif
1225 else
1226 abort ();
1230 /* Given the index IDX of a tty color on frame F, return its name, a
1231 Lisp string. */
1233 Lisp_Object
1234 tty_color_name (struct frame *f, int idx)
1236 if (idx >= 0 && !NILP (Ffboundp (Qtty_color_by_index)))
1238 Lisp_Object frame;
1239 Lisp_Object coldesc;
1241 XSETFRAME (frame, f);
1242 coldesc = call2 (Qtty_color_by_index, make_number (idx), frame);
1244 if (!NILP (coldesc))
1245 return XCAR (coldesc);
1247 #ifdef MSDOS
1248 /* We can have an MSDOG frame under -nw for a short window of
1249 opportunity before internal_terminal_init is called. DTRT. */
1250 if (FRAME_MSDOS_P (f) && !inhibit_window_system)
1251 return msdos_stdcolor_name (idx);
1252 #endif
1254 if (idx == FACE_TTY_DEFAULT_FG_COLOR)
1255 return build_string (unspecified_fg);
1256 if (idx == FACE_TTY_DEFAULT_BG_COLOR)
1257 return build_string (unspecified_bg);
1259 return Qunspecified;
1263 /* Return non-zero if COLOR_NAME is a shade of gray (or white or
1264 black) on frame F.
1266 The criterion implemented here is not a terribly sophisticated one. */
1268 static int
1269 face_color_gray_p (struct frame *f, const char *color_name)
1271 XColor color;
1272 int gray_p;
1274 if (defined_color (f, color_name, &color, 0))
1275 gray_p = (/* Any color sufficiently close to black counts as grey. */
1276 (color.red < 5000 && color.green < 5000 && color.blue < 5000)
1278 ((eabs (color.red - color.green)
1279 < max (color.red, color.green) / 20)
1280 && (eabs (color.green - color.blue)
1281 < max (color.green, color.blue) / 20)
1282 && (eabs (color.blue - color.red)
1283 < max (color.blue, color.red) / 20)));
1284 else
1285 gray_p = 0;
1287 return gray_p;
1291 /* Return non-zero if color COLOR_NAME can be displayed on frame F.
1292 BACKGROUND_P non-zero means the color will be used as background
1293 color. */
1295 static int
1296 face_color_supported_p (struct frame *f, const char *color_name, int background_p)
1298 Lisp_Object frame;
1299 XColor not_used;
1301 XSETFRAME (frame, f);
1302 return
1303 #ifdef HAVE_WINDOW_SYSTEM
1304 FRAME_WINDOW_P (f)
1305 ? (!NILP (Fxw_display_color_p (frame))
1306 || xstrcasecmp (color_name, "black") == 0
1307 || xstrcasecmp (color_name, "white") == 0
1308 || (background_p
1309 && face_color_gray_p (f, color_name))
1310 || (!NILP (Fx_display_grayscale_p (frame))
1311 && face_color_gray_p (f, color_name)))
1313 #endif
1314 tty_defined_color (f, color_name, &not_used, 0);
1318 DEFUN ("color-gray-p", Fcolor_gray_p, Scolor_gray_p, 1, 2, 0,
1319 doc: /* Return non-nil if COLOR is a shade of gray (or white or black).
1320 FRAME specifies the frame and thus the display for interpreting COLOR.
1321 If FRAME is nil or omitted, use the selected frame. */)
1322 (Lisp_Object color, Lisp_Object frame)
1324 struct frame *f;
1326 CHECK_STRING (color);
1327 if (NILP (frame))
1328 frame = selected_frame;
1329 else
1330 CHECK_FRAME (frame);
1331 f = XFRAME (frame);
1332 return face_color_gray_p (f, SDATA (color)) ? Qt : Qnil;
1336 DEFUN ("color-supported-p", Fcolor_supported_p,
1337 Scolor_supported_p, 1, 3, 0,
1338 doc: /* Return non-nil if COLOR can be displayed on FRAME.
1339 BACKGROUND-P non-nil means COLOR is used as a background.
1340 Otherwise, this function tells whether it can be used as a foreground.
1341 If FRAME is nil or omitted, use the selected frame.
1342 COLOR must be a valid color name. */)
1343 (Lisp_Object color, Lisp_Object frame, Lisp_Object background_p)
1345 struct frame *f;
1347 CHECK_STRING (color);
1348 if (NILP (frame))
1349 frame = selected_frame;
1350 else
1351 CHECK_FRAME (frame);
1352 f = XFRAME (frame);
1353 if (face_color_supported_p (f, SDATA (color), !NILP (background_p)))
1354 return Qt;
1355 return Qnil;
1359 /* Load color with name NAME for use by face FACE on frame F.
1360 TARGET_INDEX must be one of LFACE_FOREGROUND_INDEX,
1361 LFACE_BACKGROUND_INDEX, LFACE_UNDERLINE_INDEX, LFACE_OVERLINE_INDEX,
1362 LFACE_STRIKE_THROUGH_INDEX, or LFACE_BOX_INDEX. Value is the
1363 pixel color. If color cannot be loaded, display a message, and
1364 return the foreground, background or underline color of F, but
1365 record that fact in flags of the face so that we don't try to free
1366 these colors. */
1368 unsigned long
1369 load_color (struct frame *f, struct face *face, Lisp_Object name, enum lface_attribute_index target_index)
1371 XColor color;
1373 xassert (STRINGP (name));
1374 xassert (target_index == LFACE_FOREGROUND_INDEX
1375 || target_index == LFACE_BACKGROUND_INDEX
1376 || target_index == LFACE_UNDERLINE_INDEX
1377 || target_index == LFACE_OVERLINE_INDEX
1378 || target_index == LFACE_STRIKE_THROUGH_INDEX
1379 || target_index == LFACE_BOX_INDEX);
1381 /* if the color map is full, defined_color will return a best match
1382 to the values in an existing cell. */
1383 if (!defined_color (f, SDATA (name), &color, 1))
1385 add_to_log ("Unable to load color \"%s\"", name, Qnil);
1387 switch (target_index)
1389 case LFACE_FOREGROUND_INDEX:
1390 face->foreground_defaulted_p = 1;
1391 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1392 break;
1394 case LFACE_BACKGROUND_INDEX:
1395 face->background_defaulted_p = 1;
1396 color.pixel = FRAME_BACKGROUND_PIXEL (f);
1397 break;
1399 case LFACE_UNDERLINE_INDEX:
1400 face->underline_defaulted_p = 1;
1401 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1402 break;
1404 case LFACE_OVERLINE_INDEX:
1405 face->overline_color_defaulted_p = 1;
1406 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1407 break;
1409 case LFACE_STRIKE_THROUGH_INDEX:
1410 face->strike_through_color_defaulted_p = 1;
1411 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1412 break;
1414 case LFACE_BOX_INDEX:
1415 face->box_color_defaulted_p = 1;
1416 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1417 break;
1419 default:
1420 abort ();
1423 #if GLYPH_DEBUG
1424 else
1425 ++ncolors_allocated;
1426 #endif
1428 return color.pixel;
1432 #ifdef HAVE_WINDOW_SYSTEM
1434 /* Load colors for face FACE which is used on frame F. Colors are
1435 specified by slots LFACE_BACKGROUND_INDEX and LFACE_FOREGROUND_INDEX
1436 of ATTRS. If the background color specified is not supported on F,
1437 try to emulate gray colors with a stipple from Vface_default_stipple. */
1439 static void
1440 load_face_colors (struct frame *f, struct face *face, Lisp_Object *attrs)
1442 Lisp_Object fg, bg;
1444 bg = attrs[LFACE_BACKGROUND_INDEX];
1445 fg = attrs[LFACE_FOREGROUND_INDEX];
1447 /* Swap colors if face is inverse-video. */
1448 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1450 Lisp_Object tmp;
1451 tmp = fg;
1452 fg = bg;
1453 bg = tmp;
1456 /* Check for support for foreground, not for background because
1457 face_color_supported_p is smart enough to know that grays are
1458 "supported" as background because we are supposed to use stipple
1459 for them. */
1460 if (!face_color_supported_p (f, SDATA (bg), 0)
1461 && !NILP (Fbitmap_spec_p (Vface_default_stipple)))
1463 x_destroy_bitmap (f, face->stipple);
1464 face->stipple = load_pixmap (f, Vface_default_stipple,
1465 &face->pixmap_w, &face->pixmap_h);
1468 face->background = load_color (f, face, bg, LFACE_BACKGROUND_INDEX);
1469 face->foreground = load_color (f, face, fg, LFACE_FOREGROUND_INDEX);
1473 /* Free color PIXEL on frame F. */
1475 void
1476 unload_color (struct frame *f, long unsigned int pixel)
1478 #ifdef HAVE_X_WINDOWS
1479 if (pixel != -1)
1481 BLOCK_INPUT;
1482 x_free_colors (f, &pixel, 1);
1483 UNBLOCK_INPUT;
1485 #endif
1489 /* Free colors allocated for FACE. */
1491 static void
1492 free_face_colors (struct frame *f, struct face *face)
1494 /* PENDING(NS): need to do something here? */
1495 #ifdef HAVE_X_WINDOWS
1496 if (face->colors_copied_bitwise_p)
1497 return;
1499 BLOCK_INPUT;
1501 if (!face->foreground_defaulted_p)
1503 x_free_colors (f, &face->foreground, 1);
1504 IF_DEBUG (--ncolors_allocated);
1507 if (!face->background_defaulted_p)
1509 x_free_colors (f, &face->background, 1);
1510 IF_DEBUG (--ncolors_allocated);
1513 if (face->underline_p
1514 && !face->underline_defaulted_p)
1516 x_free_colors (f, &face->underline_color, 1);
1517 IF_DEBUG (--ncolors_allocated);
1520 if (face->overline_p
1521 && !face->overline_color_defaulted_p)
1523 x_free_colors (f, &face->overline_color, 1);
1524 IF_DEBUG (--ncolors_allocated);
1527 if (face->strike_through_p
1528 && !face->strike_through_color_defaulted_p)
1530 x_free_colors (f, &face->strike_through_color, 1);
1531 IF_DEBUG (--ncolors_allocated);
1534 if (face->box != FACE_NO_BOX
1535 && !face->box_color_defaulted_p)
1537 x_free_colors (f, &face->box_color, 1);
1538 IF_DEBUG (--ncolors_allocated);
1541 UNBLOCK_INPUT;
1542 #endif /* HAVE_X_WINDOWS */
1545 #endif /* HAVE_WINDOW_SYSTEM */
1549 /***********************************************************************
1550 XLFD Font Names
1551 ***********************************************************************/
1553 /* An enumerator for each field of an XLFD font name. */
1555 enum xlfd_field
1557 XLFD_FOUNDRY,
1558 XLFD_FAMILY,
1559 XLFD_WEIGHT,
1560 XLFD_SLANT,
1561 XLFD_SWIDTH,
1562 XLFD_ADSTYLE,
1563 XLFD_PIXEL_SIZE,
1564 XLFD_POINT_SIZE,
1565 XLFD_RESX,
1566 XLFD_RESY,
1567 XLFD_SPACING,
1568 XLFD_AVGWIDTH,
1569 XLFD_REGISTRY,
1570 XLFD_ENCODING,
1571 XLFD_LAST
1574 /* An enumerator for each possible slant value of a font. Taken from
1575 the XLFD specification. */
1577 enum xlfd_slant
1579 XLFD_SLANT_UNKNOWN,
1580 XLFD_SLANT_ROMAN,
1581 XLFD_SLANT_ITALIC,
1582 XLFD_SLANT_OBLIQUE,
1583 XLFD_SLANT_REVERSE_ITALIC,
1584 XLFD_SLANT_REVERSE_OBLIQUE,
1585 XLFD_SLANT_OTHER
1588 /* Relative font weight according to XLFD documentation. */
1590 enum xlfd_weight
1592 XLFD_WEIGHT_UNKNOWN,
1593 XLFD_WEIGHT_ULTRA_LIGHT, /* 10 */
1594 XLFD_WEIGHT_EXTRA_LIGHT, /* 20 */
1595 XLFD_WEIGHT_LIGHT, /* 30 */
1596 XLFD_WEIGHT_SEMI_LIGHT, /* 40: SemiLight, Book, ... */
1597 XLFD_WEIGHT_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1598 XLFD_WEIGHT_SEMI_BOLD, /* 60: SemiBold, DemiBold, ... */
1599 XLFD_WEIGHT_BOLD, /* 70: Bold, ... */
1600 XLFD_WEIGHT_EXTRA_BOLD, /* 80: ExtraBold, Heavy, ... */
1601 XLFD_WEIGHT_ULTRA_BOLD /* 90: UltraBold, Black, ... */
1604 /* Relative proportionate width. */
1606 enum xlfd_swidth
1608 XLFD_SWIDTH_UNKNOWN,
1609 XLFD_SWIDTH_ULTRA_CONDENSED, /* 10 */
1610 XLFD_SWIDTH_EXTRA_CONDENSED, /* 20 */
1611 XLFD_SWIDTH_CONDENSED, /* 30: Condensed, Narrow, Compressed, ... */
1612 XLFD_SWIDTH_SEMI_CONDENSED, /* 40: semicondensed */
1613 XLFD_SWIDTH_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1614 XLFD_SWIDTH_SEMI_EXPANDED, /* 60: SemiExpanded, DemiExpanded, ... */
1615 XLFD_SWIDTH_EXPANDED, /* 70: Expanded... */
1616 XLFD_SWIDTH_EXTRA_EXPANDED, /* 80: ExtraExpanded, Wide... */
1617 XLFD_SWIDTH_ULTRA_EXPANDED /* 90: UltraExpanded... */
1620 /* Order by which font selection chooses fonts. The default values
1621 mean `first, find a best match for the font width, then for the
1622 font height, then for weight, then for slant.' This variable can be
1623 set via set-face-font-sort-order. */
1625 static int font_sort_order[4];
1627 #ifdef HAVE_WINDOW_SYSTEM
1629 static enum font_property_index font_props_for_sorting[FONT_SIZE_INDEX];
1631 static int
1632 compare_fonts_by_sort_order (const void *v1, const void *v2)
1634 Lisp_Object font1 = *(Lisp_Object *) v1;
1635 Lisp_Object font2 = *(Lisp_Object *) v2;
1636 int i;
1638 for (i = 0; i < FONT_SIZE_INDEX; i++)
1640 enum font_property_index idx = font_props_for_sorting[i];
1641 Lisp_Object val1 = AREF (font1, idx), val2 = AREF (font2, idx);
1642 int result;
1644 if (idx <= FONT_REGISTRY_INDEX)
1646 if (STRINGP (val1))
1647 result = STRINGP (val2) ? strcmp (SDATA (val1), SDATA (val2)) : -1;
1648 else
1649 result = STRINGP (val2) ? 1 : 0;
1651 else
1653 if (INTEGERP (val1))
1654 result = INTEGERP (val2) ? XINT (val1) - XINT (val2) : -1;
1655 else
1656 result = INTEGERP (val2) ? 1 : 0;
1658 if (result)
1659 return result;
1661 return 0;
1664 DEFUN ("x-family-fonts", Fx_family_fonts, Sx_family_fonts, 0, 2, 0,
1665 doc: /* Return a list of available fonts of family FAMILY on FRAME.
1666 If FAMILY is omitted or nil, list all families.
1667 Otherwise, FAMILY must be a string, possibly containing wildcards
1668 `?' and `*'.
1669 If FRAME is omitted or nil, use the selected frame.
1670 Each element of the result is a vector [FAMILY WIDTH POINT-SIZE WEIGHT
1671 SLANT FIXED-P FULL REGISTRY-AND-ENCODING].
1672 FAMILY is the font family name. POINT-SIZE is the size of the
1673 font in 1/10 pt. WIDTH, WEIGHT, and SLANT are symbols describing the
1674 width, weight and slant of the font. These symbols are the same as for
1675 face attributes. FIXED-P is non-nil if the font is fixed-pitch.
1676 FULL is the full name of the font, and REGISTRY-AND-ENCODING is a string
1677 giving the registry and encoding of the font.
1678 The result list is sorted according to the current setting of
1679 the face font sort order. */)
1680 (Lisp_Object family, Lisp_Object frame)
1682 Lisp_Object font_spec, list, *drivers, vec;
1683 int i, nfonts, ndrivers;
1684 Lisp_Object result;
1686 if (NILP (frame))
1687 frame = selected_frame;
1688 CHECK_LIVE_FRAME (frame);
1690 font_spec = Ffont_spec (0, NULL);
1691 if (!NILP (family))
1693 CHECK_STRING (family);
1694 font_parse_family_registry (family, Qnil, font_spec);
1697 list = font_list_entities (frame, font_spec);
1698 if (NILP (list))
1699 return Qnil;
1701 /* Sort the font entities. */
1702 for (i = 0; i < 4; i++)
1703 switch (font_sort_order[i])
1705 case XLFD_SWIDTH:
1706 font_props_for_sorting[i] = FONT_WIDTH_INDEX; break;
1707 case XLFD_POINT_SIZE:
1708 font_props_for_sorting[i] = FONT_SIZE_INDEX; break;
1709 case XLFD_WEIGHT:
1710 font_props_for_sorting[i] = FONT_WEIGHT_INDEX; break;
1711 default:
1712 font_props_for_sorting[i] = FONT_SLANT_INDEX; break;
1714 font_props_for_sorting[i++] = FONT_FAMILY_INDEX;
1715 font_props_for_sorting[i++] = FONT_FOUNDRY_INDEX;
1716 font_props_for_sorting[i++] = FONT_ADSTYLE_INDEX;
1717 font_props_for_sorting[i++] = FONT_REGISTRY_INDEX;
1719 ndrivers = XINT (Flength (list));
1720 drivers = alloca (sizeof (Lisp_Object) * ndrivers);
1721 for (i = 0; i < ndrivers; i++, list = XCDR (list))
1722 drivers[i] = XCAR (list);
1723 vec = Fvconcat (ndrivers, drivers);
1724 nfonts = ASIZE (vec);
1726 qsort (XVECTOR (vec)->contents, nfonts, sizeof (Lisp_Object),
1727 compare_fonts_by_sort_order);
1729 result = Qnil;
1730 for (i = nfonts - 1; i >= 0; --i)
1732 Lisp_Object font = AREF (vec, i);
1733 Lisp_Object v = Fmake_vector (make_number (8), Qnil);
1734 int point;
1735 Lisp_Object spacing;
1737 ASET (v, 0, AREF (font, FONT_FAMILY_INDEX));
1738 ASET (v, 1, FONT_WIDTH_SYMBOLIC (font));
1739 point = PIXEL_TO_POINT (XINT (AREF (font, FONT_SIZE_INDEX)) * 10,
1740 XFRAME (frame)->resy);
1741 ASET (v, 2, make_number (point));
1742 ASET (v, 3, FONT_WEIGHT_SYMBOLIC (font));
1743 ASET (v, 4, FONT_SLANT_SYMBOLIC (font));
1744 spacing = Ffont_get (font, QCspacing);
1745 ASET (v, 5, (NILP (spacing) || EQ (spacing, Qp)) ? Qnil : Qt);
1746 ASET (v, 6, Ffont_xlfd_name (font, Qnil));
1747 ASET (v, 7, AREF (font, FONT_REGISTRY_INDEX));
1749 result = Fcons (v, result);
1752 return result;
1755 DEFUN ("x-list-fonts", Fx_list_fonts, Sx_list_fonts, 1, 5, 0,
1756 doc: /* Return a list of the names of available fonts matching PATTERN.
1757 If optional arguments FACE and FRAME are specified, return only fonts
1758 the same size as FACE on FRAME.
1760 PATTERN should be a string containing a font name in the XLFD,
1761 Fontconfig, or GTK format. A font name given in the XLFD format may
1762 contain wildcard characters:
1763 the * character matches any substring, and
1764 the ? character matches any single character.
1765 PATTERN is case-insensitive.
1767 The return value is a list of strings, suitable as arguments to
1768 `set-face-font'.
1770 Fonts Emacs can't use may or may not be excluded
1771 even if they match PATTERN and FACE.
1772 The optional fourth argument MAXIMUM sets a limit on how many
1773 fonts to match. The first MAXIMUM fonts are reported.
1774 The optional fifth argument WIDTH, if specified, is a number of columns
1775 occupied by a character of a font. In that case, return only fonts
1776 the WIDTH times as wide as FACE on FRAME. */)
1777 (Lisp_Object pattern, Lisp_Object face, Lisp_Object frame, Lisp_Object maximum, Lisp_Object width)
1779 struct frame *f;
1780 int size, avgwidth;
1782 check_x ();
1783 CHECK_STRING (pattern);
1785 if (! NILP (maximum))
1786 CHECK_NATNUM (maximum);
1788 if (!NILP (width))
1789 CHECK_NUMBER (width);
1791 /* We can't simply call check_x_frame because this function may be
1792 called before any frame is created. */
1793 if (NILP (frame))
1794 frame = selected_frame;
1795 f = frame_or_selected_frame (frame, 2);
1796 if (! FRAME_WINDOW_P (f))
1798 /* Perhaps we have not yet created any frame. */
1799 f = NULL;
1800 frame = Qnil;
1801 face = Qnil;
1804 /* Determine the width standard for comparison with the fonts we find. */
1806 if (NILP (face))
1807 size = 0;
1808 else
1810 /* This is of limited utility since it works with character
1811 widths. Keep it for compatibility. --gerd. */
1812 int face_id = lookup_named_face (f, face, 0);
1813 struct face *face = (face_id < 0
1814 ? NULL
1815 : FACE_FROM_ID (f, face_id));
1817 if (face && face->font)
1819 size = face->font->pixel_size;
1820 avgwidth = face->font->average_width;
1822 else
1824 size = FRAME_FONT (f)->pixel_size;
1825 avgwidth = FRAME_FONT (f)->average_width;
1827 if (!NILP (width))
1828 avgwidth *= XINT (width);
1832 Lisp_Object font_spec;
1833 Lisp_Object args[2], tail;
1835 font_spec = font_spec_from_name (pattern);
1836 if (!FONTP (font_spec))
1837 signal_error ("Invalid font name", pattern);
1839 if (size)
1841 Ffont_put (font_spec, QCsize, make_number (size));
1842 Ffont_put (font_spec, QCavgwidth, make_number (avgwidth));
1844 args[0] = Flist_fonts (font_spec, frame, maximum, font_spec);
1845 for (tail = args[0]; CONSP (tail); tail = XCDR (tail))
1847 Lisp_Object font_entity;
1849 font_entity = XCAR (tail);
1850 if ((NILP (AREF (font_entity, FONT_SIZE_INDEX))
1851 || XINT (AREF (font_entity, FONT_SIZE_INDEX)) == 0)
1852 && ! NILP (AREF (font_spec, FONT_SIZE_INDEX)))
1854 /* This is a scalable font. For backward compatibility,
1855 we set the specified size. */
1856 font_entity = Fcopy_font_spec (font_entity);
1857 ASET (font_entity, FONT_SIZE_INDEX,
1858 AREF (font_spec, FONT_SIZE_INDEX));
1860 XSETCAR (tail, Ffont_xlfd_name (font_entity, Qnil));
1862 if (NILP (frame))
1863 /* We don't have to check fontsets. */
1864 return args[0];
1865 args[1] = list_fontsets (f, pattern, size);
1866 return Fnconc (2, args);
1870 #endif /* HAVE_WINDOW_SYSTEM */
1873 /***********************************************************************
1874 Lisp Faces
1875 ***********************************************************************/
1877 /* Access face attributes of face LFACE, a Lisp vector. */
1879 #define LFACE_FAMILY(LFACE) AREF ((LFACE), LFACE_FAMILY_INDEX)
1880 #define LFACE_FOUNDRY(LFACE) AREF ((LFACE), LFACE_FOUNDRY_INDEX)
1881 #define LFACE_HEIGHT(LFACE) AREF ((LFACE), LFACE_HEIGHT_INDEX)
1882 #define LFACE_WEIGHT(LFACE) AREF ((LFACE), LFACE_WEIGHT_INDEX)
1883 #define LFACE_SLANT(LFACE) AREF ((LFACE), LFACE_SLANT_INDEX)
1884 #define LFACE_UNDERLINE(LFACE) AREF ((LFACE), LFACE_UNDERLINE_INDEX)
1885 #define LFACE_INVERSE(LFACE) AREF ((LFACE), LFACE_INVERSE_INDEX)
1886 #define LFACE_FOREGROUND(LFACE) AREF ((LFACE), LFACE_FOREGROUND_INDEX)
1887 #define LFACE_BACKGROUND(LFACE) AREF ((LFACE), LFACE_BACKGROUND_INDEX)
1888 #define LFACE_STIPPLE(LFACE) AREF ((LFACE), LFACE_STIPPLE_INDEX)
1889 #define LFACE_SWIDTH(LFACE) AREF ((LFACE), LFACE_SWIDTH_INDEX)
1890 #define LFACE_OVERLINE(LFACE) AREF ((LFACE), LFACE_OVERLINE_INDEX)
1891 #define LFACE_STRIKE_THROUGH(LFACE) AREF ((LFACE), LFACE_STRIKE_THROUGH_INDEX)
1892 #define LFACE_BOX(LFACE) AREF ((LFACE), LFACE_BOX_INDEX)
1893 #define LFACE_FONT(LFACE) AREF ((LFACE), LFACE_FONT_INDEX)
1894 #define LFACE_INHERIT(LFACE) AREF ((LFACE), LFACE_INHERIT_INDEX)
1895 #define LFACE_FONTSET(LFACE) AREF ((LFACE), LFACE_FONTSET_INDEX)
1897 /* Non-zero if LFACE is a Lisp face. A Lisp face is a vector of size
1898 LFACE_VECTOR_SIZE which has the symbol `face' in slot 0. */
1900 #define LFACEP(LFACE) \
1901 (VECTORP (LFACE) \
1902 && XVECTOR (LFACE)->size == LFACE_VECTOR_SIZE \
1903 && EQ (AREF (LFACE, 0), Qface))
1906 #if GLYPH_DEBUG
1908 /* Check consistency of Lisp face attribute vector ATTRS. */
1910 static void
1911 check_lface_attrs (attrs)
1912 Lisp_Object *attrs;
1914 xassert (UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
1915 || IGNORE_DEFFACE_P (attrs[LFACE_FAMILY_INDEX])
1916 || STRINGP (attrs[LFACE_FAMILY_INDEX]));
1917 xassert (UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
1918 || IGNORE_DEFFACE_P (attrs[LFACE_FOUNDRY_INDEX])
1919 || STRINGP (attrs[LFACE_FOUNDRY_INDEX]));
1920 xassert (UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
1921 || IGNORE_DEFFACE_P (attrs[LFACE_SWIDTH_INDEX])
1922 || SYMBOLP (attrs[LFACE_SWIDTH_INDEX]));
1923 xassert (UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
1924 || IGNORE_DEFFACE_P (attrs[LFACE_HEIGHT_INDEX])
1925 || INTEGERP (attrs[LFACE_HEIGHT_INDEX])
1926 || FLOATP (attrs[LFACE_HEIGHT_INDEX])
1927 || FUNCTIONP (attrs[LFACE_HEIGHT_INDEX]));
1928 xassert (UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
1929 || IGNORE_DEFFACE_P (attrs[LFACE_WEIGHT_INDEX])
1930 || SYMBOLP (attrs[LFACE_WEIGHT_INDEX]));
1931 xassert (UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
1932 || IGNORE_DEFFACE_P (attrs[LFACE_SLANT_INDEX])
1933 || SYMBOLP (attrs[LFACE_SLANT_INDEX]));
1934 xassert (UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
1935 || IGNORE_DEFFACE_P (attrs[LFACE_UNDERLINE_INDEX])
1936 || SYMBOLP (attrs[LFACE_UNDERLINE_INDEX])
1937 || STRINGP (attrs[LFACE_UNDERLINE_INDEX]));
1938 xassert (UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
1939 || IGNORE_DEFFACE_P (attrs[LFACE_OVERLINE_INDEX])
1940 || SYMBOLP (attrs[LFACE_OVERLINE_INDEX])
1941 || STRINGP (attrs[LFACE_OVERLINE_INDEX]));
1942 xassert (UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1943 || IGNORE_DEFFACE_P (attrs[LFACE_STRIKE_THROUGH_INDEX])
1944 || SYMBOLP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1945 || STRINGP (attrs[LFACE_STRIKE_THROUGH_INDEX]));
1946 xassert (UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
1947 || IGNORE_DEFFACE_P (attrs[LFACE_BOX_INDEX])
1948 || SYMBOLP (attrs[LFACE_BOX_INDEX])
1949 || STRINGP (attrs[LFACE_BOX_INDEX])
1950 || INTEGERP (attrs[LFACE_BOX_INDEX])
1951 || CONSP (attrs[LFACE_BOX_INDEX]));
1952 xassert (UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
1953 || IGNORE_DEFFACE_P (attrs[LFACE_INVERSE_INDEX])
1954 || SYMBOLP (attrs[LFACE_INVERSE_INDEX]));
1955 xassert (UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
1956 || IGNORE_DEFFACE_P (attrs[LFACE_FOREGROUND_INDEX])
1957 || STRINGP (attrs[LFACE_FOREGROUND_INDEX]));
1958 xassert (UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
1959 || IGNORE_DEFFACE_P (attrs[LFACE_BACKGROUND_INDEX])
1960 || STRINGP (attrs[LFACE_BACKGROUND_INDEX]));
1961 xassert (UNSPECIFIEDP (attrs[LFACE_INHERIT_INDEX])
1962 || IGNORE_DEFFACE_P (attrs[LFACE_INHERIT_INDEX])
1963 || NILP (attrs[LFACE_INHERIT_INDEX])
1964 || SYMBOLP (attrs[LFACE_INHERIT_INDEX])
1965 || CONSP (attrs[LFACE_INHERIT_INDEX]));
1966 #ifdef HAVE_WINDOW_SYSTEM
1967 xassert (UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
1968 || IGNORE_DEFFACE_P (attrs[LFACE_STIPPLE_INDEX])
1969 || SYMBOLP (attrs[LFACE_STIPPLE_INDEX])
1970 || !NILP (Fbitmap_spec_p (attrs[LFACE_STIPPLE_INDEX])));
1971 xassert (UNSPECIFIEDP (attrs[LFACE_FONT_INDEX])
1972 || IGNORE_DEFFACE_P (attrs[LFACE_FONT_INDEX])
1973 || FONTP (attrs[LFACE_FONT_INDEX]));
1974 xassert (UNSPECIFIEDP (attrs[LFACE_FONTSET_INDEX])
1975 || STRINGP (attrs[LFACE_FONTSET_INDEX]));
1976 #endif
1980 /* Check consistency of attributes of Lisp face LFACE (a Lisp vector). */
1982 static void
1983 check_lface (lface)
1984 Lisp_Object lface;
1986 if (!NILP (lface))
1988 xassert (LFACEP (lface));
1989 check_lface_attrs (XVECTOR (lface)->contents);
1993 #else /* GLYPH_DEBUG == 0 */
1995 #define check_lface_attrs(attrs) (void) 0
1996 #define check_lface(lface) (void) 0
1998 #endif /* GLYPH_DEBUG == 0 */
2002 /* Face-merge cycle checking. */
2004 enum named_merge_point_kind
2006 NAMED_MERGE_POINT_NORMAL,
2007 NAMED_MERGE_POINT_REMAP
2010 /* A `named merge point' is simply a point during face-merging where we
2011 look up a face by name. We keep a stack of which named lookups we're
2012 currently processing so that we can easily detect cycles, using a
2013 linked- list of struct named_merge_point structures, typically
2014 allocated on the stack frame of the named lookup functions which are
2015 active (so no consing is required). */
2016 struct named_merge_point
2018 Lisp_Object face_name;
2019 enum named_merge_point_kind named_merge_point_kind;
2020 struct named_merge_point *prev;
2024 /* If a face merging cycle is detected for FACE_NAME, return 0,
2025 otherwise add NEW_NAMED_MERGE_POINT, which is initialized using
2026 FACE_NAME and NAMED_MERGE_POINT_KIND, as the head of the linked list
2027 pointed to by NAMED_MERGE_POINTS, and return 1. */
2029 static INLINE int
2030 push_named_merge_point (struct named_merge_point *new_named_merge_point,
2031 Lisp_Object face_name,
2032 enum named_merge_point_kind named_merge_point_kind,
2033 struct named_merge_point **named_merge_points)
2035 struct named_merge_point *prev;
2037 for (prev = *named_merge_points; prev; prev = prev->prev)
2038 if (EQ (face_name, prev->face_name))
2040 if (prev->named_merge_point_kind == named_merge_point_kind)
2041 /* A cycle, so fail. */
2042 return 0;
2043 else if (prev->named_merge_point_kind == NAMED_MERGE_POINT_REMAP)
2044 /* A remap `hides ' any previous normal merge points
2045 (because the remap means that it's actually different face),
2046 so as we know the current merge point must be normal, we
2047 can just assume it's OK. */
2048 break;
2051 new_named_merge_point->face_name = face_name;
2052 new_named_merge_point->named_merge_point_kind = named_merge_point_kind;
2053 new_named_merge_point->prev = *named_merge_points;
2055 *named_merge_points = new_named_merge_point;
2057 return 1;
2062 #if 0 /* Seems to be unused. */
2063 static Lisp_Object
2064 internal_resolve_face_name (nargs, args)
2065 int nargs;
2066 Lisp_Object *args;
2068 return Fget (args[0], args[1]);
2071 static Lisp_Object
2072 resolve_face_name_error (ignore)
2073 Lisp_Object ignore;
2075 return Qnil;
2077 #endif
2079 /* Resolve face name FACE_NAME. If FACE_NAME is a string, intern it
2080 to make it a symbol. If FACE_NAME is an alias for another face,
2081 return that face's name.
2083 Return default face in case of errors. */
2085 static Lisp_Object
2086 resolve_face_name (Lisp_Object face_name, int signal_p)
2088 Lisp_Object orig_face;
2089 Lisp_Object tortoise, hare;
2091 if (STRINGP (face_name))
2092 face_name = intern (SDATA (face_name));
2094 if (NILP (face_name) || !SYMBOLP (face_name))
2095 return face_name;
2097 orig_face = face_name;
2098 tortoise = hare = face_name;
2100 while (1)
2102 face_name = hare;
2103 hare = Fget (hare, Qface_alias);
2104 if (NILP (hare) || !SYMBOLP (hare))
2105 break;
2107 face_name = hare;
2108 hare = Fget (hare, Qface_alias);
2109 if (NILP (hare) || !SYMBOLP (hare))
2110 break;
2112 tortoise = Fget (tortoise, Qface_alias);
2113 if (EQ (hare, tortoise))
2115 if (signal_p)
2116 xsignal1 (Qcircular_list, orig_face);
2117 return Qdefault;
2121 return face_name;
2125 /* Return the face definition of FACE_NAME on frame F. F null means
2126 return the definition for new frames. FACE_NAME may be a string or
2127 a symbol (apparently Emacs 20.2 allowed strings as face names in
2128 face text properties; Ediff uses that). If SIGNAL_P is non-zero,
2129 signal an error if FACE_NAME is not a valid face name. If SIGNAL_P
2130 is zero, value is nil if FACE_NAME is not a valid face name. */
2131 static INLINE Lisp_Object
2132 lface_from_face_name_no_resolve (struct frame *f, Lisp_Object face_name, int signal_p)
2134 Lisp_Object lface;
2136 if (f)
2137 lface = assq_no_quit (face_name, f->face_alist);
2138 else
2139 lface = assq_no_quit (face_name, Vface_new_frame_defaults);
2141 if (CONSP (lface))
2142 lface = XCDR (lface);
2143 else if (signal_p)
2144 signal_error ("Invalid face", face_name);
2146 check_lface (lface);
2148 return lface;
2151 /* Return the face definition of FACE_NAME on frame F. F null means
2152 return the definition for new frames. FACE_NAME may be a string or
2153 a symbol (apparently Emacs 20.2 allowed strings as face names in
2154 face text properties; Ediff uses that). If FACE_NAME is an alias
2155 for another face, return that face's definition. If SIGNAL_P is
2156 non-zero, signal an error if FACE_NAME is not a valid face name.
2157 If SIGNAL_P is zero, value is nil if FACE_NAME is not a valid face
2158 name. */
2159 static INLINE Lisp_Object
2160 lface_from_face_name (struct frame *f, Lisp_Object face_name, int signal_p)
2162 face_name = resolve_face_name (face_name, signal_p);
2163 return lface_from_face_name_no_resolve (f, face_name, signal_p);
2167 /* Get face attributes of face FACE_NAME from frame-local faces on
2168 frame F. Store the resulting attributes in ATTRS which must point
2169 to a vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If SIGNAL_P
2170 is non-zero, signal an error if FACE_NAME does not name a face.
2171 Otherwise, value is zero if FACE_NAME is not a face. */
2173 static INLINE int
2174 get_lface_attributes_no_remap (struct frame *f, Lisp_Object face_name, Lisp_Object *attrs, int signal_p)
2176 Lisp_Object lface;
2178 lface = lface_from_face_name_no_resolve (f, face_name, signal_p);
2180 if (! NILP (lface))
2181 memcpy (attrs, XVECTOR (lface)->contents,
2182 LFACE_VECTOR_SIZE * sizeof *attrs);
2184 return !NILP (lface);
2187 /* Get face attributes of face FACE_NAME from frame-local faces on frame
2188 F. Store the resulting attributes in ATTRS which must point to a
2189 vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If FACE_NAME is an
2190 alias for another face, use that face's definition. If SIGNAL_P is
2191 non-zero, signal an error if FACE_NAME does not name a face.
2192 Otherwise, value is zero if FACE_NAME is not a face. */
2194 static INLINE int
2195 get_lface_attributes (struct frame *f, Lisp_Object face_name, Lisp_Object *attrs, int signal_p, struct named_merge_point *named_merge_points)
2197 Lisp_Object face_remapping;
2199 face_name = resolve_face_name (face_name, signal_p);
2201 /* See if SYMBOL has been remapped to some other face (usually this
2202 is done buffer-locally). */
2203 face_remapping = assq_no_quit (face_name, Vface_remapping_alist);
2204 if (CONSP (face_remapping))
2206 struct named_merge_point named_merge_point;
2208 if (push_named_merge_point (&named_merge_point,
2209 face_name, NAMED_MERGE_POINT_REMAP,
2210 &named_merge_points))
2212 int i;
2214 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2215 attrs[i] = Qunspecified;
2217 return merge_face_ref (f, XCDR (face_remapping), attrs,
2218 signal_p, named_merge_points);
2222 /* Default case, no remapping. */
2223 return get_lface_attributes_no_remap (f, face_name, attrs, signal_p);
2227 /* Non-zero if all attributes in face attribute vector ATTRS are
2228 specified, i.e. are non-nil. */
2230 static int
2231 lface_fully_specified_p (Lisp_Object *attrs)
2233 int i;
2235 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2236 if (i != LFACE_FONT_INDEX && i != LFACE_INHERIT_INDEX)
2237 if ((UNSPECIFIEDP (attrs[i]) || IGNORE_DEFFACE_P (attrs[i])))
2238 break;
2240 return i == LFACE_VECTOR_SIZE;
2243 #ifdef HAVE_WINDOW_SYSTEM
2245 /* Set font-related attributes of Lisp face LFACE from FONT-OBJECT.
2246 If FORCE_P is zero, set only unspecified attributes of LFACE. The
2247 exception is `font' attribute. It is set to FONT_OBJECT regardless
2248 of FORCE_P. */
2250 static int
2251 set_lface_from_font (struct frame *f, Lisp_Object lface, Lisp_Object font_object, int force_p)
2253 Lisp_Object val;
2254 struct font *font = XFONT_OBJECT (font_object);
2256 /* Set attributes only if unspecified, otherwise face defaults for
2257 new frames would never take effect. If the font doesn't have a
2258 specific property, set a normal value for that. */
2260 if (force_p || UNSPECIFIEDP (LFACE_FAMILY (lface)))
2262 Lisp_Object family = AREF (font_object, FONT_FAMILY_INDEX);
2264 LFACE_FAMILY (lface) = SYMBOL_NAME (family);
2267 if (force_p || UNSPECIFIEDP (LFACE_FOUNDRY (lface)))
2269 Lisp_Object foundry = AREF (font_object, FONT_FOUNDRY_INDEX);
2271 LFACE_FOUNDRY (lface) = SYMBOL_NAME (foundry);
2274 if (force_p || UNSPECIFIEDP (LFACE_HEIGHT (lface)))
2276 int pt = PIXEL_TO_POINT (font->pixel_size * 10, f->resy);
2278 xassert (pt > 0);
2279 LFACE_HEIGHT (lface) = make_number (pt);
2282 if (force_p || UNSPECIFIEDP (LFACE_WEIGHT (lface)))
2284 val = FONT_WEIGHT_FOR_FACE (font_object);
2285 LFACE_WEIGHT (lface) = ! NILP (val) ? val :Qnormal;
2287 if (force_p || UNSPECIFIEDP (LFACE_SLANT (lface)))
2289 val = FONT_SLANT_FOR_FACE (font_object);
2290 LFACE_SLANT (lface) = ! NILP (val) ? val : Qnormal;
2292 if (force_p || UNSPECIFIEDP (LFACE_SWIDTH (lface)))
2294 val = FONT_WIDTH_FOR_FACE (font_object);
2295 LFACE_SWIDTH (lface) = ! NILP (val) ? val : Qnormal;
2298 LFACE_FONT (lface) = font_object;
2299 return 1;
2302 #endif /* HAVE_WINDOW_SYSTEM */
2305 /* Merges the face height FROM with the face height TO, and returns the
2306 merged height. If FROM is an invalid height, then INVALID is
2307 returned instead. FROM and TO may be either absolute face heights or
2308 `relative' heights; the returned value is always an absolute height
2309 unless both FROM and TO are relative. */
2311 Lisp_Object
2312 merge_face_heights (Lisp_Object from, Lisp_Object to, Lisp_Object invalid)
2314 Lisp_Object result = invalid;
2316 if (INTEGERP (from))
2317 /* FROM is absolute, just use it as is. */
2318 result = from;
2319 else if (FLOATP (from))
2320 /* FROM is a scale, use it to adjust TO. */
2322 if (INTEGERP (to))
2323 /* relative X absolute => absolute */
2324 result = make_number ((EMACS_INT)(XFLOAT_DATA (from) * XINT (to)));
2325 else if (FLOATP (to))
2326 /* relative X relative => relative */
2327 result = make_float (XFLOAT_DATA (from) * XFLOAT_DATA (to));
2328 else if (UNSPECIFIEDP (to))
2329 result = from;
2331 else if (FUNCTIONP (from))
2332 /* FROM is a function, which use to adjust TO. */
2334 /* Call function with current height as argument.
2335 From is the new height. */
2336 Lisp_Object args[2];
2338 args[0] = from;
2339 args[1] = to;
2340 result = safe_call (2, args);
2342 /* Ensure that if TO was absolute, so is the result. */
2343 if (INTEGERP (to) && !INTEGERP (result))
2344 result = invalid;
2347 return result;
2351 /* Merge two Lisp face attribute vectors on frame F, FROM and TO, and
2352 store the resulting attributes in TO, which must be already be
2353 completely specified and contain only absolute attributes. Every
2354 specified attribute of FROM overrides the corresponding attribute of
2355 TO; relative attributes in FROM are merged with the absolute value in
2356 TO and replace it. NAMED_MERGE_POINTS is used internally to detect
2357 loops in face inheritance/remapping; it should be 0 when called from
2358 other places. */
2360 static INLINE void
2361 merge_face_vectors (struct frame *f, Lisp_Object *from, Lisp_Object *to, struct named_merge_point *named_merge_points)
2363 int i;
2365 /* If FROM inherits from some other faces, merge their attributes into
2366 TO before merging FROM's direct attributes. Note that an :inherit
2367 attribute of `unspecified' is the same as one of nil; we never
2368 merge :inherit attributes, so nil is more correct, but lots of
2369 other code uses `unspecified' as a generic value for face attributes. */
2370 if (!UNSPECIFIEDP (from[LFACE_INHERIT_INDEX])
2371 && !NILP (from[LFACE_INHERIT_INDEX]))
2372 merge_face_ref (f, from[LFACE_INHERIT_INDEX], to, 0, named_merge_points);
2374 i = LFACE_FONT_INDEX;
2375 if (!UNSPECIFIEDP (from[i]))
2377 if (!UNSPECIFIEDP (to[i]))
2378 to[i] = Fmerge_font_spec (from[i], to[i]);
2379 else
2380 to[i] = Fcopy_font_spec (from[i]);
2381 if (! NILP (AREF (to[i], FONT_FOUNDRY_INDEX)))
2382 to[LFACE_FOUNDRY_INDEX] = SYMBOL_NAME (AREF (to[i], FONT_FOUNDRY_INDEX));
2383 if (! NILP (AREF (to[i], FONT_FAMILY_INDEX)))
2384 to[LFACE_FAMILY_INDEX] = SYMBOL_NAME (AREF (to[i], FONT_FAMILY_INDEX));
2385 if (! NILP (AREF (to[i], FONT_WEIGHT_INDEX)))
2386 to[LFACE_WEIGHT_INDEX] = FONT_WEIGHT_FOR_FACE (to[i]);
2387 if (! NILP (AREF (to[i], FONT_SLANT_INDEX)))
2388 to[LFACE_SLANT_INDEX] = FONT_SLANT_FOR_FACE (to[i]);
2389 if (! NILP (AREF (to[i], FONT_WIDTH_INDEX)))
2390 to[LFACE_SWIDTH_INDEX] = FONT_WIDTH_FOR_FACE (to[i]);
2391 ASET (to[i], FONT_SIZE_INDEX, Qnil);
2394 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2395 if (!UNSPECIFIEDP (from[i]))
2397 if (i == LFACE_HEIGHT_INDEX && !INTEGERP (from[i]))
2399 to[i] = merge_face_heights (from[i], to[i], to[i]);
2400 font_clear_prop (to, FONT_SIZE_INDEX);
2402 else if (i != LFACE_FONT_INDEX
2403 && ! EQ (to[i], from[i]))
2405 to[i] = from[i];
2406 if (i >= LFACE_FAMILY_INDEX && i <=LFACE_SLANT_INDEX)
2407 font_clear_prop (to,
2408 (i == LFACE_FAMILY_INDEX ? FONT_FAMILY_INDEX
2409 : i == LFACE_FOUNDRY_INDEX ? FONT_FOUNDRY_INDEX
2410 : i == LFACE_SWIDTH_INDEX ? FONT_WIDTH_INDEX
2411 : i == LFACE_HEIGHT_INDEX ? FONT_SIZE_INDEX
2412 : i == LFACE_WEIGHT_INDEX ? FONT_WEIGHT_INDEX
2413 : FONT_SLANT_INDEX));
2417 /* TO is always an absolute face, which should inherit from nothing.
2418 We blindly copy the :inherit attribute above and fix it up here. */
2419 to[LFACE_INHERIT_INDEX] = Qnil;
2422 /* Merge the named face FACE_NAME on frame F, into the vector of face
2423 attributes TO. NAMED_MERGE_POINTS is used to detect loops in face
2424 inheritance. Returns true if FACE_NAME is a valid face name and
2425 merging succeeded. */
2427 static int
2428 merge_named_face (struct frame *f, Lisp_Object face_name, Lisp_Object *to, struct named_merge_point *named_merge_points)
2430 struct named_merge_point named_merge_point;
2432 if (push_named_merge_point (&named_merge_point,
2433 face_name, NAMED_MERGE_POINT_NORMAL,
2434 &named_merge_points))
2436 struct gcpro gcpro1;
2437 Lisp_Object from[LFACE_VECTOR_SIZE];
2438 int ok = get_lface_attributes (f, face_name, from, 0, named_merge_points);
2440 if (ok)
2442 GCPRO1 (named_merge_point.face_name);
2443 merge_face_vectors (f, from, to, named_merge_points);
2444 UNGCPRO;
2447 return ok;
2449 else
2450 return 0;
2454 /* Merge face attributes from the lisp `face reference' FACE_REF on
2455 frame F into the face attribute vector TO. If ERR_MSGS is non-zero,
2456 problems with FACE_REF cause an error message to be shown. Return
2457 non-zero if no errors occurred (regardless of the value of ERR_MSGS).
2458 NAMED_MERGE_POINTS is used to detect loops in face inheritance or
2459 list structure; it may be 0 for most callers.
2461 FACE_REF may be a single face specification or a list of such
2462 specifications. Each face specification can be:
2464 1. A symbol or string naming a Lisp face.
2466 2. A property list of the form (KEYWORD VALUE ...) where each
2467 KEYWORD is a face attribute name, and value is an appropriate value
2468 for that attribute.
2470 3. Conses or the form (FOREGROUND-COLOR . COLOR) or
2471 (BACKGROUND-COLOR . COLOR) where COLOR is a color name. This is
2472 for compatibility with 20.2.
2474 Face specifications earlier in lists take precedence over later
2475 specifications. */
2477 static int
2478 merge_face_ref (struct frame *f, Lisp_Object face_ref, Lisp_Object *to, int err_msgs, struct named_merge_point *named_merge_points)
2480 int ok = 1; /* Succeed without an error? */
2482 if (CONSP (face_ref))
2484 Lisp_Object first = XCAR (face_ref);
2486 if (EQ (first, Qforeground_color)
2487 || EQ (first, Qbackground_color))
2489 /* One of (FOREGROUND-COLOR . COLOR) or (BACKGROUND-COLOR
2490 . COLOR). COLOR must be a string. */
2491 Lisp_Object color_name = XCDR (face_ref);
2492 Lisp_Object color = first;
2494 if (STRINGP (color_name))
2496 if (EQ (color, Qforeground_color))
2497 to[LFACE_FOREGROUND_INDEX] = color_name;
2498 else
2499 to[LFACE_BACKGROUND_INDEX] = color_name;
2501 else
2503 if (err_msgs)
2504 add_to_log ("Invalid face color", color_name, Qnil);
2505 ok = 0;
2508 else if (SYMBOLP (first)
2509 && *SDATA (SYMBOL_NAME (first)) == ':')
2511 /* Assume this is the property list form. */
2512 while (CONSP (face_ref) && CONSP (XCDR (face_ref)))
2514 Lisp_Object keyword = XCAR (face_ref);
2515 Lisp_Object value = XCAR (XCDR (face_ref));
2516 int err = 0;
2518 /* Specifying `unspecified' is a no-op. */
2519 if (EQ (value, Qunspecified))
2521 else if (EQ (keyword, QCfamily))
2523 if (STRINGP (value))
2525 to[LFACE_FAMILY_INDEX] = value;
2526 font_clear_prop (to, FONT_FAMILY_INDEX);
2528 else
2529 err = 1;
2531 else if (EQ (keyword, QCfoundry))
2533 if (STRINGP (value))
2535 to[LFACE_FOUNDRY_INDEX] = value;
2536 font_clear_prop (to, FONT_FOUNDRY_INDEX);
2538 else
2539 err = 1;
2541 else if (EQ (keyword, QCheight))
2543 Lisp_Object new_height =
2544 merge_face_heights (value, to[LFACE_HEIGHT_INDEX], Qnil);
2546 if (! NILP (new_height))
2548 to[LFACE_HEIGHT_INDEX] = new_height;
2549 font_clear_prop (to, FONT_SIZE_INDEX);
2551 else
2552 err = 1;
2554 else if (EQ (keyword, QCweight))
2556 if (SYMBOLP (value) && FONT_WEIGHT_NAME_NUMERIC (value) >= 0)
2558 to[LFACE_WEIGHT_INDEX] = value;
2559 font_clear_prop (to, FONT_WEIGHT_INDEX);
2561 else
2562 err = 1;
2564 else if (EQ (keyword, QCslant))
2566 if (SYMBOLP (value) && FONT_SLANT_NAME_NUMERIC (value) >= 0)
2568 to[LFACE_SLANT_INDEX] = value;
2569 font_clear_prop (to, FONT_SLANT_INDEX);
2571 else
2572 err = 1;
2574 else if (EQ (keyword, QCunderline))
2576 if (EQ (value, Qt)
2577 || NILP (value)
2578 || STRINGP (value))
2579 to[LFACE_UNDERLINE_INDEX] = value;
2580 else
2581 err = 1;
2583 else if (EQ (keyword, QCoverline))
2585 if (EQ (value, Qt)
2586 || NILP (value)
2587 || STRINGP (value))
2588 to[LFACE_OVERLINE_INDEX] = value;
2589 else
2590 err = 1;
2592 else if (EQ (keyword, QCstrike_through))
2594 if (EQ (value, Qt)
2595 || NILP (value)
2596 || STRINGP (value))
2597 to[LFACE_STRIKE_THROUGH_INDEX] = value;
2598 else
2599 err = 1;
2601 else if (EQ (keyword, QCbox))
2603 if (EQ (value, Qt))
2604 value = make_number (1);
2605 if (INTEGERP (value)
2606 || STRINGP (value)
2607 || CONSP (value)
2608 || NILP (value))
2609 to[LFACE_BOX_INDEX] = value;
2610 else
2611 err = 1;
2613 else if (EQ (keyword, QCinverse_video)
2614 || EQ (keyword, QCreverse_video))
2616 if (EQ (value, Qt) || NILP (value))
2617 to[LFACE_INVERSE_INDEX] = value;
2618 else
2619 err = 1;
2621 else if (EQ (keyword, QCforeground))
2623 if (STRINGP (value))
2624 to[LFACE_FOREGROUND_INDEX] = value;
2625 else
2626 err = 1;
2628 else if (EQ (keyword, QCbackground))
2630 if (STRINGP (value))
2631 to[LFACE_BACKGROUND_INDEX] = value;
2632 else
2633 err = 1;
2635 else if (EQ (keyword, QCstipple))
2637 #if defined(HAVE_X_WINDOWS) || defined(HAVE_NS)
2638 Lisp_Object pixmap_p = Fbitmap_spec_p (value);
2639 if (!NILP (pixmap_p))
2640 to[LFACE_STIPPLE_INDEX] = value;
2641 else
2642 err = 1;
2643 #endif
2645 else if (EQ (keyword, QCwidth))
2647 if (SYMBOLP (value) && FONT_WIDTH_NAME_NUMERIC (value) >= 0)
2649 to[LFACE_SWIDTH_INDEX] = value;
2650 font_clear_prop (to, FONT_WIDTH_INDEX);
2652 else
2653 err = 1;
2655 else if (EQ (keyword, QCinherit))
2657 /* This is not really very useful; it's just like a
2658 normal face reference. */
2659 if (! merge_face_ref (f, value, to,
2660 err_msgs, named_merge_points))
2661 err = 1;
2663 else
2664 err = 1;
2666 if (err)
2668 add_to_log ("Invalid face attribute %S %S", keyword, value);
2669 ok = 0;
2672 face_ref = XCDR (XCDR (face_ref));
2675 else
2677 /* This is a list of face refs. Those at the beginning of the
2678 list take precedence over what follows, so we have to merge
2679 from the end backwards. */
2680 Lisp_Object next = XCDR (face_ref);
2682 if (! NILP (next))
2683 ok = merge_face_ref (f, next, to, err_msgs, named_merge_points);
2685 if (! merge_face_ref (f, first, to, err_msgs, named_merge_points))
2686 ok = 0;
2689 else
2691 /* FACE_REF ought to be a face name. */
2692 ok = merge_named_face (f, face_ref, to, named_merge_points);
2693 if (!ok && err_msgs)
2694 add_to_log ("Invalid face reference: %s", face_ref, Qnil);
2697 return ok;
2701 DEFUN ("internal-make-lisp-face", Finternal_make_lisp_face,
2702 Sinternal_make_lisp_face, 1, 2, 0,
2703 doc: /* Make FACE, a symbol, a Lisp face with all attributes nil.
2704 If FACE was not known as a face before, create a new one.
2705 If optional argument FRAME is specified, make a frame-local face
2706 for that frame. Otherwise operate on the global face definition.
2707 Value is a vector of face attributes. */)
2708 (Lisp_Object face, Lisp_Object frame)
2710 Lisp_Object global_lface, lface;
2711 struct frame *f;
2712 int i;
2714 CHECK_SYMBOL (face);
2715 global_lface = lface_from_face_name (NULL, face, 0);
2717 if (!NILP (frame))
2719 CHECK_LIVE_FRAME (frame);
2720 f = XFRAME (frame);
2721 lface = lface_from_face_name (f, face, 0);
2723 else
2724 f = NULL, lface = Qnil;
2726 /* Add a global definition if there is none. */
2727 if (NILP (global_lface))
2729 global_lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2730 Qunspecified);
2731 ASET (global_lface, 0, Qface);
2732 Vface_new_frame_defaults = Fcons (Fcons (face, global_lface),
2733 Vface_new_frame_defaults);
2735 /* Assign the new Lisp face a unique ID. The mapping from Lisp
2736 face id to Lisp face is given by the vector lface_id_to_name.
2737 The mapping from Lisp face to Lisp face id is given by the
2738 property `face' of the Lisp face name. */
2739 if (next_lface_id == lface_id_to_name_size)
2741 int new_size = max (50, 2 * lface_id_to_name_size);
2742 int sz = new_size * sizeof *lface_id_to_name;
2743 lface_id_to_name = (Lisp_Object *) xrealloc (lface_id_to_name, sz);
2744 lface_id_to_name_size = new_size;
2747 lface_id_to_name[next_lface_id] = face;
2748 Fput (face, Qface, make_number (next_lface_id));
2749 ++next_lface_id;
2751 else if (f == NULL)
2752 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2753 ASET (global_lface, i, Qunspecified);
2755 /* Add a frame-local definition. */
2756 if (f)
2758 if (NILP (lface))
2760 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2761 Qunspecified);
2762 ASET (lface, 0, Qface);
2763 f->face_alist = Fcons (Fcons (face, lface), f->face_alist);
2765 else
2766 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2767 ASET (lface, i, Qunspecified);
2769 else
2770 lface = global_lface;
2772 /* Changing a named face means that all realized faces depending on
2773 that face are invalid. Since we cannot tell which realized faces
2774 depend on the face, make sure they are all removed. This is done
2775 by incrementing face_change_count. The next call to
2776 init_iterator will then free realized faces. */
2777 if (NILP (Fget (face, Qface_no_inherit)))
2779 ++face_change_count;
2780 ++windows_or_buffers_changed;
2783 xassert (LFACEP (lface));
2784 check_lface (lface);
2785 return lface;
2789 DEFUN ("internal-lisp-face-p", Finternal_lisp_face_p,
2790 Sinternal_lisp_face_p, 1, 2, 0,
2791 doc: /* Return non-nil if FACE names a face.
2792 FACE should be a symbol or string.
2793 If optional second argument FRAME is non-nil, check for the
2794 existence of a frame-local face with name FACE on that frame.
2795 Otherwise check for the existence of a global face. */)
2796 (Lisp_Object face, Lisp_Object frame)
2798 Lisp_Object lface;
2800 face = resolve_face_name (face, 1);
2802 if (!NILP (frame))
2804 CHECK_LIVE_FRAME (frame);
2805 lface = lface_from_face_name (XFRAME (frame), face, 0);
2807 else
2808 lface = lface_from_face_name (NULL, face, 0);
2810 return lface;
2814 DEFUN ("internal-copy-lisp-face", Finternal_copy_lisp_face,
2815 Sinternal_copy_lisp_face, 4, 4, 0,
2816 doc: /* Copy face FROM to TO.
2817 If FRAME is t, copy the global face definition of FROM.
2818 Otherwise, copy the frame-local definition of FROM on FRAME.
2819 If NEW-FRAME is a frame, copy that data into the frame-local
2820 definition of TO on NEW-FRAME. If NEW-FRAME is nil,
2821 FRAME controls where the data is copied to.
2823 The value is TO. */)
2824 (Lisp_Object from, Lisp_Object to, Lisp_Object frame, Lisp_Object new_frame)
2826 Lisp_Object lface, copy;
2828 CHECK_SYMBOL (from);
2829 CHECK_SYMBOL (to);
2831 if (EQ (frame, Qt))
2833 /* Copy global definition of FROM. We don't make copies of
2834 strings etc. because 20.2 didn't do it either. */
2835 lface = lface_from_face_name (NULL, from, 1);
2836 copy = Finternal_make_lisp_face (to, Qnil);
2838 else
2840 /* Copy frame-local definition of FROM. */
2841 if (NILP (new_frame))
2842 new_frame = frame;
2843 CHECK_LIVE_FRAME (frame);
2844 CHECK_LIVE_FRAME (new_frame);
2845 lface = lface_from_face_name (XFRAME (frame), from, 1);
2846 copy = Finternal_make_lisp_face (to, new_frame);
2849 memcpy (XVECTOR (copy)->contents, XVECTOR (lface)->contents,
2850 LFACE_VECTOR_SIZE * sizeof (Lisp_Object));
2852 /* Changing a named face means that all realized faces depending on
2853 that face are invalid. Since we cannot tell which realized faces
2854 depend on the face, make sure they are all removed. This is done
2855 by incrementing face_change_count. The next call to
2856 init_iterator will then free realized faces. */
2857 if (NILP (Fget (to, Qface_no_inherit)))
2859 ++face_change_count;
2860 ++windows_or_buffers_changed;
2863 return to;
2867 DEFUN ("internal-set-lisp-face-attribute", Finternal_set_lisp_face_attribute,
2868 Sinternal_set_lisp_face_attribute, 3, 4, 0,
2869 doc: /* Set attribute ATTR of FACE to VALUE.
2870 FRAME being a frame means change the face on that frame.
2871 FRAME nil means change the face of the selected frame.
2872 FRAME t means change the default for new frames.
2873 FRAME 0 means change the face on all frames, and change the default
2874 for new frames. */)
2875 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
2877 Lisp_Object lface;
2878 Lisp_Object old_value = Qnil;
2879 /* Set one of enum font_property_index (> 0) if ATTR is one of
2880 font-related attributes other than QCfont and QCfontset. */
2881 enum font_property_index prop_index = 0;
2883 CHECK_SYMBOL (face);
2884 CHECK_SYMBOL (attr);
2886 face = resolve_face_name (face, 1);
2888 /* If FRAME is 0, change face on all frames, and change the
2889 default for new frames. */
2890 if (INTEGERP (frame) && XINT (frame) == 0)
2892 Lisp_Object tail;
2893 Finternal_set_lisp_face_attribute (face, attr, value, Qt);
2894 FOR_EACH_FRAME (tail, frame)
2895 Finternal_set_lisp_face_attribute (face, attr, value, frame);
2896 return face;
2899 /* Set lface to the Lisp attribute vector of FACE. */
2900 if (EQ (frame, Qt))
2902 lface = lface_from_face_name (NULL, face, 1);
2904 /* When updating face-new-frame-defaults, we put :ignore-defface
2905 where the caller wants `unspecified'. This forces the frame
2906 defaults to ignore the defface value. Otherwise, the defface
2907 will take effect, which is generally not what is intended.
2908 The value of that attribute will be inherited from some other
2909 face during face merging. See internal_merge_in_global_face. */
2910 if (UNSPECIFIEDP (value))
2911 value = Qignore_defface;
2913 else
2915 if (NILP (frame))
2916 frame = selected_frame;
2918 CHECK_LIVE_FRAME (frame);
2919 lface = lface_from_face_name (XFRAME (frame), face, 0);
2921 /* If a frame-local face doesn't exist yet, create one. */
2922 if (NILP (lface))
2923 lface = Finternal_make_lisp_face (face, frame);
2926 if (EQ (attr, QCfamily))
2928 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2930 CHECK_STRING (value);
2931 if (SCHARS (value) == 0)
2932 signal_error ("Invalid face family", value);
2934 old_value = LFACE_FAMILY (lface);
2935 LFACE_FAMILY (lface) = value;
2936 prop_index = FONT_FAMILY_INDEX;
2938 else if (EQ (attr, QCfoundry))
2940 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2942 CHECK_STRING (value);
2943 if (SCHARS (value) == 0)
2944 signal_error ("Invalid face foundry", value);
2946 old_value = LFACE_FOUNDRY (lface);
2947 LFACE_FOUNDRY (lface) = value;
2948 prop_index = FONT_FOUNDRY_INDEX;
2950 else if (EQ (attr, QCheight))
2952 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2954 if (EQ (face, Qdefault))
2956 /* The default face must have an absolute size. */
2957 if (!INTEGERP (value) || XINT (value) <= 0)
2958 signal_error ("Invalid default face height", value);
2960 else
2962 /* For non-default faces, do a test merge with a random
2963 height to see if VALUE's ok. */
2964 Lisp_Object test = merge_face_heights (value,
2965 make_number (10),
2966 Qnil);
2967 if (!INTEGERP (test) || XINT (test) <= 0)
2968 signal_error ("Invalid face height", value);
2972 old_value = LFACE_HEIGHT (lface);
2973 LFACE_HEIGHT (lface) = value;
2974 prop_index = FONT_SIZE_INDEX;
2976 else if (EQ (attr, QCweight))
2978 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2980 CHECK_SYMBOL (value);
2981 if (FONT_WEIGHT_NAME_NUMERIC (value) < 0)
2982 signal_error ("Invalid face weight", value);
2984 old_value = LFACE_WEIGHT (lface);
2985 LFACE_WEIGHT (lface) = value;
2986 prop_index = FONT_WEIGHT_INDEX;
2988 else if (EQ (attr, QCslant))
2990 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2992 CHECK_SYMBOL (value);
2993 if (FONT_SLANT_NAME_NUMERIC (value) < 0)
2994 signal_error ("Invalid face slant", value);
2996 old_value = LFACE_SLANT (lface);
2997 LFACE_SLANT (lface) = value;
2998 prop_index = FONT_SLANT_INDEX;
3000 else if (EQ (attr, QCunderline))
3002 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3003 if ((SYMBOLP (value)
3004 && !EQ (value, Qt)
3005 && !EQ (value, Qnil))
3006 /* Underline color. */
3007 || (STRINGP (value)
3008 && SCHARS (value) == 0))
3009 signal_error ("Invalid face underline", value);
3011 old_value = LFACE_UNDERLINE (lface);
3012 LFACE_UNDERLINE (lface) = value;
3014 else if (EQ (attr, QCoverline))
3016 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3017 if ((SYMBOLP (value)
3018 && !EQ (value, Qt)
3019 && !EQ (value, Qnil))
3020 /* Overline color. */
3021 || (STRINGP (value)
3022 && SCHARS (value) == 0))
3023 signal_error ("Invalid face overline", value);
3025 old_value = LFACE_OVERLINE (lface);
3026 LFACE_OVERLINE (lface) = value;
3028 else if (EQ (attr, QCstrike_through))
3030 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3031 if ((SYMBOLP (value)
3032 && !EQ (value, Qt)
3033 && !EQ (value, Qnil))
3034 /* Strike-through color. */
3035 || (STRINGP (value)
3036 && SCHARS (value) == 0))
3037 signal_error ("Invalid face strike-through", value);
3039 old_value = LFACE_STRIKE_THROUGH (lface);
3040 LFACE_STRIKE_THROUGH (lface) = value;
3042 else if (EQ (attr, QCbox))
3044 int valid_p;
3046 /* Allow t meaning a simple box of width 1 in foreground color
3047 of the face. */
3048 if (EQ (value, Qt))
3049 value = make_number (1);
3051 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
3052 valid_p = 1;
3053 else if (NILP (value))
3054 valid_p = 1;
3055 else if (INTEGERP (value))
3056 valid_p = XINT (value) != 0;
3057 else if (STRINGP (value))
3058 valid_p = SCHARS (value) > 0;
3059 else if (CONSP (value))
3061 Lisp_Object tem;
3063 tem = value;
3064 while (CONSP (tem))
3066 Lisp_Object k, v;
3068 k = XCAR (tem);
3069 tem = XCDR (tem);
3070 if (!CONSP (tem))
3071 break;
3072 v = XCAR (tem);
3073 tem = XCDR (tem);
3075 if (EQ (k, QCline_width))
3077 if (!INTEGERP (v) || XINT (v) == 0)
3078 break;
3080 else if (EQ (k, QCcolor))
3082 if (!NILP (v) && (!STRINGP (v) || SCHARS (v) == 0))
3083 break;
3085 else if (EQ (k, QCstyle))
3087 if (!EQ (v, Qpressed_button) && !EQ (v, Qreleased_button))
3088 break;
3090 else
3091 break;
3094 valid_p = NILP (tem);
3096 else
3097 valid_p = 0;
3099 if (!valid_p)
3100 signal_error ("Invalid face box", value);
3102 old_value = LFACE_BOX (lface);
3103 LFACE_BOX (lface) = value;
3105 else if (EQ (attr, QCinverse_video)
3106 || EQ (attr, QCreverse_video))
3108 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3110 CHECK_SYMBOL (value);
3111 if (!EQ (value, Qt) && !NILP (value))
3112 signal_error ("Invalid inverse-video face attribute value", value);
3114 old_value = LFACE_INVERSE (lface);
3115 LFACE_INVERSE (lface) = value;
3117 else if (EQ (attr, QCforeground))
3119 /* Compatibility with 20.x. */
3120 if (NILP (value))
3121 value = Qunspecified;
3122 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3124 /* Don't check for valid color names here because it depends
3125 on the frame (display) whether the color will be valid
3126 when the face is realized. */
3127 CHECK_STRING (value);
3128 if (SCHARS (value) == 0)
3129 signal_error ("Empty foreground color value", value);
3131 old_value = LFACE_FOREGROUND (lface);
3132 LFACE_FOREGROUND (lface) = value;
3134 else if (EQ (attr, QCbackground))
3136 /* Compatibility with 20.x. */
3137 if (NILP (value))
3138 value = Qunspecified;
3139 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3141 /* Don't check for valid color names here because it depends
3142 on the frame (display) whether the color will be valid
3143 when the face is realized. */
3144 CHECK_STRING (value);
3145 if (SCHARS (value) == 0)
3146 signal_error ("Empty background color value", value);
3148 old_value = LFACE_BACKGROUND (lface);
3149 LFACE_BACKGROUND (lface) = value;
3151 else if (EQ (attr, QCstipple))
3153 #if defined(HAVE_X_WINDOWS) || defined(HAVE_NS)
3154 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3155 && !NILP (value)
3156 && NILP (Fbitmap_spec_p (value)))
3157 signal_error ("Invalid stipple attribute", value);
3158 old_value = LFACE_STIPPLE (lface);
3159 LFACE_STIPPLE (lface) = value;
3160 #endif /* HAVE_X_WINDOWS || HAVE_NS */
3162 else if (EQ (attr, QCwidth))
3164 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3166 CHECK_SYMBOL (value);
3167 if (FONT_WIDTH_NAME_NUMERIC (value) < 0)
3168 signal_error ("Invalid face width", value);
3170 old_value = LFACE_SWIDTH (lface);
3171 LFACE_SWIDTH (lface) = value;
3172 prop_index = FONT_WIDTH_INDEX;
3174 else if (EQ (attr, QCfont))
3176 #ifdef HAVE_WINDOW_SYSTEM
3177 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
3179 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3181 FRAME_PTR f;
3183 old_value = LFACE_FONT (lface);
3184 if (! FONTP (value))
3186 if (STRINGP (value))
3188 Lisp_Object name = value;
3189 int fontset = fs_query_fontset (name, 0);
3191 if (fontset >= 0)
3192 name = fontset_ascii (fontset);
3193 value = font_spec_from_name (name);
3194 if (!FONTP (value))
3195 signal_error ("Invalid font name", name);
3197 else
3198 signal_error ("Invalid font or font-spec", value);
3200 if (EQ (frame, Qt))
3201 f = XFRAME (selected_frame);
3202 else
3203 f = XFRAME (frame);
3204 if (! FONT_OBJECT_P (value))
3206 Lisp_Object *attrs = XVECTOR (lface)->contents;
3207 Lisp_Object font_object;
3209 font_object = font_load_for_lface (f, attrs, value);
3210 if (NILP (font_object))
3211 signal_error ("Font not available", value);
3212 value = font_object;
3214 set_lface_from_font (f, lface, value, 1);
3216 else
3217 LFACE_FONT (lface) = value;
3219 #endif /* HAVE_WINDOW_SYSTEM */
3221 else if (EQ (attr, QCfontset))
3223 #ifdef HAVE_WINDOW_SYSTEM
3224 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
3226 Lisp_Object tmp;
3228 old_value = LFACE_FONTSET (lface);
3229 tmp = Fquery_fontset (value, Qnil);
3230 if (NILP (tmp))
3231 signal_error ("Invalid fontset name", value);
3232 LFACE_FONTSET (lface) = value = tmp;
3234 #endif /* HAVE_WINDOW_SYSTEM */
3236 else if (EQ (attr, QCinherit))
3238 Lisp_Object tail;
3239 if (SYMBOLP (value))
3240 tail = Qnil;
3241 else
3242 for (tail = value; CONSP (tail); tail = XCDR (tail))
3243 if (!SYMBOLP (XCAR (tail)))
3244 break;
3245 if (NILP (tail))
3246 LFACE_INHERIT (lface) = value;
3247 else
3248 signal_error ("Invalid face inheritance", value);
3250 else if (EQ (attr, QCbold))
3252 old_value = LFACE_WEIGHT (lface);
3253 LFACE_WEIGHT (lface) = NILP (value) ? Qnormal : Qbold;
3254 prop_index = FONT_WEIGHT_INDEX;
3256 else if (EQ (attr, QCitalic))
3258 attr = QCslant;
3259 old_value = LFACE_SLANT (lface);
3260 LFACE_SLANT (lface) = NILP (value) ? Qnormal : Qitalic;
3261 prop_index = FONT_SLANT_INDEX;
3263 else
3264 signal_error ("Invalid face attribute name", attr);
3266 if (prop_index)
3268 /* If a font-related attribute other than QCfont and QCfontset
3269 is specified, and if the original QCfont attribute has a font
3270 (font-spec or font-object), set the corresponding property in
3271 the font to nil so that the font selector doesn't think that
3272 the attribute is mandatory. Also, clear the average
3273 width. */
3274 font_clear_prop (XVECTOR (lface)->contents, prop_index);
3277 /* Changing a named face means that all realized faces depending on
3278 that face are invalid. Since we cannot tell which realized faces
3279 depend on the face, make sure they are all removed. This is done
3280 by incrementing face_change_count. The next call to
3281 init_iterator will then free realized faces. */
3282 if (!EQ (frame, Qt)
3283 && NILP (Fget (face, Qface_no_inherit))
3284 && NILP (Fequal (old_value, value)))
3286 ++face_change_count;
3287 ++windows_or_buffers_changed;
3290 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3291 && NILP (Fequal (old_value, value)))
3293 Lisp_Object param;
3295 param = Qnil;
3297 if (EQ (face, Qdefault))
3299 #ifdef HAVE_WINDOW_SYSTEM
3300 /* Changed font-related attributes of the `default' face are
3301 reflected in changed `font' frame parameters. */
3302 if (FRAMEP (frame)
3303 && (prop_index || EQ (attr, QCfont))
3304 && lface_fully_specified_p (XVECTOR (lface)->contents))
3305 set_font_frame_param (frame, lface);
3306 else
3307 #endif /* HAVE_WINDOW_SYSTEM */
3309 if (EQ (attr, QCforeground))
3310 param = Qforeground_color;
3311 else if (EQ (attr, QCbackground))
3312 param = Qbackground_color;
3314 #ifdef HAVE_WINDOW_SYSTEM
3315 #ifndef WINDOWSNT
3316 else if (EQ (face, Qscroll_bar))
3318 /* Changing the colors of `scroll-bar' sets frame parameters
3319 `scroll-bar-foreground' and `scroll-bar-background'. */
3320 if (EQ (attr, QCforeground))
3321 param = Qscroll_bar_foreground;
3322 else if (EQ (attr, QCbackground))
3323 param = Qscroll_bar_background;
3325 #endif /* not WINDOWSNT */
3326 else if (EQ (face, Qborder))
3328 /* Changing background color of `border' sets frame parameter
3329 `border-color'. */
3330 if (EQ (attr, QCbackground))
3331 param = Qborder_color;
3333 else if (EQ (face, Qcursor))
3335 /* Changing background color of `cursor' sets frame parameter
3336 `cursor-color'. */
3337 if (EQ (attr, QCbackground))
3338 param = Qcursor_color;
3340 else if (EQ (face, Qmouse))
3342 /* Changing background color of `mouse' sets frame parameter
3343 `mouse-color'. */
3344 if (EQ (attr, QCbackground))
3345 param = Qmouse_color;
3347 #endif /* HAVE_WINDOW_SYSTEM */
3348 else if (EQ (face, Qmenu))
3350 /* Indicate that we have to update the menu bar when
3351 realizing faces on FRAME. FRAME t change the
3352 default for new frames. We do this by setting
3353 setting the flag in new face caches */
3354 if (FRAMEP (frame))
3356 struct frame *f = XFRAME (frame);
3357 if (FRAME_FACE_CACHE (f) == NULL)
3358 FRAME_FACE_CACHE (f) = make_face_cache (f);
3359 FRAME_FACE_CACHE (f)->menu_face_changed_p = 1;
3361 else
3362 menu_face_changed_default = 1;
3365 if (!NILP (param))
3367 if (EQ (frame, Qt))
3368 /* Update `default-frame-alist', which is used for new frames. */
3370 store_in_alist (&Vdefault_frame_alist, param, value);
3372 else
3373 /* Update the current frame's parameters. */
3375 Lisp_Object cons;
3376 cons = XCAR (Vparam_value_alist);
3377 XSETCAR (cons, param);
3378 XSETCDR (cons, value);
3379 Fmodify_frame_parameters (frame, Vparam_value_alist);
3384 return face;
3388 /* Update the corresponding face when frame parameter PARAM on frame F
3389 has been assigned the value NEW_VALUE. */
3391 void
3392 update_face_from_frame_parameter (struct frame *f, Lisp_Object param, Lisp_Object new_value)
3394 Lisp_Object face = Qnil;
3395 Lisp_Object lface;
3397 /* If there are no faces yet, give up. This is the case when called
3398 from Fx_create_frame, and we do the necessary things later in
3399 face-set-after-frame-defaults. */
3400 if (NILP (f->face_alist))
3401 return;
3403 if (EQ (param, Qforeground_color))
3405 face = Qdefault;
3406 lface = lface_from_face_name (f, face, 1);
3407 LFACE_FOREGROUND (lface) = (STRINGP (new_value)
3408 ? new_value : Qunspecified);
3409 realize_basic_faces (f);
3411 else if (EQ (param, Qbackground_color))
3413 Lisp_Object frame;
3415 /* Changing the background color might change the background
3416 mode, so that we have to load new defface specs.
3417 Call frame-update-face-colors to do that. */
3418 XSETFRAME (frame, f);
3419 call1 (Qframe_set_background_mode, frame);
3421 face = Qdefault;
3422 lface = lface_from_face_name (f, face, 1);
3423 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
3424 ? new_value : Qunspecified);
3425 realize_basic_faces (f);
3427 #ifdef HAVE_WINDOW_SYSTEM
3428 else if (EQ (param, Qborder_color))
3430 face = Qborder;
3431 lface = lface_from_face_name (f, face, 1);
3432 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
3433 ? new_value : Qunspecified);
3435 else if (EQ (param, Qcursor_color))
3437 face = Qcursor;
3438 lface = lface_from_face_name (f, face, 1);
3439 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
3440 ? new_value : Qunspecified);
3442 else if (EQ (param, Qmouse_color))
3444 face = Qmouse;
3445 lface = lface_from_face_name (f, face, 1);
3446 LFACE_BACKGROUND (lface) = (STRINGP (new_value)
3447 ? new_value : Qunspecified);
3449 #endif
3451 /* Changing a named face means that all realized faces depending on
3452 that face are invalid. Since we cannot tell which realized faces
3453 depend on the face, make sure they are all removed. This is done
3454 by incrementing face_change_count. The next call to
3455 init_iterator will then free realized faces. */
3456 if (!NILP (face)
3457 && NILP (Fget (face, Qface_no_inherit)))
3459 ++face_change_count;
3460 ++windows_or_buffers_changed;
3465 #ifdef HAVE_WINDOW_SYSTEM
3467 /* Set the `font' frame parameter of FRAME determined from the
3468 font-object set in `default' face attributes LFACE. */
3470 static void
3471 set_font_frame_param (Lisp_Object frame, Lisp_Object lface)
3473 struct frame *f = XFRAME (frame);
3474 Lisp_Object font;
3476 if (FRAME_WINDOW_P (f)
3477 /* Don't do anything if the font is `unspecified'. This can
3478 happen during frame creation. */
3479 && (font = LFACE_FONT (lface),
3480 ! UNSPECIFIEDP (font)))
3482 if (FONT_SPEC_P (font))
3484 font = font_load_for_lface (f, XVECTOR (lface)->contents, font);
3485 if (NILP (font))
3486 return;
3487 LFACE_FONT (lface) = font;
3489 f->default_face_done_p = 0;
3490 Fmodify_frame_parameters (frame, Fcons (Fcons (Qfont, font), Qnil));
3495 /* Get the value of X resource RESOURCE, class CLASS for the display
3496 of frame FRAME. This is here because ordinary `x-get-resource'
3497 doesn't take a frame argument. */
3499 DEFUN ("internal-face-x-get-resource", Finternal_face_x_get_resource,
3500 Sinternal_face_x_get_resource, 3, 3, 0, doc: /* */)
3501 (Lisp_Object resource, Lisp_Object class, Lisp_Object frame)
3503 Lisp_Object value = Qnil;
3504 CHECK_STRING (resource);
3505 CHECK_STRING (class);
3506 CHECK_LIVE_FRAME (frame);
3507 BLOCK_INPUT;
3508 value = display_x_get_resource (FRAME_X_DISPLAY_INFO (XFRAME (frame)),
3509 resource, class, Qnil, Qnil);
3510 UNBLOCK_INPUT;
3511 return value;
3515 /* Return resource string VALUE as a boolean value, i.e. nil, or t.
3516 If VALUE is "on" or "true", return t. If VALUE is "off" or
3517 "false", return nil. Otherwise, if SIGNAL_P is non-zero, signal an
3518 error; if SIGNAL_P is zero, return 0. */
3520 static Lisp_Object
3521 face_boolean_x_resource_value (Lisp_Object value, int signal_p)
3523 Lisp_Object result = make_number (0);
3525 xassert (STRINGP (value));
3527 if (xstrcasecmp (SDATA (value), "on") == 0
3528 || xstrcasecmp (SDATA (value), "true") == 0)
3529 result = Qt;
3530 else if (xstrcasecmp (SDATA (value), "off") == 0
3531 || xstrcasecmp (SDATA (value), "false") == 0)
3532 result = Qnil;
3533 else if (xstrcasecmp (SDATA (value), "unspecified") == 0)
3534 result = Qunspecified;
3535 else if (signal_p)
3536 signal_error ("Invalid face attribute value from X resource", value);
3538 return result;
3542 DEFUN ("internal-set-lisp-face-attribute-from-resource",
3543 Finternal_set_lisp_face_attribute_from_resource,
3544 Sinternal_set_lisp_face_attribute_from_resource,
3545 3, 4, 0, doc: /* */)
3546 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
3548 CHECK_SYMBOL (face);
3549 CHECK_SYMBOL (attr);
3550 CHECK_STRING (value);
3552 if (xstrcasecmp (SDATA (value), "unspecified") == 0)
3553 value = Qunspecified;
3554 else if (EQ (attr, QCheight))
3556 value = Fstring_to_number (value, make_number (10));
3557 if (XINT (value) <= 0)
3558 signal_error ("Invalid face height from X resource", value);
3560 else if (EQ (attr, QCbold) || EQ (attr, QCitalic))
3561 value = face_boolean_x_resource_value (value, 1);
3562 else if (EQ (attr, QCweight) || EQ (attr, QCslant) || EQ (attr, QCwidth))
3563 value = intern (SDATA (value));
3564 else if (EQ (attr, QCreverse_video) || EQ (attr, QCinverse_video))
3565 value = face_boolean_x_resource_value (value, 1);
3566 else if (EQ (attr, QCunderline)
3567 || EQ (attr, QCoverline)
3568 || EQ (attr, QCstrike_through))
3570 Lisp_Object boolean_value;
3572 /* If the result of face_boolean_x_resource_value is t or nil,
3573 VALUE does NOT specify a color. */
3574 boolean_value = face_boolean_x_resource_value (value, 0);
3575 if (SYMBOLP (boolean_value))
3576 value = boolean_value;
3578 else if (EQ (attr, QCbox) || EQ (attr, QCinherit))
3579 value = Fcar (Fread_from_string (value, Qnil, Qnil));
3581 return Finternal_set_lisp_face_attribute (face, attr, value, frame);
3584 #endif /* HAVE_WINDOW_SYSTEM */
3587 /***********************************************************************
3588 Menu face
3589 ***********************************************************************/
3591 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
3593 /* Make menus on frame F appear as specified by the `menu' face. */
3595 static void
3596 x_update_menu_appearance (struct frame *f)
3598 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3599 XrmDatabase rdb;
3601 if (dpyinfo
3602 && (rdb = XrmGetDatabase (FRAME_X_DISPLAY (f)),
3603 rdb != NULL))
3605 char line[512];
3606 Lisp_Object lface = lface_from_face_name (f, Qmenu, 1);
3607 struct face *face = FACE_FROM_ID (f, MENU_FACE_ID);
3608 const char *myname = SDATA (Vx_resource_name);
3609 int changed_p = 0;
3610 #ifdef USE_MOTIF
3611 const char *popup_path = "popup_menu";
3612 #else
3613 const char *popup_path = "menu.popup";
3614 #endif
3616 if (STRINGP (LFACE_FOREGROUND (lface)))
3618 sprintf (line, "%s.%s*foreground: %s",
3619 myname, popup_path,
3620 SDATA (LFACE_FOREGROUND (lface)));
3621 XrmPutLineResource (&rdb, line);
3622 sprintf (line, "%s.pane.menubar*foreground: %s",
3623 myname, SDATA (LFACE_FOREGROUND (lface)));
3624 XrmPutLineResource (&rdb, line);
3625 changed_p = 1;
3628 if (STRINGP (LFACE_BACKGROUND (lface)))
3630 sprintf (line, "%s.%s*background: %s",
3631 myname, popup_path,
3632 SDATA (LFACE_BACKGROUND (lface)));
3633 XrmPutLineResource (&rdb, line);
3634 sprintf (line, "%s.pane.menubar*background: %s",
3635 myname, SDATA (LFACE_BACKGROUND (lface)));
3636 XrmPutLineResource (&rdb, line);
3637 changed_p = 1;
3640 if (face->font
3641 /* On Solaris 5.8, it's been reported that the `menu' face
3642 can be unspecified here, during startup. Why this
3643 happens remains unknown. -- cyd */
3644 && FONTP (LFACE_FONT (lface))
3645 && (!UNSPECIFIEDP (LFACE_FAMILY (lface))
3646 || !UNSPECIFIEDP (LFACE_FOUNDRY (lface))
3647 || !UNSPECIFIEDP (LFACE_SWIDTH (lface))
3648 || !UNSPECIFIEDP (LFACE_WEIGHT (lface))
3649 || !UNSPECIFIEDP (LFACE_SLANT (lface))
3650 || !UNSPECIFIEDP (LFACE_HEIGHT (lface))))
3652 Lisp_Object xlfd = Ffont_xlfd_name (LFACE_FONT (lface), Qnil);
3653 #ifdef USE_MOTIF
3654 const char *suffix = "List";
3655 Bool motif = True;
3656 #else
3657 #if defined HAVE_X_I18N
3659 const char *suffix = "Set";
3660 #else
3661 const char *suffix = "";
3662 #endif
3663 Bool motif = False;
3664 #endif
3666 if (! NILP (xlfd))
3668 #if defined HAVE_X_I18N
3669 char *fontsetname = xic_create_fontsetname (SDATA (xlfd), motif);
3670 #else
3671 char *fontsetname = (char *) SDATA (xlfd);
3672 #endif
3673 sprintf (line, "%s.pane.menubar*font%s: %s",
3674 myname, suffix, fontsetname);
3675 XrmPutLineResource (&rdb, line);
3676 sprintf (line, "%s.%s*font%s: %s",
3677 myname, popup_path, suffix, fontsetname);
3678 XrmPutLineResource (&rdb, line);
3679 changed_p = 1;
3680 if (fontsetname != (char *) SDATA (xlfd))
3681 xfree (fontsetname);
3685 if (changed_p && f->output_data.x->menubar_widget)
3686 free_frame_menubar (f);
3690 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
3693 DEFUN ("face-attribute-relative-p", Fface_attribute_relative_p,
3694 Sface_attribute_relative_p,
3695 2, 2, 0,
3696 doc: /* Check whether a face attribute value is relative.
3697 Specifically, this function returns t if the attribute ATTRIBUTE
3698 with the value VALUE is relative.
3700 A relative value is one that doesn't entirely override whatever is
3701 inherited from another face. For most possible attributes,
3702 the only relative value that users see is `unspecified'.
3703 However, for :height, floating point values are also relative. */)
3704 (Lisp_Object attribute, Lisp_Object value)
3706 if (EQ (value, Qunspecified) || (EQ (value, Qignore_defface)))
3707 return Qt;
3708 else if (EQ (attribute, QCheight))
3709 return INTEGERP (value) ? Qnil : Qt;
3710 else
3711 return Qnil;
3714 DEFUN ("merge-face-attribute", Fmerge_face_attribute, Smerge_face_attribute,
3715 3, 3, 0,
3716 doc: /* Return face ATTRIBUTE VALUE1 merged with VALUE2.
3717 If VALUE1 or VALUE2 are absolute (see `face-attribute-relative-p'), then
3718 the result will be absolute, otherwise it will be relative. */)
3719 (Lisp_Object attribute, Lisp_Object value1, Lisp_Object value2)
3721 if (EQ (value1, Qunspecified) || EQ (value1, Qignore_defface))
3722 return value2;
3723 else if (EQ (attribute, QCheight))
3724 return merge_face_heights (value1, value2, value1);
3725 else
3726 return value1;
3730 DEFUN ("internal-get-lisp-face-attribute", Finternal_get_lisp_face_attribute,
3731 Sinternal_get_lisp_face_attribute,
3732 2, 3, 0,
3733 doc: /* Return face attribute KEYWORD of face SYMBOL.
3734 If SYMBOL does not name a valid Lisp face or KEYWORD isn't a valid
3735 face attribute name, signal an error.
3736 If the optional argument FRAME is given, report on face SYMBOL in that
3737 frame. If FRAME is t, report on the defaults for face SYMBOL (for new
3738 frames). If FRAME is omitted or nil, use the selected frame. */)
3739 (Lisp_Object symbol, Lisp_Object keyword, Lisp_Object frame)
3741 Lisp_Object lface, value = Qnil;
3743 CHECK_SYMBOL (symbol);
3744 CHECK_SYMBOL (keyword);
3746 if (EQ (frame, Qt))
3747 lface = lface_from_face_name (NULL, symbol, 1);
3748 else
3750 if (NILP (frame))
3751 frame = selected_frame;
3752 CHECK_LIVE_FRAME (frame);
3753 lface = lface_from_face_name (XFRAME (frame), symbol, 1);
3756 if (EQ (keyword, QCfamily))
3757 value = LFACE_FAMILY (lface);
3758 else if (EQ (keyword, QCfoundry))
3759 value = LFACE_FOUNDRY (lface);
3760 else if (EQ (keyword, QCheight))
3761 value = LFACE_HEIGHT (lface);
3762 else if (EQ (keyword, QCweight))
3763 value = LFACE_WEIGHT (lface);
3764 else if (EQ (keyword, QCslant))
3765 value = LFACE_SLANT (lface);
3766 else if (EQ (keyword, QCunderline))
3767 value = LFACE_UNDERLINE (lface);
3768 else if (EQ (keyword, QCoverline))
3769 value = LFACE_OVERLINE (lface);
3770 else if (EQ (keyword, QCstrike_through))
3771 value = LFACE_STRIKE_THROUGH (lface);
3772 else if (EQ (keyword, QCbox))
3773 value = LFACE_BOX (lface);
3774 else if (EQ (keyword, QCinverse_video)
3775 || EQ (keyword, QCreverse_video))
3776 value = LFACE_INVERSE (lface);
3777 else if (EQ (keyword, QCforeground))
3778 value = LFACE_FOREGROUND (lface);
3779 else if (EQ (keyword, QCbackground))
3780 value = LFACE_BACKGROUND (lface);
3781 else if (EQ (keyword, QCstipple))
3782 value = LFACE_STIPPLE (lface);
3783 else if (EQ (keyword, QCwidth))
3784 value = LFACE_SWIDTH (lface);
3785 else if (EQ (keyword, QCinherit))
3786 value = LFACE_INHERIT (lface);
3787 else if (EQ (keyword, QCfont))
3788 value = LFACE_FONT (lface);
3789 else if (EQ (keyword, QCfontset))
3790 value = LFACE_FONTSET (lface);
3791 else
3792 signal_error ("Invalid face attribute name", keyword);
3794 if (IGNORE_DEFFACE_P (value))
3795 return Qunspecified;
3797 return value;
3801 DEFUN ("internal-lisp-face-attribute-values",
3802 Finternal_lisp_face_attribute_values,
3803 Sinternal_lisp_face_attribute_values, 1, 1, 0,
3804 doc: /* Return a list of valid discrete values for face attribute ATTR.
3805 Value is nil if ATTR doesn't have a discrete set of valid values. */)
3806 (Lisp_Object attr)
3808 Lisp_Object result = Qnil;
3810 CHECK_SYMBOL (attr);
3812 if (EQ (attr, QCunderline))
3813 result = Fcons (Qt, Fcons (Qnil, Qnil));
3814 else if (EQ (attr, QCoverline))
3815 result = Fcons (Qt, Fcons (Qnil, Qnil));
3816 else if (EQ (attr, QCstrike_through))
3817 result = Fcons (Qt, Fcons (Qnil, Qnil));
3818 else if (EQ (attr, QCinverse_video) || EQ (attr, QCreverse_video))
3819 result = Fcons (Qt, Fcons (Qnil, Qnil));
3821 return result;
3825 DEFUN ("internal-merge-in-global-face", Finternal_merge_in_global_face,
3826 Sinternal_merge_in_global_face, 2, 2, 0,
3827 doc: /* Add attributes from frame-default definition of FACE to FACE on FRAME.
3828 Default face attributes override any local face attributes. */)
3829 (Lisp_Object face, Lisp_Object frame)
3831 int i;
3832 Lisp_Object global_lface, local_lface, *gvec, *lvec;
3833 struct frame *f = XFRAME (frame);
3835 CHECK_LIVE_FRAME (frame);
3836 global_lface = lface_from_face_name (NULL, face, 1);
3837 local_lface = lface_from_face_name (f, face, 0);
3838 if (NILP (local_lface))
3839 local_lface = Finternal_make_lisp_face (face, frame);
3841 /* Make every specified global attribute override the local one.
3842 BEWARE!! This is only used from `face-set-after-frame-default' where
3843 the local frame is defined from default specs in `face-defface-spec'
3844 and those should be overridden by global settings. Hence the strange
3845 "global before local" priority. */
3846 lvec = XVECTOR (local_lface)->contents;
3847 gvec = XVECTOR (global_lface)->contents;
3848 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3849 if (IGNORE_DEFFACE_P (gvec[i]))
3850 lvec[i] = Qunspecified;
3851 else if (! UNSPECIFIEDP (gvec[i]))
3852 lvec[i] = gvec[i];
3854 /* If the default face was changed, update the face cache and the
3855 `font' frame parameter. */
3856 if (EQ (face, Qdefault))
3858 struct face_cache *c = FRAME_FACE_CACHE (f);
3859 struct face *newface, *oldface = FACE_FROM_ID (f, DEFAULT_FACE_ID);
3860 Lisp_Object attrs[LFACE_VECTOR_SIZE];
3862 /* This can be NULL (e.g., in batch mode). */
3863 if (oldface)
3865 /* Ensure that the face vector is fully specified by merging
3866 the previously-cached vector. */
3867 memcpy (attrs, oldface->lface, sizeof attrs);
3868 merge_face_vectors (f, lvec, attrs, 0);
3869 memcpy (lvec, attrs, sizeof attrs);
3870 newface = realize_face (c, lvec, DEFAULT_FACE_ID);
3872 if ((! UNSPECIFIEDP (gvec[LFACE_FAMILY_INDEX])
3873 || ! UNSPECIFIEDP (gvec[LFACE_FOUNDRY_INDEX])
3874 || ! UNSPECIFIEDP (gvec[LFACE_HEIGHT_INDEX])
3875 || ! UNSPECIFIEDP (gvec[LFACE_WEIGHT_INDEX])
3876 || ! UNSPECIFIEDP (gvec[LFACE_SLANT_INDEX])
3877 || ! UNSPECIFIEDP (gvec[LFACE_SWIDTH_INDEX])
3878 || ! UNSPECIFIEDP (gvec[LFACE_FONT_INDEX]))
3879 && newface->font)
3881 Lisp_Object name = newface->font->props[FONT_NAME_INDEX];
3882 Fmodify_frame_parameters (frame, Fcons (Fcons (Qfont, name),
3883 Qnil));
3888 return Qnil;
3892 /* The following function is implemented for compatibility with 20.2.
3893 The function is used in x-resolve-fonts when it is asked to
3894 return fonts with the same size as the font of a face. This is
3895 done in fontset.el. */
3897 DEFUN ("face-font", Fface_font, Sface_font, 1, 3, 0,
3898 doc: /* Return the font name of face FACE, or nil if it is unspecified.
3899 The font name is, by default, for ASCII characters.
3900 If the optional argument FRAME is given, report on face FACE in that frame.
3901 If FRAME is t, report on the defaults for face FACE (for new frames).
3902 The font default for a face is either nil, or a list
3903 of the form (bold), (italic) or (bold italic).
3904 If FRAME is omitted or nil, use the selected frame. And, in this case,
3905 if the optional third argument CHARACTER is given,
3906 return the font name used for CHARACTER. */)
3907 (Lisp_Object face, Lisp_Object frame, Lisp_Object character)
3909 if (EQ (frame, Qt))
3911 Lisp_Object result = Qnil;
3912 Lisp_Object lface = lface_from_face_name (NULL, face, 1);
3914 if (!UNSPECIFIEDP (LFACE_WEIGHT (lface))
3915 && !EQ (LFACE_WEIGHT (lface), Qnormal))
3916 result = Fcons (Qbold, result);
3918 if (!UNSPECIFIEDP (LFACE_SLANT (lface))
3919 && !EQ (LFACE_SLANT (lface), Qnormal))
3920 result = Fcons (Qitalic, result);
3922 return result;
3924 else
3926 struct frame *f = frame_or_selected_frame (frame, 1);
3927 int face_id = lookup_named_face (f, face, 1);
3928 struct face *face = FACE_FROM_ID (f, face_id);
3930 if (! face)
3931 return Qnil;
3932 #ifdef HAVE_WINDOW_SYSTEM
3933 if (FRAME_WINDOW_P (f) && !NILP (character))
3935 CHECK_CHARACTER (character);
3936 face_id = FACE_FOR_CHAR (f, face, XINT (character), -1, Qnil);
3937 face = FACE_FROM_ID (f, face_id);
3939 return (face->font
3940 ? face->font->props[FONT_NAME_INDEX]
3941 : Qnil);
3942 #else /* !HAVE_WINDOW_SYSTEM */
3943 return build_string (FRAME_MSDOS_P (f)
3944 ? "ms-dos"
3945 : FRAME_W32_P (f) ? "w32term"
3946 :"tty");
3947 #endif
3952 /* Compare face-attribute values v1 and v2 for equality. Value is non-zero if
3953 all attributes are `equal'. Tries to be fast because this function
3954 is called quite often. */
3956 static INLINE int
3957 face_attr_equal_p (Lisp_Object v1, Lisp_Object v2)
3959 /* Type can differ, e.g. when one attribute is unspecified, i.e. nil,
3960 and the other is specified. */
3961 if (XTYPE (v1) != XTYPE (v2))
3962 return 0;
3964 if (EQ (v1, v2))
3965 return 1;
3967 switch (XTYPE (v1))
3969 case Lisp_String:
3970 if (SBYTES (v1) != SBYTES (v2))
3971 return 0;
3973 return memcmp (SDATA (v1), SDATA (v2), SBYTES (v1)) == 0;
3975 case_Lisp_Int:
3976 case Lisp_Symbol:
3977 return 0;
3979 default:
3980 return !NILP (Fequal (v1, v2));
3985 /* Compare face vectors V1 and V2 for equality. Value is non-zero if
3986 all attributes are `equal'. Tries to be fast because this function
3987 is called quite often. */
3989 static INLINE int
3990 lface_equal_p (Lisp_Object *v1, Lisp_Object *v2)
3992 int i, equal_p = 1;
3994 for (i = 1; i < LFACE_VECTOR_SIZE && equal_p; ++i)
3995 equal_p = face_attr_equal_p (v1[i], v2[i]);
3997 return equal_p;
4001 DEFUN ("internal-lisp-face-equal-p", Finternal_lisp_face_equal_p,
4002 Sinternal_lisp_face_equal_p, 2, 3, 0,
4003 doc: /* True if FACE1 and FACE2 are equal.
4004 If the optional argument FRAME is given, report on FACE1 and FACE2 in that frame.
4005 If FRAME is t, report on the defaults for FACE1 and FACE2 (for new frames).
4006 If FRAME is omitted or nil, use the selected frame. */)
4007 (Lisp_Object face1, Lisp_Object face2, Lisp_Object frame)
4009 int equal_p;
4010 struct frame *f;
4011 Lisp_Object lface1, lface2;
4013 if (EQ (frame, Qt))
4014 f = NULL;
4015 else
4016 /* Don't use check_x_frame here because this function is called
4017 before X frames exist. At that time, if FRAME is nil,
4018 selected_frame will be used which is the frame dumped with
4019 Emacs. That frame is not an X frame. */
4020 f = frame_or_selected_frame (frame, 2);
4022 lface1 = lface_from_face_name (f, face1, 1);
4023 lface2 = lface_from_face_name (f, face2, 1);
4024 equal_p = lface_equal_p (XVECTOR (lface1)->contents,
4025 XVECTOR (lface2)->contents);
4026 return equal_p ? Qt : Qnil;
4030 DEFUN ("internal-lisp-face-empty-p", Finternal_lisp_face_empty_p,
4031 Sinternal_lisp_face_empty_p, 1, 2, 0,
4032 doc: /* True if FACE has no attribute specified.
4033 If the optional argument FRAME is given, report on face FACE in that frame.
4034 If FRAME is t, report on the defaults for face FACE (for new frames).
4035 If FRAME is omitted or nil, use the selected frame. */)
4036 (Lisp_Object face, Lisp_Object frame)
4038 struct frame *f;
4039 Lisp_Object lface;
4040 int i;
4042 if (NILP (frame))
4043 frame = selected_frame;
4044 CHECK_LIVE_FRAME (frame);
4045 f = XFRAME (frame);
4047 if (EQ (frame, Qt))
4048 lface = lface_from_face_name (NULL, face, 1);
4049 else
4050 lface = lface_from_face_name (f, face, 1);
4052 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
4053 if (!UNSPECIFIEDP (AREF (lface, i)))
4054 break;
4056 return i == LFACE_VECTOR_SIZE ? Qt : Qnil;
4060 DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist,
4061 0, 1, 0,
4062 doc: /* Return an alist of frame-local faces defined on FRAME.
4063 For internal use only. */)
4064 (Lisp_Object frame)
4066 struct frame *f = frame_or_selected_frame (frame, 0);
4067 return f->face_alist;
4071 /* Return a hash code for Lisp string STRING with case ignored. Used
4072 below in computing a hash value for a Lisp face. */
4074 static INLINE unsigned
4075 hash_string_case_insensitive (Lisp_Object string)
4077 const unsigned char *s;
4078 unsigned hash = 0;
4079 xassert (STRINGP (string));
4080 for (s = SDATA (string); *s; ++s)
4081 hash = (hash << 1) ^ tolower (*s);
4082 return hash;
4086 /* Return a hash code for face attribute vector V. */
4088 static INLINE unsigned
4089 lface_hash (Lisp_Object *v)
4091 return (hash_string_case_insensitive (v[LFACE_FAMILY_INDEX])
4092 ^ hash_string_case_insensitive (v[LFACE_FOUNDRY_INDEX])
4093 ^ hash_string_case_insensitive (v[LFACE_FOREGROUND_INDEX])
4094 ^ hash_string_case_insensitive (v[LFACE_BACKGROUND_INDEX])
4095 ^ XHASH (v[LFACE_WEIGHT_INDEX])
4096 ^ XHASH (v[LFACE_SLANT_INDEX])
4097 ^ XHASH (v[LFACE_SWIDTH_INDEX])
4098 ^ XHASH (v[LFACE_HEIGHT_INDEX]));
4102 /* Return non-zero if LFACE1 and LFACE2 specify the same font (without
4103 considering charsets/registries). They do if they specify the same
4104 family, point size, weight, width, slant, and font. Both
4105 LFACE1 and LFACE2 must be fully-specified. */
4107 static INLINE int
4108 lface_same_font_attributes_p (Lisp_Object *lface1, Lisp_Object *lface2)
4110 xassert (lface_fully_specified_p (lface1)
4111 && lface_fully_specified_p (lface2));
4112 return (xstrcasecmp (SDATA (lface1[LFACE_FAMILY_INDEX]),
4113 SDATA (lface2[LFACE_FAMILY_INDEX])) == 0
4114 && xstrcasecmp (SDATA (lface1[LFACE_FOUNDRY_INDEX]),
4115 SDATA (lface2[LFACE_FOUNDRY_INDEX])) == 0
4116 && EQ (lface1[LFACE_HEIGHT_INDEX], lface2[LFACE_HEIGHT_INDEX])
4117 && EQ (lface1[LFACE_SWIDTH_INDEX], lface2[LFACE_SWIDTH_INDEX])
4118 && EQ (lface1[LFACE_WEIGHT_INDEX], lface2[LFACE_WEIGHT_INDEX])
4119 && EQ (lface1[LFACE_SLANT_INDEX], lface2[LFACE_SLANT_INDEX])
4120 && EQ (lface1[LFACE_FONT_INDEX], lface2[LFACE_FONT_INDEX])
4121 && (EQ (lface1[LFACE_FONTSET_INDEX], lface2[LFACE_FONTSET_INDEX])
4122 || (STRINGP (lface1[LFACE_FONTSET_INDEX])
4123 && STRINGP (lface2[LFACE_FONTSET_INDEX])
4124 && ! xstrcasecmp (SDATA (lface1[LFACE_FONTSET_INDEX]),
4125 SDATA (lface2[LFACE_FONTSET_INDEX]))))
4131 /***********************************************************************
4132 Realized Faces
4133 ***********************************************************************/
4135 /* Allocate and return a new realized face for Lisp face attribute
4136 vector ATTR. */
4138 static struct face *
4139 make_realized_face (Lisp_Object *attr)
4141 struct face *face = (struct face *) xmalloc (sizeof *face);
4142 memset (face, 0, sizeof *face);
4143 face->ascii_face = face;
4144 memcpy (face->lface, attr, sizeof face->lface);
4145 return face;
4149 /* Free realized face FACE, including its X resources. FACE may
4150 be null. */
4152 void
4153 free_realized_face (struct frame *f, struct face *face)
4155 if (face)
4157 #ifdef HAVE_WINDOW_SYSTEM
4158 if (FRAME_WINDOW_P (f))
4160 /* Free fontset of FACE if it is ASCII face. */
4161 if (face->fontset >= 0 && face == face->ascii_face)
4162 free_face_fontset (f, face);
4163 if (face->gc)
4165 BLOCK_INPUT;
4166 if (face->font)
4167 font_done_for_face (f, face);
4168 x_free_gc (f, face->gc);
4169 face->gc = 0;
4170 UNBLOCK_INPUT;
4173 free_face_colors (f, face);
4174 x_destroy_bitmap (f, face->stipple);
4176 #endif /* HAVE_WINDOW_SYSTEM */
4178 xfree (face);
4183 /* Prepare face FACE for subsequent display on frame F. This
4184 allocated GCs if they haven't been allocated yet or have been freed
4185 by clearing the face cache. */
4187 void
4188 prepare_face_for_display (struct frame *f, struct face *face)
4190 #ifdef HAVE_WINDOW_SYSTEM
4191 xassert (FRAME_WINDOW_P (f));
4193 if (face->gc == 0)
4195 XGCValues xgcv;
4196 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
4198 xgcv.foreground = face->foreground;
4199 xgcv.background = face->background;
4200 #ifdef HAVE_X_WINDOWS
4201 xgcv.graphics_exposures = False;
4202 #endif
4204 BLOCK_INPUT;
4205 #ifdef HAVE_X_WINDOWS
4206 if (face->stipple)
4208 xgcv.fill_style = FillOpaqueStippled;
4209 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
4210 mask |= GCFillStyle | GCStipple;
4212 #endif
4213 face->gc = x_create_gc (f, mask, &xgcv);
4214 if (face->font)
4215 font_prepare_for_face (f, face);
4216 UNBLOCK_INPUT;
4218 #endif /* HAVE_WINDOW_SYSTEM */
4222 /* Returns the `distance' between the colors X and Y. */
4224 static int
4225 color_distance (XColor *x, XColor *y)
4227 /* This formula is from a paper title `Colour metric' by Thiadmer Riemersma.
4228 Quoting from that paper:
4230 This formula has results that are very close to L*u*v* (with the
4231 modified lightness curve) and, more importantly, it is a more even
4232 algorithm: it does not have a range of colours where it suddenly
4233 gives far from optimal results.
4235 See <http://www.compuphase.com/cmetric.htm> for more info. */
4237 long r = (x->red - y->red) >> 8;
4238 long g = (x->green - y->green) >> 8;
4239 long b = (x->blue - y->blue) >> 8;
4240 long r_mean = (x->red + y->red) >> 9;
4242 return
4243 (((512 + r_mean) * r * r) >> 8)
4244 + 4 * g * g
4245 + (((767 - r_mean) * b * b) >> 8);
4249 DEFUN ("color-distance", Fcolor_distance, Scolor_distance, 2, 3, 0,
4250 doc: /* Return an integer distance between COLOR1 and COLOR2 on FRAME.
4251 COLOR1 and COLOR2 may be either strings containing the color name,
4252 or lists of the form (RED GREEN BLUE).
4253 If FRAME is unspecified or nil, the current frame is used. */)
4254 (Lisp_Object color1, Lisp_Object color2, Lisp_Object frame)
4256 struct frame *f;
4257 XColor cdef1, cdef2;
4259 if (NILP (frame))
4260 frame = selected_frame;
4261 CHECK_LIVE_FRAME (frame);
4262 f = XFRAME (frame);
4264 if (!(CONSP (color1) && parse_rgb_list (color1, &cdef1))
4265 && !(STRINGP (color1) && defined_color (f, SDATA (color1), &cdef1, 0)))
4266 signal_error ("Invalid color", color1);
4267 if (!(CONSP (color2) && parse_rgb_list (color2, &cdef2))
4268 && !(STRINGP (color2) && defined_color (f, SDATA (color2), &cdef2, 0)))
4269 signal_error ("Invalid color", color2);
4271 return make_number (color_distance (&cdef1, &cdef2));
4275 /***********************************************************************
4276 Face Cache
4277 ***********************************************************************/
4279 /* Return a new face cache for frame F. */
4281 static struct face_cache *
4282 make_face_cache (struct frame *f)
4284 struct face_cache *c;
4285 int size;
4287 c = (struct face_cache *) xmalloc (sizeof *c);
4288 memset (c, 0, sizeof *c);
4289 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4290 c->buckets = (struct face **) xmalloc (size);
4291 memset (c->buckets, 0, size);
4292 c->size = 50;
4293 c->faces_by_id = (struct face **) xmalloc (c->size * sizeof *c->faces_by_id);
4294 c->f = f;
4295 c->menu_face_changed_p = menu_face_changed_default;
4296 return c;
4300 /* Clear out all graphics contexts for all realized faces, except for
4301 the basic faces. This should be done from time to time just to avoid
4302 keeping too many graphics contexts that are no longer needed. */
4304 static void
4305 clear_face_gcs (struct face_cache *c)
4307 if (c && FRAME_WINDOW_P (c->f))
4309 #ifdef HAVE_WINDOW_SYSTEM
4310 int i;
4311 for (i = BASIC_FACE_ID_SENTINEL; i < c->used; ++i)
4313 struct face *face = c->faces_by_id[i];
4314 if (face && face->gc)
4316 BLOCK_INPUT;
4317 if (face->font)
4318 font_done_for_face (c->f, face);
4319 x_free_gc (c->f, face->gc);
4320 face->gc = 0;
4321 UNBLOCK_INPUT;
4324 #endif /* HAVE_WINDOW_SYSTEM */
4329 /* Free all realized faces in face cache C, including basic faces.
4330 C may be null. If faces are freed, make sure the frame's current
4331 matrix is marked invalid, so that a display caused by an expose
4332 event doesn't try to use faces we destroyed. */
4334 static void
4335 free_realized_faces (struct face_cache *c)
4337 if (c && c->used)
4339 int i, size;
4340 struct frame *f = c->f;
4342 /* We must block input here because we can't process X events
4343 safely while only some faces are freed, or when the frame's
4344 current matrix still references freed faces. */
4345 BLOCK_INPUT;
4347 for (i = 0; i < c->used; ++i)
4349 free_realized_face (f, c->faces_by_id[i]);
4350 c->faces_by_id[i] = NULL;
4353 c->used = 0;
4354 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4355 memset (c->buckets, 0, size);
4357 /* Must do a thorough redisplay the next time. Mark current
4358 matrices as invalid because they will reference faces freed
4359 above. This function is also called when a frame is
4360 destroyed. In this case, the root window of F is nil. */
4361 if (WINDOWP (f->root_window))
4363 clear_current_matrices (f);
4364 ++windows_or_buffers_changed;
4367 UNBLOCK_INPUT;
4372 /* Free all realized faces that are using FONTSET on frame F. */
4374 void
4375 free_realized_faces_for_fontset (struct frame *f, int fontset)
4377 struct face_cache *cache = FRAME_FACE_CACHE (f);
4378 struct face *face;
4379 int i;
4381 /* We must block input here because we can't process X events safely
4382 while only some faces are freed, or when the frame's current
4383 matrix still references freed faces. */
4384 BLOCK_INPUT;
4386 for (i = 0; i < cache->used; i++)
4388 face = cache->faces_by_id[i];
4389 if (face
4390 && face->fontset == fontset)
4392 uncache_face (cache, face);
4393 free_realized_face (f, face);
4397 /* Must do a thorough redisplay the next time. Mark current
4398 matrices as invalid because they will reference faces freed
4399 above. This function is also called when a frame is destroyed.
4400 In this case, the root window of F is nil. */
4401 if (WINDOWP (f->root_window))
4403 clear_current_matrices (f);
4404 ++windows_or_buffers_changed;
4407 UNBLOCK_INPUT;
4411 /* Free all realized faces on FRAME or on all frames if FRAME is nil.
4412 This is done after attributes of a named face have been changed,
4413 because we can't tell which realized faces depend on that face. */
4415 void
4416 free_all_realized_faces (Lisp_Object frame)
4418 if (NILP (frame))
4420 Lisp_Object rest;
4421 FOR_EACH_FRAME (rest, frame)
4422 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4424 else
4425 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4429 /* Free face cache C and faces in it, including their X resources. */
4431 static void
4432 free_face_cache (struct face_cache *c)
4434 if (c)
4436 free_realized_faces (c);
4437 xfree (c->buckets);
4438 xfree (c->faces_by_id);
4439 xfree (c);
4444 /* Cache realized face FACE in face cache C. HASH is the hash value
4445 of FACE. If FACE is for ASCII characters (i.e. FACE->ascii_face ==
4446 FACE), insert the new face to the beginning of the collision list
4447 of the face hash table of C. Otherwise, add the new face to the
4448 end of the collision list. This way, lookup_face can quickly find
4449 that a requested face is not cached. */
4451 static void
4452 cache_face (struct face_cache *c, struct face *face, unsigned int hash)
4454 int i = hash % FACE_CACHE_BUCKETS_SIZE;
4456 face->hash = hash;
4458 if (face->ascii_face != face)
4460 struct face *last = c->buckets[i];
4461 if (last)
4463 while (last->next)
4464 last = last->next;
4465 last->next = face;
4466 face->prev = last;
4467 face->next = NULL;
4469 else
4471 c->buckets[i] = face;
4472 face->prev = face->next = NULL;
4475 else
4477 face->prev = NULL;
4478 face->next = c->buckets[i];
4479 if (face->next)
4480 face->next->prev = face;
4481 c->buckets[i] = face;
4484 /* Find a free slot in C->faces_by_id and use the index of the free
4485 slot as FACE->id. */
4486 for (i = 0; i < c->used; ++i)
4487 if (c->faces_by_id[i] == NULL)
4488 break;
4489 face->id = i;
4491 /* Maybe enlarge C->faces_by_id. */
4492 if (i == c->used)
4494 if (c->used == c->size)
4496 int new_size, sz;
4497 new_size = min (2 * c->size, MAX_FACE_ID);
4498 if (new_size == c->size)
4499 abort (); /* Alternatives? ++kfs */
4500 sz = new_size * sizeof *c->faces_by_id;
4501 c->faces_by_id = (struct face **) xrealloc (c->faces_by_id, sz);
4502 c->size = new_size;
4504 c->used++;
4507 #if GLYPH_DEBUG
4508 /* Check that FACE got a unique id. */
4510 int j, n;
4511 struct face *face;
4513 for (j = n = 0; j < FACE_CACHE_BUCKETS_SIZE; ++j)
4514 for (face = c->buckets[j]; face; face = face->next)
4515 if (face->id == i)
4516 ++n;
4518 xassert (n == 1);
4520 #endif /* GLYPH_DEBUG */
4522 c->faces_by_id[i] = face;
4526 /* Remove face FACE from cache C. */
4528 static void
4529 uncache_face (struct face_cache *c, struct face *face)
4531 int i = face->hash % FACE_CACHE_BUCKETS_SIZE;
4533 if (face->prev)
4534 face->prev->next = face->next;
4535 else
4536 c->buckets[i] = face->next;
4538 if (face->next)
4539 face->next->prev = face->prev;
4541 c->faces_by_id[face->id] = NULL;
4542 if (face->id == c->used)
4543 --c->used;
4547 /* Look up a realized face with face attributes ATTR in the face cache
4548 of frame F. The face will be used to display ASCII characters.
4549 Value is the ID of the face found. If no suitable face is found,
4550 realize a new one. */
4552 INLINE int
4553 lookup_face (struct frame *f, Lisp_Object *attr)
4555 struct face_cache *cache = FRAME_FACE_CACHE (f);
4556 unsigned hash;
4557 int i;
4558 struct face *face;
4560 xassert (cache != NULL);
4561 check_lface_attrs (attr);
4563 /* Look up ATTR in the face cache. */
4564 hash = lface_hash (attr);
4565 i = hash % FACE_CACHE_BUCKETS_SIZE;
4567 for (face = cache->buckets[i]; face; face = face->next)
4569 if (face->ascii_face != face)
4571 /* There's no more ASCII face. */
4572 face = NULL;
4573 break;
4575 if (face->hash == hash
4576 && lface_equal_p (face->lface, attr))
4577 break;
4580 /* If not found, realize a new face. */
4581 if (face == NULL)
4582 face = realize_face (cache, attr, -1);
4584 #if GLYPH_DEBUG
4585 xassert (face == FACE_FROM_ID (f, face->id));
4586 #endif /* GLYPH_DEBUG */
4588 return face->id;
4591 #ifdef HAVE_WINDOW_SYSTEM
4592 /* Look up a realized face that has the same attributes as BASE_FACE
4593 except for the font in the face cache of frame F. If FONT-OBJECT
4594 is not nil, it is an already opened font. If FONT-OBJECT is nil,
4595 the face has no font. Value is the ID of the face found. If no
4596 suitable face is found, realize a new one. */
4599 face_for_font (struct frame *f, Lisp_Object font_object, struct face *base_face)
4601 struct face_cache *cache = FRAME_FACE_CACHE (f);
4602 unsigned hash;
4603 int i;
4604 struct face *face;
4606 xassert (cache != NULL);
4607 base_face = base_face->ascii_face;
4608 hash = lface_hash (base_face->lface);
4609 i = hash % FACE_CACHE_BUCKETS_SIZE;
4611 for (face = cache->buckets[i]; face; face = face->next)
4613 if (face->ascii_face == face)
4614 continue;
4615 if (face->ascii_face == base_face
4616 && face->font == (NILP (font_object) ? NULL
4617 : XFONT_OBJECT (font_object))
4618 && lface_equal_p (face->lface, base_face->lface))
4619 return face->id;
4622 /* If not found, realize a new face. */
4623 face = realize_non_ascii_face (f, font_object, base_face);
4624 return face->id;
4626 #endif /* HAVE_WINDOW_SYSTEM */
4628 /* Return the face id of the realized face for named face SYMBOL on
4629 frame F suitable for displaying ASCII characters. Value is -1 if
4630 the face couldn't be determined, which might happen if the default
4631 face isn't realized and cannot be realized. */
4634 lookup_named_face (struct frame *f, Lisp_Object symbol, int signal_p)
4636 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4637 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4638 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4640 if (default_face == NULL)
4642 if (!realize_basic_faces (f))
4643 return -1;
4644 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4645 if (default_face == NULL)
4646 abort (); /* realize_basic_faces must have set it up */
4649 if (! get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4650 return -1;
4652 memcpy (attrs, default_face->lface, sizeof attrs);
4653 merge_face_vectors (f, symbol_attrs, attrs, 0);
4655 return lookup_face (f, attrs);
4659 /* Return the display face-id of the basic face who's canonical face-id
4660 is FACE_ID. The return value will usually simply be FACE_ID, unless that
4661 basic face has bee remapped via Vface_remapping_alist. This function is
4662 conservative: if something goes wrong, it will simply return FACE_ID
4663 rather than signal an error. */
4666 lookup_basic_face (struct frame *f, int face_id)
4668 Lisp_Object name, mapping;
4669 int remapped_face_id;
4671 if (NILP (Vface_remapping_alist))
4672 return face_id; /* Nothing to do. */
4674 switch (face_id)
4676 case DEFAULT_FACE_ID: name = Qdefault; break;
4677 case MODE_LINE_FACE_ID: name = Qmode_line; break;
4678 case MODE_LINE_INACTIVE_FACE_ID: name = Qmode_line_inactive; break;
4679 case HEADER_LINE_FACE_ID: name = Qheader_line; break;
4680 case TOOL_BAR_FACE_ID: name = Qtool_bar; break;
4681 case FRINGE_FACE_ID: name = Qfringe; break;
4682 case SCROLL_BAR_FACE_ID: name = Qscroll_bar; break;
4683 case BORDER_FACE_ID: name = Qborder; break;
4684 case CURSOR_FACE_ID: name = Qcursor; break;
4685 case MOUSE_FACE_ID: name = Qmouse; break;
4686 case MENU_FACE_ID: name = Qmenu; break;
4688 default:
4689 abort (); /* the caller is supposed to pass us a basic face id */
4692 /* Do a quick scan through Vface_remapping_alist, and return immediately
4693 if there is no remapping for face NAME. This is just an optimization
4694 for the very common no-remapping case. */
4695 mapping = assq_no_quit (name, Vface_remapping_alist);
4696 if (NILP (mapping))
4697 return face_id; /* Give up. */
4699 /* If there is a remapping entry, lookup the face using NAME, which will
4700 handle the remapping too. */
4701 remapped_face_id = lookup_named_face (f, name, 0);
4702 if (remapped_face_id < 0)
4703 return face_id; /* Give up. */
4705 return remapped_face_id;
4709 /* Return the ID of the realized ASCII face of Lisp face with ID
4710 LFACE_ID on frame F. Value is -1 if LFACE_ID isn't valid. */
4713 ascii_face_of_lisp_face (struct frame *f, int lface_id)
4715 int face_id;
4717 if (lface_id >= 0 && lface_id < lface_id_to_name_size)
4719 Lisp_Object face_name = lface_id_to_name[lface_id];
4720 face_id = lookup_named_face (f, face_name, 1);
4722 else
4723 face_id = -1;
4725 return face_id;
4729 /* Return a face for charset ASCII that is like the face with id
4730 FACE_ID on frame F, but has a font that is STEPS steps smaller.
4731 STEPS < 0 means larger. Value is the id of the face. */
4734 smaller_face (struct frame *f, int face_id, int steps)
4736 #ifdef HAVE_WINDOW_SYSTEM
4737 struct face *face;
4738 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4739 int pt, last_pt, last_height;
4740 int delta;
4741 int new_face_id;
4742 struct face *new_face;
4744 /* If not called for an X frame, just return the original face. */
4745 if (FRAME_TERMCAP_P (f))
4746 return face_id;
4748 /* Try in increments of 1/2 pt. */
4749 delta = steps < 0 ? 5 : -5;
4750 steps = eabs (steps);
4752 face = FACE_FROM_ID (f, face_id);
4753 memcpy (attrs, face->lface, sizeof attrs);
4754 pt = last_pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
4755 new_face_id = face_id;
4756 last_height = FONT_HEIGHT (face->font);
4758 while (steps
4759 && pt + delta > 0
4760 /* Give up if we cannot find a font within 10pt. */
4761 && eabs (last_pt - pt) < 100)
4763 /* Look up a face for a slightly smaller/larger font. */
4764 pt += delta;
4765 attrs[LFACE_HEIGHT_INDEX] = make_number (pt);
4766 new_face_id = lookup_face (f, attrs);
4767 new_face = FACE_FROM_ID (f, new_face_id);
4769 /* If height changes, count that as one step. */
4770 if ((delta < 0 && FONT_HEIGHT (new_face->font) < last_height)
4771 || (delta > 0 && FONT_HEIGHT (new_face->font) > last_height))
4773 --steps;
4774 last_height = FONT_HEIGHT (new_face->font);
4775 last_pt = pt;
4779 return new_face_id;
4781 #else /* not HAVE_WINDOW_SYSTEM */
4783 return face_id;
4785 #endif /* not HAVE_WINDOW_SYSTEM */
4789 /* Return a face for charset ASCII that is like the face with id
4790 FACE_ID on frame F, but has height HEIGHT. */
4793 face_with_height (struct frame *f, int face_id, int height)
4795 #ifdef HAVE_WINDOW_SYSTEM
4796 struct face *face;
4797 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4799 if (FRAME_TERMCAP_P (f)
4800 || height <= 0)
4801 return face_id;
4803 face = FACE_FROM_ID (f, face_id);
4804 memcpy (attrs, face->lface, sizeof attrs);
4805 attrs[LFACE_HEIGHT_INDEX] = make_number (height);
4806 font_clear_prop (attrs, FONT_SIZE_INDEX);
4807 face_id = lookup_face (f, attrs);
4808 #endif /* HAVE_WINDOW_SYSTEM */
4810 return face_id;
4814 /* Return the face id of the realized face for named face SYMBOL on
4815 frame F suitable for displaying ASCII characters, and use
4816 attributes of the face FACE_ID for attributes that aren't
4817 completely specified by SYMBOL. This is like lookup_named_face,
4818 except that the default attributes come from FACE_ID, not from the
4819 default face. FACE_ID is assumed to be already realized. */
4822 lookup_derived_face (struct frame *f, Lisp_Object symbol, int face_id, int signal_p)
4824 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4825 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4826 struct face *default_face = FACE_FROM_ID (f, face_id);
4828 if (!default_face)
4829 abort ();
4831 if (!get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4832 return -1;
4834 memcpy (attrs, default_face->lface, sizeof attrs);
4835 merge_face_vectors (f, symbol_attrs, attrs, 0);
4836 return lookup_face (f, attrs);
4839 DEFUN ("face-attributes-as-vector", Fface_attributes_as_vector,
4840 Sface_attributes_as_vector, 1, 1, 0,
4841 doc: /* Return a vector of face attributes corresponding to PLIST. */)
4842 (Lisp_Object plist)
4844 Lisp_Object lface;
4845 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
4846 Qunspecified);
4847 merge_face_ref (XFRAME (selected_frame), plist, XVECTOR (lface)->contents,
4848 1, 0);
4849 return lface;
4854 /***********************************************************************
4855 Face capability testing
4856 ***********************************************************************/
4859 /* If the distance (as returned by color_distance) between two colors is
4860 less than this, then they are considered the same, for determining
4861 whether a color is supported or not. The range of values is 0-65535. */
4863 #define TTY_SAME_COLOR_THRESHOLD 10000
4865 #ifdef HAVE_WINDOW_SYSTEM
4867 /* Return non-zero if all the face attributes in ATTRS are supported
4868 on the window-system frame F.
4870 The definition of `supported' is somewhat heuristic, but basically means
4871 that a face containing all the attributes in ATTRS, when merged with the
4872 default face for display, can be represented in a way that's
4874 \(1) different in appearance than the default face, and
4875 \(2) `close in spirit' to what the attributes specify, if not exact. */
4877 static int
4878 x_supports_face_attributes_p (struct frame *f, Lisp_Object *attrs, struct face *def_face)
4880 Lisp_Object *def_attrs = def_face->lface;
4882 /* Check that other specified attributes are different that the default
4883 face. */
4884 if ((!UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
4885 && face_attr_equal_p (attrs[LFACE_UNDERLINE_INDEX],
4886 def_attrs[LFACE_UNDERLINE_INDEX]))
4887 || (!UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
4888 && face_attr_equal_p (attrs[LFACE_INVERSE_INDEX],
4889 def_attrs[LFACE_INVERSE_INDEX]))
4890 || (!UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
4891 && face_attr_equal_p (attrs[LFACE_FOREGROUND_INDEX],
4892 def_attrs[LFACE_FOREGROUND_INDEX]))
4893 || (!UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
4894 && face_attr_equal_p (attrs[LFACE_BACKGROUND_INDEX],
4895 def_attrs[LFACE_BACKGROUND_INDEX]))
4896 || (!UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4897 && face_attr_equal_p (attrs[LFACE_STIPPLE_INDEX],
4898 def_attrs[LFACE_STIPPLE_INDEX]))
4899 || (!UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4900 && face_attr_equal_p (attrs[LFACE_OVERLINE_INDEX],
4901 def_attrs[LFACE_OVERLINE_INDEX]))
4902 || (!UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4903 && face_attr_equal_p (attrs[LFACE_STRIKE_THROUGH_INDEX],
4904 def_attrs[LFACE_STRIKE_THROUGH_INDEX]))
4905 || (!UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
4906 && face_attr_equal_p (attrs[LFACE_BOX_INDEX],
4907 def_attrs[LFACE_BOX_INDEX])))
4908 return 0;
4910 /* Check font-related attributes, as those are the most commonly
4911 "unsupported" on a window-system (because of missing fonts). */
4912 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4913 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4914 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4915 || !UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
4916 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
4917 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX]))
4919 int face_id;
4920 struct face *face;
4921 Lisp_Object merged_attrs[LFACE_VECTOR_SIZE];
4922 int i;
4924 memcpy (merged_attrs, def_attrs, sizeof merged_attrs);
4926 merge_face_vectors (f, attrs, merged_attrs, 0);
4928 face_id = lookup_face (f, merged_attrs);
4929 face = FACE_FROM_ID (f, face_id);
4931 if (! face)
4932 error ("Cannot make face");
4934 /* If the font is the same, or no font is found, then not
4935 supported. */
4936 if (face->font == def_face->font
4937 || ! face->font)
4938 return 0;
4939 for (i = FONT_TYPE_INDEX; i <= FONT_SIZE_INDEX; i++)
4940 if (! EQ (face->font->props[i], def_face->font->props[i]))
4942 Lisp_Object s1, s2;
4944 if (i < FONT_FOUNDRY_INDEX || i > FONT_REGISTRY_INDEX
4945 || face->font->driver->case_sensitive)
4946 return 1;
4947 s1 = SYMBOL_NAME (face->font->props[i]);
4948 s2 = SYMBOL_NAME (def_face->font->props[i]);
4949 if (! EQ (Fcompare_strings (s1, make_number (0), Qnil,
4950 s2, make_number (0), Qnil, Qt), Qt))
4951 return 1;
4953 return 0;
4956 /* Everything checks out, this face is supported. */
4957 return 1;
4960 #endif /* HAVE_WINDOW_SYSTEM */
4962 /* Return non-zero if all the face attributes in ATTRS are supported
4963 on the tty frame F.
4965 The definition of `supported' is somewhat heuristic, but basically means
4966 that a face containing all the attributes in ATTRS, when merged
4967 with the default face for display, can be represented in a way that's
4969 \(1) different in appearance than the default face, and
4970 \(2) `close in spirit' to what the attributes specify, if not exact.
4972 Point (2) implies that a `:weight black' attribute will be satisfied
4973 by any terminal that can display bold, and a `:foreground "yellow"' as
4974 long as the terminal can display a yellowish color, but `:slant italic'
4975 will _not_ be satisfied by the tty display code's automatic
4976 substitution of a `dim' face for italic. */
4978 static int
4979 tty_supports_face_attributes_p (struct frame *f, Lisp_Object *attrs, struct face *def_face)
4981 int weight;
4982 Lisp_Object val, fg, bg;
4983 XColor fg_tty_color, fg_std_color;
4984 XColor bg_tty_color, bg_std_color;
4985 unsigned test_caps = 0;
4986 Lisp_Object *def_attrs = def_face->lface;
4989 /* First check some easy-to-check stuff; ttys support none of the
4990 following attributes, so we can just return false if any are requested
4991 (even if `nominal' values are specified, we should still return false,
4992 as that will be the same value that the default face uses). We
4993 consider :slant unsupportable on ttys, even though the face code
4994 actually `fakes' them using a dim attribute if possible. This is
4995 because the faked result is too different from what the face
4996 specifies. */
4997 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4998 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4999 || !UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
5000 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
5001 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
5002 || !UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
5003 || !UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
5004 || !UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
5005 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX]))
5006 return 0;
5009 /* Test for terminal `capabilities' (non-color character attributes). */
5011 /* font weight (bold/dim) */
5012 val = attrs[LFACE_WEIGHT_INDEX];
5013 if (!UNSPECIFIEDP (val)
5014 && (weight = FONT_WEIGHT_NAME_NUMERIC (val), weight >= 0))
5016 int def_weight = FONT_WEIGHT_NAME_NUMERIC (def_attrs[LFACE_WEIGHT_INDEX]);
5018 if (weight > 100)
5020 if (def_weight > 100)
5021 return 0; /* same as default */
5022 test_caps = TTY_CAP_BOLD;
5024 else if (weight < 100)
5026 if (def_weight < 100)
5027 return 0; /* same as default */
5028 test_caps = TTY_CAP_DIM;
5030 else if (def_weight == 100)
5031 return 0; /* same as default */
5034 /* underlining */
5035 val = attrs[LFACE_UNDERLINE_INDEX];
5036 if (!UNSPECIFIEDP (val))
5038 if (STRINGP (val))
5039 return 0; /* ttys can't use colored underlines */
5040 else if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
5041 return 0; /* same as default */
5042 else
5043 test_caps |= TTY_CAP_UNDERLINE;
5046 /* inverse video */
5047 val = attrs[LFACE_INVERSE_INDEX];
5048 if (!UNSPECIFIEDP (val))
5050 if (face_attr_equal_p (val, def_attrs[LFACE_INVERSE_INDEX]))
5051 return 0; /* same as default */
5052 else
5053 test_caps |= TTY_CAP_INVERSE;
5057 /* Color testing. */
5059 /* Default the color indices in FG_TTY_COLOR and BG_TTY_COLOR, since
5060 we use them when calling `tty_capable_p' below, even if the face
5061 specifies no colors. */
5062 fg_tty_color.pixel = FACE_TTY_DEFAULT_FG_COLOR;
5063 bg_tty_color.pixel = FACE_TTY_DEFAULT_BG_COLOR;
5065 /* Check if foreground color is close enough. */
5066 fg = attrs[LFACE_FOREGROUND_INDEX];
5067 if (STRINGP (fg))
5069 Lisp_Object def_fg = def_attrs[LFACE_FOREGROUND_INDEX];
5071 if (face_attr_equal_p (fg, def_fg))
5072 return 0; /* same as default */
5073 else if (! tty_lookup_color (f, fg, &fg_tty_color, &fg_std_color))
5074 return 0; /* not a valid color */
5075 else if (color_distance (&fg_tty_color, &fg_std_color)
5076 > TTY_SAME_COLOR_THRESHOLD)
5077 return 0; /* displayed color is too different */
5078 else
5079 /* Make sure the color is really different than the default. */
5081 XColor def_fg_color;
5082 if (tty_lookup_color (f, def_fg, &def_fg_color, 0)
5083 && (color_distance (&fg_tty_color, &def_fg_color)
5084 <= TTY_SAME_COLOR_THRESHOLD))
5085 return 0;
5089 /* Check if background color is close enough. */
5090 bg = attrs[LFACE_BACKGROUND_INDEX];
5091 if (STRINGP (bg))
5093 Lisp_Object def_bg = def_attrs[LFACE_BACKGROUND_INDEX];
5095 if (face_attr_equal_p (bg, def_bg))
5096 return 0; /* same as default */
5097 else if (! tty_lookup_color (f, bg, &bg_tty_color, &bg_std_color))
5098 return 0; /* not a valid color */
5099 else if (color_distance (&bg_tty_color, &bg_std_color)
5100 > TTY_SAME_COLOR_THRESHOLD)
5101 return 0; /* displayed color is too different */
5102 else
5103 /* Make sure the color is really different than the default. */
5105 XColor def_bg_color;
5106 if (tty_lookup_color (f, def_bg, &def_bg_color, 0)
5107 && (color_distance (&bg_tty_color, &def_bg_color)
5108 <= TTY_SAME_COLOR_THRESHOLD))
5109 return 0;
5113 /* If both foreground and background are requested, see if the
5114 distance between them is OK. We just check to see if the distance
5115 between the tty's foreground and background is close enough to the
5116 distance between the standard foreground and background. */
5117 if (STRINGP (fg) && STRINGP (bg))
5119 int delta_delta
5120 = (color_distance (&fg_std_color, &bg_std_color)
5121 - color_distance (&fg_tty_color, &bg_tty_color));
5122 if (delta_delta > TTY_SAME_COLOR_THRESHOLD
5123 || delta_delta < -TTY_SAME_COLOR_THRESHOLD)
5124 return 0;
5128 /* See if the capabilities we selected above are supported, with the
5129 given colors. */
5130 if (test_caps != 0 &&
5131 ! tty_capable_p (FRAME_TTY (f), test_caps, fg_tty_color.pixel, bg_tty_color.pixel))
5132 return 0;
5135 /* Hmmm, everything checks out, this terminal must support this face. */
5136 return 1;
5140 DEFUN ("display-supports-face-attributes-p",
5141 Fdisplay_supports_face_attributes_p, Sdisplay_supports_face_attributes_p,
5142 1, 2, 0,
5143 doc: /* Return non-nil if all the face attributes in ATTRIBUTES are supported.
5144 The optional argument DISPLAY can be a display name, a frame, or
5145 nil (meaning the selected frame's display).
5147 The definition of `supported' is somewhat heuristic, but basically means
5148 that a face containing all the attributes in ATTRIBUTES, when merged
5149 with the default face for display, can be represented in a way that's
5151 \(1) different in appearance than the default face, and
5152 \(2) `close in spirit' to what the attributes specify, if not exact.
5154 Point (2) implies that a `:weight black' attribute will be satisfied by
5155 any display that can display bold, and a `:foreground \"yellow\"' as long
5156 as it can display a yellowish color, but `:slant italic' will _not_ be
5157 satisfied by the tty display code's automatic substitution of a `dim'
5158 face for italic. */)
5159 (Lisp_Object attributes, Lisp_Object display)
5161 int supports = 0, i;
5162 Lisp_Object frame;
5163 struct frame *f;
5164 struct face *def_face;
5165 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5167 if (noninteractive || !initialized)
5168 /* We may not be able to access low-level face information in batch
5169 mode, or before being dumped, and this function is not going to
5170 be very useful in those cases anyway, so just give up. */
5171 return Qnil;
5173 if (NILP (display))
5174 frame = selected_frame;
5175 else if (FRAMEP (display))
5176 frame = display;
5177 else
5179 /* Find any frame on DISPLAY. */
5180 Lisp_Object fl_tail;
5182 frame = Qnil;
5183 for (fl_tail = Vframe_list; CONSP (fl_tail); fl_tail = XCDR (fl_tail))
5185 frame = XCAR (fl_tail);
5186 if (!NILP (Fequal (Fcdr (Fassq (Qdisplay,
5187 XFRAME (frame)->param_alist)),
5188 display)))
5189 break;
5193 CHECK_LIVE_FRAME (frame);
5194 f = XFRAME (frame);
5196 for (i = 0; i < LFACE_VECTOR_SIZE; i++)
5197 attrs[i] = Qunspecified;
5198 merge_face_ref (f, attributes, attrs, 1, 0);
5200 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5201 if (def_face == NULL)
5203 if (! realize_basic_faces (f))
5204 error ("Cannot realize default face");
5205 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5206 if (def_face == NULL)
5207 abort (); /* realize_basic_faces must have set it up */
5210 /* Dispatch to the appropriate handler. */
5211 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5212 supports = tty_supports_face_attributes_p (f, attrs, def_face);
5213 #ifdef HAVE_WINDOW_SYSTEM
5214 else
5215 supports = x_supports_face_attributes_p (f, attrs, def_face);
5216 #endif
5218 return supports ? Qt : Qnil;
5222 /***********************************************************************
5223 Font selection
5224 ***********************************************************************/
5226 DEFUN ("internal-set-font-selection-order",
5227 Finternal_set_font_selection_order,
5228 Sinternal_set_font_selection_order, 1, 1, 0,
5229 doc: /* Set font selection order for face font selection to ORDER.
5230 ORDER must be a list of length 4 containing the symbols `:width',
5231 `:height', `:weight', and `:slant'. Face attributes appearing
5232 first in ORDER are matched first, e.g. if `:height' appears before
5233 `:weight' in ORDER, font selection first tries to find a font with
5234 a suitable height, and then tries to match the font weight.
5235 Value is ORDER. */)
5236 (Lisp_Object order)
5238 Lisp_Object list;
5239 int i;
5240 int indices[DIM (font_sort_order)];
5242 CHECK_LIST (order);
5243 memset (indices, 0, sizeof indices);
5244 i = 0;
5246 for (list = order;
5247 CONSP (list) && i < DIM (indices);
5248 list = XCDR (list), ++i)
5250 Lisp_Object attr = XCAR (list);
5251 int xlfd;
5253 if (EQ (attr, QCwidth))
5254 xlfd = XLFD_SWIDTH;
5255 else if (EQ (attr, QCheight))
5256 xlfd = XLFD_POINT_SIZE;
5257 else if (EQ (attr, QCweight))
5258 xlfd = XLFD_WEIGHT;
5259 else if (EQ (attr, QCslant))
5260 xlfd = XLFD_SLANT;
5261 else
5262 break;
5264 if (indices[i] != 0)
5265 break;
5266 indices[i] = xlfd;
5269 if (!NILP (list) || i != DIM (indices))
5270 signal_error ("Invalid font sort order", order);
5271 for (i = 0; i < DIM (font_sort_order); ++i)
5272 if (indices[i] == 0)
5273 signal_error ("Invalid font sort order", order);
5275 if (memcmp (indices, font_sort_order, sizeof indices) != 0)
5277 memcpy (font_sort_order, indices, sizeof font_sort_order);
5278 free_all_realized_faces (Qnil);
5281 font_update_sort_order (font_sort_order);
5283 return Qnil;
5287 DEFUN ("internal-set-alternative-font-family-alist",
5288 Finternal_set_alternative_font_family_alist,
5289 Sinternal_set_alternative_font_family_alist, 1, 1, 0,
5290 doc: /* Define alternative font families to try in face font selection.
5291 ALIST is an alist of (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5292 Each ALTERNATIVE is tried in order if no fonts of font family FAMILY can
5293 be found. Value is ALIST. */)
5294 (Lisp_Object alist)
5296 Lisp_Object entry, tail, tail2;
5298 CHECK_LIST (alist);
5299 alist = Fcopy_sequence (alist);
5300 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5302 entry = XCAR (tail);
5303 CHECK_LIST (entry);
5304 entry = Fcopy_sequence (entry);
5305 XSETCAR (tail, entry);
5306 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5307 XSETCAR (tail2, Fintern (XCAR (tail2), Qnil));
5310 Vface_alternative_font_family_alist = alist;
5311 free_all_realized_faces (Qnil);
5312 return alist;
5316 DEFUN ("internal-set-alternative-font-registry-alist",
5317 Finternal_set_alternative_font_registry_alist,
5318 Sinternal_set_alternative_font_registry_alist, 1, 1, 0,
5319 doc: /* Define alternative font registries to try in face font selection.
5320 ALIST is an alist of (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5321 Each ALTERNATIVE is tried in order if no fonts of font registry REGISTRY can
5322 be found. Value is ALIST. */)
5323 (Lisp_Object alist)
5325 Lisp_Object entry, tail, tail2;
5327 CHECK_LIST (alist);
5328 alist = Fcopy_sequence (alist);
5329 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5331 entry = XCAR (tail);
5332 CHECK_LIST (entry);
5333 entry = Fcopy_sequence (entry);
5334 XSETCAR (tail, entry);
5335 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5336 XSETCAR (tail2, Fdowncase (XCAR (tail2)));
5338 Vface_alternative_font_registry_alist = alist;
5339 free_all_realized_faces (Qnil);
5340 return alist;
5344 #ifdef HAVE_WINDOW_SYSTEM
5346 /* Ignore the difference of font point size less than this value. */
5348 #define FONT_POINT_SIZE_QUANTUM 5
5350 /* Return the fontset id of the base fontset name or alias name given
5351 by the fontset attribute of ATTRS. Value is -1 if the fontset
5352 attribute of ATTRS doesn't name a fontset. */
5354 static int
5355 face_fontset (Lisp_Object *attrs)
5357 Lisp_Object name;
5359 name = attrs[LFACE_FONTSET_INDEX];
5360 if (!STRINGP (name))
5361 return -1;
5362 return fs_query_fontset (name, 0);
5365 #endif /* HAVE_WINDOW_SYSTEM */
5369 /***********************************************************************
5370 Face Realization
5371 ***********************************************************************/
5373 /* Realize basic faces on frame F. Value is zero if frame parameters
5374 of F don't contain enough information needed to realize the default
5375 face. */
5377 static int
5378 realize_basic_faces (struct frame *f)
5380 int success_p = 0;
5381 int count = SPECPDL_INDEX ();
5383 /* Block input here so that we won't be surprised by an X expose
5384 event, for instance, without having the faces set up. */
5385 BLOCK_INPUT;
5386 specbind (Qscalable_fonts_allowed, Qt);
5388 if (realize_default_face (f))
5390 realize_named_face (f, Qmode_line, MODE_LINE_FACE_ID);
5391 realize_named_face (f, Qmode_line_inactive, MODE_LINE_INACTIVE_FACE_ID);
5392 realize_named_face (f, Qtool_bar, TOOL_BAR_FACE_ID);
5393 realize_named_face (f, Qfringe, FRINGE_FACE_ID);
5394 realize_named_face (f, Qheader_line, HEADER_LINE_FACE_ID);
5395 realize_named_face (f, Qscroll_bar, SCROLL_BAR_FACE_ID);
5396 realize_named_face (f, Qborder, BORDER_FACE_ID);
5397 realize_named_face (f, Qcursor, CURSOR_FACE_ID);
5398 realize_named_face (f, Qmouse, MOUSE_FACE_ID);
5399 realize_named_face (f, Qmenu, MENU_FACE_ID);
5400 realize_named_face (f, Qvertical_border, VERTICAL_BORDER_FACE_ID);
5402 /* Reflect changes in the `menu' face in menu bars. */
5403 if (FRAME_FACE_CACHE (f)->menu_face_changed_p)
5405 FRAME_FACE_CACHE (f)->menu_face_changed_p = 0;
5406 #ifdef USE_X_TOOLKIT
5407 if (FRAME_WINDOW_P (f))
5408 x_update_menu_appearance (f);
5409 #endif
5412 success_p = 1;
5415 unbind_to (count, Qnil);
5416 UNBLOCK_INPUT;
5417 return success_p;
5421 /* Realize the default face on frame F. If the face is not fully
5422 specified, make it fully-specified. Attributes of the default face
5423 that are not explicitly specified are taken from frame parameters. */
5425 static int
5426 realize_default_face (struct frame *f)
5428 struct face_cache *c = FRAME_FACE_CACHE (f);
5429 Lisp_Object lface;
5430 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5431 struct face *face;
5433 /* If the `default' face is not yet known, create it. */
5434 lface = lface_from_face_name (f, Qdefault, 0);
5435 if (NILP (lface))
5437 Lisp_Object frame;
5438 XSETFRAME (frame, f);
5439 lface = Finternal_make_lisp_face (Qdefault, frame);
5442 #ifdef HAVE_WINDOW_SYSTEM
5443 if (FRAME_WINDOW_P (f))
5445 Lisp_Object font_object;
5447 XSETFONT (font_object, FRAME_FONT (f));
5448 set_lface_from_font (f, lface, font_object, f->default_face_done_p);
5449 LFACE_FONTSET (lface) = fontset_name (FRAME_FONTSET (f));
5450 f->default_face_done_p = 1;
5452 #endif /* HAVE_WINDOW_SYSTEM */
5454 if (!FRAME_WINDOW_P (f))
5456 LFACE_FAMILY (lface) = build_string ("default");
5457 LFACE_FOUNDRY (lface) = LFACE_FAMILY (lface);
5458 LFACE_SWIDTH (lface) = Qnormal;
5459 LFACE_HEIGHT (lface) = make_number (1);
5460 if (UNSPECIFIEDP (LFACE_WEIGHT (lface)))
5461 LFACE_WEIGHT (lface) = Qnormal;
5462 if (UNSPECIFIEDP (LFACE_SLANT (lface)))
5463 LFACE_SLANT (lface) = Qnormal;
5464 if (UNSPECIFIEDP (LFACE_FONTSET (lface)))
5465 LFACE_FONTSET (lface) = Qnil;
5468 if (UNSPECIFIEDP (LFACE_UNDERLINE (lface)))
5469 LFACE_UNDERLINE (lface) = Qnil;
5471 if (UNSPECIFIEDP (LFACE_OVERLINE (lface)))
5472 LFACE_OVERLINE (lface) = Qnil;
5474 if (UNSPECIFIEDP (LFACE_STRIKE_THROUGH (lface)))
5475 LFACE_STRIKE_THROUGH (lface) = Qnil;
5477 if (UNSPECIFIEDP (LFACE_BOX (lface)))
5478 LFACE_BOX (lface) = Qnil;
5480 if (UNSPECIFIEDP (LFACE_INVERSE (lface)))
5481 LFACE_INVERSE (lface) = Qnil;
5483 if (UNSPECIFIEDP (LFACE_FOREGROUND (lface)))
5485 /* This function is called so early that colors are not yet
5486 set in the frame parameter list. */
5487 Lisp_Object color = Fassq (Qforeground_color, f->param_alist);
5489 if (CONSP (color) && STRINGP (XCDR (color)))
5490 LFACE_FOREGROUND (lface) = XCDR (color);
5491 else if (FRAME_WINDOW_P (f))
5492 return 0;
5493 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5494 LFACE_FOREGROUND (lface) = build_string (unspecified_fg);
5495 else
5496 abort ();
5499 if (UNSPECIFIEDP (LFACE_BACKGROUND (lface)))
5501 /* This function is called so early that colors are not yet
5502 set in the frame parameter list. */
5503 Lisp_Object color = Fassq (Qbackground_color, f->param_alist);
5504 if (CONSP (color) && STRINGP (XCDR (color)))
5505 LFACE_BACKGROUND (lface) = XCDR (color);
5506 else if (FRAME_WINDOW_P (f))
5507 return 0;
5508 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5509 LFACE_BACKGROUND (lface) = build_string (unspecified_bg);
5510 else
5511 abort ();
5514 if (UNSPECIFIEDP (LFACE_STIPPLE (lface)))
5515 LFACE_STIPPLE (lface) = Qnil;
5517 /* Realize the face; it must be fully-specified now. */
5518 xassert (lface_fully_specified_p (XVECTOR (lface)->contents));
5519 check_lface (lface);
5520 memcpy (attrs, XVECTOR (lface)->contents, sizeof attrs);
5521 face = realize_face (c, attrs, DEFAULT_FACE_ID);
5523 #ifdef HAVE_WINDOW_SYSTEM
5524 #ifdef HAVE_X_WINDOWS
5525 if (FRAME_X_P (f) && face->font != FRAME_FONT (f))
5527 /* This can happen when making a frame on a display that does
5528 not support the default font. */
5529 if (!face->font)
5530 return 0;
5532 /* Otherwise, the font specified for the frame was not
5533 acceptable as a font for the default face (perhaps because
5534 auto-scaled fonts are rejected), so we must adjust the frame
5535 font. */
5536 x_set_font (f, LFACE_FONT (lface), Qnil);
5538 #endif /* HAVE_X_WINDOWS */
5539 #endif /* HAVE_WINDOW_SYSTEM */
5540 return 1;
5544 /* Realize basic faces other than the default face in face cache C.
5545 SYMBOL is the face name, ID is the face id the realized face must
5546 have. The default face must have been realized already. */
5548 static void
5549 realize_named_face (struct frame *f, Lisp_Object symbol, int id)
5551 struct face_cache *c = FRAME_FACE_CACHE (f);
5552 Lisp_Object lface = lface_from_face_name (f, symbol, 0);
5553 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5554 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5555 struct face *new_face;
5557 /* The default face must exist and be fully specified. */
5558 get_lface_attributes_no_remap (f, Qdefault, attrs, 1);
5559 check_lface_attrs (attrs);
5560 xassert (lface_fully_specified_p (attrs));
5562 /* If SYMBOL isn't know as a face, create it. */
5563 if (NILP (lface))
5565 Lisp_Object frame;
5566 XSETFRAME (frame, f);
5567 lface = Finternal_make_lisp_face (symbol, frame);
5570 /* Merge SYMBOL's face with the default face. */
5571 get_lface_attributes_no_remap (f, symbol, symbol_attrs, 1);
5572 merge_face_vectors (f, symbol_attrs, attrs, 0);
5574 /* Realize the face. */
5575 new_face = realize_face (c, attrs, id);
5579 /* Realize the fully-specified face with attributes ATTRS in face
5580 cache CACHE for ASCII characters. If FORMER_FACE_ID is
5581 non-negative, it is an ID of face to remove before caching the new
5582 face. Value is a pointer to the newly created realized face. */
5584 static struct face *
5585 realize_face (struct face_cache *cache, Lisp_Object *attrs, int former_face_id)
5587 struct face *face;
5589 /* LFACE must be fully specified. */
5590 xassert (cache != NULL);
5591 check_lface_attrs (attrs);
5593 if (former_face_id >= 0 && cache->used > former_face_id)
5595 /* Remove the former face. */
5596 struct face *former_face = cache->faces_by_id[former_face_id];
5597 uncache_face (cache, former_face);
5598 free_realized_face (cache->f, former_face);
5599 SET_FRAME_GARBAGED (cache->f);
5602 if (FRAME_WINDOW_P (cache->f))
5603 face = realize_x_face (cache, attrs);
5604 else if (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f))
5605 face = realize_tty_face (cache, attrs);
5606 else if (FRAME_INITIAL_P (cache->f))
5608 /* Create a dummy face. */
5609 face = make_realized_face (attrs);
5611 else
5612 abort ();
5614 /* Insert the new face. */
5615 cache_face (cache, face, lface_hash (attrs));
5616 return face;
5620 #ifdef HAVE_WINDOW_SYSTEM
5621 /* Realize the fully-specified face that uses FONT-OBJECT and has the
5622 same attributes as BASE_FACE except for the font on frame F.
5623 FONT-OBJECT may be nil, in which case, realized a face of
5624 no-font. */
5626 static struct face *
5627 realize_non_ascii_face (struct frame *f, Lisp_Object font_object, struct face *base_face)
5629 struct face_cache *cache = FRAME_FACE_CACHE (f);
5630 struct face *face;
5632 face = (struct face *) xmalloc (sizeof *face);
5633 *face = *base_face;
5634 face->gc = 0;
5635 face->extra = NULL;
5636 face->overstrike
5637 = (! NILP (font_object)
5638 && FONT_WEIGHT_NAME_NUMERIC (face->lface[LFACE_WEIGHT_INDEX]) > 100
5639 && FONT_WEIGHT_NUMERIC (font_object) <= 100);
5641 /* Don't try to free the colors copied bitwise from BASE_FACE. */
5642 face->colors_copied_bitwise_p = 1;
5643 face->font = NILP (font_object) ? NULL : XFONT_OBJECT (font_object);
5644 face->gc = 0;
5646 cache_face (cache, face, face->hash);
5648 return face;
5650 #endif /* HAVE_WINDOW_SYSTEM */
5653 /* Realize the fully-specified face with attributes ATTRS in face
5654 cache CACHE for ASCII characters. Do it for X frame CACHE->f. If
5655 the new face doesn't share font with the default face, a fontname
5656 is allocated from the heap and set in `font_name' of the new face,
5657 but it is not yet loaded here. Value is a pointer to the newly
5658 created realized face. */
5660 static struct face *
5661 realize_x_face (struct face_cache *cache, Lisp_Object *attrs)
5663 struct face *face = NULL;
5664 #ifdef HAVE_WINDOW_SYSTEM
5665 struct face *default_face;
5666 struct frame *f;
5667 Lisp_Object stipple, overline, strike_through, box;
5669 xassert (FRAME_WINDOW_P (cache->f));
5671 /* Allocate a new realized face. */
5672 face = make_realized_face (attrs);
5673 face->ascii_face = face;
5675 f = cache->f;
5677 /* Determine the font to use. Most of the time, the font will be
5678 the same as the font of the default face, so try that first. */
5679 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5680 if (default_face
5681 && lface_same_font_attributes_p (default_face->lface, attrs))
5683 face->font = default_face->font;
5684 face->fontset
5685 = make_fontset_for_ascii_face (f, default_face->fontset, face);
5687 else
5689 /* If the face attribute ATTRS specifies a fontset, use it as
5690 the base of a new realized fontset. Otherwise, use the same
5691 base fontset as of the default face. The base determines
5692 registry and encoding of a font. It may also determine
5693 foundry and family. The other fields of font name pattern
5694 are constructed from ATTRS. */
5695 int fontset = face_fontset (attrs);
5697 /* If we are realizing the default face, ATTRS should specify a
5698 fontset. In other words, if FONTSET is -1, we are not
5699 realizing the default face, thus the default face should have
5700 already been realized. */
5701 if (fontset == -1)
5703 if (default_face)
5704 fontset = default_face->fontset;
5705 if (fontset == -1)
5706 abort ();
5708 if (! FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5709 attrs[LFACE_FONT_INDEX]
5710 = font_load_for_lface (f, attrs, attrs[LFACE_FONT_INDEX]);
5711 if (FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5713 face->font = XFONT_OBJECT (attrs[LFACE_FONT_INDEX]);
5714 face->fontset = make_fontset_for_ascii_face (f, fontset, face);
5716 else
5718 face->font = NULL;
5719 face->fontset = -1;
5723 if (face->font
5724 && FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]) > 100
5725 && FONT_WEIGHT_NUMERIC (attrs[LFACE_FONT_INDEX]) <= 100)
5726 face->overstrike = 1;
5728 /* Load colors, and set remaining attributes. */
5730 load_face_colors (f, face, attrs);
5732 /* Set up box. */
5733 box = attrs[LFACE_BOX_INDEX];
5734 if (STRINGP (box))
5736 /* A simple box of line width 1 drawn in color given by
5737 the string. */
5738 face->box_color = load_color (f, face, attrs[LFACE_BOX_INDEX],
5739 LFACE_BOX_INDEX);
5740 face->box = FACE_SIMPLE_BOX;
5741 face->box_line_width = 1;
5743 else if (INTEGERP (box))
5745 /* Simple box of specified line width in foreground color of the
5746 face. */
5747 xassert (XINT (box) != 0);
5748 face->box = FACE_SIMPLE_BOX;
5749 face->box_line_width = XINT (box);
5750 face->box_color = face->foreground;
5751 face->box_color_defaulted_p = 1;
5753 else if (CONSP (box))
5755 /* `(:width WIDTH :color COLOR :shadow SHADOW)'. SHADOW
5756 being one of `raised' or `sunken'. */
5757 face->box = FACE_SIMPLE_BOX;
5758 face->box_color = face->foreground;
5759 face->box_color_defaulted_p = 1;
5760 face->box_line_width = 1;
5762 while (CONSP (box))
5764 Lisp_Object keyword, value;
5766 keyword = XCAR (box);
5767 box = XCDR (box);
5769 if (!CONSP (box))
5770 break;
5771 value = XCAR (box);
5772 box = XCDR (box);
5774 if (EQ (keyword, QCline_width))
5776 if (INTEGERP (value) && XINT (value) != 0)
5777 face->box_line_width = XINT (value);
5779 else if (EQ (keyword, QCcolor))
5781 if (STRINGP (value))
5783 face->box_color = load_color (f, face, value,
5784 LFACE_BOX_INDEX);
5785 face->use_box_color_for_shadows_p = 1;
5788 else if (EQ (keyword, QCstyle))
5790 if (EQ (value, Qreleased_button))
5791 face->box = FACE_RAISED_BOX;
5792 else if (EQ (value, Qpressed_button))
5793 face->box = FACE_SUNKEN_BOX;
5798 /* Text underline, overline, strike-through. */
5800 if (EQ (attrs[LFACE_UNDERLINE_INDEX], Qt))
5802 /* Use default color (same as foreground color). */
5803 face->underline_p = 1;
5804 face->underline_defaulted_p = 1;
5805 face->underline_color = 0;
5807 else if (STRINGP (attrs[LFACE_UNDERLINE_INDEX]))
5809 /* Use specified color. */
5810 face->underline_p = 1;
5811 face->underline_defaulted_p = 0;
5812 face->underline_color
5813 = load_color (f, face, attrs[LFACE_UNDERLINE_INDEX],
5814 LFACE_UNDERLINE_INDEX);
5816 else if (NILP (attrs[LFACE_UNDERLINE_INDEX]))
5818 face->underline_p = 0;
5819 face->underline_defaulted_p = 0;
5820 face->underline_color = 0;
5823 overline = attrs[LFACE_OVERLINE_INDEX];
5824 if (STRINGP (overline))
5826 face->overline_color
5827 = load_color (f, face, attrs[LFACE_OVERLINE_INDEX],
5828 LFACE_OVERLINE_INDEX);
5829 face->overline_p = 1;
5831 else if (EQ (overline, Qt))
5833 face->overline_color = face->foreground;
5834 face->overline_color_defaulted_p = 1;
5835 face->overline_p = 1;
5838 strike_through = attrs[LFACE_STRIKE_THROUGH_INDEX];
5839 if (STRINGP (strike_through))
5841 face->strike_through_color
5842 = load_color (f, face, attrs[LFACE_STRIKE_THROUGH_INDEX],
5843 LFACE_STRIKE_THROUGH_INDEX);
5844 face->strike_through_p = 1;
5846 else if (EQ (strike_through, Qt))
5848 face->strike_through_color = face->foreground;
5849 face->strike_through_color_defaulted_p = 1;
5850 face->strike_through_p = 1;
5853 stipple = attrs[LFACE_STIPPLE_INDEX];
5854 if (!NILP (stipple))
5855 face->stipple = load_pixmap (f, stipple, &face->pixmap_w, &face->pixmap_h);
5856 #endif /* HAVE_WINDOW_SYSTEM */
5858 return face;
5862 /* Map a specified color of face FACE on frame F to a tty color index.
5863 IDX is either LFACE_FOREGROUND_INDEX or LFACE_BACKGROUND_INDEX, and
5864 specifies which color to map. Set *DEFAULTED to 1 if mapping to the
5865 default foreground/background colors. */
5867 static void
5868 map_tty_color (struct frame *f, struct face *face, enum lface_attribute_index idx, int *defaulted)
5870 Lisp_Object frame, color, def;
5871 int foreground_p = idx == LFACE_FOREGROUND_INDEX;
5872 unsigned long default_pixel, default_other_pixel, pixel;
5874 xassert (idx == LFACE_FOREGROUND_INDEX || idx == LFACE_BACKGROUND_INDEX);
5876 if (foreground_p)
5878 pixel = default_pixel = FACE_TTY_DEFAULT_FG_COLOR;
5879 default_other_pixel = FACE_TTY_DEFAULT_BG_COLOR;
5881 else
5883 pixel = default_pixel = FACE_TTY_DEFAULT_BG_COLOR;
5884 default_other_pixel = FACE_TTY_DEFAULT_FG_COLOR;
5887 XSETFRAME (frame, f);
5888 color = face->lface[idx];
5890 if (STRINGP (color)
5891 && SCHARS (color)
5892 && CONSP (Vtty_defined_color_alist)
5893 && (def = assq_no_quit (color, call1 (Qtty_color_alist, frame)),
5894 CONSP (def)))
5896 /* Associations in tty-defined-color-alist are of the form
5897 (NAME INDEX R G B). We need the INDEX part. */
5898 pixel = XINT (XCAR (XCDR (def)));
5901 if (pixel == default_pixel && STRINGP (color))
5903 pixel = load_color (f, face, color, idx);
5905 #ifdef MSDOS
5906 /* If the foreground of the default face is the default color,
5907 use the foreground color defined by the frame. */
5908 if (FRAME_MSDOS_P (f))
5910 if (pixel == default_pixel
5911 || pixel == FACE_TTY_DEFAULT_COLOR)
5913 if (foreground_p)
5914 pixel = FRAME_FOREGROUND_PIXEL (f);
5915 else
5916 pixel = FRAME_BACKGROUND_PIXEL (f);
5917 face->lface[idx] = tty_color_name (f, pixel);
5918 *defaulted = 1;
5920 else if (pixel == default_other_pixel)
5922 if (foreground_p)
5923 pixel = FRAME_BACKGROUND_PIXEL (f);
5924 else
5925 pixel = FRAME_FOREGROUND_PIXEL (f);
5926 face->lface[idx] = tty_color_name (f, pixel);
5927 *defaulted = 1;
5930 #endif /* MSDOS */
5933 if (foreground_p)
5934 face->foreground = pixel;
5935 else
5936 face->background = pixel;
5940 /* Realize the fully-specified face with attributes ATTRS in face
5941 cache CACHE for ASCII characters. Do it for TTY frame CACHE->f.
5942 Value is a pointer to the newly created realized face. */
5944 static struct face *
5945 realize_tty_face (struct face_cache *cache, Lisp_Object *attrs)
5947 struct face *face;
5948 int weight, slant;
5949 int face_colors_defaulted = 0;
5950 struct frame *f = cache->f;
5952 /* Frame must be a termcap frame. */
5953 xassert (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f));
5955 /* Allocate a new realized face. */
5956 face = make_realized_face (attrs);
5957 #if 0
5958 face->font_name = FRAME_MSDOS_P (cache->f) ? "ms-dos" : "tty";
5959 #endif
5961 /* Map face attributes to TTY appearances. We map slant to
5962 dimmed text because we want italic text to appear differently
5963 and because dimmed text is probably used infrequently. */
5964 weight = FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]);
5965 slant = FONT_SLANT_NAME_NUMERIC (attrs[LFACE_SLANT_INDEX]);
5966 if (weight > 100)
5967 face->tty_bold_p = 1;
5968 if (weight < 100 || slant != 100)
5969 face->tty_dim_p = 1;
5970 if (!NILP (attrs[LFACE_UNDERLINE_INDEX]))
5971 face->tty_underline_p = 1;
5972 if (!NILP (attrs[LFACE_INVERSE_INDEX]))
5973 face->tty_reverse_p = 1;
5975 /* Map color names to color indices. */
5976 map_tty_color (f, face, LFACE_FOREGROUND_INDEX, &face_colors_defaulted);
5977 map_tty_color (f, face, LFACE_BACKGROUND_INDEX, &face_colors_defaulted);
5979 /* Swap colors if face is inverse-video. If the colors are taken
5980 from the frame colors, they are already inverted, since the
5981 frame-creation function calls x-handle-reverse-video. */
5982 if (face->tty_reverse_p && !face_colors_defaulted)
5984 unsigned long tem = face->foreground;
5985 face->foreground = face->background;
5986 face->background = tem;
5989 if (tty_suppress_bold_inverse_default_colors_p
5990 && face->tty_bold_p
5991 && face->background == FACE_TTY_DEFAULT_FG_COLOR
5992 && face->foreground == FACE_TTY_DEFAULT_BG_COLOR)
5993 face->tty_bold_p = 0;
5995 return face;
5999 DEFUN ("tty-suppress-bold-inverse-default-colors",
6000 Ftty_suppress_bold_inverse_default_colors,
6001 Stty_suppress_bold_inverse_default_colors, 1, 1, 0,
6002 doc: /* Suppress/allow boldness of faces with inverse default colors.
6003 SUPPRESS non-nil means suppress it.
6004 This affects bold faces on TTYs whose foreground is the default background
6005 color of the display and whose background is the default foreground color.
6006 For such faces, the bold face attribute is ignored if this variable
6007 is non-nil. */)
6008 (Lisp_Object suppress)
6010 tty_suppress_bold_inverse_default_colors_p = !NILP (suppress);
6011 ++face_change_count;
6012 return suppress;
6017 /***********************************************************************
6018 Computing Faces
6019 ***********************************************************************/
6021 /* Return the ID of the face to use to display character CH with face
6022 property PROP on frame F in current_buffer. */
6025 compute_char_face (struct frame *f, int ch, Lisp_Object prop)
6027 int face_id;
6029 if (NILP (current_buffer->enable_multibyte_characters))
6030 ch = 0;
6032 if (NILP (prop))
6034 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6035 face_id = FACE_FOR_CHAR (f, face, ch, -1, Qnil);
6037 else
6039 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6040 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6041 memcpy (attrs, default_face->lface, sizeof attrs);
6042 merge_face_ref (f, prop, attrs, 1, 0);
6043 face_id = lookup_face (f, attrs);
6046 return face_id;
6049 /* Return the face ID associated with buffer position POS for
6050 displaying ASCII characters. Return in *ENDPTR the position at
6051 which a different face is needed, as far as text properties and
6052 overlays are concerned. W is a window displaying current_buffer.
6054 REGION_BEG, REGION_END delimit the region, so it can be
6055 highlighted.
6057 LIMIT is a position not to scan beyond. That is to limit the time
6058 this function can take.
6060 If MOUSE is non-zero, use the character's mouse-face, not its face.
6062 BASE_FACE_ID, if non-negative, specifies a base face id to use
6063 instead of DEFAULT_FACE_ID.
6065 The face returned is suitable for displaying ASCII characters. */
6068 face_at_buffer_position (struct window *w, EMACS_INT pos,
6069 EMACS_INT region_beg, EMACS_INT region_end,
6070 EMACS_INT *endptr, EMACS_INT limit,
6071 int mouse, int base_face_id)
6073 struct frame *f = XFRAME (w->frame);
6074 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6075 Lisp_Object prop, position;
6076 int i, noverlays;
6077 Lisp_Object *overlay_vec;
6078 Lisp_Object frame;
6079 EMACS_INT endpos;
6080 Lisp_Object propname = mouse ? Qmouse_face : Qface;
6081 Lisp_Object limit1, end;
6082 struct face *default_face;
6084 /* W must display the current buffer. We could write this function
6085 to use the frame and buffer of W, but right now it doesn't. */
6086 /* xassert (XBUFFER (w->buffer) == current_buffer); */
6088 XSETFRAME (frame, f);
6089 XSETFASTINT (position, pos);
6091 endpos = ZV;
6092 if (pos < region_beg && region_beg < endpos)
6093 endpos = region_beg;
6095 /* Get the `face' or `mouse_face' text property at POS, and
6096 determine the next position at which the property changes. */
6097 prop = Fget_text_property (position, propname, w->buffer);
6098 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
6099 end = Fnext_single_property_change (position, propname, w->buffer, limit1);
6100 if (INTEGERP (end))
6101 endpos = XINT (end);
6103 /* Look at properties from overlays. */
6105 EMACS_INT next_overlay;
6107 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, &next_overlay, 0);
6108 if (next_overlay < endpos)
6109 endpos = next_overlay;
6112 *endptr = endpos;
6114 default_face = FACE_FROM_ID (f, base_face_id >= 0 ? base_face_id
6115 : NILP (Vface_remapping_alist) ? DEFAULT_FACE_ID
6116 : lookup_basic_face (f, DEFAULT_FACE_ID));
6118 /* Optimize common cases where we can use the default face. */
6119 if (noverlays == 0
6120 && NILP (prop)
6121 && !(pos >= region_beg && pos < region_end))
6122 return default_face->id;
6124 /* Begin with attributes from the default face. */
6125 memcpy (attrs, default_face->lface, sizeof attrs);
6127 /* Merge in attributes specified via text properties. */
6128 if (!NILP (prop))
6129 merge_face_ref (f, prop, attrs, 1, 0);
6131 /* Now merge the overlay data. */
6132 noverlays = sort_overlays (overlay_vec, noverlays, w);
6133 for (i = 0; i < noverlays; i++)
6135 Lisp_Object oend;
6136 int oendpos;
6138 prop = Foverlay_get (overlay_vec[i], propname);
6139 if (!NILP (prop))
6140 merge_face_ref (f, prop, attrs, 1, 0);
6142 oend = OVERLAY_END (overlay_vec[i]);
6143 oendpos = OVERLAY_POSITION (oend);
6144 if (oendpos < endpos)
6145 endpos = oendpos;
6148 /* If in the region, merge in the region face. */
6149 if (pos >= region_beg && pos < region_end)
6151 merge_named_face (f, Qregion, attrs, 0);
6153 if (region_end < endpos)
6154 endpos = region_end;
6157 *endptr = endpos;
6159 /* Look up a realized face with the given face attributes,
6160 or realize a new one for ASCII characters. */
6161 return lookup_face (f, attrs);
6164 /* Return the face ID at buffer position POS for displaying ASCII
6165 characters associated with overlay strings for overlay OVERLAY.
6167 Like face_at_buffer_position except for OVERLAY. Currently it
6168 simply disregards the `face' properties of all overlays. */
6171 face_for_overlay_string (struct window *w, EMACS_INT pos,
6172 EMACS_INT region_beg, EMACS_INT region_end,
6173 EMACS_INT *endptr, EMACS_INT limit,
6174 int mouse, Lisp_Object overlay)
6176 struct frame *f = XFRAME (w->frame);
6177 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6178 Lisp_Object prop, position;
6179 Lisp_Object frame;
6180 int endpos;
6181 Lisp_Object propname = mouse ? Qmouse_face : Qface;
6182 Lisp_Object limit1, end;
6183 struct face *default_face;
6185 /* W must display the current buffer. We could write this function
6186 to use the frame and buffer of W, but right now it doesn't. */
6187 /* xassert (XBUFFER (w->buffer) == current_buffer); */
6189 XSETFRAME (frame, f);
6190 XSETFASTINT (position, pos);
6192 endpos = ZV;
6193 if (pos < region_beg && region_beg < endpos)
6194 endpos = region_beg;
6196 /* Get the `face' or `mouse_face' text property at POS, and
6197 determine the next position at which the property changes. */
6198 prop = Fget_text_property (position, propname, w->buffer);
6199 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
6200 end = Fnext_single_property_change (position, propname, w->buffer, limit1);
6201 if (INTEGERP (end))
6202 endpos = XINT (end);
6204 *endptr = endpos;
6206 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6208 /* Optimize common cases where we can use the default face. */
6209 if (NILP (prop)
6210 && !(pos >= region_beg && pos < region_end))
6211 return DEFAULT_FACE_ID;
6213 /* Begin with attributes from the default face. */
6214 memcpy (attrs, default_face->lface, sizeof attrs);
6216 /* Merge in attributes specified via text properties. */
6217 if (!NILP (prop))
6218 merge_face_ref (f, prop, attrs, 1, 0);
6220 /* If in the region, merge in the region face. */
6221 if (pos >= region_beg && pos < region_end)
6223 merge_named_face (f, Qregion, attrs, 0);
6225 if (region_end < endpos)
6226 endpos = region_end;
6229 *endptr = endpos;
6231 /* Look up a realized face with the given face attributes,
6232 or realize a new one for ASCII characters. */
6233 return lookup_face (f, attrs);
6237 /* Compute the face at character position POS in Lisp string STRING on
6238 window W, for ASCII characters.
6240 If STRING is an overlay string, it comes from position BUFPOS in
6241 current_buffer, otherwise BUFPOS is zero to indicate that STRING is
6242 not an overlay string. W must display the current buffer.
6243 REGION_BEG and REGION_END give the start and end positions of the
6244 region; both are -1 if no region is visible.
6246 BASE_FACE_ID is the id of a face to merge with. For strings coming
6247 from overlays or the `display' property it is the face at BUFPOS.
6249 If MOUSE_P is non-zero, use the character's mouse-face, not its face.
6251 Set *ENDPTR to the next position where to check for faces in
6252 STRING; -1 if the face is constant from POS to the end of the
6253 string.
6255 Value is the id of the face to use. The face returned is suitable
6256 for displaying ASCII characters. */
6259 face_at_string_position (struct window *w, Lisp_Object string,
6260 EMACS_INT pos, EMACS_INT bufpos,
6261 EMACS_INT region_beg, EMACS_INT region_end,
6262 EMACS_INT *endptr, enum face_id base_face_id,
6263 int mouse_p)
6265 Lisp_Object prop, position, end, limit;
6266 struct frame *f = XFRAME (WINDOW_FRAME (w));
6267 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6268 struct face *base_face;
6269 int multibyte_p = STRING_MULTIBYTE (string);
6270 Lisp_Object prop_name = mouse_p ? Qmouse_face : Qface;
6272 /* Get the value of the face property at the current position within
6273 STRING. Value is nil if there is no face property. */
6274 XSETFASTINT (position, pos);
6275 prop = Fget_text_property (position, prop_name, string);
6277 /* Get the next position at which to check for faces. Value of end
6278 is nil if face is constant all the way to the end of the string.
6279 Otherwise it is a string position where to check faces next.
6280 Limit is the maximum position up to which to check for property
6281 changes in Fnext_single_property_change. Strings are usually
6282 short, so set the limit to the end of the string. */
6283 XSETFASTINT (limit, SCHARS (string));
6284 end = Fnext_single_property_change (position, prop_name, string, limit);
6285 if (INTEGERP (end))
6286 *endptr = XFASTINT (end);
6287 else
6288 *endptr = -1;
6290 base_face = FACE_FROM_ID (f, base_face_id);
6291 xassert (base_face);
6293 /* Optimize the default case that there is no face property and we
6294 are not in the region. */
6295 if (NILP (prop)
6296 && (base_face_id != DEFAULT_FACE_ID
6297 /* BUFPOS <= 0 means STRING is not an overlay string, so
6298 that the region doesn't have to be taken into account. */
6299 || bufpos <= 0
6300 || bufpos < region_beg
6301 || bufpos >= region_end)
6302 && (multibyte_p
6303 /* We can't realize faces for different charsets differently
6304 if we don't have fonts, so we can stop here if not working
6305 on a window-system frame. */
6306 || !FRAME_WINDOW_P (f)
6307 || FACE_SUITABLE_FOR_CHAR_P (base_face, 0)))
6308 return base_face->id;
6310 /* Begin with attributes from the base face. */
6311 memcpy (attrs, base_face->lface, sizeof attrs);
6313 /* Merge in attributes specified via text properties. */
6314 if (!NILP (prop))
6315 merge_face_ref (f, prop, attrs, 1, 0);
6317 /* If in the region, merge in the region face. */
6318 if (bufpos
6319 && bufpos >= region_beg
6320 && bufpos < region_end)
6321 merge_named_face (f, Qregion, attrs, 0);
6323 /* Look up a realized face with the given face attributes,
6324 or realize a new one for ASCII characters. */
6325 return lookup_face (f, attrs);
6329 /* Merge a face into a realized face.
6331 F is frame where faces are (to be) realized.
6333 FACE_NAME is named face to merge.
6335 If FACE_NAME is nil, FACE_ID is face_id of realized face to merge.
6337 If FACE_NAME is t, FACE_ID is lface_id of face to merge.
6339 BASE_FACE_ID is realized face to merge into.
6341 Return new face id.
6345 merge_faces (struct frame *f, Lisp_Object face_name, int face_id, int base_face_id)
6347 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6348 struct face *base_face;
6350 base_face = FACE_FROM_ID (f, base_face_id);
6351 if (!base_face)
6352 return base_face_id;
6354 if (EQ (face_name, Qt))
6356 if (face_id < 0 || face_id >= lface_id_to_name_size)
6357 return base_face_id;
6358 face_name = lface_id_to_name[face_id];
6359 /* When called during make-frame, lookup_derived_face may fail
6360 if the faces are uninitialized. Don't signal an error. */
6361 face_id = lookup_derived_face (f, face_name, base_face_id, 0);
6362 return (face_id >= 0 ? face_id : base_face_id);
6365 /* Begin with attributes from the base face. */
6366 memcpy (attrs, base_face->lface, sizeof attrs);
6368 if (!NILP (face_name))
6370 if (!merge_named_face (f, face_name, attrs, 0))
6371 return base_face_id;
6373 else
6375 struct face *face;
6376 if (face_id < 0)
6377 return base_face_id;
6378 face = FACE_FROM_ID (f, face_id);
6379 if (!face)
6380 return base_face_id;
6381 merge_face_vectors (f, face->lface, attrs, 0);
6384 /* Look up a realized face with the given face attributes,
6385 or realize a new one for ASCII characters. */
6386 return lookup_face (f, attrs);
6391 #ifndef HAVE_X_WINDOWS
6392 DEFUN ("x-load-color-file", Fx_load_color_file,
6393 Sx_load_color_file, 1, 1, 0,
6394 doc: /* Create an alist of color entries from an external file.
6396 The file should define one named RGB color per line like so:
6397 R G B name
6398 where R,G,B are numbers between 0 and 255 and name is an arbitrary string. */)
6399 (Lisp_Object filename)
6401 FILE *fp;
6402 Lisp_Object cmap = Qnil;
6403 Lisp_Object abspath;
6405 CHECK_STRING (filename);
6406 abspath = Fexpand_file_name (filename, Qnil);
6408 fp = fopen (SDATA (filename), "rt");
6409 if (fp)
6411 char buf[512];
6412 int red, green, blue;
6413 int num;
6415 BLOCK_INPUT;
6417 while (fgets (buf, sizeof (buf), fp) != NULL) {
6418 if (sscanf (buf, "%u %u %u %n", &red, &green, &blue, &num) == 3)
6420 char *name = buf + num;
6421 num = strlen (name) - 1;
6422 if (num >= 0 && name[num] == '\n')
6423 name[num] = 0;
6424 cmap = Fcons (Fcons (build_string (name),
6425 #ifdef WINDOWSNT
6426 make_number (RGB (red, green, blue))),
6427 #else
6428 make_number ((red << 16) | (green << 8) | blue)),
6429 #endif
6430 cmap);
6433 fclose (fp);
6435 UNBLOCK_INPUT;
6438 return cmap;
6440 #endif
6443 /***********************************************************************
6444 Tests
6445 ***********************************************************************/
6447 #if GLYPH_DEBUG
6449 /* Print the contents of the realized face FACE to stderr. */
6451 static void
6452 dump_realized_face (face)
6453 struct face *face;
6455 fprintf (stderr, "ID: %d\n", face->id);
6456 #ifdef HAVE_X_WINDOWS
6457 fprintf (stderr, "gc: %ld\n", (long) face->gc);
6458 #endif
6459 fprintf (stderr, "foreground: 0x%lx (%s)\n",
6460 face->foreground,
6461 SDATA (face->lface[LFACE_FOREGROUND_INDEX]));
6462 fprintf (stderr, "background: 0x%lx (%s)\n",
6463 face->background,
6464 SDATA (face->lface[LFACE_BACKGROUND_INDEX]));
6465 if (face->font)
6466 fprintf (stderr, "font_name: %s (%s)\n",
6467 SDATA (face->font->props[FONT_NAME_INDEX]),
6468 SDATA (face->lface[LFACE_FAMILY_INDEX]));
6469 #ifdef HAVE_X_WINDOWS
6470 fprintf (stderr, "font = %p\n", face->font);
6471 #endif
6472 fprintf (stderr, "fontset: %d\n", face->fontset);
6473 fprintf (stderr, "underline: %d (%s)\n",
6474 face->underline_p,
6475 SDATA (Fsymbol_name (face->lface[LFACE_UNDERLINE_INDEX])));
6476 fprintf (stderr, "hash: %d\n", face->hash);
6480 DEFUN ("dump-face", Fdump_face, Sdump_face, 0, 1, 0, doc: /* */)
6481 (Lisp_Object n)
6483 if (NILP (n))
6485 int i;
6487 fprintf (stderr, "font selection order: ");
6488 for (i = 0; i < DIM (font_sort_order); ++i)
6489 fprintf (stderr, "%d ", font_sort_order[i]);
6490 fprintf (stderr, "\n");
6492 fprintf (stderr, "alternative fonts: ");
6493 debug_print (Vface_alternative_font_family_alist);
6494 fprintf (stderr, "\n");
6496 for (i = 0; i < FRAME_FACE_CACHE (SELECTED_FRAME ())->used; ++i)
6497 Fdump_face (make_number (i));
6499 else
6501 struct face *face;
6502 CHECK_NUMBER (n);
6503 face = FACE_FROM_ID (SELECTED_FRAME (), XINT (n));
6504 if (face == NULL)
6505 error ("Not a valid face");
6506 dump_realized_face (face);
6509 return Qnil;
6513 DEFUN ("show-face-resources", Fshow_face_resources, Sshow_face_resources,
6514 0, 0, 0, doc: /* */)
6515 (void)
6517 fprintf (stderr, "number of colors = %d\n", ncolors_allocated);
6518 fprintf (stderr, "number of pixmaps = %d\n", npixmaps_allocated);
6519 fprintf (stderr, "number of GCs = %d\n", ngcs);
6520 return Qnil;
6523 #endif /* GLYPH_DEBUG != 0 */
6527 /***********************************************************************
6528 Initialization
6529 ***********************************************************************/
6531 void
6532 syms_of_xfaces (void)
6534 Qface = intern_c_string ("face");
6535 staticpro (&Qface);
6536 Qface_no_inherit = intern_c_string ("face-no-inherit");
6537 staticpro (&Qface_no_inherit);
6538 Qbitmap_spec_p = intern_c_string ("bitmap-spec-p");
6539 staticpro (&Qbitmap_spec_p);
6540 Qframe_set_background_mode = intern_c_string ("frame-set-background-mode");
6541 staticpro (&Qframe_set_background_mode);
6543 /* Lisp face attribute keywords. */
6544 QCfamily = intern_c_string (":family");
6545 staticpro (&QCfamily);
6546 QCheight = intern_c_string (":height");
6547 staticpro (&QCheight);
6548 QCweight = intern_c_string (":weight");
6549 staticpro (&QCweight);
6550 QCslant = intern_c_string (":slant");
6551 staticpro (&QCslant);
6552 QCunderline = intern_c_string (":underline");
6553 staticpro (&QCunderline);
6554 QCinverse_video = intern_c_string (":inverse-video");
6555 staticpro (&QCinverse_video);
6556 QCreverse_video = intern_c_string (":reverse-video");
6557 staticpro (&QCreverse_video);
6558 QCforeground = intern_c_string (":foreground");
6559 staticpro (&QCforeground);
6560 QCbackground = intern_c_string (":background");
6561 staticpro (&QCbackground);
6562 QCstipple = intern_c_string (":stipple");
6563 staticpro (&QCstipple);
6564 QCwidth = intern_c_string (":width");
6565 staticpro (&QCwidth);
6566 QCfont = intern_c_string (":font");
6567 staticpro (&QCfont);
6568 QCfontset = intern_c_string (":fontset");
6569 staticpro (&QCfontset);
6570 QCbold = intern_c_string (":bold");
6571 staticpro (&QCbold);
6572 QCitalic = intern_c_string (":italic");
6573 staticpro (&QCitalic);
6574 QCoverline = intern_c_string (":overline");
6575 staticpro (&QCoverline);
6576 QCstrike_through = intern_c_string (":strike-through");
6577 staticpro (&QCstrike_through);
6578 QCbox = intern_c_string (":box");
6579 staticpro (&QCbox);
6580 QCinherit = intern_c_string (":inherit");
6581 staticpro (&QCinherit);
6583 /* Symbols used for Lisp face attribute values. */
6584 QCcolor = intern_c_string (":color");
6585 staticpro (&QCcolor);
6586 QCline_width = intern_c_string (":line-width");
6587 staticpro (&QCline_width);
6588 QCstyle = intern_c_string (":style");
6589 staticpro (&QCstyle);
6590 Qreleased_button = intern_c_string ("released-button");
6591 staticpro (&Qreleased_button);
6592 Qpressed_button = intern_c_string ("pressed-button");
6593 staticpro (&Qpressed_button);
6594 Qnormal = intern_c_string ("normal");
6595 staticpro (&Qnormal);
6596 Qultra_light = intern_c_string ("ultra-light");
6597 staticpro (&Qultra_light);
6598 Qextra_light = intern_c_string ("extra-light");
6599 staticpro (&Qextra_light);
6600 Qlight = intern_c_string ("light");
6601 staticpro (&Qlight);
6602 Qsemi_light = intern_c_string ("semi-light");
6603 staticpro (&Qsemi_light);
6604 Qsemi_bold = intern_c_string ("semi-bold");
6605 staticpro (&Qsemi_bold);
6606 Qbold = intern_c_string ("bold");
6607 staticpro (&Qbold);
6608 Qextra_bold = intern_c_string ("extra-bold");
6609 staticpro (&Qextra_bold);
6610 Qultra_bold = intern_c_string ("ultra-bold");
6611 staticpro (&Qultra_bold);
6612 Qoblique = intern_c_string ("oblique");
6613 staticpro (&Qoblique);
6614 Qitalic = intern_c_string ("italic");
6615 staticpro (&Qitalic);
6616 Qreverse_oblique = intern_c_string ("reverse-oblique");
6617 staticpro (&Qreverse_oblique);
6618 Qreverse_italic = intern_c_string ("reverse-italic");
6619 staticpro (&Qreverse_italic);
6620 Qultra_condensed = intern_c_string ("ultra-condensed");
6621 staticpro (&Qultra_condensed);
6622 Qextra_condensed = intern_c_string ("extra-condensed");
6623 staticpro (&Qextra_condensed);
6624 Qcondensed = intern_c_string ("condensed");
6625 staticpro (&Qcondensed);
6626 Qsemi_condensed = intern_c_string ("semi-condensed");
6627 staticpro (&Qsemi_condensed);
6628 Qsemi_expanded = intern_c_string ("semi-expanded");
6629 staticpro (&Qsemi_expanded);
6630 Qexpanded = intern_c_string ("expanded");
6631 staticpro (&Qexpanded);
6632 Qextra_expanded = intern_c_string ("extra-expanded");
6633 staticpro (&Qextra_expanded);
6634 Qultra_expanded = intern_c_string ("ultra-expanded");
6635 staticpro (&Qultra_expanded);
6636 Qbackground_color = intern_c_string ("background-color");
6637 staticpro (&Qbackground_color);
6638 Qforeground_color = intern_c_string ("foreground-color");
6639 staticpro (&Qforeground_color);
6640 Qunspecified = intern_c_string ("unspecified");
6641 staticpro (&Qunspecified);
6642 Qignore_defface = intern_c_string (":ignore-defface");
6643 staticpro (&Qignore_defface);
6645 Qface_alias = intern_c_string ("face-alias");
6646 staticpro (&Qface_alias);
6647 Qdefault = intern_c_string ("default");
6648 staticpro (&Qdefault);
6649 Qtool_bar = intern_c_string ("tool-bar");
6650 staticpro (&Qtool_bar);
6651 Qregion = intern_c_string ("region");
6652 staticpro (&Qregion);
6653 Qfringe = intern_c_string ("fringe");
6654 staticpro (&Qfringe);
6655 Qheader_line = intern_c_string ("header-line");
6656 staticpro (&Qheader_line);
6657 Qscroll_bar = intern_c_string ("scroll-bar");
6658 staticpro (&Qscroll_bar);
6659 Qmenu = intern_c_string ("menu");
6660 staticpro (&Qmenu);
6661 Qcursor = intern_c_string ("cursor");
6662 staticpro (&Qcursor);
6663 Qborder = intern_c_string ("border");
6664 staticpro (&Qborder);
6665 Qmouse = intern_c_string ("mouse");
6666 staticpro (&Qmouse);
6667 Qmode_line_inactive = intern_c_string ("mode-line-inactive");
6668 staticpro (&Qmode_line_inactive);
6669 Qvertical_border = intern_c_string ("vertical-border");
6670 staticpro (&Qvertical_border);
6671 Qtty_color_desc = intern_c_string ("tty-color-desc");
6672 staticpro (&Qtty_color_desc);
6673 Qtty_color_standard_values = intern_c_string ("tty-color-standard-values");
6674 staticpro (&Qtty_color_standard_values);
6675 Qtty_color_by_index = intern_c_string ("tty-color-by-index");
6676 staticpro (&Qtty_color_by_index);
6677 Qtty_color_alist = intern_c_string ("tty-color-alist");
6678 staticpro (&Qtty_color_alist);
6679 Qscalable_fonts_allowed = intern_c_string ("scalable-fonts-allowed");
6680 staticpro (&Qscalable_fonts_allowed);
6682 Vparam_value_alist = Fcons (Fcons (Qnil, Qnil), Qnil);
6683 staticpro (&Vparam_value_alist);
6684 Vface_alternative_font_family_alist = Qnil;
6685 staticpro (&Vface_alternative_font_family_alist);
6686 Vface_alternative_font_registry_alist = Qnil;
6687 staticpro (&Vface_alternative_font_registry_alist);
6689 defsubr (&Sinternal_make_lisp_face);
6690 defsubr (&Sinternal_lisp_face_p);
6691 defsubr (&Sinternal_set_lisp_face_attribute);
6692 #ifdef HAVE_WINDOW_SYSTEM
6693 defsubr (&Sinternal_set_lisp_face_attribute_from_resource);
6694 #endif
6695 defsubr (&Scolor_gray_p);
6696 defsubr (&Scolor_supported_p);
6697 #ifndef HAVE_X_WINDOWS
6698 defsubr (&Sx_load_color_file);
6699 #endif
6700 defsubr (&Sface_attribute_relative_p);
6701 defsubr (&Smerge_face_attribute);
6702 defsubr (&Sinternal_get_lisp_face_attribute);
6703 defsubr (&Sinternal_lisp_face_attribute_values);
6704 defsubr (&Sinternal_lisp_face_equal_p);
6705 defsubr (&Sinternal_lisp_face_empty_p);
6706 defsubr (&Sinternal_copy_lisp_face);
6707 defsubr (&Sinternal_merge_in_global_face);
6708 defsubr (&Sface_font);
6709 defsubr (&Sframe_face_alist);
6710 defsubr (&Sdisplay_supports_face_attributes_p);
6711 defsubr (&Scolor_distance);
6712 defsubr (&Sinternal_set_font_selection_order);
6713 defsubr (&Sinternal_set_alternative_font_family_alist);
6714 defsubr (&Sinternal_set_alternative_font_registry_alist);
6715 defsubr (&Sface_attributes_as_vector);
6716 #if GLYPH_DEBUG
6717 defsubr (&Sdump_face);
6718 defsubr (&Sshow_face_resources);
6719 #endif /* GLYPH_DEBUG */
6720 defsubr (&Sclear_face_cache);
6721 defsubr (&Stty_suppress_bold_inverse_default_colors);
6723 #if defined DEBUG_X_COLORS && defined HAVE_X_WINDOWS
6724 defsubr (&Sdump_colors);
6725 #endif
6727 DEFVAR_LISP ("font-list-limit", &Vfont_list_limit,
6728 doc: /* *Limit for font matching.
6729 If an integer > 0, font matching functions won't load more than
6730 that number of fonts when searching for a matching font. */);
6731 Vfont_list_limit = make_number (DEFAULT_FONT_LIST_LIMIT);
6733 DEFVAR_LISP ("face-new-frame-defaults", &Vface_new_frame_defaults,
6734 doc: /* List of global face definitions (for internal use only.) */);
6735 Vface_new_frame_defaults = Qnil;
6737 DEFVAR_LISP ("face-default-stipple", &Vface_default_stipple,
6738 doc: /* *Default stipple pattern used on monochrome displays.
6739 This stipple pattern is used on monochrome displays
6740 instead of shades of gray for a face background color.
6741 See `set-face-stipple' for possible values for this variable. */);
6742 Vface_default_stipple = make_pure_c_string ("gray3");
6744 DEFVAR_LISP ("tty-defined-color-alist", &Vtty_defined_color_alist,
6745 doc: /* An alist of defined terminal colors and their RGB values. */);
6746 Vtty_defined_color_alist = Qnil;
6748 DEFVAR_LISP ("scalable-fonts-allowed", &Vscalable_fonts_allowed,
6749 doc: /* Allowed scalable fonts.
6750 A value of nil means don't allow any scalable fonts.
6751 A value of t means allow any scalable font.
6752 Otherwise, value must be a list of regular expressions. A font may be
6753 scaled if its name matches a regular expression in the list.
6754 Note that if value is nil, a scalable font might still be used, if no
6755 other font of the appropriate family and registry is available. */);
6756 Vscalable_fonts_allowed = Qnil;
6758 DEFVAR_LISP ("face-ignored-fonts", &Vface_ignored_fonts,
6759 doc: /* List of ignored fonts.
6760 Each element is a regular expression that matches names of fonts to
6761 ignore. */);
6762 Vface_ignored_fonts = Qnil;
6764 DEFVAR_LISP ("face-remapping-alist", &Vface_remapping_alist,
6765 doc: /* Alist of face remappings.
6766 Each element is of the form:
6768 (FACE REPLACEMENT...),
6770 which causes display of the face FACE to use REPLACEMENT... instead.
6771 REPLACEMENT... is interpreted the same way as the value of a `face'
6772 text property: it may be (1) A face name, (2) A list of face names,
6773 (3) A property-list of face attribute/value pairs, or (4) A list of
6774 face names or lists containing face attribute/value pairs.
6776 Multiple entries in REPLACEMENT... are merged together to form the final
6777 result, with faces or attributes earlier in the list taking precedence
6778 over those that are later.
6780 Face-name remapping cycles are suppressed; recursive references use the
6781 underlying face instead of the remapped face. So a remapping of the form:
6783 (FACE EXTRA-FACE... FACE)
6787 (FACE (FACE-ATTR VAL ...) FACE)
6789 will cause EXTRA-FACE... or (FACE-ATTR VAL ...) to be _merged_ with the
6790 existing definition of FACE. Note that for the default face, this isn't
6791 necessary, as every face inherits from the default face.
6793 Making this variable buffer-local is a good way to allow buffer-specific
6794 face definitions. For instance, the mode my-mode could define a face
6795 `my-mode-default', and then in the mode setup function, do:
6797 (set (make-local-variable 'face-remapping-alist)
6798 '((default my-mode-default)))).
6800 Because Emacs normally only redraws screen areas when the underlying
6801 buffer contents change, you may need to call `redraw-display' after
6802 changing this variable for it to take effect. */);
6803 Vface_remapping_alist = Qnil;
6805 DEFVAR_LISP ("face-font-rescale-alist", &Vface_font_rescale_alist,
6806 doc: /* Alist of fonts vs the rescaling factors.
6807 Each element is a cons (FONT-PATTERN . RESCALE-RATIO), where
6808 FONT-PATTERN is a font-spec or a regular expression matching a font name, and
6809 RESCALE-RATIO is a floating point number to specify how much larger
6810 \(or smaller) font we should use. For instance, if a face requests
6811 a font of 10 point, we actually use a font of 10 * RESCALE-RATIO point. */);
6812 Vface_font_rescale_alist = Qnil;
6814 #ifdef HAVE_WINDOW_SYSTEM
6815 defsubr (&Sbitmap_spec_p);
6816 defsubr (&Sx_list_fonts);
6817 defsubr (&Sinternal_face_x_get_resource);
6818 defsubr (&Sx_family_fonts);
6819 #endif
6822 /* arch-tag: 8a0f7598-5517-408d-9ab3-1da6fcd4c749
6823 (do not change this comment) */