Some desktop doc.
[emacs.git] / src / xfaces.c
blob4271e47c36f8eb72cfed1295f4734ab7f8cf5583
1 /* xfaces.c -- "Face" primitives.
3 Copyright (C) 1993-1994, 1998-2014 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;
327 static Lisp_Object Qwindow_divider_first_pixel;
328 static Lisp_Object Qwindow_divider_last_pixel;
330 /* The symbol `face-alias'. A symbols having that property is an
331 alias for another face. Value of the property is the name of
332 the aliased face. */
334 static Lisp_Object Qface_alias;
336 /* Alist of alternative font families. Each element is of the form
337 (FAMILY FAMILY1 FAMILY2 ...). If fonts of FAMILY can't be loaded,
338 try FAMILY1, then FAMILY2, ... */
340 Lisp_Object Vface_alternative_font_family_alist;
342 /* Alist of alternative font registries. Each element is of the form
343 (REGISTRY REGISTRY1 REGISTRY2...). If fonts of REGISTRY can't be
344 loaded, try REGISTRY1, then REGISTRY2, ... */
346 Lisp_Object Vface_alternative_font_registry_alist;
348 /* Allowed scalable fonts. A value of nil means don't allow any
349 scalable fonts. A value of t means allow the use of any scalable
350 font. Otherwise, value must be a list of regular expressions. A
351 font may be scaled if its name matches a regular expression in the
352 list. */
354 static Lisp_Object Qscalable_fonts_allowed;
356 /* The symbols `foreground-color' and `background-color' which can be
357 used as part of a `face' property. This is for compatibility with
358 Emacs 20.2. */
360 Lisp_Object Qforeground_color, Qbackground_color;
362 /* The symbols `face' and `mouse-face' used as text properties. */
364 Lisp_Object Qface;
366 /* Property for basic faces which other faces cannot inherit. */
368 static Lisp_Object Qface_no_inherit;
370 /* Error symbol for wrong_type_argument in load_pixmap. */
372 static Lisp_Object Qbitmap_spec_p;
374 /* The next ID to assign to Lisp faces. */
376 static int next_lface_id;
378 /* A vector mapping Lisp face Id's to face names. */
380 static Lisp_Object *lface_id_to_name;
381 static ptrdiff_t lface_id_to_name_size;
383 /* TTY color-related functions (defined in tty-colors.el). */
385 static Lisp_Object Qtty_color_desc, Qtty_color_by_index, Qtty_color_standard_values;
387 /* The name of the function used to compute colors on TTYs. */
389 static Lisp_Object Qtty_color_alist;
391 #ifdef HAVE_WINDOW_SYSTEM
393 /* Counter for calls to clear_face_cache. If this counter reaches
394 CLEAR_FONT_TABLE_COUNT, and a frame has more than
395 CLEAR_FONT_TABLE_NFONTS load, unused fonts are freed. */
397 static int clear_font_table_count;
398 #define CLEAR_FONT_TABLE_COUNT 100
399 #define CLEAR_FONT_TABLE_NFONTS 10
401 #endif /* HAVE_WINDOW_SYSTEM */
403 /* Non-zero means face attributes have been changed since the last
404 redisplay. Used in redisplay_internal. */
406 int face_change_count;
408 /* Non-zero means don't display bold text if a face's foreground
409 and background colors are the inverse of the default colors of the
410 display. This is a kluge to suppress `bold black' foreground text
411 which is hard to read on an LCD monitor. */
413 static int tty_suppress_bold_inverse_default_colors_p;
415 /* A list of the form `((x . y))' used to avoid consing in
416 Finternal_set_lisp_face_attribute. */
418 static Lisp_Object Vparam_value_alist;
420 /* The total number of colors currently allocated. */
422 #ifdef GLYPH_DEBUG
423 static int ncolors_allocated;
424 static int npixmaps_allocated;
425 static int ngcs;
426 #endif
428 /* Non-zero means the definition of the `menu' face for new frames has
429 been changed. */
431 static int menu_face_changed_default;
433 struct named_merge_point;
435 static struct face *realize_face (struct face_cache *, Lisp_Object *,
436 int);
437 static struct face *realize_x_face (struct face_cache *, Lisp_Object *);
438 static struct face *realize_tty_face (struct face_cache *, Lisp_Object *);
439 static bool realize_basic_faces (struct frame *);
440 static bool realize_default_face (struct frame *);
441 static void realize_named_face (struct frame *, Lisp_Object, int);
442 static struct face_cache *make_face_cache (struct frame *);
443 static void free_face_cache (struct face_cache *);
444 static int merge_face_ref (struct frame *, Lisp_Object, Lisp_Object *,
445 int, struct named_merge_point *);
446 static int color_distance (XColor *x, XColor *y);
448 #ifdef HAVE_WINDOW_SYSTEM
449 static void set_font_frame_param (Lisp_Object, Lisp_Object);
450 static void clear_face_gcs (struct face_cache *);
451 static struct face *realize_non_ascii_face (struct frame *, Lisp_Object,
452 struct face *);
453 #endif /* HAVE_WINDOW_SYSTEM */
455 /***********************************************************************
456 Utilities
457 ***********************************************************************/
459 #ifdef HAVE_X_WINDOWS
461 #ifdef DEBUG_X_COLORS
463 /* The following is a poor mans infrastructure for debugging X color
464 allocation problems on displays with PseudoColor-8. Some X servers
465 like 3.3.5 XF86_SVGA with Matrox cards apparently don't implement
466 color reference counts completely so that they don't signal an
467 error when a color is freed whose reference count is already 0.
468 Other X servers do. To help me debug this, the following code
469 implements a simple reference counting schema of its own, for a
470 single display/screen. --gerd. */
472 /* Reference counts for pixel colors. */
474 int color_count[256];
476 /* Register color PIXEL as allocated. */
478 void
479 register_color (unsigned long pixel)
481 eassert (pixel < 256);
482 ++color_count[pixel];
486 /* Register color PIXEL as deallocated. */
488 void
489 unregister_color (unsigned long pixel)
491 eassert (pixel < 256);
492 if (color_count[pixel] > 0)
493 --color_count[pixel];
494 else
495 emacs_abort ();
499 /* Register N colors from PIXELS as deallocated. */
501 void
502 unregister_colors (unsigned long *pixels, int n)
504 int i;
505 for (i = 0; i < n; ++i)
506 unregister_color (pixels[i]);
510 DEFUN ("dump-colors", Fdump_colors, Sdump_colors, 0, 0, 0,
511 doc: /* Dump currently allocated colors to stderr. */)
512 (void)
514 int i, n;
516 fputc ('\n', stderr);
518 for (i = n = 0; i < sizeof color_count / sizeof color_count[0]; ++i)
519 if (color_count[i])
521 fprintf (stderr, "%3d: %5d", i, color_count[i]);
522 ++n;
523 if (n % 5 == 0)
524 fputc ('\n', stderr);
525 else
526 fputc ('\t', stderr);
529 if (n % 5 != 0)
530 fputc ('\n', stderr);
531 return Qnil;
534 #endif /* DEBUG_X_COLORS */
537 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
538 color values. Interrupt input must be blocked when this function
539 is called. */
541 void
542 x_free_colors (struct frame *f, unsigned long *pixels, int npixels)
544 int class = FRAME_DISPLAY_INFO (f)->visual->class;
546 /* If display has an immutable color map, freeing colors is not
547 necessary and some servers don't allow it. So don't do it. */
548 if (class != StaticColor && class != StaticGray && class != TrueColor)
550 #ifdef DEBUG_X_COLORS
551 unregister_colors (pixels, npixels);
552 #endif
553 XFreeColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
554 pixels, npixels, 0);
559 #ifdef USE_X_TOOLKIT
561 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
562 color values. Interrupt input must be blocked when this function
563 is called. */
565 void
566 x_free_dpy_colors (Display *dpy, Screen *screen, Colormap cmap,
567 unsigned long *pixels, int npixels)
569 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
570 int class = dpyinfo->visual->class;
572 /* If display has an immutable color map, freeing colors is not
573 necessary and some servers don't allow it. So don't do it. */
574 if (class != StaticColor && class != StaticGray && class != TrueColor)
576 #ifdef DEBUG_X_COLORS
577 unregister_colors (pixels, npixels);
578 #endif
579 XFreeColors (dpy, cmap, pixels, npixels, 0);
582 #endif /* USE_X_TOOLKIT */
584 /* Create and return a GC for use on frame F. GC values and mask
585 are given by XGCV and MASK. */
587 static GC
588 x_create_gc (struct frame *f, unsigned long mask, XGCValues *xgcv)
590 GC gc;
591 block_input ();
592 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), mask, xgcv);
593 unblock_input ();
594 IF_DEBUG (++ngcs);
595 return gc;
599 /* Free GC which was used on frame F. */
601 static void
602 x_free_gc (struct frame *f, GC gc)
604 eassert (input_blocked_p ());
605 IF_DEBUG ((--ngcs, eassert (ngcs >= 0)));
606 XFreeGC (FRAME_X_DISPLAY (f), gc);
609 #endif /* HAVE_X_WINDOWS */
611 #ifdef HAVE_NTGUI
612 /* W32 emulation of GCs */
614 static GC
615 x_create_gc (struct frame *f, unsigned long mask, XGCValues *xgcv)
617 GC gc;
618 block_input ();
619 gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, xgcv);
620 unblock_input ();
621 IF_DEBUG (++ngcs);
622 return gc;
626 /* Free GC which was used on frame F. */
628 static void
629 x_free_gc (struct frame *f, GC gc)
631 IF_DEBUG ((--ngcs, eassert (ngcs >= 0)));
632 xfree (gc);
635 #endif /* HAVE_NTGUI */
637 #ifdef HAVE_NS
638 /* NS emulation of GCs */
640 static GC
641 x_create_gc (struct frame *f,
642 unsigned long mask,
643 XGCValues *xgcv)
645 GC gc = xmalloc (sizeof *gc);
646 *gc = *xgcv;
647 return gc;
650 static void
651 x_free_gc (struct frame *f, GC gc)
653 xfree (gc);
655 #endif /* HAVE_NS */
657 /***********************************************************************
658 Frames and faces
659 ***********************************************************************/
661 /* Initialize face cache and basic faces for frame F. */
663 void
664 init_frame_faces (struct frame *f)
666 /* Make a face cache, if F doesn't have one. */
667 if (FRAME_FACE_CACHE (f) == NULL)
668 FRAME_FACE_CACHE (f) = make_face_cache (f);
670 #ifdef HAVE_WINDOW_SYSTEM
671 /* Make the image cache. */
672 if (FRAME_WINDOW_P (f))
674 /* We initialize the image cache when creating the first frame
675 on a terminal, and not during terminal creation. This way,
676 `x-open-connection' on a tty won't create an image cache. */
677 if (FRAME_IMAGE_CACHE (f) == NULL)
678 FRAME_IMAGE_CACHE (f) = make_image_cache ();
679 ++FRAME_IMAGE_CACHE (f)->refcount;
681 #endif /* HAVE_WINDOW_SYSTEM */
683 /* Realize basic faces. Must have enough information in frame
684 parameters to realize basic faces at this point. */
685 #ifdef HAVE_X_WINDOWS
686 if (!FRAME_X_P (f) || FRAME_X_WINDOW (f))
687 #endif
688 #ifdef HAVE_NTGUI
689 if (!FRAME_WINDOW_P (f) || FRAME_W32_WINDOW (f))
690 #endif
691 #ifdef HAVE_NS
692 if (!FRAME_NS_P (f) || FRAME_NS_WINDOW (f))
693 #endif
694 if (!realize_basic_faces (f))
695 emacs_abort ();
699 /* Free face cache of frame F. Called from frame-dependent
700 resource freeing function, e.g. (x|tty)_free_frame_resources. */
702 void
703 free_frame_faces (struct frame *f)
705 struct face_cache *face_cache = FRAME_FACE_CACHE (f);
707 if (face_cache)
709 free_face_cache (face_cache);
710 FRAME_FACE_CACHE (f) = NULL;
713 #ifdef HAVE_WINDOW_SYSTEM
714 if (FRAME_WINDOW_P (f))
716 struct image_cache *image_cache = FRAME_IMAGE_CACHE (f);
717 if (image_cache)
719 --image_cache->refcount;
720 if (image_cache->refcount == 0)
721 free_image_cache (f);
724 #endif /* HAVE_WINDOW_SYSTEM */
728 /* Clear face caches, and recompute basic faces for frame F. Call
729 this after changing frame parameters on which those faces depend,
730 or when realized faces have been freed due to changing attributes
731 of named faces. */
733 void
734 recompute_basic_faces (struct frame *f)
736 if (FRAME_FACE_CACHE (f))
738 clear_face_cache (0);
739 if (!realize_basic_faces (f))
740 emacs_abort ();
745 /* Clear the face caches of all frames. CLEAR_FONTS_P non-zero means
746 try to free unused fonts, too. */
748 void
749 clear_face_cache (int clear_fonts_p)
751 #ifdef HAVE_WINDOW_SYSTEM
752 Lisp_Object tail, frame;
754 if (clear_fonts_p
755 || ++clear_font_table_count == CLEAR_FONT_TABLE_COUNT)
757 /* From time to time see if we can unload some fonts. This also
758 frees all realized faces on all frames. Fonts needed by
759 faces will be loaded again when faces are realized again. */
760 clear_font_table_count = 0;
762 FOR_EACH_FRAME (tail, frame)
764 struct frame *f = XFRAME (frame);
765 if (FRAME_WINDOW_P (f)
766 && FRAME_DISPLAY_INFO (f)->n_fonts > CLEAR_FONT_TABLE_NFONTS)
768 clear_font_cache (f);
769 free_all_realized_faces (frame);
773 else
775 /* Clear GCs of realized faces. */
776 FOR_EACH_FRAME (tail, frame)
778 struct frame *f = XFRAME (frame);
779 if (FRAME_WINDOW_P (f))
780 clear_face_gcs (FRAME_FACE_CACHE (f));
782 clear_image_caches (Qnil);
784 #endif /* HAVE_WINDOW_SYSTEM */
788 DEFUN ("clear-face-cache", Fclear_face_cache, Sclear_face_cache, 0, 1, 0,
789 doc: /* Clear face caches on all frames.
790 Optional THOROUGHLY non-nil means try to free unused fonts, too. */)
791 (Lisp_Object thoroughly)
793 clear_face_cache (!NILP (thoroughly));
794 ++face_change_count;
795 windows_or_buffers_changed = 53;
796 return Qnil;
800 /***********************************************************************
801 X Pixmaps
802 ***********************************************************************/
804 #ifdef HAVE_WINDOW_SYSTEM
806 DEFUN ("bitmap-spec-p", Fbitmap_spec_p, Sbitmap_spec_p, 1, 1, 0,
807 doc: /* Value is non-nil if OBJECT is a valid bitmap specification.
808 A bitmap specification is either a string, a file name, or a list
809 \(WIDTH HEIGHT DATA) where WIDTH is the pixel width of the bitmap,
810 HEIGHT is its height, and DATA is a string containing the bits of
811 the pixmap. Bits are stored row by row, each row occupies
812 \(WIDTH + 7)/8 bytes. */)
813 (Lisp_Object object)
815 bool pixmap_p = 0;
817 if (STRINGP (object))
818 /* If OBJECT is a string, it's a file name. */
819 pixmap_p = 1;
820 else if (CONSP (object))
822 /* Otherwise OBJECT must be (WIDTH HEIGHT DATA), WIDTH and
823 HEIGHT must be ints > 0, and DATA must be string large
824 enough to hold a bitmap of the specified size. */
825 Lisp_Object width, height, data;
827 height = width = data = Qnil;
829 if (CONSP (object))
831 width = XCAR (object);
832 object = XCDR (object);
833 if (CONSP (object))
835 height = XCAR (object);
836 object = XCDR (object);
837 if (CONSP (object))
838 data = XCAR (object);
842 if (STRINGP (data)
843 && RANGED_INTEGERP (1, width, INT_MAX)
844 && RANGED_INTEGERP (1, height, INT_MAX))
846 int bytes_per_row = ((XINT (width) + BITS_PER_CHAR - 1)
847 / BITS_PER_CHAR);
848 if (XINT (height) <= SBYTES (data) / bytes_per_row)
849 pixmap_p = 1;
853 return pixmap_p ? Qt : Qnil;
857 /* Load a bitmap according to NAME (which is either a file name or a
858 pixmap spec) for use on frame F. Value is the bitmap_id (see
859 xfns.c). If NAME is nil, return with a bitmap id of zero. If
860 bitmap cannot be loaded, display a message saying so, and return
861 zero. */
863 static ptrdiff_t
864 load_pixmap (struct frame *f, Lisp_Object name)
866 ptrdiff_t bitmap_id;
868 if (NILP (name))
869 return 0;
871 CHECK_TYPE (!NILP (Fbitmap_spec_p (name)), Qbitmap_spec_p, name);
873 block_input ();
874 if (CONSP (name))
876 /* Decode a bitmap spec into a bitmap. */
878 int h, w;
879 Lisp_Object bits;
881 w = XINT (Fcar (name));
882 h = XINT (Fcar (Fcdr (name)));
883 bits = Fcar (Fcdr (Fcdr (name)));
885 bitmap_id = x_create_bitmap_from_data (f, SSDATA (bits),
886 w, h);
888 else
890 /* It must be a string -- a file name. */
891 bitmap_id = x_create_bitmap_from_file (f, name);
893 unblock_input ();
895 if (bitmap_id < 0)
897 add_to_log ("Invalid or undefined bitmap `%s'", name, Qnil);
898 bitmap_id = 0;
900 else
902 #ifdef GLYPH_DEBUG
903 ++npixmaps_allocated;
904 #endif
907 return bitmap_id;
910 #endif /* HAVE_WINDOW_SYSTEM */
914 /***********************************************************************
915 X Colors
916 ***********************************************************************/
918 /* Parse RGB_LIST, and fill in the RGB fields of COLOR.
919 RGB_LIST should contain (at least) 3 lisp integers.
920 Return 0 if there's a problem with RGB_LIST, otherwise return 1. */
922 static int
923 parse_rgb_list (Lisp_Object rgb_list, XColor *color)
925 #define PARSE_RGB_LIST_FIELD(field) \
926 if (CONSP (rgb_list) && INTEGERP (XCAR (rgb_list))) \
928 color->field = XINT (XCAR (rgb_list)); \
929 rgb_list = XCDR (rgb_list); \
931 else \
932 return 0;
934 PARSE_RGB_LIST_FIELD (red);
935 PARSE_RGB_LIST_FIELD (green);
936 PARSE_RGB_LIST_FIELD (blue);
938 return 1;
942 /* Lookup on frame F the color described by the lisp string COLOR.
943 The resulting tty color is returned in TTY_COLOR; if STD_COLOR is
944 non-zero, then the `standard' definition of the same color is
945 returned in it. */
947 static bool
948 tty_lookup_color (struct frame *f, Lisp_Object color, XColor *tty_color,
949 XColor *std_color)
951 Lisp_Object frame, color_desc;
953 if (!STRINGP (color) || NILP (Ffboundp (Qtty_color_desc)))
954 return 0;
956 XSETFRAME (frame, f);
958 color_desc = call2 (Qtty_color_desc, color, frame);
959 if (CONSP (color_desc) && CONSP (XCDR (color_desc)))
961 Lisp_Object rgb;
963 if (! INTEGERP (XCAR (XCDR (color_desc))))
964 return 0;
966 tty_color->pixel = XINT (XCAR (XCDR (color_desc)));
968 rgb = XCDR (XCDR (color_desc));
969 if (! parse_rgb_list (rgb, tty_color))
970 return 0;
972 /* Should we fill in STD_COLOR too? */
973 if (std_color)
975 /* Default STD_COLOR to the same as TTY_COLOR. */
976 *std_color = *tty_color;
978 /* Do a quick check to see if the returned descriptor is
979 actually _exactly_ equal to COLOR, otherwise we have to
980 lookup STD_COLOR separately. If it's impossible to lookup
981 a standard color, we just give up and use TTY_COLOR. */
982 if ((!STRINGP (XCAR (color_desc))
983 || NILP (Fstring_equal (color, XCAR (color_desc))))
984 && !NILP (Ffboundp (Qtty_color_standard_values)))
986 /* Look up STD_COLOR separately. */
987 rgb = call1 (Qtty_color_standard_values, color);
988 if (! parse_rgb_list (rgb, std_color))
989 return 0;
993 return 1;
995 else if (NILP (Fsymbol_value (intern ("tty-defined-color-alist"))))
996 /* We were called early during startup, and the colors are not
997 yet set up in tty-defined-color-alist. Don't return a failure
998 indication, since this produces the annoying "Unable to
999 load color" messages in the *Messages* buffer. */
1000 return 1;
1001 else
1002 /* tty-color-desc seems to have returned a bad value. */
1003 return 0;
1006 /* A version of defined_color for non-X frames. */
1008 static bool
1009 tty_defined_color (struct frame *f, const char *color_name,
1010 XColor *color_def, bool alloc)
1012 bool status = 1;
1014 /* Defaults. */
1015 color_def->pixel = FACE_TTY_DEFAULT_COLOR;
1016 color_def->red = 0;
1017 color_def->blue = 0;
1018 color_def->green = 0;
1020 if (*color_name)
1021 status = tty_lookup_color (f, build_string (color_name), color_def, NULL);
1023 if (color_def->pixel == FACE_TTY_DEFAULT_COLOR && *color_name)
1025 if (strcmp (color_name, "unspecified-fg") == 0)
1026 color_def->pixel = FACE_TTY_DEFAULT_FG_COLOR;
1027 else if (strcmp (color_name, "unspecified-bg") == 0)
1028 color_def->pixel = FACE_TTY_DEFAULT_BG_COLOR;
1031 if (color_def->pixel != FACE_TTY_DEFAULT_COLOR)
1032 status = 1;
1034 return status;
1038 /* Decide if color named COLOR_NAME is valid for the display
1039 associated with the frame F; if so, return the rgb values in
1040 COLOR_DEF. If ALLOC, allocate a new colormap cell.
1042 This does the right thing for any type of frame. */
1044 static bool
1045 defined_color (struct frame *f, const char *color_name, XColor *color_def,
1046 bool alloc)
1048 if (!FRAME_WINDOW_P (f))
1049 return tty_defined_color (f, color_name, color_def, alloc);
1050 #ifdef HAVE_X_WINDOWS
1051 else if (FRAME_X_P (f))
1052 return x_defined_color (f, color_name, color_def, alloc);
1053 #endif
1054 #ifdef HAVE_NTGUI
1055 else if (FRAME_W32_P (f))
1056 return w32_defined_color (f, color_name, color_def, alloc);
1057 #endif
1058 #ifdef HAVE_NS
1059 else if (FRAME_NS_P (f))
1060 return ns_defined_color (f, color_name, color_def, alloc, 1);
1061 #endif
1062 else
1063 emacs_abort ();
1067 /* Given the index IDX of a tty color on frame F, return its name, a
1068 Lisp string. */
1070 Lisp_Object
1071 tty_color_name (struct frame *f, int idx)
1073 if (idx >= 0 && !NILP (Ffboundp (Qtty_color_by_index)))
1075 Lisp_Object frame;
1076 Lisp_Object coldesc;
1078 XSETFRAME (frame, f);
1079 coldesc = call2 (Qtty_color_by_index, make_number (idx), frame);
1081 if (!NILP (coldesc))
1082 return XCAR (coldesc);
1084 #ifdef MSDOS
1085 /* We can have an MSDOG frame under -nw for a short window of
1086 opportunity before internal_terminal_init is called. DTRT. */
1087 if (FRAME_MSDOS_P (f) && !inhibit_window_system)
1088 return msdos_stdcolor_name (idx);
1089 #endif
1091 if (idx == FACE_TTY_DEFAULT_FG_COLOR)
1092 return build_string (unspecified_fg);
1093 if (idx == FACE_TTY_DEFAULT_BG_COLOR)
1094 return build_string (unspecified_bg);
1096 return Qunspecified;
1100 /* Return non-zero if COLOR_NAME is a shade of gray (or white or
1101 black) on frame F.
1103 The criterion implemented here is not a terribly sophisticated one. */
1105 static int
1106 face_color_gray_p (struct frame *f, const char *color_name)
1108 XColor color;
1109 int gray_p;
1111 if (defined_color (f, color_name, &color, 0))
1112 gray_p = (/* Any color sufficiently close to black counts as gray. */
1113 (color.red < 5000 && color.green < 5000 && color.blue < 5000)
1115 ((eabs (color.red - color.green)
1116 < max (color.red, color.green) / 20)
1117 && (eabs (color.green - color.blue)
1118 < max (color.green, color.blue) / 20)
1119 && (eabs (color.blue - color.red)
1120 < max (color.blue, color.red) / 20)));
1121 else
1122 gray_p = 0;
1124 return gray_p;
1128 /* Return non-zero if color COLOR_NAME can be displayed on frame F.
1129 BACKGROUND_P non-zero means the color will be used as background
1130 color. */
1132 static int
1133 face_color_supported_p (struct frame *f, const char *color_name,
1134 int background_p)
1136 Lisp_Object frame;
1137 XColor not_used;
1139 XSETFRAME (frame, f);
1140 return
1141 #ifdef HAVE_WINDOW_SYSTEM
1142 FRAME_WINDOW_P (f)
1143 ? (!NILP (Fxw_display_color_p (frame))
1144 || xstrcasecmp (color_name, "black") == 0
1145 || xstrcasecmp (color_name, "white") == 0
1146 || (background_p
1147 && face_color_gray_p (f, color_name))
1148 || (!NILP (Fx_display_grayscale_p (frame))
1149 && face_color_gray_p (f, color_name)))
1151 #endif
1152 tty_defined_color (f, color_name, &not_used, 0);
1156 DEFUN ("color-gray-p", Fcolor_gray_p, Scolor_gray_p, 1, 2, 0,
1157 doc: /* Return non-nil if COLOR is a shade of gray (or white or black).
1158 FRAME specifies the frame and thus the display for interpreting COLOR.
1159 If FRAME is nil or omitted, use the selected frame. */)
1160 (Lisp_Object color, Lisp_Object frame)
1162 CHECK_STRING (color);
1163 return (face_color_gray_p (decode_any_frame (frame), SSDATA (color))
1164 ? Qt : Qnil);
1168 DEFUN ("color-supported-p", Fcolor_supported_p,
1169 Scolor_supported_p, 1, 3, 0,
1170 doc: /* Return non-nil if COLOR can be displayed on FRAME.
1171 BACKGROUND-P non-nil means COLOR is used as a background.
1172 Otherwise, this function tells whether it can be used as a foreground.
1173 If FRAME is nil or omitted, use the selected frame.
1174 COLOR must be a valid color name. */)
1175 (Lisp_Object color, Lisp_Object frame, Lisp_Object background_p)
1177 CHECK_STRING (color);
1178 return (face_color_supported_p (decode_any_frame (frame),
1179 SSDATA (color), !NILP (background_p))
1180 ? Qt : Qnil);
1184 static unsigned long
1185 load_color2 (struct frame *f, struct face *face, Lisp_Object name,
1186 enum lface_attribute_index target_index, XColor *color)
1188 eassert (STRINGP (name));
1189 eassert (target_index == LFACE_FOREGROUND_INDEX
1190 || target_index == LFACE_BACKGROUND_INDEX
1191 || target_index == LFACE_UNDERLINE_INDEX
1192 || target_index == LFACE_OVERLINE_INDEX
1193 || target_index == LFACE_STRIKE_THROUGH_INDEX
1194 || target_index == LFACE_BOX_INDEX);
1196 /* if the color map is full, defined_color will return a best match
1197 to the values in an existing cell. */
1198 if (!defined_color (f, SSDATA (name), color, 1))
1200 add_to_log ("Unable to load color \"%s\"", name, Qnil);
1202 switch (target_index)
1204 case LFACE_FOREGROUND_INDEX:
1205 face->foreground_defaulted_p = 1;
1206 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1207 break;
1209 case LFACE_BACKGROUND_INDEX:
1210 face->background_defaulted_p = 1;
1211 color->pixel = FRAME_BACKGROUND_PIXEL (f);
1212 break;
1214 case LFACE_UNDERLINE_INDEX:
1215 face->underline_defaulted_p = 1;
1216 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1217 break;
1219 case LFACE_OVERLINE_INDEX:
1220 face->overline_color_defaulted_p = 1;
1221 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1222 break;
1224 case LFACE_STRIKE_THROUGH_INDEX:
1225 face->strike_through_color_defaulted_p = 1;
1226 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1227 break;
1229 case LFACE_BOX_INDEX:
1230 face->box_color_defaulted_p = 1;
1231 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1232 break;
1234 default:
1235 emacs_abort ();
1238 #ifdef GLYPH_DEBUG
1239 else
1240 ++ncolors_allocated;
1241 #endif
1243 return color->pixel;
1246 /* Load color with name NAME for use by face FACE on frame F.
1247 TARGET_INDEX must be one of LFACE_FOREGROUND_INDEX,
1248 LFACE_BACKGROUND_INDEX, LFACE_UNDERLINE_INDEX, LFACE_OVERLINE_INDEX,
1249 LFACE_STRIKE_THROUGH_INDEX, or LFACE_BOX_INDEX. Value is the
1250 pixel color. If color cannot be loaded, display a message, and
1251 return the foreground, background or underline color of F, but
1252 record that fact in flags of the face so that we don't try to free
1253 these colors. */
1255 #ifndef MSDOS
1256 static
1257 #endif
1258 unsigned long
1259 load_color (struct frame *f, struct face *face, Lisp_Object name,
1260 enum lface_attribute_index target_index)
1262 XColor color;
1263 return load_color2 (f, face, name, target_index, &color);
1267 #ifdef HAVE_WINDOW_SYSTEM
1269 #define NEAR_SAME_COLOR_THRESHOLD 30000
1271 /* Load colors for face FACE which is used on frame F. Colors are
1272 specified by slots LFACE_BACKGROUND_INDEX and LFACE_FOREGROUND_INDEX
1273 of ATTRS. If the background color specified is not supported on F,
1274 try to emulate gray colors with a stipple from Vface_default_stipple. */
1276 static void
1277 load_face_colors (struct frame *f, struct face *face,
1278 Lisp_Object attrs[LFACE_VECTOR_SIZE])
1280 Lisp_Object fg, bg, dfg;
1281 XColor xfg, xbg;
1283 bg = attrs[LFACE_BACKGROUND_INDEX];
1284 fg = attrs[LFACE_FOREGROUND_INDEX];
1286 /* Swap colors if face is inverse-video. */
1287 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1289 Lisp_Object tmp;
1290 tmp = fg;
1291 fg = bg;
1292 bg = tmp;
1295 /* Check for support for foreground, not for background because
1296 face_color_supported_p is smart enough to know that grays are
1297 "supported" as background because we are supposed to use stipple
1298 for them. */
1299 if (!face_color_supported_p (f, SSDATA (bg), 0)
1300 && !NILP (Fbitmap_spec_p (Vface_default_stipple)))
1302 x_destroy_bitmap (f, face->stipple);
1303 face->stipple = load_pixmap (f, Vface_default_stipple);
1306 face->background = load_color2 (f, face, bg, LFACE_BACKGROUND_INDEX, &xbg);
1307 face->foreground = load_color2 (f, face, fg, LFACE_FOREGROUND_INDEX, &xfg);
1309 dfg = attrs[LFACE_DISTANT_FOREGROUND_INDEX];
1310 if (!NILP (dfg) && !UNSPECIFIEDP (dfg)
1311 && color_distance (&xbg, &xfg) < NEAR_SAME_COLOR_THRESHOLD)
1313 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1314 face->background = load_color (f, face, dfg, LFACE_BACKGROUND_INDEX);
1315 else
1316 face->foreground = load_color (f, face, dfg, LFACE_FOREGROUND_INDEX);
1320 #ifdef HAVE_X_WINDOWS
1322 /* Free color PIXEL on frame F. */
1324 void
1325 unload_color (struct frame *f, unsigned long pixel)
1327 if (pixel != -1)
1329 block_input ();
1330 x_free_colors (f, &pixel, 1);
1331 unblock_input ();
1335 /* Free colors allocated for FACE. */
1337 static void
1338 free_face_colors (struct frame *f, struct face *face)
1340 /* PENDING(NS): need to do something here? */
1342 if (face->colors_copied_bitwise_p)
1343 return;
1345 block_input ();
1347 if (!face->foreground_defaulted_p)
1349 x_free_colors (f, &face->foreground, 1);
1350 IF_DEBUG (--ncolors_allocated);
1353 if (!face->background_defaulted_p)
1355 x_free_colors (f, &face->background, 1);
1356 IF_DEBUG (--ncolors_allocated);
1359 if (face->underline_p
1360 && !face->underline_defaulted_p)
1362 x_free_colors (f, &face->underline_color, 1);
1363 IF_DEBUG (--ncolors_allocated);
1366 if (face->overline_p
1367 && !face->overline_color_defaulted_p)
1369 x_free_colors (f, &face->overline_color, 1);
1370 IF_DEBUG (--ncolors_allocated);
1373 if (face->strike_through_p
1374 && !face->strike_through_color_defaulted_p)
1376 x_free_colors (f, &face->strike_through_color, 1);
1377 IF_DEBUG (--ncolors_allocated);
1380 if (face->box != FACE_NO_BOX
1381 && !face->box_color_defaulted_p)
1383 x_free_colors (f, &face->box_color, 1);
1384 IF_DEBUG (--ncolors_allocated);
1387 unblock_input ();
1390 #endif /* HAVE_X_WINDOWS */
1392 #endif /* HAVE_WINDOW_SYSTEM */
1396 /***********************************************************************
1397 XLFD Font Names
1398 ***********************************************************************/
1400 /* An enumerator for each field of an XLFD font name. */
1402 enum xlfd_field
1404 XLFD_FOUNDRY,
1405 XLFD_FAMILY,
1406 XLFD_WEIGHT,
1407 XLFD_SLANT,
1408 XLFD_SWIDTH,
1409 XLFD_ADSTYLE,
1410 XLFD_PIXEL_SIZE,
1411 XLFD_POINT_SIZE,
1412 XLFD_RESX,
1413 XLFD_RESY,
1414 XLFD_SPACING,
1415 XLFD_AVGWIDTH,
1416 XLFD_REGISTRY,
1417 XLFD_ENCODING,
1418 XLFD_LAST
1421 /* An enumerator for each possible slant value of a font. Taken from
1422 the XLFD specification. */
1424 enum xlfd_slant
1426 XLFD_SLANT_UNKNOWN,
1427 XLFD_SLANT_ROMAN,
1428 XLFD_SLANT_ITALIC,
1429 XLFD_SLANT_OBLIQUE,
1430 XLFD_SLANT_REVERSE_ITALIC,
1431 XLFD_SLANT_REVERSE_OBLIQUE,
1432 XLFD_SLANT_OTHER
1435 /* Relative font weight according to XLFD documentation. */
1437 enum xlfd_weight
1439 XLFD_WEIGHT_UNKNOWN,
1440 XLFD_WEIGHT_ULTRA_LIGHT, /* 10 */
1441 XLFD_WEIGHT_EXTRA_LIGHT, /* 20 */
1442 XLFD_WEIGHT_LIGHT, /* 30 */
1443 XLFD_WEIGHT_SEMI_LIGHT, /* 40: SemiLight, Book, ... */
1444 XLFD_WEIGHT_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1445 XLFD_WEIGHT_SEMI_BOLD, /* 60: SemiBold, DemiBold, ... */
1446 XLFD_WEIGHT_BOLD, /* 70: Bold, ... */
1447 XLFD_WEIGHT_EXTRA_BOLD, /* 80: ExtraBold, Heavy, ... */
1448 XLFD_WEIGHT_ULTRA_BOLD /* 90: UltraBold, Black, ... */
1451 /* Relative proportionate width. */
1453 enum xlfd_swidth
1455 XLFD_SWIDTH_UNKNOWN,
1456 XLFD_SWIDTH_ULTRA_CONDENSED, /* 10 */
1457 XLFD_SWIDTH_EXTRA_CONDENSED, /* 20 */
1458 XLFD_SWIDTH_CONDENSED, /* 30: Condensed, Narrow, Compressed, ... */
1459 XLFD_SWIDTH_SEMI_CONDENSED, /* 40: semicondensed */
1460 XLFD_SWIDTH_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1461 XLFD_SWIDTH_SEMI_EXPANDED, /* 60: SemiExpanded, DemiExpanded, ... */
1462 XLFD_SWIDTH_EXPANDED, /* 70: Expanded... */
1463 XLFD_SWIDTH_EXTRA_EXPANDED, /* 80: ExtraExpanded, Wide... */
1464 XLFD_SWIDTH_ULTRA_EXPANDED /* 90: UltraExpanded... */
1467 /* Order by which font selection chooses fonts. The default values
1468 mean `first, find a best match for the font width, then for the
1469 font height, then for weight, then for slant.' This variable can be
1470 set via set-face-font-sort-order. */
1472 static int font_sort_order[4];
1474 #ifdef HAVE_WINDOW_SYSTEM
1476 static enum font_property_index font_props_for_sorting[FONT_SIZE_INDEX];
1478 static int
1479 compare_fonts_by_sort_order (const void *v1, const void *v2)
1481 Lisp_Object const *p1 = v1;
1482 Lisp_Object const *p2 = v2;
1483 Lisp_Object font1 = *p1;
1484 Lisp_Object font2 = *p2;
1485 int i;
1487 for (i = 0; i < FONT_SIZE_INDEX; i++)
1489 enum font_property_index idx = font_props_for_sorting[i];
1490 Lisp_Object val1 = AREF (font1, idx), val2 = AREF (font2, idx);
1491 int result;
1493 if (idx <= FONT_REGISTRY_INDEX)
1495 if (STRINGP (val1))
1496 result = STRINGP (val2) ? strcmp (SSDATA (val1), SSDATA (val2)) : -1;
1497 else
1498 result = STRINGP (val2) ? 1 : 0;
1500 else
1502 if (INTEGERP (val1))
1503 result = (INTEGERP (val2) && XINT (val1) >= XINT (val2)
1504 ? XINT (val1) > XINT (val2)
1505 : -1);
1506 else
1507 result = INTEGERP (val2) ? 1 : 0;
1509 if (result)
1510 return result;
1512 return 0;
1515 DEFUN ("x-family-fonts", Fx_family_fonts, Sx_family_fonts, 0, 2, 0,
1516 doc: /* Return a list of available fonts of family FAMILY on FRAME.
1517 If FAMILY is omitted or nil, list all families.
1518 Otherwise, FAMILY must be a string, possibly containing wildcards
1519 `?' and `*'.
1520 If FRAME is omitted or nil, use the selected frame.
1521 Each element of the result is a vector [FAMILY WIDTH POINT-SIZE WEIGHT
1522 SLANT FIXED-P FULL REGISTRY-AND-ENCODING].
1523 FAMILY is the font family name. POINT-SIZE is the size of the
1524 font in 1/10 pt. WIDTH, WEIGHT, and SLANT are symbols describing the
1525 width, weight and slant of the font. These symbols are the same as for
1526 face attributes. FIXED-P is non-nil if the font is fixed-pitch.
1527 FULL is the full name of the font, and REGISTRY-AND-ENCODING is a string
1528 giving the registry and encoding of the font.
1529 The result list is sorted according to the current setting of
1530 the face font sort order. */)
1531 (Lisp_Object family, Lisp_Object frame)
1533 Lisp_Object font_spec, list, *drivers, vec;
1534 struct frame *f = decode_live_frame (frame);
1535 ptrdiff_t i, nfonts;
1536 EMACS_INT ndrivers;
1537 Lisp_Object result;
1538 USE_SAFE_ALLOCA;
1540 font_spec = Ffont_spec (0, NULL);
1541 if (!NILP (family))
1543 CHECK_STRING (family);
1544 font_parse_family_registry (family, Qnil, font_spec);
1547 list = font_list_entities (f, font_spec);
1548 if (NILP (list))
1549 return Qnil;
1551 /* Sort the font entities. */
1552 for (i = 0; i < 4; i++)
1553 switch (font_sort_order[i])
1555 case XLFD_SWIDTH:
1556 font_props_for_sorting[i] = FONT_WIDTH_INDEX; break;
1557 case XLFD_POINT_SIZE:
1558 font_props_for_sorting[i] = FONT_SIZE_INDEX; break;
1559 case XLFD_WEIGHT:
1560 font_props_for_sorting[i] = FONT_WEIGHT_INDEX; break;
1561 default:
1562 font_props_for_sorting[i] = FONT_SLANT_INDEX; break;
1564 font_props_for_sorting[i++] = FONT_FAMILY_INDEX;
1565 font_props_for_sorting[i++] = FONT_FOUNDRY_INDEX;
1566 font_props_for_sorting[i++] = FONT_ADSTYLE_INDEX;
1567 font_props_for_sorting[i++] = FONT_REGISTRY_INDEX;
1569 ndrivers = XINT (Flength (list));
1570 SAFE_ALLOCA_LISP (drivers, ndrivers);
1571 for (i = 0; i < ndrivers; i++, list = XCDR (list))
1572 drivers[i] = XCAR (list);
1573 vec = Fvconcat (ndrivers, drivers);
1574 nfonts = ASIZE (vec);
1576 qsort (XVECTOR (vec)->contents, nfonts, word_size,
1577 compare_fonts_by_sort_order);
1579 result = Qnil;
1580 for (i = nfonts - 1; i >= 0; --i)
1582 Lisp_Object font = AREF (vec, i);
1583 Lisp_Object v = make_uninit_vector (8);
1584 int point;
1585 Lisp_Object spacing;
1587 ASET (v, 0, AREF (font, FONT_FAMILY_INDEX));
1588 ASET (v, 1, FONT_WIDTH_SYMBOLIC (font));
1589 point = PIXEL_TO_POINT (XINT (AREF (font, FONT_SIZE_INDEX)) * 10,
1590 FRAME_RES_Y (f));
1591 ASET (v, 2, make_number (point));
1592 ASET (v, 3, FONT_WEIGHT_SYMBOLIC (font));
1593 ASET (v, 4, FONT_SLANT_SYMBOLIC (font));
1594 spacing = Ffont_get (font, QCspacing);
1595 ASET (v, 5, (NILP (spacing) || EQ (spacing, Qp)) ? Qnil : Qt);
1596 ASET (v, 6, Ffont_xlfd_name (font, Qnil));
1597 ASET (v, 7, AREF (font, FONT_REGISTRY_INDEX));
1599 result = Fcons (v, result);
1602 SAFE_FREE ();
1603 return result;
1606 DEFUN ("x-list-fonts", Fx_list_fonts, Sx_list_fonts, 1, 5, 0,
1607 doc: /* Return a list of the names of available fonts matching PATTERN.
1608 If optional arguments FACE and FRAME are specified, return only fonts
1609 the same size as FACE on FRAME.
1611 PATTERN should be a string containing a font name in the XLFD,
1612 Fontconfig, or GTK format. A font name given in the XLFD format may
1613 contain wildcard characters:
1614 the * character matches any substring, and
1615 the ? character matches any single character.
1616 PATTERN is case-insensitive.
1618 The return value is a list of strings, suitable as arguments to
1619 `set-face-font'.
1621 Fonts Emacs can't use may or may not be excluded
1622 even if they match PATTERN and FACE.
1623 The optional fourth argument MAXIMUM sets a limit on how many
1624 fonts to match. The first MAXIMUM fonts are reported.
1625 The optional fifth argument WIDTH, if specified, is a number of columns
1626 occupied by a character of a font. In that case, return only fonts
1627 the WIDTH times as wide as FACE on FRAME. */)
1628 (Lisp_Object pattern, Lisp_Object face, Lisp_Object frame,
1629 Lisp_Object maximum, Lisp_Object width)
1631 struct frame *f;
1632 int size, avgwidth IF_LINT (= 0);
1634 check_window_system (NULL);
1635 CHECK_STRING (pattern);
1637 if (! NILP (maximum))
1638 CHECK_NATNUM (maximum);
1640 if (!NILP (width))
1641 CHECK_NUMBER (width);
1643 /* We can't simply call decode_window_system_frame because
1644 this function may be called before any frame is created. */
1645 f = decode_live_frame (frame);
1646 if (! FRAME_WINDOW_P (f))
1648 /* Perhaps we have not yet created any frame. */
1649 f = NULL;
1650 frame = Qnil;
1651 face = Qnil;
1653 else
1654 XSETFRAME (frame, f);
1656 /* Determine the width standard for comparison with the fonts we find. */
1658 if (NILP (face))
1659 size = 0;
1660 else
1662 /* This is of limited utility since it works with character
1663 widths. Keep it for compatibility. --gerd. */
1664 int face_id = lookup_named_face (f, face, 0);
1665 struct face *width_face = (face_id < 0
1666 ? NULL
1667 : FACE_FROM_ID (f, face_id));
1669 if (width_face && width_face->font)
1671 size = width_face->font->pixel_size;
1672 avgwidth = width_face->font->average_width;
1674 else
1676 size = FRAME_FONT (f)->pixel_size;
1677 avgwidth = FRAME_FONT (f)->average_width;
1679 if (!NILP (width))
1680 avgwidth *= XINT (width);
1684 Lisp_Object font_spec;
1685 Lisp_Object args[2], tail;
1687 font_spec = font_spec_from_name (pattern);
1688 if (!FONTP (font_spec))
1689 signal_error ("Invalid font name", pattern);
1691 if (size)
1693 Ffont_put (font_spec, QCsize, make_number (size));
1694 Ffont_put (font_spec, QCavgwidth, make_number (avgwidth));
1696 args[0] = Flist_fonts (font_spec, frame, maximum, font_spec);
1697 for (tail = args[0]; CONSP (tail); tail = XCDR (tail))
1699 Lisp_Object font_entity;
1701 font_entity = XCAR (tail);
1702 if ((NILP (AREF (font_entity, FONT_SIZE_INDEX))
1703 || XINT (AREF (font_entity, FONT_SIZE_INDEX)) == 0)
1704 && ! NILP (AREF (font_spec, FONT_SIZE_INDEX)))
1706 /* This is a scalable font. For backward compatibility,
1707 we set the specified size. */
1708 font_entity = copy_font_spec (font_entity);
1709 ASET (font_entity, FONT_SIZE_INDEX,
1710 AREF (font_spec, FONT_SIZE_INDEX));
1712 XSETCAR (tail, Ffont_xlfd_name (font_entity, Qnil));
1714 if (NILP (frame))
1715 /* We don't have to check fontsets. */
1716 return args[0];
1717 args[1] = list_fontsets (f, pattern, size);
1718 return Fnconc (2, args);
1722 #endif /* HAVE_WINDOW_SYSTEM */
1725 /***********************************************************************
1726 Lisp Faces
1727 ***********************************************************************/
1729 /* Access face attributes of face LFACE, a Lisp vector. */
1731 #define LFACE_FAMILY(LFACE) AREF ((LFACE), LFACE_FAMILY_INDEX)
1732 #define LFACE_FOUNDRY(LFACE) AREF ((LFACE), LFACE_FOUNDRY_INDEX)
1733 #define LFACE_HEIGHT(LFACE) AREF ((LFACE), LFACE_HEIGHT_INDEX)
1734 #define LFACE_WEIGHT(LFACE) AREF ((LFACE), LFACE_WEIGHT_INDEX)
1735 #define LFACE_SLANT(LFACE) AREF ((LFACE), LFACE_SLANT_INDEX)
1736 #define LFACE_UNDERLINE(LFACE) AREF ((LFACE), LFACE_UNDERLINE_INDEX)
1737 #define LFACE_INVERSE(LFACE) AREF ((LFACE), LFACE_INVERSE_INDEX)
1738 #define LFACE_FOREGROUND(LFACE) AREF ((LFACE), LFACE_FOREGROUND_INDEX)
1739 #define LFACE_BACKGROUND(LFACE) AREF ((LFACE), LFACE_BACKGROUND_INDEX)
1740 #define LFACE_STIPPLE(LFACE) AREF ((LFACE), LFACE_STIPPLE_INDEX)
1741 #define LFACE_SWIDTH(LFACE) AREF ((LFACE), LFACE_SWIDTH_INDEX)
1742 #define LFACE_OVERLINE(LFACE) AREF ((LFACE), LFACE_OVERLINE_INDEX)
1743 #define LFACE_STRIKE_THROUGH(LFACE) AREF ((LFACE), LFACE_STRIKE_THROUGH_INDEX)
1744 #define LFACE_BOX(LFACE) AREF ((LFACE), LFACE_BOX_INDEX)
1745 #define LFACE_FONT(LFACE) AREF ((LFACE), LFACE_FONT_INDEX)
1746 #define LFACE_INHERIT(LFACE) AREF ((LFACE), LFACE_INHERIT_INDEX)
1747 #define LFACE_FONTSET(LFACE) AREF ((LFACE), LFACE_FONTSET_INDEX)
1748 #define LFACE_DISTANT_FOREGROUND(LFACE) \
1749 AREF ((LFACE), LFACE_DISTANT_FOREGROUND_INDEX)
1751 /* Non-zero if LFACE is a Lisp face. A Lisp face is a vector of size
1752 LFACE_VECTOR_SIZE which has the symbol `face' in slot 0. */
1754 #define LFACEP(LFACE) \
1755 (VECTORP (LFACE) \
1756 && ASIZE (LFACE) == LFACE_VECTOR_SIZE \
1757 && EQ (AREF (LFACE, 0), Qface))
1760 #ifdef GLYPH_DEBUG
1762 /* Check consistency of Lisp face attribute vector ATTRS. */
1764 static void
1765 check_lface_attrs (Lisp_Object attrs[LFACE_VECTOR_SIZE])
1767 eassert (UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
1768 || IGNORE_DEFFACE_P (attrs[LFACE_FAMILY_INDEX])
1769 || STRINGP (attrs[LFACE_FAMILY_INDEX]));
1770 eassert (UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
1771 || IGNORE_DEFFACE_P (attrs[LFACE_FOUNDRY_INDEX])
1772 || STRINGP (attrs[LFACE_FOUNDRY_INDEX]));
1773 eassert (UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
1774 || IGNORE_DEFFACE_P (attrs[LFACE_SWIDTH_INDEX])
1775 || SYMBOLP (attrs[LFACE_SWIDTH_INDEX]));
1776 eassert (UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
1777 || IGNORE_DEFFACE_P (attrs[LFACE_HEIGHT_INDEX])
1778 || INTEGERP (attrs[LFACE_HEIGHT_INDEX])
1779 || FLOATP (attrs[LFACE_HEIGHT_INDEX])
1780 || FUNCTIONP (attrs[LFACE_HEIGHT_INDEX]));
1781 eassert (UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
1782 || IGNORE_DEFFACE_P (attrs[LFACE_WEIGHT_INDEX])
1783 || SYMBOLP (attrs[LFACE_WEIGHT_INDEX]));
1784 eassert (UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
1785 || IGNORE_DEFFACE_P (attrs[LFACE_SLANT_INDEX])
1786 || SYMBOLP (attrs[LFACE_SLANT_INDEX]));
1787 eassert (UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
1788 || IGNORE_DEFFACE_P (attrs[LFACE_UNDERLINE_INDEX])
1789 || SYMBOLP (attrs[LFACE_UNDERLINE_INDEX])
1790 || STRINGP (attrs[LFACE_UNDERLINE_INDEX])
1791 || CONSP (attrs[LFACE_UNDERLINE_INDEX]));
1792 eassert (UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
1793 || IGNORE_DEFFACE_P (attrs[LFACE_OVERLINE_INDEX])
1794 || SYMBOLP (attrs[LFACE_OVERLINE_INDEX])
1795 || STRINGP (attrs[LFACE_OVERLINE_INDEX]));
1796 eassert (UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1797 || IGNORE_DEFFACE_P (attrs[LFACE_STRIKE_THROUGH_INDEX])
1798 || SYMBOLP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1799 || STRINGP (attrs[LFACE_STRIKE_THROUGH_INDEX]));
1800 eassert (UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
1801 || IGNORE_DEFFACE_P (attrs[LFACE_BOX_INDEX])
1802 || SYMBOLP (attrs[LFACE_BOX_INDEX])
1803 || STRINGP (attrs[LFACE_BOX_INDEX])
1804 || INTEGERP (attrs[LFACE_BOX_INDEX])
1805 || CONSP (attrs[LFACE_BOX_INDEX]));
1806 eassert (UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
1807 || IGNORE_DEFFACE_P (attrs[LFACE_INVERSE_INDEX])
1808 || SYMBOLP (attrs[LFACE_INVERSE_INDEX]));
1809 eassert (UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
1810 || IGNORE_DEFFACE_P (attrs[LFACE_FOREGROUND_INDEX])
1811 || STRINGP (attrs[LFACE_FOREGROUND_INDEX]));
1812 eassert (UNSPECIFIEDP (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
1813 || IGNORE_DEFFACE_P (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
1814 || STRINGP (attrs[LFACE_DISTANT_FOREGROUND_INDEX]));
1815 eassert (UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
1816 || IGNORE_DEFFACE_P (attrs[LFACE_BACKGROUND_INDEX])
1817 || STRINGP (attrs[LFACE_BACKGROUND_INDEX]));
1818 eassert (UNSPECIFIEDP (attrs[LFACE_INHERIT_INDEX])
1819 || IGNORE_DEFFACE_P (attrs[LFACE_INHERIT_INDEX])
1820 || NILP (attrs[LFACE_INHERIT_INDEX])
1821 || SYMBOLP (attrs[LFACE_INHERIT_INDEX])
1822 || CONSP (attrs[LFACE_INHERIT_INDEX]));
1823 #ifdef HAVE_WINDOW_SYSTEM
1824 eassert (UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
1825 || IGNORE_DEFFACE_P (attrs[LFACE_STIPPLE_INDEX])
1826 || SYMBOLP (attrs[LFACE_STIPPLE_INDEX])
1827 || !NILP (Fbitmap_spec_p (attrs[LFACE_STIPPLE_INDEX])));
1828 eassert (UNSPECIFIEDP (attrs[LFACE_FONT_INDEX])
1829 || IGNORE_DEFFACE_P (attrs[LFACE_FONT_INDEX])
1830 || FONTP (attrs[LFACE_FONT_INDEX]));
1831 eassert (UNSPECIFIEDP (attrs[LFACE_FONTSET_INDEX])
1832 || STRINGP (attrs[LFACE_FONTSET_INDEX])
1833 || NILP (attrs[LFACE_FONTSET_INDEX]));
1834 #endif
1838 /* Check consistency of attributes of Lisp face LFACE (a Lisp vector). */
1840 static void
1841 check_lface (Lisp_Object lface)
1843 if (!NILP (lface))
1845 eassert (LFACEP (lface));
1846 check_lface_attrs (XVECTOR (lface)->contents);
1850 #else /* not GLYPH_DEBUG */
1852 #define check_lface_attrs(attrs) (void) 0
1853 #define check_lface(lface) (void) 0
1855 #endif /* GLYPH_DEBUG */
1859 /* Face-merge cycle checking. */
1861 enum named_merge_point_kind
1863 NAMED_MERGE_POINT_NORMAL,
1864 NAMED_MERGE_POINT_REMAP
1867 /* A `named merge point' is simply a point during face-merging where we
1868 look up a face by name. We keep a stack of which named lookups we're
1869 currently processing so that we can easily detect cycles, using a
1870 linked- list of struct named_merge_point structures, typically
1871 allocated on the stack frame of the named lookup functions which are
1872 active (so no consing is required). */
1873 struct named_merge_point
1875 Lisp_Object face_name;
1876 enum named_merge_point_kind named_merge_point_kind;
1877 struct named_merge_point *prev;
1881 /* If a face merging cycle is detected for FACE_NAME, return 0,
1882 otherwise add NEW_NAMED_MERGE_POINT, which is initialized using
1883 FACE_NAME and NAMED_MERGE_POINT_KIND, as the head of the linked list
1884 pointed to by NAMED_MERGE_POINTS, and return 1. */
1886 static int
1887 push_named_merge_point (struct named_merge_point *new_named_merge_point,
1888 Lisp_Object face_name,
1889 enum named_merge_point_kind named_merge_point_kind,
1890 struct named_merge_point **named_merge_points)
1892 struct named_merge_point *prev;
1894 for (prev = *named_merge_points; prev; prev = prev->prev)
1895 if (EQ (face_name, prev->face_name))
1897 if (prev->named_merge_point_kind == named_merge_point_kind)
1898 /* A cycle, so fail. */
1899 return 0;
1900 else if (prev->named_merge_point_kind == NAMED_MERGE_POINT_REMAP)
1901 /* A remap `hides ' any previous normal merge points
1902 (because the remap means that it's actually different face),
1903 so as we know the current merge point must be normal, we
1904 can just assume it's OK. */
1905 break;
1908 new_named_merge_point->face_name = face_name;
1909 new_named_merge_point->named_merge_point_kind = named_merge_point_kind;
1910 new_named_merge_point->prev = *named_merge_points;
1912 *named_merge_points = new_named_merge_point;
1914 return 1;
1918 /* Resolve face name FACE_NAME. If FACE_NAME is a string, intern it
1919 to make it a symbol. If FACE_NAME is an alias for another face,
1920 return that face's name.
1922 Return default face in case of errors. */
1924 static Lisp_Object
1925 resolve_face_name (Lisp_Object face_name, int signal_p)
1927 Lisp_Object orig_face;
1928 Lisp_Object tortoise, hare;
1930 if (STRINGP (face_name))
1931 face_name = intern (SSDATA (face_name));
1933 if (NILP (face_name) || !SYMBOLP (face_name))
1934 return face_name;
1936 orig_face = face_name;
1937 tortoise = hare = face_name;
1939 while (1)
1941 face_name = hare;
1942 hare = Fget (hare, Qface_alias);
1943 if (NILP (hare) || !SYMBOLP (hare))
1944 break;
1946 face_name = hare;
1947 hare = Fget (hare, Qface_alias);
1948 if (NILP (hare) || !SYMBOLP (hare))
1949 break;
1951 tortoise = Fget (tortoise, Qface_alias);
1952 if (EQ (hare, tortoise))
1954 if (signal_p)
1955 xsignal1 (Qcircular_list, orig_face);
1956 return Qdefault;
1960 return face_name;
1964 /* Return the face definition of FACE_NAME on frame F. F null means
1965 return the definition for new frames. FACE_NAME may be a string or
1966 a symbol (apparently Emacs 20.2 allowed strings as face names in
1967 face text properties; Ediff uses that). If SIGNAL_P is non-zero,
1968 signal an error if FACE_NAME is not a valid face name. If SIGNAL_P
1969 is zero, value is nil if FACE_NAME is not a valid face name. */
1970 static Lisp_Object
1971 lface_from_face_name_no_resolve (struct frame *f, Lisp_Object face_name,
1972 int signal_p)
1974 Lisp_Object lface;
1976 if (f)
1977 lface = assq_no_quit (face_name, f->face_alist);
1978 else
1979 lface = assq_no_quit (face_name, Vface_new_frame_defaults);
1981 if (CONSP (lface))
1982 lface = XCDR (lface);
1983 else if (signal_p)
1984 signal_error ("Invalid face", face_name);
1986 check_lface (lface);
1988 return lface;
1991 /* Return the face definition of FACE_NAME on frame F. F null means
1992 return the definition for new frames. FACE_NAME may be a string or
1993 a symbol (apparently Emacs 20.2 allowed strings as face names in
1994 face text properties; Ediff uses that). If FACE_NAME is an alias
1995 for another face, return that face's definition. If SIGNAL_P is
1996 non-zero, signal an error if FACE_NAME is not a valid face name.
1997 If SIGNAL_P is zero, value is nil if FACE_NAME is not a valid face
1998 name. */
1999 static Lisp_Object
2000 lface_from_face_name (struct frame *f, Lisp_Object face_name, int signal_p)
2002 face_name = resolve_face_name (face_name, signal_p);
2003 return lface_from_face_name_no_resolve (f, face_name, signal_p);
2007 /* Get face attributes of face FACE_NAME from frame-local faces on
2008 frame F. Store the resulting attributes in ATTRS which must point
2009 to a vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If SIGNAL_P
2010 is non-zero, signal an error if FACE_NAME does not name a face.
2011 Otherwise, value is zero if FACE_NAME is not a face. */
2013 static int
2014 get_lface_attributes_no_remap (struct frame *f, Lisp_Object face_name,
2015 Lisp_Object attrs[LFACE_VECTOR_SIZE],
2016 int signal_p)
2018 Lisp_Object lface;
2020 lface = lface_from_face_name_no_resolve (f, face_name, signal_p);
2022 if (! NILP (lface))
2023 memcpy (attrs, XVECTOR (lface)->contents,
2024 LFACE_VECTOR_SIZE * sizeof *attrs);
2026 return !NILP (lface);
2029 /* Get face attributes of face FACE_NAME from frame-local faces on frame
2030 F. Store the resulting attributes in ATTRS which must point to a
2031 vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If FACE_NAME is an
2032 alias for another face, use that face's definition. If SIGNAL_P is
2033 non-zero, signal an error if FACE_NAME does not name a face.
2034 Otherwise, value is zero if FACE_NAME is not a face. */
2036 static int
2037 get_lface_attributes (struct frame *f, Lisp_Object face_name,
2038 Lisp_Object attrs[LFACE_VECTOR_SIZE], int signal_p,
2039 struct named_merge_point *named_merge_points)
2041 Lisp_Object face_remapping;
2043 face_name = resolve_face_name (face_name, signal_p);
2045 /* See if SYMBOL has been remapped to some other face (usually this
2046 is done buffer-locally). */
2047 face_remapping = assq_no_quit (face_name, Vface_remapping_alist);
2048 if (CONSP (face_remapping))
2050 struct named_merge_point named_merge_point;
2052 if (push_named_merge_point (&named_merge_point,
2053 face_name, NAMED_MERGE_POINT_REMAP,
2054 &named_merge_points))
2056 int i;
2058 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2059 attrs[i] = Qunspecified;
2061 return merge_face_ref (f, XCDR (face_remapping), attrs,
2062 signal_p, named_merge_points);
2066 /* Default case, no remapping. */
2067 return get_lface_attributes_no_remap (f, face_name, attrs, signal_p);
2071 /* Non-zero if all attributes in face attribute vector ATTRS are
2072 specified, i.e. are non-nil. */
2074 static int
2075 lface_fully_specified_p (Lisp_Object attrs[LFACE_VECTOR_SIZE])
2077 int i;
2079 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2080 if (i != LFACE_FONT_INDEX && i != LFACE_INHERIT_INDEX
2081 && i != LFACE_DISTANT_FOREGROUND_INDEX)
2082 if ((UNSPECIFIEDP (attrs[i]) || IGNORE_DEFFACE_P (attrs[i])))
2083 break;
2085 return i == LFACE_VECTOR_SIZE;
2088 #ifdef HAVE_WINDOW_SYSTEM
2090 /* Set font-related attributes of Lisp face LFACE from FONT-OBJECT.
2091 If FORCE_P is zero, set only unspecified attributes of LFACE. The
2092 exception is `font' attribute. It is set to FONT_OBJECT regardless
2093 of FORCE_P. */
2095 static int
2096 set_lface_from_font (struct frame *f, Lisp_Object lface,
2097 Lisp_Object font_object, int force_p)
2099 Lisp_Object val;
2100 struct font *font = XFONT_OBJECT (font_object);
2102 /* Set attributes only if unspecified, otherwise face defaults for
2103 new frames would never take effect. If the font doesn't have a
2104 specific property, set a normal value for that. */
2106 if (force_p || UNSPECIFIEDP (LFACE_FAMILY (lface)))
2108 Lisp_Object family = AREF (font_object, FONT_FAMILY_INDEX);
2110 ASET (lface, LFACE_FAMILY_INDEX, SYMBOL_NAME (family));
2113 if (force_p || UNSPECIFIEDP (LFACE_FOUNDRY (lface)))
2115 Lisp_Object foundry = AREF (font_object, FONT_FOUNDRY_INDEX);
2117 ASET (lface, LFACE_FOUNDRY_INDEX, SYMBOL_NAME (foundry));
2120 if (force_p || UNSPECIFIEDP (LFACE_HEIGHT (lface)))
2122 int pt = PIXEL_TO_POINT (font->pixel_size * 10, FRAME_RES_Y (f));
2124 eassert (pt > 0);
2125 ASET (lface, LFACE_HEIGHT_INDEX, make_number (pt));
2128 if (force_p || UNSPECIFIEDP (LFACE_WEIGHT (lface)))
2130 val = FONT_WEIGHT_FOR_FACE (font_object);
2131 ASET (lface, LFACE_WEIGHT_INDEX, ! NILP (val) ? val :Qnormal);
2133 if (force_p || UNSPECIFIEDP (LFACE_SLANT (lface)))
2135 val = FONT_SLANT_FOR_FACE (font_object);
2136 ASET (lface, LFACE_SLANT_INDEX, ! NILP (val) ? val : Qnormal);
2138 if (force_p || UNSPECIFIEDP (LFACE_SWIDTH (lface)))
2140 val = FONT_WIDTH_FOR_FACE (font_object);
2141 ASET (lface, LFACE_SWIDTH_INDEX, ! NILP (val) ? val : Qnormal);
2144 ASET (lface, LFACE_FONT_INDEX, font_object);
2145 return 1;
2148 #endif /* HAVE_WINDOW_SYSTEM */
2151 /* Merges the face height FROM with the face height TO, and returns the
2152 merged height. If FROM is an invalid height, then INVALID is
2153 returned instead. FROM and TO may be either absolute face heights or
2154 `relative' heights; the returned value is always an absolute height
2155 unless both FROM and TO are relative. */
2157 static Lisp_Object
2158 merge_face_heights (Lisp_Object from, Lisp_Object to, Lisp_Object invalid)
2160 Lisp_Object result = invalid;
2162 if (INTEGERP (from))
2163 /* FROM is absolute, just use it as is. */
2164 result = from;
2165 else if (FLOATP (from))
2166 /* FROM is a scale, use it to adjust TO. */
2168 if (INTEGERP (to))
2169 /* relative X absolute => absolute */
2170 result = make_number (XFLOAT_DATA (from) * XINT (to));
2171 else if (FLOATP (to))
2172 /* relative X relative => relative */
2173 result = make_float (XFLOAT_DATA (from) * XFLOAT_DATA (to));
2174 else if (UNSPECIFIEDP (to))
2175 result = from;
2177 else if (FUNCTIONP (from))
2178 /* FROM is a function, which use to adjust TO. */
2180 /* Call function with current height as argument.
2181 From is the new height. */
2182 result = safe_call1 (from, to);
2184 /* Ensure that if TO was absolute, so is the result. */
2185 if (INTEGERP (to) && !INTEGERP (result))
2186 result = invalid;
2189 return result;
2193 /* Merge two Lisp face attribute vectors on frame F, FROM and TO, and
2194 store the resulting attributes in TO, which must be already be
2195 completely specified and contain only absolute attributes. Every
2196 specified attribute of FROM overrides the corresponding attribute of
2197 TO; relative attributes in FROM are merged with the absolute value in
2198 TO and replace it. NAMED_MERGE_POINTS is used internally to detect
2199 loops in face inheritance/remapping; it should be 0 when called from
2200 other places. */
2202 static void
2203 merge_face_vectors (struct frame *f, Lisp_Object *from, Lisp_Object *to,
2204 struct named_merge_point *named_merge_points)
2206 int i;
2207 Lisp_Object font = Qnil;
2209 /* If FROM inherits from some other faces, merge their attributes into
2210 TO before merging FROM's direct attributes. Note that an :inherit
2211 attribute of `unspecified' is the same as one of nil; we never
2212 merge :inherit attributes, so nil is more correct, but lots of
2213 other code uses `unspecified' as a generic value for face attributes. */
2214 if (!UNSPECIFIEDP (from[LFACE_INHERIT_INDEX])
2215 && !NILP (from[LFACE_INHERIT_INDEX]))
2216 merge_face_ref (f, from[LFACE_INHERIT_INDEX], to, 0, named_merge_points);
2218 if (FONT_SPEC_P (from[LFACE_FONT_INDEX]))
2220 if (!UNSPECIFIEDP (to[LFACE_FONT_INDEX]))
2221 font = merge_font_spec (from[LFACE_FONT_INDEX], to[LFACE_FONT_INDEX]);
2222 else
2223 font = copy_font_spec (from[LFACE_FONT_INDEX]);
2224 to[LFACE_FONT_INDEX] = font;
2227 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2228 if (!UNSPECIFIEDP (from[i]))
2230 if (i == LFACE_HEIGHT_INDEX && !INTEGERP (from[i]))
2232 to[i] = merge_face_heights (from[i], to[i], to[i]);
2233 font_clear_prop (to, FONT_SIZE_INDEX);
2235 else if (i != LFACE_FONT_INDEX && ! EQ (to[i], from[i]))
2237 to[i] = from[i];
2238 if (i >= LFACE_FAMILY_INDEX && i <=LFACE_SLANT_INDEX)
2239 font_clear_prop (to,
2240 (i == LFACE_FAMILY_INDEX ? FONT_FAMILY_INDEX
2241 : i == LFACE_FOUNDRY_INDEX ? FONT_FOUNDRY_INDEX
2242 : i == LFACE_SWIDTH_INDEX ? FONT_WIDTH_INDEX
2243 : i == LFACE_HEIGHT_INDEX ? FONT_SIZE_INDEX
2244 : i == LFACE_WEIGHT_INDEX ? FONT_WEIGHT_INDEX
2245 : FONT_SLANT_INDEX));
2249 /* If FROM specifies a font spec, make its contents take precedence
2250 over :family and other attributes. This is needed for face
2251 remapping using :font to work. */
2253 if (!NILP (font))
2255 if (! NILP (AREF (font, FONT_FOUNDRY_INDEX)))
2256 to[LFACE_FOUNDRY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FOUNDRY_INDEX));
2257 if (! NILP (AREF (font, FONT_FAMILY_INDEX)))
2258 to[LFACE_FAMILY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FAMILY_INDEX));
2259 if (! NILP (AREF (font, FONT_WEIGHT_INDEX)))
2260 to[LFACE_WEIGHT_INDEX] = FONT_WEIGHT_FOR_FACE (font);
2261 if (! NILP (AREF (font, FONT_SLANT_INDEX)))
2262 to[LFACE_SLANT_INDEX] = FONT_SLANT_FOR_FACE (font);
2263 if (! NILP (AREF (font, FONT_WIDTH_INDEX)))
2264 to[LFACE_SWIDTH_INDEX] = FONT_WIDTH_FOR_FACE (font);
2265 ASET (font, FONT_SIZE_INDEX, Qnil);
2268 /* TO is always an absolute face, which should inherit from nothing.
2269 We blindly copy the :inherit attribute above and fix it up here. */
2270 to[LFACE_INHERIT_INDEX] = Qnil;
2273 /* Merge the named face FACE_NAME on frame F, into the vector of face
2274 attributes TO. NAMED_MERGE_POINTS is used to detect loops in face
2275 inheritance. Returns true if FACE_NAME is a valid face name and
2276 merging succeeded. */
2278 static int
2279 merge_named_face (struct frame *f, Lisp_Object face_name, Lisp_Object *to,
2280 struct named_merge_point *named_merge_points)
2282 struct named_merge_point named_merge_point;
2284 if (push_named_merge_point (&named_merge_point,
2285 face_name, NAMED_MERGE_POINT_NORMAL,
2286 &named_merge_points))
2288 struct gcpro gcpro1;
2289 Lisp_Object from[LFACE_VECTOR_SIZE];
2290 int ok = get_lface_attributes (f, face_name, from, 0, named_merge_points);
2292 if (ok)
2294 GCPRO1 (named_merge_point.face_name);
2295 merge_face_vectors (f, from, to, named_merge_points);
2296 UNGCPRO;
2299 return ok;
2301 else
2302 return 0;
2306 /* Merge face attributes from the lisp `face reference' FACE_REF on
2307 frame F into the face attribute vector TO. If ERR_MSGS is non-zero,
2308 problems with FACE_REF cause an error message to be shown. Return
2309 non-zero if no errors occurred (regardless of the value of ERR_MSGS).
2310 NAMED_MERGE_POINTS is used to detect loops in face inheritance or
2311 list structure; it may be 0 for most callers.
2313 FACE_REF may be a single face specification or a list of such
2314 specifications. Each face specification can be:
2316 1. A symbol or string naming a Lisp face.
2318 2. A property list of the form (KEYWORD VALUE ...) where each
2319 KEYWORD is a face attribute name, and value is an appropriate value
2320 for that attribute.
2322 3. Conses or the form (FOREGROUND-COLOR . COLOR) or
2323 (BACKGROUND-COLOR . COLOR) where COLOR is a color name. This is
2324 for compatibility with 20.2.
2326 Face specifications earlier in lists take precedence over later
2327 specifications. */
2329 static int
2330 merge_face_ref (struct frame *f, Lisp_Object face_ref, Lisp_Object *to,
2331 int err_msgs, struct named_merge_point *named_merge_points)
2333 int ok = 1; /* Succeed without an error? */
2335 if (CONSP (face_ref))
2337 Lisp_Object first = XCAR (face_ref);
2339 if (EQ (first, Qforeground_color)
2340 || EQ (first, Qbackground_color))
2342 /* One of (FOREGROUND-COLOR . COLOR) or (BACKGROUND-COLOR
2343 . COLOR). COLOR must be a string. */
2344 Lisp_Object color_name = XCDR (face_ref);
2345 Lisp_Object color = first;
2347 if (STRINGP (color_name))
2349 if (EQ (color, Qforeground_color))
2350 to[LFACE_FOREGROUND_INDEX] = color_name;
2351 else
2352 to[LFACE_BACKGROUND_INDEX] = color_name;
2354 else
2356 if (err_msgs)
2357 add_to_log ("Invalid face color", color_name, Qnil);
2358 ok = 0;
2361 else if (SYMBOLP (first)
2362 && *SDATA (SYMBOL_NAME (first)) == ':')
2364 /* Assume this is the property list form. */
2365 while (CONSP (face_ref) && CONSP (XCDR (face_ref)))
2367 Lisp_Object keyword = XCAR (face_ref);
2368 Lisp_Object value = XCAR (XCDR (face_ref));
2369 int err = 0;
2371 /* Specifying `unspecified' is a no-op. */
2372 if (EQ (value, Qunspecified))
2374 else if (EQ (keyword, QCfamily))
2376 if (STRINGP (value))
2378 to[LFACE_FAMILY_INDEX] = value;
2379 font_clear_prop (to, FONT_FAMILY_INDEX);
2381 else
2382 err = 1;
2384 else if (EQ (keyword, QCfoundry))
2386 if (STRINGP (value))
2388 to[LFACE_FOUNDRY_INDEX] = value;
2389 font_clear_prop (to, FONT_FOUNDRY_INDEX);
2391 else
2392 err = 1;
2394 else if (EQ (keyword, QCheight))
2396 Lisp_Object new_height =
2397 merge_face_heights (value, to[LFACE_HEIGHT_INDEX], Qnil);
2399 if (! NILP (new_height))
2401 to[LFACE_HEIGHT_INDEX] = new_height;
2402 font_clear_prop (to, FONT_SIZE_INDEX);
2404 else
2405 err = 1;
2407 else if (EQ (keyword, QCweight))
2409 if (SYMBOLP (value) && FONT_WEIGHT_NAME_NUMERIC (value) >= 0)
2411 to[LFACE_WEIGHT_INDEX] = value;
2412 font_clear_prop (to, FONT_WEIGHT_INDEX);
2414 else
2415 err = 1;
2417 else if (EQ (keyword, QCslant))
2419 if (SYMBOLP (value) && FONT_SLANT_NAME_NUMERIC (value) >= 0)
2421 to[LFACE_SLANT_INDEX] = value;
2422 font_clear_prop (to, FONT_SLANT_INDEX);
2424 else
2425 err = 1;
2427 else if (EQ (keyword, QCunderline))
2429 if (EQ (value, Qt)
2430 || NILP (value)
2431 || STRINGP (value)
2432 || CONSP (value))
2433 to[LFACE_UNDERLINE_INDEX] = value;
2434 else
2435 err = 1;
2437 else if (EQ (keyword, QCoverline))
2439 if (EQ (value, Qt)
2440 || NILP (value)
2441 || STRINGP (value))
2442 to[LFACE_OVERLINE_INDEX] = value;
2443 else
2444 err = 1;
2446 else if (EQ (keyword, QCstrike_through))
2448 if (EQ (value, Qt)
2449 || NILP (value)
2450 || STRINGP (value))
2451 to[LFACE_STRIKE_THROUGH_INDEX] = value;
2452 else
2453 err = 1;
2455 else if (EQ (keyword, QCbox))
2457 if (EQ (value, Qt))
2458 value = make_number (1);
2459 if (INTEGERP (value)
2460 || STRINGP (value)
2461 || CONSP (value)
2462 || NILP (value))
2463 to[LFACE_BOX_INDEX] = value;
2464 else
2465 err = 1;
2467 else if (EQ (keyword, QCinverse_video)
2468 || EQ (keyword, QCreverse_video))
2470 if (EQ (value, Qt) || NILP (value))
2471 to[LFACE_INVERSE_INDEX] = value;
2472 else
2473 err = 1;
2475 else if (EQ (keyword, QCforeground))
2477 if (STRINGP (value))
2478 to[LFACE_FOREGROUND_INDEX] = value;
2479 else
2480 err = 1;
2482 else if (EQ (keyword, QCdistant_foreground))
2484 if (STRINGP (value))
2485 to[LFACE_DISTANT_FOREGROUND_INDEX] = value;
2486 else
2487 err = 1;
2489 else if (EQ (keyword, QCbackground))
2491 if (STRINGP (value))
2492 to[LFACE_BACKGROUND_INDEX] = value;
2493 else
2494 err = 1;
2496 else if (EQ (keyword, QCstipple))
2498 #if defined (HAVE_WINDOW_SYSTEM)
2499 Lisp_Object pixmap_p = Fbitmap_spec_p (value);
2500 if (!NILP (pixmap_p))
2501 to[LFACE_STIPPLE_INDEX] = value;
2502 else
2503 err = 1;
2504 #endif /* HAVE_WINDOW_SYSTEM */
2506 else if (EQ (keyword, QCwidth))
2508 if (SYMBOLP (value) && FONT_WIDTH_NAME_NUMERIC (value) >= 0)
2510 to[LFACE_SWIDTH_INDEX] = value;
2511 font_clear_prop (to, FONT_WIDTH_INDEX);
2513 else
2514 err = 1;
2516 else if (EQ (keyword, QCfont))
2518 if (FONTP (value))
2519 to[LFACE_FONT_INDEX] = value;
2520 else
2521 err = 1;
2523 else if (EQ (keyword, QCinherit))
2525 /* This is not really very useful; it's just like a
2526 normal face reference. */
2527 if (! merge_face_ref (f, value, to,
2528 err_msgs, named_merge_points))
2529 err = 1;
2531 else
2532 err = 1;
2534 if (err)
2536 add_to_log ("Invalid face attribute %S %S", keyword, value);
2537 ok = 0;
2540 face_ref = XCDR (XCDR (face_ref));
2543 else
2545 /* This is a list of face refs. Those at the beginning of the
2546 list take precedence over what follows, so we have to merge
2547 from the end backwards. */
2548 Lisp_Object next = XCDR (face_ref);
2550 if (! NILP (next))
2551 ok = merge_face_ref (f, next, to, err_msgs, named_merge_points);
2553 if (! merge_face_ref (f, first, to, err_msgs, named_merge_points))
2554 ok = 0;
2557 else
2559 /* FACE_REF ought to be a face name. */
2560 ok = merge_named_face (f, face_ref, to, named_merge_points);
2561 if (!ok && err_msgs)
2562 add_to_log ("Invalid face reference: %s", face_ref, Qnil);
2565 return ok;
2569 DEFUN ("internal-make-lisp-face", Finternal_make_lisp_face,
2570 Sinternal_make_lisp_face, 1, 2, 0,
2571 doc: /* Make FACE, a symbol, a Lisp face with all attributes nil.
2572 If FACE was not known as a face before, create a new one.
2573 If optional argument FRAME is specified, make a frame-local face
2574 for that frame. Otherwise operate on the global face definition.
2575 Value is a vector of face attributes. */)
2576 (Lisp_Object face, Lisp_Object frame)
2578 Lisp_Object global_lface, lface;
2579 struct frame *f;
2580 int i;
2582 CHECK_SYMBOL (face);
2583 global_lface = lface_from_face_name (NULL, face, 0);
2585 if (!NILP (frame))
2587 CHECK_LIVE_FRAME (frame);
2588 f = XFRAME (frame);
2589 lface = lface_from_face_name (f, face, 0);
2591 else
2592 f = NULL, lface = Qnil;
2594 /* Add a global definition if there is none. */
2595 if (NILP (global_lface))
2597 global_lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2598 Qunspecified);
2599 ASET (global_lface, 0, Qface);
2600 Vface_new_frame_defaults = Fcons (Fcons (face, global_lface),
2601 Vface_new_frame_defaults);
2603 /* Assign the new Lisp face a unique ID. The mapping from Lisp
2604 face id to Lisp face is given by the vector lface_id_to_name.
2605 The mapping from Lisp face to Lisp face id is given by the
2606 property `face' of the Lisp face name. */
2607 if (next_lface_id == lface_id_to_name_size)
2608 lface_id_to_name =
2609 xpalloc (lface_id_to_name, &lface_id_to_name_size, 1, MAX_FACE_ID,
2610 sizeof *lface_id_to_name);
2612 lface_id_to_name[next_lface_id] = face;
2613 Fput (face, Qface, make_number (next_lface_id));
2614 ++next_lface_id;
2616 else if (f == NULL)
2617 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2618 ASET (global_lface, i, Qunspecified);
2620 /* Add a frame-local definition. */
2621 if (f)
2623 if (NILP (lface))
2625 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2626 Qunspecified);
2627 ASET (lface, 0, Qface);
2628 fset_face_alist (f, Fcons (Fcons (face, lface), f->face_alist));
2630 else
2631 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2632 ASET (lface, i, Qunspecified);
2634 else
2635 lface = global_lface;
2637 /* Changing a named face means that all realized faces depending on
2638 that face are invalid. Since we cannot tell which realized faces
2639 depend on the face, make sure they are all removed. This is done
2640 by incrementing face_change_count. The next call to
2641 init_iterator will then free realized faces. */
2642 if (NILP (Fget (face, Qface_no_inherit)))
2644 ++face_change_count;
2645 windows_or_buffers_changed = 54;
2648 eassert (LFACEP (lface));
2649 check_lface (lface);
2650 return lface;
2654 DEFUN ("internal-lisp-face-p", Finternal_lisp_face_p,
2655 Sinternal_lisp_face_p, 1, 2, 0,
2656 doc: /* Return non-nil if FACE names a face.
2657 FACE should be a symbol or string.
2658 If optional second argument FRAME is non-nil, check for the
2659 existence of a frame-local face with name FACE on that frame.
2660 Otherwise check for the existence of a global face. */)
2661 (Lisp_Object face, Lisp_Object frame)
2663 Lisp_Object lface;
2665 face = resolve_face_name (face, 1);
2667 if (!NILP (frame))
2669 CHECK_LIVE_FRAME (frame);
2670 lface = lface_from_face_name (XFRAME (frame), face, 0);
2672 else
2673 lface = lface_from_face_name (NULL, face, 0);
2675 return lface;
2679 DEFUN ("internal-copy-lisp-face", Finternal_copy_lisp_face,
2680 Sinternal_copy_lisp_face, 4, 4, 0,
2681 doc: /* Copy face FROM to TO.
2682 If FRAME is t, copy the global face definition of FROM.
2683 Otherwise, copy the frame-local definition of FROM on FRAME.
2684 If NEW-FRAME is a frame, copy that data into the frame-local
2685 definition of TO on NEW-FRAME. If NEW-FRAME is nil,
2686 FRAME controls where the data is copied to.
2688 The value is TO. */)
2689 (Lisp_Object from, Lisp_Object to, Lisp_Object frame, Lisp_Object new_frame)
2691 Lisp_Object lface, copy;
2693 CHECK_SYMBOL (from);
2694 CHECK_SYMBOL (to);
2696 if (EQ (frame, Qt))
2698 /* Copy global definition of FROM. We don't make copies of
2699 strings etc. because 20.2 didn't do it either. */
2700 lface = lface_from_face_name (NULL, from, 1);
2701 copy = Finternal_make_lisp_face (to, Qnil);
2703 else
2705 /* Copy frame-local definition of FROM. */
2706 if (NILP (new_frame))
2707 new_frame = frame;
2708 CHECK_LIVE_FRAME (frame);
2709 CHECK_LIVE_FRAME (new_frame);
2710 lface = lface_from_face_name (XFRAME (frame), from, 1);
2711 copy = Finternal_make_lisp_face (to, new_frame);
2714 vcopy (copy, 0, XVECTOR (lface)->contents, LFACE_VECTOR_SIZE);
2716 /* Changing a named face means that all realized faces depending on
2717 that face are invalid. Since we cannot tell which realized faces
2718 depend on the face, make sure they are all removed. This is done
2719 by incrementing face_change_count. The next call to
2720 init_iterator will then free realized faces. */
2721 if (NILP (Fget (to, Qface_no_inherit)))
2723 ++face_change_count;
2724 windows_or_buffers_changed = 55;
2727 return to;
2731 DEFUN ("internal-set-lisp-face-attribute", Finternal_set_lisp_face_attribute,
2732 Sinternal_set_lisp_face_attribute, 3, 4, 0,
2733 doc: /* Set attribute ATTR of FACE to VALUE.
2734 FRAME being a frame means change the face on that frame.
2735 FRAME nil means change the face of the selected frame.
2736 FRAME t means change the default for new frames.
2737 FRAME 0 means change the face on all frames, and change the default
2738 for new frames. */)
2739 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
2741 Lisp_Object lface;
2742 Lisp_Object old_value = Qnil;
2743 /* Set one of enum font_property_index (> 0) if ATTR is one of
2744 font-related attributes other than QCfont and QCfontset. */
2745 enum font_property_index prop_index = 0;
2747 CHECK_SYMBOL (face);
2748 CHECK_SYMBOL (attr);
2750 face = resolve_face_name (face, 1);
2752 /* If FRAME is 0, change face on all frames, and change the
2753 default for new frames. */
2754 if (INTEGERP (frame) && XINT (frame) == 0)
2756 Lisp_Object tail;
2757 Finternal_set_lisp_face_attribute (face, attr, value, Qt);
2758 FOR_EACH_FRAME (tail, frame)
2759 Finternal_set_lisp_face_attribute (face, attr, value, frame);
2760 return face;
2763 /* Set lface to the Lisp attribute vector of FACE. */
2764 if (EQ (frame, Qt))
2766 lface = lface_from_face_name (NULL, face, 1);
2768 /* When updating face-new-frame-defaults, we put :ignore-defface
2769 where the caller wants `unspecified'. This forces the frame
2770 defaults to ignore the defface value. Otherwise, the defface
2771 will take effect, which is generally not what is intended.
2772 The value of that attribute will be inherited from some other
2773 face during face merging. See internal_merge_in_global_face. */
2774 if (UNSPECIFIEDP (value))
2775 value = QCignore_defface;
2777 else
2779 if (NILP (frame))
2780 frame = selected_frame;
2782 CHECK_LIVE_FRAME (frame);
2783 lface = lface_from_face_name (XFRAME (frame), face, 0);
2785 /* If a frame-local face doesn't exist yet, create one. */
2786 if (NILP (lface))
2787 lface = Finternal_make_lisp_face (face, frame);
2790 if (EQ (attr, QCfamily))
2792 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2794 CHECK_STRING (value);
2795 if (SCHARS (value) == 0)
2796 signal_error ("Invalid face family", value);
2798 old_value = LFACE_FAMILY (lface);
2799 ASET (lface, LFACE_FAMILY_INDEX, value);
2800 prop_index = FONT_FAMILY_INDEX;
2802 else if (EQ (attr, QCfoundry))
2804 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2806 CHECK_STRING (value);
2807 if (SCHARS (value) == 0)
2808 signal_error ("Invalid face foundry", value);
2810 old_value = LFACE_FOUNDRY (lface);
2811 ASET (lface, LFACE_FOUNDRY_INDEX, value);
2812 prop_index = FONT_FOUNDRY_INDEX;
2814 else if (EQ (attr, QCheight))
2816 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2818 if (EQ (face, Qdefault))
2820 /* The default face must have an absolute size. */
2821 if (!INTEGERP (value) || XINT (value) <= 0)
2822 signal_error ("Default face height not absolute and positive",
2823 value);
2825 else
2827 /* For non-default faces, do a test merge with a random
2828 height to see if VALUE's ok. */
2829 Lisp_Object test = merge_face_heights (value,
2830 make_number (10),
2831 Qnil);
2832 if (!INTEGERP (test) || XINT (test) <= 0)
2833 signal_error ("Face height does not produce a positive integer",
2834 value);
2838 old_value = LFACE_HEIGHT (lface);
2839 ASET (lface, LFACE_HEIGHT_INDEX, value);
2840 prop_index = FONT_SIZE_INDEX;
2842 else if (EQ (attr, QCweight))
2844 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2846 CHECK_SYMBOL (value);
2847 if (FONT_WEIGHT_NAME_NUMERIC (value) < 0)
2848 signal_error ("Invalid face weight", value);
2850 old_value = LFACE_WEIGHT (lface);
2851 ASET (lface, LFACE_WEIGHT_INDEX, value);
2852 prop_index = FONT_WEIGHT_INDEX;
2854 else if (EQ (attr, QCslant))
2856 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2858 CHECK_SYMBOL (value);
2859 if (FONT_SLANT_NAME_NUMERIC (value) < 0)
2860 signal_error ("Invalid face slant", value);
2862 old_value = LFACE_SLANT (lface);
2863 ASET (lface, LFACE_SLANT_INDEX, value);
2864 prop_index = FONT_SLANT_INDEX;
2866 else if (EQ (attr, QCunderline))
2868 bool valid_p = 0;
2870 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2871 valid_p = 1;
2872 else if (NILP (value) || EQ (value, Qt))
2873 valid_p = 1;
2874 else if (STRINGP (value) && SCHARS (value) > 0)
2875 valid_p = 1;
2876 else if (CONSP (value))
2878 Lisp_Object key, val, list;
2880 list = value;
2881 /* FIXME? This errs on the side of acceptance. Eg it accepts:
2882 (defface foo '((t :underline 'foo) "doc")
2883 Maybe this is intentional, maybe it isn't.
2884 Non-nil symbols other than t are not documented as being valid.
2885 Eg compare with inverse-video, which explicitly rejects them.
2887 valid_p = 1;
2889 while (!NILP (CAR_SAFE(list)))
2891 key = CAR_SAFE (list);
2892 list = CDR_SAFE (list);
2893 val = CAR_SAFE (list);
2894 list = CDR_SAFE (list);
2896 if (NILP (key) || NILP (val))
2898 valid_p = 0;
2899 break;
2902 else if (EQ (key, QCcolor)
2903 && !(EQ (val, Qforeground_color)
2904 || (STRINGP (val) && SCHARS (val) > 0)))
2906 valid_p = 0;
2907 break;
2910 else if (EQ (key, QCstyle)
2911 && !(EQ (val, Qline) || EQ (val, Qwave)))
2913 valid_p = 0;
2914 break;
2919 if (!valid_p)
2920 signal_error ("Invalid face underline", value);
2922 old_value = LFACE_UNDERLINE (lface);
2923 ASET (lface, LFACE_UNDERLINE_INDEX, value);
2925 else if (EQ (attr, QCoverline))
2927 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2928 if ((SYMBOLP (value)
2929 && !EQ (value, Qt)
2930 && !EQ (value, Qnil))
2931 /* Overline color. */
2932 || (STRINGP (value)
2933 && SCHARS (value) == 0))
2934 signal_error ("Invalid face overline", value);
2936 old_value = LFACE_OVERLINE (lface);
2937 ASET (lface, LFACE_OVERLINE_INDEX, value);
2939 else if (EQ (attr, QCstrike_through))
2941 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2942 if ((SYMBOLP (value)
2943 && !EQ (value, Qt)
2944 && !EQ (value, Qnil))
2945 /* Strike-through color. */
2946 || (STRINGP (value)
2947 && SCHARS (value) == 0))
2948 signal_error ("Invalid face strike-through", value);
2950 old_value = LFACE_STRIKE_THROUGH (lface);
2951 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, value);
2953 else if (EQ (attr, QCbox))
2955 bool valid_p;
2957 /* Allow t meaning a simple box of width 1 in foreground color
2958 of the face. */
2959 if (EQ (value, Qt))
2960 value = make_number (1);
2962 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2963 valid_p = 1;
2964 else if (NILP (value))
2965 valid_p = 1;
2966 else if (INTEGERP (value))
2967 valid_p = XINT (value) != 0;
2968 else if (STRINGP (value))
2969 valid_p = SCHARS (value) > 0;
2970 else if (CONSP (value))
2972 Lisp_Object tem;
2974 tem = value;
2975 while (CONSP (tem))
2977 Lisp_Object k, v;
2979 k = XCAR (tem);
2980 tem = XCDR (tem);
2981 if (!CONSP (tem))
2982 break;
2983 v = XCAR (tem);
2984 tem = XCDR (tem);
2986 if (EQ (k, QCline_width))
2988 if (!INTEGERP (v) || XINT (v) == 0)
2989 break;
2991 else if (EQ (k, QCcolor))
2993 if (!NILP (v) && (!STRINGP (v) || SCHARS (v) == 0))
2994 break;
2996 else if (EQ (k, QCstyle))
2998 if (!EQ (v, Qpressed_button) && !EQ (v, Qreleased_button))
2999 break;
3001 else
3002 break;
3005 valid_p = NILP (tem);
3007 else
3008 valid_p = 0;
3010 if (!valid_p)
3011 signal_error ("Invalid face box", value);
3013 old_value = LFACE_BOX (lface);
3014 ASET (lface, LFACE_BOX_INDEX, value);
3016 else if (EQ (attr, QCinverse_video)
3017 || EQ (attr, QCreverse_video))
3019 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3021 CHECK_SYMBOL (value);
3022 if (!EQ (value, Qt) && !NILP (value))
3023 signal_error ("Invalid inverse-video face attribute value", value);
3025 old_value = LFACE_INVERSE (lface);
3026 ASET (lface, LFACE_INVERSE_INDEX, value);
3028 else if (EQ (attr, QCforeground))
3030 /* Compatibility with 20.x. */
3031 if (NILP (value))
3032 value = Qunspecified;
3033 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3035 /* Don't check for valid color names here because it depends
3036 on the frame (display) whether the color will be valid
3037 when the face is realized. */
3038 CHECK_STRING (value);
3039 if (SCHARS (value) == 0)
3040 signal_error ("Empty foreground color value", value);
3042 old_value = LFACE_FOREGROUND (lface);
3043 ASET (lface, LFACE_FOREGROUND_INDEX, value);
3045 else if (EQ (attr, QCdistant_foreground))
3047 /* Compatibility with 20.x. */
3048 if (NILP (value))
3049 value = Qunspecified;
3050 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3052 /* Don't check for valid color names here because it depends
3053 on the frame (display) whether the color will be valid
3054 when the face is realized. */
3055 CHECK_STRING (value);
3056 if (SCHARS (value) == 0)
3057 signal_error ("Empty distant-foreground color value", value);
3059 old_value = LFACE_DISTANT_FOREGROUND (lface);
3060 ASET (lface, LFACE_DISTANT_FOREGROUND_INDEX, value);
3062 else if (EQ (attr, QCbackground))
3064 /* Compatibility with 20.x. */
3065 if (NILP (value))
3066 value = Qunspecified;
3067 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3069 /* Don't check for valid color names here because it depends
3070 on the frame (display) whether the color will be valid
3071 when the face is realized. */
3072 CHECK_STRING (value);
3073 if (SCHARS (value) == 0)
3074 signal_error ("Empty background color value", value);
3076 old_value = LFACE_BACKGROUND (lface);
3077 ASET (lface, LFACE_BACKGROUND_INDEX, value);
3079 else if (EQ (attr, QCstipple))
3081 #if defined (HAVE_WINDOW_SYSTEM)
3082 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3083 && !NILP (value)
3084 && NILP (Fbitmap_spec_p (value)))
3085 signal_error ("Invalid stipple attribute", value);
3086 old_value = LFACE_STIPPLE (lface);
3087 ASET (lface, LFACE_STIPPLE_INDEX, value);
3088 #endif /* HAVE_WINDOW_SYSTEM */
3090 else if (EQ (attr, QCwidth))
3092 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3094 CHECK_SYMBOL (value);
3095 if (FONT_WIDTH_NAME_NUMERIC (value) < 0)
3096 signal_error ("Invalid face width", value);
3098 old_value = LFACE_SWIDTH (lface);
3099 ASET (lface, LFACE_SWIDTH_INDEX, value);
3100 prop_index = FONT_WIDTH_INDEX;
3102 else if (EQ (attr, QCfont))
3104 #ifdef HAVE_WINDOW_SYSTEM
3105 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
3107 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3109 struct frame *f;
3111 old_value = LFACE_FONT (lface);
3112 if (! FONTP (value))
3114 if (STRINGP (value))
3116 Lisp_Object name = value;
3117 int fontset = fs_query_fontset (name, 0);
3119 if (fontset >= 0)
3120 name = fontset_ascii (fontset);
3121 value = font_spec_from_name (name);
3122 if (!FONTP (value))
3123 signal_error ("Invalid font name", name);
3125 else
3126 signal_error ("Invalid font or font-spec", value);
3128 if (EQ (frame, Qt))
3129 f = XFRAME (selected_frame);
3130 else
3131 f = XFRAME (frame);
3132 if (! FONT_OBJECT_P (value))
3134 Lisp_Object *attrs = XVECTOR (lface)->contents;
3135 Lisp_Object font_object;
3137 font_object = font_load_for_lface (f, attrs, value);
3138 if (NILP (font_object))
3139 signal_error ("Font not available", value);
3140 value = font_object;
3142 set_lface_from_font (f, lface, value, 1);
3144 else
3145 ASET (lface, LFACE_FONT_INDEX, value);
3147 #endif /* HAVE_WINDOW_SYSTEM */
3149 else if (EQ (attr, QCfontset))
3151 #ifdef HAVE_WINDOW_SYSTEM
3152 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
3154 Lisp_Object tmp;
3156 old_value = LFACE_FONTSET (lface);
3157 tmp = Fquery_fontset (value, Qnil);
3158 if (NILP (tmp))
3159 signal_error ("Invalid fontset name", value);
3160 ASET (lface, LFACE_FONTSET_INDEX, value = tmp);
3162 #endif /* HAVE_WINDOW_SYSTEM */
3164 else if (EQ (attr, QCinherit))
3166 Lisp_Object tail;
3167 if (SYMBOLP (value))
3168 tail = Qnil;
3169 else
3170 for (tail = value; CONSP (tail); tail = XCDR (tail))
3171 if (!SYMBOLP (XCAR (tail)))
3172 break;
3173 if (NILP (tail))
3174 ASET (lface, LFACE_INHERIT_INDEX, value);
3175 else
3176 signal_error ("Invalid face inheritance", value);
3178 else if (EQ (attr, QCbold))
3180 old_value = LFACE_WEIGHT (lface);
3181 ASET (lface, LFACE_WEIGHT_INDEX, NILP (value) ? Qnormal : Qbold);
3182 prop_index = FONT_WEIGHT_INDEX;
3184 else if (EQ (attr, QCitalic))
3186 attr = QCslant;
3187 old_value = LFACE_SLANT (lface);
3188 ASET (lface, LFACE_SLANT_INDEX, NILP (value) ? Qnormal : Qitalic);
3189 prop_index = FONT_SLANT_INDEX;
3191 else
3192 signal_error ("Invalid face attribute name", attr);
3194 if (prop_index)
3196 /* If a font-related attribute other than QCfont and QCfontset
3197 is specified, and if the original QCfont attribute has a font
3198 (font-spec or font-object), set the corresponding property in
3199 the font to nil so that the font selector doesn't think that
3200 the attribute is mandatory. Also, clear the average
3201 width. */
3202 font_clear_prop (XVECTOR (lface)->contents, prop_index);
3205 /* Changing a named face means that all realized faces depending on
3206 that face are invalid. Since we cannot tell which realized faces
3207 depend on the face, make sure they are all removed. This is done
3208 by incrementing face_change_count. The next call to
3209 init_iterator will then free realized faces. */
3210 if (!EQ (frame, Qt)
3211 && NILP (Fget (face, Qface_no_inherit))
3212 && NILP (Fequal (old_value, value)))
3214 ++face_change_count;
3215 windows_or_buffers_changed = 56;
3218 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3219 && NILP (Fequal (old_value, value)))
3221 Lisp_Object param;
3223 param = Qnil;
3225 if (EQ (face, Qdefault))
3227 #ifdef HAVE_WINDOW_SYSTEM
3228 /* Changed font-related attributes of the `default' face are
3229 reflected in changed `font' frame parameters. */
3230 if (FRAMEP (frame)
3231 && (prop_index || EQ (attr, QCfont))
3232 && lface_fully_specified_p (XVECTOR (lface)->contents))
3233 set_font_frame_param (frame, lface);
3234 else
3235 #endif /* HAVE_WINDOW_SYSTEM */
3237 if (EQ (attr, QCforeground))
3238 param = Qforeground_color;
3239 else if (EQ (attr, QCbackground))
3240 param = Qbackground_color;
3242 #ifdef HAVE_WINDOW_SYSTEM
3243 #ifndef HAVE_NTGUI
3244 else if (EQ (face, Qscroll_bar))
3246 /* Changing the colors of `scroll-bar' sets frame parameters
3247 `scroll-bar-foreground' and `scroll-bar-background'. */
3248 if (EQ (attr, QCforeground))
3249 param = Qscroll_bar_foreground;
3250 else if (EQ (attr, QCbackground))
3251 param = Qscroll_bar_background;
3253 #endif /* not HAVE_NTGUI */
3254 else if (EQ (face, Qborder))
3256 /* Changing background color of `border' sets frame parameter
3257 `border-color'. */
3258 if (EQ (attr, QCbackground))
3259 param = Qborder_color;
3261 else if (EQ (face, Qcursor))
3263 /* Changing background color of `cursor' sets frame parameter
3264 `cursor-color'. */
3265 if (EQ (attr, QCbackground))
3266 param = Qcursor_color;
3268 else if (EQ (face, Qmouse))
3270 /* Changing background color of `mouse' sets frame parameter
3271 `mouse-color'. */
3272 if (EQ (attr, QCbackground))
3273 param = Qmouse_color;
3275 #endif /* HAVE_WINDOW_SYSTEM */
3276 else if (EQ (face, Qmenu))
3278 /* Indicate that we have to update the menu bar when realizing
3279 faces on FRAME. FRAME t change the default for new frames.
3280 We do this by setting the flag in new face caches. */
3281 if (FRAMEP (frame))
3283 struct frame *f = XFRAME (frame);
3284 if (FRAME_FACE_CACHE (f) == NULL)
3285 FRAME_FACE_CACHE (f) = make_face_cache (f);
3286 FRAME_FACE_CACHE (f)->menu_face_changed_p = 1;
3288 else
3289 menu_face_changed_default = 1;
3292 if (!NILP (param))
3294 if (EQ (frame, Qt))
3295 /* Update `default-frame-alist', which is used for new frames. */
3297 store_in_alist (&Vdefault_frame_alist, param, value);
3299 else
3300 /* Update the current frame's parameters. */
3302 Lisp_Object cons;
3303 cons = XCAR (Vparam_value_alist);
3304 XSETCAR (cons, param);
3305 XSETCDR (cons, value);
3306 Fmodify_frame_parameters (frame, Vparam_value_alist);
3311 return face;
3315 /* Update the corresponding face when frame parameter PARAM on frame F
3316 has been assigned the value NEW_VALUE. */
3318 void
3319 update_face_from_frame_parameter (struct frame *f, Lisp_Object param,
3320 Lisp_Object new_value)
3322 Lisp_Object face = Qnil;
3323 Lisp_Object lface;
3325 /* If there are no faces yet, give up. This is the case when called
3326 from Fx_create_frame, and we do the necessary things later in
3327 face-set-after-frame-defaults. */
3328 if (NILP (f->face_alist))
3329 return;
3331 if (EQ (param, Qforeground_color))
3333 face = Qdefault;
3334 lface = lface_from_face_name (f, face, 1);
3335 ASET (lface, LFACE_FOREGROUND_INDEX,
3336 (STRINGP (new_value) ? new_value : Qunspecified));
3337 realize_basic_faces (f);
3339 else if (EQ (param, Qbackground_color))
3341 Lisp_Object frame;
3343 /* Changing the background color might change the background
3344 mode, so that we have to load new defface specs.
3345 Call frame-set-background-mode to do that. */
3346 XSETFRAME (frame, f);
3347 call1 (Qframe_set_background_mode, frame);
3349 face = Qdefault;
3350 lface = lface_from_face_name (f, face, 1);
3351 ASET (lface, LFACE_BACKGROUND_INDEX,
3352 (STRINGP (new_value) ? new_value : Qunspecified));
3353 realize_basic_faces (f);
3355 #ifdef HAVE_WINDOW_SYSTEM
3356 else if (EQ (param, Qborder_color))
3358 face = Qborder;
3359 lface = lface_from_face_name (f, face, 1);
3360 ASET (lface, LFACE_BACKGROUND_INDEX,
3361 (STRINGP (new_value) ? new_value : Qunspecified));
3363 else if (EQ (param, Qcursor_color))
3365 face = Qcursor;
3366 lface = lface_from_face_name (f, face, 1);
3367 ASET (lface, LFACE_BACKGROUND_INDEX,
3368 (STRINGP (new_value) ? new_value : Qunspecified));
3370 else if (EQ (param, Qmouse_color))
3372 face = Qmouse;
3373 lface = lface_from_face_name (f, face, 1);
3374 ASET (lface, LFACE_BACKGROUND_INDEX,
3375 (STRINGP (new_value) ? new_value : Qunspecified));
3377 #endif
3379 /* Changing a named face means that all realized faces depending on
3380 that face are invalid. Since we cannot tell which realized faces
3381 depend on the face, make sure they are all removed. This is done
3382 by incrementing face_change_count. The next call to
3383 init_iterator will then free realized faces. */
3384 if (!NILP (face)
3385 && NILP (Fget (face, Qface_no_inherit)))
3387 ++face_change_count;
3388 windows_or_buffers_changed = 57;
3393 #ifdef HAVE_WINDOW_SYSTEM
3395 /* Set the `font' frame parameter of FRAME determined from the
3396 font-object set in `default' face attributes LFACE. */
3398 static void
3399 set_font_frame_param (Lisp_Object frame, Lisp_Object lface)
3401 struct frame *f = XFRAME (frame);
3402 Lisp_Object font;
3404 if (FRAME_WINDOW_P (f)
3405 /* Don't do anything if the font is `unspecified'. This can
3406 happen during frame creation. */
3407 && (font = LFACE_FONT (lface),
3408 ! UNSPECIFIEDP (font)))
3410 if (FONT_SPEC_P (font))
3412 font = font_load_for_lface (f, XVECTOR (lface)->contents, font);
3413 if (NILP (font))
3414 return;
3415 ASET (lface, LFACE_FONT_INDEX, font);
3417 f->default_face_done_p = 0;
3418 Fmodify_frame_parameters (frame, list1 (Fcons (Qfont, font)));
3422 DEFUN ("internal-face-x-get-resource", Finternal_face_x_get_resource,
3423 Sinternal_face_x_get_resource, 2, 3, 0,
3424 doc: /* Get the value of X resource RESOURCE, class CLASS.
3425 Returned value is for the display of frame FRAME. If FRAME is not
3426 specified or nil, use selected frame. This function exists because
3427 ordinary `x-get-resource' doesn't take a frame argument. */)
3428 (Lisp_Object resource, Lisp_Object class, Lisp_Object frame)
3430 Lisp_Object value = Qnil;
3431 struct frame *f;
3433 CHECK_STRING (resource);
3434 CHECK_STRING (class);
3435 f = decode_live_frame (frame);
3436 block_input ();
3437 value = display_x_get_resource (FRAME_DISPLAY_INFO (f),
3438 resource, class, Qnil, Qnil);
3439 unblock_input ();
3440 return value;
3444 /* Return resource string VALUE as a boolean value, i.e. nil, or t.
3445 If VALUE is "on" or "true", return t. If VALUE is "off" or
3446 "false", return nil. Otherwise, if SIGNAL_P is non-zero, signal an
3447 error; if SIGNAL_P is zero, return 0. */
3449 static Lisp_Object
3450 face_boolean_x_resource_value (Lisp_Object value, int signal_p)
3452 Lisp_Object result = make_number (0);
3454 eassert (STRINGP (value));
3456 if (xstrcasecmp (SSDATA (value), "on") == 0
3457 || xstrcasecmp (SSDATA (value), "true") == 0)
3458 result = Qt;
3459 else if (xstrcasecmp (SSDATA (value), "off") == 0
3460 || xstrcasecmp (SSDATA (value), "false") == 0)
3461 result = Qnil;
3462 else if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3463 result = Qunspecified;
3464 else if (signal_p)
3465 signal_error ("Invalid face attribute value from X resource", value);
3467 return result;
3471 DEFUN ("internal-set-lisp-face-attribute-from-resource",
3472 Finternal_set_lisp_face_attribute_from_resource,
3473 Sinternal_set_lisp_face_attribute_from_resource,
3474 3, 4, 0, doc: /* */)
3475 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
3477 CHECK_SYMBOL (face);
3478 CHECK_SYMBOL (attr);
3479 CHECK_STRING (value);
3481 if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3482 value = Qunspecified;
3483 else if (EQ (attr, QCheight))
3485 value = Fstring_to_number (value, make_number (10));
3486 if (XINT (value) <= 0)
3487 signal_error ("Invalid face height from X resource", value);
3489 else if (EQ (attr, QCbold) || EQ (attr, QCitalic))
3490 value = face_boolean_x_resource_value (value, 1);
3491 else if (EQ (attr, QCweight) || EQ (attr, QCslant) || EQ (attr, QCwidth))
3492 value = intern (SSDATA (value));
3493 else if (EQ (attr, QCreverse_video) || EQ (attr, QCinverse_video))
3494 value = face_boolean_x_resource_value (value, 1);
3495 else if (EQ (attr, QCunderline)
3496 || EQ (attr, QCoverline)
3497 || EQ (attr, QCstrike_through))
3499 Lisp_Object boolean_value;
3501 /* If the result of face_boolean_x_resource_value is t or nil,
3502 VALUE does NOT specify a color. */
3503 boolean_value = face_boolean_x_resource_value (value, 0);
3504 if (SYMBOLP (boolean_value))
3505 value = boolean_value;
3507 else if (EQ (attr, QCbox) || EQ (attr, QCinherit))
3508 value = Fcar (Fread_from_string (value, Qnil, Qnil));
3510 return Finternal_set_lisp_face_attribute (face, attr, value, frame);
3513 #endif /* HAVE_WINDOW_SYSTEM */
3516 /***********************************************************************
3517 Menu face
3518 ***********************************************************************/
3520 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
3522 /* Make menus on frame F appear as specified by the `menu' face. */
3524 static void
3525 x_update_menu_appearance (struct frame *f)
3527 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
3528 XrmDatabase rdb;
3530 if (dpyinfo
3531 && (rdb = XrmGetDatabase (FRAME_X_DISPLAY (f)),
3532 rdb != NULL))
3534 char line[512];
3535 char *buf = line;
3536 ptrdiff_t bufsize = sizeof line;
3537 Lisp_Object lface = lface_from_face_name (f, Qmenu, 1);
3538 struct face *face = FACE_FROM_ID (f, MENU_FACE_ID);
3539 const char *myname = SSDATA (Vx_resource_name);
3540 bool changed_p = 0;
3541 #ifdef USE_MOTIF
3542 const char *popup_path = "popup_menu";
3543 #else
3544 const char *popup_path = "menu.popup";
3545 #endif
3547 if (STRINGP (LFACE_FOREGROUND (lface)))
3549 exprintf (&buf, &bufsize, line, -1, "%s.%s*foreground: %s",
3550 myname, popup_path,
3551 SDATA (LFACE_FOREGROUND (lface)));
3552 XrmPutLineResource (&rdb, line);
3553 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*foreground: %s",
3554 myname, SDATA (LFACE_FOREGROUND (lface)));
3555 XrmPutLineResource (&rdb, line);
3556 changed_p = 1;
3559 if (STRINGP (LFACE_BACKGROUND (lface)))
3561 exprintf (&buf, &bufsize, line, -1, "%s.%s*background: %s",
3562 myname, popup_path,
3563 SDATA (LFACE_BACKGROUND (lface)));
3564 XrmPutLineResource (&rdb, line);
3566 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*background: %s",
3567 myname, SDATA (LFACE_BACKGROUND (lface)));
3568 XrmPutLineResource (&rdb, line);
3569 changed_p = 1;
3572 if (face->font
3573 /* On Solaris 5.8, it's been reported that the `menu' face
3574 can be unspecified here, during startup. Why this
3575 happens remains unknown. -- cyd */
3576 && FONTP (LFACE_FONT (lface))
3577 && (!UNSPECIFIEDP (LFACE_FAMILY (lface))
3578 || !UNSPECIFIEDP (LFACE_FOUNDRY (lface))
3579 || !UNSPECIFIEDP (LFACE_SWIDTH (lface))
3580 || !UNSPECIFIEDP (LFACE_WEIGHT (lface))
3581 || !UNSPECIFIEDP (LFACE_SLANT (lface))
3582 || !UNSPECIFIEDP (LFACE_HEIGHT (lface))))
3584 Lisp_Object xlfd = Ffont_xlfd_name (LFACE_FONT (lface), Qnil);
3585 #ifdef USE_MOTIF
3586 const char *suffix = "List";
3587 Bool motif = True;
3588 #else
3589 #if defined HAVE_X_I18N
3591 const char *suffix = "Set";
3592 #else
3593 const char *suffix = "";
3594 #endif
3595 Bool motif = False;
3596 #endif
3598 if (! NILP (xlfd))
3600 #if defined HAVE_X_I18N
3601 char *fontsetname = xic_create_fontsetname (SSDATA (xlfd), motif);
3602 #else
3603 char *fontsetname = SSDATA (xlfd);
3604 #endif
3605 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*font%s: %s",
3606 myname, suffix, fontsetname);
3607 XrmPutLineResource (&rdb, line);
3609 exprintf (&buf, &bufsize, line, -1, "%s.%s*font%s: %s",
3610 myname, popup_path, suffix, fontsetname);
3611 XrmPutLineResource (&rdb, line);
3612 changed_p = 1;
3613 if (fontsetname != SSDATA (xlfd))
3614 xfree (fontsetname);
3618 if (changed_p && f->output_data.x->menubar_widget)
3619 free_frame_menubar (f);
3621 if (buf != line)
3622 xfree (buf);
3626 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
3629 DEFUN ("face-attribute-relative-p", Fface_attribute_relative_p,
3630 Sface_attribute_relative_p,
3631 2, 2, 0,
3632 doc: /* Check whether a face attribute value is relative.
3633 Specifically, this function returns t if the attribute ATTRIBUTE
3634 with the value VALUE is relative.
3636 A relative value is one that doesn't entirely override whatever is
3637 inherited from another face. For most possible attributes,
3638 the only relative value that users see is `unspecified'.
3639 However, for :height, floating point values are also relative. */)
3640 (Lisp_Object attribute, Lisp_Object value)
3642 if (EQ (value, Qunspecified) || (EQ (value, QCignore_defface)))
3643 return Qt;
3644 else if (EQ (attribute, QCheight))
3645 return INTEGERP (value) ? Qnil : Qt;
3646 else
3647 return Qnil;
3650 DEFUN ("merge-face-attribute", Fmerge_face_attribute, Smerge_face_attribute,
3651 3, 3, 0,
3652 doc: /* Return face ATTRIBUTE VALUE1 merged with VALUE2.
3653 If VALUE1 or VALUE2 are absolute (see `face-attribute-relative-p'), then
3654 the result will be absolute, otherwise it will be relative. */)
3655 (Lisp_Object attribute, Lisp_Object value1, Lisp_Object value2)
3657 if (EQ (value1, Qunspecified) || EQ (value1, QCignore_defface))
3658 return value2;
3659 else if (EQ (attribute, QCheight))
3660 return merge_face_heights (value1, value2, value1);
3661 else
3662 return value1;
3666 DEFUN ("internal-get-lisp-face-attribute", Finternal_get_lisp_face_attribute,
3667 Sinternal_get_lisp_face_attribute,
3668 2, 3, 0,
3669 doc: /* Return face attribute KEYWORD of face SYMBOL.
3670 If SYMBOL does not name a valid Lisp face or KEYWORD isn't a valid
3671 face attribute name, signal an error.
3672 If the optional argument FRAME is given, report on face SYMBOL in that
3673 frame. If FRAME is t, report on the defaults for face SYMBOL (for new
3674 frames). If FRAME is omitted or nil, use the selected frame. */)
3675 (Lisp_Object symbol, Lisp_Object keyword, Lisp_Object frame)
3677 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3678 Lisp_Object lface = lface_from_face_name (f, symbol, 1), value = Qnil;
3680 CHECK_SYMBOL (symbol);
3681 CHECK_SYMBOL (keyword);
3683 if (EQ (keyword, QCfamily))
3684 value = LFACE_FAMILY (lface);
3685 else if (EQ (keyword, QCfoundry))
3686 value = LFACE_FOUNDRY (lface);
3687 else if (EQ (keyword, QCheight))
3688 value = LFACE_HEIGHT (lface);
3689 else if (EQ (keyword, QCweight))
3690 value = LFACE_WEIGHT (lface);
3691 else if (EQ (keyword, QCslant))
3692 value = LFACE_SLANT (lface);
3693 else if (EQ (keyword, QCunderline))
3694 value = LFACE_UNDERLINE (lface);
3695 else if (EQ (keyword, QCoverline))
3696 value = LFACE_OVERLINE (lface);
3697 else if (EQ (keyword, QCstrike_through))
3698 value = LFACE_STRIKE_THROUGH (lface);
3699 else if (EQ (keyword, QCbox))
3700 value = LFACE_BOX (lface);
3701 else if (EQ (keyword, QCinverse_video)
3702 || EQ (keyword, QCreverse_video))
3703 value = LFACE_INVERSE (lface);
3704 else if (EQ (keyword, QCforeground))
3705 value = LFACE_FOREGROUND (lface);
3706 else if (EQ (keyword, QCdistant_foreground))
3707 value = LFACE_DISTANT_FOREGROUND (lface);
3708 else if (EQ (keyword, QCbackground))
3709 value = LFACE_BACKGROUND (lface);
3710 else if (EQ (keyword, QCstipple))
3711 value = LFACE_STIPPLE (lface);
3712 else if (EQ (keyword, QCwidth))
3713 value = LFACE_SWIDTH (lface);
3714 else if (EQ (keyword, QCinherit))
3715 value = LFACE_INHERIT (lface);
3716 else if (EQ (keyword, QCfont))
3717 value = LFACE_FONT (lface);
3718 else if (EQ (keyword, QCfontset))
3719 value = LFACE_FONTSET (lface);
3720 else
3721 signal_error ("Invalid face attribute name", keyword);
3723 if (IGNORE_DEFFACE_P (value))
3724 return Qunspecified;
3726 return value;
3730 DEFUN ("internal-lisp-face-attribute-values",
3731 Finternal_lisp_face_attribute_values,
3732 Sinternal_lisp_face_attribute_values, 1, 1, 0,
3733 doc: /* Return a list of valid discrete values for face attribute ATTR.
3734 Value is nil if ATTR doesn't have a discrete set of valid values. */)
3735 (Lisp_Object attr)
3737 Lisp_Object result = Qnil;
3739 CHECK_SYMBOL (attr);
3741 if (EQ (attr, QCunderline) || EQ (attr, QCoverline)
3742 || EQ (attr, QCstrike_through)
3743 || EQ (attr, QCinverse_video) || EQ (attr, QCreverse_video))
3744 result = list2 (Qt, Qnil);
3746 return result;
3750 DEFUN ("internal-merge-in-global-face", Finternal_merge_in_global_face,
3751 Sinternal_merge_in_global_face, 2, 2, 0,
3752 doc: /* Add attributes from frame-default definition of FACE to FACE on FRAME.
3753 Default face attributes override any local face attributes. */)
3754 (Lisp_Object face, Lisp_Object frame)
3756 int i;
3757 Lisp_Object global_lface, local_lface, *gvec, *lvec;
3758 struct frame *f = XFRAME (frame);
3760 CHECK_LIVE_FRAME (frame);
3761 global_lface = lface_from_face_name (NULL, face, 1);
3762 local_lface = lface_from_face_name (f, face, 0);
3763 if (NILP (local_lface))
3764 local_lface = Finternal_make_lisp_face (face, frame);
3766 /* Make every specified global attribute override the local one.
3767 BEWARE!! This is only used from `face-set-after-frame-default' where
3768 the local frame is defined from default specs in `face-defface-spec'
3769 and those should be overridden by global settings. Hence the strange
3770 "global before local" priority. */
3771 lvec = XVECTOR (local_lface)->contents;
3772 gvec = XVECTOR (global_lface)->contents;
3773 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3774 if (IGNORE_DEFFACE_P (gvec[i]))
3775 ASET (local_lface, i, Qunspecified);
3776 else if (! UNSPECIFIEDP (gvec[i]))
3777 ASET (local_lface, i, AREF (global_lface, i));
3779 /* If the default face was changed, update the face cache and the
3780 `font' frame parameter. */
3781 if (EQ (face, Qdefault))
3783 struct face_cache *c = FRAME_FACE_CACHE (f);
3784 struct face *newface, *oldface = FACE_FROM_ID (f, DEFAULT_FACE_ID);
3785 Lisp_Object attrs[LFACE_VECTOR_SIZE];
3787 /* This can be NULL (e.g., in batch mode). */
3788 if (oldface)
3790 /* Ensure that the face vector is fully specified by merging
3791 the previously-cached vector. */
3792 memcpy (attrs, oldface->lface, sizeof attrs);
3793 merge_face_vectors (f, lvec, attrs, 0);
3794 vcopy (local_lface, 0, attrs, LFACE_VECTOR_SIZE);
3795 newface = realize_face (c, lvec, DEFAULT_FACE_ID);
3797 if ((! UNSPECIFIEDP (gvec[LFACE_FAMILY_INDEX])
3798 || ! UNSPECIFIEDP (gvec[LFACE_FOUNDRY_INDEX])
3799 || ! UNSPECIFIEDP (gvec[LFACE_HEIGHT_INDEX])
3800 || ! UNSPECIFIEDP (gvec[LFACE_WEIGHT_INDEX])
3801 || ! UNSPECIFIEDP (gvec[LFACE_SLANT_INDEX])
3802 || ! UNSPECIFIEDP (gvec[LFACE_SWIDTH_INDEX])
3803 || ! UNSPECIFIEDP (gvec[LFACE_FONT_INDEX]))
3804 && newface->font)
3806 Lisp_Object name = newface->font->props[FONT_NAME_INDEX];
3807 Fmodify_frame_parameters (frame, list1 (Fcons (Qfont, name)));
3810 if (STRINGP (gvec[LFACE_FOREGROUND_INDEX]))
3811 Fmodify_frame_parameters (frame,
3812 list1 (Fcons (Qforeground_color,
3813 gvec[LFACE_FOREGROUND_INDEX])));
3815 if (STRINGP (gvec[LFACE_BACKGROUND_INDEX]))
3816 Fmodify_frame_parameters (frame,
3817 list1 (Fcons (Qbackground_color,
3818 gvec[LFACE_BACKGROUND_INDEX])));
3822 return Qnil;
3826 /* The following function is implemented for compatibility with 20.2.
3827 The function is used in x-resolve-fonts when it is asked to
3828 return fonts with the same size as the font of a face. This is
3829 done in fontset.el. */
3831 DEFUN ("face-font", Fface_font, Sface_font, 1, 3, 0,
3832 doc: /* Return the font name of face FACE, or nil if it is unspecified.
3833 The font name is, by default, for ASCII characters.
3834 If the optional argument FRAME is given, report on face FACE in that frame.
3835 If FRAME is t, report on the defaults for face FACE (for new frames).
3836 The font default for a face is either nil, or a list
3837 of the form (bold), (italic) or (bold italic).
3838 If FRAME is omitted or nil, use the selected frame. And, in this case,
3839 if the optional third argument CHARACTER is given,
3840 return the font name used for CHARACTER. */)
3841 (Lisp_Object face, Lisp_Object frame, Lisp_Object character)
3843 if (EQ (frame, Qt))
3845 Lisp_Object result = Qnil;
3846 Lisp_Object lface = lface_from_face_name (NULL, face, 1);
3848 if (!UNSPECIFIEDP (LFACE_WEIGHT (lface))
3849 && !EQ (LFACE_WEIGHT (lface), Qnormal))
3850 result = Fcons (Qbold, result);
3852 if (!UNSPECIFIEDP (LFACE_SLANT (lface))
3853 && !EQ (LFACE_SLANT (lface), Qnormal))
3854 result = Fcons (Qitalic, result);
3856 return result;
3858 else
3860 struct frame *f = decode_live_frame (frame);
3861 int face_id = lookup_named_face (f, face, 1);
3862 struct face *fface = FACE_FROM_ID (f, face_id);
3864 if (! fface)
3865 return Qnil;
3866 #ifdef HAVE_WINDOW_SYSTEM
3867 if (FRAME_WINDOW_P (f) && !NILP (character))
3869 CHECK_CHARACTER (character);
3870 face_id = FACE_FOR_CHAR (f, fface, XINT (character), -1, Qnil);
3871 fface = FACE_FROM_ID (f, face_id);
3873 return (fface->font
3874 ? fface->font->props[FONT_NAME_INDEX]
3875 : Qnil);
3876 #else /* !HAVE_WINDOW_SYSTEM */
3877 return build_string (FRAME_MSDOS_P (f)
3878 ? "ms-dos"
3879 : FRAME_W32_P (f) ? "w32term"
3880 :"tty");
3881 #endif
3886 /* Compare face-attribute values v1 and v2 for equality. Value is non-zero if
3887 all attributes are `equal'. Tries to be fast because this function
3888 is called quite often. */
3890 static bool
3891 face_attr_equal_p (Lisp_Object v1, Lisp_Object v2)
3893 /* Type can differ, e.g. when one attribute is unspecified, i.e. nil,
3894 and the other is specified. */
3895 if (XTYPE (v1) != XTYPE (v2))
3896 return 0;
3898 if (EQ (v1, v2))
3899 return 1;
3901 switch (XTYPE (v1))
3903 case Lisp_String:
3904 if (SBYTES (v1) != SBYTES (v2))
3905 return 0;
3907 return memcmp (SDATA (v1), SDATA (v2), SBYTES (v1)) == 0;
3909 case_Lisp_Int:
3910 case Lisp_Symbol:
3911 return 0;
3913 default:
3914 return !NILP (Fequal (v1, v2));
3919 /* Compare face vectors V1 and V2 for equality. Value is non-zero if
3920 all attributes are `equal'. Tries to be fast because this function
3921 is called quite often. */
3923 static bool
3924 lface_equal_p (Lisp_Object *v1, Lisp_Object *v2)
3926 int i;
3927 bool equal_p = 1;
3929 for (i = 1; i < LFACE_VECTOR_SIZE && equal_p; ++i)
3930 equal_p = face_attr_equal_p (v1[i], v2[i]);
3932 return equal_p;
3936 DEFUN ("internal-lisp-face-equal-p", Finternal_lisp_face_equal_p,
3937 Sinternal_lisp_face_equal_p, 2, 3, 0,
3938 doc: /* True if FACE1 and FACE2 are equal.
3939 If the optional argument FRAME is given, report on FACE1 and FACE2 in that frame.
3940 If FRAME is t, report on the defaults for FACE1 and FACE2 (for new frames).
3941 If FRAME is omitted or nil, use the selected frame. */)
3942 (Lisp_Object face1, Lisp_Object face2, Lisp_Object frame)
3944 int equal_p;
3945 struct frame *f;
3946 Lisp_Object lface1, lface2;
3948 /* Don't use decode_window_system_frame here because this function
3949 is called before X frames exist. At that time, if FRAME is nil,
3950 selected_frame will be used which is the frame dumped with
3951 Emacs. That frame is not an X frame. */
3952 f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3954 lface1 = lface_from_face_name (f, face1, 1);
3955 lface2 = lface_from_face_name (f, face2, 1);
3956 equal_p = lface_equal_p (XVECTOR (lface1)->contents,
3957 XVECTOR (lface2)->contents);
3958 return equal_p ? Qt : Qnil;
3962 DEFUN ("internal-lisp-face-empty-p", Finternal_lisp_face_empty_p,
3963 Sinternal_lisp_face_empty_p, 1, 2, 0,
3964 doc: /* True if FACE has no attribute specified.
3965 If the optional argument FRAME is given, report on face FACE in that frame.
3966 If FRAME is t, report on the defaults for face FACE (for new frames).
3967 If FRAME is omitted or nil, use the selected frame. */)
3968 (Lisp_Object face, Lisp_Object frame)
3970 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3971 Lisp_Object lface = lface_from_face_name (f, face, 1);
3972 int i;
3974 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3975 if (!UNSPECIFIEDP (AREF (lface, i)))
3976 break;
3978 return i == LFACE_VECTOR_SIZE ? Qt : Qnil;
3982 DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist,
3983 0, 1, 0,
3984 doc: /* Return an alist of frame-local faces defined on FRAME.
3985 For internal use only. */)
3986 (Lisp_Object frame)
3988 return decode_live_frame (frame)->face_alist;
3992 /* Return a hash code for Lisp string STRING with case ignored. Used
3993 below in computing a hash value for a Lisp face. */
3995 static unsigned
3996 hash_string_case_insensitive (Lisp_Object string)
3998 const unsigned char *s;
3999 unsigned hash = 0;
4000 eassert (STRINGP (string));
4001 for (s = SDATA (string); *s; ++s)
4002 hash = (hash << 1) ^ c_tolower (*s);
4003 return hash;
4007 /* Return a hash code for face attribute vector V. */
4009 static unsigned
4010 lface_hash (Lisp_Object *v)
4012 return (hash_string_case_insensitive (v[LFACE_FAMILY_INDEX])
4013 ^ hash_string_case_insensitive (v[LFACE_FOUNDRY_INDEX])
4014 ^ hash_string_case_insensitive (v[LFACE_FOREGROUND_INDEX])
4015 ^ hash_string_case_insensitive (v[LFACE_BACKGROUND_INDEX])
4016 ^ XHASH (v[LFACE_WEIGHT_INDEX])
4017 ^ XHASH (v[LFACE_SLANT_INDEX])
4018 ^ XHASH (v[LFACE_SWIDTH_INDEX])
4019 ^ XHASH (v[LFACE_HEIGHT_INDEX]));
4022 #ifdef HAVE_WINDOW_SYSTEM
4024 /* Return non-zero if LFACE1 and LFACE2 specify the same font (without
4025 considering charsets/registries). They do if they specify the same
4026 family, point size, weight, width, slant, and font. Both
4027 LFACE1 and LFACE2 must be fully-specified. */
4029 static int
4030 lface_same_font_attributes_p (Lisp_Object *lface1, Lisp_Object *lface2)
4032 eassert (lface_fully_specified_p (lface1)
4033 && lface_fully_specified_p (lface2));
4034 return (xstrcasecmp (SSDATA (lface1[LFACE_FAMILY_INDEX]),
4035 SSDATA (lface2[LFACE_FAMILY_INDEX])) == 0
4036 && xstrcasecmp (SSDATA (lface1[LFACE_FOUNDRY_INDEX]),
4037 SSDATA (lface2[LFACE_FOUNDRY_INDEX])) == 0
4038 && EQ (lface1[LFACE_HEIGHT_INDEX], lface2[LFACE_HEIGHT_INDEX])
4039 && EQ (lface1[LFACE_SWIDTH_INDEX], lface2[LFACE_SWIDTH_INDEX])
4040 && EQ (lface1[LFACE_WEIGHT_INDEX], lface2[LFACE_WEIGHT_INDEX])
4041 && EQ (lface1[LFACE_SLANT_INDEX], lface2[LFACE_SLANT_INDEX])
4042 && EQ (lface1[LFACE_FONT_INDEX], lface2[LFACE_FONT_INDEX])
4043 && (EQ (lface1[LFACE_FONTSET_INDEX], lface2[LFACE_FONTSET_INDEX])
4044 || (STRINGP (lface1[LFACE_FONTSET_INDEX])
4045 && STRINGP (lface2[LFACE_FONTSET_INDEX])
4046 && ! xstrcasecmp (SSDATA (lface1[LFACE_FONTSET_INDEX]),
4047 SSDATA (lface2[LFACE_FONTSET_INDEX]))))
4051 #endif /* HAVE_WINDOW_SYSTEM */
4053 /***********************************************************************
4054 Realized Faces
4055 ***********************************************************************/
4057 /* Allocate and return a new realized face for Lisp face attribute
4058 vector ATTR. */
4060 static struct face *
4061 make_realized_face (Lisp_Object *attr)
4063 enum { off = offsetof (struct face, id) };
4064 struct face *face = xmalloc (sizeof *face);
4066 memcpy (face->lface, attr, sizeof face->lface);
4067 memset (&face->id, 0, sizeof *face - off);
4068 face->ascii_face = face;
4070 return face;
4074 /* Free realized face FACE, including its X resources. FACE may
4075 be null. */
4077 static void
4078 free_realized_face (struct frame *f, struct face *face)
4080 if (face)
4082 #ifdef HAVE_WINDOW_SYSTEM
4083 if (FRAME_WINDOW_P (f))
4085 /* Free fontset of FACE if it is ASCII face. */
4086 if (face->fontset >= 0 && face == face->ascii_face)
4087 free_face_fontset (f, face);
4088 if (face->gc)
4090 block_input ();
4091 if (face->font)
4092 font_done_for_face (f, face);
4093 x_free_gc (f, face->gc);
4094 face->gc = 0;
4095 unblock_input ();
4097 #ifdef HAVE_X_WINDOWS
4098 free_face_colors (f, face);
4099 #endif /* HAVE_X_WINDOWS */
4100 x_destroy_bitmap (f, face->stipple);
4102 #endif /* HAVE_WINDOW_SYSTEM */
4104 xfree (face);
4109 /* Prepare face FACE for subsequent display on frame F. This
4110 allocated GCs if they haven't been allocated yet or have been freed
4111 by clearing the face cache. */
4113 void
4114 prepare_face_for_display (struct frame *f, struct face *face)
4116 #ifdef HAVE_WINDOW_SYSTEM
4117 eassert (FRAME_WINDOW_P (f));
4119 if (face->gc == 0)
4121 XGCValues xgcv;
4122 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
4124 xgcv.foreground = face->foreground;
4125 xgcv.background = face->background;
4126 #ifdef HAVE_X_WINDOWS
4127 xgcv.graphics_exposures = False;
4128 #endif
4130 block_input ();
4131 #ifdef HAVE_X_WINDOWS
4132 if (face->stipple)
4134 xgcv.fill_style = FillOpaqueStippled;
4135 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
4136 mask |= GCFillStyle | GCStipple;
4138 #endif
4139 face->gc = x_create_gc (f, mask, &xgcv);
4140 if (face->font)
4141 font_prepare_for_face (f, face);
4142 unblock_input ();
4144 #endif /* HAVE_WINDOW_SYSTEM */
4148 /* Returns the `distance' between the colors X and Y. */
4150 static int
4151 color_distance (XColor *x, XColor *y)
4153 /* This formula is from a paper titled `Colour metric' by Thiadmer Riemersma.
4154 Quoting from that paper:
4156 This formula has results that are very close to L*u*v* (with the
4157 modified lightness curve) and, more importantly, it is a more even
4158 algorithm: it does not have a range of colors where it suddenly
4159 gives far from optimal results.
4161 See <http://www.compuphase.com/cmetric.htm> for more info. */
4163 long r = (x->red - y->red) >> 8;
4164 long g = (x->green - y->green) >> 8;
4165 long b = (x->blue - y->blue) >> 8;
4166 long r_mean = (x->red + y->red) >> 9;
4168 return
4169 (((512 + r_mean) * r * r) >> 8)
4170 + 4 * g * g
4171 + (((767 - r_mean) * b * b) >> 8);
4175 DEFUN ("color-distance", Fcolor_distance, Scolor_distance, 2, 3, 0,
4176 doc: /* Return an integer distance between COLOR1 and COLOR2 on FRAME.
4177 COLOR1 and COLOR2 may be either strings containing the color name,
4178 or lists of the form (RED GREEN BLUE).
4179 If FRAME is unspecified or nil, the current frame is used. */)
4180 (Lisp_Object color1, Lisp_Object color2, Lisp_Object frame)
4182 struct frame *f = decode_live_frame (frame);
4183 XColor cdef1, cdef2;
4185 if (!(CONSP (color1) && parse_rgb_list (color1, &cdef1))
4186 && !(STRINGP (color1) && defined_color (f, SSDATA (color1), &cdef1, 0)))
4187 signal_error ("Invalid color", color1);
4188 if (!(CONSP (color2) && parse_rgb_list (color2, &cdef2))
4189 && !(STRINGP (color2) && defined_color (f, SSDATA (color2), &cdef2, 0)))
4190 signal_error ("Invalid color", color2);
4192 return make_number (color_distance (&cdef1, &cdef2));
4196 /***********************************************************************
4197 Face Cache
4198 ***********************************************************************/
4200 /* Return a new face cache for frame F. */
4202 static struct face_cache *
4203 make_face_cache (struct frame *f)
4205 struct face_cache *c = xmalloc (sizeof *c);
4207 c->buckets = xzalloc (FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets);
4208 c->size = 50;
4209 c->used = 0;
4210 c->faces_by_id = xmalloc (c->size * sizeof *c->faces_by_id);
4211 c->f = f;
4212 c->menu_face_changed_p = menu_face_changed_default;
4213 return c;
4216 #ifdef HAVE_WINDOW_SYSTEM
4218 /* Clear out all graphics contexts for all realized faces, except for
4219 the basic faces. This should be done from time to time just to avoid
4220 keeping too many graphics contexts that are no longer needed. */
4222 static void
4223 clear_face_gcs (struct face_cache *c)
4225 if (c && FRAME_WINDOW_P (c->f))
4227 int i;
4228 for (i = BASIC_FACE_ID_SENTINEL; i < c->used; ++i)
4230 struct face *face = c->faces_by_id[i];
4231 if (face && face->gc)
4233 block_input ();
4234 if (face->font)
4235 font_done_for_face (c->f, face);
4236 x_free_gc (c->f, face->gc);
4237 face->gc = 0;
4238 unblock_input ();
4244 #endif /* HAVE_WINDOW_SYSTEM */
4246 /* Free all realized faces in face cache C, including basic faces.
4247 C may be null. If faces are freed, make sure the frame's current
4248 matrix is marked invalid, so that a display caused by an expose
4249 event doesn't try to use faces we destroyed. */
4251 static void
4252 free_realized_faces (struct face_cache *c)
4254 if (c && c->used)
4256 int i, size;
4257 struct frame *f = c->f;
4259 /* We must block input here because we can't process X events
4260 safely while only some faces are freed, or when the frame's
4261 current matrix still references freed faces. */
4262 block_input ();
4264 for (i = 0; i < c->used; ++i)
4266 free_realized_face (f, c->faces_by_id[i]);
4267 c->faces_by_id[i] = NULL;
4270 c->used = 0;
4271 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4272 memset (c->buckets, 0, size);
4274 /* Must do a thorough redisplay the next time. Mark current
4275 matrices as invalid because they will reference faces freed
4276 above. This function is also called when a frame is
4277 destroyed. In this case, the root window of F is nil. */
4278 if (WINDOWP (f->root_window))
4280 clear_current_matrices (f);
4281 windows_or_buffers_changed = 58;
4284 unblock_input ();
4289 /* Free all realized faces on FRAME or on all frames if FRAME is nil.
4290 This is done after attributes of a named face have been changed,
4291 because we can't tell which realized faces depend on that face. */
4293 void
4294 free_all_realized_faces (Lisp_Object frame)
4296 if (NILP (frame))
4298 Lisp_Object rest;
4299 FOR_EACH_FRAME (rest, frame)
4300 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4302 else
4303 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4307 /* Free face cache C and faces in it, including their X resources. */
4309 static void
4310 free_face_cache (struct face_cache *c)
4312 if (c)
4314 free_realized_faces (c);
4315 xfree (c->buckets);
4316 xfree (c->faces_by_id);
4317 xfree (c);
4322 /* Cache realized face FACE in face cache C. HASH is the hash value
4323 of FACE. If FACE is for ASCII characters (i.e. FACE->ascii_face ==
4324 FACE), insert the new face to the beginning of the collision list
4325 of the face hash table of C. Otherwise, add the new face to the
4326 end of the collision list. This way, lookup_face can quickly find
4327 that a requested face is not cached. */
4329 static void
4330 cache_face (struct face_cache *c, struct face *face, unsigned int hash)
4332 int i = hash % FACE_CACHE_BUCKETS_SIZE;
4334 face->hash = hash;
4336 if (face->ascii_face != face)
4338 struct face *last = c->buckets[i];
4339 if (last)
4341 while (last->next)
4342 last = last->next;
4343 last->next = face;
4344 face->prev = last;
4345 face->next = NULL;
4347 else
4349 c->buckets[i] = face;
4350 face->prev = face->next = NULL;
4353 else
4355 face->prev = NULL;
4356 face->next = c->buckets[i];
4357 if (face->next)
4358 face->next->prev = face;
4359 c->buckets[i] = face;
4362 /* Find a free slot in C->faces_by_id and use the index of the free
4363 slot as FACE->id. */
4364 for (i = 0; i < c->used; ++i)
4365 if (c->faces_by_id[i] == NULL)
4366 break;
4367 face->id = i;
4369 #ifdef GLYPH_DEBUG
4370 /* Check that FACE got a unique id. */
4372 int j, n;
4373 struct face *face1;
4375 for (j = n = 0; j < FACE_CACHE_BUCKETS_SIZE; ++j)
4376 for (face1 = c->buckets[j]; face1; face1 = face1->next)
4377 if (face1->id == i)
4378 ++n;
4380 eassert (n == 1);
4382 #endif /* GLYPH_DEBUG */
4384 /* Maybe enlarge C->faces_by_id. */
4385 if (i == c->used)
4387 if (c->used == c->size)
4388 c->faces_by_id = xpalloc (c->faces_by_id, &c->size, 1, MAX_FACE_ID,
4389 sizeof *c->faces_by_id);
4390 c->used++;
4393 c->faces_by_id[i] = face;
4397 /* Remove face FACE from cache C. */
4399 static void
4400 uncache_face (struct face_cache *c, struct face *face)
4402 int i = face->hash % FACE_CACHE_BUCKETS_SIZE;
4404 if (face->prev)
4405 face->prev->next = face->next;
4406 else
4407 c->buckets[i] = face->next;
4409 if (face->next)
4410 face->next->prev = face->prev;
4412 c->faces_by_id[face->id] = NULL;
4413 if (face->id == c->used)
4414 --c->used;
4418 /* Look up a realized face with face attributes ATTR in the face cache
4419 of frame F. The face will be used to display ASCII characters.
4420 Value is the ID of the face found. If no suitable face is found,
4421 realize a new one. */
4423 static int
4424 lookup_face (struct frame *f, Lisp_Object *attr)
4426 struct face_cache *cache = FRAME_FACE_CACHE (f);
4427 unsigned hash;
4428 int i;
4429 struct face *face;
4431 eassert (cache != NULL);
4432 check_lface_attrs (attr);
4434 /* Look up ATTR in the face cache. */
4435 hash = lface_hash (attr);
4436 i = hash % FACE_CACHE_BUCKETS_SIZE;
4438 for (face = cache->buckets[i]; face; face = face->next)
4440 if (face->ascii_face != face)
4442 /* There's no more ASCII face. */
4443 face = NULL;
4444 break;
4446 if (face->hash == hash
4447 && lface_equal_p (face->lface, attr))
4448 break;
4451 /* If not found, realize a new face. */
4452 if (face == NULL)
4453 face = realize_face (cache, attr, -1);
4455 #ifdef GLYPH_DEBUG
4456 eassert (face == FACE_FROM_ID (f, face->id));
4457 #endif /* GLYPH_DEBUG */
4459 return face->id;
4462 #ifdef HAVE_WINDOW_SYSTEM
4463 /* Look up a realized face that has the same attributes as BASE_FACE
4464 except for the font in the face cache of frame F. If FONT-OBJECT
4465 is not nil, it is an already opened font. If FONT-OBJECT is nil,
4466 the face has no font. Value is the ID of the face found. If no
4467 suitable face is found, realize a new one. */
4470 face_for_font (struct frame *f, Lisp_Object font_object, struct face *base_face)
4472 struct face_cache *cache = FRAME_FACE_CACHE (f);
4473 unsigned hash;
4474 int i;
4475 struct face *face;
4477 eassert (cache != NULL);
4478 base_face = base_face->ascii_face;
4479 hash = lface_hash (base_face->lface);
4480 i = hash % FACE_CACHE_BUCKETS_SIZE;
4482 for (face = cache->buckets[i]; face; face = face->next)
4484 if (face->ascii_face == face)
4485 continue;
4486 if (face->ascii_face == base_face
4487 && face->font == (NILP (font_object) ? NULL
4488 : XFONT_OBJECT (font_object))
4489 && lface_equal_p (face->lface, base_face->lface))
4490 return face->id;
4493 /* If not found, realize a new face. */
4494 face = realize_non_ascii_face (f, font_object, base_face);
4495 return face->id;
4497 #endif /* HAVE_WINDOW_SYSTEM */
4499 /* Return the face id of the realized face for named face SYMBOL on
4500 frame F suitable for displaying ASCII characters. Value is -1 if
4501 the face couldn't be determined, which might happen if the default
4502 face isn't realized and cannot be realized. */
4505 lookup_named_face (struct frame *f, Lisp_Object symbol, int signal_p)
4507 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4508 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4509 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4511 if (default_face == NULL)
4513 if (!realize_basic_faces (f))
4514 return -1;
4515 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4516 if (default_face == NULL)
4517 emacs_abort (); /* realize_basic_faces must have set it up */
4520 if (! get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4521 return -1;
4523 memcpy (attrs, default_face->lface, sizeof attrs);
4524 merge_face_vectors (f, symbol_attrs, attrs, 0);
4526 return lookup_face (f, attrs);
4530 /* Return the display face-id of the basic face whose canonical face-id
4531 is FACE_ID. The return value will usually simply be FACE_ID, unless that
4532 basic face has bee remapped via Vface_remapping_alist. This function is
4533 conservative: if something goes wrong, it will simply return FACE_ID
4534 rather than signal an error. */
4537 lookup_basic_face (struct frame *f, int face_id)
4539 Lisp_Object name, mapping;
4540 int remapped_face_id;
4542 if (NILP (Vface_remapping_alist))
4543 return face_id; /* Nothing to do. */
4545 switch (face_id)
4547 case DEFAULT_FACE_ID: name = Qdefault; break;
4548 case MODE_LINE_FACE_ID: name = Qmode_line; break;
4549 case MODE_LINE_INACTIVE_FACE_ID: name = Qmode_line_inactive; break;
4550 case HEADER_LINE_FACE_ID: name = Qheader_line; break;
4551 case TOOL_BAR_FACE_ID: name = Qtool_bar; break;
4552 case FRINGE_FACE_ID: name = Qfringe; break;
4553 case SCROLL_BAR_FACE_ID: name = Qscroll_bar; break;
4554 case BORDER_FACE_ID: name = Qborder; break;
4555 case CURSOR_FACE_ID: name = Qcursor; break;
4556 case MOUSE_FACE_ID: name = Qmouse; break;
4557 case MENU_FACE_ID: name = Qmenu; break;
4559 default:
4560 emacs_abort (); /* the caller is supposed to pass us a basic face id */
4563 /* Do a quick scan through Vface_remapping_alist, and return immediately
4564 if there is no remapping for face NAME. This is just an optimization
4565 for the very common no-remapping case. */
4566 mapping = assq_no_quit (name, Vface_remapping_alist);
4567 if (NILP (mapping))
4568 return face_id; /* Give up. */
4570 /* If there is a remapping entry, lookup the face using NAME, which will
4571 handle the remapping too. */
4572 remapped_face_id = lookup_named_face (f, name, 0);
4573 if (remapped_face_id < 0)
4574 return face_id; /* Give up. */
4576 return remapped_face_id;
4580 /* Return a face for charset ASCII that is like the face with id
4581 FACE_ID on frame F, but has a font that is STEPS steps smaller.
4582 STEPS < 0 means larger. Value is the id of the face. */
4585 smaller_face (struct frame *f, int face_id, int steps)
4587 #ifdef HAVE_WINDOW_SYSTEM
4588 struct face *face;
4589 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4590 int pt, last_pt, last_height;
4591 int delta;
4592 int new_face_id;
4593 struct face *new_face;
4595 /* If not called for an X frame, just return the original face. */
4596 if (FRAME_TERMCAP_P (f))
4597 return face_id;
4599 /* Try in increments of 1/2 pt. */
4600 delta = steps < 0 ? 5 : -5;
4601 steps = eabs (steps);
4603 face = FACE_FROM_ID (f, face_id);
4604 memcpy (attrs, face->lface, sizeof attrs);
4605 pt = last_pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
4606 new_face_id = face_id;
4607 last_height = FONT_HEIGHT (face->font);
4609 while (steps
4610 && pt + delta > 0
4611 /* Give up if we cannot find a font within 10pt. */
4612 && eabs (last_pt - pt) < 100)
4614 /* Look up a face for a slightly smaller/larger font. */
4615 pt += delta;
4616 attrs[LFACE_HEIGHT_INDEX] = make_number (pt);
4617 new_face_id = lookup_face (f, attrs);
4618 new_face = FACE_FROM_ID (f, new_face_id);
4620 /* If height changes, count that as one step. */
4621 if ((delta < 0 && FONT_HEIGHT (new_face->font) < last_height)
4622 || (delta > 0 && FONT_HEIGHT (new_face->font) > last_height))
4624 --steps;
4625 last_height = FONT_HEIGHT (new_face->font);
4626 last_pt = pt;
4630 return new_face_id;
4632 #else /* not HAVE_WINDOW_SYSTEM */
4634 return face_id;
4636 #endif /* not HAVE_WINDOW_SYSTEM */
4640 /* Return a face for charset ASCII that is like the face with id
4641 FACE_ID on frame F, but has height HEIGHT. */
4644 face_with_height (struct frame *f, int face_id, int height)
4646 #ifdef HAVE_WINDOW_SYSTEM
4647 struct face *face;
4648 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4650 if (FRAME_TERMCAP_P (f)
4651 || height <= 0)
4652 return face_id;
4654 face = FACE_FROM_ID (f, face_id);
4655 memcpy (attrs, face->lface, sizeof attrs);
4656 attrs[LFACE_HEIGHT_INDEX] = make_number (height);
4657 font_clear_prop (attrs, FONT_SIZE_INDEX);
4658 face_id = lookup_face (f, attrs);
4659 #endif /* HAVE_WINDOW_SYSTEM */
4661 return face_id;
4665 /* Return the face id of the realized face for named face SYMBOL on
4666 frame F suitable for displaying ASCII characters, and use
4667 attributes of the face FACE_ID for attributes that aren't
4668 completely specified by SYMBOL. This is like lookup_named_face,
4669 except that the default attributes come from FACE_ID, not from the
4670 default face. FACE_ID is assumed to be already realized. */
4673 lookup_derived_face (struct frame *f, Lisp_Object symbol, int face_id,
4674 int signal_p)
4676 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4677 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4678 struct face *default_face = FACE_FROM_ID (f, face_id);
4680 if (!default_face)
4681 emacs_abort ();
4683 if (!get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4684 return -1;
4686 memcpy (attrs, default_face->lface, sizeof attrs);
4687 merge_face_vectors (f, symbol_attrs, attrs, 0);
4688 return lookup_face (f, attrs);
4691 DEFUN ("face-attributes-as-vector", Fface_attributes_as_vector,
4692 Sface_attributes_as_vector, 1, 1, 0,
4693 doc: /* Return a vector of face attributes corresponding to PLIST. */)
4694 (Lisp_Object plist)
4696 Lisp_Object lface;
4697 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
4698 Qunspecified);
4699 merge_face_ref (XFRAME (selected_frame), plist, XVECTOR (lface)->contents,
4700 1, 0);
4701 return lface;
4706 /***********************************************************************
4707 Face capability testing
4708 ***********************************************************************/
4711 /* If the distance (as returned by color_distance) between two colors is
4712 less than this, then they are considered the same, for determining
4713 whether a color is supported or not. The range of values is 0-65535. */
4715 #define TTY_SAME_COLOR_THRESHOLD 10000
4717 #ifdef HAVE_WINDOW_SYSTEM
4719 /* Return true if all the face attributes in ATTRS are supported
4720 on the window-system frame F.
4722 The definition of `supported' is somewhat heuristic, but basically means
4723 that a face containing all the attributes in ATTRS, when merged with the
4724 default face for display, can be represented in a way that's
4726 \(1) different in appearance than the default face, and
4727 \(2) `close in spirit' to what the attributes specify, if not exact. */
4729 static bool
4730 x_supports_face_attributes_p (struct frame *f,
4731 Lisp_Object attrs[LFACE_VECTOR_SIZE],
4732 struct face *def_face)
4734 Lisp_Object *def_attrs = def_face->lface;
4736 /* Check that other specified attributes are different that the default
4737 face. */
4738 if ((!UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
4739 && face_attr_equal_p (attrs[LFACE_UNDERLINE_INDEX],
4740 def_attrs[LFACE_UNDERLINE_INDEX]))
4741 || (!UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
4742 && face_attr_equal_p (attrs[LFACE_INVERSE_INDEX],
4743 def_attrs[LFACE_INVERSE_INDEX]))
4744 || (!UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
4745 && face_attr_equal_p (attrs[LFACE_FOREGROUND_INDEX],
4746 def_attrs[LFACE_FOREGROUND_INDEX]))
4747 || (!UNSPECIFIEDP (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
4748 && face_attr_equal_p (attrs[LFACE_DISTANT_FOREGROUND_INDEX],
4749 def_attrs[LFACE_DISTANT_FOREGROUND_INDEX]))
4750 || (!UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
4751 && face_attr_equal_p (attrs[LFACE_BACKGROUND_INDEX],
4752 def_attrs[LFACE_BACKGROUND_INDEX]))
4753 || (!UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4754 && face_attr_equal_p (attrs[LFACE_STIPPLE_INDEX],
4755 def_attrs[LFACE_STIPPLE_INDEX]))
4756 || (!UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4757 && face_attr_equal_p (attrs[LFACE_OVERLINE_INDEX],
4758 def_attrs[LFACE_OVERLINE_INDEX]))
4759 || (!UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4760 && face_attr_equal_p (attrs[LFACE_STRIKE_THROUGH_INDEX],
4761 def_attrs[LFACE_STRIKE_THROUGH_INDEX]))
4762 || (!UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
4763 && face_attr_equal_p (attrs[LFACE_BOX_INDEX],
4764 def_attrs[LFACE_BOX_INDEX])))
4765 return 0;
4767 /* Check font-related attributes, as those are the most commonly
4768 "unsupported" on a window-system (because of missing fonts). */
4769 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4770 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4771 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4772 || !UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
4773 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
4774 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX]))
4776 int face_id;
4777 struct face *face;
4778 Lisp_Object merged_attrs[LFACE_VECTOR_SIZE];
4779 int i;
4781 memcpy (merged_attrs, def_attrs, sizeof merged_attrs);
4783 merge_face_vectors (f, attrs, merged_attrs, 0);
4785 face_id = lookup_face (f, merged_attrs);
4786 face = FACE_FROM_ID (f, face_id);
4788 if (! face)
4789 error ("Cannot make face");
4791 /* If the font is the same, or no font is found, then not
4792 supported. */
4793 if (face->font == def_face->font
4794 || ! face->font)
4795 return 0;
4796 for (i = FONT_TYPE_INDEX; i <= FONT_SIZE_INDEX; i++)
4797 if (! EQ (face->font->props[i], def_face->font->props[i]))
4799 Lisp_Object s1, s2;
4801 if (i < FONT_FOUNDRY_INDEX || i > FONT_REGISTRY_INDEX
4802 || face->font->driver->case_sensitive)
4803 return 1;
4804 s1 = SYMBOL_NAME (face->font->props[i]);
4805 s2 = SYMBOL_NAME (def_face->font->props[i]);
4806 if (! EQ (Fcompare_strings (s1, make_number (0), Qnil,
4807 s2, make_number (0), Qnil, Qt), Qt))
4808 return 1;
4810 return 0;
4813 /* Everything checks out, this face is supported. */
4814 return 1;
4817 #endif /* HAVE_WINDOW_SYSTEM */
4819 /* Return true if all the face attributes in ATTRS are supported
4820 on the tty frame F.
4822 The definition of `supported' is somewhat heuristic, but basically means
4823 that a face containing all the attributes in ATTRS, when merged
4824 with the default face for display, can be represented in a way that's
4826 \(1) different in appearance than the default face, and
4827 \(2) `close in spirit' to what the attributes specify, if not exact.
4829 Point (2) implies that a `:weight black' attribute will be satisfied
4830 by any terminal that can display bold, and a `:foreground "yellow"' as
4831 long as the terminal can display a yellowish color, but `:slant italic'
4832 will _not_ be satisfied by the tty display code's automatic
4833 substitution of a `dim' face for italic. */
4835 static bool
4836 tty_supports_face_attributes_p (struct frame *f,
4837 Lisp_Object attrs[LFACE_VECTOR_SIZE],
4838 struct face *def_face)
4840 int weight, slant;
4841 Lisp_Object val, fg, bg;
4842 XColor fg_tty_color, fg_std_color;
4843 XColor bg_tty_color, bg_std_color;
4844 unsigned test_caps = 0;
4845 Lisp_Object *def_attrs = def_face->lface;
4847 /* First check some easy-to-check stuff; ttys support none of the
4848 following attributes, so we can just return false if any are requested
4849 (even if `nominal' values are specified, we should still return false,
4850 as that will be the same value that the default face uses). We
4851 consider :slant unsupportable on ttys, even though the face code
4852 actually `fakes' them using a dim attribute if possible. This is
4853 because the faked result is too different from what the face
4854 specifies. */
4855 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4856 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4857 || !UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4858 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4859 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
4860 || !UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4861 || !UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4862 || !UNSPECIFIEDP (attrs[LFACE_BOX_INDEX]))
4863 return 0;
4865 /* Test for terminal `capabilities' (non-color character attributes). */
4867 /* font weight (bold/dim) */
4868 val = attrs[LFACE_WEIGHT_INDEX];
4869 if (!UNSPECIFIEDP (val)
4870 && (weight = FONT_WEIGHT_NAME_NUMERIC (val), weight >= 0))
4872 int def_weight = FONT_WEIGHT_NAME_NUMERIC (def_attrs[LFACE_WEIGHT_INDEX]);
4874 if (weight > 100)
4876 if (def_weight > 100)
4877 return 0; /* same as default */
4878 test_caps = TTY_CAP_BOLD;
4880 else if (weight < 100)
4882 if (def_weight < 100)
4883 return 0; /* same as default */
4884 test_caps = TTY_CAP_DIM;
4886 else if (def_weight == 100)
4887 return 0; /* same as default */
4890 /* font slant */
4891 val = attrs[LFACE_SLANT_INDEX];
4892 if (!UNSPECIFIEDP (val)
4893 && (slant = FONT_SLANT_NAME_NUMERIC (val), slant >= 0))
4895 int def_slant = FONT_SLANT_NAME_NUMERIC (def_attrs[LFACE_SLANT_INDEX]);
4896 if (slant == 100 || slant == def_slant)
4897 return 0; /* same as default */
4898 else
4899 test_caps |= TTY_CAP_ITALIC;
4902 /* underlining */
4903 val = attrs[LFACE_UNDERLINE_INDEX];
4904 if (!UNSPECIFIEDP (val))
4906 if (STRINGP (val))
4907 return 0; /* ttys can't use colored underlines */
4908 else if (EQ (CAR_SAFE (val), QCstyle) && EQ (CAR_SAFE (CDR_SAFE (val)), Qwave))
4909 return 0; /* ttys can't use wave underlines */
4910 else if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
4911 return 0; /* same as default */
4912 else
4913 test_caps |= TTY_CAP_UNDERLINE;
4916 /* inverse video */
4917 val = attrs[LFACE_INVERSE_INDEX];
4918 if (!UNSPECIFIEDP (val))
4920 if (face_attr_equal_p (val, def_attrs[LFACE_INVERSE_INDEX]))
4921 return 0; /* same as default */
4922 else
4923 test_caps |= TTY_CAP_INVERSE;
4927 /* Color testing. */
4929 /* Check if foreground color is close enough. */
4930 fg = attrs[LFACE_FOREGROUND_INDEX];
4931 if (STRINGP (fg))
4933 Lisp_Object def_fg = def_attrs[LFACE_FOREGROUND_INDEX];
4935 if (face_attr_equal_p (fg, def_fg))
4936 return 0; /* same as default */
4937 else if (! tty_lookup_color (f, fg, &fg_tty_color, &fg_std_color))
4938 return 0; /* not a valid color */
4939 else if (color_distance (&fg_tty_color, &fg_std_color)
4940 > TTY_SAME_COLOR_THRESHOLD)
4941 return 0; /* displayed color is too different */
4942 else
4943 /* Make sure the color is really different than the default. */
4945 XColor def_fg_color;
4946 if (tty_lookup_color (f, def_fg, &def_fg_color, 0)
4947 && (color_distance (&fg_tty_color, &def_fg_color)
4948 <= TTY_SAME_COLOR_THRESHOLD))
4949 return 0;
4953 /* Check if background color is close enough. */
4954 bg = attrs[LFACE_BACKGROUND_INDEX];
4955 if (STRINGP (bg))
4957 Lisp_Object def_bg = def_attrs[LFACE_BACKGROUND_INDEX];
4959 if (face_attr_equal_p (bg, def_bg))
4960 return 0; /* same as default */
4961 else if (! tty_lookup_color (f, bg, &bg_tty_color, &bg_std_color))
4962 return 0; /* not a valid color */
4963 else if (color_distance (&bg_tty_color, &bg_std_color)
4964 > TTY_SAME_COLOR_THRESHOLD)
4965 return 0; /* displayed color is too different */
4966 else
4967 /* Make sure the color is really different than the default. */
4969 XColor def_bg_color;
4970 if (tty_lookup_color (f, def_bg, &def_bg_color, 0)
4971 && (color_distance (&bg_tty_color, &def_bg_color)
4972 <= TTY_SAME_COLOR_THRESHOLD))
4973 return 0;
4977 /* If both foreground and background are requested, see if the
4978 distance between them is OK. We just check to see if the distance
4979 between the tty's foreground and background is close enough to the
4980 distance between the standard foreground and background. */
4981 if (STRINGP (fg) && STRINGP (bg))
4983 int delta_delta
4984 = (color_distance (&fg_std_color, &bg_std_color)
4985 - color_distance (&fg_tty_color, &bg_tty_color));
4986 if (delta_delta > TTY_SAME_COLOR_THRESHOLD
4987 || delta_delta < -TTY_SAME_COLOR_THRESHOLD)
4988 return 0;
4992 /* See if the capabilities we selected above are supported, with the
4993 given colors. */
4994 return tty_capable_p (FRAME_TTY (f), test_caps);
4998 DEFUN ("display-supports-face-attributes-p",
4999 Fdisplay_supports_face_attributes_p, Sdisplay_supports_face_attributes_p,
5000 1, 2, 0,
5001 doc: /* Return non-nil if all the face attributes in ATTRIBUTES are supported.
5002 The optional argument DISPLAY can be a display name, a frame, or
5003 nil (meaning the selected frame's display).
5005 The definition of `supported' is somewhat heuristic, but basically means
5006 that a face containing all the attributes in ATTRIBUTES, when merged
5007 with the default face for display, can be represented in a way that's
5009 \(1) different in appearance than the default face, and
5010 \(2) `close in spirit' to what the attributes specify, if not exact.
5012 Point (2) implies that a `:weight black' attribute will be satisfied by
5013 any display that can display bold, and a `:foreground \"yellow\"' as long
5014 as it can display a yellowish color, but `:slant italic' will _not_ be
5015 satisfied by the tty display code's automatic substitution of a `dim'
5016 face for italic. */)
5017 (Lisp_Object attributes, Lisp_Object display)
5019 bool supports = 0;
5020 int i;
5021 Lisp_Object frame;
5022 struct frame *f;
5023 struct face *def_face;
5024 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5026 if (noninteractive || !initialized)
5027 /* We may not be able to access low-level face information in batch
5028 mode, or before being dumped, and this function is not going to
5029 be very useful in those cases anyway, so just give up. */
5030 return Qnil;
5032 if (NILP (display))
5033 frame = selected_frame;
5034 else if (FRAMEP (display))
5035 frame = display;
5036 else
5038 /* Find any frame on DISPLAY. */
5039 Lisp_Object tail;
5041 frame = Qnil;
5042 FOR_EACH_FRAME (tail, frame)
5043 if (!NILP (Fequal (Fcdr (Fassq (Qdisplay,
5044 XFRAME (frame)->param_alist)),
5045 display)))
5046 break;
5049 CHECK_LIVE_FRAME (frame);
5050 f = XFRAME (frame);
5052 for (i = 0; i < LFACE_VECTOR_SIZE; i++)
5053 attrs[i] = Qunspecified;
5054 merge_face_ref (f, attributes, attrs, 1, 0);
5056 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5057 if (def_face == NULL)
5059 if (! realize_basic_faces (f))
5060 error ("Cannot realize default face");
5061 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5062 if (def_face == NULL)
5063 emacs_abort (); /* realize_basic_faces must have set it up */
5066 /* Dispatch to the appropriate handler. */
5067 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5068 supports = tty_supports_face_attributes_p (f, attrs, def_face);
5069 #ifdef HAVE_WINDOW_SYSTEM
5070 else
5071 supports = x_supports_face_attributes_p (f, attrs, def_face);
5072 #endif
5074 return supports ? Qt : Qnil;
5078 /***********************************************************************
5079 Font selection
5080 ***********************************************************************/
5082 DEFUN ("internal-set-font-selection-order",
5083 Finternal_set_font_selection_order,
5084 Sinternal_set_font_selection_order, 1, 1, 0,
5085 doc: /* Set font selection order for face font selection to ORDER.
5086 ORDER must be a list of length 4 containing the symbols `:width',
5087 `:height', `:weight', and `:slant'. Face attributes appearing
5088 first in ORDER are matched first, e.g. if `:height' appears before
5089 `:weight' in ORDER, font selection first tries to find a font with
5090 a suitable height, and then tries to match the font weight.
5091 Value is ORDER. */)
5092 (Lisp_Object order)
5094 Lisp_Object list;
5095 int i;
5096 int indices[DIM (font_sort_order)];
5098 CHECK_LIST (order);
5099 memset (indices, 0, sizeof indices);
5100 i = 0;
5102 for (list = order;
5103 CONSP (list) && i < DIM (indices);
5104 list = XCDR (list), ++i)
5106 Lisp_Object attr = XCAR (list);
5107 int xlfd;
5109 if (EQ (attr, QCwidth))
5110 xlfd = XLFD_SWIDTH;
5111 else if (EQ (attr, QCheight))
5112 xlfd = XLFD_POINT_SIZE;
5113 else if (EQ (attr, QCweight))
5114 xlfd = XLFD_WEIGHT;
5115 else if (EQ (attr, QCslant))
5116 xlfd = XLFD_SLANT;
5117 else
5118 break;
5120 if (indices[i] != 0)
5121 break;
5122 indices[i] = xlfd;
5125 if (!NILP (list) || i != DIM (indices))
5126 signal_error ("Invalid font sort order", order);
5127 for (i = 0; i < DIM (font_sort_order); ++i)
5128 if (indices[i] == 0)
5129 signal_error ("Invalid font sort order", order);
5131 if (memcmp (indices, font_sort_order, sizeof indices) != 0)
5133 memcpy (font_sort_order, indices, sizeof font_sort_order);
5134 free_all_realized_faces (Qnil);
5137 font_update_sort_order (font_sort_order);
5139 return Qnil;
5143 DEFUN ("internal-set-alternative-font-family-alist",
5144 Finternal_set_alternative_font_family_alist,
5145 Sinternal_set_alternative_font_family_alist, 1, 1, 0,
5146 doc: /* Define alternative font families to try in face font selection.
5147 ALIST is an alist of (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5148 Each ALTERNATIVE is tried in order if no fonts of font family FAMILY can
5149 be found. Value is ALIST. */)
5150 (Lisp_Object alist)
5152 Lisp_Object entry, tail, tail2;
5154 CHECK_LIST (alist);
5155 alist = Fcopy_sequence (alist);
5156 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5158 entry = XCAR (tail);
5159 CHECK_LIST (entry);
5160 entry = Fcopy_sequence (entry);
5161 XSETCAR (tail, entry);
5162 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5163 XSETCAR (tail2, Fintern (XCAR (tail2), Qnil));
5166 Vface_alternative_font_family_alist = alist;
5167 free_all_realized_faces (Qnil);
5168 return alist;
5172 DEFUN ("internal-set-alternative-font-registry-alist",
5173 Finternal_set_alternative_font_registry_alist,
5174 Sinternal_set_alternative_font_registry_alist, 1, 1, 0,
5175 doc: /* Define alternative font registries to try in face font selection.
5176 ALIST is an alist of (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5177 Each ALTERNATIVE is tried in order if no fonts of font registry REGISTRY can
5178 be found. Value is ALIST. */)
5179 (Lisp_Object alist)
5181 Lisp_Object entry, tail, tail2;
5183 CHECK_LIST (alist);
5184 alist = Fcopy_sequence (alist);
5185 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5187 entry = XCAR (tail);
5188 CHECK_LIST (entry);
5189 entry = Fcopy_sequence (entry);
5190 XSETCAR (tail, entry);
5191 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5192 XSETCAR (tail2, Fdowncase (XCAR (tail2)));
5194 Vface_alternative_font_registry_alist = alist;
5195 free_all_realized_faces (Qnil);
5196 return alist;
5200 #ifdef HAVE_WINDOW_SYSTEM
5202 /* Return the fontset id of the base fontset name or alias name given
5203 by the fontset attribute of ATTRS. Value is -1 if the fontset
5204 attribute of ATTRS doesn't name a fontset. */
5206 static int
5207 face_fontset (Lisp_Object attrs[LFACE_VECTOR_SIZE])
5209 Lisp_Object name;
5211 name = attrs[LFACE_FONTSET_INDEX];
5212 if (!STRINGP (name))
5213 return -1;
5214 return fs_query_fontset (name, 0);
5217 #endif /* HAVE_WINDOW_SYSTEM */
5221 /***********************************************************************
5222 Face Realization
5223 ***********************************************************************/
5225 /* Realize basic faces on frame F. Value is zero if frame parameters
5226 of F don't contain enough information needed to realize the default
5227 face. */
5229 static bool
5230 realize_basic_faces (struct frame *f)
5232 bool success_p = 0;
5233 ptrdiff_t count = SPECPDL_INDEX ();
5235 /* Block input here so that we won't be surprised by an X expose
5236 event, for instance, without having the faces set up. */
5237 block_input ();
5238 specbind (Qscalable_fonts_allowed, Qt);
5240 if (realize_default_face (f))
5242 realize_named_face (f, Qmode_line, MODE_LINE_FACE_ID);
5243 realize_named_face (f, Qmode_line_inactive, MODE_LINE_INACTIVE_FACE_ID);
5244 realize_named_face (f, Qtool_bar, TOOL_BAR_FACE_ID);
5245 realize_named_face (f, Qfringe, FRINGE_FACE_ID);
5246 realize_named_face (f, Qheader_line, HEADER_LINE_FACE_ID);
5247 realize_named_face (f, Qscroll_bar, SCROLL_BAR_FACE_ID);
5248 realize_named_face (f, Qborder, BORDER_FACE_ID);
5249 realize_named_face (f, Qcursor, CURSOR_FACE_ID);
5250 realize_named_face (f, Qmouse, MOUSE_FACE_ID);
5251 realize_named_face (f, Qmenu, MENU_FACE_ID);
5252 realize_named_face (f, Qvertical_border, VERTICAL_BORDER_FACE_ID);
5253 realize_named_face (f, Qwindow_divider, WINDOW_DIVIDER_FACE_ID);
5254 realize_named_face (f, Qwindow_divider_first_pixel,
5255 WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
5256 realize_named_face (f, Qwindow_divider_last_pixel,
5257 WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
5259 /* Reflect changes in the `menu' face in menu bars. */
5260 if (FRAME_FACE_CACHE (f)->menu_face_changed_p)
5262 FRAME_FACE_CACHE (f)->menu_face_changed_p = 0;
5263 #ifdef USE_X_TOOLKIT
5264 if (FRAME_WINDOW_P (f))
5265 x_update_menu_appearance (f);
5266 #endif
5269 success_p = 1;
5272 unbind_to (count, Qnil);
5273 unblock_input ();
5274 return success_p;
5278 /* Realize the default face on frame F. If the face is not fully
5279 specified, make it fully-specified. Attributes of the default face
5280 that are not explicitly specified are taken from frame parameters. */
5282 static bool
5283 realize_default_face (struct frame *f)
5285 struct face_cache *c = FRAME_FACE_CACHE (f);
5286 Lisp_Object lface;
5287 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5288 struct face *face;
5290 /* If the `default' face is not yet known, create it. */
5291 lface = lface_from_face_name (f, Qdefault, 0);
5292 if (NILP (lface))
5294 Lisp_Object frame;
5295 XSETFRAME (frame, f);
5296 lface = Finternal_make_lisp_face (Qdefault, frame);
5299 #ifdef HAVE_WINDOW_SYSTEM
5300 if (FRAME_WINDOW_P (f))
5302 Lisp_Object font_object;
5304 XSETFONT (font_object, FRAME_FONT (f));
5305 set_lface_from_font (f, lface, font_object, f->default_face_done_p);
5306 ASET (lface, LFACE_FONTSET_INDEX, fontset_name (FRAME_FONTSET (f)));
5307 f->default_face_done_p = 1;
5309 #endif /* HAVE_WINDOW_SYSTEM */
5311 if (!FRAME_WINDOW_P (f))
5313 ASET (lface, LFACE_FAMILY_INDEX, build_string ("default"));
5314 ASET (lface, LFACE_FOUNDRY_INDEX, LFACE_FAMILY (lface));
5315 ASET (lface, LFACE_SWIDTH_INDEX, Qnormal);
5316 ASET (lface, LFACE_HEIGHT_INDEX, make_number (1));
5317 if (UNSPECIFIEDP (LFACE_WEIGHT (lface)))
5318 ASET (lface, LFACE_WEIGHT_INDEX, Qnormal);
5319 if (UNSPECIFIEDP (LFACE_SLANT (lface)))
5320 ASET (lface, LFACE_SLANT_INDEX, Qnormal);
5321 if (UNSPECIFIEDP (LFACE_FONTSET (lface)))
5322 ASET (lface, LFACE_FONTSET_INDEX, Qnil);
5325 if (UNSPECIFIEDP (LFACE_UNDERLINE (lface)))
5326 ASET (lface, LFACE_UNDERLINE_INDEX, Qnil);
5328 if (UNSPECIFIEDP (LFACE_OVERLINE (lface)))
5329 ASET (lface, LFACE_OVERLINE_INDEX, Qnil);
5331 if (UNSPECIFIEDP (LFACE_STRIKE_THROUGH (lface)))
5332 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, Qnil);
5334 if (UNSPECIFIEDP (LFACE_BOX (lface)))
5335 ASET (lface, LFACE_BOX_INDEX, Qnil);
5337 if (UNSPECIFIEDP (LFACE_INVERSE (lface)))
5338 ASET (lface, LFACE_INVERSE_INDEX, Qnil);
5340 if (UNSPECIFIEDP (LFACE_FOREGROUND (lface)))
5342 /* This function is called so early that colors are not yet
5343 set in the frame parameter list. */
5344 Lisp_Object color = Fassq (Qforeground_color, f->param_alist);
5346 if (CONSP (color) && STRINGP (XCDR (color)))
5347 ASET (lface, LFACE_FOREGROUND_INDEX, XCDR (color));
5348 else if (FRAME_WINDOW_P (f))
5349 return 0;
5350 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5351 ASET (lface, LFACE_FOREGROUND_INDEX, build_string (unspecified_fg));
5352 else
5353 emacs_abort ();
5356 if (UNSPECIFIEDP (LFACE_BACKGROUND (lface)))
5358 /* This function is called so early that colors are not yet
5359 set in the frame parameter list. */
5360 Lisp_Object color = Fassq (Qbackground_color, f->param_alist);
5361 if (CONSP (color) && STRINGP (XCDR (color)))
5362 ASET (lface, LFACE_BACKGROUND_INDEX, XCDR (color));
5363 else if (FRAME_WINDOW_P (f))
5364 return 0;
5365 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5366 ASET (lface, LFACE_BACKGROUND_INDEX, build_string (unspecified_bg));
5367 else
5368 emacs_abort ();
5371 if (UNSPECIFIEDP (LFACE_STIPPLE (lface)))
5372 ASET (lface, LFACE_STIPPLE_INDEX, Qnil);
5374 /* Realize the face; it must be fully-specified now. */
5375 eassert (lface_fully_specified_p (XVECTOR (lface)->contents));
5376 check_lface (lface);
5377 memcpy (attrs, XVECTOR (lface)->contents, sizeof attrs);
5378 face = realize_face (c, attrs, DEFAULT_FACE_ID);
5380 #ifdef HAVE_WINDOW_SYSTEM
5381 #ifdef HAVE_X_WINDOWS
5382 if (FRAME_X_P (f) && face->font != FRAME_FONT (f))
5384 /* This can happen when making a frame on a display that does
5385 not support the default font. */
5386 if (!face->font)
5387 return 0;
5389 /* Otherwise, the font specified for the frame was not
5390 acceptable as a font for the default face (perhaps because
5391 auto-scaled fonts are rejected), so we must adjust the frame
5392 font. */
5393 x_set_font (f, LFACE_FONT (lface), Qnil);
5395 #endif /* HAVE_X_WINDOWS */
5396 #endif /* HAVE_WINDOW_SYSTEM */
5397 return 1;
5401 /* Realize basic faces other than the default face in face cache C.
5402 SYMBOL is the face name, ID is the face id the realized face must
5403 have. The default face must have been realized already. */
5405 static void
5406 realize_named_face (struct frame *f, Lisp_Object symbol, int id)
5408 struct face_cache *c = FRAME_FACE_CACHE (f);
5409 Lisp_Object lface = lface_from_face_name (f, symbol, 0);
5410 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5411 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5413 /* The default face must exist and be fully specified. */
5414 get_lface_attributes_no_remap (f, Qdefault, attrs, 1);
5415 check_lface_attrs (attrs);
5416 eassert (lface_fully_specified_p (attrs));
5418 /* If SYMBOL isn't know as a face, create it. */
5419 if (NILP (lface))
5421 Lisp_Object frame;
5422 XSETFRAME (frame, f);
5423 lface = Finternal_make_lisp_face (symbol, frame);
5426 /* Merge SYMBOL's face with the default face. */
5427 get_lface_attributes_no_remap (f, symbol, symbol_attrs, 1);
5428 merge_face_vectors (f, symbol_attrs, attrs, 0);
5430 /* Realize the face. */
5431 realize_face (c, attrs, id);
5435 /* Realize the fully-specified face with attributes ATTRS in face
5436 cache CACHE for ASCII characters. If FORMER_FACE_ID is
5437 non-negative, it is an ID of face to remove before caching the new
5438 face. Value is a pointer to the newly created realized face. */
5440 static struct face *
5441 realize_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE],
5442 int former_face_id)
5444 struct face *face;
5446 /* LFACE must be fully specified. */
5447 eassert (cache != NULL);
5448 check_lface_attrs (attrs);
5450 if (former_face_id >= 0 && cache->used > former_face_id)
5452 /* Remove the former face. */
5453 struct face *former_face = cache->faces_by_id[former_face_id];
5454 uncache_face (cache, former_face);
5455 free_realized_face (cache->f, former_face);
5456 SET_FRAME_GARBAGED (cache->f);
5459 if (FRAME_WINDOW_P (cache->f))
5460 face = realize_x_face (cache, attrs);
5461 else if (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f))
5462 face = realize_tty_face (cache, attrs);
5463 else if (FRAME_INITIAL_P (cache->f))
5465 /* Create a dummy face. */
5466 face = make_realized_face (attrs);
5468 else
5469 emacs_abort ();
5471 /* Insert the new face. */
5472 cache_face (cache, face, lface_hash (attrs));
5473 return face;
5477 #ifdef HAVE_WINDOW_SYSTEM
5478 /* Realize the fully-specified face that uses FONT-OBJECT and has the
5479 same attributes as BASE_FACE except for the font on frame F.
5480 FONT-OBJECT may be nil, in which case, realized a face of
5481 no-font. */
5483 static struct face *
5484 realize_non_ascii_face (struct frame *f, Lisp_Object font_object,
5485 struct face *base_face)
5487 struct face_cache *cache = FRAME_FACE_CACHE (f);
5488 struct face *face;
5490 face = xmalloc (sizeof *face);
5491 *face = *base_face;
5492 face->gc = 0;
5493 face->extra = NULL;
5494 face->overstrike
5495 = (! NILP (font_object)
5496 && FONT_WEIGHT_NAME_NUMERIC (face->lface[LFACE_WEIGHT_INDEX]) > 100
5497 && FONT_WEIGHT_NUMERIC (font_object) <= 100);
5499 /* Don't try to free the colors copied bitwise from BASE_FACE. */
5500 face->colors_copied_bitwise_p = 1;
5501 face->font = NILP (font_object) ? NULL : XFONT_OBJECT (font_object);
5502 face->gc = 0;
5504 cache_face (cache, face, face->hash);
5506 return face;
5508 #endif /* HAVE_WINDOW_SYSTEM */
5511 /* Realize the fully-specified face with attributes ATTRS in face
5512 cache CACHE for ASCII characters. Do it for X frame CACHE->f. If
5513 the new face doesn't share font with the default face, a fontname
5514 is allocated from the heap and set in `font_name' of the new face,
5515 but it is not yet loaded here. Value is a pointer to the newly
5516 created realized face. */
5518 static struct face *
5519 realize_x_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE])
5521 struct face *face = NULL;
5522 #ifdef HAVE_WINDOW_SYSTEM
5523 struct face *default_face;
5524 struct frame *f;
5525 Lisp_Object stipple, underline, overline, strike_through, box;
5527 eassert (FRAME_WINDOW_P (cache->f));
5529 /* Allocate a new realized face. */
5530 face = make_realized_face (attrs);
5531 face->ascii_face = face;
5533 f = cache->f;
5535 /* Determine the font to use. Most of the time, the font will be
5536 the same as the font of the default face, so try that first. */
5537 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5538 if (default_face
5539 && lface_same_font_attributes_p (default_face->lface, attrs))
5541 face->font = default_face->font;
5542 face->fontset
5543 = make_fontset_for_ascii_face (f, default_face->fontset, face);
5545 else
5547 /* If the face attribute ATTRS specifies a fontset, use it as
5548 the base of a new realized fontset. Otherwise, use the same
5549 base fontset as of the default face. The base determines
5550 registry and encoding of a font. It may also determine
5551 foundry and family. The other fields of font name pattern
5552 are constructed from ATTRS. */
5553 int fontset = face_fontset (attrs);
5555 /* If we are realizing the default face, ATTRS should specify a
5556 fontset. In other words, if FONTSET is -1, we are not
5557 realizing the default face, thus the default face should have
5558 already been realized. */
5559 if (fontset == -1)
5561 if (default_face)
5562 fontset = default_face->fontset;
5563 if (fontset == -1)
5564 emacs_abort ();
5566 if (! FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5567 attrs[LFACE_FONT_INDEX]
5568 = font_load_for_lface (f, attrs, attrs[LFACE_FONT_INDEX]);
5569 if (FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5571 face->font = XFONT_OBJECT (attrs[LFACE_FONT_INDEX]);
5572 face->fontset = make_fontset_for_ascii_face (f, fontset, face);
5574 else
5576 face->font = NULL;
5577 face->fontset = -1;
5581 if (face->font
5582 && FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]) > 100
5583 && FONT_WEIGHT_NUMERIC (attrs[LFACE_FONT_INDEX]) <= 100)
5584 face->overstrike = 1;
5586 /* Load colors, and set remaining attributes. */
5588 load_face_colors (f, face, attrs);
5590 /* Set up box. */
5591 box = attrs[LFACE_BOX_INDEX];
5592 if (STRINGP (box))
5594 /* A simple box of line width 1 drawn in color given by
5595 the string. */
5596 face->box_color = load_color (f, face, attrs[LFACE_BOX_INDEX],
5597 LFACE_BOX_INDEX);
5598 face->box = FACE_SIMPLE_BOX;
5599 face->box_line_width = 1;
5601 else if (INTEGERP (box))
5603 /* Simple box of specified line width in foreground color of the
5604 face. */
5605 eassert (XINT (box) != 0);
5606 face->box = FACE_SIMPLE_BOX;
5607 face->box_line_width = XINT (box);
5608 face->box_color = face->foreground;
5609 face->box_color_defaulted_p = 1;
5611 else if (CONSP (box))
5613 /* `(:width WIDTH :color COLOR :shadow SHADOW)'. SHADOW
5614 being one of `raised' or `sunken'. */
5615 face->box = FACE_SIMPLE_BOX;
5616 face->box_color = face->foreground;
5617 face->box_color_defaulted_p = 1;
5618 face->box_line_width = 1;
5620 while (CONSP (box))
5622 Lisp_Object keyword, value;
5624 keyword = XCAR (box);
5625 box = XCDR (box);
5627 if (!CONSP (box))
5628 break;
5629 value = XCAR (box);
5630 box = XCDR (box);
5632 if (EQ (keyword, QCline_width))
5634 if (INTEGERP (value) && XINT (value) != 0)
5635 face->box_line_width = XINT (value);
5637 else if (EQ (keyword, QCcolor))
5639 if (STRINGP (value))
5641 face->box_color = load_color (f, face, value,
5642 LFACE_BOX_INDEX);
5643 face->use_box_color_for_shadows_p = 1;
5646 else if (EQ (keyword, QCstyle))
5648 if (EQ (value, Qreleased_button))
5649 face->box = FACE_RAISED_BOX;
5650 else if (EQ (value, Qpressed_button))
5651 face->box = FACE_SUNKEN_BOX;
5656 /* Text underline, overline, strike-through. */
5658 underline = attrs[LFACE_UNDERLINE_INDEX];
5659 if (EQ (underline, Qt))
5661 /* Use default color (same as foreground color). */
5662 face->underline_p = 1;
5663 face->underline_type = FACE_UNDER_LINE;
5664 face->underline_defaulted_p = 1;
5665 face->underline_color = 0;
5667 else if (STRINGP (underline))
5669 /* Use specified color. */
5670 face->underline_p = 1;
5671 face->underline_type = FACE_UNDER_LINE;
5672 face->underline_defaulted_p = 0;
5673 face->underline_color
5674 = load_color (f, face, underline,
5675 LFACE_UNDERLINE_INDEX);
5677 else if (NILP (underline))
5679 face->underline_p = 0;
5680 face->underline_defaulted_p = 0;
5681 face->underline_color = 0;
5683 else if (CONSP (underline))
5685 /* `(:color COLOR :style STYLE)'.
5686 STYLE being one of `line' or `wave'. */
5687 face->underline_p = 1;
5688 face->underline_color = 0;
5689 face->underline_defaulted_p = 1;
5690 face->underline_type = FACE_UNDER_LINE;
5692 /* FIXME? This is also not robust about checking the precise form.
5693 See comments in Finternal_set_lisp_face_attribute. */
5694 while (CONSP (underline))
5696 Lisp_Object keyword, value;
5698 keyword = XCAR (underline);
5699 underline = XCDR (underline);
5701 if (!CONSP (underline))
5702 break;
5703 value = XCAR (underline);
5704 underline = XCDR (underline);
5706 if (EQ (keyword, QCcolor))
5708 if (EQ (value, Qforeground_color))
5710 face->underline_defaulted_p = 1;
5711 face->underline_color = 0;
5713 else if (STRINGP (value))
5715 face->underline_defaulted_p = 0;
5716 face->underline_color = load_color (f, face, value,
5717 LFACE_UNDERLINE_INDEX);
5720 else if (EQ (keyword, QCstyle))
5722 if (EQ (value, Qline))
5723 face->underline_type = FACE_UNDER_LINE;
5724 else if (EQ (value, Qwave))
5725 face->underline_type = FACE_UNDER_WAVE;
5730 overline = attrs[LFACE_OVERLINE_INDEX];
5731 if (STRINGP (overline))
5733 face->overline_color
5734 = load_color (f, face, attrs[LFACE_OVERLINE_INDEX],
5735 LFACE_OVERLINE_INDEX);
5736 face->overline_p = 1;
5738 else if (EQ (overline, Qt))
5740 face->overline_color = face->foreground;
5741 face->overline_color_defaulted_p = 1;
5742 face->overline_p = 1;
5745 strike_through = attrs[LFACE_STRIKE_THROUGH_INDEX];
5746 if (STRINGP (strike_through))
5748 face->strike_through_color
5749 = load_color (f, face, attrs[LFACE_STRIKE_THROUGH_INDEX],
5750 LFACE_STRIKE_THROUGH_INDEX);
5751 face->strike_through_p = 1;
5753 else if (EQ (strike_through, Qt))
5755 face->strike_through_color = face->foreground;
5756 face->strike_through_color_defaulted_p = 1;
5757 face->strike_through_p = 1;
5760 stipple = attrs[LFACE_STIPPLE_INDEX];
5761 if (!NILP (stipple))
5762 face->stipple = load_pixmap (f, stipple);
5763 #endif /* HAVE_WINDOW_SYSTEM */
5765 return face;
5769 /* Map a specified color of face FACE on frame F to a tty color index.
5770 IDX is either LFACE_FOREGROUND_INDEX or LFACE_BACKGROUND_INDEX, and
5771 specifies which color to map. Set *DEFAULTED to 1 if mapping to the
5772 default foreground/background colors. */
5774 static void
5775 map_tty_color (struct frame *f, struct face *face,
5776 enum lface_attribute_index idx, int *defaulted)
5778 Lisp_Object frame, color, def;
5779 int foreground_p = idx == LFACE_FOREGROUND_INDEX;
5780 unsigned long default_pixel =
5781 foreground_p ? FACE_TTY_DEFAULT_FG_COLOR : FACE_TTY_DEFAULT_BG_COLOR;
5782 unsigned long pixel = default_pixel;
5783 #ifdef MSDOS
5784 unsigned long default_other_pixel =
5785 foreground_p ? FACE_TTY_DEFAULT_BG_COLOR : FACE_TTY_DEFAULT_FG_COLOR;
5786 #endif
5788 eassert (idx == LFACE_FOREGROUND_INDEX || idx == LFACE_BACKGROUND_INDEX);
5790 XSETFRAME (frame, f);
5791 color = face->lface[idx];
5793 if (STRINGP (color)
5794 && SCHARS (color)
5795 && CONSP (Vtty_defined_color_alist)
5796 && (def = assq_no_quit (color, call1 (Qtty_color_alist, frame)),
5797 CONSP (def)))
5799 /* Associations in tty-defined-color-alist are of the form
5800 (NAME INDEX R G B). We need the INDEX part. */
5801 pixel = XINT (XCAR (XCDR (def)));
5804 if (pixel == default_pixel && STRINGP (color))
5806 pixel = load_color (f, face, color, idx);
5808 #ifdef MSDOS
5809 /* If the foreground of the default face is the default color,
5810 use the foreground color defined by the frame. */
5811 if (FRAME_MSDOS_P (f))
5813 if (pixel == default_pixel
5814 || pixel == FACE_TTY_DEFAULT_COLOR)
5816 if (foreground_p)
5817 pixel = FRAME_FOREGROUND_PIXEL (f);
5818 else
5819 pixel = FRAME_BACKGROUND_PIXEL (f);
5820 face->lface[idx] = tty_color_name (f, pixel);
5821 *defaulted = 1;
5823 else if (pixel == default_other_pixel)
5825 if (foreground_p)
5826 pixel = FRAME_BACKGROUND_PIXEL (f);
5827 else
5828 pixel = FRAME_FOREGROUND_PIXEL (f);
5829 face->lface[idx] = tty_color_name (f, pixel);
5830 *defaulted = 1;
5833 #endif /* MSDOS */
5836 if (foreground_p)
5837 face->foreground = pixel;
5838 else
5839 face->background = pixel;
5843 /* Realize the fully-specified face with attributes ATTRS in face
5844 cache CACHE for ASCII characters. Do it for TTY frame CACHE->f.
5845 Value is a pointer to the newly created realized face. */
5847 static struct face *
5848 realize_tty_face (struct face_cache *cache,
5849 Lisp_Object attrs[LFACE_VECTOR_SIZE])
5851 struct face *face;
5852 int weight, slant;
5853 int face_colors_defaulted = 0;
5854 struct frame *f = cache->f;
5856 /* Frame must be a termcap frame. */
5857 eassert (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f));
5859 /* Allocate a new realized face. */
5860 face = make_realized_face (attrs);
5861 #if 0
5862 face->font_name = FRAME_MSDOS_P (cache->f) ? "ms-dos" : "tty";
5863 #endif
5865 /* Map face attributes to TTY appearances. */
5866 weight = FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]);
5867 slant = FONT_SLANT_NAME_NUMERIC (attrs[LFACE_SLANT_INDEX]);
5868 if (weight > 100)
5869 face->tty_bold_p = 1;
5870 if (slant != 100)
5871 face->tty_italic_p = 1;
5872 if (!NILP (attrs[LFACE_UNDERLINE_INDEX]))
5873 face->tty_underline_p = 1;
5874 if (!NILP (attrs[LFACE_INVERSE_INDEX]))
5875 face->tty_reverse_p = 1;
5877 /* Map color names to color indices. */
5878 map_tty_color (f, face, LFACE_FOREGROUND_INDEX, &face_colors_defaulted);
5879 map_tty_color (f, face, LFACE_BACKGROUND_INDEX, &face_colors_defaulted);
5881 /* Swap colors if face is inverse-video. If the colors are taken
5882 from the frame colors, they are already inverted, since the
5883 frame-creation function calls x-handle-reverse-video. */
5884 if (face->tty_reverse_p && !face_colors_defaulted)
5886 unsigned long tem = face->foreground;
5887 face->foreground = face->background;
5888 face->background = tem;
5891 if (tty_suppress_bold_inverse_default_colors_p
5892 && face->tty_bold_p
5893 && face->background == FACE_TTY_DEFAULT_FG_COLOR
5894 && face->foreground == FACE_TTY_DEFAULT_BG_COLOR)
5895 face->tty_bold_p = 0;
5897 return face;
5901 DEFUN ("tty-suppress-bold-inverse-default-colors",
5902 Ftty_suppress_bold_inverse_default_colors,
5903 Stty_suppress_bold_inverse_default_colors, 1, 1, 0,
5904 doc: /* Suppress/allow boldness of faces with inverse default colors.
5905 SUPPRESS non-nil means suppress it.
5906 This affects bold faces on TTYs whose foreground is the default background
5907 color of the display and whose background is the default foreground color.
5908 For such faces, the bold face attribute is ignored if this variable
5909 is non-nil. */)
5910 (Lisp_Object suppress)
5912 tty_suppress_bold_inverse_default_colors_p = !NILP (suppress);
5913 ++face_change_count;
5914 return suppress;
5919 /***********************************************************************
5920 Computing Faces
5921 ***********************************************************************/
5923 /* Return the ID of the face to use to display character CH with face
5924 property PROP on frame F in current_buffer. */
5927 compute_char_face (struct frame *f, int ch, Lisp_Object prop)
5929 int face_id;
5931 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
5932 ch = 0;
5934 if (NILP (prop))
5936 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5937 face_id = FACE_FOR_CHAR (f, face, ch, -1, Qnil);
5939 else
5941 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5942 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5943 memcpy (attrs, default_face->lface, sizeof attrs);
5944 merge_face_ref (f, prop, attrs, 1, 0);
5945 face_id = lookup_face (f, attrs);
5948 return face_id;
5951 /* Return the face ID associated with buffer position POS for
5952 displaying ASCII characters. Return in *ENDPTR the position at
5953 which a different face is needed, as far as text properties and
5954 overlays are concerned. W is a window displaying current_buffer.
5956 REGION_BEG, REGION_END delimit the region, so it can be
5957 highlighted.
5959 LIMIT is a position not to scan beyond. That is to limit the time
5960 this function can take.
5962 If MOUSE is non-zero, use the character's mouse-face, not its face.
5964 BASE_FACE_ID, if non-negative, specifies a base face id to use
5965 instead of DEFAULT_FACE_ID.
5967 The face returned is suitable for displaying ASCII characters. */
5970 face_at_buffer_position (struct window *w, ptrdiff_t pos,
5971 ptrdiff_t *endptr, ptrdiff_t limit,
5972 int mouse, int base_face_id)
5974 struct frame *f = XFRAME (w->frame);
5975 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5976 Lisp_Object prop, position;
5977 ptrdiff_t i, noverlays;
5978 Lisp_Object *overlay_vec;
5979 ptrdiff_t endpos;
5980 Lisp_Object propname = mouse ? Qmouse_face : Qface;
5981 Lisp_Object limit1, end;
5982 struct face *default_face;
5984 /* W must display the current buffer. We could write this function
5985 to use the frame and buffer of W, but right now it doesn't. */
5986 /* eassert (XBUFFER (w->contents) == current_buffer); */
5988 XSETFASTINT (position, pos);
5990 endpos = ZV;
5992 /* Get the `face' or `mouse_face' text property at POS, and
5993 determine the next position at which the property changes. */
5994 prop = Fget_text_property (position, propname, w->contents);
5995 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
5996 end = Fnext_single_property_change (position, propname, w->contents, limit1);
5997 if (INTEGERP (end))
5998 endpos = XINT (end);
6000 /* Look at properties from overlays. */
6002 ptrdiff_t next_overlay;
6004 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, &next_overlay, 0);
6005 if (next_overlay < endpos)
6006 endpos = next_overlay;
6009 *endptr = endpos;
6012 int face_id;
6014 if (base_face_id >= 0)
6015 face_id = base_face_id;
6016 else if (NILP (Vface_remapping_alist))
6017 face_id = DEFAULT_FACE_ID;
6018 else
6019 face_id = lookup_basic_face (f, DEFAULT_FACE_ID);
6021 default_face = FACE_FROM_ID (f, face_id);
6024 /* Optimize common cases where we can use the default face. */
6025 if (noverlays == 0
6026 && NILP (prop))
6027 return default_face->id;
6029 /* Begin with attributes from the default face. */
6030 memcpy (attrs, default_face->lface, sizeof attrs);
6032 /* Merge in attributes specified via text properties. */
6033 if (!NILP (prop))
6034 merge_face_ref (f, prop, attrs, 1, 0);
6036 /* Now merge the overlay data. */
6037 noverlays = sort_overlays (overlay_vec, noverlays, w);
6038 for (i = 0; i < noverlays; i++)
6040 Lisp_Object oend;
6041 ptrdiff_t oendpos;
6043 prop = Foverlay_get (overlay_vec[i], propname);
6044 if (!NILP (prop))
6045 merge_face_ref (f, prop, attrs, 1, 0);
6047 oend = OVERLAY_END (overlay_vec[i]);
6048 oendpos = OVERLAY_POSITION (oend);
6049 if (oendpos < endpos)
6050 endpos = oendpos;
6053 *endptr = endpos;
6055 /* Look up a realized face with the given face attributes,
6056 or realize a new one for ASCII characters. */
6057 return lookup_face (f, attrs);
6060 /* Return the face ID at buffer position POS for displaying ASCII
6061 characters associated with overlay strings for overlay OVERLAY.
6063 Like face_at_buffer_position except for OVERLAY. Currently it
6064 simply disregards the `face' properties of all overlays. */
6067 face_for_overlay_string (struct window *w, ptrdiff_t pos,
6068 ptrdiff_t *endptr, ptrdiff_t limit,
6069 int mouse, Lisp_Object overlay)
6071 struct frame *f = XFRAME (w->frame);
6072 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6073 Lisp_Object prop, position;
6074 ptrdiff_t endpos;
6075 Lisp_Object propname = mouse ? Qmouse_face : Qface;
6076 Lisp_Object limit1, end;
6077 struct face *default_face;
6079 /* W must display the current buffer. We could write this function
6080 to use the frame and buffer of W, but right now it doesn't. */
6081 /* eassert (XBUFFER (w->contents) == current_buffer); */
6083 XSETFASTINT (position, pos);
6085 endpos = ZV;
6087 /* Get the `face' or `mouse_face' text property at POS, and
6088 determine the next position at which the property changes. */
6089 prop = Fget_text_property (position, propname, w->contents);
6090 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
6091 end = Fnext_single_property_change (position, propname, w->contents, limit1);
6092 if (INTEGERP (end))
6093 endpos = XINT (end);
6095 *endptr = endpos;
6097 /* Optimize common case where we can use the default face. */
6098 if (NILP (prop)
6099 && NILP (Vface_remapping_alist))
6100 return DEFAULT_FACE_ID;
6102 /* Begin with attributes from the default face. */
6103 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
6104 memcpy (attrs, default_face->lface, sizeof attrs);
6106 /* Merge in attributes specified via text properties. */
6107 if (!NILP (prop))
6108 merge_face_ref (f, prop, attrs, 1, 0);
6110 *endptr = endpos;
6112 /* Look up a realized face with the given face attributes,
6113 or realize a new one for ASCII characters. */
6114 return lookup_face (f, attrs);
6118 /* Compute the face at character position POS in Lisp string STRING on
6119 window W, for ASCII characters.
6121 If STRING is an overlay string, it comes from position BUFPOS in
6122 current_buffer, otherwise BUFPOS is zero to indicate that STRING is
6123 not an overlay string. W must display the current buffer.
6124 REGION_BEG and REGION_END give the start and end positions of the
6125 region; both are -1 if no region is visible.
6127 BASE_FACE_ID is the id of a face to merge with. For strings coming
6128 from overlays or the `display' property it is the face at BUFPOS.
6130 If MOUSE_P is non-zero, use the character's mouse-face, not its face.
6132 Set *ENDPTR to the next position where to check for faces in
6133 STRING; -1 if the face is constant from POS to the end of the
6134 string.
6136 Value is the id of the face to use. The face returned is suitable
6137 for displaying ASCII characters. */
6140 face_at_string_position (struct window *w, Lisp_Object string,
6141 ptrdiff_t pos, ptrdiff_t bufpos,
6142 ptrdiff_t *endptr, enum face_id base_face_id,
6143 int mouse_p)
6145 Lisp_Object prop, position, end, limit;
6146 struct frame *f = XFRAME (WINDOW_FRAME (w));
6147 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6148 struct face *base_face;
6149 bool multibyte_p = STRING_MULTIBYTE (string);
6150 Lisp_Object prop_name = mouse_p ? Qmouse_face : Qface;
6152 /* Get the value of the face property at the current position within
6153 STRING. Value is nil if there is no face property. */
6154 XSETFASTINT (position, pos);
6155 prop = Fget_text_property (position, prop_name, string);
6157 /* Get the next position at which to check for faces. Value of end
6158 is nil if face is constant all the way to the end of the string.
6159 Otherwise it is a string position where to check faces next.
6160 Limit is the maximum position up to which to check for property
6161 changes in Fnext_single_property_change. Strings are usually
6162 short, so set the limit to the end of the string. */
6163 XSETFASTINT (limit, SCHARS (string));
6164 end = Fnext_single_property_change (position, prop_name, string, limit);
6165 if (INTEGERP (end))
6166 *endptr = XFASTINT (end);
6167 else
6168 *endptr = -1;
6170 base_face = FACE_FROM_ID (f, base_face_id);
6171 eassert (base_face);
6173 /* Optimize the default case that there is no face property. */
6174 if (NILP (prop)
6175 && (multibyte_p
6176 /* We can't realize faces for different charsets differently
6177 if we don't have fonts, so we can stop here if not working
6178 on a window-system frame. */
6179 || !FRAME_WINDOW_P (f)
6180 || FACE_SUITABLE_FOR_ASCII_CHAR_P (base_face, 0)))
6181 return base_face->id;
6183 /* Begin with attributes from the base face. */
6184 memcpy (attrs, base_face->lface, sizeof attrs);
6186 /* Merge in attributes specified via text properties. */
6187 if (!NILP (prop))
6188 merge_face_ref (f, prop, attrs, 1, 0);
6190 /* Look up a realized face with the given face attributes,
6191 or realize a new one for ASCII characters. */
6192 return lookup_face (f, attrs);
6196 /* Merge a face into a realized face.
6198 F is frame where faces are (to be) realized.
6200 FACE_NAME is named face to merge.
6202 If FACE_NAME is nil, FACE_ID is face_id of realized face to merge.
6204 If FACE_NAME is t, FACE_ID is lface_id of face to merge.
6206 BASE_FACE_ID is realized face to merge into.
6208 Return new face id.
6212 merge_faces (struct frame *f, Lisp_Object face_name, int face_id,
6213 int base_face_id)
6215 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6216 struct face *base_face;
6218 base_face = FACE_FROM_ID (f, base_face_id);
6219 if (!base_face)
6220 return base_face_id;
6222 if (EQ (face_name, Qt))
6224 if (face_id < 0 || face_id >= lface_id_to_name_size)
6225 return base_face_id;
6226 face_name = lface_id_to_name[face_id];
6227 /* When called during make-frame, lookup_derived_face may fail
6228 if the faces are uninitialized. Don't signal an error. */
6229 face_id = lookup_derived_face (f, face_name, base_face_id, 0);
6230 return (face_id >= 0 ? face_id : base_face_id);
6233 /* Begin with attributes from the base face. */
6234 memcpy (attrs, base_face->lface, sizeof attrs);
6236 if (!NILP (face_name))
6238 if (!merge_named_face (f, face_name, attrs, 0))
6239 return base_face_id;
6241 else
6243 struct face *face;
6244 if (face_id < 0)
6245 return base_face_id;
6246 face = FACE_FROM_ID (f, face_id);
6247 if (!face)
6248 return base_face_id;
6249 merge_face_vectors (f, face->lface, attrs, 0);
6252 /* Look up a realized face with the given face attributes,
6253 or realize a new one for ASCII characters. */
6254 return lookup_face (f, attrs);
6259 #ifndef HAVE_X_WINDOWS
6260 DEFUN ("x-load-color-file", Fx_load_color_file,
6261 Sx_load_color_file, 1, 1, 0,
6262 doc: /* Create an alist of color entries from an external file.
6264 The file should define one named RGB color per line like so:
6265 R G B name
6266 where R,G,B are numbers between 0 and 255 and name is an arbitrary string. */)
6267 (Lisp_Object filename)
6269 FILE *fp;
6270 Lisp_Object cmap = Qnil;
6271 Lisp_Object abspath;
6273 CHECK_STRING (filename);
6274 abspath = Fexpand_file_name (filename, Qnil);
6276 block_input ();
6277 fp = emacs_fopen (SSDATA (abspath), "rt");
6278 if (fp)
6280 char buf[512];
6281 int red, green, blue;
6282 int num;
6284 while (fgets (buf, sizeof (buf), fp) != NULL) {
6285 if (sscanf (buf, "%u %u %u %n", &red, &green, &blue, &num) == 3)
6287 #ifdef HAVE_NTGUI
6288 int color = RGB (red, green, blue);
6289 #else
6290 int color = (red << 16) | (green << 8) | blue;
6291 #endif
6292 char *name = buf + num;
6293 ptrdiff_t len = strlen (name);
6294 len -= 0 < len && name[len - 1] == '\n';
6295 cmap = Fcons (Fcons (make_string (name, len), make_number (color)),
6296 cmap);
6299 fclose (fp);
6301 unblock_input ();
6302 return cmap;
6304 #endif
6307 /***********************************************************************
6308 Tests
6309 ***********************************************************************/
6311 #ifdef GLYPH_DEBUG
6313 /* Print the contents of the realized face FACE to stderr. */
6315 static void
6316 dump_realized_face (struct face *face)
6318 fprintf (stderr, "ID: %d\n", face->id);
6319 #ifdef HAVE_X_WINDOWS
6320 fprintf (stderr, "gc: %ld\n", (long) face->gc);
6321 #endif
6322 fprintf (stderr, "foreground: 0x%lx (%s)\n",
6323 face->foreground,
6324 SDATA (face->lface[LFACE_FOREGROUND_INDEX]));
6325 fprintf (stderr, "background: 0x%lx (%s)\n",
6326 face->background,
6327 SDATA (face->lface[LFACE_BACKGROUND_INDEX]));
6328 if (face->font)
6329 fprintf (stderr, "font_name: %s (%s)\n",
6330 SDATA (face->font->props[FONT_NAME_INDEX]),
6331 SDATA (face->lface[LFACE_FAMILY_INDEX]));
6332 #ifdef HAVE_X_WINDOWS
6333 fprintf (stderr, "font = %p\n", face->font);
6334 #endif
6335 fprintf (stderr, "fontset: %d\n", face->fontset);
6336 fprintf (stderr, "underline: %d (%s)\n",
6337 face->underline_p,
6338 SDATA (Fsymbol_name (face->lface[LFACE_UNDERLINE_INDEX])));
6339 fprintf (stderr, "hash: %d\n", face->hash);
6343 DEFUN ("dump-face", Fdump_face, Sdump_face, 0, 1, 0, doc: /* */)
6344 (Lisp_Object n)
6346 if (NILP (n))
6348 int i;
6350 fprintf (stderr, "font selection order: ");
6351 for (i = 0; i < DIM (font_sort_order); ++i)
6352 fprintf (stderr, "%d ", font_sort_order[i]);
6353 fprintf (stderr, "\n");
6355 fprintf (stderr, "alternative fonts: ");
6356 debug_print (Vface_alternative_font_family_alist);
6357 fprintf (stderr, "\n");
6359 for (i = 0; i < FRAME_FACE_CACHE (SELECTED_FRAME ())->used; ++i)
6360 Fdump_face (make_number (i));
6362 else
6364 struct face *face;
6365 CHECK_NUMBER (n);
6366 face = FACE_FROM_ID (SELECTED_FRAME (), XINT (n));
6367 if (face == NULL)
6368 error ("Not a valid face");
6369 dump_realized_face (face);
6372 return Qnil;
6376 DEFUN ("show-face-resources", Fshow_face_resources, Sshow_face_resources,
6377 0, 0, 0, doc: /* */)
6378 (void)
6380 fprintf (stderr, "number of colors = %d\n", ncolors_allocated);
6381 fprintf (stderr, "number of pixmaps = %d\n", npixmaps_allocated);
6382 fprintf (stderr, "number of GCs = %d\n", ngcs);
6383 return Qnil;
6386 #endif /* GLYPH_DEBUG */
6390 /***********************************************************************
6391 Initialization
6392 ***********************************************************************/
6394 void
6395 syms_of_xfaces (void)
6397 DEFSYM (Qface, "face");
6398 DEFSYM (Qface_no_inherit, "face-no-inherit");
6399 DEFSYM (Qbitmap_spec_p, "bitmap-spec-p");
6400 DEFSYM (Qframe_set_background_mode, "frame-set-background-mode");
6402 /* Lisp face attribute keywords. */
6403 DEFSYM (QCfamily, ":family");
6404 DEFSYM (QCheight, ":height");
6405 DEFSYM (QCweight, ":weight");
6406 DEFSYM (QCslant, ":slant");
6407 DEFSYM (QCunderline, ":underline");
6408 DEFSYM (QCinverse_video, ":inverse-video");
6409 DEFSYM (QCreverse_video, ":reverse-video");
6410 DEFSYM (QCforeground, ":foreground");
6411 DEFSYM (QCbackground, ":background");
6412 DEFSYM (QCstipple, ":stipple");
6413 DEFSYM (QCwidth, ":width");
6414 DEFSYM (QCfont, ":font");
6415 DEFSYM (QCfontset, ":fontset");
6416 DEFSYM (QCdistant_foreground, ":distant-foreground");
6417 DEFSYM (QCbold, ":bold");
6418 DEFSYM (QCitalic, ":italic");
6419 DEFSYM (QCoverline, ":overline");
6420 DEFSYM (QCstrike_through, ":strike-through");
6421 DEFSYM (QCbox, ":box");
6422 DEFSYM (QCinherit, ":inherit");
6424 /* Symbols used for Lisp face attribute values. */
6425 DEFSYM (QCcolor, ":color");
6426 DEFSYM (QCline_width, ":line-width");
6427 DEFSYM (QCstyle, ":style");
6428 DEFSYM (Qline, "line");
6429 DEFSYM (Qwave, "wave");
6430 DEFSYM (Qreleased_button, "released-button");
6431 DEFSYM (Qpressed_button, "pressed-button");
6432 DEFSYM (Qnormal, "normal");
6433 DEFSYM (Qextra_light, "extra-light");
6434 DEFSYM (Qlight, "light");
6435 DEFSYM (Qsemi_light, "semi-light");
6436 DEFSYM (Qsemi_bold, "semi-bold");
6437 DEFSYM (Qbold, "bold");
6438 DEFSYM (Qextra_bold, "extra-bold");
6439 DEFSYM (Qultra_bold, "ultra-bold");
6440 DEFSYM (Qoblique, "oblique");
6441 DEFSYM (Qitalic, "italic");
6442 DEFSYM (Qbackground_color, "background-color");
6443 DEFSYM (Qforeground_color, "foreground-color");
6444 DEFSYM (Qunspecified, "unspecified");
6445 DEFSYM (QCignore_defface, ":ignore-defface");
6447 DEFSYM (Qface_alias, "face-alias");
6448 DEFSYM (Qdefault, "default");
6449 DEFSYM (Qtool_bar, "tool-bar");
6450 DEFSYM (Qregion, "region");
6451 DEFSYM (Qfringe, "fringe");
6452 DEFSYM (Qheader_line, "header-line");
6453 DEFSYM (Qscroll_bar, "scroll-bar");
6454 DEFSYM (Qmenu, "menu");
6455 DEFSYM (Qcursor, "cursor");
6456 DEFSYM (Qborder, "border");
6457 DEFSYM (Qmouse, "mouse");
6458 DEFSYM (Qmode_line_inactive, "mode-line-inactive");
6459 DEFSYM (Qvertical_border, "vertical-border");
6460 DEFSYM (Qwindow_divider, "window-divider");
6461 DEFSYM (Qwindow_divider_first_pixel, "window-divider-first-pixel");
6462 DEFSYM (Qwindow_divider_last_pixel, "window-divider-last-pixel");
6463 DEFSYM (Qtty_color_desc, "tty-color-desc");
6464 DEFSYM (Qtty_color_standard_values, "tty-color-standard-values");
6465 DEFSYM (Qtty_color_by_index, "tty-color-by-index");
6466 DEFSYM (Qtty_color_alist, "tty-color-alist");
6467 DEFSYM (Qscalable_fonts_allowed, "scalable-fonts-allowed");
6469 Vparam_value_alist = list1 (Fcons (Qnil, Qnil));
6470 staticpro (&Vparam_value_alist);
6471 Vface_alternative_font_family_alist = Qnil;
6472 staticpro (&Vface_alternative_font_family_alist);
6473 Vface_alternative_font_registry_alist = Qnil;
6474 staticpro (&Vface_alternative_font_registry_alist);
6476 defsubr (&Sinternal_make_lisp_face);
6477 defsubr (&Sinternal_lisp_face_p);
6478 defsubr (&Sinternal_set_lisp_face_attribute);
6479 #ifdef HAVE_WINDOW_SYSTEM
6480 defsubr (&Sinternal_set_lisp_face_attribute_from_resource);
6481 #endif
6482 defsubr (&Scolor_gray_p);
6483 defsubr (&Scolor_supported_p);
6484 #ifndef HAVE_X_WINDOWS
6485 defsubr (&Sx_load_color_file);
6486 #endif
6487 defsubr (&Sface_attribute_relative_p);
6488 defsubr (&Smerge_face_attribute);
6489 defsubr (&Sinternal_get_lisp_face_attribute);
6490 defsubr (&Sinternal_lisp_face_attribute_values);
6491 defsubr (&Sinternal_lisp_face_equal_p);
6492 defsubr (&Sinternal_lisp_face_empty_p);
6493 defsubr (&Sinternal_copy_lisp_face);
6494 defsubr (&Sinternal_merge_in_global_face);
6495 defsubr (&Sface_font);
6496 defsubr (&Sframe_face_alist);
6497 defsubr (&Sdisplay_supports_face_attributes_p);
6498 defsubr (&Scolor_distance);
6499 defsubr (&Sinternal_set_font_selection_order);
6500 defsubr (&Sinternal_set_alternative_font_family_alist);
6501 defsubr (&Sinternal_set_alternative_font_registry_alist);
6502 defsubr (&Sface_attributes_as_vector);
6503 #ifdef GLYPH_DEBUG
6504 defsubr (&Sdump_face);
6505 defsubr (&Sshow_face_resources);
6506 #endif /* GLYPH_DEBUG */
6507 defsubr (&Sclear_face_cache);
6508 defsubr (&Stty_suppress_bold_inverse_default_colors);
6510 #if defined DEBUG_X_COLORS && defined HAVE_X_WINDOWS
6511 defsubr (&Sdump_colors);
6512 #endif
6514 DEFVAR_LISP ("face-new-frame-defaults", Vface_new_frame_defaults,
6515 doc: /* List of global face definitions (for internal use only.) */);
6516 Vface_new_frame_defaults = Qnil;
6518 DEFVAR_LISP ("face-default-stipple", Vface_default_stipple,
6519 doc: /* Default stipple pattern used on monochrome displays.
6520 This stipple pattern is used on monochrome displays
6521 instead of shades of gray for a face background color.
6522 See `set-face-stipple' for possible values for this variable. */);
6523 Vface_default_stipple = build_pure_c_string ("gray3");
6525 DEFVAR_LISP ("tty-defined-color-alist", Vtty_defined_color_alist,
6526 doc: /* An alist of defined terminal colors and their RGB values.
6527 See the docstring of `tty-color-alist' for the details. */);
6528 Vtty_defined_color_alist = Qnil;
6530 DEFVAR_LISP ("scalable-fonts-allowed", Vscalable_fonts_allowed,
6531 doc: /* Allowed scalable fonts.
6532 A value of nil means don't allow any scalable fonts.
6533 A value of t means allow any scalable font.
6534 Otherwise, value must be a list of regular expressions. A font may be
6535 scaled if its name matches a regular expression in the list.
6536 Note that if value is nil, a scalable font might still be used, if no
6537 other font of the appropriate family and registry is available. */);
6538 Vscalable_fonts_allowed = Qnil;
6540 DEFVAR_LISP ("face-ignored-fonts", Vface_ignored_fonts,
6541 doc: /* List of ignored fonts.
6542 Each element is a regular expression that matches names of fonts to
6543 ignore. */);
6544 Vface_ignored_fonts = Qnil;
6546 DEFVAR_LISP ("face-remapping-alist", Vface_remapping_alist,
6547 doc: /* Alist of face remappings.
6548 Each element is of the form:
6550 (FACE . REPLACEMENT),
6552 which causes display of the face FACE to use REPLACEMENT instead.
6553 REPLACEMENT is a face specification, i.e. one of the following:
6555 (1) a face name
6556 (2) a property list of attribute/value pairs, or
6557 (3) a list in which each element has the form of (1) or (2).
6559 List values for REPLACEMENT are merged to form the final face
6560 specification, with earlier entries taking precedence, in the same as
6561 as in the `face' text property.
6563 Face-name remapping cycles are suppressed; recursive references use
6564 the underlying face instead of the remapped face. So a remapping of
6565 the form:
6567 (FACE EXTRA-FACE... FACE)
6571 (FACE (FACE-ATTR VAL ...) FACE)
6573 causes EXTRA-FACE... or (FACE-ATTR VAL ...) to be _merged_ with the
6574 existing definition of FACE. Note that this isn't necessary for the
6575 default face, since every face inherits from the default face.
6577 If this variable is made buffer-local, the face remapping takes effect
6578 only in that buffer. For instance, the mode my-mode could define a
6579 face `my-mode-default', and then in the mode setup function, do:
6581 (set (make-local-variable 'face-remapping-alist)
6582 '((default my-mode-default)))).
6584 Because Emacs normally only redraws screen areas when the underlying
6585 buffer contents change, you may need to call `redraw-display' after
6586 changing this variable for it to take effect. */);
6587 Vface_remapping_alist = Qnil;
6589 DEFVAR_LISP ("face-font-rescale-alist", Vface_font_rescale_alist,
6590 doc: /* Alist of fonts vs the rescaling factors.
6591 Each element is a cons (FONT-PATTERN . RESCALE-RATIO), where
6592 FONT-PATTERN is a font-spec or a regular expression matching a font name, and
6593 RESCALE-RATIO is a floating point number to specify how much larger
6594 \(or smaller) font we should use. For instance, if a face requests
6595 a font of 10 point, we actually use a font of 10 * RESCALE-RATIO point. */);
6596 Vface_font_rescale_alist = Qnil;
6598 #ifdef HAVE_WINDOW_SYSTEM
6599 defsubr (&Sbitmap_spec_p);
6600 defsubr (&Sx_list_fonts);
6601 defsubr (&Sinternal_face_x_get_resource);
6602 defsubr (&Sx_family_fonts);
6603 #endif