* process.c (allocate_pty) [PTY_OPEN]: Set fd's FD_CLOEXEC flag.
[emacs.git] / src / xfaces.c
blobb76f9d24180d0d053e78297ed013351aad512744
1 /* xfaces.c -- "Face" primitives.
3 Copyright (C) 1993-1994, 1998-2013 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 foundry 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 corresponding 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 the 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 "sysstdio.h"
204 #include <sys/types.h>
205 #include <sys/stat.h>
207 #include "lisp.h"
208 #include "character.h"
209 #include "charset.h"
210 #include "keyboard.h"
211 #include "frame.h"
212 #include "termhooks.h"
214 #ifdef HAVE_X_WINDOWS
215 #include "xterm.h"
216 #ifdef USE_MOTIF
217 #include <Xm/Xm.h>
218 #include <Xm/XmStrDefs.h>
219 #endif /* USE_MOTIF */
220 #endif /* HAVE_X_WINDOWS */
222 #ifdef MSDOS
223 #include "dosfns.h"
224 #endif
226 #ifdef HAVE_WINDOW_SYSTEM
227 #include TERM_HEADER
228 #include "fontset.h"
229 #ifdef HAVE_NTGUI
230 #undef FRAME_X_DISPLAY_INFO
231 #define FRAME_X_DISPLAY_INFO FRAME_W32_DISPLAY_INFO
232 #define x_display_info w32_display_info
233 #define GCGraphicsExposures 0
234 #endif /* HAVE_NTGUI */
236 #ifdef HAVE_NS
237 #undef FRAME_X_DISPLAY_INFO
238 #define FRAME_X_DISPLAY_INFO FRAME_NS_DISPLAY_INFO
239 #define x_display_info ns_display_info
240 #define GCGraphicsExposures 0
241 #endif /* HAVE_NS */
242 #endif /* HAVE_WINDOW_SYSTEM */
244 #include "buffer.h"
245 #include "dispextern.h"
246 #include "blockinput.h"
247 #include "window.h"
248 #include "intervals.h"
249 #include "termchar.h"
251 #include "font.h"
253 #ifdef HAVE_X_WINDOWS
255 /* Compensate for a bug in Xos.h on some systems, on which it requires
256 time.h. On some such systems, Xos.h tries to redefine struct
257 timeval and struct timezone if USG is #defined while it is
258 #included. */
260 #ifdef XOS_NEEDS_TIME_H
261 #include <time.h>
262 #undef USG
263 #include <X11/Xos.h>
264 #define USG
265 #define __TIMEVAL__
266 #if defined USG || defined __TIMEVAL__ /* Don't warn about unused macros. */
267 #endif
268 #else /* not XOS_NEEDS_TIME_H */
269 #include <X11/Xos.h>
270 #endif /* not XOS_NEEDS_TIME_H */
272 #endif /* HAVE_X_WINDOWS */
274 #include <c-ctype.h>
276 /* Number of pt per inch (from the TeXbook). */
278 #define PT_PER_INCH 72.27
280 /* Non-zero if face attribute ATTR is unspecified. */
282 #define UNSPECIFIEDP(ATTR) EQ ((ATTR), Qunspecified)
284 /* Non-zero if face attribute ATTR is `ignore-defface'. */
286 #define IGNORE_DEFFACE_P(ATTR) EQ ((ATTR), QCignore_defface)
288 /* Value is the number of elements of VECTOR. */
290 #define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
292 /* Size of hash table of realized faces in face caches (should be a
293 prime number). */
295 #define FACE_CACHE_BUCKETS_SIZE 1001
297 /* Keyword symbols used for face attribute names. */
299 Lisp_Object QCfamily, QCheight, QCweight, QCslant;
300 static Lisp_Object QCunderline;
301 static Lisp_Object QCinverse_video, QCstipple;
302 Lisp_Object QCforeground, QCbackground;
303 Lisp_Object QCwidth;
304 static Lisp_Object QCfont, QCbold, QCitalic;
305 static Lisp_Object QCreverse_video;
306 static Lisp_Object QCoverline, QCstrike_through, QCbox, QCinherit;
307 static Lisp_Object QCfontset;
309 /* Symbols used for attribute values. */
311 Lisp_Object Qnormal;
312 Lisp_Object Qbold;
313 static Lisp_Object Qline, Qwave;
314 Lisp_Object Qextra_light, Qlight;
315 Lisp_Object Qsemi_light, Qsemi_bold, Qextra_bold, Qultra_bold;
316 Lisp_Object Qoblique;
317 Lisp_Object Qitalic;
318 static Lisp_Object Qreleased_button, Qpressed_button;
319 static Lisp_Object QCstyle, QCcolor, QCline_width;
320 Lisp_Object Qunspecified; /* used in dosfns.c */
321 static Lisp_Object QCignore_defface;
323 char unspecified_fg[] = "unspecified-fg", unspecified_bg[] = "unspecified-bg";
325 /* The name of the function to call when the background of the frame
326 has changed, frame_set_background_mode. */
328 static Lisp_Object Qframe_set_background_mode;
330 /* Names of basic faces. */
332 Lisp_Object Qdefault, Qtool_bar, Qfringe;
333 static Lisp_Object Qregion;
334 Lisp_Object Qheader_line, Qscroll_bar, Qcursor;
335 static Lisp_Object Qborder, Qmouse, Qmenu;
336 Lisp_Object Qmode_line_inactive;
337 static Lisp_Object Qvertical_border;
339 /* The symbol `face-alias'. A symbols having that property is an
340 alias for another face. Value of the property is the name of
341 the aliased face. */
343 static Lisp_Object Qface_alias;
345 /* Alist of alternative font families. Each element is of the form
346 (FAMILY FAMILY1 FAMILY2 ...). If fonts of FAMILY can't be loaded,
347 try FAMILY1, then FAMILY2, ... */
349 Lisp_Object Vface_alternative_font_family_alist;
351 /* Alist of alternative font registries. Each element is of the form
352 (REGISTRY REGISTRY1 REGISTRY2...). If fonts of REGISTRY can't be
353 loaded, try REGISTRY1, then REGISTRY2, ... */
355 Lisp_Object Vface_alternative_font_registry_alist;
357 /* Allowed scalable fonts. A value of nil means don't allow any
358 scalable fonts. A value of t means allow the use of any scalable
359 font. Otherwise, value must be a list of regular expressions. A
360 font may be scaled if its name matches a regular expression in the
361 list. */
363 static Lisp_Object Qscalable_fonts_allowed;
365 /* The symbols `foreground-color' and `background-color' which can be
366 used as part of a `face' property. This is for compatibility with
367 Emacs 20.2. */
369 Lisp_Object Qforeground_color, Qbackground_color;
371 /* The symbols `face' and `mouse-face' used as text properties. */
373 Lisp_Object Qface;
375 /* Property for basic faces which other faces cannot inherit. */
377 static Lisp_Object Qface_no_inherit;
379 /* Error symbol for wrong_type_argument in load_pixmap. */
381 static Lisp_Object Qbitmap_spec_p;
383 /* The next ID to assign to Lisp faces. */
385 static int next_lface_id;
387 /* A vector mapping Lisp face Id's to face names. */
389 static Lisp_Object *lface_id_to_name;
390 static ptrdiff_t lface_id_to_name_size;
392 /* TTY color-related functions (defined in tty-colors.el). */
394 static Lisp_Object Qtty_color_desc, Qtty_color_by_index, Qtty_color_standard_values;
396 /* The name of the function used to compute colors on TTYs. */
398 static Lisp_Object Qtty_color_alist;
400 /* Counter for calls to clear_face_cache. If this counter reaches
401 CLEAR_FONT_TABLE_COUNT, and a frame has more than
402 CLEAR_FONT_TABLE_NFONTS load, unused fonts are freed. */
404 static int clear_font_table_count;
405 #define CLEAR_FONT_TABLE_COUNT 100
406 #define CLEAR_FONT_TABLE_NFONTS 10
408 /* Non-zero means face attributes have been changed since the last
409 redisplay. Used in redisplay_internal. */
411 int face_change_count;
413 /* Non-zero means don't display bold text if a face's foreground
414 and background colors are the inverse of the default colors of the
415 display. This is a kluge to suppress `bold black' foreground text
416 which is hard to read on an LCD monitor. */
418 static int tty_suppress_bold_inverse_default_colors_p;
420 /* A list of the form `((x . y))' used to avoid consing in
421 Finternal_set_lisp_face_attribute. */
423 static Lisp_Object Vparam_value_alist;
425 /* The total number of colors currently allocated. */
427 #ifdef GLYPH_DEBUG
428 static int ncolors_allocated;
429 static int npixmaps_allocated;
430 static int ngcs;
431 #endif
433 /* Non-zero means the definition of the `menu' face for new frames has
434 been changed. */
436 static int menu_face_changed_default;
439 /* Function prototypes. */
441 struct table_entry;
442 struct named_merge_point;
444 static void set_font_frame_param (Lisp_Object, Lisp_Object);
445 static struct face *realize_face (struct face_cache *, Lisp_Object *,
446 int);
447 static struct face *realize_non_ascii_face (struct frame *, Lisp_Object,
448 struct face *);
449 static struct face *realize_x_face (struct face_cache *, Lisp_Object *);
450 static struct face *realize_tty_face (struct face_cache *, Lisp_Object *);
451 static bool realize_basic_faces (struct frame *);
452 static bool realize_default_face (struct frame *);
453 static void realize_named_face (struct frame *, Lisp_Object, int);
454 static struct face_cache *make_face_cache (struct frame *);
455 static void clear_face_gcs (struct face_cache *);
456 static void free_face_cache (struct face_cache *);
457 static int merge_face_ref (struct frame *, Lisp_Object, Lisp_Object *,
458 int, struct named_merge_point *);
461 /***********************************************************************
462 Utilities
463 ***********************************************************************/
465 #ifdef HAVE_X_WINDOWS
467 #ifdef DEBUG_X_COLORS
469 /* The following is a poor mans infrastructure for debugging X color
470 allocation problems on displays with PseudoColor-8. Some X servers
471 like 3.3.5 XF86_SVGA with Matrox cards apparently don't implement
472 color reference counts completely so that they don't signal an
473 error when a color is freed whose reference count is already 0.
474 Other X servers do. To help me debug this, the following code
475 implements a simple reference counting schema of its own, for a
476 single display/screen. --gerd. */
478 /* Reference counts for pixel colors. */
480 int color_count[256];
482 /* Register color PIXEL as allocated. */
484 void
485 register_color (unsigned long pixel)
487 eassert (pixel < 256);
488 ++color_count[pixel];
492 /* Register color PIXEL as deallocated. */
494 void
495 unregister_color (unsigned long pixel)
497 eassert (pixel < 256);
498 if (color_count[pixel] > 0)
499 --color_count[pixel];
500 else
501 emacs_abort ();
505 /* Register N colors from PIXELS as deallocated. */
507 void
508 unregister_colors (unsigned long *pixels, int n)
510 int i;
511 for (i = 0; i < n; ++i)
512 unregister_color (pixels[i]);
516 DEFUN ("dump-colors", Fdump_colors, Sdump_colors, 0, 0, 0,
517 doc: /* Dump currently allocated colors to stderr. */)
518 (void)
520 int i, n;
522 fputc ('\n', stderr);
524 for (i = n = 0; i < sizeof color_count / sizeof color_count[0]; ++i)
525 if (color_count[i])
527 fprintf (stderr, "%3d: %5d", i, color_count[i]);
528 ++n;
529 if (n % 5 == 0)
530 fputc ('\n', stderr);
531 else
532 fputc ('\t', stderr);
535 if (n % 5 != 0)
536 fputc ('\n', stderr);
537 return Qnil;
540 #endif /* DEBUG_X_COLORS */
543 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
544 color values. Interrupt input must be blocked when this function
545 is called. */
547 void
548 x_free_colors (struct frame *f, long unsigned int *pixels, int npixels)
550 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
552 /* If display has an immutable color map, freeing colors is not
553 necessary and some servers don't allow it. So don't do it. */
554 if (class != StaticColor && class != StaticGray && class != TrueColor)
556 #ifdef DEBUG_X_COLORS
557 unregister_colors (pixels, npixels);
558 #endif
559 XFreeColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
560 pixels, npixels, 0);
565 #ifdef USE_X_TOOLKIT
567 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
568 color values. Interrupt input must be blocked when this function
569 is called. */
571 void
572 x_free_dpy_colors (Display *dpy, Screen *screen, Colormap cmap,
573 long unsigned int *pixels, int npixels)
575 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
576 int class = dpyinfo->visual->class;
578 /* If display has an immutable color map, freeing colors is not
579 necessary and some servers don't allow it. So don't do it. */
580 if (class != StaticColor && class != StaticGray && class != TrueColor)
582 #ifdef DEBUG_X_COLORS
583 unregister_colors (pixels, npixels);
584 #endif
585 XFreeColors (dpy, cmap, pixels, npixels, 0);
588 #endif /* USE_X_TOOLKIT */
590 /* Create and return a GC for use on frame F. GC values and mask
591 are given by XGCV and MASK. */
593 static GC
594 x_create_gc (struct frame *f, long unsigned int mask, XGCValues *xgcv)
596 GC gc;
597 block_input ();
598 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), mask, xgcv);
599 unblock_input ();
600 IF_DEBUG (++ngcs);
601 return gc;
605 /* Free GC which was used on frame F. */
607 static void
608 x_free_gc (struct frame *f, GC gc)
610 eassert (input_blocked_p ());
611 IF_DEBUG (eassert (--ngcs >= 0));
612 XFreeGC (FRAME_X_DISPLAY (f), gc);
615 #endif /* HAVE_X_WINDOWS */
617 #ifdef HAVE_NTGUI
618 /* W32 emulation of GCs */
620 static GC
621 x_create_gc (struct frame *f, unsigned long mask, XGCValues *xgcv)
623 GC gc;
624 block_input ();
625 gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, xgcv);
626 unblock_input ();
627 IF_DEBUG (++ngcs);
628 return gc;
632 /* Free GC which was used on frame F. */
634 static void
635 x_free_gc (struct frame *f, GC gc)
637 IF_DEBUG (eassert (--ngcs >= 0));
638 xfree (gc);
641 #endif /* HAVE_NTGUI */
643 #ifdef HAVE_NS
644 /* NS emulation of GCs */
646 static GC
647 x_create_gc (struct frame *f,
648 unsigned long mask,
649 XGCValues *xgcv)
651 GC gc = xmalloc (sizeof *gc);
652 *gc = *xgcv;
653 return gc;
656 static void
657 x_free_gc (struct frame *f, GC gc)
659 xfree (gc);
661 #endif /* HAVE_NS */
663 /***********************************************************************
664 Frames and faces
665 ***********************************************************************/
667 /* Initialize face cache and basic faces for frame F. */
669 void
670 init_frame_faces (struct frame *f)
672 /* Make a face cache, if F doesn't have one. */
673 if (FRAME_FACE_CACHE (f) == NULL)
674 FRAME_FACE_CACHE (f) = make_face_cache (f);
676 #ifdef HAVE_WINDOW_SYSTEM
677 /* Make the image cache. */
678 if (FRAME_WINDOW_P (f))
680 /* We initialize the image cache when creating the first frame
681 on a terminal, and not during terminal creation. This way,
682 `x-open-connection' on a tty won't create an image cache. */
683 if (FRAME_IMAGE_CACHE (f) == NULL)
684 FRAME_IMAGE_CACHE (f) = make_image_cache ();
685 ++FRAME_IMAGE_CACHE (f)->refcount;
687 #endif /* HAVE_WINDOW_SYSTEM */
689 /* Realize basic faces. Must have enough information in frame
690 parameters to realize basic faces at this point. */
691 #ifdef HAVE_X_WINDOWS
692 if (!FRAME_X_P (f) || FRAME_X_WINDOW (f))
693 #endif
694 #ifdef HAVE_NTGUI
695 if (!FRAME_WINDOW_P (f) || FRAME_W32_WINDOW (f))
696 #endif
697 #ifdef HAVE_NS
698 if (!FRAME_NS_P (f) || FRAME_NS_WINDOW (f))
699 #endif
700 if (!realize_basic_faces (f))
701 emacs_abort ();
705 /* Free face cache of frame F. Called from delete_frame. */
707 void
708 free_frame_faces (struct frame *f)
710 struct face_cache *face_cache = FRAME_FACE_CACHE (f);
712 if (face_cache)
714 free_face_cache (face_cache);
715 FRAME_FACE_CACHE (f) = NULL;
718 #ifdef HAVE_WINDOW_SYSTEM
719 if (FRAME_WINDOW_P (f))
721 struct image_cache *image_cache = FRAME_IMAGE_CACHE (f);
722 if (image_cache)
724 --image_cache->refcount;
725 if (image_cache->refcount == 0)
726 free_image_cache (f);
729 #endif /* HAVE_WINDOW_SYSTEM */
733 /* Clear face caches, and recompute basic faces for frame F. Call
734 this after changing frame parameters on which those faces depend,
735 or when realized faces have been freed due to changing attributes
736 of named faces. */
738 void
739 recompute_basic_faces (struct frame *f)
741 if (FRAME_FACE_CACHE (f))
743 clear_face_cache (0);
744 if (!realize_basic_faces (f))
745 emacs_abort ();
750 /* Clear the face caches of all frames. CLEAR_FONTS_P non-zero means
751 try to free unused fonts, too. */
753 void
754 clear_face_cache (int clear_fonts_p)
756 #ifdef HAVE_WINDOW_SYSTEM
757 Lisp_Object tail, frame;
759 if (clear_fonts_p
760 || ++clear_font_table_count == CLEAR_FONT_TABLE_COUNT)
762 /* From time to time see if we can unload some fonts. This also
763 frees all realized faces on all frames. Fonts needed by
764 faces will be loaded again when faces are realized again. */
765 clear_font_table_count = 0;
767 FOR_EACH_FRAME (tail, frame)
769 struct frame *f = XFRAME (frame);
770 if (FRAME_WINDOW_P (f)
771 && FRAME_X_DISPLAY_INFO (f)->n_fonts > CLEAR_FONT_TABLE_NFONTS)
773 clear_font_cache (f);
774 free_all_realized_faces (frame);
778 else
780 /* Clear GCs of realized faces. */
781 FOR_EACH_FRAME (tail, frame)
783 struct frame *f = XFRAME (frame);
784 if (FRAME_WINDOW_P (f))
785 clear_face_gcs (FRAME_FACE_CACHE (f));
787 clear_image_caches (Qnil);
789 #endif /* HAVE_WINDOW_SYSTEM */
793 DEFUN ("clear-face-cache", Fclear_face_cache, Sclear_face_cache, 0, 1, 0,
794 doc: /* Clear face caches on all frames.
795 Optional THOROUGHLY non-nil means try to free unused fonts, too. */)
796 (Lisp_Object thoroughly)
798 clear_face_cache (!NILP (thoroughly));
799 ++face_change_count;
800 ++windows_or_buffers_changed;
801 return Qnil;
805 /***********************************************************************
806 X Pixmaps
807 ***********************************************************************/
809 #ifdef HAVE_WINDOW_SYSTEM
811 DEFUN ("bitmap-spec-p", Fbitmap_spec_p, Sbitmap_spec_p, 1, 1, 0,
812 doc: /* Value is non-nil if OBJECT is a valid bitmap specification.
813 A bitmap specification is either a string, a file name, or a list
814 \(WIDTH HEIGHT DATA) where WIDTH is the pixel width of the bitmap,
815 HEIGHT is its height, and DATA is a string containing the bits of
816 the pixmap. Bits are stored row by row, each row occupies
817 \(WIDTH + 7)/8 bytes. */)
818 (Lisp_Object object)
820 bool pixmap_p = 0;
822 if (STRINGP (object))
823 /* If OBJECT is a string, it's a file name. */
824 pixmap_p = 1;
825 else if (CONSP (object))
827 /* Otherwise OBJECT must be (WIDTH HEIGHT DATA), WIDTH and
828 HEIGHT must be ints > 0, and DATA must be string large
829 enough to hold a bitmap of the specified size. */
830 Lisp_Object width, height, data;
832 height = width = data = Qnil;
834 if (CONSP (object))
836 width = XCAR (object);
837 object = XCDR (object);
838 if (CONSP (object))
840 height = XCAR (object);
841 object = XCDR (object);
842 if (CONSP (object))
843 data = XCAR (object);
847 if (STRINGP (data)
848 && RANGED_INTEGERP (1, width, INT_MAX)
849 && RANGED_INTEGERP (1, height, INT_MAX))
851 int bytes_per_row = ((XINT (width) + BITS_PER_CHAR - 1)
852 / BITS_PER_CHAR);
853 if (XINT (height) <= SBYTES (data) / bytes_per_row)
854 pixmap_p = 1;
858 return pixmap_p ? Qt : Qnil;
862 /* Load a bitmap according to NAME (which is either a file name or a
863 pixmap spec) for use on frame F. Value is the bitmap_id (see
864 xfns.c). If NAME is nil, return with a bitmap id of zero. If
865 bitmap cannot be loaded, display a message saying so, and return
866 zero. Store the bitmap width in *W_PTR and its height in *H_PTR,
867 if these pointers are not null. */
869 static ptrdiff_t
870 load_pixmap (struct frame *f, Lisp_Object name, unsigned int *w_ptr,
871 unsigned int *h_ptr)
873 ptrdiff_t bitmap_id;
875 if (NILP (name))
876 return 0;
878 CHECK_TYPE (!NILP (Fbitmap_spec_p (name)), Qbitmap_spec_p, name);
880 block_input ();
881 if (CONSP (name))
883 /* Decode a bitmap spec into a bitmap. */
885 int h, w;
886 Lisp_Object bits;
888 w = XINT (Fcar (name));
889 h = XINT (Fcar (Fcdr (name)));
890 bits = Fcar (Fcdr (Fcdr (name)));
892 bitmap_id = x_create_bitmap_from_data (f, SSDATA (bits),
893 w, h);
895 else
897 /* It must be a string -- a file name. */
898 bitmap_id = x_create_bitmap_from_file (f, name);
900 unblock_input ();
902 if (bitmap_id < 0)
904 add_to_log ("Invalid or undefined bitmap `%s'", name, Qnil);
905 bitmap_id = 0;
907 if (w_ptr)
908 *w_ptr = 0;
909 if (h_ptr)
910 *h_ptr = 0;
912 else
914 #ifdef GLYPH_DEBUG
915 ++npixmaps_allocated;
916 #endif
917 if (w_ptr)
918 *w_ptr = x_bitmap_width (f, bitmap_id);
920 if (h_ptr)
921 *h_ptr = x_bitmap_height (f, bitmap_id);
924 return bitmap_id;
927 #endif /* HAVE_WINDOW_SYSTEM */
931 /***********************************************************************
932 X Colors
933 ***********************************************************************/
935 /* Parse RGB_LIST, and fill in the RGB fields of COLOR.
936 RGB_LIST should contain (at least) 3 lisp integers.
937 Return 0 if there's a problem with RGB_LIST, otherwise return 1. */
939 static int
940 parse_rgb_list (Lisp_Object rgb_list, XColor *color)
942 #define PARSE_RGB_LIST_FIELD(field) \
943 if (CONSP (rgb_list) && INTEGERP (XCAR (rgb_list))) \
945 color->field = XINT (XCAR (rgb_list)); \
946 rgb_list = XCDR (rgb_list); \
948 else \
949 return 0;
951 PARSE_RGB_LIST_FIELD (red);
952 PARSE_RGB_LIST_FIELD (green);
953 PARSE_RGB_LIST_FIELD (blue);
955 return 1;
959 /* Lookup on frame F the color described by the lisp string COLOR.
960 The resulting tty color is returned in TTY_COLOR; if STD_COLOR is
961 non-zero, then the `standard' definition of the same color is
962 returned in it. */
964 static bool
965 tty_lookup_color (struct frame *f, Lisp_Object color, XColor *tty_color,
966 XColor *std_color)
968 Lisp_Object frame, color_desc;
970 if (!STRINGP (color) || NILP (Ffboundp (Qtty_color_desc)))
971 return 0;
973 XSETFRAME (frame, f);
975 color_desc = call2 (Qtty_color_desc, color, frame);
976 if (CONSP (color_desc) && CONSP (XCDR (color_desc)))
978 Lisp_Object rgb;
980 if (! INTEGERP (XCAR (XCDR (color_desc))))
981 return 0;
983 tty_color->pixel = XINT (XCAR (XCDR (color_desc)));
985 rgb = XCDR (XCDR (color_desc));
986 if (! parse_rgb_list (rgb, tty_color))
987 return 0;
989 /* Should we fill in STD_COLOR too? */
990 if (std_color)
992 /* Default STD_COLOR to the same as TTY_COLOR. */
993 *std_color = *tty_color;
995 /* Do a quick check to see if the returned descriptor is
996 actually _exactly_ equal to COLOR, otherwise we have to
997 lookup STD_COLOR separately. If it's impossible to lookup
998 a standard color, we just give up and use TTY_COLOR. */
999 if ((!STRINGP (XCAR (color_desc))
1000 || NILP (Fstring_equal (color, XCAR (color_desc))))
1001 && !NILP (Ffboundp (Qtty_color_standard_values)))
1003 /* Look up STD_COLOR separately. */
1004 rgb = call1 (Qtty_color_standard_values, color);
1005 if (! parse_rgb_list (rgb, std_color))
1006 return 0;
1010 return 1;
1012 else if (NILP (Fsymbol_value (intern ("tty-defined-color-alist"))))
1013 /* We were called early during startup, and the colors are not
1014 yet set up in tty-defined-color-alist. Don't return a failure
1015 indication, since this produces the annoying "Unable to
1016 load color" messages in the *Messages* buffer. */
1017 return 1;
1018 else
1019 /* tty-color-desc seems to have returned a bad value. */
1020 return 0;
1023 /* A version of defined_color for non-X frames. */
1025 static bool
1026 tty_defined_color (struct frame *f, const char *color_name,
1027 XColor *color_def, bool alloc)
1029 bool status = 1;
1031 /* Defaults. */
1032 color_def->pixel = FACE_TTY_DEFAULT_COLOR;
1033 color_def->red = 0;
1034 color_def->blue = 0;
1035 color_def->green = 0;
1037 if (*color_name)
1038 status = tty_lookup_color (f, build_string (color_name), color_def, NULL);
1040 if (color_def->pixel == FACE_TTY_DEFAULT_COLOR && *color_name)
1042 if (strcmp (color_name, "unspecified-fg") == 0)
1043 color_def->pixel = FACE_TTY_DEFAULT_FG_COLOR;
1044 else if (strcmp (color_name, "unspecified-bg") == 0)
1045 color_def->pixel = FACE_TTY_DEFAULT_BG_COLOR;
1048 if (color_def->pixel != FACE_TTY_DEFAULT_COLOR)
1049 status = 1;
1051 return status;
1055 /* Decide if color named COLOR_NAME is valid for the display
1056 associated with the frame F; if so, return the rgb values in
1057 COLOR_DEF. If ALLOC, allocate a new colormap cell.
1059 This does the right thing for any type of frame. */
1061 static bool
1062 defined_color (struct frame *f, const char *color_name, XColor *color_def,
1063 bool alloc)
1065 if (!FRAME_WINDOW_P (f))
1066 return tty_defined_color (f, color_name, color_def, alloc);
1067 #ifdef HAVE_X_WINDOWS
1068 else if (FRAME_X_P (f))
1069 return x_defined_color (f, color_name, color_def, alloc);
1070 #endif
1071 #ifdef HAVE_NTGUI
1072 else if (FRAME_W32_P (f))
1073 return w32_defined_color (f, color_name, color_def, alloc);
1074 #endif
1075 #ifdef HAVE_NS
1076 else if (FRAME_NS_P (f))
1077 return ns_defined_color (f, color_name, color_def, alloc, 1);
1078 #endif
1079 else
1080 emacs_abort ();
1084 /* Given the index IDX of a tty color on frame F, return its name, a
1085 Lisp string. */
1087 Lisp_Object
1088 tty_color_name (struct frame *f, int idx)
1090 if (idx >= 0 && !NILP (Ffboundp (Qtty_color_by_index)))
1092 Lisp_Object frame;
1093 Lisp_Object coldesc;
1095 XSETFRAME (frame, f);
1096 coldesc = call2 (Qtty_color_by_index, make_number (idx), frame);
1098 if (!NILP (coldesc))
1099 return XCAR (coldesc);
1101 #ifdef MSDOS
1102 /* We can have an MSDOG frame under -nw for a short window of
1103 opportunity before internal_terminal_init is called. DTRT. */
1104 if (FRAME_MSDOS_P (f) && !inhibit_window_system)
1105 return msdos_stdcolor_name (idx);
1106 #endif
1108 if (idx == FACE_TTY_DEFAULT_FG_COLOR)
1109 return build_string (unspecified_fg);
1110 if (idx == FACE_TTY_DEFAULT_BG_COLOR)
1111 return build_string (unspecified_bg);
1113 return Qunspecified;
1117 /* Return non-zero if COLOR_NAME is a shade of gray (or white or
1118 black) on frame F.
1120 The criterion implemented here is not a terribly sophisticated one. */
1122 static int
1123 face_color_gray_p (struct frame *f, const char *color_name)
1125 XColor color;
1126 int gray_p;
1128 if (defined_color (f, color_name, &color, 0))
1129 gray_p = (/* Any color sufficiently close to black counts as gray. */
1130 (color.red < 5000 && color.green < 5000 && color.blue < 5000)
1132 ((eabs (color.red - color.green)
1133 < max (color.red, color.green) / 20)
1134 && (eabs (color.green - color.blue)
1135 < max (color.green, color.blue) / 20)
1136 && (eabs (color.blue - color.red)
1137 < max (color.blue, color.red) / 20)));
1138 else
1139 gray_p = 0;
1141 return gray_p;
1145 /* Return non-zero if color COLOR_NAME can be displayed on frame F.
1146 BACKGROUND_P non-zero means the color will be used as background
1147 color. */
1149 static int
1150 face_color_supported_p (struct frame *f, const char *color_name,
1151 int background_p)
1153 Lisp_Object frame;
1154 XColor not_used;
1156 XSETFRAME (frame, f);
1157 return
1158 #ifdef HAVE_WINDOW_SYSTEM
1159 FRAME_WINDOW_P (f)
1160 ? (!NILP (Fxw_display_color_p (frame))
1161 || xstrcasecmp (color_name, "black") == 0
1162 || xstrcasecmp (color_name, "white") == 0
1163 || (background_p
1164 && face_color_gray_p (f, color_name))
1165 || (!NILP (Fx_display_grayscale_p (frame))
1166 && face_color_gray_p (f, color_name)))
1168 #endif
1169 tty_defined_color (f, color_name, &not_used, 0);
1173 DEFUN ("color-gray-p", Fcolor_gray_p, Scolor_gray_p, 1, 2, 0,
1174 doc: /* Return non-nil if COLOR is a shade of gray (or white or black).
1175 FRAME specifies the frame and thus the display for interpreting COLOR.
1176 If FRAME is nil or omitted, use the selected frame. */)
1177 (Lisp_Object color, Lisp_Object frame)
1179 CHECK_STRING (color);
1180 return (face_color_gray_p (decode_any_frame (frame), SSDATA (color))
1181 ? Qt : Qnil);
1185 DEFUN ("color-supported-p", Fcolor_supported_p,
1186 Scolor_supported_p, 1, 3, 0,
1187 doc: /* Return non-nil if COLOR can be displayed on FRAME.
1188 BACKGROUND-P non-nil means COLOR is used as a background.
1189 Otherwise, this function tells whether it can be used as a foreground.
1190 If FRAME is nil or omitted, use the selected frame.
1191 COLOR must be a valid color name. */)
1192 (Lisp_Object color, Lisp_Object frame, Lisp_Object background_p)
1194 CHECK_STRING (color);
1195 return (face_color_supported_p (decode_any_frame (frame),
1196 SSDATA (color), !NILP (background_p))
1197 ? Qt : Qnil);
1201 /* Load color with name NAME for use by face FACE on frame F.
1202 TARGET_INDEX must be one of LFACE_FOREGROUND_INDEX,
1203 LFACE_BACKGROUND_INDEX, LFACE_UNDERLINE_INDEX, LFACE_OVERLINE_INDEX,
1204 LFACE_STRIKE_THROUGH_INDEX, or LFACE_BOX_INDEX. Value is the
1205 pixel color. If color cannot be loaded, display a message, and
1206 return the foreground, background or underline color of F, but
1207 record that fact in flags of the face so that we don't try to free
1208 these colors. */
1210 unsigned long
1211 load_color (struct frame *f, struct face *face, Lisp_Object name,
1212 enum lface_attribute_index target_index)
1214 XColor color;
1216 eassert (STRINGP (name));
1217 eassert (target_index == LFACE_FOREGROUND_INDEX
1218 || target_index == LFACE_BACKGROUND_INDEX
1219 || target_index == LFACE_UNDERLINE_INDEX
1220 || target_index == LFACE_OVERLINE_INDEX
1221 || target_index == LFACE_STRIKE_THROUGH_INDEX
1222 || target_index == LFACE_BOX_INDEX);
1224 /* if the color map is full, defined_color will return a best match
1225 to the values in an existing cell. */
1226 if (!defined_color (f, SSDATA (name), &color, 1))
1228 add_to_log ("Unable to load color \"%s\"", name, Qnil);
1230 switch (target_index)
1232 case LFACE_FOREGROUND_INDEX:
1233 face->foreground_defaulted_p = 1;
1234 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1235 break;
1237 case LFACE_BACKGROUND_INDEX:
1238 face->background_defaulted_p = 1;
1239 color.pixel = FRAME_BACKGROUND_PIXEL (f);
1240 break;
1242 case LFACE_UNDERLINE_INDEX:
1243 face->underline_defaulted_p = 1;
1244 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1245 break;
1247 case LFACE_OVERLINE_INDEX:
1248 face->overline_color_defaulted_p = 1;
1249 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1250 break;
1252 case LFACE_STRIKE_THROUGH_INDEX:
1253 face->strike_through_color_defaulted_p = 1;
1254 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1255 break;
1257 case LFACE_BOX_INDEX:
1258 face->box_color_defaulted_p = 1;
1259 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1260 break;
1262 default:
1263 emacs_abort ();
1266 #ifdef GLYPH_DEBUG
1267 else
1268 ++ncolors_allocated;
1269 #endif
1271 return color.pixel;
1275 #ifdef HAVE_WINDOW_SYSTEM
1277 /* Load colors for face FACE which is used on frame F. Colors are
1278 specified by slots LFACE_BACKGROUND_INDEX and LFACE_FOREGROUND_INDEX
1279 of ATTRS. If the background color specified is not supported on F,
1280 try to emulate gray colors with a stipple from Vface_default_stipple. */
1282 static void
1283 load_face_colors (struct frame *f, struct face *face,
1284 Lisp_Object attrs[LFACE_VECTOR_SIZE])
1286 Lisp_Object fg, bg;
1288 bg = attrs[LFACE_BACKGROUND_INDEX];
1289 fg = attrs[LFACE_FOREGROUND_INDEX];
1291 /* Swap colors if face is inverse-video. */
1292 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1294 Lisp_Object tmp;
1295 tmp = fg;
1296 fg = bg;
1297 bg = tmp;
1300 /* Check for support for foreground, not for background because
1301 face_color_supported_p is smart enough to know that grays are
1302 "supported" as background because we are supposed to use stipple
1303 for them. */
1304 if (!face_color_supported_p (f, SSDATA (bg), 0)
1305 && !NILP (Fbitmap_spec_p (Vface_default_stipple)))
1307 x_destroy_bitmap (f, face->stipple);
1308 face->stipple = load_pixmap (f, Vface_default_stipple,
1309 &face->pixmap_w, &face->pixmap_h);
1312 face->background = load_color (f, face, bg, LFACE_BACKGROUND_INDEX);
1313 face->foreground = load_color (f, face, fg, LFACE_FOREGROUND_INDEX);
1317 /* Free color PIXEL on frame F. */
1319 void
1320 unload_color (struct frame *f, long unsigned int pixel)
1322 #ifdef HAVE_X_WINDOWS
1323 if (pixel != -1)
1325 block_input ();
1326 x_free_colors (f, &pixel, 1);
1327 unblock_input ();
1329 #endif
1333 /* Free colors allocated for FACE. */
1335 static void
1336 free_face_colors (struct frame *f, struct face *face)
1338 /* PENDING(NS): need to do something here? */
1339 #ifdef HAVE_X_WINDOWS
1340 if (face->colors_copied_bitwise_p)
1341 return;
1343 block_input ();
1345 if (!face->foreground_defaulted_p)
1347 x_free_colors (f, &face->foreground, 1);
1348 IF_DEBUG (--ncolors_allocated);
1351 if (!face->background_defaulted_p)
1353 x_free_colors (f, &face->background, 1);
1354 IF_DEBUG (--ncolors_allocated);
1357 if (face->underline_p
1358 && !face->underline_defaulted_p)
1360 x_free_colors (f, &face->underline_color, 1);
1361 IF_DEBUG (--ncolors_allocated);
1364 if (face->overline_p
1365 && !face->overline_color_defaulted_p)
1367 x_free_colors (f, &face->overline_color, 1);
1368 IF_DEBUG (--ncolors_allocated);
1371 if (face->strike_through_p
1372 && !face->strike_through_color_defaulted_p)
1374 x_free_colors (f, &face->strike_through_color, 1);
1375 IF_DEBUG (--ncolors_allocated);
1378 if (face->box != FACE_NO_BOX
1379 && !face->box_color_defaulted_p)
1381 x_free_colors (f, &face->box_color, 1);
1382 IF_DEBUG (--ncolors_allocated);
1385 unblock_input ();
1386 #endif /* HAVE_X_WINDOWS */
1389 #endif /* HAVE_WINDOW_SYSTEM */
1393 /***********************************************************************
1394 XLFD Font Names
1395 ***********************************************************************/
1397 /* An enumerator for each field of an XLFD font name. */
1399 enum xlfd_field
1401 XLFD_FOUNDRY,
1402 XLFD_FAMILY,
1403 XLFD_WEIGHT,
1404 XLFD_SLANT,
1405 XLFD_SWIDTH,
1406 XLFD_ADSTYLE,
1407 XLFD_PIXEL_SIZE,
1408 XLFD_POINT_SIZE,
1409 XLFD_RESX,
1410 XLFD_RESY,
1411 XLFD_SPACING,
1412 XLFD_AVGWIDTH,
1413 XLFD_REGISTRY,
1414 XLFD_ENCODING,
1415 XLFD_LAST
1418 /* An enumerator for each possible slant value of a font. Taken from
1419 the XLFD specification. */
1421 enum xlfd_slant
1423 XLFD_SLANT_UNKNOWN,
1424 XLFD_SLANT_ROMAN,
1425 XLFD_SLANT_ITALIC,
1426 XLFD_SLANT_OBLIQUE,
1427 XLFD_SLANT_REVERSE_ITALIC,
1428 XLFD_SLANT_REVERSE_OBLIQUE,
1429 XLFD_SLANT_OTHER
1432 /* Relative font weight according to XLFD documentation. */
1434 enum xlfd_weight
1436 XLFD_WEIGHT_UNKNOWN,
1437 XLFD_WEIGHT_ULTRA_LIGHT, /* 10 */
1438 XLFD_WEIGHT_EXTRA_LIGHT, /* 20 */
1439 XLFD_WEIGHT_LIGHT, /* 30 */
1440 XLFD_WEIGHT_SEMI_LIGHT, /* 40: SemiLight, Book, ... */
1441 XLFD_WEIGHT_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1442 XLFD_WEIGHT_SEMI_BOLD, /* 60: SemiBold, DemiBold, ... */
1443 XLFD_WEIGHT_BOLD, /* 70: Bold, ... */
1444 XLFD_WEIGHT_EXTRA_BOLD, /* 80: ExtraBold, Heavy, ... */
1445 XLFD_WEIGHT_ULTRA_BOLD /* 90: UltraBold, Black, ... */
1448 /* Relative proportionate width. */
1450 enum xlfd_swidth
1452 XLFD_SWIDTH_UNKNOWN,
1453 XLFD_SWIDTH_ULTRA_CONDENSED, /* 10 */
1454 XLFD_SWIDTH_EXTRA_CONDENSED, /* 20 */
1455 XLFD_SWIDTH_CONDENSED, /* 30: Condensed, Narrow, Compressed, ... */
1456 XLFD_SWIDTH_SEMI_CONDENSED, /* 40: semicondensed */
1457 XLFD_SWIDTH_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1458 XLFD_SWIDTH_SEMI_EXPANDED, /* 60: SemiExpanded, DemiExpanded, ... */
1459 XLFD_SWIDTH_EXPANDED, /* 70: Expanded... */
1460 XLFD_SWIDTH_EXTRA_EXPANDED, /* 80: ExtraExpanded, Wide... */
1461 XLFD_SWIDTH_ULTRA_EXPANDED /* 90: UltraExpanded... */
1464 /* Order by which font selection chooses fonts. The default values
1465 mean `first, find a best match for the font width, then for the
1466 font height, then for weight, then for slant.' This variable can be
1467 set via set-face-font-sort-order. */
1469 static int font_sort_order[4];
1471 #ifdef HAVE_WINDOW_SYSTEM
1473 static enum font_property_index font_props_for_sorting[FONT_SIZE_INDEX];
1475 static int
1476 compare_fonts_by_sort_order (const void *v1, const void *v2)
1478 Lisp_Object const *p1 = v1;
1479 Lisp_Object const *p2 = v2;
1480 Lisp_Object font1 = *p1;
1481 Lisp_Object font2 = *p2;
1482 int i;
1484 for (i = 0; i < FONT_SIZE_INDEX; i++)
1486 enum font_property_index idx = font_props_for_sorting[i];
1487 Lisp_Object val1 = AREF (font1, idx), val2 = AREF (font2, idx);
1488 int result;
1490 if (idx <= FONT_REGISTRY_INDEX)
1492 if (STRINGP (val1))
1493 result = STRINGP (val2) ? strcmp (SSDATA (val1), SSDATA (val2)) : -1;
1494 else
1495 result = STRINGP (val2) ? 1 : 0;
1497 else
1499 if (INTEGERP (val1))
1500 result = (INTEGERP (val2) && XINT (val1) >= XINT (val2)
1501 ? XINT (val1) > XINT (val2)
1502 : -1);
1503 else
1504 result = INTEGERP (val2) ? 1 : 0;
1506 if (result)
1507 return result;
1509 return 0;
1512 DEFUN ("x-family-fonts", Fx_family_fonts, Sx_family_fonts, 0, 2, 0,
1513 doc: /* Return a list of available fonts of family FAMILY on FRAME.
1514 If FAMILY is omitted or nil, list all families.
1515 Otherwise, FAMILY must be a string, possibly containing wildcards
1516 `?' and `*'.
1517 If FRAME is omitted or nil, use the selected frame.
1518 Each element of the result is a vector [FAMILY WIDTH POINT-SIZE WEIGHT
1519 SLANT FIXED-P FULL REGISTRY-AND-ENCODING].
1520 FAMILY is the font family name. POINT-SIZE is the size of the
1521 font in 1/10 pt. WIDTH, WEIGHT, and SLANT are symbols describing the
1522 width, weight and slant of the font. These symbols are the same as for
1523 face attributes. FIXED-P is non-nil if the font is fixed-pitch.
1524 FULL is the full name of the font, and REGISTRY-AND-ENCODING is a string
1525 giving the registry and encoding of the font.
1526 The result list is sorted according to the current setting of
1527 the face font sort order. */)
1528 (Lisp_Object family, Lisp_Object frame)
1530 Lisp_Object font_spec, list, *drivers, vec;
1531 struct frame *f = decode_live_frame (frame);
1532 ptrdiff_t i, nfonts;
1533 EMACS_INT ndrivers;
1534 Lisp_Object result;
1535 USE_SAFE_ALLOCA;
1537 font_spec = Ffont_spec (0, NULL);
1538 if (!NILP (family))
1540 CHECK_STRING (family);
1541 font_parse_family_registry (family, Qnil, font_spec);
1544 list = font_list_entities (f, font_spec);
1545 if (NILP (list))
1546 return Qnil;
1548 /* Sort the font entities. */
1549 for (i = 0; i < 4; i++)
1550 switch (font_sort_order[i])
1552 case XLFD_SWIDTH:
1553 font_props_for_sorting[i] = FONT_WIDTH_INDEX; break;
1554 case XLFD_POINT_SIZE:
1555 font_props_for_sorting[i] = FONT_SIZE_INDEX; break;
1556 case XLFD_WEIGHT:
1557 font_props_for_sorting[i] = FONT_WEIGHT_INDEX; break;
1558 default:
1559 font_props_for_sorting[i] = FONT_SLANT_INDEX; break;
1561 font_props_for_sorting[i++] = FONT_FAMILY_INDEX;
1562 font_props_for_sorting[i++] = FONT_FOUNDRY_INDEX;
1563 font_props_for_sorting[i++] = FONT_ADSTYLE_INDEX;
1564 font_props_for_sorting[i++] = FONT_REGISTRY_INDEX;
1566 ndrivers = XINT (Flength (list));
1567 SAFE_ALLOCA_LISP (drivers, ndrivers);
1568 for (i = 0; i < ndrivers; i++, list = XCDR (list))
1569 drivers[i] = XCAR (list);
1570 vec = Fvconcat (ndrivers, drivers);
1571 nfonts = ASIZE (vec);
1573 qsort (XVECTOR (vec)->contents, nfonts, word_size,
1574 compare_fonts_by_sort_order);
1576 result = Qnil;
1577 for (i = nfonts - 1; i >= 0; --i)
1579 Lisp_Object font = AREF (vec, i);
1580 Lisp_Object v = make_uninit_vector (8);
1581 int point;
1582 Lisp_Object spacing;
1584 ASET (v, 0, AREF (font, FONT_FAMILY_INDEX));
1585 ASET (v, 1, FONT_WIDTH_SYMBOLIC (font));
1586 point = PIXEL_TO_POINT (XINT (AREF (font, FONT_SIZE_INDEX)) * 10,
1587 FRAME_RES_Y (f));
1588 ASET (v, 2, make_number (point));
1589 ASET (v, 3, FONT_WEIGHT_SYMBOLIC (font));
1590 ASET (v, 4, FONT_SLANT_SYMBOLIC (font));
1591 spacing = Ffont_get (font, QCspacing);
1592 ASET (v, 5, (NILP (spacing) || EQ (spacing, Qp)) ? Qnil : Qt);
1593 ASET (v, 6, Ffont_xlfd_name (font, Qnil));
1594 ASET (v, 7, AREF (font, FONT_REGISTRY_INDEX));
1596 result = Fcons (v, result);
1599 SAFE_FREE ();
1600 return result;
1603 DEFUN ("x-list-fonts", Fx_list_fonts, Sx_list_fonts, 1, 5, 0,
1604 doc: /* Return a list of the names of available fonts matching PATTERN.
1605 If optional arguments FACE and FRAME are specified, return only fonts
1606 the same size as FACE on FRAME.
1608 PATTERN should be a string containing a font name in the XLFD,
1609 Fontconfig, or GTK format. A font name given in the XLFD format may
1610 contain wildcard characters:
1611 the * character matches any substring, and
1612 the ? character matches any single character.
1613 PATTERN is case-insensitive.
1615 The return value is a list of strings, suitable as arguments to
1616 `set-face-font'.
1618 Fonts Emacs can't use may or may not be excluded
1619 even if they match PATTERN and FACE.
1620 The optional fourth argument MAXIMUM sets a limit on how many
1621 fonts to match. The first MAXIMUM fonts are reported.
1622 The optional fifth argument WIDTH, if specified, is a number of columns
1623 occupied by a character of a font. In that case, return only fonts
1624 the WIDTH times as wide as FACE on FRAME. */)
1625 (Lisp_Object pattern, Lisp_Object face, Lisp_Object frame,
1626 Lisp_Object maximum, Lisp_Object width)
1628 struct frame *f;
1629 int size, avgwidth IF_LINT (= 0);
1631 check_window_system (NULL);
1632 CHECK_STRING (pattern);
1634 if (! NILP (maximum))
1635 CHECK_NATNUM (maximum);
1637 if (!NILP (width))
1638 CHECK_NUMBER (width);
1640 /* We can't simply call decode_window_system_frame because
1641 this function may be called before any frame is created. */
1642 f = decode_live_frame (frame);
1643 if (! FRAME_WINDOW_P (f))
1645 /* Perhaps we have not yet created any frame. */
1646 f = NULL;
1647 frame = Qnil;
1648 face = Qnil;
1650 else
1651 XSETFRAME (frame, f);
1653 /* Determine the width standard for comparison with the fonts we find. */
1655 if (NILP (face))
1656 size = 0;
1657 else
1659 /* This is of limited utility since it works with character
1660 widths. Keep it for compatibility. --gerd. */
1661 int face_id = lookup_named_face (f, face, 0);
1662 struct face *width_face = (face_id < 0
1663 ? NULL
1664 : FACE_FROM_ID (f, face_id));
1666 if (width_face && width_face->font)
1668 size = width_face->font->pixel_size;
1669 avgwidth = width_face->font->average_width;
1671 else
1673 size = FRAME_FONT (f)->pixel_size;
1674 avgwidth = FRAME_FONT (f)->average_width;
1676 if (!NILP (width))
1677 avgwidth *= XINT (width);
1681 Lisp_Object font_spec;
1682 Lisp_Object args[2], tail;
1684 font_spec = font_spec_from_name (pattern);
1685 if (!FONTP (font_spec))
1686 signal_error ("Invalid font name", pattern);
1688 if (size)
1690 Ffont_put (font_spec, QCsize, make_number (size));
1691 Ffont_put (font_spec, QCavgwidth, make_number (avgwidth));
1693 args[0] = Flist_fonts (font_spec, frame, maximum, font_spec);
1694 for (tail = args[0]; CONSP (tail); tail = XCDR (tail))
1696 Lisp_Object font_entity;
1698 font_entity = XCAR (tail);
1699 if ((NILP (AREF (font_entity, FONT_SIZE_INDEX))
1700 || XINT (AREF (font_entity, FONT_SIZE_INDEX)) == 0)
1701 && ! NILP (AREF (font_spec, FONT_SIZE_INDEX)))
1703 /* This is a scalable font. For backward compatibility,
1704 we set the specified size. */
1705 font_entity = copy_font_spec (font_entity);
1706 ASET (font_entity, FONT_SIZE_INDEX,
1707 AREF (font_spec, FONT_SIZE_INDEX));
1709 XSETCAR (tail, Ffont_xlfd_name (font_entity, Qnil));
1711 if (NILP (frame))
1712 /* We don't have to check fontsets. */
1713 return args[0];
1714 args[1] = list_fontsets (f, pattern, size);
1715 return Fnconc (2, args);
1719 #endif /* HAVE_WINDOW_SYSTEM */
1722 /***********************************************************************
1723 Lisp Faces
1724 ***********************************************************************/
1726 /* Access face attributes of face LFACE, a Lisp vector. */
1728 #define LFACE_FAMILY(LFACE) AREF ((LFACE), LFACE_FAMILY_INDEX)
1729 #define LFACE_FOUNDRY(LFACE) AREF ((LFACE), LFACE_FOUNDRY_INDEX)
1730 #define LFACE_HEIGHT(LFACE) AREF ((LFACE), LFACE_HEIGHT_INDEX)
1731 #define LFACE_WEIGHT(LFACE) AREF ((LFACE), LFACE_WEIGHT_INDEX)
1732 #define LFACE_SLANT(LFACE) AREF ((LFACE), LFACE_SLANT_INDEX)
1733 #define LFACE_UNDERLINE(LFACE) AREF ((LFACE), LFACE_UNDERLINE_INDEX)
1734 #define LFACE_INVERSE(LFACE) AREF ((LFACE), LFACE_INVERSE_INDEX)
1735 #define LFACE_FOREGROUND(LFACE) AREF ((LFACE), LFACE_FOREGROUND_INDEX)
1736 #define LFACE_BACKGROUND(LFACE) AREF ((LFACE), LFACE_BACKGROUND_INDEX)
1737 #define LFACE_STIPPLE(LFACE) AREF ((LFACE), LFACE_STIPPLE_INDEX)
1738 #define LFACE_SWIDTH(LFACE) AREF ((LFACE), LFACE_SWIDTH_INDEX)
1739 #define LFACE_OVERLINE(LFACE) AREF ((LFACE), LFACE_OVERLINE_INDEX)
1740 #define LFACE_STRIKE_THROUGH(LFACE) AREF ((LFACE), LFACE_STRIKE_THROUGH_INDEX)
1741 #define LFACE_BOX(LFACE) AREF ((LFACE), LFACE_BOX_INDEX)
1742 #define LFACE_FONT(LFACE) AREF ((LFACE), LFACE_FONT_INDEX)
1743 #define LFACE_INHERIT(LFACE) AREF ((LFACE), LFACE_INHERIT_INDEX)
1744 #define LFACE_FONTSET(LFACE) AREF ((LFACE), LFACE_FONTSET_INDEX)
1746 /* Non-zero if LFACE is a Lisp face. A Lisp face is a vector of size
1747 LFACE_VECTOR_SIZE which has the symbol `face' in slot 0. */
1749 #define LFACEP(LFACE) \
1750 (VECTORP (LFACE) \
1751 && ASIZE (LFACE) == LFACE_VECTOR_SIZE \
1752 && EQ (AREF (LFACE, 0), Qface))
1755 #ifdef GLYPH_DEBUG
1757 /* Check consistency of Lisp face attribute vector ATTRS. */
1759 static void
1760 check_lface_attrs (Lisp_Object attrs[LFACE_VECTOR_SIZE])
1762 eassert (UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
1763 || IGNORE_DEFFACE_P (attrs[LFACE_FAMILY_INDEX])
1764 || STRINGP (attrs[LFACE_FAMILY_INDEX]));
1765 eassert (UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
1766 || IGNORE_DEFFACE_P (attrs[LFACE_FOUNDRY_INDEX])
1767 || STRINGP (attrs[LFACE_FOUNDRY_INDEX]));
1768 eassert (UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
1769 || IGNORE_DEFFACE_P (attrs[LFACE_SWIDTH_INDEX])
1770 || SYMBOLP (attrs[LFACE_SWIDTH_INDEX]));
1771 eassert (UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
1772 || IGNORE_DEFFACE_P (attrs[LFACE_HEIGHT_INDEX])
1773 || INTEGERP (attrs[LFACE_HEIGHT_INDEX])
1774 || FLOATP (attrs[LFACE_HEIGHT_INDEX])
1775 || FUNCTIONP (attrs[LFACE_HEIGHT_INDEX]));
1776 eassert (UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
1777 || IGNORE_DEFFACE_P (attrs[LFACE_WEIGHT_INDEX])
1778 || SYMBOLP (attrs[LFACE_WEIGHT_INDEX]));
1779 eassert (UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
1780 || IGNORE_DEFFACE_P (attrs[LFACE_SLANT_INDEX])
1781 || SYMBOLP (attrs[LFACE_SLANT_INDEX]));
1782 eassert (UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
1783 || IGNORE_DEFFACE_P (attrs[LFACE_UNDERLINE_INDEX])
1784 || SYMBOLP (attrs[LFACE_UNDERLINE_INDEX])
1785 || STRINGP (attrs[LFACE_UNDERLINE_INDEX])
1786 || CONSP (attrs[LFACE_UNDERLINE_INDEX]));
1787 eassert (UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
1788 || IGNORE_DEFFACE_P (attrs[LFACE_OVERLINE_INDEX])
1789 || SYMBOLP (attrs[LFACE_OVERLINE_INDEX])
1790 || STRINGP (attrs[LFACE_OVERLINE_INDEX]));
1791 eassert (UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1792 || IGNORE_DEFFACE_P (attrs[LFACE_STRIKE_THROUGH_INDEX])
1793 || SYMBOLP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1794 || STRINGP (attrs[LFACE_STRIKE_THROUGH_INDEX]));
1795 eassert (UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
1796 || IGNORE_DEFFACE_P (attrs[LFACE_BOX_INDEX])
1797 || SYMBOLP (attrs[LFACE_BOX_INDEX])
1798 || STRINGP (attrs[LFACE_BOX_INDEX])
1799 || INTEGERP (attrs[LFACE_BOX_INDEX])
1800 || CONSP (attrs[LFACE_BOX_INDEX]));
1801 eassert (UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
1802 || IGNORE_DEFFACE_P (attrs[LFACE_INVERSE_INDEX])
1803 || SYMBOLP (attrs[LFACE_INVERSE_INDEX]));
1804 eassert (UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
1805 || IGNORE_DEFFACE_P (attrs[LFACE_FOREGROUND_INDEX])
1806 || STRINGP (attrs[LFACE_FOREGROUND_INDEX]));
1807 eassert (UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
1808 || IGNORE_DEFFACE_P (attrs[LFACE_BACKGROUND_INDEX])
1809 || STRINGP (attrs[LFACE_BACKGROUND_INDEX]));
1810 eassert (UNSPECIFIEDP (attrs[LFACE_INHERIT_INDEX])
1811 || IGNORE_DEFFACE_P (attrs[LFACE_INHERIT_INDEX])
1812 || NILP (attrs[LFACE_INHERIT_INDEX])
1813 || SYMBOLP (attrs[LFACE_INHERIT_INDEX])
1814 || CONSP (attrs[LFACE_INHERIT_INDEX]));
1815 #ifdef HAVE_WINDOW_SYSTEM
1816 eassert (UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
1817 || IGNORE_DEFFACE_P (attrs[LFACE_STIPPLE_INDEX])
1818 || SYMBOLP (attrs[LFACE_STIPPLE_INDEX])
1819 || !NILP (Fbitmap_spec_p (attrs[LFACE_STIPPLE_INDEX])));
1820 eassert (UNSPECIFIEDP (attrs[LFACE_FONT_INDEX])
1821 || IGNORE_DEFFACE_P (attrs[LFACE_FONT_INDEX])
1822 || FONTP (attrs[LFACE_FONT_INDEX]));
1823 eassert (UNSPECIFIEDP (attrs[LFACE_FONTSET_INDEX])
1824 || STRINGP (attrs[LFACE_FONTSET_INDEX])
1825 || NILP (attrs[LFACE_FONTSET_INDEX]));
1826 #endif
1830 /* Check consistency of attributes of Lisp face LFACE (a Lisp vector). */
1832 static void
1833 check_lface (Lisp_Object lface)
1835 if (!NILP (lface))
1837 eassert (LFACEP (lface));
1838 check_lface_attrs (XVECTOR (lface)->contents);
1842 #else /* not GLYPH_DEBUG */
1844 #define check_lface_attrs(attrs) (void) 0
1845 #define check_lface(lface) (void) 0
1847 #endif /* GLYPH_DEBUG */
1851 /* Face-merge cycle checking. */
1853 enum named_merge_point_kind
1855 NAMED_MERGE_POINT_NORMAL,
1856 NAMED_MERGE_POINT_REMAP
1859 /* A `named merge point' is simply a point during face-merging where we
1860 look up a face by name. We keep a stack of which named lookups we're
1861 currently processing so that we can easily detect cycles, using a
1862 linked- list of struct named_merge_point structures, typically
1863 allocated on the stack frame of the named lookup functions which are
1864 active (so no consing is required). */
1865 struct named_merge_point
1867 Lisp_Object face_name;
1868 enum named_merge_point_kind named_merge_point_kind;
1869 struct named_merge_point *prev;
1873 /* If a face merging cycle is detected for FACE_NAME, return 0,
1874 otherwise add NEW_NAMED_MERGE_POINT, which is initialized using
1875 FACE_NAME and NAMED_MERGE_POINT_KIND, as the head of the linked list
1876 pointed to by NAMED_MERGE_POINTS, and return 1. */
1878 static int
1879 push_named_merge_point (struct named_merge_point *new_named_merge_point,
1880 Lisp_Object face_name,
1881 enum named_merge_point_kind named_merge_point_kind,
1882 struct named_merge_point **named_merge_points)
1884 struct named_merge_point *prev;
1886 for (prev = *named_merge_points; prev; prev = prev->prev)
1887 if (EQ (face_name, prev->face_name))
1889 if (prev->named_merge_point_kind == named_merge_point_kind)
1890 /* A cycle, so fail. */
1891 return 0;
1892 else if (prev->named_merge_point_kind == NAMED_MERGE_POINT_REMAP)
1893 /* A remap `hides ' any previous normal merge points
1894 (because the remap means that it's actually different face),
1895 so as we know the current merge point must be normal, we
1896 can just assume it's OK. */
1897 break;
1900 new_named_merge_point->face_name = face_name;
1901 new_named_merge_point->named_merge_point_kind = named_merge_point_kind;
1902 new_named_merge_point->prev = *named_merge_points;
1904 *named_merge_points = new_named_merge_point;
1906 return 1;
1910 /* Resolve face name FACE_NAME. If FACE_NAME is a string, intern it
1911 to make it a symbol. If FACE_NAME is an alias for another face,
1912 return that face's name.
1914 Return default face in case of errors. */
1916 static Lisp_Object
1917 resolve_face_name (Lisp_Object face_name, int signal_p)
1919 Lisp_Object orig_face;
1920 Lisp_Object tortoise, hare;
1922 if (STRINGP (face_name))
1923 face_name = intern (SSDATA (face_name));
1925 if (NILP (face_name) || !SYMBOLP (face_name))
1926 return face_name;
1928 orig_face = face_name;
1929 tortoise = hare = face_name;
1931 while (1)
1933 face_name = hare;
1934 hare = Fget (hare, Qface_alias);
1935 if (NILP (hare) || !SYMBOLP (hare))
1936 break;
1938 face_name = hare;
1939 hare = Fget (hare, Qface_alias);
1940 if (NILP (hare) || !SYMBOLP (hare))
1941 break;
1943 tortoise = Fget (tortoise, Qface_alias);
1944 if (EQ (hare, tortoise))
1946 if (signal_p)
1947 xsignal1 (Qcircular_list, orig_face);
1948 return Qdefault;
1952 return face_name;
1956 /* Return the face definition of FACE_NAME on frame F. F null means
1957 return the definition for new frames. FACE_NAME may be a string or
1958 a symbol (apparently Emacs 20.2 allowed strings as face names in
1959 face text properties; Ediff uses that). If SIGNAL_P is non-zero,
1960 signal an error if FACE_NAME is not a valid face name. If SIGNAL_P
1961 is zero, value is nil if FACE_NAME is not a valid face name. */
1962 static Lisp_Object
1963 lface_from_face_name_no_resolve (struct frame *f, Lisp_Object face_name,
1964 int signal_p)
1966 Lisp_Object lface;
1968 if (f)
1969 lface = assq_no_quit (face_name, f->face_alist);
1970 else
1971 lface = assq_no_quit (face_name, Vface_new_frame_defaults);
1973 if (CONSP (lface))
1974 lface = XCDR (lface);
1975 else if (signal_p)
1976 signal_error ("Invalid face", face_name);
1978 check_lface (lface);
1980 return lface;
1983 /* Return the face definition of FACE_NAME on frame F. F null means
1984 return the definition for new frames. FACE_NAME may be a string or
1985 a symbol (apparently Emacs 20.2 allowed strings as face names in
1986 face text properties; Ediff uses that). If FACE_NAME is an alias
1987 for another face, return that face's definition. If SIGNAL_P is
1988 non-zero, signal an error if FACE_NAME is not a valid face name.
1989 If SIGNAL_P is zero, value is nil if FACE_NAME is not a valid face
1990 name. */
1991 static Lisp_Object
1992 lface_from_face_name (struct frame *f, Lisp_Object face_name, int signal_p)
1994 face_name = resolve_face_name (face_name, signal_p);
1995 return lface_from_face_name_no_resolve (f, face_name, signal_p);
1999 /* Get face attributes of face FACE_NAME from frame-local faces on
2000 frame F. Store the resulting attributes in ATTRS which must point
2001 to a vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If SIGNAL_P
2002 is non-zero, signal an error if FACE_NAME does not name a face.
2003 Otherwise, value is zero if FACE_NAME is not a face. */
2005 static int
2006 get_lface_attributes_no_remap (struct frame *f, Lisp_Object face_name,
2007 Lisp_Object attrs[LFACE_VECTOR_SIZE],
2008 int signal_p)
2010 Lisp_Object lface;
2012 lface = lface_from_face_name_no_resolve (f, face_name, signal_p);
2014 if (! NILP (lface))
2015 memcpy (attrs, XVECTOR (lface)->contents,
2016 LFACE_VECTOR_SIZE * sizeof *attrs);
2018 return !NILP (lface);
2021 /* Get face attributes of face FACE_NAME from frame-local faces on frame
2022 F. Store the resulting attributes in ATTRS which must point to a
2023 vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If FACE_NAME is an
2024 alias for another face, use that face's definition. If SIGNAL_P is
2025 non-zero, signal an error if FACE_NAME does not name a face.
2026 Otherwise, value is zero if FACE_NAME is not a face. */
2028 static int
2029 get_lface_attributes (struct frame *f, Lisp_Object face_name,
2030 Lisp_Object attrs[LFACE_VECTOR_SIZE], int signal_p,
2031 struct named_merge_point *named_merge_points)
2033 Lisp_Object face_remapping;
2035 face_name = resolve_face_name (face_name, signal_p);
2037 /* See if SYMBOL has been remapped to some other face (usually this
2038 is done buffer-locally). */
2039 face_remapping = assq_no_quit (face_name, Vface_remapping_alist);
2040 if (CONSP (face_remapping))
2042 struct named_merge_point named_merge_point;
2044 if (push_named_merge_point (&named_merge_point,
2045 face_name, NAMED_MERGE_POINT_REMAP,
2046 &named_merge_points))
2048 int i;
2050 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2051 attrs[i] = Qunspecified;
2053 return merge_face_ref (f, XCDR (face_remapping), attrs,
2054 signal_p, named_merge_points);
2058 /* Default case, no remapping. */
2059 return get_lface_attributes_no_remap (f, face_name, attrs, signal_p);
2063 /* Non-zero if all attributes in face attribute vector ATTRS are
2064 specified, i.e. are non-nil. */
2066 static int
2067 lface_fully_specified_p (Lisp_Object attrs[LFACE_VECTOR_SIZE])
2069 int i;
2071 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2072 if (i != LFACE_FONT_INDEX && i != LFACE_INHERIT_INDEX)
2073 if ((UNSPECIFIEDP (attrs[i]) || IGNORE_DEFFACE_P (attrs[i])))
2074 break;
2076 return i == LFACE_VECTOR_SIZE;
2079 #ifdef HAVE_WINDOW_SYSTEM
2081 /* Set font-related attributes of Lisp face LFACE from FONT-OBJECT.
2082 If FORCE_P is zero, set only unspecified attributes of LFACE. The
2083 exception is `font' attribute. It is set to FONT_OBJECT regardless
2084 of FORCE_P. */
2086 static int
2087 set_lface_from_font (struct frame *f, Lisp_Object lface,
2088 Lisp_Object font_object, int force_p)
2090 Lisp_Object val;
2091 struct font *font = XFONT_OBJECT (font_object);
2093 /* Set attributes only if unspecified, otherwise face defaults for
2094 new frames would never take effect. If the font doesn't have a
2095 specific property, set a normal value for that. */
2097 if (force_p || UNSPECIFIEDP (LFACE_FAMILY (lface)))
2099 Lisp_Object family = AREF (font_object, FONT_FAMILY_INDEX);
2101 ASET (lface, LFACE_FAMILY_INDEX, SYMBOL_NAME (family));
2104 if (force_p || UNSPECIFIEDP (LFACE_FOUNDRY (lface)))
2106 Lisp_Object foundry = AREF (font_object, FONT_FOUNDRY_INDEX);
2108 ASET (lface, LFACE_FOUNDRY_INDEX, SYMBOL_NAME (foundry));
2111 if (force_p || UNSPECIFIEDP (LFACE_HEIGHT (lface)))
2113 int pt = PIXEL_TO_POINT (font->pixel_size * 10, FRAME_RES_Y (f));
2115 eassert (pt > 0);
2116 ASET (lface, LFACE_HEIGHT_INDEX, make_number (pt));
2119 if (force_p || UNSPECIFIEDP (LFACE_WEIGHT (lface)))
2121 val = FONT_WEIGHT_FOR_FACE (font_object);
2122 ASET (lface, LFACE_WEIGHT_INDEX, ! NILP (val) ? val :Qnormal);
2124 if (force_p || UNSPECIFIEDP (LFACE_SLANT (lface)))
2126 val = FONT_SLANT_FOR_FACE (font_object);
2127 ASET (lface, LFACE_SLANT_INDEX, ! NILP (val) ? val : Qnormal);
2129 if (force_p || UNSPECIFIEDP (LFACE_SWIDTH (lface)))
2131 val = FONT_WIDTH_FOR_FACE (font_object);
2132 ASET (lface, LFACE_SWIDTH_INDEX, ! NILP (val) ? val : Qnormal);
2135 ASET (lface, LFACE_FONT_INDEX, font_object);
2136 return 1;
2139 #endif /* HAVE_WINDOW_SYSTEM */
2142 /* Merges the face height FROM with the face height TO, and returns the
2143 merged height. If FROM is an invalid height, then INVALID is
2144 returned instead. FROM and TO may be either absolute face heights or
2145 `relative' heights; the returned value is always an absolute height
2146 unless both FROM and TO are relative. */
2148 static Lisp_Object
2149 merge_face_heights (Lisp_Object from, Lisp_Object to, Lisp_Object invalid)
2151 Lisp_Object result = invalid;
2153 if (INTEGERP (from))
2154 /* FROM is absolute, just use it as is. */
2155 result = from;
2156 else if (FLOATP (from))
2157 /* FROM is a scale, use it to adjust TO. */
2159 if (INTEGERP (to))
2160 /* relative X absolute => absolute */
2161 result = make_number (XFLOAT_DATA (from) * XINT (to));
2162 else if (FLOATP (to))
2163 /* relative X relative => relative */
2164 result = make_float (XFLOAT_DATA (from) * XFLOAT_DATA (to));
2165 else if (UNSPECIFIEDP (to))
2166 result = from;
2168 else if (FUNCTIONP (from))
2169 /* FROM is a function, which use to adjust TO. */
2171 /* Call function with current height as argument.
2172 From is the new height. */
2173 result = safe_call1 (from, to);
2175 /* Ensure that if TO was absolute, so is the result. */
2176 if (INTEGERP (to) && !INTEGERP (result))
2177 result = invalid;
2180 return result;
2184 /* Merge two Lisp face attribute vectors on frame F, FROM and TO, and
2185 store the resulting attributes in TO, which must be already be
2186 completely specified and contain only absolute attributes. Every
2187 specified attribute of FROM overrides the corresponding attribute of
2188 TO; relative attributes in FROM are merged with the absolute value in
2189 TO and replace it. NAMED_MERGE_POINTS is used internally to detect
2190 loops in face inheritance/remapping; it should be 0 when called from
2191 other places. */
2193 static void
2194 merge_face_vectors (struct frame *f, Lisp_Object *from, Lisp_Object *to,
2195 struct named_merge_point *named_merge_points)
2197 int i;
2198 Lisp_Object font = Qnil;
2200 /* If FROM inherits from some other faces, merge their attributes into
2201 TO before merging FROM's direct attributes. Note that an :inherit
2202 attribute of `unspecified' is the same as one of nil; we never
2203 merge :inherit attributes, so nil is more correct, but lots of
2204 other code uses `unspecified' as a generic value for face attributes. */
2205 if (!UNSPECIFIEDP (from[LFACE_INHERIT_INDEX])
2206 && !NILP (from[LFACE_INHERIT_INDEX]))
2207 merge_face_ref (f, from[LFACE_INHERIT_INDEX], to, 0, named_merge_points);
2209 if (FONT_SPEC_P (from[LFACE_FONT_INDEX]))
2211 if (!UNSPECIFIEDP (to[LFACE_FONT_INDEX]))
2212 font = merge_font_spec (from[LFACE_FONT_INDEX], to[LFACE_FONT_INDEX]);
2213 else
2214 font = copy_font_spec (from[LFACE_FONT_INDEX]);
2215 to[LFACE_FONT_INDEX] = font;
2218 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2219 if (!UNSPECIFIEDP (from[i]))
2221 if (i == LFACE_HEIGHT_INDEX && !INTEGERP (from[i]))
2223 to[i] = merge_face_heights (from[i], to[i], to[i]);
2224 font_clear_prop (to, FONT_SIZE_INDEX);
2226 else if (i != LFACE_FONT_INDEX && ! EQ (to[i], from[i]))
2228 to[i] = from[i];
2229 if (i >= LFACE_FAMILY_INDEX && i <=LFACE_SLANT_INDEX)
2230 font_clear_prop (to,
2231 (i == LFACE_FAMILY_INDEX ? FONT_FAMILY_INDEX
2232 : i == LFACE_FOUNDRY_INDEX ? FONT_FOUNDRY_INDEX
2233 : i == LFACE_SWIDTH_INDEX ? FONT_WIDTH_INDEX
2234 : i == LFACE_HEIGHT_INDEX ? FONT_SIZE_INDEX
2235 : i == LFACE_WEIGHT_INDEX ? FONT_WEIGHT_INDEX
2236 : FONT_SLANT_INDEX));
2240 /* If FROM specifies a font spec, make its contents take precedence
2241 over :family and other attributes. This is needed for face
2242 remapping using :font to work. */
2244 if (!NILP (font))
2246 if (! NILP (AREF (font, FONT_FOUNDRY_INDEX)))
2247 to[LFACE_FOUNDRY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FOUNDRY_INDEX));
2248 if (! NILP (AREF (font, FONT_FAMILY_INDEX)))
2249 to[LFACE_FAMILY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FAMILY_INDEX));
2250 if (! NILP (AREF (font, FONT_WEIGHT_INDEX)))
2251 to[LFACE_WEIGHT_INDEX] = FONT_WEIGHT_FOR_FACE (font);
2252 if (! NILP (AREF (font, FONT_SLANT_INDEX)))
2253 to[LFACE_SLANT_INDEX] = FONT_SLANT_FOR_FACE (font);
2254 if (! NILP (AREF (font, FONT_WIDTH_INDEX)))
2255 to[LFACE_SWIDTH_INDEX] = FONT_WIDTH_FOR_FACE (font);
2256 ASET (font, FONT_SIZE_INDEX, Qnil);
2259 /* TO is always an absolute face, which should inherit from nothing.
2260 We blindly copy the :inherit attribute above and fix it up here. */
2261 to[LFACE_INHERIT_INDEX] = Qnil;
2264 /* Merge the named face FACE_NAME on frame F, into the vector of face
2265 attributes TO. NAMED_MERGE_POINTS is used to detect loops in face
2266 inheritance. Returns true if FACE_NAME is a valid face name and
2267 merging succeeded. */
2269 static int
2270 merge_named_face (struct frame *f, Lisp_Object face_name, Lisp_Object *to,
2271 struct named_merge_point *named_merge_points)
2273 struct named_merge_point named_merge_point;
2275 if (push_named_merge_point (&named_merge_point,
2276 face_name, NAMED_MERGE_POINT_NORMAL,
2277 &named_merge_points))
2279 struct gcpro gcpro1;
2280 Lisp_Object from[LFACE_VECTOR_SIZE];
2281 int ok = get_lface_attributes (f, face_name, from, 0, named_merge_points);
2283 if (ok)
2285 GCPRO1 (named_merge_point.face_name);
2286 merge_face_vectors (f, from, to, named_merge_points);
2287 UNGCPRO;
2290 return ok;
2292 else
2293 return 0;
2297 /* Merge face attributes from the lisp `face reference' FACE_REF on
2298 frame F into the face attribute vector TO. If ERR_MSGS is non-zero,
2299 problems with FACE_REF cause an error message to be shown. Return
2300 non-zero if no errors occurred (regardless of the value of ERR_MSGS).
2301 NAMED_MERGE_POINTS is used to detect loops in face inheritance or
2302 list structure; it may be 0 for most callers.
2304 FACE_REF may be a single face specification or a list of such
2305 specifications. Each face specification can be:
2307 1. A symbol or string naming a Lisp face.
2309 2. A property list of the form (KEYWORD VALUE ...) where each
2310 KEYWORD is a face attribute name, and value is an appropriate value
2311 for that attribute.
2313 3. Conses or the form (FOREGROUND-COLOR . COLOR) or
2314 (BACKGROUND-COLOR . COLOR) where COLOR is a color name. This is
2315 for compatibility with 20.2.
2317 Face specifications earlier in lists take precedence over later
2318 specifications. */
2320 static int
2321 merge_face_ref (struct frame *f, Lisp_Object face_ref, Lisp_Object *to,
2322 int err_msgs, struct named_merge_point *named_merge_points)
2324 int ok = 1; /* Succeed without an error? */
2326 if (CONSP (face_ref))
2328 Lisp_Object first = XCAR (face_ref);
2330 if (EQ (first, Qforeground_color)
2331 || EQ (first, Qbackground_color))
2333 /* One of (FOREGROUND-COLOR . COLOR) or (BACKGROUND-COLOR
2334 . COLOR). COLOR must be a string. */
2335 Lisp_Object color_name = XCDR (face_ref);
2336 Lisp_Object color = first;
2338 if (STRINGP (color_name))
2340 if (EQ (color, Qforeground_color))
2341 to[LFACE_FOREGROUND_INDEX] = color_name;
2342 else
2343 to[LFACE_BACKGROUND_INDEX] = color_name;
2345 else
2347 if (err_msgs)
2348 add_to_log ("Invalid face color", color_name, Qnil);
2349 ok = 0;
2352 else if (SYMBOLP (first)
2353 && *SDATA (SYMBOL_NAME (first)) == ':')
2355 /* Assume this is the property list form. */
2356 while (CONSP (face_ref) && CONSP (XCDR (face_ref)))
2358 Lisp_Object keyword = XCAR (face_ref);
2359 Lisp_Object value = XCAR (XCDR (face_ref));
2360 int err = 0;
2362 /* Specifying `unspecified' is a no-op. */
2363 if (EQ (value, Qunspecified))
2365 else if (EQ (keyword, QCfamily))
2367 if (STRINGP (value))
2369 to[LFACE_FAMILY_INDEX] = value;
2370 font_clear_prop (to, FONT_FAMILY_INDEX);
2372 else
2373 err = 1;
2375 else if (EQ (keyword, QCfoundry))
2377 if (STRINGP (value))
2379 to[LFACE_FOUNDRY_INDEX] = value;
2380 font_clear_prop (to, FONT_FOUNDRY_INDEX);
2382 else
2383 err = 1;
2385 else if (EQ (keyword, QCheight))
2387 Lisp_Object new_height =
2388 merge_face_heights (value, to[LFACE_HEIGHT_INDEX], Qnil);
2390 if (! NILP (new_height))
2392 to[LFACE_HEIGHT_INDEX] = new_height;
2393 font_clear_prop (to, FONT_SIZE_INDEX);
2395 else
2396 err = 1;
2398 else if (EQ (keyword, QCweight))
2400 if (SYMBOLP (value) && FONT_WEIGHT_NAME_NUMERIC (value) >= 0)
2402 to[LFACE_WEIGHT_INDEX] = value;
2403 font_clear_prop (to, FONT_WEIGHT_INDEX);
2405 else
2406 err = 1;
2408 else if (EQ (keyword, QCslant))
2410 if (SYMBOLP (value) && FONT_SLANT_NAME_NUMERIC (value) >= 0)
2412 to[LFACE_SLANT_INDEX] = value;
2413 font_clear_prop (to, FONT_SLANT_INDEX);
2415 else
2416 err = 1;
2418 else if (EQ (keyword, QCunderline))
2420 if (EQ (value, Qt)
2421 || NILP (value)
2422 || STRINGP (value)
2423 || CONSP (value))
2424 to[LFACE_UNDERLINE_INDEX] = value;
2425 else
2426 err = 1;
2428 else if (EQ (keyword, QCoverline))
2430 if (EQ (value, Qt)
2431 || NILP (value)
2432 || STRINGP (value))
2433 to[LFACE_OVERLINE_INDEX] = value;
2434 else
2435 err = 1;
2437 else if (EQ (keyword, QCstrike_through))
2439 if (EQ (value, Qt)
2440 || NILP (value)
2441 || STRINGP (value))
2442 to[LFACE_STRIKE_THROUGH_INDEX] = value;
2443 else
2444 err = 1;
2446 else if (EQ (keyword, QCbox))
2448 if (EQ (value, Qt))
2449 value = make_number (1);
2450 if (INTEGERP (value)
2451 || STRINGP (value)
2452 || CONSP (value)
2453 || NILP (value))
2454 to[LFACE_BOX_INDEX] = value;
2455 else
2456 err = 1;
2458 else if (EQ (keyword, QCinverse_video)
2459 || EQ (keyword, QCreverse_video))
2461 if (EQ (value, Qt) || NILP (value))
2462 to[LFACE_INVERSE_INDEX] = value;
2463 else
2464 err = 1;
2466 else if (EQ (keyword, QCforeground))
2468 if (STRINGP (value))
2469 to[LFACE_FOREGROUND_INDEX] = value;
2470 else
2471 err = 1;
2473 else if (EQ (keyword, QCbackground))
2475 if (STRINGP (value))
2476 to[LFACE_BACKGROUND_INDEX] = value;
2477 else
2478 err = 1;
2480 else if (EQ (keyword, QCstipple))
2482 #if defined (HAVE_WINDOW_SYSTEM)
2483 Lisp_Object pixmap_p = Fbitmap_spec_p (value);
2484 if (!NILP (pixmap_p))
2485 to[LFACE_STIPPLE_INDEX] = value;
2486 else
2487 err = 1;
2488 #endif /* HAVE_WINDOW_SYSTEM */
2490 else if (EQ (keyword, QCwidth))
2492 if (SYMBOLP (value) && FONT_WIDTH_NAME_NUMERIC (value) >= 0)
2494 to[LFACE_SWIDTH_INDEX] = value;
2495 font_clear_prop (to, FONT_WIDTH_INDEX);
2497 else
2498 err = 1;
2500 else if (EQ (keyword, QCfont))
2502 if (FONTP (value))
2503 to[LFACE_FONT_INDEX] = value;
2504 else
2505 err = 1;
2507 else if (EQ (keyword, QCinherit))
2509 /* This is not really very useful; it's just like a
2510 normal face reference. */
2511 if (! merge_face_ref (f, value, to,
2512 err_msgs, named_merge_points))
2513 err = 1;
2515 else
2516 err = 1;
2518 if (err)
2520 add_to_log ("Invalid face attribute %S %S", keyword, value);
2521 ok = 0;
2524 face_ref = XCDR (XCDR (face_ref));
2527 else
2529 /* This is a list of face refs. Those at the beginning of the
2530 list take precedence over what follows, so we have to merge
2531 from the end backwards. */
2532 Lisp_Object next = XCDR (face_ref);
2534 if (! NILP (next))
2535 ok = merge_face_ref (f, next, to, err_msgs, named_merge_points);
2537 if (! merge_face_ref (f, first, to, err_msgs, named_merge_points))
2538 ok = 0;
2541 else
2543 /* FACE_REF ought to be a face name. */
2544 ok = merge_named_face (f, face_ref, to, named_merge_points);
2545 if (!ok && err_msgs)
2546 add_to_log ("Invalid face reference: %s", face_ref, Qnil);
2549 return ok;
2553 DEFUN ("internal-make-lisp-face", Finternal_make_lisp_face,
2554 Sinternal_make_lisp_face, 1, 2, 0,
2555 doc: /* Make FACE, a symbol, a Lisp face with all attributes nil.
2556 If FACE was not known as a face before, create a new one.
2557 If optional argument FRAME is specified, make a frame-local face
2558 for that frame. Otherwise operate on the global face definition.
2559 Value is a vector of face attributes. */)
2560 (Lisp_Object face, Lisp_Object frame)
2562 Lisp_Object global_lface, lface;
2563 struct frame *f;
2564 int i;
2566 CHECK_SYMBOL (face);
2567 global_lface = lface_from_face_name (NULL, face, 0);
2569 if (!NILP (frame))
2571 CHECK_LIVE_FRAME (frame);
2572 f = XFRAME (frame);
2573 lface = lface_from_face_name (f, face, 0);
2575 else
2576 f = NULL, lface = Qnil;
2578 /* Add a global definition if there is none. */
2579 if (NILP (global_lface))
2581 global_lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2582 Qunspecified);
2583 ASET (global_lface, 0, Qface);
2584 Vface_new_frame_defaults = Fcons (Fcons (face, global_lface),
2585 Vface_new_frame_defaults);
2587 /* Assign the new Lisp face a unique ID. The mapping from Lisp
2588 face id to Lisp face is given by the vector lface_id_to_name.
2589 The mapping from Lisp face to Lisp face id is given by the
2590 property `face' of the Lisp face name. */
2591 if (next_lface_id == lface_id_to_name_size)
2592 lface_id_to_name =
2593 xpalloc (lface_id_to_name, &lface_id_to_name_size, 1, MAX_FACE_ID,
2594 sizeof *lface_id_to_name);
2596 lface_id_to_name[next_lface_id] = face;
2597 Fput (face, Qface, make_number (next_lface_id));
2598 ++next_lface_id;
2600 else if (f == NULL)
2601 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2602 ASET (global_lface, i, Qunspecified);
2604 /* Add a frame-local definition. */
2605 if (f)
2607 if (NILP (lface))
2609 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2610 Qunspecified);
2611 ASET (lface, 0, Qface);
2612 fset_face_alist (f, Fcons (Fcons (face, lface), f->face_alist));
2614 else
2615 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2616 ASET (lface, i, Qunspecified);
2618 else
2619 lface = global_lface;
2621 /* Changing a named face means that all realized faces depending on
2622 that face are invalid. Since we cannot tell which realized faces
2623 depend on the face, make sure they are all removed. This is done
2624 by incrementing face_change_count. The next call to
2625 init_iterator will then free realized faces. */
2626 if (NILP (Fget (face, Qface_no_inherit)))
2628 ++face_change_count;
2629 ++windows_or_buffers_changed;
2632 eassert (LFACEP (lface));
2633 check_lface (lface);
2634 return lface;
2638 DEFUN ("internal-lisp-face-p", Finternal_lisp_face_p,
2639 Sinternal_lisp_face_p, 1, 2, 0,
2640 doc: /* Return non-nil if FACE names a face.
2641 FACE should be a symbol or string.
2642 If optional second argument FRAME is non-nil, check for the
2643 existence of a frame-local face with name FACE on that frame.
2644 Otherwise check for the existence of a global face. */)
2645 (Lisp_Object face, Lisp_Object frame)
2647 Lisp_Object lface;
2649 face = resolve_face_name (face, 1);
2651 if (!NILP (frame))
2653 CHECK_LIVE_FRAME (frame);
2654 lface = lface_from_face_name (XFRAME (frame), face, 0);
2656 else
2657 lface = lface_from_face_name (NULL, face, 0);
2659 return lface;
2663 DEFUN ("internal-copy-lisp-face", Finternal_copy_lisp_face,
2664 Sinternal_copy_lisp_face, 4, 4, 0,
2665 doc: /* Copy face FROM to TO.
2666 If FRAME is t, copy the global face definition of FROM.
2667 Otherwise, copy the frame-local definition of FROM on FRAME.
2668 If NEW-FRAME is a frame, copy that data into the frame-local
2669 definition of TO on NEW-FRAME. If NEW-FRAME is nil,
2670 FRAME controls where the data is copied to.
2672 The value is TO. */)
2673 (Lisp_Object from, Lisp_Object to, Lisp_Object frame, Lisp_Object new_frame)
2675 Lisp_Object lface, copy;
2677 CHECK_SYMBOL (from);
2678 CHECK_SYMBOL (to);
2680 if (EQ (frame, Qt))
2682 /* Copy global definition of FROM. We don't make copies of
2683 strings etc. because 20.2 didn't do it either. */
2684 lface = lface_from_face_name (NULL, from, 1);
2685 copy = Finternal_make_lisp_face (to, Qnil);
2687 else
2689 /* Copy frame-local definition of FROM. */
2690 if (NILP (new_frame))
2691 new_frame = frame;
2692 CHECK_LIVE_FRAME (frame);
2693 CHECK_LIVE_FRAME (new_frame);
2694 lface = lface_from_face_name (XFRAME (frame), from, 1);
2695 copy = Finternal_make_lisp_face (to, new_frame);
2698 vcopy (copy, 0, XVECTOR (lface)->contents, LFACE_VECTOR_SIZE);
2700 /* Changing a named face means that all realized faces depending on
2701 that face are invalid. Since we cannot tell which realized faces
2702 depend on the face, make sure they are all removed. This is done
2703 by incrementing face_change_count. The next call to
2704 init_iterator will then free realized faces. */
2705 if (NILP (Fget (to, Qface_no_inherit)))
2707 ++face_change_count;
2708 ++windows_or_buffers_changed;
2711 return to;
2715 DEFUN ("internal-set-lisp-face-attribute", Finternal_set_lisp_face_attribute,
2716 Sinternal_set_lisp_face_attribute, 3, 4, 0,
2717 doc: /* Set attribute ATTR of FACE to VALUE.
2718 FRAME being a frame means change the face on that frame.
2719 FRAME nil means change the face of the selected frame.
2720 FRAME t means change the default for new frames.
2721 FRAME 0 means change the face on all frames, and change the default
2722 for new frames. */)
2723 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
2725 Lisp_Object lface;
2726 Lisp_Object old_value = Qnil;
2727 /* Set one of enum font_property_index (> 0) if ATTR is one of
2728 font-related attributes other than QCfont and QCfontset. */
2729 enum font_property_index prop_index = 0;
2731 CHECK_SYMBOL (face);
2732 CHECK_SYMBOL (attr);
2734 face = resolve_face_name (face, 1);
2736 /* If FRAME is 0, change face on all frames, and change the
2737 default for new frames. */
2738 if (INTEGERP (frame) && XINT (frame) == 0)
2740 Lisp_Object tail;
2741 Finternal_set_lisp_face_attribute (face, attr, value, Qt);
2742 FOR_EACH_FRAME (tail, frame)
2743 Finternal_set_lisp_face_attribute (face, attr, value, frame);
2744 return face;
2747 /* Set lface to the Lisp attribute vector of FACE. */
2748 if (EQ (frame, Qt))
2750 lface = lface_from_face_name (NULL, face, 1);
2752 /* When updating face-new-frame-defaults, we put :ignore-defface
2753 where the caller wants `unspecified'. This forces the frame
2754 defaults to ignore the defface value. Otherwise, the defface
2755 will take effect, which is generally not what is intended.
2756 The value of that attribute will be inherited from some other
2757 face during face merging. See internal_merge_in_global_face. */
2758 if (UNSPECIFIEDP (value))
2759 value = QCignore_defface;
2761 else
2763 if (NILP (frame))
2764 frame = selected_frame;
2766 CHECK_LIVE_FRAME (frame);
2767 lface = lface_from_face_name (XFRAME (frame), face, 0);
2769 /* If a frame-local face doesn't exist yet, create one. */
2770 if (NILP (lface))
2771 lface = Finternal_make_lisp_face (face, frame);
2774 if (EQ (attr, QCfamily))
2776 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2778 CHECK_STRING (value);
2779 if (SCHARS (value) == 0)
2780 signal_error ("Invalid face family", value);
2782 old_value = LFACE_FAMILY (lface);
2783 ASET (lface, LFACE_FAMILY_INDEX, value);
2784 prop_index = FONT_FAMILY_INDEX;
2786 else if (EQ (attr, QCfoundry))
2788 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2790 CHECK_STRING (value);
2791 if (SCHARS (value) == 0)
2792 signal_error ("Invalid face foundry", value);
2794 old_value = LFACE_FOUNDRY (lface);
2795 ASET (lface, LFACE_FOUNDRY_INDEX, value);
2796 prop_index = FONT_FOUNDRY_INDEX;
2798 else if (EQ (attr, QCheight))
2800 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2802 if (EQ (face, Qdefault))
2804 /* The default face must have an absolute size. */
2805 if (!INTEGERP (value) || XINT (value) <= 0)
2806 signal_error ("Default face height not absolute and positive",
2807 value);
2809 else
2811 /* For non-default faces, do a test merge with a random
2812 height to see if VALUE's ok. */
2813 Lisp_Object test = merge_face_heights (value,
2814 make_number (10),
2815 Qnil);
2816 if (!INTEGERP (test) || XINT (test) <= 0)
2817 signal_error ("Face height does not produce a positive integer",
2818 value);
2822 old_value = LFACE_HEIGHT (lface);
2823 ASET (lface, LFACE_HEIGHT_INDEX, value);
2824 prop_index = FONT_SIZE_INDEX;
2826 else if (EQ (attr, QCweight))
2828 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2830 CHECK_SYMBOL (value);
2831 if (FONT_WEIGHT_NAME_NUMERIC (value) < 0)
2832 signal_error ("Invalid face weight", value);
2834 old_value = LFACE_WEIGHT (lface);
2835 ASET (lface, LFACE_WEIGHT_INDEX, value);
2836 prop_index = FONT_WEIGHT_INDEX;
2838 else if (EQ (attr, QCslant))
2840 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2842 CHECK_SYMBOL (value);
2843 if (FONT_SLANT_NAME_NUMERIC (value) < 0)
2844 signal_error ("Invalid face slant", value);
2846 old_value = LFACE_SLANT (lface);
2847 ASET (lface, LFACE_SLANT_INDEX, value);
2848 prop_index = FONT_SLANT_INDEX;
2850 else if (EQ (attr, QCunderline))
2852 bool valid_p = 0;
2854 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2855 valid_p = 1;
2856 else if (NILP (value) || EQ (value, Qt))
2857 valid_p = 1;
2858 else if (STRINGP (value) && SCHARS (value) > 0)
2859 valid_p = 1;
2860 else if (CONSP (value))
2862 Lisp_Object key, val, list;
2864 list = value;
2865 /* FIXME? This errs on the side of acceptance. Eg it accepts:
2866 (defface foo '((t :underline 'foo) "doc")
2867 Maybe this is intentional, maybe it isn't.
2868 Non-nil symbols other than t are not documented as being valid.
2869 Eg compare with inverse-video, which explicitly rejects them.
2871 valid_p = 1;
2873 while (!NILP (CAR_SAFE(list)))
2875 key = CAR_SAFE (list);
2876 list = CDR_SAFE (list);
2877 val = CAR_SAFE (list);
2878 list = CDR_SAFE (list);
2880 if (NILP (key) || NILP (val))
2882 valid_p = 0;
2883 break;
2886 else if (EQ (key, QCcolor)
2887 && !(EQ (val, Qforeground_color)
2888 || (STRINGP (val) && SCHARS (val) > 0)))
2890 valid_p = 0;
2891 break;
2894 else if (EQ (key, QCstyle)
2895 && !(EQ (val, Qline) || EQ (val, Qwave)))
2897 valid_p = 0;
2898 break;
2903 if (!valid_p)
2904 signal_error ("Invalid face underline", value);
2906 old_value = LFACE_UNDERLINE (lface);
2907 ASET (lface, LFACE_UNDERLINE_INDEX, value);
2909 else if (EQ (attr, QCoverline))
2911 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2912 if ((SYMBOLP (value)
2913 && !EQ (value, Qt)
2914 && !EQ (value, Qnil))
2915 /* Overline color. */
2916 || (STRINGP (value)
2917 && SCHARS (value) == 0))
2918 signal_error ("Invalid face overline", value);
2920 old_value = LFACE_OVERLINE (lface);
2921 ASET (lface, LFACE_OVERLINE_INDEX, value);
2923 else if (EQ (attr, QCstrike_through))
2925 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2926 if ((SYMBOLP (value)
2927 && !EQ (value, Qt)
2928 && !EQ (value, Qnil))
2929 /* Strike-through color. */
2930 || (STRINGP (value)
2931 && SCHARS (value) == 0))
2932 signal_error ("Invalid face strike-through", value);
2934 old_value = LFACE_STRIKE_THROUGH (lface);
2935 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, value);
2937 else if (EQ (attr, QCbox))
2939 bool valid_p;
2941 /* Allow t meaning a simple box of width 1 in foreground color
2942 of the face. */
2943 if (EQ (value, Qt))
2944 value = make_number (1);
2946 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2947 valid_p = 1;
2948 else if (NILP (value))
2949 valid_p = 1;
2950 else if (INTEGERP (value))
2951 valid_p = XINT (value) != 0;
2952 else if (STRINGP (value))
2953 valid_p = SCHARS (value) > 0;
2954 else if (CONSP (value))
2956 Lisp_Object tem;
2958 tem = value;
2959 while (CONSP (tem))
2961 Lisp_Object k, v;
2963 k = XCAR (tem);
2964 tem = XCDR (tem);
2965 if (!CONSP (tem))
2966 break;
2967 v = XCAR (tem);
2968 tem = XCDR (tem);
2970 if (EQ (k, QCline_width))
2972 if (!INTEGERP (v) || XINT (v) == 0)
2973 break;
2975 else if (EQ (k, QCcolor))
2977 if (!NILP (v) && (!STRINGP (v) || SCHARS (v) == 0))
2978 break;
2980 else if (EQ (k, QCstyle))
2982 if (!EQ (v, Qpressed_button) && !EQ (v, Qreleased_button))
2983 break;
2985 else
2986 break;
2989 valid_p = NILP (tem);
2991 else
2992 valid_p = 0;
2994 if (!valid_p)
2995 signal_error ("Invalid face box", value);
2997 old_value = LFACE_BOX (lface);
2998 ASET (lface, LFACE_BOX_INDEX, value);
3000 else if (EQ (attr, QCinverse_video)
3001 || EQ (attr, QCreverse_video))
3003 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3005 CHECK_SYMBOL (value);
3006 if (!EQ (value, Qt) && !NILP (value))
3007 signal_error ("Invalid inverse-video face attribute value", value);
3009 old_value = LFACE_INVERSE (lface);
3010 ASET (lface, LFACE_INVERSE_INDEX, value);
3012 else if (EQ (attr, QCforeground))
3014 /* Compatibility with 20.x. */
3015 if (NILP (value))
3016 value = Qunspecified;
3017 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3019 /* Don't check for valid color names here because it depends
3020 on the frame (display) whether the color will be valid
3021 when the face is realized. */
3022 CHECK_STRING (value);
3023 if (SCHARS (value) == 0)
3024 signal_error ("Empty foreground color value", value);
3026 old_value = LFACE_FOREGROUND (lface);
3027 ASET (lface, LFACE_FOREGROUND_INDEX, value);
3029 else if (EQ (attr, QCbackground))
3031 /* Compatibility with 20.x. */
3032 if (NILP (value))
3033 value = Qunspecified;
3034 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3036 /* Don't check for valid color names here because it depends
3037 on the frame (display) whether the color will be valid
3038 when the face is realized. */
3039 CHECK_STRING (value);
3040 if (SCHARS (value) == 0)
3041 signal_error ("Empty background color value", value);
3043 old_value = LFACE_BACKGROUND (lface);
3044 ASET (lface, LFACE_BACKGROUND_INDEX, value);
3046 else if (EQ (attr, QCstipple))
3048 #if defined (HAVE_WINDOW_SYSTEM)
3049 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3050 && !NILP (value)
3051 && NILP (Fbitmap_spec_p (value)))
3052 signal_error ("Invalid stipple attribute", value);
3053 old_value = LFACE_STIPPLE (lface);
3054 ASET (lface, LFACE_STIPPLE_INDEX, value);
3055 #endif /* HAVE_WINDOW_SYSTEM */
3057 else if (EQ (attr, QCwidth))
3059 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3061 CHECK_SYMBOL (value);
3062 if (FONT_WIDTH_NAME_NUMERIC (value) < 0)
3063 signal_error ("Invalid face width", value);
3065 old_value = LFACE_SWIDTH (lface);
3066 ASET (lface, LFACE_SWIDTH_INDEX, value);
3067 prop_index = FONT_WIDTH_INDEX;
3069 else if (EQ (attr, QCfont))
3071 #ifdef HAVE_WINDOW_SYSTEM
3072 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
3074 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3076 struct frame *f;
3078 old_value = LFACE_FONT (lface);
3079 if (! FONTP (value))
3081 if (STRINGP (value))
3083 Lisp_Object name = value;
3084 int fontset = fs_query_fontset (name, 0);
3086 if (fontset >= 0)
3087 name = fontset_ascii (fontset);
3088 value = font_spec_from_name (name);
3089 if (!FONTP (value))
3090 signal_error ("Invalid font name", name);
3092 else
3093 signal_error ("Invalid font or font-spec", value);
3095 if (EQ (frame, Qt))
3096 f = XFRAME (selected_frame);
3097 else
3098 f = XFRAME (frame);
3099 if (! FONT_OBJECT_P (value))
3101 Lisp_Object *attrs = XVECTOR (lface)->contents;
3102 Lisp_Object font_object;
3104 font_object = font_load_for_lface (f, attrs, value);
3105 if (NILP (font_object))
3106 signal_error ("Font not available", value);
3107 value = font_object;
3109 set_lface_from_font (f, lface, value, 1);
3111 else
3112 ASET (lface, LFACE_FONT_INDEX, value);
3114 #endif /* HAVE_WINDOW_SYSTEM */
3116 else if (EQ (attr, QCfontset))
3118 #ifdef HAVE_WINDOW_SYSTEM
3119 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
3121 Lisp_Object tmp;
3123 old_value = LFACE_FONTSET (lface);
3124 tmp = Fquery_fontset (value, Qnil);
3125 if (NILP (tmp))
3126 signal_error ("Invalid fontset name", value);
3127 ASET (lface, LFACE_FONTSET_INDEX, value = tmp);
3129 #endif /* HAVE_WINDOW_SYSTEM */
3131 else if (EQ (attr, QCinherit))
3133 Lisp_Object tail;
3134 if (SYMBOLP (value))
3135 tail = Qnil;
3136 else
3137 for (tail = value; CONSP (tail); tail = XCDR (tail))
3138 if (!SYMBOLP (XCAR (tail)))
3139 break;
3140 if (NILP (tail))
3141 ASET (lface, LFACE_INHERIT_INDEX, value);
3142 else
3143 signal_error ("Invalid face inheritance", value);
3145 else if (EQ (attr, QCbold))
3147 old_value = LFACE_WEIGHT (lface);
3148 ASET (lface, LFACE_WEIGHT_INDEX, NILP (value) ? Qnormal : Qbold);
3149 prop_index = FONT_WEIGHT_INDEX;
3151 else if (EQ (attr, QCitalic))
3153 attr = QCslant;
3154 old_value = LFACE_SLANT (lface);
3155 ASET (lface, LFACE_SLANT_INDEX, NILP (value) ? Qnormal : Qitalic);
3156 prop_index = FONT_SLANT_INDEX;
3158 else
3159 signal_error ("Invalid face attribute name", attr);
3161 if (prop_index)
3163 /* If a font-related attribute other than QCfont and QCfontset
3164 is specified, and if the original QCfont attribute has a font
3165 (font-spec or font-object), set the corresponding property in
3166 the font to nil so that the font selector doesn't think that
3167 the attribute is mandatory. Also, clear the average
3168 width. */
3169 font_clear_prop (XVECTOR (lface)->contents, prop_index);
3172 /* Changing a named face means that all realized faces depending on
3173 that face are invalid. Since we cannot tell which realized faces
3174 depend on the face, make sure they are all removed. This is done
3175 by incrementing face_change_count. The next call to
3176 init_iterator will then free realized faces. */
3177 if (!EQ (frame, Qt)
3178 && NILP (Fget (face, Qface_no_inherit))
3179 && NILP (Fequal (old_value, value)))
3181 ++face_change_count;
3182 ++windows_or_buffers_changed;
3185 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3186 && NILP (Fequal (old_value, value)))
3188 Lisp_Object param;
3190 param = Qnil;
3192 if (EQ (face, Qdefault))
3194 #ifdef HAVE_WINDOW_SYSTEM
3195 /* Changed font-related attributes of the `default' face are
3196 reflected in changed `font' frame parameters. */
3197 if (FRAMEP (frame)
3198 && (prop_index || EQ (attr, QCfont))
3199 && lface_fully_specified_p (XVECTOR (lface)->contents))
3200 set_font_frame_param (frame, lface);
3201 else
3202 #endif /* HAVE_WINDOW_SYSTEM */
3204 if (EQ (attr, QCforeground))
3205 param = Qforeground_color;
3206 else if (EQ (attr, QCbackground))
3207 param = Qbackground_color;
3209 #ifdef HAVE_WINDOW_SYSTEM
3210 #ifndef HAVE_NTGUI
3211 else if (EQ (face, Qscroll_bar))
3213 /* Changing the colors of `scroll-bar' sets frame parameters
3214 `scroll-bar-foreground' and `scroll-bar-background'. */
3215 if (EQ (attr, QCforeground))
3216 param = Qscroll_bar_foreground;
3217 else if (EQ (attr, QCbackground))
3218 param = Qscroll_bar_background;
3220 #endif /* not HAVE_NTGUI */
3221 else if (EQ (face, Qborder))
3223 /* Changing background color of `border' sets frame parameter
3224 `border-color'. */
3225 if (EQ (attr, QCbackground))
3226 param = Qborder_color;
3228 else if (EQ (face, Qcursor))
3230 /* Changing background color of `cursor' sets frame parameter
3231 `cursor-color'. */
3232 if (EQ (attr, QCbackground))
3233 param = Qcursor_color;
3235 else if (EQ (face, Qmouse))
3237 /* Changing background color of `mouse' sets frame parameter
3238 `mouse-color'. */
3239 if (EQ (attr, QCbackground))
3240 param = Qmouse_color;
3242 #endif /* HAVE_WINDOW_SYSTEM */
3243 else if (EQ (face, Qmenu))
3245 /* Indicate that we have to update the menu bar when realizing
3246 faces on FRAME. FRAME t change the default for new frames.
3247 We do this by setting the flag in new face caches. */
3248 if (FRAMEP (frame))
3250 struct frame *f = XFRAME (frame);
3251 if (FRAME_FACE_CACHE (f) == NULL)
3252 FRAME_FACE_CACHE (f) = make_face_cache (f);
3253 FRAME_FACE_CACHE (f)->menu_face_changed_p = 1;
3255 else
3256 menu_face_changed_default = 1;
3259 if (!NILP (param))
3261 if (EQ (frame, Qt))
3262 /* Update `default-frame-alist', which is used for new frames. */
3264 store_in_alist (&Vdefault_frame_alist, param, value);
3266 else
3267 /* Update the current frame's parameters. */
3269 Lisp_Object cons;
3270 cons = XCAR (Vparam_value_alist);
3271 XSETCAR (cons, param);
3272 XSETCDR (cons, value);
3273 Fmodify_frame_parameters (frame, Vparam_value_alist);
3278 return face;
3282 /* Update the corresponding face when frame parameter PARAM on frame F
3283 has been assigned the value NEW_VALUE. */
3285 void
3286 update_face_from_frame_parameter (struct frame *f, Lisp_Object param,
3287 Lisp_Object new_value)
3289 Lisp_Object face = Qnil;
3290 Lisp_Object lface;
3292 /* If there are no faces yet, give up. This is the case when called
3293 from Fx_create_frame, and we do the necessary things later in
3294 face-set-after-frame-defaults. */
3295 if (NILP (f->face_alist))
3296 return;
3298 if (EQ (param, Qforeground_color))
3300 face = Qdefault;
3301 lface = lface_from_face_name (f, face, 1);
3302 ASET (lface, LFACE_FOREGROUND_INDEX,
3303 (STRINGP (new_value) ? new_value : Qunspecified));
3304 realize_basic_faces (f);
3306 else if (EQ (param, Qbackground_color))
3308 Lisp_Object frame;
3310 /* Changing the background color might change the background
3311 mode, so that we have to load new defface specs.
3312 Call frame-set-background-mode to do that. */
3313 XSETFRAME (frame, f);
3314 call1 (Qframe_set_background_mode, frame);
3316 face = Qdefault;
3317 lface = lface_from_face_name (f, face, 1);
3318 ASET (lface, LFACE_BACKGROUND_INDEX,
3319 (STRINGP (new_value) ? new_value : Qunspecified));
3320 realize_basic_faces (f);
3322 #ifdef HAVE_WINDOW_SYSTEM
3323 else if (EQ (param, Qborder_color))
3325 face = Qborder;
3326 lface = lface_from_face_name (f, face, 1);
3327 ASET (lface, LFACE_BACKGROUND_INDEX,
3328 (STRINGP (new_value) ? new_value : Qunspecified));
3330 else if (EQ (param, Qcursor_color))
3332 face = Qcursor;
3333 lface = lface_from_face_name (f, face, 1);
3334 ASET (lface, LFACE_BACKGROUND_INDEX,
3335 (STRINGP (new_value) ? new_value : Qunspecified));
3337 else if (EQ (param, Qmouse_color))
3339 face = Qmouse;
3340 lface = lface_from_face_name (f, face, 1);
3341 ASET (lface, LFACE_BACKGROUND_INDEX,
3342 (STRINGP (new_value) ? new_value : Qunspecified));
3344 #endif
3346 /* Changing a named face means that all realized faces depending on
3347 that face are invalid. Since we cannot tell which realized faces
3348 depend on the face, make sure they are all removed. This is done
3349 by incrementing face_change_count. The next call to
3350 init_iterator will then free realized faces. */
3351 if (!NILP (face)
3352 && NILP (Fget (face, Qface_no_inherit)))
3354 ++face_change_count;
3355 ++windows_or_buffers_changed;
3360 #ifdef HAVE_WINDOW_SYSTEM
3362 /* Set the `font' frame parameter of FRAME determined from the
3363 font-object set in `default' face attributes LFACE. */
3365 static void
3366 set_font_frame_param (Lisp_Object frame, Lisp_Object lface)
3368 struct frame *f = XFRAME (frame);
3369 Lisp_Object font;
3371 if (FRAME_WINDOW_P (f)
3372 /* Don't do anything if the font is `unspecified'. This can
3373 happen during frame creation. */
3374 && (font = LFACE_FONT (lface),
3375 ! UNSPECIFIEDP (font)))
3377 if (FONT_SPEC_P (font))
3379 font = font_load_for_lface (f, XVECTOR (lface)->contents, font);
3380 if (NILP (font))
3381 return;
3382 ASET (lface, LFACE_FONT_INDEX, font);
3384 f->default_face_done_p = 0;
3385 Fmodify_frame_parameters (frame, list1 (Fcons (Qfont, font)));
3389 DEFUN ("internal-face-x-get-resource", Finternal_face_x_get_resource,
3390 Sinternal_face_x_get_resource, 2, 3, 0,
3391 doc: /* Get the value of X resource RESOURCE, class CLASS.
3392 Returned value is for the display of frame FRAME. If FRAME is not
3393 specified or nil, use selected frame. This function exists because
3394 ordinary `x-get-resource' doesn't take a frame argument. */)
3395 (Lisp_Object resource, Lisp_Object class, Lisp_Object frame)
3397 Lisp_Object value = Qnil;
3398 struct frame *f;
3400 CHECK_STRING (resource);
3401 CHECK_STRING (class);
3402 f = decode_live_frame (frame);
3403 block_input ();
3404 value = display_x_get_resource (FRAME_X_DISPLAY_INFO (f),
3405 resource, class, Qnil, Qnil);
3406 unblock_input ();
3407 return value;
3411 /* Return resource string VALUE as a boolean value, i.e. nil, or t.
3412 If VALUE is "on" or "true", return t. If VALUE is "off" or
3413 "false", return nil. Otherwise, if SIGNAL_P is non-zero, signal an
3414 error; if SIGNAL_P is zero, return 0. */
3416 static Lisp_Object
3417 face_boolean_x_resource_value (Lisp_Object value, int signal_p)
3419 Lisp_Object result = make_number (0);
3421 eassert (STRINGP (value));
3423 if (xstrcasecmp (SSDATA (value), "on") == 0
3424 || xstrcasecmp (SSDATA (value), "true") == 0)
3425 result = Qt;
3426 else if (xstrcasecmp (SSDATA (value), "off") == 0
3427 || xstrcasecmp (SSDATA (value), "false") == 0)
3428 result = Qnil;
3429 else if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3430 result = Qunspecified;
3431 else if (signal_p)
3432 signal_error ("Invalid face attribute value from X resource", value);
3434 return result;
3438 DEFUN ("internal-set-lisp-face-attribute-from-resource",
3439 Finternal_set_lisp_face_attribute_from_resource,
3440 Sinternal_set_lisp_face_attribute_from_resource,
3441 3, 4, 0, doc: /* */)
3442 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
3444 CHECK_SYMBOL (face);
3445 CHECK_SYMBOL (attr);
3446 CHECK_STRING (value);
3448 if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3449 value = Qunspecified;
3450 else if (EQ (attr, QCheight))
3452 value = Fstring_to_number (value, make_number (10));
3453 if (XINT (value) <= 0)
3454 signal_error ("Invalid face height from X resource", value);
3456 else if (EQ (attr, QCbold) || EQ (attr, QCitalic))
3457 value = face_boolean_x_resource_value (value, 1);
3458 else if (EQ (attr, QCweight) || EQ (attr, QCslant) || EQ (attr, QCwidth))
3459 value = intern (SSDATA (value));
3460 else if (EQ (attr, QCreverse_video) || EQ (attr, QCinverse_video))
3461 value = face_boolean_x_resource_value (value, 1);
3462 else if (EQ (attr, QCunderline)
3463 || EQ (attr, QCoverline)
3464 || EQ (attr, QCstrike_through))
3466 Lisp_Object boolean_value;
3468 /* If the result of face_boolean_x_resource_value is t or nil,
3469 VALUE does NOT specify a color. */
3470 boolean_value = face_boolean_x_resource_value (value, 0);
3471 if (SYMBOLP (boolean_value))
3472 value = boolean_value;
3474 else if (EQ (attr, QCbox) || EQ (attr, QCinherit))
3475 value = Fcar (Fread_from_string (value, Qnil, Qnil));
3477 return Finternal_set_lisp_face_attribute (face, attr, value, frame);
3480 #endif /* HAVE_WINDOW_SYSTEM */
3483 /***********************************************************************
3484 Menu face
3485 ***********************************************************************/
3487 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
3489 /* Make menus on frame F appear as specified by the `menu' face. */
3491 static void
3492 x_update_menu_appearance (struct frame *f)
3494 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3495 XrmDatabase rdb;
3497 if (dpyinfo
3498 && (rdb = XrmGetDatabase (FRAME_X_DISPLAY (f)),
3499 rdb != NULL))
3501 char line[512];
3502 char *buf = line;
3503 ptrdiff_t bufsize = sizeof line;
3504 Lisp_Object lface = lface_from_face_name (f, Qmenu, 1);
3505 struct face *face = FACE_FROM_ID (f, MENU_FACE_ID);
3506 const char *myname = SSDATA (Vx_resource_name);
3507 bool changed_p = 0;
3508 #ifdef USE_MOTIF
3509 const char *popup_path = "popup_menu";
3510 #else
3511 const char *popup_path = "menu.popup";
3512 #endif
3514 if (STRINGP (LFACE_FOREGROUND (lface)))
3516 exprintf (&buf, &bufsize, line, -1, "%s.%s*foreground: %s",
3517 myname, popup_path,
3518 SDATA (LFACE_FOREGROUND (lface)));
3519 XrmPutLineResource (&rdb, line);
3520 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*foreground: %s",
3521 myname, SDATA (LFACE_FOREGROUND (lface)));
3522 XrmPutLineResource (&rdb, line);
3523 changed_p = 1;
3526 if (STRINGP (LFACE_BACKGROUND (lface)))
3528 exprintf (&buf, &bufsize, line, -1, "%s.%s*background: %s",
3529 myname, popup_path,
3530 SDATA (LFACE_BACKGROUND (lface)));
3531 XrmPutLineResource (&rdb, line);
3533 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*background: %s",
3534 myname, SDATA (LFACE_BACKGROUND (lface)));
3535 XrmPutLineResource (&rdb, line);
3536 changed_p = 1;
3539 if (face->font
3540 /* On Solaris 5.8, it's been reported that the `menu' face
3541 can be unspecified here, during startup. Why this
3542 happens remains unknown. -- cyd */
3543 && FONTP (LFACE_FONT (lface))
3544 && (!UNSPECIFIEDP (LFACE_FAMILY (lface))
3545 || !UNSPECIFIEDP (LFACE_FOUNDRY (lface))
3546 || !UNSPECIFIEDP (LFACE_SWIDTH (lface))
3547 || !UNSPECIFIEDP (LFACE_WEIGHT (lface))
3548 || !UNSPECIFIEDP (LFACE_SLANT (lface))
3549 || !UNSPECIFIEDP (LFACE_HEIGHT (lface))))
3551 Lisp_Object xlfd = Ffont_xlfd_name (LFACE_FONT (lface), Qnil);
3552 #ifdef USE_MOTIF
3553 const char *suffix = "List";
3554 Bool motif = True;
3555 #else
3556 #if defined HAVE_X_I18N
3558 const char *suffix = "Set";
3559 #else
3560 const char *suffix = "";
3561 #endif
3562 Bool motif = False;
3563 #endif
3565 if (! NILP (xlfd))
3567 #if defined HAVE_X_I18N
3568 char *fontsetname = xic_create_fontsetname (SSDATA (xlfd), motif);
3569 #else
3570 char *fontsetname = SSDATA (xlfd);
3571 #endif
3572 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*font%s: %s",
3573 myname, suffix, fontsetname);
3574 XrmPutLineResource (&rdb, line);
3576 exprintf (&buf, &bufsize, line, -1, "%s.%s*font%s: %s",
3577 myname, popup_path, suffix, fontsetname);
3578 XrmPutLineResource (&rdb, line);
3579 changed_p = 1;
3580 if (fontsetname != SSDATA (xlfd))
3581 xfree (fontsetname);
3585 if (changed_p && f->output_data.x->menubar_widget)
3586 free_frame_menubar (f);
3588 if (buf != line)
3589 xfree (buf);
3593 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
3596 DEFUN ("face-attribute-relative-p", Fface_attribute_relative_p,
3597 Sface_attribute_relative_p,
3598 2, 2, 0,
3599 doc: /* Check whether a face attribute value is relative.
3600 Specifically, this function returns t if the attribute ATTRIBUTE
3601 with the value VALUE is relative.
3603 A relative value is one that doesn't entirely override whatever is
3604 inherited from another face. For most possible attributes,
3605 the only relative value that users see is `unspecified'.
3606 However, for :height, floating point values are also relative. */)
3607 (Lisp_Object attribute, Lisp_Object value)
3609 if (EQ (value, Qunspecified) || (EQ (value, QCignore_defface)))
3610 return Qt;
3611 else if (EQ (attribute, QCheight))
3612 return INTEGERP (value) ? Qnil : Qt;
3613 else
3614 return Qnil;
3617 DEFUN ("merge-face-attribute", Fmerge_face_attribute, Smerge_face_attribute,
3618 3, 3, 0,
3619 doc: /* Return face ATTRIBUTE VALUE1 merged with VALUE2.
3620 If VALUE1 or VALUE2 are absolute (see `face-attribute-relative-p'), then
3621 the result will be absolute, otherwise it will be relative. */)
3622 (Lisp_Object attribute, Lisp_Object value1, Lisp_Object value2)
3624 if (EQ (value1, Qunspecified) || EQ (value1, QCignore_defface))
3625 return value2;
3626 else if (EQ (attribute, QCheight))
3627 return merge_face_heights (value1, value2, value1);
3628 else
3629 return value1;
3633 DEFUN ("internal-get-lisp-face-attribute", Finternal_get_lisp_face_attribute,
3634 Sinternal_get_lisp_face_attribute,
3635 2, 3, 0,
3636 doc: /* Return face attribute KEYWORD of face SYMBOL.
3637 If SYMBOL does not name a valid Lisp face or KEYWORD isn't a valid
3638 face attribute name, signal an error.
3639 If the optional argument FRAME is given, report on face SYMBOL in that
3640 frame. If FRAME is t, report on the defaults for face SYMBOL (for new
3641 frames). If FRAME is omitted or nil, use the selected frame. */)
3642 (Lisp_Object symbol, Lisp_Object keyword, Lisp_Object frame)
3644 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3645 Lisp_Object lface = lface_from_face_name (f, symbol, 1), value = Qnil;
3647 CHECK_SYMBOL (symbol);
3648 CHECK_SYMBOL (keyword);
3650 if (EQ (keyword, QCfamily))
3651 value = LFACE_FAMILY (lface);
3652 else if (EQ (keyword, QCfoundry))
3653 value = LFACE_FOUNDRY (lface);
3654 else if (EQ (keyword, QCheight))
3655 value = LFACE_HEIGHT (lface);
3656 else if (EQ (keyword, QCweight))
3657 value = LFACE_WEIGHT (lface);
3658 else if (EQ (keyword, QCslant))
3659 value = LFACE_SLANT (lface);
3660 else if (EQ (keyword, QCunderline))
3661 value = LFACE_UNDERLINE (lface);
3662 else if (EQ (keyword, QCoverline))
3663 value = LFACE_OVERLINE (lface);
3664 else if (EQ (keyword, QCstrike_through))
3665 value = LFACE_STRIKE_THROUGH (lface);
3666 else if (EQ (keyword, QCbox))
3667 value = LFACE_BOX (lface);
3668 else if (EQ (keyword, QCinverse_video)
3669 || EQ (keyword, QCreverse_video))
3670 value = LFACE_INVERSE (lface);
3671 else if (EQ (keyword, QCforeground))
3672 value = LFACE_FOREGROUND (lface);
3673 else if (EQ (keyword, QCbackground))
3674 value = LFACE_BACKGROUND (lface);
3675 else if (EQ (keyword, QCstipple))
3676 value = LFACE_STIPPLE (lface);
3677 else if (EQ (keyword, QCwidth))
3678 value = LFACE_SWIDTH (lface);
3679 else if (EQ (keyword, QCinherit))
3680 value = LFACE_INHERIT (lface);
3681 else if (EQ (keyword, QCfont))
3682 value = LFACE_FONT (lface);
3683 else if (EQ (keyword, QCfontset))
3684 value = LFACE_FONTSET (lface);
3685 else
3686 signal_error ("Invalid face attribute name", keyword);
3688 if (IGNORE_DEFFACE_P (value))
3689 return Qunspecified;
3691 return value;
3695 DEFUN ("internal-lisp-face-attribute-values",
3696 Finternal_lisp_face_attribute_values,
3697 Sinternal_lisp_face_attribute_values, 1, 1, 0,
3698 doc: /* Return a list of valid discrete values for face attribute ATTR.
3699 Value is nil if ATTR doesn't have a discrete set of valid values. */)
3700 (Lisp_Object attr)
3702 Lisp_Object result = Qnil;
3704 CHECK_SYMBOL (attr);
3706 if (EQ (attr, QCunderline) || EQ (attr, QCoverline)
3707 || EQ (attr, QCstrike_through)
3708 || EQ (attr, QCinverse_video) || EQ (attr, QCreverse_video))
3709 result = list2 (Qt, Qnil);
3711 return result;
3715 DEFUN ("internal-merge-in-global-face", Finternal_merge_in_global_face,
3716 Sinternal_merge_in_global_face, 2, 2, 0,
3717 doc: /* Add attributes from frame-default definition of FACE to FACE on FRAME.
3718 Default face attributes override any local face attributes. */)
3719 (Lisp_Object face, Lisp_Object frame)
3721 int i;
3722 Lisp_Object global_lface, local_lface, *gvec, *lvec;
3723 struct frame *f = XFRAME (frame);
3725 CHECK_LIVE_FRAME (frame);
3726 global_lface = lface_from_face_name (NULL, face, 1);
3727 local_lface = lface_from_face_name (f, face, 0);
3728 if (NILP (local_lface))
3729 local_lface = Finternal_make_lisp_face (face, frame);
3731 /* Make every specified global attribute override the local one.
3732 BEWARE!! This is only used from `face-set-after-frame-default' where
3733 the local frame is defined from default specs in `face-defface-spec'
3734 and those should be overridden by global settings. Hence the strange
3735 "global before local" priority. */
3736 lvec = XVECTOR (local_lface)->contents;
3737 gvec = XVECTOR (global_lface)->contents;
3738 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3739 if (IGNORE_DEFFACE_P (gvec[i]))
3740 ASET (local_lface, i, Qunspecified);
3741 else if (! UNSPECIFIEDP (gvec[i]))
3742 ASET (local_lface, i, AREF (global_lface, i));
3744 /* If the default face was changed, update the face cache and the
3745 `font' frame parameter. */
3746 if (EQ (face, Qdefault))
3748 struct face_cache *c = FRAME_FACE_CACHE (f);
3749 struct face *newface, *oldface = FACE_FROM_ID (f, DEFAULT_FACE_ID);
3750 Lisp_Object attrs[LFACE_VECTOR_SIZE];
3752 /* This can be NULL (e.g., in batch mode). */
3753 if (oldface)
3755 /* Ensure that the face vector is fully specified by merging
3756 the previously-cached vector. */
3757 memcpy (attrs, oldface->lface, sizeof attrs);
3758 merge_face_vectors (f, lvec, attrs, 0);
3759 vcopy (local_lface, 0, attrs, LFACE_VECTOR_SIZE);
3760 newface = realize_face (c, lvec, DEFAULT_FACE_ID);
3762 if ((! UNSPECIFIEDP (gvec[LFACE_FAMILY_INDEX])
3763 || ! UNSPECIFIEDP (gvec[LFACE_FOUNDRY_INDEX])
3764 || ! UNSPECIFIEDP (gvec[LFACE_HEIGHT_INDEX])
3765 || ! UNSPECIFIEDP (gvec[LFACE_WEIGHT_INDEX])
3766 || ! UNSPECIFIEDP (gvec[LFACE_SLANT_INDEX])
3767 || ! UNSPECIFIEDP (gvec[LFACE_SWIDTH_INDEX])
3768 || ! UNSPECIFIEDP (gvec[LFACE_FONT_INDEX]))
3769 && newface->font)
3771 Lisp_Object name = newface->font->props[FONT_NAME_INDEX];
3772 Fmodify_frame_parameters (frame, list1 (Fcons (Qfont, name)));
3775 if (STRINGP (gvec[LFACE_FOREGROUND_INDEX]))
3776 Fmodify_frame_parameters (frame,
3777 list1 (Fcons (Qforeground_color,
3778 gvec[LFACE_FOREGROUND_INDEX])));
3780 if (STRINGP (gvec[LFACE_BACKGROUND_INDEX]))
3781 Fmodify_frame_parameters (frame,
3782 list1 (Fcons (Qbackground_color,
3783 gvec[LFACE_BACKGROUND_INDEX])));
3787 return Qnil;
3791 /* The following function is implemented for compatibility with 20.2.
3792 The function is used in x-resolve-fonts when it is asked to
3793 return fonts with the same size as the font of a face. This is
3794 done in fontset.el. */
3796 DEFUN ("face-font", Fface_font, Sface_font, 1, 3, 0,
3797 doc: /* Return the font name of face FACE, or nil if it is unspecified.
3798 The font name is, by default, for ASCII characters.
3799 If the optional argument FRAME is given, report on face FACE in that frame.
3800 If FRAME is t, report on the defaults for face FACE (for new frames).
3801 The font default for a face is either nil, or a list
3802 of the form (bold), (italic) or (bold italic).
3803 If FRAME is omitted or nil, use the selected frame. And, in this case,
3804 if the optional third argument CHARACTER is given,
3805 return the font name used for CHARACTER. */)
3806 (Lisp_Object face, Lisp_Object frame, Lisp_Object character)
3808 if (EQ (frame, Qt))
3810 Lisp_Object result = Qnil;
3811 Lisp_Object lface = lface_from_face_name (NULL, face, 1);
3813 if (!UNSPECIFIEDP (LFACE_WEIGHT (lface))
3814 && !EQ (LFACE_WEIGHT (lface), Qnormal))
3815 result = Fcons (Qbold, result);
3817 if (!UNSPECIFIEDP (LFACE_SLANT (lface))
3818 && !EQ (LFACE_SLANT (lface), Qnormal))
3819 result = Fcons (Qitalic, result);
3821 return result;
3823 else
3825 struct frame *f = decode_live_frame (frame);
3826 int face_id = lookup_named_face (f, face, 1);
3827 struct face *fface = FACE_FROM_ID (f, face_id);
3829 if (! fface)
3830 return Qnil;
3831 #ifdef HAVE_WINDOW_SYSTEM
3832 if (FRAME_WINDOW_P (f) && !NILP (character))
3834 CHECK_CHARACTER (character);
3835 face_id = FACE_FOR_CHAR (f, fface, XINT (character), -1, Qnil);
3836 fface = FACE_FROM_ID (f, face_id);
3838 return (fface->font
3839 ? fface->font->props[FONT_NAME_INDEX]
3840 : Qnil);
3841 #else /* !HAVE_WINDOW_SYSTEM */
3842 return build_string (FRAME_MSDOS_P (f)
3843 ? "ms-dos"
3844 : FRAME_W32_P (f) ? "w32term"
3845 :"tty");
3846 #endif
3851 /* Compare face-attribute values v1 and v2 for equality. Value is non-zero if
3852 all attributes are `equal'. Tries to be fast because this function
3853 is called quite often. */
3855 static bool
3856 face_attr_equal_p (Lisp_Object v1, Lisp_Object v2)
3858 /* Type can differ, e.g. when one attribute is unspecified, i.e. nil,
3859 and the other is specified. */
3860 if (XTYPE (v1) != XTYPE (v2))
3861 return 0;
3863 if (EQ (v1, v2))
3864 return 1;
3866 switch (XTYPE (v1))
3868 case Lisp_String:
3869 if (SBYTES (v1) != SBYTES (v2))
3870 return 0;
3872 return memcmp (SDATA (v1), SDATA (v2), SBYTES (v1)) == 0;
3874 case_Lisp_Int:
3875 case Lisp_Symbol:
3876 return 0;
3878 default:
3879 return !NILP (Fequal (v1, v2));
3884 /* Compare face vectors V1 and V2 for equality. Value is non-zero if
3885 all attributes are `equal'. Tries to be fast because this function
3886 is called quite often. */
3888 static bool
3889 lface_equal_p (Lisp_Object *v1, Lisp_Object *v2)
3891 int i;
3892 bool equal_p = 1;
3894 for (i = 1; i < LFACE_VECTOR_SIZE && equal_p; ++i)
3895 equal_p = face_attr_equal_p (v1[i], v2[i]);
3897 return equal_p;
3901 DEFUN ("internal-lisp-face-equal-p", Finternal_lisp_face_equal_p,
3902 Sinternal_lisp_face_equal_p, 2, 3, 0,
3903 doc: /* True if FACE1 and FACE2 are equal.
3904 If the optional argument FRAME is given, report on FACE1 and FACE2 in that frame.
3905 If FRAME is t, report on the defaults for FACE1 and FACE2 (for new frames).
3906 If FRAME is omitted or nil, use the selected frame. */)
3907 (Lisp_Object face1, Lisp_Object face2, Lisp_Object frame)
3909 int equal_p;
3910 struct frame *f;
3911 Lisp_Object lface1, lface2;
3913 /* Don't use decode_window_system_frame here because this function
3914 is called before X frames exist. At that time, if FRAME is nil,
3915 selected_frame will be used which is the frame dumped with
3916 Emacs. That frame is not an X frame. */
3917 f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3919 lface1 = lface_from_face_name (f, face1, 1);
3920 lface2 = lface_from_face_name (f, face2, 1);
3921 equal_p = lface_equal_p (XVECTOR (lface1)->contents,
3922 XVECTOR (lface2)->contents);
3923 return equal_p ? Qt : Qnil;
3927 DEFUN ("internal-lisp-face-empty-p", Finternal_lisp_face_empty_p,
3928 Sinternal_lisp_face_empty_p, 1, 2, 0,
3929 doc: /* True if FACE has no attribute specified.
3930 If the optional argument FRAME is given, report on face FACE in that frame.
3931 If FRAME is t, report on the defaults for face FACE (for new frames).
3932 If FRAME is omitted or nil, use the selected frame. */)
3933 (Lisp_Object face, Lisp_Object frame)
3935 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3936 Lisp_Object lface = lface_from_face_name (f, face, 1);
3937 int i;
3939 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3940 if (!UNSPECIFIEDP (AREF (lface, i)))
3941 break;
3943 return i == LFACE_VECTOR_SIZE ? Qt : Qnil;
3947 DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist,
3948 0, 1, 0,
3949 doc: /* Return an alist of frame-local faces defined on FRAME.
3950 For internal use only. */)
3951 (Lisp_Object frame)
3953 return decode_live_frame (frame)->face_alist;
3957 /* Return a hash code for Lisp string STRING with case ignored. Used
3958 below in computing a hash value for a Lisp face. */
3960 static unsigned
3961 hash_string_case_insensitive (Lisp_Object string)
3963 const unsigned char *s;
3964 unsigned hash = 0;
3965 eassert (STRINGP (string));
3966 for (s = SDATA (string); *s; ++s)
3967 hash = (hash << 1) ^ c_tolower (*s);
3968 return hash;
3972 /* Return a hash code for face attribute vector V. */
3974 static unsigned
3975 lface_hash (Lisp_Object *v)
3977 return (hash_string_case_insensitive (v[LFACE_FAMILY_INDEX])
3978 ^ hash_string_case_insensitive (v[LFACE_FOUNDRY_INDEX])
3979 ^ hash_string_case_insensitive (v[LFACE_FOREGROUND_INDEX])
3980 ^ hash_string_case_insensitive (v[LFACE_BACKGROUND_INDEX])
3981 ^ XHASH (v[LFACE_WEIGHT_INDEX])
3982 ^ XHASH (v[LFACE_SLANT_INDEX])
3983 ^ XHASH (v[LFACE_SWIDTH_INDEX])
3984 ^ XHASH (v[LFACE_HEIGHT_INDEX]));
3988 /* Return non-zero if LFACE1 and LFACE2 specify the same font (without
3989 considering charsets/registries). They do if they specify the same
3990 family, point size, weight, width, slant, and font. Both
3991 LFACE1 and LFACE2 must be fully-specified. */
3993 static int
3994 lface_same_font_attributes_p (Lisp_Object *lface1, Lisp_Object *lface2)
3996 eassert (lface_fully_specified_p (lface1)
3997 && lface_fully_specified_p (lface2));
3998 return (xstrcasecmp (SSDATA (lface1[LFACE_FAMILY_INDEX]),
3999 SSDATA (lface2[LFACE_FAMILY_INDEX])) == 0
4000 && xstrcasecmp (SSDATA (lface1[LFACE_FOUNDRY_INDEX]),
4001 SSDATA (lface2[LFACE_FOUNDRY_INDEX])) == 0
4002 && EQ (lface1[LFACE_HEIGHT_INDEX], lface2[LFACE_HEIGHT_INDEX])
4003 && EQ (lface1[LFACE_SWIDTH_INDEX], lface2[LFACE_SWIDTH_INDEX])
4004 && EQ (lface1[LFACE_WEIGHT_INDEX], lface2[LFACE_WEIGHT_INDEX])
4005 && EQ (lface1[LFACE_SLANT_INDEX], lface2[LFACE_SLANT_INDEX])
4006 && EQ (lface1[LFACE_FONT_INDEX], lface2[LFACE_FONT_INDEX])
4007 && (EQ (lface1[LFACE_FONTSET_INDEX], lface2[LFACE_FONTSET_INDEX])
4008 || (STRINGP (lface1[LFACE_FONTSET_INDEX])
4009 && STRINGP (lface2[LFACE_FONTSET_INDEX])
4010 && ! xstrcasecmp (SSDATA (lface1[LFACE_FONTSET_INDEX]),
4011 SSDATA (lface2[LFACE_FONTSET_INDEX]))))
4017 /***********************************************************************
4018 Realized Faces
4019 ***********************************************************************/
4021 /* Allocate and return a new realized face for Lisp face attribute
4022 vector ATTR. */
4024 static struct face *
4025 make_realized_face (Lisp_Object *attr)
4027 struct face *face = xzalloc (sizeof *face);
4028 face->ascii_face = face;
4029 memcpy (face->lface, attr, sizeof face->lface);
4030 return face;
4034 /* Free realized face FACE, including its X resources. FACE may
4035 be null. */
4037 static void
4038 free_realized_face (struct frame *f, struct face *face)
4040 if (face)
4042 #ifdef HAVE_WINDOW_SYSTEM
4043 if (FRAME_WINDOW_P (f))
4045 /* Free fontset of FACE if it is ASCII face. */
4046 if (face->fontset >= 0 && face == face->ascii_face)
4047 free_face_fontset (f, face);
4048 if (face->gc)
4050 block_input ();
4051 if (face->font)
4052 font_done_for_face (f, face);
4053 x_free_gc (f, face->gc);
4054 face->gc = 0;
4055 unblock_input ();
4058 free_face_colors (f, face);
4059 x_destroy_bitmap (f, face->stipple);
4061 #endif /* HAVE_WINDOW_SYSTEM */
4063 xfree (face);
4068 /* Prepare face FACE for subsequent display on frame F. This
4069 allocated GCs if they haven't been allocated yet or have been freed
4070 by clearing the face cache. */
4072 void
4073 prepare_face_for_display (struct frame *f, struct face *face)
4075 #ifdef HAVE_WINDOW_SYSTEM
4076 eassert (FRAME_WINDOW_P (f));
4078 if (face->gc == 0)
4080 XGCValues xgcv;
4081 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
4083 xgcv.foreground = face->foreground;
4084 xgcv.background = face->background;
4085 #ifdef HAVE_X_WINDOWS
4086 xgcv.graphics_exposures = False;
4087 #endif
4089 block_input ();
4090 #ifdef HAVE_X_WINDOWS
4091 if (face->stipple)
4093 xgcv.fill_style = FillOpaqueStippled;
4094 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
4095 mask |= GCFillStyle | GCStipple;
4097 #endif
4098 face->gc = x_create_gc (f, mask, &xgcv);
4099 if (face->font)
4100 font_prepare_for_face (f, face);
4101 unblock_input ();
4103 #endif /* HAVE_WINDOW_SYSTEM */
4107 /* Returns the `distance' between the colors X and Y. */
4109 static int
4110 color_distance (XColor *x, XColor *y)
4112 /* This formula is from a paper titled `Colour metric' by Thiadmer Riemersma.
4113 Quoting from that paper:
4115 This formula has results that are very close to L*u*v* (with the
4116 modified lightness curve) and, more importantly, it is a more even
4117 algorithm: it does not have a range of colors where it suddenly
4118 gives far from optimal results.
4120 See <http://www.compuphase.com/cmetric.htm> for more info. */
4122 long r = (x->red - y->red) >> 8;
4123 long g = (x->green - y->green) >> 8;
4124 long b = (x->blue - y->blue) >> 8;
4125 long r_mean = (x->red + y->red) >> 9;
4127 return
4128 (((512 + r_mean) * r * r) >> 8)
4129 + 4 * g * g
4130 + (((767 - r_mean) * b * b) >> 8);
4134 DEFUN ("color-distance", Fcolor_distance, Scolor_distance, 2, 3, 0,
4135 doc: /* Return an integer distance between COLOR1 and COLOR2 on FRAME.
4136 COLOR1 and COLOR2 may be either strings containing the color name,
4137 or lists of the form (RED GREEN BLUE).
4138 If FRAME is unspecified or nil, the current frame is used. */)
4139 (Lisp_Object color1, Lisp_Object color2, Lisp_Object frame)
4141 struct frame *f = decode_live_frame (frame);
4142 XColor cdef1, cdef2;
4144 if (!(CONSP (color1) && parse_rgb_list (color1, &cdef1))
4145 && !(STRINGP (color1) && defined_color (f, SSDATA (color1), &cdef1, 0)))
4146 signal_error ("Invalid color", color1);
4147 if (!(CONSP (color2) && parse_rgb_list (color2, &cdef2))
4148 && !(STRINGP (color2) && defined_color (f, SSDATA (color2), &cdef2, 0)))
4149 signal_error ("Invalid color", color2);
4151 return make_number (color_distance (&cdef1, &cdef2));
4155 /***********************************************************************
4156 Face Cache
4157 ***********************************************************************/
4159 /* Return a new face cache for frame F. */
4161 static struct face_cache *
4162 make_face_cache (struct frame *f)
4164 struct face_cache *c = xmalloc (sizeof *c);
4166 c->buckets = xzalloc (FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets);
4167 c->size = 50;
4168 c->used = 0;
4169 c->faces_by_id = xmalloc (c->size * sizeof *c->faces_by_id);
4170 c->f = f;
4171 c->menu_face_changed_p = menu_face_changed_default;
4172 return c;
4176 /* Clear out all graphics contexts for all realized faces, except for
4177 the basic faces. This should be done from time to time just to avoid
4178 keeping too many graphics contexts that are no longer needed. */
4180 static void
4181 clear_face_gcs (struct face_cache *c)
4183 if (c && FRAME_WINDOW_P (c->f))
4185 #ifdef HAVE_WINDOW_SYSTEM
4186 int i;
4187 for (i = BASIC_FACE_ID_SENTINEL; i < c->used; ++i)
4189 struct face *face = c->faces_by_id[i];
4190 if (face && face->gc)
4192 block_input ();
4193 if (face->font)
4194 font_done_for_face (c->f, face);
4195 x_free_gc (c->f, face->gc);
4196 face->gc = 0;
4197 unblock_input ();
4200 #endif /* HAVE_WINDOW_SYSTEM */
4205 /* Free all realized faces in face cache C, including basic faces.
4206 C may be null. If faces are freed, make sure the frame's current
4207 matrix is marked invalid, so that a display caused by an expose
4208 event doesn't try to use faces we destroyed. */
4210 static void
4211 free_realized_faces (struct face_cache *c)
4213 if (c && c->used)
4215 int i, size;
4216 struct frame *f = c->f;
4218 /* We must block input here because we can't process X events
4219 safely while only some faces are freed, or when the frame's
4220 current matrix still references freed faces. */
4221 block_input ();
4223 for (i = 0; i < c->used; ++i)
4225 free_realized_face (f, c->faces_by_id[i]);
4226 c->faces_by_id[i] = NULL;
4229 c->used = 0;
4230 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4231 memset (c->buckets, 0, size);
4233 /* Must do a thorough redisplay the next time. Mark current
4234 matrices as invalid because they will reference faces freed
4235 above. This function is also called when a frame is
4236 destroyed. In this case, the root window of F is nil. */
4237 if (WINDOWP (f->root_window))
4239 clear_current_matrices (f);
4240 ++windows_or_buffers_changed;
4243 unblock_input ();
4248 /* Free all realized faces on FRAME or on all frames if FRAME is nil.
4249 This is done after attributes of a named face have been changed,
4250 because we can't tell which realized faces depend on that face. */
4252 void
4253 free_all_realized_faces (Lisp_Object frame)
4255 if (NILP (frame))
4257 Lisp_Object rest;
4258 FOR_EACH_FRAME (rest, frame)
4259 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4261 else
4262 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4266 /* Free face cache C and faces in it, including their X resources. */
4268 static void
4269 free_face_cache (struct face_cache *c)
4271 if (c)
4273 free_realized_faces (c);
4274 xfree (c->buckets);
4275 xfree (c->faces_by_id);
4276 xfree (c);
4281 /* Cache realized face FACE in face cache C. HASH is the hash value
4282 of FACE. If FACE is for ASCII characters (i.e. FACE->ascii_face ==
4283 FACE), insert the new face to the beginning of the collision list
4284 of the face hash table of C. Otherwise, add the new face to the
4285 end of the collision list. This way, lookup_face can quickly find
4286 that a requested face is not cached. */
4288 static void
4289 cache_face (struct face_cache *c, struct face *face, unsigned int hash)
4291 int i = hash % FACE_CACHE_BUCKETS_SIZE;
4293 face->hash = hash;
4295 if (face->ascii_face != face)
4297 struct face *last = c->buckets[i];
4298 if (last)
4300 while (last->next)
4301 last = last->next;
4302 last->next = face;
4303 face->prev = last;
4304 face->next = NULL;
4306 else
4308 c->buckets[i] = face;
4309 face->prev = face->next = NULL;
4312 else
4314 face->prev = NULL;
4315 face->next = c->buckets[i];
4316 if (face->next)
4317 face->next->prev = face;
4318 c->buckets[i] = face;
4321 /* Find a free slot in C->faces_by_id and use the index of the free
4322 slot as FACE->id. */
4323 for (i = 0; i < c->used; ++i)
4324 if (c->faces_by_id[i] == NULL)
4325 break;
4326 face->id = i;
4328 #ifdef GLYPH_DEBUG
4329 /* Check that FACE got a unique id. */
4331 int j, n;
4332 struct face *face1;
4334 for (j = n = 0; j < FACE_CACHE_BUCKETS_SIZE; ++j)
4335 for (face1 = c->buckets[j]; face1; face1 = face1->next)
4336 if (face1->id == i)
4337 ++n;
4339 eassert (n == 1);
4341 #endif /* GLYPH_DEBUG */
4343 /* Maybe enlarge C->faces_by_id. */
4344 if (i == c->used)
4346 if (c->used == c->size)
4347 c->faces_by_id = xpalloc (c->faces_by_id, &c->size, 1, MAX_FACE_ID,
4348 sizeof *c->faces_by_id);
4349 c->used++;
4352 c->faces_by_id[i] = face;
4356 /* Remove face FACE from cache C. */
4358 static void
4359 uncache_face (struct face_cache *c, struct face *face)
4361 int i = face->hash % FACE_CACHE_BUCKETS_SIZE;
4363 if (face->prev)
4364 face->prev->next = face->next;
4365 else
4366 c->buckets[i] = face->next;
4368 if (face->next)
4369 face->next->prev = face->prev;
4371 c->faces_by_id[face->id] = NULL;
4372 if (face->id == c->used)
4373 --c->used;
4377 /* Look up a realized face with face attributes ATTR in the face cache
4378 of frame F. The face will be used to display ASCII characters.
4379 Value is the ID of the face found. If no suitable face is found,
4380 realize a new one. */
4382 static int
4383 lookup_face (struct frame *f, Lisp_Object *attr)
4385 struct face_cache *cache = FRAME_FACE_CACHE (f);
4386 unsigned hash;
4387 int i;
4388 struct face *face;
4390 eassert (cache != NULL);
4391 check_lface_attrs (attr);
4393 /* Look up ATTR in the face cache. */
4394 hash = lface_hash (attr);
4395 i = hash % FACE_CACHE_BUCKETS_SIZE;
4397 for (face = cache->buckets[i]; face; face = face->next)
4399 if (face->ascii_face != face)
4401 /* There's no more ASCII face. */
4402 face = NULL;
4403 break;
4405 if (face->hash == hash
4406 && lface_equal_p (face->lface, attr))
4407 break;
4410 /* If not found, realize a new face. */
4411 if (face == NULL)
4412 face = realize_face (cache, attr, -1);
4414 #ifdef GLYPH_DEBUG
4415 eassert (face == FACE_FROM_ID (f, face->id));
4416 #endif /* GLYPH_DEBUG */
4418 return face->id;
4421 #ifdef HAVE_WINDOW_SYSTEM
4422 /* Look up a realized face that has the same attributes as BASE_FACE
4423 except for the font in the face cache of frame F. If FONT-OBJECT
4424 is not nil, it is an already opened font. If FONT-OBJECT is nil,
4425 the face has no font. Value is the ID of the face found. If no
4426 suitable face is found, realize a new one. */
4429 face_for_font (struct frame *f, Lisp_Object font_object, struct face *base_face)
4431 struct face_cache *cache = FRAME_FACE_CACHE (f);
4432 unsigned hash;
4433 int i;
4434 struct face *face;
4436 eassert (cache != NULL);
4437 base_face = base_face->ascii_face;
4438 hash = lface_hash (base_face->lface);
4439 i = hash % FACE_CACHE_BUCKETS_SIZE;
4441 for (face = cache->buckets[i]; face; face = face->next)
4443 if (face->ascii_face == face)
4444 continue;
4445 if (face->ascii_face == base_face
4446 && face->font == (NILP (font_object) ? NULL
4447 : XFONT_OBJECT (font_object))
4448 && lface_equal_p (face->lface, base_face->lface))
4449 return face->id;
4452 /* If not found, realize a new face. */
4453 face = realize_non_ascii_face (f, font_object, base_face);
4454 return face->id;
4456 #endif /* HAVE_WINDOW_SYSTEM */
4458 /* Return the face id of the realized face for named face SYMBOL on
4459 frame F suitable for displaying ASCII characters. Value is -1 if
4460 the face couldn't be determined, which might happen if the default
4461 face isn't realized and cannot be realized. */
4464 lookup_named_face (struct frame *f, Lisp_Object symbol, int signal_p)
4466 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4467 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4468 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4470 if (default_face == NULL)
4472 if (!realize_basic_faces (f))
4473 return -1;
4474 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4475 if (default_face == NULL)
4476 emacs_abort (); /* realize_basic_faces must have set it up */
4479 if (! get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4480 return -1;
4482 memcpy (attrs, default_face->lface, sizeof attrs);
4483 merge_face_vectors (f, symbol_attrs, attrs, 0);
4485 return lookup_face (f, attrs);
4489 /* Return the display face-id of the basic face whose canonical face-id
4490 is FACE_ID. The return value will usually simply be FACE_ID, unless that
4491 basic face has bee remapped via Vface_remapping_alist. This function is
4492 conservative: if something goes wrong, it will simply return FACE_ID
4493 rather than signal an error. */
4496 lookup_basic_face (struct frame *f, int face_id)
4498 Lisp_Object name, mapping;
4499 int remapped_face_id;
4501 if (NILP (Vface_remapping_alist))
4502 return face_id; /* Nothing to do. */
4504 switch (face_id)
4506 case DEFAULT_FACE_ID: name = Qdefault; break;
4507 case MODE_LINE_FACE_ID: name = Qmode_line; break;
4508 case MODE_LINE_INACTIVE_FACE_ID: name = Qmode_line_inactive; break;
4509 case HEADER_LINE_FACE_ID: name = Qheader_line; break;
4510 case TOOL_BAR_FACE_ID: name = Qtool_bar; break;
4511 case FRINGE_FACE_ID: name = Qfringe; break;
4512 case SCROLL_BAR_FACE_ID: name = Qscroll_bar; break;
4513 case BORDER_FACE_ID: name = Qborder; break;
4514 case CURSOR_FACE_ID: name = Qcursor; break;
4515 case MOUSE_FACE_ID: name = Qmouse; break;
4516 case MENU_FACE_ID: name = Qmenu; break;
4518 default:
4519 emacs_abort (); /* the caller is supposed to pass us a basic face id */
4522 /* Do a quick scan through Vface_remapping_alist, and return immediately
4523 if there is no remapping for face NAME. This is just an optimization
4524 for the very common no-remapping case. */
4525 mapping = assq_no_quit (name, Vface_remapping_alist);
4526 if (NILP (mapping))
4527 return face_id; /* Give up. */
4529 /* If there is a remapping entry, lookup the face using NAME, which will
4530 handle the remapping too. */
4531 remapped_face_id = lookup_named_face (f, name, 0);
4532 if (remapped_face_id < 0)
4533 return face_id; /* Give up. */
4535 return remapped_face_id;
4539 /* Return a face for charset ASCII that is like the face with id
4540 FACE_ID on frame F, but has a font that is STEPS steps smaller.
4541 STEPS < 0 means larger. Value is the id of the face. */
4544 smaller_face (struct frame *f, int face_id, int steps)
4546 #ifdef HAVE_WINDOW_SYSTEM
4547 struct face *face;
4548 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4549 int pt, last_pt, last_height;
4550 int delta;
4551 int new_face_id;
4552 struct face *new_face;
4554 /* If not called for an X frame, just return the original face. */
4555 if (FRAME_TERMCAP_P (f))
4556 return face_id;
4558 /* Try in increments of 1/2 pt. */
4559 delta = steps < 0 ? 5 : -5;
4560 steps = eabs (steps);
4562 face = FACE_FROM_ID (f, face_id);
4563 memcpy (attrs, face->lface, sizeof attrs);
4564 pt = last_pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
4565 new_face_id = face_id;
4566 last_height = FONT_HEIGHT (face->font);
4568 while (steps
4569 && pt + delta > 0
4570 /* Give up if we cannot find a font within 10pt. */
4571 && eabs (last_pt - pt) < 100)
4573 /* Look up a face for a slightly smaller/larger font. */
4574 pt += delta;
4575 attrs[LFACE_HEIGHT_INDEX] = make_number (pt);
4576 new_face_id = lookup_face (f, attrs);
4577 new_face = FACE_FROM_ID (f, new_face_id);
4579 /* If height changes, count that as one step. */
4580 if ((delta < 0 && FONT_HEIGHT (new_face->font) < last_height)
4581 || (delta > 0 && FONT_HEIGHT (new_face->font) > last_height))
4583 --steps;
4584 last_height = FONT_HEIGHT (new_face->font);
4585 last_pt = pt;
4589 return new_face_id;
4591 #else /* not HAVE_WINDOW_SYSTEM */
4593 return face_id;
4595 #endif /* not HAVE_WINDOW_SYSTEM */
4599 /* Return a face for charset ASCII that is like the face with id
4600 FACE_ID on frame F, but has height HEIGHT. */
4603 face_with_height (struct frame *f, int face_id, int height)
4605 #ifdef HAVE_WINDOW_SYSTEM
4606 struct face *face;
4607 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4609 if (FRAME_TERMCAP_P (f)
4610 || height <= 0)
4611 return face_id;
4613 face = FACE_FROM_ID (f, face_id);
4614 memcpy (attrs, face->lface, sizeof attrs);
4615 attrs[LFACE_HEIGHT_INDEX] = make_number (height);
4616 font_clear_prop (attrs, FONT_SIZE_INDEX);
4617 face_id = lookup_face (f, attrs);
4618 #endif /* HAVE_WINDOW_SYSTEM */
4620 return face_id;
4624 /* Return the face id of the realized face for named face SYMBOL on
4625 frame F suitable for displaying ASCII characters, and use
4626 attributes of the face FACE_ID for attributes that aren't
4627 completely specified by SYMBOL. This is like lookup_named_face,
4628 except that the default attributes come from FACE_ID, not from the
4629 default face. FACE_ID is assumed to be already realized. */
4632 lookup_derived_face (struct frame *f, Lisp_Object symbol, int face_id,
4633 int signal_p)
4635 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4636 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4637 struct face *default_face = FACE_FROM_ID (f, face_id);
4639 if (!default_face)
4640 emacs_abort ();
4642 if (!get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4643 return -1;
4645 memcpy (attrs, default_face->lface, sizeof attrs);
4646 merge_face_vectors (f, symbol_attrs, attrs, 0);
4647 return lookup_face (f, attrs);
4650 DEFUN ("face-attributes-as-vector", Fface_attributes_as_vector,
4651 Sface_attributes_as_vector, 1, 1, 0,
4652 doc: /* Return a vector of face attributes corresponding to PLIST. */)
4653 (Lisp_Object plist)
4655 Lisp_Object lface;
4656 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
4657 Qunspecified);
4658 merge_face_ref (XFRAME (selected_frame), plist, XVECTOR (lface)->contents,
4659 1, 0);
4660 return lface;
4665 /***********************************************************************
4666 Face capability testing
4667 ***********************************************************************/
4670 /* If the distance (as returned by color_distance) between two colors is
4671 less than this, then they are considered the same, for determining
4672 whether a color is supported or not. The range of values is 0-65535. */
4674 #define TTY_SAME_COLOR_THRESHOLD 10000
4676 #ifdef HAVE_WINDOW_SYSTEM
4678 /* Return non-zero if all the face attributes in ATTRS are supported
4679 on the window-system frame F.
4681 The definition of `supported' is somewhat heuristic, but basically means
4682 that a face containing all the attributes in ATTRS, when merged with the
4683 default face for display, can be represented in a way that's
4685 \(1) different in appearance than the default face, and
4686 \(2) `close in spirit' to what the attributes specify, if not exact. */
4688 static int
4689 x_supports_face_attributes_p (struct frame *f,
4690 Lisp_Object attrs[LFACE_VECTOR_SIZE],
4691 struct face *def_face)
4693 Lisp_Object *def_attrs = def_face->lface;
4695 /* Check that other specified attributes are different that the default
4696 face. */
4697 if ((!UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
4698 && face_attr_equal_p (attrs[LFACE_UNDERLINE_INDEX],
4699 def_attrs[LFACE_UNDERLINE_INDEX]))
4700 || (!UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
4701 && face_attr_equal_p (attrs[LFACE_INVERSE_INDEX],
4702 def_attrs[LFACE_INVERSE_INDEX]))
4703 || (!UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
4704 && face_attr_equal_p (attrs[LFACE_FOREGROUND_INDEX],
4705 def_attrs[LFACE_FOREGROUND_INDEX]))
4706 || (!UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
4707 && face_attr_equal_p (attrs[LFACE_BACKGROUND_INDEX],
4708 def_attrs[LFACE_BACKGROUND_INDEX]))
4709 || (!UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4710 && face_attr_equal_p (attrs[LFACE_STIPPLE_INDEX],
4711 def_attrs[LFACE_STIPPLE_INDEX]))
4712 || (!UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4713 && face_attr_equal_p (attrs[LFACE_OVERLINE_INDEX],
4714 def_attrs[LFACE_OVERLINE_INDEX]))
4715 || (!UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4716 && face_attr_equal_p (attrs[LFACE_STRIKE_THROUGH_INDEX],
4717 def_attrs[LFACE_STRIKE_THROUGH_INDEX]))
4718 || (!UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
4719 && face_attr_equal_p (attrs[LFACE_BOX_INDEX],
4720 def_attrs[LFACE_BOX_INDEX])))
4721 return 0;
4723 /* Check font-related attributes, as those are the most commonly
4724 "unsupported" on a window-system (because of missing fonts). */
4725 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4726 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4727 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4728 || !UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
4729 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
4730 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX]))
4732 int face_id;
4733 struct face *face;
4734 Lisp_Object merged_attrs[LFACE_VECTOR_SIZE];
4735 int i;
4737 memcpy (merged_attrs, def_attrs, sizeof merged_attrs);
4739 merge_face_vectors (f, attrs, merged_attrs, 0);
4741 face_id = lookup_face (f, merged_attrs);
4742 face = FACE_FROM_ID (f, face_id);
4744 if (! face)
4745 error ("Cannot make face");
4747 /* If the font is the same, or no font is found, then not
4748 supported. */
4749 if (face->font == def_face->font
4750 || ! face->font)
4751 return 0;
4752 for (i = FONT_TYPE_INDEX; i <= FONT_SIZE_INDEX; i++)
4753 if (! EQ (face->font->props[i], def_face->font->props[i]))
4755 Lisp_Object s1, s2;
4757 if (i < FONT_FOUNDRY_INDEX || i > FONT_REGISTRY_INDEX
4758 || face->font->driver->case_sensitive)
4759 return 1;
4760 s1 = SYMBOL_NAME (face->font->props[i]);
4761 s2 = SYMBOL_NAME (def_face->font->props[i]);
4762 if (! EQ (Fcompare_strings (s1, make_number (0), Qnil,
4763 s2, make_number (0), Qnil, Qt), Qt))
4764 return 1;
4766 return 0;
4769 /* Everything checks out, this face is supported. */
4770 return 1;
4773 #endif /* HAVE_WINDOW_SYSTEM */
4775 /* Return non-zero if all the face attributes in ATTRS are supported
4776 on the tty frame F.
4778 The definition of `supported' is somewhat heuristic, but basically means
4779 that a face containing all the attributes in ATTRS, when merged
4780 with the default face for display, can be represented in a way that's
4782 \(1) different in appearance than the default face, and
4783 \(2) `close in spirit' to what the attributes specify, if not exact.
4785 Point (2) implies that a `:weight black' attribute will be satisfied
4786 by any terminal that can display bold, and a `:foreground "yellow"' as
4787 long as the terminal can display a yellowish color, but `:slant italic'
4788 will _not_ be satisfied by the tty display code's automatic
4789 substitution of a `dim' face for italic. */
4791 static int
4792 tty_supports_face_attributes_p (struct frame *f,
4793 Lisp_Object attrs[LFACE_VECTOR_SIZE],
4794 struct face *def_face)
4796 int weight, slant;
4797 Lisp_Object val, fg, bg;
4798 XColor fg_tty_color, fg_std_color;
4799 XColor bg_tty_color, bg_std_color;
4800 unsigned test_caps = 0;
4801 Lisp_Object *def_attrs = def_face->lface;
4803 /* First check some easy-to-check stuff; ttys support none of the
4804 following attributes, so we can just return false if any are requested
4805 (even if `nominal' values are specified, we should still return false,
4806 as that will be the same value that the default face uses). We
4807 consider :slant unsupportable on ttys, even though the face code
4808 actually `fakes' them using a dim attribute if possible. This is
4809 because the faked result is too different from what the face
4810 specifies. */
4811 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4812 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4813 || !UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4814 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4815 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
4816 || !UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4817 || !UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4818 || !UNSPECIFIEDP (attrs[LFACE_BOX_INDEX]))
4819 return 0;
4821 /* Test for terminal `capabilities' (non-color character attributes). */
4823 /* font weight (bold/dim) */
4824 val = attrs[LFACE_WEIGHT_INDEX];
4825 if (!UNSPECIFIEDP (val)
4826 && (weight = FONT_WEIGHT_NAME_NUMERIC (val), weight >= 0))
4828 int def_weight = FONT_WEIGHT_NAME_NUMERIC (def_attrs[LFACE_WEIGHT_INDEX]);
4830 if (weight > 100)
4832 if (def_weight > 100)
4833 return 0; /* same as default */
4834 test_caps = TTY_CAP_BOLD;
4836 else if (weight < 100)
4838 if (def_weight < 100)
4839 return 0; /* same as default */
4840 test_caps = TTY_CAP_DIM;
4842 else if (def_weight == 100)
4843 return 0; /* same as default */
4846 /* font slant */
4847 val = attrs[LFACE_SLANT_INDEX];
4848 if (!UNSPECIFIEDP (val)
4849 && (slant = FONT_SLANT_NAME_NUMERIC (val), slant >= 0))
4851 int def_slant = FONT_SLANT_NAME_NUMERIC (def_attrs[LFACE_SLANT_INDEX]);
4852 if (slant == 100 || slant == def_slant)
4853 return 0; /* same as default */
4854 else
4855 test_caps |= TTY_CAP_ITALIC;
4858 /* underlining */
4859 val = attrs[LFACE_UNDERLINE_INDEX];
4860 if (!UNSPECIFIEDP (val))
4862 if (STRINGP (val))
4863 return 0; /* ttys can't use colored underlines */
4864 else if (EQ (CAR_SAFE (val), QCstyle) && EQ (CAR_SAFE (CDR_SAFE (val)), Qwave))
4865 return 0; /* ttys can't use wave underlines */
4866 else if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
4867 return 0; /* same as default */
4868 else
4869 test_caps |= TTY_CAP_UNDERLINE;
4872 /* inverse video */
4873 val = attrs[LFACE_INVERSE_INDEX];
4874 if (!UNSPECIFIEDP (val))
4876 if (face_attr_equal_p (val, def_attrs[LFACE_INVERSE_INDEX]))
4877 return 0; /* same as default */
4878 else
4879 test_caps |= TTY_CAP_INVERSE;
4883 /* Color testing. */
4885 /* Default the color indices in FG_TTY_COLOR and BG_TTY_COLOR, since
4886 we use them when calling `tty_capable_p' below, even if the face
4887 specifies no colors. */
4888 fg_tty_color.pixel = FACE_TTY_DEFAULT_FG_COLOR;
4889 bg_tty_color.pixel = FACE_TTY_DEFAULT_BG_COLOR;
4891 /* Check if foreground color is close enough. */
4892 fg = attrs[LFACE_FOREGROUND_INDEX];
4893 if (STRINGP (fg))
4895 Lisp_Object def_fg = def_attrs[LFACE_FOREGROUND_INDEX];
4897 if (face_attr_equal_p (fg, def_fg))
4898 return 0; /* same as default */
4899 else if (! tty_lookup_color (f, fg, &fg_tty_color, &fg_std_color))
4900 return 0; /* not a valid color */
4901 else if (color_distance (&fg_tty_color, &fg_std_color)
4902 > TTY_SAME_COLOR_THRESHOLD)
4903 return 0; /* displayed color is too different */
4904 else
4905 /* Make sure the color is really different than the default. */
4907 XColor def_fg_color;
4908 if (tty_lookup_color (f, def_fg, &def_fg_color, 0)
4909 && (color_distance (&fg_tty_color, &def_fg_color)
4910 <= TTY_SAME_COLOR_THRESHOLD))
4911 return 0;
4915 /* Check if background color is close enough. */
4916 bg = attrs[LFACE_BACKGROUND_INDEX];
4917 if (STRINGP (bg))
4919 Lisp_Object def_bg = def_attrs[LFACE_BACKGROUND_INDEX];
4921 if (face_attr_equal_p (bg, def_bg))
4922 return 0; /* same as default */
4923 else if (! tty_lookup_color (f, bg, &bg_tty_color, &bg_std_color))
4924 return 0; /* not a valid color */
4925 else if (color_distance (&bg_tty_color, &bg_std_color)
4926 > TTY_SAME_COLOR_THRESHOLD)
4927 return 0; /* displayed color is too different */
4928 else
4929 /* Make sure the color is really different than the default. */
4931 XColor def_bg_color;
4932 if (tty_lookup_color (f, def_bg, &def_bg_color, 0)
4933 && (color_distance (&bg_tty_color, &def_bg_color)
4934 <= TTY_SAME_COLOR_THRESHOLD))
4935 return 0;
4939 /* If both foreground and background are requested, see if the
4940 distance between them is OK. We just check to see if the distance
4941 between the tty's foreground and background is close enough to the
4942 distance between the standard foreground and background. */
4943 if (STRINGP (fg) && STRINGP (bg))
4945 int delta_delta
4946 = (color_distance (&fg_std_color, &bg_std_color)
4947 - color_distance (&fg_tty_color, &bg_tty_color));
4948 if (delta_delta > TTY_SAME_COLOR_THRESHOLD
4949 || delta_delta < -TTY_SAME_COLOR_THRESHOLD)
4950 return 0;
4954 /* See if the capabilities we selected above are supported, with the
4955 given colors. */
4956 if (test_caps != 0 &&
4957 ! tty_capable_p (FRAME_TTY (f), test_caps, fg_tty_color.pixel,
4958 bg_tty_color.pixel))
4959 return 0;
4962 /* Hmmm, everything checks out, this terminal must support this face. */
4963 return 1;
4967 DEFUN ("display-supports-face-attributes-p",
4968 Fdisplay_supports_face_attributes_p, Sdisplay_supports_face_attributes_p,
4969 1, 2, 0,
4970 doc: /* Return non-nil if all the face attributes in ATTRIBUTES are supported.
4971 The optional argument DISPLAY can be a display name, a frame, or
4972 nil (meaning the selected frame's display).
4974 The definition of `supported' is somewhat heuristic, but basically means
4975 that a face containing all the attributes in ATTRIBUTES, when merged
4976 with the default face for display, can be represented in a way that's
4978 \(1) different in appearance than the default face, and
4979 \(2) `close in spirit' to what the attributes specify, if not exact.
4981 Point (2) implies that a `:weight black' attribute will be satisfied by
4982 any display that can display bold, and a `:foreground \"yellow\"' as long
4983 as it can display a yellowish color, but `:slant italic' will _not_ be
4984 satisfied by the tty display code's automatic substitution of a `dim'
4985 face for italic. */)
4986 (Lisp_Object attributes, Lisp_Object display)
4988 int supports = 0, i;
4989 Lisp_Object frame;
4990 struct frame *f;
4991 struct face *def_face;
4992 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4994 if (noninteractive || !initialized)
4995 /* We may not be able to access low-level face information in batch
4996 mode, or before being dumped, and this function is not going to
4997 be very useful in those cases anyway, so just give up. */
4998 return Qnil;
5000 if (NILP (display))
5001 frame = selected_frame;
5002 else if (FRAMEP (display))
5003 frame = display;
5004 else
5006 /* Find any frame on DISPLAY. */
5007 Lisp_Object tail;
5009 frame = Qnil;
5010 FOR_EACH_FRAME (tail, frame)
5011 if (!NILP (Fequal (Fcdr (Fassq (Qdisplay,
5012 XFRAME (frame)->param_alist)),
5013 display)))
5014 break;
5017 CHECK_LIVE_FRAME (frame);
5018 f = XFRAME (frame);
5020 for (i = 0; i < LFACE_VECTOR_SIZE; i++)
5021 attrs[i] = Qunspecified;
5022 merge_face_ref (f, attributes, attrs, 1, 0);
5024 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5025 if (def_face == NULL)
5027 if (! realize_basic_faces (f))
5028 error ("Cannot realize default face");
5029 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5030 if (def_face == NULL)
5031 emacs_abort (); /* realize_basic_faces must have set it up */
5034 /* Dispatch to the appropriate handler. */
5035 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5036 supports = tty_supports_face_attributes_p (f, attrs, def_face);
5037 #ifdef HAVE_WINDOW_SYSTEM
5038 else
5039 supports = x_supports_face_attributes_p (f, attrs, def_face);
5040 #endif
5042 return supports ? Qt : Qnil;
5046 /***********************************************************************
5047 Font selection
5048 ***********************************************************************/
5050 DEFUN ("internal-set-font-selection-order",
5051 Finternal_set_font_selection_order,
5052 Sinternal_set_font_selection_order, 1, 1, 0,
5053 doc: /* Set font selection order for face font selection to ORDER.
5054 ORDER must be a list of length 4 containing the symbols `:width',
5055 `:height', `:weight', and `:slant'. Face attributes appearing
5056 first in ORDER are matched first, e.g. if `:height' appears before
5057 `:weight' in ORDER, font selection first tries to find a font with
5058 a suitable height, and then tries to match the font weight.
5059 Value is ORDER. */)
5060 (Lisp_Object order)
5062 Lisp_Object list;
5063 int i;
5064 int indices[DIM (font_sort_order)];
5066 CHECK_LIST (order);
5067 memset (indices, 0, sizeof indices);
5068 i = 0;
5070 for (list = order;
5071 CONSP (list) && i < DIM (indices);
5072 list = XCDR (list), ++i)
5074 Lisp_Object attr = XCAR (list);
5075 int xlfd;
5077 if (EQ (attr, QCwidth))
5078 xlfd = XLFD_SWIDTH;
5079 else if (EQ (attr, QCheight))
5080 xlfd = XLFD_POINT_SIZE;
5081 else if (EQ (attr, QCweight))
5082 xlfd = XLFD_WEIGHT;
5083 else if (EQ (attr, QCslant))
5084 xlfd = XLFD_SLANT;
5085 else
5086 break;
5088 if (indices[i] != 0)
5089 break;
5090 indices[i] = xlfd;
5093 if (!NILP (list) || i != DIM (indices))
5094 signal_error ("Invalid font sort order", order);
5095 for (i = 0; i < DIM (font_sort_order); ++i)
5096 if (indices[i] == 0)
5097 signal_error ("Invalid font sort order", order);
5099 if (memcmp (indices, font_sort_order, sizeof indices) != 0)
5101 memcpy (font_sort_order, indices, sizeof font_sort_order);
5102 free_all_realized_faces (Qnil);
5105 font_update_sort_order (font_sort_order);
5107 return Qnil;
5111 DEFUN ("internal-set-alternative-font-family-alist",
5112 Finternal_set_alternative_font_family_alist,
5113 Sinternal_set_alternative_font_family_alist, 1, 1, 0,
5114 doc: /* Define alternative font families to try in face font selection.
5115 ALIST is an alist of (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5116 Each ALTERNATIVE is tried in order if no fonts of font family FAMILY can
5117 be found. Value is ALIST. */)
5118 (Lisp_Object alist)
5120 Lisp_Object entry, tail, tail2;
5122 CHECK_LIST (alist);
5123 alist = Fcopy_sequence (alist);
5124 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5126 entry = XCAR (tail);
5127 CHECK_LIST (entry);
5128 entry = Fcopy_sequence (entry);
5129 XSETCAR (tail, entry);
5130 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5131 XSETCAR (tail2, Fintern (XCAR (tail2), Qnil));
5134 Vface_alternative_font_family_alist = alist;
5135 free_all_realized_faces (Qnil);
5136 return alist;
5140 DEFUN ("internal-set-alternative-font-registry-alist",
5141 Finternal_set_alternative_font_registry_alist,
5142 Sinternal_set_alternative_font_registry_alist, 1, 1, 0,
5143 doc: /* Define alternative font registries to try in face font selection.
5144 ALIST is an alist of (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5145 Each ALTERNATIVE is tried in order if no fonts of font registry REGISTRY can
5146 be found. Value is ALIST. */)
5147 (Lisp_Object alist)
5149 Lisp_Object entry, tail, tail2;
5151 CHECK_LIST (alist);
5152 alist = Fcopy_sequence (alist);
5153 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5155 entry = XCAR (tail);
5156 CHECK_LIST (entry);
5157 entry = Fcopy_sequence (entry);
5158 XSETCAR (tail, entry);
5159 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5160 XSETCAR (tail2, Fdowncase (XCAR (tail2)));
5162 Vface_alternative_font_registry_alist = alist;
5163 free_all_realized_faces (Qnil);
5164 return alist;
5168 #ifdef HAVE_WINDOW_SYSTEM
5170 /* Return the fontset id of the base fontset name or alias name given
5171 by the fontset attribute of ATTRS. Value is -1 if the fontset
5172 attribute of ATTRS doesn't name a fontset. */
5174 static int
5175 face_fontset (Lisp_Object attrs[LFACE_VECTOR_SIZE])
5177 Lisp_Object name;
5179 name = attrs[LFACE_FONTSET_INDEX];
5180 if (!STRINGP (name))
5181 return -1;
5182 return fs_query_fontset (name, 0);
5185 #endif /* HAVE_WINDOW_SYSTEM */
5189 /***********************************************************************
5190 Face Realization
5191 ***********************************************************************/
5193 /* Realize basic faces on frame F. Value is zero if frame parameters
5194 of F don't contain enough information needed to realize the default
5195 face. */
5197 static bool
5198 realize_basic_faces (struct frame *f)
5200 bool success_p = 0;
5201 ptrdiff_t count = SPECPDL_INDEX ();
5203 /* Block input here so that we won't be surprised by an X expose
5204 event, for instance, without having the faces set up. */
5205 block_input ();
5206 specbind (Qscalable_fonts_allowed, Qt);
5208 if (realize_default_face (f))
5210 realize_named_face (f, Qmode_line, MODE_LINE_FACE_ID);
5211 realize_named_face (f, Qmode_line_inactive, MODE_LINE_INACTIVE_FACE_ID);
5212 realize_named_face (f, Qtool_bar, TOOL_BAR_FACE_ID);
5213 realize_named_face (f, Qfringe, FRINGE_FACE_ID);
5214 realize_named_face (f, Qheader_line, HEADER_LINE_FACE_ID);
5215 realize_named_face (f, Qscroll_bar, SCROLL_BAR_FACE_ID);
5216 realize_named_face (f, Qborder, BORDER_FACE_ID);
5217 realize_named_face (f, Qcursor, CURSOR_FACE_ID);
5218 realize_named_face (f, Qmouse, MOUSE_FACE_ID);
5219 realize_named_face (f, Qmenu, MENU_FACE_ID);
5220 realize_named_face (f, Qvertical_border, VERTICAL_BORDER_FACE_ID);
5222 /* Reflect changes in the `menu' face in menu bars. */
5223 if (FRAME_FACE_CACHE (f)->menu_face_changed_p)
5225 FRAME_FACE_CACHE (f)->menu_face_changed_p = 0;
5226 #ifdef USE_X_TOOLKIT
5227 if (FRAME_WINDOW_P (f))
5228 x_update_menu_appearance (f);
5229 #endif
5232 success_p = 1;
5235 unbind_to (count, Qnil);
5236 unblock_input ();
5237 return success_p;
5241 /* Realize the default face on frame F. If the face is not fully
5242 specified, make it fully-specified. Attributes of the default face
5243 that are not explicitly specified are taken from frame parameters. */
5245 static bool
5246 realize_default_face (struct frame *f)
5248 struct face_cache *c = FRAME_FACE_CACHE (f);
5249 Lisp_Object lface;
5250 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5251 struct face *face;
5253 /* If the `default' face is not yet known, create it. */
5254 lface = lface_from_face_name (f, Qdefault, 0);
5255 if (NILP (lface))
5257 Lisp_Object frame;
5258 XSETFRAME (frame, f);
5259 lface = Finternal_make_lisp_face (Qdefault, frame);
5262 #ifdef HAVE_WINDOW_SYSTEM
5263 if (FRAME_WINDOW_P (f))
5265 Lisp_Object font_object;
5267 XSETFONT (font_object, FRAME_FONT (f));
5268 set_lface_from_font (f, lface, font_object, f->default_face_done_p);
5269 ASET (lface, LFACE_FONTSET_INDEX, fontset_name (FRAME_FONTSET (f)));
5270 f->default_face_done_p = 1;
5272 #endif /* HAVE_WINDOW_SYSTEM */
5274 if (!FRAME_WINDOW_P (f))
5276 ASET (lface, LFACE_FAMILY_INDEX, build_string ("default"));
5277 ASET (lface, LFACE_FOUNDRY_INDEX, LFACE_FAMILY (lface));
5278 ASET (lface, LFACE_SWIDTH_INDEX, Qnormal);
5279 ASET (lface, LFACE_HEIGHT_INDEX, make_number (1));
5280 if (UNSPECIFIEDP (LFACE_WEIGHT (lface)))
5281 ASET (lface, LFACE_WEIGHT_INDEX, Qnormal);
5282 if (UNSPECIFIEDP (LFACE_SLANT (lface)))
5283 ASET (lface, LFACE_SLANT_INDEX, Qnormal);
5284 if (UNSPECIFIEDP (LFACE_FONTSET (lface)))
5285 ASET (lface, LFACE_FONTSET_INDEX, Qnil);
5288 if (UNSPECIFIEDP (LFACE_UNDERLINE (lface)))
5289 ASET (lface, LFACE_UNDERLINE_INDEX, Qnil);
5291 if (UNSPECIFIEDP (LFACE_OVERLINE (lface)))
5292 ASET (lface, LFACE_OVERLINE_INDEX, Qnil);
5294 if (UNSPECIFIEDP (LFACE_STRIKE_THROUGH (lface)))
5295 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, Qnil);
5297 if (UNSPECIFIEDP (LFACE_BOX (lface)))
5298 ASET (lface, LFACE_BOX_INDEX, Qnil);
5300 if (UNSPECIFIEDP (LFACE_INVERSE (lface)))
5301 ASET (lface, LFACE_INVERSE_INDEX, Qnil);
5303 if (UNSPECIFIEDP (LFACE_FOREGROUND (lface)))
5305 /* This function is called so early that colors are not yet
5306 set in the frame parameter list. */
5307 Lisp_Object color = Fassq (Qforeground_color, f->param_alist);
5309 if (CONSP (color) && STRINGP (XCDR (color)))
5310 ASET (lface, LFACE_FOREGROUND_INDEX, XCDR (color));
5311 else if (FRAME_WINDOW_P (f))
5312 return 0;
5313 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5314 ASET (lface, LFACE_FOREGROUND_INDEX, build_string (unspecified_fg));
5315 else
5316 emacs_abort ();
5319 if (UNSPECIFIEDP (LFACE_BACKGROUND (lface)))
5321 /* This function is called so early that colors are not yet
5322 set in the frame parameter list. */
5323 Lisp_Object color = Fassq (Qbackground_color, f->param_alist);
5324 if (CONSP (color) && STRINGP (XCDR (color)))
5325 ASET (lface, LFACE_BACKGROUND_INDEX, XCDR (color));
5326 else if (FRAME_WINDOW_P (f))
5327 return 0;
5328 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5329 ASET (lface, LFACE_BACKGROUND_INDEX, build_string (unspecified_bg));
5330 else
5331 emacs_abort ();
5334 if (UNSPECIFIEDP (LFACE_STIPPLE (lface)))
5335 ASET (lface, LFACE_STIPPLE_INDEX, Qnil);
5337 /* Realize the face; it must be fully-specified now. */
5338 eassert (lface_fully_specified_p (XVECTOR (lface)->contents));
5339 check_lface (lface);
5340 memcpy (attrs, XVECTOR (lface)->contents, sizeof attrs);
5341 face = realize_face (c, attrs, DEFAULT_FACE_ID);
5343 #ifdef HAVE_WINDOW_SYSTEM
5344 #ifdef HAVE_X_WINDOWS
5345 if (FRAME_X_P (f) && face->font != FRAME_FONT (f))
5347 /* This can happen when making a frame on a display that does
5348 not support the default font. */
5349 if (!face->font)
5350 return 0;
5352 /* Otherwise, the font specified for the frame was not
5353 acceptable as a font for the default face (perhaps because
5354 auto-scaled fonts are rejected), so we must adjust the frame
5355 font. */
5356 x_set_font (f, LFACE_FONT (lface), Qnil);
5358 #endif /* HAVE_X_WINDOWS */
5359 #endif /* HAVE_WINDOW_SYSTEM */
5360 return 1;
5364 /* Realize basic faces other than the default face in face cache C.
5365 SYMBOL is the face name, ID is the face id the realized face must
5366 have. The default face must have been realized already. */
5368 static void
5369 realize_named_face (struct frame *f, Lisp_Object symbol, int id)
5371 struct face_cache *c = FRAME_FACE_CACHE (f);
5372 Lisp_Object lface = lface_from_face_name (f, symbol, 0);
5373 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5374 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5376 /* The default face must exist and be fully specified. */
5377 get_lface_attributes_no_remap (f, Qdefault, attrs, 1);
5378 check_lface_attrs (attrs);
5379 eassert (lface_fully_specified_p (attrs));
5381 /* If SYMBOL isn't know as a face, create it. */
5382 if (NILP (lface))
5384 Lisp_Object frame;
5385 XSETFRAME (frame, f);
5386 lface = Finternal_make_lisp_face (symbol, frame);
5389 /* Merge SYMBOL's face with the default face. */
5390 get_lface_attributes_no_remap (f, symbol, symbol_attrs, 1);
5391 merge_face_vectors (f, symbol_attrs, attrs, 0);
5393 /* Realize the face. */
5394 realize_face (c, attrs, id);
5398 /* Realize the fully-specified face with attributes ATTRS in face
5399 cache CACHE for ASCII characters. If FORMER_FACE_ID is
5400 non-negative, it is an ID of face to remove before caching the new
5401 face. Value is a pointer to the newly created realized face. */
5403 static struct face *
5404 realize_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE],
5405 int former_face_id)
5407 struct face *face;
5409 /* LFACE must be fully specified. */
5410 eassert (cache != NULL);
5411 check_lface_attrs (attrs);
5413 if (former_face_id >= 0 && cache->used > former_face_id)
5415 /* Remove the former face. */
5416 struct face *former_face = cache->faces_by_id[former_face_id];
5417 uncache_face (cache, former_face);
5418 free_realized_face (cache->f, former_face);
5419 SET_FRAME_GARBAGED (cache->f);
5422 if (FRAME_WINDOW_P (cache->f))
5423 face = realize_x_face (cache, attrs);
5424 else if (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f))
5425 face = realize_tty_face (cache, attrs);
5426 else if (FRAME_INITIAL_P (cache->f))
5428 /* Create a dummy face. */
5429 face = make_realized_face (attrs);
5431 else
5432 emacs_abort ();
5434 /* Insert the new face. */
5435 cache_face (cache, face, lface_hash (attrs));
5436 return face;
5440 #ifdef HAVE_WINDOW_SYSTEM
5441 /* Realize the fully-specified face that uses FONT-OBJECT and has the
5442 same attributes as BASE_FACE except for the font on frame F.
5443 FONT-OBJECT may be nil, in which case, realized a face of
5444 no-font. */
5446 static struct face *
5447 realize_non_ascii_face (struct frame *f, Lisp_Object font_object,
5448 struct face *base_face)
5450 struct face_cache *cache = FRAME_FACE_CACHE (f);
5451 struct face *face;
5453 face = xmalloc (sizeof *face);
5454 *face = *base_face;
5455 face->gc = 0;
5456 face->extra = NULL;
5457 face->overstrike
5458 = (! NILP (font_object)
5459 && FONT_WEIGHT_NAME_NUMERIC (face->lface[LFACE_WEIGHT_INDEX]) > 100
5460 && FONT_WEIGHT_NUMERIC (font_object) <= 100);
5462 /* Don't try to free the colors copied bitwise from BASE_FACE. */
5463 face->colors_copied_bitwise_p = 1;
5464 face->font = NILP (font_object) ? NULL : XFONT_OBJECT (font_object);
5465 face->gc = 0;
5467 cache_face (cache, face, face->hash);
5469 return face;
5471 #endif /* HAVE_WINDOW_SYSTEM */
5474 /* Realize the fully-specified face with attributes ATTRS in face
5475 cache CACHE for ASCII characters. Do it for X frame CACHE->f. If
5476 the new face doesn't share font with the default face, a fontname
5477 is allocated from the heap and set in `font_name' of the new face,
5478 but it is not yet loaded here. Value is a pointer to the newly
5479 created realized face. */
5481 static struct face *
5482 realize_x_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE])
5484 struct face *face = NULL;
5485 #ifdef HAVE_WINDOW_SYSTEM
5486 struct face *default_face;
5487 struct frame *f;
5488 Lisp_Object stipple, underline, overline, strike_through, box;
5490 eassert (FRAME_WINDOW_P (cache->f));
5492 /* Allocate a new realized face. */
5493 face = make_realized_face (attrs);
5494 face->ascii_face = face;
5496 f = cache->f;
5498 /* Determine the font to use. Most of the time, the font will be
5499 the same as the font of the default face, so try that first. */
5500 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5501 if (default_face
5502 && lface_same_font_attributes_p (default_face->lface, attrs))
5504 face->font = default_face->font;
5505 face->fontset
5506 = make_fontset_for_ascii_face (f, default_face->fontset, face);
5508 else
5510 /* If the face attribute ATTRS specifies a fontset, use it as
5511 the base of a new realized fontset. Otherwise, use the same
5512 base fontset as of the default face. The base determines
5513 registry and encoding of a font. It may also determine
5514 foundry and family. The other fields of font name pattern
5515 are constructed from ATTRS. */
5516 int fontset = face_fontset (attrs);
5518 /* If we are realizing the default face, ATTRS should specify a
5519 fontset. In other words, if FONTSET is -1, we are not
5520 realizing the default face, thus the default face should have
5521 already been realized. */
5522 if (fontset == -1)
5524 if (default_face)
5525 fontset = default_face->fontset;
5526 if (fontset == -1)
5527 emacs_abort ();
5529 if (! FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5530 attrs[LFACE_FONT_INDEX]
5531 = font_load_for_lface (f, attrs, attrs[LFACE_FONT_INDEX]);
5532 if (FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5534 face->font = XFONT_OBJECT (attrs[LFACE_FONT_INDEX]);
5535 face->fontset = make_fontset_for_ascii_face (f, fontset, face);
5537 else
5539 face->font = NULL;
5540 face->fontset = -1;
5544 if (face->font
5545 && FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]) > 100
5546 && FONT_WEIGHT_NUMERIC (attrs[LFACE_FONT_INDEX]) <= 100)
5547 face->overstrike = 1;
5549 /* Load colors, and set remaining attributes. */
5551 load_face_colors (f, face, attrs);
5553 /* Set up box. */
5554 box = attrs[LFACE_BOX_INDEX];
5555 if (STRINGP (box))
5557 /* A simple box of line width 1 drawn in color given by
5558 the string. */
5559 face->box_color = load_color (f, face, attrs[LFACE_BOX_INDEX],
5560 LFACE_BOX_INDEX);
5561 face->box = FACE_SIMPLE_BOX;
5562 face->box_line_width = 1;
5564 else if (INTEGERP (box))
5566 /* Simple box of specified line width in foreground color of the
5567 face. */
5568 eassert (XINT (box) != 0);
5569 face->box = FACE_SIMPLE_BOX;
5570 face->box_line_width = XINT (box);
5571 face->box_color = face->foreground;
5572 face->box_color_defaulted_p = 1;
5574 else if (CONSP (box))
5576 /* `(:width WIDTH :color COLOR :shadow SHADOW)'. SHADOW
5577 being one of `raised' or `sunken'. */
5578 face->box = FACE_SIMPLE_BOX;
5579 face->box_color = face->foreground;
5580 face->box_color_defaulted_p = 1;
5581 face->box_line_width = 1;
5583 while (CONSP (box))
5585 Lisp_Object keyword, value;
5587 keyword = XCAR (box);
5588 box = XCDR (box);
5590 if (!CONSP (box))
5591 break;
5592 value = XCAR (box);
5593 box = XCDR (box);
5595 if (EQ (keyword, QCline_width))
5597 if (INTEGERP (value) && XINT (value) != 0)
5598 face->box_line_width = XINT (value);
5600 else if (EQ (keyword, QCcolor))
5602 if (STRINGP (value))
5604 face->box_color = load_color (f, face, value,
5605 LFACE_BOX_INDEX);
5606 face->use_box_color_for_shadows_p = 1;
5609 else if (EQ (keyword, QCstyle))
5611 if (EQ (value, Qreleased_button))
5612 face->box = FACE_RAISED_BOX;
5613 else if (EQ (value, Qpressed_button))
5614 face->box = FACE_SUNKEN_BOX;
5619 /* Text underline, overline, strike-through. */
5621 underline = attrs[LFACE_UNDERLINE_INDEX];
5622 if (EQ (underline, Qt))
5624 /* Use default color (same as foreground color). */
5625 face->underline_p = 1;
5626 face->underline_type = FACE_UNDER_LINE;
5627 face->underline_defaulted_p = 1;
5628 face->underline_color = 0;
5630 else if (STRINGP (underline))
5632 /* Use specified color. */
5633 face->underline_p = 1;
5634 face->underline_type = FACE_UNDER_LINE;
5635 face->underline_defaulted_p = 0;
5636 face->underline_color
5637 = load_color (f, face, underline,
5638 LFACE_UNDERLINE_INDEX);
5640 else if (NILP (underline))
5642 face->underline_p = 0;
5643 face->underline_defaulted_p = 0;
5644 face->underline_color = 0;
5646 else if (CONSP (underline))
5648 /* `(:color COLOR :style STYLE)'.
5649 STYLE being one of `line' or `wave'. */
5650 face->underline_p = 1;
5651 face->underline_color = 0;
5652 face->underline_defaulted_p = 1;
5653 face->underline_type = FACE_UNDER_LINE;
5655 /* FIXME? This is also not robust about checking the precise form.
5656 See comments in Finternal_set_lisp_face_attribute. */
5657 while (CONSP (underline))
5659 Lisp_Object keyword, value;
5661 keyword = XCAR (underline);
5662 underline = XCDR (underline);
5664 if (!CONSP (underline))
5665 break;
5666 value = XCAR (underline);
5667 underline = XCDR (underline);
5669 if (EQ (keyword, QCcolor))
5671 if (EQ (value, Qforeground_color))
5673 face->underline_defaulted_p = 1;
5674 face->underline_color = 0;
5676 else if (STRINGP (value))
5678 face->underline_defaulted_p = 0;
5679 face->underline_color = load_color (f, face, value,
5680 LFACE_UNDERLINE_INDEX);
5683 else if (EQ (keyword, QCstyle))
5685 if (EQ (value, Qline))
5686 face->underline_type = FACE_UNDER_LINE;
5687 else if (EQ (value, Qwave))
5688 face->underline_type = FACE_UNDER_WAVE;
5693 overline = attrs[LFACE_OVERLINE_INDEX];
5694 if (STRINGP (overline))
5696 face->overline_color
5697 = load_color (f, face, attrs[LFACE_OVERLINE_INDEX],
5698 LFACE_OVERLINE_INDEX);
5699 face->overline_p = 1;
5701 else if (EQ (overline, Qt))
5703 face->overline_color = face->foreground;
5704 face->overline_color_defaulted_p = 1;
5705 face->overline_p = 1;
5708 strike_through = attrs[LFACE_STRIKE_THROUGH_INDEX];
5709 if (STRINGP (strike_through))
5711 face->strike_through_color
5712 = load_color (f, face, attrs[LFACE_STRIKE_THROUGH_INDEX],
5713 LFACE_STRIKE_THROUGH_INDEX);
5714 face->strike_through_p = 1;
5716 else if (EQ (strike_through, Qt))
5718 face->strike_through_color = face->foreground;
5719 face->strike_through_color_defaulted_p = 1;
5720 face->strike_through_p = 1;
5723 stipple = attrs[LFACE_STIPPLE_INDEX];
5724 if (!NILP (stipple))
5725 face->stipple = load_pixmap (f, stipple, &face->pixmap_w, &face->pixmap_h);
5726 #endif /* HAVE_WINDOW_SYSTEM */
5728 return face;
5732 /* Map a specified color of face FACE on frame F to a tty color index.
5733 IDX is either LFACE_FOREGROUND_INDEX or LFACE_BACKGROUND_INDEX, and
5734 specifies which color to map. Set *DEFAULTED to 1 if mapping to the
5735 default foreground/background colors. */
5737 static void
5738 map_tty_color (struct frame *f, struct face *face,
5739 enum lface_attribute_index idx, int *defaulted)
5741 Lisp_Object frame, color, def;
5742 int foreground_p = idx == LFACE_FOREGROUND_INDEX;
5743 unsigned long default_pixel =
5744 foreground_p ? FACE_TTY_DEFAULT_FG_COLOR : FACE_TTY_DEFAULT_BG_COLOR;
5745 unsigned long pixel = default_pixel;
5746 #ifdef MSDOS
5747 unsigned long default_other_pixel =
5748 foreground_p ? FACE_TTY_DEFAULT_BG_COLOR : FACE_TTY_DEFAULT_FG_COLOR;
5749 #endif
5751 eassert (idx == LFACE_FOREGROUND_INDEX || idx == LFACE_BACKGROUND_INDEX);
5753 XSETFRAME (frame, f);
5754 color = face->lface[idx];
5756 if (STRINGP (color)
5757 && SCHARS (color)
5758 && CONSP (Vtty_defined_color_alist)
5759 && (def = assq_no_quit (color, call1 (Qtty_color_alist, frame)),
5760 CONSP (def)))
5762 /* Associations in tty-defined-color-alist are of the form
5763 (NAME INDEX R G B). We need the INDEX part. */
5764 pixel = XINT (XCAR (XCDR (def)));
5767 if (pixel == default_pixel && STRINGP (color))
5769 pixel = load_color (f, face, color, idx);
5771 #ifdef MSDOS
5772 /* If the foreground of the default face is the default color,
5773 use the foreground color defined by the frame. */
5774 if (FRAME_MSDOS_P (f))
5776 if (pixel == default_pixel
5777 || pixel == FACE_TTY_DEFAULT_COLOR)
5779 if (foreground_p)
5780 pixel = FRAME_FOREGROUND_PIXEL (f);
5781 else
5782 pixel = FRAME_BACKGROUND_PIXEL (f);
5783 face->lface[idx] = tty_color_name (f, pixel);
5784 *defaulted = 1;
5786 else if (pixel == default_other_pixel)
5788 if (foreground_p)
5789 pixel = FRAME_BACKGROUND_PIXEL (f);
5790 else
5791 pixel = FRAME_FOREGROUND_PIXEL (f);
5792 face->lface[idx] = tty_color_name (f, pixel);
5793 *defaulted = 1;
5796 #endif /* MSDOS */
5799 if (foreground_p)
5800 face->foreground = pixel;
5801 else
5802 face->background = pixel;
5806 /* Realize the fully-specified face with attributes ATTRS in face
5807 cache CACHE for ASCII characters. Do it for TTY frame CACHE->f.
5808 Value is a pointer to the newly created realized face. */
5810 static struct face *
5811 realize_tty_face (struct face_cache *cache,
5812 Lisp_Object attrs[LFACE_VECTOR_SIZE])
5814 struct face *face;
5815 int weight, slant;
5816 int face_colors_defaulted = 0;
5817 struct frame *f = cache->f;
5819 /* Frame must be a termcap frame. */
5820 eassert (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f));
5822 /* Allocate a new realized face. */
5823 face = make_realized_face (attrs);
5824 #if 0
5825 face->font_name = FRAME_MSDOS_P (cache->f) ? "ms-dos" : "tty";
5826 #endif
5828 /* Map face attributes to TTY appearances. */
5829 weight = FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]);
5830 slant = FONT_SLANT_NAME_NUMERIC (attrs[LFACE_SLANT_INDEX]);
5831 if (weight > 100)
5832 face->tty_bold_p = 1;
5833 if (slant != 100)
5834 face->tty_italic_p = 1;
5835 if (!NILP (attrs[LFACE_UNDERLINE_INDEX]))
5836 face->tty_underline_p = 1;
5837 if (!NILP (attrs[LFACE_INVERSE_INDEX]))
5838 face->tty_reverse_p = 1;
5840 /* Map color names to color indices. */
5841 map_tty_color (f, face, LFACE_FOREGROUND_INDEX, &face_colors_defaulted);
5842 map_tty_color (f, face, LFACE_BACKGROUND_INDEX, &face_colors_defaulted);
5844 /* Swap colors if face is inverse-video. If the colors are taken
5845 from the frame colors, they are already inverted, since the
5846 frame-creation function calls x-handle-reverse-video. */
5847 if (face->tty_reverse_p && !face_colors_defaulted)
5849 unsigned long tem = face->foreground;
5850 face->foreground = face->background;
5851 face->background = tem;
5854 if (tty_suppress_bold_inverse_default_colors_p
5855 && face->tty_bold_p
5856 && face->background == FACE_TTY_DEFAULT_FG_COLOR
5857 && face->foreground == FACE_TTY_DEFAULT_BG_COLOR)
5858 face->tty_bold_p = 0;
5860 return face;
5864 DEFUN ("tty-suppress-bold-inverse-default-colors",
5865 Ftty_suppress_bold_inverse_default_colors,
5866 Stty_suppress_bold_inverse_default_colors, 1, 1, 0,
5867 doc: /* Suppress/allow boldness of faces with inverse default colors.
5868 SUPPRESS non-nil means suppress it.
5869 This affects bold faces on TTYs whose foreground is the default background
5870 color of the display and whose background is the default foreground color.
5871 For such faces, the bold face attribute is ignored if this variable
5872 is non-nil. */)
5873 (Lisp_Object suppress)
5875 tty_suppress_bold_inverse_default_colors_p = !NILP (suppress);
5876 ++face_change_count;
5877 return suppress;
5882 /***********************************************************************
5883 Computing Faces
5884 ***********************************************************************/
5886 /* Return the ID of the face to use to display character CH with face
5887 property PROP on frame F in current_buffer. */
5890 compute_char_face (struct frame *f, int ch, Lisp_Object prop)
5892 int face_id;
5894 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
5895 ch = 0;
5897 if (NILP (prop))
5899 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5900 face_id = FACE_FOR_CHAR (f, face, ch, -1, Qnil);
5902 else
5904 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5905 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5906 memcpy (attrs, default_face->lface, sizeof attrs);
5907 merge_face_ref (f, prop, attrs, 1, 0);
5908 face_id = lookup_face (f, attrs);
5911 return face_id;
5914 /* Return the face ID associated with buffer position POS for
5915 displaying ASCII characters. Return in *ENDPTR the position at
5916 which a different face is needed, as far as text properties and
5917 overlays are concerned. W is a window displaying current_buffer.
5919 REGION_BEG, REGION_END delimit the region, so it can be
5920 highlighted.
5922 LIMIT is a position not to scan beyond. That is to limit the time
5923 this function can take.
5925 If MOUSE is non-zero, use the character's mouse-face, not its face.
5927 BASE_FACE_ID, if non-negative, specifies a base face id to use
5928 instead of DEFAULT_FACE_ID.
5930 The face returned is suitable for displaying ASCII characters. */
5933 face_at_buffer_position (struct window *w, ptrdiff_t pos,
5934 ptrdiff_t region_beg, ptrdiff_t region_end,
5935 ptrdiff_t *endptr, ptrdiff_t limit,
5936 int mouse, int base_face_id)
5938 struct frame *f = XFRAME (w->frame);
5939 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5940 Lisp_Object prop, position;
5941 ptrdiff_t i, noverlays;
5942 Lisp_Object *overlay_vec;
5943 ptrdiff_t endpos;
5944 Lisp_Object propname = mouse ? Qmouse_face : Qface;
5945 Lisp_Object limit1, end;
5946 struct face *default_face;
5948 /* W must display the current buffer. We could write this function
5949 to use the frame and buffer of W, but right now it doesn't. */
5950 /* eassert (XBUFFER (w->contents) == current_buffer); */
5952 XSETFASTINT (position, pos);
5954 endpos = ZV;
5955 if (pos < region_beg && region_beg < endpos)
5956 endpos = region_beg;
5958 /* Get the `face' or `mouse_face' text property at POS, and
5959 determine the next position at which the property changes. */
5960 prop = Fget_text_property (position, propname, w->contents);
5961 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
5962 end = Fnext_single_property_change (position, propname, w->contents, limit1);
5963 if (INTEGERP (end))
5964 endpos = XINT (end);
5966 /* Look at properties from overlays. */
5968 ptrdiff_t next_overlay;
5970 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, &next_overlay, 0);
5971 if (next_overlay < endpos)
5972 endpos = next_overlay;
5975 *endptr = endpos;
5978 int face_id;
5980 if (base_face_id >= 0)
5981 face_id = base_face_id;
5982 else if (NILP (Vface_remapping_alist))
5983 face_id = DEFAULT_FACE_ID;
5984 else
5985 face_id = lookup_basic_face (f, DEFAULT_FACE_ID);
5987 default_face = FACE_FROM_ID (f, face_id);
5990 /* Optimize common cases where we can use the default face. */
5991 if (noverlays == 0
5992 && NILP (prop)
5993 && !(pos >= region_beg && pos < region_end))
5994 return default_face->id;
5996 /* Begin with attributes from the default face. */
5997 memcpy (attrs, default_face->lface, sizeof attrs);
5999 /* Merge in attributes specified via text properties. */
6000 if (!NILP (prop))
6001 merge_face_ref (f, prop, attrs, 1, 0);
6003 /* Now merge the overlay data. */
6004 noverlays = sort_overlays (overlay_vec, noverlays, w);
6005 for (i = 0; i < noverlays; i++)
6007 Lisp_Object oend;
6008 ptrdiff_t oendpos;
6010 prop = Foverlay_get (overlay_vec[i], propname);
6011 if (!NILP (prop))
6012 merge_face_ref (f, prop, attrs, 1, 0);
6014 oend = OVERLAY_END (overlay_vec[i]);
6015 oendpos = OVERLAY_POSITION (oend);
6016 if (oendpos < endpos)
6017 endpos = oendpos;
6020 /* If in the region, merge in the region face. */
6021 if (pos >= region_beg && pos < region_end)
6023 merge_named_face (f, Qregion, attrs, 0);
6025 if (region_end < endpos)
6026 endpos = region_end;
6029 *endptr = endpos;
6031 /* Look up a realized face with the given face attributes,
6032 or realize a new one for ASCII characters. */
6033 return lookup_face (f, attrs);
6036 /* Return the face ID at buffer position POS for displaying ASCII
6037 characters associated with overlay strings for overlay OVERLAY.
6039 Like face_at_buffer_position except for OVERLAY. Currently it
6040 simply disregards the `face' properties of all overlays. */
6043 face_for_overlay_string (struct window *w, ptrdiff_t pos,
6044 ptrdiff_t region_beg, ptrdiff_t region_end,
6045 ptrdiff_t *endptr, ptrdiff_t limit,
6046 int mouse, Lisp_Object overlay)
6048 struct frame *f = XFRAME (w->frame);
6049 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6050 Lisp_Object prop, position;
6051 ptrdiff_t endpos;
6052 Lisp_Object propname = mouse ? Qmouse_face : Qface;
6053 Lisp_Object limit1, end;
6054 struct face *default_face;
6056 /* W must display the current buffer. We could write this function
6057 to use the frame and buffer of W, but right now it doesn't. */
6058 /* eassert (XBUFFER (w->contents) == current_buffer); */
6060 XSETFASTINT (position, pos);
6062 endpos = ZV;
6063 if (pos < region_beg && region_beg < endpos)
6064 endpos = region_beg;
6066 /* Get the `face' or `mouse_face' text property at POS, and
6067 determine the next position at which the property changes. */
6068 prop = Fget_text_property (position, propname, w->contents);
6069 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
6070 end = Fnext_single_property_change (position, propname, w->contents, limit1);
6071 if (INTEGERP (end))
6072 endpos = XINT (end);
6074 *endptr = endpos;
6076 /* Optimize common case where we can use the default face. */
6077 if (NILP (prop)
6078 && !(pos >= region_beg && pos < region_end)
6079 && NILP (Vface_remapping_alist))
6080 return DEFAULT_FACE_ID;
6082 /* Begin with attributes from the default face. */
6083 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
6084 memcpy (attrs, default_face->lface, sizeof attrs);
6086 /* Merge in attributes specified via text properties. */
6087 if (!NILP (prop))
6088 merge_face_ref (f, prop, attrs, 1, 0);
6090 /* If in the region, merge in the region face. */
6091 if (pos >= region_beg && pos < region_end)
6093 merge_named_face (f, Qregion, attrs, 0);
6095 if (region_end < endpos)
6096 endpos = region_end;
6099 *endptr = endpos;
6101 /* Look up a realized face with the given face attributes,
6102 or realize a new one for ASCII characters. */
6103 return lookup_face (f, attrs);
6107 /* Compute the face at character position POS in Lisp string STRING on
6108 window W, for ASCII characters.
6110 If STRING is an overlay string, it comes from position BUFPOS in
6111 current_buffer, otherwise BUFPOS is zero to indicate that STRING is
6112 not an overlay string. W must display the current buffer.
6113 REGION_BEG and REGION_END give the start and end positions of the
6114 region; both are -1 if no region is visible.
6116 BASE_FACE_ID is the id of a face to merge with. For strings coming
6117 from overlays or the `display' property it is the face at BUFPOS.
6119 If MOUSE_P is non-zero, use the character's mouse-face, not its face.
6121 Set *ENDPTR to the next position where to check for faces in
6122 STRING; -1 if the face is constant from POS to the end of the
6123 string.
6125 Value is the id of the face to use. The face returned is suitable
6126 for displaying ASCII characters. */
6129 face_at_string_position (struct window *w, Lisp_Object string,
6130 ptrdiff_t pos, ptrdiff_t bufpos,
6131 ptrdiff_t region_beg, ptrdiff_t region_end,
6132 ptrdiff_t *endptr, enum face_id base_face_id,
6133 int mouse_p)
6135 Lisp_Object prop, position, end, limit;
6136 struct frame *f = XFRAME (WINDOW_FRAME (w));
6137 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6138 struct face *base_face;
6139 bool multibyte_p = STRING_MULTIBYTE (string);
6140 Lisp_Object prop_name = mouse_p ? Qmouse_face : Qface;
6142 /* Get the value of the face property at the current position within
6143 STRING. Value is nil if there is no face property. */
6144 XSETFASTINT (position, pos);
6145 prop = Fget_text_property (position, prop_name, string);
6147 /* Get the next position at which to check for faces. Value of end
6148 is nil if face is constant all the way to the end of the string.
6149 Otherwise it is a string position where to check faces next.
6150 Limit is the maximum position up to which to check for property
6151 changes in Fnext_single_property_change. Strings are usually
6152 short, so set the limit to the end of the string. */
6153 XSETFASTINT (limit, SCHARS (string));
6154 end = Fnext_single_property_change (position, prop_name, string, limit);
6155 if (INTEGERP (end))
6156 *endptr = XFASTINT (end);
6157 else
6158 *endptr = -1;
6160 base_face = FACE_FROM_ID (f, base_face_id);
6161 eassert (base_face);
6163 /* Optimize the default case that there is no face property and we
6164 are not in the region. */
6165 if (NILP (prop)
6166 && (base_face_id != DEFAULT_FACE_ID
6167 /* BUFPOS <= 0 means STRING is not an overlay string, so
6168 that the region doesn't have to be taken into account. */
6169 || bufpos <= 0
6170 || bufpos < region_beg
6171 || bufpos >= region_end)
6172 && (multibyte_p
6173 /* We can't realize faces for different charsets differently
6174 if we don't have fonts, so we can stop here if not working
6175 on a window-system frame. */
6176 || !FRAME_WINDOW_P (f)
6177 || FACE_SUITABLE_FOR_ASCII_CHAR_P (base_face, 0)))
6178 return base_face->id;
6180 /* Begin with attributes from the base face. */
6181 memcpy (attrs, base_face->lface, sizeof attrs);
6183 /* Merge in attributes specified via text properties. */
6184 if (!NILP (prop))
6185 merge_face_ref (f, prop, attrs, 1, 0);
6187 /* If in the region, merge in the region face. */
6188 if (bufpos
6189 && bufpos >= region_beg
6190 && bufpos < region_end)
6191 merge_named_face (f, Qregion, attrs, 0);
6193 /* Look up a realized face with the given face attributes,
6194 or realize a new one for ASCII characters. */
6195 return lookup_face (f, attrs);
6199 /* Merge a face into a realized face.
6201 F is frame where faces are (to be) realized.
6203 FACE_NAME is named face to merge.
6205 If FACE_NAME is nil, FACE_ID is face_id of realized face to merge.
6207 If FACE_NAME is t, FACE_ID is lface_id of face to merge.
6209 BASE_FACE_ID is realized face to merge into.
6211 Return new face id.
6215 merge_faces (struct frame *f, Lisp_Object face_name, int face_id,
6216 int base_face_id)
6218 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6219 struct face *base_face;
6221 base_face = FACE_FROM_ID (f, base_face_id);
6222 if (!base_face)
6223 return base_face_id;
6225 if (EQ (face_name, Qt))
6227 if (face_id < 0 || face_id >= lface_id_to_name_size)
6228 return base_face_id;
6229 face_name = lface_id_to_name[face_id];
6230 /* When called during make-frame, lookup_derived_face may fail
6231 if the faces are uninitialized. Don't signal an error. */
6232 face_id = lookup_derived_face (f, face_name, base_face_id, 0);
6233 return (face_id >= 0 ? face_id : base_face_id);
6236 /* Begin with attributes from the base face. */
6237 memcpy (attrs, base_face->lface, sizeof attrs);
6239 if (!NILP (face_name))
6241 if (!merge_named_face (f, face_name, attrs, 0))
6242 return base_face_id;
6244 else
6246 struct face *face;
6247 if (face_id < 0)
6248 return base_face_id;
6249 face = FACE_FROM_ID (f, face_id);
6250 if (!face)
6251 return base_face_id;
6252 merge_face_vectors (f, face->lface, attrs, 0);
6255 /* Look up a realized face with the given face attributes,
6256 or realize a new one for ASCII characters. */
6257 return lookup_face (f, attrs);
6262 #ifndef HAVE_X_WINDOWS
6263 DEFUN ("x-load-color-file", Fx_load_color_file,
6264 Sx_load_color_file, 1, 1, 0,
6265 doc: /* Create an alist of color entries from an external file.
6267 The file should define one named RGB color per line like so:
6268 R G B name
6269 where R,G,B are numbers between 0 and 255 and name is an arbitrary string. */)
6270 (Lisp_Object filename)
6272 FILE *fp;
6273 Lisp_Object cmap = Qnil;
6274 Lisp_Object abspath;
6276 CHECK_STRING (filename);
6277 abspath = Fexpand_file_name (filename, Qnil);
6279 block_input ();
6280 fp = emacs_fopen (SSDATA (abspath), "rt");
6281 if (fp)
6283 char buf[512];
6284 int red, green, blue;
6285 int num;
6287 while (fgets (buf, sizeof (buf), fp) != NULL) {
6288 if (sscanf (buf, "%u %u %u %n", &red, &green, &blue, &num) == 3)
6290 #ifdef HAVE_NTGUI
6291 int color = RGB (red, green, blue);
6292 #else
6293 int color = (red << 16) | (green << 8) | blue;
6294 #endif
6295 char *name = buf + num;
6296 ptrdiff_t len = strlen (name);
6297 len -= 0 < len && name[len - 1] == '\n';
6298 cmap = Fcons (Fcons (make_string (name, len), make_number (color)),
6299 cmap);
6302 fclose (fp);
6304 unblock_input ();
6305 return cmap;
6307 #endif
6310 /***********************************************************************
6311 Tests
6312 ***********************************************************************/
6314 #ifdef GLYPH_DEBUG
6316 /* Print the contents of the realized face FACE to stderr. */
6318 static void
6319 dump_realized_face (struct face *face)
6321 fprintf (stderr, "ID: %d\n", face->id);
6322 #ifdef HAVE_X_WINDOWS
6323 fprintf (stderr, "gc: %ld\n", (long) face->gc);
6324 #endif
6325 fprintf (stderr, "foreground: 0x%lx (%s)\n",
6326 face->foreground,
6327 SDATA (face->lface[LFACE_FOREGROUND_INDEX]));
6328 fprintf (stderr, "background: 0x%lx (%s)\n",
6329 face->background,
6330 SDATA (face->lface[LFACE_BACKGROUND_INDEX]));
6331 if (face->font)
6332 fprintf (stderr, "font_name: %s (%s)\n",
6333 SDATA (face->font->props[FONT_NAME_INDEX]),
6334 SDATA (face->lface[LFACE_FAMILY_INDEX]));
6335 #ifdef HAVE_X_WINDOWS
6336 fprintf (stderr, "font = %p\n", face->font);
6337 #endif
6338 fprintf (stderr, "fontset: %d\n", face->fontset);
6339 fprintf (stderr, "underline: %d (%s)\n",
6340 face->underline_p,
6341 SDATA (Fsymbol_name (face->lface[LFACE_UNDERLINE_INDEX])));
6342 fprintf (stderr, "hash: %d\n", face->hash);
6346 DEFUN ("dump-face", Fdump_face, Sdump_face, 0, 1, 0, doc: /* */)
6347 (Lisp_Object n)
6349 if (NILP (n))
6351 int i;
6353 fprintf (stderr, "font selection order: ");
6354 for (i = 0; i < DIM (font_sort_order); ++i)
6355 fprintf (stderr, "%d ", font_sort_order[i]);
6356 fprintf (stderr, "\n");
6358 fprintf (stderr, "alternative fonts: ");
6359 debug_print (Vface_alternative_font_family_alist);
6360 fprintf (stderr, "\n");
6362 for (i = 0; i < FRAME_FACE_CACHE (SELECTED_FRAME ())->used; ++i)
6363 Fdump_face (make_number (i));
6365 else
6367 struct face *face;
6368 CHECK_NUMBER (n);
6369 face = FACE_FROM_ID (SELECTED_FRAME (), XINT (n));
6370 if (face == NULL)
6371 error ("Not a valid face");
6372 dump_realized_face (face);
6375 return Qnil;
6379 DEFUN ("show-face-resources", Fshow_face_resources, Sshow_face_resources,
6380 0, 0, 0, doc: /* */)
6381 (void)
6383 fprintf (stderr, "number of colors = %d\n", ncolors_allocated);
6384 fprintf (stderr, "number of pixmaps = %d\n", npixmaps_allocated);
6385 fprintf (stderr, "number of GCs = %d\n", ngcs);
6386 return Qnil;
6389 #endif /* GLYPH_DEBUG */
6393 /***********************************************************************
6394 Initialization
6395 ***********************************************************************/
6397 void
6398 syms_of_xfaces (void)
6400 DEFSYM (Qface, "face");
6401 DEFSYM (Qface_no_inherit, "face-no-inherit");
6402 DEFSYM (Qbitmap_spec_p, "bitmap-spec-p");
6403 DEFSYM (Qframe_set_background_mode, "frame-set-background-mode");
6405 /* Lisp face attribute keywords. */
6406 DEFSYM (QCfamily, ":family");
6407 DEFSYM (QCheight, ":height");
6408 DEFSYM (QCweight, ":weight");
6409 DEFSYM (QCslant, ":slant");
6410 DEFSYM (QCunderline, ":underline");
6411 DEFSYM (QCinverse_video, ":inverse-video");
6412 DEFSYM (QCreverse_video, ":reverse-video");
6413 DEFSYM (QCforeground, ":foreground");
6414 DEFSYM (QCbackground, ":background");
6415 DEFSYM (QCstipple, ":stipple");
6416 DEFSYM (QCwidth, ":width");
6417 DEFSYM (QCfont, ":font");
6418 DEFSYM (QCfontset, ":fontset");
6419 DEFSYM (QCbold, ":bold");
6420 DEFSYM (QCitalic, ":italic");
6421 DEFSYM (QCoverline, ":overline");
6422 DEFSYM (QCstrike_through, ":strike-through");
6423 DEFSYM (QCbox, ":box");
6424 DEFSYM (QCinherit, ":inherit");
6426 /* Symbols used for Lisp face attribute values. */
6427 DEFSYM (QCcolor, ":color");
6428 DEFSYM (QCline_width, ":line-width");
6429 DEFSYM (QCstyle, ":style");
6430 DEFSYM (Qline, "line");
6431 DEFSYM (Qwave, "wave");
6432 DEFSYM (Qreleased_button, "released-button");
6433 DEFSYM (Qpressed_button, "pressed-button");
6434 DEFSYM (Qnormal, "normal");
6435 DEFSYM (Qextra_light, "extra-light");
6436 DEFSYM (Qlight, "light");
6437 DEFSYM (Qsemi_light, "semi-light");
6438 DEFSYM (Qsemi_bold, "semi-bold");
6439 DEFSYM (Qbold, "bold");
6440 DEFSYM (Qextra_bold, "extra-bold");
6441 DEFSYM (Qultra_bold, "ultra-bold");
6442 DEFSYM (Qoblique, "oblique");
6443 DEFSYM (Qitalic, "italic");
6444 DEFSYM (Qbackground_color, "background-color");
6445 DEFSYM (Qforeground_color, "foreground-color");
6446 DEFSYM (Qunspecified, "unspecified");
6447 DEFSYM (QCignore_defface, ":ignore-defface");
6449 DEFSYM (Qface_alias, "face-alias");
6450 DEFSYM (Qdefault, "default");
6451 DEFSYM (Qtool_bar, "tool-bar");
6452 DEFSYM (Qregion, "region");
6453 DEFSYM (Qfringe, "fringe");
6454 DEFSYM (Qheader_line, "header-line");
6455 DEFSYM (Qscroll_bar, "scroll-bar");
6456 DEFSYM (Qmenu, "menu");
6457 DEFSYM (Qcursor, "cursor");
6458 DEFSYM (Qborder, "border");
6459 DEFSYM (Qmouse, "mouse");
6460 DEFSYM (Qmode_line_inactive, "mode-line-inactive");
6461 DEFSYM (Qvertical_border, "vertical-border");
6462 DEFSYM (Qtty_color_desc, "tty-color-desc");
6463 DEFSYM (Qtty_color_standard_values, "tty-color-standard-values");
6464 DEFSYM (Qtty_color_by_index, "tty-color-by-index");
6465 DEFSYM (Qtty_color_alist, "tty-color-alist");
6466 DEFSYM (Qscalable_fonts_allowed, "scalable-fonts-allowed");
6468 Vparam_value_alist = list1 (Fcons (Qnil, Qnil));
6469 staticpro (&Vparam_value_alist);
6470 Vface_alternative_font_family_alist = Qnil;
6471 staticpro (&Vface_alternative_font_family_alist);
6472 Vface_alternative_font_registry_alist = Qnil;
6473 staticpro (&Vface_alternative_font_registry_alist);
6475 defsubr (&Sinternal_make_lisp_face);
6476 defsubr (&Sinternal_lisp_face_p);
6477 defsubr (&Sinternal_set_lisp_face_attribute);
6478 #ifdef HAVE_WINDOW_SYSTEM
6479 defsubr (&Sinternal_set_lisp_face_attribute_from_resource);
6480 #endif
6481 defsubr (&Scolor_gray_p);
6482 defsubr (&Scolor_supported_p);
6483 #ifndef HAVE_X_WINDOWS
6484 defsubr (&Sx_load_color_file);
6485 #endif
6486 defsubr (&Sface_attribute_relative_p);
6487 defsubr (&Smerge_face_attribute);
6488 defsubr (&Sinternal_get_lisp_face_attribute);
6489 defsubr (&Sinternal_lisp_face_attribute_values);
6490 defsubr (&Sinternal_lisp_face_equal_p);
6491 defsubr (&Sinternal_lisp_face_empty_p);
6492 defsubr (&Sinternal_copy_lisp_face);
6493 defsubr (&Sinternal_merge_in_global_face);
6494 defsubr (&Sface_font);
6495 defsubr (&Sframe_face_alist);
6496 defsubr (&Sdisplay_supports_face_attributes_p);
6497 defsubr (&Scolor_distance);
6498 defsubr (&Sinternal_set_font_selection_order);
6499 defsubr (&Sinternal_set_alternative_font_family_alist);
6500 defsubr (&Sinternal_set_alternative_font_registry_alist);
6501 defsubr (&Sface_attributes_as_vector);
6502 #ifdef GLYPH_DEBUG
6503 defsubr (&Sdump_face);
6504 defsubr (&Sshow_face_resources);
6505 #endif /* GLYPH_DEBUG */
6506 defsubr (&Sclear_face_cache);
6507 defsubr (&Stty_suppress_bold_inverse_default_colors);
6509 #if defined DEBUG_X_COLORS && defined HAVE_X_WINDOWS
6510 defsubr (&Sdump_colors);
6511 #endif
6513 DEFVAR_LISP ("face-new-frame-defaults", Vface_new_frame_defaults,
6514 doc: /* List of global face definitions (for internal use only.) */);
6515 Vface_new_frame_defaults = Qnil;
6517 DEFVAR_LISP ("face-default-stipple", Vface_default_stipple,
6518 doc: /* Default stipple pattern used on monochrome displays.
6519 This stipple pattern is used on monochrome displays
6520 instead of shades of gray for a face background color.
6521 See `set-face-stipple' for possible values for this variable. */);
6522 Vface_default_stipple = build_pure_c_string ("gray3");
6524 DEFVAR_LISP ("tty-defined-color-alist", Vtty_defined_color_alist,
6525 doc: /* An alist of defined terminal colors and their RGB values.
6526 See the docstring of `tty-color-alist' for the details. */);
6527 Vtty_defined_color_alist = Qnil;
6529 DEFVAR_LISP ("scalable-fonts-allowed", Vscalable_fonts_allowed,
6530 doc: /* Allowed scalable fonts.
6531 A value of nil means don't allow any scalable fonts.
6532 A value of t means allow any scalable font.
6533 Otherwise, value must be a list of regular expressions. A font may be
6534 scaled if its name matches a regular expression in the list.
6535 Note that if value is nil, a scalable font might still be used, if no
6536 other font of the appropriate family and registry is available. */);
6537 Vscalable_fonts_allowed = Qnil;
6539 DEFVAR_LISP ("face-ignored-fonts", Vface_ignored_fonts,
6540 doc: /* List of ignored fonts.
6541 Each element is a regular expression that matches names of fonts to
6542 ignore. */);
6543 Vface_ignored_fonts = Qnil;
6545 DEFVAR_LISP ("face-remapping-alist", Vface_remapping_alist,
6546 doc: /* Alist of face remappings.
6547 Each element is of the form:
6549 (FACE . REPLACEMENT),
6551 which causes display of the face FACE to use REPLACEMENT instead.
6552 REPLACEMENT is a face specification, i.e. one of the following:
6554 (1) a face name
6555 (2) a property list of attribute/value pairs, or
6556 (3) a list in which each element has the form of (1) or (2).
6558 List values for REPLACEMENT are merged to form the final face
6559 specification, with earlier entries taking precedence, in the same as
6560 as in the `face' text property.
6562 Face-name remapping cycles are suppressed; recursive references use
6563 the underlying face instead of the remapped face. So a remapping of
6564 the form:
6566 (FACE EXTRA-FACE... FACE)
6570 (FACE (FACE-ATTR VAL ...) FACE)
6572 causes EXTRA-FACE... or (FACE-ATTR VAL ...) to be _merged_ with the
6573 existing definition of FACE. Note that this isn't necessary for the
6574 default face, since every face inherits from the default face.
6576 If this variable is made buffer-local, the face remapping takes effect
6577 only in that buffer. For instance, the mode my-mode could define a
6578 face `my-mode-default', and then in the mode setup function, do:
6580 (set (make-local-variable 'face-remapping-alist)
6581 '((default my-mode-default)))).
6583 Because Emacs normally only redraws screen areas when the underlying
6584 buffer contents change, you may need to call `redraw-display' after
6585 changing this variable for it to take effect. */);
6586 Vface_remapping_alist = Qnil;
6588 DEFVAR_LISP ("face-font-rescale-alist", Vface_font_rescale_alist,
6589 doc: /* Alist of fonts vs the rescaling factors.
6590 Each element is a cons (FONT-PATTERN . RESCALE-RATIO), where
6591 FONT-PATTERN is a font-spec or a regular expression matching a font name, and
6592 RESCALE-RATIO is a floating point number to specify how much larger
6593 \(or smaller) font we should use. For instance, if a face requests
6594 a font of 10 point, we actually use a font of 10 * RESCALE-RATIO point. */);
6595 Vface_font_rescale_alist = Qnil;
6597 #ifdef HAVE_WINDOW_SYSTEM
6598 defsubr (&Sbitmap_spec_p);
6599 defsubr (&Sx_list_fonts);
6600 defsubr (&Sinternal_face_x_get_resource);
6601 defsubr (&Sx_family_fonts);
6602 #endif