Remove a few simple cases of global redisplay
[emacs.git] / src / xfaces.c
bloba4f1aa89f718b6f940b51ad29a4e3e1c8491ca64
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 */
691 DEFUN ("clear-face-cache", Fclear_face_cache, Sclear_face_cache, 0, 1, 0,
692 doc: /* Clear face caches on all frames.
693 Optional THOROUGHLY non-nil means try to free unused fonts, too. */)
694 (Lisp_Object thoroughly)
696 clear_face_cache (!NILP (thoroughly));
697 face_change = true;
698 windows_or_buffers_changed = 53;
699 return Qnil;
703 /***********************************************************************
704 X Pixmaps
705 ***********************************************************************/
707 #ifdef HAVE_WINDOW_SYSTEM
709 DEFUN ("bitmap-spec-p", Fbitmap_spec_p, Sbitmap_spec_p, 1, 1, 0,
710 doc: /* Value is non-nil if OBJECT is a valid bitmap specification.
711 A bitmap specification is either a string, a file name, or a list
712 \(WIDTH HEIGHT DATA) where WIDTH is the pixel width of the bitmap,
713 HEIGHT is its height, and DATA is a string containing the bits of
714 the pixmap. Bits are stored row by row, each row occupies
715 \(WIDTH + 7)/8 bytes. */)
716 (Lisp_Object object)
718 bool pixmap_p = false;
720 if (STRINGP (object))
721 /* If OBJECT is a string, it's a file name. */
722 pixmap_p = true;
723 else if (CONSP (object))
725 /* Otherwise OBJECT must be (WIDTH HEIGHT DATA), WIDTH and
726 HEIGHT must be ints > 0, and DATA must be string large
727 enough to hold a bitmap of the specified size. */
728 Lisp_Object width, height, data;
730 height = width = data = Qnil;
732 if (CONSP (object))
734 width = XCAR (object);
735 object = XCDR (object);
736 if (CONSP (object))
738 height = XCAR (object);
739 object = XCDR (object);
740 if (CONSP (object))
741 data = XCAR (object);
745 if (STRINGP (data)
746 && RANGED_INTEGERP (1, width, INT_MAX)
747 && RANGED_INTEGERP (1, height, INT_MAX))
749 int bytes_per_row = ((XINT (width) + BITS_PER_CHAR - 1)
750 / BITS_PER_CHAR);
751 if (XINT (height) <= SBYTES (data) / bytes_per_row)
752 pixmap_p = true;
756 return pixmap_p ? Qt : Qnil;
760 /* Load a bitmap according to NAME (which is either a file name or a
761 pixmap spec) for use on frame F. Value is the bitmap_id (see
762 xfns.c). If NAME is nil, return with a bitmap id of zero. If
763 bitmap cannot be loaded, display a message saying so, and return
764 zero. */
766 static ptrdiff_t
767 load_pixmap (struct frame *f, Lisp_Object name)
769 ptrdiff_t bitmap_id;
771 if (NILP (name))
772 return 0;
774 CHECK_TYPE (!NILP (Fbitmap_spec_p (name)), Qbitmap_spec_p, name);
776 block_input ();
777 if (CONSP (name))
779 /* Decode a bitmap spec into a bitmap. */
781 int h, w;
782 Lisp_Object bits;
784 w = XINT (Fcar (name));
785 h = XINT (Fcar (Fcdr (name)));
786 bits = Fcar (Fcdr (Fcdr (name)));
788 bitmap_id = x_create_bitmap_from_data (f, SSDATA (bits),
789 w, h);
791 else
793 /* It must be a string -- a file name. */
794 bitmap_id = x_create_bitmap_from_file (f, name);
796 unblock_input ();
798 if (bitmap_id < 0)
800 add_to_log ("Invalid or undefined bitmap `%s'", name);
801 bitmap_id = 0;
803 else
805 #ifdef GLYPH_DEBUG
806 ++npixmaps_allocated;
807 #endif
810 return bitmap_id;
813 #endif /* HAVE_WINDOW_SYSTEM */
817 /***********************************************************************
818 X Colors
819 ***********************************************************************/
821 /* Parse RGB_LIST, and fill in the RGB fields of COLOR.
822 RGB_LIST should contain (at least) 3 lisp integers.
823 Return true iff RGB_LIST is OK. */
825 static bool
826 parse_rgb_list (Lisp_Object rgb_list, XColor *color)
828 #define PARSE_RGB_LIST_FIELD(field) \
829 if (CONSP (rgb_list) && INTEGERP (XCAR (rgb_list))) \
831 color->field = XINT (XCAR (rgb_list)); \
832 rgb_list = XCDR (rgb_list); \
834 else \
835 return false;
837 PARSE_RGB_LIST_FIELD (red);
838 PARSE_RGB_LIST_FIELD (green);
839 PARSE_RGB_LIST_FIELD (blue);
841 return true;
845 /* Lookup on frame F the color described by the lisp string COLOR.
846 The resulting tty color is returned in TTY_COLOR; if STD_COLOR is
847 non-zero, then the `standard' definition of the same color is
848 returned in it. */
850 static bool
851 tty_lookup_color (struct frame *f, Lisp_Object color, XColor *tty_color,
852 XColor *std_color)
854 Lisp_Object frame, color_desc;
856 if (!STRINGP (color) || NILP (Ffboundp (Qtty_color_desc)))
857 return false;
859 XSETFRAME (frame, f);
861 color_desc = call2 (Qtty_color_desc, color, frame);
862 if (CONSP (color_desc) && CONSP (XCDR (color_desc)))
864 Lisp_Object rgb;
866 if (! INTEGERP (XCAR (XCDR (color_desc))))
867 return false;
869 tty_color->pixel = XINT (XCAR (XCDR (color_desc)));
871 rgb = XCDR (XCDR (color_desc));
872 if (! parse_rgb_list (rgb, tty_color))
873 return false;
875 /* Should we fill in STD_COLOR too? */
876 if (std_color)
878 /* Default STD_COLOR to the same as TTY_COLOR. */
879 *std_color = *tty_color;
881 /* Do a quick check to see if the returned descriptor is
882 actually _exactly_ equal to COLOR, otherwise we have to
883 lookup STD_COLOR separately. If it's impossible to lookup
884 a standard color, we just give up and use TTY_COLOR. */
885 if ((!STRINGP (XCAR (color_desc))
886 || NILP (Fstring_equal (color, XCAR (color_desc))))
887 && !NILP (Ffboundp (Qtty_color_standard_values)))
889 /* Look up STD_COLOR separately. */
890 rgb = call1 (Qtty_color_standard_values, color);
891 if (! parse_rgb_list (rgb, std_color))
892 return false;
896 return true;
898 else if (NILP (Fsymbol_value (intern ("tty-defined-color-alist"))))
899 /* We were called early during startup, and the colors are not
900 yet set up in tty-defined-color-alist. Don't return a failure
901 indication, since this produces the annoying "Unable to
902 load color" messages in the *Messages* buffer. */
903 return true;
904 else
905 /* tty-color-desc seems to have returned a bad value. */
906 return false;
909 /* A version of defined_color for non-X frames. */
911 static bool
912 tty_defined_color (struct frame *f, const char *color_name,
913 XColor *color_def, bool alloc)
915 bool status = true;
917 /* Defaults. */
918 color_def->pixel = FACE_TTY_DEFAULT_COLOR;
919 color_def->red = 0;
920 color_def->blue = 0;
921 color_def->green = 0;
923 if (*color_name)
924 status = tty_lookup_color (f, build_string (color_name), color_def, NULL);
926 if (color_def->pixel == FACE_TTY_DEFAULT_COLOR && *color_name)
928 if (strcmp (color_name, "unspecified-fg") == 0)
929 color_def->pixel = FACE_TTY_DEFAULT_FG_COLOR;
930 else if (strcmp (color_name, "unspecified-bg") == 0)
931 color_def->pixel = FACE_TTY_DEFAULT_BG_COLOR;
934 if (color_def->pixel != FACE_TTY_DEFAULT_COLOR)
935 status = true;
937 return status;
941 /* Decide if color named COLOR_NAME is valid for the display
942 associated with the frame F; if so, return the rgb values in
943 COLOR_DEF. If ALLOC, allocate a new colormap cell.
945 This does the right thing for any type of frame. */
947 static bool
948 defined_color (struct frame *f, const char *color_name, XColor *color_def,
949 bool alloc)
951 if (!FRAME_WINDOW_P (f))
952 return tty_defined_color (f, color_name, color_def, alloc);
953 #ifdef HAVE_X_WINDOWS
954 else if (FRAME_X_P (f))
955 return x_defined_color (f, color_name, color_def, alloc);
956 #endif
957 #ifdef HAVE_NTGUI
958 else if (FRAME_W32_P (f))
959 return w32_defined_color (f, color_name, color_def, alloc);
960 #endif
961 #ifdef HAVE_NS
962 else if (FRAME_NS_P (f))
963 return ns_defined_color (f, color_name, color_def, alloc, true);
964 #endif
965 else
966 emacs_abort ();
970 /* Given the index IDX of a tty color on frame F, return its name, a
971 Lisp string. */
973 Lisp_Object
974 tty_color_name (struct frame *f, int idx)
976 if (idx >= 0 && !NILP (Ffboundp (Qtty_color_by_index)))
978 Lisp_Object frame;
979 Lisp_Object coldesc;
981 XSETFRAME (frame, f);
982 coldesc = call2 (Qtty_color_by_index, make_number (idx), frame);
984 if (!NILP (coldesc))
985 return XCAR (coldesc);
987 #ifdef MSDOS
988 /* We can have an MS-DOS frame under -nw for a short window of
989 opportunity before internal_terminal_init is called. DTRT. */
990 if (FRAME_MSDOS_P (f) && !inhibit_window_system)
991 return msdos_stdcolor_name (idx);
992 #endif
994 if (idx == FACE_TTY_DEFAULT_FG_COLOR)
995 return build_string (unspecified_fg);
996 if (idx == FACE_TTY_DEFAULT_BG_COLOR)
997 return build_string (unspecified_bg);
999 return Qunspecified;
1003 /* Return true if COLOR_NAME is a shade of gray (or white or
1004 black) on frame F.
1006 The criterion implemented here is not a terribly sophisticated one. */
1008 static bool
1009 face_color_gray_p (struct frame *f, const char *color_name)
1011 XColor color;
1012 bool gray_p;
1014 if (defined_color (f, color_name, &color, false))
1015 gray_p = (/* Any color sufficiently close to black counts as gray. */
1016 (color.red < 5000 && color.green < 5000 && color.blue < 5000)
1018 ((eabs (color.red - color.green)
1019 < max (color.red, color.green) / 20)
1020 && (eabs (color.green - color.blue)
1021 < max (color.green, color.blue) / 20)
1022 && (eabs (color.blue - color.red)
1023 < max (color.blue, color.red) / 20)));
1024 else
1025 gray_p = false;
1027 return gray_p;
1031 /* Return true if color COLOR_NAME can be displayed on frame F.
1032 BACKGROUND_P means the color will be used as background color. */
1034 static bool
1035 face_color_supported_p (struct frame *f, const char *color_name,
1036 bool background_p)
1038 Lisp_Object frame;
1039 XColor not_used;
1041 XSETFRAME (frame, f);
1042 return
1043 #ifdef HAVE_WINDOW_SYSTEM
1044 FRAME_WINDOW_P (f)
1045 ? (!NILP (Fxw_display_color_p (frame))
1046 || xstrcasecmp (color_name, "black") == 0
1047 || xstrcasecmp (color_name, "white") == 0
1048 || (background_p
1049 && face_color_gray_p (f, color_name))
1050 || (!NILP (Fx_display_grayscale_p (frame))
1051 && face_color_gray_p (f, color_name)))
1053 #endif
1054 tty_defined_color (f, color_name, &not_used, false);
1058 DEFUN ("color-gray-p", Fcolor_gray_p, Scolor_gray_p, 1, 2, 0,
1059 doc: /* Return non-nil if COLOR is a shade of gray (or white or black).
1060 FRAME specifies the frame and thus the display for interpreting COLOR.
1061 If FRAME is nil or omitted, use the selected frame. */)
1062 (Lisp_Object color, Lisp_Object frame)
1064 CHECK_STRING (color);
1065 return (face_color_gray_p (decode_any_frame (frame), SSDATA (color))
1066 ? Qt : Qnil);
1070 DEFUN ("color-supported-p", Fcolor_supported_p,
1071 Scolor_supported_p, 1, 3, 0,
1072 doc: /* Return non-nil if COLOR can be displayed on FRAME.
1073 BACKGROUND-P non-nil means COLOR is used as a background.
1074 Otherwise, this function tells whether it can be used as a foreground.
1075 If FRAME is nil or omitted, use the selected frame.
1076 COLOR must be a valid color name. */)
1077 (Lisp_Object color, Lisp_Object frame, Lisp_Object background_p)
1079 CHECK_STRING (color);
1080 return (face_color_supported_p (decode_any_frame (frame),
1081 SSDATA (color), !NILP (background_p))
1082 ? Qt : Qnil);
1086 static unsigned long
1087 load_color2 (struct frame *f, struct face *face, Lisp_Object name,
1088 enum lface_attribute_index target_index, XColor *color)
1090 eassert (STRINGP (name));
1091 eassert (target_index == LFACE_FOREGROUND_INDEX
1092 || target_index == LFACE_BACKGROUND_INDEX
1093 || target_index == LFACE_UNDERLINE_INDEX
1094 || target_index == LFACE_OVERLINE_INDEX
1095 || target_index == LFACE_STRIKE_THROUGH_INDEX
1096 || target_index == LFACE_BOX_INDEX);
1098 /* if the color map is full, defined_color will return a best match
1099 to the values in an existing cell. */
1100 if (!defined_color (f, SSDATA (name), color, true))
1102 add_to_log ("Unable to load color \"%s\"", name);
1104 switch (target_index)
1106 case LFACE_FOREGROUND_INDEX:
1107 face->foreground_defaulted_p = true;
1108 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1109 break;
1111 case LFACE_BACKGROUND_INDEX:
1112 face->background_defaulted_p = true;
1113 color->pixel = FRAME_BACKGROUND_PIXEL (f);
1114 break;
1116 case LFACE_UNDERLINE_INDEX:
1117 face->underline_defaulted_p = true;
1118 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1119 break;
1121 case LFACE_OVERLINE_INDEX:
1122 face->overline_color_defaulted_p = true;
1123 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1124 break;
1126 case LFACE_STRIKE_THROUGH_INDEX:
1127 face->strike_through_color_defaulted_p = true;
1128 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1129 break;
1131 case LFACE_BOX_INDEX:
1132 face->box_color_defaulted_p = true;
1133 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1134 break;
1136 default:
1137 emacs_abort ();
1140 #ifdef GLYPH_DEBUG
1141 else
1142 ++ncolors_allocated;
1143 #endif
1145 return color->pixel;
1148 /* Load color with name NAME for use by face FACE on frame F.
1149 TARGET_INDEX must be one of LFACE_FOREGROUND_INDEX,
1150 LFACE_BACKGROUND_INDEX, LFACE_UNDERLINE_INDEX, LFACE_OVERLINE_INDEX,
1151 LFACE_STRIKE_THROUGH_INDEX, or LFACE_BOX_INDEX. Value is the
1152 pixel color. If color cannot be loaded, display a message, and
1153 return the foreground, background or underline color of F, but
1154 record that fact in flags of the face so that we don't try to free
1155 these colors. */
1157 unsigned long
1158 load_color (struct frame *f, struct face *face, Lisp_Object name,
1159 enum lface_attribute_index target_index)
1161 XColor color;
1162 return load_color2 (f, face, name, target_index, &color);
1166 #ifdef HAVE_WINDOW_SYSTEM
1168 #define NEAR_SAME_COLOR_THRESHOLD 30000
1170 /* Load colors for face FACE which is used on frame F. Colors are
1171 specified by slots LFACE_BACKGROUND_INDEX and LFACE_FOREGROUND_INDEX
1172 of ATTRS. If the background color specified is not supported on F,
1173 try to emulate gray colors with a stipple from Vface_default_stipple. */
1175 static void
1176 load_face_colors (struct frame *f, struct face *face,
1177 Lisp_Object attrs[LFACE_VECTOR_SIZE])
1179 Lisp_Object fg, bg, dfg;
1180 XColor xfg, xbg;
1182 bg = attrs[LFACE_BACKGROUND_INDEX];
1183 fg = attrs[LFACE_FOREGROUND_INDEX];
1185 /* Swap colors if face is inverse-video. */
1186 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1188 Lisp_Object tmp;
1189 tmp = fg;
1190 fg = bg;
1191 bg = tmp;
1194 /* Check for support for foreground, not for background because
1195 face_color_supported_p is smart enough to know that grays are
1196 "supported" as background because we are supposed to use stipple
1197 for them. */
1198 if (!face_color_supported_p (f, SSDATA (bg), false)
1199 && !NILP (Fbitmap_spec_p (Vface_default_stipple)))
1201 x_destroy_bitmap (f, face->stipple);
1202 face->stipple = load_pixmap (f, Vface_default_stipple);
1205 face->background = load_color2 (f, face, bg, LFACE_BACKGROUND_INDEX, &xbg);
1206 face->foreground = load_color2 (f, face, fg, LFACE_FOREGROUND_INDEX, &xfg);
1208 dfg = attrs[LFACE_DISTANT_FOREGROUND_INDEX];
1209 if (!NILP (dfg) && !UNSPECIFIEDP (dfg)
1210 && color_distance (&xbg, &xfg) < NEAR_SAME_COLOR_THRESHOLD)
1212 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1213 face->background = load_color (f, face, dfg, LFACE_BACKGROUND_INDEX);
1214 else
1215 face->foreground = load_color (f, face, dfg, LFACE_FOREGROUND_INDEX);
1219 #ifdef HAVE_X_WINDOWS
1221 /* Free color PIXEL on frame F. */
1223 void
1224 unload_color (struct frame *f, unsigned long pixel)
1226 if (pixel != -1)
1228 block_input ();
1229 x_free_colors (f, &pixel, 1);
1230 unblock_input ();
1234 /* Free colors allocated for FACE. */
1236 static void
1237 free_face_colors (struct frame *f, struct face *face)
1239 /* PENDING(NS): need to do something here? */
1241 if (face->colors_copied_bitwise_p)
1242 return;
1244 block_input ();
1246 if (!face->foreground_defaulted_p)
1248 x_free_colors (f, &face->foreground, 1);
1249 IF_DEBUG (--ncolors_allocated);
1252 if (!face->background_defaulted_p)
1254 x_free_colors (f, &face->background, 1);
1255 IF_DEBUG (--ncolors_allocated);
1258 if (face->underline_p
1259 && !face->underline_defaulted_p)
1261 x_free_colors (f, &face->underline_color, 1);
1262 IF_DEBUG (--ncolors_allocated);
1265 if (face->overline_p
1266 && !face->overline_color_defaulted_p)
1268 x_free_colors (f, &face->overline_color, 1);
1269 IF_DEBUG (--ncolors_allocated);
1272 if (face->strike_through_p
1273 && !face->strike_through_color_defaulted_p)
1275 x_free_colors (f, &face->strike_through_color, 1);
1276 IF_DEBUG (--ncolors_allocated);
1279 if (face->box != FACE_NO_BOX
1280 && !face->box_color_defaulted_p)
1282 x_free_colors (f, &face->box_color, 1);
1283 IF_DEBUG (--ncolors_allocated);
1286 unblock_input ();
1289 #endif /* HAVE_X_WINDOWS */
1291 #endif /* HAVE_WINDOW_SYSTEM */
1295 /***********************************************************************
1296 XLFD Font Names
1297 ***********************************************************************/
1299 /* An enumerator for each field of an XLFD font name. */
1301 enum xlfd_field
1303 XLFD_FOUNDRY,
1304 XLFD_FAMILY,
1305 XLFD_WEIGHT,
1306 XLFD_SLANT,
1307 XLFD_SWIDTH,
1308 XLFD_ADSTYLE,
1309 XLFD_PIXEL_SIZE,
1310 XLFD_POINT_SIZE,
1311 XLFD_RESX,
1312 XLFD_RESY,
1313 XLFD_SPACING,
1314 XLFD_AVGWIDTH,
1315 XLFD_REGISTRY,
1316 XLFD_ENCODING,
1317 XLFD_LAST
1320 /* An enumerator for each possible slant value of a font. Taken from
1321 the XLFD specification. */
1323 enum xlfd_slant
1325 XLFD_SLANT_UNKNOWN,
1326 XLFD_SLANT_ROMAN,
1327 XLFD_SLANT_ITALIC,
1328 XLFD_SLANT_OBLIQUE,
1329 XLFD_SLANT_REVERSE_ITALIC,
1330 XLFD_SLANT_REVERSE_OBLIQUE,
1331 XLFD_SLANT_OTHER
1334 /* Relative font weight according to XLFD documentation. */
1336 enum xlfd_weight
1338 XLFD_WEIGHT_UNKNOWN,
1339 XLFD_WEIGHT_ULTRA_LIGHT, /* 10 */
1340 XLFD_WEIGHT_EXTRA_LIGHT, /* 20 */
1341 XLFD_WEIGHT_LIGHT, /* 30 */
1342 XLFD_WEIGHT_SEMI_LIGHT, /* 40: SemiLight, Book, ... */
1343 XLFD_WEIGHT_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1344 XLFD_WEIGHT_SEMI_BOLD, /* 60: SemiBold, DemiBold, ... */
1345 XLFD_WEIGHT_BOLD, /* 70: Bold, ... */
1346 XLFD_WEIGHT_EXTRA_BOLD, /* 80: ExtraBold, Heavy, ... */
1347 XLFD_WEIGHT_ULTRA_BOLD /* 90: UltraBold, Black, ... */
1350 /* Relative proportionate width. */
1352 enum xlfd_swidth
1354 XLFD_SWIDTH_UNKNOWN,
1355 XLFD_SWIDTH_ULTRA_CONDENSED, /* 10 */
1356 XLFD_SWIDTH_EXTRA_CONDENSED, /* 20 */
1357 XLFD_SWIDTH_CONDENSED, /* 30: Condensed, Narrow, Compressed, ... */
1358 XLFD_SWIDTH_SEMI_CONDENSED, /* 40: semicondensed */
1359 XLFD_SWIDTH_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1360 XLFD_SWIDTH_SEMI_EXPANDED, /* 60: SemiExpanded, DemiExpanded, ... */
1361 XLFD_SWIDTH_EXPANDED, /* 70: Expanded... */
1362 XLFD_SWIDTH_EXTRA_EXPANDED, /* 80: ExtraExpanded, Wide... */
1363 XLFD_SWIDTH_ULTRA_EXPANDED /* 90: UltraExpanded... */
1366 /* Order by which font selection chooses fonts. The default values
1367 mean `first, find a best match for the font width, then for the
1368 font height, then for weight, then for slant.' This variable can be
1369 set via set-face-font-sort-order. */
1371 static int font_sort_order[4];
1373 #ifdef HAVE_WINDOW_SYSTEM
1375 static enum font_property_index font_props_for_sorting[FONT_SIZE_INDEX];
1377 static int
1378 compare_fonts_by_sort_order (const void *v1, const void *v2)
1380 Lisp_Object const *p1 = v1;
1381 Lisp_Object const *p2 = v2;
1382 Lisp_Object font1 = *p1;
1383 Lisp_Object font2 = *p2;
1384 int i;
1386 for (i = 0; i < FONT_SIZE_INDEX; i++)
1388 enum font_property_index idx = font_props_for_sorting[i];
1389 Lisp_Object val1 = AREF (font1, idx), val2 = AREF (font2, idx);
1390 int result;
1392 if (idx <= FONT_REGISTRY_INDEX)
1394 if (STRINGP (val1))
1395 result = STRINGP (val2) ? strcmp (SSDATA (val1), SSDATA (val2)) : -1;
1396 else
1397 result = STRINGP (val2) ? 1 : 0;
1399 else
1401 if (INTEGERP (val1))
1402 result = (INTEGERP (val2) && XINT (val1) >= XINT (val2)
1403 ? XINT (val1) > XINT (val2)
1404 : -1);
1405 else
1406 result = INTEGERP (val2) ? 1 : 0;
1408 if (result)
1409 return result;
1411 return 0;
1414 DEFUN ("x-family-fonts", Fx_family_fonts, Sx_family_fonts, 0, 2, 0,
1415 doc: /* Return a list of available fonts of family FAMILY on FRAME.
1416 If FAMILY is omitted or nil, list all families.
1417 Otherwise, FAMILY must be a string, possibly containing wildcards
1418 `?' and `*'.
1419 If FRAME is omitted or nil, use the selected frame.
1420 Each element of the result is a vector [FAMILY WIDTH POINT-SIZE WEIGHT
1421 SLANT FIXED-P FULL REGISTRY-AND-ENCODING].
1422 FAMILY is the font family name. POINT-SIZE is the size of the
1423 font in 1/10 pt. WIDTH, WEIGHT, and SLANT are symbols describing the
1424 width, weight and slant of the font. These symbols are the same as for
1425 face attributes. FIXED-P is non-nil if the font is fixed-pitch.
1426 FULL is the full name of the font, and REGISTRY-AND-ENCODING is a string
1427 giving the registry and encoding of the font.
1428 The result list is sorted according to the current setting of
1429 the face font sort order. */)
1430 (Lisp_Object family, Lisp_Object frame)
1432 Lisp_Object font_spec, list, *drivers, vec;
1433 struct frame *f = decode_live_frame (frame);
1434 ptrdiff_t i, nfonts;
1435 EMACS_INT ndrivers;
1436 Lisp_Object result;
1437 USE_SAFE_ALLOCA;
1439 font_spec = Ffont_spec (0, NULL);
1440 if (!NILP (family))
1442 CHECK_STRING (family);
1443 font_parse_family_registry (family, Qnil, font_spec);
1446 list = font_list_entities (f, font_spec);
1447 if (NILP (list))
1448 return Qnil;
1450 /* Sort the font entities. */
1451 for (i = 0; i < 4; i++)
1452 switch (font_sort_order[i])
1454 case XLFD_SWIDTH:
1455 font_props_for_sorting[i] = FONT_WIDTH_INDEX; break;
1456 case XLFD_POINT_SIZE:
1457 font_props_for_sorting[i] = FONT_SIZE_INDEX; break;
1458 case XLFD_WEIGHT:
1459 font_props_for_sorting[i] = FONT_WEIGHT_INDEX; break;
1460 default:
1461 font_props_for_sorting[i] = FONT_SLANT_INDEX; break;
1463 font_props_for_sorting[i++] = FONT_FAMILY_INDEX;
1464 font_props_for_sorting[i++] = FONT_FOUNDRY_INDEX;
1465 font_props_for_sorting[i++] = FONT_ADSTYLE_INDEX;
1466 font_props_for_sorting[i++] = FONT_REGISTRY_INDEX;
1468 ndrivers = XINT (Flength (list));
1469 SAFE_ALLOCA_LISP (drivers, ndrivers);
1470 for (i = 0; i < ndrivers; i++, list = XCDR (list))
1471 drivers[i] = XCAR (list);
1472 vec = Fvconcat (ndrivers, drivers);
1473 nfonts = ASIZE (vec);
1475 qsort (XVECTOR (vec)->contents, nfonts, word_size,
1476 compare_fonts_by_sort_order);
1478 result = Qnil;
1479 for (i = nfonts - 1; i >= 0; --i)
1481 Lisp_Object font = AREF (vec, i);
1482 Lisp_Object v = make_uninit_vector (8);
1483 int point;
1484 Lisp_Object spacing;
1486 ASET (v, 0, AREF (font, FONT_FAMILY_INDEX));
1487 ASET (v, 1, FONT_WIDTH_SYMBOLIC (font));
1488 point = PIXEL_TO_POINT (XINT (AREF (font, FONT_SIZE_INDEX)) * 10,
1489 FRAME_RES_Y (f));
1490 ASET (v, 2, make_number (point));
1491 ASET (v, 3, FONT_WEIGHT_SYMBOLIC (font));
1492 ASET (v, 4, FONT_SLANT_SYMBOLIC (font));
1493 spacing = Ffont_get (font, QCspacing);
1494 ASET (v, 5, (NILP (spacing) || EQ (spacing, Qp)) ? Qnil : Qt);
1495 ASET (v, 6, Ffont_xlfd_name (font, Qnil));
1496 ASET (v, 7, AREF (font, FONT_REGISTRY_INDEX));
1498 result = Fcons (v, result);
1501 SAFE_FREE ();
1502 return result;
1505 DEFUN ("x-list-fonts", Fx_list_fonts, Sx_list_fonts, 1, 5, 0,
1506 doc: /* Return a list of the names of available fonts matching PATTERN.
1507 If optional arguments FACE and FRAME are specified, return only fonts
1508 the same size as FACE on FRAME.
1510 PATTERN should be a string containing a font name in the XLFD,
1511 Fontconfig, or GTK format. A font name given in the XLFD format may
1512 contain wildcard characters:
1513 the * character matches any substring, and
1514 the ? character matches any single character.
1515 PATTERN is case-insensitive.
1517 The return value is a list of strings, suitable as arguments to
1518 `set-face-font'.
1520 Fonts Emacs can't use may or may not be excluded
1521 even if they match PATTERN and FACE.
1522 The optional fourth argument MAXIMUM sets a limit on how many
1523 fonts to match. The first MAXIMUM fonts are reported.
1524 The optional fifth argument WIDTH, if specified, is a number of columns
1525 occupied by a character of a font. In that case, return only fonts
1526 the WIDTH times as wide as FACE on FRAME. */)
1527 (Lisp_Object pattern, Lisp_Object face, Lisp_Object frame,
1528 Lisp_Object maximum, Lisp_Object width)
1530 struct frame *f;
1531 int size, avgwidth IF_LINT (= 0);
1533 check_window_system (NULL);
1534 CHECK_STRING (pattern);
1536 if (! NILP (maximum))
1537 CHECK_NATNUM (maximum);
1539 if (!NILP (width))
1540 CHECK_NUMBER (width);
1542 /* We can't simply call decode_window_system_frame because
1543 this function may be called before any frame is created. */
1544 f = decode_live_frame (frame);
1545 if (! FRAME_WINDOW_P (f))
1547 /* Perhaps we have not yet created any frame. */
1548 f = NULL;
1549 frame = Qnil;
1550 face = Qnil;
1552 else
1553 XSETFRAME (frame, f);
1555 /* Determine the width standard for comparison with the fonts we find. */
1557 if (NILP (face))
1558 size = 0;
1559 else
1561 /* This is of limited utility since it works with character
1562 widths. Keep it for compatibility. --gerd. */
1563 int face_id = lookup_named_face (f, face, false);
1564 struct face *width_face = (face_id < 0
1565 ? NULL
1566 : FACE_FROM_ID (f, face_id));
1568 if (width_face && width_face->font)
1570 size = width_face->font->pixel_size;
1571 avgwidth = width_face->font->average_width;
1573 else
1575 size = FRAME_FONT (f)->pixel_size;
1576 avgwidth = FRAME_FONT (f)->average_width;
1578 if (!NILP (width))
1579 avgwidth *= XINT (width);
1582 Lisp_Object font_spec = font_spec_from_name (pattern);
1583 if (!FONTP (font_spec))
1584 signal_error ("Invalid font name", pattern);
1586 if (size)
1588 Ffont_put (font_spec, QCsize, make_number (size));
1589 Ffont_put (font_spec, QCavgwidth, make_number (avgwidth));
1591 Lisp_Object fonts = Flist_fonts (font_spec, frame, maximum, font_spec);
1592 for (Lisp_Object tail = fonts; CONSP (tail); tail = XCDR (tail))
1594 Lisp_Object font_entity;
1596 font_entity = XCAR (tail);
1597 if ((NILP (AREF (font_entity, FONT_SIZE_INDEX))
1598 || XINT (AREF (font_entity, FONT_SIZE_INDEX)) == 0)
1599 && ! NILP (AREF (font_spec, FONT_SIZE_INDEX)))
1601 /* This is a scalable font. For backward compatibility,
1602 we set the specified size. */
1603 font_entity = copy_font_spec (font_entity);
1604 ASET (font_entity, FONT_SIZE_INDEX,
1605 AREF (font_spec, FONT_SIZE_INDEX));
1607 XSETCAR (tail, Ffont_xlfd_name (font_entity, Qnil));
1609 if (NILP (frame))
1610 /* We don't have to check fontsets. */
1611 return fonts;
1612 Lisp_Object fontsets = list_fontsets (f, pattern, size);
1613 return CALLN (Fnconc, fonts, fontsets);
1616 #endif /* HAVE_WINDOW_SYSTEM */
1619 /***********************************************************************
1620 Lisp Faces
1621 ***********************************************************************/
1623 /* Access face attributes of face LFACE, a Lisp vector. */
1625 #define LFACE_FAMILY(LFACE) AREF ((LFACE), LFACE_FAMILY_INDEX)
1626 #define LFACE_FOUNDRY(LFACE) AREF ((LFACE), LFACE_FOUNDRY_INDEX)
1627 #define LFACE_HEIGHT(LFACE) AREF ((LFACE), LFACE_HEIGHT_INDEX)
1628 #define LFACE_WEIGHT(LFACE) AREF ((LFACE), LFACE_WEIGHT_INDEX)
1629 #define LFACE_SLANT(LFACE) AREF ((LFACE), LFACE_SLANT_INDEX)
1630 #define LFACE_UNDERLINE(LFACE) AREF ((LFACE), LFACE_UNDERLINE_INDEX)
1631 #define LFACE_INVERSE(LFACE) AREF ((LFACE), LFACE_INVERSE_INDEX)
1632 #define LFACE_FOREGROUND(LFACE) AREF ((LFACE), LFACE_FOREGROUND_INDEX)
1633 #define LFACE_BACKGROUND(LFACE) AREF ((LFACE), LFACE_BACKGROUND_INDEX)
1634 #define LFACE_STIPPLE(LFACE) AREF ((LFACE), LFACE_STIPPLE_INDEX)
1635 #define LFACE_SWIDTH(LFACE) AREF ((LFACE), LFACE_SWIDTH_INDEX)
1636 #define LFACE_OVERLINE(LFACE) AREF ((LFACE), LFACE_OVERLINE_INDEX)
1637 #define LFACE_STRIKE_THROUGH(LFACE) AREF ((LFACE), LFACE_STRIKE_THROUGH_INDEX)
1638 #define LFACE_BOX(LFACE) AREF ((LFACE), LFACE_BOX_INDEX)
1639 #define LFACE_FONT(LFACE) AREF ((LFACE), LFACE_FONT_INDEX)
1640 #define LFACE_INHERIT(LFACE) AREF ((LFACE), LFACE_INHERIT_INDEX)
1641 #define LFACE_FONTSET(LFACE) AREF ((LFACE), LFACE_FONTSET_INDEX)
1642 #define LFACE_DISTANT_FOREGROUND(LFACE) \
1643 AREF ((LFACE), LFACE_DISTANT_FOREGROUND_INDEX)
1645 /* True if LFACE is a Lisp face. A Lisp face is a vector of size
1646 LFACE_VECTOR_SIZE which has the symbol `face' in slot 0. */
1648 #define LFACEP(LFACE) \
1649 (VECTORP (LFACE) \
1650 && ASIZE (LFACE) == LFACE_VECTOR_SIZE \
1651 && EQ (AREF (LFACE, 0), Qface))
1654 #ifdef GLYPH_DEBUG
1656 /* Check consistency of Lisp face attribute vector ATTRS. */
1658 static void
1659 check_lface_attrs (Lisp_Object attrs[LFACE_VECTOR_SIZE])
1661 eassert (UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
1662 || IGNORE_DEFFACE_P (attrs[LFACE_FAMILY_INDEX])
1663 || STRINGP (attrs[LFACE_FAMILY_INDEX]));
1664 eassert (UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
1665 || IGNORE_DEFFACE_P (attrs[LFACE_FOUNDRY_INDEX])
1666 || STRINGP (attrs[LFACE_FOUNDRY_INDEX]));
1667 eassert (UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
1668 || IGNORE_DEFFACE_P (attrs[LFACE_SWIDTH_INDEX])
1669 || SYMBOLP (attrs[LFACE_SWIDTH_INDEX]));
1670 eassert (UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
1671 || IGNORE_DEFFACE_P (attrs[LFACE_HEIGHT_INDEX])
1672 || INTEGERP (attrs[LFACE_HEIGHT_INDEX])
1673 || FLOATP (attrs[LFACE_HEIGHT_INDEX])
1674 || FUNCTIONP (attrs[LFACE_HEIGHT_INDEX]));
1675 eassert (UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
1676 || IGNORE_DEFFACE_P (attrs[LFACE_WEIGHT_INDEX])
1677 || SYMBOLP (attrs[LFACE_WEIGHT_INDEX]));
1678 eassert (UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
1679 || IGNORE_DEFFACE_P (attrs[LFACE_SLANT_INDEX])
1680 || SYMBOLP (attrs[LFACE_SLANT_INDEX]));
1681 eassert (UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
1682 || IGNORE_DEFFACE_P (attrs[LFACE_UNDERLINE_INDEX])
1683 || SYMBOLP (attrs[LFACE_UNDERLINE_INDEX])
1684 || STRINGP (attrs[LFACE_UNDERLINE_INDEX])
1685 || CONSP (attrs[LFACE_UNDERLINE_INDEX]));
1686 eassert (UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
1687 || IGNORE_DEFFACE_P (attrs[LFACE_OVERLINE_INDEX])
1688 || SYMBOLP (attrs[LFACE_OVERLINE_INDEX])
1689 || STRINGP (attrs[LFACE_OVERLINE_INDEX]));
1690 eassert (UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1691 || IGNORE_DEFFACE_P (attrs[LFACE_STRIKE_THROUGH_INDEX])
1692 || SYMBOLP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1693 || STRINGP (attrs[LFACE_STRIKE_THROUGH_INDEX]));
1694 eassert (UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
1695 || IGNORE_DEFFACE_P (attrs[LFACE_BOX_INDEX])
1696 || SYMBOLP (attrs[LFACE_BOX_INDEX])
1697 || STRINGP (attrs[LFACE_BOX_INDEX])
1698 || INTEGERP (attrs[LFACE_BOX_INDEX])
1699 || CONSP (attrs[LFACE_BOX_INDEX]));
1700 eassert (UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
1701 || IGNORE_DEFFACE_P (attrs[LFACE_INVERSE_INDEX])
1702 || SYMBOLP (attrs[LFACE_INVERSE_INDEX]));
1703 eassert (UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
1704 || IGNORE_DEFFACE_P (attrs[LFACE_FOREGROUND_INDEX])
1705 || STRINGP (attrs[LFACE_FOREGROUND_INDEX]));
1706 eassert (UNSPECIFIEDP (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
1707 || IGNORE_DEFFACE_P (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
1708 || STRINGP (attrs[LFACE_DISTANT_FOREGROUND_INDEX]));
1709 eassert (UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
1710 || IGNORE_DEFFACE_P (attrs[LFACE_BACKGROUND_INDEX])
1711 || STRINGP (attrs[LFACE_BACKGROUND_INDEX]));
1712 eassert (UNSPECIFIEDP (attrs[LFACE_INHERIT_INDEX])
1713 || IGNORE_DEFFACE_P (attrs[LFACE_INHERIT_INDEX])
1714 || NILP (attrs[LFACE_INHERIT_INDEX])
1715 || SYMBOLP (attrs[LFACE_INHERIT_INDEX])
1716 || CONSP (attrs[LFACE_INHERIT_INDEX]));
1717 #ifdef HAVE_WINDOW_SYSTEM
1718 eassert (UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
1719 || IGNORE_DEFFACE_P (attrs[LFACE_STIPPLE_INDEX])
1720 || SYMBOLP (attrs[LFACE_STIPPLE_INDEX])
1721 || !NILP (Fbitmap_spec_p (attrs[LFACE_STIPPLE_INDEX])));
1722 eassert (UNSPECIFIEDP (attrs[LFACE_FONT_INDEX])
1723 || IGNORE_DEFFACE_P (attrs[LFACE_FONT_INDEX])
1724 || FONTP (attrs[LFACE_FONT_INDEX]));
1725 eassert (UNSPECIFIEDP (attrs[LFACE_FONTSET_INDEX])
1726 || STRINGP (attrs[LFACE_FONTSET_INDEX])
1727 || NILP (attrs[LFACE_FONTSET_INDEX]));
1728 #endif
1732 /* Check consistency of attributes of Lisp face LFACE (a Lisp vector). */
1734 static void
1735 check_lface (Lisp_Object lface)
1737 if (!NILP (lface))
1739 eassert (LFACEP (lface));
1740 check_lface_attrs (XVECTOR (lface)->contents);
1744 #else /* not GLYPH_DEBUG */
1746 #define check_lface_attrs(attrs) (void) 0
1747 #define check_lface(lface) (void) 0
1749 #endif /* GLYPH_DEBUG */
1753 /* Face-merge cycle checking. */
1755 enum named_merge_point_kind
1757 NAMED_MERGE_POINT_NORMAL,
1758 NAMED_MERGE_POINT_REMAP
1761 /* A `named merge point' is simply a point during face-merging where we
1762 look up a face by name. We keep a stack of which named lookups we're
1763 currently processing so that we can easily detect cycles, using a
1764 linked- list of struct named_merge_point structures, typically
1765 allocated on the stack frame of the named lookup functions which are
1766 active (so no consing is required). */
1767 struct named_merge_point
1769 Lisp_Object face_name;
1770 enum named_merge_point_kind named_merge_point_kind;
1771 struct named_merge_point *prev;
1775 /* If a face merging cycle is detected for FACE_NAME, return false,
1776 otherwise add NEW_NAMED_MERGE_POINT, which is initialized using
1777 FACE_NAME and NAMED_MERGE_POINT_KIND, as the head of the linked list
1778 pointed to by NAMED_MERGE_POINTS, and return true. */
1780 static bool
1781 push_named_merge_point (struct named_merge_point *new_named_merge_point,
1782 Lisp_Object face_name,
1783 enum named_merge_point_kind named_merge_point_kind,
1784 struct named_merge_point **named_merge_points)
1786 struct named_merge_point *prev;
1788 for (prev = *named_merge_points; prev; prev = prev->prev)
1789 if (EQ (face_name, prev->face_name))
1791 if (prev->named_merge_point_kind == named_merge_point_kind)
1792 /* A cycle, so fail. */
1793 return false;
1794 else if (prev->named_merge_point_kind == NAMED_MERGE_POINT_REMAP)
1795 /* A remap `hides ' any previous normal merge points
1796 (because the remap means that it's actually different face),
1797 so as we know the current merge point must be normal, we
1798 can just assume it's OK. */
1799 break;
1802 new_named_merge_point->face_name = face_name;
1803 new_named_merge_point->named_merge_point_kind = named_merge_point_kind;
1804 new_named_merge_point->prev = *named_merge_points;
1806 *named_merge_points = new_named_merge_point;
1808 return true;
1812 /* Resolve face name FACE_NAME. If FACE_NAME is a string, intern it
1813 to make it a symbol. If FACE_NAME is an alias for another face,
1814 return that face's name.
1816 Return default face in case of errors. */
1818 static Lisp_Object
1819 resolve_face_name (Lisp_Object face_name, bool signal_p)
1821 Lisp_Object orig_face;
1822 Lisp_Object tortoise, hare;
1824 if (STRINGP (face_name))
1825 face_name = Fintern (face_name, Qnil);
1827 if (NILP (face_name) || !SYMBOLP (face_name))
1828 return face_name;
1830 orig_face = face_name;
1831 tortoise = hare = face_name;
1833 while (true)
1835 face_name = hare;
1836 hare = Fget (hare, Qface_alias);
1837 if (NILP (hare) || !SYMBOLP (hare))
1838 break;
1840 face_name = hare;
1841 hare = Fget (hare, Qface_alias);
1842 if (NILP (hare) || !SYMBOLP (hare))
1843 break;
1845 tortoise = Fget (tortoise, Qface_alias);
1846 if (EQ (hare, tortoise))
1848 if (signal_p)
1849 xsignal1 (Qcircular_list, orig_face);
1850 return Qdefault;
1854 return face_name;
1858 /* Return the face definition of FACE_NAME on frame F. F null means
1859 return the definition for new frames. FACE_NAME may be a string or
1860 a symbol (apparently Emacs 20.2 allowed strings as face names in
1861 face text properties; Ediff uses that).
1862 If SIGNAL_P, signal an error if FACE_NAME is not a valid face name.
1863 Otherwise, value is nil if FACE_NAME is not a valid face name. */
1864 static Lisp_Object
1865 lface_from_face_name_no_resolve (struct frame *f, Lisp_Object face_name,
1866 bool signal_p)
1868 Lisp_Object lface;
1870 if (f)
1871 lface = assq_no_quit (face_name, f->face_alist);
1872 else
1873 lface = assq_no_quit (face_name, Vface_new_frame_defaults);
1875 if (CONSP (lface))
1876 lface = XCDR (lface);
1877 else if (signal_p)
1878 signal_error ("Invalid face", face_name);
1880 check_lface (lface);
1882 return lface;
1885 /* Return the face definition of FACE_NAME on frame F. F null means
1886 return the definition for new frames. FACE_NAME may be a string or
1887 a symbol (apparently Emacs 20.2 allowed strings as face names in
1888 face text properties; Ediff uses that). If FACE_NAME is an alias
1889 for another face, return that face's definition.
1890 If SIGNAL_P, signal an error if FACE_NAME is not a valid face name.
1891 Otherwise, value is nil if FACE_NAME is not a valid face name. */
1892 static Lisp_Object
1893 lface_from_face_name (struct frame *f, Lisp_Object face_name, bool signal_p)
1895 face_name = resolve_face_name (face_name, signal_p);
1896 return lface_from_face_name_no_resolve (f, face_name, signal_p);
1900 /* Get face attributes of face FACE_NAME from frame-local faces on
1901 frame F. Store the resulting attributes in ATTRS which must point
1902 to a vector of Lisp_Objects of size LFACE_VECTOR_SIZE.
1903 If SIGNAL_P, signal an error if FACE_NAME does not name a face.
1904 Otherwise, return true iff FACE_NAME is a face. */
1906 static bool
1907 get_lface_attributes_no_remap (struct frame *f, Lisp_Object face_name,
1908 Lisp_Object attrs[LFACE_VECTOR_SIZE],
1909 bool signal_p)
1911 Lisp_Object lface;
1913 lface = lface_from_face_name_no_resolve (f, face_name, signal_p);
1915 if (! NILP (lface))
1916 memcpy (attrs, XVECTOR (lface)->contents,
1917 LFACE_VECTOR_SIZE * sizeof *attrs);
1919 return !NILP (lface);
1922 /* Get face attributes of face FACE_NAME from frame-local faces on frame
1923 F. Store the resulting attributes in ATTRS which must point to a
1924 vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If FACE_NAME is an
1925 alias for another face, use that face's definition.
1926 If SIGNAL_P, signal an error if FACE_NAME does not name a face.
1927 Otherwise, return true iff FACE_NAME is a face. */
1929 static bool
1930 get_lface_attributes (struct frame *f, Lisp_Object face_name,
1931 Lisp_Object attrs[LFACE_VECTOR_SIZE], bool signal_p,
1932 struct named_merge_point *named_merge_points)
1934 Lisp_Object face_remapping;
1936 face_name = resolve_face_name (face_name, signal_p);
1938 /* See if SYMBOL has been remapped to some other face (usually this
1939 is done buffer-locally). */
1940 face_remapping = assq_no_quit (face_name, Vface_remapping_alist);
1941 if (CONSP (face_remapping))
1943 struct named_merge_point named_merge_point;
1945 if (push_named_merge_point (&named_merge_point,
1946 face_name, NAMED_MERGE_POINT_REMAP,
1947 &named_merge_points))
1949 int i;
1951 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
1952 attrs[i] = Qunspecified;
1954 return merge_face_ref (f, XCDR (face_remapping), attrs,
1955 signal_p, named_merge_points);
1959 /* Default case, no remapping. */
1960 return get_lface_attributes_no_remap (f, face_name, attrs, signal_p);
1964 /* True iff all attributes in face attribute vector ATTRS are
1965 specified, i.e. are non-nil. */
1967 static bool
1968 lface_fully_specified_p (Lisp_Object attrs[LFACE_VECTOR_SIZE])
1970 int i;
1972 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
1973 if (i != LFACE_FONT_INDEX && i != LFACE_INHERIT_INDEX
1974 && i != LFACE_DISTANT_FOREGROUND_INDEX)
1975 if ((UNSPECIFIEDP (attrs[i]) || IGNORE_DEFFACE_P (attrs[i])))
1976 break;
1978 return i == LFACE_VECTOR_SIZE;
1981 #ifdef HAVE_WINDOW_SYSTEM
1983 /* Set font-related attributes of Lisp face LFACE from FONT-OBJECT.
1984 If FORCE_P, set only unspecified attributes of LFACE. The
1985 exception is `font' attribute. It is set to FONT_OBJECT regardless
1986 of FORCE_P. */
1988 static void
1989 set_lface_from_font (struct frame *f, Lisp_Object lface,
1990 Lisp_Object font_object, bool force_p)
1992 Lisp_Object val;
1993 struct font *font = XFONT_OBJECT (font_object);
1995 /* Set attributes only if unspecified, otherwise face defaults for
1996 new frames would never take effect. If the font doesn't have a
1997 specific property, set a normal value for that. */
1999 if (force_p || UNSPECIFIEDP (LFACE_FAMILY (lface)))
2001 Lisp_Object family = AREF (font_object, FONT_FAMILY_INDEX);
2003 ASET (lface, LFACE_FAMILY_INDEX, SYMBOL_NAME (family));
2006 if (force_p || UNSPECIFIEDP (LFACE_FOUNDRY (lface)))
2008 Lisp_Object foundry = AREF (font_object, FONT_FOUNDRY_INDEX);
2010 ASET (lface, LFACE_FOUNDRY_INDEX, SYMBOL_NAME (foundry));
2013 if (force_p || UNSPECIFIEDP (LFACE_HEIGHT (lface)))
2015 int pt = PIXEL_TO_POINT (font->pixel_size * 10, FRAME_RES_Y (f));
2017 eassert (pt > 0);
2018 ASET (lface, LFACE_HEIGHT_INDEX, make_number (pt));
2021 if (force_p || UNSPECIFIEDP (LFACE_WEIGHT (lface)))
2023 val = FONT_WEIGHT_FOR_FACE (font_object);
2024 ASET (lface, LFACE_WEIGHT_INDEX, ! NILP (val) ? val :Qnormal);
2026 if (force_p || UNSPECIFIEDP (LFACE_SLANT (lface)))
2028 val = FONT_SLANT_FOR_FACE (font_object);
2029 ASET (lface, LFACE_SLANT_INDEX, ! NILP (val) ? val : Qnormal);
2031 if (force_p || UNSPECIFIEDP (LFACE_SWIDTH (lface)))
2033 val = FONT_WIDTH_FOR_FACE (font_object);
2034 ASET (lface, LFACE_SWIDTH_INDEX, ! NILP (val) ? val : Qnormal);
2037 ASET (lface, LFACE_FONT_INDEX, font_object);
2040 #endif /* HAVE_WINDOW_SYSTEM */
2043 /* Merges the face height FROM with the face height TO, and returns the
2044 merged height. If FROM is an invalid height, then INVALID is
2045 returned instead. FROM and TO may be either absolute face heights or
2046 `relative' heights; the returned value is always an absolute height
2047 unless both FROM and TO are relative. */
2049 static Lisp_Object
2050 merge_face_heights (Lisp_Object from, Lisp_Object to, Lisp_Object invalid)
2052 Lisp_Object result = invalid;
2054 if (INTEGERP (from))
2055 /* FROM is absolute, just use it as is. */
2056 result = from;
2057 else if (FLOATP (from))
2058 /* FROM is a scale, use it to adjust TO. */
2060 if (INTEGERP (to))
2061 /* relative X absolute => absolute */
2062 result = make_number (XFLOAT_DATA (from) * XINT (to));
2063 else if (FLOATP (to))
2064 /* relative X relative => relative */
2065 result = make_float (XFLOAT_DATA (from) * XFLOAT_DATA (to));
2066 else if (UNSPECIFIEDP (to))
2067 result = from;
2069 else if (FUNCTIONP (from))
2070 /* FROM is a function, which use to adjust TO. */
2072 /* Call function with current height as argument.
2073 From is the new height. */
2074 result = safe_call1 (from, to);
2076 /* Ensure that if TO was absolute, so is the result. */
2077 if (INTEGERP (to) && !INTEGERP (result))
2078 result = invalid;
2081 return result;
2085 /* Merge two Lisp face attribute vectors on frame F, FROM and TO, and
2086 store the resulting attributes in TO, which must be already be
2087 completely specified and contain only absolute attributes. Every
2088 specified attribute of FROM overrides the corresponding attribute of
2089 TO; relative attributes in FROM are merged with the absolute value in
2090 TO and replace it. NAMED_MERGE_POINTS is used internally to detect
2091 loops in face inheritance/remapping; it should be 0 when called from
2092 other places. */
2094 static void
2095 merge_face_vectors (struct frame *f, Lisp_Object *from, Lisp_Object *to,
2096 struct named_merge_point *named_merge_points)
2098 int i;
2099 Lisp_Object font = Qnil;
2101 /* If FROM inherits from some other faces, merge their attributes into
2102 TO before merging FROM's direct attributes. Note that an :inherit
2103 attribute of `unspecified' is the same as one of nil; we never
2104 merge :inherit attributes, so nil is more correct, but lots of
2105 other code uses `unspecified' as a generic value for face attributes. */
2106 if (!UNSPECIFIEDP (from[LFACE_INHERIT_INDEX])
2107 && !NILP (from[LFACE_INHERIT_INDEX]))
2108 merge_face_ref (f, from[LFACE_INHERIT_INDEX], to, false, named_merge_points);
2110 if (FONT_SPEC_P (from[LFACE_FONT_INDEX]))
2112 if (!UNSPECIFIEDP (to[LFACE_FONT_INDEX]))
2113 font = merge_font_spec (from[LFACE_FONT_INDEX], to[LFACE_FONT_INDEX]);
2114 else
2115 font = copy_font_spec (from[LFACE_FONT_INDEX]);
2116 to[LFACE_FONT_INDEX] = font;
2119 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2120 if (!UNSPECIFIEDP (from[i]))
2122 if (i == LFACE_HEIGHT_INDEX && !INTEGERP (from[i]))
2124 to[i] = merge_face_heights (from[i], to[i], to[i]);
2125 font_clear_prop (to, FONT_SIZE_INDEX);
2127 else if (i != LFACE_FONT_INDEX && ! EQ (to[i], from[i]))
2129 to[i] = from[i];
2130 if (i >= LFACE_FAMILY_INDEX && i <=LFACE_SLANT_INDEX)
2131 font_clear_prop (to,
2132 (i == LFACE_FAMILY_INDEX ? FONT_FAMILY_INDEX
2133 : i == LFACE_FOUNDRY_INDEX ? FONT_FOUNDRY_INDEX
2134 : i == LFACE_SWIDTH_INDEX ? FONT_WIDTH_INDEX
2135 : i == LFACE_HEIGHT_INDEX ? FONT_SIZE_INDEX
2136 : i == LFACE_WEIGHT_INDEX ? FONT_WEIGHT_INDEX
2137 : FONT_SLANT_INDEX));
2141 /* If FROM specifies a font spec, make its contents take precedence
2142 over :family and other attributes. This is needed for face
2143 remapping using :font to work. */
2145 if (!NILP (font))
2147 if (! NILP (AREF (font, FONT_FOUNDRY_INDEX)))
2148 to[LFACE_FOUNDRY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FOUNDRY_INDEX));
2149 if (! NILP (AREF (font, FONT_FAMILY_INDEX)))
2150 to[LFACE_FAMILY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FAMILY_INDEX));
2151 if (! NILP (AREF (font, FONT_WEIGHT_INDEX)))
2152 to[LFACE_WEIGHT_INDEX] = FONT_WEIGHT_FOR_FACE (font);
2153 if (! NILP (AREF (font, FONT_SLANT_INDEX)))
2154 to[LFACE_SLANT_INDEX] = FONT_SLANT_FOR_FACE (font);
2155 if (! NILP (AREF (font, FONT_WIDTH_INDEX)))
2156 to[LFACE_SWIDTH_INDEX] = FONT_WIDTH_FOR_FACE (font);
2157 ASET (font, FONT_SIZE_INDEX, Qnil);
2160 /* TO is always an absolute face, which should inherit from nothing.
2161 We blindly copy the :inherit attribute above and fix it up here. */
2162 to[LFACE_INHERIT_INDEX] = Qnil;
2165 /* Merge the named face FACE_NAME on frame F, into the vector of face
2166 attributes TO. Use NAMED_MERGE_POINTS to detect loops in face
2167 inheritance. Return true if FACE_NAME is a valid face name and
2168 merging succeeded. */
2170 static bool
2171 merge_named_face (struct frame *f, Lisp_Object face_name, Lisp_Object *to,
2172 struct named_merge_point *named_merge_points)
2174 struct named_merge_point named_merge_point;
2176 if (push_named_merge_point (&named_merge_point,
2177 face_name, NAMED_MERGE_POINT_NORMAL,
2178 &named_merge_points))
2180 Lisp_Object from[LFACE_VECTOR_SIZE];
2181 bool ok = get_lface_attributes (f, face_name, from, false,
2182 named_merge_points);
2184 if (ok)
2185 merge_face_vectors (f, from, to, named_merge_points);
2187 return ok;
2189 else
2190 return false;
2194 /* Merge face attributes from the lisp `face reference' FACE_REF on
2195 frame F into the face attribute vector TO. If ERR_MSGS,
2196 problems with FACE_REF cause an error message to be shown. Return
2197 true if no errors occurred (regardless of the value of ERR_MSGS).
2198 Use NAMED_MERGE_POINTS to detect loops in face inheritance or
2199 list structure; it may be 0 for most callers.
2201 FACE_REF may be a single face specification or a list of such
2202 specifications. Each face specification can be:
2204 1. A symbol or string naming a Lisp face.
2206 2. A property list of the form (KEYWORD VALUE ...) where each
2207 KEYWORD is a face attribute name, and value is an appropriate value
2208 for that attribute.
2210 3. Conses or the form (FOREGROUND-COLOR . COLOR) or
2211 (BACKGROUND-COLOR . COLOR) where COLOR is a color name. This is
2212 for compatibility with 20.2.
2214 Face specifications earlier in lists take precedence over later
2215 specifications. */
2217 static bool
2218 merge_face_ref (struct frame *f, Lisp_Object face_ref, Lisp_Object *to,
2219 bool err_msgs, struct named_merge_point *named_merge_points)
2221 bool ok = true; /* Succeed without an error? */
2223 if (CONSP (face_ref))
2225 Lisp_Object first = XCAR (face_ref);
2227 if (EQ (first, Qforeground_color)
2228 || EQ (first, Qbackground_color))
2230 /* One of (FOREGROUND-COLOR . COLOR) or (BACKGROUND-COLOR
2231 . COLOR). COLOR must be a string. */
2232 Lisp_Object color_name = XCDR (face_ref);
2233 Lisp_Object color = first;
2235 if (STRINGP (color_name))
2237 if (EQ (color, Qforeground_color))
2238 to[LFACE_FOREGROUND_INDEX] = color_name;
2239 else
2240 to[LFACE_BACKGROUND_INDEX] = color_name;
2242 else
2244 if (err_msgs)
2245 add_to_log ("Invalid face color %S", color_name);
2246 ok = false;
2249 else if (SYMBOLP (first)
2250 && *SDATA (SYMBOL_NAME (first)) == ':')
2252 /* Assume this is the property list form. */
2253 while (CONSP (face_ref) && CONSP (XCDR (face_ref)))
2255 Lisp_Object keyword = XCAR (face_ref);
2256 Lisp_Object value = XCAR (XCDR (face_ref));
2257 bool err = false;
2259 /* Specifying `unspecified' is a no-op. */
2260 if (EQ (value, Qunspecified))
2262 else if (EQ (keyword, QCfamily))
2264 if (STRINGP (value))
2266 to[LFACE_FAMILY_INDEX] = value;
2267 font_clear_prop (to, FONT_FAMILY_INDEX);
2269 else
2270 err = true;
2272 else if (EQ (keyword, QCfoundry))
2274 if (STRINGP (value))
2276 to[LFACE_FOUNDRY_INDEX] = value;
2277 font_clear_prop (to, FONT_FOUNDRY_INDEX);
2279 else
2280 err = true;
2282 else if (EQ (keyword, QCheight))
2284 Lisp_Object new_height =
2285 merge_face_heights (value, to[LFACE_HEIGHT_INDEX], Qnil);
2287 if (! NILP (new_height))
2289 to[LFACE_HEIGHT_INDEX] = new_height;
2290 font_clear_prop (to, FONT_SIZE_INDEX);
2292 else
2293 err = true;
2295 else if (EQ (keyword, QCweight))
2297 if (SYMBOLP (value) && FONT_WEIGHT_NAME_NUMERIC (value) >= 0)
2299 to[LFACE_WEIGHT_INDEX] = value;
2300 font_clear_prop (to, FONT_WEIGHT_INDEX);
2302 else
2303 err = true;
2305 else if (EQ (keyword, QCslant))
2307 if (SYMBOLP (value) && FONT_SLANT_NAME_NUMERIC (value) >= 0)
2309 to[LFACE_SLANT_INDEX] = value;
2310 font_clear_prop (to, FONT_SLANT_INDEX);
2312 else
2313 err = true;
2315 else if (EQ (keyword, QCunderline))
2317 if (EQ (value, Qt)
2318 || NILP (value)
2319 || STRINGP (value)
2320 || CONSP (value))
2321 to[LFACE_UNDERLINE_INDEX] = value;
2322 else
2323 err = true;
2325 else if (EQ (keyword, QCoverline))
2327 if (EQ (value, Qt)
2328 || NILP (value)
2329 || STRINGP (value))
2330 to[LFACE_OVERLINE_INDEX] = value;
2331 else
2332 err = true;
2334 else if (EQ (keyword, QCstrike_through))
2336 if (EQ (value, Qt)
2337 || NILP (value)
2338 || STRINGP (value))
2339 to[LFACE_STRIKE_THROUGH_INDEX] = value;
2340 else
2341 err = true;
2343 else if (EQ (keyword, QCbox))
2345 if (EQ (value, Qt))
2346 value = make_number (1);
2347 if (INTEGERP (value)
2348 || STRINGP (value)
2349 || CONSP (value)
2350 || NILP (value))
2351 to[LFACE_BOX_INDEX] = value;
2352 else
2353 err = true;
2355 else if (EQ (keyword, QCinverse_video)
2356 || EQ (keyword, QCreverse_video))
2358 if (EQ (value, Qt) || NILP (value))
2359 to[LFACE_INVERSE_INDEX] = value;
2360 else
2361 err = true;
2363 else if (EQ (keyword, QCforeground))
2365 if (STRINGP (value))
2366 to[LFACE_FOREGROUND_INDEX] = value;
2367 else
2368 err = true;
2370 else if (EQ (keyword, QCdistant_foreground))
2372 if (STRINGP (value))
2373 to[LFACE_DISTANT_FOREGROUND_INDEX] = value;
2374 else
2375 err = true;
2377 else if (EQ (keyword, QCbackground))
2379 if (STRINGP (value))
2380 to[LFACE_BACKGROUND_INDEX] = value;
2381 else
2382 err = true;
2384 else if (EQ (keyword, QCstipple))
2386 #if defined (HAVE_WINDOW_SYSTEM)
2387 Lisp_Object pixmap_p = Fbitmap_spec_p (value);
2388 if (!NILP (pixmap_p))
2389 to[LFACE_STIPPLE_INDEX] = value;
2390 else
2391 err = true;
2392 #endif /* HAVE_WINDOW_SYSTEM */
2394 else if (EQ (keyword, QCwidth))
2396 if (SYMBOLP (value) && FONT_WIDTH_NAME_NUMERIC (value) >= 0)
2398 to[LFACE_SWIDTH_INDEX] = value;
2399 font_clear_prop (to, FONT_WIDTH_INDEX);
2401 else
2402 err = true;
2404 else if (EQ (keyword, QCfont))
2406 if (FONTP (value))
2407 to[LFACE_FONT_INDEX] = value;
2408 else
2409 err = true;
2411 else if (EQ (keyword, QCinherit))
2413 /* This is not really very useful; it's just like a
2414 normal face reference. */
2415 if (! merge_face_ref (f, value, to,
2416 err_msgs, named_merge_points))
2417 err = true;
2419 else
2420 err = true;
2422 if (err)
2424 add_to_log ("Invalid face attribute %S %S", keyword, value);
2425 ok = false;
2428 face_ref = XCDR (XCDR (face_ref));
2431 else
2433 /* This is a list of face refs. Those at the beginning of the
2434 list take precedence over what follows, so we have to merge
2435 from the end backwards. */
2436 Lisp_Object next = XCDR (face_ref);
2438 if (! NILP (next))
2439 ok = merge_face_ref (f, next, to, err_msgs, named_merge_points);
2441 if (! merge_face_ref (f, first, to, err_msgs, named_merge_points))
2442 ok = false;
2445 else
2447 /* FACE_REF ought to be a face name. */
2448 ok = merge_named_face (f, face_ref, to, named_merge_points);
2449 if (!ok && err_msgs)
2450 add_to_log ("Invalid face reference: %s", face_ref);
2453 return ok;
2457 DEFUN ("internal-make-lisp-face", Finternal_make_lisp_face,
2458 Sinternal_make_lisp_face, 1, 2, 0,
2459 doc: /* Make FACE, a symbol, a Lisp face with all attributes nil.
2460 If FACE was not known as a face before, create a new one.
2461 If optional argument FRAME is specified, make a frame-local face
2462 for that frame. Otherwise operate on the global face definition.
2463 Value is a vector of face attributes. */)
2464 (Lisp_Object face, Lisp_Object frame)
2466 Lisp_Object global_lface, lface;
2467 struct frame *f;
2468 int i;
2470 CHECK_SYMBOL (face);
2471 global_lface = lface_from_face_name (NULL, face, false);
2473 if (!NILP (frame))
2475 CHECK_LIVE_FRAME (frame);
2476 f = XFRAME (frame);
2477 lface = lface_from_face_name (f, face, false);
2479 else
2480 f = NULL, lface = Qnil;
2482 /* Add a global definition if there is none. */
2483 if (NILP (global_lface))
2485 global_lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2486 Qunspecified);
2487 ASET (global_lface, 0, Qface);
2488 Vface_new_frame_defaults = Fcons (Fcons (face, global_lface),
2489 Vface_new_frame_defaults);
2491 /* Assign the new Lisp face a unique ID. The mapping from Lisp
2492 face id to Lisp face is given by the vector lface_id_to_name.
2493 The mapping from Lisp face to Lisp face id is given by the
2494 property `face' of the Lisp face name. */
2495 if (next_lface_id == lface_id_to_name_size)
2496 lface_id_to_name =
2497 xpalloc (lface_id_to_name, &lface_id_to_name_size, 1, MAX_FACE_ID,
2498 sizeof *lface_id_to_name);
2500 lface_id_to_name[next_lface_id] = face;
2501 Fput (face, Qface, make_number (next_lface_id));
2502 ++next_lface_id;
2504 else if (f == NULL)
2505 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2506 ASET (global_lface, i, Qunspecified);
2508 /* Add a frame-local definition. */
2509 if (f)
2511 if (NILP (lface))
2513 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2514 Qunspecified);
2515 ASET (lface, 0, Qface);
2516 fset_face_alist (f, Fcons (Fcons (face, lface), f->face_alist));
2518 else
2519 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2520 ASET (lface, i, Qunspecified);
2522 else
2523 lface = global_lface;
2525 /* Changing a named face means that all realized faces depending on
2526 that face are invalid. Since we cannot tell which realized faces
2527 depend on the face, make sure they are all removed. This is done
2528 by setting face_change. The next call to init_iterator will then
2529 free realized faces. */
2530 if (NILP (Fget (face, Qface_no_inherit)))
2532 face_change = true;
2533 windows_or_buffers_changed = 54;
2536 eassert (LFACEP (lface));
2537 check_lface (lface);
2538 return lface;
2542 DEFUN ("internal-lisp-face-p", Finternal_lisp_face_p,
2543 Sinternal_lisp_face_p, 1, 2, 0,
2544 doc: /* Return non-nil if FACE names a face.
2545 FACE should be a symbol or string.
2546 If optional second argument FRAME is non-nil, check for the
2547 existence of a frame-local face with name FACE on that frame.
2548 Otherwise check for the existence of a global face. */)
2549 (Lisp_Object face, Lisp_Object frame)
2551 Lisp_Object lface;
2553 face = resolve_face_name (face, true);
2555 if (!NILP (frame))
2557 CHECK_LIVE_FRAME (frame);
2558 lface = lface_from_face_name (XFRAME (frame), face, false);
2560 else
2561 lface = lface_from_face_name (NULL, face, false);
2563 return lface;
2567 DEFUN ("internal-copy-lisp-face", Finternal_copy_lisp_face,
2568 Sinternal_copy_lisp_face, 4, 4, 0,
2569 doc: /* Copy face FROM to TO.
2570 If FRAME is t, copy the global face definition of FROM.
2571 Otherwise, copy the frame-local definition of FROM on FRAME.
2572 If NEW-FRAME is a frame, copy that data into the frame-local
2573 definition of TO on NEW-FRAME. If NEW-FRAME is nil,
2574 FRAME controls where the data is copied to.
2576 The value is TO. */)
2577 (Lisp_Object from, Lisp_Object to, Lisp_Object frame, Lisp_Object new_frame)
2579 Lisp_Object lface, copy;
2581 CHECK_SYMBOL (from);
2582 CHECK_SYMBOL (to);
2584 if (EQ (frame, Qt))
2586 /* Copy global definition of FROM. We don't make copies of
2587 strings etc. because 20.2 didn't do it either. */
2588 lface = lface_from_face_name (NULL, from, true);
2589 copy = Finternal_make_lisp_face (to, Qnil);
2591 else
2593 /* Copy frame-local definition of FROM. */
2594 if (NILP (new_frame))
2595 new_frame = frame;
2596 CHECK_LIVE_FRAME (frame);
2597 CHECK_LIVE_FRAME (new_frame);
2598 lface = lface_from_face_name (XFRAME (frame), from, true);
2599 copy = Finternal_make_lisp_face (to, new_frame);
2602 vcopy (copy, 0, XVECTOR (lface)->contents, LFACE_VECTOR_SIZE);
2604 /* Changing a named face means that all realized faces depending on
2605 that face are invalid. Since we cannot tell which realized faces
2606 depend on the face, make sure they are all removed. This is done
2607 by setting face_change. The next call to init_iterator will then
2608 free realized faces. */
2609 if (NILP (Fget (to, Qface_no_inherit)))
2611 face_change = true;
2612 windows_or_buffers_changed = 55;
2615 return to;
2619 DEFUN ("internal-set-lisp-face-attribute", Finternal_set_lisp_face_attribute,
2620 Sinternal_set_lisp_face_attribute, 3, 4, 0,
2621 doc: /* Set attribute ATTR of FACE to VALUE.
2622 FRAME being a frame means change the face on that frame.
2623 FRAME nil means change the face of the selected frame.
2624 FRAME t means change the default for new frames.
2625 FRAME 0 means change the face on all frames, and change the default
2626 for new frames. */)
2627 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
2629 Lisp_Object lface;
2630 Lisp_Object old_value = Qnil;
2631 /* Set one of enum font_property_index (> 0) if ATTR is one of
2632 font-related attributes other than QCfont and QCfontset. */
2633 enum font_property_index prop_index = 0;
2635 CHECK_SYMBOL (face);
2636 CHECK_SYMBOL (attr);
2638 face = resolve_face_name (face, true);
2640 /* If FRAME is 0, change face on all frames, and change the
2641 default for new frames. */
2642 if (INTEGERP (frame) && XINT (frame) == 0)
2644 Lisp_Object tail;
2645 Finternal_set_lisp_face_attribute (face, attr, value, Qt);
2646 FOR_EACH_FRAME (tail, frame)
2647 Finternal_set_lisp_face_attribute (face, attr, value, frame);
2648 return face;
2651 /* Set lface to the Lisp attribute vector of FACE. */
2652 if (EQ (frame, Qt))
2654 lface = lface_from_face_name (NULL, face, true);
2656 /* When updating face-new-frame-defaults, we put :ignore-defface
2657 where the caller wants `unspecified'. This forces the frame
2658 defaults to ignore the defface value. Otherwise, the defface
2659 will take effect, which is generally not what is intended.
2660 The value of that attribute will be inherited from some other
2661 face during face merging. See internal_merge_in_global_face. */
2662 if (UNSPECIFIEDP (value))
2663 value = QCignore_defface;
2665 else
2667 if (NILP (frame))
2668 frame = selected_frame;
2670 CHECK_LIVE_FRAME (frame);
2671 lface = lface_from_face_name (XFRAME (frame), face, false);
2673 /* If a frame-local face doesn't exist yet, create one. */
2674 if (NILP (lface))
2675 lface = Finternal_make_lisp_face (face, frame);
2678 if (EQ (attr, QCfamily))
2680 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2682 CHECK_STRING (value);
2683 if (SCHARS (value) == 0)
2684 signal_error ("Invalid face family", value);
2686 old_value = LFACE_FAMILY (lface);
2687 ASET (lface, LFACE_FAMILY_INDEX, value);
2688 prop_index = FONT_FAMILY_INDEX;
2690 else if (EQ (attr, QCfoundry))
2692 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2694 CHECK_STRING (value);
2695 if (SCHARS (value) == 0)
2696 signal_error ("Invalid face foundry", value);
2698 old_value = LFACE_FOUNDRY (lface);
2699 ASET (lface, LFACE_FOUNDRY_INDEX, value);
2700 prop_index = FONT_FOUNDRY_INDEX;
2702 else if (EQ (attr, QCheight))
2704 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2706 if (EQ (face, Qdefault))
2708 /* The default face must have an absolute size. */
2709 if (!INTEGERP (value) || XINT (value) <= 0)
2710 signal_error ("Default face height not absolute and positive",
2711 value);
2713 else
2715 /* For non-default faces, do a test merge with a random
2716 height to see if VALUE's ok. */
2717 Lisp_Object test = merge_face_heights (value,
2718 make_number (10),
2719 Qnil);
2720 if (!INTEGERP (test) || XINT (test) <= 0)
2721 signal_error ("Face height does not produce a positive integer",
2722 value);
2726 old_value = LFACE_HEIGHT (lface);
2727 ASET (lface, LFACE_HEIGHT_INDEX, value);
2728 prop_index = FONT_SIZE_INDEX;
2730 else if (EQ (attr, QCweight))
2732 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2734 CHECK_SYMBOL (value);
2735 if (FONT_WEIGHT_NAME_NUMERIC (value) < 0)
2736 signal_error ("Invalid face weight", value);
2738 old_value = LFACE_WEIGHT (lface);
2739 ASET (lface, LFACE_WEIGHT_INDEX, value);
2740 prop_index = FONT_WEIGHT_INDEX;
2742 else if (EQ (attr, QCslant))
2744 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2746 CHECK_SYMBOL (value);
2747 if (FONT_SLANT_NAME_NUMERIC (value) < 0)
2748 signal_error ("Invalid face slant", value);
2750 old_value = LFACE_SLANT (lface);
2751 ASET (lface, LFACE_SLANT_INDEX, value);
2752 prop_index = FONT_SLANT_INDEX;
2754 else if (EQ (attr, QCunderline))
2756 bool valid_p = false;
2758 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2759 valid_p = true;
2760 else if (NILP (value) || EQ (value, Qt))
2761 valid_p = true;
2762 else if (STRINGP (value) && SCHARS (value) > 0)
2763 valid_p = true;
2764 else if (CONSP (value))
2766 Lisp_Object key, val, list;
2768 list = value;
2769 /* FIXME? This errs on the side of acceptance. Eg it accepts:
2770 (defface foo '((t :underline 'foo) "doc")
2771 Maybe this is intentional, maybe it isn't.
2772 Non-nil symbols other than t are not documented as being valid.
2773 Eg compare with inverse-video, which explicitly rejects them.
2775 valid_p = true;
2777 while (!NILP (CAR_SAFE(list)))
2779 key = CAR_SAFE (list);
2780 list = CDR_SAFE (list);
2781 val = CAR_SAFE (list);
2782 list = CDR_SAFE (list);
2784 if (NILP (key) || NILP (val))
2786 valid_p = false;
2787 break;
2790 else if (EQ (key, QCcolor)
2791 && !(EQ (val, Qforeground_color)
2792 || (STRINGP (val) && SCHARS (val) > 0)))
2794 valid_p = false;
2795 break;
2798 else if (EQ (key, QCstyle)
2799 && !(EQ (val, Qline) || EQ (val, Qwave)))
2801 valid_p = false;
2802 break;
2807 if (!valid_p)
2808 signal_error ("Invalid face underline", value);
2810 old_value = LFACE_UNDERLINE (lface);
2811 ASET (lface, LFACE_UNDERLINE_INDEX, value);
2813 else if (EQ (attr, QCoverline))
2815 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2816 if ((SYMBOLP (value)
2817 && !EQ (value, Qt)
2818 && !EQ (value, Qnil))
2819 /* Overline color. */
2820 || (STRINGP (value)
2821 && SCHARS (value) == 0))
2822 signal_error ("Invalid face overline", value);
2824 old_value = LFACE_OVERLINE (lface);
2825 ASET (lface, LFACE_OVERLINE_INDEX, value);
2827 else if (EQ (attr, QCstrike_through))
2829 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2830 if ((SYMBOLP (value)
2831 && !EQ (value, Qt)
2832 && !EQ (value, Qnil))
2833 /* Strike-through color. */
2834 || (STRINGP (value)
2835 && SCHARS (value) == 0))
2836 signal_error ("Invalid face strike-through", value);
2838 old_value = LFACE_STRIKE_THROUGH (lface);
2839 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, value);
2841 else if (EQ (attr, QCbox))
2843 bool valid_p;
2845 /* Allow t meaning a simple box of width 1 in foreground color
2846 of the face. */
2847 if (EQ (value, Qt))
2848 value = make_number (1);
2850 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2851 valid_p = true;
2852 else if (NILP (value))
2853 valid_p = true;
2854 else if (INTEGERP (value))
2855 valid_p = XINT (value) != 0;
2856 else if (STRINGP (value))
2857 valid_p = SCHARS (value) > 0;
2858 else if (CONSP (value))
2860 Lisp_Object tem;
2862 tem = value;
2863 while (CONSP (tem))
2865 Lisp_Object k, v;
2867 k = XCAR (tem);
2868 tem = XCDR (tem);
2869 if (!CONSP (tem))
2870 break;
2871 v = XCAR (tem);
2872 tem = XCDR (tem);
2874 if (EQ (k, QCline_width))
2876 if (!INTEGERP (v) || XINT (v) == 0)
2877 break;
2879 else if (EQ (k, QCcolor))
2881 if (!NILP (v) && (!STRINGP (v) || SCHARS (v) == 0))
2882 break;
2884 else if (EQ (k, QCstyle))
2886 if (!EQ (v, Qpressed_button) && !EQ (v, Qreleased_button))
2887 break;
2889 else
2890 break;
2893 valid_p = NILP (tem);
2895 else
2896 valid_p = false;
2898 if (!valid_p)
2899 signal_error ("Invalid face box", value);
2901 old_value = LFACE_BOX (lface);
2902 ASET (lface, LFACE_BOX_INDEX, value);
2904 else if (EQ (attr, QCinverse_video)
2905 || EQ (attr, QCreverse_video))
2907 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2909 CHECK_SYMBOL (value);
2910 if (!EQ (value, Qt) && !NILP (value))
2911 signal_error ("Invalid inverse-video face attribute value", value);
2913 old_value = LFACE_INVERSE (lface);
2914 ASET (lface, LFACE_INVERSE_INDEX, value);
2916 else if (EQ (attr, QCforeground))
2918 /* Compatibility with 20.x. */
2919 if (NILP (value))
2920 value = Qunspecified;
2921 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2923 /* Don't check for valid color names here because it depends
2924 on the frame (display) whether the color will be valid
2925 when the face is realized. */
2926 CHECK_STRING (value);
2927 if (SCHARS (value) == 0)
2928 signal_error ("Empty foreground color value", value);
2930 old_value = LFACE_FOREGROUND (lface);
2931 ASET (lface, LFACE_FOREGROUND_INDEX, value);
2933 else if (EQ (attr, QCdistant_foreground))
2935 /* Compatibility with 20.x. */
2936 if (NILP (value))
2937 value = Qunspecified;
2938 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2940 /* Don't check for valid color names here because it depends
2941 on the frame (display) whether the color will be valid
2942 when the face is realized. */
2943 CHECK_STRING (value);
2944 if (SCHARS (value) == 0)
2945 signal_error ("Empty distant-foreground color value", value);
2947 old_value = LFACE_DISTANT_FOREGROUND (lface);
2948 ASET (lface, LFACE_DISTANT_FOREGROUND_INDEX, value);
2950 else if (EQ (attr, QCbackground))
2952 /* Compatibility with 20.x. */
2953 if (NILP (value))
2954 value = Qunspecified;
2955 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2957 /* Don't check for valid color names here because it depends
2958 on the frame (display) whether the color will be valid
2959 when the face is realized. */
2960 CHECK_STRING (value);
2961 if (SCHARS (value) == 0)
2962 signal_error ("Empty background color value", value);
2964 old_value = LFACE_BACKGROUND (lface);
2965 ASET (lface, LFACE_BACKGROUND_INDEX, value);
2967 else if (EQ (attr, QCstipple))
2969 #if defined (HAVE_WINDOW_SYSTEM)
2970 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
2971 && !NILP (value)
2972 && NILP (Fbitmap_spec_p (value)))
2973 signal_error ("Invalid stipple attribute", value);
2974 old_value = LFACE_STIPPLE (lface);
2975 ASET (lface, LFACE_STIPPLE_INDEX, value);
2976 #endif /* HAVE_WINDOW_SYSTEM */
2978 else if (EQ (attr, QCwidth))
2980 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2982 CHECK_SYMBOL (value);
2983 if (FONT_WIDTH_NAME_NUMERIC (value) < 0)
2984 signal_error ("Invalid face width", value);
2986 old_value = LFACE_SWIDTH (lface);
2987 ASET (lface, LFACE_SWIDTH_INDEX, value);
2988 prop_index = FONT_WIDTH_INDEX;
2990 else if (EQ (attr, QCfont))
2992 #ifdef HAVE_WINDOW_SYSTEM
2993 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
2995 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2997 struct frame *f;
2999 old_value = LFACE_FONT (lface);
3000 if (! FONTP (value))
3002 if (STRINGP (value))
3004 Lisp_Object name = value;
3005 int fontset = fs_query_fontset (name, 0);
3007 if (fontset >= 0)
3008 name = fontset_ascii (fontset);
3009 value = font_spec_from_name (name);
3010 if (!FONTP (value))
3011 signal_error ("Invalid font name", name);
3013 else
3014 signal_error ("Invalid font or font-spec", value);
3016 if (EQ (frame, Qt))
3017 f = XFRAME (selected_frame);
3018 else
3019 f = XFRAME (frame);
3021 /* FIXME:
3022 If frame is t, and selected frame is a tty frame, the font
3023 can't be realized. An improvement would be to loop over frames
3024 for a non-tty frame and use that. See discussion in Bug#18573.
3025 For a daemon, frame may be an initial frame (Bug#18869). */
3026 if (FRAME_WINDOW_P (f))
3028 if (! FONT_OBJECT_P (value))
3030 Lisp_Object *attrs = XVECTOR (lface)->contents;
3031 Lisp_Object font_object;
3033 font_object = font_load_for_lface (f, attrs, value);
3034 if (NILP (font_object))
3035 signal_error ("Font not available", value);
3036 value = font_object;
3038 set_lface_from_font (f, lface, value, true);
3041 else
3042 ASET (lface, LFACE_FONT_INDEX, value);
3044 #endif /* HAVE_WINDOW_SYSTEM */
3046 else if (EQ (attr, QCfontset))
3048 #ifdef HAVE_WINDOW_SYSTEM
3049 if (EQ (frame, Qt) || FRAME_WINDOW_P (XFRAME (frame)))
3051 Lisp_Object tmp;
3053 old_value = LFACE_FONTSET (lface);
3054 tmp = Fquery_fontset (value, Qnil);
3055 if (NILP (tmp))
3056 signal_error ("Invalid fontset name", value);
3057 ASET (lface, LFACE_FONTSET_INDEX, value = tmp);
3059 #endif /* HAVE_WINDOW_SYSTEM */
3061 else if (EQ (attr, QCinherit))
3063 Lisp_Object tail;
3064 if (SYMBOLP (value))
3065 tail = Qnil;
3066 else
3067 for (tail = value; CONSP (tail); tail = XCDR (tail))
3068 if (!SYMBOLP (XCAR (tail)))
3069 break;
3070 if (NILP (tail))
3071 ASET (lface, LFACE_INHERIT_INDEX, value);
3072 else
3073 signal_error ("Invalid face inheritance", value);
3075 else if (EQ (attr, QCbold))
3077 old_value = LFACE_WEIGHT (lface);
3078 ASET (lface, LFACE_WEIGHT_INDEX, NILP (value) ? Qnormal : Qbold);
3079 prop_index = FONT_WEIGHT_INDEX;
3081 else if (EQ (attr, QCitalic))
3083 attr = QCslant;
3084 old_value = LFACE_SLANT (lface);
3085 ASET (lface, LFACE_SLANT_INDEX, NILP (value) ? Qnormal : Qitalic);
3086 prop_index = FONT_SLANT_INDEX;
3088 else
3089 signal_error ("Invalid face attribute name", attr);
3091 if (prop_index)
3093 /* If a font-related attribute other than QCfont and QCfontset
3094 is specified, and if the original QCfont attribute has a font
3095 (font-spec or font-object), set the corresponding property in
3096 the font to nil so that the font selector doesn't think that
3097 the attribute is mandatory. Also, clear the average
3098 width. */
3099 font_clear_prop (XVECTOR (lface)->contents, prop_index);
3102 /* Changing a named face means that all realized faces depending on
3103 that face are invalid. Since we cannot tell which realized faces
3104 depend on the face, make sure they are all removed. This is done
3105 by setting face_change. The next call to init_iterator will then
3106 free realized faces. */
3107 if (!EQ (frame, Qt)
3108 && NILP (Fget (face, Qface_no_inherit))
3109 && NILP (Fequal (old_value, value)))
3111 face_change = true;
3112 windows_or_buffers_changed = 56;
3115 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3116 && NILP (Fequal (old_value, value)))
3118 Lisp_Object param;
3120 param = Qnil;
3122 if (EQ (face, Qdefault))
3124 #ifdef HAVE_WINDOW_SYSTEM
3125 /* Changed font-related attributes of the `default' face are
3126 reflected in changed `font' frame parameters. */
3127 if (FRAMEP (frame)
3128 && (prop_index || EQ (attr, QCfont))
3129 && lface_fully_specified_p (XVECTOR (lface)->contents))
3130 set_font_frame_param (frame, lface);
3131 else
3132 #endif /* HAVE_WINDOW_SYSTEM */
3134 if (EQ (attr, QCforeground))
3135 param = Qforeground_color;
3136 else if (EQ (attr, QCbackground))
3137 param = Qbackground_color;
3139 #ifdef HAVE_WINDOW_SYSTEM
3140 #ifndef HAVE_NTGUI
3141 else if (EQ (face, Qscroll_bar))
3143 /* Changing the colors of `scroll-bar' sets frame parameters
3144 `scroll-bar-foreground' and `scroll-bar-background'. */
3145 if (EQ (attr, QCforeground))
3146 param = Qscroll_bar_foreground;
3147 else if (EQ (attr, QCbackground))
3148 param = Qscroll_bar_background;
3150 #endif /* not HAVE_NTGUI */
3151 else if (EQ (face, Qborder))
3153 /* Changing background color of `border' sets frame parameter
3154 `border-color'. */
3155 if (EQ (attr, QCbackground))
3156 param = Qborder_color;
3158 else if (EQ (face, Qcursor))
3160 /* Changing background color of `cursor' sets frame parameter
3161 `cursor-color'. */
3162 if (EQ (attr, QCbackground))
3163 param = Qcursor_color;
3165 else if (EQ (face, Qmouse))
3167 /* Changing background color of `mouse' sets frame parameter
3168 `mouse-color'. */
3169 if (EQ (attr, QCbackground))
3170 param = Qmouse_color;
3172 #endif /* HAVE_WINDOW_SYSTEM */
3173 else if (EQ (face, Qmenu))
3175 /* Indicate that we have to update the menu bar when realizing
3176 faces on FRAME. FRAME t change the default for new frames.
3177 We do this by setting the flag in new face caches. */
3178 if (FRAMEP (frame))
3180 struct frame *f = XFRAME (frame);
3181 if (FRAME_FACE_CACHE (f) == NULL)
3182 FRAME_FACE_CACHE (f) = make_face_cache (f);
3183 FRAME_FACE_CACHE (f)->menu_face_changed_p = true;
3185 else
3186 menu_face_changed_default = true;
3189 if (!NILP (param))
3191 if (EQ (frame, Qt))
3192 /* Update `default-frame-alist', which is used for new frames. */
3194 store_in_alist (&Vdefault_frame_alist, param, value);
3196 else
3197 /* Update the current frame's parameters. */
3199 Lisp_Object cons;
3200 cons = XCAR (Vparam_value_alist);
3201 XSETCAR (cons, param);
3202 XSETCDR (cons, value);
3203 Fmodify_frame_parameters (frame, Vparam_value_alist);
3208 return face;
3212 /* Update the corresponding face when frame parameter PARAM on frame F
3213 has been assigned the value NEW_VALUE. */
3215 void
3216 update_face_from_frame_parameter (struct frame *f, Lisp_Object param,
3217 Lisp_Object new_value)
3219 Lisp_Object face = Qnil;
3220 Lisp_Object lface;
3222 /* If there are no faces yet, give up. This is the case when called
3223 from Fx_create_frame, and we do the necessary things later in
3224 face-set-after-frame-defaults. */
3225 if (NILP (f->face_alist))
3226 return;
3228 if (EQ (param, Qforeground_color))
3230 face = Qdefault;
3231 lface = lface_from_face_name (f, face, true);
3232 ASET (lface, LFACE_FOREGROUND_INDEX,
3233 (STRINGP (new_value) ? new_value : Qunspecified));
3234 realize_basic_faces (f);
3236 else if (EQ (param, Qbackground_color))
3238 Lisp_Object frame;
3240 /* Changing the background color might change the background
3241 mode, so that we have to load new defface specs.
3242 Call frame-set-background-mode to do that. */
3243 XSETFRAME (frame, f);
3244 call1 (Qframe_set_background_mode, frame);
3246 face = Qdefault;
3247 lface = lface_from_face_name (f, face, true);
3248 ASET (lface, LFACE_BACKGROUND_INDEX,
3249 (STRINGP (new_value) ? new_value : Qunspecified));
3250 realize_basic_faces (f);
3252 #ifdef HAVE_WINDOW_SYSTEM
3253 else if (EQ (param, Qborder_color))
3255 face = Qborder;
3256 lface = lface_from_face_name (f, face, true);
3257 ASET (lface, LFACE_BACKGROUND_INDEX,
3258 (STRINGP (new_value) ? new_value : Qunspecified));
3260 else if (EQ (param, Qcursor_color))
3262 face = Qcursor;
3263 lface = lface_from_face_name (f, face, true);
3264 ASET (lface, LFACE_BACKGROUND_INDEX,
3265 (STRINGP (new_value) ? new_value : Qunspecified));
3267 else if (EQ (param, Qmouse_color))
3269 face = Qmouse;
3270 lface = lface_from_face_name (f, face, true);
3271 ASET (lface, LFACE_BACKGROUND_INDEX,
3272 (STRINGP (new_value) ? new_value : Qunspecified));
3274 #endif
3276 /* Changing a named face means that all realized faces depending on
3277 that face are invalid. Since we cannot tell which realized faces
3278 depend on the face, make sure they are all removed. This is done
3279 by setting face_change. The next call to init_iterator will then
3280 free realized faces. */
3281 if (!NILP (face)
3282 && NILP (Fget (face, Qface_no_inherit)))
3284 face_change = true;
3285 windows_or_buffers_changed = 57;
3290 #ifdef HAVE_WINDOW_SYSTEM
3292 /* Set the `font' frame parameter of FRAME determined from the
3293 font-object set in `default' face attributes LFACE. */
3295 static void
3296 set_font_frame_param (Lisp_Object frame, Lisp_Object lface)
3298 struct frame *f = XFRAME (frame);
3299 Lisp_Object font;
3301 if (FRAME_WINDOW_P (f)
3302 /* Don't do anything if the font is `unspecified'. This can
3303 happen during frame creation. */
3304 && (font = LFACE_FONT (lface),
3305 ! UNSPECIFIEDP (font)))
3307 if (FONT_SPEC_P (font))
3309 font = font_load_for_lface (f, XVECTOR (lface)->contents, font);
3310 if (NILP (font))
3311 return;
3312 ASET (lface, LFACE_FONT_INDEX, font);
3314 f->default_face_done_p = false;
3315 AUTO_FRAME_ARG (arg, Qfont, font);
3316 Fmodify_frame_parameters (frame, arg);
3320 DEFUN ("internal-face-x-get-resource", Finternal_face_x_get_resource,
3321 Sinternal_face_x_get_resource, 2, 3, 0,
3322 doc: /* Get the value of X resource RESOURCE, class CLASS.
3323 Returned value is for the display of frame FRAME. If FRAME is not
3324 specified or nil, use selected frame. This function exists because
3325 ordinary `x-get-resource' doesn't take a frame argument. */)
3326 (Lisp_Object resource, Lisp_Object class, Lisp_Object frame)
3328 Lisp_Object value = Qnil;
3329 struct frame *f;
3331 CHECK_STRING (resource);
3332 CHECK_STRING (class);
3333 f = decode_live_frame (frame);
3334 block_input ();
3335 value = display_x_get_resource (FRAME_DISPLAY_INFO (f),
3336 resource, class, Qnil, Qnil);
3337 unblock_input ();
3338 return value;
3342 /* Return resource string VALUE as a boolean value, i.e. nil, or t.
3343 If VALUE is "on" or "true", return t. If VALUE is "off" or
3344 "false", return nil. Otherwise, if SIGNAL_P, signal an
3345 error; if !SIGNAL_P, return 0. */
3347 static Lisp_Object
3348 face_boolean_x_resource_value (Lisp_Object value, bool signal_p)
3350 Lisp_Object result = make_number (0);
3352 eassert (STRINGP (value));
3354 if (xstrcasecmp (SSDATA (value), "on") == 0
3355 || xstrcasecmp (SSDATA (value), "true") == 0)
3356 result = Qt;
3357 else if (xstrcasecmp (SSDATA (value), "off") == 0
3358 || xstrcasecmp (SSDATA (value), "false") == 0)
3359 result = Qnil;
3360 else if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3361 result = Qunspecified;
3362 else if (signal_p)
3363 signal_error ("Invalid face attribute value from X resource", value);
3365 return result;
3369 DEFUN ("internal-set-lisp-face-attribute-from-resource",
3370 Finternal_set_lisp_face_attribute_from_resource,
3371 Sinternal_set_lisp_face_attribute_from_resource,
3372 3, 4, 0, doc: /* */)
3373 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
3375 CHECK_SYMBOL (face);
3376 CHECK_SYMBOL (attr);
3377 CHECK_STRING (value);
3379 if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3380 value = Qunspecified;
3381 else if (EQ (attr, QCheight))
3383 value = Fstring_to_number (value, make_number (10));
3384 if (XINT (value) <= 0)
3385 signal_error ("Invalid face height from X resource", value);
3387 else if (EQ (attr, QCbold) || EQ (attr, QCitalic))
3388 value = face_boolean_x_resource_value (value, true);
3389 else if (EQ (attr, QCweight) || EQ (attr, QCslant) || EQ (attr, QCwidth))
3390 value = intern (SSDATA (value));
3391 else if (EQ (attr, QCreverse_video) || EQ (attr, QCinverse_video))
3392 value = face_boolean_x_resource_value (value, true);
3393 else if (EQ (attr, QCunderline)
3394 || EQ (attr, QCoverline)
3395 || EQ (attr, QCstrike_through))
3397 Lisp_Object boolean_value;
3399 /* If the result of face_boolean_x_resource_value is t or nil,
3400 VALUE does NOT specify a color. */
3401 boolean_value = face_boolean_x_resource_value (value, false);
3402 if (SYMBOLP (boolean_value))
3403 value = boolean_value;
3405 else if (EQ (attr, QCbox) || EQ (attr, QCinherit))
3406 value = Fcar (Fread_from_string (value, Qnil, Qnil));
3408 return Finternal_set_lisp_face_attribute (face, attr, value, frame);
3411 #endif /* HAVE_WINDOW_SYSTEM */
3414 /***********************************************************************
3415 Menu face
3416 ***********************************************************************/
3418 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
3420 /* Make menus on frame F appear as specified by the `menu' face. */
3422 static void
3423 x_update_menu_appearance (struct frame *f)
3425 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
3426 XrmDatabase rdb;
3428 if (dpyinfo
3429 && (rdb = XrmGetDatabase (FRAME_X_DISPLAY (f)),
3430 rdb != NULL))
3432 char line[512];
3433 char *buf = line;
3434 ptrdiff_t bufsize = sizeof line;
3435 Lisp_Object lface = lface_from_face_name (f, Qmenu, true);
3436 struct face *face = FACE_FROM_ID (f, MENU_FACE_ID);
3437 const char *myname = SSDATA (Vx_resource_name);
3438 bool changed_p = false;
3439 #ifdef USE_MOTIF
3440 const char *popup_path = "popup_menu";
3441 #else
3442 const char *popup_path = "menu.popup";
3443 #endif
3445 if (STRINGP (LFACE_FOREGROUND (lface)))
3447 exprintf (&buf, &bufsize, line, -1, "%s.%s*foreground: %s",
3448 myname, popup_path,
3449 SDATA (LFACE_FOREGROUND (lface)));
3450 XrmPutLineResource (&rdb, line);
3451 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*foreground: %s",
3452 myname, SDATA (LFACE_FOREGROUND (lface)));
3453 XrmPutLineResource (&rdb, line);
3454 changed_p = true;
3457 if (STRINGP (LFACE_BACKGROUND (lface)))
3459 exprintf (&buf, &bufsize, line, -1, "%s.%s*background: %s",
3460 myname, popup_path,
3461 SDATA (LFACE_BACKGROUND (lface)));
3462 XrmPutLineResource (&rdb, line);
3464 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*background: %s",
3465 myname, SDATA (LFACE_BACKGROUND (lface)));
3466 XrmPutLineResource (&rdb, line);
3467 changed_p = true;
3470 if (face->font
3471 /* On Solaris 5.8, it's been reported that the `menu' face
3472 can be unspecified here, during startup. Why this
3473 happens remains unknown. -- cyd */
3474 && FONTP (LFACE_FONT (lface))
3475 && (!UNSPECIFIEDP (LFACE_FAMILY (lface))
3476 || !UNSPECIFIEDP (LFACE_FOUNDRY (lface))
3477 || !UNSPECIFIEDP (LFACE_SWIDTH (lface))
3478 || !UNSPECIFIEDP (LFACE_WEIGHT (lface))
3479 || !UNSPECIFIEDP (LFACE_SLANT (lface))
3480 || !UNSPECIFIEDP (LFACE_HEIGHT (lface))))
3482 Lisp_Object xlfd = Ffont_xlfd_name (LFACE_FONT (lface), Qnil);
3483 #ifdef USE_MOTIF
3484 const char *suffix = "List";
3485 bool motif = true;
3486 #else
3487 #if defined HAVE_X_I18N
3489 const char *suffix = "Set";
3490 #else
3491 const char *suffix = "";
3492 #endif
3493 bool motif = false;
3494 #endif
3496 if (! NILP (xlfd))
3498 #if defined HAVE_X_I18N
3499 char *fontsetname = xic_create_fontsetname (SSDATA (xlfd), motif);
3500 #else
3501 char *fontsetname = SSDATA (xlfd);
3502 #endif
3503 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*font%s: %s",
3504 myname, suffix, fontsetname);
3505 XrmPutLineResource (&rdb, line);
3507 exprintf (&buf, &bufsize, line, -1, "%s.%s*font%s: %s",
3508 myname, popup_path, suffix, fontsetname);
3509 XrmPutLineResource (&rdb, line);
3510 changed_p = true;
3511 if (fontsetname != SSDATA (xlfd))
3512 xfree (fontsetname);
3516 if (changed_p && f->output_data.x->menubar_widget)
3517 free_frame_menubar (f);
3519 if (buf != line)
3520 xfree (buf);
3524 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
3527 DEFUN ("face-attribute-relative-p", Fface_attribute_relative_p,
3528 Sface_attribute_relative_p,
3529 2, 2, 0,
3530 doc: /* Check whether a face attribute value is relative.
3531 Specifically, this function returns t if the attribute ATTRIBUTE
3532 with the value VALUE is relative.
3534 A relative value is one that doesn't entirely override whatever is
3535 inherited from another face. For most possible attributes,
3536 the only relative value that users see is `unspecified'.
3537 However, for :height, floating point values are also relative. */
3538 attributes: const)
3539 (Lisp_Object attribute, Lisp_Object value)
3541 if (EQ (value, Qunspecified) || (EQ (value, QCignore_defface)))
3542 return Qt;
3543 else if (EQ (attribute, QCheight))
3544 return INTEGERP (value) ? Qnil : Qt;
3545 else
3546 return Qnil;
3549 DEFUN ("merge-face-attribute", Fmerge_face_attribute, Smerge_face_attribute,
3550 3, 3, 0,
3551 doc: /* Return face ATTRIBUTE VALUE1 merged with VALUE2.
3552 If VALUE1 or VALUE2 are absolute (see `face-attribute-relative-p'), then
3553 the result will be absolute, otherwise it will be relative. */)
3554 (Lisp_Object attribute, Lisp_Object value1, Lisp_Object value2)
3556 if (EQ (value1, Qunspecified) || EQ (value1, QCignore_defface))
3557 return value2;
3558 else if (EQ (attribute, QCheight))
3559 return merge_face_heights (value1, value2, value1);
3560 else
3561 return value1;
3565 DEFUN ("internal-get-lisp-face-attribute", Finternal_get_lisp_face_attribute,
3566 Sinternal_get_lisp_face_attribute,
3567 2, 3, 0,
3568 doc: /* Return face attribute KEYWORD of face SYMBOL.
3569 If SYMBOL does not name a valid Lisp face or KEYWORD isn't a valid
3570 face attribute name, signal an error.
3571 If the optional argument FRAME is given, report on face SYMBOL in that
3572 frame. If FRAME is t, report on the defaults for face SYMBOL (for new
3573 frames). If FRAME is omitted or nil, use the selected frame. */)
3574 (Lisp_Object symbol, Lisp_Object keyword, Lisp_Object frame)
3576 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3577 Lisp_Object lface = lface_from_face_name (f, symbol, true), value = Qnil;
3579 CHECK_SYMBOL (symbol);
3580 CHECK_SYMBOL (keyword);
3582 if (EQ (keyword, QCfamily))
3583 value = LFACE_FAMILY (lface);
3584 else if (EQ (keyword, QCfoundry))
3585 value = LFACE_FOUNDRY (lface);
3586 else if (EQ (keyword, QCheight))
3587 value = LFACE_HEIGHT (lface);
3588 else if (EQ (keyword, QCweight))
3589 value = LFACE_WEIGHT (lface);
3590 else if (EQ (keyword, QCslant))
3591 value = LFACE_SLANT (lface);
3592 else if (EQ (keyword, QCunderline))
3593 value = LFACE_UNDERLINE (lface);
3594 else if (EQ (keyword, QCoverline))
3595 value = LFACE_OVERLINE (lface);
3596 else if (EQ (keyword, QCstrike_through))
3597 value = LFACE_STRIKE_THROUGH (lface);
3598 else if (EQ (keyword, QCbox))
3599 value = LFACE_BOX (lface);
3600 else if (EQ (keyword, QCinverse_video)
3601 || EQ (keyword, QCreverse_video))
3602 value = LFACE_INVERSE (lface);
3603 else if (EQ (keyword, QCforeground))
3604 value = LFACE_FOREGROUND (lface);
3605 else if (EQ (keyword, QCdistant_foreground))
3606 value = LFACE_DISTANT_FOREGROUND (lface);
3607 else if (EQ (keyword, QCbackground))
3608 value = LFACE_BACKGROUND (lface);
3609 else if (EQ (keyword, QCstipple))
3610 value = LFACE_STIPPLE (lface);
3611 else if (EQ (keyword, QCwidth))
3612 value = LFACE_SWIDTH (lface);
3613 else if (EQ (keyword, QCinherit))
3614 value = LFACE_INHERIT (lface);
3615 else if (EQ (keyword, QCfont))
3616 value = LFACE_FONT (lface);
3617 else if (EQ (keyword, QCfontset))
3618 value = LFACE_FONTSET (lface);
3619 else
3620 signal_error ("Invalid face attribute name", keyword);
3622 if (IGNORE_DEFFACE_P (value))
3623 return Qunspecified;
3625 return value;
3629 DEFUN ("internal-lisp-face-attribute-values",
3630 Finternal_lisp_face_attribute_values,
3631 Sinternal_lisp_face_attribute_values, 1, 1, 0,
3632 doc: /* Return a list of valid discrete values for face attribute ATTR.
3633 Value is nil if ATTR doesn't have a discrete set of valid values. */)
3634 (Lisp_Object attr)
3636 Lisp_Object result = Qnil;
3638 CHECK_SYMBOL (attr);
3640 if (EQ (attr, QCunderline) || EQ (attr, QCoverline)
3641 || EQ (attr, QCstrike_through)
3642 || EQ (attr, QCinverse_video) || EQ (attr, QCreverse_video))
3643 result = list2 (Qt, Qnil);
3645 return result;
3649 DEFUN ("internal-merge-in-global-face", Finternal_merge_in_global_face,
3650 Sinternal_merge_in_global_face, 2, 2, 0,
3651 doc: /* Add attributes from frame-default definition of FACE to FACE on FRAME.
3652 Default face attributes override any local face attributes. */)
3653 (Lisp_Object face, Lisp_Object frame)
3655 int i;
3656 Lisp_Object global_lface, local_lface, *gvec, *lvec;
3657 struct frame *f = XFRAME (frame);
3659 CHECK_LIVE_FRAME (frame);
3660 global_lface = lface_from_face_name (NULL, face, true);
3661 local_lface = lface_from_face_name (f, face, false);
3662 if (NILP (local_lface))
3663 local_lface = Finternal_make_lisp_face (face, frame);
3665 /* Make every specified global attribute override the local one.
3666 BEWARE!! This is only used from `face-set-after-frame-default' where
3667 the local frame is defined from default specs in `face-defface-spec'
3668 and those should be overridden by global settings. Hence the strange
3669 "global before local" priority. */
3670 lvec = XVECTOR (local_lface)->contents;
3671 gvec = XVECTOR (global_lface)->contents;
3672 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3673 if (IGNORE_DEFFACE_P (gvec[i]))
3674 ASET (local_lface, i, Qunspecified);
3675 else if (! UNSPECIFIEDP (gvec[i]))
3676 ASET (local_lface, i, AREF (global_lface, i));
3678 /* If the default face was changed, update the face cache and the
3679 `font' frame parameter. */
3680 if (EQ (face, Qdefault))
3682 struct face_cache *c = FRAME_FACE_CACHE (f);
3683 struct face *newface, *oldface = FACE_FROM_ID (f, DEFAULT_FACE_ID);
3684 Lisp_Object attrs[LFACE_VECTOR_SIZE];
3686 /* This can be NULL (e.g., in batch mode). */
3687 if (oldface)
3689 /* Ensure that the face vector is fully specified by merging
3690 the previously-cached vector. */
3691 memcpy (attrs, oldface->lface, sizeof attrs);
3692 merge_face_vectors (f, lvec, attrs, 0);
3693 vcopy (local_lface, 0, attrs, LFACE_VECTOR_SIZE);
3694 newface = realize_face (c, lvec, DEFAULT_FACE_ID);
3696 if ((! UNSPECIFIEDP (gvec[LFACE_FAMILY_INDEX])
3697 || ! UNSPECIFIEDP (gvec[LFACE_FOUNDRY_INDEX])
3698 || ! UNSPECIFIEDP (gvec[LFACE_HEIGHT_INDEX])
3699 || ! UNSPECIFIEDP (gvec[LFACE_WEIGHT_INDEX])
3700 || ! UNSPECIFIEDP (gvec[LFACE_SLANT_INDEX])
3701 || ! UNSPECIFIEDP (gvec[LFACE_SWIDTH_INDEX])
3702 || ! UNSPECIFIEDP (gvec[LFACE_FONT_INDEX]))
3703 && newface->font)
3705 Lisp_Object name = newface->font->props[FONT_NAME_INDEX];
3706 AUTO_FRAME_ARG (arg, Qfont, name);
3707 Fmodify_frame_parameters (frame, arg);
3710 if (STRINGP (gvec[LFACE_FOREGROUND_INDEX]))
3712 AUTO_FRAME_ARG (arg, Qforeground_color,
3713 gvec[LFACE_FOREGROUND_INDEX]);
3714 Fmodify_frame_parameters (frame, arg);
3717 if (STRINGP (gvec[LFACE_BACKGROUND_INDEX]))
3719 AUTO_FRAME_ARG (arg, Qbackground_color,
3720 gvec[LFACE_BACKGROUND_INDEX]);
3721 Fmodify_frame_parameters (frame, arg);
3726 return Qnil;
3730 /* The following function is implemented for compatibility with 20.2.
3731 The function is used in x-resolve-fonts when it is asked to
3732 return fonts with the same size as the font of a face. This is
3733 done in fontset.el. */
3735 DEFUN ("face-font", Fface_font, Sface_font, 1, 3, 0,
3736 doc: /* Return the font name of face FACE, or nil if it is unspecified.
3737 The font name is, by default, for ASCII characters.
3738 If the optional argument FRAME is given, report on face FACE in that frame.
3739 If FRAME is t, report on the defaults for face FACE (for new frames).
3740 The font default for a face is either nil, or a list
3741 of the form (bold), (italic) or (bold italic).
3742 If FRAME is omitted or nil, use the selected frame. And, in this case,
3743 if the optional third argument CHARACTER is given,
3744 return the font name used for CHARACTER. */)
3745 (Lisp_Object face, Lisp_Object frame, Lisp_Object character)
3747 if (EQ (frame, Qt))
3749 Lisp_Object result = Qnil;
3750 Lisp_Object lface = lface_from_face_name (NULL, face, true);
3752 if (!UNSPECIFIEDP (LFACE_WEIGHT (lface))
3753 && !EQ (LFACE_WEIGHT (lface), Qnormal))
3754 result = Fcons (Qbold, result);
3756 if (!UNSPECIFIEDP (LFACE_SLANT (lface))
3757 && !EQ (LFACE_SLANT (lface), Qnormal))
3758 result = Fcons (Qitalic, result);
3760 return result;
3762 else
3764 struct frame *f = decode_live_frame (frame);
3765 int face_id = lookup_named_face (f, face, true);
3766 struct face *fface = FACE_FROM_ID (f, face_id);
3768 if (! fface)
3769 return Qnil;
3770 #ifdef HAVE_WINDOW_SYSTEM
3771 if (FRAME_WINDOW_P (f) && !NILP (character))
3773 CHECK_CHARACTER (character);
3774 face_id = FACE_FOR_CHAR (f, fface, XINT (character), -1, Qnil);
3775 fface = FACE_FROM_ID (f, face_id);
3777 return (fface->font
3778 ? fface->font->props[FONT_NAME_INDEX]
3779 : Qnil);
3780 #else /* !HAVE_WINDOW_SYSTEM */
3781 return build_string (FRAME_MSDOS_P (f)
3782 ? "ms-dos"
3783 : FRAME_W32_P (f) ? "w32term"
3784 :"tty");
3785 #endif
3790 /* Compare face-attribute values v1 and v2 for equality. Value is true if
3791 all attributes are `equal'. Tries to be fast because this function
3792 is called quite often. */
3794 static bool
3795 face_attr_equal_p (Lisp_Object v1, Lisp_Object v2)
3797 /* Type can differ, e.g. when one attribute is unspecified, i.e. nil,
3798 and the other is specified. */
3799 if (XTYPE (v1) != XTYPE (v2))
3800 return false;
3802 if (EQ (v1, v2))
3803 return true;
3805 switch (XTYPE (v1))
3807 case Lisp_String:
3808 if (SBYTES (v1) != SBYTES (v2))
3809 return false;
3811 return memcmp (SDATA (v1), SDATA (v2), SBYTES (v1)) == 0;
3813 case_Lisp_Int:
3814 case Lisp_Symbol:
3815 return false;
3817 default:
3818 return !NILP (Fequal (v1, v2));
3823 /* Compare face vectors V1 and V2 for equality. Value is true if
3824 all attributes are `equal'. Tries to be fast because this function
3825 is called quite often. */
3827 static bool
3828 lface_equal_p (Lisp_Object *v1, Lisp_Object *v2)
3830 int i;
3831 bool equal_p = true;
3833 for (i = 1; i < LFACE_VECTOR_SIZE && equal_p; ++i)
3834 equal_p = face_attr_equal_p (v1[i], v2[i]);
3836 return equal_p;
3840 DEFUN ("internal-lisp-face-equal-p", Finternal_lisp_face_equal_p,
3841 Sinternal_lisp_face_equal_p, 2, 3, 0,
3842 doc: /* True if FACE1 and FACE2 are equal.
3843 If the optional argument FRAME is given, report on FACE1 and FACE2 in that frame.
3844 If FRAME is t, report on the defaults for FACE1 and FACE2 (for new frames).
3845 If FRAME is omitted or nil, use the selected frame. */)
3846 (Lisp_Object face1, Lisp_Object face2, Lisp_Object frame)
3848 bool equal_p;
3849 struct frame *f;
3850 Lisp_Object lface1, lface2;
3852 /* Don't use decode_window_system_frame here because this function
3853 is called before X frames exist. At that time, if FRAME is nil,
3854 selected_frame will be used which is the frame dumped with
3855 Emacs. That frame is not an X frame. */
3856 f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3858 lface1 = lface_from_face_name (f, face1, true);
3859 lface2 = lface_from_face_name (f, face2, true);
3860 equal_p = lface_equal_p (XVECTOR (lface1)->contents,
3861 XVECTOR (lface2)->contents);
3862 return equal_p ? Qt : Qnil;
3866 DEFUN ("internal-lisp-face-empty-p", Finternal_lisp_face_empty_p,
3867 Sinternal_lisp_face_empty_p, 1, 2, 0,
3868 doc: /* True if FACE has no attribute specified.
3869 If the optional argument FRAME is given, report on face FACE in that frame.
3870 If FRAME is t, report on the defaults for face FACE (for new frames).
3871 If FRAME is omitted or nil, use the selected frame. */)
3872 (Lisp_Object face, Lisp_Object frame)
3874 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3875 Lisp_Object lface = lface_from_face_name (f, face, true);
3876 int i;
3878 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3879 if (!UNSPECIFIEDP (AREF (lface, i)))
3880 break;
3882 return i == LFACE_VECTOR_SIZE ? Qt : Qnil;
3886 DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist,
3887 0, 1, 0,
3888 doc: /* Return an alist of frame-local faces defined on FRAME.
3889 For internal use only. */)
3890 (Lisp_Object frame)
3892 return decode_live_frame (frame)->face_alist;
3896 /* Return a hash code for Lisp string STRING with case ignored. Used
3897 below in computing a hash value for a Lisp face. */
3899 static unsigned
3900 hash_string_case_insensitive (Lisp_Object string)
3902 const unsigned char *s;
3903 unsigned hash = 0;
3904 eassert (STRINGP (string));
3905 for (s = SDATA (string); *s; ++s)
3906 hash = (hash << 1) ^ c_tolower (*s);
3907 return hash;
3911 /* Return a hash code for face attribute vector V. */
3913 static unsigned
3914 lface_hash (Lisp_Object *v)
3916 return (hash_string_case_insensitive (v[LFACE_FAMILY_INDEX])
3917 ^ hash_string_case_insensitive (v[LFACE_FOUNDRY_INDEX])
3918 ^ hash_string_case_insensitive (v[LFACE_FOREGROUND_INDEX])
3919 ^ hash_string_case_insensitive (v[LFACE_BACKGROUND_INDEX])
3920 ^ XHASH (v[LFACE_WEIGHT_INDEX])
3921 ^ XHASH (v[LFACE_SLANT_INDEX])
3922 ^ XHASH (v[LFACE_SWIDTH_INDEX])
3923 ^ XHASH (v[LFACE_HEIGHT_INDEX]));
3926 #ifdef HAVE_WINDOW_SYSTEM
3928 /* Return true if LFACE1 and LFACE2 specify the same font (without
3929 considering charsets/registries). They do if they specify the same
3930 family, point size, weight, width, slant, and font. Both
3931 LFACE1 and LFACE2 must be fully-specified. */
3933 static bool
3934 lface_same_font_attributes_p (Lisp_Object *lface1, Lisp_Object *lface2)
3936 eassert (lface_fully_specified_p (lface1)
3937 && lface_fully_specified_p (lface2));
3938 return (xstrcasecmp (SSDATA (lface1[LFACE_FAMILY_INDEX]),
3939 SSDATA (lface2[LFACE_FAMILY_INDEX])) == 0
3940 && xstrcasecmp (SSDATA (lface1[LFACE_FOUNDRY_INDEX]),
3941 SSDATA (lface2[LFACE_FOUNDRY_INDEX])) == 0
3942 && EQ (lface1[LFACE_HEIGHT_INDEX], lface2[LFACE_HEIGHT_INDEX])
3943 && EQ (lface1[LFACE_SWIDTH_INDEX], lface2[LFACE_SWIDTH_INDEX])
3944 && EQ (lface1[LFACE_WEIGHT_INDEX], lface2[LFACE_WEIGHT_INDEX])
3945 && EQ (lface1[LFACE_SLANT_INDEX], lface2[LFACE_SLANT_INDEX])
3946 && EQ (lface1[LFACE_FONT_INDEX], lface2[LFACE_FONT_INDEX])
3947 && (EQ (lface1[LFACE_FONTSET_INDEX], lface2[LFACE_FONTSET_INDEX])
3948 || (STRINGP (lface1[LFACE_FONTSET_INDEX])
3949 && STRINGP (lface2[LFACE_FONTSET_INDEX])
3950 && ! xstrcasecmp (SSDATA (lface1[LFACE_FONTSET_INDEX]),
3951 SSDATA (lface2[LFACE_FONTSET_INDEX]))))
3955 #endif /* HAVE_WINDOW_SYSTEM */
3957 /***********************************************************************
3958 Realized Faces
3959 ***********************************************************************/
3961 /* Allocate and return a new realized face for Lisp face attribute
3962 vector ATTR. */
3964 static struct face *
3965 make_realized_face (Lisp_Object *attr)
3967 enum { off = offsetof (struct face, id) };
3968 struct face *face = xmalloc (sizeof *face);
3970 memcpy (face->lface, attr, sizeof face->lface);
3971 memset (&face->id, 0, sizeof *face - off);
3972 face->ascii_face = face;
3974 return face;
3978 /* Free realized face FACE, including its X resources. FACE may
3979 be null. */
3981 static void
3982 free_realized_face (struct frame *f, struct face *face)
3984 if (face)
3986 #ifdef HAVE_WINDOW_SYSTEM
3987 if (FRAME_WINDOW_P (f))
3989 /* Free fontset of FACE if it is ASCII face. */
3990 if (face->fontset >= 0 && face == face->ascii_face)
3991 free_face_fontset (f, face);
3992 if (face->gc)
3994 block_input ();
3995 if (face->font)
3996 font_done_for_face (f, face);
3997 x_free_gc (f, face->gc);
3998 face->gc = 0;
3999 unblock_input ();
4001 #ifdef HAVE_X_WINDOWS
4002 free_face_colors (f, face);
4003 #endif /* HAVE_X_WINDOWS */
4004 x_destroy_bitmap (f, face->stipple);
4006 #endif /* HAVE_WINDOW_SYSTEM */
4008 xfree (face);
4012 #ifdef HAVE_WINDOW_SYSTEM
4014 /* Prepare face FACE for subsequent display on frame F. This must be called
4015 before using X resources of FACE to allocate GCs if they haven't been
4016 allocated yet or have been freed by clearing the face cache. */
4018 void
4019 prepare_face_for_display (struct frame *f, struct face *face)
4021 eassert (FRAME_WINDOW_P (f));
4023 if (face->gc == 0)
4025 XGCValues xgcv;
4026 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
4028 xgcv.foreground = face->foreground;
4029 xgcv.background = face->background;
4030 #ifdef HAVE_X_WINDOWS
4031 xgcv.graphics_exposures = False;
4032 #endif
4034 block_input ();
4035 #ifdef HAVE_X_WINDOWS
4036 if (face->stipple)
4038 xgcv.fill_style = FillOpaqueStippled;
4039 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
4040 mask |= GCFillStyle | GCStipple;
4042 #endif
4043 face->gc = x_create_gc (f, mask, &xgcv);
4044 if (face->font)
4045 font_prepare_for_face (f, face);
4046 unblock_input ();
4050 #endif /* HAVE_WINDOW_SYSTEM */
4052 /* Returns the `distance' between the colors X and Y. */
4054 static int
4055 color_distance (XColor *x, XColor *y)
4057 /* This formula is from a paper titled `Colour metric' by Thiadmer Riemersma.
4058 Quoting from that paper:
4060 This formula has results that are very close to L*u*v* (with the
4061 modified lightness curve) and, more importantly, it is a more even
4062 algorithm: it does not have a range of colors where it suddenly
4063 gives far from optimal results.
4065 See <http://www.compuphase.com/cmetric.htm> for more info. */
4067 long r = (x->red - y->red) >> 8;
4068 long g = (x->green - y->green) >> 8;
4069 long b = (x->blue - y->blue) >> 8;
4070 long r_mean = (x->red + y->red) >> 9;
4072 return
4073 (((512 + r_mean) * r * r) >> 8)
4074 + 4 * g * g
4075 + (((767 - r_mean) * b * b) >> 8);
4079 DEFUN ("color-distance", Fcolor_distance, Scolor_distance, 2, 3, 0,
4080 doc: /* Return an integer distance between COLOR1 and COLOR2 on FRAME.
4081 COLOR1 and COLOR2 may be either strings containing the color name,
4082 or lists of the form (RED GREEN BLUE).
4083 If FRAME is unspecified or nil, the current frame is used. */)
4084 (Lisp_Object color1, Lisp_Object color2, Lisp_Object frame)
4086 struct frame *f = decode_live_frame (frame);
4087 XColor cdef1, cdef2;
4089 if (!(CONSP (color1) && parse_rgb_list (color1, &cdef1))
4090 && !(STRINGP (color1)
4091 && defined_color (f, SSDATA (color1), &cdef1, false)))
4092 signal_error ("Invalid color", color1);
4093 if (!(CONSP (color2) && parse_rgb_list (color2, &cdef2))
4094 && !(STRINGP (color2)
4095 && defined_color (f, SSDATA (color2), &cdef2, false)))
4096 signal_error ("Invalid color", color2);
4098 return make_number (color_distance (&cdef1, &cdef2));
4102 /***********************************************************************
4103 Face Cache
4104 ***********************************************************************/
4106 /* Return a new face cache for frame F. */
4108 static struct face_cache *
4109 make_face_cache (struct frame *f)
4111 struct face_cache *c = xmalloc (sizeof *c);
4113 c->buckets = xzalloc (FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets);
4114 c->size = 50;
4115 c->used = 0;
4116 c->faces_by_id = xmalloc (c->size * sizeof *c->faces_by_id);
4117 c->f = f;
4118 c->menu_face_changed_p = menu_face_changed_default;
4119 return c;
4122 #ifdef HAVE_WINDOW_SYSTEM
4124 /* Clear out all graphics contexts for all realized faces, except for
4125 the basic faces. This should be done from time to time just to avoid
4126 keeping too many graphics contexts that are no longer needed. */
4128 static void
4129 clear_face_gcs (struct face_cache *c)
4131 if (c && FRAME_WINDOW_P (c->f))
4133 int i;
4134 for (i = BASIC_FACE_ID_SENTINEL; i < c->used; ++i)
4136 struct face *face = c->faces_by_id[i];
4137 if (face && face->gc)
4139 block_input ();
4140 if (face->font)
4141 font_done_for_face (c->f, face);
4142 x_free_gc (c->f, face->gc);
4143 face->gc = 0;
4144 unblock_input ();
4150 #endif /* HAVE_WINDOW_SYSTEM */
4152 /* Free all realized faces in face cache C, including basic faces.
4153 C may be null. If faces are freed, make sure the frame's current
4154 matrix is marked invalid, so that a display caused by an expose
4155 event doesn't try to use faces we destroyed. */
4157 static void
4158 free_realized_faces (struct face_cache *c)
4160 if (c && c->used)
4162 int i, size;
4163 struct frame *f = c->f;
4165 /* We must block input here because we can't process X events
4166 safely while only some faces are freed, or when the frame's
4167 current matrix still references freed faces. */
4168 block_input ();
4170 for (i = 0; i < c->used; ++i)
4172 free_realized_face (f, c->faces_by_id[i]);
4173 c->faces_by_id[i] = NULL;
4176 /* Forget the escape-glyph and glyphless-char faces. */
4177 forget_escape_and_glyphless_faces ();
4178 c->used = 0;
4179 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4180 memset (c->buckets, 0, size);
4182 /* Must do a thorough redisplay the next time. Mark current
4183 matrices as invalid because they will reference faces freed
4184 above. This function is also called when a frame is
4185 destroyed. In this case, the root window of F is nil. */
4186 if (WINDOWP (f->root_window))
4188 clear_current_matrices (f);
4189 fset_redisplay (f);
4192 unblock_input ();
4197 /* Free all realized faces on FRAME or on all frames if FRAME is nil.
4198 This is done after attributes of a named face have been changed,
4199 because we can't tell which realized faces depend on that face. */
4201 void
4202 free_all_realized_faces (Lisp_Object frame)
4204 if (NILP (frame))
4206 Lisp_Object rest;
4207 FOR_EACH_FRAME (rest, frame)
4208 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4209 windows_or_buffers_changed = 58;
4211 else
4212 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4216 /* Free face cache C and faces in it, including their X resources. */
4218 static void
4219 free_face_cache (struct face_cache *c)
4221 if (c)
4223 free_realized_faces (c);
4224 xfree (c->buckets);
4225 xfree (c->faces_by_id);
4226 xfree (c);
4231 /* Cache realized face FACE in face cache C. HASH is the hash value
4232 of FACE. If FACE is for ASCII characters (i.e. FACE->ascii_face ==
4233 FACE), insert the new face to the beginning of the collision list
4234 of the face hash table of C. Otherwise, add the new face to the
4235 end of the collision list. This way, lookup_face can quickly find
4236 that a requested face is not cached. */
4238 static void
4239 cache_face (struct face_cache *c, struct face *face, unsigned int hash)
4241 int i = hash % FACE_CACHE_BUCKETS_SIZE;
4243 face->hash = hash;
4245 if (face->ascii_face != face)
4247 struct face *last = c->buckets[i];
4248 if (last)
4250 while (last->next)
4251 last = last->next;
4252 last->next = face;
4253 face->prev = last;
4254 face->next = NULL;
4256 else
4258 c->buckets[i] = face;
4259 face->prev = face->next = NULL;
4262 else
4264 face->prev = NULL;
4265 face->next = c->buckets[i];
4266 if (face->next)
4267 face->next->prev = face;
4268 c->buckets[i] = face;
4271 /* Find a free slot in C->faces_by_id and use the index of the free
4272 slot as FACE->id. */
4273 for (i = 0; i < c->used; ++i)
4274 if (c->faces_by_id[i] == NULL)
4275 break;
4276 face->id = i;
4278 #ifdef GLYPH_DEBUG
4279 /* Check that FACE got a unique id. */
4281 int j, n;
4282 struct face *face1;
4284 for (j = n = 0; j < FACE_CACHE_BUCKETS_SIZE; ++j)
4285 for (face1 = c->buckets[j]; face1; face1 = face1->next)
4286 if (face1->id == i)
4287 ++n;
4289 eassert (n == 1);
4291 #endif /* GLYPH_DEBUG */
4293 /* Maybe enlarge C->faces_by_id. */
4294 if (i == c->used)
4296 if (c->used == c->size)
4297 c->faces_by_id = xpalloc (c->faces_by_id, &c->size, 1, MAX_FACE_ID,
4298 sizeof *c->faces_by_id);
4299 c->used++;
4302 c->faces_by_id[i] = face;
4306 /* Remove face FACE from cache C. */
4308 static void
4309 uncache_face (struct face_cache *c, struct face *face)
4311 int i = face->hash % FACE_CACHE_BUCKETS_SIZE;
4313 if (face->prev)
4314 face->prev->next = face->next;
4315 else
4316 c->buckets[i] = face->next;
4318 if (face->next)
4319 face->next->prev = face->prev;
4321 c->faces_by_id[face->id] = NULL;
4322 if (face->id == c->used)
4323 --c->used;
4327 /* Look up a realized face with face attributes ATTR in the face cache
4328 of frame F. The face will be used to display ASCII characters.
4329 Value is the ID of the face found. If no suitable face is found,
4330 realize a new one. */
4332 static int
4333 lookup_face (struct frame *f, Lisp_Object *attr)
4335 struct face_cache *cache = FRAME_FACE_CACHE (f);
4336 unsigned hash;
4337 int i;
4338 struct face *face;
4340 eassert (cache != NULL);
4341 check_lface_attrs (attr);
4343 /* Look up ATTR in the face cache. */
4344 hash = lface_hash (attr);
4345 i = hash % FACE_CACHE_BUCKETS_SIZE;
4347 for (face = cache->buckets[i]; face; face = face->next)
4349 if (face->ascii_face != face)
4351 /* There's no more ASCII face. */
4352 face = NULL;
4353 break;
4355 if (face->hash == hash
4356 && lface_equal_p (face->lface, attr))
4357 break;
4360 /* If not found, realize a new face. */
4361 if (face == NULL)
4362 face = realize_face (cache, attr, -1);
4364 #ifdef GLYPH_DEBUG
4365 eassert (face == FACE_FROM_ID (f, face->id));
4366 #endif /* GLYPH_DEBUG */
4368 return face->id;
4371 #ifdef HAVE_WINDOW_SYSTEM
4372 /* Look up a realized face that has the same attributes as BASE_FACE
4373 except for the font in the face cache of frame F. If FONT-OBJECT
4374 is not nil, it is an already opened font. If FONT-OBJECT is nil,
4375 the face has no font. Value is the ID of the face found. If no
4376 suitable face is found, realize a new one. */
4379 face_for_font (struct frame *f, Lisp_Object font_object, struct face *base_face)
4381 struct face_cache *cache = FRAME_FACE_CACHE (f);
4382 unsigned hash;
4383 int i;
4384 struct face *face;
4386 eassert (cache != NULL);
4387 base_face = base_face->ascii_face;
4388 hash = lface_hash (base_face->lface);
4389 i = hash % FACE_CACHE_BUCKETS_SIZE;
4391 for (face = cache->buckets[i]; face; face = face->next)
4393 if (face->ascii_face == face)
4394 continue;
4395 if (face->ascii_face == base_face
4396 && face->font == (NILP (font_object) ? NULL
4397 : XFONT_OBJECT (font_object))
4398 && lface_equal_p (face->lface, base_face->lface))
4399 return face->id;
4402 /* If not found, realize a new face. */
4403 face = realize_non_ascii_face (f, font_object, base_face);
4404 return face->id;
4406 #endif /* HAVE_WINDOW_SYSTEM */
4408 /* Return the face id of the realized face for named face SYMBOL on
4409 frame F suitable for displaying ASCII characters. Value is -1 if
4410 the face couldn't be determined, which might happen if the default
4411 face isn't realized and cannot be realized. */
4414 lookup_named_face (struct frame *f, Lisp_Object symbol, bool signal_p)
4416 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4417 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4418 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4420 if (default_face == NULL)
4422 if (!realize_basic_faces (f))
4423 return -1;
4424 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4425 if (default_face == NULL)
4426 emacs_abort (); /* realize_basic_faces must have set it up */
4429 if (! get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4430 return -1;
4432 memcpy (attrs, default_face->lface, sizeof attrs);
4433 merge_face_vectors (f, symbol_attrs, attrs, 0);
4435 return lookup_face (f, attrs);
4439 /* Return the display face-id of the basic face whose canonical face-id
4440 is FACE_ID. The return value will usually simply be FACE_ID, unless that
4441 basic face has bee remapped via Vface_remapping_alist. This function is
4442 conservative: if something goes wrong, it will simply return FACE_ID
4443 rather than signal an error. */
4446 lookup_basic_face (struct frame *f, int face_id)
4448 Lisp_Object name, mapping;
4449 int remapped_face_id;
4451 if (NILP (Vface_remapping_alist))
4452 return face_id; /* Nothing to do. */
4454 switch (face_id)
4456 case DEFAULT_FACE_ID: name = Qdefault; break;
4457 case MODE_LINE_FACE_ID: name = Qmode_line; break;
4458 case MODE_LINE_INACTIVE_FACE_ID: name = Qmode_line_inactive; break;
4459 case HEADER_LINE_FACE_ID: name = Qheader_line; break;
4460 case TOOL_BAR_FACE_ID: name = Qtool_bar; break;
4461 case FRINGE_FACE_ID: name = Qfringe; break;
4462 case SCROLL_BAR_FACE_ID: name = Qscroll_bar; break;
4463 case BORDER_FACE_ID: name = Qborder; break;
4464 case CURSOR_FACE_ID: name = Qcursor; break;
4465 case MOUSE_FACE_ID: name = Qmouse; break;
4466 case MENU_FACE_ID: name = Qmenu; break;
4468 default:
4469 emacs_abort (); /* the caller is supposed to pass us a basic face id */
4472 /* Do a quick scan through Vface_remapping_alist, and return immediately
4473 if there is no remapping for face NAME. This is just an optimization
4474 for the very common no-remapping case. */
4475 mapping = assq_no_quit (name, Vface_remapping_alist);
4476 if (NILP (mapping))
4477 return face_id; /* Give up. */
4479 /* If there is a remapping entry, lookup the face using NAME, which will
4480 handle the remapping too. */
4481 remapped_face_id = lookup_named_face (f, name, false);
4482 if (remapped_face_id < 0)
4483 return face_id; /* Give up. */
4485 return remapped_face_id;
4489 /* Return a face for charset ASCII that is like the face with id
4490 FACE_ID on frame F, but has a font that is STEPS steps smaller.
4491 STEPS < 0 means larger. Value is the id of the face. */
4494 smaller_face (struct frame *f, int face_id, int steps)
4496 #ifdef HAVE_WINDOW_SYSTEM
4497 struct face *face;
4498 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4499 int pt, last_pt, last_height;
4500 int delta;
4501 int new_face_id;
4502 struct face *new_face;
4504 /* If not called for an X frame, just return the original face. */
4505 if (FRAME_TERMCAP_P (f))
4506 return face_id;
4508 /* Try in increments of 1/2 pt. */
4509 delta = steps < 0 ? 5 : -5;
4510 steps = eabs (steps);
4512 face = FACE_FROM_ID (f, face_id);
4513 memcpy (attrs, face->lface, sizeof attrs);
4514 pt = last_pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
4515 new_face_id = face_id;
4516 last_height = FONT_HEIGHT (face->font);
4518 while (steps
4519 && pt + delta > 0
4520 /* Give up if we cannot find a font within 10pt. */
4521 && eabs (last_pt - pt) < 100)
4523 /* Look up a face for a slightly smaller/larger font. */
4524 pt += delta;
4525 attrs[LFACE_HEIGHT_INDEX] = make_number (pt);
4526 new_face_id = lookup_face (f, attrs);
4527 new_face = FACE_FROM_ID (f, new_face_id);
4529 /* If height changes, count that as one step. */
4530 if ((delta < 0 && FONT_HEIGHT (new_face->font) < last_height)
4531 || (delta > 0 && FONT_HEIGHT (new_face->font) > last_height))
4533 --steps;
4534 last_height = FONT_HEIGHT (new_face->font);
4535 last_pt = pt;
4539 return new_face_id;
4541 #else /* not HAVE_WINDOW_SYSTEM */
4543 return face_id;
4545 #endif /* not HAVE_WINDOW_SYSTEM */
4549 /* Return a face for charset ASCII that is like the face with id
4550 FACE_ID on frame F, but has height HEIGHT. */
4553 face_with_height (struct frame *f, int face_id, int height)
4555 #ifdef HAVE_WINDOW_SYSTEM
4556 struct face *face;
4557 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4559 if (FRAME_TERMCAP_P (f)
4560 || height <= 0)
4561 return face_id;
4563 face = FACE_FROM_ID (f, face_id);
4564 memcpy (attrs, face->lface, sizeof attrs);
4565 attrs[LFACE_HEIGHT_INDEX] = make_number (height);
4566 font_clear_prop (attrs, FONT_SIZE_INDEX);
4567 face_id = lookup_face (f, attrs);
4568 #endif /* HAVE_WINDOW_SYSTEM */
4570 return face_id;
4574 /* Return the face id of the realized face for named face SYMBOL on
4575 frame F suitable for displaying ASCII characters, and use
4576 attributes of the face FACE_ID for attributes that aren't
4577 completely specified by SYMBOL. This is like lookup_named_face,
4578 except that the default attributes come from FACE_ID, not from the
4579 default face. FACE_ID is assumed to be already realized. */
4582 lookup_derived_face (struct frame *f, Lisp_Object symbol, int face_id,
4583 bool signal_p)
4585 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4586 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4587 struct face *default_face = FACE_FROM_ID (f, face_id);
4589 if (!default_face)
4590 emacs_abort ();
4592 if (!get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4593 return -1;
4595 memcpy (attrs, default_face->lface, sizeof attrs);
4596 merge_face_vectors (f, symbol_attrs, attrs, 0);
4597 return lookup_face (f, attrs);
4600 DEFUN ("face-attributes-as-vector", Fface_attributes_as_vector,
4601 Sface_attributes_as_vector, 1, 1, 0,
4602 doc: /* Return a vector of face attributes corresponding to PLIST. */)
4603 (Lisp_Object plist)
4605 Lisp_Object lface;
4606 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
4607 Qunspecified);
4608 merge_face_ref (XFRAME (selected_frame), plist, XVECTOR (lface)->contents,
4609 true, 0);
4610 return lface;
4615 /***********************************************************************
4616 Face capability testing
4617 ***********************************************************************/
4620 /* If the distance (as returned by color_distance) between two colors is
4621 less than this, then they are considered the same, for determining
4622 whether a color is supported or not. The range of values is 0-65535. */
4624 #define TTY_SAME_COLOR_THRESHOLD 10000
4626 #ifdef HAVE_WINDOW_SYSTEM
4628 /* Return true if all the face attributes in ATTRS are supported
4629 on the window-system frame F.
4631 The definition of `supported' is somewhat heuristic, but basically means
4632 that a face containing all the attributes in ATTRS, when merged with the
4633 default face for display, can be represented in a way that's
4635 \(1) different in appearance than the default face, and
4636 \(2) `close in spirit' to what the attributes specify, if not exact. */
4638 static bool
4639 x_supports_face_attributes_p (struct frame *f,
4640 Lisp_Object attrs[LFACE_VECTOR_SIZE],
4641 struct face *def_face)
4643 Lisp_Object *def_attrs = def_face->lface;
4645 /* Check that other specified attributes are different that the default
4646 face. */
4647 if ((!UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
4648 && face_attr_equal_p (attrs[LFACE_UNDERLINE_INDEX],
4649 def_attrs[LFACE_UNDERLINE_INDEX]))
4650 || (!UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
4651 && face_attr_equal_p (attrs[LFACE_INVERSE_INDEX],
4652 def_attrs[LFACE_INVERSE_INDEX]))
4653 || (!UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
4654 && face_attr_equal_p (attrs[LFACE_FOREGROUND_INDEX],
4655 def_attrs[LFACE_FOREGROUND_INDEX]))
4656 || (!UNSPECIFIEDP (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
4657 && face_attr_equal_p (attrs[LFACE_DISTANT_FOREGROUND_INDEX],
4658 def_attrs[LFACE_DISTANT_FOREGROUND_INDEX]))
4659 || (!UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
4660 && face_attr_equal_p (attrs[LFACE_BACKGROUND_INDEX],
4661 def_attrs[LFACE_BACKGROUND_INDEX]))
4662 || (!UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4663 && face_attr_equal_p (attrs[LFACE_STIPPLE_INDEX],
4664 def_attrs[LFACE_STIPPLE_INDEX]))
4665 || (!UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4666 && face_attr_equal_p (attrs[LFACE_OVERLINE_INDEX],
4667 def_attrs[LFACE_OVERLINE_INDEX]))
4668 || (!UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4669 && face_attr_equal_p (attrs[LFACE_STRIKE_THROUGH_INDEX],
4670 def_attrs[LFACE_STRIKE_THROUGH_INDEX]))
4671 || (!UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
4672 && face_attr_equal_p (attrs[LFACE_BOX_INDEX],
4673 def_attrs[LFACE_BOX_INDEX])))
4674 return false;
4676 /* Check font-related attributes, as those are the most commonly
4677 "unsupported" on a window-system (because of missing fonts). */
4678 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4679 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4680 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4681 || !UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
4682 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
4683 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX]))
4685 int face_id;
4686 struct face *face;
4687 Lisp_Object merged_attrs[LFACE_VECTOR_SIZE];
4688 int i;
4690 memcpy (merged_attrs, def_attrs, sizeof merged_attrs);
4692 merge_face_vectors (f, attrs, merged_attrs, 0);
4694 face_id = lookup_face (f, merged_attrs);
4695 face = FACE_FROM_ID (f, face_id);
4697 if (! face)
4698 error ("Cannot make face");
4700 /* If the font is the same, or no font is found, then not
4701 supported. */
4702 if (face->font == def_face->font
4703 || ! face->font)
4704 return false;
4705 for (i = FONT_TYPE_INDEX; i <= FONT_SIZE_INDEX; i++)
4706 if (! EQ (face->font->props[i], def_face->font->props[i]))
4708 Lisp_Object s1, s2;
4710 if (i < FONT_FOUNDRY_INDEX || i > FONT_REGISTRY_INDEX
4711 || face->font->driver->case_sensitive)
4712 return true;
4713 s1 = SYMBOL_NAME (face->font->props[i]);
4714 s2 = SYMBOL_NAME (def_face->font->props[i]);
4715 if (! EQ (Fcompare_strings (s1, make_number (0), Qnil,
4716 s2, make_number (0), Qnil, Qt), Qt))
4717 return true;
4719 return false;
4722 /* Everything checks out, this face is supported. */
4723 return true;
4726 #endif /* HAVE_WINDOW_SYSTEM */
4728 /* Return true if all the face attributes in ATTRS are supported
4729 on the tty frame F.
4731 The definition of `supported' is somewhat heuristic, but basically means
4732 that a face containing all the attributes in ATTRS, when merged
4733 with the default face for display, can be represented in a way that's
4735 \(1) different in appearance than the default face, and
4736 \(2) `close in spirit' to what the attributes specify, if not exact.
4738 Point (2) implies that a `:weight black' attribute will be satisfied
4739 by any terminal that can display bold, and a `:foreground "yellow"' as
4740 long as the terminal can display a yellowish color, but `:slant italic'
4741 will _not_ be satisfied by the tty display code's automatic
4742 substitution of a `dim' face for italic. */
4744 static bool
4745 tty_supports_face_attributes_p (struct frame *f,
4746 Lisp_Object attrs[LFACE_VECTOR_SIZE],
4747 struct face *def_face)
4749 int weight, slant;
4750 Lisp_Object val, fg, bg;
4751 XColor fg_tty_color, fg_std_color;
4752 XColor bg_tty_color, bg_std_color;
4753 unsigned test_caps = 0;
4754 Lisp_Object *def_attrs = def_face->lface;
4756 /* First check some easy-to-check stuff; ttys support none of the
4757 following attributes, so we can just return false if any are requested
4758 (even if `nominal' values are specified, we should still return false,
4759 as that will be the same value that the default face uses). We
4760 consider :slant unsupportable on ttys, even though the face code
4761 actually `fakes' them using a dim attribute if possible. This is
4762 because the faked result is too different from what the face
4763 specifies. */
4764 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4765 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4766 || !UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4767 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4768 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
4769 || !UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4770 || !UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4771 || !UNSPECIFIEDP (attrs[LFACE_BOX_INDEX]))
4772 return false;
4774 /* Test for terminal `capabilities' (non-color character attributes). */
4776 /* font weight (bold/dim) */
4777 val = attrs[LFACE_WEIGHT_INDEX];
4778 if (!UNSPECIFIEDP (val)
4779 && (weight = FONT_WEIGHT_NAME_NUMERIC (val), weight >= 0))
4781 int def_weight = FONT_WEIGHT_NAME_NUMERIC (def_attrs[LFACE_WEIGHT_INDEX]);
4783 if (weight > 100)
4785 if (def_weight > 100)
4786 return false; /* same as default */
4787 test_caps = TTY_CAP_BOLD;
4789 else if (weight < 100)
4791 if (def_weight < 100)
4792 return false; /* same as default */
4793 test_caps = TTY_CAP_DIM;
4795 else if (def_weight == 100)
4796 return false; /* same as default */
4799 /* font slant */
4800 val = attrs[LFACE_SLANT_INDEX];
4801 if (!UNSPECIFIEDP (val)
4802 && (slant = FONT_SLANT_NAME_NUMERIC (val), slant >= 0))
4804 int def_slant = FONT_SLANT_NAME_NUMERIC (def_attrs[LFACE_SLANT_INDEX]);
4805 if (slant == 100 || slant == def_slant)
4806 return false; /* same as default */
4807 else
4808 test_caps |= TTY_CAP_ITALIC;
4811 /* underlining */
4812 val = attrs[LFACE_UNDERLINE_INDEX];
4813 if (!UNSPECIFIEDP (val))
4815 if (STRINGP (val))
4816 return false; /* ttys can't use colored underlines */
4817 else if (EQ (CAR_SAFE (val), QCstyle) && EQ (CAR_SAFE (CDR_SAFE (val)), Qwave))
4818 return false; /* ttys can't use wave underlines */
4819 else if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
4820 return false; /* same as default */
4821 else
4822 test_caps |= TTY_CAP_UNDERLINE;
4825 /* inverse video */
4826 val = attrs[LFACE_INVERSE_INDEX];
4827 if (!UNSPECIFIEDP (val))
4829 if (face_attr_equal_p (val, def_attrs[LFACE_INVERSE_INDEX]))
4830 return false; /* same as default */
4831 else
4832 test_caps |= TTY_CAP_INVERSE;
4836 /* Color testing. */
4838 /* Check if foreground color is close enough. */
4839 fg = attrs[LFACE_FOREGROUND_INDEX];
4840 if (STRINGP (fg))
4842 Lisp_Object def_fg = def_attrs[LFACE_FOREGROUND_INDEX];
4844 if (face_attr_equal_p (fg, def_fg))
4845 return false; /* same as default */
4846 else if (! tty_lookup_color (f, fg, &fg_tty_color, &fg_std_color))
4847 return false; /* not a valid color */
4848 else if (color_distance (&fg_tty_color, &fg_std_color)
4849 > TTY_SAME_COLOR_THRESHOLD)
4850 return false; /* displayed color is too different */
4851 else
4852 /* Make sure the color is really different than the default. */
4854 XColor def_fg_color;
4855 if (tty_lookup_color (f, def_fg, &def_fg_color, 0)
4856 && (color_distance (&fg_tty_color, &def_fg_color)
4857 <= TTY_SAME_COLOR_THRESHOLD))
4858 return false;
4862 /* Check if background color is close enough. */
4863 bg = attrs[LFACE_BACKGROUND_INDEX];
4864 if (STRINGP (bg))
4866 Lisp_Object def_bg = def_attrs[LFACE_BACKGROUND_INDEX];
4868 if (face_attr_equal_p (bg, def_bg))
4869 return false; /* same as default */
4870 else if (! tty_lookup_color (f, bg, &bg_tty_color, &bg_std_color))
4871 return false; /* not a valid color */
4872 else if (color_distance (&bg_tty_color, &bg_std_color)
4873 > TTY_SAME_COLOR_THRESHOLD)
4874 return false; /* displayed color is too different */
4875 else
4876 /* Make sure the color is really different than the default. */
4878 XColor def_bg_color;
4879 if (tty_lookup_color (f, def_bg, &def_bg_color, 0)
4880 && (color_distance (&bg_tty_color, &def_bg_color)
4881 <= TTY_SAME_COLOR_THRESHOLD))
4882 return false;
4886 /* If both foreground and background are requested, see if the
4887 distance between them is OK. We just check to see if the distance
4888 between the tty's foreground and background is close enough to the
4889 distance between the standard foreground and background. */
4890 if (STRINGP (fg) && STRINGP (bg))
4892 int delta_delta
4893 = (color_distance (&fg_std_color, &bg_std_color)
4894 - color_distance (&fg_tty_color, &bg_tty_color));
4895 if (delta_delta > TTY_SAME_COLOR_THRESHOLD
4896 || delta_delta < -TTY_SAME_COLOR_THRESHOLD)
4897 return false;
4901 /* See if the capabilities we selected above are supported, with the
4902 given colors. */
4903 return tty_capable_p (FRAME_TTY (f), test_caps);
4907 DEFUN ("display-supports-face-attributes-p",
4908 Fdisplay_supports_face_attributes_p, Sdisplay_supports_face_attributes_p,
4909 1, 2, 0,
4910 doc: /* Return non-nil if all the face attributes in ATTRIBUTES are supported.
4911 The optional argument DISPLAY can be a display name, a frame, or
4912 nil (meaning the selected frame's display).
4914 The definition of `supported' is somewhat heuristic, but basically means
4915 that a face containing all the attributes in ATTRIBUTES, when merged
4916 with the default face for display, can be represented in a way that's
4918 \(1) different in appearance than the default face, and
4919 \(2) `close in spirit' to what the attributes specify, if not exact.
4921 Point (2) implies that a `:weight black' attribute will be satisfied by
4922 any display that can display bold, and a `:foreground \"yellow\"' as long
4923 as it can display a yellowish color, but `:slant italic' will _not_ be
4924 satisfied by the tty display code's automatic substitution of a `dim'
4925 face for italic. */)
4926 (Lisp_Object attributes, Lisp_Object display)
4928 bool supports = false;
4929 int i;
4930 Lisp_Object frame;
4931 struct frame *f;
4932 struct face *def_face;
4933 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4935 if (noninteractive || !initialized)
4936 /* We may not be able to access low-level face information in batch
4937 mode, or before being dumped, and this function is not going to
4938 be very useful in those cases anyway, so just give up. */
4939 return Qnil;
4941 if (NILP (display))
4942 frame = selected_frame;
4943 else if (FRAMEP (display))
4944 frame = display;
4945 else
4947 /* Find any frame on DISPLAY. */
4948 Lisp_Object tail;
4950 frame = Qnil;
4951 FOR_EACH_FRAME (tail, frame)
4952 if (!NILP (Fequal (Fcdr (Fassq (Qdisplay,
4953 XFRAME (frame)->param_alist)),
4954 display)))
4955 break;
4958 CHECK_LIVE_FRAME (frame);
4959 f = XFRAME (frame);
4961 for (i = 0; i < LFACE_VECTOR_SIZE; i++)
4962 attrs[i] = Qunspecified;
4963 merge_face_ref (f, attributes, attrs, true, 0);
4965 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4966 if (def_face == NULL)
4968 if (! realize_basic_faces (f))
4969 error ("Cannot realize default face");
4970 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4971 if (def_face == NULL)
4972 emacs_abort (); /* realize_basic_faces must have set it up */
4975 /* Dispatch to the appropriate handler. */
4976 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
4977 supports = tty_supports_face_attributes_p (f, attrs, def_face);
4978 #ifdef HAVE_WINDOW_SYSTEM
4979 else
4980 supports = x_supports_face_attributes_p (f, attrs, def_face);
4981 #endif
4983 return supports ? Qt : Qnil;
4987 /***********************************************************************
4988 Font selection
4989 ***********************************************************************/
4991 DEFUN ("internal-set-font-selection-order",
4992 Finternal_set_font_selection_order,
4993 Sinternal_set_font_selection_order, 1, 1, 0,
4994 doc: /* Set font selection order for face font selection to ORDER.
4995 ORDER must be a list of length 4 containing the symbols `:width',
4996 `:height', `:weight', and `:slant'. Face attributes appearing
4997 first in ORDER are matched first, e.g. if `:height' appears before
4998 `:weight' in ORDER, font selection first tries to find a font with
4999 a suitable height, and then tries to match the font weight.
5000 Value is ORDER. */)
5001 (Lisp_Object order)
5003 Lisp_Object list;
5004 int i;
5005 int indices[ARRAYELTS (font_sort_order)];
5007 CHECK_LIST (order);
5008 memset (indices, 0, sizeof indices);
5009 i = 0;
5011 for (list = order;
5012 CONSP (list) && i < ARRAYELTS (indices);
5013 list = XCDR (list), ++i)
5015 Lisp_Object attr = XCAR (list);
5016 int xlfd;
5018 if (EQ (attr, QCwidth))
5019 xlfd = XLFD_SWIDTH;
5020 else if (EQ (attr, QCheight))
5021 xlfd = XLFD_POINT_SIZE;
5022 else if (EQ (attr, QCweight))
5023 xlfd = XLFD_WEIGHT;
5024 else if (EQ (attr, QCslant))
5025 xlfd = XLFD_SLANT;
5026 else
5027 break;
5029 if (indices[i] != 0)
5030 break;
5031 indices[i] = xlfd;
5034 if (!NILP (list) || i != ARRAYELTS (indices))
5035 signal_error ("Invalid font sort order", order);
5036 for (i = 0; i < ARRAYELTS (font_sort_order); ++i)
5037 if (indices[i] == 0)
5038 signal_error ("Invalid font sort order", order);
5040 if (memcmp (indices, font_sort_order, sizeof indices) != 0)
5042 memcpy (font_sort_order, indices, sizeof font_sort_order);
5043 free_all_realized_faces (Qnil);
5046 font_update_sort_order (font_sort_order);
5048 return Qnil;
5052 DEFUN ("internal-set-alternative-font-family-alist",
5053 Finternal_set_alternative_font_family_alist,
5054 Sinternal_set_alternative_font_family_alist, 1, 1, 0,
5055 doc: /* Define alternative font families to try in face font selection.
5056 ALIST is an alist of (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5057 Each ALTERNATIVE is tried in order if no fonts of font family FAMILY can
5058 be found. Value is ALIST. */)
5059 (Lisp_Object alist)
5061 Lisp_Object entry, tail, tail2;
5063 CHECK_LIST (alist);
5064 alist = Fcopy_sequence (alist);
5065 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5067 entry = XCAR (tail);
5068 CHECK_LIST (entry);
5069 entry = Fcopy_sequence (entry);
5070 XSETCAR (tail, entry);
5071 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5072 XSETCAR (tail2, Fintern (XCAR (tail2), Qnil));
5075 Vface_alternative_font_family_alist = alist;
5076 free_all_realized_faces (Qnil);
5077 return alist;
5081 DEFUN ("internal-set-alternative-font-registry-alist",
5082 Finternal_set_alternative_font_registry_alist,
5083 Sinternal_set_alternative_font_registry_alist, 1, 1, 0,
5084 doc: /* Define alternative font registries to try in face font selection.
5085 ALIST is an alist of (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5086 Each ALTERNATIVE is tried in order if no fonts of font registry REGISTRY can
5087 be found. Value is ALIST. */)
5088 (Lisp_Object alist)
5090 Lisp_Object entry, tail, tail2;
5092 CHECK_LIST (alist);
5093 alist = Fcopy_sequence (alist);
5094 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5096 entry = XCAR (tail);
5097 CHECK_LIST (entry);
5098 entry = Fcopy_sequence (entry);
5099 XSETCAR (tail, entry);
5100 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5101 XSETCAR (tail2, Fdowncase (XCAR (tail2)));
5103 Vface_alternative_font_registry_alist = alist;
5104 free_all_realized_faces (Qnil);
5105 return alist;
5109 #ifdef HAVE_WINDOW_SYSTEM
5111 /* Return the fontset id of the base fontset name or alias name given
5112 by the fontset attribute of ATTRS. Value is -1 if the fontset
5113 attribute of ATTRS doesn't name a fontset. */
5115 static int
5116 face_fontset (Lisp_Object attrs[LFACE_VECTOR_SIZE])
5118 Lisp_Object name;
5120 name = attrs[LFACE_FONTSET_INDEX];
5121 if (!STRINGP (name))
5122 return -1;
5123 return fs_query_fontset (name, 0);
5126 #endif /* HAVE_WINDOW_SYSTEM */
5130 /***********************************************************************
5131 Face Realization
5132 ***********************************************************************/
5134 /* Realize basic faces on frame F. Value is zero if frame parameters
5135 of F don't contain enough information needed to realize the default
5136 face. */
5138 static bool
5139 realize_basic_faces (struct frame *f)
5141 bool success_p = false;
5143 /* Block input here so that we won't be surprised by an X expose
5144 event, for instance, without having the faces set up. */
5145 block_input ();
5147 if (realize_default_face (f))
5149 realize_named_face (f, Qmode_line, MODE_LINE_FACE_ID);
5150 realize_named_face (f, Qmode_line_inactive, MODE_LINE_INACTIVE_FACE_ID);
5151 realize_named_face (f, Qtool_bar, TOOL_BAR_FACE_ID);
5152 realize_named_face (f, Qfringe, FRINGE_FACE_ID);
5153 realize_named_face (f, Qheader_line, HEADER_LINE_FACE_ID);
5154 realize_named_face (f, Qscroll_bar, SCROLL_BAR_FACE_ID);
5155 realize_named_face (f, Qborder, BORDER_FACE_ID);
5156 realize_named_face (f, Qcursor, CURSOR_FACE_ID);
5157 realize_named_face (f, Qmouse, MOUSE_FACE_ID);
5158 realize_named_face (f, Qmenu, MENU_FACE_ID);
5159 realize_named_face (f, Qvertical_border, VERTICAL_BORDER_FACE_ID);
5160 realize_named_face (f, Qwindow_divider, WINDOW_DIVIDER_FACE_ID);
5161 realize_named_face (f, Qwindow_divider_first_pixel,
5162 WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
5163 realize_named_face (f, Qwindow_divider_last_pixel,
5164 WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
5166 /* Reflect changes in the `menu' face in menu bars. */
5167 if (FRAME_FACE_CACHE (f)->menu_face_changed_p)
5169 FRAME_FACE_CACHE (f)->menu_face_changed_p = false;
5170 #ifdef USE_X_TOOLKIT
5171 if (FRAME_WINDOW_P (f))
5172 x_update_menu_appearance (f);
5173 #endif
5176 success_p = true;
5179 unblock_input ();
5180 return success_p;
5184 /* Realize the default face on frame F. If the face is not fully
5185 specified, make it fully-specified. Attributes of the default face
5186 that are not explicitly specified are taken from frame parameters. */
5188 static bool
5189 realize_default_face (struct frame *f)
5191 struct face_cache *c = FRAME_FACE_CACHE (f);
5192 Lisp_Object lface;
5193 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5194 struct face *face;
5196 /* If the `default' face is not yet known, create it. */
5197 lface = lface_from_face_name (f, Qdefault, false);
5198 if (NILP (lface))
5200 Lisp_Object frame;
5201 XSETFRAME (frame, f);
5202 lface = Finternal_make_lisp_face (Qdefault, frame);
5205 #ifdef HAVE_WINDOW_SYSTEM
5206 if (FRAME_WINDOW_P (f))
5208 Lisp_Object font_object;
5210 XSETFONT (font_object, FRAME_FONT (f));
5211 set_lface_from_font (f, lface, font_object, f->default_face_done_p);
5212 ASET (lface, LFACE_FONTSET_INDEX, fontset_name (FRAME_FONTSET (f)));
5213 f->default_face_done_p = true;
5215 #endif /* HAVE_WINDOW_SYSTEM */
5217 if (!FRAME_WINDOW_P (f))
5219 ASET (lface, LFACE_FAMILY_INDEX, build_string ("default"));
5220 ASET (lface, LFACE_FOUNDRY_INDEX, LFACE_FAMILY (lface));
5221 ASET (lface, LFACE_SWIDTH_INDEX, Qnormal);
5222 ASET (lface, LFACE_HEIGHT_INDEX, make_number (1));
5223 if (UNSPECIFIEDP (LFACE_WEIGHT (lface)))
5224 ASET (lface, LFACE_WEIGHT_INDEX, Qnormal);
5225 if (UNSPECIFIEDP (LFACE_SLANT (lface)))
5226 ASET (lface, LFACE_SLANT_INDEX, Qnormal);
5227 if (UNSPECIFIEDP (LFACE_FONTSET (lface)))
5228 ASET (lface, LFACE_FONTSET_INDEX, Qnil);
5231 if (UNSPECIFIEDP (LFACE_UNDERLINE (lface)))
5232 ASET (lface, LFACE_UNDERLINE_INDEX, Qnil);
5234 if (UNSPECIFIEDP (LFACE_OVERLINE (lface)))
5235 ASET (lface, LFACE_OVERLINE_INDEX, Qnil);
5237 if (UNSPECIFIEDP (LFACE_STRIKE_THROUGH (lface)))
5238 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, Qnil);
5240 if (UNSPECIFIEDP (LFACE_BOX (lface)))
5241 ASET (lface, LFACE_BOX_INDEX, Qnil);
5243 if (UNSPECIFIEDP (LFACE_INVERSE (lface)))
5244 ASET (lface, LFACE_INVERSE_INDEX, Qnil);
5246 if (UNSPECIFIEDP (LFACE_FOREGROUND (lface)))
5248 /* This function is called so early that colors are not yet
5249 set in the frame parameter list. */
5250 Lisp_Object color = Fassq (Qforeground_color, f->param_alist);
5252 if (CONSP (color) && STRINGP (XCDR (color)))
5253 ASET (lface, LFACE_FOREGROUND_INDEX, XCDR (color));
5254 else if (FRAME_WINDOW_P (f))
5255 return false;
5256 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5257 ASET (lface, LFACE_FOREGROUND_INDEX, build_string (unspecified_fg));
5258 else
5259 emacs_abort ();
5262 if (UNSPECIFIEDP (LFACE_BACKGROUND (lface)))
5264 /* This function is called so early that colors are not yet
5265 set in the frame parameter list. */
5266 Lisp_Object color = Fassq (Qbackground_color, f->param_alist);
5267 if (CONSP (color) && STRINGP (XCDR (color)))
5268 ASET (lface, LFACE_BACKGROUND_INDEX, XCDR (color));
5269 else if (FRAME_WINDOW_P (f))
5270 return false;
5271 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5272 ASET (lface, LFACE_BACKGROUND_INDEX, build_string (unspecified_bg));
5273 else
5274 emacs_abort ();
5277 if (UNSPECIFIEDP (LFACE_STIPPLE (lface)))
5278 ASET (lface, LFACE_STIPPLE_INDEX, Qnil);
5280 /* Realize the face; it must be fully-specified now. */
5281 eassert (lface_fully_specified_p (XVECTOR (lface)->contents));
5282 check_lface (lface);
5283 memcpy (attrs, XVECTOR (lface)->contents, sizeof attrs);
5284 face = realize_face (c, attrs, DEFAULT_FACE_ID);
5286 #ifdef HAVE_WINDOW_SYSTEM
5287 #ifdef HAVE_X_WINDOWS
5288 if (FRAME_X_P (f) && face->font != FRAME_FONT (f))
5290 /* This can happen when making a frame on a display that does
5291 not support the default font. */
5292 if (!face->font)
5293 return false;
5295 /* Otherwise, the font specified for the frame was not
5296 acceptable as a font for the default face (perhaps because
5297 auto-scaled fonts are rejected), so we must adjust the frame
5298 font. */
5299 x_set_font (f, LFACE_FONT (lface), Qnil);
5301 #endif /* HAVE_X_WINDOWS */
5302 #endif /* HAVE_WINDOW_SYSTEM */
5303 return true;
5307 /* Realize basic faces other than the default face in face cache C.
5308 SYMBOL is the face name, ID is the face id the realized face must
5309 have. The default face must have been realized already. */
5311 static void
5312 realize_named_face (struct frame *f, Lisp_Object symbol, int id)
5314 struct face_cache *c = FRAME_FACE_CACHE (f);
5315 Lisp_Object lface = lface_from_face_name (f, symbol, false);
5316 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5317 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5319 /* The default face must exist and be fully specified. */
5320 get_lface_attributes_no_remap (f, Qdefault, attrs, true);
5321 check_lface_attrs (attrs);
5322 eassert (lface_fully_specified_p (attrs));
5324 /* If SYMBOL isn't know as a face, create it. */
5325 if (NILP (lface))
5327 Lisp_Object frame;
5328 XSETFRAME (frame, f);
5329 lface = Finternal_make_lisp_face (symbol, frame);
5332 /* Merge SYMBOL's face with the default face. */
5333 get_lface_attributes_no_remap (f, symbol, symbol_attrs, true);
5334 merge_face_vectors (f, symbol_attrs, attrs, 0);
5336 /* Realize the face. */
5337 realize_face (c, attrs, id);
5341 /* Realize the fully-specified face with attributes ATTRS in face
5342 cache CACHE for ASCII characters. If FORMER_FACE_ID is
5343 non-negative, it is an ID of face to remove before caching the new
5344 face. Value is a pointer to the newly created realized face. */
5346 static struct face *
5347 realize_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE],
5348 int former_face_id)
5350 struct face *face;
5352 /* LFACE must be fully specified. */
5353 eassert (cache != NULL);
5354 check_lface_attrs (attrs);
5356 if (former_face_id >= 0 && cache->used > former_face_id)
5358 /* Remove the former face. */
5359 struct face *former_face = cache->faces_by_id[former_face_id];
5360 uncache_face (cache, former_face);
5361 free_realized_face (cache->f, former_face);
5362 SET_FRAME_GARBAGED (cache->f);
5365 if (FRAME_WINDOW_P (cache->f))
5366 face = realize_x_face (cache, attrs);
5367 else if (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f))
5368 face = realize_tty_face (cache, attrs);
5369 else if (FRAME_INITIAL_P (cache->f))
5371 /* Create a dummy face. */
5372 face = make_realized_face (attrs);
5374 else
5375 emacs_abort ();
5377 /* Insert the new face. */
5378 cache_face (cache, face, lface_hash (attrs));
5379 return face;
5383 #ifdef HAVE_WINDOW_SYSTEM
5384 /* Realize the fully-specified face that uses FONT-OBJECT and has the
5385 same attributes as BASE_FACE except for the font on frame F.
5386 FONT-OBJECT may be nil, in which case, realized a face of
5387 no-font. */
5389 static struct face *
5390 realize_non_ascii_face (struct frame *f, Lisp_Object font_object,
5391 struct face *base_face)
5393 struct face_cache *cache = FRAME_FACE_CACHE (f);
5394 struct face *face;
5396 face = xmalloc (sizeof *face);
5397 *face = *base_face;
5398 face->gc = 0;
5399 face->overstrike
5400 = (! NILP (font_object)
5401 && FONT_WEIGHT_NAME_NUMERIC (face->lface[LFACE_WEIGHT_INDEX]) > 100
5402 && FONT_WEIGHT_NUMERIC (font_object) <= 100);
5404 /* Don't try to free the colors copied bitwise from BASE_FACE. */
5405 face->colors_copied_bitwise_p = true;
5406 face->font = NILP (font_object) ? NULL : XFONT_OBJECT (font_object);
5407 face->gc = 0;
5409 cache_face (cache, face, face->hash);
5411 return face;
5413 #endif /* HAVE_WINDOW_SYSTEM */
5416 /* Realize the fully-specified face with attributes ATTRS in face
5417 cache CACHE for ASCII characters. Do it for X frame CACHE->f. If
5418 the new face doesn't share font with the default face, a fontname
5419 is allocated from the heap and set in `font_name' of the new face,
5420 but it is not yet loaded here. Value is a pointer to the newly
5421 created realized face. */
5423 static struct face *
5424 realize_x_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE])
5426 struct face *face = NULL;
5427 #ifdef HAVE_WINDOW_SYSTEM
5428 struct face *default_face;
5429 struct frame *f;
5430 Lisp_Object stipple, underline, overline, strike_through, box;
5432 eassert (FRAME_WINDOW_P (cache->f));
5434 /* Allocate a new realized face. */
5435 face = make_realized_face (attrs);
5436 face->ascii_face = face;
5438 f = cache->f;
5440 /* Determine the font to use. Most of the time, the font will be
5441 the same as the font of the default face, so try that first. */
5442 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5443 if (default_face
5444 && lface_same_font_attributes_p (default_face->lface, attrs))
5446 face->font = default_face->font;
5447 face->fontset
5448 = make_fontset_for_ascii_face (f, default_face->fontset, face);
5450 else
5452 /* If the face attribute ATTRS specifies a fontset, use it as
5453 the base of a new realized fontset. Otherwise, use the same
5454 base fontset as of the default face. The base determines
5455 registry and encoding of a font. It may also determine
5456 foundry and family. The other fields of font name pattern
5457 are constructed from ATTRS. */
5458 int fontset = face_fontset (attrs);
5460 /* If we are realizing the default face, ATTRS should specify a
5461 fontset. In other words, if FONTSET is -1, we are not
5462 realizing the default face, thus the default face should have
5463 already been realized. */
5464 if (fontset == -1)
5466 if (default_face)
5467 fontset = default_face->fontset;
5468 if (fontset == -1)
5469 emacs_abort ();
5471 if (! FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5472 attrs[LFACE_FONT_INDEX]
5473 = font_load_for_lface (f, attrs, Ffont_spec (0, NULL));
5474 if (FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5476 face->font = XFONT_OBJECT (attrs[LFACE_FONT_INDEX]);
5477 face->fontset = make_fontset_for_ascii_face (f, fontset, face);
5479 else
5481 face->font = NULL;
5482 face->fontset = -1;
5486 if (face->font
5487 && FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]) > 100
5488 && FONT_WEIGHT_NUMERIC (attrs[LFACE_FONT_INDEX]) <= 100)
5489 face->overstrike = true;
5491 /* Load colors, and set remaining attributes. */
5493 load_face_colors (f, face, attrs);
5495 /* Set up box. */
5496 box = attrs[LFACE_BOX_INDEX];
5497 if (STRINGP (box))
5499 /* A simple box of line width 1 drawn in color given by
5500 the string. */
5501 face->box_color = load_color (f, face, attrs[LFACE_BOX_INDEX],
5502 LFACE_BOX_INDEX);
5503 face->box = FACE_SIMPLE_BOX;
5504 face->box_line_width = 1;
5506 else if (INTEGERP (box))
5508 /* Simple box of specified line width in foreground color of the
5509 face. */
5510 eassert (XINT (box) != 0);
5511 face->box = FACE_SIMPLE_BOX;
5512 face->box_line_width = XINT (box);
5513 face->box_color = face->foreground;
5514 face->box_color_defaulted_p = true;
5516 else if (CONSP (box))
5518 /* `(:width WIDTH :color COLOR :shadow SHADOW)'. SHADOW
5519 being one of `raised' or `sunken'. */
5520 face->box = FACE_SIMPLE_BOX;
5521 face->box_color = face->foreground;
5522 face->box_color_defaulted_p = true;
5523 face->box_line_width = 1;
5525 while (CONSP (box))
5527 Lisp_Object keyword, value;
5529 keyword = XCAR (box);
5530 box = XCDR (box);
5532 if (!CONSP (box))
5533 break;
5534 value = XCAR (box);
5535 box = XCDR (box);
5537 if (EQ (keyword, QCline_width))
5539 if (INTEGERP (value) && XINT (value) != 0)
5540 face->box_line_width = XINT (value);
5542 else if (EQ (keyword, QCcolor))
5544 if (STRINGP (value))
5546 face->box_color = load_color (f, face, value,
5547 LFACE_BOX_INDEX);
5548 face->use_box_color_for_shadows_p = true;
5551 else if (EQ (keyword, QCstyle))
5553 if (EQ (value, Qreleased_button))
5554 face->box = FACE_RAISED_BOX;
5555 else if (EQ (value, Qpressed_button))
5556 face->box = FACE_SUNKEN_BOX;
5561 /* Text underline, overline, strike-through. */
5563 underline = attrs[LFACE_UNDERLINE_INDEX];
5564 if (EQ (underline, Qt))
5566 /* Use default color (same as foreground color). */
5567 face->underline_p = true;
5568 face->underline_type = FACE_UNDER_LINE;
5569 face->underline_defaulted_p = true;
5570 face->underline_color = 0;
5572 else if (STRINGP (underline))
5574 /* Use specified color. */
5575 face->underline_p = true;
5576 face->underline_type = FACE_UNDER_LINE;
5577 face->underline_defaulted_p = false;
5578 face->underline_color
5579 = load_color (f, face, underline,
5580 LFACE_UNDERLINE_INDEX);
5582 else if (NILP (underline))
5584 face->underline_p = false;
5585 face->underline_defaulted_p = false;
5586 face->underline_color = 0;
5588 else if (CONSP (underline))
5590 /* `(:color COLOR :style STYLE)'.
5591 STYLE being one of `line' or `wave'. */
5592 face->underline_p = true;
5593 face->underline_color = 0;
5594 face->underline_defaulted_p = true;
5595 face->underline_type = FACE_UNDER_LINE;
5597 /* FIXME? This is also not robust about checking the precise form.
5598 See comments in Finternal_set_lisp_face_attribute. */
5599 while (CONSP (underline))
5601 Lisp_Object keyword, value;
5603 keyword = XCAR (underline);
5604 underline = XCDR (underline);
5606 if (!CONSP (underline))
5607 break;
5608 value = XCAR (underline);
5609 underline = XCDR (underline);
5611 if (EQ (keyword, QCcolor))
5613 if (EQ (value, Qforeground_color))
5615 face->underline_defaulted_p = true;
5616 face->underline_color = 0;
5618 else if (STRINGP (value))
5620 face->underline_defaulted_p = false;
5621 face->underline_color = load_color (f, face, value,
5622 LFACE_UNDERLINE_INDEX);
5625 else if (EQ (keyword, QCstyle))
5627 if (EQ (value, Qline))
5628 face->underline_type = FACE_UNDER_LINE;
5629 else if (EQ (value, Qwave))
5630 face->underline_type = FACE_UNDER_WAVE;
5635 overline = attrs[LFACE_OVERLINE_INDEX];
5636 if (STRINGP (overline))
5638 face->overline_color
5639 = load_color (f, face, attrs[LFACE_OVERLINE_INDEX],
5640 LFACE_OVERLINE_INDEX);
5641 face->overline_p = true;
5643 else if (EQ (overline, Qt))
5645 face->overline_color = face->foreground;
5646 face->overline_color_defaulted_p = true;
5647 face->overline_p = true;
5650 strike_through = attrs[LFACE_STRIKE_THROUGH_INDEX];
5651 if (STRINGP (strike_through))
5653 face->strike_through_color
5654 = load_color (f, face, attrs[LFACE_STRIKE_THROUGH_INDEX],
5655 LFACE_STRIKE_THROUGH_INDEX);
5656 face->strike_through_p = true;
5658 else if (EQ (strike_through, Qt))
5660 face->strike_through_color = face->foreground;
5661 face->strike_through_color_defaulted_p = true;
5662 face->strike_through_p = true;
5665 stipple = attrs[LFACE_STIPPLE_INDEX];
5666 if (!NILP (stipple))
5667 face->stipple = load_pixmap (f, stipple);
5668 #endif /* HAVE_WINDOW_SYSTEM */
5670 return face;
5674 /* Map a specified color of face FACE on frame F to a tty color index.
5675 IDX is either LFACE_FOREGROUND_INDEX or LFACE_BACKGROUND_INDEX, and
5676 specifies which color to map. Set *DEFAULTED to true if mapping to the
5677 default foreground/background colors. */
5679 static void
5680 map_tty_color (struct frame *f, struct face *face,
5681 enum lface_attribute_index idx, bool *defaulted)
5683 Lisp_Object frame, color, def;
5684 bool foreground_p = idx == LFACE_FOREGROUND_INDEX;
5685 unsigned long default_pixel =
5686 foreground_p ? FACE_TTY_DEFAULT_FG_COLOR : FACE_TTY_DEFAULT_BG_COLOR;
5687 unsigned long pixel = default_pixel;
5688 #ifdef MSDOS
5689 unsigned long default_other_pixel =
5690 foreground_p ? FACE_TTY_DEFAULT_BG_COLOR : FACE_TTY_DEFAULT_FG_COLOR;
5691 #endif
5693 eassert (idx == LFACE_FOREGROUND_INDEX || idx == LFACE_BACKGROUND_INDEX);
5695 XSETFRAME (frame, f);
5696 color = face->lface[idx];
5698 if (STRINGP (color)
5699 && SCHARS (color)
5700 && CONSP (Vtty_defined_color_alist)
5701 && (def = assoc_no_quit (color, call1 (Qtty_color_alist, frame)),
5702 CONSP (def)))
5704 /* Associations in tty-defined-color-alist are of the form
5705 (NAME INDEX R G B). We need the INDEX part. */
5706 pixel = XINT (XCAR (XCDR (def)));
5709 if (pixel == default_pixel && STRINGP (color))
5711 pixel = load_color (f, face, color, idx);
5713 #ifdef MSDOS
5714 /* If the foreground of the default face is the default color,
5715 use the foreground color defined by the frame. */
5716 if (FRAME_MSDOS_P (f))
5718 if (pixel == default_pixel
5719 || pixel == FACE_TTY_DEFAULT_COLOR)
5721 if (foreground_p)
5722 pixel = FRAME_FOREGROUND_PIXEL (f);
5723 else
5724 pixel = FRAME_BACKGROUND_PIXEL (f);
5725 face->lface[idx] = tty_color_name (f, pixel);
5726 *defaulted = true;
5728 else if (pixel == default_other_pixel)
5730 if (foreground_p)
5731 pixel = FRAME_BACKGROUND_PIXEL (f);
5732 else
5733 pixel = FRAME_FOREGROUND_PIXEL (f);
5734 face->lface[idx] = tty_color_name (f, pixel);
5735 *defaulted = true;
5738 #endif /* MSDOS */
5741 if (foreground_p)
5742 face->foreground = pixel;
5743 else
5744 face->background = pixel;
5748 /* Realize the fully-specified face with attributes ATTRS in face
5749 cache CACHE for ASCII characters. Do it for TTY frame CACHE->f.
5750 Value is a pointer to the newly created realized face. */
5752 static struct face *
5753 realize_tty_face (struct face_cache *cache,
5754 Lisp_Object attrs[LFACE_VECTOR_SIZE])
5756 struct face *face;
5757 int weight, slant;
5758 bool face_colors_defaulted = false;
5759 struct frame *f = cache->f;
5761 /* Frame must be a termcap frame. */
5762 eassert (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f));
5764 /* Allocate a new realized face. */
5765 face = make_realized_face (attrs);
5766 #if false
5767 face->font_name = FRAME_MSDOS_P (cache->f) ? "ms-dos" : "tty";
5768 #endif
5770 /* Map face attributes to TTY appearances. */
5771 weight = FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]);
5772 slant = FONT_SLANT_NAME_NUMERIC (attrs[LFACE_SLANT_INDEX]);
5773 if (weight > 100)
5774 face->tty_bold_p = true;
5775 if (slant != 100)
5776 face->tty_italic_p = true;
5777 if (!NILP (attrs[LFACE_UNDERLINE_INDEX]))
5778 face->tty_underline_p = true;
5779 if (!NILP (attrs[LFACE_INVERSE_INDEX]))
5780 face->tty_reverse_p = true;
5782 /* Map color names to color indices. */
5783 map_tty_color (f, face, LFACE_FOREGROUND_INDEX, &face_colors_defaulted);
5784 map_tty_color (f, face, LFACE_BACKGROUND_INDEX, &face_colors_defaulted);
5786 /* Swap colors if face is inverse-video. If the colors are taken
5787 from the frame colors, they are already inverted, since the
5788 frame-creation function calls x-handle-reverse-video. */
5789 if (face->tty_reverse_p && !face_colors_defaulted)
5791 unsigned long tem = face->foreground;
5792 face->foreground = face->background;
5793 face->background = tem;
5796 if (tty_suppress_bold_inverse_default_colors_p
5797 && face->tty_bold_p
5798 && face->background == FACE_TTY_DEFAULT_FG_COLOR
5799 && face->foreground == FACE_TTY_DEFAULT_BG_COLOR)
5800 face->tty_bold_p = false;
5802 return face;
5806 DEFUN ("tty-suppress-bold-inverse-default-colors",
5807 Ftty_suppress_bold_inverse_default_colors,
5808 Stty_suppress_bold_inverse_default_colors, 1, 1, 0,
5809 doc: /* Suppress/allow boldness of faces with inverse default colors.
5810 SUPPRESS non-nil means suppress it.
5811 This affects bold faces on TTYs whose foreground is the default background
5812 color of the display and whose background is the default foreground color.
5813 For such faces, the bold face attribute is ignored if this variable
5814 is non-nil. */)
5815 (Lisp_Object suppress)
5817 tty_suppress_bold_inverse_default_colors_p = !NILP (suppress);
5818 face_change = true;
5819 return suppress;
5824 /***********************************************************************
5825 Computing Faces
5826 ***********************************************************************/
5828 /* Return the ID of the face to use to display character CH with face
5829 property PROP on frame F in current_buffer. */
5832 compute_char_face (struct frame *f, int ch, Lisp_Object prop)
5834 int face_id;
5836 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
5837 ch = 0;
5839 if (NILP (prop))
5841 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5842 face_id = FACE_FOR_CHAR (f, face, ch, -1, Qnil);
5844 else
5846 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5847 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5848 memcpy (attrs, default_face->lface, sizeof attrs);
5849 merge_face_ref (f, prop, attrs, true, 0);
5850 face_id = lookup_face (f, attrs);
5853 return face_id;
5856 /* Return the face ID associated with buffer position POS for
5857 displaying ASCII characters. Return in *ENDPTR the position at
5858 which a different face is needed, as far as text properties and
5859 overlays are concerned. W is a window displaying current_buffer.
5861 REGION_BEG, REGION_END delimit the region, so it can be
5862 highlighted.
5864 LIMIT is a position not to scan beyond. That is to limit the time
5865 this function can take.
5867 If MOUSE, use the character's mouse-face, not its face.
5869 BASE_FACE_ID, if non-negative, specifies a base face id to use
5870 instead of DEFAULT_FACE_ID.
5872 The face returned is suitable for displaying ASCII characters. */
5875 face_at_buffer_position (struct window *w, ptrdiff_t pos,
5876 ptrdiff_t *endptr, ptrdiff_t limit,
5877 bool mouse, int base_face_id)
5879 struct frame *f = XFRAME (w->frame);
5880 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5881 Lisp_Object prop, position;
5882 ptrdiff_t i, noverlays;
5883 Lisp_Object *overlay_vec;
5884 ptrdiff_t endpos;
5885 Lisp_Object propname = mouse ? Qmouse_face : Qface;
5886 Lisp_Object limit1, end;
5887 struct face *default_face;
5889 /* W must display the current buffer. We could write this function
5890 to use the frame and buffer of W, but right now it doesn't. */
5891 /* eassert (XBUFFER (w->contents) == current_buffer); */
5893 XSETFASTINT (position, pos);
5895 endpos = ZV;
5897 /* Get the `face' or `mouse_face' text property at POS, and
5898 determine the next position at which the property changes. */
5899 prop = Fget_text_property (position, propname, w->contents);
5900 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
5901 end = Fnext_single_property_change (position, propname, w->contents, limit1);
5902 if (INTEGERP (end))
5903 endpos = XINT (end);
5905 /* Look at properties from overlays. */
5906 USE_SAFE_ALLOCA;
5908 ptrdiff_t next_overlay;
5910 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, &next_overlay, false);
5911 if (next_overlay < endpos)
5912 endpos = next_overlay;
5915 *endptr = endpos;
5918 int face_id;
5920 if (base_face_id >= 0)
5921 face_id = base_face_id;
5922 else if (NILP (Vface_remapping_alist))
5923 face_id = DEFAULT_FACE_ID;
5924 else
5925 face_id = lookup_basic_face (f, DEFAULT_FACE_ID);
5927 default_face = FACE_FROM_ID (f, face_id);
5930 /* Optimize common cases where we can use the default face. */
5931 if (noverlays == 0
5932 && NILP (prop))
5934 SAFE_FREE ();
5935 return default_face->id;
5938 /* Begin with attributes from the default face. */
5939 memcpy (attrs, default_face->lface, sizeof attrs);
5941 /* Merge in attributes specified via text properties. */
5942 if (!NILP (prop))
5943 merge_face_ref (f, prop, attrs, true, 0);
5945 /* Now merge the overlay data. */
5946 noverlays = sort_overlays (overlay_vec, noverlays, w);
5947 for (i = 0; i < noverlays; i++)
5949 Lisp_Object oend;
5950 ptrdiff_t oendpos;
5952 prop = Foverlay_get (overlay_vec[i], propname);
5953 if (!NILP (prop))
5954 merge_face_ref (f, prop, attrs, true, 0);
5956 oend = OVERLAY_END (overlay_vec[i]);
5957 oendpos = OVERLAY_POSITION (oend);
5958 if (oendpos < endpos)
5959 endpos = oendpos;
5962 *endptr = endpos;
5964 SAFE_FREE ();
5966 /* Look up a realized face with the given face attributes,
5967 or realize a new one for ASCII characters. */
5968 return lookup_face (f, attrs);
5971 /* Return the face ID at buffer position POS for displaying ASCII
5972 characters associated with overlay strings for overlay OVERLAY.
5974 Like face_at_buffer_position except for OVERLAY. Currently it
5975 simply disregards the `face' properties of all overlays. */
5978 face_for_overlay_string (struct window *w, ptrdiff_t pos,
5979 ptrdiff_t *endptr, ptrdiff_t limit,
5980 bool mouse, Lisp_Object overlay)
5982 struct frame *f = XFRAME (w->frame);
5983 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5984 Lisp_Object prop, position;
5985 ptrdiff_t endpos;
5986 Lisp_Object propname = mouse ? Qmouse_face : Qface;
5987 Lisp_Object limit1, end;
5988 struct face *default_face;
5990 /* W must display the current buffer. We could write this function
5991 to use the frame and buffer of W, but right now it doesn't. */
5992 /* eassert (XBUFFER (w->contents) == current_buffer); */
5994 XSETFASTINT (position, pos);
5996 endpos = ZV;
5998 /* Get the `face' or `mouse_face' text property at POS, and
5999 determine the next position at which the property changes. */
6000 prop = Fget_text_property (position, propname, w->contents);
6001 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
6002 end = Fnext_single_property_change (position, propname, w->contents, limit1);
6003 if (INTEGERP (end))
6004 endpos = XINT (end);
6006 *endptr = endpos;
6008 /* Optimize common case where we can use the default face. */
6009 if (NILP (prop)
6010 && NILP (Vface_remapping_alist))
6011 return DEFAULT_FACE_ID;
6013 /* Begin with attributes from the default face. */
6014 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
6015 memcpy (attrs, default_face->lface, sizeof attrs);
6017 /* Merge in attributes specified via text properties. */
6018 if (!NILP (prop))
6019 merge_face_ref (f, prop, attrs, true, 0);
6021 *endptr = endpos;
6023 /* Look up a realized face with the given face attributes,
6024 or realize a new one for ASCII characters. */
6025 return lookup_face (f, attrs);
6029 /* Compute the face at character position POS in Lisp string STRING on
6030 window W, for ASCII characters.
6032 If STRING is an overlay string, it comes from position BUFPOS in
6033 current_buffer, otherwise BUFPOS is zero to indicate that STRING is
6034 not an overlay string. W must display the current buffer.
6035 REGION_BEG and REGION_END give the start and end positions of the
6036 region; both are -1 if no region is visible.
6038 BASE_FACE_ID is the id of a face to merge with. For strings coming
6039 from overlays or the `display' property it is the face at BUFPOS.
6041 If MOUSE_P, use the character's mouse-face, not its face.
6043 Set *ENDPTR to the next position where to check for faces in
6044 STRING; -1 if the face is constant from POS to the end of the
6045 string.
6047 Value is the id of the face to use. The face returned is suitable
6048 for displaying ASCII characters. */
6051 face_at_string_position (struct window *w, Lisp_Object string,
6052 ptrdiff_t pos, ptrdiff_t bufpos,
6053 ptrdiff_t *endptr, enum face_id base_face_id,
6054 bool mouse_p)
6056 Lisp_Object prop, position, end, limit;
6057 struct frame *f = XFRAME (WINDOW_FRAME (w));
6058 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6059 struct face *base_face;
6060 bool multibyte_p = STRING_MULTIBYTE (string);
6061 Lisp_Object prop_name = mouse_p ? Qmouse_face : Qface;
6063 /* Get the value of the face property at the current position within
6064 STRING. Value is nil if there is no face property. */
6065 XSETFASTINT (position, pos);
6066 prop = Fget_text_property (position, prop_name, string);
6068 /* Get the next position at which to check for faces. Value of end
6069 is nil if face is constant all the way to the end of the string.
6070 Otherwise it is a string position where to check faces next.
6071 Limit is the maximum position up to which to check for property
6072 changes in Fnext_single_property_change. Strings are usually
6073 short, so set the limit to the end of the string. */
6074 XSETFASTINT (limit, SCHARS (string));
6075 end = Fnext_single_property_change (position, prop_name, string, limit);
6076 if (INTEGERP (end))
6077 *endptr = XFASTINT (end);
6078 else
6079 *endptr = -1;
6081 base_face = FACE_FROM_ID (f, base_face_id);
6082 eassert (base_face);
6084 /* Optimize the default case that there is no face property. */
6085 if (NILP (prop)
6086 && (multibyte_p
6087 /* We can't realize faces for different charsets differently
6088 if we don't have fonts, so we can stop here if not working
6089 on a window-system frame. */
6090 || !FRAME_WINDOW_P (f)
6091 || FACE_SUITABLE_FOR_ASCII_CHAR_P (base_face, 0)))
6092 return base_face->id;
6094 /* Begin with attributes from the base face. */
6095 memcpy (attrs, base_face->lface, sizeof attrs);
6097 /* Merge in attributes specified via text properties. */
6098 if (!NILP (prop))
6099 merge_face_ref (f, prop, attrs, true, 0);
6101 /* Look up a realized face with the given face attributes,
6102 or realize a new one for ASCII characters. */
6103 return lookup_face (f, attrs);
6107 /* Merge a face into a realized face.
6109 F is frame where faces are (to be) realized.
6111 FACE_NAME is named face to merge.
6113 If FACE_NAME is nil, FACE_ID is face_id of realized face to merge.
6115 If FACE_NAME is t, FACE_ID is lface_id of face to merge.
6117 BASE_FACE_ID is realized face to merge into.
6119 Return new face id.
6123 merge_faces (struct frame *f, Lisp_Object face_name, int face_id,
6124 int base_face_id)
6126 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6127 struct face *base_face;
6129 base_face = FACE_FROM_ID (f, base_face_id);
6130 if (!base_face)
6131 return base_face_id;
6133 if (EQ (face_name, Qt))
6135 if (face_id < 0 || face_id >= lface_id_to_name_size)
6136 return base_face_id;
6137 face_name = lface_id_to_name[face_id];
6138 /* When called during make-frame, lookup_derived_face may fail
6139 if the faces are uninitialized. Don't signal an error. */
6140 face_id = lookup_derived_face (f, face_name, base_face_id, 0);
6141 return (face_id >= 0 ? face_id : base_face_id);
6144 /* Begin with attributes from the base face. */
6145 memcpy (attrs, base_face->lface, sizeof attrs);
6147 if (!NILP (face_name))
6149 if (!merge_named_face (f, face_name, attrs, 0))
6150 return base_face_id;
6152 else
6154 struct face *face;
6155 if (face_id < 0)
6156 return base_face_id;
6157 face = FACE_FROM_ID (f, face_id);
6158 if (!face)
6159 return base_face_id;
6160 merge_face_vectors (f, face->lface, attrs, 0);
6163 /* Look up a realized face with the given face attributes,
6164 or realize a new one for ASCII characters. */
6165 return lookup_face (f, attrs);
6170 #ifndef HAVE_X_WINDOWS
6171 DEFUN ("x-load-color-file", Fx_load_color_file,
6172 Sx_load_color_file, 1, 1, 0,
6173 doc: /* Create an alist of color entries from an external file.
6175 The file should define one named RGB color per line like so:
6176 R G B name
6177 where R,G,B are numbers between 0 and 255 and name is an arbitrary string. */)
6178 (Lisp_Object filename)
6180 FILE *fp;
6181 Lisp_Object cmap = Qnil;
6182 Lisp_Object abspath;
6184 CHECK_STRING (filename);
6185 abspath = Fexpand_file_name (filename, Qnil);
6187 block_input ();
6188 fp = emacs_fopen (SSDATA (abspath), "r" FOPEN_TEXT);
6189 if (fp)
6191 char buf[512];
6192 int red, green, blue;
6193 int num;
6195 while (fgets (buf, sizeof (buf), fp) != NULL) {
6196 if (sscanf (buf, "%u %u %u %n", &red, &green, &blue, &num) == 3)
6198 #ifdef HAVE_NTGUI
6199 int color = RGB (red, green, blue);
6200 #else
6201 int color = (red << 16) | (green << 8) | blue;
6202 #endif
6203 char *name = buf + num;
6204 ptrdiff_t len = strlen (name);
6205 len -= 0 < len && name[len - 1] == '\n';
6206 cmap = Fcons (Fcons (make_string (name, len), make_number (color)),
6207 cmap);
6210 fclose (fp);
6212 unblock_input ();
6213 return cmap;
6215 #endif
6218 /***********************************************************************
6219 Tests
6220 ***********************************************************************/
6222 #ifdef GLYPH_DEBUG
6224 /* Print the contents of the realized face FACE to stderr. */
6226 static void
6227 dump_realized_face (struct face *face)
6229 fprintf (stderr, "ID: %d\n", face->id);
6230 #ifdef HAVE_X_WINDOWS
6231 fprintf (stderr, "gc: %p\n", face->gc);
6232 #endif
6233 fprintf (stderr, "foreground: 0x%lx (%s)\n",
6234 face->foreground,
6235 SDATA (face->lface[LFACE_FOREGROUND_INDEX]));
6236 fprintf (stderr, "background: 0x%lx (%s)\n",
6237 face->background,
6238 SDATA (face->lface[LFACE_BACKGROUND_INDEX]));
6239 if (face->font)
6240 fprintf (stderr, "font_name: %s (%s)\n",
6241 SDATA (face->font->props[FONT_NAME_INDEX]),
6242 SDATA (face->lface[LFACE_FAMILY_INDEX]));
6243 #ifdef HAVE_X_WINDOWS
6244 fprintf (stderr, "font = %p\n", face->font);
6245 #endif
6246 fprintf (stderr, "fontset: %d\n", face->fontset);
6247 fprintf (stderr, "underline: %d (%s)\n",
6248 face->underline_p,
6249 SDATA (Fsymbol_name (face->lface[LFACE_UNDERLINE_INDEX])));
6250 fprintf (stderr, "hash: %d\n", face->hash);
6254 DEFUN ("dump-face", Fdump_face, Sdump_face, 0, 1, 0, doc: /* */)
6255 (Lisp_Object n)
6257 if (NILP (n))
6259 int i;
6261 fprintf (stderr, "font selection order: ");
6262 for (i = 0; i < ARRAYELTS (font_sort_order); ++i)
6263 fprintf (stderr, "%d ", font_sort_order[i]);
6264 fprintf (stderr, "\n");
6266 fprintf (stderr, "alternative fonts: ");
6267 debug_print (Vface_alternative_font_family_alist);
6268 fprintf (stderr, "\n");
6270 for (i = 0; i < FRAME_FACE_CACHE (SELECTED_FRAME ())->used; ++i)
6271 Fdump_face (make_number (i));
6273 else
6275 struct face *face;
6276 CHECK_NUMBER (n);
6277 face = FACE_FROM_ID (SELECTED_FRAME (), XINT (n));
6278 if (face == NULL)
6279 error ("Not a valid face");
6280 dump_realized_face (face);
6283 return Qnil;
6287 DEFUN ("show-face-resources", Fshow_face_resources, Sshow_face_resources,
6288 0, 0, 0, doc: /* */)
6289 (void)
6291 fprintf (stderr, "number of colors = %d\n", ncolors_allocated);
6292 fprintf (stderr, "number of pixmaps = %d\n", npixmaps_allocated);
6293 fprintf (stderr, "number of GCs = %d\n", ngcs);
6294 return Qnil;
6297 #endif /* GLYPH_DEBUG */
6301 /***********************************************************************
6302 Initialization
6303 ***********************************************************************/
6305 void
6306 syms_of_xfaces (void)
6308 /* The symbols `face' and `mouse-face' used as text properties. */
6309 DEFSYM (Qface, "face");
6311 /* Property for basic faces which other faces cannot inherit. */
6312 DEFSYM (Qface_no_inherit, "face-no-inherit");
6314 /* Error symbol for wrong_type_argument in load_pixmap. */
6315 DEFSYM (Qbitmap_spec_p, "bitmap-spec-p");
6317 /* The name of the function to call when the background of the frame
6318 has changed, frame_set_background_mode. */
6319 DEFSYM (Qframe_set_background_mode, "frame-set-background-mode");
6321 /* Lisp face attribute keywords. */
6322 DEFSYM (QCfamily, ":family");
6323 DEFSYM (QCheight, ":height");
6324 DEFSYM (QCweight, ":weight");
6325 DEFSYM (QCslant, ":slant");
6326 DEFSYM (QCunderline, ":underline");
6327 DEFSYM (QCinverse_video, ":inverse-video");
6328 DEFSYM (QCreverse_video, ":reverse-video");
6329 DEFSYM (QCforeground, ":foreground");
6330 DEFSYM (QCbackground, ":background");
6331 DEFSYM (QCstipple, ":stipple");
6332 DEFSYM (QCwidth, ":width");
6333 DEFSYM (QCfont, ":font");
6334 DEFSYM (QCfontset, ":fontset");
6335 DEFSYM (QCdistant_foreground, ":distant-foreground");
6336 DEFSYM (QCbold, ":bold");
6337 DEFSYM (QCitalic, ":italic");
6338 DEFSYM (QCoverline, ":overline");
6339 DEFSYM (QCstrike_through, ":strike-through");
6340 DEFSYM (QCbox, ":box");
6341 DEFSYM (QCinherit, ":inherit");
6343 /* Symbols used for Lisp face attribute values. */
6344 DEFSYM (QCcolor, ":color");
6345 DEFSYM (QCline_width, ":line-width");
6346 DEFSYM (QCstyle, ":style");
6347 DEFSYM (Qline, "line");
6348 DEFSYM (Qwave, "wave");
6349 DEFSYM (Qreleased_button, "released-button");
6350 DEFSYM (Qpressed_button, "pressed-button");
6351 DEFSYM (Qnormal, "normal");
6352 DEFSYM (Qextra_light, "extra-light");
6353 DEFSYM (Qlight, "light");
6354 DEFSYM (Qsemi_light, "semi-light");
6355 DEFSYM (Qsemi_bold, "semi-bold");
6356 DEFSYM (Qbold, "bold");
6357 DEFSYM (Qextra_bold, "extra-bold");
6358 DEFSYM (Qultra_bold, "ultra-bold");
6359 DEFSYM (Qoblique, "oblique");
6360 DEFSYM (Qitalic, "italic");
6362 /* The symbols `foreground-color' and `background-color' which can be
6363 used as part of a `face' property. This is for compatibility with
6364 Emacs 20.2. */
6365 DEFSYM (Qbackground_color, "background-color");
6366 DEFSYM (Qforeground_color, "foreground-color");
6368 DEFSYM (Qunspecified, "unspecified");
6369 DEFSYM (QCignore_defface, ":ignore-defface");
6371 /* The symbol `face-alias'. A symbol having that property is an
6372 alias for another face. Value of the property is the name of
6373 the aliased face. */
6374 DEFSYM (Qface_alias, "face-alias");
6376 /* Names of basic faces. */
6377 DEFSYM (Qdefault, "default");
6378 DEFSYM (Qtool_bar, "tool-bar");
6379 DEFSYM (Qfringe, "fringe");
6380 DEFSYM (Qheader_line, "header-line");
6381 DEFSYM (Qscroll_bar, "scroll-bar");
6382 DEFSYM (Qmenu, "menu");
6383 DEFSYM (Qcursor, "cursor");
6384 DEFSYM (Qborder, "border");
6385 DEFSYM (Qmouse, "mouse");
6386 DEFSYM (Qmode_line_inactive, "mode-line-inactive");
6387 DEFSYM (Qvertical_border, "vertical-border");
6389 /* TTY color-related functions (defined in tty-colors.el). */
6390 DEFSYM (Qwindow_divider, "window-divider");
6391 DEFSYM (Qwindow_divider_first_pixel, "window-divider-first-pixel");
6392 DEFSYM (Qwindow_divider_last_pixel, "window-divider-last-pixel");
6393 DEFSYM (Qtty_color_desc, "tty-color-desc");
6394 DEFSYM (Qtty_color_standard_values, "tty-color-standard-values");
6395 DEFSYM (Qtty_color_by_index, "tty-color-by-index");
6397 /* The name of the function used to compute colors on TTYs. */
6398 DEFSYM (Qtty_color_alist, "tty-color-alist");
6400 Vparam_value_alist = list1 (Fcons (Qnil, Qnil));
6401 staticpro (&Vparam_value_alist);
6402 Vface_alternative_font_family_alist = Qnil;
6403 staticpro (&Vface_alternative_font_family_alist);
6404 Vface_alternative_font_registry_alist = Qnil;
6405 staticpro (&Vface_alternative_font_registry_alist);
6407 defsubr (&Sinternal_make_lisp_face);
6408 defsubr (&Sinternal_lisp_face_p);
6409 defsubr (&Sinternal_set_lisp_face_attribute);
6410 #ifdef HAVE_WINDOW_SYSTEM
6411 defsubr (&Sinternal_set_lisp_face_attribute_from_resource);
6412 #endif
6413 defsubr (&Scolor_gray_p);
6414 defsubr (&Scolor_supported_p);
6415 #ifndef HAVE_X_WINDOWS
6416 defsubr (&Sx_load_color_file);
6417 #endif
6418 defsubr (&Sface_attribute_relative_p);
6419 defsubr (&Smerge_face_attribute);
6420 defsubr (&Sinternal_get_lisp_face_attribute);
6421 defsubr (&Sinternal_lisp_face_attribute_values);
6422 defsubr (&Sinternal_lisp_face_equal_p);
6423 defsubr (&Sinternal_lisp_face_empty_p);
6424 defsubr (&Sinternal_copy_lisp_face);
6425 defsubr (&Sinternal_merge_in_global_face);
6426 defsubr (&Sface_font);
6427 defsubr (&Sframe_face_alist);
6428 defsubr (&Sdisplay_supports_face_attributes_p);
6429 defsubr (&Scolor_distance);
6430 defsubr (&Sinternal_set_font_selection_order);
6431 defsubr (&Sinternal_set_alternative_font_family_alist);
6432 defsubr (&Sinternal_set_alternative_font_registry_alist);
6433 defsubr (&Sface_attributes_as_vector);
6434 #ifdef GLYPH_DEBUG
6435 defsubr (&Sdump_face);
6436 defsubr (&Sshow_face_resources);
6437 #endif /* GLYPH_DEBUG */
6438 defsubr (&Sclear_face_cache);
6439 defsubr (&Stty_suppress_bold_inverse_default_colors);
6441 #if defined DEBUG_X_COLORS && defined HAVE_X_WINDOWS
6442 defsubr (&Sdump_colors);
6443 #endif
6445 DEFVAR_LISP ("face-new-frame-defaults", Vface_new_frame_defaults,
6446 doc: /* List of global face definitions (for internal use only.) */);
6447 Vface_new_frame_defaults = Qnil;
6449 DEFVAR_LISP ("face-default-stipple", Vface_default_stipple,
6450 doc: /* Default stipple pattern used on monochrome displays.
6451 This stipple pattern is used on monochrome displays
6452 instead of shades of gray for a face background color.
6453 See `set-face-stipple' for possible values for this variable. */);
6454 Vface_default_stipple = build_pure_c_string ("gray3");
6456 DEFVAR_LISP ("tty-defined-color-alist", Vtty_defined_color_alist,
6457 doc: /* An alist of defined terminal colors and their RGB values.
6458 See the docstring of `tty-color-alist' for the details. */);
6459 Vtty_defined_color_alist = Qnil;
6461 DEFVAR_LISP ("scalable-fonts-allowed", Vscalable_fonts_allowed,
6462 doc: /* Allowed scalable fonts.
6463 A value of nil means don't allow any scalable fonts.
6464 A value of t means allow any scalable font.
6465 Otherwise, value must be a list of regular expressions. A font may be
6466 scaled if its name matches a regular expression in the list.
6467 Note that if value is nil, a scalable font might still be used, if no
6468 other font of the appropriate family and registry is available. */);
6469 Vscalable_fonts_allowed = Qnil;
6471 DEFVAR_LISP ("face-ignored-fonts", Vface_ignored_fonts,
6472 doc: /* List of ignored fonts.
6473 Each element is a regular expression that matches names of fonts to
6474 ignore. */);
6475 Vface_ignored_fonts = Qnil;
6477 DEFVAR_LISP ("face-remapping-alist", Vface_remapping_alist,
6478 doc: /* Alist of face remappings.
6479 Each element is of the form:
6481 (FACE . REPLACEMENT),
6483 which causes display of the face FACE to use REPLACEMENT instead.
6484 REPLACEMENT is a face specification, i.e. one of the following:
6486 (1) a face name
6487 (2) a property list of attribute/value pairs, or
6488 (3) a list in which each element has the form of (1) or (2).
6490 List values for REPLACEMENT are merged to form the final face
6491 specification, with earlier entries taking precedence, in the same as
6492 as in the `face' text property.
6494 Face-name remapping cycles are suppressed; recursive references use
6495 the underlying face instead of the remapped face. So a remapping of
6496 the form:
6498 (FACE EXTRA-FACE... FACE)
6502 (FACE (FACE-ATTR VAL ...) FACE)
6504 causes EXTRA-FACE... or (FACE-ATTR VAL ...) to be _merged_ with the
6505 existing definition of FACE. Note that this isn't necessary for the
6506 default face, since every face inherits from the default face.
6508 If this variable is made buffer-local, the face remapping takes effect
6509 only in that buffer. For instance, the mode my-mode could define a
6510 face `my-mode-default', and then in the mode setup function, do:
6512 (set (make-local-variable \\='face-remapping-alist)
6513 \\='((default my-mode-default)))).
6515 Because Emacs normally only redraws screen areas when the underlying
6516 buffer contents change, you may need to call `redraw-display' after
6517 changing this variable for it to take effect. */);
6518 Vface_remapping_alist = Qnil;
6520 DEFVAR_LISP ("face-font-rescale-alist", Vface_font_rescale_alist,
6521 doc: /* Alist of fonts vs the rescaling factors.
6522 Each element is a cons (FONT-PATTERN . RESCALE-RATIO), where
6523 FONT-PATTERN is a font-spec or a regular expression matching a font name, and
6524 RESCALE-RATIO is a floating point number to specify how much larger
6525 \(or smaller) font we should use. For instance, if a face requests
6526 a font of 10 point, we actually use a font of 10 * RESCALE-RATIO point. */);
6527 Vface_font_rescale_alist = Qnil;
6529 #ifdef HAVE_WINDOW_SYSTEM
6530 defsubr (&Sbitmap_spec_p);
6531 defsubr (&Sx_list_fonts);
6532 defsubr (&Sinternal_face_x_get_resource);
6533 defsubr (&Sx_family_fonts);
6534 #endif