merge from trunk
[emacs.git] / src / xfaces.c
blobe8cb7a703221d05880ac06f73fe98782c2f629bc
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 USE_MOTIF
215 #include <Xm/Xm.h>
216 #include <Xm/XmStrDefs.h>
217 #endif /* USE_MOTIF */
219 #ifdef MSDOS
220 #include "dosfns.h"
221 #endif
223 #ifdef HAVE_WINDOW_SYSTEM
224 #include TERM_HEADER
225 #include "fontset.h"
226 #ifdef HAVE_NTGUI
227 #undef FRAME_X_DISPLAY_INFO
228 #define FRAME_X_DISPLAY_INFO FRAME_W32_DISPLAY_INFO
229 #define x_display_info w32_display_info
230 #define GCGraphicsExposures 0
231 #endif /* HAVE_NTGUI */
233 #ifdef HAVE_NS
234 #undef FRAME_X_DISPLAY_INFO
235 #define FRAME_X_DISPLAY_INFO FRAME_NS_DISPLAY_INFO
236 #define GCGraphicsExposures 0
237 #endif /* HAVE_NS */
239 /* Number of pt per inch (from the TeXbook). */
241 #define PT_PER_INCH 72.27
243 #endif /* HAVE_WINDOW_SYSTEM */
245 #include "buffer.h"
246 #include "dispextern.h"
247 #include "blockinput.h"
248 #include "window.h"
249 #include "intervals.h"
250 #include "termchar.h"
252 #include "font.h"
254 #ifdef HAVE_X_WINDOWS
256 /* Compensate for a bug in Xos.h on some systems, on which it requires
257 time.h. On some such systems, Xos.h tries to redefine struct
258 timeval and struct timezone if USG is #defined while it is
259 #included. */
261 #ifdef XOS_NEEDS_TIME_H
262 #include <time.h>
263 #undef USG
264 #include <X11/Xos.h>
265 #define USG
266 #define __TIMEVAL__
267 #if defined USG || defined __TIMEVAL__ /* Don't warn about unused macros. */
268 #endif
269 #else /* not XOS_NEEDS_TIME_H */
270 #include <X11/Xos.h>
271 #endif /* not XOS_NEEDS_TIME_H */
273 #endif /* HAVE_X_WINDOWS */
275 #include <c-ctype.h>
277 /* Non-zero if face attribute ATTR is unspecified. */
279 #define UNSPECIFIEDP(ATTR) EQ ((ATTR), Qunspecified)
281 /* Non-zero if face attribute ATTR is `ignore-defface'. */
283 #define IGNORE_DEFFACE_P(ATTR) EQ ((ATTR), QCignore_defface)
285 /* Value is the number of elements of VECTOR. */
287 #define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
289 /* Size of hash table of realized faces in face caches (should be a
290 prime number). */
292 #define FACE_CACHE_BUCKETS_SIZE 1001
294 /* Keyword symbols used for face attribute names. */
296 Lisp_Object QCfamily, QCheight, QCweight, QCslant;
297 static Lisp_Object QCunderline;
298 static Lisp_Object QCinverse_video, QCstipple;
299 Lisp_Object QCforeground, QCbackground;
300 Lisp_Object QCwidth;
301 static Lisp_Object QCfont, QCbold, QCitalic;
302 static Lisp_Object QCreverse_video;
303 static Lisp_Object QCoverline, QCstrike_through, QCbox, QCinherit;
304 static Lisp_Object QCfontset;
306 /* Symbols used for attribute values. */
308 Lisp_Object Qnormal;
309 Lisp_Object Qbold;
310 static Lisp_Object Qline, Qwave;
311 Lisp_Object Qextra_light, Qlight;
312 Lisp_Object Qsemi_light, Qsemi_bold, Qextra_bold, Qultra_bold;
313 Lisp_Object Qoblique;
314 Lisp_Object Qitalic;
315 static Lisp_Object Qreleased_button, Qpressed_button;
316 static Lisp_Object QCstyle, QCcolor, QCline_width;
317 Lisp_Object Qunspecified; /* used in dosfns.c */
318 static Lisp_Object QCignore_defface;
320 char unspecified_fg[] = "unspecified-fg", unspecified_bg[] = "unspecified-bg";
322 /* The name of the function to call when the background of the frame
323 has changed, frame_set_background_mode. */
325 static Lisp_Object Qframe_set_background_mode;
327 /* Names of basic faces. */
329 Lisp_Object Qdefault, Qtool_bar, Qfringe;
330 static Lisp_Object Qregion;
331 Lisp_Object Qheader_line, Qscroll_bar, Qcursor;
332 static Lisp_Object Qborder, Qmouse, Qmenu;
333 Lisp_Object Qmode_line_inactive;
334 static Lisp_Object Qvertical_border;
336 /* The symbol `face-alias'. A symbols having that property is an
337 alias for another face. Value of the property is the name of
338 the aliased face. */
340 static Lisp_Object Qface_alias;
342 /* Alist of alternative font families. Each element is of the form
343 (FAMILY FAMILY1 FAMILY2 ...). If fonts of FAMILY can't be loaded,
344 try FAMILY1, then FAMILY2, ... */
346 Lisp_Object Vface_alternative_font_family_alist;
348 /* Alist of alternative font registries. Each element is of the form
349 (REGISTRY REGISTRY1 REGISTRY2...). If fonts of REGISTRY can't be
350 loaded, try REGISTRY1, then REGISTRY2, ... */
352 Lisp_Object Vface_alternative_font_registry_alist;
354 /* Allowed scalable fonts. A value of nil means don't allow any
355 scalable fonts. A value of t means allow the use of any scalable
356 font. Otherwise, value must be a list of regular expressions. A
357 font may be scaled if its name matches a regular expression in the
358 list. */
360 static Lisp_Object Qscalable_fonts_allowed;
362 /* The symbols `foreground-color' and `background-color' which can be
363 used as part of a `face' property. This is for compatibility with
364 Emacs 20.2. */
366 Lisp_Object Qforeground_color, Qbackground_color;
368 /* The symbols `face' and `mouse-face' used as text properties. */
370 Lisp_Object Qface;
372 /* Property for basic faces which other faces cannot inherit. */
374 static Lisp_Object Qface_no_inherit;
376 /* Error symbol for wrong_type_argument in load_pixmap. */
378 static Lisp_Object Qbitmap_spec_p;
380 /* The next ID to assign to Lisp faces. */
382 static int next_lface_id;
384 /* A vector mapping Lisp face Id's to face names. */
386 static Lisp_Object *lface_id_to_name;
387 static ptrdiff_t lface_id_to_name_size;
389 /* TTY color-related functions (defined in tty-colors.el). */
391 static Lisp_Object Qtty_color_desc, Qtty_color_by_index, Qtty_color_standard_values;
393 /* The name of the function used to compute colors on TTYs. */
395 static Lisp_Object Qtty_color_alist;
397 #ifdef HAVE_WINDOW_SYSTEM
399 /* Counter for calls to clear_face_cache. If this counter reaches
400 CLEAR_FONT_TABLE_COUNT, and a frame has more than
401 CLEAR_FONT_TABLE_NFONTS load, unused fonts are freed. */
403 static int clear_font_table_count;
404 #define CLEAR_FONT_TABLE_COUNT 100
405 #define CLEAR_FONT_TABLE_NFONTS 10
407 #endif /* HAVE_WINDOW_SYSTEM */
409 /* Non-zero means face attributes have been changed since the last
410 redisplay. Used in redisplay_internal. */
412 int face_change_count;
414 /* Non-zero means don't display bold text if a face's foreground
415 and background colors are the inverse of the default colors of the
416 display. This is a kluge to suppress `bold black' foreground text
417 which is hard to read on an LCD monitor. */
419 static int tty_suppress_bold_inverse_default_colors_p;
421 /* A list of the form `((x . y))' used to avoid consing in
422 Finternal_set_lisp_face_attribute. */
424 static Lisp_Object Vparam_value_alist;
426 /* The total number of colors currently allocated. */
428 #ifdef GLYPH_DEBUG
429 static int ncolors_allocated;
430 static int npixmaps_allocated;
431 static int ngcs;
432 #endif
434 /* Non-zero means the definition of the `menu' face for new frames has
435 been changed. */
437 static int menu_face_changed_default;
439 struct named_merge_point;
441 static struct face *realize_face (struct face_cache *, Lisp_Object *,
442 int);
443 static struct face *realize_x_face (struct face_cache *, Lisp_Object *);
444 static struct face *realize_tty_face (struct face_cache *, Lisp_Object *);
445 static bool realize_basic_faces (struct frame *);
446 static bool realize_default_face (struct frame *);
447 static void realize_named_face (struct frame *, Lisp_Object, int);
448 static struct face_cache *make_face_cache (struct frame *);
449 static void free_face_cache (struct face_cache *);
450 static int merge_face_ref (struct frame *, Lisp_Object, Lisp_Object *,
451 int, struct named_merge_point *);
453 #ifdef HAVE_WINDOW_SYSTEM
454 static void set_font_frame_param (Lisp_Object, Lisp_Object);
455 static void clear_face_gcs (struct face_cache *);
456 static struct face *realize_non_ascii_face (struct frame *, Lisp_Object,
457 struct face *);
458 #endif /* HAVE_WINDOW_SYSTEM */
460 /***********************************************************************
461 Utilities
462 ***********************************************************************/
464 #ifdef HAVE_X_WINDOWS
466 #ifdef DEBUG_X_COLORS
468 /* The following is a poor mans infrastructure for debugging X color
469 allocation problems on displays with PseudoColor-8. Some X servers
470 like 3.3.5 XF86_SVGA with Matrox cards apparently don't implement
471 color reference counts completely so that they don't signal an
472 error when a color is freed whose reference count is already 0.
473 Other X servers do. To help me debug this, the following code
474 implements a simple reference counting schema of its own, for a
475 single display/screen. --gerd. */
477 /* Reference counts for pixel colors. */
479 int color_count[256];
481 /* Register color PIXEL as allocated. */
483 void
484 register_color (unsigned long pixel)
486 eassert (pixel < 256);
487 ++color_count[pixel];
491 /* Register color PIXEL as deallocated. */
493 void
494 unregister_color (unsigned long pixel)
496 eassert (pixel < 256);
497 if (color_count[pixel] > 0)
498 --color_count[pixel];
499 else
500 emacs_abort ();
504 /* Register N colors from PIXELS as deallocated. */
506 void
507 unregister_colors (unsigned long *pixels, int n)
509 int i;
510 for (i = 0; i < n; ++i)
511 unregister_color (pixels[i]);
515 DEFUN ("dump-colors", Fdump_colors, Sdump_colors, 0, 0, 0,
516 doc: /* Dump currently allocated colors to stderr. */)
517 (void)
519 int i, n;
521 fputc ('\n', stderr);
523 for (i = n = 0; i < sizeof color_count / sizeof color_count[0]; ++i)
524 if (color_count[i])
526 fprintf (stderr, "%3d: %5d", i, color_count[i]);
527 ++n;
528 if (n % 5 == 0)
529 fputc ('\n', stderr);
530 else
531 fputc ('\t', stderr);
534 if (n % 5 != 0)
535 fputc ('\n', stderr);
536 return Qnil;
539 #endif /* DEBUG_X_COLORS */
542 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
543 color values. Interrupt input must be blocked when this function
544 is called. */
546 void
547 x_free_colors (struct frame *f, long unsigned int *pixels, int npixels)
549 int class = FRAME_X_DISPLAY_INFO (f)->visual->class;
551 /* If display has an immutable color map, freeing colors is not
552 necessary and some servers don't allow it. So don't do it. */
553 if (class != StaticColor && class != StaticGray && class != TrueColor)
555 #ifdef DEBUG_X_COLORS
556 unregister_colors (pixels, npixels);
557 #endif
558 XFreeColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
559 pixels, npixels, 0);
564 #ifdef USE_X_TOOLKIT
566 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
567 color values. Interrupt input must be blocked when this function
568 is called. */
570 void
571 x_free_dpy_colors (Display *dpy, Screen *screen, Colormap cmap,
572 long unsigned int *pixels, int npixels)
574 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
575 int class = dpyinfo->visual->class;
577 /* If display has an immutable color map, freeing colors is not
578 necessary and some servers don't allow it. So don't do it. */
579 if (class != StaticColor && class != StaticGray && class != TrueColor)
581 #ifdef DEBUG_X_COLORS
582 unregister_colors (pixels, npixels);
583 #endif
584 XFreeColors (dpy, cmap, pixels, npixels, 0);
587 #endif /* USE_X_TOOLKIT */
589 /* Create and return a GC for use on frame F. GC values and mask
590 are given by XGCV and MASK. */
592 static GC
593 x_create_gc (struct frame *f, long unsigned int mask, XGCValues *xgcv)
595 GC gc;
596 block_input ();
597 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), mask, xgcv);
598 unblock_input ();
599 IF_DEBUG (++ngcs);
600 return gc;
604 /* Free GC which was used on frame F. */
606 static void
607 x_free_gc (struct frame *f, GC gc)
609 eassert (input_blocked_p ());
610 IF_DEBUG (eassert (--ngcs >= 0));
611 XFreeGC (FRAME_X_DISPLAY (f), gc);
614 #endif /* HAVE_X_WINDOWS */
616 #ifdef HAVE_NTGUI
617 /* W32 emulation of GCs */
619 static GC
620 x_create_gc (struct frame *f, unsigned long mask, XGCValues *xgcv)
622 GC gc;
623 block_input ();
624 gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, xgcv);
625 unblock_input ();
626 IF_DEBUG (++ngcs);
627 return gc;
631 /* Free GC which was used on frame F. */
633 static void
634 x_free_gc (struct frame *f, GC gc)
636 IF_DEBUG (eassert (--ngcs >= 0));
637 xfree (gc);
640 #endif /* HAVE_NTGUI */
642 #ifdef HAVE_NS
643 /* NS emulation of GCs */
645 static GC
646 x_create_gc (struct frame *f,
647 unsigned long mask,
648 XGCValues *xgcv)
650 GC gc = xmalloc (sizeof *gc);
651 *gc = *xgcv;
652 return gc;
655 static void
656 x_free_gc (struct frame *f, GC gc)
658 xfree (gc);
660 #endif /* HAVE_NS */
662 /***********************************************************************
663 Frames and faces
664 ***********************************************************************/
666 /* Initialize face cache and basic faces for frame F. */
668 void
669 init_frame_faces (struct frame *f)
671 /* Make a face cache, if F doesn't have one. */
672 if (FRAME_FACE_CACHE (f) == NULL)
673 FRAME_FACE_CACHE (f) = make_face_cache (f);
675 #ifdef HAVE_WINDOW_SYSTEM
676 /* Make the image cache. */
677 if (FRAME_WINDOW_P (f))
679 /* We initialize the image cache when creating the first frame
680 on a terminal, and not during terminal creation. This way,
681 `x-open-connection' on a tty won't create an image cache. */
682 if (FRAME_IMAGE_CACHE (f) == NULL)
683 FRAME_IMAGE_CACHE (f) = make_image_cache ();
684 ++FRAME_IMAGE_CACHE (f)->refcount;
686 #endif /* HAVE_WINDOW_SYSTEM */
688 /* Realize basic faces. Must have enough information in frame
689 parameters to realize basic faces at this point. */
690 #ifdef HAVE_X_WINDOWS
691 if (!FRAME_X_P (f) || FRAME_X_WINDOW (f))
692 #endif
693 #ifdef HAVE_NTGUI
694 if (!FRAME_WINDOW_P (f) || FRAME_W32_WINDOW (f))
695 #endif
696 #ifdef HAVE_NS
697 if (!FRAME_NS_P (f) || FRAME_NS_WINDOW (f))
698 #endif
699 if (!realize_basic_faces (f))
700 emacs_abort ();
704 /* Free face cache of frame F. Called from delete_frame. */
706 void
707 free_frame_faces (struct frame *f)
709 struct face_cache *face_cache = FRAME_FACE_CACHE (f);
711 if (face_cache)
713 free_face_cache (face_cache);
714 FRAME_FACE_CACHE (f) = NULL;
717 #ifdef HAVE_WINDOW_SYSTEM
718 if (FRAME_WINDOW_P (f))
720 struct image_cache *image_cache = FRAME_IMAGE_CACHE (f);
721 if (image_cache)
723 --image_cache->refcount;
724 if (image_cache->refcount == 0)
725 free_image_cache (f);
728 #endif /* HAVE_WINDOW_SYSTEM */
732 /* Clear face caches, and recompute basic faces for frame F. Call
733 this after changing frame parameters on which those faces depend,
734 or when realized faces have been freed due to changing attributes
735 of named faces. */
737 void
738 recompute_basic_faces (struct frame *f)
740 if (FRAME_FACE_CACHE (f))
742 clear_face_cache (0);
743 if (!realize_basic_faces (f))
744 emacs_abort ();
749 /* Clear the face caches of all frames. CLEAR_FONTS_P non-zero means
750 try to free unused fonts, too. */
752 void
753 clear_face_cache (int clear_fonts_p)
755 #ifdef HAVE_WINDOW_SYSTEM
756 Lisp_Object tail, frame;
758 if (clear_fonts_p
759 || ++clear_font_table_count == CLEAR_FONT_TABLE_COUNT)
761 /* From time to time see if we can unload some fonts. This also
762 frees all realized faces on all frames. Fonts needed by
763 faces will be loaded again when faces are realized again. */
764 clear_font_table_count = 0;
766 FOR_EACH_FRAME (tail, frame)
768 struct frame *f = XFRAME (frame);
769 if (FRAME_WINDOW_P (f)
770 && FRAME_X_DISPLAY_INFO (f)->n_fonts > CLEAR_FONT_TABLE_NFONTS)
772 clear_font_cache (f);
773 free_all_realized_faces (frame);
777 else
779 /* Clear GCs of realized faces. */
780 FOR_EACH_FRAME (tail, frame)
782 struct frame *f = XFRAME (frame);
783 if (FRAME_WINDOW_P (f))
784 clear_face_gcs (FRAME_FACE_CACHE (f));
786 clear_image_caches (Qnil);
788 #endif /* HAVE_WINDOW_SYSTEM */
792 DEFUN ("clear-face-cache", Fclear_face_cache, Sclear_face_cache, 0, 1, 0,
793 doc: /* Clear face caches on all frames.
794 Optional THOROUGHLY non-nil means try to free unused fonts, too. */)
795 (Lisp_Object thoroughly)
797 clear_face_cache (!NILP (thoroughly));
798 ++face_change_count;
799 ++windows_or_buffers_changed;
800 return Qnil;
804 /***********************************************************************
805 X Pixmaps
806 ***********************************************************************/
808 #ifdef HAVE_WINDOW_SYSTEM
810 DEFUN ("bitmap-spec-p", Fbitmap_spec_p, Sbitmap_spec_p, 1, 1, 0,
811 doc: /* Value is non-nil if OBJECT is a valid bitmap specification.
812 A bitmap specification is either a string, a file name, or a list
813 \(WIDTH HEIGHT DATA) where WIDTH is the pixel width of the bitmap,
814 HEIGHT is its height, and DATA is a string containing the bits of
815 the pixmap. Bits are stored row by row, each row occupies
816 \(WIDTH + 7)/8 bytes. */)
817 (Lisp_Object object)
819 bool pixmap_p = 0;
821 if (STRINGP (object))
822 /* If OBJECT is a string, it's a file name. */
823 pixmap_p = 1;
824 else if (CONSP (object))
826 /* Otherwise OBJECT must be (WIDTH HEIGHT DATA), WIDTH and
827 HEIGHT must be ints > 0, and DATA must be string large
828 enough to hold a bitmap of the specified size. */
829 Lisp_Object width, height, data;
831 height = width = data = Qnil;
833 if (CONSP (object))
835 width = XCAR (object);
836 object = XCDR (object);
837 if (CONSP (object))
839 height = XCAR (object);
840 object = XCDR (object);
841 if (CONSP (object))
842 data = XCAR (object);
846 if (STRINGP (data)
847 && RANGED_INTEGERP (1, width, INT_MAX)
848 && RANGED_INTEGERP (1, height, INT_MAX))
850 int bytes_per_row = ((XINT (width) + BITS_PER_CHAR - 1)
851 / BITS_PER_CHAR);
852 if (XINT (height) <= SBYTES (data) / bytes_per_row)
853 pixmap_p = 1;
857 return pixmap_p ? Qt : Qnil;
861 /* Load a bitmap according to NAME (which is either a file name or a
862 pixmap spec) for use on frame F. Value is the bitmap_id (see
863 xfns.c). If NAME is nil, return with a bitmap id of zero. If
864 bitmap cannot be loaded, display a message saying so, and return
865 zero. Store the bitmap width in *W_PTR and its height in *H_PTR,
866 if these pointers are not null. */
868 static ptrdiff_t
869 load_pixmap (struct frame *f, Lisp_Object name, unsigned int *w_ptr,
870 unsigned int *h_ptr)
872 ptrdiff_t bitmap_id;
874 if (NILP (name))
875 return 0;
877 CHECK_TYPE (!NILP (Fbitmap_spec_p (name)), Qbitmap_spec_p, name);
879 block_input ();
880 if (CONSP (name))
882 /* Decode a bitmap spec into a bitmap. */
884 int h, w;
885 Lisp_Object bits;
887 w = XINT (Fcar (name));
888 h = XINT (Fcar (Fcdr (name)));
889 bits = Fcar (Fcdr (Fcdr (name)));
891 bitmap_id = x_create_bitmap_from_data (f, SSDATA (bits),
892 w, h);
894 else
896 /* It must be a string -- a file name. */
897 bitmap_id = x_create_bitmap_from_file (f, name);
899 unblock_input ();
901 if (bitmap_id < 0)
903 add_to_log ("Invalid or undefined bitmap `%s'", name, Qnil);
904 bitmap_id = 0;
906 if (w_ptr)
907 *w_ptr = 0;
908 if (h_ptr)
909 *h_ptr = 0;
911 else
913 #ifdef GLYPH_DEBUG
914 ++npixmaps_allocated;
915 #endif
916 if (w_ptr)
917 *w_ptr = x_bitmap_width (f, bitmap_id);
919 if (h_ptr)
920 *h_ptr = x_bitmap_height (f, bitmap_id);
923 return bitmap_id;
926 #endif /* HAVE_WINDOW_SYSTEM */
930 /***********************************************************************
931 X Colors
932 ***********************************************************************/
934 /* Parse RGB_LIST, and fill in the RGB fields of COLOR.
935 RGB_LIST should contain (at least) 3 lisp integers.
936 Return 0 if there's a problem with RGB_LIST, otherwise return 1. */
938 static int
939 parse_rgb_list (Lisp_Object rgb_list, XColor *color)
941 #define PARSE_RGB_LIST_FIELD(field) \
942 if (CONSP (rgb_list) && INTEGERP (XCAR (rgb_list))) \
944 color->field = XINT (XCAR (rgb_list)); \
945 rgb_list = XCDR (rgb_list); \
947 else \
948 return 0;
950 PARSE_RGB_LIST_FIELD (red);
951 PARSE_RGB_LIST_FIELD (green);
952 PARSE_RGB_LIST_FIELD (blue);
954 return 1;
958 /* Lookup on frame F the color described by the lisp string COLOR.
959 The resulting tty color is returned in TTY_COLOR; if STD_COLOR is
960 non-zero, then the `standard' definition of the same color is
961 returned in it. */
963 static bool
964 tty_lookup_color (struct frame *f, Lisp_Object color, XColor *tty_color,
965 XColor *std_color)
967 Lisp_Object frame, color_desc;
969 if (!STRINGP (color) || NILP (Ffboundp (Qtty_color_desc)))
970 return 0;
972 XSETFRAME (frame, f);
974 color_desc = call2 (Qtty_color_desc, color, frame);
975 if (CONSP (color_desc) && CONSP (XCDR (color_desc)))
977 Lisp_Object rgb;
979 if (! INTEGERP (XCAR (XCDR (color_desc))))
980 return 0;
982 tty_color->pixel = XINT (XCAR (XCDR (color_desc)));
984 rgb = XCDR (XCDR (color_desc));
985 if (! parse_rgb_list (rgb, tty_color))
986 return 0;
988 /* Should we fill in STD_COLOR too? */
989 if (std_color)
991 /* Default STD_COLOR to the same as TTY_COLOR. */
992 *std_color = *tty_color;
994 /* Do a quick check to see if the returned descriptor is
995 actually _exactly_ equal to COLOR, otherwise we have to
996 lookup STD_COLOR separately. If it's impossible to lookup
997 a standard color, we just give up and use TTY_COLOR. */
998 if ((!STRINGP (XCAR (color_desc))
999 || NILP (Fstring_equal (color, XCAR (color_desc))))
1000 && !NILP (Ffboundp (Qtty_color_standard_values)))
1002 /* Look up STD_COLOR separately. */
1003 rgb = call1 (Qtty_color_standard_values, color);
1004 if (! parse_rgb_list (rgb, std_color))
1005 return 0;
1009 return 1;
1011 else if (NILP (Fsymbol_value (intern ("tty-defined-color-alist"))))
1012 /* We were called early during startup, and the colors are not
1013 yet set up in tty-defined-color-alist. Don't return a failure
1014 indication, since this produces the annoying "Unable to
1015 load color" messages in the *Messages* buffer. */
1016 return 1;
1017 else
1018 /* tty-color-desc seems to have returned a bad value. */
1019 return 0;
1022 /* A version of defined_color for non-X frames. */
1024 static bool
1025 tty_defined_color (struct frame *f, const char *color_name,
1026 XColor *color_def, bool alloc)
1028 bool status = 1;
1030 /* Defaults. */
1031 color_def->pixel = FACE_TTY_DEFAULT_COLOR;
1032 color_def->red = 0;
1033 color_def->blue = 0;
1034 color_def->green = 0;
1036 if (*color_name)
1037 status = tty_lookup_color (f, build_string (color_name), color_def, NULL);
1039 if (color_def->pixel == FACE_TTY_DEFAULT_COLOR && *color_name)
1041 if (strcmp (color_name, "unspecified-fg") == 0)
1042 color_def->pixel = FACE_TTY_DEFAULT_FG_COLOR;
1043 else if (strcmp (color_name, "unspecified-bg") == 0)
1044 color_def->pixel = FACE_TTY_DEFAULT_BG_COLOR;
1047 if (color_def->pixel != FACE_TTY_DEFAULT_COLOR)
1048 status = 1;
1050 return status;
1054 /* Decide if color named COLOR_NAME is valid for the display
1055 associated with the frame F; if so, return the rgb values in
1056 COLOR_DEF. If ALLOC, allocate a new colormap cell.
1058 This does the right thing for any type of frame. */
1060 static bool
1061 defined_color (struct frame *f, const char *color_name, XColor *color_def,
1062 bool alloc)
1064 if (!FRAME_WINDOW_P (f))
1065 return tty_defined_color (f, color_name, color_def, alloc);
1066 #ifdef HAVE_X_WINDOWS
1067 else if (FRAME_X_P (f))
1068 return x_defined_color (f, color_name, color_def, alloc);
1069 #endif
1070 #ifdef HAVE_NTGUI
1071 else if (FRAME_W32_P (f))
1072 return w32_defined_color (f, color_name, color_def, alloc);
1073 #endif
1074 #ifdef HAVE_NS
1075 else if (FRAME_NS_P (f))
1076 return ns_defined_color (f, color_name, color_def, alloc, 1);
1077 #endif
1078 else
1079 emacs_abort ();
1083 /* Given the index IDX of a tty color on frame F, return its name, a
1084 Lisp string. */
1086 Lisp_Object
1087 tty_color_name (struct frame *f, int idx)
1089 if (idx >= 0 && !NILP (Ffboundp (Qtty_color_by_index)))
1091 Lisp_Object frame;
1092 Lisp_Object coldesc;
1094 XSETFRAME (frame, f);
1095 coldesc = call2 (Qtty_color_by_index, make_number (idx), frame);
1097 if (!NILP (coldesc))
1098 return XCAR (coldesc);
1100 #ifdef MSDOS
1101 /* We can have an MSDOG frame under -nw for a short window of
1102 opportunity before internal_terminal_init is called. DTRT. */
1103 if (FRAME_MSDOS_P (f) && !inhibit_window_system)
1104 return msdos_stdcolor_name (idx);
1105 #endif
1107 if (idx == FACE_TTY_DEFAULT_FG_COLOR)
1108 return build_string (unspecified_fg);
1109 if (idx == FACE_TTY_DEFAULT_BG_COLOR)
1110 return build_string (unspecified_bg);
1112 return Qunspecified;
1116 /* Return non-zero if COLOR_NAME is a shade of gray (or white or
1117 black) on frame F.
1119 The criterion implemented here is not a terribly sophisticated one. */
1121 static int
1122 face_color_gray_p (struct frame *f, const char *color_name)
1124 XColor color;
1125 int gray_p;
1127 if (defined_color (f, color_name, &color, 0))
1128 gray_p = (/* Any color sufficiently close to black counts as gray. */
1129 (color.red < 5000 && color.green < 5000 && color.blue < 5000)
1131 ((eabs (color.red - color.green)
1132 < max (color.red, color.green) / 20)
1133 && (eabs (color.green - color.blue)
1134 < max (color.green, color.blue) / 20)
1135 && (eabs (color.blue - color.red)
1136 < max (color.blue, color.red) / 20)));
1137 else
1138 gray_p = 0;
1140 return gray_p;
1144 /* Return non-zero if color COLOR_NAME can be displayed on frame F.
1145 BACKGROUND_P non-zero means the color will be used as background
1146 color. */
1148 static int
1149 face_color_supported_p (struct frame *f, const char *color_name,
1150 int background_p)
1152 Lisp_Object frame;
1153 XColor not_used;
1155 XSETFRAME (frame, f);
1156 return
1157 #ifdef HAVE_WINDOW_SYSTEM
1158 FRAME_WINDOW_P (f)
1159 ? (!NILP (Fxw_display_color_p (frame))
1160 || xstrcasecmp (color_name, "black") == 0
1161 || xstrcasecmp (color_name, "white") == 0
1162 || (background_p
1163 && face_color_gray_p (f, color_name))
1164 || (!NILP (Fx_display_grayscale_p (frame))
1165 && face_color_gray_p (f, color_name)))
1167 #endif
1168 tty_defined_color (f, color_name, &not_used, 0);
1172 DEFUN ("color-gray-p", Fcolor_gray_p, Scolor_gray_p, 1, 2, 0,
1173 doc: /* Return non-nil if COLOR is a shade of gray (or white or black).
1174 FRAME specifies the frame and thus the display for interpreting COLOR.
1175 If FRAME is nil or omitted, use the selected frame. */)
1176 (Lisp_Object color, Lisp_Object frame)
1178 CHECK_STRING (color);
1179 return (face_color_gray_p (decode_any_frame (frame), SSDATA (color))
1180 ? Qt : Qnil);
1184 DEFUN ("color-supported-p", Fcolor_supported_p,
1185 Scolor_supported_p, 1, 3, 0,
1186 doc: /* Return non-nil if COLOR can be displayed on FRAME.
1187 BACKGROUND-P non-nil means COLOR is used as a background.
1188 Otherwise, this function tells whether it can be used as a foreground.
1189 If FRAME is nil or omitted, use the selected frame.
1190 COLOR must be a valid color name. */)
1191 (Lisp_Object color, Lisp_Object frame, Lisp_Object background_p)
1193 CHECK_STRING (color);
1194 return (face_color_supported_p (decode_any_frame (frame),
1195 SSDATA (color), !NILP (background_p))
1196 ? Qt : Qnil);
1200 /* Load color with name NAME for use by face FACE on frame F.
1201 TARGET_INDEX must be one of LFACE_FOREGROUND_INDEX,
1202 LFACE_BACKGROUND_INDEX, LFACE_UNDERLINE_INDEX, LFACE_OVERLINE_INDEX,
1203 LFACE_STRIKE_THROUGH_INDEX, or LFACE_BOX_INDEX. Value is the
1204 pixel color. If color cannot be loaded, display a message, and
1205 return the foreground, background or underline color of F, but
1206 record that fact in flags of the face so that we don't try to free
1207 these colors. */
1209 unsigned long
1210 load_color (struct frame *f, struct face *face, Lisp_Object name,
1211 enum lface_attribute_index target_index)
1213 XColor color;
1215 eassert (STRINGP (name));
1216 eassert (target_index == LFACE_FOREGROUND_INDEX
1217 || target_index == LFACE_BACKGROUND_INDEX
1218 || target_index == LFACE_UNDERLINE_INDEX
1219 || target_index == LFACE_OVERLINE_INDEX
1220 || target_index == LFACE_STRIKE_THROUGH_INDEX
1221 || target_index == LFACE_BOX_INDEX);
1223 /* if the color map is full, defined_color will return a best match
1224 to the values in an existing cell. */
1225 if (!defined_color (f, SSDATA (name), &color, 1))
1227 add_to_log ("Unable to load color \"%s\"", name, Qnil);
1229 switch (target_index)
1231 case LFACE_FOREGROUND_INDEX:
1232 face->foreground_defaulted_p = 1;
1233 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1234 break;
1236 case LFACE_BACKGROUND_INDEX:
1237 face->background_defaulted_p = 1;
1238 color.pixel = FRAME_BACKGROUND_PIXEL (f);
1239 break;
1241 case LFACE_UNDERLINE_INDEX:
1242 face->underline_defaulted_p = 1;
1243 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1244 break;
1246 case LFACE_OVERLINE_INDEX:
1247 face->overline_color_defaulted_p = 1;
1248 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1249 break;
1251 case LFACE_STRIKE_THROUGH_INDEX:
1252 face->strike_through_color_defaulted_p = 1;
1253 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1254 break;
1256 case LFACE_BOX_INDEX:
1257 face->box_color_defaulted_p = 1;
1258 color.pixel = FRAME_FOREGROUND_PIXEL (f);
1259 break;
1261 default:
1262 emacs_abort ();
1265 #ifdef GLYPH_DEBUG
1266 else
1267 ++ncolors_allocated;
1268 #endif
1270 return color.pixel;
1274 #ifdef HAVE_WINDOW_SYSTEM
1276 /* Load colors for face FACE which is used on frame F. Colors are
1277 specified by slots LFACE_BACKGROUND_INDEX and LFACE_FOREGROUND_INDEX
1278 of ATTRS. If the background color specified is not supported on F,
1279 try to emulate gray colors with a stipple from Vface_default_stipple. */
1281 static void
1282 load_face_colors (struct frame *f, struct face *face,
1283 Lisp_Object attrs[LFACE_VECTOR_SIZE])
1285 Lisp_Object fg, bg;
1287 bg = attrs[LFACE_BACKGROUND_INDEX];
1288 fg = attrs[LFACE_FOREGROUND_INDEX];
1290 /* Swap colors if face is inverse-video. */
1291 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1293 Lisp_Object tmp;
1294 tmp = fg;
1295 fg = bg;
1296 bg = tmp;
1299 /* Check for support for foreground, not for background because
1300 face_color_supported_p is smart enough to know that grays are
1301 "supported" as background because we are supposed to use stipple
1302 for them. */
1303 if (!face_color_supported_p (f, SSDATA (bg), 0)
1304 && !NILP (Fbitmap_spec_p (Vface_default_stipple)))
1306 x_destroy_bitmap (f, face->stipple);
1307 face->stipple = load_pixmap (f, Vface_default_stipple,
1308 &face->pixmap_w, &face->pixmap_h);
1311 face->background = load_color (f, face, bg, LFACE_BACKGROUND_INDEX);
1312 face->foreground = load_color (f, face, fg, LFACE_FOREGROUND_INDEX);
1316 /* Free color PIXEL on frame F. */
1318 void
1319 unload_color (struct frame *f, long unsigned int pixel)
1321 #ifdef HAVE_X_WINDOWS
1322 if (pixel != -1)
1324 block_input ();
1325 x_free_colors (f, &pixel, 1);
1326 unblock_input ();
1328 #endif
1332 /* Free colors allocated for FACE. */
1334 static void
1335 free_face_colors (struct frame *f, struct face *face)
1337 /* PENDING(NS): need to do something here? */
1338 #ifdef HAVE_X_WINDOWS
1339 if (face->colors_copied_bitwise_p)
1340 return;
1342 block_input ();
1344 if (!face->foreground_defaulted_p)
1346 x_free_colors (f, &face->foreground, 1);
1347 IF_DEBUG (--ncolors_allocated);
1350 if (!face->background_defaulted_p)
1352 x_free_colors (f, &face->background, 1);
1353 IF_DEBUG (--ncolors_allocated);
1356 if (face->underline_p
1357 && !face->underline_defaulted_p)
1359 x_free_colors (f, &face->underline_color, 1);
1360 IF_DEBUG (--ncolors_allocated);
1363 if (face->overline_p
1364 && !face->overline_color_defaulted_p)
1366 x_free_colors (f, &face->overline_color, 1);
1367 IF_DEBUG (--ncolors_allocated);
1370 if (face->strike_through_p
1371 && !face->strike_through_color_defaulted_p)
1373 x_free_colors (f, &face->strike_through_color, 1);
1374 IF_DEBUG (--ncolors_allocated);
1377 if (face->box != FACE_NO_BOX
1378 && !face->box_color_defaulted_p)
1380 x_free_colors (f, &face->box_color, 1);
1381 IF_DEBUG (--ncolors_allocated);
1384 unblock_input ();
1385 #endif /* HAVE_X_WINDOWS */
1388 #endif /* HAVE_WINDOW_SYSTEM */
1392 /***********************************************************************
1393 XLFD Font Names
1394 ***********************************************************************/
1396 /* An enumerator for each field of an XLFD font name. */
1398 enum xlfd_field
1400 XLFD_FOUNDRY,
1401 XLFD_FAMILY,
1402 XLFD_WEIGHT,
1403 XLFD_SLANT,
1404 XLFD_SWIDTH,
1405 XLFD_ADSTYLE,
1406 XLFD_PIXEL_SIZE,
1407 XLFD_POINT_SIZE,
1408 XLFD_RESX,
1409 XLFD_RESY,
1410 XLFD_SPACING,
1411 XLFD_AVGWIDTH,
1412 XLFD_REGISTRY,
1413 XLFD_ENCODING,
1414 XLFD_LAST
1417 /* An enumerator for each possible slant value of a font. Taken from
1418 the XLFD specification. */
1420 enum xlfd_slant
1422 XLFD_SLANT_UNKNOWN,
1423 XLFD_SLANT_ROMAN,
1424 XLFD_SLANT_ITALIC,
1425 XLFD_SLANT_OBLIQUE,
1426 XLFD_SLANT_REVERSE_ITALIC,
1427 XLFD_SLANT_REVERSE_OBLIQUE,
1428 XLFD_SLANT_OTHER
1431 /* Relative font weight according to XLFD documentation. */
1433 enum xlfd_weight
1435 XLFD_WEIGHT_UNKNOWN,
1436 XLFD_WEIGHT_ULTRA_LIGHT, /* 10 */
1437 XLFD_WEIGHT_EXTRA_LIGHT, /* 20 */
1438 XLFD_WEIGHT_LIGHT, /* 30 */
1439 XLFD_WEIGHT_SEMI_LIGHT, /* 40: SemiLight, Book, ... */
1440 XLFD_WEIGHT_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1441 XLFD_WEIGHT_SEMI_BOLD, /* 60: SemiBold, DemiBold, ... */
1442 XLFD_WEIGHT_BOLD, /* 70: Bold, ... */
1443 XLFD_WEIGHT_EXTRA_BOLD, /* 80: ExtraBold, Heavy, ... */
1444 XLFD_WEIGHT_ULTRA_BOLD /* 90: UltraBold, Black, ... */
1447 /* Relative proportionate width. */
1449 enum xlfd_swidth
1451 XLFD_SWIDTH_UNKNOWN,
1452 XLFD_SWIDTH_ULTRA_CONDENSED, /* 10 */
1453 XLFD_SWIDTH_EXTRA_CONDENSED, /* 20 */
1454 XLFD_SWIDTH_CONDENSED, /* 30: Condensed, Narrow, Compressed, ... */
1455 XLFD_SWIDTH_SEMI_CONDENSED, /* 40: semicondensed */
1456 XLFD_SWIDTH_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1457 XLFD_SWIDTH_SEMI_EXPANDED, /* 60: SemiExpanded, DemiExpanded, ... */
1458 XLFD_SWIDTH_EXPANDED, /* 70: Expanded... */
1459 XLFD_SWIDTH_EXTRA_EXPANDED, /* 80: ExtraExpanded, Wide... */
1460 XLFD_SWIDTH_ULTRA_EXPANDED /* 90: UltraExpanded... */
1463 /* Order by which font selection chooses fonts. The default values
1464 mean `first, find a best match for the font width, then for the
1465 font height, then for weight, then for slant.' This variable can be
1466 set via set-face-font-sort-order. */
1468 static int font_sort_order[4];
1470 #ifdef HAVE_WINDOW_SYSTEM
1472 static enum font_property_index font_props_for_sorting[FONT_SIZE_INDEX];
1474 static int
1475 compare_fonts_by_sort_order (const void *v1, const void *v2)
1477 Lisp_Object const *p1 = v1;
1478 Lisp_Object const *p2 = v2;
1479 Lisp_Object font1 = *p1;
1480 Lisp_Object font2 = *p2;
1481 int i;
1483 for (i = 0; i < FONT_SIZE_INDEX; i++)
1485 enum font_property_index idx = font_props_for_sorting[i];
1486 Lisp_Object val1 = AREF (font1, idx), val2 = AREF (font2, idx);
1487 int result;
1489 if (idx <= FONT_REGISTRY_INDEX)
1491 if (STRINGP (val1))
1492 result = STRINGP (val2) ? strcmp (SSDATA (val1), SSDATA (val2)) : -1;
1493 else
1494 result = STRINGP (val2) ? 1 : 0;
1496 else
1498 if (INTEGERP (val1))
1499 result = (INTEGERP (val2) && XINT (val1) >= XINT (val2)
1500 ? XINT (val1) > XINT (val2)
1501 : -1);
1502 else
1503 result = INTEGERP (val2) ? 1 : 0;
1505 if (result)
1506 return result;
1508 return 0;
1511 DEFUN ("x-family-fonts", Fx_family_fonts, Sx_family_fonts, 0, 2, 0,
1512 doc: /* Return a list of available fonts of family FAMILY on FRAME.
1513 If FAMILY is omitted or nil, list all families.
1514 Otherwise, FAMILY must be a string, possibly containing wildcards
1515 `?' and `*'.
1516 If FRAME is omitted or nil, use the selected frame.
1517 Each element of the result is a vector [FAMILY WIDTH POINT-SIZE WEIGHT
1518 SLANT FIXED-P FULL REGISTRY-AND-ENCODING].
1519 FAMILY is the font family name. POINT-SIZE is the size of the
1520 font in 1/10 pt. WIDTH, WEIGHT, and SLANT are symbols describing the
1521 width, weight and slant of the font. These symbols are the same as for
1522 face attributes. FIXED-P is non-nil if the font is fixed-pitch.
1523 FULL is the full name of the font, and REGISTRY-AND-ENCODING is a string
1524 giving the registry and encoding of the font.
1525 The result list is sorted according to the current setting of
1526 the face font sort order. */)
1527 (Lisp_Object family, Lisp_Object frame)
1529 Lisp_Object font_spec, list, *drivers, vec;
1530 struct frame *f = decode_live_frame (frame);
1531 ptrdiff_t i, nfonts;
1532 EMACS_INT ndrivers;
1533 Lisp_Object result;
1534 USE_SAFE_ALLOCA;
1536 font_spec = Ffont_spec (0, NULL);
1537 if (!NILP (family))
1539 CHECK_STRING (family);
1540 font_parse_family_registry (family, Qnil, font_spec);
1543 list = font_list_entities (f, font_spec);
1544 if (NILP (list))
1545 return Qnil;
1547 /* Sort the font entities. */
1548 for (i = 0; i < 4; i++)
1549 switch (font_sort_order[i])
1551 case XLFD_SWIDTH:
1552 font_props_for_sorting[i] = FONT_WIDTH_INDEX; break;
1553 case XLFD_POINT_SIZE:
1554 font_props_for_sorting[i] = FONT_SIZE_INDEX; break;
1555 case XLFD_WEIGHT:
1556 font_props_for_sorting[i] = FONT_WEIGHT_INDEX; break;
1557 default:
1558 font_props_for_sorting[i] = FONT_SLANT_INDEX; break;
1560 font_props_for_sorting[i++] = FONT_FAMILY_INDEX;
1561 font_props_for_sorting[i++] = FONT_FOUNDRY_INDEX;
1562 font_props_for_sorting[i++] = FONT_ADSTYLE_INDEX;
1563 font_props_for_sorting[i++] = FONT_REGISTRY_INDEX;
1565 ndrivers = XINT (Flength (list));
1566 SAFE_ALLOCA_LISP (drivers, ndrivers);
1567 for (i = 0; i < ndrivers; i++, list = XCDR (list))
1568 drivers[i] = XCAR (list);
1569 vec = Fvconcat (ndrivers, drivers);
1570 nfonts = ASIZE (vec);
1572 qsort (XVECTOR (vec)->contents, nfonts, word_size,
1573 compare_fonts_by_sort_order);
1575 result = Qnil;
1576 for (i = nfonts - 1; i >= 0; --i)
1578 Lisp_Object font = AREF (vec, i);
1579 Lisp_Object v = make_uninit_vector (8);
1580 int point;
1581 Lisp_Object spacing;
1583 ASET (v, 0, AREF (font, FONT_FAMILY_INDEX));
1584 ASET (v, 1, FONT_WIDTH_SYMBOLIC (font));
1585 point = PIXEL_TO_POINT (XINT (AREF (font, FONT_SIZE_INDEX)) * 10,
1586 FRAME_RES_Y (f));
1587 ASET (v, 2, make_number (point));
1588 ASET (v, 3, FONT_WEIGHT_SYMBOLIC (font));
1589 ASET (v, 4, FONT_SLANT_SYMBOLIC (font));
1590 spacing = Ffont_get (font, QCspacing);
1591 ASET (v, 5, (NILP (spacing) || EQ (spacing, Qp)) ? Qnil : Qt);
1592 ASET (v, 6, Ffont_xlfd_name (font, Qnil));
1593 ASET (v, 7, AREF (font, FONT_REGISTRY_INDEX));
1595 result = Fcons (v, result);
1598 SAFE_FREE ();
1599 return result;
1602 DEFUN ("x-list-fonts", Fx_list_fonts, Sx_list_fonts, 1, 5, 0,
1603 doc: /* Return a list of the names of available fonts matching PATTERN.
1604 If optional arguments FACE and FRAME are specified, return only fonts
1605 the same size as FACE on FRAME.
1607 PATTERN should be a string containing a font name in the XLFD,
1608 Fontconfig, or GTK format. A font name given in the XLFD format may
1609 contain wildcard characters:
1610 the * character matches any substring, and
1611 the ? character matches any single character.
1612 PATTERN is case-insensitive.
1614 The return value is a list of strings, suitable as arguments to
1615 `set-face-font'.
1617 Fonts Emacs can't use may or may not be excluded
1618 even if they match PATTERN and FACE.
1619 The optional fourth argument MAXIMUM sets a limit on how many
1620 fonts to match. The first MAXIMUM fonts are reported.
1621 The optional fifth argument WIDTH, if specified, is a number of columns
1622 occupied by a character of a font. In that case, return only fonts
1623 the WIDTH times as wide as FACE on FRAME. */)
1624 (Lisp_Object pattern, Lisp_Object face, Lisp_Object frame,
1625 Lisp_Object maximum, Lisp_Object width)
1627 struct frame *f;
1628 int size, avgwidth IF_LINT (= 0);
1630 check_window_system (NULL);
1631 CHECK_STRING (pattern);
1633 if (! NILP (maximum))
1634 CHECK_NATNUM (maximum);
1636 if (!NILP (width))
1637 CHECK_NUMBER (width);
1639 /* We can't simply call decode_window_system_frame because
1640 this function may be called before any frame is created. */
1641 f = decode_live_frame (frame);
1642 if (! FRAME_WINDOW_P (f))
1644 /* Perhaps we have not yet created any frame. */
1645 f = NULL;
1646 frame = Qnil;
1647 face = Qnil;
1649 else
1650 XSETFRAME (frame, f);
1652 /* Determine the width standard for comparison with the fonts we find. */
1654 if (NILP (face))
1655 size = 0;
1656 else
1658 /* This is of limited utility since it works with character
1659 widths. Keep it for compatibility. --gerd. */
1660 int face_id = lookup_named_face (f, face, 0);
1661 struct face *width_face = (face_id < 0
1662 ? NULL
1663 : FACE_FROM_ID (f, face_id));
1665 if (width_face && width_face->font)
1667 size = width_face->font->pixel_size;
1668 avgwidth = width_face->font->average_width;
1670 else
1672 size = FRAME_FONT (f)->pixel_size;
1673 avgwidth = FRAME_FONT (f)->average_width;
1675 if (!NILP (width))
1676 avgwidth *= XINT (width);
1680 Lisp_Object font_spec;
1681 Lisp_Object args[2], tail;
1683 font_spec = font_spec_from_name (pattern);
1684 if (!FONTP (font_spec))
1685 signal_error ("Invalid font name", pattern);
1687 if (size)
1689 Ffont_put (font_spec, QCsize, make_number (size));
1690 Ffont_put (font_spec, QCavgwidth, make_number (avgwidth));
1692 args[0] = Flist_fonts (font_spec, frame, maximum, font_spec);
1693 for (tail = args[0]; CONSP (tail); tail = XCDR (tail))
1695 Lisp_Object font_entity;
1697 font_entity = XCAR (tail);
1698 if ((NILP (AREF (font_entity, FONT_SIZE_INDEX))
1699 || XINT (AREF (font_entity, FONT_SIZE_INDEX)) == 0)
1700 && ! NILP (AREF (font_spec, FONT_SIZE_INDEX)))
1702 /* This is a scalable font. For backward compatibility,
1703 we set the specified size. */
1704 font_entity = copy_font_spec (font_entity);
1705 ASET (font_entity, FONT_SIZE_INDEX,
1706 AREF (font_spec, FONT_SIZE_INDEX));
1708 XSETCAR (tail, Ffont_xlfd_name (font_entity, Qnil));
1710 if (NILP (frame))
1711 /* We don't have to check fontsets. */
1712 return args[0];
1713 args[1] = list_fontsets (f, pattern, size);
1714 return Fnconc (2, args);
1718 #endif /* HAVE_WINDOW_SYSTEM */
1721 /***********************************************************************
1722 Lisp Faces
1723 ***********************************************************************/
1725 /* Access face attributes of face LFACE, a Lisp vector. */
1727 #define LFACE_FAMILY(LFACE) AREF ((LFACE), LFACE_FAMILY_INDEX)
1728 #define LFACE_FOUNDRY(LFACE) AREF ((LFACE), LFACE_FOUNDRY_INDEX)
1729 #define LFACE_HEIGHT(LFACE) AREF ((LFACE), LFACE_HEIGHT_INDEX)
1730 #define LFACE_WEIGHT(LFACE) AREF ((LFACE), LFACE_WEIGHT_INDEX)
1731 #define LFACE_SLANT(LFACE) AREF ((LFACE), LFACE_SLANT_INDEX)
1732 #define LFACE_UNDERLINE(LFACE) AREF ((LFACE), LFACE_UNDERLINE_INDEX)
1733 #define LFACE_INVERSE(LFACE) AREF ((LFACE), LFACE_INVERSE_INDEX)
1734 #define LFACE_FOREGROUND(LFACE) AREF ((LFACE), LFACE_FOREGROUND_INDEX)
1735 #define LFACE_BACKGROUND(LFACE) AREF ((LFACE), LFACE_BACKGROUND_INDEX)
1736 #define LFACE_STIPPLE(LFACE) AREF ((LFACE), LFACE_STIPPLE_INDEX)
1737 #define LFACE_SWIDTH(LFACE) AREF ((LFACE), LFACE_SWIDTH_INDEX)
1738 #define LFACE_OVERLINE(LFACE) AREF ((LFACE), LFACE_OVERLINE_INDEX)
1739 #define LFACE_STRIKE_THROUGH(LFACE) AREF ((LFACE), LFACE_STRIKE_THROUGH_INDEX)
1740 #define LFACE_BOX(LFACE) AREF ((LFACE), LFACE_BOX_INDEX)
1741 #define LFACE_FONT(LFACE) AREF ((LFACE), LFACE_FONT_INDEX)
1742 #define LFACE_INHERIT(LFACE) AREF ((LFACE), LFACE_INHERIT_INDEX)
1743 #define LFACE_FONTSET(LFACE) AREF ((LFACE), LFACE_FONTSET_INDEX)
1745 /* Non-zero if LFACE is a Lisp face. A Lisp face is a vector of size
1746 LFACE_VECTOR_SIZE which has the symbol `face' in slot 0. */
1748 #define LFACEP(LFACE) \
1749 (VECTORP (LFACE) \
1750 && ASIZE (LFACE) == LFACE_VECTOR_SIZE \
1751 && EQ (AREF (LFACE, 0), Qface))
1754 #ifdef GLYPH_DEBUG
1756 /* Check consistency of Lisp face attribute vector ATTRS. */
1758 static void
1759 check_lface_attrs (Lisp_Object attrs[LFACE_VECTOR_SIZE])
1761 eassert (UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
1762 || IGNORE_DEFFACE_P (attrs[LFACE_FAMILY_INDEX])
1763 || STRINGP (attrs[LFACE_FAMILY_INDEX]));
1764 eassert (UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
1765 || IGNORE_DEFFACE_P (attrs[LFACE_FOUNDRY_INDEX])
1766 || STRINGP (attrs[LFACE_FOUNDRY_INDEX]));
1767 eassert (UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
1768 || IGNORE_DEFFACE_P (attrs[LFACE_SWIDTH_INDEX])
1769 || SYMBOLP (attrs[LFACE_SWIDTH_INDEX]));
1770 eassert (UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
1771 || IGNORE_DEFFACE_P (attrs[LFACE_HEIGHT_INDEX])
1772 || INTEGERP (attrs[LFACE_HEIGHT_INDEX])
1773 || FLOATP (attrs[LFACE_HEIGHT_INDEX])
1774 || FUNCTIONP (attrs[LFACE_HEIGHT_INDEX]));
1775 eassert (UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
1776 || IGNORE_DEFFACE_P (attrs[LFACE_WEIGHT_INDEX])
1777 || SYMBOLP (attrs[LFACE_WEIGHT_INDEX]));
1778 eassert (UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
1779 || IGNORE_DEFFACE_P (attrs[LFACE_SLANT_INDEX])
1780 || SYMBOLP (attrs[LFACE_SLANT_INDEX]));
1781 eassert (UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
1782 || IGNORE_DEFFACE_P (attrs[LFACE_UNDERLINE_INDEX])
1783 || SYMBOLP (attrs[LFACE_UNDERLINE_INDEX])
1784 || STRINGP (attrs[LFACE_UNDERLINE_INDEX])
1785 || CONSP (attrs[LFACE_UNDERLINE_INDEX]));
1786 eassert (UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
1787 || IGNORE_DEFFACE_P (attrs[LFACE_OVERLINE_INDEX])
1788 || SYMBOLP (attrs[LFACE_OVERLINE_INDEX])
1789 || STRINGP (attrs[LFACE_OVERLINE_INDEX]));
1790 eassert (UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1791 || IGNORE_DEFFACE_P (attrs[LFACE_STRIKE_THROUGH_INDEX])
1792 || SYMBOLP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1793 || STRINGP (attrs[LFACE_STRIKE_THROUGH_INDEX]));
1794 eassert (UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
1795 || IGNORE_DEFFACE_P (attrs[LFACE_BOX_INDEX])
1796 || SYMBOLP (attrs[LFACE_BOX_INDEX])
1797 || STRINGP (attrs[LFACE_BOX_INDEX])
1798 || INTEGERP (attrs[LFACE_BOX_INDEX])
1799 || CONSP (attrs[LFACE_BOX_INDEX]));
1800 eassert (UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
1801 || IGNORE_DEFFACE_P (attrs[LFACE_INVERSE_INDEX])
1802 || SYMBOLP (attrs[LFACE_INVERSE_INDEX]));
1803 eassert (UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
1804 || IGNORE_DEFFACE_P (attrs[LFACE_FOREGROUND_INDEX])
1805 || STRINGP (attrs[LFACE_FOREGROUND_INDEX]));
1806 eassert (UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
1807 || IGNORE_DEFFACE_P (attrs[LFACE_BACKGROUND_INDEX])
1808 || STRINGP (attrs[LFACE_BACKGROUND_INDEX]));
1809 eassert (UNSPECIFIEDP (attrs[LFACE_INHERIT_INDEX])
1810 || IGNORE_DEFFACE_P (attrs[LFACE_INHERIT_INDEX])
1811 || NILP (attrs[LFACE_INHERIT_INDEX])
1812 || SYMBOLP (attrs[LFACE_INHERIT_INDEX])
1813 || CONSP (attrs[LFACE_INHERIT_INDEX]));
1814 #ifdef HAVE_WINDOW_SYSTEM
1815 eassert (UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
1816 || IGNORE_DEFFACE_P (attrs[LFACE_STIPPLE_INDEX])
1817 || SYMBOLP (attrs[LFACE_STIPPLE_INDEX])
1818 || !NILP (Fbitmap_spec_p (attrs[LFACE_STIPPLE_INDEX])));
1819 eassert (UNSPECIFIEDP (attrs[LFACE_FONT_INDEX])
1820 || IGNORE_DEFFACE_P (attrs[LFACE_FONT_INDEX])
1821 || FONTP (attrs[LFACE_FONT_INDEX]));
1822 eassert (UNSPECIFIEDP (attrs[LFACE_FONTSET_INDEX])
1823 || STRINGP (attrs[LFACE_FONTSET_INDEX])
1824 || NILP (attrs[LFACE_FONTSET_INDEX]));
1825 #endif
1829 /* Check consistency of attributes of Lisp face LFACE (a Lisp vector). */
1831 static void
1832 check_lface (Lisp_Object lface)
1834 if (!NILP (lface))
1836 eassert (LFACEP (lface));
1837 check_lface_attrs (XVECTOR (lface)->contents);
1841 #else /* not GLYPH_DEBUG */
1843 #define check_lface_attrs(attrs) (void) 0
1844 #define check_lface(lface) (void) 0
1846 #endif /* GLYPH_DEBUG */
1850 /* Face-merge cycle checking. */
1852 enum named_merge_point_kind
1854 NAMED_MERGE_POINT_NORMAL,
1855 NAMED_MERGE_POINT_REMAP
1858 /* A `named merge point' is simply a point during face-merging where we
1859 look up a face by name. We keep a stack of which named lookups we're
1860 currently processing so that we can easily detect cycles, using a
1861 linked- list of struct named_merge_point structures, typically
1862 allocated on the stack frame of the named lookup functions which are
1863 active (so no consing is required). */
1864 struct named_merge_point
1866 Lisp_Object face_name;
1867 enum named_merge_point_kind named_merge_point_kind;
1868 struct named_merge_point *prev;
1872 /* If a face merging cycle is detected for FACE_NAME, return 0,
1873 otherwise add NEW_NAMED_MERGE_POINT, which is initialized using
1874 FACE_NAME and NAMED_MERGE_POINT_KIND, as the head of the linked list
1875 pointed to by NAMED_MERGE_POINTS, and return 1. */
1877 static int
1878 push_named_merge_point (struct named_merge_point *new_named_merge_point,
1879 Lisp_Object face_name,
1880 enum named_merge_point_kind named_merge_point_kind,
1881 struct named_merge_point **named_merge_points)
1883 struct named_merge_point *prev;
1885 for (prev = *named_merge_points; prev; prev = prev->prev)
1886 if (EQ (face_name, prev->face_name))
1888 if (prev->named_merge_point_kind == named_merge_point_kind)
1889 /* A cycle, so fail. */
1890 return 0;
1891 else if (prev->named_merge_point_kind == NAMED_MERGE_POINT_REMAP)
1892 /* A remap `hides ' any previous normal merge points
1893 (because the remap means that it's actually different face),
1894 so as we know the current merge point must be normal, we
1895 can just assume it's OK. */
1896 break;
1899 new_named_merge_point->face_name = face_name;
1900 new_named_merge_point->named_merge_point_kind = named_merge_point_kind;
1901 new_named_merge_point->prev = *named_merge_points;
1903 *named_merge_points = new_named_merge_point;
1905 return 1;
1909 /* Resolve face name FACE_NAME. If FACE_NAME is a string, intern it
1910 to make it a symbol. If FACE_NAME is an alias for another face,
1911 return that face's name.
1913 Return default face in case of errors. */
1915 static Lisp_Object
1916 resolve_face_name (Lisp_Object face_name, int signal_p)
1918 Lisp_Object orig_face;
1919 Lisp_Object tortoise, hare;
1921 if (STRINGP (face_name))
1922 face_name = intern (SSDATA (face_name));
1924 if (NILP (face_name) || !SYMBOLP (face_name))
1925 return face_name;
1927 orig_face = face_name;
1928 tortoise = hare = face_name;
1930 while (1)
1932 face_name = hare;
1933 hare = Fget (hare, Qface_alias);
1934 if (NILP (hare) || !SYMBOLP (hare))
1935 break;
1937 face_name = hare;
1938 hare = Fget (hare, Qface_alias);
1939 if (NILP (hare) || !SYMBOLP (hare))
1940 break;
1942 tortoise = Fget (tortoise, Qface_alias);
1943 if (EQ (hare, tortoise))
1945 if (signal_p)
1946 xsignal1 (Qcircular_list, orig_face);
1947 return Qdefault;
1951 return face_name;
1955 /* Return the face definition of FACE_NAME on frame F. F null means
1956 return the definition for new frames. FACE_NAME may be a string or
1957 a symbol (apparently Emacs 20.2 allowed strings as face names in
1958 face text properties; Ediff uses that). If SIGNAL_P is non-zero,
1959 signal an error if FACE_NAME is not a valid face name. If SIGNAL_P
1960 is zero, value is nil if FACE_NAME is not a valid face name. */
1961 static Lisp_Object
1962 lface_from_face_name_no_resolve (struct frame *f, Lisp_Object face_name,
1963 int signal_p)
1965 Lisp_Object lface;
1967 if (f)
1968 lface = assq_no_quit (face_name, f->face_alist);
1969 else
1970 lface = assq_no_quit (face_name, Vface_new_frame_defaults);
1972 if (CONSP (lface))
1973 lface = XCDR (lface);
1974 else if (signal_p)
1975 signal_error ("Invalid face", face_name);
1977 check_lface (lface);
1979 return lface;
1982 /* Return the face definition of FACE_NAME on frame F. F null means
1983 return the definition for new frames. FACE_NAME may be a string or
1984 a symbol (apparently Emacs 20.2 allowed strings as face names in
1985 face text properties; Ediff uses that). If FACE_NAME is an alias
1986 for another face, return that face's definition. If SIGNAL_P is
1987 non-zero, signal an error if FACE_NAME is not a valid face name.
1988 If SIGNAL_P is zero, value is nil if FACE_NAME is not a valid face
1989 name. */
1990 static Lisp_Object
1991 lface_from_face_name (struct frame *f, Lisp_Object face_name, int signal_p)
1993 face_name = resolve_face_name (face_name, signal_p);
1994 return lface_from_face_name_no_resolve (f, face_name, signal_p);
1998 /* Get face attributes of face FACE_NAME from frame-local faces on
1999 frame F. Store the resulting attributes in ATTRS which must point
2000 to a vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If SIGNAL_P
2001 is non-zero, signal an error if FACE_NAME does not name a face.
2002 Otherwise, value is zero if FACE_NAME is not a face. */
2004 static int
2005 get_lface_attributes_no_remap (struct frame *f, Lisp_Object face_name,
2006 Lisp_Object attrs[LFACE_VECTOR_SIZE],
2007 int signal_p)
2009 Lisp_Object lface;
2011 lface = lface_from_face_name_no_resolve (f, face_name, signal_p);
2013 if (! NILP (lface))
2014 memcpy (attrs, XVECTOR (lface)->contents,
2015 LFACE_VECTOR_SIZE * sizeof *attrs);
2017 return !NILP (lface);
2020 /* Get face attributes of face FACE_NAME from frame-local faces on frame
2021 F. Store the resulting attributes in ATTRS which must point to a
2022 vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If FACE_NAME is an
2023 alias for another face, use that face's definition. If SIGNAL_P is
2024 non-zero, signal an error if FACE_NAME does not name a face.
2025 Otherwise, value is zero if FACE_NAME is not a face. */
2027 static int
2028 get_lface_attributes (struct frame *f, Lisp_Object face_name,
2029 Lisp_Object attrs[LFACE_VECTOR_SIZE], int signal_p,
2030 struct named_merge_point *named_merge_points)
2032 Lisp_Object face_remapping;
2034 face_name = resolve_face_name (face_name, signal_p);
2036 /* See if SYMBOL has been remapped to some other face (usually this
2037 is done buffer-locally). */
2038 face_remapping = assq_no_quit (face_name, Vface_remapping_alist);
2039 if (CONSP (face_remapping))
2041 struct named_merge_point named_merge_point;
2043 if (push_named_merge_point (&named_merge_point,
2044 face_name, NAMED_MERGE_POINT_REMAP,
2045 &named_merge_points))
2047 int i;
2049 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2050 attrs[i] = Qunspecified;
2052 return merge_face_ref (f, XCDR (face_remapping), attrs,
2053 signal_p, named_merge_points);
2057 /* Default case, no remapping. */
2058 return get_lface_attributes_no_remap (f, face_name, attrs, signal_p);
2062 /* Non-zero if all attributes in face attribute vector ATTRS are
2063 specified, i.e. are non-nil. */
2065 static int
2066 lface_fully_specified_p (Lisp_Object attrs[LFACE_VECTOR_SIZE])
2068 int i;
2070 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2071 if (i != LFACE_FONT_INDEX && i != LFACE_INHERIT_INDEX)
2072 if ((UNSPECIFIEDP (attrs[i]) || IGNORE_DEFFACE_P (attrs[i])))
2073 break;
2075 return i == LFACE_VECTOR_SIZE;
2078 #ifdef HAVE_WINDOW_SYSTEM
2080 /* Set font-related attributes of Lisp face LFACE from FONT-OBJECT.
2081 If FORCE_P is zero, set only unspecified attributes of LFACE. The
2082 exception is `font' attribute. It is set to FONT_OBJECT regardless
2083 of FORCE_P. */
2085 static int
2086 set_lface_from_font (struct frame *f, Lisp_Object lface,
2087 Lisp_Object font_object, int force_p)
2089 Lisp_Object val;
2090 struct font *font = XFONT_OBJECT (font_object);
2092 /* Set attributes only if unspecified, otherwise face defaults for
2093 new frames would never take effect. If the font doesn't have a
2094 specific property, set a normal value for that. */
2096 if (force_p || UNSPECIFIEDP (LFACE_FAMILY (lface)))
2098 Lisp_Object family = AREF (font_object, FONT_FAMILY_INDEX);
2100 ASET (lface, LFACE_FAMILY_INDEX, SYMBOL_NAME (family));
2103 if (force_p || UNSPECIFIEDP (LFACE_FOUNDRY (lface)))
2105 Lisp_Object foundry = AREF (font_object, FONT_FOUNDRY_INDEX);
2107 ASET (lface, LFACE_FOUNDRY_INDEX, SYMBOL_NAME (foundry));
2110 if (force_p || UNSPECIFIEDP (LFACE_HEIGHT (lface)))
2112 int pt = PIXEL_TO_POINT (font->pixel_size * 10, FRAME_RES_Y (f));
2114 eassert (pt > 0);
2115 ASET (lface, LFACE_HEIGHT_INDEX, make_number (pt));
2118 if (force_p || UNSPECIFIEDP (LFACE_WEIGHT (lface)))
2120 val = FONT_WEIGHT_FOR_FACE (font_object);
2121 ASET (lface, LFACE_WEIGHT_INDEX, ! NILP (val) ? val :Qnormal);
2123 if (force_p || UNSPECIFIEDP (LFACE_SLANT (lface)))
2125 val = FONT_SLANT_FOR_FACE (font_object);
2126 ASET (lface, LFACE_SLANT_INDEX, ! NILP (val) ? val : Qnormal);
2128 if (force_p || UNSPECIFIEDP (LFACE_SWIDTH (lface)))
2130 val = FONT_WIDTH_FOR_FACE (font_object);
2131 ASET (lface, LFACE_SWIDTH_INDEX, ! NILP (val) ? val : Qnormal);
2134 ASET (lface, LFACE_FONT_INDEX, font_object);
2135 return 1;
2138 #endif /* HAVE_WINDOW_SYSTEM */
2141 /* Merges the face height FROM with the face height TO, and returns the
2142 merged height. If FROM is an invalid height, then INVALID is
2143 returned instead. FROM and TO may be either absolute face heights or
2144 `relative' heights; the returned value is always an absolute height
2145 unless both FROM and TO are relative. */
2147 static Lisp_Object
2148 merge_face_heights (Lisp_Object from, Lisp_Object to, Lisp_Object invalid)
2150 Lisp_Object result = invalid;
2152 if (INTEGERP (from))
2153 /* FROM is absolute, just use it as is. */
2154 result = from;
2155 else if (FLOATP (from))
2156 /* FROM is a scale, use it to adjust TO. */
2158 if (INTEGERP (to))
2159 /* relative X absolute => absolute */
2160 result = make_number (XFLOAT_DATA (from) * XINT (to));
2161 else if (FLOATP (to))
2162 /* relative X relative => relative */
2163 result = make_float (XFLOAT_DATA (from) * XFLOAT_DATA (to));
2164 else if (UNSPECIFIEDP (to))
2165 result = from;
2167 else if (FUNCTIONP (from))
2168 /* FROM is a function, which use to adjust TO. */
2170 /* Call function with current height as argument.
2171 From is the new height. */
2172 result = safe_call1 (from, to);
2174 /* Ensure that if TO was absolute, so is the result. */
2175 if (INTEGERP (to) && !INTEGERP (result))
2176 result = invalid;
2179 return result;
2183 /* Merge two Lisp face attribute vectors on frame F, FROM and TO, and
2184 store the resulting attributes in TO, which must be already be
2185 completely specified and contain only absolute attributes. Every
2186 specified attribute of FROM overrides the corresponding attribute of
2187 TO; relative attributes in FROM are merged with the absolute value in
2188 TO and replace it. NAMED_MERGE_POINTS is used internally to detect
2189 loops in face inheritance/remapping; it should be 0 when called from
2190 other places. */
2192 static void
2193 merge_face_vectors (struct frame *f, Lisp_Object *from, Lisp_Object *to,
2194 struct named_merge_point *named_merge_points)
2196 int i;
2197 Lisp_Object font = Qnil;
2199 /* If FROM inherits from some other faces, merge their attributes into
2200 TO before merging FROM's direct attributes. Note that an :inherit
2201 attribute of `unspecified' is the same as one of nil; we never
2202 merge :inherit attributes, so nil is more correct, but lots of
2203 other code uses `unspecified' as a generic value for face attributes. */
2204 if (!UNSPECIFIEDP (from[LFACE_INHERIT_INDEX])
2205 && !NILP (from[LFACE_INHERIT_INDEX]))
2206 merge_face_ref (f, from[LFACE_INHERIT_INDEX], to, 0, named_merge_points);
2208 if (FONT_SPEC_P (from[LFACE_FONT_INDEX]))
2210 if (!UNSPECIFIEDP (to[LFACE_FONT_INDEX]))
2211 font = merge_font_spec (from[LFACE_FONT_INDEX], to[LFACE_FONT_INDEX]);
2212 else
2213 font = copy_font_spec (from[LFACE_FONT_INDEX]);
2214 to[LFACE_FONT_INDEX] = font;
2217 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2218 if (!UNSPECIFIEDP (from[i]))
2220 if (i == LFACE_HEIGHT_INDEX && !INTEGERP (from[i]))
2222 to[i] = merge_face_heights (from[i], to[i], to[i]);
2223 font_clear_prop (to, FONT_SIZE_INDEX);
2225 else if (i != LFACE_FONT_INDEX && ! EQ (to[i], from[i]))
2227 to[i] = from[i];
2228 if (i >= LFACE_FAMILY_INDEX && i <=LFACE_SLANT_INDEX)
2229 font_clear_prop (to,
2230 (i == LFACE_FAMILY_INDEX ? FONT_FAMILY_INDEX
2231 : i == LFACE_FOUNDRY_INDEX ? FONT_FOUNDRY_INDEX
2232 : i == LFACE_SWIDTH_INDEX ? FONT_WIDTH_INDEX
2233 : i == LFACE_HEIGHT_INDEX ? FONT_SIZE_INDEX
2234 : i == LFACE_WEIGHT_INDEX ? FONT_WEIGHT_INDEX
2235 : FONT_SLANT_INDEX));
2239 /* If FROM specifies a font spec, make its contents take precedence
2240 over :family and other attributes. This is needed for face
2241 remapping using :font to work. */
2243 if (!NILP (font))
2245 if (! NILP (AREF (font, FONT_FOUNDRY_INDEX)))
2246 to[LFACE_FOUNDRY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FOUNDRY_INDEX));
2247 if (! NILP (AREF (font, FONT_FAMILY_INDEX)))
2248 to[LFACE_FAMILY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FAMILY_INDEX));
2249 if (! NILP (AREF (font, FONT_WEIGHT_INDEX)))
2250 to[LFACE_WEIGHT_INDEX] = FONT_WEIGHT_FOR_FACE (font);
2251 if (! NILP (AREF (font, FONT_SLANT_INDEX)))
2252 to[LFACE_SLANT_INDEX] = FONT_SLANT_FOR_FACE (font);
2253 if (! NILP (AREF (font, FONT_WIDTH_INDEX)))
2254 to[LFACE_SWIDTH_INDEX] = FONT_WIDTH_FOR_FACE (font);
2255 ASET (font, FONT_SIZE_INDEX, Qnil);
2258 /* TO is always an absolute face, which should inherit from nothing.
2259 We blindly copy the :inherit attribute above and fix it up here. */
2260 to[LFACE_INHERIT_INDEX] = Qnil;
2263 /* Merge the named face FACE_NAME on frame F, into the vector of face
2264 attributes TO. NAMED_MERGE_POINTS is used to detect loops in face
2265 inheritance. Returns true if FACE_NAME is a valid face name and
2266 merging succeeded. */
2268 static int
2269 merge_named_face (struct frame *f, Lisp_Object face_name, Lisp_Object *to,
2270 struct named_merge_point *named_merge_points)
2272 struct named_merge_point named_merge_point;
2274 if (push_named_merge_point (&named_merge_point,
2275 face_name, NAMED_MERGE_POINT_NORMAL,
2276 &named_merge_points))
2278 struct gcpro gcpro1;
2279 Lisp_Object from[LFACE_VECTOR_SIZE];
2280 int ok = get_lface_attributes (f, face_name, from, 0, named_merge_points);
2282 if (ok)
2284 GCPRO1 (named_merge_point.face_name);
2285 merge_face_vectors (f, from, to, named_merge_points);
2286 UNGCPRO;
2289 return ok;
2291 else
2292 return 0;
2296 /* Merge face attributes from the lisp `face reference' FACE_REF on
2297 frame F into the face attribute vector TO. If ERR_MSGS is non-zero,
2298 problems with FACE_REF cause an error message to be shown. Return
2299 non-zero if no errors occurred (regardless of the value of ERR_MSGS).
2300 NAMED_MERGE_POINTS is used to detect loops in face inheritance or
2301 list structure; it may be 0 for most callers.
2303 FACE_REF may be a single face specification or a list of such
2304 specifications. Each face specification can be:
2306 1. A symbol or string naming a Lisp face.
2308 2. A property list of the form (KEYWORD VALUE ...) where each
2309 KEYWORD is a face attribute name, and value is an appropriate value
2310 for that attribute.
2312 3. Conses or the form (FOREGROUND-COLOR . COLOR) or
2313 (BACKGROUND-COLOR . COLOR) where COLOR is a color name. This is
2314 for compatibility with 20.2.
2316 Face specifications earlier in lists take precedence over later
2317 specifications. */
2319 static int
2320 merge_face_ref (struct frame *f, Lisp_Object face_ref, Lisp_Object *to,
2321 int err_msgs, struct named_merge_point *named_merge_points)
2323 int ok = 1; /* Succeed without an error? */
2325 if (CONSP (face_ref))
2327 Lisp_Object first = XCAR (face_ref);
2329 if (EQ (first, Qforeground_color)
2330 || EQ (first, Qbackground_color))
2332 /* One of (FOREGROUND-COLOR . COLOR) or (BACKGROUND-COLOR
2333 . COLOR). COLOR must be a string. */
2334 Lisp_Object color_name = XCDR (face_ref);
2335 Lisp_Object color = first;
2337 if (STRINGP (color_name))
2339 if (EQ (color, Qforeground_color))
2340 to[LFACE_FOREGROUND_INDEX] = color_name;
2341 else
2342 to[LFACE_BACKGROUND_INDEX] = color_name;
2344 else
2346 if (err_msgs)
2347 add_to_log ("Invalid face color", color_name, Qnil);
2348 ok = 0;
2351 else if (SYMBOLP (first)
2352 && *SDATA (SYMBOL_NAME (first)) == ':')
2354 /* Assume this is the property list form. */
2355 while (CONSP (face_ref) && CONSP (XCDR (face_ref)))
2357 Lisp_Object keyword = XCAR (face_ref);
2358 Lisp_Object value = XCAR (XCDR (face_ref));
2359 int err = 0;
2361 /* Specifying `unspecified' is a no-op. */
2362 if (EQ (value, Qunspecified))
2364 else if (EQ (keyword, QCfamily))
2366 if (STRINGP (value))
2368 to[LFACE_FAMILY_INDEX] = value;
2369 font_clear_prop (to, FONT_FAMILY_INDEX);
2371 else
2372 err = 1;
2374 else if (EQ (keyword, QCfoundry))
2376 if (STRINGP (value))
2378 to[LFACE_FOUNDRY_INDEX] = value;
2379 font_clear_prop (to, FONT_FOUNDRY_INDEX);
2381 else
2382 err = 1;
2384 else if (EQ (keyword, QCheight))
2386 Lisp_Object new_height =
2387 merge_face_heights (value, to[LFACE_HEIGHT_INDEX], Qnil);
2389 if (! NILP (new_height))
2391 to[LFACE_HEIGHT_INDEX] = new_height;
2392 font_clear_prop (to, FONT_SIZE_INDEX);
2394 else
2395 err = 1;
2397 else if (EQ (keyword, QCweight))
2399 if (SYMBOLP (value) && FONT_WEIGHT_NAME_NUMERIC (value) >= 0)
2401 to[LFACE_WEIGHT_INDEX] = value;
2402 font_clear_prop (to, FONT_WEIGHT_INDEX);
2404 else
2405 err = 1;
2407 else if (EQ (keyword, QCslant))
2409 if (SYMBOLP (value) && FONT_SLANT_NAME_NUMERIC (value) >= 0)
2411 to[LFACE_SLANT_INDEX] = value;
2412 font_clear_prop (to, FONT_SLANT_INDEX);
2414 else
2415 err = 1;
2417 else if (EQ (keyword, QCunderline))
2419 if (EQ (value, Qt)
2420 || NILP (value)
2421 || STRINGP (value)
2422 || CONSP (value))
2423 to[LFACE_UNDERLINE_INDEX] = value;
2424 else
2425 err = 1;
2427 else if (EQ (keyword, QCoverline))
2429 if (EQ (value, Qt)
2430 || NILP (value)
2431 || STRINGP (value))
2432 to[LFACE_OVERLINE_INDEX] = value;
2433 else
2434 err = 1;
2436 else if (EQ (keyword, QCstrike_through))
2438 if (EQ (value, Qt)
2439 || NILP (value)
2440 || STRINGP (value))
2441 to[LFACE_STRIKE_THROUGH_INDEX] = value;
2442 else
2443 err = 1;
2445 else if (EQ (keyword, QCbox))
2447 if (EQ (value, Qt))
2448 value = make_number (1);
2449 if (INTEGERP (value)
2450 || STRINGP (value)
2451 || CONSP (value)
2452 || NILP (value))
2453 to[LFACE_BOX_INDEX] = value;
2454 else
2455 err = 1;
2457 else if (EQ (keyword, QCinverse_video)
2458 || EQ (keyword, QCreverse_video))
2460 if (EQ (value, Qt) || NILP (value))
2461 to[LFACE_INVERSE_INDEX] = value;
2462 else
2463 err = 1;
2465 else if (EQ (keyword, QCforeground))
2467 if (STRINGP (value))
2468 to[LFACE_FOREGROUND_INDEX] = value;
2469 else
2470 err = 1;
2472 else if (EQ (keyword, QCbackground))
2474 if (STRINGP (value))
2475 to[LFACE_BACKGROUND_INDEX] = value;
2476 else
2477 err = 1;
2479 else if (EQ (keyword, QCstipple))
2481 #if defined (HAVE_WINDOW_SYSTEM)
2482 Lisp_Object pixmap_p = Fbitmap_spec_p (value);
2483 if (!NILP (pixmap_p))
2484 to[LFACE_STIPPLE_INDEX] = value;
2485 else
2486 err = 1;
2487 #endif /* HAVE_WINDOW_SYSTEM */
2489 else if (EQ (keyword, QCwidth))
2491 if (SYMBOLP (value) && FONT_WIDTH_NAME_NUMERIC (value) >= 0)
2493 to[LFACE_SWIDTH_INDEX] = value;
2494 font_clear_prop (to, FONT_WIDTH_INDEX);
2496 else
2497 err = 1;
2499 else if (EQ (keyword, QCfont))
2501 if (FONTP (value))
2502 to[LFACE_FONT_INDEX] = value;
2503 else
2504 err = 1;
2506 else if (EQ (keyword, QCinherit))
2508 /* This is not really very useful; it's just like a
2509 normal face reference. */
2510 if (! merge_face_ref (f, value, to,
2511 err_msgs, named_merge_points))
2512 err = 1;
2514 else
2515 err = 1;
2517 if (err)
2519 add_to_log ("Invalid face attribute %S %S", keyword, value);
2520 ok = 0;
2523 face_ref = XCDR (XCDR (face_ref));
2526 else
2528 /* This is a list of face refs. Those at the beginning of the
2529 list take precedence over what follows, so we have to merge
2530 from the end backwards. */
2531 Lisp_Object next = XCDR (face_ref);
2533 if (! NILP (next))
2534 ok = merge_face_ref (f, next, to, err_msgs, named_merge_points);
2536 if (! merge_face_ref (f, first, to, err_msgs, named_merge_points))
2537 ok = 0;
2540 else
2542 /* FACE_REF ought to be a face name. */
2543 ok = merge_named_face (f, face_ref, to, named_merge_points);
2544 if (!ok && err_msgs)
2545 add_to_log ("Invalid face reference: %s", face_ref, Qnil);
2548 return ok;
2552 DEFUN ("internal-make-lisp-face", Finternal_make_lisp_face,
2553 Sinternal_make_lisp_face, 1, 2, 0,
2554 doc: /* Make FACE, a symbol, a Lisp face with all attributes nil.
2555 If FACE was not known as a face before, create a new one.
2556 If optional argument FRAME is specified, make a frame-local face
2557 for that frame. Otherwise operate on the global face definition.
2558 Value is a vector of face attributes. */)
2559 (Lisp_Object face, Lisp_Object frame)
2561 Lisp_Object global_lface, lface;
2562 struct frame *f;
2563 int i;
2565 CHECK_SYMBOL (face);
2566 global_lface = lface_from_face_name (NULL, face, 0);
2568 if (!NILP (frame))
2570 CHECK_LIVE_FRAME (frame);
2571 f = XFRAME (frame);
2572 lface = lface_from_face_name (f, face, 0);
2574 else
2575 f = NULL, lface = Qnil;
2577 /* Add a global definition if there is none. */
2578 if (NILP (global_lface))
2580 global_lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2581 Qunspecified);
2582 ASET (global_lface, 0, Qface);
2583 Vface_new_frame_defaults = Fcons (Fcons (face, global_lface),
2584 Vface_new_frame_defaults);
2586 /* Assign the new Lisp face a unique ID. The mapping from Lisp
2587 face id to Lisp face is given by the vector lface_id_to_name.
2588 The mapping from Lisp face to Lisp face id is given by the
2589 property `face' of the Lisp face name. */
2590 if (next_lface_id == lface_id_to_name_size)
2591 lface_id_to_name =
2592 xpalloc (lface_id_to_name, &lface_id_to_name_size, 1, MAX_FACE_ID,
2593 sizeof *lface_id_to_name);
2595 lface_id_to_name[next_lface_id] = face;
2596 Fput (face, Qface, make_number (next_lface_id));
2597 ++next_lface_id;
2599 else if (f == NULL)
2600 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2601 ASET (global_lface, i, Qunspecified);
2603 /* Add a frame-local definition. */
2604 if (f)
2606 if (NILP (lface))
2608 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2609 Qunspecified);
2610 ASET (lface, 0, Qface);
2611 fset_face_alist (f, Fcons (Fcons (face, lface), f->face_alist));
2613 else
2614 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2615 ASET (lface, i, Qunspecified);
2617 else
2618 lface = global_lface;
2620 /* Changing a named face means that all realized faces depending on
2621 that face are invalid. Since we cannot tell which realized faces
2622 depend on the face, make sure they are all removed. This is done
2623 by incrementing face_change_count. The next call to
2624 init_iterator will then free realized faces. */
2625 if (NILP (Fget (face, Qface_no_inherit)))
2627 ++face_change_count;
2628 ++windows_or_buffers_changed;
2631 eassert (LFACEP (lface));
2632 check_lface (lface);
2633 return lface;
2637 DEFUN ("internal-lisp-face-p", Finternal_lisp_face_p,
2638 Sinternal_lisp_face_p, 1, 2, 0,
2639 doc: /* Return non-nil if FACE names a face.
2640 FACE should be a symbol or string.
2641 If optional second argument FRAME is non-nil, check for the
2642 existence of a frame-local face with name FACE on that frame.
2643 Otherwise check for the existence of a global face. */)
2644 (Lisp_Object face, Lisp_Object frame)
2646 Lisp_Object lface;
2648 face = resolve_face_name (face, 1);
2650 if (!NILP (frame))
2652 CHECK_LIVE_FRAME (frame);
2653 lface = lface_from_face_name (XFRAME (frame), face, 0);
2655 else
2656 lface = lface_from_face_name (NULL, face, 0);
2658 return lface;
2662 DEFUN ("internal-copy-lisp-face", Finternal_copy_lisp_face,
2663 Sinternal_copy_lisp_face, 4, 4, 0,
2664 doc: /* Copy face FROM to TO.
2665 If FRAME is t, copy the global face definition of FROM.
2666 Otherwise, copy the frame-local definition of FROM on FRAME.
2667 If NEW-FRAME is a frame, copy that data into the frame-local
2668 definition of TO on NEW-FRAME. If NEW-FRAME is nil,
2669 FRAME controls where the data is copied to.
2671 The value is TO. */)
2672 (Lisp_Object from, Lisp_Object to, Lisp_Object frame, Lisp_Object new_frame)
2674 Lisp_Object lface, copy;
2676 CHECK_SYMBOL (from);
2677 CHECK_SYMBOL (to);
2679 if (EQ (frame, Qt))
2681 /* Copy global definition of FROM. We don't make copies of
2682 strings etc. because 20.2 didn't do it either. */
2683 lface = lface_from_face_name (NULL, from, 1);
2684 copy = Finternal_make_lisp_face (to, Qnil);
2686 else
2688 /* Copy frame-local definition of FROM. */
2689 if (NILP (new_frame))
2690 new_frame = frame;
2691 CHECK_LIVE_FRAME (frame);
2692 CHECK_LIVE_FRAME (new_frame);
2693 lface = lface_from_face_name (XFRAME (frame), from, 1);
2694 copy = Finternal_make_lisp_face (to, new_frame);
2697 vcopy (copy, 0, XVECTOR (lface)->contents, LFACE_VECTOR_SIZE);
2699 /* Changing a named face means that all realized faces depending on
2700 that face are invalid. Since we cannot tell which realized faces
2701 depend on the face, make sure they are all removed. This is done
2702 by incrementing face_change_count. The next call to
2703 init_iterator will then free realized faces. */
2704 if (NILP (Fget (to, Qface_no_inherit)))
2706 ++face_change_count;
2707 ++windows_or_buffers_changed;
2710 return to;
2714 DEFUN ("internal-set-lisp-face-attribute", Finternal_set_lisp_face_attribute,
2715 Sinternal_set_lisp_face_attribute, 3, 4, 0,
2716 doc: /* Set attribute ATTR of FACE to VALUE.
2717 FRAME being a frame means change the face on that frame.
2718 FRAME nil means change the face of the selected frame.
2719 FRAME t means change the default for new frames.
2720 FRAME 0 means change the face on all frames, and change the default
2721 for new frames. */)
2722 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
2724 Lisp_Object lface;
2725 Lisp_Object old_value = Qnil;
2726 /* Set one of enum font_property_index (> 0) if ATTR is one of
2727 font-related attributes other than QCfont and QCfontset. */
2728 enum font_property_index prop_index = 0;
2730 CHECK_SYMBOL (face);
2731 CHECK_SYMBOL (attr);
2733 face = resolve_face_name (face, 1);
2735 /* If FRAME is 0, change face on all frames, and change the
2736 default for new frames. */
2737 if (INTEGERP (frame) && XINT (frame) == 0)
2739 Lisp_Object tail;
2740 Finternal_set_lisp_face_attribute (face, attr, value, Qt);
2741 FOR_EACH_FRAME (tail, frame)
2742 Finternal_set_lisp_face_attribute (face, attr, value, frame);
2743 return face;
2746 /* Set lface to the Lisp attribute vector of FACE. */
2747 if (EQ (frame, Qt))
2749 lface = lface_from_face_name (NULL, face, 1);
2751 /* When updating face-new-frame-defaults, we put :ignore-defface
2752 where the caller wants `unspecified'. This forces the frame
2753 defaults to ignore the defface value. Otherwise, the defface
2754 will take effect, which is generally not what is intended.
2755 The value of that attribute will be inherited from some other
2756 face during face merging. See internal_merge_in_global_face. */
2757 if (UNSPECIFIEDP (value))
2758 value = QCignore_defface;
2760 else
2762 if (NILP (frame))
2763 frame = selected_frame;
2765 CHECK_LIVE_FRAME (frame);
2766 lface = lface_from_face_name (XFRAME (frame), face, 0);
2768 /* If a frame-local face doesn't exist yet, create one. */
2769 if (NILP (lface))
2770 lface = Finternal_make_lisp_face (face, frame);
2773 if (EQ (attr, QCfamily))
2775 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2777 CHECK_STRING (value);
2778 if (SCHARS (value) == 0)
2779 signal_error ("Invalid face family", value);
2781 old_value = LFACE_FAMILY (lface);
2782 ASET (lface, LFACE_FAMILY_INDEX, value);
2783 prop_index = FONT_FAMILY_INDEX;
2785 else if (EQ (attr, QCfoundry))
2787 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2789 CHECK_STRING (value);
2790 if (SCHARS (value) == 0)
2791 signal_error ("Invalid face foundry", value);
2793 old_value = LFACE_FOUNDRY (lface);
2794 ASET (lface, LFACE_FOUNDRY_INDEX, value);
2795 prop_index = FONT_FOUNDRY_INDEX;
2797 else if (EQ (attr, QCheight))
2799 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2801 if (EQ (face, Qdefault))
2803 /* The default face must have an absolute size. */
2804 if (!INTEGERP (value) || XINT (value) <= 0)
2805 signal_error ("Default face height not absolute and positive",
2806 value);
2808 else
2810 /* For non-default faces, do a test merge with a random
2811 height to see if VALUE's ok. */
2812 Lisp_Object test = merge_face_heights (value,
2813 make_number (10),
2814 Qnil);
2815 if (!INTEGERP (test) || XINT (test) <= 0)
2816 signal_error ("Face height does not produce a positive integer",
2817 value);
2821 old_value = LFACE_HEIGHT (lface);
2822 ASET (lface, LFACE_HEIGHT_INDEX, value);
2823 prop_index = FONT_SIZE_INDEX;
2825 else if (EQ (attr, QCweight))
2827 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2829 CHECK_SYMBOL (value);
2830 if (FONT_WEIGHT_NAME_NUMERIC (value) < 0)
2831 signal_error ("Invalid face weight", value);
2833 old_value = LFACE_WEIGHT (lface);
2834 ASET (lface, LFACE_WEIGHT_INDEX, value);
2835 prop_index = FONT_WEIGHT_INDEX;
2837 else if (EQ (attr, QCslant))
2839 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2841 CHECK_SYMBOL (value);
2842 if (FONT_SLANT_NAME_NUMERIC (value) < 0)
2843 signal_error ("Invalid face slant", value);
2845 old_value = LFACE_SLANT (lface);
2846 ASET (lface, LFACE_SLANT_INDEX, value);
2847 prop_index = FONT_SLANT_INDEX;
2849 else if (EQ (attr, QCunderline))
2851 bool valid_p = 0;
2853 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2854 valid_p = 1;
2855 else if (NILP (value) || EQ (value, Qt))
2856 valid_p = 1;
2857 else if (STRINGP (value) && SCHARS (value) > 0)
2858 valid_p = 1;
2859 else if (CONSP (value))
2861 Lisp_Object key, val, list;
2863 list = value;
2864 /* FIXME? This errs on the side of acceptance. Eg it accepts:
2865 (defface foo '((t :underline 'foo) "doc")
2866 Maybe this is intentional, maybe it isn't.
2867 Non-nil symbols other than t are not documented as being valid.
2868 Eg compare with inverse-video, which explicitly rejects them.
2870 valid_p = 1;
2872 while (!NILP (CAR_SAFE(list)))
2874 key = CAR_SAFE (list);
2875 list = CDR_SAFE (list);
2876 val = CAR_SAFE (list);
2877 list = CDR_SAFE (list);
2879 if (NILP (key) || NILP (val))
2881 valid_p = 0;
2882 break;
2885 else if (EQ (key, QCcolor)
2886 && !(EQ (val, Qforeground_color)
2887 || (STRINGP (val) && SCHARS (val) > 0)))
2889 valid_p = 0;
2890 break;
2893 else if (EQ (key, QCstyle)
2894 && !(EQ (val, Qline) || EQ (val, Qwave)))
2896 valid_p = 0;
2897 break;
2902 if (!valid_p)
2903 signal_error ("Invalid face underline", value);
2905 old_value = LFACE_UNDERLINE (lface);
2906 ASET (lface, LFACE_UNDERLINE_INDEX, value);
2908 else if (EQ (attr, QCoverline))
2910 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2911 if ((SYMBOLP (value)
2912 && !EQ (value, Qt)
2913 && !EQ (value, Qnil))
2914 /* Overline color. */
2915 || (STRINGP (value)
2916 && SCHARS (value) == 0))
2917 signal_error ("Invalid face overline", value);
2919 old_value = LFACE_OVERLINE (lface);
2920 ASET (lface, LFACE_OVERLINE_INDEX, value);
2922 else if (EQ (attr, QCstrike_through))
2924 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2925 if ((SYMBOLP (value)
2926 && !EQ (value, Qt)
2927 && !EQ (value, Qnil))
2928 /* Strike-through color. */
2929 || (STRINGP (value)
2930 && SCHARS (value) == 0))
2931 signal_error ("Invalid face strike-through", value);
2933 old_value = LFACE_STRIKE_THROUGH (lface);
2934 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, value);
2936 else if (EQ (attr, QCbox))
2938 bool valid_p;
2940 /* Allow t meaning a simple box of width 1 in foreground color
2941 of the face. */
2942 if (EQ (value, Qt))
2943 value = make_number (1);
2945 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2946 valid_p = 1;
2947 else if (NILP (value))
2948 valid_p = 1;
2949 else if (INTEGERP (value))
2950 valid_p = XINT (value) != 0;
2951 else if (STRINGP (value))
2952 valid_p = SCHARS (value) > 0;
2953 else if (CONSP (value))
2955 Lisp_Object tem;
2957 tem = value;
2958 while (CONSP (tem))
2960 Lisp_Object k, v;
2962 k = XCAR (tem);
2963 tem = XCDR (tem);
2964 if (!CONSP (tem))
2965 break;
2966 v = XCAR (tem);
2967 tem = XCDR (tem);
2969 if (EQ (k, QCline_width))
2971 if (!INTEGERP (v) || XINT (v) == 0)
2972 break;
2974 else if (EQ (k, QCcolor))
2976 if (!NILP (v) && (!STRINGP (v) || SCHARS (v) == 0))
2977 break;
2979 else if (EQ (k, QCstyle))
2981 if (!EQ (v, Qpressed_button) && !EQ (v, Qreleased_button))
2982 break;
2984 else
2985 break;
2988 valid_p = NILP (tem);
2990 else
2991 valid_p = 0;
2993 if (!valid_p)
2994 signal_error ("Invalid face box", value);
2996 old_value = LFACE_BOX (lface);
2997 ASET (lface, LFACE_BOX_INDEX, value);
2999 else if (EQ (attr, QCinverse_video)
3000 || EQ (attr, QCreverse_video))
3002 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3004 CHECK_SYMBOL (value);
3005 if (!EQ (value, Qt) && !NILP (value))
3006 signal_error ("Invalid inverse-video face attribute value", value);
3008 old_value = LFACE_INVERSE (lface);
3009 ASET (lface, LFACE_INVERSE_INDEX, value);
3011 else if (EQ (attr, QCforeground))
3013 /* Compatibility with 20.x. */
3014 if (NILP (value))
3015 value = Qunspecified;
3016 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3018 /* Don't check for valid color names here because it depends
3019 on the frame (display) whether the color will be valid
3020 when the face is realized. */
3021 CHECK_STRING (value);
3022 if (SCHARS (value) == 0)
3023 signal_error ("Empty foreground color value", value);
3025 old_value = LFACE_FOREGROUND (lface);
3026 ASET (lface, LFACE_FOREGROUND_INDEX, value);
3028 else if (EQ (attr, QCbackground))
3030 /* Compatibility with 20.x. */
3031 if (NILP (value))
3032 value = Qunspecified;
3033 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3035 /* Don't check for valid color names here because it depends
3036 on the frame (display) whether the color will be valid
3037 when the face is realized. */
3038 CHECK_STRING (value);
3039 if (SCHARS (value) == 0)
3040 signal_error ("Empty background color value", value);
3042 old_value = LFACE_BACKGROUND (lface);
3043 ASET (lface, LFACE_BACKGROUND_INDEX, value);
3045 else if (EQ (attr, QCstipple))
3047 #if defined (HAVE_WINDOW_SYSTEM)
3048 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3049 && !NILP (value)
3050 && NILP (Fbitmap_spec_p (value)))
3051 signal_error ("Invalid stipple attribute", value);
3052 old_value = LFACE_STIPPLE (lface);
3053 ASET (lface, LFACE_STIPPLE_INDEX, value);
3054 #endif /* HAVE_WINDOW_SYSTEM */
3056 else if (EQ (attr, QCwidth))
3058 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3060 CHECK_SYMBOL (value);
3061 if (FONT_WIDTH_NAME_NUMERIC (value) < 0)
3062 signal_error ("Invalid face width", value);
3064 old_value = LFACE_SWIDTH (lface);
3065 ASET (lface, LFACE_SWIDTH_INDEX, value);
3066 prop_index = FONT_WIDTH_INDEX;
3068 else if (EQ (attr, QCfont))
3070 #ifdef HAVE_WINDOW_SYSTEM
3071 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
3073 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3075 struct frame *f;
3077 old_value = LFACE_FONT (lface);
3078 if (! FONTP (value))
3080 if (STRINGP (value))
3082 Lisp_Object name = value;
3083 int fontset = fs_query_fontset (name, 0);
3085 if (fontset >= 0)
3086 name = fontset_ascii (fontset);
3087 value = font_spec_from_name (name);
3088 if (!FONTP (value))
3089 signal_error ("Invalid font name", name);
3091 else
3092 signal_error ("Invalid font or font-spec", value);
3094 if (EQ (frame, Qt))
3095 f = XFRAME (selected_frame);
3096 else
3097 f = XFRAME (frame);
3098 if (! FONT_OBJECT_P (value))
3100 Lisp_Object *attrs = XVECTOR (lface)->contents;
3101 Lisp_Object font_object;
3103 font_object = font_load_for_lface (f, attrs, value);
3104 if (NILP (font_object))
3105 signal_error ("Font not available", value);
3106 value = font_object;
3108 set_lface_from_font (f, lface, value, 1);
3110 else
3111 ASET (lface, LFACE_FONT_INDEX, value);
3113 #endif /* HAVE_WINDOW_SYSTEM */
3115 else if (EQ (attr, QCfontset))
3117 #ifdef HAVE_WINDOW_SYSTEM
3118 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
3120 Lisp_Object tmp;
3122 old_value = LFACE_FONTSET (lface);
3123 tmp = Fquery_fontset (value, Qnil);
3124 if (NILP (tmp))
3125 signal_error ("Invalid fontset name", value);
3126 ASET (lface, LFACE_FONTSET_INDEX, value = tmp);
3128 #endif /* HAVE_WINDOW_SYSTEM */
3130 else if (EQ (attr, QCinherit))
3132 Lisp_Object tail;
3133 if (SYMBOLP (value))
3134 tail = Qnil;
3135 else
3136 for (tail = value; CONSP (tail); tail = XCDR (tail))
3137 if (!SYMBOLP (XCAR (tail)))
3138 break;
3139 if (NILP (tail))
3140 ASET (lface, LFACE_INHERIT_INDEX, value);
3141 else
3142 signal_error ("Invalid face inheritance", value);
3144 else if (EQ (attr, QCbold))
3146 old_value = LFACE_WEIGHT (lface);
3147 ASET (lface, LFACE_WEIGHT_INDEX, NILP (value) ? Qnormal : Qbold);
3148 prop_index = FONT_WEIGHT_INDEX;
3150 else if (EQ (attr, QCitalic))
3152 attr = QCslant;
3153 old_value = LFACE_SLANT (lface);
3154 ASET (lface, LFACE_SLANT_INDEX, NILP (value) ? Qnormal : Qitalic);
3155 prop_index = FONT_SLANT_INDEX;
3157 else
3158 signal_error ("Invalid face attribute name", attr);
3160 if (prop_index)
3162 /* If a font-related attribute other than QCfont and QCfontset
3163 is specified, and if the original QCfont attribute has a font
3164 (font-spec or font-object), set the corresponding property in
3165 the font to nil so that the font selector doesn't think that
3166 the attribute is mandatory. Also, clear the average
3167 width. */
3168 font_clear_prop (XVECTOR (lface)->contents, prop_index);
3171 /* Changing a named face means that all realized faces depending on
3172 that face are invalid. Since we cannot tell which realized faces
3173 depend on the face, make sure they are all removed. This is done
3174 by incrementing face_change_count. The next call to
3175 init_iterator will then free realized faces. */
3176 if (!EQ (frame, Qt)
3177 && NILP (Fget (face, Qface_no_inherit))
3178 && NILP (Fequal (old_value, value)))
3180 ++face_change_count;
3181 ++windows_or_buffers_changed;
3184 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3185 && NILP (Fequal (old_value, value)))
3187 Lisp_Object param;
3189 param = Qnil;
3191 if (EQ (face, Qdefault))
3193 #ifdef HAVE_WINDOW_SYSTEM
3194 /* Changed font-related attributes of the `default' face are
3195 reflected in changed `font' frame parameters. */
3196 if (FRAMEP (frame)
3197 && (prop_index || EQ (attr, QCfont))
3198 && lface_fully_specified_p (XVECTOR (lface)->contents))
3199 set_font_frame_param (frame, lface);
3200 else
3201 #endif /* HAVE_WINDOW_SYSTEM */
3203 if (EQ (attr, QCforeground))
3204 param = Qforeground_color;
3205 else if (EQ (attr, QCbackground))
3206 param = Qbackground_color;
3208 #ifdef HAVE_WINDOW_SYSTEM
3209 #ifndef HAVE_NTGUI
3210 else if (EQ (face, Qscroll_bar))
3212 /* Changing the colors of `scroll-bar' sets frame parameters
3213 `scroll-bar-foreground' and `scroll-bar-background'. */
3214 if (EQ (attr, QCforeground))
3215 param = Qscroll_bar_foreground;
3216 else if (EQ (attr, QCbackground))
3217 param = Qscroll_bar_background;
3219 #endif /* not HAVE_NTGUI */
3220 else if (EQ (face, Qborder))
3222 /* Changing background color of `border' sets frame parameter
3223 `border-color'. */
3224 if (EQ (attr, QCbackground))
3225 param = Qborder_color;
3227 else if (EQ (face, Qcursor))
3229 /* Changing background color of `cursor' sets frame parameter
3230 `cursor-color'. */
3231 if (EQ (attr, QCbackground))
3232 param = Qcursor_color;
3234 else if (EQ (face, Qmouse))
3236 /* Changing background color of `mouse' sets frame parameter
3237 `mouse-color'. */
3238 if (EQ (attr, QCbackground))
3239 param = Qmouse_color;
3241 #endif /* HAVE_WINDOW_SYSTEM */
3242 else if (EQ (face, Qmenu))
3244 /* Indicate that we have to update the menu bar when realizing
3245 faces on FRAME. FRAME t change the default for new frames.
3246 We do this by setting the flag in new face caches. */
3247 if (FRAMEP (frame))
3249 struct frame *f = XFRAME (frame);
3250 if (FRAME_FACE_CACHE (f) == NULL)
3251 FRAME_FACE_CACHE (f) = make_face_cache (f);
3252 FRAME_FACE_CACHE (f)->menu_face_changed_p = 1;
3254 else
3255 menu_face_changed_default = 1;
3258 if (!NILP (param))
3260 if (EQ (frame, Qt))
3261 /* Update `default-frame-alist', which is used for new frames. */
3263 store_in_alist (&Vdefault_frame_alist, param, value);
3265 else
3266 /* Update the current frame's parameters. */
3268 Lisp_Object cons;
3269 cons = XCAR (Vparam_value_alist);
3270 XSETCAR (cons, param);
3271 XSETCDR (cons, value);
3272 Fmodify_frame_parameters (frame, Vparam_value_alist);
3277 return face;
3281 /* Update the corresponding face when frame parameter PARAM on frame F
3282 has been assigned the value NEW_VALUE. */
3284 void
3285 update_face_from_frame_parameter (struct frame *f, Lisp_Object param,
3286 Lisp_Object new_value)
3288 Lisp_Object face = Qnil;
3289 Lisp_Object lface;
3291 /* If there are no faces yet, give up. This is the case when called
3292 from Fx_create_frame, and we do the necessary things later in
3293 face-set-after-frame-defaults. */
3294 if (NILP (f->face_alist))
3295 return;
3297 if (EQ (param, Qforeground_color))
3299 face = Qdefault;
3300 lface = lface_from_face_name (f, face, 1);
3301 ASET (lface, LFACE_FOREGROUND_INDEX,
3302 (STRINGP (new_value) ? new_value : Qunspecified));
3303 realize_basic_faces (f);
3305 else if (EQ (param, Qbackground_color))
3307 Lisp_Object frame;
3309 /* Changing the background color might change the background
3310 mode, so that we have to load new defface specs.
3311 Call frame-set-background-mode to do that. */
3312 XSETFRAME (frame, f);
3313 call1 (Qframe_set_background_mode, frame);
3315 face = Qdefault;
3316 lface = lface_from_face_name (f, face, 1);
3317 ASET (lface, LFACE_BACKGROUND_INDEX,
3318 (STRINGP (new_value) ? new_value : Qunspecified));
3319 realize_basic_faces (f);
3321 #ifdef HAVE_WINDOW_SYSTEM
3322 else if (EQ (param, Qborder_color))
3324 face = Qborder;
3325 lface = lface_from_face_name (f, face, 1);
3326 ASET (lface, LFACE_BACKGROUND_INDEX,
3327 (STRINGP (new_value) ? new_value : Qunspecified));
3329 else if (EQ (param, Qcursor_color))
3331 face = Qcursor;
3332 lface = lface_from_face_name (f, face, 1);
3333 ASET (lface, LFACE_BACKGROUND_INDEX,
3334 (STRINGP (new_value) ? new_value : Qunspecified));
3336 else if (EQ (param, Qmouse_color))
3338 face = Qmouse;
3339 lface = lface_from_face_name (f, face, 1);
3340 ASET (lface, LFACE_BACKGROUND_INDEX,
3341 (STRINGP (new_value) ? new_value : Qunspecified));
3343 #endif
3345 /* Changing a named face means that all realized faces depending on
3346 that face are invalid. Since we cannot tell which realized faces
3347 depend on the face, make sure they are all removed. This is done
3348 by incrementing face_change_count. The next call to
3349 init_iterator will then free realized faces. */
3350 if (!NILP (face)
3351 && NILP (Fget (face, Qface_no_inherit)))
3353 ++face_change_count;
3354 ++windows_or_buffers_changed;
3359 #ifdef HAVE_WINDOW_SYSTEM
3361 /* Set the `font' frame parameter of FRAME determined from the
3362 font-object set in `default' face attributes LFACE. */
3364 static void
3365 set_font_frame_param (Lisp_Object frame, Lisp_Object lface)
3367 struct frame *f = XFRAME (frame);
3368 Lisp_Object font;
3370 if (FRAME_WINDOW_P (f)
3371 /* Don't do anything if the font is `unspecified'. This can
3372 happen during frame creation. */
3373 && (font = LFACE_FONT (lface),
3374 ! UNSPECIFIEDP (font)))
3376 if (FONT_SPEC_P (font))
3378 font = font_load_for_lface (f, XVECTOR (lface)->contents, font);
3379 if (NILP (font))
3380 return;
3381 ASET (lface, LFACE_FONT_INDEX, font);
3383 f->default_face_done_p = 0;
3384 Fmodify_frame_parameters (frame, list1 (Fcons (Qfont, font)));
3388 DEFUN ("internal-face-x-get-resource", Finternal_face_x_get_resource,
3389 Sinternal_face_x_get_resource, 2, 3, 0,
3390 doc: /* Get the value of X resource RESOURCE, class CLASS.
3391 Returned value is for the display of frame FRAME. If FRAME is not
3392 specified or nil, use selected frame. This function exists because
3393 ordinary `x-get-resource' doesn't take a frame argument. */)
3394 (Lisp_Object resource, Lisp_Object class, Lisp_Object frame)
3396 Lisp_Object value = Qnil;
3397 struct frame *f;
3399 CHECK_STRING (resource);
3400 CHECK_STRING (class);
3401 f = decode_live_frame (frame);
3402 block_input ();
3403 value = display_x_get_resource (FRAME_X_DISPLAY_INFO (f),
3404 resource, class, Qnil, Qnil);
3405 unblock_input ();
3406 return value;
3410 /* Return resource string VALUE as a boolean value, i.e. nil, or t.
3411 If VALUE is "on" or "true", return t. If VALUE is "off" or
3412 "false", return nil. Otherwise, if SIGNAL_P is non-zero, signal an
3413 error; if SIGNAL_P is zero, return 0. */
3415 static Lisp_Object
3416 face_boolean_x_resource_value (Lisp_Object value, int signal_p)
3418 Lisp_Object result = make_number (0);
3420 eassert (STRINGP (value));
3422 if (xstrcasecmp (SSDATA (value), "on") == 0
3423 || xstrcasecmp (SSDATA (value), "true") == 0)
3424 result = Qt;
3425 else if (xstrcasecmp (SSDATA (value), "off") == 0
3426 || xstrcasecmp (SSDATA (value), "false") == 0)
3427 result = Qnil;
3428 else if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3429 result = Qunspecified;
3430 else if (signal_p)
3431 signal_error ("Invalid face attribute value from X resource", value);
3433 return result;
3437 DEFUN ("internal-set-lisp-face-attribute-from-resource",
3438 Finternal_set_lisp_face_attribute_from_resource,
3439 Sinternal_set_lisp_face_attribute_from_resource,
3440 3, 4, 0, doc: /* */)
3441 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
3443 CHECK_SYMBOL (face);
3444 CHECK_SYMBOL (attr);
3445 CHECK_STRING (value);
3447 if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3448 value = Qunspecified;
3449 else if (EQ (attr, QCheight))
3451 value = Fstring_to_number (value, make_number (10));
3452 if (XINT (value) <= 0)
3453 signal_error ("Invalid face height from X resource", value);
3455 else if (EQ (attr, QCbold) || EQ (attr, QCitalic))
3456 value = face_boolean_x_resource_value (value, 1);
3457 else if (EQ (attr, QCweight) || EQ (attr, QCslant) || EQ (attr, QCwidth))
3458 value = intern (SSDATA (value));
3459 else if (EQ (attr, QCreverse_video) || EQ (attr, QCinverse_video))
3460 value = face_boolean_x_resource_value (value, 1);
3461 else if (EQ (attr, QCunderline)
3462 || EQ (attr, QCoverline)
3463 || EQ (attr, QCstrike_through))
3465 Lisp_Object boolean_value;
3467 /* If the result of face_boolean_x_resource_value is t or nil,
3468 VALUE does NOT specify a color. */
3469 boolean_value = face_boolean_x_resource_value (value, 0);
3470 if (SYMBOLP (boolean_value))
3471 value = boolean_value;
3473 else if (EQ (attr, QCbox) || EQ (attr, QCinherit))
3474 value = Fcar (Fread_from_string (value, Qnil, Qnil));
3476 return Finternal_set_lisp_face_attribute (face, attr, value, frame);
3479 #endif /* HAVE_WINDOW_SYSTEM */
3482 /***********************************************************************
3483 Menu face
3484 ***********************************************************************/
3486 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
3488 /* Make menus on frame F appear as specified by the `menu' face. */
3490 static void
3491 x_update_menu_appearance (struct frame *f)
3493 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3494 XrmDatabase rdb;
3496 if (dpyinfo
3497 && (rdb = XrmGetDatabase (FRAME_X_DISPLAY (f)),
3498 rdb != NULL))
3500 char line[512];
3501 char *buf = line;
3502 ptrdiff_t bufsize = sizeof line;
3503 Lisp_Object lface = lface_from_face_name (f, Qmenu, 1);
3504 struct face *face = FACE_FROM_ID (f, MENU_FACE_ID);
3505 const char *myname = SSDATA (Vx_resource_name);
3506 bool changed_p = 0;
3507 #ifdef USE_MOTIF
3508 const char *popup_path = "popup_menu";
3509 #else
3510 const char *popup_path = "menu.popup";
3511 #endif
3513 if (STRINGP (LFACE_FOREGROUND (lface)))
3515 exprintf (&buf, &bufsize, line, -1, "%s.%s*foreground: %s",
3516 myname, popup_path,
3517 SDATA (LFACE_FOREGROUND (lface)));
3518 XrmPutLineResource (&rdb, line);
3519 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*foreground: %s",
3520 myname, SDATA (LFACE_FOREGROUND (lface)));
3521 XrmPutLineResource (&rdb, line);
3522 changed_p = 1;
3525 if (STRINGP (LFACE_BACKGROUND (lface)))
3527 exprintf (&buf, &bufsize, line, -1, "%s.%s*background: %s",
3528 myname, popup_path,
3529 SDATA (LFACE_BACKGROUND (lface)));
3530 XrmPutLineResource (&rdb, line);
3532 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*background: %s",
3533 myname, SDATA (LFACE_BACKGROUND (lface)));
3534 XrmPutLineResource (&rdb, line);
3535 changed_p = 1;
3538 if (face->font
3539 /* On Solaris 5.8, it's been reported that the `menu' face
3540 can be unspecified here, during startup. Why this
3541 happens remains unknown. -- cyd */
3542 && FONTP (LFACE_FONT (lface))
3543 && (!UNSPECIFIEDP (LFACE_FAMILY (lface))
3544 || !UNSPECIFIEDP (LFACE_FOUNDRY (lface))
3545 || !UNSPECIFIEDP (LFACE_SWIDTH (lface))
3546 || !UNSPECIFIEDP (LFACE_WEIGHT (lface))
3547 || !UNSPECIFIEDP (LFACE_SLANT (lface))
3548 || !UNSPECIFIEDP (LFACE_HEIGHT (lface))))
3550 Lisp_Object xlfd = Ffont_xlfd_name (LFACE_FONT (lface), Qnil);
3551 #ifdef USE_MOTIF
3552 const char *suffix = "List";
3553 Bool motif = True;
3554 #else
3555 #if defined HAVE_X_I18N
3557 const char *suffix = "Set";
3558 #else
3559 const char *suffix = "";
3560 #endif
3561 Bool motif = False;
3562 #endif
3564 if (! NILP (xlfd))
3566 #if defined HAVE_X_I18N
3567 char *fontsetname = xic_create_fontsetname (SSDATA (xlfd), motif);
3568 #else
3569 char *fontsetname = SSDATA (xlfd);
3570 #endif
3571 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*font%s: %s",
3572 myname, suffix, fontsetname);
3573 XrmPutLineResource (&rdb, line);
3575 exprintf (&buf, &bufsize, line, -1, "%s.%s*font%s: %s",
3576 myname, popup_path, suffix, fontsetname);
3577 XrmPutLineResource (&rdb, line);
3578 changed_p = 1;
3579 if (fontsetname != SSDATA (xlfd))
3580 xfree (fontsetname);
3584 if (changed_p && f->output_data.x->menubar_widget)
3585 free_frame_menubar (f);
3587 if (buf != line)
3588 xfree (buf);
3592 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
3595 DEFUN ("face-attribute-relative-p", Fface_attribute_relative_p,
3596 Sface_attribute_relative_p,
3597 2, 2, 0,
3598 doc: /* Check whether a face attribute value is relative.
3599 Specifically, this function returns t if the attribute ATTRIBUTE
3600 with the value VALUE is relative.
3602 A relative value is one that doesn't entirely override whatever is
3603 inherited from another face. For most possible attributes,
3604 the only relative value that users see is `unspecified'.
3605 However, for :height, floating point values are also relative. */)
3606 (Lisp_Object attribute, Lisp_Object value)
3608 if (EQ (value, Qunspecified) || (EQ (value, QCignore_defface)))
3609 return Qt;
3610 else if (EQ (attribute, QCheight))
3611 return INTEGERP (value) ? Qnil : Qt;
3612 else
3613 return Qnil;
3616 DEFUN ("merge-face-attribute", Fmerge_face_attribute, Smerge_face_attribute,
3617 3, 3, 0,
3618 doc: /* Return face ATTRIBUTE VALUE1 merged with VALUE2.
3619 If VALUE1 or VALUE2 are absolute (see `face-attribute-relative-p'), then
3620 the result will be absolute, otherwise it will be relative. */)
3621 (Lisp_Object attribute, Lisp_Object value1, Lisp_Object value2)
3623 if (EQ (value1, Qunspecified) || EQ (value1, QCignore_defface))
3624 return value2;
3625 else if (EQ (attribute, QCheight))
3626 return merge_face_heights (value1, value2, value1);
3627 else
3628 return value1;
3632 DEFUN ("internal-get-lisp-face-attribute", Finternal_get_lisp_face_attribute,
3633 Sinternal_get_lisp_face_attribute,
3634 2, 3, 0,
3635 doc: /* Return face attribute KEYWORD of face SYMBOL.
3636 If SYMBOL does not name a valid Lisp face or KEYWORD isn't a valid
3637 face attribute name, signal an error.
3638 If the optional argument FRAME is given, report on face SYMBOL in that
3639 frame. If FRAME is t, report on the defaults for face SYMBOL (for new
3640 frames). If FRAME is omitted or nil, use the selected frame. */)
3641 (Lisp_Object symbol, Lisp_Object keyword, Lisp_Object frame)
3643 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3644 Lisp_Object lface = lface_from_face_name (f, symbol, 1), value = Qnil;
3646 CHECK_SYMBOL (symbol);
3647 CHECK_SYMBOL (keyword);
3649 if (EQ (keyword, QCfamily))
3650 value = LFACE_FAMILY (lface);
3651 else if (EQ (keyword, QCfoundry))
3652 value = LFACE_FOUNDRY (lface);
3653 else if (EQ (keyword, QCheight))
3654 value = LFACE_HEIGHT (lface);
3655 else if (EQ (keyword, QCweight))
3656 value = LFACE_WEIGHT (lface);
3657 else if (EQ (keyword, QCslant))
3658 value = LFACE_SLANT (lface);
3659 else if (EQ (keyword, QCunderline))
3660 value = LFACE_UNDERLINE (lface);
3661 else if (EQ (keyword, QCoverline))
3662 value = LFACE_OVERLINE (lface);
3663 else if (EQ (keyword, QCstrike_through))
3664 value = LFACE_STRIKE_THROUGH (lface);
3665 else if (EQ (keyword, QCbox))
3666 value = LFACE_BOX (lface);
3667 else if (EQ (keyword, QCinverse_video)
3668 || EQ (keyword, QCreverse_video))
3669 value = LFACE_INVERSE (lface);
3670 else if (EQ (keyword, QCforeground))
3671 value = LFACE_FOREGROUND (lface);
3672 else if (EQ (keyword, QCbackground))
3673 value = LFACE_BACKGROUND (lface);
3674 else if (EQ (keyword, QCstipple))
3675 value = LFACE_STIPPLE (lface);
3676 else if (EQ (keyword, QCwidth))
3677 value = LFACE_SWIDTH (lface);
3678 else if (EQ (keyword, QCinherit))
3679 value = LFACE_INHERIT (lface);
3680 else if (EQ (keyword, QCfont))
3681 value = LFACE_FONT (lface);
3682 else if (EQ (keyword, QCfontset))
3683 value = LFACE_FONTSET (lface);
3684 else
3685 signal_error ("Invalid face attribute name", keyword);
3687 if (IGNORE_DEFFACE_P (value))
3688 return Qunspecified;
3690 return value;
3694 DEFUN ("internal-lisp-face-attribute-values",
3695 Finternal_lisp_face_attribute_values,
3696 Sinternal_lisp_face_attribute_values, 1, 1, 0,
3697 doc: /* Return a list of valid discrete values for face attribute ATTR.
3698 Value is nil if ATTR doesn't have a discrete set of valid values. */)
3699 (Lisp_Object attr)
3701 Lisp_Object result = Qnil;
3703 CHECK_SYMBOL (attr);
3705 if (EQ (attr, QCunderline) || EQ (attr, QCoverline)
3706 || EQ (attr, QCstrike_through)
3707 || EQ (attr, QCinverse_video) || EQ (attr, QCreverse_video))
3708 result = list2 (Qt, Qnil);
3710 return result;
3714 DEFUN ("internal-merge-in-global-face", Finternal_merge_in_global_face,
3715 Sinternal_merge_in_global_face, 2, 2, 0,
3716 doc: /* Add attributes from frame-default definition of FACE to FACE on FRAME.
3717 Default face attributes override any local face attributes. */)
3718 (Lisp_Object face, Lisp_Object frame)
3720 int i;
3721 Lisp_Object global_lface, local_lface, *gvec, *lvec;
3722 struct frame *f = XFRAME (frame);
3724 CHECK_LIVE_FRAME (frame);
3725 global_lface = lface_from_face_name (NULL, face, 1);
3726 local_lface = lface_from_face_name (f, face, 0);
3727 if (NILP (local_lface))
3728 local_lface = Finternal_make_lisp_face (face, frame);
3730 /* Make every specified global attribute override the local one.
3731 BEWARE!! This is only used from `face-set-after-frame-default' where
3732 the local frame is defined from default specs in `face-defface-spec'
3733 and those should be overridden by global settings. Hence the strange
3734 "global before local" priority. */
3735 lvec = XVECTOR (local_lface)->contents;
3736 gvec = XVECTOR (global_lface)->contents;
3737 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3738 if (IGNORE_DEFFACE_P (gvec[i]))
3739 ASET (local_lface, i, Qunspecified);
3740 else if (! UNSPECIFIEDP (gvec[i]))
3741 ASET (local_lface, i, AREF (global_lface, i));
3743 /* If the default face was changed, update the face cache and the
3744 `font' frame parameter. */
3745 if (EQ (face, Qdefault))
3747 struct face_cache *c = FRAME_FACE_CACHE (f);
3748 struct face *newface, *oldface = FACE_FROM_ID (f, DEFAULT_FACE_ID);
3749 Lisp_Object attrs[LFACE_VECTOR_SIZE];
3751 /* This can be NULL (e.g., in batch mode). */
3752 if (oldface)
3754 /* Ensure that the face vector is fully specified by merging
3755 the previously-cached vector. */
3756 memcpy (attrs, oldface->lface, sizeof attrs);
3757 merge_face_vectors (f, lvec, attrs, 0);
3758 vcopy (local_lface, 0, attrs, LFACE_VECTOR_SIZE);
3759 newface = realize_face (c, lvec, DEFAULT_FACE_ID);
3761 if ((! UNSPECIFIEDP (gvec[LFACE_FAMILY_INDEX])
3762 || ! UNSPECIFIEDP (gvec[LFACE_FOUNDRY_INDEX])
3763 || ! UNSPECIFIEDP (gvec[LFACE_HEIGHT_INDEX])
3764 || ! UNSPECIFIEDP (gvec[LFACE_WEIGHT_INDEX])
3765 || ! UNSPECIFIEDP (gvec[LFACE_SLANT_INDEX])
3766 || ! UNSPECIFIEDP (gvec[LFACE_SWIDTH_INDEX])
3767 || ! UNSPECIFIEDP (gvec[LFACE_FONT_INDEX]))
3768 && newface->font)
3770 Lisp_Object name = newface->font->props[FONT_NAME_INDEX];
3771 Fmodify_frame_parameters (frame, list1 (Fcons (Qfont, name)));
3774 if (STRINGP (gvec[LFACE_FOREGROUND_INDEX]))
3775 Fmodify_frame_parameters (frame,
3776 list1 (Fcons (Qforeground_color,
3777 gvec[LFACE_FOREGROUND_INDEX])));
3779 if (STRINGP (gvec[LFACE_BACKGROUND_INDEX]))
3780 Fmodify_frame_parameters (frame,
3781 list1 (Fcons (Qbackground_color,
3782 gvec[LFACE_BACKGROUND_INDEX])));
3786 return Qnil;
3790 /* The following function is implemented for compatibility with 20.2.
3791 The function is used in x-resolve-fonts when it is asked to
3792 return fonts with the same size as the font of a face. This is
3793 done in fontset.el. */
3795 DEFUN ("face-font", Fface_font, Sface_font, 1, 3, 0,
3796 doc: /* Return the font name of face FACE, or nil if it is unspecified.
3797 The font name is, by default, for ASCII characters.
3798 If the optional argument FRAME is given, report on face FACE in that frame.
3799 If FRAME is t, report on the defaults for face FACE (for new frames).
3800 The font default for a face is either nil, or a list
3801 of the form (bold), (italic) or (bold italic).
3802 If FRAME is omitted or nil, use the selected frame. And, in this case,
3803 if the optional third argument CHARACTER is given,
3804 return the font name used for CHARACTER. */)
3805 (Lisp_Object face, Lisp_Object frame, Lisp_Object character)
3807 if (EQ (frame, Qt))
3809 Lisp_Object result = Qnil;
3810 Lisp_Object lface = lface_from_face_name (NULL, face, 1);
3812 if (!UNSPECIFIEDP (LFACE_WEIGHT (lface))
3813 && !EQ (LFACE_WEIGHT (lface), Qnormal))
3814 result = Fcons (Qbold, result);
3816 if (!UNSPECIFIEDP (LFACE_SLANT (lface))
3817 && !EQ (LFACE_SLANT (lface), Qnormal))
3818 result = Fcons (Qitalic, result);
3820 return result;
3822 else
3824 struct frame *f = decode_live_frame (frame);
3825 int face_id = lookup_named_face (f, face, 1);
3826 struct face *fface = FACE_FROM_ID (f, face_id);
3828 if (! fface)
3829 return Qnil;
3830 #ifdef HAVE_WINDOW_SYSTEM
3831 if (FRAME_WINDOW_P (f) && !NILP (character))
3833 CHECK_CHARACTER (character);
3834 face_id = FACE_FOR_CHAR (f, fface, XINT (character), -1, Qnil);
3835 fface = FACE_FROM_ID (f, face_id);
3837 return (fface->font
3838 ? fface->font->props[FONT_NAME_INDEX]
3839 : Qnil);
3840 #else /* !HAVE_WINDOW_SYSTEM */
3841 return build_string (FRAME_MSDOS_P (f)
3842 ? "ms-dos"
3843 : FRAME_W32_P (f) ? "w32term"
3844 :"tty");
3845 #endif
3850 /* Compare face-attribute values v1 and v2 for equality. Value is non-zero if
3851 all attributes are `equal'. Tries to be fast because this function
3852 is called quite often. */
3854 static bool
3855 face_attr_equal_p (Lisp_Object v1, Lisp_Object v2)
3857 /* Type can differ, e.g. when one attribute is unspecified, i.e. nil,
3858 and the other is specified. */
3859 if (XTYPE (v1) != XTYPE (v2))
3860 return 0;
3862 if (EQ (v1, v2))
3863 return 1;
3865 switch (XTYPE (v1))
3867 case Lisp_String:
3868 if (SBYTES (v1) != SBYTES (v2))
3869 return 0;
3871 return memcmp (SDATA (v1), SDATA (v2), SBYTES (v1)) == 0;
3873 case_Lisp_Int:
3874 case Lisp_Symbol:
3875 return 0;
3877 default:
3878 return !NILP (Fequal (v1, v2));
3883 /* Compare face vectors V1 and V2 for equality. Value is non-zero if
3884 all attributes are `equal'. Tries to be fast because this function
3885 is called quite often. */
3887 static bool
3888 lface_equal_p (Lisp_Object *v1, Lisp_Object *v2)
3890 int i;
3891 bool equal_p = 1;
3893 for (i = 1; i < LFACE_VECTOR_SIZE && equal_p; ++i)
3894 equal_p = face_attr_equal_p (v1[i], v2[i]);
3896 return equal_p;
3900 DEFUN ("internal-lisp-face-equal-p", Finternal_lisp_face_equal_p,
3901 Sinternal_lisp_face_equal_p, 2, 3, 0,
3902 doc: /* True if FACE1 and FACE2 are equal.
3903 If the optional argument FRAME is given, report on FACE1 and FACE2 in that frame.
3904 If FRAME is t, report on the defaults for FACE1 and FACE2 (for new frames).
3905 If FRAME is omitted or nil, use the selected frame. */)
3906 (Lisp_Object face1, Lisp_Object face2, Lisp_Object frame)
3908 int equal_p;
3909 struct frame *f;
3910 Lisp_Object lface1, lface2;
3912 /* Don't use decode_window_system_frame here because this function
3913 is called before X frames exist. At that time, if FRAME is nil,
3914 selected_frame will be used which is the frame dumped with
3915 Emacs. That frame is not an X frame. */
3916 f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3918 lface1 = lface_from_face_name (f, face1, 1);
3919 lface2 = lface_from_face_name (f, face2, 1);
3920 equal_p = lface_equal_p (XVECTOR (lface1)->contents,
3921 XVECTOR (lface2)->contents);
3922 return equal_p ? Qt : Qnil;
3926 DEFUN ("internal-lisp-face-empty-p", Finternal_lisp_face_empty_p,
3927 Sinternal_lisp_face_empty_p, 1, 2, 0,
3928 doc: /* True if FACE has no attribute specified.
3929 If the optional argument FRAME is given, report on face FACE in that frame.
3930 If FRAME is t, report on the defaults for face FACE (for new frames).
3931 If FRAME is omitted or nil, use the selected frame. */)
3932 (Lisp_Object face, Lisp_Object frame)
3934 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3935 Lisp_Object lface = lface_from_face_name (f, face, 1);
3936 int i;
3938 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3939 if (!UNSPECIFIEDP (AREF (lface, i)))
3940 break;
3942 return i == LFACE_VECTOR_SIZE ? Qt : Qnil;
3946 DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist,
3947 0, 1, 0,
3948 doc: /* Return an alist of frame-local faces defined on FRAME.
3949 For internal use only. */)
3950 (Lisp_Object frame)
3952 return decode_live_frame (frame)->face_alist;
3956 /* Return a hash code for Lisp string STRING with case ignored. Used
3957 below in computing a hash value for a Lisp face. */
3959 static unsigned
3960 hash_string_case_insensitive (Lisp_Object string)
3962 const unsigned char *s;
3963 unsigned hash = 0;
3964 eassert (STRINGP (string));
3965 for (s = SDATA (string); *s; ++s)
3966 hash = (hash << 1) ^ c_tolower (*s);
3967 return hash;
3971 /* Return a hash code for face attribute vector V. */
3973 static unsigned
3974 lface_hash (Lisp_Object *v)
3976 return (hash_string_case_insensitive (v[LFACE_FAMILY_INDEX])
3977 ^ hash_string_case_insensitive (v[LFACE_FOUNDRY_INDEX])
3978 ^ hash_string_case_insensitive (v[LFACE_FOREGROUND_INDEX])
3979 ^ hash_string_case_insensitive (v[LFACE_BACKGROUND_INDEX])
3980 ^ XHASH (v[LFACE_WEIGHT_INDEX])
3981 ^ XHASH (v[LFACE_SLANT_INDEX])
3982 ^ XHASH (v[LFACE_SWIDTH_INDEX])
3983 ^ XHASH (v[LFACE_HEIGHT_INDEX]));
3986 #ifdef HAVE_WINDOW_SYSTEM
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]))))
4015 #endif /* HAVE_WINDOW_SYSTEM */
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;
4175 #ifdef HAVE_WINDOW_SYSTEM
4177 /* Clear out all graphics contexts for all realized faces, except for
4178 the basic faces. This should be done from time to time just to avoid
4179 keeping too many graphics contexts that are no longer needed. */
4181 static void
4182 clear_face_gcs (struct face_cache *c)
4184 if (c && FRAME_WINDOW_P (c->f))
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 ();
4203 #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