; * admin/MAINTAINERS: Add an entry for Xue Fuqiao.
[emacs.git] / src / xfaces.c
blob40713f167ffc5e4a62b29cfd6a81c3c4286391e9
1 /* xfaces.c -- "Face" primitives.
3 Copyright (C) 1993-1994, 1998-2015 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* New face implementation by Gerd Moellmann <gerd@gnu.org>. */
22 /* Faces.
24 When using Emacs with X, the display style of characters can be
25 changed by defining `faces'. Each face can specify the following
26 display attributes:
28 1. Font family name.
30 2. Font foundry name.
32 3. Relative proportionate width, aka character set width or set
33 width (swidth), e.g. `semi-compressed'.
35 4. Font height in 1/10pt.
37 5. Font weight, e.g. `bold'.
39 6. Font slant, e.g. `italic'.
41 7. Foreground color.
43 8. Background color.
45 9. Whether or not characters should be underlined, and in what color.
47 10. Whether or not characters should be displayed in inverse video.
49 11. A background stipple, a bitmap.
51 12. Whether or not characters should be overlined, and in what color.
53 13. Whether or not characters should be strike-through, and in what
54 color.
56 14. Whether or not a box should be drawn around characters, the box
57 type, and, for simple boxes, in what color.
59 15. Font-spec, or nil. This is a special attribute.
61 A font-spec is a collection of font attributes (specs).
63 When this attribute is specified, the face uses a font matching
64 with the specs as is except for what overwritten by the specs in
65 the fontset (see below). In addition, the other font-related
66 attributes (1st thru 5th) are updated from the spec.
68 On the other hand, if one of the other font-related attributes are
69 specified, the corresponding specs in this attribute is set to nil.
71 15. A face name or list of face names from which to inherit attributes.
73 16. A specified average font width, which is invisible from Lisp,
74 and is used to ensure that a font specified on the command line,
75 for example, can be matched exactly.
77 17. A fontset name. This is another special attribute.
79 A fontset is a mappings from characters to font-specs, and the
80 specs overwrite the font-spec in the 14th attribute.
83 Faces are frame-local by nature because Emacs allows to define the
84 same named face (face names are symbols) differently for different
85 frames. Each frame has an alist of face definitions for all named
86 faces. The value of a named face in such an alist is a Lisp vector
87 with the symbol `face' in slot 0, and a slot for each of the face
88 attributes mentioned above.
90 There is also a global face alist `Vface_new_frame_defaults'. Face
91 definitions from this list are used to initialize faces of newly
92 created frames.
94 A face doesn't have to specify all attributes. Those not specified
95 have a value of `unspecified'. Faces specifying all attributes but
96 the 14th are called `fully-specified'.
99 Face merging.
101 The display style of a given character in the text is determined by
102 combining several faces. This process is called `face merging'.
103 Any aspect of the display style that isn't specified by overlays or
104 text properties is taken from the `default' face. Since it is made
105 sure that the default face is always fully-specified, face merging
106 always results in a fully-specified face.
109 Face realization.
111 After all face attributes for a character have been determined by
112 merging faces of that character, that face is `realized'. The
113 realization process maps face attributes to what is physically
114 available on the system where Emacs runs. The result is a
115 `realized face' in the form of a struct face which is stored in the
116 face cache of the frame on which it was realized.
118 Face realization is done in the context of the character to display
119 because different fonts may be used for different characters. In
120 other words, for characters that have different font
121 specifications, different realized faces are needed to display
122 them.
124 Font specification is done by fontsets. See the comment in
125 fontset.c for the details. In the current implementation, all ASCII
126 characters share the same font in a fontset.
128 Faces are at first realized for ASCII characters, and, at that
129 time, assigned a specific realized fontset. Hereafter, we call
130 such a face as `ASCII face'. When a face for a multibyte character
131 is realized, it inherits (thus shares) a fontset of an ASCII face
132 that has the same attributes other than font-related ones.
134 Thus, all realized faces have a realized fontset.
137 Unibyte text.
139 Unibyte text (i.e. raw 8-bit characters) is displayed with the same
140 font as ASCII characters. That is because it is expected that
141 unibyte text users specify a font that is suitable both for ASCII
142 and raw 8-bit characters.
145 Font selection.
147 Font selection tries to find the best available matching font for a
148 given (character, face) combination.
150 If the face specifies a fontset name, that fontset determines a
151 pattern for fonts of the given character. If the face specifies a
152 font name or the other font-related attributes, a fontset is
153 realized from the default fontset. In that case, that
154 specification determines a pattern for ASCII characters and the
155 default fontset determines a pattern for multibyte characters.
157 Available fonts on the system on which Emacs runs are then matched
158 against the font pattern. The result of font selection is the best
159 match for the given face attributes in this font list.
161 Font selection can be influenced by the user.
163 1. The user can specify the relative importance he gives the face
164 attributes width, height, weight, and slant by setting
165 face-font-selection-order (faces.el) to a list of face attribute
166 names. The default is '(:width :height :weight :slant), and means
167 that font selection first tries to find a good match for the font
168 width specified by a face, then---within fonts with that
169 width---tries to find a best match for the specified font height,
170 etc.
172 2. Setting face-font-family-alternatives allows the user to
173 specify alternative font families to try if a family specified by a
174 face doesn't exist.
176 3. Setting face-font-registry-alternatives allows the user to
177 specify all alternative font registries to try for a face
178 specifying a registry.
180 4. Setting face-ignored-fonts allows the user to ignore specific
181 fonts.
184 Character composition.
186 Usually, the realization process is already finished when Emacs
187 actually reflects the desired glyph matrix on the screen. However,
188 on displaying a composition (sequence of characters to be composed
189 on the screen), a suitable font for the components of the
190 composition is selected and realized while drawing them on the
191 screen, i.e. the realization process is delayed but in principle
192 the same.
195 Initialization of basic faces.
197 The faces `default', `modeline' are considered `basic faces'.
198 When redisplay happens the first time for a newly created frame,
199 basic faces are realized for CHARSET_ASCII. Frame parameters are
200 used to fill in unspecified attributes of the default face. */
202 #include <config.h>
203 #include "sysstdio.h"
204 #include <sys/types.h>
205 #include <sys/stat.h>
207 #include "lisp.h"
208 #include "character.h"
209 #include "charset.h"
210 #include "keyboard.h"
211 #include "frame.h"
212 #include "termhooks.h"
214 #ifdef USE_MOTIF
215 #include <Xm/Xm.h>
216 #include <Xm/XmStrDefs.h>
217 #endif /* USE_MOTIF */
219 #ifdef MSDOS
220 #include "dosfns.h"
221 #endif
223 #ifdef HAVE_WINDOW_SYSTEM
224 #include TERM_HEADER
225 #include "fontset.h"
226 #ifdef HAVE_NTGUI
227 #define x_display_info w32_display_info
228 #define GCGraphicsExposures 0
229 #endif /* HAVE_NTGUI */
231 #ifdef HAVE_NS
232 #define GCGraphicsExposures 0
233 #endif /* HAVE_NS */
234 #endif /* HAVE_WINDOW_SYSTEM */
236 #include "buffer.h"
237 #include "dispextern.h"
238 #include "blockinput.h"
239 #include "window.h"
240 #include "intervals.h"
241 #include "termchar.h"
243 #include "font.h"
245 #ifdef HAVE_X_WINDOWS
247 /* Compensate for a bug in Xos.h on some systems, on which it requires
248 time.h. On some such systems, Xos.h tries to redefine struct
249 timeval and struct timezone if USG is #defined while it is
250 #included. */
252 #ifdef XOS_NEEDS_TIME_H
253 #include <time.h>
254 #undef USG
255 #include <X11/Xos.h>
256 #define USG
257 #define __TIMEVAL__
258 #if defined USG || defined __TIMEVAL__ /* Don't warn about unused macros. */
259 #endif
260 #else /* not XOS_NEEDS_TIME_H */
261 #include <X11/Xos.h>
262 #endif /* not XOS_NEEDS_TIME_H */
264 #endif /* HAVE_X_WINDOWS */
266 #include <c-ctype.h>
268 /* True if face attribute ATTR is unspecified. */
270 #define UNSPECIFIEDP(ATTR) EQ ((ATTR), Qunspecified)
272 /* True if face attribute ATTR is `ignore-defface'. */
274 #define IGNORE_DEFFACE_P(ATTR) EQ ((ATTR), QCignore_defface)
276 /* Size of hash table of realized faces in face caches (should be a
277 prime number). */
279 #define FACE_CACHE_BUCKETS_SIZE 1001
281 char unspecified_fg[] = "unspecified-fg", unspecified_bg[] = "unspecified-bg";
283 /* Alist of alternative font families. Each element is of the form
284 (FAMILY FAMILY1 FAMILY2 ...). If fonts of FAMILY can't be loaded,
285 try FAMILY1, then FAMILY2, ... */
287 Lisp_Object Vface_alternative_font_family_alist;
289 /* Alist of alternative font registries. Each element is of the form
290 (REGISTRY REGISTRY1 REGISTRY2...). If fonts of REGISTRY can't be
291 loaded, try REGISTRY1, then REGISTRY2, ... */
293 Lisp_Object Vface_alternative_font_registry_alist;
295 /* The next ID to assign to Lisp faces. */
297 static int next_lface_id;
299 /* A vector mapping Lisp face Id's to face names. */
301 static Lisp_Object *lface_id_to_name;
302 static ptrdiff_t lface_id_to_name_size;
304 #ifdef HAVE_WINDOW_SYSTEM
306 /* Counter for calls to clear_face_cache. If this counter reaches
307 CLEAR_FONT_TABLE_COUNT, and a frame has more than
308 CLEAR_FONT_TABLE_NFONTS load, unused fonts are freed. */
310 static int clear_font_table_count;
311 #define CLEAR_FONT_TABLE_COUNT 100
312 #define CLEAR_FONT_TABLE_NFONTS 10
314 #endif /* HAVE_WINDOW_SYSTEM */
316 /* True means face attributes have been changed since the last
317 redisplay. Used in redisplay_internal. */
319 bool face_change;
321 /* True means don't display bold text if a face's foreground
322 and background colors are the inverse of the default colors of the
323 display. This is a kluge to suppress `bold black' foreground text
324 which is hard to read on an LCD monitor. */
326 static bool tty_suppress_bold_inverse_default_colors_p;
328 /* A list of the form `((x . y))' used to avoid consing in
329 Finternal_set_lisp_face_attribute. */
331 static Lisp_Object Vparam_value_alist;
333 /* The total number of colors currently allocated. */
335 #ifdef GLYPH_DEBUG
336 static int ncolors_allocated;
337 static int npixmaps_allocated;
338 static int ngcs;
339 #endif
341 /* True means the definition of the `menu' face for new frames has
342 been changed. */
344 static bool menu_face_changed_default;
346 struct named_merge_point;
348 static struct face *realize_face (struct face_cache *, Lisp_Object *,
349 int);
350 static struct face *realize_x_face (struct face_cache *, Lisp_Object *);
351 static struct face *realize_tty_face (struct face_cache *, Lisp_Object *);
352 static bool realize_basic_faces (struct frame *);
353 static bool realize_default_face (struct frame *);
354 static void realize_named_face (struct frame *, Lisp_Object, int);
355 static struct face_cache *make_face_cache (struct frame *);
356 static void free_face_cache (struct face_cache *);
357 static bool merge_face_ref (struct frame *, Lisp_Object, Lisp_Object *,
358 bool, struct named_merge_point *);
359 static int color_distance (XColor *x, XColor *y);
361 #ifdef HAVE_WINDOW_SYSTEM
362 static void set_font_frame_param (Lisp_Object, Lisp_Object);
363 static void clear_face_gcs (struct face_cache *);
364 static struct face *realize_non_ascii_face (struct frame *, Lisp_Object,
365 struct face *);
366 #endif /* HAVE_WINDOW_SYSTEM */
368 /***********************************************************************
369 Utilities
370 ***********************************************************************/
372 #ifdef HAVE_X_WINDOWS
374 #ifdef DEBUG_X_COLORS
376 /* The following is a poor mans infrastructure for debugging X color
377 allocation problems on displays with PseudoColor-8. Some X servers
378 like 3.3.5 XF86_SVGA with Matrox cards apparently don't implement
379 color reference counts completely so that they don't signal an
380 error when a color is freed whose reference count is already 0.
381 Other X servers do. To help me debug this, the following code
382 implements a simple reference counting schema of its own, for a
383 single display/screen. --gerd. */
385 /* Reference counts for pixel colors. */
387 int color_count[256];
389 /* Register color PIXEL as allocated. */
391 void
392 register_color (unsigned long pixel)
394 eassert (pixel < 256);
395 ++color_count[pixel];
399 /* Register color PIXEL as deallocated. */
401 void
402 unregister_color (unsigned long pixel)
404 eassert (pixel < 256);
405 if (color_count[pixel] > 0)
406 --color_count[pixel];
407 else
408 emacs_abort ();
412 /* Register N colors from PIXELS as deallocated. */
414 void
415 unregister_colors (unsigned long *pixels, int n)
417 int i;
418 for (i = 0; i < n; ++i)
419 unregister_color (pixels[i]);
423 DEFUN ("dump-colors", Fdump_colors, Sdump_colors, 0, 0, 0,
424 doc: /* Dump currently allocated colors to stderr. */)
425 (void)
427 int i, n;
429 fputc ('\n', stderr);
431 for (i = n = 0; i < ARRAYELTS (color_count); ++i)
432 if (color_count[i])
434 fprintf (stderr, "%3d: %5d", i, color_count[i]);
435 ++n;
436 if (n % 5 == 0)
437 fputc ('\n', stderr);
438 else
439 fputc ('\t', stderr);
442 if (n % 5 != 0)
443 fputc ('\n', stderr);
444 return Qnil;
447 #endif /* DEBUG_X_COLORS */
450 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
451 color values. Interrupt input must be blocked when this function
452 is called. */
454 void
455 x_free_colors (struct frame *f, unsigned long *pixels, int npixels)
457 int class = FRAME_DISPLAY_INFO (f)->visual->class;
459 /* If display has an immutable color map, freeing colors is not
460 necessary and some servers don't allow it. So don't do it. */
461 if (class != StaticColor && class != StaticGray && class != TrueColor)
463 #ifdef DEBUG_X_COLORS
464 unregister_colors (pixels, npixels);
465 #endif
466 XFreeColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
467 pixels, npixels, 0);
472 #ifdef USE_X_TOOLKIT
474 /* Free colors used on frame F. PIXELS is an array of NPIXELS pixel
475 color values. Interrupt input must be blocked when this function
476 is called. */
478 void
479 x_free_dpy_colors (Display *dpy, Screen *screen, Colormap cmap,
480 unsigned long *pixels, int npixels)
482 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
483 int class = dpyinfo->visual->class;
485 /* If display has an immutable color map, freeing colors is not
486 necessary and some servers don't allow it. So don't do it. */
487 if (class != StaticColor && class != StaticGray && class != TrueColor)
489 #ifdef DEBUG_X_COLORS
490 unregister_colors (pixels, npixels);
491 #endif
492 XFreeColors (dpy, cmap, pixels, npixels, 0);
495 #endif /* USE_X_TOOLKIT */
497 /* Create and return a GC for use on frame F. GC values and mask
498 are given by XGCV and MASK. */
500 static GC
501 x_create_gc (struct frame *f, unsigned long mask, XGCValues *xgcv)
503 GC gc;
504 block_input ();
505 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), mask, xgcv);
506 unblock_input ();
507 IF_DEBUG (++ngcs);
508 return gc;
512 /* Free GC which was used on frame F. */
514 static void
515 x_free_gc (struct frame *f, GC gc)
517 eassert (input_blocked_p ());
518 IF_DEBUG ((--ngcs, eassert (ngcs >= 0)));
519 XFreeGC (FRAME_X_DISPLAY (f), gc);
522 #endif /* HAVE_X_WINDOWS */
524 #ifdef HAVE_NTGUI
525 /* W32 emulation of GCs */
527 static GC
528 x_create_gc (struct frame *f, unsigned long mask, XGCValues *xgcv)
530 GC gc;
531 block_input ();
532 gc = XCreateGC (NULL, FRAME_W32_WINDOW (f), mask, xgcv);
533 unblock_input ();
534 IF_DEBUG (++ngcs);
535 return gc;
539 /* Free GC which was used on frame F. */
541 static void
542 x_free_gc (struct frame *f, GC gc)
544 IF_DEBUG ((--ngcs, eassert (ngcs >= 0)));
545 xfree (gc);
548 #endif /* HAVE_NTGUI */
550 #ifdef HAVE_NS
551 /* NS emulation of GCs */
553 static GC
554 x_create_gc (struct frame *f,
555 unsigned long mask,
556 XGCValues *xgcv)
558 GC gc = xmalloc (sizeof *gc);
559 *gc = *xgcv;
560 return gc;
563 static void
564 x_free_gc (struct frame *f, GC gc)
566 xfree (gc);
568 #endif /* HAVE_NS */
570 /***********************************************************************
571 Frames and faces
572 ***********************************************************************/
574 /* Initialize face cache and basic faces for frame F. */
576 void
577 init_frame_faces (struct frame *f)
579 /* Make a face cache, if F doesn't have one. */
580 if (FRAME_FACE_CACHE (f) == NULL)
581 FRAME_FACE_CACHE (f) = make_face_cache (f);
583 #ifdef HAVE_WINDOW_SYSTEM
584 /* Make the image cache. */
585 if (FRAME_WINDOW_P (f))
587 /* We initialize the image cache when creating the first frame
588 on a terminal, and not during terminal creation. This way,
589 `x-open-connection' on a tty won't create an image cache. */
590 if (FRAME_IMAGE_CACHE (f) == NULL)
591 FRAME_IMAGE_CACHE (f) = make_image_cache ();
592 ++FRAME_IMAGE_CACHE (f)->refcount;
594 #endif /* HAVE_WINDOW_SYSTEM */
596 /* Realize faces early (Bug#17889). */
597 if (!realize_basic_faces (f))
598 emacs_abort ();
602 /* Free face cache of frame F. Called from frame-dependent
603 resource freeing function, e.g. (x|tty)_free_frame_resources. */
605 void
606 free_frame_faces (struct frame *f)
608 struct face_cache *face_cache = FRAME_FACE_CACHE (f);
610 if (face_cache)
612 free_face_cache (face_cache);
613 FRAME_FACE_CACHE (f) = NULL;
616 #ifdef HAVE_WINDOW_SYSTEM
617 if (FRAME_WINDOW_P (f))
619 struct image_cache *image_cache = FRAME_IMAGE_CACHE (f);
620 if (image_cache)
622 --image_cache->refcount;
623 if (image_cache->refcount == 0)
624 free_image_cache (f);
627 #endif /* HAVE_WINDOW_SYSTEM */
631 /* Clear face caches, and recompute basic faces for frame F. Call
632 this after changing frame parameters on which those faces depend,
633 or when realized faces have been freed due to changing attributes
634 of named faces. */
636 void
637 recompute_basic_faces (struct frame *f)
639 if (FRAME_FACE_CACHE (f))
641 clear_face_cache (false);
642 if (!realize_basic_faces (f))
643 emacs_abort ();
648 /* Clear the face caches of all frames. CLEAR_FONTS_P means
649 try to free unused fonts, too. */
651 void
652 clear_face_cache (bool clear_fonts_p)
654 #ifdef HAVE_WINDOW_SYSTEM
655 Lisp_Object tail, frame;
657 if (clear_fonts_p
658 || ++clear_font_table_count == CLEAR_FONT_TABLE_COUNT)
660 /* From time to time see if we can unload some fonts. This also
661 frees all realized faces on all frames. Fonts needed by
662 faces will be loaded again when faces are realized again. */
663 clear_font_table_count = 0;
665 FOR_EACH_FRAME (tail, frame)
667 struct frame *f = XFRAME (frame);
668 if (FRAME_WINDOW_P (f)
669 && FRAME_DISPLAY_INFO (f)->n_fonts > CLEAR_FONT_TABLE_NFONTS)
671 clear_font_cache (f);
672 free_all_realized_faces (frame);
676 else
678 /* Clear GCs of realized faces. */
679 FOR_EACH_FRAME (tail, frame)
681 struct frame *f = XFRAME (frame);
682 if (FRAME_WINDOW_P (f))
683 clear_face_gcs (FRAME_FACE_CACHE (f));
685 clear_image_caches (Qnil);
687 #endif /* HAVE_WINDOW_SYSTEM */
690 DEFUN ("clear-face-cache", Fclear_face_cache, Sclear_face_cache, 0, 1, 0,
691 doc: /* Clear face caches on all frames.
692 Optional THOROUGHLY non-nil means try to free unused fonts, too. */)
693 (Lisp_Object thoroughly)
695 clear_face_cache (!NILP (thoroughly));
696 face_change = true;
697 windows_or_buffers_changed = 53;
698 return Qnil;
702 /***********************************************************************
703 X Pixmaps
704 ***********************************************************************/
706 #ifdef HAVE_WINDOW_SYSTEM
708 DEFUN ("bitmap-spec-p", Fbitmap_spec_p, Sbitmap_spec_p, 1, 1, 0,
709 doc: /* Value is non-nil if OBJECT is a valid bitmap specification.
710 A bitmap specification is either a string, a file name, or a list
711 (WIDTH HEIGHT DATA) where WIDTH is the pixel width of the bitmap,
712 HEIGHT is its height, and DATA is a string containing the bits of
713 the pixmap. Bits are stored row by row, each row occupies
714 (WIDTH + 7)/8 bytes. */)
715 (Lisp_Object object)
717 bool pixmap_p = false;
719 if (STRINGP (object))
720 /* If OBJECT is a string, it's a file name. */
721 pixmap_p = true;
722 else if (CONSP (object))
724 /* Otherwise OBJECT must be (WIDTH HEIGHT DATA), WIDTH and
725 HEIGHT must be ints > 0, and DATA must be string large
726 enough to hold a bitmap of the specified size. */
727 Lisp_Object width, height, data;
729 height = width = data = Qnil;
731 if (CONSP (object))
733 width = XCAR (object);
734 object = XCDR (object);
735 if (CONSP (object))
737 height = XCAR (object);
738 object = XCDR (object);
739 if (CONSP (object))
740 data = XCAR (object);
744 if (STRINGP (data)
745 && RANGED_INTEGERP (1, width, INT_MAX)
746 && RANGED_INTEGERP (1, height, INT_MAX))
748 int bytes_per_row = ((XINT (width) + BITS_PER_CHAR - 1)
749 / BITS_PER_CHAR);
750 if (XINT (height) <= SBYTES (data) / bytes_per_row)
751 pixmap_p = true;
755 return pixmap_p ? Qt : Qnil;
759 /* Load a bitmap according to NAME (which is either a file name or a
760 pixmap spec) for use on frame F. Value is the bitmap_id (see
761 xfns.c). If NAME is nil, return with a bitmap id of zero. If
762 bitmap cannot be loaded, display a message saying so, and return
763 zero. */
765 static ptrdiff_t
766 load_pixmap (struct frame *f, Lisp_Object name)
768 ptrdiff_t bitmap_id;
770 if (NILP (name))
771 return 0;
773 CHECK_TYPE (!NILP (Fbitmap_spec_p (name)), Qbitmap_spec_p, name);
775 block_input ();
776 if (CONSP (name))
778 /* Decode a bitmap spec into a bitmap. */
780 int h, w;
781 Lisp_Object bits;
783 w = XINT (Fcar (name));
784 h = XINT (Fcar (Fcdr (name)));
785 bits = Fcar (Fcdr (Fcdr (name)));
787 bitmap_id = x_create_bitmap_from_data (f, SSDATA (bits),
788 w, h);
790 else
792 /* It must be a string -- a file name. */
793 bitmap_id = x_create_bitmap_from_file (f, name);
795 unblock_input ();
797 if (bitmap_id < 0)
799 add_to_log ("Invalid or undefined bitmap `%s'", name);
800 bitmap_id = 0;
802 else
804 #ifdef GLYPH_DEBUG
805 ++npixmaps_allocated;
806 #endif
809 return bitmap_id;
812 #endif /* HAVE_WINDOW_SYSTEM */
816 /***********************************************************************
817 X Colors
818 ***********************************************************************/
820 /* Parse RGB_LIST, and fill in the RGB fields of COLOR.
821 RGB_LIST should contain (at least) 3 lisp integers.
822 Return true iff RGB_LIST is OK. */
824 static bool
825 parse_rgb_list (Lisp_Object rgb_list, XColor *color)
827 #define PARSE_RGB_LIST_FIELD(field) \
828 if (CONSP (rgb_list) && INTEGERP (XCAR (rgb_list))) \
830 color->field = XINT (XCAR (rgb_list)); \
831 rgb_list = XCDR (rgb_list); \
833 else \
834 return false;
836 PARSE_RGB_LIST_FIELD (red);
837 PARSE_RGB_LIST_FIELD (green);
838 PARSE_RGB_LIST_FIELD (blue);
840 return true;
844 /* Lookup on frame F the color described by the lisp string COLOR.
845 The resulting tty color is returned in TTY_COLOR; if STD_COLOR is
846 non-zero, then the `standard' definition of the same color is
847 returned in it. */
849 static bool
850 tty_lookup_color (struct frame *f, Lisp_Object color, XColor *tty_color,
851 XColor *std_color)
853 Lisp_Object frame, color_desc;
855 if (!STRINGP (color) || NILP (Ffboundp (Qtty_color_desc)))
856 return false;
858 XSETFRAME (frame, f);
860 color_desc = call2 (Qtty_color_desc, color, frame);
861 if (CONSP (color_desc) && CONSP (XCDR (color_desc)))
863 Lisp_Object rgb;
865 if (! INTEGERP (XCAR (XCDR (color_desc))))
866 return false;
868 tty_color->pixel = XINT (XCAR (XCDR (color_desc)));
870 rgb = XCDR (XCDR (color_desc));
871 if (! parse_rgb_list (rgb, tty_color))
872 return false;
874 /* Should we fill in STD_COLOR too? */
875 if (std_color)
877 /* Default STD_COLOR to the same as TTY_COLOR. */
878 *std_color = *tty_color;
880 /* Do a quick check to see if the returned descriptor is
881 actually _exactly_ equal to COLOR, otherwise we have to
882 lookup STD_COLOR separately. If it's impossible to lookup
883 a standard color, we just give up and use TTY_COLOR. */
884 if ((!STRINGP (XCAR (color_desc))
885 || NILP (Fstring_equal (color, XCAR (color_desc))))
886 && !NILP (Ffboundp (Qtty_color_standard_values)))
888 /* Look up STD_COLOR separately. */
889 rgb = call1 (Qtty_color_standard_values, color);
890 if (! parse_rgb_list (rgb, std_color))
891 return false;
895 return true;
897 else if (NILP (Fsymbol_value (intern ("tty-defined-color-alist"))))
898 /* We were called early during startup, and the colors are not
899 yet set up in tty-defined-color-alist. Don't return a failure
900 indication, since this produces the annoying "Unable to
901 load color" messages in the *Messages* buffer. */
902 return true;
903 else
904 /* tty-color-desc seems to have returned a bad value. */
905 return false;
908 /* A version of defined_color for non-X frames. */
910 static bool
911 tty_defined_color (struct frame *f, const char *color_name,
912 XColor *color_def, bool alloc)
914 bool status = true;
916 /* Defaults. */
917 color_def->pixel = FACE_TTY_DEFAULT_COLOR;
918 color_def->red = 0;
919 color_def->blue = 0;
920 color_def->green = 0;
922 if (*color_name)
923 status = tty_lookup_color (f, build_string (color_name), color_def, NULL);
925 if (color_def->pixel == FACE_TTY_DEFAULT_COLOR && *color_name)
927 if (strcmp (color_name, "unspecified-fg") == 0)
928 color_def->pixel = FACE_TTY_DEFAULT_FG_COLOR;
929 else if (strcmp (color_name, "unspecified-bg") == 0)
930 color_def->pixel = FACE_TTY_DEFAULT_BG_COLOR;
933 if (color_def->pixel != FACE_TTY_DEFAULT_COLOR)
934 status = true;
936 return status;
940 /* Decide if color named COLOR_NAME is valid for the display
941 associated with the frame F; if so, return the rgb values in
942 COLOR_DEF. If ALLOC, allocate a new colormap cell.
944 This does the right thing for any type of frame. */
946 static bool
947 defined_color (struct frame *f, const char *color_name, XColor *color_def,
948 bool alloc)
950 if (!FRAME_WINDOW_P (f))
951 return tty_defined_color (f, color_name, color_def, alloc);
952 #ifdef HAVE_X_WINDOWS
953 else if (FRAME_X_P (f))
954 return x_defined_color (f, color_name, color_def, alloc);
955 #endif
956 #ifdef HAVE_NTGUI
957 else if (FRAME_W32_P (f))
958 return w32_defined_color (f, color_name, color_def, alloc);
959 #endif
960 #ifdef HAVE_NS
961 else if (FRAME_NS_P (f))
962 return ns_defined_color (f, color_name, color_def, alloc, true);
963 #endif
964 else
965 emacs_abort ();
969 /* Given the index IDX of a tty color on frame F, return its name, a
970 Lisp string. */
972 Lisp_Object
973 tty_color_name (struct frame *f, int idx)
975 if (idx >= 0 && !NILP (Ffboundp (Qtty_color_by_index)))
977 Lisp_Object frame;
978 Lisp_Object coldesc;
980 XSETFRAME (frame, f);
981 coldesc = call2 (Qtty_color_by_index, make_number (idx), frame);
983 if (!NILP (coldesc))
984 return XCAR (coldesc);
986 #ifdef MSDOS
987 /* We can have an MS-DOS frame under -nw for a short window of
988 opportunity before internal_terminal_init is called. DTRT. */
989 if (FRAME_MSDOS_P (f) && !inhibit_window_system)
990 return msdos_stdcolor_name (idx);
991 #endif
993 if (idx == FACE_TTY_DEFAULT_FG_COLOR)
994 return build_string (unspecified_fg);
995 if (idx == FACE_TTY_DEFAULT_BG_COLOR)
996 return build_string (unspecified_bg);
998 return Qunspecified;
1002 /* Return true if COLOR_NAME is a shade of gray (or white or
1003 black) on frame F.
1005 The criterion implemented here is not a terribly sophisticated one. */
1007 static bool
1008 face_color_gray_p (struct frame *f, const char *color_name)
1010 XColor color;
1011 bool gray_p;
1013 if (defined_color (f, color_name, &color, false))
1014 gray_p = (/* Any color sufficiently close to black counts as gray. */
1015 (color.red < 5000 && color.green < 5000 && color.blue < 5000)
1017 ((eabs (color.red - color.green)
1018 < max (color.red, color.green) / 20)
1019 && (eabs (color.green - color.blue)
1020 < max (color.green, color.blue) / 20)
1021 && (eabs (color.blue - color.red)
1022 < max (color.blue, color.red) / 20)));
1023 else
1024 gray_p = false;
1026 return gray_p;
1030 /* Return true if color COLOR_NAME can be displayed on frame F.
1031 BACKGROUND_P means the color will be used as background color. */
1033 static bool
1034 face_color_supported_p (struct frame *f, const char *color_name,
1035 bool background_p)
1037 Lisp_Object frame;
1038 XColor not_used;
1040 XSETFRAME (frame, f);
1041 return
1042 #ifdef HAVE_WINDOW_SYSTEM
1043 FRAME_WINDOW_P (f)
1044 ? (!NILP (Fxw_display_color_p (frame))
1045 || xstrcasecmp (color_name, "black") == 0
1046 || xstrcasecmp (color_name, "white") == 0
1047 || (background_p
1048 && face_color_gray_p (f, color_name))
1049 || (!NILP (Fx_display_grayscale_p (frame))
1050 && face_color_gray_p (f, color_name)))
1052 #endif
1053 tty_defined_color (f, color_name, &not_used, false);
1057 DEFUN ("color-gray-p", Fcolor_gray_p, Scolor_gray_p, 1, 2, 0,
1058 doc: /* Return non-nil if COLOR is a shade of gray (or white or black).
1059 FRAME specifies the frame and thus the display for interpreting COLOR.
1060 If FRAME is nil or omitted, use the selected frame. */)
1061 (Lisp_Object color, Lisp_Object frame)
1063 CHECK_STRING (color);
1064 return (face_color_gray_p (decode_any_frame (frame), SSDATA (color))
1065 ? Qt : Qnil);
1069 DEFUN ("color-supported-p", Fcolor_supported_p,
1070 Scolor_supported_p, 1, 3, 0,
1071 doc: /* Return non-nil if COLOR can be displayed on FRAME.
1072 BACKGROUND-P non-nil means COLOR is used as a background.
1073 Otherwise, this function tells whether it can be used as a foreground.
1074 If FRAME is nil or omitted, use the selected frame.
1075 COLOR must be a valid color name. */)
1076 (Lisp_Object color, Lisp_Object frame, Lisp_Object background_p)
1078 CHECK_STRING (color);
1079 return (face_color_supported_p (decode_any_frame (frame),
1080 SSDATA (color), !NILP (background_p))
1081 ? Qt : Qnil);
1085 static unsigned long
1086 load_color2 (struct frame *f, struct face *face, Lisp_Object name,
1087 enum lface_attribute_index target_index, XColor *color)
1089 eassert (STRINGP (name));
1090 eassert (target_index == LFACE_FOREGROUND_INDEX
1091 || target_index == LFACE_BACKGROUND_INDEX
1092 || target_index == LFACE_UNDERLINE_INDEX
1093 || target_index == LFACE_OVERLINE_INDEX
1094 || target_index == LFACE_STRIKE_THROUGH_INDEX
1095 || target_index == LFACE_BOX_INDEX);
1097 /* if the color map is full, defined_color will return a best match
1098 to the values in an existing cell. */
1099 if (!defined_color (f, SSDATA (name), color, true))
1101 add_to_log ("Unable to load color \"%s\"", name);
1103 switch (target_index)
1105 case LFACE_FOREGROUND_INDEX:
1106 face->foreground_defaulted_p = true;
1107 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1108 break;
1110 case LFACE_BACKGROUND_INDEX:
1111 face->background_defaulted_p = true;
1112 color->pixel = FRAME_BACKGROUND_PIXEL (f);
1113 break;
1115 case LFACE_UNDERLINE_INDEX:
1116 face->underline_defaulted_p = true;
1117 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1118 break;
1120 case LFACE_OVERLINE_INDEX:
1121 face->overline_color_defaulted_p = true;
1122 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1123 break;
1125 case LFACE_STRIKE_THROUGH_INDEX:
1126 face->strike_through_color_defaulted_p = true;
1127 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1128 break;
1130 case LFACE_BOX_INDEX:
1131 face->box_color_defaulted_p = true;
1132 color->pixel = FRAME_FOREGROUND_PIXEL (f);
1133 break;
1135 default:
1136 emacs_abort ();
1139 #ifdef GLYPH_DEBUG
1140 else
1141 ++ncolors_allocated;
1142 #endif
1144 return color->pixel;
1147 /* Load color with name NAME for use by face FACE on frame F.
1148 TARGET_INDEX must be one of LFACE_FOREGROUND_INDEX,
1149 LFACE_BACKGROUND_INDEX, LFACE_UNDERLINE_INDEX, LFACE_OVERLINE_INDEX,
1150 LFACE_STRIKE_THROUGH_INDEX, or LFACE_BOX_INDEX. Value is the
1151 pixel color. If color cannot be loaded, display a message, and
1152 return the foreground, background or underline color of F, but
1153 record that fact in flags of the face so that we don't try to free
1154 these colors. */
1156 unsigned long
1157 load_color (struct frame *f, struct face *face, Lisp_Object name,
1158 enum lface_attribute_index target_index)
1160 XColor color;
1161 return load_color2 (f, face, name, target_index, &color);
1165 #ifdef HAVE_WINDOW_SYSTEM
1167 #define NEAR_SAME_COLOR_THRESHOLD 30000
1169 /* Load colors for face FACE which is used on frame F. Colors are
1170 specified by slots LFACE_BACKGROUND_INDEX and LFACE_FOREGROUND_INDEX
1171 of ATTRS. If the background color specified is not supported on F,
1172 try to emulate gray colors with a stipple from Vface_default_stipple. */
1174 static void
1175 load_face_colors (struct frame *f, struct face *face,
1176 Lisp_Object attrs[LFACE_VECTOR_SIZE])
1178 Lisp_Object fg, bg, dfg;
1179 XColor xfg, xbg;
1181 bg = attrs[LFACE_BACKGROUND_INDEX];
1182 fg = attrs[LFACE_FOREGROUND_INDEX];
1184 /* Swap colors if face is inverse-video. */
1185 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1187 Lisp_Object tmp;
1188 tmp = fg;
1189 fg = bg;
1190 bg = tmp;
1193 /* Check for support for foreground, not for background because
1194 face_color_supported_p is smart enough to know that grays are
1195 "supported" as background because we are supposed to use stipple
1196 for them. */
1197 if (!face_color_supported_p (f, SSDATA (bg), false)
1198 && !NILP (Fbitmap_spec_p (Vface_default_stipple)))
1200 x_destroy_bitmap (f, face->stipple);
1201 face->stipple = load_pixmap (f, Vface_default_stipple);
1204 face->background = load_color2 (f, face, bg, LFACE_BACKGROUND_INDEX, &xbg);
1205 face->foreground = load_color2 (f, face, fg, LFACE_FOREGROUND_INDEX, &xfg);
1207 dfg = attrs[LFACE_DISTANT_FOREGROUND_INDEX];
1208 if (!NILP (dfg) && !UNSPECIFIEDP (dfg)
1209 && color_distance (&xbg, &xfg) < NEAR_SAME_COLOR_THRESHOLD)
1211 if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
1212 face->background = load_color (f, face, dfg, LFACE_BACKGROUND_INDEX);
1213 else
1214 face->foreground = load_color (f, face, dfg, LFACE_FOREGROUND_INDEX);
1218 #ifdef HAVE_X_WINDOWS
1220 /* Free color PIXEL on frame F. */
1222 void
1223 unload_color (struct frame *f, unsigned long pixel)
1225 if (pixel != -1)
1227 block_input ();
1228 x_free_colors (f, &pixel, 1);
1229 unblock_input ();
1233 /* Free colors allocated for FACE. */
1235 static void
1236 free_face_colors (struct frame *f, struct face *face)
1238 /* PENDING(NS): need to do something here? */
1240 if (face->colors_copied_bitwise_p)
1241 return;
1243 block_input ();
1245 if (!face->foreground_defaulted_p)
1247 x_free_colors (f, &face->foreground, 1);
1248 IF_DEBUG (--ncolors_allocated);
1251 if (!face->background_defaulted_p)
1253 x_free_colors (f, &face->background, 1);
1254 IF_DEBUG (--ncolors_allocated);
1257 if (face->underline_p
1258 && !face->underline_defaulted_p)
1260 x_free_colors (f, &face->underline_color, 1);
1261 IF_DEBUG (--ncolors_allocated);
1264 if (face->overline_p
1265 && !face->overline_color_defaulted_p)
1267 x_free_colors (f, &face->overline_color, 1);
1268 IF_DEBUG (--ncolors_allocated);
1271 if (face->strike_through_p
1272 && !face->strike_through_color_defaulted_p)
1274 x_free_colors (f, &face->strike_through_color, 1);
1275 IF_DEBUG (--ncolors_allocated);
1278 if (face->box != FACE_NO_BOX
1279 && !face->box_color_defaulted_p)
1281 x_free_colors (f, &face->box_color, 1);
1282 IF_DEBUG (--ncolors_allocated);
1285 unblock_input ();
1288 #endif /* HAVE_X_WINDOWS */
1290 #endif /* HAVE_WINDOW_SYSTEM */
1294 /***********************************************************************
1295 XLFD Font Names
1296 ***********************************************************************/
1298 /* An enumerator for each field of an XLFD font name. */
1300 enum xlfd_field
1302 XLFD_FOUNDRY,
1303 XLFD_FAMILY,
1304 XLFD_WEIGHT,
1305 XLFD_SLANT,
1306 XLFD_SWIDTH,
1307 XLFD_ADSTYLE,
1308 XLFD_PIXEL_SIZE,
1309 XLFD_POINT_SIZE,
1310 XLFD_RESX,
1311 XLFD_RESY,
1312 XLFD_SPACING,
1313 XLFD_AVGWIDTH,
1314 XLFD_REGISTRY,
1315 XLFD_ENCODING,
1316 XLFD_LAST
1319 /* An enumerator for each possible slant value of a font. Taken from
1320 the XLFD specification. */
1322 enum xlfd_slant
1324 XLFD_SLANT_UNKNOWN,
1325 XLFD_SLANT_ROMAN,
1326 XLFD_SLANT_ITALIC,
1327 XLFD_SLANT_OBLIQUE,
1328 XLFD_SLANT_REVERSE_ITALIC,
1329 XLFD_SLANT_REVERSE_OBLIQUE,
1330 XLFD_SLANT_OTHER
1333 /* Relative font weight according to XLFD documentation. */
1335 enum xlfd_weight
1337 XLFD_WEIGHT_UNKNOWN,
1338 XLFD_WEIGHT_ULTRA_LIGHT, /* 10 */
1339 XLFD_WEIGHT_EXTRA_LIGHT, /* 20 */
1340 XLFD_WEIGHT_LIGHT, /* 30 */
1341 XLFD_WEIGHT_SEMI_LIGHT, /* 40: SemiLight, Book, ... */
1342 XLFD_WEIGHT_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1343 XLFD_WEIGHT_SEMI_BOLD, /* 60: SemiBold, DemiBold, ... */
1344 XLFD_WEIGHT_BOLD, /* 70: Bold, ... */
1345 XLFD_WEIGHT_EXTRA_BOLD, /* 80: ExtraBold, Heavy, ... */
1346 XLFD_WEIGHT_ULTRA_BOLD /* 90: UltraBold, Black, ... */
1349 /* Relative proportionate width. */
1351 enum xlfd_swidth
1353 XLFD_SWIDTH_UNKNOWN,
1354 XLFD_SWIDTH_ULTRA_CONDENSED, /* 10 */
1355 XLFD_SWIDTH_EXTRA_CONDENSED, /* 20 */
1356 XLFD_SWIDTH_CONDENSED, /* 30: Condensed, Narrow, Compressed, ... */
1357 XLFD_SWIDTH_SEMI_CONDENSED, /* 40: semicondensed */
1358 XLFD_SWIDTH_MEDIUM, /* 50: Medium, Normal, Regular, ... */
1359 XLFD_SWIDTH_SEMI_EXPANDED, /* 60: SemiExpanded, DemiExpanded, ... */
1360 XLFD_SWIDTH_EXPANDED, /* 70: Expanded... */
1361 XLFD_SWIDTH_EXTRA_EXPANDED, /* 80: ExtraExpanded, Wide... */
1362 XLFD_SWIDTH_ULTRA_EXPANDED /* 90: UltraExpanded... */
1365 /* Order by which font selection chooses fonts. The default values
1366 mean `first, find a best match for the font width, then for the
1367 font height, then for weight, then for slant.' This variable can be
1368 set via set-face-font-sort-order. */
1370 static int font_sort_order[4];
1372 #ifdef HAVE_WINDOW_SYSTEM
1374 static enum font_property_index font_props_for_sorting[FONT_SIZE_INDEX];
1376 static int
1377 compare_fonts_by_sort_order (const void *v1, const void *v2)
1379 Lisp_Object const *p1 = v1;
1380 Lisp_Object const *p2 = v2;
1381 Lisp_Object font1 = *p1;
1382 Lisp_Object font2 = *p2;
1383 int i;
1385 for (i = 0; i < FONT_SIZE_INDEX; i++)
1387 enum font_property_index idx = font_props_for_sorting[i];
1388 Lisp_Object val1 = AREF (font1, idx), val2 = AREF (font2, idx);
1389 int result;
1391 if (idx <= FONT_REGISTRY_INDEX)
1393 if (STRINGP (val1))
1394 result = STRINGP (val2) ? strcmp (SSDATA (val1), SSDATA (val2)) : -1;
1395 else
1396 result = STRINGP (val2) ? 1 : 0;
1398 else
1400 if (INTEGERP (val1))
1401 result = (INTEGERP (val2) && XINT (val1) >= XINT (val2)
1402 ? XINT (val1) > XINT (val2)
1403 : -1);
1404 else
1405 result = INTEGERP (val2) ? 1 : 0;
1407 if (result)
1408 return result;
1410 return 0;
1413 DEFUN ("x-family-fonts", Fx_family_fonts, Sx_family_fonts, 0, 2, 0,
1414 doc: /* Return a list of available fonts of family FAMILY on FRAME.
1415 If FAMILY is omitted or nil, list all families.
1416 Otherwise, FAMILY must be a string, possibly containing wildcards
1417 `?' and `*'.
1418 If FRAME is omitted or nil, use the selected frame.
1419 Each element of the result is a vector [FAMILY WIDTH POINT-SIZE WEIGHT
1420 SLANT FIXED-P FULL REGISTRY-AND-ENCODING].
1421 FAMILY is the font family name. POINT-SIZE is the size of the
1422 font in 1/10 pt. WIDTH, WEIGHT, and SLANT are symbols describing the
1423 width, weight and slant of the font. These symbols are the same as for
1424 face attributes. FIXED-P is non-nil if the font is fixed-pitch.
1425 FULL is the full name of the font, and REGISTRY-AND-ENCODING is a string
1426 giving the registry and encoding of the font.
1427 The result list is sorted according to the current setting of
1428 the face font sort order. */)
1429 (Lisp_Object family, Lisp_Object frame)
1431 Lisp_Object font_spec, list, *drivers, vec;
1432 struct frame *f = decode_live_frame (frame);
1433 ptrdiff_t i, nfonts;
1434 EMACS_INT ndrivers;
1435 Lisp_Object result;
1436 USE_SAFE_ALLOCA;
1438 font_spec = Ffont_spec (0, NULL);
1439 if (!NILP (family))
1441 CHECK_STRING (family);
1442 font_parse_family_registry (family, Qnil, font_spec);
1445 list = font_list_entities (f, font_spec);
1446 if (NILP (list))
1447 return Qnil;
1449 /* Sort the font entities. */
1450 for (i = 0; i < 4; i++)
1451 switch (font_sort_order[i])
1453 case XLFD_SWIDTH:
1454 font_props_for_sorting[i] = FONT_WIDTH_INDEX; break;
1455 case XLFD_POINT_SIZE:
1456 font_props_for_sorting[i] = FONT_SIZE_INDEX; break;
1457 case XLFD_WEIGHT:
1458 font_props_for_sorting[i] = FONT_WEIGHT_INDEX; break;
1459 default:
1460 font_props_for_sorting[i] = FONT_SLANT_INDEX; break;
1462 font_props_for_sorting[i++] = FONT_FAMILY_INDEX;
1463 font_props_for_sorting[i++] = FONT_FOUNDRY_INDEX;
1464 font_props_for_sorting[i++] = FONT_ADSTYLE_INDEX;
1465 font_props_for_sorting[i++] = FONT_REGISTRY_INDEX;
1467 ndrivers = XINT (Flength (list));
1468 SAFE_ALLOCA_LISP (drivers, ndrivers);
1469 for (i = 0; i < ndrivers; i++, list = XCDR (list))
1470 drivers[i] = XCAR (list);
1471 vec = Fvconcat (ndrivers, drivers);
1472 nfonts = ASIZE (vec);
1474 qsort (XVECTOR (vec)->contents, nfonts, word_size,
1475 compare_fonts_by_sort_order);
1477 result = Qnil;
1478 for (i = nfonts - 1; i >= 0; --i)
1480 Lisp_Object font = AREF (vec, i);
1481 Lisp_Object v = make_uninit_vector (8);
1482 int point;
1483 Lisp_Object spacing;
1485 ASET (v, 0, AREF (font, FONT_FAMILY_INDEX));
1486 ASET (v, 1, FONT_WIDTH_SYMBOLIC (font));
1487 point = PIXEL_TO_POINT (XINT (AREF (font, FONT_SIZE_INDEX)) * 10,
1488 FRAME_RES_Y (f));
1489 ASET (v, 2, make_number (point));
1490 ASET (v, 3, FONT_WEIGHT_SYMBOLIC (font));
1491 ASET (v, 4, FONT_SLANT_SYMBOLIC (font));
1492 spacing = Ffont_get (font, QCspacing);
1493 ASET (v, 5, (NILP (spacing) || EQ (spacing, Qp)) ? Qnil : Qt);
1494 ASET (v, 6, Ffont_xlfd_name (font, Qnil));
1495 ASET (v, 7, AREF (font, FONT_REGISTRY_INDEX));
1497 result = Fcons (v, result);
1500 SAFE_FREE ();
1501 return result;
1504 DEFUN ("x-list-fonts", Fx_list_fonts, Sx_list_fonts, 1, 5, 0,
1505 doc: /* Return a list of the names of available fonts matching PATTERN.
1506 If optional arguments FACE and FRAME are specified, return only fonts
1507 the same size as FACE on FRAME.
1509 PATTERN should be a string containing a font name in the XLFD,
1510 Fontconfig, or GTK format. A font name given in the XLFD format may
1511 contain wildcard characters:
1512 the * character matches any substring, and
1513 the ? character matches any single character.
1514 PATTERN is case-insensitive.
1516 The return value is a list of strings, suitable as arguments to
1517 `set-face-font'.
1519 Fonts Emacs can't use may or may not be excluded
1520 even if they match PATTERN and FACE.
1521 The optional fourth argument MAXIMUM sets a limit on how many
1522 fonts to match. The first MAXIMUM fonts are reported.
1523 The optional fifth argument WIDTH, if specified, is a number of columns
1524 occupied by a character of a font. In that case, return only fonts
1525 the WIDTH times as wide as FACE on FRAME. */)
1526 (Lisp_Object pattern, Lisp_Object face, Lisp_Object frame,
1527 Lisp_Object maximum, Lisp_Object width)
1529 struct frame *f;
1530 int size, avgwidth IF_LINT (= 0);
1532 check_window_system (NULL);
1533 CHECK_STRING (pattern);
1535 if (! NILP (maximum))
1536 CHECK_NATNUM (maximum);
1538 if (!NILP (width))
1539 CHECK_NUMBER (width);
1541 /* We can't simply call decode_window_system_frame because
1542 this function may be called before any frame is created. */
1543 f = decode_live_frame (frame);
1544 if (! FRAME_WINDOW_P (f))
1546 /* Perhaps we have not yet created any frame. */
1547 f = NULL;
1548 frame = Qnil;
1549 face = Qnil;
1551 else
1552 XSETFRAME (frame, f);
1554 /* Determine the width standard for comparison with the fonts we find. */
1556 if (NILP (face))
1557 size = 0;
1558 else
1560 /* This is of limited utility since it works with character
1561 widths. Keep it for compatibility. --gerd. */
1562 int face_id = lookup_named_face (f, face, false);
1563 struct face *width_face = (face_id < 0
1564 ? NULL
1565 : FACE_FROM_ID (f, face_id));
1567 if (width_face && width_face->font)
1569 size = width_face->font->pixel_size;
1570 avgwidth = width_face->font->average_width;
1572 else
1574 size = FRAME_FONT (f)->pixel_size;
1575 avgwidth = FRAME_FONT (f)->average_width;
1577 if (!NILP (width))
1578 avgwidth *= XINT (width);
1581 Lisp_Object font_spec = font_spec_from_name (pattern);
1582 if (!FONTP (font_spec))
1583 signal_error ("Invalid font name", pattern);
1585 if (size)
1587 Ffont_put (font_spec, QCsize, make_number (size));
1588 Ffont_put (font_spec, QCavgwidth, make_number (avgwidth));
1590 Lisp_Object fonts = Flist_fonts (font_spec, frame, maximum, font_spec);
1591 for (Lisp_Object tail = fonts; CONSP (tail); tail = XCDR (tail))
1593 Lisp_Object font_entity;
1595 font_entity = XCAR (tail);
1596 if ((NILP (AREF (font_entity, FONT_SIZE_INDEX))
1597 || XINT (AREF (font_entity, FONT_SIZE_INDEX)) == 0)
1598 && ! NILP (AREF (font_spec, FONT_SIZE_INDEX)))
1600 /* This is a scalable font. For backward compatibility,
1601 we set the specified size. */
1602 font_entity = copy_font_spec (font_entity);
1603 ASET (font_entity, FONT_SIZE_INDEX,
1604 AREF (font_spec, FONT_SIZE_INDEX));
1606 XSETCAR (tail, Ffont_xlfd_name (font_entity, Qnil));
1608 if (NILP (frame))
1609 /* We don't have to check fontsets. */
1610 return fonts;
1611 Lisp_Object fontsets = list_fontsets (f, pattern, size);
1612 return CALLN (Fnconc, fonts, fontsets);
1615 #endif /* HAVE_WINDOW_SYSTEM */
1618 /***********************************************************************
1619 Lisp Faces
1620 ***********************************************************************/
1622 /* Access face attributes of face LFACE, a Lisp vector. */
1624 #define LFACE_FAMILY(LFACE) AREF ((LFACE), LFACE_FAMILY_INDEX)
1625 #define LFACE_FOUNDRY(LFACE) AREF ((LFACE), LFACE_FOUNDRY_INDEX)
1626 #define LFACE_HEIGHT(LFACE) AREF ((LFACE), LFACE_HEIGHT_INDEX)
1627 #define LFACE_WEIGHT(LFACE) AREF ((LFACE), LFACE_WEIGHT_INDEX)
1628 #define LFACE_SLANT(LFACE) AREF ((LFACE), LFACE_SLANT_INDEX)
1629 #define LFACE_UNDERLINE(LFACE) AREF ((LFACE), LFACE_UNDERLINE_INDEX)
1630 #define LFACE_INVERSE(LFACE) AREF ((LFACE), LFACE_INVERSE_INDEX)
1631 #define LFACE_FOREGROUND(LFACE) AREF ((LFACE), LFACE_FOREGROUND_INDEX)
1632 #define LFACE_BACKGROUND(LFACE) AREF ((LFACE), LFACE_BACKGROUND_INDEX)
1633 #define LFACE_STIPPLE(LFACE) AREF ((LFACE), LFACE_STIPPLE_INDEX)
1634 #define LFACE_SWIDTH(LFACE) AREF ((LFACE), LFACE_SWIDTH_INDEX)
1635 #define LFACE_OVERLINE(LFACE) AREF ((LFACE), LFACE_OVERLINE_INDEX)
1636 #define LFACE_STRIKE_THROUGH(LFACE) AREF ((LFACE), LFACE_STRIKE_THROUGH_INDEX)
1637 #define LFACE_BOX(LFACE) AREF ((LFACE), LFACE_BOX_INDEX)
1638 #define LFACE_FONT(LFACE) AREF ((LFACE), LFACE_FONT_INDEX)
1639 #define LFACE_INHERIT(LFACE) AREF ((LFACE), LFACE_INHERIT_INDEX)
1640 #define LFACE_FONTSET(LFACE) AREF ((LFACE), LFACE_FONTSET_INDEX)
1641 #define LFACE_DISTANT_FOREGROUND(LFACE) \
1642 AREF ((LFACE), LFACE_DISTANT_FOREGROUND_INDEX)
1644 /* True if LFACE is a Lisp face. A Lisp face is a vector of size
1645 LFACE_VECTOR_SIZE which has the symbol `face' in slot 0. */
1647 #define LFACEP(LFACE) \
1648 (VECTORP (LFACE) \
1649 && ASIZE (LFACE) == LFACE_VECTOR_SIZE \
1650 && EQ (AREF (LFACE, 0), Qface))
1653 #ifdef GLYPH_DEBUG
1655 /* Check consistency of Lisp face attribute vector ATTRS. */
1657 static void
1658 check_lface_attrs (Lisp_Object attrs[LFACE_VECTOR_SIZE])
1660 eassert (UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
1661 || IGNORE_DEFFACE_P (attrs[LFACE_FAMILY_INDEX])
1662 || STRINGP (attrs[LFACE_FAMILY_INDEX]));
1663 eassert (UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
1664 || IGNORE_DEFFACE_P (attrs[LFACE_FOUNDRY_INDEX])
1665 || STRINGP (attrs[LFACE_FOUNDRY_INDEX]));
1666 eassert (UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
1667 || IGNORE_DEFFACE_P (attrs[LFACE_SWIDTH_INDEX])
1668 || SYMBOLP (attrs[LFACE_SWIDTH_INDEX]));
1669 eassert (UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
1670 || IGNORE_DEFFACE_P (attrs[LFACE_HEIGHT_INDEX])
1671 || NUMBERP (attrs[LFACE_HEIGHT_INDEX])
1672 || FUNCTIONP (attrs[LFACE_HEIGHT_INDEX]));
1673 eassert (UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
1674 || IGNORE_DEFFACE_P (attrs[LFACE_WEIGHT_INDEX])
1675 || SYMBOLP (attrs[LFACE_WEIGHT_INDEX]));
1676 eassert (UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
1677 || IGNORE_DEFFACE_P (attrs[LFACE_SLANT_INDEX])
1678 || SYMBOLP (attrs[LFACE_SLANT_INDEX]));
1679 eassert (UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
1680 || IGNORE_DEFFACE_P (attrs[LFACE_UNDERLINE_INDEX])
1681 || SYMBOLP (attrs[LFACE_UNDERLINE_INDEX])
1682 || STRINGP (attrs[LFACE_UNDERLINE_INDEX])
1683 || CONSP (attrs[LFACE_UNDERLINE_INDEX]));
1684 eassert (UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
1685 || IGNORE_DEFFACE_P (attrs[LFACE_OVERLINE_INDEX])
1686 || SYMBOLP (attrs[LFACE_OVERLINE_INDEX])
1687 || STRINGP (attrs[LFACE_OVERLINE_INDEX]));
1688 eassert (UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1689 || IGNORE_DEFFACE_P (attrs[LFACE_STRIKE_THROUGH_INDEX])
1690 || SYMBOLP (attrs[LFACE_STRIKE_THROUGH_INDEX])
1691 || STRINGP (attrs[LFACE_STRIKE_THROUGH_INDEX]));
1692 eassert (UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
1693 || IGNORE_DEFFACE_P (attrs[LFACE_BOX_INDEX])
1694 || SYMBOLP (attrs[LFACE_BOX_INDEX])
1695 || STRINGP (attrs[LFACE_BOX_INDEX])
1696 || INTEGERP (attrs[LFACE_BOX_INDEX])
1697 || CONSP (attrs[LFACE_BOX_INDEX]));
1698 eassert (UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
1699 || IGNORE_DEFFACE_P (attrs[LFACE_INVERSE_INDEX])
1700 || SYMBOLP (attrs[LFACE_INVERSE_INDEX]));
1701 eassert (UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
1702 || IGNORE_DEFFACE_P (attrs[LFACE_FOREGROUND_INDEX])
1703 || STRINGP (attrs[LFACE_FOREGROUND_INDEX]));
1704 eassert (UNSPECIFIEDP (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
1705 || IGNORE_DEFFACE_P (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
1706 || STRINGP (attrs[LFACE_DISTANT_FOREGROUND_INDEX]));
1707 eassert (UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
1708 || IGNORE_DEFFACE_P (attrs[LFACE_BACKGROUND_INDEX])
1709 || STRINGP (attrs[LFACE_BACKGROUND_INDEX]));
1710 eassert (UNSPECIFIEDP (attrs[LFACE_INHERIT_INDEX])
1711 || IGNORE_DEFFACE_P (attrs[LFACE_INHERIT_INDEX])
1712 || NILP (attrs[LFACE_INHERIT_INDEX])
1713 || SYMBOLP (attrs[LFACE_INHERIT_INDEX])
1714 || CONSP (attrs[LFACE_INHERIT_INDEX]));
1715 #ifdef HAVE_WINDOW_SYSTEM
1716 eassert (UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
1717 || IGNORE_DEFFACE_P (attrs[LFACE_STIPPLE_INDEX])
1718 || SYMBOLP (attrs[LFACE_STIPPLE_INDEX])
1719 || !NILP (Fbitmap_spec_p (attrs[LFACE_STIPPLE_INDEX])));
1720 eassert (UNSPECIFIEDP (attrs[LFACE_FONT_INDEX])
1721 || IGNORE_DEFFACE_P (attrs[LFACE_FONT_INDEX])
1722 || FONTP (attrs[LFACE_FONT_INDEX]));
1723 eassert (UNSPECIFIEDP (attrs[LFACE_FONTSET_INDEX])
1724 || STRINGP (attrs[LFACE_FONTSET_INDEX])
1725 || NILP (attrs[LFACE_FONTSET_INDEX]));
1726 #endif
1730 /* Check consistency of attributes of Lisp face LFACE (a Lisp vector). */
1732 static void
1733 check_lface (Lisp_Object lface)
1735 if (!NILP (lface))
1737 eassert (LFACEP (lface));
1738 check_lface_attrs (XVECTOR (lface)->contents);
1742 #else /* not GLYPH_DEBUG */
1744 #define check_lface_attrs(attrs) (void) 0
1745 #define check_lface(lface) (void) 0
1747 #endif /* GLYPH_DEBUG */
1751 /* Face-merge cycle checking. */
1753 enum named_merge_point_kind
1755 NAMED_MERGE_POINT_NORMAL,
1756 NAMED_MERGE_POINT_REMAP
1759 /* A `named merge point' is simply a point during face-merging where we
1760 look up a face by name. We keep a stack of which named lookups we're
1761 currently processing so that we can easily detect cycles, using a
1762 linked- list of struct named_merge_point structures, typically
1763 allocated on the stack frame of the named lookup functions which are
1764 active (so no consing is required). */
1765 struct named_merge_point
1767 Lisp_Object face_name;
1768 enum named_merge_point_kind named_merge_point_kind;
1769 struct named_merge_point *prev;
1773 /* If a face merging cycle is detected for FACE_NAME, return false,
1774 otherwise add NEW_NAMED_MERGE_POINT, which is initialized using
1775 FACE_NAME and NAMED_MERGE_POINT_KIND, as the head of the linked list
1776 pointed to by NAMED_MERGE_POINTS, and return true. */
1778 static bool
1779 push_named_merge_point (struct named_merge_point *new_named_merge_point,
1780 Lisp_Object face_name,
1781 enum named_merge_point_kind named_merge_point_kind,
1782 struct named_merge_point **named_merge_points)
1784 struct named_merge_point *prev;
1786 for (prev = *named_merge_points; prev; prev = prev->prev)
1787 if (EQ (face_name, prev->face_name))
1789 if (prev->named_merge_point_kind == named_merge_point_kind)
1790 /* A cycle, so fail. */
1791 return false;
1792 else if (prev->named_merge_point_kind == NAMED_MERGE_POINT_REMAP)
1793 /* A remap `hides ' any previous normal merge points
1794 (because the remap means that it's actually different face),
1795 so as we know the current merge point must be normal, we
1796 can just assume it's OK. */
1797 break;
1800 new_named_merge_point->face_name = face_name;
1801 new_named_merge_point->named_merge_point_kind = named_merge_point_kind;
1802 new_named_merge_point->prev = *named_merge_points;
1804 *named_merge_points = new_named_merge_point;
1806 return true;
1810 /* Resolve face name FACE_NAME. If FACE_NAME is a string, intern it
1811 to make it a symbol. If FACE_NAME is an alias for another face,
1812 return that face's name.
1814 Return default face in case of errors. */
1816 static Lisp_Object
1817 resolve_face_name (Lisp_Object face_name, bool signal_p)
1819 Lisp_Object orig_face;
1820 Lisp_Object tortoise, hare;
1822 if (STRINGP (face_name))
1823 face_name = Fintern (face_name, Qnil);
1825 if (NILP (face_name) || !SYMBOLP (face_name))
1826 return face_name;
1828 orig_face = face_name;
1829 tortoise = hare = face_name;
1831 while (true)
1833 face_name = hare;
1834 hare = Fget (hare, Qface_alias);
1835 if (NILP (hare) || !SYMBOLP (hare))
1836 break;
1838 face_name = hare;
1839 hare = Fget (hare, Qface_alias);
1840 if (NILP (hare) || !SYMBOLP (hare))
1841 break;
1843 tortoise = Fget (tortoise, Qface_alias);
1844 if (EQ (hare, tortoise))
1846 if (signal_p)
1847 xsignal1 (Qcircular_list, orig_face);
1848 return Qdefault;
1852 return face_name;
1856 /* Return the face definition of FACE_NAME on frame F. F null means
1857 return the definition for new frames. FACE_NAME may be a string or
1858 a symbol (apparently Emacs 20.2 allowed strings as face names in
1859 face text properties; Ediff uses that).
1860 If SIGNAL_P, signal an error if FACE_NAME is not a valid face name.
1861 Otherwise, value is nil if FACE_NAME is not a valid face name. */
1862 static Lisp_Object
1863 lface_from_face_name_no_resolve (struct frame *f, Lisp_Object face_name,
1864 bool signal_p)
1866 Lisp_Object lface;
1868 if (f)
1869 lface = assq_no_quit (face_name, f->face_alist);
1870 else
1871 lface = assq_no_quit (face_name, Vface_new_frame_defaults);
1873 if (CONSP (lface))
1874 lface = XCDR (lface);
1875 else if (signal_p)
1876 signal_error ("Invalid face", face_name);
1878 check_lface (lface);
1880 return lface;
1883 /* Return the face definition of FACE_NAME on frame F. F null means
1884 return the definition for new frames. FACE_NAME may be a string or
1885 a symbol (apparently Emacs 20.2 allowed strings as face names in
1886 face text properties; Ediff uses that). If FACE_NAME is an alias
1887 for another face, return that face's definition.
1888 If SIGNAL_P, signal an error if FACE_NAME is not a valid face name.
1889 Otherwise, value is nil if FACE_NAME is not a valid face name. */
1890 static Lisp_Object
1891 lface_from_face_name (struct frame *f, Lisp_Object face_name, bool signal_p)
1893 face_name = resolve_face_name (face_name, signal_p);
1894 return lface_from_face_name_no_resolve (f, face_name, signal_p);
1898 /* Get face attributes of face FACE_NAME from frame-local faces on
1899 frame F. Store the resulting attributes in ATTRS which must point
1900 to a vector of Lisp_Objects of size LFACE_VECTOR_SIZE.
1901 If SIGNAL_P, signal an error if FACE_NAME does not name a face.
1902 Otherwise, return true iff FACE_NAME is a face. */
1904 static bool
1905 get_lface_attributes_no_remap (struct frame *f, Lisp_Object face_name,
1906 Lisp_Object attrs[LFACE_VECTOR_SIZE],
1907 bool signal_p)
1909 Lisp_Object lface;
1911 lface = lface_from_face_name_no_resolve (f, face_name, signal_p);
1913 if (! NILP (lface))
1914 memcpy (attrs, XVECTOR (lface)->contents,
1915 LFACE_VECTOR_SIZE * sizeof *attrs);
1917 return !NILP (lface);
1920 /* Get face attributes of face FACE_NAME from frame-local faces on frame
1921 F. Store the resulting attributes in ATTRS which must point to a
1922 vector of Lisp_Objects of size LFACE_VECTOR_SIZE. If FACE_NAME is an
1923 alias for another face, use that face's definition.
1924 If SIGNAL_P, signal an error if FACE_NAME does not name a face.
1925 Otherwise, return true iff FACE_NAME is a face. */
1927 static bool
1928 get_lface_attributes (struct frame *f, Lisp_Object face_name,
1929 Lisp_Object attrs[LFACE_VECTOR_SIZE], bool signal_p,
1930 struct named_merge_point *named_merge_points)
1932 Lisp_Object face_remapping;
1934 face_name = resolve_face_name (face_name, signal_p);
1936 /* See if SYMBOL has been remapped to some other face (usually this
1937 is done buffer-locally). */
1938 face_remapping = assq_no_quit (face_name, Vface_remapping_alist);
1939 if (CONSP (face_remapping))
1941 struct named_merge_point named_merge_point;
1943 if (push_named_merge_point (&named_merge_point,
1944 face_name, NAMED_MERGE_POINT_REMAP,
1945 &named_merge_points))
1947 int i;
1949 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
1950 attrs[i] = Qunspecified;
1952 return merge_face_ref (f, XCDR (face_remapping), attrs,
1953 signal_p, named_merge_points);
1957 /* Default case, no remapping. */
1958 return get_lface_attributes_no_remap (f, face_name, attrs, signal_p);
1962 /* True iff all attributes in face attribute vector ATTRS are
1963 specified, i.e. are non-nil. */
1965 static bool
1966 lface_fully_specified_p (Lisp_Object attrs[LFACE_VECTOR_SIZE])
1968 int i;
1970 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
1971 if (i != LFACE_FONT_INDEX && i != LFACE_INHERIT_INDEX
1972 && i != LFACE_DISTANT_FOREGROUND_INDEX)
1973 if ((UNSPECIFIEDP (attrs[i]) || IGNORE_DEFFACE_P (attrs[i])))
1974 break;
1976 return i == LFACE_VECTOR_SIZE;
1979 #ifdef HAVE_WINDOW_SYSTEM
1981 /* Set font-related attributes of Lisp face LFACE from FONT-OBJECT.
1982 If FORCE_P, set only unspecified attributes of LFACE. The
1983 exception is `font' attribute. It is set to FONT_OBJECT regardless
1984 of FORCE_P. */
1986 static void
1987 set_lface_from_font (struct frame *f, Lisp_Object lface,
1988 Lisp_Object font_object, bool force_p)
1990 Lisp_Object val;
1991 struct font *font = XFONT_OBJECT (font_object);
1993 /* Set attributes only if unspecified, otherwise face defaults for
1994 new frames would never take effect. If the font doesn't have a
1995 specific property, set a normal value for that. */
1997 if (force_p || UNSPECIFIEDP (LFACE_FAMILY (lface)))
1999 Lisp_Object family = AREF (font_object, FONT_FAMILY_INDEX);
2001 ASET (lface, LFACE_FAMILY_INDEX, SYMBOL_NAME (family));
2004 if (force_p || UNSPECIFIEDP (LFACE_FOUNDRY (lface)))
2006 Lisp_Object foundry = AREF (font_object, FONT_FOUNDRY_INDEX);
2008 ASET (lface, LFACE_FOUNDRY_INDEX, SYMBOL_NAME (foundry));
2011 if (force_p || UNSPECIFIEDP (LFACE_HEIGHT (lface)))
2013 int pt = PIXEL_TO_POINT (font->pixel_size * 10, FRAME_RES_Y (f));
2015 eassert (pt > 0);
2016 ASET (lface, LFACE_HEIGHT_INDEX, make_number (pt));
2019 if (force_p || UNSPECIFIEDP (LFACE_WEIGHT (lface)))
2021 val = FONT_WEIGHT_FOR_FACE (font_object);
2022 ASET (lface, LFACE_WEIGHT_INDEX, ! NILP (val) ? val :Qnormal);
2024 if (force_p || UNSPECIFIEDP (LFACE_SLANT (lface)))
2026 val = FONT_SLANT_FOR_FACE (font_object);
2027 ASET (lface, LFACE_SLANT_INDEX, ! NILP (val) ? val : Qnormal);
2029 if (force_p || UNSPECIFIEDP (LFACE_SWIDTH (lface)))
2031 val = FONT_WIDTH_FOR_FACE (font_object);
2032 ASET (lface, LFACE_SWIDTH_INDEX, ! NILP (val) ? val : Qnormal);
2035 ASET (lface, LFACE_FONT_INDEX, font_object);
2038 #endif /* HAVE_WINDOW_SYSTEM */
2041 /* Merges the face height FROM with the face height TO, and returns the
2042 merged height. If FROM is an invalid height, then INVALID is
2043 returned instead. FROM and TO may be either absolute face heights or
2044 `relative' heights; the returned value is always an absolute height
2045 unless both FROM and TO are relative. */
2047 static Lisp_Object
2048 merge_face_heights (Lisp_Object from, Lisp_Object to, Lisp_Object invalid)
2050 Lisp_Object result = invalid;
2052 if (INTEGERP (from))
2053 /* FROM is absolute, just use it as is. */
2054 result = from;
2055 else if (FLOATP (from))
2056 /* FROM is a scale, use it to adjust TO. */
2058 if (INTEGERP (to))
2059 /* relative X absolute => absolute */
2060 result = make_number (XFLOAT_DATA (from) * XINT (to));
2061 else if (FLOATP (to))
2062 /* relative X relative => relative */
2063 result = make_float (XFLOAT_DATA (from) * XFLOAT_DATA (to));
2064 else if (UNSPECIFIEDP (to))
2065 result = from;
2067 else if (FUNCTIONP (from))
2068 /* FROM is a function, which use to adjust TO. */
2070 /* Call function with current height as argument.
2071 From is the new height. */
2072 result = safe_call1 (from, to);
2074 /* Ensure that if TO was absolute, so is the result. */
2075 if (INTEGERP (to) && !INTEGERP (result))
2076 result = invalid;
2079 return result;
2083 /* Merge two Lisp face attribute vectors on frame F, FROM and TO, and
2084 store the resulting attributes in TO, which must be already be
2085 completely specified and contain only absolute attributes. Every
2086 specified attribute of FROM overrides the corresponding attribute of
2087 TO; relative attributes in FROM are merged with the absolute value in
2088 TO and replace it. NAMED_MERGE_POINTS is used internally to detect
2089 loops in face inheritance/remapping; it should be 0 when called from
2090 other places. */
2092 static void
2093 merge_face_vectors (struct frame *f, Lisp_Object *from, Lisp_Object *to,
2094 struct named_merge_point *named_merge_points)
2096 int i;
2097 Lisp_Object font = Qnil;
2099 /* If FROM inherits from some other faces, merge their attributes into
2100 TO before merging FROM's direct attributes. Note that an :inherit
2101 attribute of `unspecified' is the same as one of nil; we never
2102 merge :inherit attributes, so nil is more correct, but lots of
2103 other code uses `unspecified' as a generic value for face attributes. */
2104 if (!UNSPECIFIEDP (from[LFACE_INHERIT_INDEX])
2105 && !NILP (from[LFACE_INHERIT_INDEX]))
2106 merge_face_ref (f, from[LFACE_INHERIT_INDEX], to, false, named_merge_points);
2108 if (FONT_SPEC_P (from[LFACE_FONT_INDEX]))
2110 if (!UNSPECIFIEDP (to[LFACE_FONT_INDEX]))
2111 font = merge_font_spec (from[LFACE_FONT_INDEX], to[LFACE_FONT_INDEX]);
2112 else
2113 font = copy_font_spec (from[LFACE_FONT_INDEX]);
2114 to[LFACE_FONT_INDEX] = font;
2117 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2118 if (!UNSPECIFIEDP (from[i]))
2120 if (i == LFACE_HEIGHT_INDEX && !INTEGERP (from[i]))
2122 to[i] = merge_face_heights (from[i], to[i], to[i]);
2123 font_clear_prop (to, FONT_SIZE_INDEX);
2125 else if (i != LFACE_FONT_INDEX && ! EQ (to[i], from[i]))
2127 to[i] = from[i];
2128 if (i >= LFACE_FAMILY_INDEX && i <=LFACE_SLANT_INDEX)
2129 font_clear_prop (to,
2130 (i == LFACE_FAMILY_INDEX ? FONT_FAMILY_INDEX
2131 : i == LFACE_FOUNDRY_INDEX ? FONT_FOUNDRY_INDEX
2132 : i == LFACE_SWIDTH_INDEX ? FONT_WIDTH_INDEX
2133 : i == LFACE_HEIGHT_INDEX ? FONT_SIZE_INDEX
2134 : i == LFACE_WEIGHT_INDEX ? FONT_WEIGHT_INDEX
2135 : FONT_SLANT_INDEX));
2139 /* If FROM specifies a font spec, make its contents take precedence
2140 over :family and other attributes. This is needed for face
2141 remapping using :font to work. */
2143 if (!NILP (font))
2145 if (! NILP (AREF (font, FONT_FOUNDRY_INDEX)))
2146 to[LFACE_FOUNDRY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FOUNDRY_INDEX));
2147 if (! NILP (AREF (font, FONT_FAMILY_INDEX)))
2148 to[LFACE_FAMILY_INDEX] = SYMBOL_NAME (AREF (font, FONT_FAMILY_INDEX));
2149 if (! NILP (AREF (font, FONT_WEIGHT_INDEX)))
2150 to[LFACE_WEIGHT_INDEX] = FONT_WEIGHT_FOR_FACE (font);
2151 if (! NILP (AREF (font, FONT_SLANT_INDEX)))
2152 to[LFACE_SLANT_INDEX] = FONT_SLANT_FOR_FACE (font);
2153 if (! NILP (AREF (font, FONT_WIDTH_INDEX)))
2154 to[LFACE_SWIDTH_INDEX] = FONT_WIDTH_FOR_FACE (font);
2155 ASET (font, FONT_SIZE_INDEX, Qnil);
2158 /* TO is always an absolute face, which should inherit from nothing.
2159 We blindly copy the :inherit attribute above and fix it up here. */
2160 to[LFACE_INHERIT_INDEX] = Qnil;
2163 /* Merge the named face FACE_NAME on frame F, into the vector of face
2164 attributes TO. Use NAMED_MERGE_POINTS to detect loops in face
2165 inheritance. Return true if FACE_NAME is a valid face name and
2166 merging succeeded. */
2168 static bool
2169 merge_named_face (struct frame *f, Lisp_Object face_name, Lisp_Object *to,
2170 struct named_merge_point *named_merge_points)
2172 struct named_merge_point named_merge_point;
2174 if (push_named_merge_point (&named_merge_point,
2175 face_name, NAMED_MERGE_POINT_NORMAL,
2176 &named_merge_points))
2178 Lisp_Object from[LFACE_VECTOR_SIZE];
2179 bool ok = get_lface_attributes (f, face_name, from, false,
2180 named_merge_points);
2182 if (ok)
2183 merge_face_vectors (f, from, to, named_merge_points);
2185 return ok;
2187 else
2188 return false;
2192 /* Merge face attributes from the lisp `face reference' FACE_REF on
2193 frame F into the face attribute vector TO. If ERR_MSGS,
2194 problems with FACE_REF cause an error message to be shown. Return
2195 true if no errors occurred (regardless of the value of ERR_MSGS).
2196 Use NAMED_MERGE_POINTS to detect loops in face inheritance or
2197 list structure; it may be 0 for most callers.
2199 FACE_REF may be a single face specification or a list of such
2200 specifications. Each face specification can be:
2202 1. A symbol or string naming a Lisp face.
2204 2. A property list of the form (KEYWORD VALUE ...) where each
2205 KEYWORD is a face attribute name, and value is an appropriate value
2206 for that attribute.
2208 3. Conses or the form (FOREGROUND-COLOR . COLOR) or
2209 (BACKGROUND-COLOR . COLOR) where COLOR is a color name. This is
2210 for compatibility with 20.2.
2212 Face specifications earlier in lists take precedence over later
2213 specifications. */
2215 static bool
2216 merge_face_ref (struct frame *f, Lisp_Object face_ref, Lisp_Object *to,
2217 bool err_msgs, struct named_merge_point *named_merge_points)
2219 bool ok = true; /* Succeed without an error? */
2221 if (CONSP (face_ref))
2223 Lisp_Object first = XCAR (face_ref);
2225 if (EQ (first, Qforeground_color)
2226 || EQ (first, Qbackground_color))
2228 /* One of (FOREGROUND-COLOR . COLOR) or (BACKGROUND-COLOR
2229 . COLOR). COLOR must be a string. */
2230 Lisp_Object color_name = XCDR (face_ref);
2231 Lisp_Object color = first;
2233 if (STRINGP (color_name))
2235 if (EQ (color, Qforeground_color))
2236 to[LFACE_FOREGROUND_INDEX] = color_name;
2237 else
2238 to[LFACE_BACKGROUND_INDEX] = color_name;
2240 else
2242 if (err_msgs)
2243 add_to_log ("Invalid face color %S", color_name);
2244 ok = false;
2247 else if (SYMBOLP (first)
2248 && *SDATA (SYMBOL_NAME (first)) == ':')
2250 /* Assume this is the property list form. */
2251 while (CONSP (face_ref) && CONSP (XCDR (face_ref)))
2253 Lisp_Object keyword = XCAR (face_ref);
2254 Lisp_Object value = XCAR (XCDR (face_ref));
2255 bool err = false;
2257 /* Specifying `unspecified' is a no-op. */
2258 if (EQ (value, Qunspecified))
2260 else if (EQ (keyword, QCfamily))
2262 if (STRINGP (value))
2264 to[LFACE_FAMILY_INDEX] = value;
2265 font_clear_prop (to, FONT_FAMILY_INDEX);
2267 else
2268 err = true;
2270 else if (EQ (keyword, QCfoundry))
2272 if (STRINGP (value))
2274 to[LFACE_FOUNDRY_INDEX] = value;
2275 font_clear_prop (to, FONT_FOUNDRY_INDEX);
2277 else
2278 err = true;
2280 else if (EQ (keyword, QCheight))
2282 Lisp_Object new_height =
2283 merge_face_heights (value, to[LFACE_HEIGHT_INDEX], Qnil);
2285 if (! NILP (new_height))
2287 to[LFACE_HEIGHT_INDEX] = new_height;
2288 font_clear_prop (to, FONT_SIZE_INDEX);
2290 else
2291 err = true;
2293 else if (EQ (keyword, QCweight))
2295 if (SYMBOLP (value) && FONT_WEIGHT_NAME_NUMERIC (value) >= 0)
2297 to[LFACE_WEIGHT_INDEX] = value;
2298 font_clear_prop (to, FONT_WEIGHT_INDEX);
2300 else
2301 err = true;
2303 else if (EQ (keyword, QCslant))
2305 if (SYMBOLP (value) && FONT_SLANT_NAME_NUMERIC (value) >= 0)
2307 to[LFACE_SLANT_INDEX] = value;
2308 font_clear_prop (to, FONT_SLANT_INDEX);
2310 else
2311 err = true;
2313 else if (EQ (keyword, QCunderline))
2315 if (EQ (value, Qt)
2316 || NILP (value)
2317 || STRINGP (value)
2318 || CONSP (value))
2319 to[LFACE_UNDERLINE_INDEX] = value;
2320 else
2321 err = true;
2323 else if (EQ (keyword, QCoverline))
2325 if (EQ (value, Qt)
2326 || NILP (value)
2327 || STRINGP (value))
2328 to[LFACE_OVERLINE_INDEX] = value;
2329 else
2330 err = true;
2332 else if (EQ (keyword, QCstrike_through))
2334 if (EQ (value, Qt)
2335 || NILP (value)
2336 || STRINGP (value))
2337 to[LFACE_STRIKE_THROUGH_INDEX] = value;
2338 else
2339 err = true;
2341 else if (EQ (keyword, QCbox))
2343 if (EQ (value, Qt))
2344 value = make_number (1);
2345 if (INTEGERP (value)
2346 || STRINGP (value)
2347 || CONSP (value)
2348 || NILP (value))
2349 to[LFACE_BOX_INDEX] = value;
2350 else
2351 err = true;
2353 else if (EQ (keyword, QCinverse_video)
2354 || EQ (keyword, QCreverse_video))
2356 if (EQ (value, Qt) || NILP (value))
2357 to[LFACE_INVERSE_INDEX] = value;
2358 else
2359 err = true;
2361 else if (EQ (keyword, QCforeground))
2363 if (STRINGP (value))
2364 to[LFACE_FOREGROUND_INDEX] = value;
2365 else
2366 err = true;
2368 else if (EQ (keyword, QCdistant_foreground))
2370 if (STRINGP (value))
2371 to[LFACE_DISTANT_FOREGROUND_INDEX] = value;
2372 else
2373 err = true;
2375 else if (EQ (keyword, QCbackground))
2377 if (STRINGP (value))
2378 to[LFACE_BACKGROUND_INDEX] = value;
2379 else
2380 err = true;
2382 else if (EQ (keyword, QCstipple))
2384 #if defined (HAVE_WINDOW_SYSTEM)
2385 Lisp_Object pixmap_p = Fbitmap_spec_p (value);
2386 if (!NILP (pixmap_p))
2387 to[LFACE_STIPPLE_INDEX] = value;
2388 else
2389 err = true;
2390 #endif /* HAVE_WINDOW_SYSTEM */
2392 else if (EQ (keyword, QCwidth))
2394 if (SYMBOLP (value) && FONT_WIDTH_NAME_NUMERIC (value) >= 0)
2396 to[LFACE_SWIDTH_INDEX] = value;
2397 font_clear_prop (to, FONT_WIDTH_INDEX);
2399 else
2400 err = true;
2402 else if (EQ (keyword, QCfont))
2404 if (FONTP (value))
2405 to[LFACE_FONT_INDEX] = value;
2406 else
2407 err = true;
2409 else if (EQ (keyword, QCinherit))
2411 /* This is not really very useful; it's just like a
2412 normal face reference. */
2413 if (! merge_face_ref (f, value, to,
2414 err_msgs, named_merge_points))
2415 err = true;
2417 else
2418 err = true;
2420 if (err)
2422 add_to_log ("Invalid face attribute %S %S", keyword, value);
2423 ok = false;
2426 face_ref = XCDR (XCDR (face_ref));
2429 else
2431 /* This is a list of face refs. Those at the beginning of the
2432 list take precedence over what follows, so we have to merge
2433 from the end backwards. */
2434 Lisp_Object next = XCDR (face_ref);
2436 if (! NILP (next))
2437 ok = merge_face_ref (f, next, to, err_msgs, named_merge_points);
2439 if (! merge_face_ref (f, first, to, err_msgs, named_merge_points))
2440 ok = false;
2443 else
2445 /* FACE_REF ought to be a face name. */
2446 ok = merge_named_face (f, face_ref, to, named_merge_points);
2447 if (!ok && err_msgs)
2448 add_to_log ("Invalid face reference: %s", face_ref);
2451 return ok;
2455 DEFUN ("internal-make-lisp-face", Finternal_make_lisp_face,
2456 Sinternal_make_lisp_face, 1, 2, 0,
2457 doc: /* Make FACE, a symbol, a Lisp face with all attributes nil.
2458 If FACE was not known as a face before, create a new one.
2459 If optional argument FRAME is specified, make a frame-local face
2460 for that frame. Otherwise operate on the global face definition.
2461 Value is a vector of face attributes. */)
2462 (Lisp_Object face, Lisp_Object frame)
2464 Lisp_Object global_lface, lface;
2465 struct frame *f;
2466 int i;
2468 CHECK_SYMBOL (face);
2469 global_lface = lface_from_face_name (NULL, face, false);
2471 if (!NILP (frame))
2473 CHECK_LIVE_FRAME (frame);
2474 f = XFRAME (frame);
2475 lface = lface_from_face_name (f, face, false);
2477 else
2478 f = NULL, lface = Qnil;
2480 /* Add a global definition if there is none. */
2481 if (NILP (global_lface))
2483 global_lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2484 Qunspecified);
2485 ASET (global_lface, 0, Qface);
2486 Vface_new_frame_defaults = Fcons (Fcons (face, global_lface),
2487 Vface_new_frame_defaults);
2489 /* Assign the new Lisp face a unique ID. The mapping from Lisp
2490 face id to Lisp face is given by the vector lface_id_to_name.
2491 The mapping from Lisp face to Lisp face id is given by the
2492 property `face' of the Lisp face name. */
2493 if (next_lface_id == lface_id_to_name_size)
2494 lface_id_to_name =
2495 xpalloc (lface_id_to_name, &lface_id_to_name_size, 1, MAX_FACE_ID,
2496 sizeof *lface_id_to_name);
2498 lface_id_to_name[next_lface_id] = face;
2499 Fput (face, Qface, make_number (next_lface_id));
2500 ++next_lface_id;
2502 else if (f == NULL)
2503 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2504 ASET (global_lface, i, Qunspecified);
2506 /* Add a frame-local definition. */
2507 if (f)
2509 if (NILP (lface))
2511 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
2512 Qunspecified);
2513 ASET (lface, 0, Qface);
2514 fset_face_alist (f, Fcons (Fcons (face, lface), f->face_alist));
2516 else
2517 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
2518 ASET (lface, i, Qunspecified);
2520 else
2521 lface = global_lface;
2523 /* Changing a named face means that all realized faces depending on
2524 that face are invalid. Since we cannot tell which realized faces
2525 depend on the face, make sure they are all removed. This is done
2526 by setting face_change. The next call to init_iterator will then
2527 free realized faces. */
2528 if (NILP (Fget (face, Qface_no_inherit)))
2530 if (f)
2531 f->face_change = 1;
2532 else
2533 face_change = true;
2534 windows_or_buffers_changed = 54;
2537 eassert (LFACEP (lface));
2538 check_lface (lface);
2539 return lface;
2543 DEFUN ("internal-lisp-face-p", Finternal_lisp_face_p,
2544 Sinternal_lisp_face_p, 1, 2, 0,
2545 doc: /* Return non-nil if FACE names a face.
2546 FACE should be a symbol or string.
2547 If optional second argument FRAME is non-nil, check for the
2548 existence of a frame-local face with name FACE on that frame.
2549 Otherwise check for the existence of a global face. */)
2550 (Lisp_Object face, Lisp_Object frame)
2552 Lisp_Object lface;
2554 face = resolve_face_name (face, true);
2556 if (!NILP (frame))
2558 CHECK_LIVE_FRAME (frame);
2559 lface = lface_from_face_name (XFRAME (frame), face, false);
2561 else
2562 lface = lface_from_face_name (NULL, face, false);
2564 return lface;
2568 DEFUN ("internal-copy-lisp-face", Finternal_copy_lisp_face,
2569 Sinternal_copy_lisp_face, 4, 4, 0,
2570 doc: /* Copy face FROM to TO.
2571 If FRAME is t, copy the global face definition of FROM.
2572 Otherwise, copy the frame-local definition of FROM on FRAME.
2573 If NEW-FRAME is a frame, copy that data into the frame-local
2574 definition of TO on NEW-FRAME. If NEW-FRAME is nil,
2575 FRAME controls where the data is copied to.
2577 The value is TO. */)
2578 (Lisp_Object from, Lisp_Object to, Lisp_Object frame, Lisp_Object new_frame)
2580 Lisp_Object lface, copy;
2581 struct frame *f;
2583 CHECK_SYMBOL (from);
2584 CHECK_SYMBOL (to);
2586 if (EQ (frame, Qt))
2588 /* Copy global definition of FROM. We don't make copies of
2589 strings etc. because 20.2 didn't do it either. */
2590 lface = lface_from_face_name (NULL, from, true);
2591 copy = Finternal_make_lisp_face (to, Qnil);
2592 f = NULL;
2594 else
2596 /* Copy frame-local definition of FROM. */
2597 if (NILP (new_frame))
2598 new_frame = frame;
2599 CHECK_LIVE_FRAME (frame);
2600 CHECK_LIVE_FRAME (new_frame);
2601 lface = lface_from_face_name (XFRAME (frame), from, true);
2602 copy = Finternal_make_lisp_face (to, new_frame);
2603 f = XFRAME (new_frame);
2606 vcopy (copy, 0, XVECTOR (lface)->contents, LFACE_VECTOR_SIZE);
2608 /* Changing a named face means that all realized faces depending on
2609 that face are invalid. Since we cannot tell which realized faces
2610 depend on the face, make sure they are all removed. This is done
2611 by setting face_change. The next call to init_iterator will then
2612 free realized faces. */
2613 if (NILP (Fget (to, Qface_no_inherit)))
2615 if (f)
2616 f->face_change = 1;
2617 else
2618 face_change = true;
2619 windows_or_buffers_changed = 55;
2622 return to;
2626 DEFUN ("internal-set-lisp-face-attribute", Finternal_set_lisp_face_attribute,
2627 Sinternal_set_lisp_face_attribute, 3, 4, 0,
2628 doc: /* Set attribute ATTR of FACE to VALUE.
2629 FRAME being a frame means change the face on that frame.
2630 FRAME nil means change the face of the selected frame.
2631 FRAME t means change the default for new frames.
2632 FRAME 0 means change the face on all frames, and change the default
2633 for new frames. */)
2634 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
2636 Lisp_Object lface;
2637 Lisp_Object old_value = Qnil;
2638 /* Set one of enum font_property_index (> 0) if ATTR is one of
2639 font-related attributes other than QCfont and QCfontset. */
2640 enum font_property_index prop_index = 0;
2641 struct frame *f;
2643 CHECK_SYMBOL (face);
2644 CHECK_SYMBOL (attr);
2646 face = resolve_face_name (face, true);
2648 /* If FRAME is 0, change face on all frames, and change the
2649 default for new frames. */
2650 if (INTEGERP (frame) && XINT (frame) == 0)
2652 Lisp_Object tail;
2653 Finternal_set_lisp_face_attribute (face, attr, value, Qt);
2654 FOR_EACH_FRAME (tail, frame)
2655 Finternal_set_lisp_face_attribute (face, attr, value, frame);
2656 return face;
2659 /* Set lface to the Lisp attribute vector of FACE. */
2660 if (EQ (frame, Qt))
2662 f = NULL;
2663 lface = lface_from_face_name (NULL, face, true);
2665 /* When updating face-new-frame-defaults, we put :ignore-defface
2666 where the caller wants `unspecified'. This forces the frame
2667 defaults to ignore the defface value. Otherwise, the defface
2668 will take effect, which is generally not what is intended.
2669 The value of that attribute will be inherited from some other
2670 face during face merging. See internal_merge_in_global_face. */
2671 if (UNSPECIFIEDP (value))
2672 value = QCignore_defface;
2674 else
2676 if (NILP (frame))
2677 frame = selected_frame;
2678 f = XFRAME (frame);
2680 CHECK_LIVE_FRAME (frame);
2681 lface = lface_from_face_name (f, face, false);
2683 /* If a frame-local face doesn't exist yet, create one. */
2684 if (NILP (lface))
2685 lface = Finternal_make_lisp_face (face, frame);
2688 if (EQ (attr, QCfamily))
2690 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2692 CHECK_STRING (value);
2693 if (SCHARS (value) == 0)
2694 signal_error ("Invalid face family", value);
2696 old_value = LFACE_FAMILY (lface);
2697 ASET (lface, LFACE_FAMILY_INDEX, value);
2698 prop_index = FONT_FAMILY_INDEX;
2700 else if (EQ (attr, QCfoundry))
2702 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2704 CHECK_STRING (value);
2705 if (SCHARS (value) == 0)
2706 signal_error ("Invalid face foundry", value);
2708 old_value = LFACE_FOUNDRY (lface);
2709 ASET (lface, LFACE_FOUNDRY_INDEX, value);
2710 prop_index = FONT_FOUNDRY_INDEX;
2712 else if (EQ (attr, QCheight))
2714 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2716 if (EQ (face, Qdefault))
2718 /* The default face must have an absolute size. */
2719 if (!INTEGERP (value) || XINT (value) <= 0)
2720 signal_error ("Default face height not absolute and positive",
2721 value);
2723 else
2725 /* For non-default faces, do a test merge with a random
2726 height to see if VALUE's ok. */
2727 Lisp_Object test = merge_face_heights (value,
2728 make_number (10),
2729 Qnil);
2730 if (!INTEGERP (test) || XINT (test) <= 0)
2731 signal_error ("Face height does not produce a positive integer",
2732 value);
2736 old_value = LFACE_HEIGHT (lface);
2737 ASET (lface, LFACE_HEIGHT_INDEX, value);
2738 prop_index = FONT_SIZE_INDEX;
2740 else if (EQ (attr, QCweight))
2742 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2744 CHECK_SYMBOL (value);
2745 if (FONT_WEIGHT_NAME_NUMERIC (value) < 0)
2746 signal_error ("Invalid face weight", value);
2748 old_value = LFACE_WEIGHT (lface);
2749 ASET (lface, LFACE_WEIGHT_INDEX, value);
2750 prop_index = FONT_WEIGHT_INDEX;
2752 else if (EQ (attr, QCslant))
2754 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2756 CHECK_SYMBOL (value);
2757 if (FONT_SLANT_NAME_NUMERIC (value) < 0)
2758 signal_error ("Invalid face slant", value);
2760 old_value = LFACE_SLANT (lface);
2761 ASET (lface, LFACE_SLANT_INDEX, value);
2762 prop_index = FONT_SLANT_INDEX;
2764 else if (EQ (attr, QCunderline))
2766 bool valid_p = false;
2768 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2769 valid_p = true;
2770 else if (NILP (value) || EQ (value, Qt))
2771 valid_p = true;
2772 else if (STRINGP (value) && SCHARS (value) > 0)
2773 valid_p = true;
2774 else if (CONSP (value))
2776 Lisp_Object key, val, list;
2778 list = value;
2779 /* FIXME? This errs on the side of acceptance. Eg it accepts:
2780 (defface foo '((t :underline 'foo) "doc")
2781 Maybe this is intentional, maybe it isn't.
2782 Non-nil symbols other than t are not documented as being valid.
2783 Eg compare with inverse-video, which explicitly rejects them.
2785 valid_p = true;
2787 while (!NILP (CAR_SAFE(list)))
2789 key = CAR_SAFE (list);
2790 list = CDR_SAFE (list);
2791 val = CAR_SAFE (list);
2792 list = CDR_SAFE (list);
2794 if (NILP (key) || NILP (val))
2796 valid_p = false;
2797 break;
2800 else if (EQ (key, QCcolor)
2801 && !(EQ (val, Qforeground_color)
2802 || (STRINGP (val) && SCHARS (val) > 0)))
2804 valid_p = false;
2805 break;
2808 else if (EQ (key, QCstyle)
2809 && !(EQ (val, Qline) || EQ (val, Qwave)))
2811 valid_p = false;
2812 break;
2817 if (!valid_p)
2818 signal_error ("Invalid face underline", value);
2820 old_value = LFACE_UNDERLINE (lface);
2821 ASET (lface, LFACE_UNDERLINE_INDEX, value);
2823 else if (EQ (attr, QCoverline))
2825 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2826 if ((SYMBOLP (value)
2827 && !EQ (value, Qt)
2828 && !EQ (value, Qnil))
2829 /* Overline color. */
2830 || (STRINGP (value)
2831 && SCHARS (value) == 0))
2832 signal_error ("Invalid face overline", value);
2834 old_value = LFACE_OVERLINE (lface);
2835 ASET (lface, LFACE_OVERLINE_INDEX, value);
2837 else if (EQ (attr, QCstrike_through))
2839 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2840 if ((SYMBOLP (value)
2841 && !EQ (value, Qt)
2842 && !EQ (value, Qnil))
2843 /* Strike-through color. */
2844 || (STRINGP (value)
2845 && SCHARS (value) == 0))
2846 signal_error ("Invalid face strike-through", value);
2848 old_value = LFACE_STRIKE_THROUGH (lface);
2849 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, value);
2851 else if (EQ (attr, QCbox))
2853 bool valid_p;
2855 /* Allow t meaning a simple box of width 1 in foreground color
2856 of the face. */
2857 if (EQ (value, Qt))
2858 value = make_number (1);
2860 if (UNSPECIFIEDP (value) || IGNORE_DEFFACE_P (value))
2861 valid_p = true;
2862 else if (NILP (value))
2863 valid_p = true;
2864 else if (INTEGERP (value))
2865 valid_p = XINT (value) != 0;
2866 else if (STRINGP (value))
2867 valid_p = SCHARS (value) > 0;
2868 else if (CONSP (value))
2870 Lisp_Object tem;
2872 tem = value;
2873 while (CONSP (tem))
2875 Lisp_Object k, v;
2877 k = XCAR (tem);
2878 tem = XCDR (tem);
2879 if (!CONSP (tem))
2880 break;
2881 v = XCAR (tem);
2882 tem = XCDR (tem);
2884 if (EQ (k, QCline_width))
2886 if (!INTEGERP (v) || XINT (v) == 0)
2887 break;
2889 else if (EQ (k, QCcolor))
2891 if (!NILP (v) && (!STRINGP (v) || SCHARS (v) == 0))
2892 break;
2894 else if (EQ (k, QCstyle))
2896 if (!EQ (v, Qpressed_button) && !EQ (v, Qreleased_button))
2897 break;
2899 else
2900 break;
2903 valid_p = NILP (tem);
2905 else
2906 valid_p = false;
2908 if (!valid_p)
2909 signal_error ("Invalid face box", value);
2911 old_value = LFACE_BOX (lface);
2912 ASET (lface, LFACE_BOX_INDEX, value);
2914 else if (EQ (attr, QCinverse_video)
2915 || EQ (attr, QCreverse_video))
2917 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2919 CHECK_SYMBOL (value);
2920 if (!EQ (value, Qt) && !NILP (value))
2921 signal_error ("Invalid inverse-video face attribute value", value);
2923 old_value = LFACE_INVERSE (lface);
2924 ASET (lface, LFACE_INVERSE_INDEX, value);
2926 else if (EQ (attr, QCforeground))
2928 /* Compatibility with 20.x. */
2929 if (NILP (value))
2930 value = Qunspecified;
2931 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2933 /* Don't check for valid color names here because it depends
2934 on the frame (display) whether the color will be valid
2935 when the face is realized. */
2936 CHECK_STRING (value);
2937 if (SCHARS (value) == 0)
2938 signal_error ("Empty foreground color value", value);
2940 old_value = LFACE_FOREGROUND (lface);
2941 ASET (lface, LFACE_FOREGROUND_INDEX, value);
2943 else if (EQ (attr, QCdistant_foreground))
2945 /* Compatibility with 20.x. */
2946 if (NILP (value))
2947 value = Qunspecified;
2948 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2950 /* Don't check for valid color names here because it depends
2951 on the frame (display) whether the color will be valid
2952 when the face is realized. */
2953 CHECK_STRING (value);
2954 if (SCHARS (value) == 0)
2955 signal_error ("Empty distant-foreground color value", value);
2957 old_value = LFACE_DISTANT_FOREGROUND (lface);
2958 ASET (lface, LFACE_DISTANT_FOREGROUND_INDEX, value);
2960 else if (EQ (attr, QCbackground))
2962 /* Compatibility with 20.x. */
2963 if (NILP (value))
2964 value = Qunspecified;
2965 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2967 /* Don't check for valid color names here because it depends
2968 on the frame (display) whether the color will be valid
2969 when the face is realized. */
2970 CHECK_STRING (value);
2971 if (SCHARS (value) == 0)
2972 signal_error ("Empty background color value", value);
2974 old_value = LFACE_BACKGROUND (lface);
2975 ASET (lface, LFACE_BACKGROUND_INDEX, value);
2977 else if (EQ (attr, QCstipple))
2979 #if defined (HAVE_WINDOW_SYSTEM)
2980 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
2981 && !NILP (value)
2982 && NILP (Fbitmap_spec_p (value)))
2983 signal_error ("Invalid stipple attribute", value);
2984 old_value = LFACE_STIPPLE (lface);
2985 ASET (lface, LFACE_STIPPLE_INDEX, value);
2986 #endif /* HAVE_WINDOW_SYSTEM */
2988 else if (EQ (attr, QCwidth))
2990 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
2992 CHECK_SYMBOL (value);
2993 if (FONT_WIDTH_NAME_NUMERIC (value) < 0)
2994 signal_error ("Invalid face width", value);
2996 old_value = LFACE_SWIDTH (lface);
2997 ASET (lface, LFACE_SWIDTH_INDEX, value);
2998 prop_index = FONT_WIDTH_INDEX;
3000 else if (EQ (attr, QCfont))
3002 #ifdef HAVE_WINDOW_SYSTEM
3003 if (EQ (frame, Qt) || FRAME_WINDOW_P (f))
3005 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value))
3007 struct frame *f1;
3009 old_value = LFACE_FONT (lface);
3010 if (! FONTP (value))
3012 if (STRINGP (value))
3014 Lisp_Object name = value;
3015 int fontset = fs_query_fontset (name, 0);
3017 if (fontset >= 0)
3018 name = fontset_ascii (fontset);
3019 value = font_spec_from_name (name);
3020 if (!FONTP (value))
3021 signal_error ("Invalid font name", name);
3023 else
3024 signal_error ("Invalid font or font-spec", value);
3026 if (EQ (frame, Qt))
3027 f1 = XFRAME (selected_frame);
3028 else
3029 f1 = XFRAME (frame);
3031 /* FIXME:
3032 If frame is t, and selected frame is a tty frame, the font
3033 can't be realized. An improvement would be to loop over frames
3034 for a non-tty frame and use that. See discussion in Bug#18573.
3035 For a daemon, frame may be an initial frame (Bug#18869). */
3036 if (FRAME_WINDOW_P (f1))
3038 if (! FONT_OBJECT_P (value))
3040 Lisp_Object *attrs = XVECTOR (lface)->contents;
3041 Lisp_Object font_object;
3043 font_object = font_load_for_lface (f1, attrs, value);
3044 if (NILP (font_object))
3045 signal_error ("Font not available", value);
3046 value = font_object;
3048 set_lface_from_font (f1, lface, value, true);
3049 f1->face_change = 1;
3052 else
3053 ASET (lface, LFACE_FONT_INDEX, value);
3055 #endif /* HAVE_WINDOW_SYSTEM */
3057 else if (EQ (attr, QCfontset))
3059 #ifdef HAVE_WINDOW_SYSTEM
3060 if (EQ (frame, Qt) || FRAME_WINDOW_P (f))
3062 Lisp_Object tmp;
3064 old_value = LFACE_FONTSET (lface);
3065 tmp = Fquery_fontset (value, Qnil);
3066 if (NILP (tmp))
3067 signal_error ("Invalid fontset name", value);
3068 ASET (lface, LFACE_FONTSET_INDEX, value = tmp);
3070 #endif /* HAVE_WINDOW_SYSTEM */
3072 else if (EQ (attr, QCinherit))
3074 Lisp_Object tail;
3075 if (SYMBOLP (value))
3076 tail = Qnil;
3077 else
3078 for (tail = value; CONSP (tail); tail = XCDR (tail))
3079 if (!SYMBOLP (XCAR (tail)))
3080 break;
3081 if (NILP (tail))
3082 ASET (lface, LFACE_INHERIT_INDEX, value);
3083 else
3084 signal_error ("Invalid face inheritance", value);
3086 else if (EQ (attr, QCbold))
3088 old_value = LFACE_WEIGHT (lface);
3089 ASET (lface, LFACE_WEIGHT_INDEX, NILP (value) ? Qnormal : Qbold);
3090 prop_index = FONT_WEIGHT_INDEX;
3092 else if (EQ (attr, QCitalic))
3094 attr = QCslant;
3095 old_value = LFACE_SLANT (lface);
3096 ASET (lface, LFACE_SLANT_INDEX, NILP (value) ? Qnormal : Qitalic);
3097 prop_index = FONT_SLANT_INDEX;
3099 else
3100 signal_error ("Invalid face attribute name", attr);
3102 if (prop_index)
3104 /* If a font-related attribute other than QCfont and QCfontset
3105 is specified, and if the original QCfont attribute has a font
3106 (font-spec or font-object), set the corresponding property in
3107 the font to nil so that the font selector doesn't think that
3108 the attribute is mandatory. Also, clear the average
3109 width. */
3110 font_clear_prop (XVECTOR (lface)->contents, prop_index);
3113 /* Changing a named face means that all realized faces depending on
3114 that face are invalid. Since we cannot tell which realized faces
3115 depend on the face, make sure they are all removed. This is done
3116 by setting face_change. The next call to init_iterator will then
3117 free realized faces. */
3118 if (!EQ (frame, Qt)
3119 && NILP (Fget (face, Qface_no_inherit))
3120 && NILP (Fequal (old_value, value)))
3122 f->face_change = true;
3123 windows_or_buffers_changed = 56;
3126 if (!UNSPECIFIEDP (value) && !IGNORE_DEFFACE_P (value)
3127 && NILP (Fequal (old_value, value)))
3129 Lisp_Object param;
3131 param = Qnil;
3133 if (EQ (face, Qdefault))
3135 #ifdef HAVE_WINDOW_SYSTEM
3136 /* Changed font-related attributes of the `default' face are
3137 reflected in changed `font' frame parameters. */
3138 if (FRAMEP (frame)
3139 && (prop_index || EQ (attr, QCfont))
3140 && lface_fully_specified_p (XVECTOR (lface)->contents))
3141 set_font_frame_param (frame, lface);
3142 else
3143 #endif /* HAVE_WINDOW_SYSTEM */
3145 if (EQ (attr, QCforeground))
3146 param = Qforeground_color;
3147 else if (EQ (attr, QCbackground))
3148 param = Qbackground_color;
3150 #ifdef HAVE_WINDOW_SYSTEM
3151 #ifndef HAVE_NTGUI
3152 else if (EQ (face, Qscroll_bar))
3154 /* Changing the colors of `scroll-bar' sets frame parameters
3155 `scroll-bar-foreground' and `scroll-bar-background'. */
3156 if (EQ (attr, QCforeground))
3157 param = Qscroll_bar_foreground;
3158 else if (EQ (attr, QCbackground))
3159 param = Qscroll_bar_background;
3161 #endif /* not HAVE_NTGUI */
3162 else if (EQ (face, Qborder))
3164 /* Changing background color of `border' sets frame parameter
3165 `border-color'. */
3166 if (EQ (attr, QCbackground))
3167 param = Qborder_color;
3169 else if (EQ (face, Qcursor))
3171 /* Changing background color of `cursor' sets frame parameter
3172 `cursor-color'. */
3173 if (EQ (attr, QCbackground))
3174 param = Qcursor_color;
3176 else if (EQ (face, Qmouse))
3178 /* Changing background color of `mouse' sets frame parameter
3179 `mouse-color'. */
3180 if (EQ (attr, QCbackground))
3181 param = Qmouse_color;
3183 #endif /* HAVE_WINDOW_SYSTEM */
3184 else if (EQ (face, Qmenu))
3186 /* Indicate that we have to update the menu bar when realizing
3187 faces on FRAME. FRAME t change the default for new frames.
3188 We do this by setting the flag in new face caches. */
3189 if (FRAMEP (frame))
3191 struct frame *f = XFRAME (frame);
3192 if (FRAME_FACE_CACHE (f) == NULL)
3193 FRAME_FACE_CACHE (f) = make_face_cache (f);
3194 FRAME_FACE_CACHE (f)->menu_face_changed_p = true;
3196 else
3197 menu_face_changed_default = true;
3200 if (!NILP (param))
3202 if (EQ (frame, Qt))
3203 /* Update `default-frame-alist', which is used for new frames. */
3205 store_in_alist (&Vdefault_frame_alist, param, value);
3207 else
3208 /* Update the current frame's parameters. */
3210 Lisp_Object cons;
3211 cons = XCAR (Vparam_value_alist);
3212 XSETCAR (cons, param);
3213 XSETCDR (cons, value);
3214 Fmodify_frame_parameters (frame, Vparam_value_alist);
3219 return face;
3223 /* Update the corresponding face when frame parameter PARAM on frame F
3224 has been assigned the value NEW_VALUE. */
3226 void
3227 update_face_from_frame_parameter (struct frame *f, Lisp_Object param,
3228 Lisp_Object new_value)
3230 Lisp_Object face = Qnil;
3231 Lisp_Object lface;
3233 /* If there are no faces yet, give up. This is the case when called
3234 from Fx_create_frame, and we do the necessary things later in
3235 face-set-after-frame-defaults. */
3236 if (NILP (f->face_alist))
3237 return;
3239 if (EQ (param, Qforeground_color))
3241 face = Qdefault;
3242 lface = lface_from_face_name (f, face, true);
3243 ASET (lface, LFACE_FOREGROUND_INDEX,
3244 (STRINGP (new_value) ? new_value : Qunspecified));
3245 realize_basic_faces (f);
3247 else if (EQ (param, Qbackground_color))
3249 Lisp_Object frame;
3251 /* Changing the background color might change the background
3252 mode, so that we have to load new defface specs.
3253 Call frame-set-background-mode to do that. */
3254 XSETFRAME (frame, f);
3255 call1 (Qframe_set_background_mode, frame);
3257 face = Qdefault;
3258 lface = lface_from_face_name (f, face, true);
3259 ASET (lface, LFACE_BACKGROUND_INDEX,
3260 (STRINGP (new_value) ? new_value : Qunspecified));
3261 realize_basic_faces (f);
3263 #ifdef HAVE_WINDOW_SYSTEM
3264 else if (EQ (param, Qborder_color))
3266 face = Qborder;
3267 lface = lface_from_face_name (f, face, true);
3268 ASET (lface, LFACE_BACKGROUND_INDEX,
3269 (STRINGP (new_value) ? new_value : Qunspecified));
3271 else if (EQ (param, Qcursor_color))
3273 face = Qcursor;
3274 lface = lface_from_face_name (f, face, true);
3275 ASET (lface, LFACE_BACKGROUND_INDEX,
3276 (STRINGP (new_value) ? new_value : Qunspecified));
3278 else if (EQ (param, Qmouse_color))
3280 face = Qmouse;
3281 lface = lface_from_face_name (f, face, true);
3282 ASET (lface, LFACE_BACKGROUND_INDEX,
3283 (STRINGP (new_value) ? new_value : Qunspecified));
3285 #endif
3287 /* Changing a named face means that all realized faces depending on
3288 that face are invalid. Since we cannot tell which realized faces
3289 depend on the face, make sure they are all removed. This is done
3290 by setting face_change. The next call to init_iterator will then
3291 free realized faces. */
3292 if (!NILP (face)
3293 && NILP (Fget (face, Qface_no_inherit)))
3295 f->face_change = true;
3296 windows_or_buffers_changed = 57;
3301 #ifdef HAVE_WINDOW_SYSTEM
3303 /* Set the `font' frame parameter of FRAME determined from the
3304 font-object set in `default' face attributes LFACE. */
3306 static void
3307 set_font_frame_param (Lisp_Object frame, Lisp_Object lface)
3309 struct frame *f = XFRAME (frame);
3310 Lisp_Object font;
3312 if (FRAME_WINDOW_P (f)
3313 /* Don't do anything if the font is `unspecified'. This can
3314 happen during frame creation. */
3315 && (font = LFACE_FONT (lface),
3316 ! UNSPECIFIEDP (font)))
3318 if (FONT_SPEC_P (font))
3320 font = font_load_for_lface (f, XVECTOR (lface)->contents, font);
3321 if (NILP (font))
3322 return;
3323 ASET (lface, LFACE_FONT_INDEX, font);
3325 f->default_face_done_p = false;
3326 AUTO_FRAME_ARG (arg, Qfont, font);
3327 Fmodify_frame_parameters (frame, arg);
3331 DEFUN ("internal-face-x-get-resource", Finternal_face_x_get_resource,
3332 Sinternal_face_x_get_resource, 2, 3, 0,
3333 doc: /* Get the value of X resource RESOURCE, class CLASS.
3334 Returned value is for the display of frame FRAME. If FRAME is not
3335 specified or nil, use selected frame. This function exists because
3336 ordinary `x-get-resource' doesn't take a frame argument. */)
3337 (Lisp_Object resource, Lisp_Object class, Lisp_Object frame)
3339 Lisp_Object value = Qnil;
3340 struct frame *f;
3342 CHECK_STRING (resource);
3343 CHECK_STRING (class);
3344 f = decode_live_frame (frame);
3345 block_input ();
3346 value = display_x_get_resource (FRAME_DISPLAY_INFO (f),
3347 resource, class, Qnil, Qnil);
3348 unblock_input ();
3349 return value;
3353 /* Return resource string VALUE as a boolean value, i.e. nil, or t.
3354 If VALUE is "on" or "true", return t. If VALUE is "off" or
3355 "false", return nil. Otherwise, if SIGNAL_P, signal an
3356 error; if !SIGNAL_P, return 0. */
3358 static Lisp_Object
3359 face_boolean_x_resource_value (Lisp_Object value, bool signal_p)
3361 Lisp_Object result = make_number (0);
3363 eassert (STRINGP (value));
3365 if (xstrcasecmp (SSDATA (value), "on") == 0
3366 || xstrcasecmp (SSDATA (value), "true") == 0)
3367 result = Qt;
3368 else if (xstrcasecmp (SSDATA (value), "off") == 0
3369 || xstrcasecmp (SSDATA (value), "false") == 0)
3370 result = Qnil;
3371 else if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3372 result = Qunspecified;
3373 else if (signal_p)
3374 signal_error ("Invalid face attribute value from X resource", value);
3376 return result;
3380 DEFUN ("internal-set-lisp-face-attribute-from-resource",
3381 Finternal_set_lisp_face_attribute_from_resource,
3382 Sinternal_set_lisp_face_attribute_from_resource,
3383 3, 4, 0, doc: /* */)
3384 (Lisp_Object face, Lisp_Object attr, Lisp_Object value, Lisp_Object frame)
3386 CHECK_SYMBOL (face);
3387 CHECK_SYMBOL (attr);
3388 CHECK_STRING (value);
3390 if (xstrcasecmp (SSDATA (value), "unspecified") == 0)
3391 value = Qunspecified;
3392 else if (EQ (attr, QCheight))
3394 value = Fstring_to_number (value, make_number (10));
3395 if (XINT (value) <= 0)
3396 signal_error ("Invalid face height from X resource", value);
3398 else if (EQ (attr, QCbold) || EQ (attr, QCitalic))
3399 value = face_boolean_x_resource_value (value, true);
3400 else if (EQ (attr, QCweight) || EQ (attr, QCslant) || EQ (attr, QCwidth))
3401 value = intern (SSDATA (value));
3402 else if (EQ (attr, QCreverse_video) || EQ (attr, QCinverse_video))
3403 value = face_boolean_x_resource_value (value, true);
3404 else if (EQ (attr, QCunderline)
3405 || EQ (attr, QCoverline)
3406 || EQ (attr, QCstrike_through))
3408 Lisp_Object boolean_value;
3410 /* If the result of face_boolean_x_resource_value is t or nil,
3411 VALUE does NOT specify a color. */
3412 boolean_value = face_boolean_x_resource_value (value, false);
3413 if (SYMBOLP (boolean_value))
3414 value = boolean_value;
3416 else if (EQ (attr, QCbox) || EQ (attr, QCinherit))
3417 value = Fcar (Fread_from_string (value, Qnil, Qnil));
3419 return Finternal_set_lisp_face_attribute (face, attr, value, frame);
3422 #endif /* HAVE_WINDOW_SYSTEM */
3425 /***********************************************************************
3426 Menu face
3427 ***********************************************************************/
3429 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
3431 /* Make menus on frame F appear as specified by the `menu' face. */
3433 static void
3434 x_update_menu_appearance (struct frame *f)
3436 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
3437 XrmDatabase rdb;
3439 if (dpyinfo
3440 && (rdb = XrmGetDatabase (FRAME_X_DISPLAY (f)),
3441 rdb != NULL))
3443 char line[512];
3444 char *buf = line;
3445 ptrdiff_t bufsize = sizeof line;
3446 Lisp_Object lface = lface_from_face_name (f, Qmenu, true);
3447 struct face *face = FACE_FROM_ID (f, MENU_FACE_ID);
3448 const char *myname = SSDATA (Vx_resource_name);
3449 bool changed_p = false;
3450 #ifdef USE_MOTIF
3451 const char *popup_path = "popup_menu";
3452 #else
3453 const char *popup_path = "menu.popup";
3454 #endif
3456 if (STRINGP (LFACE_FOREGROUND (lface)))
3458 exprintf (&buf, &bufsize, line, -1, "%s.%s*foreground: %s",
3459 myname, popup_path,
3460 SDATA (LFACE_FOREGROUND (lface)));
3461 XrmPutLineResource (&rdb, line);
3462 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*foreground: %s",
3463 myname, SDATA (LFACE_FOREGROUND (lface)));
3464 XrmPutLineResource (&rdb, line);
3465 changed_p = true;
3468 if (STRINGP (LFACE_BACKGROUND (lface)))
3470 exprintf (&buf, &bufsize, line, -1, "%s.%s*background: %s",
3471 myname, popup_path,
3472 SDATA (LFACE_BACKGROUND (lface)));
3473 XrmPutLineResource (&rdb, line);
3475 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*background: %s",
3476 myname, SDATA (LFACE_BACKGROUND (lface)));
3477 XrmPutLineResource (&rdb, line);
3478 changed_p = true;
3481 if (face->font
3482 /* On Solaris 5.8, it's been reported that the `menu' face
3483 can be unspecified here, during startup. Why this
3484 happens remains unknown. -- cyd */
3485 && FONTP (LFACE_FONT (lface))
3486 && (!UNSPECIFIEDP (LFACE_FAMILY (lface))
3487 || !UNSPECIFIEDP (LFACE_FOUNDRY (lface))
3488 || !UNSPECIFIEDP (LFACE_SWIDTH (lface))
3489 || !UNSPECIFIEDP (LFACE_WEIGHT (lface))
3490 || !UNSPECIFIEDP (LFACE_SLANT (lface))
3491 || !UNSPECIFIEDP (LFACE_HEIGHT (lface))))
3493 Lisp_Object xlfd = Ffont_xlfd_name (LFACE_FONT (lface), Qnil);
3494 #ifdef USE_MOTIF
3495 const char *suffix = "List";
3496 bool motif = true;
3497 #else
3498 #if defined HAVE_X_I18N
3500 const char *suffix = "Set";
3501 #else
3502 const char *suffix = "";
3503 #endif
3504 bool motif = false;
3505 #endif
3507 if (! NILP (xlfd))
3509 #if defined HAVE_X_I18N
3510 char *fontsetname = xic_create_fontsetname (SSDATA (xlfd), motif);
3511 #else
3512 char *fontsetname = SSDATA (xlfd);
3513 #endif
3514 exprintf (&buf, &bufsize, line, -1, "%s.pane.menubar*font%s: %s",
3515 myname, suffix, fontsetname);
3516 XrmPutLineResource (&rdb, line);
3518 exprintf (&buf, &bufsize, line, -1, "%s.%s*font%s: %s",
3519 myname, popup_path, suffix, fontsetname);
3520 XrmPutLineResource (&rdb, line);
3521 changed_p = true;
3522 if (fontsetname != SSDATA (xlfd))
3523 xfree (fontsetname);
3527 if (changed_p && f->output_data.x->menubar_widget)
3528 free_frame_menubar (f);
3530 if (buf != line)
3531 xfree (buf);
3535 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
3538 DEFUN ("face-attribute-relative-p", Fface_attribute_relative_p,
3539 Sface_attribute_relative_p,
3540 2, 2, 0,
3541 doc: /* Check whether a face attribute value is relative.
3542 Specifically, this function returns t if the attribute ATTRIBUTE
3543 with the value VALUE is relative.
3545 A relative value is one that doesn't entirely override whatever is
3546 inherited from another face. For most possible attributes,
3547 the only relative value that users see is `unspecified'.
3548 However, for :height, floating point values are also relative. */
3549 attributes: const)
3550 (Lisp_Object attribute, Lisp_Object value)
3552 if (EQ (value, Qunspecified) || (EQ (value, QCignore_defface)))
3553 return Qt;
3554 else if (EQ (attribute, QCheight))
3555 return INTEGERP (value) ? Qnil : Qt;
3556 else
3557 return Qnil;
3560 DEFUN ("merge-face-attribute", Fmerge_face_attribute, Smerge_face_attribute,
3561 3, 3, 0,
3562 doc: /* Return face ATTRIBUTE VALUE1 merged with VALUE2.
3563 If VALUE1 or VALUE2 are absolute (see `face-attribute-relative-p'), then
3564 the result will be absolute, otherwise it will be relative. */)
3565 (Lisp_Object attribute, Lisp_Object value1, Lisp_Object value2)
3567 if (EQ (value1, Qunspecified) || EQ (value1, QCignore_defface))
3568 return value2;
3569 else if (EQ (attribute, QCheight))
3570 return merge_face_heights (value1, value2, value1);
3571 else
3572 return value1;
3576 DEFUN ("internal-get-lisp-face-attribute", Finternal_get_lisp_face_attribute,
3577 Sinternal_get_lisp_face_attribute,
3578 2, 3, 0,
3579 doc: /* Return face attribute KEYWORD of face SYMBOL.
3580 If SYMBOL does not name a valid Lisp face or KEYWORD isn't a valid
3581 face attribute name, signal an error.
3582 If the optional argument FRAME is given, report on face SYMBOL in that
3583 frame. If FRAME is t, report on the defaults for face SYMBOL (for new
3584 frames). If FRAME is omitted or nil, use the selected frame. */)
3585 (Lisp_Object symbol, Lisp_Object keyword, Lisp_Object frame)
3587 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3588 Lisp_Object lface = lface_from_face_name (f, symbol, true), value = Qnil;
3590 CHECK_SYMBOL (symbol);
3591 CHECK_SYMBOL (keyword);
3593 if (EQ (keyword, QCfamily))
3594 value = LFACE_FAMILY (lface);
3595 else if (EQ (keyword, QCfoundry))
3596 value = LFACE_FOUNDRY (lface);
3597 else if (EQ (keyword, QCheight))
3598 value = LFACE_HEIGHT (lface);
3599 else if (EQ (keyword, QCweight))
3600 value = LFACE_WEIGHT (lface);
3601 else if (EQ (keyword, QCslant))
3602 value = LFACE_SLANT (lface);
3603 else if (EQ (keyword, QCunderline))
3604 value = LFACE_UNDERLINE (lface);
3605 else if (EQ (keyword, QCoverline))
3606 value = LFACE_OVERLINE (lface);
3607 else if (EQ (keyword, QCstrike_through))
3608 value = LFACE_STRIKE_THROUGH (lface);
3609 else if (EQ (keyword, QCbox))
3610 value = LFACE_BOX (lface);
3611 else if (EQ (keyword, QCinverse_video)
3612 || EQ (keyword, QCreverse_video))
3613 value = LFACE_INVERSE (lface);
3614 else if (EQ (keyword, QCforeground))
3615 value = LFACE_FOREGROUND (lface);
3616 else if (EQ (keyword, QCdistant_foreground))
3617 value = LFACE_DISTANT_FOREGROUND (lface);
3618 else if (EQ (keyword, QCbackground))
3619 value = LFACE_BACKGROUND (lface);
3620 else if (EQ (keyword, QCstipple))
3621 value = LFACE_STIPPLE (lface);
3622 else if (EQ (keyword, QCwidth))
3623 value = LFACE_SWIDTH (lface);
3624 else if (EQ (keyword, QCinherit))
3625 value = LFACE_INHERIT (lface);
3626 else if (EQ (keyword, QCfont))
3627 value = LFACE_FONT (lface);
3628 else if (EQ (keyword, QCfontset))
3629 value = LFACE_FONTSET (lface);
3630 else
3631 signal_error ("Invalid face attribute name", keyword);
3633 if (IGNORE_DEFFACE_P (value))
3634 return Qunspecified;
3636 return value;
3640 DEFUN ("internal-lisp-face-attribute-values",
3641 Finternal_lisp_face_attribute_values,
3642 Sinternal_lisp_face_attribute_values, 1, 1, 0,
3643 doc: /* Return a list of valid discrete values for face attribute ATTR.
3644 Value is nil if ATTR doesn't have a discrete set of valid values. */)
3645 (Lisp_Object attr)
3647 Lisp_Object result = Qnil;
3649 CHECK_SYMBOL (attr);
3651 if (EQ (attr, QCunderline) || EQ (attr, QCoverline)
3652 || EQ (attr, QCstrike_through)
3653 || EQ (attr, QCinverse_video) || EQ (attr, QCreverse_video))
3654 result = list2 (Qt, Qnil);
3656 return result;
3660 DEFUN ("internal-merge-in-global-face", Finternal_merge_in_global_face,
3661 Sinternal_merge_in_global_face, 2, 2, 0,
3662 doc: /* Add attributes from frame-default definition of FACE to FACE on FRAME.
3663 Default face attributes override any local face attributes. */)
3664 (Lisp_Object face, Lisp_Object frame)
3666 int i;
3667 Lisp_Object global_lface, local_lface, *gvec, *lvec;
3668 struct frame *f = XFRAME (frame);
3670 CHECK_LIVE_FRAME (frame);
3671 global_lface = lface_from_face_name (NULL, face, true);
3672 local_lface = lface_from_face_name (f, face, false);
3673 if (NILP (local_lface))
3674 local_lface = Finternal_make_lisp_face (face, frame);
3676 /* Make every specified global attribute override the local one.
3677 BEWARE!! This is only used from `face-set-after-frame-default' where
3678 the local frame is defined from default specs in `face-defface-spec'
3679 and those should be overridden by global settings. Hence the strange
3680 "global before local" priority. */
3681 lvec = XVECTOR (local_lface)->contents;
3682 gvec = XVECTOR (global_lface)->contents;
3683 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3684 if (IGNORE_DEFFACE_P (gvec[i]))
3685 ASET (local_lface, i, Qunspecified);
3686 else if (! UNSPECIFIEDP (gvec[i]))
3687 ASET (local_lface, i, AREF (global_lface, i));
3689 /* If the default face was changed, update the face cache and the
3690 `font' frame parameter. */
3691 if (EQ (face, Qdefault))
3693 struct face_cache *c = FRAME_FACE_CACHE (f);
3694 struct face *newface, *oldface = FACE_FROM_ID (f, DEFAULT_FACE_ID);
3695 Lisp_Object attrs[LFACE_VECTOR_SIZE];
3697 /* This can be NULL (e.g., in batch mode). */
3698 if (oldface)
3700 /* Ensure that the face vector is fully specified by merging
3701 the previously-cached vector. */
3702 memcpy (attrs, oldface->lface, sizeof attrs);
3703 merge_face_vectors (f, lvec, attrs, 0);
3704 vcopy (local_lface, 0, attrs, LFACE_VECTOR_SIZE);
3705 newface = realize_face (c, lvec, DEFAULT_FACE_ID);
3707 if ((! UNSPECIFIEDP (gvec[LFACE_FAMILY_INDEX])
3708 || ! UNSPECIFIEDP (gvec[LFACE_FOUNDRY_INDEX])
3709 || ! UNSPECIFIEDP (gvec[LFACE_HEIGHT_INDEX])
3710 || ! UNSPECIFIEDP (gvec[LFACE_WEIGHT_INDEX])
3711 || ! UNSPECIFIEDP (gvec[LFACE_SLANT_INDEX])
3712 || ! UNSPECIFIEDP (gvec[LFACE_SWIDTH_INDEX])
3713 || ! UNSPECIFIEDP (gvec[LFACE_FONT_INDEX]))
3714 && newface->font)
3716 Lisp_Object name = newface->font->props[FONT_NAME_INDEX];
3717 AUTO_FRAME_ARG (arg, Qfont, name);
3718 Fmodify_frame_parameters (frame, arg);
3721 if (STRINGP (gvec[LFACE_FOREGROUND_INDEX]))
3723 AUTO_FRAME_ARG (arg, Qforeground_color,
3724 gvec[LFACE_FOREGROUND_INDEX]);
3725 Fmodify_frame_parameters (frame, arg);
3728 if (STRINGP (gvec[LFACE_BACKGROUND_INDEX]))
3730 AUTO_FRAME_ARG (arg, Qbackground_color,
3731 gvec[LFACE_BACKGROUND_INDEX]);
3732 Fmodify_frame_parameters (frame, arg);
3737 return Qnil;
3741 /* The following function is implemented for compatibility with 20.2.
3742 The function is used in x-resolve-fonts when it is asked to
3743 return fonts with the same size as the font of a face. This is
3744 done in fontset.el. */
3746 DEFUN ("face-font", Fface_font, Sface_font, 1, 3, 0,
3747 doc: /* Return the font name of face FACE, or nil if it is unspecified.
3748 The font name is, by default, for ASCII characters.
3749 If the optional argument FRAME is given, report on face FACE in that frame.
3750 If FRAME is t, report on the defaults for face FACE (for new frames).
3751 The font default for a face is either nil, or a list
3752 of the form (bold), (italic) or (bold italic).
3753 If FRAME is omitted or nil, use the selected frame. And, in this case,
3754 if the optional third argument CHARACTER is given,
3755 return the font name used for CHARACTER. */)
3756 (Lisp_Object face, Lisp_Object frame, Lisp_Object character)
3758 if (EQ (frame, Qt))
3760 Lisp_Object result = Qnil;
3761 Lisp_Object lface = lface_from_face_name (NULL, face, true);
3763 if (!UNSPECIFIEDP (LFACE_WEIGHT (lface))
3764 && !EQ (LFACE_WEIGHT (lface), Qnormal))
3765 result = Fcons (Qbold, result);
3767 if (!UNSPECIFIEDP (LFACE_SLANT (lface))
3768 && !EQ (LFACE_SLANT (lface), Qnormal))
3769 result = Fcons (Qitalic, result);
3771 return result;
3773 else
3775 struct frame *f = decode_live_frame (frame);
3776 int face_id = lookup_named_face (f, face, true);
3777 struct face *fface = FACE_FROM_ID (f, face_id);
3779 if (! fface)
3780 return Qnil;
3781 #ifdef HAVE_WINDOW_SYSTEM
3782 if (FRAME_WINDOW_P (f) && !NILP (character))
3784 CHECK_CHARACTER (character);
3785 face_id = FACE_FOR_CHAR (f, fface, XINT (character), -1, Qnil);
3786 fface = FACE_FROM_ID (f, face_id);
3788 return (fface->font
3789 ? fface->font->props[FONT_NAME_INDEX]
3790 : Qnil);
3791 #else /* !HAVE_WINDOW_SYSTEM */
3792 return build_string (FRAME_MSDOS_P (f)
3793 ? "ms-dos"
3794 : FRAME_W32_P (f) ? "w32term"
3795 :"tty");
3796 #endif
3801 /* Compare face-attribute values v1 and v2 for equality. Value is true if
3802 all attributes are `equal'. Tries to be fast because this function
3803 is called quite often. */
3805 static bool
3806 face_attr_equal_p (Lisp_Object v1, Lisp_Object v2)
3808 /* Type can differ, e.g. when one attribute is unspecified, i.e. nil,
3809 and the other is specified. */
3810 if (XTYPE (v1) != XTYPE (v2))
3811 return false;
3813 if (EQ (v1, v2))
3814 return true;
3816 switch (XTYPE (v1))
3818 case Lisp_String:
3819 if (SBYTES (v1) != SBYTES (v2))
3820 return false;
3822 return memcmp (SDATA (v1), SDATA (v2), SBYTES (v1)) == 0;
3824 case_Lisp_Int:
3825 case Lisp_Symbol:
3826 return false;
3828 default:
3829 return !NILP (Fequal (v1, v2));
3834 /* Compare face vectors V1 and V2 for equality. Value is true if
3835 all attributes are `equal'. Tries to be fast because this function
3836 is called quite often. */
3838 static bool
3839 lface_equal_p (Lisp_Object *v1, Lisp_Object *v2)
3841 int i;
3842 bool equal_p = true;
3844 for (i = 1; i < LFACE_VECTOR_SIZE && equal_p; ++i)
3845 equal_p = face_attr_equal_p (v1[i], v2[i]);
3847 return equal_p;
3851 DEFUN ("internal-lisp-face-equal-p", Finternal_lisp_face_equal_p,
3852 Sinternal_lisp_face_equal_p, 2, 3, 0,
3853 doc: /* True if FACE1 and FACE2 are equal.
3854 If the optional argument FRAME is given, report on FACE1 and FACE2 in that frame.
3855 If FRAME is t, report on the defaults for FACE1 and FACE2 (for new frames).
3856 If FRAME is omitted or nil, use the selected frame. */)
3857 (Lisp_Object face1, Lisp_Object face2, Lisp_Object frame)
3859 bool equal_p;
3860 struct frame *f;
3861 Lisp_Object lface1, lface2;
3863 /* Don't use decode_window_system_frame here because this function
3864 is called before X frames exist. At that time, if FRAME is nil,
3865 selected_frame will be used which is the frame dumped with
3866 Emacs. That frame is not an X frame. */
3867 f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3869 lface1 = lface_from_face_name (f, face1, true);
3870 lface2 = lface_from_face_name (f, face2, true);
3871 equal_p = lface_equal_p (XVECTOR (lface1)->contents,
3872 XVECTOR (lface2)->contents);
3873 return equal_p ? Qt : Qnil;
3877 DEFUN ("internal-lisp-face-empty-p", Finternal_lisp_face_empty_p,
3878 Sinternal_lisp_face_empty_p, 1, 2, 0,
3879 doc: /* True if FACE has no attribute specified.
3880 If the optional argument FRAME is given, report on face FACE in that frame.
3881 If FRAME is t, report on the defaults for face FACE (for new frames).
3882 If FRAME is omitted or nil, use the selected frame. */)
3883 (Lisp_Object face, Lisp_Object frame)
3885 struct frame *f = EQ (frame, Qt) ? NULL : decode_live_frame (frame);
3886 Lisp_Object lface = lface_from_face_name (f, face, true);
3887 int i;
3889 for (i = 1; i < LFACE_VECTOR_SIZE; ++i)
3890 if (!UNSPECIFIEDP (AREF (lface, i)))
3891 break;
3893 return i == LFACE_VECTOR_SIZE ? Qt : Qnil;
3897 DEFUN ("frame-face-alist", Fframe_face_alist, Sframe_face_alist,
3898 0, 1, 0,
3899 doc: /* Return an alist of frame-local faces defined on FRAME.
3900 For internal use only. */)
3901 (Lisp_Object frame)
3903 return decode_live_frame (frame)->face_alist;
3907 /* Return a hash code for Lisp string STRING with case ignored. Used
3908 below in computing a hash value for a Lisp face. */
3910 static unsigned
3911 hash_string_case_insensitive (Lisp_Object string)
3913 const unsigned char *s;
3914 unsigned hash = 0;
3915 eassert (STRINGP (string));
3916 for (s = SDATA (string); *s; ++s)
3917 hash = (hash << 1) ^ c_tolower (*s);
3918 return hash;
3922 /* Return a hash code for face attribute vector V. */
3924 static unsigned
3925 lface_hash (Lisp_Object *v)
3927 return (hash_string_case_insensitive (v[LFACE_FAMILY_INDEX])
3928 ^ hash_string_case_insensitive (v[LFACE_FOUNDRY_INDEX])
3929 ^ hash_string_case_insensitive (v[LFACE_FOREGROUND_INDEX])
3930 ^ hash_string_case_insensitive (v[LFACE_BACKGROUND_INDEX])
3931 ^ XHASH (v[LFACE_WEIGHT_INDEX])
3932 ^ XHASH (v[LFACE_SLANT_INDEX])
3933 ^ XHASH (v[LFACE_SWIDTH_INDEX])
3934 ^ XHASH (v[LFACE_HEIGHT_INDEX]));
3937 #ifdef HAVE_WINDOW_SYSTEM
3939 /* Return true if LFACE1 and LFACE2 specify the same font (without
3940 considering charsets/registries). They do if they specify the same
3941 family, point size, weight, width, slant, and font. Both
3942 LFACE1 and LFACE2 must be fully-specified. */
3944 static bool
3945 lface_same_font_attributes_p (Lisp_Object *lface1, Lisp_Object *lface2)
3947 eassert (lface_fully_specified_p (lface1)
3948 && lface_fully_specified_p (lface2));
3949 return (xstrcasecmp (SSDATA (lface1[LFACE_FAMILY_INDEX]),
3950 SSDATA (lface2[LFACE_FAMILY_INDEX])) == 0
3951 && xstrcasecmp (SSDATA (lface1[LFACE_FOUNDRY_INDEX]),
3952 SSDATA (lface2[LFACE_FOUNDRY_INDEX])) == 0
3953 && EQ (lface1[LFACE_HEIGHT_INDEX], lface2[LFACE_HEIGHT_INDEX])
3954 && EQ (lface1[LFACE_SWIDTH_INDEX], lface2[LFACE_SWIDTH_INDEX])
3955 && EQ (lface1[LFACE_WEIGHT_INDEX], lface2[LFACE_WEIGHT_INDEX])
3956 && EQ (lface1[LFACE_SLANT_INDEX], lface2[LFACE_SLANT_INDEX])
3957 && EQ (lface1[LFACE_FONT_INDEX], lface2[LFACE_FONT_INDEX])
3958 && (EQ (lface1[LFACE_FONTSET_INDEX], lface2[LFACE_FONTSET_INDEX])
3959 || (STRINGP (lface1[LFACE_FONTSET_INDEX])
3960 && STRINGP (lface2[LFACE_FONTSET_INDEX])
3961 && ! xstrcasecmp (SSDATA (lface1[LFACE_FONTSET_INDEX]),
3962 SSDATA (lface2[LFACE_FONTSET_INDEX]))))
3966 #endif /* HAVE_WINDOW_SYSTEM */
3968 /***********************************************************************
3969 Realized Faces
3970 ***********************************************************************/
3972 /* Allocate and return a new realized face for Lisp face attribute
3973 vector ATTR. */
3975 static struct face *
3976 make_realized_face (Lisp_Object *attr)
3978 enum { off = offsetof (struct face, id) };
3979 struct face *face = xmalloc (sizeof *face);
3981 memcpy (face->lface, attr, sizeof face->lface);
3982 memset (&face->id, 0, sizeof *face - off);
3983 face->ascii_face = face;
3985 return face;
3989 /* Free realized face FACE, including its X resources. FACE may
3990 be null. */
3992 static void
3993 free_realized_face (struct frame *f, struct face *face)
3995 if (face)
3997 #ifdef HAVE_WINDOW_SYSTEM
3998 if (FRAME_WINDOW_P (f))
4000 /* Free fontset of FACE if it is ASCII face. */
4001 if (face->fontset >= 0 && face == face->ascii_face)
4002 free_face_fontset (f, face);
4003 if (face->gc)
4005 block_input ();
4006 if (face->font)
4007 font_done_for_face (f, face);
4008 x_free_gc (f, face->gc);
4009 face->gc = 0;
4010 unblock_input ();
4012 #ifdef HAVE_X_WINDOWS
4013 free_face_colors (f, face);
4014 #endif /* HAVE_X_WINDOWS */
4015 x_destroy_bitmap (f, face->stipple);
4017 #endif /* HAVE_WINDOW_SYSTEM */
4019 xfree (face);
4023 #ifdef HAVE_WINDOW_SYSTEM
4025 /* Prepare face FACE for subsequent display on frame F. This must be called
4026 before using X resources of FACE to allocate GCs if they haven't been
4027 allocated yet or have been freed by clearing the face cache. */
4029 void
4030 prepare_face_for_display (struct frame *f, struct face *face)
4032 eassert (FRAME_WINDOW_P (f));
4034 if (face->gc == 0)
4036 XGCValues xgcv;
4037 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
4039 xgcv.foreground = face->foreground;
4040 xgcv.background = face->background;
4041 #ifdef HAVE_X_WINDOWS
4042 xgcv.graphics_exposures = False;
4043 #endif
4045 block_input ();
4046 #ifdef HAVE_X_WINDOWS
4047 if (face->stipple)
4049 xgcv.fill_style = FillOpaqueStippled;
4050 xgcv.stipple = x_bitmap_pixmap (f, face->stipple);
4051 mask |= GCFillStyle | GCStipple;
4053 #endif
4054 face->gc = x_create_gc (f, mask, &xgcv);
4055 if (face->font)
4056 font_prepare_for_face (f, face);
4057 unblock_input ();
4061 #endif /* HAVE_WINDOW_SYSTEM */
4063 /* Returns the `distance' between the colors X and Y. */
4065 static int
4066 color_distance (XColor *x, XColor *y)
4068 /* This formula is from a paper titled `Colour metric' by Thiadmer Riemersma.
4069 Quoting from that paper:
4071 This formula has results that are very close to L*u*v* (with the
4072 modified lightness curve) and, more importantly, it is a more even
4073 algorithm: it does not have a range of colors where it suddenly
4074 gives far from optimal results.
4076 See <http://www.compuphase.com/cmetric.htm> for more info. */
4078 long r = (x->red - y->red) >> 8;
4079 long g = (x->green - y->green) >> 8;
4080 long b = (x->blue - y->blue) >> 8;
4081 long r_mean = (x->red + y->red) >> 9;
4083 return
4084 (((512 + r_mean) * r * r) >> 8)
4085 + 4 * g * g
4086 + (((767 - r_mean) * b * b) >> 8);
4090 DEFUN ("color-distance", Fcolor_distance, Scolor_distance, 2, 3, 0,
4091 doc: /* Return an integer distance between COLOR1 and COLOR2 on FRAME.
4092 COLOR1 and COLOR2 may be either strings containing the color name,
4093 or lists of the form (RED GREEN BLUE).
4094 If FRAME is unspecified or nil, the current frame is used. */)
4095 (Lisp_Object color1, Lisp_Object color2, Lisp_Object frame)
4097 struct frame *f = decode_live_frame (frame);
4098 XColor cdef1, cdef2;
4100 if (!(CONSP (color1) && parse_rgb_list (color1, &cdef1))
4101 && !(STRINGP (color1)
4102 && defined_color (f, SSDATA (color1), &cdef1, false)))
4103 signal_error ("Invalid color", color1);
4104 if (!(CONSP (color2) && parse_rgb_list (color2, &cdef2))
4105 && !(STRINGP (color2)
4106 && defined_color (f, SSDATA (color2), &cdef2, false)))
4107 signal_error ("Invalid color", color2);
4109 return make_number (color_distance (&cdef1, &cdef2));
4113 /***********************************************************************
4114 Face Cache
4115 ***********************************************************************/
4117 /* Return a new face cache for frame F. */
4119 static struct face_cache *
4120 make_face_cache (struct frame *f)
4122 struct face_cache *c = xmalloc (sizeof *c);
4124 c->buckets = xzalloc (FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets);
4125 c->size = 50;
4126 c->used = 0;
4127 c->faces_by_id = xmalloc (c->size * sizeof *c->faces_by_id);
4128 c->f = f;
4129 c->menu_face_changed_p = menu_face_changed_default;
4130 return c;
4133 #ifdef HAVE_WINDOW_SYSTEM
4135 /* Clear out all graphics contexts for all realized faces, except for
4136 the basic faces. This should be done from time to time just to avoid
4137 keeping too many graphics contexts that are no longer needed. */
4139 static void
4140 clear_face_gcs (struct face_cache *c)
4142 if (c && FRAME_WINDOW_P (c->f))
4144 int i;
4145 for (i = BASIC_FACE_ID_SENTINEL; i < c->used; ++i)
4147 struct face *face = c->faces_by_id[i];
4148 if (face && face->gc)
4150 block_input ();
4151 if (face->font)
4152 font_done_for_face (c->f, face);
4153 x_free_gc (c->f, face->gc);
4154 face->gc = 0;
4155 unblock_input ();
4161 #endif /* HAVE_WINDOW_SYSTEM */
4163 /* Free all realized faces in face cache C, including basic faces.
4164 C may be null. If faces are freed, make sure the frame's current
4165 matrix is marked invalid, so that a display caused by an expose
4166 event doesn't try to use faces we destroyed. */
4168 static void
4169 free_realized_faces (struct face_cache *c)
4171 if (c && c->used)
4173 int i, size;
4174 struct frame *f = c->f;
4176 /* We must block input here because we can't process X events
4177 safely while only some faces are freed, or when the frame's
4178 current matrix still references freed faces. */
4179 block_input ();
4181 for (i = 0; i < c->used; ++i)
4183 free_realized_face (f, c->faces_by_id[i]);
4184 c->faces_by_id[i] = NULL;
4187 /* Forget the escape-glyph and glyphless-char faces. */
4188 forget_escape_and_glyphless_faces ();
4189 c->used = 0;
4190 size = FACE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4191 memset (c->buckets, 0, size);
4193 /* Must do a thorough redisplay the next time. Mark current
4194 matrices as invalid because they will reference faces freed
4195 above. This function is also called when a frame is
4196 destroyed. In this case, the root window of F is nil. */
4197 if (WINDOWP (f->root_window))
4199 clear_current_matrices (f);
4200 fset_redisplay (f);
4203 unblock_input ();
4208 /* Free all realized faces on FRAME or on all frames if FRAME is nil.
4209 This is done after attributes of a named face have been changed,
4210 because we can't tell which realized faces depend on that face. */
4212 void
4213 free_all_realized_faces (Lisp_Object frame)
4215 if (NILP (frame))
4217 Lisp_Object rest;
4218 FOR_EACH_FRAME (rest, frame)
4219 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4220 windows_or_buffers_changed = 58;
4222 else
4223 free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
4227 /* Free face cache C and faces in it, including their X resources. */
4229 static void
4230 free_face_cache (struct face_cache *c)
4232 if (c)
4234 free_realized_faces (c);
4235 xfree (c->buckets);
4236 xfree (c->faces_by_id);
4237 xfree (c);
4242 /* Cache realized face FACE in face cache C. HASH is the hash value
4243 of FACE. If FACE is for ASCII characters (i.e. FACE->ascii_face ==
4244 FACE), insert the new face to the beginning of the collision list
4245 of the face hash table of C. Otherwise, add the new face to the
4246 end of the collision list. This way, lookup_face can quickly find
4247 that a requested face is not cached. */
4249 static void
4250 cache_face (struct face_cache *c, struct face *face, unsigned int hash)
4252 int i = hash % FACE_CACHE_BUCKETS_SIZE;
4254 face->hash = hash;
4256 if (face->ascii_face != face)
4258 struct face *last = c->buckets[i];
4259 if (last)
4261 while (last->next)
4262 last = last->next;
4263 last->next = face;
4264 face->prev = last;
4265 face->next = NULL;
4267 else
4269 c->buckets[i] = face;
4270 face->prev = face->next = NULL;
4273 else
4275 face->prev = NULL;
4276 face->next = c->buckets[i];
4277 if (face->next)
4278 face->next->prev = face;
4279 c->buckets[i] = face;
4282 /* Find a free slot in C->faces_by_id and use the index of the free
4283 slot as FACE->id. */
4284 for (i = 0; i < c->used; ++i)
4285 if (c->faces_by_id[i] == NULL)
4286 break;
4287 face->id = i;
4289 #ifdef GLYPH_DEBUG
4290 /* Check that FACE got a unique id. */
4292 int j, n;
4293 struct face *face1;
4295 for (j = n = 0; j < FACE_CACHE_BUCKETS_SIZE; ++j)
4296 for (face1 = c->buckets[j]; face1; face1 = face1->next)
4297 if (face1->id == i)
4298 ++n;
4300 eassert (n == 1);
4302 #endif /* GLYPH_DEBUG */
4304 /* Maybe enlarge C->faces_by_id. */
4305 if (i == c->used)
4307 if (c->used == c->size)
4308 c->faces_by_id = xpalloc (c->faces_by_id, &c->size, 1, MAX_FACE_ID,
4309 sizeof *c->faces_by_id);
4310 c->used++;
4313 c->faces_by_id[i] = face;
4317 /* Remove face FACE from cache C. */
4319 static void
4320 uncache_face (struct face_cache *c, struct face *face)
4322 int i = face->hash % FACE_CACHE_BUCKETS_SIZE;
4324 if (face->prev)
4325 face->prev->next = face->next;
4326 else
4327 c->buckets[i] = face->next;
4329 if (face->next)
4330 face->next->prev = face->prev;
4332 c->faces_by_id[face->id] = NULL;
4333 if (face->id == c->used)
4334 --c->used;
4338 /* Look up a realized face with face attributes ATTR in the face cache
4339 of frame F. The face will be used to display ASCII characters.
4340 Value is the ID of the face found. If no suitable face is found,
4341 realize a new one. */
4343 static int
4344 lookup_face (struct frame *f, Lisp_Object *attr)
4346 struct face_cache *cache = FRAME_FACE_CACHE (f);
4347 unsigned hash;
4348 int i;
4349 struct face *face;
4351 eassert (cache != NULL);
4352 check_lface_attrs (attr);
4354 /* Look up ATTR in the face cache. */
4355 hash = lface_hash (attr);
4356 i = hash % FACE_CACHE_BUCKETS_SIZE;
4358 for (face = cache->buckets[i]; face; face = face->next)
4360 if (face->ascii_face != face)
4362 /* There's no more ASCII face. */
4363 face = NULL;
4364 break;
4366 if (face->hash == hash
4367 && lface_equal_p (face->lface, attr))
4368 break;
4371 /* If not found, realize a new face. */
4372 if (face == NULL)
4373 face = realize_face (cache, attr, -1);
4375 #ifdef GLYPH_DEBUG
4376 eassert (face == FACE_FROM_ID (f, face->id));
4377 #endif /* GLYPH_DEBUG */
4379 return face->id;
4382 #ifdef HAVE_WINDOW_SYSTEM
4383 /* Look up a realized face that has the same attributes as BASE_FACE
4384 except for the font in the face cache of frame F. If FONT-OBJECT
4385 is not nil, it is an already opened font. If FONT-OBJECT is nil,
4386 the face has no font. Value is the ID of the face found. If no
4387 suitable face is found, realize a new one. */
4390 face_for_font (struct frame *f, Lisp_Object font_object, struct face *base_face)
4392 struct face_cache *cache = FRAME_FACE_CACHE (f);
4393 unsigned hash;
4394 int i;
4395 struct face *face;
4397 eassert (cache != NULL);
4398 base_face = base_face->ascii_face;
4399 hash = lface_hash (base_face->lface);
4400 i = hash % FACE_CACHE_BUCKETS_SIZE;
4402 for (face = cache->buckets[i]; face; face = face->next)
4404 if (face->ascii_face == face)
4405 continue;
4406 if (face->ascii_face == base_face
4407 && face->font == (NILP (font_object) ? NULL
4408 : XFONT_OBJECT (font_object))
4409 && lface_equal_p (face->lface, base_face->lface))
4410 return face->id;
4413 /* If not found, realize a new face. */
4414 face = realize_non_ascii_face (f, font_object, base_face);
4415 return face->id;
4417 #endif /* HAVE_WINDOW_SYSTEM */
4419 /* Return the face id of the realized face for named face SYMBOL on
4420 frame F suitable for displaying ASCII characters. Value is -1 if
4421 the face couldn't be determined, which might happen if the default
4422 face isn't realized and cannot be realized. */
4425 lookup_named_face (struct frame *f, Lisp_Object symbol, bool signal_p)
4427 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4428 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4429 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4431 if (default_face == NULL)
4433 if (!realize_basic_faces (f))
4434 return -1;
4435 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4436 if (default_face == NULL)
4437 emacs_abort (); /* realize_basic_faces must have set it up */
4440 if (! get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4441 return -1;
4443 memcpy (attrs, default_face->lface, sizeof attrs);
4444 merge_face_vectors (f, symbol_attrs, attrs, 0);
4446 return lookup_face (f, attrs);
4450 /* Return the display face-id of the basic face whose canonical face-id
4451 is FACE_ID. The return value will usually simply be FACE_ID, unless that
4452 basic face has bee remapped via Vface_remapping_alist. This function is
4453 conservative: if something goes wrong, it will simply return FACE_ID
4454 rather than signal an error. */
4457 lookup_basic_face (struct frame *f, int face_id)
4459 Lisp_Object name, mapping;
4460 int remapped_face_id;
4462 if (NILP (Vface_remapping_alist))
4463 return face_id; /* Nothing to do. */
4465 switch (face_id)
4467 case DEFAULT_FACE_ID: name = Qdefault; break;
4468 case MODE_LINE_FACE_ID: name = Qmode_line; break;
4469 case MODE_LINE_INACTIVE_FACE_ID: name = Qmode_line_inactive; break;
4470 case HEADER_LINE_FACE_ID: name = Qheader_line; break;
4471 case TOOL_BAR_FACE_ID: name = Qtool_bar; break;
4472 case FRINGE_FACE_ID: name = Qfringe; break;
4473 case SCROLL_BAR_FACE_ID: name = Qscroll_bar; break;
4474 case BORDER_FACE_ID: name = Qborder; break;
4475 case CURSOR_FACE_ID: name = Qcursor; break;
4476 case MOUSE_FACE_ID: name = Qmouse; break;
4477 case MENU_FACE_ID: name = Qmenu; break;
4479 default:
4480 emacs_abort (); /* the caller is supposed to pass us a basic face id */
4483 /* Do a quick scan through Vface_remapping_alist, and return immediately
4484 if there is no remapping for face NAME. This is just an optimization
4485 for the very common no-remapping case. */
4486 mapping = assq_no_quit (name, Vface_remapping_alist);
4487 if (NILP (mapping))
4488 return face_id; /* Give up. */
4490 /* If there is a remapping entry, lookup the face using NAME, which will
4491 handle the remapping too. */
4492 remapped_face_id = lookup_named_face (f, name, false);
4493 if (remapped_face_id < 0)
4494 return face_id; /* Give up. */
4496 return remapped_face_id;
4500 /* Return a face for charset ASCII that is like the face with id
4501 FACE_ID on frame F, but has a font that is STEPS steps smaller.
4502 STEPS < 0 means larger. Value is the id of the face. */
4505 smaller_face (struct frame *f, int face_id, int steps)
4507 #ifdef HAVE_WINDOW_SYSTEM
4508 struct face *face;
4509 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4510 int pt, last_pt, last_height;
4511 int delta;
4512 int new_face_id;
4513 struct face *new_face;
4515 /* If not called for an X frame, just return the original face. */
4516 if (FRAME_TERMCAP_P (f))
4517 return face_id;
4519 /* Try in increments of 1/2 pt. */
4520 delta = steps < 0 ? 5 : -5;
4521 steps = eabs (steps);
4523 face = FACE_FROM_ID (f, face_id);
4524 memcpy (attrs, face->lface, sizeof attrs);
4525 pt = last_pt = XFASTINT (attrs[LFACE_HEIGHT_INDEX]);
4526 new_face_id = face_id;
4527 last_height = FONT_HEIGHT (face->font);
4529 while (steps
4530 && pt + delta > 0
4531 /* Give up if we cannot find a font within 10pt. */
4532 && eabs (last_pt - pt) < 100)
4534 /* Look up a face for a slightly smaller/larger font. */
4535 pt += delta;
4536 attrs[LFACE_HEIGHT_INDEX] = make_number (pt);
4537 new_face_id = lookup_face (f, attrs);
4538 new_face = FACE_FROM_ID (f, new_face_id);
4540 /* If height changes, count that as one step. */
4541 if ((delta < 0 && FONT_HEIGHT (new_face->font) < last_height)
4542 || (delta > 0 && FONT_HEIGHT (new_face->font) > last_height))
4544 --steps;
4545 last_height = FONT_HEIGHT (new_face->font);
4546 last_pt = pt;
4550 return new_face_id;
4552 #else /* not HAVE_WINDOW_SYSTEM */
4554 return face_id;
4556 #endif /* not HAVE_WINDOW_SYSTEM */
4560 /* Return a face for charset ASCII that is like the face with id
4561 FACE_ID on frame F, but has height HEIGHT. */
4564 face_with_height (struct frame *f, int face_id, int height)
4566 #ifdef HAVE_WINDOW_SYSTEM
4567 struct face *face;
4568 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4570 if (FRAME_TERMCAP_P (f)
4571 || height <= 0)
4572 return face_id;
4574 face = FACE_FROM_ID (f, face_id);
4575 memcpy (attrs, face->lface, sizeof attrs);
4576 attrs[LFACE_HEIGHT_INDEX] = make_number (height);
4577 font_clear_prop (attrs, FONT_SIZE_INDEX);
4578 face_id = lookup_face (f, attrs);
4579 #endif /* HAVE_WINDOW_SYSTEM */
4581 return face_id;
4585 /* Return the face id of the realized face for named face SYMBOL on
4586 frame F suitable for displaying ASCII characters, and use
4587 attributes of the face FACE_ID for attributes that aren't
4588 completely specified by SYMBOL. This is like lookup_named_face,
4589 except that the default attributes come from FACE_ID, not from the
4590 default face. FACE_ID is assumed to be already realized. */
4593 lookup_derived_face (struct frame *f, Lisp_Object symbol, int face_id,
4594 bool signal_p)
4596 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4597 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
4598 struct face *default_face = FACE_FROM_ID (f, face_id);
4600 if (!default_face)
4601 emacs_abort ();
4603 if (!get_lface_attributes (f, symbol, symbol_attrs, signal_p, 0))
4604 return -1;
4606 memcpy (attrs, default_face->lface, sizeof attrs);
4607 merge_face_vectors (f, symbol_attrs, attrs, 0);
4608 return lookup_face (f, attrs);
4611 DEFUN ("face-attributes-as-vector", Fface_attributes_as_vector,
4612 Sface_attributes_as_vector, 1, 1, 0,
4613 doc: /* Return a vector of face attributes corresponding to PLIST. */)
4614 (Lisp_Object plist)
4616 Lisp_Object lface;
4617 lface = Fmake_vector (make_number (LFACE_VECTOR_SIZE),
4618 Qunspecified);
4619 merge_face_ref (XFRAME (selected_frame), plist, XVECTOR (lface)->contents,
4620 true, 0);
4621 return lface;
4626 /***********************************************************************
4627 Face capability testing
4628 ***********************************************************************/
4631 /* If the distance (as returned by color_distance) between two colors is
4632 less than this, then they are considered the same, for determining
4633 whether a color is supported or not. The range of values is 0-65535. */
4635 #define TTY_SAME_COLOR_THRESHOLD 10000
4637 #ifdef HAVE_WINDOW_SYSTEM
4639 /* Return true if all the face attributes in ATTRS are supported
4640 on the window-system frame F.
4642 The definition of `supported' is somewhat heuristic, but basically means
4643 that a face containing all the attributes in ATTRS, when merged with the
4644 default face for display, can be represented in a way that's
4646 (1) different in appearance than the default face, and
4647 (2) `close in spirit' to what the attributes specify, if not exact. */
4649 static bool
4650 x_supports_face_attributes_p (struct frame *f,
4651 Lisp_Object attrs[LFACE_VECTOR_SIZE],
4652 struct face *def_face)
4654 Lisp_Object *def_attrs = def_face->lface;
4656 /* Check that other specified attributes are different that the default
4657 face. */
4658 if ((!UNSPECIFIEDP (attrs[LFACE_UNDERLINE_INDEX])
4659 && face_attr_equal_p (attrs[LFACE_UNDERLINE_INDEX],
4660 def_attrs[LFACE_UNDERLINE_INDEX]))
4661 || (!UNSPECIFIEDP (attrs[LFACE_INVERSE_INDEX])
4662 && face_attr_equal_p (attrs[LFACE_INVERSE_INDEX],
4663 def_attrs[LFACE_INVERSE_INDEX]))
4664 || (!UNSPECIFIEDP (attrs[LFACE_FOREGROUND_INDEX])
4665 && face_attr_equal_p (attrs[LFACE_FOREGROUND_INDEX],
4666 def_attrs[LFACE_FOREGROUND_INDEX]))
4667 || (!UNSPECIFIEDP (attrs[LFACE_DISTANT_FOREGROUND_INDEX])
4668 && face_attr_equal_p (attrs[LFACE_DISTANT_FOREGROUND_INDEX],
4669 def_attrs[LFACE_DISTANT_FOREGROUND_INDEX]))
4670 || (!UNSPECIFIEDP (attrs[LFACE_BACKGROUND_INDEX])
4671 && face_attr_equal_p (attrs[LFACE_BACKGROUND_INDEX],
4672 def_attrs[LFACE_BACKGROUND_INDEX]))
4673 || (!UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4674 && face_attr_equal_p (attrs[LFACE_STIPPLE_INDEX],
4675 def_attrs[LFACE_STIPPLE_INDEX]))
4676 || (!UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4677 && face_attr_equal_p (attrs[LFACE_OVERLINE_INDEX],
4678 def_attrs[LFACE_OVERLINE_INDEX]))
4679 || (!UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4680 && face_attr_equal_p (attrs[LFACE_STRIKE_THROUGH_INDEX],
4681 def_attrs[LFACE_STRIKE_THROUGH_INDEX]))
4682 || (!UNSPECIFIEDP (attrs[LFACE_BOX_INDEX])
4683 && face_attr_equal_p (attrs[LFACE_BOX_INDEX],
4684 def_attrs[LFACE_BOX_INDEX])))
4685 return false;
4687 /* Check font-related attributes, as those are the most commonly
4688 "unsupported" on a window-system (because of missing fonts). */
4689 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4690 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4691 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4692 || !UNSPECIFIEDP (attrs[LFACE_WEIGHT_INDEX])
4693 || !UNSPECIFIEDP (attrs[LFACE_SLANT_INDEX])
4694 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX]))
4696 int face_id;
4697 struct face *face;
4698 Lisp_Object merged_attrs[LFACE_VECTOR_SIZE];
4699 int i;
4701 memcpy (merged_attrs, def_attrs, sizeof merged_attrs);
4703 merge_face_vectors (f, attrs, merged_attrs, 0);
4705 face_id = lookup_face (f, merged_attrs);
4706 face = FACE_FROM_ID (f, face_id);
4708 if (! face)
4709 error ("Cannot make face");
4711 /* If the font is the same, or no font is found, then not
4712 supported. */
4713 if (face->font == def_face->font
4714 || ! face->font)
4715 return false;
4716 for (i = FONT_TYPE_INDEX; i <= FONT_SIZE_INDEX; i++)
4717 if (! EQ (face->font->props[i], def_face->font->props[i]))
4719 Lisp_Object s1, s2;
4721 if (i < FONT_FOUNDRY_INDEX || i > FONT_REGISTRY_INDEX
4722 || face->font->driver->case_sensitive)
4723 return true;
4724 s1 = SYMBOL_NAME (face->font->props[i]);
4725 s2 = SYMBOL_NAME (def_face->font->props[i]);
4726 if (! EQ (Fcompare_strings (s1, make_number (0), Qnil,
4727 s2, make_number (0), Qnil, Qt), Qt))
4728 return true;
4730 return false;
4733 /* Everything checks out, this face is supported. */
4734 return true;
4737 #endif /* HAVE_WINDOW_SYSTEM */
4739 /* Return true if all the face attributes in ATTRS are supported
4740 on the tty frame F.
4742 The definition of `supported' is somewhat heuristic, but basically means
4743 that a face containing all the attributes in ATTRS, when merged
4744 with the default face for display, can be represented in a way that's
4746 (1) different in appearance than the default face, and
4747 (2) `close in spirit' to what the attributes specify, if not exact.
4749 Point (2) implies that a `:weight black' attribute will be satisfied
4750 by any terminal that can display bold, and a `:foreground "yellow"' as
4751 long as the terminal can display a yellowish color, but `:slant italic'
4752 will _not_ be satisfied by the tty display code's automatic
4753 substitution of a `dim' face for italic. */
4755 static bool
4756 tty_supports_face_attributes_p (struct frame *f,
4757 Lisp_Object attrs[LFACE_VECTOR_SIZE],
4758 struct face *def_face)
4760 int weight, slant;
4761 Lisp_Object val, fg, bg;
4762 XColor fg_tty_color, fg_std_color;
4763 XColor bg_tty_color, bg_std_color;
4764 unsigned test_caps = 0;
4765 Lisp_Object *def_attrs = def_face->lface;
4767 /* First check some easy-to-check stuff; ttys support none of the
4768 following attributes, so we can just return false if any are requested
4769 (even if `nominal' values are specified, we should still return false,
4770 as that will be the same value that the default face uses). We
4771 consider :slant unsupportable on ttys, even though the face code
4772 actually `fakes' them using a dim attribute if possible. This is
4773 because the faked result is too different from what the face
4774 specifies. */
4775 if (!UNSPECIFIEDP (attrs[LFACE_FAMILY_INDEX])
4776 || !UNSPECIFIEDP (attrs[LFACE_FOUNDRY_INDEX])
4777 || !UNSPECIFIEDP (attrs[LFACE_STIPPLE_INDEX])
4778 || !UNSPECIFIEDP (attrs[LFACE_HEIGHT_INDEX])
4779 || !UNSPECIFIEDP (attrs[LFACE_SWIDTH_INDEX])
4780 || !UNSPECIFIEDP (attrs[LFACE_OVERLINE_INDEX])
4781 || !UNSPECIFIEDP (attrs[LFACE_STRIKE_THROUGH_INDEX])
4782 || !UNSPECIFIEDP (attrs[LFACE_BOX_INDEX]))
4783 return false;
4785 /* Test for terminal `capabilities' (non-color character attributes). */
4787 /* font weight (bold/dim) */
4788 val = attrs[LFACE_WEIGHT_INDEX];
4789 if (!UNSPECIFIEDP (val)
4790 && (weight = FONT_WEIGHT_NAME_NUMERIC (val), weight >= 0))
4792 int def_weight = FONT_WEIGHT_NAME_NUMERIC (def_attrs[LFACE_WEIGHT_INDEX]);
4794 if (weight > 100)
4796 if (def_weight > 100)
4797 return false; /* same as default */
4798 test_caps = TTY_CAP_BOLD;
4800 else if (weight < 100)
4802 if (def_weight < 100)
4803 return false; /* same as default */
4804 test_caps = TTY_CAP_DIM;
4806 else if (def_weight == 100)
4807 return false; /* same as default */
4810 /* font slant */
4811 val = attrs[LFACE_SLANT_INDEX];
4812 if (!UNSPECIFIEDP (val)
4813 && (slant = FONT_SLANT_NAME_NUMERIC (val), slant >= 0))
4815 int def_slant = FONT_SLANT_NAME_NUMERIC (def_attrs[LFACE_SLANT_INDEX]);
4816 if (slant == 100 || slant == def_slant)
4817 return false; /* same as default */
4818 else
4819 test_caps |= TTY_CAP_ITALIC;
4822 /* underlining */
4823 val = attrs[LFACE_UNDERLINE_INDEX];
4824 if (!UNSPECIFIEDP (val))
4826 if (STRINGP (val))
4827 return false; /* ttys can't use colored underlines */
4828 else if (EQ (CAR_SAFE (val), QCstyle) && EQ (CAR_SAFE (CDR_SAFE (val)), Qwave))
4829 return false; /* ttys can't use wave underlines */
4830 else if (face_attr_equal_p (val, def_attrs[LFACE_UNDERLINE_INDEX]))
4831 return false; /* same as default */
4832 else
4833 test_caps |= TTY_CAP_UNDERLINE;
4836 /* inverse video */
4837 val = attrs[LFACE_INVERSE_INDEX];
4838 if (!UNSPECIFIEDP (val))
4840 if (face_attr_equal_p (val, def_attrs[LFACE_INVERSE_INDEX]))
4841 return false; /* same as default */
4842 else
4843 test_caps |= TTY_CAP_INVERSE;
4847 /* Color testing. */
4849 /* Check if foreground color is close enough. */
4850 fg = attrs[LFACE_FOREGROUND_INDEX];
4851 if (STRINGP (fg))
4853 Lisp_Object def_fg = def_attrs[LFACE_FOREGROUND_INDEX];
4855 if (face_attr_equal_p (fg, def_fg))
4856 return false; /* same as default */
4857 else if (! tty_lookup_color (f, fg, &fg_tty_color, &fg_std_color))
4858 return false; /* not a valid color */
4859 else if (color_distance (&fg_tty_color, &fg_std_color)
4860 > TTY_SAME_COLOR_THRESHOLD)
4861 return false; /* displayed color is too different */
4862 else
4863 /* Make sure the color is really different than the default. */
4865 XColor def_fg_color;
4866 if (tty_lookup_color (f, def_fg, &def_fg_color, 0)
4867 && (color_distance (&fg_tty_color, &def_fg_color)
4868 <= TTY_SAME_COLOR_THRESHOLD))
4869 return false;
4873 /* Check if background color is close enough. */
4874 bg = attrs[LFACE_BACKGROUND_INDEX];
4875 if (STRINGP (bg))
4877 Lisp_Object def_bg = def_attrs[LFACE_BACKGROUND_INDEX];
4879 if (face_attr_equal_p (bg, def_bg))
4880 return false; /* same as default */
4881 else if (! tty_lookup_color (f, bg, &bg_tty_color, &bg_std_color))
4882 return false; /* not a valid color */
4883 else if (color_distance (&bg_tty_color, &bg_std_color)
4884 > TTY_SAME_COLOR_THRESHOLD)
4885 return false; /* displayed color is too different */
4886 else
4887 /* Make sure the color is really different than the default. */
4889 XColor def_bg_color;
4890 if (tty_lookup_color (f, def_bg, &def_bg_color, 0)
4891 && (color_distance (&bg_tty_color, &def_bg_color)
4892 <= TTY_SAME_COLOR_THRESHOLD))
4893 return false;
4897 /* If both foreground and background are requested, see if the
4898 distance between them is OK. We just check to see if the distance
4899 between the tty's foreground and background is close enough to the
4900 distance between the standard foreground and background. */
4901 if (STRINGP (fg) && STRINGP (bg))
4903 int delta_delta
4904 = (color_distance (&fg_std_color, &bg_std_color)
4905 - color_distance (&fg_tty_color, &bg_tty_color));
4906 if (delta_delta > TTY_SAME_COLOR_THRESHOLD
4907 || delta_delta < -TTY_SAME_COLOR_THRESHOLD)
4908 return false;
4912 /* See if the capabilities we selected above are supported, with the
4913 given colors. */
4914 return tty_capable_p (FRAME_TTY (f), test_caps);
4918 DEFUN ("display-supports-face-attributes-p",
4919 Fdisplay_supports_face_attributes_p, Sdisplay_supports_face_attributes_p,
4920 1, 2, 0,
4921 doc: /* Return non-nil if all the face attributes in ATTRIBUTES are supported.
4922 The optional argument DISPLAY can be a display name, a frame, or
4923 nil (meaning the selected frame's display).
4925 The definition of `supported' is somewhat heuristic, but basically means
4926 that a face containing all the attributes in ATTRIBUTES, when merged
4927 with the default face for display, can be represented in a way that's
4929 (1) different in appearance than the default face, and
4930 (2) `close in spirit' to what the attributes specify, if not exact.
4932 Point (2) implies that a `:weight black' attribute will be satisfied by
4933 any display that can display bold, and a `:foreground \"yellow\"' as long
4934 as it can display a yellowish color, but `:slant italic' will _not_ be
4935 satisfied by the tty display code's automatic substitution of a `dim'
4936 face for italic. */)
4937 (Lisp_Object attributes, Lisp_Object display)
4939 bool supports = false;
4940 int i;
4941 Lisp_Object frame;
4942 struct frame *f;
4943 struct face *def_face;
4944 Lisp_Object attrs[LFACE_VECTOR_SIZE];
4946 if (noninteractive || !initialized)
4947 /* We may not be able to access low-level face information in batch
4948 mode, or before being dumped, and this function is not going to
4949 be very useful in those cases anyway, so just give up. */
4950 return Qnil;
4952 if (NILP (display))
4953 frame = selected_frame;
4954 else if (FRAMEP (display))
4955 frame = display;
4956 else
4958 /* Find any frame on DISPLAY. */
4959 Lisp_Object tail;
4961 frame = Qnil;
4962 FOR_EACH_FRAME (tail, frame)
4963 if (!NILP (Fequal (Fcdr (Fassq (Qdisplay,
4964 XFRAME (frame)->param_alist)),
4965 display)))
4966 break;
4969 CHECK_LIVE_FRAME (frame);
4970 f = XFRAME (frame);
4972 for (i = 0; i < LFACE_VECTOR_SIZE; i++)
4973 attrs[i] = Qunspecified;
4974 merge_face_ref (f, attributes, attrs, true, 0);
4976 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4977 if (def_face == NULL)
4979 if (! realize_basic_faces (f))
4980 error ("Cannot realize default face");
4981 def_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4982 if (def_face == NULL)
4983 emacs_abort (); /* realize_basic_faces must have set it up */
4986 /* Dispatch to the appropriate handler. */
4987 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
4988 supports = tty_supports_face_attributes_p (f, attrs, def_face);
4989 #ifdef HAVE_WINDOW_SYSTEM
4990 else
4991 supports = x_supports_face_attributes_p (f, attrs, def_face);
4992 #endif
4994 return supports ? Qt : Qnil;
4998 /***********************************************************************
4999 Font selection
5000 ***********************************************************************/
5002 DEFUN ("internal-set-font-selection-order",
5003 Finternal_set_font_selection_order,
5004 Sinternal_set_font_selection_order, 1, 1, 0,
5005 doc: /* Set font selection order for face font selection to ORDER.
5006 ORDER must be a list of length 4 containing the symbols `:width',
5007 `:height', `:weight', and `:slant'. Face attributes appearing
5008 first in ORDER are matched first, e.g. if `:height' appears before
5009 `:weight' in ORDER, font selection first tries to find a font with
5010 a suitable height, and then tries to match the font weight.
5011 Value is ORDER. */)
5012 (Lisp_Object order)
5014 Lisp_Object list;
5015 int i;
5016 int indices[ARRAYELTS (font_sort_order)];
5018 CHECK_LIST (order);
5019 memset (indices, 0, sizeof indices);
5020 i = 0;
5022 for (list = order;
5023 CONSP (list) && i < ARRAYELTS (indices);
5024 list = XCDR (list), ++i)
5026 Lisp_Object attr = XCAR (list);
5027 int xlfd;
5029 if (EQ (attr, QCwidth))
5030 xlfd = XLFD_SWIDTH;
5031 else if (EQ (attr, QCheight))
5032 xlfd = XLFD_POINT_SIZE;
5033 else if (EQ (attr, QCweight))
5034 xlfd = XLFD_WEIGHT;
5035 else if (EQ (attr, QCslant))
5036 xlfd = XLFD_SLANT;
5037 else
5038 break;
5040 if (indices[i] != 0)
5041 break;
5042 indices[i] = xlfd;
5045 if (!NILP (list) || i != ARRAYELTS (indices))
5046 signal_error ("Invalid font sort order", order);
5047 for (i = 0; i < ARRAYELTS (font_sort_order); ++i)
5048 if (indices[i] == 0)
5049 signal_error ("Invalid font sort order", order);
5051 if (memcmp (indices, font_sort_order, sizeof indices) != 0)
5053 memcpy (font_sort_order, indices, sizeof font_sort_order);
5054 free_all_realized_faces (Qnil);
5057 font_update_sort_order (font_sort_order);
5059 return Qnil;
5063 DEFUN ("internal-set-alternative-font-family-alist",
5064 Finternal_set_alternative_font_family_alist,
5065 Sinternal_set_alternative_font_family_alist, 1, 1, 0,
5066 doc: /* Define alternative font families to try in face font selection.
5067 ALIST is an alist of (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5068 Each ALTERNATIVE is tried in order if no fonts of font family FAMILY can
5069 be found. Value is ALIST. */)
5070 (Lisp_Object alist)
5072 Lisp_Object entry, tail, tail2;
5074 CHECK_LIST (alist);
5075 alist = Fcopy_sequence (alist);
5076 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5078 entry = XCAR (tail);
5079 CHECK_LIST (entry);
5080 entry = Fcopy_sequence (entry);
5081 XSETCAR (tail, entry);
5082 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5083 XSETCAR (tail2, Fintern (XCAR (tail2), Qnil));
5086 Vface_alternative_font_family_alist = alist;
5087 free_all_realized_faces (Qnil);
5088 return alist;
5092 DEFUN ("internal-set-alternative-font-registry-alist",
5093 Finternal_set_alternative_font_registry_alist,
5094 Sinternal_set_alternative_font_registry_alist, 1, 1, 0,
5095 doc: /* Define alternative font registries to try in face font selection.
5096 ALIST is an alist of (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...) entries.
5097 Each ALTERNATIVE is tried in order if no fonts of font registry REGISTRY can
5098 be found. Value is ALIST. */)
5099 (Lisp_Object alist)
5101 Lisp_Object entry, tail, tail2;
5103 CHECK_LIST (alist);
5104 alist = Fcopy_sequence (alist);
5105 for (tail = alist; CONSP (tail); tail = XCDR (tail))
5107 entry = XCAR (tail);
5108 CHECK_LIST (entry);
5109 entry = Fcopy_sequence (entry);
5110 XSETCAR (tail, entry);
5111 for (tail2 = entry; CONSP (tail2); tail2 = XCDR (tail2))
5112 XSETCAR (tail2, Fdowncase (XCAR (tail2)));
5114 Vface_alternative_font_registry_alist = alist;
5115 free_all_realized_faces (Qnil);
5116 return alist;
5120 #ifdef HAVE_WINDOW_SYSTEM
5122 /* Return the fontset id of the base fontset name or alias name given
5123 by the fontset attribute of ATTRS. Value is -1 if the fontset
5124 attribute of ATTRS doesn't name a fontset. */
5126 static int
5127 face_fontset (Lisp_Object attrs[LFACE_VECTOR_SIZE])
5129 Lisp_Object name;
5131 name = attrs[LFACE_FONTSET_INDEX];
5132 if (!STRINGP (name))
5133 return -1;
5134 return fs_query_fontset (name, 0);
5137 #endif /* HAVE_WINDOW_SYSTEM */
5141 /***********************************************************************
5142 Face Realization
5143 ***********************************************************************/
5145 /* Realize basic faces on frame F. Value is zero if frame parameters
5146 of F don't contain enough information needed to realize the default
5147 face. */
5149 static bool
5150 realize_basic_faces (struct frame *f)
5152 bool success_p = false;
5154 /* Block input here so that we won't be surprised by an X expose
5155 event, for instance, without having the faces set up. */
5156 block_input ();
5158 if (realize_default_face (f))
5160 realize_named_face (f, Qmode_line, MODE_LINE_FACE_ID);
5161 realize_named_face (f, Qmode_line_inactive, MODE_LINE_INACTIVE_FACE_ID);
5162 realize_named_face (f, Qtool_bar, TOOL_BAR_FACE_ID);
5163 realize_named_face (f, Qfringe, FRINGE_FACE_ID);
5164 realize_named_face (f, Qheader_line, HEADER_LINE_FACE_ID);
5165 realize_named_face (f, Qscroll_bar, SCROLL_BAR_FACE_ID);
5166 realize_named_face (f, Qborder, BORDER_FACE_ID);
5167 realize_named_face (f, Qcursor, CURSOR_FACE_ID);
5168 realize_named_face (f, Qmouse, MOUSE_FACE_ID);
5169 realize_named_face (f, Qmenu, MENU_FACE_ID);
5170 realize_named_face (f, Qvertical_border, VERTICAL_BORDER_FACE_ID);
5171 realize_named_face (f, Qwindow_divider, WINDOW_DIVIDER_FACE_ID);
5172 realize_named_face (f, Qwindow_divider_first_pixel,
5173 WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
5174 realize_named_face (f, Qwindow_divider_last_pixel,
5175 WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
5177 /* Reflect changes in the `menu' face in menu bars. */
5178 if (FRAME_FACE_CACHE (f)->menu_face_changed_p)
5180 FRAME_FACE_CACHE (f)->menu_face_changed_p = false;
5181 #ifdef USE_X_TOOLKIT
5182 if (FRAME_WINDOW_P (f))
5183 x_update_menu_appearance (f);
5184 #endif
5187 success_p = true;
5190 unblock_input ();
5191 return success_p;
5195 /* Realize the default face on frame F. If the face is not fully
5196 specified, make it fully-specified. Attributes of the default face
5197 that are not explicitly specified are taken from frame parameters. */
5199 static bool
5200 realize_default_face (struct frame *f)
5202 struct face_cache *c = FRAME_FACE_CACHE (f);
5203 Lisp_Object lface;
5204 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5205 struct face *face;
5207 /* If the `default' face is not yet known, create it. */
5208 lface = lface_from_face_name (f, Qdefault, false);
5209 if (NILP (lface))
5211 Lisp_Object frame;
5212 XSETFRAME (frame, f);
5213 lface = Finternal_make_lisp_face (Qdefault, frame);
5216 #ifdef HAVE_WINDOW_SYSTEM
5217 if (FRAME_WINDOW_P (f))
5219 Lisp_Object font_object;
5221 XSETFONT (font_object, FRAME_FONT (f));
5222 set_lface_from_font (f, lface, font_object, f->default_face_done_p);
5223 ASET (lface, LFACE_FONTSET_INDEX, fontset_name (FRAME_FONTSET (f)));
5224 f->default_face_done_p = true;
5226 #endif /* HAVE_WINDOW_SYSTEM */
5228 if (!FRAME_WINDOW_P (f))
5230 ASET (lface, LFACE_FAMILY_INDEX, build_string ("default"));
5231 ASET (lface, LFACE_FOUNDRY_INDEX, LFACE_FAMILY (lface));
5232 ASET (lface, LFACE_SWIDTH_INDEX, Qnormal);
5233 ASET (lface, LFACE_HEIGHT_INDEX, make_number (1));
5234 if (UNSPECIFIEDP (LFACE_WEIGHT (lface)))
5235 ASET (lface, LFACE_WEIGHT_INDEX, Qnormal);
5236 if (UNSPECIFIEDP (LFACE_SLANT (lface)))
5237 ASET (lface, LFACE_SLANT_INDEX, Qnormal);
5238 if (UNSPECIFIEDP (LFACE_FONTSET (lface)))
5239 ASET (lface, LFACE_FONTSET_INDEX, Qnil);
5242 if (UNSPECIFIEDP (LFACE_UNDERLINE (lface)))
5243 ASET (lface, LFACE_UNDERLINE_INDEX, Qnil);
5245 if (UNSPECIFIEDP (LFACE_OVERLINE (lface)))
5246 ASET (lface, LFACE_OVERLINE_INDEX, Qnil);
5248 if (UNSPECIFIEDP (LFACE_STRIKE_THROUGH (lface)))
5249 ASET (lface, LFACE_STRIKE_THROUGH_INDEX, Qnil);
5251 if (UNSPECIFIEDP (LFACE_BOX (lface)))
5252 ASET (lface, LFACE_BOX_INDEX, Qnil);
5254 if (UNSPECIFIEDP (LFACE_INVERSE (lface)))
5255 ASET (lface, LFACE_INVERSE_INDEX, Qnil);
5257 if (UNSPECIFIEDP (LFACE_FOREGROUND (lface)))
5259 /* This function is called so early that colors are not yet
5260 set in the frame parameter list. */
5261 Lisp_Object color = Fassq (Qforeground_color, f->param_alist);
5263 if (CONSP (color) && STRINGP (XCDR (color)))
5264 ASET (lface, LFACE_FOREGROUND_INDEX, XCDR (color));
5265 else if (FRAME_WINDOW_P (f))
5266 return false;
5267 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5268 ASET (lface, LFACE_FOREGROUND_INDEX, build_string (unspecified_fg));
5269 else
5270 emacs_abort ();
5273 if (UNSPECIFIEDP (LFACE_BACKGROUND (lface)))
5275 /* This function is called so early that colors are not yet
5276 set in the frame parameter list. */
5277 Lisp_Object color = Fassq (Qbackground_color, f->param_alist);
5278 if (CONSP (color) && STRINGP (XCDR (color)))
5279 ASET (lface, LFACE_BACKGROUND_INDEX, XCDR (color));
5280 else if (FRAME_WINDOW_P (f))
5281 return false;
5282 else if (FRAME_INITIAL_P (f) || FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
5283 ASET (lface, LFACE_BACKGROUND_INDEX, build_string (unspecified_bg));
5284 else
5285 emacs_abort ();
5288 if (UNSPECIFIEDP (LFACE_STIPPLE (lface)))
5289 ASET (lface, LFACE_STIPPLE_INDEX, Qnil);
5291 /* Realize the face; it must be fully-specified now. */
5292 eassert (lface_fully_specified_p (XVECTOR (lface)->contents));
5293 check_lface (lface);
5294 memcpy (attrs, XVECTOR (lface)->contents, sizeof attrs);
5295 face = realize_face (c, attrs, DEFAULT_FACE_ID);
5297 #ifdef HAVE_WINDOW_SYSTEM
5298 #ifdef HAVE_X_WINDOWS
5299 if (FRAME_X_P (f) && face->font != FRAME_FONT (f))
5301 /* This can happen when making a frame on a display that does
5302 not support the default font. */
5303 if (!face->font)
5304 return false;
5306 /* Otherwise, the font specified for the frame was not
5307 acceptable as a font for the default face (perhaps because
5308 auto-scaled fonts are rejected), so we must adjust the frame
5309 font. */
5310 x_set_font (f, LFACE_FONT (lface), Qnil);
5312 #endif /* HAVE_X_WINDOWS */
5313 #endif /* HAVE_WINDOW_SYSTEM */
5314 return true;
5318 /* Realize basic faces other than the default face in face cache C.
5319 SYMBOL is the face name, ID is the face id the realized face must
5320 have. The default face must have been realized already. */
5322 static void
5323 realize_named_face (struct frame *f, Lisp_Object symbol, int id)
5325 struct face_cache *c = FRAME_FACE_CACHE (f);
5326 Lisp_Object lface = lface_from_face_name (f, symbol, false);
5327 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5328 Lisp_Object symbol_attrs[LFACE_VECTOR_SIZE];
5330 /* The default face must exist and be fully specified. */
5331 get_lface_attributes_no_remap (f, Qdefault, attrs, true);
5332 check_lface_attrs (attrs);
5333 eassert (lface_fully_specified_p (attrs));
5335 /* If SYMBOL isn't know as a face, create it. */
5336 if (NILP (lface))
5338 Lisp_Object frame;
5339 XSETFRAME (frame, f);
5340 lface = Finternal_make_lisp_face (symbol, frame);
5343 /* Merge SYMBOL's face with the default face. */
5344 get_lface_attributes_no_remap (f, symbol, symbol_attrs, true);
5345 merge_face_vectors (f, symbol_attrs, attrs, 0);
5347 /* Realize the face. */
5348 realize_face (c, attrs, id);
5352 /* Realize the fully-specified face with attributes ATTRS in face
5353 cache CACHE for ASCII characters. If FORMER_FACE_ID is
5354 non-negative, it is an ID of face to remove before caching the new
5355 face. Value is a pointer to the newly created realized face. */
5357 static struct face *
5358 realize_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE],
5359 int former_face_id)
5361 struct face *face;
5363 /* LFACE must be fully specified. */
5364 eassert (cache != NULL);
5365 check_lface_attrs (attrs);
5367 if (former_face_id >= 0 && cache->used > former_face_id)
5369 /* Remove the former face. */
5370 struct face *former_face = cache->faces_by_id[former_face_id];
5371 uncache_face (cache, former_face);
5372 free_realized_face (cache->f, former_face);
5373 SET_FRAME_GARBAGED (cache->f);
5376 if (FRAME_WINDOW_P (cache->f))
5377 face = realize_x_face (cache, attrs);
5378 else if (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f))
5379 face = realize_tty_face (cache, attrs);
5380 else if (FRAME_INITIAL_P (cache->f))
5382 /* Create a dummy face. */
5383 face = make_realized_face (attrs);
5385 else
5386 emacs_abort ();
5388 /* Insert the new face. */
5389 cache_face (cache, face, lface_hash (attrs));
5390 return face;
5394 #ifdef HAVE_WINDOW_SYSTEM
5395 /* Realize the fully-specified face that uses FONT-OBJECT and has the
5396 same attributes as BASE_FACE except for the font on frame F.
5397 FONT-OBJECT may be nil, in which case, realized a face of
5398 no-font. */
5400 static struct face *
5401 realize_non_ascii_face (struct frame *f, Lisp_Object font_object,
5402 struct face *base_face)
5404 struct face_cache *cache = FRAME_FACE_CACHE (f);
5405 struct face *face;
5407 face = xmalloc (sizeof *face);
5408 *face = *base_face;
5409 face->gc = 0;
5410 face->overstrike
5411 = (! NILP (font_object)
5412 && FONT_WEIGHT_NAME_NUMERIC (face->lface[LFACE_WEIGHT_INDEX]) > 100
5413 && FONT_WEIGHT_NUMERIC (font_object) <= 100);
5415 /* Don't try to free the colors copied bitwise from BASE_FACE. */
5416 face->colors_copied_bitwise_p = true;
5417 face->font = NILP (font_object) ? NULL : XFONT_OBJECT (font_object);
5418 face->gc = 0;
5420 cache_face (cache, face, face->hash);
5422 return face;
5424 #endif /* HAVE_WINDOW_SYSTEM */
5427 /* Realize the fully-specified face with attributes ATTRS in face
5428 cache CACHE for ASCII characters. Do it for X frame CACHE->f. If
5429 the new face doesn't share font with the default face, a fontname
5430 is allocated from the heap and set in `font_name' of the new face,
5431 but it is not yet loaded here. Value is a pointer to the newly
5432 created realized face. */
5434 static struct face *
5435 realize_x_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE])
5437 struct face *face = NULL;
5438 #ifdef HAVE_WINDOW_SYSTEM
5439 struct face *default_face;
5440 struct frame *f;
5441 Lisp_Object stipple, underline, overline, strike_through, box;
5443 eassert (FRAME_WINDOW_P (cache->f));
5445 /* Allocate a new realized face. */
5446 face = make_realized_face (attrs);
5447 face->ascii_face = face;
5449 f = cache->f;
5451 /* Determine the font to use. Most of the time, the font will be
5452 the same as the font of the default face, so try that first. */
5453 default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5454 if (default_face
5455 && lface_same_font_attributes_p (default_face->lface, attrs))
5457 face->font = default_face->font;
5458 face->fontset
5459 = make_fontset_for_ascii_face (f, default_face->fontset, face);
5461 else
5463 /* If the face attribute ATTRS specifies a fontset, use it as
5464 the base of a new realized fontset. Otherwise, use the same
5465 base fontset as of the default face. The base determines
5466 registry and encoding of a font. It may also determine
5467 foundry and family. The other fields of font name pattern
5468 are constructed from ATTRS. */
5469 int fontset = face_fontset (attrs);
5471 /* If we are realizing the default face, ATTRS should specify a
5472 fontset. In other words, if FONTSET is -1, we are not
5473 realizing the default face, thus the default face should have
5474 already been realized. */
5475 if (fontset == -1)
5477 if (default_face)
5478 fontset = default_face->fontset;
5479 if (fontset == -1)
5480 emacs_abort ();
5482 if (! FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5483 attrs[LFACE_FONT_INDEX]
5484 = font_load_for_lface (f, attrs, Ffont_spec (0, NULL));
5485 if (FONT_OBJECT_P (attrs[LFACE_FONT_INDEX]))
5487 face->font = XFONT_OBJECT (attrs[LFACE_FONT_INDEX]);
5488 face->fontset = make_fontset_for_ascii_face (f, fontset, face);
5490 else
5492 face->font = NULL;
5493 face->fontset = -1;
5497 if (face->font
5498 && FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]) > 100
5499 && FONT_WEIGHT_NUMERIC (attrs[LFACE_FONT_INDEX]) <= 100)
5500 face->overstrike = true;
5502 /* Load colors, and set remaining attributes. */
5504 load_face_colors (f, face, attrs);
5506 /* Set up box. */
5507 box = attrs[LFACE_BOX_INDEX];
5508 if (STRINGP (box))
5510 /* A simple box of line width 1 drawn in color given by
5511 the string. */
5512 face->box_color = load_color (f, face, attrs[LFACE_BOX_INDEX],
5513 LFACE_BOX_INDEX);
5514 face->box = FACE_SIMPLE_BOX;
5515 face->box_line_width = 1;
5517 else if (INTEGERP (box))
5519 /* Simple box of specified line width in foreground color of the
5520 face. */
5521 eassert (XINT (box) != 0);
5522 face->box = FACE_SIMPLE_BOX;
5523 face->box_line_width = XINT (box);
5524 face->box_color = face->foreground;
5525 face->box_color_defaulted_p = true;
5527 else if (CONSP (box))
5529 /* `(:width WIDTH :color COLOR :shadow SHADOW)'. SHADOW
5530 being one of `raised' or `sunken'. */
5531 face->box = FACE_SIMPLE_BOX;
5532 face->box_color = face->foreground;
5533 face->box_color_defaulted_p = true;
5534 face->box_line_width = 1;
5536 while (CONSP (box))
5538 Lisp_Object keyword, value;
5540 keyword = XCAR (box);
5541 box = XCDR (box);
5543 if (!CONSP (box))
5544 break;
5545 value = XCAR (box);
5546 box = XCDR (box);
5548 if (EQ (keyword, QCline_width))
5550 if (INTEGERP (value) && XINT (value) != 0)
5551 face->box_line_width = XINT (value);
5553 else if (EQ (keyword, QCcolor))
5555 if (STRINGP (value))
5557 face->box_color = load_color (f, face, value,
5558 LFACE_BOX_INDEX);
5559 face->use_box_color_for_shadows_p = true;
5562 else if (EQ (keyword, QCstyle))
5564 if (EQ (value, Qreleased_button))
5565 face->box = FACE_RAISED_BOX;
5566 else if (EQ (value, Qpressed_button))
5567 face->box = FACE_SUNKEN_BOX;
5572 /* Text underline, overline, strike-through. */
5574 underline = attrs[LFACE_UNDERLINE_INDEX];
5575 if (EQ (underline, Qt))
5577 /* Use default color (same as foreground color). */
5578 face->underline_p = true;
5579 face->underline_type = FACE_UNDER_LINE;
5580 face->underline_defaulted_p = true;
5581 face->underline_color = 0;
5583 else if (STRINGP (underline))
5585 /* Use specified color. */
5586 face->underline_p = true;
5587 face->underline_type = FACE_UNDER_LINE;
5588 face->underline_defaulted_p = false;
5589 face->underline_color
5590 = load_color (f, face, underline,
5591 LFACE_UNDERLINE_INDEX);
5593 else if (NILP (underline))
5595 face->underline_p = false;
5596 face->underline_defaulted_p = false;
5597 face->underline_color = 0;
5599 else if (CONSP (underline))
5601 /* `(:color COLOR :style STYLE)'.
5602 STYLE being one of `line' or `wave'. */
5603 face->underline_p = true;
5604 face->underline_color = 0;
5605 face->underline_defaulted_p = true;
5606 face->underline_type = FACE_UNDER_LINE;
5608 /* FIXME? This is also not robust about checking the precise form.
5609 See comments in Finternal_set_lisp_face_attribute. */
5610 while (CONSP (underline))
5612 Lisp_Object keyword, value;
5614 keyword = XCAR (underline);
5615 underline = XCDR (underline);
5617 if (!CONSP (underline))
5618 break;
5619 value = XCAR (underline);
5620 underline = XCDR (underline);
5622 if (EQ (keyword, QCcolor))
5624 if (EQ (value, Qforeground_color))
5626 face->underline_defaulted_p = true;
5627 face->underline_color = 0;
5629 else if (STRINGP (value))
5631 face->underline_defaulted_p = false;
5632 face->underline_color = load_color (f, face, value,
5633 LFACE_UNDERLINE_INDEX);
5636 else if (EQ (keyword, QCstyle))
5638 if (EQ (value, Qline))
5639 face->underline_type = FACE_UNDER_LINE;
5640 else if (EQ (value, Qwave))
5641 face->underline_type = FACE_UNDER_WAVE;
5646 overline = attrs[LFACE_OVERLINE_INDEX];
5647 if (STRINGP (overline))
5649 face->overline_color
5650 = load_color (f, face, attrs[LFACE_OVERLINE_INDEX],
5651 LFACE_OVERLINE_INDEX);
5652 face->overline_p = true;
5654 else if (EQ (overline, Qt))
5656 face->overline_color = face->foreground;
5657 face->overline_color_defaulted_p = true;
5658 face->overline_p = true;
5661 strike_through = attrs[LFACE_STRIKE_THROUGH_INDEX];
5662 if (STRINGP (strike_through))
5664 face->strike_through_color
5665 = load_color (f, face, attrs[LFACE_STRIKE_THROUGH_INDEX],
5666 LFACE_STRIKE_THROUGH_INDEX);
5667 face->strike_through_p = true;
5669 else if (EQ (strike_through, Qt))
5671 face->strike_through_color = face->foreground;
5672 face->strike_through_color_defaulted_p = true;
5673 face->strike_through_p = true;
5676 stipple = attrs[LFACE_STIPPLE_INDEX];
5677 if (!NILP (stipple))
5678 face->stipple = load_pixmap (f, stipple);
5679 #endif /* HAVE_WINDOW_SYSTEM */
5681 return face;
5685 /* Map a specified color of face FACE on frame F to a tty color index.
5686 IDX is either LFACE_FOREGROUND_INDEX or LFACE_BACKGROUND_INDEX, and
5687 specifies which color to map. Set *DEFAULTED to true if mapping to the
5688 default foreground/background colors. */
5690 static void
5691 map_tty_color (struct frame *f, struct face *face,
5692 enum lface_attribute_index idx, bool *defaulted)
5694 Lisp_Object frame, color, def;
5695 bool foreground_p = idx == LFACE_FOREGROUND_INDEX;
5696 unsigned long default_pixel =
5697 foreground_p ? FACE_TTY_DEFAULT_FG_COLOR : FACE_TTY_DEFAULT_BG_COLOR;
5698 unsigned long pixel = default_pixel;
5699 #ifdef MSDOS
5700 unsigned long default_other_pixel =
5701 foreground_p ? FACE_TTY_DEFAULT_BG_COLOR : FACE_TTY_DEFAULT_FG_COLOR;
5702 #endif
5704 eassert (idx == LFACE_FOREGROUND_INDEX || idx == LFACE_BACKGROUND_INDEX);
5706 XSETFRAME (frame, f);
5707 color = face->lface[idx];
5709 if (STRINGP (color)
5710 && SCHARS (color)
5711 && CONSP (Vtty_defined_color_alist)
5712 && (def = assoc_no_quit (color, call1 (Qtty_color_alist, frame)),
5713 CONSP (def)))
5715 /* Associations in tty-defined-color-alist are of the form
5716 (NAME INDEX R G B). We need the INDEX part. */
5717 pixel = XINT (XCAR (XCDR (def)));
5720 if (pixel == default_pixel && STRINGP (color))
5722 pixel = load_color (f, face, color, idx);
5724 #ifdef MSDOS
5725 /* If the foreground of the default face is the default color,
5726 use the foreground color defined by the frame. */
5727 if (FRAME_MSDOS_P (f))
5729 if (pixel == default_pixel
5730 || pixel == FACE_TTY_DEFAULT_COLOR)
5732 if (foreground_p)
5733 pixel = FRAME_FOREGROUND_PIXEL (f);
5734 else
5735 pixel = FRAME_BACKGROUND_PIXEL (f);
5736 face->lface[idx] = tty_color_name (f, pixel);
5737 *defaulted = true;
5739 else if (pixel == default_other_pixel)
5741 if (foreground_p)
5742 pixel = FRAME_BACKGROUND_PIXEL (f);
5743 else
5744 pixel = FRAME_FOREGROUND_PIXEL (f);
5745 face->lface[idx] = tty_color_name (f, pixel);
5746 *defaulted = true;
5749 #endif /* MSDOS */
5752 if (foreground_p)
5753 face->foreground = pixel;
5754 else
5755 face->background = pixel;
5759 /* Realize the fully-specified face with attributes ATTRS in face
5760 cache CACHE for ASCII characters. Do it for TTY frame CACHE->f.
5761 Value is a pointer to the newly created realized face. */
5763 static struct face *
5764 realize_tty_face (struct face_cache *cache,
5765 Lisp_Object attrs[LFACE_VECTOR_SIZE])
5767 struct face *face;
5768 int weight, slant;
5769 bool face_colors_defaulted = false;
5770 struct frame *f = cache->f;
5772 /* Frame must be a termcap frame. */
5773 eassert (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f));
5775 /* Allocate a new realized face. */
5776 face = make_realized_face (attrs);
5777 #if false
5778 face->font_name = FRAME_MSDOS_P (cache->f) ? "ms-dos" : "tty";
5779 #endif
5781 /* Map face attributes to TTY appearances. */
5782 weight = FONT_WEIGHT_NAME_NUMERIC (attrs[LFACE_WEIGHT_INDEX]);
5783 slant = FONT_SLANT_NAME_NUMERIC (attrs[LFACE_SLANT_INDEX]);
5784 if (weight > 100)
5785 face->tty_bold_p = true;
5786 if (slant != 100)
5787 face->tty_italic_p = true;
5788 if (!NILP (attrs[LFACE_UNDERLINE_INDEX]))
5789 face->tty_underline_p = true;
5790 if (!NILP (attrs[LFACE_INVERSE_INDEX]))
5791 face->tty_reverse_p = true;
5793 /* Map color names to color indices. */
5794 map_tty_color (f, face, LFACE_FOREGROUND_INDEX, &face_colors_defaulted);
5795 map_tty_color (f, face, LFACE_BACKGROUND_INDEX, &face_colors_defaulted);
5797 /* Swap colors if face is inverse-video. If the colors are taken
5798 from the frame colors, they are already inverted, since the
5799 frame-creation function calls x-handle-reverse-video. */
5800 if (face->tty_reverse_p && !face_colors_defaulted)
5802 unsigned long tem = face->foreground;
5803 face->foreground = face->background;
5804 face->background = tem;
5807 if (tty_suppress_bold_inverse_default_colors_p
5808 && face->tty_bold_p
5809 && face->background == FACE_TTY_DEFAULT_FG_COLOR
5810 && face->foreground == FACE_TTY_DEFAULT_BG_COLOR)
5811 face->tty_bold_p = false;
5813 return face;
5817 DEFUN ("tty-suppress-bold-inverse-default-colors",
5818 Ftty_suppress_bold_inverse_default_colors,
5819 Stty_suppress_bold_inverse_default_colors, 1, 1, 0,
5820 doc: /* Suppress/allow boldness of faces with inverse default colors.
5821 SUPPRESS non-nil means suppress it.
5822 This affects bold faces on TTYs whose foreground is the default background
5823 color of the display and whose background is the default foreground color.
5824 For such faces, the bold face attribute is ignored if this variable
5825 is non-nil. */)
5826 (Lisp_Object suppress)
5828 tty_suppress_bold_inverse_default_colors_p = !NILP (suppress);
5829 face_change = true;
5830 return suppress;
5835 /***********************************************************************
5836 Computing Faces
5837 ***********************************************************************/
5839 /* Return the ID of the face to use to display character CH with face
5840 property PROP on frame F in current_buffer. */
5843 compute_char_face (struct frame *f, int ch, Lisp_Object prop)
5845 int face_id;
5847 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
5848 ch = 0;
5850 if (NILP (prop))
5852 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5853 face_id = FACE_FOR_CHAR (f, face, ch, -1, Qnil);
5855 else
5857 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5858 struct face *default_face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5859 memcpy (attrs, default_face->lface, sizeof attrs);
5860 merge_face_ref (f, prop, attrs, true, 0);
5861 face_id = lookup_face (f, attrs);
5864 return face_id;
5867 /* Return the face ID associated with buffer position POS for
5868 displaying ASCII characters. Return in *ENDPTR the position at
5869 which a different face is needed, as far as text properties and
5870 overlays are concerned. W is a window displaying current_buffer.
5872 REGION_BEG, REGION_END delimit the region, so it can be
5873 highlighted.
5875 LIMIT is a position not to scan beyond. That is to limit the time
5876 this function can take.
5878 If MOUSE, use the character's mouse-face, not its face.
5880 BASE_FACE_ID, if non-negative, specifies a base face id to use
5881 instead of DEFAULT_FACE_ID.
5883 The face returned is suitable for displaying ASCII characters. */
5886 face_at_buffer_position (struct window *w, ptrdiff_t pos,
5887 ptrdiff_t *endptr, ptrdiff_t limit,
5888 bool mouse, int base_face_id)
5890 struct frame *f = XFRAME (w->frame);
5891 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5892 Lisp_Object prop, position;
5893 ptrdiff_t i, noverlays;
5894 Lisp_Object *overlay_vec;
5895 ptrdiff_t endpos;
5896 Lisp_Object propname = mouse ? Qmouse_face : Qface;
5897 Lisp_Object limit1, end;
5898 struct face *default_face;
5900 /* W must display the current buffer. We could write this function
5901 to use the frame and buffer of W, but right now it doesn't. */
5902 /* eassert (XBUFFER (w->contents) == current_buffer); */
5904 XSETFASTINT (position, pos);
5906 endpos = ZV;
5908 /* Get the `face' or `mouse_face' text property at POS, and
5909 determine the next position at which the property changes. */
5910 prop = Fget_text_property (position, propname, w->contents);
5911 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
5912 end = Fnext_single_property_change (position, propname, w->contents, limit1);
5913 if (INTEGERP (end))
5914 endpos = XINT (end);
5916 /* Look at properties from overlays. */
5917 USE_SAFE_ALLOCA;
5919 ptrdiff_t next_overlay;
5921 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, &next_overlay, false);
5922 if (next_overlay < endpos)
5923 endpos = next_overlay;
5926 *endptr = endpos;
5929 int face_id;
5931 if (base_face_id >= 0)
5932 face_id = base_face_id;
5933 else if (NILP (Vface_remapping_alist))
5934 face_id = DEFAULT_FACE_ID;
5935 else
5936 face_id = lookup_basic_face (f, DEFAULT_FACE_ID);
5938 default_face = FACE_FROM_ID (f, face_id);
5941 /* Optimize common cases where we can use the default face. */
5942 if (noverlays == 0
5943 && NILP (prop))
5945 SAFE_FREE ();
5946 return default_face->id;
5949 /* Begin with attributes from the default face. */
5950 memcpy (attrs, default_face->lface, sizeof attrs);
5952 /* Merge in attributes specified via text properties. */
5953 if (!NILP (prop))
5954 merge_face_ref (f, prop, attrs, true, 0);
5956 /* Now merge the overlay data. */
5957 noverlays = sort_overlays (overlay_vec, noverlays, w);
5958 for (i = 0; i < noverlays; i++)
5960 Lisp_Object oend;
5961 ptrdiff_t oendpos;
5963 prop = Foverlay_get (overlay_vec[i], propname);
5964 if (!NILP (prop))
5965 merge_face_ref (f, prop, attrs, true, 0);
5967 oend = OVERLAY_END (overlay_vec[i]);
5968 oendpos = OVERLAY_POSITION (oend);
5969 if (oendpos < endpos)
5970 endpos = oendpos;
5973 *endptr = endpos;
5975 SAFE_FREE ();
5977 /* Look up a realized face with the given face attributes,
5978 or realize a new one for ASCII characters. */
5979 return lookup_face (f, attrs);
5982 /* Return the face ID at buffer position POS for displaying ASCII
5983 characters associated with overlay strings for overlay OVERLAY.
5985 Like face_at_buffer_position except for OVERLAY. Currently it
5986 simply disregards the `face' properties of all overlays. */
5989 face_for_overlay_string (struct window *w, ptrdiff_t pos,
5990 ptrdiff_t *endptr, ptrdiff_t limit,
5991 bool mouse, Lisp_Object overlay)
5993 struct frame *f = XFRAME (w->frame);
5994 Lisp_Object attrs[LFACE_VECTOR_SIZE];
5995 Lisp_Object prop, position;
5996 ptrdiff_t endpos;
5997 Lisp_Object propname = mouse ? Qmouse_face : Qface;
5998 Lisp_Object limit1, end;
5999 struct face *default_face;
6001 /* W must display the current buffer. We could write this function
6002 to use the frame and buffer of W, but right now it doesn't. */
6003 /* eassert (XBUFFER (w->contents) == current_buffer); */
6005 XSETFASTINT (position, pos);
6007 endpos = ZV;
6009 /* Get the `face' or `mouse_face' text property at POS, and
6010 determine the next position at which the property changes. */
6011 prop = Fget_text_property (position, propname, w->contents);
6012 XSETFASTINT (limit1, (limit < endpos ? limit : endpos));
6013 end = Fnext_single_property_change (position, propname, w->contents, limit1);
6014 if (INTEGERP (end))
6015 endpos = XINT (end);
6017 *endptr = endpos;
6019 /* Optimize common case where we can use the default face. */
6020 if (NILP (prop)
6021 && NILP (Vface_remapping_alist))
6022 return DEFAULT_FACE_ID;
6024 /* Begin with attributes from the default face. */
6025 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
6026 memcpy (attrs, default_face->lface, sizeof attrs);
6028 /* Merge in attributes specified via text properties. */
6029 if (!NILP (prop))
6030 merge_face_ref (f, prop, attrs, true, 0);
6032 *endptr = endpos;
6034 /* Look up a realized face with the given face attributes,
6035 or realize a new one for ASCII characters. */
6036 return lookup_face (f, attrs);
6040 /* Compute the face at character position POS in Lisp string STRING on
6041 window W, for ASCII characters.
6043 If STRING is an overlay string, it comes from position BUFPOS in
6044 current_buffer, otherwise BUFPOS is zero to indicate that STRING is
6045 not an overlay string. W must display the current buffer.
6046 REGION_BEG and REGION_END give the start and end positions of the
6047 region; both are -1 if no region is visible.
6049 BASE_FACE_ID is the id of a face to merge with. For strings coming
6050 from overlays or the `display' property it is the face at BUFPOS.
6052 If MOUSE_P, use the character's mouse-face, not its face.
6054 Set *ENDPTR to the next position where to check for faces in
6055 STRING; -1 if the face is constant from POS to the end of the
6056 string.
6058 Value is the id of the face to use. The face returned is suitable
6059 for displaying ASCII characters. */
6062 face_at_string_position (struct window *w, Lisp_Object string,
6063 ptrdiff_t pos, ptrdiff_t bufpos,
6064 ptrdiff_t *endptr, enum face_id base_face_id,
6065 bool mouse_p)
6067 Lisp_Object prop, position, end, limit;
6068 struct frame *f = XFRAME (WINDOW_FRAME (w));
6069 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6070 struct face *base_face;
6071 bool multibyte_p = STRING_MULTIBYTE (string);
6072 Lisp_Object prop_name = mouse_p ? Qmouse_face : Qface;
6074 /* Get the value of the face property at the current position within
6075 STRING. Value is nil if there is no face property. */
6076 XSETFASTINT (position, pos);
6077 prop = Fget_text_property (position, prop_name, string);
6079 /* Get the next position at which to check for faces. Value of end
6080 is nil if face is constant all the way to the end of the string.
6081 Otherwise it is a string position where to check faces next.
6082 Limit is the maximum position up to which to check for property
6083 changes in Fnext_single_property_change. Strings are usually
6084 short, so set the limit to the end of the string. */
6085 XSETFASTINT (limit, SCHARS (string));
6086 end = Fnext_single_property_change (position, prop_name, string, limit);
6087 if (INTEGERP (end))
6088 *endptr = XFASTINT (end);
6089 else
6090 *endptr = -1;
6092 base_face = FACE_FROM_ID (f, base_face_id);
6093 eassert (base_face);
6095 /* Optimize the default case that there is no face property. */
6096 if (NILP (prop)
6097 && (multibyte_p
6098 /* We can't realize faces for different charsets differently
6099 if we don't have fonts, so we can stop here if not working
6100 on a window-system frame. */
6101 || !FRAME_WINDOW_P (f)
6102 || FACE_SUITABLE_FOR_ASCII_CHAR_P (base_face, 0)))
6103 return base_face->id;
6105 /* Begin with attributes from the base face. */
6106 memcpy (attrs, base_face->lface, sizeof attrs);
6108 /* Merge in attributes specified via text properties. */
6109 if (!NILP (prop))
6110 merge_face_ref (f, prop, attrs, true, 0);
6112 /* Look up a realized face with the given face attributes,
6113 or realize a new one for ASCII characters. */
6114 return lookup_face (f, attrs);
6118 /* Merge a face into a realized face.
6120 F is frame where faces are (to be) realized.
6122 FACE_NAME is named face to merge.
6124 If FACE_NAME is nil, FACE_ID is face_id of realized face to merge.
6126 If FACE_NAME is t, FACE_ID is lface_id of face to merge.
6128 BASE_FACE_ID is realized face to merge into.
6130 Return new face id.
6134 merge_faces (struct frame *f, Lisp_Object face_name, int face_id,
6135 int base_face_id)
6137 Lisp_Object attrs[LFACE_VECTOR_SIZE];
6138 struct face *base_face;
6140 base_face = FACE_FROM_ID (f, base_face_id);
6141 if (!base_face)
6142 return base_face_id;
6144 if (EQ (face_name, Qt))
6146 if (face_id < 0 || face_id >= lface_id_to_name_size)
6147 return base_face_id;
6148 face_name = lface_id_to_name[face_id];
6149 /* When called during make-frame, lookup_derived_face may fail
6150 if the faces are uninitialized. Don't signal an error. */
6151 face_id = lookup_derived_face (f, face_name, base_face_id, 0);
6152 return (face_id >= 0 ? face_id : base_face_id);
6155 /* Begin with attributes from the base face. */
6156 memcpy (attrs, base_face->lface, sizeof attrs);
6158 if (!NILP (face_name))
6160 if (!merge_named_face (f, face_name, attrs, 0))
6161 return base_face_id;
6163 else
6165 struct face *face;
6166 if (face_id < 0)
6167 return base_face_id;
6168 face = FACE_FROM_ID (f, face_id);
6169 if (!face)
6170 return base_face_id;
6171 merge_face_vectors (f, face->lface, attrs, 0);
6174 /* Look up a realized face with the given face attributes,
6175 or realize a new one for ASCII characters. */
6176 return lookup_face (f, attrs);
6181 #ifndef HAVE_X_WINDOWS
6182 DEFUN ("x-load-color-file", Fx_load_color_file,
6183 Sx_load_color_file, 1, 1, 0,
6184 doc: /* Create an alist of color entries from an external file.
6186 The file should define one named RGB color per line like so:
6187 R G B name
6188 where R,G,B are numbers between 0 and 255 and name is an arbitrary string. */)
6189 (Lisp_Object filename)
6191 FILE *fp;
6192 Lisp_Object cmap = Qnil;
6193 Lisp_Object abspath;
6195 CHECK_STRING (filename);
6196 abspath = Fexpand_file_name (filename, Qnil);
6198 block_input ();
6199 fp = emacs_fopen (SSDATA (abspath), "r" FOPEN_TEXT);
6200 if (fp)
6202 char buf[512];
6203 int red, green, blue;
6204 int num;
6206 while (fgets (buf, sizeof (buf), fp) != NULL) {
6207 if (sscanf (buf, "%u %u %u %n", &red, &green, &blue, &num) == 3)
6209 #ifdef HAVE_NTGUI
6210 int color = RGB (red, green, blue);
6211 #else
6212 int color = (red << 16) | (green << 8) | blue;
6213 #endif
6214 char *name = buf + num;
6215 ptrdiff_t len = strlen (name);
6216 len -= 0 < len && name[len - 1] == '\n';
6217 cmap = Fcons (Fcons (make_string (name, len), make_number (color)),
6218 cmap);
6221 fclose (fp);
6223 unblock_input ();
6224 return cmap;
6226 #endif
6229 /***********************************************************************
6230 Tests
6231 ***********************************************************************/
6233 #ifdef GLYPH_DEBUG
6235 /* Print the contents of the realized face FACE to stderr. */
6237 static void
6238 dump_realized_face (struct face *face)
6240 fprintf (stderr, "ID: %d\n", face->id);
6241 #ifdef HAVE_X_WINDOWS
6242 fprintf (stderr, "gc: %p\n", face->gc);
6243 #endif
6244 fprintf (stderr, "foreground: 0x%lx (%s)\n",
6245 face->foreground,
6246 SDATA (face->lface[LFACE_FOREGROUND_INDEX]));
6247 fprintf (stderr, "background: 0x%lx (%s)\n",
6248 face->background,
6249 SDATA (face->lface[LFACE_BACKGROUND_INDEX]));
6250 if (face->font)
6251 fprintf (stderr, "font_name: %s (%s)\n",
6252 SDATA (face->font->props[FONT_NAME_INDEX]),
6253 SDATA (face->lface[LFACE_FAMILY_INDEX]));
6254 #ifdef HAVE_X_WINDOWS
6255 fprintf (stderr, "font = %p\n", face->font);
6256 #endif
6257 fprintf (stderr, "fontset: %d\n", face->fontset);
6258 fprintf (stderr, "underline: %d (%s)\n",
6259 face->underline_p,
6260 SDATA (Fsymbol_name (face->lface[LFACE_UNDERLINE_INDEX])));
6261 fprintf (stderr, "hash: %d\n", face->hash);
6265 DEFUN ("dump-face", Fdump_face, Sdump_face, 0, 1, 0, doc: /* */)
6266 (Lisp_Object n)
6268 if (NILP (n))
6270 int i;
6272 fprintf (stderr, "font selection order: ");
6273 for (i = 0; i < ARRAYELTS (font_sort_order); ++i)
6274 fprintf (stderr, "%d ", font_sort_order[i]);
6275 fprintf (stderr, "\n");
6277 fprintf (stderr, "alternative fonts: ");
6278 debug_print (Vface_alternative_font_family_alist);
6279 fprintf (stderr, "\n");
6281 for (i = 0; i < FRAME_FACE_CACHE (SELECTED_FRAME ())->used; ++i)
6282 Fdump_face (make_number (i));
6284 else
6286 struct face *face;
6287 CHECK_NUMBER (n);
6288 face = FACE_FROM_ID (SELECTED_FRAME (), XINT (n));
6289 if (face == NULL)
6290 error ("Not a valid face");
6291 dump_realized_face (face);
6294 return Qnil;
6298 DEFUN ("show-face-resources", Fshow_face_resources, Sshow_face_resources,
6299 0, 0, 0, doc: /* */)
6300 (void)
6302 fprintf (stderr, "number of colors = %d\n", ncolors_allocated);
6303 fprintf (stderr, "number of pixmaps = %d\n", npixmaps_allocated);
6304 fprintf (stderr, "number of GCs = %d\n", ngcs);
6305 return Qnil;
6308 #endif /* GLYPH_DEBUG */
6312 /***********************************************************************
6313 Initialization
6314 ***********************************************************************/
6316 void
6317 syms_of_xfaces (void)
6319 /* The symbols `face' and `mouse-face' used as text properties. */
6320 DEFSYM (Qface, "face");
6322 /* Property for basic faces which other faces cannot inherit. */
6323 DEFSYM (Qface_no_inherit, "face-no-inherit");
6325 /* Error symbol for wrong_type_argument in load_pixmap. */
6326 DEFSYM (Qbitmap_spec_p, "bitmap-spec-p");
6328 /* The name of the function to call when the background of the frame
6329 has changed, frame_set_background_mode. */
6330 DEFSYM (Qframe_set_background_mode, "frame-set-background-mode");
6332 /* Lisp face attribute keywords. */
6333 DEFSYM (QCfamily, ":family");
6334 DEFSYM (QCheight, ":height");
6335 DEFSYM (QCweight, ":weight");
6336 DEFSYM (QCslant, ":slant");
6337 DEFSYM (QCunderline, ":underline");
6338 DEFSYM (QCinverse_video, ":inverse-video");
6339 DEFSYM (QCreverse_video, ":reverse-video");
6340 DEFSYM (QCforeground, ":foreground");
6341 DEFSYM (QCbackground, ":background");
6342 DEFSYM (QCstipple, ":stipple");
6343 DEFSYM (QCwidth, ":width");
6344 DEFSYM (QCfont, ":font");
6345 DEFSYM (QCfontset, ":fontset");
6346 DEFSYM (QCdistant_foreground, ":distant-foreground");
6347 DEFSYM (QCbold, ":bold");
6348 DEFSYM (QCitalic, ":italic");
6349 DEFSYM (QCoverline, ":overline");
6350 DEFSYM (QCstrike_through, ":strike-through");
6351 DEFSYM (QCbox, ":box");
6352 DEFSYM (QCinherit, ":inherit");
6354 /* Symbols used for Lisp face attribute values. */
6355 DEFSYM (QCcolor, ":color");
6356 DEFSYM (QCline_width, ":line-width");
6357 DEFSYM (QCstyle, ":style");
6358 DEFSYM (Qline, "line");
6359 DEFSYM (Qwave, "wave");
6360 DEFSYM (Qreleased_button, "released-button");
6361 DEFSYM (Qpressed_button, "pressed-button");
6362 DEFSYM (Qnormal, "normal");
6363 DEFSYM (Qextra_light, "extra-light");
6364 DEFSYM (Qlight, "light");
6365 DEFSYM (Qsemi_light, "semi-light");
6366 DEFSYM (Qsemi_bold, "semi-bold");
6367 DEFSYM (Qbold, "bold");
6368 DEFSYM (Qextra_bold, "extra-bold");
6369 DEFSYM (Qultra_bold, "ultra-bold");
6370 DEFSYM (Qoblique, "oblique");
6371 DEFSYM (Qitalic, "italic");
6373 /* The symbols `foreground-color' and `background-color' which can be
6374 used as part of a `face' property. This is for compatibility with
6375 Emacs 20.2. */
6376 DEFSYM (Qbackground_color, "background-color");
6377 DEFSYM (Qforeground_color, "foreground-color");
6379 DEFSYM (Qunspecified, "unspecified");
6380 DEFSYM (QCignore_defface, ":ignore-defface");
6382 /* The symbol `face-alias'. A symbol having that property is an
6383 alias for another face. Value of the property is the name of
6384 the aliased face. */
6385 DEFSYM (Qface_alias, "face-alias");
6387 /* Names of basic faces. */
6388 DEFSYM (Qdefault, "default");
6389 DEFSYM (Qtool_bar, "tool-bar");
6390 DEFSYM (Qfringe, "fringe");
6391 DEFSYM (Qheader_line, "header-line");
6392 DEFSYM (Qscroll_bar, "scroll-bar");
6393 DEFSYM (Qmenu, "menu");
6394 DEFSYM (Qcursor, "cursor");
6395 DEFSYM (Qborder, "border");
6396 DEFSYM (Qmouse, "mouse");
6397 DEFSYM (Qmode_line_inactive, "mode-line-inactive");
6398 DEFSYM (Qvertical_border, "vertical-border");
6400 /* TTY color-related functions (defined in tty-colors.el). */
6401 DEFSYM (Qwindow_divider, "window-divider");
6402 DEFSYM (Qwindow_divider_first_pixel, "window-divider-first-pixel");
6403 DEFSYM (Qwindow_divider_last_pixel, "window-divider-last-pixel");
6404 DEFSYM (Qtty_color_desc, "tty-color-desc");
6405 DEFSYM (Qtty_color_standard_values, "tty-color-standard-values");
6406 DEFSYM (Qtty_color_by_index, "tty-color-by-index");
6408 /* The name of the function used to compute colors on TTYs. */
6409 DEFSYM (Qtty_color_alist, "tty-color-alist");
6411 Vparam_value_alist = list1 (Fcons (Qnil, Qnil));
6412 staticpro (&Vparam_value_alist);
6413 Vface_alternative_font_family_alist = Qnil;
6414 staticpro (&Vface_alternative_font_family_alist);
6415 Vface_alternative_font_registry_alist = Qnil;
6416 staticpro (&Vface_alternative_font_registry_alist);
6418 defsubr (&Sinternal_make_lisp_face);
6419 defsubr (&Sinternal_lisp_face_p);
6420 defsubr (&Sinternal_set_lisp_face_attribute);
6421 #ifdef HAVE_WINDOW_SYSTEM
6422 defsubr (&Sinternal_set_lisp_face_attribute_from_resource);
6423 #endif
6424 defsubr (&Scolor_gray_p);
6425 defsubr (&Scolor_supported_p);
6426 #ifndef HAVE_X_WINDOWS
6427 defsubr (&Sx_load_color_file);
6428 #endif
6429 defsubr (&Sface_attribute_relative_p);
6430 defsubr (&Smerge_face_attribute);
6431 defsubr (&Sinternal_get_lisp_face_attribute);
6432 defsubr (&Sinternal_lisp_face_attribute_values);
6433 defsubr (&Sinternal_lisp_face_equal_p);
6434 defsubr (&Sinternal_lisp_face_empty_p);
6435 defsubr (&Sinternal_copy_lisp_face);
6436 defsubr (&Sinternal_merge_in_global_face);
6437 defsubr (&Sface_font);
6438 defsubr (&Sframe_face_alist);
6439 defsubr (&Sdisplay_supports_face_attributes_p);
6440 defsubr (&Scolor_distance);
6441 defsubr (&Sinternal_set_font_selection_order);
6442 defsubr (&Sinternal_set_alternative_font_family_alist);
6443 defsubr (&Sinternal_set_alternative_font_registry_alist);
6444 defsubr (&Sface_attributes_as_vector);
6445 #ifdef GLYPH_DEBUG
6446 defsubr (&Sdump_face);
6447 defsubr (&Sshow_face_resources);
6448 #endif /* GLYPH_DEBUG */
6449 defsubr (&Sclear_face_cache);
6450 defsubr (&Stty_suppress_bold_inverse_default_colors);
6452 #if defined DEBUG_X_COLORS && defined HAVE_X_WINDOWS
6453 defsubr (&Sdump_colors);
6454 #endif
6456 DEFVAR_LISP ("face-new-frame-defaults", Vface_new_frame_defaults,
6457 doc: /* List of global face definitions (for internal use only.) */);
6458 Vface_new_frame_defaults = Qnil;
6460 DEFVAR_LISP ("face-default-stipple", Vface_default_stipple,
6461 doc: /* Default stipple pattern used on monochrome displays.
6462 This stipple pattern is used on monochrome displays
6463 instead of shades of gray for a face background color.
6464 See `set-face-stipple' for possible values for this variable. */);
6465 Vface_default_stipple = build_pure_c_string ("gray3");
6467 DEFVAR_LISP ("tty-defined-color-alist", Vtty_defined_color_alist,
6468 doc: /* An alist of defined terminal colors and their RGB values.
6469 See the docstring of `tty-color-alist' for the details. */);
6470 Vtty_defined_color_alist = Qnil;
6472 DEFVAR_LISP ("scalable-fonts-allowed", Vscalable_fonts_allowed,
6473 doc: /* Allowed scalable fonts.
6474 A value of nil means don't allow any scalable fonts.
6475 A value of t means allow any scalable font.
6476 Otherwise, value must be a list of regular expressions. A font may be
6477 scaled if its name matches a regular expression in the list.
6478 Note that if value is nil, a scalable font might still be used, if no
6479 other font of the appropriate family and registry is available. */);
6480 Vscalable_fonts_allowed = Qnil;
6482 DEFVAR_LISP ("face-ignored-fonts", Vface_ignored_fonts,
6483 doc: /* List of ignored fonts.
6484 Each element is a regular expression that matches names of fonts to
6485 ignore. */);
6486 Vface_ignored_fonts = Qnil;
6488 DEFVAR_LISP ("face-remapping-alist", Vface_remapping_alist,
6489 doc: /* Alist of face remappings.
6490 Each element is of the form:
6492 (FACE . REPLACEMENT),
6494 which causes display of the face FACE to use REPLACEMENT instead.
6495 REPLACEMENT is a face specification, i.e. one of the following:
6497 (1) a face name
6498 (2) a property list of attribute/value pairs, or
6499 (3) a list in which each element has the form of (1) or (2).
6501 List values for REPLACEMENT are merged to form the final face
6502 specification, with earlier entries taking precedence, in the same as
6503 as in the `face' text property.
6505 Face-name remapping cycles are suppressed; recursive references use
6506 the underlying face instead of the remapped face. So a remapping of
6507 the form:
6509 (FACE EXTRA-FACE... FACE)
6513 (FACE (FACE-ATTR VAL ...) FACE)
6515 causes EXTRA-FACE... or (FACE-ATTR VAL ...) to be _merged_ with the
6516 existing definition of FACE. Note that this isn't necessary for the
6517 default face, since every face inherits from the default face.
6519 If this variable is made buffer-local, the face remapping takes effect
6520 only in that buffer. For instance, the mode my-mode could define a
6521 face `my-mode-default', and then in the mode setup function, do:
6523 (set (make-local-variable \\='face-remapping-alist)
6524 \\='((default my-mode-default)))).
6526 Because Emacs normally only redraws screen areas when the underlying
6527 buffer contents change, you may need to call `redraw-display' after
6528 changing this variable for it to take effect. */);
6529 Vface_remapping_alist = Qnil;
6531 DEFVAR_LISP ("face-font-rescale-alist", Vface_font_rescale_alist,
6532 doc: /* Alist of fonts vs the rescaling factors.
6533 Each element is a cons (FONT-PATTERN . RESCALE-RATIO), where
6534 FONT-PATTERN is a font-spec or a regular expression matching a font name, and
6535 RESCALE-RATIO is a floating point number to specify how much larger
6536 (or smaller) font we should use. For instance, if a face requests
6537 a font of 10 point, we actually use a font of 10 * RESCALE-RATIO point. */);
6538 Vface_font_rescale_alist = Qnil;
6540 #ifdef HAVE_WINDOW_SYSTEM
6541 defsubr (&Sbitmap_spec_p);
6542 defsubr (&Sx_list_fonts);
6543 defsubr (&Sinternal_face_x_get_resource);
6544 defsubr (&Sx_family_fonts);
6545 #endif