; Auto-commit of loaddefs files.
[emacs.git] / src / xfaces.c
bloba3d122fbf46e9e61ebc0dc5cec75e9f767300036
1 /* xfaces.c -- "Face" primitives.
3 Copyright (C) 1993-1994, 1998-2015 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* New face implementation by Gerd Moellmann <gerd@gnu.org>. */
22 /* Faces.
24 When using Emacs with X, the display style of characters can be
25 changed by defining `faces'. Each face can specify the following
26 display attributes:
28 1. Font family name.
30 2. Font foundry name.
32 3. Relative proportionate width, aka character set width or set
33 width (swidth), e.g. `semi-compressed'.
35 4. Font height in 1/10pt.
37 5. Font weight, e.g. `bold'.
39 6. Font slant, e.g. `italic'.
41 7. Foreground color.
43 8. Background color.
45 9. Whether or not characters should be underlined, and in what color.
47 10. Whether or not characters should be displayed in inverse video.
49 11. A background stipple, a bitmap.
51 12. Whether or not characters should be overlined, and in what color.
53 13. Whether or not characters should be strike-through, and in what
54 color.
56 14. Whether or not a box should be drawn around characters, the box
57 type, and, for simple boxes, in what color.
59 15. Font-spec, or nil. This is a special attribute.
61 A font-spec is a collection of font attributes (specs).
63 When this attribute is specified, the face uses a font matching
64 with the specs as is except for what overwritten by the specs in
65 the fontset (see below). In addition, the other font-related
66 attributes (1st thru 5th) are updated from the spec.
68 On the other hand, if one of the other font-related attributes are
69 specified, the corresponding specs in this attribute is set to nil.
71 15. A face name or list of face names from which to inherit attributes.
73 16. A specified average font width, which is invisible from Lisp,
74 and is used to ensure that a font specified on the command line,
75 for example, can be matched exactly.
77 17. A fontset name. This is another special attribute.
79 A fontset is a mappings from characters to font-specs, and the
80 specs overwrite the font-spec in the 14th attribute.
83 Faces are frame-local by nature because Emacs allows to define the
84 same named face (face names are symbols) differently for different
85 frames. Each frame has an alist of face definitions for all named
86 faces. The value of a named face in such an alist is a Lisp vector
87 with the symbol `face' in slot 0, and a slot for each of the face
88 attributes mentioned above.
90 There is also a global face alist `Vface_new_frame_defaults'. Face
91 definitions from this list are used to initialize faces of newly
92 created frames.
94 A face doesn't have to specify all attributes. Those not specified
95 have a value of `unspecified'. Faces specifying all attributes but
96 the 14th are called `fully-specified'.
99 Face merging.
101 The display style of a given character in the text is determined by
102 combining several faces. This process is called `face merging'.
103 Any aspect of the display style that isn't specified by overlays or
104 text properties is taken from the `default' face. Since it is made
105 sure that the default face is always fully-specified, face merging
106 always results in a fully-specified face.
109 Face realization.
111 After all face attributes for a character have been determined by
112 merging faces of that character, that face is `realized'. The
113 realization process maps face attributes to what is physically
114 available on the system where Emacs runs. The result is a
115 `realized face' in the form of a struct face which is stored in the
116 face cache of the frame on which it was realized.
118 Face realization is done in the context of the character to display
119 because different fonts may be used for different characters. In
120 other words, for characters that have different font
121 specifications, different realized faces are needed to display
122 them.
124 Font specification is done by fontsets. See the comment in
125 fontset.c for the details. In the current implementation, all ASCII
126 characters share the same font in a fontset.
128 Faces are at first realized for ASCII characters, and, at that
129 time, assigned a specific realized fontset. Hereafter, we call
130 such a face as `ASCII face'. When a face for a multibyte character
131 is realized, it inherits (thus shares) a fontset of an ASCII face
132 that has the same attributes other than font-related ones.
134 Thus, all realized faces have a realized fontset.
137 Unibyte text.
139 Unibyte text (i.e. raw 8-bit characters) is displayed with the same
140 font as ASCII characters. That is because it is expected that
141 unibyte text users specify a font that is suitable both for ASCII
142 and raw 8-bit characters.
145 Font selection.
147 Font selection tries to find the best available matching font for a
148 given (character, face) combination.
150 If the face specifies a fontset name, that fontset determines a
151 pattern for fonts of the given character. If the face specifies a
152 font name or the other font-related attributes, a fontset is
153 realized from the default fontset. In that case, that
154 specification determines a pattern for ASCII characters and the
155 default fontset determines a pattern for multibyte characters.
157 Available fonts on the system on which Emacs runs are then matched
158 against the font pattern. The result of font selection is the best
159 match for the given face attributes in this font list.
161 Font selection can be influenced by the user.
163 1. The user can specify the relative importance he gives the face
164 attributes width, height, weight, and slant by setting
165 face-font-selection-order (faces.el) to a list of face attribute
166 names. The default is '(:width :height :weight :slant), and means
167 that font selection first tries to find a good match for the font
168 width specified by a face, then---within fonts with that
169 width---tries to find a best match for the specified font height,
170 etc.
172 2. Setting face-font-family-alternatives allows the user to
173 specify alternative font families to try if a family specified by a
174 face doesn't exist.
176 3. Setting face-font-registry-alternatives allows the user to
177 specify all alternative font registries to try for a face
178 specifying a registry.
180 4. Setting face-ignored-fonts allows the user to ignore specific
181 fonts.
184 Character composition.
186 Usually, the realization process is already finished when Emacs
187 actually reflects the desired glyph matrix on the screen. However,
188 on displaying a composition (sequence of characters to be composed
189 on the screen), a suitable font for the components of the
190 composition is selected and realized while drawing them on the
191 screen, i.e. the realization process is delayed but in principle
192 the same.
195 Initialization of basic faces.
197 The faces `default', `modeline' are considered `basic faces'.
198 When redisplay happens the first time for a newly created frame,
199 basic faces are realized for CHARSET_ASCII. Frame parameters are
200 used to fill in unspecified attributes of the default face. */
202 #include <config.h>
203 #include "sysstdio.h"
204 #include <sys/types.h>
205 #include <sys/stat.h>
207 #include "lisp.h"
208 #include "character.h"
209 #include "charset.h"
210 #include "keyboard.h"
211 #include "frame.h"
212 #include "termhooks.h"
214 #ifdef USE_MOTIF
215 #include <Xm/Xm.h>
216 #include <Xm/XmStrDefs.h>
217 #endif /* USE_MOTIF */
219 #ifdef MSDOS
220 #include "dosfns.h"
221 #endif
223 #ifdef HAVE_WINDOW_SYSTEM
224 #include TERM_HEADER
225 #include "fontset.h"
226 #ifdef HAVE_NTGUI
227 #define x_display_info w32_display_info
228 #define GCGraphicsExposures 0
229 #endif /* HAVE_NTGUI */
231 #ifdef HAVE_NS
232 #define GCGraphicsExposures 0
233 #endif /* HAVE_NS */
234 #endif /* HAVE_WINDOW_SYSTEM */
236 #include "buffer.h"
237 #include "dispextern.h"
238 #include "blockinput.h"
239 #include "window.h"
240 #include "intervals.h"
241 #include "termchar.h"
243 #include "font.h"
245 #ifdef HAVE_X_WINDOWS
247 /* Compensate for a bug in Xos.h on some systems, on which it requires
248 time.h. On some such systems, Xos.h tries to redefine struct
249 timeval and struct timezone if USG is #defined while it is
250 #included. */
252 #ifdef XOS_NEEDS_TIME_H
253 #include <time.h>
254 #undef USG
255 #include <X11/Xos.h>
256 #define USG
257 #define __TIMEVAL__
258 #if defined USG || defined __TIMEVAL__ /* Don't warn about unused macros. */
259 #endif
260 #else /* not XOS_NEEDS_TIME_H */
261 #include <X11/Xos.h>
262 #endif /* not XOS_NEEDS_TIME_H */
264 #endif /* HAVE_X_WINDOWS */
266 #include <c-ctype.h>
268 /* True if face attribute ATTR is unspecified. */
270 #define UNSPECIFIEDP(ATTR) EQ ((ATTR), Qunspecified)
272 /* True if face attribute ATTR is `ignore-defface'. */
274 #define IGNORE_DEFFACE_P(ATTR) EQ ((ATTR), QCignore_defface)
276 /* Size of hash table of realized faces in face caches (should be a
277 prime number). */
279 #define FACE_CACHE_BUCKETS_SIZE 1001
281 char unspecified_fg[] = "unspecified-fg", unspecified_bg[] = "unspecified-bg";
283 /* Alist of alternative font families. Each element is of the form
284 (FAMILY FAMILY1 FAMILY2 ...). If fonts of FAMILY can't be loaded,
285 try FAMILY1, then FAMILY2, ... */
287 Lisp_Object Vface_alternative_font_family_alist;
289 /* Alist of alternative font registries. Each element is of the form
290 (REGISTRY REGISTRY1 REGISTRY2...). If fonts of REGISTRY can't be
291 loaded, try REGISTRY1, then REGISTRY2, ... */
293 Lisp_Object Vface_alternative_font_registry_alist;
295 /* The next ID to assign to Lisp faces. */
297 static int next_lface_id;
299 /* A vector mapping Lisp face Id's to face names. */
301 static Lisp_Object *lface_id_to_name;
302 static ptrdiff_t lface_id_to_name_size;
304 #ifdef HAVE_WINDOW_SYSTEM
306 /* Counter for calls to clear_face_cache. If this counter reaches
307 CLEAR_FONT_TABLE_COUNT, and a frame has more than
308 CLEAR_FONT_TABLE_NFONTS load, unused fonts are freed. */
310 static int clear_font_table_count;
311 #define CLEAR_FONT_TABLE_COUNT 100
312 #define CLEAR_FONT_TABLE_NFONTS 10
314 #endif /* HAVE_WINDOW_SYSTEM */
316 /* True means face attributes have been changed since the last
317 redisplay. Used in redisplay_internal. */
319 bool face_change;
321 /* True means don't display bold text if a face's foreground
322 and background colors are the inverse of the default colors of the
323 display. This is a kluge to suppress `bold black' foreground text
324 which is hard to read on an LCD monitor. */
326 static bool tty_suppress_bold_inverse_default_colors_p;
328 /* A list of the form `((x . y))' used to avoid consing in
329 Finternal_set_lisp_face_attribute. */
331 static Lisp_Object Vparam_value_alist;
333 /* The total number of colors currently allocated. */
335 #ifdef GLYPH_DEBUG
336 static int ncolors_allocated;
337 static int npixmaps_allocated;
338 static int ngcs;
339 #endif
341 /* True means the definition of the `menu' face for new frames has
342 been changed. */
344 static bool menu_face_changed_default;
346 struct named_merge_point;
348 static struct face *realize_face (struct face_cache *, Lisp_Object *,
349 int);
350 static struct face *realize_x_face (struct face_cache *, Lisp_Object *);
351 static struct face *realize_tty_face (struct face_cache *, Lisp_Object *);
352 static bool realize_basic_faces (struct frame *);
353 static bool realize_default_face (struct frame *);
354 static void realize_named_face (struct frame *, Lisp_Object, int);
355 static struct face_cache *make_face_cache (struct frame *);
356 static void free_face_cache (struct face_cache *);
357 static bool merge_face_ref (struct frame *, Lisp_Object, Lisp_Object *,
358 bool, struct named_merge_point *);
359 static int color_distance (XColor *x, XColor *y);
361 #ifdef HAVE_WINDOW_SYSTEM
362 static void set_font_frame_param (Lisp_Object, Lisp_Object);
363 static void clear_face_gcs (struct face_cache *);
364 static struct face *realize_non_ascii_face (struct frame *, Lisp_Object,
365 struct face *);
366 #endif /* HAVE_WINDOW_SYSTEM */
368 /***********************************************************************
369 Utilities
370 ***********************************************************************/
372 #ifdef HAVE_X_WINDOWS
374 #ifdef DEBUG_X_COLORS
376 /* The following is a poor mans infrastructure for debugging X color
377 allocation problems on displays with PseudoColor-8. Some X servers
378 like 3.3.5 XF86_SVGA with Matrox cards apparently don't implement
379 color reference counts completely so that they don't signal an
380 error when a color is freed whose reference count is already 0.
381 Other X servers do. To help me debug this, the following code
382 implements a simple reference counting schema of its own, for a
383 single display/screen. --gerd. */
385 /* Reference counts for pixel colors. */
387 int color_count[256];
389 /* Register color PIXEL as allocated. */
391 void
392 register_color (unsigned long pixel)
394 eassert (pixel < 256);
395 ++color_count[pixel];
399 /* Register color PIXEL as deallocated. */
401 void
402 unregister_color (unsigned long pixel)
404 eassert (pixel < 256);
405 if (color_count[pixel] > 0)
406 --color_count[pixel];
407 else
408 emacs_abort ();
412 /* Register N colors from PIXELS as deallocated. */
414 void
415 unregister_colors (unsigned long *pixels, int n)
417 int i;
418 for (i = 0; i < n; ++i)
419 unregister_color (pixels[i]);
423 DEFUN ("dump-colors", Fdump_colors, Sdump_colors, 0, 0, 0,
424 doc: /* Dump currently allocated colors to stderr. */)
425 (void)
427 int i, n;
429 fputc ('\n', stderr);
431 for (i = n = 0; i < ARRAYELTS (color_count); ++i)
432 if (color_count[i])
434 fprintf (stderr, "%3d: %5d", i, color_count[i]);
435 ++n;
436 if (n % 5 == 0)
437 fputc ('\n', stderr);
438 else
439 fputc ('\t', stderr);
442 if (n % 5 != 0)
443 fputc ('\n', stderr);
444 return Qnil;
447 #endif /* DEBUG_X_COLORS */
450 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
451 color values. Interrupt input must be blocked when this function
452 is called. */
454 void
455 x_free_colors (struct frame *f, unsigned long *pixels, int npixels)
457 /* If display has an immutable color map, freeing colors is not
458 necessary and some servers don't allow it. So don't do it. */
459 if (x_mutable_colormap (FRAME_X_VISUAL (f)))
461 #ifdef DEBUG_X_COLORS
462 unregister_colors (pixels, npixels);
463 #endif
464 XFreeColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
465 pixels, npixels, 0);
470 #ifdef USE_X_TOOLKIT
472 /* Free colors used on display DPY. PIXELS is an array of NPIXELS pixel
473 color values. Interrupt input must be blocked when this function
474 is called. */
476 void
477 x_free_dpy_colors (Display *dpy, Screen *screen, Colormap cmap,
478 unsigned long *pixels, int npixels)
480 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
482 /* If display has an immutable color map, freeing colors is not
483 necessary and some servers don't allow it. So don't do it. */
484 if (x_mutable_colormap (dpyinfo->visual))
486 #ifdef DEBUG_X_COLORS
487 unregister_colors (pixels, npixels);
488 #endif
489 XFreeColors (dpy, cmap, pixels, npixels, 0);
492 #endif /* USE_X_TOOLKIT */
494 /* Create and return a GC for use on frame F. GC values and mask
495 are given by XGCV and MASK. */
497 static GC
498 x_create_gc (struct frame *f, unsigned long mask, XGCValues *xgcv)
500 GC gc;
501 block_input ();
502 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), mask, xgcv);
503 unblock_input ();
504 IF_DEBUG (++ngcs);
505 return gc;
509 /* Free GC which was used on frame F. */
511 static void
512 x_free_gc (struct frame *f, GC gc)
514 eassert (input_blocked_p ());
515 IF_DEBUG ((--ngcs, eassert (ngcs >= 0)));
516 XFreeGC (FRAME_X_DISPLAY (f), gc);
519 #endif /* HAVE_X_WINDOWS */
521 #ifdef HAVE_NTGUI
522 /* W32 emulation of GCs */
524 static GC
525 x_create_gc (struct frame *f, unsigned long mask, XGCValues *xgcv)
527 GC gc;
528 block_input ();
529 gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, xgcv);
530 unblock_input ();
531 IF_DEBUG (++ngcs);
532 return gc;
536 /* Free GC which was used on frame F. */
538 static void
539 x_free_gc (struct frame *f, GC gc)
541 IF_DEBUG ((--ngcs, eassert (ngcs >= 0)));
542 xfree (gc);
545 #endif /* HAVE_NTGUI */
547 #ifdef HAVE_NS
548 /* NS emulation of GCs */
550 static GC
551 x_create_gc (struct frame *f,
552 unsigned long mask,
553 XGCValues *xgcv)
555 GC gc = xmalloc (sizeof *gc);
556 *gc = *xgcv;
557 return gc;
560 static void
561 x_free_gc (struct frame *f, GC gc)
563 xfree (gc);
565 #endif /* HAVE_NS */
567 /***********************************************************************
568 Frames and faces
569 ***********************************************************************/
571 /* Initialize face cache and basic faces for frame F. */
573 void
574 init_frame_faces (struct frame *f)
576 /* Make a face cache, if F doesn't have one. */
577 if (FRAME_FACE_CACHE (f) == NULL)
578 FRAME_FACE_CACHE (f) = make_face_cache (f);
580 #ifdef HAVE_WINDOW_SYSTEM
581 /* Make the image cache. */
582 if (FRAME_WINDOW_P (f))
584 /* We initialize the image cache when creating the first frame
585 on a terminal, and not during terminal creation. This way,
586 `x-open-connection' on a tty won't create an image cache. */
587 if (FRAME_IMAGE_CACHE (f) == NULL)
588 FRAME_IMAGE_CACHE (f) = make_image_cache ();
589 ++FRAME_IMAGE_CACHE (f)->refcount;
591 #endif /* HAVE_WINDOW_SYSTEM */
593 /* Realize faces early (Bug#17889). */
594 if (!realize_basic_faces (f))
595 emacs_abort ();
599 /* Free face cache of frame F. Called from frame-dependent
600 resource freeing function, e.g. (x|tty)_free_frame_resources. */
602 void
603 free_frame_faces (struct frame *f)
605 struct face_cache *face_cache = FRAME_FACE_CACHE (f);
607 if (face_cache)
609 free_face_cache (face_cache);
610 FRAME_FACE_CACHE (f) = NULL;
613 #ifdef HAVE_WINDOW_SYSTEM
614 if (FRAME_WINDOW_P (f))
616 struct image_cache *image_cache = FRAME_IMAGE_CACHE (f);
617 if (image_cache)
619 --image_cache->refcount;
620 if (image_cache->refcount == 0)
621 free_image_cache (f);
624 #endif /* HAVE_WINDOW_SYSTEM */
628 /* Clear face caches, and recompute basic faces for frame F. Call
629 this after changing frame parameters on which those faces depend,
630 or when realized faces have been freed due to changing attributes
631 of named faces. */
633 void
634 recompute_basic_faces (struct frame *f)
636 if (FRAME_FACE_CACHE (f))
638 clear_face_cache (false);
639 if (!realize_basic_faces (f))
640 emacs_abort ();
645 /* Clear the face caches of all frames. CLEAR_FONTS_P means
646 try to free unused fonts, too. */
648 void
649 clear_face_cache (bool clear_fonts_p)
651 #ifdef HAVE_WINDOW_SYSTEM
652 Lisp_Object tail, frame;
654 if (clear_fonts_p
655 || ++clear_font_table_count == CLEAR_FONT_TABLE_COUNT)
657 /* From time to time see if we can unload some fonts. This also
658 frees all realized faces on all frames. Fonts needed by
659 faces will be loaded again when faces are realized again. */
660 clear_font_table_count = 0;
662 FOR_EACH_FRAME (tail, frame)
664 struct frame *f = XFRAME (frame);
665 if (FRAME_WINDOW_P (f)
666 && FRAME_DISPLAY_INFO (f)->n_fonts > CLEAR_FONT_TABLE_NFONTS)
668 clear_font_cache (f);
669 free_all_realized_faces (frame);
673 else
675 /* Clear GCs of realized faces. */
676 FOR_EACH_FRAME (tail, frame)
678 struct frame *f = XFRAME (frame);
679 if (FRAME_WINDOW_P (f))
680 clear_face_gcs (FRAME_FACE_CACHE (f));
682 clear_image_caches (Qnil);
684 #endif /* HAVE_WINDOW_SYSTEM */
687 DEFUN ("clear-face-cache", Fclear_face_cache, Sclear_face_cache, 0, 1, 0,
688 doc: /* Clear face caches on all frames.
689 Optional THOROUGHLY non-nil means try to free unused fonts, too. */)
690 (Lisp_Object thoroughly)
692 clear_face_cache (!NILP (thoroughly));
693 face_change = true;
694 windows_or_buffers_changed = 53;
695 return Qnil;
699 /***********************************************************************
700 X Pixmaps
701 ***********************************************************************/
703 #ifdef HAVE_WINDOW_SYSTEM
705 DEFUN ("bitmap-spec-p", Fbitmap_spec_p, Sbitmap_spec_p, 1, 1, 0,
706 doc: /* Value is non-nil if OBJECT is a valid bitmap specification.
707 A bitmap specification is either a string, a file name, or a list
708 (WIDTH HEIGHT DATA) where WIDTH is the pixel width of the bitmap,
709 HEIGHT is its height, and DATA is a string containing the bits of
710 the pixmap. Bits are stored row by row, each row occupies
711 (WIDTH + 7)/8 bytes. */)
712 (Lisp_Object object)
714 bool pixmap_p = false;
716 if (STRINGP (object))
717 /* If OBJECT is a string, it's a file name. */
718 pixmap_p = true;
719 else if (CONSP (object))
721 /* Otherwise OBJECT must be (WIDTH HEIGHT DATA), WIDTH and
722 HEIGHT must be ints > 0, and DATA must be string large
723 enough to hold a bitmap of the specified size. */
724 Lisp_Object width, height, data;
726 height = width = data = Qnil;
728 if (CONSP (object))
730 width = XCAR (object);
731 object = XCDR (object);
732 if (CONSP (object))
734 height = XCAR (object);
735 object = XCDR (object);
736 if (CONSP (object))
737 data = XCAR (object);
741 if (STRINGP (data)
742 && RANGED_INTEGERP (1, width, INT_MAX)
743 && RANGED_INTEGERP (1, height, INT_MAX))
745 int bytes_per_row = ((XINT (width) + BITS_PER_CHAR - 1)
746 / BITS_PER_CHAR);
747 if (XINT (height) <= SBYTES (data) / bytes_per_row)
748 pixmap_p = true;
752 return pixmap_p ? Qt : Qnil;
756 /* Load a bitmap according to NAME (which is either a file name or a
757 pixmap spec) for use on frame F. Value is the bitmap_id (see
758 xfns.c). If NAME is nil, return with a bitmap id of zero. If
759 bitmap cannot be loaded, display a message saying so, and return
760 zero. */
762 static ptrdiff_t
763 load_pixmap (struct frame *f, Lisp_Object name)
765 ptrdiff_t bitmap_id;
767 if (NILP (name))
768 return 0;
770 CHECK_TYPE (!NILP (Fbitmap_spec_p (name)), Qbitmap_spec_p, name);
772 block_input ();
773 if (CONSP (name))
775 /* Decode a bitmap spec into a bitmap. */
777 int h, w;
778 Lisp_Object bits;
780 w = XINT (Fcar (name));
781 h = XINT (Fcar (Fcdr (name)));
782 bits = Fcar (Fcdr (Fcdr (name)));
784 bitmap_id = x_create_bitmap_from_data (f, SSDATA (bits),
785 w, h);
787 else
789 /* It must be a string -- a file name. */
790 bitmap_id = x_create_bitmap_from_file (f, name);
792 unblock_input ();
794 if (bitmap_id < 0)
796 add_to_log ("Invalid or undefined bitmap `%s'", name);
797 bitmap_id = 0;
799 else
801 #ifdef GLYPH_DEBUG
802 ++npixmaps_allocated;
803 #endif
806 return bitmap_id;
809 #endif /* HAVE_WINDOW_SYSTEM */
813 /***********************************************************************
814 X Colors
815 ***********************************************************************/
817 /* Parse RGB_LIST, and fill in the RGB fields of COLOR.
818 RGB_LIST should contain (at least) 3 lisp integers.
819 Return true iff RGB_LIST is OK. */
821 static bool
822 parse_rgb_list (Lisp_Object rgb_list, XColor *color)
824 #define PARSE_RGB_LIST_FIELD(field) \
825 if (CONSP (rgb_list) && INTEGERP (XCAR (rgb_list))) \
827 color->field = XINT (XCAR (rgb_list)); \
828 rgb_list = XCDR (rgb_list); \
830 else \
831 return false;
833 PARSE_RGB_LIST_FIELD (red);
834 PARSE_RGB_LIST_FIELD (green);
835 PARSE_RGB_LIST_FIELD (blue);
837 return true;
841 /* Lookup on frame F the color described by the lisp string COLOR.
842 The resulting tty color is returned in TTY_COLOR; if STD_COLOR is
843 non-zero, then the `standard' definition of the same color is
844 returned in it. */
846 static bool
847 tty_lookup_color (struct frame *f, Lisp_Object color, XColor *tty_color,
848 XColor *std_color)
850 Lisp_Object frame, color_desc;
852 if (!STRINGP (color) || NILP (Ffboundp (Qtty_color_desc)))
853 return false;
855 XSETFRAME (frame, f);
857 color_desc = call2 (Qtty_color_desc, color, frame);
858 if (CONSP (color_desc) && CONSP (XCDR (color_desc)))
860 Lisp_Object rgb;
862 if (! INTEGERP (XCAR (XCDR (color_desc))))
863 return false;
865 tty_color->pixel = XINT (XCAR (XCDR (color_desc)));
867 rgb = XCDR (XCDR (color_desc));
868 if (! parse_rgb_list (rgb, tty_color))
869 return false;
871 /* Should we fill in STD_COLOR too? */
872 if (std_color)
874 /* Default STD_COLOR to the same as TTY_COLOR. */
875 *std_color = *tty_color;
877 /* Do a quick check to see if the returned descriptor is
878 actually _exactly_ equal to COLOR, otherwise we have to
879 lookup STD_COLOR separately. If it's impossible to lookup
880 a standard color, we just give up and use TTY_COLOR. */
881 if ((!STRINGP (XCAR (color_desc))
882 || NILP (Fstring_equal (color, XCAR (color_desc))))
883 && !NILP (Ffboundp (Qtty_color_standard_values)))
885 /* Look up STD_COLOR separately. */
886 rgb = call1 (Qtty_color_standard_values, color);
887 if (! parse_rgb_list (rgb, std_color))
888 return false;
892 return true;
894 else if (NILP (Fsymbol_value (intern ("tty-defined-color-alist"))))
895 /* We were called early during startup, and the colors are not
896 yet set up in tty-defined-color-alist. Don't return a failure
897 indication, since this produces the annoying "Unable to
898 load color" messages in the *Messages* buffer. */
899 return true;
900 else
901 /* tty-color-desc seems to have returned a bad value. */
902 return false;
905 /* A version of defined_color for non-X frames. */
907 static bool
908 tty_defined_color (struct frame *f, const char *color_name,
909 XColor *color_def, bool alloc)
911 bool status = true;
913 /* Defaults. */
914 color_def->pixel = FACE_TTY_DEFAULT_COLOR;
915 color_def->red = 0;
916 color_def->blue = 0;
917 color_def->green = 0;
919 if (*color_name)
920 status = tty_lookup_color (f, build_string (color_name), color_def, NULL);
922 if (color_def->pixel == FACE_TTY_DEFAULT_COLOR && *color_name)
924 if (strcmp (color_name, "unspecified-fg") == 0)
925 color_def->pixel = FACE_TTY_DEFAULT_FG_COLOR;
926 else if (strcmp (color_name, "unspecified-bg") == 0)
927 color_def->pixel = FACE_TTY_DEFAULT_BG_COLOR;
930 if (color_def->pixel != FACE_TTY_DEFAULT_COLOR)
931 status = true;
933 return status;
937 /* Decide if color named COLOR_NAME is valid for the display
938 associated with the frame F; if so, return the rgb values in
939 COLOR_DEF. If ALLOC, allocate a new colormap cell.
941 This does the right thing for any type of frame. */
943 static bool
944 defined_color (struct frame *f, const char *color_name, XColor *color_def,
945 bool alloc)
947 if (!FRAME_WINDOW_P (f))
948 return tty_defined_color (f, color_name, color_def, alloc);
949 #ifdef HAVE_X_WINDOWS
950 else if (FRAME_X_P (f))
951 return x_defined_color (f, color_name, color_def, alloc);
952 #endif
953 #ifdef HAVE_NTGUI
954 else if (FRAME_W32_P (f))
955 return w32_defined_color (f, color_name, color_def, alloc);
956 #endif
957 #ifdef HAVE_NS
958 else if (FRAME_NS_P (f))
959 return ns_defined_color (f, color_name, color_def, alloc, true);
960 #endif
961 else
962 emacs_abort ();
966 /* Given the index IDX of a tty color on frame F, return its name, a
967 Lisp string. */
969 Lisp_Object
970 tty_color_name (struct frame *f, int idx)
972 if (idx >= 0 && !NILP (Ffboundp (Qtty_color_by_index)))
974 Lisp_Object frame;
975 Lisp_Object coldesc;
977 XSETFRAME (frame, f);
978 coldesc = call2 (Qtty_color_by_index, make_number (idx), frame);
980 if (!NILP (coldesc))
981 return XCAR (coldesc);
983 #ifdef MSDOS
984 /* We can have an MS-DOS frame under -nw for a short window of
985 opportunity before internal_terminal_init is called. DTRT. */
986 if (FRAME_MSDOS_P (f) && !inhibit_window_system)
987 return msdos_stdcolor_name (idx);
988 #endif
990 if (idx == FACE_TTY_DEFAULT_FG_COLOR)
991 return build_string (unspecified_fg);
992 if (idx == FACE_TTY_DEFAULT_BG_COLOR)
993 return build_string (unspecified_bg);
995 return Qunspecified;
999 /* Return true if COLOR_NAME is a shade of gray (or white or
1000 black) on frame F.
1002 The criterion implemented here is not a terribly sophisticated one. */
1004 static bool
1005 face_color_gray_p (struct frame *f, const char *color_name)
1007 XColor color;
1008 bool gray_p;
1010 if (defined_color (f, color_name, &color, false))
1011 gray_p = (/* Any color sufficiently close to black counts as gray. */
1012 (color.red < 5000 && color.green < 5000 && color.blue < 5000)
1014 ((eabs (color.red - color.green)
1015 < max (color.red, color.green) / 20)
1016 && (eabs (color.green - color.blue)
1017 < max (color.green, color.blue) / 20)
1018 && (eabs (color.blue - color.red)
1019 < max (color.blue, color.red) / 20)));
1020 else
1021 gray_p = false;
1023 return gray_p;
1027 /* Return true if color COLOR_NAME can be displayed on frame F.
1028 BACKGROUND_P means the color will be used as background color. */
1030 static bool
1031 face_color_supported_p (struct frame *f, const char *color_name,
1032 bool background_p)
1034 Lisp_Object frame;
1035 XColor not_used;
1037 XSETFRAME (frame, f);
1038 return
1039 #ifdef HAVE_WINDOW_SYSTEM
1040 FRAME_WINDOW_P (f)
1041 ? (!NILP (Fxw_display_color_p (frame))
1042 || xstrcasecmp (color_name, "black") == 0
1043 || xstrcasecmp (color_name, "white") == 0
1044 || (background_p
1045 && face_color_gray_p (f, color_name))
1046 || (!NILP (Fx_display_grayscale_p (frame))
1047 && face_color_gray_p (f, color_name)))
1049 #endif
1050 tty_defined_color (f, color_name, &not_used, false);
1054 DEFUN ("color-gray-p", Fcolor_gray_p, Scolor_gray_p, 1, 2, 0,
1055 doc: /* Return non-nil if COLOR is a shade of gray (or white or black).
1056 FRAME specifies the frame and thus the display for interpreting COLOR.
1057 If FRAME is nil or omitted, use the selected frame. */)
1058 (Lisp_Object color, Lisp_Object frame)
1060 CHECK_STRING (color);
1061 return (face_color_gray_p (decode_any_frame (frame), SSDATA (color))
1062 ? Qt : Qnil);
1066 DEFUN ("color-supported-p", Fcolor_supported_p,
1067 Scolor_supported_p, 1, 3, 0,
1068 doc: /* Return non-nil if COLOR can be displayed on FRAME.
1069 BACKGROUND-P non-nil means COLOR is used as a background.
1070 Otherwise, this function tells whether it can be used as a foreground.
1071 If FRAME is nil or omitted, use the selected frame.
1072 COLOR must be a valid color name. */)
1073 (Lisp_Object color, Lisp_Object frame, Lisp_Object background_p)
1075 CHECK_STRING (color);
1076 return (face_color_supported_p (decode_any_frame (frame),
1077 SSDATA (color), !NILP (background_p))
1078 ? Qt : Qnil);
1082 static unsigned long
1083 load_color2 (struct frame *f, struct face *face, Lisp_Object name,
1084 enum lface_attribute_index target_index, XColor *color)
1086 eassert (STRINGP (name));
1087 eassert (target_index == LFACE_FOREGROUND_INDEX
1088 || target_index == LFACE_BACKGROUND_INDEX
1089 || target_index == LFACE_UNDERLINE_INDEX
1090 || target_index == LFACE_OVERLINE_INDEX
1091 || target_index == LFACE_STRIKE_THROUGH_INDEX
1092 || target_index == LFACE_BOX_INDEX);
1094 /* if the color map is full, defined_color will return a best match
1095 to the values in an existing cell. */
1096 if (!defined_color (f, SSDATA (name), color, true))
1098 add_to_log ("Unable to load color \"%s\"", name);
1100 switch (target_index)
1102 case LFACE_FOREGROUND_INDEX:
1103 face->foreground_defaulted_p = true;
1104 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1105 break;
1107 case LFACE_BACKGROUND_INDEX:
1108 face->background_defaulted_p = true;
1109 color->pixel = FRAME_BACKGROUND_PIXEL (f);
1110 break;
1112 case LFACE_UNDERLINE_INDEX:
1113 face->underline_defaulted_p = true;
1114 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1115 break;
1117 case LFACE_OVERLINE_INDEX:
1118 face->overline_color_defaulted_p = true;
1119 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1120 break;
1122 case LFACE_STRIKE_THROUGH_INDEX:
1123 face->strike_through_color_defaulted_p = true;
1124 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1125 break;
1127 case LFACE_BOX_INDEX:
1128 face->box_color_defaulted_p = true;
1129 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1130 break;
1132 default:
1133 emacs_abort ();
1136 #ifdef GLYPH_DEBUG
1137 else
1138 ++ncolors_allocated;
1139 #endif
1141 return color->pixel;
1144 /* Load color with name NAME for use by face FACE on frame F.
1145 TARGET_INDEX must be one of LFACE_FOREGROUND_INDEX,
1146 LFACE_BACKGROUND_INDEX, LFACE_UNDERLINE_INDEX, LFACE_OVERLINE_INDEX,
1147 LFACE_STRIKE_THROUGH_INDEX, or LFACE_BOX_INDEX. Value is the
1148 pixel color. If color cannot be loaded, display a message, and
1149 return the foreground, background or underline color of F, but
1150 record that fact in flags of the face so that we don't try to free
1151 these colors. */
1153 unsigned long
1154 load_color (struct frame *f, struct face *face, Lisp_Object name,
1155 enum lface_attribute_index target_index)
1157 XColor color;
1158 return load_color2 (f, face, name, target_index, &color);
1162 #ifdef HAVE_WINDOW_SYSTEM
1164 #define NEAR_SAME_COLOR_THRESHOLD 30000
1166 /* Load colors for face FACE which is used on frame F. Colors are
1167 specified by slots LFACE_BACKGROUND_INDEX and LFACE_FOREGROUND_INDEX
1168 of ATTRS. If the background color specified is not supported on F,
1169 try to emulate gray colors with a stipple from Vface_default_stipple. */
1171 static void
1172 load_face_colors (struct frame *f, struct face *face,
1173 Lisp_Object attrs[LFACE_VECTOR_SIZE])
1175 Lisp_Object fg, bg, dfg;
1176 XColor xfg, xbg;
1178 bg = attrs[LFACE_BACKGROUND_INDEX];
1179 fg = attrs[LFACE_FOREGROUND_INDEX];
1181 /* Swap colors if face is inverse-video. */
1182 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1184 Lisp_Object tmp;
1185 tmp = fg;
1186 fg = bg;
1187 bg = tmp;
1190 /* Check for support for foreground, not for background because
1191 face_color_supported_p is smart enough to know that grays are
1192 "supported" as background because we are supposed to use stipple
1193 for them. */
1194 if (!face_color_supported_p (f, SSDATA (bg), false)
1195 && !NILP (Fbitmap_spec_p (Vface_default_stipple)))
1197 x_destroy_bitmap (f, face->stipple);
1198 face->stipple = load_pixmap (f, Vface_default_stipple);
1201 face->background = load_color2 (f, face, bg, LFACE_BACKGROUND_INDEX, &xbg);
1202 face->foreground = load_color2 (f, face, fg, LFACE_FOREGROUND_INDEX, &xfg);
1204 dfg = attrs[LFACE_DISTANT_FOREGROUND_INDEX];
1205 if (!NILP (dfg) && !UNSPECIFIEDP (dfg)
1206 && color_distance (&xbg, &xfg) < NEAR_SAME_COLOR_THRESHOLD)
1208 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1209 face->background = load_color (f, face, dfg, LFACE_BACKGROUND_INDEX);
1210 else
1211 face->foreground = load_color (f, face, dfg, LFACE_FOREGROUND_INDEX);
1215 #ifdef HAVE_X_WINDOWS
1217 /* Free color PIXEL on frame F. */
1219 void
1220 unload_color (struct frame *f, unsigned long pixel)
1222 if (pixel != -1)
1224 block_input ();
1225 x_free_colors (f, &pixel, 1);
1226 unblock_input ();
1230 /* Free colors allocated for FACE. */
1232 static void
1233 free_face_colors (struct frame *f, struct face *face)
1235 /* PENDING(NS): need to do something here? */
1237 if (face->colors_copied_bitwise_p)
1238 return;
1240 block_input ();
1242 if (!face->foreground_defaulted_p)
1244 x_free_colors (f, &face->foreground, 1);
1245 IF_DEBUG (--ncolors_allocated);
1248 if (!face->background_defaulted_p)
1250 x_free_colors (f, &face->background, 1);
1251 IF_DEBUG (--ncolors_allocated);
1254 if (face->underline_p
1255 && !face->underline_defaulted_p)
1257 x_free_colors (f, &face->underline_color, 1);
1258 IF_DEBUG (--ncolors_allocated);
1261 if (face->overline_p
1262 && !face->overline_color_defaulted_p)
1264 x_free_colors (f, &face->overline_color, 1);
1265 IF_DEBUG (--ncolors_allocated);
1268 if (face->strike_through_p
1269 && !face->strike_through_color_defaulted_p)
1271 x_free_colors (f, &face->strike_through_color, 1);
1272 IF_DEBUG (--ncolors_allocated);
1275 if (face->box != FACE_NO_BOX
1276 && !face->box_color_defaulted_p)
1278 x_free_colors (f, &face->box_color, 1);
1279 IF_DEBUG (--ncolors_allocated);
1282 unblock_input ();
1285 #endif /* HAVE_X_WINDOWS */
1287 #endif /* HAVE_WINDOW_SYSTEM */
1291 /***********************************************************************
1292 XLFD Font Names
1293 ***********************************************************************/
1295 /* An enumerator for each field of an XLFD font name. */
1297 enum xlfd_field
1299 XLFD_FOUNDRY,
1300 XLFD_FAMILY,
1301 XLFD_WEIGHT,
1302 XLFD_SLANT,
1303 XLFD_SWIDTH,
1304 XLFD_ADSTYLE,
1305 XLFD_PIXEL_SIZE,
1306 XLFD_POINT_SIZE,
1307 XLFD_RESX,
1308 XLFD_RESY,
1309 XLFD_SPACING,
1310 XLFD_AVGWIDTH,
1311 XLFD_REGISTRY,
1312 XLFD_ENCODING,
1313 XLFD_LAST
1316 /* An enumerator for each possible slant value of a font. Taken from
1317 the XLFD specification. */
1319 enum xlfd_slant
1321 XLFD_SLANT_UNKNOWN,
1322 XLFD_SLANT_ROMAN,
1323 XLFD_SLANT_ITALIC,
1324 XLFD_SLANT_OBLIQUE,
1325 XLFD_SLANT_REVERSE_ITALIC,
1326 XLFD_SLANT_REVERSE_OBLIQUE,
1327 XLFD_SLANT_OTHER
1330 /* Relative font weight according to XLFD documentation. */
1332 enum xlfd_weight
1334 XLFD_WEIGHT_UNKNOWN,
1335 XLFD_WEIGHT_ULTRA_LIGHT, /* 10 */
1336 XLFD_WEIGHT_EXTRA_LIGHT, /* 20 */
1337 XLFD_WEIGHT_LIGHT, /* 30 */
1338 XLFD_WEIGHT_SEMI_LIGHT, /* 40: SemiLight, Book, ... */
1339 XLFD_WEIGHT_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1340 XLFD_WEIGHT_SEMI_BOLD, /* 60: SemiBold, DemiBold, ... */
1341 XLFD_WEIGHT_BOLD, /* 70: Bold, ... */
1342 XLFD_WEIGHT_EXTRA_BOLD, /* 80: ExtraBold, Heavy, ... */
1343 XLFD_WEIGHT_ULTRA_BOLD /* 90: UltraBold, Black, ... */
1346 /* Relative proportionate width. */
1348 enum xlfd_swidth
1350 XLFD_SWIDTH_UNKNOWN,
1351 XLFD_SWIDTH_ULTRA_CONDENSED, /* 10 */
1352 XLFD_SWIDTH_EXTRA_CONDENSED, /* 20 */
1353 XLFD_SWIDTH_CONDENSED, /* 30: Condensed, Narrow, Compressed, ... */
1354 XLFD_SWIDTH_SEMI_CONDENSED, /* 40: semicondensed */
1355 XLFD_SWIDTH_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1356 XLFD_SWIDTH_SEMI_EXPANDED, /* 60: SemiExpanded, DemiExpanded, ... */
1357 XLFD_SWIDTH_EXPANDED, /* 70: Expanded... */
1358 XLFD_SWIDTH_EXTRA_EXPANDED, /* 80: ExtraExpanded, Wide... */
1359 XLFD_SWIDTH_ULTRA_EXPANDED /* 90: UltraExpanded... */
1362 /* Order by which font selection chooses fonts. The default values
1363 mean `first, find a best match for the font width, then for the
1364 font height, then for weight, then for slant.' This variable can be
1365 set via set-face-font-sort-order. */
1367 static int font_sort_order[4];
1369 #ifdef HAVE_WINDOW_SYSTEM
1371 static enum font_property_index font_props_for_sorting[FONT_SIZE_INDEX];
1373 static int
1374 compare_fonts_by_sort_order (const void *v1, const void *v2)
1376 Lisp_Object const *p1 = v1;
1377 Lisp_Object const *p2 = v2;
1378 Lisp_Object font1 = *p1;
1379 Lisp_Object font2 = *p2;
1380 int i;
1382 for (i = 0; i < FONT_SIZE_INDEX; i++)
1384 enum font_property_index idx = font_props_for_sorting[i];
1385 Lisp_Object val1 = AREF (font1, idx), val2 = AREF (font2, idx);
1386 int result;
1388 if (idx <= FONT_REGISTRY_INDEX)
1390 if (STRINGP (val1))
1391 result = STRINGP (val2) ? strcmp (SSDATA (val1), SSDATA (val2)) : -1;
1392 else
1393 result = STRINGP (val2) ? 1 : 0;
1395 else
1397 if (INTEGERP (val1))
1398 result = (INTEGERP (val2) && XINT (val1) >= XINT (val2)
1399 ? XINT (val1) > XINT (val2)
1400 : -1);
1401 else
1402 result = INTEGERP (val2) ? 1 : 0;
1404 if (result)
1405 return result;
1407 return 0;
1410 DEFUN ("x-family-fonts", Fx_family_fonts, Sx_family_fonts, 0, 2, 0,
1411 doc: /* Return a list of available fonts of family FAMILY on FRAME.
1412 If FAMILY is omitted or nil, list all families.
1413 Otherwise, FAMILY must be a string, possibly containing wildcards
1414 `?' and `*'.
1415 If FRAME is omitted or nil, use the selected frame.
1416 Each element of the result is a vector [FAMILY WIDTH POINT-SIZE WEIGHT
1417 SLANT FIXED-P FULL REGISTRY-AND-ENCODING].
1418 FAMILY is the font family name. POINT-SIZE is the size of the
1419 font in 1/10 pt. WIDTH, WEIGHT, and SLANT are symbols describing the
1420 width, weight and slant of the font. These symbols are the same as for
1421 face attributes. FIXED-P is non-nil if the font is fixed-pitch.
1422 FULL is the full name of the font, and REGISTRY-AND-ENCODING is a string
1423 giving the registry and encoding of the font.
1424 The result list is sorted according to the current setting of
1425 the face font sort order. */)
1426 (Lisp_Object family, Lisp_Object frame)
1428 Lisp_Object font_spec, list, *drivers, vec;
1429 struct frame *f = decode_live_frame (frame);
1430 ptrdiff_t i, nfonts;
1431 EMACS_INT ndrivers;
1432 Lisp_Object result;
1433 USE_SAFE_ALLOCA;
1435 font_spec = Ffont_spec (0, NULL);
1436 if (!NILP (family))
1438 CHECK_STRING (family);
1439 font_parse_family_registry (family, Qnil, font_spec);
1442 list = font_list_entities (f, font_spec);
1443 if (NILP (list))
1444 return Qnil;
1446 /* Sort the font entities. */
1447 for (i = 0; i < 4; i++)
1448 switch (font_sort_order[i])
1450 case XLFD_SWIDTH:
1451 font_props_for_sorting[i] = FONT_WIDTH_INDEX; break;
1452 case XLFD_POINT_SIZE:
1453 font_props_for_sorting[i] = FONT_SIZE_INDEX; break;
1454 case XLFD_WEIGHT:
1455 font_props_for_sorting[i] = FONT_WEIGHT_INDEX; break;
1456 default:
1457 font_props_for_sorting[i] = FONT_SLANT_INDEX; break;
1459 font_props_for_sorting[i++] = FONT_FAMILY_INDEX;
1460 font_props_for_sorting[i++] = FONT_FOUNDRY_INDEX;
1461 font_props_for_sorting[i++] = FONT_ADSTYLE_INDEX;
1462 font_props_for_sorting[i++] = FONT_REGISTRY_INDEX;
1464 ndrivers = XINT (Flength (list));
1465 SAFE_ALLOCA_LISP (drivers, ndrivers);
1466 for (i = 0; i < ndrivers; i++, list = XCDR (list))
1467 drivers[i] = XCAR (list);
1468 vec = Fvconcat (ndrivers, drivers);
1469 nfonts = ASIZE (vec);
1471 qsort (XVECTOR (vec)->contents, nfonts, word_size,
1472 compare_fonts_by_sort_order);
1474 result = Qnil;
1475 for (i = nfonts - 1; i >= 0; --i)
1477 Lisp_Object font = AREF (vec, i);
1478 Lisp_Object v = make_uninit_vector (8);
1479 int point;
1480 Lisp_Object spacing;
1482 ASET (v, 0, AREF (font, FONT_FAMILY_INDEX));
1483 ASET (v, 1, FONT_WIDTH_SYMBOLIC (font));
1484 point = PIXEL_TO_POINT (XINT (AREF (font, FONT_SIZE_INDEX)) * 10,
1485 FRAME_RES_Y (f));
1486 ASET (v, 2, make_number (point));
1487 ASET (v, 3, FONT_WEIGHT_SYMBOLIC (font));
1488 ASET (v, 4, FONT_SLANT_SYMBOLIC (font));
1489 spacing = Ffont_get (font, QCspacing);
1490 ASET (v, 5, (NILP (spacing) || EQ (spacing, Qp)) ? Qnil : Qt);
1491 ASET (v, 6, Ffont_xlfd_name (font, Qnil));
1492 ASET (v, 7, AREF (font, FONT_REGISTRY_INDEX));
1494 result = Fcons (v, result);
1497 SAFE_FREE ();
1498 return result;
1501 DEFUN ("x-list-fonts", Fx_list_fonts, Sx_list_fonts, 1, 5, 0,
1502 doc: /* Return a list of the names of available fonts matching PATTERN.
1503 If optional arguments FACE and FRAME are specified, return only fonts
1504 the same size as FACE on FRAME.
1506 PATTERN should be a string containing a font name in the XLFD,
1507 Fontconfig, or GTK format. A font name given in the XLFD format may
1508 contain wildcard characters:
1509 the * character matches any substring, and
1510 the ? character matches any single character.
1511 PATTERN is case-insensitive.
1513 The return value is a list of strings, suitable as arguments to
1514 `set-face-font'.
1516 Fonts Emacs can't use may or may not be excluded
1517 even if they match PATTERN and FACE.
1518 The optional fourth argument MAXIMUM sets a limit on how many
1519 fonts to match. The first MAXIMUM fonts are reported.
1520 The optional fifth argument WIDTH, if specified, is a number of columns
1521 occupied by a character of a font. In that case, return only fonts
1522 the WIDTH times as wide as FACE on FRAME. */)
1523 (Lisp_Object pattern, Lisp_Object face, Lisp_Object frame,
1524 Lisp_Object maximum, Lisp_Object width)
1526 struct frame *f;
1527 int size, avgwidth IF_LINT (= 0);
1529 check_window_system (NULL);
1530 CHECK_STRING (pattern);
1532 if (! NILP (maximum))
1533 CHECK_NATNUM (maximum);
1535 if (!NILP (width))
1536 CHECK_NUMBER (width);
1538 /* We can't simply call decode_window_system_frame because
1539 this function may be called before any frame is created. */
1540 f = decode_live_frame (frame);
1541 if (! FRAME_WINDOW_P (f))
1543 /* Perhaps we have not yet created any frame. */
1544 f = NULL;
1545 frame = Qnil;
1546 face = Qnil;
1548 else
1549 XSETFRAME (frame, f);
1551 /* Determine the width standard for comparison with the fonts we find. */
1553 if (NILP (face))
1554 size = 0;
1555 else
1557 /* This is of limited utility since it works with character
1558 widths. Keep it for compatibility. --gerd. */
1559 int face_id = lookup_named_face (f, face, false);
1560 struct face *width_face = (face_id < 0
1561 ? NULL
1562 : FACE_FROM_ID (f, face_id));
1564 if (width_face && width_face->font)
1566 size = width_face->font->pixel_size;
1567 avgwidth = width_face->font->average_width;
1569 else
1571 size = FRAME_FONT (f)->pixel_size;
1572 avgwidth = FRAME_FONT (f)->average_width;
1574 if (!NILP (width))
1575 avgwidth *= XINT (width);
1578 Lisp_Object font_spec = font_spec_from_name (pattern);
1579 if (!FONTP (font_spec))
1580 signal_error ("Invalid font name", pattern);
1582 if (size)
1584 Ffont_put (font_spec, QCsize, make_number (size));
1585 Ffont_put (font_spec, QCavgwidth, make_number (avgwidth));
1587 Lisp_Object fonts = Flist_fonts (font_spec, frame, maximum, font_spec);
1588 for (Lisp_Object tail = fonts; CONSP (tail); tail = XCDR (tail))
1590 Lisp_Object font_entity;
1592 font_entity = XCAR (tail);
1593 if ((NILP (AREF (font_entity, FONT_SIZE_INDEX))
1594 || XINT (AREF (font_entity, FONT_SIZE_INDEX)) == 0)
1595 && ! NILP (AREF (font_spec, FONT_SIZE_INDEX)))
1597 /* This is a scalable font. For backward compatibility,
1598 we set the specified size. */
1599 font_entity = copy_font_spec (font_entity);
1600 ASET (font_entity, FONT_SIZE_INDEX,
1601 AREF (font_spec, FONT_SIZE_INDEX));
1603 XSETCAR (tail, Ffont_xlfd_name (font_entity, Qnil));
1605 if (NILP (frame))
1606 /* We don't have to check fontsets. */
1607 return fonts;
1608 Lisp_Object fontsets = list_fontsets (f, pattern, size);
1609 return CALLN (Fnconc, fonts, fontsets);
1612 #endif /* HAVE_WINDOW_SYSTEM */
1615 /***********************************************************************
1616 Lisp Faces
1617 ***********************************************************************/
1619 /* Access face attributes of face LFACE, a Lisp vector. */
1621 #define LFACE_FAMILY(LFACE) AREF ((LFACE), LFACE_FAMILY_INDEX)
1622 #define LFACE_FOUNDRY(LFACE) AREF ((LFACE), LFACE_FOUNDRY_INDEX)
1623 #define LFACE_HEIGHT(LFACE) AREF ((LFACE), LFACE_HEIGHT_INDEX)
1624 #define LFACE_WEIGHT(LFACE) AREF ((LFACE), LFACE_WEIGHT_INDEX)
1625 #define LFACE_SLANT(LFACE) AREF ((LFACE), LFACE_SLANT_INDEX)
1626 #define LFACE_UNDERLINE(LFACE) AREF ((LFACE), LFACE_UNDERLINE_INDEX)
1627 #define LFACE_INVERSE(LFACE) AREF ((LFACE), LFACE_INVERSE_INDEX)
1628 #define LFACE_FOREGROUND(LFACE) AREF ((LFACE), LFACE_FOREGROUND_INDEX)
1629 #define LFACE_BACKGROUND(LFACE) AREF ((LFACE), LFACE_BACKGROUND_INDEX)
1630 #define LFACE_STIPPLE(LFACE) AREF ((LFACE), LFACE_STIPPLE_INDEX)
1631 #define LFACE_SWIDTH(LFACE) AREF ((LFACE), LFACE_SWIDTH_INDEX)
1632 #define LFACE_OVERLINE(LFACE) AREF ((LFACE), LFACE_OVERLINE_INDEX)
1633 #define LFACE_STRIKE_THROUGH(LFACE) AREF ((LFACE), LFACE_STRIKE_THROUGH_INDEX)
1634 #define LFACE_BOX(LFACE) AREF ((LFACE), LFACE_BOX_INDEX)
1635 #define LFACE_FONT(LFACE) AREF ((LFACE), LFACE_FONT_INDEX)
1636 #define LFACE_INHERIT(LFACE) AREF ((LFACE), LFACE_INHERIT_INDEX)
1637 #define LFACE_FONTSET(LFACE) AREF ((LFACE), LFACE_FONTSET_INDEX)
1638 #define LFACE_DISTANT_FOREGROUND(LFACE) \
1639 AREF ((LFACE), LFACE_DISTANT_FOREGROUND_INDEX)
1641 /* True if LFACE is a Lisp face. A Lisp face is a vector of size
1642 LFACE_VECTOR_SIZE which has the symbol `face' in slot 0. */
1644 #define LFACEP(LFACE) \
1645 (VECTORP (LFACE) \
1646 && ASIZE (LFACE) == LFACE_VECTOR_SIZE \
1647 && EQ (AREF (LFACE, 0), Qface))
1650 #ifdef GLYPH_DEBUG
1652 /* Check consistency of Lisp face attribute vector ATTRS. */
1654 static void
1655 check_lface_attrs (Lisp_Object attrs[LFACE_VECTOR_SIZE])
1657 eassert (UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
1658 || IGNORE_DEFFACE_P (attrs[LFACE_FAMILY_INDEX])
1659 || STRINGP (attrs[LFACE_FAMILY_INDEX]));
1660 eassert (UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
1661 || IGNORE_DEFFACE_P (attrs[LFACE_FOUNDRY_INDEX])
1662 || STRINGP (attrs[LFACE_FOUNDRY_INDEX]));
1663 eassert (UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
1664 || IGNORE_DEFFACE_P (attrs[LFACE_SWIDTH_INDEX])
1665 || SYMBOLP (attrs[LFACE_SWIDTH_INDEX]));
1666 eassert (UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
1667 || IGNORE_DEFFACE_P (attrs[LFACE_HEIGHT_INDEX])
1668 || NUMBERP (attrs[LFACE_HEIGHT_INDEX])
1669 || FUNCTIONP (attrs[LFACE_HEIGHT_INDEX]));
1670 eassert (UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
1671 || IGNORE_DEFFACE_P (attrs[LFACE_WEIGHT_INDEX])
1672 || SYMBOLP (attrs[LFACE_WEIGHT_INDEX]));
1673 eassert (UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
1674 || IGNORE_DEFFACE_P (attrs[LFACE_SLANT_INDEX])
1675 || SYMBOLP (attrs[LFACE_SLANT_INDEX]));
1676 eassert (UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
1677 || IGNORE_DEFFACE_P (attrs[LFACE_UNDERLINE_INDEX])
1678 || SYMBOLP (attrs[LFACE_UNDERLINE_INDEX])
1679 || STRINGP (attrs[LFACE_UNDERLINE_INDEX])
1680 || CONSP (attrs[LFACE_UNDERLINE_INDEX]));
1681 eassert (UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
1682 || IGNORE_DEFFACE_P (attrs[LFACE_OVERLINE_INDEX])
1683 || SYMBOLP (attrs[LFACE_OVERLINE_INDEX])
1684 || STRINGP (attrs[LFACE_OVERLINE_INDEX]));
1685 eassert (UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1686 || IGNORE_DEFFACE_P (attrs[LFACE_STRIKE_THROUGH_INDEX])
1687 || SYMBOLP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1688 || STRINGP (attrs[LFACE_STRIKE_THROUGH_INDEX]));
1689 eassert (UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
1690 || IGNORE_DEFFACE_P (attrs[LFACE_BOX_INDEX])
1691 || SYMBOLP (attrs[LFACE_BOX_INDEX])
1692 || STRINGP (attrs[LFACE_BOX_INDEX])
1693 || INTEGERP (attrs[LFACE_BOX_INDEX])
1694 || CONSP (attrs[LFACE_BOX_INDEX]));
1695 eassert (UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
1696 || IGNORE_DEFFACE_P (attrs[LFACE_INVERSE_INDEX])
1697 || SYMBOLP (attrs[LFACE_INVERSE_INDEX]));
1698 eassert (UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
1699 || IGNORE_DEFFACE_P (attrs[LFACE_FOREGROUND_INDEX])
1700 || STRINGP (attrs[LFACE_FOREGROUND_INDEX]));
1701 eassert (UNSPECIFIEDP (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
1702 || IGNORE_DEFFACE_P (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
1703 || STRINGP (attrs[LFACE_DISTANT_FOREGROUND_INDEX]));
1704 eassert (UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
1705 || IGNORE_DEFFACE_P (attrs[LFACE_BACKGROUND_INDEX])
1706 || STRINGP (attrs[LFACE_BACKGROUND_INDEX]));
1707 eassert (UNSPECIFIEDP (attrs[LFACE_INHERIT_INDEX])
1708 || IGNORE_DEFFACE_P (attrs[LFACE_INHERIT_INDEX])
1709 || NILP (attrs[LFACE_INHERIT_INDEX])
1710 || SYMBOLP (attrs[LFACE_INHERIT_INDEX])
1711 || CONSP (attrs[LFACE_INHERIT_INDEX]));
1712 #ifdef HAVE_WINDOW_SYSTEM
1713 eassert (UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
1714 || IGNORE_DEFFACE_P (attrs[LFACE_STIPPLE_INDEX])
1715 || SYMBOLP (attrs[LFACE_STIPPLE_INDEX])
1716 || !NILP (Fbitmap_spec_p (attrs[LFACE_STIPPLE_INDEX])));
1717 eassert (UNSPECIFIEDP (attrs[LFACE_FONT_INDEX])
1718 || IGNORE_DEFFACE_P (attrs[LFACE_FONT_INDEX])
1719 || FONTP (attrs[LFACE_FONT_INDEX]));
1720 eassert (UNSPECIFIEDP (attrs[LFACE_FONTSET_INDEX])
1721 || STRINGP (attrs[LFACE_FONTSET_INDEX])
1722 || NILP (attrs[LFACE_FONTSET_INDEX]));
1723 #endif
1727 /* Check consistency of attributes of Lisp face LFACE (a Lisp vector). */
1729 static void
1730 check_lface (Lisp_Object lface)
1732 if (!NILP (lface))
1734 eassert (LFACEP (lface));
1735 check_lface_attrs (XVECTOR (lface)->contents);
1739 #else /* not GLYPH_DEBUG */
1741 #define check_lface_attrs(attrs) (void) 0
1742 #define check_lface(lface) (void) 0
1744 #endif /* GLYPH_DEBUG */
1748 /* Face-merge cycle checking. */
1750 enum named_merge_point_kind
1752 NAMED_MERGE_POINT_NORMAL,
1753 NAMED_MERGE_POINT_REMAP
1756 /* A `named merge point' is simply a point during face-merging where we
1757 look up a face by name. We keep a stack of which named lookups we're
1758 currently processing so that we can easily detect cycles, using a
1759 linked- list of struct named_merge_point structures, typically
1760 allocated on the stack frame of the named lookup functions which are
1761 active (so no consing is required). */
1762 struct named_merge_point
1764 Lisp_Object face_name;
1765 enum named_merge_point_kind named_merge_point_kind;
1766 struct named_merge_point *prev;
1770 /* If a face merging cycle is detected for FACE_NAME, return false,
1771 otherwise add NEW_NAMED_MERGE_POINT, which is initialized using
1772 FACE_NAME and NAMED_MERGE_POINT_KIND, as the head of the linked list
1773 pointed to by NAMED_MERGE_POINTS, and return true. */
1775 static bool
1776 push_named_merge_point (struct named_merge_point *new_named_merge_point,
1777 Lisp_Object face_name,
1778 enum named_merge_point_kind named_merge_point_kind,
1779 struct named_merge_point **named_merge_points)
1781 struct named_merge_point *prev;
1783 for (prev = *named_merge_points; prev; prev = prev->prev)
1784 if (EQ (face_name, prev->face_name))
1786 if (prev->named_merge_point_kind == named_merge_point_kind)
1787 /* A cycle, so fail. */
1788 return false;
1789 else if (prev->named_merge_point_kind == NAMED_MERGE_POINT_REMAP)
1790 /* A remap `hides ' any previous normal merge points
1791 (because the remap means that it's actually different face),
1792 so as we know the current merge point must be normal, we
1793 can just assume it's OK. */
1794 break;
1797 new_named_merge_point->face_name = face_name;
1798 new_named_merge_point->named_merge_point_kind = named_merge_point_kind;
1799 new_named_merge_point->prev = *named_merge_points;
1801 *named_merge_points = new_named_merge_point;
1803 return true;
1807 /* Resolve face name FACE_NAME. If FACE_NAME is a string, intern it
1808 to make it a symbol. If FACE_NAME is an alias for another face,
1809 return that face's name.
1811 Return default face in case of errors. */
1813 static Lisp_Object
1814 resolve_face_name (Lisp_Object face_name, bool signal_p)
1816 Lisp_Object orig_face;
1817 Lisp_Object tortoise, hare;
1819 if (STRINGP (face_name))
1820 face_name = Fintern (face_name, Qnil);
1822 if (NILP (face_name) || !SYMBOLP (face_name))
1823 return face_name;
1825 orig_face = face_name;
1826 tortoise = hare = face_name;
1828 while (true)
1830 face_name = hare;
1831 hare = Fget (hare, Qface_alias);
1832 if (NILP (hare) || !SYMBOLP (hare))
1833 break;
1835 face_name = hare;
1836 hare = Fget (hare, Qface_alias);
1837 if (NILP (hare) || !SYMBOLP (hare))
1838 break;
1840 tortoise = Fget (tortoise, Qface_alias);
1841 if (EQ (hare, tortoise))
1843 if (signal_p)
1844 xsignal1 (Qcircular_list, orig_face);
1845 return Qdefault;
1849 return face_name;
1853 /* Return the face definition of FACE_NAME on frame F. F null means
1854 return the definition for new frames. FACE_NAME may be a string or
1855 a symbol (apparently Emacs 20.2 allowed strings as face names in
1856 face text properties; Ediff uses that).
1857 If SIGNAL_P, signal an error if FACE_NAME is not a valid face name.
1858 Otherwise, value is nil if FACE_NAME is not a valid face name. */
1859 static Lisp_Object
1860 lface_from_face_name_no_resolve (struct frame *f, Lisp_Object face_name,
1861 bool signal_p)
1863 Lisp_Object lface;
1865 if (f)
1866 lface = assq_no_quit (face_name, f->face_alist);
1867 else
1868 lface = assq_no_quit (face_name, Vface_new_frame_defaults);
1870 if (CONSP (lface))
1871 lface = XCDR (lface);
1872 else if (signal_p)
1873 signal_error ("Invalid face", face_name);
1875 check_lface (lface);
1877 return lface;
1880 /* Return the face definition of FACE_NAME on frame F. F null means
1881 return the definition for new frames. FACE_NAME may be a string or
1882 a symbol (apparently Emacs 20.2 allowed strings as face names in
1883 face text properties; Ediff uses that). If FACE_NAME is an alias
1884 for another face, return that face's definition.
1885 If SIGNAL_P, signal an error if FACE_NAME is not a valid face name.
1886 Otherwise, value is nil if FACE_NAME is not a valid face name. */
1887 static Lisp_Object
1888 lface_from_face_name (struct frame *f, Lisp_Object face_name, bool signal_p)
1890 face_name = resolve_face_name (face_name, signal_p);
1891 return lface_from_face_name_no_resolve (f, face_name, signal_p);
1895 /* Get face attributes of face FACE_NAME from frame-local faces on
1896 frame F. Store the resulting attributes in ATTRS which must point
1897 to a vector of Lisp_Objects of size LFACE_VECTOR_SIZE.
1898 If SIGNAL_P, signal an error if FACE_NAME does not name a face.
1899 Otherwise, return true iff FACE_NAME is a face. */
1901 static bool
1902 get_lface_attributes_no_remap (struct frame *f, Lisp_Object face_name,
1903 Lisp_Object attrs[LFACE_VECTOR_SIZE],
1904 bool signal_p)
1906 Lisp_Object lface;
1908 lface = lface_from_face_name_no_resolve (f, face_name, signal_p);
1910 if (! NILP (lface))
1911 memcpy (attrs, XVECTOR (lface)->contents,
1912 LFACE_VECTOR_SIZE * sizeof *attrs);
1914 return !NILP (lface);
1917 /* Get face attributes of face FACE_NAME from frame-local faces on frame
1918 F. Store the resulting attributes in ATTRS which must point to a
1919 vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If FACE_NAME is an
1920 alias for another face, use that face's definition.
1921 If SIGNAL_P, signal an error if FACE_NAME does not name a face.
1922 Otherwise, return true iff FACE_NAME is a face. */
1924 static bool
1925 get_lface_attributes (struct frame *f, Lisp_Object face_name,
1926 Lisp_Object attrs[LFACE_VECTOR_SIZE], bool signal_p,
1927 struct named_merge_point *named_merge_points)
1929 Lisp_Object face_remapping;
1931 face_name = resolve_face_name (face_name, signal_p);
1933 /* See if SYMBOL has been remapped to some other face (usually this
1934 is done buffer-locally). */
1935 face_remapping = assq_no_quit (face_name, Vface_remapping_alist);
1936 if (CONSP (face_remapping))
1938 struct named_merge_point named_merge_point;
1940 if (push_named_merge_point (&named_merge_point,
1941 face_name, NAMED_MERGE_POINT_REMAP,
1942 &named_merge_points))
1944 int i;
1946 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
1947 attrs[i] = Qunspecified;
1949 return merge_face_ref (f, XCDR (face_remapping), attrs,
1950 signal_p, named_merge_points);
1954 /* Default case, no remapping. */
1955 return get_lface_attributes_no_remap (f, face_name, attrs, signal_p);
1959 /* True iff all attributes in face attribute vector ATTRS are
1960 specified, i.e. are non-nil. */
1962 static bool
1963 lface_fully_specified_p (Lisp_Object attrs[LFACE_VECTOR_SIZE])
1965 int i;
1967 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
1968 if (i != LFACE_FONT_INDEX && i != LFACE_INHERIT_INDEX
1969 && i != LFACE_DISTANT_FOREGROUND_INDEX)
1970 if ((UNSPECIFIEDP (attrs[i]) || IGNORE_DEFFACE_P (attrs[i])))
1971 break;
1973 return i == LFACE_VECTOR_SIZE;
1976 #ifdef HAVE_WINDOW_SYSTEM
1978 /* Set font-related attributes of Lisp face LFACE from FONT-OBJECT.
1979 If FORCE_P, set only unspecified attributes of LFACE. The
1980 exception is `font' attribute. It is set to FONT_OBJECT regardless
1981 of FORCE_P. */
1983 static void
1984 set_lface_from_font (struct frame *f, Lisp_Object lface,
1985 Lisp_Object font_object, bool force_p)
1987 Lisp_Object val;
1988 struct font *font = XFONT_OBJECT (font_object);
1990 /* Set attributes only if unspecified, otherwise face defaults for
1991 new frames would never take effect. If the font doesn't have a
1992 specific property, set a normal value for that. */
1994 if (force_p || UNSPECIFIEDP (LFACE_FAMILY (lface)))
1996 Lisp_Object family = AREF (font_object, FONT_FAMILY_INDEX);
1998 ASET (lface, LFACE_FAMILY_INDEX, SYMBOL_NAME (family));
2001 if (force_p || UNSPECIFIEDP (LFACE_FOUNDRY (lface)))
2003 Lisp_Object foundry = AREF (font_object, FONT_FOUNDRY_INDEX);
2005 ASET (lface, LFACE_FOUNDRY_INDEX, SYMBOL_NAME (foundry));
2008 if (force_p || UNSPECIFIEDP (LFACE_HEIGHT (lface)))
2010 int pt = PIXEL_TO_POINT (font->pixel_size * 10, FRAME_RES_Y (f));
2012 eassert (pt > 0);
2013 ASET (lface, LFACE_HEIGHT_INDEX, make_number (pt));
2016 if (force_p || UNSPECIFIEDP (LFACE_WEIGHT (lface)))
2018 val = FONT_WEIGHT_FOR_FACE (font_object);
2019 ASET (lface, LFACE_WEIGHT_INDEX, ! NILP (val) ? val :Qnormal);
2021 if (force_p || UNSPECIFIEDP (LFACE_SLANT (lface)))
2023 val = FONT_SLANT_FOR_FACE (font_object);
2024 ASET (lface, LFACE_SLANT_INDEX, ! NILP (val) ? val : Qnormal);
2026 if (force_p || UNSPECIFIEDP (LFACE_SWIDTH (lface)))
2028 val = FONT_WIDTH_FOR_FACE (font_object);
2029 ASET (lface, LFACE_SWIDTH_INDEX, ! NILP (val) ? val : Qnormal);
2032 ASET (lface, LFACE_FONT_INDEX, font_object);
2035 #endif /* HAVE_WINDOW_SYSTEM */
2038 /* Merges the face height FROM with the face height TO, and returns the
2039 merged height. If FROM is an invalid height, then INVALID is
2040 returned instead. FROM and TO may be either absolute face heights or
2041 `relative' heights; the returned value is always an absolute height
2042 unless both FROM and TO are relative. */
2044 static Lisp_Object
2045 merge_face_heights (Lisp_Object from, Lisp_Object to, Lisp_Object invalid)
2047 Lisp_Object result = invalid;
2049 if (INTEGERP (from))
2050 /* FROM is absolute, just use it as is. */
2051 result = from;
2052 else if (FLOATP (from))
2053 /* FROM is a scale, use it to adjust TO. */
2055 if (INTEGERP (to))
2056 /* relative X absolute => absolute */
2057 result = make_number (XFLOAT_DATA (from) * XINT (to));
2058 else if (FLOATP (to))
2059 /* relative X relative => relative */
2060 result = make_float (XFLOAT_DATA (from) * XFLOAT_DATA (to));
2061 else if (UNSPECIFIEDP (to))
2062 result = from;
2064 else if (FUNCTIONP (from))
2065 /* FROM is a function, which use to adjust TO. */
2067 /* Call function with current height as argument.
2068 From is the new height. */
2069 result = safe_call1 (from, to);
2071 /* Ensure that if TO was absolute, so is the result. */
2072 if (INTEGERP (to) && !INTEGERP (result))
2073 result = invalid;
2076 return result;
2080 /* Merge two Lisp face attribute vectors on frame F, FROM and TO, and
2081 store the resulting attributes in TO, which must be already be
2082 completely specified and contain only absolute attributes. Every
2083 specified attribute of FROM overrides the corresponding attribute of
2084 TO; relative attributes in FROM are merged with the absolute value in
2085 TO and replace it. NAMED_MERGE_POINTS is used internally to detect
2086 loops in face inheritance/remapping; it should be 0 when called from
2087 other places. */
2089 static void
2090 merge_face_vectors (struct frame *f, Lisp_Object *from, Lisp_Object *to,
2091 struct named_merge_point *named_merge_points)
2093 int i;
2094 Lisp_Object font = Qnil;
2096 /* If FROM inherits from some other faces, merge their attributes into
2097 TO before merging FROM's direct attributes. Note that an :inherit
2098 attribute of `unspecified' is the same as one of nil; we never
2099 merge :inherit attributes, so nil is more correct, but lots of
2100 other code uses `unspecified' as a generic value for face attributes. */
2101 if (!UNSPECIFIEDP (from[LFACE_INHERIT_INDEX])
2102 && !NILP (from[LFACE_INHERIT_INDEX]))
2103 merge_face_ref (f, from[LFACE_INHERIT_INDEX], to, false, named_merge_points);
2105 if (FONT_SPEC_P (from[LFACE_FONT_INDEX]))
2107 if (!UNSPECIFIEDP (to[LFACE_FONT_INDEX]))
2108 font = merge_font_spec (from[LFACE_FONT_INDEX], to[LFACE_FONT_INDEX]);
2109 else
2110 font = copy_font_spec (from[LFACE_FONT_INDEX]);
2111 to[LFACE_FONT_INDEX] = font;
2114 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2115 if (!UNSPECIFIEDP (from[i]))
2117 if (i == LFACE_HEIGHT_INDEX && !INTEGERP (from[i]))
2119 to[i] = merge_face_heights (from[i], to[i], to[i]);
2120 font_clear_prop (to, FONT_SIZE_INDEX);
2122 else if (i != LFACE_FONT_INDEX && ! EQ (to[i], from[i]))
2124 to[i] = from[i];
2125 if (i >= LFACE_FAMILY_INDEX && i <=LFACE_SLANT_INDEX)
2126 font_clear_prop (to,
2127 (i == LFACE_FAMILY_INDEX ? FONT_FAMILY_INDEX
2128 : i == LFACE_FOUNDRY_INDEX ? FONT_FOUNDRY_INDEX
2129 : i == LFACE_SWIDTH_INDEX ? FONT_WIDTH_INDEX
2130 : i == LFACE_HEIGHT_INDEX ? FONT_SIZE_INDEX
2131 : i == LFACE_WEIGHT_INDEX ? FONT_WEIGHT_INDEX
2132 : FONT_SLANT_INDEX));
2136 /* If FROM specifies a font spec, make its contents take precedence
2137 over :family and other attributes. This is needed for face
2138 remapping using :font to work. */
2140 if (!NILP (font))
2142 if (! NILP (AREF (font, FONT_FOUNDRY_INDEX)))
2143 to[LFACE_FOUNDRY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FOUNDRY_INDEX));
2144 if (! NILP (AREF (font, FONT_FAMILY_INDEX)))
2145 to[LFACE_FAMILY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FAMILY_INDEX));
2146 if (! NILP (AREF (font, FONT_WEIGHT_INDEX)))
2147 to[LFACE_WEIGHT_INDEX] = FONT_WEIGHT_FOR_FACE (font);
2148 if (! NILP (AREF (font, FONT_SLANT_INDEX)))
2149 to[LFACE_SLANT_INDEX] = FONT_SLANT_FOR_FACE (font);
2150 if (! NILP (AREF (font, FONT_WIDTH_INDEX)))
2151 to[LFACE_SWIDTH_INDEX] = FONT_WIDTH_FOR_FACE (font);
2152 ASET (font, FONT_SIZE_INDEX, Qnil);
2155 /* TO is always an absolute face, which should inherit from nothing.
2156 We blindly copy the :inherit attribute above and fix it up here. */
2157 to[LFACE_INHERIT_INDEX] = Qnil;
2160 /* Merge the named face FACE_NAME on frame F, into the vector of face
2161 attributes TO. Use NAMED_MERGE_POINTS to detect loops in face
2162 inheritance. Return true if FACE_NAME is a valid face name and
2163 merging succeeded. */
2165 static bool
2166 merge_named_face (struct frame *f, Lisp_Object face_name, Lisp_Object *to,
2167 struct named_merge_point *named_merge_points)
2169 struct named_merge_point named_merge_point;
2171 if (push_named_merge_point (&named_merge_point,
2172 face_name, NAMED_MERGE_POINT_NORMAL,
2173 &named_merge_points))
2175 Lisp_Object from[LFACE_VECTOR_SIZE];
2176 bool ok = get_lface_attributes (f, face_name, from, false,
2177 named_merge_points);
2179 if (ok)
2180 merge_face_vectors (f, from, to, named_merge_points);
2182 return ok;
2184 else
2185 return false;
2189 /* Merge face attributes from the lisp `face reference' FACE_REF on
2190 frame F into the face attribute vector TO. If ERR_MSGS,
2191 problems with FACE_REF cause an error message to be shown. Return
2192 true if no errors occurred (regardless of the value of ERR_MSGS).
2193 Use NAMED_MERGE_POINTS to detect loops in face inheritance or
2194 list structure; it may be 0 for most callers.
2196 FACE_REF may be a single face specification or a list of such
2197 specifications. Each face specification can be:
2199 1. A symbol or string naming a Lisp face.
2201 2. A property list of the form (KEYWORD VALUE ...) where each
2202 KEYWORD is a face attribute name, and value is an appropriate value
2203 for that attribute.
2205 3. Conses or the form (FOREGROUND-COLOR . COLOR) or
2206 (BACKGROUND-COLOR . COLOR) where COLOR is a color name. This is
2207 for compatibility with 20.2.
2209 Face specifications earlier in lists take precedence over later
2210 specifications. */
2212 static bool
2213 merge_face_ref (struct frame *f, Lisp_Object face_ref, Lisp_Object *to,
2214 bool err_msgs, struct named_merge_point *named_merge_points)
2216 bool ok = true; /* Succeed without an error? */
2218 if (CONSP (face_ref))
2220 Lisp_Object first = XCAR (face_ref);
2222 if (EQ (first, Qforeground_color)
2223 || EQ (first, Qbackground_color))
2225 /* One of (FOREGROUND-COLOR . COLOR) or (BACKGROUND-COLOR
2226 . COLOR). COLOR must be a string. */
2227 Lisp_Object color_name = XCDR (face_ref);
2228 Lisp_Object color = first;
2230 if (STRINGP (color_name))
2232 if (EQ (color, Qforeground_color))
2233 to[LFACE_FOREGROUND_INDEX] = color_name;
2234 else
2235 to[LFACE_BACKGROUND_INDEX] = color_name;
2237 else
2239 if (err_msgs)
2240 add_to_log ("Invalid face color %S", color_name);
2241 ok = false;
2244 else if (SYMBOLP (first)
2245 && *SDATA (SYMBOL_NAME (first)) == ':')
2247 /* Assume this is the property list form. */
2248 while (CONSP (face_ref) && CONSP (XCDR (face_ref)))
2250 Lisp_Object keyword = XCAR (face_ref);
2251 Lisp_Object value = XCAR (XCDR (face_ref));
2252 bool err = false;
2254 /* Specifying `unspecified' is a no-op. */
2255 if (EQ (value, Qunspecified))
2257 else if (EQ (keyword, QCfamily))
2259 if (STRINGP (value))
2261 to[LFACE_FAMILY_INDEX] = value;
2262 font_clear_prop (to, FONT_FAMILY_INDEX);
2264 else
2265 err = true;
2267 else if (EQ (keyword, QCfoundry))
2269 if (STRINGP (value))
2271 to[LFACE_FOUNDRY_INDEX] = value;
2272 font_clear_prop (to, FONT_FOUNDRY_INDEX);
2274 else
2275 err = true;
2277 else if (EQ (keyword, QCheight))
2279 Lisp_Object new_height =
2280 merge_face_heights (value, to[LFACE_HEIGHT_INDEX], Qnil);
2282 if (! NILP (new_height))
2284 to[LFACE_HEIGHT_INDEX] = new_height;
2285 font_clear_prop (to, FONT_SIZE_INDEX);
2287 else
2288 err = true;
2290 else if (EQ (keyword, QCweight))
2292 if (SYMBOLP (value) && FONT_WEIGHT_NAME_NUMERIC (value) >= 0)
2294 to[LFACE_WEIGHT_INDEX] = value;
2295 font_clear_prop (to, FONT_WEIGHT_INDEX);
2297 else
2298 err = true;
2300 else if (EQ (keyword, QCslant))
2302 if (SYMBOLP (value) && FONT_SLANT_NAME_NUMERIC (value) >= 0)
2304 to[LFACE_SLANT_INDEX] = value;
2305 font_clear_prop (to, FONT_SLANT_INDEX);
2307 else
2308 err = true;
2310 else if (EQ (keyword, QCunderline))
2312 if (EQ (value, Qt)
2313 || NILP (value)
2314 || STRINGP (value)
2315 || CONSP (value))
2316 to[LFACE_UNDERLINE_INDEX] = value;
2317 else
2318 err = true;
2320 else if (EQ (keyword, QCoverline))
2322 if (EQ (value, Qt)
2323 || NILP (value)
2324 || STRINGP (value))
2325 to[LFACE_OVERLINE_INDEX] = value;
2326 else
2327 err = true;
2329 else if (EQ (keyword, QCstrike_through))
2331 if (EQ (value, Qt)
2332 || NILP (value)
2333 || STRINGP (value))
2334 to[LFACE_STRIKE_THROUGH_INDEX] = value;
2335 else
2336 err = true;
2338 else if (EQ (keyword, QCbox))
2340 if (EQ (value, Qt))
2341 value = make_number (1);
2342 if (INTEGERP (value)
2343 || STRINGP (value)
2344 || CONSP (value)
2345 || NILP (value))
2346 to[LFACE_BOX_INDEX] = value;
2347 else
2348 err = true;
2350 else if (EQ (keyword, QCinverse_video)
2351 || EQ (keyword, QCreverse_video))
2353 if (EQ (value, Qt) || NILP (value))
2354 to[LFACE_INVERSE_INDEX] = value;
2355 else
2356 err = true;
2358 else if (EQ (keyword, QCforeground))
2360 if (STRINGP (value))
2361 to[LFACE_FOREGROUND_INDEX] = value;
2362 else
2363 err = true;
2365 else if (EQ (keyword, QCdistant_foreground))
2367 if (STRINGP (value))
2368 to[LFACE_DISTANT_FOREGROUND_INDEX] = value;
2369 else
2370 err = true;
2372 else if (EQ (keyword, QCbackground))
2374 if (STRINGP (value))
2375 to[LFACE_BACKGROUND_INDEX] = value;
2376 else
2377 err = true;
2379 else if (EQ (keyword, QCstipple))
2381 #if defined (HAVE_WINDOW_SYSTEM)
2382 Lisp_Object pixmap_p = Fbitmap_spec_p (value);
2383 if (!NILP (pixmap_p))
2384 to[LFACE_STIPPLE_INDEX] = value;
2385 else
2386 err = true;
2387 #endif /* HAVE_WINDOW_SYSTEM */
2389 else if (EQ (keyword, QCwidth))
2391 if (SYMBOLP (value) && FONT_WIDTH_NAME_NUMERIC (value) >= 0)
2393 to[LFACE_SWIDTH_INDEX] = value;
2394 font_clear_prop (to, FONT_WIDTH_INDEX);
2396 else
2397 err = true;
2399 else if (EQ (keyword, QCfont))
2401 if (FONTP (value))
2402 to[LFACE_FONT_INDEX] = value;
2403 else
2404 err = true;
2406 else if (EQ (keyword, QCinherit))
2408 /* This is not really very useful; it's just like a
2409 normal face reference. */
2410 if (! merge_face_ref (f, value, to,
2411 err_msgs, named_merge_points))
2412 err = true;
2414 else
2415 err = true;
2417 if (err)
2419 add_to_log ("Invalid face attribute %S %S", keyword, value);
2420 ok = false;
2423 face_ref = XCDR (XCDR (face_ref));
2426 else
2428 /* This is a list of face refs. Those at the beginning of the
2429 list take precedence over what follows, so we have to merge
2430 from the end backwards. */
2431 Lisp_Object next = XCDR (face_ref);
2433 if (! NILP (next))
2434 ok = merge_face_ref (f, next, to, err_msgs, named_merge_points);
2436 if (! merge_face_ref (f, first, to, err_msgs, named_merge_points))
2437 ok = false;
2440 else
2442 /* FACE_REF ought to be a face name. */
2443 ok = merge_named_face (f, face_ref, to, named_merge_points);
2444 if (!ok && err_msgs)
2445 add_to_log ("Invalid face reference: %s", face_ref);
2448 return ok;
2452 DEFUN ("internal-make-lisp-face", Finternal_make_lisp_face,
2453 Sinternal_make_lisp_face, 1, 2, 0,
2454 doc: /* Make FACE, a symbol, a Lisp face with all attributes nil.
2455 If FACE was not known as a face before, create a new one.
2456 If optional argument FRAME is specified, make a frame-local face
2457 for that frame. Otherwise operate on the global face definition.
2458 Value is a vector of face attributes. */)
2459 (Lisp_Object face, Lisp_Object frame)
2461 Lisp_Object global_lface, lface;
2462 struct frame *f;
2463 int i;
2465 CHECK_SYMBOL (face);
2466 global_lface = lface_from_face_name (NULL, face, false);
2468 if (!NILP (frame))
2470 CHECK_LIVE_FRAME (frame);
2471 f = XFRAME (frame);
2472 lface = lface_from_face_name (f, face, false);
2474 else
2475 f = NULL, lface = Qnil;
2477 /* Add a global definition if there is none. */
2478 if (NILP (global_lface))
2480 global_lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2481 Qunspecified);
2482 ASET (global_lface, 0, Qface);
2483 Vface_new_frame_defaults = Fcons (Fcons (face, global_lface),
2484 Vface_new_frame_defaults);
2486 /* Assign the new Lisp face a unique ID. The mapping from Lisp
2487 face id to Lisp face is given by the vector lface_id_to_name.
2488 The mapping from Lisp face to Lisp face id is given by the
2489 property `face' of the Lisp face name. */
2490 if (next_lface_id == lface_id_to_name_size)
2491 lface_id_to_name =
2492 xpalloc (lface_id_to_name, &lface_id_to_name_size, 1, MAX_FACE_ID,
2493 sizeof *lface_id_to_name);
2495 lface_id_to_name[next_lface_id] = face;
2496 Fput (face, Qface, make_number (next_lface_id));
2497 ++next_lface_id;
2499 else if (f == NULL)
2500 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2501 ASET (global_lface, i, Qunspecified);
2503 /* Add a frame-local definition. */
2504 if (f)
2506 if (NILP (lface))
2508 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2509 Qunspecified);
2510 ASET (lface, 0, Qface);
2511 fset_face_alist (f, Fcons (Fcons (face, lface), f->face_alist));
2513 else
2514 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2515 ASET (lface, i, Qunspecified);
2517 else
2518 lface = global_lface;
2520 /* Changing a named face means that all realized faces depending on
2521 that face are invalid. Since we cannot tell which realized faces
2522 depend on the face, make sure they are all removed. This is done
2523 by setting face_change. The next call to init_iterator will then
2524 free realized faces. */
2525 if (NILP (Fget (face, Qface_no_inherit)))
2527 if (f)
2529 f->face_change = true;
2530 fset_redisplay (f);
2532 else
2534 face_change = true;
2535 windows_or_buffers_changed = 54;
2539 eassert (LFACEP (lface));
2540 check_lface (lface);
2541 return lface;
2545 DEFUN ("internal-lisp-face-p", Finternal_lisp_face_p,
2546 Sinternal_lisp_face_p, 1, 2, 0,
2547 doc: /* Return non-nil if FACE names a face.
2548 FACE should be a symbol or string.
2549 If optional second argument FRAME is non-nil, check for the
2550 existence of a frame-local face with name FACE on that frame.
2551 Otherwise check for the existence of a global face. */)
2552 (Lisp_Object face, Lisp_Object frame)
2554 Lisp_Object lface;
2556 face = resolve_face_name (face, true);
2558 if (!NILP (frame))
2560 CHECK_LIVE_FRAME (frame);
2561 lface = lface_from_face_name (XFRAME (frame), face, false);
2563 else
2564 lface = lface_from_face_name (NULL, face, false);
2566 return lface;
2570 DEFUN ("internal-copy-lisp-face", Finternal_copy_lisp_face,
2571 Sinternal_copy_lisp_face, 4, 4, 0,
2572 doc: /* Copy face FROM to TO.
2573 If FRAME is t, copy the global face definition of FROM.
2574 Otherwise, copy the frame-local definition of FROM on FRAME.
2575 If NEW-FRAME is a frame, copy that data into the frame-local
2576 definition of TO on NEW-FRAME. If NEW-FRAME is nil,
2577 FRAME controls where the data is copied to.
2579 The value is TO. */)
2580 (Lisp_Object from, Lisp_Object to, Lisp_Object frame, Lisp_Object new_frame)
2582 Lisp_Object lface, copy;
2583 struct frame *f;
2585 CHECK_SYMBOL (from);
2586 CHECK_SYMBOL (to);
2588 if (EQ (frame, Qt))
2590 /* Copy global definition of FROM. We don't make copies of
2591 strings etc. because 20.2 didn't do it either. */
2592 lface = lface_from_face_name (NULL, from, true);
2593 copy = Finternal_make_lisp_face (to, Qnil);
2594 f = NULL;
2596 else
2598 /* Copy frame-local definition of FROM. */
2599 if (NILP (new_frame))
2600 new_frame = frame;
2601 CHECK_LIVE_FRAME (frame);
2602 CHECK_LIVE_FRAME (new_frame);
2603 lface = lface_from_face_name (XFRAME (frame), from, true);
2604 copy = Finternal_make_lisp_face (to, new_frame);
2605 f = XFRAME (new_frame);
2608 vcopy (copy, 0, XVECTOR (lface)->contents, LFACE_VECTOR_SIZE);
2610 /* Changing a named face means that all realized faces depending on
2611 that face are invalid. Since we cannot tell which realized faces
2612 depend on the face, make sure they are all removed. This is done
2613 by setting face_change. The next call to init_iterator will then
2614 free realized faces. */
2615 if (NILP (Fget (to, Qface_no_inherit)))
2617 if (f)
2619 f->face_change = true;
2620 fset_redisplay (f);
2622 else
2624 face_change = true;
2625 windows_or_buffers_changed = 55;
2629 return to;
2633 DEFUN ("internal-set-lisp-face-attribute", Finternal_set_lisp_face_attribute,
2634 Sinternal_set_lisp_face_attribute, 3, 4, 0,
2635 doc: /* Set attribute ATTR of FACE to VALUE.
2636 FRAME being a frame means change the face on that frame.
2637 FRAME nil means change the face of the selected frame.
2638 FRAME t means change the default for new frames.
2639 FRAME 0 means change the face on all frames, and change the default
2640 for new frames. */)
2641 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
2643 Lisp_Object lface;
2644 Lisp_Object old_value = Qnil;
2645 /* Set one of enum font_property_index (> 0) if ATTR is one of
2646 font-related attributes other than QCfont and QCfontset. */
2647 enum font_property_index prop_index = 0;
2648 struct frame *f;
2650 CHECK_SYMBOL (face);
2651 CHECK_SYMBOL (attr);
2653 face = resolve_face_name (face, true);
2655 /* If FRAME is 0, change face on all frames, and change the
2656 default for new frames. */
2657 if (INTEGERP (frame) && XINT (frame) == 0)
2659 Lisp_Object tail;
2660 Finternal_set_lisp_face_attribute (face, attr, value, Qt);
2661 FOR_EACH_FRAME (tail, frame)
2662 Finternal_set_lisp_face_attribute (face, attr, value, frame);
2663 return face;
2666 /* Set lface to the Lisp attribute vector of FACE. */
2667 if (EQ (frame, Qt))
2669 f = NULL;
2670 lface = lface_from_face_name (NULL, face, true);
2672 /* When updating face-new-frame-defaults, we put :ignore-defface
2673 where the caller wants `unspecified'. This forces the frame
2674 defaults to ignore the defface value. Otherwise, the defface
2675 will take effect, which is generally not what is intended.
2676 The value of that attribute will be inherited from some other
2677 face during face merging. See internal_merge_in_global_face. */
2678 if (UNSPECIFIEDP (value))
2679 value = QCignore_defface;
2681 else
2683 if (NILP (frame))
2684 frame = selected_frame;
2685 f = XFRAME (frame);
2687 CHECK_LIVE_FRAME (frame);
2688 lface = lface_from_face_name (f, face, false);
2690 /* If a frame-local face doesn't exist yet, create one. */
2691 if (NILP (lface))
2692 lface = Finternal_make_lisp_face (face, frame);
2695 if (EQ (attr, QCfamily))
2697 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2699 CHECK_STRING (value);
2700 if (SCHARS (value) == 0)
2701 signal_error ("Invalid face family", value);
2703 old_value = LFACE_FAMILY (lface);
2704 ASET (lface, LFACE_FAMILY_INDEX, value);
2705 prop_index = FONT_FAMILY_INDEX;
2707 else if (EQ (attr, QCfoundry))
2709 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2711 CHECK_STRING (value);
2712 if (SCHARS (value) == 0)
2713 signal_error ("Invalid face foundry", value);
2715 old_value = LFACE_FOUNDRY (lface);
2716 ASET (lface, LFACE_FOUNDRY_INDEX, value);
2717 prop_index = FONT_FOUNDRY_INDEX;
2719 else if (EQ (attr, QCheight))
2721 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2723 if (EQ (face, Qdefault))
2725 /* The default face must have an absolute size. */
2726 if (!INTEGERP (value) || XINT (value) <= 0)
2727 signal_error ("Default face height not absolute and positive",
2728 value);
2730 else
2732 /* For non-default faces, do a test merge with a random
2733 height to see if VALUE's ok. */
2734 Lisp_Object test = merge_face_heights (value,
2735 make_number (10),
2736 Qnil);
2737 if (!INTEGERP (test) || XINT (test) <= 0)
2738 signal_error ("Face height does not produce a positive integer",
2739 value);
2743 old_value = LFACE_HEIGHT (lface);
2744 ASET (lface, LFACE_HEIGHT_INDEX, value);
2745 prop_index = FONT_SIZE_INDEX;
2747 else if (EQ (attr, QCweight))
2749 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2751 CHECK_SYMBOL (value);
2752 if (FONT_WEIGHT_NAME_NUMERIC (value) < 0)
2753 signal_error ("Invalid face weight", value);
2755 old_value = LFACE_WEIGHT (lface);
2756 ASET (lface, LFACE_WEIGHT_INDEX, value);
2757 prop_index = FONT_WEIGHT_INDEX;
2759 else if (EQ (attr, QCslant))
2761 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2763 CHECK_SYMBOL (value);
2764 if (FONT_SLANT_NAME_NUMERIC (value) < 0)
2765 signal_error ("Invalid face slant", value);
2767 old_value = LFACE_SLANT (lface);
2768 ASET (lface, LFACE_SLANT_INDEX, value);
2769 prop_index = FONT_SLANT_INDEX;
2771 else if (EQ (attr, QCunderline))
2773 bool valid_p = false;
2775 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2776 valid_p = true;
2777 else if (NILP (value) || EQ (value, Qt))
2778 valid_p = true;
2779 else if (STRINGP (value) && SCHARS (value) > 0)
2780 valid_p = true;
2781 else if (CONSP (value))
2783 Lisp_Object key, val, list;
2785 list = value;
2786 /* FIXME? This errs on the side of acceptance. Eg it accepts:
2787 (defface foo '((t :underline 'foo) "doc")
2788 Maybe this is intentional, maybe it isn't.
2789 Non-nil symbols other than t are not documented as being valid.
2790 Eg compare with inverse-video, which explicitly rejects them.
2792 valid_p = true;
2794 while (!NILP (CAR_SAFE(list)))
2796 key = CAR_SAFE (list);
2797 list = CDR_SAFE (list);
2798 val = CAR_SAFE (list);
2799 list = CDR_SAFE (list);
2801 if (NILP (key) || NILP (val))
2803 valid_p = false;
2804 break;
2807 else if (EQ (key, QCcolor)
2808 && !(EQ (val, Qforeground_color)
2809 || (STRINGP (val) && SCHARS (val) > 0)))
2811 valid_p = false;
2812 break;
2815 else if (EQ (key, QCstyle)
2816 && !(EQ (val, Qline) || EQ (val, Qwave)))
2818 valid_p = false;
2819 break;
2824 if (!valid_p)
2825 signal_error ("Invalid face underline", value);
2827 old_value = LFACE_UNDERLINE (lface);
2828 ASET (lface, LFACE_UNDERLINE_INDEX, value);
2830 else if (EQ (attr, QCoverline))
2832 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2833 if ((SYMBOLP (value)
2834 && !EQ (value, Qt)
2835 && !EQ (value, Qnil))
2836 /* Overline color. */
2837 || (STRINGP (value)
2838 && SCHARS (value) == 0))
2839 signal_error ("Invalid face overline", value);
2841 old_value = LFACE_OVERLINE (lface);
2842 ASET (lface, LFACE_OVERLINE_INDEX, value);
2844 else if (EQ (attr, QCstrike_through))
2846 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2847 if ((SYMBOLP (value)
2848 && !EQ (value, Qt)
2849 && !EQ (value, Qnil))
2850 /* Strike-through color. */
2851 || (STRINGP (value)
2852 && SCHARS (value) == 0))
2853 signal_error ("Invalid face strike-through", value);
2855 old_value = LFACE_STRIKE_THROUGH (lface);
2856 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, value);
2858 else if (EQ (attr, QCbox))
2860 bool valid_p;
2862 /* Allow t meaning a simple box of width 1 in foreground color
2863 of the face. */
2864 if (EQ (value, Qt))
2865 value = make_number (1);
2867 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2868 valid_p = true;
2869 else if (NILP (value))
2870 valid_p = true;
2871 else if (INTEGERP (value))
2872 valid_p = XINT (value) != 0;
2873 else if (STRINGP (value))
2874 valid_p = SCHARS (value) > 0;
2875 else if (CONSP (value))
2877 Lisp_Object tem;
2879 tem = value;
2880 while (CONSP (tem))
2882 Lisp_Object k, v;
2884 k = XCAR (tem);
2885 tem = XCDR (tem);
2886 if (!CONSP (tem))
2887 break;
2888 v = XCAR (tem);
2889 tem = XCDR (tem);
2891 if (EQ (k, QCline_width))
2893 if (!INTEGERP (v) || XINT (v) == 0)
2894 break;
2896 else if (EQ (k, QCcolor))
2898 if (!NILP (v) && (!STRINGP (v) || SCHARS (v) == 0))
2899 break;
2901 else if (EQ (k, QCstyle))
2903 if (!EQ (v, Qpressed_button) && !EQ (v, Qreleased_button))
2904 break;
2906 else
2907 break;
2910 valid_p = NILP (tem);
2912 else
2913 valid_p = false;
2915 if (!valid_p)
2916 signal_error ("Invalid face box", value);
2918 old_value = LFACE_BOX (lface);
2919 ASET (lface, LFACE_BOX_INDEX, value);
2921 else if (EQ (attr, QCinverse_video)
2922 || EQ (attr, QCreverse_video))
2924 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2926 CHECK_SYMBOL (value);
2927 if (!EQ (value, Qt) && !NILP (value))
2928 signal_error ("Invalid inverse-video face attribute value", value);
2930 old_value = LFACE_INVERSE (lface);
2931 ASET (lface, LFACE_INVERSE_INDEX, value);
2933 else if (EQ (attr, QCforeground))
2935 /* Compatibility with 20.x. */
2936 if (NILP (value))
2937 value = Qunspecified;
2938 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2940 /* Don't check for valid color names here because it depends
2941 on the frame (display) whether the color will be valid
2942 when the face is realized. */
2943 CHECK_STRING (value);
2944 if (SCHARS (value) == 0)
2945 signal_error ("Empty foreground color value", value);
2947 old_value = LFACE_FOREGROUND (lface);
2948 ASET (lface, LFACE_FOREGROUND_INDEX, value);
2950 else if (EQ (attr, QCdistant_foreground))
2952 /* Compatibility with 20.x. */
2953 if (NILP (value))
2954 value = Qunspecified;
2955 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2957 /* Don't check for valid color names here because it depends
2958 on the frame (display) whether the color will be valid
2959 when the face is realized. */
2960 CHECK_STRING (value);
2961 if (SCHARS (value) == 0)
2962 signal_error ("Empty distant-foreground color value", value);
2964 old_value = LFACE_DISTANT_FOREGROUND (lface);
2965 ASET (lface, LFACE_DISTANT_FOREGROUND_INDEX, value);
2967 else if (EQ (attr, QCbackground))
2969 /* Compatibility with 20.x. */
2970 if (NILP (value))
2971 value = Qunspecified;
2972 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2974 /* Don't check for valid color names here because it depends
2975 on the frame (display) whether the color will be valid
2976 when the face is realized. */
2977 CHECK_STRING (value);
2978 if (SCHARS (value) == 0)
2979 signal_error ("Empty background color value", value);
2981 old_value = LFACE_BACKGROUND (lface);
2982 ASET (lface, LFACE_BACKGROUND_INDEX, value);
2984 else if (EQ (attr, QCstipple))
2986 #if defined (HAVE_WINDOW_SYSTEM)
2987 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
2988 && !NILP (value)
2989 && NILP (Fbitmap_spec_p (value)))
2990 signal_error ("Invalid stipple attribute", value);
2991 old_value = LFACE_STIPPLE (lface);
2992 ASET (lface, LFACE_STIPPLE_INDEX, value);
2993 #endif /* HAVE_WINDOW_SYSTEM */
2995 else if (EQ (attr, QCwidth))
2997 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2999 CHECK_SYMBOL (value);
3000 if (FONT_WIDTH_NAME_NUMERIC (value) < 0)
3001 signal_error ("Invalid face width", value);
3003 old_value = LFACE_SWIDTH (lface);
3004 ASET (lface, LFACE_SWIDTH_INDEX, value);
3005 prop_index = FONT_WIDTH_INDEX;
3007 else if (EQ (attr, QCfont))
3009 #ifdef HAVE_WINDOW_SYSTEM
3010 if (EQ (frame, Qt) || FRAME_WINDOW_P (f))
3012 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3014 struct frame *f1;
3016 old_value = LFACE_FONT (lface);
3017 if (! FONTP (value))
3019 if (STRINGP (value))
3021 Lisp_Object name = value;
3022 int fontset = fs_query_fontset (name, 0);
3024 if (fontset >= 0)
3025 name = fontset_ascii (fontset);
3026 value = font_spec_from_name (name);
3027 if (!FONTP (value))
3028 signal_error ("Invalid font name", name);
3030 else
3031 signal_error ("Invalid font or font-spec", value);
3033 if (EQ (frame, Qt))
3034 f1 = XFRAME (selected_frame);
3035 else
3036 f1 = XFRAME (frame);
3038 /* FIXME:
3039 If frame is t, and selected frame is a tty frame, the font
3040 can't be realized. An improvement would be to loop over frames
3041 for a non-tty frame and use that. See discussion in Bug#18573.
3042 For a daemon, frame may be an initial frame (Bug#18869). */
3043 if (FRAME_WINDOW_P (f1))
3045 if (! FONT_OBJECT_P (value))
3047 Lisp_Object *attrs = XVECTOR (lface)->contents;
3048 Lisp_Object font_object;
3050 font_object = font_load_for_lface (f1, attrs, value);
3051 if (NILP (font_object))
3052 signal_error ("Font not available", value);
3053 value = font_object;
3055 set_lface_from_font (f1, lface, value, true);
3056 f1->face_change = 1;
3059 else
3060 ASET (lface, LFACE_FONT_INDEX, value);
3062 #endif /* HAVE_WINDOW_SYSTEM */
3064 else if (EQ (attr, QCfontset))
3066 #ifdef HAVE_WINDOW_SYSTEM
3067 if (EQ (frame, Qt) || FRAME_WINDOW_P (f))
3069 Lisp_Object tmp;
3071 old_value = LFACE_FONTSET (lface);
3072 tmp = Fquery_fontset (value, Qnil);
3073 if (NILP (tmp))
3074 signal_error ("Invalid fontset name", value);
3075 ASET (lface, LFACE_FONTSET_INDEX, value = tmp);
3077 #endif /* HAVE_WINDOW_SYSTEM */
3079 else if (EQ (attr, QCinherit))
3081 Lisp_Object tail;
3082 if (SYMBOLP (value))
3083 tail = Qnil;
3084 else
3085 for (tail = value; CONSP (tail); tail = XCDR (tail))
3086 if (!SYMBOLP (XCAR (tail)))
3087 break;
3088 if (NILP (tail))
3089 ASET (lface, LFACE_INHERIT_INDEX, value);
3090 else
3091 signal_error ("Invalid face inheritance", value);
3093 else if (EQ (attr, QCbold))
3095 old_value = LFACE_WEIGHT (lface);
3096 ASET (lface, LFACE_WEIGHT_INDEX, NILP (value) ? Qnormal : Qbold);
3097 prop_index = FONT_WEIGHT_INDEX;
3099 else if (EQ (attr, QCitalic))
3101 attr = QCslant;
3102 old_value = LFACE_SLANT (lface);
3103 ASET (lface, LFACE_SLANT_INDEX, NILP (value) ? Qnormal : Qitalic);
3104 prop_index = FONT_SLANT_INDEX;
3106 else
3107 signal_error ("Invalid face attribute name", attr);
3109 if (prop_index)
3111 /* If a font-related attribute other than QCfont and QCfontset
3112 is specified, and if the original QCfont attribute has a font
3113 (font-spec or font-object), set the corresponding property in
3114 the font to nil so that the font selector doesn't think that
3115 the attribute is mandatory. Also, clear the average
3116 width. */
3117 font_clear_prop (XVECTOR (lface)->contents, prop_index);
3120 /* Changing a named face means that all realized faces depending on
3121 that face are invalid. Since we cannot tell which realized faces
3122 depend on the face, make sure they are all removed. This is done
3123 by setting face_change. The next call to init_iterator will then
3124 free realized faces. */
3125 if (!EQ (frame, Qt)
3126 && NILP (Fget (face, Qface_no_inherit))
3127 && NILP (Fequal (old_value, value)))
3129 f->face_change = true;
3130 fset_redisplay (f);
3133 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3134 && NILP (Fequal (old_value, value)))
3136 Lisp_Object param;
3138 param = Qnil;
3140 if (EQ (face, Qdefault))
3142 #ifdef HAVE_WINDOW_SYSTEM
3143 /* Changed font-related attributes of the `default' face are
3144 reflected in changed `font' frame parameters. */
3145 if (FRAMEP (frame)
3146 && (prop_index || EQ (attr, QCfont))
3147 && lface_fully_specified_p (XVECTOR (lface)->contents))
3148 set_font_frame_param (frame, lface);
3149 else
3150 #endif /* HAVE_WINDOW_SYSTEM */
3152 if (EQ (attr, QCforeground))
3153 param = Qforeground_color;
3154 else if (EQ (attr, QCbackground))
3155 param = Qbackground_color;
3157 #ifdef HAVE_WINDOW_SYSTEM
3158 #ifndef HAVE_NTGUI
3159 else if (EQ (face, Qscroll_bar))
3161 /* Changing the colors of `scroll-bar' sets frame parameters
3162 `scroll-bar-foreground' and `scroll-bar-background'. */
3163 if (EQ (attr, QCforeground))
3164 param = Qscroll_bar_foreground;
3165 else if (EQ (attr, QCbackground))
3166 param = Qscroll_bar_background;
3168 #endif /* not HAVE_NTGUI */
3169 else if (EQ (face, Qborder))
3171 /* Changing background color of `border' sets frame parameter
3172 `border-color'. */
3173 if (EQ (attr, QCbackground))
3174 param = Qborder_color;
3176 else if (EQ (face, Qcursor))
3178 /* Changing background color of `cursor' sets frame parameter
3179 `cursor-color'. */
3180 if (EQ (attr, QCbackground))
3181 param = Qcursor_color;
3183 else if (EQ (face, Qmouse))
3185 /* Changing background color of `mouse' sets frame parameter
3186 `mouse-color'. */
3187 if (EQ (attr, QCbackground))
3188 param = Qmouse_color;
3190 #endif /* HAVE_WINDOW_SYSTEM */
3191 else if (EQ (face, Qmenu))
3193 /* Indicate that we have to update the menu bar when realizing
3194 faces on FRAME. FRAME t change the default for new frames.
3195 We do this by setting the flag in new face caches. */
3196 if (FRAMEP (frame))
3198 struct frame *f = XFRAME (frame);
3199 if (FRAME_FACE_CACHE (f) == NULL)
3200 FRAME_FACE_CACHE (f) = make_face_cache (f);
3201 FRAME_FACE_CACHE (f)->menu_face_changed_p = true;
3203 else
3204 menu_face_changed_default = true;
3207 if (!NILP (param))
3209 if (EQ (frame, Qt))
3210 /* Update `default-frame-alist', which is used for new frames. */
3212 store_in_alist (&Vdefault_frame_alist, param, value);
3214 else
3215 /* Update the current frame's parameters. */
3217 Lisp_Object cons;
3218 cons = XCAR (Vparam_value_alist);
3219 XSETCAR (cons, param);
3220 XSETCDR (cons, value);
3221 Fmodify_frame_parameters (frame, Vparam_value_alist);
3226 return face;
3230 /* Update the corresponding face when frame parameter PARAM on frame F
3231 has been assigned the value NEW_VALUE. */
3233 void
3234 update_face_from_frame_parameter (struct frame *f, Lisp_Object param,
3235 Lisp_Object new_value)
3237 Lisp_Object face = Qnil;
3238 Lisp_Object lface;
3240 /* If there are no faces yet, give up. This is the case when called
3241 from Fx_create_frame, and we do the necessary things later in
3242 face-set-after-frame-defaults. */
3243 if (NILP (f->face_alist))
3244 return;
3246 if (EQ (param, Qforeground_color))
3248 face = Qdefault;
3249 lface = lface_from_face_name (f, face, true);
3250 ASET (lface, LFACE_FOREGROUND_INDEX,
3251 (STRINGP (new_value) ? new_value : Qunspecified));
3252 realize_basic_faces (f);
3254 else if (EQ (param, Qbackground_color))
3256 Lisp_Object frame;
3258 /* Changing the background color might change the background
3259 mode, so that we have to load new defface specs.
3260 Call frame-set-background-mode to do that. */
3261 XSETFRAME (frame, f);
3262 call1 (Qframe_set_background_mode, frame);
3264 face = Qdefault;
3265 lface = lface_from_face_name (f, face, true);
3266 ASET (lface, LFACE_BACKGROUND_INDEX,
3267 (STRINGP (new_value) ? new_value : Qunspecified));
3268 realize_basic_faces (f);
3270 #ifdef HAVE_WINDOW_SYSTEM
3271 else if (EQ (param, Qborder_color))
3273 face = Qborder;
3274 lface = lface_from_face_name (f, face, true);
3275 ASET (lface, LFACE_BACKGROUND_INDEX,
3276 (STRINGP (new_value) ? new_value : Qunspecified));
3278 else if (EQ (param, Qcursor_color))
3280 face = Qcursor;
3281 lface = lface_from_face_name (f, face, true);
3282 ASET (lface, LFACE_BACKGROUND_INDEX,
3283 (STRINGP (new_value) ? new_value : Qunspecified));
3285 else if (EQ (param, Qmouse_color))
3287 face = Qmouse;
3288 lface = lface_from_face_name (f, face, true);
3289 ASET (lface, LFACE_BACKGROUND_INDEX,
3290 (STRINGP (new_value) ? new_value : Qunspecified));
3292 #endif
3294 /* Changing a named face means that all realized faces depending on
3295 that face are invalid. Since we cannot tell which realized faces
3296 depend on the face, make sure they are all removed. This is done
3297 by setting face_change. The next call to init_iterator will then
3298 free realized faces. */
3299 if (!NILP (face)
3300 && NILP (Fget (face, Qface_no_inherit)))
3302 f->face_change = true;
3303 fset_redisplay (f);
3308 #ifdef HAVE_WINDOW_SYSTEM
3310 /* Set the `font' frame parameter of FRAME determined from the
3311 font-object set in `default' face attributes LFACE. */
3313 static void
3314 set_font_frame_param (Lisp_Object frame, Lisp_Object lface)
3316 struct frame *f = XFRAME (frame);
3317 Lisp_Object font;
3319 if (FRAME_WINDOW_P (f)
3320 /* Don't do anything if the font is `unspecified'. This can
3321 happen during frame creation. */
3322 && (font = LFACE_FONT (lface),
3323 ! UNSPECIFIEDP (font)))
3325 if (FONT_SPEC_P (font))
3327 font = font_load_for_lface (f, XVECTOR (lface)->contents, font);
3328 if (NILP (font))
3329 return;
3330 ASET (lface, LFACE_FONT_INDEX, font);
3332 f->default_face_done_p = false;
3333 AUTO_FRAME_ARG (arg, Qfont, font);
3334 Fmodify_frame_parameters (frame, arg);
3338 DEFUN ("internal-face-x-get-resource", Finternal_face_x_get_resource,
3339 Sinternal_face_x_get_resource, 2, 3, 0,
3340 doc: /* Get the value of X resource RESOURCE, class CLASS.
3341 Returned value is for the display of frame FRAME. If FRAME is not
3342 specified or nil, use selected frame. This function exists because
3343 ordinary `x-get-resource' doesn't take a frame argument. */)
3344 (Lisp_Object resource, Lisp_Object class, Lisp_Object frame)
3346 Lisp_Object value = Qnil;
3347 struct frame *f;
3349 CHECK_STRING (resource);
3350 CHECK_STRING (class);
3351 f = decode_live_frame (frame);
3352 block_input ();
3353 value = display_x_get_resource (FRAME_DISPLAY_INFO (f),
3354 resource, class, Qnil, Qnil);
3355 unblock_input ();
3356 return value;
3360 /* Return resource string VALUE as a boolean value, i.e. nil, or t.
3361 If VALUE is "on" or "true", return t. If VALUE is "off" or
3362 "false", return nil. Otherwise, if SIGNAL_P, signal an
3363 error; if !SIGNAL_P, return 0. */
3365 static Lisp_Object
3366 face_boolean_x_resource_value (Lisp_Object value, bool signal_p)
3368 Lisp_Object result = make_number (0);
3370 eassert (STRINGP (value));
3372 if (xstrcasecmp (SSDATA (value), "on") == 0
3373 || xstrcasecmp (SSDATA (value), "true") == 0)
3374 result = Qt;
3375 else if (xstrcasecmp (SSDATA (value), "off") == 0
3376 || xstrcasecmp (SSDATA (value), "false") == 0)
3377 result = Qnil;
3378 else if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3379 result = Qunspecified;
3380 else if (signal_p)
3381 signal_error ("Invalid face attribute value from X resource", value);
3383 return result;
3387 DEFUN ("internal-set-lisp-face-attribute-from-resource",
3388 Finternal_set_lisp_face_attribute_from_resource,
3389 Sinternal_set_lisp_face_attribute_from_resource,
3390 3, 4, 0, doc: /* */)
3391 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
3393 CHECK_SYMBOL (face);
3394 CHECK_SYMBOL (attr);
3395 CHECK_STRING (value);
3397 if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3398 value = Qunspecified;
3399 else if (EQ (attr, QCheight))
3401 value = Fstring_to_number (value, make_number (10));
3402 if (XINT (value) <= 0)
3403 signal_error ("Invalid face height from X resource", value);
3405 else if (EQ (attr, QCbold) || EQ (attr, QCitalic))
3406 value = face_boolean_x_resource_value (value, true);
3407 else if (EQ (attr, QCweight) || EQ (attr, QCslant) || EQ (attr, QCwidth))
3408 value = intern (SSDATA (value));
3409 else if (EQ (attr, QCreverse_video) || EQ (attr, QCinverse_video))
3410 value = face_boolean_x_resource_value (value, true);
3411 else if (EQ (attr, QCunderline)
3412 || EQ (attr, QCoverline)
3413 || EQ (attr, QCstrike_through))
3415 Lisp_Object boolean_value;
3417 /* If the result of face_boolean_x_resource_value is t or nil,
3418 VALUE does NOT specify a color. */
3419 boolean_value = face_boolean_x_resource_value (value, false);
3420 if (SYMBOLP (boolean_value))
3421 value = boolean_value;
3423 else if (EQ (attr, QCbox) || EQ (attr, QCinherit))
3424 value = Fcar (Fread_from_string (value, Qnil, Qnil));
3426 return Finternal_set_lisp_face_attribute (face, attr, value, frame);
3429 #endif /* HAVE_WINDOW_SYSTEM */
3432 /***********************************************************************
3433 Menu face
3434 ***********************************************************************/
3436 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
3438 /* Make menus on frame F appear as specified by the `menu' face. */
3440 static void
3441 x_update_menu_appearance (struct frame *f)
3443 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
3444 XrmDatabase rdb;
3446 if (dpyinfo
3447 && (rdb = XrmGetDatabase (FRAME_X_DISPLAY (f)),
3448 rdb != NULL))
3450 char line[512];
3451 char *buf = line;
3452 ptrdiff_t bufsize = sizeof line;
3453 Lisp_Object lface = lface_from_face_name (f, Qmenu, true);
3454 struct face *face = FACE_FROM_ID (f, MENU_FACE_ID);
3455 const char *myname = SSDATA (Vx_resource_name);
3456 bool changed_p = false;
3457 #ifdef USE_MOTIF
3458 const char *popup_path = "popup_menu";
3459 #else
3460 const char *popup_path = "menu.popup";
3461 #endif
3463 if (STRINGP (LFACE_FOREGROUND (lface)))
3465 exprintf (&buf, &bufsize, line, -1, "%s.%s*foreground: %s",
3466 myname, popup_path,
3467 SDATA (LFACE_FOREGROUND (lface)));
3468 XrmPutLineResource (&rdb, line);
3469 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*foreground: %s",
3470 myname, SDATA (LFACE_FOREGROUND (lface)));
3471 XrmPutLineResource (&rdb, line);
3472 changed_p = true;
3475 if (STRINGP (LFACE_BACKGROUND (lface)))
3477 exprintf (&buf, &bufsize, line, -1, "%s.%s*background: %s",
3478 myname, popup_path,
3479 SDATA (LFACE_BACKGROUND (lface)));
3480 XrmPutLineResource (&rdb, line);
3482 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*background: %s",
3483 myname, SDATA (LFACE_BACKGROUND (lface)));
3484 XrmPutLineResource (&rdb, line);
3485 changed_p = true;
3488 if (face->font
3489 /* On Solaris 5.8, it's been reported that the `menu' face
3490 can be unspecified here, during startup. Why this
3491 happens remains unknown. -- cyd */
3492 && FONTP (LFACE_FONT (lface))
3493 && (!UNSPECIFIEDP (LFACE_FAMILY (lface))
3494 || !UNSPECIFIEDP (LFACE_FOUNDRY (lface))
3495 || !UNSPECIFIEDP (LFACE_SWIDTH (lface))
3496 || !UNSPECIFIEDP (LFACE_WEIGHT (lface))
3497 || !UNSPECIFIEDP (LFACE_SLANT (lface))
3498 || !UNSPECIFIEDP (LFACE_HEIGHT (lface))))
3500 Lisp_Object xlfd = Ffont_xlfd_name (LFACE_FONT (lface), Qnil);
3501 #ifdef USE_MOTIF
3502 const char *suffix = "List";
3503 bool motif = true;
3504 #else
3505 #if defined HAVE_X_I18N
3507 const char *suffix = "Set";
3508 #else
3509 const char *suffix = "";
3510 #endif
3511 bool motif = false;
3512 #endif
3514 if (! NILP (xlfd))
3516 #if defined HAVE_X_I18N
3517 char *fontsetname = xic_create_fontsetname (SSDATA (xlfd), motif);
3518 #else
3519 char *fontsetname = SSDATA (xlfd);
3520 #endif
3521 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*font%s: %s",
3522 myname, suffix, fontsetname);
3523 XrmPutLineResource (&rdb, line);
3525 exprintf (&buf, &bufsize, line, -1, "%s.%s*font%s: %s",
3526 myname, popup_path, suffix, fontsetname);
3527 XrmPutLineResource (&rdb, line);
3528 changed_p = true;
3529 if (fontsetname != SSDATA (xlfd))
3530 xfree (fontsetname);
3534 if (changed_p && f->output_data.x->menubar_widget)
3535 free_frame_menubar (f);
3537 if (buf != line)
3538 xfree (buf);
3542 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
3545 DEFUN ("face-attribute-relative-p", Fface_attribute_relative_p,
3546 Sface_attribute_relative_p,
3547 2, 2, 0,
3548 doc: /* Check whether a face attribute value is relative.
3549 Specifically, this function returns t if the attribute ATTRIBUTE
3550 with the value VALUE is relative.
3552 A relative value is one that doesn't entirely override whatever is
3553 inherited from another face. For most possible attributes,
3554 the only relative value that users see is `unspecified'.
3555 However, for :height, floating point values are also relative. */
3556 attributes: const)
3557 (Lisp_Object attribute, Lisp_Object value)
3559 if (EQ (value, Qunspecified) || (EQ (value, QCignore_defface)))
3560 return Qt;
3561 else if (EQ (attribute, QCheight))
3562 return INTEGERP (value) ? Qnil : Qt;
3563 else
3564 return Qnil;
3567 DEFUN ("merge-face-attribute", Fmerge_face_attribute, Smerge_face_attribute,
3568 3, 3, 0,
3569 doc: /* Return face ATTRIBUTE VALUE1 merged with VALUE2.
3570 If VALUE1 or VALUE2 are absolute (see `face-attribute-relative-p'), then
3571 the result will be absolute, otherwise it will be relative. */)
3572 (Lisp_Object attribute, Lisp_Object value1, Lisp_Object value2)
3574 if (EQ (value1, Qunspecified) || EQ (value1, QCignore_defface))
3575 return value2;
3576 else if (EQ (attribute, QCheight))
3577 return merge_face_heights (value1, value2, value1);
3578 else
3579 return value1;
3583 DEFUN ("internal-get-lisp-face-attribute", Finternal_get_lisp_face_attribute,
3584 Sinternal_get_lisp_face_attribute,
3585 2, 3, 0,
3586 doc: /* Return face attribute KEYWORD of face SYMBOL.
3587 If SYMBOL does not name a valid Lisp face or KEYWORD isn't a valid
3588 face attribute name, signal an error.
3589 If the optional argument FRAME is given, report on face SYMBOL in that
3590 frame. If FRAME is t, report on the defaults for face SYMBOL (for new
3591 frames). If FRAME is omitted or nil, use the selected frame. */)
3592 (Lisp_Object symbol, Lisp_Object keyword, Lisp_Object frame)
3594 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3595 Lisp_Object lface = lface_from_face_name (f, symbol, true), value = Qnil;
3597 CHECK_SYMBOL (symbol);
3598 CHECK_SYMBOL (keyword);
3600 if (EQ (keyword, QCfamily))
3601 value = LFACE_FAMILY (lface);
3602 else if (EQ (keyword, QCfoundry))
3603 value = LFACE_FOUNDRY (lface);
3604 else if (EQ (keyword, QCheight))
3605 value = LFACE_HEIGHT (lface);
3606 else if (EQ (keyword, QCweight))
3607 value = LFACE_WEIGHT (lface);
3608 else if (EQ (keyword, QCslant))
3609 value = LFACE_SLANT (lface);
3610 else if (EQ (keyword, QCunderline))
3611 value = LFACE_UNDERLINE (lface);
3612 else if (EQ (keyword, QCoverline))
3613 value = LFACE_OVERLINE (lface);
3614 else if (EQ (keyword, QCstrike_through))
3615 value = LFACE_STRIKE_THROUGH (lface);
3616 else if (EQ (keyword, QCbox))
3617 value = LFACE_BOX (lface);
3618 else if (EQ (keyword, QCinverse_video)
3619 || EQ (keyword, QCreverse_video))
3620 value = LFACE_INVERSE (lface);
3621 else if (EQ (keyword, QCforeground))
3622 value = LFACE_FOREGROUND (lface);
3623 else if (EQ (keyword, QCdistant_foreground))
3624 value = LFACE_DISTANT_FOREGROUND (lface);
3625 else if (EQ (keyword, QCbackground))
3626 value = LFACE_BACKGROUND (lface);
3627 else if (EQ (keyword, QCstipple))
3628 value = LFACE_STIPPLE (lface);
3629 else if (EQ (keyword, QCwidth))
3630 value = LFACE_SWIDTH (lface);
3631 else if (EQ (keyword, QCinherit))
3632 value = LFACE_INHERIT (lface);
3633 else if (EQ (keyword, QCfont))
3634 value = LFACE_FONT (lface);
3635 else if (EQ (keyword, QCfontset))
3636 value = LFACE_FONTSET (lface);
3637 else
3638 signal_error ("Invalid face attribute name", keyword);
3640 if (IGNORE_DEFFACE_P (value))
3641 return Qunspecified;
3643 return value;
3647 DEFUN ("internal-lisp-face-attribute-values",
3648 Finternal_lisp_face_attribute_values,
3649 Sinternal_lisp_face_attribute_values, 1, 1, 0,
3650 doc: /* Return a list of valid discrete values for face attribute ATTR.
3651 Value is nil if ATTR doesn't have a discrete set of valid values. */)
3652 (Lisp_Object attr)
3654 Lisp_Object result = Qnil;
3656 CHECK_SYMBOL (attr);
3658 if (EQ (attr, QCunderline) || EQ (attr, QCoverline)
3659 || EQ (attr, QCstrike_through)
3660 || EQ (attr, QCinverse_video) || EQ (attr, QCreverse_video))
3661 result = list2 (Qt, Qnil);
3663 return result;
3667 DEFUN ("internal-merge-in-global-face", Finternal_merge_in_global_face,
3668 Sinternal_merge_in_global_face, 2, 2, 0,
3669 doc: /* Add attributes from frame-default definition of FACE to FACE on FRAME.
3670 Default face attributes override any local face attributes. */)
3671 (Lisp_Object face, Lisp_Object frame)
3673 int i;
3674 Lisp_Object global_lface, local_lface, *gvec, *lvec;
3675 struct frame *f = XFRAME (frame);
3677 CHECK_LIVE_FRAME (frame);
3678 global_lface = lface_from_face_name (NULL, face, true);
3679 local_lface = lface_from_face_name (f, face, false);
3680 if (NILP (local_lface))
3681 local_lface = Finternal_make_lisp_face (face, frame);
3683 /* Make every specified global attribute override the local one.
3684 BEWARE!! This is only used from `face-set-after-frame-default' where
3685 the local frame is defined from default specs in `face-defface-spec'
3686 and those should be overridden by global settings. Hence the strange
3687 "global before local" priority. */
3688 lvec = XVECTOR (local_lface)->contents;
3689 gvec = XVECTOR (global_lface)->contents;
3690 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3691 if (IGNORE_DEFFACE_P (gvec[i]))
3692 ASET (local_lface, i, Qunspecified);
3693 else if (! UNSPECIFIEDP (gvec[i]))
3694 ASET (local_lface, i, AREF (global_lface, i));
3696 /* If the default face was changed, update the face cache and the
3697 `font' frame parameter. */
3698 if (EQ (face, Qdefault))
3700 struct face_cache *c = FRAME_FACE_CACHE (f);
3701 struct face *newface, *oldface = FACE_FROM_ID (f, DEFAULT_FACE_ID);
3702 Lisp_Object attrs[LFACE_VECTOR_SIZE];
3704 /* This can be NULL (e.g., in batch mode). */
3705 if (oldface)
3707 /* Ensure that the face vector is fully specified by merging
3708 the previously-cached vector. */
3709 memcpy (attrs, oldface->lface, sizeof attrs);
3710 merge_face_vectors (f, lvec, attrs, 0);
3711 vcopy (local_lface, 0, attrs, LFACE_VECTOR_SIZE);
3712 newface = realize_face (c, lvec, DEFAULT_FACE_ID);
3714 if ((! UNSPECIFIEDP (gvec[LFACE_FAMILY_INDEX])
3715 || ! UNSPECIFIEDP (gvec[LFACE_FOUNDRY_INDEX])
3716 || ! UNSPECIFIEDP (gvec[LFACE_HEIGHT_INDEX])
3717 || ! UNSPECIFIEDP (gvec[LFACE_WEIGHT_INDEX])
3718 || ! UNSPECIFIEDP (gvec[LFACE_SLANT_INDEX])
3719 || ! UNSPECIFIEDP (gvec[LFACE_SWIDTH_INDEX])
3720 || ! UNSPECIFIEDP (gvec[LFACE_FONT_INDEX]))
3721 && newface->font)
3723 Lisp_Object name = newface->font->props[FONT_NAME_INDEX];
3724 AUTO_FRAME_ARG (arg, Qfont, name);
3725 Fmodify_frame_parameters (frame, arg);
3728 if (STRINGP (gvec[LFACE_FOREGROUND_INDEX]))
3730 AUTO_FRAME_ARG (arg, Qforeground_color,
3731 gvec[LFACE_FOREGROUND_INDEX]);
3732 Fmodify_frame_parameters (frame, arg);
3735 if (STRINGP (gvec[LFACE_BACKGROUND_INDEX]))
3737 AUTO_FRAME_ARG (arg, Qbackground_color,
3738 gvec[LFACE_BACKGROUND_INDEX]);
3739 Fmodify_frame_parameters (frame, arg);
3744 return Qnil;
3748 /* The following function is implemented for compatibility with 20.2.
3749 The function is used in x-resolve-fonts when it is asked to
3750 return fonts with the same size as the font of a face. This is
3751 done in fontset.el. */
3753 DEFUN ("face-font", Fface_font, Sface_font, 1, 3, 0,
3754 doc: /* Return the font name of face FACE, or nil if it is unspecified.
3755 The font name is, by default, for ASCII characters.
3756 If the optional argument FRAME is given, report on face FACE in that frame.
3757 If FRAME is t, report on the defaults for face FACE (for new frames).
3758 The font default for a face is either nil, or a list
3759 of the form (bold), (italic) or (bold italic).
3760 If FRAME is omitted or nil, use the selected frame. And, in this case,
3761 if the optional third argument CHARACTER is given,
3762 return the font name used for CHARACTER. */)
3763 (Lisp_Object face, Lisp_Object frame, Lisp_Object character)
3765 if (EQ (frame, Qt))
3767 Lisp_Object result = Qnil;
3768 Lisp_Object lface = lface_from_face_name (NULL, face, true);
3770 if (!UNSPECIFIEDP (LFACE_WEIGHT (lface))
3771 && !EQ (LFACE_WEIGHT (lface), Qnormal))
3772 result = Fcons (Qbold, result);
3774 if (!UNSPECIFIEDP (LFACE_SLANT (lface))
3775 && !EQ (LFACE_SLANT (lface), Qnormal))
3776 result = Fcons (Qitalic, result);
3778 return result;
3780 else
3782 struct frame *f = decode_live_frame (frame);
3783 int face_id = lookup_named_face (f, face, true);
3784 struct face *fface = FACE_FROM_ID (f, face_id);
3786 if (! fface)
3787 return Qnil;
3788 #ifdef HAVE_WINDOW_SYSTEM
3789 if (FRAME_WINDOW_P (f) && !NILP (character))
3791 CHECK_CHARACTER (character);
3792 face_id = FACE_FOR_CHAR (f, fface, XINT (character), -1, Qnil);
3793 fface = FACE_FROM_ID (f, face_id);
3795 return (fface->font
3796 ? fface->font->props[FONT_NAME_INDEX]
3797 : Qnil);
3798 #else /* !HAVE_WINDOW_SYSTEM */
3799 return build_string (FRAME_MSDOS_P (f)
3800 ? "ms-dos"
3801 : FRAME_W32_P (f) ? "w32term"
3802 :"tty");
3803 #endif
3808 /* Compare face-attribute values v1 and v2 for equality. Value is true if
3809 all attributes are `equal'. Tries to be fast because this function
3810 is called quite often. */
3812 static bool
3813 face_attr_equal_p (Lisp_Object v1, Lisp_Object v2)
3815 /* Type can differ, e.g. when one attribute is unspecified, i.e. nil,
3816 and the other is specified. */
3817 if (XTYPE (v1) != XTYPE (v2))
3818 return false;
3820 if (EQ (v1, v2))
3821 return true;
3823 switch (XTYPE (v1))
3825 case Lisp_String:
3826 if (SBYTES (v1) != SBYTES (v2))
3827 return false;
3829 return memcmp (SDATA (v1), SDATA (v2), SBYTES (v1)) == 0;
3831 case_Lisp_Int:
3832 case Lisp_Symbol:
3833 return false;
3835 default:
3836 return !NILP (Fequal (v1, v2));
3841 /* Compare face vectors V1 and V2 for equality. Value is true if
3842 all attributes are `equal'. Tries to be fast because this function
3843 is called quite often. */
3845 static bool
3846 lface_equal_p (Lisp_Object *v1, Lisp_Object *v2)
3848 int i;
3849 bool equal_p = true;
3851 for (i = 1; i < LFACE_VECTOR_SIZE && equal_p; ++i)
3852 equal_p = face_attr_equal_p (v1[i], v2[i]);
3854 return equal_p;
3858 DEFUN ("internal-lisp-face-equal-p", Finternal_lisp_face_equal_p,
3859 Sinternal_lisp_face_equal_p, 2, 3, 0,
3860 doc: /* True if FACE1 and FACE2 are equal.
3861 If the optional argument FRAME is given, report on FACE1 and FACE2 in that frame.
3862 If FRAME is t, report on the defaults for FACE1 and FACE2 (for new frames).
3863 If FRAME is omitted or nil, use the selected frame. */)
3864 (Lisp_Object face1, Lisp_Object face2, Lisp_Object frame)
3866 bool equal_p;
3867 struct frame *f;
3868 Lisp_Object lface1, lface2;
3870 /* Don't use decode_window_system_frame here because this function
3871 is called before X frames exist. At that time, if FRAME is nil,
3872 selected_frame will be used which is the frame dumped with
3873 Emacs. That frame is not an X frame. */
3874 f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3876 lface1 = lface_from_face_name (f, face1, true);
3877 lface2 = lface_from_face_name (f, face2, true);
3878 equal_p = lface_equal_p (XVECTOR (lface1)->contents,
3879 XVECTOR (lface2)->contents);
3880 return equal_p ? Qt : Qnil;
3884 DEFUN ("internal-lisp-face-empty-p", Finternal_lisp_face_empty_p,
3885 Sinternal_lisp_face_empty_p, 1, 2, 0,
3886 doc: /* True if FACE has no attribute specified.
3887 If the optional argument FRAME is given, report on face FACE in that frame.
3888 If FRAME is t, report on the defaults for face FACE (for new frames).
3889 If FRAME is omitted or nil, use the selected frame. */)
3890 (Lisp_Object face, Lisp_Object frame)
3892 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3893 Lisp_Object lface = lface_from_face_name (f, face, true);
3894 int i;
3896 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3897 if (!UNSPECIFIEDP (AREF (lface, i)))
3898 break;
3900 return i == LFACE_VECTOR_SIZE ? Qt : Qnil;
3904 DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist,
3905 0, 1, 0,
3906 doc: /* Return an alist of frame-local faces defined on FRAME.
3907 For internal use only. */)
3908 (Lisp_Object frame)
3910 return decode_live_frame (frame)->face_alist;
3914 /* Return a hash code for Lisp string STRING with case ignored. Used
3915 below in computing a hash value for a Lisp face. */
3917 static unsigned
3918 hash_string_case_insensitive (Lisp_Object string)
3920 const unsigned char *s;
3921 unsigned hash = 0;
3922 eassert (STRINGP (string));
3923 for (s = SDATA (string); *s; ++s)
3924 hash = (hash << 1) ^ c_tolower (*s);
3925 return hash;
3929 /* Return a hash code for face attribute vector V. */
3931 static unsigned
3932 lface_hash (Lisp_Object *v)
3934 return (hash_string_case_insensitive (v[LFACE_FAMILY_INDEX])
3935 ^ hash_string_case_insensitive (v[LFACE_FOUNDRY_INDEX])
3936 ^ hash_string_case_insensitive (v[LFACE_FOREGROUND_INDEX])
3937 ^ hash_string_case_insensitive (v[LFACE_BACKGROUND_INDEX])
3938 ^ XHASH (v[LFACE_WEIGHT_INDEX])
3939 ^ XHASH (v[LFACE_SLANT_INDEX])
3940 ^ XHASH (v[LFACE_SWIDTH_INDEX])
3941 ^ XHASH (v[LFACE_HEIGHT_INDEX]));
3944 #ifdef HAVE_WINDOW_SYSTEM
3946 /* Return true if LFACE1 and LFACE2 specify the same font (without
3947 considering charsets/registries). They do if they specify the same
3948 family, point size, weight, width, slant, and font. Both
3949 LFACE1 and LFACE2 must be fully-specified. */
3951 static bool
3952 lface_same_font_attributes_p (Lisp_Object *lface1, Lisp_Object *lface2)
3954 eassert (lface_fully_specified_p (lface1)
3955 && lface_fully_specified_p (lface2));
3956 return (xstrcasecmp (SSDATA (lface1[LFACE_FAMILY_INDEX]),
3957 SSDATA (lface2[LFACE_FAMILY_INDEX])) == 0
3958 && xstrcasecmp (SSDATA (lface1[LFACE_FOUNDRY_INDEX]),
3959 SSDATA (lface2[LFACE_FOUNDRY_INDEX])) == 0
3960 && EQ (lface1[LFACE_HEIGHT_INDEX], lface2[LFACE_HEIGHT_INDEX])
3961 && EQ (lface1[LFACE_SWIDTH_INDEX], lface2[LFACE_SWIDTH_INDEX])
3962 && EQ (lface1[LFACE_WEIGHT_INDEX], lface2[LFACE_WEIGHT_INDEX])
3963 && EQ (lface1[LFACE_SLANT_INDEX], lface2[LFACE_SLANT_INDEX])
3964 && EQ (lface1[LFACE_FONT_INDEX], lface2[LFACE_FONT_INDEX])
3965 && (EQ (lface1[LFACE_FONTSET_INDEX], lface2[LFACE_FONTSET_INDEX])
3966 || (STRINGP (lface1[LFACE_FONTSET_INDEX])
3967 && STRINGP (lface2[LFACE_FONTSET_INDEX])
3968 && ! xstrcasecmp (SSDATA (lface1[LFACE_FONTSET_INDEX]),
3969 SSDATA (lface2[LFACE_FONTSET_INDEX]))))
3973 #endif /* HAVE_WINDOW_SYSTEM */
3975 /***********************************************************************
3976 Realized Faces
3977 ***********************************************************************/
3979 /* Allocate and return a new realized face for Lisp face attribute
3980 vector ATTR. */
3982 static struct face *
3983 make_realized_face (Lisp_Object *attr)
3985 enum { off = offsetof (struct face, id) };
3986 struct face *face = xmalloc (sizeof *face);
3988 memcpy (face->lface, attr, sizeof face->lface);
3989 memset (&face->id, 0, sizeof *face - off);
3990 face->ascii_face = face;
3992 return face;
3996 /* Free realized face FACE, including its X resources. FACE may
3997 be null. */
3999 static void
4000 free_realized_face (struct frame *f, struct face *face)
4002 if (face)
4004 #ifdef HAVE_WINDOW_SYSTEM
4005 if (FRAME_WINDOW_P (f))
4007 /* Free fontset of FACE if it is ASCII face. */
4008 if (face->fontset >= 0 && face == face->ascii_face)
4009 free_face_fontset (f, face);
4010 if (face->gc)
4012 block_input ();
4013 if (face->font)
4014 font_done_for_face (f, face);
4015 x_free_gc (f, face->gc);
4016 face->gc = 0;
4017 unblock_input ();
4019 #ifdef HAVE_X_WINDOWS
4020 free_face_colors (f, face);
4021 #endif /* HAVE_X_WINDOWS */
4022 x_destroy_bitmap (f, face->stipple);
4024 #endif /* HAVE_WINDOW_SYSTEM */
4026 xfree (face);
4030 #ifdef HAVE_WINDOW_SYSTEM
4032 /* Prepare face FACE for subsequent display on frame F. This must be called
4033 before using X resources of FACE to allocate GCs if they haven't been
4034 allocated yet or have been freed by clearing the face cache. */
4036 void
4037 prepare_face_for_display (struct frame *f, struct face *face)
4039 eassert (FRAME_WINDOW_P (f));
4041 if (face->gc == 0)
4043 XGCValues xgcv;
4044 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
4046 xgcv.foreground = face->foreground;
4047 xgcv.background = face->background;
4048 #ifdef HAVE_X_WINDOWS
4049 xgcv.graphics_exposures = False;
4050 #endif
4052 block_input ();
4053 #ifdef HAVE_X_WINDOWS
4054 if (face->stipple)
4056 xgcv.fill_style = FillOpaqueStippled;
4057 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
4058 mask |= GCFillStyle | GCStipple;
4060 #endif
4061 face->gc = x_create_gc (f, mask, &xgcv);
4062 if (face->font)
4063 font_prepare_for_face (f, face);
4064 unblock_input ();
4068 #endif /* HAVE_WINDOW_SYSTEM */
4070 /* Returns the `distance' between the colors X and Y. */
4072 static int
4073 color_distance (XColor *x, XColor *y)
4075 /* This formula is from a paper titled `Colour metric' by Thiadmer Riemersma.
4076 Quoting from that paper:
4078 This formula has results that are very close to L*u*v* (with the
4079 modified lightness curve) and, more importantly, it is a more even
4080 algorithm: it does not have a range of colors where it suddenly
4081 gives far from optimal results.
4083 See <http://www.compuphase.com/cmetric.htm> for more info. */
4085 long r = (x->red - y->red) >> 8;
4086 long g = (x->green - y->green) >> 8;
4087 long b = (x->blue - y->blue) >> 8;
4088 long r_mean = (x->red + y->red) >> 9;
4090 return
4091 (((512 + r_mean) * r * r) >> 8)
4092 + 4 * g * g
4093 + (((767 - r_mean) * b * b) >> 8);
4097 DEFUN ("color-distance", Fcolor_distance, Scolor_distance, 2, 3, 0,
4098 doc: /* Return an integer distance between COLOR1 and COLOR2 on FRAME.
4099 COLOR1 and COLOR2 may be either strings containing the color name,
4100 or lists of the form (RED GREEN BLUE).
4101 If FRAME is unspecified or nil, the current frame is used. */)
4102 (Lisp_Object color1, Lisp_Object color2, Lisp_Object frame)
4104 struct frame *f = decode_live_frame (frame);
4105 XColor cdef1, cdef2;
4107 if (!(CONSP (color1) && parse_rgb_list (color1, &cdef1))
4108 && !(STRINGP (color1)
4109 && defined_color (f, SSDATA (color1), &cdef1, false)))
4110 signal_error ("Invalid color", color1);
4111 if (!(CONSP (color2) && parse_rgb_list (color2, &cdef2))
4112 && !(STRINGP (color2)
4113 && defined_color (f, SSDATA (color2), &cdef2, false)))
4114 signal_error ("Invalid color", color2);
4116 return make_number (color_distance (&cdef1, &cdef2));
4120 /***********************************************************************
4121 Face Cache
4122 ***********************************************************************/
4124 /* Return a new face cache for frame F. */
4126 static struct face_cache *
4127 make_face_cache (struct frame *f)
4129 struct face_cache *c = xmalloc (sizeof *c);
4131 c->buckets = xzalloc (FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets);
4132 c->size = 50;
4133 c->used = 0;
4134 c->faces_by_id = xmalloc (c->size * sizeof *c->faces_by_id);
4135 c->f = f;
4136 c->menu_face_changed_p = menu_face_changed_default;
4137 return c;
4140 #ifdef HAVE_WINDOW_SYSTEM
4142 /* Clear out all graphics contexts for all realized faces, except for
4143 the basic faces. This should be done from time to time just to avoid
4144 keeping too many graphics contexts that are no longer needed. */
4146 static void
4147 clear_face_gcs (struct face_cache *c)
4149 if (c && FRAME_WINDOW_P (c->f))
4151 int i;
4152 for (i = BASIC_FACE_ID_SENTINEL; i < c->used; ++i)
4154 struct face *face = c->faces_by_id[i];
4155 if (face && face->gc)
4157 block_input ();
4158 if (face->font)
4159 font_done_for_face (c->f, face);
4160 x_free_gc (c->f, face->gc);
4161 face->gc = 0;
4162 unblock_input ();
4168 #endif /* HAVE_WINDOW_SYSTEM */
4170 /* Free all realized faces in face cache C, including basic faces.
4171 C may be null. If faces are freed, make sure the frame's current
4172 matrix is marked invalid, so that a display caused by an expose
4173 event doesn't try to use faces we destroyed. */
4175 static void
4176 free_realized_faces (struct face_cache *c)
4178 if (c && c->used)
4180 int i, size;
4181 struct frame *f = c->f;
4183 /* We must block input here because we can't process X events
4184 safely while only some faces are freed, or when the frame's
4185 current matrix still references freed faces. */
4186 block_input ();
4188 for (i = 0; i < c->used; ++i)
4190 free_realized_face (f, c->faces_by_id[i]);
4191 c->faces_by_id[i] = NULL;
4194 /* Forget the escape-glyph and glyphless-char faces. */
4195 forget_escape_and_glyphless_faces ();
4196 c->used = 0;
4197 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4198 memset (c->buckets, 0, size);
4200 /* Must do a thorough redisplay the next time. Mark current
4201 matrices as invalid because they will reference faces freed
4202 above. This function is also called when a frame is
4203 destroyed. In this case, the root window of F is nil. */
4204 if (WINDOWP (f->root_window))
4206 clear_current_matrices (f);
4207 fset_redisplay (f);
4210 unblock_input ();
4215 /* Free all realized faces on FRAME or on all frames if FRAME is nil.
4216 This is done after attributes of a named face have been changed,
4217 because we can't tell which realized faces depend on that face. */
4219 void
4220 free_all_realized_faces (Lisp_Object frame)
4222 if (NILP (frame))
4224 Lisp_Object rest;
4225 FOR_EACH_FRAME (rest, frame)
4226 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4227 windows_or_buffers_changed = 58;
4229 else
4230 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4234 /* Free face cache C and faces in it, including their X resources. */
4236 static void
4237 free_face_cache (struct face_cache *c)
4239 if (c)
4241 free_realized_faces (c);
4242 xfree (c->buckets);
4243 xfree (c->faces_by_id);
4244 xfree (c);
4249 /* Cache realized face FACE in face cache C. HASH is the hash value
4250 of FACE. If FACE is for ASCII characters (i.e. FACE->ascii_face ==
4251 FACE), insert the new face to the beginning of the collision list
4252 of the face hash table of C. Otherwise, add the new face to the
4253 end of the collision list. This way, lookup_face can quickly find
4254 that a requested face is not cached. */
4256 static void
4257 cache_face (struct face_cache *c, struct face *face, unsigned int hash)
4259 int i = hash % FACE_CACHE_BUCKETS_SIZE;
4261 face->hash = hash;
4263 if (face->ascii_face != face)
4265 struct face *last = c->buckets[i];
4266 if (last)
4268 while (last->next)
4269 last = last->next;
4270 last->next = face;
4271 face->prev = last;
4272 face->next = NULL;
4274 else
4276 c->buckets[i] = face;
4277 face->prev = face->next = NULL;
4280 else
4282 face->prev = NULL;
4283 face->next = c->buckets[i];
4284 if (face->next)
4285 face->next->prev = face;
4286 c->buckets[i] = face;
4289 /* Find a free slot in C->faces_by_id and use the index of the free
4290 slot as FACE->id. */
4291 for (i = 0; i < c->used; ++i)
4292 if (c->faces_by_id[i] == NULL)
4293 break;
4294 face->id = i;
4296 #ifdef GLYPH_DEBUG
4297 /* Check that FACE got a unique id. */
4299 int j, n;
4300 struct face *face1;
4302 for (j = n = 0; j < FACE_CACHE_BUCKETS_SIZE; ++j)
4303 for (face1 = c->buckets[j]; face1; face1 = face1->next)
4304 if (face1->id == i)
4305 ++n;
4307 eassert (n == 1);
4309 #endif /* GLYPH_DEBUG */
4311 /* Maybe enlarge C->faces_by_id. */
4312 if (i == c->used)
4314 if (c->used == c->size)
4315 c->faces_by_id = xpalloc (c->faces_by_id, &c->size, 1, MAX_FACE_ID,
4316 sizeof *c->faces_by_id);
4317 c->used++;
4320 c->faces_by_id[i] = face;
4324 /* Remove face FACE from cache C. */
4326 static void
4327 uncache_face (struct face_cache *c, struct face *face)
4329 int i = face->hash % FACE_CACHE_BUCKETS_SIZE;
4331 if (face->prev)
4332 face->prev->next = face->next;
4333 else
4334 c->buckets[i] = face->next;
4336 if (face->next)
4337 face->next->prev = face->prev;
4339 c->faces_by_id[face->id] = NULL;
4340 if (face->id == c->used)
4341 --c->used;
4345 /* Look up a realized face with face attributes ATTR in the face cache
4346 of frame F. The face will be used to display ASCII characters.
4347 Value is the ID of the face found. If no suitable face is found,
4348 realize a new one. */
4350 static int
4351 lookup_face (struct frame *f, Lisp_Object *attr)
4353 struct face_cache *cache = FRAME_FACE_CACHE (f);
4354 unsigned hash;
4355 int i;
4356 struct face *face;
4358 eassert (cache != NULL);
4359 check_lface_attrs (attr);
4361 /* Look up ATTR in the face cache. */
4362 hash = lface_hash (attr);
4363 i = hash % FACE_CACHE_BUCKETS_SIZE;
4365 for (face = cache->buckets[i]; face; face = face->next)
4367 if (face->ascii_face != face)
4369 /* There's no more ASCII face. */
4370 face = NULL;
4371 break;
4373 if (face->hash == hash
4374 && lface_equal_p (face->lface, attr))
4375 break;
4378 /* If not found, realize a new face. */
4379 if (face == NULL)
4380 face = realize_face (cache, attr, -1);
4382 #ifdef GLYPH_DEBUG
4383 eassert (face == FACE_FROM_ID (f, face->id));
4384 #endif /* GLYPH_DEBUG */
4386 return face->id;
4389 #ifdef HAVE_WINDOW_SYSTEM
4390 /* Look up a realized face that has the same attributes as BASE_FACE
4391 except for the font in the face cache of frame F. If FONT-OBJECT
4392 is not nil, it is an already opened font. If FONT-OBJECT is nil,
4393 the face has no font. Value is the ID of the face found. If no
4394 suitable face is found, realize a new one. */
4397 face_for_font (struct frame *f, Lisp_Object font_object, struct face *base_face)
4399 struct face_cache *cache = FRAME_FACE_CACHE (f);
4400 unsigned hash;
4401 int i;
4402 struct face *face;
4404 eassert (cache != NULL);
4405 base_face = base_face->ascii_face;
4406 hash = lface_hash (base_face->lface);
4407 i = hash % FACE_CACHE_BUCKETS_SIZE;
4409 for (face = cache->buckets[i]; face; face = face->next)
4411 if (face->ascii_face == face)
4412 continue;
4413 if (face->ascii_face == base_face
4414 && face->font == (NILP (font_object) ? NULL
4415 : XFONT_OBJECT (font_object))
4416 && lface_equal_p (face->lface, base_face->lface))
4417 return face->id;
4420 /* If not found, realize a new face. */
4421 face = realize_non_ascii_face (f, font_object, base_face);
4422 return face->id;
4424 #endif /* HAVE_WINDOW_SYSTEM */
4426 /* Return the face id of the realized face for named face SYMBOL on
4427 frame F suitable for displaying ASCII characters. Value is -1 if
4428 the face couldn't be determined, which might happen if the default
4429 face isn't realized and cannot be realized. */
4432 lookup_named_face (struct frame *f, Lisp_Object symbol, bool signal_p)
4434 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4435 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4436 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4438 if (default_face == NULL)
4440 if (!realize_basic_faces (f))
4441 return -1;
4442 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4443 if (default_face == NULL)
4444 emacs_abort (); /* realize_basic_faces must have set it up */
4447 if (! get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4448 return -1;
4450 memcpy (attrs, default_face->lface, sizeof attrs);
4451 merge_face_vectors (f, symbol_attrs, attrs, 0);
4453 return lookup_face (f, attrs);
4457 /* Return the display face-id of the basic face whose canonical face-id
4458 is FACE_ID. The return value will usually simply be FACE_ID, unless that
4459 basic face has bee remapped via Vface_remapping_alist. This function is
4460 conservative: if something goes wrong, it will simply return FACE_ID
4461 rather than signal an error. */
4464 lookup_basic_face (struct frame *f, int face_id)
4466 Lisp_Object name, mapping;
4467 int remapped_face_id;
4469 if (NILP (Vface_remapping_alist))
4470 return face_id; /* Nothing to do. */
4472 switch (face_id)
4474 case DEFAULT_FACE_ID: name = Qdefault; break;
4475 case MODE_LINE_FACE_ID: name = Qmode_line; break;
4476 case MODE_LINE_INACTIVE_FACE_ID: name = Qmode_line_inactive; break;
4477 case HEADER_LINE_FACE_ID: name = Qheader_line; break;
4478 case TOOL_BAR_FACE_ID: name = Qtool_bar; break;
4479 case FRINGE_FACE_ID: name = Qfringe; break;
4480 case SCROLL_BAR_FACE_ID: name = Qscroll_bar; break;
4481 case BORDER_FACE_ID: name = Qborder; break;
4482 case CURSOR_FACE_ID: name = Qcursor; break;
4483 case MOUSE_FACE_ID: name = Qmouse; break;
4484 case MENU_FACE_ID: name = Qmenu; break;
4486 default:
4487 emacs_abort (); /* the caller is supposed to pass us a basic face id */
4490 /* Do a quick scan through Vface_remapping_alist, and return immediately
4491 if there is no remapping for face NAME. This is just an optimization
4492 for the very common no-remapping case. */
4493 mapping = assq_no_quit (name, Vface_remapping_alist);
4494 if (NILP (mapping))
4495 return face_id; /* Give up. */
4497 /* If there is a remapping entry, lookup the face using NAME, which will
4498 handle the remapping too. */
4499 remapped_face_id = lookup_named_face (f, name, false);
4500 if (remapped_face_id < 0)
4501 return face_id; /* Give up. */
4503 return remapped_face_id;
4507 /* Return a face for charset ASCII that is like the face with id
4508 FACE_ID on frame F, but has a font that is STEPS steps smaller.
4509 STEPS < 0 means larger. Value is the id of the face. */
4512 smaller_face (struct frame *f, int face_id, int steps)
4514 #ifdef HAVE_WINDOW_SYSTEM
4515 struct face *face;
4516 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4517 int pt, last_pt, last_height;
4518 int delta;
4519 int new_face_id;
4520 struct face *new_face;
4522 /* If not called for an X frame, just return the original face. */
4523 if (FRAME_TERMCAP_P (f))
4524 return face_id;
4526 /* Try in increments of 1/2 pt. */
4527 delta = steps < 0 ? 5 : -5;
4528 steps = eabs (steps);
4530 face = FACE_FROM_ID (f, face_id);
4531 memcpy (attrs, face->lface, sizeof attrs);
4532 pt = last_pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
4533 new_face_id = face_id;
4534 last_height = FONT_HEIGHT (face->font);
4536 while (steps
4537 && pt + delta > 0
4538 /* Give up if we cannot find a font within 10pt. */
4539 && eabs (last_pt - pt) < 100)
4541 /* Look up a face for a slightly smaller/larger font. */
4542 pt += delta;
4543 attrs[LFACE_HEIGHT_INDEX] = make_number (pt);
4544 new_face_id = lookup_face (f, attrs);
4545 new_face = FACE_FROM_ID (f, new_face_id);
4547 /* If height changes, count that as one step. */
4548 if ((delta < 0 && FONT_HEIGHT (new_face->font) < last_height)
4549 || (delta > 0 && FONT_HEIGHT (new_face->font) > last_height))
4551 --steps;
4552 last_height = FONT_HEIGHT (new_face->font);
4553 last_pt = pt;
4557 return new_face_id;
4559 #else /* not HAVE_WINDOW_SYSTEM */
4561 return face_id;
4563 #endif /* not HAVE_WINDOW_SYSTEM */
4567 /* Return a face for charset ASCII that is like the face with id
4568 FACE_ID on frame F, but has height HEIGHT. */
4571 face_with_height (struct frame *f, int face_id, int height)
4573 #ifdef HAVE_WINDOW_SYSTEM
4574 struct face *face;
4575 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4577 if (FRAME_TERMCAP_P (f)
4578 || height <= 0)
4579 return face_id;
4581 face = FACE_FROM_ID (f, face_id);
4582 memcpy (attrs, face->lface, sizeof attrs);
4583 attrs[LFACE_HEIGHT_INDEX] = make_number (height);
4584 font_clear_prop (attrs, FONT_SIZE_INDEX);
4585 face_id = lookup_face (f, attrs);
4586 #endif /* HAVE_WINDOW_SYSTEM */
4588 return face_id;
4592 /* Return the face id of the realized face for named face SYMBOL on
4593 frame F suitable for displaying ASCII characters, and use
4594 attributes of the face FACE_ID for attributes that aren't
4595 completely specified by SYMBOL. This is like lookup_named_face,
4596 except that the default attributes come from FACE_ID, not from the
4597 default face. FACE_ID is assumed to be already realized. */
4600 lookup_derived_face (struct frame *f, Lisp_Object symbol, int face_id,
4601 bool signal_p)
4603 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4604 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4605 struct face *default_face = FACE_FROM_ID (f, face_id);
4607 if (!default_face)
4608 emacs_abort ();
4610 if (!get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4611 return -1;
4613 memcpy (attrs, default_face->lface, sizeof attrs);
4614 merge_face_vectors (f, symbol_attrs, attrs, 0);
4615 return lookup_face (f, attrs);
4618 DEFUN ("face-attributes-as-vector", Fface_attributes_as_vector,
4619 Sface_attributes_as_vector, 1, 1, 0,
4620 doc: /* Return a vector of face attributes corresponding to PLIST. */)
4621 (Lisp_Object plist)
4623 Lisp_Object lface;
4624 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
4625 Qunspecified);
4626 merge_face_ref (XFRAME (selected_frame), plist, XVECTOR (lface)->contents,
4627 true, 0);
4628 return lface;
4633 /***********************************************************************
4634 Face capability testing
4635 ***********************************************************************/
4638 /* If the distance (as returned by color_distance) between two colors is
4639 less than this, then they are considered the same, for determining
4640 whether a color is supported or not. The range of values is 0-65535. */
4642 #define TTY_SAME_COLOR_THRESHOLD 10000
4644 #ifdef HAVE_WINDOW_SYSTEM
4646 /* Return true if all the face attributes in ATTRS are supported
4647 on the window-system frame F.
4649 The definition of `supported' is somewhat heuristic, but basically means
4650 that a face containing all the attributes in ATTRS, when merged with the
4651 default face for display, can be represented in a way that's
4653 (1) different in appearance than the default face, and
4654 (2) `close in spirit' to what the attributes specify, if not exact. */
4656 static bool
4657 x_supports_face_attributes_p (struct frame *f,
4658 Lisp_Object attrs[LFACE_VECTOR_SIZE],
4659 struct face *def_face)
4661 Lisp_Object *def_attrs = def_face->lface;
4663 /* Check that other specified attributes are different that the default
4664 face. */
4665 if ((!UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
4666 && face_attr_equal_p (attrs[LFACE_UNDERLINE_INDEX],
4667 def_attrs[LFACE_UNDERLINE_INDEX]))
4668 || (!UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
4669 && face_attr_equal_p (attrs[LFACE_INVERSE_INDEX],
4670 def_attrs[LFACE_INVERSE_INDEX]))
4671 || (!UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
4672 && face_attr_equal_p (attrs[LFACE_FOREGROUND_INDEX],
4673 def_attrs[LFACE_FOREGROUND_INDEX]))
4674 || (!UNSPECIFIEDP (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
4675 && face_attr_equal_p (attrs[LFACE_DISTANT_FOREGROUND_INDEX],
4676 def_attrs[LFACE_DISTANT_FOREGROUND_INDEX]))
4677 || (!UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
4678 && face_attr_equal_p (attrs[LFACE_BACKGROUND_INDEX],
4679 def_attrs[LFACE_BACKGROUND_INDEX]))
4680 || (!UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4681 && face_attr_equal_p (attrs[LFACE_STIPPLE_INDEX],
4682 def_attrs[LFACE_STIPPLE_INDEX]))
4683 || (!UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4684 && face_attr_equal_p (attrs[LFACE_OVERLINE_INDEX],
4685 def_attrs[LFACE_OVERLINE_INDEX]))
4686 || (!UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4687 && face_attr_equal_p (attrs[LFACE_STRIKE_THROUGH_INDEX],
4688 def_attrs[LFACE_STRIKE_THROUGH_INDEX]))
4689 || (!UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
4690 && face_attr_equal_p (attrs[LFACE_BOX_INDEX],
4691 def_attrs[LFACE_BOX_INDEX])))
4692 return false;
4694 /* Check font-related attributes, as those are the most commonly
4695 "unsupported" on a window-system (because of missing fonts). */
4696 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4697 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4698 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4699 || !UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
4700 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
4701 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX]))
4703 int face_id;
4704 struct face *face;
4705 Lisp_Object merged_attrs[LFACE_VECTOR_SIZE];
4706 int i;
4708 memcpy (merged_attrs, def_attrs, sizeof merged_attrs);
4710 merge_face_vectors (f, attrs, merged_attrs, 0);
4712 face_id = lookup_face (f, merged_attrs);
4713 face = FACE_FROM_ID (f, face_id);
4715 if (! face)
4716 error ("Cannot make face");
4718 /* If the font is the same, or no font is found, then not
4719 supported. */
4720 if (face->font == def_face->font
4721 || ! face->font)
4722 return false;
4723 for (i = FONT_TYPE_INDEX; i <= FONT_SIZE_INDEX; i++)
4724 if (! EQ (face->font->props[i], def_face->font->props[i]))
4726 Lisp_Object s1, s2;
4728 if (i < FONT_FOUNDRY_INDEX || i > FONT_REGISTRY_INDEX
4729 || face->font->driver->case_sensitive)
4730 return true;
4731 s1 = SYMBOL_NAME (face->font->props[i]);
4732 s2 = SYMBOL_NAME (def_face->font->props[i]);
4733 if (! EQ (Fcompare_strings (s1, make_number (0), Qnil,
4734 s2, make_number (0), Qnil, Qt), Qt))
4735 return true;
4737 return false;
4740 /* Everything checks out, this face is supported. */
4741 return true;
4744 #endif /* HAVE_WINDOW_SYSTEM */
4746 /* Return true if all the face attributes in ATTRS are supported
4747 on the tty frame F.
4749 The definition of `supported' is somewhat heuristic, but basically means
4750 that a face containing all the attributes in ATTRS, when merged
4751 with the default face for display, can be represented in a way that's
4753 (1) different in appearance than the default face, and
4754 (2) `close in spirit' to what the attributes specify, if not exact.
4756 Point (2) implies that a `:weight black' attribute will be satisfied
4757 by any terminal that can display bold, and a `:foreground "yellow"' as
4758 long as the terminal can display a yellowish color, but `:slant italic'
4759 will _not_ be satisfied by the tty display code's automatic
4760 substitution of a `dim' face for italic. */
4762 static bool
4763 tty_supports_face_attributes_p (struct frame *f,
4764 Lisp_Object attrs[LFACE_VECTOR_SIZE],
4765 struct face *def_face)
4767 int weight, slant;
4768 Lisp_Object val, fg, bg;
4769 XColor fg_tty_color, fg_std_color;
4770 XColor bg_tty_color, bg_std_color;
4771 unsigned test_caps = 0;
4772 Lisp_Object *def_attrs = def_face->lface;
4774 /* First check some easy-to-check stuff; ttys support none of the
4775 following attributes, so we can just return false if any are requested
4776 (even if `nominal' values are specified, we should still return false,
4777 as that will be the same value that the default face uses). We
4778 consider :slant unsupportable on ttys, even though the face code
4779 actually `fakes' them using a dim attribute if possible. This is
4780 because the faked result is too different from what the face
4781 specifies. */
4782 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4783 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4784 || !UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4785 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4786 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
4787 || !UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4788 || !UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4789 || !UNSPECIFIEDP (attrs[LFACE_BOX_INDEX]))
4790 return false;
4792 /* Test for terminal `capabilities' (non-color character attributes). */
4794 /* font weight (bold/dim) */
4795 val = attrs[LFACE_WEIGHT_INDEX];
4796 if (!UNSPECIFIEDP (val)
4797 && (weight = FONT_WEIGHT_NAME_NUMERIC (val), weight >= 0))
4799 int def_weight = FONT_WEIGHT_NAME_NUMERIC (def_attrs[LFACE_WEIGHT_INDEX]);
4801 if (weight > 100)
4803 if (def_weight > 100)
4804 return false; /* same as default */
4805 test_caps = TTY_CAP_BOLD;
4807 else if (weight < 100)
4809 if (def_weight < 100)
4810 return false; /* same as default */
4811 test_caps = TTY_CAP_DIM;
4813 else if (def_weight == 100)
4814 return false; /* same as default */
4817 /* font slant */
4818 val = attrs[LFACE_SLANT_INDEX];
4819 if (!UNSPECIFIEDP (val)
4820 && (slant = FONT_SLANT_NAME_NUMERIC (val), slant >= 0))
4822 int def_slant = FONT_SLANT_NAME_NUMERIC (def_attrs[LFACE_SLANT_INDEX]);
4823 if (slant == 100 || slant == def_slant)
4824 return false; /* same as default */
4825 else
4826 test_caps |= TTY_CAP_ITALIC;
4829 /* underlining */
4830 val = attrs[LFACE_UNDERLINE_INDEX];
4831 if (!UNSPECIFIEDP (val))
4833 if (STRINGP (val))
4834 return false; /* ttys can't use colored underlines */
4835 else if (EQ (CAR_SAFE (val), QCstyle) && EQ (CAR_SAFE (CDR_SAFE (val)), Qwave))
4836 return false; /* ttys can't use wave underlines */
4837 else if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
4838 return false; /* same as default */
4839 else
4840 test_caps |= TTY_CAP_UNDERLINE;
4843 /* inverse video */
4844 val = attrs[LFACE_INVERSE_INDEX];
4845 if (!UNSPECIFIEDP (val))
4847 if (face_attr_equal_p (val, def_attrs[LFACE_INVERSE_INDEX]))
4848 return false; /* same as default */
4849 else
4850 test_caps |= TTY_CAP_INVERSE;
4854 /* Color testing. */
4856 /* Check if foreground color is close enough. */
4857 fg = attrs[LFACE_FOREGROUND_INDEX];
4858 if (STRINGP (fg))
4860 Lisp_Object def_fg = def_attrs[LFACE_FOREGROUND_INDEX];
4862 if (face_attr_equal_p (fg, def_fg))
4863 return false; /* same as default */
4864 else if (! tty_lookup_color (f, fg, &fg_tty_color, &fg_std_color))
4865 return false; /* not a valid color */
4866 else if (color_distance (&fg_tty_color, &fg_std_color)
4867 > TTY_SAME_COLOR_THRESHOLD)
4868 return false; /* displayed color is too different */
4869 else
4870 /* Make sure the color is really different than the default. */
4872 XColor def_fg_color;
4873 if (tty_lookup_color (f, def_fg, &def_fg_color, 0)
4874 && (color_distance (&fg_tty_color, &def_fg_color)
4875 <= TTY_SAME_COLOR_THRESHOLD))
4876 return false;
4880 /* Check if background color is close enough. */
4881 bg = attrs[LFACE_BACKGROUND_INDEX];
4882 if (STRINGP (bg))
4884 Lisp_Object def_bg = def_attrs[LFACE_BACKGROUND_INDEX];
4886 if (face_attr_equal_p (bg, def_bg))
4887 return false; /* same as default */
4888 else if (! tty_lookup_color (f, bg, &bg_tty_color, &bg_std_color))
4889 return false; /* not a valid color */
4890 else if (color_distance (&bg_tty_color, &bg_std_color)
4891 > TTY_SAME_COLOR_THRESHOLD)
4892 return false; /* displayed color is too different */
4893 else
4894 /* Make sure the color is really different than the default. */
4896 XColor def_bg_color;
4897 if (tty_lookup_color (f, def_bg, &def_bg_color, 0)
4898 && (color_distance (&bg_tty_color, &def_bg_color)
4899 <= TTY_SAME_COLOR_THRESHOLD))
4900 return false;
4904 /* If both foreground and background are requested, see if the
4905 distance between them is OK. We just check to see if the distance
4906 between the tty's foreground and background is close enough to the
4907 distance between the standard foreground and background. */
4908 if (STRINGP (fg) && STRINGP (bg))
4910 int delta_delta
4911 = (color_distance (&fg_std_color, &bg_std_color)
4912 - color_distance (&fg_tty_color, &bg_tty_color));
4913 if (delta_delta > TTY_SAME_COLOR_THRESHOLD
4914 || delta_delta < -TTY_SAME_COLOR_THRESHOLD)
4915 return false;
4919 /* See if the capabilities we selected above are supported, with the
4920 given colors. */
4921 return tty_capable_p (FRAME_TTY (f), test_caps);
4925 DEFUN ("display-supports-face-attributes-p",
4926 Fdisplay_supports_face_attributes_p, Sdisplay_supports_face_attributes_p,
4927 1, 2, 0,
4928 doc: /* Return non-nil if all the face attributes in ATTRIBUTES are supported.
4929 The optional argument DISPLAY can be a display name, a frame, or
4930 nil (meaning the selected frame's display).
4932 The definition of `supported' is somewhat heuristic, but basically means
4933 that a face containing all the attributes in ATTRIBUTES, when merged
4934 with the default face for display, can be represented in a way that's
4936 (1) different in appearance than the default face, and
4937 (2) `close in spirit' to what the attributes specify, if not exact.
4939 Point (2) implies that a `:weight black' attribute will be satisfied by
4940 any display that can display bold, and a `:foreground \"yellow\"' as long
4941 as it can display a yellowish color, but `:slant italic' will _not_ be
4942 satisfied by the tty display code's automatic substitution of a `dim'
4943 face for italic. */)
4944 (Lisp_Object attributes, Lisp_Object display)
4946 bool supports = false;
4947 int i;
4948 Lisp_Object frame;
4949 struct frame *f;
4950 struct face *def_face;
4951 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4953 if (noninteractive || !initialized)
4954 /* We may not be able to access low-level face information in batch
4955 mode, or before being dumped, and this function is not going to
4956 be very useful in those cases anyway, so just give up. */
4957 return Qnil;
4959 if (NILP (display))
4960 frame = selected_frame;
4961 else if (FRAMEP (display))
4962 frame = display;
4963 else
4965 /* Find any frame on DISPLAY. */
4966 Lisp_Object tail;
4968 frame = Qnil;
4969 FOR_EACH_FRAME (tail, frame)
4970 if (!NILP (Fequal (Fcdr (Fassq (Qdisplay,
4971 XFRAME (frame)->param_alist)),
4972 display)))
4973 break;
4976 CHECK_LIVE_FRAME (frame);
4977 f = XFRAME (frame);
4979 for (i = 0; i < LFACE_VECTOR_SIZE; i++)
4980 attrs[i] = Qunspecified;
4981 merge_face_ref (f, attributes, attrs, true, 0);
4983 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4984 if (def_face == NULL)
4986 if (! realize_basic_faces (f))
4987 error ("Cannot realize default face");
4988 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4989 if (def_face == NULL)
4990 emacs_abort (); /* realize_basic_faces must have set it up */
4993 /* Dispatch to the appropriate handler. */
4994 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
4995 supports = tty_supports_face_attributes_p (f, attrs, def_face);
4996 #ifdef HAVE_WINDOW_SYSTEM
4997 else
4998 supports = x_supports_face_attributes_p (f, attrs, def_face);
4999 #endif
5001 return supports ? Qt : Qnil;
5005 /***********************************************************************
5006 Font selection
5007 ***********************************************************************/
5009 DEFUN ("internal-set-font-selection-order",
5010 Finternal_set_font_selection_order,
5011 Sinternal_set_font_selection_order, 1, 1, 0,
5012 doc: /* Set font selection order for face font selection to ORDER.
5013 ORDER must be a list of length 4 containing the symbols `:width',
5014 `:height', `:weight', and `:slant'. Face attributes appearing
5015 first in ORDER are matched first, e.g. if `:height' appears before
5016 `:weight' in ORDER, font selection first tries to find a font with
5017 a suitable height, and then tries to match the font weight.
5018 Value is ORDER. */)
5019 (Lisp_Object order)
5021 Lisp_Object list;
5022 int i;
5023 int indices[ARRAYELTS (font_sort_order)];
5025 CHECK_LIST (order);
5026 memset (indices, 0, sizeof indices);
5027 i = 0;
5029 for (list = order;
5030 CONSP (list) && i < ARRAYELTS (indices);
5031 list = XCDR (list), ++i)
5033 Lisp_Object attr = XCAR (list);
5034 int xlfd;
5036 if (EQ (attr, QCwidth))
5037 xlfd = XLFD_SWIDTH;
5038 else if (EQ (attr, QCheight))
5039 xlfd = XLFD_POINT_SIZE;
5040 else if (EQ (attr, QCweight))
5041 xlfd = XLFD_WEIGHT;
5042 else if (EQ (attr, QCslant))
5043 xlfd = XLFD_SLANT;
5044 else
5045 break;
5047 if (indices[i] != 0)
5048 break;
5049 indices[i] = xlfd;
5052 if (!NILP (list) || i != ARRAYELTS (indices))
5053 signal_error ("Invalid font sort order", order);
5054 for (i = 0; i < ARRAYELTS (font_sort_order); ++i)
5055 if (indices[i] == 0)
5056 signal_error ("Invalid font sort order", order);
5058 if (memcmp (indices, font_sort_order, sizeof indices) != 0)
5060 memcpy (font_sort_order, indices, sizeof font_sort_order);
5061 free_all_realized_faces (Qnil);
5064 font_update_sort_order (font_sort_order);
5066 return Qnil;
5070 DEFUN ("internal-set-alternative-font-family-alist",
5071 Finternal_set_alternative_font_family_alist,
5072 Sinternal_set_alternative_font_family_alist, 1, 1, 0,
5073 doc: /* Define alternative font families to try in face font selection.
5074 ALIST is an alist of (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5075 Each ALTERNATIVE is tried in order if no fonts of font family FAMILY can
5076 be found. Value is ALIST. */)
5077 (Lisp_Object alist)
5079 Lisp_Object entry, tail, tail2;
5081 CHECK_LIST (alist);
5082 alist = Fcopy_sequence (alist);
5083 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5085 entry = XCAR (tail);
5086 CHECK_LIST (entry);
5087 entry = Fcopy_sequence (entry);
5088 XSETCAR (tail, entry);
5089 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5090 XSETCAR (tail2, Fintern (XCAR (tail2), Qnil));
5093 Vface_alternative_font_family_alist = alist;
5094 free_all_realized_faces (Qnil);
5095 return alist;
5099 DEFUN ("internal-set-alternative-font-registry-alist",
5100 Finternal_set_alternative_font_registry_alist,
5101 Sinternal_set_alternative_font_registry_alist, 1, 1, 0,
5102 doc: /* Define alternative font registries to try in face font selection.
5103 ALIST is an alist of (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5104 Each ALTERNATIVE is tried in order if no fonts of font registry REGISTRY can
5105 be found. Value is ALIST. */)
5106 (Lisp_Object alist)
5108 Lisp_Object entry, tail, tail2;
5110 CHECK_LIST (alist);
5111 alist = Fcopy_sequence (alist);
5112 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5114 entry = XCAR (tail);
5115 CHECK_LIST (entry);
5116 entry = Fcopy_sequence (entry);
5117 XSETCAR (tail, entry);
5118 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5119 XSETCAR (tail2, Fdowncase (XCAR (tail2)));
5121 Vface_alternative_font_registry_alist = alist;
5122 free_all_realized_faces (Qnil);
5123 return alist;
5127 #ifdef HAVE_WINDOW_SYSTEM
5129 /* Return the fontset id of the base fontset name or alias name given
5130 by the fontset attribute of ATTRS. Value is -1 if the fontset
5131 attribute of ATTRS doesn't name a fontset. */
5133 static int
5134 face_fontset (Lisp_Object attrs[LFACE_VECTOR_SIZE])
5136 Lisp_Object name;
5138 name = attrs[LFACE_FONTSET_INDEX];
5139 if (!STRINGP (name))
5140 return -1;
5141 return fs_query_fontset (name, 0);
5144 #endif /* HAVE_WINDOW_SYSTEM */
5148 /***********************************************************************
5149 Face Realization
5150 ***********************************************************************/
5152 /* Realize basic faces on frame F. Value is zero if frame parameters
5153 of F don't contain enough information needed to realize the default
5154 face. */
5156 static bool
5157 realize_basic_faces (struct frame *f)
5159 bool success_p = false;
5161 /* Block input here so that we won't be surprised by an X expose
5162 event, for instance, without having the faces set up. */
5163 block_input ();
5165 if (realize_default_face (f))
5167 realize_named_face (f, Qmode_line, MODE_LINE_FACE_ID);
5168 realize_named_face (f, Qmode_line_inactive, MODE_LINE_INACTIVE_FACE_ID);
5169 realize_named_face (f, Qtool_bar, TOOL_BAR_FACE_ID);
5170 realize_named_face (f, Qfringe, FRINGE_FACE_ID);
5171 realize_named_face (f, Qheader_line, HEADER_LINE_FACE_ID);
5172 realize_named_face (f, Qscroll_bar, SCROLL_BAR_FACE_ID);
5173 realize_named_face (f, Qborder, BORDER_FACE_ID);
5174 realize_named_face (f, Qcursor, CURSOR_FACE_ID);
5175 realize_named_face (f, Qmouse, MOUSE_FACE_ID);
5176 realize_named_face (f, Qmenu, MENU_FACE_ID);
5177 realize_named_face (f, Qvertical_border, VERTICAL_BORDER_FACE_ID);
5178 realize_named_face (f, Qwindow_divider, WINDOW_DIVIDER_FACE_ID);
5179 realize_named_face (f, Qwindow_divider_first_pixel,
5180 WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
5181 realize_named_face (f, Qwindow_divider_last_pixel,
5182 WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
5184 /* Reflect changes in the `menu' face in menu bars. */
5185 if (FRAME_FACE_CACHE (f)->menu_face_changed_p)
5187 FRAME_FACE_CACHE (f)->menu_face_changed_p = false;
5188 #ifdef USE_X_TOOLKIT
5189 if (FRAME_WINDOW_P (f))
5190 x_update_menu_appearance (f);
5191 #endif
5194 success_p = true;
5197 unblock_input ();
5198 return success_p;
5202 /* Realize the default face on frame F. If the face is not fully
5203 specified, make it fully-specified. Attributes of the default face
5204 that are not explicitly specified are taken from frame parameters. */
5206 static bool
5207 realize_default_face (struct frame *f)
5209 struct face_cache *c = FRAME_FACE_CACHE (f);
5210 Lisp_Object lface;
5211 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5212 struct face *face;
5214 /* If the `default' face is not yet known, create it. */
5215 lface = lface_from_face_name (f, Qdefault, false);
5216 if (NILP (lface))
5218 Lisp_Object frame;
5219 XSETFRAME (frame, f);
5220 lface = Finternal_make_lisp_face (Qdefault, frame);
5223 #ifdef HAVE_WINDOW_SYSTEM
5224 if (FRAME_WINDOW_P (f))
5226 Lisp_Object font_object;
5228 XSETFONT (font_object, FRAME_FONT (f));
5229 set_lface_from_font (f, lface, font_object, f->default_face_done_p);
5230 ASET (lface, LFACE_FONTSET_INDEX, fontset_name (FRAME_FONTSET (f)));
5231 f->default_face_done_p = true;
5233 #endif /* HAVE_WINDOW_SYSTEM */
5235 if (!FRAME_WINDOW_P (f))
5237 ASET (lface, LFACE_FAMILY_INDEX, build_string ("default"));
5238 ASET (lface, LFACE_FOUNDRY_INDEX, LFACE_FAMILY (lface));
5239 ASET (lface, LFACE_SWIDTH_INDEX, Qnormal);
5240 ASET (lface, LFACE_HEIGHT_INDEX, make_number (1));
5241 if (UNSPECIFIEDP (LFACE_WEIGHT (lface)))
5242 ASET (lface, LFACE_WEIGHT_INDEX, Qnormal);
5243 if (UNSPECIFIEDP (LFACE_SLANT (lface)))
5244 ASET (lface, LFACE_SLANT_INDEX, Qnormal);
5245 if (UNSPECIFIEDP (LFACE_FONTSET (lface)))
5246 ASET (lface, LFACE_FONTSET_INDEX, Qnil);
5249 if (UNSPECIFIEDP (LFACE_UNDERLINE (lface)))
5250 ASET (lface, LFACE_UNDERLINE_INDEX, Qnil);
5252 if (UNSPECIFIEDP (LFACE_OVERLINE (lface)))
5253 ASET (lface, LFACE_OVERLINE_INDEX, Qnil);
5255 if (UNSPECIFIEDP (LFACE_STRIKE_THROUGH (lface)))
5256 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, Qnil);
5258 if (UNSPECIFIEDP (LFACE_BOX (lface)))
5259 ASET (lface, LFACE_BOX_INDEX, Qnil);
5261 if (UNSPECIFIEDP (LFACE_INVERSE (lface)))
5262 ASET (lface, LFACE_INVERSE_INDEX, Qnil);
5264 if (UNSPECIFIEDP (LFACE_FOREGROUND (lface)))
5266 /* This function is called so early that colors are not yet
5267 set in the frame parameter list. */
5268 Lisp_Object color = Fassq (Qforeground_color, f->param_alist);
5270 if (CONSP (color) && STRINGP (XCDR (color)))
5271 ASET (lface, LFACE_FOREGROUND_INDEX, XCDR (color));
5272 else if (FRAME_WINDOW_P (f))
5273 return false;
5274 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5275 ASET (lface, LFACE_FOREGROUND_INDEX, build_string (unspecified_fg));
5276 else
5277 emacs_abort ();
5280 if (UNSPECIFIEDP (LFACE_BACKGROUND (lface)))
5282 /* This function is called so early that colors are not yet
5283 set in the frame parameter list. */
5284 Lisp_Object color = Fassq (Qbackground_color, f->param_alist);
5285 if (CONSP (color) && STRINGP (XCDR (color)))
5286 ASET (lface, LFACE_BACKGROUND_INDEX, XCDR (color));
5287 else if (FRAME_WINDOW_P (f))
5288 return false;
5289 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5290 ASET (lface, LFACE_BACKGROUND_INDEX, build_string (unspecified_bg));
5291 else
5292 emacs_abort ();
5295 if (UNSPECIFIEDP (LFACE_STIPPLE (lface)))
5296 ASET (lface, LFACE_STIPPLE_INDEX, Qnil);
5298 /* Realize the face; it must be fully-specified now. */
5299 eassert (lface_fully_specified_p (XVECTOR (lface)->contents));
5300 check_lface (lface);
5301 memcpy (attrs, XVECTOR (lface)->contents, sizeof attrs);
5302 face = realize_face (c, attrs, DEFAULT_FACE_ID);
5304 #ifdef HAVE_WINDOW_SYSTEM
5305 #ifdef HAVE_X_WINDOWS
5306 if (FRAME_X_P (f) && face->font != FRAME_FONT (f))
5308 /* This can happen when making a frame on a display that does
5309 not support the default font. */
5310 if (!face->font)
5311 return false;
5313 /* Otherwise, the font specified for the frame was not
5314 acceptable as a font for the default face (perhaps because
5315 auto-scaled fonts are rejected), so we must adjust the frame
5316 font. */
5317 x_set_font (f, LFACE_FONT (lface), Qnil);
5319 #endif /* HAVE_X_WINDOWS */
5320 #endif /* HAVE_WINDOW_SYSTEM */
5321 return true;
5325 /* Realize basic faces other than the default face in face cache C.
5326 SYMBOL is the face name, ID is the face id the realized face must
5327 have. The default face must have been realized already. */
5329 static void
5330 realize_named_face (struct frame *f, Lisp_Object symbol, int id)
5332 struct face_cache *c = FRAME_FACE_CACHE (f);
5333 Lisp_Object lface = lface_from_face_name (f, symbol, false);
5334 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5335 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5337 /* The default face must exist and be fully specified. */
5338 get_lface_attributes_no_remap (f, Qdefault, attrs, true);
5339 check_lface_attrs (attrs);
5340 eassert (lface_fully_specified_p (attrs));
5342 /* If SYMBOL isn't know as a face, create it. */
5343 if (NILP (lface))
5345 Lisp_Object frame;
5346 XSETFRAME (frame, f);
5347 lface = Finternal_make_lisp_face (symbol, frame);
5350 /* Merge SYMBOL's face with the default face. */
5351 get_lface_attributes_no_remap (f, symbol, symbol_attrs, true);
5352 merge_face_vectors (f, symbol_attrs, attrs, 0);
5354 /* Realize the face. */
5355 realize_face (c, attrs, id);
5359 /* Realize the fully-specified face with attributes ATTRS in face
5360 cache CACHE for ASCII characters. If FORMER_FACE_ID is
5361 non-negative, it is an ID of face to remove before caching the new
5362 face. Value is a pointer to the newly created realized face. */
5364 static struct face *
5365 realize_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE],
5366 int former_face_id)
5368 struct face *face;
5370 /* LFACE must be fully specified. */
5371 eassert (cache != NULL);
5372 check_lface_attrs (attrs);
5374 if (former_face_id >= 0 && cache->used > former_face_id)
5376 /* Remove the former face. */
5377 struct face *former_face = cache->faces_by_id[former_face_id];
5378 uncache_face (cache, former_face);
5379 free_realized_face (cache->f, former_face);
5380 SET_FRAME_GARBAGED (cache->f);
5383 if (FRAME_WINDOW_P (cache->f))
5384 face = realize_x_face (cache, attrs);
5385 else if (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f))
5386 face = realize_tty_face (cache, attrs);
5387 else if (FRAME_INITIAL_P (cache->f))
5389 /* Create a dummy face. */
5390 face = make_realized_face (attrs);
5392 else
5393 emacs_abort ();
5395 /* Insert the new face. */
5396 cache_face (cache, face, lface_hash (attrs));
5397 return face;
5401 #ifdef HAVE_WINDOW_SYSTEM
5402 /* Realize the fully-specified face that uses FONT-OBJECT and has the
5403 same attributes as BASE_FACE except for the font on frame F.
5404 FONT-OBJECT may be nil, in which case, realized a face of
5405 no-font. */
5407 static struct face *
5408 realize_non_ascii_face (struct frame *f, Lisp_Object font_object,
5409 struct face *base_face)
5411 struct face_cache *cache = FRAME_FACE_CACHE (f);
5412 struct face *face;
5414 face = xmalloc (sizeof *face);
5415 *face = *base_face;
5416 face->gc = 0;
5417 face->overstrike
5418 = (! NILP (font_object)
5419 && FONT_WEIGHT_NAME_NUMERIC (face->lface[LFACE_WEIGHT_INDEX]) > 100
5420 && FONT_WEIGHT_NUMERIC (font_object) <= 100);
5422 /* Don't try to free the colors copied bitwise from BASE_FACE. */
5423 face->colors_copied_bitwise_p = true;
5424 face->font = NILP (font_object) ? NULL : XFONT_OBJECT (font_object);
5425 face->gc = 0;
5427 cache_face (cache, face, face->hash);
5429 return face;
5431 #endif /* HAVE_WINDOW_SYSTEM */
5434 /* Realize the fully-specified face with attributes ATTRS in face
5435 cache CACHE for ASCII characters. Do it for X frame CACHE->f. If
5436 the new face doesn't share font with the default face, a fontname
5437 is allocated from the heap and set in `font_name' of the new face,
5438 but it is not yet loaded here. Value is a pointer to the newly
5439 created realized face. */
5441 static struct face *
5442 realize_x_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE])
5444 struct face *face = NULL;
5445 #ifdef HAVE_WINDOW_SYSTEM
5446 struct face *default_face;
5447 struct frame *f;
5448 Lisp_Object stipple, underline, overline, strike_through, box;
5450 eassert (FRAME_WINDOW_P (cache->f));
5452 /* Allocate a new realized face. */
5453 face = make_realized_face (attrs);
5454 face->ascii_face = face;
5456 f = cache->f;
5458 /* Determine the font to use. Most of the time, the font will be
5459 the same as the font of the default face, so try that first. */
5460 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5461 if (default_face
5462 && lface_same_font_attributes_p (default_face->lface, attrs))
5464 face->font = default_face->font;
5465 face->fontset
5466 = make_fontset_for_ascii_face (f, default_face->fontset, face);
5468 else
5470 /* If the face attribute ATTRS specifies a fontset, use it as
5471 the base of a new realized fontset. Otherwise, use the same
5472 base fontset as of the default face. The base determines
5473 registry and encoding of a font. It may also determine
5474 foundry and family. The other fields of font name pattern
5475 are constructed from ATTRS. */
5476 int fontset = face_fontset (attrs);
5478 /* If we are realizing the default face, ATTRS should specify a
5479 fontset. In other words, if FONTSET is -1, we are not
5480 realizing the default face, thus the default face should have
5481 already been realized. */
5482 if (fontset == -1)
5484 if (default_face)
5485 fontset = default_face->fontset;
5486 if (fontset == -1)
5487 emacs_abort ();
5489 if (! FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5490 attrs[LFACE_FONT_INDEX]
5491 = font_load_for_lface (f, attrs, Ffont_spec (0, NULL));
5492 if (FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5494 face->font = XFONT_OBJECT (attrs[LFACE_FONT_INDEX]);
5495 face->fontset = make_fontset_for_ascii_face (f, fontset, face);
5497 else
5499 face->font = NULL;
5500 face->fontset = -1;
5504 if (face->font
5505 && FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]) > 100
5506 && FONT_WEIGHT_NUMERIC (attrs[LFACE_FONT_INDEX]) <= 100)
5507 face->overstrike = true;
5509 /* Load colors, and set remaining attributes. */
5511 load_face_colors (f, face, attrs);
5513 /* Set up box. */
5514 box = attrs[LFACE_BOX_INDEX];
5515 if (STRINGP (box))
5517 /* A simple box of line width 1 drawn in color given by
5518 the string. */
5519 face->box_color = load_color (f, face, attrs[LFACE_BOX_INDEX],
5520 LFACE_BOX_INDEX);
5521 face->box = FACE_SIMPLE_BOX;
5522 face->box_line_width = 1;
5524 else if (INTEGERP (box))
5526 /* Simple box of specified line width in foreground color of the
5527 face. */
5528 eassert (XINT (box) != 0);
5529 face->box = FACE_SIMPLE_BOX;
5530 face->box_line_width = XINT (box);
5531 face->box_color = face->foreground;
5532 face->box_color_defaulted_p = true;
5534 else if (CONSP (box))
5536 /* `(:width WIDTH :color COLOR :shadow SHADOW)'. SHADOW
5537 being one of `raised' or `sunken'. */
5538 face->box = FACE_SIMPLE_BOX;
5539 face->box_color = face->foreground;
5540 face->box_color_defaulted_p = true;
5541 face->box_line_width = 1;
5543 while (CONSP (box))
5545 Lisp_Object keyword, value;
5547 keyword = XCAR (box);
5548 box = XCDR (box);
5550 if (!CONSP (box))
5551 break;
5552 value = XCAR (box);
5553 box = XCDR (box);
5555 if (EQ (keyword, QCline_width))
5557 if (INTEGERP (value) && XINT (value) != 0)
5558 face->box_line_width = XINT (value);
5560 else if (EQ (keyword, QCcolor))
5562 if (STRINGP (value))
5564 face->box_color = load_color (f, face, value,
5565 LFACE_BOX_INDEX);
5566 face->use_box_color_for_shadows_p = true;
5569 else if (EQ (keyword, QCstyle))
5571 if (EQ (value, Qreleased_button))
5572 face->box = FACE_RAISED_BOX;
5573 else if (EQ (value, Qpressed_button))
5574 face->box = FACE_SUNKEN_BOX;
5579 /* Text underline, overline, strike-through. */
5581 underline = attrs[LFACE_UNDERLINE_INDEX];
5582 if (EQ (underline, Qt))
5584 /* Use default color (same as foreground color). */
5585 face->underline_p = true;
5586 face->underline_type = FACE_UNDER_LINE;
5587 face->underline_defaulted_p = true;
5588 face->underline_color = 0;
5590 else if (STRINGP (underline))
5592 /* Use specified color. */
5593 face->underline_p = true;
5594 face->underline_type = FACE_UNDER_LINE;
5595 face->underline_defaulted_p = false;
5596 face->underline_color
5597 = load_color (f, face, underline,
5598 LFACE_UNDERLINE_INDEX);
5600 else if (NILP (underline))
5602 face->underline_p = false;
5603 face->underline_defaulted_p = false;
5604 face->underline_color = 0;
5606 else if (CONSP (underline))
5608 /* `(:color COLOR :style STYLE)'.
5609 STYLE being one of `line' or `wave'. */
5610 face->underline_p = true;
5611 face->underline_color = 0;
5612 face->underline_defaulted_p = true;
5613 face->underline_type = FACE_UNDER_LINE;
5615 /* FIXME? This is also not robust about checking the precise form.
5616 See comments in Finternal_set_lisp_face_attribute. */
5617 while (CONSP (underline))
5619 Lisp_Object keyword, value;
5621 keyword = XCAR (underline);
5622 underline = XCDR (underline);
5624 if (!CONSP (underline))
5625 break;
5626 value = XCAR (underline);
5627 underline = XCDR (underline);
5629 if (EQ (keyword, QCcolor))
5631 if (EQ (value, Qforeground_color))
5633 face->underline_defaulted_p = true;
5634 face->underline_color = 0;
5636 else if (STRINGP (value))
5638 face->underline_defaulted_p = false;
5639 face->underline_color = load_color (f, face, value,
5640 LFACE_UNDERLINE_INDEX);
5643 else if (EQ (keyword, QCstyle))
5645 if (EQ (value, Qline))
5646 face->underline_type = FACE_UNDER_LINE;
5647 else if (EQ (value, Qwave))
5648 face->underline_type = FACE_UNDER_WAVE;
5653 overline = attrs[LFACE_OVERLINE_INDEX];
5654 if (STRINGP (overline))
5656 face->overline_color
5657 = load_color (f, face, attrs[LFACE_OVERLINE_INDEX],
5658 LFACE_OVERLINE_INDEX);
5659 face->overline_p = true;
5661 else if (EQ (overline, Qt))
5663 face->overline_color = face->foreground;
5664 face->overline_color_defaulted_p = true;
5665 face->overline_p = true;
5668 strike_through = attrs[LFACE_STRIKE_THROUGH_INDEX];
5669 if (STRINGP (strike_through))
5671 face->strike_through_color
5672 = load_color (f, face, attrs[LFACE_STRIKE_THROUGH_INDEX],
5673 LFACE_STRIKE_THROUGH_INDEX);
5674 face->strike_through_p = true;
5676 else if (EQ (strike_through, Qt))
5678 face->strike_through_color = face->foreground;
5679 face->strike_through_color_defaulted_p = true;
5680 face->strike_through_p = true;
5683 stipple = attrs[LFACE_STIPPLE_INDEX];
5684 if (!NILP (stipple))
5685 face->stipple = load_pixmap (f, stipple);
5686 #endif /* HAVE_WINDOW_SYSTEM */
5688 return face;
5692 /* Map a specified color of face FACE on frame F to a tty color index.
5693 IDX is either LFACE_FOREGROUND_INDEX or LFACE_BACKGROUND_INDEX, and
5694 specifies which color to map. Set *DEFAULTED to true if mapping to the
5695 default foreground/background colors. */
5697 static void
5698 map_tty_color (struct frame *f, struct face *face,
5699 enum lface_attribute_index idx, bool *defaulted)
5701 Lisp_Object frame, color, def;
5702 bool foreground_p = idx == LFACE_FOREGROUND_INDEX;
5703 unsigned long default_pixel =
5704 foreground_p ? FACE_TTY_DEFAULT_FG_COLOR : FACE_TTY_DEFAULT_BG_COLOR;
5705 unsigned long pixel = default_pixel;
5706 #ifdef MSDOS
5707 unsigned long default_other_pixel =
5708 foreground_p ? FACE_TTY_DEFAULT_BG_COLOR : FACE_TTY_DEFAULT_FG_COLOR;
5709 #endif
5711 eassert (idx == LFACE_FOREGROUND_INDEX || idx == LFACE_BACKGROUND_INDEX);
5713 XSETFRAME (frame, f);
5714 color = face->lface[idx];
5716 if (STRINGP (color)
5717 && SCHARS (color)
5718 && CONSP (Vtty_defined_color_alist)
5719 && (def = assoc_no_quit (color, call1 (Qtty_color_alist, frame)),
5720 CONSP (def)))
5722 /* Associations in tty-defined-color-alist are of the form
5723 (NAME INDEX R G B). We need the INDEX part. */
5724 pixel = XINT (XCAR (XCDR (def)));
5727 if (pixel == default_pixel && STRINGP (color))
5729 pixel = load_color (f, face, color, idx);
5731 #ifdef MSDOS
5732 /* If the foreground of the default face is the default color,
5733 use the foreground color defined by the frame. */
5734 if (FRAME_MSDOS_P (f))
5736 if (pixel == default_pixel
5737 || pixel == FACE_TTY_DEFAULT_COLOR)
5739 if (foreground_p)
5740 pixel = FRAME_FOREGROUND_PIXEL (f);
5741 else
5742 pixel = FRAME_BACKGROUND_PIXEL (f);
5743 face->lface[idx] = tty_color_name (f, pixel);
5744 *defaulted = true;
5746 else if (pixel == default_other_pixel)
5748 if (foreground_p)
5749 pixel = FRAME_BACKGROUND_PIXEL (f);
5750 else
5751 pixel = FRAME_FOREGROUND_PIXEL (f);
5752 face->lface[idx] = tty_color_name (f, pixel);
5753 *defaulted = true;
5756 #endif /* MSDOS */
5759 if (foreground_p)
5760 face->foreground = pixel;
5761 else
5762 face->background = pixel;
5766 /* Realize the fully-specified face with attributes ATTRS in face
5767 cache CACHE for ASCII characters. Do it for TTY frame CACHE->f.
5768 Value is a pointer to the newly created realized face. */
5770 static struct face *
5771 realize_tty_face (struct face_cache *cache,
5772 Lisp_Object attrs[LFACE_VECTOR_SIZE])
5774 struct face *face;
5775 int weight, slant;
5776 bool face_colors_defaulted = false;
5777 struct frame *f = cache->f;
5779 /* Frame must be a termcap frame. */
5780 eassert (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f));
5782 /* Allocate a new realized face. */
5783 face = make_realized_face (attrs);
5784 #if false
5785 face->font_name = FRAME_MSDOS_P (cache->f) ? "ms-dos" : "tty";
5786 #endif
5788 /* Map face attributes to TTY appearances. */
5789 weight = FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]);
5790 slant = FONT_SLANT_NAME_NUMERIC (attrs[LFACE_SLANT_INDEX]);
5791 if (weight > 100)
5792 face->tty_bold_p = true;
5793 if (slant != 100)
5794 face->tty_italic_p = true;
5795 if (!NILP (attrs[LFACE_UNDERLINE_INDEX]))
5796 face->tty_underline_p = true;
5797 if (!NILP (attrs[LFACE_INVERSE_INDEX]))
5798 face->tty_reverse_p = true;
5800 /* Map color names to color indices. */
5801 map_tty_color (f, face, LFACE_FOREGROUND_INDEX, &face_colors_defaulted);
5802 map_tty_color (f, face, LFACE_BACKGROUND_INDEX, &face_colors_defaulted);
5804 /* Swap colors if face is inverse-video. If the colors are taken
5805 from the frame colors, they are already inverted, since the
5806 frame-creation function calls x-handle-reverse-video. */
5807 if (face->tty_reverse_p && !face_colors_defaulted)
5809 unsigned long tem = face->foreground;
5810 face->foreground = face->background;
5811 face->background = tem;
5814 if (tty_suppress_bold_inverse_default_colors_p
5815 && face->tty_bold_p
5816 && face->background == FACE_TTY_DEFAULT_FG_COLOR
5817 && face->foreground == FACE_TTY_DEFAULT_BG_COLOR)
5818 face->tty_bold_p = false;
5820 return face;
5824 DEFUN ("tty-suppress-bold-inverse-default-colors",
5825 Ftty_suppress_bold_inverse_default_colors,
5826 Stty_suppress_bold_inverse_default_colors, 1, 1, 0,
5827 doc: /* Suppress/allow boldness of faces with inverse default colors.
5828 SUPPRESS non-nil means suppress it.
5829 This affects bold faces on TTYs whose foreground is the default background
5830 color of the display and whose background is the default foreground color.
5831 For such faces, the bold face attribute is ignored if this variable
5832 is non-nil. */)
5833 (Lisp_Object suppress)
5835 tty_suppress_bold_inverse_default_colors_p = !NILP (suppress);
5836 face_change = true;
5837 return suppress;
5842 /***********************************************************************
5843 Computing Faces
5844 ***********************************************************************/
5846 /* Return the ID of the face to use to display character CH with face
5847 property PROP on frame F in current_buffer. */
5850 compute_char_face (struct frame *f, int ch, Lisp_Object prop)
5852 int face_id;
5854 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
5855 ch = 0;
5857 if (NILP (prop))
5859 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5860 face_id = FACE_FOR_CHAR (f, face, ch, -1, Qnil);
5862 else
5864 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5865 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5866 memcpy (attrs, default_face->lface, sizeof attrs);
5867 merge_face_ref (f, prop, attrs, true, 0);
5868 face_id = lookup_face (f, attrs);
5871 return face_id;
5874 /* Return the face ID associated with buffer position POS for
5875 displaying ASCII characters. Return in *ENDPTR the position at
5876 which a different face is needed, as far as text properties and
5877 overlays are concerned. W is a window displaying current_buffer.
5879 REGION_BEG, REGION_END delimit the region, so it can be
5880 highlighted.
5882 LIMIT is a position not to scan beyond. That is to limit the time
5883 this function can take.
5885 If MOUSE, use the character's mouse-face, not its face.
5887 BASE_FACE_ID, if non-negative, specifies a base face id to use
5888 instead of DEFAULT_FACE_ID.
5890 The face returned is suitable for displaying ASCII characters. */
5893 face_at_buffer_position (struct window *w, ptrdiff_t pos,
5894 ptrdiff_t *endptr, ptrdiff_t limit,
5895 bool mouse, int base_face_id)
5897 struct frame *f = XFRAME (w->frame);
5898 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5899 Lisp_Object prop, position;
5900 ptrdiff_t i, noverlays;
5901 Lisp_Object *overlay_vec;
5902 ptrdiff_t endpos;
5903 Lisp_Object propname = mouse ? Qmouse_face : Qface;
5904 Lisp_Object limit1, end;
5905 struct face *default_face;
5907 /* W must display the current buffer. We could write this function
5908 to use the frame and buffer of W, but right now it doesn't. */
5909 /* eassert (XBUFFER (w->contents) == current_buffer); */
5911 XSETFASTINT (position, pos);
5913 endpos = ZV;
5915 /* Get the `face' or `mouse_face' text property at POS, and
5916 determine the next position at which the property changes. */
5917 prop = Fget_text_property (position, propname, w->contents);
5918 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
5919 end = Fnext_single_property_change (position, propname, w->contents, limit1);
5920 if (INTEGERP (end))
5921 endpos = XINT (end);
5923 /* Look at properties from overlays. */
5924 USE_SAFE_ALLOCA;
5926 ptrdiff_t next_overlay;
5928 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, &next_overlay, false);
5929 if (next_overlay < endpos)
5930 endpos = next_overlay;
5933 *endptr = endpos;
5936 int face_id;
5938 if (base_face_id >= 0)
5939 face_id = base_face_id;
5940 else if (NILP (Vface_remapping_alist))
5941 face_id = DEFAULT_FACE_ID;
5942 else
5943 face_id = lookup_basic_face (f, DEFAULT_FACE_ID);
5945 default_face = FACE_FROM_ID (f, face_id);
5948 /* Optimize common cases where we can use the default face. */
5949 if (noverlays == 0
5950 && NILP (prop))
5952 SAFE_FREE ();
5953 return default_face->id;
5956 /* Begin with attributes from the default face. */
5957 memcpy (attrs, default_face->lface, sizeof attrs);
5959 /* Merge in attributes specified via text properties. */
5960 if (!NILP (prop))
5961 merge_face_ref (f, prop, attrs, true, 0);
5963 /* Now merge the overlay data. */
5964 noverlays = sort_overlays (overlay_vec, noverlays, w);
5965 for (i = 0; i < noverlays; i++)
5967 Lisp_Object oend;
5968 ptrdiff_t oendpos;
5970 prop = Foverlay_get (overlay_vec[i], propname);
5971 if (!NILP (prop))
5972 merge_face_ref (f, prop, attrs, true, 0);
5974 oend = OVERLAY_END (overlay_vec[i]);
5975 oendpos = OVERLAY_POSITION (oend);
5976 if (oendpos < endpos)
5977 endpos = oendpos;
5980 *endptr = endpos;
5982 SAFE_FREE ();
5984 /* Look up a realized face with the given face attributes,
5985 or realize a new one for ASCII characters. */
5986 return lookup_face (f, attrs);
5989 /* Return the face ID at buffer position POS for displaying ASCII
5990 characters associated with overlay strings for overlay OVERLAY.
5992 Like face_at_buffer_position except for OVERLAY. Currently it
5993 simply disregards the `face' properties of all overlays. */
5996 face_for_overlay_string (struct window *w, ptrdiff_t pos,
5997 ptrdiff_t *endptr, ptrdiff_t limit,
5998 bool mouse, Lisp_Object overlay)
6000 struct frame *f = XFRAME (w->frame);
6001 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6002 Lisp_Object prop, position;
6003 ptrdiff_t endpos;
6004 Lisp_Object propname = mouse ? Qmouse_face : Qface;
6005 Lisp_Object limit1, end;
6006 struct face *default_face;
6008 /* W must display the current buffer. We could write this function
6009 to use the frame and buffer of W, but right now it doesn't. */
6010 /* eassert (XBUFFER (w->contents) == current_buffer); */
6012 XSETFASTINT (position, pos);
6014 endpos = ZV;
6016 /* Get the `face' or `mouse_face' text property at POS, and
6017 determine the next position at which the property changes. */
6018 prop = Fget_text_property (position, propname, w->contents);
6019 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
6020 end = Fnext_single_property_change (position, propname, w->contents, limit1);
6021 if (INTEGERP (end))
6022 endpos = XINT (end);
6024 *endptr = endpos;
6026 /* Optimize common case where we can use the default face. */
6027 if (NILP (prop)
6028 && NILP (Vface_remapping_alist))
6029 return DEFAULT_FACE_ID;
6031 /* Begin with attributes from the default face. */
6032 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
6033 memcpy (attrs, default_face->lface, sizeof attrs);
6035 /* Merge in attributes specified via text properties. */
6036 if (!NILP (prop))
6037 merge_face_ref (f, prop, attrs, true, 0);
6039 *endptr = endpos;
6041 /* Look up a realized face with the given face attributes,
6042 or realize a new one for ASCII characters. */
6043 return lookup_face (f, attrs);
6047 /* Compute the face at character position POS in Lisp string STRING on
6048 window W, for ASCII characters.
6050 If STRING is an overlay string, it comes from position BUFPOS in
6051 current_buffer, otherwise BUFPOS is zero to indicate that STRING is
6052 not an overlay string. W must display the current buffer.
6053 REGION_BEG and REGION_END give the start and end positions of the
6054 region; both are -1 if no region is visible.
6056 BASE_FACE_ID is the id of a face to merge with. For strings coming
6057 from overlays or the `display' property it is the face at BUFPOS.
6059 If MOUSE_P, use the character's mouse-face, not its face.
6061 Set *ENDPTR to the next position where to check for faces in
6062 STRING; -1 if the face is constant from POS to the end of the
6063 string.
6065 Value is the id of the face to use. The face returned is suitable
6066 for displaying ASCII characters. */
6069 face_at_string_position (struct window *w, Lisp_Object string,
6070 ptrdiff_t pos, ptrdiff_t bufpos,
6071 ptrdiff_t *endptr, enum face_id base_face_id,
6072 bool mouse_p)
6074 Lisp_Object prop, position, end, limit;
6075 struct frame *f = XFRAME (WINDOW_FRAME (w));
6076 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6077 struct face *base_face;
6078 bool multibyte_p = STRING_MULTIBYTE (string);
6079 Lisp_Object prop_name = mouse_p ? Qmouse_face : Qface;
6081 /* Get the value of the face property at the current position within
6082 STRING. Value is nil if there is no face property. */
6083 XSETFASTINT (position, pos);
6084 prop = Fget_text_property (position, prop_name, string);
6086 /* Get the next position at which to check for faces. Value of end
6087 is nil if face is constant all the way to the end of the string.
6088 Otherwise it is a string position where to check faces next.
6089 Limit is the maximum position up to which to check for property
6090 changes in Fnext_single_property_change. Strings are usually
6091 short, so set the limit to the end of the string. */
6092 XSETFASTINT (limit, SCHARS (string));
6093 end = Fnext_single_property_change (position, prop_name, string, limit);
6094 if (INTEGERP (end))
6095 *endptr = XFASTINT (end);
6096 else
6097 *endptr = -1;
6099 base_face = FACE_FROM_ID (f, base_face_id);
6100 eassert (base_face);
6102 /* Optimize the default case that there is no face property. */
6103 if (NILP (prop)
6104 && (multibyte_p
6105 /* We can't realize faces for different charsets differently
6106 if we don't have fonts, so we can stop here if not working
6107 on a window-system frame. */
6108 || !FRAME_WINDOW_P (f)
6109 || FACE_SUITABLE_FOR_ASCII_CHAR_P (base_face, 0)))
6110 return base_face->id;
6112 /* Begin with attributes from the base face. */
6113 memcpy (attrs, base_face->lface, sizeof attrs);
6115 /* Merge in attributes specified via text properties. */
6116 if (!NILP (prop))
6117 merge_face_ref (f, prop, attrs, true, 0);
6119 /* Look up a realized face with the given face attributes,
6120 or realize a new one for ASCII characters. */
6121 return lookup_face (f, attrs);
6125 /* Merge a face into a realized face.
6127 F is frame where faces are (to be) realized.
6129 FACE_NAME is named face to merge.
6131 If FACE_NAME is nil, FACE_ID is face_id of realized face to merge.
6133 If FACE_NAME is t, FACE_ID is lface_id of face to merge.
6135 BASE_FACE_ID is realized face to merge into.
6137 Return new face id.
6141 merge_faces (struct frame *f, Lisp_Object face_name, int face_id,
6142 int base_face_id)
6144 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6145 struct face *base_face;
6147 base_face = FACE_FROM_ID (f, base_face_id);
6148 if (!base_face)
6149 return base_face_id;
6151 if (EQ (face_name, Qt))
6153 if (face_id < 0 || face_id >= lface_id_to_name_size)
6154 return base_face_id;
6155 face_name = lface_id_to_name[face_id];
6156 /* When called during make-frame, lookup_derived_face may fail
6157 if the faces are uninitialized. Don't signal an error. */
6158 face_id = lookup_derived_face (f, face_name, base_face_id, 0);
6159 return (face_id >= 0 ? face_id : base_face_id);
6162 /* Begin with attributes from the base face. */
6163 memcpy (attrs, base_face->lface, sizeof attrs);
6165 if (!NILP (face_name))
6167 if (!merge_named_face (f, face_name, attrs, 0))
6168 return base_face_id;
6170 else
6172 struct face *face;
6173 if (face_id < 0)
6174 return base_face_id;
6175 face = FACE_FROM_ID (f, face_id);
6176 if (!face)
6177 return base_face_id;
6178 merge_face_vectors (f, face->lface, attrs, 0);
6181 /* Look up a realized face with the given face attributes,
6182 or realize a new one for ASCII characters. */
6183 return lookup_face (f, attrs);
6188 #ifndef HAVE_X_WINDOWS
6189 DEFUN ("x-load-color-file", Fx_load_color_file,
6190 Sx_load_color_file, 1, 1, 0,
6191 doc: /* Create an alist of color entries from an external file.
6193 The file should define one named RGB color per line like so:
6194 R G B name
6195 where R,G,B are numbers between 0 and 255 and name is an arbitrary string. */)
6196 (Lisp_Object filename)
6198 FILE *fp;
6199 Lisp_Object cmap = Qnil;
6200 Lisp_Object abspath;
6202 CHECK_STRING (filename);
6203 abspath = Fexpand_file_name (filename, Qnil);
6205 block_input ();
6206 fp = emacs_fopen (SSDATA (abspath), "r" FOPEN_TEXT);
6207 if (fp)
6209 char buf[512];
6210 int red, green, blue;
6211 int num;
6213 while (fgets (buf, sizeof (buf), fp) != NULL) {
6214 if (sscanf (buf, "%u %u %u %n", &red, &green, &blue, &num) == 3)
6216 #ifdef HAVE_NTGUI
6217 int color = RGB (red, green, blue);
6218 #else
6219 int color = (red << 16) | (green << 8) | blue;
6220 #endif
6221 char *name = buf + num;
6222 ptrdiff_t len = strlen (name);
6223 len -= 0 < len && name[len - 1] == '\n';
6224 cmap = Fcons (Fcons (make_string (name, len), make_number (color)),
6225 cmap);
6228 fclose (fp);
6230 unblock_input ();
6231 return cmap;
6233 #endif
6236 /***********************************************************************
6237 Tests
6238 ***********************************************************************/
6240 #ifdef GLYPH_DEBUG
6242 /* Print the contents of the realized face FACE to stderr. */
6244 static void
6245 dump_realized_face (struct face *face)
6247 fprintf (stderr, "ID: %d\n", face->id);
6248 #ifdef HAVE_X_WINDOWS
6249 fprintf (stderr, "gc: %p\n", face->gc);
6250 #endif
6251 fprintf (stderr, "foreground: 0x%lx (%s)\n",
6252 face->foreground,
6253 SDATA (face->lface[LFACE_FOREGROUND_INDEX]));
6254 fprintf (stderr, "background: 0x%lx (%s)\n",
6255 face->background,
6256 SDATA (face->lface[LFACE_BACKGROUND_INDEX]));
6257 if (face->font)
6258 fprintf (stderr, "font_name: %s (%s)\n",
6259 SDATA (face->font->props[FONT_NAME_INDEX]),
6260 SDATA (face->lface[LFACE_FAMILY_INDEX]));
6261 #ifdef HAVE_X_WINDOWS
6262 fprintf (stderr, "font = %p\n", face->font);
6263 #endif
6264 fprintf (stderr, "fontset: %d\n", face->fontset);
6265 fprintf (stderr, "underline: %d (%s)\n",
6266 face->underline_p,
6267 SDATA (Fsymbol_name (face->lface[LFACE_UNDERLINE_INDEX])));
6268 fprintf (stderr, "hash: %d\n", face->hash);
6272 DEFUN ("dump-face", Fdump_face, Sdump_face, 0, 1, 0, doc: /* */)
6273 (Lisp_Object n)
6275 if (NILP (n))
6277 int i;
6279 fprintf (stderr, "font selection order: ");
6280 for (i = 0; i < ARRAYELTS (font_sort_order); ++i)
6281 fprintf (stderr, "%d ", font_sort_order[i]);
6282 fprintf (stderr, "\n");
6284 fprintf (stderr, "alternative fonts: ");
6285 debug_print (Vface_alternative_font_family_alist);
6286 fprintf (stderr, "\n");
6288 for (i = 0; i < FRAME_FACE_CACHE (SELECTED_FRAME ())->used; ++i)
6289 Fdump_face (make_number (i));
6291 else
6293 struct face *face;
6294 CHECK_NUMBER (n);
6295 face = FACE_FROM_ID (SELECTED_FRAME (), XINT (n));
6296 if (face == NULL)
6297 error ("Not a valid face");
6298 dump_realized_face (face);
6301 return Qnil;
6305 DEFUN ("show-face-resources", Fshow_face_resources, Sshow_face_resources,
6306 0, 0, 0, doc: /* */)
6307 (void)
6309 fprintf (stderr, "number of colors = %d\n", ncolors_allocated);
6310 fprintf (stderr, "number of pixmaps = %d\n", npixmaps_allocated);
6311 fprintf (stderr, "number of GCs = %d\n", ngcs);
6312 return Qnil;
6315 #endif /* GLYPH_DEBUG */
6319 /***********************************************************************
6320 Initialization
6321 ***********************************************************************/
6323 void
6324 syms_of_xfaces (void)
6326 /* The symbols `face' and `mouse-face' used as text properties. */
6327 DEFSYM (Qface, "face");
6329 /* Property for basic faces which other faces cannot inherit. */
6330 DEFSYM (Qface_no_inherit, "face-no-inherit");
6332 /* Error symbol for wrong_type_argument in load_pixmap. */
6333 DEFSYM (Qbitmap_spec_p, "bitmap-spec-p");
6335 /* The name of the function to call when the background of the frame
6336 has changed, frame_set_background_mode. */
6337 DEFSYM (Qframe_set_background_mode, "frame-set-background-mode");
6339 /* Lisp face attribute keywords. */
6340 DEFSYM (QCfamily, ":family");
6341 DEFSYM (QCheight, ":height");
6342 DEFSYM (QCweight, ":weight");
6343 DEFSYM (QCslant, ":slant");
6344 DEFSYM (QCunderline, ":underline");
6345 DEFSYM (QCinverse_video, ":inverse-video");
6346 DEFSYM (QCreverse_video, ":reverse-video");
6347 DEFSYM (QCforeground, ":foreground");
6348 DEFSYM (QCbackground, ":background");
6349 DEFSYM (QCstipple, ":stipple");
6350 DEFSYM (QCwidth, ":width");
6351 DEFSYM (QCfont, ":font");
6352 DEFSYM (QCfontset, ":fontset");
6353 DEFSYM (QCdistant_foreground, ":distant-foreground");
6354 DEFSYM (QCbold, ":bold");
6355 DEFSYM (QCitalic, ":italic");
6356 DEFSYM (QCoverline, ":overline");
6357 DEFSYM (QCstrike_through, ":strike-through");
6358 DEFSYM (QCbox, ":box");
6359 DEFSYM (QCinherit, ":inherit");
6361 /* Symbols used for Lisp face attribute values. */
6362 DEFSYM (QCcolor, ":color");
6363 DEFSYM (QCline_width, ":line-width");
6364 DEFSYM (QCstyle, ":style");
6365 DEFSYM (Qline, "line");
6366 DEFSYM (Qwave, "wave");
6367 DEFSYM (Qreleased_button, "released-button");
6368 DEFSYM (Qpressed_button, "pressed-button");
6369 DEFSYM (Qnormal, "normal");
6370 DEFSYM (Qextra_light, "extra-light");
6371 DEFSYM (Qlight, "light");
6372 DEFSYM (Qsemi_light, "semi-light");
6373 DEFSYM (Qsemi_bold, "semi-bold");
6374 DEFSYM (Qbold, "bold");
6375 DEFSYM (Qextra_bold, "extra-bold");
6376 DEFSYM (Qultra_bold, "ultra-bold");
6377 DEFSYM (Qoblique, "oblique");
6378 DEFSYM (Qitalic, "italic");
6380 /* The symbols `foreground-color' and `background-color' which can be
6381 used as part of a `face' property. This is for compatibility with
6382 Emacs 20.2. */
6383 DEFSYM (Qbackground_color, "background-color");
6384 DEFSYM (Qforeground_color, "foreground-color");
6386 DEFSYM (Qunspecified, "unspecified");
6387 DEFSYM (QCignore_defface, ":ignore-defface");
6389 /* The symbol `face-alias'. A symbol having that property is an
6390 alias for another face. Value of the property is the name of
6391 the aliased face. */
6392 DEFSYM (Qface_alias, "face-alias");
6394 /* Names of basic faces. */
6395 DEFSYM (Qdefault, "default");
6396 DEFSYM (Qtool_bar, "tool-bar");
6397 DEFSYM (Qfringe, "fringe");
6398 DEFSYM (Qheader_line, "header-line");
6399 DEFSYM (Qscroll_bar, "scroll-bar");
6400 DEFSYM (Qmenu, "menu");
6401 DEFSYM (Qcursor, "cursor");
6402 DEFSYM (Qborder, "border");
6403 DEFSYM (Qmouse, "mouse");
6404 DEFSYM (Qmode_line_inactive, "mode-line-inactive");
6405 DEFSYM (Qvertical_border, "vertical-border");
6407 /* TTY color-related functions (defined in tty-colors.el). */
6408 DEFSYM (Qwindow_divider, "window-divider");
6409 DEFSYM (Qwindow_divider_first_pixel, "window-divider-first-pixel");
6410 DEFSYM (Qwindow_divider_last_pixel, "window-divider-last-pixel");
6411 DEFSYM (Qtty_color_desc, "tty-color-desc");
6412 DEFSYM (Qtty_color_standard_values, "tty-color-standard-values");
6413 DEFSYM (Qtty_color_by_index, "tty-color-by-index");
6415 /* The name of the function used to compute colors on TTYs. */
6416 DEFSYM (Qtty_color_alist, "tty-color-alist");
6418 Vparam_value_alist = list1 (Fcons (Qnil, Qnil));
6419 staticpro (&Vparam_value_alist);
6420 Vface_alternative_font_family_alist = Qnil;
6421 staticpro (&Vface_alternative_font_family_alist);
6422 Vface_alternative_font_registry_alist = Qnil;
6423 staticpro (&Vface_alternative_font_registry_alist);
6425 defsubr (&Sinternal_make_lisp_face);
6426 defsubr (&Sinternal_lisp_face_p);
6427 defsubr (&Sinternal_set_lisp_face_attribute);
6428 #ifdef HAVE_WINDOW_SYSTEM
6429 defsubr (&Sinternal_set_lisp_face_attribute_from_resource);
6430 #endif
6431 defsubr (&Scolor_gray_p);
6432 defsubr (&Scolor_supported_p);
6433 #ifndef HAVE_X_WINDOWS
6434 defsubr (&Sx_load_color_file);
6435 #endif
6436 defsubr (&Sface_attribute_relative_p);
6437 defsubr (&Smerge_face_attribute);
6438 defsubr (&Sinternal_get_lisp_face_attribute);
6439 defsubr (&Sinternal_lisp_face_attribute_values);
6440 defsubr (&Sinternal_lisp_face_equal_p);
6441 defsubr (&Sinternal_lisp_face_empty_p);
6442 defsubr (&Sinternal_copy_lisp_face);
6443 defsubr (&Sinternal_merge_in_global_face);
6444 defsubr (&Sface_font);
6445 defsubr (&Sframe_face_alist);
6446 defsubr (&Sdisplay_supports_face_attributes_p);
6447 defsubr (&Scolor_distance);
6448 defsubr (&Sinternal_set_font_selection_order);
6449 defsubr (&Sinternal_set_alternative_font_family_alist);
6450 defsubr (&Sinternal_set_alternative_font_registry_alist);
6451 defsubr (&Sface_attributes_as_vector);
6452 #ifdef GLYPH_DEBUG
6453 defsubr (&Sdump_face);
6454 defsubr (&Sshow_face_resources);
6455 #endif /* GLYPH_DEBUG */
6456 defsubr (&Sclear_face_cache);
6457 defsubr (&Stty_suppress_bold_inverse_default_colors);
6459 #if defined DEBUG_X_COLORS && defined HAVE_X_WINDOWS
6460 defsubr (&Sdump_colors);
6461 #endif
6463 DEFVAR_LISP ("face-new-frame-defaults", Vface_new_frame_defaults,
6464 doc: /* List of global face definitions (for internal use only.) */);
6465 Vface_new_frame_defaults = Qnil;
6467 DEFVAR_LISP ("face-default-stipple", Vface_default_stipple,
6468 doc: /* Default stipple pattern used on monochrome displays.
6469 This stipple pattern is used on monochrome displays
6470 instead of shades of gray for a face background color.
6471 See `set-face-stipple' for possible values for this variable. */);
6472 Vface_default_stipple = build_pure_c_string ("gray3");
6474 DEFVAR_LISP ("tty-defined-color-alist", Vtty_defined_color_alist,
6475 doc: /* An alist of defined terminal colors and their RGB values.
6476 See the docstring of `tty-color-alist' for the details. */);
6477 Vtty_defined_color_alist = Qnil;
6479 DEFVAR_LISP ("scalable-fonts-allowed", Vscalable_fonts_allowed,
6480 doc: /* Allowed scalable fonts.
6481 A value of nil means don't allow any scalable fonts.
6482 A value of t means allow any scalable font.
6483 Otherwise, value must be a list of regular expressions. A font may be
6484 scaled if its name matches a regular expression in the list.
6485 Note that if value is nil, a scalable font might still be used, if no
6486 other font of the appropriate family and registry is available. */);
6487 Vscalable_fonts_allowed = Qnil;
6489 DEFVAR_LISP ("face-ignored-fonts", Vface_ignored_fonts,
6490 doc: /* List of ignored fonts.
6491 Each element is a regular expression that matches names of fonts to
6492 ignore. */);
6493 Vface_ignored_fonts = Qnil;
6495 DEFVAR_LISP ("face-remapping-alist", Vface_remapping_alist,
6496 doc: /* Alist of face remappings.
6497 Each element is of the form:
6499 (FACE . REPLACEMENT),
6501 which causes display of the face FACE to use REPLACEMENT instead.
6502 REPLACEMENT is a face specification, i.e. one of the following:
6504 (1) a face name
6505 (2) a property list of attribute/value pairs, or
6506 (3) a list in which each element has the form of (1) or (2).
6508 List values for REPLACEMENT are merged to form the final face
6509 specification, with earlier entries taking precedence, in the same as
6510 as in the `face' text property.
6512 Face-name remapping cycles are suppressed; recursive references use
6513 the underlying face instead of the remapped face. So a remapping of
6514 the form:
6516 (FACE EXTRA-FACE... FACE)
6520 (FACE (FACE-ATTR VAL ...) FACE)
6522 causes EXTRA-FACE... or (FACE-ATTR VAL ...) to be _merged_ with the
6523 existing definition of FACE. Note that this isn't necessary for the
6524 default face, since every face inherits from the default face.
6526 If this variable is made buffer-local, the face remapping takes effect
6527 only in that buffer. For instance, the mode my-mode could define a
6528 face `my-mode-default', and then in the mode setup function, do:
6530 (set (make-local-variable \\='face-remapping-alist)
6531 \\='((default my-mode-default)))).
6533 Because Emacs normally only redraws screen areas when the underlying
6534 buffer contents change, you may need to call `redraw-display' after
6535 changing this variable for it to take effect. */);
6536 Vface_remapping_alist = Qnil;
6538 DEFVAR_LISP ("face-font-rescale-alist", Vface_font_rescale_alist,
6539 doc: /* Alist of fonts vs the rescaling factors.
6540 Each element is a cons (FONT-PATTERN . RESCALE-RATIO), where
6541 FONT-PATTERN is a font-spec or a regular expression matching a font name, and
6542 RESCALE-RATIO is a floating point number to specify how much larger
6543 (or smaller) font we should use. For instance, if a face requests
6544 a font of 10 point, we actually use a font of 10 * RESCALE-RATIO point. */);
6545 Vface_font_rescale_alist = Qnil;
6547 #ifdef HAVE_WINDOW_SYSTEM
6548 defsubr (&Sbitmap_spec_p);
6549 defsubr (&Sx_list_fonts);
6550 defsubr (&Sinternal_face_x_get_resource);
6551 defsubr (&Sx_family_fonts);
6552 #endif