* lisp/textmodes/tex-mode.el: Use lexical-binding
[emacs.git] / src / xfaces.c
blob8cf0b4277994b47b851103007917e2ba5aa9bef0
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 int class = FRAME_DISPLAY_INFO (f)->visual->class;
459 /* If display has an immutable color map, freeing colors is not
460 necessary and some servers don't allow it. So don't do it. */
461 if (class != StaticColor && class != StaticGray && class != TrueColor)
463 #ifdef DEBUG_X_COLORS
464 unregister_colors (pixels, npixels);
465 #endif
466 XFreeColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
467 pixels, npixels, 0);
472 #ifdef USE_X_TOOLKIT
474 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
475 color values. Interrupt input must be blocked when this function
476 is called. */
478 void
479 x_free_dpy_colors (Display *dpy, Screen *screen, Colormap cmap,
480 unsigned long *pixels, int npixels)
482 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
483 int class = dpyinfo->visual->class;
485 /* If display has an immutable color map, freeing colors is not
486 necessary and some servers don't allow it. So don't do it. */
487 if (class != StaticColor && class != StaticGray && class != TrueColor)
489 #ifdef DEBUG_X_COLORS
490 unregister_colors (pixels, npixels);
491 #endif
492 XFreeColors (dpy, cmap, pixels, npixels, 0);
495 #endif /* USE_X_TOOLKIT */
497 /* Create and return a GC for use on frame F. GC values and mask
498 are given by XGCV and MASK. */
500 static GC
501 x_create_gc (struct frame *f, unsigned long mask, XGCValues *xgcv)
503 GC gc;
504 block_input ();
505 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), mask, xgcv);
506 unblock_input ();
507 IF_DEBUG (++ngcs);
508 return gc;
512 /* Free GC which was used on frame F. */
514 static void
515 x_free_gc (struct frame *f, GC gc)
517 eassert (input_blocked_p ());
518 IF_DEBUG ((--ngcs, eassert (ngcs >= 0)));
519 XFreeGC (FRAME_X_DISPLAY (f), gc);
522 #endif /* HAVE_X_WINDOWS */
524 #ifdef HAVE_NTGUI
525 /* W32 emulation of GCs */
527 static GC
528 x_create_gc (struct frame *f, unsigned long mask, XGCValues *xgcv)
530 GC gc;
531 block_input ();
532 gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, xgcv);
533 unblock_input ();
534 IF_DEBUG (++ngcs);
535 return gc;
539 /* Free GC which was used on frame F. */
541 static void
542 x_free_gc (struct frame *f, GC gc)
544 IF_DEBUG ((--ngcs, eassert (ngcs >= 0)));
545 xfree (gc);
548 #endif /* HAVE_NTGUI */
550 #ifdef HAVE_NS
551 /* NS emulation of GCs */
553 static GC
554 x_create_gc (struct frame *f,
555 unsigned long mask,
556 XGCValues *xgcv)
558 GC gc = xmalloc (sizeof *gc);
559 *gc = *xgcv;
560 return gc;
563 static void
564 x_free_gc (struct frame *f, GC gc)
566 xfree (gc);
568 #endif /* HAVE_NS */
570 /***********************************************************************
571 Frames and faces
572 ***********************************************************************/
574 /* Initialize face cache and basic faces for frame F. */
576 void
577 init_frame_faces (struct frame *f)
579 /* Make a face cache, if F doesn't have one. */
580 if (FRAME_FACE_CACHE (f) == NULL)
581 FRAME_FACE_CACHE (f) = make_face_cache (f);
583 #ifdef HAVE_WINDOW_SYSTEM
584 /* Make the image cache. */
585 if (FRAME_WINDOW_P (f))
587 /* We initialize the image cache when creating the first frame
588 on a terminal, and not during terminal creation. This way,
589 `x-open-connection' on a tty won't create an image cache. */
590 if (FRAME_IMAGE_CACHE (f) == NULL)
591 FRAME_IMAGE_CACHE (f) = make_image_cache ();
592 ++FRAME_IMAGE_CACHE (f)->refcount;
594 #endif /* HAVE_WINDOW_SYSTEM */
596 /* Realize faces early (Bug#17889). */
597 if (!realize_basic_faces (f))
598 emacs_abort ();
602 /* Free face cache of frame F. Called from frame-dependent
603 resource freeing function, e.g. (x|tty)_free_frame_resources. */
605 void
606 free_frame_faces (struct frame *f)
608 struct face_cache *face_cache = FRAME_FACE_CACHE (f);
610 if (face_cache)
612 free_face_cache (face_cache);
613 FRAME_FACE_CACHE (f) = NULL;
616 #ifdef HAVE_WINDOW_SYSTEM
617 if (FRAME_WINDOW_P (f))
619 struct image_cache *image_cache = FRAME_IMAGE_CACHE (f);
620 if (image_cache)
622 --image_cache->refcount;
623 if (image_cache->refcount == 0)
624 free_image_cache (f);
627 #endif /* HAVE_WINDOW_SYSTEM */
631 /* Clear face caches, and recompute basic faces for frame F. Call
632 this after changing frame parameters on which those faces depend,
633 or when realized faces have been freed due to changing attributes
634 of named faces. */
636 void
637 recompute_basic_faces (struct frame *f)
639 if (FRAME_FACE_CACHE (f))
641 clear_face_cache (false);
642 if (!realize_basic_faces (f))
643 emacs_abort ();
648 /* Clear the face caches of all frames. CLEAR_FONTS_P means
649 try to free unused fonts, too. */
651 void
652 clear_face_cache (bool clear_fonts_p)
654 #ifdef HAVE_WINDOW_SYSTEM
655 Lisp_Object tail, frame;
657 if (clear_fonts_p
658 || ++clear_font_table_count == CLEAR_FONT_TABLE_COUNT)
660 /* From time to time see if we can unload some fonts. This also
661 frees all realized faces on all frames. Fonts needed by
662 faces will be loaded again when faces are realized again. */
663 clear_font_table_count = 0;
665 FOR_EACH_FRAME (tail, frame)
667 struct frame *f = XFRAME (frame);
668 if (FRAME_WINDOW_P (f)
669 && FRAME_DISPLAY_INFO (f)->n_fonts > CLEAR_FONT_TABLE_NFONTS)
671 clear_font_cache (f);
672 free_all_realized_faces (frame);
676 else
678 /* Clear GCs of realized faces. */
679 FOR_EACH_FRAME (tail, frame)
681 struct frame *f = XFRAME (frame);
682 if (FRAME_WINDOW_P (f))
683 clear_face_gcs (FRAME_FACE_CACHE (f));
685 clear_image_caches (Qnil);
687 #endif /* HAVE_WINDOW_SYSTEM */
690 DEFUN ("clear-face-cache", Fclear_face_cache, Sclear_face_cache, 0, 1, 0,
691 doc: /* Clear face caches on all frames.
692 Optional THOROUGHLY non-nil means try to free unused fonts, too. */)
693 (Lisp_Object thoroughly)
695 clear_face_cache (!NILP (thoroughly));
696 face_change = true;
697 windows_or_buffers_changed = 53;
698 return Qnil;
702 /***********************************************************************
703 X Pixmaps
704 ***********************************************************************/
706 #ifdef HAVE_WINDOW_SYSTEM
708 DEFUN ("bitmap-spec-p", Fbitmap_spec_p, Sbitmap_spec_p, 1, 1, 0,
709 doc: /* Value is non-nil if OBJECT is a valid bitmap specification.
710 A bitmap specification is either a string, a file name, or a list
711 (WIDTH HEIGHT DATA) where WIDTH is the pixel width of the bitmap,
712 HEIGHT is its height, and DATA is a string containing the bits of
713 the pixmap. Bits are stored row by row, each row occupies
714 (WIDTH + 7)/8 bytes. */)
715 (Lisp_Object object)
717 bool pixmap_p = false;
719 if (STRINGP (object))
720 /* If OBJECT is a string, it's a file name. */
721 pixmap_p = true;
722 else if (CONSP (object))
724 /* Otherwise OBJECT must be (WIDTH HEIGHT DATA), WIDTH and
725 HEIGHT must be ints > 0, and DATA must be string large
726 enough to hold a bitmap of the specified size. */
727 Lisp_Object width, height, data;
729 height = width = data = Qnil;
731 if (CONSP (object))
733 width = XCAR (object);
734 object = XCDR (object);
735 if (CONSP (object))
737 height = XCAR (object);
738 object = XCDR (object);
739 if (CONSP (object))
740 data = XCAR (object);
744 if (STRINGP (data)
745 && RANGED_INTEGERP (1, width, INT_MAX)
746 && RANGED_INTEGERP (1, height, INT_MAX))
748 int bytes_per_row = ((XINT (width) + BITS_PER_CHAR - 1)
749 / BITS_PER_CHAR);
750 if (XINT (height) <= SBYTES (data) / bytes_per_row)
751 pixmap_p = true;
755 return pixmap_p ? Qt : Qnil;
759 /* Load a bitmap according to NAME (which is either a file name or a
760 pixmap spec) for use on frame F. Value is the bitmap_id (see
761 xfns.c). If NAME is nil, return with a bitmap id of zero. If
762 bitmap cannot be loaded, display a message saying so, and return
763 zero. */
765 static ptrdiff_t
766 load_pixmap (struct frame *f, Lisp_Object name)
768 ptrdiff_t bitmap_id;
770 if (NILP (name))
771 return 0;
773 CHECK_TYPE (!NILP (Fbitmap_spec_p (name)), Qbitmap_spec_p, name);
775 block_input ();
776 if (CONSP (name))
778 /* Decode a bitmap spec into a bitmap. */
780 int h, w;
781 Lisp_Object bits;
783 w = XINT (Fcar (name));
784 h = XINT (Fcar (Fcdr (name)));
785 bits = Fcar (Fcdr (Fcdr (name)));
787 bitmap_id = x_create_bitmap_from_data (f, SSDATA (bits),
788 w, h);
790 else
792 /* It must be a string -- a file name. */
793 bitmap_id = x_create_bitmap_from_file (f, name);
795 unblock_input ();
797 if (bitmap_id < 0)
799 add_to_log ("Invalid or undefined bitmap `%s'", name);
800 bitmap_id = 0;
802 else
804 #ifdef GLYPH_DEBUG
805 ++npixmaps_allocated;
806 #endif
809 return bitmap_id;
812 #endif /* HAVE_WINDOW_SYSTEM */
816 /***********************************************************************
817 X Colors
818 ***********************************************************************/
820 /* Parse RGB_LIST, and fill in the RGB fields of COLOR.
821 RGB_LIST should contain (at least) 3 lisp integers.
822 Return true iff RGB_LIST is OK. */
824 static bool
825 parse_rgb_list (Lisp_Object rgb_list, XColor *color)
827 #define PARSE_RGB_LIST_FIELD(field) \
828 if (CONSP (rgb_list) && INTEGERP (XCAR (rgb_list))) \
830 color->field = XINT (XCAR (rgb_list)); \
831 rgb_list = XCDR (rgb_list); \
833 else \
834 return false;
836 PARSE_RGB_LIST_FIELD (red);
837 PARSE_RGB_LIST_FIELD (green);
838 PARSE_RGB_LIST_FIELD (blue);
840 return true;
844 /* Lookup on frame F the color described by the lisp string COLOR.
845 The resulting tty color is returned in TTY_COLOR; if STD_COLOR is
846 non-zero, then the `standard' definition of the same color is
847 returned in it. */
849 static bool
850 tty_lookup_color (struct frame *f, Lisp_Object color, XColor *tty_color,
851 XColor *std_color)
853 Lisp_Object frame, color_desc;
855 if (!STRINGP (color) || NILP (Ffboundp (Qtty_color_desc)))
856 return false;
858 XSETFRAME (frame, f);
860 color_desc = call2 (Qtty_color_desc, color, frame);
861 if (CONSP (color_desc) && CONSP (XCDR (color_desc)))
863 Lisp_Object rgb;
865 if (! INTEGERP (XCAR (XCDR (color_desc))))
866 return false;
868 tty_color->pixel = XINT (XCAR (XCDR (color_desc)));
870 rgb = XCDR (XCDR (color_desc));
871 if (! parse_rgb_list (rgb, tty_color))
872 return false;
874 /* Should we fill in STD_COLOR too? */
875 if (std_color)
877 /* Default STD_COLOR to the same as TTY_COLOR. */
878 *std_color = *tty_color;
880 /* Do a quick check to see if the returned descriptor is
881 actually _exactly_ equal to COLOR, otherwise we have to
882 lookup STD_COLOR separately. If it's impossible to lookup
883 a standard color, we just give up and use TTY_COLOR. */
884 if ((!STRINGP (XCAR (color_desc))
885 || NILP (Fstring_equal (color, XCAR (color_desc))))
886 && !NILP (Ffboundp (Qtty_color_standard_values)))
888 /* Look up STD_COLOR separately. */
889 rgb = call1 (Qtty_color_standard_values, color);
890 if (! parse_rgb_list (rgb, std_color))
891 return false;
895 return true;
897 else if (NILP (Fsymbol_value (intern ("tty-defined-color-alist"))))
898 /* We were called early during startup, and the colors are not
899 yet set up in tty-defined-color-alist. Don't return a failure
900 indication, since this produces the annoying "Unable to
901 load color" messages in the *Messages* buffer. */
902 return true;
903 else
904 /* tty-color-desc seems to have returned a bad value. */
905 return false;
908 /* A version of defined_color for non-X frames. */
910 static bool
911 tty_defined_color (struct frame *f, const char *color_name,
912 XColor *color_def, bool alloc)
914 bool status = true;
916 /* Defaults. */
917 color_def->pixel = FACE_TTY_DEFAULT_COLOR;
918 color_def->red = 0;
919 color_def->blue = 0;
920 color_def->green = 0;
922 if (*color_name)
923 status = tty_lookup_color (f, build_string (color_name), color_def, NULL);
925 if (color_def->pixel == FACE_TTY_DEFAULT_COLOR && *color_name)
927 if (strcmp (color_name, "unspecified-fg") == 0)
928 color_def->pixel = FACE_TTY_DEFAULT_FG_COLOR;
929 else if (strcmp (color_name, "unspecified-bg") == 0)
930 color_def->pixel = FACE_TTY_DEFAULT_BG_COLOR;
933 if (color_def->pixel != FACE_TTY_DEFAULT_COLOR)
934 status = true;
936 return status;
940 /* Decide if color named COLOR_NAME is valid for the display
941 associated with the frame F; if so, return the rgb values in
942 COLOR_DEF. If ALLOC, allocate a new colormap cell.
944 This does the right thing for any type of frame. */
946 static bool
947 defined_color (struct frame *f, const char *color_name, XColor *color_def,
948 bool alloc)
950 if (!FRAME_WINDOW_P (f))
951 return tty_defined_color (f, color_name, color_def, alloc);
952 #ifdef HAVE_X_WINDOWS
953 else if (FRAME_X_P (f))
954 return x_defined_color (f, color_name, color_def, alloc);
955 #endif
956 #ifdef HAVE_NTGUI
957 else if (FRAME_W32_P (f))
958 return w32_defined_color (f, color_name, color_def, alloc);
959 #endif
960 #ifdef HAVE_NS
961 else if (FRAME_NS_P (f))
962 return ns_defined_color (f, color_name, color_def, alloc, true);
963 #endif
964 else
965 emacs_abort ();
969 /* Given the index IDX of a tty color on frame F, return its name, a
970 Lisp string. */
972 Lisp_Object
973 tty_color_name (struct frame *f, int idx)
975 if (idx >= 0 && !NILP (Ffboundp (Qtty_color_by_index)))
977 Lisp_Object frame;
978 Lisp_Object coldesc;
980 XSETFRAME (frame, f);
981 coldesc = call2 (Qtty_color_by_index, make_number (idx), frame);
983 if (!NILP (coldesc))
984 return XCAR (coldesc);
986 #ifdef MSDOS
987 /* We can have an MS-DOS frame under -nw for a short window of
988 opportunity before internal_terminal_init is called. DTRT. */
989 if (FRAME_MSDOS_P (f) && !inhibit_window_system)
990 return msdos_stdcolor_name (idx);
991 #endif
993 if (idx == FACE_TTY_DEFAULT_FG_COLOR)
994 return build_string (unspecified_fg);
995 if (idx == FACE_TTY_DEFAULT_BG_COLOR)
996 return build_string (unspecified_bg);
998 return Qunspecified;
1002 /* Return true if COLOR_NAME is a shade of gray (or white or
1003 black) on frame F.
1005 The criterion implemented here is not a terribly sophisticated one. */
1007 static bool
1008 face_color_gray_p (struct frame *f, const char *color_name)
1010 XColor color;
1011 bool gray_p;
1013 if (defined_color (f, color_name, &color, false))
1014 gray_p = (/* Any color sufficiently close to black counts as gray. */
1015 (color.red < 5000 && color.green < 5000 && color.blue < 5000)
1017 ((eabs (color.red - color.green)
1018 < max (color.red, color.green) / 20)
1019 && (eabs (color.green - color.blue)
1020 < max (color.green, color.blue) / 20)
1021 && (eabs (color.blue - color.red)
1022 < max (color.blue, color.red) / 20)));
1023 else
1024 gray_p = false;
1026 return gray_p;
1030 /* Return true if color COLOR_NAME can be displayed on frame F.
1031 BACKGROUND_P means the color will be used as background color. */
1033 static bool
1034 face_color_supported_p (struct frame *f, const char *color_name,
1035 bool background_p)
1037 Lisp_Object frame;
1038 XColor not_used;
1040 XSETFRAME (frame, f);
1041 return
1042 #ifdef HAVE_WINDOW_SYSTEM
1043 FRAME_WINDOW_P (f)
1044 ? (!NILP (Fxw_display_color_p (frame))
1045 || xstrcasecmp (color_name, "black") == 0
1046 || xstrcasecmp (color_name, "white") == 0
1047 || (background_p
1048 && face_color_gray_p (f, color_name))
1049 || (!NILP (Fx_display_grayscale_p (frame))
1050 && face_color_gray_p (f, color_name)))
1052 #endif
1053 tty_defined_color (f, color_name, &not_used, false);
1057 DEFUN ("color-gray-p", Fcolor_gray_p, Scolor_gray_p, 1, 2, 0,
1058 doc: /* Return non-nil if COLOR is a shade of gray (or white or black).
1059 FRAME specifies the frame and thus the display for interpreting COLOR.
1060 If FRAME is nil or omitted, use the selected frame. */)
1061 (Lisp_Object color, Lisp_Object frame)
1063 CHECK_STRING (color);
1064 return (face_color_gray_p (decode_any_frame (frame), SSDATA (color))
1065 ? Qt : Qnil);
1069 DEFUN ("color-supported-p", Fcolor_supported_p,
1070 Scolor_supported_p, 1, 3, 0,
1071 doc: /* Return non-nil if COLOR can be displayed on FRAME.
1072 BACKGROUND-P non-nil means COLOR is used as a background.
1073 Otherwise, this function tells whether it can be used as a foreground.
1074 If FRAME is nil or omitted, use the selected frame.
1075 COLOR must be a valid color name. */)
1076 (Lisp_Object color, Lisp_Object frame, Lisp_Object background_p)
1078 CHECK_STRING (color);
1079 return (face_color_supported_p (decode_any_frame (frame),
1080 SSDATA (color), !NILP (background_p))
1081 ? Qt : Qnil);
1085 static unsigned long
1086 load_color2 (struct frame *f, struct face *face, Lisp_Object name,
1087 enum lface_attribute_index target_index, XColor *color)
1089 eassert (STRINGP (name));
1090 eassert (target_index == LFACE_FOREGROUND_INDEX
1091 || target_index == LFACE_BACKGROUND_INDEX
1092 || target_index == LFACE_UNDERLINE_INDEX
1093 || target_index == LFACE_OVERLINE_INDEX
1094 || target_index == LFACE_STRIKE_THROUGH_INDEX
1095 || target_index == LFACE_BOX_INDEX);
1097 /* if the color map is full, defined_color will return a best match
1098 to the values in an existing cell. */
1099 if (!defined_color (f, SSDATA (name), color, true))
1101 add_to_log ("Unable to load color \"%s\"", name);
1103 switch (target_index)
1105 case LFACE_FOREGROUND_INDEX:
1106 face->foreground_defaulted_p = true;
1107 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1108 break;
1110 case LFACE_BACKGROUND_INDEX:
1111 face->background_defaulted_p = true;
1112 color->pixel = FRAME_BACKGROUND_PIXEL (f);
1113 break;
1115 case LFACE_UNDERLINE_INDEX:
1116 face->underline_defaulted_p = true;
1117 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1118 break;
1120 case LFACE_OVERLINE_INDEX:
1121 face->overline_color_defaulted_p = true;
1122 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1123 break;
1125 case LFACE_STRIKE_THROUGH_INDEX:
1126 face->strike_through_color_defaulted_p = true;
1127 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1128 break;
1130 case LFACE_BOX_INDEX:
1131 face->box_color_defaulted_p = true;
1132 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1133 break;
1135 default:
1136 emacs_abort ();
1139 #ifdef GLYPH_DEBUG
1140 else
1141 ++ncolors_allocated;
1142 #endif
1144 return color->pixel;
1147 /* Load color with name NAME for use by face FACE on frame F.
1148 TARGET_INDEX must be one of LFACE_FOREGROUND_INDEX,
1149 LFACE_BACKGROUND_INDEX, LFACE_UNDERLINE_INDEX, LFACE_OVERLINE_INDEX,
1150 LFACE_STRIKE_THROUGH_INDEX, or LFACE_BOX_INDEX. Value is the
1151 pixel color. If color cannot be loaded, display a message, and
1152 return the foreground, background or underline color of F, but
1153 record that fact in flags of the face so that we don't try to free
1154 these colors. */
1156 unsigned long
1157 load_color (struct frame *f, struct face *face, Lisp_Object name,
1158 enum lface_attribute_index target_index)
1160 XColor color;
1161 return load_color2 (f, face, name, target_index, &color);
1165 #ifdef HAVE_WINDOW_SYSTEM
1167 #define NEAR_SAME_COLOR_THRESHOLD 30000
1169 /* Load colors for face FACE which is used on frame F. Colors are
1170 specified by slots LFACE_BACKGROUND_INDEX and LFACE_FOREGROUND_INDEX
1171 of ATTRS. If the background color specified is not supported on F,
1172 try to emulate gray colors with a stipple from Vface_default_stipple. */
1174 static void
1175 load_face_colors (struct frame *f, struct face *face,
1176 Lisp_Object attrs[LFACE_VECTOR_SIZE])
1178 Lisp_Object fg, bg, dfg;
1179 XColor xfg, xbg;
1181 bg = attrs[LFACE_BACKGROUND_INDEX];
1182 fg = attrs[LFACE_FOREGROUND_INDEX];
1184 /* Swap colors if face is inverse-video. */
1185 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1187 Lisp_Object tmp;
1188 tmp = fg;
1189 fg = bg;
1190 bg = tmp;
1193 /* Check for support for foreground, not for background because
1194 face_color_supported_p is smart enough to know that grays are
1195 "supported" as background because we are supposed to use stipple
1196 for them. */
1197 if (!face_color_supported_p (f, SSDATA (bg), false)
1198 && !NILP (Fbitmap_spec_p (Vface_default_stipple)))
1200 x_destroy_bitmap (f, face->stipple);
1201 face->stipple = load_pixmap (f, Vface_default_stipple);
1204 face->background = load_color2 (f, face, bg, LFACE_BACKGROUND_INDEX, &xbg);
1205 face->foreground = load_color2 (f, face, fg, LFACE_FOREGROUND_INDEX, &xfg);
1207 dfg = attrs[LFACE_DISTANT_FOREGROUND_INDEX];
1208 if (!NILP (dfg) && !UNSPECIFIEDP (dfg)
1209 && color_distance (&xbg, &xfg) < NEAR_SAME_COLOR_THRESHOLD)
1211 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1212 face->background = load_color (f, face, dfg, LFACE_BACKGROUND_INDEX);
1213 else
1214 face->foreground = load_color (f, face, dfg, LFACE_FOREGROUND_INDEX);
1218 #ifdef HAVE_X_WINDOWS
1220 /* Free color PIXEL on frame F. */
1222 void
1223 unload_color (struct frame *f, unsigned long pixel)
1225 if (pixel != -1)
1227 block_input ();
1228 x_free_colors (f, &pixel, 1);
1229 unblock_input ();
1233 /* Free colors allocated for FACE. */
1235 static void
1236 free_face_colors (struct frame *f, struct face *face)
1238 /* PENDING(NS): need to do something here? */
1240 if (face->colors_copied_bitwise_p)
1241 return;
1243 block_input ();
1245 if (!face->foreground_defaulted_p)
1247 x_free_colors (f, &face->foreground, 1);
1248 IF_DEBUG (--ncolors_allocated);
1251 if (!face->background_defaulted_p)
1253 x_free_colors (f, &face->background, 1);
1254 IF_DEBUG (--ncolors_allocated);
1257 if (face->underline_p
1258 && !face->underline_defaulted_p)
1260 x_free_colors (f, &face->underline_color, 1);
1261 IF_DEBUG (--ncolors_allocated);
1264 if (face->overline_p
1265 && !face->overline_color_defaulted_p)
1267 x_free_colors (f, &face->overline_color, 1);
1268 IF_DEBUG (--ncolors_allocated);
1271 if (face->strike_through_p
1272 && !face->strike_through_color_defaulted_p)
1274 x_free_colors (f, &face->strike_through_color, 1);
1275 IF_DEBUG (--ncolors_allocated);
1278 if (face->box != FACE_NO_BOX
1279 && !face->box_color_defaulted_p)
1281 x_free_colors (f, &face->box_color, 1);
1282 IF_DEBUG (--ncolors_allocated);
1285 unblock_input ();
1288 #endif /* HAVE_X_WINDOWS */
1290 #endif /* HAVE_WINDOW_SYSTEM */
1294 /***********************************************************************
1295 XLFD Font Names
1296 ***********************************************************************/
1298 /* An enumerator for each field of an XLFD font name. */
1300 enum xlfd_field
1302 XLFD_FOUNDRY,
1303 XLFD_FAMILY,
1304 XLFD_WEIGHT,
1305 XLFD_SLANT,
1306 XLFD_SWIDTH,
1307 XLFD_ADSTYLE,
1308 XLFD_PIXEL_SIZE,
1309 XLFD_POINT_SIZE,
1310 XLFD_RESX,
1311 XLFD_RESY,
1312 XLFD_SPACING,
1313 XLFD_AVGWIDTH,
1314 XLFD_REGISTRY,
1315 XLFD_ENCODING,
1316 XLFD_LAST
1319 /* An enumerator for each possible slant value of a font. Taken from
1320 the XLFD specification. */
1322 enum xlfd_slant
1324 XLFD_SLANT_UNKNOWN,
1325 XLFD_SLANT_ROMAN,
1326 XLFD_SLANT_ITALIC,
1327 XLFD_SLANT_OBLIQUE,
1328 XLFD_SLANT_REVERSE_ITALIC,
1329 XLFD_SLANT_REVERSE_OBLIQUE,
1330 XLFD_SLANT_OTHER
1333 /* Relative font weight according to XLFD documentation. */
1335 enum xlfd_weight
1337 XLFD_WEIGHT_UNKNOWN,
1338 XLFD_WEIGHT_ULTRA_LIGHT, /* 10 */
1339 XLFD_WEIGHT_EXTRA_LIGHT, /* 20 */
1340 XLFD_WEIGHT_LIGHT, /* 30 */
1341 XLFD_WEIGHT_SEMI_LIGHT, /* 40: SemiLight, Book, ... */
1342 XLFD_WEIGHT_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1343 XLFD_WEIGHT_SEMI_BOLD, /* 60: SemiBold, DemiBold, ... */
1344 XLFD_WEIGHT_BOLD, /* 70: Bold, ... */
1345 XLFD_WEIGHT_EXTRA_BOLD, /* 80: ExtraBold, Heavy, ... */
1346 XLFD_WEIGHT_ULTRA_BOLD /* 90: UltraBold, Black, ... */
1349 /* Relative proportionate width. */
1351 enum xlfd_swidth
1353 XLFD_SWIDTH_UNKNOWN,
1354 XLFD_SWIDTH_ULTRA_CONDENSED, /* 10 */
1355 XLFD_SWIDTH_EXTRA_CONDENSED, /* 20 */
1356 XLFD_SWIDTH_CONDENSED, /* 30: Condensed, Narrow, Compressed, ... */
1357 XLFD_SWIDTH_SEMI_CONDENSED, /* 40: semicondensed */
1358 XLFD_SWIDTH_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1359 XLFD_SWIDTH_SEMI_EXPANDED, /* 60: SemiExpanded, DemiExpanded, ... */
1360 XLFD_SWIDTH_EXPANDED, /* 70: Expanded... */
1361 XLFD_SWIDTH_EXTRA_EXPANDED, /* 80: ExtraExpanded, Wide... */
1362 XLFD_SWIDTH_ULTRA_EXPANDED /* 90: UltraExpanded... */
1365 /* Order by which font selection chooses fonts. The default values
1366 mean `first, find a best match for the font width, then for the
1367 font height, then for weight, then for slant.' This variable can be
1368 set via set-face-font-sort-order. */
1370 static int font_sort_order[4];
1372 #ifdef HAVE_WINDOW_SYSTEM
1374 static enum font_property_index font_props_for_sorting[FONT_SIZE_INDEX];
1376 static int
1377 compare_fonts_by_sort_order (const void *v1, const void *v2)
1379 Lisp_Object const *p1 = v1;
1380 Lisp_Object const *p2 = v2;
1381 Lisp_Object font1 = *p1;
1382 Lisp_Object font2 = *p2;
1383 int i;
1385 for (i = 0; i < FONT_SIZE_INDEX; i++)
1387 enum font_property_index idx = font_props_for_sorting[i];
1388 Lisp_Object val1 = AREF (font1, idx), val2 = AREF (font2, idx);
1389 int result;
1391 if (idx <= FONT_REGISTRY_INDEX)
1393 if (STRINGP (val1))
1394 result = STRINGP (val2) ? strcmp (SSDATA (val1), SSDATA (val2)) : -1;
1395 else
1396 result = STRINGP (val2) ? 1 : 0;
1398 else
1400 if (INTEGERP (val1))
1401 result = (INTEGERP (val2) && XINT (val1) >= XINT (val2)
1402 ? XINT (val1) > XINT (val2)
1403 : -1);
1404 else
1405 result = INTEGERP (val2) ? 1 : 0;
1407 if (result)
1408 return result;
1410 return 0;
1413 DEFUN ("x-family-fonts", Fx_family_fonts, Sx_family_fonts, 0, 2, 0,
1414 doc: /* Return a list of available fonts of family FAMILY on FRAME.
1415 If FAMILY is omitted or nil, list all families.
1416 Otherwise, FAMILY must be a string, possibly containing wildcards
1417 `?' and `*'.
1418 If FRAME is omitted or nil, use the selected frame.
1419 Each element of the result is a vector [FAMILY WIDTH POINT-SIZE WEIGHT
1420 SLANT FIXED-P FULL REGISTRY-AND-ENCODING].
1421 FAMILY is the font family name. POINT-SIZE is the size of the
1422 font in 1/10 pt. WIDTH, WEIGHT, and SLANT are symbols describing the
1423 width, weight and slant of the font. These symbols are the same as for
1424 face attributes. FIXED-P is non-nil if the font is fixed-pitch.
1425 FULL is the full name of the font, and REGISTRY-AND-ENCODING is a string
1426 giving the registry and encoding of the font.
1427 The result list is sorted according to the current setting of
1428 the face font sort order. */)
1429 (Lisp_Object family, Lisp_Object frame)
1431 Lisp_Object font_spec, list, *drivers, vec;
1432 struct frame *f = decode_live_frame (frame);
1433 ptrdiff_t i, nfonts;
1434 EMACS_INT ndrivers;
1435 Lisp_Object result;
1436 USE_SAFE_ALLOCA;
1438 font_spec = Ffont_spec (0, NULL);
1439 if (!NILP (family))
1441 CHECK_STRING (family);
1442 font_parse_family_registry (family, Qnil, font_spec);
1445 list = font_list_entities (f, font_spec);
1446 if (NILP (list))
1447 return Qnil;
1449 /* Sort the font entities. */
1450 for (i = 0; i < 4; i++)
1451 switch (font_sort_order[i])
1453 case XLFD_SWIDTH:
1454 font_props_for_sorting[i] = FONT_WIDTH_INDEX; break;
1455 case XLFD_POINT_SIZE:
1456 font_props_for_sorting[i] = FONT_SIZE_INDEX; break;
1457 case XLFD_WEIGHT:
1458 font_props_for_sorting[i] = FONT_WEIGHT_INDEX; break;
1459 default:
1460 font_props_for_sorting[i] = FONT_SLANT_INDEX; break;
1462 font_props_for_sorting[i++] = FONT_FAMILY_INDEX;
1463 font_props_for_sorting[i++] = FONT_FOUNDRY_INDEX;
1464 font_props_for_sorting[i++] = FONT_ADSTYLE_INDEX;
1465 font_props_for_sorting[i++] = FONT_REGISTRY_INDEX;
1467 ndrivers = XINT (Flength (list));
1468 SAFE_ALLOCA_LISP (drivers, ndrivers);
1469 for (i = 0; i < ndrivers; i++, list = XCDR (list))
1470 drivers[i] = XCAR (list);
1471 vec = Fvconcat (ndrivers, drivers);
1472 nfonts = ASIZE (vec);
1474 qsort (XVECTOR (vec)->contents, nfonts, word_size,
1475 compare_fonts_by_sort_order);
1477 result = Qnil;
1478 for (i = nfonts - 1; i >= 0; --i)
1480 Lisp_Object font = AREF (vec, i);
1481 Lisp_Object v = make_uninit_vector (8);
1482 int point;
1483 Lisp_Object spacing;
1485 ASET (v, 0, AREF (font, FONT_FAMILY_INDEX));
1486 ASET (v, 1, FONT_WIDTH_SYMBOLIC (font));
1487 point = PIXEL_TO_POINT (XINT (AREF (font, FONT_SIZE_INDEX)) * 10,
1488 FRAME_RES_Y (f));
1489 ASET (v, 2, make_number (point));
1490 ASET (v, 3, FONT_WEIGHT_SYMBOLIC (font));
1491 ASET (v, 4, FONT_SLANT_SYMBOLIC (font));
1492 spacing = Ffont_get (font, QCspacing);
1493 ASET (v, 5, (NILP (spacing) || EQ (spacing, Qp)) ? Qnil : Qt);
1494 ASET (v, 6, Ffont_xlfd_name (font, Qnil));
1495 ASET (v, 7, AREF (font, FONT_REGISTRY_INDEX));
1497 result = Fcons (v, result);
1500 SAFE_FREE ();
1501 return result;
1504 DEFUN ("x-list-fonts", Fx_list_fonts, Sx_list_fonts, 1, 5, 0,
1505 doc: /* Return a list of the names of available fonts matching PATTERN.
1506 If optional arguments FACE and FRAME are specified, return only fonts
1507 the same size as FACE on FRAME.
1509 PATTERN should be a string containing a font name in the XLFD,
1510 Fontconfig, or GTK format. A font name given in the XLFD format may
1511 contain wildcard characters:
1512 the * character matches any substring, and
1513 the ? character matches any single character.
1514 PATTERN is case-insensitive.
1516 The return value is a list of strings, suitable as arguments to
1517 `set-face-font'.
1519 Fonts Emacs can't use may or may not be excluded
1520 even if they match PATTERN and FACE.
1521 The optional fourth argument MAXIMUM sets a limit on how many
1522 fonts to match. The first MAXIMUM fonts are reported.
1523 The optional fifth argument WIDTH, if specified, is a number of columns
1524 occupied by a character of a font. In that case, return only fonts
1525 the WIDTH times as wide as FACE on FRAME. */)
1526 (Lisp_Object pattern, Lisp_Object face, Lisp_Object frame,
1527 Lisp_Object maximum, Lisp_Object width)
1529 struct frame *f;
1530 int size, avgwidth IF_LINT (= 0);
1532 check_window_system (NULL);
1533 CHECK_STRING (pattern);
1535 if (! NILP (maximum))
1536 CHECK_NATNUM (maximum);
1538 if (!NILP (width))
1539 CHECK_NUMBER (width);
1541 /* We can't simply call decode_window_system_frame because
1542 this function may be called before any frame is created. */
1543 f = decode_live_frame (frame);
1544 if (! FRAME_WINDOW_P (f))
1546 /* Perhaps we have not yet created any frame. */
1547 f = NULL;
1548 frame = Qnil;
1549 face = Qnil;
1551 else
1552 XSETFRAME (frame, f);
1554 /* Determine the width standard for comparison with the fonts we find. */
1556 if (NILP (face))
1557 size = 0;
1558 else
1560 /* This is of limited utility since it works with character
1561 widths. Keep it for compatibility. --gerd. */
1562 int face_id = lookup_named_face (f, face, false);
1563 struct face *width_face = (face_id < 0
1564 ? NULL
1565 : FACE_FROM_ID (f, face_id));
1567 if (width_face && width_face->font)
1569 size = width_face->font->pixel_size;
1570 avgwidth = width_face->font->average_width;
1572 else
1574 size = FRAME_FONT (f)->pixel_size;
1575 avgwidth = FRAME_FONT (f)->average_width;
1577 if (!NILP (width))
1578 avgwidth *= XINT (width);
1581 Lisp_Object font_spec = font_spec_from_name (pattern);
1582 if (!FONTP (font_spec))
1583 signal_error ("Invalid font name", pattern);
1585 if (size)
1587 Ffont_put (font_spec, QCsize, make_number (size));
1588 Ffont_put (font_spec, QCavgwidth, make_number (avgwidth));
1590 Lisp_Object fonts = Flist_fonts (font_spec, frame, maximum, font_spec);
1591 for (Lisp_Object tail = fonts; CONSP (tail); tail = XCDR (tail))
1593 Lisp_Object font_entity;
1595 font_entity = XCAR (tail);
1596 if ((NILP (AREF (font_entity, FONT_SIZE_INDEX))
1597 || XINT (AREF (font_entity, FONT_SIZE_INDEX)) == 0)
1598 && ! NILP (AREF (font_spec, FONT_SIZE_INDEX)))
1600 /* This is a scalable font. For backward compatibility,
1601 we set the specified size. */
1602 font_entity = copy_font_spec (font_entity);
1603 ASET (font_entity, FONT_SIZE_INDEX,
1604 AREF (font_spec, FONT_SIZE_INDEX));
1606 XSETCAR (tail, Ffont_xlfd_name (font_entity, Qnil));
1608 if (NILP (frame))
1609 /* We don't have to check fontsets. */
1610 return fonts;
1611 Lisp_Object fontsets = list_fontsets (f, pattern, size);
1612 return CALLN (Fnconc, fonts, fontsets);
1615 #endif /* HAVE_WINDOW_SYSTEM */
1618 /***********************************************************************
1619 Lisp Faces
1620 ***********************************************************************/
1622 /* Access face attributes of face LFACE, a Lisp vector. */
1624 #define LFACE_FAMILY(LFACE) AREF ((LFACE), LFACE_FAMILY_INDEX)
1625 #define LFACE_FOUNDRY(LFACE) AREF ((LFACE), LFACE_FOUNDRY_INDEX)
1626 #define LFACE_HEIGHT(LFACE) AREF ((LFACE), LFACE_HEIGHT_INDEX)
1627 #define LFACE_WEIGHT(LFACE) AREF ((LFACE), LFACE_WEIGHT_INDEX)
1628 #define LFACE_SLANT(LFACE) AREF ((LFACE), LFACE_SLANT_INDEX)
1629 #define LFACE_UNDERLINE(LFACE) AREF ((LFACE), LFACE_UNDERLINE_INDEX)
1630 #define LFACE_INVERSE(LFACE) AREF ((LFACE), LFACE_INVERSE_INDEX)
1631 #define LFACE_FOREGROUND(LFACE) AREF ((LFACE), LFACE_FOREGROUND_INDEX)
1632 #define LFACE_BACKGROUND(LFACE) AREF ((LFACE), LFACE_BACKGROUND_INDEX)
1633 #define LFACE_STIPPLE(LFACE) AREF ((LFACE), LFACE_STIPPLE_INDEX)
1634 #define LFACE_SWIDTH(LFACE) AREF ((LFACE), LFACE_SWIDTH_INDEX)
1635 #define LFACE_OVERLINE(LFACE) AREF ((LFACE), LFACE_OVERLINE_INDEX)
1636 #define LFACE_STRIKE_THROUGH(LFACE) AREF ((LFACE), LFACE_STRIKE_THROUGH_INDEX)
1637 #define LFACE_BOX(LFACE) AREF ((LFACE), LFACE_BOX_INDEX)
1638 #define LFACE_FONT(LFACE) AREF ((LFACE), LFACE_FONT_INDEX)
1639 #define LFACE_INHERIT(LFACE) AREF ((LFACE), LFACE_INHERIT_INDEX)
1640 #define LFACE_FONTSET(LFACE) AREF ((LFACE), LFACE_FONTSET_INDEX)
1641 #define LFACE_DISTANT_FOREGROUND(LFACE) \
1642 AREF ((LFACE), LFACE_DISTANT_FOREGROUND_INDEX)
1644 /* True if LFACE is a Lisp face. A Lisp face is a vector of size
1645 LFACE_VECTOR_SIZE which has the symbol `face' in slot 0. */
1647 #define LFACEP(LFACE) \
1648 (VECTORP (LFACE) \
1649 && ASIZE (LFACE) == LFACE_VECTOR_SIZE \
1650 && EQ (AREF (LFACE, 0), Qface))
1653 #ifdef GLYPH_DEBUG
1655 /* Check consistency of Lisp face attribute vector ATTRS. */
1657 static void
1658 check_lface_attrs (Lisp_Object attrs[LFACE_VECTOR_SIZE])
1660 eassert (UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
1661 || IGNORE_DEFFACE_P (attrs[LFACE_FAMILY_INDEX])
1662 || STRINGP (attrs[LFACE_FAMILY_INDEX]));
1663 eassert (UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
1664 || IGNORE_DEFFACE_P (attrs[LFACE_FOUNDRY_INDEX])
1665 || STRINGP (attrs[LFACE_FOUNDRY_INDEX]));
1666 eassert (UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
1667 || IGNORE_DEFFACE_P (attrs[LFACE_SWIDTH_INDEX])
1668 || SYMBOLP (attrs[LFACE_SWIDTH_INDEX]));
1669 eassert (UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
1670 || IGNORE_DEFFACE_P (attrs[LFACE_HEIGHT_INDEX])
1671 || NUMBERP (attrs[LFACE_HEIGHT_INDEX])
1672 || FUNCTIONP (attrs[LFACE_HEIGHT_INDEX]));
1673 eassert (UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
1674 || IGNORE_DEFFACE_P (attrs[LFACE_WEIGHT_INDEX])
1675 || SYMBOLP (attrs[LFACE_WEIGHT_INDEX]));
1676 eassert (UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
1677 || IGNORE_DEFFACE_P (attrs[LFACE_SLANT_INDEX])
1678 || SYMBOLP (attrs[LFACE_SLANT_INDEX]));
1679 eassert (UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
1680 || IGNORE_DEFFACE_P (attrs[LFACE_UNDERLINE_INDEX])
1681 || SYMBOLP (attrs[LFACE_UNDERLINE_INDEX])
1682 || STRINGP (attrs[LFACE_UNDERLINE_INDEX])
1683 || CONSP (attrs[LFACE_UNDERLINE_INDEX]));
1684 eassert (UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
1685 || IGNORE_DEFFACE_P (attrs[LFACE_OVERLINE_INDEX])
1686 || SYMBOLP (attrs[LFACE_OVERLINE_INDEX])
1687 || STRINGP (attrs[LFACE_OVERLINE_INDEX]));
1688 eassert (UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1689 || IGNORE_DEFFACE_P (attrs[LFACE_STRIKE_THROUGH_INDEX])
1690 || SYMBOLP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1691 || STRINGP (attrs[LFACE_STRIKE_THROUGH_INDEX]));
1692 eassert (UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
1693 || IGNORE_DEFFACE_P (attrs[LFACE_BOX_INDEX])
1694 || SYMBOLP (attrs[LFACE_BOX_INDEX])
1695 || STRINGP (attrs[LFACE_BOX_INDEX])
1696 || INTEGERP (attrs[LFACE_BOX_INDEX])
1697 || CONSP (attrs[LFACE_BOX_INDEX]));
1698 eassert (UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
1699 || IGNORE_DEFFACE_P (attrs[LFACE_INVERSE_INDEX])
1700 || SYMBOLP (attrs[LFACE_INVERSE_INDEX]));
1701 eassert (UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
1702 || IGNORE_DEFFACE_P (attrs[LFACE_FOREGROUND_INDEX])
1703 || STRINGP (attrs[LFACE_FOREGROUND_INDEX]));
1704 eassert (UNSPECIFIEDP (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
1705 || IGNORE_DEFFACE_P (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
1706 || STRINGP (attrs[LFACE_DISTANT_FOREGROUND_INDEX]));
1707 eassert (UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
1708 || IGNORE_DEFFACE_P (attrs[LFACE_BACKGROUND_INDEX])
1709 || STRINGP (attrs[LFACE_BACKGROUND_INDEX]));
1710 eassert (UNSPECIFIEDP (attrs[LFACE_INHERIT_INDEX])
1711 || IGNORE_DEFFACE_P (attrs[LFACE_INHERIT_INDEX])
1712 || NILP (attrs[LFACE_INHERIT_INDEX])
1713 || SYMBOLP (attrs[LFACE_INHERIT_INDEX])
1714 || CONSP (attrs[LFACE_INHERIT_INDEX]));
1715 #ifdef HAVE_WINDOW_SYSTEM
1716 eassert (UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
1717 || IGNORE_DEFFACE_P (attrs[LFACE_STIPPLE_INDEX])
1718 || SYMBOLP (attrs[LFACE_STIPPLE_INDEX])
1719 || !NILP (Fbitmap_spec_p (attrs[LFACE_STIPPLE_INDEX])));
1720 eassert (UNSPECIFIEDP (attrs[LFACE_FONT_INDEX])
1721 || IGNORE_DEFFACE_P (attrs[LFACE_FONT_INDEX])
1722 || FONTP (attrs[LFACE_FONT_INDEX]));
1723 eassert (UNSPECIFIEDP (attrs[LFACE_FONTSET_INDEX])
1724 || STRINGP (attrs[LFACE_FONTSET_INDEX])
1725 || NILP (attrs[LFACE_FONTSET_INDEX]));
1726 #endif
1730 /* Check consistency of attributes of Lisp face LFACE (a Lisp vector). */
1732 static void
1733 check_lface (Lisp_Object lface)
1735 if (!NILP (lface))
1737 eassert (LFACEP (lface));
1738 check_lface_attrs (XVECTOR (lface)->contents);
1742 #else /* not GLYPH_DEBUG */
1744 #define check_lface_attrs(attrs) (void) 0
1745 #define check_lface(lface) (void) 0
1747 #endif /* GLYPH_DEBUG */
1751 /* Face-merge cycle checking. */
1753 enum named_merge_point_kind
1755 NAMED_MERGE_POINT_NORMAL,
1756 NAMED_MERGE_POINT_REMAP
1759 /* A `named merge point' is simply a point during face-merging where we
1760 look up a face by name. We keep a stack of which named lookups we're
1761 currently processing so that we can easily detect cycles, using a
1762 linked- list of struct named_merge_point structures, typically
1763 allocated on the stack frame of the named lookup functions which are
1764 active (so no consing is required). */
1765 struct named_merge_point
1767 Lisp_Object face_name;
1768 enum named_merge_point_kind named_merge_point_kind;
1769 struct named_merge_point *prev;
1773 /* If a face merging cycle is detected for FACE_NAME, return false,
1774 otherwise add NEW_NAMED_MERGE_POINT, which is initialized using
1775 FACE_NAME and NAMED_MERGE_POINT_KIND, as the head of the linked list
1776 pointed to by NAMED_MERGE_POINTS, and return true. */
1778 static bool
1779 push_named_merge_point (struct named_merge_point *new_named_merge_point,
1780 Lisp_Object face_name,
1781 enum named_merge_point_kind named_merge_point_kind,
1782 struct named_merge_point **named_merge_points)
1784 struct named_merge_point *prev;
1786 for (prev = *named_merge_points; prev; prev = prev->prev)
1787 if (EQ (face_name, prev->face_name))
1789 if (prev->named_merge_point_kind == named_merge_point_kind)
1790 /* A cycle, so fail. */
1791 return false;
1792 else if (prev->named_merge_point_kind == NAMED_MERGE_POINT_REMAP)
1793 /* A remap `hides ' any previous normal merge points
1794 (because the remap means that it's actually different face),
1795 so as we know the current merge point must be normal, we
1796 can just assume it's OK. */
1797 break;
1800 new_named_merge_point->face_name = face_name;
1801 new_named_merge_point->named_merge_point_kind = named_merge_point_kind;
1802 new_named_merge_point->prev = *named_merge_points;
1804 *named_merge_points = new_named_merge_point;
1806 return true;
1810 /* Resolve face name FACE_NAME. If FACE_NAME is a string, intern it
1811 to make it a symbol. If FACE_NAME is an alias for another face,
1812 return that face's name.
1814 Return default face in case of errors. */
1816 static Lisp_Object
1817 resolve_face_name (Lisp_Object face_name, bool signal_p)
1819 Lisp_Object orig_face;
1820 Lisp_Object tortoise, hare;
1822 if (STRINGP (face_name))
1823 face_name = Fintern (face_name, Qnil);
1825 if (NILP (face_name) || !SYMBOLP (face_name))
1826 return face_name;
1828 orig_face = face_name;
1829 tortoise = hare = face_name;
1831 while (true)
1833 face_name = hare;
1834 hare = Fget (hare, Qface_alias);
1835 if (NILP (hare) || !SYMBOLP (hare))
1836 break;
1838 face_name = hare;
1839 hare = Fget (hare, Qface_alias);
1840 if (NILP (hare) || !SYMBOLP (hare))
1841 break;
1843 tortoise = Fget (tortoise, Qface_alias);
1844 if (EQ (hare, tortoise))
1846 if (signal_p)
1847 xsignal1 (Qcircular_list, orig_face);
1848 return Qdefault;
1852 return face_name;
1856 /* Return the face definition of FACE_NAME on frame F. F null means
1857 return the definition for new frames. FACE_NAME may be a string or
1858 a symbol (apparently Emacs 20.2 allowed strings as face names in
1859 face text properties; Ediff uses that).
1860 If SIGNAL_P, signal an error if FACE_NAME is not a valid face name.
1861 Otherwise, value is nil if FACE_NAME is not a valid face name. */
1862 static Lisp_Object
1863 lface_from_face_name_no_resolve (struct frame *f, Lisp_Object face_name,
1864 bool signal_p)
1866 Lisp_Object lface;
1868 if (f)
1869 lface = assq_no_quit (face_name, f->face_alist);
1870 else
1871 lface = assq_no_quit (face_name, Vface_new_frame_defaults);
1873 if (CONSP (lface))
1874 lface = XCDR (lface);
1875 else if (signal_p)
1876 signal_error ("Invalid face", face_name);
1878 check_lface (lface);
1880 return lface;
1883 /* Return the face definition of FACE_NAME on frame F. F null means
1884 return the definition for new frames. FACE_NAME may be a string or
1885 a symbol (apparently Emacs 20.2 allowed strings as face names in
1886 face text properties; Ediff uses that). If FACE_NAME is an alias
1887 for another face, return that face's definition.
1888 If SIGNAL_P, signal an error if FACE_NAME is not a valid face name.
1889 Otherwise, value is nil if FACE_NAME is not a valid face name. */
1890 static Lisp_Object
1891 lface_from_face_name (struct frame *f, Lisp_Object face_name, bool signal_p)
1893 face_name = resolve_face_name (face_name, signal_p);
1894 return lface_from_face_name_no_resolve (f, face_name, signal_p);
1898 /* Get face attributes of face FACE_NAME from frame-local faces on
1899 frame F. Store the resulting attributes in ATTRS which must point
1900 to a vector of Lisp_Objects of size LFACE_VECTOR_SIZE.
1901 If SIGNAL_P, signal an error if FACE_NAME does not name a face.
1902 Otherwise, return true iff FACE_NAME is a face. */
1904 static bool
1905 get_lface_attributes_no_remap (struct frame *f, Lisp_Object face_name,
1906 Lisp_Object attrs[LFACE_VECTOR_SIZE],
1907 bool signal_p)
1909 Lisp_Object lface;
1911 lface = lface_from_face_name_no_resolve (f, face_name, signal_p);
1913 if (! NILP (lface))
1914 memcpy (attrs, XVECTOR (lface)->contents,
1915 LFACE_VECTOR_SIZE * sizeof *attrs);
1917 return !NILP (lface);
1920 /* Get face attributes of face FACE_NAME from frame-local faces on frame
1921 F. Store the resulting attributes in ATTRS which must point to a
1922 vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If FACE_NAME is an
1923 alias for another face, use that face's definition.
1924 If SIGNAL_P, signal an error if FACE_NAME does not name a face.
1925 Otherwise, return true iff FACE_NAME is a face. */
1927 static bool
1928 get_lface_attributes (struct frame *f, Lisp_Object face_name,
1929 Lisp_Object attrs[LFACE_VECTOR_SIZE], bool signal_p,
1930 struct named_merge_point *named_merge_points)
1932 Lisp_Object face_remapping;
1934 face_name = resolve_face_name (face_name, signal_p);
1936 /* See if SYMBOL has been remapped to some other face (usually this
1937 is done buffer-locally). */
1938 face_remapping = assq_no_quit (face_name, Vface_remapping_alist);
1939 if (CONSP (face_remapping))
1941 struct named_merge_point named_merge_point;
1943 if (push_named_merge_point (&named_merge_point,
1944 face_name, NAMED_MERGE_POINT_REMAP,
1945 &named_merge_points))
1947 int i;
1949 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
1950 attrs[i] = Qunspecified;
1952 return merge_face_ref (f, XCDR (face_remapping), attrs,
1953 signal_p, named_merge_points);
1957 /* Default case, no remapping. */
1958 return get_lface_attributes_no_remap (f, face_name, attrs, signal_p);
1962 /* True iff all attributes in face attribute vector ATTRS are
1963 specified, i.e. are non-nil. */
1965 static bool
1966 lface_fully_specified_p (Lisp_Object attrs[LFACE_VECTOR_SIZE])
1968 int i;
1970 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
1971 if (i != LFACE_FONT_INDEX && i != LFACE_INHERIT_INDEX
1972 && i != LFACE_DISTANT_FOREGROUND_INDEX)
1973 if ((UNSPECIFIEDP (attrs[i]) || IGNORE_DEFFACE_P (attrs[i])))
1974 break;
1976 return i == LFACE_VECTOR_SIZE;
1979 #ifdef HAVE_WINDOW_SYSTEM
1981 /* Set font-related attributes of Lisp face LFACE from FONT-OBJECT.
1982 If FORCE_P, set only unspecified attributes of LFACE. The
1983 exception is `font' attribute. It is set to FONT_OBJECT regardless
1984 of FORCE_P. */
1986 static void
1987 set_lface_from_font (struct frame *f, Lisp_Object lface,
1988 Lisp_Object font_object, bool force_p)
1990 Lisp_Object val;
1991 struct font *font = XFONT_OBJECT (font_object);
1993 /* Set attributes only if unspecified, otherwise face defaults for
1994 new frames would never take effect. If the font doesn't have a
1995 specific property, set a normal value for that. */
1997 if (force_p || UNSPECIFIEDP (LFACE_FAMILY (lface)))
1999 Lisp_Object family = AREF (font_object, FONT_FAMILY_INDEX);
2001 ASET (lface, LFACE_FAMILY_INDEX, SYMBOL_NAME (family));
2004 if (force_p || UNSPECIFIEDP (LFACE_FOUNDRY (lface)))
2006 Lisp_Object foundry = AREF (font_object, FONT_FOUNDRY_INDEX);
2008 ASET (lface, LFACE_FOUNDRY_INDEX, SYMBOL_NAME (foundry));
2011 if (force_p || UNSPECIFIEDP (LFACE_HEIGHT (lface)))
2013 int pt = PIXEL_TO_POINT (font->pixel_size * 10, FRAME_RES_Y (f));
2015 eassert (pt > 0);
2016 ASET (lface, LFACE_HEIGHT_INDEX, make_number (pt));
2019 if (force_p || UNSPECIFIEDP (LFACE_WEIGHT (lface)))
2021 val = FONT_WEIGHT_FOR_FACE (font_object);
2022 ASET (lface, LFACE_WEIGHT_INDEX, ! NILP (val) ? val :Qnormal);
2024 if (force_p || UNSPECIFIEDP (LFACE_SLANT (lface)))
2026 val = FONT_SLANT_FOR_FACE (font_object);
2027 ASET (lface, LFACE_SLANT_INDEX, ! NILP (val) ? val : Qnormal);
2029 if (force_p || UNSPECIFIEDP (LFACE_SWIDTH (lface)))
2031 val = FONT_WIDTH_FOR_FACE (font_object);
2032 ASET (lface, LFACE_SWIDTH_INDEX, ! NILP (val) ? val : Qnormal);
2035 ASET (lface, LFACE_FONT_INDEX, font_object);
2038 #endif /* HAVE_WINDOW_SYSTEM */
2041 /* Merges the face height FROM with the face height TO, and returns the
2042 merged height. If FROM is an invalid height, then INVALID is
2043 returned instead. FROM and TO may be either absolute face heights or
2044 `relative' heights; the returned value is always an absolute height
2045 unless both FROM and TO are relative. */
2047 static Lisp_Object
2048 merge_face_heights (Lisp_Object from, Lisp_Object to, Lisp_Object invalid)
2050 Lisp_Object result = invalid;
2052 if (INTEGERP (from))
2053 /* FROM is absolute, just use it as is. */
2054 result = from;
2055 else if (FLOATP (from))
2056 /* FROM is a scale, use it to adjust TO. */
2058 if (INTEGERP (to))
2059 /* relative X absolute => absolute */
2060 result = make_number (XFLOAT_DATA (from) * XINT (to));
2061 else if (FLOATP (to))
2062 /* relative X relative => relative */
2063 result = make_float (XFLOAT_DATA (from) * XFLOAT_DATA (to));
2064 else if (UNSPECIFIEDP (to))
2065 result = from;
2067 else if (FUNCTIONP (from))
2068 /* FROM is a function, which use to adjust TO. */
2070 /* Call function with current height as argument.
2071 From is the new height. */
2072 result = safe_call1 (from, to);
2074 /* Ensure that if TO was absolute, so is the result. */
2075 if (INTEGERP (to) && !INTEGERP (result))
2076 result = invalid;
2079 return result;
2083 /* Merge two Lisp face attribute vectors on frame F, FROM and TO, and
2084 store the resulting attributes in TO, which must be already be
2085 completely specified and contain only absolute attributes. Every
2086 specified attribute of FROM overrides the corresponding attribute of
2087 TO; relative attributes in FROM are merged with the absolute value in
2088 TO and replace it. NAMED_MERGE_POINTS is used internally to detect
2089 loops in face inheritance/remapping; it should be 0 when called from
2090 other places. */
2092 static void
2093 merge_face_vectors (struct frame *f, Lisp_Object *from, Lisp_Object *to,
2094 struct named_merge_point *named_merge_points)
2096 int i;
2097 Lisp_Object font = Qnil;
2099 /* If FROM inherits from some other faces, merge their attributes into
2100 TO before merging FROM's direct attributes. Note that an :inherit
2101 attribute of `unspecified' is the same as one of nil; we never
2102 merge :inherit attributes, so nil is more correct, but lots of
2103 other code uses `unspecified' as a generic value for face attributes. */
2104 if (!UNSPECIFIEDP (from[LFACE_INHERIT_INDEX])
2105 && !NILP (from[LFACE_INHERIT_INDEX]))
2106 merge_face_ref (f, from[LFACE_INHERIT_INDEX], to, false, named_merge_points);
2108 if (FONT_SPEC_P (from[LFACE_FONT_INDEX]))
2110 if (!UNSPECIFIEDP (to[LFACE_FONT_INDEX]))
2111 font = merge_font_spec (from[LFACE_FONT_INDEX], to[LFACE_FONT_INDEX]);
2112 else
2113 font = copy_font_spec (from[LFACE_FONT_INDEX]);
2114 to[LFACE_FONT_INDEX] = font;
2117 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2118 if (!UNSPECIFIEDP (from[i]))
2120 if (i == LFACE_HEIGHT_INDEX && !INTEGERP (from[i]))
2122 to[i] = merge_face_heights (from[i], to[i], to[i]);
2123 font_clear_prop (to, FONT_SIZE_INDEX);
2125 else if (i != LFACE_FONT_INDEX && ! EQ (to[i], from[i]))
2127 to[i] = from[i];
2128 if (i >= LFACE_FAMILY_INDEX && i <=LFACE_SLANT_INDEX)
2129 font_clear_prop (to,
2130 (i == LFACE_FAMILY_INDEX ? FONT_FAMILY_INDEX
2131 : i == LFACE_FOUNDRY_INDEX ? FONT_FOUNDRY_INDEX
2132 : i == LFACE_SWIDTH_INDEX ? FONT_WIDTH_INDEX
2133 : i == LFACE_HEIGHT_INDEX ? FONT_SIZE_INDEX
2134 : i == LFACE_WEIGHT_INDEX ? FONT_WEIGHT_INDEX
2135 : FONT_SLANT_INDEX));
2139 /* If FROM specifies a font spec, make its contents take precedence
2140 over :family and other attributes. This is needed for face
2141 remapping using :font to work. */
2143 if (!NILP (font))
2145 if (! NILP (AREF (font, FONT_FOUNDRY_INDEX)))
2146 to[LFACE_FOUNDRY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FOUNDRY_INDEX));
2147 if (! NILP (AREF (font, FONT_FAMILY_INDEX)))
2148 to[LFACE_FAMILY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FAMILY_INDEX));
2149 if (! NILP (AREF (font, FONT_WEIGHT_INDEX)))
2150 to[LFACE_WEIGHT_INDEX] = FONT_WEIGHT_FOR_FACE (font);
2151 if (! NILP (AREF (font, FONT_SLANT_INDEX)))
2152 to[LFACE_SLANT_INDEX] = FONT_SLANT_FOR_FACE (font);
2153 if (! NILP (AREF (font, FONT_WIDTH_INDEX)))
2154 to[LFACE_SWIDTH_INDEX] = FONT_WIDTH_FOR_FACE (font);
2155 ASET (font, FONT_SIZE_INDEX, Qnil);
2158 /* TO is always an absolute face, which should inherit from nothing.
2159 We blindly copy the :inherit attribute above and fix it up here. */
2160 to[LFACE_INHERIT_INDEX] = Qnil;
2163 /* Merge the named face FACE_NAME on frame F, into the vector of face
2164 attributes TO. Use NAMED_MERGE_POINTS to detect loops in face
2165 inheritance. Return true if FACE_NAME is a valid face name and
2166 merging succeeded. */
2168 static bool
2169 merge_named_face (struct frame *f, Lisp_Object face_name, Lisp_Object *to,
2170 struct named_merge_point *named_merge_points)
2172 struct named_merge_point named_merge_point;
2174 if (push_named_merge_point (&named_merge_point,
2175 face_name, NAMED_MERGE_POINT_NORMAL,
2176 &named_merge_points))
2178 Lisp_Object from[LFACE_VECTOR_SIZE];
2179 bool ok = get_lface_attributes (f, face_name, from, false,
2180 named_merge_points);
2182 if (ok)
2183 merge_face_vectors (f, from, to, named_merge_points);
2185 return ok;
2187 else
2188 return false;
2192 /* Merge face attributes from the lisp `face reference' FACE_REF on
2193 frame F into the face attribute vector TO. If ERR_MSGS,
2194 problems with FACE_REF cause an error message to be shown. Return
2195 true if no errors occurred (regardless of the value of ERR_MSGS).
2196 Use NAMED_MERGE_POINTS to detect loops in face inheritance or
2197 list structure; it may be 0 for most callers.
2199 FACE_REF may be a single face specification or a list of such
2200 specifications. Each face specification can be:
2202 1. A symbol or string naming a Lisp face.
2204 2. A property list of the form (KEYWORD VALUE ...) where each
2205 KEYWORD is a face attribute name, and value is an appropriate value
2206 for that attribute.
2208 3. Conses or the form (FOREGROUND-COLOR . COLOR) or
2209 (BACKGROUND-COLOR . COLOR) where COLOR is a color name. This is
2210 for compatibility with 20.2.
2212 Face specifications earlier in lists take precedence over later
2213 specifications. */
2215 static bool
2216 merge_face_ref (struct frame *f, Lisp_Object face_ref, Lisp_Object *to,
2217 bool err_msgs, struct named_merge_point *named_merge_points)
2219 bool ok = true; /* Succeed without an error? */
2221 if (CONSP (face_ref))
2223 Lisp_Object first = XCAR (face_ref);
2225 if (EQ (first, Qforeground_color)
2226 || EQ (first, Qbackground_color))
2228 /* One of (FOREGROUND-COLOR . COLOR) or (BACKGROUND-COLOR
2229 . COLOR). COLOR must be a string. */
2230 Lisp_Object color_name = XCDR (face_ref);
2231 Lisp_Object color = first;
2233 if (STRINGP (color_name))
2235 if (EQ (color, Qforeground_color))
2236 to[LFACE_FOREGROUND_INDEX] = color_name;
2237 else
2238 to[LFACE_BACKGROUND_INDEX] = color_name;
2240 else
2242 if (err_msgs)
2243 add_to_log ("Invalid face color %S", color_name);
2244 ok = false;
2247 else if (SYMBOLP (first)
2248 && *SDATA (SYMBOL_NAME (first)) == ':')
2250 /* Assume this is the property list form. */
2251 while (CONSP (face_ref) && CONSP (XCDR (face_ref)))
2253 Lisp_Object keyword = XCAR (face_ref);
2254 Lisp_Object value = XCAR (XCDR (face_ref));
2255 bool err = false;
2257 /* Specifying `unspecified' is a no-op. */
2258 if (EQ (value, Qunspecified))
2260 else if (EQ (keyword, QCfamily))
2262 if (STRINGP (value))
2264 to[LFACE_FAMILY_INDEX] = value;
2265 font_clear_prop (to, FONT_FAMILY_INDEX);
2267 else
2268 err = true;
2270 else if (EQ (keyword, QCfoundry))
2272 if (STRINGP (value))
2274 to[LFACE_FOUNDRY_INDEX] = value;
2275 font_clear_prop (to, FONT_FOUNDRY_INDEX);
2277 else
2278 err = true;
2280 else if (EQ (keyword, QCheight))
2282 Lisp_Object new_height =
2283 merge_face_heights (value, to[LFACE_HEIGHT_INDEX], Qnil);
2285 if (! NILP (new_height))
2287 to[LFACE_HEIGHT_INDEX] = new_height;
2288 font_clear_prop (to, FONT_SIZE_INDEX);
2290 else
2291 err = true;
2293 else if (EQ (keyword, QCweight))
2295 if (SYMBOLP (value) && FONT_WEIGHT_NAME_NUMERIC (value) >= 0)
2297 to[LFACE_WEIGHT_INDEX] = value;
2298 font_clear_prop (to, FONT_WEIGHT_INDEX);
2300 else
2301 err = true;
2303 else if (EQ (keyword, QCslant))
2305 if (SYMBOLP (value) && FONT_SLANT_NAME_NUMERIC (value) >= 0)
2307 to[LFACE_SLANT_INDEX] = value;
2308 font_clear_prop (to, FONT_SLANT_INDEX);
2310 else
2311 err = true;
2313 else if (EQ (keyword, QCunderline))
2315 if (EQ (value, Qt)
2316 || NILP (value)
2317 || STRINGP (value)
2318 || CONSP (value))
2319 to[LFACE_UNDERLINE_INDEX] = value;
2320 else
2321 err = true;
2323 else if (EQ (keyword, QCoverline))
2325 if (EQ (value, Qt)
2326 || NILP (value)
2327 || STRINGP (value))
2328 to[LFACE_OVERLINE_INDEX] = value;
2329 else
2330 err = true;
2332 else if (EQ (keyword, QCstrike_through))
2334 if (EQ (value, Qt)
2335 || NILP (value)
2336 || STRINGP (value))
2337 to[LFACE_STRIKE_THROUGH_INDEX] = value;
2338 else
2339 err = true;
2341 else if (EQ (keyword, QCbox))
2343 if (EQ (value, Qt))
2344 value = make_number (1);
2345 if (INTEGERP (value)
2346 || STRINGP (value)
2347 || CONSP (value)
2348 || NILP (value))
2349 to[LFACE_BOX_INDEX] = value;
2350 else
2351 err = true;
2353 else if (EQ (keyword, QCinverse_video)
2354 || EQ (keyword, QCreverse_video))
2356 if (EQ (value, Qt) || NILP (value))
2357 to[LFACE_INVERSE_INDEX] = value;
2358 else
2359 err = true;
2361 else if (EQ (keyword, QCforeground))
2363 if (STRINGP (value))
2364 to[LFACE_FOREGROUND_INDEX] = value;
2365 else
2366 err = true;
2368 else if (EQ (keyword, QCdistant_foreground))
2370 if (STRINGP (value))
2371 to[LFACE_DISTANT_FOREGROUND_INDEX] = value;
2372 else
2373 err = true;
2375 else if (EQ (keyword, QCbackground))
2377 if (STRINGP (value))
2378 to[LFACE_BACKGROUND_INDEX] = value;
2379 else
2380 err = true;
2382 else if (EQ (keyword, QCstipple))
2384 #if defined (HAVE_WINDOW_SYSTEM)
2385 Lisp_Object pixmap_p = Fbitmap_spec_p (value);
2386 if (!NILP (pixmap_p))
2387 to[LFACE_STIPPLE_INDEX] = value;
2388 else
2389 err = true;
2390 #endif /* HAVE_WINDOW_SYSTEM */
2392 else if (EQ (keyword, QCwidth))
2394 if (SYMBOLP (value) && FONT_WIDTH_NAME_NUMERIC (value) >= 0)
2396 to[LFACE_SWIDTH_INDEX] = value;
2397 font_clear_prop (to, FONT_WIDTH_INDEX);
2399 else
2400 err = true;
2402 else if (EQ (keyword, QCfont))
2404 if (FONTP (value))
2405 to[LFACE_FONT_INDEX] = value;
2406 else
2407 err = true;
2409 else if (EQ (keyword, QCinherit))
2411 /* This is not really very useful; it's just like a
2412 normal face reference. */
2413 if (! merge_face_ref (f, value, to,
2414 err_msgs, named_merge_points))
2415 err = true;
2417 else
2418 err = true;
2420 if (err)
2422 add_to_log ("Invalid face attribute %S %S", keyword, value);
2423 ok = false;
2426 face_ref = XCDR (XCDR (face_ref));
2429 else
2431 /* This is a list of face refs. Those at the beginning of the
2432 list take precedence over what follows, so we have to merge
2433 from the end backwards. */
2434 Lisp_Object next = XCDR (face_ref);
2436 if (! NILP (next))
2437 ok = merge_face_ref (f, next, to, err_msgs, named_merge_points);
2439 if (! merge_face_ref (f, first, to, err_msgs, named_merge_points))
2440 ok = false;
2443 else
2445 /* FACE_REF ought to be a face name. */
2446 ok = merge_named_face (f, face_ref, to, named_merge_points);
2447 if (!ok && err_msgs)
2448 add_to_log ("Invalid face reference: %s", face_ref);
2451 return ok;
2455 DEFUN ("internal-make-lisp-face", Finternal_make_lisp_face,
2456 Sinternal_make_lisp_face, 1, 2, 0,
2457 doc: /* Make FACE, a symbol, a Lisp face with all attributes nil.
2458 If FACE was not known as a face before, create a new one.
2459 If optional argument FRAME is specified, make a frame-local face
2460 for that frame. Otherwise operate on the global face definition.
2461 Value is a vector of face attributes. */)
2462 (Lisp_Object face, Lisp_Object frame)
2464 Lisp_Object global_lface, lface;
2465 struct frame *f;
2466 int i;
2468 CHECK_SYMBOL (face);
2469 global_lface = lface_from_face_name (NULL, face, false);
2471 if (!NILP (frame))
2473 CHECK_LIVE_FRAME (frame);
2474 f = XFRAME (frame);
2475 lface = lface_from_face_name (f, face, false);
2477 else
2478 f = NULL, lface = Qnil;
2480 /* Add a global definition if there is none. */
2481 if (NILP (global_lface))
2483 global_lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2484 Qunspecified);
2485 ASET (global_lface, 0, Qface);
2486 Vface_new_frame_defaults = Fcons (Fcons (face, global_lface),
2487 Vface_new_frame_defaults);
2489 /* Assign the new Lisp face a unique ID. The mapping from Lisp
2490 face id to Lisp face is given by the vector lface_id_to_name.
2491 The mapping from Lisp face to Lisp face id is given by the
2492 property `face' of the Lisp face name. */
2493 if (next_lface_id == lface_id_to_name_size)
2494 lface_id_to_name =
2495 xpalloc (lface_id_to_name, &lface_id_to_name_size, 1, MAX_FACE_ID,
2496 sizeof *lface_id_to_name);
2498 lface_id_to_name[next_lface_id] = face;
2499 Fput (face, Qface, make_number (next_lface_id));
2500 ++next_lface_id;
2502 else if (f == NULL)
2503 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2504 ASET (global_lface, i, Qunspecified);
2506 /* Add a frame-local definition. */
2507 if (f)
2509 if (NILP (lface))
2511 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2512 Qunspecified);
2513 ASET (lface, 0, Qface);
2514 fset_face_alist (f, Fcons (Fcons (face, lface), f->face_alist));
2516 else
2517 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2518 ASET (lface, i, Qunspecified);
2520 else
2521 lface = global_lface;
2523 /* Changing a named face means that all realized faces depending on
2524 that face are invalid. Since we cannot tell which realized faces
2525 depend on the face, make sure they are all removed. This is done
2526 by setting face_change. The next call to init_iterator will then
2527 free realized faces. */
2528 if (NILP (Fget (face, Qface_no_inherit)))
2530 if (f)
2532 f->face_change = true;
2533 fset_redisplay (f);
2535 else
2537 face_change = true;
2538 windows_or_buffers_changed = 54;
2542 eassert (LFACEP (lface));
2543 check_lface (lface);
2544 return lface;
2548 DEFUN ("internal-lisp-face-p", Finternal_lisp_face_p,
2549 Sinternal_lisp_face_p, 1, 2, 0,
2550 doc: /* Return non-nil if FACE names a face.
2551 FACE should be a symbol or string.
2552 If optional second argument FRAME is non-nil, check for the
2553 existence of a frame-local face with name FACE on that frame.
2554 Otherwise check for the existence of a global face. */)
2555 (Lisp_Object face, Lisp_Object frame)
2557 Lisp_Object lface;
2559 face = resolve_face_name (face, true);
2561 if (!NILP (frame))
2563 CHECK_LIVE_FRAME (frame);
2564 lface = lface_from_face_name (XFRAME (frame), face, false);
2566 else
2567 lface = lface_from_face_name (NULL, face, false);
2569 return lface;
2573 DEFUN ("internal-copy-lisp-face", Finternal_copy_lisp_face,
2574 Sinternal_copy_lisp_face, 4, 4, 0,
2575 doc: /* Copy face FROM to TO.
2576 If FRAME is t, copy the global face definition of FROM.
2577 Otherwise, copy the frame-local definition of FROM on FRAME.
2578 If NEW-FRAME is a frame, copy that data into the frame-local
2579 definition of TO on NEW-FRAME. If NEW-FRAME is nil,
2580 FRAME controls where the data is copied to.
2582 The value is TO. */)
2583 (Lisp_Object from, Lisp_Object to, Lisp_Object frame, Lisp_Object new_frame)
2585 Lisp_Object lface, copy;
2586 struct frame *f;
2588 CHECK_SYMBOL (from);
2589 CHECK_SYMBOL (to);
2591 if (EQ (frame, Qt))
2593 /* Copy global definition of FROM. We don't make copies of
2594 strings etc. because 20.2 didn't do it either. */
2595 lface = lface_from_face_name (NULL, from, true);
2596 copy = Finternal_make_lisp_face (to, Qnil);
2597 f = NULL;
2599 else
2601 /* Copy frame-local definition of FROM. */
2602 if (NILP (new_frame))
2603 new_frame = frame;
2604 CHECK_LIVE_FRAME (frame);
2605 CHECK_LIVE_FRAME (new_frame);
2606 lface = lface_from_face_name (XFRAME (frame), from, true);
2607 copy = Finternal_make_lisp_face (to, new_frame);
2608 f = XFRAME (new_frame);
2611 vcopy (copy, 0, XVECTOR (lface)->contents, LFACE_VECTOR_SIZE);
2613 /* Changing a named face means that all realized faces depending on
2614 that face are invalid. Since we cannot tell which realized faces
2615 depend on the face, make sure they are all removed. This is done
2616 by setting face_change. The next call to init_iterator will then
2617 free realized faces. */
2618 if (NILP (Fget (to, Qface_no_inherit)))
2620 if (f)
2622 f->face_change = true;
2623 fset_redisplay (f);
2625 else
2627 face_change = true;
2628 windows_or_buffers_changed = 55;
2632 return to;
2636 DEFUN ("internal-set-lisp-face-attribute", Finternal_set_lisp_face_attribute,
2637 Sinternal_set_lisp_face_attribute, 3, 4, 0,
2638 doc: /* Set attribute ATTR of FACE to VALUE.
2639 FRAME being a frame means change the face on that frame.
2640 FRAME nil means change the face of the selected frame.
2641 FRAME t means change the default for new frames.
2642 FRAME 0 means change the face on all frames, and change the default
2643 for new frames. */)
2644 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
2646 Lisp_Object lface;
2647 Lisp_Object old_value = Qnil;
2648 /* Set one of enum font_property_index (> 0) if ATTR is one of
2649 font-related attributes other than QCfont and QCfontset. */
2650 enum font_property_index prop_index = 0;
2651 struct frame *f;
2653 CHECK_SYMBOL (face);
2654 CHECK_SYMBOL (attr);
2656 face = resolve_face_name (face, true);
2658 /* If FRAME is 0, change face on all frames, and change the
2659 default for new frames. */
2660 if (INTEGERP (frame) && XINT (frame) == 0)
2662 Lisp_Object tail;
2663 Finternal_set_lisp_face_attribute (face, attr, value, Qt);
2664 FOR_EACH_FRAME (tail, frame)
2665 Finternal_set_lisp_face_attribute (face, attr, value, frame);
2666 return face;
2669 /* Set lface to the Lisp attribute vector of FACE. */
2670 if (EQ (frame, Qt))
2672 f = NULL;
2673 lface = lface_from_face_name (NULL, face, true);
2675 /* When updating face-new-frame-defaults, we put :ignore-defface
2676 where the caller wants `unspecified'. This forces the frame
2677 defaults to ignore the defface value. Otherwise, the defface
2678 will take effect, which is generally not what is intended.
2679 The value of that attribute will be inherited from some other
2680 face during face merging. See internal_merge_in_global_face. */
2681 if (UNSPECIFIEDP (value))
2682 value = QCignore_defface;
2684 else
2686 if (NILP (frame))
2687 frame = selected_frame;
2688 f = XFRAME (frame);
2690 CHECK_LIVE_FRAME (frame);
2691 lface = lface_from_face_name (f, face, false);
2693 /* If a frame-local face doesn't exist yet, create one. */
2694 if (NILP (lface))
2695 lface = Finternal_make_lisp_face (face, frame);
2698 if (EQ (attr, QCfamily))
2700 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2702 CHECK_STRING (value);
2703 if (SCHARS (value) == 0)
2704 signal_error ("Invalid face family", value);
2706 old_value = LFACE_FAMILY (lface);
2707 ASET (lface, LFACE_FAMILY_INDEX, value);
2708 prop_index = FONT_FAMILY_INDEX;
2710 else if (EQ (attr, QCfoundry))
2712 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2714 CHECK_STRING (value);
2715 if (SCHARS (value) == 0)
2716 signal_error ("Invalid face foundry", value);
2718 old_value = LFACE_FOUNDRY (lface);
2719 ASET (lface, LFACE_FOUNDRY_INDEX, value);
2720 prop_index = FONT_FOUNDRY_INDEX;
2722 else if (EQ (attr, QCheight))
2724 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2726 if (EQ (face, Qdefault))
2728 /* The default face must have an absolute size. */
2729 if (!INTEGERP (value) || XINT (value) <= 0)
2730 signal_error ("Default face height not absolute and positive",
2731 value);
2733 else
2735 /* For non-default faces, do a test merge with a random
2736 height to see if VALUE's ok. */
2737 Lisp_Object test = merge_face_heights (value,
2738 make_number (10),
2739 Qnil);
2740 if (!INTEGERP (test) || XINT (test) <= 0)
2741 signal_error ("Face height does not produce a positive integer",
2742 value);
2746 old_value = LFACE_HEIGHT (lface);
2747 ASET (lface, LFACE_HEIGHT_INDEX, value);
2748 prop_index = FONT_SIZE_INDEX;
2750 else if (EQ (attr, QCweight))
2752 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2754 CHECK_SYMBOL (value);
2755 if (FONT_WEIGHT_NAME_NUMERIC (value) < 0)
2756 signal_error ("Invalid face weight", value);
2758 old_value = LFACE_WEIGHT (lface);
2759 ASET (lface, LFACE_WEIGHT_INDEX, value);
2760 prop_index = FONT_WEIGHT_INDEX;
2762 else if (EQ (attr, QCslant))
2764 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2766 CHECK_SYMBOL (value);
2767 if (FONT_SLANT_NAME_NUMERIC (value) < 0)
2768 signal_error ("Invalid face slant", value);
2770 old_value = LFACE_SLANT (lface);
2771 ASET (lface, LFACE_SLANT_INDEX, value);
2772 prop_index = FONT_SLANT_INDEX;
2774 else if (EQ (attr, QCunderline))
2776 bool valid_p = false;
2778 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2779 valid_p = true;
2780 else if (NILP (value) || EQ (value, Qt))
2781 valid_p = true;
2782 else if (STRINGP (value) && SCHARS (value) > 0)
2783 valid_p = true;
2784 else if (CONSP (value))
2786 Lisp_Object key, val, list;
2788 list = value;
2789 /* FIXME? This errs on the side of acceptance. Eg it accepts:
2790 (defface foo '((t :underline 'foo) "doc")
2791 Maybe this is intentional, maybe it isn't.
2792 Non-nil symbols other than t are not documented as being valid.
2793 Eg compare with inverse-video, which explicitly rejects them.
2795 valid_p = true;
2797 while (!NILP (CAR_SAFE(list)))
2799 key = CAR_SAFE (list);
2800 list = CDR_SAFE (list);
2801 val = CAR_SAFE (list);
2802 list = CDR_SAFE (list);
2804 if (NILP (key) || NILP (val))
2806 valid_p = false;
2807 break;
2810 else if (EQ (key, QCcolor)
2811 && !(EQ (val, Qforeground_color)
2812 || (STRINGP (val) && SCHARS (val) > 0)))
2814 valid_p = false;
2815 break;
2818 else if (EQ (key, QCstyle)
2819 && !(EQ (val, Qline) || EQ (val, Qwave)))
2821 valid_p = false;
2822 break;
2827 if (!valid_p)
2828 signal_error ("Invalid face underline", value);
2830 old_value = LFACE_UNDERLINE (lface);
2831 ASET (lface, LFACE_UNDERLINE_INDEX, value);
2833 else if (EQ (attr, QCoverline))
2835 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2836 if ((SYMBOLP (value)
2837 && !EQ (value, Qt)
2838 && !EQ (value, Qnil))
2839 /* Overline color. */
2840 || (STRINGP (value)
2841 && SCHARS (value) == 0))
2842 signal_error ("Invalid face overline", value);
2844 old_value = LFACE_OVERLINE (lface);
2845 ASET (lface, LFACE_OVERLINE_INDEX, value);
2847 else if (EQ (attr, QCstrike_through))
2849 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2850 if ((SYMBOLP (value)
2851 && !EQ (value, Qt)
2852 && !EQ (value, Qnil))
2853 /* Strike-through color. */
2854 || (STRINGP (value)
2855 && SCHARS (value) == 0))
2856 signal_error ("Invalid face strike-through", value);
2858 old_value = LFACE_STRIKE_THROUGH (lface);
2859 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, value);
2861 else if (EQ (attr, QCbox))
2863 bool valid_p;
2865 /* Allow t meaning a simple box of width 1 in foreground color
2866 of the face. */
2867 if (EQ (value, Qt))
2868 value = make_number (1);
2870 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2871 valid_p = true;
2872 else if (NILP (value))
2873 valid_p = true;
2874 else if (INTEGERP (value))
2875 valid_p = XINT (value) != 0;
2876 else if (STRINGP (value))
2877 valid_p = SCHARS (value) > 0;
2878 else if (CONSP (value))
2880 Lisp_Object tem;
2882 tem = value;
2883 while (CONSP (tem))
2885 Lisp_Object k, v;
2887 k = XCAR (tem);
2888 tem = XCDR (tem);
2889 if (!CONSP (tem))
2890 break;
2891 v = XCAR (tem);
2892 tem = XCDR (tem);
2894 if (EQ (k, QCline_width))
2896 if (!INTEGERP (v) || XINT (v) == 0)
2897 break;
2899 else if (EQ (k, QCcolor))
2901 if (!NILP (v) && (!STRINGP (v) || SCHARS (v) == 0))
2902 break;
2904 else if (EQ (k, QCstyle))
2906 if (!EQ (v, Qpressed_button) && !EQ (v, Qreleased_button))
2907 break;
2909 else
2910 break;
2913 valid_p = NILP (tem);
2915 else
2916 valid_p = false;
2918 if (!valid_p)
2919 signal_error ("Invalid face box", value);
2921 old_value = LFACE_BOX (lface);
2922 ASET (lface, LFACE_BOX_INDEX, value);
2924 else if (EQ (attr, QCinverse_video)
2925 || EQ (attr, QCreverse_video))
2927 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2929 CHECK_SYMBOL (value);
2930 if (!EQ (value, Qt) && !NILP (value))
2931 signal_error ("Invalid inverse-video face attribute value", value);
2933 old_value = LFACE_INVERSE (lface);
2934 ASET (lface, LFACE_INVERSE_INDEX, value);
2936 else if (EQ (attr, QCforeground))
2938 /* Compatibility with 20.x. */
2939 if (NILP (value))
2940 value = Qunspecified;
2941 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2943 /* Don't check for valid color names here because it depends
2944 on the frame (display) whether the color will be valid
2945 when the face is realized. */
2946 CHECK_STRING (value);
2947 if (SCHARS (value) == 0)
2948 signal_error ("Empty foreground color value", value);
2950 old_value = LFACE_FOREGROUND (lface);
2951 ASET (lface, LFACE_FOREGROUND_INDEX, value);
2953 else if (EQ (attr, QCdistant_foreground))
2955 /* Compatibility with 20.x. */
2956 if (NILP (value))
2957 value = Qunspecified;
2958 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2960 /* Don't check for valid color names here because it depends
2961 on the frame (display) whether the color will be valid
2962 when the face is realized. */
2963 CHECK_STRING (value);
2964 if (SCHARS (value) == 0)
2965 signal_error ("Empty distant-foreground color value", value);
2967 old_value = LFACE_DISTANT_FOREGROUND (lface);
2968 ASET (lface, LFACE_DISTANT_FOREGROUND_INDEX, value);
2970 else if (EQ (attr, QCbackground))
2972 /* Compatibility with 20.x. */
2973 if (NILP (value))
2974 value = Qunspecified;
2975 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2977 /* Don't check for valid color names here because it depends
2978 on the frame (display) whether the color will be valid
2979 when the face is realized. */
2980 CHECK_STRING (value);
2981 if (SCHARS (value) == 0)
2982 signal_error ("Empty background color value", value);
2984 old_value = LFACE_BACKGROUND (lface);
2985 ASET (lface, LFACE_BACKGROUND_INDEX, value);
2987 else if (EQ (attr, QCstipple))
2989 #if defined (HAVE_WINDOW_SYSTEM)
2990 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
2991 && !NILP (value)
2992 && NILP (Fbitmap_spec_p (value)))
2993 signal_error ("Invalid stipple attribute", value);
2994 old_value = LFACE_STIPPLE (lface);
2995 ASET (lface, LFACE_STIPPLE_INDEX, value);
2996 #endif /* HAVE_WINDOW_SYSTEM */
2998 else if (EQ (attr, QCwidth))
3000 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3002 CHECK_SYMBOL (value);
3003 if (FONT_WIDTH_NAME_NUMERIC (value) < 0)
3004 signal_error ("Invalid face width", value);
3006 old_value = LFACE_SWIDTH (lface);
3007 ASET (lface, LFACE_SWIDTH_INDEX, value);
3008 prop_index = FONT_WIDTH_INDEX;
3010 else if (EQ (attr, QCfont))
3012 #ifdef HAVE_WINDOW_SYSTEM
3013 if (EQ (frame, Qt) || FRAME_WINDOW_P (f))
3015 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3017 struct frame *f1;
3019 old_value = LFACE_FONT (lface);
3020 if (! FONTP (value))
3022 if (STRINGP (value))
3024 Lisp_Object name = value;
3025 int fontset = fs_query_fontset (name, 0);
3027 if (fontset >= 0)
3028 name = fontset_ascii (fontset);
3029 value = font_spec_from_name (name);
3030 if (!FONTP (value))
3031 signal_error ("Invalid font name", name);
3033 else
3034 signal_error ("Invalid font or font-spec", value);
3036 if (EQ (frame, Qt))
3037 f1 = XFRAME (selected_frame);
3038 else
3039 f1 = XFRAME (frame);
3041 /* FIXME:
3042 If frame is t, and selected frame is a tty frame, the font
3043 can't be realized. An improvement would be to loop over frames
3044 for a non-tty frame and use that. See discussion in Bug#18573.
3045 For a daemon, frame may be an initial frame (Bug#18869). */
3046 if (FRAME_WINDOW_P (f1))
3048 if (! FONT_OBJECT_P (value))
3050 Lisp_Object *attrs = XVECTOR (lface)->contents;
3051 Lisp_Object font_object;
3053 font_object = font_load_for_lface (f1, attrs, value);
3054 if (NILP (font_object))
3055 signal_error ("Font not available", value);
3056 value = font_object;
3058 set_lface_from_font (f1, lface, value, true);
3059 f1->face_change = 1;
3062 else
3063 ASET (lface, LFACE_FONT_INDEX, value);
3065 #endif /* HAVE_WINDOW_SYSTEM */
3067 else if (EQ (attr, QCfontset))
3069 #ifdef HAVE_WINDOW_SYSTEM
3070 if (EQ (frame, Qt) || FRAME_WINDOW_P (f))
3072 Lisp_Object tmp;
3074 old_value = LFACE_FONTSET (lface);
3075 tmp = Fquery_fontset (value, Qnil);
3076 if (NILP (tmp))
3077 signal_error ("Invalid fontset name", value);
3078 ASET (lface, LFACE_FONTSET_INDEX, value = tmp);
3080 #endif /* HAVE_WINDOW_SYSTEM */
3082 else if (EQ (attr, QCinherit))
3084 Lisp_Object tail;
3085 if (SYMBOLP (value))
3086 tail = Qnil;
3087 else
3088 for (tail = value; CONSP (tail); tail = XCDR (tail))
3089 if (!SYMBOLP (XCAR (tail)))
3090 break;
3091 if (NILP (tail))
3092 ASET (lface, LFACE_INHERIT_INDEX, value);
3093 else
3094 signal_error ("Invalid face inheritance", value);
3096 else if (EQ (attr, QCbold))
3098 old_value = LFACE_WEIGHT (lface);
3099 ASET (lface, LFACE_WEIGHT_INDEX, NILP (value) ? Qnormal : Qbold);
3100 prop_index = FONT_WEIGHT_INDEX;
3102 else if (EQ (attr, QCitalic))
3104 attr = QCslant;
3105 old_value = LFACE_SLANT (lface);
3106 ASET (lface, LFACE_SLANT_INDEX, NILP (value) ? Qnormal : Qitalic);
3107 prop_index = FONT_SLANT_INDEX;
3109 else
3110 signal_error ("Invalid face attribute name", attr);
3112 if (prop_index)
3114 /* If a font-related attribute other than QCfont and QCfontset
3115 is specified, and if the original QCfont attribute has a font
3116 (font-spec or font-object), set the corresponding property in
3117 the font to nil so that the font selector doesn't think that
3118 the attribute is mandatory. Also, clear the average
3119 width. */
3120 font_clear_prop (XVECTOR (lface)->contents, prop_index);
3123 /* Changing a named face means that all realized faces depending on
3124 that face are invalid. Since we cannot tell which realized faces
3125 depend on the face, make sure they are all removed. This is done
3126 by setting face_change. The next call to init_iterator will then
3127 free realized faces. */
3128 if (!EQ (frame, Qt)
3129 && NILP (Fget (face, Qface_no_inherit))
3130 && NILP (Fequal (old_value, value)))
3132 f->face_change = true;
3133 fset_redisplay (f);
3136 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3137 && NILP (Fequal (old_value, value)))
3139 Lisp_Object param;
3141 param = Qnil;
3143 if (EQ (face, Qdefault))
3145 #ifdef HAVE_WINDOW_SYSTEM
3146 /* Changed font-related attributes of the `default' face are
3147 reflected in changed `font' frame parameters. */
3148 if (FRAMEP (frame)
3149 && (prop_index || EQ (attr, QCfont))
3150 && lface_fully_specified_p (XVECTOR (lface)->contents))
3151 set_font_frame_param (frame, lface);
3152 else
3153 #endif /* HAVE_WINDOW_SYSTEM */
3155 if (EQ (attr, QCforeground))
3156 param = Qforeground_color;
3157 else if (EQ (attr, QCbackground))
3158 param = Qbackground_color;
3160 #ifdef HAVE_WINDOW_SYSTEM
3161 #ifndef HAVE_NTGUI
3162 else if (EQ (face, Qscroll_bar))
3164 /* Changing the colors of `scroll-bar' sets frame parameters
3165 `scroll-bar-foreground' and `scroll-bar-background'. */
3166 if (EQ (attr, QCforeground))
3167 param = Qscroll_bar_foreground;
3168 else if (EQ (attr, QCbackground))
3169 param = Qscroll_bar_background;
3171 #endif /* not HAVE_NTGUI */
3172 else if (EQ (face, Qborder))
3174 /* Changing background color of `border' sets frame parameter
3175 `border-color'. */
3176 if (EQ (attr, QCbackground))
3177 param = Qborder_color;
3179 else if (EQ (face, Qcursor))
3181 /* Changing background color of `cursor' sets frame parameter
3182 `cursor-color'. */
3183 if (EQ (attr, QCbackground))
3184 param = Qcursor_color;
3186 else if (EQ (face, Qmouse))
3188 /* Changing background color of `mouse' sets frame parameter
3189 `mouse-color'. */
3190 if (EQ (attr, QCbackground))
3191 param = Qmouse_color;
3193 #endif /* HAVE_WINDOW_SYSTEM */
3194 else if (EQ (face, Qmenu))
3196 /* Indicate that we have to update the menu bar when realizing
3197 faces on FRAME. FRAME t change the default for new frames.
3198 We do this by setting the flag in new face caches. */
3199 if (FRAMEP (frame))
3201 struct frame *f = XFRAME (frame);
3202 if (FRAME_FACE_CACHE (f) == NULL)
3203 FRAME_FACE_CACHE (f) = make_face_cache (f);
3204 FRAME_FACE_CACHE (f)->menu_face_changed_p = true;
3206 else
3207 menu_face_changed_default = true;
3210 if (!NILP (param))
3212 if (EQ (frame, Qt))
3213 /* Update `default-frame-alist', which is used for new frames. */
3215 store_in_alist (&Vdefault_frame_alist, param, value);
3217 else
3218 /* Update the current frame's parameters. */
3220 Lisp_Object cons;
3221 cons = XCAR (Vparam_value_alist);
3222 XSETCAR (cons, param);
3223 XSETCDR (cons, value);
3224 Fmodify_frame_parameters (frame, Vparam_value_alist);
3229 return face;
3233 /* Update the corresponding face when frame parameter PARAM on frame F
3234 has been assigned the value NEW_VALUE. */
3236 void
3237 update_face_from_frame_parameter (struct frame *f, Lisp_Object param,
3238 Lisp_Object new_value)
3240 Lisp_Object face = Qnil;
3241 Lisp_Object lface;
3243 /* If there are no faces yet, give up. This is the case when called
3244 from Fx_create_frame, and we do the necessary things later in
3245 face-set-after-frame-defaults. */
3246 if (NILP (f->face_alist))
3247 return;
3249 if (EQ (param, Qforeground_color))
3251 face = Qdefault;
3252 lface = lface_from_face_name (f, face, true);
3253 ASET (lface, LFACE_FOREGROUND_INDEX,
3254 (STRINGP (new_value) ? new_value : Qunspecified));
3255 realize_basic_faces (f);
3257 else if (EQ (param, Qbackground_color))
3259 Lisp_Object frame;
3261 /* Changing the background color might change the background
3262 mode, so that we have to load new defface specs.
3263 Call frame-set-background-mode to do that. */
3264 XSETFRAME (frame, f);
3265 call1 (Qframe_set_background_mode, frame);
3267 face = Qdefault;
3268 lface = lface_from_face_name (f, face, true);
3269 ASET (lface, LFACE_BACKGROUND_INDEX,
3270 (STRINGP (new_value) ? new_value : Qunspecified));
3271 realize_basic_faces (f);
3273 #ifdef HAVE_WINDOW_SYSTEM
3274 else if (EQ (param, Qborder_color))
3276 face = Qborder;
3277 lface = lface_from_face_name (f, face, true);
3278 ASET (lface, LFACE_BACKGROUND_INDEX,
3279 (STRINGP (new_value) ? new_value : Qunspecified));
3281 else if (EQ (param, Qcursor_color))
3283 face = Qcursor;
3284 lface = lface_from_face_name (f, face, true);
3285 ASET (lface, LFACE_BACKGROUND_INDEX,
3286 (STRINGP (new_value) ? new_value : Qunspecified));
3288 else if (EQ (param, Qmouse_color))
3290 face = Qmouse;
3291 lface = lface_from_face_name (f, face, true);
3292 ASET (lface, LFACE_BACKGROUND_INDEX,
3293 (STRINGP (new_value) ? new_value : Qunspecified));
3295 #endif
3297 /* Changing a named face means that all realized faces depending on
3298 that face are invalid. Since we cannot tell which realized faces
3299 depend on the face, make sure they are all removed. This is done
3300 by setting face_change. The next call to init_iterator will then
3301 free realized faces. */
3302 if (!NILP (face)
3303 && NILP (Fget (face, Qface_no_inherit)))
3305 f->face_change = true;
3306 fset_redisplay (f);
3311 #ifdef HAVE_WINDOW_SYSTEM
3313 /* Set the `font' frame parameter of FRAME determined from the
3314 font-object set in `default' face attributes LFACE. */
3316 static void
3317 set_font_frame_param (Lisp_Object frame, Lisp_Object lface)
3319 struct frame *f = XFRAME (frame);
3320 Lisp_Object font;
3322 if (FRAME_WINDOW_P (f)
3323 /* Don't do anything if the font is `unspecified'. This can
3324 happen during frame creation. */
3325 && (font = LFACE_FONT (lface),
3326 ! UNSPECIFIEDP (font)))
3328 if (FONT_SPEC_P (font))
3330 font = font_load_for_lface (f, XVECTOR (lface)->contents, font);
3331 if (NILP (font))
3332 return;
3333 ASET (lface, LFACE_FONT_INDEX, font);
3335 f->default_face_done_p = false;
3336 AUTO_FRAME_ARG (arg, Qfont, font);
3337 Fmodify_frame_parameters (frame, arg);
3341 DEFUN ("internal-face-x-get-resource", Finternal_face_x_get_resource,
3342 Sinternal_face_x_get_resource, 2, 3, 0,
3343 doc: /* Get the value of X resource RESOURCE, class CLASS.
3344 Returned value is for the display of frame FRAME. If FRAME is not
3345 specified or nil, use selected frame. This function exists because
3346 ordinary `x-get-resource' doesn't take a frame argument. */)
3347 (Lisp_Object resource, Lisp_Object class, Lisp_Object frame)
3349 Lisp_Object value = Qnil;
3350 struct frame *f;
3352 CHECK_STRING (resource);
3353 CHECK_STRING (class);
3354 f = decode_live_frame (frame);
3355 block_input ();
3356 value = display_x_get_resource (FRAME_DISPLAY_INFO (f),
3357 resource, class, Qnil, Qnil);
3358 unblock_input ();
3359 return value;
3363 /* Return resource string VALUE as a boolean value, i.e. nil, or t.
3364 If VALUE is "on" or "true", return t. If VALUE is "off" or
3365 "false", return nil. Otherwise, if SIGNAL_P, signal an
3366 error; if !SIGNAL_P, return 0. */
3368 static Lisp_Object
3369 face_boolean_x_resource_value (Lisp_Object value, bool signal_p)
3371 Lisp_Object result = make_number (0);
3373 eassert (STRINGP (value));
3375 if (xstrcasecmp (SSDATA (value), "on") == 0
3376 || xstrcasecmp (SSDATA (value), "true") == 0)
3377 result = Qt;
3378 else if (xstrcasecmp (SSDATA (value), "off") == 0
3379 || xstrcasecmp (SSDATA (value), "false") == 0)
3380 result = Qnil;
3381 else if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3382 result = Qunspecified;
3383 else if (signal_p)
3384 signal_error ("Invalid face attribute value from X resource", value);
3386 return result;
3390 DEFUN ("internal-set-lisp-face-attribute-from-resource",
3391 Finternal_set_lisp_face_attribute_from_resource,
3392 Sinternal_set_lisp_face_attribute_from_resource,
3393 3, 4, 0, doc: /* */)
3394 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
3396 CHECK_SYMBOL (face);
3397 CHECK_SYMBOL (attr);
3398 CHECK_STRING (value);
3400 if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3401 value = Qunspecified;
3402 else if (EQ (attr, QCheight))
3404 value = Fstring_to_number (value, make_number (10));
3405 if (XINT (value) <= 0)
3406 signal_error ("Invalid face height from X resource", value);
3408 else if (EQ (attr, QCbold) || EQ (attr, QCitalic))
3409 value = face_boolean_x_resource_value (value, true);
3410 else if (EQ (attr, QCweight) || EQ (attr, QCslant) || EQ (attr, QCwidth))
3411 value = intern (SSDATA (value));
3412 else if (EQ (attr, QCreverse_video) || EQ (attr, QCinverse_video))
3413 value = face_boolean_x_resource_value (value, true);
3414 else if (EQ (attr, QCunderline)
3415 || EQ (attr, QCoverline)
3416 || EQ (attr, QCstrike_through))
3418 Lisp_Object boolean_value;
3420 /* If the result of face_boolean_x_resource_value is t or nil,
3421 VALUE does NOT specify a color. */
3422 boolean_value = face_boolean_x_resource_value (value, false);
3423 if (SYMBOLP (boolean_value))
3424 value = boolean_value;
3426 else if (EQ (attr, QCbox) || EQ (attr, QCinherit))
3427 value = Fcar (Fread_from_string (value, Qnil, Qnil));
3429 return Finternal_set_lisp_face_attribute (face, attr, value, frame);
3432 #endif /* HAVE_WINDOW_SYSTEM */
3435 /***********************************************************************
3436 Menu face
3437 ***********************************************************************/
3439 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
3441 /* Make menus on frame F appear as specified by the `menu' face. */
3443 static void
3444 x_update_menu_appearance (struct frame *f)
3446 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
3447 XrmDatabase rdb;
3449 if (dpyinfo
3450 && (rdb = XrmGetDatabase (FRAME_X_DISPLAY (f)),
3451 rdb != NULL))
3453 char line[512];
3454 char *buf = line;
3455 ptrdiff_t bufsize = sizeof line;
3456 Lisp_Object lface = lface_from_face_name (f, Qmenu, true);
3457 struct face *face = FACE_FROM_ID (f, MENU_FACE_ID);
3458 const char *myname = SSDATA (Vx_resource_name);
3459 bool changed_p = false;
3460 #ifdef USE_MOTIF
3461 const char *popup_path = "popup_menu";
3462 #else
3463 const char *popup_path = "menu.popup";
3464 #endif
3466 if (STRINGP (LFACE_FOREGROUND (lface)))
3468 exprintf (&buf, &bufsize, line, -1, "%s.%s*foreground: %s",
3469 myname, popup_path,
3470 SDATA (LFACE_FOREGROUND (lface)));
3471 XrmPutLineResource (&rdb, line);
3472 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*foreground: %s",
3473 myname, SDATA (LFACE_FOREGROUND (lface)));
3474 XrmPutLineResource (&rdb, line);
3475 changed_p = true;
3478 if (STRINGP (LFACE_BACKGROUND (lface)))
3480 exprintf (&buf, &bufsize, line, -1, "%s.%s*background: %s",
3481 myname, popup_path,
3482 SDATA (LFACE_BACKGROUND (lface)));
3483 XrmPutLineResource (&rdb, line);
3485 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*background: %s",
3486 myname, SDATA (LFACE_BACKGROUND (lface)));
3487 XrmPutLineResource (&rdb, line);
3488 changed_p = true;
3491 if (face->font
3492 /* On Solaris 5.8, it's been reported that the `menu' face
3493 can be unspecified here, during startup. Why this
3494 happens remains unknown. -- cyd */
3495 && FONTP (LFACE_FONT (lface))
3496 && (!UNSPECIFIEDP (LFACE_FAMILY (lface))
3497 || !UNSPECIFIEDP (LFACE_FOUNDRY (lface))
3498 || !UNSPECIFIEDP (LFACE_SWIDTH (lface))
3499 || !UNSPECIFIEDP (LFACE_WEIGHT (lface))
3500 || !UNSPECIFIEDP (LFACE_SLANT (lface))
3501 || !UNSPECIFIEDP (LFACE_HEIGHT (lface))))
3503 Lisp_Object xlfd = Ffont_xlfd_name (LFACE_FONT (lface), Qnil);
3504 #ifdef USE_MOTIF
3505 const char *suffix = "List";
3506 bool motif = true;
3507 #else
3508 #if defined HAVE_X_I18N
3510 const char *suffix = "Set";
3511 #else
3512 const char *suffix = "";
3513 #endif
3514 bool motif = false;
3515 #endif
3517 if (! NILP (xlfd))
3519 #if defined HAVE_X_I18N
3520 char *fontsetname = xic_create_fontsetname (SSDATA (xlfd), motif);
3521 #else
3522 char *fontsetname = SSDATA (xlfd);
3523 #endif
3524 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*font%s: %s",
3525 myname, suffix, fontsetname);
3526 XrmPutLineResource (&rdb, line);
3528 exprintf (&buf, &bufsize, line, -1, "%s.%s*font%s: %s",
3529 myname, popup_path, suffix, fontsetname);
3530 XrmPutLineResource (&rdb, line);
3531 changed_p = true;
3532 if (fontsetname != SSDATA (xlfd))
3533 xfree (fontsetname);
3537 if (changed_p && f->output_data.x->menubar_widget)
3538 free_frame_menubar (f);
3540 if (buf != line)
3541 xfree (buf);
3545 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
3548 DEFUN ("face-attribute-relative-p", Fface_attribute_relative_p,
3549 Sface_attribute_relative_p,
3550 2, 2, 0,
3551 doc: /* Check whether a face attribute value is relative.
3552 Specifically, this function returns t if the attribute ATTRIBUTE
3553 with the value VALUE is relative.
3555 A relative value is one that doesn't entirely override whatever is
3556 inherited from another face. For most possible attributes,
3557 the only relative value that users see is `unspecified'.
3558 However, for :height, floating point values are also relative. */
3559 attributes: const)
3560 (Lisp_Object attribute, Lisp_Object value)
3562 if (EQ (value, Qunspecified) || (EQ (value, QCignore_defface)))
3563 return Qt;
3564 else if (EQ (attribute, QCheight))
3565 return INTEGERP (value) ? Qnil : Qt;
3566 else
3567 return Qnil;
3570 DEFUN ("merge-face-attribute", Fmerge_face_attribute, Smerge_face_attribute,
3571 3, 3, 0,
3572 doc: /* Return face ATTRIBUTE VALUE1 merged with VALUE2.
3573 If VALUE1 or VALUE2 are absolute (see `face-attribute-relative-p'), then
3574 the result will be absolute, otherwise it will be relative. */)
3575 (Lisp_Object attribute, Lisp_Object value1, Lisp_Object value2)
3577 if (EQ (value1, Qunspecified) || EQ (value1, QCignore_defface))
3578 return value2;
3579 else if (EQ (attribute, QCheight))
3580 return merge_face_heights (value1, value2, value1);
3581 else
3582 return value1;
3586 DEFUN ("internal-get-lisp-face-attribute", Finternal_get_lisp_face_attribute,
3587 Sinternal_get_lisp_face_attribute,
3588 2, 3, 0,
3589 doc: /* Return face attribute KEYWORD of face SYMBOL.
3590 If SYMBOL does not name a valid Lisp face or KEYWORD isn't a valid
3591 face attribute name, signal an error.
3592 If the optional argument FRAME is given, report on face SYMBOL in that
3593 frame. If FRAME is t, report on the defaults for face SYMBOL (for new
3594 frames). If FRAME is omitted or nil, use the selected frame. */)
3595 (Lisp_Object symbol, Lisp_Object keyword, Lisp_Object frame)
3597 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3598 Lisp_Object lface = lface_from_face_name (f, symbol, true), value = Qnil;
3600 CHECK_SYMBOL (symbol);
3601 CHECK_SYMBOL (keyword);
3603 if (EQ (keyword, QCfamily))
3604 value = LFACE_FAMILY (lface);
3605 else if (EQ (keyword, QCfoundry))
3606 value = LFACE_FOUNDRY (lface);
3607 else if (EQ (keyword, QCheight))
3608 value = LFACE_HEIGHT (lface);
3609 else if (EQ (keyword, QCweight))
3610 value = LFACE_WEIGHT (lface);
3611 else if (EQ (keyword, QCslant))
3612 value = LFACE_SLANT (lface);
3613 else if (EQ (keyword, QCunderline))
3614 value = LFACE_UNDERLINE (lface);
3615 else if (EQ (keyword, QCoverline))
3616 value = LFACE_OVERLINE (lface);
3617 else if (EQ (keyword, QCstrike_through))
3618 value = LFACE_STRIKE_THROUGH (lface);
3619 else if (EQ (keyword, QCbox))
3620 value = LFACE_BOX (lface);
3621 else if (EQ (keyword, QCinverse_video)
3622 || EQ (keyword, QCreverse_video))
3623 value = LFACE_INVERSE (lface);
3624 else if (EQ (keyword, QCforeground))
3625 value = LFACE_FOREGROUND (lface);
3626 else if (EQ (keyword, QCdistant_foreground))
3627 value = LFACE_DISTANT_FOREGROUND (lface);
3628 else if (EQ (keyword, QCbackground))
3629 value = LFACE_BACKGROUND (lface);
3630 else if (EQ (keyword, QCstipple))
3631 value = LFACE_STIPPLE (lface);
3632 else if (EQ (keyword, QCwidth))
3633 value = LFACE_SWIDTH (lface);
3634 else if (EQ (keyword, QCinherit))
3635 value = LFACE_INHERIT (lface);
3636 else if (EQ (keyword, QCfont))
3637 value = LFACE_FONT (lface);
3638 else if (EQ (keyword, QCfontset))
3639 value = LFACE_FONTSET (lface);
3640 else
3641 signal_error ("Invalid face attribute name", keyword);
3643 if (IGNORE_DEFFACE_P (value))
3644 return Qunspecified;
3646 return value;
3650 DEFUN ("internal-lisp-face-attribute-values",
3651 Finternal_lisp_face_attribute_values,
3652 Sinternal_lisp_face_attribute_values, 1, 1, 0,
3653 doc: /* Return a list of valid discrete values for face attribute ATTR.
3654 Value is nil if ATTR doesn't have a discrete set of valid values. */)
3655 (Lisp_Object attr)
3657 Lisp_Object result = Qnil;
3659 CHECK_SYMBOL (attr);
3661 if (EQ (attr, QCunderline) || EQ (attr, QCoverline)
3662 || EQ (attr, QCstrike_through)
3663 || EQ (attr, QCinverse_video) || EQ (attr, QCreverse_video))
3664 result = list2 (Qt, Qnil);
3666 return result;
3670 DEFUN ("internal-merge-in-global-face", Finternal_merge_in_global_face,
3671 Sinternal_merge_in_global_face, 2, 2, 0,
3672 doc: /* Add attributes from frame-default definition of FACE to FACE on FRAME.
3673 Default face attributes override any local face attributes. */)
3674 (Lisp_Object face, Lisp_Object frame)
3676 int i;
3677 Lisp_Object global_lface, local_lface, *gvec, *lvec;
3678 struct frame *f = XFRAME (frame);
3680 CHECK_LIVE_FRAME (frame);
3681 global_lface = lface_from_face_name (NULL, face, true);
3682 local_lface = lface_from_face_name (f, face, false);
3683 if (NILP (local_lface))
3684 local_lface = Finternal_make_lisp_face (face, frame);
3686 /* Make every specified global attribute override the local one.
3687 BEWARE!! This is only used from `face-set-after-frame-default' where
3688 the local frame is defined from default specs in `face-defface-spec'
3689 and those should be overridden by global settings. Hence the strange
3690 "global before local" priority. */
3691 lvec = XVECTOR (local_lface)->contents;
3692 gvec = XVECTOR (global_lface)->contents;
3693 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3694 if (IGNORE_DEFFACE_P (gvec[i]))
3695 ASET (local_lface, i, Qunspecified);
3696 else if (! UNSPECIFIEDP (gvec[i]))
3697 ASET (local_lface, i, AREF (global_lface, i));
3699 /* If the default face was changed, update the face cache and the
3700 `font' frame parameter. */
3701 if (EQ (face, Qdefault))
3703 struct face_cache *c = FRAME_FACE_CACHE (f);
3704 struct face *newface, *oldface = FACE_FROM_ID (f, DEFAULT_FACE_ID);
3705 Lisp_Object attrs[LFACE_VECTOR_SIZE];
3707 /* This can be NULL (e.g., in batch mode). */
3708 if (oldface)
3710 /* Ensure that the face vector is fully specified by merging
3711 the previously-cached vector. */
3712 memcpy (attrs, oldface->lface, sizeof attrs);
3713 merge_face_vectors (f, lvec, attrs, 0);
3714 vcopy (local_lface, 0, attrs, LFACE_VECTOR_SIZE);
3715 newface = realize_face (c, lvec, DEFAULT_FACE_ID);
3717 if ((! UNSPECIFIEDP (gvec[LFACE_FAMILY_INDEX])
3718 || ! UNSPECIFIEDP (gvec[LFACE_FOUNDRY_INDEX])
3719 || ! UNSPECIFIEDP (gvec[LFACE_HEIGHT_INDEX])
3720 || ! UNSPECIFIEDP (gvec[LFACE_WEIGHT_INDEX])
3721 || ! UNSPECIFIEDP (gvec[LFACE_SLANT_INDEX])
3722 || ! UNSPECIFIEDP (gvec[LFACE_SWIDTH_INDEX])
3723 || ! UNSPECIFIEDP (gvec[LFACE_FONT_INDEX]))
3724 && newface->font)
3726 Lisp_Object name = newface->font->props[FONT_NAME_INDEX];
3727 AUTO_FRAME_ARG (arg, Qfont, name);
3728 Fmodify_frame_parameters (frame, arg);
3731 if (STRINGP (gvec[LFACE_FOREGROUND_INDEX]))
3733 AUTO_FRAME_ARG (arg, Qforeground_color,
3734 gvec[LFACE_FOREGROUND_INDEX]);
3735 Fmodify_frame_parameters (frame, arg);
3738 if (STRINGP (gvec[LFACE_BACKGROUND_INDEX]))
3740 AUTO_FRAME_ARG (arg, Qbackground_color,
3741 gvec[LFACE_BACKGROUND_INDEX]);
3742 Fmodify_frame_parameters (frame, arg);
3747 return Qnil;
3751 /* The following function is implemented for compatibility with 20.2.
3752 The function is used in x-resolve-fonts when it is asked to
3753 return fonts with the same size as the font of a face. This is
3754 done in fontset.el. */
3756 DEFUN ("face-font", Fface_font, Sface_font, 1, 3, 0,
3757 doc: /* Return the font name of face FACE, or nil if it is unspecified.
3758 The font name is, by default, for ASCII characters.
3759 If the optional argument FRAME is given, report on face FACE in that frame.
3760 If FRAME is t, report on the defaults for face FACE (for new frames).
3761 The font default for a face is either nil, or a list
3762 of the form (bold), (italic) or (bold italic).
3763 If FRAME is omitted or nil, use the selected frame. And, in this case,
3764 if the optional third argument CHARACTER is given,
3765 return the font name used for CHARACTER. */)
3766 (Lisp_Object face, Lisp_Object frame, Lisp_Object character)
3768 if (EQ (frame, Qt))
3770 Lisp_Object result = Qnil;
3771 Lisp_Object lface = lface_from_face_name (NULL, face, true);
3773 if (!UNSPECIFIEDP (LFACE_WEIGHT (lface))
3774 && !EQ (LFACE_WEIGHT (lface), Qnormal))
3775 result = Fcons (Qbold, result);
3777 if (!UNSPECIFIEDP (LFACE_SLANT (lface))
3778 && !EQ (LFACE_SLANT (lface), Qnormal))
3779 result = Fcons (Qitalic, result);
3781 return result;
3783 else
3785 struct frame *f = decode_live_frame (frame);
3786 int face_id = lookup_named_face (f, face, true);
3787 struct face *fface = FACE_FROM_ID (f, face_id);
3789 if (! fface)
3790 return Qnil;
3791 #ifdef HAVE_WINDOW_SYSTEM
3792 if (FRAME_WINDOW_P (f) && !NILP (character))
3794 CHECK_CHARACTER (character);
3795 face_id = FACE_FOR_CHAR (f, fface, XINT (character), -1, Qnil);
3796 fface = FACE_FROM_ID (f, face_id);
3798 return (fface->font
3799 ? fface->font->props[FONT_NAME_INDEX]
3800 : Qnil);
3801 #else /* !HAVE_WINDOW_SYSTEM */
3802 return build_string (FRAME_MSDOS_P (f)
3803 ? "ms-dos"
3804 : FRAME_W32_P (f) ? "w32term"
3805 :"tty");
3806 #endif
3811 /* Compare face-attribute values v1 and v2 for equality. Value is true if
3812 all attributes are `equal'. Tries to be fast because this function
3813 is called quite often. */
3815 static bool
3816 face_attr_equal_p (Lisp_Object v1, Lisp_Object v2)
3818 /* Type can differ, e.g. when one attribute is unspecified, i.e. nil,
3819 and the other is specified. */
3820 if (XTYPE (v1) != XTYPE (v2))
3821 return false;
3823 if (EQ (v1, v2))
3824 return true;
3826 switch (XTYPE (v1))
3828 case Lisp_String:
3829 if (SBYTES (v1) != SBYTES (v2))
3830 return false;
3832 return memcmp (SDATA (v1), SDATA (v2), SBYTES (v1)) == 0;
3834 case_Lisp_Int:
3835 case Lisp_Symbol:
3836 return false;
3838 default:
3839 return !NILP (Fequal (v1, v2));
3844 /* Compare face vectors V1 and V2 for equality. Value is true if
3845 all attributes are `equal'. Tries to be fast because this function
3846 is called quite often. */
3848 static bool
3849 lface_equal_p (Lisp_Object *v1, Lisp_Object *v2)
3851 int i;
3852 bool equal_p = true;
3854 for (i = 1; i < LFACE_VECTOR_SIZE && equal_p; ++i)
3855 equal_p = face_attr_equal_p (v1[i], v2[i]);
3857 return equal_p;
3861 DEFUN ("internal-lisp-face-equal-p", Finternal_lisp_face_equal_p,
3862 Sinternal_lisp_face_equal_p, 2, 3, 0,
3863 doc: /* True if FACE1 and FACE2 are equal.
3864 If the optional argument FRAME is given, report on FACE1 and FACE2 in that frame.
3865 If FRAME is t, report on the defaults for FACE1 and FACE2 (for new frames).
3866 If FRAME is omitted or nil, use the selected frame. */)
3867 (Lisp_Object face1, Lisp_Object face2, Lisp_Object frame)
3869 bool equal_p;
3870 struct frame *f;
3871 Lisp_Object lface1, lface2;
3873 /* Don't use decode_window_system_frame here because this function
3874 is called before X frames exist. At that time, if FRAME is nil,
3875 selected_frame will be used which is the frame dumped with
3876 Emacs. That frame is not an X frame. */
3877 f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3879 lface1 = lface_from_face_name (f, face1, true);
3880 lface2 = lface_from_face_name (f, face2, true);
3881 equal_p = lface_equal_p (XVECTOR (lface1)->contents,
3882 XVECTOR (lface2)->contents);
3883 return equal_p ? Qt : Qnil;
3887 DEFUN ("internal-lisp-face-empty-p", Finternal_lisp_face_empty_p,
3888 Sinternal_lisp_face_empty_p, 1, 2, 0,
3889 doc: /* True if FACE has no attribute specified.
3890 If the optional argument FRAME is given, report on face FACE in that frame.
3891 If FRAME is t, report on the defaults for face FACE (for new frames).
3892 If FRAME is omitted or nil, use the selected frame. */)
3893 (Lisp_Object face, Lisp_Object frame)
3895 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3896 Lisp_Object lface = lface_from_face_name (f, face, true);
3897 int i;
3899 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3900 if (!UNSPECIFIEDP (AREF (lface, i)))
3901 break;
3903 return i == LFACE_VECTOR_SIZE ? Qt : Qnil;
3907 DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist,
3908 0, 1, 0,
3909 doc: /* Return an alist of frame-local faces defined on FRAME.
3910 For internal use only. */)
3911 (Lisp_Object frame)
3913 return decode_live_frame (frame)->face_alist;
3917 /* Return a hash code for Lisp string STRING with case ignored. Used
3918 below in computing a hash value for a Lisp face. */
3920 static unsigned
3921 hash_string_case_insensitive (Lisp_Object string)
3923 const unsigned char *s;
3924 unsigned hash = 0;
3925 eassert (STRINGP (string));
3926 for (s = SDATA (string); *s; ++s)
3927 hash = (hash << 1) ^ c_tolower (*s);
3928 return hash;
3932 /* Return a hash code for face attribute vector V. */
3934 static unsigned
3935 lface_hash (Lisp_Object *v)
3937 return (hash_string_case_insensitive (v[LFACE_FAMILY_INDEX])
3938 ^ hash_string_case_insensitive (v[LFACE_FOUNDRY_INDEX])
3939 ^ hash_string_case_insensitive (v[LFACE_FOREGROUND_INDEX])
3940 ^ hash_string_case_insensitive (v[LFACE_BACKGROUND_INDEX])
3941 ^ XHASH (v[LFACE_WEIGHT_INDEX])
3942 ^ XHASH (v[LFACE_SLANT_INDEX])
3943 ^ XHASH (v[LFACE_SWIDTH_INDEX])
3944 ^ XHASH (v[LFACE_HEIGHT_INDEX]));
3947 #ifdef HAVE_WINDOW_SYSTEM
3949 /* Return true if LFACE1 and LFACE2 specify the same font (without
3950 considering charsets/registries). They do if they specify the same
3951 family, point size, weight, width, slant, and font. Both
3952 LFACE1 and LFACE2 must be fully-specified. */
3954 static bool
3955 lface_same_font_attributes_p (Lisp_Object *lface1, Lisp_Object *lface2)
3957 eassert (lface_fully_specified_p (lface1)
3958 && lface_fully_specified_p (lface2));
3959 return (xstrcasecmp (SSDATA (lface1[LFACE_FAMILY_INDEX]),
3960 SSDATA (lface2[LFACE_FAMILY_INDEX])) == 0
3961 && xstrcasecmp (SSDATA (lface1[LFACE_FOUNDRY_INDEX]),
3962 SSDATA (lface2[LFACE_FOUNDRY_INDEX])) == 0
3963 && EQ (lface1[LFACE_HEIGHT_INDEX], lface2[LFACE_HEIGHT_INDEX])
3964 && EQ (lface1[LFACE_SWIDTH_INDEX], lface2[LFACE_SWIDTH_INDEX])
3965 && EQ (lface1[LFACE_WEIGHT_INDEX], lface2[LFACE_WEIGHT_INDEX])
3966 && EQ (lface1[LFACE_SLANT_INDEX], lface2[LFACE_SLANT_INDEX])
3967 && EQ (lface1[LFACE_FONT_INDEX], lface2[LFACE_FONT_INDEX])
3968 && (EQ (lface1[LFACE_FONTSET_INDEX], lface2[LFACE_FONTSET_INDEX])
3969 || (STRINGP (lface1[LFACE_FONTSET_INDEX])
3970 && STRINGP (lface2[LFACE_FONTSET_INDEX])
3971 && ! xstrcasecmp (SSDATA (lface1[LFACE_FONTSET_INDEX]),
3972 SSDATA (lface2[LFACE_FONTSET_INDEX]))))
3976 #endif /* HAVE_WINDOW_SYSTEM */
3978 /***********************************************************************
3979 Realized Faces
3980 ***********************************************************************/
3982 /* Allocate and return a new realized face for Lisp face attribute
3983 vector ATTR. */
3985 static struct face *
3986 make_realized_face (Lisp_Object *attr)
3988 enum { off = offsetof (struct face, id) };
3989 struct face *face = xmalloc (sizeof *face);
3991 memcpy (face->lface, attr, sizeof face->lface);
3992 memset (&face->id, 0, sizeof *face - off);
3993 face->ascii_face = face;
3995 return face;
3999 /* Free realized face FACE, including its X resources. FACE may
4000 be null. */
4002 static void
4003 free_realized_face (struct frame *f, struct face *face)
4005 if (face)
4007 #ifdef HAVE_WINDOW_SYSTEM
4008 if (FRAME_WINDOW_P (f))
4010 /* Free fontset of FACE if it is ASCII face. */
4011 if (face->fontset >= 0 && face == face->ascii_face)
4012 free_face_fontset (f, face);
4013 if (face->gc)
4015 block_input ();
4016 if (face->font)
4017 font_done_for_face (f, face);
4018 x_free_gc (f, face->gc);
4019 face->gc = 0;
4020 unblock_input ();
4022 #ifdef HAVE_X_WINDOWS
4023 free_face_colors (f, face);
4024 #endif /* HAVE_X_WINDOWS */
4025 x_destroy_bitmap (f, face->stipple);
4027 #endif /* HAVE_WINDOW_SYSTEM */
4029 xfree (face);
4033 #ifdef HAVE_WINDOW_SYSTEM
4035 /* Prepare face FACE for subsequent display on frame F. This must be called
4036 before using X resources of FACE to allocate GCs if they haven't been
4037 allocated yet or have been freed by clearing the face cache. */
4039 void
4040 prepare_face_for_display (struct frame *f, struct face *face)
4042 eassert (FRAME_WINDOW_P (f));
4044 if (face->gc == 0)
4046 XGCValues xgcv;
4047 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
4049 xgcv.foreground = face->foreground;
4050 xgcv.background = face->background;
4051 #ifdef HAVE_X_WINDOWS
4052 xgcv.graphics_exposures = False;
4053 #endif
4055 block_input ();
4056 #ifdef HAVE_X_WINDOWS
4057 if (face->stipple)
4059 xgcv.fill_style = FillOpaqueStippled;
4060 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
4061 mask |= GCFillStyle | GCStipple;
4063 #endif
4064 face->gc = x_create_gc (f, mask, &xgcv);
4065 if (face->font)
4066 font_prepare_for_face (f, face);
4067 unblock_input ();
4071 #endif /* HAVE_WINDOW_SYSTEM */
4073 /* Returns the `distance' between the colors X and Y. */
4075 static int
4076 color_distance (XColor *x, XColor *y)
4078 /* This formula is from a paper titled `Colour metric' by Thiadmer Riemersma.
4079 Quoting from that paper:
4081 This formula has results that are very close to L*u*v* (with the
4082 modified lightness curve) and, more importantly, it is a more even
4083 algorithm: it does not have a range of colors where it suddenly
4084 gives far from optimal results.
4086 See <http://www.compuphase.com/cmetric.htm> for more info. */
4088 long r = (x->red - y->red) >> 8;
4089 long g = (x->green - y->green) >> 8;
4090 long b = (x->blue - y->blue) >> 8;
4091 long r_mean = (x->red + y->red) >> 9;
4093 return
4094 (((512 + r_mean) * r * r) >> 8)
4095 + 4 * g * g
4096 + (((767 - r_mean) * b * b) >> 8);
4100 DEFUN ("color-distance", Fcolor_distance, Scolor_distance, 2, 3, 0,
4101 doc: /* Return an integer distance between COLOR1 and COLOR2 on FRAME.
4102 COLOR1 and COLOR2 may be either strings containing the color name,
4103 or lists of the form (RED GREEN BLUE).
4104 If FRAME is unspecified or nil, the current frame is used. */)
4105 (Lisp_Object color1, Lisp_Object color2, Lisp_Object frame)
4107 struct frame *f = decode_live_frame (frame);
4108 XColor cdef1, cdef2;
4110 if (!(CONSP (color1) && parse_rgb_list (color1, &cdef1))
4111 && !(STRINGP (color1)
4112 && defined_color (f, SSDATA (color1), &cdef1, false)))
4113 signal_error ("Invalid color", color1);
4114 if (!(CONSP (color2) && parse_rgb_list (color2, &cdef2))
4115 && !(STRINGP (color2)
4116 && defined_color (f, SSDATA (color2), &cdef2, false)))
4117 signal_error ("Invalid color", color2);
4119 return make_number (color_distance (&cdef1, &cdef2));
4123 /***********************************************************************
4124 Face Cache
4125 ***********************************************************************/
4127 /* Return a new face cache for frame F. */
4129 static struct face_cache *
4130 make_face_cache (struct frame *f)
4132 struct face_cache *c = xmalloc (sizeof *c);
4134 c->buckets = xzalloc (FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets);
4135 c->size = 50;
4136 c->used = 0;
4137 c->faces_by_id = xmalloc (c->size * sizeof *c->faces_by_id);
4138 c->f = f;
4139 c->menu_face_changed_p = menu_face_changed_default;
4140 return c;
4143 #ifdef HAVE_WINDOW_SYSTEM
4145 /* Clear out all graphics contexts for all realized faces, except for
4146 the basic faces. This should be done from time to time just to avoid
4147 keeping too many graphics contexts that are no longer needed. */
4149 static void
4150 clear_face_gcs (struct face_cache *c)
4152 if (c && FRAME_WINDOW_P (c->f))
4154 int i;
4155 for (i = BASIC_FACE_ID_SENTINEL; i < c->used; ++i)
4157 struct face *face = c->faces_by_id[i];
4158 if (face && face->gc)
4160 block_input ();
4161 if (face->font)
4162 font_done_for_face (c->f, face);
4163 x_free_gc (c->f, face->gc);
4164 face->gc = 0;
4165 unblock_input ();
4171 #endif /* HAVE_WINDOW_SYSTEM */
4173 /* Free all realized faces in face cache C, including basic faces.
4174 C may be null. If faces are freed, make sure the frame's current
4175 matrix is marked invalid, so that a display caused by an expose
4176 event doesn't try to use faces we destroyed. */
4178 static void
4179 free_realized_faces (struct face_cache *c)
4181 if (c && c->used)
4183 int i, size;
4184 struct frame *f = c->f;
4186 /* We must block input here because we can't process X events
4187 safely while only some faces are freed, or when the frame's
4188 current matrix still references freed faces. */
4189 block_input ();
4191 for (i = 0; i < c->used; ++i)
4193 free_realized_face (f, c->faces_by_id[i]);
4194 c->faces_by_id[i] = NULL;
4197 /* Forget the escape-glyph and glyphless-char faces. */
4198 forget_escape_and_glyphless_faces ();
4199 c->used = 0;
4200 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4201 memset (c->buckets, 0, size);
4203 /* Must do a thorough redisplay the next time. Mark current
4204 matrices as invalid because they will reference faces freed
4205 above. This function is also called when a frame is
4206 destroyed. In this case, the root window of F is nil. */
4207 if (WINDOWP (f->root_window))
4209 clear_current_matrices (f);
4210 fset_redisplay (f);
4213 unblock_input ();
4218 /* Free all realized faces on FRAME or on all frames if FRAME is nil.
4219 This is done after attributes of a named face have been changed,
4220 because we can't tell which realized faces depend on that face. */
4222 void
4223 free_all_realized_faces (Lisp_Object frame)
4225 if (NILP (frame))
4227 Lisp_Object rest;
4228 FOR_EACH_FRAME (rest, frame)
4229 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4230 windows_or_buffers_changed = 58;
4232 else
4233 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4237 /* Free face cache C and faces in it, including their X resources. */
4239 static void
4240 free_face_cache (struct face_cache *c)
4242 if (c)
4244 free_realized_faces (c);
4245 xfree (c->buckets);
4246 xfree (c->faces_by_id);
4247 xfree (c);
4252 /* Cache realized face FACE in face cache C. HASH is the hash value
4253 of FACE. If FACE is for ASCII characters (i.e. FACE->ascii_face ==
4254 FACE), insert the new face to the beginning of the collision list
4255 of the face hash table of C. Otherwise, add the new face to the
4256 end of the collision list. This way, lookup_face can quickly find
4257 that a requested face is not cached. */
4259 static void
4260 cache_face (struct face_cache *c, struct face *face, unsigned int hash)
4262 int i = hash % FACE_CACHE_BUCKETS_SIZE;
4264 face->hash = hash;
4266 if (face->ascii_face != face)
4268 struct face *last = c->buckets[i];
4269 if (last)
4271 while (last->next)
4272 last = last->next;
4273 last->next = face;
4274 face->prev = last;
4275 face->next = NULL;
4277 else
4279 c->buckets[i] = face;
4280 face->prev = face->next = NULL;
4283 else
4285 face->prev = NULL;
4286 face->next = c->buckets[i];
4287 if (face->next)
4288 face->next->prev = face;
4289 c->buckets[i] = face;
4292 /* Find a free slot in C->faces_by_id and use the index of the free
4293 slot as FACE->id. */
4294 for (i = 0; i < c->used; ++i)
4295 if (c->faces_by_id[i] == NULL)
4296 break;
4297 face->id = i;
4299 #ifdef GLYPH_DEBUG
4300 /* Check that FACE got a unique id. */
4302 int j, n;
4303 struct face *face1;
4305 for (j = n = 0; j < FACE_CACHE_BUCKETS_SIZE; ++j)
4306 for (face1 = c->buckets[j]; face1; face1 = face1->next)
4307 if (face1->id == i)
4308 ++n;
4310 eassert (n == 1);
4312 #endif /* GLYPH_DEBUG */
4314 /* Maybe enlarge C->faces_by_id. */
4315 if (i == c->used)
4317 if (c->used == c->size)
4318 c->faces_by_id = xpalloc (c->faces_by_id, &c->size, 1, MAX_FACE_ID,
4319 sizeof *c->faces_by_id);
4320 c->used++;
4323 c->faces_by_id[i] = face;
4327 /* Remove face FACE from cache C. */
4329 static void
4330 uncache_face (struct face_cache *c, struct face *face)
4332 int i = face->hash % FACE_CACHE_BUCKETS_SIZE;
4334 if (face->prev)
4335 face->prev->next = face->next;
4336 else
4337 c->buckets[i] = face->next;
4339 if (face->next)
4340 face->next->prev = face->prev;
4342 c->faces_by_id[face->id] = NULL;
4343 if (face->id == c->used)
4344 --c->used;
4348 /* Look up a realized face with face attributes ATTR in the face cache
4349 of frame F. The face will be used to display ASCII characters.
4350 Value is the ID of the face found. If no suitable face is found,
4351 realize a new one. */
4353 static int
4354 lookup_face (struct frame *f, Lisp_Object *attr)
4356 struct face_cache *cache = FRAME_FACE_CACHE (f);
4357 unsigned hash;
4358 int i;
4359 struct face *face;
4361 eassert (cache != NULL);
4362 check_lface_attrs (attr);
4364 /* Look up ATTR in the face cache. */
4365 hash = lface_hash (attr);
4366 i = hash % FACE_CACHE_BUCKETS_SIZE;
4368 for (face = cache->buckets[i]; face; face = face->next)
4370 if (face->ascii_face != face)
4372 /* There's no more ASCII face. */
4373 face = NULL;
4374 break;
4376 if (face->hash == hash
4377 && lface_equal_p (face->lface, attr))
4378 break;
4381 /* If not found, realize a new face. */
4382 if (face == NULL)
4383 face = realize_face (cache, attr, -1);
4385 #ifdef GLYPH_DEBUG
4386 eassert (face == FACE_FROM_ID (f, face->id));
4387 #endif /* GLYPH_DEBUG */
4389 return face->id;
4392 #ifdef HAVE_WINDOW_SYSTEM
4393 /* Look up a realized face that has the same attributes as BASE_FACE
4394 except for the font in the face cache of frame F. If FONT-OBJECT
4395 is not nil, it is an already opened font. If FONT-OBJECT is nil,
4396 the face has no font. Value is the ID of the face found. If no
4397 suitable face is found, realize a new one. */
4400 face_for_font (struct frame *f, Lisp_Object font_object, struct face *base_face)
4402 struct face_cache *cache = FRAME_FACE_CACHE (f);
4403 unsigned hash;
4404 int i;
4405 struct face *face;
4407 eassert (cache != NULL);
4408 base_face = base_face->ascii_face;
4409 hash = lface_hash (base_face->lface);
4410 i = hash % FACE_CACHE_BUCKETS_SIZE;
4412 for (face = cache->buckets[i]; face; face = face->next)
4414 if (face->ascii_face == face)
4415 continue;
4416 if (face->ascii_face == base_face
4417 && face->font == (NILP (font_object) ? NULL
4418 : XFONT_OBJECT (font_object))
4419 && lface_equal_p (face->lface, base_face->lface))
4420 return face->id;
4423 /* If not found, realize a new face. */
4424 face = realize_non_ascii_face (f, font_object, base_face);
4425 return face->id;
4427 #endif /* HAVE_WINDOW_SYSTEM */
4429 /* Return the face id of the realized face for named face SYMBOL on
4430 frame F suitable for displaying ASCII characters. Value is -1 if
4431 the face couldn't be determined, which might happen if the default
4432 face isn't realized and cannot be realized. */
4435 lookup_named_face (struct frame *f, Lisp_Object symbol, bool signal_p)
4437 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4438 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4439 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4441 if (default_face == NULL)
4443 if (!realize_basic_faces (f))
4444 return -1;
4445 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4446 if (default_face == NULL)
4447 emacs_abort (); /* realize_basic_faces must have set it up */
4450 if (! get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4451 return -1;
4453 memcpy (attrs, default_face->lface, sizeof attrs);
4454 merge_face_vectors (f, symbol_attrs, attrs, 0);
4456 return lookup_face (f, attrs);
4460 /* Return the display face-id of the basic face whose canonical face-id
4461 is FACE_ID. The return value will usually simply be FACE_ID, unless that
4462 basic face has bee remapped via Vface_remapping_alist. This function is
4463 conservative: if something goes wrong, it will simply return FACE_ID
4464 rather than signal an error. */
4467 lookup_basic_face (struct frame *f, int face_id)
4469 Lisp_Object name, mapping;
4470 int remapped_face_id;
4472 if (NILP (Vface_remapping_alist))
4473 return face_id; /* Nothing to do. */
4475 switch (face_id)
4477 case DEFAULT_FACE_ID: name = Qdefault; break;
4478 case MODE_LINE_FACE_ID: name = Qmode_line; break;
4479 case MODE_LINE_INACTIVE_FACE_ID: name = Qmode_line_inactive; break;
4480 case HEADER_LINE_FACE_ID: name = Qheader_line; break;
4481 case TOOL_BAR_FACE_ID: name = Qtool_bar; break;
4482 case FRINGE_FACE_ID: name = Qfringe; break;
4483 case SCROLL_BAR_FACE_ID: name = Qscroll_bar; break;
4484 case BORDER_FACE_ID: name = Qborder; break;
4485 case CURSOR_FACE_ID: name = Qcursor; break;
4486 case MOUSE_FACE_ID: name = Qmouse; break;
4487 case MENU_FACE_ID: name = Qmenu; break;
4489 default:
4490 emacs_abort (); /* the caller is supposed to pass us a basic face id */
4493 /* Do a quick scan through Vface_remapping_alist, and return immediately
4494 if there is no remapping for face NAME. This is just an optimization
4495 for the very common no-remapping case. */
4496 mapping = assq_no_quit (name, Vface_remapping_alist);
4497 if (NILP (mapping))
4498 return face_id; /* Give up. */
4500 /* If there is a remapping entry, lookup the face using NAME, which will
4501 handle the remapping too. */
4502 remapped_face_id = lookup_named_face (f, name, false);
4503 if (remapped_face_id < 0)
4504 return face_id; /* Give up. */
4506 return remapped_face_id;
4510 /* Return a face for charset ASCII that is like the face with id
4511 FACE_ID on frame F, but has a font that is STEPS steps smaller.
4512 STEPS < 0 means larger. Value is the id of the face. */
4515 smaller_face (struct frame *f, int face_id, int steps)
4517 #ifdef HAVE_WINDOW_SYSTEM
4518 struct face *face;
4519 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4520 int pt, last_pt, last_height;
4521 int delta;
4522 int new_face_id;
4523 struct face *new_face;
4525 /* If not called for an X frame, just return the original face. */
4526 if (FRAME_TERMCAP_P (f))
4527 return face_id;
4529 /* Try in increments of 1/2 pt. */
4530 delta = steps < 0 ? 5 : -5;
4531 steps = eabs (steps);
4533 face = FACE_FROM_ID (f, face_id);
4534 memcpy (attrs, face->lface, sizeof attrs);
4535 pt = last_pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
4536 new_face_id = face_id;
4537 last_height = FONT_HEIGHT (face->font);
4539 while (steps
4540 && pt + delta > 0
4541 /* Give up if we cannot find a font within 10pt. */
4542 && eabs (last_pt - pt) < 100)
4544 /* Look up a face for a slightly smaller/larger font. */
4545 pt += delta;
4546 attrs[LFACE_HEIGHT_INDEX] = make_number (pt);
4547 new_face_id = lookup_face (f, attrs);
4548 new_face = FACE_FROM_ID (f, new_face_id);
4550 /* If height changes, count that as one step. */
4551 if ((delta < 0 && FONT_HEIGHT (new_face->font) < last_height)
4552 || (delta > 0 && FONT_HEIGHT (new_face->font) > last_height))
4554 --steps;
4555 last_height = FONT_HEIGHT (new_face->font);
4556 last_pt = pt;
4560 return new_face_id;
4562 #else /* not HAVE_WINDOW_SYSTEM */
4564 return face_id;
4566 #endif /* not HAVE_WINDOW_SYSTEM */
4570 /* Return a face for charset ASCII that is like the face with id
4571 FACE_ID on frame F, but has height HEIGHT. */
4574 face_with_height (struct frame *f, int face_id, int height)
4576 #ifdef HAVE_WINDOW_SYSTEM
4577 struct face *face;
4578 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4580 if (FRAME_TERMCAP_P (f)
4581 || height <= 0)
4582 return face_id;
4584 face = FACE_FROM_ID (f, face_id);
4585 memcpy (attrs, face->lface, sizeof attrs);
4586 attrs[LFACE_HEIGHT_INDEX] = make_number (height);
4587 font_clear_prop (attrs, FONT_SIZE_INDEX);
4588 face_id = lookup_face (f, attrs);
4589 #endif /* HAVE_WINDOW_SYSTEM */
4591 return face_id;
4595 /* Return the face id of the realized face for named face SYMBOL on
4596 frame F suitable for displaying ASCII characters, and use
4597 attributes of the face FACE_ID for attributes that aren't
4598 completely specified by SYMBOL. This is like lookup_named_face,
4599 except that the default attributes come from FACE_ID, not from the
4600 default face. FACE_ID is assumed to be already realized. */
4603 lookup_derived_face (struct frame *f, Lisp_Object symbol, int face_id,
4604 bool signal_p)
4606 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4607 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4608 struct face *default_face = FACE_FROM_ID (f, face_id);
4610 if (!default_face)
4611 emacs_abort ();
4613 if (!get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4614 return -1;
4616 memcpy (attrs, default_face->lface, sizeof attrs);
4617 merge_face_vectors (f, symbol_attrs, attrs, 0);
4618 return lookup_face (f, attrs);
4621 DEFUN ("face-attributes-as-vector", Fface_attributes_as_vector,
4622 Sface_attributes_as_vector, 1, 1, 0,
4623 doc: /* Return a vector of face attributes corresponding to PLIST. */)
4624 (Lisp_Object plist)
4626 Lisp_Object lface;
4627 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
4628 Qunspecified);
4629 merge_face_ref (XFRAME (selected_frame), plist, XVECTOR (lface)->contents,
4630 true, 0);
4631 return lface;
4636 /***********************************************************************
4637 Face capability testing
4638 ***********************************************************************/
4641 /* If the distance (as returned by color_distance) between two colors is
4642 less than this, then they are considered the same, for determining
4643 whether a color is supported or not. The range of values is 0-65535. */
4645 #define TTY_SAME_COLOR_THRESHOLD 10000
4647 #ifdef HAVE_WINDOW_SYSTEM
4649 /* Return true if all the face attributes in ATTRS are supported
4650 on the window-system frame F.
4652 The definition of `supported' is somewhat heuristic, but basically means
4653 that a face containing all the attributes in ATTRS, when merged with the
4654 default face for display, can be represented in a way that's
4656 (1) different in appearance than the default face, and
4657 (2) `close in spirit' to what the attributes specify, if not exact. */
4659 static bool
4660 x_supports_face_attributes_p (struct frame *f,
4661 Lisp_Object attrs[LFACE_VECTOR_SIZE],
4662 struct face *def_face)
4664 Lisp_Object *def_attrs = def_face->lface;
4666 /* Check that other specified attributes are different that the default
4667 face. */
4668 if ((!UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
4669 && face_attr_equal_p (attrs[LFACE_UNDERLINE_INDEX],
4670 def_attrs[LFACE_UNDERLINE_INDEX]))
4671 || (!UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
4672 && face_attr_equal_p (attrs[LFACE_INVERSE_INDEX],
4673 def_attrs[LFACE_INVERSE_INDEX]))
4674 || (!UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
4675 && face_attr_equal_p (attrs[LFACE_FOREGROUND_INDEX],
4676 def_attrs[LFACE_FOREGROUND_INDEX]))
4677 || (!UNSPECIFIEDP (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
4678 && face_attr_equal_p (attrs[LFACE_DISTANT_FOREGROUND_INDEX],
4679 def_attrs[LFACE_DISTANT_FOREGROUND_INDEX]))
4680 || (!UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
4681 && face_attr_equal_p (attrs[LFACE_BACKGROUND_INDEX],
4682 def_attrs[LFACE_BACKGROUND_INDEX]))
4683 || (!UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4684 && face_attr_equal_p (attrs[LFACE_STIPPLE_INDEX],
4685 def_attrs[LFACE_STIPPLE_INDEX]))
4686 || (!UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4687 && face_attr_equal_p (attrs[LFACE_OVERLINE_INDEX],
4688 def_attrs[LFACE_OVERLINE_INDEX]))
4689 || (!UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4690 && face_attr_equal_p (attrs[LFACE_STRIKE_THROUGH_INDEX],
4691 def_attrs[LFACE_STRIKE_THROUGH_INDEX]))
4692 || (!UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
4693 && face_attr_equal_p (attrs[LFACE_BOX_INDEX],
4694 def_attrs[LFACE_BOX_INDEX])))
4695 return false;
4697 /* Check font-related attributes, as those are the most commonly
4698 "unsupported" on a window-system (because of missing fonts). */
4699 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4700 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4701 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4702 || !UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
4703 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
4704 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX]))
4706 int face_id;
4707 struct face *face;
4708 Lisp_Object merged_attrs[LFACE_VECTOR_SIZE];
4709 int i;
4711 memcpy (merged_attrs, def_attrs, sizeof merged_attrs);
4713 merge_face_vectors (f, attrs, merged_attrs, 0);
4715 face_id = lookup_face (f, merged_attrs);
4716 face = FACE_FROM_ID (f, face_id);
4718 if (! face)
4719 error ("Cannot make face");
4721 /* If the font is the same, or no font is found, then not
4722 supported. */
4723 if (face->font == def_face->font
4724 || ! face->font)
4725 return false;
4726 for (i = FONT_TYPE_INDEX; i <= FONT_SIZE_INDEX; i++)
4727 if (! EQ (face->font->props[i], def_face->font->props[i]))
4729 Lisp_Object s1, s2;
4731 if (i < FONT_FOUNDRY_INDEX || i > FONT_REGISTRY_INDEX
4732 || face->font->driver->case_sensitive)
4733 return true;
4734 s1 = SYMBOL_NAME (face->font->props[i]);
4735 s2 = SYMBOL_NAME (def_face->font->props[i]);
4736 if (! EQ (Fcompare_strings (s1, make_number (0), Qnil,
4737 s2, make_number (0), Qnil, Qt), Qt))
4738 return true;
4740 return false;
4743 /* Everything checks out, this face is supported. */
4744 return true;
4747 #endif /* HAVE_WINDOW_SYSTEM */
4749 /* Return true if all the face attributes in ATTRS are supported
4750 on the tty frame F.
4752 The definition of `supported' is somewhat heuristic, but basically means
4753 that a face containing all the attributes in ATTRS, when merged
4754 with the default face for display, can be represented in a way that's
4756 (1) different in appearance than the default face, and
4757 (2) `close in spirit' to what the attributes specify, if not exact.
4759 Point (2) implies that a `:weight black' attribute will be satisfied
4760 by any terminal that can display bold, and a `:foreground "yellow"' as
4761 long as the terminal can display a yellowish color, but `:slant italic'
4762 will _not_ be satisfied by the tty display code's automatic
4763 substitution of a `dim' face for italic. */
4765 static bool
4766 tty_supports_face_attributes_p (struct frame *f,
4767 Lisp_Object attrs[LFACE_VECTOR_SIZE],
4768 struct face *def_face)
4770 int weight, slant;
4771 Lisp_Object val, fg, bg;
4772 XColor fg_tty_color, fg_std_color;
4773 XColor bg_tty_color, bg_std_color;
4774 unsigned test_caps = 0;
4775 Lisp_Object *def_attrs = def_face->lface;
4777 /* First check some easy-to-check stuff; ttys support none of the
4778 following attributes, so we can just return false if any are requested
4779 (even if `nominal' values are specified, we should still return false,
4780 as that will be the same value that the default face uses). We
4781 consider :slant unsupportable on ttys, even though the face code
4782 actually `fakes' them using a dim attribute if possible. This is
4783 because the faked result is too different from what the face
4784 specifies. */
4785 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4786 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4787 || !UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4788 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4789 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
4790 || !UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4791 || !UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4792 || !UNSPECIFIEDP (attrs[LFACE_BOX_INDEX]))
4793 return false;
4795 /* Test for terminal `capabilities' (non-color character attributes). */
4797 /* font weight (bold/dim) */
4798 val = attrs[LFACE_WEIGHT_INDEX];
4799 if (!UNSPECIFIEDP (val)
4800 && (weight = FONT_WEIGHT_NAME_NUMERIC (val), weight >= 0))
4802 int def_weight = FONT_WEIGHT_NAME_NUMERIC (def_attrs[LFACE_WEIGHT_INDEX]);
4804 if (weight > 100)
4806 if (def_weight > 100)
4807 return false; /* same as default */
4808 test_caps = TTY_CAP_BOLD;
4810 else if (weight < 100)
4812 if (def_weight < 100)
4813 return false; /* same as default */
4814 test_caps = TTY_CAP_DIM;
4816 else if (def_weight == 100)
4817 return false; /* same as default */
4820 /* font slant */
4821 val = attrs[LFACE_SLANT_INDEX];
4822 if (!UNSPECIFIEDP (val)
4823 && (slant = FONT_SLANT_NAME_NUMERIC (val), slant >= 0))
4825 int def_slant = FONT_SLANT_NAME_NUMERIC (def_attrs[LFACE_SLANT_INDEX]);
4826 if (slant == 100 || slant == def_slant)
4827 return false; /* same as default */
4828 else
4829 test_caps |= TTY_CAP_ITALIC;
4832 /* underlining */
4833 val = attrs[LFACE_UNDERLINE_INDEX];
4834 if (!UNSPECIFIEDP (val))
4836 if (STRINGP (val))
4837 return false; /* ttys can't use colored underlines */
4838 else if (EQ (CAR_SAFE (val), QCstyle) && EQ (CAR_SAFE (CDR_SAFE (val)), Qwave))
4839 return false; /* ttys can't use wave underlines */
4840 else if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
4841 return false; /* same as default */
4842 else
4843 test_caps |= TTY_CAP_UNDERLINE;
4846 /* inverse video */
4847 val = attrs[LFACE_INVERSE_INDEX];
4848 if (!UNSPECIFIEDP (val))
4850 if (face_attr_equal_p (val, def_attrs[LFACE_INVERSE_INDEX]))
4851 return false; /* same as default */
4852 else
4853 test_caps |= TTY_CAP_INVERSE;
4857 /* Color testing. */
4859 /* Check if foreground color is close enough. */
4860 fg = attrs[LFACE_FOREGROUND_INDEX];
4861 if (STRINGP (fg))
4863 Lisp_Object def_fg = def_attrs[LFACE_FOREGROUND_INDEX];
4865 if (face_attr_equal_p (fg, def_fg))
4866 return false; /* same as default */
4867 else if (! tty_lookup_color (f, fg, &fg_tty_color, &fg_std_color))
4868 return false; /* not a valid color */
4869 else if (color_distance (&fg_tty_color, &fg_std_color)
4870 > TTY_SAME_COLOR_THRESHOLD)
4871 return false; /* displayed color is too different */
4872 else
4873 /* Make sure the color is really different than the default. */
4875 XColor def_fg_color;
4876 if (tty_lookup_color (f, def_fg, &def_fg_color, 0)
4877 && (color_distance (&fg_tty_color, &def_fg_color)
4878 <= TTY_SAME_COLOR_THRESHOLD))
4879 return false;
4883 /* Check if background color is close enough. */
4884 bg = attrs[LFACE_BACKGROUND_INDEX];
4885 if (STRINGP (bg))
4887 Lisp_Object def_bg = def_attrs[LFACE_BACKGROUND_INDEX];
4889 if (face_attr_equal_p (bg, def_bg))
4890 return false; /* same as default */
4891 else if (! tty_lookup_color (f, bg, &bg_tty_color, &bg_std_color))
4892 return false; /* not a valid color */
4893 else if (color_distance (&bg_tty_color, &bg_std_color)
4894 > TTY_SAME_COLOR_THRESHOLD)
4895 return false; /* displayed color is too different */
4896 else
4897 /* Make sure the color is really different than the default. */
4899 XColor def_bg_color;
4900 if (tty_lookup_color (f, def_bg, &def_bg_color, 0)
4901 && (color_distance (&bg_tty_color, &def_bg_color)
4902 <= TTY_SAME_COLOR_THRESHOLD))
4903 return false;
4907 /* If both foreground and background are requested, see if the
4908 distance between them is OK. We just check to see if the distance
4909 between the tty's foreground and background is close enough to the
4910 distance between the standard foreground and background. */
4911 if (STRINGP (fg) && STRINGP (bg))
4913 int delta_delta
4914 = (color_distance (&fg_std_color, &bg_std_color)
4915 - color_distance (&fg_tty_color, &bg_tty_color));
4916 if (delta_delta > TTY_SAME_COLOR_THRESHOLD
4917 || delta_delta < -TTY_SAME_COLOR_THRESHOLD)
4918 return false;
4922 /* See if the capabilities we selected above are supported, with the
4923 given colors. */
4924 return tty_capable_p (FRAME_TTY (f), test_caps);
4928 DEFUN ("display-supports-face-attributes-p",
4929 Fdisplay_supports_face_attributes_p, Sdisplay_supports_face_attributes_p,
4930 1, 2, 0,
4931 doc: /* Return non-nil if all the face attributes in ATTRIBUTES are supported.
4932 The optional argument DISPLAY can be a display name, a frame, or
4933 nil (meaning the selected frame's display).
4935 The definition of `supported' is somewhat heuristic, but basically means
4936 that a face containing all the attributes in ATTRIBUTES, when merged
4937 with the default face for display, can be represented in a way that's
4939 (1) different in appearance than the default face, and
4940 (2) `close in spirit' to what the attributes specify, if not exact.
4942 Point (2) implies that a `:weight black' attribute will be satisfied by
4943 any display that can display bold, and a `:foreground \"yellow\"' as long
4944 as it can display a yellowish color, but `:slant italic' will _not_ be
4945 satisfied by the tty display code's automatic substitution of a `dim'
4946 face for italic. */)
4947 (Lisp_Object attributes, Lisp_Object display)
4949 bool supports = false;
4950 int i;
4951 Lisp_Object frame;
4952 struct frame *f;
4953 struct face *def_face;
4954 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4956 if (noninteractive || !initialized)
4957 /* We may not be able to access low-level face information in batch
4958 mode, or before being dumped, and this function is not going to
4959 be very useful in those cases anyway, so just give up. */
4960 return Qnil;
4962 if (NILP (display))
4963 frame = selected_frame;
4964 else if (FRAMEP (display))
4965 frame = display;
4966 else
4968 /* Find any frame on DISPLAY. */
4969 Lisp_Object tail;
4971 frame = Qnil;
4972 FOR_EACH_FRAME (tail, frame)
4973 if (!NILP (Fequal (Fcdr (Fassq (Qdisplay,
4974 XFRAME (frame)->param_alist)),
4975 display)))
4976 break;
4979 CHECK_LIVE_FRAME (frame);
4980 f = XFRAME (frame);
4982 for (i = 0; i < LFACE_VECTOR_SIZE; i++)
4983 attrs[i] = Qunspecified;
4984 merge_face_ref (f, attributes, attrs, true, 0);
4986 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4987 if (def_face == NULL)
4989 if (! realize_basic_faces (f))
4990 error ("Cannot realize default face");
4991 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4992 if (def_face == NULL)
4993 emacs_abort (); /* realize_basic_faces must have set it up */
4996 /* Dispatch to the appropriate handler. */
4997 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
4998 supports = tty_supports_face_attributes_p (f, attrs, def_face);
4999 #ifdef HAVE_WINDOW_SYSTEM
5000 else
5001 supports = x_supports_face_attributes_p (f, attrs, def_face);
5002 #endif
5004 return supports ? Qt : Qnil;
5008 /***********************************************************************
5009 Font selection
5010 ***********************************************************************/
5012 DEFUN ("internal-set-font-selection-order",
5013 Finternal_set_font_selection_order,
5014 Sinternal_set_font_selection_order, 1, 1, 0,
5015 doc: /* Set font selection order for face font selection to ORDER.
5016 ORDER must be a list of length 4 containing the symbols `:width',
5017 `:height', `:weight', and `:slant'. Face attributes appearing
5018 first in ORDER are matched first, e.g. if `:height' appears before
5019 `:weight' in ORDER, font selection first tries to find a font with
5020 a suitable height, and then tries to match the font weight.
5021 Value is ORDER. */)
5022 (Lisp_Object order)
5024 Lisp_Object list;
5025 int i;
5026 int indices[ARRAYELTS (font_sort_order)];
5028 CHECK_LIST (order);
5029 memset (indices, 0, sizeof indices);
5030 i = 0;
5032 for (list = order;
5033 CONSP (list) && i < ARRAYELTS (indices);
5034 list = XCDR (list), ++i)
5036 Lisp_Object attr = XCAR (list);
5037 int xlfd;
5039 if (EQ (attr, QCwidth))
5040 xlfd = XLFD_SWIDTH;
5041 else if (EQ (attr, QCheight))
5042 xlfd = XLFD_POINT_SIZE;
5043 else if (EQ (attr, QCweight))
5044 xlfd = XLFD_WEIGHT;
5045 else if (EQ (attr, QCslant))
5046 xlfd = XLFD_SLANT;
5047 else
5048 break;
5050 if (indices[i] != 0)
5051 break;
5052 indices[i] = xlfd;
5055 if (!NILP (list) || i != ARRAYELTS (indices))
5056 signal_error ("Invalid font sort order", order);
5057 for (i = 0; i < ARRAYELTS (font_sort_order); ++i)
5058 if (indices[i] == 0)
5059 signal_error ("Invalid font sort order", order);
5061 if (memcmp (indices, font_sort_order, sizeof indices) != 0)
5063 memcpy (font_sort_order, indices, sizeof font_sort_order);
5064 free_all_realized_faces (Qnil);
5067 font_update_sort_order (font_sort_order);
5069 return Qnil;
5073 DEFUN ("internal-set-alternative-font-family-alist",
5074 Finternal_set_alternative_font_family_alist,
5075 Sinternal_set_alternative_font_family_alist, 1, 1, 0,
5076 doc: /* Define alternative font families to try in face font selection.
5077 ALIST is an alist of (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5078 Each ALTERNATIVE is tried in order if no fonts of font family FAMILY can
5079 be found. Value is ALIST. */)
5080 (Lisp_Object alist)
5082 Lisp_Object entry, tail, tail2;
5084 CHECK_LIST (alist);
5085 alist = Fcopy_sequence (alist);
5086 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5088 entry = XCAR (tail);
5089 CHECK_LIST (entry);
5090 entry = Fcopy_sequence (entry);
5091 XSETCAR (tail, entry);
5092 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5093 XSETCAR (tail2, Fintern (XCAR (tail2), Qnil));
5096 Vface_alternative_font_family_alist = alist;
5097 free_all_realized_faces (Qnil);
5098 return alist;
5102 DEFUN ("internal-set-alternative-font-registry-alist",
5103 Finternal_set_alternative_font_registry_alist,
5104 Sinternal_set_alternative_font_registry_alist, 1, 1, 0,
5105 doc: /* Define alternative font registries to try in face font selection.
5106 ALIST is an alist of (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5107 Each ALTERNATIVE is tried in order if no fonts of font registry REGISTRY can
5108 be found. Value is ALIST. */)
5109 (Lisp_Object alist)
5111 Lisp_Object entry, tail, tail2;
5113 CHECK_LIST (alist);
5114 alist = Fcopy_sequence (alist);
5115 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5117 entry = XCAR (tail);
5118 CHECK_LIST (entry);
5119 entry = Fcopy_sequence (entry);
5120 XSETCAR (tail, entry);
5121 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5122 XSETCAR (tail2, Fdowncase (XCAR (tail2)));
5124 Vface_alternative_font_registry_alist = alist;
5125 free_all_realized_faces (Qnil);
5126 return alist;
5130 #ifdef HAVE_WINDOW_SYSTEM
5132 /* Return the fontset id of the base fontset name or alias name given
5133 by the fontset attribute of ATTRS. Value is -1 if the fontset
5134 attribute of ATTRS doesn't name a fontset. */
5136 static int
5137 face_fontset (Lisp_Object attrs[LFACE_VECTOR_SIZE])
5139 Lisp_Object name;
5141 name = attrs[LFACE_FONTSET_INDEX];
5142 if (!STRINGP (name))
5143 return -1;
5144 return fs_query_fontset (name, 0);
5147 #endif /* HAVE_WINDOW_SYSTEM */
5151 /***********************************************************************
5152 Face Realization
5153 ***********************************************************************/
5155 /* Realize basic faces on frame F. Value is zero if frame parameters
5156 of F don't contain enough information needed to realize the default
5157 face. */
5159 static bool
5160 realize_basic_faces (struct frame *f)
5162 bool success_p = false;
5164 /* Block input here so that we won't be surprised by an X expose
5165 event, for instance, without having the faces set up. */
5166 block_input ();
5168 if (realize_default_face (f))
5170 realize_named_face (f, Qmode_line, MODE_LINE_FACE_ID);
5171 realize_named_face (f, Qmode_line_inactive, MODE_LINE_INACTIVE_FACE_ID);
5172 realize_named_face (f, Qtool_bar, TOOL_BAR_FACE_ID);
5173 realize_named_face (f, Qfringe, FRINGE_FACE_ID);
5174 realize_named_face (f, Qheader_line, HEADER_LINE_FACE_ID);
5175 realize_named_face (f, Qscroll_bar, SCROLL_BAR_FACE_ID);
5176 realize_named_face (f, Qborder, BORDER_FACE_ID);
5177 realize_named_face (f, Qcursor, CURSOR_FACE_ID);
5178 realize_named_face (f, Qmouse, MOUSE_FACE_ID);
5179 realize_named_face (f, Qmenu, MENU_FACE_ID);
5180 realize_named_face (f, Qvertical_border, VERTICAL_BORDER_FACE_ID);
5181 realize_named_face (f, Qwindow_divider, WINDOW_DIVIDER_FACE_ID);
5182 realize_named_face (f, Qwindow_divider_first_pixel,
5183 WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
5184 realize_named_face (f, Qwindow_divider_last_pixel,
5185 WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
5187 /* Reflect changes in the `menu' face in menu bars. */
5188 if (FRAME_FACE_CACHE (f)->menu_face_changed_p)
5190 FRAME_FACE_CACHE (f)->menu_face_changed_p = false;
5191 #ifdef USE_X_TOOLKIT
5192 if (FRAME_WINDOW_P (f))
5193 x_update_menu_appearance (f);
5194 #endif
5197 success_p = true;
5200 unblock_input ();
5201 return success_p;
5205 /* Realize the default face on frame F. If the face is not fully
5206 specified, make it fully-specified. Attributes of the default face
5207 that are not explicitly specified are taken from frame parameters. */
5209 static bool
5210 realize_default_face (struct frame *f)
5212 struct face_cache *c = FRAME_FACE_CACHE (f);
5213 Lisp_Object lface;
5214 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5215 struct face *face;
5217 /* If the `default' face is not yet known, create it. */
5218 lface = lface_from_face_name (f, Qdefault, false);
5219 if (NILP (lface))
5221 Lisp_Object frame;
5222 XSETFRAME (frame, f);
5223 lface = Finternal_make_lisp_face (Qdefault, frame);
5226 #ifdef HAVE_WINDOW_SYSTEM
5227 if (FRAME_WINDOW_P (f))
5229 Lisp_Object font_object;
5231 XSETFONT (font_object, FRAME_FONT (f));
5232 set_lface_from_font (f, lface, font_object, f->default_face_done_p);
5233 ASET (lface, LFACE_FONTSET_INDEX, fontset_name (FRAME_FONTSET (f)));
5234 f->default_face_done_p = true;
5236 #endif /* HAVE_WINDOW_SYSTEM */
5238 if (!FRAME_WINDOW_P (f))
5240 ASET (lface, LFACE_FAMILY_INDEX, build_string ("default"));
5241 ASET (lface, LFACE_FOUNDRY_INDEX, LFACE_FAMILY (lface));
5242 ASET (lface, LFACE_SWIDTH_INDEX, Qnormal);
5243 ASET (lface, LFACE_HEIGHT_INDEX, make_number (1));
5244 if (UNSPECIFIEDP (LFACE_WEIGHT (lface)))
5245 ASET (lface, LFACE_WEIGHT_INDEX, Qnormal);
5246 if (UNSPECIFIEDP (LFACE_SLANT (lface)))
5247 ASET (lface, LFACE_SLANT_INDEX, Qnormal);
5248 if (UNSPECIFIEDP (LFACE_FONTSET (lface)))
5249 ASET (lface, LFACE_FONTSET_INDEX, Qnil);
5252 if (UNSPECIFIEDP (LFACE_UNDERLINE (lface)))
5253 ASET (lface, LFACE_UNDERLINE_INDEX, Qnil);
5255 if (UNSPECIFIEDP (LFACE_OVERLINE (lface)))
5256 ASET (lface, LFACE_OVERLINE_INDEX, Qnil);
5258 if (UNSPECIFIEDP (LFACE_STRIKE_THROUGH (lface)))
5259 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, Qnil);
5261 if (UNSPECIFIEDP (LFACE_BOX (lface)))
5262 ASET (lface, LFACE_BOX_INDEX, Qnil);
5264 if (UNSPECIFIEDP (LFACE_INVERSE (lface)))
5265 ASET (lface, LFACE_INVERSE_INDEX, Qnil);
5267 if (UNSPECIFIEDP (LFACE_FOREGROUND (lface)))
5269 /* This function is called so early that colors are not yet
5270 set in the frame parameter list. */
5271 Lisp_Object color = Fassq (Qforeground_color, f->param_alist);
5273 if (CONSP (color) && STRINGP (XCDR (color)))
5274 ASET (lface, LFACE_FOREGROUND_INDEX, XCDR (color));
5275 else if (FRAME_WINDOW_P (f))
5276 return false;
5277 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5278 ASET (lface, LFACE_FOREGROUND_INDEX, build_string (unspecified_fg));
5279 else
5280 emacs_abort ();
5283 if (UNSPECIFIEDP (LFACE_BACKGROUND (lface)))
5285 /* This function is called so early that colors are not yet
5286 set in the frame parameter list. */
5287 Lisp_Object color = Fassq (Qbackground_color, f->param_alist);
5288 if (CONSP (color) && STRINGP (XCDR (color)))
5289 ASET (lface, LFACE_BACKGROUND_INDEX, XCDR (color));
5290 else if (FRAME_WINDOW_P (f))
5291 return false;
5292 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5293 ASET (lface, LFACE_BACKGROUND_INDEX, build_string (unspecified_bg));
5294 else
5295 emacs_abort ();
5298 if (UNSPECIFIEDP (LFACE_STIPPLE (lface)))
5299 ASET (lface, LFACE_STIPPLE_INDEX, Qnil);
5301 /* Realize the face; it must be fully-specified now. */
5302 eassert (lface_fully_specified_p (XVECTOR (lface)->contents));
5303 check_lface (lface);
5304 memcpy (attrs, XVECTOR (lface)->contents, sizeof attrs);
5305 face = realize_face (c, attrs, DEFAULT_FACE_ID);
5307 #ifdef HAVE_WINDOW_SYSTEM
5308 #ifdef HAVE_X_WINDOWS
5309 if (FRAME_X_P (f) && face->font != FRAME_FONT (f))
5311 /* This can happen when making a frame on a display that does
5312 not support the default font. */
5313 if (!face->font)
5314 return false;
5316 /* Otherwise, the font specified for the frame was not
5317 acceptable as a font for the default face (perhaps because
5318 auto-scaled fonts are rejected), so we must adjust the frame
5319 font. */
5320 x_set_font (f, LFACE_FONT (lface), Qnil);
5322 #endif /* HAVE_X_WINDOWS */
5323 #endif /* HAVE_WINDOW_SYSTEM */
5324 return true;
5328 /* Realize basic faces other than the default face in face cache C.
5329 SYMBOL is the face name, ID is the face id the realized face must
5330 have. The default face must have been realized already. */
5332 static void
5333 realize_named_face (struct frame *f, Lisp_Object symbol, int id)
5335 struct face_cache *c = FRAME_FACE_CACHE (f);
5336 Lisp_Object lface = lface_from_face_name (f, symbol, false);
5337 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5338 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5340 /* The default face must exist and be fully specified. */
5341 get_lface_attributes_no_remap (f, Qdefault, attrs, true);
5342 check_lface_attrs (attrs);
5343 eassert (lface_fully_specified_p (attrs));
5345 /* If SYMBOL isn't know as a face, create it. */
5346 if (NILP (lface))
5348 Lisp_Object frame;
5349 XSETFRAME (frame, f);
5350 lface = Finternal_make_lisp_face (symbol, frame);
5353 /* Merge SYMBOL's face with the default face. */
5354 get_lface_attributes_no_remap (f, symbol, symbol_attrs, true);
5355 merge_face_vectors (f, symbol_attrs, attrs, 0);
5357 /* Realize the face. */
5358 realize_face (c, attrs, id);
5362 /* Realize the fully-specified face with attributes ATTRS in face
5363 cache CACHE for ASCII characters. If FORMER_FACE_ID is
5364 non-negative, it is an ID of face to remove before caching the new
5365 face. Value is a pointer to the newly created realized face. */
5367 static struct face *
5368 realize_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE],
5369 int former_face_id)
5371 struct face *face;
5373 /* LFACE must be fully specified. */
5374 eassert (cache != NULL);
5375 check_lface_attrs (attrs);
5377 if (former_face_id >= 0 && cache->used > former_face_id)
5379 /* Remove the former face. */
5380 struct face *former_face = cache->faces_by_id[former_face_id];
5381 uncache_face (cache, former_face);
5382 free_realized_face (cache->f, former_face);
5383 SET_FRAME_GARBAGED (cache->f);
5386 if (FRAME_WINDOW_P (cache->f))
5387 face = realize_x_face (cache, attrs);
5388 else if (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f))
5389 face = realize_tty_face (cache, attrs);
5390 else if (FRAME_INITIAL_P (cache->f))
5392 /* Create a dummy face. */
5393 face = make_realized_face (attrs);
5395 else
5396 emacs_abort ();
5398 /* Insert the new face. */
5399 cache_face (cache, face, lface_hash (attrs));
5400 return face;
5404 #ifdef HAVE_WINDOW_SYSTEM
5405 /* Realize the fully-specified face that uses FONT-OBJECT and has the
5406 same attributes as BASE_FACE except for the font on frame F.
5407 FONT-OBJECT may be nil, in which case, realized a face of
5408 no-font. */
5410 static struct face *
5411 realize_non_ascii_face (struct frame *f, Lisp_Object font_object,
5412 struct face *base_face)
5414 struct face_cache *cache = FRAME_FACE_CACHE (f);
5415 struct face *face;
5417 face = xmalloc (sizeof *face);
5418 *face = *base_face;
5419 face->gc = 0;
5420 face->overstrike
5421 = (! NILP (font_object)
5422 && FONT_WEIGHT_NAME_NUMERIC (face->lface[LFACE_WEIGHT_INDEX]) > 100
5423 && FONT_WEIGHT_NUMERIC (font_object) <= 100);
5425 /* Don't try to free the colors copied bitwise from BASE_FACE. */
5426 face->colors_copied_bitwise_p = true;
5427 face->font = NILP (font_object) ? NULL : XFONT_OBJECT (font_object);
5428 face->gc = 0;
5430 cache_face (cache, face, face->hash);
5432 return face;
5434 #endif /* HAVE_WINDOW_SYSTEM */
5437 /* Realize the fully-specified face with attributes ATTRS in face
5438 cache CACHE for ASCII characters. Do it for X frame CACHE->f. If
5439 the new face doesn't share font with the default face, a fontname
5440 is allocated from the heap and set in `font_name' of the new face,
5441 but it is not yet loaded here. Value is a pointer to the newly
5442 created realized face. */
5444 static struct face *
5445 realize_x_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE])
5447 struct face *face = NULL;
5448 #ifdef HAVE_WINDOW_SYSTEM
5449 struct face *default_face;
5450 struct frame *f;
5451 Lisp_Object stipple, underline, overline, strike_through, box;
5453 eassert (FRAME_WINDOW_P (cache->f));
5455 /* Allocate a new realized face. */
5456 face = make_realized_face (attrs);
5457 face->ascii_face = face;
5459 f = cache->f;
5461 /* Determine the font to use. Most of the time, the font will be
5462 the same as the font of the default face, so try that first. */
5463 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5464 if (default_face
5465 && lface_same_font_attributes_p (default_face->lface, attrs))
5467 face->font = default_face->font;
5468 face->fontset
5469 = make_fontset_for_ascii_face (f, default_face->fontset, face);
5471 else
5473 /* If the face attribute ATTRS specifies a fontset, use it as
5474 the base of a new realized fontset. Otherwise, use the same
5475 base fontset as of the default face. The base determines
5476 registry and encoding of a font. It may also determine
5477 foundry and family. The other fields of font name pattern
5478 are constructed from ATTRS. */
5479 int fontset = face_fontset (attrs);
5481 /* If we are realizing the default face, ATTRS should specify a
5482 fontset. In other words, if FONTSET is -1, we are not
5483 realizing the default face, thus the default face should have
5484 already been realized. */
5485 if (fontset == -1)
5487 if (default_face)
5488 fontset = default_face->fontset;
5489 if (fontset == -1)
5490 emacs_abort ();
5492 if (! FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5493 attrs[LFACE_FONT_INDEX]
5494 = font_load_for_lface (f, attrs, Ffont_spec (0, NULL));
5495 if (FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5497 face->font = XFONT_OBJECT (attrs[LFACE_FONT_INDEX]);
5498 face->fontset = make_fontset_for_ascii_face (f, fontset, face);
5500 else
5502 face->font = NULL;
5503 face->fontset = -1;
5507 if (face->font
5508 && FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]) > 100
5509 && FONT_WEIGHT_NUMERIC (attrs[LFACE_FONT_INDEX]) <= 100)
5510 face->overstrike = true;
5512 /* Load colors, and set remaining attributes. */
5514 load_face_colors (f, face, attrs);
5516 /* Set up box. */
5517 box = attrs[LFACE_BOX_INDEX];
5518 if (STRINGP (box))
5520 /* A simple box of line width 1 drawn in color given by
5521 the string. */
5522 face->box_color = load_color (f, face, attrs[LFACE_BOX_INDEX],
5523 LFACE_BOX_INDEX);
5524 face->box = FACE_SIMPLE_BOX;
5525 face->box_line_width = 1;
5527 else if (INTEGERP (box))
5529 /* Simple box of specified line width in foreground color of the
5530 face. */
5531 eassert (XINT (box) != 0);
5532 face->box = FACE_SIMPLE_BOX;
5533 face->box_line_width = XINT (box);
5534 face->box_color = face->foreground;
5535 face->box_color_defaulted_p = true;
5537 else if (CONSP (box))
5539 /* `(:width WIDTH :color COLOR :shadow SHADOW)'. SHADOW
5540 being one of `raised' or `sunken'. */
5541 face->box = FACE_SIMPLE_BOX;
5542 face->box_color = face->foreground;
5543 face->box_color_defaulted_p = true;
5544 face->box_line_width = 1;
5546 while (CONSP (box))
5548 Lisp_Object keyword, value;
5550 keyword = XCAR (box);
5551 box = XCDR (box);
5553 if (!CONSP (box))
5554 break;
5555 value = XCAR (box);
5556 box = XCDR (box);
5558 if (EQ (keyword, QCline_width))
5560 if (INTEGERP (value) && XINT (value) != 0)
5561 face->box_line_width = XINT (value);
5563 else if (EQ (keyword, QCcolor))
5565 if (STRINGP (value))
5567 face->box_color = load_color (f, face, value,
5568 LFACE_BOX_INDEX);
5569 face->use_box_color_for_shadows_p = true;
5572 else if (EQ (keyword, QCstyle))
5574 if (EQ (value, Qreleased_button))
5575 face->box = FACE_RAISED_BOX;
5576 else if (EQ (value, Qpressed_button))
5577 face->box = FACE_SUNKEN_BOX;
5582 /* Text underline, overline, strike-through. */
5584 underline = attrs[LFACE_UNDERLINE_INDEX];
5585 if (EQ (underline, Qt))
5587 /* Use default color (same as foreground color). */
5588 face->underline_p = true;
5589 face->underline_type = FACE_UNDER_LINE;
5590 face->underline_defaulted_p = true;
5591 face->underline_color = 0;
5593 else if (STRINGP (underline))
5595 /* Use specified color. */
5596 face->underline_p = true;
5597 face->underline_type = FACE_UNDER_LINE;
5598 face->underline_defaulted_p = false;
5599 face->underline_color
5600 = load_color (f, face, underline,
5601 LFACE_UNDERLINE_INDEX);
5603 else if (NILP (underline))
5605 face->underline_p = false;
5606 face->underline_defaulted_p = false;
5607 face->underline_color = 0;
5609 else if (CONSP (underline))
5611 /* `(:color COLOR :style STYLE)'.
5612 STYLE being one of `line' or `wave'. */
5613 face->underline_p = true;
5614 face->underline_color = 0;
5615 face->underline_defaulted_p = true;
5616 face->underline_type = FACE_UNDER_LINE;
5618 /* FIXME? This is also not robust about checking the precise form.
5619 See comments in Finternal_set_lisp_face_attribute. */
5620 while (CONSP (underline))
5622 Lisp_Object keyword, value;
5624 keyword = XCAR (underline);
5625 underline = XCDR (underline);
5627 if (!CONSP (underline))
5628 break;
5629 value = XCAR (underline);
5630 underline = XCDR (underline);
5632 if (EQ (keyword, QCcolor))
5634 if (EQ (value, Qforeground_color))
5636 face->underline_defaulted_p = true;
5637 face->underline_color = 0;
5639 else if (STRINGP (value))
5641 face->underline_defaulted_p = false;
5642 face->underline_color = load_color (f, face, value,
5643 LFACE_UNDERLINE_INDEX);
5646 else if (EQ (keyword, QCstyle))
5648 if (EQ (value, Qline))
5649 face->underline_type = FACE_UNDER_LINE;
5650 else if (EQ (value, Qwave))
5651 face->underline_type = FACE_UNDER_WAVE;
5656 overline = attrs[LFACE_OVERLINE_INDEX];
5657 if (STRINGP (overline))
5659 face->overline_color
5660 = load_color (f, face, attrs[LFACE_OVERLINE_INDEX],
5661 LFACE_OVERLINE_INDEX);
5662 face->overline_p = true;
5664 else if (EQ (overline, Qt))
5666 face->overline_color = face->foreground;
5667 face->overline_color_defaulted_p = true;
5668 face->overline_p = true;
5671 strike_through = attrs[LFACE_STRIKE_THROUGH_INDEX];
5672 if (STRINGP (strike_through))
5674 face->strike_through_color
5675 = load_color (f, face, attrs[LFACE_STRIKE_THROUGH_INDEX],
5676 LFACE_STRIKE_THROUGH_INDEX);
5677 face->strike_through_p = true;
5679 else if (EQ (strike_through, Qt))
5681 face->strike_through_color = face->foreground;
5682 face->strike_through_color_defaulted_p = true;
5683 face->strike_through_p = true;
5686 stipple = attrs[LFACE_STIPPLE_INDEX];
5687 if (!NILP (stipple))
5688 face->stipple = load_pixmap (f, stipple);
5689 #endif /* HAVE_WINDOW_SYSTEM */
5691 return face;
5695 /* Map a specified color of face FACE on frame F to a tty color index.
5696 IDX is either LFACE_FOREGROUND_INDEX or LFACE_BACKGROUND_INDEX, and
5697 specifies which color to map. Set *DEFAULTED to true if mapping to the
5698 default foreground/background colors. */
5700 static void
5701 map_tty_color (struct frame *f, struct face *face,
5702 enum lface_attribute_index idx, bool *defaulted)
5704 Lisp_Object frame, color, def;
5705 bool foreground_p = idx == LFACE_FOREGROUND_INDEX;
5706 unsigned long default_pixel =
5707 foreground_p ? FACE_TTY_DEFAULT_FG_COLOR : FACE_TTY_DEFAULT_BG_COLOR;
5708 unsigned long pixel = default_pixel;
5709 #ifdef MSDOS
5710 unsigned long default_other_pixel =
5711 foreground_p ? FACE_TTY_DEFAULT_BG_COLOR : FACE_TTY_DEFAULT_FG_COLOR;
5712 #endif
5714 eassert (idx == LFACE_FOREGROUND_INDEX || idx == LFACE_BACKGROUND_INDEX);
5716 XSETFRAME (frame, f);
5717 color = face->lface[idx];
5719 if (STRINGP (color)
5720 && SCHARS (color)
5721 && CONSP (Vtty_defined_color_alist)
5722 && (def = assoc_no_quit (color, call1 (Qtty_color_alist, frame)),
5723 CONSP (def)))
5725 /* Associations in tty-defined-color-alist are of the form
5726 (NAME INDEX R G B). We need the INDEX part. */
5727 pixel = XINT (XCAR (XCDR (def)));
5730 if (pixel == default_pixel && STRINGP (color))
5732 pixel = load_color (f, face, color, idx);
5734 #ifdef MSDOS
5735 /* If the foreground of the default face is the default color,
5736 use the foreground color defined by the frame. */
5737 if (FRAME_MSDOS_P (f))
5739 if (pixel == default_pixel
5740 || pixel == FACE_TTY_DEFAULT_COLOR)
5742 if (foreground_p)
5743 pixel = FRAME_FOREGROUND_PIXEL (f);
5744 else
5745 pixel = FRAME_BACKGROUND_PIXEL (f);
5746 face->lface[idx] = tty_color_name (f, pixel);
5747 *defaulted = true;
5749 else if (pixel == default_other_pixel)
5751 if (foreground_p)
5752 pixel = FRAME_BACKGROUND_PIXEL (f);
5753 else
5754 pixel = FRAME_FOREGROUND_PIXEL (f);
5755 face->lface[idx] = tty_color_name (f, pixel);
5756 *defaulted = true;
5759 #endif /* MSDOS */
5762 if (foreground_p)
5763 face->foreground = pixel;
5764 else
5765 face->background = pixel;
5769 /* Realize the fully-specified face with attributes ATTRS in face
5770 cache CACHE for ASCII characters. Do it for TTY frame CACHE->f.
5771 Value is a pointer to the newly created realized face. */
5773 static struct face *
5774 realize_tty_face (struct face_cache *cache,
5775 Lisp_Object attrs[LFACE_VECTOR_SIZE])
5777 struct face *face;
5778 int weight, slant;
5779 bool face_colors_defaulted = false;
5780 struct frame *f = cache->f;
5782 /* Frame must be a termcap frame. */
5783 eassert (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f));
5785 /* Allocate a new realized face. */
5786 face = make_realized_face (attrs);
5787 #if false
5788 face->font_name = FRAME_MSDOS_P (cache->f) ? "ms-dos" : "tty";
5789 #endif
5791 /* Map face attributes to TTY appearances. */
5792 weight = FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]);
5793 slant = FONT_SLANT_NAME_NUMERIC (attrs[LFACE_SLANT_INDEX]);
5794 if (weight > 100)
5795 face->tty_bold_p = true;
5796 if (slant != 100)
5797 face->tty_italic_p = true;
5798 if (!NILP (attrs[LFACE_UNDERLINE_INDEX]))
5799 face->tty_underline_p = true;
5800 if (!NILP (attrs[LFACE_INVERSE_INDEX]))
5801 face->tty_reverse_p = true;
5803 /* Map color names to color indices. */
5804 map_tty_color (f, face, LFACE_FOREGROUND_INDEX, &face_colors_defaulted);
5805 map_tty_color (f, face, LFACE_BACKGROUND_INDEX, &face_colors_defaulted);
5807 /* Swap colors if face is inverse-video. If the colors are taken
5808 from the frame colors, they are already inverted, since the
5809 frame-creation function calls x-handle-reverse-video. */
5810 if (face->tty_reverse_p && !face_colors_defaulted)
5812 unsigned long tem = face->foreground;
5813 face->foreground = face->background;
5814 face->background = tem;
5817 if (tty_suppress_bold_inverse_default_colors_p
5818 && face->tty_bold_p
5819 && face->background == FACE_TTY_DEFAULT_FG_COLOR
5820 && face->foreground == FACE_TTY_DEFAULT_BG_COLOR)
5821 face->tty_bold_p = false;
5823 return face;
5827 DEFUN ("tty-suppress-bold-inverse-default-colors",
5828 Ftty_suppress_bold_inverse_default_colors,
5829 Stty_suppress_bold_inverse_default_colors, 1, 1, 0,
5830 doc: /* Suppress/allow boldness of faces with inverse default colors.
5831 SUPPRESS non-nil means suppress it.
5832 This affects bold faces on TTYs whose foreground is the default background
5833 color of the display and whose background is the default foreground color.
5834 For such faces, the bold face attribute is ignored if this variable
5835 is non-nil. */)
5836 (Lisp_Object suppress)
5838 tty_suppress_bold_inverse_default_colors_p = !NILP (suppress);
5839 face_change = true;
5840 return suppress;
5845 /***********************************************************************
5846 Computing Faces
5847 ***********************************************************************/
5849 /* Return the ID of the face to use to display character CH with face
5850 property PROP on frame F in current_buffer. */
5853 compute_char_face (struct frame *f, int ch, Lisp_Object prop)
5855 int face_id;
5857 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
5858 ch = 0;
5860 if (NILP (prop))
5862 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5863 face_id = FACE_FOR_CHAR (f, face, ch, -1, Qnil);
5865 else
5867 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5868 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5869 memcpy (attrs, default_face->lface, sizeof attrs);
5870 merge_face_ref (f, prop, attrs, true, 0);
5871 face_id = lookup_face (f, attrs);
5874 return face_id;
5877 /* Return the face ID associated with buffer position POS for
5878 displaying ASCII characters. Return in *ENDPTR the position at
5879 which a different face is needed, as far as text properties and
5880 overlays are concerned. W is a window displaying current_buffer.
5882 REGION_BEG, REGION_END delimit the region, so it can be
5883 highlighted.
5885 LIMIT is a position not to scan beyond. That is to limit the time
5886 this function can take.
5888 If MOUSE, use the character's mouse-face, not its face.
5890 BASE_FACE_ID, if non-negative, specifies a base face id to use
5891 instead of DEFAULT_FACE_ID.
5893 The face returned is suitable for displaying ASCII characters. */
5896 face_at_buffer_position (struct window *w, ptrdiff_t pos,
5897 ptrdiff_t *endptr, ptrdiff_t limit,
5898 bool mouse, int base_face_id)
5900 struct frame *f = XFRAME (w->frame);
5901 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5902 Lisp_Object prop, position;
5903 ptrdiff_t i, noverlays;
5904 Lisp_Object *overlay_vec;
5905 ptrdiff_t endpos;
5906 Lisp_Object propname = mouse ? Qmouse_face : Qface;
5907 Lisp_Object limit1, end;
5908 struct face *default_face;
5910 /* W must display the current buffer. We could write this function
5911 to use the frame and buffer of W, but right now it doesn't. */
5912 /* eassert (XBUFFER (w->contents) == current_buffer); */
5914 XSETFASTINT (position, pos);
5916 endpos = ZV;
5918 /* Get the `face' or `mouse_face' text property at POS, and
5919 determine the next position at which the property changes. */
5920 prop = Fget_text_property (position, propname, w->contents);
5921 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
5922 end = Fnext_single_property_change (position, propname, w->contents, limit1);
5923 if (INTEGERP (end))
5924 endpos = XINT (end);
5926 /* Look at properties from overlays. */
5927 USE_SAFE_ALLOCA;
5929 ptrdiff_t next_overlay;
5931 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, &next_overlay, false);
5932 if (next_overlay < endpos)
5933 endpos = next_overlay;
5936 *endptr = endpos;
5939 int face_id;
5941 if (base_face_id >= 0)
5942 face_id = base_face_id;
5943 else if (NILP (Vface_remapping_alist))
5944 face_id = DEFAULT_FACE_ID;
5945 else
5946 face_id = lookup_basic_face (f, DEFAULT_FACE_ID);
5948 default_face = FACE_FROM_ID (f, face_id);
5951 /* Optimize common cases where we can use the default face. */
5952 if (noverlays == 0
5953 && NILP (prop))
5955 SAFE_FREE ();
5956 return default_face->id;
5959 /* Begin with attributes from the default face. */
5960 memcpy (attrs, default_face->lface, sizeof attrs);
5962 /* Merge in attributes specified via text properties. */
5963 if (!NILP (prop))
5964 merge_face_ref (f, prop, attrs, true, 0);
5966 /* Now merge the overlay data. */
5967 noverlays = sort_overlays (overlay_vec, noverlays, w);
5968 for (i = 0; i < noverlays; i++)
5970 Lisp_Object oend;
5971 ptrdiff_t oendpos;
5973 prop = Foverlay_get (overlay_vec[i], propname);
5974 if (!NILP (prop))
5975 merge_face_ref (f, prop, attrs, true, 0);
5977 oend = OVERLAY_END (overlay_vec[i]);
5978 oendpos = OVERLAY_POSITION (oend);
5979 if (oendpos < endpos)
5980 endpos = oendpos;
5983 *endptr = endpos;
5985 SAFE_FREE ();
5987 /* Look up a realized face with the given face attributes,
5988 or realize a new one for ASCII characters. */
5989 return lookup_face (f, attrs);
5992 /* Return the face ID at buffer position POS for displaying ASCII
5993 characters associated with overlay strings for overlay OVERLAY.
5995 Like face_at_buffer_position except for OVERLAY. Currently it
5996 simply disregards the `face' properties of all overlays. */
5999 face_for_overlay_string (struct window *w, ptrdiff_t pos,
6000 ptrdiff_t *endptr, ptrdiff_t limit,
6001 bool mouse, Lisp_Object overlay)
6003 struct frame *f = XFRAME (w->frame);
6004 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6005 Lisp_Object prop, position;
6006 ptrdiff_t endpos;
6007 Lisp_Object propname = mouse ? Qmouse_face : Qface;
6008 Lisp_Object limit1, end;
6009 struct face *default_face;
6011 /* W must display the current buffer. We could write this function
6012 to use the frame and buffer of W, but right now it doesn't. */
6013 /* eassert (XBUFFER (w->contents) == current_buffer); */
6015 XSETFASTINT (position, pos);
6017 endpos = ZV;
6019 /* Get the `face' or `mouse_face' text property at POS, and
6020 determine the next position at which the property changes. */
6021 prop = Fget_text_property (position, propname, w->contents);
6022 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
6023 end = Fnext_single_property_change (position, propname, w->contents, limit1);
6024 if (INTEGERP (end))
6025 endpos = XINT (end);
6027 *endptr = endpos;
6029 /* Optimize common case where we can use the default face. */
6030 if (NILP (prop)
6031 && NILP (Vface_remapping_alist))
6032 return DEFAULT_FACE_ID;
6034 /* Begin with attributes from the default face. */
6035 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
6036 memcpy (attrs, default_face->lface, sizeof attrs);
6038 /* Merge in attributes specified via text properties. */
6039 if (!NILP (prop))
6040 merge_face_ref (f, prop, attrs, true, 0);
6042 *endptr = endpos;
6044 /* Look up a realized face with the given face attributes,
6045 or realize a new one for ASCII characters. */
6046 return lookup_face (f, attrs);
6050 /* Compute the face at character position POS in Lisp string STRING on
6051 window W, for ASCII characters.
6053 If STRING is an overlay string, it comes from position BUFPOS in
6054 current_buffer, otherwise BUFPOS is zero to indicate that STRING is
6055 not an overlay string. W must display the current buffer.
6056 REGION_BEG and REGION_END give the start and end positions of the
6057 region; both are -1 if no region is visible.
6059 BASE_FACE_ID is the id of a face to merge with. For strings coming
6060 from overlays or the `display' property it is the face at BUFPOS.
6062 If MOUSE_P, use the character's mouse-face, not its face.
6064 Set *ENDPTR to the next position where to check for faces in
6065 STRING; -1 if the face is constant from POS to the end of the
6066 string.
6068 Value is the id of the face to use. The face returned is suitable
6069 for displaying ASCII characters. */
6072 face_at_string_position (struct window *w, Lisp_Object string,
6073 ptrdiff_t pos, ptrdiff_t bufpos,
6074 ptrdiff_t *endptr, enum face_id base_face_id,
6075 bool mouse_p)
6077 Lisp_Object prop, position, end, limit;
6078 struct frame *f = XFRAME (WINDOW_FRAME (w));
6079 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6080 struct face *base_face;
6081 bool multibyte_p = STRING_MULTIBYTE (string);
6082 Lisp_Object prop_name = mouse_p ? Qmouse_face : Qface;
6084 /* Get the value of the face property at the current position within
6085 STRING. Value is nil if there is no face property. */
6086 XSETFASTINT (position, pos);
6087 prop = Fget_text_property (position, prop_name, string);
6089 /* Get the next position at which to check for faces. Value of end
6090 is nil if face is constant all the way to the end of the string.
6091 Otherwise it is a string position where to check faces next.
6092 Limit is the maximum position up to which to check for property
6093 changes in Fnext_single_property_change. Strings are usually
6094 short, so set the limit to the end of the string. */
6095 XSETFASTINT (limit, SCHARS (string));
6096 end = Fnext_single_property_change (position, prop_name, string, limit);
6097 if (INTEGERP (end))
6098 *endptr = XFASTINT (end);
6099 else
6100 *endptr = -1;
6102 base_face = FACE_FROM_ID (f, base_face_id);
6103 eassert (base_face);
6105 /* Optimize the default case that there is no face property. */
6106 if (NILP (prop)
6107 && (multibyte_p
6108 /* We can't realize faces for different charsets differently
6109 if we don't have fonts, so we can stop here if not working
6110 on a window-system frame. */
6111 || !FRAME_WINDOW_P (f)
6112 || FACE_SUITABLE_FOR_ASCII_CHAR_P (base_face, 0)))
6113 return base_face->id;
6115 /* Begin with attributes from the base face. */
6116 memcpy (attrs, base_face->lface, sizeof attrs);
6118 /* Merge in attributes specified via text properties. */
6119 if (!NILP (prop))
6120 merge_face_ref (f, prop, attrs, true, 0);
6122 /* Look up a realized face with the given face attributes,
6123 or realize a new one for ASCII characters. */
6124 return lookup_face (f, attrs);
6128 /* Merge a face into a realized face.
6130 F is frame where faces are (to be) realized.
6132 FACE_NAME is named face to merge.
6134 If FACE_NAME is nil, FACE_ID is face_id of realized face to merge.
6136 If FACE_NAME is t, FACE_ID is lface_id of face to merge.
6138 BASE_FACE_ID is realized face to merge into.
6140 Return new face id.
6144 merge_faces (struct frame *f, Lisp_Object face_name, int face_id,
6145 int base_face_id)
6147 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6148 struct face *base_face;
6150 base_face = FACE_FROM_ID (f, base_face_id);
6151 if (!base_face)
6152 return base_face_id;
6154 if (EQ (face_name, Qt))
6156 if (face_id < 0 || face_id >= lface_id_to_name_size)
6157 return base_face_id;
6158 face_name = lface_id_to_name[face_id];
6159 /* When called during make-frame, lookup_derived_face may fail
6160 if the faces are uninitialized. Don't signal an error. */
6161 face_id = lookup_derived_face (f, face_name, base_face_id, 0);
6162 return (face_id >= 0 ? face_id : base_face_id);
6165 /* Begin with attributes from the base face. */
6166 memcpy (attrs, base_face->lface, sizeof attrs);
6168 if (!NILP (face_name))
6170 if (!merge_named_face (f, face_name, attrs, 0))
6171 return base_face_id;
6173 else
6175 struct face *face;
6176 if (face_id < 0)
6177 return base_face_id;
6178 face = FACE_FROM_ID (f, face_id);
6179 if (!face)
6180 return base_face_id;
6181 merge_face_vectors (f, face->lface, attrs, 0);
6184 /* Look up a realized face with the given face attributes,
6185 or realize a new one for ASCII characters. */
6186 return lookup_face (f, attrs);
6191 #ifndef HAVE_X_WINDOWS
6192 DEFUN ("x-load-color-file", Fx_load_color_file,
6193 Sx_load_color_file, 1, 1, 0,
6194 doc: /* Create an alist of color entries from an external file.
6196 The file should define one named RGB color per line like so:
6197 R G B name
6198 where R,G,B are numbers between 0 and 255 and name is an arbitrary string. */)
6199 (Lisp_Object filename)
6201 FILE *fp;
6202 Lisp_Object cmap = Qnil;
6203 Lisp_Object abspath;
6205 CHECK_STRING (filename);
6206 abspath = Fexpand_file_name (filename, Qnil);
6208 block_input ();
6209 fp = emacs_fopen (SSDATA (abspath), "r" FOPEN_TEXT);
6210 if (fp)
6212 char buf[512];
6213 int red, green, blue;
6214 int num;
6216 while (fgets (buf, sizeof (buf), fp) != NULL) {
6217 if (sscanf (buf, "%u %u %u %n", &red, &green, &blue, &num) == 3)
6219 #ifdef HAVE_NTGUI
6220 int color = RGB (red, green, blue);
6221 #else
6222 int color = (red << 16) | (green << 8) | blue;
6223 #endif
6224 char *name = buf + num;
6225 ptrdiff_t len = strlen (name);
6226 len -= 0 < len && name[len - 1] == '\n';
6227 cmap = Fcons (Fcons (make_string (name, len), make_number (color)),
6228 cmap);
6231 fclose (fp);
6233 unblock_input ();
6234 return cmap;
6236 #endif
6239 /***********************************************************************
6240 Tests
6241 ***********************************************************************/
6243 #ifdef GLYPH_DEBUG
6245 /* Print the contents of the realized face FACE to stderr. */
6247 static void
6248 dump_realized_face (struct face *face)
6250 fprintf (stderr, "ID: %d\n", face->id);
6251 #ifdef HAVE_X_WINDOWS
6252 fprintf (stderr, "gc: %p\n", face->gc);
6253 #endif
6254 fprintf (stderr, "foreground: 0x%lx (%s)\n",
6255 face->foreground,
6256 SDATA (face->lface[LFACE_FOREGROUND_INDEX]));
6257 fprintf (stderr, "background: 0x%lx (%s)\n",
6258 face->background,
6259 SDATA (face->lface[LFACE_BACKGROUND_INDEX]));
6260 if (face->font)
6261 fprintf (stderr, "font_name: %s (%s)\n",
6262 SDATA (face->font->props[FONT_NAME_INDEX]),
6263 SDATA (face->lface[LFACE_FAMILY_INDEX]));
6264 #ifdef HAVE_X_WINDOWS
6265 fprintf (stderr, "font = %p\n", face->font);
6266 #endif
6267 fprintf (stderr, "fontset: %d\n", face->fontset);
6268 fprintf (stderr, "underline: %d (%s)\n",
6269 face->underline_p,
6270 SDATA (Fsymbol_name (face->lface[LFACE_UNDERLINE_INDEX])));
6271 fprintf (stderr, "hash: %d\n", face->hash);
6275 DEFUN ("dump-face", Fdump_face, Sdump_face, 0, 1, 0, doc: /* */)
6276 (Lisp_Object n)
6278 if (NILP (n))
6280 int i;
6282 fprintf (stderr, "font selection order: ");
6283 for (i = 0; i < ARRAYELTS (font_sort_order); ++i)
6284 fprintf (stderr, "%d ", font_sort_order[i]);
6285 fprintf (stderr, "\n");
6287 fprintf (stderr, "alternative fonts: ");
6288 debug_print (Vface_alternative_font_family_alist);
6289 fprintf (stderr, "\n");
6291 for (i = 0; i < FRAME_FACE_CACHE (SELECTED_FRAME ())->used; ++i)
6292 Fdump_face (make_number (i));
6294 else
6296 struct face *face;
6297 CHECK_NUMBER (n);
6298 face = FACE_FROM_ID (SELECTED_FRAME (), XINT (n));
6299 if (face == NULL)
6300 error ("Not a valid face");
6301 dump_realized_face (face);
6304 return Qnil;
6308 DEFUN ("show-face-resources", Fshow_face_resources, Sshow_face_resources,
6309 0, 0, 0, doc: /* */)
6310 (void)
6312 fprintf (stderr, "number of colors = %d\n", ncolors_allocated);
6313 fprintf (stderr, "number of pixmaps = %d\n", npixmaps_allocated);
6314 fprintf (stderr, "number of GCs = %d\n", ngcs);
6315 return Qnil;
6318 #endif /* GLYPH_DEBUG */
6322 /***********************************************************************
6323 Initialization
6324 ***********************************************************************/
6326 void
6327 syms_of_xfaces (void)
6329 /* The symbols `face' and `mouse-face' used as text properties. */
6330 DEFSYM (Qface, "face");
6332 /* Property for basic faces which other faces cannot inherit. */
6333 DEFSYM (Qface_no_inherit, "face-no-inherit");
6335 /* Error symbol for wrong_type_argument in load_pixmap. */
6336 DEFSYM (Qbitmap_spec_p, "bitmap-spec-p");
6338 /* The name of the function to call when the background of the frame
6339 has changed, frame_set_background_mode. */
6340 DEFSYM (Qframe_set_background_mode, "frame-set-background-mode");
6342 /* Lisp face attribute keywords. */
6343 DEFSYM (QCfamily, ":family");
6344 DEFSYM (QCheight, ":height");
6345 DEFSYM (QCweight, ":weight");
6346 DEFSYM (QCslant, ":slant");
6347 DEFSYM (QCunderline, ":underline");
6348 DEFSYM (QCinverse_video, ":inverse-video");
6349 DEFSYM (QCreverse_video, ":reverse-video");
6350 DEFSYM (QCforeground, ":foreground");
6351 DEFSYM (QCbackground, ":background");
6352 DEFSYM (QCstipple, ":stipple");
6353 DEFSYM (QCwidth, ":width");
6354 DEFSYM (QCfont, ":font");
6355 DEFSYM (QCfontset, ":fontset");
6356 DEFSYM (QCdistant_foreground, ":distant-foreground");
6357 DEFSYM (QCbold, ":bold");
6358 DEFSYM (QCitalic, ":italic");
6359 DEFSYM (QCoverline, ":overline");
6360 DEFSYM (QCstrike_through, ":strike-through");
6361 DEFSYM (QCbox, ":box");
6362 DEFSYM (QCinherit, ":inherit");
6364 /* Symbols used for Lisp face attribute values. */
6365 DEFSYM (QCcolor, ":color");
6366 DEFSYM (QCline_width, ":line-width");
6367 DEFSYM (QCstyle, ":style");
6368 DEFSYM (Qline, "line");
6369 DEFSYM (Qwave, "wave");
6370 DEFSYM (Qreleased_button, "released-button");
6371 DEFSYM (Qpressed_button, "pressed-button");
6372 DEFSYM (Qnormal, "normal");
6373 DEFSYM (Qextra_light, "extra-light");
6374 DEFSYM (Qlight, "light");
6375 DEFSYM (Qsemi_light, "semi-light");
6376 DEFSYM (Qsemi_bold, "semi-bold");
6377 DEFSYM (Qbold, "bold");
6378 DEFSYM (Qextra_bold, "extra-bold");
6379 DEFSYM (Qultra_bold, "ultra-bold");
6380 DEFSYM (Qoblique, "oblique");
6381 DEFSYM (Qitalic, "italic");
6383 /* The symbols `foreground-color' and `background-color' which can be
6384 used as part of a `face' property. This is for compatibility with
6385 Emacs 20.2. */
6386 DEFSYM (Qbackground_color, "background-color");
6387 DEFSYM (Qforeground_color, "foreground-color");
6389 DEFSYM (Qunspecified, "unspecified");
6390 DEFSYM (QCignore_defface, ":ignore-defface");
6392 /* The symbol `face-alias'. A symbol having that property is an
6393 alias for another face. Value of the property is the name of
6394 the aliased face. */
6395 DEFSYM (Qface_alias, "face-alias");
6397 /* Names of basic faces. */
6398 DEFSYM (Qdefault, "default");
6399 DEFSYM (Qtool_bar, "tool-bar");
6400 DEFSYM (Qfringe, "fringe");
6401 DEFSYM (Qheader_line, "header-line");
6402 DEFSYM (Qscroll_bar, "scroll-bar");
6403 DEFSYM (Qmenu, "menu");
6404 DEFSYM (Qcursor, "cursor");
6405 DEFSYM (Qborder, "border");
6406 DEFSYM (Qmouse, "mouse");
6407 DEFSYM (Qmode_line_inactive, "mode-line-inactive");
6408 DEFSYM (Qvertical_border, "vertical-border");
6410 /* TTY color-related functions (defined in tty-colors.el). */
6411 DEFSYM (Qwindow_divider, "window-divider");
6412 DEFSYM (Qwindow_divider_first_pixel, "window-divider-first-pixel");
6413 DEFSYM (Qwindow_divider_last_pixel, "window-divider-last-pixel");
6414 DEFSYM (Qtty_color_desc, "tty-color-desc");
6415 DEFSYM (Qtty_color_standard_values, "tty-color-standard-values");
6416 DEFSYM (Qtty_color_by_index, "tty-color-by-index");
6418 /* The name of the function used to compute colors on TTYs. */
6419 DEFSYM (Qtty_color_alist, "tty-color-alist");
6421 Vparam_value_alist = list1 (Fcons (Qnil, Qnil));
6422 staticpro (&Vparam_value_alist);
6423 Vface_alternative_font_family_alist = Qnil;
6424 staticpro (&Vface_alternative_font_family_alist);
6425 Vface_alternative_font_registry_alist = Qnil;
6426 staticpro (&Vface_alternative_font_registry_alist);
6428 defsubr (&Sinternal_make_lisp_face);
6429 defsubr (&Sinternal_lisp_face_p);
6430 defsubr (&Sinternal_set_lisp_face_attribute);
6431 #ifdef HAVE_WINDOW_SYSTEM
6432 defsubr (&Sinternal_set_lisp_face_attribute_from_resource);
6433 #endif
6434 defsubr (&Scolor_gray_p);
6435 defsubr (&Scolor_supported_p);
6436 #ifndef HAVE_X_WINDOWS
6437 defsubr (&Sx_load_color_file);
6438 #endif
6439 defsubr (&Sface_attribute_relative_p);
6440 defsubr (&Smerge_face_attribute);
6441 defsubr (&Sinternal_get_lisp_face_attribute);
6442 defsubr (&Sinternal_lisp_face_attribute_values);
6443 defsubr (&Sinternal_lisp_face_equal_p);
6444 defsubr (&Sinternal_lisp_face_empty_p);
6445 defsubr (&Sinternal_copy_lisp_face);
6446 defsubr (&Sinternal_merge_in_global_face);
6447 defsubr (&Sface_font);
6448 defsubr (&Sframe_face_alist);
6449 defsubr (&Sdisplay_supports_face_attributes_p);
6450 defsubr (&Scolor_distance);
6451 defsubr (&Sinternal_set_font_selection_order);
6452 defsubr (&Sinternal_set_alternative_font_family_alist);
6453 defsubr (&Sinternal_set_alternative_font_registry_alist);
6454 defsubr (&Sface_attributes_as_vector);
6455 #ifdef GLYPH_DEBUG
6456 defsubr (&Sdump_face);
6457 defsubr (&Sshow_face_resources);
6458 #endif /* GLYPH_DEBUG */
6459 defsubr (&Sclear_face_cache);
6460 defsubr (&Stty_suppress_bold_inverse_default_colors);
6462 #if defined DEBUG_X_COLORS && defined HAVE_X_WINDOWS
6463 defsubr (&Sdump_colors);
6464 #endif
6466 DEFVAR_LISP ("face-new-frame-defaults", Vface_new_frame_defaults,
6467 doc: /* List of global face definitions (for internal use only.) */);
6468 Vface_new_frame_defaults = Qnil;
6470 DEFVAR_LISP ("face-default-stipple", Vface_default_stipple,
6471 doc: /* Default stipple pattern used on monochrome displays.
6472 This stipple pattern is used on monochrome displays
6473 instead of shades of gray for a face background color.
6474 See `set-face-stipple' for possible values for this variable. */);
6475 Vface_default_stipple = build_pure_c_string ("gray3");
6477 DEFVAR_LISP ("tty-defined-color-alist", Vtty_defined_color_alist,
6478 doc: /* An alist of defined terminal colors and their RGB values.
6479 See the docstring of `tty-color-alist' for the details. */);
6480 Vtty_defined_color_alist = Qnil;
6482 DEFVAR_LISP ("scalable-fonts-allowed", Vscalable_fonts_allowed,
6483 doc: /* Allowed scalable fonts.
6484 A value of nil means don't allow any scalable fonts.
6485 A value of t means allow any scalable font.
6486 Otherwise, value must be a list of regular expressions. A font may be
6487 scaled if its name matches a regular expression in the list.
6488 Note that if value is nil, a scalable font might still be used, if no
6489 other font of the appropriate family and registry is available. */);
6490 Vscalable_fonts_allowed = Qnil;
6492 DEFVAR_LISP ("face-ignored-fonts", Vface_ignored_fonts,
6493 doc: /* List of ignored fonts.
6494 Each element is a regular expression that matches names of fonts to
6495 ignore. */);
6496 Vface_ignored_fonts = Qnil;
6498 DEFVAR_LISP ("face-remapping-alist", Vface_remapping_alist,
6499 doc: /* Alist of face remappings.
6500 Each element is of the form:
6502 (FACE . REPLACEMENT),
6504 which causes display of the face FACE to use REPLACEMENT instead.
6505 REPLACEMENT is a face specification, i.e. one of the following:
6507 (1) a face name
6508 (2) a property list of attribute/value pairs, or
6509 (3) a list in which each element has the form of (1) or (2).
6511 List values for REPLACEMENT are merged to form the final face
6512 specification, with earlier entries taking precedence, in the same as
6513 as in the `face' text property.
6515 Face-name remapping cycles are suppressed; recursive references use
6516 the underlying face instead of the remapped face. So a remapping of
6517 the form:
6519 (FACE EXTRA-FACE... FACE)
6523 (FACE (FACE-ATTR VAL ...) FACE)
6525 causes EXTRA-FACE... or (FACE-ATTR VAL ...) to be _merged_ with the
6526 existing definition of FACE. Note that this isn't necessary for the
6527 default face, since every face inherits from the default face.
6529 If this variable is made buffer-local, the face remapping takes effect
6530 only in that buffer. For instance, the mode my-mode could define a
6531 face `my-mode-default', and then in the mode setup function, do:
6533 (set (make-local-variable \\='face-remapping-alist)
6534 \\='((default my-mode-default)))).
6536 Because Emacs normally only redraws screen areas when the underlying
6537 buffer contents change, you may need to call `redraw-display' after
6538 changing this variable for it to take effect. */);
6539 Vface_remapping_alist = Qnil;
6541 DEFVAR_LISP ("face-font-rescale-alist", Vface_font_rescale_alist,
6542 doc: /* Alist of fonts vs the rescaling factors.
6543 Each element is a cons (FONT-PATTERN . RESCALE-RATIO), where
6544 FONT-PATTERN is a font-spec or a regular expression matching a font name, and
6545 RESCALE-RATIO is a floating point number to specify how much larger
6546 (or smaller) font we should use. For instance, if a face requests
6547 a font of 10 point, we actually use a font of 10 * RESCALE-RATIO point. */);
6548 Vface_font_rescale_alist = Qnil;
6550 #ifdef HAVE_WINDOW_SYSTEM
6551 defsubr (&Sbitmap_spec_p);
6552 defsubr (&Sx_list_fonts);
6553 defsubr (&Sinternal_face_x_get_resource);
6554 defsubr (&Sx_family_fonts);
6555 #endif