Minor changes, detected by test suite.
[emacs.git] / src / xfaces.c
blob8a74c92b10d34e865bacb57bcd68dba5542f41a8
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 #define x_display_info w32_display_info
228 #define GCGraphicsExposures 0
229 #endif /* HAVE_NTGUI */
231 #ifdef HAVE_NS
232 #define GCGraphicsExposures 0
233 #endif /* HAVE_NS */
234 #endif /* HAVE_WINDOW_SYSTEM */
236 #include "buffer.h"
237 #include "dispextern.h"
238 #include "blockinput.h"
239 #include "window.h"
240 #include "intervals.h"
241 #include "termchar.h"
243 #include "font.h"
245 #ifdef HAVE_X_WINDOWS
247 /* Compensate for a bug in Xos.h on some systems, on which it requires
248 time.h. On some such systems, Xos.h tries to redefine struct
249 timeval and struct timezone if USG is #defined while it is
250 #included. */
252 #ifdef XOS_NEEDS_TIME_H
253 #include <time.h>
254 #undef USG
255 #include <X11/Xos.h>
256 #define USG
257 #define __TIMEVAL__
258 #if defined USG || defined __TIMEVAL__ /* Don't warn about unused macros. */
259 #endif
260 #else /* not XOS_NEEDS_TIME_H */
261 #include <X11/Xos.h>
262 #endif /* not XOS_NEEDS_TIME_H */
264 #endif /* HAVE_X_WINDOWS */
266 #include <c-ctype.h>
268 /* Non-zero if face attribute ATTR is unspecified. */
270 #define UNSPECIFIEDP(ATTR) EQ ((ATTR), Qunspecified)
272 /* Non-zero if face attribute ATTR is `ignore-defface'. */
274 #define IGNORE_DEFFACE_P(ATTR) EQ ((ATTR), QCignore_defface)
276 /* Value is the number of elements of VECTOR. */
278 #define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
280 /* Size of hash table of realized faces in face caches (should be a
281 prime number). */
283 #define FACE_CACHE_BUCKETS_SIZE 1001
285 /* Keyword symbols used for face attribute names. */
287 Lisp_Object QCfamily, QCheight, QCweight, QCslant;
288 static Lisp_Object QCunderline;
289 static Lisp_Object QCinverse_video, QCstipple;
290 Lisp_Object QCforeground, QCbackground;
291 Lisp_Object QCwidth;
292 static Lisp_Object QCfont, QCbold, QCitalic;
293 static Lisp_Object QCreverse_video;
294 static Lisp_Object QCoverline, QCstrike_through, QCbox, QCinherit;
295 static Lisp_Object QCfontset, QCdistant_foreground;
297 /* Symbols used for attribute values. */
299 Lisp_Object Qnormal;
300 Lisp_Object Qbold;
301 static Lisp_Object Qline, Qwave;
302 Lisp_Object Qextra_light, Qlight;
303 Lisp_Object Qsemi_light, Qsemi_bold, Qextra_bold, Qultra_bold;
304 Lisp_Object Qoblique;
305 Lisp_Object Qitalic;
306 static Lisp_Object Qreleased_button, Qpressed_button;
307 static Lisp_Object QCstyle, QCcolor, QCline_width;
308 Lisp_Object Qunspecified; /* used in dosfns.c */
309 static Lisp_Object QCignore_defface;
311 char unspecified_fg[] = "unspecified-fg", unspecified_bg[] = "unspecified-bg";
313 /* The name of the function to call when the background of the frame
314 has changed, frame_set_background_mode. */
316 static Lisp_Object Qframe_set_background_mode;
318 /* Names of basic faces. */
320 Lisp_Object Qdefault, Qtool_bar, Qfringe;
321 static Lisp_Object Qregion;
322 Lisp_Object Qheader_line, Qscroll_bar, Qcursor;
323 static Lisp_Object Qborder, Qmouse, Qmenu;
324 Lisp_Object Qmode_line_inactive;
325 static Lisp_Object Qvertical_border;
326 static Lisp_Object Qwindow_divider;
328 /* The symbol `face-alias'. A symbols having that property is an
329 alias for another face. Value of the property is the name of
330 the aliased face. */
332 static Lisp_Object Qface_alias;
334 /* Alist of alternative font families. Each element is of the form
335 (FAMILY FAMILY1 FAMILY2 ...). If fonts of FAMILY can't be loaded,
336 try FAMILY1, then FAMILY2, ... */
338 Lisp_Object Vface_alternative_font_family_alist;
340 /* Alist of alternative font registries. Each element is of the form
341 (REGISTRY REGISTRY1 REGISTRY2...). If fonts of REGISTRY can't be
342 loaded, try REGISTRY1, then REGISTRY2, ... */
344 Lisp_Object Vface_alternative_font_registry_alist;
346 /* Allowed scalable fonts. A value of nil means don't allow any
347 scalable fonts. A value of t means allow the use of any scalable
348 font. Otherwise, value must be a list of regular expressions. A
349 font may be scaled if its name matches a regular expression in the
350 list. */
352 static Lisp_Object Qscalable_fonts_allowed;
354 /* The symbols `foreground-color' and `background-color' which can be
355 used as part of a `face' property. This is for compatibility with
356 Emacs 20.2. */
358 Lisp_Object Qforeground_color, Qbackground_color;
360 /* The symbols `face' and `mouse-face' used as text properties. */
362 Lisp_Object Qface;
364 /* Property for basic faces which other faces cannot inherit. */
366 static Lisp_Object Qface_no_inherit;
368 /* Error symbol for wrong_type_argument in load_pixmap. */
370 static Lisp_Object Qbitmap_spec_p;
372 /* The next ID to assign to Lisp faces. */
374 static int next_lface_id;
376 /* A vector mapping Lisp face Id's to face names. */
378 static Lisp_Object *lface_id_to_name;
379 static ptrdiff_t lface_id_to_name_size;
381 /* TTY color-related functions (defined in tty-colors.el). */
383 static Lisp_Object Qtty_color_desc, Qtty_color_by_index, Qtty_color_standard_values;
385 /* The name of the function used to compute colors on TTYs. */
387 static Lisp_Object Qtty_color_alist;
389 #ifdef HAVE_WINDOW_SYSTEM
391 /* Counter for calls to clear_face_cache. If this counter reaches
392 CLEAR_FONT_TABLE_COUNT, and a frame has more than
393 CLEAR_FONT_TABLE_NFONTS load, unused fonts are freed. */
395 static int clear_font_table_count;
396 #define CLEAR_FONT_TABLE_COUNT 100
397 #define CLEAR_FONT_TABLE_NFONTS 10
399 #endif /* HAVE_WINDOW_SYSTEM */
401 /* Non-zero means face attributes have been changed since the last
402 redisplay. Used in redisplay_internal. */
404 int face_change_count;
406 /* Non-zero means don't display bold text if a face's foreground
407 and background colors are the inverse of the default colors of the
408 display. This is a kluge to suppress `bold black' foreground text
409 which is hard to read on an LCD monitor. */
411 static int tty_suppress_bold_inverse_default_colors_p;
413 /* A list of the form `((x . y))' used to avoid consing in
414 Finternal_set_lisp_face_attribute. */
416 static Lisp_Object Vparam_value_alist;
418 /* The total number of colors currently allocated. */
420 #ifdef GLYPH_DEBUG
421 static int ncolors_allocated;
422 static int npixmaps_allocated;
423 static int ngcs;
424 #endif
426 /* Non-zero means the definition of the `menu' face for new frames has
427 been changed. */
429 static int menu_face_changed_default;
431 struct named_merge_point;
433 static struct face *realize_face (struct face_cache *, Lisp_Object *,
434 int);
435 static struct face *realize_x_face (struct face_cache *, Lisp_Object *);
436 static struct face *realize_tty_face (struct face_cache *, Lisp_Object *);
437 static bool realize_basic_faces (struct frame *);
438 static bool realize_default_face (struct frame *);
439 static void realize_named_face (struct frame *, Lisp_Object, int);
440 static struct face_cache *make_face_cache (struct frame *);
441 static void free_face_cache (struct face_cache *);
442 static int merge_face_ref (struct frame *, Lisp_Object, Lisp_Object *,
443 int, struct named_merge_point *);
444 static int color_distance (XColor *x, XColor *y);
446 #ifdef HAVE_WINDOW_SYSTEM
447 static void set_font_frame_param (Lisp_Object, Lisp_Object);
448 static void clear_face_gcs (struct face_cache *);
449 static struct face *realize_non_ascii_face (struct frame *, Lisp_Object,
450 struct face *);
451 #endif /* HAVE_WINDOW_SYSTEM */
453 /***********************************************************************
454 Utilities
455 ***********************************************************************/
457 #ifdef HAVE_X_WINDOWS
459 #ifdef DEBUG_X_COLORS
461 /* The following is a poor mans infrastructure for debugging X color
462 allocation problems on displays with PseudoColor-8. Some X servers
463 like 3.3.5 XF86_SVGA with Matrox cards apparently don't implement
464 color reference counts completely so that they don't signal an
465 error when a color is freed whose reference count is already 0.
466 Other X servers do. To help me debug this, the following code
467 implements a simple reference counting schema of its own, for a
468 single display/screen. --gerd. */
470 /* Reference counts for pixel colors. */
472 int color_count[256];
474 /* Register color PIXEL as allocated. */
476 void
477 register_color (unsigned long pixel)
479 eassert (pixel < 256);
480 ++color_count[pixel];
484 /* Register color PIXEL as deallocated. */
486 void
487 unregister_color (unsigned long pixel)
489 eassert (pixel < 256);
490 if (color_count[pixel] > 0)
491 --color_count[pixel];
492 else
493 emacs_abort ();
497 /* Register N colors from PIXELS as deallocated. */
499 void
500 unregister_colors (unsigned long *pixels, int n)
502 int i;
503 for (i = 0; i < n; ++i)
504 unregister_color (pixels[i]);
508 DEFUN ("dump-colors", Fdump_colors, Sdump_colors, 0, 0, 0,
509 doc: /* Dump currently allocated colors to stderr. */)
510 (void)
512 int i, n;
514 fputc ('\n', stderr);
516 for (i = n = 0; i < sizeof color_count / sizeof color_count[0]; ++i)
517 if (color_count[i])
519 fprintf (stderr, "%3d: %5d", i, color_count[i]);
520 ++n;
521 if (n % 5 == 0)
522 fputc ('\n', stderr);
523 else
524 fputc ('\t', stderr);
527 if (n % 5 != 0)
528 fputc ('\n', stderr);
529 return Qnil;
532 #endif /* DEBUG_X_COLORS */
535 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
536 color values. Interrupt input must be blocked when this function
537 is called. */
539 void
540 x_free_colors (struct frame *f, unsigned long *pixels, int npixels)
542 int class = FRAME_DISPLAY_INFO (f)->visual->class;
544 /* If display has an immutable color map, freeing colors is not
545 necessary and some servers don't allow it. So don't do it. */
546 if (class != StaticColor && class != StaticGray && class != TrueColor)
548 #ifdef DEBUG_X_COLORS
549 unregister_colors (pixels, npixels);
550 #endif
551 XFreeColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
552 pixels, npixels, 0);
557 #ifdef USE_X_TOOLKIT
559 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
560 color values. Interrupt input must be blocked when this function
561 is called. */
563 void
564 x_free_dpy_colors (Display *dpy, Screen *screen, Colormap cmap,
565 unsigned long *pixels, int npixels)
567 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
568 int class = dpyinfo->visual->class;
570 /* If display has an immutable color map, freeing colors is not
571 necessary and some servers don't allow it. So don't do it. */
572 if (class != StaticColor && class != StaticGray && class != TrueColor)
574 #ifdef DEBUG_X_COLORS
575 unregister_colors (pixels, npixels);
576 #endif
577 XFreeColors (dpy, cmap, pixels, npixels, 0);
580 #endif /* USE_X_TOOLKIT */
582 /* Create and return a GC for use on frame F. GC values and mask
583 are given by XGCV and MASK. */
585 static GC
586 x_create_gc (struct frame *f, unsigned long mask, XGCValues *xgcv)
588 GC gc;
589 block_input ();
590 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), mask, xgcv);
591 unblock_input ();
592 IF_DEBUG (++ngcs);
593 return gc;
597 /* Free GC which was used on frame F. */
599 static void
600 x_free_gc (struct frame *f, GC gc)
602 eassert (input_blocked_p ());
603 IF_DEBUG ((--ngcs, eassert (ngcs >= 0)));
604 XFreeGC (FRAME_X_DISPLAY (f), gc);
607 #endif /* HAVE_X_WINDOWS */
609 #ifdef HAVE_NTGUI
610 /* W32 emulation of GCs */
612 static GC
613 x_create_gc (struct frame *f, unsigned long mask, XGCValues *xgcv)
615 GC gc;
616 block_input ();
617 gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, xgcv);
618 unblock_input ();
619 IF_DEBUG (++ngcs);
620 return gc;
624 /* Free GC which was used on frame F. */
626 static void
627 x_free_gc (struct frame *f, GC gc)
629 IF_DEBUG ((--ngcs, eassert (ngcs >= 0)));
630 xfree (gc);
633 #endif /* HAVE_NTGUI */
635 #ifdef HAVE_NS
636 /* NS emulation of GCs */
638 static GC
639 x_create_gc (struct frame *f,
640 unsigned long mask,
641 XGCValues *xgcv)
643 GC gc = xmalloc (sizeof *gc);
644 *gc = *xgcv;
645 return gc;
648 static void
649 x_free_gc (struct frame *f, GC gc)
651 xfree (gc);
653 #endif /* HAVE_NS */
655 /***********************************************************************
656 Frames and faces
657 ***********************************************************************/
659 /* Initialize face cache and basic faces for frame F. */
661 void
662 init_frame_faces (struct frame *f)
664 /* Make a face cache, if F doesn't have one. */
665 if (FRAME_FACE_CACHE (f) == NULL)
666 FRAME_FACE_CACHE (f) = make_face_cache (f);
668 #ifdef HAVE_WINDOW_SYSTEM
669 /* Make the image cache. */
670 if (FRAME_WINDOW_P (f))
672 /* We initialize the image cache when creating the first frame
673 on a terminal, and not during terminal creation. This way,
674 `x-open-connection' on a tty won't create an image cache. */
675 if (FRAME_IMAGE_CACHE (f) == NULL)
676 FRAME_IMAGE_CACHE (f) = make_image_cache ();
677 ++FRAME_IMAGE_CACHE (f)->refcount;
679 #endif /* HAVE_WINDOW_SYSTEM */
681 /* Realize basic faces. Must have enough information in frame
682 parameters to realize basic faces at this point. */
683 #ifdef HAVE_X_WINDOWS
684 if (!FRAME_X_P (f) || FRAME_X_WINDOW (f))
685 #endif
686 #ifdef HAVE_NTGUI
687 if (!FRAME_WINDOW_P (f) || FRAME_W32_WINDOW (f))
688 #endif
689 #ifdef HAVE_NS
690 if (!FRAME_NS_P (f) || FRAME_NS_WINDOW (f))
691 #endif
692 if (!realize_basic_faces (f))
693 emacs_abort ();
697 /* Free face cache of frame F. Called from delete_frame. */
699 void
700 free_frame_faces (struct frame *f)
702 struct face_cache *face_cache = FRAME_FACE_CACHE (f);
704 if (face_cache)
706 free_face_cache (face_cache);
707 FRAME_FACE_CACHE (f) = NULL;
710 #ifdef HAVE_WINDOW_SYSTEM
711 if (FRAME_WINDOW_P (f))
713 struct image_cache *image_cache = FRAME_IMAGE_CACHE (f);
714 if (image_cache)
716 --image_cache->refcount;
717 if (image_cache->refcount == 0)
718 free_image_cache (f);
721 #endif /* HAVE_WINDOW_SYSTEM */
725 /* Clear face caches, and recompute basic faces for frame F. Call
726 this after changing frame parameters on which those faces depend,
727 or when realized faces have been freed due to changing attributes
728 of named faces. */
730 void
731 recompute_basic_faces (struct frame *f)
733 if (FRAME_FACE_CACHE (f))
735 clear_face_cache (0);
736 if (!realize_basic_faces (f))
737 emacs_abort ();
742 /* Clear the face caches of all frames. CLEAR_FONTS_P non-zero means
743 try to free unused fonts, too. */
745 void
746 clear_face_cache (int clear_fonts_p)
748 #ifdef HAVE_WINDOW_SYSTEM
749 Lisp_Object tail, frame;
751 if (clear_fonts_p
752 || ++clear_font_table_count == CLEAR_FONT_TABLE_COUNT)
754 /* From time to time see if we can unload some fonts. This also
755 frees all realized faces on all frames. Fonts needed by
756 faces will be loaded again when faces are realized again. */
757 clear_font_table_count = 0;
759 FOR_EACH_FRAME (tail, frame)
761 struct frame *f = XFRAME (frame);
762 if (FRAME_WINDOW_P (f)
763 && FRAME_DISPLAY_INFO (f)->n_fonts > CLEAR_FONT_TABLE_NFONTS)
765 clear_font_cache (f);
766 free_all_realized_faces (frame);
770 else
772 /* Clear GCs of realized faces. */
773 FOR_EACH_FRAME (tail, frame)
775 struct frame *f = XFRAME (frame);
776 if (FRAME_WINDOW_P (f))
777 clear_face_gcs (FRAME_FACE_CACHE (f));
779 clear_image_caches (Qnil);
781 #endif /* HAVE_WINDOW_SYSTEM */
785 DEFUN ("clear-face-cache", Fclear_face_cache, Sclear_face_cache, 0, 1, 0,
786 doc: /* Clear face caches on all frames.
787 Optional THOROUGHLY non-nil means try to free unused fonts, too. */)
788 (Lisp_Object thoroughly)
790 clear_face_cache (!NILP (thoroughly));
791 ++face_change_count;
792 windows_or_buffers_changed = 53;
793 return Qnil;
797 /***********************************************************************
798 X Pixmaps
799 ***********************************************************************/
801 #ifdef HAVE_WINDOW_SYSTEM
803 DEFUN ("bitmap-spec-p", Fbitmap_spec_p, Sbitmap_spec_p, 1, 1, 0,
804 doc: /* Value is non-nil if OBJECT is a valid bitmap specification.
805 A bitmap specification is either a string, a file name, or a list
806 \(WIDTH HEIGHT DATA) where WIDTH is the pixel width of the bitmap,
807 HEIGHT is its height, and DATA is a string containing the bits of
808 the pixmap. Bits are stored row by row, each row occupies
809 \(WIDTH + 7)/8 bytes. */)
810 (Lisp_Object object)
812 bool pixmap_p = 0;
814 if (STRINGP (object))
815 /* If OBJECT is a string, it's a file name. */
816 pixmap_p = 1;
817 else if (CONSP (object))
819 /* Otherwise OBJECT must be (WIDTH HEIGHT DATA), WIDTH and
820 HEIGHT must be ints > 0, and DATA must be string large
821 enough to hold a bitmap of the specified size. */
822 Lisp_Object width, height, data;
824 height = width = data = Qnil;
826 if (CONSP (object))
828 width = XCAR (object);
829 object = XCDR (object);
830 if (CONSP (object))
832 height = XCAR (object);
833 object = XCDR (object);
834 if (CONSP (object))
835 data = XCAR (object);
839 if (STRINGP (data)
840 && RANGED_INTEGERP (1, width, INT_MAX)
841 && RANGED_INTEGERP (1, height, INT_MAX))
843 int bytes_per_row = ((XINT (width) + BITS_PER_CHAR - 1)
844 / BITS_PER_CHAR);
845 if (XINT (height) <= SBYTES (data) / bytes_per_row)
846 pixmap_p = 1;
850 return pixmap_p ? Qt : Qnil;
854 /* Load a bitmap according to NAME (which is either a file name or a
855 pixmap spec) for use on frame F. Value is the bitmap_id (see
856 xfns.c). If NAME is nil, return with a bitmap id of zero. If
857 bitmap cannot be loaded, display a message saying so, and return
858 zero. */
860 static ptrdiff_t
861 load_pixmap (struct frame *f, Lisp_Object name)
863 ptrdiff_t bitmap_id;
865 if (NILP (name))
866 return 0;
868 CHECK_TYPE (!NILP (Fbitmap_spec_p (name)), Qbitmap_spec_p, name);
870 block_input ();
871 if (CONSP (name))
873 /* Decode a bitmap spec into a bitmap. */
875 int h, w;
876 Lisp_Object bits;
878 w = XINT (Fcar (name));
879 h = XINT (Fcar (Fcdr (name)));
880 bits = Fcar (Fcdr (Fcdr (name)));
882 bitmap_id = x_create_bitmap_from_data (f, SSDATA (bits),
883 w, h);
885 else
887 /* It must be a string -- a file name. */
888 bitmap_id = x_create_bitmap_from_file (f, name);
890 unblock_input ();
892 if (bitmap_id < 0)
894 add_to_log ("Invalid or undefined bitmap `%s'", name, Qnil);
895 bitmap_id = 0;
897 else
899 #ifdef GLYPH_DEBUG
900 ++npixmaps_allocated;
901 #endif
904 return bitmap_id;
907 #endif /* HAVE_WINDOW_SYSTEM */
911 /***********************************************************************
912 X Colors
913 ***********************************************************************/
915 /* Parse RGB_LIST, and fill in the RGB fields of COLOR.
916 RGB_LIST should contain (at least) 3 lisp integers.
917 Return 0 if there's a problem with RGB_LIST, otherwise return 1. */
919 static int
920 parse_rgb_list (Lisp_Object rgb_list, XColor *color)
922 #define PARSE_RGB_LIST_FIELD(field) \
923 if (CONSP (rgb_list) && INTEGERP (XCAR (rgb_list))) \
925 color->field = XINT (XCAR (rgb_list)); \
926 rgb_list = XCDR (rgb_list); \
928 else \
929 return 0;
931 PARSE_RGB_LIST_FIELD (red);
932 PARSE_RGB_LIST_FIELD (green);
933 PARSE_RGB_LIST_FIELD (blue);
935 return 1;
939 /* Lookup on frame F the color described by the lisp string COLOR.
940 The resulting tty color is returned in TTY_COLOR; if STD_COLOR is
941 non-zero, then the `standard' definition of the same color is
942 returned in it. */
944 static bool
945 tty_lookup_color (struct frame *f, Lisp_Object color, XColor *tty_color,
946 XColor *std_color)
948 Lisp_Object frame, color_desc;
950 if (!STRINGP (color) || NILP (Ffboundp (Qtty_color_desc)))
951 return 0;
953 XSETFRAME (frame, f);
955 color_desc = call2 (Qtty_color_desc, color, frame);
956 if (CONSP (color_desc) && CONSP (XCDR (color_desc)))
958 Lisp_Object rgb;
960 if (! INTEGERP (XCAR (XCDR (color_desc))))
961 return 0;
963 tty_color->pixel = XINT (XCAR (XCDR (color_desc)));
965 rgb = XCDR (XCDR (color_desc));
966 if (! parse_rgb_list (rgb, tty_color))
967 return 0;
969 /* Should we fill in STD_COLOR too? */
970 if (std_color)
972 /* Default STD_COLOR to the same as TTY_COLOR. */
973 *std_color = *tty_color;
975 /* Do a quick check to see if the returned descriptor is
976 actually _exactly_ equal to COLOR, otherwise we have to
977 lookup STD_COLOR separately. If it's impossible to lookup
978 a standard color, we just give up and use TTY_COLOR. */
979 if ((!STRINGP (XCAR (color_desc))
980 || NILP (Fstring_equal (color, XCAR (color_desc))))
981 && !NILP (Ffboundp (Qtty_color_standard_values)))
983 /* Look up STD_COLOR separately. */
984 rgb = call1 (Qtty_color_standard_values, color);
985 if (! parse_rgb_list (rgb, std_color))
986 return 0;
990 return 1;
992 else if (NILP (Fsymbol_value (intern ("tty-defined-color-alist"))))
993 /* We were called early during startup, and the colors are not
994 yet set up in tty-defined-color-alist. Don't return a failure
995 indication, since this produces the annoying "Unable to
996 load color" messages in the *Messages* buffer. */
997 return 1;
998 else
999 /* tty-color-desc seems to have returned a bad value. */
1000 return 0;
1003 /* A version of defined_color for non-X frames. */
1005 static bool
1006 tty_defined_color (struct frame *f, const char *color_name,
1007 XColor *color_def, bool alloc)
1009 bool status = 1;
1011 /* Defaults. */
1012 color_def->pixel = FACE_TTY_DEFAULT_COLOR;
1013 color_def->red = 0;
1014 color_def->blue = 0;
1015 color_def->green = 0;
1017 if (*color_name)
1018 status = tty_lookup_color (f, build_string (color_name), color_def, NULL);
1020 if (color_def->pixel == FACE_TTY_DEFAULT_COLOR && *color_name)
1022 if (strcmp (color_name, "unspecified-fg") == 0)
1023 color_def->pixel = FACE_TTY_DEFAULT_FG_COLOR;
1024 else if (strcmp (color_name, "unspecified-bg") == 0)
1025 color_def->pixel = FACE_TTY_DEFAULT_BG_COLOR;
1028 if (color_def->pixel != FACE_TTY_DEFAULT_COLOR)
1029 status = 1;
1031 return status;
1035 /* Decide if color named COLOR_NAME is valid for the display
1036 associated with the frame F; if so, return the rgb values in
1037 COLOR_DEF. If ALLOC, allocate a new colormap cell.
1039 This does the right thing for any type of frame. */
1041 static bool
1042 defined_color (struct frame *f, const char *color_name, XColor *color_def,
1043 bool alloc)
1045 if (!FRAME_WINDOW_P (f))
1046 return tty_defined_color (f, color_name, color_def, alloc);
1047 #ifdef HAVE_X_WINDOWS
1048 else if (FRAME_X_P (f))
1049 return x_defined_color (f, color_name, color_def, alloc);
1050 #endif
1051 #ifdef HAVE_NTGUI
1052 else if (FRAME_W32_P (f))
1053 return w32_defined_color (f, color_name, color_def, alloc);
1054 #endif
1055 #ifdef HAVE_NS
1056 else if (FRAME_NS_P (f))
1057 return ns_defined_color (f, color_name, color_def, alloc, 1);
1058 #endif
1059 else
1060 emacs_abort ();
1064 /* Given the index IDX of a tty color on frame F, return its name, a
1065 Lisp string. */
1067 Lisp_Object
1068 tty_color_name (struct frame *f, int idx)
1070 if (idx >= 0 && !NILP (Ffboundp (Qtty_color_by_index)))
1072 Lisp_Object frame;
1073 Lisp_Object coldesc;
1075 XSETFRAME (frame, f);
1076 coldesc = call2 (Qtty_color_by_index, make_number (idx), frame);
1078 if (!NILP (coldesc))
1079 return XCAR (coldesc);
1081 #ifdef MSDOS
1082 /* We can have an MSDOG frame under -nw for a short window of
1083 opportunity before internal_terminal_init is called. DTRT. */
1084 if (FRAME_MSDOS_P (f) && !inhibit_window_system)
1085 return msdos_stdcolor_name (idx);
1086 #endif
1088 if (idx == FACE_TTY_DEFAULT_FG_COLOR)
1089 return build_string (unspecified_fg);
1090 if (idx == FACE_TTY_DEFAULT_BG_COLOR)
1091 return build_string (unspecified_bg);
1093 return Qunspecified;
1097 /* Return non-zero if COLOR_NAME is a shade of gray (or white or
1098 black) on frame F.
1100 The criterion implemented here is not a terribly sophisticated one. */
1102 static int
1103 face_color_gray_p (struct frame *f, const char *color_name)
1105 XColor color;
1106 int gray_p;
1108 if (defined_color (f, color_name, &color, 0))
1109 gray_p = (/* Any color sufficiently close to black counts as gray. */
1110 (color.red < 5000 && color.green < 5000 && color.blue < 5000)
1112 ((eabs (color.red - color.green)
1113 < max (color.red, color.green) / 20)
1114 && (eabs (color.green - color.blue)
1115 < max (color.green, color.blue) / 20)
1116 && (eabs (color.blue - color.red)
1117 < max (color.blue, color.red) / 20)));
1118 else
1119 gray_p = 0;
1121 return gray_p;
1125 /* Return non-zero if color COLOR_NAME can be displayed on frame F.
1126 BACKGROUND_P non-zero means the color will be used as background
1127 color. */
1129 static int
1130 face_color_supported_p (struct frame *f, const char *color_name,
1131 int background_p)
1133 Lisp_Object frame;
1134 XColor not_used;
1136 XSETFRAME (frame, f);
1137 return
1138 #ifdef HAVE_WINDOW_SYSTEM
1139 FRAME_WINDOW_P (f)
1140 ? (!NILP (Fxw_display_color_p (frame))
1141 || xstrcasecmp (color_name, "black") == 0
1142 || xstrcasecmp (color_name, "white") == 0
1143 || (background_p
1144 && face_color_gray_p (f, color_name))
1145 || (!NILP (Fx_display_grayscale_p (frame))
1146 && face_color_gray_p (f, color_name)))
1148 #endif
1149 tty_defined_color (f, color_name, &not_used, 0);
1153 DEFUN ("color-gray-p", Fcolor_gray_p, Scolor_gray_p, 1, 2, 0,
1154 doc: /* Return non-nil if COLOR is a shade of gray (or white or black).
1155 FRAME specifies the frame and thus the display for interpreting COLOR.
1156 If FRAME is nil or omitted, use the selected frame. */)
1157 (Lisp_Object color, Lisp_Object frame)
1159 CHECK_STRING (color);
1160 return (face_color_gray_p (decode_any_frame (frame), SSDATA (color))
1161 ? Qt : Qnil);
1165 DEFUN ("color-supported-p", Fcolor_supported_p,
1166 Scolor_supported_p, 1, 3, 0,
1167 doc: /* Return non-nil if COLOR can be displayed on FRAME.
1168 BACKGROUND-P non-nil means COLOR is used as a background.
1169 Otherwise, this function tells whether it can be used as a foreground.
1170 If FRAME is nil or omitted, use the selected frame.
1171 COLOR must be a valid color name. */)
1172 (Lisp_Object color, Lisp_Object frame, Lisp_Object background_p)
1174 CHECK_STRING (color);
1175 return (face_color_supported_p (decode_any_frame (frame),
1176 SSDATA (color), !NILP (background_p))
1177 ? Qt : Qnil);
1181 static unsigned long
1182 load_color2 (struct frame *f, struct face *face, Lisp_Object name,
1183 enum lface_attribute_index target_index, XColor *color)
1185 eassert (STRINGP (name));
1186 eassert (target_index == LFACE_FOREGROUND_INDEX
1187 || target_index == LFACE_BACKGROUND_INDEX
1188 || target_index == LFACE_UNDERLINE_INDEX
1189 || target_index == LFACE_OVERLINE_INDEX
1190 || target_index == LFACE_STRIKE_THROUGH_INDEX
1191 || target_index == LFACE_BOX_INDEX);
1193 /* if the color map is full, defined_color will return a best match
1194 to the values in an existing cell. */
1195 if (!defined_color (f, SSDATA (name), color, 1))
1197 add_to_log ("Unable to load color \"%s\"", name, Qnil);
1199 switch (target_index)
1201 case LFACE_FOREGROUND_INDEX:
1202 face->foreground_defaulted_p = 1;
1203 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1204 break;
1206 case LFACE_BACKGROUND_INDEX:
1207 face->background_defaulted_p = 1;
1208 color->pixel = FRAME_BACKGROUND_PIXEL (f);
1209 break;
1211 case LFACE_UNDERLINE_INDEX:
1212 face->underline_defaulted_p = 1;
1213 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1214 break;
1216 case LFACE_OVERLINE_INDEX:
1217 face->overline_color_defaulted_p = 1;
1218 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1219 break;
1221 case LFACE_STRIKE_THROUGH_INDEX:
1222 face->strike_through_color_defaulted_p = 1;
1223 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1224 break;
1226 case LFACE_BOX_INDEX:
1227 face->box_color_defaulted_p = 1;
1228 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1229 break;
1231 default:
1232 emacs_abort ();
1235 #ifdef GLYPH_DEBUG
1236 else
1237 ++ncolors_allocated;
1238 #endif
1240 return color->pixel;
1243 /* Load color with name NAME for use by face FACE on frame F.
1244 TARGET_INDEX must be one of LFACE_FOREGROUND_INDEX,
1245 LFACE_BACKGROUND_INDEX, LFACE_UNDERLINE_INDEX, LFACE_OVERLINE_INDEX,
1246 LFACE_STRIKE_THROUGH_INDEX, or LFACE_BOX_INDEX. Value is the
1247 pixel color. If color cannot be loaded, display a message, and
1248 return the foreground, background or underline color of F, but
1249 record that fact in flags of the face so that we don't try to free
1250 these colors. */
1252 #ifndef MSDOS
1253 static
1254 #endif
1255 unsigned long
1256 load_color (struct frame *f, struct face *face, Lisp_Object name,
1257 enum lface_attribute_index target_index)
1259 XColor color;
1260 return load_color2 (f, face, name, target_index, &color);
1264 #ifdef HAVE_WINDOW_SYSTEM
1266 #define NEAR_SAME_COLOR_THRESHOLD 30000
1268 /* Load colors for face FACE which is used on frame F. Colors are
1269 specified by slots LFACE_BACKGROUND_INDEX and LFACE_FOREGROUND_INDEX
1270 of ATTRS. If the background color specified is not supported on F,
1271 try to emulate gray colors with a stipple from Vface_default_stipple. */
1273 static void
1274 load_face_colors (struct frame *f, struct face *face,
1275 Lisp_Object attrs[LFACE_VECTOR_SIZE])
1277 Lisp_Object fg, bg, dfg;
1278 XColor xfg, xbg;
1280 bg = attrs[LFACE_BACKGROUND_INDEX];
1281 fg = attrs[LFACE_FOREGROUND_INDEX];
1283 /* Swap colors if face is inverse-video. */
1284 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1286 Lisp_Object tmp;
1287 tmp = fg;
1288 fg = bg;
1289 bg = tmp;
1292 /* Check for support for foreground, not for background because
1293 face_color_supported_p is smart enough to know that grays are
1294 "supported" as background because we are supposed to use stipple
1295 for them. */
1296 if (!face_color_supported_p (f, SSDATA (bg), 0)
1297 && !NILP (Fbitmap_spec_p (Vface_default_stipple)))
1299 x_destroy_bitmap (f, face->stipple);
1300 face->stipple = load_pixmap (f, Vface_default_stipple);
1303 face->background = load_color2 (f, face, bg, LFACE_BACKGROUND_INDEX, &xbg);
1304 face->foreground = load_color2 (f, face, fg, LFACE_FOREGROUND_INDEX, &xfg);
1306 dfg = attrs[LFACE_DISTANT_FOREGROUND_INDEX];
1307 if (!NILP (dfg) && !UNSPECIFIEDP (dfg)
1308 && color_distance (&xbg, &xfg) < NEAR_SAME_COLOR_THRESHOLD)
1310 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1311 face->background = load_color (f, face, dfg, LFACE_BACKGROUND_INDEX);
1312 else
1313 face->foreground = load_color (f, face, dfg, LFACE_FOREGROUND_INDEX);
1317 #ifdef HAVE_X_WINDOWS
1319 /* Free color PIXEL on frame F. */
1321 void
1322 unload_color (struct frame *f, unsigned long pixel)
1324 if (pixel != -1)
1326 block_input ();
1327 x_free_colors (f, &pixel, 1);
1328 unblock_input ();
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? */
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 ();
1387 #endif /* HAVE_X_WINDOWS */
1389 #endif /* HAVE_WINDOW_SYSTEM */
1393 /***********************************************************************
1394 XLFD Font Names
1395 ***********************************************************************/
1397 /* An enumerator for each field of an XLFD font name. */
1399 enum xlfd_field
1401 XLFD_FOUNDRY,
1402 XLFD_FAMILY,
1403 XLFD_WEIGHT,
1404 XLFD_SLANT,
1405 XLFD_SWIDTH,
1406 XLFD_ADSTYLE,
1407 XLFD_PIXEL_SIZE,
1408 XLFD_POINT_SIZE,
1409 XLFD_RESX,
1410 XLFD_RESY,
1411 XLFD_SPACING,
1412 XLFD_AVGWIDTH,
1413 XLFD_REGISTRY,
1414 XLFD_ENCODING,
1415 XLFD_LAST
1418 /* An enumerator for each possible slant value of a font. Taken from
1419 the XLFD specification. */
1421 enum xlfd_slant
1423 XLFD_SLANT_UNKNOWN,
1424 XLFD_SLANT_ROMAN,
1425 XLFD_SLANT_ITALIC,
1426 XLFD_SLANT_OBLIQUE,
1427 XLFD_SLANT_REVERSE_ITALIC,
1428 XLFD_SLANT_REVERSE_OBLIQUE,
1429 XLFD_SLANT_OTHER
1432 /* Relative font weight according to XLFD documentation. */
1434 enum xlfd_weight
1436 XLFD_WEIGHT_UNKNOWN,
1437 XLFD_WEIGHT_ULTRA_LIGHT, /* 10 */
1438 XLFD_WEIGHT_EXTRA_LIGHT, /* 20 */
1439 XLFD_WEIGHT_LIGHT, /* 30 */
1440 XLFD_WEIGHT_SEMI_LIGHT, /* 40: SemiLight, Book, ... */
1441 XLFD_WEIGHT_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1442 XLFD_WEIGHT_SEMI_BOLD, /* 60: SemiBold, DemiBold, ... */
1443 XLFD_WEIGHT_BOLD, /* 70: Bold, ... */
1444 XLFD_WEIGHT_EXTRA_BOLD, /* 80: ExtraBold, Heavy, ... */
1445 XLFD_WEIGHT_ULTRA_BOLD /* 90: UltraBold, Black, ... */
1448 /* Relative proportionate width. */
1450 enum xlfd_swidth
1452 XLFD_SWIDTH_UNKNOWN,
1453 XLFD_SWIDTH_ULTRA_CONDENSED, /* 10 */
1454 XLFD_SWIDTH_EXTRA_CONDENSED, /* 20 */
1455 XLFD_SWIDTH_CONDENSED, /* 30: Condensed, Narrow, Compressed, ... */
1456 XLFD_SWIDTH_SEMI_CONDENSED, /* 40: semicondensed */
1457 XLFD_SWIDTH_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1458 XLFD_SWIDTH_SEMI_EXPANDED, /* 60: SemiExpanded, DemiExpanded, ... */
1459 XLFD_SWIDTH_EXPANDED, /* 70: Expanded... */
1460 XLFD_SWIDTH_EXTRA_EXPANDED, /* 80: ExtraExpanded, Wide... */
1461 XLFD_SWIDTH_ULTRA_EXPANDED /* 90: UltraExpanded... */
1464 /* Order by which font selection chooses fonts. The default values
1465 mean `first, find a best match for the font width, then for the
1466 font height, then for weight, then for slant.' This variable can be
1467 set via set-face-font-sort-order. */
1469 static int font_sort_order[4];
1471 #ifdef HAVE_WINDOW_SYSTEM
1473 static enum font_property_index font_props_for_sorting[FONT_SIZE_INDEX];
1475 static int
1476 compare_fonts_by_sort_order (const void *v1, const void *v2)
1478 Lisp_Object const *p1 = v1;
1479 Lisp_Object const *p2 = v2;
1480 Lisp_Object font1 = *p1;
1481 Lisp_Object font2 = *p2;
1482 int i;
1484 for (i = 0; i < FONT_SIZE_INDEX; i++)
1486 enum font_property_index idx = font_props_for_sorting[i];
1487 Lisp_Object val1 = AREF (font1, idx), val2 = AREF (font2, idx);
1488 int result;
1490 if (idx <= FONT_REGISTRY_INDEX)
1492 if (STRINGP (val1))
1493 result = STRINGP (val2) ? strcmp (SSDATA (val1), SSDATA (val2)) : -1;
1494 else
1495 result = STRINGP (val2) ? 1 : 0;
1497 else
1499 if (INTEGERP (val1))
1500 result = (INTEGERP (val2) && XINT (val1) >= XINT (val2)
1501 ? XINT (val1) > XINT (val2)
1502 : -1);
1503 else
1504 result = INTEGERP (val2) ? 1 : 0;
1506 if (result)
1507 return result;
1509 return 0;
1512 DEFUN ("x-family-fonts", Fx_family_fonts, Sx_family_fonts, 0, 2, 0,
1513 doc: /* Return a list of available fonts of family FAMILY on FRAME.
1514 If FAMILY is omitted or nil, list all families.
1515 Otherwise, FAMILY must be a string, possibly containing wildcards
1516 `?' and `*'.
1517 If FRAME is omitted or nil, use the selected frame.
1518 Each element of the result is a vector [FAMILY WIDTH POINT-SIZE WEIGHT
1519 SLANT FIXED-P FULL REGISTRY-AND-ENCODING].
1520 FAMILY is the font family name. POINT-SIZE is the size of the
1521 font in 1/10 pt. WIDTH, WEIGHT, and SLANT are symbols describing the
1522 width, weight and slant of the font. These symbols are the same as for
1523 face attributes. FIXED-P is non-nil if the font is fixed-pitch.
1524 FULL is the full name of the font, and REGISTRY-AND-ENCODING is a string
1525 giving the registry and encoding of the font.
1526 The result list is sorted according to the current setting of
1527 the face font sort order. */)
1528 (Lisp_Object family, Lisp_Object frame)
1530 Lisp_Object font_spec, list, *drivers, vec;
1531 struct frame *f = decode_live_frame (frame);
1532 ptrdiff_t i, nfonts;
1533 EMACS_INT ndrivers;
1534 Lisp_Object result;
1535 USE_SAFE_ALLOCA;
1537 font_spec = Ffont_spec (0, NULL);
1538 if (!NILP (family))
1540 CHECK_STRING (family);
1541 font_parse_family_registry (family, Qnil, font_spec);
1544 list = font_list_entities (f, font_spec);
1545 if (NILP (list))
1546 return Qnil;
1548 /* Sort the font entities. */
1549 for (i = 0; i < 4; i++)
1550 switch (font_sort_order[i])
1552 case XLFD_SWIDTH:
1553 font_props_for_sorting[i] = FONT_WIDTH_INDEX; break;
1554 case XLFD_POINT_SIZE:
1555 font_props_for_sorting[i] = FONT_SIZE_INDEX; break;
1556 case XLFD_WEIGHT:
1557 font_props_for_sorting[i] = FONT_WEIGHT_INDEX; break;
1558 default:
1559 font_props_for_sorting[i] = FONT_SLANT_INDEX; break;
1561 font_props_for_sorting[i++] = FONT_FAMILY_INDEX;
1562 font_props_for_sorting[i++] = FONT_FOUNDRY_INDEX;
1563 font_props_for_sorting[i++] = FONT_ADSTYLE_INDEX;
1564 font_props_for_sorting[i++] = FONT_REGISTRY_INDEX;
1566 ndrivers = XINT (Flength (list));
1567 SAFE_ALLOCA_LISP (drivers, ndrivers);
1568 for (i = 0; i < ndrivers; i++, list = XCDR (list))
1569 drivers[i] = XCAR (list);
1570 vec = Fvconcat (ndrivers, drivers);
1571 nfonts = ASIZE (vec);
1573 qsort (XVECTOR (vec)->contents, nfonts, word_size,
1574 compare_fonts_by_sort_order);
1576 result = Qnil;
1577 for (i = nfonts - 1; i >= 0; --i)
1579 Lisp_Object font = AREF (vec, i);
1580 Lisp_Object v = make_uninit_vector (8);
1581 int point;
1582 Lisp_Object spacing;
1584 ASET (v, 0, AREF (font, FONT_FAMILY_INDEX));
1585 ASET (v, 1, FONT_WIDTH_SYMBOLIC (font));
1586 point = PIXEL_TO_POINT (XINT (AREF (font, FONT_SIZE_INDEX)) * 10,
1587 FRAME_RES_Y (f));
1588 ASET (v, 2, make_number (point));
1589 ASET (v, 3, FONT_WEIGHT_SYMBOLIC (font));
1590 ASET (v, 4, FONT_SLANT_SYMBOLIC (font));
1591 spacing = Ffont_get (font, QCspacing);
1592 ASET (v, 5, (NILP (spacing) || EQ (spacing, Qp)) ? Qnil : Qt);
1593 ASET (v, 6, Ffont_xlfd_name (font, Qnil));
1594 ASET (v, 7, AREF (font, FONT_REGISTRY_INDEX));
1596 result = Fcons (v, result);
1599 SAFE_FREE ();
1600 return result;
1603 DEFUN ("x-list-fonts", Fx_list_fonts, Sx_list_fonts, 1, 5, 0,
1604 doc: /* Return a list of the names of available fonts matching PATTERN.
1605 If optional arguments FACE and FRAME are specified, return only fonts
1606 the same size as FACE on FRAME.
1608 PATTERN should be a string containing a font name in the XLFD,
1609 Fontconfig, or GTK format. A font name given in the XLFD format may
1610 contain wildcard characters:
1611 the * character matches any substring, and
1612 the ? character matches any single character.
1613 PATTERN is case-insensitive.
1615 The return value is a list of strings, suitable as arguments to
1616 `set-face-font'.
1618 Fonts Emacs can't use may or may not be excluded
1619 even if they match PATTERN and FACE.
1620 The optional fourth argument MAXIMUM sets a limit on how many
1621 fonts to match. The first MAXIMUM fonts are reported.
1622 The optional fifth argument WIDTH, if specified, is a number of columns
1623 occupied by a character of a font. In that case, return only fonts
1624 the WIDTH times as wide as FACE on FRAME. */)
1625 (Lisp_Object pattern, Lisp_Object face, Lisp_Object frame,
1626 Lisp_Object maximum, Lisp_Object width)
1628 struct frame *f;
1629 int size, avgwidth IF_LINT (= 0);
1631 check_window_system (NULL);
1632 CHECK_STRING (pattern);
1634 if (! NILP (maximum))
1635 CHECK_NATNUM (maximum);
1637 if (!NILP (width))
1638 CHECK_NUMBER (width);
1640 /* We can't simply call decode_window_system_frame because
1641 this function may be called before any frame is created. */
1642 f = decode_live_frame (frame);
1643 if (! FRAME_WINDOW_P (f))
1645 /* Perhaps we have not yet created any frame. */
1646 f = NULL;
1647 frame = Qnil;
1648 face = Qnil;
1650 else
1651 XSETFRAME (frame, f);
1653 /* Determine the width standard for comparison with the fonts we find. */
1655 if (NILP (face))
1656 size = 0;
1657 else
1659 /* This is of limited utility since it works with character
1660 widths. Keep it for compatibility. --gerd. */
1661 int face_id = lookup_named_face (f, face, 0);
1662 struct face *width_face = (face_id < 0
1663 ? NULL
1664 : FACE_FROM_ID (f, face_id));
1666 if (width_face && width_face->font)
1668 size = width_face->font->pixel_size;
1669 avgwidth = width_face->font->average_width;
1671 else
1673 size = FRAME_FONT (f)->pixel_size;
1674 avgwidth = FRAME_FONT (f)->average_width;
1676 if (!NILP (width))
1677 avgwidth *= XINT (width);
1681 Lisp_Object font_spec;
1682 Lisp_Object args[2], tail;
1684 font_spec = font_spec_from_name (pattern);
1685 if (!FONTP (font_spec))
1686 signal_error ("Invalid font name", pattern);
1688 if (size)
1690 Ffont_put (font_spec, QCsize, make_number (size));
1691 Ffont_put (font_spec, QCavgwidth, make_number (avgwidth));
1693 args[0] = Flist_fonts (font_spec, frame, maximum, font_spec);
1694 for (tail = args[0]; CONSP (tail); tail = XCDR (tail))
1696 Lisp_Object font_entity;
1698 font_entity = XCAR (tail);
1699 if ((NILP (AREF (font_entity, FONT_SIZE_INDEX))
1700 || XINT (AREF (font_entity, FONT_SIZE_INDEX)) == 0)
1701 && ! NILP (AREF (font_spec, FONT_SIZE_INDEX)))
1703 /* This is a scalable font. For backward compatibility,
1704 we set the specified size. */
1705 font_entity = copy_font_spec (font_entity);
1706 ASET (font_entity, FONT_SIZE_INDEX,
1707 AREF (font_spec, FONT_SIZE_INDEX));
1709 XSETCAR (tail, Ffont_xlfd_name (font_entity, Qnil));
1711 if (NILP (frame))
1712 /* We don't have to check fontsets. */
1713 return args[0];
1714 args[1] = list_fontsets (f, pattern, size);
1715 return Fnconc (2, args);
1719 #endif /* HAVE_WINDOW_SYSTEM */
1722 /***********************************************************************
1723 Lisp Faces
1724 ***********************************************************************/
1726 /* Access face attributes of face LFACE, a Lisp vector. */
1728 #define LFACE_FAMILY(LFACE) AREF ((LFACE), LFACE_FAMILY_INDEX)
1729 #define LFACE_FOUNDRY(LFACE) AREF ((LFACE), LFACE_FOUNDRY_INDEX)
1730 #define LFACE_HEIGHT(LFACE) AREF ((LFACE), LFACE_HEIGHT_INDEX)
1731 #define LFACE_WEIGHT(LFACE) AREF ((LFACE), LFACE_WEIGHT_INDEX)
1732 #define LFACE_SLANT(LFACE) AREF ((LFACE), LFACE_SLANT_INDEX)
1733 #define LFACE_UNDERLINE(LFACE) AREF ((LFACE), LFACE_UNDERLINE_INDEX)
1734 #define LFACE_INVERSE(LFACE) AREF ((LFACE), LFACE_INVERSE_INDEX)
1735 #define LFACE_FOREGROUND(LFACE) AREF ((LFACE), LFACE_FOREGROUND_INDEX)
1736 #define LFACE_BACKGROUND(LFACE) AREF ((LFACE), LFACE_BACKGROUND_INDEX)
1737 #define LFACE_STIPPLE(LFACE) AREF ((LFACE), LFACE_STIPPLE_INDEX)
1738 #define LFACE_SWIDTH(LFACE) AREF ((LFACE), LFACE_SWIDTH_INDEX)
1739 #define LFACE_OVERLINE(LFACE) AREF ((LFACE), LFACE_OVERLINE_INDEX)
1740 #define LFACE_STRIKE_THROUGH(LFACE) AREF ((LFACE), LFACE_STRIKE_THROUGH_INDEX)
1741 #define LFACE_BOX(LFACE) AREF ((LFACE), LFACE_BOX_INDEX)
1742 #define LFACE_FONT(LFACE) AREF ((LFACE), LFACE_FONT_INDEX)
1743 #define LFACE_INHERIT(LFACE) AREF ((LFACE), LFACE_INHERIT_INDEX)
1744 #define LFACE_FONTSET(LFACE) AREF ((LFACE), LFACE_FONTSET_INDEX)
1745 #define LFACE_DISTANT_FOREGROUND(LFACE) \
1746 AREF ((LFACE), LFACE_DISTANT_FOREGROUND_INDEX)
1748 /* Non-zero if LFACE is a Lisp face. A Lisp face is a vector of size
1749 LFACE_VECTOR_SIZE which has the symbol `face' in slot 0. */
1751 #define LFACEP(LFACE) \
1752 (VECTORP (LFACE) \
1753 && ASIZE (LFACE) == LFACE_VECTOR_SIZE \
1754 && EQ (AREF (LFACE, 0), Qface))
1757 #ifdef GLYPH_DEBUG
1759 /* Check consistency of Lisp face attribute vector ATTRS. */
1761 static void
1762 check_lface_attrs (Lisp_Object attrs[LFACE_VECTOR_SIZE])
1764 eassert (UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
1765 || IGNORE_DEFFACE_P (attrs[LFACE_FAMILY_INDEX])
1766 || STRINGP (attrs[LFACE_FAMILY_INDEX]));
1767 eassert (UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
1768 || IGNORE_DEFFACE_P (attrs[LFACE_FOUNDRY_INDEX])
1769 || STRINGP (attrs[LFACE_FOUNDRY_INDEX]));
1770 eassert (UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
1771 || IGNORE_DEFFACE_P (attrs[LFACE_SWIDTH_INDEX])
1772 || SYMBOLP (attrs[LFACE_SWIDTH_INDEX]));
1773 eassert (UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
1774 || IGNORE_DEFFACE_P (attrs[LFACE_HEIGHT_INDEX])
1775 || INTEGERP (attrs[LFACE_HEIGHT_INDEX])
1776 || FLOATP (attrs[LFACE_HEIGHT_INDEX])
1777 || FUNCTIONP (attrs[LFACE_HEIGHT_INDEX]));
1778 eassert (UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
1779 || IGNORE_DEFFACE_P (attrs[LFACE_WEIGHT_INDEX])
1780 || SYMBOLP (attrs[LFACE_WEIGHT_INDEX]));
1781 eassert (UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
1782 || IGNORE_DEFFACE_P (attrs[LFACE_SLANT_INDEX])
1783 || SYMBOLP (attrs[LFACE_SLANT_INDEX]));
1784 eassert (UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
1785 || IGNORE_DEFFACE_P (attrs[LFACE_UNDERLINE_INDEX])
1786 || SYMBOLP (attrs[LFACE_UNDERLINE_INDEX])
1787 || STRINGP (attrs[LFACE_UNDERLINE_INDEX])
1788 || CONSP (attrs[LFACE_UNDERLINE_INDEX]));
1789 eassert (UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
1790 || IGNORE_DEFFACE_P (attrs[LFACE_OVERLINE_INDEX])
1791 || SYMBOLP (attrs[LFACE_OVERLINE_INDEX])
1792 || STRINGP (attrs[LFACE_OVERLINE_INDEX]));
1793 eassert (UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1794 || IGNORE_DEFFACE_P (attrs[LFACE_STRIKE_THROUGH_INDEX])
1795 || SYMBOLP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1796 || STRINGP (attrs[LFACE_STRIKE_THROUGH_INDEX]));
1797 eassert (UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
1798 || IGNORE_DEFFACE_P (attrs[LFACE_BOX_INDEX])
1799 || SYMBOLP (attrs[LFACE_BOX_INDEX])
1800 || STRINGP (attrs[LFACE_BOX_INDEX])
1801 || INTEGERP (attrs[LFACE_BOX_INDEX])
1802 || CONSP (attrs[LFACE_BOX_INDEX]));
1803 eassert (UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
1804 || IGNORE_DEFFACE_P (attrs[LFACE_INVERSE_INDEX])
1805 || SYMBOLP (attrs[LFACE_INVERSE_INDEX]));
1806 eassert (UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
1807 || IGNORE_DEFFACE_P (attrs[LFACE_FOREGROUND_INDEX])
1808 || STRINGP (attrs[LFACE_FOREGROUND_INDEX]));
1809 eassert (UNSPECIFIEDP (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
1810 || IGNORE_DEFFACE_P (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
1811 || STRINGP (attrs[LFACE_DISTANT_FOREGROUND_INDEX]));
1812 eassert (UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
1813 || IGNORE_DEFFACE_P (attrs[LFACE_BACKGROUND_INDEX])
1814 || STRINGP (attrs[LFACE_BACKGROUND_INDEX]));
1815 eassert (UNSPECIFIEDP (attrs[LFACE_INHERIT_INDEX])
1816 || IGNORE_DEFFACE_P (attrs[LFACE_INHERIT_INDEX])
1817 || NILP (attrs[LFACE_INHERIT_INDEX])
1818 || SYMBOLP (attrs[LFACE_INHERIT_INDEX])
1819 || CONSP (attrs[LFACE_INHERIT_INDEX]));
1820 #ifdef HAVE_WINDOW_SYSTEM
1821 eassert (UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
1822 || IGNORE_DEFFACE_P (attrs[LFACE_STIPPLE_INDEX])
1823 || SYMBOLP (attrs[LFACE_STIPPLE_INDEX])
1824 || !NILP (Fbitmap_spec_p (attrs[LFACE_STIPPLE_INDEX])));
1825 eassert (UNSPECIFIEDP (attrs[LFACE_FONT_INDEX])
1826 || IGNORE_DEFFACE_P (attrs[LFACE_FONT_INDEX])
1827 || FONTP (attrs[LFACE_FONT_INDEX]));
1828 eassert (UNSPECIFIEDP (attrs[LFACE_FONTSET_INDEX])
1829 || STRINGP (attrs[LFACE_FONTSET_INDEX])
1830 || NILP (attrs[LFACE_FONTSET_INDEX]));
1831 #endif
1835 /* Check consistency of attributes of Lisp face LFACE (a Lisp vector). */
1837 static void
1838 check_lface (Lisp_Object lface)
1840 if (!NILP (lface))
1842 eassert (LFACEP (lface));
1843 check_lface_attrs (XVECTOR (lface)->contents);
1847 #else /* not GLYPH_DEBUG */
1849 #define check_lface_attrs(attrs) (void) 0
1850 #define check_lface(lface) (void) 0
1852 #endif /* GLYPH_DEBUG */
1856 /* Face-merge cycle checking. */
1858 enum named_merge_point_kind
1860 NAMED_MERGE_POINT_NORMAL,
1861 NAMED_MERGE_POINT_REMAP
1864 /* A `named merge point' is simply a point during face-merging where we
1865 look up a face by name. We keep a stack of which named lookups we're
1866 currently processing so that we can easily detect cycles, using a
1867 linked- list of struct named_merge_point structures, typically
1868 allocated on the stack frame of the named lookup functions which are
1869 active (so no consing is required). */
1870 struct named_merge_point
1872 Lisp_Object face_name;
1873 enum named_merge_point_kind named_merge_point_kind;
1874 struct named_merge_point *prev;
1878 /* If a face merging cycle is detected for FACE_NAME, return 0,
1879 otherwise add NEW_NAMED_MERGE_POINT, which is initialized using
1880 FACE_NAME and NAMED_MERGE_POINT_KIND, as the head of the linked list
1881 pointed to by NAMED_MERGE_POINTS, and return 1. */
1883 static int
1884 push_named_merge_point (struct named_merge_point *new_named_merge_point,
1885 Lisp_Object face_name,
1886 enum named_merge_point_kind named_merge_point_kind,
1887 struct named_merge_point **named_merge_points)
1889 struct named_merge_point *prev;
1891 for (prev = *named_merge_points; prev; prev = prev->prev)
1892 if (EQ (face_name, prev->face_name))
1894 if (prev->named_merge_point_kind == named_merge_point_kind)
1895 /* A cycle, so fail. */
1896 return 0;
1897 else if (prev->named_merge_point_kind == NAMED_MERGE_POINT_REMAP)
1898 /* A remap `hides ' any previous normal merge points
1899 (because the remap means that it's actually different face),
1900 so as we know the current merge point must be normal, we
1901 can just assume it's OK. */
1902 break;
1905 new_named_merge_point->face_name = face_name;
1906 new_named_merge_point->named_merge_point_kind = named_merge_point_kind;
1907 new_named_merge_point->prev = *named_merge_points;
1909 *named_merge_points = new_named_merge_point;
1911 return 1;
1915 /* Resolve face name FACE_NAME. If FACE_NAME is a string, intern it
1916 to make it a symbol. If FACE_NAME is an alias for another face,
1917 return that face's name.
1919 Return default face in case of errors. */
1921 static Lisp_Object
1922 resolve_face_name (Lisp_Object face_name, int signal_p)
1924 Lisp_Object orig_face;
1925 Lisp_Object tortoise, hare;
1927 if (STRINGP (face_name))
1928 face_name = intern (SSDATA (face_name));
1930 if (NILP (face_name) || !SYMBOLP (face_name))
1931 return face_name;
1933 orig_face = face_name;
1934 tortoise = hare = face_name;
1936 while (1)
1938 face_name = hare;
1939 hare = Fget (hare, Qface_alias);
1940 if (NILP (hare) || !SYMBOLP (hare))
1941 break;
1943 face_name = hare;
1944 hare = Fget (hare, Qface_alias);
1945 if (NILP (hare) || !SYMBOLP (hare))
1946 break;
1948 tortoise = Fget (tortoise, Qface_alias);
1949 if (EQ (hare, tortoise))
1951 if (signal_p)
1952 xsignal1 (Qcircular_list, orig_face);
1953 return Qdefault;
1957 return face_name;
1961 /* Return the face definition of FACE_NAME on frame F. F null means
1962 return the definition for new frames. FACE_NAME may be a string or
1963 a symbol (apparently Emacs 20.2 allowed strings as face names in
1964 face text properties; Ediff uses that). If SIGNAL_P is non-zero,
1965 signal an error if FACE_NAME is not a valid face name. If SIGNAL_P
1966 is zero, value is nil if FACE_NAME is not a valid face name. */
1967 static Lisp_Object
1968 lface_from_face_name_no_resolve (struct frame *f, Lisp_Object face_name,
1969 int signal_p)
1971 Lisp_Object lface;
1973 if (f)
1974 lface = assq_no_quit (face_name, f->face_alist);
1975 else
1976 lface = assq_no_quit (face_name, Vface_new_frame_defaults);
1978 if (CONSP (lface))
1979 lface = XCDR (lface);
1980 else if (signal_p)
1981 signal_error ("Invalid face", face_name);
1983 check_lface (lface);
1985 return lface;
1988 /* Return the face definition of FACE_NAME on frame F. F null means
1989 return the definition for new frames. FACE_NAME may be a string or
1990 a symbol (apparently Emacs 20.2 allowed strings as face names in
1991 face text properties; Ediff uses that). If FACE_NAME is an alias
1992 for another face, return that face's definition. If SIGNAL_P is
1993 non-zero, signal an error if FACE_NAME is not a valid face name.
1994 If SIGNAL_P is zero, value is nil if FACE_NAME is not a valid face
1995 name. */
1996 static Lisp_Object
1997 lface_from_face_name (struct frame *f, Lisp_Object face_name, int signal_p)
1999 face_name = resolve_face_name (face_name, signal_p);
2000 return lface_from_face_name_no_resolve (f, face_name, signal_p);
2004 /* Get face attributes of face FACE_NAME from frame-local faces on
2005 frame F. Store the resulting attributes in ATTRS which must point
2006 to a vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If SIGNAL_P
2007 is non-zero, signal an error if FACE_NAME does not name a face.
2008 Otherwise, value is zero if FACE_NAME is not a face. */
2010 static int
2011 get_lface_attributes_no_remap (struct frame *f, Lisp_Object face_name,
2012 Lisp_Object attrs[LFACE_VECTOR_SIZE],
2013 int signal_p)
2015 Lisp_Object lface;
2017 lface = lface_from_face_name_no_resolve (f, face_name, signal_p);
2019 if (! NILP (lface))
2020 memcpy (attrs, XVECTOR (lface)->contents,
2021 LFACE_VECTOR_SIZE * sizeof *attrs);
2023 return !NILP (lface);
2026 /* Get face attributes of face FACE_NAME from frame-local faces on frame
2027 F. Store the resulting attributes in ATTRS which must point to a
2028 vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If FACE_NAME is an
2029 alias for another face, use that face's definition. If SIGNAL_P is
2030 non-zero, signal an error if FACE_NAME does not name a face.
2031 Otherwise, value is zero if FACE_NAME is not a face. */
2033 static int
2034 get_lface_attributes (struct frame *f, Lisp_Object face_name,
2035 Lisp_Object attrs[LFACE_VECTOR_SIZE], int signal_p,
2036 struct named_merge_point *named_merge_points)
2038 Lisp_Object face_remapping;
2040 face_name = resolve_face_name (face_name, signal_p);
2042 /* See if SYMBOL has been remapped to some other face (usually this
2043 is done buffer-locally). */
2044 face_remapping = assq_no_quit (face_name, Vface_remapping_alist);
2045 if (CONSP (face_remapping))
2047 struct named_merge_point named_merge_point;
2049 if (push_named_merge_point (&named_merge_point,
2050 face_name, NAMED_MERGE_POINT_REMAP,
2051 &named_merge_points))
2053 int i;
2055 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2056 attrs[i] = Qunspecified;
2058 return merge_face_ref (f, XCDR (face_remapping), attrs,
2059 signal_p, named_merge_points);
2063 /* Default case, no remapping. */
2064 return get_lface_attributes_no_remap (f, face_name, attrs, signal_p);
2068 /* Non-zero if all attributes in face attribute vector ATTRS are
2069 specified, i.e. are non-nil. */
2071 static int
2072 lface_fully_specified_p (Lisp_Object attrs[LFACE_VECTOR_SIZE])
2074 int i;
2076 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2077 if (i != LFACE_FONT_INDEX && i != LFACE_INHERIT_INDEX
2078 && i != LFACE_DISTANT_FOREGROUND_INDEX)
2079 if ((UNSPECIFIEDP (attrs[i]) || IGNORE_DEFFACE_P (attrs[i])))
2080 break;
2082 return i == LFACE_VECTOR_SIZE;
2085 #ifdef HAVE_WINDOW_SYSTEM
2087 /* Set font-related attributes of Lisp face LFACE from FONT-OBJECT.
2088 If FORCE_P is zero, set only unspecified attributes of LFACE. The
2089 exception is `font' attribute. It is set to FONT_OBJECT regardless
2090 of FORCE_P. */
2092 static int
2093 set_lface_from_font (struct frame *f, Lisp_Object lface,
2094 Lisp_Object font_object, int force_p)
2096 Lisp_Object val;
2097 struct font *font = XFONT_OBJECT (font_object);
2099 /* Set attributes only if unspecified, otherwise face defaults for
2100 new frames would never take effect. If the font doesn't have a
2101 specific property, set a normal value for that. */
2103 if (force_p || UNSPECIFIEDP (LFACE_FAMILY (lface)))
2105 Lisp_Object family = AREF (font_object, FONT_FAMILY_INDEX);
2107 ASET (lface, LFACE_FAMILY_INDEX, SYMBOL_NAME (family));
2110 if (force_p || UNSPECIFIEDP (LFACE_FOUNDRY (lface)))
2112 Lisp_Object foundry = AREF (font_object, FONT_FOUNDRY_INDEX);
2114 ASET (lface, LFACE_FOUNDRY_INDEX, SYMBOL_NAME (foundry));
2117 if (force_p || UNSPECIFIEDP (LFACE_HEIGHT (lface)))
2119 int pt = PIXEL_TO_POINT (font->pixel_size * 10, FRAME_RES_Y (f));
2121 eassert (pt > 0);
2122 ASET (lface, LFACE_HEIGHT_INDEX, make_number (pt));
2125 if (force_p || UNSPECIFIEDP (LFACE_WEIGHT (lface)))
2127 val = FONT_WEIGHT_FOR_FACE (font_object);
2128 ASET (lface, LFACE_WEIGHT_INDEX, ! NILP (val) ? val :Qnormal);
2130 if (force_p || UNSPECIFIEDP (LFACE_SLANT (lface)))
2132 val = FONT_SLANT_FOR_FACE (font_object);
2133 ASET (lface, LFACE_SLANT_INDEX, ! NILP (val) ? val : Qnormal);
2135 if (force_p || UNSPECIFIEDP (LFACE_SWIDTH (lface)))
2137 val = FONT_WIDTH_FOR_FACE (font_object);
2138 ASET (lface, LFACE_SWIDTH_INDEX, ! NILP (val) ? val : Qnormal);
2141 ASET (lface, LFACE_FONT_INDEX, font_object);
2142 return 1;
2145 #endif /* HAVE_WINDOW_SYSTEM */
2148 /* Merges the face height FROM with the face height TO, and returns the
2149 merged height. If FROM is an invalid height, then INVALID is
2150 returned instead. FROM and TO may be either absolute face heights or
2151 `relative' heights; the returned value is always an absolute height
2152 unless both FROM and TO are relative. */
2154 static Lisp_Object
2155 merge_face_heights (Lisp_Object from, Lisp_Object to, Lisp_Object invalid)
2157 Lisp_Object result = invalid;
2159 if (INTEGERP (from))
2160 /* FROM is absolute, just use it as is. */
2161 result = from;
2162 else if (FLOATP (from))
2163 /* FROM is a scale, use it to adjust TO. */
2165 if (INTEGERP (to))
2166 /* relative X absolute => absolute */
2167 result = make_number (XFLOAT_DATA (from) * XINT (to));
2168 else if (FLOATP (to))
2169 /* relative X relative => relative */
2170 result = make_float (XFLOAT_DATA (from) * XFLOAT_DATA (to));
2171 else if (UNSPECIFIEDP (to))
2172 result = from;
2174 else if (FUNCTIONP (from))
2175 /* FROM is a function, which use to adjust TO. */
2177 /* Call function with current height as argument.
2178 From is the new height. */
2179 result = safe_call1 (from, to);
2181 /* Ensure that if TO was absolute, so is the result. */
2182 if (INTEGERP (to) && !INTEGERP (result))
2183 result = invalid;
2186 return result;
2190 /* Merge two Lisp face attribute vectors on frame F, FROM and TO, and
2191 store the resulting attributes in TO, which must be already be
2192 completely specified and contain only absolute attributes. Every
2193 specified attribute of FROM overrides the corresponding attribute of
2194 TO; relative attributes in FROM are merged with the absolute value in
2195 TO and replace it. NAMED_MERGE_POINTS is used internally to detect
2196 loops in face inheritance/remapping; it should be 0 when called from
2197 other places. */
2199 static void
2200 merge_face_vectors (struct frame *f, Lisp_Object *from, Lisp_Object *to,
2201 struct named_merge_point *named_merge_points)
2203 int i;
2204 Lisp_Object font = Qnil;
2206 /* If FROM inherits from some other faces, merge their attributes into
2207 TO before merging FROM's direct attributes. Note that an :inherit
2208 attribute of `unspecified' is the same as one of nil; we never
2209 merge :inherit attributes, so nil is more correct, but lots of
2210 other code uses `unspecified' as a generic value for face attributes. */
2211 if (!UNSPECIFIEDP (from[LFACE_INHERIT_INDEX])
2212 && !NILP (from[LFACE_INHERIT_INDEX]))
2213 merge_face_ref (f, from[LFACE_INHERIT_INDEX], to, 0, named_merge_points);
2215 if (FONT_SPEC_P (from[LFACE_FONT_INDEX]))
2217 if (!UNSPECIFIEDP (to[LFACE_FONT_INDEX]))
2218 font = merge_font_spec (from[LFACE_FONT_INDEX], to[LFACE_FONT_INDEX]);
2219 else
2220 font = copy_font_spec (from[LFACE_FONT_INDEX]);
2221 to[LFACE_FONT_INDEX] = font;
2224 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2225 if (!UNSPECIFIEDP (from[i]))
2227 if (i == LFACE_HEIGHT_INDEX && !INTEGERP (from[i]))
2229 to[i] = merge_face_heights (from[i], to[i], to[i]);
2230 font_clear_prop (to, FONT_SIZE_INDEX);
2232 else if (i != LFACE_FONT_INDEX && ! EQ (to[i], from[i]))
2234 to[i] = from[i];
2235 if (i >= LFACE_FAMILY_INDEX && i <=LFACE_SLANT_INDEX)
2236 font_clear_prop (to,
2237 (i == LFACE_FAMILY_INDEX ? FONT_FAMILY_INDEX
2238 : i == LFACE_FOUNDRY_INDEX ? FONT_FOUNDRY_INDEX
2239 : i == LFACE_SWIDTH_INDEX ? FONT_WIDTH_INDEX
2240 : i == LFACE_HEIGHT_INDEX ? FONT_SIZE_INDEX
2241 : i == LFACE_WEIGHT_INDEX ? FONT_WEIGHT_INDEX
2242 : FONT_SLANT_INDEX));
2246 /* If FROM specifies a font spec, make its contents take precedence
2247 over :family and other attributes. This is needed for face
2248 remapping using :font to work. */
2250 if (!NILP (font))
2252 if (! NILP (AREF (font, FONT_FOUNDRY_INDEX)))
2253 to[LFACE_FOUNDRY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FOUNDRY_INDEX));
2254 if (! NILP (AREF (font, FONT_FAMILY_INDEX)))
2255 to[LFACE_FAMILY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FAMILY_INDEX));
2256 if (! NILP (AREF (font, FONT_WEIGHT_INDEX)))
2257 to[LFACE_WEIGHT_INDEX] = FONT_WEIGHT_FOR_FACE (font);
2258 if (! NILP (AREF (font, FONT_SLANT_INDEX)))
2259 to[LFACE_SLANT_INDEX] = FONT_SLANT_FOR_FACE (font);
2260 if (! NILP (AREF (font, FONT_WIDTH_INDEX)))
2261 to[LFACE_SWIDTH_INDEX] = FONT_WIDTH_FOR_FACE (font);
2262 ASET (font, FONT_SIZE_INDEX, Qnil);
2265 /* TO is always an absolute face, which should inherit from nothing.
2266 We blindly copy the :inherit attribute above and fix it up here. */
2267 to[LFACE_INHERIT_INDEX] = Qnil;
2270 /* Merge the named face FACE_NAME on frame F, into the vector of face
2271 attributes TO. NAMED_MERGE_POINTS is used to detect loops in face
2272 inheritance. Returns true if FACE_NAME is a valid face name and
2273 merging succeeded. */
2275 static int
2276 merge_named_face (struct frame *f, Lisp_Object face_name, Lisp_Object *to,
2277 struct named_merge_point *named_merge_points)
2279 struct named_merge_point named_merge_point;
2281 if (push_named_merge_point (&named_merge_point,
2282 face_name, NAMED_MERGE_POINT_NORMAL,
2283 &named_merge_points))
2285 struct gcpro gcpro1;
2286 Lisp_Object from[LFACE_VECTOR_SIZE];
2287 int ok = get_lface_attributes (f, face_name, from, 0, named_merge_points);
2289 if (ok)
2291 GCPRO1 (named_merge_point.face_name);
2292 merge_face_vectors (f, from, to, named_merge_points);
2293 UNGCPRO;
2296 return ok;
2298 else
2299 return 0;
2303 /* Merge face attributes from the lisp `face reference' FACE_REF on
2304 frame F into the face attribute vector TO. If ERR_MSGS is non-zero,
2305 problems with FACE_REF cause an error message to be shown. Return
2306 non-zero if no errors occurred (regardless of the value of ERR_MSGS).
2307 NAMED_MERGE_POINTS is used to detect loops in face inheritance or
2308 list structure; it may be 0 for most callers.
2310 FACE_REF may be a single face specification or a list of such
2311 specifications. Each face specification can be:
2313 1. A symbol or string naming a Lisp face.
2315 2. A property list of the form (KEYWORD VALUE ...) where each
2316 KEYWORD is a face attribute name, and value is an appropriate value
2317 for that attribute.
2319 3. Conses or the form (FOREGROUND-COLOR . COLOR) or
2320 (BACKGROUND-COLOR . COLOR) where COLOR is a color name. This is
2321 for compatibility with 20.2.
2323 Face specifications earlier in lists take precedence over later
2324 specifications. */
2326 static int
2327 merge_face_ref (struct frame *f, Lisp_Object face_ref, Lisp_Object *to,
2328 int err_msgs, struct named_merge_point *named_merge_points)
2330 int ok = 1; /* Succeed without an error? */
2332 if (CONSP (face_ref))
2334 Lisp_Object first = XCAR (face_ref);
2336 if (EQ (first, Qforeground_color)
2337 || EQ (first, Qbackground_color))
2339 /* One of (FOREGROUND-COLOR . COLOR) or (BACKGROUND-COLOR
2340 . COLOR). COLOR must be a string. */
2341 Lisp_Object color_name = XCDR (face_ref);
2342 Lisp_Object color = first;
2344 if (STRINGP (color_name))
2346 if (EQ (color, Qforeground_color))
2347 to[LFACE_FOREGROUND_INDEX] = color_name;
2348 else
2349 to[LFACE_BACKGROUND_INDEX] = color_name;
2351 else
2353 if (err_msgs)
2354 add_to_log ("Invalid face color", color_name, Qnil);
2355 ok = 0;
2358 else if (SYMBOLP (first)
2359 && *SDATA (SYMBOL_NAME (first)) == ':')
2361 /* Assume this is the property list form. */
2362 while (CONSP (face_ref) && CONSP (XCDR (face_ref)))
2364 Lisp_Object keyword = XCAR (face_ref);
2365 Lisp_Object value = XCAR (XCDR (face_ref));
2366 int err = 0;
2368 /* Specifying `unspecified' is a no-op. */
2369 if (EQ (value, Qunspecified))
2371 else if (EQ (keyword, QCfamily))
2373 if (STRINGP (value))
2375 to[LFACE_FAMILY_INDEX] = value;
2376 font_clear_prop (to, FONT_FAMILY_INDEX);
2378 else
2379 err = 1;
2381 else if (EQ (keyword, QCfoundry))
2383 if (STRINGP (value))
2385 to[LFACE_FOUNDRY_INDEX] = value;
2386 font_clear_prop (to, FONT_FOUNDRY_INDEX);
2388 else
2389 err = 1;
2391 else if (EQ (keyword, QCheight))
2393 Lisp_Object new_height =
2394 merge_face_heights (value, to[LFACE_HEIGHT_INDEX], Qnil);
2396 if (! NILP (new_height))
2398 to[LFACE_HEIGHT_INDEX] = new_height;
2399 font_clear_prop (to, FONT_SIZE_INDEX);
2401 else
2402 err = 1;
2404 else if (EQ (keyword, QCweight))
2406 if (SYMBOLP (value) && FONT_WEIGHT_NAME_NUMERIC (value) >= 0)
2408 to[LFACE_WEIGHT_INDEX] = value;
2409 font_clear_prop (to, FONT_WEIGHT_INDEX);
2411 else
2412 err = 1;
2414 else if (EQ (keyword, QCslant))
2416 if (SYMBOLP (value) && FONT_SLANT_NAME_NUMERIC (value) >= 0)
2418 to[LFACE_SLANT_INDEX] = value;
2419 font_clear_prop (to, FONT_SLANT_INDEX);
2421 else
2422 err = 1;
2424 else if (EQ (keyword, QCunderline))
2426 if (EQ (value, Qt)
2427 || NILP (value)
2428 || STRINGP (value)
2429 || CONSP (value))
2430 to[LFACE_UNDERLINE_INDEX] = value;
2431 else
2432 err = 1;
2434 else if (EQ (keyword, QCoverline))
2436 if (EQ (value, Qt)
2437 || NILP (value)
2438 || STRINGP (value))
2439 to[LFACE_OVERLINE_INDEX] = value;
2440 else
2441 err = 1;
2443 else if (EQ (keyword, QCstrike_through))
2445 if (EQ (value, Qt)
2446 || NILP (value)
2447 || STRINGP (value))
2448 to[LFACE_STRIKE_THROUGH_INDEX] = value;
2449 else
2450 err = 1;
2452 else if (EQ (keyword, QCbox))
2454 if (EQ (value, Qt))
2455 value = make_number (1);
2456 if (INTEGERP (value)
2457 || STRINGP (value)
2458 || CONSP (value)
2459 || NILP (value))
2460 to[LFACE_BOX_INDEX] = value;
2461 else
2462 err = 1;
2464 else if (EQ (keyword, QCinverse_video)
2465 || EQ (keyword, QCreverse_video))
2467 if (EQ (value, Qt) || NILP (value))
2468 to[LFACE_INVERSE_INDEX] = value;
2469 else
2470 err = 1;
2472 else if (EQ (keyword, QCforeground))
2474 if (STRINGP (value))
2475 to[LFACE_FOREGROUND_INDEX] = value;
2476 else
2477 err = 1;
2479 else if (EQ (keyword, QCdistant_foreground))
2481 if (STRINGP (value))
2482 to[LFACE_DISTANT_FOREGROUND_INDEX] = value;
2483 else
2484 err = 1;
2486 else if (EQ (keyword, QCbackground))
2488 if (STRINGP (value))
2489 to[LFACE_BACKGROUND_INDEX] = value;
2490 else
2491 err = 1;
2493 else if (EQ (keyword, QCstipple))
2495 #if defined (HAVE_WINDOW_SYSTEM)
2496 Lisp_Object pixmap_p = Fbitmap_spec_p (value);
2497 if (!NILP (pixmap_p))
2498 to[LFACE_STIPPLE_INDEX] = value;
2499 else
2500 err = 1;
2501 #endif /* HAVE_WINDOW_SYSTEM */
2503 else if (EQ (keyword, QCwidth))
2505 if (SYMBOLP (value) && FONT_WIDTH_NAME_NUMERIC (value) >= 0)
2507 to[LFACE_SWIDTH_INDEX] = value;
2508 font_clear_prop (to, FONT_WIDTH_INDEX);
2510 else
2511 err = 1;
2513 else if (EQ (keyword, QCfont))
2515 if (FONTP (value))
2516 to[LFACE_FONT_INDEX] = value;
2517 else
2518 err = 1;
2520 else if (EQ (keyword, QCinherit))
2522 /* This is not really very useful; it's just like a
2523 normal face reference. */
2524 if (! merge_face_ref (f, value, to,
2525 err_msgs, named_merge_points))
2526 err = 1;
2528 else
2529 err = 1;
2531 if (err)
2533 add_to_log ("Invalid face attribute %S %S", keyword, value);
2534 ok = 0;
2537 face_ref = XCDR (XCDR (face_ref));
2540 else
2542 /* This is a list of face refs. Those at the beginning of the
2543 list take precedence over what follows, so we have to merge
2544 from the end backwards. */
2545 Lisp_Object next = XCDR (face_ref);
2547 if (! NILP (next))
2548 ok = merge_face_ref (f, next, to, err_msgs, named_merge_points);
2550 if (! merge_face_ref (f, first, to, err_msgs, named_merge_points))
2551 ok = 0;
2554 else
2556 /* FACE_REF ought to be a face name. */
2557 ok = merge_named_face (f, face_ref, to, named_merge_points);
2558 if (!ok && err_msgs)
2559 add_to_log ("Invalid face reference: %s", face_ref, Qnil);
2562 return ok;
2566 DEFUN ("internal-make-lisp-face", Finternal_make_lisp_face,
2567 Sinternal_make_lisp_face, 1, 2, 0,
2568 doc: /* Make FACE, a symbol, a Lisp face with all attributes nil.
2569 If FACE was not known as a face before, create a new one.
2570 If optional argument FRAME is specified, make a frame-local face
2571 for that frame. Otherwise operate on the global face definition.
2572 Value is a vector of face attributes. */)
2573 (Lisp_Object face, Lisp_Object frame)
2575 Lisp_Object global_lface, lface;
2576 struct frame *f;
2577 int i;
2579 CHECK_SYMBOL (face);
2580 global_lface = lface_from_face_name (NULL, face, 0);
2582 if (!NILP (frame))
2584 CHECK_LIVE_FRAME (frame);
2585 f = XFRAME (frame);
2586 lface = lface_from_face_name (f, face, 0);
2588 else
2589 f = NULL, lface = Qnil;
2591 /* Add a global definition if there is none. */
2592 if (NILP (global_lface))
2594 global_lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2595 Qunspecified);
2596 ASET (global_lface, 0, Qface);
2597 Vface_new_frame_defaults = Fcons (Fcons (face, global_lface),
2598 Vface_new_frame_defaults);
2600 /* Assign the new Lisp face a unique ID. The mapping from Lisp
2601 face id to Lisp face is given by the vector lface_id_to_name.
2602 The mapping from Lisp face to Lisp face id is given by the
2603 property `face' of the Lisp face name. */
2604 if (next_lface_id == lface_id_to_name_size)
2605 lface_id_to_name =
2606 xpalloc (lface_id_to_name, &lface_id_to_name_size, 1, MAX_FACE_ID,
2607 sizeof *lface_id_to_name);
2609 lface_id_to_name[next_lface_id] = face;
2610 Fput (face, Qface, make_number (next_lface_id));
2611 ++next_lface_id;
2613 else if (f == NULL)
2614 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2615 ASET (global_lface, i, Qunspecified);
2617 /* Add a frame-local definition. */
2618 if (f)
2620 if (NILP (lface))
2622 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2623 Qunspecified);
2624 ASET (lface, 0, Qface);
2625 fset_face_alist (f, Fcons (Fcons (face, lface), f->face_alist));
2627 else
2628 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2629 ASET (lface, i, Qunspecified);
2631 else
2632 lface = global_lface;
2634 /* Changing a named face means that all realized faces depending on
2635 that face are invalid. Since we cannot tell which realized faces
2636 depend on the face, make sure they are all removed. This is done
2637 by incrementing face_change_count. The next call to
2638 init_iterator will then free realized faces. */
2639 if (NILP (Fget (face, Qface_no_inherit)))
2641 ++face_change_count;
2642 windows_or_buffers_changed = 54;
2645 eassert (LFACEP (lface));
2646 check_lface (lface);
2647 return lface;
2651 DEFUN ("internal-lisp-face-p", Finternal_lisp_face_p,
2652 Sinternal_lisp_face_p, 1, 2, 0,
2653 doc: /* Return non-nil if FACE names a face.
2654 FACE should be a symbol or string.
2655 If optional second argument FRAME is non-nil, check for the
2656 existence of a frame-local face with name FACE on that frame.
2657 Otherwise check for the existence of a global face. */)
2658 (Lisp_Object face, Lisp_Object frame)
2660 Lisp_Object lface;
2662 face = resolve_face_name (face, 1);
2664 if (!NILP (frame))
2666 CHECK_LIVE_FRAME (frame);
2667 lface = lface_from_face_name (XFRAME (frame), face, 0);
2669 else
2670 lface = lface_from_face_name (NULL, face, 0);
2672 return lface;
2676 DEFUN ("internal-copy-lisp-face", Finternal_copy_lisp_face,
2677 Sinternal_copy_lisp_face, 4, 4, 0,
2678 doc: /* Copy face FROM to TO.
2679 If FRAME is t, copy the global face definition of FROM.
2680 Otherwise, copy the frame-local definition of FROM on FRAME.
2681 If NEW-FRAME is a frame, copy that data into the frame-local
2682 definition of TO on NEW-FRAME. If NEW-FRAME is nil,
2683 FRAME controls where the data is copied to.
2685 The value is TO. */)
2686 (Lisp_Object from, Lisp_Object to, Lisp_Object frame, Lisp_Object new_frame)
2688 Lisp_Object lface, copy;
2690 CHECK_SYMBOL (from);
2691 CHECK_SYMBOL (to);
2693 if (EQ (frame, Qt))
2695 /* Copy global definition of FROM. We don't make copies of
2696 strings etc. because 20.2 didn't do it either. */
2697 lface = lface_from_face_name (NULL, from, 1);
2698 copy = Finternal_make_lisp_face (to, Qnil);
2700 else
2702 /* Copy frame-local definition of FROM. */
2703 if (NILP (new_frame))
2704 new_frame = frame;
2705 CHECK_LIVE_FRAME (frame);
2706 CHECK_LIVE_FRAME (new_frame);
2707 lface = lface_from_face_name (XFRAME (frame), from, 1);
2708 copy = Finternal_make_lisp_face (to, new_frame);
2711 vcopy (copy, 0, XVECTOR (lface)->contents, LFACE_VECTOR_SIZE);
2713 /* Changing a named face means that all realized faces depending on
2714 that face are invalid. Since we cannot tell which realized faces
2715 depend on the face, make sure they are all removed. This is done
2716 by incrementing face_change_count. The next call to
2717 init_iterator will then free realized faces. */
2718 if (NILP (Fget (to, Qface_no_inherit)))
2720 ++face_change_count;
2721 windows_or_buffers_changed = 55;
2724 return to;
2728 DEFUN ("internal-set-lisp-face-attribute", Finternal_set_lisp_face_attribute,
2729 Sinternal_set_lisp_face_attribute, 3, 4, 0,
2730 doc: /* Set attribute ATTR of FACE to VALUE.
2731 FRAME being a frame means change the face on that frame.
2732 FRAME nil means change the face of the selected frame.
2733 FRAME t means change the default for new frames.
2734 FRAME 0 means change the face on all frames, and change the default
2735 for new frames. */)
2736 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
2738 Lisp_Object lface;
2739 Lisp_Object old_value = Qnil;
2740 /* Set one of enum font_property_index (> 0) if ATTR is one of
2741 font-related attributes other than QCfont and QCfontset. */
2742 enum font_property_index prop_index = 0;
2744 CHECK_SYMBOL (face);
2745 CHECK_SYMBOL (attr);
2747 face = resolve_face_name (face, 1);
2749 /* If FRAME is 0, change face on all frames, and change the
2750 default for new frames. */
2751 if (INTEGERP (frame) && XINT (frame) == 0)
2753 Lisp_Object tail;
2754 Finternal_set_lisp_face_attribute (face, attr, value, Qt);
2755 FOR_EACH_FRAME (tail, frame)
2756 Finternal_set_lisp_face_attribute (face, attr, value, frame);
2757 return face;
2760 /* Set lface to the Lisp attribute vector of FACE. */
2761 if (EQ (frame, Qt))
2763 lface = lface_from_face_name (NULL, face, 1);
2765 /* When updating face-new-frame-defaults, we put :ignore-defface
2766 where the caller wants `unspecified'. This forces the frame
2767 defaults to ignore the defface value. Otherwise, the defface
2768 will take effect, which is generally not what is intended.
2769 The value of that attribute will be inherited from some other
2770 face during face merging. See internal_merge_in_global_face. */
2771 if (UNSPECIFIEDP (value))
2772 value = QCignore_defface;
2774 else
2776 if (NILP (frame))
2777 frame = selected_frame;
2779 CHECK_LIVE_FRAME (frame);
2780 lface = lface_from_face_name (XFRAME (frame), face, 0);
2782 /* If a frame-local face doesn't exist yet, create one. */
2783 if (NILP (lface))
2784 lface = Finternal_make_lisp_face (face, frame);
2787 if (EQ (attr, QCfamily))
2789 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2791 CHECK_STRING (value);
2792 if (SCHARS (value) == 0)
2793 signal_error ("Invalid face family", value);
2795 old_value = LFACE_FAMILY (lface);
2796 ASET (lface, LFACE_FAMILY_INDEX, value);
2797 prop_index = FONT_FAMILY_INDEX;
2799 else if (EQ (attr, QCfoundry))
2801 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2803 CHECK_STRING (value);
2804 if (SCHARS (value) == 0)
2805 signal_error ("Invalid face foundry", value);
2807 old_value = LFACE_FOUNDRY (lface);
2808 ASET (lface, LFACE_FOUNDRY_INDEX, value);
2809 prop_index = FONT_FOUNDRY_INDEX;
2811 else if (EQ (attr, QCheight))
2813 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2815 if (EQ (face, Qdefault))
2817 /* The default face must have an absolute size. */
2818 if (!INTEGERP (value) || XINT (value) <= 0)
2819 signal_error ("Default face height not absolute and positive",
2820 value);
2822 else
2824 /* For non-default faces, do a test merge with a random
2825 height to see if VALUE's ok. */
2826 Lisp_Object test = merge_face_heights (value,
2827 make_number (10),
2828 Qnil);
2829 if (!INTEGERP (test) || XINT (test) <= 0)
2830 signal_error ("Face height does not produce a positive integer",
2831 value);
2835 old_value = LFACE_HEIGHT (lface);
2836 ASET (lface, LFACE_HEIGHT_INDEX, value);
2837 prop_index = FONT_SIZE_INDEX;
2839 else if (EQ (attr, QCweight))
2841 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2843 CHECK_SYMBOL (value);
2844 if (FONT_WEIGHT_NAME_NUMERIC (value) < 0)
2845 signal_error ("Invalid face weight", value);
2847 old_value = LFACE_WEIGHT (lface);
2848 ASET (lface, LFACE_WEIGHT_INDEX, value);
2849 prop_index = FONT_WEIGHT_INDEX;
2851 else if (EQ (attr, QCslant))
2853 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2855 CHECK_SYMBOL (value);
2856 if (FONT_SLANT_NAME_NUMERIC (value) < 0)
2857 signal_error ("Invalid face slant", value);
2859 old_value = LFACE_SLANT (lface);
2860 ASET (lface, LFACE_SLANT_INDEX, value);
2861 prop_index = FONT_SLANT_INDEX;
2863 else if (EQ (attr, QCunderline))
2865 bool valid_p = 0;
2867 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2868 valid_p = 1;
2869 else if (NILP (value) || EQ (value, Qt))
2870 valid_p = 1;
2871 else if (STRINGP (value) && SCHARS (value) > 0)
2872 valid_p = 1;
2873 else if (CONSP (value))
2875 Lisp_Object key, val, list;
2877 list = value;
2878 /* FIXME? This errs on the side of acceptance. Eg it accepts:
2879 (defface foo '((t :underline 'foo) "doc")
2880 Maybe this is intentional, maybe it isn't.
2881 Non-nil symbols other than t are not documented as being valid.
2882 Eg compare with inverse-video, which explicitly rejects them.
2884 valid_p = 1;
2886 while (!NILP (CAR_SAFE(list)))
2888 key = CAR_SAFE (list);
2889 list = CDR_SAFE (list);
2890 val = CAR_SAFE (list);
2891 list = CDR_SAFE (list);
2893 if (NILP (key) || NILP (val))
2895 valid_p = 0;
2896 break;
2899 else if (EQ (key, QCcolor)
2900 && !(EQ (val, Qforeground_color)
2901 || (STRINGP (val) && SCHARS (val) > 0)))
2903 valid_p = 0;
2904 break;
2907 else if (EQ (key, QCstyle)
2908 && !(EQ (val, Qline) || EQ (val, Qwave)))
2910 valid_p = 0;
2911 break;
2916 if (!valid_p)
2917 signal_error ("Invalid face underline", value);
2919 old_value = LFACE_UNDERLINE (lface);
2920 ASET (lface, LFACE_UNDERLINE_INDEX, value);
2922 else if (EQ (attr, QCoverline))
2924 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2925 if ((SYMBOLP (value)
2926 && !EQ (value, Qt)
2927 && !EQ (value, Qnil))
2928 /* Overline color. */
2929 || (STRINGP (value)
2930 && SCHARS (value) == 0))
2931 signal_error ("Invalid face overline", value);
2933 old_value = LFACE_OVERLINE (lface);
2934 ASET (lface, LFACE_OVERLINE_INDEX, value);
2936 else if (EQ (attr, QCstrike_through))
2938 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2939 if ((SYMBOLP (value)
2940 && !EQ (value, Qt)
2941 && !EQ (value, Qnil))
2942 /* Strike-through color. */
2943 || (STRINGP (value)
2944 && SCHARS (value) == 0))
2945 signal_error ("Invalid face strike-through", value);
2947 old_value = LFACE_STRIKE_THROUGH (lface);
2948 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, value);
2950 else if (EQ (attr, QCbox))
2952 bool valid_p;
2954 /* Allow t meaning a simple box of width 1 in foreground color
2955 of the face. */
2956 if (EQ (value, Qt))
2957 value = make_number (1);
2959 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2960 valid_p = 1;
2961 else if (NILP (value))
2962 valid_p = 1;
2963 else if (INTEGERP (value))
2964 valid_p = XINT (value) != 0;
2965 else if (STRINGP (value))
2966 valid_p = SCHARS (value) > 0;
2967 else if (CONSP (value))
2969 Lisp_Object tem;
2971 tem = value;
2972 while (CONSP (tem))
2974 Lisp_Object k, v;
2976 k = XCAR (tem);
2977 tem = XCDR (tem);
2978 if (!CONSP (tem))
2979 break;
2980 v = XCAR (tem);
2981 tem = XCDR (tem);
2983 if (EQ (k, QCline_width))
2985 if (!INTEGERP (v) || XINT (v) == 0)
2986 break;
2988 else if (EQ (k, QCcolor))
2990 if (!NILP (v) && (!STRINGP (v) || SCHARS (v) == 0))
2991 break;
2993 else if (EQ (k, QCstyle))
2995 if (!EQ (v, Qpressed_button) && !EQ (v, Qreleased_button))
2996 break;
2998 else
2999 break;
3002 valid_p = NILP (tem);
3004 else
3005 valid_p = 0;
3007 if (!valid_p)
3008 signal_error ("Invalid face box", value);
3010 old_value = LFACE_BOX (lface);
3011 ASET (lface, LFACE_BOX_INDEX, value);
3013 else if (EQ (attr, QCinverse_video)
3014 || EQ (attr, QCreverse_video))
3016 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3018 CHECK_SYMBOL (value);
3019 if (!EQ (value, Qt) && !NILP (value))
3020 signal_error ("Invalid inverse-video face attribute value", value);
3022 old_value = LFACE_INVERSE (lface);
3023 ASET (lface, LFACE_INVERSE_INDEX, value);
3025 else if (EQ (attr, QCforeground))
3027 /* Compatibility with 20.x. */
3028 if (NILP (value))
3029 value = Qunspecified;
3030 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3032 /* Don't check for valid color names here because it depends
3033 on the frame (display) whether the color will be valid
3034 when the face is realized. */
3035 CHECK_STRING (value);
3036 if (SCHARS (value) == 0)
3037 signal_error ("Empty foreground color value", value);
3039 old_value = LFACE_FOREGROUND (lface);
3040 ASET (lface, LFACE_FOREGROUND_INDEX, value);
3042 else if (EQ (attr, QCdistant_foreground))
3044 /* Compatibility with 20.x. */
3045 if (NILP (value))
3046 value = Qunspecified;
3047 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3049 /* Don't check for valid color names here because it depends
3050 on the frame (display) whether the color will be valid
3051 when the face is realized. */
3052 CHECK_STRING (value);
3053 if (SCHARS (value) == 0)
3054 signal_error ("Empty distant-foreground color value", value);
3056 old_value = LFACE_DISTANT_FOREGROUND (lface);
3057 ASET (lface, LFACE_DISTANT_FOREGROUND_INDEX, value);
3059 else if (EQ (attr, QCbackground))
3061 /* Compatibility with 20.x. */
3062 if (NILP (value))
3063 value = Qunspecified;
3064 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3066 /* Don't check for valid color names here because it depends
3067 on the frame (display) whether the color will be valid
3068 when the face is realized. */
3069 CHECK_STRING (value);
3070 if (SCHARS (value) == 0)
3071 signal_error ("Empty background color value", value);
3073 old_value = LFACE_BACKGROUND (lface);
3074 ASET (lface, LFACE_BACKGROUND_INDEX, value);
3076 else if (EQ (attr, QCstipple))
3078 #if defined (HAVE_WINDOW_SYSTEM)
3079 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3080 && !NILP (value)
3081 && NILP (Fbitmap_spec_p (value)))
3082 signal_error ("Invalid stipple attribute", value);
3083 old_value = LFACE_STIPPLE (lface);
3084 ASET (lface, LFACE_STIPPLE_INDEX, value);
3085 #endif /* HAVE_WINDOW_SYSTEM */
3087 else if (EQ (attr, QCwidth))
3089 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3091 CHECK_SYMBOL (value);
3092 if (FONT_WIDTH_NAME_NUMERIC (value) < 0)
3093 signal_error ("Invalid face width", value);
3095 old_value = LFACE_SWIDTH (lface);
3096 ASET (lface, LFACE_SWIDTH_INDEX, value);
3097 prop_index = FONT_WIDTH_INDEX;
3099 else if (EQ (attr, QCfont))
3101 #ifdef HAVE_WINDOW_SYSTEM
3102 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
3104 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3106 struct frame *f;
3108 old_value = LFACE_FONT (lface);
3109 if (! FONTP (value))
3111 if (STRINGP (value))
3113 Lisp_Object name = value;
3114 int fontset = fs_query_fontset (name, 0);
3116 if (fontset >= 0)
3117 name = fontset_ascii (fontset);
3118 value = font_spec_from_name (name);
3119 if (!FONTP (value))
3120 signal_error ("Invalid font name", name);
3122 else
3123 signal_error ("Invalid font or font-spec", value);
3125 if (EQ (frame, Qt))
3126 f = XFRAME (selected_frame);
3127 else
3128 f = XFRAME (frame);
3129 if (! FONT_OBJECT_P (value))
3131 Lisp_Object *attrs = XVECTOR (lface)->contents;
3132 Lisp_Object font_object;
3134 font_object = font_load_for_lface (f, attrs, value);
3135 if (NILP (font_object))
3136 signal_error ("Font not available", value);
3137 value = font_object;
3139 set_lface_from_font (f, lface, value, 1);
3141 else
3142 ASET (lface, LFACE_FONT_INDEX, value);
3144 #endif /* HAVE_WINDOW_SYSTEM */
3146 else if (EQ (attr, QCfontset))
3148 #ifdef HAVE_WINDOW_SYSTEM
3149 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
3151 Lisp_Object tmp;
3153 old_value = LFACE_FONTSET (lface);
3154 tmp = Fquery_fontset (value, Qnil);
3155 if (NILP (tmp))
3156 signal_error ("Invalid fontset name", value);
3157 ASET (lface, LFACE_FONTSET_INDEX, value = tmp);
3159 #endif /* HAVE_WINDOW_SYSTEM */
3161 else if (EQ (attr, QCinherit))
3163 Lisp_Object tail;
3164 if (SYMBOLP (value))
3165 tail = Qnil;
3166 else
3167 for (tail = value; CONSP (tail); tail = XCDR (tail))
3168 if (!SYMBOLP (XCAR (tail)))
3169 break;
3170 if (NILP (tail))
3171 ASET (lface, LFACE_INHERIT_INDEX, value);
3172 else
3173 signal_error ("Invalid face inheritance", value);
3175 else if (EQ (attr, QCbold))
3177 old_value = LFACE_WEIGHT (lface);
3178 ASET (lface, LFACE_WEIGHT_INDEX, NILP (value) ? Qnormal : Qbold);
3179 prop_index = FONT_WEIGHT_INDEX;
3181 else if (EQ (attr, QCitalic))
3183 attr = QCslant;
3184 old_value = LFACE_SLANT (lface);
3185 ASET (lface, LFACE_SLANT_INDEX, NILP (value) ? Qnormal : Qitalic);
3186 prop_index = FONT_SLANT_INDEX;
3188 else
3189 signal_error ("Invalid face attribute name", attr);
3191 if (prop_index)
3193 /* If a font-related attribute other than QCfont and QCfontset
3194 is specified, and if the original QCfont attribute has a font
3195 (font-spec or font-object), set the corresponding property in
3196 the font to nil so that the font selector doesn't think that
3197 the attribute is mandatory. Also, clear the average
3198 width. */
3199 font_clear_prop (XVECTOR (lface)->contents, prop_index);
3202 /* Changing a named face means that all realized faces depending on
3203 that face are invalid. Since we cannot tell which realized faces
3204 depend on the face, make sure they are all removed. This is done
3205 by incrementing face_change_count. The next call to
3206 init_iterator will then free realized faces. */
3207 if (!EQ (frame, Qt)
3208 && NILP (Fget (face, Qface_no_inherit))
3209 && NILP (Fequal (old_value, value)))
3211 ++face_change_count;
3212 windows_or_buffers_changed = 56;
3215 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3216 && NILP (Fequal (old_value, value)))
3218 Lisp_Object param;
3220 param = Qnil;
3222 if (EQ (face, Qdefault))
3224 #ifdef HAVE_WINDOW_SYSTEM
3225 /* Changed font-related attributes of the `default' face are
3226 reflected in changed `font' frame parameters. */
3227 if (FRAMEP (frame)
3228 && (prop_index || EQ (attr, QCfont))
3229 && lface_fully_specified_p (XVECTOR (lface)->contents))
3230 set_font_frame_param (frame, lface);
3231 else
3232 #endif /* HAVE_WINDOW_SYSTEM */
3234 if (EQ (attr, QCforeground))
3235 param = Qforeground_color;
3236 else if (EQ (attr, QCbackground))
3237 param = Qbackground_color;
3239 #ifdef HAVE_WINDOW_SYSTEM
3240 #ifndef HAVE_NTGUI
3241 else if (EQ (face, Qscroll_bar))
3243 /* Changing the colors of `scroll-bar' sets frame parameters
3244 `scroll-bar-foreground' and `scroll-bar-background'. */
3245 if (EQ (attr, QCforeground))
3246 param = Qscroll_bar_foreground;
3247 else if (EQ (attr, QCbackground))
3248 param = Qscroll_bar_background;
3250 #endif /* not HAVE_NTGUI */
3251 else if (EQ (face, Qborder))
3253 /* Changing background color of `border' sets frame parameter
3254 `border-color'. */
3255 if (EQ (attr, QCbackground))
3256 param = Qborder_color;
3258 else if (EQ (face, Qcursor))
3260 /* Changing background color of `cursor' sets frame parameter
3261 `cursor-color'. */
3262 if (EQ (attr, QCbackground))
3263 param = Qcursor_color;
3265 else if (EQ (face, Qmouse))
3267 /* Changing background color of `mouse' sets frame parameter
3268 `mouse-color'. */
3269 if (EQ (attr, QCbackground))
3270 param = Qmouse_color;
3272 #endif /* HAVE_WINDOW_SYSTEM */
3273 else if (EQ (face, Qmenu))
3275 /* Indicate that we have to update the menu bar when realizing
3276 faces on FRAME. FRAME t change the default for new frames.
3277 We do this by setting the flag in new face caches. */
3278 if (FRAMEP (frame))
3280 struct frame *f = XFRAME (frame);
3281 if (FRAME_FACE_CACHE (f) == NULL)
3282 FRAME_FACE_CACHE (f) = make_face_cache (f);
3283 FRAME_FACE_CACHE (f)->menu_face_changed_p = 1;
3285 else
3286 menu_face_changed_default = 1;
3289 if (!NILP (param))
3291 if (EQ (frame, Qt))
3292 /* Update `default-frame-alist', which is used for new frames. */
3294 store_in_alist (&Vdefault_frame_alist, param, value);
3296 else
3297 /* Update the current frame's parameters. */
3299 Lisp_Object cons;
3300 cons = XCAR (Vparam_value_alist);
3301 XSETCAR (cons, param);
3302 XSETCDR (cons, value);
3303 Fmodify_frame_parameters (frame, Vparam_value_alist);
3308 return face;
3312 /* Update the corresponding face when frame parameter PARAM on frame F
3313 has been assigned the value NEW_VALUE. */
3315 void
3316 update_face_from_frame_parameter (struct frame *f, Lisp_Object param,
3317 Lisp_Object new_value)
3319 Lisp_Object face = Qnil;
3320 Lisp_Object lface;
3322 /* If there are no faces yet, give up. This is the case when called
3323 from Fx_create_frame, and we do the necessary things later in
3324 face-set-after-frame-defaults. */
3325 if (NILP (f->face_alist))
3326 return;
3328 if (EQ (param, Qforeground_color))
3330 face = Qdefault;
3331 lface = lface_from_face_name (f, face, 1);
3332 ASET (lface, LFACE_FOREGROUND_INDEX,
3333 (STRINGP (new_value) ? new_value : Qunspecified));
3334 realize_basic_faces (f);
3336 else if (EQ (param, Qbackground_color))
3338 Lisp_Object frame;
3340 /* Changing the background color might change the background
3341 mode, so that we have to load new defface specs.
3342 Call frame-set-background-mode to do that. */
3343 XSETFRAME (frame, f);
3344 call1 (Qframe_set_background_mode, frame);
3346 face = Qdefault;
3347 lface = lface_from_face_name (f, face, 1);
3348 ASET (lface, LFACE_BACKGROUND_INDEX,
3349 (STRINGP (new_value) ? new_value : Qunspecified));
3350 realize_basic_faces (f);
3352 #ifdef HAVE_WINDOW_SYSTEM
3353 else if (EQ (param, Qborder_color))
3355 face = Qborder;
3356 lface = lface_from_face_name (f, face, 1);
3357 ASET (lface, LFACE_BACKGROUND_INDEX,
3358 (STRINGP (new_value) ? new_value : Qunspecified));
3360 else if (EQ (param, Qcursor_color))
3362 face = Qcursor;
3363 lface = lface_from_face_name (f, face, 1);
3364 ASET (lface, LFACE_BACKGROUND_INDEX,
3365 (STRINGP (new_value) ? new_value : Qunspecified));
3367 else if (EQ (param, Qmouse_color))
3369 face = Qmouse;
3370 lface = lface_from_face_name (f, face, 1);
3371 ASET (lface, LFACE_BACKGROUND_INDEX,
3372 (STRINGP (new_value) ? new_value : Qunspecified));
3374 #endif
3376 /* Changing a named face means that all realized faces depending on
3377 that face are invalid. Since we cannot tell which realized faces
3378 depend on the face, make sure they are all removed. This is done
3379 by incrementing face_change_count. The next call to
3380 init_iterator will then free realized faces. */
3381 if (!NILP (face)
3382 && NILP (Fget (face, Qface_no_inherit)))
3384 ++face_change_count;
3385 windows_or_buffers_changed = 57;
3390 #ifdef HAVE_WINDOW_SYSTEM
3392 /* Set the `font' frame parameter of FRAME determined from the
3393 font-object set in `default' face attributes LFACE. */
3395 static void
3396 set_font_frame_param (Lisp_Object frame, Lisp_Object lface)
3398 struct frame *f = XFRAME (frame);
3399 Lisp_Object font;
3401 if (FRAME_WINDOW_P (f)
3402 /* Don't do anything if the font is `unspecified'. This can
3403 happen during frame creation. */
3404 && (font = LFACE_FONT (lface),
3405 ! UNSPECIFIEDP (font)))
3407 if (FONT_SPEC_P (font))
3409 font = font_load_for_lface (f, XVECTOR (lface)->contents, font);
3410 if (NILP (font))
3411 return;
3412 ASET (lface, LFACE_FONT_INDEX, font);
3414 f->default_face_done_p = 0;
3415 Fmodify_frame_parameters (frame, list1 (Fcons (Qfont, font)));
3419 DEFUN ("internal-face-x-get-resource", Finternal_face_x_get_resource,
3420 Sinternal_face_x_get_resource, 2, 3, 0,
3421 doc: /* Get the value of X resource RESOURCE, class CLASS.
3422 Returned value is for the display of frame FRAME. If FRAME is not
3423 specified or nil, use selected frame. This function exists because
3424 ordinary `x-get-resource' doesn't take a frame argument. */)
3425 (Lisp_Object resource, Lisp_Object class, Lisp_Object frame)
3427 Lisp_Object value = Qnil;
3428 struct frame *f;
3430 CHECK_STRING (resource);
3431 CHECK_STRING (class);
3432 f = decode_live_frame (frame);
3433 block_input ();
3434 value = display_x_get_resource (FRAME_DISPLAY_INFO (f),
3435 resource, class, Qnil, Qnil);
3436 unblock_input ();
3437 return value;
3441 /* Return resource string VALUE as a boolean value, i.e. nil, or t.
3442 If VALUE is "on" or "true", return t. If VALUE is "off" or
3443 "false", return nil. Otherwise, if SIGNAL_P is non-zero, signal an
3444 error; if SIGNAL_P is zero, return 0. */
3446 static Lisp_Object
3447 face_boolean_x_resource_value (Lisp_Object value, int signal_p)
3449 Lisp_Object result = make_number (0);
3451 eassert (STRINGP (value));
3453 if (xstrcasecmp (SSDATA (value), "on") == 0
3454 || xstrcasecmp (SSDATA (value), "true") == 0)
3455 result = Qt;
3456 else if (xstrcasecmp (SSDATA (value), "off") == 0
3457 || xstrcasecmp (SSDATA (value), "false") == 0)
3458 result = Qnil;
3459 else if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3460 result = Qunspecified;
3461 else if (signal_p)
3462 signal_error ("Invalid face attribute value from X resource", value);
3464 return result;
3468 DEFUN ("internal-set-lisp-face-attribute-from-resource",
3469 Finternal_set_lisp_face_attribute_from_resource,
3470 Sinternal_set_lisp_face_attribute_from_resource,
3471 3, 4, 0, doc: /* */)
3472 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
3474 CHECK_SYMBOL (face);
3475 CHECK_SYMBOL (attr);
3476 CHECK_STRING (value);
3478 if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3479 value = Qunspecified;
3480 else if (EQ (attr, QCheight))
3482 value = Fstring_to_number (value, make_number (10));
3483 if (XINT (value) <= 0)
3484 signal_error ("Invalid face height from X resource", value);
3486 else if (EQ (attr, QCbold) || EQ (attr, QCitalic))
3487 value = face_boolean_x_resource_value (value, 1);
3488 else if (EQ (attr, QCweight) || EQ (attr, QCslant) || EQ (attr, QCwidth))
3489 value = intern (SSDATA (value));
3490 else if (EQ (attr, QCreverse_video) || EQ (attr, QCinverse_video))
3491 value = face_boolean_x_resource_value (value, 1);
3492 else if (EQ (attr, QCunderline)
3493 || EQ (attr, QCoverline)
3494 || EQ (attr, QCstrike_through))
3496 Lisp_Object boolean_value;
3498 /* If the result of face_boolean_x_resource_value is t or nil,
3499 VALUE does NOT specify a color. */
3500 boolean_value = face_boolean_x_resource_value (value, 0);
3501 if (SYMBOLP (boolean_value))
3502 value = boolean_value;
3504 else if (EQ (attr, QCbox) || EQ (attr, QCinherit))
3505 value = Fcar (Fread_from_string (value, Qnil, Qnil));
3507 return Finternal_set_lisp_face_attribute (face, attr, value, frame);
3510 #endif /* HAVE_WINDOW_SYSTEM */
3513 /***********************************************************************
3514 Menu face
3515 ***********************************************************************/
3517 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
3519 /* Make menus on frame F appear as specified by the `menu' face. */
3521 static void
3522 x_update_menu_appearance (struct frame *f)
3524 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
3525 XrmDatabase rdb;
3527 if (dpyinfo
3528 && (rdb = XrmGetDatabase (FRAME_X_DISPLAY (f)),
3529 rdb != NULL))
3531 char line[512];
3532 char *buf = line;
3533 ptrdiff_t bufsize = sizeof line;
3534 Lisp_Object lface = lface_from_face_name (f, Qmenu, 1);
3535 struct face *face = FACE_FROM_ID (f, MENU_FACE_ID);
3536 const char *myname = SSDATA (Vx_resource_name);
3537 bool changed_p = 0;
3538 #ifdef USE_MOTIF
3539 const char *popup_path = "popup_menu";
3540 #else
3541 const char *popup_path = "menu.popup";
3542 #endif
3544 if (STRINGP (LFACE_FOREGROUND (lface)))
3546 exprintf (&buf, &bufsize, line, -1, "%s.%s*foreground: %s",
3547 myname, popup_path,
3548 SDATA (LFACE_FOREGROUND (lface)));
3549 XrmPutLineResource (&rdb, line);
3550 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*foreground: %s",
3551 myname, SDATA (LFACE_FOREGROUND (lface)));
3552 XrmPutLineResource (&rdb, line);
3553 changed_p = 1;
3556 if (STRINGP (LFACE_BACKGROUND (lface)))
3558 exprintf (&buf, &bufsize, line, -1, "%s.%s*background: %s",
3559 myname, popup_path,
3560 SDATA (LFACE_BACKGROUND (lface)));
3561 XrmPutLineResource (&rdb, line);
3563 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*background: %s",
3564 myname, SDATA (LFACE_BACKGROUND (lface)));
3565 XrmPutLineResource (&rdb, line);
3566 changed_p = 1;
3569 if (face->font
3570 /* On Solaris 5.8, it's been reported that the `menu' face
3571 can be unspecified here, during startup. Why this
3572 happens remains unknown. -- cyd */
3573 && FONTP (LFACE_FONT (lface))
3574 && (!UNSPECIFIEDP (LFACE_FAMILY (lface))
3575 || !UNSPECIFIEDP (LFACE_FOUNDRY (lface))
3576 || !UNSPECIFIEDP (LFACE_SWIDTH (lface))
3577 || !UNSPECIFIEDP (LFACE_WEIGHT (lface))
3578 || !UNSPECIFIEDP (LFACE_SLANT (lface))
3579 || !UNSPECIFIEDP (LFACE_HEIGHT (lface))))
3581 Lisp_Object xlfd = Ffont_xlfd_name (LFACE_FONT (lface), Qnil);
3582 #ifdef USE_MOTIF
3583 const char *suffix = "List";
3584 Bool motif = True;
3585 #else
3586 #if defined HAVE_X_I18N
3588 const char *suffix = "Set";
3589 #else
3590 const char *suffix = "";
3591 #endif
3592 Bool motif = False;
3593 #endif
3595 if (! NILP (xlfd))
3597 #if defined HAVE_X_I18N
3598 char *fontsetname = xic_create_fontsetname (SSDATA (xlfd), motif);
3599 #else
3600 char *fontsetname = SSDATA (xlfd);
3601 #endif
3602 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*font%s: %s",
3603 myname, suffix, fontsetname);
3604 XrmPutLineResource (&rdb, line);
3606 exprintf (&buf, &bufsize, line, -1, "%s.%s*font%s: %s",
3607 myname, popup_path, suffix, fontsetname);
3608 XrmPutLineResource (&rdb, line);
3609 changed_p = 1;
3610 if (fontsetname != SSDATA (xlfd))
3611 xfree (fontsetname);
3615 if (changed_p && f->output_data.x->menubar_widget)
3616 free_frame_menubar (f);
3618 if (buf != line)
3619 xfree (buf);
3623 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
3626 DEFUN ("face-attribute-relative-p", Fface_attribute_relative_p,
3627 Sface_attribute_relative_p,
3628 2, 2, 0,
3629 doc: /* Check whether a face attribute value is relative.
3630 Specifically, this function returns t if the attribute ATTRIBUTE
3631 with the value VALUE is relative.
3633 A relative value is one that doesn't entirely override whatever is
3634 inherited from another face. For most possible attributes,
3635 the only relative value that users see is `unspecified'.
3636 However, for :height, floating point values are also relative. */)
3637 (Lisp_Object attribute, Lisp_Object value)
3639 if (EQ (value, Qunspecified) || (EQ (value, QCignore_defface)))
3640 return Qt;
3641 else if (EQ (attribute, QCheight))
3642 return INTEGERP (value) ? Qnil : Qt;
3643 else
3644 return Qnil;
3647 DEFUN ("merge-face-attribute", Fmerge_face_attribute, Smerge_face_attribute,
3648 3, 3, 0,
3649 doc: /* Return face ATTRIBUTE VALUE1 merged with VALUE2.
3650 If VALUE1 or VALUE2 are absolute (see `face-attribute-relative-p'), then
3651 the result will be absolute, otherwise it will be relative. */)
3652 (Lisp_Object attribute, Lisp_Object value1, Lisp_Object value2)
3654 if (EQ (value1, Qunspecified) || EQ (value1, QCignore_defface))
3655 return value2;
3656 else if (EQ (attribute, QCheight))
3657 return merge_face_heights (value1, value2, value1);
3658 else
3659 return value1;
3663 DEFUN ("internal-get-lisp-face-attribute", Finternal_get_lisp_face_attribute,
3664 Sinternal_get_lisp_face_attribute,
3665 2, 3, 0,
3666 doc: /* Return face attribute KEYWORD of face SYMBOL.
3667 If SYMBOL does not name a valid Lisp face or KEYWORD isn't a valid
3668 face attribute name, signal an error.
3669 If the optional argument FRAME is given, report on face SYMBOL in that
3670 frame. If FRAME is t, report on the defaults for face SYMBOL (for new
3671 frames). If FRAME is omitted or nil, use the selected frame. */)
3672 (Lisp_Object symbol, Lisp_Object keyword, Lisp_Object frame)
3674 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3675 Lisp_Object lface = lface_from_face_name (f, symbol, 1), value = Qnil;
3677 CHECK_SYMBOL (symbol);
3678 CHECK_SYMBOL (keyword);
3680 if (EQ (keyword, QCfamily))
3681 value = LFACE_FAMILY (lface);
3682 else if (EQ (keyword, QCfoundry))
3683 value = LFACE_FOUNDRY (lface);
3684 else if (EQ (keyword, QCheight))
3685 value = LFACE_HEIGHT (lface);
3686 else if (EQ (keyword, QCweight))
3687 value = LFACE_WEIGHT (lface);
3688 else if (EQ (keyword, QCslant))
3689 value = LFACE_SLANT (lface);
3690 else if (EQ (keyword, QCunderline))
3691 value = LFACE_UNDERLINE (lface);
3692 else if (EQ (keyword, QCoverline))
3693 value = LFACE_OVERLINE (lface);
3694 else if (EQ (keyword, QCstrike_through))
3695 value = LFACE_STRIKE_THROUGH (lface);
3696 else if (EQ (keyword, QCbox))
3697 value = LFACE_BOX (lface);
3698 else if (EQ (keyword, QCinverse_video)
3699 || EQ (keyword, QCreverse_video))
3700 value = LFACE_INVERSE (lface);
3701 else if (EQ (keyword, QCforeground))
3702 value = LFACE_FOREGROUND (lface);
3703 else if (EQ (keyword, QCdistant_foreground))
3704 value = LFACE_DISTANT_FOREGROUND (lface);
3705 else if (EQ (keyword, QCbackground))
3706 value = LFACE_BACKGROUND (lface);
3707 else if (EQ (keyword, QCstipple))
3708 value = LFACE_STIPPLE (lface);
3709 else if (EQ (keyword, QCwidth))
3710 value = LFACE_SWIDTH (lface);
3711 else if (EQ (keyword, QCinherit))
3712 value = LFACE_INHERIT (lface);
3713 else if (EQ (keyword, QCfont))
3714 value = LFACE_FONT (lface);
3715 else if (EQ (keyword, QCfontset))
3716 value = LFACE_FONTSET (lface);
3717 else
3718 signal_error ("Invalid face attribute name", keyword);
3720 if (IGNORE_DEFFACE_P (value))
3721 return Qunspecified;
3723 return value;
3727 DEFUN ("internal-lisp-face-attribute-values",
3728 Finternal_lisp_face_attribute_values,
3729 Sinternal_lisp_face_attribute_values, 1, 1, 0,
3730 doc: /* Return a list of valid discrete values for face attribute ATTR.
3731 Value is nil if ATTR doesn't have a discrete set of valid values. */)
3732 (Lisp_Object attr)
3734 Lisp_Object result = Qnil;
3736 CHECK_SYMBOL (attr);
3738 if (EQ (attr, QCunderline) || EQ (attr, QCoverline)
3739 || EQ (attr, QCstrike_through)
3740 || EQ (attr, QCinverse_video) || EQ (attr, QCreverse_video))
3741 result = list2 (Qt, Qnil);
3743 return result;
3747 DEFUN ("internal-merge-in-global-face", Finternal_merge_in_global_face,
3748 Sinternal_merge_in_global_face, 2, 2, 0,
3749 doc: /* Add attributes from frame-default definition of FACE to FACE on FRAME.
3750 Default face attributes override any local face attributes. */)
3751 (Lisp_Object face, Lisp_Object frame)
3753 int i;
3754 Lisp_Object global_lface, local_lface, *gvec, *lvec;
3755 struct frame *f = XFRAME (frame);
3757 CHECK_LIVE_FRAME (frame);
3758 global_lface = lface_from_face_name (NULL, face, 1);
3759 local_lface = lface_from_face_name (f, face, 0);
3760 if (NILP (local_lface))
3761 local_lface = Finternal_make_lisp_face (face, frame);
3763 /* Make every specified global attribute override the local one.
3764 BEWARE!! This is only used from `face-set-after-frame-default' where
3765 the local frame is defined from default specs in `face-defface-spec'
3766 and those should be overridden by global settings. Hence the strange
3767 "global before local" priority. */
3768 lvec = XVECTOR (local_lface)->contents;
3769 gvec = XVECTOR (global_lface)->contents;
3770 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3771 if (IGNORE_DEFFACE_P (gvec[i]))
3772 ASET (local_lface, i, Qunspecified);
3773 else if (! UNSPECIFIEDP (gvec[i]))
3774 ASET (local_lface, i, AREF (global_lface, i));
3776 /* If the default face was changed, update the face cache and the
3777 `font' frame parameter. */
3778 if (EQ (face, Qdefault))
3780 struct face_cache *c = FRAME_FACE_CACHE (f);
3781 struct face *newface, *oldface = FACE_FROM_ID (f, DEFAULT_FACE_ID);
3782 Lisp_Object attrs[LFACE_VECTOR_SIZE];
3784 /* This can be NULL (e.g., in batch mode). */
3785 if (oldface)
3787 /* Ensure that the face vector is fully specified by merging
3788 the previously-cached vector. */
3789 memcpy (attrs, oldface->lface, sizeof attrs);
3790 merge_face_vectors (f, lvec, attrs, 0);
3791 vcopy (local_lface, 0, attrs, LFACE_VECTOR_SIZE);
3792 newface = realize_face (c, lvec, DEFAULT_FACE_ID);
3794 if ((! UNSPECIFIEDP (gvec[LFACE_FAMILY_INDEX])
3795 || ! UNSPECIFIEDP (gvec[LFACE_FOUNDRY_INDEX])
3796 || ! UNSPECIFIEDP (gvec[LFACE_HEIGHT_INDEX])
3797 || ! UNSPECIFIEDP (gvec[LFACE_WEIGHT_INDEX])
3798 || ! UNSPECIFIEDP (gvec[LFACE_SLANT_INDEX])
3799 || ! UNSPECIFIEDP (gvec[LFACE_SWIDTH_INDEX])
3800 || ! UNSPECIFIEDP (gvec[LFACE_FONT_INDEX]))
3801 && newface->font)
3803 Lisp_Object name = newface->font->props[FONT_NAME_INDEX];
3804 Fmodify_frame_parameters (frame, list1 (Fcons (Qfont, name)));
3807 if (STRINGP (gvec[LFACE_FOREGROUND_INDEX]))
3808 Fmodify_frame_parameters (frame,
3809 list1 (Fcons (Qforeground_color,
3810 gvec[LFACE_FOREGROUND_INDEX])));
3812 if (STRINGP (gvec[LFACE_BACKGROUND_INDEX]))
3813 Fmodify_frame_parameters (frame,
3814 list1 (Fcons (Qbackground_color,
3815 gvec[LFACE_BACKGROUND_INDEX])));
3819 return Qnil;
3823 /* The following function is implemented for compatibility with 20.2.
3824 The function is used in x-resolve-fonts when it is asked to
3825 return fonts with the same size as the font of a face. This is
3826 done in fontset.el. */
3828 DEFUN ("face-font", Fface_font, Sface_font, 1, 3, 0,
3829 doc: /* Return the font name of face FACE, or nil if it is unspecified.
3830 The font name is, by default, for ASCII characters.
3831 If the optional argument FRAME is given, report on face FACE in that frame.
3832 If FRAME is t, report on the defaults for face FACE (for new frames).
3833 The font default for a face is either nil, or a list
3834 of the form (bold), (italic) or (bold italic).
3835 If FRAME is omitted or nil, use the selected frame. And, in this case,
3836 if the optional third argument CHARACTER is given,
3837 return the font name used for CHARACTER. */)
3838 (Lisp_Object face, Lisp_Object frame, Lisp_Object character)
3840 if (EQ (frame, Qt))
3842 Lisp_Object result = Qnil;
3843 Lisp_Object lface = lface_from_face_name (NULL, face, 1);
3845 if (!UNSPECIFIEDP (LFACE_WEIGHT (lface))
3846 && !EQ (LFACE_WEIGHT (lface), Qnormal))
3847 result = Fcons (Qbold, result);
3849 if (!UNSPECIFIEDP (LFACE_SLANT (lface))
3850 && !EQ (LFACE_SLANT (lface), Qnormal))
3851 result = Fcons (Qitalic, result);
3853 return result;
3855 else
3857 struct frame *f = decode_live_frame (frame);
3858 int face_id = lookup_named_face (f, face, 1);
3859 struct face *fface = FACE_FROM_ID (f, face_id);
3861 if (! fface)
3862 return Qnil;
3863 #ifdef HAVE_WINDOW_SYSTEM
3864 if (FRAME_WINDOW_P (f) && !NILP (character))
3866 CHECK_CHARACTER (character);
3867 face_id = FACE_FOR_CHAR (f, fface, XINT (character), -1, Qnil);
3868 fface = FACE_FROM_ID (f, face_id);
3870 return (fface->font
3871 ? fface->font->props[FONT_NAME_INDEX]
3872 : Qnil);
3873 #else /* !HAVE_WINDOW_SYSTEM */
3874 return build_string (FRAME_MSDOS_P (f)
3875 ? "ms-dos"
3876 : FRAME_W32_P (f) ? "w32term"
3877 :"tty");
3878 #endif
3883 /* Compare face-attribute values 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 face_attr_equal_p (Lisp_Object v1, Lisp_Object v2)
3890 /* Type can differ, e.g. when one attribute is unspecified, i.e. nil,
3891 and the other is specified. */
3892 if (XTYPE (v1) != XTYPE (v2))
3893 return 0;
3895 if (EQ (v1, v2))
3896 return 1;
3898 switch (XTYPE (v1))
3900 case Lisp_String:
3901 if (SBYTES (v1) != SBYTES (v2))
3902 return 0;
3904 return memcmp (SDATA (v1), SDATA (v2), SBYTES (v1)) == 0;
3906 case_Lisp_Int:
3907 case Lisp_Symbol:
3908 return 0;
3910 default:
3911 return !NILP (Fequal (v1, v2));
3916 /* Compare face vectors V1 and V2 for equality. Value is non-zero if
3917 all attributes are `equal'. Tries to be fast because this function
3918 is called quite often. */
3920 static bool
3921 lface_equal_p (Lisp_Object *v1, Lisp_Object *v2)
3923 int i;
3924 bool equal_p = 1;
3926 for (i = 1; i < LFACE_VECTOR_SIZE && equal_p; ++i)
3927 equal_p = face_attr_equal_p (v1[i], v2[i]);
3929 return equal_p;
3933 DEFUN ("internal-lisp-face-equal-p", Finternal_lisp_face_equal_p,
3934 Sinternal_lisp_face_equal_p, 2, 3, 0,
3935 doc: /* True if FACE1 and FACE2 are equal.
3936 If the optional argument FRAME is given, report on FACE1 and FACE2 in that frame.
3937 If FRAME is t, report on the defaults for FACE1 and FACE2 (for new frames).
3938 If FRAME is omitted or nil, use the selected frame. */)
3939 (Lisp_Object face1, Lisp_Object face2, Lisp_Object frame)
3941 int equal_p;
3942 struct frame *f;
3943 Lisp_Object lface1, lface2;
3945 /* Don't use decode_window_system_frame here because this function
3946 is called before X frames exist. At that time, if FRAME is nil,
3947 selected_frame will be used which is the frame dumped with
3948 Emacs. That frame is not an X frame. */
3949 f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3951 lface1 = lface_from_face_name (f, face1, 1);
3952 lface2 = lface_from_face_name (f, face2, 1);
3953 equal_p = lface_equal_p (XVECTOR (lface1)->contents,
3954 XVECTOR (lface2)->contents);
3955 return equal_p ? Qt : Qnil;
3959 DEFUN ("internal-lisp-face-empty-p", Finternal_lisp_face_empty_p,
3960 Sinternal_lisp_face_empty_p, 1, 2, 0,
3961 doc: /* True if FACE has no attribute specified.
3962 If the optional argument FRAME is given, report on face FACE in that frame.
3963 If FRAME is t, report on the defaults for face FACE (for new frames).
3964 If FRAME is omitted or nil, use the selected frame. */)
3965 (Lisp_Object face, Lisp_Object frame)
3967 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3968 Lisp_Object lface = lface_from_face_name (f, face, 1);
3969 int i;
3971 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3972 if (!UNSPECIFIEDP (AREF (lface, i)))
3973 break;
3975 return i == LFACE_VECTOR_SIZE ? Qt : Qnil;
3979 DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist,
3980 0, 1, 0,
3981 doc: /* Return an alist of frame-local faces defined on FRAME.
3982 For internal use only. */)
3983 (Lisp_Object frame)
3985 return decode_live_frame (frame)->face_alist;
3989 /* Return a hash code for Lisp string STRING with case ignored. Used
3990 below in computing a hash value for a Lisp face. */
3992 static unsigned
3993 hash_string_case_insensitive (Lisp_Object string)
3995 const unsigned char *s;
3996 unsigned hash = 0;
3997 eassert (STRINGP (string));
3998 for (s = SDATA (string); *s; ++s)
3999 hash = (hash << 1) ^ c_tolower (*s);
4000 return hash;
4004 /* Return a hash code for face attribute vector V. */
4006 static unsigned
4007 lface_hash (Lisp_Object *v)
4009 return (hash_string_case_insensitive (v[LFACE_FAMILY_INDEX])
4010 ^ hash_string_case_insensitive (v[LFACE_FOUNDRY_INDEX])
4011 ^ hash_string_case_insensitive (v[LFACE_FOREGROUND_INDEX])
4012 ^ hash_string_case_insensitive (v[LFACE_BACKGROUND_INDEX])
4013 ^ XHASH (v[LFACE_WEIGHT_INDEX])
4014 ^ XHASH (v[LFACE_SLANT_INDEX])
4015 ^ XHASH (v[LFACE_SWIDTH_INDEX])
4016 ^ XHASH (v[LFACE_HEIGHT_INDEX]));
4019 #ifdef HAVE_WINDOW_SYSTEM
4021 /* Return non-zero if LFACE1 and LFACE2 specify the same font (without
4022 considering charsets/registries). They do if they specify the same
4023 family, point size, weight, width, slant, and font. Both
4024 LFACE1 and LFACE2 must be fully-specified. */
4026 static int
4027 lface_same_font_attributes_p (Lisp_Object *lface1, Lisp_Object *lface2)
4029 eassert (lface_fully_specified_p (lface1)
4030 && lface_fully_specified_p (lface2));
4031 return (xstrcasecmp (SSDATA (lface1[LFACE_FAMILY_INDEX]),
4032 SSDATA (lface2[LFACE_FAMILY_INDEX])) == 0
4033 && xstrcasecmp (SSDATA (lface1[LFACE_FOUNDRY_INDEX]),
4034 SSDATA (lface2[LFACE_FOUNDRY_INDEX])) == 0
4035 && EQ (lface1[LFACE_HEIGHT_INDEX], lface2[LFACE_HEIGHT_INDEX])
4036 && EQ (lface1[LFACE_SWIDTH_INDEX], lface2[LFACE_SWIDTH_INDEX])
4037 && EQ (lface1[LFACE_WEIGHT_INDEX], lface2[LFACE_WEIGHT_INDEX])
4038 && EQ (lface1[LFACE_SLANT_INDEX], lface2[LFACE_SLANT_INDEX])
4039 && EQ (lface1[LFACE_FONT_INDEX], lface2[LFACE_FONT_INDEX])
4040 && (EQ (lface1[LFACE_FONTSET_INDEX], lface2[LFACE_FONTSET_INDEX])
4041 || (STRINGP (lface1[LFACE_FONTSET_INDEX])
4042 && STRINGP (lface2[LFACE_FONTSET_INDEX])
4043 && ! xstrcasecmp (SSDATA (lface1[LFACE_FONTSET_INDEX]),
4044 SSDATA (lface2[LFACE_FONTSET_INDEX]))))
4048 #endif /* HAVE_WINDOW_SYSTEM */
4050 /***********************************************************************
4051 Realized Faces
4052 ***********************************************************************/
4054 /* Allocate and return a new realized face for Lisp face attribute
4055 vector ATTR. */
4057 static struct face *
4058 make_realized_face (Lisp_Object *attr)
4060 enum { off = offsetof (struct face, id) };
4061 struct face *face = xmalloc (sizeof *face);
4063 memcpy (face->lface, attr, sizeof face->lface);
4064 memset (&face->id, 0, sizeof *face - off);
4065 face->ascii_face = face;
4067 return face;
4071 /* Free realized face FACE, including its X resources. FACE may
4072 be null. */
4074 static void
4075 free_realized_face (struct frame *f, struct face *face)
4077 if (face)
4079 #ifdef HAVE_WINDOW_SYSTEM
4080 if (FRAME_WINDOW_P (f))
4082 /* Free fontset of FACE if it is ASCII face. */
4083 if (face->fontset >= 0 && face == face->ascii_face)
4084 free_face_fontset (f, face);
4085 if (face->gc)
4087 block_input ();
4088 if (face->font)
4089 font_done_for_face (f, face);
4090 x_free_gc (f, face->gc);
4091 face->gc = 0;
4092 unblock_input ();
4094 #ifdef HAVE_X_WINDOWS
4095 free_face_colors (f, face);
4096 #endif /* HAVE_X_WINDOWS */
4097 x_destroy_bitmap (f, face->stipple);
4099 #endif /* HAVE_WINDOW_SYSTEM */
4101 xfree (face);
4106 /* Prepare face FACE for subsequent display on frame F. This
4107 allocated GCs if they haven't been allocated yet or have been freed
4108 by clearing the face cache. */
4110 void
4111 prepare_face_for_display (struct frame *f, struct face *face)
4113 #ifdef HAVE_WINDOW_SYSTEM
4114 eassert (FRAME_WINDOW_P (f));
4116 if (face->gc == 0)
4118 XGCValues xgcv;
4119 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
4121 xgcv.foreground = face->foreground;
4122 xgcv.background = face->background;
4123 #ifdef HAVE_X_WINDOWS
4124 xgcv.graphics_exposures = False;
4125 #endif
4127 block_input ();
4128 #ifdef HAVE_X_WINDOWS
4129 if (face->stipple)
4131 xgcv.fill_style = FillOpaqueStippled;
4132 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
4133 mask |= GCFillStyle | GCStipple;
4135 #endif
4136 face->gc = x_create_gc (f, mask, &xgcv);
4137 if (face->font)
4138 font_prepare_for_face (f, face);
4139 unblock_input ();
4141 #endif /* HAVE_WINDOW_SYSTEM */
4145 /* Returns the `distance' between the colors X and Y. */
4147 static int
4148 color_distance (XColor *x, XColor *y)
4150 /* This formula is from a paper titled `Colour metric' by Thiadmer Riemersma.
4151 Quoting from that paper:
4153 This formula has results that are very close to L*u*v* (with the
4154 modified lightness curve) and, more importantly, it is a more even
4155 algorithm: it does not have a range of colors where it suddenly
4156 gives far from optimal results.
4158 See <http://www.compuphase.com/cmetric.htm> for more info. */
4160 long r = (x->red - y->red) >> 8;
4161 long g = (x->green - y->green) >> 8;
4162 long b = (x->blue - y->blue) >> 8;
4163 long r_mean = (x->red + y->red) >> 9;
4165 return
4166 (((512 + r_mean) * r * r) >> 8)
4167 + 4 * g * g
4168 + (((767 - r_mean) * b * b) >> 8);
4172 DEFUN ("color-distance", Fcolor_distance, Scolor_distance, 2, 3, 0,
4173 doc: /* Return an integer distance between COLOR1 and COLOR2 on FRAME.
4174 COLOR1 and COLOR2 may be either strings containing the color name,
4175 or lists of the form (RED GREEN BLUE).
4176 If FRAME is unspecified or nil, the current frame is used. */)
4177 (Lisp_Object color1, Lisp_Object color2, Lisp_Object frame)
4179 struct frame *f = decode_live_frame (frame);
4180 XColor cdef1, cdef2;
4182 if (!(CONSP (color1) && parse_rgb_list (color1, &cdef1))
4183 && !(STRINGP (color1) && defined_color (f, SSDATA (color1), &cdef1, 0)))
4184 signal_error ("Invalid color", color1);
4185 if (!(CONSP (color2) && parse_rgb_list (color2, &cdef2))
4186 && !(STRINGP (color2) && defined_color (f, SSDATA (color2), &cdef2, 0)))
4187 signal_error ("Invalid color", color2);
4189 return make_number (color_distance (&cdef1, &cdef2));
4193 /***********************************************************************
4194 Face Cache
4195 ***********************************************************************/
4197 /* Return a new face cache for frame F. */
4199 static struct face_cache *
4200 make_face_cache (struct frame *f)
4202 struct face_cache *c = xmalloc (sizeof *c);
4204 c->buckets = xzalloc (FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets);
4205 c->size = 50;
4206 c->used = 0;
4207 c->faces_by_id = xmalloc (c->size * sizeof *c->faces_by_id);
4208 c->f = f;
4209 c->menu_face_changed_p = menu_face_changed_default;
4210 return c;
4213 #ifdef HAVE_WINDOW_SYSTEM
4215 /* Clear out all graphics contexts for all realized faces, except for
4216 the basic faces. This should be done from time to time just to avoid
4217 keeping too many graphics contexts that are no longer needed. */
4219 static void
4220 clear_face_gcs (struct face_cache *c)
4222 if (c && FRAME_WINDOW_P (c->f))
4224 int i;
4225 for (i = BASIC_FACE_ID_SENTINEL; i < c->used; ++i)
4227 struct face *face = c->faces_by_id[i];
4228 if (face && face->gc)
4230 block_input ();
4231 if (face->font)
4232 font_done_for_face (c->f, face);
4233 x_free_gc (c->f, face->gc);
4234 face->gc = 0;
4235 unblock_input ();
4241 #endif /* HAVE_WINDOW_SYSTEM */
4243 /* Free all realized faces in face cache C, including basic faces.
4244 C may be null. If faces are freed, make sure the frame's current
4245 matrix is marked invalid, so that a display caused by an expose
4246 event doesn't try to use faces we destroyed. */
4248 static void
4249 free_realized_faces (struct face_cache *c)
4251 if (c && c->used)
4253 int i, size;
4254 struct frame *f = c->f;
4256 /* We must block input here because we can't process X events
4257 safely while only some faces are freed, or when the frame's
4258 current matrix still references freed faces. */
4259 block_input ();
4261 for (i = 0; i < c->used; ++i)
4263 free_realized_face (f, c->faces_by_id[i]);
4264 c->faces_by_id[i] = NULL;
4267 c->used = 0;
4268 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4269 memset (c->buckets, 0, size);
4271 /* Must do a thorough redisplay the next time. Mark current
4272 matrices as invalid because they will reference faces freed
4273 above. This function is also called when a frame is
4274 destroyed. In this case, the root window of F is nil. */
4275 if (WINDOWP (f->root_window))
4277 clear_current_matrices (f);
4278 windows_or_buffers_changed = 58;
4281 unblock_input ();
4286 /* Free all realized faces on FRAME or on all frames if FRAME is nil.
4287 This is done after attributes of a named face have been changed,
4288 because we can't tell which realized faces depend on that face. */
4290 void
4291 free_all_realized_faces (Lisp_Object frame)
4293 if (NILP (frame))
4295 Lisp_Object rest;
4296 FOR_EACH_FRAME (rest, frame)
4297 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4299 else
4300 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4304 /* Free face cache C and faces in it, including their X resources. */
4306 static void
4307 free_face_cache (struct face_cache *c)
4309 if (c)
4311 free_realized_faces (c);
4312 xfree (c->buckets);
4313 xfree (c->faces_by_id);
4314 xfree (c);
4319 /* Cache realized face FACE in face cache C. HASH is the hash value
4320 of FACE. If FACE is for ASCII characters (i.e. FACE->ascii_face ==
4321 FACE), insert the new face to the beginning of the collision list
4322 of the face hash table of C. Otherwise, add the new face to the
4323 end of the collision list. This way, lookup_face can quickly find
4324 that a requested face is not cached. */
4326 static void
4327 cache_face (struct face_cache *c, struct face *face, unsigned int hash)
4329 int i = hash % FACE_CACHE_BUCKETS_SIZE;
4331 face->hash = hash;
4333 if (face->ascii_face != face)
4335 struct face *last = c->buckets[i];
4336 if (last)
4338 while (last->next)
4339 last = last->next;
4340 last->next = face;
4341 face->prev = last;
4342 face->next = NULL;
4344 else
4346 c->buckets[i] = face;
4347 face->prev = face->next = NULL;
4350 else
4352 face->prev = NULL;
4353 face->next = c->buckets[i];
4354 if (face->next)
4355 face->next->prev = face;
4356 c->buckets[i] = face;
4359 /* Find a free slot in C->faces_by_id and use the index of the free
4360 slot as FACE->id. */
4361 for (i = 0; i < c->used; ++i)
4362 if (c->faces_by_id[i] == NULL)
4363 break;
4364 face->id = i;
4366 #ifdef GLYPH_DEBUG
4367 /* Check that FACE got a unique id. */
4369 int j, n;
4370 struct face *face1;
4372 for (j = n = 0; j < FACE_CACHE_BUCKETS_SIZE; ++j)
4373 for (face1 = c->buckets[j]; face1; face1 = face1->next)
4374 if (face1->id == i)
4375 ++n;
4377 eassert (n == 1);
4379 #endif /* GLYPH_DEBUG */
4381 /* Maybe enlarge C->faces_by_id. */
4382 if (i == c->used)
4384 if (c->used == c->size)
4385 c->faces_by_id = xpalloc (c->faces_by_id, &c->size, 1, MAX_FACE_ID,
4386 sizeof *c->faces_by_id);
4387 c->used++;
4390 c->faces_by_id[i] = face;
4394 /* Remove face FACE from cache C. */
4396 static void
4397 uncache_face (struct face_cache *c, struct face *face)
4399 int i = face->hash % FACE_CACHE_BUCKETS_SIZE;
4401 if (face->prev)
4402 face->prev->next = face->next;
4403 else
4404 c->buckets[i] = face->next;
4406 if (face->next)
4407 face->next->prev = face->prev;
4409 c->faces_by_id[face->id] = NULL;
4410 if (face->id == c->used)
4411 --c->used;
4415 /* Look up a realized face with face attributes ATTR in the face cache
4416 of frame F. The face will be used to display ASCII characters.
4417 Value is the ID of the face found. If no suitable face is found,
4418 realize a new one. */
4420 static int
4421 lookup_face (struct frame *f, Lisp_Object *attr)
4423 struct face_cache *cache = FRAME_FACE_CACHE (f);
4424 unsigned hash;
4425 int i;
4426 struct face *face;
4428 eassert (cache != NULL);
4429 check_lface_attrs (attr);
4431 /* Look up ATTR in the face cache. */
4432 hash = lface_hash (attr);
4433 i = hash % FACE_CACHE_BUCKETS_SIZE;
4435 for (face = cache->buckets[i]; face; face = face->next)
4437 if (face->ascii_face != face)
4439 /* There's no more ASCII face. */
4440 face = NULL;
4441 break;
4443 if (face->hash == hash
4444 && lface_equal_p (face->lface, attr))
4445 break;
4448 /* If not found, realize a new face. */
4449 if (face == NULL)
4450 face = realize_face (cache, attr, -1);
4452 #ifdef GLYPH_DEBUG
4453 eassert (face == FACE_FROM_ID (f, face->id));
4454 #endif /* GLYPH_DEBUG */
4456 return face->id;
4459 #ifdef HAVE_WINDOW_SYSTEM
4460 /* Look up a realized face that has the same attributes as BASE_FACE
4461 except for the font in the face cache of frame F. If FONT-OBJECT
4462 is not nil, it is an already opened font. If FONT-OBJECT is nil,
4463 the face has no font. Value is the ID of the face found. If no
4464 suitable face is found, realize a new one. */
4467 face_for_font (struct frame *f, Lisp_Object font_object, struct face *base_face)
4469 struct face_cache *cache = FRAME_FACE_CACHE (f);
4470 unsigned hash;
4471 int i;
4472 struct face *face;
4474 eassert (cache != NULL);
4475 base_face = base_face->ascii_face;
4476 hash = lface_hash (base_face->lface);
4477 i = hash % FACE_CACHE_BUCKETS_SIZE;
4479 for (face = cache->buckets[i]; face; face = face->next)
4481 if (face->ascii_face == face)
4482 continue;
4483 if (face->ascii_face == base_face
4484 && face->font == (NILP (font_object) ? NULL
4485 : XFONT_OBJECT (font_object))
4486 && lface_equal_p (face->lface, base_face->lface))
4487 return face->id;
4490 /* If not found, realize a new face. */
4491 face = realize_non_ascii_face (f, font_object, base_face);
4492 return face->id;
4494 #endif /* HAVE_WINDOW_SYSTEM */
4496 /* Return the face id of the realized face for named face SYMBOL on
4497 frame F suitable for displaying ASCII characters. Value is -1 if
4498 the face couldn't be determined, which might happen if the default
4499 face isn't realized and cannot be realized. */
4502 lookup_named_face (struct frame *f, Lisp_Object symbol, int signal_p)
4504 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4505 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4506 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4508 if (default_face == NULL)
4510 if (!realize_basic_faces (f))
4511 return -1;
4512 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4513 if (default_face == NULL)
4514 emacs_abort (); /* realize_basic_faces must have set it up */
4517 if (! get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4518 return -1;
4520 memcpy (attrs, default_face->lface, sizeof attrs);
4521 merge_face_vectors (f, symbol_attrs, attrs, 0);
4523 return lookup_face (f, attrs);
4527 /* Return the display face-id of the basic face whose canonical face-id
4528 is FACE_ID. The return value will usually simply be FACE_ID, unless that
4529 basic face has bee remapped via Vface_remapping_alist. This function is
4530 conservative: if something goes wrong, it will simply return FACE_ID
4531 rather than signal an error. */
4534 lookup_basic_face (struct frame *f, int face_id)
4536 Lisp_Object name, mapping;
4537 int remapped_face_id;
4539 if (NILP (Vface_remapping_alist))
4540 return face_id; /* Nothing to do. */
4542 switch (face_id)
4544 case DEFAULT_FACE_ID: name = Qdefault; break;
4545 case MODE_LINE_FACE_ID: name = Qmode_line; break;
4546 case MODE_LINE_INACTIVE_FACE_ID: name = Qmode_line_inactive; break;
4547 case HEADER_LINE_FACE_ID: name = Qheader_line; break;
4548 case TOOL_BAR_FACE_ID: name = Qtool_bar; break;
4549 case FRINGE_FACE_ID: name = Qfringe; break;
4550 case SCROLL_BAR_FACE_ID: name = Qscroll_bar; break;
4551 case BORDER_FACE_ID: name = Qborder; break;
4552 case CURSOR_FACE_ID: name = Qcursor; break;
4553 case MOUSE_FACE_ID: name = Qmouse; break;
4554 case MENU_FACE_ID: name = Qmenu; break;
4556 default:
4557 emacs_abort (); /* the caller is supposed to pass us a basic face id */
4560 /* Do a quick scan through Vface_remapping_alist, and return immediately
4561 if there is no remapping for face NAME. This is just an optimization
4562 for the very common no-remapping case. */
4563 mapping = assq_no_quit (name, Vface_remapping_alist);
4564 if (NILP (mapping))
4565 return face_id; /* Give up. */
4567 /* If there is a remapping entry, lookup the face using NAME, which will
4568 handle the remapping too. */
4569 remapped_face_id = lookup_named_face (f, name, 0);
4570 if (remapped_face_id < 0)
4571 return face_id; /* Give up. */
4573 return remapped_face_id;
4577 /* Return a face for charset ASCII that is like the face with id
4578 FACE_ID on frame F, but has a font that is STEPS steps smaller.
4579 STEPS < 0 means larger. Value is the id of the face. */
4582 smaller_face (struct frame *f, int face_id, int steps)
4584 #ifdef HAVE_WINDOW_SYSTEM
4585 struct face *face;
4586 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4587 int pt, last_pt, last_height;
4588 int delta;
4589 int new_face_id;
4590 struct face *new_face;
4592 /* If not called for an X frame, just return the original face. */
4593 if (FRAME_TERMCAP_P (f))
4594 return face_id;
4596 /* Try in increments of 1/2 pt. */
4597 delta = steps < 0 ? 5 : -5;
4598 steps = eabs (steps);
4600 face = FACE_FROM_ID (f, face_id);
4601 memcpy (attrs, face->lface, sizeof attrs);
4602 pt = last_pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
4603 new_face_id = face_id;
4604 last_height = FONT_HEIGHT (face->font);
4606 while (steps
4607 && pt + delta > 0
4608 /* Give up if we cannot find a font within 10pt. */
4609 && eabs (last_pt - pt) < 100)
4611 /* Look up a face for a slightly smaller/larger font. */
4612 pt += delta;
4613 attrs[LFACE_HEIGHT_INDEX] = make_number (pt);
4614 new_face_id = lookup_face (f, attrs);
4615 new_face = FACE_FROM_ID (f, new_face_id);
4617 /* If height changes, count that as one step. */
4618 if ((delta < 0 && FONT_HEIGHT (new_face->font) < last_height)
4619 || (delta > 0 && FONT_HEIGHT (new_face->font) > last_height))
4621 --steps;
4622 last_height = FONT_HEIGHT (new_face->font);
4623 last_pt = pt;
4627 return new_face_id;
4629 #else /* not HAVE_WINDOW_SYSTEM */
4631 return face_id;
4633 #endif /* not HAVE_WINDOW_SYSTEM */
4637 /* Return a face for charset ASCII that is like the face with id
4638 FACE_ID on frame F, but has height HEIGHT. */
4641 face_with_height (struct frame *f, int face_id, int height)
4643 #ifdef HAVE_WINDOW_SYSTEM
4644 struct face *face;
4645 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4647 if (FRAME_TERMCAP_P (f)
4648 || height <= 0)
4649 return face_id;
4651 face = FACE_FROM_ID (f, face_id);
4652 memcpy (attrs, face->lface, sizeof attrs);
4653 attrs[LFACE_HEIGHT_INDEX] = make_number (height);
4654 font_clear_prop (attrs, FONT_SIZE_INDEX);
4655 face_id = lookup_face (f, attrs);
4656 #endif /* HAVE_WINDOW_SYSTEM */
4658 return face_id;
4662 /* Return the face id of the realized face for named face SYMBOL on
4663 frame F suitable for displaying ASCII characters, and use
4664 attributes of the face FACE_ID for attributes that aren't
4665 completely specified by SYMBOL. This is like lookup_named_face,
4666 except that the default attributes come from FACE_ID, not from the
4667 default face. FACE_ID is assumed to be already realized. */
4670 lookup_derived_face (struct frame *f, Lisp_Object symbol, int face_id,
4671 int signal_p)
4673 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4674 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4675 struct face *default_face = FACE_FROM_ID (f, face_id);
4677 if (!default_face)
4678 emacs_abort ();
4680 if (!get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4681 return -1;
4683 memcpy (attrs, default_face->lface, sizeof attrs);
4684 merge_face_vectors (f, symbol_attrs, attrs, 0);
4685 return lookup_face (f, attrs);
4688 DEFUN ("face-attributes-as-vector", Fface_attributes_as_vector,
4689 Sface_attributes_as_vector, 1, 1, 0,
4690 doc: /* Return a vector of face attributes corresponding to PLIST. */)
4691 (Lisp_Object plist)
4693 Lisp_Object lface;
4694 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
4695 Qunspecified);
4696 merge_face_ref (XFRAME (selected_frame), plist, XVECTOR (lface)->contents,
4697 1, 0);
4698 return lface;
4703 /***********************************************************************
4704 Face capability testing
4705 ***********************************************************************/
4708 /* If the distance (as returned by color_distance) between two colors is
4709 less than this, then they are considered the same, for determining
4710 whether a color is supported or not. The range of values is 0-65535. */
4712 #define TTY_SAME_COLOR_THRESHOLD 10000
4714 #ifdef HAVE_WINDOW_SYSTEM
4716 /* Return true if all the face attributes in ATTRS are supported
4717 on the window-system frame F.
4719 The definition of `supported' is somewhat heuristic, but basically means
4720 that a face containing all the attributes in ATTRS, when merged with the
4721 default face for display, can be represented in a way that's
4723 \(1) different in appearance than the default face, and
4724 \(2) `close in spirit' to what the attributes specify, if not exact. */
4726 static bool
4727 x_supports_face_attributes_p (struct frame *f,
4728 Lisp_Object attrs[LFACE_VECTOR_SIZE],
4729 struct face *def_face)
4731 Lisp_Object *def_attrs = def_face->lface;
4733 /* Check that other specified attributes are different that the default
4734 face. */
4735 if ((!UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
4736 && face_attr_equal_p (attrs[LFACE_UNDERLINE_INDEX],
4737 def_attrs[LFACE_UNDERLINE_INDEX]))
4738 || (!UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
4739 && face_attr_equal_p (attrs[LFACE_INVERSE_INDEX],
4740 def_attrs[LFACE_INVERSE_INDEX]))
4741 || (!UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
4742 && face_attr_equal_p (attrs[LFACE_FOREGROUND_INDEX],
4743 def_attrs[LFACE_FOREGROUND_INDEX]))
4744 || (!UNSPECIFIEDP (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
4745 && face_attr_equal_p (attrs[LFACE_DISTANT_FOREGROUND_INDEX],
4746 def_attrs[LFACE_DISTANT_FOREGROUND_INDEX]))
4747 || (!UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
4748 && face_attr_equal_p (attrs[LFACE_BACKGROUND_INDEX],
4749 def_attrs[LFACE_BACKGROUND_INDEX]))
4750 || (!UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4751 && face_attr_equal_p (attrs[LFACE_STIPPLE_INDEX],
4752 def_attrs[LFACE_STIPPLE_INDEX]))
4753 || (!UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4754 && face_attr_equal_p (attrs[LFACE_OVERLINE_INDEX],
4755 def_attrs[LFACE_OVERLINE_INDEX]))
4756 || (!UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4757 && face_attr_equal_p (attrs[LFACE_STRIKE_THROUGH_INDEX],
4758 def_attrs[LFACE_STRIKE_THROUGH_INDEX]))
4759 || (!UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
4760 && face_attr_equal_p (attrs[LFACE_BOX_INDEX],
4761 def_attrs[LFACE_BOX_INDEX])))
4762 return 0;
4764 /* Check font-related attributes, as those are the most commonly
4765 "unsupported" on a window-system (because of missing fonts). */
4766 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4767 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4768 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4769 || !UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
4770 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
4771 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX]))
4773 int face_id;
4774 struct face *face;
4775 Lisp_Object merged_attrs[LFACE_VECTOR_SIZE];
4776 int i;
4778 memcpy (merged_attrs, def_attrs, sizeof merged_attrs);
4780 merge_face_vectors (f, attrs, merged_attrs, 0);
4782 face_id = lookup_face (f, merged_attrs);
4783 face = FACE_FROM_ID (f, face_id);
4785 if (! face)
4786 error ("Cannot make face");
4788 /* If the font is the same, or no font is found, then not
4789 supported. */
4790 if (face->font == def_face->font
4791 || ! face->font)
4792 return 0;
4793 for (i = FONT_TYPE_INDEX; i <= FONT_SIZE_INDEX; i++)
4794 if (! EQ (face->font->props[i], def_face->font->props[i]))
4796 Lisp_Object s1, s2;
4798 if (i < FONT_FOUNDRY_INDEX || i > FONT_REGISTRY_INDEX
4799 || face->font->driver->case_sensitive)
4800 return 1;
4801 s1 = SYMBOL_NAME (face->font->props[i]);
4802 s2 = SYMBOL_NAME (def_face->font->props[i]);
4803 if (! EQ (Fcompare_strings (s1, make_number (0), Qnil,
4804 s2, make_number (0), Qnil, Qt), Qt))
4805 return 1;
4807 return 0;
4810 /* Everything checks out, this face is supported. */
4811 return 1;
4814 #endif /* HAVE_WINDOW_SYSTEM */
4816 /* Return true if all the face attributes in ATTRS are supported
4817 on the tty frame F.
4819 The definition of `supported' is somewhat heuristic, but basically means
4820 that a face containing all the attributes in ATTRS, when merged
4821 with the default face for display, can be represented in a way that's
4823 \(1) different in appearance than the default face, and
4824 \(2) `close in spirit' to what the attributes specify, if not exact.
4826 Point (2) implies that a `:weight black' attribute will be satisfied
4827 by any terminal that can display bold, and a `:foreground "yellow"' as
4828 long as the terminal can display a yellowish color, but `:slant italic'
4829 will _not_ be satisfied by the tty display code's automatic
4830 substitution of a `dim' face for italic. */
4832 static bool
4833 tty_supports_face_attributes_p (struct frame *f,
4834 Lisp_Object attrs[LFACE_VECTOR_SIZE],
4835 struct face *def_face)
4837 int weight, slant;
4838 Lisp_Object val, fg, bg;
4839 XColor fg_tty_color, fg_std_color;
4840 XColor bg_tty_color, bg_std_color;
4841 unsigned test_caps = 0;
4842 Lisp_Object *def_attrs = def_face->lface;
4844 /* First check some easy-to-check stuff; ttys support none of the
4845 following attributes, so we can just return false if any are requested
4846 (even if `nominal' values are specified, we should still return false,
4847 as that will be the same value that the default face uses). We
4848 consider :slant unsupportable on ttys, even though the face code
4849 actually `fakes' them using a dim attribute if possible. This is
4850 because the faked result is too different from what the face
4851 specifies. */
4852 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4853 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4854 || !UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4855 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4856 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
4857 || !UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4858 || !UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4859 || !UNSPECIFIEDP (attrs[LFACE_BOX_INDEX]))
4860 return 0;
4862 /* Test for terminal `capabilities' (non-color character attributes). */
4864 /* font weight (bold/dim) */
4865 val = attrs[LFACE_WEIGHT_INDEX];
4866 if (!UNSPECIFIEDP (val)
4867 && (weight = FONT_WEIGHT_NAME_NUMERIC (val), weight >= 0))
4869 int def_weight = FONT_WEIGHT_NAME_NUMERIC (def_attrs[LFACE_WEIGHT_INDEX]);
4871 if (weight > 100)
4873 if (def_weight > 100)
4874 return 0; /* same as default */
4875 test_caps = TTY_CAP_BOLD;
4877 else if (weight < 100)
4879 if (def_weight < 100)
4880 return 0; /* same as default */
4881 test_caps = TTY_CAP_DIM;
4883 else if (def_weight == 100)
4884 return 0; /* same as default */
4887 /* font slant */
4888 val = attrs[LFACE_SLANT_INDEX];
4889 if (!UNSPECIFIEDP (val)
4890 && (slant = FONT_SLANT_NAME_NUMERIC (val), slant >= 0))
4892 int def_slant = FONT_SLANT_NAME_NUMERIC (def_attrs[LFACE_SLANT_INDEX]);
4893 if (slant == 100 || slant == def_slant)
4894 return 0; /* same as default */
4895 else
4896 test_caps |= TTY_CAP_ITALIC;
4899 /* underlining */
4900 val = attrs[LFACE_UNDERLINE_INDEX];
4901 if (!UNSPECIFIEDP (val))
4903 if (STRINGP (val))
4904 return 0; /* ttys can't use colored underlines */
4905 else if (EQ (CAR_SAFE (val), QCstyle) && EQ (CAR_SAFE (CDR_SAFE (val)), Qwave))
4906 return 0; /* ttys can't use wave underlines */
4907 else if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
4908 return 0; /* same as default */
4909 else
4910 test_caps |= TTY_CAP_UNDERLINE;
4913 /* inverse video */
4914 val = attrs[LFACE_INVERSE_INDEX];
4915 if (!UNSPECIFIEDP (val))
4917 if (face_attr_equal_p (val, def_attrs[LFACE_INVERSE_INDEX]))
4918 return 0; /* same as default */
4919 else
4920 test_caps |= TTY_CAP_INVERSE;
4924 /* Color testing. */
4926 /* Check if foreground color is close enough. */
4927 fg = attrs[LFACE_FOREGROUND_INDEX];
4928 if (STRINGP (fg))
4930 Lisp_Object def_fg = def_attrs[LFACE_FOREGROUND_INDEX];
4932 if (face_attr_equal_p (fg, def_fg))
4933 return 0; /* same as default */
4934 else if (! tty_lookup_color (f, fg, &fg_tty_color, &fg_std_color))
4935 return 0; /* not a valid color */
4936 else if (color_distance (&fg_tty_color, &fg_std_color)
4937 > TTY_SAME_COLOR_THRESHOLD)
4938 return 0; /* displayed color is too different */
4939 else
4940 /* Make sure the color is really different than the default. */
4942 XColor def_fg_color;
4943 if (tty_lookup_color (f, def_fg, &def_fg_color, 0)
4944 && (color_distance (&fg_tty_color, &def_fg_color)
4945 <= TTY_SAME_COLOR_THRESHOLD))
4946 return 0;
4950 /* Check if background color is close enough. */
4951 bg = attrs[LFACE_BACKGROUND_INDEX];
4952 if (STRINGP (bg))
4954 Lisp_Object def_bg = def_attrs[LFACE_BACKGROUND_INDEX];
4956 if (face_attr_equal_p (bg, def_bg))
4957 return 0; /* same as default */
4958 else if (! tty_lookup_color (f, bg, &bg_tty_color, &bg_std_color))
4959 return 0; /* not a valid color */
4960 else if (color_distance (&bg_tty_color, &bg_std_color)
4961 > TTY_SAME_COLOR_THRESHOLD)
4962 return 0; /* displayed color is too different */
4963 else
4964 /* Make sure the color is really different than the default. */
4966 XColor def_bg_color;
4967 if (tty_lookup_color (f, def_bg, &def_bg_color, 0)
4968 && (color_distance (&bg_tty_color, &def_bg_color)
4969 <= TTY_SAME_COLOR_THRESHOLD))
4970 return 0;
4974 /* If both foreground and background are requested, see if the
4975 distance between them is OK. We just check to see if the distance
4976 between the tty's foreground and background is close enough to the
4977 distance between the standard foreground and background. */
4978 if (STRINGP (fg) && STRINGP (bg))
4980 int delta_delta
4981 = (color_distance (&fg_std_color, &bg_std_color)
4982 - color_distance (&fg_tty_color, &bg_tty_color));
4983 if (delta_delta > TTY_SAME_COLOR_THRESHOLD
4984 || delta_delta < -TTY_SAME_COLOR_THRESHOLD)
4985 return 0;
4989 /* See if the capabilities we selected above are supported, with the
4990 given colors. */
4991 return tty_capable_p (FRAME_TTY (f), test_caps);
4995 DEFUN ("display-supports-face-attributes-p",
4996 Fdisplay_supports_face_attributes_p, Sdisplay_supports_face_attributes_p,
4997 1, 2, 0,
4998 doc: /* Return non-nil if all the face attributes in ATTRIBUTES are supported.
4999 The optional argument DISPLAY can be a display name, a frame, or
5000 nil (meaning the selected frame's display).
5002 The definition of `supported' is somewhat heuristic, but basically means
5003 that a face containing all the attributes in ATTRIBUTES, when merged
5004 with the default face for display, can be represented in a way that's
5006 \(1) different in appearance than the default face, and
5007 \(2) `close in spirit' to what the attributes specify, if not exact.
5009 Point (2) implies that a `:weight black' attribute will be satisfied by
5010 any display that can display bold, and a `:foreground \"yellow\"' as long
5011 as it can display a yellowish color, but `:slant italic' will _not_ be
5012 satisfied by the tty display code's automatic substitution of a `dim'
5013 face for italic. */)
5014 (Lisp_Object attributes, Lisp_Object display)
5016 bool supports = 0;
5017 int i;
5018 Lisp_Object frame;
5019 struct frame *f;
5020 struct face *def_face;
5021 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5023 if (noninteractive || !initialized)
5024 /* We may not be able to access low-level face information in batch
5025 mode, or before being dumped, and this function is not going to
5026 be very useful in those cases anyway, so just give up. */
5027 return Qnil;
5029 if (NILP (display))
5030 frame = selected_frame;
5031 else if (FRAMEP (display))
5032 frame = display;
5033 else
5035 /* Find any frame on DISPLAY. */
5036 Lisp_Object tail;
5038 frame = Qnil;
5039 FOR_EACH_FRAME (tail, frame)
5040 if (!NILP (Fequal (Fcdr (Fassq (Qdisplay,
5041 XFRAME (frame)->param_alist)),
5042 display)))
5043 break;
5046 CHECK_LIVE_FRAME (frame);
5047 f = XFRAME (frame);
5049 for (i = 0; i < LFACE_VECTOR_SIZE; i++)
5050 attrs[i] = Qunspecified;
5051 merge_face_ref (f, attributes, attrs, 1, 0);
5053 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5054 if (def_face == NULL)
5056 if (! realize_basic_faces (f))
5057 error ("Cannot realize default face");
5058 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5059 if (def_face == NULL)
5060 emacs_abort (); /* realize_basic_faces must have set it up */
5063 /* Dispatch to the appropriate handler. */
5064 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5065 supports = tty_supports_face_attributes_p (f, attrs, def_face);
5066 #ifdef HAVE_WINDOW_SYSTEM
5067 else
5068 supports = x_supports_face_attributes_p (f, attrs, def_face);
5069 #endif
5071 return supports ? Qt : Qnil;
5075 /***********************************************************************
5076 Font selection
5077 ***********************************************************************/
5079 DEFUN ("internal-set-font-selection-order",
5080 Finternal_set_font_selection_order,
5081 Sinternal_set_font_selection_order, 1, 1, 0,
5082 doc: /* Set font selection order for face font selection to ORDER.
5083 ORDER must be a list of length 4 containing the symbols `:width',
5084 `:height', `:weight', and `:slant'. Face attributes appearing
5085 first in ORDER are matched first, e.g. if `:height' appears before
5086 `:weight' in ORDER, font selection first tries to find a font with
5087 a suitable height, and then tries to match the font weight.
5088 Value is ORDER. */)
5089 (Lisp_Object order)
5091 Lisp_Object list;
5092 int i;
5093 int indices[DIM (font_sort_order)];
5095 CHECK_LIST (order);
5096 memset (indices, 0, sizeof indices);
5097 i = 0;
5099 for (list = order;
5100 CONSP (list) && i < DIM (indices);
5101 list = XCDR (list), ++i)
5103 Lisp_Object attr = XCAR (list);
5104 int xlfd;
5106 if (EQ (attr, QCwidth))
5107 xlfd = XLFD_SWIDTH;
5108 else if (EQ (attr, QCheight))
5109 xlfd = XLFD_POINT_SIZE;
5110 else if (EQ (attr, QCweight))
5111 xlfd = XLFD_WEIGHT;
5112 else if (EQ (attr, QCslant))
5113 xlfd = XLFD_SLANT;
5114 else
5115 break;
5117 if (indices[i] != 0)
5118 break;
5119 indices[i] = xlfd;
5122 if (!NILP (list) || i != DIM (indices))
5123 signal_error ("Invalid font sort order", order);
5124 for (i = 0; i < DIM (font_sort_order); ++i)
5125 if (indices[i] == 0)
5126 signal_error ("Invalid font sort order", order);
5128 if (memcmp (indices, font_sort_order, sizeof indices) != 0)
5130 memcpy (font_sort_order, indices, sizeof font_sort_order);
5131 free_all_realized_faces (Qnil);
5134 font_update_sort_order (font_sort_order);
5136 return Qnil;
5140 DEFUN ("internal-set-alternative-font-family-alist",
5141 Finternal_set_alternative_font_family_alist,
5142 Sinternal_set_alternative_font_family_alist, 1, 1, 0,
5143 doc: /* Define alternative font families to try in face font selection.
5144 ALIST is an alist of (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5145 Each ALTERNATIVE is tried in order if no fonts of font family FAMILY 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, Fintern (XCAR (tail2), Qnil));
5163 Vface_alternative_font_family_alist = alist;
5164 free_all_realized_faces (Qnil);
5165 return alist;
5169 DEFUN ("internal-set-alternative-font-registry-alist",
5170 Finternal_set_alternative_font_registry_alist,
5171 Sinternal_set_alternative_font_registry_alist, 1, 1, 0,
5172 doc: /* Define alternative font registries to try in face font selection.
5173 ALIST is an alist of (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5174 Each ALTERNATIVE is tried in order if no fonts of font registry REGISTRY can
5175 be found. Value is ALIST. */)
5176 (Lisp_Object alist)
5178 Lisp_Object entry, tail, tail2;
5180 CHECK_LIST (alist);
5181 alist = Fcopy_sequence (alist);
5182 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5184 entry = XCAR (tail);
5185 CHECK_LIST (entry);
5186 entry = Fcopy_sequence (entry);
5187 XSETCAR (tail, entry);
5188 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5189 XSETCAR (tail2, Fdowncase (XCAR (tail2)));
5191 Vface_alternative_font_registry_alist = alist;
5192 free_all_realized_faces (Qnil);
5193 return alist;
5197 #ifdef HAVE_WINDOW_SYSTEM
5199 /* Return the fontset id of the base fontset name or alias name given
5200 by the fontset attribute of ATTRS. Value is -1 if the fontset
5201 attribute of ATTRS doesn't name a fontset. */
5203 static int
5204 face_fontset (Lisp_Object attrs[LFACE_VECTOR_SIZE])
5206 Lisp_Object name;
5208 name = attrs[LFACE_FONTSET_INDEX];
5209 if (!STRINGP (name))
5210 return -1;
5211 return fs_query_fontset (name, 0);
5214 #endif /* HAVE_WINDOW_SYSTEM */
5218 /***********************************************************************
5219 Face Realization
5220 ***********************************************************************/
5222 /* Realize basic faces on frame F. Value is zero if frame parameters
5223 of F don't contain enough information needed to realize the default
5224 face. */
5226 static bool
5227 realize_basic_faces (struct frame *f)
5229 bool success_p = 0;
5230 ptrdiff_t count = SPECPDL_INDEX ();
5232 /* Block input here so that we won't be surprised by an X expose
5233 event, for instance, without having the faces set up. */
5234 block_input ();
5235 specbind (Qscalable_fonts_allowed, Qt);
5237 if (realize_default_face (f))
5239 realize_named_face (f, Qmode_line, MODE_LINE_FACE_ID);
5240 realize_named_face (f, Qmode_line_inactive, MODE_LINE_INACTIVE_FACE_ID);
5241 realize_named_face (f, Qtool_bar, TOOL_BAR_FACE_ID);
5242 realize_named_face (f, Qfringe, FRINGE_FACE_ID);
5243 realize_named_face (f, Qheader_line, HEADER_LINE_FACE_ID);
5244 realize_named_face (f, Qscroll_bar, SCROLL_BAR_FACE_ID);
5245 realize_named_face (f, Qborder, BORDER_FACE_ID);
5246 realize_named_face (f, Qcursor, CURSOR_FACE_ID);
5247 realize_named_face (f, Qmouse, MOUSE_FACE_ID);
5248 realize_named_face (f, Qmenu, MENU_FACE_ID);
5249 realize_named_face (f, Qvertical_border, VERTICAL_BORDER_FACE_ID);
5250 realize_named_face (f, Qwindow_divider, WINDOW_DIVIDER_FACE_ID);
5252 /* Reflect changes in the `menu' face in menu bars. */
5253 if (FRAME_FACE_CACHE (f)->menu_face_changed_p)
5255 FRAME_FACE_CACHE (f)->menu_face_changed_p = 0;
5256 #ifdef USE_X_TOOLKIT
5257 if (FRAME_WINDOW_P (f))
5258 x_update_menu_appearance (f);
5259 #endif
5262 success_p = 1;
5265 unbind_to (count, Qnil);
5266 unblock_input ();
5267 return success_p;
5271 /* Realize the default face on frame F. If the face is not fully
5272 specified, make it fully-specified. Attributes of the default face
5273 that are not explicitly specified are taken from frame parameters. */
5275 static bool
5276 realize_default_face (struct frame *f)
5278 struct face_cache *c = FRAME_FACE_CACHE (f);
5279 Lisp_Object lface;
5280 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5281 struct face *face;
5283 /* If the `default' face is not yet known, create it. */
5284 lface = lface_from_face_name (f, Qdefault, 0);
5285 if (NILP (lface))
5287 Lisp_Object frame;
5288 XSETFRAME (frame, f);
5289 lface = Finternal_make_lisp_face (Qdefault, frame);
5292 #ifdef HAVE_WINDOW_SYSTEM
5293 if (FRAME_WINDOW_P (f))
5295 Lisp_Object font_object;
5297 XSETFONT (font_object, FRAME_FONT (f));
5298 set_lface_from_font (f, lface, font_object, f->default_face_done_p);
5299 ASET (lface, LFACE_FONTSET_INDEX, fontset_name (FRAME_FONTSET (f)));
5300 f->default_face_done_p = 1;
5302 #endif /* HAVE_WINDOW_SYSTEM */
5304 if (!FRAME_WINDOW_P (f))
5306 ASET (lface, LFACE_FAMILY_INDEX, build_string ("default"));
5307 ASET (lface, LFACE_FOUNDRY_INDEX, LFACE_FAMILY (lface));
5308 ASET (lface, LFACE_SWIDTH_INDEX, Qnormal);
5309 ASET (lface, LFACE_HEIGHT_INDEX, make_number (1));
5310 if (UNSPECIFIEDP (LFACE_WEIGHT (lface)))
5311 ASET (lface, LFACE_WEIGHT_INDEX, Qnormal);
5312 if (UNSPECIFIEDP (LFACE_SLANT (lface)))
5313 ASET (lface, LFACE_SLANT_INDEX, Qnormal);
5314 if (UNSPECIFIEDP (LFACE_FONTSET (lface)))
5315 ASET (lface, LFACE_FONTSET_INDEX, Qnil);
5318 if (UNSPECIFIEDP (LFACE_UNDERLINE (lface)))
5319 ASET (lface, LFACE_UNDERLINE_INDEX, Qnil);
5321 if (UNSPECIFIEDP (LFACE_OVERLINE (lface)))
5322 ASET (lface, LFACE_OVERLINE_INDEX, Qnil);
5324 if (UNSPECIFIEDP (LFACE_STRIKE_THROUGH (lface)))
5325 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, Qnil);
5327 if (UNSPECIFIEDP (LFACE_BOX (lface)))
5328 ASET (lface, LFACE_BOX_INDEX, Qnil);
5330 if (UNSPECIFIEDP (LFACE_INVERSE (lface)))
5331 ASET (lface, LFACE_INVERSE_INDEX, Qnil);
5333 if (UNSPECIFIEDP (LFACE_FOREGROUND (lface)))
5335 /* This function is called so early that colors are not yet
5336 set in the frame parameter list. */
5337 Lisp_Object color = Fassq (Qforeground_color, f->param_alist);
5339 if (CONSP (color) && STRINGP (XCDR (color)))
5340 ASET (lface, LFACE_FOREGROUND_INDEX, XCDR (color));
5341 else if (FRAME_WINDOW_P (f))
5342 return 0;
5343 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5344 ASET (lface, LFACE_FOREGROUND_INDEX, build_string (unspecified_fg));
5345 else
5346 emacs_abort ();
5349 if (UNSPECIFIEDP (LFACE_BACKGROUND (lface)))
5351 /* This function is called so early that colors are not yet
5352 set in the frame parameter list. */
5353 Lisp_Object color = Fassq (Qbackground_color, f->param_alist);
5354 if (CONSP (color) && STRINGP (XCDR (color)))
5355 ASET (lface, LFACE_BACKGROUND_INDEX, XCDR (color));
5356 else if (FRAME_WINDOW_P (f))
5357 return 0;
5358 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5359 ASET (lface, LFACE_BACKGROUND_INDEX, build_string (unspecified_bg));
5360 else
5361 emacs_abort ();
5364 if (UNSPECIFIEDP (LFACE_STIPPLE (lface)))
5365 ASET (lface, LFACE_STIPPLE_INDEX, Qnil);
5367 /* Realize the face; it must be fully-specified now. */
5368 eassert (lface_fully_specified_p (XVECTOR (lface)->contents));
5369 check_lface (lface);
5370 memcpy (attrs, XVECTOR (lface)->contents, sizeof attrs);
5371 face = realize_face (c, attrs, DEFAULT_FACE_ID);
5373 #ifdef HAVE_WINDOW_SYSTEM
5374 #ifdef HAVE_X_WINDOWS
5375 if (FRAME_X_P (f) && face->font != FRAME_FONT (f))
5377 /* This can happen when making a frame on a display that does
5378 not support the default font. */
5379 if (!face->font)
5380 return 0;
5382 /* Otherwise, the font specified for the frame was not
5383 acceptable as a font for the default face (perhaps because
5384 auto-scaled fonts are rejected), so we must adjust the frame
5385 font. */
5386 x_set_font (f, LFACE_FONT (lface), Qnil);
5388 #endif /* HAVE_X_WINDOWS */
5389 #endif /* HAVE_WINDOW_SYSTEM */
5390 return 1;
5394 /* Realize basic faces other than the default face in face cache C.
5395 SYMBOL is the face name, ID is the face id the realized face must
5396 have. The default face must have been realized already. */
5398 static void
5399 realize_named_face (struct frame *f, Lisp_Object symbol, int id)
5401 struct face_cache *c = FRAME_FACE_CACHE (f);
5402 Lisp_Object lface = lface_from_face_name (f, symbol, 0);
5403 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5404 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5406 /* The default face must exist and be fully specified. */
5407 get_lface_attributes_no_remap (f, Qdefault, attrs, 1);
5408 check_lface_attrs (attrs);
5409 eassert (lface_fully_specified_p (attrs));
5411 /* If SYMBOL isn't know as a face, create it. */
5412 if (NILP (lface))
5414 Lisp_Object frame;
5415 XSETFRAME (frame, f);
5416 lface = Finternal_make_lisp_face (symbol, frame);
5419 /* Merge SYMBOL's face with the default face. */
5420 get_lface_attributes_no_remap (f, symbol, symbol_attrs, 1);
5421 merge_face_vectors (f, symbol_attrs, attrs, 0);
5423 /* Realize the face. */
5424 realize_face (c, attrs, id);
5428 /* Realize the fully-specified face with attributes ATTRS in face
5429 cache CACHE for ASCII characters. If FORMER_FACE_ID is
5430 non-negative, it is an ID of face to remove before caching the new
5431 face. Value is a pointer to the newly created realized face. */
5433 static struct face *
5434 realize_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE],
5435 int former_face_id)
5437 struct face *face;
5439 /* LFACE must be fully specified. */
5440 eassert (cache != NULL);
5441 check_lface_attrs (attrs);
5443 if (former_face_id >= 0 && cache->used > former_face_id)
5445 /* Remove the former face. */
5446 struct face *former_face = cache->faces_by_id[former_face_id];
5447 uncache_face (cache, former_face);
5448 free_realized_face (cache->f, former_face);
5449 SET_FRAME_GARBAGED (cache->f);
5452 if (FRAME_WINDOW_P (cache->f))
5453 face = realize_x_face (cache, attrs);
5454 else if (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f))
5455 face = realize_tty_face (cache, attrs);
5456 else if (FRAME_INITIAL_P (cache->f))
5458 /* Create a dummy face. */
5459 face = make_realized_face (attrs);
5461 else
5462 emacs_abort ();
5464 /* Insert the new face. */
5465 cache_face (cache, face, lface_hash (attrs));
5466 return face;
5470 #ifdef HAVE_WINDOW_SYSTEM
5471 /* Realize the fully-specified face that uses FONT-OBJECT and has the
5472 same attributes as BASE_FACE except for the font on frame F.
5473 FONT-OBJECT may be nil, in which case, realized a face of
5474 no-font. */
5476 static struct face *
5477 realize_non_ascii_face (struct frame *f, Lisp_Object font_object,
5478 struct face *base_face)
5480 struct face_cache *cache = FRAME_FACE_CACHE (f);
5481 struct face *face;
5483 face = xmalloc (sizeof *face);
5484 *face = *base_face;
5485 face->gc = 0;
5486 face->extra = NULL;
5487 face->overstrike
5488 = (! NILP (font_object)
5489 && FONT_WEIGHT_NAME_NUMERIC (face->lface[LFACE_WEIGHT_INDEX]) > 100
5490 && FONT_WEIGHT_NUMERIC (font_object) <= 100);
5492 /* Don't try to free the colors copied bitwise from BASE_FACE. */
5493 face->colors_copied_bitwise_p = 1;
5494 face->font = NILP (font_object) ? NULL : XFONT_OBJECT (font_object);
5495 face->gc = 0;
5497 cache_face (cache, face, face->hash);
5499 return face;
5501 #endif /* HAVE_WINDOW_SYSTEM */
5504 /* Realize the fully-specified face with attributes ATTRS in face
5505 cache CACHE for ASCII characters. Do it for X frame CACHE->f. If
5506 the new face doesn't share font with the default face, a fontname
5507 is allocated from the heap and set in `font_name' of the new face,
5508 but it is not yet loaded here. Value is a pointer to the newly
5509 created realized face. */
5511 static struct face *
5512 realize_x_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE])
5514 struct face *face = NULL;
5515 #ifdef HAVE_WINDOW_SYSTEM
5516 struct face *default_face;
5517 struct frame *f;
5518 Lisp_Object stipple, underline, overline, strike_through, box;
5520 eassert (FRAME_WINDOW_P (cache->f));
5522 /* Allocate a new realized face. */
5523 face = make_realized_face (attrs);
5524 face->ascii_face = face;
5526 f = cache->f;
5528 /* Determine the font to use. Most of the time, the font will be
5529 the same as the font of the default face, so try that first. */
5530 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5531 if (default_face
5532 && lface_same_font_attributes_p (default_face->lface, attrs))
5534 face->font = default_face->font;
5535 face->fontset
5536 = make_fontset_for_ascii_face (f, default_face->fontset, face);
5538 else
5540 /* If the face attribute ATTRS specifies a fontset, use it as
5541 the base of a new realized fontset. Otherwise, use the same
5542 base fontset as of the default face. The base determines
5543 registry and encoding of a font. It may also determine
5544 foundry and family. The other fields of font name pattern
5545 are constructed from ATTRS. */
5546 int fontset = face_fontset (attrs);
5548 /* If we are realizing the default face, ATTRS should specify a
5549 fontset. In other words, if FONTSET is -1, we are not
5550 realizing the default face, thus the default face should have
5551 already been realized. */
5552 if (fontset == -1)
5554 if (default_face)
5555 fontset = default_face->fontset;
5556 if (fontset == -1)
5557 emacs_abort ();
5559 if (! FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5560 attrs[LFACE_FONT_INDEX]
5561 = font_load_for_lface (f, attrs, attrs[LFACE_FONT_INDEX]);
5562 if (FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5564 face->font = XFONT_OBJECT (attrs[LFACE_FONT_INDEX]);
5565 face->fontset = make_fontset_for_ascii_face (f, fontset, face);
5567 else
5569 face->font = NULL;
5570 face->fontset = -1;
5574 if (face->font
5575 && FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]) > 100
5576 && FONT_WEIGHT_NUMERIC (attrs[LFACE_FONT_INDEX]) <= 100)
5577 face->overstrike = 1;
5579 /* Load colors, and set remaining attributes. */
5581 load_face_colors (f, face, attrs);
5583 /* Set up box. */
5584 box = attrs[LFACE_BOX_INDEX];
5585 if (STRINGP (box))
5587 /* A simple box of line width 1 drawn in color given by
5588 the string. */
5589 face->box_color = load_color (f, face, attrs[LFACE_BOX_INDEX],
5590 LFACE_BOX_INDEX);
5591 face->box = FACE_SIMPLE_BOX;
5592 face->box_line_width = 1;
5594 else if (INTEGERP (box))
5596 /* Simple box of specified line width in foreground color of the
5597 face. */
5598 eassert (XINT (box) != 0);
5599 face->box = FACE_SIMPLE_BOX;
5600 face->box_line_width = XINT (box);
5601 face->box_color = face->foreground;
5602 face->box_color_defaulted_p = 1;
5604 else if (CONSP (box))
5606 /* `(:width WIDTH :color COLOR :shadow SHADOW)'. SHADOW
5607 being one of `raised' or `sunken'. */
5608 face->box = FACE_SIMPLE_BOX;
5609 face->box_color = face->foreground;
5610 face->box_color_defaulted_p = 1;
5611 face->box_line_width = 1;
5613 while (CONSP (box))
5615 Lisp_Object keyword, value;
5617 keyword = XCAR (box);
5618 box = XCDR (box);
5620 if (!CONSP (box))
5621 break;
5622 value = XCAR (box);
5623 box = XCDR (box);
5625 if (EQ (keyword, QCline_width))
5627 if (INTEGERP (value) && XINT (value) != 0)
5628 face->box_line_width = XINT (value);
5630 else if (EQ (keyword, QCcolor))
5632 if (STRINGP (value))
5634 face->box_color = load_color (f, face, value,
5635 LFACE_BOX_INDEX);
5636 face->use_box_color_for_shadows_p = 1;
5639 else if (EQ (keyword, QCstyle))
5641 if (EQ (value, Qreleased_button))
5642 face->box = FACE_RAISED_BOX;
5643 else if (EQ (value, Qpressed_button))
5644 face->box = FACE_SUNKEN_BOX;
5649 /* Text underline, overline, strike-through. */
5651 underline = attrs[LFACE_UNDERLINE_INDEX];
5652 if (EQ (underline, Qt))
5654 /* Use default color (same as foreground color). */
5655 face->underline_p = 1;
5656 face->underline_type = FACE_UNDER_LINE;
5657 face->underline_defaulted_p = 1;
5658 face->underline_color = 0;
5660 else if (STRINGP (underline))
5662 /* Use specified color. */
5663 face->underline_p = 1;
5664 face->underline_type = FACE_UNDER_LINE;
5665 face->underline_defaulted_p = 0;
5666 face->underline_color
5667 = load_color (f, face, underline,
5668 LFACE_UNDERLINE_INDEX);
5670 else if (NILP (underline))
5672 face->underline_p = 0;
5673 face->underline_defaulted_p = 0;
5674 face->underline_color = 0;
5676 else if (CONSP (underline))
5678 /* `(:color COLOR :style STYLE)'.
5679 STYLE being one of `line' or `wave'. */
5680 face->underline_p = 1;
5681 face->underline_color = 0;
5682 face->underline_defaulted_p = 1;
5683 face->underline_type = FACE_UNDER_LINE;
5685 /* FIXME? This is also not robust about checking the precise form.
5686 See comments in Finternal_set_lisp_face_attribute. */
5687 while (CONSP (underline))
5689 Lisp_Object keyword, value;
5691 keyword = XCAR (underline);
5692 underline = XCDR (underline);
5694 if (!CONSP (underline))
5695 break;
5696 value = XCAR (underline);
5697 underline = XCDR (underline);
5699 if (EQ (keyword, QCcolor))
5701 if (EQ (value, Qforeground_color))
5703 face->underline_defaulted_p = 1;
5704 face->underline_color = 0;
5706 else if (STRINGP (value))
5708 face->underline_defaulted_p = 0;
5709 face->underline_color = load_color (f, face, value,
5710 LFACE_UNDERLINE_INDEX);
5713 else if (EQ (keyword, QCstyle))
5715 if (EQ (value, Qline))
5716 face->underline_type = FACE_UNDER_LINE;
5717 else if (EQ (value, Qwave))
5718 face->underline_type = FACE_UNDER_WAVE;
5723 overline = attrs[LFACE_OVERLINE_INDEX];
5724 if (STRINGP (overline))
5726 face->overline_color
5727 = load_color (f, face, attrs[LFACE_OVERLINE_INDEX],
5728 LFACE_OVERLINE_INDEX);
5729 face->overline_p = 1;
5731 else if (EQ (overline, Qt))
5733 face->overline_color = face->foreground;
5734 face->overline_color_defaulted_p = 1;
5735 face->overline_p = 1;
5738 strike_through = attrs[LFACE_STRIKE_THROUGH_INDEX];
5739 if (STRINGP (strike_through))
5741 face->strike_through_color
5742 = load_color (f, face, attrs[LFACE_STRIKE_THROUGH_INDEX],
5743 LFACE_STRIKE_THROUGH_INDEX);
5744 face->strike_through_p = 1;
5746 else if (EQ (strike_through, Qt))
5748 face->strike_through_color = face->foreground;
5749 face->strike_through_color_defaulted_p = 1;
5750 face->strike_through_p = 1;
5753 stipple = attrs[LFACE_STIPPLE_INDEX];
5754 if (!NILP (stipple))
5755 face->stipple = load_pixmap (f, stipple);
5756 #endif /* HAVE_WINDOW_SYSTEM */
5758 return face;
5762 /* Map a specified color of face FACE on frame F to a tty color index.
5763 IDX is either LFACE_FOREGROUND_INDEX or LFACE_BACKGROUND_INDEX, and
5764 specifies which color to map. Set *DEFAULTED to 1 if mapping to the
5765 default foreground/background colors. */
5767 static void
5768 map_tty_color (struct frame *f, struct face *face,
5769 enum lface_attribute_index idx, int *defaulted)
5771 Lisp_Object frame, color, def;
5772 int foreground_p = idx == LFACE_FOREGROUND_INDEX;
5773 unsigned long default_pixel =
5774 foreground_p ? FACE_TTY_DEFAULT_FG_COLOR : FACE_TTY_DEFAULT_BG_COLOR;
5775 unsigned long pixel = default_pixel;
5776 #ifdef MSDOS
5777 unsigned long default_other_pixel =
5778 foreground_p ? FACE_TTY_DEFAULT_BG_COLOR : FACE_TTY_DEFAULT_FG_COLOR;
5779 #endif
5781 eassert (idx == LFACE_FOREGROUND_INDEX || idx == LFACE_BACKGROUND_INDEX);
5783 XSETFRAME (frame, f);
5784 color = face->lface[idx];
5786 if (STRINGP (color)
5787 && SCHARS (color)
5788 && CONSP (Vtty_defined_color_alist)
5789 && (def = assq_no_quit (color, call1 (Qtty_color_alist, frame)),
5790 CONSP (def)))
5792 /* Associations in tty-defined-color-alist are of the form
5793 (NAME INDEX R G B). We need the INDEX part. */
5794 pixel = XINT (XCAR (XCDR (def)));
5797 if (pixel == default_pixel && STRINGP (color))
5799 pixel = load_color (f, face, color, idx);
5801 #ifdef MSDOS
5802 /* If the foreground of the default face is the default color,
5803 use the foreground color defined by the frame. */
5804 if (FRAME_MSDOS_P (f))
5806 if (pixel == default_pixel
5807 || pixel == FACE_TTY_DEFAULT_COLOR)
5809 if (foreground_p)
5810 pixel = FRAME_FOREGROUND_PIXEL (f);
5811 else
5812 pixel = FRAME_BACKGROUND_PIXEL (f);
5813 face->lface[idx] = tty_color_name (f, pixel);
5814 *defaulted = 1;
5816 else if (pixel == default_other_pixel)
5818 if (foreground_p)
5819 pixel = FRAME_BACKGROUND_PIXEL (f);
5820 else
5821 pixel = FRAME_FOREGROUND_PIXEL (f);
5822 face->lface[idx] = tty_color_name (f, pixel);
5823 *defaulted = 1;
5826 #endif /* MSDOS */
5829 if (foreground_p)
5830 face->foreground = pixel;
5831 else
5832 face->background = pixel;
5836 /* Realize the fully-specified face with attributes ATTRS in face
5837 cache CACHE for ASCII characters. Do it for TTY frame CACHE->f.
5838 Value is a pointer to the newly created realized face. */
5840 static struct face *
5841 realize_tty_face (struct face_cache *cache,
5842 Lisp_Object attrs[LFACE_VECTOR_SIZE])
5844 struct face *face;
5845 int weight, slant;
5846 int face_colors_defaulted = 0;
5847 struct frame *f = cache->f;
5849 /* Frame must be a termcap frame. */
5850 eassert (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f));
5852 /* Allocate a new realized face. */
5853 face = make_realized_face (attrs);
5854 #if 0
5855 face->font_name = FRAME_MSDOS_P (cache->f) ? "ms-dos" : "tty";
5856 #endif
5858 /* Map face attributes to TTY appearances. */
5859 weight = FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]);
5860 slant = FONT_SLANT_NAME_NUMERIC (attrs[LFACE_SLANT_INDEX]);
5861 if (weight > 100)
5862 face->tty_bold_p = 1;
5863 if (slant != 100)
5864 face->tty_italic_p = 1;
5865 if (!NILP (attrs[LFACE_UNDERLINE_INDEX]))
5866 face->tty_underline_p = 1;
5867 if (!NILP (attrs[LFACE_INVERSE_INDEX]))
5868 face->tty_reverse_p = 1;
5870 /* Map color names to color indices. */
5871 map_tty_color (f, face, LFACE_FOREGROUND_INDEX, &face_colors_defaulted);
5872 map_tty_color (f, face, LFACE_BACKGROUND_INDEX, &face_colors_defaulted);
5874 /* Swap colors if face is inverse-video. If the colors are taken
5875 from the frame colors, they are already inverted, since the
5876 frame-creation function calls x-handle-reverse-video. */
5877 if (face->tty_reverse_p && !face_colors_defaulted)
5879 unsigned long tem = face->foreground;
5880 face->foreground = face->background;
5881 face->background = tem;
5884 if (tty_suppress_bold_inverse_default_colors_p
5885 && face->tty_bold_p
5886 && face->background == FACE_TTY_DEFAULT_FG_COLOR
5887 && face->foreground == FACE_TTY_DEFAULT_BG_COLOR)
5888 face->tty_bold_p = 0;
5890 return face;
5894 DEFUN ("tty-suppress-bold-inverse-default-colors",
5895 Ftty_suppress_bold_inverse_default_colors,
5896 Stty_suppress_bold_inverse_default_colors, 1, 1, 0,
5897 doc: /* Suppress/allow boldness of faces with inverse default colors.
5898 SUPPRESS non-nil means suppress it.
5899 This affects bold faces on TTYs whose foreground is the default background
5900 color of the display and whose background is the default foreground color.
5901 For such faces, the bold face attribute is ignored if this variable
5902 is non-nil. */)
5903 (Lisp_Object suppress)
5905 tty_suppress_bold_inverse_default_colors_p = !NILP (suppress);
5906 ++face_change_count;
5907 return suppress;
5912 /***********************************************************************
5913 Computing Faces
5914 ***********************************************************************/
5916 /* Return the ID of the face to use to display character CH with face
5917 property PROP on frame F in current_buffer. */
5920 compute_char_face (struct frame *f, int ch, Lisp_Object prop)
5922 int face_id;
5924 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
5925 ch = 0;
5927 if (NILP (prop))
5929 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5930 face_id = FACE_FOR_CHAR (f, face, ch, -1, Qnil);
5932 else
5934 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5935 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5936 memcpy (attrs, default_face->lface, sizeof attrs);
5937 merge_face_ref (f, prop, attrs, 1, 0);
5938 face_id = lookup_face (f, attrs);
5941 return face_id;
5944 /* Return the face ID associated with buffer position POS for
5945 displaying ASCII characters. Return in *ENDPTR the position at
5946 which a different face is needed, as far as text properties and
5947 overlays are concerned. W is a window displaying current_buffer.
5949 REGION_BEG, REGION_END delimit the region, so it can be
5950 highlighted.
5952 LIMIT is a position not to scan beyond. That is to limit the time
5953 this function can take.
5955 If MOUSE is non-zero, use the character's mouse-face, not its face.
5957 BASE_FACE_ID, if non-negative, specifies a base face id to use
5958 instead of DEFAULT_FACE_ID.
5960 The face returned is suitable for displaying ASCII characters. */
5963 face_at_buffer_position (struct window *w, ptrdiff_t pos,
5964 ptrdiff_t *endptr, ptrdiff_t limit,
5965 int mouse, int base_face_id)
5967 struct frame *f = XFRAME (w->frame);
5968 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5969 Lisp_Object prop, position;
5970 ptrdiff_t i, noverlays;
5971 Lisp_Object *overlay_vec;
5972 ptrdiff_t endpos;
5973 Lisp_Object propname = mouse ? Qmouse_face : Qface;
5974 Lisp_Object limit1, end;
5975 struct face *default_face;
5977 /* W must display the current buffer. We could write this function
5978 to use the frame and buffer of W, but right now it doesn't. */
5979 /* eassert (XBUFFER (w->contents) == current_buffer); */
5981 XSETFASTINT (position, pos);
5983 endpos = ZV;
5985 /* Get the `face' or `mouse_face' text property at POS, and
5986 determine the next position at which the property changes. */
5987 prop = Fget_text_property (position, propname, w->contents);
5988 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
5989 end = Fnext_single_property_change (position, propname, w->contents, limit1);
5990 if (INTEGERP (end))
5991 endpos = XINT (end);
5993 /* Look at properties from overlays. */
5995 ptrdiff_t next_overlay;
5997 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, &next_overlay, 0);
5998 if (next_overlay < endpos)
5999 endpos = next_overlay;
6002 *endptr = endpos;
6005 int face_id;
6007 if (base_face_id >= 0)
6008 face_id = base_face_id;
6009 else if (NILP (Vface_remapping_alist))
6010 face_id = DEFAULT_FACE_ID;
6011 else
6012 face_id = lookup_basic_face (f, DEFAULT_FACE_ID);
6014 default_face = FACE_FROM_ID (f, face_id);
6017 /* Optimize common cases where we can use the default face. */
6018 if (noverlays == 0
6019 && NILP (prop))
6020 return default_face->id;
6022 /* Begin with attributes from the default face. */
6023 memcpy (attrs, default_face->lface, sizeof attrs);
6025 /* Merge in attributes specified via text properties. */
6026 if (!NILP (prop))
6027 merge_face_ref (f, prop, attrs, 1, 0);
6029 /* Now merge the overlay data. */
6030 noverlays = sort_overlays (overlay_vec, noverlays, w);
6031 for (i = 0; i < noverlays; i++)
6033 Lisp_Object oend;
6034 ptrdiff_t oendpos;
6036 prop = Foverlay_get (overlay_vec[i], propname);
6037 if (!NILP (prop))
6038 merge_face_ref (f, prop, attrs, 1, 0);
6040 oend = OVERLAY_END (overlay_vec[i]);
6041 oendpos = OVERLAY_POSITION (oend);
6042 if (oendpos < endpos)
6043 endpos = oendpos;
6046 *endptr = endpos;
6048 /* Look up a realized face with the given face attributes,
6049 or realize a new one for ASCII characters. */
6050 return lookup_face (f, attrs);
6053 /* Return the face ID at buffer position POS for displaying ASCII
6054 characters associated with overlay strings for overlay OVERLAY.
6056 Like face_at_buffer_position except for OVERLAY. Currently it
6057 simply disregards the `face' properties of all overlays. */
6060 face_for_overlay_string (struct window *w, ptrdiff_t pos,
6061 ptrdiff_t *endptr, ptrdiff_t limit,
6062 int mouse, Lisp_Object overlay)
6064 struct frame *f = XFRAME (w->frame);
6065 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6066 Lisp_Object prop, position;
6067 ptrdiff_t endpos;
6068 Lisp_Object propname = mouse ? Qmouse_face : Qface;
6069 Lisp_Object limit1, end;
6070 struct face *default_face;
6072 /* W must display the current buffer. We could write this function
6073 to use the frame and buffer of W, but right now it doesn't. */
6074 /* eassert (XBUFFER (w->contents) == current_buffer); */
6076 XSETFASTINT (position, pos);
6078 endpos = ZV;
6080 /* Get the `face' or `mouse_face' text property at POS, and
6081 determine the next position at which the property changes. */
6082 prop = Fget_text_property (position, propname, w->contents);
6083 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
6084 end = Fnext_single_property_change (position, propname, w->contents, limit1);
6085 if (INTEGERP (end))
6086 endpos = XINT (end);
6088 *endptr = endpos;
6090 /* Optimize common case where we can use the default face. */
6091 if (NILP (prop)
6092 && NILP (Vface_remapping_alist))
6093 return DEFAULT_FACE_ID;
6095 /* Begin with attributes from the default face. */
6096 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
6097 memcpy (attrs, default_face->lface, sizeof attrs);
6099 /* Merge in attributes specified via text properties. */
6100 if (!NILP (prop))
6101 merge_face_ref (f, prop, attrs, 1, 0);
6103 *endptr = endpos;
6105 /* Look up a realized face with the given face attributes,
6106 or realize a new one for ASCII characters. */
6107 return lookup_face (f, attrs);
6111 /* Compute the face at character position POS in Lisp string STRING on
6112 window W, for ASCII characters.
6114 If STRING is an overlay string, it comes from position BUFPOS in
6115 current_buffer, otherwise BUFPOS is zero to indicate that STRING is
6116 not an overlay string. W must display the current buffer.
6117 REGION_BEG and REGION_END give the start and end positions of the
6118 region; both are -1 if no region is visible.
6120 BASE_FACE_ID is the id of a face to merge with. For strings coming
6121 from overlays or the `display' property it is the face at BUFPOS.
6123 If MOUSE_P is non-zero, use the character's mouse-face, not its face.
6125 Set *ENDPTR to the next position where to check for faces in
6126 STRING; -1 if the face is constant from POS to the end of the
6127 string.
6129 Value is the id of the face to use. The face returned is suitable
6130 for displaying ASCII characters. */
6133 face_at_string_position (struct window *w, Lisp_Object string,
6134 ptrdiff_t pos, ptrdiff_t bufpos,
6135 ptrdiff_t *endptr, enum face_id base_face_id,
6136 int mouse_p)
6138 Lisp_Object prop, position, end, limit;
6139 struct frame *f = XFRAME (WINDOW_FRAME (w));
6140 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6141 struct face *base_face;
6142 bool multibyte_p = STRING_MULTIBYTE (string);
6143 Lisp_Object prop_name = mouse_p ? Qmouse_face : Qface;
6145 /* Get the value of the face property at the current position within
6146 STRING. Value is nil if there is no face property. */
6147 XSETFASTINT (position, pos);
6148 prop = Fget_text_property (position, prop_name, string);
6150 /* Get the next position at which to check for faces. Value of end
6151 is nil if face is constant all the way to the end of the string.
6152 Otherwise it is a string position where to check faces next.
6153 Limit is the maximum position up to which to check for property
6154 changes in Fnext_single_property_change. Strings are usually
6155 short, so set the limit to the end of the string. */
6156 XSETFASTINT (limit, SCHARS (string));
6157 end = Fnext_single_property_change (position, prop_name, string, limit);
6158 if (INTEGERP (end))
6159 *endptr = XFASTINT (end);
6160 else
6161 *endptr = -1;
6163 base_face = FACE_FROM_ID (f, base_face_id);
6164 eassert (base_face);
6166 /* Optimize the default case that there is no face property. */
6167 if (NILP (prop)
6168 && (multibyte_p
6169 /* We can't realize faces for different charsets differently
6170 if we don't have fonts, so we can stop here if not working
6171 on a window-system frame. */
6172 || !FRAME_WINDOW_P (f)
6173 || FACE_SUITABLE_FOR_ASCII_CHAR_P (base_face, 0)))
6174 return base_face->id;
6176 /* Begin with attributes from the base face. */
6177 memcpy (attrs, base_face->lface, sizeof attrs);
6179 /* Merge in attributes specified via text properties. */
6180 if (!NILP (prop))
6181 merge_face_ref (f, prop, attrs, 1, 0);
6183 /* Look up a realized face with the given face attributes,
6184 or realize a new one for ASCII characters. */
6185 return lookup_face (f, attrs);
6189 /* Merge a face into a realized face.
6191 F is frame where faces are (to be) realized.
6193 FACE_NAME is named face to merge.
6195 If FACE_NAME is nil, FACE_ID is face_id of realized face to merge.
6197 If FACE_NAME is t, FACE_ID is lface_id of face to merge.
6199 BASE_FACE_ID is realized face to merge into.
6201 Return new face id.
6205 merge_faces (struct frame *f, Lisp_Object face_name, int face_id,
6206 int base_face_id)
6208 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6209 struct face *base_face;
6211 base_face = FACE_FROM_ID (f, base_face_id);
6212 if (!base_face)
6213 return base_face_id;
6215 if (EQ (face_name, Qt))
6217 if (face_id < 0 || face_id >= lface_id_to_name_size)
6218 return base_face_id;
6219 face_name = lface_id_to_name[face_id];
6220 /* When called during make-frame, lookup_derived_face may fail
6221 if the faces are uninitialized. Don't signal an error. */
6222 face_id = lookup_derived_face (f, face_name, base_face_id, 0);
6223 return (face_id >= 0 ? face_id : base_face_id);
6226 /* Begin with attributes from the base face. */
6227 memcpy (attrs, base_face->lface, sizeof attrs);
6229 if (!NILP (face_name))
6231 if (!merge_named_face (f, face_name, attrs, 0))
6232 return base_face_id;
6234 else
6236 struct face *face;
6237 if (face_id < 0)
6238 return base_face_id;
6239 face = FACE_FROM_ID (f, face_id);
6240 if (!face)
6241 return base_face_id;
6242 merge_face_vectors (f, face->lface, attrs, 0);
6245 /* Look up a realized face with the given face attributes,
6246 or realize a new one for ASCII characters. */
6247 return lookup_face (f, attrs);
6252 #ifndef HAVE_X_WINDOWS
6253 DEFUN ("x-load-color-file", Fx_load_color_file,
6254 Sx_load_color_file, 1, 1, 0,
6255 doc: /* Create an alist of color entries from an external file.
6257 The file should define one named RGB color per line like so:
6258 R G B name
6259 where R,G,B are numbers between 0 and 255 and name is an arbitrary string. */)
6260 (Lisp_Object filename)
6262 FILE *fp;
6263 Lisp_Object cmap = Qnil;
6264 Lisp_Object abspath;
6266 CHECK_STRING (filename);
6267 abspath = Fexpand_file_name (filename, Qnil);
6269 block_input ();
6270 fp = emacs_fopen (SSDATA (abspath), "rt");
6271 if (fp)
6273 char buf[512];
6274 int red, green, blue;
6275 int num;
6277 while (fgets (buf, sizeof (buf), fp) != NULL) {
6278 if (sscanf (buf, "%u %u %u %n", &red, &green, &blue, &num) == 3)
6280 #ifdef HAVE_NTGUI
6281 int color = RGB (red, green, blue);
6282 #else
6283 int color = (red << 16) | (green << 8) | blue;
6284 #endif
6285 char *name = buf + num;
6286 ptrdiff_t len = strlen (name);
6287 len -= 0 < len && name[len - 1] == '\n';
6288 cmap = Fcons (Fcons (make_string (name, len), make_number (color)),
6289 cmap);
6292 fclose (fp);
6294 unblock_input ();
6295 return cmap;
6297 #endif
6300 /***********************************************************************
6301 Tests
6302 ***********************************************************************/
6304 #ifdef GLYPH_DEBUG
6306 /* Print the contents of the realized face FACE to stderr. */
6308 static void
6309 dump_realized_face (struct face *face)
6311 fprintf (stderr, "ID: %d\n", face->id);
6312 #ifdef HAVE_X_WINDOWS
6313 fprintf (stderr, "gc: %ld\n", (long) face->gc);
6314 #endif
6315 fprintf (stderr, "foreground: 0x%lx (%s)\n",
6316 face->foreground,
6317 SDATA (face->lface[LFACE_FOREGROUND_INDEX]));
6318 fprintf (stderr, "background: 0x%lx (%s)\n",
6319 face->background,
6320 SDATA (face->lface[LFACE_BACKGROUND_INDEX]));
6321 if (face->font)
6322 fprintf (stderr, "font_name: %s (%s)\n",
6323 SDATA (face->font->props[FONT_NAME_INDEX]),
6324 SDATA (face->lface[LFACE_FAMILY_INDEX]));
6325 #ifdef HAVE_X_WINDOWS
6326 fprintf (stderr, "font = %p\n", face->font);
6327 #endif
6328 fprintf (stderr, "fontset: %d\n", face->fontset);
6329 fprintf (stderr, "underline: %d (%s)\n",
6330 face->underline_p,
6331 SDATA (Fsymbol_name (face->lface[LFACE_UNDERLINE_INDEX])));
6332 fprintf (stderr, "hash: %d\n", face->hash);
6336 DEFUN ("dump-face", Fdump_face, Sdump_face, 0, 1, 0, doc: /* */)
6337 (Lisp_Object n)
6339 if (NILP (n))
6341 int i;
6343 fprintf (stderr, "font selection order: ");
6344 for (i = 0; i < DIM (font_sort_order); ++i)
6345 fprintf (stderr, "%d ", font_sort_order[i]);
6346 fprintf (stderr, "\n");
6348 fprintf (stderr, "alternative fonts: ");
6349 debug_print (Vface_alternative_font_family_alist);
6350 fprintf (stderr, "\n");
6352 for (i = 0; i < FRAME_FACE_CACHE (SELECTED_FRAME ())->used; ++i)
6353 Fdump_face (make_number (i));
6355 else
6357 struct face *face;
6358 CHECK_NUMBER (n);
6359 face = FACE_FROM_ID (SELECTED_FRAME (), XINT (n));
6360 if (face == NULL)
6361 error ("Not a valid face");
6362 dump_realized_face (face);
6365 return Qnil;
6369 DEFUN ("show-face-resources", Fshow_face_resources, Sshow_face_resources,
6370 0, 0, 0, doc: /* */)
6371 (void)
6373 fprintf (stderr, "number of colors = %d\n", ncolors_allocated);
6374 fprintf (stderr, "number of pixmaps = %d\n", npixmaps_allocated);
6375 fprintf (stderr, "number of GCs = %d\n", ngcs);
6376 return Qnil;
6379 #endif /* GLYPH_DEBUG */
6383 /***********************************************************************
6384 Initialization
6385 ***********************************************************************/
6387 void
6388 syms_of_xfaces (void)
6390 DEFSYM (Qface, "face");
6391 DEFSYM (Qface_no_inherit, "face-no-inherit");
6392 DEFSYM (Qbitmap_spec_p, "bitmap-spec-p");
6393 DEFSYM (Qframe_set_background_mode, "frame-set-background-mode");
6395 /* Lisp face attribute keywords. */
6396 DEFSYM (QCfamily, ":family");
6397 DEFSYM (QCheight, ":height");
6398 DEFSYM (QCweight, ":weight");
6399 DEFSYM (QCslant, ":slant");
6400 DEFSYM (QCunderline, ":underline");
6401 DEFSYM (QCinverse_video, ":inverse-video");
6402 DEFSYM (QCreverse_video, ":reverse-video");
6403 DEFSYM (QCforeground, ":foreground");
6404 DEFSYM (QCbackground, ":background");
6405 DEFSYM (QCstipple, ":stipple");
6406 DEFSYM (QCwidth, ":width");
6407 DEFSYM (QCfont, ":font");
6408 DEFSYM (QCfontset, ":fontset");
6409 DEFSYM (QCdistant_foreground, ":distant-foreground");
6410 DEFSYM (QCbold, ":bold");
6411 DEFSYM (QCitalic, ":italic");
6412 DEFSYM (QCoverline, ":overline");
6413 DEFSYM (QCstrike_through, ":strike-through");
6414 DEFSYM (QCbox, ":box");
6415 DEFSYM (QCinherit, ":inherit");
6417 /* Symbols used for Lisp face attribute values. */
6418 DEFSYM (QCcolor, ":color");
6419 DEFSYM (QCline_width, ":line-width");
6420 DEFSYM (QCstyle, ":style");
6421 DEFSYM (Qline, "line");
6422 DEFSYM (Qwave, "wave");
6423 DEFSYM (Qreleased_button, "released-button");
6424 DEFSYM (Qpressed_button, "pressed-button");
6425 DEFSYM (Qnormal, "normal");
6426 DEFSYM (Qextra_light, "extra-light");
6427 DEFSYM (Qlight, "light");
6428 DEFSYM (Qsemi_light, "semi-light");
6429 DEFSYM (Qsemi_bold, "semi-bold");
6430 DEFSYM (Qbold, "bold");
6431 DEFSYM (Qextra_bold, "extra-bold");
6432 DEFSYM (Qultra_bold, "ultra-bold");
6433 DEFSYM (Qoblique, "oblique");
6434 DEFSYM (Qitalic, "italic");
6435 DEFSYM (Qbackground_color, "background-color");
6436 DEFSYM (Qforeground_color, "foreground-color");
6437 DEFSYM (Qunspecified, "unspecified");
6438 DEFSYM (QCignore_defface, ":ignore-defface");
6440 DEFSYM (Qface_alias, "face-alias");
6441 DEFSYM (Qdefault, "default");
6442 DEFSYM (Qtool_bar, "tool-bar");
6443 DEFSYM (Qregion, "region");
6444 DEFSYM (Qfringe, "fringe");
6445 DEFSYM (Qheader_line, "header-line");
6446 DEFSYM (Qscroll_bar, "scroll-bar");
6447 DEFSYM (Qmenu, "menu");
6448 DEFSYM (Qcursor, "cursor");
6449 DEFSYM (Qborder, "border");
6450 DEFSYM (Qmouse, "mouse");
6451 DEFSYM (Qmode_line_inactive, "mode-line-inactive");
6452 DEFSYM (Qvertical_border, "vertical-border");
6453 DEFSYM (Qwindow_divider, "window-divider");
6454 DEFSYM (Qtty_color_desc, "tty-color-desc");
6455 DEFSYM (Qtty_color_standard_values, "tty-color-standard-values");
6456 DEFSYM (Qtty_color_by_index, "tty-color-by-index");
6457 DEFSYM (Qtty_color_alist, "tty-color-alist");
6458 DEFSYM (Qscalable_fonts_allowed, "scalable-fonts-allowed");
6460 Vparam_value_alist = list1 (Fcons (Qnil, Qnil));
6461 staticpro (&Vparam_value_alist);
6462 Vface_alternative_font_family_alist = Qnil;
6463 staticpro (&Vface_alternative_font_family_alist);
6464 Vface_alternative_font_registry_alist = Qnil;
6465 staticpro (&Vface_alternative_font_registry_alist);
6467 defsubr (&Sinternal_make_lisp_face);
6468 defsubr (&Sinternal_lisp_face_p);
6469 defsubr (&Sinternal_set_lisp_face_attribute);
6470 #ifdef HAVE_WINDOW_SYSTEM
6471 defsubr (&Sinternal_set_lisp_face_attribute_from_resource);
6472 #endif
6473 defsubr (&Scolor_gray_p);
6474 defsubr (&Scolor_supported_p);
6475 #ifndef HAVE_X_WINDOWS
6476 defsubr (&Sx_load_color_file);
6477 #endif
6478 defsubr (&Sface_attribute_relative_p);
6479 defsubr (&Smerge_face_attribute);
6480 defsubr (&Sinternal_get_lisp_face_attribute);
6481 defsubr (&Sinternal_lisp_face_attribute_values);
6482 defsubr (&Sinternal_lisp_face_equal_p);
6483 defsubr (&Sinternal_lisp_face_empty_p);
6484 defsubr (&Sinternal_copy_lisp_face);
6485 defsubr (&Sinternal_merge_in_global_face);
6486 defsubr (&Sface_font);
6487 defsubr (&Sframe_face_alist);
6488 defsubr (&Sdisplay_supports_face_attributes_p);
6489 defsubr (&Scolor_distance);
6490 defsubr (&Sinternal_set_font_selection_order);
6491 defsubr (&Sinternal_set_alternative_font_family_alist);
6492 defsubr (&Sinternal_set_alternative_font_registry_alist);
6493 defsubr (&Sface_attributes_as_vector);
6494 #ifdef GLYPH_DEBUG
6495 defsubr (&Sdump_face);
6496 defsubr (&Sshow_face_resources);
6497 #endif /* GLYPH_DEBUG */
6498 defsubr (&Sclear_face_cache);
6499 defsubr (&Stty_suppress_bold_inverse_default_colors);
6501 #if defined DEBUG_X_COLORS && defined HAVE_X_WINDOWS
6502 defsubr (&Sdump_colors);
6503 #endif
6505 DEFVAR_LISP ("face-new-frame-defaults", Vface_new_frame_defaults,
6506 doc: /* List of global face definitions (for internal use only.) */);
6507 Vface_new_frame_defaults = Qnil;
6509 DEFVAR_LISP ("face-default-stipple", Vface_default_stipple,
6510 doc: /* Default stipple pattern used on monochrome displays.
6511 This stipple pattern is used on monochrome displays
6512 instead of shades of gray for a face background color.
6513 See `set-face-stipple' for possible values for this variable. */);
6514 Vface_default_stipple = build_pure_c_string ("gray3");
6516 DEFVAR_LISP ("tty-defined-color-alist", Vtty_defined_color_alist,
6517 doc: /* An alist of defined terminal colors and their RGB values.
6518 See the docstring of `tty-color-alist' for the details. */);
6519 Vtty_defined_color_alist = Qnil;
6521 DEFVAR_LISP ("scalable-fonts-allowed", Vscalable_fonts_allowed,
6522 doc: /* Allowed scalable fonts.
6523 A value of nil means don't allow any scalable fonts.
6524 A value of t means allow any scalable font.
6525 Otherwise, value must be a list of regular expressions. A font may be
6526 scaled if its name matches a regular expression in the list.
6527 Note that if value is nil, a scalable font might still be used, if no
6528 other font of the appropriate family and registry is available. */);
6529 Vscalable_fonts_allowed = Qnil;
6531 DEFVAR_LISP ("face-ignored-fonts", Vface_ignored_fonts,
6532 doc: /* List of ignored fonts.
6533 Each element is a regular expression that matches names of fonts to
6534 ignore. */);
6535 Vface_ignored_fonts = Qnil;
6537 DEFVAR_LISP ("face-remapping-alist", Vface_remapping_alist,
6538 doc: /* Alist of face remappings.
6539 Each element is of the form:
6541 (FACE . REPLACEMENT),
6543 which causes display of the face FACE to use REPLACEMENT instead.
6544 REPLACEMENT is a face specification, i.e. one of the following:
6546 (1) a face name
6547 (2) a property list of attribute/value pairs, or
6548 (3) a list in which each element has the form of (1) or (2).
6550 List values for REPLACEMENT are merged to form the final face
6551 specification, with earlier entries taking precedence, in the same as
6552 as in the `face' text property.
6554 Face-name remapping cycles are suppressed; recursive references use
6555 the underlying face instead of the remapped face. So a remapping of
6556 the form:
6558 (FACE EXTRA-FACE... FACE)
6562 (FACE (FACE-ATTR VAL ...) FACE)
6564 causes EXTRA-FACE... or (FACE-ATTR VAL ...) to be _merged_ with the
6565 existing definition of FACE. Note that this isn't necessary for the
6566 default face, since every face inherits from the default face.
6568 If this variable is made buffer-local, the face remapping takes effect
6569 only in that buffer. For instance, the mode my-mode could define a
6570 face `my-mode-default', and then in the mode setup function, do:
6572 (set (make-local-variable 'face-remapping-alist)
6573 '((default my-mode-default)))).
6575 Because Emacs normally only redraws screen areas when the underlying
6576 buffer contents change, you may need to call `redraw-display' after
6577 changing this variable for it to take effect. */);
6578 Vface_remapping_alist = Qnil;
6580 DEFVAR_LISP ("face-font-rescale-alist", Vface_font_rescale_alist,
6581 doc: /* Alist of fonts vs the rescaling factors.
6582 Each element is a cons (FONT-PATTERN . RESCALE-RATIO), where
6583 FONT-PATTERN is a font-spec or a regular expression matching a font name, and
6584 RESCALE-RATIO is a floating point number to specify how much larger
6585 \(or smaller) font we should use. For instance, if a face requests
6586 a font of 10 point, we actually use a font of 10 * RESCALE-RATIO point. */);
6587 Vface_font_rescale_alist = Qnil;
6589 #ifdef HAVE_WINDOW_SYSTEM
6590 defsubr (&Sbitmap_spec_p);
6591 defsubr (&Sx_list_fonts);
6592 defsubr (&Sinternal_face_x_get_resource);
6593 defsubr (&Sx_family_fonts);
6594 #endif