Support variable-unquoting syntax in bat-mode
[emacs.git] / src / xfaces.c
bloba9c2f37e9f2bcaf92d0af687c255648d29d2163a
1 /* xfaces.c -- "Face" primitives.
3 Copyright (C) 1993-1994, 1998-2018 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 (at
10 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 <https://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 you 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 <stdlib.h>
204 #include "sysstdio.h"
205 #include <sys/types.h>
206 #include <sys/stat.h>
208 #include "lisp.h"
209 #include "character.h"
210 #include "frame.h"
212 #ifdef USE_MOTIF
213 #include <Xm/Xm.h>
214 #include <Xm/XmStrDefs.h>
215 #endif /* USE_MOTIF */
217 #ifdef MSDOS
218 #include "dosfns.h"
219 #endif
221 #ifdef HAVE_WINDOW_SYSTEM
222 #include TERM_HEADER
223 #include "fontset.h"
224 #ifdef HAVE_NTGUI
225 #define GCGraphicsExposures 0
226 #endif /* HAVE_NTGUI */
228 #ifdef HAVE_NS
229 #define GCGraphicsExposures 0
230 #endif /* HAVE_NS */
231 #endif /* HAVE_WINDOW_SYSTEM */
233 #include "buffer.h"
234 #include "dispextern.h"
235 #include "blockinput.h"
236 #include "window.h"
237 #include "termchar.h"
239 #include "font.h"
241 #ifdef HAVE_X_WINDOWS
243 /* Compensate for a bug in Xos.h on some systems, on which it requires
244 time.h. On some such systems, Xos.h tries to redefine struct
245 timeval and struct timezone if USG is #defined while it is
246 #included. */
248 #ifdef XOS_NEEDS_TIME_H
249 #include <time.h>
250 #undef USG
251 #include <X11/Xos.h>
252 #define USG
253 #define __TIMEVAL__
254 #if defined USG || defined __TIMEVAL__ /* Don't warn about unused macros. */
255 #endif
256 #else /* not XOS_NEEDS_TIME_H */
257 #include <X11/Xos.h>
258 #endif /* not XOS_NEEDS_TIME_H */
260 #endif /* HAVE_X_WINDOWS */
262 #include <c-ctype.h>
264 /* True if face attribute ATTR is unspecified. */
266 #define UNSPECIFIEDP(ATTR) EQ ((ATTR), Qunspecified)
268 /* True if face attribute ATTR is `ignore-defface'. */
270 #define IGNORE_DEFFACE_P(ATTR) EQ ((ATTR), QCignore_defface)
272 /* Size of hash table of realized faces in face caches (should be a
273 prime number). */
275 #define FACE_CACHE_BUCKETS_SIZE 1001
277 char unspecified_fg[] = "unspecified-fg", unspecified_bg[] = "unspecified-bg";
279 /* Alist of alternative font families. Each element is of the form
280 (FAMILY FAMILY1 FAMILY2 ...). If fonts of FAMILY can't be loaded,
281 try FAMILY1, then FAMILY2, ... */
283 Lisp_Object Vface_alternative_font_family_alist;
285 /* Alist of alternative font registries. Each element is of the form
286 (REGISTRY REGISTRY1 REGISTRY2...). If fonts of REGISTRY can't be
287 loaded, try REGISTRY1, then REGISTRY2, ... */
289 Lisp_Object Vface_alternative_font_registry_alist;
291 /* The next ID to assign to Lisp faces. */
293 static int next_lface_id;
295 /* A vector mapping Lisp face Id's to face names. */
297 static Lisp_Object *lface_id_to_name;
298 static ptrdiff_t lface_id_to_name_size;
300 #ifdef HAVE_WINDOW_SYSTEM
302 /* Counter for calls to clear_face_cache. If this counter reaches
303 CLEAR_FONT_TABLE_COUNT, and a frame has more than
304 CLEAR_FONT_TABLE_NFONTS load, unused fonts are freed. */
306 static int clear_font_table_count;
307 #define CLEAR_FONT_TABLE_COUNT 100
308 #define CLEAR_FONT_TABLE_NFONTS 10
310 #endif /* HAVE_WINDOW_SYSTEM */
312 /* True means face attributes have been changed since the last
313 redisplay. Used in redisplay_internal. */
315 bool face_change;
317 /* True means don't display bold text if a face's foreground
318 and background colors are the inverse of the default colors of the
319 display. This is a kluge to suppress `bold black' foreground text
320 which is hard to read on an LCD monitor. */
322 static bool tty_suppress_bold_inverse_default_colors_p;
324 /* A list of the form `((x . y))' used to avoid consing in
325 Finternal_set_lisp_face_attribute. */
327 static Lisp_Object Vparam_value_alist;
329 /* The total number of colors currently allocated. */
331 #ifdef GLYPH_DEBUG
332 static int ncolors_allocated;
333 static int npixmaps_allocated;
334 static int ngcs;
335 #endif
337 /* True means the definition of the `menu' face for new frames has
338 been changed. */
340 static bool menu_face_changed_default;
342 struct named_merge_point;
344 static struct face *realize_face (struct face_cache *, Lisp_Object *,
345 int);
346 static struct face *realize_x_face (struct face_cache *, Lisp_Object *);
347 static struct face *realize_tty_face (struct face_cache *, Lisp_Object *);
348 static bool realize_basic_faces (struct frame *);
349 static bool realize_default_face (struct frame *);
350 static void realize_named_face (struct frame *, Lisp_Object, int);
351 static struct face_cache *make_face_cache (struct frame *);
352 static void free_face_cache (struct face_cache *);
353 static bool merge_face_ref (struct frame *, Lisp_Object, Lisp_Object *,
354 bool, struct named_merge_point *);
355 static int color_distance (XColor *x, XColor *y);
357 #ifdef HAVE_WINDOW_SYSTEM
358 static void set_font_frame_param (Lisp_Object, Lisp_Object);
359 static void clear_face_gcs (struct face_cache *);
360 static struct face *realize_non_ascii_face (struct frame *, Lisp_Object,
361 struct face *);
362 #endif /* HAVE_WINDOW_SYSTEM */
364 /***********************************************************************
365 Utilities
366 ***********************************************************************/
368 #ifdef HAVE_X_WINDOWS
370 #ifdef DEBUG_X_COLORS
372 /* The following is a poor mans infrastructure for debugging X color
373 allocation problems on displays with PseudoColor-8. Some X servers
374 like 3.3.5 XF86_SVGA with Matrox cards apparently don't implement
375 color reference counts completely so that they don't signal an
376 error when a color is freed whose reference count is already 0.
377 Other X servers do. To help me debug this, the following code
378 implements a simple reference counting schema of its own, for a
379 single display/screen. --gerd. */
381 /* Reference counts for pixel colors. */
383 int color_count[256];
385 /* Register color PIXEL as allocated. */
387 void
388 register_color (unsigned long pixel)
390 eassert (pixel < 256);
391 ++color_count[pixel];
395 /* Register color PIXEL as deallocated. */
397 void
398 unregister_color (unsigned long pixel)
400 eassert (pixel < 256);
401 if (color_count[pixel] > 0)
402 --color_count[pixel];
403 else
404 emacs_abort ();
408 /* Register N colors from PIXELS as deallocated. */
410 void
411 unregister_colors (unsigned long *pixels, int n)
413 int i;
414 for (i = 0; i < n; ++i)
415 unregister_color (pixels[i]);
419 DEFUN ("dump-colors", Fdump_colors, Sdump_colors, 0, 0, 0,
420 doc: /* Dump currently allocated colors to stderr. */)
421 (void)
423 int i, n;
425 fputc ('\n', stderr);
427 for (i = n = 0; i < ARRAYELTS (color_count); ++i)
428 if (color_count[i])
430 fprintf (stderr, "%3d: %5d", i, color_count[i]);
431 ++n;
432 if (n % 5 == 0)
433 fputc ('\n', stderr);
434 else
435 fputc ('\t', stderr);
438 if (n % 5 != 0)
439 fputc ('\n', stderr);
440 return Qnil;
443 #endif /* DEBUG_X_COLORS */
446 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
447 color values. Interrupt input must be blocked when this function
448 is called. */
450 void
451 x_free_colors (struct frame *f, unsigned long *pixels, int npixels)
453 /* If display has an immutable color map, freeing colors is not
454 necessary and some servers don't allow it. So don't do it. */
455 if (x_mutable_colormap (FRAME_X_VISUAL (f)))
457 #ifdef DEBUG_X_COLORS
458 unregister_colors (pixels, npixels);
459 #endif
460 XFreeColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
461 pixels, npixels, 0);
466 #ifdef USE_X_TOOLKIT
468 /* Free colors used on display DPY. PIXELS is an array of NPIXELS pixel
469 color values. Interrupt input must be blocked when this function
470 is called. */
472 void
473 x_free_dpy_colors (Display *dpy, Screen *screen, Colormap cmap,
474 unsigned long *pixels, int npixels)
476 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
478 /* If display has an immutable color map, freeing colors is not
479 necessary and some servers don't allow it. So don't do it. */
480 if (x_mutable_colormap (dpyinfo->visual))
482 #ifdef DEBUG_X_COLORS
483 unregister_colors (pixels, npixels);
484 #endif
485 XFreeColors (dpy, cmap, pixels, npixels, 0);
488 #endif /* USE_X_TOOLKIT */
490 /* Create and return a GC for use on frame F. GC values and mask
491 are given by XGCV and MASK. */
493 static GC
494 x_create_gc (struct frame *f, unsigned long mask, XGCValues *xgcv)
496 GC gc;
497 block_input ();
498 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f), mask, xgcv);
499 unblock_input ();
500 IF_DEBUG (++ngcs);
501 return gc;
505 /* Free GC which was used on frame F. */
507 static void
508 x_free_gc (struct frame *f, GC gc)
510 eassert (input_blocked_p ());
511 IF_DEBUG ((--ngcs, eassert (ngcs >= 0)));
512 XFreeGC (FRAME_X_DISPLAY (f), gc);
515 #endif /* HAVE_X_WINDOWS */
517 #ifdef HAVE_NTGUI
518 /* W32 emulation of GCs */
520 static GC
521 x_create_gc (struct frame *f, unsigned long mask, XGCValues *xgcv)
523 GC gc;
524 block_input ();
525 gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, xgcv);
526 unblock_input ();
527 IF_DEBUG (++ngcs);
528 return gc;
532 /* Free GC which was used on frame F. */
534 static void
535 x_free_gc (struct frame *f, GC gc)
537 IF_DEBUG ((--ngcs, eassert (ngcs >= 0)));
538 xfree (gc);
541 #endif /* HAVE_NTGUI */
543 #ifdef HAVE_NS
544 /* NS emulation of GCs */
546 static GC
547 x_create_gc (struct frame *f,
548 unsigned long mask,
549 XGCValues *xgcv)
551 GC gc = xmalloc (sizeof *gc);
552 *gc = *xgcv;
553 return gc;
556 static void
557 x_free_gc (struct frame *f, GC gc)
559 xfree (gc);
561 #endif /* HAVE_NS */
563 /***********************************************************************
564 Frames and faces
565 ***********************************************************************/
567 /* Initialize face cache and basic faces for frame F. */
569 void
570 init_frame_faces (struct frame *f)
572 /* Make a face cache, if F doesn't have one. */
573 if (FRAME_FACE_CACHE (f) == NULL)
574 FRAME_FACE_CACHE (f) = make_face_cache (f);
576 #ifdef HAVE_WINDOW_SYSTEM
577 /* Make the image cache. */
578 if (FRAME_WINDOW_P (f))
580 /* We initialize the image cache when creating the first frame
581 on a terminal, and not during terminal creation. This way,
582 `x-open-connection' on a tty won't create an image cache. */
583 if (FRAME_IMAGE_CACHE (f) == NULL)
584 FRAME_IMAGE_CACHE (f) = make_image_cache ();
585 ++FRAME_IMAGE_CACHE (f)->refcount;
587 #endif /* HAVE_WINDOW_SYSTEM */
589 /* Realize faces early (Bug#17889). */
590 if (!realize_basic_faces (f))
591 emacs_abort ();
595 /* Free face cache of frame F. Called from frame-dependent
596 resource freeing function, e.g. (x|tty)_free_frame_resources. */
598 void
599 free_frame_faces (struct frame *f)
601 struct face_cache *face_cache = FRAME_FACE_CACHE (f);
603 if (face_cache)
605 free_face_cache (face_cache);
606 FRAME_FACE_CACHE (f) = NULL;
609 #ifdef HAVE_WINDOW_SYSTEM
610 if (FRAME_WINDOW_P (f))
612 struct image_cache *image_cache = FRAME_IMAGE_CACHE (f);
613 if (image_cache)
615 --image_cache->refcount;
616 if (image_cache->refcount == 0)
617 free_image_cache (f);
620 #endif /* HAVE_WINDOW_SYSTEM */
624 /* Clear face caches, and recompute basic faces for frame F. Call
625 this after changing frame parameters on which those faces depend,
626 or when realized faces have been freed due to changing attributes
627 of named faces. */
629 void
630 recompute_basic_faces (struct frame *f)
632 if (FRAME_FACE_CACHE (f))
634 clear_face_cache (false);
635 if (!realize_basic_faces (f))
636 emacs_abort ();
641 /* Clear the face caches of all frames. CLEAR_FONTS_P means
642 try to free unused fonts, too. */
644 void
645 clear_face_cache (bool clear_fonts_p)
647 #ifdef HAVE_WINDOW_SYSTEM
648 Lisp_Object tail, frame;
650 if (clear_fonts_p
651 || ++clear_font_table_count == CLEAR_FONT_TABLE_COUNT)
653 /* From time to time see if we can unload some fonts. This also
654 frees all realized faces on all frames. Fonts needed by
655 faces will be loaded again when faces are realized again. */
656 clear_font_table_count = 0;
658 FOR_EACH_FRAME (tail, frame)
660 struct frame *f = XFRAME (frame);
661 if (FRAME_WINDOW_P (f)
662 && FRAME_DISPLAY_INFO (f)->n_fonts > CLEAR_FONT_TABLE_NFONTS)
664 clear_font_cache (f);
665 free_all_realized_faces (frame);
669 else
671 /* Clear GCs of realized faces. */
672 FOR_EACH_FRAME (tail, frame)
674 struct frame *f = XFRAME (frame);
675 if (FRAME_WINDOW_P (f))
676 clear_face_gcs (FRAME_FACE_CACHE (f));
678 clear_image_caches (Qnil);
680 #endif /* HAVE_WINDOW_SYSTEM */
683 DEFUN ("clear-face-cache", Fclear_face_cache, Sclear_face_cache, 0, 1, 0,
684 doc: /* Clear face caches on all frames.
685 Optional THOROUGHLY non-nil means try to free unused fonts, too. */)
686 (Lisp_Object thoroughly)
688 clear_face_cache (!NILP (thoroughly));
689 face_change = true;
690 windows_or_buffers_changed = 53;
691 return Qnil;
695 /***********************************************************************
696 X Pixmaps
697 ***********************************************************************/
699 #ifdef HAVE_WINDOW_SYSTEM
701 DEFUN ("bitmap-spec-p", Fbitmap_spec_p, Sbitmap_spec_p, 1, 1, 0,
702 doc: /* Value is non-nil if OBJECT is a valid bitmap specification.
703 A bitmap specification is either a string, a file name, or a list
704 \(WIDTH HEIGHT DATA) where WIDTH is the pixel width of the bitmap,
705 HEIGHT is its height, and DATA is a string containing the bits of
706 the pixmap. Bits are stored row by row, each row occupies
707 \(WIDTH + 7)/8 bytes. */)
708 (Lisp_Object object)
710 bool pixmap_p = false;
712 if (STRINGP (object))
713 /* If OBJECT is a string, it's a file name. */
714 pixmap_p = true;
715 else if (CONSP (object))
717 /* Otherwise OBJECT must be (WIDTH HEIGHT DATA), WIDTH and
718 HEIGHT must be ints > 0, and DATA must be string large
719 enough to hold a bitmap of the specified size. */
720 Lisp_Object width, height, data;
722 height = width = data = Qnil;
724 if (CONSP (object))
726 width = XCAR (object);
727 object = XCDR (object);
728 if (CONSP (object))
730 height = XCAR (object);
731 object = XCDR (object);
732 if (CONSP (object))
733 data = XCAR (object);
737 if (STRINGP (data)
738 && RANGED_INTEGERP (1, width, INT_MAX)
739 && RANGED_INTEGERP (1, height, INT_MAX))
741 int bytes_per_row = (XINT (width) + CHAR_BIT - 1) / CHAR_BIT;
742 if (XINT (height) <= SBYTES (data) / bytes_per_row)
743 pixmap_p = true;
747 return pixmap_p ? Qt : Qnil;
751 /* Load a bitmap according to NAME (which is either a file name or a
752 pixmap spec) for use on frame F. Value is the bitmap_id (see
753 xfns.c). If NAME is nil, return with a bitmap id of zero. If
754 bitmap cannot be loaded, display a message saying so, and return
755 zero. */
757 static ptrdiff_t
758 load_pixmap (struct frame *f, Lisp_Object name)
760 ptrdiff_t bitmap_id;
762 if (NILP (name))
763 return 0;
765 CHECK_TYPE (!NILP (Fbitmap_spec_p (name)), Qbitmap_spec_p, name);
767 block_input ();
768 if (CONSP (name))
770 /* Decode a bitmap spec into a bitmap. */
772 int h, w;
773 Lisp_Object bits;
775 w = XINT (Fcar (name));
776 h = XINT (Fcar (Fcdr (name)));
777 bits = Fcar (Fcdr (Fcdr (name)));
779 bitmap_id = x_create_bitmap_from_data (f, SSDATA (bits),
780 w, h);
782 else
784 /* It must be a string -- a file name. */
785 bitmap_id = x_create_bitmap_from_file (f, name);
787 unblock_input ();
789 if (bitmap_id < 0)
791 add_to_log ("Invalid or undefined bitmap `%s'", name);
792 bitmap_id = 0;
794 else
796 #ifdef GLYPH_DEBUG
797 ++npixmaps_allocated;
798 #endif
801 return bitmap_id;
804 #endif /* HAVE_WINDOW_SYSTEM */
808 /***********************************************************************
809 X Colors
810 ***********************************************************************/
812 /* Parse RGB_LIST, and fill in the RGB fields of COLOR.
813 RGB_LIST should contain (at least) 3 lisp integers.
814 Return true iff RGB_LIST is OK. */
816 static bool
817 parse_rgb_list (Lisp_Object rgb_list, XColor *color)
819 #define PARSE_RGB_LIST_FIELD(field) \
820 if (CONSP (rgb_list) && INTEGERP (XCAR (rgb_list))) \
822 color->field = XINT (XCAR (rgb_list)); \
823 rgb_list = XCDR (rgb_list); \
825 else \
826 return false;
828 PARSE_RGB_LIST_FIELD (red);
829 PARSE_RGB_LIST_FIELD (green);
830 PARSE_RGB_LIST_FIELD (blue);
832 return true;
836 /* Lookup on frame F the color described by the lisp string COLOR.
837 The resulting tty color is returned in TTY_COLOR; if STD_COLOR is
838 non-zero, then the `standard' definition of the same color is
839 returned in it. */
841 static bool
842 tty_lookup_color (struct frame *f, Lisp_Object color, XColor *tty_color,
843 XColor *std_color)
845 Lisp_Object frame, color_desc;
847 if (!STRINGP (color) || NILP (Ffboundp (Qtty_color_desc)))
848 return false;
850 XSETFRAME (frame, f);
852 color_desc = call2 (Qtty_color_desc, color, frame);
853 if (CONSP (color_desc) && CONSP (XCDR (color_desc)))
855 Lisp_Object rgb;
857 if (! INTEGERP (XCAR (XCDR (color_desc))))
858 return false;
860 tty_color->pixel = XINT (XCAR (XCDR (color_desc)));
862 rgb = XCDR (XCDR (color_desc));
863 if (! parse_rgb_list (rgb, tty_color))
864 return false;
866 /* Should we fill in STD_COLOR too? */
867 if (std_color)
869 /* Default STD_COLOR to the same as TTY_COLOR. */
870 *std_color = *tty_color;
872 /* Do a quick check to see if the returned descriptor is
873 actually _exactly_ equal to COLOR, otherwise we have to
874 lookup STD_COLOR separately. If it's impossible to lookup
875 a standard color, we just give up and use TTY_COLOR. */
876 if ((!STRINGP (XCAR (color_desc))
877 || NILP (Fstring_equal (color, XCAR (color_desc))))
878 && !NILP (Ffboundp (Qtty_color_standard_values)))
880 /* Look up STD_COLOR separately. */
881 rgb = call1 (Qtty_color_standard_values, color);
882 if (! parse_rgb_list (rgb, std_color))
883 return false;
887 return true;
889 else if (NILP (Fsymbol_value (intern ("tty-defined-color-alist"))))
890 /* We were called early during startup, and the colors are not
891 yet set up in tty-defined-color-alist. Don't return a failure
892 indication, since this produces the annoying "Unable to
893 load color" messages in the *Messages* buffer. */
894 return true;
895 else
896 /* tty-color-desc seems to have returned a bad value. */
897 return false;
900 /* A version of defined_color for non-X frames. */
902 static bool
903 tty_defined_color (struct frame *f, const char *color_name,
904 XColor *color_def, bool alloc)
906 bool status = true;
908 /* Defaults. */
909 color_def->pixel = FACE_TTY_DEFAULT_COLOR;
910 color_def->red = 0;
911 color_def->blue = 0;
912 color_def->green = 0;
914 if (*color_name)
915 status = tty_lookup_color (f, build_string (color_name), color_def, NULL);
917 if (color_def->pixel == FACE_TTY_DEFAULT_COLOR && *color_name)
919 if (strcmp (color_name, "unspecified-fg") == 0)
920 color_def->pixel = FACE_TTY_DEFAULT_FG_COLOR;
921 else if (strcmp (color_name, "unspecified-bg") == 0)
922 color_def->pixel = FACE_TTY_DEFAULT_BG_COLOR;
925 if (color_def->pixel != FACE_TTY_DEFAULT_COLOR)
926 status = true;
928 return status;
932 /* Decide if color named COLOR_NAME is valid for the display
933 associated with the frame F; if so, return the rgb values in
934 COLOR_DEF. If ALLOC, allocate a new colormap cell.
936 This does the right thing for any type of frame. */
938 static bool
939 defined_color (struct frame *f, const char *color_name, XColor *color_def,
940 bool alloc)
942 if (!FRAME_WINDOW_P (f))
943 return tty_defined_color (f, color_name, color_def, alloc);
944 #ifdef HAVE_X_WINDOWS
945 else if (FRAME_X_P (f))
946 return x_defined_color (f, color_name, color_def, alloc);
947 #endif
948 #ifdef HAVE_NTGUI
949 else if (FRAME_W32_P (f))
950 return w32_defined_color (f, color_name, color_def, alloc);
951 #endif
952 #ifdef HAVE_NS
953 else if (FRAME_NS_P (f))
954 return ns_defined_color (f, color_name, color_def, alloc, true);
955 #endif
956 else
957 emacs_abort ();
961 /* Given the index IDX of a tty color on frame F, return its name, a
962 Lisp string. */
964 Lisp_Object
965 tty_color_name (struct frame *f, int idx)
967 if (idx >= 0 && !NILP (Ffboundp (Qtty_color_by_index)))
969 Lisp_Object frame;
970 Lisp_Object coldesc;
972 XSETFRAME (frame, f);
973 coldesc = call2 (Qtty_color_by_index, make_number (idx), frame);
975 if (!NILP (coldesc))
976 return XCAR (coldesc);
978 #ifdef MSDOS
979 /* We can have an MS-DOS frame under -nw for a short window of
980 opportunity before internal_terminal_init is called. DTRT. */
981 if (FRAME_MSDOS_P (f) && !inhibit_window_system)
982 return msdos_stdcolor_name (idx);
983 #endif
985 if (idx == FACE_TTY_DEFAULT_FG_COLOR)
986 return build_string (unspecified_fg);
987 if (idx == FACE_TTY_DEFAULT_BG_COLOR)
988 return build_string (unspecified_bg);
990 return Qunspecified;
994 /* Return true if COLOR_NAME is a shade of gray (or white or
995 black) on frame F.
997 The criterion implemented here is not a terribly sophisticated one. */
999 static bool
1000 face_color_gray_p (struct frame *f, const char *color_name)
1002 XColor color;
1003 bool gray_p;
1005 if (defined_color (f, color_name, &color, false))
1006 gray_p = (/* Any color sufficiently close to black counts as gray. */
1007 (color.red < 5000 && color.green < 5000 && color.blue < 5000)
1009 ((eabs (color.red - color.green)
1010 < max (color.red, color.green) / 20)
1011 && (eabs (color.green - color.blue)
1012 < max (color.green, color.blue) / 20)
1013 && (eabs (color.blue - color.red)
1014 < max (color.blue, color.red) / 20)));
1015 else
1016 gray_p = false;
1018 return gray_p;
1022 /* Return true if color COLOR_NAME can be displayed on frame F.
1023 BACKGROUND_P means the color will be used as background color. */
1025 static bool
1026 face_color_supported_p (struct frame *f, const char *color_name,
1027 bool background_p)
1029 Lisp_Object frame;
1030 XColor not_used;
1032 XSETFRAME (frame, f);
1033 return
1034 #ifdef HAVE_WINDOW_SYSTEM
1035 FRAME_WINDOW_P (f)
1036 ? (!NILP (Fxw_display_color_p (frame))
1037 || xstrcasecmp (color_name, "black") == 0
1038 || xstrcasecmp (color_name, "white") == 0
1039 || (background_p
1040 && face_color_gray_p (f, color_name))
1041 || (!NILP (Fx_display_grayscale_p (frame))
1042 && face_color_gray_p (f, color_name)))
1044 #endif
1045 tty_defined_color (f, color_name, &not_used, false);
1049 DEFUN ("color-gray-p", Fcolor_gray_p, Scolor_gray_p, 1, 2, 0,
1050 doc: /* Return non-nil if COLOR is a shade of gray (or white or black).
1051 FRAME specifies the frame and thus the display for interpreting COLOR.
1052 If FRAME is nil or omitted, use the selected frame. */)
1053 (Lisp_Object color, Lisp_Object frame)
1055 CHECK_STRING (color);
1056 return (face_color_gray_p (decode_any_frame (frame), SSDATA (color))
1057 ? Qt : Qnil);
1061 DEFUN ("color-supported-p", Fcolor_supported_p,
1062 Scolor_supported_p, 1, 3, 0,
1063 doc: /* Return non-nil if COLOR can be displayed on FRAME.
1064 BACKGROUND-P non-nil means COLOR is used as a background.
1065 Otherwise, this function tells whether it can be used as a foreground.
1066 If FRAME is nil or omitted, use the selected frame.
1067 COLOR must be a valid color name. */)
1068 (Lisp_Object color, Lisp_Object frame, Lisp_Object background_p)
1070 CHECK_STRING (color);
1071 return (face_color_supported_p (decode_any_frame (frame),
1072 SSDATA (color), !NILP (background_p))
1073 ? Qt : Qnil);
1077 static unsigned long
1078 load_color2 (struct frame *f, struct face *face, Lisp_Object name,
1079 enum lface_attribute_index target_index, XColor *color)
1081 eassert (STRINGP (name));
1082 eassert (target_index == LFACE_FOREGROUND_INDEX
1083 || target_index == LFACE_BACKGROUND_INDEX
1084 || target_index == LFACE_UNDERLINE_INDEX
1085 || target_index == LFACE_OVERLINE_INDEX
1086 || target_index == LFACE_STRIKE_THROUGH_INDEX
1087 || target_index == LFACE_BOX_INDEX);
1089 /* if the color map is full, defined_color will return a best match
1090 to the values in an existing cell. */
1091 if (!defined_color (f, SSDATA (name), color, true))
1093 add_to_log ("Unable to load color \"%s\"", name);
1095 switch (target_index)
1097 case LFACE_FOREGROUND_INDEX:
1098 face->foreground_defaulted_p = true;
1099 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1100 break;
1102 case LFACE_BACKGROUND_INDEX:
1103 face->background_defaulted_p = true;
1104 color->pixel = FRAME_BACKGROUND_PIXEL (f);
1105 break;
1107 case LFACE_UNDERLINE_INDEX:
1108 face->underline_defaulted_p = true;
1109 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1110 break;
1112 case LFACE_OVERLINE_INDEX:
1113 face->overline_color_defaulted_p = true;
1114 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1115 break;
1117 case LFACE_STRIKE_THROUGH_INDEX:
1118 face->strike_through_color_defaulted_p = true;
1119 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1120 break;
1122 case LFACE_BOX_INDEX:
1123 face->box_color_defaulted_p = true;
1124 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1125 break;
1127 default:
1128 emacs_abort ();
1131 #ifdef GLYPH_DEBUG
1132 else
1133 ++ncolors_allocated;
1134 #endif
1136 return color->pixel;
1139 /* Load color with name NAME for use by face FACE on frame F.
1140 TARGET_INDEX must be one of LFACE_FOREGROUND_INDEX,
1141 LFACE_BACKGROUND_INDEX, LFACE_UNDERLINE_INDEX, LFACE_OVERLINE_INDEX,
1142 LFACE_STRIKE_THROUGH_INDEX, or LFACE_BOX_INDEX. Value is the
1143 pixel color. If color cannot be loaded, display a message, and
1144 return the foreground, background or underline color of F, but
1145 record that fact in flags of the face so that we don't try to free
1146 these colors. */
1148 unsigned long
1149 load_color (struct frame *f, struct face *face, Lisp_Object name,
1150 enum lface_attribute_index target_index)
1152 XColor color;
1153 return load_color2 (f, face, name, target_index, &color);
1157 #ifdef HAVE_WINDOW_SYSTEM
1159 #define NEAR_SAME_COLOR_THRESHOLD 30000
1161 /* Load colors for face FACE which is used on frame F. Colors are
1162 specified by slots LFACE_BACKGROUND_INDEX and LFACE_FOREGROUND_INDEX
1163 of ATTRS. If the background color specified is not supported on F,
1164 try to emulate gray colors with a stipple from Vface_default_stipple. */
1166 static void
1167 load_face_colors (struct frame *f, struct face *face,
1168 Lisp_Object attrs[LFACE_VECTOR_SIZE])
1170 Lisp_Object fg, bg, dfg;
1171 XColor xfg, xbg;
1173 bg = attrs[LFACE_BACKGROUND_INDEX];
1174 fg = attrs[LFACE_FOREGROUND_INDEX];
1176 /* Swap colors if face is inverse-video. */
1177 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1179 Lisp_Object tmp;
1180 tmp = fg;
1181 fg = bg;
1182 bg = tmp;
1185 /* Check for support for foreground, not for background because
1186 face_color_supported_p is smart enough to know that grays are
1187 "supported" as background because we are supposed to use stipple
1188 for them. */
1189 if (!face_color_supported_p (f, SSDATA (bg), false)
1190 && !NILP (Fbitmap_spec_p (Vface_default_stipple)))
1192 x_destroy_bitmap (f, face->stipple);
1193 face->stipple = load_pixmap (f, Vface_default_stipple);
1196 face->background = load_color2 (f, face, bg, LFACE_BACKGROUND_INDEX, &xbg);
1197 face->foreground = load_color2 (f, face, fg, LFACE_FOREGROUND_INDEX, &xfg);
1199 dfg = attrs[LFACE_DISTANT_FOREGROUND_INDEX];
1200 if (!NILP (dfg) && !UNSPECIFIEDP (dfg)
1201 && color_distance (&xbg, &xfg) < NEAR_SAME_COLOR_THRESHOLD)
1203 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1204 face->background = load_color (f, face, dfg, LFACE_BACKGROUND_INDEX);
1205 else
1206 face->foreground = load_color (f, face, dfg, LFACE_FOREGROUND_INDEX);
1210 #ifdef HAVE_X_WINDOWS
1212 /* Free color PIXEL on frame F. */
1214 void
1215 unload_color (struct frame *f, unsigned long pixel)
1217 if (pixel != -1)
1219 block_input ();
1220 x_free_colors (f, &pixel, 1);
1221 unblock_input ();
1225 /* Free colors allocated for FACE. */
1227 static void
1228 free_face_colors (struct frame *f, struct face *face)
1230 /* PENDING(NS): need to do something here? */
1232 if (face->colors_copied_bitwise_p)
1233 return;
1235 block_input ();
1237 if (!face->foreground_defaulted_p)
1239 x_free_colors (f, &face->foreground, 1);
1240 IF_DEBUG (--ncolors_allocated);
1243 if (!face->background_defaulted_p)
1245 x_free_colors (f, &face->background, 1);
1246 IF_DEBUG (--ncolors_allocated);
1249 if (face->underline_p
1250 && !face->underline_defaulted_p)
1252 x_free_colors (f, &face->underline_color, 1);
1253 IF_DEBUG (--ncolors_allocated);
1256 if (face->overline_p
1257 && !face->overline_color_defaulted_p)
1259 x_free_colors (f, &face->overline_color, 1);
1260 IF_DEBUG (--ncolors_allocated);
1263 if (face->strike_through_p
1264 && !face->strike_through_color_defaulted_p)
1266 x_free_colors (f, &face->strike_through_color, 1);
1267 IF_DEBUG (--ncolors_allocated);
1270 if (face->box != FACE_NO_BOX
1271 && !face->box_color_defaulted_p)
1273 x_free_colors (f, &face->box_color, 1);
1274 IF_DEBUG (--ncolors_allocated);
1277 unblock_input ();
1280 #endif /* HAVE_X_WINDOWS */
1282 #endif /* HAVE_WINDOW_SYSTEM */
1286 /***********************************************************************
1287 XLFD Font Names
1288 ***********************************************************************/
1290 /* An enumerator for each field of an XLFD font name. */
1292 enum xlfd_field
1294 XLFD_FOUNDRY,
1295 XLFD_FAMILY,
1296 XLFD_WEIGHT,
1297 XLFD_SLANT,
1298 XLFD_SWIDTH,
1299 XLFD_ADSTYLE,
1300 XLFD_PIXEL_SIZE,
1301 XLFD_POINT_SIZE,
1302 XLFD_RESX,
1303 XLFD_RESY,
1304 XLFD_SPACING,
1305 XLFD_AVGWIDTH,
1306 XLFD_REGISTRY,
1307 XLFD_ENCODING,
1308 XLFD_LAST
1311 /* An enumerator for each possible slant value of a font. Taken from
1312 the XLFD specification. */
1314 enum xlfd_slant
1316 XLFD_SLANT_UNKNOWN,
1317 XLFD_SLANT_ROMAN,
1318 XLFD_SLANT_ITALIC,
1319 XLFD_SLANT_OBLIQUE,
1320 XLFD_SLANT_REVERSE_ITALIC,
1321 XLFD_SLANT_REVERSE_OBLIQUE,
1322 XLFD_SLANT_OTHER
1325 /* Relative font weight according to XLFD documentation. */
1327 enum xlfd_weight
1329 XLFD_WEIGHT_UNKNOWN,
1330 XLFD_WEIGHT_ULTRA_LIGHT, /* 10 */
1331 XLFD_WEIGHT_EXTRA_LIGHT, /* 20 */
1332 XLFD_WEIGHT_LIGHT, /* 30 */
1333 XLFD_WEIGHT_SEMI_LIGHT, /* 40: SemiLight, Book, ... */
1334 XLFD_WEIGHT_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1335 XLFD_WEIGHT_SEMI_BOLD, /* 60: SemiBold, DemiBold, ... */
1336 XLFD_WEIGHT_BOLD, /* 70: Bold, ... */
1337 XLFD_WEIGHT_EXTRA_BOLD, /* 80: ExtraBold, Heavy, ... */
1338 XLFD_WEIGHT_ULTRA_BOLD /* 90: UltraBold, Black, ... */
1341 /* Relative proportionate width. */
1343 enum xlfd_swidth
1345 XLFD_SWIDTH_UNKNOWN,
1346 XLFD_SWIDTH_ULTRA_CONDENSED, /* 10 */
1347 XLFD_SWIDTH_EXTRA_CONDENSED, /* 20 */
1348 XLFD_SWIDTH_CONDENSED, /* 30: Condensed, Narrow, Compressed, ... */
1349 XLFD_SWIDTH_SEMI_CONDENSED, /* 40: semicondensed */
1350 XLFD_SWIDTH_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1351 XLFD_SWIDTH_SEMI_EXPANDED, /* 60: SemiExpanded, DemiExpanded, ... */
1352 XLFD_SWIDTH_EXPANDED, /* 70: Expanded... */
1353 XLFD_SWIDTH_EXTRA_EXPANDED, /* 80: ExtraExpanded, Wide... */
1354 XLFD_SWIDTH_ULTRA_EXPANDED /* 90: UltraExpanded... */
1357 /* Order by which font selection chooses fonts. The default values
1358 mean `first, find a best match for the font width, then for the
1359 font height, then for weight, then for slant.' This variable can be
1360 set via set-face-font-sort-order. */
1362 static int font_sort_order[4];
1364 #ifdef HAVE_WINDOW_SYSTEM
1366 static enum font_property_index font_props_for_sorting[FONT_SIZE_INDEX];
1368 static int
1369 compare_fonts_by_sort_order (const void *v1, const void *v2)
1371 Lisp_Object const *p1 = v1;
1372 Lisp_Object const *p2 = v2;
1373 Lisp_Object font1 = *p1;
1374 Lisp_Object font2 = *p2;
1375 int i;
1377 for (i = 0; i < FONT_SIZE_INDEX; i++)
1379 enum font_property_index idx = font_props_for_sorting[i];
1380 Lisp_Object val1 = AREF (font1, idx), val2 = AREF (font2, idx);
1381 int result;
1383 if (idx <= FONT_REGISTRY_INDEX)
1385 if (STRINGP (val1))
1386 result = STRINGP (val2) ? strcmp (SSDATA (val1), SSDATA (val2)) : -1;
1387 else
1388 result = STRINGP (val2) ? 1 : 0;
1390 else
1392 if (INTEGERP (val1))
1393 result = (INTEGERP (val2) && XINT (val1) >= XINT (val2)
1394 ? XINT (val1) > XINT (val2)
1395 : -1);
1396 else
1397 result = INTEGERP (val2) ? 1 : 0;
1399 if (result)
1400 return result;
1402 return 0;
1405 DEFUN ("x-family-fonts", Fx_family_fonts, Sx_family_fonts, 0, 2, 0,
1406 doc: /* Return a list of available fonts of family FAMILY on FRAME.
1407 If FAMILY is omitted or nil, list all families.
1408 Otherwise, FAMILY must be a string, possibly containing wildcards
1409 `?' and `*'.
1410 If FRAME is omitted or nil, use the selected frame.
1411 Each element of the result is a vector [FAMILY WIDTH POINT-SIZE WEIGHT
1412 SLANT FIXED-P FULL REGISTRY-AND-ENCODING].
1413 FAMILY is the font family name. POINT-SIZE is the size of the
1414 font in 1/10 pt. WIDTH, WEIGHT, and SLANT are symbols describing the
1415 width, weight and slant of the font. These symbols are the same as for
1416 face attributes. FIXED-P is non-nil if the font is fixed-pitch.
1417 FULL is the full name of the font, and REGISTRY-AND-ENCODING is a string
1418 giving the registry and encoding of the font.
1419 The result list is sorted according to the current setting of
1420 the face font sort order. */)
1421 (Lisp_Object family, Lisp_Object frame)
1423 Lisp_Object font_spec, list, *drivers, vec;
1424 struct frame *f = decode_live_frame (frame);
1425 ptrdiff_t i, nfonts;
1426 EMACS_INT ndrivers;
1427 Lisp_Object result;
1428 USE_SAFE_ALLOCA;
1430 font_spec = Ffont_spec (0, NULL);
1431 if (!NILP (family))
1433 CHECK_STRING (family);
1434 font_parse_family_registry (family, Qnil, font_spec);
1437 list = font_list_entities (f, font_spec);
1438 if (NILP (list))
1439 return Qnil;
1441 /* Sort the font entities. */
1442 for (i = 0; i < 4; i++)
1443 switch (font_sort_order[i])
1445 case XLFD_SWIDTH:
1446 font_props_for_sorting[i] = FONT_WIDTH_INDEX; break;
1447 case XLFD_POINT_SIZE:
1448 font_props_for_sorting[i] = FONT_SIZE_INDEX; break;
1449 case XLFD_WEIGHT:
1450 font_props_for_sorting[i] = FONT_WEIGHT_INDEX; break;
1451 default:
1452 font_props_for_sorting[i] = FONT_SLANT_INDEX; break;
1454 font_props_for_sorting[i++] = FONT_FAMILY_INDEX;
1455 font_props_for_sorting[i++] = FONT_FOUNDRY_INDEX;
1456 font_props_for_sorting[i++] = FONT_ADSTYLE_INDEX;
1457 font_props_for_sorting[i++] = FONT_REGISTRY_INDEX;
1459 ndrivers = XINT (Flength (list));
1460 SAFE_ALLOCA_LISP (drivers, ndrivers);
1461 for (i = 0; i < ndrivers; i++, list = XCDR (list))
1462 drivers[i] = XCAR (list);
1463 vec = Fvconcat (ndrivers, drivers);
1464 nfonts = ASIZE (vec);
1466 qsort (XVECTOR (vec)->contents, nfonts, word_size,
1467 compare_fonts_by_sort_order);
1469 result = Qnil;
1470 for (i = nfonts - 1; i >= 0; --i)
1472 Lisp_Object font = AREF (vec, i);
1473 Lisp_Object v = make_uninit_vector (8);
1474 int point;
1475 Lisp_Object spacing;
1477 ASET (v, 0, AREF (font, FONT_FAMILY_INDEX));
1478 ASET (v, 1, FONT_WIDTH_SYMBOLIC (font));
1479 point = PIXEL_TO_POINT (XINT (AREF (font, FONT_SIZE_INDEX)) * 10,
1480 FRAME_RES_Y (f));
1481 ASET (v, 2, make_number (point));
1482 ASET (v, 3, FONT_WEIGHT_SYMBOLIC (font));
1483 ASET (v, 4, FONT_SLANT_SYMBOLIC (font));
1484 spacing = Ffont_get (font, QCspacing);
1485 ASET (v, 5, (NILP (spacing) || EQ (spacing, Qp)) ? Qnil : Qt);
1486 ASET (v, 6, Ffont_xlfd_name (font, Qnil));
1487 ASET (v, 7, AREF (font, FONT_REGISTRY_INDEX));
1489 result = Fcons (v, result);
1492 SAFE_FREE ();
1493 return result;
1496 DEFUN ("x-list-fonts", Fx_list_fonts, Sx_list_fonts, 1, 5, 0,
1497 doc: /* Return a list of the names of available fonts matching PATTERN.
1498 If optional arguments FACE and FRAME are specified, return only fonts
1499 the same size as FACE on FRAME.
1501 PATTERN should be a string containing a font name in the XLFD,
1502 Fontconfig, or GTK format. A font name given in the XLFD format may
1503 contain wildcard characters:
1504 the * character matches any substring, and
1505 the ? character matches any single character.
1506 PATTERN is case-insensitive.
1508 The return value is a list of strings, suitable as arguments to
1509 `set-face-font'.
1511 Fonts Emacs can't use may or may not be excluded
1512 even if they match PATTERN and FACE.
1513 The optional fourth argument MAXIMUM sets a limit on how many
1514 fonts to match. The first MAXIMUM fonts are reported.
1515 The optional fifth argument WIDTH, if specified, is a number of columns
1516 occupied by a character of a font. In that case, return only fonts
1517 the WIDTH times as wide as FACE on FRAME. */)
1518 (Lisp_Object pattern, Lisp_Object face, Lisp_Object frame,
1519 Lisp_Object maximum, Lisp_Object width)
1521 struct frame *f;
1522 int size, avgwidth;
1524 check_window_system (NULL);
1525 CHECK_STRING (pattern);
1527 if (! NILP (maximum))
1528 CHECK_NATNUM (maximum);
1530 if (!NILP (width))
1531 CHECK_NUMBER (width);
1533 /* We can't simply call decode_window_system_frame because
1534 this function may be called before any frame is created. */
1535 f = decode_live_frame (frame);
1536 if (! FRAME_WINDOW_P (f))
1538 /* Perhaps we have not yet created any frame. */
1539 f = NULL;
1540 frame = Qnil;
1541 face = Qnil;
1543 else
1544 XSETFRAME (frame, f);
1546 /* Determine the width standard for comparison with the fonts we find. */
1548 if (NILP (face))
1549 size = 0;
1550 else
1552 /* This is of limited utility since it works with character
1553 widths. Keep it for compatibility. --gerd. */
1554 int face_id = lookup_named_face (f, face, false);
1555 struct face *width_face = FACE_FROM_ID_OR_NULL (f, face_id);
1557 if (width_face && width_face->font)
1559 size = width_face->font->pixel_size;
1560 avgwidth = width_face->font->average_width;
1562 else
1564 size = FRAME_FONT (f)->pixel_size;
1565 avgwidth = FRAME_FONT (f)->average_width;
1567 if (!NILP (width))
1568 avgwidth *= XINT (width);
1571 Lisp_Object font_spec = font_spec_from_name (pattern);
1572 if (!FONTP (font_spec))
1573 signal_error ("Invalid font name", pattern);
1575 if (size)
1577 Ffont_put (font_spec, QCsize, make_number (size));
1578 Ffont_put (font_spec, QCavgwidth, make_number (avgwidth));
1580 Lisp_Object fonts = Flist_fonts (font_spec, frame, maximum, font_spec);
1581 for (Lisp_Object tail = fonts; CONSP (tail); tail = XCDR (tail))
1583 Lisp_Object font_entity;
1585 font_entity = XCAR (tail);
1586 if ((NILP (AREF (font_entity, FONT_SIZE_INDEX))
1587 || XINT (AREF (font_entity, FONT_SIZE_INDEX)) == 0)
1588 && ! NILP (AREF (font_spec, FONT_SIZE_INDEX)))
1590 /* This is a scalable font. For backward compatibility,
1591 we set the specified size. */
1592 font_entity = copy_font_spec (font_entity);
1593 ASET (font_entity, FONT_SIZE_INDEX,
1594 AREF (font_spec, FONT_SIZE_INDEX));
1596 XSETCAR (tail, Ffont_xlfd_name (font_entity, Qnil));
1598 if (NILP (frame))
1599 /* We don't have to check fontsets. */
1600 return fonts;
1601 Lisp_Object fontsets = list_fontsets (f, pattern, size);
1602 return CALLN (Fnconc, fonts, fontsets);
1605 #endif /* HAVE_WINDOW_SYSTEM */
1608 /***********************************************************************
1609 Lisp Faces
1610 ***********************************************************************/
1612 /* Access face attributes of face LFACE, a Lisp vector. */
1614 #define LFACE_FAMILY(LFACE) AREF ((LFACE), LFACE_FAMILY_INDEX)
1615 #define LFACE_FOUNDRY(LFACE) AREF ((LFACE), LFACE_FOUNDRY_INDEX)
1616 #define LFACE_HEIGHT(LFACE) AREF ((LFACE), LFACE_HEIGHT_INDEX)
1617 #define LFACE_WEIGHT(LFACE) AREF ((LFACE), LFACE_WEIGHT_INDEX)
1618 #define LFACE_SLANT(LFACE) AREF ((LFACE), LFACE_SLANT_INDEX)
1619 #define LFACE_UNDERLINE(LFACE) AREF ((LFACE), LFACE_UNDERLINE_INDEX)
1620 #define LFACE_INVERSE(LFACE) AREF ((LFACE), LFACE_INVERSE_INDEX)
1621 #define LFACE_FOREGROUND(LFACE) AREF ((LFACE), LFACE_FOREGROUND_INDEX)
1622 #define LFACE_BACKGROUND(LFACE) AREF ((LFACE), LFACE_BACKGROUND_INDEX)
1623 #define LFACE_STIPPLE(LFACE) AREF ((LFACE), LFACE_STIPPLE_INDEX)
1624 #define LFACE_SWIDTH(LFACE) AREF ((LFACE), LFACE_SWIDTH_INDEX)
1625 #define LFACE_OVERLINE(LFACE) AREF ((LFACE), LFACE_OVERLINE_INDEX)
1626 #define LFACE_STRIKE_THROUGH(LFACE) AREF ((LFACE), LFACE_STRIKE_THROUGH_INDEX)
1627 #define LFACE_BOX(LFACE) AREF ((LFACE), LFACE_BOX_INDEX)
1628 #define LFACE_FONT(LFACE) AREF ((LFACE), LFACE_FONT_INDEX)
1629 #define LFACE_INHERIT(LFACE) AREF ((LFACE), LFACE_INHERIT_INDEX)
1630 #define LFACE_FONTSET(LFACE) AREF ((LFACE), LFACE_FONTSET_INDEX)
1631 #define LFACE_DISTANT_FOREGROUND(LFACE) \
1632 AREF ((LFACE), LFACE_DISTANT_FOREGROUND_INDEX)
1634 /* True if LFACE is a Lisp face. A Lisp face is a vector of size
1635 LFACE_VECTOR_SIZE which has the symbol `face' in slot 0. */
1637 #define LFACEP(LFACE) \
1638 (VECTORP (LFACE) \
1639 && ASIZE (LFACE) == LFACE_VECTOR_SIZE \
1640 && EQ (AREF (LFACE, 0), Qface))
1643 #ifdef GLYPH_DEBUG
1645 /* Check consistency of Lisp face attribute vector ATTRS. */
1647 static void
1648 check_lface_attrs (Lisp_Object attrs[LFACE_VECTOR_SIZE])
1650 eassert (UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
1651 || IGNORE_DEFFACE_P (attrs[LFACE_FAMILY_INDEX])
1652 || STRINGP (attrs[LFACE_FAMILY_INDEX]));
1653 eassert (UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
1654 || IGNORE_DEFFACE_P (attrs[LFACE_FOUNDRY_INDEX])
1655 || STRINGP (attrs[LFACE_FOUNDRY_INDEX]));
1656 eassert (UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
1657 || IGNORE_DEFFACE_P (attrs[LFACE_SWIDTH_INDEX])
1658 || SYMBOLP (attrs[LFACE_SWIDTH_INDEX]));
1659 eassert (UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
1660 || IGNORE_DEFFACE_P (attrs[LFACE_HEIGHT_INDEX])
1661 || NUMBERP (attrs[LFACE_HEIGHT_INDEX])
1662 || FUNCTIONP (attrs[LFACE_HEIGHT_INDEX]));
1663 eassert (UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
1664 || IGNORE_DEFFACE_P (attrs[LFACE_WEIGHT_INDEX])
1665 || SYMBOLP (attrs[LFACE_WEIGHT_INDEX]));
1666 eassert (UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
1667 || IGNORE_DEFFACE_P (attrs[LFACE_SLANT_INDEX])
1668 || SYMBOLP (attrs[LFACE_SLANT_INDEX]));
1669 eassert (UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
1670 || IGNORE_DEFFACE_P (attrs[LFACE_UNDERLINE_INDEX])
1671 || SYMBOLP (attrs[LFACE_UNDERLINE_INDEX])
1672 || STRINGP (attrs[LFACE_UNDERLINE_INDEX])
1673 || CONSP (attrs[LFACE_UNDERLINE_INDEX]));
1674 eassert (UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
1675 || IGNORE_DEFFACE_P (attrs[LFACE_OVERLINE_INDEX])
1676 || SYMBOLP (attrs[LFACE_OVERLINE_INDEX])
1677 || STRINGP (attrs[LFACE_OVERLINE_INDEX]));
1678 eassert (UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1679 || IGNORE_DEFFACE_P (attrs[LFACE_STRIKE_THROUGH_INDEX])
1680 || SYMBOLP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1681 || STRINGP (attrs[LFACE_STRIKE_THROUGH_INDEX]));
1682 eassert (UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
1683 || IGNORE_DEFFACE_P (attrs[LFACE_BOX_INDEX])
1684 || SYMBOLP (attrs[LFACE_BOX_INDEX])
1685 || STRINGP (attrs[LFACE_BOX_INDEX])
1686 || INTEGERP (attrs[LFACE_BOX_INDEX])
1687 || CONSP (attrs[LFACE_BOX_INDEX]));
1688 eassert (UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
1689 || IGNORE_DEFFACE_P (attrs[LFACE_INVERSE_INDEX])
1690 || SYMBOLP (attrs[LFACE_INVERSE_INDEX]));
1691 eassert (UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
1692 || IGNORE_DEFFACE_P (attrs[LFACE_FOREGROUND_INDEX])
1693 || STRINGP (attrs[LFACE_FOREGROUND_INDEX]));
1694 eassert (UNSPECIFIEDP (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
1695 || IGNORE_DEFFACE_P (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
1696 || STRINGP (attrs[LFACE_DISTANT_FOREGROUND_INDEX]));
1697 eassert (UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
1698 || IGNORE_DEFFACE_P (attrs[LFACE_BACKGROUND_INDEX])
1699 || STRINGP (attrs[LFACE_BACKGROUND_INDEX]));
1700 eassert (UNSPECIFIEDP (attrs[LFACE_INHERIT_INDEX])
1701 || IGNORE_DEFFACE_P (attrs[LFACE_INHERIT_INDEX])
1702 || NILP (attrs[LFACE_INHERIT_INDEX])
1703 || SYMBOLP (attrs[LFACE_INHERIT_INDEX])
1704 || CONSP (attrs[LFACE_INHERIT_INDEX]));
1705 #ifdef HAVE_WINDOW_SYSTEM
1706 eassert (UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
1707 || IGNORE_DEFFACE_P (attrs[LFACE_STIPPLE_INDEX])
1708 || SYMBOLP (attrs[LFACE_STIPPLE_INDEX])
1709 || !NILP (Fbitmap_spec_p (attrs[LFACE_STIPPLE_INDEX])));
1710 eassert (UNSPECIFIEDP (attrs[LFACE_FONT_INDEX])
1711 || IGNORE_DEFFACE_P (attrs[LFACE_FONT_INDEX])
1712 || FONTP (attrs[LFACE_FONT_INDEX]));
1713 eassert (UNSPECIFIEDP (attrs[LFACE_FONTSET_INDEX])
1714 || STRINGP (attrs[LFACE_FONTSET_INDEX])
1715 || NILP (attrs[LFACE_FONTSET_INDEX]));
1716 #endif
1720 /* Check consistency of attributes of Lisp face LFACE (a Lisp vector). */
1722 static void
1723 check_lface (Lisp_Object lface)
1725 if (!NILP (lface))
1727 eassert (LFACEP (lface));
1728 check_lface_attrs (XVECTOR (lface)->contents);
1732 #else /* not GLYPH_DEBUG */
1734 #define check_lface_attrs(attrs) (void) 0
1735 #define check_lface(lface) (void) 0
1737 #endif /* GLYPH_DEBUG */
1741 /* Face-merge cycle checking. */
1743 enum named_merge_point_kind
1745 NAMED_MERGE_POINT_NORMAL,
1746 NAMED_MERGE_POINT_REMAP
1749 /* A `named merge point' is simply a point during face-merging where we
1750 look up a face by name. We keep a stack of which named lookups we're
1751 currently processing so that we can easily detect cycles, using a
1752 linked- list of struct named_merge_point structures, typically
1753 allocated on the stack frame of the named lookup functions which are
1754 active (so no consing is required). */
1755 struct named_merge_point
1757 Lisp_Object face_name;
1758 enum named_merge_point_kind named_merge_point_kind;
1759 struct named_merge_point *prev;
1763 /* If a face merging cycle is detected for FACE_NAME, return false,
1764 otherwise add NEW_NAMED_MERGE_POINT, which is initialized using
1765 FACE_NAME and NAMED_MERGE_POINT_KIND, as the head of the linked list
1766 pointed to by NAMED_MERGE_POINTS, and return true. */
1768 static bool
1769 push_named_merge_point (struct named_merge_point *new_named_merge_point,
1770 Lisp_Object face_name,
1771 enum named_merge_point_kind named_merge_point_kind,
1772 struct named_merge_point **named_merge_points)
1774 struct named_merge_point *prev;
1776 for (prev = *named_merge_points; prev; prev = prev->prev)
1777 if (EQ (face_name, prev->face_name))
1779 if (prev->named_merge_point_kind == named_merge_point_kind)
1780 /* A cycle, so fail. */
1781 return false;
1782 else if (prev->named_merge_point_kind == NAMED_MERGE_POINT_REMAP)
1783 /* A remap `hides ' any previous normal merge points
1784 (because the remap means that it's actually different face),
1785 so as we know the current merge point must be normal, we
1786 can just assume it's OK. */
1787 break;
1790 new_named_merge_point->face_name = face_name;
1791 new_named_merge_point->named_merge_point_kind = named_merge_point_kind;
1792 new_named_merge_point->prev = *named_merge_points;
1794 *named_merge_points = new_named_merge_point;
1796 return true;
1800 /* Resolve face name FACE_NAME. If FACE_NAME is a string, intern it
1801 to make it a symbol. If FACE_NAME is an alias for another face,
1802 return that face's name.
1804 Return default face in case of errors. */
1806 static Lisp_Object
1807 resolve_face_name (Lisp_Object face_name, bool signal_p)
1809 Lisp_Object orig_face;
1810 Lisp_Object tortoise, hare;
1812 if (STRINGP (face_name))
1813 face_name = Fintern (face_name, Qnil);
1815 if (NILP (face_name) || !SYMBOLP (face_name))
1816 return face_name;
1818 orig_face = face_name;
1819 tortoise = hare = face_name;
1821 while (true)
1823 face_name = hare;
1824 hare = Fget (hare, Qface_alias);
1825 if (NILP (hare) || !SYMBOLP (hare))
1826 break;
1828 face_name = hare;
1829 hare = Fget (hare, Qface_alias);
1830 if (NILP (hare) || !SYMBOLP (hare))
1831 break;
1833 tortoise = Fget (tortoise, Qface_alias);
1834 if (EQ (hare, tortoise))
1836 if (signal_p)
1837 xsignal1 (Qcircular_list, orig_face);
1838 return Qdefault;
1842 return face_name;
1846 /* Return the face definition of FACE_NAME on frame F. F null means
1847 return the definition for new frames. FACE_NAME may be a string or
1848 a symbol (apparently Emacs 20.2 allowed strings as face names in
1849 face text properties; Ediff uses that).
1850 If SIGNAL_P, signal an error if FACE_NAME is not a valid face name.
1851 Otherwise, value is nil if FACE_NAME is not a valid face name. */
1852 static Lisp_Object
1853 lface_from_face_name_no_resolve (struct frame *f, Lisp_Object face_name,
1854 bool signal_p)
1856 Lisp_Object lface;
1858 if (f)
1859 lface = assq_no_quit (face_name, f->face_alist);
1860 else
1861 lface = assq_no_quit (face_name, Vface_new_frame_defaults);
1863 if (CONSP (lface))
1864 lface = XCDR (lface);
1865 else if (signal_p)
1866 signal_error ("Invalid face", face_name);
1868 check_lface (lface);
1870 return lface;
1873 /* Return the face definition of FACE_NAME on frame F. F null means
1874 return the definition for new frames. FACE_NAME may be a string or
1875 a symbol (apparently Emacs 20.2 allowed strings as face names in
1876 face text properties; Ediff uses that). If FACE_NAME is an alias
1877 for another face, return that face's definition.
1878 If SIGNAL_P, signal an error if FACE_NAME is not a valid face name.
1879 Otherwise, value is nil if FACE_NAME is not a valid face name. */
1880 static Lisp_Object
1881 lface_from_face_name (struct frame *f, Lisp_Object face_name, bool signal_p)
1883 face_name = resolve_face_name (face_name, signal_p);
1884 return lface_from_face_name_no_resolve (f, face_name, signal_p);
1888 /* Get face attributes of face FACE_NAME from frame-local faces on
1889 frame F. Store the resulting attributes in ATTRS which must point
1890 to a vector of Lisp_Objects of size LFACE_VECTOR_SIZE.
1891 If SIGNAL_P, signal an error if FACE_NAME does not name a face.
1892 Otherwise, return true iff FACE_NAME is a face. */
1894 static bool
1895 get_lface_attributes_no_remap (struct frame *f, Lisp_Object face_name,
1896 Lisp_Object attrs[LFACE_VECTOR_SIZE],
1897 bool signal_p)
1899 Lisp_Object lface;
1901 lface = lface_from_face_name_no_resolve (f, face_name, signal_p);
1903 if (! NILP (lface))
1904 memcpy (attrs, XVECTOR (lface)->contents,
1905 LFACE_VECTOR_SIZE * sizeof *attrs);
1907 return !NILP (lface);
1910 /* Get face attributes of face FACE_NAME from frame-local faces on frame
1911 F. Store the resulting attributes in ATTRS which must point to a
1912 vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If FACE_NAME is an
1913 alias for another face, use that face's definition.
1914 If SIGNAL_P, signal an error if FACE_NAME does not name a face.
1915 Otherwise, return true iff FACE_NAME is a face. */
1917 static bool
1918 get_lface_attributes (struct frame *f, Lisp_Object face_name,
1919 Lisp_Object attrs[LFACE_VECTOR_SIZE], bool signal_p,
1920 struct named_merge_point *named_merge_points)
1922 Lisp_Object face_remapping;
1924 face_name = resolve_face_name (face_name, signal_p);
1926 /* See if SYMBOL has been remapped to some other face (usually this
1927 is done buffer-locally). */
1928 face_remapping = assq_no_quit (face_name, Vface_remapping_alist);
1929 if (CONSP (face_remapping))
1931 struct named_merge_point named_merge_point;
1933 if (push_named_merge_point (&named_merge_point,
1934 face_name, NAMED_MERGE_POINT_REMAP,
1935 &named_merge_points))
1937 int i;
1939 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
1940 attrs[i] = Qunspecified;
1942 return merge_face_ref (f, XCDR (face_remapping), attrs,
1943 signal_p, named_merge_points);
1947 /* Default case, no remapping. */
1948 return get_lface_attributes_no_remap (f, face_name, attrs, signal_p);
1952 /* True iff all attributes in face attribute vector ATTRS are
1953 specified, i.e. are non-nil. */
1955 static bool
1956 lface_fully_specified_p (Lisp_Object attrs[LFACE_VECTOR_SIZE])
1958 int i;
1960 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
1961 if (i != LFACE_FONT_INDEX && i != LFACE_INHERIT_INDEX
1962 && i != LFACE_DISTANT_FOREGROUND_INDEX)
1963 if ((UNSPECIFIEDP (attrs[i]) || IGNORE_DEFFACE_P (attrs[i])))
1964 break;
1966 return i == LFACE_VECTOR_SIZE;
1969 #ifdef HAVE_WINDOW_SYSTEM
1971 /* Set font-related attributes of Lisp face LFACE from FONT-OBJECT.
1972 If FORCE_P, set only unspecified attributes of LFACE. The
1973 exception is `font' attribute. It is set to FONT_OBJECT regardless
1974 of FORCE_P. */
1976 static void
1977 set_lface_from_font (struct frame *f, Lisp_Object lface,
1978 Lisp_Object font_object, bool force_p)
1980 Lisp_Object val;
1981 struct font *font = XFONT_OBJECT (font_object);
1983 /* Set attributes only if unspecified, otherwise face defaults for
1984 new frames would never take effect. If the font doesn't have a
1985 specific property, set a normal value for that. */
1987 if (force_p || UNSPECIFIEDP (LFACE_FAMILY (lface)))
1989 Lisp_Object family = AREF (font_object, FONT_FAMILY_INDEX);
1991 ASET (lface, LFACE_FAMILY_INDEX, SYMBOL_NAME (family));
1994 if (force_p || UNSPECIFIEDP (LFACE_FOUNDRY (lface)))
1996 Lisp_Object foundry = AREF (font_object, FONT_FOUNDRY_INDEX);
1998 ASET (lface, LFACE_FOUNDRY_INDEX, SYMBOL_NAME (foundry));
2001 if (force_p || UNSPECIFIEDP (LFACE_HEIGHT (lface)))
2003 int pt = PIXEL_TO_POINT (font->pixel_size * 10, FRAME_RES_Y (f));
2005 eassert (pt > 0);
2006 ASET (lface, LFACE_HEIGHT_INDEX, make_number (pt));
2009 if (force_p || UNSPECIFIEDP (LFACE_WEIGHT (lface)))
2011 val = FONT_WEIGHT_FOR_FACE (font_object);
2012 ASET (lface, LFACE_WEIGHT_INDEX, ! NILP (val) ? val :Qnormal);
2014 if (force_p || UNSPECIFIEDP (LFACE_SLANT (lface)))
2016 val = FONT_SLANT_FOR_FACE (font_object);
2017 ASET (lface, LFACE_SLANT_INDEX, ! NILP (val) ? val : Qnormal);
2019 if (force_p || UNSPECIFIEDP (LFACE_SWIDTH (lface)))
2021 val = FONT_WIDTH_FOR_FACE (font_object);
2022 ASET (lface, LFACE_SWIDTH_INDEX, ! NILP (val) ? val : Qnormal);
2025 ASET (lface, LFACE_FONT_INDEX, font_object);
2028 #endif /* HAVE_WINDOW_SYSTEM */
2031 /* Merges the face height FROM with the face height TO, and returns the
2032 merged height. If FROM is an invalid height, then INVALID is
2033 returned instead. FROM and TO may be either absolute face heights or
2034 `relative' heights; the returned value is always an absolute height
2035 unless both FROM and TO are relative. */
2037 static Lisp_Object
2038 merge_face_heights (Lisp_Object from, Lisp_Object to, Lisp_Object invalid)
2040 Lisp_Object result = invalid;
2042 if (INTEGERP (from))
2043 /* FROM is absolute, just use it as is. */
2044 result = from;
2045 else if (FLOATP (from))
2046 /* FROM is a scale, use it to adjust TO. */
2048 if (INTEGERP (to))
2049 /* relative X absolute => absolute */
2050 result = make_number (XFLOAT_DATA (from) * XINT (to));
2051 else if (FLOATP (to))
2052 /* relative X relative => relative */
2053 result = make_float (XFLOAT_DATA (from) * XFLOAT_DATA (to));
2054 else if (UNSPECIFIEDP (to))
2055 result = from;
2057 else if (FUNCTIONP (from))
2058 /* FROM is a function, which use to adjust TO. */
2060 /* Call function with current height as argument.
2061 From is the new height. */
2062 result = safe_call1 (from, to);
2064 /* Ensure that if TO was absolute, so is the result. */
2065 if (INTEGERP (to) && !INTEGERP (result))
2066 result = invalid;
2069 return result;
2073 /* Merge two Lisp face attribute vectors on frame F, FROM and TO, and
2074 store the resulting attributes in TO, which must be already be
2075 completely specified and contain only absolute attributes. Every
2076 specified attribute of FROM overrides the corresponding attribute of
2077 TO; relative attributes in FROM are merged with the absolute value in
2078 TO and replace it. NAMED_MERGE_POINTS is used internally to detect
2079 loops in face inheritance/remapping; it should be 0 when called from
2080 other places. */
2082 static void
2083 merge_face_vectors (struct frame *f, Lisp_Object *from, Lisp_Object *to,
2084 struct named_merge_point *named_merge_points)
2086 int i;
2087 Lisp_Object font = Qnil;
2089 /* If FROM inherits from some other faces, merge their attributes into
2090 TO before merging FROM's direct attributes. Note that an :inherit
2091 attribute of `unspecified' is the same as one of nil; we never
2092 merge :inherit attributes, so nil is more correct, but lots of
2093 other code uses `unspecified' as a generic value for face attributes. */
2094 if (!UNSPECIFIEDP (from[LFACE_INHERIT_INDEX])
2095 && !NILP (from[LFACE_INHERIT_INDEX]))
2096 merge_face_ref (f, from[LFACE_INHERIT_INDEX], to, false, named_merge_points);
2098 if (FONT_SPEC_P (from[LFACE_FONT_INDEX]))
2100 if (!UNSPECIFIEDP (to[LFACE_FONT_INDEX]))
2101 font = merge_font_spec (from[LFACE_FONT_INDEX], to[LFACE_FONT_INDEX]);
2102 else
2103 font = copy_font_spec (from[LFACE_FONT_INDEX]);
2104 to[LFACE_FONT_INDEX] = font;
2107 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2108 if (!UNSPECIFIEDP (from[i]))
2110 if (i == LFACE_HEIGHT_INDEX && !INTEGERP (from[i]))
2112 to[i] = merge_face_heights (from[i], to[i], to[i]);
2113 font_clear_prop (to, FONT_SIZE_INDEX);
2115 else if (i != LFACE_FONT_INDEX && ! EQ (to[i], from[i]))
2117 to[i] = from[i];
2118 if (i >= LFACE_FAMILY_INDEX && i <=LFACE_SLANT_INDEX)
2119 font_clear_prop (to,
2120 (i == LFACE_FAMILY_INDEX ? FONT_FAMILY_INDEX
2121 : i == LFACE_FOUNDRY_INDEX ? FONT_FOUNDRY_INDEX
2122 : i == LFACE_SWIDTH_INDEX ? FONT_WIDTH_INDEX
2123 : i == LFACE_HEIGHT_INDEX ? FONT_SIZE_INDEX
2124 : i == LFACE_WEIGHT_INDEX ? FONT_WEIGHT_INDEX
2125 : FONT_SLANT_INDEX));
2129 /* If FROM specifies a font spec, make its contents take precedence
2130 over :family and other attributes. This is needed for face
2131 remapping using :font to work. */
2133 if (!NILP (font))
2135 if (! NILP (AREF (font, FONT_FOUNDRY_INDEX)))
2136 to[LFACE_FOUNDRY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FOUNDRY_INDEX));
2137 if (! NILP (AREF (font, FONT_FAMILY_INDEX)))
2138 to[LFACE_FAMILY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FAMILY_INDEX));
2139 if (! NILP (AREF (font, FONT_WEIGHT_INDEX)))
2140 to[LFACE_WEIGHT_INDEX] = FONT_WEIGHT_FOR_FACE (font);
2141 if (! NILP (AREF (font, FONT_SLANT_INDEX)))
2142 to[LFACE_SLANT_INDEX] = FONT_SLANT_FOR_FACE (font);
2143 if (! NILP (AREF (font, FONT_WIDTH_INDEX)))
2144 to[LFACE_SWIDTH_INDEX] = FONT_WIDTH_FOR_FACE (font);
2145 ASET (font, FONT_SIZE_INDEX, Qnil);
2148 /* TO is always an absolute face, which should inherit from nothing.
2149 We blindly copy the :inherit attribute above and fix it up here. */
2150 to[LFACE_INHERIT_INDEX] = Qnil;
2153 /* Merge the named face FACE_NAME on frame F, into the vector of face
2154 attributes TO. Use NAMED_MERGE_POINTS to detect loops in face
2155 inheritance. Return true if FACE_NAME is a valid face name and
2156 merging succeeded. */
2158 static bool
2159 merge_named_face (struct frame *f, Lisp_Object face_name, Lisp_Object *to,
2160 struct named_merge_point *named_merge_points)
2162 struct named_merge_point named_merge_point;
2164 if (push_named_merge_point (&named_merge_point,
2165 face_name, NAMED_MERGE_POINT_NORMAL,
2166 &named_merge_points))
2168 Lisp_Object from[LFACE_VECTOR_SIZE];
2169 bool ok = get_lface_attributes (f, face_name, from, false,
2170 named_merge_points);
2172 if (ok)
2173 merge_face_vectors (f, from, to, named_merge_points);
2175 return ok;
2177 else
2178 return false;
2182 /* Merge face attributes from the lisp `face reference' FACE_REF on
2183 frame F into the face attribute vector TO. If ERR_MSGS,
2184 problems with FACE_REF cause an error message to be shown. Return
2185 true if no errors occurred (regardless of the value of ERR_MSGS).
2186 Use NAMED_MERGE_POINTS to detect loops in face inheritance or
2187 list structure; it may be 0 for most callers.
2189 FACE_REF may be a single face specification or a list of such
2190 specifications. Each face specification can be:
2192 1. A symbol or string naming a Lisp face.
2194 2. A property list of the form (KEYWORD VALUE ...) where each
2195 KEYWORD is a face attribute name, and value is an appropriate value
2196 for that attribute.
2198 3. Conses or the form (FOREGROUND-COLOR . COLOR) or
2199 (BACKGROUND-COLOR . COLOR) where COLOR is a color name. This is
2200 for compatibility with 20.2.
2202 Face specifications earlier in lists take precedence over later
2203 specifications. */
2205 static bool
2206 merge_face_ref (struct frame *f, Lisp_Object face_ref, Lisp_Object *to,
2207 bool err_msgs, struct named_merge_point *named_merge_points)
2209 bool ok = true; /* Succeed without an error? */
2211 if (CONSP (face_ref))
2213 Lisp_Object first = XCAR (face_ref);
2215 if (EQ (first, Qforeground_color)
2216 || EQ (first, Qbackground_color))
2218 /* One of (FOREGROUND-COLOR . COLOR) or (BACKGROUND-COLOR
2219 . COLOR). COLOR must be a string. */
2220 Lisp_Object color_name = XCDR (face_ref);
2221 Lisp_Object color = first;
2223 if (STRINGP (color_name))
2225 if (EQ (color, Qforeground_color))
2226 to[LFACE_FOREGROUND_INDEX] = color_name;
2227 else
2228 to[LFACE_BACKGROUND_INDEX] = color_name;
2230 else
2232 if (err_msgs)
2233 add_to_log ("Invalid face color %S", color_name);
2234 ok = false;
2237 else if (SYMBOLP (first)
2238 && *SDATA (SYMBOL_NAME (first)) == ':')
2240 /* Assume this is the property list form. */
2241 while (CONSP (face_ref) && CONSP (XCDR (face_ref)))
2243 Lisp_Object keyword = XCAR (face_ref);
2244 Lisp_Object value = XCAR (XCDR (face_ref));
2245 bool err = false;
2247 /* Specifying `unspecified' is a no-op. */
2248 if (EQ (value, Qunspecified))
2250 else if (EQ (keyword, QCfamily))
2252 if (STRINGP (value))
2254 to[LFACE_FAMILY_INDEX] = value;
2255 font_clear_prop (to, FONT_FAMILY_INDEX);
2257 else
2258 err = true;
2260 else if (EQ (keyword, QCfoundry))
2262 if (STRINGP (value))
2264 to[LFACE_FOUNDRY_INDEX] = value;
2265 font_clear_prop (to, FONT_FOUNDRY_INDEX);
2267 else
2268 err = true;
2270 else if (EQ (keyword, QCheight))
2272 Lisp_Object new_height =
2273 merge_face_heights (value, to[LFACE_HEIGHT_INDEX], Qnil);
2275 if (! NILP (new_height))
2277 to[LFACE_HEIGHT_INDEX] = new_height;
2278 font_clear_prop (to, FONT_SIZE_INDEX);
2280 else
2281 err = true;
2283 else if (EQ (keyword, QCweight))
2285 if (SYMBOLP (value) && FONT_WEIGHT_NAME_NUMERIC (value) >= 0)
2287 to[LFACE_WEIGHT_INDEX] = value;
2288 font_clear_prop (to, FONT_WEIGHT_INDEX);
2290 else
2291 err = true;
2293 else if (EQ (keyword, QCslant))
2295 if (SYMBOLP (value) && FONT_SLANT_NAME_NUMERIC (value) >= 0)
2297 to[LFACE_SLANT_INDEX] = value;
2298 font_clear_prop (to, FONT_SLANT_INDEX);
2300 else
2301 err = true;
2303 else if (EQ (keyword, QCunderline))
2305 if (EQ (value, Qt)
2306 || NILP (value)
2307 || STRINGP (value)
2308 || CONSP (value))
2309 to[LFACE_UNDERLINE_INDEX] = value;
2310 else
2311 err = true;
2313 else if (EQ (keyword, QCoverline))
2315 if (EQ (value, Qt)
2316 || NILP (value)
2317 || STRINGP (value))
2318 to[LFACE_OVERLINE_INDEX] = value;
2319 else
2320 err = true;
2322 else if (EQ (keyword, QCstrike_through))
2324 if (EQ (value, Qt)
2325 || NILP (value)
2326 || STRINGP (value))
2327 to[LFACE_STRIKE_THROUGH_INDEX] = value;
2328 else
2329 err = true;
2331 else if (EQ (keyword, QCbox))
2333 if (EQ (value, Qt))
2334 value = make_number (1);
2335 if (INTEGERP (value)
2336 || STRINGP (value)
2337 || CONSP (value)
2338 || NILP (value))
2339 to[LFACE_BOX_INDEX] = value;
2340 else
2341 err = true;
2343 else if (EQ (keyword, QCinverse_video)
2344 || EQ (keyword, QCreverse_video))
2346 if (EQ (value, Qt) || NILP (value))
2347 to[LFACE_INVERSE_INDEX] = value;
2348 else
2349 err = true;
2351 else if (EQ (keyword, QCforeground))
2353 if (STRINGP (value))
2354 to[LFACE_FOREGROUND_INDEX] = value;
2355 else
2356 err = true;
2358 else if (EQ (keyword, QCdistant_foreground))
2360 if (STRINGP (value))
2361 to[LFACE_DISTANT_FOREGROUND_INDEX] = value;
2362 else
2363 err = true;
2365 else if (EQ (keyword, QCbackground))
2367 if (STRINGP (value))
2368 to[LFACE_BACKGROUND_INDEX] = value;
2369 else
2370 err = true;
2372 else if (EQ (keyword, QCstipple))
2374 #if defined (HAVE_WINDOW_SYSTEM)
2375 Lisp_Object pixmap_p = Fbitmap_spec_p (value);
2376 if (!NILP (pixmap_p))
2377 to[LFACE_STIPPLE_INDEX] = value;
2378 else
2379 err = true;
2380 #endif /* HAVE_WINDOW_SYSTEM */
2382 else if (EQ (keyword, QCwidth))
2384 if (SYMBOLP (value) && FONT_WIDTH_NAME_NUMERIC (value) >= 0)
2386 to[LFACE_SWIDTH_INDEX] = value;
2387 font_clear_prop (to, FONT_WIDTH_INDEX);
2389 else
2390 err = true;
2392 else if (EQ (keyword, QCfont))
2394 if (FONTP (value))
2395 to[LFACE_FONT_INDEX] = value;
2396 else
2397 err = true;
2399 else if (EQ (keyword, QCinherit))
2401 /* This is not really very useful; it's just like a
2402 normal face reference. */
2403 if (! merge_face_ref (f, value, to,
2404 err_msgs, named_merge_points))
2405 err = true;
2407 else
2408 err = true;
2410 if (err)
2412 add_to_log ("Invalid face attribute %S %S", keyword, value);
2413 ok = false;
2416 face_ref = XCDR (XCDR (face_ref));
2419 else
2421 /* This is a list of face refs. Those at the beginning of the
2422 list take precedence over what follows, so we have to merge
2423 from the end backwards. */
2424 Lisp_Object next = XCDR (face_ref);
2426 if (! NILP (next))
2427 ok = merge_face_ref (f, next, to, err_msgs, named_merge_points);
2429 if (! merge_face_ref (f, first, to, err_msgs, named_merge_points))
2430 ok = false;
2433 else
2435 /* FACE_REF ought to be a face name. */
2436 ok = merge_named_face (f, face_ref, to, named_merge_points);
2437 if (!ok && err_msgs)
2438 add_to_log ("Invalid face reference: %s", face_ref);
2441 return ok;
2445 DEFUN ("internal-make-lisp-face", Finternal_make_lisp_face,
2446 Sinternal_make_lisp_face, 1, 2, 0,
2447 doc: /* Make FACE, a symbol, a Lisp face with all attributes nil.
2448 If FACE was not known as a face before, create a new one.
2449 If optional argument FRAME is specified, make a frame-local face
2450 for that frame. Otherwise operate on the global face definition.
2451 Value is a vector of face attributes. */)
2452 (Lisp_Object face, Lisp_Object frame)
2454 Lisp_Object global_lface, lface;
2455 struct frame *f;
2456 int i;
2458 CHECK_SYMBOL (face);
2459 global_lface = lface_from_face_name (NULL, face, false);
2461 if (!NILP (frame))
2463 CHECK_LIVE_FRAME (frame);
2464 f = XFRAME (frame);
2465 lface = lface_from_face_name (f, face, false);
2467 else
2468 f = NULL, lface = Qnil;
2470 /* Add a global definition if there is none. */
2471 if (NILP (global_lface))
2473 global_lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2474 Qunspecified);
2475 ASET (global_lface, 0, Qface);
2476 Vface_new_frame_defaults = Fcons (Fcons (face, global_lface),
2477 Vface_new_frame_defaults);
2479 /* Assign the new Lisp face a unique ID. The mapping from Lisp
2480 face id to Lisp face is given by the vector lface_id_to_name.
2481 The mapping from Lisp face to Lisp face id is given by the
2482 property `face' of the Lisp face name. */
2483 if (next_lface_id == lface_id_to_name_size)
2484 lface_id_to_name =
2485 xpalloc (lface_id_to_name, &lface_id_to_name_size, 1, MAX_FACE_ID,
2486 sizeof *lface_id_to_name);
2488 lface_id_to_name[next_lface_id] = face;
2489 Fput (face, Qface, make_number (next_lface_id));
2490 ++next_lface_id;
2492 else if (f == NULL)
2493 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2494 ASET (global_lface, i, Qunspecified);
2496 /* Add a frame-local definition. */
2497 if (f)
2499 if (NILP (lface))
2501 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2502 Qunspecified);
2503 ASET (lface, 0, Qface);
2504 fset_face_alist (f, Fcons (Fcons (face, lface), f->face_alist));
2506 else
2507 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2508 ASET (lface, i, Qunspecified);
2510 else
2511 lface = global_lface;
2513 /* Changing a named face means that all realized faces depending on
2514 that face are invalid. Since we cannot tell which realized faces
2515 depend on the face, make sure they are all removed. This is done
2516 by setting face_change. The next call to init_iterator will then
2517 free realized faces. */
2518 if (NILP (Fget (face, Qface_no_inherit)))
2520 if (f)
2522 f->face_change = true;
2523 fset_redisplay (f);
2525 else
2527 face_change = true;
2528 windows_or_buffers_changed = 54;
2532 eassert (LFACEP (lface));
2533 check_lface (lface);
2534 return lface;
2538 DEFUN ("internal-lisp-face-p", Finternal_lisp_face_p,
2539 Sinternal_lisp_face_p, 1, 2, 0,
2540 doc: /* Return non-nil if FACE names a face.
2541 FACE should be a symbol or string.
2542 If optional second argument FRAME is non-nil, check for the
2543 existence of a frame-local face with name FACE on that frame.
2544 Otherwise check for the existence of a global face. */)
2545 (Lisp_Object face, Lisp_Object frame)
2547 Lisp_Object lface;
2549 face = resolve_face_name (face, true);
2551 if (!NILP (frame))
2553 CHECK_LIVE_FRAME (frame);
2554 lface = lface_from_face_name (XFRAME (frame), face, false);
2556 else
2557 lface = lface_from_face_name (NULL, face, false);
2559 return lface;
2563 DEFUN ("internal-copy-lisp-face", Finternal_copy_lisp_face,
2564 Sinternal_copy_lisp_face, 4, 4, 0,
2565 doc: /* Copy face FROM to TO.
2566 If FRAME is t, copy the global face definition of FROM.
2567 Otherwise, copy the frame-local definition of FROM on FRAME.
2568 If NEW-FRAME is a frame, copy that data into the frame-local
2569 definition of TO on NEW-FRAME. If NEW-FRAME is nil,
2570 FRAME controls where the data is copied to.
2572 The value is TO. */)
2573 (Lisp_Object from, Lisp_Object to, Lisp_Object frame, Lisp_Object new_frame)
2575 Lisp_Object lface, copy;
2576 struct frame *f;
2578 CHECK_SYMBOL (from);
2579 CHECK_SYMBOL (to);
2581 if (EQ (frame, Qt))
2583 /* Copy global definition of FROM. We don't make copies of
2584 strings etc. because 20.2 didn't do it either. */
2585 lface = lface_from_face_name (NULL, from, true);
2586 copy = Finternal_make_lisp_face (to, Qnil);
2587 f = NULL;
2589 else
2591 /* Copy frame-local definition of FROM. */
2592 if (NILP (new_frame))
2593 new_frame = frame;
2594 CHECK_LIVE_FRAME (frame);
2595 CHECK_LIVE_FRAME (new_frame);
2596 lface = lface_from_face_name (XFRAME (frame), from, true);
2597 copy = Finternal_make_lisp_face (to, new_frame);
2598 f = XFRAME (new_frame);
2601 vcopy (copy, 0, XVECTOR (lface)->contents, LFACE_VECTOR_SIZE);
2603 /* Changing a named face means that all realized faces depending on
2604 that face are invalid. Since we cannot tell which realized faces
2605 depend on the face, make sure they are all removed. This is done
2606 by setting face_change. The next call to init_iterator will then
2607 free realized faces. */
2608 if (NILP (Fget (to, Qface_no_inherit)))
2610 if (f)
2612 f->face_change = true;
2613 fset_redisplay (f);
2615 else
2617 face_change = true;
2618 windows_or_buffers_changed = 55;
2622 return to;
2626 DEFUN ("internal-set-lisp-face-attribute", Finternal_set_lisp_face_attribute,
2627 Sinternal_set_lisp_face_attribute, 3, 4, 0,
2628 doc: /* Set attribute ATTR of FACE to VALUE.
2629 FRAME being a frame means change the face on that frame.
2630 FRAME nil means change the face of the selected frame.
2631 FRAME t means change the default for new frames.
2632 FRAME 0 means change the face on all frames, and change the default
2633 for new frames. */)
2634 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
2636 Lisp_Object lface;
2637 Lisp_Object old_value = Qnil;
2638 /* Set one of enum font_property_index (> 0) if ATTR is one of
2639 font-related attributes other than QCfont and QCfontset. */
2640 enum font_property_index prop_index = 0;
2641 struct frame *f;
2643 CHECK_SYMBOL (face);
2644 CHECK_SYMBOL (attr);
2646 face = resolve_face_name (face, true);
2648 /* If FRAME is 0, change face on all frames, and change the
2649 default for new frames. */
2650 if (INTEGERP (frame) && XINT (frame) == 0)
2652 Lisp_Object tail;
2653 Finternal_set_lisp_face_attribute (face, attr, value, Qt);
2654 FOR_EACH_FRAME (tail, frame)
2655 Finternal_set_lisp_face_attribute (face, attr, value, frame);
2656 return face;
2659 /* Set lface to the Lisp attribute vector of FACE. */
2660 if (EQ (frame, Qt))
2662 f = NULL;
2663 lface = lface_from_face_name (NULL, face, true);
2665 /* When updating face-new-frame-defaults, we put :ignore-defface
2666 where the caller wants `unspecified'. This forces the frame
2667 defaults to ignore the defface value. Otherwise, the defface
2668 will take effect, which is generally not what is intended.
2669 The value of that attribute will be inherited from some other
2670 face during face merging. See internal_merge_in_global_face. */
2671 if (UNSPECIFIEDP (value))
2672 value = QCignore_defface;
2674 else
2676 if (NILP (frame))
2677 frame = selected_frame;
2679 CHECK_LIVE_FRAME (frame);
2680 f = XFRAME (frame);
2682 lface = lface_from_face_name (f, face, false);
2684 /* If a frame-local face doesn't exist yet, create one. */
2685 if (NILP (lface))
2686 lface = Finternal_make_lisp_face (face, frame);
2689 if (EQ (attr, QCfamily))
2691 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2693 CHECK_STRING (value);
2694 if (SCHARS (value) == 0)
2695 signal_error ("Invalid face family", value);
2697 old_value = LFACE_FAMILY (lface);
2698 ASET (lface, LFACE_FAMILY_INDEX, value);
2699 prop_index = FONT_FAMILY_INDEX;
2701 else if (EQ (attr, QCfoundry))
2703 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2705 CHECK_STRING (value);
2706 if (SCHARS (value) == 0)
2707 signal_error ("Invalid face foundry", value);
2709 old_value = LFACE_FOUNDRY (lface);
2710 ASET (lface, LFACE_FOUNDRY_INDEX, value);
2711 prop_index = FONT_FOUNDRY_INDEX;
2713 else if (EQ (attr, QCheight))
2715 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2717 if (EQ (face, Qdefault))
2719 /* The default face must have an absolute size. */
2720 if (!INTEGERP (value) || XINT (value) <= 0)
2721 signal_error ("Default face height not absolute and positive",
2722 value);
2724 else
2726 /* For non-default faces, do a test merge with a random
2727 height to see if VALUE's ok. */
2728 Lisp_Object test = merge_face_heights (value,
2729 make_number (10),
2730 Qnil);
2731 if (!INTEGERP (test) || XINT (test) <= 0)
2732 signal_error ("Face height does not produce a positive integer",
2733 value);
2737 old_value = LFACE_HEIGHT (lface);
2738 ASET (lface, LFACE_HEIGHT_INDEX, value);
2739 prop_index = FONT_SIZE_INDEX;
2741 else if (EQ (attr, QCweight))
2743 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2745 CHECK_SYMBOL (value);
2746 if (FONT_WEIGHT_NAME_NUMERIC (value) < 0)
2747 signal_error ("Invalid face weight", value);
2749 old_value = LFACE_WEIGHT (lface);
2750 ASET (lface, LFACE_WEIGHT_INDEX, value);
2751 prop_index = FONT_WEIGHT_INDEX;
2753 else if (EQ (attr, QCslant))
2755 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2757 CHECK_SYMBOL (value);
2758 if (FONT_SLANT_NAME_NUMERIC (value) < 0)
2759 signal_error ("Invalid face slant", value);
2761 old_value = LFACE_SLANT (lface);
2762 ASET (lface, LFACE_SLANT_INDEX, value);
2763 prop_index = FONT_SLANT_INDEX;
2765 else if (EQ (attr, QCunderline))
2767 bool valid_p = false;
2769 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2770 valid_p = true;
2771 else if (NILP (value) || EQ (value, Qt))
2772 valid_p = true;
2773 else if (STRINGP (value) && SCHARS (value) > 0)
2774 valid_p = true;
2775 else if (CONSP (value))
2777 Lisp_Object key, val, list;
2779 list = value;
2780 /* FIXME? This errs on the side of acceptance. Eg it accepts:
2781 (defface foo '((t :underline 'foo) "doc")
2782 Maybe this is intentional, maybe it isn't.
2783 Non-nil symbols other than t are not documented as being valid.
2784 Eg compare with inverse-video, which explicitly rejects them.
2786 valid_p = true;
2788 while (!NILP (CAR_SAFE(list)))
2790 key = CAR_SAFE (list);
2791 list = CDR_SAFE (list);
2792 val = CAR_SAFE (list);
2793 list = CDR_SAFE (list);
2795 if (NILP (key) || NILP (val))
2797 valid_p = false;
2798 break;
2801 else if (EQ (key, QCcolor)
2802 && !(EQ (val, Qforeground_color)
2803 || (STRINGP (val) && SCHARS (val) > 0)))
2805 valid_p = false;
2806 break;
2809 else if (EQ (key, QCstyle)
2810 && !(EQ (val, Qline) || EQ (val, Qwave)))
2812 valid_p = false;
2813 break;
2818 if (!valid_p)
2819 signal_error ("Invalid face underline", value);
2821 old_value = LFACE_UNDERLINE (lface);
2822 ASET (lface, LFACE_UNDERLINE_INDEX, value);
2824 else if (EQ (attr, QCoverline))
2826 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2827 if ((SYMBOLP (value)
2828 && !EQ (value, Qt)
2829 && !EQ (value, Qnil))
2830 /* Overline color. */
2831 || (STRINGP (value)
2832 && SCHARS (value) == 0))
2833 signal_error ("Invalid face overline", value);
2835 old_value = LFACE_OVERLINE (lface);
2836 ASET (lface, LFACE_OVERLINE_INDEX, value);
2838 else if (EQ (attr, QCstrike_through))
2840 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2841 if ((SYMBOLP (value)
2842 && !EQ (value, Qt)
2843 && !EQ (value, Qnil))
2844 /* Strike-through color. */
2845 || (STRINGP (value)
2846 && SCHARS (value) == 0))
2847 signal_error ("Invalid face strike-through", value);
2849 old_value = LFACE_STRIKE_THROUGH (lface);
2850 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, value);
2852 else if (EQ (attr, QCbox))
2854 bool valid_p;
2856 /* Allow t meaning a simple box of width 1 in foreground color
2857 of the face. */
2858 if (EQ (value, Qt))
2859 value = make_number (1);
2861 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2862 valid_p = true;
2863 else if (NILP (value))
2864 valid_p = true;
2865 else if (INTEGERP (value))
2866 valid_p = XINT (value) != 0;
2867 else if (STRINGP (value))
2868 valid_p = SCHARS (value) > 0;
2869 else if (CONSP (value))
2871 Lisp_Object tem;
2873 tem = value;
2874 while (CONSP (tem))
2876 Lisp_Object k, v;
2878 k = XCAR (tem);
2879 tem = XCDR (tem);
2880 if (!CONSP (tem))
2881 break;
2882 v = XCAR (tem);
2883 tem = XCDR (tem);
2885 if (EQ (k, QCline_width))
2887 if (!INTEGERP (v) || XINT (v) == 0)
2888 break;
2890 else if (EQ (k, QCcolor))
2892 if (!NILP (v) && (!STRINGP (v) || SCHARS (v) == 0))
2893 break;
2895 else if (EQ (k, QCstyle))
2897 if (!EQ (v, Qpressed_button) && !EQ (v, Qreleased_button))
2898 break;
2900 else
2901 break;
2904 valid_p = NILP (tem);
2906 else
2907 valid_p = false;
2909 if (!valid_p)
2910 signal_error ("Invalid face box", value);
2912 old_value = LFACE_BOX (lface);
2913 ASET (lface, LFACE_BOX_INDEX, value);
2915 else if (EQ (attr, QCinverse_video)
2916 || EQ (attr, QCreverse_video))
2918 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2920 CHECK_SYMBOL (value);
2921 if (!EQ (value, Qt) && !NILP (value))
2922 signal_error ("Invalid inverse-video face attribute value", value);
2924 old_value = LFACE_INVERSE (lface);
2925 ASET (lface, LFACE_INVERSE_INDEX, value);
2927 else if (EQ (attr, QCforeground))
2929 /* Compatibility with 20.x. */
2930 if (NILP (value))
2931 value = Qunspecified;
2932 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2934 /* Don't check for valid color names here because it depends
2935 on the frame (display) whether the color will be valid
2936 when the face is realized. */
2937 CHECK_STRING (value);
2938 if (SCHARS (value) == 0)
2939 signal_error ("Empty foreground color value", value);
2941 old_value = LFACE_FOREGROUND (lface);
2942 ASET (lface, LFACE_FOREGROUND_INDEX, value);
2944 else if (EQ (attr, QCdistant_foreground))
2946 /* Compatibility with 20.x. */
2947 if (NILP (value))
2948 value = Qunspecified;
2949 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2951 /* Don't check for valid color names here because it depends
2952 on the frame (display) whether the color will be valid
2953 when the face is realized. */
2954 CHECK_STRING (value);
2955 if (SCHARS (value) == 0)
2956 signal_error ("Empty distant-foreground color value", value);
2958 old_value = LFACE_DISTANT_FOREGROUND (lface);
2959 ASET (lface, LFACE_DISTANT_FOREGROUND_INDEX, value);
2961 else if (EQ (attr, QCbackground))
2963 /* Compatibility with 20.x. */
2964 if (NILP (value))
2965 value = Qunspecified;
2966 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2968 /* Don't check for valid color names here because it depends
2969 on the frame (display) whether the color will be valid
2970 when the face is realized. */
2971 CHECK_STRING (value);
2972 if (SCHARS (value) == 0)
2973 signal_error ("Empty background color value", value);
2975 old_value = LFACE_BACKGROUND (lface);
2976 ASET (lface, LFACE_BACKGROUND_INDEX, value);
2978 else if (EQ (attr, QCstipple))
2980 #if defined (HAVE_WINDOW_SYSTEM)
2981 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
2982 && !NILP (value)
2983 && NILP (Fbitmap_spec_p (value)))
2984 signal_error ("Invalid stipple attribute", value);
2985 old_value = LFACE_STIPPLE (lface);
2986 ASET (lface, LFACE_STIPPLE_INDEX, value);
2987 #endif /* HAVE_WINDOW_SYSTEM */
2989 else if (EQ (attr, QCwidth))
2991 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2993 CHECK_SYMBOL (value);
2994 if (FONT_WIDTH_NAME_NUMERIC (value) < 0)
2995 signal_error ("Invalid face width", value);
2997 old_value = LFACE_SWIDTH (lface);
2998 ASET (lface, LFACE_SWIDTH_INDEX, value);
2999 prop_index = FONT_WIDTH_INDEX;
3001 else if (EQ (attr, QCfont))
3003 #ifdef HAVE_WINDOW_SYSTEM
3004 if (EQ (frame, Qt) || FRAME_WINDOW_P (f))
3006 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3008 struct frame *f1;
3010 old_value = LFACE_FONT (lface);
3011 if (! FONTP (value))
3013 if (STRINGP (value))
3015 Lisp_Object name = value;
3016 int fontset = fs_query_fontset (name, 0);
3018 if (fontset >= 0)
3019 name = fontset_ascii (fontset);
3020 value = font_spec_from_name (name);
3021 if (!FONTP (value))
3022 signal_error ("Invalid font name", name);
3024 else
3025 signal_error ("Invalid font or font-spec", value);
3027 if (EQ (frame, Qt))
3028 f1 = XFRAME (selected_frame);
3029 else
3030 f1 = XFRAME (frame);
3032 /* FIXME:
3033 If frame is t, and selected frame is a tty frame, the font
3034 can't be realized. An improvement would be to loop over frames
3035 for a non-tty frame and use that. See discussion in Bug#18573.
3036 For a daemon, frame may be an initial frame (Bug#18869). */
3037 if (FRAME_WINDOW_P (f1))
3039 if (! FONT_OBJECT_P (value))
3041 Lisp_Object *attrs = XVECTOR (lface)->contents;
3042 Lisp_Object font_object;
3044 font_object = font_load_for_lface (f1, attrs, value);
3045 if (NILP (font_object))
3046 signal_error ("Font not available", value);
3047 value = font_object;
3049 set_lface_from_font (f1, lface, value, true);
3050 f1->face_change = 1;
3053 else
3054 ASET (lface, LFACE_FONT_INDEX, value);
3056 #endif /* HAVE_WINDOW_SYSTEM */
3058 else if (EQ (attr, QCfontset))
3060 #ifdef HAVE_WINDOW_SYSTEM
3061 if (EQ (frame, Qt) || FRAME_WINDOW_P (f))
3063 Lisp_Object tmp;
3065 old_value = LFACE_FONTSET (lface);
3066 tmp = Fquery_fontset (value, Qnil);
3067 if (NILP (tmp))
3068 signal_error ("Invalid fontset name", value);
3069 ASET (lface, LFACE_FONTSET_INDEX, value = tmp);
3071 #endif /* HAVE_WINDOW_SYSTEM */
3073 else if (EQ (attr, QCinherit))
3075 Lisp_Object tail;
3076 if (SYMBOLP (value))
3077 tail = Qnil;
3078 else
3079 for (tail = value; CONSP (tail); tail = XCDR (tail))
3080 if (!SYMBOLP (XCAR (tail)))
3081 break;
3082 if (NILP (tail))
3083 ASET (lface, LFACE_INHERIT_INDEX, value);
3084 else
3085 signal_error ("Invalid face inheritance", value);
3087 else if (EQ (attr, QCbold))
3089 old_value = LFACE_WEIGHT (lface);
3090 ASET (lface, LFACE_WEIGHT_INDEX, NILP (value) ? Qnormal : Qbold);
3091 prop_index = FONT_WEIGHT_INDEX;
3093 else if (EQ (attr, QCitalic))
3095 attr = QCslant;
3096 old_value = LFACE_SLANT (lface);
3097 ASET (lface, LFACE_SLANT_INDEX, NILP (value) ? Qnormal : Qitalic);
3098 prop_index = FONT_SLANT_INDEX;
3100 else
3101 signal_error ("Invalid face attribute name", attr);
3103 if (prop_index)
3105 /* If a font-related attribute other than QCfont and QCfontset
3106 is specified, and if the original QCfont attribute has a font
3107 (font-spec or font-object), set the corresponding property in
3108 the font to nil so that the font selector doesn't think that
3109 the attribute is mandatory. Also, clear the average
3110 width. */
3111 font_clear_prop (XVECTOR (lface)->contents, prop_index);
3114 /* Changing a named face means that all realized faces depending on
3115 that face are invalid. Since we cannot tell which realized faces
3116 depend on the face, make sure they are all removed. This is done
3117 by setting face_change. The next call to init_iterator will then
3118 free realized faces. */
3119 if (!EQ (frame, Qt)
3120 && NILP (Fget (face, Qface_no_inherit))
3121 && NILP (Fequal (old_value, value)))
3123 f->face_change = true;
3124 fset_redisplay (f);
3127 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3128 && NILP (Fequal (old_value, value)))
3130 Lisp_Object param;
3132 param = Qnil;
3134 if (EQ (face, Qdefault))
3136 #ifdef HAVE_WINDOW_SYSTEM
3137 /* Changed font-related attributes of the `default' face are
3138 reflected in changed `font' frame parameters. */
3139 if (FRAMEP (frame)
3140 && (prop_index || EQ (attr, QCfont))
3141 && lface_fully_specified_p (XVECTOR (lface)->contents))
3142 set_font_frame_param (frame, lface);
3143 else
3144 #endif /* HAVE_WINDOW_SYSTEM */
3146 if (EQ (attr, QCforeground))
3147 param = Qforeground_color;
3148 else if (EQ (attr, QCbackground))
3149 param = Qbackground_color;
3151 #ifdef HAVE_WINDOW_SYSTEM
3152 #ifndef HAVE_NTGUI
3153 else if (EQ (face, Qscroll_bar))
3155 /* Changing the colors of `scroll-bar' sets frame parameters
3156 `scroll-bar-foreground' and `scroll-bar-background'. */
3157 if (EQ (attr, QCforeground))
3158 param = Qscroll_bar_foreground;
3159 else if (EQ (attr, QCbackground))
3160 param = Qscroll_bar_background;
3162 #endif /* not HAVE_NTGUI */
3163 else if (EQ (face, Qborder))
3165 /* Changing background color of `border' sets frame parameter
3166 `border-color'. */
3167 if (EQ (attr, QCbackground))
3168 param = Qborder_color;
3170 else if (EQ (face, Qcursor))
3172 /* Changing background color of `cursor' sets frame parameter
3173 `cursor-color'. */
3174 if (EQ (attr, QCbackground))
3175 param = Qcursor_color;
3177 else if (EQ (face, Qmouse))
3179 /* Changing background color of `mouse' sets frame parameter
3180 `mouse-color'. */
3181 if (EQ (attr, QCbackground))
3182 param = Qmouse_color;
3184 #endif /* HAVE_WINDOW_SYSTEM */
3185 else if (EQ (face, Qmenu))
3187 /* Indicate that we have to update the menu bar when realizing
3188 faces on FRAME. FRAME t change the default for new frames.
3189 We do this by setting the flag in new face caches. */
3190 if (FRAMEP (frame))
3192 struct frame *f = XFRAME (frame);
3193 if (FRAME_FACE_CACHE (f) == NULL)
3194 FRAME_FACE_CACHE (f) = make_face_cache (f);
3195 FRAME_FACE_CACHE (f)->menu_face_changed_p = true;
3197 else
3198 menu_face_changed_default = true;
3201 if (!NILP (param))
3203 if (EQ (frame, Qt))
3204 /* Update `default-frame-alist', which is used for new frames. */
3206 store_in_alist (&Vdefault_frame_alist, param, value);
3208 else
3209 /* Update the current frame's parameters. */
3211 Lisp_Object cons;
3212 cons = XCAR (Vparam_value_alist);
3213 XSETCAR (cons, param);
3214 XSETCDR (cons, value);
3215 Fmodify_frame_parameters (frame, Vparam_value_alist);
3220 return face;
3224 /* Update the corresponding face when frame parameter PARAM on frame F
3225 has been assigned the value NEW_VALUE. */
3227 void
3228 update_face_from_frame_parameter (struct frame *f, Lisp_Object param,
3229 Lisp_Object new_value)
3231 Lisp_Object face = Qnil;
3232 Lisp_Object lface;
3234 /* If there are no faces yet, give up. This is the case when called
3235 from Fx_create_frame, and we do the necessary things later in
3236 face-set-after-frame-defaults. */
3237 if (NILP (f->face_alist))
3238 return;
3240 if (EQ (param, Qforeground_color))
3242 face = Qdefault;
3243 lface = lface_from_face_name (f, face, true);
3244 ASET (lface, LFACE_FOREGROUND_INDEX,
3245 (STRINGP (new_value) ? new_value : Qunspecified));
3246 realize_basic_faces (f);
3248 else if (EQ (param, Qbackground_color))
3250 Lisp_Object frame;
3252 /* Changing the background color might change the background
3253 mode, so that we have to load new defface specs.
3254 Call frame-set-background-mode to do that. */
3255 XSETFRAME (frame, f);
3256 call1 (Qframe_set_background_mode, frame);
3258 face = Qdefault;
3259 lface = lface_from_face_name (f, face, true);
3260 ASET (lface, LFACE_BACKGROUND_INDEX,
3261 (STRINGP (new_value) ? new_value : Qunspecified));
3262 realize_basic_faces (f);
3264 #ifdef HAVE_WINDOW_SYSTEM
3265 else if (EQ (param, Qborder_color))
3267 face = Qborder;
3268 lface = lface_from_face_name (f, face, true);
3269 ASET (lface, LFACE_BACKGROUND_INDEX,
3270 (STRINGP (new_value) ? new_value : Qunspecified));
3272 else if (EQ (param, Qcursor_color))
3274 face = Qcursor;
3275 lface = lface_from_face_name (f, face, true);
3276 ASET (lface, LFACE_BACKGROUND_INDEX,
3277 (STRINGP (new_value) ? new_value : Qunspecified));
3279 else if (EQ (param, Qmouse_color))
3281 face = Qmouse;
3282 lface = lface_from_face_name (f, face, true);
3283 ASET (lface, LFACE_BACKGROUND_INDEX,
3284 (STRINGP (new_value) ? new_value : Qunspecified));
3286 #endif
3288 /* Changing a named face means that all realized faces depending on
3289 that face are invalid. Since we cannot tell which realized faces
3290 depend on the face, make sure they are all removed. This is done
3291 by setting face_change. The next call to init_iterator will then
3292 free realized faces. */
3293 if (!NILP (face)
3294 && NILP (Fget (face, Qface_no_inherit)))
3296 f->face_change = true;
3297 fset_redisplay (f);
3302 #ifdef HAVE_WINDOW_SYSTEM
3304 /* Set the `font' frame parameter of FRAME determined from the
3305 font-object set in `default' face attributes LFACE. */
3307 static void
3308 set_font_frame_param (Lisp_Object frame, Lisp_Object lface)
3310 struct frame *f = XFRAME (frame);
3311 Lisp_Object font;
3313 if (FRAME_WINDOW_P (f)
3314 /* Don't do anything if the font is `unspecified'. This can
3315 happen during frame creation. */
3316 && (font = LFACE_FONT (lface),
3317 ! UNSPECIFIEDP (font)))
3319 if (FONT_SPEC_P (font))
3321 font = font_load_for_lface (f, XVECTOR (lface)->contents, font);
3322 if (NILP (font))
3323 return;
3324 ASET (lface, LFACE_FONT_INDEX, font);
3326 f->default_face_done_p = false;
3327 AUTO_FRAME_ARG (arg, Qfont, font);
3328 Fmodify_frame_parameters (frame, arg);
3332 DEFUN ("internal-face-x-get-resource", Finternal_face_x_get_resource,
3333 Sinternal_face_x_get_resource, 2, 3, 0,
3334 doc: /* Get the value of X resource RESOURCE, class CLASS.
3335 Returned value is for the display of frame FRAME. If FRAME is not
3336 specified or nil, use selected frame. This function exists because
3337 ordinary `x-get-resource' doesn't take a frame argument. */)
3338 (Lisp_Object resource, Lisp_Object class, Lisp_Object frame)
3340 Lisp_Object value = Qnil;
3341 struct frame *f;
3343 CHECK_STRING (resource);
3344 CHECK_STRING (class);
3345 f = decode_live_frame (frame);
3346 block_input ();
3347 value = display_x_get_resource (FRAME_DISPLAY_INFO (f),
3348 resource, class, Qnil, Qnil);
3349 unblock_input ();
3350 return value;
3354 /* Return resource string VALUE as a boolean value, i.e. nil, or t.
3355 If VALUE is "on" or "true", return t. If VALUE is "off" or
3356 "false", return nil. Otherwise, if SIGNAL_P, signal an
3357 error; if !SIGNAL_P, return 0. */
3359 static Lisp_Object
3360 face_boolean_x_resource_value (Lisp_Object value, bool signal_p)
3362 Lisp_Object result = make_number (0);
3364 eassert (STRINGP (value));
3366 if (xstrcasecmp (SSDATA (value), "on") == 0
3367 || xstrcasecmp (SSDATA (value), "true") == 0)
3368 result = Qt;
3369 else if (xstrcasecmp (SSDATA (value), "off") == 0
3370 || xstrcasecmp (SSDATA (value), "false") == 0)
3371 result = Qnil;
3372 else if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3373 result = Qunspecified;
3374 else if (signal_p)
3375 signal_error ("Invalid face attribute value from X resource", value);
3377 return result;
3381 DEFUN ("internal-set-lisp-face-attribute-from-resource",
3382 Finternal_set_lisp_face_attribute_from_resource,
3383 Sinternal_set_lisp_face_attribute_from_resource,
3384 3, 4, 0, doc: /* */)
3385 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
3387 CHECK_SYMBOL (face);
3388 CHECK_SYMBOL (attr);
3389 CHECK_STRING (value);
3391 if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3392 value = Qunspecified;
3393 else if (EQ (attr, QCheight))
3395 value = Fstring_to_number (value, Qnil);
3396 if (!INTEGERP (value) || XINT (value) <= 0)
3397 signal_error ("Invalid face height from X resource", value);
3399 else if (EQ (attr, QCbold) || EQ (attr, QCitalic))
3400 value = face_boolean_x_resource_value (value, true);
3401 else if (EQ (attr, QCweight) || EQ (attr, QCslant) || EQ (attr, QCwidth))
3402 value = intern (SSDATA (value));
3403 else if (EQ (attr, QCreverse_video) || EQ (attr, QCinverse_video))
3404 value = face_boolean_x_resource_value (value, true);
3405 else if (EQ (attr, QCunderline)
3406 || EQ (attr, QCoverline)
3407 || EQ (attr, QCstrike_through))
3409 Lisp_Object boolean_value;
3411 /* If the result of face_boolean_x_resource_value is t or nil,
3412 VALUE does NOT specify a color. */
3413 boolean_value = face_boolean_x_resource_value (value, false);
3414 if (SYMBOLP (boolean_value))
3415 value = boolean_value;
3417 else if (EQ (attr, QCbox) || EQ (attr, QCinherit))
3418 value = Fcar (Fread_from_string (value, Qnil, Qnil));
3420 return Finternal_set_lisp_face_attribute (face, attr, value, frame);
3423 #endif /* HAVE_WINDOW_SYSTEM */
3426 /***********************************************************************
3427 Menu face
3428 ***********************************************************************/
3430 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
3432 /* Make menus on frame F appear as specified by the `menu' face. */
3434 static void
3435 x_update_menu_appearance (struct frame *f)
3437 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
3438 XrmDatabase rdb;
3440 if (dpyinfo
3441 && (rdb = XrmGetDatabase (FRAME_X_DISPLAY (f)),
3442 rdb != NULL))
3444 char line[512];
3445 char *buf = line;
3446 ptrdiff_t bufsize = sizeof line;
3447 Lisp_Object lface = lface_from_face_name (f, Qmenu, true);
3448 struct face *face = FACE_FROM_ID (f, MENU_FACE_ID);
3449 const char *myname = SSDATA (Vx_resource_name);
3450 bool changed_p = false;
3451 #ifdef USE_MOTIF
3452 const char *popup_path = "popup_menu";
3453 #else
3454 const char *popup_path = "menu.popup";
3455 #endif
3457 if (STRINGP (LFACE_FOREGROUND (lface)))
3459 exprintf (&buf, &bufsize, line, -1, "%s.%s*foreground: %s",
3460 myname, popup_path,
3461 SDATA (LFACE_FOREGROUND (lface)));
3462 XrmPutLineResource (&rdb, line);
3463 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*foreground: %s",
3464 myname, SDATA (LFACE_FOREGROUND (lface)));
3465 XrmPutLineResource (&rdb, line);
3466 changed_p = true;
3469 if (STRINGP (LFACE_BACKGROUND (lface)))
3471 exprintf (&buf, &bufsize, line, -1, "%s.%s*background: %s",
3472 myname, popup_path,
3473 SDATA (LFACE_BACKGROUND (lface)));
3474 XrmPutLineResource (&rdb, line);
3476 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*background: %s",
3477 myname, SDATA (LFACE_BACKGROUND (lface)));
3478 XrmPutLineResource (&rdb, line);
3479 changed_p = true;
3482 if (face->font
3483 /* On Solaris 5.8, it's been reported that the `menu' face
3484 can be unspecified here, during startup. Why this
3485 happens remains unknown. -- cyd */
3486 && FONTP (LFACE_FONT (lface))
3487 && (!UNSPECIFIEDP (LFACE_FAMILY (lface))
3488 || !UNSPECIFIEDP (LFACE_FOUNDRY (lface))
3489 || !UNSPECIFIEDP (LFACE_SWIDTH (lface))
3490 || !UNSPECIFIEDP (LFACE_WEIGHT (lface))
3491 || !UNSPECIFIEDP (LFACE_SLANT (lface))
3492 || !UNSPECIFIEDP (LFACE_HEIGHT (lface))))
3494 Lisp_Object xlfd = Ffont_xlfd_name (LFACE_FONT (lface), Qnil);
3495 #ifdef USE_MOTIF
3496 const char *suffix = "List";
3497 bool motif = true;
3498 #else
3499 #if defined HAVE_X_I18N
3501 const char *suffix = "Set";
3502 #else
3503 const char *suffix = "";
3504 #endif
3505 bool motif = false;
3506 #endif
3508 if (! NILP (xlfd))
3510 #if defined HAVE_X_I18N
3511 char *fontsetname = xic_create_fontsetname (SSDATA (xlfd), motif);
3512 #else
3513 char *fontsetname = SSDATA (xlfd);
3514 #endif
3515 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*font%s: %s",
3516 myname, suffix, fontsetname);
3517 XrmPutLineResource (&rdb, line);
3519 exprintf (&buf, &bufsize, line, -1, "%s.%s*font%s: %s",
3520 myname, popup_path, suffix, fontsetname);
3521 XrmPutLineResource (&rdb, line);
3522 changed_p = true;
3523 if (fontsetname != SSDATA (xlfd))
3524 xfree (fontsetname);
3528 if (changed_p && f->output_data.x->menubar_widget)
3529 free_frame_menubar (f);
3531 if (buf != line)
3532 xfree (buf);
3536 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
3539 DEFUN ("face-attribute-relative-p", Fface_attribute_relative_p,
3540 Sface_attribute_relative_p,
3541 2, 2, 0,
3542 doc: /* Check whether a face attribute value is relative.
3543 Specifically, this function returns t if the attribute ATTRIBUTE
3544 with the value VALUE is relative.
3546 A relative value is one that doesn't entirely override whatever is
3547 inherited from another face. For most possible attributes,
3548 the only relative value that users see is `unspecified'.
3549 However, for :height, floating point values are also relative. */
3550 attributes: const)
3551 (Lisp_Object attribute, Lisp_Object value)
3553 if (EQ (value, Qunspecified) || (EQ (value, QCignore_defface)))
3554 return Qt;
3555 else if (EQ (attribute, QCheight))
3556 return INTEGERP (value) ? Qnil : Qt;
3557 else
3558 return Qnil;
3561 DEFUN ("merge-face-attribute", Fmerge_face_attribute, Smerge_face_attribute,
3562 3, 3, 0,
3563 doc: /* Return face ATTRIBUTE VALUE1 merged with VALUE2.
3564 If VALUE1 or VALUE2 are absolute (see `face-attribute-relative-p'), then
3565 the result will be absolute, otherwise it will be relative. */)
3566 (Lisp_Object attribute, Lisp_Object value1, Lisp_Object value2)
3568 if (EQ (value1, Qunspecified) || EQ (value1, QCignore_defface))
3569 return value2;
3570 else if (EQ (attribute, QCheight))
3571 return merge_face_heights (value1, value2, value1);
3572 else
3573 return value1;
3577 DEFUN ("internal-get-lisp-face-attribute", Finternal_get_lisp_face_attribute,
3578 Sinternal_get_lisp_face_attribute,
3579 2, 3, 0,
3580 doc: /* Return face attribute KEYWORD of face SYMBOL.
3581 If SYMBOL does not name a valid Lisp face or KEYWORD isn't a valid
3582 face attribute name, signal an error.
3583 If the optional argument FRAME is given, report on face SYMBOL in that
3584 frame. If FRAME is t, report on the defaults for face SYMBOL (for new
3585 frames). If FRAME is omitted or nil, use the selected frame. */)
3586 (Lisp_Object symbol, Lisp_Object keyword, Lisp_Object frame)
3588 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3589 Lisp_Object lface = lface_from_face_name (f, symbol, true), value = Qnil;
3591 CHECK_SYMBOL (symbol);
3592 CHECK_SYMBOL (keyword);
3594 if (EQ (keyword, QCfamily))
3595 value = LFACE_FAMILY (lface);
3596 else if (EQ (keyword, QCfoundry))
3597 value = LFACE_FOUNDRY (lface);
3598 else if (EQ (keyword, QCheight))
3599 value = LFACE_HEIGHT (lface);
3600 else if (EQ (keyword, QCweight))
3601 value = LFACE_WEIGHT (lface);
3602 else if (EQ (keyword, QCslant))
3603 value = LFACE_SLANT (lface);
3604 else if (EQ (keyword, QCunderline))
3605 value = LFACE_UNDERLINE (lface);
3606 else if (EQ (keyword, QCoverline))
3607 value = LFACE_OVERLINE (lface);
3608 else if (EQ (keyword, QCstrike_through))
3609 value = LFACE_STRIKE_THROUGH (lface);
3610 else if (EQ (keyword, QCbox))
3611 value = LFACE_BOX (lface);
3612 else if (EQ (keyword, QCinverse_video)
3613 || EQ (keyword, QCreverse_video))
3614 value = LFACE_INVERSE (lface);
3615 else if (EQ (keyword, QCforeground))
3616 value = LFACE_FOREGROUND (lface);
3617 else if (EQ (keyword, QCdistant_foreground))
3618 value = LFACE_DISTANT_FOREGROUND (lface);
3619 else if (EQ (keyword, QCbackground))
3620 value = LFACE_BACKGROUND (lface);
3621 else if (EQ (keyword, QCstipple))
3622 value = LFACE_STIPPLE (lface);
3623 else if (EQ (keyword, QCwidth))
3624 value = LFACE_SWIDTH (lface);
3625 else if (EQ (keyword, QCinherit))
3626 value = LFACE_INHERIT (lface);
3627 else if (EQ (keyword, QCfont))
3628 value = LFACE_FONT (lface);
3629 else if (EQ (keyword, QCfontset))
3630 value = LFACE_FONTSET (lface);
3631 else
3632 signal_error ("Invalid face attribute name", keyword);
3634 if (IGNORE_DEFFACE_P (value))
3635 return Qunspecified;
3637 return value;
3641 DEFUN ("internal-lisp-face-attribute-values",
3642 Finternal_lisp_face_attribute_values,
3643 Sinternal_lisp_face_attribute_values, 1, 1, 0,
3644 doc: /* Return a list of valid discrete values for face attribute ATTR.
3645 Value is nil if ATTR doesn't have a discrete set of valid values. */)
3646 (Lisp_Object attr)
3648 Lisp_Object result = Qnil;
3650 CHECK_SYMBOL (attr);
3652 if (EQ (attr, QCunderline) || EQ (attr, QCoverline)
3653 || EQ (attr, QCstrike_through)
3654 || EQ (attr, QCinverse_video) || EQ (attr, QCreverse_video))
3655 result = list2 (Qt, Qnil);
3657 return result;
3661 DEFUN ("internal-merge-in-global-face", Finternal_merge_in_global_face,
3662 Sinternal_merge_in_global_face, 2, 2, 0,
3663 doc: /* Add attributes from frame-default definition of FACE to FACE on FRAME.
3664 Default face attributes override any local face attributes. */)
3665 (Lisp_Object face, Lisp_Object frame)
3667 int i;
3668 Lisp_Object global_lface, local_lface, *gvec, *lvec;
3669 struct frame *f = XFRAME (frame);
3671 CHECK_LIVE_FRAME (frame);
3672 global_lface = lface_from_face_name (NULL, face, true);
3673 local_lface = lface_from_face_name (f, face, false);
3674 if (NILP (local_lface))
3675 local_lface = Finternal_make_lisp_face (face, frame);
3677 /* Make every specified global attribute override the local one.
3678 BEWARE!! This is only used from `face-set-after-frame-default' where
3679 the local frame is defined from default specs in `face-defface-spec'
3680 and those should be overridden by global settings. Hence the strange
3681 "global before local" priority. */
3682 lvec = XVECTOR (local_lface)->contents;
3683 gvec = XVECTOR (global_lface)->contents;
3684 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3685 if (IGNORE_DEFFACE_P (gvec[i]))
3686 ASET (local_lface, i, Qunspecified);
3687 else if (! UNSPECIFIEDP (gvec[i]))
3688 ASET (local_lface, i, AREF (global_lface, i));
3690 /* If the default face was changed, update the face cache and the
3691 `font' frame parameter. */
3692 if (EQ (face, Qdefault))
3694 struct face_cache *c = FRAME_FACE_CACHE (f);
3695 struct face *newface, *oldface = FACE_FROM_ID_OR_NULL (f, DEFAULT_FACE_ID);
3696 Lisp_Object attrs[LFACE_VECTOR_SIZE];
3698 /* This can be NULL (e.g., in batch mode). */
3699 if (oldface)
3701 /* Ensure that the face vector is fully specified by merging
3702 the previously-cached vector. */
3703 memcpy (attrs, oldface->lface, sizeof attrs);
3704 merge_face_vectors (f, lvec, attrs, 0);
3705 vcopy (local_lface, 0, attrs, LFACE_VECTOR_SIZE);
3706 newface = realize_face (c, lvec, DEFAULT_FACE_ID);
3708 if ((! UNSPECIFIEDP (gvec[LFACE_FAMILY_INDEX])
3709 || ! UNSPECIFIEDP (gvec[LFACE_FOUNDRY_INDEX])
3710 || ! UNSPECIFIEDP (gvec[LFACE_HEIGHT_INDEX])
3711 || ! UNSPECIFIEDP (gvec[LFACE_WEIGHT_INDEX])
3712 || ! UNSPECIFIEDP (gvec[LFACE_SLANT_INDEX])
3713 || ! UNSPECIFIEDP (gvec[LFACE_SWIDTH_INDEX])
3714 || ! UNSPECIFIEDP (gvec[LFACE_FONT_INDEX]))
3715 && newface->font)
3717 Lisp_Object name = newface->font->props[FONT_NAME_INDEX];
3718 AUTO_FRAME_ARG (arg, Qfont, name);
3719 Fmodify_frame_parameters (frame, arg);
3722 if (STRINGP (gvec[LFACE_FOREGROUND_INDEX]))
3724 AUTO_FRAME_ARG (arg, Qforeground_color,
3725 gvec[LFACE_FOREGROUND_INDEX]);
3726 Fmodify_frame_parameters (frame, arg);
3729 if (STRINGP (gvec[LFACE_BACKGROUND_INDEX]))
3731 AUTO_FRAME_ARG (arg, Qbackground_color,
3732 gvec[LFACE_BACKGROUND_INDEX]);
3733 Fmodify_frame_parameters (frame, arg);
3738 return Qnil;
3742 /* The following function is implemented for compatibility with 20.2.
3743 The function is used in x-resolve-fonts when it is asked to
3744 return fonts with the same size as the font of a face. This is
3745 done in fontset.el. */
3747 DEFUN ("face-font", Fface_font, Sface_font, 1, 3, 0,
3748 doc: /* Return the font name of face FACE, or nil if it is unspecified.
3749 The font name is, by default, for ASCII characters.
3750 If the optional argument FRAME is given, report on face FACE in that frame.
3751 If FRAME is t, report on the defaults for face FACE (for new frames).
3752 The font default for a face is either nil, or a list
3753 of the form (bold), (italic) or (bold italic).
3754 If FRAME is omitted or nil, use the selected frame. And, in this case,
3755 if the optional third argument CHARACTER is given,
3756 return the font name used for CHARACTER. */)
3757 (Lisp_Object face, Lisp_Object frame, Lisp_Object character)
3759 if (EQ (frame, Qt))
3761 Lisp_Object result = Qnil;
3762 Lisp_Object lface = lface_from_face_name (NULL, face, true);
3764 if (!UNSPECIFIEDP (LFACE_WEIGHT (lface))
3765 && !EQ (LFACE_WEIGHT (lface), Qnormal))
3766 result = Fcons (Qbold, result);
3768 if (!UNSPECIFIEDP (LFACE_SLANT (lface))
3769 && !EQ (LFACE_SLANT (lface), Qnormal))
3770 result = Fcons (Qitalic, result);
3772 return result;
3774 else
3776 struct frame *f = decode_live_frame (frame);
3777 int face_id = lookup_named_face (f, face, true);
3778 struct face *fface = FACE_FROM_ID_OR_NULL (f, face_id);
3780 if (! fface)
3781 return Qnil;
3782 #ifdef HAVE_WINDOW_SYSTEM
3783 if (FRAME_WINDOW_P (f) && !NILP (character))
3785 CHECK_CHARACTER (character);
3786 face_id = FACE_FOR_CHAR (f, fface, XINT (character), -1, Qnil);
3787 fface = FACE_FROM_ID_OR_NULL (f, face_id);
3789 return ((fface && fface->font)
3790 ? fface->font->props[FONT_NAME_INDEX]
3791 : Qnil);
3792 #else /* !HAVE_WINDOW_SYSTEM */
3793 return build_string (FRAME_MSDOS_P (f)
3794 ? "ms-dos"
3795 : FRAME_W32_P (f) ? "w32term"
3796 :"tty");
3797 #endif
3802 /* Compare face-attribute values v1 and v2 for equality. Value is true if
3803 all attributes are `equal'. Tries to be fast because this function
3804 is called quite often. */
3806 static bool
3807 face_attr_equal_p (Lisp_Object v1, Lisp_Object v2)
3809 /* Type can differ, e.g. when one attribute is unspecified, i.e. nil,
3810 and the other is specified. */
3811 if (XTYPE (v1) != XTYPE (v2))
3812 return false;
3814 if (EQ (v1, v2))
3815 return true;
3817 switch (XTYPE (v1))
3819 case Lisp_String:
3820 if (SBYTES (v1) != SBYTES (v2))
3821 return false;
3823 return memcmp (SDATA (v1), SDATA (v2), SBYTES (v1)) == 0;
3825 case_Lisp_Int:
3826 case Lisp_Symbol:
3827 return false;
3829 default:
3830 return !NILP (Fequal (v1, v2));
3835 /* Compare face vectors V1 and V2 for equality. Value is true if
3836 all attributes are `equal'. Tries to be fast because this function
3837 is called quite often. */
3839 static bool
3840 lface_equal_p (Lisp_Object *v1, Lisp_Object *v2)
3842 int i;
3843 bool equal_p = true;
3845 for (i = 1; i < LFACE_VECTOR_SIZE && equal_p; ++i)
3846 equal_p = face_attr_equal_p (v1[i], v2[i]);
3848 return equal_p;
3852 DEFUN ("internal-lisp-face-equal-p", Finternal_lisp_face_equal_p,
3853 Sinternal_lisp_face_equal_p, 2, 3, 0,
3854 doc: /* True if FACE1 and FACE2 are equal.
3855 If the optional argument FRAME is given, report on FACE1 and FACE2 in that frame.
3856 If FRAME is t, report on the defaults for FACE1 and FACE2 (for new frames).
3857 If FRAME is omitted or nil, use the selected frame. */)
3858 (Lisp_Object face1, Lisp_Object face2, Lisp_Object frame)
3860 bool equal_p;
3861 struct frame *f;
3862 Lisp_Object lface1, lface2;
3864 /* Don't use decode_window_system_frame here because this function
3865 is called before X frames exist. At that time, if FRAME is nil,
3866 selected_frame will be used which is the frame dumped with
3867 Emacs. That frame is not an X frame. */
3868 f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3870 lface1 = lface_from_face_name (f, face1, true);
3871 lface2 = lface_from_face_name (f, face2, true);
3872 equal_p = lface_equal_p (XVECTOR (lface1)->contents,
3873 XVECTOR (lface2)->contents);
3874 return equal_p ? Qt : Qnil;
3878 DEFUN ("internal-lisp-face-empty-p", Finternal_lisp_face_empty_p,
3879 Sinternal_lisp_face_empty_p, 1, 2, 0,
3880 doc: /* True if FACE has no attribute specified.
3881 If the optional argument FRAME is given, report on face FACE in that frame.
3882 If FRAME is t, report on the defaults for face FACE (for new frames).
3883 If FRAME is omitted or nil, use the selected frame. */)
3884 (Lisp_Object face, Lisp_Object frame)
3886 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3887 Lisp_Object lface = lface_from_face_name (f, face, true);
3888 int i;
3890 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3891 if (!UNSPECIFIEDP (AREF (lface, i)))
3892 break;
3894 return i == LFACE_VECTOR_SIZE ? Qt : Qnil;
3898 DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist,
3899 0, 1, 0,
3900 doc: /* Return an alist of frame-local faces defined on FRAME.
3901 For internal use only. */)
3902 (Lisp_Object frame)
3904 return decode_live_frame (frame)->face_alist;
3908 /* Return a hash code for Lisp string STRING with case ignored. Used
3909 below in computing a hash value for a Lisp face. */
3911 static unsigned
3912 hash_string_case_insensitive (Lisp_Object string)
3914 const unsigned char *s;
3915 unsigned hash = 0;
3916 eassert (STRINGP (string));
3917 for (s = SDATA (string); *s; ++s)
3918 hash = (hash << 1) ^ c_tolower (*s);
3919 return hash;
3923 /* Return a hash code for face attribute vector V. */
3925 static unsigned
3926 lface_hash (Lisp_Object *v)
3928 return (hash_string_case_insensitive (v[LFACE_FAMILY_INDEX])
3929 ^ hash_string_case_insensitive (v[LFACE_FOUNDRY_INDEX])
3930 ^ hash_string_case_insensitive (v[LFACE_FOREGROUND_INDEX])
3931 ^ hash_string_case_insensitive (v[LFACE_BACKGROUND_INDEX])
3932 ^ XHASH (v[LFACE_WEIGHT_INDEX])
3933 ^ XHASH (v[LFACE_SLANT_INDEX])
3934 ^ XHASH (v[LFACE_SWIDTH_INDEX])
3935 ^ XHASH (v[LFACE_HEIGHT_INDEX]));
3938 #ifdef HAVE_WINDOW_SYSTEM
3940 /* Return true if LFACE1 and LFACE2 specify the same font (without
3941 considering charsets/registries). They do if they specify the same
3942 family, point size, weight, width, slant, and font. Both
3943 LFACE1 and LFACE2 must be fully-specified. */
3945 static bool
3946 lface_same_font_attributes_p (Lisp_Object *lface1, Lisp_Object *lface2)
3948 eassert (lface_fully_specified_p (lface1)
3949 && lface_fully_specified_p (lface2));
3950 return (xstrcasecmp (SSDATA (lface1[LFACE_FAMILY_INDEX]),
3951 SSDATA (lface2[LFACE_FAMILY_INDEX])) == 0
3952 && xstrcasecmp (SSDATA (lface1[LFACE_FOUNDRY_INDEX]),
3953 SSDATA (lface2[LFACE_FOUNDRY_INDEX])) == 0
3954 && EQ (lface1[LFACE_HEIGHT_INDEX], lface2[LFACE_HEIGHT_INDEX])
3955 && EQ (lface1[LFACE_SWIDTH_INDEX], lface2[LFACE_SWIDTH_INDEX])
3956 && EQ (lface1[LFACE_WEIGHT_INDEX], lface2[LFACE_WEIGHT_INDEX])
3957 && EQ (lface1[LFACE_SLANT_INDEX], lface2[LFACE_SLANT_INDEX])
3958 && EQ (lface1[LFACE_FONT_INDEX], lface2[LFACE_FONT_INDEX])
3959 && (EQ (lface1[LFACE_FONTSET_INDEX], lface2[LFACE_FONTSET_INDEX])
3960 || (STRINGP (lface1[LFACE_FONTSET_INDEX])
3961 && STRINGP (lface2[LFACE_FONTSET_INDEX])
3962 && ! xstrcasecmp (SSDATA (lface1[LFACE_FONTSET_INDEX]),
3963 SSDATA (lface2[LFACE_FONTSET_INDEX]))))
3967 #endif /* HAVE_WINDOW_SYSTEM */
3969 /***********************************************************************
3970 Realized Faces
3971 ***********************************************************************/
3973 /* Allocate and return a new realized face for Lisp face attribute
3974 vector ATTR. */
3976 static struct face *
3977 make_realized_face (Lisp_Object *attr)
3979 enum { off = offsetof (struct face, id) };
3980 struct face *face = xmalloc (sizeof *face);
3982 memcpy (face->lface, attr, sizeof face->lface);
3983 memset (&face->id, 0, sizeof *face - off);
3984 face->ascii_face = face;
3986 return face;
3990 /* Free realized face FACE, including its X resources. FACE may
3991 be null. */
3993 static void
3994 free_realized_face (struct frame *f, struct face *face)
3996 if (face)
3998 #ifdef HAVE_WINDOW_SYSTEM
3999 if (FRAME_WINDOW_P (f))
4001 /* Free fontset of FACE if it is ASCII face. */
4002 if (face->fontset >= 0 && face == face->ascii_face)
4003 free_face_fontset (f, face);
4004 if (face->gc)
4006 block_input ();
4007 if (face->font)
4008 font_done_for_face (f, face);
4009 x_free_gc (f, face->gc);
4010 face->gc = 0;
4011 unblock_input ();
4013 #ifdef HAVE_X_WINDOWS
4014 free_face_colors (f, face);
4015 #endif /* HAVE_X_WINDOWS */
4016 x_destroy_bitmap (f, face->stipple);
4018 #endif /* HAVE_WINDOW_SYSTEM */
4020 xfree (face);
4024 #ifdef HAVE_WINDOW_SYSTEM
4026 /* Prepare face FACE for subsequent display on frame F. This must be called
4027 before using X resources of FACE to allocate GCs if they haven't been
4028 allocated yet or have been freed by clearing the face cache. */
4030 void
4031 prepare_face_for_display (struct frame *f, struct face *face)
4033 eassert (FRAME_WINDOW_P (f));
4035 if (face->gc == 0)
4037 XGCValues xgcv;
4038 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
4040 xgcv.foreground = face->foreground;
4041 xgcv.background = face->background;
4042 #ifdef HAVE_X_WINDOWS
4043 xgcv.graphics_exposures = False;
4044 #endif
4046 block_input ();
4047 #ifdef HAVE_X_WINDOWS
4048 if (face->stipple)
4050 xgcv.fill_style = FillOpaqueStippled;
4051 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
4052 mask |= GCFillStyle | GCStipple;
4054 #endif
4055 face->gc = x_create_gc (f, mask, &xgcv);
4056 if (face->font)
4057 font_prepare_for_face (f, face);
4058 unblock_input ();
4062 #endif /* HAVE_WINDOW_SYSTEM */
4064 /* Returns the `distance' between the colors X and Y. */
4066 static int
4067 color_distance (XColor *x, XColor *y)
4069 /* This formula is from a paper titled `Colour metric' by Thiadmer Riemersma.
4070 Quoting from that paper:
4072 This formula has results that are very close to L*u*v* (with the
4073 modified lightness curve) and, more importantly, it is a more even
4074 algorithm: it does not have a range of colors where it suddenly
4075 gives far from optimal results.
4077 See <http://www.compuphase.com/cmetric.htm> for more info. */
4079 long r = (x->red - y->red) >> 8;
4080 long g = (x->green - y->green) >> 8;
4081 long b = (x->blue - y->blue) >> 8;
4082 long r_mean = (x->red + y->red) >> 9;
4084 return
4085 (((512 + r_mean) * r * r) >> 8)
4086 + 4 * g * g
4087 + (((767 - r_mean) * b * b) >> 8);
4091 DEFUN ("color-distance", Fcolor_distance, Scolor_distance, 2, 4, 0,
4092 doc: /* Return an integer distance between COLOR1 and COLOR2 on FRAME.
4093 COLOR1 and COLOR2 may be either strings containing the color name,
4094 or lists of the form (RED GREEN BLUE), each in the range 0 to 65535 inclusive.
4095 If FRAME is unspecified or nil, the current frame is used.
4096 If METRIC is specified, it should be a function that accepts
4097 two lists of the form (RED GREEN BLUE) aforementioned. */)
4098 (Lisp_Object color1, Lisp_Object color2, Lisp_Object frame,
4099 Lisp_Object metric)
4101 struct frame *f = decode_live_frame (frame);
4102 XColor cdef1, cdef2;
4104 if (!(CONSP (color1) && parse_rgb_list (color1, &cdef1))
4105 && !(STRINGP (color1)
4106 && defined_color (f, SSDATA (color1), &cdef1, false)))
4107 signal_error ("Invalid color", color1);
4108 if (!(CONSP (color2) && parse_rgb_list (color2, &cdef2))
4109 && !(STRINGP (color2)
4110 && defined_color (f, SSDATA (color2), &cdef2, false)))
4111 signal_error ("Invalid color", color2);
4113 if (NILP (metric))
4114 return make_number (color_distance (&cdef1, &cdef2));
4115 else
4116 return call2 (metric,
4117 list3 (make_number (cdef1.red),
4118 make_number (cdef1.green),
4119 make_number (cdef1.blue)),
4120 list3 (make_number (cdef2.red),
4121 make_number (cdef2.green),
4122 make_number (cdef2.blue)));
4126 /***********************************************************************
4127 Face Cache
4128 ***********************************************************************/
4130 /* Return a new face cache for frame F. */
4132 static struct face_cache *
4133 make_face_cache (struct frame *f)
4135 struct face_cache *c = xmalloc (sizeof *c);
4137 c->buckets = xzalloc (FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets);
4138 c->size = 50;
4139 c->used = 0;
4140 c->faces_by_id = xmalloc (c->size * sizeof *c->faces_by_id);
4141 c->f = f;
4142 c->menu_face_changed_p = menu_face_changed_default;
4143 return c;
4146 #ifdef HAVE_WINDOW_SYSTEM
4148 /* Clear out all graphics contexts for all realized faces, except for
4149 the basic faces. This should be done from time to time just to avoid
4150 keeping too many graphics contexts that are no longer needed. */
4152 static void
4153 clear_face_gcs (struct face_cache *c)
4155 if (c && FRAME_WINDOW_P (c->f))
4157 int i;
4158 for (i = BASIC_FACE_ID_SENTINEL; i < c->used; ++i)
4160 struct face *face = c->faces_by_id[i];
4161 if (face && face->gc)
4163 block_input ();
4164 if (face->font)
4165 font_done_for_face (c->f, face);
4166 x_free_gc (c->f, face->gc);
4167 face->gc = 0;
4168 unblock_input ();
4174 #endif /* HAVE_WINDOW_SYSTEM */
4176 /* Free all realized faces in face cache C, including basic faces.
4177 C may be null. If faces are freed, make sure the frame's current
4178 matrix is marked invalid, so that a display caused by an expose
4179 event doesn't try to use faces we destroyed. */
4181 static void
4182 free_realized_faces (struct face_cache *c)
4184 if (c && c->used)
4186 int i, size;
4187 struct frame *f = c->f;
4189 /* We must block input here because we can't process X events
4190 safely while only some faces are freed, or when the frame's
4191 current matrix still references freed faces. */
4192 block_input ();
4194 for (i = 0; i < c->used; ++i)
4196 free_realized_face (f, c->faces_by_id[i]);
4197 c->faces_by_id[i] = NULL;
4200 /* Forget the escape-glyph and glyphless-char faces. */
4201 forget_escape_and_glyphless_faces ();
4202 c->used = 0;
4203 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4204 memset (c->buckets, 0, size);
4206 /* Must do a thorough redisplay the next time. Mark current
4207 matrices as invalid because they will reference faces freed
4208 above. This function is also called when a frame is
4209 destroyed. In this case, the root window of F is nil. */
4210 if (WINDOWP (f->root_window))
4212 clear_current_matrices (f);
4213 fset_redisplay (f);
4216 unblock_input ();
4221 /* Free all realized faces on FRAME or on all frames if FRAME is nil.
4222 This is done after attributes of a named face have been changed,
4223 because we can't tell which realized faces depend on that face. */
4225 void
4226 free_all_realized_faces (Lisp_Object frame)
4228 if (NILP (frame))
4230 Lisp_Object rest;
4231 FOR_EACH_FRAME (rest, frame)
4232 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4233 windows_or_buffers_changed = 58;
4235 else
4236 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4240 /* Free face cache C and faces in it, including their X resources. */
4242 static void
4243 free_face_cache (struct face_cache *c)
4245 if (c)
4247 free_realized_faces (c);
4248 xfree (c->buckets);
4249 xfree (c->faces_by_id);
4250 xfree (c);
4255 /* Cache realized face FACE in face cache C. HASH is the hash value
4256 of FACE. If FACE is for ASCII characters (i.e. FACE->ascii_face ==
4257 FACE), insert the new face to the beginning of the collision list
4258 of the face hash table of C. Otherwise, add the new face to the
4259 end of the collision list. This way, lookup_face can quickly find
4260 that a requested face is not cached. */
4262 static void
4263 cache_face (struct face_cache *c, struct face *face, unsigned int hash)
4265 int i = hash % FACE_CACHE_BUCKETS_SIZE;
4267 face->hash = hash;
4269 if (face->ascii_face != face)
4271 struct face *last = c->buckets[i];
4272 if (last)
4274 while (last->next)
4275 last = last->next;
4276 last->next = face;
4277 face->prev = last;
4278 face->next = NULL;
4280 else
4282 c->buckets[i] = face;
4283 face->prev = face->next = NULL;
4286 else
4288 face->prev = NULL;
4289 face->next = c->buckets[i];
4290 if (face->next)
4291 face->next->prev = face;
4292 c->buckets[i] = face;
4295 /* Find a free slot in C->faces_by_id and use the index of the free
4296 slot as FACE->id. */
4297 for (i = 0; i < c->used; ++i)
4298 if (c->faces_by_id[i] == NULL)
4299 break;
4300 face->id = i;
4302 #ifdef GLYPH_DEBUG
4303 /* Check that FACE got a unique id. */
4305 int j, n;
4306 struct face *face1;
4308 for (j = n = 0; j < FACE_CACHE_BUCKETS_SIZE; ++j)
4309 for (face1 = c->buckets[j]; face1; face1 = face1->next)
4310 if (face1->id == i)
4311 ++n;
4313 eassert (n == 1);
4315 #endif /* GLYPH_DEBUG */
4317 /* Maybe enlarge C->faces_by_id. */
4318 if (i == c->used)
4320 if (c->used == c->size)
4321 c->faces_by_id = xpalloc (c->faces_by_id, &c->size, 1, MAX_FACE_ID,
4322 sizeof *c->faces_by_id);
4323 c->used++;
4326 c->faces_by_id[i] = face;
4330 /* Remove face FACE from cache C. */
4332 static void
4333 uncache_face (struct face_cache *c, struct face *face)
4335 int i = face->hash % FACE_CACHE_BUCKETS_SIZE;
4337 if (face->prev)
4338 face->prev->next = face->next;
4339 else
4340 c->buckets[i] = face->next;
4342 if (face->next)
4343 face->next->prev = face->prev;
4345 c->faces_by_id[face->id] = NULL;
4346 if (face->id == c->used)
4347 --c->used;
4351 /* Look up a realized face with face attributes ATTR in the face cache
4352 of frame F. The face will be used to display ASCII characters.
4353 Value is the ID of the face found. If no suitable face is found,
4354 realize a new one. */
4356 static int
4357 lookup_face (struct frame *f, Lisp_Object *attr)
4359 struct face_cache *cache = FRAME_FACE_CACHE (f);
4360 unsigned hash;
4361 int i;
4362 struct face *face;
4364 eassert (cache != NULL);
4365 check_lface_attrs (attr);
4367 /* Look up ATTR in the face cache. */
4368 hash = lface_hash (attr);
4369 i = hash % FACE_CACHE_BUCKETS_SIZE;
4371 for (face = cache->buckets[i]; face; face = face->next)
4373 if (face->ascii_face != face)
4375 /* There's no more ASCII face. */
4376 face = NULL;
4377 break;
4379 if (face->hash == hash
4380 && lface_equal_p (face->lface, attr))
4381 break;
4384 /* If not found, realize a new face. */
4385 if (face == NULL)
4386 face = realize_face (cache, attr, -1);
4388 #ifdef GLYPH_DEBUG
4389 eassert (face == FACE_FROM_ID_OR_NULL (f, face->id));
4390 #endif /* GLYPH_DEBUG */
4392 return face->id;
4395 #ifdef HAVE_WINDOW_SYSTEM
4396 /* Look up a realized face that has the same attributes as BASE_FACE
4397 except for the font in the face cache of frame F. If FONT-OBJECT
4398 is not nil, it is an already opened font. If FONT-OBJECT is nil,
4399 the face has no font. Value is the ID of the face found. If no
4400 suitable face is found, realize a new one. */
4403 face_for_font (struct frame *f, Lisp_Object font_object, struct face *base_face)
4405 struct face_cache *cache = FRAME_FACE_CACHE (f);
4406 unsigned hash;
4407 int i;
4408 struct face *face;
4410 eassert (cache != NULL);
4411 base_face = base_face->ascii_face;
4412 hash = lface_hash (base_face->lface);
4413 i = hash % FACE_CACHE_BUCKETS_SIZE;
4415 for (face = cache->buckets[i]; face; face = face->next)
4417 if (face->ascii_face == face)
4418 continue;
4419 if (face->ascii_face == base_face
4420 && face->font == (NILP (font_object) ? NULL
4421 : XFONT_OBJECT (font_object))
4422 && lface_equal_p (face->lface, base_face->lface))
4423 return face->id;
4426 /* If not found, realize a new face. */
4427 face = realize_non_ascii_face (f, font_object, base_face);
4428 return face->id;
4430 #endif /* HAVE_WINDOW_SYSTEM */
4432 /* Return the face id of the realized face for named face SYMBOL on
4433 frame F suitable for displaying ASCII characters. Value is -1 if
4434 the face couldn't be determined, which might happen if the default
4435 face isn't realized and cannot be realized. */
4438 lookup_named_face (struct frame *f, Lisp_Object symbol, bool signal_p)
4440 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4441 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4442 struct face *default_face = FACE_FROM_ID_OR_NULL (f, DEFAULT_FACE_ID);
4444 if (default_face == NULL)
4446 if (!realize_basic_faces (f))
4447 return -1;
4448 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4451 if (! get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4452 return -1;
4454 memcpy (attrs, default_face->lface, sizeof attrs);
4455 merge_face_vectors (f, symbol_attrs, attrs, 0);
4457 return lookup_face (f, attrs);
4461 /* Return the display face-id of the basic face whose canonical face-id
4462 is FACE_ID. The return value will usually simply be FACE_ID, unless that
4463 basic face has bee remapped via Vface_remapping_alist. This function is
4464 conservative: if something goes wrong, it will simply return FACE_ID
4465 rather than signal an error. */
4468 lookup_basic_face (struct frame *f, int face_id)
4470 Lisp_Object name, mapping;
4471 int remapped_face_id;
4473 if (NILP (Vface_remapping_alist))
4474 return face_id; /* Nothing to do. */
4476 switch (face_id)
4478 case DEFAULT_FACE_ID: name = Qdefault; break;
4479 case MODE_LINE_FACE_ID: name = Qmode_line; break;
4480 case MODE_LINE_INACTIVE_FACE_ID: name = Qmode_line_inactive; break;
4481 case HEADER_LINE_FACE_ID: name = Qheader_line; break;
4482 case TOOL_BAR_FACE_ID: name = Qtool_bar; break;
4483 case FRINGE_FACE_ID: name = Qfringe; break;
4484 case SCROLL_BAR_FACE_ID: name = Qscroll_bar; break;
4485 case BORDER_FACE_ID: name = Qborder; break;
4486 case CURSOR_FACE_ID: name = Qcursor; break;
4487 case MOUSE_FACE_ID: name = Qmouse; break;
4488 case MENU_FACE_ID: name = Qmenu; break;
4489 case WINDOW_DIVIDER_FACE_ID: name = Qwindow_divider; break;
4490 case VERTICAL_BORDER_FACE_ID: name = Qvertical_border; break;
4491 case WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID: name = Qwindow_divider_first_pixel; break;
4492 case WINDOW_DIVIDER_LAST_PIXEL_FACE_ID: name = Qwindow_divider_last_pixel; break;
4493 case INTERNAL_BORDER_FACE_ID: name = Qinternal_border; break;
4495 default:
4496 emacs_abort (); /* the caller is supposed to pass us a basic face id */
4499 /* Do a quick scan through Vface_remapping_alist, and return immediately
4500 if there is no remapping for face NAME. This is just an optimization
4501 for the very common no-remapping case. */
4502 mapping = assq_no_quit (name, Vface_remapping_alist);
4503 if (NILP (mapping))
4504 return face_id; /* Give up. */
4506 /* If there is a remapping entry, lookup the face using NAME, which will
4507 handle the remapping too. */
4508 remapped_face_id = lookup_named_face (f, name, false);
4509 if (remapped_face_id < 0)
4510 return face_id; /* Give up. */
4512 return remapped_face_id;
4516 /* Return a face for charset ASCII that is like the face with id
4517 FACE_ID on frame F, but has a font that is STEPS steps smaller.
4518 STEPS < 0 means larger. Value is the id of the face. */
4521 smaller_face (struct frame *f, int face_id, int steps)
4523 #ifdef HAVE_WINDOW_SYSTEM
4524 struct face *face;
4525 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4526 int pt, last_pt, last_height;
4527 int delta;
4528 int new_face_id;
4529 struct face *new_face;
4531 /* If not called for an X frame, just return the original face. */
4532 if (FRAME_TERMCAP_P (f))
4533 return face_id;
4535 /* Try in increments of 1/2 pt. */
4536 delta = steps < 0 ? 5 : -5;
4537 steps = eabs (steps);
4539 face = FACE_FROM_ID (f, face_id);
4540 memcpy (attrs, face->lface, sizeof attrs);
4541 pt = last_pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
4542 new_face_id = face_id;
4543 last_height = FONT_HEIGHT (face->font);
4545 while (steps
4546 && pt + delta > 0
4547 /* Give up if we cannot find a font within 10pt. */
4548 && eabs (last_pt - pt) < 100)
4550 /* Look up a face for a slightly smaller/larger font. */
4551 pt += delta;
4552 attrs[LFACE_HEIGHT_INDEX] = make_number (pt);
4553 new_face_id = lookup_face (f, attrs);
4554 new_face = FACE_FROM_ID (f, new_face_id);
4556 /* If height changes, count that as one step. */
4557 if ((delta < 0 && FONT_HEIGHT (new_face->font) < last_height)
4558 || (delta > 0 && FONT_HEIGHT (new_face->font) > last_height))
4560 --steps;
4561 last_height = FONT_HEIGHT (new_face->font);
4562 last_pt = pt;
4566 return new_face_id;
4568 #else /* not HAVE_WINDOW_SYSTEM */
4570 return face_id;
4572 #endif /* not HAVE_WINDOW_SYSTEM */
4576 /* Return a face for charset ASCII that is like the face with id
4577 FACE_ID on frame F, but has height HEIGHT. */
4580 face_with_height (struct frame *f, int face_id, int height)
4582 #ifdef HAVE_WINDOW_SYSTEM
4583 struct face *face;
4584 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4586 if (FRAME_TERMCAP_P (f)
4587 || height <= 0)
4588 return face_id;
4590 face = FACE_FROM_ID (f, face_id);
4591 memcpy (attrs, face->lface, sizeof attrs);
4592 attrs[LFACE_HEIGHT_INDEX] = make_number (height);
4593 font_clear_prop (attrs, FONT_SIZE_INDEX);
4594 face_id = lookup_face (f, attrs);
4595 #endif /* HAVE_WINDOW_SYSTEM */
4597 return face_id;
4601 /* Return the face id of the realized face for named face SYMBOL on
4602 frame F suitable for displaying ASCII characters, and use
4603 attributes of the face FACE_ID for attributes that aren't
4604 completely specified by SYMBOL. This is like lookup_named_face,
4605 except that the default attributes come from FACE_ID, not from the
4606 default face. FACE_ID is assumed to be already realized. */
4609 lookup_derived_face (struct frame *f, Lisp_Object symbol, int face_id,
4610 bool signal_p)
4612 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4613 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4614 struct face *default_face;
4616 if (!get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4617 return -1;
4619 default_face = FACE_FROM_ID (f, face_id);
4620 memcpy (attrs, default_face->lface, sizeof attrs);
4621 merge_face_vectors (f, symbol_attrs, attrs, 0);
4622 return lookup_face (f, attrs);
4625 DEFUN ("face-attributes-as-vector", Fface_attributes_as_vector,
4626 Sface_attributes_as_vector, 1, 1, 0,
4627 doc: /* Return a vector of face attributes corresponding to PLIST. */)
4628 (Lisp_Object plist)
4630 Lisp_Object lface;
4631 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
4632 Qunspecified);
4633 merge_face_ref (XFRAME (selected_frame), plist, XVECTOR (lface)->contents,
4634 true, 0);
4635 return lface;
4640 /***********************************************************************
4641 Face capability testing
4642 ***********************************************************************/
4645 /* If the distance (as returned by color_distance) between two colors is
4646 less than this, then they are considered the same, for determining
4647 whether a color is supported or not. The range of values is 0-65535. */
4649 #define TTY_SAME_COLOR_THRESHOLD 10000
4651 #ifdef HAVE_WINDOW_SYSTEM
4653 /* Return true if all the face attributes in ATTRS are supported
4654 on the window-system frame F.
4656 The definition of `supported' is somewhat heuristic, but basically means
4657 that a face containing all the attributes in ATTRS, when merged with the
4658 default face for display, can be represented in a way that's
4660 (1) different in appearance than the default face, and
4661 (2) `close in spirit' to what the attributes specify, if not exact. */
4663 static bool
4664 x_supports_face_attributes_p (struct frame *f,
4665 Lisp_Object attrs[LFACE_VECTOR_SIZE],
4666 struct face *def_face)
4668 Lisp_Object *def_attrs = def_face->lface;
4670 /* Check that other specified attributes are different that the default
4671 face. */
4672 if ((!UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
4673 && face_attr_equal_p (attrs[LFACE_UNDERLINE_INDEX],
4674 def_attrs[LFACE_UNDERLINE_INDEX]))
4675 || (!UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
4676 && face_attr_equal_p (attrs[LFACE_INVERSE_INDEX],
4677 def_attrs[LFACE_INVERSE_INDEX]))
4678 || (!UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
4679 && face_attr_equal_p (attrs[LFACE_FOREGROUND_INDEX],
4680 def_attrs[LFACE_FOREGROUND_INDEX]))
4681 || (!UNSPECIFIEDP (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
4682 && face_attr_equal_p (attrs[LFACE_DISTANT_FOREGROUND_INDEX],
4683 def_attrs[LFACE_DISTANT_FOREGROUND_INDEX]))
4684 || (!UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
4685 && face_attr_equal_p (attrs[LFACE_BACKGROUND_INDEX],
4686 def_attrs[LFACE_BACKGROUND_INDEX]))
4687 || (!UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4688 && face_attr_equal_p (attrs[LFACE_STIPPLE_INDEX],
4689 def_attrs[LFACE_STIPPLE_INDEX]))
4690 || (!UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4691 && face_attr_equal_p (attrs[LFACE_OVERLINE_INDEX],
4692 def_attrs[LFACE_OVERLINE_INDEX]))
4693 || (!UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4694 && face_attr_equal_p (attrs[LFACE_STRIKE_THROUGH_INDEX],
4695 def_attrs[LFACE_STRIKE_THROUGH_INDEX]))
4696 || (!UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
4697 && face_attr_equal_p (attrs[LFACE_BOX_INDEX],
4698 def_attrs[LFACE_BOX_INDEX])))
4699 return false;
4701 /* Check font-related attributes, as those are the most commonly
4702 "unsupported" on a window-system (because of missing fonts). */
4703 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4704 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4705 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4706 || !UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
4707 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
4708 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX]))
4710 int face_id;
4711 struct face *face;
4712 Lisp_Object merged_attrs[LFACE_VECTOR_SIZE];
4713 int i;
4715 memcpy (merged_attrs, def_attrs, sizeof merged_attrs);
4717 merge_face_vectors (f, attrs, merged_attrs, 0);
4719 face_id = lookup_face (f, merged_attrs);
4720 face = FACE_FROM_ID_OR_NULL (f, face_id);
4722 if (! face)
4723 error ("Cannot make face");
4725 /* If the font is the same, or no font is found, then not
4726 supported. */
4727 if (face->font == def_face->font
4728 || ! face->font)
4729 return false;
4730 for (i = FONT_TYPE_INDEX; i <= FONT_SIZE_INDEX; i++)
4731 if (! EQ (face->font->props[i], def_face->font->props[i]))
4733 Lisp_Object s1, s2;
4735 if (i < FONT_FOUNDRY_INDEX || i > FONT_REGISTRY_INDEX
4736 || face->font->driver->case_sensitive)
4737 return true;
4738 s1 = SYMBOL_NAME (face->font->props[i]);
4739 s2 = SYMBOL_NAME (def_face->font->props[i]);
4740 if (! EQ (Fcompare_strings (s1, make_number (0), Qnil,
4741 s2, make_number (0), Qnil, Qt), Qt))
4742 return true;
4744 return false;
4747 /* Everything checks out, this face is supported. */
4748 return true;
4751 #endif /* HAVE_WINDOW_SYSTEM */
4753 /* Return true if all the face attributes in ATTRS are supported
4754 on the tty frame F.
4756 The definition of `supported' is somewhat heuristic, but basically means
4757 that a face containing all the attributes in ATTRS, when merged
4758 with the default face for display, can be represented in a way that's
4760 (1) different in appearance than the default face, and
4761 (2) `close in spirit' to what the attributes specify, if not exact.
4763 Point (2) implies that a `:weight black' attribute will be satisfied
4764 by any terminal that can display bold, and a `:foreground "yellow"' as
4765 long as the terminal can display a yellowish color, but `:slant italic'
4766 will _not_ be satisfied by the tty display code's automatic
4767 substitution of a `dim' face for italic. */
4769 static bool
4770 tty_supports_face_attributes_p (struct frame *f,
4771 Lisp_Object attrs[LFACE_VECTOR_SIZE],
4772 struct face *def_face)
4774 int weight, slant;
4775 Lisp_Object val, fg, bg;
4776 XColor fg_tty_color, fg_std_color;
4777 XColor bg_tty_color, bg_std_color;
4778 unsigned test_caps = 0;
4779 Lisp_Object *def_attrs = def_face->lface;
4781 /* First check some easy-to-check stuff; ttys support none of the
4782 following attributes, so we can just return false if any are requested
4783 (even if `nominal' values are specified, we should still return false,
4784 as that will be the same value that the default face uses). We
4785 consider :slant unsupportable on ttys, even though the face code
4786 actually `fakes' them using a dim attribute if possible. This is
4787 because the faked result is too different from what the face
4788 specifies. */
4789 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4790 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4791 || !UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4792 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4793 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
4794 || !UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4795 || !UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4796 || !UNSPECIFIEDP (attrs[LFACE_BOX_INDEX]))
4797 return false;
4799 /* Test for terminal `capabilities' (non-color character attributes). */
4801 /* font weight (bold/dim) */
4802 val = attrs[LFACE_WEIGHT_INDEX];
4803 if (!UNSPECIFIEDP (val)
4804 && (weight = FONT_WEIGHT_NAME_NUMERIC (val), weight >= 0))
4806 int def_weight = FONT_WEIGHT_NAME_NUMERIC (def_attrs[LFACE_WEIGHT_INDEX]);
4808 if (weight > 100)
4810 if (def_weight > 100)
4811 return false; /* same as default */
4812 test_caps = TTY_CAP_BOLD;
4814 else if (weight < 100)
4816 if (def_weight < 100)
4817 return false; /* same as default */
4818 test_caps = TTY_CAP_DIM;
4820 else if (def_weight == 100)
4821 return false; /* same as default */
4824 /* font slant */
4825 val = attrs[LFACE_SLANT_INDEX];
4826 if (!UNSPECIFIEDP (val)
4827 && (slant = FONT_SLANT_NAME_NUMERIC (val), slant >= 0))
4829 int def_slant = FONT_SLANT_NAME_NUMERIC (def_attrs[LFACE_SLANT_INDEX]);
4830 if (slant == 100 || slant == def_slant)
4831 return false; /* same as default */
4832 else
4833 test_caps |= TTY_CAP_ITALIC;
4836 /* underlining */
4837 val = attrs[LFACE_UNDERLINE_INDEX];
4838 if (!UNSPECIFIEDP (val))
4840 if (STRINGP (val))
4841 return false; /* ttys can't use colored underlines */
4842 else if (EQ (CAR_SAFE (val), QCstyle) && EQ (CAR_SAFE (CDR_SAFE (val)), Qwave))
4843 return false; /* ttys can't use wave underlines */
4844 else if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
4845 return false; /* same as default */
4846 else
4847 test_caps |= TTY_CAP_UNDERLINE;
4850 /* inverse video */
4851 val = attrs[LFACE_INVERSE_INDEX];
4852 if (!UNSPECIFIEDP (val))
4854 if (face_attr_equal_p (val, def_attrs[LFACE_INVERSE_INDEX]))
4855 return false; /* same as default */
4856 else
4857 test_caps |= TTY_CAP_INVERSE;
4861 /* Color testing. */
4863 /* Check if foreground color is close enough. */
4864 fg = attrs[LFACE_FOREGROUND_INDEX];
4865 if (STRINGP (fg))
4867 Lisp_Object def_fg = def_attrs[LFACE_FOREGROUND_INDEX];
4869 if (face_attr_equal_p (fg, def_fg))
4870 return false; /* same as default */
4871 else if (! tty_lookup_color (f, fg, &fg_tty_color, &fg_std_color))
4872 return false; /* not a valid color */
4873 else if (color_distance (&fg_tty_color, &fg_std_color)
4874 > TTY_SAME_COLOR_THRESHOLD)
4875 return false; /* displayed color is too different */
4876 else
4877 /* Make sure the color is really different than the default. */
4879 XColor def_fg_color;
4880 if (tty_lookup_color (f, def_fg, &def_fg_color, 0)
4881 && (color_distance (&fg_tty_color, &def_fg_color)
4882 <= TTY_SAME_COLOR_THRESHOLD))
4883 return false;
4887 /* Check if background color is close enough. */
4888 bg = attrs[LFACE_BACKGROUND_INDEX];
4889 if (STRINGP (bg))
4891 Lisp_Object def_bg = def_attrs[LFACE_BACKGROUND_INDEX];
4893 if (face_attr_equal_p (bg, def_bg))
4894 return false; /* same as default */
4895 else if (! tty_lookup_color (f, bg, &bg_tty_color, &bg_std_color))
4896 return false; /* not a valid color */
4897 else if (color_distance (&bg_tty_color, &bg_std_color)
4898 > TTY_SAME_COLOR_THRESHOLD)
4899 return false; /* displayed color is too different */
4900 else
4901 /* Make sure the color is really different than the default. */
4903 XColor def_bg_color;
4904 if (tty_lookup_color (f, def_bg, &def_bg_color, 0)
4905 && (color_distance (&bg_tty_color, &def_bg_color)
4906 <= TTY_SAME_COLOR_THRESHOLD))
4907 return false;
4911 /* If both foreground and background are requested, see if the
4912 distance between them is OK. We just check to see if the distance
4913 between the tty's foreground and background is close enough to the
4914 distance between the standard foreground and background. */
4915 if (STRINGP (fg) && STRINGP (bg))
4917 int delta_delta
4918 = (color_distance (&fg_std_color, &bg_std_color)
4919 - color_distance (&fg_tty_color, &bg_tty_color));
4920 if (delta_delta > TTY_SAME_COLOR_THRESHOLD
4921 || delta_delta < -TTY_SAME_COLOR_THRESHOLD)
4922 return false;
4926 /* See if the capabilities we selected above are supported, with the
4927 given colors. */
4928 return tty_capable_p (FRAME_TTY (f), test_caps);
4932 DEFUN ("display-supports-face-attributes-p",
4933 Fdisplay_supports_face_attributes_p, Sdisplay_supports_face_attributes_p,
4934 1, 2, 0,
4935 doc: /* Return non-nil if all the face attributes in ATTRIBUTES are supported.
4936 The optional argument DISPLAY can be a display name, a frame, or
4937 nil (meaning the selected frame's display).
4939 The definition of `supported' is somewhat heuristic, but basically means
4940 that a face containing all the attributes in ATTRIBUTES, when merged
4941 with the default face for display, can be represented in a way that's
4943 (1) different in appearance than the default face, and
4944 (2) `close in spirit' to what the attributes specify, if not exact.
4946 Point (2) implies that a `:weight black' attribute will be satisfied by
4947 any display that can display bold, and a `:foreground \"yellow\"' as long
4948 as it can display a yellowish color, but `:slant italic' will _not_ be
4949 satisfied by the tty display code's automatic substitution of a `dim'
4950 face for italic. */)
4951 (Lisp_Object attributes, Lisp_Object display)
4953 bool supports = false;
4954 int i;
4955 Lisp_Object frame;
4956 struct frame *f;
4957 struct face *def_face;
4958 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4960 if (noninteractive || !initialized)
4961 /* We may not be able to access low-level face information in batch
4962 mode, or before being dumped, and this function is not going to
4963 be very useful in those cases anyway, so just give up. */
4964 return Qnil;
4966 if (NILP (display))
4967 frame = selected_frame;
4968 else if (FRAMEP (display))
4969 frame = display;
4970 else
4972 /* Find any frame on DISPLAY. */
4973 Lisp_Object tail;
4975 frame = Qnil;
4976 FOR_EACH_FRAME (tail, frame)
4977 if (!NILP (Fequal (Fcdr (Fassq (Qdisplay,
4978 XFRAME (frame)->param_alist)),
4979 display)))
4980 break;
4983 CHECK_LIVE_FRAME (frame);
4984 f = XFRAME (frame);
4986 for (i = 0; i < LFACE_VECTOR_SIZE; i++)
4987 attrs[i] = Qunspecified;
4988 merge_face_ref (f, attributes, attrs, true, 0);
4990 def_face = FACE_FROM_ID_OR_NULL (f, DEFAULT_FACE_ID);
4991 if (def_face == NULL)
4993 if (! realize_basic_faces (f))
4994 error ("Cannot realize default face");
4995 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4998 /* Dispatch to the appropriate handler. */
4999 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5000 supports = tty_supports_face_attributes_p (f, attrs, def_face);
5001 #ifdef HAVE_WINDOW_SYSTEM
5002 else
5003 supports = x_supports_face_attributes_p (f, attrs, def_face);
5004 #endif
5006 return supports ? Qt : Qnil;
5010 /***********************************************************************
5011 Font selection
5012 ***********************************************************************/
5014 DEFUN ("internal-set-font-selection-order",
5015 Finternal_set_font_selection_order,
5016 Sinternal_set_font_selection_order, 1, 1, 0,
5017 doc: /* Set font selection order for face font selection to ORDER.
5018 ORDER must be a list of length 4 containing the symbols `:width',
5019 `:height', `:weight', and `:slant'. Face attributes appearing
5020 first in ORDER are matched first, e.g. if `:height' appears before
5021 `:weight' in ORDER, font selection first tries to find a font with
5022 a suitable height, and then tries to match the font weight.
5023 Value is ORDER. */)
5024 (Lisp_Object order)
5026 Lisp_Object list;
5027 int i;
5028 int indices[ARRAYELTS (font_sort_order)];
5030 CHECK_LIST (order);
5031 memset (indices, 0, sizeof indices);
5032 i = 0;
5034 for (list = order;
5035 CONSP (list) && i < ARRAYELTS (indices);
5036 list = XCDR (list), ++i)
5038 Lisp_Object attr = XCAR (list);
5039 int xlfd;
5041 if (EQ (attr, QCwidth))
5042 xlfd = XLFD_SWIDTH;
5043 else if (EQ (attr, QCheight))
5044 xlfd = XLFD_POINT_SIZE;
5045 else if (EQ (attr, QCweight))
5046 xlfd = XLFD_WEIGHT;
5047 else if (EQ (attr, QCslant))
5048 xlfd = XLFD_SLANT;
5049 else
5050 break;
5052 if (indices[i] != 0)
5053 break;
5054 indices[i] = xlfd;
5057 if (!NILP (list) || i != ARRAYELTS (indices))
5058 signal_error ("Invalid font sort order", order);
5059 for (i = 0; i < ARRAYELTS (font_sort_order); ++i)
5060 if (indices[i] == 0)
5061 signal_error ("Invalid font sort order", order);
5063 if (memcmp (indices, font_sort_order, sizeof indices) != 0)
5065 memcpy (font_sort_order, indices, sizeof font_sort_order);
5066 free_all_realized_faces (Qnil);
5069 font_update_sort_order (font_sort_order);
5071 return Qnil;
5075 DEFUN ("internal-set-alternative-font-family-alist",
5076 Finternal_set_alternative_font_family_alist,
5077 Sinternal_set_alternative_font_family_alist, 1, 1, 0,
5078 doc: /* Define alternative font families to try in face font selection.
5079 ALIST is an alist of (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5080 Each ALTERNATIVE is tried in order if no fonts of font family FAMILY can
5081 be found. Value is ALIST. */)
5082 (Lisp_Object alist)
5084 Lisp_Object entry, tail, tail2;
5086 CHECK_LIST (alist);
5087 alist = Fcopy_sequence (alist);
5088 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5090 entry = XCAR (tail);
5091 CHECK_LIST (entry);
5092 entry = Fcopy_sequence (entry);
5093 XSETCAR (tail, entry);
5094 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5095 XSETCAR (tail2, Fintern (XCAR (tail2), Qnil));
5098 Vface_alternative_font_family_alist = alist;
5099 free_all_realized_faces (Qnil);
5100 return alist;
5104 DEFUN ("internal-set-alternative-font-registry-alist",
5105 Finternal_set_alternative_font_registry_alist,
5106 Sinternal_set_alternative_font_registry_alist, 1, 1, 0,
5107 doc: /* Define alternative font registries to try in face font selection.
5108 ALIST is an alist of (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5109 Each ALTERNATIVE is tried in order if no fonts of font registry REGISTRY can
5110 be found. Value is ALIST. */)
5111 (Lisp_Object alist)
5113 Lisp_Object entry, tail, tail2;
5115 CHECK_LIST (alist);
5116 alist = Fcopy_sequence (alist);
5117 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5119 entry = XCAR (tail);
5120 CHECK_LIST (entry);
5121 entry = Fcopy_sequence (entry);
5122 XSETCAR (tail, entry);
5123 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5124 XSETCAR (tail2, Fdowncase (XCAR (tail2)));
5126 Vface_alternative_font_registry_alist = alist;
5127 free_all_realized_faces (Qnil);
5128 return alist;
5132 #ifdef HAVE_WINDOW_SYSTEM
5134 /* Return the fontset id of the base fontset name or alias name given
5135 by the fontset attribute of ATTRS. Value is -1 if the fontset
5136 attribute of ATTRS doesn't name a fontset. */
5138 static int
5139 face_fontset (Lisp_Object attrs[LFACE_VECTOR_SIZE])
5141 Lisp_Object name;
5143 name = attrs[LFACE_FONTSET_INDEX];
5144 if (!STRINGP (name))
5145 return -1;
5146 return fs_query_fontset (name, 0);
5149 #endif /* HAVE_WINDOW_SYSTEM */
5153 /***********************************************************************
5154 Face Realization
5155 ***********************************************************************/
5157 /* Realize basic faces on frame F. Value is zero if frame parameters
5158 of F don't contain enough information needed to realize the default
5159 face. */
5161 static bool
5162 realize_basic_faces (struct frame *f)
5164 bool success_p = false;
5166 /* Block input here so that we won't be surprised by an X expose
5167 event, for instance, without having the faces set up. */
5168 block_input ();
5170 if (realize_default_face (f))
5172 realize_named_face (f, Qmode_line, MODE_LINE_FACE_ID);
5173 realize_named_face (f, Qmode_line_inactive, MODE_LINE_INACTIVE_FACE_ID);
5174 realize_named_face (f, Qtool_bar, TOOL_BAR_FACE_ID);
5175 realize_named_face (f, Qfringe, FRINGE_FACE_ID);
5176 realize_named_face (f, Qheader_line, HEADER_LINE_FACE_ID);
5177 realize_named_face (f, Qscroll_bar, SCROLL_BAR_FACE_ID);
5178 realize_named_face (f, Qborder, BORDER_FACE_ID);
5179 realize_named_face (f, Qcursor, CURSOR_FACE_ID);
5180 realize_named_face (f, Qmouse, MOUSE_FACE_ID);
5181 realize_named_face (f, Qmenu, MENU_FACE_ID);
5182 realize_named_face (f, Qvertical_border, VERTICAL_BORDER_FACE_ID);
5183 realize_named_face (f, Qwindow_divider, WINDOW_DIVIDER_FACE_ID);
5184 realize_named_face (f, Qwindow_divider_first_pixel,
5185 WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
5186 realize_named_face (f, Qwindow_divider_last_pixel,
5187 WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
5188 realize_named_face (f, Qinternal_border, INTERNAL_BORDER_FACE_ID);
5190 /* Reflect changes in the `menu' face in menu bars. */
5191 if (FRAME_FACE_CACHE (f)->menu_face_changed_p)
5193 FRAME_FACE_CACHE (f)->menu_face_changed_p = false;
5194 #ifdef USE_X_TOOLKIT
5195 if (FRAME_WINDOW_P (f))
5196 x_update_menu_appearance (f);
5197 #endif
5200 success_p = true;
5203 unblock_input ();
5204 return success_p;
5208 /* Realize the default face on frame F. If the face is not fully
5209 specified, make it fully-specified. Attributes of the default face
5210 that are not explicitly specified are taken from frame parameters. */
5212 static bool
5213 realize_default_face (struct frame *f)
5215 struct face_cache *c = FRAME_FACE_CACHE (f);
5216 Lisp_Object lface;
5217 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5219 /* If the `default' face is not yet known, create it. */
5220 lface = lface_from_face_name (f, Qdefault, false);
5221 if (NILP (lface))
5223 Lisp_Object frame;
5224 XSETFRAME (frame, f);
5225 lface = Finternal_make_lisp_face (Qdefault, frame);
5228 #ifdef HAVE_WINDOW_SYSTEM
5229 if (FRAME_WINDOW_P (f))
5231 Lisp_Object font_object;
5233 XSETFONT (font_object, FRAME_FONT (f));
5234 set_lface_from_font (f, lface, font_object, f->default_face_done_p);
5235 ASET (lface, LFACE_FONTSET_INDEX, fontset_name (FRAME_FONTSET (f)));
5236 f->default_face_done_p = true;
5238 #endif /* HAVE_WINDOW_SYSTEM */
5240 if (!FRAME_WINDOW_P (f))
5242 ASET (lface, LFACE_FAMILY_INDEX, build_string ("default"));
5243 ASET (lface, LFACE_FOUNDRY_INDEX, LFACE_FAMILY (lface));
5244 ASET (lface, LFACE_SWIDTH_INDEX, Qnormal);
5245 ASET (lface, LFACE_HEIGHT_INDEX, make_number (1));
5246 if (UNSPECIFIEDP (LFACE_WEIGHT (lface)))
5247 ASET (lface, LFACE_WEIGHT_INDEX, Qnormal);
5248 if (UNSPECIFIEDP (LFACE_SLANT (lface)))
5249 ASET (lface, LFACE_SLANT_INDEX, Qnormal);
5250 if (UNSPECIFIEDP (LFACE_FONTSET (lface)))
5251 ASET (lface, LFACE_FONTSET_INDEX, Qnil);
5254 if (UNSPECIFIEDP (LFACE_UNDERLINE (lface)))
5255 ASET (lface, LFACE_UNDERLINE_INDEX, Qnil);
5257 if (UNSPECIFIEDP (LFACE_OVERLINE (lface)))
5258 ASET (lface, LFACE_OVERLINE_INDEX, Qnil);
5260 if (UNSPECIFIEDP (LFACE_STRIKE_THROUGH (lface)))
5261 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, Qnil);
5263 if (UNSPECIFIEDP (LFACE_BOX (lface)))
5264 ASET (lface, LFACE_BOX_INDEX, Qnil);
5266 if (UNSPECIFIEDP (LFACE_INVERSE (lface)))
5267 ASET (lface, LFACE_INVERSE_INDEX, Qnil);
5269 if (UNSPECIFIEDP (LFACE_FOREGROUND (lface)))
5271 /* This function is called so early that colors are not yet
5272 set in the frame parameter list. */
5273 Lisp_Object color = Fassq (Qforeground_color, f->param_alist);
5275 if (CONSP (color) && STRINGP (XCDR (color)))
5276 ASET (lface, LFACE_FOREGROUND_INDEX, XCDR (color));
5277 else if (FRAME_WINDOW_P (f))
5278 return false;
5279 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5280 ASET (lface, LFACE_FOREGROUND_INDEX, build_string (unspecified_fg));
5281 else
5282 emacs_abort ();
5285 if (UNSPECIFIEDP (LFACE_BACKGROUND (lface)))
5287 /* This function is called so early that colors are not yet
5288 set in the frame parameter list. */
5289 Lisp_Object color = Fassq (Qbackground_color, f->param_alist);
5290 if (CONSP (color) && STRINGP (XCDR (color)))
5291 ASET (lface, LFACE_BACKGROUND_INDEX, XCDR (color));
5292 else if (FRAME_WINDOW_P (f))
5293 return false;
5294 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5295 ASET (lface, LFACE_BACKGROUND_INDEX, build_string (unspecified_bg));
5296 else
5297 emacs_abort ();
5300 if (UNSPECIFIEDP (LFACE_STIPPLE (lface)))
5301 ASET (lface, LFACE_STIPPLE_INDEX, Qnil);
5303 /* Realize the face; it must be fully-specified now. */
5304 eassert (lface_fully_specified_p (XVECTOR (lface)->contents));
5305 check_lface (lface);
5306 memcpy (attrs, XVECTOR (lface)->contents, sizeof attrs);
5307 struct face *face = realize_face (c, attrs, DEFAULT_FACE_ID);
5309 #ifndef HAVE_WINDOW_SYSTEM
5310 (void) face;
5311 #else
5312 if (FRAME_X_P (f) && face->font != FRAME_FONT (f))
5314 /* This can happen when making a frame on a display that does
5315 not support the default font. */
5316 if (!face->font)
5317 return false;
5319 /* Otherwise, the font specified for the frame was not
5320 acceptable as a font for the default face (perhaps because
5321 auto-scaled fonts are rejected), so we must adjust the frame
5322 font. */
5323 x_set_font (f, LFACE_FONT (lface), Qnil);
5325 #endif
5326 return true;
5330 /* Realize basic faces other than the default face in face cache C.
5331 SYMBOL is the face name, ID is the face id the realized face must
5332 have. The default face must have been realized already. */
5334 static void
5335 realize_named_face (struct frame *f, Lisp_Object symbol, int id)
5337 struct face_cache *c = FRAME_FACE_CACHE (f);
5338 Lisp_Object lface = lface_from_face_name (f, symbol, false);
5339 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5340 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5342 /* The default face must exist and be fully specified. */
5343 get_lface_attributes_no_remap (f, Qdefault, attrs, true);
5344 check_lface_attrs (attrs);
5345 eassert (lface_fully_specified_p (attrs));
5347 /* If SYMBOL isn't know as a face, create it. */
5348 if (NILP (lface))
5350 Lisp_Object frame;
5351 XSETFRAME (frame, f);
5352 lface = Finternal_make_lisp_face (symbol, frame);
5355 /* Merge SYMBOL's face with the default face. */
5356 get_lface_attributes_no_remap (f, symbol, symbol_attrs, true);
5357 merge_face_vectors (f, symbol_attrs, attrs, 0);
5359 /* Realize the face. */
5360 realize_face (c, attrs, id);
5364 /* Realize the fully-specified face with attributes ATTRS in face
5365 cache CACHE for ASCII characters. If FORMER_FACE_ID is
5366 non-negative, it is an ID of face to remove before caching the new
5367 face. Value is a pointer to the newly created realized face. */
5369 static struct face *
5370 realize_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE],
5371 int former_face_id)
5373 struct face *face;
5375 /* LFACE must be fully specified. */
5376 eassert (cache != NULL);
5377 check_lface_attrs (attrs);
5379 if (former_face_id >= 0 && cache->used > former_face_id)
5381 /* Remove the former face. */
5382 struct face *former_face = cache->faces_by_id[former_face_id];
5383 uncache_face (cache, former_face);
5384 free_realized_face (cache->f, former_face);
5385 SET_FRAME_GARBAGED (cache->f);
5388 if (FRAME_WINDOW_P (cache->f))
5389 face = realize_x_face (cache, attrs);
5390 else if (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f))
5391 face = realize_tty_face (cache, attrs);
5392 else if (FRAME_INITIAL_P (cache->f))
5394 /* Create a dummy face. */
5395 face = make_realized_face (attrs);
5397 else
5398 emacs_abort ();
5400 /* Insert the new face. */
5401 cache_face (cache, face, lface_hash (attrs));
5402 return face;
5406 #ifdef HAVE_WINDOW_SYSTEM
5407 /* Realize the fully-specified face that uses FONT-OBJECT and has the
5408 same attributes as BASE_FACE except for the font on frame F.
5409 FONT-OBJECT may be nil, in which case, realized a face of
5410 no-font. */
5412 static struct face *
5413 realize_non_ascii_face (struct frame *f, Lisp_Object font_object,
5414 struct face *base_face)
5416 struct face_cache *cache = FRAME_FACE_CACHE (f);
5417 struct face *face;
5419 face = xmalloc (sizeof *face);
5420 *face = *base_face;
5421 face->gc = 0;
5422 face->overstrike
5423 = (! NILP (font_object)
5424 && FONT_WEIGHT_NAME_NUMERIC (face->lface[LFACE_WEIGHT_INDEX]) > 100
5425 && FONT_WEIGHT_NUMERIC (font_object) <= 100);
5427 /* Don't try to free the colors copied bitwise from BASE_FACE. */
5428 face->colors_copied_bitwise_p = true;
5429 face->font = NILP (font_object) ? NULL : XFONT_OBJECT (font_object);
5430 face->gc = 0;
5432 cache_face (cache, face, face->hash);
5434 return face;
5436 #endif /* HAVE_WINDOW_SYSTEM */
5439 /* Realize the fully-specified face with attributes ATTRS in face
5440 cache CACHE for ASCII characters. Do it for X frame CACHE->f. If
5441 the new face doesn't share font with the default face, a fontname
5442 is allocated from the heap and set in `font_name' of the new face,
5443 but it is not yet loaded here. Value is a pointer to the newly
5444 created realized face. */
5446 static struct face *
5447 realize_x_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE])
5449 struct face *face = NULL;
5450 #ifdef HAVE_WINDOW_SYSTEM
5451 struct face *default_face;
5452 struct frame *f;
5453 Lisp_Object stipple, underline, overline, strike_through, box;
5455 eassert (FRAME_WINDOW_P (cache->f));
5457 /* Allocate a new realized face. */
5458 face = make_realized_face (attrs);
5459 face->ascii_face = face;
5461 f = cache->f;
5463 /* Determine the font to use. Most of the time, the font will be
5464 the same as the font of the default face, so try that first. */
5465 default_face = FACE_FROM_ID_OR_NULL (f, DEFAULT_FACE_ID);
5466 if (default_face
5467 && lface_same_font_attributes_p (default_face->lface, attrs))
5469 face->font = default_face->font;
5470 face->fontset
5471 = make_fontset_for_ascii_face (f, default_face->fontset, face);
5473 else
5475 /* If the face attribute ATTRS specifies a fontset, use it as
5476 the base of a new realized fontset. Otherwise, use the same
5477 base fontset as of the default face. The base determines
5478 registry and encoding of a font. It may also determine
5479 foundry and family. The other fields of font name pattern
5480 are constructed from ATTRS. */
5481 int fontset = face_fontset (attrs);
5483 /* If we are realizing the default face, ATTRS should specify a
5484 fontset. In other words, if FONTSET is -1, we are not
5485 realizing the default face, thus the default face should have
5486 already been realized. */
5487 if (fontset == -1)
5489 if (default_face)
5490 fontset = default_face->fontset;
5491 if (fontset == -1)
5492 emacs_abort ();
5494 if (! FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5495 attrs[LFACE_FONT_INDEX]
5496 = font_load_for_lface (f, attrs, Ffont_spec (0, NULL));
5497 if (FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5499 face->font = XFONT_OBJECT (attrs[LFACE_FONT_INDEX]);
5500 face->fontset = make_fontset_for_ascii_face (f, fontset, face);
5502 else
5504 face->font = NULL;
5505 face->fontset = -1;
5509 if (face->font
5510 && FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]) > 100
5511 && FONT_WEIGHT_NUMERIC (attrs[LFACE_FONT_INDEX]) <= 100)
5512 face->overstrike = true;
5514 /* Load colors, and set remaining attributes. */
5516 load_face_colors (f, face, attrs);
5518 /* Set up box. */
5519 box = attrs[LFACE_BOX_INDEX];
5520 if (STRINGP (box))
5522 /* A simple box of line width 1 drawn in color given by
5523 the string. */
5524 face->box_color = load_color (f, face, attrs[LFACE_BOX_INDEX],
5525 LFACE_BOX_INDEX);
5526 face->box = FACE_SIMPLE_BOX;
5527 face->box_line_width = 1;
5529 else if (INTEGERP (box))
5531 /* Simple box of specified line width in foreground color of the
5532 face. */
5533 eassert (XINT (box) != 0);
5534 face->box = FACE_SIMPLE_BOX;
5535 face->box_line_width = XINT (box);
5536 face->box_color = face->foreground;
5537 face->box_color_defaulted_p = true;
5539 else if (CONSP (box))
5541 /* `(:width WIDTH :color COLOR :shadow SHADOW)'. SHADOW
5542 being one of `raised' or `sunken'. */
5543 face->box = FACE_SIMPLE_BOX;
5544 face->box_color = face->foreground;
5545 face->box_color_defaulted_p = true;
5546 face->box_line_width = 1;
5548 while (CONSP (box))
5550 Lisp_Object keyword, value;
5552 keyword = XCAR (box);
5553 box = XCDR (box);
5555 if (!CONSP (box))
5556 break;
5557 value = XCAR (box);
5558 box = XCDR (box);
5560 if (EQ (keyword, QCline_width))
5562 if (INTEGERP (value) && XINT (value) != 0)
5563 face->box_line_width = XINT (value);
5565 else if (EQ (keyword, QCcolor))
5567 if (STRINGP (value))
5569 face->box_color = load_color (f, face, value,
5570 LFACE_BOX_INDEX);
5571 face->use_box_color_for_shadows_p = true;
5574 else if (EQ (keyword, QCstyle))
5576 if (EQ (value, Qreleased_button))
5577 face->box = FACE_RAISED_BOX;
5578 else if (EQ (value, Qpressed_button))
5579 face->box = FACE_SUNKEN_BOX;
5584 /* Text underline, overline, strike-through. */
5586 underline = attrs[LFACE_UNDERLINE_INDEX];
5587 if (EQ (underline, Qt))
5589 /* Use default color (same as foreground color). */
5590 face->underline_p = true;
5591 face->underline_type = FACE_UNDER_LINE;
5592 face->underline_defaulted_p = true;
5593 face->underline_color = 0;
5595 else if (STRINGP (underline))
5597 /* Use specified color. */
5598 face->underline_p = true;
5599 face->underline_type = FACE_UNDER_LINE;
5600 face->underline_defaulted_p = false;
5601 face->underline_color
5602 = load_color (f, face, underline,
5603 LFACE_UNDERLINE_INDEX);
5605 else if (NILP (underline))
5607 face->underline_p = false;
5608 face->underline_defaulted_p = false;
5609 face->underline_color = 0;
5611 else if (CONSP (underline))
5613 /* `(:color COLOR :style STYLE)'.
5614 STYLE being one of `line' or `wave'. */
5615 face->underline_p = true;
5616 face->underline_color = 0;
5617 face->underline_defaulted_p = true;
5618 face->underline_type = FACE_UNDER_LINE;
5620 /* FIXME? This is also not robust about checking the precise form.
5621 See comments in Finternal_set_lisp_face_attribute. */
5622 while (CONSP (underline))
5624 Lisp_Object keyword, value;
5626 keyword = XCAR (underline);
5627 underline = XCDR (underline);
5629 if (!CONSP (underline))
5630 break;
5631 value = XCAR (underline);
5632 underline = XCDR (underline);
5634 if (EQ (keyword, QCcolor))
5636 if (EQ (value, Qforeground_color))
5638 face->underline_defaulted_p = true;
5639 face->underline_color = 0;
5641 else if (STRINGP (value))
5643 face->underline_defaulted_p = false;
5644 face->underline_color = load_color (f, face, value,
5645 LFACE_UNDERLINE_INDEX);
5648 else if (EQ (keyword, QCstyle))
5650 if (EQ (value, Qline))
5651 face->underline_type = FACE_UNDER_LINE;
5652 else if (EQ (value, Qwave))
5653 face->underline_type = FACE_UNDER_WAVE;
5658 overline = attrs[LFACE_OVERLINE_INDEX];
5659 if (STRINGP (overline))
5661 face->overline_color
5662 = load_color (f, face, attrs[LFACE_OVERLINE_INDEX],
5663 LFACE_OVERLINE_INDEX);
5664 face->overline_p = true;
5666 else if (EQ (overline, Qt))
5668 face->overline_color = face->foreground;
5669 face->overline_color_defaulted_p = true;
5670 face->overline_p = true;
5673 strike_through = attrs[LFACE_STRIKE_THROUGH_INDEX];
5674 if (STRINGP (strike_through))
5676 face->strike_through_color
5677 = load_color (f, face, attrs[LFACE_STRIKE_THROUGH_INDEX],
5678 LFACE_STRIKE_THROUGH_INDEX);
5679 face->strike_through_p = true;
5681 else if (EQ (strike_through, Qt))
5683 face->strike_through_color = face->foreground;
5684 face->strike_through_color_defaulted_p = true;
5685 face->strike_through_p = true;
5688 stipple = attrs[LFACE_STIPPLE_INDEX];
5689 if (!NILP (stipple))
5690 face->stipple = load_pixmap (f, stipple);
5691 #endif /* HAVE_WINDOW_SYSTEM */
5693 return face;
5697 /* Map a specified color of face FACE on frame F to a tty color index.
5698 IDX is either LFACE_FOREGROUND_INDEX or LFACE_BACKGROUND_INDEX, and
5699 specifies which color to map. Set *DEFAULTED to true if mapping to the
5700 default foreground/background colors. */
5702 static void
5703 map_tty_color (struct frame *f, struct face *face,
5704 enum lface_attribute_index idx, bool *defaulted)
5706 Lisp_Object frame, color, def;
5707 bool foreground_p = idx == LFACE_FOREGROUND_INDEX;
5708 unsigned long default_pixel =
5709 foreground_p ? FACE_TTY_DEFAULT_FG_COLOR : FACE_TTY_DEFAULT_BG_COLOR;
5710 unsigned long pixel = default_pixel;
5711 #ifdef MSDOS
5712 unsigned long default_other_pixel =
5713 foreground_p ? FACE_TTY_DEFAULT_BG_COLOR : FACE_TTY_DEFAULT_FG_COLOR;
5714 #endif
5716 eassert (idx == LFACE_FOREGROUND_INDEX || idx == LFACE_BACKGROUND_INDEX);
5718 XSETFRAME (frame, f);
5719 color = face->lface[idx];
5721 if (STRINGP (color)
5722 && SCHARS (color)
5723 && CONSP (Vtty_defined_color_alist)
5724 && (def = assoc_no_quit (color, call1 (Qtty_color_alist, frame)),
5725 CONSP (def)))
5727 /* Associations in tty-defined-color-alist are of the form
5728 (NAME INDEX R G B). We need the INDEX part. */
5729 pixel = XINT (XCAR (XCDR (def)));
5732 if (pixel == default_pixel && STRINGP (color))
5734 pixel = load_color (f, face, color, idx);
5736 #ifdef MSDOS
5737 /* If the foreground of the default face is the default color,
5738 use the foreground color defined by the frame. */
5739 if (FRAME_MSDOS_P (f))
5741 if (pixel == default_pixel
5742 || pixel == FACE_TTY_DEFAULT_COLOR)
5744 if (foreground_p)
5745 pixel = FRAME_FOREGROUND_PIXEL (f);
5746 else
5747 pixel = FRAME_BACKGROUND_PIXEL (f);
5748 face->lface[idx] = tty_color_name (f, pixel);
5749 *defaulted = true;
5751 else if (pixel == default_other_pixel)
5753 if (foreground_p)
5754 pixel = FRAME_BACKGROUND_PIXEL (f);
5755 else
5756 pixel = FRAME_FOREGROUND_PIXEL (f);
5757 face->lface[idx] = tty_color_name (f, pixel);
5758 *defaulted = true;
5761 #endif /* MSDOS */
5764 if (foreground_p)
5765 face->foreground = pixel;
5766 else
5767 face->background = pixel;
5771 /* Realize the fully-specified face with attributes ATTRS in face
5772 cache CACHE for ASCII characters. Do it for TTY frame CACHE->f.
5773 Value is a pointer to the newly created realized face. */
5775 static struct face *
5776 realize_tty_face (struct face_cache *cache,
5777 Lisp_Object attrs[LFACE_VECTOR_SIZE])
5779 struct face *face;
5780 int weight, slant;
5781 bool face_colors_defaulted = false;
5782 struct frame *f = cache->f;
5784 /* Frame must be a termcap frame. */
5785 eassert (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f));
5787 /* Allocate a new realized face. */
5788 face = make_realized_face (attrs);
5789 #if false
5790 face->font_name = FRAME_MSDOS_P (cache->f) ? "ms-dos" : "tty";
5791 #endif
5793 /* Map face attributes to TTY appearances. */
5794 weight = FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]);
5795 slant = FONT_SLANT_NAME_NUMERIC (attrs[LFACE_SLANT_INDEX]);
5796 if (weight > 100)
5797 face->tty_bold_p = true;
5798 if (slant != 100)
5799 face->tty_italic_p = true;
5800 if (!NILP (attrs[LFACE_UNDERLINE_INDEX]))
5801 face->tty_underline_p = true;
5802 if (!NILP (attrs[LFACE_INVERSE_INDEX]))
5803 face->tty_reverse_p = true;
5805 /* Map color names to color indices. */
5806 map_tty_color (f, face, LFACE_FOREGROUND_INDEX, &face_colors_defaulted);
5807 map_tty_color (f, face, LFACE_BACKGROUND_INDEX, &face_colors_defaulted);
5809 /* Swap colors if face is inverse-video. If the colors are taken
5810 from the frame colors, they are already inverted, since the
5811 frame-creation function calls x-handle-reverse-video. */
5812 if (face->tty_reverse_p && !face_colors_defaulted)
5814 unsigned long tem = face->foreground;
5815 face->foreground = face->background;
5816 face->background = tem;
5819 if (tty_suppress_bold_inverse_default_colors_p
5820 && face->tty_bold_p
5821 && face->background == FACE_TTY_DEFAULT_FG_COLOR
5822 && face->foreground == FACE_TTY_DEFAULT_BG_COLOR)
5823 face->tty_bold_p = false;
5825 return face;
5829 DEFUN ("tty-suppress-bold-inverse-default-colors",
5830 Ftty_suppress_bold_inverse_default_colors,
5831 Stty_suppress_bold_inverse_default_colors, 1, 1, 0,
5832 doc: /* Suppress/allow boldness of faces with inverse default colors.
5833 SUPPRESS non-nil means suppress it.
5834 This affects bold faces on TTYs whose foreground is the default background
5835 color of the display and whose background is the default foreground color.
5836 For such faces, the bold face attribute is ignored if this variable
5837 is non-nil. */)
5838 (Lisp_Object suppress)
5840 tty_suppress_bold_inverse_default_colors_p = !NILP (suppress);
5841 face_change = true;
5842 return suppress;
5847 /***********************************************************************
5848 Computing Faces
5849 ***********************************************************************/
5851 /* Return the ID of the face to use to display character CH with face
5852 property PROP on frame F in current_buffer. */
5855 compute_char_face (struct frame *f, int ch, Lisp_Object prop)
5857 int face_id;
5859 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
5860 ch = 0;
5862 if (NILP (prop))
5864 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5865 face_id = FACE_FOR_CHAR (f, face, ch, -1, Qnil);
5867 else
5869 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5870 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5871 memcpy (attrs, default_face->lface, sizeof attrs);
5872 merge_face_ref (f, prop, attrs, true, 0);
5873 face_id = lookup_face (f, attrs);
5876 return face_id;
5879 /* Return the face ID associated with buffer position POS for
5880 displaying ASCII characters. Return in *ENDPTR the position at
5881 which a different face is needed, as far as text properties and
5882 overlays are concerned. W is a window displaying current_buffer.
5884 REGION_BEG, REGION_END delimit the region, so it can be
5885 highlighted.
5887 LIMIT is a position not to scan beyond. That is to limit the time
5888 this function can take.
5890 If MOUSE, use the character's mouse-face, not its face, and only
5891 consider the highest-priority source of mouse-face at POS,
5892 i.e. don't merge different mouse-face values if more than one
5893 source specifies it.
5895 BASE_FACE_ID, if non-negative, specifies a base face id to use
5896 instead of DEFAULT_FACE_ID.
5898 The face returned is suitable for displaying ASCII characters. */
5901 face_at_buffer_position (struct window *w, ptrdiff_t pos,
5902 ptrdiff_t *endptr, ptrdiff_t limit,
5903 bool mouse, int base_face_id)
5905 struct frame *f = XFRAME (w->frame);
5906 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5907 Lisp_Object prop, position;
5908 ptrdiff_t i, noverlays;
5909 Lisp_Object *overlay_vec;
5910 ptrdiff_t endpos;
5911 Lisp_Object propname = mouse ? Qmouse_face : Qface;
5912 Lisp_Object limit1, end;
5913 struct face *default_face;
5915 /* W must display the current buffer. We could write this function
5916 to use the frame and buffer of W, but right now it doesn't. */
5917 /* eassert (XBUFFER (w->contents) == current_buffer); */
5919 XSETFASTINT (position, pos);
5921 endpos = ZV;
5923 /* Get the `face' or `mouse_face' text property at POS, and
5924 determine the next position at which the property changes. */
5925 prop = Fget_text_property (position, propname, w->contents);
5926 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
5927 end = Fnext_single_property_change (position, propname, w->contents, limit1);
5928 if (INTEGERP (end))
5929 endpos = XINT (end);
5931 /* Look at properties from overlays. */
5932 USE_SAFE_ALLOCA;
5934 ptrdiff_t next_overlay;
5936 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, &next_overlay, false);
5937 if (next_overlay < endpos)
5938 endpos = next_overlay;
5941 *endptr = endpos;
5944 int face_id;
5946 if (base_face_id >= 0)
5947 face_id = base_face_id;
5948 else if (NILP (Vface_remapping_alist))
5949 face_id = DEFAULT_FACE_ID;
5950 else
5951 face_id = lookup_basic_face (f, DEFAULT_FACE_ID);
5953 default_face = FACE_FROM_ID (f, face_id);
5956 /* Optimize common cases where we can use the default face. */
5957 if (noverlays == 0
5958 && NILP (prop))
5960 SAFE_FREE ();
5961 return default_face->id;
5964 /* Begin with attributes from the default face. */
5965 memcpy (attrs, default_face->lface, sizeof attrs);
5967 /* Merge in attributes specified via text properties. */
5968 if (!NILP (prop))
5969 merge_face_ref (f, prop, attrs, true, 0);
5971 /* Now merge the overlay data. */
5972 noverlays = sort_overlays (overlay_vec, noverlays, w);
5973 /* For mouse-face, we need only the single highest-priority face
5974 from the overlays, if any. */
5975 if (mouse)
5977 for (prop = Qnil, i = noverlays - 1; i >= 0 && NILP (prop); --i)
5979 Lisp_Object oend;
5980 ptrdiff_t oendpos;
5982 prop = Foverlay_get (overlay_vec[i], propname);
5983 if (!NILP (prop))
5985 /* Overlays always take priority over text properties,
5986 so discard the mouse-face text property, if any, and
5987 use the overlay property instead. */
5988 memcpy (attrs, default_face->lface, sizeof attrs);
5989 merge_face_ref (f, prop, attrs, true, 0);
5992 oend = OVERLAY_END (overlay_vec[i]);
5993 oendpos = OVERLAY_POSITION (oend);
5994 if (oendpos < endpos)
5995 endpos = oendpos;
5998 else
6000 for (i = 0; i < noverlays; i++)
6002 Lisp_Object oend;
6003 ptrdiff_t oendpos;
6005 prop = Foverlay_get (overlay_vec[i], propname);
6006 if (!NILP (prop))
6007 merge_face_ref (f, prop, attrs, true, 0);
6009 oend = OVERLAY_END (overlay_vec[i]);
6010 oendpos = OVERLAY_POSITION (oend);
6011 if (oendpos < endpos)
6012 endpos = oendpos;
6016 *endptr = endpos;
6018 SAFE_FREE ();
6020 /* Look up a realized face with the given face attributes,
6021 or realize a new one for ASCII characters. */
6022 return lookup_face (f, attrs);
6025 /* Return the face ID at buffer position POS for displaying ASCII
6026 characters associated with overlay strings for overlay OVERLAY.
6028 Like face_at_buffer_position except for OVERLAY. Currently it
6029 simply disregards the `face' properties of all overlays. */
6032 face_for_overlay_string (struct window *w, ptrdiff_t pos,
6033 ptrdiff_t *endptr, ptrdiff_t limit,
6034 bool mouse, Lisp_Object overlay)
6036 struct frame *f = XFRAME (w->frame);
6037 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6038 Lisp_Object prop, position;
6039 ptrdiff_t endpos;
6040 Lisp_Object propname = mouse ? Qmouse_face : Qface;
6041 Lisp_Object limit1, end;
6042 struct face *default_face;
6044 /* W must display the current buffer. We could write this function
6045 to use the frame and buffer of W, but right now it doesn't. */
6046 /* eassert (XBUFFER (w->contents) == current_buffer); */
6048 XSETFASTINT (position, pos);
6050 endpos = ZV;
6052 /* Get the `face' or `mouse_face' text property at POS, and
6053 determine the next position at which the property changes. */
6054 prop = Fget_text_property (position, propname, w->contents);
6055 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
6056 end = Fnext_single_property_change (position, propname, w->contents, limit1);
6057 if (INTEGERP (end))
6058 endpos = XINT (end);
6060 *endptr = endpos;
6062 /* Optimize common case where we can use the default face. */
6063 if (NILP (prop)
6064 && NILP (Vface_remapping_alist))
6065 return DEFAULT_FACE_ID;
6067 /* Begin with attributes from the default face. */
6068 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
6069 memcpy (attrs, default_face->lface, sizeof attrs);
6071 /* Merge in attributes specified via text properties. */
6072 if (!NILP (prop))
6073 merge_face_ref (f, prop, attrs, true, 0);
6075 *endptr = endpos;
6077 /* Look up a realized face with the given face attributes,
6078 or realize a new one for ASCII characters. */
6079 return lookup_face (f, attrs);
6083 /* Compute the face at character position POS in Lisp string STRING on
6084 window W, for ASCII characters.
6086 If STRING is an overlay string, it comes from position BUFPOS in
6087 current_buffer, otherwise BUFPOS is zero to indicate that STRING is
6088 not an overlay string. W must display the current buffer.
6089 REGION_BEG and REGION_END give the start and end positions of the
6090 region; both are -1 if no region is visible.
6092 BASE_FACE_ID is the id of a face to merge with. For strings coming
6093 from overlays or the `display' property it is the face at BUFPOS.
6095 If MOUSE_P, use the character's mouse-face, not its face.
6097 Set *ENDPTR to the next position where to check for faces in
6098 STRING; -1 if the face is constant from POS to the end of the
6099 string.
6101 Value is the id of the face to use. The face returned is suitable
6102 for displaying ASCII characters. */
6105 face_at_string_position (struct window *w, Lisp_Object string,
6106 ptrdiff_t pos, ptrdiff_t bufpos,
6107 ptrdiff_t *endptr, enum face_id base_face_id,
6108 bool mouse_p)
6110 Lisp_Object prop, position, end, limit;
6111 struct frame *f = XFRAME (WINDOW_FRAME (w));
6112 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6113 struct face *base_face;
6114 bool multibyte_p = STRING_MULTIBYTE (string);
6115 Lisp_Object prop_name = mouse_p ? Qmouse_face : Qface;
6117 /* Get the value of the face property at the current position within
6118 STRING. Value is nil if there is no face property. */
6119 XSETFASTINT (position, pos);
6120 prop = Fget_text_property (position, prop_name, string);
6122 /* Get the next position at which to check for faces. Value of end
6123 is nil if face is constant all the way to the end of the string.
6124 Otherwise it is a string position where to check faces next.
6125 Limit is the maximum position up to which to check for property
6126 changes in Fnext_single_property_change. Strings are usually
6127 short, so set the limit to the end of the string. */
6128 XSETFASTINT (limit, SCHARS (string));
6129 end = Fnext_single_property_change (position, prop_name, string, limit);
6130 if (INTEGERP (end))
6131 *endptr = XFASTINT (end);
6132 else
6133 *endptr = -1;
6135 base_face = FACE_FROM_ID (f, base_face_id);
6137 /* Optimize the default case that there is no face property. */
6138 if (NILP (prop)
6139 && (multibyte_p
6140 /* We can't realize faces for different charsets differently
6141 if we don't have fonts, so we can stop here if not working
6142 on a window-system frame. */
6143 || !FRAME_WINDOW_P (f)
6144 || FACE_SUITABLE_FOR_ASCII_CHAR_P (base_face)))
6145 return base_face->id;
6147 /* Begin with attributes from the base face. */
6148 memcpy (attrs, base_face->lface, sizeof attrs);
6150 /* Merge in attributes specified via text properties. */
6151 if (!NILP (prop))
6152 merge_face_ref (f, prop, attrs, true, 0);
6154 /* Look up a realized face with the given face attributes,
6155 or realize a new one for ASCII characters. */
6156 return lookup_face (f, attrs);
6160 /* Merge a face into a realized face.
6162 F is frame where faces are (to be) realized.
6164 FACE_NAME is named face to merge.
6166 If FACE_NAME is nil, FACE_ID is face_id of realized face to merge.
6168 If FACE_NAME is t, FACE_ID is lface_id of face to merge.
6170 BASE_FACE_ID is realized face to merge into.
6172 Return new face id.
6176 merge_faces (struct frame *f, Lisp_Object face_name, int face_id,
6177 int base_face_id)
6179 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6180 struct face *base_face;
6182 base_face = FACE_FROM_ID_OR_NULL (f, base_face_id);
6183 if (!base_face)
6184 return base_face_id;
6186 if (EQ (face_name, Qt))
6188 if (face_id < 0 || face_id >= lface_id_to_name_size)
6189 return base_face_id;
6190 face_name = lface_id_to_name[face_id];
6191 /* When called during make-frame, lookup_derived_face may fail
6192 if the faces are uninitialized. Don't signal an error. */
6193 face_id = lookup_derived_face (f, face_name, base_face_id, 0);
6194 return (face_id >= 0 ? face_id : base_face_id);
6197 /* Begin with attributes from the base face. */
6198 memcpy (attrs, base_face->lface, sizeof attrs);
6200 if (!NILP (face_name))
6202 if (!merge_named_face (f, face_name, attrs, 0))
6203 return base_face_id;
6205 else
6207 struct face *face;
6208 if (face_id < 0)
6209 return base_face_id;
6210 face = FACE_FROM_ID_OR_NULL (f, face_id);
6211 if (!face)
6212 return base_face_id;
6213 merge_face_vectors (f, face->lface, attrs, 0);
6216 /* Look up a realized face with the given face attributes,
6217 or realize a new one for ASCII characters. */
6218 return lookup_face (f, attrs);
6223 #ifndef HAVE_X_WINDOWS
6224 DEFUN ("x-load-color-file", Fx_load_color_file,
6225 Sx_load_color_file, 1, 1, 0,
6226 doc: /* Create an alist of color entries from an external file.
6228 The file should define one named RGB color per line like so:
6229 R G B name
6230 where R,G,B are numbers between 0 and 255 and name is an arbitrary string. */)
6231 (Lisp_Object filename)
6233 FILE *fp;
6234 Lisp_Object cmap = Qnil;
6235 Lisp_Object abspath;
6237 CHECK_STRING (filename);
6238 abspath = Fexpand_file_name (filename, Qnil);
6240 block_input ();
6241 fp = emacs_fopen (SSDATA (abspath), "r" FOPEN_TEXT);
6242 if (fp)
6244 char buf[512];
6245 int red, green, blue;
6246 int num;
6248 while (fgets_unlocked (buf, sizeof (buf), fp) != NULL) {
6249 if (sscanf (buf, "%d %d %d %n", &red, &green, &blue, &num) == 3)
6251 #ifdef HAVE_NTGUI
6252 int color = RGB (red, green, blue);
6253 #else
6254 int color = (red << 16) | (green << 8) | blue;
6255 #endif
6256 char *name = buf + num;
6257 ptrdiff_t len = strlen (name);
6258 len -= 0 < len && name[len - 1] == '\n';
6259 cmap = Fcons (Fcons (make_string (name, len), make_number (color)),
6260 cmap);
6263 fclose (fp);
6265 unblock_input ();
6266 return cmap;
6268 #endif
6271 /***********************************************************************
6272 Tests
6273 ***********************************************************************/
6275 #ifdef GLYPH_DEBUG
6277 /* Print the contents of the realized face FACE to stderr. */
6279 static void
6280 dump_realized_face (struct face *face)
6282 fprintf (stderr, "ID: %d\n", face->id);
6283 #ifdef HAVE_X_WINDOWS
6284 fprintf (stderr, "gc: %p\n", face->gc);
6285 #endif
6286 fprintf (stderr, "foreground: 0x%lx (%s)\n",
6287 face->foreground,
6288 SDATA (face->lface[LFACE_FOREGROUND_INDEX]));
6289 fprintf (stderr, "background: 0x%lx (%s)\n",
6290 face->background,
6291 SDATA (face->lface[LFACE_BACKGROUND_INDEX]));
6292 if (face->font)
6293 fprintf (stderr, "font_name: %s (%s)\n",
6294 SDATA (face->font->props[FONT_NAME_INDEX]),
6295 SDATA (face->lface[LFACE_FAMILY_INDEX]));
6296 #ifdef HAVE_X_WINDOWS
6297 fprintf (stderr, "font = %p\n", face->font);
6298 #endif
6299 fprintf (stderr, "fontset: %d\n", face->fontset);
6300 fprintf (stderr, "underline: %d (%s)\n",
6301 face->underline_p,
6302 SDATA (Fsymbol_name (face->lface[LFACE_UNDERLINE_INDEX])));
6303 fprintf (stderr, "hash: %u\n", face->hash);
6307 DEFUN ("dump-face", Fdump_face, Sdump_face, 0, 1, 0, doc: /* */)
6308 (Lisp_Object n)
6310 if (NILP (n))
6312 int i;
6314 fprintf (stderr, "font selection order: ");
6315 for (i = 0; i < ARRAYELTS (font_sort_order); ++i)
6316 fprintf (stderr, "%d ", font_sort_order[i]);
6317 fprintf (stderr, "\n");
6319 fprintf (stderr, "alternative fonts: ");
6320 debug_print (Vface_alternative_font_family_alist);
6321 fprintf (stderr, "\n");
6323 for (i = 0; i < FRAME_FACE_CACHE (SELECTED_FRAME ())->used; ++i)
6324 Fdump_face (make_number (i));
6326 else
6328 struct face *face;
6329 CHECK_NUMBER (n);
6330 face = FACE_FROM_ID_OR_NULL (SELECTED_FRAME (), XINT (n));
6331 if (face == NULL)
6332 error ("Not a valid face");
6333 dump_realized_face (face);
6336 return Qnil;
6340 DEFUN ("show-face-resources", Fshow_face_resources, Sshow_face_resources,
6341 0, 0, 0, doc: /* */)
6342 (void)
6344 fprintf (stderr, "number of colors = %d\n", ncolors_allocated);
6345 fprintf (stderr, "number of pixmaps = %d\n", npixmaps_allocated);
6346 fprintf (stderr, "number of GCs = %d\n", ngcs);
6347 return Qnil;
6350 #endif /* GLYPH_DEBUG */
6354 /***********************************************************************
6355 Initialization
6356 ***********************************************************************/
6358 void
6359 syms_of_xfaces (void)
6361 /* The symbols `face' and `mouse-face' used as text properties. */
6362 DEFSYM (Qface, "face");
6364 /* Property for basic faces which other faces cannot inherit. */
6365 DEFSYM (Qface_no_inherit, "face-no-inherit");
6367 /* Error symbol for wrong_type_argument in load_pixmap. */
6368 DEFSYM (Qbitmap_spec_p, "bitmap-spec-p");
6370 /* The name of the function to call when the background of the frame
6371 has changed, frame_set_background_mode. */
6372 DEFSYM (Qframe_set_background_mode, "frame-set-background-mode");
6374 /* Lisp face attribute keywords. */
6375 DEFSYM (QCfamily, ":family");
6376 DEFSYM (QCheight, ":height");
6377 DEFSYM (QCweight, ":weight");
6378 DEFSYM (QCslant, ":slant");
6379 DEFSYM (QCunderline, ":underline");
6380 DEFSYM (QCinverse_video, ":inverse-video");
6381 DEFSYM (QCreverse_video, ":reverse-video");
6382 DEFSYM (QCforeground, ":foreground");
6383 DEFSYM (QCbackground, ":background");
6384 DEFSYM (QCstipple, ":stipple");
6385 DEFSYM (QCwidth, ":width");
6386 DEFSYM (QCfont, ":font");
6387 DEFSYM (QCfontset, ":fontset");
6388 DEFSYM (QCdistant_foreground, ":distant-foreground");
6389 DEFSYM (QCbold, ":bold");
6390 DEFSYM (QCitalic, ":italic");
6391 DEFSYM (QCoverline, ":overline");
6392 DEFSYM (QCstrike_through, ":strike-through");
6393 DEFSYM (QCbox, ":box");
6394 DEFSYM (QCinherit, ":inherit");
6396 /* Symbols used for Lisp face attribute values. */
6397 DEFSYM (QCcolor, ":color");
6398 DEFSYM (QCline_width, ":line-width");
6399 DEFSYM (QCstyle, ":style");
6400 DEFSYM (Qline, "line");
6401 DEFSYM (Qwave, "wave");
6402 DEFSYM (Qreleased_button, "released-button");
6403 DEFSYM (Qpressed_button, "pressed-button");
6404 DEFSYM (Qnormal, "normal");
6405 DEFSYM (Qextra_light, "extra-light");
6406 DEFSYM (Qlight, "light");
6407 DEFSYM (Qsemi_light, "semi-light");
6408 DEFSYM (Qsemi_bold, "semi-bold");
6409 DEFSYM (Qbold, "bold");
6410 DEFSYM (Qextra_bold, "extra-bold");
6411 DEFSYM (Qultra_bold, "ultra-bold");
6412 DEFSYM (Qoblique, "oblique");
6413 DEFSYM (Qitalic, "italic");
6415 /* The symbols `foreground-color' and `background-color' which can be
6416 used as part of a `face' property. This is for compatibility with
6417 Emacs 20.2. */
6418 DEFSYM (Qbackground_color, "background-color");
6419 DEFSYM (Qforeground_color, "foreground-color");
6421 DEFSYM (Qunspecified, "unspecified");
6422 DEFSYM (QCignore_defface, ":ignore-defface");
6424 /* The symbol `face-alias'. A symbol having that property is an
6425 alias for another face. Value of the property is the name of
6426 the aliased face. */
6427 DEFSYM (Qface_alias, "face-alias");
6429 /* Names of basic faces. */
6430 DEFSYM (Qdefault, "default");
6431 DEFSYM (Qtool_bar, "tool-bar");
6432 DEFSYM (Qfringe, "fringe");
6433 DEFSYM (Qheader_line, "header-line");
6434 DEFSYM (Qscroll_bar, "scroll-bar");
6435 DEFSYM (Qmenu, "menu");
6436 DEFSYM (Qcursor, "cursor");
6437 DEFSYM (Qborder, "border");
6438 DEFSYM (Qmouse, "mouse");
6439 DEFSYM (Qmode_line_inactive, "mode-line-inactive");
6440 DEFSYM (Qvertical_border, "vertical-border");
6441 DEFSYM (Qwindow_divider, "window-divider");
6442 DEFSYM (Qwindow_divider_first_pixel, "window-divider-first-pixel");
6443 DEFSYM (Qwindow_divider_last_pixel, "window-divider-last-pixel");
6444 DEFSYM (Qinternal_border, "internal-border");
6446 /* TTY color-related functions (defined in tty-colors.el). */
6447 DEFSYM (Qtty_color_desc, "tty-color-desc");
6448 DEFSYM (Qtty_color_standard_values, "tty-color-standard-values");
6449 DEFSYM (Qtty_color_by_index, "tty-color-by-index");
6451 /* The name of the function used to compute colors on TTYs. */
6452 DEFSYM (Qtty_color_alist, "tty-color-alist");
6454 Vparam_value_alist = list1 (Fcons (Qnil, Qnil));
6455 staticpro (&Vparam_value_alist);
6456 Vface_alternative_font_family_alist = Qnil;
6457 staticpro (&Vface_alternative_font_family_alist);
6458 Vface_alternative_font_registry_alist = Qnil;
6459 staticpro (&Vface_alternative_font_registry_alist);
6461 defsubr (&Sinternal_make_lisp_face);
6462 defsubr (&Sinternal_lisp_face_p);
6463 defsubr (&Sinternal_set_lisp_face_attribute);
6464 #ifdef HAVE_WINDOW_SYSTEM
6465 defsubr (&Sinternal_set_lisp_face_attribute_from_resource);
6466 #endif
6467 defsubr (&Scolor_gray_p);
6468 defsubr (&Scolor_supported_p);
6469 #ifndef HAVE_X_WINDOWS
6470 defsubr (&Sx_load_color_file);
6471 #endif
6472 defsubr (&Sface_attribute_relative_p);
6473 defsubr (&Smerge_face_attribute);
6474 defsubr (&Sinternal_get_lisp_face_attribute);
6475 defsubr (&Sinternal_lisp_face_attribute_values);
6476 defsubr (&Sinternal_lisp_face_equal_p);
6477 defsubr (&Sinternal_lisp_face_empty_p);
6478 defsubr (&Sinternal_copy_lisp_face);
6479 defsubr (&Sinternal_merge_in_global_face);
6480 defsubr (&Sface_font);
6481 defsubr (&Sframe_face_alist);
6482 defsubr (&Sdisplay_supports_face_attributes_p);
6483 defsubr (&Scolor_distance);
6484 defsubr (&Sinternal_set_font_selection_order);
6485 defsubr (&Sinternal_set_alternative_font_family_alist);
6486 defsubr (&Sinternal_set_alternative_font_registry_alist);
6487 defsubr (&Sface_attributes_as_vector);
6488 #ifdef GLYPH_DEBUG
6489 defsubr (&Sdump_face);
6490 defsubr (&Sshow_face_resources);
6491 #endif /* GLYPH_DEBUG */
6492 defsubr (&Sclear_face_cache);
6493 defsubr (&Stty_suppress_bold_inverse_default_colors);
6495 #if defined DEBUG_X_COLORS && defined HAVE_X_WINDOWS
6496 defsubr (&Sdump_colors);
6497 #endif
6499 DEFVAR_LISP ("face-new-frame-defaults", Vface_new_frame_defaults,
6500 doc: /* List of global face definitions (for internal use only.) */);
6501 Vface_new_frame_defaults = Qnil;
6503 DEFVAR_LISP ("face-default-stipple", Vface_default_stipple,
6504 doc: /* Default stipple pattern used on monochrome displays.
6505 This stipple pattern is used on monochrome displays
6506 instead of shades of gray for a face background color.
6507 See `set-face-stipple' for possible values for this variable. */);
6508 Vface_default_stipple = build_pure_c_string ("gray3");
6510 DEFVAR_LISP ("tty-defined-color-alist", Vtty_defined_color_alist,
6511 doc: /* An alist of defined terminal colors and their RGB values.
6512 See the docstring of `tty-color-alist' for the details. */);
6513 Vtty_defined_color_alist = Qnil;
6515 DEFVAR_LISP ("scalable-fonts-allowed", Vscalable_fonts_allowed,
6516 doc: /* Allowed scalable fonts.
6517 A value of nil means don't allow any scalable fonts.
6518 A value of t means allow any scalable font.
6519 Otherwise, value must be a list of regular expressions. A font may be
6520 scaled if its name matches a regular expression in the list.
6521 Note that if value is nil, a scalable font might still be used, if no
6522 other font of the appropriate family and registry is available. */);
6523 Vscalable_fonts_allowed = Qnil;
6525 DEFVAR_LISP ("face-ignored-fonts", Vface_ignored_fonts,
6526 doc: /* List of ignored fonts.
6527 Each element is a regular expression that matches names of fonts to
6528 ignore. */);
6529 #ifdef HAVE_OTF_KANNADA_BUG
6530 /* https://debbugs.gnu.org/30193 */
6531 Vface_ignored_fonts = list1 (build_string ("Noto Serif Kannada"));
6532 #else
6533 Vface_ignored_fonts = Qnil;
6534 #endif
6536 DEFVAR_LISP ("face-remapping-alist", Vface_remapping_alist,
6537 doc: /* Alist of face remappings.
6538 Each element is of the form:
6540 (FACE . REPLACEMENT),
6542 which causes display of the face FACE to use REPLACEMENT instead.
6543 REPLACEMENT is a face specification, i.e. one of the following:
6545 (1) a face name
6546 (2) a property list of attribute/value pairs, or
6547 (3) a list in which each element has the form of (1) or (2).
6549 List values for REPLACEMENT are merged to form the final face
6550 specification, with earlier entries taking precedence, in the same way
6551 as with the `face' text property.
6553 Face-name remapping cycles are suppressed; recursive references use
6554 the underlying face instead of the remapped face. So a remapping of
6555 the form:
6557 (FACE EXTRA-FACE... FACE)
6561 (FACE (FACE-ATTR VAL ...) FACE)
6563 causes EXTRA-FACE... or (FACE-ATTR VAL ...) to be _merged_ with the
6564 existing definition of FACE. Note that this isn't necessary for the
6565 default face, since every face inherits from the default face.
6567 If this variable is made buffer-local, the face remapping takes effect
6568 only in that buffer. For instance, the mode my-mode could define a
6569 face `my-mode-default', and then in the mode setup function, do:
6571 (set (make-local-variable \\='face-remapping-alist)
6572 \\='((default my-mode-default)))).
6574 Because Emacs normally only redraws screen areas when the underlying
6575 buffer contents change, you may need to call `redraw-display' after
6576 changing this variable for it to take effect. */);
6577 Vface_remapping_alist = Qnil;
6579 DEFVAR_LISP ("face-font-rescale-alist", Vface_font_rescale_alist,
6580 doc: /* Alist of fonts vs the rescaling factors.
6581 Each element is a cons (FONT-PATTERN . RESCALE-RATIO), where
6582 FONT-PATTERN is a font-spec or a regular expression matching a font name, and
6583 RESCALE-RATIO is a floating point number to specify how much larger
6584 \(or smaller) font we should use. For instance, if a face requests
6585 a font of 10 point, we actually use a font of 10 * RESCALE-RATIO point. */);
6586 Vface_font_rescale_alist = Qnil;
6588 #ifdef HAVE_WINDOW_SYSTEM
6589 defsubr (&Sbitmap_spec_p);
6590 defsubr (&Sx_list_fonts);
6591 defsubr (&Sinternal_face_x_get_resource);
6592 defsubr (&Sx_family_fonts);
6593 #endif