* w32fns.c, xfaces.c: Remove obsolete static declarations.
[emacs.git] / src / xfaces.c
blobc0c53f3aa1fb3d67e546f0504b296d9eb7f48f69
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;
414 extern Lisp_Object Qmouse_face;
416 /* Property for basic faces which other faces cannot inherit. */
418 Lisp_Object Qface_no_inherit;
420 /* Error symbol for wrong_type_argument in load_pixmap. */
422 Lisp_Object Qbitmap_spec_p;
424 /* Alist of global face definitions. Each element is of the form
425 (FACE . LFACE) where FACE is a symbol naming a face and LFACE
426 is a Lisp vector of face attributes. These faces are used
427 to initialize faces for new frames. */
429 Lisp_Object Vface_new_frame_defaults;
431 /* Alist of face remappings. Each element is of the form:
432 (FACE REPLACEMENT...) which causes display of the face FACE to use
433 REPLACEMENT... instead. REPLACEMENT... is interpreted the same way
434 the value of a `face' text property is: it may be (1) A face name,
435 (2) A list of face names, (3) A property-list of face attribute/value
436 pairs, or (4) A list of face names intermixed with lists containing
437 face attribute/value pairs.
439 Multiple entries in REPLACEMENT... are merged together to form the final
440 result, with faces or attributes earlier in the list taking precedence
441 over those that are later.
443 Face-name remapping cycles are suppressed; recursive references use
444 the underlying face instead of the remapped face. */
446 Lisp_Object Vface_remapping_alist;
448 /* The next ID to assign to Lisp faces. */
450 static int next_lface_id;
452 /* A vector mapping Lisp face Id's to face names. */
454 static Lisp_Object *lface_id_to_name;
455 static int lface_id_to_name_size;
457 /* TTY color-related functions (defined in tty-colors.el). */
459 Lisp_Object Qtty_color_desc, Qtty_color_by_index, Qtty_color_standard_values;
461 /* The name of the function used to compute colors on TTYs. */
463 Lisp_Object Qtty_color_alist;
465 /* An alist of defined terminal colors and their RGB values. */
467 Lisp_Object Vtty_defined_color_alist;
469 /* Counter for calls to clear_face_cache. If this counter reaches
470 CLEAR_FONT_TABLE_COUNT, and a frame has more than
471 CLEAR_FONT_TABLE_NFONTS load, unused fonts are freed. */
473 static int clear_font_table_count;
474 #define CLEAR_FONT_TABLE_COUNT 100
475 #define CLEAR_FONT_TABLE_NFONTS 10
477 /* Non-zero means face attributes have been changed since the last
478 redisplay. Used in redisplay_internal. */
480 int face_change_count;
482 /* Non-zero means don't display bold text if a face's foreground
483 and background colors are the inverse of the default colors of the
484 display. This is a kluge to suppress `bold black' foreground text
485 which is hard to read on an LCD monitor. */
487 int tty_suppress_bold_inverse_default_colors_p;
489 /* A list of the form `((x . y))' used to avoid consing in
490 Finternal_set_lisp_face_attribute. */
492 static Lisp_Object Vparam_value_alist;
494 /* The total number of colors currently allocated. */
496 #if GLYPH_DEBUG
497 static int ncolors_allocated;
498 static int npixmaps_allocated;
499 static int ngcs;
500 #endif
502 /* Non-zero means the definition of the `menu' face for new frames has
503 been changed. */
505 int menu_face_changed_default;
508 /* Function prototypes. */
510 struct table_entry;
511 struct named_merge_point;
513 static void map_tty_color (struct frame *, struct face *,
514 enum lface_attribute_index, int *);
515 static Lisp_Object resolve_face_name (Lisp_Object, int);
516 static void set_font_frame_param (Lisp_Object, Lisp_Object);
517 static int get_lface_attributes (struct frame *, Lisp_Object, Lisp_Object *,
518 int, struct named_merge_point *);
519 static int load_pixmap (struct frame *, Lisp_Object, unsigned *, unsigned *);
520 static struct frame *frame_or_selected_frame (Lisp_Object, int);
521 static void load_face_colors (struct frame *, struct face *, Lisp_Object *);
522 static void free_face_colors (struct frame *, struct face *);
523 static int face_color_gray_p (struct frame *, char *);
524 static struct face *realize_face (struct face_cache *, Lisp_Object *,
525 int);
526 static struct face *realize_non_ascii_face (struct frame *, Lisp_Object,
527 struct face *);
528 static struct face *realize_x_face (struct face_cache *, Lisp_Object *);
529 static struct face *realize_tty_face (struct face_cache *, Lisp_Object *);
530 static int realize_basic_faces (struct frame *);
531 static int realize_default_face (struct frame *);
532 static void realize_named_face (struct frame *, Lisp_Object, int);
533 static int lface_fully_specified_p (Lisp_Object *);
534 static int lface_equal_p (Lisp_Object *, Lisp_Object *);
535 static unsigned hash_string_case_insensitive (Lisp_Object);
536 static unsigned lface_hash (Lisp_Object *);
537 static int lface_same_font_attributes_p (Lisp_Object *, Lisp_Object *);
538 static struct face_cache *make_face_cache (struct frame *);
539 static void clear_face_gcs (struct face_cache *);
540 static void free_face_cache (struct face_cache *);
541 static int face_fontset (Lisp_Object *);
542 static void merge_face_vectors (struct frame *, Lisp_Object *, Lisp_Object*,
543 struct named_merge_point *);
544 static int merge_face_ref (struct frame *, Lisp_Object, Lisp_Object *,
545 int, struct named_merge_point *);
546 static int set_lface_from_font (struct frame *, Lisp_Object, Lisp_Object,
547 int);
548 static Lisp_Object lface_from_face_name (struct frame *, Lisp_Object, int);
549 static struct face *make_realized_face (Lisp_Object *);
550 static void cache_face (struct face_cache *, struct face *, unsigned);
551 static void uncache_face (struct face_cache *, struct face *);
553 #ifdef HAVE_WINDOW_SYSTEM
555 static GC x_create_gc (struct frame *, unsigned long, XGCValues *);
556 static void x_free_gc (struct frame *, GC);
558 #ifdef USE_X_TOOLKIT
559 static void x_update_menu_appearance (struct frame *);
561 extern void free_frame_menubar (struct frame *);
562 #endif /* USE_X_TOOLKIT */
564 #endif /* HAVE_WINDOW_SYSTEM */
567 /***********************************************************************
568 Utilities
569 ***********************************************************************/
571 #ifdef HAVE_X_WINDOWS
573 #ifdef DEBUG_X_COLORS
575 /* The following is a poor mans infrastructure for debugging X color
576 allocation problems on displays with PseudoColor-8. Some X servers
577 like 3.3.5 XF86_SVGA with Matrox cards apparently don't implement
578 color reference counts completely so that they don't signal an
579 error when a color is freed whose reference count is already 0.
580 Other X servers do. To help me debug this, the following code
581 implements a simple reference counting schema of its own, for a
582 single display/screen. --gerd. */
584 /* Reference counts for pixel colors. */
586 int color_count[256];
588 /* Register color PIXEL as allocated. */
590 void
591 register_color (pixel)
592 unsigned long pixel;
594 xassert (pixel < 256);
595 ++color_count[pixel];
599 /* Register color PIXEL as deallocated. */
601 void
602 unregister_color (pixel)
603 unsigned long pixel;
605 xassert (pixel < 256);
606 if (color_count[pixel] > 0)
607 --color_count[pixel];
608 else
609 abort ();
613 /* Register N colors from PIXELS as deallocated. */
615 void
616 unregister_colors (pixels, n)
617 unsigned long *pixels;
618 int n;
620 int i;
621 for (i = 0; i < n; ++i)
622 unregister_color (pixels[i]);
626 DEFUN ("dump-colors", Fdump_colors, Sdump_colors, 0, 0, 0,
627 doc: /* Dump currently allocated colors to stderr. */)
628 (void)
630 int i, n;
632 fputc ('\n', stderr);
634 for (i = n = 0; i < sizeof color_count / sizeof color_count[0]; ++i)
635 if (color_count[i])
637 fprintf (stderr, "%3d: %5d", i, color_count[i]);
638 ++n;
639 if (n % 5 == 0)
640 fputc ('\n', stderr);
641 else
642 fputc ('\t', stderr);
645 if (n % 5 != 0)
646 fputc ('\n', stderr);
647 return Qnil;
650 #endif /* DEBUG_X_COLORS */
653 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
654 color values. Interrupt input must be blocked when this function
655 is called. */
657 void
658 x_free_colors (struct frame *f, long unsigned int *pixels, int npixels)
660 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
662 /* If display has an immutable color map, freeing colors is not
663 necessary and some servers don't allow it. So don't do it. */
664 if (class != StaticColor && class != StaticGray && class != TrueColor)
666 #ifdef DEBUG_X_COLORS
667 unregister_colors (pixels, npixels);
668 #endif
669 XFreeColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
670 pixels, npixels, 0);
675 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
676 color values. Interrupt input must be blocked when this function
677 is called. */
679 void
680 x_free_dpy_colors (Display *dpy, Screen *screen, Colormap cmap, long unsigned int *pixels, int npixels)
682 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
683 int class = dpyinfo->visual->class;
685 /* If display has an immutable color map, freeing colors is not
686 necessary and some servers don't allow it. So don't do it. */
687 if (class != StaticColor && class != StaticGray && class != TrueColor)
689 #ifdef DEBUG_X_COLORS
690 unregister_colors (pixels, npixels);
691 #endif
692 XFreeColors (dpy, cmap, pixels, npixels, 0);
697 /* Create and return a GC for use on frame F. GC values and mask
698 are given by XGCV and MASK. */
700 static INLINE GC
701 x_create_gc (struct frame *f, long unsigned int mask, XGCValues *xgcv)
703 GC gc;
704 BLOCK_INPUT;
705 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), mask, xgcv);
706 UNBLOCK_INPUT;
707 IF_DEBUG (++ngcs);
708 return gc;
712 /* Free GC which was used on frame F. */
714 static INLINE void
715 x_free_gc (struct frame *f, GC gc)
717 eassert (interrupt_input_blocked);
718 IF_DEBUG (xassert (--ngcs >= 0));
719 XFreeGC (FRAME_X_DISPLAY (f), gc);
722 #endif /* HAVE_X_WINDOWS */
724 #ifdef WINDOWSNT
725 /* W32 emulation of GCs */
727 static INLINE GC
728 x_create_gc (struct frame *f, unsigned long mask, XGCValues *xgcv)
730 GC gc;
731 BLOCK_INPUT;
732 gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, xgcv);
733 UNBLOCK_INPUT;
734 IF_DEBUG (++ngcs);
735 return gc;
739 /* Free GC which was used on frame F. */
741 static INLINE void
742 x_free_gc (struct frame *f, GC gc)
744 IF_DEBUG (xassert (--ngcs >= 0));
745 xfree (gc);
748 #endif /* WINDOWSNT */
750 #ifdef HAVE_NS
751 /* NS emulation of GCs */
753 static INLINE GC
754 x_create_gc (f, mask, xgcv)
755 struct frame *f;
756 unsigned long mask;
757 XGCValues *xgcv;
759 GC gc = xmalloc (sizeof (*gc));
760 if (gc)
761 memcpy (gc, xgcv, sizeof (XGCValues));
762 return gc;
765 static INLINE void
766 x_free_gc (f, gc)
767 struct frame *f;
768 GC gc;
770 xfree (gc);
772 #endif /* HAVE_NS */
774 /* Like strcasecmp/stricmp. Used to compare parts of font names which
775 are in ISO8859-1. */
778 xstrcasecmp (const unsigned char *s1, const unsigned char *s2)
780 while (*s1 && *s2)
782 unsigned char c1 = tolower (*s1);
783 unsigned char c2 = tolower (*s2);
784 if (c1 != c2)
785 return c1 < c2 ? -1 : 1;
786 ++s1, ++s2;
789 if (*s1 == 0)
790 return *s2 == 0 ? 0 : -1;
791 return 1;
795 /* If FRAME is nil, return a pointer to the selected frame.
796 Otherwise, check that FRAME is a live frame, and return a pointer
797 to it. NPARAM is the parameter number of FRAME, for
798 CHECK_LIVE_FRAME. This is here because it's a frequent pattern in
799 Lisp function definitions. */
801 static INLINE struct frame *
802 frame_or_selected_frame (Lisp_Object frame, int nparam)
804 if (NILP (frame))
805 frame = selected_frame;
807 CHECK_LIVE_FRAME (frame);
808 return XFRAME (frame);
812 /***********************************************************************
813 Frames and faces
814 ***********************************************************************/
816 /* Initialize face cache and basic faces for frame F. */
818 void
819 init_frame_faces (struct frame *f)
821 /* Make a face cache, if F doesn't have one. */
822 if (FRAME_FACE_CACHE (f) == NULL)
823 FRAME_FACE_CACHE (f) = make_face_cache (f);
825 #ifdef HAVE_WINDOW_SYSTEM
826 /* Make the image cache. */
827 if (FRAME_WINDOW_P (f))
829 /* We initialize the image cache when creating the first frame
830 on a terminal, and not during terminal creation. This way,
831 `x-open-connection' on a tty won't create an image cache. */
832 if (FRAME_IMAGE_CACHE (f) == NULL)
833 FRAME_IMAGE_CACHE (f) = make_image_cache ();
834 ++FRAME_IMAGE_CACHE (f)->refcount;
836 #endif /* HAVE_WINDOW_SYSTEM */
838 /* Realize basic faces. Must have enough information in frame
839 parameters to realize basic faces at this point. */
840 #ifdef HAVE_X_WINDOWS
841 if (!FRAME_X_P (f) || FRAME_X_WINDOW (f))
842 #endif
843 #ifdef WINDOWSNT
844 if (!FRAME_WINDOW_P (f) || FRAME_W32_WINDOW (f))
845 #endif
846 #ifdef HAVE_NS
847 if (!FRAME_NS_P (f) || FRAME_NS_WINDOW (f))
848 #endif
849 if (!realize_basic_faces (f))
850 abort ();
854 /* Free face cache of frame F. Called from delete_frame. */
856 void
857 free_frame_faces (struct frame *f)
859 struct face_cache *face_cache = FRAME_FACE_CACHE (f);
861 if (face_cache)
863 free_face_cache (face_cache);
864 FRAME_FACE_CACHE (f) = NULL;
867 #ifdef HAVE_WINDOW_SYSTEM
868 if (FRAME_WINDOW_P (f))
870 struct image_cache *image_cache = FRAME_IMAGE_CACHE (f);
871 if (image_cache)
873 --image_cache->refcount;
874 if (image_cache->refcount == 0)
875 free_image_cache (f);
878 #endif /* HAVE_WINDOW_SYSTEM */
882 /* Clear face caches, and recompute basic faces for frame F. Call
883 this after changing frame parameters on which those faces depend,
884 or when realized faces have been freed due to changing attributes
885 of named faces. */
887 void
888 recompute_basic_faces (struct frame *f)
890 if (FRAME_FACE_CACHE (f))
892 clear_face_cache (0);
893 if (!realize_basic_faces (f))
894 abort ();
899 /* Clear the face caches of all frames. CLEAR_FONTS_P non-zero means
900 try to free unused fonts, too. */
902 void
903 clear_face_cache (int clear_fonts_p)
905 #ifdef HAVE_WINDOW_SYSTEM
906 Lisp_Object tail, frame;
907 struct frame *f;
909 if (clear_fonts_p
910 || ++clear_font_table_count == CLEAR_FONT_TABLE_COUNT)
912 #if 0
913 /* Not yet implemented. */
914 clear_font_cache (frame);
915 #endif
917 /* From time to time see if we can unload some fonts. This also
918 frees all realized faces on all frames. Fonts needed by
919 faces will be loaded again when faces are realized again. */
920 clear_font_table_count = 0;
922 FOR_EACH_FRAME (tail, frame)
924 struct frame *f = XFRAME (frame);
925 if (FRAME_WINDOW_P (f)
926 && FRAME_X_DISPLAY_INFO (f)->n_fonts > CLEAR_FONT_TABLE_NFONTS)
927 free_all_realized_faces (frame);
930 else
932 /* Clear GCs of realized faces. */
933 FOR_EACH_FRAME (tail, frame)
935 f = XFRAME (frame);
936 if (FRAME_WINDOW_P (f))
937 clear_face_gcs (FRAME_FACE_CACHE (f));
939 clear_image_caches (Qnil);
941 #endif /* HAVE_WINDOW_SYSTEM */
945 DEFUN ("clear-face-cache", Fclear_face_cache, Sclear_face_cache, 0, 1, 0,
946 doc: /* Clear face caches on all frames.
947 Optional THOROUGHLY non-nil means try to free unused fonts, too. */)
948 (Lisp_Object thoroughly)
950 clear_face_cache (!NILP (thoroughly));
951 ++face_change_count;
952 ++windows_or_buffers_changed;
953 return Qnil;
957 /***********************************************************************
958 X Pixmaps
959 ***********************************************************************/
961 #ifdef HAVE_WINDOW_SYSTEM
963 DEFUN ("bitmap-spec-p", Fbitmap_spec_p, Sbitmap_spec_p, 1, 1, 0,
964 doc: /* Value is non-nil if OBJECT is a valid bitmap specification.
965 A bitmap specification is either a string, a file name, or a list
966 \(WIDTH HEIGHT DATA) where WIDTH is the pixel width of the bitmap,
967 HEIGHT is its height, and DATA is a string containing the bits of
968 the pixmap. Bits are stored row by row, each row occupies
969 \(WIDTH + 7)/8 bytes. */)
970 (Lisp_Object object)
972 int pixmap_p = 0;
974 if (STRINGP (object))
975 /* If OBJECT is a string, it's a file name. */
976 pixmap_p = 1;
977 else if (CONSP (object))
979 /* Otherwise OBJECT must be (WIDTH HEIGHT DATA), WIDTH and
980 HEIGHT must be integers > 0, and DATA must be string large
981 enough to hold a bitmap of the specified size. */
982 Lisp_Object width, height, data;
984 height = width = data = Qnil;
986 if (CONSP (object))
988 width = XCAR (object);
989 object = XCDR (object);
990 if (CONSP (object))
992 height = XCAR (object);
993 object = XCDR (object);
994 if (CONSP (object))
995 data = XCAR (object);
999 if (NATNUMP (width) && NATNUMP (height) && STRINGP (data))
1001 int bytes_per_row = ((XFASTINT (width) + BITS_PER_CHAR - 1)
1002 / BITS_PER_CHAR);
1003 if (SBYTES (data) >= bytes_per_row * XINT (height))
1004 pixmap_p = 1;
1008 return pixmap_p ? Qt : Qnil;
1012 /* Load a bitmap according to NAME (which is either a file name or a
1013 pixmap spec) for use on frame F. Value is the bitmap_id (see
1014 xfns.c). If NAME is nil, return with a bitmap id of zero. If
1015 bitmap cannot be loaded, display a message saying so, and return
1016 zero. Store the bitmap width in *W_PTR and its height in *H_PTR,
1017 if these pointers are not null. */
1019 static int
1020 load_pixmap (FRAME_PTR f, Lisp_Object name, unsigned int *w_ptr, unsigned int *h_ptr)
1022 int bitmap_id;
1024 if (NILP (name))
1025 return 0;
1027 CHECK_TYPE (!NILP (Fbitmap_spec_p (name)), Qbitmap_spec_p, name);
1029 BLOCK_INPUT;
1030 if (CONSP (name))
1032 /* Decode a bitmap spec into a bitmap. */
1034 int h, w;
1035 Lisp_Object bits;
1037 w = XINT (Fcar (name));
1038 h = XINT (Fcar (Fcdr (name)));
1039 bits = Fcar (Fcdr (Fcdr (name)));
1041 bitmap_id = x_create_bitmap_from_data (f, SDATA (bits),
1042 w, h);
1044 else
1046 /* It must be a string -- a file name. */
1047 bitmap_id = x_create_bitmap_from_file (f, name);
1049 UNBLOCK_INPUT;
1051 if (bitmap_id < 0)
1053 add_to_log ("Invalid or undefined bitmap `%s'", name, Qnil);
1054 bitmap_id = 0;
1056 if (w_ptr)
1057 *w_ptr = 0;
1058 if (h_ptr)
1059 *h_ptr = 0;
1061 else
1063 #if GLYPH_DEBUG
1064 ++npixmaps_allocated;
1065 #endif
1066 if (w_ptr)
1067 *w_ptr = x_bitmap_width (f, bitmap_id);
1069 if (h_ptr)
1070 *h_ptr = x_bitmap_height (f, bitmap_id);
1073 return bitmap_id;
1076 #endif /* HAVE_WINDOW_SYSTEM */
1080 /***********************************************************************
1081 X Colors
1082 ***********************************************************************/
1084 /* Parse RGB_LIST, and fill in the RGB fields of COLOR.
1085 RGB_LIST should contain (at least) 3 lisp integers.
1086 Return 0 if there's a problem with RGB_LIST, otherwise return 1. */
1088 static int
1089 parse_rgb_list (Lisp_Object rgb_list, XColor *color)
1091 #define PARSE_RGB_LIST_FIELD(field) \
1092 if (CONSP (rgb_list) && INTEGERP (XCAR (rgb_list))) \
1094 color->field = XINT (XCAR (rgb_list)); \
1095 rgb_list = XCDR (rgb_list); \
1097 else \
1098 return 0;
1100 PARSE_RGB_LIST_FIELD (red);
1101 PARSE_RGB_LIST_FIELD (green);
1102 PARSE_RGB_LIST_FIELD (blue);
1104 return 1;
1108 /* Lookup on frame F the color described by the lisp string COLOR.
1109 The resulting tty color is returned in TTY_COLOR; if STD_COLOR is
1110 non-zero, then the `standard' definition of the same color is
1111 returned in it. */
1113 static int
1114 tty_lookup_color (struct frame *f, Lisp_Object color, XColor *tty_color, XColor *std_color)
1116 Lisp_Object frame, color_desc;
1118 if (!STRINGP (color) || NILP (Ffboundp (Qtty_color_desc)))
1119 return 0;
1121 XSETFRAME (frame, f);
1123 color_desc = call2 (Qtty_color_desc, color, frame);
1124 if (CONSP (color_desc) && CONSP (XCDR (color_desc)))
1126 Lisp_Object rgb;
1128 if (! INTEGERP (XCAR (XCDR (color_desc))))
1129 return 0;
1131 tty_color->pixel = XINT (XCAR (XCDR (color_desc)));
1133 rgb = XCDR (XCDR (color_desc));
1134 if (! parse_rgb_list (rgb, tty_color))
1135 return 0;
1137 /* Should we fill in STD_COLOR too? */
1138 if (std_color)
1140 /* Default STD_COLOR to the same as TTY_COLOR. */
1141 *std_color = *tty_color;
1143 /* Do a quick check to see if the returned descriptor is
1144 actually _exactly_ equal to COLOR, otherwise we have to
1145 lookup STD_COLOR separately. If it's impossible to lookup
1146 a standard color, we just give up and use TTY_COLOR. */
1147 if ((!STRINGP (XCAR (color_desc))
1148 || NILP (Fstring_equal (color, XCAR (color_desc))))
1149 && !NILP (Ffboundp (Qtty_color_standard_values)))
1151 /* Look up STD_COLOR separately. */
1152 rgb = call1 (Qtty_color_standard_values, color);
1153 if (! parse_rgb_list (rgb, std_color))
1154 return 0;
1158 return 1;
1160 else if (NILP (Fsymbol_value (intern ("tty-defined-color-alist"))))
1161 /* We were called early during startup, and the colors are not
1162 yet set up in tty-defined-color-alist. Don't return a failure
1163 indication, since this produces the annoying "Unable to
1164 load color" messages in the *Messages* buffer. */
1165 return 1;
1166 else
1167 /* tty-color-desc seems to have returned a bad value. */
1168 return 0;
1171 /* A version of defined_color for non-X frames. */
1174 tty_defined_color (struct frame *f, char *color_name, 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, 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, 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, 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 extern char *xic_create_fontsetname
3670 (char *base_fontname, Bool motif);
3671 char *fontsetname = xic_create_fontsetname (SDATA (xlfd), motif);
3672 #else
3673 char *fontsetname = (char *) SDATA (xlfd);
3674 #endif
3675 sprintf (line, "%s.pane.menubar*font%s: %s",
3676 myname, suffix, fontsetname);
3677 XrmPutLineResource (&rdb, line);
3678 sprintf (line, "%s.%s*font%s: %s",
3679 myname, popup_path, suffix, fontsetname);
3680 XrmPutLineResource (&rdb, line);
3681 changed_p = 1;
3682 if (fontsetname != (char *) SDATA (xlfd))
3683 xfree (fontsetname);
3687 if (changed_p && f->output_data.x->menubar_widget)
3688 free_frame_menubar (f);
3692 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
3695 DEFUN ("face-attribute-relative-p", Fface_attribute_relative_p,
3696 Sface_attribute_relative_p,
3697 2, 2, 0,
3698 doc: /* Check whether a face attribute value is relative.
3699 Specifically, this function returns t if the attribute ATTRIBUTE
3700 with the value VALUE is relative.
3702 A relative value is one that doesn't entirely override whatever is
3703 inherited from another face. For most possible attributes,
3704 the only relative value that users see is `unspecified'.
3705 However, for :height, floating point values are also relative. */)
3706 (Lisp_Object attribute, Lisp_Object value)
3708 if (EQ (value, Qunspecified) || (EQ (value, Qignore_defface)))
3709 return Qt;
3710 else if (EQ (attribute, QCheight))
3711 return INTEGERP (value) ? Qnil : Qt;
3712 else
3713 return Qnil;
3716 DEFUN ("merge-face-attribute", Fmerge_face_attribute, Smerge_face_attribute,
3717 3, 3, 0,
3718 doc: /* Return face ATTRIBUTE VALUE1 merged with VALUE2.
3719 If VALUE1 or VALUE2 are absolute (see `face-attribute-relative-p'), then
3720 the result will be absolute, otherwise it will be relative. */)
3721 (Lisp_Object attribute, Lisp_Object value1, Lisp_Object value2)
3723 if (EQ (value1, Qunspecified) || EQ (value1, Qignore_defface))
3724 return value2;
3725 else if (EQ (attribute, QCheight))
3726 return merge_face_heights (value1, value2, value1);
3727 else
3728 return value1;
3732 DEFUN ("internal-get-lisp-face-attribute", Finternal_get_lisp_face_attribute,
3733 Sinternal_get_lisp_face_attribute,
3734 2, 3, 0,
3735 doc: /* Return face attribute KEYWORD of face SYMBOL.
3736 If SYMBOL does not name a valid Lisp face or KEYWORD isn't a valid
3737 face attribute name, signal an error.
3738 If the optional argument FRAME is given, report on face SYMBOL in that
3739 frame. If FRAME is t, report on the defaults for face SYMBOL (for new
3740 frames). If FRAME is omitted or nil, use the selected frame. */)
3741 (Lisp_Object symbol, Lisp_Object keyword, Lisp_Object frame)
3743 Lisp_Object lface, value = Qnil;
3745 CHECK_SYMBOL (symbol);
3746 CHECK_SYMBOL (keyword);
3748 if (EQ (frame, Qt))
3749 lface = lface_from_face_name (NULL, symbol, 1);
3750 else
3752 if (NILP (frame))
3753 frame = selected_frame;
3754 CHECK_LIVE_FRAME (frame);
3755 lface = lface_from_face_name (XFRAME (frame), symbol, 1);
3758 if (EQ (keyword, QCfamily))
3759 value = LFACE_FAMILY (lface);
3760 else if (EQ (keyword, QCfoundry))
3761 value = LFACE_FOUNDRY (lface);
3762 else if (EQ (keyword, QCheight))
3763 value = LFACE_HEIGHT (lface);
3764 else if (EQ (keyword, QCweight))
3765 value = LFACE_WEIGHT (lface);
3766 else if (EQ (keyword, QCslant))
3767 value = LFACE_SLANT (lface);
3768 else if (EQ (keyword, QCunderline))
3769 value = LFACE_UNDERLINE (lface);
3770 else if (EQ (keyword, QCoverline))
3771 value = LFACE_OVERLINE (lface);
3772 else if (EQ (keyword, QCstrike_through))
3773 value = LFACE_STRIKE_THROUGH (lface);
3774 else if (EQ (keyword, QCbox))
3775 value = LFACE_BOX (lface);
3776 else if (EQ (keyword, QCinverse_video)
3777 || EQ (keyword, QCreverse_video))
3778 value = LFACE_INVERSE (lface);
3779 else if (EQ (keyword, QCforeground))
3780 value = LFACE_FOREGROUND (lface);
3781 else if (EQ (keyword, QCbackground))
3782 value = LFACE_BACKGROUND (lface);
3783 else if (EQ (keyword, QCstipple))
3784 value = LFACE_STIPPLE (lface);
3785 else if (EQ (keyword, QCwidth))
3786 value = LFACE_SWIDTH (lface);
3787 else if (EQ (keyword, QCinherit))
3788 value = LFACE_INHERIT (lface);
3789 else if (EQ (keyword, QCfont))
3790 value = LFACE_FONT (lface);
3791 else if (EQ (keyword, QCfontset))
3792 value = LFACE_FONTSET (lface);
3793 else
3794 signal_error ("Invalid face attribute name", keyword);
3796 if (IGNORE_DEFFACE_P (value))
3797 return Qunspecified;
3799 return value;
3803 DEFUN ("internal-lisp-face-attribute-values",
3804 Finternal_lisp_face_attribute_values,
3805 Sinternal_lisp_face_attribute_values, 1, 1, 0,
3806 doc: /* Return a list of valid discrete values for face attribute ATTR.
3807 Value is nil if ATTR doesn't have a discrete set of valid values. */)
3808 (Lisp_Object attr)
3810 Lisp_Object result = Qnil;
3812 CHECK_SYMBOL (attr);
3814 if (EQ (attr, QCunderline))
3815 result = Fcons (Qt, Fcons (Qnil, Qnil));
3816 else if (EQ (attr, QCoverline))
3817 result = Fcons (Qt, Fcons (Qnil, Qnil));
3818 else if (EQ (attr, QCstrike_through))
3819 result = Fcons (Qt, Fcons (Qnil, Qnil));
3820 else if (EQ (attr, QCinverse_video) || EQ (attr, QCreverse_video))
3821 result = Fcons (Qt, Fcons (Qnil, Qnil));
3823 return result;
3827 DEFUN ("internal-merge-in-global-face", Finternal_merge_in_global_face,
3828 Sinternal_merge_in_global_face, 2, 2, 0,
3829 doc: /* Add attributes from frame-default definition of FACE to FACE on FRAME.
3830 Default face attributes override any local face attributes. */)
3831 (Lisp_Object face, Lisp_Object frame)
3833 int i;
3834 Lisp_Object global_lface, local_lface, *gvec, *lvec;
3835 struct frame *f = XFRAME (frame);
3837 CHECK_LIVE_FRAME (frame);
3838 global_lface = lface_from_face_name (NULL, face, 1);
3839 local_lface = lface_from_face_name (f, face, 0);
3840 if (NILP (local_lface))
3841 local_lface = Finternal_make_lisp_face (face, frame);
3843 /* Make every specified global attribute override the local one.
3844 BEWARE!! This is only used from `face-set-after-frame-default' where
3845 the local frame is defined from default specs in `face-defface-spec'
3846 and those should be overridden by global settings. Hence the strange
3847 "global before local" priority. */
3848 lvec = XVECTOR (local_lface)->contents;
3849 gvec = XVECTOR (global_lface)->contents;
3850 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3851 if (IGNORE_DEFFACE_P (gvec[i]))
3852 lvec[i] = Qunspecified;
3853 else if (! UNSPECIFIEDP (gvec[i]))
3854 lvec[i] = gvec[i];
3856 /* If the default face was changed, update the face cache and the
3857 `font' frame parameter. */
3858 if (EQ (face, Qdefault))
3860 struct face_cache *c = FRAME_FACE_CACHE (f);
3861 struct face *newface, *oldface = FACE_FROM_ID (f, DEFAULT_FACE_ID);
3862 Lisp_Object attrs[LFACE_VECTOR_SIZE];
3864 /* This can be NULL (e.g., in batch mode). */
3865 if (oldface)
3867 /* Ensure that the face vector is fully specified by merging
3868 the previously-cached vector. */
3869 memcpy (attrs, oldface->lface, sizeof attrs);
3870 merge_face_vectors (f, lvec, attrs, 0);
3871 memcpy (lvec, attrs, sizeof attrs);
3872 newface = realize_face (c, lvec, DEFAULT_FACE_ID);
3874 if ((! UNSPECIFIEDP (gvec[LFACE_FAMILY_INDEX])
3875 || ! UNSPECIFIEDP (gvec[LFACE_FOUNDRY_INDEX])
3876 || ! UNSPECIFIEDP (gvec[LFACE_HEIGHT_INDEX])
3877 || ! UNSPECIFIEDP (gvec[LFACE_WEIGHT_INDEX])
3878 || ! UNSPECIFIEDP (gvec[LFACE_SLANT_INDEX])
3879 || ! UNSPECIFIEDP (gvec[LFACE_SWIDTH_INDEX])
3880 || ! UNSPECIFIEDP (gvec[LFACE_FONT_INDEX]))
3881 && newface->font)
3883 Lisp_Object name = newface->font->props[FONT_NAME_INDEX];
3884 Fmodify_frame_parameters (frame, Fcons (Fcons (Qfont, name),
3885 Qnil));
3890 return Qnil;
3894 /* The following function is implemented for compatibility with 20.2.
3895 The function is used in x-resolve-fonts when it is asked to
3896 return fonts with the same size as the font of a face. This is
3897 done in fontset.el. */
3899 DEFUN ("face-font", Fface_font, Sface_font, 1, 3, 0,
3900 doc: /* Return the font name of face FACE, or nil if it is unspecified.
3901 The font name is, by default, for ASCII characters.
3902 If the optional argument FRAME is given, report on face FACE in that frame.
3903 If FRAME is t, report on the defaults for face FACE (for new frames).
3904 The font default for a face is either nil, or a list
3905 of the form (bold), (italic) or (bold italic).
3906 If FRAME is omitted or nil, use the selected frame. And, in this case,
3907 if the optional third argument CHARACTER is given,
3908 return the font name used for CHARACTER. */)
3909 (Lisp_Object face, Lisp_Object frame, Lisp_Object character)
3911 if (EQ (frame, Qt))
3913 Lisp_Object result = Qnil;
3914 Lisp_Object lface = lface_from_face_name (NULL, face, 1);
3916 if (!UNSPECIFIEDP (LFACE_WEIGHT (lface))
3917 && !EQ (LFACE_WEIGHT (lface), Qnormal))
3918 result = Fcons (Qbold, result);
3920 if (!UNSPECIFIEDP (LFACE_SLANT (lface))
3921 && !EQ (LFACE_SLANT (lface), Qnormal))
3922 result = Fcons (Qitalic, result);
3924 return result;
3926 else
3928 struct frame *f = frame_or_selected_frame (frame, 1);
3929 int face_id = lookup_named_face (f, face, 1);
3930 struct face *face = FACE_FROM_ID (f, face_id);
3932 if (! face)
3933 return Qnil;
3934 #ifdef HAVE_WINDOW_SYSTEM
3935 if (FRAME_WINDOW_P (f) && !NILP (character))
3937 CHECK_CHARACTER (character);
3938 face_id = FACE_FOR_CHAR (f, face, XINT (character), -1, Qnil);
3939 face = FACE_FROM_ID (f, face_id);
3941 return (face->font
3942 ? face->font->props[FONT_NAME_INDEX]
3943 : Qnil);
3944 #else /* !HAVE_WINDOW_SYSTEM */
3945 return build_string (FRAME_MSDOS_P (f)
3946 ? "ms-dos"
3947 : FRAME_W32_P (f) ? "w32term"
3948 :"tty");
3949 #endif
3954 /* Compare face-attribute values v1 and v2 for equality. Value is non-zero if
3955 all attributes are `equal'. Tries to be fast because this function
3956 is called quite often. */
3958 static INLINE int
3959 face_attr_equal_p (Lisp_Object v1, Lisp_Object v2)
3961 /* Type can differ, e.g. when one attribute is unspecified, i.e. nil,
3962 and the other is specified. */
3963 if (XTYPE (v1) != XTYPE (v2))
3964 return 0;
3966 if (EQ (v1, v2))
3967 return 1;
3969 switch (XTYPE (v1))
3971 case Lisp_String:
3972 if (SBYTES (v1) != SBYTES (v2))
3973 return 0;
3975 return memcmp (SDATA (v1), SDATA (v2), SBYTES (v1)) == 0;
3977 case_Lisp_Int:
3978 case Lisp_Symbol:
3979 return 0;
3981 default:
3982 return !NILP (Fequal (v1, v2));
3987 /* Compare face vectors V1 and V2 for equality. Value is non-zero if
3988 all attributes are `equal'. Tries to be fast because this function
3989 is called quite often. */
3991 static INLINE int
3992 lface_equal_p (Lisp_Object *v1, Lisp_Object *v2)
3994 int i, equal_p = 1;
3996 for (i = 1; i < LFACE_VECTOR_SIZE && equal_p; ++i)
3997 equal_p = face_attr_equal_p (v1[i], v2[i]);
3999 return equal_p;
4003 DEFUN ("internal-lisp-face-equal-p", Finternal_lisp_face_equal_p,
4004 Sinternal_lisp_face_equal_p, 2, 3, 0,
4005 doc: /* True if FACE1 and FACE2 are equal.
4006 If the optional argument FRAME is given, report on FACE1 and FACE2 in that frame.
4007 If FRAME is t, report on the defaults for FACE1 and FACE2 (for new frames).
4008 If FRAME is omitted or nil, use the selected frame. */)
4009 (Lisp_Object face1, Lisp_Object face2, Lisp_Object frame)
4011 int equal_p;
4012 struct frame *f;
4013 Lisp_Object lface1, lface2;
4015 if (EQ (frame, Qt))
4016 f = NULL;
4017 else
4018 /* Don't use check_x_frame here because this function is called
4019 before X frames exist. At that time, if FRAME is nil,
4020 selected_frame will be used which is the frame dumped with
4021 Emacs. That frame is not an X frame. */
4022 f = frame_or_selected_frame (frame, 2);
4024 lface1 = lface_from_face_name (f, face1, 1);
4025 lface2 = lface_from_face_name (f, face2, 1);
4026 equal_p = lface_equal_p (XVECTOR (lface1)->contents,
4027 XVECTOR (lface2)->contents);
4028 return equal_p ? Qt : Qnil;
4032 DEFUN ("internal-lisp-face-empty-p", Finternal_lisp_face_empty_p,
4033 Sinternal_lisp_face_empty_p, 1, 2, 0,
4034 doc: /* True if FACE has no attribute specified.
4035 If the optional argument FRAME is given, report on face FACE in that frame.
4036 If FRAME is t, report on the defaults for face FACE (for new frames).
4037 If FRAME is omitted or nil, use the selected frame. */)
4038 (Lisp_Object face, Lisp_Object frame)
4040 struct frame *f;
4041 Lisp_Object lface;
4042 int i;
4044 if (NILP (frame))
4045 frame = selected_frame;
4046 CHECK_LIVE_FRAME (frame);
4047 f = XFRAME (frame);
4049 if (EQ (frame, Qt))
4050 lface = lface_from_face_name (NULL, face, 1);
4051 else
4052 lface = lface_from_face_name (f, face, 1);
4054 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
4055 if (!UNSPECIFIEDP (AREF (lface, i)))
4056 break;
4058 return i == LFACE_VECTOR_SIZE ? Qt : Qnil;
4062 DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist,
4063 0, 1, 0,
4064 doc: /* Return an alist of frame-local faces defined on FRAME.
4065 For internal use only. */)
4066 (Lisp_Object frame)
4068 struct frame *f = frame_or_selected_frame (frame, 0);
4069 return f->face_alist;
4073 /* Return a hash code for Lisp string STRING with case ignored. Used
4074 below in computing a hash value for a Lisp face. */
4076 static INLINE unsigned
4077 hash_string_case_insensitive (Lisp_Object string)
4079 const unsigned char *s;
4080 unsigned hash = 0;
4081 xassert (STRINGP (string));
4082 for (s = SDATA (string); *s; ++s)
4083 hash = (hash << 1) ^ tolower (*s);
4084 return hash;
4088 /* Return a hash code for face attribute vector V. */
4090 static INLINE unsigned
4091 lface_hash (Lisp_Object *v)
4093 return (hash_string_case_insensitive (v[LFACE_FAMILY_INDEX])
4094 ^ hash_string_case_insensitive (v[LFACE_FOUNDRY_INDEX])
4095 ^ hash_string_case_insensitive (v[LFACE_FOREGROUND_INDEX])
4096 ^ hash_string_case_insensitive (v[LFACE_BACKGROUND_INDEX])
4097 ^ XHASH (v[LFACE_WEIGHT_INDEX])
4098 ^ XHASH (v[LFACE_SLANT_INDEX])
4099 ^ XHASH (v[LFACE_SWIDTH_INDEX])
4100 ^ XHASH (v[LFACE_HEIGHT_INDEX]));
4104 /* Return non-zero if LFACE1 and LFACE2 specify the same font (without
4105 considering charsets/registries). They do if they specify the same
4106 family, point size, weight, width, slant, and font. Both
4107 LFACE1 and LFACE2 must be fully-specified. */
4109 static INLINE int
4110 lface_same_font_attributes_p (Lisp_Object *lface1, Lisp_Object *lface2)
4112 xassert (lface_fully_specified_p (lface1)
4113 && lface_fully_specified_p (lface2));
4114 return (xstrcasecmp (SDATA (lface1[LFACE_FAMILY_INDEX]),
4115 SDATA (lface2[LFACE_FAMILY_INDEX])) == 0
4116 && xstrcasecmp (SDATA (lface1[LFACE_FOUNDRY_INDEX]),
4117 SDATA (lface2[LFACE_FOUNDRY_INDEX])) == 0
4118 && EQ (lface1[LFACE_HEIGHT_INDEX], lface2[LFACE_HEIGHT_INDEX])
4119 && EQ (lface1[LFACE_SWIDTH_INDEX], lface2[LFACE_SWIDTH_INDEX])
4120 && EQ (lface1[LFACE_WEIGHT_INDEX], lface2[LFACE_WEIGHT_INDEX])
4121 && EQ (lface1[LFACE_SLANT_INDEX], lface2[LFACE_SLANT_INDEX])
4122 && EQ (lface1[LFACE_FONT_INDEX], lface2[LFACE_FONT_INDEX])
4123 && (EQ (lface1[LFACE_FONTSET_INDEX], lface2[LFACE_FONTSET_INDEX])
4124 || (STRINGP (lface1[LFACE_FONTSET_INDEX])
4125 && STRINGP (lface2[LFACE_FONTSET_INDEX])
4126 && ! xstrcasecmp (SDATA (lface1[LFACE_FONTSET_INDEX]),
4127 SDATA (lface2[LFACE_FONTSET_INDEX]))))
4133 /***********************************************************************
4134 Realized Faces
4135 ***********************************************************************/
4137 /* Allocate and return a new realized face for Lisp face attribute
4138 vector ATTR. */
4140 static struct face *
4141 make_realized_face (Lisp_Object *attr)
4143 struct face *face = (struct face *) xmalloc (sizeof *face);
4144 memset (face, 0, sizeof *face);
4145 face->ascii_face = face;
4146 memcpy (face->lface, attr, sizeof face->lface);
4147 return face;
4151 /* Free realized face FACE, including its X resources. FACE may
4152 be null. */
4154 void
4155 free_realized_face (struct frame *f, struct face *face)
4157 if (face)
4159 #ifdef HAVE_WINDOW_SYSTEM
4160 if (FRAME_WINDOW_P (f))
4162 /* Free fontset of FACE if it is ASCII face. */
4163 if (face->fontset >= 0 && face == face->ascii_face)
4164 free_face_fontset (f, face);
4165 if (face->gc)
4167 BLOCK_INPUT;
4168 if (face->font)
4169 font_done_for_face (f, face);
4170 x_free_gc (f, face->gc);
4171 face->gc = 0;
4172 UNBLOCK_INPUT;
4175 free_face_colors (f, face);
4176 x_destroy_bitmap (f, face->stipple);
4178 #endif /* HAVE_WINDOW_SYSTEM */
4180 xfree (face);
4185 /* Prepare face FACE for subsequent display on frame F. This
4186 allocated GCs if they haven't been allocated yet or have been freed
4187 by clearing the face cache. */
4189 void
4190 prepare_face_for_display (struct frame *f, struct face *face)
4192 #ifdef HAVE_WINDOW_SYSTEM
4193 xassert (FRAME_WINDOW_P (f));
4195 if (face->gc == 0)
4197 XGCValues xgcv;
4198 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
4200 xgcv.foreground = face->foreground;
4201 xgcv.background = face->background;
4202 #ifdef HAVE_X_WINDOWS
4203 xgcv.graphics_exposures = False;
4204 #endif
4206 BLOCK_INPUT;
4207 #ifdef HAVE_X_WINDOWS
4208 if (face->stipple)
4210 xgcv.fill_style = FillOpaqueStippled;
4211 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
4212 mask |= GCFillStyle | GCStipple;
4214 #endif
4215 face->gc = x_create_gc (f, mask, &xgcv);
4216 if (face->font)
4217 font_prepare_for_face (f, face);
4218 UNBLOCK_INPUT;
4220 #endif /* HAVE_WINDOW_SYSTEM */
4224 /* Returns the `distance' between the colors X and Y. */
4226 static int
4227 color_distance (XColor *x, XColor *y)
4229 /* This formula is from a paper title `Colour metric' by Thiadmer Riemersma.
4230 Quoting from that paper:
4232 This formula has results that are very close to L*u*v* (with the
4233 modified lightness curve) and, more importantly, it is a more even
4234 algorithm: it does not have a range of colours where it suddenly
4235 gives far from optimal results.
4237 See <http://www.compuphase.com/cmetric.htm> for more info. */
4239 long r = (x->red - y->red) >> 8;
4240 long g = (x->green - y->green) >> 8;
4241 long b = (x->blue - y->blue) >> 8;
4242 long r_mean = (x->red + y->red) >> 9;
4244 return
4245 (((512 + r_mean) * r * r) >> 8)
4246 + 4 * g * g
4247 + (((767 - r_mean) * b * b) >> 8);
4251 DEFUN ("color-distance", Fcolor_distance, Scolor_distance, 2, 3, 0,
4252 doc: /* Return an integer distance between COLOR1 and COLOR2 on FRAME.
4253 COLOR1 and COLOR2 may be either strings containing the color name,
4254 or lists of the form (RED GREEN BLUE).
4255 If FRAME is unspecified or nil, the current frame is used. */)
4256 (Lisp_Object color1, Lisp_Object color2, Lisp_Object frame)
4258 struct frame *f;
4259 XColor cdef1, cdef2;
4261 if (NILP (frame))
4262 frame = selected_frame;
4263 CHECK_LIVE_FRAME (frame);
4264 f = XFRAME (frame);
4266 if (!(CONSP (color1) && parse_rgb_list (color1, &cdef1))
4267 && !(STRINGP (color1) && defined_color (f, SDATA (color1), &cdef1, 0)))
4268 signal_error ("Invalid color", color1);
4269 if (!(CONSP (color2) && parse_rgb_list (color2, &cdef2))
4270 && !(STRINGP (color2) && defined_color (f, SDATA (color2), &cdef2, 0)))
4271 signal_error ("Invalid color", color2);
4273 return make_number (color_distance (&cdef1, &cdef2));
4277 /***********************************************************************
4278 Face Cache
4279 ***********************************************************************/
4281 /* Return a new face cache for frame F. */
4283 static struct face_cache *
4284 make_face_cache (struct frame *f)
4286 struct face_cache *c;
4287 int size;
4289 c = (struct face_cache *) xmalloc (sizeof *c);
4290 memset (c, 0, sizeof *c);
4291 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4292 c->buckets = (struct face **) xmalloc (size);
4293 memset (c->buckets, 0, size);
4294 c->size = 50;
4295 c->faces_by_id = (struct face **) xmalloc (c->size * sizeof *c->faces_by_id);
4296 c->f = f;
4297 c->menu_face_changed_p = menu_face_changed_default;
4298 return c;
4302 /* Clear out all graphics contexts for all realized faces, except for
4303 the basic faces. This should be done from time to time just to avoid
4304 keeping too many graphics contexts that are no longer needed. */
4306 static void
4307 clear_face_gcs (struct face_cache *c)
4309 if (c && FRAME_WINDOW_P (c->f))
4311 #ifdef HAVE_WINDOW_SYSTEM
4312 int i;
4313 for (i = BASIC_FACE_ID_SENTINEL; i < c->used; ++i)
4315 struct face *face = c->faces_by_id[i];
4316 if (face && face->gc)
4318 BLOCK_INPUT;
4319 if (face->font)
4320 font_done_for_face (c->f, face);
4321 x_free_gc (c->f, face->gc);
4322 face->gc = 0;
4323 UNBLOCK_INPUT;
4326 #endif /* HAVE_WINDOW_SYSTEM */
4331 /* Free all realized faces in face cache C, including basic faces.
4332 C may be null. If faces are freed, make sure the frame's current
4333 matrix is marked invalid, so that a display caused by an expose
4334 event doesn't try to use faces we destroyed. */
4336 static void
4337 free_realized_faces (struct face_cache *c)
4339 if (c && c->used)
4341 int i, size;
4342 struct frame *f = c->f;
4344 /* We must block input here because we can't process X events
4345 safely while only some faces are freed, or when the frame's
4346 current matrix still references freed faces. */
4347 BLOCK_INPUT;
4349 for (i = 0; i < c->used; ++i)
4351 free_realized_face (f, c->faces_by_id[i]);
4352 c->faces_by_id[i] = NULL;
4355 c->used = 0;
4356 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4357 memset (c->buckets, 0, size);
4359 /* Must do a thorough redisplay the next time. Mark current
4360 matrices as invalid because they will reference faces freed
4361 above. This function is also called when a frame is
4362 destroyed. In this case, the root window of F is nil. */
4363 if (WINDOWP (f->root_window))
4365 clear_current_matrices (f);
4366 ++windows_or_buffers_changed;
4369 UNBLOCK_INPUT;
4374 /* Free all realized faces that are using FONTSET on frame F. */
4376 void
4377 free_realized_faces_for_fontset (struct frame *f, int fontset)
4379 struct face_cache *cache = FRAME_FACE_CACHE (f);
4380 struct face *face;
4381 int i;
4383 /* We must block input here because we can't process X events safely
4384 while only some faces are freed, or when the frame's current
4385 matrix still references freed faces. */
4386 BLOCK_INPUT;
4388 for (i = 0; i < cache->used; i++)
4390 face = cache->faces_by_id[i];
4391 if (face
4392 && face->fontset == fontset)
4394 uncache_face (cache, face);
4395 free_realized_face (f, face);
4399 /* Must do a thorough redisplay the next time. Mark current
4400 matrices as invalid because they will reference faces freed
4401 above. This function is also called when a frame is destroyed.
4402 In this case, the root window of F is nil. */
4403 if (WINDOWP (f->root_window))
4405 clear_current_matrices (f);
4406 ++windows_or_buffers_changed;
4409 UNBLOCK_INPUT;
4413 /* Free all realized faces on FRAME or on all frames if FRAME is nil.
4414 This is done after attributes of a named face have been changed,
4415 because we can't tell which realized faces depend on that face. */
4417 void
4418 free_all_realized_faces (Lisp_Object frame)
4420 if (NILP (frame))
4422 Lisp_Object rest;
4423 FOR_EACH_FRAME (rest, frame)
4424 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4426 else
4427 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4431 /* Free face cache C and faces in it, including their X resources. */
4433 static void
4434 free_face_cache (struct face_cache *c)
4436 if (c)
4438 free_realized_faces (c);
4439 xfree (c->buckets);
4440 xfree (c->faces_by_id);
4441 xfree (c);
4446 /* Cache realized face FACE in face cache C. HASH is the hash value
4447 of FACE. If FACE is for ASCII characters (i.e. FACE->ascii_face ==
4448 FACE), insert the new face to the beginning of the collision list
4449 of the face hash table of C. Otherwise, add the new face to the
4450 end of the collision list. This way, lookup_face can quickly find
4451 that a requested face is not cached. */
4453 static void
4454 cache_face (struct face_cache *c, struct face *face, unsigned int hash)
4456 int i = hash % FACE_CACHE_BUCKETS_SIZE;
4458 face->hash = hash;
4460 if (face->ascii_face != face)
4462 struct face *last = c->buckets[i];
4463 if (last)
4465 while (last->next)
4466 last = last->next;
4467 last->next = face;
4468 face->prev = last;
4469 face->next = NULL;
4471 else
4473 c->buckets[i] = face;
4474 face->prev = face->next = NULL;
4477 else
4479 face->prev = NULL;
4480 face->next = c->buckets[i];
4481 if (face->next)
4482 face->next->prev = face;
4483 c->buckets[i] = face;
4486 /* Find a free slot in C->faces_by_id and use the index of the free
4487 slot as FACE->id. */
4488 for (i = 0; i < c->used; ++i)
4489 if (c->faces_by_id[i] == NULL)
4490 break;
4491 face->id = i;
4493 /* Maybe enlarge C->faces_by_id. */
4494 if (i == c->used)
4496 if (c->used == c->size)
4498 int new_size, sz;
4499 new_size = min (2 * c->size, MAX_FACE_ID);
4500 if (new_size == c->size)
4501 abort (); /* Alternatives? ++kfs */
4502 sz = new_size * sizeof *c->faces_by_id;
4503 c->faces_by_id = (struct face **) xrealloc (c->faces_by_id, sz);
4504 c->size = new_size;
4506 c->used++;
4509 #if GLYPH_DEBUG
4510 /* Check that FACE got a unique id. */
4512 int j, n;
4513 struct face *face;
4515 for (j = n = 0; j < FACE_CACHE_BUCKETS_SIZE; ++j)
4516 for (face = c->buckets[j]; face; face = face->next)
4517 if (face->id == i)
4518 ++n;
4520 xassert (n == 1);
4522 #endif /* GLYPH_DEBUG */
4524 c->faces_by_id[i] = face;
4528 /* Remove face FACE from cache C. */
4530 static void
4531 uncache_face (struct face_cache *c, struct face *face)
4533 int i = face->hash % FACE_CACHE_BUCKETS_SIZE;
4535 if (face->prev)
4536 face->prev->next = face->next;
4537 else
4538 c->buckets[i] = face->next;
4540 if (face->next)
4541 face->next->prev = face->prev;
4543 c->faces_by_id[face->id] = NULL;
4544 if (face->id == c->used)
4545 --c->used;
4549 /* Look up a realized face with face attributes ATTR in the face cache
4550 of frame F. The face will be used to display ASCII characters.
4551 Value is the ID of the face found. If no suitable face is found,
4552 realize a new one. */
4554 INLINE int
4555 lookup_face (struct frame *f, Lisp_Object *attr)
4557 struct face_cache *cache = FRAME_FACE_CACHE (f);
4558 unsigned hash;
4559 int i;
4560 struct face *face;
4562 xassert (cache != NULL);
4563 check_lface_attrs (attr);
4565 /* Look up ATTR in the face cache. */
4566 hash = lface_hash (attr);
4567 i = hash % FACE_CACHE_BUCKETS_SIZE;
4569 for (face = cache->buckets[i]; face; face = face->next)
4571 if (face->ascii_face != face)
4573 /* There's no more ASCII face. */
4574 face = NULL;
4575 break;
4577 if (face->hash == hash
4578 && lface_equal_p (face->lface, attr))
4579 break;
4582 /* If not found, realize a new face. */
4583 if (face == NULL)
4584 face = realize_face (cache, attr, -1);
4586 #if GLYPH_DEBUG
4587 xassert (face == FACE_FROM_ID (f, face->id));
4588 #endif /* GLYPH_DEBUG */
4590 return face->id;
4593 #ifdef HAVE_WINDOW_SYSTEM
4594 /* Look up a realized face that has the same attributes as BASE_FACE
4595 except for the font in the face cache of frame F. If FONT-OBJECT
4596 is not nil, it is an already opened font. If FONT-OBJECT is nil,
4597 the face has no font. Value is the ID of the face found. If no
4598 suitable face is found, realize a new one. */
4601 face_for_font (struct frame *f, Lisp_Object font_object, struct face *base_face)
4603 struct face_cache *cache = FRAME_FACE_CACHE (f);
4604 unsigned hash;
4605 int i;
4606 struct face *face;
4608 xassert (cache != NULL);
4609 base_face = base_face->ascii_face;
4610 hash = lface_hash (base_face->lface);
4611 i = hash % FACE_CACHE_BUCKETS_SIZE;
4613 for (face = cache->buckets[i]; face; face = face->next)
4615 if (face->ascii_face == face)
4616 continue;
4617 if (face->ascii_face == base_face
4618 && face->font == (NILP (font_object) ? NULL
4619 : XFONT_OBJECT (font_object))
4620 && lface_equal_p (face->lface, base_face->lface))
4621 return face->id;
4624 /* If not found, realize a new face. */
4625 face = realize_non_ascii_face (f, font_object, base_face);
4626 return face->id;
4628 #endif /* HAVE_WINDOW_SYSTEM */
4630 /* Return the face id of the realized face for named face SYMBOL on
4631 frame F suitable for displaying ASCII characters. Value is -1 if
4632 the face couldn't be determined, which might happen if the default
4633 face isn't realized and cannot be realized. */
4636 lookup_named_face (struct frame *f, Lisp_Object symbol, int signal_p)
4638 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4639 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4640 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4642 if (default_face == NULL)
4644 if (!realize_basic_faces (f))
4645 return -1;
4646 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4647 if (default_face == NULL)
4648 abort (); /* realize_basic_faces must have set it up */
4651 if (! get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4652 return -1;
4654 memcpy (attrs, default_face->lface, sizeof attrs);
4655 merge_face_vectors (f, symbol_attrs, attrs, 0);
4657 return lookup_face (f, attrs);
4661 /* Return the display face-id of the basic face who's canonical face-id
4662 is FACE_ID. The return value will usually simply be FACE_ID, unless that
4663 basic face has bee remapped via Vface_remapping_alist. This function is
4664 conservative: if something goes wrong, it will simply return FACE_ID
4665 rather than signal an error. */
4668 lookup_basic_face (struct frame *f, int face_id)
4670 Lisp_Object name, mapping;
4671 int remapped_face_id;
4673 if (NILP (Vface_remapping_alist))
4674 return face_id; /* Nothing to do. */
4676 switch (face_id)
4678 case DEFAULT_FACE_ID: name = Qdefault; break;
4679 case MODE_LINE_FACE_ID: name = Qmode_line; break;
4680 case MODE_LINE_INACTIVE_FACE_ID: name = Qmode_line_inactive; break;
4681 case HEADER_LINE_FACE_ID: name = Qheader_line; break;
4682 case TOOL_BAR_FACE_ID: name = Qtool_bar; break;
4683 case FRINGE_FACE_ID: name = Qfringe; break;
4684 case SCROLL_BAR_FACE_ID: name = Qscroll_bar; break;
4685 case BORDER_FACE_ID: name = Qborder; break;
4686 case CURSOR_FACE_ID: name = Qcursor; break;
4687 case MOUSE_FACE_ID: name = Qmouse; break;
4688 case MENU_FACE_ID: name = Qmenu; break;
4690 default:
4691 abort (); /* the caller is supposed to pass us a basic face id */
4694 /* Do a quick scan through Vface_remapping_alist, and return immediately
4695 if there is no remapping for face NAME. This is just an optimization
4696 for the very common no-remapping case. */
4697 mapping = assq_no_quit (name, Vface_remapping_alist);
4698 if (NILP (mapping))
4699 return face_id; /* Give up. */
4701 /* If there is a remapping entry, lookup the face using NAME, which will
4702 handle the remapping too. */
4703 remapped_face_id = lookup_named_face (f, name, 0);
4704 if (remapped_face_id < 0)
4705 return face_id; /* Give up. */
4707 return remapped_face_id;
4711 /* Return the ID of the realized ASCII face of Lisp face with ID
4712 LFACE_ID on frame F. Value is -1 if LFACE_ID isn't valid. */
4715 ascii_face_of_lisp_face (struct frame *f, int lface_id)
4717 int face_id;
4719 if (lface_id >= 0 && lface_id < lface_id_to_name_size)
4721 Lisp_Object face_name = lface_id_to_name[lface_id];
4722 face_id = lookup_named_face (f, face_name, 1);
4724 else
4725 face_id = -1;
4727 return face_id;
4731 /* Return a face for charset ASCII that is like the face with id
4732 FACE_ID on frame F, but has a font that is STEPS steps smaller.
4733 STEPS < 0 means larger. Value is the id of the face. */
4736 smaller_face (struct frame *f, int face_id, int steps)
4738 #ifdef HAVE_WINDOW_SYSTEM
4739 struct face *face;
4740 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4741 int pt, last_pt, last_height;
4742 int delta;
4743 int new_face_id;
4744 struct face *new_face;
4746 /* If not called for an X frame, just return the original face. */
4747 if (FRAME_TERMCAP_P (f))
4748 return face_id;
4750 /* Try in increments of 1/2 pt. */
4751 delta = steps < 0 ? 5 : -5;
4752 steps = eabs (steps);
4754 face = FACE_FROM_ID (f, face_id);
4755 memcpy (attrs, face->lface, sizeof attrs);
4756 pt = last_pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
4757 new_face_id = face_id;
4758 last_height = FONT_HEIGHT (face->font);
4760 while (steps
4761 && pt + delta > 0
4762 /* Give up if we cannot find a font within 10pt. */
4763 && eabs (last_pt - pt) < 100)
4765 /* Look up a face for a slightly smaller/larger font. */
4766 pt += delta;
4767 attrs[LFACE_HEIGHT_INDEX] = make_number (pt);
4768 new_face_id = lookup_face (f, attrs);
4769 new_face = FACE_FROM_ID (f, new_face_id);
4771 /* If height changes, count that as one step. */
4772 if ((delta < 0 && FONT_HEIGHT (new_face->font) < last_height)
4773 || (delta > 0 && FONT_HEIGHT (new_face->font) > last_height))
4775 --steps;
4776 last_height = FONT_HEIGHT (new_face->font);
4777 last_pt = pt;
4781 return new_face_id;
4783 #else /* not HAVE_WINDOW_SYSTEM */
4785 return face_id;
4787 #endif /* not HAVE_WINDOW_SYSTEM */
4791 /* Return a face for charset ASCII that is like the face with id
4792 FACE_ID on frame F, but has height HEIGHT. */
4795 face_with_height (struct frame *f, int face_id, int height)
4797 #ifdef HAVE_WINDOW_SYSTEM
4798 struct face *face;
4799 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4801 if (FRAME_TERMCAP_P (f)
4802 || height <= 0)
4803 return face_id;
4805 face = FACE_FROM_ID (f, face_id);
4806 memcpy (attrs, face->lface, sizeof attrs);
4807 attrs[LFACE_HEIGHT_INDEX] = make_number (height);
4808 font_clear_prop (attrs, FONT_SIZE_INDEX);
4809 face_id = lookup_face (f, attrs);
4810 #endif /* HAVE_WINDOW_SYSTEM */
4812 return face_id;
4816 /* Return the face id of the realized face for named face SYMBOL on
4817 frame F suitable for displaying ASCII characters, and use
4818 attributes of the face FACE_ID for attributes that aren't
4819 completely specified by SYMBOL. This is like lookup_named_face,
4820 except that the default attributes come from FACE_ID, not from the
4821 default face. FACE_ID is assumed to be already realized. */
4824 lookup_derived_face (struct frame *f, Lisp_Object symbol, int face_id, int signal_p)
4826 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4827 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4828 struct face *default_face = FACE_FROM_ID (f, face_id);
4830 if (!default_face)
4831 abort ();
4833 if (!get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4834 return -1;
4836 memcpy (attrs, default_face->lface, sizeof attrs);
4837 merge_face_vectors (f, symbol_attrs, attrs, 0);
4838 return lookup_face (f, attrs);
4841 DEFUN ("face-attributes-as-vector", Fface_attributes_as_vector,
4842 Sface_attributes_as_vector, 1, 1, 0,
4843 doc: /* Return a vector of face attributes corresponding to PLIST. */)
4844 (Lisp_Object plist)
4846 Lisp_Object lface;
4847 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
4848 Qunspecified);
4849 merge_face_ref (XFRAME (selected_frame), plist, XVECTOR (lface)->contents,
4850 1, 0);
4851 return lface;
4856 /***********************************************************************
4857 Face capability testing
4858 ***********************************************************************/
4861 /* If the distance (as returned by color_distance) between two colors is
4862 less than this, then they are considered the same, for determining
4863 whether a color is supported or not. The range of values is 0-65535. */
4865 #define TTY_SAME_COLOR_THRESHOLD 10000
4867 #ifdef HAVE_WINDOW_SYSTEM
4869 /* Return non-zero if all the face attributes in ATTRS are supported
4870 on the window-system frame F.
4872 The definition of `supported' is somewhat heuristic, but basically means
4873 that a face containing all the attributes in ATTRS, when merged with the
4874 default face for display, can be represented in a way that's
4876 \(1) different in appearance than the default face, and
4877 \(2) `close in spirit' to what the attributes specify, if not exact. */
4879 static int
4880 x_supports_face_attributes_p (struct frame *f, Lisp_Object *attrs, struct face *def_face)
4882 Lisp_Object *def_attrs = def_face->lface;
4884 /* Check that other specified attributes are different that the default
4885 face. */
4886 if ((!UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
4887 && face_attr_equal_p (attrs[LFACE_UNDERLINE_INDEX],
4888 def_attrs[LFACE_UNDERLINE_INDEX]))
4889 || (!UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
4890 && face_attr_equal_p (attrs[LFACE_INVERSE_INDEX],
4891 def_attrs[LFACE_INVERSE_INDEX]))
4892 || (!UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
4893 && face_attr_equal_p (attrs[LFACE_FOREGROUND_INDEX],
4894 def_attrs[LFACE_FOREGROUND_INDEX]))
4895 || (!UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
4896 && face_attr_equal_p (attrs[LFACE_BACKGROUND_INDEX],
4897 def_attrs[LFACE_BACKGROUND_INDEX]))
4898 || (!UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4899 && face_attr_equal_p (attrs[LFACE_STIPPLE_INDEX],
4900 def_attrs[LFACE_STIPPLE_INDEX]))
4901 || (!UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4902 && face_attr_equal_p (attrs[LFACE_OVERLINE_INDEX],
4903 def_attrs[LFACE_OVERLINE_INDEX]))
4904 || (!UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4905 && face_attr_equal_p (attrs[LFACE_STRIKE_THROUGH_INDEX],
4906 def_attrs[LFACE_STRIKE_THROUGH_INDEX]))
4907 || (!UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
4908 && face_attr_equal_p (attrs[LFACE_BOX_INDEX],
4909 def_attrs[LFACE_BOX_INDEX])))
4910 return 0;
4912 /* Check font-related attributes, as those are the most commonly
4913 "unsupported" on a window-system (because of missing fonts). */
4914 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4915 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4916 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4917 || !UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
4918 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
4919 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX]))
4921 int face_id;
4922 struct face *face;
4923 Lisp_Object merged_attrs[LFACE_VECTOR_SIZE];
4924 int i;
4926 memcpy (merged_attrs, def_attrs, sizeof merged_attrs);
4928 merge_face_vectors (f, attrs, merged_attrs, 0);
4930 face_id = lookup_face (f, merged_attrs);
4931 face = FACE_FROM_ID (f, face_id);
4933 if (! face)
4934 error ("Cannot make face");
4936 /* If the font is the same, or no font is found, then not
4937 supported. */
4938 if (face->font == def_face->font
4939 || ! face->font)
4940 return 0;
4941 for (i = FONT_TYPE_INDEX; i <= FONT_SIZE_INDEX; i++)
4942 if (! EQ (face->font->props[i], def_face->font->props[i]))
4944 Lisp_Object s1, s2;
4946 if (i < FONT_FOUNDRY_INDEX || i > FONT_REGISTRY_INDEX
4947 || face->font->driver->case_sensitive)
4948 return 1;
4949 s1 = SYMBOL_NAME (face->font->props[i]);
4950 s2 = SYMBOL_NAME (def_face->font->props[i]);
4951 if (! EQ (Fcompare_strings (s1, make_number (0), Qnil,
4952 s2, make_number (0), Qnil, Qt), Qt))
4953 return 1;
4955 return 0;
4958 /* Everything checks out, this face is supported. */
4959 return 1;
4962 #endif /* HAVE_WINDOW_SYSTEM */
4964 /* Return non-zero if all the face attributes in ATTRS are supported
4965 on the tty frame F.
4967 The definition of `supported' is somewhat heuristic, but basically means
4968 that a face containing all the attributes in ATTRS, when merged
4969 with the default face for display, can be represented in a way that's
4971 \(1) different in appearance than the default face, and
4972 \(2) `close in spirit' to what the attributes specify, if not exact.
4974 Point (2) implies that a `:weight black' attribute will be satisfied
4975 by any terminal that can display bold, and a `:foreground "yellow"' as
4976 long as the terminal can display a yellowish color, but `:slant italic'
4977 will _not_ be satisfied by the tty display code's automatic
4978 substitution of a `dim' face for italic. */
4980 static int
4981 tty_supports_face_attributes_p (struct frame *f, Lisp_Object *attrs, struct face *def_face)
4983 int weight;
4984 Lisp_Object val, fg, bg;
4985 XColor fg_tty_color, fg_std_color;
4986 XColor bg_tty_color, bg_std_color;
4987 unsigned test_caps = 0;
4988 Lisp_Object *def_attrs = def_face->lface;
4991 /* First check some easy-to-check stuff; ttys support none of the
4992 following attributes, so we can just return false if any are requested
4993 (even if `nominal' values are specified, we should still return false,
4994 as that will be the same value that the default face uses). We
4995 consider :slant unsupportable on ttys, even though the face code
4996 actually `fakes' them using a dim attribute if possible. This is
4997 because the faked result is too different from what the face
4998 specifies. */
4999 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
5000 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
5001 || !UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
5002 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
5003 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
5004 || !UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
5005 || !UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
5006 || !UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
5007 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX]))
5008 return 0;
5011 /* Test for terminal `capabilities' (non-color character attributes). */
5013 /* font weight (bold/dim) */
5014 val = attrs[LFACE_WEIGHT_INDEX];
5015 if (!UNSPECIFIEDP (val)
5016 && (weight = FONT_WEIGHT_NAME_NUMERIC (val), weight >= 0))
5018 int def_weight = FONT_WEIGHT_NAME_NUMERIC (def_attrs[LFACE_WEIGHT_INDEX]);
5020 if (weight > 100)
5022 if (def_weight > 100)
5023 return 0; /* same as default */
5024 test_caps = TTY_CAP_BOLD;
5026 else if (weight < 100)
5028 if (def_weight < 100)
5029 return 0; /* same as default */
5030 test_caps = TTY_CAP_DIM;
5032 else if (def_weight == 100)
5033 return 0; /* same as default */
5036 /* underlining */
5037 val = attrs[LFACE_UNDERLINE_INDEX];
5038 if (!UNSPECIFIEDP (val))
5040 if (STRINGP (val))
5041 return 0; /* ttys can't use colored underlines */
5042 else if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
5043 return 0; /* same as default */
5044 else
5045 test_caps |= TTY_CAP_UNDERLINE;
5048 /* inverse video */
5049 val = attrs[LFACE_INVERSE_INDEX];
5050 if (!UNSPECIFIEDP (val))
5052 if (face_attr_equal_p (val, def_attrs[LFACE_INVERSE_INDEX]))
5053 return 0; /* same as default */
5054 else
5055 test_caps |= TTY_CAP_INVERSE;
5059 /* Color testing. */
5061 /* Default the color indices in FG_TTY_COLOR and BG_TTY_COLOR, since
5062 we use them when calling `tty_capable_p' below, even if the face
5063 specifies no colors. */
5064 fg_tty_color.pixel = FACE_TTY_DEFAULT_FG_COLOR;
5065 bg_tty_color.pixel = FACE_TTY_DEFAULT_BG_COLOR;
5067 /* Check if foreground color is close enough. */
5068 fg = attrs[LFACE_FOREGROUND_INDEX];
5069 if (STRINGP (fg))
5071 Lisp_Object def_fg = def_attrs[LFACE_FOREGROUND_INDEX];
5073 if (face_attr_equal_p (fg, def_fg))
5074 return 0; /* same as default */
5075 else if (! tty_lookup_color (f, fg, &fg_tty_color, &fg_std_color))
5076 return 0; /* not a valid color */
5077 else if (color_distance (&fg_tty_color, &fg_std_color)
5078 > TTY_SAME_COLOR_THRESHOLD)
5079 return 0; /* displayed color is too different */
5080 else
5081 /* Make sure the color is really different than the default. */
5083 XColor def_fg_color;
5084 if (tty_lookup_color (f, def_fg, &def_fg_color, 0)
5085 && (color_distance (&fg_tty_color, &def_fg_color)
5086 <= TTY_SAME_COLOR_THRESHOLD))
5087 return 0;
5091 /* Check if background color is close enough. */
5092 bg = attrs[LFACE_BACKGROUND_INDEX];
5093 if (STRINGP (bg))
5095 Lisp_Object def_bg = def_attrs[LFACE_BACKGROUND_INDEX];
5097 if (face_attr_equal_p (bg, def_bg))
5098 return 0; /* same as default */
5099 else if (! tty_lookup_color (f, bg, &bg_tty_color, &bg_std_color))
5100 return 0; /* not a valid color */
5101 else if (color_distance (&bg_tty_color, &bg_std_color)
5102 > TTY_SAME_COLOR_THRESHOLD)
5103 return 0; /* displayed color is too different */
5104 else
5105 /* Make sure the color is really different than the default. */
5107 XColor def_bg_color;
5108 if (tty_lookup_color (f, def_bg, &def_bg_color, 0)
5109 && (color_distance (&bg_tty_color, &def_bg_color)
5110 <= TTY_SAME_COLOR_THRESHOLD))
5111 return 0;
5115 /* If both foreground and background are requested, see if the
5116 distance between them is OK. We just check to see if the distance
5117 between the tty's foreground and background is close enough to the
5118 distance between the standard foreground and background. */
5119 if (STRINGP (fg) && STRINGP (bg))
5121 int delta_delta
5122 = (color_distance (&fg_std_color, &bg_std_color)
5123 - color_distance (&fg_tty_color, &bg_tty_color));
5124 if (delta_delta > TTY_SAME_COLOR_THRESHOLD
5125 || delta_delta < -TTY_SAME_COLOR_THRESHOLD)
5126 return 0;
5130 /* See if the capabilities we selected above are supported, with the
5131 given colors. */
5132 if (test_caps != 0 &&
5133 ! tty_capable_p (FRAME_TTY (f), test_caps, fg_tty_color.pixel, bg_tty_color.pixel))
5134 return 0;
5137 /* Hmmm, everything checks out, this terminal must support this face. */
5138 return 1;
5142 DEFUN ("display-supports-face-attributes-p",
5143 Fdisplay_supports_face_attributes_p, Sdisplay_supports_face_attributes_p,
5144 1, 2, 0,
5145 doc: /* Return non-nil if all the face attributes in ATTRIBUTES are supported.
5146 The optional argument DISPLAY can be a display name, a frame, or
5147 nil (meaning the selected frame's display).
5149 The definition of `supported' is somewhat heuristic, but basically means
5150 that a face containing all the attributes in ATTRIBUTES, when merged
5151 with the default face for display, can be represented in a way that's
5153 \(1) different in appearance than the default face, and
5154 \(2) `close in spirit' to what the attributes specify, if not exact.
5156 Point (2) implies that a `:weight black' attribute will be satisfied by
5157 any display that can display bold, and a `:foreground \"yellow\"' as long
5158 as it can display a yellowish color, but `:slant italic' will _not_ be
5159 satisfied by the tty display code's automatic substitution of a `dim'
5160 face for italic. */)
5161 (Lisp_Object attributes, Lisp_Object display)
5163 int supports = 0, i;
5164 Lisp_Object frame;
5165 struct frame *f;
5166 struct face *def_face;
5167 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5169 if (noninteractive || !initialized)
5170 /* We may not be able to access low-level face information in batch
5171 mode, or before being dumped, and this function is not going to
5172 be very useful in those cases anyway, so just give up. */
5173 return Qnil;
5175 if (NILP (display))
5176 frame = selected_frame;
5177 else if (FRAMEP (display))
5178 frame = display;
5179 else
5181 /* Find any frame on DISPLAY. */
5182 Lisp_Object fl_tail;
5184 frame = Qnil;
5185 for (fl_tail = Vframe_list; CONSP (fl_tail); fl_tail = XCDR (fl_tail))
5187 frame = XCAR (fl_tail);
5188 if (!NILP (Fequal (Fcdr (Fassq (Qdisplay,
5189 XFRAME (frame)->param_alist)),
5190 display)))
5191 break;
5195 CHECK_LIVE_FRAME (frame);
5196 f = XFRAME (frame);
5198 for (i = 0; i < LFACE_VECTOR_SIZE; i++)
5199 attrs[i] = Qunspecified;
5200 merge_face_ref (f, attributes, attrs, 1, 0);
5202 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5203 if (def_face == NULL)
5205 if (! realize_basic_faces (f))
5206 error ("Cannot realize default face");
5207 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5208 if (def_face == NULL)
5209 abort (); /* realize_basic_faces must have set it up */
5212 /* Dispatch to the appropriate handler. */
5213 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5214 supports = tty_supports_face_attributes_p (f, attrs, def_face);
5215 #ifdef HAVE_WINDOW_SYSTEM
5216 else
5217 supports = x_supports_face_attributes_p (f, attrs, def_face);
5218 #endif
5220 return supports ? Qt : Qnil;
5224 /***********************************************************************
5225 Font selection
5226 ***********************************************************************/
5228 DEFUN ("internal-set-font-selection-order",
5229 Finternal_set_font_selection_order,
5230 Sinternal_set_font_selection_order, 1, 1, 0,
5231 doc: /* Set font selection order for face font selection to ORDER.
5232 ORDER must be a list of length 4 containing the symbols `:width',
5233 `:height', `:weight', and `:slant'. Face attributes appearing
5234 first in ORDER are matched first, e.g. if `:height' appears before
5235 `:weight' in ORDER, font selection first tries to find a font with
5236 a suitable height, and then tries to match the font weight.
5237 Value is ORDER. */)
5238 (Lisp_Object order)
5240 Lisp_Object list;
5241 int i;
5242 int indices[DIM (font_sort_order)];
5244 CHECK_LIST (order);
5245 memset (indices, 0, sizeof indices);
5246 i = 0;
5248 for (list = order;
5249 CONSP (list) && i < DIM (indices);
5250 list = XCDR (list), ++i)
5252 Lisp_Object attr = XCAR (list);
5253 int xlfd;
5255 if (EQ (attr, QCwidth))
5256 xlfd = XLFD_SWIDTH;
5257 else if (EQ (attr, QCheight))
5258 xlfd = XLFD_POINT_SIZE;
5259 else if (EQ (attr, QCweight))
5260 xlfd = XLFD_WEIGHT;
5261 else if (EQ (attr, QCslant))
5262 xlfd = XLFD_SLANT;
5263 else
5264 break;
5266 if (indices[i] != 0)
5267 break;
5268 indices[i] = xlfd;
5271 if (!NILP (list) || i != DIM (indices))
5272 signal_error ("Invalid font sort order", order);
5273 for (i = 0; i < DIM (font_sort_order); ++i)
5274 if (indices[i] == 0)
5275 signal_error ("Invalid font sort order", order);
5277 if (memcmp (indices, font_sort_order, sizeof indices) != 0)
5279 memcpy (font_sort_order, indices, sizeof font_sort_order);
5280 free_all_realized_faces (Qnil);
5283 font_update_sort_order (font_sort_order);
5285 return Qnil;
5289 DEFUN ("internal-set-alternative-font-family-alist",
5290 Finternal_set_alternative_font_family_alist,
5291 Sinternal_set_alternative_font_family_alist, 1, 1, 0,
5292 doc: /* Define alternative font families to try in face font selection.
5293 ALIST is an alist of (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5294 Each ALTERNATIVE is tried in order if no fonts of font family FAMILY can
5295 be found. Value is ALIST. */)
5296 (Lisp_Object alist)
5298 Lisp_Object entry, tail, tail2;
5300 CHECK_LIST (alist);
5301 alist = Fcopy_sequence (alist);
5302 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5304 entry = XCAR (tail);
5305 CHECK_LIST (entry);
5306 entry = Fcopy_sequence (entry);
5307 XSETCAR (tail, entry);
5308 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5309 XSETCAR (tail2, Fintern (XCAR (tail2), Qnil));
5312 Vface_alternative_font_family_alist = alist;
5313 free_all_realized_faces (Qnil);
5314 return alist;
5318 DEFUN ("internal-set-alternative-font-registry-alist",
5319 Finternal_set_alternative_font_registry_alist,
5320 Sinternal_set_alternative_font_registry_alist, 1, 1, 0,
5321 doc: /* Define alternative font registries to try in face font selection.
5322 ALIST is an alist of (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5323 Each ALTERNATIVE is tried in order if no fonts of font registry REGISTRY can
5324 be found. Value is ALIST. */)
5325 (Lisp_Object alist)
5327 Lisp_Object entry, tail, tail2;
5329 CHECK_LIST (alist);
5330 alist = Fcopy_sequence (alist);
5331 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5333 entry = XCAR (tail);
5334 CHECK_LIST (entry);
5335 entry = Fcopy_sequence (entry);
5336 XSETCAR (tail, entry);
5337 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5338 XSETCAR (tail2, Fdowncase (XCAR (tail2)));
5340 Vface_alternative_font_registry_alist = alist;
5341 free_all_realized_faces (Qnil);
5342 return alist;
5346 #ifdef HAVE_WINDOW_SYSTEM
5348 /* Ignore the difference of font point size less than this value. */
5350 #define FONT_POINT_SIZE_QUANTUM 5
5352 /* Return the fontset id of the base fontset name or alias name given
5353 by the fontset attribute of ATTRS. Value is -1 if the fontset
5354 attribute of ATTRS doesn't name a fontset. */
5356 static int
5357 face_fontset (Lisp_Object *attrs)
5359 Lisp_Object name;
5361 name = attrs[LFACE_FONTSET_INDEX];
5362 if (!STRINGP (name))
5363 return -1;
5364 return fs_query_fontset (name, 0);
5367 #endif /* HAVE_WINDOW_SYSTEM */
5371 /***********************************************************************
5372 Face Realization
5373 ***********************************************************************/
5375 /* Realize basic faces on frame F. Value is zero if frame parameters
5376 of F don't contain enough information needed to realize the default
5377 face. */
5379 static int
5380 realize_basic_faces (struct frame *f)
5382 int success_p = 0;
5383 int count = SPECPDL_INDEX ();
5385 /* Block input here so that we won't be surprised by an X expose
5386 event, for instance, without having the faces set up. */
5387 BLOCK_INPUT;
5388 specbind (Qscalable_fonts_allowed, Qt);
5390 if (realize_default_face (f))
5392 realize_named_face (f, Qmode_line, MODE_LINE_FACE_ID);
5393 realize_named_face (f, Qmode_line_inactive, MODE_LINE_INACTIVE_FACE_ID);
5394 realize_named_face (f, Qtool_bar, TOOL_BAR_FACE_ID);
5395 realize_named_face (f, Qfringe, FRINGE_FACE_ID);
5396 realize_named_face (f, Qheader_line, HEADER_LINE_FACE_ID);
5397 realize_named_face (f, Qscroll_bar, SCROLL_BAR_FACE_ID);
5398 realize_named_face (f, Qborder, BORDER_FACE_ID);
5399 realize_named_face (f, Qcursor, CURSOR_FACE_ID);
5400 realize_named_face (f, Qmouse, MOUSE_FACE_ID);
5401 realize_named_face (f, Qmenu, MENU_FACE_ID);
5402 realize_named_face (f, Qvertical_border, VERTICAL_BORDER_FACE_ID);
5404 /* Reflect changes in the `menu' face in menu bars. */
5405 if (FRAME_FACE_CACHE (f)->menu_face_changed_p)
5407 FRAME_FACE_CACHE (f)->menu_face_changed_p = 0;
5408 #ifdef USE_X_TOOLKIT
5409 if (FRAME_WINDOW_P (f))
5410 x_update_menu_appearance (f);
5411 #endif
5414 success_p = 1;
5417 unbind_to (count, Qnil);
5418 UNBLOCK_INPUT;
5419 return success_p;
5423 /* Realize the default face on frame F. If the face is not fully
5424 specified, make it fully-specified. Attributes of the default face
5425 that are not explicitly specified are taken from frame parameters. */
5427 static int
5428 realize_default_face (struct frame *f)
5430 struct face_cache *c = FRAME_FACE_CACHE (f);
5431 Lisp_Object lface;
5432 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5433 struct face *face;
5435 /* If the `default' face is not yet known, create it. */
5436 lface = lface_from_face_name (f, Qdefault, 0);
5437 if (NILP (lface))
5439 Lisp_Object frame;
5440 XSETFRAME (frame, f);
5441 lface = Finternal_make_lisp_face (Qdefault, frame);
5444 #ifdef HAVE_WINDOW_SYSTEM
5445 if (FRAME_WINDOW_P (f))
5447 Lisp_Object font_object;
5449 XSETFONT (font_object, FRAME_FONT (f));
5450 set_lface_from_font (f, lface, font_object, f->default_face_done_p);
5451 LFACE_FONTSET (lface) = fontset_name (FRAME_FONTSET (f));
5452 f->default_face_done_p = 1;
5454 #endif /* HAVE_WINDOW_SYSTEM */
5456 if (!FRAME_WINDOW_P (f))
5458 LFACE_FAMILY (lface) = build_string ("default");
5459 LFACE_FOUNDRY (lface) = LFACE_FAMILY (lface);
5460 LFACE_SWIDTH (lface) = Qnormal;
5461 LFACE_HEIGHT (lface) = make_number (1);
5462 if (UNSPECIFIEDP (LFACE_WEIGHT (lface)))
5463 LFACE_WEIGHT (lface) = Qnormal;
5464 if (UNSPECIFIEDP (LFACE_SLANT (lface)))
5465 LFACE_SLANT (lface) = Qnormal;
5466 if (UNSPECIFIEDP (LFACE_FONTSET (lface)))
5467 LFACE_FONTSET (lface) = Qnil;
5470 if (UNSPECIFIEDP (LFACE_UNDERLINE (lface)))
5471 LFACE_UNDERLINE (lface) = Qnil;
5473 if (UNSPECIFIEDP (LFACE_OVERLINE (lface)))
5474 LFACE_OVERLINE (lface) = Qnil;
5476 if (UNSPECIFIEDP (LFACE_STRIKE_THROUGH (lface)))
5477 LFACE_STRIKE_THROUGH (lface) = Qnil;
5479 if (UNSPECIFIEDP (LFACE_BOX (lface)))
5480 LFACE_BOX (lface) = Qnil;
5482 if (UNSPECIFIEDP (LFACE_INVERSE (lface)))
5483 LFACE_INVERSE (lface) = Qnil;
5485 if (UNSPECIFIEDP (LFACE_FOREGROUND (lface)))
5487 /* This function is called so early that colors are not yet
5488 set in the frame parameter list. */
5489 Lisp_Object color = Fassq (Qforeground_color, f->param_alist);
5491 if (CONSP (color) && STRINGP (XCDR (color)))
5492 LFACE_FOREGROUND (lface) = XCDR (color);
5493 else if (FRAME_WINDOW_P (f))
5494 return 0;
5495 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5496 LFACE_FOREGROUND (lface) = build_string (unspecified_fg);
5497 else
5498 abort ();
5501 if (UNSPECIFIEDP (LFACE_BACKGROUND (lface)))
5503 /* This function is called so early that colors are not yet
5504 set in the frame parameter list. */
5505 Lisp_Object color = Fassq (Qbackground_color, f->param_alist);
5506 if (CONSP (color) && STRINGP (XCDR (color)))
5507 LFACE_BACKGROUND (lface) = XCDR (color);
5508 else if (FRAME_WINDOW_P (f))
5509 return 0;
5510 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5511 LFACE_BACKGROUND (lface) = build_string (unspecified_bg);
5512 else
5513 abort ();
5516 if (UNSPECIFIEDP (LFACE_STIPPLE (lface)))
5517 LFACE_STIPPLE (lface) = Qnil;
5519 /* Realize the face; it must be fully-specified now. */
5520 xassert (lface_fully_specified_p (XVECTOR (lface)->contents));
5521 check_lface (lface);
5522 memcpy (attrs, XVECTOR (lface)->contents, sizeof attrs);
5523 face = realize_face (c, attrs, DEFAULT_FACE_ID);
5525 #ifdef HAVE_WINDOW_SYSTEM
5526 #ifdef HAVE_X_WINDOWS
5527 if (FRAME_X_P (f) && face->font != FRAME_FONT (f))
5529 /* This can happen when making a frame on a display that does
5530 not support the default font. */
5531 if (!face->font)
5532 return 0;
5534 /* Otherwise, the font specified for the frame was not
5535 acceptable as a font for the default face (perhaps because
5536 auto-scaled fonts are rejected), so we must adjust the frame
5537 font. */
5538 x_set_font (f, LFACE_FONT (lface), Qnil);
5540 #endif /* HAVE_X_WINDOWS */
5541 #endif /* HAVE_WINDOW_SYSTEM */
5542 return 1;
5546 /* Realize basic faces other than the default face in face cache C.
5547 SYMBOL is the face name, ID is the face id the realized face must
5548 have. The default face must have been realized already. */
5550 static void
5551 realize_named_face (struct frame *f, Lisp_Object symbol, int id)
5553 struct face_cache *c = FRAME_FACE_CACHE (f);
5554 Lisp_Object lface = lface_from_face_name (f, symbol, 0);
5555 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5556 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5557 struct face *new_face;
5559 /* The default face must exist and be fully specified. */
5560 get_lface_attributes_no_remap (f, Qdefault, attrs, 1);
5561 check_lface_attrs (attrs);
5562 xassert (lface_fully_specified_p (attrs));
5564 /* If SYMBOL isn't know as a face, create it. */
5565 if (NILP (lface))
5567 Lisp_Object frame;
5568 XSETFRAME (frame, f);
5569 lface = Finternal_make_lisp_face (symbol, frame);
5572 /* Merge SYMBOL's face with the default face. */
5573 get_lface_attributes_no_remap (f, symbol, symbol_attrs, 1);
5574 merge_face_vectors (f, symbol_attrs, attrs, 0);
5576 /* Realize the face. */
5577 new_face = realize_face (c, attrs, id);
5581 /* Realize the fully-specified face with attributes ATTRS in face
5582 cache CACHE for ASCII characters. If FORMER_FACE_ID is
5583 non-negative, it is an ID of face to remove before caching the new
5584 face. Value is a pointer to the newly created realized face. */
5586 static struct face *
5587 realize_face (struct face_cache *cache, Lisp_Object *attrs, int former_face_id)
5589 struct face *face;
5591 /* LFACE must be fully specified. */
5592 xassert (cache != NULL);
5593 check_lface_attrs (attrs);
5595 if (former_face_id >= 0 && cache->used > former_face_id)
5597 /* Remove the former face. */
5598 struct face *former_face = cache->faces_by_id[former_face_id];
5599 uncache_face (cache, former_face);
5600 free_realized_face (cache->f, former_face);
5601 SET_FRAME_GARBAGED (cache->f);
5604 if (FRAME_WINDOW_P (cache->f))
5605 face = realize_x_face (cache, attrs);
5606 else if (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f))
5607 face = realize_tty_face (cache, attrs);
5608 else if (FRAME_INITIAL_P (cache->f))
5610 /* Create a dummy face. */
5611 face = make_realized_face (attrs);
5613 else
5614 abort ();
5616 /* Insert the new face. */
5617 cache_face (cache, face, lface_hash (attrs));
5618 return face;
5622 #ifdef HAVE_WINDOW_SYSTEM
5623 /* Realize the fully-specified face that uses FONT-OBJECT and has the
5624 same attributes as BASE_FACE except for the font on frame F.
5625 FONT-OBJECT may be nil, in which case, realized a face of
5626 no-font. */
5628 static struct face *
5629 realize_non_ascii_face (struct frame *f, Lisp_Object font_object, struct face *base_face)
5631 struct face_cache *cache = FRAME_FACE_CACHE (f);
5632 struct face *face;
5634 face = (struct face *) xmalloc (sizeof *face);
5635 *face = *base_face;
5636 face->gc = 0;
5637 face->extra = NULL;
5638 face->overstrike
5639 = (! NILP (font_object)
5640 && FONT_WEIGHT_NAME_NUMERIC (face->lface[LFACE_WEIGHT_INDEX]) > 100
5641 && FONT_WEIGHT_NUMERIC (font_object) <= 100);
5643 /* Don't try to free the colors copied bitwise from BASE_FACE. */
5644 face->colors_copied_bitwise_p = 1;
5645 face->font = NILP (font_object) ? NULL : XFONT_OBJECT (font_object);
5646 face->gc = 0;
5648 cache_face (cache, face, face->hash);
5650 return face;
5652 #endif /* HAVE_WINDOW_SYSTEM */
5655 /* Realize the fully-specified face with attributes ATTRS in face
5656 cache CACHE for ASCII characters. Do it for X frame CACHE->f. If
5657 the new face doesn't share font with the default face, a fontname
5658 is allocated from the heap and set in `font_name' of the new face,
5659 but it is not yet loaded here. Value is a pointer to the newly
5660 created realized face. */
5662 static struct face *
5663 realize_x_face (struct face_cache *cache, Lisp_Object *attrs)
5665 struct face *face = NULL;
5666 #ifdef HAVE_WINDOW_SYSTEM
5667 struct face *default_face;
5668 struct frame *f;
5669 Lisp_Object stipple, overline, strike_through, box;
5671 xassert (FRAME_WINDOW_P (cache->f));
5673 /* Allocate a new realized face. */
5674 face = make_realized_face (attrs);
5675 face->ascii_face = face;
5677 f = cache->f;
5679 /* Determine the font to use. Most of the time, the font will be
5680 the same as the font of the default face, so try that first. */
5681 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5682 if (default_face
5683 && lface_same_font_attributes_p (default_face->lface, attrs))
5685 face->font = default_face->font;
5686 face->fontset
5687 = make_fontset_for_ascii_face (f, default_face->fontset, face);
5689 else
5691 /* If the face attribute ATTRS specifies a fontset, use it as
5692 the base of a new realized fontset. Otherwise, use the same
5693 base fontset as of the default face. The base determines
5694 registry and encoding of a font. It may also determine
5695 foundry and family. The other fields of font name pattern
5696 are constructed from ATTRS. */
5697 int fontset = face_fontset (attrs);
5699 /* If we are realizing the default face, ATTRS should specify a
5700 fontset. In other words, if FONTSET is -1, we are not
5701 realizing the default face, thus the default face should have
5702 already been realized. */
5703 if (fontset == -1)
5705 if (default_face)
5706 fontset = default_face->fontset;
5707 if (fontset == -1)
5708 abort ();
5710 if (! FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5711 attrs[LFACE_FONT_INDEX]
5712 = font_load_for_lface (f, attrs, attrs[LFACE_FONT_INDEX]);
5713 if (FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5715 face->font = XFONT_OBJECT (attrs[LFACE_FONT_INDEX]);
5716 face->fontset = make_fontset_for_ascii_face (f, fontset, face);
5718 else
5720 face->font = NULL;
5721 face->fontset = -1;
5725 if (face->font
5726 && FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]) > 100
5727 && FONT_WEIGHT_NUMERIC (attrs[LFACE_FONT_INDEX]) <= 100)
5728 face->overstrike = 1;
5730 /* Load colors, and set remaining attributes. */
5732 load_face_colors (f, face, attrs);
5734 /* Set up box. */
5735 box = attrs[LFACE_BOX_INDEX];
5736 if (STRINGP (box))
5738 /* A simple box of line width 1 drawn in color given by
5739 the string. */
5740 face->box_color = load_color (f, face, attrs[LFACE_BOX_INDEX],
5741 LFACE_BOX_INDEX);
5742 face->box = FACE_SIMPLE_BOX;
5743 face->box_line_width = 1;
5745 else if (INTEGERP (box))
5747 /* Simple box of specified line width in foreground color of the
5748 face. */
5749 xassert (XINT (box) != 0);
5750 face->box = FACE_SIMPLE_BOX;
5751 face->box_line_width = XINT (box);
5752 face->box_color = face->foreground;
5753 face->box_color_defaulted_p = 1;
5755 else if (CONSP (box))
5757 /* `(:width WIDTH :color COLOR :shadow SHADOW)'. SHADOW
5758 being one of `raised' or `sunken'. */
5759 face->box = FACE_SIMPLE_BOX;
5760 face->box_color = face->foreground;
5761 face->box_color_defaulted_p = 1;
5762 face->box_line_width = 1;
5764 while (CONSP (box))
5766 Lisp_Object keyword, value;
5768 keyword = XCAR (box);
5769 box = XCDR (box);
5771 if (!CONSP (box))
5772 break;
5773 value = XCAR (box);
5774 box = XCDR (box);
5776 if (EQ (keyword, QCline_width))
5778 if (INTEGERP (value) && XINT (value) != 0)
5779 face->box_line_width = XINT (value);
5781 else if (EQ (keyword, QCcolor))
5783 if (STRINGP (value))
5785 face->box_color = load_color (f, face, value,
5786 LFACE_BOX_INDEX);
5787 face->use_box_color_for_shadows_p = 1;
5790 else if (EQ (keyword, QCstyle))
5792 if (EQ (value, Qreleased_button))
5793 face->box = FACE_RAISED_BOX;
5794 else if (EQ (value, Qpressed_button))
5795 face->box = FACE_SUNKEN_BOX;
5800 /* Text underline, overline, strike-through. */
5802 if (EQ (attrs[LFACE_UNDERLINE_INDEX], Qt))
5804 /* Use default color (same as foreground color). */
5805 face->underline_p = 1;
5806 face->underline_defaulted_p = 1;
5807 face->underline_color = 0;
5809 else if (STRINGP (attrs[LFACE_UNDERLINE_INDEX]))
5811 /* Use specified color. */
5812 face->underline_p = 1;
5813 face->underline_defaulted_p = 0;
5814 face->underline_color
5815 = load_color (f, face, attrs[LFACE_UNDERLINE_INDEX],
5816 LFACE_UNDERLINE_INDEX);
5818 else if (NILP (attrs[LFACE_UNDERLINE_INDEX]))
5820 face->underline_p = 0;
5821 face->underline_defaulted_p = 0;
5822 face->underline_color = 0;
5825 overline = attrs[LFACE_OVERLINE_INDEX];
5826 if (STRINGP (overline))
5828 face->overline_color
5829 = load_color (f, face, attrs[LFACE_OVERLINE_INDEX],
5830 LFACE_OVERLINE_INDEX);
5831 face->overline_p = 1;
5833 else if (EQ (overline, Qt))
5835 face->overline_color = face->foreground;
5836 face->overline_color_defaulted_p = 1;
5837 face->overline_p = 1;
5840 strike_through = attrs[LFACE_STRIKE_THROUGH_INDEX];
5841 if (STRINGP (strike_through))
5843 face->strike_through_color
5844 = load_color (f, face, attrs[LFACE_STRIKE_THROUGH_INDEX],
5845 LFACE_STRIKE_THROUGH_INDEX);
5846 face->strike_through_p = 1;
5848 else if (EQ (strike_through, Qt))
5850 face->strike_through_color = face->foreground;
5851 face->strike_through_color_defaulted_p = 1;
5852 face->strike_through_p = 1;
5855 stipple = attrs[LFACE_STIPPLE_INDEX];
5856 if (!NILP (stipple))
5857 face->stipple = load_pixmap (f, stipple, &face->pixmap_w, &face->pixmap_h);
5858 #endif /* HAVE_WINDOW_SYSTEM */
5860 return face;
5864 /* Map a specified color of face FACE on frame F to a tty color index.
5865 IDX is either LFACE_FOREGROUND_INDEX or LFACE_BACKGROUND_INDEX, and
5866 specifies which color to map. Set *DEFAULTED to 1 if mapping to the
5867 default foreground/background colors. */
5869 static void
5870 map_tty_color (struct frame *f, struct face *face, enum lface_attribute_index idx, int *defaulted)
5872 Lisp_Object frame, color, def;
5873 int foreground_p = idx == LFACE_FOREGROUND_INDEX;
5874 unsigned long default_pixel, default_other_pixel, pixel;
5876 xassert (idx == LFACE_FOREGROUND_INDEX || idx == LFACE_BACKGROUND_INDEX);
5878 if (foreground_p)
5880 pixel = default_pixel = FACE_TTY_DEFAULT_FG_COLOR;
5881 default_other_pixel = FACE_TTY_DEFAULT_BG_COLOR;
5883 else
5885 pixel = default_pixel = FACE_TTY_DEFAULT_BG_COLOR;
5886 default_other_pixel = FACE_TTY_DEFAULT_FG_COLOR;
5889 XSETFRAME (frame, f);
5890 color = face->lface[idx];
5892 if (STRINGP (color)
5893 && SCHARS (color)
5894 && CONSP (Vtty_defined_color_alist)
5895 && (def = assq_no_quit (color, call1 (Qtty_color_alist, frame)),
5896 CONSP (def)))
5898 /* Associations in tty-defined-color-alist are of the form
5899 (NAME INDEX R G B). We need the INDEX part. */
5900 pixel = XINT (XCAR (XCDR (def)));
5903 if (pixel == default_pixel && STRINGP (color))
5905 pixel = load_color (f, face, color, idx);
5907 #ifdef MSDOS
5908 /* If the foreground of the default face is the default color,
5909 use the foreground color defined by the frame. */
5910 if (FRAME_MSDOS_P (f))
5912 if (pixel == default_pixel
5913 || pixel == FACE_TTY_DEFAULT_COLOR)
5915 if (foreground_p)
5916 pixel = FRAME_FOREGROUND_PIXEL (f);
5917 else
5918 pixel = FRAME_BACKGROUND_PIXEL (f);
5919 face->lface[idx] = tty_color_name (f, pixel);
5920 *defaulted = 1;
5922 else if (pixel == default_other_pixel)
5924 if (foreground_p)
5925 pixel = FRAME_BACKGROUND_PIXEL (f);
5926 else
5927 pixel = FRAME_FOREGROUND_PIXEL (f);
5928 face->lface[idx] = tty_color_name (f, pixel);
5929 *defaulted = 1;
5932 #endif /* MSDOS */
5935 if (foreground_p)
5936 face->foreground = pixel;
5937 else
5938 face->background = pixel;
5942 /* Realize the fully-specified face with attributes ATTRS in face
5943 cache CACHE for ASCII characters. Do it for TTY frame CACHE->f.
5944 Value is a pointer to the newly created realized face. */
5946 static struct face *
5947 realize_tty_face (struct face_cache *cache, Lisp_Object *attrs)
5949 struct face *face;
5950 int weight, slant;
5951 int face_colors_defaulted = 0;
5952 struct frame *f = cache->f;
5954 /* Frame must be a termcap frame. */
5955 xassert (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f));
5957 /* Allocate a new realized face. */
5958 face = make_realized_face (attrs);
5959 #if 0
5960 face->font_name = FRAME_MSDOS_P (cache->f) ? "ms-dos" : "tty";
5961 #endif
5963 /* Map face attributes to TTY appearances. We map slant to
5964 dimmed text because we want italic text to appear differently
5965 and because dimmed text is probably used infrequently. */
5966 weight = FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]);
5967 slant = FONT_SLANT_NAME_NUMERIC (attrs[LFACE_SLANT_INDEX]);
5968 if (weight > 100)
5969 face->tty_bold_p = 1;
5970 if (weight < 100 || slant != 100)
5971 face->tty_dim_p = 1;
5972 if (!NILP (attrs[LFACE_UNDERLINE_INDEX]))
5973 face->tty_underline_p = 1;
5974 if (!NILP (attrs[LFACE_INVERSE_INDEX]))
5975 face->tty_reverse_p = 1;
5977 /* Map color names to color indices. */
5978 map_tty_color (f, face, LFACE_FOREGROUND_INDEX, &face_colors_defaulted);
5979 map_tty_color (f, face, LFACE_BACKGROUND_INDEX, &face_colors_defaulted);
5981 /* Swap colors if face is inverse-video. If the colors are taken
5982 from the frame colors, they are already inverted, since the
5983 frame-creation function calls x-handle-reverse-video. */
5984 if (face->tty_reverse_p && !face_colors_defaulted)
5986 unsigned long tem = face->foreground;
5987 face->foreground = face->background;
5988 face->background = tem;
5991 if (tty_suppress_bold_inverse_default_colors_p
5992 && face->tty_bold_p
5993 && face->background == FACE_TTY_DEFAULT_FG_COLOR
5994 && face->foreground == FACE_TTY_DEFAULT_BG_COLOR)
5995 face->tty_bold_p = 0;
5997 return face;
6001 DEFUN ("tty-suppress-bold-inverse-default-colors",
6002 Ftty_suppress_bold_inverse_default_colors,
6003 Stty_suppress_bold_inverse_default_colors, 1, 1, 0,
6004 doc: /* Suppress/allow boldness of faces with inverse default colors.
6005 SUPPRESS non-nil means suppress it.
6006 This affects bold faces on TTYs whose foreground is the default background
6007 color of the display and whose background is the default foreground color.
6008 For such faces, the bold face attribute is ignored if this variable
6009 is non-nil. */)
6010 (Lisp_Object suppress)
6012 tty_suppress_bold_inverse_default_colors_p = !NILP (suppress);
6013 ++face_change_count;
6014 return suppress;
6019 /***********************************************************************
6020 Computing Faces
6021 ***********************************************************************/
6023 /* Return the ID of the face to use to display character CH with face
6024 property PROP on frame F in current_buffer. */
6027 compute_char_face (struct frame *f, int ch, Lisp_Object prop)
6029 int face_id;
6031 if (NILP (current_buffer->enable_multibyte_characters))
6032 ch = 0;
6034 if (NILP (prop))
6036 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6037 face_id = FACE_FOR_CHAR (f, face, ch, -1, Qnil);
6039 else
6041 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6042 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6043 memcpy (attrs, default_face->lface, sizeof attrs);
6044 merge_face_ref (f, prop, attrs, 1, 0);
6045 face_id = lookup_face (f, attrs);
6048 return face_id;
6051 /* Return the face ID associated with buffer position POS for
6052 displaying ASCII characters. Return in *ENDPTR the position at
6053 which a different face is needed, as far as text properties and
6054 overlays are concerned. W is a window displaying current_buffer.
6056 REGION_BEG, REGION_END delimit the region, so it can be
6057 highlighted.
6059 LIMIT is a position not to scan beyond. That is to limit the time
6060 this function can take.
6062 If MOUSE is non-zero, use the character's mouse-face, not its face.
6064 BASE_FACE_ID, if non-negative, specifies a base face id to use
6065 instead of DEFAULT_FACE_ID.
6067 The face returned is suitable for displaying ASCII characters. */
6070 face_at_buffer_position (struct window *w, EMACS_INT pos,
6071 EMACS_INT region_beg, EMACS_INT region_end,
6072 EMACS_INT *endptr, EMACS_INT limit,
6073 int mouse, int base_face_id)
6075 struct frame *f = XFRAME (w->frame);
6076 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6077 Lisp_Object prop, position;
6078 int i, noverlays;
6079 Lisp_Object *overlay_vec;
6080 Lisp_Object frame;
6081 EMACS_INT endpos;
6082 Lisp_Object propname = mouse ? Qmouse_face : Qface;
6083 Lisp_Object limit1, end;
6084 struct face *default_face;
6086 /* W must display the current buffer. We could write this function
6087 to use the frame and buffer of W, but right now it doesn't. */
6088 /* xassert (XBUFFER (w->buffer) == current_buffer); */
6090 XSETFRAME (frame, f);
6091 XSETFASTINT (position, pos);
6093 endpos = ZV;
6094 if (pos < region_beg && region_beg < endpos)
6095 endpos = region_beg;
6097 /* Get the `face' or `mouse_face' text property at POS, and
6098 determine the next position at which the property changes. */
6099 prop = Fget_text_property (position, propname, w->buffer);
6100 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
6101 end = Fnext_single_property_change (position, propname, w->buffer, limit1);
6102 if (INTEGERP (end))
6103 endpos = XINT (end);
6105 /* Look at properties from overlays. */
6107 EMACS_INT next_overlay;
6109 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, &next_overlay, 0);
6110 if (next_overlay < endpos)
6111 endpos = next_overlay;
6114 *endptr = endpos;
6116 default_face = FACE_FROM_ID (f, base_face_id >= 0 ? base_face_id
6117 : NILP (Vface_remapping_alist) ? DEFAULT_FACE_ID
6118 : lookup_basic_face (f, DEFAULT_FACE_ID));
6120 /* Optimize common cases where we can use the default face. */
6121 if (noverlays == 0
6122 && NILP (prop)
6123 && !(pos >= region_beg && pos < region_end))
6124 return default_face->id;
6126 /* Begin with attributes from the default face. */
6127 memcpy (attrs, default_face->lface, sizeof attrs);
6129 /* Merge in attributes specified via text properties. */
6130 if (!NILP (prop))
6131 merge_face_ref (f, prop, attrs, 1, 0);
6133 /* Now merge the overlay data. */
6134 noverlays = sort_overlays (overlay_vec, noverlays, w);
6135 for (i = 0; i < noverlays; i++)
6137 Lisp_Object oend;
6138 int oendpos;
6140 prop = Foverlay_get (overlay_vec[i], propname);
6141 if (!NILP (prop))
6142 merge_face_ref (f, prop, attrs, 1, 0);
6144 oend = OVERLAY_END (overlay_vec[i]);
6145 oendpos = OVERLAY_POSITION (oend);
6146 if (oendpos < endpos)
6147 endpos = oendpos;
6150 /* If in the region, merge in the region face. */
6151 if (pos >= region_beg && pos < region_end)
6153 merge_named_face (f, Qregion, attrs, 0);
6155 if (region_end < endpos)
6156 endpos = region_end;
6159 *endptr = endpos;
6161 /* Look up a realized face with the given face attributes,
6162 or realize a new one for ASCII characters. */
6163 return lookup_face (f, attrs);
6166 /* Return the face ID at buffer position POS for displaying ASCII
6167 characters associated with overlay strings for overlay OVERLAY.
6169 Like face_at_buffer_position except for OVERLAY. Currently it
6170 simply disregards the `face' properties of all overlays. */
6173 face_for_overlay_string (struct window *w, EMACS_INT pos,
6174 EMACS_INT region_beg, EMACS_INT region_end,
6175 EMACS_INT *endptr, EMACS_INT limit,
6176 int mouse, Lisp_Object overlay)
6178 struct frame *f = XFRAME (w->frame);
6179 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6180 Lisp_Object prop, position;
6181 Lisp_Object frame;
6182 int endpos;
6183 Lisp_Object propname = mouse ? Qmouse_face : Qface;
6184 Lisp_Object limit1, end;
6185 struct face *default_face;
6187 /* W must display the current buffer. We could write this function
6188 to use the frame and buffer of W, but right now it doesn't. */
6189 /* xassert (XBUFFER (w->buffer) == current_buffer); */
6191 XSETFRAME (frame, f);
6192 XSETFASTINT (position, pos);
6194 endpos = ZV;
6195 if (pos < region_beg && region_beg < endpos)
6196 endpos = region_beg;
6198 /* Get the `face' or `mouse_face' text property at POS, and
6199 determine the next position at which the property changes. */
6200 prop = Fget_text_property (position, propname, w->buffer);
6201 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
6202 end = Fnext_single_property_change (position, propname, w->buffer, limit1);
6203 if (INTEGERP (end))
6204 endpos = XINT (end);
6206 *endptr = endpos;
6208 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
6210 /* Optimize common cases where we can use the default face. */
6211 if (NILP (prop)
6212 && !(pos >= region_beg && pos < region_end))
6213 return DEFAULT_FACE_ID;
6215 /* Begin with attributes from the default face. */
6216 memcpy (attrs, default_face->lface, sizeof attrs);
6218 /* Merge in attributes specified via text properties. */
6219 if (!NILP (prop))
6220 merge_face_ref (f, prop, attrs, 1, 0);
6222 /* If in the region, merge in the region face. */
6223 if (pos >= region_beg && pos < region_end)
6225 merge_named_face (f, Qregion, attrs, 0);
6227 if (region_end < endpos)
6228 endpos = region_end;
6231 *endptr = endpos;
6233 /* Look up a realized face with the given face attributes,
6234 or realize a new one for ASCII characters. */
6235 return lookup_face (f, attrs);
6239 /* Compute the face at character position POS in Lisp string STRING on
6240 window W, for ASCII characters.
6242 If STRING is an overlay string, it comes from position BUFPOS in
6243 current_buffer, otherwise BUFPOS is zero to indicate that STRING is
6244 not an overlay string. W must display the current buffer.
6245 REGION_BEG and REGION_END give the start and end positions of the
6246 region; both are -1 if no region is visible.
6248 BASE_FACE_ID is the id of a face to merge with. For strings coming
6249 from overlays or the `display' property it is the face at BUFPOS.
6251 If MOUSE_P is non-zero, use the character's mouse-face, not its face.
6253 Set *ENDPTR to the next position where to check for faces in
6254 STRING; -1 if the face is constant from POS to the end of the
6255 string.
6257 Value is the id of the face to use. The face returned is suitable
6258 for displaying ASCII characters. */
6261 face_at_string_position (struct window *w, Lisp_Object string,
6262 EMACS_INT pos, EMACS_INT bufpos,
6263 EMACS_INT region_beg, EMACS_INT region_end,
6264 EMACS_INT *endptr, enum face_id base_face_id,
6265 int mouse_p)
6267 Lisp_Object prop, position, end, limit;
6268 struct frame *f = XFRAME (WINDOW_FRAME (w));
6269 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6270 struct face *base_face;
6271 int multibyte_p = STRING_MULTIBYTE (string);
6272 Lisp_Object prop_name = mouse_p ? Qmouse_face : Qface;
6274 /* Get the value of the face property at the current position within
6275 STRING. Value is nil if there is no face property. */
6276 XSETFASTINT (position, pos);
6277 prop = Fget_text_property (position, prop_name, string);
6279 /* Get the next position at which to check for faces. Value of end
6280 is nil if face is constant all the way to the end of the string.
6281 Otherwise it is a string position where to check faces next.
6282 Limit is the maximum position up to which to check for property
6283 changes in Fnext_single_property_change. Strings are usually
6284 short, so set the limit to the end of the string. */
6285 XSETFASTINT (limit, SCHARS (string));
6286 end = Fnext_single_property_change (position, prop_name, string, limit);
6287 if (INTEGERP (end))
6288 *endptr = XFASTINT (end);
6289 else
6290 *endptr = -1;
6292 base_face = FACE_FROM_ID (f, base_face_id);
6293 xassert (base_face);
6295 /* Optimize the default case that there is no face property and we
6296 are not in the region. */
6297 if (NILP (prop)
6298 && (base_face_id != DEFAULT_FACE_ID
6299 /* BUFPOS <= 0 means STRING is not an overlay string, so
6300 that the region doesn't have to be taken into account. */
6301 || bufpos <= 0
6302 || bufpos < region_beg
6303 || bufpos >= region_end)
6304 && (multibyte_p
6305 /* We can't realize faces for different charsets differently
6306 if we don't have fonts, so we can stop here if not working
6307 on a window-system frame. */
6308 || !FRAME_WINDOW_P (f)
6309 || FACE_SUITABLE_FOR_CHAR_P (base_face, 0)))
6310 return base_face->id;
6312 /* Begin with attributes from the base face. */
6313 memcpy (attrs, base_face->lface, sizeof attrs);
6315 /* Merge in attributes specified via text properties. */
6316 if (!NILP (prop))
6317 merge_face_ref (f, prop, attrs, 1, 0);
6319 /* If in the region, merge in the region face. */
6320 if (bufpos
6321 && bufpos >= region_beg
6322 && bufpos < region_end)
6323 merge_named_face (f, Qregion, attrs, 0);
6325 /* Look up a realized face with the given face attributes,
6326 or realize a new one for ASCII characters. */
6327 return lookup_face (f, attrs);
6331 /* Merge a face into a realized face.
6333 F is frame where faces are (to be) realized.
6335 FACE_NAME is named face to merge.
6337 If FACE_NAME is nil, FACE_ID is face_id of realized face to merge.
6339 If FACE_NAME is t, FACE_ID is lface_id of face to merge.
6341 BASE_FACE_ID is realized face to merge into.
6343 Return new face id.
6347 merge_faces (struct frame *f, Lisp_Object face_name, int face_id, int base_face_id)
6349 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6350 struct face *base_face;
6352 base_face = FACE_FROM_ID (f, base_face_id);
6353 if (!base_face)
6354 return base_face_id;
6356 if (EQ (face_name, Qt))
6358 if (face_id < 0 || face_id >= lface_id_to_name_size)
6359 return base_face_id;
6360 face_name = lface_id_to_name[face_id];
6361 /* When called during make-frame, lookup_derived_face may fail
6362 if the faces are uninitialized. Don't signal an error. */
6363 face_id = lookup_derived_face (f, face_name, base_face_id, 0);
6364 return (face_id >= 0 ? face_id : base_face_id);
6367 /* Begin with attributes from the base face. */
6368 memcpy (attrs, base_face->lface, sizeof attrs);
6370 if (!NILP (face_name))
6372 if (!merge_named_face (f, face_name, attrs, 0))
6373 return base_face_id;
6375 else
6377 struct face *face;
6378 if (face_id < 0)
6379 return base_face_id;
6380 face = FACE_FROM_ID (f, face_id);
6381 if (!face)
6382 return base_face_id;
6383 merge_face_vectors (f, face->lface, attrs, 0);
6386 /* Look up a realized face with the given face attributes,
6387 or realize a new one for ASCII characters. */
6388 return lookup_face (f, attrs);
6393 #ifndef HAVE_X_WINDOWS
6394 DEFUN ("x-load-color-file", Fx_load_color_file,
6395 Sx_load_color_file, 1, 1, 0,
6396 doc: /* Create an alist of color entries from an external file.
6398 The file should define one named RGB color per line like so:
6399 R G B name
6400 where R,G,B are numbers between 0 and 255 and name is an arbitrary string. */)
6401 (Lisp_Object filename)
6403 FILE *fp;
6404 Lisp_Object cmap = Qnil;
6405 Lisp_Object abspath;
6407 CHECK_STRING (filename);
6408 abspath = Fexpand_file_name (filename, Qnil);
6410 fp = fopen (SDATA (filename), "rt");
6411 if (fp)
6413 char buf[512];
6414 int red, green, blue;
6415 int num;
6417 BLOCK_INPUT;
6419 while (fgets (buf, sizeof (buf), fp) != NULL) {
6420 if (sscanf (buf, "%u %u %u %n", &red, &green, &blue, &num) == 3)
6422 char *name = buf + num;
6423 num = strlen (name) - 1;
6424 if (num >= 0 && name[num] == '\n')
6425 name[num] = 0;
6426 cmap = Fcons (Fcons (build_string (name),
6427 #ifdef WINDOWSNT
6428 make_number (RGB (red, green, blue))),
6429 #else
6430 make_number ((red << 16) | (green << 8) | blue)),
6431 #endif
6432 cmap);
6435 fclose (fp);
6437 UNBLOCK_INPUT;
6440 return cmap;
6442 #endif
6445 /***********************************************************************
6446 Tests
6447 ***********************************************************************/
6449 #if GLYPH_DEBUG
6451 /* Print the contents of the realized face FACE to stderr. */
6453 static void
6454 dump_realized_face (face)
6455 struct face *face;
6457 fprintf (stderr, "ID: %d\n", face->id);
6458 #ifdef HAVE_X_WINDOWS
6459 fprintf (stderr, "gc: %ld\n", (long) face->gc);
6460 #endif
6461 fprintf (stderr, "foreground: 0x%lx (%s)\n",
6462 face->foreground,
6463 SDATA (face->lface[LFACE_FOREGROUND_INDEX]));
6464 fprintf (stderr, "background: 0x%lx (%s)\n",
6465 face->background,
6466 SDATA (face->lface[LFACE_BACKGROUND_INDEX]));
6467 if (face->font)
6468 fprintf (stderr, "font_name: %s (%s)\n",
6469 SDATA (face->font->props[FONT_NAME_INDEX]),
6470 SDATA (face->lface[LFACE_FAMILY_INDEX]));
6471 #ifdef HAVE_X_WINDOWS
6472 fprintf (stderr, "font = %p\n", face->font);
6473 #endif
6474 fprintf (stderr, "fontset: %d\n", face->fontset);
6475 fprintf (stderr, "underline: %d (%s)\n",
6476 face->underline_p,
6477 SDATA (Fsymbol_name (face->lface[LFACE_UNDERLINE_INDEX])));
6478 fprintf (stderr, "hash: %d\n", face->hash);
6482 DEFUN ("dump-face", Fdump_face, Sdump_face, 0, 1, 0, doc: /* */)
6483 (Lisp_Object n)
6485 if (NILP (n))
6487 int i;
6489 fprintf (stderr, "font selection order: ");
6490 for (i = 0; i < DIM (font_sort_order); ++i)
6491 fprintf (stderr, "%d ", font_sort_order[i]);
6492 fprintf (stderr, "\n");
6494 fprintf (stderr, "alternative fonts: ");
6495 debug_print (Vface_alternative_font_family_alist);
6496 fprintf (stderr, "\n");
6498 for (i = 0; i < FRAME_FACE_CACHE (SELECTED_FRAME ())->used; ++i)
6499 Fdump_face (make_number (i));
6501 else
6503 struct face *face;
6504 CHECK_NUMBER (n);
6505 face = FACE_FROM_ID (SELECTED_FRAME (), XINT (n));
6506 if (face == NULL)
6507 error ("Not a valid face");
6508 dump_realized_face (face);
6511 return Qnil;
6515 DEFUN ("show-face-resources", Fshow_face_resources, Sshow_face_resources,
6516 0, 0, 0, doc: /* */)
6517 (void)
6519 fprintf (stderr, "number of colors = %d\n", ncolors_allocated);
6520 fprintf (stderr, "number of pixmaps = %d\n", npixmaps_allocated);
6521 fprintf (stderr, "number of GCs = %d\n", ngcs);
6522 return Qnil;
6525 #endif /* GLYPH_DEBUG != 0 */
6529 /***********************************************************************
6530 Initialization
6531 ***********************************************************************/
6533 void
6534 syms_of_xfaces (void)
6536 Qface = intern_c_string ("face");
6537 staticpro (&Qface);
6538 Qface_no_inherit = intern_c_string ("face-no-inherit");
6539 staticpro (&Qface_no_inherit);
6540 Qbitmap_spec_p = intern_c_string ("bitmap-spec-p");
6541 staticpro (&Qbitmap_spec_p);
6542 Qframe_set_background_mode = intern_c_string ("frame-set-background-mode");
6543 staticpro (&Qframe_set_background_mode);
6545 /* Lisp face attribute keywords. */
6546 QCfamily = intern_c_string (":family");
6547 staticpro (&QCfamily);
6548 QCheight = intern_c_string (":height");
6549 staticpro (&QCheight);
6550 QCweight = intern_c_string (":weight");
6551 staticpro (&QCweight);
6552 QCslant = intern_c_string (":slant");
6553 staticpro (&QCslant);
6554 QCunderline = intern_c_string (":underline");
6555 staticpro (&QCunderline);
6556 QCinverse_video = intern_c_string (":inverse-video");
6557 staticpro (&QCinverse_video);
6558 QCreverse_video = intern_c_string (":reverse-video");
6559 staticpro (&QCreverse_video);
6560 QCforeground = intern_c_string (":foreground");
6561 staticpro (&QCforeground);
6562 QCbackground = intern_c_string (":background");
6563 staticpro (&QCbackground);
6564 QCstipple = intern_c_string (":stipple");
6565 staticpro (&QCstipple);
6566 QCwidth = intern_c_string (":width");
6567 staticpro (&QCwidth);
6568 QCfont = intern_c_string (":font");
6569 staticpro (&QCfont);
6570 QCfontset = intern_c_string (":fontset");
6571 staticpro (&QCfontset);
6572 QCbold = intern_c_string (":bold");
6573 staticpro (&QCbold);
6574 QCitalic = intern_c_string (":italic");
6575 staticpro (&QCitalic);
6576 QCoverline = intern_c_string (":overline");
6577 staticpro (&QCoverline);
6578 QCstrike_through = intern_c_string (":strike-through");
6579 staticpro (&QCstrike_through);
6580 QCbox = intern_c_string (":box");
6581 staticpro (&QCbox);
6582 QCinherit = intern_c_string (":inherit");
6583 staticpro (&QCinherit);
6585 /* Symbols used for Lisp face attribute values. */
6586 QCcolor = intern_c_string (":color");
6587 staticpro (&QCcolor);
6588 QCline_width = intern_c_string (":line-width");
6589 staticpro (&QCline_width);
6590 QCstyle = intern_c_string (":style");
6591 staticpro (&QCstyle);
6592 Qreleased_button = intern_c_string ("released-button");
6593 staticpro (&Qreleased_button);
6594 Qpressed_button = intern_c_string ("pressed-button");
6595 staticpro (&Qpressed_button);
6596 Qnormal = intern_c_string ("normal");
6597 staticpro (&Qnormal);
6598 Qultra_light = intern_c_string ("ultra-light");
6599 staticpro (&Qultra_light);
6600 Qextra_light = intern_c_string ("extra-light");
6601 staticpro (&Qextra_light);
6602 Qlight = intern_c_string ("light");
6603 staticpro (&Qlight);
6604 Qsemi_light = intern_c_string ("semi-light");
6605 staticpro (&Qsemi_light);
6606 Qsemi_bold = intern_c_string ("semi-bold");
6607 staticpro (&Qsemi_bold);
6608 Qbold = intern_c_string ("bold");
6609 staticpro (&Qbold);
6610 Qextra_bold = intern_c_string ("extra-bold");
6611 staticpro (&Qextra_bold);
6612 Qultra_bold = intern_c_string ("ultra-bold");
6613 staticpro (&Qultra_bold);
6614 Qoblique = intern_c_string ("oblique");
6615 staticpro (&Qoblique);
6616 Qitalic = intern_c_string ("italic");
6617 staticpro (&Qitalic);
6618 Qreverse_oblique = intern_c_string ("reverse-oblique");
6619 staticpro (&Qreverse_oblique);
6620 Qreverse_italic = intern_c_string ("reverse-italic");
6621 staticpro (&Qreverse_italic);
6622 Qultra_condensed = intern_c_string ("ultra-condensed");
6623 staticpro (&Qultra_condensed);
6624 Qextra_condensed = intern_c_string ("extra-condensed");
6625 staticpro (&Qextra_condensed);
6626 Qcondensed = intern_c_string ("condensed");
6627 staticpro (&Qcondensed);
6628 Qsemi_condensed = intern_c_string ("semi-condensed");
6629 staticpro (&Qsemi_condensed);
6630 Qsemi_expanded = intern_c_string ("semi-expanded");
6631 staticpro (&Qsemi_expanded);
6632 Qexpanded = intern_c_string ("expanded");
6633 staticpro (&Qexpanded);
6634 Qextra_expanded = intern_c_string ("extra-expanded");
6635 staticpro (&Qextra_expanded);
6636 Qultra_expanded = intern_c_string ("ultra-expanded");
6637 staticpro (&Qultra_expanded);
6638 Qbackground_color = intern_c_string ("background-color");
6639 staticpro (&Qbackground_color);
6640 Qforeground_color = intern_c_string ("foreground-color");
6641 staticpro (&Qforeground_color);
6642 Qunspecified = intern_c_string ("unspecified");
6643 staticpro (&Qunspecified);
6644 Qignore_defface = intern_c_string (":ignore-defface");
6645 staticpro (&Qignore_defface);
6647 Qface_alias = intern_c_string ("face-alias");
6648 staticpro (&Qface_alias);
6649 Qdefault = intern_c_string ("default");
6650 staticpro (&Qdefault);
6651 Qtool_bar = intern_c_string ("tool-bar");
6652 staticpro (&Qtool_bar);
6653 Qregion = intern_c_string ("region");
6654 staticpro (&Qregion);
6655 Qfringe = intern_c_string ("fringe");
6656 staticpro (&Qfringe);
6657 Qheader_line = intern_c_string ("header-line");
6658 staticpro (&Qheader_line);
6659 Qscroll_bar = intern_c_string ("scroll-bar");
6660 staticpro (&Qscroll_bar);
6661 Qmenu = intern_c_string ("menu");
6662 staticpro (&Qmenu);
6663 Qcursor = intern_c_string ("cursor");
6664 staticpro (&Qcursor);
6665 Qborder = intern_c_string ("border");
6666 staticpro (&Qborder);
6667 Qmouse = intern_c_string ("mouse");
6668 staticpro (&Qmouse);
6669 Qmode_line_inactive = intern_c_string ("mode-line-inactive");
6670 staticpro (&Qmode_line_inactive);
6671 Qvertical_border = intern_c_string ("vertical-border");
6672 staticpro (&Qvertical_border);
6673 Qtty_color_desc = intern_c_string ("tty-color-desc");
6674 staticpro (&Qtty_color_desc);
6675 Qtty_color_standard_values = intern_c_string ("tty-color-standard-values");
6676 staticpro (&Qtty_color_standard_values);
6677 Qtty_color_by_index = intern_c_string ("tty-color-by-index");
6678 staticpro (&Qtty_color_by_index);
6679 Qtty_color_alist = intern_c_string ("tty-color-alist");
6680 staticpro (&Qtty_color_alist);
6681 Qscalable_fonts_allowed = intern_c_string ("scalable-fonts-allowed");
6682 staticpro (&Qscalable_fonts_allowed);
6684 Vparam_value_alist = Fcons (Fcons (Qnil, Qnil), Qnil);
6685 staticpro (&Vparam_value_alist);
6686 Vface_alternative_font_family_alist = Qnil;
6687 staticpro (&Vface_alternative_font_family_alist);
6688 Vface_alternative_font_registry_alist = Qnil;
6689 staticpro (&Vface_alternative_font_registry_alist);
6691 defsubr (&Sinternal_make_lisp_face);
6692 defsubr (&Sinternal_lisp_face_p);
6693 defsubr (&Sinternal_set_lisp_face_attribute);
6694 #ifdef HAVE_WINDOW_SYSTEM
6695 defsubr (&Sinternal_set_lisp_face_attribute_from_resource);
6696 #endif
6697 defsubr (&Scolor_gray_p);
6698 defsubr (&Scolor_supported_p);
6699 #ifndef HAVE_X_WINDOWS
6700 defsubr (&Sx_load_color_file);
6701 #endif
6702 defsubr (&Sface_attribute_relative_p);
6703 defsubr (&Smerge_face_attribute);
6704 defsubr (&Sinternal_get_lisp_face_attribute);
6705 defsubr (&Sinternal_lisp_face_attribute_values);
6706 defsubr (&Sinternal_lisp_face_equal_p);
6707 defsubr (&Sinternal_lisp_face_empty_p);
6708 defsubr (&Sinternal_copy_lisp_face);
6709 defsubr (&Sinternal_merge_in_global_face);
6710 defsubr (&Sface_font);
6711 defsubr (&Sframe_face_alist);
6712 defsubr (&Sdisplay_supports_face_attributes_p);
6713 defsubr (&Scolor_distance);
6714 defsubr (&Sinternal_set_font_selection_order);
6715 defsubr (&Sinternal_set_alternative_font_family_alist);
6716 defsubr (&Sinternal_set_alternative_font_registry_alist);
6717 defsubr (&Sface_attributes_as_vector);
6718 #if GLYPH_DEBUG
6719 defsubr (&Sdump_face);
6720 defsubr (&Sshow_face_resources);
6721 #endif /* GLYPH_DEBUG */
6722 defsubr (&Sclear_face_cache);
6723 defsubr (&Stty_suppress_bold_inverse_default_colors);
6725 #if defined DEBUG_X_COLORS && defined HAVE_X_WINDOWS
6726 defsubr (&Sdump_colors);
6727 #endif
6729 DEFVAR_LISP ("font-list-limit", &Vfont_list_limit,
6730 doc: /* *Limit for font matching.
6731 If an integer > 0, font matching functions won't load more than
6732 that number of fonts when searching for a matching font. */);
6733 Vfont_list_limit = make_number (DEFAULT_FONT_LIST_LIMIT);
6735 DEFVAR_LISP ("face-new-frame-defaults", &Vface_new_frame_defaults,
6736 doc: /* List of global face definitions (for internal use only.) */);
6737 Vface_new_frame_defaults = Qnil;
6739 DEFVAR_LISP ("face-default-stipple", &Vface_default_stipple,
6740 doc: /* *Default stipple pattern used on monochrome displays.
6741 This stipple pattern is used on monochrome displays
6742 instead of shades of gray for a face background color.
6743 See `set-face-stipple' for possible values for this variable. */);
6744 Vface_default_stipple = make_pure_c_string ("gray3");
6746 DEFVAR_LISP ("tty-defined-color-alist", &Vtty_defined_color_alist,
6747 doc: /* An alist of defined terminal colors and their RGB values. */);
6748 Vtty_defined_color_alist = Qnil;
6750 DEFVAR_LISP ("scalable-fonts-allowed", &Vscalable_fonts_allowed,
6751 doc: /* Allowed scalable fonts.
6752 A value of nil means don't allow any scalable fonts.
6753 A value of t means allow any scalable font.
6754 Otherwise, value must be a list of regular expressions. A font may be
6755 scaled if its name matches a regular expression in the list.
6756 Note that if value is nil, a scalable font might still be used, if no
6757 other font of the appropriate family and registry is available. */);
6758 Vscalable_fonts_allowed = Qnil;
6760 DEFVAR_LISP ("face-ignored-fonts", &Vface_ignored_fonts,
6761 doc: /* List of ignored fonts.
6762 Each element is a regular expression that matches names of fonts to
6763 ignore. */);
6764 Vface_ignored_fonts = Qnil;
6766 DEFVAR_LISP ("face-remapping-alist", &Vface_remapping_alist,
6767 doc: /* Alist of face remappings.
6768 Each element is of the form:
6770 (FACE REPLACEMENT...),
6772 which causes display of the face FACE to use REPLACEMENT... instead.
6773 REPLACEMENT... is interpreted the same way as the value of a `face'
6774 text property: it may be (1) A face name, (2) A list of face names,
6775 (3) A property-list of face attribute/value pairs, or (4) A list of
6776 face names or lists containing face attribute/value pairs.
6778 Multiple entries in REPLACEMENT... are merged together to form the final
6779 result, with faces or attributes earlier in the list taking precedence
6780 over those that are later.
6782 Face-name remapping cycles are suppressed; recursive references use the
6783 underlying face instead of the remapped face. So a remapping of the form:
6785 (FACE EXTRA-FACE... FACE)
6789 (FACE (FACE-ATTR VAL ...) FACE)
6791 will cause EXTRA-FACE... or (FACE-ATTR VAL ...) to be _merged_ with the
6792 existing definition of FACE. Note that for the default face, this isn't
6793 necessary, as every face inherits from the default face.
6795 Making this variable buffer-local is a good way to allow buffer-specific
6796 face definitions. For instance, the mode my-mode could define a face
6797 `my-mode-default', and then in the mode setup function, do:
6799 (set (make-local-variable 'face-remapping-alist)
6800 '((default my-mode-default)))).
6802 Because Emacs normally only redraws screen areas when the underlying
6803 buffer contents change, you may need to call `redraw-display' after
6804 changing this variable for it to take effect. */);
6805 Vface_remapping_alist = Qnil;
6807 DEFVAR_LISP ("face-font-rescale-alist", &Vface_font_rescale_alist,
6808 doc: /* Alist of fonts vs the rescaling factors.
6809 Each element is a cons (FONT-PATTERN . RESCALE-RATIO), where
6810 FONT-PATTERN is a font-spec or a regular expression matching a font name, and
6811 RESCALE-RATIO is a floating point number to specify how much larger
6812 \(or smaller) font we should use. For instance, if a face requests
6813 a font of 10 point, we actually use a font of 10 * RESCALE-RATIO point. */);
6814 Vface_font_rescale_alist = Qnil;
6816 #ifdef HAVE_WINDOW_SYSTEM
6817 defsubr (&Sbitmap_spec_p);
6818 defsubr (&Sx_list_fonts);
6819 defsubr (&Sinternal_face_x_get_resource);
6820 defsubr (&Sx_family_fonts);
6821 #endif
6824 /* arch-tag: 8a0f7598-5517-408d-9ab3-1da6fcd4c749
6825 (do not change this comment) */